algorithm.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 ForwardIt1, class ForwardIt2, class BinaryPredicate>
  97. ForwardIt1 find_end (ForwardIt1 first1, ForwardIt1 last1
  98. ,ForwardIt2 first2, ForwardIt2 last2
  99. ,BinaryPredicate p)
  100. {
  101. if (first2==last2)
  102. return last1; // specified in C++11
  103. ForwardIt1 ret = last1;
  104. while (first1!=last1)
  105. {
  106. ForwardIt1 it1 = first1;
  107. ForwardIt2 it2 = first2;
  108. while ( p(*it1, *it2) ) {
  109. ++it1; ++it2;
  110. if (it2==last2) {
  111. ret=first1;
  112. break;
  113. }
  114. if (it1==last1)
  115. return ret;
  116. }
  117. ++first1;
  118. }
  119. return ret;
  120. }
  121. template<class InputIt, class ForwardIt, class BinaryPredicate>
  122. InputIt find_first_of(InputIt first1, InputIt last1, ForwardIt first2, ForwardIt last2, BinaryPredicate p)
  123. {
  124. for (; first1 != last1; ++first1) {
  125. for (ForwardIt it = first2; it != last2; ++it) {
  126. if (p(*first1, *it)) {
  127. return first1;
  128. }
  129. }
  130. }
  131. return last1;
  132. }
  133. template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
  134. ForwardIt1 search(ForwardIt1 first1, ForwardIt1 last1,
  135. ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p)
  136. {
  137. for (; ; ++first1) {
  138. ForwardIt1 it = first1;
  139. for (ForwardIt2 it2 = first2; ; ++it, ++it2) {
  140. if (it2 == last2) {
  141. return first1;
  142. }
  143. if (it == last1) {
  144. return last1;
  145. }
  146. if (!p(*it, *it2)) {
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. } //namespace container {
  153. } //namespace boost {
  154. #endif //#ifndef BOOST_CONTAINER_DETAIL_ALGORITHM_HPP