Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloprobst committed Sep 18, 2024
1 parent ab9e6a0 commit 03ec7d0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
22 changes: 18 additions & 4 deletions hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,15 @@ const pair<const OptCursor, const Pattern_or_Template_List> InMemoryDB::get_matc

//------------------------------------------------------------------------------
const pair<const OptCursor, const Pattern_or_Template_List> 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);
Expand Down Expand Up @@ -411,10 +419,16 @@ const shared_ptr<const Link> 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<string>(&element)) {
hash_template.push_back(this->_build_named_type_hash_template(*str));
} else if (auto list = any_cast<ListOfAny>(&element)) {
hash_template.push_back(this->_build_named_type_hash_template(*list));
} else {
throw invalid_argument("Invalid composite type element.");
}
}
return move(hash_template);
}
Expand Down
4 changes: 2 additions & 2 deletions hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class InMemoryDB : public AtomDB {
const KwArgs& kwargs = {}) const override;

const pair<const OptCursor, const Pattern_or_Template_List> get_matched_type_template(
const StringList& _template, const KwArgs& kwargs = {}) const override;
const ListOfAny& _template, const KwArgs& kwargs = {}) const override;

const pair<const OptCursor, const Pattern_or_Template_List> get_matched_type(
const string& link_type,
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion hyperon_das_atomdb_cpp/src/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class AtomDB {
* the matched type templates.
*/
virtual const pair<const OptCursor, const Pattern_or_Template_List> 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.
Expand Down
2 changes: 1 addition & 1 deletion hyperon_das_atomdb_cpp/src/nanobind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ = {}
Expand Down
22 changes: 22 additions & 0 deletions hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(&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.
*
Expand Down

0 comments on commit 03ec7d0

Please sign in to comment.