pw_containers: FilteredView class
- The FilteredView class applies a filter to a container, allowing
iteration over only matching elements.
- Prevent copying an IntrusiveList::Item, since this would break the
intrusive list the item is in.
Change-Id: Ifefb95c421e057e746440532f0af6505fb2dc32d
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/64960
Commit-Queue: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
diff --git a/pw_containers/docs.rst b/pw_containers/docs.rst
index 51b224b..be63a98 100644
--- a/pw_containers/docs.rst
+++ b/pw_containers/docs.rst
@@ -90,9 +90,26 @@
need to be sorted. During construction, ``pw::containers::FlatMap`` will
perform a constexpr insertion sort.
+pw::containers::FilteredView
+============================
+``pw::containers::FilteredView`` provides a view of a container that only
+contains elements that match the specified filter. This class is similar to
+C++20's `std::ranges::filter_view
+<https://en.cppreference.com/w/cpp/ranges/filter_view>`_.
+
+To create a ``FilteredView``, pass a container and a filter object, which may be
+a lambda or class that implements ``operator()`` for the container's value type.
+
+.. code-block:: cpp
+
+ std::array<int, 99> kNumbers = {3, 1, 4, 1, ...};
+
+ for (int even : FilteredView(kNumbers, [](int n) { return n % 2 == 0; })) {
+ PW_LOG_INFO("This number is even: %d", even);
+ }
+
Compatibility
=============
-* C
* C++17
Dependencies