BitSet.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2009  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_BitSet_hxx
00024 #define __ccxx_BitSet_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/OutOfBoundsException.h++>
00028 #include <commonc++/String.h++>
00029 
00030 #include <iostream>
00031 
00032 namespace ccxx {
00033 
00043 class COMMONCPP_API BitSet
00044 {
00045   public:
00046 
00051   class COMMONCPP_API Bit
00052   {
00053     friend class BitSet;
00054 
00055     public:
00056 
00058     Bit(const Bit &other) throw();
00059 
00061     Bit& operator=(const Bit& other) throw();
00062 
00064     Bit& operator=(bool val) throw();
00065 
00067     ~Bit() throw();
00068 
00070     operator bool() const throw();
00071 
00073     Bit& flip() throw();
00074 
00075     private:
00076 
00077     Bit(BitSet& set, uint_t pos) throw();
00078 
00079     BitSet& _set;
00080     uint_t _pos;
00081   };
00082 
00087   BitSet(uint_t size);
00088 
00090   BitSet(const BitSet& other);
00091 
00097   BitSet(const String& bits);
00098 
00100   virtual ~BitSet() throw();
00101 
00107   BitSet& set(uint_t pos, bool value = true) throw();
00108 
00115   BitSet& setRange(uint_t startPos, uint_t endPos, bool value = true)
00116     throw();
00117 
00122   BitSet& setAll(bool value = true) throw();
00123 
00128   inline BitSet& clear(uint_t pos) throw()
00129   { return(set(pos, false)); }
00130 
00136   inline BitSet& clearRange(uint_t startPos, uint_t endPos) throw()
00137   { return(setRange(startPos, endPos, false)); }
00138 
00140   inline BitSet& clearAll() throw()
00141   { return(setAll(false)); }
00142 
00144   bool isSet(uint_t pos) const throw();
00145 
00147   inline bool get(uint_t pos) const throw()
00148   { return(isSet(pos)); }
00149 
00151   inline bool isClear(uint_t pos) const throw()
00152   { return(! isSet(pos)); }
00153 
00155   bool isAnySet() const throw();
00156 
00158   bool isAllSet() const throw();
00159 
00161   inline bool isAllClear() const throw()
00162   { return(! isAnySet()); }
00163 
00171   inline int nextSetBit(uint_t startPos) const throw()
00172   { return(_nextBit(startPos, true)); }
00173 
00179   inline int firstSetBit() const throw()
00180   { return(_nextBit(0, true)); }
00181 
00189   inline int nextClearBit(uint_t startPos) const throw()
00190   { return(_nextBit(startPos, false)); }
00191 
00197   inline int firstClearBit() const throw()
00198   { return(_nextBit(0, false)); }
00199 
00203   inline bool operator!() const throw()
00204   { return(! isAnySet()); }
00205 
00207   BitSet& flip() throw();
00208 
00213   BitSet& flip(uint_t pos) throw(OutOfBoundsException);
00214 
00219   void swap(BitSet &other) throw(OutOfBoundsException);
00220 
00227   Bit operator[](uint_t pos) throw(OutOfBoundsException);
00228 
00235   inline bool operator[](uint_t pos) const throw()
00236   { return(isSet(pos)); }
00237 
00239   inline uint_t getSize() const throw()
00240   { return(_size); }
00241 
00245   inline size_t getWordCount() const throw()
00246   { return(_length); }
00247 
00254   inline uint32_t getWord(uint_t index) const throw()
00255   { return(index >= _length ? 0 : _words[index]); }
00256 
00266   inline uint32_t getWordDiff(uint_t index, uint32_t other) const throw()
00267   { return(getWord(index) ^ other); }
00268 
00275   void setWord(uint_t index, uint32_t word) throw();
00276 
00278   String toString() const;
00279 
00281   void write(std::ostream &stream) const;
00282 
00284   bool operator==(const BitSet& other) const throw();
00285 
00287   inline bool operator!=(const BitSet& other) const throw()
00288   { return(! operator==(other)); }
00289 
00291   BitSet& operator=(const BitSet& other);
00292 
00297   BitSet& operator|=(const BitSet& other) throw(OutOfBoundsException);
00298 
00303   BitSet& operator&=(const BitSet& other) throw(OutOfBoundsException);
00304 
00309   BitSet& operator^=(const BitSet& other) throw(OutOfBoundsException);
00310 
00315   BitSet& operator>>=(size_t n) throw();
00316 
00321   BitSet& operator<<=(size_t n) throw();
00322 
00328   inline BitSet operator>>(size_t n) const
00329   { return(BitSet(*this) >>= n); }
00330 
00336   inline BitSet operator<<(size_t n) const
00337   { return(BitSet(*this) <<= n); }
00338 
00343   inline BitSet operator~() const throw()
00344   { return(BitSet(*this).flip()); }
00345 
00346   private:
00347 
00348   void _init(uint_t size);
00349   int _nextBit(uint_t startPos, bool set) const throw();
00350 
00351   uint_t _size;
00352   size_t _length;
00353   uint32_t *_words;
00354   uint32_t _tailMask;
00355 };
00356 
00357 inline std::ostream& operator<<(std::ostream &stream, const BitSet &bs)
00358 {
00359   bs.write(stream);
00360   return(stream);
00361 }
00362 
00363 }; // namespace ccxx
00364 
00365 #endif // __ccxx_BitSet_hxx
00366 
00367 /* end of header file */

Generated on Sat Apr 17 23:03:05 2010 for libcommonc++ by  doxygen 1.5.9