Class: Sequence::Buffered

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

Overview

how much of that should remain true?

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, #_delete_position, #_normalize_pos, #_parse_slice_args, #all_data, #append, #begin, #begin!, #check, #check_until, #checkback, #checkback_until, #close, #closed?, #delete, #each, #empty?, #end, #end!, #eof?, #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(input, buffer_size = 1024, buffer = nil) ⇒ Buffered

Returns a new instance of Buffered.



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

def initialize(input,buffer_size=1024,buffer=nil)
    @input = input
    huh #@input used incorrectly... it should be used kinda like a read-once data store
        #and Buffered should have an independant position
    @buffer_size=buffer_size
    @buffer = buffer||@input.new_data
#        @output_pos = output_pos
    @buffer_pos=@pos=@input.pos
    
    @input.on_change_notify self  
end

Instance Attribute Details

#buffer_sizeObject

Returns the value of attribute buffer_size.



39
40
41
# File 'lib/sequence/buffered.rb', line 39

def buffer_size
  @buffer_size
end

#posObject (readonly)

Returns the value of attribute pos.



40
41
42
# File 'lib/sequence/buffered.rb', line 40

def pos
  @pos
end

Instance Method Details

#_default_maxmatchlenObject



46
# File 'lib/sequence/buffered.rb', line 46

def _default_maxmatchlen; @buffer_size/2 end

#_pos=(pos) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sequence/buffered.rb', line 48

def _pos=(pos)
  if pos<@buffer_pos
    @pos=@input.pos=pos  #could raise exception, if @input doesn't support #pos=
  elsif pos<=@buffer_pos+@buffer.size
    @pos=pos
  else #@pos > buffer_end_pos
    assert @buffer_pos+@buffer.size==@input.pos
    @buffer<<@input.read(pos-@input.pos)
    buffer_begin_ageout!
  end
end

#buffer_begin_ageout!Object



111
112
113
114
115
116
117
# File 'lib/sequence/buffered.rb', line 111

def buffer_begin_ageout!
  diff=@buffer.size-@buffer_size 
  if diff>0 
    @buffer.slice!(0,diff)
    @buffer_pos+=diff
  end
end

#buffer_end_ageout!Object



119
120
121
122
123
124
# File 'lib/sequence/buffered.rb', line 119

def buffer_end_ageout!
  diff=@buffer.size-@buffer_size 
  if diff>0 
    @buffer.slice!(-diff..-1)
  end
end

#change_notificationObject



34
35
36
37
# File 'lib/sequence/buffered.rb', line 34

def change_notification
  huh #invalidate (part of) @buffer if it overlaps the changed area
  huh #adjust @buffer_pos as necessary
end

#crude_read(len) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/sequence/buffered.rb', line 66

def crude_read(len)
    assert @buffer_pos+@buffer.size==@input.pos
    result=@input.read(len)
    @buffer<<result
    buffer_begin_ageout!
    result
end

#crude_read_before(len) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/sequence/buffered.rb', line 74

def crude_read_before(len)
    assert @buffer_pos+@buffer.size==@input.pos
    result=@input.read(len)
    @buffer.insert(0,*result)
    buffer_end_ageout!
    result
end

#history_mode?(pos = @pos) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sequence/buffered.rb', line 60

def history_mode?(pos=@pos)
  pos<@buffer_pos+@buffer.size
end

#modify(*args) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sequence/buffered.rb', line 126

def modify(*args)
  huh "what does it mean to write to a Buffered?"
  repldata=args.pop
  first,len,only1=_parse_slice_args(*args)
  first<pos-@buffer.size and huh
  result=new_data
  if first<pos
    result=@buffer[huh]
  end
  if first+len>pos
    huh
  end
  huh
end

#new_dataObject

:stopdoc:



43
44
45
# File 'lib/sequence/buffered.rb', line 43

def new_data
    @input.new_data
end

#read(len) ⇒ Object



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

def read(len)
  if @pos<@buffer_pos
    if @buffer_pos-@pos >= @buffer_size
      @buffer_pos=@pos
      @buffer=new_data
      return crude_read(len)
    else 
      crude_read_before(@buffer_pos-@pos)
      self._pos=@buffer_pos
      
      #fall thru
    end
  end
  
  if history_mode?
    if history_mode?(pos+len-1)
      result=@buffer[@pos-@buffer_pos,len]
      @pos+=len
    else
      result=@buffer[@pos-@buffer.pos..0]
      result<<crude_read(len-result.size)
    end

    result
  else
    crude_read len
  end
end