1 /* Copyright (C) 2007 glibmm development team
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <glibmm/refptr.h>
25 #include <glibmm/ustring.h>
26 #include <glibmm/error.h>
27 #include <glibmm/arrayhandle.h>
33 //Hand-written, instead of using _WRAP_ENUM,
34 //because the C enum values don't have a prefix.
36 /** Specifies the type of traveral performed by methods such as NodeTree::_traverse() and NodeTree::find().
38 * @ingroup glibmmEnums
42 TRAVERSE_IN_ORDER = G_IN_ORDER, /*!< Visits a node's left child first, then the node itself, then its right child. This is the one to use if you want the output sorted according to the compare function. */
43 TRAVERSE_PRE_ORDER = G_PRE_ORDER, /*!< Visits a node, then its children. */
44 TRAVERSE_POST_ORDER = G_POST_ORDER, /*!< Visits the node's children, then the node itself. */
45 TRAVERSE_LEVEL_ORDER = G_LEVEL_ORDER /*!< For NodeTree, it vists the root node first, then its children, then its grandchildren, and so on. Note that this is less efficient than the other orders. This is not implemented for Glib::Tree. */
48 /** N-ary Trees - trees of data with any number of branches
49 * The NodeTree class and its associated functions provide an N-ary tree data structure, in which nodes in the tree can contain arbitrary data.
51 * To insert a node into a tree use insert(), insert_before(), append() or prepend().
53 * To create a new node and insert it into a tree use insert_data(), insert_data_before(), append_data() and prepend_data().
55 * To reverse the children of a node use reverse_children().
57 * To find a node use root(), find(), find_child(), index_of(), child_index(), first_child(), last_child(), nth_child(), first_sibling(), prev_sibling(), next_sibling() or last_sibling().
59 * To get information about a node or tree use is_leaf(), is_root(), depth(), node_count(), child_count(), is_ancestor() or max_height().
61 * To traverse a tree, calling a function for each node visited in the traversal, use traverse() or foreach().
63 * To remove a node or subtree from a tree use unlink().
70 _CLASS_GENERIC(NodeTree, GNode)
72 typedef sigc::slot<bool, NodeTree<T>&> TraverseFunc;
73 typedef sigc::slot<void, NodeTree<T>&> ForeachFunc;
76 static NodeTree<T>* wrap(GNode* node)
81 return reinterpret_cast<NodeTree<T>* >(node->data);
90 explicit NodeTree(const T& the_data) :
97 NodeTree(const NodeTree<T>& node) :
103 /** Removes the instance and its children from the tree,
104 * freeing any memory allocated.
113 _IGNORE(g_node_destroy)
115 NodeTree<T>& operator=(const NodeTree<T>& node)
125 /// Provides access to the underlying C GObject.
131 /// Provides access to the underlying C GObject.
132 inline const GNode* gobj() const
137 /** Inserts a NodeTree beneath the parent at the given position.
139 * @param position the position to place node at, with respect to its siblings
140 * If position is -1, node is inserted as the last child of parent
141 * @param node the NodeTree to insert
142 * @return the inserted NodeTree
144 NodeTree<T>& insert(int position, NodeTree<T>& node)
146 g_node_insert(gobj(), position, node.gobj());
149 _IGNORE(g_node_insert)
151 /** Inserts a NodeTree beneath the parent before the given sibling.
153 * @param sibling the sibling NodeTree to place node before.
154 * @param node the NodeTree to insert
155 * @return the inserted NodeTree
157 NodeTree<T>& insert_before(NodeTree<T>& sibling, NodeTree<T>& node)
159 g_node_insert_before(gobj(), sibling.gobj(), node.gobj());
162 _IGNORE(g_node_insert_before)
164 /** Inserts a NodeTree beneath the parent after the given sibling.
166 * @param sibling the sibling NodeTree to place node after.
167 * @param node the NodeTree to insert
168 * @return the inserted NodeTree
170 NodeTree<T>& insert_after(NodeTree<T>& sibling, NodeTree<T>& node)
172 g_node_insert_after(gobj(), sibling.gobj(), node.gobj());
175 _IGNORE(g_node_insert_after)
178 /** Inserts a NodeTree as the last child.
180 * @param node the NodeTree to append
181 * @return the new NodeTree
183 NodeTree<T>& append(NodeTree<T>& node)
185 g_node_append(gobj(), node.gobj());
188 _IGNORE(g_node_append)
190 /** Inserts a NodeTree as the first child.
192 * @param data the data for the NodeTree
193 * @return the NodeTree
195 NodeTree<T>& prepend(NodeTree<T>& node)
197 g_node_prepend(gobj(), node.gobj());
200 _IGNORE(g_node_prepend)
202 /** Inserts a new NodeTree at the given position.
204 * @param position the position to place the new NodeTree at.
205 * If position is -1, the new NodeTree is inserted as the last child of parent
206 * @param data the data for the new NodeTree
207 * @return the new NodeTree
209 NodeTree<T>* insert_data(int position, const T& the_data)
211 NodeTree<T>* node = new NodeTree<T>(the_data);
212 insert(position, *node);
215 _IGNORE(g_node_insert_data)
217 /** Inserts a new NodeTree before the given sibling.
219 * @param sibling the sibling NodeTree to place node before.
220 * @param data the data for the new NodeTree
221 * @return the new NodeTree
223 NodeTree<T>* insert_data_before(NodeTree<T>& sibling, const T& the_data)
225 NodeTree<T>* node = new NodeTree<T>(the_data);
226 insert_before(sibling, *node);
229 _IGNORE(g_node_insert_data_before)
231 /** Inserts a new NodeTree as the last child.
233 * @param data the data for the new NodeTree
234 * @return the new NodeTree
236 NodeTree<T>* append_data(const T& the_data)
238 NodeTree<T>* node = new NodeTree<T>(the_data);
242 _IGNORE(g_node_append_data)
244 /** Inserts a new NodeTree as the first child.
246 * @param data the data for the new NodeTree
247 * @return the new NodeTree
249 NodeTree<T>* prepend_data(const T& the_data)
251 NodeTree<T>* node = new NodeTree<T>(the_data);
255 _IGNORE(g_node_prepend_data)
257 /** Reverses the order of the children.
259 void reverse_children()
261 g_node_reverse_children(gobj());
263 _IGNORE(g_node_reverse_children)
265 /** Returns a pointer to the root of the tree.
267 * @return A pointer to the root of the tree.
269 NodeTree<T>* get_root()
271 return wrap(g_node_get_root(gobj()));
274 const NodeTree<T>* get_root() const
276 return wrap(g_node_get_root(gobj()));
278 _IGNORE(g_node_get_root)
281 /** Specifies which nodes are visited during several of the NodeTree methods,
282 * including traverse() and find().
284 * @ingroup glibmmEnums
288 TRAVERSE_LEAVES = G_TRAVERSE_LEAVES, /*!< Only leaf nodes should be visited. */
289 TRAVERSE_NON_LEAVES = G_TRAVERSE_NON_LEAVES, /*!< Only non-leaf nodes should be visited. */
290 TRAVERSE_ALL = G_TRAVERSE_ALL, /*!< All nodes should be visited. */
291 TRAVERSE_MASK = G_TRAVERSE_MASK /*!< A mask of all traverse flags. */
294 /** Traverses a tree starting at the current node.
295 * It calls the given function for each node visited.
296 * The traversal can be halted at any point by returning true from @a func.
298 * @param order The order in which nodes are visited.
299 * @param flags Which types of children are to be visited.
300 * @param max_depth The maximum depth of the traversal.
301 * Nodes below this depth will not be visited.
302 * If max_depth is -1 all nodes in the tree are visited.
303 * If max_depth is 1, only the root is visited.
304 * If max_depth is 2, the root and its children are visited. And so on.
305 * @param func the slot to invoke for each visited child
307 void traverse(const TraverseFunc& func, TraverseType order = TRAVERSE_IN_ORDER, TraverseFlags flags = TRAVERSE_ALL, int max_depth = -1)
309 TraverseFunc func_copy = func;
310 g_node_traverse(gobj(), (GTraverseType)order, (GTraverseFlags)flags, max_depth, c_callback_traverse, reinterpret_cast<gpointer>(&func_copy));
312 _IGNORE(g_node_traverse);
314 /** Calls a function for each of the children of a NodeTree.
315 * Note that it doesn't descend beneath the child nodes.
317 * @param flags Wwhich types of children are to be visited.
318 * @param func The slot to invoke for each visited node.
320 void foreach(const ForeachFunc& func, TraverseFlags flags = TRAVERSE_ALL)
322 ForeachFunc func_copy = func;
323 g_node_children_foreach(gobj(), (GTraverseFlags)flags, c_callback_foreach, reinterpret_cast<gpointer>(&func_copy));
325 _IGNORE(g_node_children_foreach)
327 /** Finds the first child of a NodeTree with the given data.
329 * @param flags Which types of children are to be visited, one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
330 * @param data The data for which to search.
331 * @return the found child, or 0 if the data is not found
333 NodeTree<T>* find_child(const T& the_data, TraverseFlags flags = TRAVERSE_ALL)
335 sigc::slot<void, GNode*, const T&, GNode**> real_slot = sigc::ptr_fun(on_compare_child);
338 typedef sigc::slot<void, GNode*> type_foreach_gnode_slot;
339 type_foreach_gnode_slot bound_slot = sigc::bind(real_slot, the_data, &child);
341 g_node_children_foreach(gobj(), (GTraverseFlags)flags, c_callback_foreach_compare_child, reinterpret_cast<gpointer>(&bound_slot));
346 /** Finds the first child of a NodeTree with the given data.
348 * @param flags Which types of children are to be visited, one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
349 * @param data The data for which to search.
350 * @return the found child, or 0 if the data is not found
352 const NodeTree<T>* find_child(const T& the_data, TraverseFlags flags = TRAVERSE_ALL) const
354 return const_cast<NodeTree<T>*>(this)->find_child(flags, the_data);
357 _IGNORE(g_node_find_child)
359 /** Finds a node in a tree.
361 * @param order The order in which nodes are visited: IN_ORDER, TRAVERSE_PRE_ORDER, TRAVERSE_POST_ORDER, or TRAVERSE_LEVEL_ORDER
362 * @param flags Which types of children are to be visited: one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
363 * @param data The data for which to search.
364 * @return The found node, or 0 if the data is not found.
366 NodeTree<T>* find(const T& the_data, TraverseType order = TRAVERSE_IN_ORDER, TraverseFlags flags = TRAVERSE_ALL)
368 //We use a sigc::slot for the C callback, so we can bind some extra data.
369 sigc::slot<gboolean, GNode*, const T&, GNode**> real_slot = sigc::ptr_fun(on_compare_node);
372 typedef sigc::slot<gboolean, GNode*> type_traverse_gnode_slot;
373 type_traverse_gnode_slot bound_slot = sigc::bind(real_slot, the_data, &child);
375 g_node_traverse(const_cast<GNode*>(gobj()), (GTraverseType)order, (GTraverseFlags)flags, -1, c_callback_traverse_compare_node, reinterpret_cast<gpointer>(&bound_slot));
380 /** Finds a node in a tree.
382 * @param order The order in which nodes are visited.
383 * @param flags Which types of children are to be visited.
384 * @param data The data for which to search.
385 * @return The found node, or 0 if the data is not found.
387 const NodeTree<T>* find(const T& the_data, TraverseType order = TRAVERSE_IN_ORDER, TraverseFlags flags = TRAVERSE_ALL) const
389 return const_cast<NodeTree<T>*>(this)->find(order, flags, the_data);
393 /** Gets the position of the first child which contains the given data.
395 * @param data The data to find.
396 * @return The index of the child which contains data, or -1 if the data is not found.
398 int child_index(const T& the_data) const
402 for(const NodeTree<T>* i = first_child(); i != 0; i = i->next_sibling())
404 if((i->data()) == the_data)
412 _IGNORE(g_node_child_index)
414 /** Gets the position with respect to its siblings.
415 * child must be a child of node.
416 * The first child is numbered 0, the second 1, and so on.
418 * @param child A child
419 * @return The position of @a child with respect to its siblings.
421 int child_position(const NodeTree<T>& child) const
423 return g_node_child_position(const_cast<GNode*>(gobj()), const_cast<GNode*>(child.gobj()));
425 _IGNORE(g_node_child_position)
427 /** Gets the first child.
429 * @return The first child, or 0 if the node has no children.
431 NodeTree<T>* first_child()
433 return wrap(g_node_first_child(gobj()));
436 /** Gets the first child.
438 * @return The first child, or 0 if the node has no children.
440 const NodeTree<T>* first_child() const
442 return const_cast<NodeTree<T>*>(this)->first_child();
444 _IGNORE(g_node_first_child)
446 /** Gets the last child.
448 * @return The last child, or 0 if the node has no children.
450 NodeTree<T>* last_child()
452 return wrap(g_node_last_child(gobj()));
455 /** Gets the last child.
457 * @return The last child, or 0 if the node has no children.
459 const NodeTree<T>* last_child() const
461 return const_cast<NodeTree<T>*>(this)->last_child();
463 _IGNORE(g_node_last_child)
465 /** Gets the nth child.
467 * @return The nth child, or 0 if n is too large.
469 NodeTree<T>* nth_child(int n)
471 return wrap(g_node_nth_child(gobj(), n));
474 /** Gets the nth child.
476 * @return The nth child, or 0 if n is too large.
478 const NodeTree<T>* nth_child(int n) const
480 return const_cast<NodeTree<T>*>(this)->nth_child(n);
482 _IGNORE(g_node_nth_child)
484 /** Gets the first sibling
485 * @return The first sibling, or 0 if the node has no siblings.
487 NodeTree<T>* first_sibling()
489 return wrap(g_node_first_sibling(gobj()));
492 /** Gets the first sibling
493 * @return The first sibling, or 0 if the node has no siblings.
495 const NodeTree<T>* first_sibling() const
497 return const_cast<NodeTree<T>*>(this)->first_sibling();
499 _IGNORE(g_node_first_sibling)
501 /** Gets the previous sibling.
503 * @return The previous sibling, or 0 if the node has no siblings.
505 NodeTree<T>* prev_sibling()
507 return wrap(g_node_prev_sibling(gobj()));
510 /** Gets the previous sibling.
512 * @return The previous sibling, or 0 if the node has no siblings.
514 const NodeTree<T>* prev_sibling() const
516 return const_cast<NodeTree<T>*>(this)->prev_sibling();
518 _IGNORE(g_node_prev_sibling)
520 /** Gets the next sibling
522 * @return The next sibling, or 0 if the node has no siblings.
524 NodeTree<T>* next_sibling()
526 return wrap(g_node_next_sibling(gobj()));
529 /** Gets the next sibling
531 * @return The next sibling, or 0 if the node has no siblings.
533 const NodeTree<T>* next_sibling() const
535 return const_cast<NodeTree<T>*>(this)->next_sibling();
537 _IGNORE(g_node_next_sibling)
539 /** Gets the last sibling.
541 * @return The last sibling, or 0 if the node has no siblings.
543 NodeTree<T>* last_sibling()
545 return wrap(g_node_last_sibling(gobj()));
548 /** Gets the last sibling.
550 * @return The last sibling, or 0 if the node has no siblings.
552 const NodeTree<T>* last_sibling() const
554 return const_cast<NodeTree<T>*>(this)->last_sibling();
556 _IGNORE(g_node_last_sibling)
558 /** Returns true if this is a leaf node.
560 * @return true if this is a leaf node.
564 return G_NODE_IS_LEAF(const_cast<GNode*>(gobj()));
567 /** Returns true if this is the root node.
569 * @return true if this is the root node.
573 return G_NODE_IS_ROOT(const_cast<GNode*>(gobj()));
576 /** Gets the depth of this node.
577 * The root node has a depth of 1.
578 * For the children of the root node the depth is 2. And so on.
580 * @return the depth of this node
584 return g_node_depth(const_cast<GNode*>(gobj()));
586 _IGNORE(g_node_depth)
588 /** Gets the number of nodes in a tree.
590 * @param flags Which types of children are to be counted: one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES
591 * @return The number of nodes in the tree.
593 guint node_count(TraverseFlags flags = TRAVERSE_ALL) const
595 return g_node_n_nodes(const_cast<GNode*>(gobj()), (GTraverseFlags)flags);
597 _IGNORE(g_node_n_nodes)
599 /** Gets the number children.
601 * @return The number of children.
603 guint child_count() const
605 return g_node_n_children(const_cast<GNode*>(gobj()));
607 _IGNORE(g_node_n_children)
609 /** Returns true if this is an ancestor of @a descendant.
610 * This is true if this is the parent of @a descendant,
611 * or if this is the grandparent of @a descendant etc.
613 * @param descendant A node.
614 * @return true if this is an ancestor of descendant.
616 bool is_ancestor(const NodeTree<T>& descendant) const
618 return g_node_is_ancestor(const_cast<GNode*>(gobj()), const_cast<GNode*>(descendant.gobj()));
620 _IGNORE(g_node_is_ancestor)
622 /** Gets the maximum height of all branches beneath this node.
623 * This is the maximum distance from the node to all leaf nodes.
624 * If root has no children, 1 is returned. If root has children, 2 is returned. And so on.
626 * @return The maximum height of all branches.
628 guint get_max_height() const
630 return g_node_max_height(const_cast<GNode*>(gobj()));
632 _IGNORE(g_node_max_height)
634 /** Unlinks a node from a tree, resulting in two separate trees.
638 g_node_unlink(gobj());
640 _IGNORE(g_node_unlink)
642 #if 0 //Commented-out because people can just use the copy constructor.
643 /** Recursively copies a node and it's data.
645 * Returns: a new node containing the copies of the data.
647 NodeTree<T>* copy_deep() const
649 //Use copy constructor instead of g_node_copy_deep to create C++ wrappers also not only the wrapped C objects.
650 return new NodeTree<T>(*this);
653 _IGNORE(g_node_copy_deep)
655 /// Accessor for this node's data
661 /// Accessor for this node's data
662 const T& data() const
667 /** Accessor for this node's parent.
669 * @return The node's parent.
671 const NodeTree<T>* parent() const
673 return wrap(gobj()->parent);
676 // Do not wrap this shallow copy function, because it is not useful:
684 //Free the children (not just with g_node_destroy(), to avoid the leaking of C++ wrapper objects):
685 while(NodeTree<T>* i = first_child())
688 //Free the wrapped object (g_node_free not available)
689 g_slice_free(GNode, gobject_);
693 ///Create a new GNode, taking the contents of an existing node if one is specified.
694 void clone(const NodeTree<T>* node = 0)
696 //Store the this pointer in the GNode so we can discover this wrapper later:
697 gobject_ = g_node_new(reinterpret_cast<gpointer>(this));
701 //Prepend the copied children of @node to the constructing node.
702 for(const NodeTree<T>* i = node->last_child(); i != 0; i = i->prev_sibling())
703 prepend(*(new NodeTree<T>(*i)));
707 /// Wrapper for invoking a TraverseFunc.
708 static gboolean c_callback_traverse(GNode* node, gpointer slot)
710 const TraverseFunc* tf = reinterpret_cast<const TraverseFunc*>(slot);
711 return (*tf)(*wrap(node));
714 /// Wrapper for invoking a ForeachFunc.
715 static void c_callback_foreach(GNode* node, gpointer slot)
717 const ForeachFunc* ff = reinterpret_cast<const ForeachFunc*>(slot);
721 /// Method for comparing a single child (Internal use).
722 static void on_compare_child(GNode* node, const T& needle, GNode** result)
724 if((0 != result) && (wrap(node)->data() == needle))
730 /// Wrapper for invoking a sigc::slot<void,GNode*> (Internal use).
731 static void c_callback_foreach_compare_child(GNode* node, gpointer data)
733 const ForeachFunc* slot = reinterpret_cast<const ForeachFunc*>(data);
734 (*slot)(*wrap(node));
737 /// Method for comparing a single node (Internal use).
738 static gboolean on_compare_node(GNode* node, const T& needle, GNode** result)
740 if(wrap(node)->data() == needle)
748 /// Wrapper for invoking a sigc::slot<gboolean,GNode*> (Internal use).
749 static gboolean c_callback_traverse_compare_node(GNode* node, gpointer data)
751 const TraverseFunc* slot = reinterpret_cast<const TraverseFunc*>(data);
752 return (*slot)(*wrap(node));