Class: SlowEnumeratorTools::Merger::Iterator

Inherits:
Object
  • Object
show all
Defined in:
lib/slow_enumerator_tools/merger.rb

Instance Method Summary collapse

Constructor Details

#initialize(enums) ⇒ Iterator

Returns a new instance of Iterator.



14
15
16
17
18
# File 'lib/slow_enumerator_tools/merger.rb', line 14

def initialize(enums)
  @enums = enums
  @q = SizedQueue.new(5)
  @done = false
end

Instance Method Details

#nextObject

Raises:

  • (StopIteration)


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/slow_enumerator_tools/merger.rb', line 20

def next
  raise StopIteration if @done

  nxt = @q.pop
  if SlowEnumeratorTools::Util::STOP_OK.equal?(nxt)
    @done = true
    raise StopIteration
  elsif SlowEnumeratorTools::Util::STOP_ERR.equal?(nxt)
    raise @q.pop
  else
    nxt
  end
end

#startObject



34
35
36
37
38
39
40
41
# File 'lib/slow_enumerator_tools/merger.rb', line 34

def start
  threads = @enums.map { |enum| spawn_empty_into(enum, @q) }

  Thread.new do
    threads.each(&:join)
    @q << SlowEnumeratorTools::Util::STOP_OK
  end
end