BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
bitVector.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_DATATYPE_BITVECTOR_H
6 #define BALL_DATATYPE_BITVECTOR_H
7 
8 #ifndef BALL_COMMON_H
9 # include <BALL/common.h>
10 #endif
11 
12 #ifndef BALL_CONCEPT_PERSISTENCEMANAGER_H
14 #endif
15 
16 #ifndef BALL_COMMON_EXCEPTION_H
17 # include <BALL/COMMON/exception.h>
18 #endif
19 
20 
21 #include <cstring>
22 
23 #define BALL_BLOCK_BITS 8
24 #define BALL_BLOCK_MASK (BALL_BLOCK_BITS - 1)
25 #define BALL_BLOCK_SHIFT 3
26 #define BALL_BLOCK_ALL_BITS_SET 0xFF
27 #define BALL_BLOCK_ALL_BITS_CLEARED 0x00
28 
29 
30 #define BALL_BLOCK_SIZE(bits) (Size)(((bits) + BALL_BLOCK_BITS - 1) >> BALL_BLOCK_SHIFT)
31 
32 
33 namespace BALL
34 {
35  class BitVector;
36 
43  {
44  public:
45 
49 
54  {
55  public:
56  IllegalOperation(const char* file, int line);
57  };
58 
60 
61 
65 
67 
68 
70  Bit();
71 
74  Bit(const Bit& bit);
75 
81  Bit(BitVector* bitvector, Index index = 0);
82 
89  Bit(const BitVector* const bitvector, Index index = 0);
90 
93  virtual ~Bit();
95 
96 
100 
104  operator bool() const;
105 
107 
110 
114  Bit& operator = (const Bit& bit);
115 
122  Bit& operator = (const bool bit);
123 
126  virtual void clear();
127 
129 
132 
136  bool operator == (const Bit& bit) const;
137 
142  bool operator == (bool bit) const;
143 
147  bool operator != (const Bit& bit) const;
148 
153  bool operator != (bool bit) const;
154 
156 
157  private:
158 
159  // --- ATTRIBUTES
160 
161  BitVector* bitvector_;
162  Index index_;
163  bool bitvector_muteable_;
164  };
165 
166 
177  {
178  public:
179 
180  BALL_CREATE(BitVector)
181 
182 
183 
186 
188  typedef unsigned char BlockType;
190  typedef std::vector<BlockType> VectorType;
192  static const Size BlockSize;
193 
195 
198 
201  BitVector();
202 
206  BitVector(Size size);
207 
211  BitVector(const BitVector& bit_vector);
212 
217  BitVector(const char* bit_string);
218 
221  virtual ~BitVector();
222 
226  void clear();
227 
229 
232 
236  void set(const BitVector& bit_vector);
237 
243  void set(const char* bit_string);
244 
248  BitVector& operator = (const BitVector& bit_vector);
249 
254  BitVector& operator = (const char *bit_string);
255 
259  void get(BitVector& bitvector) const;
261 
265 
274  BitVector operator () (Index first, Index last) const;
275 
281  void setSize(Size size, bool keep = true);
282 
285  Size getSize() const;
286 
291  Size countValue(bool value) const;
292 
296  VectorType& getBitSet();
297 
301  const VectorType& getBitSet() const;
302 
309  Bit operator [] (Index index);
310 
317  bool operator [] (Index index) const;
318 
328  void setBit(Index index, bool value = true);
329 
338  bool getBit(Index index);
339 
348  bool getBit(Index index) const;
349 
356  void toggleBit(Index index);
357 
366  void fill(bool value = true, Index first = 0 , Index last = -1);
367 
376  void toggle(Index first = 0, Index last = -1);
377 
382  void setUnsignedChar(unsigned char bit_pattern);
383 
388  unsigned char getUnsignedChar() const;
389 
393  void setUnsignedShort(unsigned short bit_pattern);
394 
398  unsigned short getUnsignedShort() const;
399 
403  void setUnsignedInt(unsigned int bit_pattern);
404 
408  unsigned int getUnsignedInt() const;
409 
413  void setUnsignedLong(unsigned long bit_pattern);
414 
418  unsigned long getUnsignedLong() const;
419 
424  void bitwiseOr(const BitVector& bit_vector);
425 
430  void bitwiseXor(const BitVector& bit_vector);
431 
436  void bitwiseAnd(const BitVector& bit_vector);
437 
443  BitVector operator | (const BitVector& bit_vector);
444 
449  BitVector& operator |= (const BitVector& bit_vector);
450 
456  BitVector operator & (const BitVector& bit_vector);
457 
462  BitVector& operator &= (const BitVector& bit_vector);
463 
469  BitVector operator ^ (const BitVector& bit_vector);
470 
475  BitVector& operator ^= (const BitVector& bit_vector);
476 
482  BitVector operator ~ ();
483 
485 
488 
490  bool operator == (const BitVector& bit_vector) const;
491 
493  bool operator != (const BitVector& bit_vector) const;
494 
502  bool isAnyBit(bool value, Index first = 0, Index last = -1) const;
503 
511  bool isEveryBit(bool value, Index first = 0, Index last = -1) const;
512 
514 
517 
520  bool isValid() const;
521 
523 
526 
531  BALL_EXPORT friend std::istream& operator >> (std::istream& s, BitVector& bit_vector);
532 
536  BALL_EXPORT friend std::ostream& operator << (std::ostream& s, const BitVector& bit_vector);
537 
541  virtual void read(std::istream& s);
542 
545  virtual void write(std::ostream& s) const;
546 
549  virtual void write(PersistenceManager& pm) const;
550 
554  virtual bool read(PersistenceManager& pm);
555 
557 
558  protected:
559 
560  // @exception Exception::IndexUnderflow
561  // @exception Exception::OutOfMemory
562  void validateIndex_(Index& index);
563 
564  // @exception Exception::IndexUnderflow
565  // @exception Exception::OutOfMemory
566  void validateIndex_(Index& index) const;
567 
568  // @exception Exception::IndexUnderflow
569  // @exception Exception::IndexOverflow
570  void validateRange_(Index& first, Index& last) const;
571 
572 
573  private:
574 
575  // @exception Exception::IndexUnderflow
576  // @exception Exception::OutOfMemory
577  Index block_(Index index);
578 
579  // @exception Exception::IndexUnderflow
580  // @exception Exception::OutOfMemory
581  Index block_(Index index) const;
582 
583  BlockType mask_(Index index) const;
584 
585  // --- ATTRIBUTES
586 
587  Size size_;
588  VectorType bitset_;
589  };
590 
591 # ifndef BALL_NO_INLINE_FUNCTIONS
592 # include <BALL/DATATYPE/bitVector.iC>
593 # endif
594 } //namespace BALL
595 
596 
597 #endif // BALL_DATATYPE_BITVECTOR_H
#define BALL_CREATE(name)
Definition: create.h:62
unsigned char BlockType
Definition: bitVector.h:188
std::vector< BlockType > VectorType
Definition: bitVector.h:190
#define BALL_EXPORT
Definition: COMMON/global.h:50