Skip to content

Commit

Permalink
breaking get_all_nodes into two separated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloprobst committed Oct 15, 2024
1 parent 2aa14ef commit 0db9905
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
28 changes: 16 additions & 12 deletions hyperon_das_atomdb_cpp/src/adapters/ram_only.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,29 @@ const StringList InMemoryDB::get_node_by_name_starting_with(const string& node_t
}

//------------------------------------------------------------------------------
const StringList InMemoryDB::get_all_nodes(const string& node_type, bool names) const {
const StringList InMemoryDB::get_all_nodes_handles(const string& node_type) const {
auto node_type_hash = ExpressionHasher::named_type_hash(node_type);
StringList node_handles;
if (names) {
for (const auto& [_, node] : this->db.node) {
if (node->composite_type_hash == node_type_hash) {
node_handles.emplace_back(node->name);
}
}
} else {
for (const auto& [handle, node] : this->db.node) {
if (node->composite_type_hash == node_type_hash) {
node_handles.emplace_back(handle);
}
for (const auto& [handle, node] : this->db.node) {
if (node->composite_type_hash == node_type_hash) {
node_handles.emplace_back(handle);
}
}
return move(node_handles);
}

//------------------------------------------------------------------------------
const StringList InMemoryDB::get_all_nodes_names(const string& node_type) const {
auto node_type_hash = ExpressionHasher::named_type_hash(node_type);
StringList node_names;
for (const auto& [_, node] : this->db.node) {
if (node->composite_type_hash == node_type_hash) {
node_names.emplace_back(node->name);
}
}
return move(node_names);
}

//------------------------------------------------------------------------------
const StringUnorderedSet InMemoryDB::get_all_links(const string& link_type) const {
StringUnorderedSet link_handles;
Expand Down
4 changes: 3 additions & 1 deletion hyperon_das_atomdb_cpp/src/adapters/ram_only.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ class InMemoryDB : public AtomDB {
const StringList get_node_by_name_starting_with(const string& node_type,
const string& startswith) const override;

const StringList get_all_nodes(const string& node_type, bool names = false) const override;
const StringList get_all_nodes_handles(const string& node_type) const override;

const StringList get_all_nodes_names(const string& node_type) const override;

const StringUnorderedSet get_all_links(const string& link_type) const override;

Expand Down
14 changes: 10 additions & 4 deletions hyperon_das_atomdb_cpp/src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,18 @@ class AtomDB {
const string& startswith) const = 0;

/**
* @brief Get all nodes of a specific type.
* @brief Get all nodes handles of a specific type.
* @param node_type The node type.
* @param names If True, return node names instead of handles. Default is False.
* @return A list of node handles or names, depending on the value of `names`.
* @return A list of node handles.
*/
virtual const StringList get_all_nodes(const string& node_type, bool names = false) const = 0;
virtual const StringList get_all_nodes_handles(const string& node_type) const = 0;

/**
* @brief Get all nodes names of a specific type.
* @param node_type The node type.
* @return A list of names.
*/
virtual const StringList get_all_nodes_names(const string& node_type) const = 0;

/**
* @brief Retrieves all links of the specified type from the database.
Expand Down
10 changes: 7 additions & 3 deletions hyperon_das_atomdb_cpp/src/nanobind/atom_db_trampoline.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace nb = nanobind;
* allowing for custom behavior and additional functionality.
*/
struct AtomDBTrampoline : AtomDB {
NB_TRAMPOLINE(AtomDB, 31);
NB_TRAMPOLINE(AtomDB, 32);

const string get_node_handle(const string& node_type, const string& node_name) const override {
NB_OVERRIDE_PURE(get_node_handle, node_type, node_name);
Expand Down Expand Up @@ -60,8 +60,12 @@ struct AtomDBTrampoline : AtomDB {
NB_OVERRIDE_PURE(get_node_by_name_starting_with, node_type, startswith);
}

const StringList get_all_nodes(const string& node_type, bool names = false) const override {
NB_OVERRIDE_PURE(get_all_nodes, node_type, names);
const StringList get_all_nodes_handles(const string& node_type) const override {
NB_OVERRIDE_PURE(get_all_nodes_handles, node_type);
}

const StringList get_all_nodes_names(const string& node_type) const override {
NB_OVERRIDE_PURE(get_all_nodes_names, node_type);
}

const StringUnorderedSet get_all_links(const string& link_type) const override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ NB_MODULE(ext, m) {
&AtomDB::get_node_by_name_starting_with,
"node_type"_a,
"startswith"_a)
.def("get_all_nodes", &AtomDB::get_all_nodes, "node_type"_a, "names"_a = false)
.def("get_all_nodes_handles", &AtomDB::get_all_nodes_handles, "node_type"_a)
.def("get_all_nodes_names", &AtomDB::get_all_nodes_names, "node_type"_a)
.def(
"get_all_links",
[](AtomDB& self, const string& link_type, const nb::kwargs& _ = {})
Expand Down

0 comments on commit 0db9905

Please sign in to comment.