Changeset 2286


Ignore:
Timestamp:
Jun 27, 2010, 5:55:32 AM (11 years ago)
Author:
Peter
Message:

replace class PtrCompare? with two function make_ptr_compare that return compose_f_gx_hy functors. closes #632

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/utility_test.cc

    r2285 r2286  
    470470void test_ptr_compare(test::Suite& suite)
    471471{
    472   double* a = new double(0);
    473   double* b = new double(1);
    474 
    475   utility::PtrCompare<double> ptr_less;
    476   if (!ptr_less(a, b)) {
    477     suite.out() << "ptr_less(a, b) returned false\n";
    478     suite.add(false);
    479   }
    480   if (ptr_less(b, a)) {
    481     suite.out() << "ptr_less(b, a) returned true\n";
    482     suite.add(false);
    483   }
    484 
    485   utility::PtrCompare<double, std::greater<double> > ptr_greater;
    486   if (ptr_greater(a, b)) {
    487     suite.out() << "ptr_greater(a, b) returned true\n";
    488     suite.add(false);
    489   }
    490   if (!ptr_greater(b, a)) {
    491     suite.out() << "ptr_greater(b, a) returned false\n";
    492     suite.add(false);
    493   }
    494   delete a;
    495   delete b;
    496 
     472  using utility::make_ptr_compare;
    497473  std::vector<double*> vec(10);
    498474  std::greater<double> d_greater;
    499   utility::PtrCompare<double, std::greater<double> > ptr_greater2(d_greater);
    500475  for (size_t i=0; i<vec.size(); ++i)
    501476    vec[i] = new double(i);
    502   std::sort(vec.begin(), vec.end(), ptr_greater2);
     477  std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin(), d_greater));
    503478  for (size_t i=1; i<vec.size(); ++i) {
    504479    suite.add( *vec[i-1] > *vec[i]);
    505480  }
     481  std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin()));
     482  for (size_t i=1; i<vec.size(); ++i) {
     483    suite.add( *vec[i-1] < *vec[i]);
     484  }
    506485  for (size_t i=0; i<vec.size(); ++i)
    507486    delete vec[i];
  • trunk/yat/utility/stl_utility.h

    r2285 r2286  
    3535///
    3636
     37#include "concept_check.h"
    3738#include "DataWeight.h"
    3839#include "Exception.h"
     
    100101     by <a href=http://www.sgi.com/tech/stl/iterator_traits.html>
    101102     std::iterator_traits </a>.
     103
     104     \since New in yat 0.7
    102105   */
    103106  template<typename Pointer>
     
    106109                               typename std::iterator_traits<Pointer>::reference>
    107110  {
     111    /**
     112       \brief constructor
     113     */
     114    Dereferencer(void)
     115    { BOOST_CONCEPT_ASSERT((TrivialIterator<Pointer>)); }
     116
    108117    /**
    109118       \return * \a ti
     
    677686
    678687  /**
    679      Adaptor class that can be used to compare pointers, T*, when you
    680      want to compare them with respect to the objects they point to.
     688     Convenient function that creates a binary predicate that can be
     689     used to compare pointers when you want to compare them with
     690     respect to the objects they point to.
    681691
    682692     Example:
     
    684694     std::vector<MyClass*> vec(18);
    685695     ...
    686      PtrCompare<MyClass, std::greater<MyClass> > ptr_compare;
    687      std::sort(vec.begin(), vec.end(), ptr_compare);
     696     std::sort(vec.begin(), vec.end(),
     697               make_ptr_compare(vec[0], std::greater<MyClass>());
    688698     \endcode
    689699
    690      The class Compare must be a <a
    691      href="http://www.sgi.com/tech/stl/BinaryFunction.html">binary
    692      functor</a> that takes two \c const \c T& and returns \c bool.
     700
     701     Type Requirement:
     702     - \a compare must be a <a
     703     href="http://www.sgi.com/tech/stl/AdaptableBinaryPredicate.html">Adaptable
     704     Binary Predicate</a>.
     705     - value_type of Pointer must be convertible to argument_type of
     706       compare
     707
     708     \return a compose_f_gx_hy in which \c F is defined by \a compare
     709     and both \c G and \c H are \c Dereferencer functors.
     710
     711     \see compose_f_gx_hy
    693712
    694713     \since New in yat 0.7
    695714   */
    696   template<typename T, class Compare = std::less<T> >
    697   class PtrCompare : std::binary_function<T const* const, T const* const, bool>
    698   {
    699   public:
    700     /**
    701        \brief Constructor.
    702 
    703        Creates an instance of Compare using its default constructor.
    704      */
    705     PtrCompare(void)
    706     {
    707       BOOST_CONCEPT_ASSERT((boost::BinaryFunction<Compare, bool, const T&,
    708                             const T&>));
    709     }
    710 
    711     /**
    712        \brief Constructor.
    713 
    714        Creates a copy of \a c that will be used later.
    715      */
    716     PtrCompare(Compare c)
    717       : compare_(c)
    718     {
    719       BOOST_CONCEPT_ASSERT((boost::BinaryFunction<Compare, bool, const T&,
    720                             const T&>));
    721     }
    722 
    723     /**
    724        \return true iff Compare(* \a lhs, * \a rhs ) is true.
    725      */
    726     bool operator()(T const* const lhs, T const* const rhs) const
    727     {
    728       return compare_(*lhs, *rhs);
    729     }
    730   private:
    731     Compare compare_;
    732 
    733     // using compiler generated copy
    734     // PtrCompare(const PtrCompare&)
    735     // PtrCompare& operator=(const PtrCompare&)
    736   };
     715  template<typename Pointer, class Compare>
     716  compose_f_gx_hy<Compare, Dereferencer<Pointer>, Dereferencer<Pointer> >
     717  make_ptr_compare(Pointer p, Compare compare)
     718  {
     719    return make_compose_f_gx_hy(compare, Dereferencer<Pointer>(),
     720                                Dereferencer<Pointer>());
     721  }
     722
     723  /**
     724     Same as make_ptr_compare(2) except that std::less is used to
     725     compare pointers.
     726
     727     \since New in yat 0.7
     728   */
     729  template<typename Pointer>
     730  compose_f_gx_hy<std::less<typename std::iterator_traits<Pointer>::value_type>,
     731                  Dereferencer<Pointer>, Dereferencer<Pointer> >
     732  make_ptr_compare(Pointer p)
     733  {
     734    typedef typename std::iterator_traits<Pointer>::value_type value_type;
     735    BOOST_CONCEPT_ASSERT((boost::LessThanComparable<value_type>));
     736    std::less<value_type> compare;
     737    return make_ptr_compare(p, compare);
     738  }
     739
    737740
    738741  ///
Note: See TracChangeset for help on using the changeset viewer.