blob: 59aeed7d264d927e44648428056cc9c489fad844 [file] [log] [blame]
mistergc2e75482017-09-19 16:54:40 -04001// 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//
nik727338b70432019-03-08 10:27:53 -05007// https://www.apache.org/licenses/LICENSE-2.0
mistergc2e75482017-09-19 16:54:40 -04008//
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
22#ifndef ABSL_ALGORITHM_ALGORITHM_H_
23#define ABSL_ALGORITHM_ALGORITHM_H_
24
25#include <algorithm>
26#include <iterator>
27#include <type_traits>
28
Abseil Team12bc53e2019-12-12 10:36:03 -080029#include "absl/base/config.h"
30
mistergc2e75482017-09-19 16:54:40 -040031namespace absl {
Abseil Team12bc53e2019-12-12 10:36:03 -080032ABSL_NAMESPACE_BEGIN
mistergc2e75482017-09-19 16:54:40 -040033
Abseil Team3df7b522019-11-15 08:07:12 -080034// equal()
Derek Maurob5fb0582023-10-20 12:49:11 -070035// rotate()
Abseil Team3df7b522019-11-15 08:07:12 -080036//
Derek Maurob5fb0582023-10-20 12:49:11 -070037// Historical note: Abseil once provided implementations of these algorithms
38// prior to their adoption in C++14. New code should prefer to use the std
39// variants.
mistergc2e75482017-09-19 16:54:40 -040040//
Derek Maurob5fb0582023-10-20 12:49:11 -070041// See the documentation for the STL <algorithm> header for more information:
42// https://en.cppreference.com/w/cpp/header/algorithm
43using std::equal;
44using std::rotate;
mistergc2e75482017-09-19 16:54:40 -040045
Abseil Team3df7b522019-11-15 08:07:12 -080046// linear_search()
47//
mistergc2e75482017-09-19 16:54:40 -040048// Performs a linear search for `value` using the iterator `first` up to
49// but not including `last`, returning true if [`first`, `last`) contains an
50// element equal to `value`.
51//
52// A linear search is of O(n) complexity which is guaranteed to make at most
53// n = (`last` - `first`) comparisons. A linear search over short containers
54// may be faster than a binary search, even when the container is sorted.
55template <typename InputIterator, typename EqualityComparable>
56bool linear_search(InputIterator first, InputIterator last,
57 const EqualityComparable& value) {
58 return std::find(first, last, value) != last;
59}
60
Abseil Team12bc53e2019-12-12 10:36:03 -080061ABSL_NAMESPACE_END
mistergc2e75482017-09-19 16:54:40 -040062} // namespace absl
63
64#endif // ABSL_ALGORITHM_ALGORITHM_H_