Exported maxSize proc, fixed insert() behavior with insertion at the last index
This commit is contained in:
parent
6f450cb5ca
commit
5f31aa2944
|
@ -35,4 +35,5 @@ export extend
|
|||
export reversedPairs
|
||||
export clear
|
||||
export clearPop
|
||||
export extendLeft
|
||||
export extendLeft
|
||||
export maxSize
|
|
@ -19,7 +19,7 @@ import strformat
|
|||
|
||||
type
|
||||
DequeNode[T] = ref object
|
||||
## Linked list node
|
||||
## A doubly linked list node
|
||||
next: DequeNode[T]
|
||||
prev: DequeNode[T]
|
||||
val: T
|
||||
|
@ -85,7 +85,7 @@ proc getNode[T](self: LinkedDeque[T], i: int): DequeNode[T] =
|
|||
## a node object back
|
||||
if self.high() == -1:
|
||||
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()}")
|
||||
var pos = 0
|
||||
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 =
|
||||
## Pops an element off the queue
|
||||
## Pops an element off the deque
|
||||
## at the given index (default 0).
|
||||
## The queue is optimized for popping
|
||||
## and appending in roughly constant time
|
||||
## 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
|
||||
var node = self.getNode(pos)
|
||||
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) =
|
||||
## Inserts the given value at the given
|
||||
## position. When pos equals 0 or self.high(),
|
||||
## this proc is equivalent to addLeft and add
|
||||
## respectively. In all other cases, all items
|
||||
## are "shifted" by 1 (shifted is in quotes because
|
||||
## position. When pos equals 0, this proc is equivalent
|
||||
## to addLeft. In all other cases, all items are
|
||||
## "shifted" by 1 (shifted is in quotes because
|
||||
## no shifting actually occurs, but the result is
|
||||
## the same). The operation takes constant time at
|
||||
## 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})")
|
||||
if pos == 0:
|
||||
self.addLeft(val)
|
||||
elif pos == self.high():
|
||||
self.add(val)
|
||||
else:
|
||||
var node = self.getNode(pos)
|
||||
var prev: DequeNode[T] = node.prev
|
||||
|
|
Loading…
Reference in New Issue