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.



500
501
502
503
# File 'lib/stream.rb', line 500

def initialize(streamOfStreams)
  super
  set_to_begin
end

Instance Method Details

#at_beginning?Boolean

Same as at_end? the other way round.

Returns:

  • (Boolean)


533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/stream.rb', line 533

def at_beginning?
  # same algorithm as at_end? the other way round.
  unless @current_stream.at_beginning?
    return false
  end

  until streamOfStreams.at_beginning?
    dir = @dir_of_last_move
    @dir_of_last_move = :backward
    s = streamOfStreams.basic_backward
    next if dir == :forward

    s.set_to_end
    if s.at_beginning?
      next
    else
      @current_stream = s
      return false
    end
  end
  reached_boundary
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.

Returns:

  • (Boolean)


507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/stream.rb', line 507

def at_end?
  unless @current_stream.at_end?
    return false
  end

  until streamOfStreams.at_end?
    dir = @dir_of_last_move
    @dir_of_last_move = :forward
    s = streamOfStreams.basic_forward
    # if last move was backwards, then @current_stream 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
      @current_stream = s
      return false # found non empty stream
    end
  end # until
  reached_boundary # sets @dir_of_last_move and @current_stream
end

#basic_backwardObject

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



572
573
574
# File 'lib/stream.rb', line 572

def basic_backward
  @current_stream.basic_backward
end

#basic_forwardObject

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



566
567
568
# File 'lib/stream.rb', line 566

def basic_forward
  @current_stream.basic_forward
end

#set_to_beginObject



556
557
558
# File 'lib/stream.rb', line 556

def set_to_begin
  super; reached_boundary
end

#set_to_endObject



560
561
562
# File 'lib/stream.rb', line 560

def set_to_end
  super; reached_boundary
end