Class: Sequence::List
Defined Under Namespace
Modules: Overlaid
Constant Summary
Constants inherited
from Sequence
SubSequence, VERSION
Instance Attribute Summary collapse
Attributes inherited from Sequence
#last_match, #maxmatchlen
Instance Method Summary
collapse
Methods inherited from Sequence
#-, #<<, #[], [], #[]=, #_adjust_pos_on_change, #_default_maxmatchlen, #_delete_position, #_normalize_pos, #_parse_slice_args, #all_data, #begin, #begin!, #check, #check_until, #checkback, #checkback_until, #close, #closed?, #delete, #each, #empty?, #end, #end!, #exist?, #existback?, #first, #goto, #holding_position, #holding_position!, #holding_position?, #insert, #last, #length, #match?, #matchback?, #more_data?, #move, #move!, #nearbegin, #nearend, #new, #notify_change, #on_change_notify, #original__new, #overwrite, #pop, #pos=, #pos?, #position, #position=, #position?, #pred, #prop, #read!, #read1, #readahead1, #readback, #readback1, #readbehind, #readbehind1, #rest_size, #reversed, #shift, #skip, #skip_literal, #skip_until, #skip_until_literal, #skipback, #skipback_until, #slice, #slice!, #slice1, #slice1!, #subseq, #succ, #to_sequence, #was_data?, #write, #writeahead, #writeback, #writebehind
Constructor Details
#initialize(seqs) ⇒ List
Returns a new instance of List.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/sequence/list.rb', line 6
def initialize(seqs)
seqs.empty? and raise( ArgumentError, 'empty List not allowed' )
@list=seqs
@current_idx=0
@pos=0
@start_pos=[0]
@list=@list.inject([]){|li,seq|
seq=seq.to_sequence
case seq
when Circular; raise ArgumentError, 'no circular seqs in lists'
when List; li+seq.list
else li<<seq
end
}
@list.each{|seq| seq.on_change_notify(self) }
_rebuild_idxs
extend @list.first.like
end
|
Instance Attribute Details
#list ⇒ Object
Returns the value of attribute list.
27
28
29
|
# File 'lib/sequence/list.rb', line 27
def list
@list
end
|
#pos ⇒ Object
Returns the value of attribute pos.
141
142
143
|
# File 'lib/sequence/list.rb', line 141
def pos
@pos
end
|
Instance Method Details
#+(other) ⇒ Object
157
158
159
160
|
# File 'lib/sequence/list.rb', line 157
def + other
return super unless ::Sequence===other
return List[*@list+[other]]
end
|
#_fragment_discard_after(pos) ⇒ Object
162
163
164
165
166
167
|
# File 'lib/sequence/list.rb', line 162
def _fragment_discard_after(pos)
idx=_lookup_idx(pos)
pos-=@start_pos[idx]
@list[idx]=@list[idx].subseq(0...pos)
return idx
end
|
#_fragment_discard_before(pos) ⇒ Object
169
170
171
172
173
174
|
# File 'lib/sequence/list.rb', line 169
def _fragment_discard_before(pos)
idx=_lookup_idx(pos)
pos-=@start_pos[idx]
@list[idx]=@list[idx].subseq(pos..-1)
return idx
end
|
#_lookup_idx(pos) ⇒ Object
def _lookup_idx(pos)
low=0;high=@start_pos.size-1
while(high>low+1)
mid=(low+high)/2
this_pos,next_pos=*@start_pos[mid,2]
if pos<this_pos
high=mid # - 1 ??
elsif pos<next_pos
return mid
elsif pos==next_pos
return mid+1
else
low=mid + 1
end
end
low
end
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/sequence/list.rb', line 70
def _lookup_idx(pos)
pos==size and return @list.size-1
assert((0...size)===pos)
assert @start_pos.size==@list.size+1
low=0;high=@start_pos.size-1
assert @start_pos[low]<=pos
assert @start_pos[high]>pos
while(high>low+1)
assert @start_pos[low]<=pos
assert @start_pos[high]>pos
mid=(low+high)/2
case pos<=>@start_pos[mid]
when -1; high=mid
when 0; break low=mid
when 1; low=mid
end
end
assert @start_pos[low]<=pos
assert @start_pos[low+1]>pos
low
end
|
#_pos=(pos) ⇒ Object
143
144
145
146
147
|
# File 'lib/sequence/list.rb', line 143
def _pos=pos
@pos=pos
assert((0..size)===pos)
@current_idx= _lookup_idx(pos)
end
|
#_rebuild_idxs(start = 1) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/sequence/list.rb', line 40
def _rebuild_idxs(start=1)
seed=@start_pos[0...start]
seed.empty? and seed=[0]
start==0 and start=1
@start_pos=(start..@list.size).inject(seed){|arr,i|
arr<<arr.last+@list[i-1].size
}
end
|
#append(data) ⇒ Object
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/sequence/list.rb', line 274
def append(data)
if @list.last.overlaid?
@list.last.append data
return self
end
data=data.dup.to_sequence
data.extend Overlaid
@list<<data
@start_pos<<@start_pos.last+data.size
notify_change(self,@start_pos[-2], 0, data.size)
self
end
|
#change_notification(cu, first, oldsize, newsize) ⇒ Object
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/sequence/list.rb', line 29
def change_notification(cu,first,oldsize,newsize)
idx=@list.each_with_index{|item,i| cu.equal? item and break i}
diff=newsize-oldsize
(idx+1...@start_pos.size).each{|i|
@start_pos[i]+=diff
}
@pos=_adjust_pos_on_change(@pos, first,oldsize,newsize)
@current_idx=_lookup_idx @pos
notify_change(self,@start_pos[idx]+first,oldsize,newsize)
end
|
#eof? ⇒ Boolean
153
154
155
|
# File 'lib/sequence/list.rb', line 153
def eof?
@pos>=size
end
|
#holding ⇒ Object
111
112
113
114
115
116
117
118
|
# File 'lib/sequence/list.rb', line 111
def holding
oldpos,oldidx=@pos,@current_idx
begin
yield self
ensure
@pos,@current_idx=oldpos,oldidx
end
end
|
#holding!(&block) ⇒ Object
like #holding, but block is instance_eval’d in the seq.
132
133
134
135
136
137
138
139
|
# File 'lib/sequence/list.rb', line 132
def holding! &block
oldpos,oldidx=@pos,@current_idx
begin
instance_eval self, &block
ensure
@pos,@current_idx=oldpos,oldidx
end
end
|
#holding? ⇒ Boolean
like #holding, but position is reset only if block returns false or nil (or raises an exception).
122
123
124
125
126
127
128
129
|
# File 'lib/sequence/list.rb', line 122
def holding?
oldpos,oldidx=@pos,@current_idx
begin
result=yield self
ensure
(@pos,@current_idx=oldpos,oldidx) unless result
end
end
|
#modify(*args) ⇒ Object
Overlaid =proc do
class<<self
def overlaid?; true end
def subseq(*)
result=super
result.instance_eval(& Overlaid)
return result
end
end
end
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
|
# File 'lib/sequence/list.rb', line 195
def modify(*args)
result=repldata=args.pop
repllen=repldata.size
repldata=repldata.dup.to_sequence
repldata.extend Overlaid
replseqs=[repldata]
first,len,only1=_parse_slice_args(*args)
if len.zero? and repllen.zero?
notify_change self,first,len,repllen
return result
end
f_idx=first.zero?? 0 : _lookup_idx(first-1)
last_item_i=first+len-1
last_item_i=0 if last_item_i<0
l_idx=_lookup_idx(last_item_i)
assert f_idx <= l_idx
fragrest_f=first-@start_pos[i=_lookup_idx(first)]
fragrest_l=first+len-@start_pos[l_idx]
assert fragrest_f <= @list[i].size
if fragrest_f.nonzero? replseqs.unshift( item=@list[i].subseq(0...fragrest_f) )
if item.respond_to? :overlaid?
repldata.prepend item.all_data
replseqs.shift
end
end
if fragrest_l < @list[l_idx].size replseqs.push( item=@list[l_idx].subseq(fragrest_l..-1) )
if item.respond_to? :overlaid?
repldata.append item.all_data
replseqs.pop
end
end
replseqs.delete_if{|cu| !cu or cu.empty? }
assert f_idx >= 0
assert l_idx >= 0
assert f_idx < @list.size
assert l_idx <= @list.size
assert f_idx <= l_idx
@list[i..l_idx]=replseqs
_rebuild_idxs(f_idx)
@pos=_adjust_pos_on_change(@pos, first,len,result.size)
@current_idx=_lookup_idx @pos
notify_change(self,first,len,repllen)
result
end
|
#prepend(data) ⇒ Object
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
# File 'lib/sequence/list.rb', line 287
def prepend(data)
if @list.first.overlaid?
@list.first.prepend data
return self
end
data=data.dup.to_sequence
data.extend Overlaid
@list[0,0]=data
sz=data.size
@start_pos=@start_pos.map{|n| n+sz}
@start_pos[0,0]=0
notify_change(self,0, 0, data.size)
self
end
|
#read(len) ⇒ Object
105
106
107
108
109
|
# File 'lib/sequence/list.rb', line 105
def read(len)
result=readahead(len)
move result.size
result
end
|
#readahead(len) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/sequence/list.rb', line 92
def readahead(len)
idx=_lookup_idx(pos)
result=@list[idx][pos-@start_pos[idx],len] || new_data
len-=result.size
assert len>=0
(idx+1).upto(@list.size-1){|i|
break(result+=@list[i][0,len]) if len<@list[i].size
result+=@list[i].all_data
len-=@list[i].size
}
result
end
|
#size ⇒ Object
149
150
151
|
# File 'lib/sequence/list.rb', line 149
def size
@start_pos.last
end
|