src/multistacks

  Source   Edit

Types

MultiStack[T] = ref object
  tops: seq[MultiStackNode[T]]
  height: Natural
  Source   Edit

Procs

func height[T](stack: MultiStack[T]): Natural
Gets the height of the stack.   Source   Edit
func isEmpty[T](stack: MultiStack[T]): bool
Checks if the stack is empty.

Example:

var stack = newMultiStack[int]()

assert stack.isEmpty()

stack.push([@[1, 2, 3]])
assert not stack.isEmpty()

discard stack.pop(0)
assert stack.isEmpty()
  Source   Edit
proc newMultiStack[T](): MultiStack[T]
Creates a new empty stack.   Source   Edit
func peek[T](stack: MultiStack[T]): seq[T]
Gets the top values of the stack.   Source   Edit
proc pop[T](stack: var MultiStack[T]; topIndex: Natural): T
Pops the top value of the given top index.

Example:

var stack = newMultiStack[int]()

stack.push([@[0, 1, 2]])
assert stack.pop(0) == 0
assert stack.peek().len == 0
assert stack.height == 0

stack.push([@[0, 1, 2]])
stack.push([@[3, 4], @[], @[5]])
stack.push([@[], @[6, 7, 8], @[], @[9]])
assert stack.pop(3) == 8
assert stack.peek() == @[3, 4, 1, 5]
assert stack.height == 2

assert stack.pop(2) == 1
assert stack.peek().len == 0
assert stack.height == 0
  Source   Edit
proc popSingle[T](stack: var MultiStack[T]): seq[T]
Pops the top values of the stack. This procedure always reduces the height of the stack by 1. If the stack is empty, an empty sequence is returned.

Example:

var stack = newMultiStack[int]()

stack.push([@[0, 1, 2]])
stack.push([@[3, 4], @[], @[5]])
stack.push([@[], @[6, 7, 8], @[], @[9]])
assert stack.popSingle() == @[3, 6, 7, 8, 1, 9]
assert stack.height == 2

assert stack.popSingle() == @[3, 4, 1, 5]
assert stack.height == 1

assert stack.popSingle() == @[0, 1, 2]
assert stack.height == 0

assert stack.popSingle().len == 0
  Source   Edit
proc push[T](stack: var MultiStack[T]; values: openArray[T];
             onTopIndices: openArray[Natural])
Pushes the given values on the given tops of the stack.

Example:

var stack = newMultiStack[int]()

stack.push([1, 2, 3], [0.Natural, 0, 0])
assert stack.peek() == @[1, 2, 3]
assert stack.height == 1

stack.push([4, 5, 6], [0.Natural, 0, 2])
assert stack.peek() == @[4, 5, 2, 6]
assert stack.height == 2

stack.push([7, 8, 9, 10], [1.Natural, 1, 1, 3])
assert stack.peek() == @[4, 7, 8, 9, 2, 10]
assert stack.height == 3
  Source   Edit
proc push[T](stack: var MultiStack[T]; valuesByTopIndex: openArray[seq[T]])
Pushes the given values on each tops of the stack.

Example:

var stack = newMultiStack[int]()

stack.push([@[1, 2, 3]])
assert stack.peek() == @[1, 2, 3]
assert stack.height == 1

stack.push([@[4, 5], @[], @[6]])
assert stack.peek() == @[4, 5, 2, 6]
assert stack.height == 2

stack.push([@[], @[7, 8, 9], @[], @[10]])
assert stack.peek() == @[4, 7, 8, 9, 2, 10]
assert stack.height == 3
  Source   Edit