libSplash
DataContainer.hpp
1 
23 #ifndef DATACONTAINER_HPP
24 #define DATACONTAINER_HPP
25 
26 #include <vector>
27 
28 #include "splash/DCException.hpp"
29 #include "splash/Dimensions.hpp"
30 #include "splash/domains/DomainData.hpp"
31 
32 namespace splash
33 {
34 
41  {
42  public:
43 
48  offset(0, 0, 0),
49  size(1, 1, 1)
50  {
51  }
52 
58  virtual ~DataContainer()
59  {
60  for (std::vector<DomainData* >::iterator iter = subdomains.begin();
61  iter != subdomains.end(); ++iter)
62  {
63  if (*iter != NULL)
64  delete (*iter);
65  }
66 
67  subdomains.clear();
68  }
69 
73  void add(DomainData* entry)
74  {
75  if (entry == NULL)
76  throw DCException("Entry in DataContainer must not be NULL.");
77 
78  if (entry->getData() == NULL)
79  throw DCException("Data in entry in DataContainer must not be NULL.");
80 
81  const Dimensions &entryOffset = entry->getOffset();
82  const Dimensions entryBack = entry->getBack(); // last index INSIDE
83 
84  for (uint32_t i = 0; i < DSP_DIM_MAX; ++i)
85  {
86  if (subdomains.empty())
87  offset[i] = entryOffset[i];
88  else
89  offset[i] = std::min(entryOffset[i], offset[i]);
90  size[i] = std::max(entryBack[i] + 1 - offset[i], size[i]);
91  }
92 
93  subdomains.push_back(entry);
94  }
95 
102  {
103  return subdomains.size();
104  }
105 
111  size_t getNumElements()
112  {
113  size_t num_elements = 0;
114 
115  for (std::vector<DomainData* >::const_iterator iter = subdomains.begin();
116  iter != subdomains.end(); ++iter)
117  {
118  num_elements += (*iter)->getElements().getScalarSize();
119  }
120 
121  return num_elements;
122  }
123 
130  {
131  return size;
132  }
133 
140  {
141  return offset;
142  }
143 
151  {
152  return offset + size - Dimensions(1, 1, 1);
153  }
154 
161  DomainData* getIndex(size_t index)
162  {
163  if (subdomains.size() > index)
164  return subdomains[index];
165 
166  throw DCException("Invalid index in DataContainer");
167  }
168 
175  DomainData* get(size_t x)
176  {
177  if (x < size[0])
178  return subdomains[x];
179 
180  throw DCException("Invalid entry in DataContainer (1)");
181  }
182 
190  DomainData* get(size_t x, size_t y)
191  {
192  if (x < size[0] && y < size[1])
193  return subdomains[y * size[0] + x];
194 
195  throw DCException("Invalid entry in DataContainer (2)");
196  }
197 
206  DomainData* get(size_t x, size_t y, size_t z)
207  {
208  if (x < size[0] && y < size[1] && z < size[2])
209  return subdomains[z * size[0] * size[1] + y * size[0] + x];
210 
211  throw DCException("Invalid entry in DataContainer (3)");
212  }
213 
221  void* getElement(size_t index)
222  {
223  if (subdomains.size() == 0)
224  return NULL;
225 
226  size_t elements = 0;
227 
228  for (size_t i = 0; i < subdomains.size(); ++i)
229  {
230  DomainData *subdomain = subdomains[i];
231  size_t subdomain_elements = subdomain->getElements().getScalarSize();
232 
233  if (elements + subdomain_elements > index)
234  {
235  size_t type_size = subdomain->getTypeSize();
236  size_t local_index = index - elements;
237 
238  assert(subdomain->getData() != NULL);
239  if (subdomain->getData() == NULL)
240  return NULL;
241 
242  return (((uint8_t*) (subdomain->getData())) + (type_size * local_index));
243  } else
244  elements += subdomain_elements;
245  }
246 
247  return NULL;
248  }
249 
250  private:
251  std::vector<DomainData* > subdomains;
252  Dimensions offset;
253  Dimensions size;
254  };
255 
256 }
257 
258 #endif /* DATACONTAINER_HPP */
Dimensions getOffset() const
Dimensions & getOffset()
Definition: Domain.hpp:101
Dimensions getBack() const
Definition: Domain.hpp:122
size_t getScalarSize() const
Definition: Dimensions.hpp:219
DomainData * getIndex(size_t index)
void add(DomainData *entry)
void * getElement(size_t index)
Dimensions getBack() const
Dimensions & getElements()
Definition: DomainData.hpp:102
Dimensions getSize() const