Re: glib API for iterating the g_hash_table



On Wed, Jan 14, 2004 at 10:02:54PM +0800, Tony Yat-Tung Cheung wrote:
> Hi,
> 
> Is there any API for iterating GHashTable?
> 
> I only see the the g_hash_table_foreach() here. But sometimes people may
> like to iterate through a hash table and stop the iteration when certain
> conditions are met.
> 
> Currently, there is g_hash_table_size(), we could easily add iteration
> by providing the following two new APIs,
> 
> gconstpointer g_hash_table_get_key(GHashTable *hash_table, int pos);
> gconspointer g_hash_table_get_value(GHashTable *hash_table, int pos);
> 
> The two APIs allows one to iterate the hash table, for example,
> 
> for (i = 0; i < g_hash_table_size(hash_table); i++) {
>  gconstpointer value = g_hash_table_get_value(hash_table, i);
> 
>  /* Do some processing here */
>  if (is_match(value))
>    return TRUE;
> }
> 
> Should we add the APIs?


I made something similar, but for GData:


typedef struct _GQDPair GQDPair;
struct _GQDPair
{
	GQuark    quark;
	gpointer  data;
};

GArray * g_datalist_pairs(GArray *a, GData **dl);

#define GQDq(qp, i) g_array_index(qp, GQDPair, i).quark
#define GQDd(qp, i) g_array_index(qp, GQDPair, i).data



/* GQuark/gpointer pairs from GDataList ------------------------ */

void
on_keyval2GArray(GQuark key, gpointer *data, GArray *a)
{
	GQDPair p;

	p.quark = key;
	p.data = data;
	g_array_append_val(a, p);
}

GArray *
g_datalist_pairs(GArray *a, GData **dl)
{
	if (a)
	{
		g_array_set_size(a, 0);
	}
	else
		a = g_array_new(TRUE, FALSE, sizeof(GQDPair));

	g_datalist_foreach(dl, (GDataForeachFunc)on_keyval2GArray, a);
	
	return a;
}


bajcik
-- 
Krzysztof Garus <bajcik kolos math uni lodz pl>            Linux User 171721
  Stronka: http://kolos.math.uni.lodz.pl/~bajcik/
  Polecam: http://kolos.math.uni.lodz.pl/~bajcik/duskc.html



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