Class: Evidence::SlicedStreams

Inherits:
Object
  • Object
show all
Includes:
Stream
Defined in:
lib/evidence/stream.rb

Instance Method Summary collapse

Methods included from Stream

#|

Constructor Details

#initialize(stream, index, start_index, end_index) ⇒ SlicedStreams

Returns a new instance of SlicedStreams.



98
99
100
101
# File 'lib/evidence/stream.rb', line 98

def initialize(stream, index, start_index, end_index)
  @stream, @index, @start_index, @end_index = stream, index, start_index, end_index
  @slice_start_index, @slice_end_index = nil
end

Instance Method Details

#each(&output) ⇒ Object



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
# File 'lib/evidence/stream.rb', line 107

def each(&output)
  return if eos?
  @head ||= @stream.first
  @slice_start_index ||= @start_index || @index[@head]
  @slice_end_index ||= @end_index[@slice_start_index]
  @eos_in_slice ||= false
  loop do
    if @slice_start_index > @index[@head]
      return if eos?
      @head = @stream.first
    else
      break
    end
  end

  loop do
    break if @eos_in_slice
    range = @slice_start_index..@slice_end_index
    slice_enum = Enumerator.new do |y|
      loop do
        break if range.max <= @index[@head]
        if @eos_in_slice = eos?
          y << @head
          break
        end
        head, @head = @head, @stream.first
        y << head
      end
    end
    @slice_start_index, @slice_end_index = range.max, @end_index[range.max]
    output[range: range, stream: EnumStream.new(slice_enum)]
  end
end

#eos?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/evidence/stream.rb', line 103

def eos?
  @stream.eos?
end