mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-09-05 23:48:44 +02:00
Add hash table iterators. (#500507, Jean-Yves Lefort)
2007-12-14 Matthias Clasen <mclasen@redhat.com> * glib/glib.symbols: * glib/ghash.[hc]: Add hash table iterators. (#500507, Jean-Yves Lefort) * tests/hash-test.c: Test iterators. svn path=/trunk/; revision=6130
This commit is contained in:
committed by
Matthias Clasen
parent
f506365079
commit
d741d3e7a3
@@ -44,7 +44,50 @@
|
||||
|
||||
int array[10000];
|
||||
|
||||
static void
|
||||
fill_hash_table_and_array (GHashTable *hash_table)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
{
|
||||
array[i] = i;
|
||||
g_hash_table_insert (hash_table, &array[i], &array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
init_result_array (int result_array[10000])
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
result_array[i] = -1;
|
||||
}
|
||||
|
||||
static void
|
||||
verify_result_array (int array[10000])
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
g_assert (array[i] == i);
|
||||
}
|
||||
|
||||
static void
|
||||
handle_pair (gpointer key, gpointer value, int result_array[10000])
|
||||
{
|
||||
int n;
|
||||
|
||||
g_assert (key == value);
|
||||
|
||||
n = *((int *) value);
|
||||
|
||||
g_assert (n >= 0 && n < 10000);
|
||||
g_assert (result_array[n] == -1);
|
||||
|
||||
result_array[n] = n;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
my_hash_callback_remove (gpointer key,
|
||||
@@ -75,8 +118,7 @@ my_hash_callback (gpointer key,
|
||||
gpointer value,
|
||||
gpointer user_data)
|
||||
{
|
||||
int *d = value;
|
||||
*d = 1;
|
||||
handle_pair (key, value, user_data);
|
||||
}
|
||||
|
||||
static guint
|
||||
@@ -347,13 +389,12 @@ main (int argc,
|
||||
gint *pvalue;
|
||||
GList *keys, *values;
|
||||
gint keys_len, values_len;
|
||||
GHashTableIter iter;
|
||||
gpointer ikey, ivalue;
|
||||
int result_array[10000];
|
||||
|
||||
hash_table = g_hash_table_new (my_hash, my_hash_equal);
|
||||
for (i = 0; i < 10000; i++)
|
||||
{
|
||||
array[i] = i;
|
||||
g_hash_table_insert (hash_table, &array[i], &array[i]);
|
||||
}
|
||||
fill_hash_table_and_array (hash_table);
|
||||
pvalue = g_hash_table_find (hash_table, find_first, &value);
|
||||
if (!pvalue || *pvalue != value)
|
||||
g_assert_not_reached();
|
||||
@@ -373,21 +414,32 @@ main (int argc,
|
||||
|
||||
g_list_free (keys);
|
||||
g_list_free (values);
|
||||
|
||||
g_hash_table_foreach (hash_table, my_hash_callback, NULL);
|
||||
|
||||
init_result_array (result_array);
|
||||
g_hash_table_iter_init (&iter, hash_table);
|
||||
for (i = 0; i < 10000; i++)
|
||||
if (array[i] == 0)
|
||||
g_assert_not_reached();
|
||||
{
|
||||
g_assert (g_hash_table_iter_next (&iter, &ikey, &ivalue));
|
||||
|
||||
handle_pair (ikey, ivalue, result_array);
|
||||
|
||||
if (i % 2)
|
||||
g_hash_table_iter_remove (&iter);
|
||||
}
|
||||
g_assert (! g_hash_table_iter_next (&iter, &ikey, &ivalue));
|
||||
g_assert (g_hash_table_size (hash_table) == 5000);
|
||||
verify_result_array (result_array);
|
||||
|
||||
fill_hash_table_and_array (hash_table);
|
||||
|
||||
init_result_array (result_array);
|
||||
g_hash_table_foreach (hash_table, my_hash_callback, result_array);
|
||||
verify_result_array (result_array);
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
g_hash_table_remove (hash_table, &array[i]);
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
{
|
||||
array[i] = i;
|
||||
g_hash_table_insert (hash_table, &array[i], &array[i]);
|
||||
}
|
||||
fill_hash_table_and_array (hash_table);
|
||||
|
||||
if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
|
||||
g_hash_table_size (hash_table) != 5000)
|
||||
|
Reference in New Issue
Block a user