After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 580279 - GTree wrapper implementation
GTree wrapper implementation
Status: RESOLVED FIXED
Product: glibmm
Classification: Bindings
Component: general
2.20.x
Other All
: Normal enhancement
: ---
Assigned To: gtkmm-forge
gtkmm-forge
Depends on:
Blocks:
 
 
Reported: 2009-04-26 12:11 UTC by Szilárd Pfeiffer
Modified: 2010-01-06 05:18 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
Patch (19.62 KB, patch)
2009-04-26 12:18 UTC, Szilárd Pfeiffer
none Details | Review
Updated the patch. (20.67 KB, patch)
2009-12-15 21:46 UTC, Szilárd Pfeiffer
committed Details | Review
Reference counting for BalancedTree. (9.54 KB, patch)
2009-12-30 18:14 UTC, Szilárd Pfeiffer
committed Details | Review
fixes of bugs mentioned in comment 8 and comment 9 (10.41 KB, patch)
2010-01-05 23:10 UTC, Szilárd Pfeiffer
committed Details | Review

Description Szilárd Pfeiffer 2009-04-26 12:11:35 UTC
glibmm wrapper for glib's GTree.
Comment 1 Szilárd Pfeiffer 2009-04-26 12:18:05 UTC
Created attachment 133325 [details] [review]
Patch
Comment 2 Szilárd Pfeiffer 2009-12-15 21:46:05 UTC
Created attachment 149802 [details] [review]
Updated the patch.
Comment 3 Murray Cumming 2009-12-17 08:18:50 UTC
It looks generally good. Have you checked that it's consistent with Glib::Node where/if necessary?

I would rename nnodes() to something more obvious/consistent.

