BALL  1.5.0
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 
181 
182 
183 
186 
188  typedef unsigned char BlockType;
190  typedef std::vector<BlockType> VectorType;
192  static const Size BlockSize;
194  static const Size CharBits;
195 
197 
200 
204 
209 
213  BitVector(const BitVector& bit_vector);
214 
219  BitVector(const char* bit_string);
220 
223  virtual ~BitVector();
224 
228  void clear();
229 
231 
234 
238  void set(const BitVector& bit_vector);
239 
245  void set(const char* bit_string);
246 
250  BitVector& operator = (const BitVector& bit_vector);
251 
256  BitVector& operator = (const char *bit_string);
257 
261  void get(BitVector& bitvector) const;
263 
267 
276  BitVector operator () (Index first, Index last) const;
277 
283  void setSize(Size size, bool keep = true);
284 
287  Size getSize() const;
288 
293  Size countValue(bool value) const;
294 
298  VectorType& getBitSet();
299 
303  const VectorType& getBitSet() const;
304 
311  Bit operator [] (Index index);
312 
319  bool operator [] (Index index) const;
320 
330  void setBit(Index index, bool value = true);
331 
340  bool getBit(Index index);
341 
350  bool getBit(Index index) const;
351 
358  void toggleBit(Index index);
359 
368  void fill(bool value = true, Index first = 0 , Index last = -1);
369 
378  void toggle(Index first = 0, Index last = -1);
379 
384  void setUnsignedChar(unsigned char bit_pattern);
385 
390  unsigned char getUnsignedChar() const;
391 
395  void setUnsignedShort(unsigned short bit_pattern);
396 
400  unsigned short getUnsignedShort() const;
401 
405  void setUnsignedInt(unsigned int bit_pattern);
406 
410  unsigned int getUnsignedInt() const;
411 
415  void setUnsignedLong(unsigned long bit_pattern);
416 
420  unsigned long getUnsignedLong() const;
421 
426  void bitwiseOr(const BitVector& bit_vector);
427 
432  void bitwiseXor(const BitVector& bit_vector);
433 
438  void bitwiseAnd(const BitVector& bit_vector);
439 
445  BitVector operator | (const BitVector& bit_vector);
446 
451  BitVector& operator |= (const BitVector& bit_vector);
452 
458  BitVector operator & (const BitVector& bit_vector);
459 
464  BitVector& operator &= (const BitVector& bit_vector);
465 
471  BitVector operator ^ (const BitVector& bit_vector);
472 
477  BitVector& operator ^= (const BitVector& bit_vector);
478 
484  BitVector operator ~ ();
485 
487 
490 
492  bool operator == (const BitVector& bit_vector) const;
493 
495  bool operator != (const BitVector& bit_vector) const;
496 
504  bool isAnyBit(bool value, Index first = 0, Index last = -1) const;
505 
513  bool isEveryBit(bool value, Index first = 0, Index last = -1) const;
514 
516 
519 
522  bool isValid() const;
523 
525 
528 
533  BALL_EXPORT friend std::istream& operator >> (std::istream& s, BitVector& bit_vector);
534 
538  BALL_EXPORT friend std::ostream& operator << (std::ostream& s, const BitVector& bit_vector);
539 
543  virtual void read(std::istream& s);
544 
547  virtual void write(std::ostream& s) const;
548 
551  virtual void write(PersistenceManager& pm) const;
552 
556  virtual bool read(PersistenceManager& pm);
557 
559 
560  protected:
561 
562  // @exception Exception::IndexUnderflow
563  // @exception Exception::OutOfMemory
564  void validateIndex_(Index& index);
565 
566  // @exception Exception::IndexUnderflow
567  // @exception Exception::OutOfMemory
568  void validateIndex_(Index& index) const;
569 
570  // @exception Exception::IndexUnderflow
571  // @exception Exception::IndexOverflow
572  void validateRange_(Index& first, Index& last) const;
573 
574 
575  private:
576 
577  // @exception Exception::IndexUnderflow
578  // @exception Exception::OutOfMemory
579  Index block_(Index index);
580 
581  // @exception Exception::IndexUnderflow
582  // @exception Exception::OutOfMemory
583  Index block_(Index index) const;
584 
585  BlockType mask_(Index index) const;
586 
587  // --- ATTRIBUTES
588 
589  Size size_;
590  VectorType bitset_;
591  };
592 
593 # ifndef BALL_NO_INLINE_FUNCTIONS
594 # include <BALL/DATATYPE/bitVector.iC>
595 # endif
596 } //namespace BALL
597 
598 
599 #endif // BALL_DATATYPE_BITVECTOR_H
BALL_EXPORT
#define BALL_EXPORT
Definition: COMMON/global.h:50
BALL::Bit::IllegalOperation
Definition: bitVector.h:54
BALL::BitVector::VectorType
std::vector< BlockType > VectorType
Definition: bitVector.h:190
BALL::BitVector::BlockType
unsigned char BlockType
Definition: bitVector.h:188
BALL::PersistenceManager
Definition: persistenceManager.h:73
BALL
Definition: constants.h:13
BALL_INDEX_TYPE
BALL_SIZE_TYPE
BALL::Bit::IllegalOperation::IllegalOperation
IllegalOperation(const char *file, int line)
BALL::Bit
Definition: bitVector.h:43
exception.h
common.h
BALL::Exception::GeneralException
Definition: COMMON/exception.h:61
BALL_CREATE
#define BALL_CREATE(name)
Definition: create.h:62
persistenceManager.h
BALL::BitVector
Definition: bitVector.h:177