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

© Witold Gantzke and Kirill Lepskiy