tables.nim: Add named fields in smallest and largest (#14919)

The `smallest` and `largest` procs for `CountTable` returned a tuple
with named fields, but the same procs for `CountTableRef` returned an
anonymous tuple.

This commit makes those `CountTableRef` procs more consistent, and adds
a test.

Fixes: #14918
This commit is contained in:
ee7 2020-07-06 14:02:03 +02:00 • committed by GitHub
commit a754160d65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -2670,14 +2670,14 @@ proc inc*[A](t: CountTableRef[A], key: A, val = 1) =
doAssert a == newCountTable("aaabbbbbbbbbbb") doAssert a == newCountTable("aaabbbbbbbbbbb")
t[].inc(key, val) t[].inc(key, val)
proc smallest*[A](t: CountTableRef[A]): (A, int) = proc smallest*[A](t: CountTableRef[A]): tuple[key: A, val: int] =
## Returns the ``(key, value)`` pair with the smallest ``val``. Efficiency: O(n) ## Returns the ``(key, value)`` pair with the smallest ``val``. Efficiency: O(n)
## ##
## See also: ## See also:
## * `largest proc<#largest,CountTableRef[A]>`_ ## * `largest proc<#largest,CountTableRef[A]>`_
t[].smallest t[].smallest
proc largest*[A](t: CountTableRef[A]): (A, int) = proc largest*[A](t: CountTableRef[A]): tuple[key: A, val: int] =
## Returns the ``(key, value)`` pair with the largest ``val``. Efficiency: O(n) ## Returns the ``(key, value)`` pair with the largest ``val``. Efficiency: O(n)
## ##
## See also: ## See also:

View file

@ -325,6 +325,20 @@ block tablesref:
else: break else: break
inc i inc i
block smallestLargestNamedFieldsTest: # bug #14918
const a = [7, 8, 8]
proc testNamedFields(t: CountTable | CountTableRef) =
doAssert t.smallest.key == 7
doAssert t.smallest.val == 1
doAssert t.largest.key == 8
doAssert t.largest.val == 2
let t1 = toCountTable(a)
testNamedFields(t1)
let t2 = newCountTable(a)
testNamedFields(t2)
block SyntaxTest: block SyntaxTest:
var x = newTable[int, string]({:}) var x = newTable[int, string]({:})
discard x discard x