From 03ec7d005cc642b59c9b2a71c4287cd8140cc41d Mon Sep 17 00:00:00 2001 From: Angelo Probst Date: Wed, 18 Sep 2024 16:32:38 -0300 Subject: [PATCH] wip --- .../src/adapters/ram_only.cpp | 22 +++++++++++++++---- .../src/adapters/ram_only.hpp | 4 ++-- hyperon_das_atomdb_cpp/src/database.hpp | 2 +- hyperon_das_atomdb_cpp/src/nanobind.cpp | 2 +- .../src/utils/expression_hasher.hpp | 22 +++++++++++++++++++ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp b/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp index 3081448c..08605a03 100644 --- a/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp +++ b/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp @@ -205,7 +205,15 @@ const pair InMemoryDB::get_matc //------------------------------------------------------------------------------ const pair InMemoryDB::get_matched_type_template( - const StringList& _template, const KwArgs& kwargs) const { + const ListOfAny& _template, const KwArgs& kwargs) const { + /** + * NOTE: + * Next two lines are spending a lot of time in handling ListOfAny, however + * all test cases are passing in a flat list of strings to this method. + * So it seems that we could safely change the signature of this method to + * receive a StringList instead of ListOfAny and then simplify the underlying + * implementation. + */ auto hash_base = this->_build_named_type_hash_template(_template); auto template_hash = ExpressionHasher::composite_hash(hash_base); auto it = this->db.templates.find(template_hash); @@ -411,10 +419,16 @@ const shared_ptr InMemoryDB::_get_and_delete_link(const string& link } //------------------------------------------------------------------------------ -const StringList InMemoryDB::_build_named_type_hash_template(const StringList& _template) const { - StringList hash_template; +const ListOfAny InMemoryDB::_build_named_type_hash_template(const ListOfAny& _template) const { + ListOfAny hash_template; for (const auto& element : _template) { - hash_template.push_back(this->_build_atom_type_key_hash(element)); + if (auto str = any_cast(&element)) { + hash_template.push_back(this->_build_named_type_hash_template(*str)); + } else if (auto list = any_cast(&element)) { + hash_template.push_back(this->_build_named_type_hash_template(*list)); + } else { + throw invalid_argument("Invalid composite type element."); + } } return move(hash_template); } diff --git a/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp b/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp index 70b81b65..bdb1f866 100644 --- a/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp +++ b/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp @@ -135,7 +135,7 @@ class InMemoryDB : public AtomDB { const KwArgs& kwargs = {}) const override; const pair get_matched_type_template( - const StringList& _template, const KwArgs& kwargs = {}) const override; + const ListOfAny& _template, const KwArgs& kwargs = {}) const override; const pair get_matched_type( const string& link_type, @@ -215,7 +215,7 @@ class InMemoryDB : public AtomDB { * ``` * @endcode */ - const StringList _build_named_type_hash_template(const StringList& _template) const; + const ListOfAny _build_named_type_hash_template(const ListOfAny& _template) const; const string _build_named_type_hash_template(const string& _template) const; diff --git a/hyperon_das_atomdb_cpp/src/database.hpp b/hyperon_das_atomdb_cpp/src/database.hpp index df96c56c..920f8933 100644 --- a/hyperon_das_atomdb_cpp/src/database.hpp +++ b/hyperon_das_atomdb_cpp/src/database.hpp @@ -263,7 +263,7 @@ class AtomDB { * the matched type templates. */ virtual const pair get_matched_type_template( - const StringList& _template, const KwArgs& kwargs = {}) const = 0; + const ListOfAny& _template, const KwArgs& kwargs = {}) const = 0; /** * @brief Retrieves matched types based on the specified link type. diff --git a/hyperon_das_atomdb_cpp/src/nanobind.cpp b/hyperon_das_atomdb_cpp/src/nanobind.cpp index 4d000070..be3bdc65 100644 --- a/hyperon_das_atomdb_cpp/src/nanobind.cpp +++ b/hyperon_das_atomdb_cpp/src/nanobind.cpp @@ -330,7 +330,7 @@ NB_MODULE(hyperon_das_atomdb, m) { "get_matched_type_template", []( InMemoryDB& self, - const StringList& _template, + const ListOfAny& _template, const OptCursor cursor = nullopt, bool toplevel_only = false, const nb::kwargs _ = {} diff --git a/hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp b/hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp index a5b1c1df..e6a5d757 100644 --- a/hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp +++ b/hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp @@ -83,6 +83,28 @@ class ExpressionHasher { return compute_hash(hashable_string); } + /** + * @brief Generates a composite hash from a list of elements. + * + * This function takes a vector of elements, each of which can be of any type, + * and generates a composite hash representing the combined hash of all elements. + * + * @param elements A vector of elements of type std::any, representing the components to be + * hashed. + * @return A string representing the composite hash generated from the elements. + */ + static std::string composite_hash(const ListOfAny& elements) { + StringList hashable_elements; + for (const auto& element : elements) { + if (auto str = any_cast(&element)) { + hashable_elements.push_back(*str); + } else { + throw invalid_argument("Invalid composite type element."); + } + } + return composite_hash(hashable_elements); + } + /** * @brief Generates a composite hash from a base hash. *