Changeset 2286
- Timestamp:
- Jun 27, 2010, 5:55:32 AM (13 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/utility_test.cc
r2285 r2286 470 470 void test_ptr_compare(test::Suite& suite) 471 471 { 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; 497 473 std::vector<double*> vec(10); 498 474 std::greater<double> d_greater; 499 utility::PtrCompare<double, std::greater<double> > ptr_greater2(d_greater);500 475 for (size_t i=0; i<vec.size(); ++i) 501 476 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)); 503 478 for (size_t i=1; i<vec.size(); ++i) { 504 479 suite.add( *vec[i-1] > *vec[i]); 505 480 } 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 } 506 485 for (size_t i=0; i<vec.size(); ++i) 507 486 delete vec[i]; -
trunk/yat/utility/stl_utility.h
r2285 r2286 35 35 /// 36 36 37 #include "concept_check.h" 37 38 #include "DataWeight.h" 38 39 #include "Exception.h" … … 100 101 by <a href=http://www.sgi.com/tech/stl/iterator_traits.html> 101 102 std::iterator_traits </a>. 103 104 \since New in yat 0.7 102 105 */ 103 106 template<typename Pointer> … … 106 109 typename std::iterator_traits<Pointer>::reference> 107 110 { 111 /** 112 \brief constructor 113 */ 114 Dereferencer(void) 115 { BOOST_CONCEPT_ASSERT((TrivialIterator<Pointer>)); } 116 108 117 /** 109 118 \return * \a ti … … 677 686 678 687 /** 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. 681 691 682 692 Example: … … 684 694 std::vector<MyClass*> vec(18); 685 695 ... 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>()); 688 698 \endcode 689 699 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 693 712 694 713 \since New in yat 0.7 695 714 */ 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 737 740 738 741 ///
Note: See TracChangeset
for help on using the changeset viewer.