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.



374
375
376
377
# File 'lib/stream.rb', line 374

def initialize(streamOfStreams)
  super
  set_to_begin
end

Instance Method Details

#at_beginning?Boolean

Same as at_end? the other way round.

Returns:

  • (Boolean)


404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/stream.rb', line 404

def at_beginning?
  # same algorithm as at_end? the other way round.
   if @currentStream.at_beginning?
     return false
   else
   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.

Returns:

  • (Boolean)


381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/stream.rb', line 381

def at_end?
  if @currentStream.at_end?
    return false
  else      
  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                     # until
  reachedBoundary         # sets @dirOfLastMove and @currentStream
  end
end

#basic_backwardObject

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



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

def basic_backward; @currentStream.basic_backward end

#basic_forwardObject

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



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

def basic_forward; @currentStream.basic_forward end

#set_to_beginObject



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

def set_to_begin; super; reachedBoundary end

#set_to_endObject



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

def set_to_end; super; reachedBoundary end