$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
util/couple.hh
1 // Copyright (C) 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_COUPLE_HH
27 # define MLN_UTIL_COUPLE_HH
28 
34 
35 # include <mln/core/concept/object.hh>
36 # include <mln/util/ord.hh>
37 
38 namespace mln
39 {
40 
41  namespace util
42  {
43 
47  template <typename T, typename U>
48  class couple : public mln::Object< couple<T,U> >
49  {
50  public:
51  couple();
52  couple(const T& val1, const U& val2);
53 
56  const T& first() const;
57  T& first();
59 
62  const U& second() const;
63  U& second();
65 
67  void change_first(const T& val);
68 
70  void change_second(const U& val);
71 
73  void change_both(const T& first, const U& second);
74 
75  private:
76  T first_;
77  U second_;
78  };
79 
80 
81  template <typename T, typename U>
82  bool operator==(const couple<T,U>& lhs, const couple<T,U>& rhs);
83 
84  template <typename T, typename U>
85  bool operator< (const couple<T,U>& lhs, const couple<T,U>& rhs);
86 
87  template <typename T, typename U>
88  bool operator<=(const couple<T,U>& lhs, const couple<T,U>& rhs);
89 
90 
91  template <typename T, typename U>
92  std::ostream& operator<<(std::ostream& ostr, const couple<T,U>& op);
93 
94  } // end of namespace mln::util
95 
96 
97  namespace make
98  {
100  template <typename T, typename U>
101  util::couple<T,U> couple(const T& val1, const T& val2);
102  }
103 
104 
105 # ifndef MLN_INCLUDE_ONLY
106 
107  namespace util
108  {
109 
110  /*---------------.
111  | Construction. |
112  `---------------*/
113 
114  template <typename T, typename U>
115  inline
117  {
118  }
119 
120  template <typename T, typename U>
121  inline
122  couple<T,U>::couple(const T& val1, const U& val2)
123  {
124  change_both(val1, val2);
125  }
126 
127  /*---------.
128  | Access. |
129  `---------*/
130 
131  template <typename T, typename U>
132  inline
133  const T&
134  couple<T,U>::first() const
135  {
136  return first_;
137  }
138 
139  template <typename T, typename U>
140  inline
141  T&
143  {
144  return first_;
145  }
146 
147  template <typename T, typename U>
148  inline
149  const U&
150  couple<T,U>::second() const
151  {
152  return second_;
153  }
154 
155  template <typename T, typename U>
156  inline
157  U&
159  {
160  return second_;
161  }
162 
163  template <typename T, typename U>
164  inline
165  void
166  couple<T,U>::change_first(const T& val)
167  {
168  first_ = val;
169  }
170 
171  template <typename T, typename U>
172  inline
173  void
174  couple<T,U>::change_second(const U& val)
175  {
176  second_ = val;
177  }
178 
179  template <typename T, typename U>
180  inline
181  void
182  couple<T,U>::change_both(const T& val1, const U& val2)
183  {
184  first_ = val1;
185  second_ = val2;
186  }
187 
188  /*-------------.
189  | Comparison. |
190  `-------------*/
191 
192  template <typename T, typename U>
193  inline
194  bool operator==(const couple<T,U>& lhs, const couple<T,U>& rhs)
195  {
196  return lhs.first() == rhs.first() && lhs.second() == rhs.second();
197  }
198 
199  template <typename T, typename U>
200  inline
201  bool operator< (const couple<T,U>& lhs, const couple<T,U>& rhs)
202  {
203  return
204  util::ord_strict(lhs.first(), rhs.first()) ||
205  (lhs.first() == rhs.first() &&
206  util::ord_strict(lhs.second(), rhs.second()));
207  }
208 
209  template <typename T, typename U>
210  inline
211  bool operator<=(const couple<T,U>& lhs, const couple<T,U>& rhs)
212  {
213  return
214  util::ord_strict(lhs.first(), rhs.first()) ||
215  (lhs.first() == rhs.first() &&
216  util::ord_weak(lhs.second(), rhs.second()));
217  }
218 
219  /*------------------.
220  | Pretty-printing. |
221  `------------------*/
222 
223  template <typename T, typename U>
224  inline
225  std::ostream& operator<<(std::ostream& ostr, const couple<T,U>& op)
226  {
227  return ostr << '(' << op.first() << ',' << op.second() << ')';
228  }
229 
230  } // end of namespace mln::util
231 
232 
233  namespace make
234  {
235 
236  template <typename T, typename U>
237  inline
238  util::couple<T,U>
239  couple(const T& val1, const U& val2)
240  {
241  util::couple<T,U> tmp(val1, val2);
242  return tmp;
243  }
244 
245  } // end of namespace mln::make
246 
247 # endif // ! MLN_INCLUDE_ONLY
248 
249 } // end of namespace mln
250 
251 
252 #endif // ! MLN_UTIL_COUPLE_HH