TArray.h
Go to the documentation of this file.
1 #ifndef istd_TArray_included
2 #define istd_TArray_included
3 
4 
5 // STL includes
6 #include <vector>
7 
8 // Qt includes
9 #include <QtCore/QtGlobal>
10 
11 // ACF includes
12 #include <istd/TIndex.h>
13 
14 
15 namespace istd
16 {
17 
18 
22 template <class Element, int Dimensions>
23 class TArray
24 {
25 public:
28  typedef Element ElementType;
29 
30  class Iterator: public TIndex<Dimensions>
31  {
32  public:
34 
35  Iterator(const Iterator& iterator);
36 
37  Element& operator*();
38  const Element& operator*() const;
39 
40  Element* operator->();
41  const Element* operator->() const;
42 
44  Iterator operator++(int);
45 
47  Iterator operator--(int);
48 
49  Iterator& operator=(const Iterator& iterator);
50 
51  bool operator==(const Iterator& iterator) const;
52  bool operator!=(const Iterator& iterator) const;
53 
54  friend class TArray<Element, Dimensions>;
55 
56  protected:
57  Iterator(TArray* arrayPtr);
58 
59  private:
60  TArray* m_arrayPtr;
61  };
62 
63  TArray();
64  TArray(const TArray& array);
65  explicit TArray(const SizesType& sizes, const ElementType& value = ElementType());
66 
70  void Reset();
71 
75  bool IsEmpty() const;
76 
81  bool IsDimensionsCountFixed() const;
82 
86  int GetDimensionsCount() const;
87 
94  bool SetDimensionsCount(int count);
95 
99  const SizesType& GetSizes() const;
100 
105  bool SetSizes(const SizesType& sizes);
106 
110  int GetSize(int dimension) const;
111 
117  bool SetSize(int dimension, int size);
118 
122  const Element& GetAt(const IndexType& index) const;
123 
127  Element& GetAtRef(const IndexType& index);
128 
132  void SetAt(const IndexType& index, const Element& value);
133 
137  void SetAllElements(const Element& value);
138 
139  // iterator support
144  Iterator Begin() const;
149  const Iterator& End() const;
150 
151  // operators
152  bool operator==(const TArray<Element, Dimensions>& value) const;
153  bool operator!=(const TArray<Element, Dimensions>& value) const;
154  const Element& operator[](const IndexType& index) const;
155  Element& operator[](const IndexType& index);
156 
157 protected:
158  typedef std::vector<Element> Elements;
159 
163  int GetElementIndex(const IndexType& index) const;
167  void UpdateElementsSize(const ElementType& value = ElementType());
168 
169  void DeepCopy(const Elements& elements, const SizesType& sizes);
170 
173 
174 private:
175  static Iterator s_endIterator;
176 };
177 
178 
179 // inline methods
180 
181 template <class Element, int Dimensions>
183 {
184  return m_sizes.IsSizeEmpty();
185 }
186 
187 
188 template <class Element, int Dimensions>
190 {
191  return true;
192 }
193 
194 
195 template <class Element, int Dimensions>
197 {
198  return Dimensions;
199 }
200 
201 
202 template <class Element, int Dimensions>
204 {
205  return (count == GetDimensionsCount());
206 }
207 
208 
209 template <class Element, int Dimensions>
211 {
212  return m_sizes;
213 }
214 
215 
216 template <class Element, int Dimensions>
217 inline int TArray<Element, Dimensions>::GetSize(int dimension) const
218 {
219  Q_ASSERT(dimension >= 0);
220  Q_ASSERT(dimension < Dimensions);
221 
222  return m_sizes[dimension];
223 }
224 
225 
226 template <class Element, int Dimensions>
227 inline const Element& TArray<Element, Dimensions>::GetAt(const IndexType& index) const
228 {
229  Q_ASSERT(index.IsInside(m_sizes));
230 
231  int elementIndex = GetElementIndex(index);
232  Q_ASSERT(elementIndex < int(m_elements.size()));
233 
234  return m_elements[elementIndex];
235 }
236 
237 
238 template <class Element, int Dimensions>
240 {
241  Q_ASSERT(index.IsInside(m_sizes));
242 
243  int elementIndex = GetElementIndex(index);
244  Q_ASSERT(elementIndex < int(m_elements.size()));
245 
246  return m_elements[elementIndex];
247 }
248 
249 
250 template <class Element, int Dimensions>
251 inline void TArray<Element, Dimensions>::SetAt(const IndexType& index, const Element& value)
252 {
253  Q_ASSERT(index.IsInside(m_sizes));
254 
255  int elementIndex = GetElementIndex(index);
256  Q_ASSERT(elementIndex < int(m_elements.size()));
257 
258  m_elements[elementIndex] = value;
259 }
260 
261 
262 // iterator support
263 
264 template <class Element, int Dimensions>
266 {
267  return Iterator(const_cast<TArray<Element, Dimensions>*>(this));
268 }
269 
270 
271 template <class Element, int Dimensions>
273 {
274  return s_endIterator;
275 }
276 
277 
278 template <class Element, int Dimensions>
279 inline const Element& TArray<Element, Dimensions>::operator[](const IndexType& index) const
280 {
281  return GetAt(index);
282 }
283 
284 
285 template <class Element, int Dimensions>
287 {
288  int elementIndex = GetElementIndex(index);
289  Q_ASSERT(elementIndex < int(m_elements.size()));
290 
291  return m_elements[elementIndex];
292 }
293 
294 
295 // inline protected methods
296 
297 template <class Element, int Dimensions>
299 {
300  int elementIndex = 0;
301  int cumulatedSizes = 1;
302  for (int i = 0; i < Dimensions; ++i){
303  Q_ASSERT(index[i] >= 0);
304  Q_ASSERT(index[i] < m_sizes[i]);
305 
306  elementIndex += index[i] * cumulatedSizes;
307 
308  cumulatedSizes *= m_sizes[i];
309  }
310 
311  return elementIndex;
312 }
313 
314 
315 // public methods
316 
317 template <class Element, int Dimensions>
319 {
320  for (int i = 0; i < Dimensions; ++i){
321  m_sizes[i] = 0;
322  }
323 }
324 
325 
326 template <class Element, int Dimensions>
328 : m_sizes(array.m_sizes), m_elements(array.m_elements)
329 {
330 }
331 
332 
333 template <class Element, int Dimensions>
335 : m_sizes(sizes)
336 {
337  UpdateElementsSize(value);
338 }
339 
340 
341 template <class Element, int Dimensions>
343 {
344  m_sizes.Reset();
345 
346  m_elements.clear();
347 }
348 
349 
350 template <class Element, int Dimensions>
352 {
353  m_sizes = sizes;
354 
355  UpdateElementsSize();
356 
357  return true;
358 }
359 
360 
361 template <class Element, int Dimensions>
362 bool TArray<Element, Dimensions>::SetSize(int dimension, int size)
363 {
364  Q_ASSERT(dimension >= 0);
365  Q_ASSERT(dimension < Dimensions);
366 
367  m_sizes[dimension] = size;
368 
369  UpdateElementsSize();
370 
371  return true;
372 }
373 
374 
375 template <class Element, int Dimensions>
377 {
378  for ( typename Elements::iterator iter = m_elements.begin();
379  iter != m_elements.end();
380  ++iter){
381  *iter = value;
382  }
383 }
384 
385 
386 // operators
387 
388 template <class Element, int Dimensions>
390 {
391  return (m_sizes == value.m_sizes) && (m_elements == value.m_elements);
392 }
393 
394 
395 template <class Element, int Dimensions>
397 {
398  return (m_sizes != value.m_sizes) || (m_elements != value.m_elements);
399 }
400 
401 
402 // protected methods
403 
404 template <class Element, int Dimensions>
406 {
407  int cumulatedSizes = 1;
408  for (int i = 0; i < Dimensions; ++i){
409  cumulatedSizes *= m_sizes[i];
410  }
411 
412  m_elements.resize(cumulatedSizes, value);
413 }
414 
415 
416 template <class Element, int Dimensions>
417 void TArray<Element, Dimensions>::DeepCopy(const Elements& elements, const SizesType& sizes)
418 {
419  Q_ASSERT(int(elements.size()) == sizes[0] * sizes[1]);
420 
421  m_elements = elements;
422  m_sizes = sizes;
423 }
424 
425 
426 // static attributes
427 
428 template <class Element, int Dimensions>
430 
431 
432 // public methods of embedded class Iterator
433 
434 template <class Element, int Dimensions>
435 TArray<Element, Dimensions>::Iterator::Iterator(const Iterator& iterator)
436 : BaseClass(iterator), m_arrayPtr(iterator.m_arrayPtr)
437 {
438 }
439 
440 
441 template <class Element, int Dimensions>
443 {
444  Q_ASSERT(m_arrayPtr != NULL);
445  Q_ASSERT(BaseClass::IsInside(m_arrayPtr->GetSizes()));
446 
447  return m_arrayPtr->GetAt(*this);
448 }
449 
450 
451 template <class Element, int Dimensions>
453 {
454  Q_ASSERT(m_arrayPtr != NULL);
455  Q_ASSERT(BaseClass::IsInside(m_arrayPtr->GetSizes()));
456 
457  return m_arrayPtr->operator[](*this);
458 }
459 
460 
461 template <class Element, int Dimensions>
463 {
464  Q_ASSERT(m_arrayPtr != NULL);
465  Q_ASSERT(BaseClass::IsInside(m_arrayPtr->GetSizes()));
466 
467  return &m_arrayPtr->GetAt(*this);
468 }
469 
470 
471 template <class Element, int Dimensions>
473 {
474  Q_ASSERT(m_arrayPtr != NULL);
475  Q_ASSERT(BaseClass::IsInside(m_arrayPtr->GetSizes()));
476 
477  return &m_arrayPtr->operator[](*this);
478 }
479 
480 
481 template <class Element, int Dimensions>
483 {
484  if ((m_arrayPtr != NULL) && !BaseClass::Increase(m_arrayPtr->GetSizes())){
485  m_arrayPtr = NULL;
486  }
487 
488  return *this;
489 }
490 
491 
492 template <class Element, int Dimensions>
494 {
495  Iterator retVal = *this;
496 
497  if ((m_arrayPtr != NULL) && !BaseClass::Increase(m_arrayPtr->GetSizes())){
498  m_arrayPtr = NULL;
499  }
500 
501  return retVal;
502 }
503 
504 
505 template <class Element, int Dimensions>
507 {
508  if ((m_arrayPtr != NULL) && !Decrease(m_arrayPtr->GetSizes())){
509  m_arrayPtr = NULL;
510  }
511 
512  return *this;
513 }
514 
515 
516 template <class Element, int Dimensions>
518 {
519  Iterator retVal = *this;
520 
521  if ((m_arrayPtr != NULL) && !Decrease(m_arrayPtr->GetSizes())){
522  m_arrayPtr = NULL;
523  }
524 
525  return retVal;
526 }
527 
528 
529 template <class Element, int Dimensions>
531 {
532  BaseClass::operator=(iterator);
533 
534  m_arrayPtr = iterator.m_arrayPtr;
535 }
536 
537 
538 template <class Element, int Dimensions>
540 {
541  if ((m_arrayPtr != NULL) && (iterator.m_arrayPtr != NULL)){
542  return (m_arrayPtr == iterator.m_arrayPtr) && (BaseClass::operator==(iterator));
543  }
544 
545  return (m_arrayPtr == iterator.m_arrayPtr);
546 }
547 
548 
549 template <class Element, int Dimensions>
551 {
552  return !operator==(iterator);
553 }
554 
555 
556 // protected methods of emedded class Iterator
557 
558 template <class Element, int Dimensions>
560 : BaseClass(0), m_arrayPtr(arrayPtr)
561 {
562  if ((m_arrayPtr != NULL) && !BaseClass::IsInside(m_arrayPtr->GetSizes())){
563  m_arrayPtr = NULL; // if it is not inside of array set it directly to the end iterator state
564  }
565 }
566 
567 
568 } // namespace istd
569 
570 
571 #endif // !istd_TArray_included
572 
573 
TIndex< Dimensions > SizesType
Definition: TArray.h:27
int * Iterator
Definition: TIndex.h:21
Iterator & operator=(const Iterator &iterator)
Definition: TArray.h:530
void SetAt(const IndexType &index, const Element &value)
Set element at specified index.
Definition: TArray.h:251
Multidimensional index used to addressing fixed-size array.
Definition: TIndex.h:17
const Element & GetAt(const IndexType &index) const
Get element stored at specified index.
Definition: TArray.h:227
Multidimensional array with fixed number of dimensions.
Definition: TArray.h:23
Iterator(const Iterator &iterator)
bool IsDimensionsCountFixed() const
Check, if number dimensions is fixed.
Definition: TArray.h:189
void SetAllElements(const Element &value)
Set some value to all elements.
Definition: TArray.h:376
TIndex< Dimensions > IndexType
Definition: TArray.h:26
bool SetSize(int dimension, int size)
Set size of array for specified dimension.
Definition: TArray.h:362
Element ElementType
Definition: TArray.h:28
Elements m_elements
Definition: TArray.h:172
int GetElementIndex(const IndexType &index) const
Get index of element in one dimensional array.
Definition: TArray.h:298
Iterator & operator++()
Definition: TArray.h:482
std::vector< Element > Elements
Definition: TArray.h:158
bool SetSizes(const SizesType &sizes)
Set list of all sizes.
Definition: TArray.h:351
const Iterator & End() const
Get end value of element access iterator.
Definition: TArray.h:272
bool operator==(const Iterator &iterator) const
Definition: TArray.h:539
void DeepCopy(const Elements &elements, const SizesType &sizes)
Definition: TArray.h:417
Element & GetAtRef(const IndexType &index)
Get reference to element stored at specified index.
Definition: TArray.h:239
#define NULL
Definition: istd.h:64
TIndex< Dimensions > BaseClass
Definition: TArray.h:33
Iterator Begin() const
Get begin value of element access iterator.
Definition: TArray.h:265
void UpdateElementsSize(const ElementType &value=ElementType())
Update size of elements to size changes.
Definition: TArray.h:405
int GetDimensionsCount() const
Get number of dimensions of this array.
Definition: TArray.h:196
void Reset()
Removes all elements and set all sizes to 0.
Definition: TArray.h:342
bool IsEmpty() const
Check if this array has no elements.
Definition: TArray.h:182
const SizesType & GetSizes() const
Get list of all sizes.
Definition: TArray.h:210
bool operator!=(const TArray< Element, Dimensions > &value) const
Definition: TArray.h:396
bool IsInside(const TIndex &boundaries) const
Check if index is inside boundaries.
Definition: TIndex.h:456
Element & operator*()
Definition: TArray.h:452
Iterator & operator--()
Definition: TArray.h:506
const Element & operator[](const IndexType &index) const
Definition: TArray.h:279
int GetSize(int dimension) const
Get size of array for specified dimension.
Definition: TArray.h:217
bool operator==(const TArray< Element, Dimensions > &value) const
Definition: TArray.h:389
bool operator!=(const Iterator &iterator) const
Definition: TArray.h:550
bool SetDimensionsCount(int count)
Set number of dimensions of this array.
Definition: TArray.h:203
Element * operator->()
Definition: TArray.h:472
SizesType m_sizes
Definition: TArray.h:171

© 2007-2017 Witold Gantzke and Kirill Lepskiy