STK++ 0.9.13
STK::DataFrame Class Reference

DataFrame is a List of Variable with the same number of rows. More...

#include <STK_DataFrame.h>

Inheritance diagram for STK::DataFrame:
Inheritance graph

Public Types

typedef IContainer2D< UnknownSize, UnknownSizeBase2D
 Type of the Base container.
 
typedef List1D< IVariable * > Base
 Type for the list container.
 
typedef TRange< UnknownSizeRowRange
 Type of the Range for the rows.
 
typedef TRange< UnknownSizeColRange
 Type of the Range for the columns.
 
- Public Types inherited from STK::IContainer2D< UnknownSize, UnknownSize >
typedef TRange< SizeRows_RowRange
 Type of the Range for the rows.
 
typedef TRange< SizeCols_ColRange
 Type of the Range for the columns.
 

Public Member Functions

 DataFrame ()
 Default Constructor, empty table.
 
 DataFrame (DataFrame const &T, bool ref=false)
 Copy constructor If ref is true, only references of the variables are copied into the DataFrame.
 
virtual ~DataFrame ()
 Destructor.
 
RowRange constrows () const
 
int beginRows () const
 
int endRows () const
 
int sizeRows () const
 
ColRange cols () const
 
int beginCols () const
 
int endCols () const
 
int sizeCols () const
 
int lastIdxRows () const
 
int lastIdxCols () const
 
bool empty () const
 
void clear ()
 Clear the object.
 
IVariable *& elt (int const &i)
 access to an element.
 
IVariable *const elt (int const &i) const
 access to a constant element.
 
DataFrameoperator= (DataFrame const &T)
 Operator = : overwrite the DataFrame with T.
 
void resize (Range const &I, Range const &J)
 resize the container:
 
void shift (int const &cbeg=1)
 New beginning index for the object.
 
void shift (int const &rbeg, int const &cbeg)
 New beginning index for the object.
 
void popBackCols (int const &n)
 Delete n columns of the container.
 
void eraseCols (int pos, int const &n=1)
 Delete n columns at the position of the container.
 
void swapCols (int j1, int j2)
 Swapping the j1th column and the j2th column.
 
void popBackRows (int const &n)
 Dell last rows of the container.
 
void eraseRows (int pos, int const &n=1)
 Delete n rows at the position pos to the container.
 
void insertVariable (int pos, IVariable *const &V)
 Insert a Vartiable at the specified position to the container.
 
void pushBackVariable (IVariable *const &V)
 Append a DataFrame back.
 
void pushFrontVariable (IVariable *const &V)
 Append a DataFrame front.
 
void merge (DataFrame const &D)
 merge this DataFrame with D.
 
void insertDataFrame (int pos, DataFrame const &D)
 Insert a DataFrame at the specified position to the container.
 
void pushBackDataFrame (DataFrame const &D)
 Append a DataFrame back.
 
void pushFrontDataFrame (DataFrame const &D)
 Append a DataFrame front.
 
void writeDataFrame (ostream &os, int const &left, int const &right) const
 write a DataFrame to the output stream os.
 
- Public Member Functions inherited from STK::IContainer2D< UnknownSize, UnknownSize >
 IContainer2D ()
 Default constructor.
 
 IContainer2D (RowRange const &I, ColRange const &J)
 Constructor with specified ranges.
 
 IContainer2D (IContainer2D const &T)
 Copy constructor.
 
 ~IContainer2D ()
 destructor.
 
ColRange constcols () const
 
int beginCols () const
 
int endCols () const
 
int sizeCols () const
 
RowRange constrows () const
 
int beginRows () const
 
int endRows () const
 
int sizeRows () const
 
int lastIdxCols () const
 
int lastIdxRows () const
 
bool empty () const
 

Protected Member Functions

void freeMem ()
 function for memory deallocation.
 
void freeCols ()
 function for row memory deallocation.
 
void pushBackCols (int const &n=1)
 Add columns to the container.
 
void insertCols (int pos, int const &n=1)
 Insert columns at the specified position to the container.
 
void pushBackRows (int const &n=1)
 Add n rows to the container.
 
void insertRows (int pos, int const &n=1)
 Insert n rows at the ith position of the container.
 
- Protected Member Functions inherited from STK::List1D< IVariable * >
 List1D (Cell *const &p_first, Cell *const &p_last, Range const &J)
 constructor by reference, ref_=1.
 
void initialize (Range const &I)
 Protected function for initialization.
 
void freeMem ()
 Protected function for deallocation.
 
 List1D ()
 Default constructor : empty List.
 
 List1D (Range const &I)
 constructor with specified Range.
 
 List1D (Range const &I, Type const &v)
 Misc constructor, initialization with a constant.
 
 List1D (List1D< Type > const &T)
 Copy constructor.
 
 List1D (List1D< Type > const &T, Range const &J, bool ref=true)
 access to many elements.
 
RowRange constrows () const
 
int beginRows () const
 
int endRows () const
 
int sizeRows () const
 
ColRange cols () const
 
int beginCols () const
 
int endCols () const
 
int sizeCols () const
 
int lastIdxRows () const
 
int lastIdxCols () const
 
 ~List1D ()
 destructor.
 
Cell const *const p_begin () const
 
Cell const *const p_lastIdx () const
 
Typeelt1Impl (int pos)
 access to one element.
 
Type constelt1Impl (int pos) const
 access to one element const.
 
List1D subImpl (Range const &J) const
 access to many elements.
 
void shiftImpl (int const &beg)
 New first index for the object.
 
void clear ()
 Clear the object.
 
void pushBack (int const &n=1)
 Add n Elts to the container.
 
void insert (Range const &I, Type const &v)
 Insert element v in the range I of the List1D.
 
void merge (List1D const &other)
 merge this with other.
 
void insertElt (int pos, int const &n=1)
 Insert n elts at the position pos of the container.
 
void popBack (int const &n=1)
 Delete n last elements of the container.
 
