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

© Witold Gantzke and Kirill Lepskiy