18 std::numeric_limits<EdgeWeight>::max() / 4 - 1;
32 updates.push_back({u, v, w,
true, ts});
36 updates.push_back({u, v, 0,
false, ts});
40 updates.push_back({u, v, w,
true, ts});
43 [[nodiscard]]
bool empty() const noexcept {
return updates.empty(); }
49 : directed_(
directed), adjacency_(vertices) {}
51 [[nodiscard]] std::size_t
vertex_count() const noexcept {
return adjacency_.size(); }
52 [[nodiscard]] std::uint64_t
version() const noexcept {
return version_; }
53 [[nodiscard]]
bool directed() const noexcept {
return directed_; }
56 const auto n =
static_cast<std::size_t
>(v) + 1;
57 if (n > adjacency_.size()) adjacency_.resize(n);
63 for (
const auto& op : batch.
updates) {
64 if (op.src == op.dst)
continue;
66 throw std::invalid_argument(
67 "edge weight exceeds the representable finite-distance domain");
71 for (
const auto& op : batch.
updates) {
72 if (op.src == op.dst)
continue;
80 if (!batch.
empty()) ++version_;
84 if (u >= adjacency_.size())
return std::nullopt;
85 const auto it = adjacency_[u].find(v);
86 if (it == adjacency_[u].end())
return std::nullopt;
91 return u < adjacency_.size() ? adjacency_[u].size() : 0;
96 if (u >= adjacency_.size())
return;
97 for (
const auto& [v, w] : adjacency_[u]) fn(v, w);
101 if (u >= adjacency_.size())
return {};
102 std::vector<std::pair<VertexId, EdgeWeight>> out;
103 out.reserve(adjacency_[u].size());
104 for (
const auto& [v, w] : adjacency_[u]) out.push_back({v, w});
109 void apply_one(
const WeightedEdgeUpdate& op) {
111 adjacency_[op.src][op.dst] = op.weight;
113 adjacency_[op.src].erase(op.dst);
117 bool directed_{
false};
118 std::vector<std::map<VertexId, EdgeWeight>> adjacency_;
119 std::uint64_t version_{0};
bool directed() const noexcept
std::size_t vertex_count() const noexcept
void for_each_neighbor(VertexId u, Fn &&fn) const
std::optional< EdgeWeight > weight(VertexId u, VertexId v) const
std::uint64_t version() const noexcept
void ensure_vertex(VertexId v)
std::size_t degree(VertexId u) const noexcept
std::vector< std::pair< VertexId, EdgeWeight > > neighbors(VertexId u) const
void apply(const WeightedUpdateBatch &batch)
WeightedDynamicGraph(std::size_t vertices=0, bool directed=false)
constexpr EdgeWeight kMaxFiniteWeightedDistance
std::vector< WeightedEdgeUpdate > updates
bool empty() const noexcept
void update(VertexId u, VertexId v, EdgeWeight w, std::uint64_t ts=0)
void remove(VertexId u, VertexId v, std::uint64_t ts=0)
void add(VertexId u, VertexId v, EdgeWeight w, std::uint64_t ts=0)