Skip to content

Commit

Permalink
Implement generic addNodes and addEdges (ZigRazor#363)
Browse files Browse the repository at this point in the history
* Add header file containig type traits for edges and nodes

* Add `is_node_v` and `is_edge_v` templates

* Implement addNodes and addEdges methods

* Add tests for addEdges

* Add tests for addNodes

* Document addNodes and addEdges methods

* Specify different type trait for Node and Edge pointers, is_node_ptr and is_edge_ptr
  • Loading branch information
sbaldu authored Oct 9, 2023
1 parent 916e6cc commit db7a3cd
Show file tree
Hide file tree
Showing 3 changed files with 444 additions and 7 deletions.
70 changes: 70 additions & 0 deletions include/CXXGraph/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "CXXGraph/Utility/PointerHash.hpp"
#include "CXXGraph/Utility/Reader.hpp"
#include "CXXGraph/Utility/ThreadSafe.hpp"
#include "CXXGraph/Utility/TypeTraits.hpp"
#include "CXXGraph/Utility/Typedef.hpp"
#include "CXXGraph/Utility/Writer.hpp"

Expand Down Expand Up @@ -179,6 +180,27 @@ class Graph {
*
*/
virtual void addEdge(shared<const Edge<T>> edge);
/**
* \brief
* Function that adds any number of Edges to the Graph Edge set
* Note: This is the overload needed to terminate the
* recursion
*
* @param None
*
*/
template <typename... Tn>
void addEdges();
/**
* \brief
* Function that adds any number of Edges to the Graph Edge set
*
* @param Raw pointers or shared pointers to the Edges
*
*/
template <typename T1, typename... Tn>
std::enable_if<is_edge_ptr_v<T1> && (is_edge_ptr_v<Tn> && ...), void> addEdges(
T1 edge, Tn... edges);
/**
* \brief
* Function to add a Node to the Graph Node Set
Expand All @@ -197,6 +219,26 @@ class Graph {
*
*/
virtual void addNode(shared<const Node<T>> node);
/**
* \brief
* Function that adds any number of Nodes to the Graph Node set
* Note: This overload is needed to terminate the recursion
*
* @param None
*
*/
template <typename... Tn>
void addNodes();
/**
* \brief
* Function that adds any number of Nodes to the Graph Node set
*
* @param Raw pointers or shared pointers to the Edges
*
*/
template <typename T1, typename... Tn>
std::enable_if<is_node_ptr_v<T1> && (is_node_ptr_v<Tn> && ...), void> addNodes(
T1 node, Tn... nodes);
/**
* \brief
* Function remove an Edge from the Graph Edge Set
Expand Down Expand Up @@ -931,6 +973,20 @@ void Graph<T>::addEdge(shared<const Edge<T>> edge) {
}
}

template <typename T>
template <typename... Tn>
void Graph<T>::addEdges() {
return;
}

template <typename T>
template <typename T1, typename... Tn>
std::enable_if<is_edge_ptr_v<T1> && (is_edge_ptr_v<Tn> && ...), void> Graph<T>::addEdges(
T1 edge, Tn... edges) {
addEdge(edge);
addEdges(edges...);
}

template <typename T>
void Graph<T>::addNode(const Node<T> *node) {
auto node_shared = make_shared<const Node<T>>(*node);
Expand All @@ -942,6 +998,20 @@ void Graph<T>::addNode(shared<const Node<T>> node) {
this->isolatedNodesSet.insert(node);
}

template <typename T>
template <typename... Tn>
void Graph<T>::addNodes() {
return;
}

template <typename T>
template <typename T1, typename... Tn>
std::enable_if<is_node_ptr_v<T1> && (is_node_ptr_v<Tn> && ...), void> Graph<T>::addNodes(
T1 node, Tn... nodes) {
addNode(node);
addNodes(nodes...);
}

template <typename T>
void Graph<T>::removeEdge(const CXXGraph::id_t edgeId) {
auto edgeOpt = Graph<T>::getEdge(edgeId);
Expand Down
78 changes: 78 additions & 0 deletions include/CXXGraph/Utility/TypeTraits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/***********************************************************/
/*** ______ ____ ______ _ ***/
/*** / ___\ \/ /\ \/ / ___|_ __ __ _ _ __ | |__ ***/
/*** | | \ / \ / | _| '__/ _` | '_ \| '_ \ ***/
/*** | |___ / \ / \ |_| | | | (_| | |_) | | | | ***/
/*** \____/_/\_\/_/\_\____|_| \__,_| .__/|_| |_| ***/
/*** |_| ***/
/***********************************************************/
/*** Header-Only C++ Library for Graph ***/
/*** Representation and Algorithms ***/
/***********************************************************/
/*** Author: ZigRazor ***/
/*** E-Mail: zigrazor@gmail.com ***/
/***********************************************************/
/*** Collaboration: ----------- ***/
/***********************************************************/
/*** License: AGPL v3.0 ***/
/***********************************************************/

#ifndef __CXXGRAPH_TYPE_TRAITS__
#define __CXXGRAPH_TYPE_TRAITS__

#pragma once

#include <memory>

#include "CXXGraph/Edge/DirectedEdge.hpp"
#include "CXXGraph/Edge/DirectedWeightedEdge.hpp"
#include "CXXGraph/Edge/Edge.hpp"
#include "CXXGraph/Edge/UndirectedEdge.hpp"
#include "CXXGraph/Edge/UndirectedWeightedEdge.hpp"
#include "CXXGraph/Edge/Weighted.hpp"
#include "CXXGraph/Node/Node.hpp"

namespace CXXGraph {

// define is_node type trait for Nodes, Nodes pointers and shared pointers
template <typename T>
struct is_node : std::false_type {};

template <typename T>
struct is_node<Node<T>> : std::true_type {};

// define is_node_ptr type trait for Node pointers and shared pointers
template <typename T>
struct is_node_ptr : std::false_type {};

template <typename T>
struct is_node_ptr<const Node<T>*> : std::true_type {};

template <typename T>
struct is_node_ptr<shared<const Node<T>>> : std::true_type {};

template <typename T>
inline constexpr bool is_node_ptr_v = is_node<T>::value;

// define is_edge type trait for Edges
template <typename T>
struct is_edge : std::false_type {};

template <typename T>
struct is_edge<Edge<T>> : std::true_type {};

// define is_edge_ptr type trait for Edge pointers and shared pointers
template <typename T>
struct is_edge_ptr : std::false_type {};

template <typename T>
struct is_edge_ptr<const Edge<T>*> : std::true_type {};

template <typename T>
struct is_edge_ptr<shared<const Edge<T>>> : std::true_type {};

template <typename T>
inline constexpr bool is_edge_ptr_v = is_edge<T>::value;
} // namespace CXXGraph

#endif
Loading

0 comments on commit db7a3cd

Please sign in to comment.