CVarIndex.h
Go to the documentation of this file.
1 #ifndef istd_CVarIndex_included
2 #define istd_CVarIndex_included
3 
4 
5 // Qt includes
6 #include <QtCore/QVector>
7 
8 // ACF includes
9 #include <istd/TIndex.h>
10 
11 
12 namespace istd
13 {
14 
15 
19 class CVarIndex
20 {
21 public:
22  typedef int IndexType;
23  typedef QVector<int>::iterator Iterator;
24 
28  CVarIndex();
29 
33  explicit CVarIndex(int size, int value = 0);
34 
38  CVarIndex(const CVarIndex& index);
39 
43  template <int Dimensions>
44  CVarIndex(const TIndex<Dimensions> index);
45 
50  bool IsValid() const;
51 
56  bool IsZero() const;
57 
62  bool IsSizeEmpty() const;
63 
68  void Reset();
69 
73  void Clear();
74 
79  bool IsDimensionsCountFixed() const;
80 
84  int GetDimensionsCount() const;
85 
92  bool SetDimensionsCount(int count, int value = 0);
93 
97  int GetAt(int index) const;
98 
102  void SetAt(int index, int value);
103 
107  void SetAllTo(int value);
108 
114  bool IncreaseAt(int index);
115 
121  bool DecreaseAt(int index);
122 
127  bool IsInside(const CVarIndex& boundaries) const;
128 
135  bool Increase(const CVarIndex& boundaries);
136 
143  bool Decrease(const CVarIndex& boundaries);
144 
149  int GetProductVolume() const;
150 
154  int GetIterationIndex(const CVarIndex& boundaries) const;
155 
160  int GetMinDimensionsCount() const;
161 
166  Iterator Begin() const;
171  Iterator End() const;
172 
176  int operator[](int index) const;
180  int& operator[](int index);
181 
182  bool operator==(const CVarIndex& index) const;
183  bool operator!=(const CVarIndex& index) const;
184  bool operator<(const CVarIndex& index) const;
185  bool operator>(const CVarIndex& index) const;
186  bool operator<=(const CVarIndex& index) const;
187  bool operator>=(const CVarIndex& index) const;
188 
189  CVarIndex& operator=(const CVarIndex& index);
190  CVarIndex& operator+=(const CVarIndex& index);
191  CVarIndex& operator-=(const CVarIndex& index);
192 
193  friend uint qHash(const CVarIndex& index, uint seed);
194 
195 private:
196  QVector<int> m_elements;
197 };
198 
199 
200 // inline methods
201 
203 {
204  return false;
205 }
206 
207 
209 {
210  return int(m_elements.size());
211 }
212 
213 
214 inline bool CVarIndex::SetDimensionsCount(int count, int value)
215 {
216  int lastSize = m_elements.size();
217 
218  m_elements.resize(count);
219 
220  for (int i = lastSize; i < count; ++i){
221  m_elements[i] = value;
222  }
223 
224  return true;
225 }
226 
227 
228 inline int CVarIndex::GetAt(int index) const
229 {
230  Q_ASSERT(index >= 0);
231  Q_ASSERT(index < int(m_elements.size()));
232 
233  return m_elements[index];
234 }
235 
236 
237 inline void CVarIndex::SetAt(int index, int value)
238 {
239  Q_ASSERT(index >= 0);
240  Q_ASSERT(index < int(m_elements.size()));
241 
242  m_elements[index] = value;
243 }
244 
245 
246 inline bool CVarIndex::IncreaseAt(int index)
247 {
248  Q_ASSERT(index >= 0);
249  Q_ASSERT(index < int(m_elements.size()));
250 
251  ++m_elements[index];
252 
253  return true;
254 }
255 
256 
257 inline bool CVarIndex::DecreaseAt(int index)
258 {
259  Q_ASSERT(index >= 0);
260  Q_ASSERT(index < int(m_elements.size()));
261 
262  --m_elements[index];
263 
264  return true;
265 }
266 
267 
268 inline int CVarIndex::GetProductVolume() const
269 {
270  int retVal = 1;
271 
272  int dimensionsCount = GetDimensionsCount();
273 
274  for (int i = 0; i < dimensionsCount; ++i){
275  retVal *= GetAt(i);
276  }
277 
278  return retVal;
279 }
280 
281 
283 {
284  int dimensionsCount = GetDimensionsCount();
285 
286  for (int i = dimensionsCount - 1; i >= 0; --i){
287  if (GetAt(i) > 0){
288  return i + 1;
289  }
290  }
291 
292  return 0;
293 }
294 
295 
297 {
298  return const_cast<CVarIndex*>(this)->m_elements.begin();
299 }
300 
301 
303 {
304  return const_cast<CVarIndex*>(this)->m_elements.end();
305 }
306 
307 
308 inline int CVarIndex::operator[](int index) const
309 {
310  return GetAt(index);
311 }
312 
313 
314 inline int& CVarIndex::operator[](int index)
315 {
316  Q_ASSERT(index >= 0);
317  Q_ASSERT(index < int(m_elements.size()));
318 
319  return m_elements[index];
320 }
321 
322 
323 
324 inline bool CVarIndex::operator<(const CVarIndex& index) const
325 {
326  int count = m_elements.size();
327  int indexCount = index.m_elements.size();
328  int commonSize = qMin(count, indexCount);
329  for (int i = 0; i < commonSize; ++i){
330  int element = m_elements[i];
331  int indexElement = index.m_elements[i];
332  if (element != indexElement){
333  return element < indexElement;
334  }
335  }
336 
337  return count < indexCount;
338 }
339 
340 
341 inline bool CVarIndex::operator>(const CVarIndex& index) const
342 {
343  int count = m_elements.size();
344  int indexCount = index.m_elements.size();
345  int commonSize = qMin(count, indexCount);
346  for (int i = 0; i < commonSize; ++i){
347  int element = m_elements[i];
348  int indexElement = index.m_elements[i];
349  if (element != indexElement){
350  return element > indexElement;
351  }
352  }
353 
354  return count > indexCount;
355 }
356 
357 
358 inline 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 
375 inline 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 
392 // public template methods
393 
394 template <int Dimensions>
396 : m_elements(Dimensions)
397 {
398  for (int i = 0; i < Dimensions; ++i){
399  m_elements[i] = index[i];
400  }
401 }
402 
403 
404 // related global functions
405 
406 uint qHash(const CVarIndex& index, uint seed = 0);
407 
408 
409 } // namespace istd
410 
411 
412 #endif // !istd_CVarIndex_included
413 
414 
int GetProductVolume() const
Get total number of elements if this index is treated as size.
Definition: CVarIndex.h:268
bool operator>=(const CVarIndex &index) const
Definition: CVarIndex.h:375
bool IsValid() const
Check if tihs index is valid.
bool IsSizeEmpty() const
Check if this index interpreted as size is empty.
bool IsDimensionsCountFixed() const
Check, if number dimensions is fixed.
Definition: CVarIndex.h:202
int GetIterationIndex(const CVarIndex &boundaries) const
Get index of iteration from zero to current index inside some boundaries.
bool operator!=(const CVarIndex &index) const
bool IsInside(const CVarIndex &boundaries) const
Check if index is inside boundaries.
bool SetDimensionsCount(int count, int value=0)
Set number of dimensions of this index.
Definition: CVarIndex.h:214
CVarIndex & operator-=(const CVarIndex &index)
CVarIndex & operator=(const CVarIndex &index)
QVector< int >::iterator Iterator
Definition: CVarIndex.h:23
void Clear()
Set all components to 0.
Multidimensional index used to addressing fixed-size array.
Definition: TIndex.h:17
CVarIndex()
Default constructor without member initialization.
bool operator==(const CVarIndex &index) const
int GetAt(int index) const
Get element stored at specified index.
Definition: CVarIndex.h:228
int operator[](int index) const
Get access to single index components.
Definition: CVarIndex.h:308
bool IncreaseAt(int index)
Increase single component at specified position.
Definition: CVarIndex.h:246
bool DecreaseAt(int index)
Decrease single component at specified position.
Definition: CVarIndex.h:257
Multidimensional index used to addressing index.
Definition: CVarIndex.h:19
bool operator<(const CVarIndex &index) const
Definition: CVarIndex.h:324
Iterator End() const
Get end value of element access iterator.
Definition: CVarIndex.h:302
friend uint qHash(const CVarIndex &index, uint seed)
int GetMinDimensionsCount() const
Get minimal number of dimensions needed to to represent this index.
Definition: CVarIndex.h:282
Iterator Begin() const
Get begin value of element access iterator.
Definition: CVarIndex.h:296
bool Increase(const CVarIndex &boundaries)
Increase this index inside the boundaries.
uint qHash(const CVarIndex &index, uint seed=0)
void Reset()
Reset this object.
void SetAt(int index, int value)
Set element at specified index.
Definition: CVarIndex.h:237
int GetDimensionsCount() const
Get number of dimensions of this index.
Definition: CVarIndex.h:208
void SetAllTo(int value)
Set all components to specified value.
bool IsZero() const
Check if this index point at zero element.
bool operator>(const CVarIndex &index) const
Definition: CVarIndex.h:341
CVarIndex & operator+=(const CVarIndex &index)
bool operator<=(const CVarIndex &index) const
Definition: CVarIndex.h:358
bool Decrease(const CVarIndex &boundaries)
Decrese this index inside the boundaries.

© 2007-2017 Witold Gantzke and Kirill Lepskiy