bitVector.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: bitVector.h,v 1.47 2005/12/23 17:01:41 amoll Exp $
00005 //
00006 
00007 #ifndef BALL_DATATYPE_BITVECTOR_H
00008 #define BALL_DATATYPE_BITVECTOR_H
00009 
00010 #ifndef BALL_COMMON_H
00011 # include <BALL/common.h>
00012 #endif
00013 
00014 #ifndef BALL_CONCEPT_PERSISTENCEMANAGER_H
00015 # include <BALL/CONCEPT/persistenceManager.h>
00016 #endif
00017 
00018 #ifndef BALL_COMMON_EXCEPTION_H
00019 # include <BALL/COMMON/exception.h>
00020 #endif
00021 
00022 
00023 #include <string.h>
00024 
00025 #define BALL_BLOCK_BITS 8
00026 #define BALL_BLOCK_MASK (BALL_BLOCK_BITS - 1)
00027 #define BALL_BLOCK_SHIFT              3
00028 #define BALL_BLOCK_ALL_BITS_SET       0xFF
00029 #define BALL_BLOCK_ALL_BITS_CLEARED   0x00
00030 
00031 
00032 #define BALL_BLOCK_SIZE(bits) (Size)(((bits) + BALL_BLOCK_BITS - 1) >> BALL_BLOCK_SHIFT)
00033 
00034 
00035 namespace BALL 
00036 {
00037   class BitVector;
00038 
00044   class BALL_EXPORT Bit
00045   {
00046     public:
00047 
00051 
00054     class BALL_EXPORT IllegalOperation
00055       : public Exception::GeneralException
00056     {
00057       public:
00058       IllegalOperation(const char* file, int line);
00059     };
00060 
00062 
00063 
00067 
00068     BALL_CREATE(Bit)
00069 
00070     
00072     Bit();
00073 
00076     Bit(const Bit& bit);
00077 
00083     Bit(BitVector* bitvector, Index index = 0) 
00084       throw(Exception::NullPointer);
00085 
00092     Bit(const BitVector* const bitvector, Index index = 0) 
00093       throw(Exception::NullPointer, Exception::IndexUnderflow, Exception::IndexOverflow);
00094 
00097     virtual ~Bit();
00099 
00100 
00104     
00107     operator bool() const throw(Exception::NullPointer);
00108 
00110 
00113     
00117     Bit& operator = (const Bit& bit);
00118 
00124     Bit& operator = (const bool bit) 
00125       throw(Exception::NullPointer, IllegalOperation);
00126 
00129     virtual void clear();
00130 
00132 
00135 
00139     bool operator == (const Bit& bit) const;
00140 
00144     bool operator == (bool bit) const throw(Exception::NullPointer);
00145 
00149     bool operator != (const Bit& bit) const;
00150 
00154     bool operator != (bool bit) const throw(Exception::NullPointer);
00155 
00157 
00158     private:
00159 
00160     // --- ATTRIBUTES
00161 
00162     BitVector*  bitvector_;
00163     Index       index_;
00164     bool        bitvector_muteable_;
00165   };
00166 
00167 
00177   class BALL_EXPORT BitVector
00178   {
00179     public:
00180 
00181     BALL_CREATE(BitVector)
00182 
00183 
00184     
00187 
00189     typedef unsigned char BlockType;
00191     typedef std::vector<BlockType>  VectorType;
00193     static const Size BlockSize;
00194 
00196 
00199 
00202     BitVector();
00203 
00206     BitVector(Size size) throw(Exception::OutOfMemory);
00207 
00210     BitVector(const BitVector& bit_vector) 
00211       throw(Exception::OutOfMemory);
00212 
00216     BitVector(const char* bit_string)
00217      throw(Exception::OutOfMemory);
00218 
00221     virtual ~BitVector();
00222 
00226     void clear();
00227 
00229 
00232 
00234     void set(const BitVector& bit_vector)
00235       throw(Exception::OutOfMemory);
00236 
00241     void set(const char* bit_string)
00242       throw(Exception::OutOfMemory);
00243 
00245     BitVector& operator = (const BitVector& bit_vector)
00246       throw(Exception::OutOfMemory);
00247 
00251     BitVector& operator = (const char *bit_string)
00252       throw(Exception::OutOfMemory);
00253 
00255     void get(BitVector& bitvector) const
00256       throw(Exception::OutOfMemory);
00258 
00262 
00269     BitVector operator () (Index first,Index last) const
00270       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00271 
00276     void setSize(Size size, bool keep = true)
00277       throw(Exception::OutOfMemory);
00278 
00281     Size getSize() const;
00282 
00287     Size countValue(bool value) const;
00288 
00292     VectorType& getBitSet();
00293 
00297     const VectorType& getBitSet() const;
00298 
00304     Bit operator [] (Index index)
00305       throw(Exception::OutOfMemory);
00306 
00311     bool operator [] (Index index) const
00312       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00313 
00321     void setBit(Index index, bool value = true)
00322       throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00323 
00330     bool getBit(Index index)
00331       throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00332 
00339     bool getBit(Index index) const
00340       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00341 
00346     void toggleBit(Index index)
00347       throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00348 
00355     void fill(bool value = true, Index first = 0 , Index last = -1)
00356       throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00357 
00364     void toggle(Index first = 0, Index last = -1)
00365       throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00366 
00371     void setUnsignedChar(unsigned char bit_pattern);
00372 
00377     unsigned char getUnsignedChar() const;
00378 
00382     void setUnsignedShort(unsigned short bit_pattern);
00383 
00387     unsigned short getUnsignedShort() const;
00388 
00392     void setUnsignedInt(unsigned int bit_pattern);
00393 
00397     unsigned int getUnsignedInt() const;
00398 
00402     void setUnsignedLong(unsigned long bit_pattern);
00403 
00407     unsigned long getUnsignedLong() const;
00408 
00412     void bitwiseOr(const BitVector& bit_vector) 
00413       throw(Exception::OutOfMemory);
00414 
00418     void bitwiseXor(const BitVector& bit_vector) 
00419       throw(Exception::OutOfMemory);
00420 
00424     void bitwiseAnd(const BitVector& bit_vector) 
00425       throw(Exception::OutOfMemory);
00426 
00431     BitVector operator | (const BitVector& bit_vector) 
00432       throw(Exception::OutOfMemory);
00433 
00437     BitVector& operator |= (const BitVector& bit_vector) 
00438       throw(Exception::OutOfMemory);
00439 
00444     BitVector operator & (const BitVector& bit_vector) 
00445       throw(Exception::OutOfMemory);
00446 
00450     BitVector& operator &= (const BitVector& bit_vector) 
00451       throw(Exception::OutOfMemory);
00452 
00457     BitVector operator ^ (const BitVector& bit_vector) 
00458       throw(Exception::OutOfMemory);
00459 
00463     BitVector& operator ^= (const BitVector& bit_vector) 
00464       throw(Exception::OutOfMemory);
00465 
00470     BitVector operator ~ () 
00471       throw(Exception::OutOfMemory);
00472 
00474 
00477 
00479     bool operator == (const BitVector& bit_vector) const;
00480 
00482     bool operator != (const BitVector& bit_vector) const;
00483 
00489     bool isAnyBit(bool value, Index first = 0, Index last = -1) const
00490       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00491 
00497     bool isEveryBit(bool value, Index first = 0, Index last = -1) const
00498       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00499 
00501 
00504 
00507     bool isValid() const;
00508 
00510 
00513 
00517     BALL_EXPORT friend std::istream& operator >> (std::istream& s, BitVector& bit_vector)
00518       throw(Exception::OutOfMemory);
00519 
00523     BALL_EXPORT friend std::ostream& operator << (std::ostream& s, const BitVector& bit_vector);
00524 
00527     virtual void read(std::istream& s)
00528       throw(Exception::OutOfMemory);
00529 
00532     virtual void write(std::ostream& s) const;
00533 
00536     virtual void write(PersistenceManager& pm) const;
00537 
00540     virtual bool read(PersistenceManager& pm)
00541       throw(Exception::OutOfMemory);
00542 
00544 
00545     protected:
00546   
00547     void validateIndex_(Index& index)
00548       throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00549 
00550     void validateIndex_(Index& index) const
00551       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00552 
00553     void validateRange_(Index& first, Index& last) const
00554       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00555 
00556 
00557     private:
00558   
00559     Index block_(Index index)
00560       throw(Exception::IndexUnderflow, Exception::OutOfMemory);
00561 
00562     Index block_(Index index) const
00563       throw(Exception::IndexUnderflow, Exception::IndexOverflow);
00564 
00565     BlockType mask_(Index index) const;
00566 
00567     // --- ATTRIBUTES
00568 
00569     Size    size_;
00570     VectorType  bitset_;
00571   };
00572 
00573 # ifndef BALL_NO_INLINE_FUNCTIONS
00574 #   include <BALL/DATATYPE/bitVector.iC>
00575 # endif
00576 } //namespace BALL
00577 
00578 
00579 #endif // BALL_DATATYPE_BITVECTOR_H