GNode and GTree seem badly named. Both are trees, but one is like a map, right?
Comment 4 Szilárd Pfeiffer 2009-12-30 15:56:42 UTC
(In reply to comment #3)
> It looks generally good. Have you checked that it's consistent with Glib::Node
> where/if necessary?

Thanks. I've checked it now. The only change is the ref/unref functions in the version 2.22. Do you think it necessary in glibmm?

> 
> I would rename nnodes() to something more obvious/consistent.

This is the original name in Glib, that's why I named it nnodes.
http://library.gnome.org/devel/glib/stable/glib-Balanced-Binary-Trees.html#g-tree-nnodes

> 
> GNode and GTree seem badly named. Both are trees, but one is like a map, right?

First is an N-ary tree, the second is a binary tree, but the name Tree is "taken" in glibmm for Glib::NodeTree. I think B-tree is widely used name that's why I choose this.
Comment 5 Murray Cumming 2009-12-30 16:01:40 UTC
(In reply to comment #4)
> (In reply to comment #3)
> > It looks generally good. Have you checked that it's consistent with Glib::Node
> > where/if necessary?
> 
> Thanks. I've checked it now. The only change is the ref/unref functions in the
> version 2.22. Do you think it necessary in glibmm?

What do you mean?

> > I would rename nnodes() to something more obvious/consistent.
> 
> This is the original name in Glib, that's why I named it nnodes.
> http://library.gnome.org/devel/glib/stable/glib-Balanced-Binary-Trees.html#g-tree-nnodes

We often rename bad API.
Comment 6 Szilárd Pfeiffer 2009-12-30 18:12:05 UTC
(In reply to comment #5)
> (In reply to comment #4)
> > (In reply to comment #3)
> > > It looks generally good. Have you checked that it's consistent with Glib::Node
> > > where/if necessary?
> > 
> > Thanks. I've checked it now. The only change is the ref/unref functions in the
> > version 2.22. Do you think it necessary in glibmm?
> 
> What do you mean?

I've implemented the ref/unref functions (with names reference and unreference which are required by Glib::RefPtr) and other necessary changes. 

> 
> > > I would rename nnodes() to something more obvious/consistent.
> > 
> > This is the original name in Glib, that's why I named it nnodes.
> > http://library.gnome.org/devel/glib/stable/glib-Balanced-Binary-Trees.html#g-tree-nnodes
> 
> We often rename bad API.

It's ok for me.
Comment 7 Szilárd Pfeiffer 2009-12-30 18:14:51 UTC
Created attachment 150591 [details] [review]
Reference counting for BalancedTree.
Comment 8 Jonathon Jongsma 2010-01-03 23:16:08 UTC
Review of attachment 149802 [details] [review]:

Looks mostly fine.  A few comments follow.

::: glib/src/btree.hg
@@ +26,3 @@
+namespace Glib
+{
+/** N-ary Trees — trees of data with any number of branches

Is this a copy-paste error?  I thought this was a binary tree, not a N-ary tree.

@@ +27,3 @@
+{
+/** N-ary Trees — trees of data with any number of branches
+ * The BalancedTree class and its associated functions provide a balancedctree data structure, in which trees in the tree can contain arbitrary data.

balancedctree?  I assume that's just a typo?

@@ +78,3 @@
+ * supplied a value_destroy_func when creating the #GTree, the old value is 
+ * freed using that function. If you supplied a @key_destroy_func when 
+ * creating the #GTree, the passed key is freed using that function.

This documentation about the key_destroy_func and value_destroy_func does not seem to be valid at all for this implementation since we use them internally and don't allow the user to specify them anywhere (as far as I can see).  Also, in general, if we're copying the C documentation into the C++ source, we should at least use the C++ names instead of the old C names (e.g. change #GTree to BalancedTree, etc).

@@ +87,3 @@
+    g_tree_insert(gobj(), reinterpret_cast<void *>(new K(key)), reinterpret_cast<void *>(new V(value)));
+  }
+  _IGNORE(g_tree_insert)

This obviously requires both K and V to be copyable, perhaps that should be stated in the class documentation more obviously?

@@ +99,3 @@
+ * creating the #GTree, the old value is freed using that function. If you 
+ * supplied a @key_destroy_func when creating the #GTree, the old key is 
+ * freed using that function. 

same documentation issues as mentioned above

@@ +108,3 @@
+    g_tree_replace(gobj(), new K(key), new V(value));
+  }
+  _IGNORE(g_tree_replace)

This function doesn't seem to be different at all from insert() from a user's point of view.  The only things that are getting 'replaced' or 'freed' are the internal copies that we created of the key and value they passed to use.  None of the user's arguments are getting modified or freed in any way.

@@ +117,3 @@
+ *
+ * If the #GTree was created using g_tree_new_full(), the key and value 
+ * are freed using the supplied destroy functions, otherwise you have to 

Again, this documentation doesn't apply to the C++ API.

@@ +146,3 @@
+    return g_tree_steal(gobj(), &key);
+  }
+  _IGNORE(g_tree_steal)

This function could just as well be renamed "leak_memory()".  We *always* want the copies to be freed when they're removed from the tree, since we create the copies internally.  The user of the API should have no reason to call such an API, so it should just be omitted.

@@ +220,3 @@
+    g_tree_foreach(gobj(), c_callback_traverse, reinterpret_cast<gpointer>(&func_copy));
+  }
+  _IGNORE(g_tree_foreach);

Would it be possible to make this function const to enforce the documented restrictions about not modifying the tree while traversing it?

@@ +272,3 @@
+  }
+
+private:

It doesn't look like any of the following helper functions really need to be static class functions.  You could just as well move them down to the .cc file and make them plain C functions.  That might even be more portable.
Comment 9 Jonathon Jongsma 2010-01-03 23:20:15 UTC
Review of attachment 150591 [details] [review]:

This patch looks fine other than another documentation issue.

::: glib/src/btree.hg
@@ +98,3 @@
+ * Decrements the reference count of tree by one.  If the reference count
+ * drops to 0, all keys and values will be destroyed (if destroy
+ * functions were specified) and all memory allocated by @tree will be

again, the user can't specify destroy functions in the C++ implementation, so this documentation is a bit misleading.
Comment 10 Szilárd Pfeiffer 2010-01-05 23:10:00 UTC
Created attachment 150871 [details] [review]
fixes of bugs mentioned in comment 8 and comment 9

I fixed the bugs mentioned in comment 8 and comment 9 except the last one in comment 8:

> @@ +272,3 @@
> +  }
> +
> +private:
> 
> It doesn't look like any of the following helper functions really need to be
> static class functions.  You could just as well move them down to the .cc file
> and make them plain C functions.  That might even be more portable.

These functions are using the K and V template parameters, so they must be member functions of the class.
Comment 11 Jonathon Jongsma 2010-01-05 23:27:55 UTC
Review of attachment 150871 [details] [review]:

looks good
Comment 12 Jonathon Jongsma 2010-01-06 05:17:09 UTC
ok, I've committed this to git, but I've renamed the files themselves balancedtree.h rather than btree.h to follow the existing glibmm header conventions better.