$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
branch_iter_ind.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_IND_HH
28 # define MLN_UTIL_BRANCH_ITER_IND_HH
29 
37 # include <stack>
38 # include <mln/util/tree.hh>
39 
40 namespace mln
41 {
42 
43  namespace util
44  {
45  template<typename T>
46  struct bi_elt
47  {
48  typedef std::vector< util::tree_node<T>* > child_list;
49 
51  : list_(list),
52  previous_(0),
53  pos_(-1) {}
54 
57  int pos_;
58  };
59 
68  template <typename T>
70  {
71  public:
73 
75  operator util::tree_node<T>&() const;
77 
79  bool is_valid() const;
80 
82  void invalidate();
83 
85  void start();
86 
88  void next();
89 
91  unsigned deepness() const;
92 
93  private:
95  util::branch<T> branch_;
96 
98  std::stack< bi_elt<T> > s_;
99 
100  util::tree_node<T>* n_;
101  };
102 
103 # ifndef MLN_INCLUDE_ONLY
104 
105 
106  template <typename T>
107  inline
109  : branch_(branch)
110  {
111  invalidate();
112  }
113 
114  template <typename T>
115  inline
116  branch_iter_ind<T>::operator util::tree_node<T>&() const
117  {
118  mln_assertion(n_);
119  return *n_;
120  }
121 
122  template <typename T>
123  inline
124  util::tree_node<T>&
126  {
127  mln_assertion(n_);
128  return *n_;
129  }
130 
131  template <typename T>
132  inline
133  unsigned
135  {
136  mln_assertion(is_valid());
137  unsigned i = 0;
138  tree_node<T>* p = n_;
139  while (p)
140  {
141  p = p->parent();
142  i++;
143  }
144  return i;
145  }
146 
147  template <typename T>
148  inline
149  bool
151  {
152  return n_ != 0;
153  }
154 
155  template <typename T>
156  inline
157  void
159  {
160  n_ = 0;
161  }
162 
163 
164  template <typename T>
165  inline
166  void
168  {
169  s_.push(bi_elt<T>(&branch_.apex().children()));
170 
171  n_ = &branch_.apex();
172  }
173 
174  template <typename T>
175  inline
176  void
178  {
179  // First : list of children.
180  // Second : i;
181 
182  if (s_.size() == 0)
183  invalidate();
184  else
185  {
186  s_.top().pos_++;
187  if (s_.top().list_->size() == (unsigned)s_.top().pos_)
188  {
189  s_.pop();
190  next();
191  return;
192  }
193  else
194  {
195  mln_assertion(s_.top().list_->size() > (unsigned)s_.top().pos_);
196  if (s_.top().previous_ != 0)
197  mln_assertion(s_.top().previous_ == (*(s_.top().list_))[s_.top().pos_ - 1]);
198 
199  n_ = (*(s_.top().list_))[s_.top().pos_];
200  s_.top().previous_ = n_;
201 
202  if (!n_)
203  {
204  next();
205  return;
206  }
207 
208  mln_assertion(n_);
209  if (n_->children().size() > 0)
210  {
211  s_.push(bi_elt<T>(&n_->children()));
212  }
213  return;
214  }
215  }
216  }
217 
218 # endif // ! MLN_INCLUDE_ONLY
219 
220 
221  }
222 
223 } // end of namespace mln
224 
225 
226 #endif // ! MLN_UTIL_BRANCH_ITER_IND_HH