Re: [gtk-list] Hash tables



On Fri, 9 Apr 1999, Andreas Tille wrote:

> Hello,
> 
> I use structures of this type:
> 
>   struct {
>       const char *key;
>       union {....} value;
>       int          value_type;
>   }
> 
> Comparing the keyword with a certain string I determine, which value
> is used (read from a file, should be printed, ...) and value_type
> tells me, which data type from the union has to be choosen.
> 
> This seems me to be a case for the g_hash_...() functions instead of
> every time 
>   for ( all possible keys )
>      strcmp( key, string )
> 
> to determine the value.  Unfortunately I can't find any documentation
> about the hash functions.

especially for string hashing, they are pretty easily to use:

struct { ... } *struct_p;
GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);

/* fill hash table */
for ( all key structs )
  g_hash_table_insert (ht, struct_p->key, struct_p);

/* do the lookup */
struct_p = g_hash_table_lookup (ht, "some string");

/* later */
g_hash_table_destroy (ht);


keep in mind that you may not modify the key part of your struct
while it's kept in the hash table, as the hash table will not
duplicate the key or value.


> 
> Do I suspect right if I could enhance my code with hash tables?
> Could someone point me to a description or simple example for this
> functions?
> 
> Kind regards
> 
>          Andreas.
> 

---
ciaoTJ



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