range-tree: Add print utility

Useful for debugging
This commit is contained in:
Georges Basile Stavracas Neto
2020-04-14 19:08:05 +00:00
parent 3efbc2436c
commit bed8e10609
2 changed files with 46 additions and 0 deletions
+44
View File
@@ -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);
}
+2
View File
@@ -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