Ethereal-dev: Re: [Ethereal-dev] GHashTable

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Alejandro Vaquero <alejandrovaquero@xxxxxxxxx>
Date: Tue, 07 Feb 2006 10:51:56 -0700
Hi Favio,
You can check the GTK/GLIB doc located in file:///C:/ethereal-win32-libs/glib/share/gtk-doc/html/glib/glib-Hash-Tables.html (this is in Windows)


     g_hash_table_new ()

GHashTable <cid:[email protected]>* g_hash_table_new                (GHashFunc <cid:[email protected]> hash_func,
                                            GEqualFunc <cid:[email protected]> key_equal_func);

Creates a new GHashTable <cid:[email protected]>.

/hash_func/ : a function to create a hash value from a key. Hash values are used to determine where keys are stored within the GHashTable <cid:[email protected]> data structure. The g_direct_hash() <cid:[email protected]>, g_int_hash() <cid:[email protected]> and g_str_hash() <cid:[email protected]> functions are provided for some common types of keys. If hash_func is NULL, g_direct_hash() <cid:[email protected]> is used. /key_equal_func/ : a function to check two keys for equality. This is used when looking up keys in the GHashTable <cid:[email protected]>. The g_direct_equal() <cid:[email protected]>, g_int_equal() <cid:[email protected]> and g_str_equal() <cid:[email protected]> functions are provided for the most common types of keys. If /key_equal_func/ is NULL, keys are compared directly in a similar fashion to g_direct_equal() <cid:[email protected]>, but without the overhead of a function call.
/Returns/ : 	a new GHashTable <cid:[email protected]>.



Alejandro

fabio matturro wrote:
Hello,
I would like to attach different data structures to the same conversation. This subject is not fully explained in the developer guide (par. 2.2.9) so I'd like you to
give me some tips ;)

Let's take the example on the guide (taken from packet_afs.c)


Here a structure request_key is being initialized and is used to look up into a hash table

Now, how should I initialized this GHashTable structure?
Taking packet_afs.c as an example I notice this line:

afs_request_hash = g_hash_table_new(afs_hash, afs_equal);

This is supposed to initialize the hash table

Now, what are afs_hash & afs_equal for?
I suppose that afs_equal contains the criterium to discern whether a key is contained
in the table
[...]
static gint
afs_equal(gconstpointer v, gconstpointer w)
{
 const struct afs_request_key *v1 = (const struct afs_request_key *)v;
 const struct afs_request_key *v2 = (const struct afs_request_key *)w;

 if (v1 -> conversation == v2 -> conversation &&
     v1 -> service == v2 -> service &&
     v1 -> callnumber == v2 -> callnumber ) {

   return 1;
 }

 return 0;
}

[...]

and afs_hash calculates a numeric key starting from the data structure pointer it is passed

[...]
static guint
afs_hash (gconstpointer v)
{
const struct afs_request_key *key = (const struct afs_request_key *)v;
    guint val;

    val = key -> conversation + key -> service + key -> callnumber;

    return val;
}
[...]

Can you confirm it?

: - D Thanx

_________________________________________________________________
Scarica gratuitamente MSN Toolbar! http://toolbar.msn.it/

_______________________________________________
Ethereal-dev mailing list
Ethereal-dev@xxxxxxxxxxxx
http://www.ethereal.com/mailman/listinfo/ethereal-dev