Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
branch_iter.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_UTIL_BRANCH_ITER_HH
27 # define MLN_UTIL_BRANCH_ITER_HH
28 
36 # include <stack>
37 # include <mln/util/tree.hh>
38 
39 namespace mln
40 {
41 
42  namespace util
43  {
44 
51  template <typename T>
53  {
54  public:
56 
58  operator util::tree_node<T>&() const;
59  util::tree_node<T>& operator *();
60 
62  bool is_valid() const;
63 
65  void invalidate();
66 
68  void start();
69 
71  void next();
72 
74  unsigned deepness() const;
75  private:
77  util::branch<T> branch_;
78 
79  typedef typename std::vector< util::tree_node<T>* >::iterator child_iter;
80  typedef std::pair<child_iter, child_iter> iter_pair;
82  std::stack< iter_pair > s_;
83 
85  };
86 
87 
88 # ifndef MLN_INCLUDE_ONLY
89 
90 
91  template <typename T>
92  inline
94  : branch_(branch)
95  {
96  invalidate();
97  }
98 
99  template <typename T>
100  inline
102  {
103  mln_assertion(n_);
104  return *n_;
105  }
106 
107  template <typename T>
108  inline
111  {
112  mln_assertion(n_);
113  return *n_;
114  }
115 
116  template <typename T>
117  inline
118  unsigned
120  {
121  mln_assertion(is_valid());
122  unsigned i = 0;
123  tree_node<T>* p = n_;
124  while (p)
125  {
126  p = p->parent();
127  i++;
128  }
129  return i;
130  }
131 
132  template <typename T>
133  inline
134  bool
136  {
137  return n_ != 0;
138  }
139 
140  template <typename T>
141  inline
142  void
144  {
145  n_ = 0;
146  }
147 
148 
149  template <typename T>
150  inline
151  void
153  {
154  s_.push(iter_pair(branch_.apex().children().begin(),
155  branch_.apex().children().end()));
156  n_ = &branch_.apex();
157  }
158 
159  template <typename T>
160  inline
161  void
163  {
164  if (s_.size() == 0)
165  invalidate();
166  else
167  {
168  if (s_.top().first == s_.top().second)
169  {
170  s_.pop();
171  next();
172  return;
173  }
174  else
175  {
176  n_ = *(s_.top().first);
177  s_.top().first++;
178 
179  if (!n_)
180  {
181  next();
182  return;
183  }
184 
185  mln_assertion(n_);
186  if (n_->children().size() > 0)
187  {
188  s_.push(iter_pair(n_->children().begin(),
189  n_->children().end()));
190  }
191  return;
192  }
193  }
194  }
195 
196 # endif // ! MLN_INCLUDE_ONLY
197 
198 
199  }
200 
201 } // end of namespace mln
202 
203 
204 #endif // ! MLN_UTIL_BRANCH_ITER_HH