-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_array.rb
More file actions
300 lines (267 loc) · 6.12 KB
/
multi_array.rb
File metadata and controls
300 lines (267 loc) · 6.12 KB
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
class MultiArray
include Enumerable
def initialize(arg=nil, default=nil)
if arg.is_a? Integer
@b = [default]
@e = [arg]
elsif arg.is_a? MultiArray
@b = arg._b.dup
@e = arg._e.dup
else
@b = []
@e = []
if arg.respond_to? :to_hash
arg.to_hash.each do |b, e|
if e > 0
@b << b
@e << (@e[-1] || 0) + e
end
end
elsif arg.is_a? Enumerable
arg.each{|x| self << x}
end
end
end
class << self
forward :[], :new
def _create(b, e)
x = allocate
x._set(b, e)
x
end
end
def inspect
"[#{each_run.map{|b, e| "#{b.inspect}#{" × #{e.inspect}" if e > 1}"}.join(', ')}]"
end
forward :to_s, :inspect
def _b
@b
end
def _e
@e
end
def _idx(i)
@e.bsearch_index{|s| i < s }
end
def _set(b, e)
@b = b
@e = e
end
def hash
[@b, @e].hash
end
def eql?(x)
x.is_a?(MultiArray) && @b.eql?(x._b) && @e.eql?(x._e)
end
forward :==, :eql?
def matches?(seq)
if seq.is_a?(MultiArray)
@b.eql?(seq._b) && @e.eql?(seq._e)
else
en = seq.each
all? do |x|
y = begin en.next
rescue StopIteration
return false
end
x.eql?(y)
end
end
end
def empty?
@e.empty?
end
def size
if @e.empty?
0
else
@e[-1]
end
end
def first
@b[0]
end
def last
@b[-1]
end
def to_a
each.to_a
end
forward :to_ary, :to_a
def each_factor
e0 = 0
@b.size.times do |i|
e1 = @e[i]
yield @b[i], e1-e0
e0 = e1
end
end
enum_method :each_run
def each
e0 = 0
@b.size.times do |i|
b = @b[i]
e1 = @e[i]
(e1-e0).times do
yield b
end
e0 = e1
end
end
enum_method :each
def map_factors(&block)
bs = []
es = []
each_factor do |b, e|
b, e = block[b, e]
if e > 0
if !bs.empty? && bs[-1].eql?(b)
es[-1] += e
else
bs << b
es << (es[-1] || 0) + e
end
end
end
self.class._create(bs, es)
end
enum_method :map_factors
def map(&block)
self.class._create(@b.map(&block), @e.dup)
end
enum_method :map
def select(&block)
bs = []
es = []
each_factor do |b, e|
if block[b]
bs << b
es << (es[-1] || 0) + e
end
end
self.class._create(bs, es)
end
def reject(&block)
bs = []
es = []
each_factor do |b, e|
unless block[b]
bs << b
es << (es[-1] || 0) + e
end
end
self.class._create(bs, es)
end
def zip(*seqs, &block)
case seqs.size
when 0
each(&block)
when 1
en = seqs[0].enum_for(:each)
each do |a|
block[a, en.try_next]
end
else
ens = seqs.map{|seq| seq.enum_for(:each) }
each do |a|
block[a, *ens.map(&:try_next)]
end
end
end
enum_method :zip
def _sub(a, z)
z = z.min(size)
if a == z
self.class._create([], [])
elsif ia = _idx(a)
iz = _idx(z-1) || @b.size-1
r = ia..iz
b = @b[r]
e = r.map{|k| @e[k] - a }
e[-1] = z-a
self.class._create(b, e)
end
end
def take(n)
n >= 0 or raise ArgumentError, "attempt to take negative size"
_sub(0, n)
end
def drop(n)
n >= 0 or raise ArgumentError, "attempt to drop negative size"
_sub(n, size)
end
def [](i, j=nil)
if !j.nil?
if j > 0
_sub(i, i+j)
elsif j == 0
self.class._create([], [])
end
elsif i.is_a? Range
z = i.end
z += size if z < 0
if i.exclude_end?
_sub(i.begin, z)
else
_sub(i.begin, z+1)
end
elsif i < 0
self[size + i]
else
j = _idx(i) and @b[j]
end
end
forward :slice, :[]
def <<(x)
if !@b.empty? && @b[-1].eql?(x)
@e[-1] += 1
else
@b << x
@e << (@e[-1] || 0) + 1
end
self
end
def push(*xs)
xs.each{|x| self << x }
end
def pushn(x, n)
if n > 0
if !@b.empty? && @b[-1].eql?(x)
@e[-1] += n
else
@b << x
@e << n
end
end
self
end
def concat(a)
if a.respond_to? :to_hash
a.to_hash.each do |x, n|
pushn(x, n)
end
elsif a.is_a? MultiArray
if empty?
@b = a._b.dup
@e = a._e.dup
elsif !a.empty?
s = size
if @b[-1].eql? a._b[0]
@e[-1] += a._e[0]
@b.concat(a._b[1..-1])
@e.concat(a._e[1..-1].map{|k| s + k })
else
@b.concat(a._b)
@e.concat(a._e.map{|k| s + k })
end
end
elsif a.respond_to? :to_ary
a.to_ary.each do |x|
self << x
end
else
raise TypeError, "no implicit conversion of #{a.class} into Array"
end
self
end
end