From 9870cbfadce03be7a7eb89cbbb94b5352912e68a Mon Sep 17 00:00:00 2001 From: "Hans-J. Schmid" Date: Mon, 17 Feb 2020 15:30:18 +0100 Subject: [PATCH 1/3] adds take iterator to CountTable. --- lib/pure/collections/tables.nim | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index a0cc73ad62920..0d065f36ad605 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -2619,6 +2619,16 @@ iterator mvalues*[A](t: var CountTable[A]): var int = yield t.data[h].val assert(len(t) == L, "the length of the table changed while iterating over it") +iterator take*[A](t: CountTable[A], n: Natural): (A, int) = + let L = len(t) + var n = n + for h in 0 .. high(t.data): + if n == 0: break + if t.data[h].val != 0: + yield (t.data[h].key, t.data[h].val) + dec n + assert(len(t) == L, "the length of the table changed while iterating over it") + From c437132d7f4ef8e587ab278293cc113afab6a642 Mon Sep 17 00:00:00 2001 From: "Hans-J. Schmid" Date: Tue, 18 Feb 2020 11:00:17 +0100 Subject: [PATCH 2/3] adds take and takeWhile to CountTable. --- lib/pure/collections/tables.nim | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index a0cc73ad62920..72463d30199c6 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -2619,6 +2619,28 @@ iterator mvalues*[A](t: var CountTable[A]): var int = yield t.data[h].val assert(len(t) == L, "the length of the table changed while iterating over it") +iterator take*[A](t: CountTable[A], n: Natural): (A, int) = + let L = len(t) + var n = n + for h in 0 .. high(t.data): + if n == 0: break + if t.data[h].val != 0: + dec n + yield (t.data[h].key, t.data[h].val) + assert(len(t) == L, "the length of the table changed while iterating over it") + +iterator takeWhile*[A](t: CountTable[A], fun: proc(key: A, val: int): bool): (A, int) = + let L = len(t) + for h in 0 .. high(t.data): + if t.data[h].val != 0: + let + key = t.data[h].key + val = t.data[h].val + if fun(key, val): + yield (key, val) + assert(len(t) == L, "the length of the table changed while iterating over it") + else: break + From 48cb41229625f2b1deac07ab4059089389e6982f Mon Sep 17 00:00:00 2001 From: "Hans-J. Schmid" Date: Tue, 18 Feb 2020 11:11:16 +0100 Subject: [PATCH 3/3] adds take and takeWhile to CountTable. --- lib/pure/collections/tables.nim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index 72463d30199c6..f465fa12221f7 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -2629,14 +2629,14 @@ iterator take*[A](t: CountTable[A], n: Natural): (A, int) = yield (t.data[h].key, t.data[h].val) assert(len(t) == L, "the length of the table changed while iterating over it") -iterator takeWhile*[A](t: CountTable[A], fun: proc(key: A, val: int): bool): (A, int) = +iterator takeWhile*[A](t: CountTable[A], fn: proc(key: A, val: int): bool): (A, int) = let L = len(t) for h in 0 .. high(t.data): - if t.data[h].val != 0: - let - key = t.data[h].key - val = t.data[h].val - if fun(key, val): + let + key = t.data[h].key + val = t.data[h].val + if val != 0: + if fn(key, val): yield (key, val) assert(len(t) == L, "the length of the table changed while iterating over it") else: break