1#ifndef BLOCK_ACCESSOR_H
2#define BLOCK_ACCESSOR_H
5#include <unordered_map>
61class BlockAccessor :
public std::unordered_map<std::string, std::shared_ptr<Block>> {
65 using base_t = std::unordered_map<std::string, std::shared_ptr<Block>>;
68 using base_t::operator[];
254 std::unordered_set<ParamId>
337 std::shared_ptr<Block>&
at(
const BlockName& block_name);
347 const std::shared_ptr<Block>&
at(
const BlockName& block_name)
const;
364 friend std::shared_ptr<BlockAccessor>
operator+(std::shared_ptr<BlockAccessor> lhs, std::shared_ptr<BlockAccessor> rhs);
383 friend std::shared_ptr<BlockAccessor>
operator>>(std::shared_ptr<BlockAccessor> lhs, std::shared_ptr<BlockAccessor> rhs);
396 std::shared_ptr<BlockAccessor>
operator[](std::unordered_set<BlockName> block_names);
416 friend std::ostream&
operator<<(std::ostream&, std::shared_ptr<BlockAccessor>);
530 std::shared_ptr<Block>&
at(
const std::string& name) {
return at(std::string_view{name}); }
532 const std::shared_ptr<Block>&
at(
const std::string& name)
const {
return at(std::string_view{name}); }
534 std::shared_ptr<Block>&
at(
const char* name) {
return at(std::string_view{name}); }
536 const std::shared_ptr<Block>&
at(
const char* name)
const {
return at(std::string_view{name}); }
578 bool has_scale(std::string_view block)
const;
581 double get_scale(std::string_view block)
const;
584 bool contains(std::string_view name)
const;
587 void emplace(std::string_view name, std::shared_ptr<Block> blk) {
592 std::shared_ptr<Block>&
at(std::string_view name);
595 const std::shared_ptr<Block>&
at(std::string_view name)
const;
617 static std::unordered_set<std::string> norm_aliases(
const BlockName& n);
624 static std::string choose_key(
const std::unordered_set<std::string>& aliases_norm);
634 static std::string normalize(std::string_view s);
642 std::string key_for(std::string_view alias)
const;
652 std::string key_for(
const BlockName& name)
const;
660 std::string key_for(
const std::string& name)
const;
665 std::string resolve_key(std::string_view name)
const;
670 std::string resolve_key(
const BlockName& name)
const;
681 void merge_name_into_key(
const std::string& key,
const BlockName& name);
684 std::unordered_map<std::string, std::string> alias_to_key_;
687 std::unordered_map<std::string, BlockName> key_to_name_;
Defines classes used to store parameters and to build derived/dependent parameter blocks.
Defines parameters whose values are lazily computed from other parameters.
Alias-aware container / façade over several parameter blocks.
void emplace(const char *name, std::shared_ptr< Block > blk)
Insert/replace a block using a C-string name (converted to BlockName).
void remove_item(const std::string &block, const LhaID &id)
Remove parameter (std::string overload).
void erase_block(const std::string &name)
Erase by name/alias (std::string overload).
bool has_scale(const std::string &block) const
Scale existence check (std::string overload).
void remove_item(const char *block, const LhaID &id)
Remove parameter (C-string overload).
void erase_block(const char *name)
Erase by name/alias (C-string overload).
void remove_item(const BlockName &block_name, LhaID id)
Removes one parameter from a block.
bool contains(const char *name) const
Alias-aware existence check (C-string overload).
std::unordered_map< std::string, std::shared_ptr< Block > > get_block_sources(const BlockName &block_name) const
Returns the source blocks of a given block.
std::shared_ptr< Block > & at(const BlockName &block_name)
Alias-aware mutable access to a block.
friend std::shared_ptr< BlockAccessor > operator>>(std::shared_ptr< BlockAccessor > lhs, std::shared_ptr< BlockAccessor > rhs)
Merges two accessors with priority given to lhs.
void reattach_block(const BlockName &block_name)
Reattaches a previously detached dependent block to its saved sources.
void setValue(const char *block, const LhaID &id, scalar_t v)
Value setter (C-string overload).
double get_scale(const char *block) const
Scale getter (C-string overload).
std::vector< std::string > get_source_block_names(const BlockName &block_name) const
Returns the direct source block names of a block.
void setValue(const std::string &block, const LhaID &id, scalar_t v)
Value setter (std::string overload).
bool contains(const std::string &name) const
Alias-aware existence check (std::string overload).
bool contains(const BlockName &block_name) const
Checks whether a block exists (alias-aware).
bool is_dependent_block(const BlockName &block_name) const
Checks whether a block is implemented as a DependentBlock.
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 emplace(const std::string &name, std::shared_ptr< Block > blk)
Insert/replace a block using a raw string name (converted to BlockName).
void erase_block(const BlockName &name)
Erases a block and removes all associated aliases from metadata.
std::shared_ptr< Block > & at(const std::string &name)
Alias-aware block access (std::string overload).
const std::shared_ptr< Block > & at(const char *name) const
Alias-aware block access (C-string overload, const).
bool has_param(const std::string &block, const LhaID &id) const
Parameter existence check (std::string overload).
void reattach_parameter(const BlockName &block_name, LhaID id)
Reattaches a previously detached dependent parameter.
std::unordered_set< ParamId > get_all_source_parameters(const std::unordered_set< ParamId > ¶m_ids) const
Recursively finds all leaf source parameters behind a set of parameters.
std::unordered_map< ParamId, std::shared_ptr< Parameter > > get_parameter_sources(const BlockName &block_name, LhaID id) const
Returns the source parameters of a given parameter.
std::vector< std::string > get_all_source_block_names(const BlockName &block_name) const
Returns all transitive source block names of a block.
const std::shared_ptr< Block > & at(const std::string &name) const
Alias-aware block access (std::string overload, const).
void emplace(const BlockName &name, std::shared_ptr< Block > blk)
Inserts or replaces a block, updating alias metadata.
void setParameter(const BlockName &blockName, LhaID id, std::shared_ptr< Parameter > source)
Inserts or updates a full Parameter object in a block.
scalar_t getValue(const char *block, const LhaID &id) const
Value getter (C-string overload).
double get_scale(const std::string &block) const
Scale getter (std::string overload).
scalar_t getValue(const std::string &block, const LhaID &id) const
Value getter (std::string overload).
std::shared_ptr< Block > & at(const char *name)
Alias-aware block access (C-string overload).
std::unordered_set< BlockName > get_block_names() const
Returns the set of all known block names.
friend std::shared_ptr< BlockAccessor > operator+(std::shared_ptr< BlockAccessor > lhs, std::shared_ptr< BlockAccessor > rhs)
Merges two block accessors with no conflict allowed.
std::unordered_map< std::string, std::shared_ptr< Block > > base_t
Underlying associative container type.
void detach_block(const BlockName &block_name)
Detaches a dependent block from its upstream dependencies.
bool has_param(const BlockName &blockName, LhaID pdgCode) const
Checks whether a given parameter exists inside a named block.
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.
std::map< LhaID, scalar_t > getAllValues(BlockName blockName)
Returns all current scalar values stored in a block.
bool has_param(const char *block, const LhaID &id) const
Parameter existence check (C-string overload).
std::vector< std::string > get_dependent_block_names(const BlockName &block_name) const
Returns the direct dependent block names observing a block.
friend std::ostream & operator<<(std::ostream &, std::shared_ptr< BlockAccessor >)
Stream output operator for BlockAccessor.
double get_scale(const BlockName &block_name) const
Returns the scale attached to a given block.
std::shared_ptr< BlockAccessor > deep_clone_plain() const
Deep-copy all stored blocks as independent plain blocks.
std::shared_ptr< BlockAccessor > operator[](std::unordered_set< BlockName > block_names)
Extracts a subset of blocks into a new accessor.
std::vector< std::string > get_all_dependent_block_names(const BlockName &block_name) const
Returns all transitive dependent block names observing a block.
bool has_scale(const char *block) const
Scale existence check (C-string overload).
bool has_scale(const BlockName &block_name) const
Checks whether a given block has a scale attached.
Block identifier with alias support.
Represents an identifier of a LHA element, possibly containing several sub-ids.