ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
GridGraph.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File GridGraph.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
115/*--------------------------------------------------------------------------*/
116/*----------------------------- DEFINITIONS --------------------------------*/
117/*--------------------------------------------------------------------------*/
118
119#ifndef __ADS_GridGraph
120 #define __ADS_GridGraph
121/* self-identification: #endif constant */
122
123/*--------------------------------------------------------------------------*/
124/*------------------------------ INCLUDES ----------------------------------*/
125/*--------------------------------------------------------------------------*/
126
127#include "ADS/Contract.h"
128#include "ADS/Modification.h"
129#include "ADS/Portable.h"
130#include "ADS/graph/Graph.h"
131#include "ADS/graph/Grid.h"
132
133#include <cstddef>
134#include <istream>
135#include <optional>
136#include <ranges>
137#include <type_traits>
138#include <vector>
139
140/*--------------------------------------------------------------------------*/
141/*--------------------------- NAMESPACE ------------------------------------*/
142/*--------------------------------------------------------------------------*/
143
144namespace ADS
145{
146
147/*--------------------------------------------------------------------------*/
148/*-------------------------------- TYPES -----------------------------------*/
149/*--------------------------------------------------------------------------*/
150
152enum class GridMoves {
155 };
156
158enum class Corners {
162 };
163
165enum class MoveCost {
168 };
169
170/*--------------------------------------------------------------------------*/
171/*----------------------------- CLASS GridGraph ----------------------------*/
172/*--------------------------------------------------------------------------*/
173
177template< class W = int , class C = W , class Policy = NoObservers >
179{
180
181/*--------------------------------------------------------------------------*/
182 public:
183 using Weight = W;
184 using Cost = C;
190 using Subject = typename Policy::template bind< modifications >;
193
201 Corners corners = Corners::NO_CUT , W straight = W( 1 ) ,
202 std::optional< W > diagonal = std::nullopt ,
203 MoveCost model = MoveCost::ENTERED );
204
212 GridGraph( int rows , int cols , std::vector< C > cost ,
214 Corners corners = Corners::NO_CUT , W straight = W( 1 ) ,
215 std::optional< W > diagonal = std::nullopt ,
216 MoveCost model = MoveCost::ENTERED );
217
221 template< class... O >
222 requires( sizeof...( O ) > 0 )
223 GridGraph( int rows , int cols , std::vector< C > cost , GridMoves moves ,
224 Corners corners , W straight , std::optional< W > diagonal ,
225 MoveCost model , O &... o );
226
228 [[nodiscard]] int rows( void ) const { return( f_rows ); }
229
231 [[nodiscard]] int cols( void ) const { return( f_cols ); }
232
234 [[nodiscard]] GridMoves moves( void ) const { return( f_moves ); }
235
237 [[nodiscard]] Corners corners( void ) const { return( f_corners ); }
238
240 [[nodiscard]] MoveCost moveCost( void ) const { return( f_model ); }
241
243 [[nodiscard]] W straight( void ) const { return( f_straight ); }
244
246 [[nodiscard]] W diagonal( void ) const { return( f_diagonal ); }
247
249 [[nodiscard]] bool directed( void ) const { return( f_directed ); }
250
252 [[nodiscard]] int n( void ) const { return( f_n ); }
253
256 [[nodiscard]] int bound( void ) const { return( f_rows * f_cols ); }
257
259 [[nodiscard]] bool hasNode( int u ) const {
260 return( ( u >= 0 ) && ( u < bound() ) && open( u ) );
261 }
262
265 [[nodiscard]] int node( int r , int c ) const;
266
269 [[nodiscard]] GridCell cell( int u ) const;
270
273 [[nodiscard]] C cost( int u ) const;
274
277 [[nodiscard]] C minCost( void ) const { return( f_min ); }
278
312 void setCost( int u , const C & c );
313
342 template< class F >
343 void forEachAffected( int u , const C & from , const C & to , F && f ) const;
344
346 [[nodiscard]] std::vector< int > V( void ) const;
347
350 [[nodiscard]] std::vector< int > adj( int u ) const;
351
355 template< class F >
356 void forEachAdj( int u , F && f ) const;
357
361 template< class F >
362 void forEachEdge( int u , F && f ) const;
363
370 template< class F >
371 void forEachMove( int u , F && f ) const;
372
373/*--------------------------- THE OBSERVERS --------------------------------*/
374
377 template< class D >
378 int attach( D & o )
379 requires( Subject::dynamic )
380 {
381 return( f_sub.attach( o ) );
382 }
383
387 void detach( int id )
388 requires( Subject::dynamic )
389 {
390 f_sub.detach( id );
391 }
392
395 [[nodiscard]] Batch batch( void ) { return( Batch( *this ) ); }
396
398 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
400 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
401
402/*------------------------- THE STANDARD INTERFACE -------------------------*/
403
406 [[nodiscard]] auto nodes( void ) const {
407 return( std::views::iota( 0 , bound() ) |
408 std::views::filter( [ this ]( int u ) { return( open( u ) ); } ) );
409 }
410
411/*--------------------------------------------------------------------------*/
412 private:
414 void init( std::optional< W > diagonal );
415
417 [[nodiscard]] bool open( int u ) const {
418 return( f_cost.empty() || ( f_cost[ u ] > C( 0 ) ) );
419 }
420
423 [[nodiscard]] bool corner( bool a , bool b ) const {
424 return( ( f_corners == Corners::CUT ) ||
425 ( f_corners == Corners::NO_SQUEEZE ? ( a || b ) : ( a && b ) ) );
426 }
427
429 template< class G >
430 void scan( int u , G && g ) const;
431
433 [[nodiscard]] bool counted( void ) const;
434
436 void recount( void );
437
439 ADS_COLD_PATH void tellCold( const CellChanged & e ) {
440 f_sub.emit( e );
441 f_sub.audit( *this );
442 }
443
444 int f_rows;
445 int f_cols;
446 GridMoves f_moves;
447 Corners f_corners;
448 MoveCost f_model;
449 W f_straight;
450 W f_diagonal;
451 std::vector< C > f_cost;
452 int f_n = 0;
453 int f_atmin = 0;
454 C f_min = C( 1 );
455 bool f_directed = false;
457
458 }; // end( class GridGraph )
459
460/*--------------------------------------------------------------------------*/
461/*------------------------------- FUNCTIONS --------------------------------*/
462/*--------------------------------------------------------------------------*/
463
489template< class W , class C , class P >
490std::vector< int > gridDistances( const GridGraph< W , C , P > & g ,
491 const std::vector< int > & S );
492
498template< class W = int >
499W manhattan( GridCell a , GridCell b , W straight = W( 1 ) );
500
508template< class W >
509W octile( GridCell a , GridCell b , W straight , W diagonal );
510
516template< class W = int >
517W chebyshev( GridCell a , GridCell b , W straight = W( 1 ) );
518
542template< class W , class C , class P >
543W gridHeuristic( const GridGraph< W , C , P > & g , int u , int t );
544
564template< class W = double , class C = unsigned char >
565GridGraph< W , C > readGridMap( std::istream & in ,
567 Corners corners = Corners::NO_CUT );
568
569/*--------------------------------------------------------------------------*/
570
571} // end( namespace ADS )
572
573/*--------------------------------------------------------------------------*/
574/*-------------------------- IMPLEMENTATION --------------------------------*/
575/*--------------------------------------------------------------------------*/
576
577#include "ADS/graph/impl/GridGraph.tpp"
578
579/*--------------------------------------------------------------------------*/
580
581#endif /* GridGraph.h included */
582
583/*--------------------------------------------------------------------------*/
584/*------------------------- End File GridGraph.h ---------------------------*/
585/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The visits of a GRID, the implicit graph whose nodes are the cells of a matrix and whose edges are ...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
the implicit graph of a grid map, the cells of cost and the weights of the moves of type ,...
Definition GridGraph.h:179
const Subject & subject(void) const
the subject
Definition GridGraph.h:400
W straight(void) const
the cost of a move to a side on a map of unit costs
Definition GridGraph.h:243
GridGraph(int rows, int cols, GridMoves moves=GridMoves::FOUR, Corners corners=Corners::NO_CUT, W straight=W(1), std::optional< W > diagonal=std::nullopt, MoveCost model=MoveCost::ENTERED)
the grid of free cells of cost 1, with no vector; the diagonal cost times the straight one if not g...
std::vector< int > V(void) const
the set of the nodes, in increasing order
void forEachMove(int u, F &&f) const
on each neighbor of , in increasing order, whether the move is to a corner, with no allocation: the...
GridMoves moves(void) const
the moves
Definition GridGraph.h:234
C minCost(void) const
the least cost of a free cell, 1 if there is no vector of the costs or no free cell
Definition GridGraph.h:277
GridGraph(int rows, int cols, std::vector< C > cost, GridMoves moves=GridMoves::FOUR, Corners corners=Corners::NO_CUT, W straight=W(1), std::optional< W > diagonal=std::nullopt, MoveCost model=MoveCost::ENTERED)
the grid of cells of the given costs, that of the cell , a cell blocked if its cost is not positive...
MoveCost moveCost(void) const
the cell whose cost weighs a move
Definition GridGraph.h:240
C cost(int u) const
the cost of the cell , not positive if blocked
void forEachAdj(int u, F &&f) const
on each neighbor of , in increasing order, with no allocation
typename Policy::template bind< modifications > Subject
the subject
Definition GridGraph.h:190
int n(void) const
the number of nodes, the free cells
Definition GridGraph.h:252
void setCost(int u, const C &c)
the cost of the cell set to , the cell blocked if is not positive and free otherwise,...
C Cost
the type of the costs of the cells
Definition GridGraph.h:184
mod::Changed< mod::Value, int, C > CellChanged
the event of a change of the cost of a cell, the cell its handle
Definition GridGraph.h:186
void forEachAffected(int u, const C &from, const C &to, F &&f) const
, in increasing order of , on each cell whose moves in may have been created, removed or weighed dif...
void detach(int id)
detaches the observer number
Definition GridGraph.h:387
GridGraph(int rows, int cols, std::vector< C > cost, GridMoves moves, Corners corners, W straight, std::optional< W > diagonal, MoveCost model, O &... o)
the grid of the given costs as above, every parameter given, and the static observers o....
bool hasNode(int u) const
whether is a free cell
Definition GridGraph.h:259
auto nodes(void) const
the nodes in increasing order, a view on the names that skips the blocked cells
Definition GridGraph.h:406
mod::list< CellChanged > modifications
the events
Definition GridGraph.h:188
int rows(void) const
the number of rows
Definition GridGraph.h:228
ADS::Batch< GridGraph > Batch
the channel of a batch, open while it lives [see batch()]
Definition GridGraph.h:192
int node(int r, int c) const
the node of the cell
bool directed(void) const
whether the graph is oriented, i.e., two free cells cost differently
Definition GridGraph.h:249
void forEachEdge(int u, F &&f) const
on each neighbor of , the cost of the move, in increasing order of
GridCell cell(int u) const
the cell of the name
int cols(void) const
the number of columns
Definition GridGraph.h:231
Corners corners(void) const
the rule on the corners
Definition GridGraph.h:237
int attach(D &o)
attaches the observer and gives its number
Definition GridGraph.h:378
Batch batch(void)
opens a batch: the events are collected until it is closed, and sent coalesced
Definition GridGraph.h:395
int bound(void) const
the names of the nodes are in
Definition GridGraph.h:256
std::vector< int > adj(int u) const
the neighbors of , in increasing order
W Weight
the type of the weights
Definition GridGraph.h:183
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition GridGraph.h:398
W diagonal(void) const
the cost of a move to a corner on a map of unit costs
Definition GridGraph.h:246
The GRAPH of chapter 9 and its realizations.
#define ADS_COLD_PATH
a function of a rare path: never inlined, laid out away from the hot code
Definition Portable.h:107
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
GridMoves
the moves of a GridGraph
Definition GridGraph.h:152
W octile(GridCell a, GridCell b, W straight, W diagonal)
the octile distance between the cells and , with and the larger and the smaller of and ,...
MoveCost
which cell of a move of a GridGraph weighs it
Definition GridGraph.h:165
Corners
when a move to a corner of a cell is allowed, the corner free
Definition GridGraph.h:158
W manhattan(GridCell a, GridCell b, W straight=W(1))
the distance of Manhattan between the cells and , , the least cost of a path of moves to the sides o...
W gridHeuristic(const GridGraph< W, C, P > &g, int u, int t)
a lower bound on the cost of a path from to in , consistent, for the A* of Hart,...
GridGraph< W, C > readGridMap(std::istream &in, GridMoves moves=GridMoves::EIGHT, Corners corners=Corners::NO_CUT)
the grid of a map in the format of the benchmarks of Sturtevant: the lines type octile,...
W chebyshev(GridCell a, GridCell b, W straight=W(1))
the distance of Chebyshev between the cells and , , the octile distance when a move to a corner cost...
@ FOUR
to the four sides of a cell
Definition GridGraph.h:153
@ EIGHT
to the sides and to the four corners
Definition GridGraph.h:154
@ LEFT
the cell left: weighed by
Definition GridGraph.h:167
@ ENTERED
the cell entered: weighed by
Definition GridGraph.h:166
@ NO_CUT
if both sides are free
Definition GridGraph.h:161
@ NO_SQUEEZE
if at least one of the two sides is free
Definition GridGraph.h:160
@ CUT
always, even between two blocked sides
Definition GridGraph.h:159
std::vector< std::vector< int > > gridDistances(int rows, int cols, const std::vector< GridCell > &sources, Free &&free, const Moves &moves=FOUR_MOVES)
the distance in moves of each cell of the grid from the nearest source, INF if none reaches it,...
std::pair< int, int > GridCell
a cell of a grid, or a move as the difference of two cells
Definition Grid.h:114
Definition ADT.h:96
the attribute A of the item of handle changed from from to to
Definition Modification.h:600