proper way to calculate toHash() for string

aki via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 30 07:19:37 PDT 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.



More information about the Digitalmars-d-learn mailing list