• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

couple.hh

00001 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_UTIL_COUPLE_HH
00027 # define MLN_UTIL_COUPLE_HH
00028 
00034 
00035 # include <mln/core/concept/object.hh>
00036 # include <mln/util/ord.hh>
00037 
00038 namespace mln
00039 {
00040 
00041   namespace util
00042   {
00043 
00047     template <typename T, typename U>
00048     class couple : public mln::Object< couple<T,U> >
00049     {
00050     public:
00051       couple();
00052       couple(const T& val1, const U& val2);
00053 
00056       const T& first() const;
00057       T& first();
00059 
00062       const U& second() const;
00063       U& second();
00065 
00067       void change_first(const T& val);
00068 
00070       void change_second(const U& val);
00071 
00073       void change_both(const T& first, const U& second);
00074 
00075     private:
00076       T first_;
00077       U second_;
00078     };
00079 
00080 
00081     template <typename T, typename U>
00082     bool operator==(const couple<T,U>& lhs, const couple<T,U>& rhs);
00083 
00084     template <typename T, typename U>
00085     bool operator< (const couple<T,U>& lhs, const couple<T,U>& rhs);
00086 
00087     template <typename T, typename U>
00088     bool operator<=(const couple<T,U>& lhs, const couple<T,U>& rhs);
00089 
00090 
00091     template <typename T, typename U>
00092     std::ostream& operator<<(std::ostream& ostr, const couple<T,U>& op);
00093 
00094   } // end of namespace mln::util
00095 
00096 
00097   namespace make
00098   {
00100     template <typename T, typename U>
00101     util::couple<T,U> couple(const T& val1, const T& val2);
00102   }
00103 
00104 
00105 # ifndef MLN_INCLUDE_ONLY
00106 
00107   namespace util
00108   {
00109 
00110     /*---------------.
00111     | Construction.  |
00112     `---------------*/
00113 
00114     template <typename T, typename U>
00115     inline
00116     couple<T,U>::couple()
00117     {
00118     }
00119 
00120     template <typename T, typename U>
00121     inline
00122     couple<T,U>::couple(const T& val1, const U& val2)
00123     {
00124       change_both(val1, val2);
00125     }
00126 
00127     /*---------.
00128     | Access.  |
00129     `---------*/
00130 
00131     template <typename T, typename U>
00132     inline
00133     const T&
00134     couple<T,U>::first() const
00135     {
00136       return first_;
00137     }
00138 
00139     template <typename T, typename U>
00140     inline
00141     T&
00142     couple<T,U>::first()
00143     {
00144       return first_;
00145     }
00146 
00147     template <typename T, typename U>
00148     inline
00149     const U&
00150     couple<T,U>::second() const
00151     {
00152       return second_;
00153     }
00154 
00155     template <typename T, typename U>
00156     inline
00157     U&
00158     couple<T,U>::second()
00159     {
00160       return second_;
00161     }
00162 
00163     template <typename T, typename U>
00164     inline
00165     void
00166     couple<T,U>::change_first(const T& val)
00167     {
00168       first_ = val;
00169     }
00170 
00171     template <typename T, typename U>
00172     inline
00173     void
00174     couple<T,U>::change_second(const U& val)
00175     {
00176       second_ = val;
00177     }
00178 
00179     template <typename T, typename U>
00180     inline
00181     void
00182     couple<T,U>::change_both(const T& val1, const U& val2)
00183     {
00184       first_  = val1;
00185       second_ = val2;
00186     }
00187 
00188     /*-------------.
00189     | Comparison.  |
00190     `-------------*/
00191 
00192     template <typename T, typename U>
00193     inline
00194     bool operator==(const couple<T,U>& lhs, const couple<T,U>& rhs)
00195     {
00196       return lhs.first() == rhs.first() && lhs.second() == rhs.second();
00197     }
00198 
00199     template <typename T, typename U>
00200     inline
00201     bool operator< (const couple<T,U>& lhs, const couple<T,U>& rhs)
00202     {
00203       return
00204         util::ord_strict(lhs.first(), rhs.first()) ||
00205         (lhs.first() == rhs.first() &&
00206          util::ord_strict(lhs.second(), rhs.second()));
00207     }
00208 
00209     template <typename T, typename U>
00210     inline
00211     bool operator<=(const couple<T,U>& lhs, const couple<T,U>& rhs)
00212     {
00213       return
00214         util::ord_strict(lhs.first(), rhs.first()) ||
00215         (lhs.first() == rhs.first() &&
00216          util::ord_weak(lhs.second(), rhs.second()));
00217     }
00218 
00219     /*------------------.
00220     | Pretty-printing.  |
00221     `------------------*/
00222 
00223     template <typename T, typename U>
00224     inline
00225     std::ostream& operator<<(std::ostream& ostr, const couple<T,U>& op)
00226     {
00227       return ostr << '(' << op.first() << ',' << op.second() << ')';
00228     }
00229 
00230   } // end of namespace mln::util
00231 
00232 
00233   namespace make
00234   {
00235 
00236     template <typename T, typename U>
00237     inline
00238     util::couple<T,U>
00239     couple(const T& val1, const U& val2)
00240     {
00241       util::couple<T,U> tmp(val1, val2);
00242       return tmp;
00243     }
00244 
00245   } // end of namespace mln::make
00246 
00247 # endif // ! MLN_INCLUDE_ONLY
00248 
00249 } // end of namespace mln
00250 
00251 
00252 #endif // ! MLN_UTIL_COUPLE_HH

Generated on Tue Oct 4 2011 15:23:40 for Milena (Olena) by  doxygen 1.7.1