00001
00002
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 throw(Exception::NullPointer);
00083
00090 Bit(const BitVector* const bitvector, Index index = 0)
00091 throw(Exception::NullPointer, Exception::IndexUnderflow, Exception::IndexOverflow);
00092
00095 virtual ~Bit();
00097
00098
00102
00105 operator bool() const throw(Exception::NullPointer);
00106
00108
00111
00115 Bit& operator = (const Bit& bit);
00116
00122 Bit& operator = (const bool bit)
00123 throw(Exception::NullPointer, IllegalOperation);
00124
00127 virtual void clear();
00128
00130
00133
00137 bool operator == (const Bit& bit) const;
00138
00142 bool operator == (bool bit) const throw(Exception::NullPointer);
00143
00147 bool operator != (const Bit& bit) const;
00148
00152 bool operator != (bool bit) const throw(Exception::NullPointer);
00153
00155
00156 private:
00157
00158
00159
00160 BitVector* bitvector_;
00161 Index index_;
00162 bool bitvector_muteable_;
00163 };
00164
00165
00175 class BALL_EXPORT BitVector
00176 {
00177 public:
00178
00179 BALL_CREATE(BitVector)
00180
00181
00182
00185
00187 typedef unsigned char BlockType;
00189 typedef std::vector<BlockType> VectorType;
00191 static const Size BlockSize;
00192
00194
00197
00200 BitVector();
00201
00204 BitVector(Size size) throw(Exception::OutOfMemory);
00205
00208 BitVector(const BitVector& bit_vector)
00209 throw(Exception::OutOfMemory);
00210
00214 BitVector(const char* bit_string)
00215 throw(Exception::OutOfMemory);
00216
00219 virtual ~BitVector();
00220
00224 void clear();
00225
00227
00230
00232 void set(const BitVector& bit_vector)
00233 throw(Exception::OutOfMemory);
00234
00239 void set(const char* bit_string)
00240 throw(Exception::OutOfMemory);
00241
00243 BitVector& operator = (const BitVector& bit_vector)
00244 throw(Exception::OutOfMemory);
00245
00249 BitVector& operator = (const char *bit_string)
00250 throw(Exception::OutOfMemory);
00251
00253 void get(BitVector& bitvector) const
00254 throw(Exception::OutOfMemory);
00256
00260
00267 BitVector operator () (Index first,Index last) const
00268 throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00269
00274 void setSize(Size size, bool keep = true)
00275 throw(Exception::OutOfMemory);
00276
00279 Size getSize() const;
00280
00285 Size countValue(bool value) const;
00286
00290 VectorType& getBitSet();
00291
00295 const VectorType& getBitSet() const;
00296
00302 Bit operator [] (Index index)
00303 throw(Exception::OutOfMemory);
00304
00309 bool operator [] (Index index) const
00310 throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00311
00319 void setBit(Index index, bool value = true)
00320 throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00321
00328 bool getBit(Index index)
00329 throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00330
00337 bool getBit(Index index) const
00338 throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00339
00344 void toggleBit(Index index)
00345 throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00346
00353 void fill(bool value = true, Index first = 0 , Index last = -1)
00354 throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00355
00362 void toggle(Index first = 0, Index last = -1)
00363 throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00364
00369 void setUnsignedChar(unsigned char bit_pattern);
00370
00375 unsigned char getUnsignedChar() const;
00376
00380 void setUnsignedShort(unsigned short bit_pattern);
00381
00385 unsigned short getUnsignedShort() const;
00386
00390 void setUnsignedInt(unsigned int bit_pattern);
00391
00395 unsigned int getUnsignedInt() const;
00396
00400 void setUnsignedLong(unsigned long bit_pattern);
00401
00405 unsigned long getUnsignedLong() const;
00406
00410 void bitwiseOr(const BitVector& bit_vector)
00411 throw(Exception::OutOfMemory);
00412
00416 void bitwiseXor(const BitVector& bit_vector)
00417 throw(Exception::OutOfMemory);
00418
00422 void bitwiseAnd(const BitVector& bit_vector)
00423 throw(Exception::OutOfMemory);
00424
00429 BitVector operator | (const BitVector& bit_vector)
00430 throw(Exception::OutOfMemory);
00431
00435 BitVector& operator |= (const BitVector& bit_vector)
00436 throw(Exception::OutOfMemory);
00437
00442 BitVector operator & (const BitVector& bit_vector)
00443 throw(Exception::OutOfMemory);
00444
00448 BitVector& operator &= (const BitVector& bit_vector)
00449 throw(Exception::OutOfMemory);
00450
00455 BitVector operator ^ (const BitVector& bit_vector)
00456 throw(Exception::OutOfMemory);
00457
00461 BitVector& operator ^= (const BitVector& bit_vector)
00462 throw(Exception::OutOfMemory);
00463
00468 BitVector operator ~ ()
00469 throw(Exception::OutOfMemory);
00470
00472
00475
00477 bool operator == (const BitVector& bit_vector) const;
00478
00480 bool operator != (const BitVector& bit_vector) const;
00481
00487 bool isAnyBit(bool value, Index first = 0, Index last = -1) const
00488 throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00489
00495 bool isEveryBit(bool value, Index first = 0, Index last = -1) const
00496 throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00497
00499
00502
00505 bool isValid() const;
00506
00508
00511
00515 BALL_EXPORT friend std::istream& operator >> (std::istream& s, BitVector& bit_vector)
00516 throw(Exception::OutOfMemory);
00517
00521 BALL_EXPORT friend std::ostream& operator << (std::ostream& s, const BitVector& bit_vector);
00522
00525 virtual void read(std::istream& s)
00526 throw(Exception::OutOfMemory);
00527
00530 virtual void write(std::ostream& s) const;
00531
00534 virtual void write(PersistenceManager& pm) const;
00535
00538 virtual bool read(PersistenceManager& pm)
00539 throw(Exception::OutOfMemory);
00540
00542
00543 protected:
00544
00545 void validateIndex_(Index& index)
00546 throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00547
00548 void validateIndex_(Index& index) const
00549 throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00550
00551 void validateRange_(Index& first, Index& last) const
00552 throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00553
00554
00555 private:
00556
00557 Index block_(Index index)
00558 throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00559
00560 Index block_(Index index) const
00561 throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00562
00563 BlockType mask_(Index index) const;
00564
00565
00566
00567 Size size_;
00568 VectorType bitset_;
00569 };
00570
00571 # ifndef BALL_NO_INLINE_FUNCTIONS
00572 # include <BALL/DATATYPE/bitVector.iC>
00573 # endif
00574 }
00575
00576
00577 #endif // BALL_DATATYPE_BITVECTOR_H