Sha256: 8eda9ee755d4c7525dac74afceb579062ae0531a3df5891a94871eec40783005

Contents?: true

Size: 1.15 KB

Versions: 396

Compression:

Stored size: 1.15 KB

Contents

class CircularBuffer
  def initialize(capacity)
    @capacity = capacity
    @write_point = @read_point = 0
    @buffer = Array.new(capacity, nil)
  end

  def write(data)
    update_buffer(data) do
      fail BufferFullException if full?
      @buffer[@write_point] = data
    end
  end

  def write!(data)
    update_buffer(data) do
      @buffer[@write_point] = data
      if overwriting?
        update_read_point
      end
    end
  end

  def read
    fail BufferEmptyException if empty?
    data = @buffer[@read_point]
    @buffer[@read_point] = nil
    update_read_point
    data
  end

  def clear
    @buffer = Array.new(@capacity)
  end

  private

  def update_buffer(item)
    return if item.nil?
    yield if block_given?
    update_write_point
  end

  def update_read_point
    @read_point = (@read_point + 1) % @capacity
  end

  def update_write_point
    @write_point = (@write_point + 1) % @capacity
  end

  def overwriting?
    (full? && @write_point == @read_point)
  end

  def empty?
    @buffer.compact.empty?
  end

  def full?
    @buffer.all?
  end

  class BufferFullException < Exception; end

  class BufferEmptyException < Exception; end
end

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.179 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.178 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.177 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.176 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.175 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.174 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.173 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.172 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.171 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.170 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.169 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.167 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.166 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.165 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.164 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.163 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.162 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.161 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb
trackler-2.2.1.160 tracks/ruby/exercises/circular-buffer/.meta/solutions/circular_buffer.rb