Parent

Files

Class/Module Index [+]

Quicksearch

Concurrent::Channel::RingBuffer

non-thread safe buffer

@api Channel @!macro edge_warning

Public Class Methods

new(capacity) click to toggle source
# File lib/concurrent/channel/ring_buffer.rb, line 10
def initialize(capacity)
  @buffer = Array.new(capacity)
  @first = @last = 0
  @count = 0
end

Public Instance Methods

capacity() click to toggle source

@return [Integer] the capacity of the buffer

# File lib/concurrent/channel/ring_buffer.rb, line 18
def capacity
  @buffer.size
end
count() click to toggle source

@return [Integer] the number of elements currently in the buffer

# File lib/concurrent/channel/ring_buffer.rb, line 23
def count
  @count
end
empty?() click to toggle source

@return [Boolean] true if buffer is empty, false otherwise

# File lib/concurrent/channel/ring_buffer.rb, line 28
def empty?
  @count == 0
end
full?() click to toggle source

@return [Boolean] true if buffer is full, false otherwise

# File lib/concurrent/channel/ring_buffer.rb, line 33
def full?
  @count == capacity
end
offer(value) click to toggle source

@param [Object] value @return [Boolean] true if value has been inserted, false otherwise

# File lib/concurrent/channel/ring_buffer.rb, line 39
def offer(value)
  return false if full?

  @buffer[@last] = value
  @last = (@last + 1) % @buffer.size
  @count += 1
  true
end
peek() click to toggle source

@return [Object] the first available value and without removing it from

the buffer. If buffer is empty returns nil
# File lib/concurrent/channel/ring_buffer.rb, line 59
def peek
  @buffer[@first]
end
poll() click to toggle source

@return [Object] the first available value and removes it from the buffer. If buffer is empty returns nil

# File lib/concurrent/channel/ring_buffer.rb, line 49
def poll
  result = @buffer[@first]
  @buffer[@first] = nil
  @first = (@first + 1) % @buffer.size
  @count -= 1
  result
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.