Exported maxSize proc, fixed insert() behavior with insertion at the last index

This commit is contained in:
Nocturn9x 2022-03-16 11:08:21 +01:00
parent 6f450cb5ca
commit 5f31aa2944
2 changed files with 9 additions and 11 deletions

View File

@ -35,4 +35,5 @@ export extend
export reversedPairs export reversedPairs
export clear export clear
export clearPop export clearPop
export extendLeft export extendLeft
export maxSize

View File

@ -19,7 +19,7 @@ import strformat
type type
DequeNode[T] = ref object DequeNode[T] = ref object
## Linked list node ## A doubly linked list node
next: DequeNode[T] next: DequeNode[T]
prev: DequeNode[T] prev: DequeNode[T]
val: T val: T
@ -85,7 +85,7 @@ proc getNode[T](self: LinkedDeque[T], i: int): DequeNode[T] =
## a node object back ## a node object back
if self.high() == -1: if self.high() == -1:
raise newException(IndexDefect, "LinkedDeque is empty") raise newException(IndexDefect, "LinkedDeque is empty")
elif i > self.high(): elif i > self.high() or i < 0:
raise newException(IndexDefect, &"{i} notin 0..{self.high()}") raise newException(IndexDefect, &"{i} notin 0..{self.high()}")
var pos = 0 var pos = 0
if i < self.high() div 2: if i < self.high() div 2:
@ -132,12 +132,12 @@ proc `[]=`*[T](self: LinkedDeque[T], i: BackwardsIndex, val: T) =
proc pop*[T](self: LinkedDeque[T], pos: int = 0): T = proc pop*[T](self: LinkedDeque[T], pos: int = 0): T =
## Pops an element off the queue ## Pops an element off the deque
## at the given index (default 0). ## at the given index (default 0).
## The queue is optimized for popping ## The queue is optimized for popping
## and appending in roughly constant time ## and appending in roughly constant time
## at both ends, so this is quite fast ## at both ends, so this is quite fast
## if the operation is nears the ends of ## if the operation is near the ends of
## the iterable. The popped item is returned ## the iterable. The popped item is returned
var node = self.getNode(pos) var node = self.getNode(pos)
if pos == 0: if pos == 0:
@ -209,10 +209,9 @@ proc addLeft*[T](self: LinkedDeque[T], val: T) =
proc insert*[T](self: LinkedDeque[T], pos: int, val: T) = proc insert*[T](self: LinkedDeque[T], pos: int, val: T) =
## Inserts the given value at the given ## Inserts the given value at the given
## position. When pos equals 0 or self.high(), ## position. When pos equals 0, this proc is equivalent
## this proc is equivalent to addLeft and add ## to addLeft. In all other cases, all items are
## respectively. In all other cases, all items ## "shifted" by 1 (shifted is in quotes because
## are "shifted" by 1 (shifted is in quotes because
## no shifting actually occurs, but the result is ## no shifting actually occurs, but the result is
## the same). The operation takes constant time at ## the same). The operation takes constant time at
## the ends, but the complexity grows to O(n) the closer ## the ends, but the complexity grows to O(n) the closer
@ -223,8 +222,6 @@ proc insert*[T](self: LinkedDeque[T], pos: int, val: T) =
raise newException(IndexDefect, &"LinkedDeque has reached its maximum size ({self.maxSize})") raise newException(IndexDefect, &"LinkedDeque has reached its maximum size ({self.maxSize})")
if pos == 0: if pos == 0:
self.addLeft(val) self.addLeft(val)
elif pos == self.high():
self.add(val)
else: else:
var node = self.getNode(pos) var node = self.getNode(pos)
var prev: DequeNode[T] = node.prev var prev: DequeNode[T] = node.prev