Changeset 2239
- Timestamp:
- Apr 11, 2010, 1:05:12 AM (13 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/utility_test.cc
r2209 r2239 60 60 void test_sort_index(test::Suite& suite); 61 61 void test_less_nan(test::Suite& suite); 62 void test_ptr_compare(test::Suite& suite); 62 63 63 64 int main(int argc, char* argv[]) … … 168 169 test_less_nan(suite); 169 170 test_errno_error(suite); 171 test_ptr_compare(suite); 170 172 171 173 return suite.return_value(); … … 370 372 } 371 373 } 374 375 void test_ptr_compare(test::Suite& suite) 376 { 377 double* a = new double(0); 378 double* b = new double(1); 379 380 utility::PtrCompare<double> ptr_less; 381 if (!ptr_less(a, b)) { 382 suite.out() << "ptr_less(a, b) returned false\n"; 383 suite.add(false); 384 } 385 if (ptr_less(b, a)) { 386 suite.out() << "ptr_less(b, a) returned true\n"; 387 suite.add(false); 388 } 389 390 utility::PtrCompare<double, std::greater<double> > ptr_greater; 391 if (ptr_greater(a, b)) { 392 suite.out() << "ptr_greater(a, b) returned true\n"; 393 suite.add(false); 394 } 395 if (!ptr_greater(b, a)) { 396 suite.out() << "ptr_greater(b, a) returned false\n"; 397 suite.add(false); 398 } 399 delete a; 400 delete b; 401 402 std::vector<double*> vec(10); 403 std::greater<double> d_greater; 404 utility::PtrCompare<double, std::greater<double> > ptr_greater2(d_greater); 405 for (size_t i=0; i<vec.size(); ++i) 406 vec[i] = new double(i); 407 std::sort(vec.begin(), vec.end(), ptr_greater2); 408 for (size_t i=1; i<vec.size(); ++i) { 409 suite.add( *vec[i-1] > *vec[i]); 410 } 411 for (size_t i=0; i<vec.size(); ++i) 412 delete vec[i]; 413 414 } -
trunk/yat/utility/stl_utility.h
r2220 r2239 499 499 500 500 501 501 /** 502 Adaptor class that can be used to compare pointers, T*, when you 503 want to compare them with respect to the objects they point to. 504 505 Example: 506 \code 507 std::vector<MyClass*> vec(18); 508 ... 509 PtrCompare<MyClass, std::greater<MyClass> > ptr_compare; 510 std::sort(vec.begin(), vec.end(), ptr_compare); 511 \endcode 512 513 The class Compare must be a <a 514 href="http://www.sgi.com/tech/stl/BinaryFunction.html">binary 515 functor</a> that takes two \c const \c T& and returns \c bool. 516 517 \since New in yat 0.7 518 */ 519 template<typename T, class Compare = std::less<T> > 520 class PtrCompare : std::binary_function<T const* const, T const* const, bool> 521 { 522 public: 523 /** 524 \brief Constructor. 525 526 Creates an instance of Compare using its default constructor. 527 */ 528 PtrCompare(void) 529 { 530 BOOST_CONCEPT_ASSERT((boost::BinaryFunction<Compare, bool, const T&, 531 const T&>)); 532 } 533 534 /** 535 \brief Constructor. 536 537 Creates a copy of \a c that will be used later. 538 */ 539 PtrCompare(Compare c) 540 : compare_(c) 541 { 542 BOOST_CONCEPT_ASSERT((boost::BinaryFunction<Compare, bool, const T&, 543 const T&>)); 544 } 545 546 /** 547 \return true iff Compare(* \a lhs, * \a rhs ) is true. 548 */ 549 bool operator()(T const* const lhs, T const* const rhs) const 550 { 551 return compare_(*lhs, *rhs); 552 } 553 private: 554 Compare compare_; 555 556 // using compiler generated copy 557 // PtrCompare(const PtrCompare&) 558 // PtrCompare& operator=(const PtrCompare&) 559 }; 502 560 503 561 ///
Note: See TracChangeset
for help on using the changeset viewer.