graph  5.0.5
Graph: generic attributed relational graphs
base.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef GRAPH_TREE_BASE_H
35 #define GRAPH_TREE_BASE_H
36 
37 
38 //--- header files ------------------------------------------------------------
39 
41 #include <list>
42 #include <algorithm>
43 
44 
45 //--- class declarations ------------------------------------------------------
46 
54 {
55 public:
56  typedef std::list<BaseTree *>::iterator iterator;
57  typedef std::list<BaseTree *>::const_iterator const_iterator;
58  typedef std::list<BaseTree *>::reverse_iterator reverse_iterator;
59  typedef std::list<BaseTree *>::const_reverse_iterator const_reverse_iterator;
60 
66  BaseTree( bool allowsChildren = true );
67  virtual ~BaseTree();
68 
69  const std::list<BaseTree *> & children() const;
71  bool getAllowsChildren() const;
73  BaseTree* getChildAt( unsigned childIndex ) const;
75  int getIndex( BaseTree* node ) const;
77  BaseTree* getParent() const;
79  BaseTree* getTopParent();
81  const BaseTree* getTopParent() const;
83  bool isLeaf() const;
84 
86  void insert( BaseTree* child, int index=-1 );
88  void remove( unsigned index );
90  void remove( BaseTree* node );
92  void removeFromParent();
94  void setParent( BaseTree* newParent );
96  void clear();
97 
104  virtual size_t size() const
105  __attribute__((__deprecated__("use childrenSize() for the number of children")));
107  virtual size_t childrenSize() const;
108  const_iterator begin() const;
109  const_iterator end() const;
110  const_reverse_iterator rbegin() const;
111  const_reverse_iterator rend() const;
113 
114 protected:
115 
116 private:
117  bool _allowsChildren;
118  BaseTree *_parent;
119  std::list<BaseTree *> _children;
120 };
121 
122 
123 //--- inline methods ----------------------------------------------------------
124 
125 inline BaseTree::BaseTree( bool allowsChildren )
126  : _allowsChildren( allowsChildren ), _parent( 0 )
127 {
128 }
129 
130 
131 inline bool BaseTree::getAllowsChildren() const
132 {
133  return _allowsChildren;
134 }
135 
136 
137 inline BaseTree* BaseTree::getChildAt( unsigned childIndex ) const
138 {
139  const_iterator it;
140  unsigned i;
141 
142  for( i=0, it=_children.begin(); i<childIndex && it!=_children.end();
143  ++it, ++i ) {}
144  if( it != _children.end() ) return *it;
145  else return 0;
146 }
147 
148 
149 inline size_t BaseTree::size() const
150 {
151  return _children.size();
152 }
153 
154 
155 inline size_t BaseTree::childrenSize() const
156 {
157  return _children.size();
158 }
159 
160 
162 {
163  return _parent;
164 }
165 
166 
167 inline bool BaseTree::isLeaf() const
168 {
169  return _children.size() == 0;
170 }
171 
172 
174 {
175  return _children.begin();
176 }
177 
178 
180 {
181  return _children.end();
182 }
183 
184 
186 {
187  return _children.rbegin();
188 }
189 
190 
192 {
193  return _children.rend();
194 }
195 
196 
198 {
199  if( getParent() ) getParent()->remove( this );
200  setParent( 0 );
201 }
202 
203 
204 inline void BaseTree::setParent( BaseTree* newParent )
205 {
206  _parent = newParent;
207 }
208 
209 
210 #endif
std::list< BaseTree * >::const_reverse_iterator const_reverse_iterator
Definition: base.h:59
BaseTree * getParent() const
Returns the parent BaseTree of the receiver.
Definition: base.h:161
void setParent(BaseTree *newParent)
Sets the parent of the receiver to newParent.
Definition: base.h:204
BaseTree element.
Definition: base.h:53
virtual size_t childrenSize() const
Returns the number of children BaseTree the receiver contains.
Definition: base.h:155
virtual size_t size() const __attribute__((__deprecated__("use childrenSize() for the number of children")))
deprecated - use childrenSize() for the number of children.
Definition: base.h:149
STL namespace.
const_reverse_iterator rbegin() const
Definition: base.h:185
#define GRAPH_API
Definition: graph_config.h:46
std::list< BaseTree * >::const_iterator const_iterator
Definition: base.h:57
std::list< BaseTree * >::iterator iterator
Definition: base.h:56
const_iterator end() const
Definition: base.h:179
const_iterator begin() const
Definition: base.h:173
bool getAllowsChildren() const
Returns true if the receiver allows children.
Definition: base.h:131
BaseTree * getChildAt(unsigned childIndex) const
Returns the child BaseTree pointer at index childIndex.
Definition: base.h:137
void removeFromParent()
Removes the receiver from its parent.
Definition: base.h:197
bool isLeaf() const
Returns true if the receiver is a leaf (terminal node)
Definition: base.h:167
std::list< BaseTree * >::reverse_iterator reverse_iterator
Definition: base.h:58
const_reverse_iterator rend() const
Definition: base.h:191