Class: Sequence::Shifting

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

Constant Summary

Constants inherited from Sequence

SubSequence, VERSION

Instance Attribute Summary

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, #readahead, #readahead1, #readback, #readback1, #readbehind, #readbehind1, #rest_size, #reversed, #shift, #size, #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(seq, data = Sequence::Indexed.new(seq.new_data)) ⇒ Shifting

Returns a new instance of Shifting.



17
18
19
20
21
22
23
24
25
# File 'lib/sequence/shifting.rb', line 17

def initialize(seq,data= Sequence::Indexed.new(seq.new_data))
    @seq=seq #primary
    @data = data   #secondary
    @offset=@seq.pos
    
    extend @seq.like
    
    @seq.on_change_notify self
end

Instance Method Details

#_pos=(x) ⇒ Object



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
# File 'lib/sequence/shifting.rb', line 90

def _pos= x; 
  assert @data.empty?|| (0..@data.size)===pos-@offset
  #assert @data.size+@[email protected]
  if @data.empty?
    diff=x-@offset
    @data<<
      if diff<0
        @offset=x
        diff=-diff
        @seq[x,diff] rescue
          case data_class
          when ::Array;  [nil]
          when ::String; "\0"
          else fail
          end*diff
     
      else #diff>=0
        @seq.read(diff)
      end
  
    @data.pos=x-@offset
    assert @data.size+@offset==@seq.pos
    assert( (0..@data.size)===x-@offset )
  elsif x<@offset
    offset=@offset
    @offset=x
    @data.prepend((
      @seq[x...offset] rescue
      case data_class
      when ::Array;  [nil]
      when ::String; "\0"
      else fail
      end*(offset-x)
    ))
#       assert @data.size+@[email protected]
    assert( (0..@data.size)===x-@offset )
  elsif x>@data.size+@offset
#       assert @[email protected]+@offset
    @data<<@seq.read(x-@data.size-@offset)
    @data.pos=@data.size
    #assert @data.size+@[email protected]
    assert( (0..@data.size)===x-@offset )
  else
    @data.pos=x-@offset
    #assert @data.size+@[email protected]
    assert( (0..@data.size)===x-@offset )
  end
  #assert @data.size+@[email protected]
  assert( (0..@data.size)===x-@offset )
#     assert(pos==x || pos==size)
  x
end

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sequence/shifting.rb', line 31

def change_notification(cu,first,oldsize,newsize)
  assert @seq==cu
  
  if first<=@offset
    if first+oldsize>=@offset
      offset=first
    end
    @offset-=oldsize-newsize
  end 
  
  #modify @data (shift buffer) to suit
  if first<@offset
    @data.prepend @seq[first...@offset]
    @offset=first
  elsif first<@data.size+@offset  #if change within already shifted data
    @data[first-@offset,oldsize]= @seq[first,newsize]  
  end
  
  notify_change(self,first,oldsize,newsize)
end

#closed?Boolean

:startdoc:

Returns:

  • (Boolean)


183
184
185
# File 'lib/sequence/shifting.rb', line 183

def closed?
   super or @seq.closed?
end

#eof?Boolean

Returns:

  • (Boolean)


180
# File 'lib/sequence/shifting.rb', line 180

def eof?; @data.eof? and @seq.eof? end

#modify(*args) ⇒ Object



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
# File 'lib/sequence/shifting.rb', line 143

def modify *args
  newvals=args.pop
  first,len,only1=_parse_slice_args( *args )
  only1 and newvals=new_data<<newvals

  assert @data.size+@offset==@seq.pos

  oldpos=pos

  if @data.empty?
    @offset=first
    @data<<newvals
    @data.pos=_adjust_pos_on_change(oldpos, first, len, newvals.size)-@offset
    #assert @data.size+@[email protected]
  else
    #if first...first+len outside of @data, read it into @data first
    oldpos=pos
    self._pos=first
    self._pos=first+len
    self._pos=oldpos #then revert to orig position
  
    assert( (0...@data.size)===first-@offset )
    assert( (0...@data.size)===first+len-@offset )
    @data.modify first-@offset,len,newvals
    #assert @data.size+@[email protected]
  end
  notify_change(self,first,len,newvals.size)
  
  #assert @data.size+@[email protected]

  newvals
end

#new_dataObject

:stopdoc:



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

def new_data
    @data.new_data
end

#posObject



176
# File 'lib/sequence/shifting.rb', line 176

def pos; @data.pos+@offset end

#read(len) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sequence/shifting.rb', line 75

def read(len)
  assert oldpos=pos
  if history_mode?
    rest=@seq.pos-pos
    len<=rest and return @data.read(len)
    @data.append @seq.read(len-rest)
  else
    assert @data.pos+@offset==@seq.pos
    @data.append @seq.read(len)
  end
  result=@data.read!
#     assert pos==oldpos+len || pos==size
  return result
end