55#ifndef __ADS_interop_boost_Graph
56 #define __ADS_interop_boost_Graph
59#if defined( ADS_WITH_BOOST )
70#include <boost/graph/graph_traits.hpp>
71#include <boost/graph/properties.hpp>
72#include <boost/iterator/counting_iterator.hpp>
73#include <boost/iterator/iterator_facade.hpp>
74#include <boost/property_map/property_map.hpp>
102template<
class W , ArcLayout L >
103struct Rows< StaticGraph< W , L > >
106 static constexpr bool weighted = StaticGraph< W , L >::weighted;
108 static const int * offsets(
const StaticGraph< W , L > & g ) {
109 return( g.offsets() );
112 static int head(
const StaticGraph< W , L > & g ,
int k ) {
113 return( g.arcHeads()[ k ] );
116 static const W & weight(
const StaticGraph< W , L > & g ,
int k )
119 return( g.arcWeights()[ k ] );
126struct Rows< ArrayGraph< W > >
129 static constexpr bool weighted = ! std::is_same_v< W , std::monostate >;
131 static const int * offsets(
const ArrayGraph< W > & g ) {
132 return( g.offsets() );
135 static int head(
const ArrayGraph< W > & g ,
int k ) {
136 return( g.heads()[ k ] );
139 static const W & weight(
const ArrayGraph< W > & g ,
int k )
142 return( g.weightData()[ k ] );
150concept RowGraph =
requires { Rows< G >::weighted; };
158 friend bool operator==( Arc , Arc ) =
default;
164 :
public boost::iterator_facade< OutArcIterator , Arc ,
165 boost::random_access_traversal_tag , Arc >
169 OutArcIterator(
void ) =
default;
171 OutArcIterator(
int u ,
int k ) : f_u( u ) , f_k( k ) { }
174 friend class boost::iterator_core_access;
175 Arc dereference(
void )
const {
return( Arc{ f_u , f_k } ); }
176 bool equal(
const OutArcIterator & o )
const {
return( f_k == o.f_k ); }
177 void increment(
void ) { ++f_k; }
178 void decrement(
void ) { --f_k; }
179 void advance( std::ptrdiff_t d ) { f_k += int( d ); }
180 std::ptrdiff_t distance_to(
const OutArcIterator & o )
const {
181 return( o.f_k - f_k );
192 :
public boost::iterator_facade< AllArcIterator< G > , Arc ,
193 boost::forward_traversal_tag , Arc >
197 AllArcIterator(
void ) =
default;
199 AllArcIterator(
const G & g ,
int k ) : f_g( &g ) , f_k( k ) { skip(); }
202 friend class boost::iterator_core_access;
205 const int * off = Rows< G >::offsets( *f_g );
206 while( ( f_u < f_g->n() ) && ( off[ f_u + 1 ] <= f_k ) )
209 Arc dereference(
void )
const {
return( Arc{ f_u , f_k } ); }
210 bool equal(
const AllArcIterator & o )
const {
return( f_k == o.f_k ); }
211 void increment(
void ) {
216 const G * f_g =
nullptr;
224 :
public boost::iterator_facade< HeadIterator< G > , int ,
225 boost::random_access_traversal_tag , int >
229 HeadIterator(
void ) =
default;
231 HeadIterator(
const G & g ,
int k ) : f_g( &g ) , f_k( k ) { }
234 friend class boost::iterator_core_access;
235 int dereference(
void )
const {
return( Rows< G >::head( *f_g , f_k ) ); }
236 bool equal(
const HeadIterator & o )
const {
return( f_k == o.f_k ); }
237 void increment(
void ) { ++f_k; }
238 void decrement(
void ) { --f_k; }
239 void advance( std::ptrdiff_t d ) { f_k += int( d ); }
240 std::ptrdiff_t distance_to(
const HeadIterator & o )
const {
241 return( o.f_k - f_k );
244 const G * f_g =
nullptr;
254 using key_type = Arc;
255 using value_type =
typename Rows< G >::Weight;
256 using reference =
const value_type &;
257 using category = boost::readable_property_map_tag;
258 const G * g =
nullptr;
260 friend reference get(
const RowWeightMap & m , Arc a ) {
261 return( Rows< G >::weight( *m.g , a.pos ) );
268 using key_type = Arc;
269 using value_type = int;
270 using reference = int;
271 using category = boost::readable_property_map_tag;
273 friend int get( ArcIndexMap , Arc a ) {
return( a.pos ); }
278struct row_traversal :
virtual boost::incidence_graph_tag ,
279 virtual boost::adjacency_graph_tag ,
280 virtual boost::vertex_list_graph_tag ,
281 virtual boost::edge_list_graph_tag
288 using vertex_descriptor = int;
289 using edge_descriptor = Arc;
290 using directed_category = boost::directed_tag;
291 using edge_parallel_category = boost::allow_parallel_edge_tag;
292 using traversal_category = row_traversal;
293 using vertex_iterator = boost::counting_iterator< int >;
294 using out_edge_iterator = OutArcIterator;
295 using adjacency_iterator = HeadIterator< G >;
296 using edge_iterator = AllArcIterator< G >;
297 using in_edge_iterator = void;
298 using vertices_size_type = int;
299 using edges_size_type = int;
300 using degree_size_type = int;
302 static vertex_descriptor null_vertex(
void ) {
return( -1 ); }
318template<
class W ,
class T >
319struct Lists< ListGraph< W , T > >
323 static const ListGraph< W , T > & lists(
const ListGraph< W , T > & g ) {
330template<
class W ,
class T ,
class P >
331struct Lists< ObservedGraph< ListGraph< W , T > , P > >
335 static const ListGraph< W , T > &
336 lists(
const ObservedGraph< ListGraph< W , T > , P > & g ) {
345concept ListedGraph =
requires {
typename Lists< G >::Weight; };
352 friend bool operator==( Edge , Edge ) =
default;
359 :
public boost::iterator_facade< NodeIterator< G > , int ,
360 boost::forward_traversal_tag , int >
364 NodeIterator(
void ) =
default;
366 NodeIterator(
const G & g ,
int u ) : f_g( &g ) , f_u( u ) { skip(); }
369 friend class boost::iterator_core_access;
371 while( ( f_u < f_g->bound() ) && ! f_g->hasNode( f_u ) )
374 int dereference(
void )
const {
return( f_u ); }
375 bool equal(
const NodeIterator & o )
const {
return( f_u == o.f_u ); }
376 void increment(
void ) {
381 const G * f_g =
nullptr;
388 :
public boost::iterator_facade< OutEdgeIterator< I > , Edge ,
389 boost::forward_traversal_tag , Edge >
393 OutEdgeIterator(
void ) =
default;
395 OutEdgeIterator(
int u , I i ) : f_u( u ) , f_i( i ) { }
398 friend class boost::iterator_core_access;
399 Edge dereference(
void )
const {
return( Edge{ f_u , int( *f_i ) } ); }
400 bool equal(
const OutEdgeIterator & o )
const {
return( f_i == o.f_i ); }
401 void increment(
void ) { ++f_i; }
409class AdjacentIterator
410 :
public boost::iterator_facade< AdjacentIterator< I > , int ,
411 boost::forward_traversal_tag , int >
415 AdjacentIterator(
void ) =
default;
417 explicit AdjacentIterator( I i ) : f_i( i ) { }
420 friend class boost::iterator_core_access;
421 int dereference(
void )
const {
return(
int( *f_i ) ); }
422 bool equal(
const AdjacentIterator & o )
const {
return( f_i == o.f_i ); }
423 void increment(
void ) { ++f_i; }
435 using key_type = Edge;
436 using value_type =
typename Lists< G >::Weight;
437 using reference = value_type;
438 using category = boost::readable_property_map_tag;
439 const G * g =
nullptr;
442 friend value_type get(
const ListWeightMap & m , Edge e ) {
443 return( *Lists< G >::lists( *m.g ).weight( e.src , e.dst ) );
449struct list_traversal :
virtual boost::incidence_graph_tag ,
450 virtual boost::adjacency_graph_tag ,
451 virtual boost::vertex_list_graph_tag
459 using List = std::ranges::iterator_t<
460 decltype( std::declval<
const decltype( Lists< G >::lists(
461 std::declval< const G & >() ) ) & >()
463 using vertex_descriptor = int;
464 using edge_descriptor = Edge;
465 using directed_category = boost::directed_tag;
466 using edge_parallel_category = boost::disallow_parallel_edge_tag;
467 using traversal_category = list_traversal;
468 using vertex_iterator = NodeIterator< G >;
469 using out_edge_iterator = OutEdgeIterator< List >;
470 using adjacency_iterator = AdjacentIterator< List >;
471 using edge_iterator = void;
472 using in_edge_iterator = void;
473 using vertices_size_type = int;
474 using edges_size_type = int;
475 using degree_size_type = int;
477 static vertex_descriptor null_vertex(
void ) {
return( -1 ); }
491template<
class W , ADS::ArcLayout L >
492struct graph_traits< ADS::StaticGraph< W , L > >
493 : ADS::bgl::RowTraits< ADS::StaticGraph< W , L > >
497struct graph_traits< ADS::ArrayGraph< W > >
498 : ADS::bgl::RowTraits< ADS::ArrayGraph< W > >
501template<
class W ,
class T >
502struct graph_traits< ADS::ListGraph< W , T > >
503 : ADS::bgl::ListTraits< ADS::ListGraph< W , T > >
506template<
class W ,
class T ,
class P >
507struct graph_traits< ADS::ObservedGraph< ADS::ListGraph< W , T > , P > >
508 : ADS::bgl::ListTraits< ADS::ObservedGraph< ADS::ListGraph< W , T > , P > >
512 requires( ADS::bgl::RowGraph< G > || ADS::bgl::ListedGraph< G > )
513struct graph_traits< const G > : graph_traits< G >
517 requires ADS::bgl::RowGraph< std::remove_const_t< G > >
518struct property_map< G , edge_weight_t >
520 using type = ADS::bgl::RowWeightMap< std::remove_const_t< G > >;
521 using const_type =
type;
525 requires ADS::bgl::ListedGraph< std::remove_const_t< G > >
526struct property_map< G , edge_weight_t >
528 using type = ADS::bgl::ListWeightMap< std::remove_const_t< G > >;
529 using const_type =
type;
533 requires( ADS::bgl::RowGraph< std::remove_const_t< G > > ||
534 ADS::bgl::ListedGraph< std::remove_const_t< G > > )
535struct property_map< G , vertex_index_t >
537 using type = typed_identity_property_map< int >;
538 using const_type =
type;
542 requires ADS::bgl::RowGraph< std::remove_const_t< G > >
543struct property_map< G , edge_index_t >
545 using type = ADS::bgl::ArcIndexMap;
546 using const_type =
type;
562template< bgl::RowGraph G >
563std::pair< boost::counting_iterator< int > , boost::counting_iterator< int > >
564vertices(
const G & g )
566 return( std::pair( boost::counting_iterator< int >( 0 ) ,
567 boost::counting_iterator< int >( g.n() ) ) );
570template< bgl::RowGraph G >
571int num_vertices(
const G & g )
576template< bgl::RowGraph G >
577int num_edges(
const G & g )
579 return( bgl::Rows< G >::offsets( g )[ g.n() ] );
582template< bgl::RowGraph G >
583std::pair< bgl::OutArcIterator , bgl::OutArcIterator >
584out_edges(
int u ,
const G & g )
586 const int * off = bgl::Rows< G >::offsets( g );
587 return( std::pair( bgl::OutArcIterator( u , off[ u ] ) ,
588 bgl::OutArcIterator( u , off[ u + 1 ] ) ) );
591template< bgl::RowGraph G >
592int out_degree(
int u ,
const G & g )
594 const int * off = bgl::Rows< G >::offsets( g );
595 return( off[ u + 1 ] - off[ u ] );
598template< bgl::RowGraph G >
599int source( bgl::Arc a ,
const G & )
604template< bgl::RowGraph G >
605int target( bgl::Arc a ,
const G & g )
607 return( bgl::Rows< G >::head( g , a.pos ) );
610template< bgl::RowGraph G >
611std::pair< bgl::HeadIterator< G > , bgl::HeadIterator< G > >
612adjacent_vertices(
int u ,
const G & g )
614 const int * off = bgl::Rows< G >::offsets( g );
615 return( std::pair( bgl::HeadIterator< G >( g , off[ u ] ) ,
616 bgl::HeadIterator< G >( g , off[ u + 1 ] ) ) );
621template<
class W , ArcLayout L >
622std::pair< bgl::AllArcIterator< StaticGraph< W , L > > ,
623 bgl::AllArcIterator< StaticGraph< W , L > > >
626 using It = bgl::AllArcIterator< StaticGraph< W , L > >;
627 return( std::pair( It( g , 0 ) , It( g , num_edges( g ) ) ) );
631std::pair< bgl::AllArcIterator< ArrayGraph< W > > ,
632 bgl::AllArcIterator< ArrayGraph< W > > >
635 using It = bgl::AllArcIterator< ArrayGraph< W > >;
636 return( std::pair( It( g , 0 ) , It( g , num_edges( g ) ) ) );
639template< bgl::RowGraph G >
640 requires bgl::Rows< G >::weighted
641bgl::RowWeightMap< G > get( boost::edge_weight_t ,
const G & g )
643 return( bgl::RowWeightMap< G >{ &g } );
646template< bgl::RowGraph G >
647 requires bgl::Rows< G >::weighted
648const typename bgl::Rows< G >::Weight & get( boost::edge_weight_t ,
652 return( bgl::Rows< G >::weight( g , a.pos ) );
655template< bgl::RowGraph G >
656bgl::ArcIndexMap get( boost::edge_index_t ,
const G & )
658 return( bgl::ArcIndexMap() );
661template< bgl::RowGraph G >
662int get( boost::edge_index_t ,
const G & , bgl::Arc a )
669template< bgl::ListedGraph G >
670std::pair< bgl::NodeIterator< G > , bgl::NodeIterator< G > >
671vertices(
const G & g )
673 return( std::pair( bgl::NodeIterator< G >( g , 0 ) ,
674 bgl::NodeIterator< G >( g , g.bound() ) ) );
677template< bgl::ListedGraph G >
678int num_vertices(
const G & g )
683template< bgl::ListedGraph G >
684auto out_edges(
int u ,
const G & g )
686 using It =
typename boost::graph_traits< G >::out_edge_iterator;
687 const auto r = bgl::Lists< G >::lists( g ).neighbors( u );
688 return( std::pair< It , It >( It( u , r.begin() ) , It( u , r.end() ) ) );
691template< bgl::ListedGraph G >
692int out_degree(
int u ,
const G & g )
694 const auto r = bgl::Lists< G >::lists( g ).neighbors( u );
695 return(
int( std::ranges::distance( r ) ) );
698template< bgl::ListedGraph G >
699int source( bgl::Edge e ,
const G & )
704template< bgl::ListedGraph G >
705int target( bgl::Edge e ,
const G & )
710template< bgl::ListedGraph G >
711auto adjacent_vertices(
int u ,
const G & g )
713 using It =
typename boost::graph_traits< G >::adjacency_iterator;
714 const auto r = bgl::Lists< G >::lists( g ).neighbors( u );
715 return( std::pair< It , It >( It( r.begin() ) , It( r.end() ) ) );
718template< bgl::ListedGraph G >
719std::pair< bgl::Edge , bool > edge(
int u ,
int v ,
const G & g )
721 return( std::pair( bgl::Edge{ u , v } ,
722 bgl::Lists< G >::lists( g ).hasEdge( u , v ) ) );
725template< bgl::ListedGraph G >
726std::pair< bgl::Edge , bool > add_edge(
int u ,
int v , G & g )
728 if( bgl::Lists< G >::lists( g ).hasEdge( u , v ) )
729 return( std::pair( bgl::Edge{ u , v } , false ) );
730 g.insertEdge( u , v );
731 return( std::pair( bgl::Edge{ u , v } , true ) );
734template< bgl::ListedGraph G >
735std::pair< bgl::Edge , bool >
736add_edge(
int u ,
int v ,
const typename bgl::Lists< G >::Weight & w , G & g )
738 if( bgl::Lists< G >::lists( g ).hasEdge( u , v ) )
739 return( std::pair( bgl::Edge{ u , v } , false ) );
740 g.insertEdge( u , v , w );
741 return( std::pair( bgl::Edge{ u , v } , true ) );
744template< bgl::ListedGraph G >
745void remove_edge(
int u ,
int v , G & g )
747 if( bgl::Lists< G >::lists( g ).hasEdge( u , v ) )
748 g.deleteEdge( u , v );
751template< bgl::ListedGraph G >
752void remove_edge( bgl::Edge e , G & g )
754 remove_edge( e.src , e.dst , g );
757template< bgl::ListedGraph G >
758int add_vertex( G & g )
760 return( g.insertNode() );
763template< bgl::ListedGraph G >
764void clear_vertex(
int u , G & g )
766 const auto & L = bgl::Lists< G >::lists( g );
767 for(
int v : L.adj( u ) )
768 if( L.hasEdge( u , v ) )
769 g.deleteEdge( u , v );
771 for(
int v : L.inAdj( u ) )
772 g.deleteEdge( v , u );
775template< bgl::ListedGraph G >
776void remove_vertex(
int u , G & g )
781template< bgl::ListedGraph G >
782bgl::ListWeightMap< G > get( boost::edge_weight_t ,
const G & g )
784 return( bgl::ListWeightMap< G >{ &g } );
787template< bgl::ListedGraph G >
788typename bgl::Lists< G >::Weight get( boost::edge_weight_t ,
const G & g ,
791 return( *bgl::Lists< G >::lists( g ).weight( e.src , e.dst ) );
797 requires( bgl::RowGraph< G > || bgl::ListedGraph< G > )
798boost::typed_identity_property_map< int > get( boost::vertex_index_t ,
801 return( boost::typed_identity_property_map< int >() );
805 requires( bgl::RowGraph< G > || bgl::ListedGraph< G > )
806int get( boost::vertex_index_t ,
const G & ,
int u )
The static GRAPH with vectors of adjacency of section 9.4 [see Graph.h]: the adjacent nodes of all th...
The GRAPH with lists of adjacency of section 9.4 [see Graph.h]: for each node the LIST of its adjacen...
A GRAPH that tells its changes, beyond the book: the wrapper Observable< G , O... > routes the change...
A static GRAPH for the engineered algorithms, beyond the book [see Graph.h]: the compressed sparse ro...
a static graph with vectors of adjacency, all in one vector
Definition ArrayGraph.h:62
a static graph in compressed sparse rows, the arcs of each node sorted by head, their heads and their...
Definition StaticGraph.h:275
std::vector< WeightedEdge< typename G::Weight > > edges(const G &g)
the edges of an unoriented graph, each once, from its smaller end
GrB_Type type(void)
the type of GraphBLAS of the numbers T: GrB_BOOL, the integers of 8 to 64 bits with or without sign,...
Definition GraphBLAS.h:128