search.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2022-2022.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #ifndef BOOST_MOVE_DETAIL_SEARCH_HPP
  12. #define BOOST_MOVE_DETAIL_SEARCH_HPP
  13. #include <boost/move/detail/iterator_traits.hpp>
  14. #if defined(BOOST_CLANG) || (defined(BOOST_GCC) && (BOOST_GCC >= 40600))
  15. #pragma GCC diagnostic push
  16. #pragma GCC diagnostic ignored "-Wsign-conversion"
  17. #endif
  18. namespace boost {
  19. namespace movelib {
  20. template <class RandIt, class T, class Compare>
  21. RandIt lower_bound
  22. (RandIt first, const RandIt last, const T& key, Compare comp)
  23. {
  24. typedef typename iter_size<RandIt>::type size_type;
  25. size_type len = size_type(last - first);
  26. RandIt middle;
  27. while (len) {
  28. size_type step = size_type(len >> 1);
  29. middle = first;
  30. middle += step;
  31. if (comp(*middle, key)) {
  32. first = ++middle;
  33. len = size_type(len - (step + 1));
  34. }
  35. else{
  36. len = step;
  37. }
  38. }
  39. return first;
  40. }
  41. template <class RandIt, class T, class Compare>
  42. RandIt upper_bound
  43. (RandIt first, const RandIt last, const T& key, Compare comp)
  44. {
  45. typedef typename iter_size<RandIt>::type size_type;
  46. size_type len = size_type(last - first);
  47. RandIt middle;
  48. while (len) {
  49. size_type step = size_type(len >> 1);
  50. middle = first;
  51. middle += step;
  52. if (!comp(key, *middle)) {
  53. first = ++middle;
  54. len = size_type(len - (step + 1));
  55. }
  56. else{
  57. len = step;
  58. }
  59. }
  60. return first;
  61. }
  62. } //namespace movelib {
  63. } //namespace boost {
  64. #if defined(BOOST_CLANG) || (defined(BOOST_GCC) && (BOOST_GCC >= 40600))
  65. #pragma GCC diagnostic pop
  66. #endif
  67. #endif //#define BOOST_MOVE_DETAIL_SEARCH_HPP