logo top
Main Page   glibmm Namespaces   Book  

Glib::Tree<T> Class Template Reference

N-ary Trees — trees of data with any number of branches The Tree class and its associated functions provide an N-ary tree data structure, in which nodes in the tree can contain arbitrary data. More...

List of all members.

Public Types

typedef gpointer iterator
typedef sigc::slot<bool, T&>* TraverseFunc
typedef sigc::slot<void, T&>* ForeachFunc
typedef std::map< gpointer,
Tree<T >*> 
NodeMap

Public Member Functions

 Tree ()
 Creates a new Tree containing the given data.
 Tree (T& data)
 ~Tree ()
 Removes the instance and its children from the tree, freeing any memory allocated.
Tree<T>& insert (int position, Tree<T>& node)
 Inserts a Tree beneath the parent at the given position.
Tree<T>& insert_before (Tree<T>& sibling, Tree<T>& node)
 Inserts a Tree beneath the parent before the given sibling.
Tree<T>& insert_after (Tree<T>& sibling, Tree<T>& node)
 Inserts a Tree beneath the parent after the given sibling.
Tree<T>& append (Tree<T>& node)
 Inserts a Tree as the last child.
Tree<T>& prepend (Tree<T>& node)
 Inserts a Tree as the first child.
Tree<T>* insert_data (int position, T& data)
 Inserts a new Tree at the given position.
Tree<T>* insert_data_before (Tree<T>& sibling, T& data)
 Inserts a new Tree before the given sibling.
Tree<T>* append_data (T& data)
 Inserts a new Tree as the last child.
Tree<T>* prepend_data (T& data)
 Inserts a new Tree as the first child.
void reverse_children ()
 Reverses the order of the children.
Tree<T>* root () const
 Returns a pointer to the root of the tree.
void traverse (TraverseType order, TraverseFlags flags, int max_depth, TraverseFunc func)
 Traverses a tree starting at the current node.
void foreach (TraverseFlags flags, ForeachFunc func)
 Calls a function for each of the children of a Tree.
Tree<T>* find_child (TraverseFlags flags, const T& data) const
 Finds the first child of a Tree with the given data.
Tree<T>* find (TraverseType order, TraverseFlags flags, const T& data) const
 Finds a node in a tree.
int index_of (const T& data) const
 Gets the position of the first child which contains the given data.
int position_of (const Tree<T>& child) const
 Gets the position with respect to its siblings.
Tree<T>* first_child () const
 Gets the first child.
Tree<T>* last_child () const
 Gets the last child.
Tree<T>* nth_child (int n) const
 Gets the nth child.
Tree<T>* first_sibling () const
 Gets the first sibling.
Tree<T>* prev_sibling () const
 Gets the previous sibling.
Tree<T>* next_sibling () const
 Gets the next sibling.
Tree<T>* last_sibling () const
 Gets the last sibling.
bool is_leaf () const
 Returns true if this is a leaf node.
bool is_root () const
 Returns true if this is the root node.
unsigned int depth () const
 Gets the depth of this node.
unsigned int node_count (TraverseFlags flags) const
 Gets the number of nodes in a tree.
unsigned int child_count () const
 Gets the number children.
bool is_ancestor (const Tree<T>& descendant) const
 Returns true if this is an ancestor of descendant.
unsigned int max_height () const
 Gets the maximum height of all branches beneath this node.
void unlink (Tree<T>& child)
 Unlinks a node from a tree, resulting in two separate trees.
iterator iter () const
 Accessor for this node's iterator.
T& data () const
 Accessor for this node's data.
Tree<T>* lookup (const iterator child) const
 Lookup a child by its iterator.
GNode* gobj ()
const GNode* gobj () const

Protected Types

typedef sigc::slot< gboolean,
GNode* > 
type_traverse_gnode_slot
typedef sigc::slot<void, GNode*> type_foreach_gnode_slot

Protected Member Functions

Tree<T>* parent (Tree<T>* newparent=0)
 Accessor for this node's parent.

Static Protected Member Functions

static gboolean wrap_traverse_slot (GNode* node, gpointer slot)
 Wrapper for invoking a TraverseFunc.
static void wrap_foreach_slot (GNode* node, gpointer slot)
 Wrapper for invoking a ForeachFunc.
static void on_compare_child (GNode* node, const T& needle, GNode* result)
 Method for comparing a single child (Internal use).
