Add deques.peekFirst/Last(var Deque[T]) -> var T (#13542)

* Add deques.peekFirst/Last(var Deque[T]) -> var T
* Add changelog entry for deques.peekFirst/Last var T overloads
* Add since annotation to peekFirst/peekLast

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
hlaaftana
2020-04-21 23:09:19 +03:00
committed by GitHub
parent cb2b9bd797
commit 5608a4e57b
2 changed files with 42 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
- The default hash for `Ordinal` has changed to something more bit-scrambling.
`import hashes; proc hash(x: myInt): Hash = hashIdentity(x)` recovers the old
one in an instantiation context while `-d:nimIntHash1` recovers it globally.
- `deques.peekFirst` and `deques.peekLast` now have `var Deque[T] -> var T` overloads.
- File handles created from high-level abstractions in the stdlib will no longer
be inherited by child processes. In particular, these modules are affected:
`system`, `nativesockets`, `net` and `selectors`.

View File

@@ -51,6 +51,7 @@
## * `lists module <lists.html>`_ for singly and doubly linked lists and rings
## * `channels module <channels.html>`_ for inter-thread communication
include system/inclrtl
import math
@@ -363,6 +364,46 @@ proc peekLast*[T](deq: Deque[T]): T {.inline.} =
emptyCheck(deq)
result = deq.data[(deq.tail - 1) and deq.mask]
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.
##
## See also:
## * `addFirst proc <#addFirst,Deque[T],T>`_
## * `addLast proc <#addLast,Deque[T],T>`_
## * `peekLast proc <#peekLast,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples:
var a = initDeque[int]()
for i in 1 .. 5:
a.addLast(10*i)
assert $a == "[10, 20, 30, 40, 50]"
assert a.peekFirst == 10
assert len(a) == 5
emptyCheck(deq)
result = deq.data[deq.head]
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.
##
## See also:
## * `addFirst proc <#addFirst,Deque[T],T>`_
## * `addLast proc <#addLast,Deque[T],T>`_
## * `peekFirst proc <#peekFirst,Deque[T]>`_
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples:
var a = initDeque[int]()
for i in 1 .. 5:
a.addLast(10*i)
assert $a == "[10, 20, 30, 40, 50]"
assert a.peekLast == 50
assert len(a) == 5
emptyCheck(deq)
result = deq.data[(deq.tail - 1) and deq.mask]
template destroy(x: untyped) =
reset(x)