Class: Sequence::SubSeq

Inherits:
Sequence show all
Defined in:
lib/sequence/subseq.rb

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, #append, #begin, #begin!, #check, #check_until, #checkback, #checkback_until, #close, #delete, #each, #empty?, #end, #end!, #exist?, #existback?, #first, #goto, #holding, #holding!, #holding?, #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, #prepend, #prop, #read!, #read1, #readahead1, #readback1, #readbehind1, #rest_size, #reversed, #shift, #skip, #skip_literal, #skip_until, #skip_until_literal, #skipback, #skipback_until, #slice, #slice!, #slice1, #slice1!, #succ, #to_sequence, #was_data?, #write, #writeahead, #writeback, #writebehind

Constructor Details

#initialize(seq, first, len) ⇒ SubSeq

Returns a new instance of SubSeq.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sequence/subseq.rb', line 8

def initialize(seq, first,len)
  assert first
  first+len-1>=seq.size and len=seq.size-first
  @data=seq
  @pos=0
  @first,@size=first,len
  extend seq.like

  #ask for notifications on the parent seq...
  @data.on_change_notify self
  assert @first
  #p [:init,__id__]
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



73
74
75
# File 'lib/sequence/subseq.rb', line 73

def data
  @data
end

#posObject (readonly)

Returns the value of attribute pos.



65
66
67
# File 'lib/sequence/subseq.rb', line 65

def pos
  @pos
end

#sizeObject (readonly)

Returns the value of attribute size.



65
66
67
# File 'lib/sequence/subseq.rb', line 65

def size
  @size
end

Instance Method Details

#_pos=(newp) ⇒ Object



67
68
69
# File 'lib/sequence/subseq.rb', line 67

def _pos=newp
  @pos=newp
end

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



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sequence/subseq.rb', line 22

def change_notification data,first,oldsize,newsize
  assert @data==data 
  old_first=@first
  old_size=@size
  @pos=(_adjust_pos_on_change @first+@pos,first,oldsize,newsize)-@first
  @size=(_adjust_pos_on_change @first+@size,first,oldsize,newsize)-@first
  @first=_adjust_pos_on_change @first,first,oldsize,newsize
  assert @first
  #p [:cn, __id__]
  
  notify_change(self, first-@first, oldsize, newsize)
end

#closed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/sequence/subseq.rb', line 89

def closed?
  super or @data.closed?
end

#eof?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/sequence/subseq.rb', line 61

def eof?
  @pos>=@size
end

#modify(*args) ⇒ Object



81
82
83
84
85
86
# File 'lib/sequence/subseq.rb', line 81

def modify(*args)
  data=args.pop
  first,len,only1=_parse_slice_args( *args)
  first+=@first
  only1 ? @data.modify(first,data) : @data.modify(first,len,data)
end

#offsetObject



35
# File 'lib/sequence/subseq.rb', line 35

def offset; @first end

#read(len) ⇒ Object



49
50
51
52
53
# File 'lib/sequence/subseq.rb', line 49

def read(len)
  result=readahead(len)
  move result.size
  result
end

#readahead(len) ⇒ Object



37
38
39
40
41
# File 'lib/sequence/subseq.rb', line 37

def readahead(len)
    eof? and return new_data
    len>rest=rest_size and len=rest
    @data[@pos+offset,len]
end

#readback(len) ⇒ Object



55
56
57
58
59
# File 'lib/sequence/subseq.rb', line 55

def readback(len)
  result=readbehind(len)
  move( -result.size )
  result
end

#readbehind(len) ⇒ Object



43
44
45
46
47
# File 'lib/sequence/subseq.rb', line 43

def readbehind(len)
  @pos.zero? and return new_data
  @pos>=len or len=@pos
  @data[@pos+offset-len,len] 
end

#subseq(*args) ⇒ Object



75
76
77
78
79
# File 'lib/sequence/subseq.rb', line 75

def subseq *args
  #p [:subseq, __id__]
  first,len,only1=_parse_slice_args( *args)
  SubSeq.new(@data,@first+first,len)
end