Class: Stream::IntervalStream

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

Overview

A simple Iterator for iterating over a sequence of integers starting from zero up to a given upper bound. Mainly used by Stream::FilteredStream. Could be made private but if somebody needs it here it is. Is there a better name for it?

The upper bound is stored in the instance variable @stop which can be incremented dynamically by the method increment_stop.

Constant Summary

Constants included from Stream

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

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(stop = 0) ⇒ IntervalStream

Create a new IntervalStream with upper bound stop. stop - 1 is the last element. By default stop is zero which means that the stream is empty.

Parameters:

  • stop (Integer) (defaults to: 0)

    exclusive upper bound; stream yields 0..stop-1



265
266
267
268
# File 'lib/stream.rb', line 265

def initialize(stop = 0)
  @stop = stop - 1
  set_to_begin
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



260
261
262
# File 'lib/stream.rb', line 260

def pos
  @pos
end

Instance Method Details

#at_beginning?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/stream.rb', line 270

def at_beginning?
  @pos < 0
end

#at_end?Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/stream.rb', line 274

def at_end?
  @pos == @stop
end

#basic_backwardObject



295
296
297
298
# File 'lib/stream.rb', line 295

def basic_backward
  @pos -= 1
  @pos + 1
end

#basic_forwardObject



291
292
293
# File 'lib/stream.rb', line 291

def basic_forward
  @pos += 1
end

#increment_stop(incr = 1) ⇒ Object

Increment the upper bound by incr.



287
288
289
# File 'lib/stream.rb', line 287

def increment_stop(incr = 1)
  @stop += incr
end

#set_to_beginObject



282
283
284
# File 'lib/stream.rb', line 282

def set_to_begin
  @pos = -1
end

#set_to_endObject



278
279
280
# File 'lib/stream.rb', line 278

def set_to_end
  @pos = @stop
end