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
70#ifndef GRID_H
71#define GRID_H
72
73# include <ah-graph-concepts.H>
74
75#include <ah-errors.H>
76#include <aleph-graph.H>
77#include <tpl_array.H>
78
79namespace Aleph {
80
86template <class GT>
88{
99 void operator () (GT &g, typename GT::Node *n, const size_t &row, const size_t &col)
100 {
101 (void) g;
102 (void) n;
103 (void) row;
104 (void) col;
105 }
106};
107
113template <class GT>
115{
126 void operator () (GT &g, typename GT::Arc *a, const size_t &row, const size_t &col)
127 {
128 (void) g;
129 (void) a;
130 (void) row;
131 (void) col;
132 }
133};
134
175template <AlephGraph GT, class Operation_On_Node = Default_Operation_On_Node<GT>,
176 class Operation_On_Arc = Default_Operation_On_Arc<GT>>
178{
179public:
194 void operator () (GT &g, const size_t &width, const size_t &height)
195 {
196 ah_domain_error_if(g.get_num_nodes() != 0) << "Build_Grid requires an empty graph";
197
198 ah_length_error_if(width < 2 or height < 2) << "The minimum size must be 2 x 2";
199
200 // Flat, single-allocation lookup table (row-major: map[i * width + j])
201 // instead of a vector-of-vectors, avoiding height+1 separate
202 // allocations and keeping the whole table contiguous.
203 Array<typename GT::Node*> map(width * height, nullptr);
204
205 try
206 {
207 for (size_t i = 0; i < height; ++i)
208 {
209 for (size_t j = 0; j < width; ++j)
210 {
211 typename GT::Node *n = g.insert_node(typename GT::Node_Type());
212 Operation_On_Node()(g, n, i, j);
213 map[i * width + j] = n;
214 if (j > 0) // Connect to left neighbor
215 {
216 typename GT::Arc *a = g.insert_arc(n, map[i * width + (j - 1)]);
217 Operation_On_Arc()(g, a, i, j);
218 }
219 if (i > 0) // Connect to upper neighbor
220 {
221 typename GT::Arc *a = g.insert_arc(n, map[(i - 1) * width + j]);
222 Operation_On_Arc()(g, a, i, j);
223 }
224 if (i > 0 and j > 0) // Connect to upper-left diagonal
225 {
226 typename GT::Arc *a = g.insert_arc(n, map[(i - 1) * width + (j - 1)]);
227 Operation_On_Arc()(g, a, i, j);
228 }
229 if (j + 1 < width and i > 0) // Connect to upper-right diagonal
230 {
231 typename GT::Arc *a = g.insert_arc(n, map[(i - 1) * width + (j + 1)]);
232 Operation_On_Arc()(g, a, i, j);
233 }
234 }
235 }
236 }
237 catch (...)
238 {
239 clear_graph(g);
240 throw;
241 }
242 }
243};
244
245} // namespace Aleph
246
247#endif
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
Definition ah-errors.H:703
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:527
C++20 concepts for the protocol shared by graph algorithms.
Simplified graph interface for common use cases.
size_t row
Definition ca-c-api.h:115
size_t size_t col
Definition ca-c-api.h:116
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
Functor to build a 2D grid graph with 8-connectivity.
Definition grid.H:178
void operator()(GT &g, const size_t &width, const size_t &height)
Build a grid graph.
Definition grid.H:194
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:525
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:437
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:605
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
void clear_graph(GT &g) noexcept
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:3659
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
Default no-op operation for arc initialization.
Definition grid.H:115
void operator()(GT &g, typename GT::Arc *a, const size_t &row, const size_t &col)
Called for each created arc.
Definition grid.H:126
Default no-op operation for node initialization.
Definition grid.H:88
void operator()(GT &g, typename GT::Node *n, const size_t &row, const size_t &col)
Called for each created node.
Definition grid.H:99
Dynamic array container with automatic resizing.