Thread overview
proper way to calculate toHash() for string
Jun 30, 2015
aki
Jun 30, 2015
Jonathan M Davis
Jun 30, 2015
Jacob Carlborg
Jul 01, 2015
aki
June 30, 2015
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.

June 30, 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
June 30, 2015
On 30/06/15 16:19, aki wrote:

> Please suggest me if anyone have an idea.

You can use TypeInfo.getHash [1] to get the hash of a given value. Something like:

string a = "foo";
typeid(a).getHash(&a)

[1] http://dlang.org/phobos/object.html#.TypeInfo.getHash

-- 
/Jacob Carlborg
July 01, 2015
On Tuesday, 30 June 2015 at 19:36:24 UTC, Jacob Carlborg wrote:
> On 30/06/15 16:19, aki wrote:
>
>> Please suggest me if anyone have an idea.
>
> You can use TypeInfo.getHash [1] to get the hash of a given value. Something like:
>
> string a = "foo";
> typeid(a).getHash(&a)
>
> [1] http://dlang.org/phobos/object.html#.TypeInfo.getHash

Wow, this is what I was looking for.
It's generic way. Thank you for good suggestion.
Your 2 lines code is also a good explanation for the
manual of getHash. Because it's not obvious what is
the argument for the getHash.