about summary refs log tree commit
path: root/ext/rpatricia/rpatricia.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/rpatricia/rpatricia.c')
-rw-r--r--ext/rpatricia/rpatricia.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/ext/rpatricia/rpatricia.c b/ext/rpatricia/rpatricia.c
index e1ae1df..03f6588 100644
--- a/ext/rpatricia/rpatricia.c
+++ b/ext/rpatricia/rpatricia.c
@@ -203,6 +203,30 @@ p_print_nodes (int argc, VALUE *argv, VALUE self)
 }
 
 static VALUE
+p_nodes (VALUE self)
+{
+  VALUE buf = rb_str_new(0, 0);
+  char *cbuf;
+  patricia_tree_t *tree;
+  patricia_node_t *node;
+  VALUE hash;
+
+  Data_Get_Struct(self, patricia_tree_t, tree);
+
+  hash = rb_hash_new();
+  if (tree->head) {
+    PATRICIA_WALK(tree->head, node) {
+      rb_str_resize(buf, PATRICIA_MAXSTRLEN);
+      cbuf = RSTRING_PTR(buf);
+      prefix_toa2x(node->prefix, cbuf, 1);
+      rb_str_set_len(buf, strlen(cbuf));
+      rb_hash_aset(hash, buf, (VALUE)node->data);
+    } PATRICIA_WALK_END;
+  }
+  return hash;
+}
+
+static VALUE
 p_data (VALUE self)
 {
   VALUE user_data;
@@ -385,6 +409,7 @@ Init_rpatricia (void)
   /* derivatives of climb */
   rb_define_method(cPatricia, "num_nodes", p_num_nodes, 0);
   rb_define_method(cPatricia, "show_nodes", p_print_nodes, -1);
+  rb_define_method(cPatricia, "nodes", p_nodes, 0);
 
   /* destroy tree */
   rb_define_method(cPatricia, "destroy", p_destroy, 0);