ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::GridGraph< W, C, Policy > Class Template Reference

the implicit graph of a grid map, the cells of cost \( C \) and the weights of the moves of type \( W \), whose changes of the costs are told through the subject policy Policy [see Modification.h] More...

#include <ADS/graph/GridGraph.h>

Public types

using Weight = W
 the type of the weights
using Cost = C
 the type of the costs of the cells
using CellChanged = mod::Changed< mod::Value , int , C >
 the event of a change of the cost of a cell, the cell its handle
using modifications = mod::list< CellChanged >
 the events
using Subject = typename Policy::template bind< modifications >
 the subject
using Batch = ADS::Batch< GridGraph >
 the channel of a batch, open while it lives [see batch()]

Public member functions

 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 \( \mathit{rows} \times \mathit{cols} \) free cells of cost 1, with no vector; the diagonal cost \( \sqrt{2} \) times the straight one if not given
 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 \( \mathit{rows} \times \mathit{cols} \) cells of the given costs, \( \mathit{cost}[ r \cdot \mathit{cols} + c ] \) that of the cell \( ( r , c ) \), a cell blocked if its cost is not positive, each move weighed by the cost of the cell that \( \mathit{model} \) names (with no vector, as above, the two models coincide)
template<class... O>
requires ( sizeof...( O ) > 0 )
 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... of Policy, which must outlive it
int rows (void) const
 the number of rows
int cols (void) const
 the number of columns
GridMoves moves (void) const
 the moves
Corners corners (void) const
 the rule on the corners
MoveCost moveCost (void) const
 the cell whose cost weighs a move
W straight (void) const
 the cost of a move to a side on a map of unit costs
W diagonal (void) const
 the cost of a move to a corner on a map of unit costs
bool directed (void) const
 whether the graph is oriented, i.e., two free cells cost differently
int n (void) const
 the number of nodes, the free cells
int bound (void) const
 the names of the nodes are in \( [ 0 , \mathit{rows} \cdot \mathit{cols} ) \)
bool hasNode (int u) const
 whether \( u \) is a free cell
int node (int r, int c) const
 the node of the cell \( ( r , c ) \)
GridCell cell (int u) const
 the cell \( ( r , c ) \) of the name \( u \)
C cost (int u) const
 the cost of the cell \( u \), not positive if blocked
C minCost (void) const
 the least cost of a free cell, 1 if there is no vector of the costs or no free cell
void setCost (int u, const C &c)
 the cost of the cell \( u \) set to \( c \), the cell blocked if \( c \) is not positive and free otherwise, and CellChanged told with the cost before and \( c \); nothing if the cost is already \( c \)
template<class F>
void forEachAffected (int u, const C &from, const C &to, F &&f) const
 \( f( v ) \), in increasing order of \( v \), on each cell \( v \) whose moves in may have been created, removed or weighed differently by a change of the cost of the cell \( u \) from \( \mathit{from} \) to \( \mathit{to} \), i.e., on a superset of the heads of the arcs that the change touched: none if the cell is blocked before and after, \( u \) (under ENTERED) or its free neighbors (under LEFT) if it is free before and after, \( u \) and its free neighbors of the four or the eight moves if it changes between free and blocked; at most nine calls, read on the grid after the change
std::vector< int > V (void) const
 the set of the nodes, in increasing order
std::vector< int > adj (int u) const
 the neighbors of \( u \), in increasing order
template<class F>
void forEachAdj (int u, F &&f) const
 \( f( v ) \) on each neighbor \( v \) of \( u \), in increasing order, with no allocation
template<class F>
void forEachEdge (int u, F &&f) const
 \( f( v , w ) \) on each neighbor \( v \) of \( u \), \( w \) the cost of the move, in increasing order of \( v \)
template<class F>
void forEachMove (int u, F &&f) const
 \( f( v , d ) \) on each neighbor \( v \) of \( u \), in increasing order, \( d \) whether the move is to a corner, with no allocation: the weight of the move is \( \mathit{cost}( x ) \) times diagonal() if \( d \) and straight() if not, \( x \) the cell that moveCost() names
template<class D>
requires ( Subject::dynamic )
int attach (D &o)
 attaches the observer \( o \) and gives its number
void detach (int id)
 detaches the observer number \( \mathit{id} \)
Batch batch (void)
 opens a batch: the events are collected until it is closed, and sent coalesced
Subject & subject (void)
 the subject, for ADS::batch() and ADS::quiet()
const Subject & subject (void) const
 the subject
