|
STK++ 1.0
|
DataFrame is a List of Variable with the same number of rows. More...
#include <STK_DataFrame.h>

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. | |
| void | clear () |
| Clear the object. | |
| IVariable *& | elt (Integer const &i) |
| access to an element. | |
| IVariable *const & | elt (Integer const &i) const |
| access to a constant element. | |
| DataFrame & | operator= (DataFrame const &T) |
| Operator = : overwrite the DataFrame with T. | |
| void | shift (Integer const &cbeg=1) |
| New beginning index for the object. | |
| void | shift (Integer const &rbeg, Integer const &cbeg) |
| New beginning index for the object. | |
| void | popBackCols (Integer const &n) |
| Del n column of the container. | |
| void | eraseCols (Integer const &pos, Integer const &n=1) |
| Del n cols at the position of the container. | |
| void | swapCols (Integer j1, Integer j2) |
| Swapping the j1th column and the j2th column. | |
| void | popBackRows (Integer const &n) |
| Dell last rows of the container. | |
| void | eraseRows (Integer const &pos, Integer const &n=1) |
Delete n rows at the position pos to the container. | |
| void | insertVariable (Integer const &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 | insertDataFrame (Integer const &pos, const DataFrame &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, Integer const &left, Integer const &right) const |
| write a DataFrame to the output stream os. | |
Protected Types | |
| typedef List1D< IVariable * > | _BaseList |
| Type for the list container. | |
Protected Member Functions | |
| void | freeMem () |
| function for memory deallocation. | |
| void | freeRows () |
| function for row memory deallocation. | |
| void | pushBackCols (Integer const &n=1) |
| Add cols to the container. | |
| void | insertCols (Integer const &pos, Integer const &n=1) |
| Insert cols at the specified position to the container. | |
| void | pushBackRows (Integer const &n=1) |
| Add n rows to the container. | |
| void | insertRows (Integer const &pos, Integer const &n=1) |
| Insert n rows at the ith position of the container. | |
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 56 of file STK_DataFrame.h.
typedef List1D<IVariable*> STK::DataFrame::_BaseList [protected] |
Type for the list container.
Definition at line 61 of file STK_DataFrame.h.
| STK::DataFrame::DataFrame | ( | ) |
Default Constructor, empty table.
Definition at line 43 of file STK_DataFrame.cpp.
: _BaseList() , IContainer2D(Range(), Range()) { ;}
| 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.
| T | the DataFrame to copy |
| ref | true if we want to wrap the variables of T |
Definition at line 48 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::IRecursiveTemplate< Leaf >::clone(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), and STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last().
| STK::DataFrame::~DataFrame | ( | ) | [virtual] |
Destructor.
Definition at line 60 of file STK_DataFrame.cpp.
References freeRows().
{
// free the rows as the destructor of _BaseList will not free the mem
freeRows();
}
| void STK::DataFrame::clear | ( | ) | [virtual] |
Clear the object.
Reimplemented from STK::List1D< IVariable * >.
Definition at line 67 of file STK_DataFrame.cpp.
References freeMem(), STK::IContainer2D::setRange(), and STK::ITContainer1D< IVariable *, List1D< IVariable * > >::setRange().
{
freeMem();
// set default range for list
_BaseList::setRange();
// set default range for container2D
IContainer2D::setRange();
}
access to an element.
Set the method elt as a public method.
Reimplemented from STK::List1D< IVariable * >.
Definition at line 81 of file STK_DataFrame.h.
References STK::List1D< IVariable * >::elt().
Referenced by STK::ExportToArray2D< TYPE >::ExportToArray2D(), and STK::ExportToCsv::ExportToCsv().
{ return _BaseList::elt(i);}
access to a constant element.
Set the method elt as a public method.
Reimplemented from STK::List1D< IVariable * >.
Definition at line 85 of file STK_DataFrame.h.
References STK::List1D< IVariable * >::elt().
{ return _BaseList::elt(i);}
Operator = : overwrite the DataFrame with T.
Definition at line 77 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::IRecursiveTemplate< Leaf >::clone(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::IContainer2D::rangeHo(), STK::IContainer1D::resize(), and STK::IContainer2D::sizeHo().
{
// Resize if necessary.
if (sizeHo() != T.sizeHo()) _BaseList::resize(T.rangeHo());
// Copy without overlapping.
if ((T.first()>=first()))
{
for (Integer jt=T.first(), j=first(); jt<=T.last(); j++, jt++)
{
// clear old mem if any
if (at(j)) delete at(j);
// if there is a variable, create a copy
if (T[jt]) at(j) = T[jt]->clone();
else at(j) = (IVariable*)NULL;
}
}
else
{
for (Integer jt=T.last(), j=last(); jt>=T.first(); j--, jt--)
{
// clear old mem if any
if (at(j)) delete at(j);
// if there is a variable, create a copy
if (T[jt]) at(j) = T[jt]->clone();
else at(j) = (IVariable*)NULL;
}
}
return *this;
}
| void STK::DataFrame::shift | ( | Integer const & | cbeg = 1 | ) | [virtual] |
New beginning index for the object.
Reimplemented from STK::List1D< IVariable * >.
Definition at line 109 of file STK_DataFrame.cpp.
References STK::IContainer2D::setFirstHo(), and STK::List1D< IVariable * >::shift().
Referenced by shift().
{
// list1D shift
_BaseList::shift(cbeg);
// IContainer2D shift for Col
setFirstHo(cbeg);
}
New beginning index for the object.
Implements STK::IContainer2D.
Definition at line 118 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::IContainer2D::firstCol(), STK::IContainer2D::firstRow(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), STK::IContainer2D::setFirstVe(), STK::IContainer1D::shift(), and shift().
| void STK::DataFrame::popBackCols | ( | Integer const & | n | ) | [virtual] |
Del n column of the container.
Implements STK::IContainer2D.
Definition at line 135 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::IContainer2D::decLastHo(), freeMem(), STK::IContainer2D::lastCol(), STK::List1D< IVariable * >::popBack(), and STK::IContainer2D::sizeHo().
{
// if n<=0 nothing to do
if (n<=0) return;
// if there is columns to erase
if (sizeHo()<n)
{ throw std::out_of_range("DataFrame::popBackCols(n) "
"sizeHo() < n");
}
// for all columns, delete variables
for (Integer j=lastCol() - n +1; j<=lastCol(); j++)
if (at(j)) delete at(j);
// popBack() of List1D
_BaseList::popBack(n);
// update IContainer2D
decLastHo(n);
// if it was the last elt, free mem
if (this->sizeHo() == 0) freeMem();
}
Del n cols at the position of the container.
Definition at line 156 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::IContainer2D::decLastHo(), STK::List1D< IVariable * >::erase(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), freeMem(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), and STK::IContainer2D::sizeHo().
{
// if n<=0 nothing to do
if (n<=0) return;
// check conditions
if (pos<first())
{ throw std::out_of_range("DataFrame::eraseCols(pos, n) "
"pos < first()");
}
if (pos>last())
{ throw std::out_of_range("DataFrame::eraseCols(pos, n) "
"pos > last()");
}
if (last() < pos+n-1)
{ throw std::out_of_range("DataFrame::eraseCols(pos, n) "
"last() < pos+n-1");
}
// for all columns, delete variables
for (Integer j=pos+n-1; j>=pos; j--)
if (at(j)) { delete at(j);}
// delete elements of the List1D
erase(pos, n);
// update rangeHo_
decLastHo(n);
// if it was the last col, free mem
if (this->sizeHo() == 0) freeMem();
}
Swapping the j1th column and the j2th column.
| j1 | index of the first column to swap |
| j2 | index of the second column to swap |
Definition at line 107 of file STK_DataFrame.h.
References STK::IContainer2D::swap().
{ List1D::swap(j1, j2);}
| void STK::DataFrame::popBackRows | ( | Integer const & | n | ) | [virtual] |
Dell last rows of the container.
| n | number of rows to delete |
Implements STK::IContainer2D.
Definition at line 383 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::IContainer2D::decLastVe(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), STK::IContainer1D::popBack(), and STK::IContainer2D::sizeVe().
Delete n rows at the position pos to the container.
| pos | position of the rows to delete |
| n | number of rows to delete |
Definition at line 397 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::IContainer2D::decLastVe(), STK::IContainer1D::erase(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::IContainer2D::firstRow(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), and STK::IContainer2D::lastRow().
{
// if n<=0 nothing to do
if (n<=0) return;
#ifdef STK_BOUNDS_CHECK
if (firstRow() > pos)
{ throw std::out_of_range("DataFrame::eraseRows(pos, n) "
"firstRow() > pos");
}
if (lastRow() < pos)
{ throw std::out_of_range("DataFrame::eraseRows(pos, n) "
"lastRow() < pos");
}
if (lastRow() < pos+n-1)
{ throw std::out_of_range("DataFrame::eraseRows(pos, n) "
"lastRow() < pos+n-1");
}
#endif
// for each variable erase elts
for (Integer j=first(); j<=last(); j++)
if (at(j)) { at(j)->erase(pos, n);}
// update rangeVe_
decLastVe(n);
}
Insert a Vartiable at the specified position to the container.
| pos | the position in the container |
| V | the Variable to insert |
Definition at line 185 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::IContainer2D::firstCol(), STK::IContainer2D::firstRow(), STK::IContainer2D::incLastHo(), STK::IContainer2D::incLastVe(), STK::List1D< IVariable * >::insertElt(), STK::IContainer2D::lastCol(), STK::IVariable::pushBackNAValues(), STK::IContainer1D::shift(), STK::IContainer1D::size(), and STK::IContainer2D::sizeVe().
Referenced by pushFrontVariable().
{
// List1D
List1D<IVariable*>::insertElt(pos);
at(pos) = V;
// the variable have to be in the same range
at(pos)->shift(firstRow());
// update rows with NA values
Integer inc = sizeVe() - V->size();
// update LastHo
incLastHo();
if (inc == 0) return; // same size
if (inc > 0) // V has less rows
{ // put NA values to the inserted columns
at(pos)->pushBackNAValues(inc);
}
else
{ // put NA values to the other columns
for (Integer i=this->firstCol(); i <pos; i++)
if (at(i)) { at(i)->pushBackNAValues(-inc);}
for (Integer i=pos+1; i <=this->lastCol(); i++)
if (at(i)) { at(i)->pushBackNAValues(-inc);}
// update LastVe
incLastVe(-inc);
}
}
| void STK::DataFrame::pushBackVariable | ( | IVariable *const & | V | ) |
Append a DataFrame back.
| V | The variable to append to the DataFrame |
Definition at line 215 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::IContainer2D::firstRow(), STK::IContainer2D::incLastHo(), STK::IContainer2D::incLastVe(), STK::IContainer2D::lastCol(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::push_back(), STK::IVariable::pushBackNAValues(), STK::IContainer1D::shift(), STK::IContainer1D::size(), and STK::IContainer2D::sizeVe().
Referenced by STK::ImportFromCsv::runNumeric(), STK::ImportFromCsv::runOnlyNumeric(), and STK::ImportFromCsv::runString().
{
// update LastHo
incLastHo();
// List1D
push_back(V);
at(lastCol())->shift(firstRow());
// update rows with NA values
Integer inc = sizeVe() - V->size();
if (inc == 0) return; // same size
if (inc > 0) //V has less rows
{ // put NA values to the inserted columns
at(lastCol())->pushBackNAValues(inc);
}
else
{ // put NA values to the oter columns
for (Integer i=this->first(); i <lastCol(); i++)
if (at(i)) { at(i)->pushBackNAValues(-inc);}
// update LastVe
incLastVe(-inc);
}
}
| void STK::DataFrame::pushFrontVariable | ( | IVariable *const & | V | ) | [inline] |
Append a DataFrame front.
Definition at line 133 of file STK_DataFrame.h.
References STK::IContainer2D::firstCol(), and insertVariable().
{ insertVariable(firstCol(), V);}
Insert a DataFrame at the specified position to the container.
Definition at line 239 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::ITContainer1D< TYPE, TContainer1D >::at(), STK::IVariable::clone(), STK::IContainer2D::firstCol(), STK::IContainer2D::firstRow(), STK::IContainer2D::incLastHo(), STK::IContainer2D::incLastVe(), STK::List1D< IVariable * >::insertElt(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), STK::IContainer2D::lastCol(), STK::IVariable::pushBackNAValues(), STK::IContainer1D::shift(), STK::IContainer2D::sizeHo(), and STK::IContainer2D::sizeVe().
Referenced by pushFrontDataFrame().
{
// List1D
insertElt(pos, D.sizeHo());
// insert all columns of D
for (Integer i = D.firstCol(), icol = pos; i <=D.lastCol(); i++, icol++)
{
if (D.at(i))
{
at(icol) = D.at(i)->clone();
at(icol)->shift(firstRow());
}
}
// update LastHo
incLastHo(D.sizeHo());
// update rows with NA values
Integer inc = sizeVe() - D.sizeVe();
if (inc == 0) return; // same size
if (inc > 0) // D has less rows
{ // put NA values to the inserted columns
for (Integer i= pos+D.sizeHo()-1; i >=pos; i--)
if (at(i)) { at(i)->pushBackNAValues(inc);}
}
else
{ // put NA values to the oter columns
for (Integer i=this->firstCol(); i <pos; i++)
if (at(i)) { at(i)->pushBackNAValues(-inc);}
for (Integer i=pos+D.sizeHo(); i <=this->last(); i++)
if (at(i)) { at(i)->pushBackNAValues(-inc);}
// update LastVe
incLastVe(-inc);
}
}
| void STK::DataFrame::pushBackDataFrame | ( | DataFrame const & | D | ) |
Append a DataFrame back.
Definition at line 275 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::ITContainer1D< TYPE, TContainer1D >::at(), STK::IVariable::clone(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::IContainer2D::firstRow(), STK::IContainer2D::incLastHo(), STK::IContainer2D::incLastVe(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), STK::List1D< IVariable * >::pushBack(), STK::IVariable::pushBackNAValues(), STK::IContainer1D::shift(), STK::IContainer2D::sizeHo(), and STK::IContainer2D::sizeVe().
{
// compute pos
Integer pos(last()+1);
// List1D
pushBack(D.sizeHo());
// insert all columns of D
for (Integer i = D.first(), icol = pos; i <=D.last(); i++, icol++)
{
if (D.at(i))
{
at(icol) = D.at(i)->clone();
at(icol)->shift(firstRow());
}
}
// update LastHo
incLastHo(D.sizeHo());
// update rows with NA values
Integer inc = sizeVe() - D.sizeVe();
if (inc == 0) return; // same size
if (inc > 0) // D has less rows
{ // put NA values to the inserted columns
for (Integer i= last(); i >=pos; i--)
if (at(i)) { at(i)->pushBackNAValues(inc);}
}
else
{ // put NA values to the oter columns
for (Integer i=this->first(); i <pos; i++)
if (at(i)) { at(i)->pushBackNAValues(-inc);}
// update LastVe
incLastVe(-inc);
}
}
| void STK::DataFrame::pushFrontDataFrame | ( | DataFrame const & | D | ) | [inline] |
Append a DataFrame front.
Definition at line 145 of file STK_DataFrame.h.
References STK::IContainer2D::firstCol(), and insertDataFrame().
{ insertDataFrame(firstCol(), D);}
| void STK::DataFrame::writeDataFrame | ( | ostream & | os, |
| Integer const & | left, | ||
| Integer const & | right | ||
| ) | const |
write a DataFrame to the output stream os.
Definition at line 449 of file STK_DataFrame.cpp.
References STK::IContainer2D::firstRow(), STK::IContainer2D::lastRow(), STK::ExportToCsv::p_readWriteCsv(), STK::ReadWriteCsv::setDelimiters(), STK::STRING_BLANK, and STK::ReadWriteCsv::writeSelection().
Referenced by STK::operator<<().
{
// Export to csv the DataFrame
ExportToCsv csv(*this);
// get the csv
ReadWriteCsv* pData = csv.p_readWriteCsv();
// set delimiters to blank
pData->setDelimiters(STRING_BLANK);
// write the csv
pData->writeSelection(os, firstRow(), lastRow(), left, right);
}
| void STK::DataFrame::freeMem | ( | ) | [protected] |
function for memory deallocation.
Reimplemented from STK::List1D< IVariable * >.
Definition at line 423 of file STK_DataFrame.cpp.
References STK::List1D< IVariable * >::freeMem(), freeRows(), STK::IContainer2D::setRangeHo(), and STK::IContainer2D::setRangeVe().
Referenced by clear(), eraseCols(), and popBackCols().
{
// liberate variables
freeRows();
// call base freeMem
_BaseList::freeMem();
// set range to default
setRangeVe();
setRangeHo();
}
| void STK::DataFrame::freeRows | ( | ) | [protected] |
function for row memory deallocation.
Definition at line 435 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), and STK::IContainer2D::setRangeVe().
Referenced by freeMem(), and ~DataFrame().
| void STK::DataFrame::pushBackCols | ( | Integer const & | n = 1 | ) | [protected, virtual] |
Add cols to the container.
Implements STK::IContainer2D.
Definition at line 310 of file STK_DataFrame.cpp.
References STK::IContainer2D::incLastHo(), STK::List1D< IVariable * >::insert(), and STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last().
Insert cols at the specified position to the container.
Definition at line 321 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::IContainer2D::incLastHo(), STK::List1D< IVariable * >::insert(), and STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last().
{
if (n <= 0) return; // if n<=0 nothing to do
#ifdef STK_BOUNDS_CHECK
// check conditions
if (pos<first())
{ throw std::out_of_range("DataFrame::insertCols(pos, n) "
"pos<first()");
}
if (pos>last())
{ throw std::out_of_range("Dataframe::insertCols(pos, n) "
"pos>last()");
}
#endif
// insert n elements in list1D
insert(Range(pos, pos+n-1), (IVariable*)NULL);
// update IContainer2D
incLastHo(n);
}
| void STK::DataFrame::pushBackRows | ( | Integer const & | n = 1 | ) | [protected, virtual] |
Add n rows to the container.
Implements STK::IContainer2D.
Definition at line 342 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::IContainer2D::incRangeVe(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), and STK::IContainer1D::pushBack().
Insert n rows at the ith position of the container.
Definition at line 357 of file STK_DataFrame.cpp.
References STK::ITContainer1D< IVariable *, List1D< IVariable * > >::at(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::first(), STK::IContainer2D::firstRow(), STK::IContainer2D::incLastVe(), STK::IContainer1D::insertElt(), STK::ITContainer1D< IVariable *, List1D< IVariable * > >::last(), and STK::IContainer2D::lastRow().
{
// if n<=0 nothing to do
if (n<=0) return;
#ifdef STK_BOUNDS_CHECK
if (firstRow() > pos)
{ throw std::out_of_range("DataFrame::insertRows(pos, n) "
"firstRow() > pos");
}
if (lastRow()+1 < pos)
{ throw std::out_of_range("DataFrame::insertRows(pos, n) "
"lastRow()+1 < pos");
}
#endif
// insert rows to each variables
for (Integer j=first(); j<=last(); j++)
{
// if there is a variable
if (at(j))
{ at(j)->insertElt(pos, n);}
}
// update rangeVe_
incLastVe(n);
}