Nemo  2.2.0
Public Member Functions | Private Member Functions | Private Attributes
DataTable< T > Class Template Reference

A class to aggregate structured data in an array. More...

#include <datatable.h>

+ Collaboration diagram for DataTable< T >:

List of all members.

Public Member Functions

 DataTable ()
 Default constructor.
 DataTable (unsigned int nbgroups, unsigned int nbclasses, unsigned int **classSizes)
 Constructor.
 ~DataTable ()
void allocate (unsigned int nbgroups, unsigned int nbclasses, unsigned int **classSizes)
 Creates a table of size given by the sum of all classes passed by the 'classSizes' matrix.
void update (unsigned int nbgroups, unsigned int nbclasses, unsigned int **classSizes)
 Updates the group and classe sizes and re-allocates the table according to its new length.
void free ()
 Deallocates all the allocated tables.
unsigned int length ()
 Returns the length of the table (total number of elements present).
T * getTable ()
 Accessor to the table array.
T * getGroup (unsigned int group)
 Accessor to a group array.
T * getClassWithinGroup (unsigned int group, unsigned int Class)
 Accessor to a class array whithin a group.
get (unsigned int group, unsigned int Class, unsigned int elmnt)
 Returns value stored of the element 'elmnt' of the class 'Class' in the group 'group'.
void set (unsigned int group, unsigned int Class, unsigned int elmnt, T val)
 Sets the element 'elmnt' of the class 'Class' in the group 'group' to the value 'val'.
void increment (unsigned int group, unsigned int Class, unsigned int elmnt)
 Increments 'elmnt' of the class 'Class' in the group 'group' by one.
void plus (unsigned int group, unsigned int Class, unsigned int elmnt, T val)
 Adds 'val' to 'elmnt' in the class 'Class' in the group 'group'.
void minus (unsigned int group, unsigned int Class, unsigned int elmnt, T val)
 Substracts 'val' from 'elmnt' in the class 'Class' in the group 'group'.
void multiply (unsigned int group, unsigned int Class, unsigned int elmnt, T val)
 Multiplies 'elmnt' of the class 'Class' in the group 'group' by 'val'.
void subdivide (unsigned int group, unsigned int Class, unsigned int elmnt, T val)
 Subdivide 'elmnt' of the class 'Class' in the group 'group' by 'val'.
void init (T val)
 Sets all elements of the table to value 'val'.
unsigned int getNumGroups ()
unsigned int getNumClasses ()
unsigned int size (unsigned int i, unsigned int j)
void show_up ()

Private Member Functions

void store_sizes (unsigned int nbgroups, unsigned int nbclasses, unsigned int **classSizes)
bool sizeChange (unsigned int nbgroups, unsigned int nbclasses, unsigned int **classSizes)

Private Attributes

unsigned int _length
 length of the table
unsigned int _groups
 number of groups in the table
unsigned int _classes
 number of classes within each group
unsigned int ** _cumulClassSizes
 Stores the indexes of each class of each group present in the table.
unsigned int * _cumulGroupSizes
 Stores the indexes of each group present in the table.
unsigned int ** _sizes
T * _table

Detailed Description

template<class T>
class DataTable< T >

A class to aggregate structured data in an array.

The information is subdivided into groups (e.g., patch) themselves structured in classes (e.g., age or sex classes).

Definition at line 37 of file datatable.h.


Constructor & Destructor Documentation

template<class T>
DataTable< T >::DataTable ( ) [inline]

Default constructor.

Definition at line 90 of file datatable.h.

template<class T>
DataTable< T >::DataTable ( unsigned int  nbgroups,
unsigned int  nbclasses,
unsigned int **  classSizes 
) [inline]

Constructor.

Parameters:
nbgroupsthe number of rows in the table, typically the number of patches
nbclassesan array of the sizes of each row, i.e. the Patch sizes
classSizesa matrix of the sizes of each class within each group

Definition at line 96 of file datatable.h.

                                                                                      :
  _length(0), _groups(0), _classes(0), _cumulClassSizes(0), _cumulGroupSizes(0), _table(0) 
  {     allocate(nbgroups, nbclasses, classSizes); }
template<class T>
DataTable< T >::~DataTable ( ) [inline]

