forked from thomasballinger/bittorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparsebitarray.py
567 lines (506 loc) · 20.5 KB
/
sparsebitarray.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
"""
Sparse (really Frequently Contiguous) Binary Array
TODO:
Eliminate need for .normalize() by ensuring self.changes never have duplicates
binary search to find overlapping segments
"""
import operator
import bisect
import collections
import sys
class SparseArray(object):
"""Abstract class for code shared between Sparse Arrays"""
def __init__(self, length=None, iterable=None, scale=None, repetitions=None, default=None):
if (length is None and iterable is None) or (length is not None and iterable is not None):
raise ValueError("Must initialize with either length or iterable")
if length is not None and (scale is not None or repetitions is not None):
raise ValueError("repate and scale can't be used with length")
if length is not None:
self.length = length
self._initialize_structures()
if default is not None:
self[:] = default
else:
scale = scale if scale is not None else 1
repetitions = repetitions if repetitions is not None else 1
if isinstance(iterable, basestring):
iterable = [int(x) for x in iterable]
else:
iterable = list(iterable)
self.length = len(iterable)*scale*(repetitions)
self._initialize_structures()
start = 0
for i in range(repetitions):
for x in iterable:
end = start + scale
self[start:end] = x
start += scale
def __len__(self):
return self.length
def _decode_slice(self, key):
start, step, end = key.start, key.step, key.stop
if start is None: start = 0
if end is None: end = len(self)
if end == sys.maxint: end = len(self)
if step not in [None, 1]: raise ValueError("Custom steps not allowed: "+repr(key))
return start, end
def _indices(self, start, end):
start_index = bisect.bisect_right(self.changes, start)
end_index = bisect.bisect_left(self.changes, end)
return start_index, end_index
def _apply_sparsearraywise(f):
"""Creates a function which will combine two arrays elementwise given a function f
"""
def arraywise_function(self, other):
if not isinstance(other, self.__class__):
other = self.__class__(self.length, default=other)
self_index = 0
other_index = 0
new = self.__class__(self.length)
cur = 0
while True:
self_num = self.changes[self_index] if self_index < len(self.changes) else len(self)
other_num = other.changes[other_index] if other_index < len(other.changes) else len(self)
if self_num == other_num == len(self):
break
if self_num <= other_num:
old_cur = cur
cur = self_num
self_index += 1
if self_num == other_num:
continue
else:
old_cur = cur
cur = self_num
other_index += 1
if cur == old_cur:
continue
new[old_cur:cur] = f(self[old_cur], other[old_cur])
return new
return arraywise_function
class SparseObjectArray(SparseArray):
"""Sparse Array of Objects, in which data is represented by indices of
changes from one value to another
>>> a = SparseObjectArray(2, default=frozenset([7])); a
SparseObjectArray([frozenset([7]), frozenset([7])])
>>> b = SparseObjectArray(2, default=frozenset()); b[1:2] = frozenset([1,2,3]);
>>> b
SparseObjectArray([frozenset([]), frozenset([1, 2, 3])])
>>> a.arraywise(frozenset.union, b)
SparseObjectArray([frozenset([7]), frozenset([7])])
>>> a.union(b)
SparseObjectArray([frozenset([7]), frozenset([7])])
>>> SparseObjectArray(2)
SparseObjectArray([None, None])
>>> a = SparseObjectArray(4, default=0); a
SparseObjectArray([0, 0, 0, 0])
>>> a[1:3]
SparseObjectArray([0, 0])
>>> a[1:3] = 1; a
SparseObjectArray([0, 1, 1, 0])
>>> a = SparseObjectArray(4, default=0)
#>>> a[1:3].changes
#[0]
#>>> a[1:3] + 1; a
#SparseObjectArray([1, 1])
"""
def __init__(self, *args, **kwargs):
super(SparseObjectArray, self).__init__(*args, **kwargs)
def _initialize_structures(self):
self.values = [None]
self.changes = [0]
self.runs = collections.defaultdict(set)
self.runs[None].add((0, self.length))
def arraywise(self, f, other):
"""Apply any 2-argument function arraywise.
>>> a = SparseObjectArray(4, default=2)
>>> b = SparseObjectArray(4, default=3)
>>> import operator
>>> a.arraywise(operator.lt, b)
SparseObjectArray([True, True, True, True])
"""
g = _apply_sparsearraywise(f)
return g(self, other)
union = _apply_sparsearraywise(frozenset.union)
__add__ = _apply_sparsearraywise(operator.add)
def __sub__(self, other):
"""For combining two arrays elementwise, not for extending arrays"""
raise Exception("not Yet implemented")
def all(self):
"""TODO implement caching, this only does a few falsy things
>>> a = SparseObjectArray(4, default=2); a.all()
True
>>> a[1:2] = 0; a.all()
False
"""
return all(self.runs.keys())
def none(self):
return not any(self.runs.keys())
def index(self, value):
"""Return index of element or raise ValueError if not found
>>> a = SparseObjectArray(4, default=2); a.index(2)
0
"""
try:
return min(x[0] for x in self.runs[value])
except IndexError:
raise ValueError('%s is not in %s instance' % (value, self.__class__.__name__))
def __getitem__(self, key):
"""Get a slice or the value of an entry
>>> s = SparseObjectArray(6, default=0); s[2:4] = 2;
>>> s, s.changes, s.values
(SparseObjectArray([0, 0, 2, 2, 0, 0]), [0, 2, 4], [0, 2, 0])
>>> a = s[1:5]; a, a.changes, a.values, a.runs
(SparseObjectArray([0, 2, 2, 0]), [0, 1, 3], [0, 2, 0], defaultdict(<type 'set'>, {0: set([(0, 1), (3, 4)]), 2: set([(1, 3)])}))
>>> b = s[2:4]; b, b.changes, b.values
(SparseObjectArray([2, 2]), [0], [2])
>>> s[0:5]
SparseObjectArray([0, 0, 2, 2, 0])
>>> s[1:6]
SparseObjectArray([0, 2, 2, 0, 0])
>>> s[2:6]
SparseObjectArray([2, 2, 0, 0])
>>> s = SparseObjectArray(6, default=0); s.changes = [0]; s.values = [0]; s.runs = {0:{(0, 6)}}
>>> s
SparseObjectArray([0, 0, 0, 0, 0, 0])
>>> s = SparseObjectArray(4, default=0)
>>> a = s[1:3]
>>> a.changes
[0]
"""
if isinstance(key, slice):
start, end = self._decode_slice(key)
before_or_at_start = bisect.bisect_right(self.changes, start) - 1
before_or_at_end = bisect.bisect_right(self.changes, end) - 1
result = SparseObjectArray(end - start)
result.changes = [max(0, change - start) for change in self.changes[before_or_at_start:before_or_at_end]]
result.values = self.values[before_or_at_start:before_or_at_end]
if self.changes[before_or_at_end] == end:
pass
elif self.changes[before_or_at_end] < end:
result.changes.append(max(0, self.changes[before_or_at_end] - start))
result.values.append(self.values[before_or_at_end])
else:
raise Exception('Logic Error')
result.runs = collections.defaultdict(set)
temp = result.changes+[end-start]
for i, value in enumerate(result.values):
result.runs[value].add((result.changes[i], temp[i+1]))
return result
else:
if key >= len(self):
raise IndexError(key)
index_of_value_at_or_before_pos = bisect.bisect_right(self.changes, key) - 1
return self.values[index_of_value_at_or_before_pos]
def __repr__(self):
return 'SparseObjectArray('+repr([x for x in self])+')'
def __setitem__(self, key, value):
"""Sets item or slice to an integer
changes, values, runs
[0], [0], {0:{(0, 100)}}
s[5:10] = 2
[0,5,10], [0, 2, 0], {0:{(0, 5), (10, 100)}, 2:{(5, 10)}}
>>> s = SparseObjectArray(6, default=0); s
SparseObjectArray([0, 0, 0, 0, 0, 0])
>>> s.runs
defaultdict(<type 'set'>, {0: set([(0, 6)])})
>>> s[4:6] = 7; s.changes, s.values, s
([0, 4], [0, 7], SparseObjectArray([0, 0, 0, 0, 7, 7]))
>>> s.runs
defaultdict(<type 'set'>, {0: set([(0, 4)]), 7: set([(4, 6)])})
>>> s[:1] = 3; s.changes, s.values, s
([0, 1, 4], [3, 0, 7], SparseObjectArray([3, 0, 0, 0, 7, 7]))
>>> s.runs
defaultdict(<type 'set'>, {0: set([(1, 4)]), 3: set([(0, 1)]), 7: set([(4, 6)])})
>>> s[1:2] = 6; s.changes, s.values, s
([0, 1, 2, 4], [3, 6, 0, 7], SparseObjectArray([3, 6, 0, 0, 7, 7]))
>>> s.runs
defaultdict(<type 'set'>, {0: set([(2, 4)]), 3: set([(0, 1)]), 6: set([(1, 2)]), 7: set([(4, 6)])})
>>> s[:] = 0; s.changes, s.values, s
([0], [0], SparseObjectArray([0, 0, 0, 0, 0, 0]))
>>> s.runs
defaultdict(<type 'set'>, {0: set([(0, 6)])})
"""
if isinstance(key, slice):
start, end = self._decode_slice(key)
before_or_at_start = bisect.bisect_right(self.changes, start) - 1
before_or_at_end = bisect.bisect_right(self.changes, end) - 1
new_changes = self.changes[:before_or_at_start]
new_values = self.values[:before_or_at_start]
before_run = None
new_run = [start, end]
after_run = None
if self.values[before_or_at_start] == value:
new_changes.append(self.changes[before_or_at_start])
new_values.append(self.changes[before_or_at_start])
# replace before run, if finish actually extends it
new_run[0] = self.changes[before_or_at_start]
elif self.changes[before_or_at_start] == start:
new_changes.append(start)
new_values.append(value)
# throw out the before run, then add new one
elif self.changes[before_or_at_start] < start:
new_changes.append(self.changes[before_or_at_start])
new_values.append(self.values[before_or_at_start])
new_changes.append(start)
new_values.append(value)
# shorten before run, then add new one
before_run = [self.changes[before_or_at_start], start]
else:
raise Exception("Logic Error")
if end == self.length:
pass
# extend run we're modifying
elif self.values[before_or_at_end] == value:
# extend run we're modifying through next run and remove after run
new_run[1] = self.changes[before_or_at_end]
elif self.changes[before_or_at_end] == end:
new_changes.append(self.changes[before_or_at_end])
new_values.append(self.values[before_or_at_end])
# use our run, keep next run
if len(self.changes) == before_or_at_end + 1:
after_run = [end, len(self)]
else:
after_run = [end, self.changes[before_or_at_end+1]]
elif self.changes[before_or_at_end] < end:
new_changes.append(end)
new_values.append(self.values[before_or_at_end])
# add our run, shorten next run
if len(self.changes) == before_or_at_end + 1:
after_run = [end, len(self)]
else:
after_run = [end, self.changes[before_or_at_end + 1]]
else:
raise Exception("Logic Error")
new_changes.extend(self.changes[before_or_at_end+1:])
new_values.extend(self.values[before_or_at_end+1:])
self.changes += [len(self)] # not how it's stored, but it's never accessed again for loop works better
for i in range(before_or_at_start, before_or_at_end+1):
self.runs[self.values[i]].remove((self.changes[i], self.changes[i+1]))
if self.runs[self.values[i]] == set():
del self.runs[self.values[i]]
if before_run:
self.runs[self.values[before_or_at_start]].add(tuple(before_run))
self.runs[value].add(tuple(new_run))
if after_run:
self.runs[self.values[before_or_at_end]].add(tuple(after_run))
self.changes = new_changes
self.values = new_values
else:
raise ValueError("Single element assignment not allowed")
class SparseBitArray(SparseArray):
"""Sparse BitArray, in which data is represented by indices of changes
between runs of set and unset values
>>> s = SparseBitArray(20); s
SparseBitArray('00000000000000000000')
>>> s = SparseBitArray(iterable=[1,0,0,1], scale=3, repetitions=2); s
SparseBitArray('111000000111111000000111')
>>> s[:] = True; all(s), any(s)
(True, True)
"""
def __init__(self, *args, **kwargs):
super(SparseBitArray, self).__init__(*args, **kwargs)
def _initialize_structures(self):
self.changes = []
self.cached_ones = None
def all(self):
self.normalize()
return True if self.changes == [0, self.length] else False
def none(self):
self.normalize()
return True if self.changes == [] else False
def normalize(self):
"""Changes self.changes to the canonical representation
>>> s = SparseBitArray(iterable='00000000000000000000')
>>> s.changes = [0,0,0,0,20,20]
>>> s.normalize()
>>> s.changes
[]
"""
if len(set(self.changes)) == len(self.changes):
return
i = 0
while True:
if i >= len(self.changes) - 1:
break
if self.changes[i] == self.changes[i+1]:
del self.changes[i]
del self.changes[i]
else:
i += 1
def index(self, x):
"""Return index of element or raise ValueError if not found
>>> s = SparseBitArray(iterable='0101')
>>> s.index(0)
0
>>> s.index(1)
1
"""
if x and self.none():
raise ValueError('no set bits in array')
if (not x) and self.all():
raise ValueError('no unset bits in array')
x = bool(x)
if x:
return self.changes[0]
else:
if self.changes and self.changes[0] == 0:
return self.changes[1]
else:
return 0
raise Exception('Logic Error')
def count(self, x):
"""counts true or false values
>>> s = SparseBitArray(iterable=[1,0,0,1], scale=3, repetitions=2); s
SparseBitArray('111000000111111000000111')
>>> s.count(1)
12
>>> s.count(0)
12
>>> s[3:5] = 1; s.count(1)
14
>>> s.all(), s.none()
(False, False)
>>> s[:] = True; s.all()
True
>>> s[:] = False; s.none()
True
"""
if self.cached_ones is None:
ones = 0
last_one = None
for i in self.changes:
if last_one is None:
last_one = i
else:
ones += i - last_one
last_one = None
self.cached_ones = ones
if x:
return self.cached_ones
else:
return self.length - self.cached_ones
def __repr__(self):
s = ''.join(str(int(x)) for x in self)
return 'SparseBitArray(\'%s\')' % s
def __getitem__(self, key):
"""Get a slice or the value of an entry
>>> s = SparseBitArray(20); s[2:6] = True; s
SparseBitArray('00111100000000000000')
>>> s[2:6]
SparseBitArray('1111')
>>> s[4:10]
SparseBitArray('110000')
>>> s[9:14] = True; s[17:19] = True; s
SparseBitArray('00111100011111000110')
>>> s[2:18]
SparseBitArray('1111000111110001')
>>> s[1], s[10]
(False, True)
"""
if isinstance(key, slice):
start, end = self._decode_slice(key)
start_index, end_index = self._indices(start, end)
result = SparseBitArray(end - start)
if self[start]:
result.changes.append(0)
result.changes.extend( change - start for change in self.changes[start_index:end_index] )
return result
else:
if key >= len(self):
raise IndexError(key)
return bool(bisect.bisect_right(self.changes, key) % 2)
def __setitem__(self, key, value):
"""Sets item or slice to True or False
>>> s = SparseBitArray(20); s[2:6] = True; s
SparseBitArray('00111100000000000000')
>>> s[4:10] = True; s
SparseBitArray('00111111110000000000')
>>> s[3:11] = False; s
SparseBitArray('00100000000000000000')
>>> s[4:14] = True; s
SparseBitArray('00101111111111000000')
>>> s[8:11] = False; s
SparseBitArray('00101111000111000000')
>>> s[5:7] = True; s
SparseBitArray('00101111000111000000')
>>> s[15:18] = False; s
SparseBitArray('00101111000111000000')
>>> s[14:16] = False; s
SparseBitArray('00101111000111000000')
>>> s[14:16] = True; s
SparseBitArray('00101111000111110000')
>>> s[4:] = True; s
SparseBitArray('00101111111111111111')
>>> s[:10] = False; s
SparseBitArray('00000000001111111111')
>>> s[:] = False; s
SparseBitArray('00000000000000000000')
>>> s[:] = True; s
SparseBitArray('11111111111111111111')
"""
self.cached_ones = None
value = bool(value)
if isinstance(key, slice):
start, end = self._decode_slice(key)
start_index, end_index = self._indices(start, end)
new_changes = self.changes[:start_index]
if bool(len(new_changes) % 2) != value:
new_changes.append(start)
if bool(end_index % 2) != value:
new_changes.append(end)
new_changes.extend(self.changes[end_index:])
self.changes = new_changes
else:
raise ValueError("Single element assignment not allowed")
def __invert__(self):
"""bitwise inverse
>>> a = SparseBitArray(20); a[4:6] = True; a[10:16] = True;
>>> ~a
SparseBitArray('11110011110000001111')
>>> a = SparseBitArray(20); a[:] = True
>>> ~a
SparseBitArray('00000000000000000000')
"""
new = SparseBitArray(self.length)
new.changes = self.changes
new.changes.append(self.length)
new.changes.insert(0, 0)
new.normalize()
return new
def __and__(self, other):
"""
>>> a = SparseBitArray(20); a[4:6] = True; a[10:16] = True;
>>> b = SparseBitArray(20); b[5:9] = True; b[11:18] = True;
>>> a; b; a & b
SparseBitArray('00001100001111110000')
SparseBitArray('00000111100111111100')
SparseBitArray('00000100000111110000')
"""
self_index = 0
other_index = 0
new = SparseBitArray(self.length)
state = False
while True:
self_num = self.changes[self_index] if self_index < len(self.changes) else sys.maxint
other_num = other.changes[other_index] if other_index < len(other.changes) else sys.maxint
if self_num == other_num == sys.maxint:
break
if self_num <= other_num:
cur = self_num
self_index += 1
if self_num == other_num:
continue
else:
cur = other_num
other_index += 1
if bool(self_index % 2) and bool(other_index % 2) and not state:
new.changes.append(cur)
state = True
elif state and not bool(self_index % 2 or not bool(other_index % 2)):
new.changes.append(cur)
state = False
return new
if __name__ == '__main__':
import doctest
print doctest.testmod()