From 0963ce521c7b3f1376817f827ae3a1f5c13d8af9 Mon Sep 17 00:00:00 2001 From: matclim Date: Tue, 20 Oct 2026 17:48:56 +0200 Subject: [PATCH 1/2] docs: document unfolds The unfold contract is only visible in the implementation and tests. Add a user-guide page that explains: - the unfolder class, initial_value(), and the predicate/unfold loop, including the accepted callable forms and the optional child data_cell_index argument - per-parent calls, child numbering, empty parents, and why there is no way to skip a candidate - that all input selectors must name the same layer (rejected since v0.4.3, see issue #955) and the transform-then-unfold workaround - where resource{} arguments go (issue #946) Also add a Doxygen description of graph_proxy::unfold, and point api/graph_proxy.rst at phlex::detail::graph_proxy, where the class actually lives, so its documentation is rendered. Assisted by: Claude Haiku 5.5 --- docs/source/guide/unfolds.rst | 137 ++++++++++++++++++++++++++++++++++ phlex/core/graph_proxy.hpp | 19 ++++- 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 docs/source/guide/unfolds.rst diff --git a/docs/source/guide/unfolds.rst b/docs/source/guide/unfolds.rst new file mode 100644 index 000000000..de2bd4dcb --- /dev/null +++ b/docs/source/guide/unfolds.rst @@ -0,0 +1,137 @@ +Unfolds +======= + +An *unfold* splits one data cell into child data cells in a new, lower +data layer: for example, a spill into the events it contains, or an event +into track seeds. +Each child gets one product, which downstream algorithms select in the +child layer. + +Registering an unfold +--------------------- + +An unfold is described by a class (the *unfolder*) and two functions: + +.. code-block:: cpp + + class VectorSplitter { + public: + explicit VectorSplitter(std::vector const& values) : values_{values} {} + + std::size_t initial_value() const { return 0; } + bool predicate(std::size_t i) const { return i < values_.size(); } + std::pair unfold(std::size_t i) const { return {i + 1, values_[i]}; } + + private: + std::vector const& values_; + }; + + PHLEX_REGISTER_ALGORITHMS(m) + { + using namespace phlex; + + m.unfold("split", + &VectorSplitter::predicate, + &VectorSplitter::unfold, + "number", // child layer + concurrency::unlimited) + .input_family(product_selector{.creator = "input", .layer = "spill", .suffix = "numbers"}) + .output_product_suffixes("number"); + + m.transform("square", [](int n) { return n * n; }, concurrency::unlimited) + .input_family(product_selector{.creator = "split", .layer = "number", .suffix = "number"}) + .output_product_suffixes("squared"); + } + +For each parent data cell, Phlex: + +#. constructs a ``VectorSplitter`` from the products selected by + ``input_family(...)``, passed to the constructor in order; +#. sets a running value to ``initial_value()``; +#. while ``predicate(value)`` returns ``true``, calls ``unfold(value)``, + which returns the next running value and the product for a new child + data cell. + +Phlex deduces the unfolder's inputs from its constructor parameters, one +per selector. +Give the unfolder exactly one constructor: if it also has a default or a +shorter constructor, Phlex picks that one, and registration fails to +compile. + +The predicate and unfold functions may be member-function pointers of the +unfolder, or callables that take the unfolder as their first argument, or +callables that take only the running value. +The unfold function may also take the new child's ``data_cell_index`` +after the running value. + +The child layer does not need to be declared in the driver configuration; +it is created by the unfold. +Its products have the unfold's name as creator and the suffix given to +``output_product_suffixes(...)``. + +Behaviour to be aware of +------------------------ + +One call per parent + The whole loop for one parent runs inside a single call of the node, and + the ``concurrency`` argument limits how many parents are unfolded at the + same time. + Children are passed downstream as they are created, so downstream + algorithms can start before the loop finishes. + +Children are numbered from 0 + Child data cells are numbered 0, 1, 2, ... separately for each parent. + +No children is allowed + If the predicate returns ``false`` for the initial value, the parent has + no children, and child-layer algorithms are not called for it. + +No skipping + Every successful predicate call creates a child. + To leave out candidates, advance the running value past them inside the + unfolder (for example, store "index of the next accepted candidate" as + the running value), rather than creating a child and discarding it later. + +Products may refer to the inputs + The unfolder is constructed and used within one call, while its input + products are alive, so it may keep references to them, as above. + The child products themselves must not refer to the unfolder. + +Input layers +------------ + +Every input selector of an unfold must name a data layer, and all of them +must name the *same* layer. +Since Phlex v0.4.3, an unfold whose inputs come from different layers (for +example, a spill-layer product and a run-layer product) is rejected with an +``std::invalid_argument`` exception, ``Unfolds cannot yet accept multi-layer +inputs``, before any data is processed. +Earlier versions accepted such unfolds but could leave the higher layer +unflushed, so that folds over it never produced results +(`issue #955 `_). +Support is planned for Phlex v0.5. + +Until then, combine the products in a transform on the parent layer and +unfold its single output: + +.. code-block:: cpp + + // spill-layer transform that bundles what the unfolder needs + m.transform("prepare", prepare, concurrency::unlimited) + .input_family(product_selector{.creator = "hits", .layer = "spill"}, + product_selector{.creator = "geometry", .layer = "job"}) + .output_product_suffixes("context"); + + m.unfold("seeds", &SeedFinder::more, &SeedFinder::next, "seed") + .input_family(product_selector{.creator = "prepare", .layer = "spill", .suffix = "context"}); + +Algorithms downstream of an unfold may combine child-layer products with +products from the parent or higher layers. + +Resources +--------- + +``resource{}`` arguments given after the selectors in +``input_family(...)`` are passed to the unfold function after its other +arguments, not to the unfolder's constructor +(`issue #946 `_). diff --git a/phlex/core/graph_proxy.hpp b/phlex/core/graph_proxy.hpp index 4f8ad5917..bf3834f0b 100644 --- a/phlex/core/graph_proxy.hpp +++ b/phlex/core/graph_proxy.hpp @@ -97,7 +97,24 @@ namespace phlex::detail { is_transform_like auto f, // NOLINT(performance-unnecessary-value-param) concurrency c = concurrency::serial) const; - /// @brief Registers an unfold node. + /// @brief Registers an unfold node, which creates child data cells from one parent. + /// + /// For each parent data cell, an @p Unfolder is constructed from the products selected + /// by @c input_family(...), in the order of its constructor parameters. Starting from + /// @c Unfolder::initial_value(), the node then repeatedly calls @p pred with the current + /// value and, while it returns @c true, calls @p unf, which returns a @c std::pair of the + /// next value and the product for a new child data cell. Each call of @p pred and + /// @p unf may take the @p Unfolder as its first argument (for example, member-function + /// pointers). @p unf may also take the child's @c data_cell_index after the value. + /// + /// Children are numbered from 0 for each parent and placed in @p destination_data_layer. + /// A parent may have no children. All input selectors must name the same data layer. + /// + /// @param name Algorithm name. + /// @param pred Returns whether to create another child. + /// @param unf Creates the next child's product. + /// @param destination_data_layer Name of the child data layer. + /// @param c Maximum number of parents unfolded at the same time. template auto unfold(std::string_view name, is_predicate_like auto pred, From 312dc5948ff5c9c822e1ad292fec4faf12b738d0 Mon Sep 17 00:00:00 2001 From: matclim Date: Thu, 12 Nov 2026 15:16:55 +0100 Subject: [PATCH 2/2] docs(unfolds): use generator terminology and clarify what an unfold does Address review comments: - Describe an unfold as generating a family of child data cells and populating them with products created from the parent's inputs; the parent data cell is not modified. - Use the design document's term "generator" for the callable that creates each child's product, instead of "unfold function", in the guide and in the graph_proxy::unfold Doxygen comment. Rename the example's member function from unfold to generate to match. - State that each child currently receives exactly one data product: the design allows several, but the generator's result is unpacked as a (next value, product) pair, so returning a tuple of products is not supported yet. --- docs/source/guide/unfolds.rst | 43 ++++++++++++++++++++--------------- phlex/core/graph_proxy.hpp | 13 ++++++----- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/docs/source/guide/unfolds.rst b/docs/source/guide/unfolds.rst index de2bd4dcb..c4c531227 100644 --- a/docs/source/guide/unfolds.rst +++ b/docs/source/guide/unfolds.rst @@ -1,16 +1,23 @@ Unfolds ======= -An *unfold* splits one data cell into child data cells in a new, lower -data layer: for example, a spill into the events it contains, or an event -into track seeds. -Each child gets one product, which downstream algorithms select in the -child layer. +An *unfold* generates a family of child data cells for a parent data cell, +in a new, lower data layer, and populates each child with a data product +created from the parent's input products: for example, the events contained +in a spill, or the track seeds found in an event. +The parent data cell is not modified. +Downstream algorithms select the children's products in the child layer. + +In the current implementation each child receives exactly one data product. +The design allows a generator to create several products per child, but +returning a tuple of products is not supported yet. Registering an unfold --------------------- -An unfold is described by a class (the *unfolder*) and two functions: +An unfold is described by a class (the *unfolder*) and two operators: a +*predicate*, which says whether to create another child, and a *generator*, +which creates it: .. code-block:: cpp @@ -20,7 +27,7 @@ An unfold is described by a class (the *unfolder*) and two functions: std::size_t initial_value() const { return 0; } bool predicate(std::size_t i) const { return i < values_.size(); } - std::pair unfold(std::size_t i) const { return {i + 1, values_[i]}; } + std::pair generate(std::size_t i) const { return {i + 1, values_[i]}; } private: std::vector const& values_; @@ -32,7 +39,7 @@ An unfold is described by a class (the *unfolder*) and two functions: m.unfold("split", &VectorSplitter::predicate, - &VectorSplitter::unfold, + &VectorSplitter::generate, "number", // child layer concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "spill", .suffix = "numbers"}) @@ -48,9 +55,9 @@ For each parent data cell, Phlex: #. constructs a ``VectorSplitter`` from the products selected by ``input_family(...)``, passed to the constructor in order; #. sets a running value to ``initial_value()``; -#. while ``predicate(value)`` returns ``true``, calls ``unfold(value)``, - which returns the next running value and the product for a new child - data cell. +#. while the predicate returns ``true`` for the running value, calls the + generator, ``generate(value)``, which returns the next running value and + the product for a new child data cell. Phlex deduces the unfolder's inputs from its constructor parameters, one per selector. @@ -58,11 +65,11 @@ Give the unfolder exactly one constructor: if it also has a default or a shorter constructor, Phlex picks that one, and registration fails to compile. -The predicate and unfold functions may be member-function pointers of the +The predicate and the generator may be member-function pointers of the unfolder, or callables that take the unfolder as their first argument, or callables that take only the running value. -The unfold function may also take the new child's ``data_cell_index`` -after the running value. +The generator may also take the new child's ``data_cell_index`` after the +running value. The child layer does not need to be declared in the driver configuration; it is created by the unfold. @@ -88,9 +95,9 @@ No children is allowed No skipping Every successful predicate call creates a child. - To leave out candidates, advance the running value past them inside the - unfolder (for example, store "index of the next accepted candidate" as - the running value), rather than creating a child and discarding it later. + To leave out candidates, have the generator advance the running value past + them (for example, store "index of the next accepted candidate" as the + running value), rather than creating a child and discarding it later. Products may refer to the inputs The unfolder is constructed and used within one call, while its input @@ -132,6 +139,6 @@ Resources --------- ``resource{}`` arguments given after the selectors in -``input_family(...)`` are passed to the unfold function after its other +``input_family(...)`` are passed to the generator after its other arguments, not to the unfolder's constructor (`issue #946 `_). diff --git a/phlex/core/graph_proxy.hpp b/phlex/core/graph_proxy.hpp index bf3834f0b..56794d59f 100644 --- a/phlex/core/graph_proxy.hpp +++ b/phlex/core/graph_proxy.hpp @@ -102,17 +102,18 @@ namespace phlex::detail { /// For each parent data cell, an @p Unfolder is constructed from the products selected /// by @c input_family(...), in the order of its constructor parameters. Starting from /// @c Unfolder::initial_value(), the node then repeatedly calls @p pred with the current - /// value and, while it returns @c true, calls @p unf, which returns a @c std::pair of the - /// next value and the product for a new child data cell. Each call of @p pred and - /// @p unf may take the @p Unfolder as its first argument (for example, member-function - /// pointers). @p unf may also take the child's @c data_cell_index after the value. + /// value and, while it returns @c true, calls the generator @p unf, which returns a + /// @c std::pair of the next value and the product for a new child data cell. Each call of + /// @p pred and @p unf may take the @p Unfolder as its first argument (for example, + /// member-function pointers). @p unf may also take the child's @c data_cell_index after + /// the value. /// /// Children are numbered from 0 for each parent and placed in @p destination_data_layer. /// A parent may have no children. All input selectors must name the same data layer. /// /// @param name Algorithm name. - /// @param pred Returns whether to create another child. - /// @param unf Creates the next child's product. + /// @param pred Predicate: returns whether to create another child. + /// @param unf Generator: creates the next child's product. /// @param destination_data_layer Name of the child data layer. /// @param c Maximum number of parents unfolded at the same time. template