Skip to content

Commit

Permalink
better implementation of CustomAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloprobst committed Oct 14, 2024
1 parent 14673d7 commit 8fa63d2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 212 deletions.
47 changes: 3 additions & 44 deletions hyperon_das_atomdb_cpp/src/document_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,50 +112,9 @@ class Atom {
result += ", composite_type_hash: '" + this->composite_type_hash + "'";
result += ", named_type: '" + this->named_type + "'";
result += ", custom_attributes: ";
if (this->custom_attributes.has_value()) {
result += "CustomAttributes(";
if (not this->custom_attributes->strings.empty()) {
result += "strings: {";
for (const auto& [key, value] : this->custom_attributes->strings) {
result += key + ": '" + value + "', ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
if (not this->custom_attributes->integers.empty()) {
result += "integers: {";
for (const auto& [key, value] : this->custom_attributes->integers) {
result += key + ": " + std::to_string(value) + ", ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
if (not this->custom_attributes->floats.empty()) {
result += "floats: {";
for (const auto& [key, value] : this->custom_attributes->floats) {
result += key + ": " + std::to_string(value) + ", ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
if (not this->custom_attributes->booleans.empty()) {
result += "booleans: {";
for (const auto& [key, value] : this->custom_attributes->booleans) {
result += key + ": " + (value ? "true" : "false") + ", ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
result.pop_back();
result.pop_back();
result += ")";
} else {
result += "NULL";
}
result += (this->custom_attributes.has_value()
? custom_attributes_to_string(this->custom_attributes.value())
: "NULL");
return move(result);
}
};
Expand Down
38 changes: 1 addition & 37 deletions hyperon_das_atomdb_cpp/src/nanobind/bind_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,7 @@ static nb::dict atom_to_dict(const Atom& self) {
dict["handle"] = self.handle;
dict["composite_type_hash"] = self.composite_type_hash;
dict["named_type"] = self.named_type;
if (self.custom_attributes.has_value()) {
nb::dict custom_attributes;
custom_attributes["strings"] = self.custom_attributes->strings;
custom_attributes["integers"] = self.custom_attributes->integers;
custom_attributes["floats"] = self.custom_attributes->floats;
custom_attributes["booleans"] = self.custom_attributes->booleans;
dict["custom_attributes"] = move(custom_attributes);
} else {
dict["custom_attributes"] = nullptr;
}
dict["custom_attributes"] = self.custom_attributes;
return move(dict);
};

Expand Down Expand Up @@ -181,33 +172,6 @@ static nb::dict link_to_dict(const Link& self) {
return move(dict);
};

/**
* @brief Converts CustomAttributes to a CustomAttributesTuple.
* @param custom_attributes The CustomAttributes to be converted.
* @return A CustomAttributesTuple containing the attributes of the CustomAttributes.
*/
static CustomAttributesTuple custom_attributes_to_tuple(const CustomAttributes& custom_attributes) {
return make_tuple(custom_attributes.strings,
custom_attributes.integers,
custom_attributes.floats,
custom_attributes.booleans);
}

/**
* @brief Converts a CustomAttributesTuple to CustomAttributes.
* @param custom_attributes The CustomAttributes object to be updated.
* @param state The CustomAttributesTuple containing the new state.
*/
static void tuple_to_custom_attributes(CustomAttributes& custom_attributes,
const CustomAttributesTuple& state) {
new (&custom_attributes) CustomAttributes({
strings : std::get<0>(state),
integers : std::get<1>(state),
floats : std::get<2>(state),
booleans : std::get<3>(state)
});
}

/**
* @brief Converts an AtomType object to an AtomTypeTuple.
* @param atom_type The AtomType object to be converted.
Expand Down
15 changes: 0 additions & 15 deletions hyperon_das_atomdb_cpp/src/nanobind/hyperon_das_atomdb_cpp_bind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,6 @@ NB_MODULE(ext, m) {
// ---------------------------------------------------------------------------------------------
// database submodule --------------------------------------------------------------------------
nb::module_ database = m.def_submodule("database");
nb::class_<CustomAttributes>(database, "CustomAttributes")
.def(nb::init<const StringUnorderedMap&,
const IntUnorderedMap&,
const FloatUnorderedMap&,
const BoolUnorderedMap&>(),
"strings"_a = StringUnorderedMap(),
"integers"_a = IntUnorderedMap(),
"floats"_a = FloatUnorderedMap(),
"booleans"_a = BoolUnorderedMap())
.def_rw("strings", &CustomAttributes::strings)
.def_rw("integers", &CustomAttributes::integers)
.def_rw("floats", &CustomAttributes::floats)
.def_rw("booleans", &CustomAttributes::booleans)
.def("__getstate__", &helpers::custom_attributes_to_tuple)
.def("__setstate__", &helpers::tuple_to_custom_attributes);
nb::class_<NodeParams>(database, "NodeParams")
.def(nb::init<const string&, const string&, const opt<CustomAttributes>&>(),
"type"_a,
Expand Down
174 changes: 58 additions & 116 deletions hyperon_das_atomdb_cpp/src/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,54 @@ using namespace std;

namespace atomdb {

using CustomAttributesKey = string;
using CustomAttributesValue = variant<string, long, double, bool>;
using CustomAttributes = unordered_map<CustomAttributesKey, CustomAttributesValue>;

/**
* @brief Retrieves a custom attribute from the given custom attributes.
* @param custom_attributes The custom attributes map.
* @param key The key for the custom attribute to retrieve.
* @return An optional value of type T if the custom attribute exists.
*/
template <typename T>
static const opt<T> get_custom_attribute(const CustomAttributes& custom_attributes,
const CustomAttributesKey& key) {
if (custom_attributes.find(key) != custom_attributes.end()) {
return std::get<T>(custom_attributes.at(key));
}
return nullopt;
}

/**
* @brief Converts custom attributes to a string representation.
* @param custom_attributes The custom attributes to be converted.
* @return A string representation of the custom attributes.
*/
static string custom_attributes_to_string(const CustomAttributes& custom_attributes) {
if (custom_attributes.empty()) {
return "{}";
}
string result = "{";
for (const auto& [key, value] : custom_attributes) {
result += key + ": ";
if (auto str = std::get_if<string>(&value)) {
result += "'" + *str + "'";
} else if (auto integer = std::get_if<long>(&value)) {
result += std::to_string(*integer);
} else if (auto floating = std::get_if<double>(&value)) {
result += std::to_string(*floating);
} else if (auto boolean = std::get_if<bool>(&value)) {
result += *boolean ? "true" : "false";
}
result += ", ";
}
result.pop_back();
result.pop_back();
result += "}";
return move(result);
}

/**
* @brief A Plain Old Data (POD) type representing various boolean flags for configuration options.
*
Expand All @@ -44,29 +92,6 @@ struct KwArgs {
bool handles_only = false;
};

/**
* @brief A Plain Old Data (POD) type which holds custom attributes categorized by type.
*
* This structure contains unordered maps for storing string, integer, float, and boolean attributes.
* It also provides an equality operator to compare two CustomAttributes objects.
*/
struct CustomAttributes {
StringUnorderedMap strings = {};
IntUnorderedMap integers = {};
FloatUnorderedMap floats = {};
BoolUnorderedMap booleans = {};

/**
* @brief Compares this CustomAttributes object with another for equality.
* @param other The CustomAttributes object to compare against.
* @return true if all attributes (strings, integers, floats, booleans) are equal, false otherwise.
*/
bool operator==(const CustomAttributes& other) const noexcept {
return this->strings == other.strings and this->integers == other.integers and
this->floats == other.floats and this->booleans == other.booleans;
}
};

/**
* @brief A Plain Old Data (POD) type representing parameters for a node.
*
Expand All @@ -88,52 +113,11 @@ struct NodeParams {
result += "type: '" + this->type + "'";
result += ", name: '" + this->name + "'";
result += ", custom_attributes: ";
if (this->custom_attributes.has_value()) {
result += "CustomAttributes(";
if (not this->custom_attributes->strings.empty()) {
result += "strings: {";
for (const auto& [key, value] : this->custom_attributes->strings) {
result += key + ": '" + value + "', ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
if (not this->custom_attributes->integers.empty()) {
result += "integers: {";
for (const auto& [key, value] : this->custom_attributes->integers) {
result += key + ": " + std::to_string(value) + ", ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
if (not this->custom_attributes->floats.empty()) {
result += "floats: {";
for (const auto& [key, value] : this->custom_attributes->floats) {
result += key + ": " + std::to_string(value) + ", ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
if (not this->custom_attributes->booleans.empty()) {
result += "booleans: {";
for (const auto& [key, value] : this->custom_attributes->booleans) {
result += key + ": " + (value ? "true" : "false") + ", ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
result.pop_back();
result.pop_back();
result += ")";
} else {
result += "None";
}
result += (this->custom_attributes.has_value()
? custom_attributes_to_string(this->custom_attributes.value())
: "NULL");
result += ")";
return result;
return move(result);
}
};

Expand All @@ -158,64 +142,22 @@ struct LinkParams {
result += "[";
if (not this->targets.empty()) {
for (const auto& target : this->targets) {
if (auto node_params = get_if<NodeParams>(&target)) {
if (auto node_params = std::get_if<NodeParams>(&target)) {
result += node_params->to_string() + ", ";
} else if (auto link_params = get_if<LinkParams>(&target)) {
} else if (auto link_params = std::get_if<LinkParams>(&target)) {
result += link_params->to_string() + ", ";
}
}
result.pop_back();
result.pop_back();
}
result += "]";

result += ", custom_attributes: ";
if (this->custom_attributes.has_value()) {
result += "CustomAttributes(";
if (not this->custom_attributes->strings.empty()) {
result += "strings: {";
for (const auto& [key, value] : this->custom_attributes->strings) {
result += key + ": '" + value + "', ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
if (not this->custom_attributes->integers.empty()) {
result += "integers: {";
for (const auto& [key, value] : this->custom_attributes->integers) {
result += key + ": " + std::to_string(value) + ", ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
if (not this->custom_attributes->floats.empty()) {
result += "floats: {";
for (const auto& [key, value] : this->custom_attributes->floats) {
result += key + ": " + std::to_string(value) + ", ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
if (not this->custom_attributes->booleans.empty()) {
result += "booleans: {";
for (const auto& [key, value] : this->custom_attributes->booleans) {
result += key + ": " + (value ? "true" : "false") + ", ";
}
result.pop_back();
result.pop_back();
result += "}, ";
}
result.pop_back();
result.pop_back();
result += ")";
} else {
result += "None";
}
result += (this->custom_attributes.has_value()
? custom_attributes_to_string(this->custom_attributes.value())
: "NULL");
result += ")";
return result;
return move(result);
}
};

Expand Down

0 comments on commit 8fa63d2

Please sign in to comment.