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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/*******************************************************************************
********************************************************************************
digraph : directed graph
Copyright © 2023 Yann Orlarey. All rights reserved.
*******************************************************************************
******************************************************************************/
#pragma once
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <stack>
//===========================================================
// digraph : a directed graph, a set of nodes f type N and a
// set of connections between these nodes. Connections have an
// associated value, by default 0. This value is used in Faust
// to represent the time dependency between computations.
//===========================================================
template <typename N>
class digraph {
using TWeights = std::set<int>;
using TDestinations = std::map<N, TWeights>;
static inline const TWeights gEmptyWeights;
//--------------------------------------------------------------------------
// Real/internal structure of a graph. A graph is a set of nodes
// and a set of connections between theses nodes. These connections
// have integer values attached.
class internalgraph {
private:
std::set<N> fNodes; // {n1,n2,...}
std::map<N, TDestinations> fConnections; // {(ni -{d1,d2,...}-> nj),...}
public:
#if 0
internalgraph() { std::cout << "create internalgraph " << this << '\n'; }
~internalgraph() { std::cout << "delete internalgraph " << this << '\n'; }
#endif
//----------------------------------------------------------------------
// Methods used to build the graph
//----------------------------------------------------------------------
// Add a node n to the graph
void add(N n)
{
fNodes.insert(n);
(void)fConnections[n]; // make sure we have an empty set of connections for n
}
// add two nodes with a set of connections of weights w
void add(const N& n1, const N& n2, const TWeights& w)
{
add(n1);
add(n2);
fConnections[n1][n2].insert(w.begin(), w.end());
}
//----------------------------------------------------------------------
// Methods used to query the graph
//----------------------------------------------------------------------
// returns the set of nodes of the graph
[[nodiscard]] const std::set<N>& nodes() const { return fNodes; }
// returns the set of nodes of the graph
[[nodiscard]] const std::map<N, TDestinations>& connections() const { return fConnections; }
// Returns the destinations of node n in the graph
[[nodiscard]] const TDestinations& destinations(const N& n) const
{
assert(fNodes.find(n) != fNodes.end());
return fConnections.at(n);
}
// Returns true is n1 and n2 are connected in the graph
[[nodiscard]] bool areConnected(const N& n1, const N& n2) const
{
// check we test connexions between existing nodes
assert(fNodes.find(n1) != fNodes.end());
assert(fNodes.find(n2) != fNodes.end());
auto cnx1 = fConnections.find(n1);
if (cnx1 == fConnections.end()) {
// n1 has no connection
return false;
} else {
auto cnx2 = cnx1->second.find(n2);
if (cnx2 == cnx1->second.end()) {
// n1 has connections, but not to n2
return false;
} else {
// its seems we have connections between n1 and n2,
// but we need to check
const std::set<int>& w12 = cnx2->second;
return !w12.empty();
}
}
}
// Returns the destinations of node n in the graph
[[nodiscard]] bool areConnected(const N& n1, const N& n2, int& d) const
{
// check we test connexions between existing nodes
assert(fNodes.find(n1) != fNodes.end());
assert(fNodes.find(n2) != fNodes.end());
auto cnx1 = fConnections.find(n1);
if (cnx1 == fConnections.end()) {
// n1 has no connection
return false;
} else {
auto cnx2 = cnx1->second.find(n2);
if (cnx2 == cnx1->second.end()) {
// n1 has connections, but not to n2
return false;
} else {
// its seems we have connections between n1 and n2,
// but we need to check
const std::set<int>& w12 = cnx2->second;
if (!w12.empty()) {
d = *w12.begin();
return true;
} else {
return false;
}
}
}
}
// Returns the weights of the connections between two nodes
[[nodiscard]] const TWeights& weights(const N& n1, const N& n2) const
{
// check we test connexions between existing nodes
assert(fNodes.find(n1) != fNodes.end());
assert(fNodes.find(n2) != fNodes.end());
auto cnx1 = fConnections.find(n1);
if (cnx1 == fConnections.end()) {
// n1 has no connection
return gEmptyWeights;
} else {
auto cnx2 = cnx1->second.find(n2);
if (cnx2 == cnx1->second.end()) {
// n1 has connections, but not to n2
return gEmptyWeights;
} else {
// its seems we have connections between n1 and n2,
// but we need to check
const std::set<int>& w12 = cnx2->second;
return w12;
}
}
}
};
std::shared_ptr<internalgraph> fContent = std::make_shared<internalgraph>();
public:
//--------------------------------------------------------------------------
// Methods used to build the graph
//--------------------------------------------------------------------------
// Add the node n to the graph
digraph& add(N n)
{
fContent->add(n);
return *this;
}
// add two nodes with a set of connections of weights w
digraph& add(const N& n1, const N& n2, const TWeights& w)
{
fContent->add(n1, n2, w);
return *this;
}
// Add the nodes n1 and n2 and the connection (n1 -d-> n2) to the graph.
digraph& add(const N& n1, const N& n2, int d = 0)
{
fContent->add(n1, n2, {d});
return *this;
}
// add a whole graph g
digraph& add(const digraph& g)
{
for (auto& n : g.nodes()) {
add(n);
for (auto& c : g.destinations(n)) {
add(n, c.first, c.second);
}
}
return *this;
}
//--------------------------------------------------------------------------
// Methods used to visit the graph
//--------------------------------------------------------------------------
// returns the set of nodes of the graph
[[nodiscard]] const std::set<N>& nodes() const { return fContent->nodes(); }
// returns the set of nodes of the graph
[[nodiscard]] const std::map<N, TDestinations>& connections() const
{
return fContent->connections();
}
// returns the destinations of node n in the graph
[[nodiscard]] const TDestinations& destinations(const N& n) const
{
return fContent->destinations(n);
}
// returns the weights of the connections between two nodes
[[nodiscard]] const TWeights& weights(const N& n1, const N& n2) const
{
return fContent->weights(n1, n2);
}
//--------------------------------------------------------------------------
// Methods used to query the graph
//--------------------------------------------------------------------------
// true is there is any connection between nodes n1 and n2
[[nodiscard]] bool areConnected(const N& n1, const N& n2) const
{
return fContent->areConnected(n1, n2);
}
// true is there is any connection between nodes n1 and n2.
// The smallest weight is returned in d.
bool areConnected(const N& n1, const N& n2, int& d) const
{
return fContent->areConnected(n1, n2, d);
}
//--------------------------------------------------------------------------
// compare graphs for maps and other containers
//--------------------------------------------------------------------------
friend bool operator<(const digraph& p1, const digraph& p2)
{
return (p1.nodes() < p2.nodes()) ||
((p1.nodes() == p2.nodes()) && (p1.connections() < p2.connections()));
}
friend bool operator==(const digraph& p1, const digraph& p2)
{
return p1.nodes() == p2.nodes() && p1.connections() == p2.connections();
}
};
|