Guido van Rossum | b6957e4 | 1993-10-27 09:27:13 +0000 | [diff] [blame] | 1 | # |
| 2 | # this is a rather strict implementation of a bit vector class |
| 3 | # it is accessed the same way as an array of python-ints, except |
| 4 | # the value must be 0 or 1 |
| 5 | # |
| 6 | |
| 7 | import sys; rprt = sys.stderr.write #for debugging |
| 8 | |
| 9 | error = 'bitvec.error' |
| 10 | |
| 11 | |
| 12 | def _check_value(value): |
| 13 | if type(value) != type(0) or not 0 <= value < 2: |
| 14 | raise error, 'bitvec() items must have int value 0 or 1' |
| 15 | |
| 16 | |
| 17 | import math |
| 18 | |
| 19 | def _compute_len(param): |
| 20 | mant, l = math.frexp(float(param)) |
| 21 | bitmask = 1L << l |
| 22 | if bitmask <= param: |
| 23 | raise 'FATAL', '(param, l) = ' + `param, l` |
| 24 | while l: |
| 25 | bitmask = bitmask >> 1 |
| 26 | if param & bitmask: |
| 27 | break |
| 28 | l = l - 1 |
| 29 | return l |
| 30 | |
| 31 | |
| 32 | def _check_key(len, key): |
| 33 | if type(key) != type(0): |
| 34 | raise TypeError, 'sequence subscript not int' |
| 35 | if key < 0: |
| 36 | key = key + len |
| 37 | if not 0 <= key < len: |
| 38 | raise IndexError, 'list index out of range' |
| 39 | return key |
| 40 | |
| 41 | def _check_slice(len, i, j): |
| 42 | #the type is ok, Python already checked that |
| 43 | i, j = max(i, 0), min(len, j) |
| 44 | if i > j: |
| 45 | i = j |
| 46 | return i, j |
| 47 | |
| 48 | |
| 49 | class BitVec: |
| 50 | |
| 51 | def init(self, *params): |
| 52 | self._data = 0L |
| 53 | self._len = 0 |
| 54 | if not len(params): |
| 55 | pass |
| 56 | elif len(params) == 1: |
| 57 | param, = params |
| 58 | if type(param) == type([]): |
| 59 | value = 0L |
| 60 | bit_mask = 1L |
| 61 | for item in param: |
| 62 | # strict check |
| 63 | #_check_value(item) |
| 64 | if item: |
| 65 | value = value | bit_mask |
| 66 | bit_mask = bit_mask << 1 |
| 67 | self._data = value |
| 68 | self._len = len(param) |
| 69 | elif type(param) == type(0L): |
| 70 | if param < 0: |
| 71 | raise error, 'bitvec() can\'t handle negative longs' |
| 72 | self._data = param |
| 73 | self._len = _compute_len(param) |
| 74 | else: |
| 75 | raise error, 'bitvec() requires array or long parameter' |
| 76 | elif len(params) == 2: |
| 77 | param, length = params |
| 78 | if type(param) == type(0L): |
| 79 | if param < 0: |
| 80 | raise error, \ |
| 81 | 'can\'t handle negative longs' |
| 82 | self._data = param |
| 83 | if type(length) != type(0): |
| 84 | raise error, 'bitvec()\'s 2nd parameter must be int' |
| 85 | computed_length = _compute_len(param) |
| 86 | if computed_length > length: |
| 87 | print 'warning: bitvec() value is longer than the lenght indicates, truncating value' |
| 88 | self._data = self._data & \ |
| 89 | ((1L << length) - 1) |
| 90 | self._len = length |
| 91 | else: |
| 92 | raise error, 'bitvec() requires array or long parameter' |
| 93 | else: |
| 94 | raise error, 'bitvec() requires 0 -- 2 parameter(s)' |
| 95 | |
| 96 | return self |
| 97 | |
| 98 | |
| 99 | def _init(self, data, len): |
| 100 | self._data = data |
| 101 | self._len = len |
| 102 | return self |
| 103 | |
| 104 | |
| 105 | def append(self, item): |
| 106 | #_check_value(item) |
| 107 | #self[self._len:self._len] = [item] |
| 108 | self[self._len:self._len] = \ |
| 109 | BitVec()._init(long(not not item), 1) |
| 110 | |
| 111 | |
| 112 | def count(self, value): |
| 113 | #_check_value(value) |
| 114 | if value: |
| 115 | data = self._data |
| 116 | else: |
| 117 | data = (~self)._data |
| 118 | count = 0 |
| 119 | while data: |
| 120 | data, count = data >> 1, count + (data & 1 != 0) |
| 121 | return count |
| 122 | |
| 123 | |
| 124 | def index(self, value): |
| 125 | #_check_value(value): |
| 126 | if value: |
| 127 | data = self._data |
| 128 | else: |
| 129 | data = (~self)._data |
| 130 | index = 0 |
| 131 | if not data: |
| 132 | raise ValueError, 'list.index(x): x not in list' |
| 133 | while not (data & 1): |
| 134 | data, index = data >> 1, index + 1 |
| 135 | return index |
| 136 | |
| 137 | |
| 138 | def insert(self, index, item): |
| 139 | #_check_value(item) |
| 140 | #self[index:index] = [item] |
| 141 | self[index:index] = BitVec()._init(long(not not item), 1) |
| 142 | |
| 143 | |
| 144 | def remove(self, value): |
| 145 | del self[self.index(value)] |
| 146 | |
| 147 | |
| 148 | def reverse(self): |
| 149 | #ouch, this one is expensive! |
| 150 | #for i in self._len>>1: self[i], self[l-i] = self[l-i], self[i] |
| 151 | data, result = self._data, 0L |
| 152 | for i in range(self._len): |
| 153 | if not data: |
| 154 | result = result << (self._len - i) |
| 155 | break |
| 156 | result, data = (result << 1) | (data & 1), data >> 1 |
| 157 | self._data = result |
| 158 | |
| 159 | |
| 160 | def sort(self): |
| 161 | c = self.count(1) |
| 162 | self._data = ((1L << c) - 1) << (self._len - c) |
| 163 | |
| 164 | |
| 165 | def copy(self): |
| 166 | return BitVec()._init(self._data, self._len) |
| 167 | |
| 168 | |
| 169 | def seq(self): |
| 170 | result = [] |
| 171 | for i in self: |
| 172 | result.append(i) |
| 173 | return result |
| 174 | |
| 175 | |
| 176 | def __repr__(self): |
| 177 | ##rprt('<bitvec class instance object>.' + '__repr__()\n') |
| 178 | return 'bitvec' + `self._data, self._len` |
| 179 | |
| 180 | def __cmp__(self, other, *rest): |
| 181 | #rprt(`self`+'.__cmp__'+`(other, ) + rest`+'\n') |
| 182 | if type(other) != type(self): |
| 183 | other = apply(bitvec, (other, ) + rest) |
| 184 | #expensive solution... recursive binary, with slicing |
| 185 | length = self._len |
| 186 | if length == 0 or other._len == 0: |
| 187 | return cmp(length, other._len) |
| 188 | if length != other._len: |
| 189 | min_lenght = min(length, other._len) |
| 190 | return cmp(self[:min_length], other[:min_length]) or \ |
| 191 | cmp(self[min_length:], other[min_length:]) |
| 192 | #the lengths are the same now... |
| 193 | if self._data == other._data: |
| 194 | return 0 |
| 195 | if length == 1: |
| 196 | return cmp(self[0], other[0]) |
| 197 | else: |
| 198 | length = length >> 1 |
| 199 | return cmp(self[:length], other[:length]) or \ |
| 200 | cmp(self[length:], other[length:]) |
| 201 | |
| 202 | |
| 203 | def __len__(self): |
| 204 | #rprt(`self`+'.__len__()\n') |
| 205 | return self._len |
| 206 | |
| 207 | def __getitem__(self, key): |
| 208 | #rprt(`self`+'.__getitem__('+`key`+')\n') |
| 209 | key = _check_key(self._len, key) |
| 210 | return self._data & (1L << key) != 0 |
| 211 | |
| 212 | def __setitem__(self, key, value): |
| 213 | #rprt(`self`+'.__setitem__'+`key, value`+'\n') |
| 214 | key = _check_key(self._len, key) |
| 215 | #_check_value(value) |
| 216 | if value: |
| 217 | self._data = self._data | (1L << key) |
| 218 | else: |
| 219 | self._data = self._data & ~(1L << key) |
| 220 | |
| 221 | def __delitem__(self, key): |
| 222 | #rprt(`self`+'.__delitem__('+`key`+')\n') |
| 223 | key = _check_key(self._len, key) |
| 224 | #el cheapo solution... |
| 225 | self._data = self[:key]._data | self[key+1:]._data >> key |
| 226 | self._len = self._len - 1 |
| 227 | |
| 228 | def __getslice__(self, i, j): |
| 229 | #rprt(`self`+'.__getslice__'+`i, j`+'\n') |
| 230 | i, j = _check_slice(self._len, i, j) |
| 231 | if i >= j: |
| 232 | return BitVec()._init(0L, 0) |
| 233 | if i: |
| 234 | ndata = self._data >> i |
| 235 | else: |
| 236 | ndata = self._data |
| 237 | nlength = j - i |
| 238 | if j != self._len: |
| 239 | #we'll have to invent faster variants here |
| 240 | #e.g. mod_2exp |
| 241 | ndata = ndata & ((1L << nlength) - 1) |
| 242 | return BitVec()._init(ndata, nlength) |
| 243 | |
| 244 | def __setslice__(self, i, j, sequence, *rest): |
| 245 | #rprt(`self`+'.__setslice__'+`(i, j, sequence) + rest`+'\n') |
| 246 | i, j = _check_slice(self._len, i, j) |
| 247 | if type(sequence) != type(self): |
| 248 | sequence = apply(bitvec, (sequence, ) + rest) |
| 249 | #sequence is now of our own type |
| 250 | ls_part = self[:i] |
| 251 | ms_part = self[j:] |
| 252 | self._data = ls_part._data | \ |
| 253 | ((sequence._data | \ |
| 254 | (ms_part._data << sequence._len)) << ls_part._len) |
| 255 | self._len = self._len - j + i + sequence._len |
| 256 | |
| 257 | def __delslice__(self, i, j): |
| 258 | #rprt(`self`+'.__delslice__'+`i, j`+'\n') |
| 259 | i, j = _check_slice(self._len, i, j) |
| 260 | if i == 0 and j == self._len: |
| 261 | self._data, self._len = 0L, 0 |
| 262 | elif i < j: |
| 263 | self._data = self[:i]._data | (self[j:]._data >> i) |
| 264 | self._len = self._len - j + i |
| 265 | |
| 266 | def __add__(self, other): |
| 267 | #rprt(`self`+'.__add__('+`other`+')\n') |
| 268 | retval = self.copy() |
| 269 | retval[self._len:self._len] = other |
| 270 | return retval |
| 271 | |
| 272 | def __mul__(self, multiplier): |
| 273 | #rprt(`self`+'.__mul__('+`multiplier`+')\n') |
| 274 | if type(multiplier) != type(0): |
| 275 | raise TypeError, 'sequence subscript not int' |
| 276 | if multiplier <= 0: |
| 277 | return BitVec()._init(0L, 0) |
| 278 | elif multiplier == 1: |
| 279 | return self.copy() |
| 280 | #handle special cases all 0 or all 1... |
| 281 | if self._data == 0L: |
| 282 | return BitVec()._init(0L, self._len * multiplier) |
| 283 | elif (~self)._data == 0L: |
| 284 | return ~BitVec()._init(0L, self._len * multiplier) |
| 285 | #otherwise el cheapo again... |
| 286 | retval = BitVec()._init(0L, 0) |
| 287 | while multiplier: |
| 288 | retval, multiplier = retval + self, multiplier - 1 |
| 289 | return retval |
| 290 | |
| 291 | def __and__(self, otherseq, *rest): |
| 292 | #rprt(`self`+'.__and__'+`(otherseq, ) + rest`+'\n') |
| 293 | if type(otherseq) != type(self): |
| 294 | otherseq = apply(bitvec, (otherseq, ) + rest) |
| 295 | #sequence is now of our own type |
| 296 | return BitVec()._init(self._data & otherseq._data, \ |
| 297 | min(self._len, otherseq._len)) |
| 298 | |
| 299 | |
| 300 | def __xor__(self, otherseq, *rest): |
| 301 | #rprt(`self`+'.__xor__'+`(otherseq, ) + rest`+'\n') |
| 302 | if type(otherseq) != type(self): |
| 303 | otherseq = apply(bitvec, (otherseq, ) + rest) |
| 304 | #sequence is now of our own type |
| 305 | return BitVec()._init(self._data ^ otherseq._data, \ |
| 306 | max(self._len, otherseq._len)) |
| 307 | |
| 308 | |
| 309 | def __or__(self, otherseq, *rest): |
| 310 | #rprt(`self`+'.__or__'+`(otherseq, ) + rest`+'\n') |
| 311 | if type(otherseq) != type(self): |
| 312 | otherseq = apply(bitvec, (otherseq, ) + rest) |
| 313 | #sequence is now of our own type |
| 314 | return BitVec()._init(self._data | otherseq._data, \ |
| 315 | max(self._len, otherseq._len)) |
| 316 | |
| 317 | |
| 318 | def __invert__(self): |
| 319 | #rprt(`self`+'.__invert__()\n') |
| 320 | return BitVec()._init(~self._data & ((1L << self._len) - 1), \ |
| 321 | self._len) |
| 322 | |
| 323 | def __coerce__(self, otherseq, *rest): |
| 324 | #needed for *some* of the arithmetic operations |
| 325 | #rprt(`self`+'.__coerce__'+`(otherseq, ) + rest`+'\n') |
| 326 | if type(otherseq) != type(self): |
| 327 | otherseq = apply(bitvec, (otherseq, ) + rest) |
| 328 | return self, otherseq |
| 329 | |
| 330 | def __int__(self): |
| 331 | return int(self._data) |
| 332 | |
| 333 | def __long__(self): |
| 334 | return long(self._data) |
| 335 | |
| 336 | def __float__(self): |
| 337 | return float(self._data) |
| 338 | |
| 339 | |
| 340 | def bitvec(params): |
| 341 | return apply(BitVec().init, params) |