deba@127
|
1 |
/* -*- C++ -*-
|
deba@127
|
2 |
*
|
deba@127
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
deba@127
|
4 |
*
|
deba@127
|
5 |
* Copyright (C) 2003-2008
|
deba@127
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@127
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@127
|
8 |
*
|
deba@127
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@127
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@127
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@127
|
12 |
*
|
deba@127
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@127
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@127
|
15 |
* purpose.
|
deba@127
|
16 |
*
|
deba@127
|
17 |
*/
|
deba@127
|
18 |
|
deba@127
|
19 |
///\ingroup lemon_io
|
deba@127
|
20 |
///\file
|
kpeter@192
|
21 |
///\brief \ref lgf-format "Lemon Graph Format" writer.
|
deba@127
|
22 |
|
deba@127
|
23 |
|
deba@127
|
24 |
#ifndef LEMON_LGF_WRITER_H
|
deba@127
|
25 |
#define LEMON_LGF_WRITER_H
|
deba@127
|
26 |
|
deba@127
|
27 |
#include <iostream>
|
deba@127
|
28 |
#include <fstream>
|
deba@127
|
29 |
#include <sstream>
|
deba@127
|
30 |
|
deba@127
|
31 |
#include <algorithm>
|
deba@127
|
32 |
|
deba@127
|
33 |
#include <vector>
|
deba@127
|
34 |
#include <functional>
|
deba@127
|
35 |
|
deba@127
|
36 |
#include <lemon/assert.h>
|
deba@127
|
37 |
#include <lemon/graph_utils.h>
|
deba@127
|
38 |
|
deba@127
|
39 |
namespace lemon {
|
deba@127
|
40 |
|
deba@127
|
41 |
namespace _writer_bits {
|
deba@127
|
42 |
|
deba@127
|
43 |
template <typename Value>
|
deba@127
|
44 |
struct DefaultConverter {
|
deba@127
|
45 |
std::string operator()(const Value& value) {
|
deba@127
|
46 |
std::ostringstream os;
|
deba@127
|
47 |
os << value;
|
deba@127
|
48 |
return os.str();
|
deba@127
|
49 |
}
|
deba@127
|
50 |
};
|
deba@127
|
51 |
|
deba@127
|
52 |
template <typename T>
|
deba@127
|
53 |
bool operator<(const T&, const T&) {
|
deba@127
|
54 |
throw DataFormatError("Label map is not comparable");
|
deba@127
|
55 |
}
|
deba@127
|
56 |
|
deba@127
|
57 |
template <typename _Map>
|
deba@127
|
58 |
class MapLess {
|
deba@127
|
59 |
public:
|
deba@127
|
60 |
typedef _Map Map;
|
deba@127
|
61 |
typedef typename Map::Key Item;
|
deba@127
|
62 |
|
deba@127
|
63 |
private:
|
deba@127
|
64 |
const Map& _map;
|
deba@127
|
65 |
|
deba@127
|
66 |
public:
|
deba@127
|
67 |
MapLess(const Map& map) : _map(map) {}
|
deba@127
|
68 |
|
deba@127
|
69 |
bool operator()(const Item& left, const Item& right) {
|
deba@127
|
70 |
return _map[left] < _map[right];
|
deba@127
|
71 |
}
|
deba@127
|
72 |
};
|
deba@127
|
73 |
|
deba@165
|
74 |
template <typename _Graph, bool _dir, typename _Map>
|
deba@165
|
75 |
class GraphArcMapLess {
|
deba@165
|
76 |
public:
|
deba@165
|
77 |
typedef _Map Map;
|
deba@165
|
78 |
typedef _Graph Graph;
|
deba@165
|
79 |
typedef typename Graph::Edge Item;
|
deba@165
|
80 |
|
deba@165
|
81 |
private:
|
deba@165
|
82 |
const Graph& _graph;
|
deba@165
|
83 |
const Map& _map;
|
deba@165
|
84 |
|
deba@165
|
85 |
public:
|
deba@165
|
86 |
GraphArcMapLess(const Graph& graph, const Map& map)
|
deba@165
|
87 |
: _graph(graph), _map(map) {}
|
deba@165
|
88 |
|
deba@165
|
89 |
bool operator()(const Item& left, const Item& right) {
|
deba@165
|
90 |
return _map[_graph.direct(left, _dir)] <
|
deba@165
|
91 |
_map[_graph.direct(right, _dir)];
|
deba@165
|
92 |
}
|
deba@165
|
93 |
};
|
deba@165
|
94 |
|
deba@127
|
95 |
template <typename _Item>
|
deba@127
|
96 |
class MapStorageBase {
|
deba@127
|
97 |
public:
|
deba@127
|
98 |
typedef _Item Item;
|
deba@127
|
99 |
|
deba@127
|
100 |
public:
|
deba@127
|
101 |
MapStorageBase() {}
|
deba@127
|
102 |
virtual ~MapStorageBase() {}
|
deba@127
|
103 |
|
deba@127
|
104 |
virtual std::string get(const Item& item) = 0;
|
deba@127
|
105 |
virtual void sort(std::vector<Item>&) = 0;
|
deba@127
|
106 |
};
|
deba@127
|
107 |
|
deba@127
|
108 |
template <typename _Item, typename _Map,
|
deba@127
|
109 |
typename _Converter = DefaultConverter<typename _Map::Value> >
|
deba@127
|
110 |
class MapStorage : public MapStorageBase<_Item> {
|
deba@127
|
111 |
public:
|
deba@127
|
112 |
typedef _Map Map;
|
deba@127
|
113 |
typedef _Converter Converter;
|
deba@127
|
114 |
typedef _Item Item;
|
deba@127
|
115 |
|
deba@127
|
116 |
private:
|
deba@127
|
117 |
const Map& _map;
|
deba@127
|
118 |
Converter _converter;
|
deba@127
|
119 |
|
deba@127
|
120 |
public:
|
deba@127
|
121 |
MapStorage(const Map& map, const Converter& converter = Converter())
|
deba@127
|
122 |
: _map(map), _converter(converter) {}
|
deba@127
|
123 |
virtual ~MapStorage() {}
|
deba@127
|
124 |
|
deba@127
|
125 |
virtual std::string get(const Item& item) {
|
deba@127
|
126 |
return _converter(_map[item]);
|
deba@127
|
127 |
}
|
deba@127
|
128 |
virtual void sort(std::vector<Item>& items) {
|
deba@127
|
129 |
MapLess<Map> less(_map);
|
deba@127
|
130 |
std::sort(items.begin(), items.end(), less);
|
deba@127
|
131 |
}
|
deba@127
|
132 |
};
|
deba@127
|
133 |
|
deba@165
|
134 |
template <typename _Graph, bool _dir, typename _Map,
|
deba@165
|
135 |
typename _Converter = DefaultConverter<typename _Map::Value> >
|
deba@165
|
136 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
|
deba@165
|
137 |
public:
|
deba@165
|
138 |
typedef _Map Map;
|
deba@165
|
139 |
typedef _Converter Converter;
|
deba@165
|
140 |
typedef _Graph Graph;
|
deba@165
|
141 |
typedef typename Graph::Edge Item;
|
deba@165
|
142 |
static const bool dir = _dir;
|
deba@165
|
143 |
|
deba@165
|
144 |
private:
|
deba@165
|
145 |
const Graph& _graph;
|
deba@165
|
146 |
const Map& _map;
|
deba@165
|
147 |
Converter _converter;
|
deba@165
|
148 |
|
deba@165
|
149 |
public:
|
deba@165
|
150 |
GraphArcMapStorage(const Graph& graph, const Map& map,
|
deba@165
|
151 |
const Converter& converter = Converter())
|
deba@165
|
152 |
: _graph(graph), _map(map), _converter(converter) {}
|
deba@165
|
153 |
virtual ~GraphArcMapStorage() {}
|
deba@165
|
154 |
|
deba@165
|
155 |
virtual std::string get(const Item& item) {
|
deba@165
|
156 |
return _converter(_map[_graph.direct(item, dir)]);
|
deba@165
|
157 |
}
|
deba@165
|
158 |
virtual void sort(std::vector<Item>& items) {
|
deba@165
|
159 |
GraphArcMapLess<Graph, dir, Map> less(_graph, _map);
|
deba@165
|
160 |
std::sort(items.begin(), items.end(), less);
|
deba@165
|
161 |
}
|
deba@165
|
162 |
};
|
deba@165
|
163 |
|
deba@127
|
164 |
class ValueStorageBase {
|
deba@127
|
165 |
public:
|
deba@127
|
166 |
ValueStorageBase() {}
|
deba@127
|
167 |
virtual ~ValueStorageBase() {}
|
deba@127
|
168 |
|
deba@127
|
169 |
virtual std::string get() = 0;
|
deba@127
|
170 |
};
|
deba@127
|
171 |
|
deba@127
|
172 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> >
|
deba@127
|
173 |
class ValueStorage : public ValueStorageBase {
|
deba@127
|
174 |
public:
|
deba@127
|
175 |
typedef _Value Value;
|
deba@127
|
176 |
typedef _Converter Converter;
|
deba@127
|
177 |
|
deba@127
|
178 |
private:
|
deba@127
|
179 |
const Value& _value;
|
deba@127
|
180 |
Converter _converter;
|
deba@127
|
181 |
|
deba@127
|
182 |
public:
|
deba@127
|
183 |
ValueStorage(const Value& value, const Converter& converter = Converter())
|
deba@127
|
184 |
: _value(value), _converter(converter) {}
|
deba@127
|
185 |
|
deba@127
|
186 |
virtual std::string get() {
|
deba@127
|
187 |
return _converter(_value);
|
deba@127
|
188 |
}
|
deba@127
|
189 |
};
|
deba@127
|
190 |
|
deba@127
|
191 |
template <typename Value>
|
deba@127
|
192 |
struct MapLookUpConverter {
|
deba@127
|
193 |
const std::map<Value, std::string>& _map;
|
deba@127
|
194 |
|
deba@127
|
195 |
MapLookUpConverter(const std::map<Value, std::string>& map)
|
deba@127
|
196 |
: _map(map) {}
|
deba@127
|
197 |
|
deba@127
|
198 |
std::string operator()(const Value& str) {
|
deba@127
|
199 |
typename std::map<Value, std::string>::const_iterator it =
|
deba@127
|
200 |
_map.find(str);
|
deba@127
|
201 |
if (it == _map.end()) {
|
deba@127
|
202 |
throw DataFormatError("Item not found");
|
deba@127
|
203 |
}
|
deba@127
|
204 |
return it->second;
|
deba@127
|
205 |
}
|
deba@127
|
206 |
};
|
deba@127
|
207 |
|
deba@165
|
208 |
template <typename Graph>
|
deba@165
|
209 |
struct GraphArcLookUpConverter {
|
deba@165
|
210 |
const Graph& _graph;
|
deba@165
|
211 |
const std::map<typename Graph::Edge, std::string>& _map;
|
deba@165
|
212 |
|
deba@165
|
213 |
GraphArcLookUpConverter(const Graph& graph,
|
deba@165
|
214 |
const std::map<typename Graph::Edge,
|
deba@165
|
215 |
std::string>& map)
|
deba@165
|
216 |
: _graph(graph), _map(map) {}
|
deba@165
|
217 |
|
deba@165
|
218 |
std::string operator()(const typename Graph::Arc& val) {
|
deba@165
|
219 |
typename std::map<typename Graph::Edge, std::string>
|
deba@165
|
220 |
::const_iterator it = _map.find(val);
|
deba@165
|
221 |
if (it == _map.end()) {
|
deba@165
|
222 |
throw DataFormatError("Item not found");
|
deba@165
|
223 |
}
|
deba@165
|
224 |
return (_graph.direction(val) ? '+' : '-') + it->second;
|
deba@165
|
225 |
}
|
deba@165
|
226 |
};
|
deba@165
|
227 |
|
deba@127
|
228 |
bool isWhiteSpace(char c) {
|
deba@127
|
229 |
return c == ' ' || c == '\t' || c == '\v' ||
|
deba@127
|
230 |
c == '\n' || c == '\r' || c == '\f';
|
deba@127
|
231 |
}
|
deba@127
|
232 |
|
deba@127
|
233 |
bool isEscaped(char c) {
|
deba@127
|
234 |
return c == '\\' || c == '\"' || c == '\'' ||
|
deba@127
|
235 |
c == '\a' || c == '\b';
|
deba@127
|
236 |
}
|
deba@127
|
237 |
|
deba@127
|
238 |
static void writeEscape(std::ostream& os, char c) {
|
deba@127
|
239 |
switch (c) {
|
deba@127
|
240 |
case '\\':
|
deba@127
|
241 |
os << "\\\\";
|
deba@127
|
242 |
return;
|
deba@127
|
243 |
case '\"':
|
deba@127
|
244 |
os << "\\\"";
|
deba@127
|
245 |
return;
|
deba@127
|
246 |
case '\a':
|
deba@127
|
247 |
os << "\\a";
|
deba@127
|
248 |
return;
|
deba@127
|
249 |
case '\b':
|
deba@127
|
250 |
os << "\\b";
|
deba@127
|
251 |
return;
|
deba@127
|
252 |
case '\f':
|
deba@127
|
253 |
os << "\\f";
|
deba@127
|
254 |
return;
|
deba@127
|
255 |
case '\r':
|
deba@127
|
256 |
os << "\\r";
|
deba@127
|
257 |
return;
|
deba@127
|
258 |
case '\n':
|
deba@127
|
259 |
os << "\\n";
|
deba@127
|
260 |
return;
|
deba@127
|
261 |
case '\t':
|
deba@127
|
262 |
os << "\\t";
|
deba@127
|
263 |
return;
|
deba@127
|
264 |
case '\v':
|
deba@127
|
265 |
os << "\\v";
|
deba@127
|
266 |
return;
|
deba@127
|
267 |
default:
|
deba@127
|
268 |
if (c < 0x20) {
|
deba@163
|
269 |
std::ios::fmtflags flags = os.flags();
|
deba@127
|
270 |
os << '\\' << std::oct << static_cast<int>(c);
|
deba@163
|
271 |
os.flags(flags);
|
deba@127
|
272 |
} else {
|
deba@127
|
273 |
os << c;
|
deba@127
|
274 |
}
|
deba@127
|
275 |
return;
|
deba@127
|
276 |
}
|
deba@127
|
277 |
}
|
deba@127
|
278 |
|
deba@127
|
279 |
bool requireEscape(const std::string& str) {
|
alpar@156
|
280 |
if (str.empty() || str[0] == '@') return true;
|
deba@127
|
281 |
std::istringstream is(str);
|
deba@127
|
282 |
char c;
|
deba@127
|
283 |
while (is.get(c)) {
|
deba@127
|
284 |
if (isWhiteSpace(c) || isEscaped(c)) {
|
deba@127
|
285 |
return true;
|
deba@127
|
286 |
}
|
deba@127
|
287 |
}
|
deba@127
|
288 |
return false;
|
deba@127
|
289 |
}
|
deba@127
|
290 |
|
deba@127
|
291 |
std::ostream& writeToken(std::ostream& os, const std::string& str) {
|
deba@127
|
292 |
|
deba@127
|
293 |
if (requireEscape(str)) {
|
deba@127
|
294 |
os << '\"';
|
deba@127
|
295 |
for (std::string::const_iterator it = str.begin();
|
deba@127
|
296 |
it != str.end(); ++it) {
|
deba@127
|
297 |
writeEscape(os, *it);
|
deba@127
|
298 |
}
|
deba@127
|
299 |
os << '\"';
|
deba@127
|
300 |
} else {
|
deba@127
|
301 |
os << str;
|
deba@127
|
302 |
}
|
deba@127
|
303 |
return os;
|
deba@127
|
304 |
}
|
deba@127
|
305 |
|
deba@127
|
306 |
}
|
deba@190
|
307 |
|
deba@190
|
308 |
template <typename Digraph>
|
deba@190
|
309 |
class DigraphWriter;
|
deba@190
|
310 |
|
deba@190
|
311 |
template <typename Digraph>
|
deba@190
|
312 |
DigraphWriter<Digraph> digraphWriter(std::ostream& os,
|
deba@190
|
313 |
const Digraph& digraph);
|
deba@190
|
314 |
|
deba@190
|
315 |
template <typename Digraph>
|
deba@190
|
316 |
DigraphWriter<Digraph> digraphWriter(const std::string& fn,
|
deba@190
|
317 |
const Digraph& digraph);
|
deba@190
|
318 |
|
deba@190
|
319 |
template <typename Digraph>
|
deba@190
|
320 |
DigraphWriter<Digraph> digraphWriter(const char *fn,
|
deba@190
|
321 |
const Digraph& digraph);
|
deba@127
|
322 |
|
alpar@156
|
323 |
/// \ingroup lemon_io
|
alpar@156
|
324 |
///
|
kpeter@192
|
325 |
/// \brief \ref lgf-format "LGF" writer for directed graphs
|
alpar@156
|
326 |
///
|
alpar@156
|
327 |
/// This utility writes an \ref lgf-format "LGF" file.
|
alpar@156
|
328 |
///
|
alpar@156
|
329 |
/// The writing method does a batch processing. The user creates a
|
alpar@156
|
330 |
/// writer object, then various writing rules can be added to the
|
alpar@156
|
331 |
/// writer, and eventually the writing is executed with the \c run()
|
alpar@156
|
332 |
/// member function. A map writing rule can be added to the writer
|
alpar@156
|
333 |
/// with the \c nodeMap() or \c arcMap() members. An optional
|
deba@163
|
334 |
/// converter parameter can also be added as a standard functor
|
kpeter@192
|
335 |
/// converting from the value type of the map to \c std::string. If it
|
kpeter@192
|
336 |
/// is set, it will determine how the value type of the map is written to
|
deba@163
|
337 |
/// the output stream. If the functor is not set, then a default
|
deba@163
|
338 |
/// conversion will be used. The \c attribute(), \c node() and \c
|
deba@163
|
339 |
/// arc() functions are used to add attribute writing rules.
|
alpar@156
|
340 |
///
|
alpar@156
|
341 |
///\code
|
kpeter@192
|
342 |
/// DigraphWriter<Digraph>(std::cout, digraph).
|
kpeter@192
|
343 |
/// nodeMap("coordinates", coord_map).
|
kpeter@192
|
344 |
/// nodeMap("size", size).
|
kpeter@192
|
345 |
/// nodeMap("title", title).
|
kpeter@192
|
346 |
/// arcMap("capacity", cap_map).
|
kpeter@192
|
347 |
/// node("source", src).
|
kpeter@192
|
348 |
/// node("target", trg).
|
kpeter@192
|
349 |
/// attribute("caption", caption).
|
kpeter@192
|
350 |
/// run();
|
alpar@156
|
351 |
///\endcode
|
alpar@156
|
352 |
///
|
alpar@156
|
353 |
///
|
alpar@156
|
354 |
/// By default, the writer does not write additional captions to the
|
alpar@156
|
355 |
/// sections, but they can be give as an optional parameter of
|
alpar@156
|
356 |
/// the \c nodes(), \c arcs() or \c
|
alpar@156
|
357 |
/// attributes() functions.
|
alpar@156
|
358 |
///
|
alpar@156
|
359 |
/// The \c skipNodes() and \c skipArcs() functions forbid the
|
deba@163
|
360 |
/// writing of the sections. If two arc sections should be written
|
deba@163
|
361 |
/// to the output, it can be done in two passes, the first pass
|
deba@163
|
362 |
/// writes the node section and the first arc section, then the
|
deba@163
|
363 |
/// second pass skips the node section and writes just the arc
|
deba@163
|
364 |
/// section to the stream. The output stream can be retrieved with
|
deba@163
|
365 |
/// the \c ostream() function, hence the second pass can append its
|
deba@163
|
366 |
/// output to the output of the first pass.
|
deba@127
|
367 |
template <typename _Digraph>
|
deba@127
|
368 |
class DigraphWriter {
|
deba@127
|
369 |
public:
|
deba@127
|
370 |
|
deba@127
|
371 |
typedef _Digraph Digraph;
|
deba@148
|
372 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
deba@127
|
373 |
|
deba@127
|
374 |
private:
|
deba@127
|
375 |
|
deba@127
|
376 |
|
deba@127
|
377 |
std::ostream* _os;
|
deba@127
|
378 |
bool local_os;
|
deba@127
|
379 |
|
deba@190
|
380 |
const Digraph& _digraph;
|
deba@127
|
381 |
|
deba@127
|
382 |
std::string _nodes_caption;
|
deba@127
|
383 |
std::string _arcs_caption;
|
deba@127
|
384 |
std::string _attributes_caption;
|
deba@127
|
385 |
|
deba@127
|
386 |
typedef std::map<Node, std::string> NodeIndex;
|
deba@127
|
387 |
NodeIndex _node_index;
|
deba@127
|
388 |
typedef std::map<Arc, std::string> ArcIndex;
|
deba@127
|
389 |
ArcIndex _arc_index;
|
deba@127
|
390 |
|
deba@127
|
391 |
typedef std::vector<std::pair<std::string,
|
deba@127
|
392 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps;
|
deba@127
|
393 |
NodeMaps _node_maps;
|
deba@127
|
394 |
|
deba@127
|
395 |
typedef std::vector<std::pair<std::string,
|
deba@127
|
396 |
_writer_bits::MapStorageBase<Arc>* > >ArcMaps;
|
deba@127
|
397 |
ArcMaps _arc_maps;
|
deba@127
|
398 |
|
deba@127
|
399 |
typedef std::vector<std::pair<std::string,
|
deba@127
|
400 |
_writer_bits::ValueStorageBase*> > Attributes;
|
deba@127
|
401 |
Attributes _attributes;
|
deba@127
|
402 |
|
deba@127
|
403 |
bool _skip_nodes;
|
deba@127
|
404 |
bool _skip_arcs;
|
deba@127
|
405 |
|
deba@127
|
406 |
public:
|
deba@127
|
407 |
|
alpar@156
|
408 |
/// \brief Constructor
|
alpar@156
|
409 |
///
|
alpar@156
|
410 |
/// Construct a directed graph writer, which writes to the given
|
alpar@156
|
411 |
/// output stream.
|
deba@190
|
412 |
DigraphWriter(std::ostream& is, const Digraph& digraph)
|
deba@127
|
413 |
: _os(&is), local_os(false), _digraph(digraph),
|
deba@127
|
414 |
_skip_nodes(false), _skip_arcs(false) {}
|
deba@127
|
415 |
|
alpar@156
|
416 |
/// \brief Constructor
|
alpar@156
|
417 |
///
|
alpar@156
|
418 |
/// Construct a directed graph writer, which writes to the given
|
alpar@156
|
419 |
/// output file.
|
deba@190
|
420 |
DigraphWriter(const std::string& fn, const Digraph& digraph)
|
deba@127
|
421 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
|
deba@127
|
422 |
_skip_nodes(false), _skip_arcs(false) {}
|
deba@127
|
423 |
|
alpar@156
|
424 |
/// \brief Constructor
|
alpar@156
|
425 |
///
|
alpar@156
|
426 |
/// Construct a directed graph writer, which writes to the given
|
alpar@156
|
427 |
/// output file.
|
deba@190
|
428 |
DigraphWriter(const char* fn, const Digraph& digraph)
|
deba@127
|
429 |
: _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
|
deba@127
|
430 |
_skip_nodes(false), _skip_arcs(false) {}
|
deba@127
|
431 |
|
alpar@156
|
432 |
/// \brief Destructor
|
deba@127
|
433 |
~DigraphWriter() {
|
deba@127
|
434 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@127
|
435 |
it != _node_maps.end(); ++it) {
|
deba@127
|
436 |
delete it->second;
|
deba@127
|
437 |
}
|
deba@127
|
438 |
|
deba@127
|
439 |
for (typename ArcMaps::iterator it = _arc_maps.begin();
|
deba@127
|
440 |
it != _arc_maps.end(); ++it) {
|
deba@127
|
441 |
delete it->second;
|
deba@127
|
442 |
}
|
deba@127
|
443 |
|
deba@127
|
444 |
for (typename Attributes::iterator it = _attributes.begin();
|
deba@127
|
445 |
it != _attributes.end(); ++it) {
|
deba@127
|
446 |
delete it->second;
|
deba@127
|
447 |
}
|
deba@127
|
448 |
|
deba@127
|
449 |
if (local_os) {
|
deba@127
|
450 |
delete _os;
|
deba@127
|
451 |
}
|
deba@127
|
452 |
}
|
deba@127
|
453 |
|
deba@127
|
454 |
private:
|
deba@190
|
455 |
|
deba@190
|
456 |
friend DigraphWriter<Digraph> digraphWriter<>(std::ostream& os,
|
deba@190
|
457 |
const Digraph& digraph);
|
deba@190
|
458 |
friend DigraphWriter<Digraph> digraphWriter<>(const std::string& fn,
|
deba@190
|
459 |
const Digraph& digraph);
|
deba@190
|
460 |
friend DigraphWriter<Digraph> digraphWriter<>(const char *fn,
|
deba@190
|
461 |
const Digraph& digraph);
|
deba@190
|
462 |
|
deba@190
|
463 |
DigraphWriter(DigraphWriter& other)
|
deba@190
|
464 |
: _os(other._os), local_os(other.local_os), _digraph(other._digraph),
|
deba@190
|
465 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
deba@190
|
466 |
|
deba@190
|
467 |
other._os = 0;
|
deba@190
|
468 |
other.local_os = false;
|
deba@190
|
469 |
|
deba@190
|
470 |
_node_index.swap(other._node_index);
|
deba@190
|
471 |
_arc_index.swap(other._arc_index);
|
deba@190
|
472 |
|
deba@190
|
473 |
_node_maps.swap(other._node_maps);
|
deba@190
|
474 |
_arc_maps.swap(other._arc_maps);
|
deba@190
|
475 |
_attributes.swap(other._attributes);
|
deba@190
|
476 |
|
deba@190
|
477 |
_nodes_caption = other._nodes_caption;
|
deba@190
|
478 |
_arcs_caption = other._arcs_caption;
|
deba@190
|
479 |
_attributes_caption = other._attributes_caption;
|
deba@190
|
480 |
}
|
deba@127
|
481 |
|
deba@127
|
482 |
DigraphWriter& operator=(const DigraphWriter&);
|
deba@127
|
483 |
|
deba@127
|
484 |
public:
|
deba@127
|
485 |
|
alpar@156
|
486 |
/// \name Writing rules
|
alpar@156
|
487 |
/// @{
|
alpar@156
|
488 |
|
kpeter@192
|
489 |
/// \brief Node map writing rule
|
alpar@156
|
490 |
///
|
kpeter@192
|
491 |
/// Add a node map writing rule to the writer.
|
deba@127
|
492 |
template <typename Map>
|
deba@127
|
493 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
|
deba@127
|
494 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
|
deba@127
|
495 |
_writer_bits::MapStorageBase<Node>* storage =
|
deba@127
|
496 |
new _writer_bits::MapStorage<Node, Map>(map);
|
deba@127
|
497 |
_node_maps.push_back(std::make_pair(caption, storage));
|
deba@127
|
498 |
return *this;
|
deba@127
|
499 |
}
|
deba@127
|
500 |
|
alpar@156
|
501 |
/// \brief Node map writing rule
|
alpar@156
|
502 |
///
|
alpar@156
|
503 |
/// Add a node map writing rule with specialized converter to the
|
alpar@156
|
504 |
/// writer.
|
deba@127
|
505 |
template <typename Map, typename Converter>
|
deba@127
|
506 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map,
|
deba@127
|
507 |
const Converter& converter = Converter()) {
|
deba@127
|
508 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
|
deba@127
|
509 |
_writer_bits::MapStorageBase<Node>* storage =
|
deba@127
|
510 |
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
|
deba@127
|
511 |
_node_maps.push_back(std::make_pair(caption, storage));
|
deba@127
|
512 |
return *this;
|
deba@127
|
513 |
}
|
deba@127
|
514 |
|
alpar@156
|
515 |
/// \brief Arc map writing rule
|
alpar@156
|
516 |
///
|
alpar@156
|
517 |
/// Add an arc map writing rule to the writer.
|
deba@127
|
518 |
template <typename Map>
|
deba@127
|
519 |
DigraphWriter& arcMap(const std::string& caption, const Map& map) {
|
deba@127
|
520 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
|
deba@127
|
521 |
_writer_bits::MapStorageBase<Arc>* storage =
|
deba@127
|
522 |
new _writer_bits::MapStorage<Arc, Map>(map);
|
deba@127
|
523 |
_arc_maps.push_back(std::make_pair(caption, storage));
|
deba@127
|
524 |
return *this;
|
deba@127
|
525 |
}
|
deba@127
|
526 |
|
alpar@156
|
527 |
/// \brief Arc map writing rule
|
alpar@156
|
528 |
///
|
alpar@156
|
529 |
/// Add an arc map writing rule with specialized converter to the
|
alpar@156
|
530 |
/// writer.
|
deba@127
|
531 |
template <typename Map, typename Converter>
|
deba@127
|
532 |
DigraphWriter& arcMap(const std::string& caption, const Map& map,
|
deba@127
|
533 |
const Converter& converter = Converter()) {
|
deba@127
|
534 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
|
deba@127
|
535 |
_writer_bits::MapStorageBase<Arc>* storage =
|
deba@127
|
536 |
new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter);
|
deba@127
|
537 |
_arc_maps.push_back(std::make_pair(caption, storage));
|
deba@127
|
538 |
return *this;
|
deba@127
|
539 |
}
|
deba@127
|
540 |
|
alpar@156
|
541 |
/// \brief Attribute writing rule
|
alpar@156
|
542 |
///
|
alpar@156
|
543 |
/// Add an attribute writing rule to the writer.
|
deba@127
|
544 |
template <typename Value>
|
deba@127
|
545 |
DigraphWriter& attribute(const std::string& caption, const Value& value) {
|
deba@127
|
546 |
_writer_bits::ValueStorageBase* storage =
|
deba@127
|
547 |
new _writer_bits::ValueStorage<Value>(value);
|
deba@127
|
548 |
_attributes.push_back(std::make_pair(caption, storage));
|
deba@127
|
549 |
return *this;
|
deba@127
|
550 |
}
|
deba@127
|
551 |
|
alpar@156
|
552 |
/// \brief Attribute writing rule
|
alpar@156
|
553 |
///
|
alpar@156
|
554 |
/// Add an attribute writing rule with specialized converter to the
|
alpar@156
|
555 |
/// writer.
|
deba@127
|
556 |
template <typename Value, typename Converter>
|
deba@127
|
557 |
DigraphWriter& attribute(const std::string& caption, const Value& value,
|
deba@127
|
558 |
const Converter& converter = Converter()) {
|
deba@127
|
559 |
_writer_bits::ValueStorageBase* storage =
|
deba@127
|
560 |
new _writer_bits::ValueStorage<Value, Converter>(value, converter);
|
deba@127
|
561 |
_attributes.push_back(std::make_pair(caption, storage));
|
deba@127
|
562 |
return *this;
|
deba@127
|
563 |
}
|
deba@127
|
564 |
|
alpar@156
|
565 |
/// \brief Node writing rule
|
alpar@156
|
566 |
///
|
alpar@156
|
567 |
/// Add a node writing rule to the writer.
|
deba@127
|
568 |
DigraphWriter& node(const std::string& caption, const Node& node) {
|
deba@127
|
569 |
typedef _writer_bits::MapLookUpConverter<Node> Converter;
|
deba@127
|
570 |
Converter converter(_node_index);
|
deba@127
|
571 |
_writer_bits::ValueStorageBase* storage =
|
deba@127
|
572 |
new _writer_bits::ValueStorage<Node, Converter>(node, converter);
|
deba@127
|
573 |
_attributes.push_back(std::make_pair(caption, storage));
|
deba@127
|
574 |
return *this;
|
deba@127
|
575 |
}
|
deba@127
|
576 |
|
alpar@156
|
577 |
/// \brief Arc writing rule
|
alpar@156
|
578 |
///
|
alpar@156
|
579 |
/// Add an arc writing rule to writer.
|
deba@127
|
580 |
DigraphWriter& arc(const std::string& caption, const Arc& arc) {
|
deba@127
|
581 |
typedef _writer_bits::MapLookUpConverter<Arc> Converter;
|
deba@127
|
582 |
Converter converter(_arc_index);
|
deba@127
|
583 |
_writer_bits::ValueStorageBase* storage =
|
deba@127
|
584 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
|
deba@127
|
585 |
_attributes.push_back(std::make_pair(caption, storage));
|
deba@127
|
586 |
return *this;
|
deba@127
|
587 |
}
|
deba@127
|
588 |
|
kpeter@192
|
589 |
/// \name Section captions
|
alpar@156
|
590 |
/// @{
|
alpar@156
|
591 |
|
kpeter@192
|
592 |
/// \brief Add an additional caption to the \c \@nodes section
|
alpar@156
|
593 |
///
|
kpeter@192
|
594 |
/// Add an additional caption to the \c \@nodes section.
|
deba@127
|
595 |
DigraphWriter& nodes(const std::string& caption) {
|
deba@127
|
596 |
_nodes_caption = caption;
|
deba@127
|
597 |
return *this;
|
deba@127
|
598 |
}
|
deba@127
|
599 |
|
kpeter@192
|
600 |
/// \brief Add an additional caption to the \c \@arcs section
|
alpar@156
|
601 |
///
|
kpeter@192
|
602 |
/// Add an additional caption to the \c \@arcs section.
|
deba@127
|
603 |
DigraphWriter& arcs(const std::string& caption) {
|
deba@127
|
604 |
_arcs_caption = caption;
|
deba@127
|
605 |
return *this;
|
deba@127
|
606 |
}
|
deba@127
|
607 |
|
kpeter@192
|
608 |
/// \brief Add an additional caption to the \c \@attributes section
|
alpar@156
|
609 |
///
|
kpeter@192
|
610 |
/// Add an additional caption to the \c \@attributes section.
|
deba@127
|
611 |
DigraphWriter& attributes(const std::string& caption) {
|
deba@127
|
612 |
_attributes_caption = caption;
|
deba@127
|
613 |
return *this;
|
deba@127
|
614 |
}
|
deba@127
|
615 |
|
alpar@156
|
616 |
/// \name Skipping section
|
alpar@156
|
617 |
/// @{
|
alpar@156
|
618 |
|
alpar@156
|
619 |
/// \brief Skip writing the node set
|
alpar@156
|
620 |
///
|
kpeter@192
|
621 |
/// The \c \@nodes section will not be written to the stream.
|
deba@127
|
622 |
DigraphWriter& skipNodes() {
|
deba@127
|
623 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
|
deba@185
|
624 |
_skip_nodes = true;
|
deba@127
|
625 |
return *this;
|
deba@127
|
626 |
}
|
deba@127
|
627 |
|
alpar@156
|
628 |
/// \brief Skip writing arc set
|
alpar@156
|
629 |
///
|
kpeter@192
|
630 |
/// The \c \@arcs section will not be written to the stream.
|
deba@127
|
631 |
DigraphWriter& skipArcs() {
|
deba@127
|
632 |
LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
|
deba@185
|
633 |
_skip_arcs = true;
|
deba@127
|
634 |
return *this;
|
deba@127
|
635 |
}
|
deba@127
|
636 |
|
alpar@156
|
637 |
/// @}
|
alpar@156
|
638 |
|
deba@127
|
639 |
private:
|
deba@127
|
640 |
|
deba@127
|
641 |
void writeNodes() {
|
deba@127
|
642 |
_writer_bits::MapStorageBase<Node>* label = 0;
|
deba@127
|
643 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@127
|
644 |
it != _node_maps.end(); ++it) {
|
deba@127
|
645 |
if (it->first == "label") {
|
deba@127
|
646 |
label = it->second;
|
deba@127
|
647 |
break;
|
deba@127
|
648 |
}
|
deba@127
|
649 |
}
|
deba@127
|
650 |
|
deba@127
|
651 |
*_os << "@nodes";
|
deba@127
|
652 |
if (!_nodes_caption.empty()) {
|
alpar@156
|
653 |
_writer_bits::writeToken(*_os << ' ', _nodes_caption);
|
deba@127
|
654 |
}
|
deba@127
|
655 |
*_os << std::endl;
|
deba@127
|
656 |
|
deba@127
|
657 |
if (label == 0) {
|
deba@127
|
658 |
*_os << "label" << '\t';
|
deba@127
|
659 |
}
|
deba@127
|
660 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@127
|
661 |
it != _node_maps.end(); ++it) {
|
alpar@156
|
662 |
_writer_bits::writeToken(*_os, it->first) << '\t';
|
deba@127
|
663 |
}
|
deba@127
|
664 |
*_os << std::endl;
|
deba@127
|
665 |
|
deba@127
|
666 |
std::vector<Node> nodes;
|
deba@127
|
667 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
deba@127
|
668 |
nodes.push_back(n);
|
deba@127
|
669 |
}
|
deba@127
|
670 |
|
deba@127
|
671 |
if (label == 0) {
|
deba@127
|
672 |
IdMap<Digraph, Node> id_map(_digraph);
|
deba@127
|
673 |
_writer_bits::MapLess<IdMap<Digraph, Node> > id_less(id_map);
|
deba@127
|
674 |
std::sort(nodes.begin(), nodes.end(), id_less);
|
deba@127
|
675 |
} else {
|
deba@127
|
676 |
label->sort(nodes);
|
deba@127
|
677 |
}
|
deba@127
|
678 |
|
deba@127
|
679 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
|
deba@127
|
680 |
Node n = nodes[i];
|
deba@127
|
681 |
if (label == 0) {
|
deba@127
|
682 |
std::ostringstream os;
|
deba@127
|
683 |
os << _digraph.id(n);
|
deba@127
|
684 |
_writer_bits::writeToken(*_os, os.str());
|
deba@127
|
685 |
*_os << '\t';
|
deba@127
|
686 |
_node_index.insert(std::make_pair(n, os.str()));
|
deba@127
|
687 |
}
|
deba@127
|
688 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@127
|
689 |
it != _node_maps.end(); ++it) {
|
deba@127
|
690 |
std::string value = it->second->get(n);
|
deba@127
|
691 |
_writer_bits::writeToken(*_os, value);
|
deba@127
|
692 |
if (it->first == "label") {
|
deba@127
|
693 |
_node_index.insert(std::make_pair(n, value));
|
deba@127
|
694 |
}
|
deba@127
|
695 |
*_os << '\t';
|
deba@127
|
696 |
}
|
deba@127
|
697 |
*_os << std::endl;
|
deba@127
|
698 |
}
|
deba@127
|
699 |
}
|
deba@127
|
700 |
|
deba@185
|
701 |
void createNodeIndex() {
|
deba@185
|
702 |
_writer_bits::MapStorageBase<Node>* label = 0;
|
deba@185
|
703 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@185
|
704 |
it != _node_maps.end(); ++it) {
|
deba@185
|
705 |
if (it->first == "label") {
|
deba@185
|
706 |
label = it->second;
|
deba@185
|
707 |
break;
|
deba@185
|
708 |
}
|
deba@185
|
709 |
}
|
deba@185
|
710 |
|
deba@185
|
711 |
if (label == 0) {
|
deba@185
|
712 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
deba@185
|
713 |
std::ostringstream os;
|
deba@185
|
714 |
os << _digraph.id(n);
|
deba@185
|
715 |
_node_index.insert(std::make_pair(n, os.str()));
|
deba@185
|
716 |
}
|
deba@185
|
717 |
} else {
|
deba@185
|
718 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
deba@185
|
719 |
std::string value = label->get(n);
|
deba@185
|
720 |
_node_index.insert(std::make_pair(n, value));
|
deba@185
|
721 |
}
|
deba@185
|
722 |
}
|
deba@185
|
723 |
}
|
deba@185
|
724 |
|
deba@127
|
725 |
void writeArcs() {
|
deba@127
|
726 |
_writer_bits::MapStorageBase<Arc>* label = 0;
|
deba@127
|
727 |
for (typename ArcMaps::iterator it = _arc_maps.begin();
|
deba@127
|
728 |
it != _arc_maps.end(); ++it) {
|
deba@127
|
729 |
if (it->first == "label") {
|
deba@127
|
730 |
label = it->second;
|
deba@127
|
731 |
break;
|
deba@127
|
732 |
}
|
deba@127
|
733 |
}
|
deba@127
|
734 |
|
deba@127
|
735 |
*_os << "@arcs";
|
deba@127
|
736 |
if (!_arcs_caption.empty()) {
|
alpar@156
|
737 |
_writer_bits::writeToken(*_os << ' ', _arcs_caption);
|
deba@127
|
738 |
}
|
deba@127
|
739 |
*_os << std::endl;
|
deba@127
|
740 |
|
deba@127
|
741 |
*_os << '\t' << '\t';
|
deba@127
|
742 |
if (label == 0) {
|
deba@127
|
743 |
*_os << "label" << '\t';
|
deba@127
|
744 |
}
|
deba@127
|
745 |
for (typename ArcMaps::iterator it = _arc_maps.begin();
|
deba@127
|
746 |
it != _arc_maps.end(); ++it) {
|
alpar@156
|
747 |
_writer_bits::writeToken(*_os, it->first) << '\t';
|
deba@127
|
748 |
}
|
deba@127
|
749 |
*_os << std::endl;
|
deba@127
|
750 |
|
deba@127
|
751 |
std::vector<Arc> arcs;
|
deba@127
|
752 |
for (ArcIt n(_digraph); n != INVALID; ++n) {
|
deba@127
|
753 |
arcs.push_back(n);
|
deba@127
|
754 |
}
|
deba@127
|
755 |
|
deba@127
|
756 |
if (label == 0) {
|
deba@127
|
757 |
IdMap<Digraph, Arc> id_map(_digraph);
|
deba@127
|
758 |
_writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map);
|
deba@127
|
759 |
std::sort(arcs.begin(), arcs.end(), id_less);
|
deba@127
|
760 |
} else {
|
deba@127
|
761 |
label->sort(arcs);
|
deba@127
|
762 |
}
|
deba@127
|
763 |
|
deba@127
|
764 |
for (int i = 0; i < static_cast<int>(arcs.size()); ++i) {
|
deba@127
|
765 |
Arc a = arcs[i];
|
deba@127
|
766 |
_writer_bits::writeToken(*_os, _node_index.
|
deba@127
|
767 |
find(_digraph.source(a))->second);
|
deba@127
|
768 |
*_os << '\t';
|
deba@127
|
769 |
_writer_bits::writeToken(*_os, _node_index.
|
deba@127
|
770 |
find(_digraph.target(a))->second);
|
deba@127
|
771 |
*_os << '\t';
|
deba@127
|
772 |
if (label == 0) {
|
deba@127
|
773 |
std::ostringstream os;
|
deba@127
|
774 |
os << _digraph.id(a);
|
deba@127
|
775 |
_writer_bits::writeToken(*_os, os.str());
|
deba@127
|
776 |
*_os << '\t';
|
deba@127
|
777 |
_arc_index.insert(std::make_pair(a, os.str()));
|
deba@127
|
778 |
}
|
deba@127
|
779 |
for (typename ArcMaps::iterator it = _arc_maps.begin();
|
deba@127
|
780 |
it != _arc_maps.end(); ++it) {
|
deba@127
|
781 |
std::string value = it->second->get(a);
|
deba@127
|
782 |
_writer_bits::writeToken(*_os, value);
|
deba@127
|
783 |
if (it->first == "label") {
|
deba@127
|
784 |
_arc_index.insert(std::make_pair(a, value));
|
deba@127
|
785 |
}
|
deba@127
|
786 |
*_os << '\t';
|
deba@127
|
787 |
}
|
deba@127
|
788 |
*_os << std::endl;
|
deba@127
|
789 |
}
|
deba@127
|
790 |
}
|
deba@127
|
791 |
|
deba@185
|
792 |
void createArcIndex() {
|
deba@185
|
793 |
_writer_bits::MapStorageBase<Arc>* label = 0;
|
deba@185
|
794 |
for (typename ArcMaps::iterator it = _arc_maps.begin();
|
deba@185
|
795 |
it != _arc_maps.end(); ++it) {
|
deba@185
|
796 |
if (it->first == "label") {
|
deba@185
|
797 |
label = it->second;
|
deba@185
|
798 |
break;
|
deba@185
|
799 |
}
|
deba@185
|
800 |
}
|
deba@185
|
801 |
|
deba@185
|
802 |
if (label == 0) {
|
deba@185
|
803 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
deba@185
|
804 |
std::ostringstream os;
|
deba@185
|
805 |
os << _digraph.id(a);
|
deba@185
|
806 |
_arc_index.insert(std::make_pair(a, os.str()));
|
deba@185
|
807 |
}
|
deba@185
|
808 |
} else {
|
deba@185
|
809 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
deba@185
|
810 |
std::string value = label->get(a);
|
deba@185
|
811 |
_arc_index.insert(std::make_pair(a, value));
|
deba@185
|
812 |
}
|
deba@185
|
813 |
}
|
deba@185
|
814 |
}
|
deba@185
|
815 |
|
deba@127
|
816 |
void writeAttributes() {
|
deba@127
|
817 |
if (_attributes.empty()) return;
|
deba@127
|
818 |
*_os << "@attributes";
|
deba@127
|
819 |
if (!_attributes_caption.empty()) {
|
alpar@156
|
820 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption);
|
deba@127
|
821 |
}
|
deba@127
|
822 |
*_os << std::endl;
|
deba@127
|
823 |
for (typename Attributes::iterator it = _attributes.begin();
|
deba@127
|
824 |
it != _attributes.end(); ++it) {
|
alpar@156
|
825 |
_writer_bits::writeToken(*_os, it->first) << ' ';
|
deba@127
|
826 |
_writer_bits::writeToken(*_os, it->second->get());
|
deba@127
|
827 |
*_os << std::endl;
|
deba@127
|
828 |
}
|
deba@127
|
829 |
}
|
deba@127
|
830 |
|
deba@127
|
831 |
public:
|
deba@127
|
832 |
|
alpar@156
|
833 |
/// \name Execution of the writer
|
alpar@156
|
834 |
/// @{
|
alpar@156
|
835 |
|
alpar@156
|
836 |
/// \brief Start the batch processing
|
alpar@156
|
837 |
///
|
kpeter@192
|
838 |
/// This function starts the batch processing.
|
deba@127
|
839 |
void run() {
|
deba@127
|
840 |
if (!_skip_nodes) {
|
deba@127
|
841 |
writeNodes();
|
deba@185
|
842 |
} else {
|
deba@185
|
843 |
createNodeIndex();
|
deba@127
|
844 |
}
|
deba@127
|
845 |
if (!_skip_arcs) {
|
deba@127
|
846 |
writeArcs();
|
deba@185
|
847 |
} else {
|
deba@185
|
848 |
createArcIndex();
|
deba@127
|
849 |
}
|
deba@127
|
850 |
writeAttributes();
|
deba@127
|
851 |
}
|
deba@127
|
852 |
|
kpeter@192
|
853 |
/// \brief Give back the stream of the writer
|
alpar@156
|
854 |
///
|
kpeter@192
|
855 |
/// Give back the stream of the writer.
|
alpar@156
|
856 |
std::ostream& ostream() {
|
deba@127
|
857 |
return *_os;
|
deba@127
|
858 |
}
|
alpar@156
|
859 |
|
alpar@156
|
860 |
/// @}
|
deba@127
|
861 |
};
|
deba@127
|
862 |
|
kpeter@192
|
863 |
/// \brief Return a \ref DigraphWriter class
|
kpeter@192
|
864 |
///
|
kpeter@192
|
865 |
/// This function just returns a \ref DigraphWriter class.
|
alpar@156
|
866 |
/// \relates DigraphWriter
|
deba@127
|
867 |
template <typename Digraph>
|
deba@190
|
868 |
DigraphWriter<Digraph> digraphWriter(std::ostream& os,
|
deba@190
|
869 |
const Digraph& digraph) {
|
deba@163
|
870 |
DigraphWriter<Digraph> tmp(os, digraph);
|
deba@163
|
871 |
return tmp;
|
deba@127
|
872 |
}
|
deba@127
|
873 |
|
kpeter@192
|
874 |
/// \brief Return a \ref DigraphWriter class
|
kpeter@192
|
875 |
///
|
kpeter@192
|
876 |
/// This function just returns a \ref DigraphWriter class.
|
alpar@156
|
877 |
/// \relates DigraphWriter
|
deba@127
|
878 |
template <typename Digraph>
|
deba@127
|
879 |
DigraphWriter<Digraph> digraphWriter(const std::string& fn,
|
deba@190
|
880 |
const Digraph& digraph) {
|
deba@163
|
881 |
DigraphWriter<Digraph> tmp(fn, digraph);
|
deba@163
|
882 |
return tmp;
|
deba@127
|
883 |
}
|
deba@127
|
884 |
|
kpeter@192
|
885 |
/// \brief Return a \ref DigraphWriter class
|
kpeter@192
|
886 |
///
|
kpeter@192
|
887 |
/// This function just returns a \ref DigraphWriter class.
|
alpar@156
|
888 |
/// \relates DigraphWriter
|
deba@127
|
889 |
template <typename Digraph>
|
deba@190
|
890 |
DigraphWriter<Digraph> digraphWriter(const char* fn,
|
deba@190
|
891 |
const Digraph& digraph) {
|
deba@163
|
892 |
DigraphWriter<Digraph> tmp(fn, digraph);
|
deba@163
|
893 |
return tmp;
|
deba@127
|
894 |
}
|
deba@165
|
895 |
|
deba@190
|
896 |
template <typename Graph>
|
deba@190
|
897 |
class GraphWriter;
|
deba@190
|
898 |
|
deba@190
|
899 |
template <typename Graph>
|
deba@190
|
900 |
GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph);
|
deba@190
|
901 |
|
deba@190
|
902 |
template <typename Graph>
|
deba@190
|
903 |
GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph);
|
deba@190
|
904 |
|
deba@190
|
905 |
template <typename Graph>
|
deba@190
|
906 |
GraphWriter<Graph> graphWriter(const char *fn, const Graph& graph);
|
deba@190
|
907 |
|
deba@165
|
908 |
/// \ingroup lemon_io
|
deba@165
|
909 |
///
|
kpeter@192
|
910 |
/// \brief \ref lgf-format "LGF" writer for directed graphs
|
deba@165
|
911 |
///
|
deba@165
|
912 |
/// This utility writes an \ref lgf-format "LGF" file.
|
kpeter@192
|
913 |
///
|
kpeter@192
|
914 |
/// It can be used almost the same way as \c DigraphWriter.
|
kpeter@192
|
915 |
/// The only difference is that this class can handle edges and
|
kpeter@192
|
916 |
/// edge maps as well as arcs and arc maps.
|
deba@165
|
917 |
template <typename _Graph>
|
deba@165
|
918 |
class GraphWriter {
|
deba@165
|
919 |
public:
|
deba@165
|
920 |
|
deba@165
|
921 |
typedef _Graph Graph;
|
deba@165
|
922 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
deba@165
|
923 |
|
deba@165
|
924 |
private:
|
deba@165
|
925 |
|
deba@165
|
926 |
|
deba@165
|
927 |
std::ostream* _os;
|
deba@165
|
928 |
bool local_os;
|
deba@165
|
929 |
|
deba@165
|
930 |
Graph& _graph;
|
deba@165
|
931 |
|
deba@165
|
932 |
std::string _nodes_caption;
|
deba@165
|
933 |
std::string _edges_caption;
|
deba@165
|
934 |
std::string _attributes_caption;
|
deba@165
|
935 |
|
deba@165
|
936 |
typedef std::map<Node, std::string> NodeIndex;
|
deba@165
|
937 |
NodeIndex _node_index;
|
deba@165
|
938 |
typedef std::map<Edge, std::string> EdgeIndex;
|
deba@165
|
939 |
EdgeIndex _edge_index;
|
deba@165
|
940 |
|
deba@165
|
941 |
typedef std::vector<std::pair<std::string,
|
deba@165
|
942 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps;
|
deba@165
|
943 |
NodeMaps _node_maps;
|
deba@165
|
944 |
|
deba@165
|
945 |
typedef std::vector<std::pair<std::string,
|
deba@165
|
946 |
_writer_bits::MapStorageBase<Edge>* > >EdgeMaps;
|
deba@165
|
947 |
EdgeMaps _edge_maps;
|
deba@165
|
948 |
|
deba@165
|
949 |
typedef std::vector<std::pair<std::string,
|
deba@165
|
950 |
_writer_bits::ValueStorageBase*> > Attributes;
|
deba@165
|
951 |
Attributes _attributes;
|
deba@165
|
952 |
|
deba@165
|
953 |
bool _skip_nodes;
|
deba@165
|
954 |
bool _skip_edges;
|
deba@165
|
955 |
|
deba@165
|
956 |
public:
|
deba@165
|
957 |
|
deba@165
|
958 |
/// \brief Constructor
|
deba@165
|
959 |
///
|
deba@165
|
960 |
/// Construct a directed graph writer, which writes to the given
|
deba@165
|
961 |
/// output stream.
|
deba@190
|
962 |
GraphWriter(std::ostream& is, const Graph& graph)
|
deba@165
|
963 |
: _os(&is), local_os(false), _graph(graph),
|
deba@165
|
964 |
_skip_nodes(false), _skip_edges(false) {}
|
deba@165
|
965 |
|
deba@165
|
966 |
/// \brief Constructor
|
deba@165
|
967 |
///
|
deba@165
|
968 |
/// Construct a directed graph writer, which writes to the given
|
deba@165
|
969 |
/// output file.
|
deba@190
|
970 |
GraphWriter(const std::string& fn, const Graph& graph)
|
deba@165
|
971 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
|
deba@165
|
972 |
_skip_nodes(false), _skip_edges(false) {}
|
deba@165
|
973 |
|
deba@165
|
974 |
/// \brief Constructor
|
deba@165
|
975 |
///
|
deba@165
|
976 |
/// Construct a directed graph writer, which writes to the given
|
deba@165
|
977 |
/// output file.
|
deba@190
|
978 |
GraphWriter(const char* fn, const Graph& graph)
|
deba@165
|
979 |
: _os(new std::ofstream(fn)), local_os(true), _graph(graph),
|
deba@165
|
980 |
_skip_nodes(false), _skip_edges(false) {}
|
deba@165
|
981 |
|
deba@165
|
982 |
/// \brief Destructor
|
deba@165
|
983 |
~GraphWriter() {
|
deba@165
|
984 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@165
|
985 |
it != _node_maps.end(); ++it) {
|
deba@165
|
986 |
delete it->second;
|
deba@165
|
987 |
}
|
deba@165
|
988 |
|
deba@165
|
989 |
for (typename EdgeMaps::iterator it = _edge_maps.begin();
|
deba@165
|
990 |
it != _edge_maps.end(); ++it) {
|
deba@165
|
991 |
delete it->second;
|
deba@165
|
992 |
}
|
deba@165
|
993 |
|
deba@165
|
994 |
for (typename Attributes::iterator it = _attributes.begin();
|
deba@165
|
995 |
it != _attributes.end(); ++it) {
|
deba@165
|
996 |
delete it->second;
|
deba@165
|
997 |
}
|
deba@165
|
998 |
|
deba@165
|
999 |
if (local_os) {
|
deba@165
|
1000 |
delete _os;
|
deba@165
|
1001 |
}
|
deba@165
|
1002 |
}
|
deba@190
|
1003 |
|
deba@190
|
1004 |
private:
|
deba@165
|
1005 |
|
deba@190
|
1006 |
friend GraphWriter<Graph> graphWriter<>(std::ostream& os,
|
deba@190
|
1007 |
const Graph& graph);
|
deba@190
|
1008 |
friend GraphWriter<Graph> graphWriter<>(const std::string& fn,
|
deba@190
|
1009 |
const Graph& graph);
|
deba@190
|
1010 |
friend GraphWriter<Graph> graphWriter<>(const char *fn,
|
deba@190
|
1011 |
const Graph& graph);
|
deba@190
|
1012 |
|
deba@190
|
1013 |
GraphWriter(GraphWriter& other)
|
deba@190
|
1014 |
: _os(other._os), local_os(other.local_os), _graph(other._graph),
|
deba@190
|
1015 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
deba@190
|
1016 |
|
deba@190
|
1017 |
other._os = 0;
|
deba@190
|
1018 |
other.local_os = false;
|
deba@190
|
1019 |
|
deba@190
|
1020 |
_node_index.swap(other._node_index);
|
deba@190
|
1021 |
_edge_index.swap(other._edge_index);
|
deba@190
|
1022 |
|
deba@190
|
1023 |
_node_maps.swap(other._node_maps);
|
deba@190
|
1024 |
_edge_maps.swap(other._edge_maps);
|
deba@190
|
1025 |
_attributes.swap(other._attributes);
|
deba@190
|
1026 |
|
deba@190
|
1027 |
_nodes_caption = other._nodes_caption;
|
deba@190
|
1028 |
_edges_caption = other._edges_caption;
|
deba@190
|
1029 |
_attributes_caption = other._attributes_caption;
|
deba@190
|
1030 |
}
|
deba@190
|
1031 |
|
deba@165
|
1032 |
GraphWriter& operator=(const GraphWriter&);
|
deba@165
|
1033 |
|
deba@165
|
1034 |
public:
|
deba@165
|
1035 |
|
deba@165
|
1036 |
/// \name Writing rules
|
deba@165
|
1037 |
/// @{
|
deba@165
|
1038 |
|
kpeter@192
|
1039 |
/// \brief Node map writing rule
|
deba@165
|
1040 |
///
|
kpeter@192
|
1041 |
/// Add a node map writing rule to the writer.
|
deba@165
|
1042 |
template <typename Map>
|
deba@165
|
1043 |
GraphWriter& nodeMap(const std::string& caption, const Map& map) {
|
deba@165
|
1044 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
|
deba@165
|
1045 |
_writer_bits::MapStorageBase<Node>* storage =
|
deba@165
|
1046 |
new _writer_bits::MapStorage<Node, Map>(map);
|
deba@165
|
1047 |
_node_maps.push_back(std::make_pair(caption, storage));
|
deba@165
|
1048 |
return *this;
|
deba@165
|
1049 |
}
|
deba@165
|
1050 |
|
deba@165
|
1051 |
/// \brief Node map writing rule
|
deba@165
|
1052 |
///
|
deba@165
|
1053 |
/// Add a node map writing rule with specialized converter to the
|
deba@165
|
1054 |
/// writer.
|
deba@165
|
1055 |
template <typename Map, typename Converter>
|
deba@165
|
1056 |
GraphWriter& nodeMap(const std::string& caption, const Map& map,
|
deba@165
|
1057 |
const Converter& converter = Converter()) {
|
deba@165
|
1058 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
|
deba@165
|
1059 |
_writer_bits::MapStorageBase<Node>* storage =
|
deba@165
|
1060 |
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
|
deba@165
|
1061 |
_node_maps.push_back(std::make_pair(caption, storage));
|
deba@165
|
1062 |
return *this;
|
deba@165
|
1063 |
}
|
deba@165
|
1064 |
|
deba@165
|
1065 |
/// \brief Edge map writing rule
|
deba@165
|
1066 |
///
|
deba@165
|
1067 |
/// Add an edge map writing rule to the writer.
|
deba@165
|
1068 |
template <typename Map>
|
deba@165
|
1069 |
GraphWriter& edgeMap(const std::string& caption, const Map& map) {
|
deba@165
|
1070 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
|
deba@165
|
1071 |
_writer_bits::MapStorageBase<Edge>* storage =
|
deba@165
|
1072 |
new _writer_bits::MapStorage<Edge, Map>(map);
|
deba@165
|
1073 |
_edge_maps.push_back(std::make_pair(caption, storage));
|
deba@165
|
1074 |
return *this;
|
deba@165
|
1075 |
}
|
deba@165
|
1076 |
|
deba@165
|
1077 |
/// \brief Edge map writing rule
|
deba@165
|
1078 |
///
|
deba@165
|
1079 |
/// Add an edge map writing rule with specialized converter to the
|
deba@165
|
1080 |
/// writer.
|
deba@165
|
1081 |
template <typename Map, typename Converter>
|
deba@165
|
1082 |
GraphWriter& edgeMap(const std::string& caption, const Map& map,
|
deba@165
|
1083 |
const Converter& converter = Converter()) {
|
deba@165
|
1084 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
|
deba@165
|
1085 |
_writer_bits::MapStorageBase<Edge>* storage =
|
deba@165
|
1086 |
new _writer_bits::MapStorage<Edge, Map, Converter>(map, converter);
|
deba@165
|
1087 |
_edge_maps.push_back(std::make_pair(caption, storage));
|
deba@165
|
1088 |
return *this;
|
deba@165
|
1089 |
}
|
deba@165
|
1090 |
|
deba@165
|
1091 |
/// \brief Arc map writing rule
|
deba@165
|
1092 |
///
|
deba@165
|
1093 |
/// Add an arc map writing rule to the writer.
|
deba@165
|
1094 |
template <typename Map>
|
deba@165
|
1095 |
GraphWriter& arcMap(const std::string& caption, const Map& map) {
|
deba@165
|
1096 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
|
deba@165
|
1097 |
_writer_bits::MapStorageBase<Edge>* forward_storage =
|
deba@165
|
1098 |
new _writer_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
|
deba@165
|
1099 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
deba@165
|
1100 |
_writer_bits::MapStorageBase<Edge>* backward_storage =
|
deba@165
|
1101 |
new _writer_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
|
deba@165
|
1102 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
deba@165
|
1103 |
return *this;
|
deba@165
|
1104 |
}
|
deba@165
|
1105 |
|
deba@165
|
1106 |
/// \brief Arc map writing rule
|
deba@165
|
1107 |
///
|
deba@165
|
1108 |
/// Add an arc map writing rule with specialized converter to the
|
deba@165
|
1109 |
/// writer.
|
deba@165
|
1110 |
template <typename Map, typename Converter>
|
deba@165
|
1111 |
GraphWriter& arcMap(const std::string& caption, const Map& map,
|
deba@165
|
1112 |
const Converter& converter = Converter()) {
|
deba@165
|
1113 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
|
deba@165
|
1114 |
_writer_bits::MapStorageBase<Edge>* forward_storage =
|
deba@165
|
1115 |
new _writer_bits::GraphArcMapStorage<Graph, true, Map, Converter>
|
deba@165
|
1116 |
(_graph, map, converter);
|
deba@165
|
1117 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
deba@165
|
1118 |
_writer_bits::MapStorageBase<Edge>* backward_storage =
|
deba@165
|
1119 |
new _writer_bits::GraphArcMapStorage<Graph, false, Map, Converter>
|
deba@165
|
1120 |
(_graph, map, converter);
|
deba@165
|
1121 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
deba@165
|
1122 |
return *this;
|
deba@165
|
1123 |
}
|
deba@165
|
1124 |
|
deba@165
|
1125 |
/// \brief Attribute writing rule
|
deba@165
|
1126 |
///
|
deba@165
|
1127 |
/// Add an attribute writing rule to the writer.
|
deba@165
|
1128 |
template <typename Value>
|
deba@165
|
1129 |
GraphWriter& attribute(const std::string& caption, const Value& value) {
|
deba@165
|
1130 |
_writer_bits::ValueStorageBase* storage =
|
deba@165
|
1131 |
new _writer_bits::ValueStorage<Value>(value);
|
deba@165
|
1132 |
_attributes.push_back(std::make_pair(caption, storage));
|
deba@165
|
1133 |
return *this;
|
deba@165
|
1134 |
}
|
deba@165
|
1135 |
|
deba@165
|
1136 |
/// \brief Attribute writing rule
|
deba@165
|
1137 |
///
|
deba@165
|
1138 |
/// Add an attribute writing rule with specialized converter to the
|
deba@165
|
1139 |
/// writer.
|
deba@165
|
1140 |
template <typename Value, typename Converter>
|
deba@165
|
1141 |
GraphWriter& attribute(const std::string& caption, const Value& value,
|
deba@165
|
1142 |
const Converter& converter = Converter()) {
|
deba@165
|
1143 |
_writer_bits::ValueStorageBase* storage =
|
deba@165
|
1144 |
new _writer_bits::ValueStorage<Value, Converter>(value, converter);
|
deba@165
|
1145 |
_attributes.push_back(std::make_pair(caption, storage));
|
deba@165
|
1146 |
return *this;
|
deba@165
|
1147 |
}
|
deba@165
|
1148 |
|
deba@165
|
1149 |
/// \brief Node writing rule
|
deba@165
|
1150 |
///
|
deba@165
|
1151 |
/// Add a node writing rule to the writer.
|
deba@165
|
1152 |
GraphWriter& node(const std::string& caption, const Node& node) {
|
deba@165
|
1153 |
typedef _writer_bits::MapLookUpConverter<Node> Converter;
|
deba@165
|
1154 |
Converter converter(_node_index);
|
deba@165
|
1155 |
_writer_bits::ValueStorageBase* storage =
|
deba@165
|
1156 |
new _writer_bits::ValueStorage<Node, Converter>(node, converter);
|
deba@165
|
1157 |
_attributes.push_back(std::make_pair(caption, storage));
|
deba@165
|
1158 |
return *this;
|
deba@165
|
1159 |
}
|
deba@165
|
1160 |
|
deba@165
|
1161 |
/// \brief Edge writing rule
|
deba@165
|
1162 |
///
|
deba@165
|
1163 |
/// Add an edge writing rule to writer.
|
deba@165
|
1164 |
GraphWriter& edge(const std::string& caption, const Edge& edge) {
|
deba@165
|
1165 |
typedef _writer_bits::MapLookUpConverter<Edge> Converter;
|
deba@165
|
1166 |
Converter converter(_edge_index);
|
deba@165
|
1167 |
_writer_bits::ValueStorageBase* storage =
|
deba@165
|
1168 |
new _writer_bits::ValueStorage<Edge, Converter>(edge, converter);
|
deba@165
|
1169 |
_attributes.push_back(std::make_pair(caption, storage));
|
deba@165
|
1170 |
return *this;
|
deba@165
|
1171 |
}
|
deba@165
|
1172 |
|
deba@165
|
1173 |
/// \brief Arc writing rule
|
deba@165
|
1174 |
///
|
deba@165
|
1175 |
/// Add an arc writing rule to writer.
|
deba@165
|
1176 |
GraphWriter& arc(const std::string& caption, const Arc& arc) {
|
deba@165
|
1177 |
typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter;
|
deba@165
|
1178 |
Converter converter(_graph, _edge_index);
|
deba@165
|
1179 |
_writer_bits::ValueStorageBase* storage =
|
deba@165
|
1180 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
|
deba@165
|
1181 |
_attributes.push_back(std::make_pair(caption, storage));
|
deba@165
|
1182 |
return *this;
|
deba@165
|
1183 |
}
|
deba@165
|
1184 |
|
kpeter@192
|
1185 |
/// \name Section captions
|
deba@165
|
1186 |
/// @{
|
deba@165
|
1187 |
|
kpeter@192
|
1188 |
/// \brief Add an additional caption to the \c \@nodes section
|
deba@165
|
1189 |
///
|
kpeter@192
|
1190 |
/// Add an additional caption to the \c \@nodes section.
|
deba@165
|
1191 |
GraphWriter& nodes(const std::string& caption) {
|
deba@165
|
1192 |
_nodes_caption = caption;
|
deba@165
|
1193 |
return *this;
|
deba@165
|
1194 |
}
|
deba@165
|
1195 |
|
kpeter@192
|
1196 |
/// \brief Add an additional caption to the \c \@arcs section
|
deba@165
|
1197 |
///
|
kpeter@192
|
1198 |
/// Add an additional caption to the \c \@arcs section.
|
deba@165
|
1199 |
GraphWriter& edges(const std::string& caption) {
|
deba@165
|
1200 |
_edges_caption = caption;
|
deba@165
|
1201 |
return *this;
|
deba@165
|
1202 |
}
|
deba@165
|
1203 |
|
kpeter@192
|
1204 |
/// \brief Add an additional caption to the \c \@attributes section
|
deba@165
|
1205 |
///
|
kpeter@192
|
1206 |
/// Add an additional caption to the \c \@attributes section.
|
deba@165
|
1207 |
GraphWriter& attributes(const std::string& caption) {
|
deba@165
|
1208 |
_attributes_caption = caption;
|
deba@165
|
1209 |
return *this;
|
deba@165
|
1210 |
}
|
deba@165
|
1211 |
|
deba@165
|
1212 |
/// \name Skipping section
|
deba@165
|
1213 |
/// @{
|
deba@165
|
1214 |
|
deba@165
|
1215 |
/// \brief Skip writing the node set
|
deba@165
|
1216 |
///
|
kpeter@192
|
1217 |
/// The \c \@nodes section will not be written to the stream.
|
deba@165
|
1218 |
GraphWriter& skipNodes() {
|
deba@165
|
1219 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
|
deba@185
|
1220 |
_skip_nodes = true;
|
deba@165
|
1221 |
return *this;
|
deba@165
|
1222 |
}
|
deba@165
|
1223 |
|
deba@165
|
1224 |
/// \brief Skip writing edge set
|
deba@165
|
1225 |
///
|
kpeter@192
|
1226 |
/// The \c \@edges section will not be written to the stream.
|
deba@165
|
1227 |
GraphWriter& skipEdges() {
|
deba@165
|
1228 |
LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
|
deba@185
|
1229 |
_skip_edges = true;
|
deba@165
|
1230 |
return *this;
|
deba@165
|
1231 |
}
|
deba@165
|
1232 |
|
deba@165
|
1233 |
/// @}
|
deba@165
|
1234 |
|
deba@165
|
1235 |
private:
|
deba@165
|
1236 |
|
deba@165
|
1237 |
void writeNodes() {
|
deba@165
|
1238 |
_writer_bits::MapStorageBase<Node>* label = 0;
|
deba@165
|
1239 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@165
|
1240 |
it != _node_maps.end(); ++it) {
|
deba@165
|
1241 |
if (it->first == "label") {
|
deba@165
|
1242 |
label = it->second;
|
deba@165
|
1243 |
break;
|
deba@165
|
1244 |
}
|
deba@165
|
1245 |
}
|
deba@165
|
1246 |
|
deba@165
|
1247 |
*_os << "@nodes";
|
deba@165
|
1248 |
if (!_nodes_caption.empty()) {
|
deba@165
|
1249 |
_writer_bits::writeToken(*_os << ' ', _nodes_caption);
|
deba@165
|
1250 |
}
|
deba@165
|
1251 |
*_os << std::endl;
|
deba@165
|
1252 |
|
deba@165
|
1253 |
if (label == 0) {
|
deba@165
|
1254 |
*_os << "label" << '\t';
|
deba@165
|
1255 |
}
|
deba@165
|
1256 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@165
|
1257 |
it != _node_maps.end(); ++it) {
|
deba@165
|
1258 |
_writer_bits::writeToken(*_os, it->first) << '\t';
|
deba@165
|
1259 |
}
|
deba@165
|
1260 |
*_os << std::endl;
|
deba@165
|
1261 |
|
deba@165
|
1262 |
std::vector<Node> nodes;
|
deba@165
|
1263 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@165
|
1264 |
nodes.push_back(n);
|
deba@165
|
1265 |
}
|
deba@165
|
1266 |
|
deba@165
|
1267 |
if (label == 0) {
|
deba@165
|
1268 |
IdMap<Graph, Node> id_map(_graph);
|
deba@165
|
1269 |
_writer_bits::MapLess<IdMap<Graph, Node> > id_less(id_map);
|
deba@165
|
1270 |
std::sort(nodes.begin(), nodes.end(), id_less);
|
deba@165
|
1271 |
} else {
|
deba@165
|
1272 |
label->sort(nodes);
|
deba@165
|
1273 |
}
|
deba@165
|
1274 |
|
deba@165
|
1275 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
|
deba@165
|
1276 |
Node n = nodes[i];
|
deba@165
|
1277 |
if (label == 0) {
|
deba@165
|
1278 |
std::ostringstream os;
|
deba@165
|
1279 |
os << _graph.id(n);
|
deba@165
|
1280 |
_writer_bits::writeToken(*_os, os.str());
|
deba@165
|
1281 |
*_os << '\t';
|
deba@165
|
1282 |
_node_index.insert(std::make_pair(n, os.str()));
|
deba@165
|
1283 |
}
|
deba@165
|
1284 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@165
|
1285 |
it != _node_maps.end(); ++it) {
|
deba@165
|
1286 |
std::string value = it->second->get(n);
|
deba@165
|
1287 |
_writer_bits::writeToken(*_os, value);
|
deba@165
|
1288 |
if (it->first == "label") {
|
deba@165
|
1289 |
_node_index.insert(std::make_pair(n, value));
|
deba@165
|
1290 |
}
|
deba@165
|
1291 |
*_os << '\t';
|
deba@165
|
1292 |
}
|
deba@165
|
1293 |
*_os << std::endl;
|
deba@165
|
1294 |
}
|
deba@165
|
1295 |
}
|
deba@165
|
1296 |
|
deba@185
|
1297 |
void createNodeIndex() {
|
deba@185
|
1298 |
_writer_bits::MapStorageBase<Node>* label = 0;
|
deba@185
|
1299 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
deba@185
|
1300 |
it != _node_maps.end(); ++it) {
|
deba@185
|
1301 |
if (it->first == "label") {
|
deba@185
|
1302 |
label = it->second;
|
deba@185
|
1303 |
break;
|
deba@185
|
1304 |
}
|
deba@185
|
1305 |
}
|
deba@185
|
1306 |
|
deba@185
|
1307 |
if (label == 0) {
|
deba@185
|
1308 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@185
|
1309 |
std::ostringstream os;
|
deba@185
|
1310 |
os << _graph.id(n);
|
deba@185
|
1311 |
_node_index.insert(std::make_pair(n, os.str()));
|
deba@185
|
1312 |
}
|
deba@185
|
1313 |
} else {
|
deba@185
|
1314 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@185
|
1315 |
std::string value = label->get(n);
|
deba@185
|
1316 |
_node_index.insert(std::make_pair(n, value));
|
deba@185
|
1317 |
}
|
deba@185
|
1318 |
}
|
deba@185
|
1319 |
}
|
deba@185
|
1320 |
|
deba@165
|
1321 |
void writeEdges() {
|
deba@165
|
1322 |
_writer_bits::MapStorageBase<Edge>* label = 0;
|
deba@165
|
1323 |
for (typename EdgeMaps::iterator it = _edge_maps.begin();
|
deba@165
|
1324 |
it != _edge_maps.end(); ++it) {
|
deba@165
|
1325 |
if (it->first == "label") {
|
deba@165
|
1326 |
label = it->second;
|
deba@165
|
1327 |
break;
|
deba@165
|
1328 |
}
|
deba@165
|
1329 |
}
|
deba@165
|
1330 |
|
deba@165
|
1331 |
*_os << "@edges";
|
deba@165
|
1332 |
if (!_edges_caption.empty()) {
|
deba@165
|
1333 |
_writer_bits::writeToken(*_os << ' ', _edges_caption);
|
deba@165
|
1334 |
}
|
deba@165
|
1335 |
*_os << std::endl;
|
deba@165
|
1336 |
|
deba@165
|
1337 |
*_os << '\t' << '\t';
|
deba@165
|
1338 |
if (label == 0) {
|
deba@165
|
1339 |
*_os << "label" << '\t';
|
deba@165
|
1340 |
}
|
deba@165
|
1341 |
for (typename EdgeMaps::iterator it = _edge_maps.begin();
|
deba@165
|
1342 |
it != _edge_maps.end(); ++it) {
|
deba@165
|
1343 |
_writer_bits::writeToken(*_os, it->first) << '\t';
|
deba@165
|
1344 |
}
|
deba@165
|
1345 |
*_os << std::endl;
|
deba@165
|
1346 |
|
deba@165
|
1347 |
std::vector<Edge> edges;
|
deba@165
|
1348 |
for (EdgeIt n(_graph); n != INVALID; ++n) {
|
deba@165
|
1349 |
edges.push_back(n);
|
deba@165
|
1350 |
}
|
deba@165
|
1351 |
|
deba@165
|
1352 |
if (label == 0) {
|
deba@165
|
1353 |
IdMap<Graph, Edge> id_map(_graph);
|
deba@165
|
1354 |
_writer_bits::MapLess<IdMap<Graph, Edge> > id_less(id_map);
|
deba@165
|
1355 |
std::sort(edges.begin(), edges.end(), id_less);
|
deba@165
|
1356 |
} else {
|
deba@165
|
1357 |
label->sort(edges);
|
deba@165
|
1358 |
}
|
deba@165
|
1359 |
|
deba@165
|
1360 |
for (int i = 0; i < static_cast<int>(edges.size()); ++i) {
|
deba@165
|
1361 |
Edge e = edges[i];
|
deba@165
|
1362 |
_writer_bits::writeToken(*_os, _node_index.
|
deba@165
|
1363 |
find(_graph.u(e))->second);
|
deba@165
|
1364 |
*_os << '\t';
|
deba@165
|
1365 |
_writer_bits::writeToken(*_os, _node_index.
|
deba@165
|
1366 |
find(_graph.v(e))->second);
|
deba@165
|
1367 |
*_os << '\t';
|
deba@165
|
1368 |
if (label == 0) {
|
deba@165
|
1369 |
std::ostringstream os;
|
deba@165
|
1370 |
os << _graph.id(e);
|
deba@165
|
1371 |
_writer_bits::writeToken(*_os, os.str());
|
deba@165
|
1372 |
*_os << '\t';
|
deba@165
|
1373 |
_edge_index.insert(std::make_pair(e, os.str()));
|
deba@165
|
1374 |
}
|
deba@165
|
1375 |
for (typename EdgeMaps::iterator it = _edge_maps.begin();
|
deba@165
|
1376 |
it != _edge_maps.end(); ++it) {
|
deba@165
|
1377 |
std::string value = it->second->get(e);
|
deba@165
|
1378 |
_writer_bits::writeToken(*_os, value);
|
deba@165
|
1379 |
if (it->first == "label") {
|
deba@165
|
1380 |
_edge_index.insert(std::make_pair(e, value));
|
deba@165
|
1381 |
}
|
deba@165
|
1382 |
*_os << '\t';
|
deba@165
|
1383 |
}
|
deba@165
|
1384 |
*_os << std::endl;
|
deba@165
|
1385 |
}
|
deba@165
|
1386 |
}
|
deba@165
|
1387 |
|
deba@185
|
1388 |
void createEdgeIndex() {
|
deba@185
|
1389 |
_writer_bits::MapStorageBase<Edge>* label = 0;
|
deba@185
|
1390 |
for (typename EdgeMaps::iterator it = _edge_maps.begin();
|
deba@185
|
1391 |
it != _edge_maps.end(); ++it) {
|
deba@185
|
1392 |
if (it->first == "label") {
|
deba@185
|
1393 |
label = it->second;
|
deba@185
|
1394 |
break;
|
deba@185
|
1395 |
}
|
deba@185
|
1396 |
}
|
deba@185
|
1397 |
|
deba@185
|
1398 |
if (label == 0) {
|
deba@185
|
1399 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@185
|
1400 |
std::ostringstream os;
|
deba@185
|
1401 |
os << _graph.id(e);
|
deba@185
|
1402 |
_edge_index.insert(std::make_pair(e, os.str()));
|
deba@185
|
1403 |
}
|
deba@185
|
1404 |
} else {
|
deba@185
|
1405 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@185
|
1406 |
std::string value = label->get(e);
|
deba@185
|
1407 |
_edge_index.insert(std::make_pair(e, value));
|
deba@185
|
1408 |
}
|
deba@185
|
1409 |
}
|
deba@185
|
1410 |
}
|
deba@185
|
1411 |
|
deba@165
|
1412 |
void writeAttributes() {
|
deba@165
|
1413 |
if (_attributes.empty()) return;
|
deba@165
|
1414 |
*_os << "@attributes";
|
deba@165
|
1415 |
if (!_attributes_caption.empty()) {
|
deba@165
|
1416 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption);
|
deba@165
|
1417 |
}
|
deba@165
|
1418 |
*_os << std::endl;
|
deba@165
|
1419 |
for (typename Attributes::iterator it = _attributes.begin();
|
deba@165
|
1420 |
it != _attributes.end(); ++it) {
|
deba@165
|
1421 |
_writer_bits::writeToken(*_os, it->first) << ' ';
|
deba@165
|
1422 |
_writer_bits::writeToken(*_os, it->second->get());
|
deba@165
|
1423 |
*_os << std::endl;
|
deba@165
|
1424 |
}
|
deba@165
|
1425 |
}
|
deba@165
|
1426 |
|
deba@165
|
1427 |
public:
|
deba@165
|
1428 |
|
deba@165
|
1429 |
/// \name Execution of the writer
|
deba@165
|
1430 |
/// @{
|
deba@165
|
1431 |
|
deba@165
|
1432 |
/// \brief Start the batch processing
|
deba@165
|
1433 |
///
|
kpeter@192
|
1434 |
/// This function starts the batch processing.
|
deba@165
|
1435 |
void run() {
|
deba@165
|
1436 |
if (!_skip_nodes) {
|
deba@165
|
1437 |
writeNodes();
|
deba@185
|
1438 |
} else {
|
deba@185
|
1439 |
createNodeIndex();
|
deba@165
|
1440 |
}
|
deba@165
|
1441 |
if (!_skip_edges) {
|
deba@165
|
1442 |
writeEdges();
|
deba@185
|
1443 |
} else {
|
deba@185
|
1444 |
createEdgeIndex();
|
deba@165
|
1445 |
}
|
deba@165
|
1446 |
writeAttributes();
|
deba@165
|
1447 |
}
|
deba@165
|
1448 |
|
kpeter@192
|
1449 |
/// \brief Give back the stream of the writer
|
deba@165
|
1450 |
///
|
kpeter@192
|
1451 |
/// Give back the stream of the writer
|
deba@165
|
1452 |
std::ostream& ostream() {
|
deba@165
|
1453 |
return *_os;
|
deba@165
|
1454 |
}
|
deba@165
|
1455 |
|
deba@165
|
1456 |
/// @}
|
deba@165
|
1457 |
};
|
deba@165
|
1458 |
|
kpeter@192
|
1459 |
/// \brief Return a \ref GraphWriter class
|
kpeter@192
|
1460 |
///
|
kpeter@192
|
1461 |
/// This function just returns a \ref GraphWriter class.
|
deba@165
|
1462 |
/// \relates GraphWriter
|
deba@165
|
1463 |
template <typename Graph>
|
deba@190
|
1464 |
GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph) {
|
deba@165
|
1465 |
GraphWriter<Graph> tmp(os, graph);
|
deba@165
|
1466 |
return tmp;
|
deba@165
|
1467 |
}
|
deba@165
|
1468 |
|
kpeter@192
|
1469 |
/// \brief Return a \ref GraphWriter class
|
kpeter@192
|
1470 |
///
|
kpeter@192
|
1471 |
/// This function just returns a \ref GraphWriter class.
|
deba@165
|
1472 |
/// \relates GraphWriter
|
deba@165
|
1473 |
template <typename Graph>
|
deba@190
|
1474 |
GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph) {
|
deba@165
|
1475 |
GraphWriter<Graph> tmp(fn, graph);
|
deba@165
|
1476 |
return tmp;
|
deba@165
|
1477 |
}
|
deba@165
|
1478 |
|
kpeter@192
|
1479 |
/// \brief Return a \ref GraphWriter class
|
kpeter@192
|
1480 |
///
|
kpeter@192
|
1481 |
/// This function just returns a \ref GraphWriter class.
|
deba@165
|
1482 |
/// \relates GraphWriter
|
deba@165
|
1483 |
template <typename Graph>
|
deba@190
|
1484 |
GraphWriter<Graph> graphWriter(const char* fn, const Graph& graph) {
|
deba@165
|
1485 |
GraphWriter<Graph> tmp(fn, graph);
|
deba@165
|
1486 |
return tmp;
|
deba@165
|
1487 |
}
|
deba@127
|
1488 |
}
|
deba@127
|
1489 |
|
deba@127
|
1490 |
#endif
|