-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbf.py
More file actions
215 lines (151 loc) · 5.43 KB
/
bf.py
File metadata and controls
215 lines (151 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import array
class BitField(object):
def __init__(self):
self.array = array.array('I')
self.initializer = 0
self.shift = 3
self.mask = 0x7
def __count_bits(self, n):
count = 0
while n:
count += 1
n &= (n - 1)
return count
def count(self):
count = 0
for x in range(0, len(self.array)):
count += self.__count_bits(self.array[x])
return count
def hb(self, n):
return self.__highest_bit(n)
def __highest_bit(self, n):
count = 0
while n != 1:
n = n>>1
count += 1
return count
def max(self):
x = len(self.array) -1
while x >= 0:
val = self.array[x]
if val != 0:
return self.__highest_bit(val) + (x * 2**self.shift)
x -= 1
return 0
def __array(self):
return self.array
def __reverse(self):
self.initializer = 7
def __get_index(self, i):
return (i>>self.shift)
def set(self, i):
self.__prefill(self.__get_index(i))
self.array[self.__get_index(i)] |= (1 << (i&self.mask))
def unset(self, i):
if len(self.array) == 0:
pass
elif len(self.array) <= (self.__get_index(i)):
pass
else:
self.array[self.__get_index(i)] ^= (1 << (i&self.mask)) # uses xor=
def get(self, i):
if len(self.array) == 0:
return False
elif len(self.array)-1 < (self.__get_index(i)):
return False
else:
return (self.array[self.__get_index(i)] & (1 << (i&self.mask))) != 0
def __prefill(self, i):
while len(self.array) <= i:
self.array.fromlist([self.initializer] * 10)
# returns a paginated list of bits that are set
def indexes_by_page(self, page=1, per_page=10, starting_index=0):
def items_in_array_index(k):
items = []
for i in range(0,8):
if (k & (1 << (i&self.mask))) != 0:
items.append(i)
return items
items = []
for x in range(self.__get_index(starting_index), len(self.array)):
if self.array[x] != 0:
indexes = items_in_array_index(self.array[x])
positioned = [(x * 8) + i for i in indexes]
positioned = filter(lambda x: x > starting_index, positioned)
items.extend(positioned)
#OPTIMIZE: this will keep searching after enough have been found
return items[(page * per_page)-per_page:page * per_page]
def fromstring(self, value):
self.array.fromstring(value)
def __str__(self):
return self.tostring()
def tostring(self):
return self.array.tostring()
#FIXME: this is not really done properly... should be false if one is bigger, for example
def __eq__(self, other):
smaller = min(len(self.__array()), len(other.__array()))
for i in range(0, smaller):
if self.__array()[i] != other.__array()[i]:
return False
return True
def __compareeacharray(self, other, fcompare, fminmax):
b = BitField()
# deepcopy is used here to prevent unnecessarily growing arrays... in some cases the growth
# is just for a comparison and can be discarded after the comparison returns
s = copy.deepcopy(self)
o = copy.deepcopy(other)
while len(s.__array()) < len(o.__array()):
s.__array().fromlist([self.initializer] * 100)
while len(o.__array()) < len(s.__array()):
o.__array().fromlist([self.initializer] * 100)
for i in range(0, len(s.__array())):
b.__array().append( fcompare(s.__array()[i], o.__array()[i]))
return b
def __xor__(self, other):
return self.__compareeacharray(other, long.__xor__, max)
def __or__(self, other):
return self.__compareeacharray(other, long.__or__, max)
def __and__(self, other):
return self.__compareeacharray(other, long.__and__, max)
def __compareeach(self, other, fcompare, fminmax):
b = BitField()
dominant = fminmax(self.max(), other.max())
for i in range(0, dominant+1):
s = self.get(i)
o = other.get(i)
fcompare(s, o, b, i)
return b
# item by item invert, ... tbd what to do... probably a custom method that takes two args... the
# bitfield and the size to invert it for ?
def __invert__(self):
b = BitField()
for i in range(0, self.max()):
if not self.get(i): b.set(i)
return b
def __iadd__(self, other):
b = (self | other)
self.array = b.__array()
return self
def __isub__(self, other):
b = (self ^ other) & self
self.array = b.__array()
return self
def tobitstring(self, length=20):
out = ""
for x in range(0, length):
if self.get(x):
out += "1"
else:
out += "0"
return out
def copy(self):
c = BitField()
c += self
return c
def test():
b1 = BitField()
assert b1.get(33) == False
assert b1.get(523424) == False
print "tests finished"
if __name__ == "__main__":
test()