void erase (int pos, int const &n=1)
 Delete n elts at the pos index to the container.
 
void swap (int const &j1, int const &j2)
 Swapping the j1th column and the j2th column.
 
List1Doperator= (const List1D &T)
 operator = : overwrite the List1D with T.
 
List1D< Type > & operator= (Type const &v)
 operator= : set the container to a constant value.
 
List1D< Type > & resizeImpl (Range const &I)
 Resize the container.
 
int begin () const
 
int end () const
 
int size () const
 
int lastIdx () const
 
- Protected Member Functions inherited from STK::ITContainer1D< Derived >
 ITContainer1D ()
 Default constructor.
 
 ITContainer1D (RowRange const &I)
 constructor with a specified range.
 
 ~ITContainer1D ()
 destructor.
 
void exchange (ITContainer1D &T)
 exchange this container with T
 
void setRange (RowRange const &I=RowRange())
 Set range of the rows of the container.
 
void incRange (int n=1)
 increment the range of the container (can be negative).
 
void incFirst (int n=1)
 increment the beginning of the container (can be negative).
 
void decFirst (int n=1)
 decrement the beginning of the container.
 
void incLast (int n=1)
 increment the end of the container (can be negative).
 
void decLast (int n=1)
 decrement the end of the container.
 
RowRange constrange () const
 
int begin () const
 
int end () const
 
int size () const
 
int lastIdx () const
 
Iterator beginIterator ()
 
Iterator endIterator ()
 
ConstIterator beginConstIterator () const
 
ConstIterator endConstIterator () const
 
ReverseIterator rbeginIterator ()
 
ReverseIterator rendIterator ()
 
ConstReverseIterator rbeginConstIterator () const
 
ConstReverseIterator rendConstIterator () const
 
bool empty () const
 Is there some data ?
 
Typeelt (int i)
 
TypeConst elt (int i) const
 
Typeoperator[] (int i)
 
TypeConst operator[] (int i) const
 
Typeat (int i)
 
TypeConst at (int i) const
 
SubVector sub (Range const &I) const
 Access to many elements.
 
Typefront ()
 
TypeConst front () const
 
Typeback ()
 
TypeConst back () const
 
void shift (int beg)
 
Derived & resize (Range const &I=RowRange())
 
- Protected Member Functions inherited from STK::IRecursiveTemplate< Derived >
 IRecursiveTemplate ()
 constructor.
 
 ~IRecursiveTemplate ()
 destructor.
 
Derived & asDerived ()
 static cast : return a reference of this with a cast to the derived class.
 
Derived constasDerived () const
 static cast : return a const reference of this with a cast to the derived class.
 
Derived * asPtrDerived ()
 static cast : return a ptr on a Derived of this with a cast to the derived class.
 
Derived constasPtrDerived () const
 static cast : return a ptr on a constant Derived of this with a cast to the derived class.
 
Derived * clone () const
 create a leaf using the copy constructor of the Derived class.
 
Derived * clone (bool isRef) const
 create a leaf using the copy constructor of the Derived class and a flag determining if the clone is a reference or not.
 
- Protected Member Functions inherited from STK::TRef<-1 >
 TRef (bool ref)
 Default constructor We have to specify the member ref_.
 
 ~TRef ()
 destructor.
 
bool isRef () const
 
bool ref () const
 
void exchange (TRef const &T)
 swap this with the container T.
 
void setRef (bool ref) const
 Modify the container : can become a reference or the owner of the data.
 
- Protected Member Functions inherited from STK::IContainer2D< UnknownSize, UnknownSize >
void shift (int rbeg, int cbeg)
 Set the first index of the rows and columns.
 
void setRanges (RowRange const &I=RowRange(), ColRange const &J=ColRange())
 Set the ranges of the container.
 
void setRows (RowRange const &I=RowRange())
 Set the range of the number of rows.
 
void shiftRows (int beg)
 Set the first index of the rows.
 
void incRangeRows (int inc)
 Increment the range of the number of rows.
 
void incBeginRows (int inc)
 Increment the first index of the number of rows.
 
void decBeginRows (int dec)
 Decrement the first index of the number of rows.
 
void incEndRows (int inc)
 Increment the end of the number of rows.
 
void decEndRows (int dec)
 Decrement the end of the number of rows.
 
void setCols (ColRange const &J=ColRange())
 Set the columns range.
 
void shiftCols (int beg)
 Shift the columns first index to beg.
 
void incRangeCols (int inc)
 Increment the columns range.
 
void incBeginCols (int inc)
 increment the first index of the columns.
 
void decBeginCols (int dec)
 Decrement the columns first index.
 
void incEndCols (int inc)
 Increment the last index of the columns.
 
void decEndCols (int dec)
 Decrement the last index of the columns.
 
void exchange (IContainer2D &T)
 exchange this container with T
 
void incLastIdxRows (int inc)
 Increment the end of the number of rows.
 
void decLastIdxRows (int dec)
 Decrement the end of the number of rows.
 
void incLastIdxCols (int inc)
 Increment the last index of the columns.
 
void decLastIdxCols (int dec)
 Decrement the last index of the columns.
 
 IContainer2D ()
 Default constructor.
 
 IContainer2D (RowRange const &I, ColRange const &J)
 Constructor with specified ranges.
 
 IContainer2D (IContainer2D const &T)
 Copy constructor.
 
 ~IContainer2D ()
 destructor.
 
ColRange constcols () const
 
int beginCols () const
 
int endCols () const
 
int sizeCols () const
 
RowRange constrows () const
 
int beginRows () const
 
int endRows () const
 
int sizeRows () const
 
int lastIdxCols () const
 
int lastIdxRows () const
 
bool empty () const
 

Additional Inherited Members

- Protected Types inherited from STK::List1D< IVariable * >
enum  
 
typedef hidden::Traits< List1D< IVariable * > >::Type Type
 
