diff options
Diffstat (limited to 'Demo/classes/bitvec.py')
-rwxr-xr-x | Demo/classes/bitvec.py | 75 |
1 files changed, 37 insertions, 38 deletions
diff --git a/Demo/classes/bitvec.py b/Demo/classes/bitvec.py index 934d33a..97921f2 100755 --- a/Demo/classes/bitvec.py +++ b/Demo/classes/bitvec.py @@ -11,14 +11,14 @@ error = 'bitvec.error' def _check_value(value): if type(value) != type(0) or not 0 <= value < 2: - raise error, 'bitvec() items must have int value 0 or 1' + raise error('bitvec() items must have int value 0 or 1') import math def _compute_len(param): mant, l = math.frexp(float(param)) - bitmask = 1L << l + bitmask = 1 << l if bitmask <= param: raise 'FATAL', '(param, l) = %r' % ((param, l),) while l: @@ -31,11 +31,11 @@ def _compute_len(param): def _check_key(len, key): if type(key) != type(0): - raise TypeError, 'sequence subscript not int' + raise TypeError('sequence subscript not int') if key < 0: key = key + len if not 0 <= key < len: - raise IndexError, 'list index out of range' + raise IndexError('list index out of range') return key def _check_slice(len, i, j): @@ -49,15 +49,15 @@ def _check_slice(len, i, j): class BitVec: def __init__(self, *params): - self._data = 0L + self._data = 0 self._len = 0 if not len(params): pass elif len(params) == 1: param, = params if type(param) == type([]): - value = 0L - bit_mask = 1L + value = 0 + bit_mask = 1 for item in param: # strict check #_check_value(item) @@ -66,39 +66,38 @@ class BitVec: bit_mask = bit_mask << 1 self._data = value self._len = len(param) - elif type(param) == type(0L): + elif type(param) == type(0): if param < 0: - raise error, 'bitvec() can\'t handle negative longs' + raise error('bitvec() can\'t handle negative longs') self._data = param self._len = _compute_len(param) else: - raise error, 'bitvec() requires array or long parameter' + raise error('bitvec() requires array or long parameter') elif len(params) == 2: param, length = params - if type(param) == type(0L): + if type(param) == type(0): if param < 0: - raise error, \ - 'can\'t handle negative longs' + raise error('can\'t handle negative longs') self._data = param if type(length) != type(0): - raise error, 'bitvec()\'s 2nd parameter must be int' + raise error('bitvec()\'s 2nd parameter must be int') computed_length = _compute_len(param) if computed_length > length: - print 'warning: bitvec() value is longer than the length indicates, truncating value' + print('warning: bitvec() value is longer than the length indicates, truncating value') self._data = self._data & \ - ((1L << length) - 1) + ((1 << length) - 1) self._len = length else: - raise error, 'bitvec() requires array or long parameter' + raise error('bitvec() requires array or long parameter') else: - raise error, 'bitvec() requires 0 -- 2 parameter(s)' + raise error('bitvec() requires 0 -- 2 parameter(s)') def append(self, item): #_check_value(item) #self[self._len:self._len] = [item] self[self._len:self._len] = \ - BitVec(long(not not item), 1) + BitVec(int(not not item), 1) def count(self, value): @@ -121,7 +120,7 @@ class BitVec: data = (~self)._data index = 0 if not data: - raise ValueError, 'list.index(x): x not in list' + raise ValueError('list.index(x): x not in list') while not (data & 1): data, index = data >> 1, index + 1 return index @@ -130,7 +129,7 @@ class BitVec: def insert(self, index, item): #_check_value(item) #self[index:index] = [item] - self[index:index] = BitVec(long(not not item), 1) + self[index:index] = BitVec(int(not not item), 1) def remove(self, value): @@ -140,7 +139,7 @@ class BitVec: def reverse(self): #ouch, this one is expensive! #for i in self._len>>1: self[i], self[l-i] = self[l-i], self[i] - data, result = self._data, 0L + data, result = self._data, 0 for i in range(self._len): if not data: result = result << (self._len - i) @@ -151,7 +150,7 @@ class BitVec: def sort(self): c = self.count(1) - self._data = ((1L << c) - 1) << (self._len - c) + self._data = ((1 << c) - 1) << (self._len - c) def copy(self): @@ -199,16 +198,16 @@ class BitVec: def __getitem__(self, key): #rprt('%r.__getitem__(%r)\n' % (self, key)) key = _check_key(self._len, key) - return self._data & (1L << key) != 0 + return self._data & (1 << key) != 0 def __setitem__(self, key, value): #rprt('%r.__setitem__(%r, %r)\n' % (self, key, value)) key = _check_key(self._len, key) #_check_value(value) if value: - self._data = self._data | (1L << key) + self._data = self._data | (1 << key) else: - self._data = self._data & ~(1L << key) + self._data = self._data & ~(1 << key) def __delitem__(self, key): #rprt('%r.__delitem__(%r)\n' % (self, key)) @@ -221,7 +220,7 @@ class BitVec: #rprt('%r.__getslice__(%r, %r)\n' % (self, i, j)) i, j = _check_slice(self._len, i, j) if i >= j: - return BitVec(0L, 0) + return BitVec(0, 0) if i: ndata = self._data >> i else: @@ -230,7 +229,7 @@ class BitVec: if j != self._len: #we'll have to invent faster variants here #e.g. mod_2exp - ndata = ndata & ((1L << nlength) - 1) + ndata = ndata & ((1 << nlength) - 1) return BitVec(ndata, nlength) def __setslice__(self, i, j, sequence, *rest): @@ -250,7 +249,7 @@ class BitVec: #rprt('%r.__delslice__(%r, %r)\n' % (self, i, j)) i, j = _check_slice(self._len, i, j) if i == 0 and j == self._len: - self._data, self._len = 0L, 0 + self._data, self._len = 0, 0 elif i < j: self._data = self[:i]._data | (self[j:]._data >> i) self._len = self._len - j + i @@ -264,18 +263,18 @@ class BitVec: def __mul__(self, multiplier): #rprt('%r.__mul__(%r)\n' % (self, multiplier)) if type(multiplier) != type(0): - raise TypeError, 'sequence subscript not int' + raise TypeError('sequence subscript not int') if multiplier <= 0: - return BitVec(0L, 0) + return BitVec(0, 0) elif multiplier == 1: return self.copy() #handle special cases all 0 or all 1... - if self._data == 0L: - return BitVec(0L, self._len * multiplier) - elif (~self)._data == 0L: - return ~BitVec(0L, self._len * multiplier) + if self._data == 0: + return BitVec(0, self._len * multiplier) + elif (~self)._data == 0: + return ~BitVec(0, self._len * multiplier) #otherwise el cheapo again... - retval = BitVec(0L, 0) + retval = BitVec(0, 0) while multiplier: retval, multiplier = retval + self, multiplier - 1 return retval @@ -309,7 +308,7 @@ class BitVec: def __invert__(self): #rprt('%r.__invert__()\n' % (self,)) - return BitVec(~self._data & ((1L << self._len) - 1), \ + return BitVec(~self._data & ((1 << self._len) - 1), \ self._len) def __coerce__(self, otherseq, *rest): @@ -323,7 +322,7 @@ class BitVec: return int(self._data) def __long__(self): - return long(self._data) + return int(self._data) def __float__(self): return float(self._data) |