auto nodes (void) const
 the nodes in increasing order, a view on the names that skips the blocked cells

Detailed description

template<class W = int, class C = W, class Policy = NoObservers>
class ADS::GridGraph< W, C, Policy >

the implicit graph of a grid map, the cells of cost \( C \) and the weights of the moves of type \( W \), whose changes of the costs are told through the subject policy Policy [see Modification.h]

Member typedef documentation

◆ Batch

template<class W = int, class C = W, class Policy = NoObservers>
using ADS::GridGraph< W, C, Policy >::Batch = ADS::Batch< GridGraph >

the channel of a batch, open while it lives [see batch()]

◆ CellChanged

template<class W = int, class C = W, class Policy = NoObservers>
using ADS::GridGraph< W, C, Policy >::CellChanged = mod::Changed< mod::Value , int , C >

the event of a change of the cost of a cell, the cell its handle

◆ Cost

template<class W = int, class C = W, class Policy = NoObservers>
using ADS::GridGraph< W, C, Policy >::Cost = C

the type of the costs of the cells

◆ modifications

template<class W = int, class C = W, class Policy = NoObservers>
using ADS::GridGraph< W, C, Policy >::modifications = mod::list< CellChanged >

the events

◆ Subject

template<class W = int, class C = W, class Policy = NoObservers>
using ADS::GridGraph< W, C, Policy >::Subject = typename Policy::template bind< modifications >

the subject

◆ Weight

template<class W = int, class C = W, class Policy = NoObservers>
using ADS::GridGraph< W, C, Policy >::Weight = W

the type of the weights

Constructor and destructor documentation

◆ GridGraph() [1/3]

template<class W = int, class C = W, class Policy = NoObservers>
ADS::GridGraph< W, C, Policy >::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 \( \mathit{rows} \times \mathit{cols} \) free cells of cost 1, with no vector; the diagonal cost \( \sqrt{2} \) times the straight one if not given

Precondition
\( \mathit{rows} , \mathit{cols} \ge 0 \), at most \( 2^{31} - 1 \) cells, positive costs of the moves, the diagonal one given if \( W \) is integral and the moves are eight

◆ GridGraph() [2/3]

template<class W = int, class C = W, class Policy = NoObservers>
ADS::GridGraph< W, C, Policy >::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 \( \mathit{rows} \times \mathit{cols} \) cells of the given costs, \( \mathit{cost}[ r \cdot \mathit{cols} + c ] \) that of the cell \( ( r , c ) \), a cell blocked if its cost is not positive, each move weighed by the cost of the cell that \( \mathit{model} \) names (with no vector, as above, the two models coincide)

Precondition
as above, and \( \mathit{cost} \) has \( \mathit{rows} \cdot \mathit{cols} \) places

◆ GridGraph() [3/3]

template<class W = int, class C = W, class Policy = NoObservers>
template<class... O>
requires ( sizeof...( O ) > 0 )
ADS::GridGraph< W, C, Policy >::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... of Policy, which must outlive it

Precondition
as above

Member function documentation

◆ adj()

template<class W = int, class C = W, class Policy = NoObservers>
std::vector< int > ADS::GridGraph< W, C, Policy >::adj ( int u) const
nodiscard

the neighbors of \( u \), in increasing order

Precondition
\( u \) is a node

◆ attach()

template<class W = int, class C = W, class Policy = NoObservers>
template<class D>
requires ( Subject::dynamic )
int ADS::GridGraph< W, C, Policy >::attach ( D & o)
inline

attaches the observer \( o \) and gives its number

Precondition
no batch is open

◆ batch()

template<class W = int, class C = W, class Policy = NoObservers>
Batch ADS::GridGraph< W, C, Policy >::batch ( void )
inlinenodiscard

opens a batch: the events are collected until it is closed, and sent coalesced

◆ bound()

template<class W = int, class C = W, class Policy = NoObservers>
int ADS::GridGraph< W, C, Policy >::bound ( void ) const
inlinenodiscard

the names of the nodes are in \( [ 0 , \mathit{rows} \cdot \mathit{cols} ) \)

◆ cell()

template<class W = int, class C = W, class Policy = NoObservers>
GridCell ADS::GridGraph< W, C, Policy >::cell ( int u) const
nodiscard

the cell \( ( r , c ) \) of the name \( u \)

Precondition
\( 0 \le u < \mathrm{bound}() \)

◆ cols()

template<class W = int, class C = W, class Policy = NoObservers>
int ADS::GridGraph< W, C, Policy >::cols ( void ) const
inlinenodiscard

