algorithm.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2014.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/container for documentation.
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_CONTAINER_DETAIL_ALGORITHM_HPP
  13. #define BOOST_CONTAINER_DETAIL_ALGORITHM_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #if defined(BOOST_HAS_PRAGMA_ONCE)
  18. # pragma once
  19. #endif
  20. #include <boost/intrusive/detail/algorithm.hpp>
  21. namespace boost {
  22. namespace container {
  23. using boost::intrusive::algo_equal;
  24. using boost::intrusive::algo_lexicographical_compare;
  25. template<class Func>
  26. class binder1st
  27. {
  28. public:
  29. typedef typename Func::second_argument_type argument_type;
  30. typedef typename Func::result_type result_type;
  31. binder1st(const Func& func, const typename Func::first_argument_type& arg)
  32. : op(func), value(arg)
  33. {}
  34. result_type operator()(const argument_type& arg) const
  35. { return op(value, arg); }
  36. result_type operator()(argument_type& arg) const
  37. { return op(value, arg); }
  38. private:
  39. Func op;
  40. typename Func::first_argument_type value;
  41. };
  42. template<class Func, class T>
  43. inline binder1st<Func> bind1st(const Func& func, const T& arg)
  44. { return boost::container::binder1st<Func>(func, arg); }
  45. template<class Func>
  46. class binder2nd
  47. {
  48. public:
  49. typedef typename Func::first_argument_type argument_type;
  50. typedef typename Func::result_type result_type;
  51. binder2nd(const Func& func, const typename Func::second_argument_type& arg)
  52. : op(func), value(arg)
  53. {}
  54. result_type operator()(const argument_type& arg) const
  55. { return op(arg, value); }
  56. result_type operator()(argument_type& arg) const
  57. { return op(arg, value); }
  58. private:
  59. Func op;
  60. typename Func::second_argument_type value;
  61. };
  62. template<class Func, class T>
  63. inline binder2nd<Func> bind2nd(const Func& func, const T& arg)
  64. {
  65. return (boost::container::binder2nd<Func>(func, arg));
  66. }
  67. template<class Func>
  68. class unary_negate
  69. {
  70. public:
  71. typedef typename Func::argument_type argument_type;
  72. typedef typename Func::result_type result_type;
  73. explicit unary_negate(const Func& func)
  74. : m_func(func)
  75. {}
  76. bool operator()(const typename Func::argument_type& arg) const
  77. { return !m_func(arg); }
  78. private:
  79. Func m_func;
  80. };
  81. template<class Func> inline
  82. unary_negate<Func> not1(const Func& func)
  83. {
  84. return boost::container::unary_negate<Func>(func);
  85. }
  86. template<class InputIt, class UnaryPredicate>
  87. InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
  88. {
  89. for (; first != last; ++first) {
  90. if (p(*first)) {
  91. return first;
  92. }
  93. }
  94. return last;
  95. }
  96. template<class InputIt, class ForwardIt, class BinaryPredicate>
  97. InputIt find_first_of(InputIt first1, InputIt last1, ForwardIt first2, ForwardIt last2, BinaryPredicate p)
  98. {
  99. for (; first1 != last1; ++first1) {
  100. for (ForwardIt it = first2; it != last2; ++it) {
  101. if (p(*first1, *it)) {
  102. return first1;
  103. }
  104. }
  105. }
  106. return last1;
  107. }
  108. template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
  109. ForwardIt1 search(ForwardIt1 first1, ForwardIt1 last1,
  110. ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p)
  111. {
  112. for (; ; ++first1) {
  113. ForwardIt1 it = first1;
  114. for (ForwardIt2 it2 = first2; ; ++it, ++it2) {
  115. if (it2 == last2) {
  116. return first1;
  117. }
  118. if (it == last1) {
  119. return last1;
  120. }
  121. if (!p(*it, *it2)) {
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. } //namespace container {
  128. } //namespace boost {
  129. #endif //#ifndef BOOST_CONTAINER_DETAIL_ALGORITHM_HPP