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