Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
dynamic_registry.h
Go to the documentation of this file.
1#ifndef DYNAMIC_REGISTRY_H
2#define DYNAMIC_REGISTRY_H
3
4#include <string>
5#include <string_view>
6#include <vector>
7#include <unordered_map>
8#include <optional>
9#include <algorithm>
10#include <cctype>
11
27// ------------------------------------------------------------------
28// SymbolId<Tag> : wrapper string-strong-typed
29// ------------------------------------------------------------------
30
46template<class Tag>
47class SymbolId {
48 std::string name_;
49public:
53 SymbolId() = default;
54
63 explicit SymbolId(std::string n) : name_(std::move(n)) {}
64
72 const std::string& str() const { return name_; }
73};
74
75
76
77
78
79// ------------------------------------------------------------------
80// case-insensitive
81// ------------------------------------------------------------------
82
96inline std::string normalize_key(std::string_view s) {
97 std::string out;
98 out.reserve(s.size());
99 for (unsigned char c : s) {
100 // if (!std::isalnum(c)) continue; // <-- option : ignore space and punctuation
101 out.push_back(static_cast<char>(std::tolower(c)));
102 }
103 return out;
104}
105
118template<class Tag>
119inline bool operator==(const SymbolId<Tag>& a, const SymbolId<Tag>& b) noexcept {
120 return normalize_key(a.str()) == normalize_key(b.str());
121}
122
136template<class Tag>
137inline bool operator<(const SymbolId<Tag>& a, const SymbolId<Tag>& b) noexcept {
138 const auto an = normalize_key(a.str());
139 const auto bn = normalize_key(b.str());
140 return an < bn;
141}
142
152namespace std {
153 template<class Tag>
154 struct hash<SymbolId<Tag>> {
161 size_t operator()(const SymbolId<Tag>& id) const noexcept {
162 return std::hash<std::string>{}(normalize_key(id.str()));
163 }
164 };
165}
166
167
168// ------------------------------------------------------------------
169// DynamicRegistry: registry of SymbolId with optional external key
170// ------------------------------------------------------------------
171
200template<class Tag, class ExternalKey, class Hash = std::hash<ExternalKey>>
202public:
212 struct Entry {
214 std::vector<std::string> aliases;
215 std::optional<ExternalKey> ext;
216 bool builtin = false;
217 };
218
219private:
221 std::unordered_map<std::string, Entry> by_name_;
223 std::unordered_map<ExternalKey, std::string, Hash> by_ext_;
224
225public:
250 std::vector<std::string> aliases,
251 std::optional<ExternalKey> ext,
252 bool builtin);
253
264 std::optional<SymbolId<Tag>> find(std::string_view name) const;
265
275 std::optional<SymbolId<Tag>> find_by_external(const ExternalKey& k) const;
276
283 std::optional<ExternalKey> external_of(const SymbolId<Tag>& id) const;
284
295 bool update_external(const SymbolId<Tag>& id, const ExternalKey& k);
296
306 std::vector<SymbolId<Tag>> list_all() const;
307};
308
309// ------------------------------------------------------------------
310// DynamicRegistry specialization without external key
311// ------------------------------------------------------------------
312
333template<class Tag>
334class DynamicRegistry<Tag, void, void> {
335public:
339 struct Entry {
341 std::vector<std::string> aliases;
342 bool builtin = false;
343 };
344private:
346 std::unordered_map<std::string, Entry> by_name_;
347
348public:
364 std::vector<std::string> aliases,
365 bool builtin);
366
373 std::optional<SymbolId<Tag>> find(std::string_view name) const;
374
383 std::vector<SymbolId<Tag>> list_all() const;
384};
385
386#include "dynamic_registry.tpp"
387
388#endif
bool register_id(const SymbolId< Tag > &id, std::vector< std::string > aliases, bool builtin)
Registers or updates an identifier (no external key).
std::vector< SymbolId< Tag > > list_all() const
Lists all distinct identifiers in the registry.
std::optional< SymbolId< Tag > > find(std::string_view name) const
Finds an identifier by name (or alias), case-insensitively.
Case-insensitive registry of SymbolId entries with optional external keys.
std::optional< SymbolId< Tag > > find(std::string_view name) const
Finds an identifier by name (or alias), case-insensitively.
bool register_id(const SymbolId< Tag > &id, std::vector< std::string > aliases, std::optional< ExternalKey > ext, bool builtin)
Registers or updates an identifier in the registry.
std::vector< SymbolId< Tag > > list_all() const
Lists all distinct identifiers stored in the registry.
std::optional< ExternalKey > external_of(const SymbolId< Tag > &id) const
Returns the external key associated with a given identifier.
bool update_external(const SymbolId< Tag > &id, const ExternalKey &k)
Updates or sets the external key of a given identifier.
std::optional< SymbolId< Tag > > find_by_external(const ExternalKey &k) const
Finds an identifier by external key.
Returns an uppercase copy of the input string.
SymbolId(std::string n)
Constructs a SymbolId from a string.
const std::string & str() const
Returns the underlying string.
SymbolId()=default
Default constructor, creates an empty identifier.
bool operator==(const SymbolId< Tag > &a, const SymbolId< Tag > &b) noexcept
Case-insensitive equality comparison for SymbolId<Tag>.
std::string normalize_key(std::string_view s)
Normalizes a string key in a case-insensitive manner.
bool operator<(const SymbolId< Tag > &a, const SymbolId< Tag > &b) noexcept
Case-insensitive strict ordering for SymbolId<Tag>.
Hash specialization for SymbolId<Tag>.
Definition BlockName.h:353
Entry stored in the registry.
std::optional< ExternalKey > ext
Optional external key.
bool builtin
If true, entry cannot be overridden.
std::vector< std::string > aliases
Additional names mapping to this entry.
SymbolId< Tag > id
Canonical identifier.
SymbolId< Tag > id
Canonical identifier.
std::vector< std::string > aliases
Alias names.
size_t operator()(const SymbolId< Tag > &id) const noexcept
Computes the hash value of a SymbolId<Tag>.