$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tree_fast.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_TREE_FAST_HH
27 # define MLN_UTIL_TREE_FAST_HH
28 
29 # include <vector>
30 # include <mln/core/contract.hh>
31 
35 
36 
37 namespace mln
38 {
39 
40  namespace util
41  {
42 
43  template <typename T>
44  struct tree_fast
45  {
49  tree_fast();
50 
55  tree_fast(T& elt);
56 
61  unsigned size() const;
62 
63 
68  bool has (T& elt) const;
69 
70 
77  unsigned search (T& elt) const;
78 
79 
84  bool is_root (unsigned i) const;
85 
86 
91  unsigned add_child (unsigned i, T& elt);
92 
93 
98  unsigned add_parent (T& elt);
99 
101  std::vector<T> data_;
102 
104  std::vector<unsigned> parent_;
105 
107  std::vector<std::vector<unsigned> > child_;
108 
110  unsigned root_;
111  };
112 
113 
114 
115 # ifndef MLN_INCLUDE_ONLY
116 
117  template <typename T>
118  inline
120  {
121  }
122 
123  template <typename T>
124  inline
126  {
127  std::vector<unsigned> v;
128  data_.push_back(elt);
129  parent_.push_back(0);
130  child_.push_back(v);
131  root_ = 0;
132  }
133 
134  template <typename T>
135  inline
136  unsigned
137  tree_fast<T>::size() const
138  {
139  return (data_.size ());
140  }
141 
142 
143  template <typename T>
144  inline
145  bool
146  tree_fast<T>::has (T& elt) const
147  {
148  for (unsigned i = 0; i < data_.size (); ++i)
149  if (data_[i] == elt)
150  return true;
151 
152  return false;
153  }
154 
155  template <typename T>
156  inline
157  unsigned
158  tree_fast<T>::search (T& elt) const
159  {
160  for (unsigned i = 0; i < data_.size (); ++i)
161  if (data_[i] == elt)
162  return i;
163 
165  mln_assertion (false);
166  return (unsigned)(-1);
167  }
168 
169  template <typename T>
170  inline
171  bool
172  tree_fast<T>::is_root (unsigned i) const
173  {
174  return (root_ == i);
175  }
176 
177  template <typename T>
178  inline
179  unsigned
180  tree_fast<T>::add_child (unsigned i, T& elt)
181  {
182  mln_assertion (i < data_.size ());
183  std::vector<unsigned> v;
184  data_.push_back(elt);
185  parent_.push_back(i);
186  child_.push_back(v);
187  child_[i].push_back(data_.size () - 1);
188  return (data_.size () - 1);
189  }
190 
191  template <typename T>
192  inline
193  unsigned
195  {
196  data_.push_back(elt);
197  parent_.push_back(data_.size () - 1);
198  std::vector<unsigned> v;
199  v.push_back (root_);
200  child_.push_back(v);
201  parent_[root_] = data_.size () - 1;
202  root_ = data_.size () - 1;
203  return (data_.size () - 1);
204  }
205 
206 
207 
208 
209 # endif // ! MLN_INCLUDE_ONLY
210 
211  } // end of namespace mln::util
212 
213 } // end of namespace mln
214 
215 
216 #endif // ! MLN_UTIL_TREE_FAST_HH