Ice Sheet System Model  4.18
Code documentation
DataSet.cpp
Go to the documentation of this file.
1 /*
2  * \file DataSet.cpp
3  * \brief: Implementation of DataSet class
4  */
5 
6 /*Headers: {{{*/
7 #ifdef HAVE_CONFIG_H
8  #include <config.h>
9 #else
10 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
11 #endif
12 
13 #include <cstring>
14 #include <vector>
15 #include <functional>
16 #include <algorithm>
17 #include <iostream>
18 
19 #include "../datastructures/datastructures.h"
20 #include "../shared/shared.h"
21 #include "../classes/classes.h"
22 
23 using namespace std;
24 /*}}}*/
25 
26 /*Constructors/Destructors*/
28 
29  sorted=0;
30  numsorted=0;
31  presorted=0;
32  enum_type=-1;
33  sorted_ids=NULL;
34  id_offsets=NULL;
35 
36 }
37 /*}}}*/
38 DataSet::DataSet(int dataset_enum){/*{{{*/
39  enum_type=dataset_enum;
40 
41  sorted=0;
42  numsorted=0;
43  presorted=0;
44  sorted_ids=NULL;
45  id_offsets=NULL;
46 
47 }
48 /*}}}*/
49 DataSet* DataSet::Copy(void){/*{{{*/
50 
51  vector<Object*>::iterator obj;
52  Object* object_copy=NULL;
53 
54  DataSet* copy=new DataSet(this->enum_type);
55 
56  copy->sorted=this->sorted;
57  copy->numsorted=this->numsorted;
58  copy->presorted=this->presorted;
59 
60  /*Now we need to deep copy the objects: */
61  for ( obj=this->objects.begin() ; obj < this->objects.end(); obj++ ){
62  /*Call copy on object: */
63  object_copy = (*obj)->copy();
64  copy->AddObject(object_copy);
65  }
66 
67  /*Build id_offsets and sorted_ids*/
68  int objsize = this->numsorted;
69  if(this->sorted && objsize>0 && this->id_offsets){
70  /*Allocate new ids*/
71  copy->id_offsets=xNew<int>(objsize);
72  xMemCpy<int>(copy->id_offsets,this->id_offsets,objsize);
73  }
74  else copy->id_offsets=NULL;
75  if(this->sorted && objsize>0 && this->sorted_ids){
76  /*Allocate new ids*/
77  copy->sorted_ids=xNew<int>(objsize);
78  xMemCpy<int>(copy->sorted_ids,this->sorted_ids,objsize);
79  }
80  else copy->sorted_ids=NULL;
81 
82  return copy;
83 }
84 /*}}}*/
86  clear();
87  xDelete<int>(sorted_ids);
88  xDelete<int>(id_offsets);
89 }
90 /*}}}*/
91 
92 /*Specific methods*/
93 void DataSet::Marshall(char** pmarshalled_data, int* pmarshalled_data_size, int marshall_direction){ /*{{{*/
94 
95  vector<Object*>::iterator obj;
96  int obj_size=0;
97  int obj_enum=0;
98  int i;
99 
100  if(marshall_direction==MARSHALLING_FORWARD || marshall_direction==MARSHALLING_SIZE){
101  obj_size=objects.size();
102  }
103  else{
104  clear();
105  }
106 
108  MARSHALLING(enum_type);
109  MARSHALLING(sorted);
110  MARSHALLING(presorted);
111  MARSHALLING(numsorted);
112 
113  /*Now branch according to direction of marshalling: */
114  if(marshall_direction==MARSHALLING_FORWARD || marshall_direction==MARSHALLING_SIZE){
115  if(!(this->sorted && numsorted>0 && this->id_offsets)){
116  sorted_ids=NULL;
117  id_offsets=NULL;
118  }
119  MARSHALLING_DYNAMIC(sorted_ids,int,numsorted);
120  MARSHALLING_DYNAMIC(id_offsets,int,numsorted);
121  MARSHALLING(obj_size);
122 
123  /*Go through our objects, and marshall them into the buffer: */
124  for( obj=this->objects.begin() ; obj < this->objects.end(); obj++ ){
125  obj_enum=(*obj)->ObjectEnum();
126  MARSHALLING(obj_enum);
127  (*obj)->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
128  }
129  }
130  else{
131 
132  MARSHALLING_DYNAMIC(sorted_ids,int,numsorted);
133  MARSHALLING_DYNAMIC(id_offsets,int,numsorted);
134  if (!(this->sorted && numsorted>0)){
135  sorted_ids=NULL;
136  id_offsets=NULL;
137  }
138  MARSHALLING(obj_size);
139 
140  /*This is the heart of the demashalling method. We have a buffer coming
141  in, and we are supposed to create a dataset out of it. No such thing
142  as class orientation for buffers, we need to key off the enum of each
143  object stored in the buffer. */
144  for(i=0;i<obj_size;i++){
145 
146  /*Recover enum of object first: */
147  MARSHALLING(obj_enum);
148 
149  /*Giant case statement to spin-up the right object, and demarshall into it the information
150  *stored in the buffer: */
151  if(obj_enum==NodeEnum){
152  Node* node=NULL;
153  node=new Node();
154  node->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
155  this->AddObject(node);
156  }
157  else if(obj_enum==VertexEnum){
158  Vertex* vertex=NULL;
159  vertex=new Vertex();
160  vertex->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
161  this->AddObject(vertex);
162  }
163  else if(obj_enum==MaticeEnum){
164  Matice* matice=NULL;
165  matice=new Matice();
166  matice->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
167  this->AddObject(matice);
168  }
169  else if(obj_enum==MatestarEnum){
170  Matestar* matestar=NULL;
171  matestar=new Matestar();
172  matestar->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
173  this->AddObject(matestar);
174  }
175  else if(obj_enum==SpcStaticEnum){
176  SpcStatic* spcstatic=NULL;
177  spcstatic=new SpcStatic();
178  spcstatic->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
179  this->AddObject(spcstatic);
180  }
181  else if(obj_enum==SpcDynamicEnum){
182  SpcDynamic* spcdynamic=NULL;
183  spcdynamic=new SpcDynamic();
184  spcdynamic->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
185  this->AddObject(spcdynamic);
186  }
187  else if(obj_enum==SpcTransientEnum){
188  SpcTransient* spctransient=NULL;
189  spctransient=new SpcTransient();
190  spctransient->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
191  this->AddObject(spctransient);
192  }
193  else if(obj_enum==TriaEnum){
194  Tria* tria=NULL;
195  tria=new Tria();
196  tria->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
197  this->AddObject(tria);
198  }
199  else if(obj_enum==PentaEnum){
200  Penta* penta=NULL;
201  penta=new Penta();
202  penta->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
203  this->AddObject(penta);
204  }
205  else if(obj_enum==TetraEnum){
206  Tetra* tetra=NULL;
207  tetra=new Tetra();
208  tetra->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
209  this->AddObject(tetra);
210  }
211  else if(obj_enum==SegEnum){
212  Seg* seg=NULL;
213  seg=new Seg();
214  seg->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
215  this->AddObject(seg);
216  }
217  else if(obj_enum==RiftfrontEnum){
218  Riftfront* rift=NULL;
219  rift=new Riftfront();
220  rift->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
221  this->AddObject(rift);
222  }
223  else if(obj_enum==NumericalfluxEnum){
224  Numericalflux* numflux=NULL;
225  numflux=new Numericalflux();
226  numflux->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
227  this->AddObject(numflux);
228  }
229  else if(obj_enum==PengridEnum){
230  Pengrid* pengrid=NULL;
231  pengrid=new Pengrid();
232  pengrid->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
233  this->AddObject(pengrid);
234  }
235  else if(obj_enum==PenpairEnum){
236  Penpair* penpair=NULL;
237  penpair=new Penpair();
238  penpair->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
239  this->AddObject(penpair);
240  }
241  else if(obj_enum==DoubleExternalResultEnum){
243  result->Marshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
244  result->Echo();
245  this->AddObject(result);
246  }
247  else _error_("could not recognize enum type: " << obj_enum << ": " << EnumToStringx(obj_enum) );
248  }
249  }
250 }
251 /*}}}*/
252 int DataSet::AddObject(Object* object){/*{{{*/
253 
254  _assert_(this);
255  objects.push_back(object);
256 
257  return 1;
258 }
259 /*}}}*/
260 void DataSet::clear(){/*{{{*/
261 
262 /* use reverse_iterator for efficiency in matlab memory manager
263  (keeping old code in case it needs to revert back) */
264 
265 // vector<Object*>::iterator object;
266  vector<Object*>::reverse_iterator object;
267 
268 // for ( object=objects.begin() ; object < objects.end(); object++ ){
269 // delete (*object);
270 // }
271  for ( object=objects.rbegin() ; object < objects.rend(); object++ ){
272  delete (*object);
273  }
274  objects.clear();
275 }
276 /*}}}*/
277 int DataSet::DeleteObject(Object* object){/*{{{*/
278 
279  vector<Object*>::iterator iterator;
280 
281  if(object){
282  iterator = find(objects.begin(), objects.end(),object);
283  delete *iterator;
284  objects.erase(iterator);
285  }
286 
287  return 1;
288 
289 }
290 /*}}}*/
291 void DataSet::DeepEcho(){/*{{{*/
292 
293  vector<Object*>::iterator object;
294 
295  _assert_(this);
296 
297  _printf0_("DataSet echo: " << objects.size() << " objects\n");
298 
299  for ( object=objects.begin() ; object < objects.end(); object++ ){
300 
301  /*Call deep echo on object: */
302  (*object)->DeepEcho();
303 
304  }
305 }
306 /*}}}*/
307 void DataSet::Echo(){/*{{{*/
308 
309  vector<Object*>::iterator object;
310 
311  _assert_(this);
312 
313  _printf0_("DataSet echo: " << objects.size() << " objects\n");
314 
315  for ( object=objects.begin() ; object < objects.end(); object++ ){
316 
317  /*Call echo on object: */
318  (*object)->Echo();
319 
320  }
321  return;
322 }
323 /*}}}*/
324 int DataSet::GetEnum(){/*{{{*/
325  return enum_type;
326 }
327 /*}}}*/
328 int DataSet::GetEnum(int offset){/*{{{*/
329 
330  return objects[offset]->ObjectEnum();
331 
332 }
333 /*}}}*/
334 Object* DataSet::GetObjectByOffset(int offset){/*{{{*/
335 
336  /*Check index in debugging mode*/
337  _assert_(this!=NULL);
338  _assert_(offset>=0);
339  _assert_(offset<this->Size());
340 
341  return objects[offset];
342 
343 }
344 /*}}}*/
345 Object* DataSet::GetObjectById(int* poffset,int eid){/*{{{*/
346 
347  int id_offset;
348  int offset;
349 
350  _assert_(this);
351  if(!sorted || objects.size()>numsorted)_error_("trying to binary search on a non-sorted dataset!");
352 
353  /*Carry out a binary search on the sorted_ids: */
354  if(!binary_search(&id_offset,eid,sorted_ids,objects.size())){
355  _error_("could not find object with id " << eid << " in DataSet " << EnumToStringx(enum_type));
356  }
357 
358  /*Convert the id offset into sorted offset: */
359  offset=id_offsets[id_offset];
360 
361  /*Assign output pointers if requested:*/
362  if(poffset)*poffset=offset;
363 
364  /*Return object at offset position in objects :*/
365  return objects[offset];
366 }
367 /*}}}*/
368 void DataSet::Presort(){/*{{{*/
369 
370  /*vector of objects is already sorted, just allocate the sorted ids and their
371  * offsets:*/
372  if(objects.size()){
373 
374  /*Delete existing ids*/
375  if(sorted_ids) xDelete<int>(sorted_ids);
376  if(id_offsets) xDelete<int>(id_offsets);
377 
378  /*Allocate new ids*/
379  sorted_ids=xNew<int>(objects.size());
380  id_offsets=xNew<int>(objects.size());
381 
382  /*Build id_offsets and sorted_ids*/
383  for(int i=0;i<objects.size();i++){
384  id_offsets[i]=i;
385  sorted_ids[i]=objects[i]->Id();
386 
387  /*In debugging mode, make sure Ids are ACTUALLY sorted...*/
388  #ifdef _ISSM_DEBUG_
389  //if(i>0) _assert_(sorted_ids[i]>sorted_ids[i-1]);
390  #endif
391  }
392  }
393 
394  /*set sorted flag: */
395  numsorted=objects.size();
396  sorted=1;
397 }
398 /*}}}*/
399 int DataSet::Size(void){/*{{{*/
400  _assert_(this!=NULL);
401 
402  return objects.size();
403 }
404 /*}}}*/
405 void DataSet::Sort(){/*{{{*/
406 
407  /*Only sort if we are not already sorted: */
408  if(!sorted){
409  _error_("not implemented yet!");
410  }
411 }
412 /*}}}*/
DataSet::Size
int Size()
Definition: DataSet.cpp:399
SpcTransientEnum
@ SpcTransientEnum
Definition: EnumDefinitions.h:1281
PengridEnum
@ PengridEnum
Definition: EnumDefinitions.h:1229
_assert_
#define _assert_(ignore)
Definition: exceptions.h:37
Seg::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Seg.cpp:122
TetraEnum
@ TetraEnum
Definition: EnumDefinitions.h:1300
_printf0_
#define _printf0_(StreamArgs)
Definition: Print.h:29
DataSet::GetObjectById
Object * GetObjectById(int *poffset, int eid)
Definition: DataSet.cpp:345
DataSet::AddObject
int AddObject(Object *object)
Definition: DataSet.cpp:252
Penpair::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Penpair.cpp:86
MARSHALLING_ENUM
#define MARSHALLING_ENUM(EN)
Definition: Marshalling.h:14
FemModel::Size
int Size(void)
Definition: FemModel.cpp:638
MARSHALLING_SIZE
@ MARSHALLING_SIZE
Definition: Marshalling.h:11
NodeEnum
@ NodeEnum
Definition: EnumDefinitions.h:659
PenpairEnum
@ PenpairEnum
Definition: EnumDefinitions.h:1230
Matestar
Definition: Matestar.h:24
GenericExternalResult::Echo
void Echo(void)
Definition: GenericExternalResult.h:155
Pengrid::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Pengrid.cpp:121
Tetra::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Tetra.cpp:120
SegEnum
@ SegEnum
Definition: EnumDefinitions.h:1268
Penta
Definition: Penta.h:29
Vertex::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Vertex.cpp:116
SpcStatic
Definition: SpcStatic.h:13
MARSHALLING_DYNAMIC
#define MARSHALLING_DYNAMIC(FIELD, TYPE, SIZE)
Definition: Marshalling.h:61
DataSet::Sort
void Sort()
Definition: DataSet.cpp:405
SpcStatic::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: SpcStatic.cpp:75
DataSet::DeepEcho
void DeepEcho()
Definition: DataSet.cpp:291
SpcTransient::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: SpcTransient.cpp:81
Object
Definition: Object.h:13
Tetra
Definition: Tetra.h:27
Riftfront::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Riftfront.cpp:189
DataSet::DeleteObject
int DeleteObject(int id)
SpcStaticEnum
@ SpcStaticEnum
Definition: EnumDefinitions.h:1280
Penpair
Definition: Penpair.h:16
MatestarEnum
@ MatestarEnum
Definition: EnumDefinitions.h:1168
NumericalfluxEnum
@ NumericalfluxEnum
Definition: EnumDefinitions.h:1206
EnumToStringx
const char * EnumToStringx(int enum_in)
Definition: EnumToStringx.cpp:15
Pengrid
Definition: Pengrid.h:21
Matestar::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Matestar.cpp:103
DataSet::Echo
void Echo()
Definition: DataSet.cpp:307
SpcDynamicEnum
@ SpcDynamicEnum
Definition: EnumDefinitions.h:1279
MARSHALLING
#define MARSHALLING(FIELD)
Definition: Marshalling.h:29
SpcDynamic::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: SpcDynamic.cpp:73
DataSet::GetEnum
int GetEnum()
Definition: DataSet.cpp:324
Penta::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Penta.cpp:143
RiftfrontEnum
@ RiftfrontEnum
Definition: EnumDefinitions.h:1240
DataSet::clear
void clear()
Definition: DataSet.cpp:260
VertexEnum
@ VertexEnum
Definition: EnumDefinitions.h:1322
SpcTransient
Definition: SpcTransient.h:13
Seg
Definition: Seg.h:27
SpcDynamic
Definition: SpcDynamic.h:13
Matice
Definition: Matice.h:24
DataSet::Presort
void Presort()
Definition: DataSet.cpp:368
GenericExternalResult
Definition: GenericExternalResult.h:21
Node::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Node.cpp:251
Node
Definition: Node.h:23
Matice::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Matice.cpp:161
Numericalflux
Definition: Numericalflux.h:18
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
Tria
Definition: Tria.h:29
DataSetEnum
@ DataSetEnum
Definition: EnumDefinitions.h:1028
DataSet::GetObjectByOffset
Object * GetObjectByOffset(int offset)
Definition: DataSet.cpp:334
PentaEnum
@ PentaEnum
Definition: EnumDefinitions.h:1231
MARSHALLING_FORWARD
@ MARSHALLING_FORWARD
Definition: Marshalling.h:9
GenericExternalResult::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: GenericExternalResult.h:169
DoubleExternalResultEnum
@ DoubleExternalResultEnum
Definition: EnumDefinitions.h:1042
DataSet::Copy
DataSet * Copy(void)
Definition: DataSet.cpp:49
FemModel::copy
FemModel * copy()
Definition: FemModel.cpp:320
Tria::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Tria.cpp:146
Numericalflux::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: Numericalflux.cpp:203
Object::copy
virtual Object * copy()=0
MaticeEnum
@ MaticeEnum
Definition: EnumDefinitions.h:1169
Vertex
Definition: Vertex.h:19
binary_search
int binary_search(int *poffset, int target, int *sorted_integers, int num_integers)
Definition: binary_search.cpp:14
DataSet
Declaration of DataSet class.
Definition: DataSet.h:14
DataSet::~DataSet
~DataSet()
Definition: DataSet.cpp:85
DataSet::DataSet
DataSet()
Definition: DataSet.cpp:27
TriaEnum
@ TriaEnum
Definition: EnumDefinitions.h:1318
Riftfront
Definition: Riftfront.h:16
DataSet::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: DataSet.cpp:93