Improve documentation for deques (#16589)

This commit is contained in:
konsumlamm 2021-01-06 20:16:26 +01:00 • committed by GitHub
commit 8a3b6190c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,25 +7,24 @@
# distribution, for details about the copyright. # distribution, for details about the copyright.
# #
## Implementation of a `deque`:idx: (double-ended queue). ## An implementation of a `deque`:idx: (double-ended queue).
## The underlying implementation uses a ``seq``. ## The underlying implementation uses a `seq`.
## ##
## None of the procs that get an individual value from the deque can be used ## Note that none of the procs that get an individual value from the deque should be used
## on an empty deque. ## on an empty deque.
## If compiled with `boundChecks` option, those procs will raise an `IndexDefect` ## 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 ## on such access. This should not be relied upon, as `-d:danger` or `--checks:off` will
## disable those checks and may return garbage or crash the program. ## 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 ## As such, a check to see if the deque is empty is needed before any
## access, unless your program logic guarantees it indirectly. ## access, unless your program logic guarantees it indirectly.
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40].toDeque
doAssertRaises(IndexDefect, echo a[0]) doAssertRaises(IndexDefect, echo a[4])
for i in 1 .. 5: a.addLast(50)
a.addLast(10*i)
assert $a == "[10, 20, 30, 40, 50]" assert $a == "[10, 20, 30, 40, 50]"
assert a.peekFirst == 10 assert a.peekFirst == 10
@ -44,7 +43,8 @@ runnableExamples:
a.shrink(fromFirst = 1, fromLast = 2) a.shrink(fromFirst = 1, fromLast = 2)
assert $a == "[22, 11, 20]" assert $a == "[22, 11, 20]"
## **See also:** ## See also
## ========
## * `lists module <lists.html>`_ for singly and doubly linked lists and rings ## * `lists module <lists.html>`_ for singly and doubly linked lists and rings
## * `channels module <channels.html>`_ for inter-thread communication ## * `channels module <channels.html>`_ for inter-thread communication
@ -54,9 +54,10 @@ import math
type type
Deque*[T] = object Deque*[T] = object
## A double-ended queue backed with a ringed seq buffer. ## A double-ended queue backed with a ringed `seq` buffer.
## ##
## To initialize an empty deque use `initDeque proc <#initDeque,int>`_. ## To initialize an empty deque,
## use the `initDeque proc <#initDeque,int>`_.
data: seq[T] data: seq[T]
head, tail, count, mask: int head, tail, count, mask: int
@ -65,7 +66,7 @@ const
template initImpl(result: typed, initialSize: int) = template initImpl(result: typed, initialSize: int) =
let correctSize = nextPowerOfTwo(initialSize) let correctSize = nextPowerOfTwo(initialSize)
result.mask = correctSize-1 result.mask = correctSize - 1
newSeq(result.data, correctSize) newSeq(result.data, correctSize)
template checkIfInitialized(deq: typed) = template checkIfInitialized(deq: typed) =
@ -73,27 +74,27 @@ template checkIfInitialized(deq: typed) =
if deq.mask == 0: if deq.mask == 0:
initImpl(deq, defaultInitialSize) initImpl(deq, defaultInitialSize)
proc initDeque*[T](initialSize: int = 4): Deque[T] = proc initDeque*[T](initialSize: int = defaultInitialSize): Deque[T] =
## Creates a new empty deque. ## Creates a new empty deque.
## ##
## Optionally, the initial capacity can be reserved via `initialSize` ## Optionally, the initial capacity can be reserved via `initialSize`
## as a performance optimization. ## as a performance optimization
## (default: `defaultInitialSize <#defaultInitialSize>`_).
## The length of a newly created deque will still be 0. ## The length of a newly created deque will still be 0.
## ##
## See also: ## **See also:**
## * `toDeque proc <#toDeque,openArray[T]>`_ ## * `toDeque proc <#toDeque,openArray[T]>`_
result.initImpl(initialSize) result.initImpl(initialSize)
proc toDeque*[T](x: openArray[T]): Deque[T] {.since: (1, 3).} = proc toDeque*[T](x: openArray[T]): Deque[T] {.since: (1, 3).} =
## Creates a new deque that contains the elements of `x` (in the same order). ## Creates a new deque that contains the elements of `x` (in the same order).
## ##
## See also: ## **See also:**
## * `initDeque proc <#initDeque,int>`_ ## * `initDeque proc <#initDeque,int>`_
runnableExamples: runnableExamples:
var a = toDeque([7, 8, 9]) let a = toDeque([7, 8, 9])
assert len(a) == 3 assert len(a) == 3
assert a.popFirst == 7 assert $a == "[7, 8, 9]"
assert len(a) == 2
result.initImpl(x.len) result.initImpl(x.len)
for item in items(x): for item in items(x):
@ -120,11 +121,9 @@ template xBoundsCheck(deq, i) =
"Out of bounds: " & $i & " < 0") "Out of bounds: " & $i & " < 0")
proc `[]`*[T](deq: Deque[T], i: Natural): T {.inline.} = proc `[]`*[T](deq: Deque[T], i: Natural): T {.inline.} =
## Accesses the i-th element of `deq`. ## Accesses the `i`-th element of `deq`.
runnableExamples: runnableExamples:
var a = initDeque[int]() let a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5:
a.addLast(10*i)
assert a[0] == 10 assert a[0] == 10
assert a[3] == 40 assert a[3] == 40
doAssertRaises(IndexDefect, echo a[8]) doAssertRaises(IndexDefect, echo a[8])
@ -133,25 +132,20 @@ proc `[]`*[T](deq: Deque[T], i: Natural): T {.inline.} =
return deq.data[(deq.head + i) and deq.mask] return deq.data[(deq.head + i) and deq.mask]
proc `[]`*[T](deq: var Deque[T], i: Natural): var T {.inline.} = proc `[]`*[T](deq: var Deque[T], i: Natural): var T {.inline.} =
## Accesses the i-th element of `deq` and return a mutable ## Accesses the `i`-th element of `deq` and returns a mutable
## reference to it. ## reference to it.
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5: inc(a[0])
a.addLast(10*i) assert a[0] == 11
assert a[0] == 10
assert a[3] == 40
doAssertRaises(IndexDefect, echo a[8])
xBoundsCheck(deq, i) xBoundsCheck(deq, i)
return deq.data[(deq.head + i) and deq.mask] return deq.data[(deq.head + i) and deq.mask]
proc `[]=`*[T](deq: var Deque[T], i: Natural, val: T) {.inline.} = proc `[]=`*[T](deq: var Deque[T], i: Natural, val: T) {.inline.} =
## Changes the i-th element of `deq`. ## Sets the `i`-th element of `deq` to `val`.
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5:
a.addLast(10*i)
a[0] = 99 a[0] = 99
a[3] = 66 a[3] = 66
assert $a == "[99, 20, 30, 66, 50]" assert $a == "[99, 20, 30, 66, 50]"
@ -161,13 +155,11 @@ proc `[]=`*[T](deq: var Deque[T], i: Natural, val: T) {.inline.} =
deq.data[(deq.head + i) and deq.mask] = val deq.data[(deq.head + i) and deq.mask] = val
proc `[]`*[T](deq: Deque[T], i: BackwardsIndex): T {.inline.} = proc `[]`*[T](deq: Deque[T], i: BackwardsIndex): T {.inline.} =
## Accesses the backwards indexed i-th element. ## Accesses the backwards indexed `i`-th element.
## ##
## `deq[^1]` is the last element. ## `deq[^1]` is the last element.
runnableExamples: runnableExamples:
var a = initDeque[int]() let a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5:
a.addLast(10*i)
assert a[^1] == 50 assert a[^1] == 50
assert a[^4] == 20 assert a[^4] == 20
doAssertRaises(IndexDefect, echo a[^9]) doAssertRaises(IndexDefect, echo a[^9])
@ -176,28 +168,24 @@ proc `[]`*[T](deq: Deque[T], i: BackwardsIndex): T {.inline.} =
return deq[deq.len - int(i)] return deq[deq.len - int(i)]
proc `[]`*[T](deq: var Deque[T], i: BackwardsIndex): var T {.inline.} = proc `[]`*[T](deq: var Deque[T], i: BackwardsIndex): var T {.inline.} =
## Accesses the backwards indexed i-th element. ## Accesses the backwards indexed `i`-th element and returns a mutable
## reference to it.
## ##
## `deq[^1]` is the last element. ## `deq[^1]` is the last element.
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5: inc(a[^1])
a.addLast(10*i) assert a[^1] == 51
assert a[^1] == 50
assert a[^4] == 20
doAssertRaises(IndexDefect, echo a[^9])
xBoundsCheck(deq, deq.len - int(i)) xBoundsCheck(deq, deq.len - int(i))
return deq[deq.len - int(i)] return deq[deq.len - int(i)]
proc `[]=`*[T](deq: var Deque[T], i: BackwardsIndex, x: T) {.inline.} = proc `[]=`*[T](deq: var Deque[T], i: BackwardsIndex, x: T) {.inline.} =
## Changes the backwards indexed i-th element. ## Sets the backwards indexed `i`-th element of `deq` to `x`.
## ##
## `deq[^1]` is the last element. ## `deq[^1]` is the last element.
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5:
a.addLast(10*i)
a[^1] = 99 a[^1] = 99
a[^3] = 77 a[^3] = 77
assert $a == "[10, 20, 77, 40, 99]" assert $a == "[10, 20, 77, 40, 99]"
@ -208,14 +196,15 @@ proc `[]=`*[T](deq: var Deque[T], i: BackwardsIndex, x: T) {.inline.} =
iterator items*[T](deq: Deque[T]): T = iterator items*[T](deq: Deque[T]): T =
## Yields every element of `deq`. ## Yields every element of `deq`.
##
## **See also:**
## * `mitems iterator <#mitems,Deque[T]>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() from sequtils import toSeq
for i in 1 .. 3:
a.addLast(10*i) let a = [10, 20, 30, 40, 50].toDeque
from sugar import collect assert toSeq(a.items) == @[10, 20, 30, 40, 50]
doAssert collect(for x in a: x) == [10, 20, 30]
# same as above:
doAssert collect(for x in items(a): x) == [10, 20, 30]
var i = deq.head var i = deq.head
for c in 0 ..< deq.count: for c in 0 ..< deq.count:
yield deq.data[i] yield deq.data[i]
@ -223,13 +212,14 @@ iterator items*[T](deq: Deque[T]): T =
iterator mitems*[T](deq: var Deque[T]): var T = iterator mitems*[T](deq: var Deque[T]): var T =
## Yields every element of `deq`, which can be modified. ## Yields every element of `deq`, which can be modified.
##
## **See also:**
## * `items iterator <#items,Deque[T]>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5:
a.addLast(10*i)
assert $a == "[10, 20, 30, 40, 50]" assert $a == "[10, 20, 30, 40, 50]"
for x in mitems(a): for x in mitems(a):
x = 5*x - 1 x = 5 * x - 1
assert $a == "[49, 99, 149, 199, 249]" assert $a == "[49, 99, 149, 199, 249]"
var i = deq.head var i = deq.head
@ -238,13 +228,13 @@ iterator mitems*[T](deq: var Deque[T]): var T =
i = (i + 1) and deq.mask i = (i + 1) and deq.mask
iterator pairs*[T](deq: Deque[T]): tuple[key: int, val: T] = iterator pairs*[T](deq: Deque[T]): tuple[key: int, val: T] =
## Yields every (position, value) of `deq`. ## Yields every `(position, value)`-pair of `deq`.
runnableExamples: runnableExamples:
var a = initDeque[int]() from sequtils import toSeq
for i in 1 .. 3:
a.addLast(10*i) let a = [10, 20, 30].toDeque
from sugar import collect assert toSeq(a.pairs) == @[(0, 10), (1, 20), (2, 30)]
doAssert collect(for k, v in pairs(a): (k, v)) == @[(0, 10), (1, 20), (2, 30)]
var i = deq.head var i = deq.head
for c in 0 ..< deq.count: for c in 0 ..< deq.count:
yield (c, deq.data[i]) yield (c, deq.data[i])
@ -253,13 +243,14 @@ iterator pairs*[T](deq: Deque[T]): tuple[key: int, val: T] =
proc contains*[T](deq: Deque[T], item: T): bool {.inline.} = proc contains*[T](deq: Deque[T], item: T): bool {.inline.} =
## Returns true if `item` is in `deq` or false if not found. ## Returns true if `item` is in `deq` or false if not found.
## ##
## Usually used via the ``in`` operator. ## Usually used via the `in` operator.
## It is the equivalent of ``deq.find(item) >= 0``. ## It is the equivalent of `deq.find(item) >= 0`.
runnableExamples: runnableExamples:
var q = [7, 9].toDeque let q = [7, 9].toDeque
assert 7 in q assert 7 in q
assert q.contains 7 assert q.contains(7)
assert 8 notin q assert 8 notin q
for e in deq: for e in deq:
if e == item: return true if e == item: return true
return false return false
@ -280,18 +271,14 @@ proc expandIfNeeded[T](deq: var Deque[T]) =
deq.head = 0 deq.head = 0
proc addFirst*[T](deq: var Deque[T], item: T) = proc addFirst*[T](deq: var Deque[T], item: T) =
## Adds an `item` to the beginning of the `deq`. ## Adds an `item` to the beginning of `deq`.
## ##
## See also: ## **See also:**
## * `addLast proc <#addLast,Deque[T],T>`_ ## * `addLast proc <#addLast,Deque[T],T>`_
## * `peekFirst proc <#peekFirst,Deque[T]>`_
## * `peekLast proc <#peekLast,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = initDeque[int]()
for i in 1 .. 5: for i in 1 .. 5:
a.addFirst(10*i) a.addFirst(10 * i)
assert $a == "[50, 40, 30, 20, 10]" assert $a == "[50, 40, 30, 20, 10]"
expandIfNeeded(deq) expandIfNeeded(deq)
@ -300,18 +287,14 @@ proc addFirst*[T](deq: var Deque[T], item: T) =
deq.data[deq.head] = item deq.data[deq.head] = item
proc addLast*[T](deq: var Deque[T], item: T) = proc addLast*[T](deq: var Deque[T], item: T) =
## Adds an `item` to the end of the `deq`. ## Adds an `item` to the end of `deq`.
## ##
## See also: ## **See also:**
## * `addFirst proc <#addFirst,Deque[T],T>`_ ## * `addFirst proc <#addFirst,Deque[T],T>`_
## * `peekFirst proc <#peekFirst,Deque[T]>`_
## * `peekLast proc <#peekLast,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = initDeque[int]()
for i in 1 .. 5: for i in 1 .. 5:
a.addLast(10*i) a.addLast(10 * i)
assert $a == "[10, 20, 30, 40, 50]" assert $a == "[10, 20, 30, 40, 50]"
expandIfNeeded(deq) expandIfNeeded(deq)
@ -322,16 +305,11 @@ proc addLast*[T](deq: var Deque[T], item: T) =
proc peekFirst*[T](deq: Deque[T]): T {.inline.} = proc peekFirst*[T](deq: Deque[T]): T {.inline.} =
## Returns the first element of `deq`, but does not remove it from the deque. ## Returns the first element of `deq`, but does not remove it from the deque.
## ##
## See also: ## **See also:**
## * `addFirst proc <#addFirst,Deque[T],T>`_ ## * `peekFirst proc <#peekFirst,Deque[T]_2>`_ which returns a mutable reference
## * `addLast proc <#addLast,Deque[T],T>`_
## * `peekLast proc <#peekLast,Deque[T]>`_ ## * `peekLast proc <#peekLast,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() let a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5:
a.addLast(10*i)
assert $a == "[10, 20, 30, 40, 50]" assert $a == "[10, 20, 30, 40, 50]"
assert a.peekFirst == 10 assert a.peekFirst == 10
assert len(a) == 5 assert len(a) == 5
@ -342,16 +320,11 @@ proc peekFirst*[T](deq: Deque[T]): T {.inline.} =
proc peekLast*[T](deq: Deque[T]): T {.inline.} = proc peekLast*[T](deq: Deque[T]): T {.inline.} =
## Returns the last element of `deq`, but does not remove it from the deque. ## Returns the last element of `deq`, but does not remove it from the deque.
## ##
## See also: ## **See also:**
## * `addFirst proc <#addFirst,Deque[T],T>`_ ## * `peekLast proc <#peekLast,Deque[T]_2>`_ which returns a mutable reference
## * `addLast proc <#addLast,Deque[T],T>`_
## * `peekFirst proc <#peekFirst,Deque[T]>`_ ## * `peekFirst proc <#peekFirst,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() let a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5:
a.addLast(10*i)
assert $a == "[10, 20, 30, 40, 50]" assert $a == "[10, 20, 30, 40, 50]"
assert a.peekLast == 50 assert a.peekLast == 50
assert len(a) == 5 assert len(a) == 5
@ -360,41 +333,31 @@ proc peekLast*[T](deq: Deque[T]): T {.inline.} =
result = deq.data[(deq.tail - 1) and deq.mask] result = deq.data[(deq.tail - 1) and deq.mask]
proc peekFirst*[T](deq: var Deque[T]): var T {.inline, since: (1, 3).} = proc peekFirst*[T](deq: var Deque[T]): var T {.inline, since: (1, 3).} =
## Returns the first element of `deq`, but does not remove it from the deque. ## Returns a mutable reference to the first element of `deq`,
## but does not remove it from the deque.
## ##
## See also: ## **See also:**
## * `addFirst proc <#addFirst,Deque[T],T>`_ ## * `peekFirst proc <#peekFirst,Deque[T]>`_
## * `addLast proc <#addLast,Deque[T],T>`_ ## * `peekLast proc <#peekLast,Deque[T]_2>`_
## * `peekLast proc <#peekLast,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5: a.peekFirst() = 99
a.addLast(10*i) assert $a == "[99, 20, 30, 40, 50]"
assert $a == "[10, 20, 30, 40, 50]"
assert a.peekFirst == 10
assert len(a) == 5
emptyCheck(deq) emptyCheck(deq)
result = deq.data[deq.head] result = deq.data[deq.head]
proc peekLast*[T](deq: var Deque[T]): var T {.inline, since: (1, 3).} = proc peekLast*[T](deq: var Deque[T]): var T {.inline, since: (1, 3).} =
## Returns the last element of `deq`, but does not remove it from the deque. ## Returns a mutable reference to the last element of `deq`,
## but does not remove it from the deque.
## ##
## See also: ## **See also:**
## * `addFirst proc <#addFirst,Deque[T],T>`_ ## * `peekFirst proc <#peekFirst,Deque[T]_2>`_
## * `addLast proc <#addLast,Deque[T],T>`_ ## * `peekLast proc <#peekLast,Deque[T]>`_
## * `peekFirst proc <#peekFirst,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5: a.peekLast() = 99
a.addLast(10*i) assert $a == "[10, 20, 30, 40, 99]"
assert $a == "[10, 20, 30, 40, 50]"
assert a.peekLast == 50
assert len(a) == 5
emptyCheck(deq) emptyCheck(deq)
result = deq.data[(deq.tail - 1) and deq.mask] result = deq.data[(deq.tail - 1) and deq.mask]
@ -406,17 +369,10 @@ proc popFirst*[T](deq: var Deque[T]): T {.inline, discardable.} =
## Removes and returns the first element of the `deq`. ## Removes and returns the first element of the `deq`.
## ##
## See also: ## See also:
## * `addFirst proc <#addFirst,Deque[T],T>`_
## * `addLast proc <#addLast,Deque[T],T>`_
## * `peekFirst proc <#peekFirst,Deque[T]>`_
## * `peekLast proc <#peekLast,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_ ## * `popLast proc <#popLast,Deque[T]>`_
## * `clear proc <#clear,Deque[T]>`_
## * `shrink proc <#shrink,Deque[T],int,int>`_ ## * `shrink proc <#shrink,Deque[T],int,int>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5:
a.addLast(10*i)
assert $a == "[10, 20, 30, 40, 50]" assert $a == "[10, 20, 30, 40, 50]"
assert a.popFirst == 10 assert a.popFirst == 10
assert $a == "[20, 30, 40, 50]" assert $a == "[20, 30, 40, 50]"
@ -430,18 +386,11 @@ proc popFirst*[T](deq: var Deque[T]): T {.inline, discardable.} =
proc popLast*[T](deq: var Deque[T]): T {.inline, discardable.} = proc popLast*[T](deq: var Deque[T]): T {.inline, discardable.} =
## Removes and returns the last element of the `deq`. ## Removes and returns the last element of the `deq`.
## ##
## See also: ## **See also:**
## * `addFirst proc <#addFirst,Deque[T],T>`_
## * `addLast proc <#addLast,Deque[T],T>`_
## * `peekFirst proc <#peekFirst,Deque[T]>`_
## * `peekLast proc <#peekLast,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_ ## * `popFirst proc <#popFirst,Deque[T]>`_
## * `clear proc <#clear,Deque[T]>`_
## * `shrink proc <#shrink,Deque[T],int,int>`_ ## * `shrink proc <#shrink,Deque[T],int,int>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5:
a.addLast(10*i)
assert $a == "[10, 20, 30, 40, 50]" assert $a == "[10, 20, 30, 40, 50]"
assert a.popLast == 50 assert a.popLast == 50
assert $a == "[10, 20, 30, 40]" assert $a == "[10, 20, 30, 40]"
@ -455,14 +404,11 @@ proc popLast*[T](deq: var Deque[T]): T {.inline, discardable.} =
proc clear*[T](deq: var Deque[T]) {.inline.} = proc clear*[T](deq: var Deque[T]) {.inline.} =
## Resets the deque so that it is empty. ## Resets the deque so that it is empty.
## ##
## See also: ## **See also:**
## * `clear proc <#clear,Deque[T]>`_
## * `shrink proc <#shrink,Deque[T],int,int>`_ ## * `shrink proc <#shrink,Deque[T],int,int>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5: assert $a == "[10, 20, 30, 40, 50]"
a.addFirst(10*i)
assert $a == "[50, 40, 30, 20, 10]"
clear(a) clear(a)
assert len(a) == 0 assert len(a) == 0
@ -477,15 +423,15 @@ proc shrink*[T](deq: var Deque[T], fromFirst = 0, fromLast = 0) =
## If the supplied number of elements exceeds the total number of elements ## If the supplied number of elements exceeds the total number of elements
## in the deque, the deque will remain empty. ## in the deque, the deque will remain empty.
## ##
## See also: ## **See also:**
## * `clear proc <#clear,Deque[T]>`_ ## * `clear proc <#clear,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples: runnableExamples:
var a = initDeque[int]() var a = [10, 20, 30, 40, 50].toDeque
for i in 1 .. 5: assert $a == "[10, 20, 30, 40, 50]"
a.addFirst(10*i)
assert $a == "[50, 40, 30, 20, 10]"
a.shrink(fromFirst = 2, fromLast = 1) a.shrink(fromFirst = 2, fromLast = 1)
assert $a == "[30, 20]" assert $a == "[30, 40]"
if fromFirst + fromLast > deq.count: if fromFirst + fromLast > deq.count:
clear(deq) clear(deq)
@ -503,6 +449,10 @@ proc shrink*[T](deq: var Deque[T], fromFirst = 0, fromLast = 0) =
proc `$`*[T](deq: Deque[T]): string = proc `$`*[T](deq: Deque[T]): string =
## Turns a deque into its string representation. ## Turns a deque into its string representation.
runnableExamples:
let a = [10, 20, 30].toDeque
assert $a == "[10, 20, 30]"
result = "[" result = "["
for x in deq: for x in deq:
if result.len > 1: result.add(", ") if result.len > 1: result.add(", ")