| 1 |
/******************************************************************************* |
|---|
| 2 |
|
|---|
| 3 |
copyright: Copyright (c) 2009 Kris Bell. All rights reserved |
|---|
| 4 |
|
|---|
| 5 |
license: BSD style: $(LICENSE) |
|---|
| 6 |
|
|---|
| 7 |
version: Sept 2009: Initial release |
|---|
| 8 |
|
|---|
| 9 |
since: 0.99.9 |
|---|
| 10 |
|
|---|
| 11 |
author: Kris |
|---|
| 12 |
|
|---|
| 13 |
*******************************************************************************/ |
|---|
| 14 |
|
|---|
| 15 |
module tango.util.container.more.BitSet; |
|---|
| 16 |
|
|---|
| 17 |
private import std.intrinsic; |
|---|
| 18 |
|
|---|
| 19 |
/****************************************************************************** |
|---|
| 20 |
|
|---|
| 21 |
A fixed or dynamic set of bits. Note that this does no memory |
|---|
| 22 |
allocation of its own when Size != 0, and does heap allocation |
|---|
| 23 |
when Size is zero. Thus you can have a fixed-size low-overhead |
|---|
| 24 |
'instance, or a heap oriented instance. The latter has support |
|---|
| 25 |
for resizing, whereas the former does not. |
|---|
| 26 |
|
|---|
| 27 |
Note that leveraging intrinsics is slower when using dmd ... |
|---|
| 28 |
|
|---|
| 29 |
******************************************************************************/ |
|---|
| 30 |
|
|---|
| 31 |
struct BitSet (int Count=0) |
|---|
| 32 |
{ |
|---|
| 33 |
public alias and opAnd; |
|---|
| 34 |
public alias or opOrAssign; |
|---|
| 35 |
public alias xor opXorAssign; |
|---|
| 36 |
private const width = size_t.sizeof * 8; |
|---|
| 37 |
|
|---|
| 38 |
static if (Count == 0) |
|---|
| 39 |
private size_t[] bits; |
|---|
| 40 |
else |
|---|
| 41 |
private size_t [(Count+width-1)/width] bits; |
|---|
| 42 |
|
|---|
| 43 |
/********************************************************************** |
|---|
| 44 |
|
|---|
| 45 |
Set the indexed bit, resizing as necessary for heap-based |
|---|
| 46 |
instances (IndexOutOfBounds for statically-sized instances) |
|---|
| 47 |
|
|---|
| 48 |
**********************************************************************/ |
|---|
| 49 |
|
|---|
| 50 |
void add (size_t i) |
|---|
| 51 |
{ |
|---|
| 52 |
static if (Count == 0) |
|---|
| 53 |
size (i); |
|---|
| 54 |
or (i); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
/********************************************************************** |
|---|
| 58 |
|
|---|
| 59 |
Test whether the indexed bit is enabled |
|---|
| 60 |
|
|---|
| 61 |
**********************************************************************/ |
|---|
| 62 |
|
|---|
| 63 |
bool has (size_t i) |
|---|
| 64 |
{ |
|---|
| 65 |
auto idx = i / width; |
|---|
| 66 |
return idx < bits.length && (bits[idx] & (1 << (i % width))) != 0; |
|---|
| 67 |
//return idx < bits.length && bt(&bits[idx], i % width) != 0; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
/********************************************************************** |
|---|
| 71 |
|
|---|
| 72 |
Like get() but a little faster for when you know the range |
|---|
| 73 |
is valid |
|---|
| 74 |
|
|---|
| 75 |
**********************************************************************/ |
|---|
| 76 |
|
|---|
| 77 |
bool and (size_t i) |
|---|
| 78 |
{ |
|---|
| 79 |
return (bits[i / width] & (1 << (i % width))) != 0; |
|---|
| 80 |
//return bt(&bits[i / width], i % width) != 0; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
/********************************************************************** |
|---|
| 84 |
|
|---|
| 85 |
Turn on an indexed bit |
|---|
| 86 |
|
|---|
| 87 |
**********************************************************************/ |
|---|
| 88 |
|
|---|
| 89 |
void or (size_t i) |
|---|
| 90 |
{ |
|---|
| 91 |
bits[i / width] |= (1 << (i % width)); |
|---|
| 92 |
//bts (&bits[i / width], i % width); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
/********************************************************************** |
|---|
| 96 |
|
|---|
| 97 |
Invert an indexed bit |
|---|
| 98 |
|
|---|
| 99 |
**********************************************************************/ |
|---|
| 100 |
|
|---|
| 101 |
void xor (size_t i) |
|---|
| 102 |
{ |
|---|
| 103 |
bits[i / width] ^= (1 << (i % width)); |
|---|
| 104 |
//btc (&bits[i / width], i % width); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
/********************************************************************** |
|---|
| 108 |
|
|---|
| 109 |
Clear an indexed bit |
|---|
| 110 |
|
|---|
| 111 |
**********************************************************************/ |
|---|
| 112 |
|
|---|
| 113 |
void clr (size_t i) |
|---|
| 114 |
{ |
|---|
| 115 |
bits[i / width] &= ~(1 << (i % width)); |
|---|
| 116 |
//btr (&bits[i / width], i % width); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
/********************************************************************** |
|---|
| 120 |
|
|---|
| 121 |
Clear all bits |
|---|
| 122 |
|
|---|
| 123 |
**********************************************************************/ |
|---|
| 124 |
|
|---|
| 125 |
BitSet* clr () |
|---|
| 126 |
{ |
|---|
| 127 |
bits[] = 0; |
|---|
| 128 |
return this; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
/********************************************************************** |
|---|
| 132 |
|
|---|
| 133 |
Clone this BitSet and return it |
|---|
| 134 |
|
|---|
| 135 |
**********************************************************************/ |
|---|
| 136 |
|
|---|
| 137 |
BitSet dup () |
|---|
| 138 |
{ |
|---|
| 139 |
BitSet x; |
|---|
| 140 |
static if (Count == 0) |
|---|
| 141 |
x.bits.length = this.bits.length; |
|---|
| 142 |
x.bits[] = bits[]; |
|---|
| 143 |
return x; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
/********************************************************************** |
|---|
| 147 |
|
|---|
| 148 |
Return the number of bits we have room for |
|---|
| 149 |
|
|---|
| 150 |
**********************************************************************/ |
|---|
| 151 |
|
|---|
| 152 |
size_t size () |
|---|
| 153 |
{ |
|---|
| 154 |
return width * bits.length; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
/********************************************************************** |
|---|
| 158 |
|
|---|
| 159 |
Expand to include the indexed bit (dynamic only) |
|---|
| 160 |
|
|---|
| 161 |
**********************************************************************/ |
|---|
| 162 |
|
|---|
| 163 |
static if (Count == 0) BitSet* size (size_t i) |
|---|
| 164 |
{ |
|---|
| 165 |
i = i / width; |
|---|
| 166 |
if (i >= bits.length) |
|---|
| 167 |
bits.length = i + 1; |
|---|
| 168 |
return this; |
|---|
| 169 |
} |
|---|
| 170 |
} |
|---|