Class: LookAheadIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/look_ahead_iterator.rb,
lib/look_ahead_iterator/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enumerable, options = {}) ⇒ LookAheadIterator

A LookAheadIterator can be created from any enumerable object.

Options:

  • :stop : If true, the iteration will stop by throwing StopIteration like other external iterators. This will end a loop.

   Otherwise the user must check of end of iteration by

  • :end : is used to define a special value to be returned if the iteration has ended (or when looking beyond the end of the collection). By default it is nil.



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

def initialize(enumerable, options = {})
  @end_value = options[:end]
  @stop = options[:stop]
  @iterator = enumerable.each
  @iterator_ended = false
  @buffer = []
  @current = nil
  @ended = false
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



26
27
28
# File 'lib/look_ahead_iterator.rb', line 26

def current
  @current
end

#end_valueObject (readonly)

Returns the value of attribute end_value.



26
27
28
# File 'lib/look_ahead_iterator.rb', line 26

def end_value
  @end_value
end

Instance Method Details

#eachObject



28
29
30
31
32
33
34
35
36
# File 'lib/look_ahead_iterator.rb', line 28

def each
  if @stop
    loop do
      yield self.next
    end
  else
    yield self.next while next?
  end
end

#ended?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/look_ahead_iterator.rb', line 60

def ended?
  # If @end_value cannot be contained in the collection,
  # then this is equivalent to:
  #   @buffer.empty? && @current = @end_value
  @ended
end

#is_end?(value) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/look_ahead_iterator.rb', line 90

def is_end?(value)
  value == @end_value
end

#is_valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/look_ahead_iterator.rb', line 94

def is_valid?(value)
  value != @end_value
end

#look_ahead(n = 1) ⇒ Object

This method is used to obtained the next value to be returned by each. If a parameter n is passed the value to be returned after n calls to each is returned. The value 0 can be used to return the current value (from the previous call to each),



79
80
81
82
83
84
85
86
87
88
# File 'lib/look_ahead_iterator.rb', line 79

def look_ahead(n=1)
  raise "Can't look back!" if n < 0
  return @current if n == 0

  while n > @buffer.size && !@iterator_ended
    value = next_or_end
    @buffer << value unless @iterator_ended
  end
  n <= @buffer.size ? @buffer[n-1] : @end_value
end

#nextObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/look_ahead_iterator.rb', line 40

def next
  if @buffer.size > 0
    @current = @buffer.shift
  else
    if @stop
      begin
        @current = @iterator.next
      rescue StopIteration
        @iterator_ended = @ended = true
        @current = @end_value
        raise
      end
    else
      @current = next_or_end
      @ended = @iterator_ended
      @current
    end
  end
end

#next?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/look_ahead_iterator.rb', line 67

def next?
  # If @end_value cannot be contained in the collection,
  # then this is equivalent to:
  #   !ended? && (look_ahead(1) != @end_value)
  !ended? && (look_ahead(1); !(@iterator_ended && @buffer.size==0))
end