Sha256: 3789a59fd267199949495268d3487900924e9456237d46d6b3087c1c90de5bb3

Contents?: true

Size: 1.26 KB

Versions: 15

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Textbringer
  class Ring
    include Enumerable

    def initialize(max = 30, on_delete: ->(x) {})
      @max = max
      @ring = []
      @current = -1
      @on_delete = on_delete
    end

    def clear
      @ring.clear
      @current = -1
    end

    def push(obj)
      @current += 1
      if @ring.size < @max
        @ring.insert(@current, obj)
      else
        if @current == @max
          @current = 0
        end
        @on_delete.call(@ring[@current])
        @ring[@current] = obj
      end
    end

    def pop
      x = @ring[@current]
      rotate(1)
      x
    end

    def current
      if @ring.empty?
        raise EditorError, "Ring is empty"
      end
      @ring[@current]
    end

    def rotate(n)
      @current = get_index(n)
      @ring[@current]
    end

    def [](n = 0)
      @ring[get_index(n)]
    end

    def empty?
      @ring.empty?
    end

    def size
      @ring.size
    end

    def each(&block)
      @ring.each(&block)
    end

    def to_a
      @ring.to_a
    end

    private

    def get_index(n)
      if @ring.empty?
        raise EditorError, "Ring is empty"
      end
      i = @current - n
      if 0 <= i && i < @ring.size
        i
      else
        i % @ring.size
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
textbringer-1.0.0 lib/textbringer/ring.rb
textbringer-0.3.2 lib/textbringer/ring.rb
textbringer-0.3.1 lib/textbringer/ring.rb
textbringer-0.3.0 lib/textbringer/ring.rb
textbringer-0.2.9 lib/textbringer/ring.rb
textbringer-0.2.8 lib/textbringer/ring.rb
textbringer-0.2.7 lib/textbringer/ring.rb
textbringer-0.2.6 lib/textbringer/ring.rb
textbringer-0.2.4 lib/textbringer/ring.rb
textbringer-0.2.5 lib/textbringer/ring.rb
textbringer-0.2.3 lib/textbringer/ring.rb
textbringer-0.2.2 lib/textbringer/ring.rb
textbringer-0.2.1 lib/textbringer/ring.rb
textbringer-0.2.0 lib/textbringer/ring.rb
textbringer-0.1.9 lib/textbringer/ring.rb