2 * This module contains a specialized bitset implementation.
4 * Copyright: Copyright (C) 2005-2006 Digital Mars, www.digitalmars.com.
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, in both source and binary form, subject to the following
16 * o The origin of this software must not be misrepresented; you must not
17 * claim that you wrote the original software. If you use this software
18 * in a product, an acknowledgment in the product documentation would be
19 * appreciated but is not required.
20 * o Altered source versions must be plainly marked as such, and must not
21 * be misrepresented as being the original software.
22 * o This notice may not be removed or altered from any source
24 * Authors: Walter Bright, David Friedman, Sean Kelly
33 extern (C) void onOutOfMemoryError();
43 // use the unoptimized version
45 else version (D_InlineAsm_X86)
52 const int BITS_PER_WORD = 32;
53 const int BITS_SHIFT = 5;
54 const int BITS_MASK = 31;
57 size_t nwords = 0; // allocated words in data[] excluding sentinals
58 size_t nbits = 0; // number of bits in data[] excluding sentinals
73 assert(nwords * data[0].sizeof * 8 >= nbits);
77 void alloc(size_t nbits)
80 nwords = (nbits + (BITS_PER_WORD - 1)) >> BITS_SHIFT;
81 data = cast(uint*)calloc(nwords + 2, uint.sizeof);
93 //return (cast(bit *)(data + 1))[i];
94 return data[1 + (i >> BITS_SHIFT)] & (1 << (i & BITS_MASK));
104 //(cast(bit *)(data + 1))[i] = 1;
105 data[1 + (i >> BITS_SHIFT)] |= (1 << (i & BITS_MASK));
115 //(cast(bit *)(data + 1))[i] = 0;
116 data[1 + (i >> BITS_SHIFT)] &= ~(1 << (i & BITS_MASK));
119 uint testClear(size_t i)
123 return std.intrinsic.btr(data + 1, i);
140 //result = (cast(bit *)(data + 1))[i];
141 //(cast(bit *)(data + 1))[i] = 0;
143 uint* p = &data[1 + (i >> BITS_SHIFT)];
144 uint mask = (1 << (i & BITS_MASK));
153 memset(data + 1, 0, nwords * uint.sizeof);
159 assert(nwords == f.nwords);
163 memcpy(data + 1, f.data + 1, nwords * uint.sizeof);
182 assert(b.test(123) == 0);
183 assert(b.testClear(123) == 0);
185 assert(b.test(123) != 0);
186 assert(b.testClear(123) != 0);
187 assert(b.test(123) == 0);
191 assert(b.test(785) != 0);
192 assert(b.test(0) != 0);
194 assert(b.test(785) == 0);
195 assert(b.test(0) == 0);
201 assert(b.test(38) != 0);