diff --git a/src/nimdeque.nim b/src/nimdeque.nim index 768d054..e364e29 100644 --- a/src/nimdeque.nim +++ b/src/nimdeque.nim @@ -35,4 +35,5 @@ export extend export reversedPairs export clear export clearPop -export extendLeft \ No newline at end of file +export extendLeft +export maxSize \ No newline at end of file diff --git a/src/private/queues/linked.nim b/src/private/queues/linked.nim index 6348876..23b1e13 100644 --- a/src/private/queues/linked.nim +++ b/src/private/queues/linked.nim @@ -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