Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
grid.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
71# ifndef GRID_H
72# define GRID_H
73
74namespace Aleph
75{
76
82template <class GT>
84{
95 void operator () (GT & g, typename GT::Node * n, const size_t & row, const size_t & col) { (void)g; (void)n; (void)row; (void)col; }
96};
97
103template <class GT>
105{
116 void operator () (GT & g, typename GT::Arc * a, const size_t & row, const size_t & col) { (void)g; (void)a; (void)row; (void)col; }
117};
118
159template <
160 class GT,
163 >
165{
166public:
167
182 void operator () (GT & g, const size_t & width, const size_t & height)
183 {
185 << "There is nodes in graph";
186
187 ah_length_error_if(width < 2 or height < 2)
188 << "The minimun size must be 2 x 2";
189
190 typename GT::Node *** map = new typename GT::Node **[height];
191 for (size_t i = 0; i < height; ++i)
192 {
193 try
194 {
195 map[i] = new typename GT::Node *[width];
196 for (size_t j = 0; j < width; ++j)
197 {
198 typename GT::Node * n = g.insert_node(typename GT::Node_Type());
199 Operation_On_Node()(g, n, i, j);
200 map[i][j] = n;
201 if (j > 0) // Connect to left neighbor
202 {
203 typename GT::Arc * a = g.insert_arc(n, map[i][j - 1]);
204 Operation_On_Arc()(g, a, i, j);
205 }
206 if (i > 0) // Connect to upper neighbor
207 {
208 typename GT::Arc * a = g.insert_arc(n, map[i - 1][j]);
209 Operation_On_Arc()(g, a, i, j);
210 }
211 if (i > 0 and j > 0) // Connect to upper-left diagonal
212 {
213 typename GT::Arc * a = g.insert_arc(n, map[i - 1][j - 1]);
214 Operation_On_Arc()(g, a, i, j);
215 }
216 if (j + 1 < width and i > 0) // Connect to upper-right diagonal
217 {
218 typename GT::Arc * a = g.insert_arc(n, map[i - 1][j + 1]);
219 Operation_On_Arc()(g, a, i, j);
220 }
221 }
222 }
223 catch (...)
224 {
225 for (size_t k = i - 1; k >= 0; --k)
226 delete [] map[k];
227 delete [] map;
228 clear_graph(g);
229 throw;
230 }
231 }
232
233 for (size_t i = 0; i < height; ++i)
234 delete [] map[i];
235 delete [] map;
236 }
237};
238
239}
240
241# endif
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
Definition ah-errors.H:698
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
Functor to build a 2D grid graph with 8-connectivity.
Definition grid.H:165
void operator()(GT &g, const size_t &width, const size_t &height)
Build a grid graph.
Definition grid.H:182
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:436
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
void clear_graph(GT &g) noexcept
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:3549
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Default no-op operation for arc initialization.
Definition grid.H:105
void operator()(GT &g, typename GT::Arc *a, const size_t &row, const size_t &col)
Called for each created arc.
Definition grid.H:116
Default no-op operation for node initialization.
Definition grid.H:84
void operator()(GT &g, typename GT::Node *n, const size_t &row, const size_t &col)
Called for each created node.
Definition grid.H:95
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222