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:
2025-02-12 12:53:50 -05:00
parent 4227eacd5b
commit 18ce7bf6de
9 changed files with 96 additions and 71 deletions

View File

@@ -12,6 +12,7 @@ libconst = library('const',
const_sources,
include_directories: include_directories('public'),
cpp_args: ['-fvisibility=default'],
dependencies: [const_dep],
install : true)
# Make headers accessible

View File

@@ -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;
}

View File

@@ -10,7 +10,7 @@
/**
* @brief Structure to hold a constant's details.
*/
struct constant {
struct Constant {
const std::string name; ///< Name of the constant
const double value; ///< Value of the constant
const double uncertainty; ///< Uncertainty in the constant's value
@@ -25,13 +25,13 @@ struct constant {
* @param unit The unit of the constant.
* @param reference The reference for the constant's value.
*/
constant(const std::string& name, double value, double uncertainty, const std::string& unit, const std::string& reference)
Constant(const std::string& name, const double value, const double uncertainty, const std::string& unit, const std::string& reference)
: name(name), value(value), uncertainty(uncertainty), unit(unit), reference(reference) {}
/**
* @brief overload the << operator for pretty printing
*/
friend std::ostream& operator<<(std::ostream& os, const constant& c) {
friend std::ostream& operator<<(std::ostream& os, const Constant& c) {
os << "<" << c.name << ": ";
os << c.value << "±" << c.uncertainty << " ";
os << c.unit << " (" << c.reference << ")>\n";
@@ -42,18 +42,28 @@ struct constant {
/**
* @brief Class to manage a collection of constants.
*/
class constants {
class Constants {
private:
bool loaded_ = false; ///< Flag to indicate if constants are loaded
const int col_widths_[6] = {25, 52, 20, 20, 17, 45}; // From the python script used to generate the constants file
std::map<std::string, constant> constants_; ///< Map to store constants by name
std::map<std::string, Constant> constants_; ///< Map to store constants by name
/**
* @brief Load constants from a file.
* @param filename The name of the file to load constants from.
* @brief Default constructor. Private to avoid direct instantiation
*/
Constants();
/**
* @brief Load constants from the embedded header file.
* @return True if loading was successful, false otherwise.
*/
bool load(const std::string& filename);
bool load();
/**
* @brief Initialize constants.
* @return True if initialization was successful, false otherwise.
*/
bool initialize();
/**
* @brief Trim leading and trailing whitespace from a string.
@@ -63,36 +73,28 @@ private:
std::string trim(const std::string& str);
public:
/**
* @brief Default constructor.
*/
constants();
/**
* @brief Constructor that initializes constants from a file.
* @param filename The name of the file to load constants from.
* @brief get instance of constants singelton
* @return instance of constants
*/
constants(const std::string& filename);
static Constants& getInstance() {
static Constants instance;
return instance;
}
/**
* @brief Check if constants are loaded.
* @return True if constants are loaded, false otherwise.
*/
bool is_loaded() { return loaded_; }
/**
* @brief Initialize constants from a file.
* @param filename The name of the file to load constants from.
* @return True if initialization was successful, false otherwise.
*/
bool initialize(const std::string& filename);
bool isLoaded() { return loaded_; }
/**
* @brief Get a constant by key.
* @param key The name of the constant to retrieve.
* @return The constant associated with the given key.
*/
constant get(const std::string& key) const;
Constant get(const std::string& key) const;
/**
* @brief Overloaded subscript operator to access constants by key.
@@ -100,7 +102,7 @@ public:
* @return The constant associated with the given key.
* @throws std::out_of_range if the key is not found.
*/
constant operator[](const std::string& key) const;
Constant operator[](const std::string& key) const;
/**
* @brief Check if a constant exists by key.