DG-CPP 0.1.0
Directed Graph in C++
core.hpp
Go to the documentation of this file.
1
12#ifndef _DG_CORE_HPP_
13#define _DG_CORE_HPP_
14
15#include "base.hpp"
16
17namespace dg {
25template <typename NodeT, typename EdgeT = void, typename IDT = std::string>
26class DGraph : public DGraphBase<EdgeT, IDT> {
27 protected:
30
31 private:
32 using DGraphBase<EdgeT, IDT>::insertNode;
33
34 public:
35 NodeT node(IDT id) const;
36
37 NodeT& node(IDT id);
38
39 bool strictCheck() const;
40
41 void insertNode(IDT id, const NodeT& data);
42
43 void insertNode(IDT id, const NodeT& data, const _CN& cn);
44
45 void removeNode(IDT id, bool keep_edge = false);
46
47 void insertEdge(IDT from_id, IDT to_id, [[maybe_unused]] bool to_exists = true)
48 requires std::is_void_v<EdgeT>;
49
50 void insertEdge(IDT from_id, IDT to_id, EdgeT_CR data, [[maybe_unused]] bool to_exists = true)
51 requires EdgeVT<EdgeT>;
52
53 NodeT operator[](IDT id) const;
54
55 NodeT& operator[](IDT id);
56
57 bool operator==(const DGraph& dg) const;
58
59 bool operator!=(const DGraph& dg) const;
60
61 private:
62 std::map<IDT, NodeT> node_data;
63};
64
65template <typename EdgeT, typename IDT>
66class DGraph<void, EdgeT, IDT> : public DGraphBase<EdgeT, IDT> {};
67
68template <typename NodeT, typename EdgeT, typename IDT>
69inline NodeT DGraph<NodeT, EdgeT, IDT>::node(IDT id) const {
70 return node_data[id];
71}
72
73template <typename NodeT, typename EdgeT, typename IDT>
74inline NodeT& DGraph<NodeT, EdgeT, IDT>::node(IDT id) {
75 return node_data[id];
76}
77
78template <typename NodeT, typename EdgeT, typename IDT>
80 return this->size() == node_data.size() && this->DGraphBase<EdgeT, IDT>::strictCheck() &&
81 std::equal(this->graph().begin(), this->graph().end(), node_data.begin(),
82 [](auto a, auto b) { return a.first == b.first; });
83}
84
85template <typename NodeT, typename EdgeT, typename IDT>
86inline void DGraph<NodeT, EdgeT, IDT>::insertNode(IDT id, const NodeT& data) {
88 node_data[id] = data;
89}
90
91template <typename NodeT, typename EdgeT, typename IDT>
92inline void DGraph<NodeT, EdgeT, IDT>::insertNode(IDT id, const NodeT& data, const _CN& cn) {
94 node_data[id] = data;
95}
96
97template <typename NodeT, typename EdgeT, typename IDT>
98inline void DGraph<NodeT, EdgeT, IDT>::removeNode(IDT id, bool keep_edge) {
100 node_data.erase(id);
101}
102
103template <typename NodeT, typename EdgeT, typename IDT>
104inline void DGraph<NodeT, EdgeT, IDT>::insertEdge(IDT from_id, IDT to_id, bool to_exists)
105 requires std::is_void_v<EdgeT> {
106 this->insertEdgeToExists(from_id, to_id);
107}
108
109template <typename NodeT, typename EdgeT, typename IDT>
110inline void DGraph<NodeT, EdgeT, IDT>::insertEdge(IDT from_id, IDT to_id, EdgeT_CR data, bool to_exists)
111 requires EdgeVT<EdgeT> {
112 this->insertEdgeToExists(from_id, to_id, data);
113}
114
115template <typename NodeT, typename EdgeT, typename IDT>
116inline NodeT DGraph<NodeT, EdgeT, IDT>::operator[](IDT id) const {
117 return node(id);
118}
119
120template <typename NodeT, typename EdgeT, typename IDT>
122 return node(id);
123}
124
125template <typename NodeT, typename EdgeT, typename IDT>
127 return this->DGraphBase<EdgeT, IDT>::operator==(dg) && dg.node_data == node_data;
128}
129
130template <typename NodeT, typename EdgeT, typename IDT>
132 return this->DGraphBase<EdgeT, IDT>::operator!=(dg) || dg.node_data != node_data;
133}
134
135} // namespace dg
136
137#endif
Directed Graph Base Class.
Directed graph base class.
Definition: base.hpp:44
size_t size() const noexcept
The total number of nodes in the graph.
Definition: base.hpp:605
const std::map< IDT, _CN > & graph() const
Return the const reference to the graph map.
Definition: base.hpp:948
void removeNode(IDT id, bool keep_edge=false)
Remove the node from the graph.
Definition: base.hpp:699
bool strictCheck() const
Check if the graph is strictly valid.
Definition: base.hpp:651
bool operator==(const DGraphBase< EdgeT, IDT > &dg) const
Check if two graphs are identical.
Definition: base.hpp:913
void insertNode(IDT id)
Insert a node.
Definition: base.hpp:678
void insertEdgeToExists(IDT from_id, IDT to_id)
Definition: base.hpp:711
bool operator!=(const DGraphBase< EdgeT, IDT > &dg) const
Check if two graphs are different.
Definition: base.hpp:918
Directed graph class.
Definition: core.hpp:26
bool operator==(const DGraph &dg) const
Definition: core.hpp:126
NodeT operator[](IDT id) const
Definition: core.hpp:116
std::map< IDT, NodeT > node_data
Definition: core.hpp:62
NodeT node(IDT id) const
Definition: core.hpp:69
typename DGraphBase< EdgeT, IDT >::_CN _CN
Definition: core.hpp:28
void insertEdge(IDT from_id, IDT to_id, bool to_exists=true)
Definition: core.hpp:104
void insertNode(IDT id, const NodeT &data)
Definition: core.hpp:86
void removeNode(IDT id, bool keep_edge=false)
Definition: core.hpp:98
typename DGraphBase< EdgeT, IDT >::EdgeT_CR EdgeT_CR
Definition: core.hpp:29
bool strictCheck() const
Definition: core.hpp:79
bool operator!=(const DGraph &dg) const
Definition: core.hpp:131
Definition: common.hpp:25
Directed Graph namespace.
Definition: base.hpp:35