mirror of
https://github.com/abseil/abseil-cpp.git
synced 2026-06-06 13:24:31 +08:00
-0033c9ea91Fix build on FreeBSD/powerpc (#616) by kgotlinux <60880393+kgotlinux@users.noreply.github.com> -0d5ce2797eExport of internal Abseil changes by Abseil Team <absl-team@google.com> -b69c7d880cExport of internal Abseil changes by Abseil Team <absl-team@google.com> -2a5633fc07Merge "Export of internal Abseil changes" by Xiaoyi Zhang <zhangxy@google.com> -f9b3d6e493Add RISCV support to GetProgramCounter() (#621) by Khem Raj <raj.khem@gmail.com> -0232c87f21Add missing ABSL_HAVE_VDSO_SUPPORT conditional (#622) by Sinan Kaya <41809318+franksinankaya@users.noreply.github.com> -3c81410510Export of internal Abseil changes by Abseil Team <absl-team@google.com> -c44657f556Export of internal Abseil changes by Abseil Team <absl-team@google.com> -98eb410c93Export of internal Abseil changes by Abseil Team <absl-team@google.com> -bf78e97730Export of internal Abseil changes by Abseil Team <absl-team@google.com> -d95d156716Export of internal Abseil changes by Abseil Team <absl-team@google.com> -24713a7036Export of internal Abseil changes by Abseil Team <absl-team@google.com> -72382c21feExport of internal Abseil changes by Abseil Team <absl-team@google.com> -08a7e7bf97Export of internal Abseil changes by Abseil Team <absl-team@google.com> -36bcd9599bFix pointer format specifier in documentation (#614) by Andre Nguyen <andre-nguyen@users.noreply.github.com> -0f86336b69Export of internal Abseil changes by Abseil Team <absl-team@google.com> -c512f118ddExport of internal Abseil changes by Abseil Team <absl-team@google.com> -37dd2562ecExport of internal Abseil changes by Abseil Team <absl-team@google.com> -4442770261fix: Add support for more ARM processors detection (#608) by Andre Nguyen <andre-nguyen@users.noreply.github.com> -159bf2bf6dExport of internal Abseil changes by Abseil Team <absl-team@google.com> -a2e6adecc2Use https links. (#586) by nlewycky <nicholas@mxc.ca> -564001ae50Export of internal Abseil changes by Abseil Team <absl-team@google.com> -b3aaac8a37Export of internal Abseil changes by Abseil Team <absl-team@google.com> -63ee2f8877Export of internal Abseil changes by Abseil Team <absl-team@google.com> -a048203a88Export of internal Abseil changes by Abseil Team <absl-team@google.com> -1de0166368Export of internal Abseil changes by Abseil Team <absl-team@google.com> -ad904b6cd3Export of internal Abseil changes by Abseil Team <absl-team@google.com> -2923513914Export of internal Abseil changes by Abseil Team <absl-team@google.com> -bf86cfe165Export of internal Abseil changes by Abseil Team <absl-team@google.com> -12bc53e031Export of internal Abseil changes by Abseil Team <absl-team@google.com> -1e39f8626aExport of internal Abseil changes by Abseil Team <absl-team@google.com> -77f87009a3Export of internal Abseil changes by Abseil Team <absl-team@google.com> -d659fe54b3Export of internal Abseil changes by Abseil Team <absl-team@google.com> -a4b757b5d4Export of internal Abseil changes by Abseil Team <absl-team@google.com> -0514227d25Export of internal Abseil changes by Abseil Team <absl-team@google.com> -7f4fe64af8Export of internal Abseil changes by Abseil Team <absl-team@google.com> -16d9fd58a5Export of internal Abseil changes by Abseil Team <absl-team@google.com> -bcaae6009cExport of internal Abseil changes by Abseil Team <absl-team@google.com> -8ba96a8244Export of internal Abseil changes by Abseil Team <absl-team@google.com> -2103fd9acdExport of internal Abseil changes by Abseil Team <absl-team@google.com> -3df7b52a6aExport of internal Abseil changes by Abseil Team <absl-team@google.com> -fa8c75182fExport of internal Abseil changes by Abseil Team <absl-team@google.com> -85092b4b64Fix Conan builds (#400) by Adrian Ostrowski <adr.ostrowski@gmail.com> -e96ae2203bExport of internal Abseil changes by Abseil Team <absl-team@google.com> -20de2db748Export of internal Abseil changes by Abseil Team <absl-team@google.com> -846e5dbedaExport of internal Abseil changes by Abseil Team <absl-team@google.com> -8207907f4fExport of internal Abseil changes by Abseil Team <absl-team@google.com> -078b89b3c0Export of internal Abseil changes by Abseil Team <absl-team@google.com> -19b021cb3fExport of internal Abseil changes by Abseil Team <absl-team@google.com> -ecc0033b54Always enable proper symbolize implementation on Windows ... by Loo Rong Jie <loorongjie@gmail.com> -2796d500aeExport of internal Abseil changes by Abseil Team <absl-team@google.com> -e4c8d0eb8eExport of internal Abseil changes by Abseil Team <absl-team@google.com> -a15364ce4dExport of internal Abseil changes by Abseil Team <absl-team@google.com> -ab3552a189Export of internal Abseil changes by Abseil Team <absl-team@google.com> -e9f9000c7cFix ABSL_WAITER_MODE detection for mingw (#342) by Joe Sylve <Joe.Sylve@gmail.com> -abea769b55Fix ABSL_HAVE_ALARM check on mingw (#341) by Joe Sylve <Joe.Sylve@gmail.com> -25597bdfc1Export of internal Abseil changes by Abseil Team <absl-team@google.com> -aad33fefaaExport of internal Abseil changes by Abseil Team <absl-team@google.com> -8fe7214fe2Export of internal Abseil changes by Abseil Team <absl-team@google.com> -debac94cfbExport of internal Abseil changes by Abseil Team <absl-team@google.com> -882b3501a3Fix spelling errors (#384) by Sungmann Cho <55860394+chosungmann@users.noreply.github.com> -502efe6d78Export of internal Abseil changes by Abseil Team <absl-team@google.com> -ccdd1d57b6Export of internal Abseil changes by Abseil Team <absl-team@google.com> -ddf8e52a29Export of internal Abseil changes by Abseil Team <absl-team@google.com> -6ec1362810Export of internal Abseil changes by Abseil Team <absl-team@google.com> -ac78ffc3bcExport of internal Abseil changes by Abseil Team <absl-team@google.com> -5374c56e51Export of internal Abseil changes by Abseil Team <absl-team@google.com> -97c1664b4bExport of internal Abseil changes by Abseil Team <absl-team@google.com> -325fd7b042Export of internal Abseil changes by Abseil Team <absl-team@google.com> -83c1d65c90Export of internal Abseil changes by Abseil Team <absl-team@google.com> -eb6b7bd23bExport of internal Abseil changes by Abseil Team <absl-team@google.com> -9ddac555b7Export of internal Abseil changes by Abseil Team <absl-team@google.com> -1948f6f967Export of internal Abseil changes by Abseil Team <absl-team@google.com> -a0d1e098c2Export of internal Abseil changes by Abseil Team <absl-team@google.com> -2d2d7fbc28Export of internal Abseil changes by Abseil Team <absl-team@google.com> -0302d1e5fasupppress unused variable warning for gcc (#372) by Martin <pizzard@users.noreply.github.com> -262d74ba81Export of internal Abseil changes by Abseil Team <absl-team@google.com> -f0afae0d49Export of internal Abseil changes by Abseil Team <absl-team@google.com> -0e7afdcbd2Export of internal Abseil changes by Abseil Team <absl-team@google.com> -9a41ffdd3aExport of internal Abseil changes by Abseil Team <absl-team@google.com> -36910d3d7e[bazel] Add fixes for --incompatible_load_cc_rules_from_b... by Yannic <contact@yannic-bonenberger.com> -aae8143cf9Export of internal Abseil changes by Abseil Team <absl-team@google.com> -d9aa92d7fbExport of internal Abseil changes by Abseil Team <absl-team@google.com> -321ab53030Export of internal Abseil changes by Abseil Team <absl-team@google.com> -4ef574064eExport of internal Abseil changes by Abseil Team <absl-team@google.com> GitOrigin-RevId:0033c9ea91Change-Id: I8a2b70063cb3ab40c6943a6db0fe40cae71ed8d7
160 lines
6.1 KiB
C++
160 lines
6.1 KiB
C++
// Copyright 2017 The Abseil Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
// -----------------------------------------------------------------------------
|
|
// File: algorithm.h
|
|
// -----------------------------------------------------------------------------
|
|
//
|
|
// This header file contains Google extensions to the standard <algorithm> C++
|
|
// header.
|
|
|
|
#ifndef ABSL_ALGORITHM_ALGORITHM_H_
|
|
#define ABSL_ALGORITHM_ALGORITHM_H_
|
|
|
|
#include <algorithm>
|
|
#include <iterator>
|
|
#include <type_traits>
|
|
|
|
#include "absl/base/config.h"
|
|
|
|
namespace absl {
|
|
ABSL_NAMESPACE_BEGIN
|
|
|
|
namespace algorithm_internal {
|
|
|
|
// Performs comparisons with operator==, similar to C++14's `std::equal_to<>`.
|
|
struct EqualTo {
|
|
template <typename T, typename U>
|
|
bool operator()(const T& a, const U& b) const {
|
|
return a == b;
|
|
}
|
|
};
|
|
|
|
template <typename InputIter1, typename InputIter2, typename Pred>
|
|
bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
|
InputIter2 last2, Pred pred, std::input_iterator_tag,
|
|
std::input_iterator_tag) {
|
|
while (true) {
|
|
if (first1 == last1) return first2 == last2;
|
|
if (first2 == last2) return false;
|
|
if (!pred(*first1, *first2)) return false;
|
|
++first1;
|
|
++first2;
|
|
}
|
|
}
|
|
|
|
template <typename InputIter1, typename InputIter2, typename Pred>
|
|
bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
|
InputIter2 last2, Pred&& pred, std::random_access_iterator_tag,
|
|
std::random_access_iterator_tag) {
|
|
return (last1 - first1 == last2 - first2) &&
|
|
std::equal(first1, last1, first2, std::forward<Pred>(pred));
|
|
}
|
|
|
|
// When we are using our own internal predicate that just applies operator==, we
|
|
// forward to the non-predicate form of std::equal. This enables an optimization
|
|
// in libstdc++ that can result in std::memcmp being used for integer types.
|
|
template <typename InputIter1, typename InputIter2>
|
|
bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
|
InputIter2 last2, algorithm_internal::EqualTo /* unused */,
|
|
std::random_access_iterator_tag,
|
|
std::random_access_iterator_tag) {
|
|
return (last1 - first1 == last2 - first2) &&
|
|
std::equal(first1, last1, first2);
|
|
}
|
|
|
|
template <typename It>
|
|
It RotateImpl(It first, It middle, It last, std::true_type) {
|
|
return std::rotate(first, middle, last);
|
|
}
|
|
|
|
template <typename It>
|
|
It RotateImpl(It first, It middle, It last, std::false_type) {
|
|
std::rotate(first, middle, last);
|
|
return std::next(first, std::distance(middle, last));
|
|
}
|
|
|
|
} // namespace algorithm_internal
|
|
|
|
// equal()
|
|
//
|
|
// Compares the equality of two ranges specified by pairs of iterators, using
|
|
// the given predicate, returning true iff for each corresponding iterator i1
|
|
// and i2 in the first and second range respectively, pred(*i1, *i2) == true
|
|
//
|
|
// This comparison takes at most min(`last1` - `first1`, `last2` - `first2`)
|
|
// invocations of the predicate. Additionally, if InputIter1 and InputIter2 are
|
|
// both random-access iterators, and `last1` - `first1` != `last2` - `first2`,
|
|
// then the predicate is never invoked and the function returns false.
|
|
//
|
|
// This is a C++11-compatible implementation of C++14 `std::equal`. See
|
|
// https://en.cppreference.com/w/cpp/algorithm/equal for more information.
|
|
template <typename InputIter1, typename InputIter2, typename Pred>
|
|
bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
|
InputIter2 last2, Pred&& pred) {
|
|
return algorithm_internal::EqualImpl(
|
|
first1, last1, first2, last2, std::forward<Pred>(pred),
|
|
typename std::iterator_traits<InputIter1>::iterator_category{},
|
|
typename std::iterator_traits<InputIter2>::iterator_category{});
|
|
}
|
|
|
|
// Overload of equal() that performs comparison of two ranges specified by pairs
|
|
// of iterators using operator==.
|
|
template <typename InputIter1, typename InputIter2>
|
|
bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
|
InputIter2 last2) {
|
|
return absl::equal(first1, last1, first2, last2,
|
|
algorithm_internal::EqualTo{});
|
|
}
|
|
|
|
// linear_search()
|
|
//
|
|
// Performs a linear search for `value` using the iterator `first` up to
|
|
// but not including `last`, returning true if [`first`, `last`) contains an
|
|
// element equal to `value`.
|
|
//
|
|
// A linear search is of O(n) complexity which is guaranteed to make at most
|
|
// n = (`last` - `first`) comparisons. A linear search over short containers
|
|
// may be faster than a binary search, even when the container is sorted.
|
|
template <typename InputIterator, typename EqualityComparable>
|
|
bool linear_search(InputIterator first, InputIterator last,
|
|
const EqualityComparable& value) {
|
|
return std::find(first, last, value) != last;
|
|
}
|
|
|
|
// rotate()
|
|
//
|
|
// Performs a left rotation on a range of elements (`first`, `last`) such that
|
|
// `middle` is now the first element. `rotate()` returns an iterator pointing to
|
|
// the first element before rotation. This function is exactly the same as
|
|
// `std::rotate`, but fixes a bug in gcc
|
|
// <= 4.9 where `std::rotate` returns `void` instead of an iterator.
|
|
//
|
|
// The complexity of this algorithm is the same as that of `std::rotate`, but if
|
|
// `ForwardIterator` is not a random-access iterator, then `absl::rotate`
|
|
// performs an additional pass over the range to construct the return value.
|
|
template <typename ForwardIterator>
|
|
ForwardIterator rotate(ForwardIterator first, ForwardIterator middle,
|
|
ForwardIterator last) {
|
|
return algorithm_internal::RotateImpl(
|
|
first, middle, last,
|
|
std::is_same<decltype(std::rotate(first, middle, last)),
|
|
ForwardIterator>());
|
|
}
|
|
|
|
ABSL_NAMESPACE_END
|
|
} // namespace absl
|
|
|
|
#endif // ABSL_ALGORITHM_ALGORITHM_H_
|