BALL
1.4.1
|
00001 // -*- Mode: C++; tab-width: 2; -*- 00002 // vi: set ts=2: 00003 // 00004 00005 #ifndef BALL_DATATYPE_BITVECTOR_H 00006 #define BALL_DATATYPE_BITVECTOR_H 00007 00008 #ifndef BALL_COMMON_H 00009 # include <BALL/common.h> 00010 #endif 00011 00012 #ifndef BALL_CONCEPT_PERSISTENCEMANAGER_H 00013 # include <BALL/CONCEPT/persistenceManager.h> 00014 #endif 00015 00016 #ifndef BALL_COMMON_EXCEPTION_H 00017 # include <BALL/COMMON/exception.h> 00018 #endif 00019 00020 00021 #include <string.h> 00022 00023 #define BALL_BLOCK_BITS 8 00024 #define BALL_BLOCK_MASK (BALL_BLOCK_BITS - 1) 00025 #define BALL_BLOCK_SHIFT 3 00026 #define BALL_BLOCK_ALL_BITS_SET 0xFF 00027 #define BALL_BLOCK_ALL_BITS_CLEARED 0x00 00028 00029 00030 #define BALL_BLOCK_SIZE(bits) (Size)(((bits) + BALL_BLOCK_BITS - 1) >> BALL_BLOCK_SHIFT) 00031 00032 00033 namespace BALL 00034 { 00035 class BitVector; 00036 00042 class BALL_EXPORT Bit 00043 { 00044 public: 00045 00049 00052 class BALL_EXPORT IllegalOperation 00053 : public Exception::GeneralException 00054 { 00055 public: 00056 IllegalOperation(const char* file, int line); 00057 }; 00058 00060 00061 00065 00066 BALL_CREATE(Bit) 00067 00068 00070 Bit(); 00071 00074 Bit(const Bit& bit); 00075 00081 Bit(BitVector* bitvector, Index index = 0); 00082 00089 Bit(const BitVector* const bitvector, Index index = 0); 00090 00093 virtual ~Bit(); 00095 00096 00100 00104 operator bool() const; 00105 00107 00110 00114 Bit& operator = (const Bit& bit); 00115 00122 Bit& operator = (const bool bit); 00123 00126 virtual void clear(); 00127 00129 00132 00136 bool operator == (const Bit& bit) const; 00137 00142 bool operator == (bool bit) const; 00143 00147 bool operator != (const Bit& bit) const; 00148 00153 bool operator != (bool bit) const; 00154 00156 00157 private: 00158 00159 // --- ATTRIBUTES 00160 00161 BitVector* bitvector_; 00162 Index index_; 00163 bool bitvector_muteable_; 00164 }; 00165 00166 00176 class BALL_EXPORT BitVector 00177 { 00178 public: 00179 00180 BALL_CREATE(BitVector) 00181 00182 00183 00186 00188 typedef unsigned char BlockType; 00190 typedef std::vector<BlockType> VectorType; 00192 static const Size BlockSize; 00193 00195 00198 00201 BitVector(); 00202 00206 BitVector(Size size); 00207 00211 BitVector(const BitVector& bit_vector); 00212 00217 BitVector(const char* bit_string); 00218 00221 virtual ~BitVector(); 00222 00226 void clear(); 00227 00229 00232 00236 void set(const BitVector& bit_vector); 00237 00243 void set(const char* bit_string); 00244 00248 BitVector& operator = (const BitVector& bit_vector); 00249 00254 BitVector& operator = (const char *bit_string); 00255 00259 void get(BitVector& bitvector) const; 00261 00265 00274 BitVector operator () (Index first, Index last) const; 00275 00281 void setSize(Size size, bool keep = true); 00282 00285 Size getSize() const; 00286 00291 Size countValue(bool value) const; 00292 00296 VectorType& getBitSet(); 00297 00301 const VectorType& getBitSet() const; 00302 00309 Bit operator [] (Index index); 00310 00317 bool operator [] (Index index) const; 00318 00328 void setBit(Index index, bool value = true); 00329 00338 bool getBit(Index index); 00339 00348 bool getBit(Index index) const; 00349 00356 void toggleBit(Index index); 00357 00366 void fill(bool value = true, Index first = 0 , Index last = -1); 00367 00376 void toggle(Index first = 0, Index last = -1); 00377 00382 void setUnsignedChar(unsigned char bit_pattern); 00383 00388 unsigned char getUnsignedChar() const; 00389 00393 void setUnsignedShort(unsigned short bit_pattern); 00394 00398 unsigned short getUnsignedShort() const; 00399 00403 void setUnsignedInt(unsigned int bit_pattern); 00404 00408 unsigned int getUnsignedInt() const; 00409 00413 void setUnsignedLong(unsigned long bit_pattern); 00414 00418 unsigned long getUnsignedLong() const; 00419 00424 void bitwiseOr(const BitVector& bit_vector); 00425 00430 void bitwiseXor(const BitVector& bit_vector); 00431 00436 void bitwiseAnd(const BitVector& bit_vector); 00437 00443 BitVector operator | (const BitVector& bit_vector); 00444 00449 BitVector& operator |= (const BitVector& bit_vector); 00450 00456 BitVector operator & (const BitVector& bit_vector); 00457 00462 BitVector& operator &= (const BitVector& bit_vector); 00463 00469 BitVector operator ^ (const BitVector& bit_vector); 00470 00475 BitVector& operator ^= (const BitVector& bit_vector); 00476 00482 BitVector operator ~ (); 00483 00485 00488 00490 bool operator == (const BitVector& bit_vector) const; 00491 00493 bool operator != (const BitVector& bit_vector) const; 00494 00502 bool isAnyBit(bool value, Index first = 0, Index last = -1) const; 00503 00511 bool isEveryBit(bool value, Index first = 0, Index last = -1) const; 00512 00514 00517 00520 bool isValid() const; 00521 00523 00526 00531 BALL_EXPORT friend std::istream& operator >> (std::istream& s, BitVector& bit_vector); 00532 00536 BALL_EXPORT friend std::ostream& operator << (std::ostream& s, const BitVector& bit_vector); 00537 00541 virtual void read(std::istream& s); 00542 00545 virtual void write(std::ostream& s) const; 00546 00549 virtual void write(PersistenceManager& pm) const; 00550 00554 virtual bool read(PersistenceManager& pm); 00555 00557 00558 protected: 00559 00560 // @exception Exception::IndexUnderflow 00561 // @exception Exception::OutOfMemory 00562 void validateIndex_(Index& index); 00563 00564 // @exception Exception::IndexUnderflow 00565 // @exception Exception::OutOfMemory 00566 void validateIndex_(Index& index) const; 00567 00568 // @exception Exception::IndexUnderflow 00569 // @exception Exception::IndexOverflow 00570 void validateRange_(Index& first, Index& last) const; 00571 00572 00573 private: 00574 00575 // @exception Exception::IndexUnderflow 00576 // @exception Exception::OutOfMemory 00577 Index block_(Index index); 00578 00579 // @exception Exception::IndexUnderflow 00580 // @exception Exception::OutOfMemory 00581 Index block_(Index index) const; 00582 00583 BlockType mask_(Index index) const; 00584 00585 // --- ATTRIBUTES 00586 00587 Size size_; 00588 VectorType bitset_; 00589 }; 00590 00591 # ifndef BALL_NO_INLINE_FUNCTIONS 00592 # include <BALL/DATATYPE/bitVector.iC> 00593 # endif 00594 } //namespace BALL 00595 00596 00597 #endif // BALL_DATATYPE_BITVECTOR_H