Parent

Files

LazyList::ReadQueue

ReadQueue is the implementation of an read-only queue that only supports shift and empty? methods. It’s used as a wrapper to encapsulate enumerables in lazy lists.

Public Class Methods

new(enumerable) click to toggle source

Creates an ReadQueue object from an enumerable.

# File lib/lazylist/enumerator_queue.rb, line 7
    def initialize(enumerable)
      @enum = enumerable.to_enum
      @empty = false
      shift
    end
new(enumerable) click to toggle source

Creates an ReadQueue object from an enumerable.

# File lib/lazylist/thread_queue.rb, line 7
    def initialize(enumerable)
      @data = []
      @producer = Thread.new do
        Thread.stop
        begin
          enumerable.each do |value|
            old, Thread.critical = Thread.critical, true
            begin
              @data << value
              @consumer.wakeup
              Thread.stop
            ensure
              Thread.critical = old
            end
          end
        rescue => e
          @consumer.raise e
        ensure
          @consumer.wakeup
        end
      end
      Thread.pass until @producer.stop?
    end

Public Instance Methods

empty?() click to toggle source

Returns true if the queue is empty.

# File lib/lazylist/thread_queue.rb, line 44
    def empty?
      if @data.empty?
        old, Thread.critical = Thread.critical, true
        begin
          @consumer = Thread.current
          @producer.wakeup
          Thread.stop
        rescue ThreadError
          ;
        ensure
          @consumer = nil
          Thread.critical = old
        end
      end
      @data.empty?
    end
empty?() click to toggle source

Returns true if the queue is empty.

# File lib/lazylist/enumerator_queue.rb, line 31
    def empty?
      !@next and @empty
    end
pop() click to toggle source

Alias for shift

pop() click to toggle source

Alias for shift

shift() click to toggle source

Extracts the top element from the queue or nil if the queue is empty.

# File lib/lazylist/enumerator_queue.rb, line 14
    def shift
      if @empty
        nil
      else
        result = @next
        @next = @enum.next
        result
      end
    rescue StopIteration
      @next = nil
      @empty = true
      result
    end
Also aliased as: pop, pop
shift() click to toggle source

Extracts the top element from the queue or nil if the queue is empty.

# File lib/lazylist/thread_queue.rb, line 33
    def shift
      if empty?
        nil
      else
        @data.shift
      end
    end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.