Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_const_graph_view.H File Reference

Strict read-only access to a graph: Const_Graph_View and the read-only iterators on the nodes, on the arcs and on the arcs of a node. More...

#include <cstddef>
#include <type_traits>
#include <utility>
#include <ah-concepts.H>
#include <ah-graph-concepts.H>
#include <ah-iterator.H>
#include <htlist.H>
Include dependency graph for tpl_const_graph_view.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Aleph::Const_Node_Iterator< GT >
 Read-only iterator on the nodes of a graph. More...
 
class  Aleph::Const_Arc_Iterator< GT >
 Read-only iterator on the arcs of a graph. More...
 
class  Aleph::Const_Node_Arc_Iterator< GT >
 Read-only iterator on the arcs of a node. More...
 
class  Aleph::Const_Graph_View< GT >
 Strict read-only view of a graph. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 
namespace  Aleph::const_graph_view_detail
 

Functions

template<class T >
constexpr T * Aleph::const_graph_view_detail::as_graph_item (const T *p) noexcept
 Hands a node or an arc read through a view back to the graph.
 
template<AlephGraph GT>
Const_Graph_View< GT > Aleph::const_view (const GT &g) noexcept
 Strict read-only view of a graph.
 
template<AlephGraph GT>
void Aleph::const_view (const GT &&)=delete
 A view of a temporary graph would dangle.
 

Detailed Description

Strict read-only access to a graph: Const_Graph_View and the read-only iterators on the nodes, on the arcs and on the arcs of a node.

The const of a graph of Aleph-w protects its topology: the members that insert or remove nodes and arcs are not available through it. Its iterators and its traversals still hand out Node * and Arc *, because the algorithms that receive a const GT & keep their bookkeeping (control bits, counters and cookies) in the nodes and in the arcs. As a consequence, the information stored in a node or in an arc can be written through a const GT &:

void reader(const Graph & g)
{
g.for_each_arc([] (Graph::Arc * a) { a->get_info() = 0; }); // compiles
}
Arc Arc
The node class type.
Definition tpl_graph.H:444
void for_each_arc(Operation &op) const
Unconditionally traverse all the arcs of graph and on each one perform an operation.
Definition graph-dry.H:1805

That meaning is kept. What this header adds is the strict counterpart, for the code that must only read:

  • Const_Node_Iterator<GT>, Const_Arc_Iterator<GT> and Const_Node_Arc_Iterator<GT> wrap the iterators of the graph GT and hand out const Node * and const Arc *.
  • Const_Graph_View<GT> is a non-owning view of a graph whose whole interface is expressed with those pointers to const.
void reader(Const_Graph_View<Graph> g)
{
g.for_each_arc([] (const Graph::Arc * a) { use(a->get_info()); });
// g.for_each_arc([] (Graph::Arc * a) { ... }); does not compile
}
reader(const_view(graph));
Const_Graph_View< GT > const_view(const GT &g) noexcept
Strict read-only view of a graph.

Through a pointer to const neither the information (get_info() is a reference to const) nor the bookkeeping (NODE_BITS, NODE_COUNTER, NODE_COOKIE and their arc counterparts are not assignable) can be written. An algorithm that needs the bookkeeping does not compile on a view; it runs on the graph itself, which its owner keeps.

The view satisfies AlephGraph, with Node and Arc being the constant node and arc types. So the generic code written in terms of typename GT::Node * and typename GT::Arc * reads a view unchanged: the filtered iterators Node_Iterator<GT, SN>, Arc_Iterator<GT, SA> and Node_Arc_Iterator<GT, SA>, the free functions for_each_node(), for_each_arc(), forall_node()..., and the concepts NodeFilter, ArcFilter and ArcDistance, which then require operations on pointers to const. The algorithms that keep no bookkeeping in the graph, such as most_negative_cycle_bounded() and find_disjoint_negative_cycles(), run on a view too. They need a distance functor that takes a const Arc *: the default one, Dft_Dist, hands out a modifiable reference to the information of the arc and does not compile for a view.

struct Cost
{
using Distance_Type = long;
long operator () (const Graph::Arc * a) const { return a->get_info(); }
};
auto cycle = most_negative_cycle_bounded(const_view(graph), 4, Cost());
// cycle.cycle_arcs is a DynList<const Graph::Arc *>
Bounded_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_bounded(const GT &g, const size_t max_length, Distance distance=Distance(), SA sa=SA())
Most negative simple cycle of at most max_length arcs, ranked by total cost (not by mean cost as Karp...

Cost

A view is one pointer and its iterators are the iterators of the graph: nothing is copied and nothing is allocated per node or per arc. Only the members that build a list (nodes(), arcs(), filter_nodes(), nodes_map()...) allocate, as their counterparts of the graph do.

Lifetime and invalidation

A view does not own the graph: it is valid while the graph is alive and stays at the same address. It is not a snapshot: it shows the changes that the owner makes to the graph. An iterator obtained from a view is invalidated exactly when the iterator of the graph that it wraps is.

What a view does not protect

A view constrains what can be written through its interface; it is not a security boundary. The arcs keep their end points in the public data members src_node and tgt_node, which are void * and which the arc classes ask not to use: read through a const Arc * they still convert to a Node * with a cast. The same holds for what a cookie points to. Use get_src_node(), get_tgt_node() and get_connected_node().

Definition in file tpl_const_graph_view.H.