Definition at line 100 of file datatable.h.

{free();}

Member Function Documentation

template<class T>
void DataTable< T >::allocate ( unsigned int  nbgroups,
unsigned int  nbclasses,
unsigned int **  classSizes 
) [inline]

Creates a table of size given by the sum of all classes passed by the 'classSizes' matrix.

Parameters:
nbgroupsthe number of rows in the table, typically the number of patches
nbclassesthe number of classes within a group, e.g. the age-classes
classSizesa matrix (nbgroups X nbclasses) of the sizes of each class within each group

Definition at line 107 of file datatable.h.

Referenced by TTNeutralGenesSH::allocateTables(), DataTable< unsigned int >::DataTable(), DataTable< unsigned int >::update(), and TTNeutralGenesFH::write_varcompWC().

                                                                                                  { 
    //#ifdef _DEBUG_
    //    message("DataTable::allocate:\n");
    //#endif
    
    free();
    
    store_sizes(nbgroups, nbclasses, classSizes);
    
    _groups = nbgroups;
    _classes = nbclasses;
    _length = 0;
    
    _cumulClassSizes = new unsigned int*[_groups];
    
    for(unsigned int i = 0; i < _groups; i++){
      _cumulClassSizes[i] = new unsigned int [_classes];
      _cumulClassSizes[i][0] = 0;
      for(unsigned int j = 1; j < _classes; ++j)
        _cumulClassSizes[i][j] = _cumulClassSizes[i][j-1] + classSizes[i][j-1];
    }
    
    _cumulGroupSizes = new unsigned int[_groups];
    
    _cumulGroupSizes[0] = 0; 
    for(unsigned int i = 1; i < _groups; i++){
      _cumulGroupSizes[i] = _cumulGroupSizes[i-1];
      for(unsigned int j = 0; j < _classes; ++j) {
        _cumulGroupSizes[i] += classSizes[(i-1)][j];//total size of the previous group
        _length += classSizes[i][j];//aggregate to compute total length
      }
    }
    //add the sizes of first group classes to have the total length of the table
    for(unsigned int j = 0; j < _classes; ++j)
      _length += classSizes[0][j];
    
    _table = new T[_length];
    //#ifdef _DEBUG_
    //    message("DataTable::allocate:_table = 0x%x\n",_table);
    //#endif
  }
template<class T>
void DataTable< T >::free ( ) [inline]

Deallocates all the allocated tables.

Definition at line 189 of file datatable.h.

Referenced by DataTable< unsigned int >::allocate(), and DataTable< unsigned int >::~DataTable().

                {
    if(_table != NULL) delete [] _table;
    
    if(_cumulGroupSizes != NULL) delete [] _cumulGroupSizes;
    
    if(_cumulClassSizes != NULL){
      for(unsigned int i = 0; i < _groups; ++i) delete [] _cumulClassSizes[i];
      delete [] _cumulClassSizes;
    }
    
    if(_sizes != NULL){
      for(unsigned int i = 0; i < _groups; ++i) delete [] _sizes[i];
      delete [] _sizes;
    }
    
    _table = NULL;
    _cumulGroupSizes = NULL;
    _cumulClassSizes = NULL;
    _sizes = NULL;
    _length = 0;
  }
template<class T>
T DataTable< T >::get ( unsigned int  group,
unsigned int  Class,
unsigned int  elmnt 
) [inline]

Returns value stored of the element 'elmnt' of the class 'Class' in the group 'group'.

Definition at line 220 of file datatable.h.

Referenced by TTNeutralGenesFH::write_freq(), and TTNeutralGenesFH::write_varcompWC().

                                                                             {
    return _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ];
  } 
template<class T>
T* DataTable< T >::getClassWithinGroup ( unsigned int  group,
unsigned int  Class 
) [inline]

Accessor to a class array whithin a group.

Definition at line 217 of file datatable.h.

                                                                         {
    return &_table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] ];}
template<class T>
T* DataTable< T >::getGroup ( unsigned int  group) [inline]

Accessor to a group array.

Definition at line 215 of file datatable.h.

{return &_table[ _cumulGroupSizes[group] ];}
template<class T>
unsigned int DataTable< T >::getNumClasses ( ) [inline]

