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

Generated for Nemo v2.4.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR