10std::ostream&
operator<<(std::ostream& out,
Cost cost) {
return cost.is_inf() ? out <<
"inf" : out << cost.value(); }
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;
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");
23 &&
"link: cost matrix has the wrong shape");
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);
34 if (e ==
npos)
return link(m, n, costs);
37 const MatrixSpan from_m = edges_[e].src == m ? sum.span() : sum.span().transposed();
44 assert((costs.size() ==
num_options(n)) &&
"set_node_costs: option count must not change");
45 std::ranges::copy(costs, nodes_[n].costs.begin());
50 &&
"set_edge_costs: cost matrix has the wrong shape");
51 edges_[e].matrix = intern(costs);
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;
64Graph::MatrixId Graph::intern(
MatrixView 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);
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());
constexpr size_t size() const
constexpr size_t rows() const
constexpr bool row_major() const
Whether rows are contiguous, which also holds for a transposed single row or column.
constexpr size_t cols() const
constexpr T * data() const
A saturating cost; Cost::inf forbids an option or option pair.
friend std::ostream & operator<<(std::ostream &, Cost)
Prints Cost::inf as inf.
Ends edge(EdgeId e) const
void set_node_costs(NodeId n, fe::View< Cost > costs)
Replace a cost vector; the option count must not change.
static constexpr EdgeId npos
size_t num_options(NodeId n) const
MatrixView edge_matrix(EdgeId e) const
Row-major with the src option as the row.
NodeId add(fe::View< Cost > costs)
Add a node with the given cost vector.
auto edges() const
All edge ids.
void set_edge_costs(EdgeId e, MatrixView costs)
Replace a cost matrix, src options as rows.
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.
EdgeId link(NodeId m, NodeId n, MatrixView costs)
Link m and n with a num_options(m) × num_options(n) matrix.
An owning row-major cost matrix; it views its own storage, so writes go through span().
BasicMatrix< const Cost > MatrixView
size_t EdgeId
Identifier of an edge in a Graph.
size_t NodeId
Identifier of a node in a Graph.
BasicMatrix< Cost > MatrixSpan