use sink and lent in deques (#17661)

* use sink and lent in deques

* Update lib/pure/collections/deques.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
flywind 2021-04-07 20:17:48 +08:00 • committed by GitHub
commit 834d5641e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 12 deletions

View file

@ -50,7 +50,7 @@ runnableExamples:
import std/private/since
import math
import std/math
type
Deque*[T] = object
@ -120,7 +120,7 @@ template xBoundsCheck(deq, i) =
raise newException(IndexDefect,
"Out of bounds: " & $i & " < 0")
proc `[]`*[T](deq: Deque[T], i: Natural): T {.inline.} =
proc `[]`*[T](deq: Deque[T], i: Natural): lent T {.inline.} =
## Accesses the `i`-th element of `deq`.
runnableExamples:
let a = [10, 20, 30, 40, 50].toDeque
@ -142,7 +142,7 @@ proc `[]`*[T](deq: var Deque[T], i: Natural): var T {.inline.} =
xBoundsCheck(deq, i)
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: sink T) {.inline.} =
## Sets the `i`-th element of `deq` to `val`.
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
@ -154,7 +154,7 @@ proc `[]=`*[T](deq: var Deque[T], i: Natural, val: T) {.inline.} =
xBoundsCheck(deq, i)
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): lent T {.inline.} =
## Accesses the backwards indexed `i`-th element.
##
## `deq[^1]` is the last element.
@ -180,7 +180,7 @@ proc `[]`*[T](deq: var Deque[T], i: BackwardsIndex): var T {.inline.} =
xBoundsCheck(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: sink T) {.inline.} =
## Sets the backwards indexed `i`-th element of `deq` to `x`.
##
## `deq[^1]` is the last element.
@ -194,7 +194,7 @@ proc `[]=`*[T](deq: var Deque[T], i: BackwardsIndex, x: T) {.inline.} =
xBoundsCheck(deq, deq.len - int(i))
deq[deq.len - int(i)] = x
iterator items*[T](deq: Deque[T]): T =
iterator items*[T](deq: Deque[T]): lent T =
## Yields every element of `deq`.
##
## **See also:**
@ -270,7 +270,7 @@ proc expandIfNeeded[T](deq: var Deque[T]) =
deq.tail = deq.count
deq.head = 0
proc addFirst*[T](deq: var Deque[T], item: T) =
proc addFirst*[T](deq: var Deque[T], item: sink T) =
## Adds an `item` to the beginning of `deq`.
##
## **See also:**
@ -286,7 +286,7 @@ proc addFirst*[T](deq: var Deque[T], item: T) =
deq.head = (deq.head - 1) and deq.mask
deq.data[deq.head] = item
proc addLast*[T](deq: var Deque[T], item: T) =
proc addLast*[T](deq: var Deque[T], item: sink T) =
## Adds an `item` to the end of `deq`.
##
## **See also:**
@ -302,7 +302,7 @@ proc addLast*[T](deq: var Deque[T], item: T) =
deq.data[deq.tail] = item
deq.tail = (deq.tail + 1) and deq.mask
proc peekFirst*[T](deq: Deque[T]): T {.inline.} =
proc peekFirst*[T](deq: Deque[T]): lent T {.inline.} =
## Returns the first element of `deq`, but does not remove it from the deque.
##
## **See also:**
@ -317,7 +317,7 @@ proc peekFirst*[T](deq: Deque[T]): T {.inline.} =
emptyCheck(deq)
result = deq.data[deq.head]
proc peekLast*[T](deq: Deque[T]): T {.inline.} =
proc peekLast*[T](deq: Deque[T]): lent T {.inline.} =
## Returns the last element of `deq`, but does not remove it from the deque.
##
## **See also:**