Class: CouchShell::RingBuffer
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
          - Object
- CouchShell::RingBuffer
 show all
    - Defined in:
- lib/couch-shell/ring_buffer.rb
 
Overview
  
Defined Under Namespace
  
    
  
    
      Classes: UninitializedAccess
    
  
  
    
      Instance Method Summary
      collapse
    
    
  
  Constructor Details
  
    
  
  
    
Returns a new instance of RingBuffer.
   
 
  
  
    | 
22
23
24
25
26 | # File 'lib/couch-shell/ring_buffer.rb', line 22
def initialize(size)
  @ary = Array.new(size, nil)
  @index = nil
  @written = 0
end | 
 
  
 
  
    Instance Method Details
    
      
  
  
    #<<(elem)  ⇒ Object 
  
  
  
  
    | 
73
74
75
76
77
78
79
80
81
82
83 | # File 'lib/couch-shell/ring_buffer.rb', line 73
def <<(elem)
  if @index.nil? || @index == size - 1
    @index = 0
  else
    @index += 1
  end
  @ary[@index] = elem
  if @written < @index + 1
    @written = @index + 1
  end
end | 
 
    
      
  
  
    #[](i)  ⇒ Object 
  
  
  
  
    | 
65
66
67
68
69
70
71 | # File 'lib/couch-shell/ring_buffer.rb', line 65
def [](i)
  i = i.to_int
  if i >= @written
    raise UninitializedAccess.new(i)
  end
  @ary[i]
end | 
 
    
      
  
  
    #current  ⇒ Object 
  
  
  
  
    | 
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 | # File 'lib/couch-shell/ring_buffer.rb', line 40
def current
  if @index.nil?
    if block_given?
      nil
    else
      raise UninitializedAccess.new(0)
    end
  else
    if block_given?
      yield @ary[index]
    else
      @ary[index]
    end
  end
end | 
 
    
      
  
  
    #empty?  ⇒ Boolean 
  
  
  
  
    | 
36
37
38 | # File 'lib/couch-shell/ring_buffer.rb', line 36
def empty?
  @written == 0
end | 
 
    
      
  
  
    #index  ⇒ Object 
  
  
  
  
    
index of current (last written) element, or nil if empty
   
 
  
  
    | 
61
62
63 | # File 'lib/couch-shell/ring_buffer.rb', line 61
def index
  @index
end | 
 
    
      
  
  
    #initialized_size  ⇒ Object 
  
  
  
  
    | 
32
33
34 | # File 'lib/couch-shell/ring_buffer.rb', line 32
def initialized_size
  @written
end | 
 
    
      
  
  
    #readable_index?(i)  ⇒ Boolean 
  
  
  
  
    | 
56
57
58 | # File 'lib/couch-shell/ring_buffer.rb', line 56
def readable_index?(i)
  i >= 0 && i < @written
end | 
 
    
      
  
  
    #size  ⇒ Object 
  
  
  
  
    | 
28
29
30 | # File 'lib/couch-shell/ring_buffer.rb', line 28
def size
  @ary.size
end |