Commit b18c07f9 authored by Morten Welinder's avatar Morten Welinder

utils: new function for ordered hash traversal.

parent b0d4bb4f
2014-05-20 Morten Welinder <terra@gnome.org>
* src/gutils.c (gnm_hash_table_foreach_ordered): New function.
* src/gnm-so-filled.c (gnm_so_filled_write_xml_sax): Don't write
outline and fill colours that are auto, even for the 1.0 format.
......@@ -1355,7 +1357,7 @@
2013-04-04 Jean Brefort <jean.brefort@normalesup.org>
* src/graph.c (gnm_go_data_vector_load_len): correctly evaluate array
fucntions used as vectors. [697281]
functions used as vectors. [697281]
2013-04-04 Jean Brefort <jean.brefort@normalesup.org>
......
......@@ -742,3 +742,56 @@ gnm_float_hash (gnm_float const *d)
/* ------------------------------------------------------------------------- */
struct cb_compare {
GnmHashTableOrder order;
gpointer user;
};
static gint
cb_compare (gconstpointer a_, gconstpointer b_, gpointer user_data)
{
struct cb_compare *user = user_data;
gpointer *a = (gpointer )a_;
gpointer *b = (gpointer )b_;
return user->order (a[0], a[1], b[0], b[1], user->user);
}
void
gnm_hash_table_foreach_ordered (GHashTable *h,
GHFunc callback,
GnmHashTableOrder order,
gpointer user)
{
unsigned ui;
GPtrArray *data;
struct cb_compare u;
GHashTableIter hiter;
gpointer key, value;
/* Gather all key-value pairs */
data = g_ptr_array_new ();
g_hash_table_iter_init (&hiter, h);
while (g_hash_table_iter_next (&hiter, &key, &value)) {
g_ptr_array_add (data, key);
g_ptr_array_add (data, value);
}
/* Sort according to given ordering */
u.order = order;
u.user = user;
g_qsort_with_data (data->pdata,
data->len / 2, 2 * sizeof (gpointer),
cb_compare,
&u);
/* Call user callback with all pairs */
for (ui = 0; ui < data->len; ui += 2)
callback (g_ptr_array_index (data, ui),
g_ptr_array_index (data, ui + 1),
user);
/* Clean up */
g_ptr_array_free (data, TRUE);
}
......@@ -52,6 +52,15 @@ gboolean gnm_object_has_readable_prop (gconstpointer obj,
gint gnm_float_equal (gnm_float const *a, const gnm_float *b);
guint gnm_float_hash (gnm_float const *d);
typedef int (*GnmHashTableOrder) (gpointer key_a, gpointer val_a,
gpointer key_b, gpointer val_b,
gpointer user);
void gnm_hash_table_foreach_ordered (GHashTable *h,
GHFunc callback,
GnmHashTableOrder order,
gpointer user);
G_END_DECLS
#endif /* _GNM_GUTILS_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment