forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intset.jl
283 lines (248 loc) · 6.02 KB
/
intset.jl
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
type IntSet
bits::Array{Uint32,1}
limit::Int
fill1s::Bool
IntSet() = new(zeros(Uint32,256>>>5), 256, false)
end
IntSet(args...) = (s=IntSet(); for a in args; add!(s,a); end; s)
similar(s::IntSet) = IntSet()
copy(s::IntSet) = union!(IntSet(), s)
eltype(s::IntSet) = Int64
function sizehint(s::IntSet, top::Integer)
if top >= s.limit
lim = ((top+31) & -32)>>>5
olsz = length(s.bits)
if olsz < lim
resize!(s.bits, lim)
fill = s.fill1s ? uint32(-1) : uint32(0)
for i=(olsz+1):lim; s.bits[i] = fill; end
end
s.limit = top
end
s
end
function add!(s::IntSet, n::Integer)
if n >= s.limit
if s.fill1s
return s
else
lim = int(n + div(n,2))
sizehint(s, lim)
end
end
s.bits[n>>5 + 1] |= (uint32(1)<<(n&31))
return s
end
function union!(s::IntSet, ns)
for n in ns
add!(s, n)
end
return s
end
function delete!(s::IntSet, n::Integer, deflt)
if n >= s.limit
if s.fill1s
lim = int(n + div(n,2))
sizehint(s, lim)
else
return deflt
end
end
mask = uint32(1)<<(n&31)
idx = n>>5 + 1
b = s.bits[idx]
if (b&mask)==0; return deflt; end
s.bits[idx] = b&~mask
return n
end
function delete!(s::IntSet, n::Integer)
if delete!(s, n, n+1) == n+1
throw(KeyError(n))
end
return n
end
function setdiff!(s::IntSet, ns)
for n in ns
delete!(s, n, nothing)
end
return s
end
setdiff(a::IntSet, b::IntSet) = setdiff!(copy(a),b)
symdiff(a::IntSet, b::IntSet) =
(s1.limit >= s2.limit ? symdiff!(copy(s1), s2) : symdiff!(copy(s2), s1))
function empty!(s::IntSet)
s.bits[:] = 0
return s
end
function symdiff!(s::IntSet, n::Integer)
if n >= s.limit
lim = int(n + dim(n,2))
sizehint(s, lim)
end
s.bits[n>>5 + 1] $= (uint32(1)<<(n&31))
return s
end
function symdiff!(s::IntSet, ns)
for n in ns
symdiff!(s, n)
end
return s
end
function copy!(to::IntSet, from::IntSet)
empty!(to)
union!(to, from)
end
function contains(s::IntSet, n::Integer)
if n >= s.limit
# max IntSet length is typemax(Int), so highest possible element is
# typemax(Int)-1
s.fill1s && n >= 0 && n < typemax(Int)
else
(s.bits[n>>5 + 1] & (uint32(1)<<(n&31))) != 0
end
end
start(s::IntSet) = int64(0)
done(s::IntSet, i) = (!s.fill1s && next(s,i)[1] >= s.limit) || i == typemax(Int)
function next(s::IntSet, i)
if i >= s.limit
n = int64(i)
else
n = int64(ccall(:bitvector_next, Uint64, (Ptr{Uint32}, Uint64, Uint64), s.bits, i, s.limit))
end
(n, n+1)
end
isempty(s::IntSet) =
!s.fill1s && ccall(:bitvector_any1, Uint32, (Ptr{Uint32}, Uint64, Uint64), s.bits, 0, s.limit)==0
function first(s::IntSet)
n = next(s,0)[1]
if n >= s.limit
error("first: set is empty")
end
return n
end
shift!(s::IntSet) = delete!(s, first(s))
function last(s::IntSet)
if !s.fill1s
for i = length(s.bits):-1:1
w = s.bits[i]
if w != 0
return (i-1)<<5 + (31-leading_zeros(w))
end
end
end
error("last: set has no last element")
end
pop!(s::IntSet) = delete!(s, last(s))
length(s::IntSet) = int(ccall(:bitvector_count, Uint64, (Ptr{Uint32}, Uint64, Uint64), s.bits, 0, s.limit)) +
(s.fill1s ? typemax(Int) - s.limit : 0)
function show(io::IO, s::IntSet)
print(io, "IntSet(")
first = true
for n in s
if n > s.limit
break
end
if !first
print(io, ", ")
end
print(io, n)
first = false
end
if s.fill1s
print(io, ", ..., ", typemax(Int)-1, ")")
else
print(io, ")")
end
end
# Math functions
function union!(s::IntSet, s2::IntSet)
if s2.limit > s.limit
sizehint(s, s2.limit)
end
lim = length(s2.bits)
for n = 1:lim
s.bits[n] |= s2.bits[n]
end
if s2.fill1s
for n=lim+1:length(s.bits)
s.bits[n] = uint32(-1)
end
end
s.fill1s |= s2.fill1s
s
end
union(s1::IntSet) = copy(s1)
union(s1::IntSet, s2::IntSet) = (s1.limit >= s2.limit ? union!(copy(s1), s2) : union!(copy(s2), s1))
union(s1::IntSet, ss::IntSet...) = union(s1, union(ss...))
function intersect!(s::IntSet, s2::IntSet)
if s2.limit > s.limit
sizehint(s, s2.limit)
end
lim = length(s2.bits)
for n = 1:lim
s.bits[n] &= s2.bits[n]
end
if !s2.fill1s
for n=lim+1:length(s.bits)
s.bits[n] = uint32(0)
end
end
s.fill1s &= s2.fill1s
s
end
intersect(s1::IntSet) = copy(s1)
intersect(s1::IntSet, s2::IntSet) =
(s1.limit >= s2.limit ? intersect!(copy(s1), s2) : intersect!(copy(s2), s1))
intersect(s1::IntSet, ss::IntSet...) = intersect(s1, intersect(ss...))
function complement!(s::IntSet)
for n = 1:length(s.bits)
s.bits[n] = ~s.bits[n]
end
s.fill1s = !s.fill1s
s
end
complement(s::IntSet) = complement!(copy(s))
function symdiff!(s::IntSet, s2::IntSet)
if s2.limit > s.limit
sizehint(s, s2.limit)
end
lim = length(s2.bits)
for n = 1:lim
s.bits[n] $= s2.bits[n]
end
if s2.fill1s
for n=lim+1:length(s.bits)
s.bits[n] = ~s.bits[n]
end
end
s.fill1s $= s2.fill1s
s
end
function isequal(s1::IntSet, s2::IntSet)
if s1.fill1s != s2.fill1s
return false
end
lim1 = length(s1.bits)
lim2 = length(s2.bits)
for i = 1:min(lim1,lim2)
if s1.bits[i] != s2.bits[i]
return false
end
end
filln = s1.fill1s ? uint32(-1) : uint32(0)
if lim1 > lim2
for i = lim2:lim1
if s1.bits[i] != filln
return false
end
end
else
for i = lim1:lim2
if s2.bits[i] != filln
return false
end
end
end
return true
end
issubset(a::IntSet, b::IntSet) = isequal(a, intersect(a,b))