$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
p_queue_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_CORE_SITE_SET_P_QUEUE_FAST_HH
27 # define MLN_CORE_SITE_SET_P_QUEUE_FAST_HH
28 
35 
36 # include <mln/core/site_set/p_array.hh>
37 
38 
39 namespace mln
40 {
41 
42  // Forward declaration.
43  template <typename P> class p_queue_fast;
44 
45 
46 
47  namespace trait
48  {
49 
50  template <typename P>
51  struct site_set_< p_queue_fast<P> >
52  {
53  typedef trait::site_set::nsites::known nsites;
54  typedef trait::site_set::bbox::unknown bbox;
55  typedef trait::site_set::contents::growing contents;
56  typedef trait::site_set::arity::multiple arity;
57  };
58 
59  } // end of namespace trait
60 
61 
62 
66 
71  template <typename P>
72  class p_queue_fast : public internal::site_set_base_< P, p_queue_fast<P> >
73  {
74  typedef p_queue_fast<P> self_;
75  public:
76 
78  typedef P element;
79 
82 
85 
88 
90  typedef fwd_piter piter;
91 
92 
94  p_queue_fast();
95 
97  void reserve(typename p_array<P>::size_type n);
98 
100  bool has(const psite& p) const;
101 
103  bool has(const util::index& i) const;
104 
106  bool is_valid() const;
107 
109  bool compute_has(const P& p) const;
110 
112  unsigned nsites() const;
113 
115  bool empty() const;
116 
118  void push(const P& p);
119 
121  typedef P i_element;
122 
124  void insert(const P& p);
125 
126 
129  void pop();
130 
133  const P& front() const;
134 
138  const P& pop_front();
139 
140 
142  void purge();
143 
145  void clear();
146 
147 
149  const P& operator[](unsigned i) const;
150 
152  const std::vector<P>& std_vector() const;
153 
155  std::size_t memory_size() const;
156 
157  protected:
158 
160  unsigned begin_;
161  unsigned end_;
162  };
163 
164 
165 
166 # ifndef MLN_INCLUDE_ONLY
167 
168  template <typename P>
169  inline
171  {
172  begin_ = 0;
173  end_ = 0;
174  }
175 
176  template <typename P>
177  inline
178  void
179  p_queue_fast<P>::reserve(typename p_array<P>::size_type n)
180  {
181  q_.reserve(n);
182  }
183 
184  template <typename P>
185  inline
186  void
188  {
189  std::vector<P>& v = q_.hook_std_vector_();
190  std::copy(v.begin() + begin_,
191  v.begin() + end_,
192  v.begin());
193  v.resize(end_ - begin_);
194  end_ -= begin_;
195  begin_ = 0;
196  }
197 
198  template <typename P>
199  inline
200  bool
201  p_queue_fast<P>::has(const psite& p) const
202  {
203  mln_precondition(p.target_() == this); // FIXME: Refine.
204  if (p.index() < 0 || unsigned(p.index()) >= nsites())
205  return false;
206  // The type of rhs below is mln_site(p_array<P>).
207 // mln_invariant(p.to_site() == (*this)[p.index()]);
208  return true;
209  }
210 
211  template <typename P>
212  inline
213  bool
214  p_queue_fast<P>::has(const util::index& i) const
215  {
216  return i >= 0 && unsigned(i) < nsites();
217  }
218 
219  template <typename P>
220  inline
221  bool
222  p_queue_fast<P>::compute_has(const P& p) const
223  {
224  for (unsigned i = begin_; i < end_; ++i)
225  if (q_[i] == p)
226  return true;
227  return false;
228  }
229 
230  template <typename P>
231  inline
232  bool
234  {
235  return true;
236  }
237 
238  template <typename P>
239  inline
240  unsigned
242  {
243  mln_invariant(end_ >= begin_);
244  return end_ - begin_;
245  }
246 
247  template <typename P>
248  inline
249  bool
250  p_queue_fast<P>::empty() const
251  {
252  mln_invariant(end_ >= begin_);
253  return end_ == begin_;
254  }
255 
256  template <typename P>
257  inline
258  void
259  p_queue_fast<P>::push(const P& p)
260  {
261  q_.append(p);
262  ++end_;
263  }
264 
265  template <typename P>
266  inline
267  void
269  {
270  mln_precondition(! this->is_empty());
271  ++begin_;
272  }
273 
274  template <typename P>
275  inline
276  const P&
277  p_queue_fast<P>::front() const
278  {
279  mln_precondition(! this->is_empty());
280  return q_[begin_];
281  }
282 
283  template <typename P>
284  inline
285  const P&
287  {
288  mln_precondition(! this->is_empty());
289  const P& res = this->front();
290  this->pop();
291  return res;
292  }
293 
294  template <typename P>
295  inline
296  void
298  {
299  end_ = begin_;
300  }
301 
302  template <typename P>
303  inline
304  const P&
305  p_queue_fast<P>::operator[](unsigned i) const
306  {
307  mln_precondition(i < nsites());
308  return q_[begin_ + i];
309  }
310 
311  template <typename P>
312  inline
313  void
314  p_queue_fast<P>::insert(const P& p)
315  {
316  this->push(p);
317  }
318 
319  template <typename P>
320  inline
321  const std::vector<P>&
323  {
324  return q_.std_vector();
325  }
326 
327  template <typename P>
328  inline
329  std::size_t
331  {
332  return q_.memory_size() + 2 * sizeof(unsigned);
333  }
334 
335 # endif // ! MLN_INCLUDE_ONLY
336 
337 } // end of namespace mln
338 
339 
340 #endif // ! MLN_CORE_SITE_SET_P_QUEUE_FAST_HH