Class: Oppen::ScanStack
- Inherits:
-
Object
- Object
- Oppen::ScanStack
- Extended by:
- Mixins
- Defined in:
- lib/oppen/scan_stack.rb
Overview
A fixed-size stack that can be popped from top and bottom.
Instance Method Summary collapse
-
#bottom ⇒ Object
The bottom element of the stack.
-
#decrement(index) ⇒ Integer
Decrement index (no overflow).
-
#empty? ⇒ Boolean
Whether the stack is empty.
-
#increment(index) ⇒ Integer
Increment index (no overflow).
-
#initialize(size, config) ⇒ ScanStack
constructor
A new instance of ScanStack.
-
#length ⇒ Integer
The current length of the stack.
-
#pop ⇒ Nil
Pop a value from the top.
-
#pop_bottom ⇒ Nil
Pop a value from the bottom.
-
#push(value) ⇒ Nil
Push a value to the top.
-
#top ⇒ Object
The top element of the stack.
-
#update_indexes(offset) ⇒ Array<Integer>
Offset the values of the stack.
Methods included from Mixins
Constructor Details
#initialize(size, config) ⇒ ScanStack
Returns a new instance of ScanStack.
11 12 13 14 15 16 17 |
# File 'lib/oppen/scan_stack.rb', line 11 def initialize(size, config) @bottom = 0 # Points to the bottom of the stack. @config = config # Printing config. @empty = true # Emptiness flag. @stack = Array.new size # The fixed sized stack. @top = 0 # Points to the top of the stack. end |
Instance Method Details
#bottom ⇒ Object
The bottom element of the stack.
49 50 51 52 53 54 55 |
# File 'lib/oppen/scan_stack.rb', line 49 def bottom if empty? raise 'Accessing empty stack from bottom' end @stack[@bottom] end |
#decrement(index) ⇒ Integer
Decrement index (no overflow).
71 72 73 |
# File 'lib/oppen/scan_stack.rb', line 71 def decrement(index) (index - 1) % length end |
#empty? ⇒ Boolean
Whether the stack is empty.
22 |
# File 'lib/oppen/scan_stack.rb', line 22 def empty? = @empty |
#increment(index) ⇒ Integer
Increment index (no overflow).
62 63 64 |
# File 'lib/oppen/scan_stack.rb', line 62 def increment(index) (index + 1) % length end |
#length ⇒ Integer
The current length of the stack.
27 |
# File 'lib/oppen/scan_stack.rb', line 27 def length = @stack.length |
#pop ⇒ Nil
Pop a value from the top.
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/oppen/scan_stack.rb', line 104 def pop if empty? raise 'Popping empty stack from top' end res = top if @top == @bottom @empty = true else @top = decrement @top end res end |
#pop_bottom ⇒ Nil
Pop a value from the bottom.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/oppen/scan_stack.rb', line 124 def pop_bottom if empty? raise 'Popping empty stack from bottom' end res = bottom if @top == @bottom @empty = true else @bottom = increment @bottom end res end |
#push(value) ⇒ Nil
Push a value to the top.
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/oppen/scan_stack.rb', line 84 def push(value) if empty? @empty = false else @top = increment @top if @top == @bottom raise 'Stack full' if !@config.upsize_stack? @stack, @bottom, @top = ScanStack.upsize_circular_array @stack, @bottom end end @stack[@top] = value end |
#top ⇒ Object
The top element of the stack.
35 36 37 38 39 40 41 |
# File 'lib/oppen/scan_stack.rb', line 35 def top if empty? raise 'Accessing empty stack from top' end @stack[@top] end |
#update_indexes(offset) ⇒ Array<Integer>
Offset the values of the stack.
143 144 145 146 147 |
# File 'lib/oppen/scan_stack.rb', line 143 def update_indexes(offset) @stack = @stack.map { |val| (val + offset) % length if val } end |