typedef hidden::Traits< List1D< IVariable * > >::TypeConst TypeConst
 
typedef hidden::Traits< List1D< IVariable * > >::RowRange RowRange
 
typedef hidden::Traits< List1D< IVariable * > >::ColRange ColRange
 
typedef ITContainer1D< List1D< Type > > Base
 
typedef CellHo< TypeCell
 
typedef hidden::Traits< List1D< IVariable * > >::Iterator Iterator
 
typedef hidden::Traits< List1D< IVariable * > >::ConstIterator ConstIterator
 
typedef hidden::Traits< List1D< IVariable * > >::ReverseIterator ReverseIterator
 
typedef hidden::Traits< List1D< IVariable * > >::ConstReverseIterator ConstReverseIterator
 
typedef Iterator iterator
 
typedef ConstIterator const_iterator
 
typedef ReverseIterator reverse_iterator
 
typedef ConstReverseIterator const_reverse_iterator
 
- Protected Types inherited from STK::ITContainer1D< Derived >
enum  { size_ = hidden::Traits<Derived>::size_ }
 
typedef hidden::Traits< Derived >::Type Type
 
typedef hidden::Traits< Derived >::TypeConst TypeConst
 
typedef hidden::Traits< Derived >::RowRange RowRange
 
typedef hidden::Traits< Derived >::ColRange ColRange
 
typedef hidden::Traits< Derived >::Row Row
 
typedef hidden::Traits< Derived >::Col Col
 
typedef hidden::Traits< Derived >::SubVector SubVector
 
typedef hidden::Traits< Derived >::Iterator Iterator
 
typedef hidden::Traits< Derived >::ConstIterator ConstIterator
 
typedef hidden::Traits< Derived >::ReverseIterator ReverseIterator
 
typedef hidden::Traits< Derived >::ConstReverseIterator ConstReverseIterator
 
- Protected Types inherited from STK::IContainer2D< UnknownSize, UnknownSize >
typedef TRange< SizeRows_RowRange
 Type of the Range for the rows.
 
typedef TRange< SizeCols_ColRange
 Type of the Range for the columns.
 
- Protected Attributes inherited from STK::List1D< IVariable * >
Cellp_begin_
 First Element of the List.
 
Cellp_last_
 Last Element of the List.
 

Detailed Description

DataFrame is a List of Variable with the same number of rows.

This is thus also a 2D container.

A DataFrame inherit from the class List1D and and IContainer2D. It is essentially a List, except that each column (the variables) possess the same dimension.

Each Cell of the List1D contain a pointer on a Variable.

Definition at line 58 of file STK_DataFrame.h.

Member Typedef Documentation

◆ Base

Type for the list container.

Definition at line 64 of file STK_DataFrame.h.

◆ Base2D

Type of the Base container.

Definition at line 62 of file STK_DataFrame.h.

◆ ColRange

Type of the Range for the columns.

Definition at line 68 of file STK_DataFrame.h.

◆ RowRange

Type of the Range for the rows.

Definition at line 66 of file STK_DataFrame.h.

Constructor & Destructor Documentation

◆ DataFrame() [1/2]

STK::DataFrame::DataFrame ( )

Default Constructor, empty table.

Definition at line 41 of file STK_DataFrame.cpp.

41: Base(), Base2D() {}
List1D< IVariable * > Base
Type for the list container.
IContainer2D< UnknownSize, UnknownSize > Base2D
Type of the Base container.

◆ DataFrame() [2/2]

STK::DataFrame::DataFrame ( DataFrame const T,
bool  ref = false 
)

Copy constructor If ref is true, only references of the variables are copied into the DataFrame.

Parameters
Tthe DataFrame to copy
reftrue if we want to wrap the variables of T

Definition at line 44 of file STK_DataFrame.cpp.

45 : Base(T.range())
46 , Base2D(T)
47{
48 // we need to copy explicitly the data
49 for (int j=begin(); j<end(); j++) // for all columns
50 if (T[j]) // if there is data
51 elt(j) = T[j]->clone(ref); // set the adress of a clone
52}
IVariable *& elt(int const &i)
access to an element.
virtual IVariable * clone(bool ref=false) const =0

References STK::List1D< IVariable * >::begin(), STK::IRecursiveTemplate< Derived >::clone(), elt(), STK::List1D< IVariable * >::end(), and STK::TRef<-1 >::ref().

◆ ~DataFrame()

STK::DataFrame::~DataFrame ( )
virtual

Destructor.

Definition at line 55 of file STK_DataFrame.cpp.

55{ if (!isRef()) freeCols();}
void freeCols()
function for row memory deallocation.
bool isRef() const

References freeCols(), and STK::TRef<-1 >::isRef().

Member Function Documentation

◆ beginCols()

◆ beginRows()

int STK::DataFrame::beginRows ( ) const
inline

◆ clear()

void STK::DataFrame::clear ( )

Clear the object.

Definition at line 58 of file STK_DataFrame.cpp.

59{
60 freeMem();
61 // set default range for list
63 // set default range for container2D
65}
void freeMem()
function for memory deallocation.
void setRanges(RowRange const &I=RowRange(), ColRange const &J=ColRange())
Set the ranges of the container.
void setRange(RowRange const &I=RowRange())
Set range of the rows of the container.

References freeMem(), STK::ITContainer1D< Derived >::setRange(), and STK::IContainer2D< UnknownSize, UnknownSize >::setRanges().

◆ cols()

ColRange STK::DataFrame::cols ( ) const
inline
Returns
the Horizontal range (1 column)

Definition at line 92 of file STK_DataFrame.h.

92{ return ColRange(1);}
TRange< UnknownSize > ColRange
Type of the Range for the columns.

Referenced by operator=(), and resize().

◆ elt() [1/2]

◆ elt() [2/2]

IVariable *const STK::DataFrame::elt ( int const i) const
inline

access to a constant element.

Set the method elt as a public method.

Definition at line 114 of file STK_DataFrame.h.

