Class: Bluepill::Util::RotationalArray
- Inherits:
-
Array
- Object
- Array
- Bluepill::Util::RotationalArray
- Defined in:
- lib/bluepill/util/rotational_array.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #each(&block) ⇒ Object
- #first ⇒ Object
-
#initialize(size) ⇒ RotationalArray
constructor
A new instance of RotationalArray.
- #last ⇒ Object
- #nitems ⇒ Object
- #pop ⇒ Object
- #push(value) ⇒ Object (also: #<<)
- #shift ⇒ Object
- #unshift ⇒ Object
Constructor Details
#initialize(size) ⇒ RotationalArray
Returns a new instance of RotationalArray.
4 5 6 7 8 9 |
# File 'lib/bluepill/util/rotational_array.rb', line 4 def initialize(size) super(size) @capacity = size @counter = 0 end |
Instance Method Details
#clear ⇒ Object
46 47 48 49 |
# File 'lib/bluepill/util/rotational_array.rb', line 46 def clear @counter = 0 super end |
#each(&block) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/bluepill/util/rotational_array.rb', line 51 def each(&block) times = @counter >= @capacity ? @capacity : @counter start = @counter >= @capacity ? rotational_idx(@counter) : 0 times.times do |i| block.call(self[rotational_idx(start + i)]) end end |
#first ⇒ Object
39 40 41 42 43 44 |
# File 'lib/bluepill/util/rotational_array.rb', line 39 def first return if @counter.zero? return self[0] if @counter <= @capacity self[rotational_idx(@counter)] end |
#last ⇒ Object
33 34 35 36 37 |
# File 'lib/bluepill/util/rotational_array.rb', line 33 def last return if @counter.zero? self[rotational_idx(@counter - 1)] end |
#nitems ⇒ Object
60 61 62 |
# File 'lib/bluepill/util/rotational_array.rb', line 60 def nitems compact.length end |
#pop ⇒ Object
21 22 23 |
# File 'lib/bluepill/util/rotational_array.rb', line 21 def pop raise "Cannot call pop on a rotational array" end |
#push(value) ⇒ Object Also known as: <<
11 12 13 14 15 16 17 |
# File 'lib/bluepill/util/rotational_array.rb', line 11 def push(value) idx = rotational_idx(@counter) self[idx] = value @counter += 1 self end |
#shift ⇒ Object
25 26 27 |
# File 'lib/bluepill/util/rotational_array.rb', line 25 def shift raise "Cannot call shift on a rotational array" end |
#unshift ⇒ Object
29 30 31 |
# File 'lib/bluepill/util/rotational_array.rb', line 29 def unshift raise "Cannot call unshift on a rotational array" end |