algorithm.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: algorithm.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file contains Google extensions to the standard <algorithm> C++
  20. // header.
  21. #ifndef Y_ABSL_ALGORITHM_ALGORITHM_H_
  22. #define Y_ABSL_ALGORITHM_ALGORITHM_H_
  23. #include <algorithm>
  24. #include <iterator>
  25. #include <type_traits>
  26. #include "y_absl/base/config.h"
  27. namespace y_absl {
  28. Y_ABSL_NAMESPACE_BEGIN
  29. namespace algorithm_internal {
  30. // Performs comparisons with operator==, similar to C++14's `std::equal_to<>`.
  31. struct EqualTo {
  32. template <typename T, typename U>
  33. bool operator()(const T& a, const U& b) const {
  34. return a == b;
  35. }
  36. };
  37. template <typename InputIter1, typename InputIter2, typename Pred>
  38. bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
  39. InputIter2 last2, Pred pred, std::input_iterator_tag,
  40. std::input_iterator_tag) {
  41. while (true) {
  42. if (first1 == last1) return first2 == last2;
  43. if (first2 == last2) return false;
  44. if (!pred(*first1, *first2)) return false;
  45. ++first1;
  46. ++first2;
  47. }
  48. }
  49. template <typename InputIter1, typename InputIter2, typename Pred>
  50. bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
  51. InputIter2 last2, Pred&& pred, std::random_access_iterator_tag,
  52. std::random_access_iterator_tag) {
  53. return (last1 - first1 == last2 - first2) &&
  54. std::equal(first1, last1, first2, std::forward<Pred>(pred));
  55. }
  56. // When we are using our own internal predicate that just applies operator==, we
  57. // forward to the non-predicate form of std::equal. This enables an optimization
  58. // in libstdc++ that can result in std::memcmp being used for integer types.
  59. template <typename InputIter1, typename InputIter2>
  60. bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
  61. InputIter2 last2, algorithm_internal::EqualTo /* unused */,
  62. std::random_access_iterator_tag,
  63. std::random_access_iterator_tag) {
  64. return (last1 - first1 == last2 - first2) &&
  65. std::equal(first1, last1, first2);
  66. }
  67. template <typename It>
  68. It RotateImpl(It first, It middle, It last, std::true_type) {
  69. return std::rotate(first, middle, last);
  70. }
  71. template <typename It>
  72. It RotateImpl(It first, It middle, It last, std::false_type) {
  73. std::rotate(first, middle, last);
  74. return std::next(first, std::distance(middle, last));
  75. }
  76. } // namespace algorithm_internal
  77. // equal()
  78. //
  79. // Compares the equality of two ranges specified by pairs of iterators, using
  80. // the given predicate, returning true iff for each corresponding iterator i1
  81. // and i2 in the first and second range respectively, pred(*i1, *i2) == true
  82. //
  83. // This comparison takes at most min(`last1` - `first1`, `last2` - `first2`)
  84. // invocations of the predicate. Additionally, if InputIter1 and InputIter2 are
  85. // both random-access iterators, and `last1` - `first1` != `last2` - `first2`,
  86. // then the predicate is never invoked and the function returns false.
  87. //
  88. // This is a C++11-compatible implementation of C++14 `std::equal`. See
  89. // https://en.cppreference.com/w/cpp/algorithm/equal for more information.
  90. template <typename InputIter1, typename InputIter2, typename Pred>
  91. bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2,
  92. InputIter2 last2, Pred&& pred) {
  93. return algorithm_internal::EqualImpl(
  94. first1, last1, first2, last2, std::forward<Pred>(pred),
  95. typename std::iterator_traits<InputIter1>::iterator_category{},
  96. typename std::iterator_traits<InputIter2>::iterator_category{});
  97. }
  98. // Overload of equal() that performs comparison of two ranges specified by pairs
  99. // of iterators using operator==.
  100. template <typename InputIter1, typename InputIter2>
  101. bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2,
  102. InputIter2 last2) {
  103. return y_absl::equal(first1, last1, first2, last2,
  104. algorithm_internal::EqualTo{});
  105. }
  106. // linear_search()
  107. //
  108. // Performs a linear search for `value` using the iterator `first` up to
  109. // but not including `last`, returning true if [`first`, `last`) contains an
  110. // element equal to `value`.
  111. //
  112. // A linear search is of O(n) complexity which is guaranteed to make at most
  113. // n = (`last` - `first`) comparisons. A linear search over short containers
  114. // may be faster than a binary search, even when the container is sorted.
  115. template <typename InputIterator, typename EqualityComparable>
  116. bool linear_search(InputIterator first, InputIterator last,
  117. const EqualityComparable& value) {
  118. return std::find(first, last, value) != last;
  119. }
  120. // rotate()
  121. //
  122. // Performs a left rotation on a range of elements (`first`, `last`) such that
  123. // `middle` is now the first element. `rotate()` returns an iterator pointing to
  124. // the first element before rotation. This function is exactly the same as
  125. // `std::rotate`, but fixes a bug in gcc
  126. // <= 4.9 where `std::rotate` returns `void` instead of an iterator.
  127. //
  128. // The complexity of this algorithm is the same as that of `std::rotate`, but if
  129. // `ForwardIterator` is not a random-access iterator, then `y_absl::rotate`
  130. // performs an additional pass over the range to construct the return value.
  131. template <typename ForwardIterator>
  132. ForwardIterator rotate(ForwardIterator first, ForwardIterator middle,
  133. ForwardIterator last) {
  134. return algorithm_internal::RotateImpl(
  135. first, middle, last,
  136. std::is_same<decltype(std::rotate(first, middle, last)),
  137. ForwardIterator>());
  138. }
  139. Y_ABSL_NAMESPACE_END
  140. } // namespace y_absl
  141. #endif // Y_ABSL_ALGORITHM_ALGORITHM_H_