Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
BlockName.h
Go to the documentation of this file.
1#ifndef BLOCK_NAME_H
2#define BLOCK_NAME_H
3
4#include <string>
5#include <stdexcept>
6#include <algorithm>
7#include <iostream>
8#include <unordered_set>
9#include <initializer_list>
10#include <map>
11#include <vector>
12#include <set>
13#include <ranges>
14#include <concepts>
15#include <variant>
16
17#include "Logger.h"
18#include "Utils.h"
19#include "GeneralEnum.h"
20
21namespace fs = std::filesystem;
22
59class BlockName {
60private:
67 std::unordered_set<std::string> block_names;
68 std::string primary;
69
70 static inline std::string upper_copy(std::string s) {
71 std::transform(s.begin(), s.end(), s.begin(),
72 [](unsigned char c){ return static_cast<char>(std::toupper(c)); });
73 return s;
74 }
75
76 static inline std::unordered_set<std::string> split_aliases(const std::string& s) {
77 std::unordered_set<std::string> out;
78 std::string cur;
79 for (char ch : s) {
80 if (ch == '/') {
81 if (!cur.empty()) out.insert(cur);
82 cur.clear();
83 } else cur.push_back(ch);
84 }
85 if (!cur.empty()) out.insert(cur);
86 return out;
87 }
88
89 static std::unordered_set<std::string> split_slash_aliases(const std::string& s) {
90 std::unordered_set<std::string> out;
91 std::string cur;
92 cur.reserve(s.size());
93 for (char c : s) {
94 if (c == '/') {
95 if (!cur.empty()) { out.insert(cur); cur.clear(); }
96 } else {
97 cur.push_back(c);
98 }
99 }
100 if (!cur.empty()) out.insert(cur);
101 return out;
102 }
103 public:
109 BlockName() = default;
110
118 BlockName(const std::string& name);
119
128 BlockName(const char* name);
129
142 BlockName(std::initializer_list<std::string> names);
143
151 BlockName(const std::unordered_set<std::string>& names);
152
161 std::unordered_set<std::string> get_alias() const;
162
164 const std::string& canonical() const;
165
177 operator std::string() const;
178
185 bool hasAlias(const std::string& alias) const;
186
195 std::string to_string() const;
196
208 bool operator==(const BlockName& other) const;
209
216 bool operator!=(const BlockName& other) const;
217
226 bool operator==(const std::string& name) const;
227
236 bool operator==(const char* name) const;
237
244 bool operator!=(const std::string& name) const;
245
256 BlockName& addAlias(const std::string& alias);
257
258 BlockName& addAliases(const std::unordered_set<std::string>& as);
259
276 friend BlockName operator+(const std::string& lhs, const BlockName& rhs);
277
291 friend std::ostream& operator<<(std::ostream& os, const BlockName& block);
292
302 void to_upper();
303
320 bool operator<(const BlockName& other) const;
321};
322
335inline bool operator==(const std::string& lhs, const BlockName& rhs) {
336 return rhs == lhs;
337}
338
346inline bool operator!=(const std::string& lhs, const BlockName& rhs) {
347 return !(rhs == lhs);
348}
349
350// inline bool operator==(const std::string& lhs, const BlockName& rhs) { return rhs.hasAlias(lhs); }
351// inline bool operator!=(const std::string& lhs, const BlockName& rhs) { return !(lhs == rhs); }
352
353namespace std {
362 template <>
363 struct hash<BlockName> {
374 // std::size_t operator()(const BlockName& p) const noexcept {
375 // size_t h = 0;
376 // for (const auto& name : p.get_alias()) {
377 // h ^= std::hash<std::string>{}(name) + 0x9e3779b9 + (h << 6) + (h >> 2); // boost-style hash combine
378 // }
379 // return h;
380 // }
381 // size_t operator()(const BlockName& b) const noexcept {
382 // return std::hash<std::string>{}(b.canonical());
383 // }
384 std::size_t operator()(const BlockName& p) const noexcept {
385 // hash order-independent sur tous les aliases
386 size_t h = 0;
387 for (const auto& name : p.get_alias()) {
388 h ^= std::hash<std::string>{}(name) + 0x9e3779b9 + (h << 6) + (h >> 2);
389 }
390 return h;
391 }
392 // size_t operator()(const BlockName& p) const noexcept {
393 // return std::hash<std::string>{}(p.canonical());
394 // }
395 };
396}
397
398#endif
bool operator!=(const std::string &lhs, const BlockName &rhs)
Symmetric inequality between std::string and BlockName.
Definition BlockName.h:346
bool operator==(const std::string &lhs, const BlockName &rhs)
Symmetric comparison between std::string and BlockName.
Definition BlockName.h:335
Block identifier with alias support.
Definition BlockName.h:59
bool hasAlias(const std::string &alias) const
Checks whether a given alias is registered for this block.
Definition BlockName.cpp:37
BlockName & addAlias(const std::string &alias)
Adds a new alias to this block.
Definition BlockName.cpp:68
std::string to_string() const
Returns a string representation of the block name.
Definition BlockName.cpp:41
BlockName & addAliases(const std::unordered_set< std::string > &as)
std::unordered_set< std::string > get_alias() const
Returns the set of aliases associated with this block.
Definition BlockName.cpp:22
BlockName()=default
Default constructor.
const std::string & canonical() const
Definition BlockName.cpp:26
bool operator!=(const BlockName &other) const
Inequality comparison between two BlockName objects.
Definition BlockName.cpp:52
friend std::ostream & operator<<(std::ostream &os, const BlockName &block)
Stream output operator for BlockName.
Definition BlockName.cpp:82
friend BlockName operator+(const std::string &lhs, const BlockName &rhs)
Concatenation operator between a prefix string and a BlockName.
Definition BlockName.cpp:74
bool operator==(const BlockName &other) const
Equality comparison between two BlockName objects.
Definition BlockName.cpp:45
bool operator<(const BlockName &other) const
Strict ordering operator for BlockName.
void to_upper()
Converts all aliases to upper-case in-place.
Definition BlockName.cpp:92
Hash specialization for SymbolId<Tag>.
Definition BlockName.h:353
std::size_t operator()(const BlockName &p) const noexcept
Computes a hash value for a BlockName.
Definition BlockName.h:384