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
-
#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
-
#update_indexes(offset) ⇒ Array[Integer]
Offset the values of the stack.
Methods included from Mixins
tokens_to_wadler, upsize_circular_array
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 @config = config @empty = true @stack = Array.new(size) @top = 0 end |
Instance Method Details
#bottom ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/oppen/scan_stack.rb', line 39 def bottom if empty? raise 'Accessing empty stack from bottom' end @stack[@bottom] end |
#decrement(index) ⇒ Integer
Decrement index (no overflow).
61 62 63 |
# File 'lib/oppen/scan_stack.rb', line 61 def decrement(index) (index - 1) % length end |
#empty? ⇒ Boolean
20 21 22 |
# File 'lib/oppen/scan_stack.rb', line 20 def empty? @empty end |
#increment(index) ⇒ Integer
Increment index (no overflow).
52 53 54 |
# File 'lib/oppen/scan_stack.rb', line 52 def increment(index) (index + 1) % length end |
#length ⇒ Integer
25 26 27 |
# File 'lib/oppen/scan_stack.rb', line 25 def length @stack.length end |
#pop ⇒ Nil
Pop a value from the top.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/oppen/scan_stack.rb', line 87 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.
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/oppen/scan_stack.rb', line 104 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.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/oppen/scan_stack.rb', line 70 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
30 31 32 33 34 35 36 |
# File 'lib/oppen/scan_stack.rb', line 30 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.
123 124 125 126 127 |
# File 'lib/oppen/scan_stack.rb', line 123 def update_indexes(offset) @stack = @stack.map { |val| (val + offset) % length if val } end |