indirect_cmp.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. //
  11. #ifndef BOOST_INDIRECT_CMP_HPP
  12. #define BOOST_INDIRECT_CMP_HPP
  13. #include <functional>
  14. #include <boost/config.hpp>
  15. #include <boost/property_map/property_map.hpp>
  16. namespace boost {
  17. //: indirect_cmp
  18. //
  19. // could also do this with compose_f_gx_hx, and the member binder...
  20. //
  21. //!category: functors
  22. //!component: type
  23. //!tparam: ReadablePropertyMap - a model of ReadablePropertyMap
  24. //!definition: functor.h
  25. template <class ReadablePropertyMap, class Compare>
  26. class indirect_cmp {
  27. public:
  28. typedef typename boost::property_traits<ReadablePropertyMap>::value_type T;
  29. typedef typename boost::property_traits<ReadablePropertyMap>::key_type K;
  30. typedef K first_argument_type;
  31. typedef K second_argument_type;
  32. typedef bool result_type;
  33. inline indirect_cmp(const ReadablePropertyMap& df, const Compare& c = Compare())
  34. : d(df), cmp(c) { }
  35. template <class A, class B>
  36. inline bool
  37. operator()(const A& u, const B& v) const {
  38. const T& du = get(d, u);
  39. const T& dv = get(d, v);
  40. return cmp(du, dv);
  41. }
  42. protected:
  43. ReadablePropertyMap d;
  44. Compare cmp;
  45. };
  46. template <typename Compare, typename ReadablePropertyMap>
  47. indirect_cmp<ReadablePropertyMap, Compare>
  48. make_indirect_cmp(const Compare& cmp, ReadablePropertyMap pmap) {
  49. indirect_cmp<ReadablePropertyMap, Compare> p(pmap, cmp);
  50. return p;
  51. }
  52. template <class ReadablePropertyMap>
  53. class indirect_pmap {
  54. public:
  55. typedef typename boost::property_traits<ReadablePropertyMap>::value_type T;
  56. typedef typename boost::property_traits<ReadablePropertyMap>::key_type K;
  57. typedef K argument_type;
  58. typedef T result_type;
  59. inline indirect_pmap(const ReadablePropertyMap& df)
  60. : d(df) { }
  61. inline T operator()(const K& u) const {
  62. return get(d, u);
  63. }
  64. protected:
  65. ReadablePropertyMap d;
  66. };
  67. template <typename ReadablePropertyMap>
  68. indirect_pmap<ReadablePropertyMap>
  69. make_indirect_pmap(ReadablePropertyMap pmap) {
  70. indirect_pmap<ReadablePropertyMap> f(pmap);
  71. return f;
  72. }
  73. } // namespace boost
  74. #endif // GGCL_INDIRECT_CMP_HPP