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 00058 const unsigned BitVectorIndexShift = 00059 sizeof(unsigned)==2 ? 4 : // assume 2 bytes is 2^4 bits 00060 sizeof(unsigned)==4 ? 5 : // assume 4 bytes is 2^5 bits 00061 sizeof(unsigned)==8 ? 6 : // assume 8 bytes is 2^6 bits 00062 ~0u; // value which will cause later compile error 00063 00067 const unsigned BitVectorIndexMask = (1<<BitVectorIndexShift)-1; 00068 00069 // check that size of 'unsigned' is really as we specified... 00070 ASSERT_COMPILE( (1u<<BitVectorIndexMask) == (~0u>>1)+1 ); 00071 00072 00081 template <size_t S> 00082 struct BitVectorBits 00083 { 00087 unsigned Buffer[(S+BitVectorIndexMask)>>BitVectorIndexShift]; 00088 00092 inline operator unsigned*() 00093 { 00094 return Buffer; 00095 } 00096 }; 00097 00098 00104 template <> 00105 struct BitVectorBits<0> 00106 { 00110 inline operator unsigned*() 00111 { 00112 return 0; 00113 } 00114 }; 00115 00116 00124 class BitVectorPointer 00125 { 00126 public: 00132 template <size_t S> 00133 inline BitVectorPointer(BitVectorBits<S>& bits) 00134 : Bits(bits), Size(S) 00135 {} 00136 00143 inline BitVectorPointer(unsigned* bits, size_t size) 00144 : Bits(bits), Size(size) 00145 {} 00146 00150 inline BitVectorPointer() 00151 {} 00152 00156 void Reset(); 00157 00165 int Test(unsigned index); 00166 00172 void Set(unsigned index); 00173 00179 void Clear(unsigned index); 00180 00189 int TestSet(unsigned index, size_t count); 00190 00199 int TestClear(unsigned index, size_t count); 00200 00207 void Set(unsigned index, size_t count); 00208 00215 void Clear(unsigned index, size_t count); 00216 00225 int Find(size_t count, unsigned state); 00226 00227 public: 00228 unsigned* Bits; 00229 size_t Size; 00230 }; 00231 00232 00239 template <size_t S> 00240 class BitVector : public BitVectorPointer, public BitVectorBits<S> 00241 { 00242 public: 00246 inline BitVector() 00247 : BitVectorPointer(static_cast<BitVectorBits<S>&>(*this)) 00248 { 00249 Reset(); 00250 } 00251 }; 00252 00253 // End of group 00255 00256 #endif // BITVECTOR_H
1.6.1