Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
xml_graph.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
43# ifndef XML_GRAPH_H
44# define XML_GRAPH_H
45
46# include <tpl_dynArray.H>
47# include <tpl_graph.H>
48# include <tpl_dynMapTree.H>
49
50# include <libxml++/libxml++.h>
51# include <libxml++/parsers/textreader.h>
52# include <libxml++/nodes/node.h>
53# include <libxml++/document.h>
54
55using namespace Aleph;
56
57namespace Aleph
58{
59 struct Attr
60 {
61 std::string name;
62 std::string value;
63 };
64
65 template <class GT>
67 {
68 void operator () (GT &, typename GT::Node *, DynArray <Attr> &)
69 {
70 // Empty
71 }
72 };
73
74 template <class GT>
76 {
77 void operator () (GT &, typename GT::Node *, DynArray <Attr> &)
78 {
79 // Empty
80 }
81 };
82
83 template <class GT>
85 {
86 void operator () (GT &, typename GT::Arc *, DynArray <Attr> &)
87 {
88 // Empty
89 }
90 };
91
92 template <class GT>
94 {
95 void operator () (GT &, typename GT::Arc *, DynArray <Attr> &)
96 {
97 // Empty
98 }
99 };
100
101 /* TODO: le debo la explicación de los detalles de cada una de las clases de
102 lectura y escritura
103 */
104
114 template <class GT,
119 >
121 {
122 std::string graph_name;
123
124 std::string node_name;
125
126 std::string arc_name;
127
129
131
133
135
136 GT read_graph(xmlpp::TextReader & reader)
137 {
138 GT g;
140
141 size_t num_nodes = 0;
142
143 while(reader.read())
144 {
145 if (reader.get_name() == node_name)
146 {
147 typename GT::Node * p = g.insert_node();
148 map.insert(num_nodes++, p);
149
150 if (not reader.has_attributes())
151 continue;
152
153 reader.move_to_first_attribute();
154
155 DynArray <Attr> attrs;
156
157 do
158 {
159 Attr & attr = attrs.append();
160 attr.name = reader.get_name();
161 attr.value = reader.get_value();
162 }
163 while (reader.move_to_next_attribute());
164
165 node_reader(g, p, attrs);
166
167 reader.move_to_element();
168 }
169
170 else if (reader.get_name() == arc_name)
171 {
172 assert(reader.has_attributes());
173 reader.move_to_first_attribute();
174 size_t src = std::atol(reader.get_value().c_str());
175 bool test = reader.move_to_next_attribute();
176 assert(test);
177 size_t tgt = std::atol(reader.get_value().c_str());
178 test = reader.move_to_next_attribute();
179
180 typename GT::Arc * a = g.insert_arc(map.find(src), map.find(tgt));
181
182 if (not test)
183 {
184 reader.move_to_element();
185 continue;
186 }
187
188 DynArray <Attr> attrs;
189
190 do
191 {
192 Attr & attr = attrs.append();
193 attr.name = reader.get_name();
194 attr.value = reader.get_value();
195 }
196 while (reader.move_to_next_attribute());
197
198 arc_reader(g, a, attrs);
199
200 reader.move_to_element();
201 }
202 }
203 return g;
204 }
205
206 GT read(const std::string & file_name)
207 {
208 xmlpp::TextReader reader(file_name);
209 return read_graph(reader);
210 }
211
212 void write_graph(GT & g, xmlpp::Document & doc)
213 {
214 xmlpp::Element * element = doc.create_root_node(graph_name);
215
216 xmlpp::Element * nodes = element->add_child("nodes");
217
219
220 size_t i = 0;
221
222 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
223 {
224 typename GT::Node * p = it.get_curr();
225
226 map.insert(p, i);
227
228 xmlpp::Element * node = nodes->add_child(node_name);
229
230 DynArray <Attr> attrs;
231
232 node_writer(g, p, attrs);
233
234 for (size_t i = 0; i < attrs.size(); ++i)
235 {
236 Attr & attr = attrs.access(i);
237 node->set_attribute(attr.name, attr.value);
238 }
239 }
240
241 xmlpp::Element * arcs = element->add_child("arcs");
242
243 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
244 {
245 typename GT::Arc * a = it.get_curr();
246
247 xmlpp::Element * arc = arcs->add_child(arc_name);
248
249 const size_t & src = map.find(g.get_src_node(a));
250 arc->set_attribute("src", std::to_string(src));
251
252 const size_t & tgt = map.find(g.get_tgt_node(a));
253 arc->set_attribute("tgt", std::to_string(tgt));
254
255 DynArray <Attr> attrs;
256
257 arc_writer(g, a, attrs);
258
259 for (size_t i = 0; i < attrs.size(); ++i)
260 {
261 Attr & attr = attrs.access(i);
262 arc->set_attribute(attr.name, attr.value);
263 }
264 }
265 }
266
267 void write(GT & g, const std::string & file_name)
268 {
269 xmlpp::Document doc;
270 write_graph(g, doc);
271 doc.write_to_file_formatted(file_name, "UTF-8");
272 }
273
274 public:
283
294
295 const std::string & get_graph_name() const
296 {
297 return graph_name;
298 }
299
300 void set_graph_name(const std::string & _graph_name)
301 {
303 }
304
305 const std::string & get_node_name() const
306 {
307 return node_name;
308 }
309
310 void set_node_name(const std::string & _node_name)
311 {
313 }
314
315 const std::string & get_arc_name() const
316 {
317 return arc_name;
318 }
319
320 void set_arc_name(const std::string & _arc_name)
321 {
323 }
324
325 GT operator () (const std::string & file_name)
326 {
327 return read(file_name);
328 }
329
330 void operator () (GT & g, const std::string & file_name)
331 {
332 write(g, file_name);
333 }
334 };
335} // End namespace Aleph
336
337# endif // XML_GRAPH_H
338
int num_nodes
Definition btreepic.C:410
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
Clase que escribe y lee un grafo (de forma muy elemental) en XML.
Definition xml_graph.H:121
void write_graph(GT &g, xmlpp::Document &doc)
Definition xml_graph.H:212
const std::string & get_arc_name() const
Definition xml_graph.H:315
const std::string & get_node_name() const
Definition xml_graph.H:305
Xml_Graph(Node_Reader &&_node_reader=Node_Reader(), Arc_Reader &&_arc_reader=Arc_Reader(), Node_Writer &&_node_writer=Node_Writer(), Arc_Writer &&_arc_writer=Arc_Writer())
Definition xml_graph.H:284
Xml_Graph(Node_Reader &_node_reader, Arc_Reader &_arc_reader, Node_Writer &_node_writer, Arc_Writer &_arc_writer)
Definition xml_graph.H:275
Arc_Reader & arc_reader
Definition xml_graph.H:130
GT read_graph(xmlpp::TextReader &reader)
Definition xml_graph.H:136
Arc_Writer & arc_writer
Definition xml_graph.H:134
const std::string & get_graph_name() const
Definition xml_graph.H:295
void set_graph_name(const std::string &_graph_name)
Definition xml_graph.H:300
void set_node_name(const std::string &_node_name)
Definition xml_graph.H:310
Node_Writer & node_writer
Definition xml_graph.H:132
std::string node_name
Definition xml_graph.H:124
std::string graph_name
Definition xml_graph.H:122
std::string arc_name
Definition xml_graph.H:126
void write(GT &g, const std::string &file_name)
Definition xml_graph.H:267
GT read(const std::string &file_name)
Definition xml_graph.H:206
GT operator()(const std::string &file_name)
Definition xml_graph.H:325
void set_arc_name(const std::string &_arc_name)
Definition xml_graph.H:320
Node_Reader & node_reader
Definition xml_graph.H:128
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:731
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:737
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
static char doc[]
Definition ntreepic.C:1831
std::string name
Definition xml_graph.H:61
std::string value
Definition xml_graph.H:62
void operator()(GT &, typename GT::Arc *, DynArray< Attr > &)
Definition xml_graph.H:86
void operator()(GT &, typename GT::Arc *, DynArray< Attr > &)
Definition xml_graph.H:95
void operator()(GT &, typename GT::Node *, DynArray< Attr > &)
Definition xml_graph.H:68
void operator()(GT &, typename GT::Node *, DynArray< Attr > &)
Definition xml_graph.H:77
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
void test(unsigned long n, gsl_rng *r)
Lazy and scalable dynamic array implementation.
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.