Sha256: 19b71518dbeaf331524aa85c1721cb76687a0f8d6cf80384c081461036005e65

Contents?: true

Size: 1017 Bytes

Versions: 28

Compression:

Stored size: 1017 Bytes

Contents

module Bluepill
  module Util
   class RotationalArray < Array
     def initialize(size)
       super(size)
       
       @capacity = size
       @counter = 0
     end
     
     def push(value)
       idx = rotational_idx(@counter)
       self[idx] = value
       
       @counter += 1
       self
     end
     
     alias_method :<<, :push
     
     def pop
       raise "Cannot call pop on a rotational array"
     end

     def shift
       raise "Cannot call shift on a rotational array"
     end

     def unshift
       raise "Cannot call unshift on a rotational array"
     end
     
     def last
       return if @counter.zero?
       
       self[rotational_idx(@counter - 1)]
     end
     
     def first
       return if @counter.zero?
       return self[0] if @counter <= @capacity
       
      self[rotational_idx(@counter)]
     end
     
     def clear
      @counter = 0
      super
     end
     
     private
     
     def rotational_idx(idx)
       idx % @capacity
     end
   end
 end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
bluepill-0.0.28 lib/bluepill/util/rotational_array.rb
bluepill-0.0.27 lib/bluepill/util/rotational_array.rb
gvarela-bluepill-0.0.28 lib/bluepill/util/rotational_array.rb
gvarela-bluepill-0.0.27 lib/bluepill/util/rotational_array.rb
bluepill-0.0.26 lib/bluepill/util/rotational_array.rb
bluepill-0.0.25 lib/bluepill/util/rotational_array.rb
bluepill-0.0.24 lib/bluepill/util/rotational_array.rb
bluepill-0.0.23 lib/bluepill/util/rotational_array.rb
bluepill-0.0.22 lib/bluepill/util/rotational_array.rb
bluepill-0.0.21 lib/bluepill/util/rotational_array.rb
bluepill-0.0.20 lib/bluepill/util/rotational_array.rb
bluepill-0.0.19 lib/bluepill/util/rotational_array.rb
bluepill-0.0.18 lib/bluepill/util/rotational_array.rb
bluepill-0.0.17 lib/bluepill/util/rotational_array.rb
bluepill-0.0.16 lib/bluepill/util/rotational_array.rb
bluepill-0.0.15 lib/bluepill/util/rotational_array.rb
bluepill-0.0.14 lib/bluepill/util/rotational_array.rb
bluepill-0.0.13 lib/bluepill/util/rotational_array.rb
bluepill-0.0.12 lib/bluepill/util/rotational_array.rb
bluepill-0.0.11 lib/bluepill/util/rotational_array.rb