static void on_wrap_compare_child (GNode* node, gpointer data)
 Wrapper for invoking a sigc::slot<void,GNode*> (Internal use).
static gboolean on_compare_node (GNode* node, const T& needle, GNode** result)
 Method for comparing a single node (Internal use).
static gboolean on_wrap_compare_node (GNode* node, gpointer data)
 Wrapper for invoking a sigc::slot<gboolean,GNode*> (Internal use).

Protected Attributes

GNode* gobject_
bool owns_gobject_
NodeMap children_
Tree<T>* parent_


Detailed Description

template <typename T>
class Glib::Tree< T >

N-ary Trees — trees of data with any number of branches The Tree class and its associated functions provide an N-ary tree data structure, in which nodes in the tree can contain arbitrary data.

To insert a node into a tree use insert(), insert_before(), append() or prepend().

To create a new node and insert it into a tree use insert_data(), insert_data_before(), append_data() and prepend_data().

To reverse the children of a node use reverse_children().

To find a node use root(), find(), find_child(), index_of(), position_of(), first_child(), last_child(), nth_child(), first_sibling(), prev_sibling(), next_sibling() or last_sibling().

To get information about a node or tree use is_leaf(), is_root(), depth(), node_count(), child_count(), is_ancestor() or max_height().

To traverse a tree, calling a function for each node visited in the traversal, use traverse() or foreach().

To remove a node or subtree from a tree use unlink().

Since glibmm 2.18:

Member Typedef Documentation

template <typename T>
typedef gpointer Glib::Tree<T>::iterator

template <typename T>
typedef sigc::slot<bool, T&>* Glib::Tree<T>::TraverseFunc

template <typename T>
typedef sigc::slot<void, T&>* Glib::Tree<T>::ForeachFunc

template <typename T>
typedef std::map<gpointer, Tree<T>*> Glib::Tree<T>::NodeMap

template <typename T>
typedef sigc::slot<gboolean, GNode*> Glib::Tree<T>::type_traverse_gnode_slot [protected]

template <typename T>
typedef sigc::slot<void, GNode*> Glib::Tree<T>::type_foreach_gnode_slot [protected]


Constructor & Destructor Documentation

template <typename T>
Glib::Tree<T>::Tree (  )  [inline]

Creates a new Tree containing the given data.

Used to create the first node in a tree.

template <typename T>
Glib::Tree<T>::Tree ( T &  data  )  [inline, explicit]

template <typename T>
Glib::Tree<T>::~Tree (  )  [inline]


Member Function Documentation

template <typename T>
Tree<T>& Glib::Tree<T>::insert ( int  position,
Tree<T>&  node 
) [inline]

Inserts a Tree beneath the parent at the given position.

Parameters:
position the position to place node at, with respect to its siblings If position is -1, node is inserted as the last child of parent
node the Tree to insert
Returns:
the inserted Tree

References Glib::Tree<T>::children_, Glib::Tree<T>::gobj(), Glib::Tree<T>::gobject_, and Glib::Tree<T>::parent().

Referenced by Glib::Tree<T>::insert_data().

template <typename T>
Tree<T>& Glib::Tree<T>::insert_before ( Tree<T>&  sibling,
Tree<T>&  node 
) [inline]

Inserts a Tree beneath the parent before the given sibling.

Parameters:
sibling the sibling Tree to place node before.
node the Tree to insert
Returns:
the inserted Tree

References Glib::Tree<T>::children_, Glib::Tree<T>::gobj(), Glib::Tree<T>::gobject_, and Glib::Tree<T>::parent().

Referenced by Glib::Tree<T>::insert_data_before().

template <typename T>
Tree<T>& Glib::Tree<T>::insert_after ( Tree<T>&  sibling,
Tree<T>&  node 
) [inline]

Inserts a Tree beneath the parent after the given sibling.

Parameters:
sibling the sibling Tree to place node after.
node the Tree to insert
Returns:
the inserted Tree

References Glib::Tree<T>::children_, Glib::Tree<T>::gobj(), Glib::Tree<T>::gobject_, and Glib::Tree<T>::parent().

template <typename T>
Tree<T>& Glib::Tree<T>::append ( Tree<T>&  node  )  [inline]

Inserts a Tree as the last child.

Parameters:
node the Tree to append
Returns:
the new Tree

References Glib::Tree<T>::children_, Glib::Tree<T>::gobj(), Glib::Tree<T>::gobject_, and Glib::Tree<T>::parent().

