From 63d4b2fe1e0d4bd6a7b916f398643db40c35624b Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 26 Jun 2024 14:45:44 -0700 Subject: Add `c_contains()` and `c_contains_subrange()` to `absl/algorithm/container.h`. These functions are based on the C++23's `std::ranges::contains()` and `std::ranges::contains_subrange()` functions, see: https://en.cppreference.com/w/cpp/algorithm/ranges/contains PiperOrigin-RevId: 647084955 Change-Id: If5a01784e3cf1cc4d88e7f2fef92a3701fafc886 --- absl/algorithm/container.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'absl/algorithm/container.h') diff --git a/absl/algorithm/container.h b/absl/algorithm/container.h index a2d126b7..5a025e46 100644 --- a/absl/algorithm/container.h +++ b/absl/algorithm/container.h @@ -211,6 +211,16 @@ container_algorithm_internal::ContainerIter c_find(C& c, T&& value) { std::forward(value)); } +// c_contains() +// +// Container-based version of the `std::ranges::contains()` C++23 +// function to search a container for a value. +template +bool c_contains(const Sequence& sequence, T&& value) { + return absl::c_find(sequence, std::forward(value)) != + container_algorithm_internal::c_end(sequence); +} + // c_find_if() // // Container-based version of the `std::find_if()` function to find @@ -427,6 +437,26 @@ container_algorithm_internal::ContainerIter c_search( std::forward(pred)); } +// c_contains_subrange() +// +// Container-based version of the `std::ranges::contains_subrange()` +// C++23 function to search a container for a subsequence. +template +bool c_contains_subrange(Sequence1& sequence, Sequence2& subsequence) { + return absl::c_search(sequence, subsequence) != + container_algorithm_internal::c_end(sequence); +} + +// Overload of c_contains_subrange() for using a predicate evaluation other than +// `==` as the function's test condition. +template +bool c_contains_subrange(Sequence1& sequence, Sequence2& subsequence, + BinaryPredicate&& pred) { + return absl::c_search(sequence, subsequence, + std::forward(pred)) != + container_algorithm_internal::c_end(sequence); +} + // c_search_n() // // Container-based version of the `std::search_n()` function to -- cgit v1.2.3