www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - associativeArray.toHash()

reply Michiel <nomail please.com> writes:
I'm now implementing the toHash() function of my new Set class. But I'm
not sure how to extract hash-information from an associative array of
which the key-type can be anything. The values are irrelevant. So you
might see the problem as extracting hash-information from its .keys array.

I'm sure the hash-information of its key-type can be retrieved, since
that's how the associative array works in the first place. How do I
retrieve it manually?

Thanks!

-- 
Michiel
Feb 21 2007
parent reply Michiel <nomail please.com> writes:
Michiel wrote:

 I'm now implementing the toHash() function of my new Set class. But I'm
 not sure how to extract hash-information from an associative array of
 which the key-type can be anything. The values are irrelevant. So you
 might see the problem as extracting hash-information from its .keys array.
 
 I'm sure the hash-information of its key-type can be retrieved, since
 that's how the associative array works in the first place. How do I
 retrieve it manually?
I think I found a way in Phobos, but it might not be the most elegant one. I used std.boxer to pack each element of the array into a box. Then I call the Box.toHash() function on each and use the sum to calculate the sets hash. Is there a better way? It does seem to work. I can now have sets of sets of sets of whatever, and it still works as expected. I'll continue to tweak the class a bit and then I'll publish it here. -- Michiel
Feb 21 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Michiel" <nomail please.com> wrote in message 
news:eritp3$d3j$1 digitalmars.com...
 Michiel wrote:

 I think I found a way in Phobos, but it might not be the most elegant
 one. I used std.boxer to pack each element of the array into a box. Then
 I call the Box.toHash() function on each and use the sum to calculate
 the sets hash.

 Is there a better way?
You can use the .getHash method of the typeinfo any type, and it'll use the built in hashing function. class Set(T) { ... T[] values; // or however you store them void someFunc(T data) { hash_t hash = typeid(T).getHash(&data); } }
Feb 21 2007
parent Michiel <nomail please.com> writes:
Jarrett Billingsley wrote:

 I think I found a way in Phobos, but it might not be the most elegant
 one. I used std.boxer to pack each element of the array into a box. Then
 I call the Box.toHash() function on each and use the sum to calculate
 the sets hash.

 Is there a better way?
You can use the .getHash method of the typeinfo any type, and it'll use the built in hashing function. class Set(T) { ... T[] values; // or however you store them void someFunc(T data) { hash_t hash = typeid(T).getHash(&data); } }
That also works nicely! I tried something similar, just not exactly like that. Anyway, it's more elegant than my box way. Thanks! -- Michiel
Feb 22 2007