compare_functors.hpp 3.9 KB

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