[Glade-users] libglade: coding style: just curious about glade_xml_get_widget implementation

David Hoover karma@deadmoose.com
Fri, 22 Oct 2004 19:49:45 -0700


> does anyone know whether glade_xml_get_widget uses a hash
> table on the input strings to look up the pointer to retrieve?
> Either way, if it uses a hash table, how does it hash on the strings, 
> given that they are of variable length strings?

Internally, it stores the widgets in a GHashTable
( http://developer.gnome.org/doc/API/2.0/glib/glib-Hash-Tables.html )
keyed off the name

For a hash function, it uses g_str_hash() 
( http://developer.gnome.org/doc/API/2.0/glib/glib-Hash-Tables.html#g-str-hash )

That's implemented (in glib head, at least) as:

        guint
        g_str_hash (gconstpointer key)
        {
          const char *p = key;
          guint h = *p;
        
          if (h)
            for (p += 1; *p != '\0'; p++)
              h = (h << 5) - h + *p;
        
          return h;
        }