-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsplit_square_verify.cpp
More file actions
55 lines (46 loc) · 1.77 KB
/
Copy pathsplit_square_verify.cpp
File metadata and controls
55 lines (46 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "phlex/module.hpp"
#include <cassert>
#include <cstddef>
#include <utility>
#include <vector>
// Splitter for an unfold. The framework constructs one per parent cell from
// the input product (the constructor signature is inspected to deduce the
// expected number and types of input products), then iterates by threading
// an opaque state value:
//
// initial_value() -> first state
// predicate(state) -> true while more children remain
// unfold(state) -> { next state, emitted child product }
class VectorSplitter {
public:
explicit VectorSplitter(std::vector<int> values) : values_{std::move(values)} {}
std::size_t initial_value() const { return 0; }
bool predicate(std::size_t i) const { return i < values_.size(); }
std::pair<std::size_t, int> unfold(std::size_t i) const { return {i + 1, values_[i]}; }
private:
std::vector<int> values_;
};
PHLEX_REGISTER_ALGORITHMS(m, config)
{
using namespace phlex;
auto const parent_layer = config.get<std::string>("parent_layer");
auto const child_layer = config.get<std::string>("child_layer");
m.unfold<VectorSplitter>(
"split",
&VectorSplitter::predicate,
&VectorSplitter::unfold,
child_layer,
concurrency::unlimited)
.input_family(product_selector{
.creator = "input", .layer = parent_layer, .suffix = "numbers"})
.output_product_suffixes("number");
m.transform(
"square", [](int n) { return n * n; }, concurrency::unlimited)
.input_family(
product_selector{.creator = "split", .layer = child_layer, .suffix = "number"})
.output_product_suffixes("squared");
m.observe(
"verify", [](int sq) { assert(sq >= 0); }, concurrency::unlimited)
.input_family(
product_selector{.creator = "square", .layer = child_layer, .suffix = "squared"});
}