diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 13ce0c9e..1025d2e5 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -2,9 +2,7 @@ name: CMake on: push: - branches: [ master ] pull_request: - branches: [ master ] env: # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) @@ -41,7 +39,7 @@ jobs: # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type run: | if [ ${{ matrix.os }} == 'windows-latest' ]; then - cmake -DTEST=ON -Dgtest_disable_pthreads=ON -B ${{github.workspace}}/build + cmake -DTEST=ON -Dgtest_disable_pthreads=ON -B ./build else cmake -DTEST=ON -B ${{github.workspace}}/build fi; diff --git a/.gitignore b/.gitignore index f982ca42..8d678b02 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,5 @@ packaging/ .vscode .code-workspace CXXGraph.code-workspace +# ignore Visual Studio files +/.vs diff --git a/include/CXXGraph/CXXGraphConfig.h b/include/CXXGraph/CXXGraphConfig.h index f6ff9ff4..44cfc71d 100644 --- a/include/CXXGraph/CXXGraphConfig.h +++ b/include/CXXGraph/CXXGraphConfig.h @@ -1,4 +1,4 @@ -// the configured options and settings for CXXGraph -#define CXXGraph_VERSION_MAJOR 4 -#define CXXGraph_VERSION_MINOR 1 -#define CXXGraph_VERSION_PATCH 0 +// the configured options and settings for CXXGraph +#define CXXGraph_VERSION_MAJOR 4 +#define CXXGraph_VERSION_MINOR 1 +#define CXXGraph_VERSION_PATCH 0 diff --git a/include/CXXGraph/Edge/DirectedEdge.h b/include/CXXGraph/Edge/DirectedEdge.h index 76c8e593..79e19b5b 100755 --- a/include/CXXGraph/Edge/DirectedEdge.h +++ b/include/CXXGraph/Edge/DirectedEdge.h @@ -17,11 +17,11 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_DIRECTEDEDGE_H__ -#define __CXXGRAPH_DIRECTEDEDGE_H__ +#ifndef CXXGRAPH_DIRECTEDEDGE_H +#define CXXGRAPH_DIRECTEDEDGE_H #pragma once #include "DirectedEdge_impl.hpp" -#endif // __CXXGRAPH_DIRECTEDEDGE_H__ +#endif // CXXGRAPH_DIRECTEDEDGE_H diff --git a/include/CXXGraph/Edge/DirectedEdge_decl.h b/include/CXXGraph/Edge/DirectedEdge_decl.h index b4611c3a..f33eabd1 100644 --- a/include/CXXGraph/Edge/DirectedEdge_decl.h +++ b/include/CXXGraph/Edge/DirectedEdge_decl.h @@ -17,8 +17,8 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_DIRECTEDEDGE_DECL_H__ -#define __CXXGRAPH_DIRECTEDEDGE_DECL_H__ +#ifndef CXXGRAPH_DIRECTEDEDGE_DECL_H_ +#define CXXGRAPH_DIRECTEDEDGE_DECL_H_ #pragma once @@ -42,19 +42,21 @@ std::ostream &operator<<(std::ostream &o, const DirectedEdge &edge); template class DirectedEdge : public Edge { public: - DirectedEdge(const CXXGraph::id_t id, const Node &node1, + constexpr DirectedEdge(const CXXGraph::id_t id, const Node &node1, const Node &node2); - DirectedEdge(const CXXGraph::id_t id, shared> node1, + constexpr DirectedEdge(const CXXGraph::id_t id, shared> node1, shared> node2); - DirectedEdge(const CXXGraph::id_t id, + constexpr DirectedEdge( + const CXXGraph::id_t id, const std::pair *, const Node *> &nodepair); - DirectedEdge( + constexpr DirectedEdge( const CXXGraph::id_t id, const std::pair>, shared>> &nodepair); - DirectedEdge(const Edge &edge); + constexpr DirectedEdge(const Edge &edge); virtual ~DirectedEdge() = default; - const Node &getFrom() const; - const Node &getTo() const; + constexpr const Node &getFrom() const; + constexpr const Node &getTo() const; + // const std::optional isDirected() const override; const std::optional isWeighted() const override; // operator @@ -67,4 +69,4 @@ class DirectedEdge : public Edge { }; } // namespace CXXGraph -#endif // __CXXGRAPH_DIRECTEDEDGE_H__ +#endif // CXXGRAPH_DIRECTEDEDGE_H_ diff --git a/include/CXXGraph/Edge/DirectedEdge_impl.hpp b/include/CXXGraph/Edge/DirectedEdge_impl.hpp index 9faa1a34..9bbf50e6 100644 --- a/include/CXXGraph/Edge/DirectedEdge_impl.hpp +++ b/include/CXXGraph/Edge/DirectedEdge_impl.hpp @@ -26,44 +26,42 @@ namespace CXXGraph { -using std::make_shared; -using std::make_unique; - template -DirectedEdge::DirectedEdge(const CXXGraph::id_t id, const Node &node1, +constexpr DirectedEdge::DirectedEdge(const CXXGraph::id_t id, + const Node &node1, const Node &node2) : Edge(id, node1, node2) {} template -DirectedEdge::DirectedEdge(const CXXGraph::id_t id, +constexpr DirectedEdge::DirectedEdge(const CXXGraph::id_t id, shared> node1, shared> node2) : Edge(id, node1, node2) {} template -DirectedEdge::DirectedEdge( +constexpr DirectedEdge::DirectedEdge( const CXXGraph::id_t id, const std::pair *, const Node *> &nodepair) : Edge(id, nodepair) {} template -DirectedEdge::DirectedEdge( +constexpr DirectedEdge::DirectedEdge( const CXXGraph::id_t id, const std::pair>, shared>> &nodepair) : Edge(id, nodepair) {} template -DirectedEdge::DirectedEdge(const Edge &edge) +constexpr DirectedEdge::DirectedEdge(const Edge &edge) : DirectedEdge(edge.getId(), *(edge.getNodePair().first), *(edge.getNodePair().second)) {} template -const Node &DirectedEdge::getFrom() const { +constexpr const Node &DirectedEdge::getFrom() const { return *(Edge::getNodePair().first); } template -const Node &DirectedEdge::getTo() const { +constexpr const Node &DirectedEdge::getTo() const { return *(Edge::getNodePair().second); } diff --git a/include/CXXGraph/Edge/DirectedWeightedEdge.h b/include/CXXGraph/Edge/DirectedWeightedEdge.h index e8a05bf1..2a8bb362 100755 --- a/include/CXXGraph/Edge/DirectedWeightedEdge.h +++ b/include/CXXGraph/Edge/DirectedWeightedEdge.h @@ -16,11 +16,11 @@ /***********************************************************/ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_DIRECTEDWEIGHTEDEDGE_H__ -#define __CXXGRAPH_DIRECTEDWEIGHTEDEDGE_H__ +#ifndef CXXGRAPH_DIRECTEDWEIGHTEDEDGE_H_ +#define CXXGRAPH_DIRECTEDWEIGHTEDEDGE_H_ #pragma once #include "DirectedWeightedEdge_impl.hpp" -#endif // __CXXGRAPH_DIRECTEDWEIGHTEDEDGE_H__ +#endif // CXXGRAPH_DIRECTEDWEIGHTEDEDGE_H_ diff --git a/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h b/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h index 6e3622fc..ef3963f3 100644 --- a/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h +++ b/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h @@ -16,8 +16,8 @@ /***********************************************************/ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_DIRECTEDWEIGHTEDEDGE_DECL_H__ -#define __CXXGRAPH_DIRECTEDWEIGHTEDEDGE_DECL_H__ +#ifndef CXXGRAPH_DIRECTEDWEIGHTEDEDGE_DECL_H_ +#define CXXGRAPH_DIRECTEDWEIGHTEDEDGE_DECL_H_ #pragma once @@ -31,7 +31,7 @@ using unique = std::unique_ptr; template using shared = std::shared_ptr; -// Foward Declaration +// Forward Declaration template class UndirectedWeightedEdge; @@ -45,23 +45,25 @@ std::ostream &operator<<(std::ostream &o, const DirectedWeightedEdge &edge); template class DirectedWeightedEdge : public DirectedEdge, public Weighted { public: - DirectedWeightedEdge(const CXXGraph::id_t id, const Node &node1, + constexpr DirectedWeightedEdge(const CXXGraph::id_t id, const Node &node1, const Node &node2, const double weight); - DirectedWeightedEdge(const CXXGraph::id_t id, shared> node1, + constexpr DirectedWeightedEdge(const CXXGraph::id_t id, + shared> node1, shared> node2, const double weight); - DirectedWeightedEdge( + constexpr DirectedWeightedEdge( const CXXGraph::id_t id, const std::pair *, const Node *> &nodepair, const double weight); - DirectedWeightedEdge( + constexpr DirectedWeightedEdge( const CXXGraph::id_t id, const std::pair>, shared>> &nodepair, const double weight); - DirectedWeightedEdge(const DirectedEdge &edge, const double weight); - DirectedWeightedEdge(const Edge &edge, const double weight); - DirectedWeightedEdge(const DirectedEdge &edge); - DirectedWeightedEdge(const Edge &edge); - DirectedWeightedEdge(const UndirectedWeightedEdge &edge); + constexpr DirectedWeightedEdge(const DirectedEdge &edge, + const double weight); + constexpr DirectedWeightedEdge(const Edge &edge, const double weight); + constexpr DirectedWeightedEdge(const DirectedEdge &edge); + constexpr DirectedWeightedEdge(const Edge &edge); + constexpr DirectedWeightedEdge(const UndirectedWeightedEdge &edge); virtual ~DirectedWeightedEdge() = default; const std::optional isWeighted() const override; // operator @@ -76,4 +78,4 @@ class DirectedWeightedEdge : public DirectedEdge, public Weighted { } // namespace CXXGraph -#endif // __CXXGRAPH_DIRECTEDWEIGHTEDEDGE_DECL_H__ +#endif // CXXGRAPH_DIRECTEDWEIGHTEDEDGE_DECL_H_ diff --git a/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp b/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp index 8bf19f07..3ba0aa10 100644 --- a/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp +++ b/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp @@ -26,57 +26,57 @@ namespace CXXGraph { -using std::make_shared; -using std::make_unique; - template -DirectedWeightedEdge::DirectedWeightedEdge(const CXXGraph::id_t id, +constexpr DirectedWeightedEdge::DirectedWeightedEdge(const CXXGraph::id_t id, const Node &node1, const Node &node2, const double weight) : DirectedEdge(id, node1, node2), Weighted(weight) {} template -DirectedWeightedEdge::DirectedWeightedEdge(const CXXGraph::id_t id, +constexpr DirectedWeightedEdge::DirectedWeightedEdge( + const CXXGraph::id_t id, shared> node1, shared> node2, const double weight) : DirectedEdge(id, node1, node2), Weighted(weight) {} template -DirectedWeightedEdge::DirectedWeightedEdge( +constexpr DirectedWeightedEdge::DirectedWeightedEdge( const CXXGraph::id_t id, const std::pair *, const Node *> &nodepair, const double weight) : DirectedEdge(id, nodepair), Weighted(weight) {} template -DirectedWeightedEdge::DirectedWeightedEdge( +constexpr DirectedWeightedEdge::DirectedWeightedEdge( const CXXGraph::id_t id, const std::pair>, shared>> &nodepair, const double weight) : DirectedEdge(id, nodepair), Weighted(weight) {} template -DirectedWeightedEdge::DirectedWeightedEdge(const DirectedEdge &edge, +constexpr DirectedWeightedEdge::DirectedWeightedEdge( + const DirectedEdge &edge, const double weight) : DirectedEdge(edge), Weighted(weight) {} template -DirectedWeightedEdge::DirectedWeightedEdge(const Edge &edge, +constexpr DirectedWeightedEdge::DirectedWeightedEdge(const Edge &edge, const double weight) : DirectedEdge(edge), Weighted(weight) {} template -DirectedWeightedEdge::DirectedWeightedEdge(const DirectedEdge &edge) +constexpr DirectedWeightedEdge::DirectedWeightedEdge( + const DirectedEdge &edge) : DirectedEdge(edge), Weighted() {} template -DirectedWeightedEdge::DirectedWeightedEdge(const Edge &edge) +constexpr DirectedWeightedEdge::DirectedWeightedEdge(const Edge &edge) : DirectedEdge(edge), Weighted() {} template -DirectedWeightedEdge::DirectedWeightedEdge( +constexpr DirectedWeightedEdge::DirectedWeightedEdge( const UndirectedWeightedEdge &edge) : DirectedEdge(edge), Weighted(edge.getWeight()) {} diff --git a/include/CXXGraph/Edge/Edge.h b/include/CXXGraph/Edge/Edge.h index 7b18abc1..327949c8 100755 --- a/include/CXXGraph/Edge/Edge.h +++ b/include/CXXGraph/Edge/Edge.h @@ -17,11 +17,11 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_EDGE_H__ -#define __CXXGRAPH_EDGE_H__ +#ifndef CXXGRAPH_EDGE_H_ +#define CXXGRAPH_EDGE_H_ #pragma once #include "CXXGraph/Edge/Edge_impl.hpp" -#endif // __CXXGRAPH_EDGE_H__ +#endif // CXXGRAPH_EDGE_H_ diff --git a/include/CXXGraph/Edge/Edge_decl.h b/include/CXXGraph/Edge/Edge_decl.h index 3be7b185..4f24053a 100644 --- a/include/CXXGraph/Edge/Edge_decl.h +++ b/include/CXXGraph/Edge/Edge_decl.h @@ -17,8 +17,8 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_EDGE_DECL_H__ -#define __CXXGRAPH_EDGE_DECL_H__ +#ifndef CXXGRAPH_EDGE_DECL_H_ +#define CXXGRAPH_EDGE_DECL_H_ #pragma once @@ -49,17 +49,18 @@ class Edge { public: typedef T Node_t; - Edge(const CXXGraph::id_t id, const Node &node1, const Node &node2); - Edge(const CXXGraph::id_t id, shared> node1, + constexpr Edge(const CXXGraph::id_t id, const Node &node1, const Node &node2); + constexpr Edge(const CXXGraph::id_t id, shared> node1, shared> node2); - Edge(const CXXGraph::id_t id, + constexpr Edge(const CXXGraph::id_t id, const std::pair *, const Node *> &nodepair); - Edge(const CXXGraph::id_t id, + constexpr Edge( + const CXXGraph::id_t id, const std::pair>, shared>> &nodepair); virtual ~Edge() = default; void setFirstNode(shared> node); void setSecondNode(shared> node); - unsigned long long getId() const; + constexpr unsigned long long getId() const; const std::pair>, shared>> &getNodePair() const; shared> getOtherNode(shared> node) const; @@ -67,11 +68,11 @@ class Edge { virtual const std::optional isWeighted() const; // operator virtual bool operator==(const Edge &b) const; - bool operator<(const Edge &b) const; + constexpr bool operator<(const Edge &b) const; friend std::ostream &operator<< <>(std::ostream &os, const Edge &edge); }; } // namespace CXXGraph -#endif // __CXXGRAPH_EDGE_DECL_H__ +#endif // CXXGRAPH_EDGE_DECL_H_ diff --git a/include/CXXGraph/Edge/Edge_impl.hpp b/include/CXXGraph/Edge/Edge_impl.hpp index 7f35228b..cd2f5b5c 100644 --- a/include/CXXGraph/Edge/Edge_impl.hpp +++ b/include/CXXGraph/Edge/Edge_impl.hpp @@ -35,36 +35,30 @@ using std::make_shared; using std::make_unique; template -Edge::Edge(const CXXGraph::id_t id, const Node &node1, - const Node &node2) { - this->nodePair.first = make_shared>(node1); - this->nodePair.second = make_shared>(node2); - this->id = id; -} +constexpr Edge::Edge(const CXXGraph::id_t otherId, const Node &node1, + const Node &node2) + : id(otherId), nodePair{std::make_shared>(node1), + std::make_shared>(node2)} + {} template -Edge::Edge(const CXXGraph::id_t id, shared> node1, - shared> node2) { - this->nodePair.first = node1; - this->nodePair.second = node2; - this->id = id; -} +constexpr Edge::Edge(const CXXGraph::id_t otherId, shared> node1, + shared> node2) + : id(otherId), nodePair(node1, node2) {} template -Edge::Edge(const CXXGraph::id_t id, - const std::pair *, const Node *> &nodepair) { - this->nodePair.first = make_shared>(*(nodepair.first)); - this->nodePair.second = make_shared>(*(nodepair.second)); - this->id = id; -} +constexpr Edge::Edge(const CXXGraph::id_t otherId, + const std::pair *, const Node *> &nodepair) + : id(otherId) + , nodePair(std::make_shared>(*(nodepair.first)), + std::make_shared>(*(nodepair.second))) +{ } template -Edge::Edge( - const CXXGraph::id_t id, +constexpr Edge::Edge( + const CXXGraph::id_t otherId, const std::pair>, shared>> &nodepair) - : nodePair(nodepair) { - this->id = id; -} + : id(otherId), nodePair(nodepair) { } template void Edge::setFirstNode(shared> node) { @@ -79,7 +73,7 @@ void Edge::setSecondNode(shared> node) { } template -unsigned long long Edge::getId() const { +constexpr unsigned long long Edge::getId() const { return id; } @@ -114,7 +108,7 @@ bool Edge::operator==(const Edge &b) const { } template -bool Edge::operator<(const Edge &b) const { +constexpr bool Edge::operator<(const Edge &b) const { return (this->id < b.id); } diff --git a/include/CXXGraph/Edge/UndirectedEdge.h b/include/CXXGraph/Edge/UndirectedEdge.h index 858b3aae..f4f4b37a 100755 --- a/include/CXXGraph/Edge/UndirectedEdge.h +++ b/include/CXXGraph/Edge/UndirectedEdge.h @@ -17,11 +17,11 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_UNDIRECTEDEDGE_H__ -#define __CXXGRAPH_UNDIRECTEDEDGE_H__ +#ifndef CXXGRAPH_UNDIRECTEDEDGE_H_ +#define CXXGRAPH_UNDIRECTEDEDGE_H_ #pragma once #include "UndirectedEdge_impl.hpp" -#endif // __CXXGRAPH_UNDIRECTEDEDGE_H__ +#endif // CXXGRAPH_UNDIRECTEDEDGE_H_ diff --git a/include/CXXGraph/Edge/UndirectedEdge_decl.h b/include/CXXGraph/Edge/UndirectedEdge_decl.h index a2371113..42bc780a 100644 --- a/include/CXXGraph/Edge/UndirectedEdge_decl.h +++ b/include/CXXGraph/Edge/UndirectedEdge_decl.h @@ -17,8 +17,8 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_UNDIRECTEDEDGE_DECL_H__ -#define __CXXGRAPH_UNDIRECTEDEDGE_DECL_H__ +#ifndef CXXGRAPH_UNDIRECTEDEDGE_DECL_H_ +#define CXXGRAPH_UNDIRECTEDEDGE_DECL_H_ #pragma once @@ -67,4 +67,4 @@ class UndirectedEdge : public Edge { } // namespace CXXGraph -#endif // __CXXGRAPH_UNDIRECTEDEDGE_DECL_H__ +#endif // CXXGRAPH_UNDIRECTEDEDGE_DECL_H_ diff --git a/include/CXXGraph/Edge/UndirectedWeightedEdge.h b/include/CXXGraph/Edge/UndirectedWeightedEdge.h index 03b3e05a..cf78de2a 100755 --- a/include/CXXGraph/Edge/UndirectedWeightedEdge.h +++ b/include/CXXGraph/Edge/UndirectedWeightedEdge.h @@ -17,11 +17,11 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_H__ -#define __CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_H__ +#ifndef CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_H_ +#define CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_H_ #pragma once #include "UndirectedWeightedEdge_impl.hpp" -#endif // __CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_H__ +#endif // CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_H_ diff --git a/include/CXXGraph/Edge/UndirectedWeightedEdge_decl.h b/include/CXXGraph/Edge/UndirectedWeightedEdge_decl.h index e35ef3d4..d0c142a5 100644 --- a/include/CXXGraph/Edge/UndirectedWeightedEdge_decl.h +++ b/include/CXXGraph/Edge/UndirectedWeightedEdge_decl.h @@ -17,8 +17,8 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_DECL_H__ -#define __CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_DECL_H__ +#ifndef CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_DECL_H_ +#define CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_DECL_H_ #pragma once @@ -78,4 +78,4 @@ class UndirectedWeightedEdge : public UndirectedEdge, public Weighted { } // namespace CXXGraph -#endif // __CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_DECL_H__ +#endif // CXXGRAPH_UNDIRECTEDWEIGHTEDEDGE_DECL_H_ diff --git a/include/CXXGraph/Edge/Weighted.h b/include/CXXGraph/Edge/Weighted.h index 8b889a5d..5d6611a5 100755 --- a/include/CXXGraph/Edge/Weighted.h +++ b/include/CXXGraph/Edge/Weighted.h @@ -17,11 +17,11 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_WEIGHTED_H__ -#define __CXXGRAPH_WEIGHTED_H__ +#ifndef CXXGRAPH_WEIGHTED_H_ +#define CXXGRAPH_WEIGHTED_H_ #pragma once #include "Weighted_impl.hpp" -#endif // __CXXGRAPH_WEIGHTED_H__ \ No newline at end of file +#endif // CXXGRAPH_WEIGHTED_H_ \ No newline at end of file diff --git a/include/CXXGraph/Edge/Weighted_decl.h b/include/CXXGraph/Edge/Weighted_decl.h index 2cba9ee2..2b68d31e 100644 --- a/include/CXXGraph/Edge/Weighted_decl.h +++ b/include/CXXGraph/Edge/Weighted_decl.h @@ -17,8 +17,8 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_WEIGHTED_DECL_H__ -#define __CXXGRAPH_WEIGHTED_DECL_H__ +#ifndef CXXGRAPH_WEIGHTED_DECL_H_ +#define CXXGRAPH_WEIGHTED_DECL_H_ #pragma once @@ -28,12 +28,12 @@ class Weighted { double weight = 0.0; public: - Weighted(); - explicit Weighted(const double weight); - virtual ~Weighted() = default; - double getWeight() const; - void setWeight(const double weight); + constexpr Weighted() noexcept; + constexpr explicit Weighted(const double weight) noexcept; + virtual ~Weighted() noexcept = default; + constexpr double getWeight() const noexcept; + constexpr void setWeight(const double weight); }; } // namespace CXXGraph -#endif // __CXXGRAPH_WEIGHTED_DECL_H__ \ No newline at end of file +#endif // CXXGRAPH_WEIGHTED_DECL_H_ \ No newline at end of file diff --git a/include/CXXGraph/Edge/Weighted_impl.hpp b/include/CXXGraph/Edge/Weighted_impl.hpp index bcd7cda6..3b295b1e 100644 --- a/include/CXXGraph/Edge/Weighted_impl.hpp +++ b/include/CXXGraph/Edge/Weighted_impl.hpp @@ -26,17 +26,16 @@ namespace CXXGraph { -// inline because the implementation of non-template function in header file -inline Weighted::Weighted() { weight = 0.0; } +constexpr Weighted::Weighted() noexcept : weight(0.0) {} -// inline because the implementation of non-template function in header file -inline Weighted::Weighted(const double weight) { this->weight = weight; } +constexpr Weighted::Weighted(const double inputWeight) noexcept + : weight(inputWeight) {} -// inline because the implementation of non-template function in header file -inline double Weighted::getWeight() const { return weight; } +constexpr double Weighted::getWeight() const noexcept { return weight; } -// inline because the implementation of non-template function in header file -inline void Weighted::setWeight(const double weight) { this->weight = weight; } +constexpr void Weighted::setWeight(const double inputWeight) { + weight = inputWeight; +} } // namespace CXXGraph diff --git a/include/CXXGraph/Graph/Graph.h b/include/CXXGraph/Graph/Graph.h index c942f02a..277953b9 100644 --- a/include/CXXGraph/Graph/Graph.h +++ b/include/CXXGraph/Graph/Graph.h @@ -17,8 +17,10 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_GRAPH_H__ -#define __CXXGRAPH_GRAPH_H__ +#ifndef CXXGRAPH_GRAPH_H_ +#define CXXGRAPH_GRAPH_H_ + +#pragma once #include "CXXGraph/Graph/Graph_impl.hpp" @@ -49,4 +51,4 @@ #include "CXXGraph/Graph/IO/InputOperation_impl.hpp" #include "CXXGraph/Graph/IO/OutputOperation_impl.hpp" -#endif // __CXXGRAPH_GRAPH_H__ +#endif // CXXGRAPH_GRAPH_H_ diff --git a/include/CXXGraph/Graph/Graph_decl.h b/include/CXXGraph/Graph/Graph_decl.h index 35fe6690..6c70e961 100644 --- a/include/CXXGraph/Graph/Graph_decl.h +++ b/include/CXXGraph/Graph/Graph_decl.h @@ -17,8 +17,8 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_GRAPH_DECL_H__ -#define __CXXGRAPH_GRAPH_DECL_H__ +#ifndef CXXGRAPH_GRAPH_DECL_H_ +#define CXXGRAPH_GRAPH_DECL_H_ #pragma once @@ -1028,4 +1028,4 @@ class Graph { }; } // namespace CXXGraph -#endif // __CXXGRAPH_GRAPH_DECL_H__ +#endif // CXXGRAPH_GRAPH_DECL_H_ diff --git a/include/CXXGraph/Graph/Graph_impl.hpp b/include/CXXGraph/Graph/Graph_impl.hpp index 950cd8da..0bcba37d 100644 --- a/include/CXXGraph/Graph/Graph_impl.hpp +++ b/include/CXXGraph/Graph/Graph_impl.hpp @@ -31,28 +31,21 @@ namespace CXXGraph { using std::make_shared; -using std::make_unique; template -Graph::Graph() { - /* Caching the adjacency matrix */ - cacheAdjMatrix(); - cacheDegreeMatrix(); - cacheLaplacianMatrix(); - cacheTransitionMatrix(); -} +Graph::Graph() + : cachedAdjMatrix(Graph::getAdjMatrix()), + cachedDegreeMatrix(Graph::getDegreeMatrix()), + cachedLaplacianMatrix(Graph::getLaplacianMatrix()), + cachedTransitionMatrix(Graph::getTransitionMatrix()) {} template -Graph::Graph(const T_EdgeSet &edgeSet) { - for (auto edgeIt : edgeSet) { - this->edgeSet.insert(edgeIt); - } - /* Caching the adjacency matrix */ - cacheAdjMatrix(); - cacheDegreeMatrix(); - cacheLaplacianMatrix(); - cacheTransitionMatrix(); -} +Graph::Graph(const T_EdgeSet &edgeSetToCopy) + : edgeSet(edgeSetToCopy), + cachedAdjMatrix(Graph::getAdjMatrix()), + cachedDegreeMatrix(Graph::getDegreeMatrix()), + cachedLaplacianMatrix(Graph::getLaplacianMatrix()), + cachedTransitionMatrix(Graph::getTransitionMatrix()) {} template const T_EdgeSet &Graph::getEdgeSet() const { diff --git a/include/CXXGraph/Node/Node.h b/include/CXXGraph/Node/Node.h index 96fa0687..da17f653 100755 --- a/include/CXXGraph/Node/Node.h +++ b/include/CXXGraph/Node/Node.h @@ -17,11 +17,11 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_NODE_H__ -#define __CXXGRAPH_NODE_H__ +#ifndef CXXGRAPH_NODE_H_ +#define CXXGRAPH_NODE_H_ #pragma once #include "Node_impl.hpp" -#endif // __CXXGRAPH_NODE_H__ +#endif // CXXGRAPH_NODE_H_ diff --git a/include/CXXGraph/Node/Node_decl.h b/include/CXXGraph/Node/Node_decl.h index d8650894..c6a0e38d 100644 --- a/include/CXXGraph/Node/Node_decl.h +++ b/include/CXXGraph/Node/Node_decl.h @@ -17,44 +17,45 @@ /*** License: MPL v2.0 ***/ /***********************************************************/ -#ifndef __CXXGRAPH_NODE_DECL_H__ -#define __CXXGRAPH_NODE_DECL_H__ +#ifndef CXXGRAPH_NODE_DECL_H_ +#define CXXGRAPH_NODE_DECL_H_ #pragma once #include - +#include #include "CXXGraph/Utility/id_t.hpp" namespace CXXGraph { -template +template class Node; -template -std::ostream &operator<<(std::ostream &os, const Node &node); -template +template +std::ostream &operator<<(std::ostream &os, const Node &node); +template class Node { private: CXXGraph::id_t id = 0; - std::string userId = ""; + UserID userId{}; T data; - void setId(const std::string &); + constexpr void setId(const UserID &); public: - typedef T Node_t; + using Node_t = T; - Node(const std::string &, const T &data); + constexpr Node(const UserID &, const T &data); // Move constructor - Node(const std::string &, T &&data) noexcept; - ~Node() = default; - const CXXGraph::id_t &getId() const; - const std::string &getUserId() const; - const T &getData() const; - void setData(T &&new_data); + constexpr Node(const UserID &, T &&data) noexcept(std::is_nothrow_move_assignable::value); + ~Node() noexcept = default; + constexpr const CXXGraph::id_t &getId() const; + constexpr const UserID &getUserId() const; + constexpr const T &getData() const; + constexpr void setData(T &&new_data); // operator - bool operator==(const Node &b) const; - bool operator<(const Node &b) const; - friend std::ostream &operator<< <>(std::ostream &os, const Node &node); + constexpr bool operator==(const Node &b) const; + constexpr bool operator<(const Node &b) const; + + friend std::ostream &operator<< <>(std::ostream &os, const Node &node); }; } // namespace CXXGraph -#endif // __CXXGRAPH_NODE_DECL_H__ +#endif // CXXGRAPH_NODE_DECL_H_ diff --git a/include/CXXGraph/Node/Node_impl.hpp b/include/CXXGraph/Node/Node_impl.hpp index 792cabdf..63a50526 100644 --- a/include/CXXGraph/Node/Node_impl.hpp +++ b/include/CXXGraph/Node/Node_impl.hpp @@ -20,71 +20,117 @@ #ifndef __CXXGRAPH_NODE_IMPL_H__ #define __CXXGRAPH_NODE_IMPL_H__ +#if __cplusplus >= 202004L +#include +#else +#include +#endif + #include +#include +#include #include "Node_decl.h" namespace CXXGraph { +// internals not intended to be accessed by the end user +namespace internals { +template +constexpr CXXGraph::id_t UserIdToId(const AnyUserID &id) { + // in C++ 23 we have constexpr hashes + if constexpr (__cplusplus >= 202302L) return std::hash{}(id); + // hashing is trivial for integral types + if constexpr (std::is_integral::value) { + // assert nothing is greater than a size_t to avoid issues with GCC and + // clang's 128 bit ints, a size_t is usually 64 bits + static_assert(std::numeric_limits::max() >= + std::numeric_limits::max()); + + if constexpr (std::is_signed::value) { + // Add id to half max so that we have something unique + return (std::numeric_limits::max() / 2) + id; + } + return static_cast(id); + } + // typepun the float into an int + if constexpr (std::is_floating_point::value) { + // static assert to avoid long double (80 bits rather than 64) + static_assert(sizeof(CXXGraph::id_t) >= sizeof(AnyUserID)); +// In C++20 we can do typepunning at compile time +#if __cplusplus >= 202004L + return std::bit_cast(id); +#else // < C++20 + // otherwise use memcpy (not compile time) + CXXGraph::id_t result{}; + std::memcpy(&result, &id, sizeof(id)); + return result; +#endif // >= C++20 + } + // resort back to runtime hash + return std::hash{}(id); +} +} // namespace internals -template +template class Node; -template -Node::Node(const std::string &id, const T &data) { - this->userId = id; - // the userid is set as sha512 hash of the user provided id - setId(id); - this->data = data; +template +constexpr Node::Node(const UserID &otherId, const T &otherData) + : id(internals::UserIdToId(otherId)), userId(otherId), data(otherData) { + static_assert( + !std::is_pointer::value, + "Pointer is not allowed as an ID type, did you mean std::string?"); } -template -Node::Node(const std::string &id, T &&data) noexcept { - this->userId = id; - // the userid is set as sha512 hash of the user provided id - setId(id); - this->data = std::move(data); +template +constexpr Node::Node(const UserID &otherId, T &&otherData) noexcept( + std::is_nothrow_move_assignable::value) + : id(internals::UserIdToId(otherId)), userId(otherId), data(otherData) { + static_assert( + !std::is_pointer::value, + "Pointer is not allowed as an ID type, did you mean std::string?"); } -template -void Node::setId(const std::string &inpId) { - this->id = std::hash{}(inpId); +template +constexpr void Node::setId(const UserID &inputId) { + this->id = UserIdToId(inputId); } -template -const CXXGraph::id_t &Node::getId() const { +template +constexpr const CXXGraph::id_t &Node::getId() const { return id; } -template -const std::string &Node::getUserId() const { +template +constexpr const UserID &Node::getUserId() const { return userId; } -template -const T &Node::getData() const { +template +constexpr const T &Node::getData() const { return data; } -template -void Node::setData(T &&new_data) { - this->data = std::move(new_data); +template +constexpr void Node::setData(T &&newData) { + this->data = std::move(newData); } // The data type T must have an overload of the equality operator -template -bool Node::operator==(const Node &b) const { +template +constexpr bool Node::operator==(const Node &b) const { return (this->id == b.id && this->data == b.data); } -template -bool Node::operator<(const Node &b) const { +template +constexpr bool Node::operator<(const Node &b) const { return (this->id < b.id); } // ostream overload // The data type T must have an overload of the ostream operator -template -std::ostream &operator<<(std::ostream &os, const Node &node) { +template +std::ostream &operator<<(std::ostream &os, const Node &node) { os << "Node: {\n" << " Id:\t" << node.userId << "\n Data:\t" << node.data << "\n}"; return os; diff --git a/include/CXXGraph/Utility/Typedef.hpp b/include/CXXGraph/Utility/Typedef.hpp index d255184a..ad7779d2 100644 --- a/include/CXXGraph/Utility/Typedef.hpp +++ b/include/CXXGraph/Utility/Typedef.hpp @@ -36,7 +36,7 @@ namespace CXXGraph { template using shared = std::shared_ptr; -template +template class Node; template diff --git a/test/ConstexprTests.cpp b/test/ConstexprTests.cpp new file mode 100644 index 00000000..061ba778 --- /dev/null +++ b/test/ConstexprTests.cpp @@ -0,0 +1,26 @@ + +#include + +#include "CXXGraph/CXXGraph.hpp" +#include "gtest/gtest.h" + +// Boolean results will always be true +// functions are only here to test that this compiles +constexpr bool IsConstexprContextCreateable() { + CXXGraph::Node node1(1, 1); + return true; +} + +constexpr bool IsConstexprCreateable() { + constexpr CXXGraph::Node node1(1, 1); + return true; +} + + +TEST(ConstexprTests, IsConstexprContextCreateable) { + ASSERT_TRUE(IsConstexprContextCreateable()); +} + +TEST(ConstexprTests, IsConstexprCreateable) { + ASSERT_TRUE(IsConstexprCreateable()); +} \ No newline at end of file