compare_functors.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. template <class U>
  32. inline bool operator()(const U &t)const
  33. {
  34. return t_ == t;
  35. }
  36. };
  37. template<class ValueType>
  38. class equal_to_value_first
  39. {
  40. typedef ValueType value_type;
  41. const value_type &t_;
  42. public:
  43. inline explicit equal_to_value_first(const value_type &t)
  44. : t_(t)
  45. {}
  46. template <class U>
  47. inline bool operator()(const U &t)const
  48. {
  49. return t_ == t.first;
  50. }
  51. };
  52. template<class Node, class Pred, class Ret = bool>
  53. struct value_to_node_compare
  54. : Pred
  55. {
  56. typedef Pred predicate_type;
  57. typedef Node node_type;
  58. inline value_to_node_compare()
  59. : Pred()
  60. {}
  61. inline explicit value_to_node_compare(Pred pred)
  62. : Pred(pred)
  63. {}
  64. inline Ret operator()(const Node &a, const Node &b) const
  65. { return static_cast<const Pred&>(*this)(a.get_data(), b.get_data()); }
  66. inline Ret operator()(const Node &a) const
  67. { return static_cast<const Pred&>(*this)(a.get_data()); }
  68. inline Ret operator()(const Node &a, const Node &b)
  69. { return static_cast<Pred&>(*this)(a.get_data(), b.get_data()); }
  70. inline Ret operator()(const Node &a)
  71. { return static_cast<Pred&>(*this)(a.get_data()); }
  72. inline predicate_type & predicate() { return static_cast<predicate_type&>(*this); }
  73. inline const predicate_type & predicate() const { return static_cast<predicate_type&>(*this); }
  74. };
  75. template<class KeyPred, class KeyOfValue, class Node, class Ret = bool>
  76. struct key_node_pred
  77. : public boost::intrusive::detail::ebo_functor_holder<KeyPred>
  78. {
  79. inline explicit key_node_pred(const KeyPred &comp)
  80. : base_t(comp)
  81. {}
  82. inline explicit key_node_pred()
  83. {}
  84. typedef boost::intrusive::detail::ebo_functor_holder<KeyPred> base_t;
  85. typedef KeyPred key_predicate;
  86. typedef KeyOfValue key_of_value;
  87. typedef typename KeyOfValue::type key_type;
  88. inline static const key_type &key_from(const Node &n)
  89. {
  90. return key_of_value()(n.get_data());
  91. }
  92. template <class T>
  93. inline static const T &
  94. key_from(const T &t)
  95. { return t; }
  96. inline const key_predicate &key_pred() const
  97. { return static_cast<const key_predicate &>(*this); }
  98. inline key_predicate &key_pred()
  99. { return static_cast<key_predicate &>(*this); }
  100. inline Ret operator()(const key_type &key) const
  101. { return this->key_pred()(key); }
  102. template<class U>
  103. inline Ret operator()(const U &nonkey) const
  104. { return this->key_pred()(this->key_from(nonkey)); }
  105. inline bool operator()(const key_type &key1, const key_type &key2) const
  106. { return this->key_pred()(key1, key2); }
  107. template<class U>
  108. inline bool operator()(const key_type &key1, const U &nonkey2) const
  109. { return this->key_pred()(key1, this->key_from(nonkey2)); }
  110. template<class U>
  111. inline bool operator()(const U &nonkey1, const key_type &key2) const
  112. { return this->key_pred()(this->key_from(nonkey1), key2); }
  113. template<class U, class V>
  114. inline bool operator()(const U &nonkey1, const V &nonkey2) const
  115. { return this->key_pred()(this->key_from(nonkey1), this->key_from(nonkey2)); }
  116. };
  117. } //namespace container {
  118. } //namespace boost {
  119. #endif //BOOST_CONTAINER_DETAIL_COMPARE_FUNCTORS_HPP