summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoree7 <45465154+ee7@users.noreply.github.com>2020-07-06 14:02:03 +0200
committerGitHub <noreply@github.com>2020-07-06 14:02:03 +0200
commita754160d654f03584088412f51b153ae6a170adb (patch)
treef3263ac6c39acdb2ee188b39f167a0457f4607e8
parent65af99a203387882d9fba7d94ae0cd46f479dd1f (diff)
downloadNim-a754160d654f03584088412f51b153ae6a170adb.tar.gz
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
-rw-r--r--lib/pure/collections/tables.nim4
-rw-r--r--tests/collections/ttables.nim14
2 files changed, 16 insertions, 2 deletions
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim
index 2ea58ce1f..a969a4c5d 100644
--- a/lib/pure/collections/tables.nim
+++ b/lib/pure/collections/tables.nim
@@ -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:
diff --git a/tests/collections/ttables.nim b/tests/collections/ttables.nim
index 2a590dd26..392b5e93e 100644
--- a/tests/collections/ttables.nim
+++ b/tests/collections/ttables.nim
@@ -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