vtf-logo

OrderedPair.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00008 #if !defined(__ads_algorithm_OrderedPair_h__)
00009 #define __ads_algorithm_OrderedPair_h__
00010 
00011 #include "../defs.h"
00012 
00013 #include "../../third-party/loki/TypeTraits.h"
00014 
00015 #include <algorithm>
00016 #include <utility>
00017 
00018 BEGIN_NAMESPACE_ADS
00019 
00020 
00022 
00025 template<typename T>
00026 class OrderedPair :
00027   public std::pair<T,T> {
00028 
00029   //
00030   // Public types.
00031   //
00032 
00033 public:
00034 
00036   typedef T Value;
00038   typedef typename Loki::TypeTraits<Value>::ParameterType ParameterType;
00040   typedef std::pair<T,T> Base;
00041 
00042   //
00043   // Using
00044   //
00045 
00046 private:
00047 
00048   using Base::first;
00049   using Base::second;
00050 
00051 public:
00052 
00053   //--------------------------------------------------------------------------
00055 
00056 
00058 
00061   OrderedPair() :
00062     Base()
00063   {}
00064   
00066   OrderedPair(const OrderedPair& other) :
00067     Base(other)
00068   {}
00069   
00071   OrderedPair&
00072   operator=(const OrderedPair& other)
00073   {
00074     // Avoid assignment to self.
00075     if (this != &other) {
00076       Base::operator=(other);
00077     }
00078     // Return *this so assignments can chain.
00079     return *this;
00080   }
00081   
00083   ~OrderedPair()
00084   {}
00085   
00087   OrderedPair(const Value& a, const Value& b) : 
00088     Base(a, b) {
00089     order();
00090   }
00091 
00093   template<typename T1, typename T2>
00094   OrderedPair(const std::pair<T1,T2>& x) : 
00095     Base(x) {
00096     order();
00097   }
00098   
00100   //--------------------------------------------------------------------------
00102 
00103 
00105   ParameterType
00106   getFirst() const {
00107     return first;
00108   }
00109 
00111   ParameterType
00112   getSecond() const {
00113     return second;
00114   }
00115 
00117   //--------------------------------------------------------------------------
00119 
00120 
00122 
00125   void
00126   set(ParameterType x, ParameterType y) {
00127     first = x;
00128     second = y;
00129     order();
00130   }
00131 
00133 
00136   void
00137   setFirst(ParameterType x) {
00138     first = x;
00139     order();
00140   }
00141 
00143 
00146   void
00147   setSecond(ParameterType x) {
00148     second = x;
00149     order();
00150   }
00151 
00153   //--------------------------------------------------------------------------
00154   // Private member functions.
00155 
00157   void
00158   order() {
00159     if (second < first) {
00160       std::swap(first, second);
00161     }
00162   }
00163 
00164 };
00165 
00166 // Inherit equality and inequality from std::pair.
00167 
00169 
00174 template<typename T>
00175 inline 
00176 OrderedPair<T>
00177 makeOrderedPair(const T& x, const T& y) { 
00178   return OrderedPair<T>(x, y); 
00179 }
00180 
00181 END_NAMESPACE_ADS
00182 
00183 #endif

Generated on Fri Aug 24 12:55:27 2007 for Algorithms and Data Structures Package by  doxygen 1.4.7