diff --git a/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp b/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp index e864ec47..0e8baa73 100644 --- a/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp +++ b/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp @@ -178,22 +178,23 @@ const pair InMemoryDB::get_matc if (link_type != WILDCARD && find(target_handles.begin(), target_handles.end(), WILDCARD) == target_handles.end()) { return {params.get(ParamsKeys::CURSOR), - {make_tuple(this->get_link_handle(link_type, target_handles), nullopt)}}; + {make_pair(this->get_link_handle(link_type, target_handles), nullopt)}}; } auto link_type_hash = link_type == WILDCARD ? WILDCARD : ExpressionHasher::named_type_hash(link_type); - StringList handles({link_type_hash}); + StringList handles; + handles.reserve(target_handles.size() + 1); + handles.push_back(link_type_hash); handles.insert(handles.end(), target_handles.begin(), target_handles.end()); auto pattern_hash = ExpressionHasher::composite_hash(handles); Pattern_or_Template_List patterns_matched; auto it = this->db.patterns.find(pattern_hash); if (it != this->db.patterns.end()) { - for (const auto& pattern_tuple : it->second) { - patterns_matched.push_back(pattern_tuple); - } + patterns_matched.reserve(it->second.size()); + patterns_matched.insert(patterns_matched.end(), it->second.begin(), it->second.end()); } if (params.get(ParamsKeys::TOPLEVEL_ONLY).value_or(false)) { @@ -509,17 +510,18 @@ void InMemoryDB::_add_templates(const string& composite_type_hash, const StringList& targets_hash) { auto it = this->db.templates.find(composite_type_hash); if (it != this->db.templates.end()) { - it->second.insert(make_tuple(key, targets_hash)); + it->second.insert(make_pair(key, targets_hash)); } else { this->db.templates[composite_type_hash] = - Database::TemplatesSet({make_tuple(key, targets_hash)}); + Database::Pattern_or_Template_Set({make_pair(key, targets_hash)}); } it = this->db.templates.find(named_type_hash); if (it != this->db.templates.end()) { - it->second.insert(make_tuple(key, targets_hash)); + it->second.insert(make_pair(key, targets_hash)); } else { - this->db.templates[named_type_hash] = Database::PatternsSet({make_tuple(key, targets_hash)}); + this->db.templates[named_type_hash] = + Database::Pattern_or_Template_Set({make_pair(key, targets_hash)}); } } @@ -531,12 +533,12 @@ void InMemoryDB::_delete_templates(const Link& link_document, const StringList& auto it = this->db.templates.find(composite_type_hash); if (it != this->db.templates.end()) { - it->second.erase(make_tuple(key, targets_hash)); + it->second.erase(make_pair(key, targets_hash)); } it = this->db.templates.find(named_type_hash); if (it != this->db.templates.end()) { - it->second.erase(make_tuple(key, targets_hash)); + it->second.erase(make_pair(key, targets_hash)); } } @@ -550,9 +552,10 @@ void InMemoryDB::_add_patterns(const string& named_type_hash, for (const auto& pattern_key : pattern_keys) { auto it = this->db.patterns.find(pattern_key); if (it == this->db.patterns.end()) { - this->db.patterns[pattern_key] = Database::PatternsSet({make_tuple(key, targets_hash)}); + this->db.patterns[pattern_key] = + Database::Pattern_or_Template_Set({make_pair(key, targets_hash)}); } else { - it->second.insert(make_tuple(key, targets_hash)); + it->second.insert(make_pair(key, targets_hash)); } } } @@ -567,7 +570,7 @@ void InMemoryDB::_delete_patterns(const Link& link_document, const StringList& t for (const auto& pattern_key : pattern_keys) { auto it = this->db.patterns.find(pattern_key); if (it != this->db.patterns.end()) { - it->second.erase(make_tuple(key, targets_hash)); + it->second.erase(make_pair(key, targets_hash)); } } } diff --git a/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp b/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp index 5d17f504..fb7798d2 100644 --- a/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp +++ b/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp @@ -13,28 +13,27 @@ namespace atomdb { */ class Database { public: - struct TupleHash { - auto operator()(const tuple& t) const -> size_t { + using Pattern_or_Template = pair; + + struct Pattern_or_Template_Hash { + auto operator()(const Pattern_or_Template& t) const -> size_t { size_t list_hash = 0; - for (const auto& str : get<1>(t)) { + for (const auto& str : t.second) { list_hash ^= hash{}(str); } - return hash{}(get<0>(t)) ^ list_hash; + return hash{}(t.first) ^ list_hash; } }; - using Pattern = tuple; - using PatternsSet = unordered_set; - using Template = tuple; - using TemplatesSet = unordered_set; + using Pattern_or_Template_Set = unordered_set; unordered_map> atom_type; unordered_map> node; unordered_map> link; unordered_map outgoing_set; unordered_map incoming_set; - unordered_map patterns; - unordered_map templates; + unordered_map patterns; + unordered_map templates; Database() : atom_type({}), diff --git a/hyperon_das_atomdb_cpp/src/nanobind.cpp b/hyperon_das_atomdb_cpp/src/nanobind.cpp index 025649f6..e19755d1 100644 --- a/hyperon_das_atomdb_cpp/src/nanobind.cpp +++ b/hyperon_das_atomdb_cpp/src/nanobind.cpp @@ -145,11 +145,14 @@ NB_MODULE(hyperon_das_atomdb, m) { .def( "add_link", []( - InMemoryDB& self, const LinkParams& link_params, bool toplevel + InMemoryDB& self, + const LinkParams& link_params, + bool toplevel ) -> shared_ptr { return self.add_link(link_params, toplevel); }, - "link_params"_a, "toplevel"_a = true + "link_params"_a, + "toplevel"_a = true ) .def( "get_atom", @@ -160,12 +163,14 @@ NB_MODULE(hyperon_das_atomdb, m) { bool targets_documents = false, bool deep_representation = false ) -> shared_ptr { - Params params = Params({ - {ParamsKeys::NO_TARGET_FORMAT, no_target_format}, - {ParamsKeys::TARGETS_DOCUMENTS, targets_documents}, - {ParamsKeys::DEEP_REPRESENTATION, deep_representation} - }); - return self.get_atom(handle, params); + /* TODO: adds a lot of overhead, have to find a faster alternative + * Params params = Params({ + * {ParamsKeys::NO_TARGET_FORMAT, no_target_format}, + * {ParamsKeys::TARGETS_DOCUMENTS, targets_documents}, + * {ParamsKeys::DEEP_REPRESENTATION, deep_representation} + * }); + */ + return self.get_atom(handle); //, params); }, "handle"_a, nb::kw_only(), @@ -176,7 +181,9 @@ NB_MODULE(hyperon_das_atomdb, m) { .def( "get_node_handle", []( - InMemoryDB& self, const string& node_type, const string& node_name + InMemoryDB& self, + const string& node_type, + const string& node_name ) -> const string { return self.get_node_handle(node_type, node_name); } @@ -190,10 +197,12 @@ NB_MODULE(hyperon_das_atomdb, m) { opt cursor = nullopt, bool toplevel_only = false ) -> const pair { - Params params = Params({{ParamsKeys::TOPLEVEL_ONLY, toplevel_only}}); - if (cursor) - params.set(ParamsKeys::CURSOR, cursor.value()); - return self.get_matched_links(link_type, target_handles, params); + /* TODO: adds a lot of overhead, have to find a faster alternative + * Params params = Params({{ParamsKeys::TOPLEVEL_ONLY, toplevel_only}}); + * if (cursor) + * params.set(ParamsKeys::CURSOR, cursor.value()); + */ + return self.get_matched_links(link_type, target_handles); //, params); }, "link_type"_a, "target_handles"_a, diff --git a/hyperon_das_atomdb_cpp/src/type_aliases.hpp b/hyperon_das_atomdb_cpp/src/type_aliases.hpp index c4c618aa..b9f36813 100644 --- a/hyperon_das_atomdb_cpp/src/type_aliases.hpp +++ b/hyperon_das_atomdb_cpp/src/type_aliases.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -25,7 +24,7 @@ using StringList = vector; using StringUnorderedSet = unordered_set; using ListOfAny = vector; -using Pattern_or_Template = tuple>; +using Pattern_or_Template = pair>; using Pattern_or_Template_List = vector; } // namespace atomdb diff --git a/hyperon_das_atomdb_cpp/src/utils/params.hpp b/hyperon_das_atomdb_cpp/src/utils/params.hpp index d2669dbf..02b62849 100644 --- a/hyperon_das_atomdb_cpp/src/utils/params.hpp +++ b/hyperon_das_atomdb_cpp/src/utils/params.hpp @@ -40,9 +40,7 @@ class Params : private ParamsMap { * Defaults to an empty map if not provided. * @return A Params object initialized with the specified params. */ - Params(const ParamsMap& params = {}) { - this->insert(params.begin(), params.end()); - }; + Params(const ParamsMap& params = {}) { this->insert(params.begin(), params.end()); }; /** * @brief Checks if the specified parameter exists in the collection.