114{ return Base::elt(i);}

References STK::ITContainer1D< Derived >::elt().

◆ empty()

bool STK::DataFrame::empty ( ) const
inline
Returns
true if the container is empty, false otherwise

Definition at line 107 of file STK_DataFrame.h.

107{ return Base::empty();}
bool empty() const
Is there some data ?

References STK::ITContainer1D< Derived >::empty().

Referenced by insertDataFrame(), and pushBackDataFrame().

◆ endCols()

int STK::DataFrame::endCols ( ) const
inline
Returns
the index of the ending column

Definition at line 96 of file STK_DataFrame.h.

References STK::IContainer2D< UnknownSize, UnknownSize >::endCols().

Referenced by STK::ExportToCsv::ExportToCsv().

◆ endRows()

int STK::DataFrame::endRows ( ) const
inline
Returns
the ending index of the elements

Definition at line 87 of file STK_DataFrame.h.

References STK::IContainer2D< UnknownSize, UnknownSize >::endRows().

◆ eraseCols()

void STK::DataFrame::eraseCols ( int  pos,
int const n = 1 
)

Delete n columns at the position of the container.

Definition at line 178 of file STK_DataFrame.cpp.

179{
180 // if n<=0 nothing to do
181 if (n<=0) return;
182 // check conditions
183 if (pos<begin())
185 if (pos>lastIdx())
187 if (lastIdx() < pos+n-1)
189 // for all columns, delete variables
190 for (int j=pos+n-1; j>=pos; j--)
191 if (elt(j)) { delete elt(j);}
192 // delete elements of the List1D
193 erase(pos, n);
194 // update cols_
196 // if it was the last col, free mem
197 if (this->sizeCols() == 0) freeMem();
198}
#define STKOUT_OF_RANGE_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:102
int sizeCols() const
void eraseCols(int pos, int const &n=1)
Delete n columns at the position of the container.
void decLastIdxCols(int dec)
Decrement the last index of the columns.
void erase(int pos, int const &n=1)
Delete n elts at the pos index to the container.
Definition STK_List1D.h:475

References STK::List1D< IVariable * >::begin(), STK::IContainer2D< UnknownSize, UnknownSize >::decLastIdxCols(), elt(), STK::List1D< IVariable * >::erase(), eraseCols(), freeMem(), STK::List1D< IVariable * >::lastIdx(), sizeCols(), and STKOUT_OF_RANGE_2ARG.

Referenced by eraseCols().

◆ eraseRows()

void STK::DataFrame::eraseRows ( int  pos,
int const n = 1 
)

Delete n rows at the position pos to the container.

Parameters
posposition of the rows to delete
nnumber of rows to delete

Definition at line 443 of file STK_DataFrame.cpp.

444{
445 // if n<=0 nothing to do
446 if (n<=0) return;
447 if (beginRows() > pos)
449 if (lastIdxRows() < pos)
451 if (lastIdxRows() < pos+n-1)
453 // for each variable erase elts
454 for (int j=begin(); j<=lastIdx(); j++)
455 if (elt(j)) { elt(j)->erase(pos, n);}
456 // update rows_
458}
void eraseRows(int pos, int const &n=1)
Delete n rows at the position pos to the container.
int beginRows() const
int lastIdxRows() const
void decLastIdxRows(int dec)
Decrement the end of the number of rows.
virtual void erase(int pos, int n=1)=0
Delete n elements at the pos index from the container.

References STK::List1D< IVariable * >::begin(), beginRows(), STK::IContainer2D< UnknownSize, UnknownSize >::decLastIdxRows(), elt(), STK::IVariable::erase(), eraseRows(), STK::List1D< IVariable * >::lastIdx(), lastIdxRows(), and STKOUT_OF_RANGE_2ARG.

Referenced by eraseRows().

◆ freeCols()

void STK::DataFrame::freeCols ( )
protected

function for row memory deallocation.

Definition at line 472 of file STK_DataFrame.cpp.

473{
474 if (isRef()) return;
475 // for all columns
476 for (int j=begin(); j<=lastIdx(); j++)
477 if (elt(j)) // if there is mem allocated
478 { delete elt(j); elt(j) = 0;}
479 // set default range
480 setRows();
481}
void setRows(RowRange const &I=RowRange())
Set the range of the number of rows.

References STK::List1D< IVariable * >::begin(), elt(), STK::TRef<-1 >::isRef(), STK::List1D< IVariable * >::lastIdx(), and STK::IContainer2D< UnknownSize, UnknownSize >::setRows().

Referenced by freeMem(), and ~DataFrame().

◆ freeMem()

void STK::DataFrame::freeMem ( )
protected

function for memory deallocation.

Definition at line 461 of file STK_DataFrame.cpp.

462{
463 if (isRef()) return;
464 // liberate variables
465 freeCols();
467 setRows();
468 setCols();
469}
void setCols(ColRange const &J=ColRange())
Set the columns range.
void freeMem()
Protected function for deallocation.
Definition STK_List1D.h:651

References freeCols(), STK::List1D< IVariable * >::freeMem(), STK::TRef<-1 >::isRef(), STK::IContainer2D< UnknownSize, UnknownSize >::setCols(), and STK::IContainer2D< UnknownSize, UnknownSize >::setRows().

Referenced by clear(), eraseCols(), and popBackCols().

◆ insertCols()

void STK::DataFrame::insertCols ( int  pos,
int const n = 1 
)
protected

Insert columns at the specified position to the container.

Definition at line 387 of file STK_DataFrame.cpp.

388{
389 if (n <= 0) return; // if n<=0 nothing to do
390 // check conditions
391 if (pos<begin())
393 if (pos>lastIdx())
394 { STKOUT_OF_RANGE_2ARG(Dataframe::insertCols,pos, n,pos>lastIdx());}
395 // insert n elements in list1D
396 insert(Range(pos, n), 0);
397 // update IContainer2D
399}
void insertCols(int pos, int const &n=1)
Insert columns at the specified position to the container.
void incLastIdxCols(int inc)
Increment the last index of the columns.
void insert(Range const &I, Type const &v)
Insert element v in the range I of the List1D.
Definition STK_List1D.h:370
TRange< UnknownSize > Range
Definition STK_Range.h:59

