84#ifndef __ADS_StaticGraph
85 #define __ADS_StaticGraph
106#include <string_view>
108#include <type_traits>
143template<
class W ,
class T , T WeightedArc< W >::* M >
147 ArcMember(
void ) =
default;
150 explicit ArcMember(
const WeightedArc< W > * a ) : f_a( a ) { }
153 const T & operator[]( std::ptrdiff_t k )
const {
return( f_a[ k ].*M ); }
155 const T & operator*(
void )
const {
return( ( *f_a ).*M ); }
157 friend ArcMember operator+( ArcMember p , std::ptrdiff_t k ) {
161 friend bool operator==(
const ArcMember & ,
const ArcMember & ) =
default;
164 const WeightedArc< W > * f_a =
nullptr;
172class WeightedArcCursor
175 using iterator_concept = std::random_access_iterator_tag;
176 using iterator_category = std::input_iterator_tag;
177 using value_type = WeightedArc< W >;
178 using difference_type = std::ptrdiff_t;
179 using reference = WeightedArc< W >;
185 const WeightedArc< W > * operator->(
void )
const {
return( &a ); }
187 using pointer = Pointer;
189 WeightedArcCursor(
void ) =
default;
192 WeightedArcCursor(
const int * h ,
const W * w ) : f_h( h ) , f_w( w ) { }
195 reference operator*(
void )
const {
return( reference{ *f_h , *f_w } ); }
197 pointer operator->(
void )
const {
return( Pointer{ **
this } ); }
199 reference operator[]( difference_type k )
const {
200 return( reference{ f_h[ k ] , f_w[ k ] } );
203 WeightedArcCursor & operator++(
void ) {
208 WeightedArcCursor operator++(
int ) {
209 WeightedArcCursor c = *
this;
213 WeightedArcCursor & operator--(
void ) {
218 WeightedArcCursor operator--(
int ) {
219 WeightedArcCursor c = *
this;
223 WeightedArcCursor & operator+=( difference_type k ) {
228 WeightedArcCursor & operator-=( difference_type k ) {
233 friend WeightedArcCursor operator+( WeightedArcCursor c ,
234 difference_type k ) {
237 friend WeightedArcCursor operator+( difference_type k ,
238 WeightedArcCursor c ) {
241 friend WeightedArcCursor operator-( WeightedArcCursor c ,
242 difference_type k ) {
245 friend difference_type operator-(
const WeightedArcCursor & a ,
246 const WeightedArcCursor & b ) {
247 return( a.f_h - b.f_h );
249 friend bool operator==(
const WeightedArcCursor & a ,
250 const WeightedArcCursor & b ) {
251 return( a.f_h == b.f_h );
253 friend auto operator<=>(
const WeightedArcCursor & a ,
254 const WeightedArcCursor & b ) {
255 return( a.f_h <=> b.f_h );
259 [[nodiscard]]
const int * heads(
void )
const {
return( f_h ); }
261 [[nodiscard]]
const W * weights(
void )
const {
return( f_w ); }
264 const int * f_h =
nullptr;
265 const W * f_w =
nullptr;
273template<
class W = std::monostate , ArcLayout L = ArcLayout::Separate >
282 static constexpr bool weighted = ! std::is_same_v< W , std::monostate >;
292 using Arc = std::conditional_t< weighted , WeightedArc< W > ,
int >;
298 std::conditional_t< interleaved , const WeightedArc< W > * ,
299 detail::WeightedArcCursor< W > > >;
303 !
weighted , std::span< const int > ,
304 std::conditional_t< interleaved , std::span< const WeightedArc< W > > ,
305 std::ranges::subrange< ArcCursor > > >;
311 detail::ArcMember< W , int , &WeightedArc< W >::to > ,
316 interleaved , detail::ArcMember< W , W , &WeightedArc< W >::w > ,
351 StaticGraph(
int n ,
const std::vector< std::tuple< int , int , W > > & E ,
368 [[nodiscard]]
bool directed(
void )
const {
return( f_directed ); }
371 [[nodiscard]]
int n(
void )
const {
return(
int( f_off.size() ) - 1 ); }
374 [[nodiscard]]
int m(
void )
const {
return( f_m ); }
377 [[nodiscard]]
int arcs(
void )
const {
378 return(
int(
interleaved ? f_arc.size() : f_head.size() ) );
381 [[nodiscard]]
int bound(
void )
const {
return(
n() ); }
384 return( ( u >= 0 ) && ( u <
n() ) );
390 [[nodiscard]] std::vector< int >
V(
void )
const;
393 [[nodiscard]] std::vector< int >
adj(
int u )
const;
408 [[nodiscard]] std::optional< W >
weight(
int u ,
int v )
const
428 [[nodiscard]]
const int *
offsets(
void )
const {
return( f_off.data() ); }
432 [[nodiscard]]
const int *
heads(
void )
const
435 return( f_head.data() );
443 return( f_w.data() );
452 return( f_head.data() );
462 return( f_w.data() );
470 return( f_arc.data() );
472 return(
ArcCursor( f_head.data() , f_w.data() ) );
474 return( f_head.data() );
513 [[nodiscard]]
auto nodes(
void )
const {
514 return( std::views::iota( 0 ,
n() ) );
524 [[nodiscard]]
static constexpr std::uint64_t
persistTag(
void ) {
526 return( detail::persistMix(
527 {
sizeof( W ) , std::is_floating_point_v< W > ,
528 std::is_signed_v< W > } ) );
542 template<
class Edges ,
class Get >
543 void build(
int n ,
const Edges & E , Get && get );
547 [[nodiscard]]
int find(
int u ,
int v )
const;
550 [[nodiscard]] W & weightAt(
int k )
554 return( f_arc[ std::size_t( k ) ].w );
556 return( f_w[ std::size_t( k ) ] );
561 [[nodiscard]]
bool consistent(
void )
const;
563 bool f_directed =
true;
565 std::vector< int , MappableAllocator< int > > f_off;
567 std::vector< int , MappableAllocator< int > > f_head;
569 std::vector< W , MappableAllocator< W > > f_w;
571 std::vector< WeightedArc< W > > f_arc;
587 { g.offsets() } -> std::same_as< const int * >;
588 { g.heads() } -> std::same_as< const int * >;
589 } && ( ( !
requires {
requires G::weighted; } ) ||
requires(
const G & g )
593 } -> std::same_as< const typename G::Weight * >;
622template<
class W , ArcLayout L >
633#include "ADS/graph/impl/StaticGraph.tpp"
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
a static graph in compressed sparse rows, the arcs of each node sorted by head, their heads and their...
Definition StaticGraph.h:275
static int head(const Arc &a)
the head of an arc
Definition StaticGraph.h:478
void forEachAdj(int u, F &&f) const
bool setWeight(int u, int v, const W &w)
the weight of the first arc set to , and that of the first arc if the graph is unoriented,...
static constexpr ArcLayout layout
the layout of the arcs
Definition StaticGraph.h:285
static constexpr std::uint64_t persistTag(void)
the type of the weights, in a file
Definition StaticGraph.h:524
static constexpr bool interleaved
whether the arcs are the pairs of one array (never if unweighted)
Definition StaticGraph.h:288
static constexpr bool weighted
whether the arcs have a weight
Definition StaticGraph.h:282
StaticGraph(int n, const std::vector< std::tuple< int, int, W > > &E, bool directed=true)
the graph of the nodes and of the given edges; an unoriented edge is given once
StaticGraph relabel(const std::vector< int > &p) const
the same graph with the node renamed
int arcs(void) const
the number of arcs, 2 m() if the graph is unoriented
Definition StaticGraph.h:377
StaticGraph transpose(void) const
the graph with each arc reversed (the same graph if unoriented)
bool hasNode(int u) const
Definition StaticGraph.h:383
std::conditional_t< ! weighted, std::span< const int >, std::conditional_t< interleaved, std::span< const WeightedArc< W > >, std::ranges::subrange< ArcCursor > > > ArcRange
the arcs of a row, random access
Definition StaticGraph.h:302
std::conditional_t< interleaved, detail::ArcMember< W, int, &WeightedArc< W >::to >, const int * > HeadArray
the heads of the arcs read in place by their places: a pointer, or the heads of the pairs if interlea...
Definition StaticGraph.h:309
int m(void) const
the number of edges, an unoriented one counted once
Definition StaticGraph.h:374
auto nodes(void) const
the nodes, , random access
Definition StaticGraph.h:513
std::conditional_t< ! weighted, const int *, std::conditional_t< interleaved, const WeightedArc< W > *, detail::WeightedArcCursor< W > > > ArcCursor
the cursor of the arcs: a pointer to the heads if the graph is unweighted, to the pairs if interleave...
Definition StaticGraph.h:296
std::conditional_t< interleaved, detail::ArcMember< W, W, &WeightedArc< W >::w >, const W * > WeightArray
the weights of the arcs read in place by their places
Definition StaticGraph.h:315
std::vector< int > V(void) const
int bound(void) const
Definition StaticGraph.h:381
StaticGraph(const G &g)
the copy of any graph, the names that are not nodes of becoming isolated nodes
WeightArray arcWeights(void) const
the weights of all the arcs read in place, in either layout
Definition StaticGraph.h:456
StaticGraph(int n, const std::vector< std::pair< int, int > > &E, bool directed=true)
the unweighted graph of the nodes and of the edges
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
StaticGraph(void)
the graph with no node
Definition StaticGraph.h:320
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition StaticGraph.h:521
int n(void) const
the number of nodes
Definition StaticGraph.h:371
W Weight
the type of the weights
Definition StaticGraph.h:279
ArcRange out(int u) const
the arcs out of , contiguous, random access: on a weighted graph the WeightedArc values made from hea...
std::vector< int > adj(int u) const
std::conditional_t< weighted, WeightedArc< W >, int > Arc
an arc: its head, and its weight if the graph is weighted
Definition StaticGraph.h:292
ArcCursor arcData(void) const
all the arcs, row by row: the cursor at the first, whose place is the arc (the pointer heads() if t...
Definition StaticGraph.h:468
const W * weightData(void) const
the weights of all the arcs, in the places of heads(), an array of the separate layout
Definition StaticGraph.h:440
const int * offsets(void) const
the offsets of the rows: the arcs of u are those in the places [ offsets()[ u ] , offsets()[ u + 1 ] ...
Definition StaticGraph.h:428
bool directed(void) const
whether the graph is oriented
Definition StaticGraph.h:368
void forEachEdge(int u, F &&f) const
on each arc of weight
const int * heads(void) const
the heads of all the arcs, row by row, arcs() of them, an array of the separate layout
Definition StaticGraph.h:432
std::optional< W > weight(int u, int v) const
the weight of the first arc , nothing if there is none, by a binary search of the row of ,...
HeadArray arcHeads(void) const
the heads of all the arcs read in place, in either layout: heads(), or the heads of the pairs
Definition StaticGraph.h:448
a graph whose compressed rows are plain arrays, offsets(), heads() and, if weighted,...
Definition StaticGraph.h:585
The GRAPH of chapter 9 and its realizations.
ArcLayout
the layout of the arcs of a weighted StaticGraph
Definition StaticGraph.h:133
std::vector< int > bfsOrder(const StaticGraph< W, L > &g, int r=0)
the order of a visit in breadth from , then from the least node not reached, and so on: the name p[ u...
@ Interleaved
the pairs WeightedArc in one array
Definition StaticGraph.h:135
@ Separate
the heads and the weights in two arrays of the same places
Definition StaticGraph.h:134
an arc of a weighted StaticGraph, its head with its weight
Definition StaticGraph.h:127
W w
the weight
Definition StaticGraph.h:129
int to
the head
Definition StaticGraph.h:128