Class: Evidence::MergedStream
- Inherits:
-
Object
- Object
- Evidence::MergedStream
- Includes:
- Stream
- Defined in:
- lib/evidence/stream.rb
Instance Method Summary collapse
- #each(&output) ⇒ Object
- #eos? ⇒ Boolean
-
#initialize(streams, comparator) ⇒ MergedStream
constructor
A new instance of MergedStream.
- #pull(stream) ⇒ Object
- #pull_heads ⇒ Object
Methods included from Stream
Constructor Details
#initialize(streams, comparator) ⇒ MergedStream
Returns a new instance of MergedStream.
60 61 62 63 |
# File 'lib/evidence/stream.rb', line 60 def initialize(streams, comparator) @comparator = comparator @heads = streams.map{|s| {stream: s}} end |
Instance Method Details
#each(&output) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/evidence/stream.rb', line 69 def each(&output) pull_heads loop do if min = @heads.min{|a, b| @comparator.call(a[:element], b[:element])} output.call(min.delete(:element).tap{ pull_heads }) else break if @heads.empty? pull_heads end end end |
#eos? ⇒ Boolean
65 66 67 |
# File 'lib/evidence/stream.rb', line 65 def eos? @heads.empty? end |
#pull(stream) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/evidence/stream.rb', line 85 def pull(stream) loop do return nil if stream.eos? if n = stream.first return n end end end |
#pull_heads ⇒ Object
81 82 83 |
# File 'lib/evidence/stream.rb', line 81 def pull_heads @heads.select!{|h| h[:element] ||= pull(h[:stream])} end |