From 5608a4e57b521f2a31b35b4c7f905a87b6e7b0aa Mon Sep 17 00:00:00 2001 From: hlaaftana <10591326+hlaaftana@users.noreply.github.com> Date: Tue, 21 Apr 2020 23:09:19 +0300 Subject: [PATCH] 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 --- changelog.md | 1 + lib/pure/collections/deques.nim | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/changelog.md b/changelog.md index c30abab9f..69e7ebfb8 100644 --- a/changelog.md +++ b/changelog.md @@ -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`. diff --git a/lib/pure/collections/deques.nim b/lib/pure/collections/deques.nim index 77579a52b..1f34c3e18 100644 --- a/lib/pure/collections/deques.nim +++ b/lib/pure/collections/deques.nim @@ -51,6 +51,7 @@ ## * `lists module `_ for singly and doubly linked lists and rings ## * `channels module `_ 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)