Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
p_set_of.hh
1 // Copyright (C) 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_SET_OF_HH
27 # define MLN_CORE_SITE_SET_P_SET_OF_HH
28 
36 # include <mln/core/site_set/p_double.hh>
37 # include <mln/core/internal/site_set_base.hh>
38 # include <mln/core/internal/site_set_impl.hh>
39 # include <mln/util/set.hh>
40 
41 
42 
43 namespace mln
44 {
45 
46  // Forward declaration.
47  template <typename S> class p_set_of;
48 
49 
50  namespace trait
51  {
52 
53  template <typename S>
54  struct site_set_< p_set_of<S> >
55  {
56  typedef mln_trait_site_set_nsites(S) nsites;
57  typedef mln_trait_site_set_bbox(S) bbox;
58  typedef trait::site_set::contents::growing contents;
59  typedef trait::site_set::arity::multiple arity;
60  };
61 
62  } // end of namespace trait
63 
64 
65 
70  template <typename S>
71  class p_set_of : public internal::site_set_base_< mln_site(S),
72  p_set_of<S> >,
73  public internal::site_set_impl<S>,
74  private mlc_is_a(S, Site_Set)::check_t
75  {
76  typedef p_set_of<S> self_;
77  typedef util::set<S> set_;
78  public:
79 
81  typedef S element;
82 
83 
85  typedef p_double_psite<self_, element> psite;
86 
88  typedef p_double_piter<self_,
89  mln_fwd_eiter(set_),
90  mln_fwd_piter(S)> fwd_piter;
91 
93  typedef p_double_piter<self_,
94  mln_bkd_eiter(set_),
95  mln_bkd_piter(S)> bkd_piter;
96 
98  typedef fwd_piter piter;
99 
100 
102  p_set_of();
103 
104 
106  bool has(const psite& p) const;
107 
109  bool is_valid() const;
110 
111 
113  typedef S i_element;
114 
116  void insert(const S& s);
117 
118 
120  const S& operator[](unsigned i) const;
121 
123  unsigned nelements() const;
124 
125 
127  void clear();
128 
129 
131  std::size_t memory_size() const;
132 
134  const util::set<S>& set_hook_() const;
135 
136 
137  // Required by p_double-related classes.
138  const util::set<S>& set_1_() const;
139  template <typename I>
140  const S& set_2_(const I& it) const;
141 
142  protected:
143 
145  util::set<S> s_;
146  };
147 
148 
149 
150  template <typename S>
151  std::ostream& operator<<(std::ostream& ostr, const p_set_of<S>& s);
152 
153 
154 
155 
156 # ifndef MLN_INCLUDE_ONLY
157 
158  template <typename S>
159  inline
160  p_set_of<S>::p_set_of()
161  {
162  }
163 
164  template <typename S>
165  inline
166  bool
167  p_set_of<S>::has(const psite& p) const
168  {
169  return p.index() < s_.nelements() && s_[p.index()].has(p.p());
170  }
171 
172  template <typename S>
173  inline
174  bool
175  p_set_of<S>::is_valid() const
176  {
177  return true;
178  }
179 
180  template <typename S>
181  inline
182  void
183  p_set_of<S>::insert(const S& s)
184  {
185  mln_precondition(s.is_valid());
186  s_.insert(s);
187  this->update_nsites_(s);
188  this->update_bbox_(s);
189  }
190 
191  template <typename S>
192  inline
193  const S&
194  p_set_of<S>::operator[](unsigned i) const
195  {
196  mln_precondition(i < s_.nelements());
197  return s_[i];
198  }
199 
200  template <typename S>
201  inline
202  unsigned
203  p_set_of<S>::nelements() const
204  {
205  return s_.nelements();
206  }
207 
208  template <typename S>
209  inline
210  void
211  p_set_of<S>::clear()
212  {
213  s_.clear();
214  mln_postcondition(this->is_empty());
215  }
216 
217  template <typename S>
218  inline
219  std::size_t
220  p_set_of<S>::memory_size() const
221  {
222  return s_.memory_size();
223  }
224 
225  template <typename S>
226  inline
227  const util::set<S>&
228  p_set_of<S>::set_hook_() const
229  {
230  return s_;
231  }
232 
233  template <typename S>
234  inline
235  const util::set<S>&
236  p_set_of<S>::set_1_() const
237  {
238  return s_;
239  }
240 
241  template <typename S>
242  template <typename I>
243  inline
244  const S&
245  p_set_of<S>::set_2_(const I& it) const
246  {
247  return it.element();
248  }
249 
250 
251  template <typename S>
252  std::ostream& operator<<(std::ostream& ostr, const p_set_of<S>& s)
253  {
254  return ostr << s.set_hook_();
255  }
256 
257 # endif // ! MLN_INCLUDE_ONLY
258 
259 } // end of namespace mln
260 
261 
262 #endif // ! MLN_CORE_SITE_SET_P_SET_OF_HH