Re: glib.h suggestion: update_hash functions




Darin Adler <darin@eazel.com> writes:

> > you don't really want to call orther functions from hash functions,
> > KISS, bite the apple and implement your own specialized hash function
> > for that case, there were enough examples posted already to work from.
> 
> KISS?
> 
> So if I have two strings that I need to hash, you think that this:
> 
>     guint hash (gconstpointer v)
>     {
>         TwoStrings *t = v;
>         const char *p;
>         guint h = 0;
>         for (p = t->string_1; *p != '\0'; p += 1)
>             h = ( h << 5 ) - h  + *p;
>         for (p = t->string_2; *p != '\0'; p += 1)
>             h = ( h << 5 ) - h  + *p;
>         return h;
>     }
> 
> is simpler than this:
> 
>     guint hash (gconstpointer v)
>     {
>         TwoStrings *t = v;
>         gint h = g_str_hash (t->string_1);
>         return g_str_update_hash (h, t->string_2);
>     }

I certainly agree that you shouldn't be writing the string
hashes by hand in most cases. But is the advantage of your
code over: 

  TwoStrings *t = v;
  return g_str_hash (t->string_1) ^ (g_str_hash (t->string_2) << 16);

worth the extra API calls? 

I would say that a: 

 g_mem_hash (guchar *mem, int len);

could be a valuable addition to the API as a quick-and-dirty
way of constructing hashes over structures (though with the 
appropriate warnings about padding in the docs!)

Regards,
                                        Owen



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]