ICF 3.1.1.10
Technical documentation of ICF Libraries
CFastBinaryIndex.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// ICF includes
10#include <istd/CBitManip.h>
11
12
13namespace istd
14{
15
16
18{
19public:
20 typedef int IndexType;
21
22 enum{
24 };
25
27 {
28 public:
29 iterator();
30 iterator(const CFastBinaryIndex* indexPtr, int position);
31 iterator(const iterator& iter);
32
33 int operator*();
36
37 bool operator==(const iterator& iter) const;
38 bool operator!=(const iterator& iter) const;
39
40 iterator& operator=(const iterator& iter);
41
42 private:
43 const CFastBinaryIndex* m_indexPtr;
44 int m_position;
45 };
46
51
55 explicit CFastBinaryIndex(int size, int value = 0);
56
64 explicit CFastBinaryIndex(quint32 bits, int size, int dummy);
65
70
76 bool IsValid() const;
77
82 bool IsZero() const;
83
89 void Reset();
90
94 void Clear();
95
100 bool IsDimensionsCountFixed() const;
101
105 int GetDimensionsCount() const;
106
113 bool SetDimensionsCount(int count);
114
118 int GetAt(int index) const;
119
123 void SetAt(int index, int value);
124
128 quint32 GetBits() const;
129
133 void SetAllTo(int value);
134
140 bool IncreaseAt(int index);
141
147 bool DecreaseAt(int index);
148
153 int GetProductVolume() const;
154
159 bool IsInside(const CFastBinaryIndex& boundaries) const;
160
165 bool Increase(const CFastBinaryIndex& boundaries);
166
171 bool Decrease(const CFastBinaryIndex& boundaries);
172
177 iterator Begin() const;
182 iterator End() const;
183
184 int operator[](int index) const;
185
186 bool operator==(const CFastBinaryIndex& index) const;
187 bool operator!=(const CFastBinaryIndex& index) const;
188
189private:
190 quint32 m_bits;
191 int m_size;
192};
193
194
195// inline methods
196
198: m_bits(0), m_size(0)
199{
200}
201
202
203inline CFastBinaryIndex::CFastBinaryIndex(int size, int value)
204{
205 m_size = size;
206
207 SetAllTo(value);
208}
209
210
211inline CFastBinaryIndex::CFastBinaryIndex(quint32 bits, int size, int /*dummy*/)
212: m_bits(bits), m_size(size)
213{
214 Q_ASSERT(CBitManip::instance.GetFirstBitIndex(bits) < size);
215}
216
217
219: m_bits(index.m_bits), m_size(index.m_size)
220{
221 Q_ASSERT(m_size <= MAX_ELEMENTS_COUNT);
222 Q_ASSERT((m_bits >> m_size) == 0);
223}
224
225
226inline bool CFastBinaryIndex::IsValid() const
227{
228 return true;
229}
230
231
232inline bool CFastBinaryIndex::IsZero() const
233{
234 return m_bits == 0;
235}
236
237
239{
240 m_size = 0;
241 m_bits = 0;
242}
243
244
246{
247 m_size = 0;
248 m_bits = 0;
249}
250
251
253{
254 return false;
255}
256
257
259{
260 return m_size;
261}
262
263
265{
266 if (count <= MAX_ELEMENTS_COUNT){
267 m_size = count;
268 m_bits &= quint32(1 << m_size) - 1;
269
270 return true;
271 }
272
273 return false;
274}
275
276
277inline int CFastBinaryIndex::GetAt(int index) const
278{
279 Q_ASSERT(index >= 0);
280 Q_ASSERT(index < m_size);
281
282 return int(m_bits >> index) & 1;
283}
284
285
286inline void CFastBinaryIndex::SetAt(int index, int value)
287{
288 Q_ASSERT(index >= 0);
289 Q_ASSERT(index < m_size);
290
291 if (value > 0){
292 m_bits |= quint32(1 << index);
293 }
294 else{
295 m_bits &= ~quint32(1 << index);
296 }
297}
298
299
300inline quint32 CFastBinaryIndex::GetBits() const
301{
302 return m_bits;
303}
304
305
306inline void CFastBinaryIndex::SetAllTo(int value)
307{
308 if (value){
309 m_bits = quint32(1 << m_size) - 1;
310 }
311 else{
312 m_bits = 0;
313 }
314}
315
316
317inline bool CFastBinaryIndex::IncreaseAt(int index)
318{
319 Q_ASSERT(index >= 0);
320 Q_ASSERT(index < m_size);
321
322 quint32 mask = quint32(1 << index);
323
324 bool retVal = ((m_bits & mask) == 0);
325
326 m_bits |= mask;
327
328 return retVal;
329}
330
331
332inline bool CFastBinaryIndex::DecreaseAt(int index)
333{
334 Q_ASSERT(index >= 0);
335 Q_ASSERT(index < m_size);
336
337 quint32 mask = quint32(1 << index);
338
339 bool retVal = ((m_bits & mask) != 0);
340
341 m_bits &= ~mask;
342
343 return retVal;
344}
345
346
347inline bool CFastBinaryIndex::IsInside(const CFastBinaryIndex& boundaries) const
348{
349 return (((boundaries.m_bits + 1) & quint32(1 << m_size)) != 0);
350}
351
352
353inline bool CFastBinaryIndex::Increase(const CFastBinaryIndex& /*boundaries*/)
354{
355 return false;
356}
357
358
359inline bool CFastBinaryIndex::Decrease(const CFastBinaryIndex& /*boundaries*/)
360{
361 return false;
362}
363
364
366{
367 return int(m_bits + 1) >> m_size; // it is 1 if all bits are 1
368}
369
370
372{
373 return iterator(this, 0);
374}
375
376
378{
379 return iterator(this, m_size);
380}
381
382
383inline int CFastBinaryIndex::operator[](int index) const
384{
385 return GetAt(index);
386}
387
388
389inline bool CFastBinaryIndex::operator==(const CFastBinaryIndex& index) const
390{
391 return (m_size == index.m_size) && (m_bits == index.m_bits);
392}
393
394
395inline bool CFastBinaryIndex::operator!=(const CFastBinaryIndex& index) const
396{
397 return (m_size != index.m_size) || (m_bits != index.m_bits);
398}
399
400
401// inline methods of embedded class iterator
402
404: m_indexPtr(nullptr), m_position(0)
405{
406}
407
408
409inline CFastBinaryIndex::iterator::iterator(const CFastBinaryIndex* indexPtr, int position)
410: m_indexPtr(indexPtr), m_position(position)
411{
412}
413
414
416: m_indexPtr(iter.m_indexPtr), m_position(iter.m_position)
417{
418}
419
420
422{
423 Q_ASSERT(m_indexPtr != nullptr);
424
425 return m_indexPtr->GetAt(m_position);
426}
427
428
430{
431 ++m_position;
432
433 return *this;
434}
435
436
438{
439 iterator retVal(*this);
440
441 ++m_position;
442
443 return retVal;
444}
445
446
448{
449 return (m_indexPtr == iter.m_indexPtr) && (m_position == iter.m_position);
450}
451
452
454{
455 return (m_indexPtr != iter.m_indexPtr) || (m_position != iter.m_position);
456}
457
458
460{
461 m_indexPtr = iter.m_indexPtr;
462 m_position = iter.m_position;
463
464 return *this;
465}
466
467
468} // namespace istd
469
470
static CBitManip instance
Definition CBitManip.h:30
bool operator==(const iterator &iter) const
iterator & operator=(const iterator &iter)
bool operator!=(const iterator &iter) const
bool IsInside(const CFastBinaryIndex &boundaries) const
Check if index is inside boundaries.
quint32 GetBits() const
Get bit coded value of this index.
CFastBinaryIndex()
Default constructor without member initialization.
void Clear()
Set all components to 0 (false).
void SetAllTo(int value)
Set all components to specified value.
bool IncreaseAt(int index)
Increase single component at specified position.
bool IsDimensionsCountFixed() const
Check, if number dimensions is fixed.
bool operator!=(const CFastBinaryIndex &index) const
bool SetDimensionsCount(int count)
Set number of dimensions of this index.
bool Decrease(const CFastBinaryIndex &boundaries)
Decrese this index inside the boundaries.
bool operator==(const CFastBinaryIndex &index) const
int operator[](int index) const
bool IsValid() const
Check if this index is valid.
bool DecreaseAt(int index)
Decrease single component at specified position.
bool IsZero() const
Check if this index point at zero element.
int GetProductVolume() const
Get total number of elements if this index is treated as size.
int GetAt(int index) const
Get element stored at specified index.
int GetDimensionsCount() const
Get number of dimensions of this index.
bool Increase(const CFastBinaryIndex &boundaries)
Increase this index inside the boundaries.
iterator Begin() const
Get begin value of element access iterator.
void Reset()
Reset this object.
void SetAt(int index, int value)
Set element at specified index.
iterator End() const
Get end value of element access iterator.
Standard library.
Definition IComponent.h:17

© Witold Gantzke and Kirill Lepskiy