[gnumeric] utils: new function for ordered hash traversal.
- From: Morten Welinder <mortenw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnumeric] utils: new function for ordered hash traversal.
- Date: Tue, 20 May 2014 23:18:37 +0000 (UTC)
commit b18c07f95919ea94d2f3adc004254f7b7ece20d0
Author: Morten Welinder <terra gnome org>
Date: Tue May 20 19:18:03 2014 -0400
utils: new function for ordered hash traversal.
ChangeLog | 4 +++-
src/gutils.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
src/gutils.h | 9 +++++++++
3 files changed, 65 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 2de2aa5..4a4c23a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
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>
diff --git a/src/gutils.c b/src/gutils.c
index 7109a39..d857977 100644
--- a/src/gutils.c
+++ b/src/gutils.c
@@ -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);
+}
diff --git a/src/gutils.h b/src/gutils.h
index e74e1b4..53755ef 100644
--- a/src/gutils.h
+++ b/src/gutils.h
@@ -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_ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]