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