mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-19 15:31:28 +00:00
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:
@@ -2670,14 +2670,14 @@ proc inc*[A](t: CountTableRef[A], key: A, val = 1) =
|
||||
doAssert a == newCountTable("aaabbbbbbbbbbb")
|
||||
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)
|
||||
##
|
||||
## See also:
|
||||
## * `largest proc<#largest,CountTableRef[A]>`_
|
||||
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)
|
||||
##
|
||||
## See also:
|
||||
|
||||
@@ -325,6 +325,20 @@ block tablesref:
|
||||
else: break
|
||||
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:
|
||||
var x = newTable[int, string]({:})
|
||||
discard x
|
||||
|
||||
Reference in New Issue
Block a user