CFastBinaryIndex.h
Go to the documentation of this file.
1 #ifndef istd_CBinaryIndex_included
2 #define istd_CBinaryIndex_included
3 
4 
5 // ACF includes
6 #include <istd/CBitManip.h>
7 
8 
9 namespace istd
10 {
11 
12 
14 {
15 public:
16  typedef int IndexType;
17 
18  enum{
20  };
21 
22  class Iterator
23  {
24  public:
25  Iterator();
26  Iterator(const CFastBinaryIndex* indexPtr, int position);
27  Iterator(const Iterator& iter);
28 
29  int operator*();
31  Iterator operator++(int);
32 
33  bool operator==(const Iterator& iter) const;
34  bool operator!=(const Iterator& iter) const;
35 
36  Iterator& operator=(const Iterator& iter);
37 
38  private:
39  const CFastBinaryIndex* m_indexPtr;
40  int m_position;
41  };
42 
47 
51  explicit CFastBinaryIndex(int size, int value = 0);
52 
60  explicit CFastBinaryIndex(quint32 bits, int size, int dummy);
61 
65  CFastBinaryIndex(const CFastBinaryIndex& index);
66 
72  bool IsValid() const;
73 
78  bool IsZero() const;
79 
85  void Reset();
86 
90  void Clear();
91 
96  bool IsDimensionsCountFixed() const;
97 
101  int GetDimensionsCount() const;
102 
109  bool SetDimensionsCount(int count);
110 
114  int GetAt(int index) const;
115 
119  void SetAt(int index, int value);
120 
124  quint32 GetBits() const;
125 
129  void SetAllTo(int value);
130 
136  bool IncreaseAt(int index);
137 
143  bool DecreaseAt(int index);
144 
149  int GetProductVolume() const;
150 
155  bool IsInside(const CFastBinaryIndex& boundaries) const;
156 
161  bool Increase(const CFastBinaryIndex& boundaries);
162 
167  bool Decrease(const CFastBinaryIndex& boundaries);
168 
173  Iterator Begin() const;
178  Iterator End() const;
179 
180  int operator[](int index) const;
181 
182  bool operator==(const CFastBinaryIndex& index) const;
183  bool operator!=(const CFastBinaryIndex& index) const;
184 
185 private:
186  quint32 m_bits;
187  int m_size;
188 };
189 
190 
191 // inline methods
192 
194 : m_bits(0), m_size(0)
195 {
196 }
197 
198 
199 inline CFastBinaryIndex::CFastBinaryIndex(int size, int value)
200 {
201  m_size = size;
202 
203  SetAllTo(value);
204 }
205 
206 
207 inline CFastBinaryIndex::CFastBinaryIndex(quint32 bits, int size, int /*dummy*/)
208 : m_bits(bits), m_size(size)
209 {
210  Q_ASSERT(CBitManip::instance.GetFirstBitIndex(bits) < size);
211 }
212 
213 
215 : m_bits(index.m_bits), m_size(index.m_size)
216 {
217  Q_ASSERT(m_size <= MAX_ELEMENTS_COUNT);
218  Q_ASSERT((m_bits >> m_size) == 0);
219 }
220 
221 
222 inline bool CFastBinaryIndex::IsValid() const
223 {
224  return true;
225 }
226 
227 
228 inline bool CFastBinaryIndex::IsZero() const
229 {
230  return m_bits == 0;
231 }
232 
233 
235 {
236  m_size = 0;
237  m_bits = 0;
238 }
239 
240 
242 {
243  m_size = 0;
244  m_bits = 0;
245 }
246 
247 
249 {
250  return false;
251 }
252 
253 
255 {
256  return m_size;
257 }
258 
259 
261 {
262  if (count <= MAX_ELEMENTS_COUNT){
263  m_size = count;
264  m_bits &= quint32(1 << m_size) - 1;
265 
266  return true;
267  }
268 
269  return false;
270 }
271 
272 
273 inline int CFastBinaryIndex::GetAt(int index) const
274 {
275  Q_ASSERT(index >= 0);
276  Q_ASSERT(index < m_size);
277 
278  return int(m_bits >> index) & 1;
279 }
280 
281 
282 inline void CFastBinaryIndex::SetAt(int index, int value)
283 {
284  Q_ASSERT(index >= 0);
285  Q_ASSERT(index < m_size);
286 
287  if (value > 0){
288  m_bits |= quint32(1 << index);
289  }
290  else{
291  m_bits &= ~quint32(1 << index);
292  }
293 }
294 
295 
296 inline quint32 CFastBinaryIndex::GetBits() const
297 {
298  return m_bits;
299 }
300 
301 
302 inline void CFastBinaryIndex::SetAllTo(int value)
303 {
304  if (value){
305  m_bits = quint32(1 << m_size) - 1;
306  }
307  else{
308  m_bits = 0;
309  }
310 }
311 
312 
313 inline bool CFastBinaryIndex::IncreaseAt(int index)
314 {
315  Q_ASSERT(index >= 0);
316  Q_ASSERT(index < m_size);
317 
318  quint32 mask = quint32(1 << index);
319 
320  bool retVal = ((m_bits & mask) == 0);
321 
322  m_bits |= mask;
323 
324  return retVal;
325 }
326 
327 
328 inline bool CFastBinaryIndex::DecreaseAt(int index)
329 {
330  Q_ASSERT(index >= 0);
331  Q_ASSERT(index < m_size);
332 
333  quint32 mask = quint32(1 << index);
334 
335  bool retVal = ((m_bits & mask) != 0);
336 
337  m_bits &= ~mask;
338 
339  return retVal;
340 }
341 
342 
343 inline bool CFastBinaryIndex::IsInside(const CFastBinaryIndex& boundaries) const
344 {
345  return (((boundaries.m_bits + 1) & quint32(1 << m_size)) != 0);
346 }
347 
348 
349 inline bool CFastBinaryIndex::Increase(const CFastBinaryIndex& /*boundaries*/)
350 {
351  return false;
352 }
353 
354 
355 inline bool CFastBinaryIndex::Decrease(const CFastBinaryIndex& /*boundaries*/)
356 {
357  return false;
358 }
359 
360 
362 {
363  return int(m_bits + 1) >> m_size; // it is 1 if all bits are 1
364 }
365 
366 
368 {
369  return Iterator(this, 0);
370 }
371 
372 
374 {
375  return Iterator(this, m_size);
376 }
377 
378 
379 inline int CFastBinaryIndex::operator[](int index) const
380 {
381  return GetAt(index);
382 }
383 
384 
385 inline bool CFastBinaryIndex::operator==(const CFastBinaryIndex& index) const
386 {
387  return (m_size == index.m_size) && (m_bits == index.m_bits);
388 }
389 
390 
391 inline bool CFastBinaryIndex::operator!=(const CFastBinaryIndex& index) const
392 {
393  return (m_size != index.m_size) || (m_bits != index.m_bits);
394 }
395 
396 
397 // inline methods of embedded class Iterator
398 
400 : m_indexPtr(NULL), m_position(0)
401 {
402 }
403 
404 
405 inline CFastBinaryIndex::Iterator::Iterator(const CFastBinaryIndex* indexPtr, int position)
406 : m_indexPtr(indexPtr), m_position(position)
407 {
408 }
409 
410 
412 : m_indexPtr(iter.m_indexPtr), m_position(iter.m_position)
413 {
414 }
415 
416 
418 {
419  Q_ASSERT(m_indexPtr != NULL);
420 
421  return m_indexPtr->GetAt(m_position);
422 }
423 
424 
426 {
427  ++m_position;
428 
429  return *this;
430 }
431 
432 
434 {
435  Iterator retVal(*this);
436 
437  ++m_position;
438 
439  return retVal;
440 }
441 
442 
443 inline bool CFastBinaryIndex::Iterator::operator==(const Iterator& iter) const
444 {
445  return (m_indexPtr == iter.m_indexPtr) && (m_position == iter.m_position);
446 }
447 
448 
449 inline bool CFastBinaryIndex::Iterator::operator!=(const Iterator& iter) const
450 {
451  return (m_indexPtr != iter.m_indexPtr) || (m_position != iter.m_position);
452 }
453 
454 
456 {
457  m_indexPtr = iter.m_indexPtr;
458  m_position = iter.m_position;
459 
460  return *this;
461 }
462 
463 
464 } // namespace istd
465 
466 
467 #endif // !istd_CBinaryIndex_included
468 
469 
int GetProductVolume() const
Get total number of elements if this index is treated as size.
bool Increase(const CFastBinaryIndex &boundaries)
Increase this index inside the boundaries.
bool operator!=(const CFastBinaryIndex &index) const
Iterator End() const
Get end value of element access iterator.
Iterator & operator=(const Iterator &iter)
bool IsInside(const CFastBinaryIndex &boundaries) const
Check if index is inside boundaries.
bool operator==(const Iterator &iter) const
void SetAt(int index, int value)
Set element at specified index.
bool operator==(const CFastBinaryIndex &index) const
int operator[](int index) const
quint32 GetBits() const
Get bit coded value of this index.
void Clear()
Set all components to 0 (false).
bool IsDimensionsCountFixed() const
Check, if number dimensions is fixed.
bool operator!=(const Iterator &iter) const
bool IncreaseAt(int index)
Increase single component at specified position.
static CBitManip instance
Definition: CBitManip.h:26
bool IsValid() const
Check if this index is valid.
CFastBinaryIndex()
Default constructor without member initialization.
bool Decrease(const CFastBinaryIndex &boundaries)
Decrese this index inside the boundaries.
void SetAllTo(int value)
Set all components to specified value.
int GetDimensionsCount() const
Get number of dimensions of this index.
#define NULL
Definition: istd.h:64
bool IsZero() const
Check if this index point at zero element.
void Reset()
Reset this object.
bool SetDimensionsCount(int count)
Set number of dimensions of this index.
Iterator Begin() const
Get begin value of element access iterator.
int GetAt(int index) const
Get element stored at specified index.
bool DecreaseAt(int index)
Decrease single component at specified position.

© 2007-2017 Witold Gantzke and Kirill Lepskiy