48#ifndef SHORTEST_PATH_COMMON_H
49#define SHORTEST_PATH_COMMON_H
74#define SP_NODE_INFO(p) (static_cast<typename Shortest_Path_Base::Node_Info*>(NODE_COOKIE(p)))
77#define SP_TREE_NODE_INFO(p) (static_cast<typename Shortest_Path_Base::Tree_Node_Info*>(NODE_COOKIE(p)))
80#define SP_ACC(p) (SP_NODE_INFO(p)->dist)
83#define SP_HEAPNODE(p) (SP_NODE_INFO(p)->heap_node)
86#define SP_PARENT(p) (SP_NODE_INFO(p)->ret_node)
89#define SP_TREENODE(p) (SP_TREE_NODE_INFO(p)->tree_node)
92#define SP_ARC_INFO(p) (static_cast<typename Shortest_Path_Base::Arc_Info*>(ARC_COOKIE(p)))
95#define SP_TREE_ARC_INFO(p) (static_cast<typename Shortest_Path_Base::Tree_Arc_Info*>(ARC_COOKIE(p)))
98#define SP_POT(p) (SP_ARC_INFO(p)->pot)
101#define SP_TREEARC(p) (SP_TREE_ARC_INFO(p)->tree_arc)
109namespace shortest_path_detail
126 if constexpr (std::is_integral_v<T>)
129 <<
"Integer overflow in distance addition: " << a <<
" + " << b;
132 <<
"Integer underflow in distance addition: " << a <<
" + " << b;
153 if constexpr (std::is_integral_v<T>)
154 if (b > 0
and a > std::numeric_limits<T>::max() - b)
170 using std::overflow_error::overflow_error;
198template <AlephGraph
GT,
200 template <
typename,
class>
class Itor,
202 template <
class,
class,
class>
class HeapT>
423 using Heap = HeapT<GT, Get_Potential_Arc, Heap_Info>;
472 template <
class IN,
class IA>
489 IN()(g, it.get_curr());
493 if constexpr (nothrow_arc_filter_v<GT, SA>)
495 if (
auto a = it.get_curr_ne();
sa(a))
504 if (
auto a = it.get_curr_ne();
sa(a))
519 IN::release(nit.get_curr());
520 if constexpr (nothrow_arc_filter_v<GT, SA>)
523 for (
size_t i = 0; i < num_arcs; ++i, ait.
next_ne())
524 IA::release(ait.get_curr());
529 for (
size_t i = 0; i < num_arcs; ++i, ait.
next_ne())
546 template <
class DN,
class DA>
561 template <
class DN,
class DA>
569 if constexpr (nothrow_arc_filter_v<GT, SA>)
590 [[gnu::always_inline]]
bool
596 if constexpr (std::is_integral_v<T>)
598 if (b > 0
and a > std::numeric_limits<T>::max() - b) [[unlikely]]
604 <<
"Integer underflow in distance addition: " << a <<
" + " << b;
625 [[gnu::noinline, gnu::cold]]
627 typename GT::Node* end =
nullptr)
const
643 std::ostringstream() <<
"(" << __FILE__ <<
":" << __LINE__ <<
") | "
644 <<
"the distance from the start to a node does not fit in its type";
645 for (Itor<GT, SA> it(p,
sa); it.has_curr(); it.next_ne())
646 if (
Node* t = it.get_tgt_node(); seen.
insert(t) !=
nullptr)
739 <<
"node is not reachable from start";
743 for (
auto curr = node; curr !=
s;)
746 if (parent ==
nullptr)
749 for (Itor<GT, SA> it(parent,
sa); it.has_curr(); it.next_ne())
751 auto arc = it.get_current_arc_ne();
752 auto tgt = it.get_tgt_node();
786 return Aleph::get_min_path<GT, Distance>(
s, end, path,
distance);
872 auto a = it.get_curr();
884 copy_painted_arcs(tree, g);
913 auto ts = mapped_node<GT>(
s);
914 auto te = mapped_node<GT>(end);
916 <<
"get_min_path(): the end is not in the tree";
922 <<
"get_min_path(): the end is not reached in the tree";
929 auto ga = mapped_arc<GT>(it.get_current_arc_ne());
941#undef SP_TREE_NODE_INFO
947#undef SP_TREE_ARC_INFO
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
WeightedDigraph::Node Node
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
size_t size_t int32_t * out
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
bool is_empty() const noexcept
Return true if this is empty.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Performs a depth-first search for a path between a pair of nodes.
Graph_Node< Node_Info > Node
The graph type.
Filtered iterator on the nodes of a graph.
Functor that traverses the arcs of a graph and performs an operation.
Functor that traverses the nodes of a graph and performs an operation.
Iterator on nodes and arcs of a path.
bool has_current_arc() const noexcept
Return true if iterator has current arc.
void empty()
Clean the path: all the nodes and arc are removed.
void init(Node *start_node)
Set the first node of a path.
void append(Arc *arc)
Append an arc to the path.
Generic RAII scope guard for cleanup operations on graphs.
Base class providing common infrastructure for shortest path algorithms.
Shortest_Path_Base(Distance dist=Distance(), SA __sa=SA())
Default constructor.
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extracts a shortest path to end from a previously painted graph.
void check_beyond_max(const GT &g, typename GT::Node *start, typename GT::Node *end=nullptr) const
After a painting in which a sum went beyond max(), throws if a node that start reaches was left out,...
Distance distance
Arc weights.
Get_Potential_Arc get_pot
Potential accessor.
Distance::Distance_Type copy_painted_min_paths_tree(GT &g, GT &tree)
Extracts the painted shortest paths tree and puts it in tree.
bool is_painted() const noexcept
Check if the graph has been painted.
auto uninit_on_exit()
Returns a guard that calls uninit() when it goes out of scope.
Distance::Distance_Type get_min_path(const GT &tree, typename GT::Node *end, Path< GT > &path)
Extracts path from tree to end node.
bool has_computation() const noexcept
Check if a computation has been performed.
Distance::Distance_Type checked_add(const typename Distance::Distance_Type &a, const typename Distance::Distance_Type &b) const
Checked addition to prevent integer overflow.
GT * get_graph() const noexcept
Get the graph of the last computation.
Distance::Distance_Type get_distance(typename GT::Node *node)
Gets the accumulated distance to a node after painting.
bool leave_out_beyond_max
Whether a painting leaves out without throwing the nodes whose distance does not fit.
GT::Node * get_start_node() const noexcept
Get the start node of the last computation.
bool add_if_fits(const typename Distance::Distance_Type &a, const typename Distance::Distance_Type &b, typename Distance::Distance_Type &out)
Adds a tentative distance that may go beyond max().
bool beyond_max
A sum of the painting went beyond max()
void uninit()
Cleanup algorithm state.
GT * ptr_g
Pointer to the graph.
bool painted
Whether graph has been painted.
HeapT< GT, Get_Potential_Arc, Heap_Info > Heap
void init(const GT &g, typename GT::Node *start)
Initialize algorithm state.
void set_bit(Node *node, int bit, int value) const noexcept
Set the control bit of node to value
static void map_arcs(A1 *p, A2 *q) noexcept
Map the arcs through their cookies.
void reset_bit(Node *node, int bit) const noexcept
Reset the bit of node (to zero)
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
RAII guards for graph node/arc cookies.
#define ARC_COOKIE(p)
Return the arc cookie
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define NODE_COOKIE(p)
Return the node cookie
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
bool add_if_fits(const T &a, const T &b, T &out)
Adds two distance values when the sum fits.
T checked_add(const T &a, const T &b)
Safely add two distance values with overflow checking.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
#define SP_TREE_ARC_INFO(p)
Convert arc cookie to Tree_Arc_Info pointer.
#define SP_TREE_NODE_INFO(p)
Convert node cookie to Tree_Node_Info pointer.
#define SP_ARC_INFO(p)
Convert arc cookie to Arc_Info pointer.
#define SP_PARENT(p)
Access parent pointer from node cookie.
#define SP_POT(p)
Access arc potential from arc cookie.
#define SP_TREEARC(p)
Access tree arc mapping from arc cookie.
#define SP_NODE_INFO(p)
Convert node cookie to Node_Info pointer.
#define SP_HEAPNODE(p)
Access heap node handle from node cookie.
#define SP_TREENODE(p)
Access tree node mapping from node cookie.
Filtered iterator on all the arcs of a graph.
Exception constructor with formatted message.
Information stored in arc cookies for painting version.
Distance::Distance_Type pot
Arc potential (priority)
Arc cleanup for painting version.
void operator()(const GT &, typename GT::Arc *ga) const noexcept
Node cleanup for painting version.
void operator()(const GT &, typename GT::Node *p) const noexcept
Arc cleanup and mapping for tree-building version.
void operator()(const GT &, typename GT::Arc *ga) const noexcept
Node cleanup and mapping for tree-building version.
void operator()(const GT &, typename GT::Node *p) const noexcept
Functor to get arc potential for heap ordering.
Distance::Distance_Type operator()(typename GT::Arc *a) const noexcept
Get_Potential_Arc(Distance &d) noexcept
Get_Potential_Arc()=default
Functor to access heap node handle from node cookie.
void *& operator()(typename GT::Node *p) const noexcept
Arc initialization for painting version.
void operator()(const GT &g, typename GT::Arc *a) const
static void release(typename GT::Arc *a) noexcept
Frees the cookie of a and leaves it null.
Node initialization for painting version.
void operator()(const GT &g, typename GT::Node *p) const
static void release(typename GT::Node *p) noexcept
Frees the cookie of p and leaves it null.
Arc initialization for tree-building version.
void operator()(const GT &g, typename GT::Arc *a) const
static void release(typename GT::Arc *a) noexcept
Frees the cookie of a and leaves it null.
Node initialization for tree-building version.
static void release(typename GT::Node *p) noexcept
Frees the cookie of p and leaves it null.
void operator()(const GT &g, typename GT::Node *p) const
Information stored in node cookies for painting version.
Distance::Distance_Type dist
Accumulated distance from start.
void * heap_node
Handle in priority queue.
void * ret_node
Parent node (for backtracking)
Arc filter that shows the arcs of the painted tree.
bool operator()(typename GT::Arc *a) const noexcept
Node filter that shows the nodes of the painted tree.
bool operator()(typename GT::Node *p) const noexcept
Distance totalizer class for path copying.
bool operator()(typename GT::Arc *a)
Adds the weight of an arc to dist.
Distance::Distance_Type dist
Total()=default
Builds the totalizer with a distance accessor built by Distance().
Distance distance
Distance accessor for the arc weights.
Total(Distance __distance)
Builds the totalizer with a distance accessor.
Extended arc info with tree arc mapping.
GT::Arc * tree_arc
Corresponding arc in spanning tree.
Extended node info with tree node mapping.
GT::Node * tree_node
Corresponding node in spanning tree.
The std::overflow_error of a painting that leaves out a node whose distance does not fit.
Dynamic queue implementation based on linked lists.
Dynamic set implementations based on balanced binary search trees.
Path finding algorithms in graphs.
Generic graph and digraph implementations.