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:
parent
65af99a203
commit
a754160d65
2 changed files with 16 additions and 2 deletions
|
|
@ -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:
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue