update util lib

This commit is contained in:
Patrick Brosi 2022-03-28 14:59:55 +02:00
parent a58ed3d54d
commit 361063185e
19 changed files with 440 additions and 1794 deletions

View file

@ -28,7 +28,7 @@ class DirGraph : public Graph<N, E> {
Node<N, E>* addNd(const N& pl);
Edge<N, E>* addEdg(Node<N, E>* from, Node<N, E>* to, const E& p);
Node<N, E>* mergeNds(Node<N, E>* a, Node<N, E>* b);
virtual Node<N, E>* mergeNds(Node<N, E>* a, Node<N, E>* b);
};

View file

@ -114,16 +114,18 @@ class EDijkstra : public ShortestPath<EDijkstra> {
using SettledInit = tsl::robin_map<Edge<N, E>*, RouteEdgeInit<N, E, C>>;
template <typename N, typename E, typename C>
using SettledInitNoRes = tsl::robin_map<Edge<N, E>*, RouteEdgeInitNoRes<N, E, C>>;
using SettledInitNoRes =
tsl::robin_map<Edge<N, E>*, RouteEdgeInitNoRes<N, E, C>>;
template <typename N, typename E, typename C>
using PQInit = radix_heap::pair_radix_heap<C, RouteEdgeInit<N, E, C>>;
template <typename N, typename E, typename C>
using PQInitNoRes = radix_heap::pair_radix_heap<C, RouteEdgeInitNoRes<N, E, C>>;
using PQInitNoRes =
radix_heap::pair_radix_heap<C, RouteEdgeInitNoRes<N, E, C>>;
template <typename N, typename E, typename C>
static C shortestPathImpl(const std::set<Edge<N, E>*> from,
static C shortestPathImpl(const std::set<Edge<N, E>*>& from,
const std::set<Edge<N, E>*>& to,
const util::graph::CostFunc<N, E, C>& costFunc,
const util::graph::HeurFunc<N, E, C>& heurFunc,
@ -148,6 +150,13 @@ class EDijkstra : public ShortestPath<EDijkstra> {
const util::graph::HeurFunc<N, E, C>& heurFunc,
EList<N, E>* resEdges, NList<N, E>* resNodes);
template <typename N, typename E, typename C>
static C shortestPathImpl(const std::set<Node<N, E>*>& from,
const std::set<Node<N, E>*>& to,
const util::graph::CostFunc<N, E, C>& costFunc,
const util::graph::HeurFunc<N, E, C>& heurFunc,
EList<N, E>* resEdges, NList<N, E>* resNodes);
template <typename N, typename E, typename C>
static std::unordered_map<Edge<N, E>*, C> shortestPathImpl(
const std::set<Edge<N, E>*>& from,
@ -161,6 +170,15 @@ class EDijkstra : public ShortestPath<EDijkstra> {
std::unordered_map<Edge<N, E>*, EList<N, E>*> resEdges,
std::unordered_map<Edge<N, E>*, NList<N, E>*> resNodes);
template <typename N, typename E, typename C>
static std::unordered_map<Edge<N, E>*, C> shortestPathImpl(
const std::set<Edge<N, E>*>& from, const std::set<Edge<N, E>*>& to,
const std::unordered_map<Edge<N, E>*, C>& initCosts,
const util::graph::CostFunc<N, E, C>& costFunc,
const util::graph::HeurFunc<N, E, C>& heurFunc,
std::unordered_map<Edge<N, E>*, EList<N, E>*> resEdges,
std::unordered_map<Edge<N, E>*, NList<N, E>*> resNodes);
template <typename N, typename E, typename C>
static std::unordered_map<Edge<N, E>*, C> shortestPathImpl(
const std::set<Edge<N, E>*>& from, const std::set<Edge<N, E>*>& to,
@ -202,11 +220,10 @@ class EDijkstra : public ShortestPath<EDijkstra> {
PQInit<N, E, C>& pq);
template <typename N, typename E, typename C>
static inline void relaxInitNoResEdgs(RouteEdgeInitNoRes<N, E, C>& cur,
const std::set<Edge<N, E>*>& to, C stall,
const util::graph::CostFunc<N, E, C>& costFunc,
const util::graph::HeurFunc<N, E, C>& heurFunc,
PQInitNoRes<N, E, C>& pq);
static inline void relaxInitNoResEdgs(
RouteEdgeInitNoRes<N, E, C>& cur, const std::set<Edge<N, E>*>& to,
C stall, const util::graph::CostFunc<N, E, C>& costFunc,
const util::graph::HeurFunc<N, E, C>& heurFunc, PQInitNoRes<N, E, C>& pq);
template <typename N, typename E, typename C>
static void relaxInv(RouteEdge<N, E, C>& cur,

View file

@ -47,7 +47,30 @@ C EDijkstra::shortestPathImpl(Edge<N, E>* from, const std::set<Node<N, E>*>& to,
// _____________________________________________________________________________
template <typename N, typename E, typename C>
C EDijkstra::shortestPathImpl(const std::set<Edge<N, E>*> from,
C EDijkstra::shortestPathImpl(const std::set<Node<N, E>*>& from,
const std::set<Node<N, E>*>& to,
const util::graph::CostFunc<N, E, C>& costFunc,
const util::graph::HeurFunc<N, E, C>& heurFunc,
EList<N, E>* resEdges, NList<N, E>* resNodes) {
std::set<Edge<N, E>*> frEs;
std::set<Edge<N, E>*> toEs;
for (auto n : from) {
frEs.insert(n->getAdjListIn().begin(), n->getAdjListIn().end());
}
for (auto n : to) {
toEs.insert(n->getAdjListIn().begin(), n->getAdjListIn().end());
}
C cost = shortestPathImpl(frEs, toEs, costFunc, heurFunc, resEdges, resNodes);
return cost;
}
// _____________________________________________________________________________
template <typename N, typename E, typename C>
C EDijkstra::shortestPathImpl(const std::set<Edge<N, E>*>& from,
const std::set<Edge<N, E>*>& to,
const util::graph::CostFunc<N, E, C>& costFunc,
const util::graph::HeurFunc<N, E, C>& heurFunc,

View file

@ -5,10 +5,10 @@
#ifndef UTIL_GRAPH_GRAPH_H_
#define UTIL_GRAPH_GRAPH_H_
#include <cassert>
#include <iostream>
#include <set>
#include <string>
#include <iostream>
#include <cassert>
#include "util/graph/Edge.h"
#include "util/graph/Node.h"
@ -25,7 +25,7 @@ class Graph {
Edge<N, E>* addEdg(Node<N, E>* from, Node<N, E>* to);
virtual Edge<N, E>* addEdg(Node<N, E>* from, Node<N, E>* to, const E& p) = 0;
Edge<N, E>* getEdg(Node<N, E>* from, Node<N, E>* to);
const Edge<N, E>* getEdg(Node<N, E>* from, Node<N, E>* to) const;
const Edge<N, E>* getEdg(const Node<N, E>* from, const Node<N, E>* to) const;
virtual Node<N, E>* mergeNds(Node<N, E>* a, Node<N, E>* b) = 0;
@ -39,11 +39,11 @@ class Graph {
void delEdg(Node<N, E>* from, Node<N, E>* to);
protected:
std::set<Node<N, E>*> _nodes;
std::set<Node<N, E>*> _nodes;
};
#include "util/graph/Graph.tpp"
}
}
} // namespace graph
} // namespace util
#endif // UTIL_GRAPH_GRAPH_H_

View file

@ -22,8 +22,7 @@ const std::set<Node<N, E>*>& Graph<N, E>::getNds() const {
// _____________________________________________________________________________
template <typename N, typename E>
typename std::set<Node<N, E>*>::iterator Graph<N, E>::delNd(
Node<N, E>* n) {
typename std::set<Node<N, E>*>::iterator Graph<N, E>::delNd(Node<N, E>* n) {
return delNd(_nodes.find(n));
}
@ -69,10 +68,10 @@ Node<N, E>* Graph<N, E>::sharedNode(const Edge<N, E>* a, const Edge<N, E>* b) {
return r;
}
// _____________________________________________________________________________
template <typename N, typename E>
const Edge<N, E>* Graph<N, E>::getEdg(Node<N, E>* from, Node<N, E>* to) const {
const Edge<N, E>* Graph<N, E>::getEdg(const Node<N, E>* from,
const Node<N, E>* to) const {
for (auto e : from->getAdjList()) {
if (e->getOtherNd(from) == to) return e;
}

View file

@ -79,6 +79,16 @@ class ShortestPath {
resNodes);
}
template <typename N, typename E, typename C>
static C shortestPath(const std::set<Node<N, E>*>& from,
const std::set<Node<N, E>*>& to,
const CostFunc<N, E, C>& costFunc) {
EList<N, E>* el = 0;
NList<N, E>* nl = 0;
return D::shortestPathImpl(from, to, costFunc, ZeroHeurFunc<N, E, C>(),
el, nl);
}
template <typename N, typename E, typename C>
static C shortestPath(const std::set<Node<N, E>*> from,
const std::set<Node<N, E>*>& to,

View file

@ -30,7 +30,7 @@ class UndirGraph : public Graph<N, E> {
Node<N, E>* addNd(const N& pl);
Edge<N, E>* addEdg(Node<N, E>* from, Node<N, E>* to, const E& p);
Node<N, E>* mergeNds(Node<N, E>* a, Node<N, E>* b);
virtual Node<N, E>* mergeNds(Node<N, E>* a, Node<N, E>* b);
};

View file

@ -42,12 +42,14 @@ Edge<N, E>* UndirGraph<N, E>::addEdg(Node<N, E>* from, Node<N, E>* to,
template <typename N, typename E>
Node<N, E>* UndirGraph<N, E>::mergeNds(Node<N, E>* a, Node<N, E>* b) {
for (auto e : a->getAdjListOut()) {
if (e->getFrom() != a) continue;
if (e->getTo() != b) {
addEdg(b, e->getTo(), e->pl());
}
}
for (auto e : a->getAdjListIn()) {
if (e->getTo() != a) continue;
if (e->getFrom() != b) {
addEdg(e->getFrom(), b, e->pl());
}

View file

@ -128,7 +128,7 @@ class pair_radix_heap {
void push(key_type key, const value_type &value) {
unsigned_key_type x = encoder_type::encode(key);
if (last_ > x) {
std::cerr << "Not monotone: " << last_ << " vs " << x << std::endl;
std::cerr << "PQ: not monotone: " << last_ << " vs " << x << std::endl;
x = last_;
}
++size_;
@ -140,7 +140,7 @@ class pair_radix_heap {
void push(key_type key, value_type &&value) {
unsigned_key_type x = encoder_type::encode(key);
if (last_ > x) {
std::cerr << "Not monotone: " << last_ << " vs " << x << std::endl;
std::cerr << "PQ: not monotone: " << last_ << " vs " << x << std::endl;
x = last_;
}
++size_;