Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ReduceNode.hpp
1#pragma once
2#include <Dataflow/Core/Node.hpp>
3
4#include <functional>
5
6namespace Ra {
7namespace Dataflow {
8namespace Core {
9namespace Functionals {
10
29// TODO, allow to specify the type of the reduced information. This will allow, e.g, given a
30// collection of X, to compute a tuple containing mean and standard deviation of the collection
31// as reduction (using online Welford algo).
32template <typename coll_t, typename v_t = typename coll_t::value_type>
33class ReduceNode : public Node
34{
35 public:
39 using ReduceOperator = std::function<v_t( const v_t&, const v_t& )>;
40
45 explicit ReduceNode( const std::string& instanceName );
46
53 ReduceNode( const std::string& instanceName, ReduceOperator op, v_t initialValue = v_t {} );
54
55 void init() override;
56 bool execute() override;
57
59 void set_operator( ReduceOperator op, v_t initialValue = v_t {} );
60
61 protected:
62 ReduceNode( const std::string& instanceName,
63 const std::string& typeName,
65 v_t initialValue );
66
67 void toJsonInternal( nlohmann::json& data ) const override { Node::toJsonInternal( data ); }
68 bool fromJsonInternal( const nlohmann::json& data ) override {
69 return Node::fromJsonInternal( data );
70 }
71
72 private:
73 RA_NODE_PORT_IN( v_t, init );
74 RA_NODE_PORT_IN( coll_t, data );
75 RA_NODE_PORT_IN( ReduceOperator, op );
76 RA_NODE_PORT_OUT_WITH_DATA( v_t, result );
77
78 public:
79 static const std::string& node_typename();
80};
81
82// -----------------------------------------------------------------
83// ---------------------- inline methods ---------------------------
84
85template <typename coll_t, typename v_t>
88 instanceName,
89 node_typename(),
90 []( const v_t& a, const v_t& ) -> v_t { return a; },
91 v_t {} ) {}
92
93template <typename coll_t, typename v_t>
96 v_t initialValue ) :
97 ReduceNode( instanceName, node_typename(), op, initialValue ) {}
98
99template <typename coll_t, typename v_t>
101 m_port_in_op->set_default_value( op );
102 m_port_in_init->set_default_value( initialValue );
103}
104
105template <typename coll_t, typename v_t>
107 Node::init();
108 m_result = m_port_in_init->data();
109}
110
111template <typename coll_t, typename v_t>
113 const auto& f = m_port_in_op->data();
114 const auto& in = m_port_in_data->data();
115
116 m_result = std::accumulate( in.begin(), in.end(), m_port_in_init->data(), f );
117
118 return true;
119}
120
121template <typename coll_t, typename v_t>
123 static std::string demangledName =
124 std::string { "Reduce<" } + Ra::Core::Utils::simplifiedDemangledType<coll_t>() + ">";
125 return demangledName;
126}
127
128template <typename coll_t, typename v_t>
130 const std::string& typeName,
131 ReduceOperator op,
132 v_t initialValue ) :
133 Node( instanceName, typeName ) {
134 m_port_in_op->set_default_value( op );
135 m_port_in_init->set_default_value( initialValue );
136}
137
138} // namespace Functionals
139} // namespace Core
140} // namespace Dataflow
141} // namespace Ra
T accumulate(T... args)
Reduce an iterable collection using a given operator.
void init() override
Initializes the node content.
bool execute() override
Executes the node.
void set_operator(ReduceOperator op, v_t initialValue=v_t {})
Sets the operator on the node.
bool fromJsonInternal(const nlohmann::json &data) override
Internal json representation of the Node.
ReduceNode(const std::string &instanceName)
Construct an identity transformer.
std::function< v_t(const v_t &, const v_t &)> ReduceOperator
void toJsonInternal(nlohmann::json &data) const override
Internal json representation of the Node.
Base abstract class for all the nodes added and used by the node system.
Definition Node.hpp:40
virtual void init()
Initializes the node content.
Definition Node.hpp:428
virtual bool fromJsonInternal(const nlohmann::json &data)
Internal json representation of the Node.
Definition Node.cpp:91
virtual void toJsonInternal(nlohmann::json &data) const
Internal json representation of the Node.
Definition Node.cpp:109
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4