Changeset 2730
 Timestamp:
 Apr 24, 2012, 3:07:47 AM (10 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/yat/utility/stl_utility.h
r2530 r2730 1 1 #ifndef _theplu_yat_utility_stl_utility_ 2 #define _theplu_yat_utility_stl_utility_ 2 #define _theplu_yat_utility_stl_utility_ 3 3 4 4 // $Id$ … … 9 9 Copyright (C) 2006 Jari Häkkinen 10 10 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson 11 Copyright (C) 2009, 2010, 2011 Peter Johansson11 Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson 12 12 13 13 This file is part of the yat library, http://dev.thep.lu.se/yat … … 65 65 66 66 /// 67 /// Print out a pair 67 /// Print out a pair 68 68 /// 69 69 // This is in namespace std because we have not figured out how to have 70 70 // pair and its operator<< in different namespaces 71 template <class T1, class T2> 72 std::ostream& operator<<(std::ostream& out, const std::pair<T1,T2>& p) 71 template <class T1, class T2> 72 std::ostream& operator<<(std::ostream& out, const std::pair<T1,T2>& p) 73 73 { out << p.first << "\t" << p.second; return out; } 74 74 … … 97 97 \brief Adaptor between pointer and pointee interface 98 98 99 Pointer must have an \c operator*, i.e., \c Pointer can be a 100 traditional pointer or an \input_iterator. Return type is decided 101 by <a href=http://www.sgi.com/tech/stl/iterator_traits.html> 102 std::iterator_traits </a>. 99 Functor takes a pointer and returns a reference to the instance 100 pointer is pointing to. Return type is decided by <a 101 href=http://www.sgi.com/tech/stl/iterator_traits.html> 102 std::iterator_traits<Pointer>::reference </a>. Pointer must have 103 an \c operator*, i.e., \c Pointer can be a traditional pointer or 104 an \input_iterator. 105 106 The class is designed to be used with boost::transform_iterator 107 108 \code 109 std::vector<MyClass*> vec; 110 ... 111 Dereferencer<MyClass*> dereferencer; 112 std::sort(boost::make_transform_iterator(vec.begin(), dereferencer), 113 boost::make_transform_iterator(vec.end(), dereferencer)) 114 \endcode 115 116 where elements in vec is sorted using MyClass::operator< 103 117 104 118 \since New in yat 0.7 105 119 */ 106 120 template<typename Pointer> 107 struct Dereferencer : 108 public std::unary_function<Pointer, 121 struct Dereferencer : 122 public std::unary_function<Pointer, 109 123 typename std::iterator_traits<Pointer>::reference> 110 124 { … … 112 126 \brief constructor 113 127 */ 114 Dereferencer(void) 128 Dereferencer(void) 115 129 { BOOST_CONCEPT_ASSERT((TrivialIterator<Pointer>)); } 116 130 … … 137 151  G must be an <a 138 152 href="http://www.sgi.com/tech/stl/AdaptableUnaryFunction.html"> 139 AdaptableUnaryFunction</a> 153 AdaptableUnaryFunction</a> 140 154  H must be an <a 141 155 href="http://www.sgi.com/tech/stl/AdaptableUnaryFunction.html"> 142 AdaptableUnaryFunction</a> 156 AdaptableUnaryFunction</a> 143 157  \c G::result_type is convertible to \c F::first_argument_type 144 158  \c H::result_type is convertible to \c F::second_argument_type … … 147 161 */ 148 162 template<class F, class G, class H> 149 class compose_f_gx_hy : 163 class compose_f_gx_hy : 150 164 public std::binary_function<typename G::argument_type, 151 165 typename H::argument_type, … … 157 171 */ 158 172 compose_f_gx_hy(F f, G g, H h) 159 : f_(f), g_(g), h_(h) 173 : f_(f), g_(g), h_(h) 160 174 { 161 175 BOOST_CONCEPT_ASSERT((boost::Convertible<typename G::result_type … … 170 184 */ 171 185 typename F::result_type 172 operator()(typename G::argument_type x, 186 operator()(typename G::argument_type x, 173 187 typename H::argument_type y) const 174 188 { … … 193 207 { 194 208 return compose_f_gx_hy<F,G,H>(f,g,h); 195 } 209 } 196 210 197 211 … … 210 224  G must be an <a 211 225 href="http://www.sgi.com/tech/stl/AdaptableBinaryFunction.html"> 212 AdaptableBinaryFunction</a> 226 AdaptableBinaryFunction</a> 213 227  \c G::result_type is convertible to \c F::argument_type 214 228 … … 218 232 */ 219 233 template<class F, class G> 220 class compose_f_gxy : 234 class compose_f_gxy : 221 235 public std::binary_function<typename G::first_argument_type, 222 236 typename G::second_argument_type, … … 228 242 */ 229 243 compose_f_gxy(F f, G g) 230 : f_(f), g_(g) 244 : f_(f), g_(g) 231 245 { 232 246 BOOST_CONCEPT_ASSERT((boost::Convertible<typename G::result_type … … 238 252 */ 239 253 typename F::result_type 240 operator()(typename G::first_argument_type x, 254 operator()(typename G::first_argument_type x, 241 255 typename G::second_argument_type y) const 242 256 { … … 262 276 { 263 277 return compose_f_gxy<F,G>(f,g); 264 } 278 } 265 279 266 280 … … 278 292  G must be an <a 279 293 href="http://www.sgi.com/tech/stl/AdaptableUnaryFunction.html"> 280 AdaptableUnaryFunction</a> 294 AdaptableUnaryFunction</a> 281 295  \c G::result_type is convertible to \c F::argument_type 282 296 … … 294 308 */ 295 309 compose_f_gx(F f, G g) 296 : f_(f), g_(g) 310 : f_(f), g_(g) 297 311 { 298 312 BOOST_CONCEPT_ASSERT((boost::Convertible<typename G::result_type … … 327 341 { 328 342 return compose_f_gx<F,G>(f,g); 329 } 343 } 330 344 331 345 … … 405 419 */ 406 420 template<typename Key, typename InputIterator, typename Comp> 407 void inverse(InputIterator first, InputIterator last, 421 void inverse(InputIterator first, InputIterator last, 408 422 std::multimap<Key, size_t, Comp>& m) 409 423 { … … 445 459 */ 446 460 inline bool operator()(T x, T y) const 447 { 461 { 448 462 if (std::isnan(x)) 449 463 return false; … … 459 473 */ 460 474 template<> 461 struct less_nan<DataWeight> 475 struct less_nan<DataWeight> 462 476 : std::binary_function<DataWeight, DataWeight, bool> 463 477 { … … 466 480 */ 467 481 inline bool operator()(const DataWeight& x, const DataWeight& y) const 468 { 482 { 469 483 less_nan<double> compare; 470 484 return compare(x.data(), y.data()); … … 562 576 const std::pair<T1,T2>& y) { 563 577 return ((x.second<y.second)  564 (!(y.second<x.second) && (x.first<y.first))); 578 (!(y.second<x.second) && (x.first<y.first))); 565 579 } 566 580 }; … … 574 588 */ 575 589 template <class Pair> 576 struct PairFirst 590 struct PairFirst 577 591 { 578 592 /** … … 582 596 */ 583 597 typedef typename boost::mpl::if_< 584 typename boost::is_const<Pair>::type, 598 typename boost::is_const<Pair>::type, 585 599 typename boost::add_const<typename Pair::first_type>::type&, 586 600 typename Pair::first_type&>::type result_type; 587 601 588 602 /** 589 603 The argument type is Pair&. … … 608 622 */ 609 623 template <class Pair> 610 struct PairSecond 624 struct PairSecond 611 625 { 612 626 /** … … 616 630 */ 617 631 typedef typename boost::mpl::if_< 618 typename boost::is_const<Pair>::type, 632 typename boost::is_const<Pair>::type, 619 633 typename boost::add_const<typename Pair::second_type>::type&, 620 634 typename Pair::second_type&>::type result_type; 621 635 622 636 /** 623 637 The argument type is Pair&. … … 662 676 typedef typename std::iterator_traits<Iter>::reference ref_type; 663 677 typedef typename boost::remove_reference<ref_type>::type val_type; 664 typedef PairFirst<val_type> PF; 678 typedef PairFirst<val_type> PF; 665 679 return boost::transform_iterator<PF, Iter>(i, PF()); 666 680 } … … 693 707 typedef typename std::iterator_traits<Iter>::reference ref_type; 694 708 typedef typename boost::remove_reference<ref_type>::type val_type; 695 typedef PairSecond<val_type> PS; 709 typedef PairSecond<val_type> PS; 696 710 return boost::transform_iterator<PS, Iter>(i, PS()); 697 711 } … … 707 721 std::vector<MyClass*> vec(18); 708 722 ... 709 std::sort(vec.begin(), vec.end(), 723 std::sort(vec.begin(), vec.end(), 710 724 make_ptr_compare(vec[0], std::greater<MyClass>()); 711 725 \endcode … … 771 785 if (iter==m.end()) { 772 786 std::stringstream ss; 773 ss << "utility::get(const Map&, const Key&): `" 787 ss << "utility::get(const Map&, const Key&): `" 774 788 << key << "' not found in map\n"; 775 789 throw runtime_error(ss.str());
Note: See TracChangeset
for help on using the changeset viewer.