-
Notifications
You must be signed in to change notification settings - Fork 5
/
cppgraph.h
133 lines (113 loc) · 4.04 KB
/
cppgraph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#if !defined(CPPGRAPH_H_INCLUDED)
#define CPPGRAPH_H_INCLUDED
#include <memory>
template<typename ND, typename LD>
struct Graph {
struct Link;
struct Node {
ND data;
Graph& graph;
Node *prev, *next;
Link *firstIn, *lastIn, *firstOut, *lastOut;
Node(ND data, Graph& graph)
: data(std::move(data)),
graph(graph),
prev(graph.lastNode), next(nullptr),
firstIn(nullptr), lastIn(nullptr),
firstOut(nullptr), lastOut(nullptr)
{
if (prev) prev->next = this; else graph.firstNode = this;
graph.lastNode = this;
}
Node(Graph& graph)
: Node(ND(), graph)
{}
~Node() {
while (lastIn) delete lastIn;
while (lastOut) delete lastOut;
if (prev) prev->next = next; else graph.firstNode = next;
if (next) next->prev = prev; else graph.lastNode = prev;
}
template<typename CBack>
bool forEachOutgoingLink(CBack cb) {
for (Link *x=firstOut; x; x=x->nextInFrom) {
if (!cb(x)) return false;
}
return true;
}
template<typename CBack>
bool forEachIncomingLink(CBack cb) {
for (Link *x=firstIn; x; x=x->nextInTo) {
if (!cb(x)) return false;
}
return true;
}
};
struct Link {
LD data;
Node *from, *to;
Link *prev, *next, *prevInFrom, *nextInFrom, *prevInTo, *nextInTo;
Link(LD data, Node *from, Node *to)
: data(std::move(data)),
from(from), to(to),
prev(from->graph.lastLink), next(nullptr),
prevInFrom(from->lastOut), nextInFrom(nullptr),
prevInTo(to->lastIn), nextInTo(nullptr)
{
if (&from->graph != &to->graph) {
throw std::runtime_error("Cannot link nodes from different graphs");
}
if (prev) prev->next = this; else from->graph.firstLink = this;
from->graph.lastLink = this;
if (prevInFrom) prevInFrom->nextInFrom = this; else from->firstOut = this;
from->lastOut = this;
if (prevInTo) prevInTo->nextInTo = this; else to->firstIn = this;
to->lastIn = this;
}
Link(Node *from, Node *to)
: Link(LD(), from, to)
{}
~Link() {
if (prevInTo) prevInTo->nextInTo = nextInTo; else to->firstIn = nextInTo;
if (nextInTo) nextInTo->prevInTo = prevInTo; else to->lastIn = prevInTo;
if (prevInFrom) prevInFrom->nextInFrom = nextInFrom; else from->firstOut = nextInFrom;
if (nextInFrom) nextInFrom->prevInFrom = prevInFrom; else from->lastOut = prevInFrom;
if (prev) prev->next = next; else from->graph.firstLink = next;
if (next) next->prev = prev; else from->graph.lastLink = prev;
}
Link(const Link&) = delete;
Link(Link&&) = delete;
Link& operator=(const Link&) = delete;
};
Node *firstNode, *lastNode;
Link *firstLink, *lastLink;
Graph()
: firstNode(nullptr), lastNode(nullptr),
firstLink(nullptr), lastLink(nullptr)
{}
~Graph() {
while (lastNode) delete lastNode;
}
Graph(const Graph&) = delete;
Graph(Graph&&) = delete;
Graph& operator=(const Graph&) = delete;
Node *addNode(ND data) { return new Node(data, *this); }
Link *addLink(LD data, Node *from, Node *to) { return new Link(data, from, to); }
Node *addNode() { return new Node(*this); }
Link *addLink(Node *from, Node *to) { return new Link(from, to); }
template<typename CBack>
bool forEachNode(CBack cb) {
for (Node *n=firstNode; n; n=n->next) {
if (!cb(n)) return false;
}
return true;
}
template<typename CBack>
bool forEachLink(CBack cb) {
for (Link *L=firstLink; L; L=L->next) {
if (!cb(L)) return false;
}
return true;
}
};
#endif