indirect_cmp.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. {
  18. //: indirect_cmp
  19. //
  20. // could also do this with compose_f_gx_hx, and the member binder...
  21. //
  22. //! category: functors
  23. //! component: type
  24. //! tparam: ReadablePropertyMap - a model of ReadablePropertyMap
  25. //! definition: functor.h
  26. template < class ReadablePropertyMap, class Compare > class indirect_cmp
  27. {
  28. public:
  29. typedef
  30. typename boost::property_traits< ReadablePropertyMap >::value_type T;
  31. typedef typename boost::property_traits< ReadablePropertyMap >::key_type K;
  32. typedef K first_argument_type;
  33. typedef K second_argument_type;
  34. typedef bool result_type;
  35. inline indirect_cmp(
  36. const ReadablePropertyMap& df, const Compare& c = Compare())
  37. : d(df), cmp(c)
  38. {
  39. }
  40. template < class A, class B >
  41. inline bool operator()(const A& u, const B& v) const
  42. {
  43. const T& du = get(d, u);
  44. const T& dv = get(d, v);
  45. return cmp(du, dv);
  46. }
  47. protected:
  48. ReadablePropertyMap d;
  49. Compare cmp;
  50. };
  51. template < typename Compare, typename ReadablePropertyMap >
  52. indirect_cmp< ReadablePropertyMap, Compare > make_indirect_cmp(
  53. const Compare& cmp, ReadablePropertyMap pmap)
  54. {
  55. indirect_cmp< ReadablePropertyMap, Compare > p(pmap, cmp);
  56. return p;
  57. }
  58. template < class ReadablePropertyMap > class indirect_pmap
  59. {
  60. public:
  61. typedef
  62. typename boost::property_traits< ReadablePropertyMap >::value_type T;
  63. typedef typename boost::property_traits< ReadablePropertyMap >::key_type K;
  64. typedef K argument_type;
  65. typedef T result_type;
  66. inline indirect_pmap(const ReadablePropertyMap& df) : d(df) {}
  67. inline T operator()(const K& u) const { return get(d, u); }
  68. protected:
  69. ReadablePropertyMap d;
  70. };
  71. template < typename ReadablePropertyMap >
  72. indirect_pmap< ReadablePropertyMap > make_indirect_pmap(
  73. ReadablePropertyMap pmap)
  74. {
  75. indirect_pmap< ReadablePropertyMap > f(pmap);
  76. return f;
  77. }
  78. } // namespace boost
  79. #endif // GGCL_INDIRECT_CMP_HPP