How to use TrieMap without Hash.hash

this is the sample code in document, but Hash.hash is deprecated as below picture, is there a better way to create a TrieMap?

The warning is just the Hash.hash is not a very good hash function because it only uses the least significant 32 bits of the Nat and you might want to supply your own function that uses all the bits of the Nat. If your Nats are actually smaller than 2^32, then you should be ok.

I’m sorry that we still don’t have something better available, but we’ve been discussing options off-line.

1 Like

You may want to look at GitHub - ZhenyaUsenko/motoko-hash-map: Stable hash maps for Motoko for better performance and more features.

1 Like

Claudio, this and post (Field hash is deprecated) are still the first result when querying this problem, and unfortunately no example of a bespoke function is given (that can use >32 bits Nat).

I looked into Zhenya lib and this bespoke function seems to work:

public func hashNat(key: Nat): Nat32 {
    var hash = Prim.intToNat64Wrap(key);

    hash := hash >> 30 ^ hash *% 0xbf58476d1ce4e5b9;
    hash := hash >> 27 ^ hash *% 0x94d049bb133111eb;

    Prim.nat64ToNat32(hash >> 31 ^ hash & 0x3fffffff);

(Source: motoko-hash-map/src/Set/ at 428f4a7f8c7ca811de0a6afb3f54329bbd8750fd · ZhenyaUsenko/motoko-hash-map · GitHub)

Do you see anything wrong in using it as a temporary solution, while we wait for Base library to provide a solution to this problem?