Class: Stream::ConcatenatedStream

Inherits:
WrappedStream show all
Defined in:
lib/stream.rb

Overview

Given a stream of streams. Than a ConcatenatedStream is obtained by concatenating these in the given order. A ConcatenatedStream is created by the methods Stream#concatenate or Stream#concatenate_collected send to a stream of streams or by the method + which concatenats two streams:

((1..3).create_stream + [4,5].create_stream).to_a ==> [1, 2, 3, 4, 5]

Instance Attribute Summary

Attributes inherited from WrappedStream

#wrapped_stream

Instance Method Summary collapse

Methods inherited from WrappedStream

#unwrapped

Methods included from Stream

#+, #backward, #collect, #concatenate, #concatenate_collected, #create_stream, #current, #current_edge, #each, #empty?, #filtered, #first, #forward, #last, #modify, #move_backward_until, #move_forward_until, #peek, #remove_first, #remove_last, #reverse, #unwrapped

Methods included from Enumerable

#create_stream

Constructor Details

#initialize(streamOfStreams) ⇒ ConcatenatedStream

Creates a new ConcatenatedStream wrapping the stream of streams streamOfStreams.



370
371
372
373
# File 'lib/stream.rb', line 370

def initialize (streamOfStreams)
  super
  set_to_begin
end

Instance Method Details

#at_beginning?Boolean

Same as at_end? the other way round.



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/stream.rb', line 399

def at_beginning?
# same algorithm as at_end? the other way round. Could we do it
# with metaprogramming?
 @currentStream.at_beginning? and
    begin
 until streamOfStreams.at_beginning?
dir, @dirOfLastMove = @dirOfLastMove, :backward
s = streamOfStreams.basic_backward
next if dir == :forward
s.set_to_end
if s.at_beginning?
  next
else
  @currentStream = s
  return false
end
 end
 reachedBoundary
    end
end

#at_end?Boolean

If the current stream is at end, than at_end? has to look ahead to find a non empty in the stream of streams, which than gets the current stream.



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/stream.rb', line 377

def at_end?
 @currentStream.at_end? and
    begin
 until streamOfStreams.at_end?
dir, @dirOfLastMove = @dirOfLastMove, :forward
s = streamOfStreams.basic_forward
# if last move was backwards, then @currentStream is
# equivalent to s. Move to next stream.
next if dir == :backward
s.set_to_begin
if s.at_end?      # empty stream?
  next        # skip it
else
  @currentStream = s
  return false    # found non empty stream
end
 end
 reachedBoundary    # sets @dirOfLastMove and @currentStream
    end
end

#basic_backwardObject

Returns the previous element of @currentStream. at_beginning? ensured that there is one.



427
# File 'lib/stream.rb', line 427

def basic_backward; @currentStream.basic_backward end

#basic_forwardObject

Returns the next element of @currentStream. at_end? ensured that there is one.



424
# File 'lib/stream.rb', line 424

def basic_forward; @currentStream.basic_forward end

#set_to_beginObject



420
# File 'lib/stream.rb', line 420

def set_to_begin; super; reachedBoundary end

#set_to_endObject



421
# File 'lib/stream.rb', line 421

def set_to_end; super; reachedBoundary end