From a1a5f0c25b04260d4bca8ddf3919b46cb90f183b Mon Sep 17 00:00:00 2001 From: Olek Raymond Date: Fri, 3 Jan 2025 18:42:51 +0000 Subject: [PATCH 1/9] Changes defines in include guards to avoid undefined behaviour --- include/CXXGraph/CXXGraphConfig.h | 8 ++++---- include/CXXGraph/Edge/DirectedWeightedEdge.h | 6 +++--- include/CXXGraph/Edge/DirectedWeightedEdge_decl.h | 6 +++--- include/CXXGraph/Edge/Edge.h | 6 +++--- include/CXXGraph/Edge/Edge_decl.h | 6 +++--- include/CXXGraph/Edge/UndirectedEdge.h | 6 +++--- include/CXXGraph/Edge/UndirectedEdge_decl.h | 6 +++--- include/CXXGraph/Edge/UndirectedWeightedEdge.h | 6 +++--- include/CXXGraph/Edge/UndirectedWeightedEdge_decl.h | 6 +++--- include/CXXGraph/Edge/Weighted.h | 6 +++--- include/CXXGraph/Edge/Weighted_decl.h | 6 +++--- include/CXXGraph/Graph/Graph.h | 8 +++++--- include/CXXGraph/Graph/Graph_decl.h | 6 +++--- include/CXXGraph/Node/Node.h | 6 +++--- include/CXXGraph/Node/Node_decl.h | 6 +++--- 15 files changed, 48 insertions(+), 46 deletions(-) diff --git a/include/CXXGraph/CXXGraphConfig.h b/include/CXXGraph/CXXGraphConfig.h index f6ff9ff4e..44cfc71de 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/DirectedWeightedEdge.h b/include/CXXGraph/Edge/DirectedWeightedEdge.h index e8a05bf1f..2a8bb3622 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 6e3622fce..e456c8ab4 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 @@ -76,4 +76,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/Edge.h b/include/CXXGraph/Edge/Edge.h index 7b18abc1c..327949c84 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 3be7b1857..11d90d880 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 @@ -74,4 +74,4 @@ class Edge { } // namespace CXXGraph -#endif // __CXXGRAPH_EDGE_DECL_H__ +#endif // CXXGRAPH_EDGE_DECL_H_ diff --git a/include/CXXGraph/Edge/UndirectedEdge.h b/include/CXXGraph/Edge/UndirectedEdge.h index 858b3aae6..f4f4b37ae 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 a23711135..42bc780a0 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 03b3e05ad..cf78de2af 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 e35ef3d4f..d0c142a56 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 8b889a5d6..5d6611a5a 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 2cba9ee2c..6d6f97084 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 @@ -36,4 +36,4 @@ class Weighted { }; } // 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/Graph/Graph.h b/include/CXXGraph/Graph/Graph.h index c942f02aa..277953b97 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 35fe6690f..6c70e9619 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/Node/Node.h b/include/CXXGraph/Node/Node.h index 96fa06875..da17f6539 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 d86508949..78a43e333 100644 --- a/include/CXXGraph/Node/Node_decl.h +++ b/include/CXXGraph/Node/Node_decl.h @@ -17,8 +17,8 @@ /*** 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 @@ -57,4 +57,4 @@ class Node { } // namespace CXXGraph -#endif // __CXXGRAPH_NODE_DECL_H__ +#endif // CXXGRAPH_NODE_DECL_H_ From d884019e1e0e0b5e1b69d17eb7ce42c2fc1976d6 Mon Sep 17 00:00:00 2001 From: Olek Raymond Date: Fri, 3 Jan 2025 19:16:28 +0000 Subject: [PATCH 2/9] Added constexpr to some functions --- include/CXXGraph/Node/Node_decl.h | 9 +++++---- include/CXXGraph/Node/Node_impl.hpp | 12 ++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/CXXGraph/Node/Node_decl.h b/include/CXXGraph/Node/Node_decl.h index 65edf1fb7..1d31ecb36 100644 --- a/include/CXXGraph/Node/Node_decl.h +++ b/include/CXXGraph/Node/Node_decl.h @@ -28,8 +28,8 @@ namespace CXXGraph { template class Node; -template -std::ostream &operator<<(std::ostream &os, const Node &node); +template +constexpr Stream &operator<<(Stream &os, const Node &node); template class Node { private: @@ -39,7 +39,7 @@ class Node { void setId(const std::string &); public: - typedef T Node_t; + using Node_t = T; Node(const std::string &, const T &data); // Move constructor @@ -53,7 +53,8 @@ class Node { // operator bool operator==(const Node &b) const; bool operator<(const Node &b) const; - friend std::ostream &operator<< <>(std::ostream &os, const Node &node); + template + friend Stream &operator<< <>(Stream &os, const Node &node); }; } // namespace CXXGraph diff --git a/include/CXXGraph/Node/Node_impl.hpp b/include/CXXGraph/Node/Node_impl.hpp index 058f092ef..d3c0350d1 100644 --- a/include/CXXGraph/Node/Node_impl.hpp +++ b/include/CXXGraph/Node/Node_impl.hpp @@ -51,7 +51,7 @@ void Node::setId(const std::string &inpId) { } template -const CXXGraph::id_t &Node::getId() const { +constexpr const CXXGraph::id_t &Node::getId() const { return id; } @@ -61,7 +61,7 @@ const std::string &Node::getUserId() const { } template -const T &Node::getData() const { +constexpr const T &Node::getData() const { return data; } @@ -77,19 +77,19 @@ void Node::setData(T &&new_data) { // The data type T must have an overload of the equality operator template -bool Node::operator==(const Node &b) const { +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 { +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 +constexpr Stream &operator<<(Stream &os, const Node &node) { os << "Node: {\n" << " Id:\t" << node.userId << "\n Data:\t" << node.data << "\n}"; return os; From d0c98c5f8b5cad6114300fabac1e0b43a2a48688 Mon Sep 17 00:00:00 2001 From: Ray <57572379+JustCallMeRay@users.noreply.github.com> Date: Fri, 3 Jan 2025 20:16:48 +0000 Subject: [PATCH 3/9] Updates to run tests and fixes --- .github/workflows/cmake.yml | 3 +-- include/CXXGraph/Node/Node_decl.h | 18 +++++++++--------- include/CXXGraph/Node/Node_impl.hpp | 4 ++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 93061cfef..b314627bb 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -2,9 +2,8 @@ name: CMake on: push: - branches: [ master ] pull_request: - branches: [ master ] + env: # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) BUILD_TYPE: Release diff --git a/include/CXXGraph/Node/Node_decl.h b/include/CXXGraph/Node/Node_decl.h index 1d31ecb36..f3fcfbfe5 100644 --- a/include/CXXGraph/Node/Node_decl.h +++ b/include/CXXGraph/Node/Node_decl.h @@ -28,8 +28,8 @@ namespace CXXGraph { template class Node; -template -constexpr Stream &operator<<(Stream &os, const Node &node); +template +std::ostream &operator<<(std::ostream &os, const Node &node); template class Node { private: @@ -45,16 +45,16 @@ class Node { // Move constructor explicit Node(const std::string &, T &&data) noexcept; ~Node() = default; - const CXXGraph::id_t &getId() const; + constexpr const CXXGraph::id_t &getId() const; const std::string &getUserId() const; - const T &getData() const; - T& getData(); + constexpr const T &getData() const; + constexpr T& getData(); void setData(T &&new_data); // operator - bool operator==(const Node &b) const; - bool operator<(const Node &b) const; - template - friend Stream &operator<< <>(Stream &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 diff --git a/include/CXXGraph/Node/Node_impl.hpp b/include/CXXGraph/Node/Node_impl.hpp index d3c0350d1..ff9551373 100644 --- a/include/CXXGraph/Node/Node_impl.hpp +++ b/include/CXXGraph/Node/Node_impl.hpp @@ -88,8 +88,8 @@ constexpr bool Node::operator<(const Node &b) const { // ostream overload // The data type T must have an overload of the ostream operator -template -constexpr Stream &operator<<(Stream &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; From 48124b340c2ec6d488bbbab4ada5dab9639f7560 Mon Sep 17 00:00:00 2001 From: JustCallMeRay <57572379+JustCallMeRay@users.noreply.github.com> Date: Sun, 5 Jan 2025 20:49:50 +0000 Subject: [PATCH 4/9] Changes User id to be it's own type as a template, allowing for constexpr pre C++20 in most cases --- .gitignore | 2 + .../CXXGraph/Edge/DirectedWeightedEdge_decl.h | 2 +- include/CXXGraph/Edge/Weighted_decl.h | 10 +- include/CXXGraph/Edge/Weighted_impl.hpp | 11 +- include/CXXGraph/Node/Node_decl.h | 30 +++--- include/CXXGraph/Node/Node_impl.hpp | 102 +++++++++++++----- include/CXXGraph/Utility/Typedef.hpp | 2 +- test/ConstexprTests.cpp | 26 +++++ 8 files changed, 131 insertions(+), 54 deletions(-) create mode 100644 test/ConstexprTests.cpp diff --git a/.gitignore b/.gitignore index f982ca422..8d678b029 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/Edge/DirectedWeightedEdge_decl.h b/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h index e456c8ab4..647b42b62 100644 --- a/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h +++ b/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h @@ -70,7 +70,7 @@ class DirectedWeightedEdge : public DirectedEdge, public Weighted { Weighted::getWeight()); } - friend std::ostream &operator<< <>(std::ostream &os, + friend std::ostream &operator<< (std::ostream &os, const DirectedWeightedEdge &edge); }; diff --git a/include/CXXGraph/Edge/Weighted_decl.h b/include/CXXGraph/Edge/Weighted_decl.h index 6d6f97084..b3fcbb341 100644 --- a/include/CXXGraph/Edge/Weighted_decl.h +++ b/include/CXXGraph/Edge/Weighted_decl.h @@ -28,11 +28,11 @@ 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; + constexpr void setWeight(const double weight); }; } // namespace CXXGraph diff --git a/include/CXXGraph/Edge/Weighted_impl.hpp b/include/CXXGraph/Edge/Weighted_impl.hpp index bcd7cda63..125ade811 100644 --- a/include/CXXGraph/Edge/Weighted_impl.hpp +++ b/include/CXXGraph/Edge/Weighted_impl.hpp @@ -27,16 +27,19 @@ 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 weight) { + this->weight = weight; +} } // namespace CXXGraph diff --git a/include/CXXGraph/Node/Node_decl.h b/include/CXXGraph/Node/Node_decl.h index f3fcfbfe5..1668a0240 100644 --- a/include/CXXGraph/Node/Node_decl.h +++ b/include/CXXGraph/Node/Node_decl.h @@ -22,39 +22,39 @@ #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: using Node_t = T; - Node(const std::string &, const T &data); + constexpr Node(const UserID &, const T &data); // Move constructor - explicit Node(const std::string &, T &&data) noexcept; - ~Node() = default; + constexpr explicit Node(const UserID &, T &&data) noexcept(std::is_nothrow_move_assignable::value); + ~Node() noexcept = default; constexpr const CXXGraph::id_t &getId() const; - const std::string &getUserId() const; + constexpr const UserID &getUserId() const; constexpr const T &getData() const; constexpr T& getData(); - void setData(T &&new_data); + constexpr void setData(T &&new_data); // operator - constexpr bool operator==(const Node &b) const; - constexpr bool operator<(const Node &b) const; + constexpr bool operator==(const Node &b) const; + constexpr bool operator<(const Node &b) const; - friend std::ostream &operator<< <>(std::ostream &os, const Node &node); + friend std::ostream &operator<< <>(std::ostream &os, const Node &node); }; } // namespace CXXGraph diff --git a/include/CXXGraph/Node/Node_impl.hpp b/include/CXXGraph/Node/Node_impl.hpp index ff9551373..1f6b91001 100644 --- a/include/CXXGraph/Node/Node_impl.hpp +++ b/include/CXXGraph/Node/Node_impl.hpp @@ -20,48 +20,94 @@ #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 -constexpr 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 -constexpr const T &Node::getData() const { +template +constexpr const T &Node::getData() const { return data; } @@ -76,20 +122,20 @@ void Node::setData(T &&new_data) { } // The data type T must have an overload of the equality operator -template -constexpr 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 -constexpr 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 d255184ac..ad7779d27 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 000000000..061ba7780 --- /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 From ac7aff1273aa6088fee3a9b4cd027b3575db6b17 Mon Sep 17 00:00:00 2001 From: Olek Raymond Date: Mon, 6 Jan 2025 13:35:04 +0000 Subject: [PATCH 5/9] Adds constexpr to some graph types --- include/CXXGraph/Edge/DirectedWeightedEdge_decl.h | 4 ++-- include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp | 3 --- include/CXXGraph/Edge/Weighted_decl.h | 2 +- include/CXXGraph/Edge/Weighted_impl.hpp | 8 ++------ 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h b/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h index 647b42b62..4f0f981b4 100644 --- a/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h +++ b/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h @@ -31,7 +31,7 @@ using unique = std::unique_ptr; template using shared = std::shared_ptr; -// Foward Declaration +// Forward Declaration template class UndirectedWeightedEdge; @@ -70,7 +70,7 @@ class DirectedWeightedEdge : public DirectedEdge, public Weighted { Weighted::getWeight()); } - friend std::ostream &operator<< (std::ostream &os, + friend std::ostream &operator<< <>(std::ostream &os, const DirectedWeightedEdge &edge); }; diff --git a/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp b/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp index 8bf19f073..d05a6d796 100644 --- a/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp +++ b/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp @@ -26,9 +26,6 @@ namespace CXXGraph { -using std::make_shared; -using std::make_unique; - template DirectedWeightedEdge::DirectedWeightedEdge(const CXXGraph::id_t id, const Node &node1, diff --git a/include/CXXGraph/Edge/Weighted_decl.h b/include/CXXGraph/Edge/Weighted_decl.h index b3fcbb341..2b68d31ea 100644 --- a/include/CXXGraph/Edge/Weighted_decl.h +++ b/include/CXXGraph/Edge/Weighted_decl.h @@ -31,7 +31,7 @@ class Weighted { constexpr Weighted() noexcept; constexpr explicit Weighted(const double weight) noexcept; virtual ~Weighted() noexcept = default; - constexpr double getWeight() const; + constexpr double getWeight() const noexcept; constexpr void setWeight(const double weight); }; } // namespace CXXGraph diff --git a/include/CXXGraph/Edge/Weighted_impl.hpp b/include/CXXGraph/Edge/Weighted_impl.hpp index 125ade811..3b295b1eb 100644 --- a/include/CXXGraph/Edge/Weighted_impl.hpp +++ b/include/CXXGraph/Edge/Weighted_impl.hpp @@ -26,19 +26,15 @@ namespace CXXGraph { -// inline because the implementation of non-template function in header file constexpr Weighted::Weighted() noexcept : weight(0.0) {} -// inline because the implementation of non-template function in header file constexpr Weighted::Weighted(const double inputWeight) noexcept : weight(inputWeight) {} -// inline because the implementation of non-template function in header file constexpr double Weighted::getWeight() const noexcept { return weight; } -// inline because the implementation of non-template function in header file -constexpr void Weighted::setWeight(const double weight) { - this->weight = weight; +constexpr void Weighted::setWeight(const double inputWeight) { + weight = inputWeight; } } // namespace CXXGraph From a81e636978603d9404d0e81515c95d987d259bf0 Mon Sep 17 00:00:00 2001 From: Olek Raymond Date: Mon, 6 Jan 2025 14:18:55 +0000 Subject: [PATCH 6/9] Adds constexpr to more types, edges --- include/CXXGraph/Edge/DirectedEdge_decl.h | 22 +++++----- include/CXXGraph/Edge/DirectedEdge_impl.hpp | 18 ++++---- .../CXXGraph/Edge/DirectedWeightedEdge_decl.h | 20 +++++---- .../Edge/DirectedWeightedEdge_impl.hpp | 21 +++++---- include/CXXGraph/Edge/Edge_decl.h | 13 +++--- include/CXXGraph/Edge/Edge_impl.hpp | 44 ++++++++----------- 6 files changed, 69 insertions(+), 69 deletions(-) diff --git a/include/CXXGraph/Edge/DirectedEdge_decl.h b/include/CXXGraph/Edge/DirectedEdge_decl.h index b4611c3af..f33eabd1b 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 9faa1a346..9bbf50e69 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_decl.h b/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h index 4f0f981b4..ef3963f3e 100644 --- a/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h +++ b/include/CXXGraph/Edge/DirectedWeightedEdge_decl.h @@ -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 diff --git a/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp b/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp index d05a6d796..3ba0aa105 100644 --- a/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp +++ b/include/CXXGraph/Edge/DirectedWeightedEdge_impl.hpp @@ -27,53 +27,56 @@ namespace CXXGraph { 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_decl.h b/include/CXXGraph/Edge/Edge_decl.h index 11d90d880..4f24053a7 100644 --- a/include/CXXGraph/Edge/Edge_decl.h +++ b/include/CXXGraph/Edge/Edge_decl.h @@ -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,7 +68,7 @@ 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); }; diff --git a/include/CXXGraph/Edge/Edge_impl.hpp b/include/CXXGraph/Edge/Edge_impl.hpp index 7f35228b9..cd2f5b5c6 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); } From 4faf2298f852de44647e3d95b4864036b2a74adf Mon Sep 17 00:00:00 2001 From: Olek Raymond Date: Mon, 6 Jan 2025 15:14:13 +0000 Subject: [PATCH 7/9] Updates for performance and ease of moving to constexpr later, C++26 or using a constexpr set lib --- include/CXXGraph/Edge/DirectedEdge.h | 6 +++--- include/CXXGraph/Graph/Graph_impl.hpp | 29 ++++++++++----------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/include/CXXGraph/Edge/DirectedEdge.h b/include/CXXGraph/Edge/DirectedEdge.h index 76c8e5930..79e19b5b0 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/Graph/Graph_impl.hpp b/include/CXXGraph/Graph/Graph_impl.hpp index 950cd8daf..0bcba37d6 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 { From 3dc474af17df53637a673d24779de812decd07ef Mon Sep 17 00:00:00 2001 From: Ray <57572379+JustCallMeRay@users.noreply.github.com> Date: Tue, 1 Jul 2025 21:56:35 +0000 Subject: [PATCH 8/9] Fixes error during rebase --- include/CXXGraph/Node/Node_impl.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/CXXGraph/Node/Node_impl.hpp b/include/CXXGraph/Node/Node_impl.hpp index 1f6b91001..3ff1f85c2 100644 --- a/include/CXXGraph/Node/Node_impl.hpp +++ b/include/CXXGraph/Node/Node_impl.hpp @@ -111,13 +111,13 @@ constexpr const T &Node::getData() const { return data; } -template -T &Node::getData() { +template +constexpr T &Node::getData() { return data; } -template -void Node::setData(T &&new_data) { +template +constexpr void Node::setData(T &&new_data) { this->data = std::move(new_data); } From 530dc856bbc5e0d1abf89940d84825eca1f8baf1 Mon Sep 17 00:00:00 2001 From: Ray <57572379+JustCallMeRay@users.noreply.github.com> Date: Fri, 11 Jul 2025 21:51:29 +0000 Subject: [PATCH 9/9] Adds output on failure to test to aid in debuggin --- .github/workflows/cmake.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index b314627bb..334424e55 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -57,5 +57,5 @@ jobs: working-directory: ${{github.workspace}}/build/test # Execute tests defined by the CMake configuration. # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest + run: ctest --output-on-failure