proper way to calculate toHash() for string
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jun 30 08:24:24 PDT 2015
On Tuesday, 30 June 2015 at 14:19:39 UTC, aki wrote:
> I would like to know proper way to calculate
> hash for given member fields.
>
> class Foo {
> int value;
> string str;
> override nothrow @safe size_t toHash() {
> // calculate hash based on field value.
> // value and str in this example.
> }
> }
>
> boost::hash provides easy and systematic way
> to calculate hash.
> I found std.digest.crc is useful.
>
> override nothrow @safe size_t toHash() {
> if (str is null) return value;
> ubyte[4] hash = crc32Of(str);
> return
> value^((hash[0]<<24)|(hash[1]<<16)|(hash[2]<<8)|hash[3]);
> }
>
> Please suggest me if anyone have an idea.
>
> Regards, aki.
Well, technically, it really doesn't matter so long as it's
consistent with opEquals (though having a hashing algorithm which
has a low collision rate can definitely help performance; you
still don't want it to be expensive though if you can help it).
Effective Java had one that made sense which I'd probably use as
a starting point if was going to write one, but I'd have to dig
out the book to see what it was. It might make sense to add
something to Phobos which took a list of member variables and
generated an appropriate hash function for you, but we don't have
anything like that right now. But if you really want to find a
good hashing function, you'll probably need to go searching
online. There's nothing special about D with regards to how
hashing functions need to work, so it's probably pretty easy to
find some good algorithms online. But someone here may already
have a link that they can point you to.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list