1
1
# This file is a part of Julia. License is MIT: http://julialang.org/license
2
2
3
- immutable IntSet <: AbstractSet{Int}
3
+ immutable PositiveIntSet <: AbstractSet{Int}
4
4
bits:: BitVector
5
- IntSet () = new (falses (256 ))
5
+ PositiveIntSet () = new (falses (256 ))
6
6
end
7
- IntSet (itr) = union! (IntSet (), itr)
7
+ PositiveIntSet (itr) = union! (PositiveIntSet (), itr)
8
8
9
- eltype (:: Type{IntSet } ) = Int
10
- similar (s:: IntSet ) = IntSet ()
11
- copy (s1:: IntSet ) = copy! (IntSet (), s1)
12
- function copy! (dest:: IntSet , src:: IntSet )
9
+ eltype (:: Type{PositiveIntSet } ) = Int
10
+ similar (s:: PositiveIntSet ) = PositiveIntSet ()
11
+ copy (s1:: PositiveIntSet ) = copy! (PositiveIntSet (), s1)
12
+ function copy! (dest:: PositiveIntSet , src:: PositiveIntSet )
13
13
resize! (dest. bits, length (src. bits))
14
14
copy! (dest. bits, src. bits)
15
15
dest
16
16
end
17
- eltype (s:: IntSet ) = Int
18
- sizehint! (s:: IntSet , n:: Integer ) = (_resize0! (s. bits, max (n, length (s. bits))); s)
17
+ eltype (s:: PositiveIntSet ) = Int
18
+ sizehint! (s:: PositiveIntSet , n:: Integer ) = (_resize0! (s. bits, max (n, length (s. bits))); s)
19
19
20
20
# An internal function for setting the inclusion bit for a given integer n >= 0
21
- @inline function _setint! (s:: IntSet , idx:: Integer , b:: Bool )
21
+ @inline function _setint! (s:: PositiveIntSet , idx:: Integer , b:: Bool )
22
22
if idx > length (s. bits)
23
23
b || return s # setting a bit to zero outside the set's bits is a no-op
24
24
newlen = idx + idx>> 1 # This operation may overflow; we want saturation
@@ -52,7 +52,7 @@ function _matched_map!(f, b1::BitArray, b2::BitArray)
52
52
resize! (b1, l2)
53
53
map! (f, b1, b1, b2)
54
54
else
55
- # We transiently extend b2 — as IntSet internal storage this is unobservable
55
+ # We transiently extend b2 — as PositiveIntSet internal storage this is unobservable
56
56
_resize0! (b2, l1)
57
57
map! (f, b1, b1, b2)
58
58
resize! (b2, l2)
@@ -61,96 +61,96 @@ function _matched_map!(f, b1::BitArray, b2::BitArray)
61
61
b1
62
62
end
63
63
64
- @noinline _throw_intset_bounds_err () = throw (ArgumentError (" elements of IntSet must be between 1 and typemax(Int)" ))
64
+ @noinline _throw_intset_bounds_err () = throw (ArgumentError (" elements of PositiveIntSet must be between 1 and typemax(Int)" ))
65
65
@noinline _throw_keyerror (n) = throw (KeyError (n))
66
66
67
- @inline function push! (s:: IntSet , n:: Integer )
67
+ @inline function push! (s:: PositiveIntSet , n:: Integer )
68
68
0 < n <= typemax (Int) || _throw_intset_bounds_err ()
69
69
_setint! (s, n, true )
70
70
end
71
- push! (s:: IntSet , ns:: Integer... ) = (for n in ns; push! (s, n); end ; s)
71
+ push! (s:: PositiveIntSet , ns:: Integer... ) = (for n in ns; push! (s, n); end ; s)
72
72
73
- @inline function pop! (s:: IntSet )
73
+ @inline function pop! (s:: PositiveIntSet )
74
74
pop! (s, last (s))
75
75
end
76
- @inline function pop! (s:: IntSet , n:: Integer )
76
+ @inline function pop! (s:: PositiveIntSet , n:: Integer )
77
77
n in s ? (_delete! (s, n); n) : _throw_keyerror (n)
78
78
end
79
- @inline function pop! (s:: IntSet , n:: Integer , default)
79
+ @inline function pop! (s:: PositiveIntSet , n:: Integer , default)
80
80
n in s ? (_delete! (s, n); n) : default
81
81
end
82
- @inline _delete! (s:: IntSet , n:: Integer ) = _setint! (s, n, false )
83
- @inline delete! (s:: IntSet , n:: Integer ) = n < 0 ? s : _delete! (s, n)
84
- shift! (s:: IntSet ) = pop! (s, first (s))
82
+ @inline _delete! (s:: PositiveIntSet , n:: Integer ) = _setint! (s, n, false )
83
+ @inline delete! (s:: PositiveIntSet , n:: Integer ) = n < 0 ? s : _delete! (s, n)
84
+ shift! (s:: PositiveIntSet ) = pop! (s, first (s))
85
85
86
- empty! (s:: IntSet ) = (fill! (s. bits, false ); s)
87
- isempty (s:: IntSet ) = ! any (s. bits)
86
+ empty! (s:: PositiveIntSet ) = (fill! (s. bits, false ); s)
87
+ isempty (s:: PositiveIntSet ) = ! any (s. bits)
88
88
89
89
# Mathematical set functions: union!, intersect!, setdiff!, symdiff!
90
90
91
- union (s:: IntSet ) = copy (s)
92
- union (s1:: IntSet , s2:: IntSet ) = union! (copy (s1), s2)
93
- union (s1:: IntSet , ss:: IntSet ... ) = union (s1, union (ss... ))
94
- union (s:: IntSet , ns) = union! (copy (s), ns)
95
- union! (s:: IntSet , ns) = (for n in ns; push! (s, n); end ; s)
96
- function union! (s1:: IntSet , s2:: IntSet )
91
+ union (s:: PositiveIntSet ) = copy (s)
92
+ union (s1:: PositiveIntSet , s2:: PositiveIntSet ) = union! (copy (s1), s2)
93
+ union (s1:: PositiveIntSet , ss:: PositiveIntSet ... ) = union (s1, union (ss... ))
94
+ union (s:: PositiveIntSet , ns) = union! (copy (s), ns)
95
+ union! (s:: PositiveIntSet , ns) = (for n in ns; push! (s, n); end ; s)
96
+ function union! (s1:: PositiveIntSet , s2:: PositiveIntSet )
97
97
_matched_map! (| , s1. bits, s2. bits)
98
98
s1
99
99
end
100
100
101
- intersect (s1:: IntSet ) = copy (s1)
102
- intersect (s1:: IntSet , ss:: IntSet ... ) = intersect (s1, intersect (ss... ))
103
- function intersect (s1:: IntSet , ns)
104
- s = IntSet ()
101
+ intersect (s1:: PositiveIntSet ) = copy (s1)
102
+ intersect (s1:: PositiveIntSet , ss:: PositiveIntSet ... ) = intersect (s1, intersect (ss... ))
103
+ function intersect (s1:: PositiveIntSet , ns)
104
+ s = PositiveIntSet ()
105
105
for n in ns
106
106
n in s1 && push! (s, n)
107
107
end
108
108
s
109
109
end
110
- intersect (s1:: IntSet , s2:: IntSet ) =
110
+ intersect (s1:: PositiveIntSet , s2:: PositiveIntSet ) =
111
111
(length (s1. bits) >= length (s2. bits) ? intersect! (copy (s1), s2) : intersect! (copy (s2), s1))
112
112
"""
113
- intersect!(s1::IntSet , s2::IntSet )
113
+ intersect!(s1::PositiveIntSet , s2::PositiveIntSet )
114
114
115
115
Intersects sets `s1` and `s2` and overwrites the set `s1` with the result. If needed, `s1`
116
116
will be expanded to the size of `s2`.
117
117
"""
118
- function intersect! (s1:: IntSet , s2:: IntSet )
118
+ function intersect! (s1:: PositiveIntSet , s2:: PositiveIntSet )
119
119
_matched_map! (& , s1. bits, s2. bits)
120
120
s1
121
121
end
122
122
123
- setdiff (s:: IntSet , ns) = setdiff! (copy (s), ns)
124
- setdiff! (s:: IntSet , ns) = (for n in ns; _delete! (s, n); end ; s)
125
- function setdiff! (s1:: IntSet , s2:: IntSet )
123
+ setdiff (s:: PositiveIntSet , ns) = setdiff! (copy (s), ns)
124
+ setdiff! (s:: PositiveIntSet , ns) = (for n in ns; _delete! (s, n); end ; s)
125
+ function setdiff! (s1:: PositiveIntSet , s2:: PositiveIntSet )
126
126
_matched_map! (> , s1. bits, s2. bits)
127
127
s1
128
128
end
129
129
130
- symdiff (s:: IntSet , ns) = symdiff! (copy (s), ns)
130
+ symdiff (s:: PositiveIntSet , ns) = symdiff! (copy (s), ns)
131
131
"""
132
132
symdiff!(s, itr)
133
133
134
134
For each element in `itr`, destructively toggle its inclusion in set `s`.
135
135
"""
136
- symdiff! (s:: IntSet , ns) = (for n in ns; symdiff! (s, n); end ; s)
136
+ symdiff! (s:: PositiveIntSet , ns) = (for n in ns; symdiff! (s, n); end ; s)
137
137
"""
138
138
symdiff!(s, n)
139
139
140
140
The set `s` is destructively modified to toggle the inclusion of integer `n`.
141
141
"""
142
- function symdiff! (s:: IntSet , n:: Integer )
142
+ function symdiff! (s:: PositiveIntSet , n:: Integer )
143
143
0 <= n < typemax (Int) || _throw_intset_bounds_err ()
144
144
val = ! (n in s)
145
145
_setint! (s, n, val)
146
146
s
147
147
end
148
- function symdiff! (s1:: IntSet , s2:: IntSet )
148
+ function symdiff! (s1:: PositiveIntSet , s2:: PositiveIntSet )
149
149
_matched_map! (xor, s1. bits, s2. bits)
150
150
s1
151
151
end
152
152
153
- @inline function in (n:: Integer , s:: IntSet )
153
+ @inline function in (n:: Integer , s:: PositiveIntSet )
154
154
if 1 <= n <= length (s. bits)
155
155
@inbounds b = s. bits[n]
156
156
else
@@ -160,24 +160,24 @@ end
160
160
end
161
161
162
162
# Use the next-set index as the state to prevent looking it up again in done
163
- start (s:: IntSet ) = next (s, 0 )[2 ]
164
- function next (s:: IntSet , i)
163
+ start (s:: PositiveIntSet ) = next (s, 0 )[2 ]
164
+ function next (s:: PositiveIntSet , i)
165
165
nextidx = i == typemax (Int) ? 0 : findnext (s. bits, i+ 1 )
166
166
(i, nextidx)
167
167
end
168
- done (s:: IntSet , i) = i <= 0
168
+ done (s:: PositiveIntSet , i) = i <= 0
169
169
170
170
171
171
@noinline _throw_intset_notempty_error () = throw (ArgumentError (" collection must be non-empty" ))
172
- function last (s:: IntSet )
172
+ function last (s:: PositiveIntSet )
173
173
idx = findprev (s. bits, length (s. bits))
174
174
idx == 0 ? _throw_intset_notempty_error () : idx
175
175
end
176
176
177
- length (s:: IntSet ) = sum (s. bits)
177
+ length (s:: PositiveIntSet ) = sum (s. bits)
178
178
179
- function show (io:: IO , s:: IntSet )
180
- print (io, " IntSet ([" )
179
+ function show (io:: IO , s:: PositiveIntSet )
180
+ print (io, " PositiveIntSet ([" )
181
181
first = true
182
182
for n in s
183
183
! first && print (io, " , " )
@@ -187,7 +187,7 @@ function show(io::IO, s::IntSet)
187
187
print (io, " ])" )
188
188
end
189
189
190
- function == (s1:: IntSet , s2:: IntSet )
190
+ function == (s1:: PositiveIntSet , s2:: PositiveIntSet )
191
191
l1 = length (s1. bits)
192
192
l2 = length (s2. bits)
193
193
# If the lengths are the same, simply punt to bitarray comparison
@@ -211,12 +211,12 @@ function ==(s1::IntSet, s2::IntSet)
211
211
return true
212
212
end
213
213
214
- issubset (a:: IntSet , b:: IntSet ) = isequal (a, intersect (a,b))
215
- < (a:: IntSet , b:: IntSet ) = (a<= b) && ! isequal (a,b)
216
- <= (a:: IntSet , b:: IntSet ) = issubset (a, b)
214
+ issubset (a:: PositiveIntSet , b:: PositiveIntSet ) = isequal (a, intersect (a,b))
215
+ < (a:: PositiveIntSet , b:: PositiveIntSet ) = (a<= b) && ! isequal (a,b)
216
+ <= (a:: PositiveIntSet , b:: PositiveIntSet ) = issubset (a, b)
217
217
218
218
const hashis_seed = UInt === UInt64 ? 0x88989f1fc7dea67d : 0xc7dea67d
219
- function hash (s:: IntSet , h:: UInt )
219
+ function hash (s:: PositiveIntSet , h:: UInt )
220
220
h ⊻= hashis_seed
221
221
bc = s. bits. chunks
222
222
i = length (bc)
0 commit comments