diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2014-07-15 19:10:19 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2014-07-15 19:10:19 +0200 |
commit | e0404609436eafdd7da79166fdb88a09d4f16451 (patch) | |
tree | 76172c2f7aa1696a08084a05373bc6a218fbeaeb /lib/pure/algorithm.nim | |
parent | 45bf087ce72e6ad703ebd97a91f844423738b956 (diff) | |
parent | ce3d1b302ebf30d93697be9c85112350bb9e4eb0 (diff) | |
download | Nim-e0404609436eafdd7da79166fdb88a09d4f16451.tar.gz |
Merge pull request #1341 from def-/algorithm-reversed
Add reversed proc
Diffstat (limited to 'lib/pure/algorithm.nim')
-rw-r--r-- | lib/pure/algorithm.nim | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index 86d329763..89c83a8a4 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -34,6 +34,20 @@ proc reverse*[T](a: var openArray[T]) = ## reverses the array `a`. reverse(a, 0, a.high) +proc reversed*[T](a: openArray[T], first, last: int): seq[T] = + ## returns the reverse of the array `a[first..last]`. + result = newSeq[T](last - first + 1) + var x = first + var y = last + while x <= last: + result[x] = a[y] + dec(y) + inc(x) + +proc reversed*[T](a: openArray[T]): seq[T] = + ## returns the reverse of the array `a`. + reversed(a, 0, a.high) + proc binarySearch*[T](a: openArray[T], key: T): int = ## binary search for `key` in `a`. Returns -1 if not found. var b = len(a) |