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

ord_pair.hh

00001 // Copyright (C) 2007, 2008, 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_ORD_PAIR_HH
00027 # define MLN_UTIL_ORD_PAIR_HH
00028 
00032 
00033 # include <mln/core/concept/object.hh>
00034 # include <mln/util/ord.hh>
00035 
00036 
00037 namespace mln
00038 {
00039 
00040   namespace util
00041   {
00042 
00048     //
00049     template <typename T>
00050     struct ord_pair : public mln::Object< ord_pair<T> >
00051     {
00052     public:
00053       ord_pair();
00054       ord_pair(const T& val1, const T& val2);
00055 
00056     public:
00059       const T& first() const;
00060       T& first();
00062 
00065       const T& second() const;
00066       T& second();
00068 
00074       void change_first(const T& val);
00075 
00081       void change_second(const T& val);
00082 
00088       void change_both(const T& first, const T& second);
00089 
00090     private:
00091       T first_;
00092       T second_;
00093     };
00094 
00095 
00096     template <typename T>
00097     bool operator==(const ord_pair<T>& lhs, const ord_pair<T>& rhs);
00098 
00099     template <typename T>
00100     bool operator< (const ord_pair<T>& lhs, const ord_pair<T>& rhs);
00101 
00102     template <typename T>
00103     bool operator<=(const ord_pair<T>& lhs, const ord_pair<T>& rhs);
00104 
00105 
00106     template <typename T>
00107     std::ostream& operator<<(std::ostream& ostr, const ord_pair<T>& op);
00108 
00109   } // end of namespace mln::util
00110 
00111 
00112   namespace make
00113   {
00115     template <typename T>
00116     util::ord_pair<T> ord_pair(const T& val1, const T& val2);
00117   }
00118 
00119 
00120 # ifndef MLN_INCLUDE_ONLY
00121 
00122   namespace util
00123   {
00124 
00125     /*---------------.
00126     | Construction.  |
00127     `---------------*/
00128 
00129     template <typename T>
00130     inline
00131     ord_pair<T>::ord_pair()
00132     {
00133     }
00134 
00135     template <typename T>
00136     inline
00137     ord_pair<T>::ord_pair(const T& val1, const T& val2)
00138     {
00139       change_both(val1, val2);
00140     }
00141 
00142     /*---------.
00143     | Access.  |
00144     `---------*/
00145 
00146     template <typename T>
00147     inline
00148     const T&
00149     ord_pair<T>::first() const
00150     {
00151       return first_;
00152     }
00153 
00154     template <typename T>
00155     inline
00156     T&
00157     ord_pair<T>::first()
00158     {
00159       return first_;
00160     }
00161 
00162     template <typename T>
00163     inline
00164     const T&
00165     ord_pair<T>::second() const
00166     {
00167       return second_;
00168     }
00169 
00170     template <typename T>
00171     inline
00172     T&
00173     ord_pair<T>::second()
00174     {
00175       return second_;
00176     }
00177 
00178     template <typename T>
00179     inline
00180     void
00181     ord_pair<T>::change_first(const T& val)
00182     {
00183       mln_precondition(util::ord_weak(first_, second_));
00184 
00185       if (util::ord_strict(val, second_))
00186         first_ = val;
00187       else
00188         second_ = val;
00189 
00190       mln_postcondition(util::ord_weak(first_, second_));
00191     }
00192 
00193     template <typename T>
00194     inline
00195     void
00196     ord_pair<T>::change_second(const T& val)
00197     {
00198       mln_precondition(util::ord_weak(first_, second_));
00199 
00200       if (util::ord_strict(first_, val))
00201         second_ = val;
00202       else
00203         first_ = val;
00204 
00205       mln_postcondition(util::ord_weak(first_, second_));
00206     }
00207 
00208     template <typename T>
00209     inline
00210     void
00211     ord_pair<T>::change_both(const T& val1, const T& val2)
00212     {
00213       if (util::ord_strict(val1, val2))
00214         {
00215           first_  = val1;
00216           second_ = val2;
00217         }
00218       else
00219         {
00220           first_  = val2;
00221           second_ = val1;
00222         }
00223       mln_postcondition(util::ord_weak(first_, second_));
00224     }
00225 
00226     /*-------------.
00227     | Comparison.  |
00228     `-------------*/
00229 
00230     template <typename T>
00231     inline
00232     bool operator==(const ord_pair<T>& lhs, const ord_pair<T>& rhs)
00233     {
00234       return lhs.first() == rhs.first() && lhs.second() == rhs.second();
00235     }
00236 
00237     template <typename T>
00238     inline
00239     bool operator< (const ord_pair<T>& lhs, const ord_pair<T>& rhs)
00240     {
00241       return
00242         util::ord_strict(lhs.first(), rhs.first()) ||
00243         (lhs.first() == rhs.first() &&
00244          util::ord_strict(lhs.second(), rhs.second()));
00245     }
00246 
00247     template <typename T>
00248     inline
00249     bool operator<=(const ord_pair<T>& lhs, const ord_pair<T>& rhs)
00250     {
00251       return
00252         util::ord_strict(lhs.first(), rhs.first()) ||
00253         (lhs.first() == rhs.first() &&
00254          util::ord_weak(lhs.second(), rhs.second()));
00255     }
00256 
00257     /*------------------.
00258     | Pretty-printing.  |
00259     `------------------*/
00260 
00261     template <typename T>
00262     inline
00263     std::ostream& operator<<(std::ostream& ostr, const ord_pair<T>& op)
00264     {
00265       return ostr << '(' << op.first() << ',' << op.second() << ')';
00266     }
00267 
00268   } // end of namespace mln::util
00269 
00270 
00271   namespace make
00272   {
00273 
00274     template <typename T>
00275     inline
00276     util::ord_pair<T>
00277     ord_pair(const T& val1, const T& val2)
00278     {
00279       util::ord_pair<T> tmp(val1, val2);
00280       return tmp;
00281     }
00282 
00283   } // end of namespace mln::make
00284 
00285 # endif // ! MLN_INCLUDE_ONLY
00286 
00287 } // end of namespace mln
00288 
00289 
00290 #endif // ! MLN_UTIL_ORD_PAIR_HH

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