References STK::List1D< IVariable * >::begin(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxCols(), STK::List1D< IVariable * >::insert(), insertCols(), STK::List1D< IVariable * >::lastIdx(), and STKOUT_OF_RANGE_2ARG.

Referenced by insertCols().

◆ insertDataFrame()

void STK::DataFrame::insertDataFrame ( int  pos,
DataFrame const D 
)

Insert a DataFrame at the specified position to the container.

Definition at line 292 of file STK_DataFrame.cpp.

293{
294 if (D.empty()) return;
295 // is this structure just a pointer?
296 if (this->isRef())
297 { STKRUNTIME_ERROR_1ARG(DataFrame::insertDataFrame,pos,cannot operate on reference);}
298 if (begin()>pos)
300 if (lastIdx()+1<pos)
302 // List1D
303 insertElt(pos, D.sizeCols());
304 // insert all columns of D
305 for (int i = D.beginCols(), icol = pos; i <=D.lastIdxCols(); i++, icol++)
306 {
307 if (D.elt(i))
308 {
309 elt(icol) = D.elt(i)->clone();
310 elt(icol)->shift(beginRows());
311 }
312 else { elt(icol) = 0;}
313 }
314 // update LastHo
315 incLastIdxCols(D.sizeCols());
316 // update rows with NA values
317 int inc = sizeRows() - D.sizeRows();
318 if (inc == 0) return; // same size
319 if (inc > 0) // D has less rows
320 { // put NA values to the inserted columns
321 for (int i= pos+D.sizeCols()-1; i >=pos; i--)
322 if (elt(i)) { elt(i)->pushBackNAValues(inc);}
323 }
324 else
325 { // put NA values to the oter columns
326 for (int i=this->beginCols(); i <pos; i++)
327 if (elt(i)) { elt(i)->pushBackNAValues(-inc);}
328 for (int i=pos+D.sizeCols(); i <=this->lastIdx(); i++)
329 if (elt(i)) { elt(i)->pushBackNAValues(-inc);}
330 // update LastVe
331 incLastIdxRows(-inc);
332 }
333}
#define STKOUT_OF_RANGE_1ARG(Where, Arg, Error)
Definition STK_Macros.h:93
#define STKRUNTIME_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:129
int beginCols() const
void insertDataFrame(int pos, DataFrame const &D)
Insert a DataFrame at the specified position to the container.
int sizeRows() const
void incLastIdxRows(int inc)
Increment the end of the number of rows.
virtual void shift(int beg)=0
New first index for the object.
virtual void pushBackNAValues(int n=1)=0
push back n NA values.
void insertElt(int pos, int const &n=1)
Insert n elts at the position pos of the container.
Definition STK_List1D.h:414

References STK::List1D< IVariable * >::begin(), beginCols(), beginRows(), STK::IVariable::clone(), elt(), empty(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxCols(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxRows(), insertDataFrame(), STK::List1D< IVariable * >::insertElt(), STK::TRef<-1 >::isRef(), STK::List1D< IVariable * >::lastIdx(), lastIdxCols(), STK::IVariable::pushBackNAValues(), STK::IVariable::shift(), sizeCols(), sizeRows(), STKOUT_OF_RANGE_1ARG, and STKRUNTIME_ERROR_1ARG.

Referenced by insertDataFrame(), and pushFrontDataFrame().

◆ insertRows()

void STK::DataFrame::insertRows ( int  pos,
int const n = 1 
)
protected

Insert n rows at the ith position of the container.

Definition at line 416 of file STK_DataFrame.cpp.

417{
418 // if n<=0 nothing to do
419 if (n<=0) return;
420 if (beginRows() > pos)
422 if (lastIdxRows()+1 < pos)
424 // insert rows to each variables
425 for (int j=begin(); j<=lastIdx(); j++)
426 if (elt(j)) { elt(j)->insertElt(pos, n);}
427 // update rows_
429}
void insertRows(int pos, int const &n=1)
Insert n rows at the ith position of the container.
virtual void insertElt(int pos, int n=1)=0
Insert n elements at the position pos of the container.

References STK::List1D< IVariable * >::begin(), beginRows(), elt(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxRows(), STK::IVariable::insertElt(), insertRows(), STK::List1D< IVariable * >::lastIdx(), lastIdxRows(), and STKOUT_OF_RANGE_2ARG.

Referenced by insertRows().

◆ insertVariable()

void STK::DataFrame::insertVariable ( int  pos,
IVariable *const V 
)

Insert a Vartiable at the specified position to the container.

Parameters
posthe position in the container
Vthe Variable to insert

Definition at line 201 of file STK_DataFrame.cpp.

202{
203 // List1D
205 elt(pos) = V;
206 // the variable have to be in the same range
207 elt(pos)->shift(beginRows());
208 // update horizontal range (the number of column)
210
211 // update rows with NA values
212 int inc = sizeRows() - V->size();
213 if (inc == 0) return; // same size
214 if (inc > 0) // V has less rows
215 { // put NA values to the inserted column
216 elt(pos)->pushBackNAValues(inc);
217 }
218 else
219 { // put NA values to the other columns
220 for (int i=this->beginCols(); i <pos; i++)
221 if (elt(i)) { elt(i)->pushBackNAValues(-inc);}
222 for (int i=pos+1; i <=this->lastIdxCols(); i++)
223 if (elt(i)) { elt(i)->pushBackNAValues(-inc);}
224 // update LastVe
225 incLastIdxRows(-inc);
226 }
227}
int lastIdxCols() const

References beginCols(), beginRows(), elt(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxCols(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxRows(), STK::List1D< Type_ >::insertElt(), lastIdxCols(), STK::IVariable::pushBackNAValues(), STK::IVariable::shift(), STK::IVariable::size(), and sizeRows().

Referenced by pushFrontVariable().

◆ lastIdxCols()

int STK::DataFrame::lastIdxCols ( ) const
inline
Returns
the index of the last element

Definition at line 103 of file STK_DataFrame.h.

103{ return baseIdx;}
const int baseIdx
base index of the containers created in STK++.

References STK::baseIdx.

Referenced by insertDataFrame(), insertVariable(), STK::operator<<(), popBackCols(), pushBackVariable(), resize(), and STK::DataFrameToArray2D< Type >::run().

◆ lastIdxRows()

int STK::DataFrame::lastIdxRows ( ) const
inline
Returns
the index of the last element

Definition at line 101 of file STK_DataFrame.h.

101{ return this->lastIdx();}

References STK::List1D< IVariable * >::lastIdx().

Referenced by eraseRows(), insertRows(), resize(), and writeDataFrame().

◆ merge()

void STK::DataFrame::merge ( DataFrame const D)

merge this DataFrame with D.

Definition at line 256 of file STK_DataFrame.cpp.

257{
258 if (other.empty()) return;
259 // is this structure just a pointer?
260 if (this->isRef())
261 { STKRUNTIME_ERROR_NO_ARG(DataFrame::merge(other),*this is a reference.);}
262 // is T just a pointer?
263 if (other.isRef())
264 { STKRUNTIME_ERROR_NO_ARG(DataFrame::merge(other),other is a reference.);}
265 // break const reference
266 DataFrame& otherRef = const_cast<DataFrame&>(other);
267 // update rows with NA values
268 int pos(lastIdx()+1), inc = sizeRows() - otherRef.sizeRows();
269
270 otherRef.shift(beginRows(), beginCols());
271 Base::merge(otherRef);
272
273 if (inc == 0) return; // same size
274
275 if (inc > 0) // other has less rows
276 { // put NA values to the merged columns
277 for (int i= lastIdx(); i >=pos; i--)
278 if (elt(i)) { elt(i)->pushBackNAValues(inc);}
279 }
280 else
281 { // put NA values to the existing columns
282 for (int i=this->begin(); i <pos; i++)
283 if (elt(i)) { elt(i)->pushBackNAValues(-inc);}
284
285 // update LastVe
286 incLastIdxRows(-inc);
287 }
288
289}
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
void merge(DataFrame const &D)
merge this DataFrame with D.
DataFrame()
Default Constructor, empty table.
void merge(List1D const &other)
merge this with other.
Definition STK_List1D.h:379

References STK::List1D< IVariable * >::begin(), beginCols(), beginRows(), elt(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxRows(), STK::TRef<-1 >::isRef(), STK::List1D< IVariable * >::lastIdx(), merge(), STK::List1D< IVariable * >::merge(), STK::IVariable::pushBackNAValues(), sizeRows(), and STKRUNTIME_ERROR_NO_ARG.

Referenced by merge().

◆ operator=()

DataFrame & STK::DataFrame::operator= ( DataFrame const T)

Operator = : overwrite the DataFrame with T.

Definition at line 68 of file STK_DataFrame.cpp.

69{
70 // Resize if necessary.
71 if (sizeCols() != T.sizeCols()) Base::resize(T.cols());
72
73 // Copy without overlapping.
74 if ((T.begin()>=begin()))
75 {
76 for (int jt=T.begin(), j=begin(); jt<=T.lastIdx(); j++, jt++)
77 {
78 // clear old mem if any
79 if (elt(j)) delete elt(j);
80 // if there is a variable, create a copy
81 if (T[jt]) elt(j) = T[jt]->clone();
82 else elt(j) = 0;
83 }
84 }
85 else
86 {
87 for (int jt=T.lastIdx(), j=lastIdx(); jt>=T.begin(); j--, jt--)
88 {
89 // clear old mem if any
90 if (elt(j)) delete elt(j);
91 // if there is a variable, create a copy
92 if (T[jt]) elt(j) = T[jt]->clone();
93 else elt(j) = 0;
94 }
95 }
96 return *this;
97}
Derived & resize(Range const &I=RowRange())

References STK::List1D< IVariable * >::begin(), STK::List1D< Type_ >::begin(), STK::IRecursiveTemplate< Derived >::clone(), cols(), elt(), STK::List1D< IVariable * >::lastIdx(), STK::List1D< Type_ >::lastIdx(), STK::ITContainer1D< Derived >::resize(), and sizeCols().

◆ popBackCols()

void STK::DataFrame::popBackCols ( int const n)

Delete n columns of the container.

Definition at line 159 of file STK_DataFrame.cpp.

160{
161 // if n<=0 nothing to do
162 if (n<=0) return;
163 // if there is columns to erase
164 if (sizeCols()<n)
166 // for all columns, delete variables
167 for (int j=lastIdxCols() - n +1; j<=lastIdxCols(); j++)
168 if (elt(j)) delete elt(j);
169 // popBack() of List1D
170 Base::popBack(n);
171 // update IContainer2D
173 // if it was the last elt, free mem
174 if (this->sizeCols() == 0) freeMem();
175}
void popBackCols(int const &n)
Delete n columns of the container.
void popBack(int const &n=1)
Delete n last elements of the container.
Definition STK_List1D.h:458

References STK::IContainer2D< UnknownSize, UnknownSize >::decLastIdxCols(), elt(), freeMem(), lastIdxCols(), STK::List1D< IVariable * >::popBack(), popBackCols(), sizeCols(), and STKOUT_OF_RANGE_1ARG.

Referenced by popBackCols(), and resize().

◆ popBackRows()

void STK::DataFrame::popBackRows ( int const n)

Dell last rows of the container.

Parameters
nnumber of rows to delete

Definition at line 432 of file STK_DataFrame.cpp.

433{
434 if (sizeRows() < n)
436 // del last row to each variable
437 for (int j=begin(); j<=lastIdx(); j++)
438 if (elt(j)) { elt(j)->popBack(n);}
440}
void popBackRows(int const &n)
Dell last rows of the container.
virtual void popBack(int n=1)=0
Delete n last elements of the container.

References STK::List1D< IVariable * >::begin(), STK::IContainer2D< UnknownSize, UnknownSize >::decLastIdxRows(), elt(), STK::List1D< IVariable * >::lastIdx(), STK::IVariable::popBack(), popBackRows(), sizeRows(), and STKOUT_OF_RANGE_1ARG.

Referenced by popBackRows(), and resize().

◆ pushBackCols()

void STK::DataFrame::pushBackCols ( int const n = 1)
protected

Add columns to the container.

Definition at line 376 of file STK_DataFrame.cpp.

377{
378 // if n<=0 nothing to do
379 if (n <= 0) return;
380 // add n columns to list1D
381 insert(Range(lastIdx()+1, n), 0);
382 // update IContainer2D
384}

References STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxCols(), STK::List1D< IVariable * >::insert(), and STK::List1D< IVariable * >::lastIdx().

Referenced by resize().

◆ pushBackDataFrame()

void STK::DataFrame::pushBackDataFrame ( DataFrame const D)

Append a DataFrame back.

Definition at line 337 of file STK_DataFrame.cpp.

338{
339 if (D.empty()) return;
340 // is this structure just a pointer?
341 if (this->isRef())
342 { STKRUNTIME_ERROR_NO_ARG(DataFrame::pushBackDataFrame,cannot operate on reference);}
343 // compute pos
344 int pos(lastIdx()+1);
345 // List1D
346 pushBack(D.sizeCols());
347 // insert all columns of D
348 for (int i = D.begin(), icol = pos; i <=D.lastIdx(); i++, icol++)
349 {
350 if (D.elt(i))
351 {
352 elt(icol) = D.elt(i)->clone();
353 elt(icol)->shift(beginRows());
354 }
355 }
356 // update LastHo
357 incLastIdxCols(D.sizeCols());
358 // update rows with NA values
359 int inc = sizeRows() - D.sizeRows();
360 if (inc == 0) return; // same size
361 if (inc > 0) // D has less rows
362 { // put NA values to the inserted columns
363 for (int i= lastIdx(); i >=pos; i--)
364 if (elt(i)) { elt(i)->pushBackNAValues(inc);}
365 }
366 else
367 { // put NA values to the oter columns
368 for (int i=this->begin(); i <pos; i++)
369 if (elt(i)) { elt(i)->pushBackNAValues(-inc);}
370 // update LastVe
371 incLastIdxRows(-inc);
372 }
373}
void pushBackDataFrame(DataFrame const &D)
Append a DataFrame back.
void pushBack(int const &n=1)
Add n Elts to the container.
Definition STK_List1D.h:323

References STK::List1D< Type_ >::begin(), STK::List1D< IVariable * >::begin(), beginRows(), STK::IVariable::clone(), elt(), empty(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxCols(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxRows(), STK::TRef<-1 >::isRef(), STK::List1D< IVariable * >::lastIdx(), STK::List1D< Type_ >::lastIdx(), STK::List1D< IVariable * >::pushBack(), pushBackDataFrame(), STK::IVariable::pushBackNAValues(), STK::IVariable::shift(), sizeCols(), sizeRows(), and STKRUNTIME_ERROR_NO_ARG.

Referenced by pushBackDataFrame().

◆ pushBackRows()

void STK::DataFrame::pushBackRows ( int const n = 1)
protected

Add n rows to the container.


Definition at line 402 of file STK_DataFrame.cpp.

403{
404 // if n<=0 nothing to do
405 if (n<=0) return;
406 // for each column append row
407 for (int j=begin(); j<=lastIdx(); j++)
408 {
409 if (elt(j)) { elt(j)->pushBack(n);}
410 }
411 // update range of the container
412 incRangeRows(n);
413}
void incRangeRows(int inc)
Increment the range of the number of rows.
virtual void pushBack(int n=1)=0
Add n elements at the end of the container.

References STK::List1D< IVariable * >::begin(), elt(), STK::IContainer2D< UnknownSize, UnknownSize >::incRangeRows(), STK::List1D< IVariable * >::lastIdx(), and STK::IVariable::pushBack().

Referenced by resize().

◆ pushBackVariable()

void STK::DataFrame::pushBackVariable ( IVariable *const V)

Append a DataFrame back.

Parameters
VThe variable to append to the DataFrame

Definition at line 230 of file STK_DataFrame.cpp.

231{
232 // List1D
233 pushBack();
234 back() = V;
235 // update horizontal range (the number of column)
237 // adjust the first index of the inserted variable
239 // update rows with NA values
240 int inc = sizeRows() - V->size();
241 if (inc == 0) return; // same size
242 if (inc > 0) //V has less rows
243 { // put NA values to the inserted columns
245 }
246 else
247 { // put NA values to the oter columns
248 for (int i=this->begin(); i <lastIdxCols(); i++)
249 if (elt(i)) { elt(i)->pushBackNAValues(-inc);}
250 // update LastVe
251 incLastIdxRows(-inc);
252 }
253}

References STK::ITContainer1D< Derived >::back(), STK::List1D< IVariable * >::begin(), beginRows(), elt(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxCols(), STK::IContainer2D< UnknownSize, UnknownSize >::incLastIdxRows(), lastIdxCols(), STK::List1D< IVariable * >::pushBack(), STK::IVariable::pushBackNAValues(), STK::IVariable::shift(), STK::IVariable::size(), and sizeRows().

Referenced by STK::ImportFromCsv::asNumeric(), STK::ImportFromCsv::asOnlyNumeric(), and STK::ImportFromCsv::asString().

◆ pushFrontDataFrame()

void STK::DataFrame::pushFrontDataFrame ( DataFrame const D)
inline

Append a DataFrame front.

Definition at line 177 of file STK_DataFrame.h.

178 { insertDataFrame(beginCols(), D);}

References beginCols(), and insertDataFrame().

◆ pushFrontVariable()

void STK::DataFrame::pushFrontVariable ( IVariable *const V)
inline

Append a DataFrame front.

Definition at line 168 of file STK_DataFrame.h.

169 { insertVariable(beginCols(), V);}
void insertVariable(int pos, IVariable *const &V)
Insert a Vartiable at the specified position to the container.

References beginCols(), and insertVariable().

◆ resize()

void STK::DataFrame::resize ( Range const I,
Range const J 
)

resize the container:

  • call shift(I.begin(), J.begin()
  • call popBackCols() (insertRows()) and/or popBackCols() (popBackRows()). The implicit assumption made by this method is that it is easier and faster to add column than add rows to the 2D container.
    Parameters
    Ithe new range for the rows of the container
    Jthe new range for the columns of the container

Definition at line 108 of file STK_DataFrame.cpp.

109{
110 // check if there is something to do
111 if ((rows() == I) && (cols() == J)) return;
112 // translate beg
113 shift(I.begin(), J.begin());
114 // number of rows to del or add
115 int rinc = I.lastIdx() - lastIdxRows();
116 // number of cols to del or add
117 int cinc = J.lastIdx() - lastIdxCols();
118 // check if we add cols
119 if (cinc >=0) // work first on rows
120 {
121 if (rinc < 0) popBackRows(-rinc); // less rows
122 else pushBackRows(rinc); // more rows
123 pushBackCols(cinc); // add columns
124 }
125 else // work first on columns
126 {
127 popBackCols(-cinc); // remove columns
128 if (rinc < 0) popBackRows(-rinc); // less rows
129 else pushBackRows(rinc); // more rows
130 }
131}
void shift(int const &cbeg=1)
New beginning index for the object.
void pushBackCols(int const &n=1)
Add columns to the container.
RowRange const & rows() const
void pushBackRows(int const &n=1)
Add n rows to the container.
ColRange cols() const

References cols(), lastIdxCols(), lastIdxRows(), popBackCols(), popBackRows(), pushBackCols(), pushBackRows(), rows(), and shift().

◆ rows()

RowRange const & STK::DataFrame::rows ( ) const
inline
Returns
the range of the rows of the container

Definition at line 83 of file STK_DataFrame.h.

83{ return Base2D::rows();}

References STK::IContainer2D< UnknownSize, UnknownSize >::rows().

Referenced by resize().

◆ shift() [1/2]

void STK::DataFrame::shift ( int const cbeg = 1)

New beginning index for the object.

Parameters
cbegthe index of the first column of the data-frame

Definition at line 134 of file STK_DataFrame.cpp.

135{
136 // list1D shift
137 Base::shift(cbeg);
138 // IContainer2D shift for Col
139 shiftCols(cbeg);
140}
void shiftCols(int beg)
Shift the columns first index to beg.

References STK::ITContainer1D< Derived >::shift(), and STK::IContainer2D< UnknownSize, UnknownSize >::shiftCols().

Referenced by resize(), and shift().

◆ shift() [2/2]

void STK::DataFrame::shift ( int const rbeg,
int const cbeg 
)

New beginning index for the object.

Parameters
rbegthe index of the first row of the data-frame
cbegthe index of the first column of the data-frame

Definition at line 143 of file STK_DataFrame.cpp.

144{
145 if (beginRows() == rbeg && beginCols() == cbeg) return;
146 if (isRef())
147 { STKRUNTIME_ERROR_2ARG(DataFrame::shift,rbeg,cbeg,cannot operate on reference); }
148 // list1D shift
149 shift(cbeg);
150 // For each column update Variable
151 for (int j=begin(); j<=lastIdx(); j++)
152 if (elt(j)) { elt(j)->shift(rbeg);}
153 // update range of the rows
154 shiftRows(rbeg);
155}
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:120
void shiftRows(int beg)
Set the first index of the rows.

References STK::List1D< IVariable * >::begin(), beginCols(), beginRows(), elt(), STK::TRef<-1 >::isRef(), STK::List1D< IVariable * >::lastIdx(), STK::IVariable::shift(), shift(), STK::IContainer2D< UnknownSize, UnknownSize >::shiftRows(), and STKRUNTIME_ERROR_2ARG.

◆ sizeCols()

int STK::DataFrame::sizeCols ( ) const
inline

◆ sizeRows()

int STK::DataFrame::sizeRows ( ) const
inline

◆ swapCols()

void STK::DataFrame::swapCols ( int  j1,
int  j2 
)
inline

Swapping the j1th column and the j2th column.

Parameters
j1index of the first column to swap
j2index of the second column to swap

Definition at line 146 of file STK_DataFrame.h.

147 { Base::swap(j1, j2);}
void swap(int const &j1, int const &j2)
Swapping the j1th column and the j2th column.
Definition STK_List1D.h:521

References STK::List1D< IVariable * >::swap().

◆ writeDataFrame()

void STK::DataFrame::writeDataFrame ( ostream os,
int const left,
int const right 
) const

write a DataFrame to the output stream os.

Definition at line 484 of file STK_DataFrame.cpp.

487{
488 // Export to csv the DataFrame
489 ExportToCsv csv(*this);
490 // get the csv
491 ReadWriteCsv* pData = csv.p_readWriteCsv();
492 // set delimiters to blank
493 pData->setDelimiters(_T(" "));
494 // write the csv
495 pData->writeSelection(os, beginRows(), lastIdxRows(), left, right);
496}
#define _T(x)
Let x unmodified.
class TReadWriteCsv< String > ReadWriteCsv

References _T, beginRows(), and lastIdxRows().

Referenced by STK::operator<<().


The documentation for this class was generated from the following files: