5#include <unordered_map>
16bool almost_equal(
double a,
double b,
double eps = 1e-12) {
17 return std::abs(a - b) < eps;
20void check(
BlockAccessor& ba,
const std::string& block,
const LhaID&
id,
double expected) {
21 const double got = ba.
getValue(block,
id);
22 std::cout << block <<
"[" <<
id <<
"] = " << got <<
" (expected " << expected <<
")\n";
23 assert(almost_equal(got, expected));
26void banner(
const std::string& title) {
27 std::cout <<
"\n==============================\n";
28 std::cout << title <<
"\n";
29 std::cout <<
"==============================\n";
33 auto blk = std::make_shared<Block>();
34 blk->blockname =
name;
43void add_dependent_block_local(
46 std::unordered_map<std::string, std::shared_ptr<Block>> sources,
49 auto dep = std::make_shared<DependentBlock>(sources, std::move(recalculateFunc));
50 dep->blockname =
name;
56void add_dependent_parameter_local(
59 std::unordered_map<
ParamId, std::shared_ptr<Parameter>> sources,
63 add_leaf_block(ba, pid.
block);
68 if (blk->contains(pid.
code)) {
69 auto existing = blk->retrieve(pid.
code);
70 if (
auto dep = std::dynamic_pointer_cast<DependentParameter>(existing)) {
71 dep->rebind(std::move(sources), std::move(recalculateFunc));
77 auto dep = std::make_shared<DependentParameter>(pid, std::move(sources), std::move(recalculateFunc));
78 blk->store(pid.
code, dep);
96 add_leaf_block(ba,
"BASE");
97 set_typed_leaf(ba,
"BASE", A, 10.0, PT::SM);
104 add_dependent_block_local(
108 {
"BASE", ba.
at(
"BASE")}
110 [](
const BlockSrc& src, std::shared_ptr<DependentBlock> self) {
111 const double a = src.
get_val(
"BASE", 1);
112 const double c = 2.0 * a;
114 auto out = std::make_shared<Parameter>(
118 self->store_or_assign(
LhaID{1}, out);
131 ParamId src_id(PT::SM,
"DB1", C);
132 ParamId dst_id(PT::SM,
"PARAMS", P);
134 add_dependent_parameter_local(
140 [src_id](
const ParamSrc& src, std::shared_ptr<DependentParameter> self) {
141 const double c = src.
get_val(src_id);
142 self->set_expected_silent(c + 5.0);
152 add_dependent_block_local(
156 {
"DB1", ba.
at(
"DB1")},
157 {
"PARAMS", ba.
at(
"PARAMS")}
159 [](
const BlockSrc& src, std::shared_ptr<DependentBlock> self) {
160 const double c = src.
get_val(
"DB1", 1);
161 const double p = src.
get_val(
"PARAMS", 1);
162 const double z = c + p;
164 auto out = std::make_shared<Parameter>(
168 self->store_or_assign(
LhaID{1}, out);
179 banner(
"Etat initial");
180 check(ba,
"BASE", A, 10.0);
181 check(ba,
"DB1", C, 20.0);
182 check(ba,
"PARAMS", P, 25.0);
183 check(ba,
"DB2", Z, 45.0);
191 banner(
"Apres BASE[a] = 20");
193 check(ba,
"BASE", A, 20.0);
194 check(ba,
"DB1", C, 40.0);
195 check(ba,
"PARAMS", P, 45.0);
196 check(ba,
"DB2", Z, 85.0);
204 banner(
"Detach PARAMS[p], puis BASE[a] = 7");
207 check(ba,
"BASE", A, 7.0);
208 check(ba,
"DB1", C, 14.0);
209 check(ba,
"PARAMS", P, 45.0);
210 check(ba,
"DB2", Z, 59.0);
218 banner(
"Reattach PARAMS[p]");
220 check(ba,
"BASE", A, 7.0);
221 check(ba,
"DB1", C, 14.0);
222 check(ba,
"PARAMS", P, 19.0);
223 check(ba,
"DB2", Z, 33.0);
231 banner(
"Detach DB1, puis BASE[a] = 100");
234 check(ba,
"BASE", A, 100.0);
235 check(ba,
"DB1", C, 14.0);
236 check(ba,
"PARAMS", P, 19.0);
237 check(ba,
"DB2", Z, 33.0);
245 banner(
"Reattach DB1");
247 check(ba,
"BASE", A, 100.0);
248 check(ba,
"DB1", C, 200.0);
249 check(ba,
"PARAMS", P, 205.0);
250 check(ba,
"DB2", Z, 405.0);
252 std::cout <<
"\nTous les checks sont passes.\n";
Alias-aware façade for accessing and manipulating multiple parameter blocks.
Defines classes used to store parameters and to build derived/dependent parameter blocks.
std::function< void(const BlockSrc &, std::shared_ptr< DependentBlock >)> DepUpdateFunc
Function type used to recompute a DependentBlock from its sources.
Defines parameters whose values are lazily computed from other parameters.
std::function< void(const ParamSrc &, std::shared_ptr< DependentParameter >)> DepParamUpdateFunc
Function signature used to recompute a DependentParameter.
Alias-aware container / façade over several parameter blocks.
std::shared_ptr< Block > & at(const BlockName &block_name)
Alias-aware mutable access to a block.
void reattach_block(const BlockName &block_name)
Reattaches a previously detached dependent block to its saved sources.
bool contains(const BlockName &block_name) const
Checks whether a block exists (alias-aware).
scalar_t getValue(const BlockName &blockName, LhaID pdgCode) const
Retrieves the current value of a parameter from a block.
void detach_parameter(const BlockName &block_name, LhaID id)
Detaches a dependent parameter from its upstream dependencies.
void reattach_parameter(const BlockName &block_name, LhaID id)
Reattaches a previously detached dependent parameter.
void emplace(const BlockName &name, std::shared_ptr< Block > blk)
Inserts or replaces a block, updating alias metadata.
void detach_block(const BlockName &block_name)
Detaches a dependent block from its upstream dependencies.
void setValue(const BlockName &blockName, LhaID pdgCode, scalar_t value)
Sets the value of a parameter inside an existing block.
std::shared_ptr< Parameter > getParameter(const BlockName &blockName, LhaID id) const
Retrieves the full Parameter object stored in a block.
Block identifier with alias support.
Lightweight view over a set of source blocks.
scalar_t get_val(std::string_view blk, std::initializer_list< long > code) const
Retrieves the value of a parameter given as an initializer_list.
Lightweight view over a set of source parameters keyed by ParamId.
scalar_t get_val(const ParamId &id) const
Retrieves the current value of a parameter.
Represents an identifier of a LHA element, possibly containing several sub-ids.
Composite identifier for a single parameter.
BlockName block
Name of the block where the parameter is stored.
LhaID code
Index or multi-index of the parameter inside the block.