Skip to content

Commit

Permalink
Add API to remove nodes from CXXGraph by id
Browse files Browse the repository at this point in the history
  • Loading branch information
sjnptl committed Apr 29, 2024
1 parent 11e80f2 commit 53b9961
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/CXXGraph/Node/Node_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Node {
const std::string &getUserId() const;
const T &getData() const;
void setData(T &&new_data);

// operator
bool operator==(const Node<T> &b) const;
bool operator<(const Node<T> &b) const;
Expand Down
12 changes: 12 additions & 0 deletions include/CXXGraph/Node/Node_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ void Node<T>::setData(T &&new_data) {
this->data = std::move(new_data);
}

bool deleteNodeById(const std::string &id) {
auto it = std::find_if(nodes.begin(), nodes.end(), [&](const auto &pair) {
return pair.second.getUserId() == id;
});

if (it != nodes.end()) {
nodes.erase(it);
return true; // Node deleted successfully
}
return false; // Node not found
}

// The data type T must have an overload of the equality operator
template <typename T>
bool Node<T>::operator==(const Node<T> &b) const {
Expand Down

0 comments on commit 53b9961

Please sign in to comment.