$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
line_links.hh
1 // Copyright (C) 2011 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 SCRIBO_CORE_LINE_LINKS_HH
27 # define SCRIBO_CORE_LINE_LINKS_HH
28 
32 
33 
34 # include <mln/util/array.hh>
35 # include <mln/util/tracked_ptr.hh>
36 
37 # include <scribo/core/concept/serializable.hh>
38 # include <scribo/core/line_set.hh>
39 
40 
41 namespace scribo
42 {
43 
44  using namespace mln;
45 
46  // Forward declaration.
47  template <typename L> class line_links;
48 
49 
50  namespace internal
51  {
53  template <typename L>
55  {
57  line_links_data(const line_set<L>& lines, unsigned size);
58  line_links_data(const line_set<L>& lines,
59  unsigned size, line_id_t value);
60 
61  mln::util::array<line_id_t> line_to_link_;
62  line_set<L> lines_;
63  };
64 
65  } // end of namespace scribo::internal
66 
67 
68 
69 
73  //
74  template <typename L>
75  class line_links : public Serializable<line_links<L> >
76  {
78 
79  public:
80  line_links();
81  line_links(const line_set<L>& lines);
82  line_links(const line_set<L>& lines, line_id_t value);
83 
84  const line_set<L>& lines() const;
85 
86  bool is_valid() const;
87 
88  unsigned nelements() const;
89 
90  line_id_t& operator()(line_id_t comp_id);
91  const line_id_t& operator()(line_id_t comp_id) const;
92 
93  const mln::util::array<line_id_t>& line_to_link() const;
94 
95  void init();
96 
97  line_links<L> duplicate() const;
98 
99  private:
101  };
102 
103 
104  template <typename L>
105  std::ostream&
106  operator<<(std::ostream& ostr, const line_links<L>& links);
107 
108  template <typename L>
109  bool
110  operator==(const line_links<L>& lhs, const line_links<L>& rhs);
111 
112 
113 # ifndef MLN_INCLUDE_ONLY
114 
115 
116  namespace internal
117  {
118 
119 
121  template <typename L>
122  line_links_data<L>::line_links_data()
123  {
124  }
125 
126 
127  template <typename L>
128  line_links_data<L>::line_links_data(const line_set<L>& lines,
129  unsigned size)
130  : line_to_link_(size), lines_(lines)
131  {
132  };
133 
134 
135  template <typename L>
136  line_links_data<L>::line_links_data(const line_set<L>& lines,
137  unsigned size, line_id_t value)
138  : line_to_link_(size, value), lines_(lines)
139  {
140  };
141 
142 
143  } // end of namespace scribo::internal
144 
145 
146 
147  template <typename L>
148  line_links<L>::line_links()
149  : data_(0)
150  {
151  }
152 
153 
154  template <typename L>
155  line_links<L>::line_links(const line_set<L>& lines)
156  {
157  data_ = new data_t(lines, value::next(lines.nelements()));
158  }
159 
160 
161  template <typename L>
162  line_links<L>::line_links(const line_set<L>& lines,
163  line_id_t value)
164  {
165  data_ = new data_t(lines, value::next(lines.nelements()),
166  value);
167  }
168 
169 
170  template <typename L>
171  const line_set<L>&
172  line_links<L>::lines() const
173  {
174  return data_->lines_;
175  }
176 
177 
178  template <typename L>
179  bool
180  line_links<L>::is_valid() const
181  {
182  return data_->lines_.is_valid()
183  && data_->lines_.nelements() == (this->nelements() - 1);
184  }
185 
186 
187  template <typename L>
188  unsigned
189  line_links<L>::nelements() const
190  {
191  return data_->line_to_link_.nelements();
192  }
193 
194 
195  template <typename L>
196  line_id_t&
197  line_links<L>::operator()(line_id_t comp_id)
198  {
199  return data_->line_to_link_(comp_id);
200  }
201 
202 
203  template <typename L>
204  const line_id_t&
205  line_links<L>::operator()(line_id_t comp_id) const
206  {
207  return data_->line_to_link_(comp_id);
208  }
209 
210 
211  template <typename L>
213  line_links<L>::line_to_link() const
214  {
215  return data_->line_to_link_;
216  }
217 
218 
219  template <typename L>
220  void
221  line_links<L>::init()
222  {
223  for (unsigned i = 0; i < nelements(); ++i)
224  if (!data_->lines_(i).is_valid()
225  || !data_->lines_(i).is_textline())
226  {
227  data_->line_to_link_(i) = 0;
228  }
229  else
230  {
231  data_->line_to_link_(i) = i;
232  }
233  }
234 
235  template <typename L>
236  inline
237  line_links<L>
239  {
240  line_links<L> output;
241  output.data_ = new data_t();
242 
243  *(output.data_.ptr_) = *(data_.ptr_);
244  return output;
245  }
246 
247 
248  template <typename L>
249  std::ostream&
250  operator<<(std::ostream& ostr, const line_links<L>& links)
251  {
252  ostr << "line_links[";
253 
254  for_all_links(l, links)
255  ostr << l << "->" << links.line_to_link()[l] << ", ";
256 
257  ostr << "]";
258 
259  return ostr;
260  }
261 
262 
263  template <typename L>
264  bool
265  operator==(const line_links<L>& lhs, const line_links<L>& rhs)
266  {
267  return lhs.lines() == rhs.lines()
268  && lhs.line_to_link() == rhs.line_to_link();
269  }
270 
271 # endif // ! MLN_INCLUDE_ONLY
272 
273 
274 } // end of namespace scribo
275 
276 
277 #endif // ! SCRIBO_CORE_LINE_LINKS_HH