MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
graph.cpp
Go to the documentation of this file.
1#include "pbqp/graph.h"
2
3#include <algorithm>
4#include <ostream>
5#include <string_view>
6#include <utility>
7
8namespace pbqp {
9
10std::ostream& operator<<(std::ostream& out, Cost cost) { return cost.is_inf() ? out << "inf" : out << cost.value(); }
11
12NodeId Graph::add(fe::View<Cost> costs) {
13 assert((!costs.empty()) && "add: a PBQP node needs at least one option");
14 nodes_.emplace_back(Node{.costs = {costs.begin(), costs.end()}, .edges = {}});
15 return nodes_.size() - 1;
16}
17
19 assert((m < num_nodes() && n < num_nodes()) && "link: unknown node");
20 assert((m != n) && "link: self edges are not supported, fold them into the node cost vector instead");
21 assert((edge(m, n) == npos) && "link: an edge between these nodes already exists, merge the matrices instead");
22 assert((costs.rows() == num_options(m) && costs.cols() == num_options(n))
23 && "link: cost matrix has the wrong shape");
24
25 edges_.emplace_back(Edge{.src = m, .dst = n, .matrix = intern(costs)});
26 auto e = edges_.size() - 1;
27 nodes_[m].edges.emplace_back(e);
28 nodes_[n].edges.emplace_back(e);
29 return e;
30}
31
33 const EdgeId e = edge(m, n);
34 if (e == npos) return link(m, n, costs);
35
36 Matrix sum(edge_matrix(e));
37 const MatrixSpan from_m = edges_[e].src == m ? sum.span() : sum.span().transposed();
38 from_m += costs;
39 set_edge_costs(e, sum);
40 return e;
41}
42
43void Graph::set_node_costs(NodeId n, fe::View<Cost> costs) {
44 assert((costs.size() == num_options(n)) && "set_node_costs: option count must not change");
45 std::ranges::copy(costs, nodes_[n].costs.begin());
46}
47
49 assert((costs.rows() == edge_matrix(e).rows() && costs.cols() == edge_matrix(e).cols())
50 && "set_edge_costs: cost matrix has the wrong shape");
51 edges_[e].matrix = intern(costs);
52}
53
55 assert((m < num_nodes() && n < num_nodes()) && "edge: unknown node");
56 if (nodes_[n].edges.size() < nodes_[m].edges.size()) std::swap(m, n);
57 for (const auto e : nodes_[m].edges) {
58 const Edge& candidate = edges_[e];
59 if (candidate.src == n || candidate.dst == n) return e;
60 }
61 return npos;
62}
63
64Graph::MatrixId Graph::intern(MatrixView matrix) {
65 if (!matrix.row_major()) return intern(Matrix(matrix));
66 const fe::View<Cost> costs(matrix.data(), matrix.size());
67 const auto bytes = std::string_view(reinterpret_cast<const char*>(costs.data()), costs.size() * sizeof(Cost));
68 const size_t hash = ankerl::unordered_dense::hash<std::string_view>{}(bytes);
69
70 const Probe probe{costs, hash};
71 auto i = matrices_.find(probe);
72 if (i == matrices_.end()) i = matrices_.emplace(probe).first;
73 return static_cast<MatrixId>(i - matrices_.begin());
74}
75
76} // namespace pbqp
constexpr size_t size() const
Definition graph.h:90
constexpr size_t rows() const
Definition graph.h:88
constexpr bool row_major() const
Whether rows are contiguous, which also holds for a transposed single row or column.
Definition graph.h:93
constexpr size_t cols() const
Definition graph.h:89
constexpr T * data() const
Definition graph.h:99
A saturating cost; Cost::inf forbids an option or option pair.
Definition graph.h:31
constexpr Cost()=default
friend std::ostream & operator<<(std::ostream &, Cost)
Prints Cost::inf as inf.
Definition graph.cpp:10
Ends edge(EdgeId e) const
Definition graph.h:303
void set_node_costs(NodeId n, fe::View< Cost > costs)
Replace a cost vector; the option count must not change.
Definition graph.cpp:43
static constexpr EdgeId npos
Definition graph.h:259
size_t num_options(NodeId n) const
Definition graph.h:300
size_t num_nodes() const
Definition graph.h:312
MatrixView edge_matrix(EdgeId e) const
Row-major with the src option as the row.
Definition graph.h:287
NodeId add(fe::View< Cost > costs)
Add a node with the given cost vector.
Definition graph.cpp:12
auto edges() const
All edge ids.
Definition graph.h:310
void set_edge_costs(EdgeId e, MatrixView costs)
Replace a cost matrix, src options as rows.
Definition graph.cpp:48
EdgeId add_edge_costs(NodeId m, NodeId n, MatrixView costs)
Like link, but sums costs onto an existing edge between m and n, whichever way it points.
Definition graph.cpp:32
EdgeId link(NodeId m, NodeId n, MatrixView costs)
Link m and n with a num_options(m) × num_options(n) matrix.
Definition graph.cpp:18
An owning row-major cost matrix; it views its own storage, so writes go through span().
Definition graph.h:189
Matrix()=default
Definition dot.h:9
BasicMatrix< const Cost > MatrixView
Definition graph.h:186
size_t EdgeId
Identifier of an edge in a Graph.
Definition graph.h:26
size_t NodeId
Identifier of a node in a Graph.
Definition graph.h:25
BasicMatrix< Cost > MatrixSpan
Definition graph.h:185