PBeFS
Parallel Best-First Search
Loading...
Searching...
No Matches
ExplicitAdjacency.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File ExplicitAdjacency.h ------------------------*/
3/*--------------------------------------------------------------------------*/
16/*--------------------------------------------------------------------------*/
17/*------------------------------ DEFINITIONS -------------------------------*/
18/*--------------------------------------------------------------------------*/
19
20#ifndef __PBeFS_ads_ExplicitAdjacency
21 #define __PBeFS_ads_ExplicitAdjacency
22 /* self-identification: #endif at the end of the file */
23
24/*--------------------------------------------------------------------------*/
25/*-------------------------------- INCLUDES --------------------------------*/
26/*--------------------------------------------------------------------------*/
27
28#include <atomic>
29
30#include <cstddef>
31
32#include <cstdint>
33
34#include <mutex>
35
36#include <optional>
37
38#include "ADS/graph/ArrayGraph.h"
39
40#include "ADS/graph/GraphBuilder.h"
41
42/*--------------------------------------------------------------------------*/
43/*------------------------------- NAMESPACE --------------------------------*/
44/*--------------------------------------------------------------------------*/
45
47namespace PBeFS_di_unipi_it
48{
49
50/*--------------------------------------------------------------------------*/
51/*------------------------ CLASS ExplicitAdjacency -------------------------*/
52/*--------------------------------------------------------------------------*/
54
61
62template< class Cost >
64
65/*------------------------ PUBLIC PART OF THE CLASS ------------------------*/
66
67 public:
68
70
71 explicit ExplicitAdjacency( std::size_t n )
72 : f_n( n ) , f_b( int( n ) , true ) {}
73
75
77 : f_n( o.f_n ) , f_b( o.f_b ) {}
78
80
82 if( this != & o ) {
83 f_n = o.f_n;
84 f_b = o.f_b;
85 f_g.reset();
86 f_frozen.store( false , std::memory_order_relaxed );
87 }
88 return( *this );
89 }
90
91/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
93
94 void add( std::uint32_t u , std::uint32_t v , Cost c ) {
95 if( u == v )
96 return;
97 f_b.addEdge( int( u ) , int( v ) , c );
98 f_frozen.store( false , std::memory_order_relaxed );
99 }
100
102
103 template< class F >
104 void for_each( std::uint32_t u , F && f ) const {
105 if( ! f_frozen.load( std::memory_order_acquire ) )
106 freeze();
107 f_g->forEachEdge( int( u ) , [ & ]( int v , const Cost & c ) {
108 f( std::uint32_t( v ) , c );
109 } );
110 }
111
113
114 [[nodiscard]] std::size_t size( void ) const noexcept { return( f_n ); }
115
116/*----------------------- PRIVATE PART OF THE CLASS ------------------------*/
117
118 private:
119
120 // builds the ArrayGraph of the arcs, once
121
122 void freeze( void ) const {
123 std::lock_guard< std::mutex > lock( f_lock );
124 if( f_frozen.load( std::memory_order_relaxed ) )
125 return;
126 f_g.emplace( f_b.toArrayGraph() );
127 f_frozen.store( true , std::memory_order_release );
128 }
129
130/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
131
132 std::size_t f_n;
133 ADS::GraphBuilder< Cost > f_b;
134 mutable std::optional< ADS::ArrayGraph< Cost > > f_g;
136 mutable std::atomic< bool > f_frozen{ false };
137 mutable std::mutex f_lock;
138
139 }; // end( class( ExplicitAdjacency ) )
140
141/*--------------------------------------------------------------------------*/
142
143} // end( namespace PBeFS_di_unipi_it )
144
145/*--------------------------------------------------------------------------*/
146
147#endif /* __PBeFS_ads_ExplicitAdjacency */
148
149/*--------------------------------------------------------------------------*/
150/*---------------------- End File ExplicitAdjacency.h ----------------------*/
151/*--------------------------------------------------------------------------*/
void for_each(std::uint32_t u, F &&f) const
calls f( v , c ) for each arc ( u , v ) of cost c
Definition ExplicitAdjacency.h:104
ExplicitAdjacency(std::size_t n)
constructor of n nodes and no arc
Definition ExplicitAdjacency.h:71
void add(std::uint32_t u, std::uint32_t v, Cost c)
adds the arc ( u , v ) of cost c
Definition ExplicitAdjacency.h:94
std::size_t size(void) const noexcept
the number of nodes
Definition ExplicitAdjacency.h:114
ExplicitAdjacency & operator=(const ExplicitAdjacency &o)
copy assignment: the arcs, to be frozen again
Definition ExplicitAdjacency.h:81
ExplicitAdjacency(const ExplicitAdjacency &o)
copy constructor: the arcs, to be frozen again
Definition ExplicitAdjacency.h:76
namespace for the Parallel Best-First Search library
Definition AStar.h:83