Definition at line 251 of file datatable.h.

{return _classes;}
template<class T>
unsigned int DataTable< T >::getNumGroups ( ) [inline]

Definition at line 250 of file datatable.h.

{return _groups;}
template<class T>
T* DataTable< T >::getTable ( ) [inline]

Accessor to the table array.

Definition at line 213 of file datatable.h.

{return _table;}
template<class T>
void DataTable< T >::increment ( unsigned int  group,
unsigned int  Class,
unsigned int  elmnt 
) [inline]

Increments 'elmnt' of the class 'Class' in the group 'group' by one.

Definition at line 228 of file datatable.h.

                                                                                     {
    ++_table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ];
  }
template<class T>
void DataTable< T >::init ( val) [inline]

Sets all elements of the table to value 'val'.

Definition at line 248 of file datatable.h.

{for(unsigned int i = 0; i < _length; ++i) _table[i] = val;} 
template<class T>
unsigned int DataTable< T >::length ( ) [inline]

Returns the length of the table (total number of elements present).

Definition at line 211 of file datatable.h.

{return _length;}
template<class T>
void DataTable< T >::minus ( unsigned int  group,
unsigned int  Class,
unsigned int  elmnt,
val 
) [inline]

Substracts 'val' from 'elmnt' in the class 'Class' in the group 'group'.

Definition at line 236 of file datatable.h.

                                                                                        {
    _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] -= val;
  }
template<class T>
void DataTable< T >::multiply ( unsigned int  group,
unsigned int  Class,
unsigned int  elmnt,
val 
) [inline]

Multiplies 'elmnt' of the class 'Class' in the group 'group' by 'val'.

Definition at line 240 of file datatable.h.

                                                                                           {
    _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] *= val;
  }
template<class T>
void DataTable< T >::plus ( unsigned int  group,
unsigned int  Class,
unsigned int  elmnt,
val 
) [inline]

Adds 'val' to 'elmnt' in the class 'Class' in the group 'group'.

Definition at line 232 of file datatable.h.

                                                                                       {
    _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] += val;
  }
template<class T>
void DataTable< T >::set ( unsigned int  group,
unsigned int  Class,
unsigned int  elmnt,
val 
) [inline]

Sets the element 'elmnt' of the class 'Class' in the group 'group' to the value 'val'.

Definition at line 224 of file datatable.h.

                                                                                      {
    _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] = val;
  }
template<class T>
void DataTable< T >::show_up ( ) [inline]

Definition at line 254 of file datatable.h.

  {
    message("DataTable: got %i groups of %i classes; total length is %i.\n", _groups, _classes, _length);
  }
template<class T>
unsigned int DataTable< T >::size ( unsigned int  i,
unsigned int  j 
) [inline]

Definition at line 252 of file datatable.h.

{return _sizes[i][j];}
template<class T>
bool DataTable< T >::sizeChange ( unsigned int  nbgroups,
unsigned int  nbclasses,
unsigned int **  classSizes 
) [inline, private]

Definition at line 77 of file datatable.h.

Referenced by DataTable< unsigned int >::update().

                                                                                             {
    bool status = 0;
    if( nbgroups != _groups || nbclasses != _classes ) return true;
    else {
      for (unsigned int i = 0; i < _groups; i++)
        for (unsigned int j = 0; j < _classes; j++)
          status |= _sizes[i][j] != classSizes[i][j];
    }
    return status;
  }
template<class T>
void DataTable< T >::store_sizes ( unsigned int  nbgroups,
unsigned int  nbclasses,
unsigned int **  classSizes 
) [inline, private]

Definition at line 61 of file datatable.h.

Referenced by DataTable< unsigned int >::allocate(), and DataTable< unsigned int >::update().

                                                                                              {
    
    if (_sizes != NULL) {
      for(unsigned int i = 0; i < nbgroups; ++i) delete [] _sizes[i];
      delete [] _sizes;
    }
    
    _sizes = new unsigned int * [nbgroups];
    
    for(unsigned int i = 0; i < nbgroups; ++i) {
      _sizes[i] = new unsigned int [nbclasses];
      for(unsigned int j = 0; j < nbclasses; ++j)
        _sizes[i][j] = classSizes[i][j];
    }
  }
