Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature in/out Edges convenience functions #357

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 92 additions & 1 deletion include/CXXGraph/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,46 @@ class Graph {
*/
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
inOutNeighbors(shared<const Node<T>> node) const;
/**
* \brief
* \brief This function generates a set of Edges going out of a node
* in any graph
*
* @param Pointer to the node
*
*/
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>> outEdges(
const Node<T> *node) const;
/**
* \brief
* \brief This function generates a set of Edges going out of a node
* in any graph
*
* @param Shared pointer to the node
*
*/
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>> outEdges(
shared<const Node<T>> node) const;
/**
* \brief
* \brief This function generates a set of Edges coming in or going out of
* a node in any graph
*
* @param Pointer to the node
*
*/
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
inOutEdges(const Node<T> *node) const;
/**
* \brief
* \brief This function generates a set of Edges coming in or going out of
* a node in any graph
*
* @param Shared pointer to the node
*
*/
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
inOutEdges(shared<const Node<T>> node) const;
/**
* @brief This function finds the subset of given a nodeId
* Subset is stored in a map where keys are the hash-id of the node & values
Expand Down Expand Up @@ -1091,7 +1131,7 @@ std::unordered_set<shared<Node<T>>, nodeHash<T>> Graph<T>::nodeSet() {
std::const_pointer_cast<Node<T>>(edgeSetIt->getNodePair().second));
}
for (auto &isNodeIt : isolatedNodesSet) {
nodeSet.insert(std::const_pointer_cast<Node<T>>(isNodeIt));
nodeSet.insert(std::const_pointer_cast<Node<T>>(isNodeIt));
}

return nodeSet;
Expand Down Expand Up @@ -1680,6 +1720,57 @@ Graph<T>::inOutNeighbors(shared<const Node<T>> node) const {
return inOutNeighbors;
}

template <typename T>
const std::unordered_set<shared<const Edge<T>>, edgeHash<T>> Graph<T>::outEdges(
const Node<T> *node) const {
auto node_shared = make_shared<const Node<T>>(*node);

return outEdges(node_shared);
}

template <typename T>
const std::unordered_set<shared<const Edge<T>>, edgeHash<T>> Graph<T>::outEdges(
shared<const Node<T>> node) const {
if (cachedAdjMatrix->find(node) == cachedAdjMatrix->end()) {
return std::unordered_set<shared<const Edge<T>>, edgeHash<T>>();
}
auto nodeEdgePairs = cachedAdjMatrix->at(node);

std::unordered_set<shared<const Edge<T>>, edgeHash<T>> outEdges;
for (auto pair : nodeEdgePairs) {
if (pair.second->isDirected().has_value() &&
pair.second->isDirected().value()) {
outEdges.insert(pair.second);
}
}

return outEdges;
}

template <typename T>
const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
Graph<T>::inOutEdges(const Node<T> *node) const {
auto node_shared = make_shared<const Node<T>>(*node);

return outEdges(node_shared);
}

template <typename T>
const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
Graph<T>::inOutEdges(shared<const Node<T>> node) const {
if (cachedAdjMatrix->find(node) == cachedAdjMatrix->end()) {
return std::unordered_set<shared<const Edge<T>>, edgeHash<T>>();
}
auto nodeEdgePairs = cachedAdjMatrix->at(node);

std::unordered_set<shared<const Edge<T>>, edgeHash<T>> outEdges;
for (auto pair : nodeEdgePairs) {
outEdges.insert(pair.second);
}

return outEdges;
}

template <typename T>
const DijkstraResult Graph<T>::dijkstra(const Node<T> &source,
const Node<T> &target) const {
Expand Down
Loading
Loading