Class: Oppen::ScanStack

Inherits:
Object
  • Object
show all
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

Methods included from Mixins

tokens_to_wadler

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

#bottomObject

The bottom element of the stack.

Returns:

  • (Object)

Raises:

  • (RuntimeError)

    when accessing empty 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).

Parameters:

  • index (Integer)

Returns:

  • (Integer)


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.

Returns:

  • (Boolean)


22
# File 'lib/oppen/scan_stack.rb', line 22

def empty? = @empty

#increment(index) ⇒ Integer

Increment index (no overflow).

Parameters:

  • index (Integer)

Returns:

  • (Integer)


62
63
64
# File 'lib/oppen/scan_stack.rb', line 62

def increment(index)
  (index + 1) % length
end

#lengthInteger

The current length of the stack.

Returns:

  • (Integer)


27
# File 'lib/oppen/scan_stack.rb', line 27

def length = @stack.length

#popNil

Pop a value from the top.

Returns:

  • (Nil)

Raises:

  • (RuntimeError)

    when accessing empty stack.



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_bottomNil

Pop a value from the bottom.

Returns:

  • (Nil)

Raises:

  • (RuntimeError)

    when accessing empty stack.



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.

Parameters:

  • value (Object)

Returns:

  • (Nil)

Raises:

  • (RuntimeError)

    when the stack is full and the upsize_stack flag is not activated in Config.



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

#topObject

The top element of the stack.

Returns:

  • (Object)

Raises:

  • (RuntimeError)

    when accessing empty 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.

Parameters:

  • offset (Integer)

Returns:

  • (Array<Integer>)


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