Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_search_deway.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
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
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# include <fstream>
28# include <iostream>
29# include <memory>
30# include <string>
31# include <ah-errors.H>
32# include <tpl_graph.H>
33# include <graph_to_tree.H>
34# include <generate_tree.H>
35
36using namespace std;
37
38using namespace Aleph;
39
40# define INDENT " "
41
42struct Ciudad
43{
45
46 string nombre;
47
49
50 Ciudad() : tipo(DESCONOCIDO) { /* empty */ }
51
52 Ciudad(const Ciudad & c) : nombre(c.nombre), tipo(c.tipo) { /* empty */ }
53
54 Ciudad(const char * nom) : nombre(nom), tipo(DESCONOCIDO) { /* empty */ }
55
56 Ciudad(char * nom) : nombre(nom), tipo(DESCONOCIDO) { /* empty */ }
57
58 Ciudad(const string & str) : nombre(str), tipo(DESCONOCIDO) { /* empty */ }
59
60 bool operator == (const Ciudad & c) const
61 {
62 return nombre == c.nombre;
63 }
64};
65
66struct Via
67{
70
71 string nombre;
72 int distancia;
74
75 Via() : nombre("Desconocido"), distancia(0), tipo(DESCONOCIDO) {}
76
77 Via(int d)
78 : nombre("Desconocido"), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
79
80 Via(char * nom, int d)
81 : nombre(nom), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
82
83 Via(const string& nom, int d)
84 : nombre(nom), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
85
86 typedef int Distance_Type;
87
89
90 static constexpr Distance_Type Zero_Distance = 0;
91};
92
94
96
97
99
101
102
103
104struct Ciudad_Igual
105{
106 bool operator () (const Ciudad & c1, const Ciudad & c2) const
107 {
108 return c1.nombre == c2.nombre;
109 }
110};
111
112
113Mapa::Node * buscar_ciudad(Mapa& mapa, const string & nombre)
114{
115 return mapa.search_node([&nombre] (Mapa::Node * p)
116 {
117 return p->get_info().nombre == nombre;
118 });
119}
120
122 const string & c1, const string & c2,
123 int distancia)
124{
126
127 if (n1 == nullptr)
128 n1 = mapa.insert_node(c1);
129
131
132 if (n2 == nullptr)
133 n2 = mapa.insert_node(c2);
134
135 string nombre_arco = n1->get_info().nombre + "--" + n2->get_info().nombre;
136
137 mapa.insert_arc(n1, n2, Via(nombre_arco, distancia));
138}
139
140
142{
143 cout << endl
144 << "Path: ";
145 for (Path<Mapa>::Iterator itor(path); itor.has_curr(); itor.next())
146 cout << itor.get_current_node()->get_info().nombre << "-";
147
148 cout << endl;
149}
150
151
153{
154 cout << endl
155 << "Node list (" << g.get_num_nodes() << ")" << endl;
156
157 for (Mapa::Node_Iterator node_itor(g); node_itor.has_curr();
158 node_itor.next())
159 cout << INDENT << node_itor.get_current_node()->get_info().nombre << endl;
160
161 cout << endl
162 << endl
163 << "Arc list (" << g.get_num_arcs() << ")"
164 << endl;
165
167 arc_itor.next())
168 {
169 Mapa::Arc * arc = arc_itor.get_current_arc();
170 cout << arc->get_info().nombre << " " << arc->get_info().distancia
171 << " from " << g.get_src_node(arc)->get_info().nombre
172 << " to " << g.get_tgt_node(arc)->get_info().nombre << endl;
173 }
174
175 cout << endl
176 << endl
177 << "Graph listing by nodes and for each node by arcs"
178 << endl;
179 for (Mapa::Node_Iterator node_itor(g); node_itor.has_curr();
180 node_itor.next())
181 {
182 Mapa::Node * src_node = node_itor.get_current_node();
183 cout << src_node->get_info().nombre << endl;
184 for (Mapa::Node_Arc_Iterator itor(node_itor.get_current_node());
185 itor.has_curr(); itor.next())
186 {
187 Mapa::Arc * arc = itor.get_current_arc();
188 cout << INDENT << arc->get_info().distancia << " "
189 << g.get_connected_node(arc, src_node)->get_info().nombre
190 << endl;
191 }
192 }
193 cout << endl;
194}
195
197{
198 insert_via(g, "San Cristobal", "La Fria", 69);
199 insert_via(g, "San Cristobal", "Sacramento", 113);
200 insert_via(g, "San Cristobal", "San Antonio", 36);
201 insert_via(g, "Rubio", "Caparo", 150);
202 insert_via(g, "La Fria", "El Vigia", 86);
203 insert_via(g, "El Vigia", "Santa Barbara", 59);
204 insert_via(g, "El Vigia", "Merida", 79);
205 insert_via(g, "La Fria", "Machiques", 252);
206 insert_via(g, "Valera", "Merida", 167);
207 insert_via(g, "Valera", "Carora", 120);
208 insert_via(g, "Carora", "Barquisimeto", 102);
209 insert_via(g, "Merida", "Barinas", 180);
210 insert_via(g, "Barinas", "Guanare", 94);
211}
212
213
214 template <typename GT>
215struct GT_Tree
216{
218 {
219 t->get_key() = g->get_info().nombre;
220 }
221};
222
223
224struct Write_Ciudad
225{
227 {
228 return p->get_key();
229 }
230};
231
232void escribir_deway(int deway[], const size_t & n)
233{
234 for (size_t i = 0; i < n; ++i)
235 {
236 cout << deway[i];
237
238 if (i < n - 1)
239 cout << ".";
240 }
241}
242
243int main()
244{
245 Mapa tree;
246
247 construir_mapa(tree);
248
249 imprimir_mapa(tree);
250
251 Mapa::Node * c = buscar_ciudad(tree, "Merida");
252 ah_runtime_error_if(c == nullptr)
253 << "buscar_ciudad(tree, \"Merida\") returned nullptr";
254
255 std::unique_ptr<Tree_Node<string>> t(
256 Aleph::Graph_To_Tree_Node <Mapa, string, GT_Tree<Mapa> > () (tree, c));
257
258 while (true)
259 {
260 cout << "Enter key to search (type \"quit\"): ";
261 string clave;
262 if (not std::getline(std::cin, clave))
263 break;
264
265 // Remove trailing CR if present (e.g. on Windows)
266 if (not clave.empty() and clave.back() == '\r')
267 clave.pop_back();
268
269 if (clave.empty() or clave == "quit")
270 break;
271
272 const size_t Buf_Size = 512;
273 int deway[Buf_Size];
274 size_t dw_size = 0;
275
276 Tree_Node<string> * p =
277 search_deway <Tree_Node<string> >(t.get(), clave, deway, Buf_Size,
278 dw_size);
279
280 if (p == nullptr)
281 cout << clave << " was not found in the tree" << endl;
282 else
283 {
284 cout << clave << " has Deway number: ";
286 cout << endl;
287 }
288 }
289
290 cout << "Exiting ... " << endl;
291
292 ofstream test("prueba.Tree", ios::trunc);
294 << "could not open prueba.Tree for writing";
295
297}
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:266
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3854
Functor class to convert a tree graph to Tree_Node structure.
Iterator on the arcs of a graph.
Definition tpl_graph.H:830
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:428
_Graph_Node Node
The graph type.
Definition tpl_graph.H:432
_Graph_Arc Arc
The node class type.
Definition tpl_graph.H:433
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:3208
Path on a graph.
Definition tpl_graph.H:2669
Generic m-ary trees.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:737
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:778
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:784
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:743
void deway(Tree_Node< int > *p, int prefix[], const int &len, const size_t &dim)
Recursively compute and print Deway numbering for a tree node.
Definition deway.C:121
Tree visualization and output generation.
Convert spanning tree graphs to Tree_Node structures.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Node belonging to a graph implemented with a double linked adjacency list.
Definition tpl_graph.H:121
Iterator on all arcs of a graph.
Definition tpl_graph.H:917
bool operator()(const Ciudad &c1, const Ciudad &c2)
Ciudad(const char *nom)
Tipo_Ciudad tipo
string nombre
Ciudad(const Ciudad &c)
bool operator==(const Ciudad &c) const
Ciudad(const string &str)
Ciudad(char *nom)
void operator()(typename GT::Node *g, Tree_Node< string > *t)
string nombre
static const Distance_Type Zero_Distance
Via(const string &nom, int d)
int Distance_Type
Tipo_Via tipo
@ CARRETERA1
@ CHALANA
@ CARRETERA3
@ AUTOPISTA
@ CARRETERA2
@ DESCONOCIDO
@ GRANZON
Distance_Type & get_distance()
int distancia
Via(int d)
Via(char *nom, int d)
const string operator()(Tree_Node< string > *p)
void test()
Definition test-comb.C:35
void imprimir_camino(Path< Mapa > &path)
void imprimir_mapa(Mapa &g)
List_Graph< Nodo_Ciudad, Arco_Via > Mapa
List_Digraph< Nodo_Ciudad, Arco_Via > Dimapa
Graph_Node< Ciudad > Nodo_Ciudad
#define INDENT
Mapa::Node * buscar_ciudad(Mapa &mapa, const string &nombre)
Graph_Arc< Via > Arco_Via
void insert_via(Mapa &mapa, const string &c1, const string &c2, int distancia)
void construir_mapa(Mapa &g)
void escribir_deway(int deway[], const size_t &n)
int main()
Generic graph and digraph implementations.