the number of columns

◆ corners()

template<class W = int, class C = W, class Policy = NoObservers>
Corners ADS::GridGraph< W, C, Policy >::corners ( void ) const
inlinenodiscard

the rule on the corners

◆ cost()

template<class W = int, class C = W, class Policy = NoObservers>
C ADS::GridGraph< W, C, Policy >::cost ( int u) const
nodiscard

the cost of the cell \( u \), not positive if blocked

Precondition
\( 0 \le u < \mathrm{bound}() \)

◆ detach()

template<class W = int, class C = W, class Policy = NoObservers>
void ADS::GridGraph< W, C, Policy >::detach ( int id)
inline

detaches the observer number \( \mathit{id} \)

Precondition
\( \mathit{id} \) was given by attach() and not yet detached, no batch is open

◆ diagonal()

template<class W = int, class C = W, class Policy = NoObservers>
W ADS::GridGraph< W, C, Policy >::diagonal ( void ) const
inlinenodiscard

the cost of a move to a corner on a map of unit costs

◆ directed()

template<class W = int, class C = W, class Policy = NoObservers>
bool ADS::GridGraph< W, C, Policy >::directed ( void ) const
inlinenodiscard

whether the graph is oriented, i.e., two free cells cost differently

◆ forEachAdj()

template<class W = int, class C = W, class Policy = NoObservers>
template<class F>
void ADS::GridGraph< W, C, Policy >::forEachAdj ( int u,
F && f ) const

\( f( v ) \) on each neighbor \( v \) of \( u \), in increasing order, with no allocation

Precondition
\( u \) is a node

◆ forEachAffected()

template<class W = int, class C = W, class Policy = NoObservers>
template<class F>
void ADS::GridGraph< W, C, Policy >::forEachAffected ( int u,
const C & from,
const C & to,
F && f ) const

\( f( v ) \), in increasing order of \( v \), on each cell \( v \) whose moves in may have been created, removed or weighed differently by a change of the cost of the cell \( u \) from \( \mathit{from} \) to \( \mathit{to} \), i.e., on a superset of the heads of the arcs that the change touched: none if the cell is blocked before and after, \( u \) (under ENTERED) or its free neighbors (under LEFT) if it is free before and after, \( u \) and its free neighbors of the four or the eight moves if it changes between free and blocked; at most nine calls, read on the grid after the change

In pseudocode (beyond the book), \( N( u ) \) the cells at a move of \( u \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{forEachAffected}( u , a , c , f ) \cr 2 & \quad \textbf{if}\ a \le 0\ \textbf{and}\ c \le 0 \ \textbf{then}\ \textbf{return} \cr 3 & \quad \textbf{if}\ a > 0\ \textbf{and}\ c > 0\ \textbf{then} \cr 4 & \qquad \textbf{if}\ \text{the cell entered pays}\ \textbf{then} \ f( u )\ \textbf{else foreach}\ v \in \mathrm{adj}( u ) \ \textbf{do}\ f( v ) \cr 5 & \quad \textbf{else foreach}\ v \in \{ u \} \cup \{ x \in N( u ) : x \text{ free} \}\ \text{in increasing order} \ \textbf{do}\ f( v ) \end{array}} \]

Precondition
\( 0 \le u < \mathrm{bound}() \)

◆ forEachEdge()

template<class W = int, class C = W, class Policy = NoObservers>
template<class F>
void ADS::GridGraph< W, C, Policy >::forEachEdge ( int u,
F && f ) const

\( f( v , w ) \) on each neighbor \( v \) of \( u \), \( w \) the cost of the move, in increasing order of \( v \)

Precondition
\( u \) is a node

◆ forEachMove()

template<class W = int, class C = W, class Policy = NoObservers>
template<class F>
void ADS::GridGraph< W, C, Policy >::forEachMove ( int u,
F && f ) const

\( f( v , d ) \) on each neighbor \( v \) of \( u \), in increasing order, \( d \) whether the move is to a corner, with no allocation: the weight of the move is \( \mathit{cost}( x ) \) times diagonal() if \( d \) and straight() if not, \( x \) the cell that moveCost() names

Precondition
\( u \) is a node

◆ hasNode()

template<class W = int, class C = W, class Policy = NoObservers>
bool ADS::GridGraph< W, C, Policy >::hasNode ( int u) const
inlinenodiscard

whether \( u \) is a free cell

◆ minCost()

template<class W = int, class C = W, class Policy = NoObservers>
C ADS::GridGraph< W, C, Policy >::minCost ( void ) const
inlinenodiscard

