Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

take/takeWhile iterators for Counttables #13428

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/pure/collections/tables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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], fn: proc(key: A, val: int): bool): (A, int) =
let L = len(t)
for h in 0 .. high(t.data):
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




Expand Down