TVarArray.h
Go to the documentation of this file.
1 #ifndef istd_TVarArray_included
2 #define istd_TVarArray_included
3 
4 
5 // Qt includes
6 #include <QtCore/QVector>
7 
8 // ACF includes
9 #include <istd/CVarIndex.h>
10 
11 
12 namespace istd
13 {
14 
15 
19 template <class Element>
20 class TVarArray
21 {
22 public:
25  typedef Element ElementType;
26 
27  class Iterator: public CVarIndex
28  {
29  public:
31 
32  Iterator(const Iterator& iterator);
33 
34  Element& operator*();
35  const Element& operator*() const;
36 
37  Element* operator->();
38  const Element* operator->() const;
39 
41  Iterator operator++(int);
42 
44  Iterator operator--(int);
45 
46  Iterator& operator=(const Iterator& iterator);
47 
48  bool operator==(const Iterator& iterator) const;
49  bool operator!=(const Iterator& iterator) const;
50 
51  friend class TVarArray<Element>;
52 
53  protected:
54  Iterator(TVarArray* arrayPtr);
55 
56  private:
57  TVarArray* m_arrayPtr;
58  };
59 
60  TVarArray();
61  TVarArray(const TVarArray& array);
62 
66  void Reset();
67 
72  bool IsDimensionsCountFixed() const;
73 
77  int GetDimensionsCount() const;
78 
84  bool SetDimensionsCount(int count);
85 
89  const SizesType& GetSizes() const;
90 
95  bool SetSizes(const SizesType& sizes);
96 
100  int GetSize(int dimension) const;
101 
107  bool SetSize(int dimension, int size);
108 
112  const Element& GetAt(const IndexType& index) const;
113 
117  void SetAt(const IndexType& index, const Element& value);
118 
122  void SetAllElements(const Element& value);
123 
124  // iterator support
129  Iterator Begin() const;
134  const Iterator& End() const;
135 
136  const Element& operator[](const IndexType& index) const;
137  Element& operator[](const IndexType& index);
138 
142  int GetElementIndex(const IndexType& index) const;
143 
144 protected:
148  void UpdateElementsSize();
149 
150 private:
151  typedef QVector<Element> Elements;
152  Elements m_elements;
153 
154  SizesType m_sizes;
155 
156  static Iterator s_endIterator;
157 };
158 
159 
160 // inline methods
161 
162 template <class Element>
164 {
165  return false;
166 }
167 
168 
169 template <class Element>
171 {
172  return m_sizes.GetDimensionsCount();
173 }
174 
175 
176 template <class Element>
178 {
179  bool retVal = m_sizes.SetDimensionsCount(count);
180 
181  UpdateElementsSize();
182 
183  return retVal;
184 }
185 
186 
187 template <class Element>
189 {
190  return m_sizes;
191 }
192 
193 
194 template <class Element>
195 inline int TVarArray<Element>::GetSize(int dimension) const
196 {
197  Q_ASSERT(dimension >= 0);
198  Q_ASSERT(dimension < m_sizes.GetDimensionsCount());
199 
200  return m_sizes[dimension];
201 }
202 
203 
204 template <class Element>
205 inline const Element& TVarArray<Element>::GetAt(const IndexType& index) const
206 {
207  Q_ASSERT(index.IsInside(m_sizes));
208 
209  int elementIndex = GetElementIndex(index);
210  Q_ASSERT(elementIndex < int(m_elements.size()));
211 
212  return m_elements[elementIndex];
213 }
214 
215 
216 template <class Element>
217 inline void TVarArray<Element>::SetAt(const IndexType& index, const Element& value)
218 {
219  Q_ASSERT(index.IsInside(m_sizes));
220 
221  int elementIndex = GetElementIndex(index);
222  Q_ASSERT(elementIndex < int(m_elements.size()));
223 
224  m_elements[elementIndex] = value;
225 }
226 
227 
228 // iterator support
229 
230 template <class Element>
232 {
233  return Iterator(const_cast<TVarArray<Element>*>(this));
234 }
235 
236 
237 template <class Element>
239 {
240  return s_endIterator;
241 }
242 
243 
244 template <class Element>
245 inline const Element& TVarArray<Element>::operator[](const IndexType& index) const
246 {
247  return GetAt(index);
248 }
249 
250 
251 template <class Element>
252 inline Element& TVarArray<Element>::operator[](const IndexType& index)
253 {
254  int elementIndex = GetElementIndex(index);
255  Q_ASSERT(elementIndex < int(m_elements.size()));
256 
257  return m_elements[elementIndex];
258 }
259 
260 
261 template <class Element>
262 inline int TVarArray<Element>::GetElementIndex(const IndexType& index) const
263 {
264  int indexDimensionsCount = index.GetDimensionsCount();
265  int dimensionsCount = m_sizes.GetDimensionsCount();
266 
267  int minDimensionsCount = qMin(indexDimensionsCount, dimensionsCount);
268 
269  int elementIndex = 0;
270  int cumulatedSizes = 1;
271  for (int i = 0; i < minDimensionsCount; ++i){
272  Q_ASSERT(index[i] >= 0);
273  Q_ASSERT(index[i] < m_sizes[i]);
274 
275  elementIndex += index[i] * cumulatedSizes;
276 
277  cumulatedSizes *= m_sizes[i];
278  }
279 
280  return elementIndex;
281 }
282 
283 
284 // public methods
285 
286 template <class Element>
288 {
289 }
290 
291 
292 template <class Element>
294 : m_elements(array.m_elements),
295  m_sizes(array.m_sizes)
296 {
297 }
298 
299 
300 template <class Element>
302 {
303  m_sizes.Reset();
304 
305  m_elements.clear();
306 }
307 
308 
309 template <class Element>
311 {
312  m_sizes = sizes;
313 
314  UpdateElementsSize();
315 
316  return true;
317 }
318 
319 
320 template <class Element>
321 bool TVarArray<Element>::SetSize(int dimension, int size)
322 {
323  Q_ASSERT(dimension >= 0);
324  Q_ASSERT(dimension < m_sizes.GetDimensionsCount());
325 
326  m_sizes[dimension] = size;
327 
328  UpdateElementsSize();
329 
330  return true;
331 }
332 
333 
334 template <class Element>
335 void TVarArray<Element>::SetAllElements(const Element& value)
336 {
337  for ( typename Elements::Iterator iter = m_elements.begin();
338  iter != m_elements.end();
339  ++iter){
340  *iter = value;
341  }
342 }
343 
344 
345 // protected methods
346 
347 template <class Element>
349 {
350  int dimensionsCount = m_sizes.GetDimensionsCount();
351  int cumulatedSizes = 1;
352  for (int i = 0; i < dimensionsCount; ++i){
353  cumulatedSizes *= m_sizes[i];
354  }
355 
356  m_elements.resize(cumulatedSizes);
357 }
358 
359 
360 // static attributes
361 
362 template <class Element>
364 
365 
366 // public methods of embedded class Iterator
367 
368 template <class Element>
369 TVarArray<Element>::Iterator::Iterator(const Iterator& iterator)
370 : BaseClass(iterator), m_arrayPtr(iterator.m_arrayPtr)
371 {
372 }
373 
374 
375 template <class Element>
376 const Element& TVarArray<Element>::Iterator::operator*() const
377 {
378  Q_ASSERT(m_arrayPtr != NULL);
379  Q_ASSERT(BaseClass::IsInside(m_arrayPtr->GetSizes()));
380 
381  return m_arrayPtr->GetAt(*this);
382 }
383 
384 
385 template <class Element>
387 {
388  Q_ASSERT(m_arrayPtr != NULL);
389  Q_ASSERT(BaseClass::IsInside(m_arrayPtr->GetSizes()));
390 
391  return m_arrayPtr->operator[](*this);
392 }
393 
394 
395 template <class Element>
396 const Element* TVarArray<Element>::Iterator::operator->() const
397 {
398  Q_ASSERT(m_arrayPtr != NULL);
399  Q_ASSERT(BaseClass::IsInside(m_arrayPtr->GetSizes()));
400 
401  return &m_arrayPtr->GetAt(*this);
402 }
403 
404 
405 template <class Element>
407 {
408  Q_ASSERT(m_arrayPtr != NULL);
409  Q_ASSERT(BaseClass::IsInside(m_arrayPtr->GetSizes()));
410 
411  return &m_arrayPtr->operator[](*this);
412 }
413 
414 
415 template <class Element>
417 {
418  if ((m_arrayPtr != NULL) && !Increase(m_arrayPtr->GetSizes())){
419  m_arrayPtr = NULL;
420  }
421 
422  return *this;
423 }
424 
425 
426 template <class Element>
428 {
429  Iterator retVal = *this;
430 
431  if ((m_arrayPtr != NULL) && !Increase(m_arrayPtr->GetSizes())){
432  m_arrayPtr = NULL;
433  }
434 
435  return retVal;
436 }
437 
438 
439 template <class Element>
441 {
442  if ((m_arrayPtr != NULL) && !Decrease(m_arrayPtr->GetSizes())){
443  m_arrayPtr = NULL;
444  }
445 
446  return *this;
447 }
448 
449 
450 template <class Element>
452 {
453  Iterator retVal = *this;
454 
455  if ((m_arrayPtr != NULL) && !Decrease(m_arrayPtr->GetSizes())){
456  m_arrayPtr = NULL;
457  }
458 
459  return retVal;
460 }
461 
462 
463 template <class Element>
465 {
466  BaseClass::operator=(iterator);
467 
468  m_arrayPtr = iterator.m_arrayPtr;
469 }
470 
471 
472 template <class Element>
474 {
475  if ((m_arrayPtr != NULL) && (iterator.m_arrayPtr != NULL)){
476  return (m_arrayPtr == iterator.m_arrayPtr) && (BaseClass::operator==(iterator));
477  }
478 
479  return (m_arrayPtr == iterator.m_arrayPtr);
480 }
481 
482 
483 template <class Element>
485 {
486  return !operator==(iterator);
487 }
488 
489 
490 // protected methods of emedded class Iterator
491 
492 template <class Element>
494 : BaseClass((arrayPtr != NULL)? arrayPtr->GetDimensionsCount(): 0, 0), m_arrayPtr(arrayPtr)
495 {
496  if ((m_arrayPtr != NULL) && !IsInside(m_arrayPtr->GetSizes())){
497  m_arrayPtr = NULL; // if it is not inside of array set it directly to the end iterator state
498  }
499 }
500 
502 
503 
504 } // namespace istd
505 
506 
507 #endif // !istd_TVarArray_included
508 
509 
void SetAt(const IndexType &index, const Element &value)
Set element at specified index.
Definition: TVarArray.h:217
void SetAllElements(const Element &value)
Set some value to all elements.
Definition: TVarArray.h:335
void UpdateElementsSize()
Update size of elements to size changes.
Definition: TVarArray.h:348
bool IsInside(const CVarIndex &boundaries) const
Check if index is inside boundaries.
QVector< int >::iterator Iterator
Definition: CVarIndex.h:23
bool SetSizes(const SizesType &sizes)
Set list of all sizes.
Definition: TVarArray.h:310
Iterator & operator=(const Iterator &iterator)
Definition: TVarArray.h:464
Multidimensional array with fixed number of dimensions.
Definition: TVarArray.h:20
CVarIndex SizesType
Definition: TVarArray.h:24
int GetDimensionsCount() const
Get number of dimensions of this array.
Definition: TVarArray.h:170
int GetSize(int dimension) const
Get size of array for specified dimension.
Definition: TVarArray.h:195
int GetElementIndex(const IndexType &index) const
Get index of element in one dimensional array.
Definition: TVarArray.h:262
Multidimensional index used to addressing index.
Definition: CVarIndex.h:19
const SizesType & GetSizes() const
Get list of all sizes.
Definition: TVarArray.h:188
bool IsDimensionsCountFixed() const
Check, if number dimensions is fixed.
Definition: TVarArray.h:163
Iterator(const Iterator &iterator)
bool operator!=(const Iterator &iterator) const
Definition: TVarArray.h:484
Iterator & operator--()
Definition: TVarArray.h:440
bool SetDimensionsCount(int count)
Set number of dimensions of this array.
Definition: TVarArray.h:177
const Element & operator[](const IndexType &index) const
Definition: TVarArray.h:245
Iterator Begin() const
Get begin value of element access iterator.
Definition: TVarArray.h:231
#define NULL
Definition: istd.h:64
void Reset()
Removes all elements and set all sizes to 1.
Definition: TVarArray.h:301
int GetDimensionsCount() const
Get number of dimensions of this index.
Definition: CVarIndex.h:208
const Iterator & End() const
Get end value of element access iterator.
Definition: TVarArray.h:238
TVarArray< double > CVarArray
Definition: TVarArray.h:501
bool SetSize(int dimension, int size)
Set size of array for specified dimension.
Definition: TVarArray.h:321
const Element & GetAt(const IndexType &index) const
Get element stored at specified index.
Definition: TVarArray.h:205
bool operator==(const Iterator &iterator) const
Definition: TVarArray.h:473
CVarIndex IndexType
Definition: TVarArray.h:23
Element ElementType
Definition: TVarArray.h:25
Iterator & operator++()
Definition: TVarArray.h:416

© 2007-2017 Witold Gantzke and Kirill Lepskiy