[gnome-calendar/gbsneto/range: 7/12] range-tree: Add print utility



commit decee7c89653a6feac010ad5c303e0b4707ba9bb
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Apr 13 18:24:01 2020 -0300

    range-tree: Add print utility
    
    Useful for debugging

 src/core/gcal-range-tree.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 src/core/gcal-range-tree.h |  2 ++
 2 files changed, 46 insertions(+)
---
diff --git a/src/core/gcal-range-tree.c b/src/core/gcal-range-tree.c
index 59b66c6c..f87f7d0e 100644
--- a/src/core/gcal-range-tree.c
+++ b/src/core/gcal-range-tree.c
@@ -443,6 +443,30 @@ remove_data_func (GcalRange *range,
   return GCAL_TRAVERSE_CONTINUE;
 }
 
+static void
+recursively_print_node_to_string (Node    *n,
+                                  GString *string,
+                                  gint     depth)
+{
+  g_autofree gchar *range = NULL;
+  gint64 i;
+
+  for (i = 0; i < depth * 2; i++)
+    g_string_append (string, "  ");
+
+  if (!n)
+    {
+      g_string_append (string, "(null)\n");
+      return;
+    }
+
+  range = gcal_range_to_string (n->range);
+  g_string_append_printf (string, "Node %s (hits: %d)\n", range, n->hits);
+
+  recursively_print_node_to_string (n->left, string, depth + 1);
+  recursively_print_node_to_string (n->right, string, depth + 1);
+}
+
 static void
 gcal_range_tree_free (GcalRangeTree *self)
 {
@@ -714,3 +738,23 @@ gcal_range_tree_count_entries_at_range (GcalRangeTree *self,
 
   return gather_data.counter;
 }
+
+/**
+ * gcal_range_tree_print:
+ * @self: a #GcalRangeTree
+ *
+ * Prints @self. This is only useful for debugging purposes. There
+ * are no guarantees about the format, except that it's human-readable.
+ */
+void
+gcal_range_tree_print (GcalRangeTree *self)
+{
+  g_autoptr (GString) string = NULL;
+
+  g_return_if_fail (self);
+
+  string = g_string_new ("");
+  recursively_print_node_to_string (self->root, string, 0);
+
+  g_print ("%s", string->str);
+}
diff --git a/src/core/gcal-range-tree.h b/src/core/gcal-range-tree.h
index 9bfa5f8a..13ec4d33 100644
--- a/src/core/gcal-range-tree.h
+++ b/src/core/gcal-range-tree.h
@@ -82,6 +82,8 @@ GPtrArray*           gcal_range_tree_get_data_at_range           (GcalRangeTree
 guint64              gcal_range_tree_count_entries_at_range      (GcalRangeTree      *self,
                                                                   GcalRange          *range);
 
+void                 gcal_range_tree_print                       (GcalRangeTree      *self);
+
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (GcalRangeTree, gcal_range_tree_unref)
 
 G_END_DECLS


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