Aleph-w  1.9
General library for algorithms and data structures
grid.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef GRID_H
28 # define GRID_H
29 
30 namespace Aleph
31 {
32 
33 template <class GT>
34 struct Default_Operation_On_Node
35 {
36  void operator () (GT &, typename GT::Node *, const size_t &, const size_t &) { /* Empty */ }
37 };
38 
39 template <class GT>
40 struct Default_Operation_On_Arc
41 {
42  void operator () (GT &, typename GT::Arc *, const size_t &, const size_t &) { /* Empty */ }
43 };
44 
49 template <
50  class GT,
51  class Operation_On_Node = Default_Operation_On_Node<GT>,
52  class Operation_On_Arc = Default_Operation_On_Arc<GT>
53  >
55 {
56 public:
57  void operator () (GT & g, const size_t & width, const size_t & height)
58  {
59  if (g.get_num_nodes() != 0)
60  throw std::domain_error("There is nodes in graph");
61 
62  if (width < 2 or height < 2)
63  throw std::length_error("The minimun size must be 2 x 2");
64 
65  typename GT::Node *** map = new typename GT::Node **[height];
66  for (size_t i = 0; i < height; ++i)
67  {
68  try
69  {
70  map[i] = new typename GT::Node *[width];
71  for (size_t j = 0; j < width; ++j)
72  {
73  typename GT::Node * n = g.insert_node(typename GT::Node_Type());
74  Operation_On_Node()(g, n, i, j);
75  map[i][j] = n;
76  if (j > 0) //Conecta con el nodo a su izquierda
77  {
78  typename GT::Arc * a = g.insert_arc(n, map[i][j - 1]);
79  Operation_On_Arc()(g, a, i, j);
80  }
81  if (i > 0) //Conecta con el nodo que esta arriba
82  {
83  typename GT::Arc * a = g.insert_arc(n, map[i - 1][j]);
84  Operation_On_Arc()(g, a, i, j);
85  }
86  if (i > 0 and j > 0) //Conecta con el nodo que esta arriba y a la izquierda
87  {
88  typename GT::Arc * a = g.insert_arc(n, map[i - 1][j - 1]);
89  Operation_On_Arc()(g, a, i, j);
90  }
91  if (j + 1 < width and i > 0) //Conecta con el nodo que esta arriba y a la derecha
92  {
93  typename GT::Arc * a = g.insert_arc(n, map[i - 1][j + 1]);
94  Operation_On_Arc()(g, a, i, j);
95  }
96  }
97  }
98  catch (...)
99  {
100  for (size_t k = i - 1; k >= 0; --k)
101  delete [] map[k];
102  delete [] map;
103  clear_graph(g);
104  throw;
105  }
106  }
107 
108  for (size_t i = 0; i < height; ++i)
109  delete [] map[i];
110  delete [] map;
111  }
112 };
113 
114 }
115 
116 # endif
117 
Definition: grid.H:54
void clear_graph(GT &g) noexcept
Definition: tpl_graph.H:3463
Definition: ah-comb.H:35
Definition: Map.H:82

Leandro Rabindranath León