aboutsummaryrefslogtreecommitdiff
path: root/pw_containers/docs.rst
diff options
context:
space:
mode:
Diffstat (limited to 'pw_containers/docs.rst')
-rw-r--r--pw_containers/docs.rst139
1 files changed, 86 insertions, 53 deletions
diff --git a/pw_containers/docs.rst b/pw_containers/docs.rst
index 0af2eda2c..83f333402 100644
--- a/pw_containers/docs.rst
+++ b/pw_containers/docs.rst
@@ -1,12 +1,13 @@
.. _module-pw_containers:
--------------
+=============
pw_containers
--------------
+=============
The ``pw_containers`` module provides embedded-friendly container classes.
+----------
pw::Vector
-==========
+----------
The Vector class is similar to ``std::vector``, except it is backed by a
fixed-size buffer. Vectors must be declared with an explicit maximum size
(e.g. ``Vector<int, 10>``) but vectors can be used and referred to without the
@@ -18,8 +19,37 @@ size in a variable. This allows Vectors to be used without having to know
their maximum size at compile time. It also keeps code size small since
function implementations are shared for all maximum sizes.
+---------------
+pw::InlineDeque
+---------------
+.. doxygentypedef:: pw::InlineDeque
+
+---------------
+pw::InlineQueue
+---------------
+.. doxygentypedef:: pw::InlineQueue
+
+----------------------------
+pw::VariableLengthEntryQueue
+----------------------------
+.. doxygenfile:: pw_containers/variable_length_entry_queue.h
+ :sections: detaileddescription
+
+API Reference
+===============
+C
+-
+.. doxygengroup:: variable_length_entry_queue_c_api
+ :content-only:
+
+Python
+------
+.. automodule:: pw_containers.variable_length_entry_queue
+ :members:
+
+-----------------
pw::IntrusiveList
-=================
+-----------------
IntrusiveList provides an embedded-friendly singly-linked intrusive list
implementation. An intrusive list is a type of linked list that embeds the
"next" pointer into the list object itself. This allows the construction of a
@@ -33,7 +63,7 @@ pointer from being accessed by the item class, so only the ``pw::IntrusiveList``
class can modify the list.
Usage
------
+=====
While the API of ``pw::IntrusiveList`` is similar to a ``std::forward_list``,
there are extra steps to creating objects that can be stored in this data
structure. Objects that will be added to a ``IntrusiveList<T>`` must inherit
@@ -104,35 +134,53 @@ That means two key things:
}
}
+Performance Considerations
+==========================
+Items only include pointers to the next item. To reach previous items, the list
+maintains a cycle of items so that the first item can be reached from the last.
+This structure means certain operations have linear complexity in terms of the
+number of items in the list, i.e. they are "O(n)":
+
+- Adding to the end of a list with ``pw::IntrusiveList<T>::push_back(T&)``.
+- Accessing the last item in a list with ``pw::IntrusiveList<T>::back()``.
+- Destroying an item with ``pw::IntrusiveList<T>::Item::~Item()``.
+- Moving an item with either ``pw::IntrusiveList<T>::Item::Item(Item&&)`` or
+ ``pw::IntrusiveList<T>::Item::operator=(Item&&)``.
+- Removing an item from a list using ``pw::IntrusiveList<T>::remove(const T&)``.
+- Getting the list size using ``pw::IntrusiveList<T>::size()``.
+
+When using a ``pw::IntrusiveList<T>`` in a performance critical section or with
+many items, authors should prefer to avoid these methods. For example, it may be
+preferrable to create items that together with their storage outlive the list.
+
+Notably, ``pw::IntrusiveList<T>::end()`` is constant complexity (i.e. "O(1)").
+As a result iterating over a list does not incur an additional penalty.
+
+-----------------------
pw::containers::FlatMap
-=======================
-FlatMap provides a simple, fixed-size associative array with lookup by key or
-value. ``pw::containers::FlatMap`` contains the same methods and features for
-looking up data as std::map. However, there are no methods that modify the
-underlying data. The underlying array in ``pw::containers::FlatMap`` does not
-need to be sorted. During construction, ``pw::containers::FlatMap`` will
-perform a constexpr insertion sort.
-
+-----------------------
+``FlatMap`` provides a simple, fixed-size associative array with `O`\ (log `n`)
+lookup by key.
+
+``pw::containers::FlatMap`` contains the same methods and features for looking
+up data as ``std::map``. However, modification of the underlying data is limited
+to the mapped values, via ``.at()`` (key must exist) and ``mapped_iterator``
+objects returned by ``.mapped_begin()`` and ``.mapped_end()``.
+``mapped_iterator`` objects are bidirectional iterators that can be dereferenced
+to access and mutate the mapped value objects.
+
+The underlying array in ``pw::containers::FlatMap`` does not 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);
- }
+----------------------------
+.. doxygenclass:: pw::containers::FilteredView
+-------------------------------
pw::containers::WrappedIterator
-===============================
+-------------------------------
``pw::containers::WrappedIterator`` is a class that makes it easy to wrap an
existing iterator type. It reduces boilerplate by providing ``operator++``,
``operator--``, ``operator==``, ``operator!=``, and the standard iterator
@@ -181,15 +229,17 @@ in Java 8. ``WrappedIterator`` and ``FilteredView`` require no memory
allocation, which is helpful when memory is too constrained to process the items
into a new container.
+------------------------
pw::containers::to_array
-========================
+------------------------
``pw::containers::to_array`` is a C++14-compatible implementation of C++20's
`std::to_array <https://en.cppreference.com/w/cpp/container/array/to_array>`_.
In C++20, it is an alias for ``std::to_array``. It converts a C array to a
``std::array``.
+-------------------------
pw_containers/algorithm.h
-=========================
+-------------------------
Pigweed provides a set of Container-based versions of algorithmic functions
within the C++ standard library, based on a subset of
``absl/algorithm/container.h``.
@@ -199,74 +249,62 @@ within the C++ standard library, based on a subset of
Container-based version of the <algorithm> ``std::all_of()`` function to
test if all elements within a container satisfy a condition.
-
.. cpp:function:: bool pw::containers::AnyOf()
Container-based version of the <algorithm> ``std::any_of()`` function to
test if any element in a container fulfills a condition.
-
.. cpp:function:: bool pw::containers::NoneOf()
Container-based version of the <algorithm> ``std::none_of()`` function to
test if no elements in a container fulfill a condition.
-
.. cpp:function:: pw::containers::ForEach()
Container-based version of the <algorithm> ``std::for_each()`` function to
apply a function to a container's elements.
-
.. cpp:function:: pw::containers::Find()
Container-based version of the <algorithm> ``std::find()`` function to find
the first element containing the passed value within a container value.
-
.. cpp:function:: pw::containers::FindIf()
Container-based version of the <algorithm> ``std::find_if()`` function to find
the first element in a container matching the given condition.
-
.. cpp:function:: pw::containers::FindIfNot()
Container-based version of the <algorithm> ``std::find_if_not()`` function to
find the first element in a container not matching the given condition.
-
.. cpp:function:: pw::containers::FindEnd()
Container-based version of the <algorithm> ``std::find_end()`` function to
find the last subsequence within a container.
-
.. cpp:function:: pw::containers::FindFirstOf()
Container-based version of the <algorithm> ``std::find_first_of()`` function
to find the first element within the container that is also within the options
container.
-
.. cpp:function:: pw::containers::AdjacentFind()
Container-based version of the <algorithm> ``std::adjacent_find()`` function
to find equal adjacent elements within a container.
-
.. cpp:function:: pw::containers::Count()
Container-based version of the <algorithm> ``std::count()`` function to count
values that match within a container.
-
.. cpp:function:: pw::containers::CountIf()
Container-based version of the <algorithm> ``std::count_if()`` function to
count values matching a condition within a container.
-
.. cpp:function:: pw::containers::Mismatch()
Container-based version of the <algorithm> ``std::mismatch()`` function to
@@ -276,7 +314,6 @@ within the C++ standard library, based on a subset of
``pred`` to the first N elements of ``c1`` and ``c2``, where
``N = min(size(c1), size(c2))``.
-
.. cpp:function:: bool pw::containers::Equal()
Container-based version of the <algorithm> ``std::equal()`` function to
@@ -295,27 +332,23 @@ within the C++ standard library, based on a subset of
Container-based version of the <algorithm> ``std::is_permutation()`` function
to test whether a container is a permutation of another.
-
.. cpp:function:: pw::containers::Search()
Container-based version of the <algorithm> ``std::search()`` function to
search a container for a subsequence.
-
.. cpp:function:: pw::containers::SearchN()
Container-based version of the <algorithm> ``std::search_n()`` function to
search a container for the first sequence of N elements.
+-------------
Compatibility
-=============
+-------------
- C++17
-Dependencies
-============
-- :bdg-ref-primary-line:`module-pw_span`
-
+------
Zephyr
-======
+------
To enable ``pw_containers`` for Zephyr add ``CONFIG_PIGWEED_CONTAINERS=y`` to
the project's configuration.