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