2 CLAW - a C++ Library Absolutely Wonderful
4 CLAW is a free library without any particular aim but being useful to
7 Copyright (C) 2005-2011 Julien Jorge
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 contact: julien.jorge@gamned.org
27 * \brief Implementation of the claw::tree class.
28 * \author Julien Jorge
31 /*----------------------------------------------------------------------------*/
33 * \brief Default constructor.
42 /*----------------------------------------------------------------------------*/
44 * \brief Constructor with initialization.
45 * \param v The value of the root node.
48 claw::tree<T>::tree( const T& v )
54 /*----------------------------------------------------------------------------*/
56 * \brief Equality operator.
57 * \param that The tree to compare to.
60 bool claw::tree<T>::operator==( const self_type& that ) const
62 bool result = ( value == that.value );
66 typename child_list::const_iterator it_me = m_child.begin();
67 typename child_list::const_iterator it_him = that.m_child.begin();
68 typename child_list::const_iterator eit_me = m_child.end();
69 typename child_list::const_iterator eit_him = that.m_child.end();
71 while ( result && (it_me!=eit_me) && (it_him!=eit_him) )
72 result = (*it_me == *it_him);
74 if ( (it_me!=eit_me) || (it_him!=eit_him) )
79 } // tree::operator==()
81 /*----------------------------------------------------------------------------*/
83 * \brief Tell if this node is a leaf (ie. it has no child).
86 bool claw::tree<T>::is_leaf() const
88 return m_child.empty();
91 /*----------------------------------------------------------------------------*/
93 * \brief Add a child to this node.
94 * \param v The value of the child to add.
97 typename claw::tree<T>::self_type& claw::tree<T>::add_child( const T& v )
99 m_child.push_back( self_type(v) );
100 return m_child.back();
101 } // tree::add_child()
103 /*----------------------------------------------------------------------------*/
105 * \brief Add a child subtree to this node.
106 * \param v The tree to add.
109 typename claw::tree<T>::self_type&
110 claw::tree<T>::add_child( const self_type& v )
112 m_child.push_back( v );
113 } // tree::add_child()
115 /*----------------------------------------------------------------------------*/
117 * \brief Search the first child having a given value.
118 * \param v The value of the child to find.
121 typename claw::tree<T>::iterator claw::tree<T>::find( const T& v )
123 typename child_list::iterator it;
126 for ( it=m_child.begin(); !found && (it!=end()) ; )
127 if ( it->value == v )
132 return iterator( it );
135 /*----------------------------------------------------------------------------*/
137 * \brief Search the first child having a given value.
138 * \param v The value of the child to find.
141 typename claw::tree<T>::const_iterator claw::tree<T>::find( const T& v ) const
143 typename child_list::const_iterator it;
146 for ( it=m_child.begin(); !found && (it!=end()) ; )
147 if ( it->value == v )
152 return const_iterator( it );
155 /*----------------------------------------------------------------------------*/
157 * \brief Get an iterator on the begining of the children.
160 typename claw::tree<T>::iterator claw::tree<T>::begin()
162 return iterator( m_child.begin() );
165 /*----------------------------------------------------------------------------*/
167 * \brief Get an iterator just past the end of the children.
170 typename claw::tree<T>::iterator claw::tree<T>::end()
172 return iterator( m_child.end() );
175 /*----------------------------------------------------------------------------*/
177 * \brief Get a constant iterator on the begining of the children.
180 typename claw::tree<T>::const_iterator claw::tree<T>::begin() const
182 return const_iterator( m_child.begin() );
185 /*----------------------------------------------------------------------------*/
187 * \brief Get a constant iterator just past the end of the children.
190 typename claw::tree<T>::const_iterator claw::tree<T>::end() const
192 return const_iterator( m_child.end() );