Class: Bluepill::Util::RotationalArray

Inherits:
Array
  • Object
show all
Defined in:
lib/bluepill/util/rotational_array.rb

Instance Method Summary collapse

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

#clearObject



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

#firstObject



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

#lastObject



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

#popObject



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

#shiftObject



25
26
27
# File 'lib/bluepill/util/rotational_array.rb', line 25

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

#unshiftObject



29
30
31
# File 'lib/bluepill/util/rotational_array.rb', line 29

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