algorithm.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/intrusive for documentation.
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_DETAIL_ALGORITHM_HPP
  13. #define BOOST_INTRUSIVE_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. namespace boost {
  21. namespace intrusive {
  22. struct algo_pred_equal
  23. {
  24. template<class T>
  25. bool operator()(const T &x, const T &y) const
  26. { return x == y; }
  27. };
  28. struct algo_pred_less
  29. {
  30. template<class T>
  31. bool operator()(const T &x, const T &y) const
  32. { return x < y; }
  33. };
  34. template<class InputIt1, class InputIt2, class BinaryPredicate>
  35. bool algo_equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p)
  36. {
  37. for (; first1 != last1; ++first1, ++first2) {
  38. if (!p(*first1, *first2)) {
  39. return false;
  40. }
  41. }
  42. return true;
  43. }
  44. template<class InputIt1, class InputIt2>
  45. bool algo_equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
  46. { return (algo_equal)(first1, last1, first2, algo_pred_equal()); }
  47. template<class InputIt1, class InputIt2, class BinaryPredicate>
  48. bool algo_equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate pred)
  49. {
  50. for (; first1 != last1 && first2 != last2; ++first1, ++first2)
  51. if (!pred(*first1, *first2))
  52. return false;
  53. return first1 == last1 && first2 == last2;
  54. }
  55. template<class InputIt1, class InputIt2>
  56. bool algo_equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
  57. { return (algo_equal)(first1, last1, first2, last2, algo_pred_equal()); }
  58. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  59. bool algo_lexicographical_compare (InputIterator1 first1, InputIterator1 last1,
  60. InputIterator2 first2, InputIterator2 last2,
  61. BinaryPredicate pred)
  62. {
  63. while (first1 != last1){
  64. if (first2 == last2 || *first2 < *first1) return false;
  65. else if (pred(*first1, *first2)) return true;
  66. ++first1; ++first2;
  67. }
  68. return (first2 != last2);
  69. }
  70. template <class InputIterator1, class InputIterator2>
  71. bool algo_lexicographical_compare (InputIterator1 first1, InputIterator1 last1,
  72. InputIterator2 first2, InputIterator2 last2)
  73. { return (algo_lexicographical_compare)(first1, last1, first2, last2, algo_pred_less()); }
  74. } //namespace intrusive {
  75. } //namespace boost {
  76. #endif //#ifndef BOOST_INTRUSIVE_DETAIL_ALGORITHM_HPP