ACF $AcfVersion:0$
TArray.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
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
15namespace istd
16{
17
18
22template <class Element, int Dimensions>
23class TArray
24{
25public:
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
45
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 = nullptr;
61 };
62
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
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
157protected:
158 typedef std::vector<Element> Elements;
159
163 int GetElementIndex(const IndexType& index) const;
168
169 void DeepCopy(const Elements& elements, const SizesType& sizes);
170
173
174private:
175 static Iterator s_endIterator;
176};
177
178
179// inline methods
180
181template <class Element, int Dimensions>
183{
184 return m_sizes.IsSizeEmpty();
185}
186
187
188template <class Element, int Dimensions>
190{
191 return true;
192}
193
194
195template <class Element, int Dimensions>
197{
198 return Dimensions;
199}
200
201
202template <class Element, int Dimensions>
204{
205 return (count == GetDimensionsCount());
206}
207
208
209template <class Element, int Dimensions>
211{
212 return m_sizes;
213}
214
215
216template <class Element, int Dimensions>
217inline 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
226template <class Element, int Dimensions>
227inline 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
238template <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
250template <class Element, int Dimensions>
251inline 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
264template <class Element, int Dimensions>
269
270
271template <class Element, int Dimensions>
273{
274 return s_endIterator;
275}
276
277
278template <class Element, int Dimensions>
279inline const Element& TArray<Element, Dimensions>::operator[](const IndexType& index) const
280{
281 return GetAt(index);
282}
283
284
285template <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
297template <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
317template <class Element, int Dimensions>
319{
320 for (int i = 0; i < Dimensions; ++i){
321 m_sizes[i] = 0;
322 }
323}
324
325
326template <class Element, int Dimensions>
328: m_sizes(array.m_sizes), m_elements(array.m_elements)
329{
330}
331
332
333template <class Element, int Dimensions>
335: m_sizes(sizes)
336{
337 UpdateElementsSize(value);
338}
339
340
341template <class Element, int Dimensions>
343{
344 m_sizes.Reset();
345
346 m_elements.clear();
347}
348
349
350template <class Element, int Dimensions>
352{
353 m_sizes = sizes;
354
355 UpdateElementsSize();
356
357 return true;
358}
359
360
361template <class Element, int Dimensions>
362bool 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
375template <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
388template <class Element, int Dimensions>
390{
391 return (m_sizes == value.m_sizes) && (m_elements == value.m_elements);
392}
393
394
395template <class Element, int Dimensions>
397{
398 return (m_sizes != value.m_sizes) || (m_elements != value.m_elements);
399}
400
401
402// protected methods
403
404template <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
416template <class Element, int Dimensions>
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
428template <class Element, int Dimensions>
430
431
432// public methods of embedded class Iterator
433
434template <class Element, int Dimensions>
436: BaseClass(iterator), m_arrayPtr(iterator.m_arrayPtr)
437{
438}
439
440
441template <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
451template <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
461template <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
471template <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
481template <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
492template <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
505template <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
516template <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
529template <class Element, int Dimensions>
531{
532 BaseClass::operator=(iterator);
533
534 m_arrayPtr = iterator.m_arrayPtr;
535
536 return *this;
537}
538
539
540template <class Element, int Dimensions>
542{
543 if ((m_arrayPtr != NULL) && (iterator.m_arrayPtr != NULL)){
544 return (m_arrayPtr == iterator.m_arrayPtr) && (BaseClass::operator==(iterator));
545 }
546
547 return (m_arrayPtr == iterator.m_arrayPtr);
548}
549
550
551template <class Element, int Dimensions>
553{
554 return !operator==(iterator);
555}
556
557
558// protected methods of emedded class Iterator
559
560template <class Element, int Dimensions>
562: BaseClass(0), m_arrayPtr(arrayPtr)
563{
564 if ((m_arrayPtr != NULL) && !BaseClass::IsInside(m_arrayPtr->GetSizes())){
565 m_arrayPtr = NULL; // if it is not inside of array set it directly to the end iterator state
566 }
567}
568
569
570} // namespace istd
571
572
573
574
TIndex< Dimensions > BaseClass
Definition TArray.h:33
Iterator & operator=(const Iterator &iterator)
Definition TArray.h:530
Element * operator->()
Definition TArray.h:472
Element & operator*()
Definition TArray.h:452
Iterator & operator++()
Definition TArray.h:482
bool operator==(const Iterator &iterator) const
Definition TArray.h:541
Iterator & operator--()
Definition TArray.h:506
bool operator!=(const Iterator &iterator) const
Definition TArray.h:552
Multidimensional array with fixed number of dimensions.
Definition TArray.h:24
TIndex< Dimensions > IndexType
Definition TArray.h:26
void SetAllElements(const Element &value)
Set some value to all elements.
Definition TArray.h:376
const Iterator & End() const
Get end value of element access iterator.
Definition TArray.h:272
bool IsEmpty() const
Check if this array has no elements.
Definition TArray.h:182
int GetElementIndex(const IndexType &index) const
Get index of element in one dimensional array.
Definition TArray.h:298
bool SetSize(int dimension, int size)
Set size of array for specified dimension.
Definition TArray.h:362
const Element & operator[](const IndexType &index) const
Definition TArray.h:279
SizesType m_sizes
Definition TArray.h:171
void Reset()
Removes all elements and set all sizes to 0.
Definition TArray.h:342
int GetDimensionsCount() const
Get number of dimensions of this array.
Definition TArray.h:196
bool operator!=(const TArray< Element, Dimensions > &value) const
Definition TArray.h:396
TArray(const SizesType &sizes, const ElementType &value=ElementType())
Definition TArray.h:334
Element & GetAtRef(const IndexType &index)
Get reference to element stored at specified index.
Definition TArray.h:239
bool IsDimensionsCountFixed() const
Check, if number dimensions is fixed.
Definition TArray.h:189
TArray(const TArray &array)
Definition TArray.h:327
bool SetSizes(const SizesType &sizes)
Set list of all sizes.
Definition TArray.h:351
const Element & GetAt(const IndexType &index) const
Get element stored at specified index.
Definition TArray.h:227
void SetAt(const IndexType &index, const Element &value)
Set element at specified index.
Definition TArray.h:251
Elements m_elements
Definition TArray.h:172
Element & operator[](const IndexType &index)
Definition TArray.h:286
Iterator Begin() const
Get begin value of element access iterator.
Definition TArray.h:265
Element ElementType
Definition TArray.h:28
TIndex< Dimensions > SizesType
Definition TArray.h:27
void UpdateElementsSize(const ElementType &value=ElementType())
Update size of elements to size changes.
Definition TArray.h:405
std::vector< Element > Elements
Definition TArray.h:158
bool operator==(const TArray< Element, Dimensions > &value) const
Definition TArray.h:389
int GetSize(int dimension) const
Get size of array for specified dimension.
Definition TArray.h:217
const SizesType & GetSizes() const
Get list of all sizes.
Definition TArray.h:210
void DeepCopy(const Elements &elements, const SizesType &sizes)
Definition TArray.h:417
bool SetDimensionsCount(int count)
Set number of dimensions of this array.
Definition TArray.h:203
Multidimensional index used to addressing fixed-size array.
Definition TIndex.h:18
int * Iterator
Definition TIndex.h:21
bool IsInside(const TIndex &boundaries) const
Check if index is inside boundaries.
Definition TIndex.h:457
bool IsSizeEmpty() const
Check if this index interpreted as size is empty.
Definition TIndex.h:242
#define NULL
Definition istd.h:74
Standard library.
Definition IComponent.h:17