compare_functors.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. namespace boost {
  20. namespace container {
  21. template<class ValueType>
  22. class equal_to_value
  23. {
  24. typedef ValueType value_type;
  25. const value_type &t_;
  26. public:
  27. explicit equal_to_value(const value_type &t)
  28. : t_(t)
  29. {}
  30. bool operator()(const value_type &t)const
  31. { return t_ == t; }
  32. };
  33. template<class Node, class Pred, class Ret = bool>
  34. struct value_to_node_compare
  35. : Pred
  36. {
  37. typedef Pred predicate_type;
  38. typedef Node node_type;
  39. value_to_node_compare()
  40. : Pred()
  41. {}
  42. explicit value_to_node_compare(Pred pred)
  43. : Pred(pred)
  44. {}
  45. Ret operator()(const Node &a, const Node &b) const
  46. { return static_cast<const Pred&>(*this)(a.get_data(), b.get_data()); }
  47. Ret operator()(const Node &a) const
  48. { return static_cast<const Pred&>(*this)(a.get_data()); }
  49. Ret operator()(const Node &a, const Node &b)
  50. { return static_cast<Pred&>(*this)(a.get_data(), b.get_data()); }
  51. Ret operator()(const Node &a)
  52. { return static_cast<Pred&>(*this)(a.get_data()); }
  53. predicate_type & predicate() { return static_cast<predicate_type&>(*this); }
  54. const predicate_type & predicate() const { return static_cast<predicate_type&>(*this); }
  55. };
  56. template<class KeyPred, class KeyOfValue, class Node, class Ret = bool>
  57. struct key_node_pred
  58. : public boost::intrusive::detail::ebo_functor_holder<KeyPred>
  59. {
  60. BOOST_CONTAINER_FORCEINLINE explicit key_node_pred(const KeyPred &comp)
  61. : base_t(comp)
  62. {}
  63. typedef boost::intrusive::detail::ebo_functor_holder<KeyPred> base_t;
  64. typedef KeyPred key_predicate;
  65. typedef KeyOfValue key_of_value;
  66. typedef typename KeyOfValue::type key_type;
  67. BOOST_CONTAINER_FORCEINLINE static const key_type &key_from(const Node &n)
  68. {
  69. return key_of_value()(n.get_data());
  70. }
  71. template <class T>
  72. BOOST_CONTAINER_FORCEINLINE static const T &
  73. key_from(const T &t)
  74. { return t; }
  75. BOOST_CONTAINER_FORCEINLINE const key_predicate &key_pred() const
  76. { return static_cast<const key_predicate &>(*this); }
  77. BOOST_CONTAINER_FORCEINLINE key_predicate &key_pred()
  78. { return static_cast<key_predicate &>(*this); }
  79. BOOST_CONTAINER_FORCEINLINE Ret operator()(const key_type &key) const
  80. { return this->key_pred()(key); }
  81. template<class U>
  82. BOOST_CONTAINER_FORCEINLINE Ret operator()(const U &nonkey) const
  83. { return this->key_pred()(this->key_from(nonkey)); }
  84. BOOST_CONTAINER_FORCEINLINE bool operator()(const key_type &key1, const key_type &key2) const
  85. { return this->key_pred()(key1, key2); }
  86. template<class U>
  87. BOOST_CONTAINER_FORCEINLINE bool operator()(const key_type &key1, const U &nonkey2) const
  88. { return this->key_pred()(key1, this->key_from(nonkey2)); }
  89. template<class U>
  90. BOOST_CONTAINER_FORCEINLINE bool operator()(const U &nonkey1, const key_type &key2) const
  91. { return this->key_pred()(this->key_from(nonkey1), key2); }
  92. template<class U, class V>
  93. BOOST_CONTAINER_FORCEINLINE bool operator()(const U &nonkey1, const V &nonkey2) const
  94. { return this->key_pred()(this->key_from(nonkey1), this->key_from(nonkey2)); }
  95. };
  96. } //namespace container {
  97. } //namespace boost {
  98. #endif //BOOST_CONTAINER_DETAIL_COMPARE_FUNCTORS_HPP