An implementation of a deque (double-ended queue). The underlying implementation uses a seq.
If compiled with the boundChecks option, those procs will raise an IndexDefect on such access. This should not be relied upon, as -d:danger or --checks:off will disable those checks and then the procs may return garbage or crash the program.
As such, a check to see if the deque is empty is needed before any access, unless your program logic guarantees it indirectly.
Example:
import std/deques var a = [10, 20, 30, 40].toDeque doAssertRaises(IndexDefect, echo a[4]) a.addLast(50) assert $a == "[10, 20, 30, 40, 50]" assert a.peekFirst == 10 assert a.peekLast == 50 assert len(a) == 5 assert a.popFirst == 10 assert a.popLast == 50 assert len(a) == 3 a.addFirst(11) a.addFirst(22) a.addFirst(33) assert $a == "[33, 22, 11, 20, 30, 40]" a.shrink(fromFirst = 1, fromLast = 2) assert $a == "[22, 11, 20]"
See also
- lists module for singly and doubly linked lists and rings
Types
Deque[T] = object
-
A double-ended queue backed with a ringed seq buffer.
To initialize an empty deque, use the initDeque proc.
Source Edit
Procs
proc `[]`[T](deq: Deque[T]; i: BackwardsIndex): lent T {.inline.}
-
Accesses the backwards indexed i-th element.
deq[^1] is the last element.
Example:
Source Editlet a = [10, 20, 30, 40, 50].toDeque assert a[^1] == 50 assert a[^4] == 20 doAssertRaises(IndexDefect, echo a[^9])
-
Accesses the i-th element of deq.
Example:
Source Editlet a = [10, 20, 30, 40, 50].toDeque assert a[0] == 10 assert a[3] == 40 doAssertRaises(IndexDefect, echo a[8])
proc `[]`[T](deq: var Deque[T]; i: BackwardsIndex): var T {.inline.}
-
Accesses the backwards indexed i-th element and returns a mutable reference to it.
deq[^1] is the last element.
Example:
Source Editvar a = [10, 20, 30, 40, 50].toDeque inc(a[^1]) assert a[^1] == 51
-
Creates a new empty deque.
Optionally, the initial capacity can be reserved via initialSize as a performance optimization (default: defaultInitialSize). The length of a newly created deque will still be 0.
See also:
Source Edit -
Returns the first element of deq, but does not remove it from the deque.
See also:
- peekFirst proc which returns a mutable reference
- peekLast proc
Example:
Source Editlet a = [10, 20, 30, 40, 50].toDeque assert $a == "[10, 20, 30, 40, 50]" assert a.peekFirst == 10 assert len(a) == 5
-
Returns the last element of deq, but does not remove it from the deque.
See also:
- peekLast proc which returns a mutable reference
- peekFirst proc
Example:
Source Editlet a = [10, 20, 30, 40, 50].toDeque assert $a == "[10, 20, 30, 40, 50]" assert a.peekLast == 50 assert len(a) == 5
-
Removes fromFirst elements from the front of the deque and fromLast elements from the back.
If the supplied number of elements exceeds the total number of elements in the deque, the deque will remain empty.
See also:
Example:
Source Editvar a = [10, 20, 30, 40, 50].toDeque assert $a == "[10, 20, 30, 40, 50]" a.shrink(fromFirst = 2, fromLast = 1) assert $a == "[30, 40]"