Class: Algorithmable::Cups::StacksAndQueues::TripleStack

Inherits:
Object
  • Object
show all
Defined in:
lib/algorithmable/cups/stacks_and_queues/triple_stack.rb

Constant Summary collapse

MAX_INT =
2**32

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ TripleStack

Returns a new instance of TripleStack.


7
8
9
10
11
12
13
14
# File 'lib/algorithmable/cups/stacks_and_queues/triple_stack.rb', line 7

def initialize(size)
  @size = size + 1
  @pointers = [0, 0, 0]
  @array = Array.new (@size * 3)
  0.upto(2).each do |index|
    @array[@size * index] = MAX_INT
  end
end

Instance Method Details

#empty?(stack_num) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
# File 'lib/algorithmable/cups/stacks_and_queues/triple_stack.rb', line 36

def empty?(stack_num)
  @pointers[stack_num].zero?
end

#inspectObject


40
41
42
# File 'lib/algorithmable/cups/stacks_and_queues/triple_stack.rb', line 40

def inspect
  "#<Stack @pointers=#{@pointers} @stacks=#{@array}>"
end

#peek(stack_num) ⇒ Object


31
32
33
34
# File 'lib/algorithmable/cups/stacks_and_queues/triple_stack.rb', line 31

def peek(stack_num)
  pointer = get_stack_pointer(stack_num)
  @array[pointer]
end

#pop(stack_num) ⇒ Object


23
24
25
26
27
28
29
# File 'lib/algorithmable/cups/stacks_and_queues/triple_stack.rb', line 23

def pop(stack_num)
  pointer = get_stack_pointer(stack_num)
  value = @array[pointer]
  @array[pointer] = nil
  @pointers[stack_num] -= 1
  value
end

#push(stack_num, item) ⇒ Object


16
17
18
19
20
21
# File 'lib/algorithmable/cups/stacks_and_queues/triple_stack.rb', line 16

def push(stack_num, item)
  pointer = get_stack_pointer(stack_num) + 1
  @pointers[stack_num] += 1
  @array[pointer] = item
  item
end