From ea1809a931d401e97959f8fd3ddd44b5f65c77c0 Mon Sep 17 00:00:00 2001 From: apense Date: Wed, 17 Jun 2015 19:56:32 -0400 Subject: [PATCH] Added `isSorted` proc Linear-time verification that an openarray is sorted. Operates on the same parameters as `sort`. Seems much cheaper for large sorts. --- lib/pure/algorithm.nim | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index c9f7790186..bfc2e03510 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -237,6 +237,19 @@ template sortedByIt*(seq1, op: expr): expr = result = cmp(a, b)) result +proc isSorted*[T](a: openarray[T], + cmp: proc(x, y: T): int {.closure.}, + order = SortOrder.Ascending): bool = + ## Tests whether `a` is sorted + if len(a) <= 1: return true # empty or one-element lists are already sorted + + result = true + for i in 0..