Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
p_set.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_SET_HH
27 # define MLN_CORE_SITE_SET_P_SET_HH
28 
33 
34 # include <mln/core/site_set/p_array.hh>
35 # include <mln/util/set.hh>
36 
37 
38 namespace mln
39 {
40 
41  // Forward declaration.
42  template <typename P> class p_set;
43 
44 
45  namespace trait
46  {
47 
48  template <typename P>
49  struct site_set_< p_set<P> >
50  {
51  typedef trait::site_set::nsites::known nsites;
52  typedef trait::site_set::bbox::unknown bbox;
53  typedef trait::site_set::contents::free contents;
54  typedef trait::site_set::arity::unique arity;
55  };
56 
57  } // end of namespace trait
58 
59 
60 
64 
69  template <typename P>
70  class p_set : public internal::site_set_base_< P, p_set<P> >
71  {
72  typedef p_set<P> self_;
73  public:
74 
76  typedef P element;
77 
80 
83 
86 
88  typedef fwd_piter piter;
89 
90 
92  p_set();
93 
94 
96  bool has(const psite& p) const;
97 
99  bool has(const P& p) const;
100 
102  bool has(const util::index& i) const;
103 
105  bool is_valid() const;
106 
107 
109  unsigned nsites() const;
110 
111 
113  typedef P i_element;
114 
116  void insert(const P& p);
117 
119  typedef P r_element;
120 
122  void remove(const P& p);
123 
125  void clear();
126 
127 
129  const P& operator[](unsigned i) const;
130 
131 
133  std::size_t memory_size() const;
134 
136  const std::vector<P>& std_vector() const;
137 
139  const util::set<P>& util_set() const;
140 
141  protected:
142 
143  util::set<P> s_;
144  };
145 
146 
147 
148 # ifndef MLN_INCLUDE_ONLY
149 
150  template <typename P>
151  inline
153  {
154  }
155 
156  template <typename P>
157  inline
158  bool
159  p_set<P>::has(const P& p) const
160  {
161  return s_.has(p);
162  }
163 
164  template <typename P>
165  inline
166  bool
167  p_set<P>::has(const psite& p) const
168  {
169  mln_precondition(p.target_() == this); // FIXME: Refine.
170  if (! has(p.index()))
171  return false;
172  mln_invariant(p.to_site() == (*this)[p.index()]);
173  return true;
174  }
175 
176  template <typename P>
177  inline
178  bool
179  p_set<P>::has(const util::index& i) const
180  {
181  return i >= 0 && unsigned(i) < nsites();
182  }
183 
184  template <typename P>
185  inline
186  bool
188  {
189  return true;
190  }
191 
192  template <typename P>
193  inline
194  unsigned
196  {
197  return s_.nelements();
198  }
199 
200  template <typename P>
201  inline
202  void
203  p_set<P>::insert(const P& p)
204  {
205  s_.insert(p);
206  }
207 
208  template <typename P>
209  inline
210  void
211  p_set<P>::remove(const P& p)
212  {
213  s_.remove(p);
214  }
215 
216  template <typename P>
217  inline
218  void
220  {
221  s_.clear();
222  }
223 
224  template <typename P>
225  inline
226  const P&
227  p_set<P>::operator[](unsigned i) const
228  {
229  mln_precondition(i < nsites());
230  return s_[i];
231  }
232 
233  template <typename P>
234  inline
235  std::size_t
237  {
238  return s_.memory_size();
239  }
240 
241  template <typename P>
242  inline
243  const std::vector<P>&
245  {
246  return s_.std_vector();
247  }
248 
249  template <typename P>
250  inline
251  const util::set<P>&
253  {
254  return s_;
255  }
256 
257 # endif // ! MLN_INCLUDE_ONLY
258 
259 } // end of namespace mln
260 
261 
262 #endif // ! MLN_CORE_SITE_SET_P_SET_HH