algorithm.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. ABSL_NAMESPACE_BEGIN
  29. // equal()
  30. // rotate()
  31. //
  32. // Historical note: Abseil once provided implementations of these algorithms
  33. // prior to their adoption in C++14. New code should prefer to use the std
  34. // variants.
  35. //
  36. // See the documentation for the STL <algorithm> header for more information:
  37. // https://en.cppreference.com/w/cpp/header/algorithm
  38. using std::equal;
  39. using std::rotate;
  40. // linear_search()
  41. //
  42. // Performs a linear search for `value` using the iterator `first` up to
  43. // but not including `last`, returning true if [`first`, `last`) contains an
  44. // element equal to `value`.
  45. //
  46. // A linear search is of O(n) complexity which is guaranteed to make at most
  47. // n = (`last` - `first`) comparisons. A linear search over short containers
  48. // may be faster than a binary search, even when the container is sorted.
  49. template <typename InputIterator, typename EqualityComparable>
  50. bool linear_search(InputIterator first, InputIterator last,
  51. const EqualityComparable& value) {
  52. return std::find(first, last, value) != last;
  53. }
  54. ABSL_NAMESPACE_END
  55. } // namespace absl
  56. #endif // ABSL_ALGORITHM_ALGORITHM_H_