[other] prettify collections (#11695)

This commit is contained in:
Miran 2019-07-09 22:45:23 +02:00 • committed by GitHub
commit 2255d8795b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 209 additions and 186 deletions

View file

@ -89,15 +89,15 @@ template emptyCheck(deq) =
template xBoundsCheck(deq, i) =
# Bounds check for the array like accesses.
when compileOption("boundChecks"): # d:release should disable this.
if unlikely(i >= deq.count): # x < deq.low is taken care by the Natural parameter
when compileOption("boundChecks"): # d:release should disable this.
if unlikely(i >= deq.count): # x < deq.low is taken care by the Natural parameter
raise newException(IndexError,
"Out of bounds: " & $i & " > " & $(deq.count - 1))
if unlikely(i < 0): # when used with BackwardsIndex
if unlikely(i < 0): # when used with BackwardsIndex
raise newException(IndexError,
"Out of bounds: " & $i & " < 0")
proc `[]`*[T](deq: Deque[T], i: Natural) : T {.inline.} =
proc `[]`*[T](deq: Deque[T], i: Natural): T {.inline.} =
## Access the i-th element of `deq`.
runnableExamples:
var a = initDeque[int]()
@ -124,7 +124,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: T) {.inline.} =
## Change the i-th element of `deq`.
runnableExamples:
var a = initDeque[int]()
@ -259,7 +259,7 @@ proc expandIfNeeded[T](deq: var Deque[T]) =
var cap = deq.mask + 1
if unlikely(deq.count >= cap):
var n = newSeq[T](cap * 2)
for i, x in pairs(deq): # don't use copyMem because the GC and because it's slower.
for i, x in pairs(deq): # don't use copyMem because the GC and because it's slower.
shallowCopy(n[i], x)
shallowCopy(deq.data, n)
deq.mask = cap * 2 - 1
@ -306,7 +306,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]): T {.inline.} =
## Returns the first element of `deq`, but does not remove it from the deque.
##
## See also:
@ -547,9 +547,9 @@ when isMainModule:
assert deq.popFirst() > 0
#foo(0,0)
foo(8,5)
foo(10,9)
foo(1,1)
foo(2,1)
foo(1,5)
foo(3,2)
foo(8, 5)
foo(10, 9)
foo(1, 1)
foo(2, 1)
foo(1, 5)
foo(3, 2)