Performance of hashes and associative arrays

Dan dbdavidson at yahoo.com
Sat Nov 10 11:43:18 PST 2012


On Saturday, 10 November 2012 at 18:18:07 UTC, Joseph Rushton 
Wakeling wrote:
> On 11/07/2012 07:38 AM, "Raphaël.Jakse" 
> <raphael.jakse at gmail.com>"@puremagic.com wrote:
>> We want to be able to get the hash of s. Therefore, we 
>> re-implement the toHash
>> method of the Student class :
>
> OK, now I'm curious.  Assuming I don't write a custom 
> re-implementation, how would a custom struct or class be 
> hashed?  (What about a tuple?)
>
> I ask because I'm considering associative arrays where the key 
> is a custom class or tuple as part of a current coding project.

Not sure I understand the question. But here is how I'm doing it. 
No guarantees, but output looks promising. Code following output.

Thanks
Dan

-----------------------------
true
false
false
true
7F053B2DCFC0
20883845 vs 20883845
-----------------------------
import std.stdio;
import std.traits;
import std.typecons;
import opmix.mix;

struct S {
   mixin(HashSupport);
   alias Tuple!(int, char, string) X;
   X x;
   char[] mutable;
}

void main() {
   S s1 = { tuple(3, 'a', "foo".idup), ['a','b'].dup };
   S s2 = { tuple(3, 'a', "foo".idup), ['a','b'].dup };
   writeln(s1==s2);
   s1.x[0]++;
   writeln(s1==s2);
   writeln(s1<s2);
   writeln(s2<s1);
   writeln(s1 in [ s1: 3 ]);
   s2.x[0]++;
   writeln(s1.toHash(), " vs ", s2.toHash());
}



More information about the Digitalmars-d-learn mailing list