219 lines
7.3 KiB
Nim
219 lines
7.3 KiB
Nim
## A simple, dynamically-growing stack implementation
|
|
|
|
import strformat
|
|
import ../config
|
|
|
|
|
|
type Stack*[T] = object
|
|
## A stack for use in the
|
|
## peon runtime environment
|
|
container: ptr UncheckedArray[T]
|
|
capacity*: int
|
|
length: int
|
|
|
|
|
|
proc newStack*[T]: ptr Stack[T] =
|
|
## Allocates a new, empty stack
|
|
## with a starting capacity of 8
|
|
result = cast[ptr Stack[T]](alloc(sizeof(Stack)))
|
|
result.capacity = 8
|
|
result.container = cast[ptr UncheckedArray[T]](alloc(sizeof(T) * 8))
|
|
result.length = 0
|
|
|
|
|
|
proc push*[T](self: ptr Stack[T], elem: T) =
|
|
## Pushes an object onto the stack
|
|
if self.capacity <= self.length:
|
|
self.capacity *= HeapGrowFactor
|
|
self.container = cast[ptr UncheckedArray[T]](realloc(self.container, self.capacity))
|
|
self.container[self.length] = elem
|
|
self.length += 1
|
|
|
|
|
|
proc pop*[T](self: ptr Stack[T], idx: int = -1): T =
|
|
## Pops an item off the stack. By default, the last
|
|
## element is popped, in which case the operation's
|
|
## time complexity is O(1). When an arbitrary element
|
|
## is popped, the complexity rises to O(k) where k
|
|
## is the number of elements that had to be shifted
|
|
## by 1 to avoid empty slots
|
|
var idx = idx
|
|
if self.length == 0:
|
|
raise newException(IndexDefect, "pop from empty Stack")
|
|
if idx == -1:
|
|
idx = self.length - 1
|
|
if idx notin 0..self.length - 1:
|
|
raise newException(IndexDefect, &"Stack index out of bounds: {idx} notin 0..{self.length - 1}")
|
|
result = self.container[idx]
|
|
if idx != self.length - 1:
|
|
for i in countup(idx, self.length - 1):
|
|
self.container[i] = self.container[i + 1]
|
|
self.capacity -= 1
|
|
self.length -= 1
|
|
|
|
|
|
proc pop*[T](self: ptr Stack[T], idx: uint64): T =
|
|
## Pops an item off the stack
|
|
var idx = idx
|
|
if self.length == 0:
|
|
raise newException(IndexDefect, "pop from empty Stack")
|
|
if idx notin 0'u64..uint64(self.length - 1):
|
|
raise newException(IndexDefect, &"Stack index out of bounds: {idx} notin 0..{self.length - 1}")
|
|
result = self.container[idx]
|
|
if idx != uint64(self.length - 1):
|
|
for i in countup(idx, uint64(self.length - 1)):
|
|
self.container[i] = self.container[i + 1]
|
|
self.capacity -= 1
|
|
self.length -= 1
|
|
|
|
|
|
proc `[]`*[T](self: ptr Stack[T], idx: int): T =
|
|
## Retrieves an item from the stack, in constant
|
|
## time
|
|
if self.length == 0:
|
|
raise newException(IndexDefect, &"Stack index out of bounds: : {idx} notin 0..{self.length - 1}")
|
|
if idx notin 0..self.length - 1:
|
|
raise newException(IndexDefect, &"Stack index out of bounds: {idx} notin 0..{self.length - 1}")
|
|
result = self.container[idx]
|
|
|
|
|
|
proc `[]`*[T](self: ptr Stack[T], idx: uint64): T =
|
|
## Retrieves an item from the stack, in constant
|
|
## time
|
|
if self.length == 0:
|
|
raise newException(IndexDefect, &"Stack index out of bounds: : {idx} notin 0..{self.length - 1}")
|
|
if idx notin 0'u64..uint64(self.length - 1):
|
|
raise newException(IndexDefect, &"Stack index out of bounds: {idx} notin 0..{self.length - 1}")
|
|
result = self.container[idx]
|
|
|
|
|
|
proc `[]`*[T](self: ptr Stack[T], idx: BackwardsIndex): T =
|
|
## Retrieves an item from the stack, in constant
|
|
## time
|
|
result = self[self.len() - int(idx)]
|
|
|
|
|
|
proc `[]`*[T](self: ptr Stack[T], slice: Hslice[int, int]): ptr Stack[T] =
|
|
## Retrieves a subset of the stack, in O(k) time where k is the size
|
|
## of the slice
|
|
if self.length == 0:
|
|
raise newException(IndexDefect, "Stack index out of bounds")
|
|
if slice.a notin 0..self.length - 1 or slice.b notin 0..self.length:
|
|
raise newException(IndexDefect, "Stack index out of bounds")
|
|
result = newStack()
|
|
for i in countup(slice.a, slice.b - 1):
|
|
result.push(self.container[i])
|
|
|
|
|
|
proc `[]=`*[T](self: ptr Stack[T], idx: int, obj: T) =
|
|
## Assigns an object to the given index, in constant
|
|
## time
|
|
if self.length == 0:
|
|
raise newException(IndexDefect, "Stack is empty")
|
|
if idx notin 0..self.length - 1:
|
|
raise newException(IndexDefect, "Stack index out of bounds")
|
|
self.container[idx] = obj
|
|
|
|
|
|
proc `[]=`*[T](self: ptr Stack[T], idx: uint64, obj: T) =
|
|
## Assigns an object to the given index, in constant
|
|
## time
|
|
if self.length == 0:
|
|
raise newException(IndexDefect, "Stack is empty")
|
|
if idx notin 0'u64..uint64(self.length - 1):
|
|
raise newException(IndexDefect, "Stack index out of bounds")
|
|
self.container[idx] = obj
|
|
|
|
|
|
proc delete*[T](self: ptr Stack[T], idx: int) =
|
|
## Deletes an object from the given index.
|
|
## uint64his method shares the time complexity
|
|
## of self.pop()
|
|
if self.length == 0:
|
|
raise newException(IndexDefect, "delete from empty Stack")
|
|
if idx notin 0..self.length - 1:
|
|
raise newException(IndexDefect, &"Stack index out of bounds: {idx} notin 0..{self.length - 1}")
|
|
discard self.pop(idx)
|
|
|
|
|
|
proc delete*[T](self: ptr Stack[T], idx: uint64) =
|
|
## Deletes an object from the given index.
|
|
## uint64his method shares the time complexity
|
|
## of self.pop()
|
|
if self.length == 0:
|
|
raise newException(IndexDefect, "delete from empty Stack")
|
|
if idx notin 0'u64..uint64(self.length - 1):
|
|
raise newException(IndexDefect, &"Stack index out of bounds: {idx} notin 0..{self.length - 1}")
|
|
discard self.pop(idx)
|
|
|
|
|
|
proc contains*[T](self: ptr Stack[T], elem: T): bool =
|
|
## Returns true if the given object is present
|
|
## in the list, false otherwise. O(n) complexity
|
|
if self.length > 0:
|
|
for i in 0..self.length - 1:
|
|
if self[i] == elem:
|
|
return true
|
|
return false
|
|
|
|
|
|
proc high*[T](self: ptr Stack[T]): int =
|
|
## Returns the index of the last
|
|
## element in the list, in constant time
|
|
result = self.length - 1
|
|
|
|
|
|
proc len*[T](self: ptr Stack[T]): int =
|
|
## Returns the length of the list
|
|
## in constant time
|
|
result = self.length
|
|
|
|
|
|
iterator pairs*[T](self: ptr Stack[T]): tuple[key: int, val: T] =
|
|
## Implements pairwise iteration (similar to python's enumerate)
|
|
for i in countup(0, self.length - 1):
|
|
yield (key: i, val: self[i])
|
|
|
|
|
|
iterator items*[T](self: ptr Stack[T]): T =
|
|
## Implements iteration
|
|
for i in countup(0, self.length - 1):
|
|
yield self[i]
|
|
|
|
|
|
proc reversed*[T](self: ptr Stack[T], first: int = -1, last: int = 0): ptr Stack[T] =
|
|
## Returns a reversed version of the given stack, from first to last.
|
|
## First defaults to -1 (the end of the list) and last defaults to 0 (the
|
|
## beginning of the list)
|
|
var first = first
|
|
if first == -1:
|
|
first = self.length - 1
|
|
result = newStack()
|
|
for i in countdown(first, last):
|
|
result.push(self[i])
|
|
|
|
|
|
proc extend*[T](self: ptr Stack[T], other: seq[T]) =
|
|
## Iteratively calls self.push() with the elements
|
|
## from a nim sequence
|
|
for elem in other:
|
|
self.push(elem)
|
|
|
|
|
|
proc extend*[T](self: ptr Stack[T], other: ptr Stack[T]) =
|
|
## Iteratively calls self.push() with the elements
|
|
## from another Stack
|
|
for elem in other:
|
|
self.push(elem)
|
|
|
|
|
|
proc `$`*[T](self: ptr Stack[T]): string =
|
|
## Returns a string representation
|
|
## of self
|
|
result = "["
|
|
if self.length > 0:
|
|
for i in 0..self.length - 1:
|
|
result = result & $self.container[i]
|
|
if i < self.length - 1:
|
|
result = result & ", "
|
|
result = result & "]" |