Class: Oppen::ScanStack
- Inherits:
-
Object
- Object
- Oppen::ScanStack
- 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
-
#decrement(index) ⇒ Integer
Decrement index (no overflow).
- #empty? ⇒ Boolean
-
#increment(index) ⇒ Integer
Increment index (no overflow).
-
#initialize(size, config) ⇒ ScanStack
constructor
A new instance of ScanStack.
- #length ⇒ Integer
-
#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
Constructor Details
#initialize(size, config) ⇒ ScanStack
Returns a new instance of ScanStack.
9 10 11 12 13 14 15 |
# File 'lib/oppen/scan_stack.rb', line 9 def initialize(size, config) @bottom = 0 @config = config @empty = true @stack = Array.new(size) @top = 0 end |
Instance Method Details
#bottom ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/oppen/scan_stack.rb', line 37 def bottom if empty? raise 'Accessing empty stack from bottom' end @stack[@bottom] end |
#decrement(index) ⇒ Integer
Decrement index (no overflow).
59 60 61 |
# File 'lib/oppen/scan_stack.rb', line 59 def decrement(index) (index - 1) % length end |
#empty? ⇒ Boolean
18 19 20 |
# File 'lib/oppen/scan_stack.rb', line 18 def empty? @empty end |
#increment(index) ⇒ Integer
Increment index (no overflow).
50 51 52 |
# File 'lib/oppen/scan_stack.rb', line 50 def increment(index) (index + 1) % length end |
#length ⇒ Integer
23 24 25 |
# File 'lib/oppen/scan_stack.rb', line 23 def length @stack.length end |
#pop ⇒ Nil
Pop a value from the top.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/oppen/scan_stack.rb', line 85 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.
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/oppen/scan_stack.rb', line 102 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/oppen/scan_stack.rb', line 68 def push(value) if empty? @empty = false else @top = increment(@top) if @top == @bottom raise 'Stack full' if !@config.upsize_stack? @stack, @bottom, @top = Utils.upsize_circular_array(@stack, @bottom) end end @stack[@top] = value end |
#top ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/oppen/scan_stack.rb', line 28 def top if empty? raise 'Accessing empty stack from top' end @stack[@top] end |