annotate Demo/classes/bitvec.py @ 58765:2ba7677bfa95 legacy-trunk

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