compare_functors.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2014. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_DETAIL_COMPARE_FUNCTORS_HPP
  11. #define BOOST_CONTAINER_DETAIL_COMPARE_FUNCTORS_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/intrusive/detail/ebo_functor_holder.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. namespace boost {
  21. namespace container {
  22. template<class ValueType>
  23. class equal_to_value
  24. {
  25. typedef ValueType value_type;
  26. const value_type &t_;
  27. public:
  28. inline explicit equal_to_value(const value_type &t)
  29. : t_(t)
  30. {}
  31. inline bool operator()(const value_type &t)const
  32. { return t_ == t; }
  33. };
  34. template<class Node, class Pred, class Ret = bool>
  35. struct value_to_node_compare
  36. : Pred
  37. {
  38. typedef Pred predicate_type;
  39. typedef Node node_type;
  40. inline value_to_node_compare()
  41. : Pred()
  42. {}
  43. inline explicit value_to_node_compare(Pred pred)
  44. : Pred(pred)
  45. {}
  46. inline Ret operator()(const Node &a, const Node &b) const
  47. { return static_cast<const Pred&>(*this)(a.get_data(), b.get_data()); }
  48. inline Ret operator()(const Node &a) const
  49. { return static_cast<const Pred&>(*this)(a.get_data()); }
  50. inline Ret operator()(const Node &a, const Node &b)
  51. { return static_cast<Pred&>(*this)(a.get_data(), b.get_data()); }
  52. inline Ret operator()(const Node &a)
  53. { return static_cast<Pred&>(*this)(a.get_data()); }
  54. inline predicate_type & predicate() { return static_cast<predicate_type&>(*this); }
  55. inline const predicate_type & predicate() const { return static_cast<predicate_type&>(*this); }
  56. };
  57. template<class KeyPred, class KeyOfValue, class Node, class Ret = bool>
  58. struct key_node_pred
  59. : public boost::intrusive::detail::ebo_functor_holder<KeyPred>
  60. {
  61. inline explicit key_node_pred(const KeyPred &comp)
  62. : base_t(comp)
  63. {}
  64. inline explicit key_node_pred()
  65. {}
  66. typedef boost::intrusive::detail::ebo_functor_holder<KeyPred> base_t;
  67. typedef KeyPred key_predicate;
  68. typedef KeyOfValue key_of_value;
  69. typedef typename KeyOfValue::type key_type;
  70. inline static const key_type &key_from(const Node &n)
  71. {
  72. return key_of_value()(n.get_data());
  73. }
  74. template <class T>
  75. inline static const T &
  76. key_from(const T &t)
  77. { return t; }
  78. inline const key_predicate &key_pred() const
  79. { return static_cast<const key_predicate &>(*this); }
  80. inline key_predicate &key_pred()
  81. { return static_cast<key_predicate &>(*this); }
  82. inline Ret operator()(const key_type &key) const
  83. { return this->key_pred()(key); }
  84. template<class U>
  85. inline Ret operator()(const U &nonkey) const
  86. { return this->key_pred()(this->key_from(nonkey)); }
  87. inline bool operator()(const key_type &key1, const key_type &key2) const
  88. { return this->key_pred()(key1, key2); }
  89. template<class U>
  90. inline bool operator()(const key_type &key1, const U &nonkey2) const
  91. { return this->key_pred()(key1, this->key_from(nonkey2)); }
  92. template<class U>
  93. inline bool operator()(const U &nonkey1, const key_type &key2) const
  94. { return this->key_pred()(this->key_from(nonkey1), key2); }
  95. template<class U, class V>
  96. inline bool operator()(const U &nonkey1, const V &nonkey2) const
  97. { return this->key_pred()(this->key_from(nonkey1), this->key_from(nonkey2)); }
  98. };
  99. } //namespace container {
  100. } //namespace boost {
  101. #endif //BOOST_CONTAINER_DETAIL_COMPARE_FUNCTORS_HPP