template<class T>
void DataTable< T >::subdivide ( unsigned int  group,
unsigned int  Class,
unsigned int  elmnt,
val 
) [inline]

Subdivide 'elmnt' of the class 'Class' in the group 'group' by 'val'.

Definition at line 244 of file datatable.h.

                                                                                            {
    _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] /= val;
  }
template<class T>
void DataTable< T >::update ( unsigned int  nbgroups,
unsigned int  nbclasses,
unsigned int **  classSizes 
) [inline]

Updates the group and classe sizes and re-allocates the table according to its new length.

Parameters:
nbgroupsthe number of rows in the table, typically the number of patches
nbclassesthe number of classes within a group, e.g. the age-class sizes
classSizesa matrix of the sizes of each class within each group

Definition at line 154 of file datatable.h.

                                                                                               {
    //#ifdef _DEBUG_
    //    message("DataTable::update:\n");
    //#endif
    if( nbgroups != _groups || nbclasses != _classes )
      
      allocate(nbgroups, nbclasses, classSizes); //free() is called in allocate
    
    else if ( sizeChange(nbgroups, nbclasses, classSizes) ) {
      
      store_sizes(nbgroups, nbclasses, classSizes);
      
      for(unsigned int i = 0; i < _groups; i++){
        for(unsigned int j = 1; j < _classes; ++j)
          _cumulClassSizes[i][j] = _cumulClassSizes[i][j-1] + classSizes[i][j-1];
      }
      _length = 0;
      _cumulGroupSizes[0] = 0; 
      for(unsigned int i = 1; i < _groups; i++){
        _cumulGroupSizes[i] = _cumulGroupSizes[i-1];
        for(unsigned int j = 0; j < _classes; ++j) {
          _cumulGroupSizes[i] += classSizes[(i-1)][j];
          _length += classSizes[i][j];
        }
      }
      
      for(unsigned int j = 0; j < _classes; ++j)
        _length += classSizes[0][j];
      
      if(_table != NULL) delete [] _table;
      
      _table = new T[_length];
    }
  }

Member Data Documentation

template<class T>
unsigned int DataTable< T >::_classes [private]
template<class T>
unsigned int** DataTable< T >::_cumulClassSizes [private]

Stores the indexes of each class of each group present in the table.

the aggregated sizes of each classe within each group given by: size(class i!=0) = sum of size( (class 0) to (class i-1) ) and size(class i=0) = 0. This is repeated for each group.

Definition at line 50 of file datatable.h.

Referenced by DataTable< unsigned int >::allocate(), DataTable< unsigned int >::free(), DataTable< unsigned int >::get(), DataTable< unsigned int >::getClassWithinGroup(), DataTable< unsigned int >::increment(), DataTable< unsigned int >::minus(), DataTable< unsigned int >::multiply(), DataTable< unsigned int >::plus(), DataTable< unsigned int >::set(), DataTable< unsigned int >::subdivide(), and DataTable< unsigned int >::update().

template<class T>
unsigned int* DataTable< T >::_cumulGroupSizes [private]

Stores the indexes of each group present in the table.

the aggregated sizes of each group with size(group i=0) = 0 and size(group i!=0) = sum of size( (group 0) to (group i-1)) where the group sizes are the sum of the classe sizes of the groups.

Definition at line 55 of file datatable.h.

Referenced by DataTable< unsigned int >::allocate(), DataTable< unsigned int >::free(), DataTable< unsigned int >::get(), DataTable< unsigned int >::getClassWithinGroup(), DataTable< unsigned int >::getGroup(), DataTable< unsigned int >::increment(), DataTable< unsigned int >::minus(), DataTable< unsigned int >::multiply(), DataTable< unsigned int >::plus(), DataTable< unsigned int >::set(), DataTable< unsigned int >::subdivide(), and DataTable< unsigned int >::update().

template<class T>
unsigned int DataTable< T >::_groups [private]
template<class T>
unsigned int DataTable< T >::_length [private]
template<class T>
unsigned int** DataTable< T >::_sizes [private]
template<class T>
T* DataTable< T >::_table [private]

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

Generated for Nemo v2.2.0 by  doxygen 1.7.5.1 -- Nemo is hosted by  SourceForge.net Logo