perf(const): const changed to a singelton
const needds to be accessed all throughout so it has been changed to a singleton to allow for more efficient usage BREAKING CHANGE: Any previous loads to const will break, also constant->Constant and constants->Constants
This commit is contained in:
@@ -3,23 +3,22 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <set>
|
||||
|
||||
|
||||
#include "const.h"
|
||||
#include "embedded_constants.h" // Generated at build time by meson
|
||||
|
||||
constants::constants() {
|
||||
loaded_ = false;
|
||||
Constants::Constants() {
|
||||
loaded_ = initialize();
|
||||
}
|
||||
|
||||
constants::constants(const std::string& filename) {
|
||||
loaded_ = initialize(filename);
|
||||
bool Constants::initialize() {
|
||||
return load();
|
||||
}
|
||||
|
||||
bool constants::initialize(const std::string& filename) {
|
||||
return load(filename);
|
||||
}
|
||||
|
||||
constant constants::get(const std::string& name) const {
|
||||
Constant Constants::get(const std::string& name) const {
|
||||
auto it = constants_.find(name);
|
||||
if (it != constants_.end()) {
|
||||
return it->second;
|
||||
@@ -28,15 +27,15 @@ constant constants::get(const std::string& name) const {
|
||||
}
|
||||
}
|
||||
|
||||
constant constants::operator[](const std::string& name) const {
|
||||
Constant Constants::operator[](const std::string& name) const {
|
||||
return this->get(name);
|
||||
}
|
||||
|
||||
bool constants::has(const std::string& name) const {
|
||||
bool Constants::has(const std::string& name) const {
|
||||
return constants_.find(name) != constants_.end();
|
||||
}
|
||||
|
||||
std::set<std::string> constants::keys() const {
|
||||
std::set<std::string> Constants::keys() const {
|
||||
std::set<std::string> keys;
|
||||
for (const auto& pair : constants_) {
|
||||
keys.insert(pair.first);
|
||||
@@ -44,32 +43,28 @@ std::set<std::string> constants::keys() const {
|
||||
return keys;
|
||||
}
|
||||
|
||||
std::string constants::trim(const std::string& str) {
|
||||
std::string Constants::trim(const std::string& str) {
|
||||
size_t first = str.find_first_not_of(" \t");
|
||||
if (first == std::string::npos) return "";
|
||||
size_t last = str.find_last_not_of(" \t");
|
||||
return str.substr(first, last - first + 1);
|
||||
}
|
||||
|
||||
bool constants::load(const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Error: Unable to open file " << filename << std::endl;
|
||||
return false;
|
||||
}
|
||||
bool Constants::load() {
|
||||
std::istringstream fileStream(embeddedConstants);
|
||||
|
||||
std::string line;
|
||||
bool data_section = false;
|
||||
int line_count = 0;
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
while (std::getline(fileStream, line)) {
|
||||
line_count++;
|
||||
|
||||
// Detect start of data section (double divider line)
|
||||
if (!data_section) {
|
||||
if (line.find("Symbol") != std::string::npos) { // Find header row
|
||||
std::getline(file, line); // Skip dashed divider
|
||||
std::getline(file, line); // Skip second dashed divider
|
||||
std::getline(fileStream, line); // Skip dashed divider
|
||||
std::getline(fileStream, line); // Skip second dashed divider
|
||||
data_section = true;
|
||||
}
|
||||
continue;
|
||||
@@ -102,10 +97,8 @@ bool constants::load(const std::string& filename) {
|
||||
}
|
||||
|
||||
// Store in map
|
||||
constants_.emplace(symbol, constant{name, value, uncertainty, unit, reference});
|
||||
constants_.emplace(symbol, Constant{name, value, uncertainty, unit, reference});
|
||||
}
|
||||
|
||||
file.close();
|
||||
loaded_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user