9#include <initializer_list>
16#include <ankerl/unordered_dense.h>
35 constexpr Cost() =
default;
39 [[nodiscard]]
static constexpr Cost inf() {
return std::numeric_limits<Rep>::max(); }
40 [[nodiscard]]
static constexpr Cost min() {
return std::numeric_limits<Rep>::min(); }
42 [[nodiscard]]
constexpr Rep value()
const {
return value_; }
43 [[nodiscard]]
constexpr bool is_inf()
const {
return value_ == std::numeric_limits<Rep>::max(); }
44 constexpr explicit operator Rep()
const {
return value_; }
51 const auto sum = int64_t{lhs.value_} + rhs.value_;
52 return static_cast<Rep>(std::clamp<int64_t>(sum,
min().value_,
inf().value_));
66using Costs = fe::Vector<Cost, 4>;
67using Edges = fe::Vector<EdgeId, 4>;
84 assert(
row < rows_ && col < cols_);
85 return data_[
row * row_stride_ + col * col_stride_];
88 [[nodiscard]]
constexpr size_t rows()
const {
return rows_; }
89 [[nodiscard]]
constexpr size_t cols()
const {
return cols_; }
90 [[nodiscard]]
constexpr size_t size()
const {
return rows_ * cols_; }
93 [[nodiscard]]
constexpr bool row_major()
const {
return col_stride_ == 1; }
99 [[nodiscard]]
constexpr T*
data()
const {
return data_; }
100 [[nodiscard]]
constexpr T*
begin()
const {
return data_; }
101 [[nodiscard]]
constexpr T*
end()
const {
return data_ +
size(); }
102 [[nodiscard]]
constexpr T*
row(
size_t row)
const {
104 return data_ +
row * row_stride_;
109 assert(rows_ == rhs.rows() && cols_ == rhs.cols());
110 assert((
size() <= 1 || data_ != rhs.data() ||
row_major() == rhs.row_major()) &&
"+=: rhs aliases this");
111 for (
size_t r = 0; r < rows_; ++r)
112 for (
size_t c = 0; c < cols_; ++c)
113 (*
this)[r, c] += rhs[r, c];
118 assert(rows_ == src.rows() && cols_ == src.cols());
119 assert((
size() <= 1 || data_ != src.data() ||
row_major() == src.row_major()) &&
"copy_from: src aliases this");
120 for (
size_t r = 0; r < rows_; ++r)
121 for (
size_t c = 0; c < cols_; ++c)
122 (*
this)[r, c] = src[r, c];
127 constexpr void erase_rows(fe::View<size_t>
rows) &
requires(!std::is_const_v<T>) {
128 if (
rows.empty())
return;
129 assert(std::ranges::is_sorted(
rows) &&
"erase_rows: rows not sorted");
130 assert(std::ranges::adjacent_find(
rows) ==
rows.end() &&
"erase_rows: duplicate row");
131 assert(
rows.back() < rows_ &&
"erase_rows: row out of range");
135 for (
const size_t row :
rows) {
136 if (dst != src) std::copy_n(data_ + src * cols_, (
row - src) * cols_, data_ + dst * cols_);
140 if (dst != src) std::copy_n(data_ + src * cols_, (rows_ - src) * cols_, data_ + dst * cols_);
144 erase_storage_cols(
rows);
150 constexpr void erase_cols(fe::View<size_t>
cols) &
requires(!std::is_const_v<T>) {
165 constexpr void erase_storage_cols(fe::View<size_t>
cols) {
167 for (
size_t r = 0; r < cols_; ++r) {
168 const T*
const base = data_ + r * rows_;
170 for (
const auto col :
cols) {
171 out = std::copy(in, base + col, out);
174 out = std::copy(in, base + rows_, out);
181 size_t row_stride_ = 0;
182 size_t col_stride_ = 1;
198 requires std::is_invocable_r_v<Cost, F&, size_t, size_t>
Matrix(
size_t rows,
size_t cols, F f)
201 :
Matrix(view.
rows(), view.
cols(), [view](size_t r, size_t c) {
return view[r, c]; }) {}
206 :
Matrix(std::move(other.costs_), other.rows(), other.cols()) {
210 costs_ = std::move(other.costs_);
211 view() = {costs_.data(), other.rows(), other.cols()};
224 return a.rows() == b.rows() && a.cols() == b.cols() && a.costs_ == b.costs_;
229 : costs_(std::move(costs)) {
230 assert(costs_.size() == rows * cols &&
"Matrix: wrong number of costs");
231 view() = {costs_.data(), rows, cols};
288 const Edge&
edge = checked_edge(e);
289 const Costs& costs = matrix(
edge.matrix);
295 const auto [src, dst] =
edge(e);
296 assert((from == src || from == dst) &&
"edge_matrix: not an end of the edge");
304 const Edge&
edge = checked_edge(e);
305 return {.src =
edge.src, .dst =
edge.dst};
312 [[nodiscard]]
size_t num_nodes()
const {
return nodes_.size(); }
313 [[nodiscard]]
size_t num_edges()
const {
return edges_.size(); }
319 using MatrixId = size_t;
323 fe::View<Cost> costs;
327 struct PooledMatrix {
328 explicit PooledMatrix(
const Probe& probe)
329 : costs(probe.costs.begin(), probe.costs.end())
330 , hash(probe.hash) {}
336 struct PooledMatrixHash {
337 using is_transparent = void;
338 using is_avalanching = void;
339 size_t operator()(
const PooledMatrix& matrix)
const noexcept {
return matrix.hash; }
340 size_t operator()(
const Probe& probe)
const noexcept {
return probe.hash; }
343 struct PooledMatrixEq {
344 using is_transparent = void;
345 bool operator()(
const PooledMatrix& a,
const PooledMatrix& b)
const noexcept {
return a.costs ==
b.costs; }
346 bool operator()(
const Probe& probe,
const PooledMatrix& matrix)
const noexcept {
347 return std::ranges::equal(matrix.costs, probe.costs);
349 bool operator()(
const PooledMatrix& matrix,
const Probe& probe)
const noexcept {
350 return (*
this)(probe, matrix);
362 MatrixId matrix = npos;
365 [[nodiscard]]
const Node& checked_node(NodeId n)
const {
366 assert((n < num_nodes()) &&
"node: unknown node");
370 [[nodiscard]]
const Edge& checked_edge(EdgeId e)
const {
371 assert((e < num_edges()) &&
"edge: unknown edge");
375 [[nodiscard]] MatrixId intern(MatrixView matrix);
376 [[nodiscard]]
const Costs& matrix(MatrixId
id)
const {
return matrices_.values()[
id].costs; }
378 fe::Vector<Node> nodes_;
379 fe::Vector<Edge> edges_;
381 ankerl::unordered_dense::set<PooledMatrix, PooledMatrixHash, PooledMatrixEq> matrices_;
A view of a cost matrix over row-major storage owned elsewhere; transposed() views the same storage c...
constexpr size_t size() const
constexpr T * begin() const
constexpr T * end() const
constexpr T & operator[](size_t row, size_t col) const
constexpr Cost * row(size_t row) const
constexpr BasicMatrix(T *data, size_t rows, size_t cols)
constexpr const BasicMatrix & operator+=(BasicMatrix< const Cost > rhs) const
constexpr BasicMatrix transposed() const
constexpr size_t rows() const
constexpr bool row_major() const
constexpr void erase_cols(fe::View< size_t > cols) &
Erases the columns in cols, which must be sorted and distinct.
constexpr BasicMatrix()=default
constexpr void erase_rows(fe::View< size_t > rows) &
Erases the rows in rows, which must be sorted and distinct.
constexpr void copy_from(BasicMatrix< const Cost > src) const
constexpr size_t cols() const
constexpr BasicMatrix(const BasicMatrix< U > &other)
constexpr Cost * data() const
A saturating cost; Cost::inf forbids an option or option pair.
friend constexpr Cost operator+(Cost lhs, Cost rhs)
Any sign; saturates and Cost::inf absorbs.
static constexpr Cost inf()
static constexpr Cost min()
friend constexpr auto operator<=>(Cost, Cost)=default
constexpr Cost(Rep value)
constexpr Rep value() const
constexpr Cost & operator+=(Cost rhs)
constexpr bool is_inf() const
MatrixView edge_matrix(EdgeId e, NodeId from) const
The options of from, an end of e, are the rows.
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
fe::View< EdgeId > incident_edges(NodeId n) const
size_t num_matrices() const
Number of distinct edge matrices ever linked or set; edges with equal matrices share one copy.
MatrixView edge_matrix(EdgeId e) const
Row-major with the src option as the row.
fe::View< Cost > node_costs(NodeId n) const
auto nodes() const
All node ids.
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.
Matrix(const Matrix &other)
Matrix(size_t rows, size_t cols, std::initializer_list< Cost > costs)
Matrix(size_t rows, size_t cols, Cost fill=0)
Matrix & operator+=(MatrixView rhs)
Matrix & operator=(Matrix other) noexcept
Matrix(size_t rows, size_t cols, F f)
Entry (r, c) is f(r, c), generated in row-major order.
Matrix(Matrix &&other) noexcept
friend bool operator==(const Matrix &a, const Matrix &b)
size_t OptionId
Index of an option within a node's cost vector.
BasicMatrix< const Cost > MatrixView
fe::Vector< OptionId, 8 > Choices
One option per node, indexed by node id.
std::ostream & operator<<(std::ostream &out, Cost cost)
fe::Vector< EdgeId, 4 > Edges
fe::Vector< Cost, 4 > Costs
size_t EdgeId
Identifier of an edge in a Graph.
size_t NodeId
Identifier of a node in a Graph.
BasicMatrix< Cost > MatrixSpan