$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
p_queue.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_HH
27 # define MLN_CORE_SITE_SET_P_QUEUE_HH
28 
32 
40 # include <deque>
41 # include <mln/core/site_set/p_array.hh>
42 
43 
44 namespace mln
45 {
46 
47  // Forward declaration.
48  template <typename P> class p_queue;
49 
50 
51  namespace trait
52  {
53 
54  template <typename P>
55  struct site_set_< p_queue<P> >
56  {
57  typedef trait::site_set::nsites::known nsites;
58  typedef trait::site_set::bbox::unknown bbox;
59  typedef trait::site_set::contents::growing contents;
60  typedef trait::site_set::arity::multiple arity;
61  };
62 
63  } // end of namespace trait
64 
65 
66 
70 
73  template <typename P>
74  class p_queue : public internal::site_set_base_< P, p_queue<P> >
75  {
76  typedef p_queue<P> self_;
77  public:
78 
80  typedef P element;
81 
82 
85 
88 
91 
93  typedef fwd_piter piter;
94 
95 
97  p_queue();
98 
99 
101  bool has(const psite& p) const;
102 
104  bool has(const util::index& i) const;
105 
107  bool is_valid() const;
108 
109 
111  unsigned nsites() const;
112 
113 
115  void push(const P& p);
116 
118  typedef P i_element;
119 
121  void insert(const P& p);
122 
123 
126  void pop();
127 
130  const P& front() const;
131 
135  P pop_front();
136 
137 
139  void clear();
140 
141 
143  const P& operator[](unsigned i) const;
144 
146  const std::deque<P>& std_deque() const;
147 
149  std::size_t memory_size() const;
150 
151 
152  protected:
153 
154  std::deque<P> q_;
155  };
156 
157 
158 
159 # ifndef MLN_INCLUDE_ONLY
160 
161  template <typename P>
162  inline
164  {
165  }
166 
167  template <typename P>
168  inline
169  bool
170  p_queue<P>::has(const psite& p) const
171  {
172  mln_precondition(p.target_() == this); // FIXME: Refine.
173  if (p.index() < 0 || unsigned(p.index()) >= nsites())
174  return false;
175  // The type of rhs below is mln_site(p_array<P>).
176  mln_invariant(p.to_site() == (*this)[p.index()]);
177  return true;
178  }
179 
180  template <typename P>
181  inline
182  bool
183  p_queue<P>::has(const util::index& i) const
184  {
185  return i >= 0 && unsigned(i) < nsites();
186  }
187 
188  template <typename P>
189  inline
190  bool
191  p_queue<P>::is_valid() const
192  {
193  return true;
194  }
195 
196  template <typename P>
197  inline
198  unsigned
199  p_queue<P>::nsites() const
200  {
201  return q_.size();
202  }
203 
204  template <typename P>
205  inline
206  void
207  p_queue<P>::push(const P& p)
208  {
209  q_.push_back(p);
210  }
211 
212  template <typename P>
213  inline
214  void
216  {
217  mln_precondition(! this->is_empty());
218  q_.pop_front();
219  }
220 
221  template <typename P>
222  inline
223  const P&
224  p_queue<P>::front() const
225  {
226  mln_precondition(! this->is_empty());
227  return q_.front();
228  }
229 
230  template <typename P>
231  inline
232  P
234  {
235  mln_precondition(! this->is_empty());
236  P res = this->front();
237  this->pop();
238  return res;
239  }
240 
241  template <typename P>
242  inline
243  void
245  {
246  q_.clear();
247  }
248 
249  template <typename P>
250  inline
251  const P&
252  p_queue<P>::operator[](unsigned i) const
253  {
254  mln_precondition(i < nsites());
255  return q_[i];
256  }
257 
258  template <typename P>
259  inline
260  void
261  p_queue<P>::insert(const P& p)
262  {
263  this->push(p);
264  }
265 
266  template <typename P>
267  inline
268  const std::deque<P>&
269  p_queue<P>::std_deque() const
270  {
271  return q_;
272  }
273 
274  template <typename P>
275  inline
276  std::size_t
278  {
279  return sizeof(q_) + nsites() * sizeof(P);
280  }
281 
282 # endif // ! MLN_INCLUDE_ONLY
283 
284 } // end of namespace mln
285 
286 
287 #endif // ! MLN_CORE_SITE_SET_P_QUEUE_HH