From 5774145f5d6b15b831d90e0daa10806d988da799 Mon Sep 17 00:00:00 2001 From: Fabian Keller Date: Sun, 26 Feb 2017 00:17:21 +0100 Subject: [PATCH] added hash for uints (#5435) --- lib/pure/hashes.nim | 8 ++++++++ tests/collections/thashes.nim | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 17d1c64427..d5759e5073 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -112,6 +112,14 @@ proc hash*(x: int64): Hash {.inline.} = ## efficient hashing of int64 integers result = toU32(x) +proc hash*(x: uint): Hash {.inline.} = + ## efficient hashing of unsigned integers + result = cast[int](x) + +proc hash*(x: uint64): Hash {.inline.} = + ## efficient hashing of uint64 integers + result = toU32(cast[int](x)) + proc hash*(x: char): Hash {.inline.} = ## efficient hashing of characters result = ord(x) diff --git a/tests/collections/thashes.nim b/tests/collections/thashes.nim index b9c6394142..76b99313cd 100644 --- a/tests/collections/thashes.nim +++ b/tests/collections/thashes.nim @@ -72,4 +72,19 @@ block: var t = initTable[int, int]() t[0] = 0 +# Check hashability of all integer types (issue #5429) +block: + let intTables = ( + newTable[int, string](), + newTable[int8, string](), + newTable[int16, string](), + newTable[int32, string](), + newTable[int64, string](), + newTable[uint, string](), + newTable[uint8, string](), + newTable[uint16, string](), + newTable[uint32, string](), + newTable[uint64, string](), + ) + echo "true"