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:part1.02060405.08080807@yahoo.com>* g_hash_table_new                (GHashFunc <cid:part2.05020609.04070507@yahoo.com> hash_func,
                                            GEqualFunc <cid:part3.04070006.09050301@yahoo.com> key_equal_func);

Creates a new GHashTable <cid:part1.02060405.08080807@yahoo.com>.

/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:part1.02060405.08080807@yahoo.com> data structure. The g_direct_hash() <cid:part6.09000306.08030302@yahoo.com>, g_int_hash() <cid:part7.00090902.01020504@yahoo.com> and g_str_hash() <cid:part8.00030701.04010902@yahoo.com> functions are provided for some common types of keys. If hash_func is NULL, g_direct_hash() <cid:part6.09000306.08030302@yahoo.com> is used. /key_equal_func/ : a function to check two keys for equality. This is used when looking up keys in the GHashTable <cid:part1.02060405.08080807@yahoo.com>. The g_direct_equal() <cid:part11.00040508.01060405@yahoo.com>, g_int_equal() <cid:part12.09000708.08000403@yahoo.com> and g_str_equal() <cid:part13.08030408.05010207@yahoo.com> 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:part11.00040508.01060405@yahoo.com>, but without the overhead of a function call.
/Returns/ : 	a new GHashTable <cid:part1.02060405.08080807@yahoo.com>.



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