Class: Sequence::Circular

Inherits:
Sequence show all
Defined in:
lib/sequence/circular.rb,
lib/sequence/list.rb,
lib/sequence/reversed.rb

Overview

forward decl

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, #_parse_slice_args, #all_data, #append, #begin, #begin!, #check, #check_until, #checkback, #checkback_until, #close, #delete, #empty?, #exist?, #existback?, #first, #goto, #holding, #holding!, #holding?, #holding_position, #holding_position!, #holding_position?, #insert, #last, #length, #match?, #matchback?, #more_data?, #move, #new, #notify_change, #on_change_notify, #original__new, #overwrite, #pop, #pos=, #pos?, #position, #position=, #position?, #pred, #prepend, #prop, #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(sequence, pos = sequence.pos) ⇒ Circular

Create a circular sequence from a normal finite one.



16
17
18
19
20
21
22
23
# File 'lib/sequence/circular.rb', line 16

def initialize(sequence,pos=sequence.pos)
  @seq = sequence
  @pos=pos
  @size=sequence.size
  extend sequence.like

  @seq.on_change_notify self
end

Instance Attribute Details

#posObject (readonly)

def _adjust_delete(len=1,reverse=false)

  pos = _pos(false)
  ret = nil
  @positions.each { |p| ret = p.__send__(:_deletion,pos,len,reverse,ret) }
end
def _adjust_insert(len=1)
  pos = _pos(false)
  ret = nil
  @positions.each { |p| ret = p.__send__(:_insertion,pos,len,ret) }
end


51
52
53
# File 'lib/sequence/circular.rb', line 51

def pos
  @pos
end

#sizeObject (readonly)

def _adjust_delete(len=1,reverse=false)

  pos = _pos(false)
  ret = nil
  @positions.each { |p| ret = p.__send__(:_deletion,pos,len,reverse,ret) }
end
def _adjust_insert(len=1)
  pos = _pos(false)
  ret = nil
  @positions.each { |p| ret = p.__send__(:_insertion,pos,len,ret) }
end


51
52
53
# File 'lib/sequence/circular.rb', line 51

def size
  @size
end

Instance Method Details

#<=>(other) ⇒ Object

Compare to other.



190
191
192
# File 'lib/sequence/circular.rb', line 190

def <=>(other)
  position?(other) and pos<=>other.pos
end

#_normalize_pos(pos, size = nil) ⇒ Object

the default _parse_slice_args isn’t forgiving enough about large (positive or negative) indexes



27
28
29
# File 'lib/sequence/circular.rb', line 27

def _normalize_pos pos, size=nil
  pos
end

#_pos=(pos) ⇒ Object



52
53
54
55
# File 'lib/sequence/circular.rb', line 52

def _pos=(pos)
  #pos=0 if pos>=size
  @pos=pos
end

#change_notification(cu, first, oldsize, newsize) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/sequence/circular.rb', line 31

def change_notification(cu,first,oldsize,newsize)
  assert(cu.equal?( @seq ))
  @pos =_adjust_pos_on_change(@pos, first,oldsize,newsize)
  @size+=newsize-oldsize
  assert @size==@seq.size
  notify_change(self,first,oldsize,newsize)
end

#closed?Boolean

Returns:

  • (Boolean)


265
266
267
# File 'lib/sequence/circular.rb', line 265

def closed?
  super or @seq.closed?
end

#dataObject

:stopdoc:



208
209
210
# File 'lib/sequence/circular.rb', line 208

def data
  @seq
end

#eachObject



212
213
214
215
216
# File 'lib/sequence/circular.rb', line 212

def each
  po=position
  yield read1 until self==position
  po.close
end

#eof?Boolean

Returns:

  • (Boolean)


218
# File 'lib/sequence/circular.rb', line 218

def eof?; false end

#modify(*args) ⇒ Object



241
242
243
244
245
246
247
248
249
250
# File 'lib/sequence/circular.rb', line 241

def modify(*args)
  data=args.last
  first,len,only1=_parse_slice_args(*args[0...-1])
  first %= size

  len>size and raise( ArgumentError, "dst len too long")
  first+len>size and raise( ArgumentError, "wraparound modify in circular")

  @seq.modify(*args)
end

#move!(reverse = false) ⇒ Object

skip over one pass of the data to return where you started



184
185
186
# File 'lib/sequence/circular.rb', line 184

def move!(reverse=false)
    size
end

#nearbegin(len, at = pos) ⇒ Object



257
258
259
# File 'lib/sequence/circular.rb', line 257

def nearbegin(len,at=pos)
  false
end

#nearend(len, at = pos) ⇒ Object



261
262
263
# File 'lib/sequence/circular.rb', line 261

def nearend(len,at=pos)
  false
end

#new_dataObject

:stopdoc:



59
60
61
# File 'lib/sequence/circular.rb', line 59

def new_data
    @seq.new_data
end

#read(len) ⇒ Object



235
236
237
238
239
# File 'lib/sequence/circular.rb', line 235

def read len
  result=readahead len
  move len
  result
end

#read!(reverse = false) ⇒ Object

:startdoc: read over one pass of the data to return where you started



176
177
178
179
180
181
182
# File 'lib/sequence/circular.rb', line 176

def read!(reverse=false)
  unless reverse
    read(size)
  else
    readback(-size)
  end
end

#readahead(len) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/sequence/circular.rb', line 220

def readahead(len)
  result=@seq[@pos%size,len]
  len-=result.size
  len.zero? and return result
  loops=len/size

  result+=@seq[0...size]*loops if loops.nonzero?

  len%=size

  len.zero? and return result

  result+=@seq[0,len]
end

#reverseObject

when reversed and circular, always put the Circular outermost.



253
254
255
# File 'lib/sequence/circular.rb', line 253

def reverse
  Circular.new @seq.reverse
end