mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-04 20:17:42 +00:00
* fix #20023 hash for generic tables * use default computation * Update lib/pure/collections/tables.nim Co-authored-by: Dan Rose <dan@digilabs.io> * Update lib/pure/collections/tables.nim Co-authored-by: Dan Rose <dan@digilabs.io> * Update lib/pure/collections/tables.nim * Update lib/pure/collections/tables.nim * Update t20023.nim --------- Co-authored-by: Dan Rose <dan@digilabs.io> Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
@@ -2901,3 +2901,18 @@ iterator mvalues*[A](t: CountTableRef[A]): var int =
|
||||
if t.data[h].val != 0:
|
||||
yield t.data[h].val
|
||||
assert(len(t) == L, "the length of the table changed while iterating over it")
|
||||
|
||||
proc hash*[K,V](s: Table[K,V]): Hash =
|
||||
for p in pairs(s):
|
||||
result = result xor hash(p)
|
||||
result = !$result
|
||||
|
||||
proc hash*[K,V](s: OrderedTable[K,V]): Hash =
|
||||
for p in pairs(s):
|
||||
result = result !& hash(p)
|
||||
result = !$result
|
||||
|
||||
proc hash*[V](s: CountTable[V]): Hash =
|
||||
for p in pairs(s):
|
||||
result = result xor hash(p)
|
||||
result = !$result
|
||||
10
tests/stdlib/t20023.nim
Normal file
10
tests/stdlib/t20023.nim
Normal file
@@ -0,0 +1,10 @@
|
||||
import std/[tables, hashes, assertions]
|
||||
|
||||
|
||||
let t = ()
|
||||
var a = toTable({t:t})
|
||||
del(a,t)
|
||||
let b = default(typeof(a))
|
||||
|
||||
doAssert a==b , "tables are not equal"
|
||||
doAssert hash(a) == hash(b), "table hashes are not equal"
|
||||
Reference in New Issue
Block a user