Bounds and queue size checking can now be disabled

This commit is contained in:
Nocturn9x 2022-11-09 13:01:00 +01:00
parent eaaac53be0
commit 39a789b068
2 changed files with 10 additions and 6 deletions

View File

@ -107,6 +107,8 @@ queue.clearPop()
capabilities
- The objects in this module are **all** tracked references! (Unlike the `std/deques` module which implements them as value
types and gives `var` variants of each procedure)
- As with the data structure implemented in `std/deques`, all bounds checking is disabled when compiled with
`--checks:off` or `-d:danger`, but queue size checking is not. To disable queue size checking, pass `-d:noQueueSizeCheck`
## Disclaimer

View File

@ -83,10 +83,11 @@ proc maxSize*[T](self: LinkedDeque[T]): int =
proc getNode[T](self: LinkedDeque[T], i: int): DequeNode[T] {.raises: [IndexDefect, ValueError].} =
## Low level method for indexing and getting
## a node object back
if self.high() == -1:
raise newException(IndexDefect, "LinkedDeque is empty")
elif i > self.high() or i < 0:
raise newException(IndexDefect, &"{i} notin 0..{self.high()}")
when defined(boundChecks):
if self.high() == -1:
raise newException(IndexDefect, "LinkedDeque is empty")
elif i > self.high() or i < 0:
raise newException(IndexDefect, &"{i} notin 0..{self.high()}")
var pos = 0
if i < self.high() div 2:
# If we're taking an element
@ -217,8 +218,9 @@ proc insert*[T](self: LinkedDeque[T], pos: int, val: T) {.raises: [IndexDefect,
## 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})")
when not defined(noQueueSizeCheck):
if self.maxSize > 0 and self.size == self.maxSize:
raise newException(IndexDefect, &"LinkedDeque has reached its maximum size ({self.maxSize})")
if pos == 0:
self.addLeft(val)
else: