00001 /* 00002 This program is distributed under the terms of the 'MIT license'. The text 00003 of this licence follows... 00004 00005 Copyright (c) 2007 J.D.Medhurst (a.k.a. Tixy) 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a copy 00008 of this software and associated documentation files (the "Software"), to deal 00009 in the Software without restriction, including without limitation the rights 00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00011 copies of the Software, and to permit persons to whom the Software is 00012 furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00020 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00022 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00023 THE SOFTWARE. 00024 */ 00025 00032 #ifndef __BITVECTOR_H__ 00033 #define __BITVECTOR_H__ 00034 00057 const unsigned BitVectorIndexShift = LOG2(INT_BITS); 00058 00062 const unsigned BitVectorIndexMask = (1<<BitVectorIndexShift)-1; 00063 00064 00073 template <size_t S> 00074 struct BitVectorBits 00075 { 00079 unsigned Buffer[(S+BitVectorIndexMask)>>BitVectorIndexShift]; 00080 00084 inline operator unsigned*() 00085 { 00086 return Buffer; 00087 } 00088 }; 00089 00090 00096 template <> 00097 struct BitVectorBits<0> 00098 { 00102 inline operator unsigned*() 00103 { 00104 return 0; 00105 } 00106 }; 00107 00108 00116 class BitVectorPointer 00117 { 00118 public: 00124 template <size_t S> 00125 inline BitVectorPointer(BitVectorBits<S>& bits) 00126 : Bits(bits), Size(S) 00127 {} 00128 00135 inline BitVectorPointer(unsigned* bits, size_t size) 00136 : Bits(bits), Size(size) 00137 {} 00138 00142 inline BitVectorPointer() 00143 {} 00144 00148 void Reset(); 00149 00157 int Test(unsigned index); 00158 00164 void Set(unsigned index); 00165 00171 void Clear(unsigned index); 00172 00181 int TestSet(unsigned index, size_t count); 00182 00191 int TestClear(unsigned index, size_t count); 00192 00199 void Set(unsigned index, size_t count); 00200 00207 void Clear(unsigned index, size_t count); 00208 00217 int Find(size_t count, unsigned state); 00218 00219 public: 00220 unsigned* Bits; 00221 size_t Size; 00222 }; 00223 00224 00231 template <size_t S> 00232 class BitVector : public BitVectorPointer, public BitVectorBits<S> 00233 { 00234 public: 00238 inline BitVector() 00239 : BitVectorPointer(static_cast<BitVectorBits<S>&>(*this)) 00240 { 00241 Reset(); 00242 } 00243 }; 00244 00245 // End of group 00247 00248 #endif // __BITVECTOR_H__
1.4.4