Referenced by Glib::Tree<T>::append_data().

template <typename T>
Tree<T>& Glib::Tree<T>::prepend ( Tree<T>&  node  )  [inline]

Inserts a Tree as the first child.

Parameters:
data the data for the Tree
Returns:
the Tree

References Glib::Tree<T>::children_, Glib::Tree<T>::gobj(), Glib::Tree<T>::gobject_, and Glib::Tree<T>::parent().

Referenced by Glib::Tree<T>::prepend_data().

template <typename T>
Tree<T>* Glib::Tree<T>::insert_data ( int  position,
T &  data 
) [inline]

Inserts a new Tree at the given position.

Parameters:
position the position to place the new Tree at. If position is -1, the new Tree is inserted as the last child of parent
data the data for the new Tree
Returns:
the new Tree

References Glib::Tree<T>::insert().

template <typename T>
Tree<T>* Glib::Tree<T>::insert_data_before ( Tree<T>&  sibling,
T &  data 
) [inline]

Inserts a new Tree before the given sibling.

Parameters:
sibling the sibling Tree to place node before.
data the data for the new Tree
Returns:
the new Tree

References Glib::Tree<T>::insert_before().

template <typename T>
Tree<T>* Glib::Tree<T>::append_data ( T &  data  )  [inline]

Inserts a new Tree as the last child.

Parameters:
data the data for the new Tree
Returns:
the new Tree

References Glib::Tree<T>::append().

template <typename T>
Tree<T>* Glib::Tree<T>::prepend_data ( T &  data  )  [inline]

Inserts a new Tree as the first child.

Parameters:
data the data for the new Tree
Returns:
the new Tree

References Glib::Tree<T>::prepend().

template <typename T>
void Glib::Tree<T>::reverse_children (  )  [inline]

Reverses the order of the children.

References Glib::Tree<T>::gobject_.

template <typename T>
Tree<T>* Glib::Tree<T>::root (  )  const [inline]

Returns a pointer to the root of the tree.

Returns:
A pointer to the root of the tree.

References Glib::Tree<T>::parent_.

template <typename T>
void Glib::Tree<T>::traverse ( TraverseType  order,
TraverseFlags  flags,
int  max_depth,
TraverseFunc  func 
) [inline]

Traverses a tree starting at the current node.

It calls the given function for each node visited. The traversal can be halted at any point by returning true from func.

Parameters:
order The order in which nodes are visited: TRAVERSE_IN_ORDER, TRAVERSE_PRE_ORDER, TRAVERSE_POST_ORDER, or TRAVERSE_LEVEL_ORDER.
flags Which types of children are to be visited: One of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
max_depth The maximum depth of the traversal. Nodes below this depth will not be visited. If max_depth is -1 all nodes in the tree are visited. If max_depth is 1, only the root is visited. If max_depth is 2, the root and its children are visited. And so on.
func the slot to invoke for each visited child

References Glib::Tree<T>::gobject_, and Glib::Tree<T>::wrap_traverse_slot().

template <typename T>
void Glib::Tree<T>::foreach ( TraverseFlags  flags,
ForeachFunc  func 
) [inline]

Calls a function for each of the children of a Tree.

Note that it doesn't descend beneath the child nodes.

Parameters:
flags Wwhich types of children are to be visited: One of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
func The slot to invoke for each visited node.

References Glib::Tree<T>::gobject_, and Glib::Tree<T>::wrap_foreach_slot().

template <typename T>
Tree<T>* Glib::Tree<T>::find_child ( TraverseFlags  flags,
const T &  data 
) const [inline]

Finds the first child of a Tree with the given data.

Parameters:
flags Which types of children are to be visited, one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
data The data for which to search
Returns:
the found child, or 0 if the data is not found

References Glib::Tree<T>::gobject_, Glib::Tree<T>::lookup(), Glib::Tree<T>::on_compare_child(), Glib::Tree<T>::on_wrap_compare_child(), and ptr_fun().

template <typename T>
Tree<T>* Glib::Tree<T>::find ( TraverseType  order,
TraverseFlags  flags,
const T &  data 
) const [inline]

Finds a node in a tree.

Parameters:
order The order in which nodes are visited: TRAVERSE_IN_ORDER, TRAVERSE_PRE_ORDER, TRAVERSE_POST_ORDER, or TRAVERSE_LEVEL_ORDER
flags Which types of children are to be visited: one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
Returns:
The found node, or 0 if the data is not found.

