Nemo  2.4.2
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
datatable.h
Go to the documentation of this file.
1 
28 #ifndef DATATABLE_H
29 #define DATATABLE_H
30 
31 #include "output.h"
35 template <class T> class DataTable {
36 
37 private:
39  unsigned int _length;
41  unsigned int _groups;
43  unsigned int _classes;
48  unsigned int **_cumulClassSizes;
53  unsigned int *_cumulGroupSizes;
54 
55  unsigned int **_sizes;
56 
57  T* _table;
58 
59  void store_sizes (unsigned int nbgroups, unsigned int nbclasses, unsigned int** classSizes) {
60 
61  if (_sizes != NULL) {
62  for(unsigned int i = 0; i < nbgroups; ++i) delete [] _sizes[i];
63  delete [] _sizes;
64  }
65 
66  _sizes = new unsigned int * [nbgroups];
67 
68  for(unsigned int i = 0; i < nbgroups; ++i) {
69  _sizes[i] = new unsigned int [nbclasses];
70  for(unsigned int j = 0; j < nbclasses; ++j)
71  _sizes[i][j] = classSizes[i][j];
72  }
73  }
74 
75  bool sizeChange (unsigned int nbgroups, unsigned int nbclasses, unsigned int** classSizes) {
76  bool status = 0;
77  if( nbgroups != _groups || nbclasses != _classes ) return true;
78  else {
79  for (unsigned int i = 0; i < _groups; i++)
80  for (unsigned int j = 0; j < _classes; j++)
81  status |= _sizes[i][j] != classSizes[i][j];
82  }
83  return status;
84  }
85 
86 public:
94  DataTable(unsigned int nbgroups, unsigned int nbclasses, unsigned int** classSizes) :
96  { allocate(nbgroups, nbclasses, classSizes); }
97 
99 
105  inline void allocate (unsigned int nbgroups, unsigned int nbclasses, unsigned int** classSizes) {
106  //#ifdef _DEBUG_
107  // message("DataTable::allocate:\n");
108  //#endif
109 
110  free();
111 
112  store_sizes(nbgroups, nbclasses, classSizes);
113 
114  _groups = nbgroups;
115  _classes = nbclasses;
116  _length = 0;
117 
118  _cumulClassSizes = new unsigned int*[_groups];
119 
120  for(unsigned int i = 0; i < _groups; i++){
121  _cumulClassSizes[i] = new unsigned int [_classes];
122  _cumulClassSizes[i][0] = 0;
123  for(unsigned int j = 1; j < _classes; ++j)
124  _cumulClassSizes[i][j] = _cumulClassSizes[i][j-1] + classSizes[i][j-1];
125  }
126 
127  _cumulGroupSizes = new unsigned int[_groups];
128 
129  _cumulGroupSizes[0] = 0;
130  for(unsigned int i = 1; i < _groups; i++){
132  for(unsigned int j = 0; j < _classes; ++j) {
133  _cumulGroupSizes[i] += classSizes[(i-1)][j];//total size of the previous group
134  _length += classSizes[i][j];//aggregate to compute total length
135  }
136  }
137  //add the sizes of first group classes to have the total length of the table
138  for(unsigned int j = 0; j < _classes; ++j)
139  _length += classSizes[0][j];
140 
141  _table = new T[_length];
142  //#ifdef _DEBUG_
143 // cout<<"DataTable::allocate:_table="<<_table<<endl;
144  //#endif
145  }
146 
152  inline void update(unsigned int nbgroups, unsigned int nbclasses, unsigned int** classSizes) {
153  //#ifdef _DEBUG_
154  // message("DataTable::update:\n");
155  //#endif
156  if( nbgroups != _groups || nbclasses != _classes )
157 
158  allocate(nbgroups, nbclasses, classSizes); //free() is called in allocate
159 
160  else if ( sizeChange(nbgroups, nbclasses, classSizes) ) {
161 
162  store_sizes(nbgroups, nbclasses, classSizes);
163 
164  for(unsigned int i = 0; i < _groups; i++){
165  for(unsigned int j = 1; j < _classes; ++j)
166  _cumulClassSizes[i][j] = _cumulClassSizes[i][j-1] + classSizes[i][j-1];
167  }
168  _length = 0;
169  _cumulGroupSizes[0] = 0;
170  for(unsigned int i = 1; i < _groups; i++){
172  for(unsigned int j = 0; j < _classes; ++j) {
173  _cumulGroupSizes[i] += classSizes[(i-1)][j];
174  _length += classSizes[i][j];
175  }
176  }
177 
178  for(unsigned int j = 0; j < _classes; ++j)
179  _length += classSizes[0][j];
180 
181  if(_table != NULL) delete [] _table;
182 
183  _table = new T[_length];
184  }
185  }
187  void free ( ) {
188 // cout << "\ncalling DataTable::free()\n";
189 // cout << " pointer state: table="<<_table<<" cumulGroupSizes="<<_cumulGroupSizes
190 // <<"\n cumulClassSizes="<<_cumulClassSizes<<" sizes="<<_sizes
191 // <<endl;
192 
193  if(_table != NULL) delete [] _table;
194 
195  if(_cumulGroupSizes != NULL) delete [] _cumulGroupSizes;
196 // cout << " delete _cumulClassSizes\n";
197  if(_cumulClassSizes != NULL){
198  for(unsigned int i = 0; i < _groups; ++i) {
199 // cout << " "<<i<<":"<<_cumulClassSizes[i]<<endl;
200  delete [] _cumulClassSizes[i];}
201  delete [] _cumulClassSizes;
202  }
203 // cout << " delete _sizes\n";
204  if(_sizes != NULL){
205  for(unsigned int i = 0; i < _groups; ++i) delete [] _sizes[i];
206  delete [] _sizes;
207  }
208 
209  _table = NULL;
210  _cumulGroupSizes = NULL;
211  _cumulClassSizes = NULL;
212  _sizes = NULL;
213  _length = 0;
214 // cout << "DataTable::free() done\n";
215 }
217  inline unsigned int length ( ) {return _length;}
219  inline T* getTable ( ) {return _table;}
221  inline T* getGroup (unsigned int group) {return &_table[ _cumulGroupSizes[group] ];}
223  inline T* getClassWithinGroup (unsigned int group, unsigned int Class) {
224  return &_table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] ];}
226  inline T get (unsigned int group, unsigned int Class, unsigned int elmnt) {
227  return _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ];
228  }
230  inline void set (unsigned int group, unsigned int Class, unsigned int elmnt, T val) {
231  _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] = val;
232  }
234  inline void increment (unsigned int group, unsigned int Class, unsigned int elmnt) {
235  ++_table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ];
236  }
238  inline void plus (unsigned int group, unsigned int Class, unsigned int elmnt, T val) {
239  _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] += val;
240  }
242  inline void minus (unsigned int group, unsigned int Class, unsigned int elmnt, T val) {
243  _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] -= val;
244  }
246  inline void multiply (unsigned int group, unsigned int Class, unsigned int elmnt, T val) {
247  _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] *= val;
248  }
250  inline void divide (unsigned int group, unsigned int Class, unsigned int elmnt, T val) {
251  _table[ _cumulGroupSizes[group] + _cumulClassSizes[group][Class] + elmnt ] /= val;
252  }
254  inline void init (T val) {for(unsigned int i = 0; i < _length; ++i) _table[i] = val;}
255 
256  unsigned int getNumGroups () {return _groups;}
257  unsigned int getNumClasses () {return _classes;}
258  unsigned int size (unsigned int i, unsigned int j) {return _sizes[i][j];}
259 
260  void show_up ()
261  {
262  message("DataTable: got %i groups of %i classes; total length is %i.\n", _groups, _classes, _length);
263  }
264 };
265 
266 #endif //DATATABLE_H
A class to aggregate structured data in an array.
Definition: datatable.h:35
void show_up()
Definition: datatable.h:260
DataTable(unsigned int nbgroups, unsigned int nbclasses, unsigned int **classSizes)
Constructor.
Definition: datatable.h:94
T * getGroup(unsigned int group)
Accessor to a group array.
Definition: datatable.h:221
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'.
Definition: datatable.h:242
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.
Definition: datatable.h:105
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'.
Definition: datatable.h:230
bool sizeChange(unsigned int nbgroups, unsigned int nbclasses, unsigned int **classSizes)
Definition: datatable.h:75
unsigned int size(unsigned int i, unsigned int j)
Definition: datatable.h:258
~DataTable()
Definition: datatable.h:98
void divide(unsigned int group, unsigned int Class, unsigned int elmnt, T val)
Subdivide 'elmnt' of the class 'Class' in the group 'group' by 'val'.
Definition: datatable.h:250
unsigned int getNumClasses()
Definition: datatable.h:257
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'.
Definition: datatable.h:238
void store_sizes(unsigned int nbgroups, unsigned int nbclasses, unsigned int **classSizes)
Definition: datatable.h:59
void free()
Deallocates all the allocated tables.
Definition: datatable.h:187
T 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'.
Definition: datatable.h:226
unsigned int * _cumulGroupSizes
Stores the indexes of each group present in the table.
Definition: datatable.h:53
unsigned int getNumGroups()
Definition: datatable.h:256
unsigned int ** _sizes
Definition: datatable.h:55
T * _table
Definition: datatable.h:57
T * getClassWithinGroup(unsigned int group, unsigned int Class)
Accessor to a class array whithin a group.
Definition: datatable.h:223
DataTable()
Default constructor.
Definition: datatable.h:88
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.
Definition: datatable.h:152
void init(T val)
Sets all elements of the table to value 'val'.
Definition: datatable.h:254
unsigned int _length
length of the table
Definition: datatable.h:39
unsigned int _groups
number of groups in the table
Definition: datatable.h:41
unsigned int length()
Returns the length of the table (total number of elements present).
Definition: datatable.h:217
T * getTable()
Accessor to the table array.
Definition: datatable.h:219
unsigned int ** _cumulClassSizes
Stores the indexes of each class of each group present in the table.
Definition: datatable.h:48
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'.
Definition: datatable.h:246
unsigned int _classes
number of classes within each group
Definition: datatable.h:43
void increment(unsigned int group, unsigned int Class, unsigned int elmnt)
Increments 'elmnt' of the class 'Class' in the group 'group' by one.
Definition: datatable.h:234
void message(const char *message,...)
Definition: output.cc:38
Nemo2.

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR