On Wed, Oct 24, 2012 at 10:38:07PM +0200, Mehrdad wrote:
> On Wednesday, 24 October 2012 at 20:37:08 UTC, H. S. Teoh wrote:
> >On Wed, Oct 24, 2012 at 10:16:27PM +0200, Mehrdad wrote:
> >[...]
> >>struct Set(T)
> >>{
> >>    int[T] dict;
> >>    hash_t toHash() const
> >>    {
> >>            typeof(return) r = 0;
> >>            foreach (item; this.dict.keys)
> >>            {
> >>                    ???       // what should be here?
> >>            }
> >>            return r;
> >>    }
> >>}
> >
> >Try this:
> >
> >     hash_t toHash() const
> >     {
> >             hash_t h = 0;
> >             foreach (item; this.dict.keys)
> >             {
> >                     // We use a commutative operation here (+) so
> >                     // that the order of keys don't matter.
> >                     h += hashOf(&item, item.sizeof);
> >             }
> >             return h;
> >     }
> >
> >
> >T
> 
> Wouldn't that do a bitwise comparison?

Umm, the idea of a hash is to *quickly* compute a value that's (mostly)
unique for a given object. In most cases, the bit representation is good
enough. If you want a thorough recursive comparison of all subobjects,
you should overload opEquals and use that instead.

Or use compile-time reflection to iterate over every member of the given
object and recursively iterate and hash them. Though I fail to see the
point of it, since it defeats the purpose of a hash in the first place -
you might as well just do the usual recursive compare instead.


T

-- 
Never step over a puddle, always step around it. Chances are that whatever made 
it is still dripping.

Reply via email to