References Glib::Tree<T>::depth(), std::stack<_Tp, _Sequence>::empty(), Glib::Tree<T>::gobject_, Glib::Tree<T>::lookup(), Glib::Tree<T>::on_compare_node(), Glib::Tree<T>::on_wrap_compare_node(), std::stack<_Tp, _Sequence>::pop(), ptr_fun(), std::stack<_Tp, _Sequence>::push(), and std::stack<_Tp, _Sequence>::top().

template <typename T>
int Glib::Tree<T>::index_of ( const T &  data  )  const [inline]

Gets the position of the first child which contains the given data.

Parameters:
data The data to find.
Returns:
The index of the child which contains data, or -1 if the data is not found.

References Glib::Tree<T>::nth_child().

template <typename T>
int Glib::Tree<T>::position_of ( const Tree<T>&  child  )  const [inline]

Gets the position with respect to its siblings.

child must be a child of node. The first child is numbered 0, the second 1, and so on.

Parameters:
child A child
Returns:
The position of child with respect to its siblings.

References Glib::Tree<T>::gobj(), and Glib::Tree<T>::gobject_.

template <typename T>
Tree<T>* Glib::Tree<T>::first_child (  )  const [inline]

Gets the first child.

Returns:
The first child, or 0 if the node has no children.

References Glib::Tree<T>::gobject_, and Glib::Tree<T>::lookup().

template <typename T>
Tree<T>* Glib::Tree<T>::last_child (  )  const [inline]

Gets the last child.

Returns:
The last child, or 0 if the node has no children.

References Glib::Tree<T>::gobject_, and Glib::Tree<T>::lookup().

template <typename T>
Tree<T>* Glib::Tree<T>::nth_child ( int  n  )  const [inline]

Gets the nth child.

Returns:
The nth child, or 0 if n is too large.

References Glib::Tree<T>::gobject_, and Glib::Tree<T>::lookup().

Referenced by Glib::Tree<T>::index_of().

template <typename T>
Tree<T>* Glib::Tree<T>::first_sibling (  )  const [inline]

Gets the first sibling.

Returns:
The first sibling, or 0 if the node has no siblings.

References Glib::Tree<T>::gobject_, and Glib::Tree<T>::parent_.

template <typename T>
Tree<T>* Glib::Tree<T>::prev_sibling (  )  const [inline]

Gets the previous sibling.

Returns:
The previous sibling, or 0 if the node has no siblings.

References Glib::Tree<T>::gobject_, and Glib::Tree<T>::parent_.

template <typename T>
Tree<T>* Glib::Tree<T>::next_sibling (  )  const [inline]

Gets the next sibling.

Returns:
The next sibling, or 0 if the node has no siblings.

References Glib::Tree<T>::gobject_, and Glib::Tree<T>::parent_.

template <typename T>
Tree<T>* Glib::Tree<T>::last_sibling (  )  const [inline]

Gets the last sibling.

Returns:
The last sibling, or 0 if the node has no siblings.

References Glib::Tree<T>::gobject_, and Glib::Tree<T>::parent_.

template <typename T>
bool Glib::Tree<T>::is_leaf (  )  const [inline]

Returns true if this is a leaf node.

Returns:
true if this is a leaf node.

References Glib::Tree<T>::gobject_.

template <typename T>
bool Glib::Tree<T>::is_root (  )  const [inline]

Returns true if this is the root node.

Returns:
true if this is the root node.

References Glib::Tree<T>::gobject_.

template <typename T>
unsigned int Glib::Tree<T>::depth (  )  const [inline]

Gets the depth of this node.

The root node has a depth of 1. For the children of the root node the depth is 2. And so on.

Returns:
the depth of this node

References Glib::Tree<T>::gobject_.

Referenced by Glib::Tree<T>::find().

template <typename T>
unsigned int Glib::Tree<T>::node_count ( TraverseFlags  flags  )  const [inline]

Gets the number of nodes in a tree.

Parameters:
flags Which types of children are to be counted: one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES
Returns:
The number of nodes in the tree.

References Glib::Tree<T>::gobject_.

template <typename T>
unsigned int Glib::Tree<T>::child_count (  )  const [inline]

Gets the number children.

Returns:
The number of children.

References Glib::Tree<T>::gobject_.

template <typename T>
bool Glib::Tree<T>::is_ancestor ( const Tree<T>&  descendant  )  const [inline]