the least cost of a free cell, 1 if there is no vector of the costs or no free cell

◆ moveCost()

template<class W = int, class C = W, class Policy = NoObservers>
MoveCost ADS::GridGraph< W, C, Policy >::moveCost ( void ) const
inlinenodiscard

the cell whose cost weighs a move

◆ moves()

template<class W = int, class C = W, class Policy = NoObservers>
GridMoves ADS::GridGraph< W, C, Policy >::moves ( void ) const
inlinenodiscard

the moves

◆ n()

template<class W = int, class C = W, class Policy = NoObservers>
int ADS::GridGraph< W, C, Policy >::n ( void ) const
inlinenodiscard

the number of nodes, the free cells

◆ node()

template<class W = int, class C = W, class Policy = NoObservers>
int ADS::GridGraph< W, C, Policy >::node ( int r,
int c ) const
nodiscard

the node of the cell \( ( r , c ) \)

Precondition
\( ( r , c ) \) is a cell of the grid

◆ nodes()

template<class W = int, class C = W, class Policy = NoObservers>
auto ADS::GridGraph< W, C, Policy >::nodes ( void ) const
inlinenodiscard

the nodes in increasing order, a view on the names that skips the blocked cells

◆ rows()

template<class W = int, class C = W, class Policy = NoObservers>
int ADS::GridGraph< W, C, Policy >::rows ( void ) const
inlinenodiscard

the number of rows

◆ setCost()

template<class W = int, class C = W, class Policy = NoObservers>
void ADS::GridGraph< W, C, Policy >::setCost ( int u,
const C & c )

the cost of the cell \( u \) set to \( c \), the cell blocked if \( c \) is not positive and free otherwise, and CellChanged told with the cost before and \( c \); nothing if the cost is already \( c \)

In pseudocode (beyond the book), \( \gamma \) the vector of the costs, \( n \) the free cells, \( \gamma_{\mathrm{min}} \) their least cost and \( m \) the free cells of that cost:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{setCost}( u , c ) \cr 2 & \quad a \gets \gamma[ u ];\ \textbf{if}\ a = c\ \textbf{then} \ \textbf{return} \cr 3 & \quad \gamma[ u ] \gets c \cr 4 & \quad \textbf{if}\ a > 0\ \textbf{then}\ n \gets n - 1; \ \textbf{if}\ a = \gamma_{\mathrm{min}}\ \textbf{then}\ m \gets m - 1 \cr 5 & \quad \textbf{if}\ c > 0\ \textbf{then}\ n \gets n + 1; \ \textbf{if}\ n = 1\ \textbf{or}\ c < \gamma_{\mathrm{min}} \ \textbf{then}\ ( \gamma_{\mathrm{min}} , m ) \gets ( c , 1 ) \cr 6 & \qquad \textbf{else if}\ c = \gamma_{\mathrm{min}}\ \textbf{then} \ m \gets m + 1 \cr 7 & \quad \textbf{if}\ n > 0\ \textbf{and}\ m = 0\ \textbf{then} \ \text{count again } \gamma_{\mathrm{min}} \text{ and } m \text{ on the free cells} \cr 8 & \quad \mathit{directed} \gets n > 0\ \textbf{and}\ m \ne n \cr 9 & \quad \textbf{if}\ \text{someone wants the event}\ \textbf{then} \ \text{tell } \mathrm{CellChanged}( u , a , c ) \end{array}} \]

The test of line 9 is false at compile time with NoObservers, and the change is then the code of the grid alone.

Precondition
\( 0 \le u < \mathrm{bound}() \)

◆ straight()

template<class W = int, class C = W, class Policy = NoObservers>
W ADS::GridGraph< W, C, Policy >::straight ( void ) const
inlinenodiscard

the cost of a move to a side on a map of unit costs

◆ subject() [1/2]

template<class W = int, class C = W, class Policy = NoObservers>
Subject & ADS::GridGraph< W, C, Policy >::subject ( void )
inlinenodiscard

the subject, for ADS::batch() and ADS::quiet()

◆ subject() [2/2]

template<class W = int, class C = W, class Policy = NoObservers>
const Subject & ADS::GridGraph< W, C, Policy >::subject ( void ) const
inlinenodiscard

the subject

◆ V()

template<class W = int, class C = W, class Policy = NoObservers>
std::vector< int > ADS::GridGraph< W, C, Policy >::V ( void ) const
nodiscard

the set of the nodes, in increasing order


The documentation for this class was generated from the following file: