You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

algorithm.h 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 ABSL_ALGORITHM_ALGORITHM_H_
  22. #define ABSL_ALGORITHM_ALGORITHM_H_
  23. #include <algorithm>
  24. #include <iterator>
  25. #include <type_traits>
  26. #include "absl/base/config.h"
  27. namespace absl
  28. {
  29. ABSL_NAMESPACE_BEGIN
  30. namespace algorithm_internal
  31. {
  32. // Performs comparisons with operator==, similar to C++14's `std::equal_to<>`.
  33. struct EqualTo
  34. {
  35. template<typename T, typename U>
  36. bool operator()(const T& a, const U& b) const
  37. {
  38. return a == b;
  39. }
  40. };
  41. template<typename InputIter1, typename InputIter2, typename Pred>
  42. bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Pred pred, std::input_iterator_tag, std::input_iterator_tag)
  43. {
  44. while (true)
  45. {
  46. if (first1 == last1)
  47. return first2 == last2;
  48. if (first2 == last2)
  49. return false;
  50. if (!pred(*first1, *first2))
  51. return false;
  52. ++first1;
  53. ++first2;
  54. }
  55. }
  56. template<typename InputIter1, typename InputIter2, typename Pred>
  57. bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Pred&& pred, std::random_access_iterator_tag, std::random_access_iterator_tag)
  58. {
  59. return (last1 - first1 == last2 - first2) &&
  60. std::equal(first1, last1, first2, std::forward<Pred>(pred));
  61. }
  62. // When we are using our own internal predicate that just applies operator==, we
  63. // forward to the non-predicate form of std::equal. This enables an optimization
  64. // in libstdc++ that can result in std::memcmp being used for integer types.
  65. template<typename InputIter1, typename InputIter2>
  66. bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, algorithm_internal::EqualTo /* unused */, std::random_access_iterator_tag, std::random_access_iterator_tag)
  67. {
  68. return (last1 - first1 == last2 - first2) &&
  69. std::equal(first1, last1, first2);
  70. }
  71. template<typename It>
  72. It RotateImpl(It first, It middle, It last, std::true_type)
  73. {
  74. return std::rotate(first, middle, last);
  75. }
  76. template<typename It>
  77. It RotateImpl(It first, It middle, It last, std::false_type)
  78. {
  79. std::rotate(first, middle, last);
  80. return std::next(first, std::distance(middle, last));
  81. }
  82. } // namespace algorithm_internal
  83. // equal()
  84. //
  85. // Compares the equality of two ranges specified by pairs of iterators, using
  86. // the given predicate, returning true iff for each corresponding iterator i1
  87. // and i2 in the first and second range respectively, pred(*i1, *i2) == true
  88. //
  89. // This comparison takes at most min(`last1` - `first1`, `last2` - `first2`)
  90. // invocations of the predicate. Additionally, if InputIter1 and InputIter2 are
  91. // both random-access iterators, and `last1` - `first1` != `last2` - `first2`,
  92. // then the predicate is never invoked and the function returns false.
  93. //
  94. // This is a C++11-compatible implementation of C++14 `std::equal`. See
  95. // https://en.cppreference.com/w/cpp/algorithm/equal for more information.
  96. template<typename InputIter1, typename InputIter2, typename Pred>
  97. bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Pred&& pred)
  98. {
  99. return algorithm_internal::EqualImpl(
  100. first1, last1, first2, last2, std::forward<Pred>(pred), typename std::iterator_traits<InputIter1>::iterator_category{}, typename std::iterator_traits<InputIter2>::iterator_category{}
  101. );
  102. }
  103. // Overload of equal() that performs comparison of two ranges specified by pairs
  104. // of iterators using operator==.
  105. template<typename InputIter1, typename InputIter2>
  106. bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2)
  107. {
  108. return absl::equal(first1, last1, first2, last2, algorithm_internal::EqualTo{});
  109. }
  110. // linear_search()
  111. //
  112. // Performs a linear search for `value` using the iterator `first` up to
  113. // but not including `last`, returning true if [`first`, `last`) contains an
  114. // element equal to `value`.
  115. //
  116. // A linear search is of O(n) complexity which is guaranteed to make at most
  117. // n = (`last` - `first`) comparisons. A linear search over short containers
  118. // may be faster than a binary search, even when the container is sorted.
  119. template<typename InputIterator, typename EqualityComparable>
  120. bool linear_search(InputIterator first, InputIterator last, const EqualityComparable& value)
  121. {
  122. return std::find(first, last, value) != last;
  123. }
  124. // rotate()
  125. //
  126. // Performs a left rotation on a range of elements (`first`, `last`) such that
  127. // `middle` is now the first element. `rotate()` returns an iterator pointing to
  128. // the first element before rotation. This function is exactly the same as
  129. // `std::rotate`, but fixes a bug in gcc
  130. // <= 4.9 where `std::rotate` returns `void` instead of an iterator.
  131. //
  132. // The complexity of this algorithm is the same as that of `std::rotate`, but if
  133. // `ForwardIterator` is not a random-access iterator, then `absl::rotate`
  134. // performs an additional pass over the range to construct the return value.
  135. template<typename ForwardIterator>
  136. ForwardIterator rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last)
  137. {
  138. return algorithm_internal::RotateImpl(
  139. first, middle, last, std::is_same<decltype(std::rotate(first, middle, last)), ForwardIterator>()
  140. );
  141. }
  142. ABSL_NAMESPACE_END
  143. } // namespace absl
  144. #endif // ABSL_ALGORITHM_ALGORITHM_H_