Returns true if this is an ancestor of descendant.

This is true if this is the parent of descendant, or if this is the grandparent of descendant etc.

Parameters:
descendant A node.
Returns:
true if this is an ancestor of descendant.

References Glib::Tree<T>::gobj(), and Glib::Tree<T>::gobject_.

template <typename T>
unsigned int Glib::Tree<T>::max_height (  )  const [inline]

Gets the maximum height of all branches beneath this node.

This is the maximum distance from the node to all leaf nodes. If root has no children, 1 is returned. If root has children, 2 is returned. And so on.

Returns:
The maximum height of all branches.

References Glib::Tree<T>::gobject_.

template <typename T>
void Glib::Tree<T>::unlink ( Tree<T>&  child  )  [inline]

Unlinks a node from a tree, resulting in two separate trees.

References Glib::Tree<T>::children_, std::map<_Key, _Tp, _Compare, _Alloc>::erase(), Glib::Tree<T>::gobj(), and Glib::Tree<T>::parent().

template <typename T>
iterator Glib::Tree<T>::iter (  )  const [inline]

Accessor for this node's iterator.

References Glib::Tree<T>::gobject_.

template <typename T>
T& Glib::Tree<T>::data (  )  const [inline]

Accessor for this node's data.

References Glib::Tree<T>::gobject_.

template <typename T>
Tree<T>* Glib::Tree<T>::lookup ( const iterator  child  )  const [inline]

Lookup a child by its iterator.

Parameters:
child The iterator of the desired child.
Returns:
The child if found, else 0.

References Glib::Tree<T>::children_, std::map<_Key, _Tp, _Compare, _Alloc>::end(), and std::map<_Key, _Tp, _Compare, _Alloc>::find().

Referenced by Glib::Tree<T>::find(), Glib::Tree<T>::find_child(), Glib::Tree<T>::first_child(), Glib::Tree<T>::last_child(), and Glib::Tree<T>::nth_child().

template <typename T>
const GNode* Glib::Tree<T>::gobj (  )  const [inline]

template <typename T>
Tree<T>* Glib::Tree<T>::parent ( Tree<T>*  newparent = 0  )  [inline, protected]

Accessor for this node's parent.

Parameters:
newparent Provides a new parent for this node. Use 0 to get the current parent.
Returns:
The node's parent.

References Glib::Tree<T>::parent_.

Referenced by Glib::Tree<T>::append(), Glib::Tree<T>::insert(), Glib::Tree<T>::insert_after(), Glib::Tree<T>::insert_before(), Glib::Tree<T>::prepend(), and Glib::Tree<T>::unlink().

template <typename T>
static gboolean Glib::Tree<T>::wrap_traverse_slot ( GNode *  node,
gpointer  slot 
) [inline, static, protected]

Wrapper for invoking a TraverseFunc.

Referenced by Glib::Tree<T>::traverse().

template <typename T>
static void Glib::Tree<T>::wrap_foreach_slot ( GNode *  node,
gpointer  slot 
) [inline, static, protected]

Wrapper for invoking a ForeachFunc.

Referenced by Glib::Tree<T>::foreach().

template <typename T>
static void Glib::Tree<T>::on_compare_child ( GNode *  node,
const T &  needle,
GNode *  result 
) [inline, static, protected]

Method for comparing a single child (Internal use).

Referenced by Glib::Tree<T>::find_child().

template <typename T>
static void Glib::Tree<T>::on_wrap_compare_child ( GNode *  node,
gpointer  data 
) [inline, static, protected]

Wrapper for invoking a sigc::slot<void,GNode*> (Internal use).

Referenced by Glib::Tree<T>::find_child().

template <typename T>
static gboolean Glib::Tree<T>::on_compare_node ( GNode *  node,
const T &  needle,
GNode **  result 
) [inline, static, protected]

Method for comparing a single node (Internal use).

Referenced by Glib::Tree<T>::find().

template <typename T>
static gboolean Glib::Tree<T>::on_wrap_compare_node ( GNode *  node,
gpointer  data 
) [inline, static, protected]

Wrapper for invoking a sigc::slot<gboolean,GNode*> (Internal use).

Referenced by Glib::Tree<T>::find().


Member Data Documentation

template <typename T>
bool Glib::Tree<T>::owns_gobject_ [protected]


The documentation for this class was generated from the following file:

Generated for glibmm 2.4 by Doxygen 1.5.5 © 1997-2001