Added find() procedure
This commit is contained in:
parent
b90a38298f
commit
a3bcbc36e3
|
@ -36,4 +36,5 @@ export reversedPairs
|
|||
export clear
|
||||
export clearPop
|
||||
export extendLeft
|
||||
export maxSize
|
||||
export maxSize
|
||||
export find
|
||||
|
|
|
@ -80,7 +80,7 @@ proc maxSize*[T](self: LinkedDeque[T]): int =
|
|||
result = self.maxSize
|
||||
|
||||
|
||||
proc getNode[T](self: LinkedDeque[T], i: int): DequeNode[T] =
|
||||
proc getNode[T](self: LinkedDeque[T], i: int): DequeNode[T] {.raises: IndexDefect.} =
|
||||
## Low level method for indexing and getting
|
||||
## a node object back
|
||||
if self.high() == -1:
|
||||
|
@ -108,30 +108,30 @@ proc getNode[T](self: LinkedDeque[T], i: int): DequeNode[T] =
|
|||
dec(pos)
|
||||
|
||||
|
||||
proc `[]`*[T](self: LinkedDeque[T], i: int): T =
|
||||
proc `[]`*[T](self: LinkedDeque[T], i: int): T {.raises: IndexDefect.} =
|
||||
## Implements indexing into the queue
|
||||
result = self.getNode(i).val
|
||||
|
||||
|
||||
proc `[]=`*[T](self: LinkedDeque[T], i: int, val: T) =
|
||||
proc `[]=`*[T](self: LinkedDeque[T], i: int, val: T) {.raises: IndexDefect.} =
|
||||
## Sets element at position i
|
||||
## to the given value
|
||||
self[i].val = val
|
||||
|
||||
|
||||
proc `[]`*[T](self: LinkedDeque[T], i: BackwardsIndex): T =
|
||||
proc `[]`*[T](self: LinkedDeque[T], i: BackwardsIndex): T {.raises: IndexDefect.} =
|
||||
## Implements indexing into the queue
|
||||
## with backwards indeces
|
||||
result = self[self.size - int(i)]
|
||||
|
||||
|
||||
proc `[]=`*[T](self: LinkedDeque[T], i: BackwardsIndex, val: T) =
|
||||
proc `[]=`*[T](self: LinkedDeque[T], i: BackwardsIndex, val: T) {.raises: IndexDefect.} =
|
||||
## Sets element at backwards
|
||||
## position i to the given value
|
||||
self[self.size - int(i)] = val
|
||||
|
||||
|
||||
proc pop*[T](self: LinkedDeque[T], pos: int = 0): T =
|
||||
proc pop*[T](self: LinkedDeque[T], pos: int = 0): T {.raises: IndexDefect.} =
|
||||
## Pops an element off the deque
|
||||
## at the given index (default 0).
|
||||
## The queue is optimized for popping
|
||||
|
@ -155,7 +155,7 @@ proc pop*[T](self: LinkedDeque[T], pos: int = 0): T =
|
|||
dec(self.size)
|
||||
|
||||
|
||||
proc pop*[T](self: LinkedDeque[T], pos: BackwardsIndex): T =
|
||||
proc pop*[T](self: LinkedDeque[T], pos: BackwardsIndex): T {.raises: IndexDefect.} =
|
||||
## Same as self.pop but for backwards indeces
|
||||
result = self.pop(self.size - int(pos))
|
||||
|
||||
|
@ -207,17 +207,16 @@ proc addLeft*[T](self: LinkedDeque[T], val: T) =
|
|||
inc(self.size)
|
||||
|
||||
|
||||
proc insert*[T](self: LinkedDeque[T], pos: int, val: T) =
|
||||
proc insert*[T](self: LinkedDeque[T], pos: int, val: T) {.raises: IndexDefect.} =
|
||||
## Inserts the given value at the given
|
||||
## 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
|
||||
## the index gets to the middle of the deque. This
|
||||
## proc raises an IndexDefect if the queue's max
|
||||
## size is reached
|
||||
## "shifted" by 1 (this is not actually what happens,
|
||||
## but the result is analogous). The operation takes
|
||||
## constant time at the ends, but the complexity grows
|
||||
## to O(n) the closer the index gets to the middle of
|
||||
## the deque. This proc raises an IndexDefect if the
|
||||
## queue's max size is reached
|
||||
if self.maxSize > 0 and self.size == self.maxSize:
|
||||
raise newException(IndexDefect, &"LinkedDeque has reached its maximum size ({self.maxSize})")
|
||||
if pos == 0:
|
||||
|
@ -315,6 +314,16 @@ proc extendLeft*[T](self: LinkedDeque[T], other: seq[T]) =
|
|||
self.addLeft(item)
|
||||
|
||||
|
||||
proc find*[T](self: LinkedDeque[T], val: T): int =
|
||||
## Returns the first occurrence
|
||||
## of val in the queue. Returns -1
|
||||
## if the item isn't found
|
||||
for i, item in self:
|
||||
if item == val:
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
proc `$`*[T](self: LinkedDeque[T]): string =
|
||||
## Returns a string representation
|
||||
## of the deque
|
||||
|
|
Loading…
Reference in New Issue