diff options
author | 2024-05-01 17:00:22 -0700 | |
---|---|---|
committer | 2024-05-01 17:29:08 -0700 | |
commit | 23550ef3ad955f435cf0d2d16d21f31ecd448dae (patch) | |
tree | 19a44d16849c01e1e490a49c552df6476b0c1c39 /include/ftl | |
parent | d50d517fb36c05ee2a877a3684fb1d3ff6e273c2 (diff) |
ftl: contains
ftl::contains(container, value) returns true if the container contains
the value. It's implemented in terms of std::find(), so has the same
runtime complexity.
It is otherwise a simplified version of the C++23 std::ranges::contains,
which has some additional options, and can be deprecated once C++23 is
the minimal supported version.
Test: atest ftl_test
Bug: 185536303
Change-Id: I98aefe7cf6645ac3a20fddfe0657fa6822d669de
Diffstat (limited to 'include/ftl')
-rw-r--r-- | include/ftl/algorithm.h | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/include/ftl/algorithm.h b/include/ftl/algorithm.h index c0f67683ab..68826bb068 100644 --- a/include/ftl/algorithm.h +++ b/include/ftl/algorithm.h @@ -24,6 +24,18 @@ namespace android::ftl { +// Determines if a container contains a value. This is a simplified version of the C++23 +// std::ranges::contains function. +// +// const ftl::StaticVector vector = {1, 2, 3}; +// assert(ftl::contains(vector, 1)); +// +// TODO: Remove in C++23. +template <typename Container, typename Value> +auto contains(const Container& container, const Value& value) -> bool { + return std::find(container.begin(), container.end(), value) != container.end(); +} + // Adapter for std::find_if that converts the return value from iterator to optional. // // const ftl::StaticVector vector = {"upside"sv, "down"sv, "cake"sv}; |