$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ord_pair.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_UTIL_ORD_PAIR_HH
27 # define MLN_UTIL_ORD_PAIR_HH
28 
32 
33 # include <mln/core/concept/object.hh>
34 # include <mln/util/ord.hh>
35 
36 
37 namespace mln
38 {
39 
40  namespace util
41  {
42 
48  //
49  template <typename T>
50  struct ord_pair : public mln::Object< ord_pair<T> >
51  {
52  public:
53  ord_pair();
54  ord_pair(const T& val1, const T& val2);
55 
56  public:
59  const T& first() const;
60  T& first();
62 
65  const T& second() const;
66  T& second();
68 
74  void change_first(const T& val);
75 
81  void change_second(const T& val);
82 
88  void change_both(const T& first, const T& second);
89 
90  private:
91  T first_;
92  T second_;
93  };
94 
95 
96  template <typename T>
97  bool operator==(const ord_pair<T>& lhs, const ord_pair<T>& rhs);
98 
99  template <typename T>
100  bool operator< (const ord_pair<T>& lhs, const ord_pair<T>& rhs);
101 
102  template <typename T>
103  bool operator<=(const ord_pair<T>& lhs, const ord_pair<T>& rhs);
104 
105 
106  template <typename T>
107  std::ostream& operator<<(std::ostream& ostr, const ord_pair<T>& op);
108 
109  } // end of namespace mln::util
110 
111 
112  namespace make
113  {
115  template <typename T>
116  util::ord_pair<T> ord_pair(const T& val1, const T& val2);
117  }
118 
119 
120 # ifndef MLN_INCLUDE_ONLY
121 
122  namespace util
123  {
124 
125  /*---------------.
126  | Construction. |
127  `---------------*/
128 
129  template <typename T>
130  inline
132  {
133  }
134 
135  template <typename T>
136  inline
137  ord_pair<T>::ord_pair(const T& val1, const T& val2)
138  {
139  change_both(val1, val2);
140  }
141 
142  /*---------.
143  | Access. |
144  `---------*/
145 
146  template <typename T>
147  inline
148  const T&
149  ord_pair<T>::first() const
150  {
151  return first_;
152  }
153 
154  template <typename T>
155  inline
156  T&
158  {
159  return first_;
160  }
161 
162  template <typename T>
163  inline
164  const T&
165  ord_pair<T>::second() const
166  {
167  return second_;
168  }
169 
170  template <typename T>
171  inline
172  T&
174  {
175  return second_;
176  }
177 
178  template <typename T>
179  inline
180  void
181  ord_pair<T>::change_first(const T& val)
182  {
183  mln_precondition(util::ord_weak(first_, second_));
184 
185  if (util::ord_strict(val, second_))
186  first_ = val;
187  else
188  second_ = val;
189 
190  mln_postcondition(util::ord_weak(first_, second_));
191  }
192 
193  template <typename T>
194  inline
195  void
196  ord_pair<T>::change_second(const T& val)
197  {
198  mln_precondition(util::ord_weak(first_, second_));
199 
200  if (util::ord_strict(first_, val))
201  second_ = val;
202  else
203  first_ = val;
204 
205  mln_postcondition(util::ord_weak(first_, second_));
206  }
207 
208  template <typename T>
209  inline
210  void
211  ord_pair<T>::change_both(const T& val1, const T& val2)
212  {
213  if (util::ord_strict(val1, val2))
214  {
215  first_ = val1;
216  second_ = val2;
217  }
218  else
219  {
220  first_ = val2;
221  second_ = val1;
222  }
223  mln_postcondition(util::ord_weak(first_, second_));
224  }
225 
226  /*-------------.
227  | Comparison. |
228  `-------------*/
229 
230  template <typename T>
231  inline
232  bool operator==(const ord_pair<T>& lhs, const ord_pair<T>& rhs)
233  {
234  return lhs.first() == rhs.first() && lhs.second() == rhs.second();
235  }
236 
237  template <typename T>
238  inline
239  bool operator< (const ord_pair<T>& lhs, const ord_pair<T>& rhs)
240  {
241  return
242  util::ord_strict(lhs.first(), rhs.first()) ||
243  (lhs.first() == rhs.first() &&
244  util::ord_strict(lhs.second(), rhs.second()));
245  }
246 
247  template <typename T>
248  inline
249  bool operator<=(const ord_pair<T>& lhs, const ord_pair<T>& rhs)
250  {
251  return
252  util::ord_strict(lhs.first(), rhs.first()) ||
253  (lhs.first() == rhs.first() &&
254  util::ord_weak(lhs.second(), rhs.second()));
255  }
256 
257  /*------------------.
258  | Pretty-printing. |
259  `------------------*/
260 
261  template <typename T>
262  inline
263  std::ostream& operator<<(std::ostream& ostr, const ord_pair<T>& op)
264  {
265  return ostr << '(' << op.first() << ',' << op.second() << ')';
266  }
267 
268  } // end of namespace mln::util
269 
270 
271  namespace make
272  {
273 
274  template <typename T>
275  inline
276  util::ord_pair<T>
277  ord_pair(const T& val1, const T& val2)
278  {
279  util::ord_pair<T> tmp(val1, val2);
280  return tmp;
281  }
282 
283  } // end of namespace mln::make
284 
285 # endif // ! MLN_INCLUDE_ONLY
286 
287 } // end of namespace mln
288 
289 
290 #endif // ! MLN_UTIL_ORD_PAIR_HH