/
integerset.rb
executable file
·311 lines (265 loc) · 6.58 KB
/
integerset.rb
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
=begin copyright
reg - the ruby extended grammar
Copyright (C) 2005 Caleb Clausen
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
=end
require "forwardable"
SpaceshipPirate=proc{
alias spaceship__withoutpirates <=>
def <=>(other)
if NumberSet::Part===other
res=other<=>self
res and -res
else
spaceship__withoutpirates other
end
end
}
[Fixnum,Bignum,Float].each{|cl| cl.instance_eval &SpaceshipPirate }
class IntegerSet
extend Forwardable
def initialize(*pieces)
pieces.map {|r| self.insert r}
end
class<<self
alias [] new
end
def_delegator :@pieces, :[]
def insert(num)
mid=high-low/2
huh
end
def ===(num)
@pieces.empty? and return
low,high=0,@pieces.size-1
while low<high
case num <=> @pieces[mid=high-low/2]
when 1; low=mid+1
when 0; return true
when -1; high=mid-1
when nil; return false
else fail "didn't expect anything else from <=>"
end
end
return @pieces[low]===num
end
class Part
def initialize
abstract
end
def ===
abstract
end
def first
abstract
end
def last
abstract
end
def <=>(other)
if Part===other
result=(self<=>other.first)
return(result == (self<=>other.last) and result)
end
if first> other; -1
elsif last < other; 1
elsif self===other; 0
end
#else other's in our range, but not in the bitset, what else to do?
end
end
class Range < Part
include Enumerable
def initialize(first,last=nil,exclude_end=nil)
last or first,last,exclude_end=first.first,first.last,first.exclude_end?
@first,@last,@exclude_end=first,last,exclude_end||nil
end
class <<self; alias [] new; end
attr_reader :first,:last
alias begin first
alias end last
def exclude_end?; @exclude_end end
def ===(num)
lt=@exclude_end && :< || :<=
num>=@first and num.send lt,@last
end
alias member? ===
alias include? ===
def to_s
"#{@first}..#{@exclude_end && "."}#{@last}"
end
alias inspect to_s
def eql?(other)
Range===other||::Range===other and
@first.eql? other.first and
@last.eql? other.last and
@exclude_end==other.exclude_end?
end
def each
item=@first
until item==@last
yield item
item=item.succ!
end
yield item unless @exclude_end
return self
end
def step(skipcnt)
item=@first
cnt=1
until item==@last
if (cnt-=1).zero?
cnt=skipcnt
yield item
end
item=item.succ!
end
yield item unless @exclude_end || cnt!=1
return self
end
end
class Fragment < Part
include Enumerable
private
#only works for integers in the range 0..255
def ffs(i)
#Integer===i and (0..255)===i or raise #hell
return -1 if i.zero?
if (i&0x0F).nonzero?
if (i&0x03).nonzero?
if (i&0x01).nonzero?: 1
else 2
end
else
if (i&0x04).nonzero?: 3
else 4
end
end
else
if (i&0x30).nonzero?
if (i&0x10).nonzero?: 5
else 6
end
else
if (i&0x40).nonzero?: 7
else 8
end
end
end
end
def fls(i)
ffs(
i=(i&0xaa)>>1 | (i&0x55)<<1
i=(i&0xcc)>>2 | (i&0x33)<<2
i=(i&0xF0)>>4 | (i&0x0F)<<4
)
end
public
#base is an Integer, bits is a (bit-)String
attr_reader :base, :bits
def initialize(*nums)
nums.sort!
nums.last-nums.first >= 800 and huh #@bits gets rediculously large
@base=nums.first&~7
@bits="\0"
nums.each{|num| self<<num}
end
def insert(num)
num-=@base
num<0 and huh #change base and prepend null bytes to bits...
i=byteidx(num)
i>@bits.size+50 and huh #lotsa dead space in @bits...
@bits[i] |= (1<<bitidx(num))
return self
end
alias << insert
def delete(num)
num-=@base
num<0 and return #num isn't in this set
i=byteidx(num)
i>=@bits.size and return #num isn't in this set
@bits[i] &= ~(1<<bitidx(num))
i==@bits.size-1 and @bits.sub!(/\x0+$/,'')
if i.zero?
@bits.sub!(/^(\x0+)/,'')
@base+=$1.size*8
end
return self
end
def |(other)
case other
when Integer; return dup<<other
when ::Range; huh
when Range; huh
when Fragment; huh
when Set; #??
result=dup
other.each{|i| result<<i }
else huh
end
huh
end
def &(other)
case other
when Integer; return self===other && other
when ::Range; huh
when Range; huh
when Fragment; huh
when Set; huh#??
else huh
end
huh
end
def ^(other)
case other
when Integer:
result=dup
result===other ? result.delete other : result.insert other
when ::Range: huh
when Range: huh
when Fragment: huh
when Set: huh#??
else huh
end
huh
end
def begin
assert @bits[0].nonzero?
@base+ffs(@bits[0])-1
end
def end
assert fls(@bits[-1]).nonzero?
@base+ (@bits.length-1)*8 + fls(@bits[-1])-1
end
alias first begin
alias last end
def bitidx(num) num&7 end
def byteidx(num) (num&~7)>>3 end
def ===(num)
num-=@base
num<0 and return false
(@bits[byteidx(num)]&(1<<bitidx(num))).nonzero?
end
def each
(0...@bits.size).each{|idx|
bits=@bits[idx]
until bits.zero?
bit=ffs(bits)-1 #ffs not defined yet...
yield @base + idx*8 + bit
bits &= ~(1<<bit)
end
}
return self
end
end
end