ACF $AcfVersion:0$
CVarIndex.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// Qt includes
6#include <QtCore/QVector>
7
8// ACF includes
9#include <istd/TIndex.h>
10
11// std includes
12#include <vector>
13
14
15namespace istd
16{
17
18
23{
24public:
25 typedef int IndexType;
26 typedef QVector<int> Elements;
27 typedef Elements::iterator Iterator;
28
33
37 explicit CVarIndex(int size, int value = 0);
38
42 CVarIndex(const CVarIndex& index);
43
47 CVarIndex(const std::vector<IndexType>& values);
48
52 CVarIndex(const Elements& values);
53
57 template <int Dimensions>
58 CVarIndex(const TIndex<Dimensions> index);
59
60 std::vector<int> ToStdVector() const
61 {
62 return std::vector<int>(m_elements.begin(), m_elements.end());
63 }
64
69 bool IsValid() const;
70
75 bool IsZero() const;
76
81 bool IsSizeEmpty() const;
82
87 void Reset();
88
92 void Clear();
93
98 bool IsDimensionsCountFixed() const;
99
103 int GetDimensionsCount() const;
104
111 bool SetDimensionsCount(int count, int value = 0);
112
116 int GetAt(int index) const;
117
121 void SetAt(int index, int value);
122
126 void SetAllTo(int value);
127
133 bool IncreaseAt(int index);
134
140 bool DecreaseAt(int index);
141
146 bool IsInside(const CVarIndex& boundaries) const;
147
154 bool Increase(const CVarIndex& boundaries);
155
162 bool Decrease(const CVarIndex& boundaries);
163
168 int GetProductVolume() const;
169
173 int GetIterationIndex(const CVarIndex& boundaries) const;
174
179 int GetMinDimensionsCount() const;
180
184 istd::CVarIndex GetExpanded(const istd::CVarIndex& other) const;
185
190 Iterator Begin() const;
195 Iterator End() const;
196
200 int operator[](int index) const;
204 int& operator[](int index);
205
206 bool operator==(const CVarIndex& index) const;
207 bool operator!=(const CVarIndex& index) const;
208 bool operator<(const CVarIndex& index) const;
209 bool operator>(const CVarIndex& index) const;
210 bool operator<=(const CVarIndex& index) const;
211 bool operator>=(const CVarIndex& index) const;
212
216
217 friend uint qHash(const CVarIndex& index, uint seed);
218
219private:
220 Elements m_elements;
221};
222
223
224// inline methods
225
227{
228 return false;
229}
230
231
233{
234 return int(m_elements.size());
235}
236
237
238inline bool CVarIndex::SetDimensionsCount(int count, int value)
239{
240 int lastSize = m_elements.size();
241
242 m_elements.resize(count);
243
244 for (int i = lastSize; i < count; ++i){
245 m_elements[i] = value;
246 }
247
248 return true;
249}
250
251
252inline int CVarIndex::GetAt(int index) const
253{
254 Q_ASSERT(index >= 0);
255 Q_ASSERT(index < int(m_elements.size()));
256
257 return m_elements[index];
258}
259
260
261inline void CVarIndex::SetAt(int index, int value)
262{
263 Q_ASSERT(index >= 0);
264 Q_ASSERT(index < int(m_elements.size()));
265
266 m_elements[index] = value;
267}
268
269
270inline bool CVarIndex::IncreaseAt(int index)
271{
272 Q_ASSERT(index >= 0);
273 Q_ASSERT(index < int(m_elements.size()));
274
275 ++m_elements[index];
276
277 return true;
278}
279
280
281inline bool CVarIndex::DecreaseAt(int index)
282{
283 Q_ASSERT(index >= 0);
284 Q_ASSERT(index < int(m_elements.size()));
285
286 --m_elements[index];
287
288 return true;
289}
290
291
293{
294 int retVal = 1;
295
296 int dimensionsCount = GetDimensionsCount();
297
298 for (int i = 0; i < dimensionsCount; ++i){
299 retVal *= GetAt(i);
300 }
301
302 return retVal;
303}
304
305
307{
308 int dimensionsCount = GetDimensionsCount();
309
310 for (int i = dimensionsCount - 1; i >= 0; --i){
311 if (GetAt(i) > 0){
312 return i + 1;
313 }
314 }
315
316 return 0;
317}
318
319
321{
322 istd::CVarIndex retVal(*this);
323
324 retVal.m_elements += other.m_elements;
325
326 return retVal;
327}
328
329
331{
332 return const_cast<CVarIndex*>(this)->m_elements.begin();
333}
334
335
337{
338 return const_cast<CVarIndex*>(this)->m_elements.end();
339}
340
341
342inline int CVarIndex::operator[](int index) const
343{
344 return GetAt(index);
345}
346
347
348inline int& CVarIndex::operator[](int index)
349{
350 Q_ASSERT(index >= 0);
351 Q_ASSERT(index < int(m_elements.size()));
352
353 return m_elements[index];
354}
355
356
357
358inline bool CVarIndex::operator<(const CVarIndex& index) const
359{
360 int count = m_elements.size();
361 int indexCount = index.m_elements.size();
362 int commonSize = qMin(count, indexCount);
363 for (int i = 0; i < commonSize; ++i){
364 int element = m_elements[i];
365 int indexElement = index.m_elements[i];
366 if (element != indexElement){
367 return element < indexElement;
368 }
369 }
370
371 return count < indexCount;
372}
373
374
375inline bool CVarIndex::operator>(const CVarIndex& index) const
376{
377 int count = m_elements.size();
378 int indexCount = index.m_elements.size();
379 int commonSize = qMin(count, indexCount);
380 for (int i = 0; i < commonSize; ++i){
381 int element = m_elements[i];
382 int indexElement = index.m_elements[i];
383 if (element != indexElement){
384 return element > indexElement;
385 }
386 }
387
388 return count > indexCount;
389}
390
391
392inline bool CVarIndex::operator<=(const CVarIndex& index) const
393{
394 int count = m_elements.size();
395 int indexCount = index.m_elements.size();
396 int commonSize = qMin(count, indexCount);
397 for (int i = 0; i < commonSize; ++i){
398 int element = m_elements[i];
399 int indexElement = index.m_elements[i];
400 if (element != indexElement){
401 return element < indexElement;
402 }
403 }
404
405 return count <= indexCount;
406}
407
408
409inline bool CVarIndex::operator>=(const CVarIndex& index) const
410{
411 int count = m_elements.size();
412 int indexCount = index.m_elements.size();
413 int commonSize = qMin(count, indexCount);
414 for (int i = 0; i < commonSize; ++i){
415 int element = m_elements[i];
416 int indexElement = index.m_elements[i];
417 if (element != indexElement){
418 return element > indexElement;
419 }
420 }
421
422 return count >= indexCount;
423}
424
425
426// public template methods
427
428template <int Dimensions>
430: m_elements(Dimensions)
431{
432 for (int i = 0; i < Dimensions; ++i){
433 m_elements[i] = index[i];
434 }
435}
436
437
438// related global functions
439
440uint qHash(const CVarIndex& index, uint seed = 0);
441
442
443} // namespace istd
444
445
446
447
Multidimensional index used to addressing index.
Definition CVarIndex.h:23
CVarIndex(const std::vector< IndexType > &values)
Construct index from std::vector.
void Clear()
Set all components to 0.
bool IsValid() const
Check if tihs index is valid.
bool operator>(const CVarIndex &index) const
Definition CVarIndex.h:375
friend uint qHash(const CVarIndex &index, uint seed)
CVarIndex(int size, int value=0)
Constructor initializing all member to specified value.
CVarIndex(const Elements &values)
Construct index from QVector.
bool operator<=(const CVarIndex &index) const
Definition CVarIndex.h:392
bool operator!=(const CVarIndex &index) const
bool IsDimensionsCountFixed() const
Check, if number dimensions is fixed.
Definition CVarIndex.h:226
QVector< int > Elements
Definition CVarIndex.h:26
bool IncreaseAt(int index)
Increase single component at specified position.
Definition CVarIndex.h:270
std::vector< int > ToStdVector() const
Definition CVarIndex.h:60
int GetMinDimensionsCount() const
Get minimal number of dimensions needed to to represent this index.
Definition CVarIndex.h:306
bool Increase(const CVarIndex &boundaries)
Increase this index inside the boundaries.
CVarIndex & operator+=(const CVarIndex &index)
bool IsSizeEmpty() const
Check if this index interpreted as size is empty.
Elements::iterator Iterator
Definition CVarIndex.h:27
int GetAt(int index) const
Get element stored at specified index.
Definition CVarIndex.h:252
bool IsZero() const
Check if this index point at zero element.
CVarIndex & operator=(const CVarIndex &index)
void SetAt(int index, int value)
Set element at specified index.
Definition CVarIndex.h:261
CVarIndex(const CVarIndex &index)
Copy constructor.
void SetAllTo(int value)
Set all components to specified value.
int GetProductVolume() const
Get total number of elements if this index is treated as size.
Definition CVarIndex.h:292
bool operator<(const CVarIndex &index) const
Definition CVarIndex.h:358
void Reset()
Reset this object.
bool IsInside(const CVarIndex &boundaries) const
Check if index is inside boundaries.
int operator[](int index) const
Get access to single index components.
Definition CVarIndex.h:342
bool operator>=(const CVarIndex &index) const
Definition CVarIndex.h:409
int GetDimensionsCount() const
Get number of dimensions of this index.
Definition CVarIndex.h:232
bool operator==(const CVarIndex &index) const
istd::CVarIndex GetExpanded(const istd::CVarIndex &other) const
Get the index expanded by another index.
Definition CVarIndex.h:320
bool SetDimensionsCount(int count, int value=0)
Set number of dimensions of this index.
Definition CVarIndex.h:238
bool DecreaseAt(int index)
Decrease single component at specified position.
Definition CVarIndex.h:281
int GetIterationIndex(const CVarIndex &boundaries) const
Get index of iteration from zero to current index inside some boundaries.
Iterator Begin() const
Get begin value of element access iterator.
Definition CVarIndex.h:330
bool Decrease(const CVarIndex &boundaries)
Decrese this index inside the boundaries.
CVarIndex & operator-=(const CVarIndex &index)
Iterator End() const
Get end value of element access iterator.
Definition CVarIndex.h:336
CVarIndex()
Default constructor without member initialization.
Multidimensional index used to addressing fixed-size array.
Definition TIndex.h:18
Standard library.
Definition IComponent.h:17
uint qHash(const CVarIndex &index, uint seed=0)