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