-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfastset.py
145 lines (125 loc) · 3.34 KB
/
fastset.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
import numpy as np
import numba
from itertools import chain, zip_longest
def tree_reduce(func, iterable):
maybereduce = lambda a,b: func(a,b) if b is not None else a
it = iter(iterable)
pairs = zip_longest(it, it)
peek = next(pairs)
if peek[1] is None:
return peek[0]
resume = (maybereduce(a,b) for a,b in chain([peek], pairs))
peek = None
return tree_reduce(func, resume)
@numba.njit
def set_union(a, b):
if a.size == 0:
return b
elif b.size == 0:
return a
out = np.empty(a.size+b.size, dtype=a.dtype)
ia = 0
ib = 0
iout = 0
while ia < a.size and ib < b.size:
if a[ia] < b[ib]:
out[iout] = a[ia]
iout += 1
ia += 1
elif a[ia] == b[ib]:
out[iout] = a[ia]
iout += 1
ia += 1
ib += 1
elif a[ia] > b[ib]:
out[iout] = b[ib]
iout += 1
ib += 1
while ia < a.size:
out[iout] = a[ia]
iout += 1
ia += 1
while ib < b.size:
out[iout] = b[ib]
iout += 1
ib += 1
return out[:iout]
@numba.njit
def set_difference(a, b):
if a.size == 0 or b.size == 0:
return a
out = np.empty(a.size, dtype=a.dtype)
ia = 0
ib = 0
iout = 0
while ia < a.size and ib < b.size:
if a[ia] < b[ib]:
out[iout] = a[ia]
iout += 1
ia += 1
elif a[ia] == b[ib]:
ia += 1
ib += 1
elif a[ia] > b[ib]:
ib += 1
while ia < a.size:
out[iout] = a[ia]
iout += 1
ia += 1
return out[:iout]
@numba.njit
def set_intersection(a, b):
if a.size == 0:
return a
elif b.size == 0:
return b
out = np.empty(np.minimum(a.size, b.size), dtype=a.dtype)
ia = 0
ib = 0
iout = 0
while ia < a.size and ib < b.size:
if a[ia] < b[ib]:
ia += 1
elif a[ia] == b[ib]:
out[iout] = a[ia]
iout += 1
ia += 1
ib += 1
elif a[ia] > b[ib]:
ib += 1
return out[:iout]
class FastSet(object):
def __init__(self, set_in=None, dtype='i8'):
if set_in is None:
self._set = np.array([], dtype=dtype)
elif isinstance(set_in, FastSet):
self._set = set_in._set.astype(dtype)
else:
self._set = np.fromiter(set_in, dtype=dtype, count=len(set_in))
self._set.sort()
def __len__(self):
return self._set.size
def __str__(self):
return str(self._set)
def __iter__(self):
return iter(self._set)
def union(self, other):
sout = FastSet(dtype=self._set.dtype)
sout._set = set_union(self._set, other._set)
return sout
def difference(self, other):
sout = FastSet(dtype=self._set.dtype)
sout._set = set_difference(self._set, other._set)
return sout
def intersection(self, other):
sout = FastSet(dtype=self._set.dtype)
sout._set = set_intersection(self._set, other._set)
return sout
def __add__(self, other):
return self.union(other)
def __sub__(self, other):
return self.difference(other)
def __mul__(self, other):
return self.intersection(other)
def set(self):
return set(self._set)