From 34df046d37f7190834e1807dc4766c7e05b6c431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nihlg=C3=A5rd?= Date: Wed, 4 Apr 2018 13:02:14 +0200 Subject: [PATCH] Fix algorithm.fill for empty input --- lib/pure/algorithm.nim | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index 8b6d6c06e9..45e031574e 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -24,16 +24,20 @@ proc `*`*(x: int, order: SortOrder): int {.inline.} = var y = order.ord - 1 result = (x xor y) - y -proc fill*[T](a: var openArray[T], first, last: Natural, value: T) = - ## fills the array ``a[first..last]`` with `value`. +template fillImpl[T](a: var openArray[T], first, last: int, value: T) = var x = first while x <= last: a[x] = value inc(x) +proc fill*[T](a: var openArray[T], first, last: Natural, value: T) = + ## fills the array ``a[first..last]`` with `value`. + fillImpl(a, first, last, value) + proc fill*[T](a: var openArray[T], value: T) = ## fills the array `a` with `value`. - fill(a, 0, a.high, value) + fillImpl(a, 0, a.high, value) + proc reverse*[T](a: var openArray[T], first, last: Natural) = ## reverses the array ``a[first..last]``. @@ -507,3 +511,7 @@ when isMainModule: doAssert lowerBound([1,2,4], 3, system.cmp[int]) == 2 doAssert lowerBound([1,2,2,3], 4, system.cmp[int]) == 4 doAssert lowerBound([1,2,3,10], 11) == 4 + + block fillEmptySeq: + var s = newSeq[int]() + s.fill(0) \ No newline at end of file