0
12
1
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2011 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#include <lemon/list_graph.h> |
|
20 |
#include <lemon/lgf_reader.h> |
|
21 |
#include "test_tools.h" |
|
22 |
|
|
23 |
using namespace lemon; |
|
24 |
|
|
25 |
char test_lgf[] = |
|
26 |
"@nodes\n" |
|
27 |
"label\n" |
|
28 |
"0\n" |
|
29 |
"1\n" |
|
30 |
"@arcs\n" |
|
31 |
" label\n" |
|
32 |
"0 1 0\n" |
|
33 |
"1 0 1\n" |
|
34 |
"@attributes\n" |
|
35 |
"source 0\n" |
|
36 |
"target 1\n"; |
|
37 |
|
|
38 |
char test_lgf_nomap[] = |
|
39 |
"@nodes\n" |
|
40 |
"label\n" |
|
41 |
"0\n" |
|
42 |
"1\n" |
|
43 |
"@arcs\n" |
|
44 |
" -\n" |
|
45 |
"0 1\n"; |
|
46 |
|
|
47 |
char test_lgf_bad1[] = |
|
48 |
"@nodes\n" |
|
49 |
"label\n" |
|
50 |
"0\n" |
|
51 |
"1\n" |
|
52 |
"@arcs\n" |
|
53 |
" - another\n" |
|
54 |
"0 1\n"; |
|
55 |
|
|
56 |
char test_lgf_bad2[] = |
|
57 |
"@nodes\n" |
|
58 |
"label\n" |
|
59 |
"0\n" |
|
60 |
"1\n" |
|
61 |
"@arcs\n" |
|
62 |
" label -\n" |
|
63 |
"0 1\n"; |
|
64 |
|
|
65 |
|
|
66 |
int main() |
|
67 |
{ |
|
68 |
{ |
|
69 |
ListDigraph d; |
|
70 |
ListDigraph::Node s,t; |
|
71 |
ListDigraph::ArcMap<int> label(d); |
|
72 |
std::istringstream input(test_lgf); |
|
73 |
digraphReader(d, input). |
|
74 |
node("source", s). |
|
75 |
node("target", t). |
|
76 |
arcMap("label", label). |
|
77 |
run(); |
|
78 |
check(countNodes(d) == 2,"There should be 2 nodes"); |
|
79 |
check(countArcs(d) == 2,"There should be 2 arcs"); |
|
80 |
} |
|
81 |
{ |
|
82 |
ListGraph g; |
|
83 |
ListGraph::Node s,t; |
|
84 |
ListGraph::EdgeMap<int> label(g); |
|
85 |
std::istringstream input(test_lgf); |
|
86 |
graphReader(g, input). |
|
87 |
node("source", s). |
|
88 |
node("target", t). |
|
89 |
edgeMap("label", label). |
|
90 |
run(); |
|
91 |
check(countNodes(g) == 2,"There should be 2 nodes"); |
|
92 |
check(countEdges(g) == 2,"There should be 2 arcs"); |
|
93 |
} |
|
94 |
|
|
95 |
{ |
|
96 |
ListDigraph d; |
|
97 |
std::istringstream input(test_lgf_nomap); |
|
98 |
digraphReader(d, input). |
|
99 |
run(); |
|
100 |
check(countNodes(d) == 2,"There should be 2 nodes"); |
|
101 |
check(countArcs(d) == 1,"There should be 1 arc"); |
|
102 |
} |
|
103 |
{ |
|
104 |
ListGraph g; |
|
105 |
std::istringstream input(test_lgf_nomap); |
|
106 |
graphReader(g, input). |
|
107 |
run(); |
|
108 |
check(countNodes(g) == 2,"There should be 2 nodes"); |
|
109 |
check(countEdges(g) == 1,"There should be 1 edge"); |
|
110 |
} |
|
111 |
|
|
112 |
{ |
|
113 |
ListDigraph d; |
|
114 |
std::istringstream input(test_lgf_bad1); |
|
115 |
bool ok=false; |
|
116 |
try { |
|
117 |
digraphReader(d, input). |
|
118 |
run(); |
|
119 |
} |
|
120 |
catch (FormatError&) |
|
121 |
{ |
|
122 |
ok = true; |
|
123 |
} |
|
124 |
check(ok,"FormatError exception should have occured"); |
|
125 |
} |
|
126 |
{ |
|
127 |
ListGraph g; |
|
128 |
std::istringstream input(test_lgf_bad1); |
|
129 |
bool ok=false; |
|
130 |
try { |
|
131 |
graphReader(g, input). |
|
132 |
run(); |
|
133 |
} |
|
134 |
catch (FormatError&) |
|
135 |
{ |
|
136 |
ok = true; |
|
137 |
} |
|
138 |
check(ok,"FormatError exception should have occured"); |
|
139 |
} |
|
140 |
|
|
141 |
{ |
|
142 |
ListDigraph d; |
|
143 |
std::istringstream input(test_lgf_bad2); |
|
144 |
bool ok=false; |
|
145 |
try { |
|
146 |
digraphReader(d, input). |
|
147 |
run(); |
|
148 |
} |
|
149 |
catch (FormatError&) |
|
150 |
{ |
|
151 |
ok = true; |
|
152 |
} |
|
153 |
check(ok,"FormatError exception should have occured"); |
|
154 |
} |
|
155 |
{ |
|
156 |
ListGraph g; |
|
157 |
std::istringstream input(test_lgf_bad2); |
|
158 |
bool ok=false; |
|
159 |
try { |
|
160 |
graphReader(g, input). |
|
161 |
run(); |
|
162 |
} |
|
163 |
catch (FormatError&) |
|
164 |
{ |
|
165 |
ok = true; |
|
166 |
} |
|
167 |
check(ok,"FormatError exception should have occured"); |
|
168 |
} |
|
169 |
} |
1 | 1 |
The authors of the 1.x series are |
2 | 2 |
|
3 | 3 |
* Balazs Dezso <deba@inf.elte.hu> |
4 | 4 |
* Alpar Juttner <alpar@cs.elte.hu> |
5 | 5 |
* Peter Kovacs <kpeter@inf.elte.hu> |
6 | 6 |
* Akos Ladanyi <ladanyi@tmit.bme.hu> |
7 | 7 |
|
8 | 8 |
For more details on the actual contribution, please visit the history |
9 | 9 |
of the main LEMON source repository: http://lemon.cs.elte.hu/hg/lemon |
10 | 10 |
|
11 | 11 |
Moreover, this version is heavily based on the 0.x series of |
12 | 12 |
LEMON. Here is the list of people who contributed to those versions. |
13 | 13 |
|
14 | 14 |
* Mihaly Barasz <klao@cs.elte.hu> |
15 | 15 |
* Johanna Becker <beckerjc@cs.elte.hu> |
16 | 16 |
* Attila Bernath <athos@cs.elte.hu> |
17 | 17 |
* Balazs Dezso <deba@inf.elte.hu> |
18 | 18 |
* Peter Hegyi <hegyi@tmit.bme.hu> |
19 | 19 |
* Alpar Juttner <alpar@cs.elte.hu> |
20 | 20 |
* Peter Kovacs <kpeter@inf.elte.hu> |
21 | 21 |
* Akos Ladanyi <ladanyi@tmit.bme.hu> |
22 | 22 |
* Marton Makai <marci@cs.elte.hu> |
23 | 23 |
* Jacint Szabo <jacint@cs.elte.hu> |
24 | 24 |
|
25 | 25 |
Again, please visit the history of the old LEMON repository for more |
26 |
details: http://lemon.cs.elte.hu/ |
|
26 |
details: http://lemon.cs.elte.hu/hg/lemon-0.x |
|
... | ... |
No newline at end of file |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
namespace lemon { |
20 | 20 |
/*! |
21 | 21 |
|
22 | 22 |
|
23 | 23 |
|
24 | 24 |
\page lgf-format LEMON Graph Format (LGF) |
25 | 25 |
|
26 | 26 |
The \e LGF is a <em>column oriented</em> |
27 | 27 |
file format for storing graphs and associated data like |
28 | 28 |
node and edge maps. |
29 | 29 |
|
30 | 30 |
Each line with \c '#' first non-whitespace |
31 | 31 |
character is considered as a comment line. |
32 | 32 |
|
33 | 33 |
Otherwise the file consists of sections starting with |
34 | 34 |
a header line. The header lines starts with an \c '@' character followed by the |
35 | 35 |
type of section. The standard section types are \c \@nodes, \c |
36 | 36 |
\@arcs and \c \@edges |
37 | 37 |
and \@attributes. Each header line may also have an optional |
38 | 38 |
\e name, which can be use to distinguish the sections of the same |
39 | 39 |
type. |
40 | 40 |
|
41 | 41 |
The standard sections are column oriented, each line consists of |
42 | 42 |
<em>token</em>s separated by whitespaces. A token can be \e plain or |
43 | 43 |
\e quoted. A plain token is just a sequence of non-whitespace characters, |
44 | 44 |
while a quoted token is a |
45 | 45 |
character sequence surrounded by double quotes, and it can also |
46 | 46 |
contain whitespaces and escape sequences. |
47 | 47 |
|
48 | 48 |
The \c \@nodes section describes a set of nodes and associated |
49 | 49 |
maps. The first is a header line, its columns are the names of the |
50 | 50 |
maps appearing in the following lines. |
51 | 51 |
One of the maps must be called \c |
52 | 52 |
"label", which plays special role in the file. |
53 | 53 |
The following |
54 | 54 |
non-empty lines until the next section describes nodes of the |
55 | 55 |
graph. Each line contains the values of the node maps |
56 | 56 |
associated to the current node. |
57 | 57 |
|
58 | 58 |
\code |
59 | 59 |
@nodes |
60 | 60 |
label coordinates size title |
61 | 61 |
1 (10,20) 10 "First node" |
62 | 62 |
2 (80,80) 8 "Second node" |
63 | 63 |
3 (40,10) 10 "Third node" |
64 | 64 |
\endcode |
65 | 65 |
|
66 |
The \c \@arcs section is very similar to the \c \@nodes section, |
|
67 |
it again starts with a header line describing the names of the maps, |
|
68 |
but the \c "label" map is not obligatory here. The following lines |
|
69 |
describe the arcs. The first two tokens of each line are |
|
70 |
|
|
66 |
The \c \@arcs section is very similar to the \c \@nodes section, it |
|
67 |
again starts with a header line describing the names of the maps, but |
|
68 |
the \c "label" map is not obligatory here. The following lines |
|
69 |
describe the arcs. The first two tokens of each line are the source |
|
70 |
and the target node of the arc, respectively, then come the map |
|
71 | 71 |
values. The source and target tokens must be node labels. |
72 | 72 |
|
73 | 73 |
\code |
74 | 74 |
@arcs |
75 | 75 |
capacity |
76 | 76 |
1 2 16 |
77 | 77 |
1 3 12 |
78 | 78 |
2 3 18 |
79 | 79 |
\endcode |
80 | 80 |
|
81 |
If there is no map in the \c \@arcs section at all, then it must be |
|
82 |
indicated by a sole '-' sign in the first line. |
|
83 |
|
|
84 |
\code |
|
85 |
@arcs |
|
86 |
- |
|
87 |
1 2 |
|
88 |
1 3 |
|
89 |
2 3 |
|
90 |
\endcode |
|
91 |
|
|
81 | 92 |
The \c \@edges is just a synonym of \c \@arcs. The \@arcs section can |
82 | 93 |
also store the edge set of an undirected graph. In such case there is |
83 | 94 |
a conventional method for store arc maps in the file, if two columns |
84 |
|
|
95 |
have the same caption with \c '+' and \c '-' prefix, then these columns |
|
85 | 96 |
can be regarded as the values of an arc map. |
86 | 97 |
|
87 | 98 |
The \c \@attributes section contains key-value pairs, each line |
88 | 99 |
consists of two tokens, an attribute name, and then an attribute |
89 | 100 |
value. The value of the attribute could be also a label value of a |
90 | 101 |
node or an edge, or even an edge label prefixed with \c '+' or \c '-', |
91 | 102 |
which regards to the forward or backward directed arc of the |
92 | 103 |
corresponding edge. |
93 | 104 |
|
94 | 105 |
\code |
95 | 106 |
@attributes |
96 | 107 |
source 1 |
97 | 108 |
target 3 |
98 | 109 |
caption "LEMON test digraph" |
99 | 110 |
\endcode |
100 | 111 |
|
101 | 112 |
The \e LGF can contain extra sections, but there is no restriction on |
102 | 113 |
the format of such sections. |
103 | 114 |
|
104 | 115 |
*/ |
105 | 116 |
} |
106 | 117 |
|
107 | 118 |
// LocalWords: whitespace whitespaces |
1 | 1 |
EXTRA_DIST += \ |
2 | 2 |
lemon/lemon.pc.in \ |
3 |
lemon/lemon.pc.cmake \ |
|
3 | 4 |
lemon/CMakeLists.txt \ |
4 | 5 |
lemon/config.h.cmake |
5 | 6 |
|
6 | 7 |
pkgconfig_DATA += lemon/lemon.pc |
7 | 8 |
|
8 | 9 |
lib_LTLIBRARIES += lemon/libemon.la |
9 | 10 |
|
10 | 11 |
lemon_libemon_la_SOURCES = \ |
11 | 12 |
lemon/arg_parser.cc \ |
12 | 13 |
lemon/base.cc \ |
13 | 14 |
lemon/color.cc \ |
14 | 15 |
lemon/lp_base.cc \ |
15 | 16 |
lemon/lp_skeleton.cc \ |
16 | 17 |
lemon/random.cc \ |
17 | 18 |
lemon/bits/windows.cc |
18 | 19 |
|
19 | 20 |
nodist_lemon_HEADERS = lemon/config.h |
20 | 21 |
|
21 | 22 |
lemon_libemon_la_CXXFLAGS = \ |
22 | 23 |
$(AM_CXXFLAGS) \ |
23 | 24 |
$(GLPK_CFLAGS) \ |
24 | 25 |
$(CPLEX_CFLAGS) \ |
25 | 26 |
$(SOPLEX_CXXFLAGS) \ |
26 | 27 |
$(CLP_CXXFLAGS) \ |
27 | 28 |
$(CBC_CXXFLAGS) |
28 | 29 |
|
29 | 30 |
lemon_libemon_la_LDFLAGS = \ |
30 | 31 |
$(GLPK_LIBS) \ |
31 | 32 |
$(CPLEX_LIBS) \ |
32 | 33 |
$(SOPLEX_LIBS) \ |
33 | 34 |
$(CLP_LIBS) \ |
34 | 35 |
$(CBC_LIBS) |
35 | 36 |
|
36 | 37 |
if HAVE_GLPK |
37 | 38 |
lemon_libemon_la_SOURCES += lemon/glpk.cc |
38 | 39 |
endif |
39 | 40 |
|
40 | 41 |
if HAVE_CPLEX |
41 | 42 |
lemon_libemon_la_SOURCES += lemon/cplex.cc |
42 | 43 |
endif |
43 | 44 |
|
44 | 45 |
if HAVE_SOPLEX |
45 | 46 |
lemon_libemon_la_SOURCES += lemon/soplex.cc |
46 | 47 |
endif |
47 | 48 |
|
48 | 49 |
if HAVE_CLP |
49 | 50 |
lemon_libemon_la_SOURCES += lemon/clp.cc |
50 | 51 |
endif |
51 | 52 |
|
52 | 53 |
if HAVE_CBC |
53 | 54 |
lemon_libemon_la_SOURCES += lemon/cbc.cc |
54 | 55 |
endif |
55 | 56 |
|
56 | 57 |
lemon_HEADERS += \ |
57 | 58 |
lemon/adaptors.h \ |
58 | 59 |
lemon/arg_parser.h \ |
59 | 60 |
lemon/assert.h \ |
60 | 61 |
lemon/bfs.h \ |
61 | 62 |
lemon/bin_heap.h \ |
62 | 63 |
lemon/bucket_heap.h \ |
63 | 64 |
lemon/cbc.h \ |
64 | 65 |
lemon/circulation.h \ |
65 | 66 |
lemon/clp.h \ |
66 | 67 |
lemon/color.h \ |
67 | 68 |
lemon/concept_check.h \ |
68 | 69 |
lemon/connectivity.h \ |
69 | 70 |
lemon/counter.h \ |
70 | 71 |
lemon/core.h \ |
71 | 72 |
lemon/cplex.h \ |
72 | 73 |
lemon/dfs.h \ |
73 | 74 |
lemon/dijkstra.h \ |
74 | 75 |
lemon/dim2.h \ |
75 | 76 |
lemon/dimacs.h \ |
76 | 77 |
lemon/edge_set.h \ |
77 | 78 |
lemon/elevator.h \ |
78 | 79 |
lemon/error.h \ |
79 | 80 |
lemon/euler.h \ |
80 | 81 |
lemon/fib_heap.h \ |
81 | 82 |
lemon/full_graph.h \ |
82 | 83 |
lemon/glpk.h \ |
83 | 84 |
lemon/gomory_hu.h \ |
84 | 85 |
lemon/graph_to_eps.h \ |
85 | 86 |
lemon/grid_graph.h \ |
86 | 87 |
lemon/hypercube_graph.h \ |
87 | 88 |
lemon/kruskal.h \ |
88 | 89 |
lemon/hao_orlin.h \ |
89 | 90 |
lemon/lgf_reader.h \ |
90 | 91 |
lemon/lgf_writer.h \ |
91 | 92 |
lemon/list_graph.h \ |
92 | 93 |
lemon/lp.h \ |
93 | 94 |
lemon/lp_base.h \ |
94 | 95 |
lemon/lp_skeleton.h \ |
95 | 96 |
lemon/list_graph.h \ |
96 | 97 |
lemon/maps.h \ |
97 | 98 |
lemon/matching.h \ |
98 | 99 |
lemon/math.h \ |
99 | 100 |
lemon/min_cost_arborescence.h \ |
100 | 101 |
lemon/nauty_reader.h \ |
101 | 102 |
lemon/network_simplex.h \ |
102 | 103 |
lemon/path.h \ |
103 | 104 |
lemon/preflow.h \ |
104 | 105 |
lemon/radix_heap.h \ |
105 | 106 |
lemon/radix_sort.h \ |
106 | 107 |
lemon/random.h \ |
107 | 108 |
lemon/smart_graph.h \ |
108 | 109 |
lemon/soplex.h \ |
109 | 110 |
lemon/suurballe.h \ |
110 | 111 |
lemon/time_measure.h \ |
111 | 112 |
lemon/tolerance.h \ |
112 | 113 |
lemon/unionfind.h \ |
113 | 114 |
lemon/bits/windows.h |
114 | 115 |
|
115 | 116 |
bits_HEADERS += \ |
116 | 117 |
lemon/bits/alteration_notifier.h \ |
117 | 118 |
lemon/bits/array_map.h \ |
118 | 119 |
lemon/bits/bezier.h \ |
119 | 120 |
lemon/bits/default_map.h \ |
120 | 121 |
lemon/bits/edge_set_extender.h \ |
121 | 122 |
lemon/bits/enable_if.h \ |
122 | 123 |
lemon/bits/graph_adaptor_extender.h \ |
123 | 124 |
lemon/bits/graph_extender.h \ |
124 | 125 |
lemon/bits/map_extender.h \ |
125 | 126 |
lemon/bits/path_dump.h \ |
126 | 127 |
lemon/bits/solver_bits.h \ |
127 | 128 |
lemon/bits/traits.h \ |
128 | 129 |
lemon/bits/variant.h \ |
129 | 130 |
lemon/bits/vector_map.h |
130 | 131 |
|
131 | 132 |
concept_HEADERS += \ |
132 | 133 |
lemon/concepts/digraph.h \ |
133 | 134 |
lemon/concepts/graph.h \ |
134 | 135 |
lemon/concepts/graph_components.h \ |
135 | 136 |
lemon/concepts/heap.h \ |
136 | 137 |
lemon/concepts/maps.h \ |
137 | 138 |
lemon/concepts/path.h |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_PATH_DUMP_H |
20 | 20 |
#define LEMON_BITS_PATH_DUMP_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 | 25 |
namespace lemon { |
26 | 26 |
|
27 | 27 |
template <typename _Digraph, typename _PredMap> |
28 | 28 |
class PredMapPath { |
29 | 29 |
public: |
30 | 30 |
typedef True RevPathTag; |
31 | 31 |
|
32 | 32 |
typedef _Digraph Digraph; |
33 | 33 |
typedef typename Digraph::Arc Arc; |
34 | 34 |
typedef _PredMap PredMap; |
35 | 35 |
|
36 | 36 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
37 | 37 |
typename Digraph::Node _target) |
38 | 38 |
: digraph(_digraph), predMap(_predMap), target(_target) {} |
39 | 39 |
|
40 | 40 |
int length() const { |
41 | 41 |
int len = 0; |
42 | 42 |
typename Digraph::Node node = target; |
43 | 43 |
typename Digraph::Arc arc; |
44 | 44 |
while ((arc = predMap[node]) != INVALID) { |
45 | 45 |
node = digraph.source(arc); |
46 | 46 |
++len; |
47 | 47 |
} |
48 | 48 |
return len; |
49 | 49 |
} |
50 | 50 |
|
51 | 51 |
bool empty() const { |
52 |
return predMap[target] |
|
52 |
return predMap[target] == INVALID; |
|
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
class RevArcIt { |
56 | 56 |
public: |
57 | 57 |
RevArcIt() {} |
58 | 58 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
59 | 59 |
RevArcIt(const PredMapPath& _path) |
60 | 60 |
: path(&_path), current(_path.target) { |
61 | 61 |
if (path->predMap[current] == INVALID) current = INVALID; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
operator const typename Digraph::Arc() const { |
65 | 65 |
return path->predMap[current]; |
66 | 66 |
} |
67 | 67 |
|
68 | 68 |
RevArcIt& operator++() { |
69 | 69 |
current = path->digraph.source(path->predMap[current]); |
70 | 70 |
if (path->predMap[current] == INVALID) current = INVALID; |
71 | 71 |
return *this; |
72 | 72 |
} |
73 | 73 |
|
74 | 74 |
bool operator==(const RevArcIt& e) const { |
75 | 75 |
return current == e.current; |
76 | 76 |
} |
77 | 77 |
|
78 | 78 |
bool operator!=(const RevArcIt& e) const { |
79 | 79 |
return current != e.current; |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
bool operator<(const RevArcIt& e) const { |
83 | 83 |
return current < e.current; |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
private: |
87 | 87 |
const PredMapPath* path; |
88 | 88 |
typename Digraph::Node current; |
89 | 89 |
}; |
90 | 90 |
|
91 | 91 |
private: |
92 | 92 |
const Digraph& digraph; |
93 | 93 |
const PredMap& predMap; |
94 | 94 |
typename Digraph::Node target; |
95 | 95 |
}; |
96 | 96 |
|
97 | 97 |
|
98 | 98 |
template <typename _Digraph, typename _PredMatrixMap> |
99 | 99 |
class PredMatrixMapPath { |
100 | 100 |
public: |
101 | 101 |
typedef True RevPathTag; |
102 | 102 |
|
103 | 103 |
typedef _Digraph Digraph; |
104 | 104 |
typedef typename Digraph::Arc Arc; |
105 | 105 |
typedef _PredMatrixMap PredMatrixMap; |
106 | 106 |
|
107 | 107 |
PredMatrixMapPath(const Digraph& _digraph, |
108 | 108 |
const PredMatrixMap& _predMatrixMap, |
109 | 109 |
typename Digraph::Node _source, |
110 | 110 |
typename Digraph::Node _target) |
111 | 111 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
112 | 112 |
source(_source), target(_target) {} |
113 | 113 |
|
114 | 114 |
int length() const { |
115 | 115 |
int len = 0; |
116 | 116 |
typename Digraph::Node node = target; |
117 | 117 |
typename Digraph::Arc arc; |
118 | 118 |
while ((arc = predMatrixMap(source, node)) != INVALID) { |
119 | 119 |
node = digraph.source(arc); |
120 | 120 |
++len; |
121 | 121 |
} |
122 | 122 |
return len; |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
bool empty() const { |
126 |
return source |
|
126 |
return predMatrixMap(source, target) == INVALID; |
|
127 | 127 |
} |
128 | 128 |
|
129 | 129 |
class RevArcIt { |
130 | 130 |
public: |
131 | 131 |
RevArcIt() {} |
132 | 132 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
133 | 133 |
RevArcIt(const PredMatrixMapPath& _path) |
134 | 134 |
: path(&_path), current(_path.target) { |
135 | 135 |
if (path->predMatrixMap(path->source, current) == INVALID) |
136 | 136 |
current = INVALID; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
operator const typename Digraph::Arc() const { |
140 | 140 |
return path->predMatrixMap(path->source, current); |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
RevArcIt& operator++() { |
144 | 144 |
current = |
145 | 145 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
146 | 146 |
if (path->predMatrixMap(path->source, current) == INVALID) |
147 | 147 |
current = INVALID; |
148 | 148 |
return *this; |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
bool operator==(const RevArcIt& e) const { |
152 | 152 |
return current == e.current; |
153 | 153 |
} |
154 | 154 |
|
155 | 155 |
bool operator!=(const RevArcIt& e) const { |
156 | 156 |
return current != e.current; |
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
bool operator<(const RevArcIt& e) const { |
160 | 160 |
return current < e.current; |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
private: |
164 | 164 |
const PredMatrixMapPath* path; |
165 | 165 |
typename Digraph::Node current; |
166 | 166 |
}; |
167 | 167 |
|
168 | 168 |
private: |
169 | 169 |
const Digraph& digraph; |
170 | 170 |
const PredMatrixMap& predMatrixMap; |
171 | 171 |
typename Digraph::Node source; |
172 | 172 |
typename Digraph::Node target; |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CORE_H |
20 | 20 |
#define LEMON_CORE_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/config.h> |
26 | 26 |
#include <lemon/bits/enable_if.h> |
27 | 27 |
#include <lemon/bits/traits.h> |
28 | 28 |
#include <lemon/assert.h> |
29 | 29 |
|
30 | 30 |
// Disable the following warnings when compiling with MSVC: |
31 | 31 |
// C4250: 'class1' : inherits 'class2::member' via dominance |
32 | 32 |
// C4355: 'this' : used in base member initializer list |
33 | 33 |
// C4503: 'function' : decorated name length exceeded, name was truncated |
34 | 34 |
// C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning) |
35 | 35 |
// C4996: 'function': was declared deprecated |
36 | 36 |
#ifdef _MSC_VER |
37 | 37 |
#pragma warning( disable : 4250 4355 4503 4800 4996 ) |
38 | 38 |
#endif |
39 | 39 |
|
40 | 40 |
///\file |
41 | 41 |
///\brief LEMON core utilities. |
42 | 42 |
/// |
43 | 43 |
///This header file contains core utilities for LEMON. |
44 | 44 |
///It is automatically included by all graph types, therefore it usually |
45 | 45 |
///do not have to be included directly. |
46 | 46 |
|
47 | 47 |
namespace lemon { |
48 | 48 |
|
49 | 49 |
/// \brief Dummy type to make it easier to create invalid iterators. |
50 | 50 |
/// |
51 | 51 |
/// Dummy type to make it easier to create invalid iterators. |
52 | 52 |
/// See \ref INVALID for the usage. |
53 | 53 |
struct Invalid { |
54 | 54 |
public: |
55 | 55 |
bool operator==(Invalid) { return true; } |
56 | 56 |
bool operator!=(Invalid) { return false; } |
57 | 57 |
bool operator< (Invalid) { return false; } |
58 | 58 |
}; |
59 | 59 |
|
60 | 60 |
/// \brief Invalid iterators. |
61 | 61 |
/// |
62 | 62 |
/// \ref Invalid is a global type that converts to each iterator |
63 | 63 |
/// in such a way that the value of the target iterator will be invalid. |
64 | 64 |
#ifdef LEMON_ONLY_TEMPLATES |
65 | 65 |
const Invalid INVALID = Invalid(); |
66 | 66 |
#else |
67 | 67 |
extern const Invalid INVALID; |
68 | 68 |
#endif |
69 | 69 |
|
70 | 70 |
/// \addtogroup gutils |
71 | 71 |
/// @{ |
72 | 72 |
|
73 | 73 |
///Create convenience typedefs for the digraph types and iterators |
74 | 74 |
|
75 | 75 |
///This \c \#define creates convenient type definitions for the following |
76 | 76 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
77 | 77 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
78 | 78 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
79 | 79 |
/// |
80 | 80 |
///\note If the graph type is a dependent type, ie. the graph type depend |
81 | 81 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
82 | 82 |
///macro. |
83 | 83 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
84 | 84 |
typedef Digraph::Node Node; \ |
85 | 85 |
typedef Digraph::NodeIt NodeIt; \ |
86 | 86 |
typedef Digraph::Arc Arc; \ |
87 | 87 |
typedef Digraph::ArcIt ArcIt; \ |
88 | 88 |
typedef Digraph::InArcIt InArcIt; \ |
89 | 89 |
typedef Digraph::OutArcIt OutArcIt; \ |
90 | 90 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
91 | 91 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
92 | 92 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
93 | 93 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
94 | 94 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
95 | 95 |
typedef Digraph::ArcMap<double> DoubleArcMap |
96 | 96 |
|
97 | 97 |
///Create convenience typedefs for the digraph types and iterators |
98 | 98 |
|
99 | 99 |
///\see DIGRAPH_TYPEDEFS |
100 | 100 |
/// |
101 | 101 |
///\note Use this macro, if the graph type is a dependent type, |
102 | 102 |
///ie. the graph type depend on a template parameter. |
103 | 103 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
104 | 104 |
typedef typename Digraph::Node Node; \ |
105 | 105 |
typedef typename Digraph::NodeIt NodeIt; \ |
106 | 106 |
typedef typename Digraph::Arc Arc; \ |
107 | 107 |
typedef typename Digraph::ArcIt ArcIt; \ |
108 | 108 |
typedef typename Digraph::InArcIt InArcIt; \ |
109 | 109 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
110 | 110 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
111 | 111 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
112 | 112 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
113 | 113 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
114 | 114 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
115 | 115 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
116 | 116 |
|
117 | 117 |
///Create convenience typedefs for the graph types and iterators |
118 | 118 |
|
119 | 119 |
///This \c \#define creates the same convenient type definitions as defined |
120 | 120 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
121 | 121 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
122 | 122 |
///\c DoubleEdgeMap. |
123 | 123 |
/// |
124 | 124 |
///\note If the graph type is a dependent type, ie. the graph type depend |
125 | 125 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
126 | 126 |
///macro. |
127 | 127 |
#define GRAPH_TYPEDEFS(Graph) \ |
128 | 128 |
DIGRAPH_TYPEDEFS(Graph); \ |
129 | 129 |
typedef Graph::Edge Edge; \ |
130 | 130 |
typedef Graph::EdgeIt EdgeIt; \ |
131 | 131 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
132 | 132 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
133 | 133 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
134 | 134 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
135 | 135 |
|
136 | 136 |
///Create convenience typedefs for the graph types and iterators |
137 | 137 |
|
138 | 138 |
///\see GRAPH_TYPEDEFS |
139 | 139 |
/// |
140 | 140 |
///\note Use this macro, if the graph type is a dependent type, |
141 | 141 |
///ie. the graph type depend on a template parameter. |
142 | 142 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
143 | 143 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
144 | 144 |
typedef typename Graph::Edge Edge; \ |
145 | 145 |
typedef typename Graph::EdgeIt EdgeIt; \ |
146 | 146 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
147 | 147 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
148 | 148 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
149 | 149 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
150 | 150 |
|
151 | 151 |
/// \brief Function to count the items in a graph. |
152 | 152 |
/// |
153 | 153 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
154 | 154 |
/// The complexity of the function is linear because |
155 | 155 |
/// it iterates on all of the items. |
156 | 156 |
template <typename Graph, typename Item> |
157 | 157 |
inline int countItems(const Graph& g) { |
158 | 158 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
159 | 159 |
int num = 0; |
160 | 160 |
for (ItemIt it(g); it != INVALID; ++it) { |
161 | 161 |
++num; |
162 | 162 |
} |
163 | 163 |
return num; |
164 | 164 |
} |
165 | 165 |
|
166 | 166 |
// Node counting: |
167 | 167 |
|
168 | 168 |
namespace _core_bits { |
169 | 169 |
|
170 | 170 |
template <typename Graph, typename Enable = void> |
171 | 171 |
struct CountNodesSelector { |
172 | 172 |
static int count(const Graph &g) { |
173 | 173 |
return countItems<Graph, typename Graph::Node>(g); |
174 | 174 |
} |
175 | 175 |
}; |
176 | 176 |
|
177 | 177 |
template <typename Graph> |
178 | 178 |
struct CountNodesSelector< |
179 | 179 |
Graph, typename |
180 | 180 |
enable_if<typename Graph::NodeNumTag, void>::type> |
181 | 181 |
{ |
182 | 182 |
static int count(const Graph &g) { |
183 | 183 |
return g.nodeNum(); |
184 | 184 |
} |
185 | 185 |
}; |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
/// \brief Function to count the nodes in the graph. |
189 | 189 |
/// |
190 | 190 |
/// This function counts the nodes in the graph. |
191 | 191 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
192 | 192 |
/// graph structures it is specialized to run in <em>O</em>(1). |
193 | 193 |
/// |
194 | 194 |
/// \note If the graph contains a \c nodeNum() member function and a |
195 | 195 |
/// \c NodeNumTag tag then this function calls directly the member |
196 | 196 |
/// function to query the cardinality of the node set. |
197 | 197 |
template <typename Graph> |
198 | 198 |
inline int countNodes(const Graph& g) { |
199 | 199 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
200 | 200 |
} |
201 | 201 |
|
202 | 202 |
// Arc counting: |
203 | 203 |
|
204 | 204 |
namespace _core_bits { |
205 | 205 |
|
206 | 206 |
template <typename Graph, typename Enable = void> |
207 | 207 |
struct CountArcsSelector { |
208 | 208 |
static int count(const Graph &g) { |
209 | 209 |
return countItems<Graph, typename Graph::Arc>(g); |
210 | 210 |
} |
211 | 211 |
}; |
212 | 212 |
|
213 | 213 |
template <typename Graph> |
214 | 214 |
struct CountArcsSelector< |
215 | 215 |
Graph, |
216 | 216 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
217 | 217 |
{ |
218 | 218 |
static int count(const Graph &g) { |
219 | 219 |
return g.arcNum(); |
220 | 220 |
} |
221 | 221 |
}; |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
/// \brief Function to count the arcs in the graph. |
225 | 225 |
/// |
226 | 226 |
/// This function counts the arcs in the graph. |
227 | 227 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
228 | 228 |
/// graph structures it is specialized to run in <em>O</em>(1). |
229 | 229 |
/// |
230 | 230 |
/// \note If the graph contains a \c arcNum() member function and a |
231 | 231 |
/// \c ArcNumTag tag then this function calls directly the member |
232 | 232 |
/// function to query the cardinality of the arc set. |
233 | 233 |
template <typename Graph> |
234 | 234 |
inline int countArcs(const Graph& g) { |
235 | 235 |
return _core_bits::CountArcsSelector<Graph>::count(g); |
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
// Edge counting: |
239 | 239 |
|
240 | 240 |
namespace _core_bits { |
241 | 241 |
|
242 | 242 |
template <typename Graph, typename Enable = void> |
243 | 243 |
struct CountEdgesSelector { |
244 | 244 |
static int count(const Graph &g) { |
245 | 245 |
return countItems<Graph, typename Graph::Edge>(g); |
246 | 246 |
} |
247 | 247 |
}; |
248 | 248 |
|
249 | 249 |
template <typename Graph> |
250 | 250 |
struct CountEdgesSelector< |
251 | 251 |
Graph, |
252 | 252 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
253 | 253 |
{ |
254 | 254 |
static int count(const Graph &g) { |
255 | 255 |
return g.edgeNum(); |
256 | 256 |
} |
257 | 257 |
}; |
258 | 258 |
} |
259 | 259 |
|
260 | 260 |
/// \brief Function to count the edges in the graph. |
261 | 261 |
/// |
262 | 262 |
/// This function counts the edges in the graph. |
263 | 263 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
264 | 264 |
/// graph structures it is specialized to run in <em>O</em>(1). |
265 | 265 |
/// |
266 | 266 |
/// \note If the graph contains a \c edgeNum() member function and a |
267 | 267 |
/// \c EdgeNumTag tag then this function calls directly the member |
268 | 268 |
/// function to query the cardinality of the edge set. |
269 | 269 |
template <typename Graph> |
270 | 270 |
inline int countEdges(const Graph& g) { |
271 | 271 |
return _core_bits::CountEdgesSelector<Graph>::count(g); |
272 | 272 |
|
273 | 273 |
} |
274 | 274 |
|
275 | 275 |
|
276 | 276 |
template <typename Graph, typename DegIt> |
277 | 277 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
278 | 278 |
int num = 0; |
279 | 279 |
for (DegIt it(_g, _n); it != INVALID; ++it) { |
280 | 280 |
++num; |
281 | 281 |
} |
282 | 282 |
return num; |
283 | 283 |
} |
284 | 284 |
|
285 | 285 |
/// \brief Function to count the number of the out-arcs from node \c n. |
286 | 286 |
/// |
287 | 287 |
/// This function counts the number of the out-arcs from node \c n |
288 | 288 |
/// in the graph \c g. |
289 | 289 |
template <typename Graph> |
290 | 290 |
inline int countOutArcs(const Graph& g, const typename Graph::Node& n) { |
291 | 291 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n); |
292 | 292 |
} |
293 | 293 |
|
294 | 294 |
/// \brief Function to count the number of the in-arcs to node \c n. |
295 | 295 |
/// |
296 | 296 |
/// This function counts the number of the in-arcs to node \c n |
297 | 297 |
/// in the graph \c g. |
298 | 298 |
template <typename Graph> |
299 | 299 |
inline int countInArcs(const Graph& g, const typename Graph::Node& n) { |
300 | 300 |
return countNodeDegree<Graph, typename Graph::InArcIt>(g, n); |
301 | 301 |
} |
302 | 302 |
|
303 | 303 |
/// \brief Function to count the number of the inc-edges to node \c n. |
304 | 304 |
/// |
305 | 305 |
/// This function counts the number of the inc-edges to node \c n |
306 | 306 |
/// in the undirected graph \c g. |
307 | 307 |
template <typename Graph> |
308 | 308 |
inline int countIncEdges(const Graph& g, const typename Graph::Node& n) { |
309 | 309 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n); |
310 | 310 |
} |
311 | 311 |
|
312 | 312 |
namespace _core_bits { |
313 | 313 |
|
314 | 314 |
template <typename Digraph, typename Item, typename RefMap> |
315 | 315 |
class MapCopyBase { |
316 | 316 |
public: |
317 | 317 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
318 | 318 |
|
319 | 319 |
virtual ~MapCopyBase() {} |
320 | 320 |
}; |
321 | 321 |
|
322 | 322 |
template <typename Digraph, typename Item, typename RefMap, |
323 | 323 |
typename FromMap, typename ToMap> |
324 | 324 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
325 | 325 |
public: |
326 | 326 |
|
327 | 327 |
MapCopy(const FromMap& map, ToMap& tmap) |
328 | 328 |
: _map(map), _tmap(tmap) {} |
329 | 329 |
|
330 | 330 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
331 | 331 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
332 | 332 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
333 | 333 |
_tmap.set(refMap[it], _map[it]); |
334 | 334 |
} |
335 | 335 |
} |
336 | 336 |
|
337 | 337 |
private: |
338 | 338 |
const FromMap& _map; |
339 | 339 |
ToMap& _tmap; |
340 | 340 |
}; |
341 | 341 |
|
342 | 342 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
343 | 343 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
344 | 344 |
public: |
345 | 345 |
|
346 | 346 |
ItemCopy(const Item& item, It& it) : _item(item), _it(it) {} |
347 | 347 |
|
348 | 348 |
virtual void copy(const Digraph&, const RefMap& refMap) { |
349 | 349 |
_it = refMap[_item]; |
350 | 350 |
} |
351 | 351 |
|
352 | 352 |
private: |
353 | 353 |
Item _item; |
354 | 354 |
It& _it; |
355 | 355 |
}; |
356 | 356 |
|
357 | 357 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
358 | 358 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
359 | 359 |
public: |
360 | 360 |
|
361 | 361 |
RefCopy(Ref& map) : _map(map) {} |
362 | 362 |
|
363 | 363 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
364 | 364 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
365 | 365 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
366 | 366 |
_map.set(it, refMap[it]); |
367 | 367 |
} |
368 | 368 |
} |
369 | 369 |
|
370 | 370 |
private: |
371 | 371 |
Ref& _map; |
372 | 372 |
}; |
373 | 373 |
|
374 | 374 |
template <typename Digraph, typename Item, typename RefMap, |
375 | 375 |
typename CrossRef> |
376 | 376 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
377 | 377 |
public: |
378 | 378 |
|
379 | 379 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
380 | 380 |
|
381 | 381 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
382 | 382 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
383 | 383 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
384 | 384 |
_cmap.set(refMap[it], it); |
385 | 385 |
} |
386 | 386 |
} |
387 | 387 |
|
388 | 388 |
private: |
389 | 389 |
CrossRef& _cmap; |
390 | 390 |
}; |
391 | 391 |
|
392 | 392 |
template <typename Digraph, typename Enable = void> |
393 | 393 |
struct DigraphCopySelector { |
394 | 394 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
395 | 395 |
static void copy(const From& from, Digraph &to, |
396 | 396 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
397 |
to.clear(); |
|
397 | 398 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
398 | 399 |
nodeRefMap[it] = to.addNode(); |
399 | 400 |
} |
400 | 401 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
401 | 402 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
402 | 403 |
nodeRefMap[from.target(it)]); |
403 | 404 |
} |
404 | 405 |
} |
405 | 406 |
}; |
406 | 407 |
|
407 | 408 |
template <typename Digraph> |
408 | 409 |
struct DigraphCopySelector< |
409 | 410 |
Digraph, |
410 | 411 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
411 | 412 |
{ |
412 | 413 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
413 | 414 |
static void copy(const From& from, Digraph &to, |
414 | 415 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
415 | 416 |
to.build(from, nodeRefMap, arcRefMap); |
416 | 417 |
} |
417 | 418 |
}; |
418 | 419 |
|
419 | 420 |
template <typename Graph, typename Enable = void> |
420 | 421 |
struct GraphCopySelector { |
421 | 422 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
422 | 423 |
static void copy(const From& from, Graph &to, |
423 | 424 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
425 |
to.clear(); |
|
424 | 426 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
425 | 427 |
nodeRefMap[it] = to.addNode(); |
426 | 428 |
} |
427 | 429 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
428 | 430 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
429 | 431 |
nodeRefMap[from.v(it)]); |
430 | 432 |
} |
431 | 433 |
} |
432 | 434 |
}; |
433 | 435 |
|
434 | 436 |
template <typename Graph> |
435 | 437 |
struct GraphCopySelector< |
436 | 438 |
Graph, |
437 | 439 |
typename enable_if<typename Graph::BuildTag, void>::type> |
438 | 440 |
{ |
439 | 441 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
440 | 442 |
static void copy(const From& from, Graph &to, |
441 | 443 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
442 | 444 |
to.build(from, nodeRefMap, edgeRefMap); |
443 | 445 |
} |
444 | 446 |
}; |
445 | 447 |
|
446 | 448 |
} |
447 | 449 |
|
448 | 450 |
/// \brief Class to copy a digraph. |
449 | 451 |
/// |
450 | 452 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
451 | 453 |
/// simplest way of using it is through the \c digraphCopy() function. |
452 | 454 |
/// |
453 | 455 |
/// This class not only make a copy of a digraph, but it can create |
454 | 456 |
/// references and cross references between the nodes and arcs of |
455 | 457 |
/// the two digraphs, and it can copy maps to use with the newly created |
456 | 458 |
/// digraph. |
457 | 459 |
/// |
458 | 460 |
/// To make a copy from a digraph, first an instance of DigraphCopy |
459 | 461 |
/// should be created, then the data belongs to the digraph should |
460 | 462 |
/// assigned to copy. In the end, the \c run() member should be |
461 | 463 |
/// called. |
462 | 464 |
/// |
463 | 465 |
/// The next code copies a digraph with several data: |
464 | 466 |
///\code |
465 | 467 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
466 | 468 |
/// // Create references for the nodes |
467 | 469 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
468 | 470 |
/// cg.nodeRef(nr); |
469 | 471 |
/// // Create cross references (inverse) for the arcs |
470 | 472 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
471 | 473 |
/// cg.arcCrossRef(acr); |
472 | 474 |
/// // Copy an arc map |
473 | 475 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
474 | 476 |
/// NewGraph::ArcMap<double> namap(new_graph); |
475 | 477 |
/// cg.arcMap(oamap, namap); |
476 | 478 |
/// // Copy a node |
477 | 479 |
/// OrigGraph::Node on; |
478 | 480 |
/// NewGraph::Node nn; |
479 | 481 |
/// cg.node(on, nn); |
480 | 482 |
/// // Execute copying |
481 | 483 |
/// cg.run(); |
482 | 484 |
///\endcode |
483 | 485 |
template <typename From, typename To> |
484 | 486 |
class DigraphCopy { |
485 | 487 |
private: |
486 | 488 |
|
487 | 489 |
typedef typename From::Node Node; |
488 | 490 |
typedef typename From::NodeIt NodeIt; |
489 | 491 |
typedef typename From::Arc Arc; |
490 | 492 |
typedef typename From::ArcIt ArcIt; |
491 | 493 |
|
492 | 494 |
typedef typename To::Node TNode; |
493 | 495 |
typedef typename To::Arc TArc; |
494 | 496 |
|
495 | 497 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
496 | 498 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
497 | 499 |
|
498 | 500 |
public: |
499 | 501 |
|
500 | 502 |
/// \brief Constructor of DigraphCopy. |
501 | 503 |
/// |
502 | 504 |
/// Constructor of DigraphCopy for copying the content of the |
503 | 505 |
/// \c from digraph into the \c to digraph. |
504 | 506 |
DigraphCopy(const From& from, To& to) |
505 | 507 |
: _from(from), _to(to) {} |
506 | 508 |
|
507 | 509 |
/// \brief Destructor of DigraphCopy |
508 | 510 |
/// |
509 | 511 |
/// Destructor of DigraphCopy. |
510 | 512 |
~DigraphCopy() { |
511 | 513 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
512 | 514 |
delete _node_maps[i]; |
513 | 515 |
} |
514 | 516 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
515 | 517 |
delete _arc_maps[i]; |
516 | 518 |
} |
517 | 519 |
|
518 | 520 |
} |
519 | 521 |
|
520 | 522 |
/// \brief Copy the node references into the given map. |
521 | 523 |
/// |
522 | 524 |
/// This function copies the node references into the given map. |
523 | 525 |
/// The parameter should be a map, whose key type is the Node type of |
524 | 526 |
/// the source digraph, while the value type is the Node type of the |
525 | 527 |
/// destination digraph. |
526 | 528 |
template <typename NodeRef> |
527 | 529 |
DigraphCopy& nodeRef(NodeRef& map) { |
528 | 530 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
529 | 531 |
NodeRefMap, NodeRef>(map)); |
530 | 532 |
return *this; |
531 | 533 |
} |
532 | 534 |
|
533 | 535 |
/// \brief Copy the node cross references into the given map. |
534 | 536 |
/// |
535 | 537 |
/// This function copies the node cross references (reverse references) |
536 | 538 |
/// into the given map. The parameter should be a map, whose key type |
537 | 539 |
/// is the Node type of the destination digraph, while the value type is |
538 | 540 |
/// the Node type of the source digraph. |
539 | 541 |
template <typename NodeCrossRef> |
540 | 542 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
541 | 543 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
542 | 544 |
NodeRefMap, NodeCrossRef>(map)); |
543 | 545 |
return *this; |
544 | 546 |
} |
545 | 547 |
|
546 | 548 |
/// \brief Make a copy of the given node map. |
547 | 549 |
/// |
548 | 550 |
/// This function makes a copy of the given node map for the newly |
549 | 551 |
/// created digraph. |
550 | 552 |
/// The key type of the new map \c tmap should be the Node type of the |
551 | 553 |
/// destination digraph, and the key type of the original map \c map |
552 | 554 |
/// should be the Node type of the source digraph. |
553 | 555 |
template <typename FromMap, typename ToMap> |
554 | 556 |
DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
555 | 557 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
556 | 558 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
557 | 559 |
return *this; |
558 | 560 |
} |
559 | 561 |
|
560 | 562 |
/// \brief Make a copy of the given node. |
561 | 563 |
/// |
562 | 564 |
/// This function makes a copy of the given node. |
563 | 565 |
DigraphCopy& node(const Node& node, TNode& tnode) { |
564 | 566 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
565 | 567 |
NodeRefMap, TNode>(node, tnode)); |
566 | 568 |
return *this; |
567 | 569 |
} |
568 | 570 |
|
569 | 571 |
/// \brief Copy the arc references into the given map. |
570 | 572 |
/// |
571 | 573 |
/// This function copies the arc references into the given map. |
572 | 574 |
/// The parameter should be a map, whose key type is the Arc type of |
573 | 575 |
/// the source digraph, while the value type is the Arc type of the |
574 | 576 |
/// destination digraph. |
575 | 577 |
template <typename ArcRef> |
576 | 578 |
DigraphCopy& arcRef(ArcRef& map) { |
577 | 579 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
578 | 580 |
ArcRefMap, ArcRef>(map)); |
579 | 581 |
return *this; |
580 | 582 |
} |
581 | 583 |
|
582 | 584 |
/// \brief Copy the arc cross references into the given map. |
583 | 585 |
/// |
584 | 586 |
/// This function copies the arc cross references (reverse references) |
585 | 587 |
/// into the given map. The parameter should be a map, whose key type |
586 | 588 |
/// is the Arc type of the destination digraph, while the value type is |
587 | 589 |
/// the Arc type of the source digraph. |
588 | 590 |
template <typename ArcCrossRef> |
589 | 591 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) { |
590 | 592 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
591 | 593 |
ArcRefMap, ArcCrossRef>(map)); |
592 | 594 |
return *this; |
593 | 595 |
} |
594 | 596 |
|
595 | 597 |
/// \brief Make a copy of the given arc map. |
596 | 598 |
/// |
597 | 599 |
/// This function makes a copy of the given arc map for the newly |
598 | 600 |
/// created digraph. |
599 | 601 |
/// The key type of the new map \c tmap should be the Arc type of the |
600 | 602 |
/// destination digraph, and the key type of the original map \c map |
601 | 603 |
/// should be the Arc type of the source digraph. |
602 | 604 |
template <typename FromMap, typename ToMap> |
603 | 605 |
DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
604 | 606 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
605 | 607 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
606 | 608 |
return *this; |
607 | 609 |
} |
608 | 610 |
|
609 | 611 |
/// \brief Make a copy of the given arc. |
610 | 612 |
/// |
611 | 613 |
/// This function makes a copy of the given arc. |
612 | 614 |
DigraphCopy& arc(const Arc& arc, TArc& tarc) { |
613 | 615 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
614 | 616 |
ArcRefMap, TArc>(arc, tarc)); |
615 | 617 |
return *this; |
616 | 618 |
} |
617 | 619 |
|
618 | 620 |
/// \brief Execute copying. |
619 | 621 |
/// |
620 | 622 |
/// This function executes the copying of the digraph along with the |
621 | 623 |
/// copying of the assigned data. |
622 | 624 |
void run() { |
623 | 625 |
NodeRefMap nodeRefMap(_from); |
624 | 626 |
ArcRefMap arcRefMap(_from); |
625 | 627 |
_core_bits::DigraphCopySelector<To>:: |
626 | 628 |
copy(_from, _to, nodeRefMap, arcRefMap); |
627 | 629 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
628 | 630 |
_node_maps[i]->copy(_from, nodeRefMap); |
629 | 631 |
} |
630 | 632 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
631 | 633 |
_arc_maps[i]->copy(_from, arcRefMap); |
632 | 634 |
} |
633 | 635 |
} |
634 | 636 |
|
635 | 637 |
protected: |
636 | 638 |
|
637 | 639 |
const From& _from; |
638 | 640 |
To& _to; |
639 | 641 |
|
640 | 642 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
641 | 643 |
_node_maps; |
642 | 644 |
|
643 | 645 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
644 | 646 |
_arc_maps; |
645 | 647 |
|
646 | 648 |
}; |
647 | 649 |
|
648 | 650 |
/// \brief Copy a digraph to another digraph. |
649 | 651 |
/// |
650 | 652 |
/// This function copies a digraph to another digraph. |
651 | 653 |
/// The complete usage of it is detailed in the DigraphCopy class, but |
652 | 654 |
/// a short example shows a basic work: |
653 | 655 |
///\code |
654 | 656 |
/// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run(); |
655 | 657 |
///\endcode |
656 | 658 |
/// |
657 | 659 |
/// After the copy the \c nr map will contain the mapping from the |
658 | 660 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
659 | 661 |
/// \c acr will contain the mapping from the arcs of the \c to digraph |
660 | 662 |
/// to the arcs of the \c from digraph. |
661 | 663 |
/// |
662 | 664 |
/// \see DigraphCopy |
663 | 665 |
template <typename From, typename To> |
664 | 666 |
DigraphCopy<From, To> digraphCopy(const From& from, To& to) { |
665 | 667 |
return DigraphCopy<From, To>(from, to); |
666 | 668 |
} |
667 | 669 |
|
668 | 670 |
/// \brief Class to copy a graph. |
669 | 671 |
/// |
670 | 672 |
/// Class to copy a graph to another graph (duplicate a graph). The |
671 | 673 |
/// simplest way of using it is through the \c graphCopy() function. |
672 | 674 |
/// |
673 | 675 |
/// This class not only make a copy of a graph, but it can create |
674 | 676 |
/// references and cross references between the nodes, edges and arcs of |
675 | 677 |
/// the two graphs, and it can copy maps for using with the newly created |
676 | 678 |
/// graph. |
677 | 679 |
/// |
678 | 680 |
/// To make a copy from a graph, first an instance of GraphCopy |
679 | 681 |
/// should be created, then the data belongs to the graph should |
680 | 682 |
/// assigned to copy. In the end, the \c run() member should be |
681 | 683 |
/// called. |
682 | 684 |
/// |
683 | 685 |
/// The next code copies a graph with several data: |
684 | 686 |
///\code |
685 | 687 |
/// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
686 | 688 |
/// // Create references for the nodes |
687 | 689 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
688 | 690 |
/// cg.nodeRef(nr); |
689 | 691 |
/// // Create cross references (inverse) for the edges |
690 | 692 |
/// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph); |
691 | 693 |
/// cg.edgeCrossRef(ecr); |
692 | 694 |
/// // Copy an edge map |
693 | 695 |
/// OrigGraph::EdgeMap<double> oemap(orig_graph); |
694 | 696 |
/// NewGraph::EdgeMap<double> nemap(new_graph); |
695 | 697 |
/// cg.edgeMap(oemap, nemap); |
696 | 698 |
/// // Copy a node |
697 | 699 |
/// OrigGraph::Node on; |
698 | 700 |
/// NewGraph::Node nn; |
699 | 701 |
/// cg.node(on, nn); |
700 | 702 |
/// // Execute copying |
701 | 703 |
/// cg.run(); |
702 | 704 |
///\endcode |
703 | 705 |
template <typename From, typename To> |
704 | 706 |
class GraphCopy { |
705 | 707 |
private: |
706 | 708 |
|
707 | 709 |
typedef typename From::Node Node; |
708 | 710 |
typedef typename From::NodeIt NodeIt; |
709 | 711 |
typedef typename From::Arc Arc; |
710 | 712 |
typedef typename From::ArcIt ArcIt; |
711 | 713 |
typedef typename From::Edge Edge; |
712 | 714 |
typedef typename From::EdgeIt EdgeIt; |
713 | 715 |
|
714 | 716 |
typedef typename To::Node TNode; |
715 | 717 |
typedef typename To::Arc TArc; |
716 | 718 |
typedef typename To::Edge TEdge; |
717 | 719 |
|
718 | 720 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
719 | 721 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
720 | 722 |
|
721 | 723 |
struct ArcRefMap { |
722 | 724 |
ArcRefMap(const From& from, const To& to, |
723 | 725 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
724 | 726 |
: _from(from), _to(to), |
725 | 727 |
_edge_ref(edge_ref), _node_ref(node_ref) {} |
726 | 728 |
|
727 | 729 |
typedef typename From::Arc Key; |
728 | 730 |
typedef typename To::Arc Value; |
729 | 731 |
|
730 | 732 |
Value operator[](const Key& key) const { |
731 | 733 |
bool forward = _from.u(key) != _from.v(key) ? |
732 | 734 |
_node_ref[_from.source(key)] == |
733 | 735 |
_to.source(_to.direct(_edge_ref[key], true)) : |
734 | 736 |
_from.direction(key); |
735 | 737 |
return _to.direct(_edge_ref[key], forward); |
736 | 738 |
} |
737 | 739 |
|
738 | 740 |
const From& _from; |
739 | 741 |
const To& _to; |
740 | 742 |
const EdgeRefMap& _edge_ref; |
741 | 743 |
const NodeRefMap& _node_ref; |
742 | 744 |
}; |
743 | 745 |
|
744 | 746 |
public: |
745 | 747 |
|
746 | 748 |
/// \brief Constructor of GraphCopy. |
747 | 749 |
/// |
748 | 750 |
/// Constructor of GraphCopy for copying the content of the |
749 | 751 |
/// \c from graph into the \c to graph. |
750 | 752 |
GraphCopy(const From& from, To& to) |
751 | 753 |
: _from(from), _to(to) {} |
752 | 754 |
|
753 | 755 |
/// \brief Destructor of GraphCopy |
754 | 756 |
/// |
755 | 757 |
/// Destructor of GraphCopy. |
756 | 758 |
~GraphCopy() { |
757 | 759 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
758 | 760 |
delete _node_maps[i]; |
759 | 761 |
} |
760 | 762 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
761 | 763 |
delete _arc_maps[i]; |
762 | 764 |
} |
763 | 765 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
764 | 766 |
delete _edge_maps[i]; |
765 | 767 |
} |
766 | 768 |
} |
767 | 769 |
|
768 | 770 |
/// \brief Copy the node references into the given map. |
769 | 771 |
/// |
770 | 772 |
/// This function copies the node references into the given map. |
771 | 773 |
/// The parameter should be a map, whose key type is the Node type of |
772 | 774 |
/// the source graph, while the value type is the Node type of the |
773 | 775 |
/// destination graph. |
774 | 776 |
template <typename NodeRef> |
775 | 777 |
GraphCopy& nodeRef(NodeRef& map) { |
776 | 778 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
777 | 779 |
NodeRefMap, NodeRef>(map)); |
778 | 780 |
return *this; |
779 | 781 |
} |
780 | 782 |
|
781 | 783 |
/// \brief Copy the node cross references into the given map. |
782 | 784 |
/// |
783 | 785 |
/// This function copies the node cross references (reverse references) |
784 | 786 |
/// into the given map. The parameter should be a map, whose key type |
785 | 787 |
/// is the Node type of the destination graph, while the value type is |
786 | 788 |
/// the Node type of the source graph. |
787 | 789 |
template <typename NodeCrossRef> |
788 | 790 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
789 | 791 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
790 | 792 |
NodeRefMap, NodeCrossRef>(map)); |
791 | 793 |
return *this; |
792 | 794 |
} |
793 | 795 |
|
794 | 796 |
/// \brief Make a copy of the given node map. |
795 | 797 |
/// |
796 | 798 |
/// This function makes a copy of the given node map for the newly |
797 | 799 |
/// created graph. |
798 | 800 |
/// The key type of the new map \c tmap should be the Node type of the |
799 | 801 |
/// destination graph, and the key type of the original map \c map |
800 | 802 |
/// should be the Node type of the source graph. |
801 | 803 |
template <typename FromMap, typename ToMap> |
802 | 804 |
GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
803 | 805 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
804 | 806 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
805 | 807 |
return *this; |
806 | 808 |
} |
807 | 809 |
|
808 | 810 |
/// \brief Make a copy of the given node. |
809 | 811 |
/// |
810 | 812 |
/// This function makes a copy of the given node. |
811 | 813 |
GraphCopy& node(const Node& node, TNode& tnode) { |
812 | 814 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
813 | 815 |
NodeRefMap, TNode>(node, tnode)); |
814 | 816 |
return *this; |
815 | 817 |
} |
816 | 818 |
|
817 | 819 |
/// \brief Copy the arc references into the given map. |
818 | 820 |
/// |
819 | 821 |
/// This function copies the arc references into the given map. |
820 | 822 |
/// The parameter should be a map, whose key type is the Arc type of |
821 | 823 |
/// the source graph, while the value type is the Arc type of the |
822 | 824 |
/// destination graph. |
823 | 825 |
template <typename ArcRef> |
824 | 826 |
GraphCopy& arcRef(ArcRef& map) { |
825 | 827 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
826 | 828 |
ArcRefMap, ArcRef>(map)); |
827 | 829 |
return *this; |
828 | 830 |
} |
829 | 831 |
|
830 | 832 |
/// \brief Copy the arc cross references into the given map. |
831 | 833 |
/// |
832 | 834 |
/// This function copies the arc cross references (reverse references) |
833 | 835 |
/// into the given map. The parameter should be a map, whose key type |
834 | 836 |
/// is the Arc type of the destination graph, while the value type is |
835 | 837 |
/// the Arc type of the source graph. |
836 | 838 |
template <typename ArcCrossRef> |
837 | 839 |
GraphCopy& arcCrossRef(ArcCrossRef& map) { |
838 | 840 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
839 | 841 |
ArcRefMap, ArcCrossRef>(map)); |
840 | 842 |
return *this; |
841 | 843 |
} |
842 | 844 |
|
843 | 845 |
/// \brief Make a copy of the given arc map. |
844 | 846 |
/// |
845 | 847 |
/// This function makes a copy of the given arc map for the newly |
846 | 848 |
/// created graph. |
847 | 849 |
/// The key type of the new map \c tmap should be the Arc type of the |
848 | 850 |
/// destination graph, and the key type of the original map \c map |
849 | 851 |
/// should be the Arc type of the source graph. |
850 | 852 |
template <typename FromMap, typename ToMap> |
851 | 853 |
GraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
852 | 854 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
853 | 855 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
854 | 856 |
return *this; |
855 | 857 |
} |
856 | 858 |
|
857 | 859 |
/// \brief Make a copy of the given arc. |
858 | 860 |
/// |
859 | 861 |
/// This function makes a copy of the given arc. |
860 | 862 |
GraphCopy& arc(const Arc& arc, TArc& tarc) { |
861 | 863 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
862 | 864 |
ArcRefMap, TArc>(arc, tarc)); |
863 | 865 |
return *this; |
864 | 866 |
} |
865 | 867 |
|
866 | 868 |
/// \brief Copy the edge references into the given map. |
867 | 869 |
/// |
868 | 870 |
/// This function copies the edge references into the given map. |
869 | 871 |
/// The parameter should be a map, whose key type is the Edge type of |
870 | 872 |
/// the source graph, while the value type is the Edge type of the |
871 | 873 |
/// destination graph. |
872 | 874 |
template <typename EdgeRef> |
873 | 875 |
GraphCopy& edgeRef(EdgeRef& map) { |
874 | 876 |
_edge_maps.push_back(new _core_bits::RefCopy<From, Edge, |
875 | 877 |
EdgeRefMap, EdgeRef>(map)); |
876 | 878 |
return *this; |
877 | 879 |
} |
878 | 880 |
|
879 | 881 |
/// \brief Copy the edge cross references into the given map. |
880 | 882 |
/// |
881 | 883 |
/// This function copies the edge cross references (reverse references) |
882 | 884 |
/// into the given map. The parameter should be a map, whose key type |
883 | 885 |
/// is the Edge type of the destination graph, while the value type is |
884 | 886 |
/// the Edge type of the source graph. |
885 | 887 |
template <typename EdgeCrossRef> |
886 | 888 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
887 | 889 |
_edge_maps.push_back(new _core_bits::CrossRefCopy<From, |
888 | 890 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
889 | 891 |
return *this; |
890 | 892 |
} |
891 | 893 |
|
892 | 894 |
/// \brief Make a copy of the given edge map. |
893 | 895 |
/// |
894 | 896 |
/// This function makes a copy of the given edge map for the newly |
895 | 897 |
/// created graph. |
896 | 898 |
/// The key type of the new map \c tmap should be the Edge type of the |
897 | 899 |
/// destination graph, and the key type of the original map \c map |
898 | 900 |
/// should be the Edge type of the source graph. |
899 | 901 |
template <typename FromMap, typename ToMap> |
900 | 902 |
GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) { |
901 | 903 |
_edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
902 | 904 |
EdgeRefMap, FromMap, ToMap>(map, tmap)); |
903 | 905 |
return *this; |
904 | 906 |
} |
905 | 907 |
|
906 | 908 |
/// \brief Make a copy of the given edge. |
907 | 909 |
/// |
908 | 910 |
/// This function makes a copy of the given edge. |
909 | 911 |
GraphCopy& edge(const Edge& edge, TEdge& tedge) { |
910 | 912 |
_edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
911 | 913 |
EdgeRefMap, TEdge>(edge, tedge)); |
912 | 914 |
return *this; |
913 | 915 |
} |
914 | 916 |
|
915 | 917 |
/// \brief Execute copying. |
916 | 918 |
/// |
917 | 919 |
/// This function executes the copying of the graph along with the |
918 | 920 |
/// copying of the assigned data. |
919 | 921 |
void run() { |
920 | 922 |
NodeRefMap nodeRefMap(_from); |
921 | 923 |
EdgeRefMap edgeRefMap(_from); |
922 | 924 |
ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap); |
923 | 925 |
_core_bits::GraphCopySelector<To>:: |
924 | 926 |
copy(_from, _to, nodeRefMap, edgeRefMap); |
925 | 927 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
926 | 928 |
_node_maps[i]->copy(_from, nodeRefMap); |
927 | 929 |
} |
928 | 930 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
929 | 931 |
_edge_maps[i]->copy(_from, edgeRefMap); |
930 | 932 |
} |
931 | 933 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
932 | 934 |
_arc_maps[i]->copy(_from, arcRefMap); |
933 | 935 |
} |
934 | 936 |
} |
935 | 937 |
|
936 | 938 |
private: |
937 | 939 |
|
938 | 940 |
const From& _from; |
939 | 941 |
To& _to; |
940 | 942 |
|
941 | 943 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
942 | 944 |
_node_maps; |
943 | 945 |
|
944 | 946 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
945 | 947 |
_arc_maps; |
946 | 948 |
|
947 | 949 |
std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
948 | 950 |
_edge_maps; |
949 | 951 |
|
950 | 952 |
}; |
951 | 953 |
|
952 | 954 |
/// \brief Copy a graph to another graph. |
953 | 955 |
/// |
954 | 956 |
/// This function copies a graph to another graph. |
955 | 957 |
/// The complete usage of it is detailed in the GraphCopy class, |
956 | 958 |
/// but a short example shows a basic work: |
957 | 959 |
///\code |
958 | 960 |
/// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); |
959 | 961 |
///\endcode |
960 | 962 |
/// |
961 | 963 |
/// After the copy the \c nr map will contain the mapping from the |
962 | 964 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
963 | 965 |
/// \c ecr will contain the mapping from the edges of the \c to graph |
964 | 966 |
/// to the edges of the \c from graph. |
965 | 967 |
/// |
966 | 968 |
/// \see GraphCopy |
967 | 969 |
template <typename From, typename To> |
968 | 970 |
GraphCopy<From, To> |
969 | 971 |
graphCopy(const From& from, To& to) { |
970 | 972 |
return GraphCopy<From, To>(from, to); |
971 | 973 |
} |
972 | 974 |
|
973 | 975 |
namespace _core_bits { |
974 | 976 |
|
975 | 977 |
template <typename Graph, typename Enable = void> |
976 | 978 |
struct FindArcSelector { |
977 | 979 |
typedef typename Graph::Node Node; |
978 | 980 |
typedef typename Graph::Arc Arc; |
979 | 981 |
static Arc find(const Graph &g, Node u, Node v, Arc e) { |
980 | 982 |
if (e == INVALID) { |
981 | 983 |
g.firstOut(e, u); |
982 | 984 |
} else { |
983 | 985 |
g.nextOut(e); |
984 | 986 |
} |
985 | 987 |
while (e != INVALID && g.target(e) != v) { |
986 | 988 |
g.nextOut(e); |
987 | 989 |
} |
988 | 990 |
return e; |
989 | 991 |
} |
990 | 992 |
}; |
991 | 993 |
|
992 | 994 |
template <typename Graph> |
993 | 995 |
struct FindArcSelector< |
994 | 996 |
Graph, |
995 | 997 |
typename enable_if<typename Graph::FindArcTag, void>::type> |
996 | 998 |
{ |
997 | 999 |
typedef typename Graph::Node Node; |
998 | 1000 |
typedef typename Graph::Arc Arc; |
999 | 1001 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
1000 | 1002 |
return g.findArc(u, v, prev); |
1001 | 1003 |
} |
1002 | 1004 |
}; |
1003 | 1005 |
} |
1004 | 1006 |
|
1005 | 1007 |
/// \brief Find an arc between two nodes of a digraph. |
1006 | 1008 |
/// |
1007 | 1009 |
/// This function finds an arc from node \c u to node \c v in the |
1008 | 1010 |
/// digraph \c g. |
1009 | 1011 |
/// |
1010 | 1012 |
/// If \c prev is \ref INVALID (this is the default value), then |
1011 | 1013 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
1012 | 1014 |
/// the next arc from \c u to \c v after \c prev. |
1013 | 1015 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
1014 | 1016 |
/// |
1015 | 1017 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
1016 | 1018 |
///\code |
1017 | 1019 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) { |
1018 | 1020 |
/// ... |
1019 | 1021 |
/// } |
1020 | 1022 |
///\endcode |
1021 | 1023 |
/// |
1022 | 1024 |
/// \note \ref ConArcIt provides iterator interface for the same |
1023 | 1025 |
/// functionality. |
1024 | 1026 |
/// |
1025 | 1027 |
///\sa ConArcIt |
1026 | 1028 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
1027 | 1029 |
template <typename Graph> |
1028 | 1030 |
inline typename Graph::Arc |
1029 | 1031 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
1030 | 1032 |
typename Graph::Arc prev = INVALID) { |
1031 | 1033 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
1032 | 1034 |
} |
1033 | 1035 |
|
1034 | 1036 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes. |
1035 | 1037 |
/// |
1036 | 1038 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is |
1037 | 1039 |
/// a higher level interface for the \ref findArc() function. You can |
1038 | 1040 |
/// use it the following way: |
1039 | 1041 |
///\code |
1040 | 1042 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
1041 | 1043 |
/// ... |
1042 | 1044 |
/// } |
1043 | 1045 |
///\endcode |
1044 | 1046 |
/// |
1045 | 1047 |
///\sa findArc() |
1046 | 1048 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
1047 | 1049 |
template <typename GR> |
1048 | 1050 |
class ConArcIt : public GR::Arc { |
1049 | 1051 |
typedef typename GR::Arc Parent; |
1050 | 1052 |
|
1051 | 1053 |
public: |
1052 | 1054 |
|
1053 | 1055 |
typedef typename GR::Arc Arc; |
1054 | 1056 |
typedef typename GR::Node Node; |
1055 | 1057 |
|
1056 | 1058 |
/// \brief Constructor. |
1057 | 1059 |
/// |
1058 | 1060 |
/// Construct a new ConArcIt iterating on the arcs that |
1059 | 1061 |
/// connects nodes \c u and \c v. |
1060 | 1062 |
ConArcIt(const GR& g, Node u, Node v) : _graph(g) { |
1061 | 1063 |
Parent::operator=(findArc(_graph, u, v)); |
1062 | 1064 |
} |
1063 | 1065 |
|
1064 | 1066 |
/// \brief Constructor. |
1065 | 1067 |
/// |
1066 | 1068 |
/// Construct a new ConArcIt that continues the iterating from arc \c a. |
1067 | 1069 |
ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {} |
1068 | 1070 |
|
1069 | 1071 |
/// \brief Increment operator. |
1070 | 1072 |
/// |
1071 | 1073 |
/// It increments the iterator and gives back the next arc. |
1072 | 1074 |
ConArcIt& operator++() { |
1073 | 1075 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
1074 | 1076 |
_graph.target(*this), *this)); |
1075 | 1077 |
return *this; |
1076 | 1078 |
} |
1077 | 1079 |
private: |
1078 | 1080 |
const GR& _graph; |
1079 | 1081 |
}; |
1080 | 1082 |
|
1081 | 1083 |
namespace _core_bits { |
1082 | 1084 |
|
1083 | 1085 |
template <typename Graph, typename Enable = void> |
1084 | 1086 |
struct FindEdgeSelector { |
1085 | 1087 |
typedef typename Graph::Node Node; |
1086 | 1088 |
typedef typename Graph::Edge Edge; |
1087 | 1089 |
static Edge find(const Graph &g, Node u, Node v, Edge e) { |
1088 | 1090 |
bool b; |
1089 | 1091 |
if (u != v) { |
1090 | 1092 |
if (e == INVALID) { |
1091 | 1093 |
g.firstInc(e, b, u); |
1092 | 1094 |
} else { |
1093 | 1095 |
b = g.u(e) == u; |
1094 | 1096 |
g.nextInc(e, b); |
1095 | 1097 |
} |
1096 | 1098 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) { |
1097 | 1099 |
g.nextInc(e, b); |
1098 | 1100 |
} |
1099 | 1101 |
} else { |
1100 | 1102 |
if (e == INVALID) { |
1101 | 1103 |
g.firstInc(e, b, u); |
1102 | 1104 |
} else { |
1103 | 1105 |
b = true; |
1104 | 1106 |
g.nextInc(e, b); |
1105 | 1107 |
} |
1106 | 1108 |
while (e != INVALID && (!b || g.v(e) != v)) { |
1107 | 1109 |
g.nextInc(e, b); |
1108 | 1110 |
} |
1109 | 1111 |
} |
1110 | 1112 |
return e; |
1111 | 1113 |
} |
1112 | 1114 |
}; |
1113 | 1115 |
|
1114 | 1116 |
template <typename Graph> |
1115 | 1117 |
struct FindEdgeSelector< |
1116 | 1118 |
Graph, |
1117 | 1119 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
1118 | 1120 |
{ |
1119 | 1121 |
typedef typename Graph::Node Node; |
1120 | 1122 |
typedef typename Graph::Edge Edge; |
1121 | 1123 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
1122 | 1124 |
return g.findEdge(u, v, prev); |
1123 | 1125 |
} |
1124 | 1126 |
}; |
1125 | 1127 |
} |
1126 | 1128 |
|
1127 | 1129 |
/// \brief Find an edge between two nodes of a graph. |
1128 | 1130 |
/// |
1129 | 1131 |
/// This function finds an edge from node \c u to node \c v in graph \c g. |
1130 | 1132 |
/// If node \c u and node \c v is equal then each loop edge |
1131 | 1133 |
/// will be enumerated once. |
1132 | 1134 |
/// |
1133 | 1135 |
/// If \c prev is \ref INVALID (this is the default value), then |
1134 | 1136 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for |
1135 | 1137 |
/// the next edge from \c u to \c v after \c prev. |
1136 | 1138 |
/// \return The found edge or \ref INVALID if there is no such an edge. |
1137 | 1139 |
/// |
1138 | 1140 |
/// Thus you can iterate through each edge between \c u and \c v |
1139 | 1141 |
/// as it follows. |
1140 | 1142 |
///\code |
1141 | 1143 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) { |
1142 | 1144 |
/// ... |
1143 | 1145 |
/// } |
1144 | 1146 |
///\endcode |
1145 | 1147 |
/// |
1146 | 1148 |
/// \note \ref ConEdgeIt provides iterator interface for the same |
1147 | 1149 |
/// functionality. |
1148 | 1150 |
/// |
1149 | 1151 |
///\sa ConEdgeIt |
1150 | 1152 |
template <typename Graph> |
1151 | 1153 |
inline typename Graph::Edge |
1152 | 1154 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
1153 | 1155 |
typename Graph::Edge p = INVALID) { |
1154 | 1156 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
1155 | 1157 |
} |
1156 | 1158 |
|
1157 | 1159 |
/// \brief Iterator for iterating on parallel edges connecting the same nodes. |
1158 | 1160 |
/// |
1159 | 1161 |
/// Iterator for iterating on parallel edges connecting the same nodes. |
1160 | 1162 |
/// It is a higher level interface for the findEdge() function. You can |
1161 | 1163 |
/// use it the following way: |
1162 | 1164 |
///\code |
1163 | 1165 |
/// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) { |
1164 | 1166 |
/// ... |
1165 | 1167 |
/// } |
1166 | 1168 |
///\endcode |
1167 | 1169 |
/// |
1168 | 1170 |
///\sa findEdge() |
1169 | 1171 |
template <typename GR> |
1170 | 1172 |
class ConEdgeIt : public GR::Edge { |
1171 | 1173 |
typedef typename GR::Edge Parent; |
1172 | 1174 |
|
1173 | 1175 |
public: |
1174 | 1176 |
|
1175 | 1177 |
typedef typename GR::Edge Edge; |
1176 | 1178 |
typedef typename GR::Node Node; |
1177 | 1179 |
|
1178 | 1180 |
/// \brief Constructor. |
1179 | 1181 |
/// |
1180 | 1182 |
/// Construct a new ConEdgeIt iterating on the edges that |
1181 | 1183 |
/// connects nodes \c u and \c v. |
1182 | 1184 |
ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) { |
1183 | 1185 |
Parent::operator=(findEdge(_graph, _u, _v)); |
1184 | 1186 |
} |
1185 | 1187 |
|
1186 | 1188 |
/// \brief Constructor. |
1187 | 1189 |
/// |
1188 | 1190 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e. |
1189 | 1191 |
ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {} |
1190 | 1192 |
|
1191 | 1193 |
/// \brief Increment operator. |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief DFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Dfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Dfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct DfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the %DFS paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the %DFS paths. |
50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
67 | 67 |
///Instantiates a \c ProcessedMap. |
68 | 68 |
|
69 | 69 |
///This function instantiates a \ref ProcessedMap. |
70 | 70 |
///\param g is the digraph, to which |
71 | 71 |
///we would like to define the \ref ProcessedMap. |
72 | 72 |
#ifdef DOXYGEN |
73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
74 | 74 |
#else |
75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
76 | 76 |
#endif |
77 | 77 |
{ |
78 | 78 |
return new ProcessedMap(); |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
///The type of the map that indicates which nodes are reached. |
82 | 82 |
|
83 | 83 |
///The type of the map that indicates which nodes are reached. |
84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
86 | 86 |
///Instantiates a \c ReachedMap. |
87 | 87 |
|
88 | 88 |
///This function instantiates a \ref ReachedMap. |
89 | 89 |
///\param g is the digraph, to which |
90 | 90 |
///we would like to define the \ref ReachedMap. |
91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
92 | 92 |
{ |
93 | 93 |
return new ReachedMap(g); |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
///The type of the map that stores the distances of the nodes. |
97 | 97 |
|
98 | 98 |
///The type of the map that stores the distances of the nodes. |
99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
101 | 101 |
///Instantiates a \c DistMap. |
102 | 102 |
|
103 | 103 |
///This function instantiates a \ref DistMap. |
104 | 104 |
///\param g is the digraph, to which we would like to define the |
105 | 105 |
///\ref DistMap. |
106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
107 | 107 |
{ |
108 | 108 |
return new DistMap(g); |
109 | 109 |
} |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
///%DFS algorithm class. |
113 | 113 |
|
114 | 114 |
///\ingroup search |
115 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
116 | 116 |
/// |
117 | 117 |
///There is also a \ref dfs() "function-type interface" for the DFS |
118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
119 | 119 |
///used easier. |
120 | 120 |
/// |
121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
122 | 122 |
///The default type is \ref ListDigraph. |
123 | 123 |
#ifdef DOXYGEN |
124 | 124 |
template <typename GR, |
125 | 125 |
typename TR> |
126 | 126 |
#else |
127 | 127 |
template <typename GR=ListDigraph, |
128 | 128 |
typename TR=DfsDefaultTraits<GR> > |
129 | 129 |
#endif |
130 | 130 |
class Dfs { |
131 | 131 |
public: |
132 | 132 |
|
133 | 133 |
///The type of the digraph the algorithm runs on. |
134 | 134 |
typedef typename TR::Digraph Digraph; |
135 | 135 |
|
136 | 136 |
///\brief The type of the map that stores the predecessor arcs of the |
137 | 137 |
///DFS paths. |
138 | 138 |
typedef typename TR::PredMap PredMap; |
139 | 139 |
///The type of the map that stores the distances of the nodes. |
140 | 140 |
typedef typename TR::DistMap DistMap; |
141 | 141 |
///The type of the map that indicates which nodes are reached. |
142 | 142 |
typedef typename TR::ReachedMap ReachedMap; |
143 | 143 |
///The type of the map that indicates which nodes are processed. |
144 | 144 |
typedef typename TR::ProcessedMap ProcessedMap; |
145 | 145 |
///The type of the paths. |
146 | 146 |
typedef PredMapPath<Digraph, PredMap> Path; |
147 | 147 |
|
148 | 148 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
149 | 149 |
typedef TR Traits; |
150 | 150 |
|
151 | 151 |
private: |
152 | 152 |
|
153 | 153 |
typedef typename Digraph::Node Node; |
154 | 154 |
typedef typename Digraph::NodeIt NodeIt; |
155 | 155 |
typedef typename Digraph::Arc Arc; |
156 | 156 |
typedef typename Digraph::OutArcIt OutArcIt; |
157 | 157 |
|
158 | 158 |
//Pointer to the underlying digraph. |
159 | 159 |
const Digraph *G; |
160 | 160 |
//Pointer to the map of predecessor arcs. |
161 | 161 |
PredMap *_pred; |
162 | 162 |
//Indicates if _pred is locally allocated (true) or not. |
163 | 163 |
bool local_pred; |
164 | 164 |
//Pointer to the map of distances. |
165 | 165 |
DistMap *_dist; |
166 | 166 |
//Indicates if _dist is locally allocated (true) or not. |
167 | 167 |
bool local_dist; |
168 | 168 |
//Pointer to the map of reached status of the nodes. |
169 | 169 |
ReachedMap *_reached; |
170 | 170 |
//Indicates if _reached is locally allocated (true) or not. |
171 | 171 |
bool local_reached; |
172 | 172 |
//Pointer to the map of processed status of the nodes. |
173 | 173 |
ProcessedMap *_processed; |
174 | 174 |
//Indicates if _processed is locally allocated (true) or not. |
175 | 175 |
bool local_processed; |
176 | 176 |
|
177 | 177 |
std::vector<typename Digraph::OutArcIt> _stack; |
178 | 178 |
int _stack_head; |
179 | 179 |
|
180 | 180 |
//Creates the maps if necessary. |
181 | 181 |
void create_maps() |
182 | 182 |
{ |
183 | 183 |
if(!_pred) { |
184 | 184 |
local_pred = true; |
185 | 185 |
_pred = Traits::createPredMap(*G); |
186 | 186 |
} |
187 | 187 |
if(!_dist) { |
188 | 188 |
local_dist = true; |
189 | 189 |
_dist = Traits::createDistMap(*G); |
190 | 190 |
} |
191 | 191 |
if(!_reached) { |
192 | 192 |
local_reached = true; |
193 | 193 |
_reached = Traits::createReachedMap(*G); |
194 | 194 |
} |
195 | 195 |
if(!_processed) { |
196 | 196 |
local_processed = true; |
197 | 197 |
_processed = Traits::createProcessedMap(*G); |
198 | 198 |
} |
199 | 199 |
} |
200 | 200 |
|
201 | 201 |
protected: |
202 | 202 |
|
203 | 203 |
Dfs() {} |
204 | 204 |
|
205 | 205 |
public: |
206 | 206 |
|
207 | 207 |
typedef Dfs Create; |
208 | 208 |
|
209 | 209 |
///\name Named Template Parameters |
210 | 210 |
|
211 | 211 |
///@{ |
212 | 212 |
|
213 | 213 |
template <class T> |
214 | 214 |
struct SetPredMapTraits : public Traits { |
215 | 215 |
typedef T PredMap; |
216 | 216 |
static PredMap *createPredMap(const Digraph &) |
217 | 217 |
{ |
218 | 218 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
219 | 219 |
return 0; // ignore warnings |
220 | 220 |
} |
221 | 221 |
}; |
222 | 222 |
///\brief \ref named-templ-param "Named parameter" for setting |
223 | 223 |
///\c PredMap type. |
224 | 224 |
/// |
225 | 225 |
///\ref named-templ-param "Named parameter" for setting |
226 | 226 |
///\c PredMap type. |
227 | 227 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
228 | 228 |
template <class T> |
229 | 229 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
230 | 230 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
231 | 231 |
}; |
232 | 232 |
|
233 | 233 |
template <class T> |
234 | 234 |
struct SetDistMapTraits : public Traits { |
235 | 235 |
typedef T DistMap; |
236 | 236 |
static DistMap *createDistMap(const Digraph &) |
237 | 237 |
{ |
238 | 238 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
239 | 239 |
return 0; // ignore warnings |
240 | 240 |
} |
241 | 241 |
}; |
242 | 242 |
///\brief \ref named-templ-param "Named parameter" for setting |
243 | 243 |
///\c DistMap type. |
244 | 244 |
/// |
245 | 245 |
///\ref named-templ-param "Named parameter" for setting |
246 | 246 |
///\c DistMap type. |
247 | 247 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
248 | 248 |
template <class T> |
249 | 249 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { |
250 | 250 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
251 | 251 |
}; |
252 | 252 |
|
253 | 253 |
template <class T> |
254 | 254 |
struct SetReachedMapTraits : public Traits { |
255 | 255 |
typedef T ReachedMap; |
256 | 256 |
static ReachedMap *createReachedMap(const Digraph &) |
257 | 257 |
{ |
258 | 258 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
259 | 259 |
return 0; // ignore warnings |
260 | 260 |
} |
261 | 261 |
}; |
262 | 262 |
///\brief \ref named-templ-param "Named parameter" for setting |
263 | 263 |
///\c ReachedMap type. |
264 | 264 |
/// |
265 | 265 |
///\ref named-templ-param "Named parameter" for setting |
266 | 266 |
///\c ReachedMap type. |
267 | 267 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
268 | 268 |
template <class T> |
269 | 269 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > { |
270 | 270 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
271 | 271 |
}; |
272 | 272 |
|
273 | 273 |
template <class T> |
274 | 274 |
struct SetProcessedMapTraits : public Traits { |
275 | 275 |
typedef T ProcessedMap; |
276 | 276 |
static ProcessedMap *createProcessedMap(const Digraph &) |
277 | 277 |
{ |
278 | 278 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
279 | 279 |
return 0; // ignore warnings |
280 | 280 |
} |
281 | 281 |
}; |
282 | 282 |
///\brief \ref named-templ-param "Named parameter" for setting |
283 | 283 |
///\c ProcessedMap type. |
284 | 284 |
/// |
285 | 285 |
///\ref named-templ-param "Named parameter" for setting |
286 | 286 |
///\c ProcessedMap type. |
287 | 287 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
288 | 288 |
template <class T> |
289 | 289 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > { |
290 | 290 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
291 | 291 |
}; |
292 | 292 |
|
293 | 293 |
struct SetStandardProcessedMapTraits : public Traits { |
294 | 294 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
295 | 295 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
296 | 296 |
{ |
297 | 297 |
return new ProcessedMap(g); |
298 | 298 |
} |
299 | 299 |
}; |
300 | 300 |
///\brief \ref named-templ-param "Named parameter" for setting |
301 | 301 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
302 | 302 |
/// |
303 | 303 |
///\ref named-templ-param "Named parameter" for setting |
304 | 304 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
305 | 305 |
///If you don't set it explicitly, it will be automatically allocated. |
306 | 306 |
struct SetStandardProcessedMap : |
307 | 307 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
308 | 308 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
309 | 309 |
}; |
310 | 310 |
|
311 | 311 |
///@} |
312 | 312 |
|
313 | 313 |
public: |
314 | 314 |
|
315 | 315 |
///Constructor. |
316 | 316 |
|
317 | 317 |
///Constructor. |
318 | 318 |
///\param g The digraph the algorithm runs on. |
319 | 319 |
Dfs(const Digraph &g) : |
320 | 320 |
G(&g), |
321 | 321 |
_pred(NULL), local_pred(false), |
322 | 322 |
_dist(NULL), local_dist(false), |
323 | 323 |
_reached(NULL), local_reached(false), |
324 | 324 |
_processed(NULL), local_processed(false) |
325 | 325 |
{ } |
326 | 326 |
|
327 | 327 |
///Destructor. |
328 | 328 |
~Dfs() |
329 | 329 |
{ |
330 | 330 |
if(local_pred) delete _pred; |
331 | 331 |
if(local_dist) delete _dist; |
332 | 332 |
if(local_reached) delete _reached; |
333 | 333 |
if(local_processed) delete _processed; |
334 | 334 |
} |
335 | 335 |
|
336 | 336 |
///Sets the map that stores the predecessor arcs. |
337 | 337 |
|
338 | 338 |
///Sets the map that stores the predecessor arcs. |
339 | 339 |
///If you don't use this function before calling \ref run(Node) "run()" |
340 | 340 |
///or \ref init(), an instance will be allocated automatically. |
341 | 341 |
///The destructor deallocates this automatically allocated map, |
342 | 342 |
///of course. |
343 | 343 |
///\return <tt> (*this) </tt> |
344 | 344 |
Dfs &predMap(PredMap &m) |
345 | 345 |
{ |
346 | 346 |
if(local_pred) { |
347 | 347 |
delete _pred; |
348 | 348 |
local_pred=false; |
349 | 349 |
} |
350 | 350 |
_pred = &m; |
351 | 351 |
return *this; |
352 | 352 |
} |
353 | 353 |
|
354 | 354 |
///Sets the map that indicates which nodes are reached. |
355 | 355 |
|
356 | 356 |
///Sets the map that indicates which nodes are reached. |
357 | 357 |
///If you don't use this function before calling \ref run(Node) "run()" |
358 | 358 |
///or \ref init(), an instance will be allocated automatically. |
359 | 359 |
///The destructor deallocates this automatically allocated map, |
360 | 360 |
///of course. |
361 | 361 |
///\return <tt> (*this) </tt> |
362 | 362 |
Dfs &reachedMap(ReachedMap &m) |
363 | 363 |
{ |
364 | 364 |
if(local_reached) { |
365 | 365 |
delete _reached; |
366 | 366 |
local_reached=false; |
367 | 367 |
} |
368 | 368 |
_reached = &m; |
369 | 369 |
return *this; |
370 | 370 |
} |
371 | 371 |
|
372 | 372 |
///Sets the map that indicates which nodes are processed. |
373 | 373 |
|
374 | 374 |
///Sets the map that indicates which nodes are processed. |
375 | 375 |
///If you don't use this function before calling \ref run(Node) "run()" |
376 | 376 |
///or \ref init(), an instance will be allocated automatically. |
377 | 377 |
///The destructor deallocates this automatically allocated map, |
378 | 378 |
///of course. |
379 | 379 |
///\return <tt> (*this) </tt> |
380 | 380 |
Dfs &processedMap(ProcessedMap &m) |
381 | 381 |
{ |
382 | 382 |
if(local_processed) { |
383 | 383 |
delete _processed; |
384 | 384 |
local_processed=false; |
385 | 385 |
} |
386 | 386 |
_processed = &m; |
387 | 387 |
return *this; |
388 | 388 |
} |
389 | 389 |
|
390 | 390 |
///Sets the map that stores the distances of the nodes. |
391 | 391 |
|
392 | 392 |
///Sets the map that stores the distances of the nodes calculated by |
393 | 393 |
///the algorithm. |
394 | 394 |
///If you don't use this function before calling \ref run(Node) "run()" |
395 | 395 |
///or \ref init(), an instance will be allocated automatically. |
396 | 396 |
///The destructor deallocates this automatically allocated map, |
397 | 397 |
///of course. |
398 | 398 |
///\return <tt> (*this) </tt> |
399 | 399 |
Dfs &distMap(DistMap &m) |
400 | 400 |
{ |
401 | 401 |
if(local_dist) { |
402 | 402 |
delete _dist; |
403 | 403 |
local_dist=false; |
404 | 404 |
} |
405 | 405 |
_dist = &m; |
406 | 406 |
return *this; |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
public: |
410 | 410 |
|
411 | 411 |
///\name Execution Control |
412 | 412 |
///The simplest way to execute the DFS algorithm is to use one of the |
413 | 413 |
///member functions called \ref run(Node) "run()".\n |
414 | 414 |
///If you need more control on the execution, first you have to call |
415 | 415 |
///\ref init(), then you can add a source node with \ref addSource() |
416 | 416 |
///and perform the actual computation with \ref start(). |
417 | 417 |
///This procedure can be repeated if there are nodes that have not |
418 | 418 |
///been reached. |
419 | 419 |
|
420 | 420 |
///@{ |
421 | 421 |
|
422 | 422 |
///\brief Initializes the internal data structures. |
423 | 423 |
/// |
424 | 424 |
///Initializes the internal data structures. |
425 | 425 |
void init() |
426 | 426 |
{ |
427 | 427 |
create_maps(); |
428 | 428 |
_stack.resize(countNodes(*G)); |
429 | 429 |
_stack_head=-1; |
430 | 430 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
431 | 431 |
_pred->set(u,INVALID); |
432 | 432 |
_reached->set(u,false); |
433 | 433 |
_processed->set(u,false); |
434 | 434 |
} |
435 | 435 |
} |
436 | 436 |
|
437 | 437 |
///Adds a new source node. |
438 | 438 |
|
439 | 439 |
///Adds a new source node to the set of nodes to be processed. |
440 | 440 |
/// |
441 | 441 |
///\pre The stack must be empty. Otherwise the algorithm gives |
442 | 442 |
///wrong results. (One of the outgoing arcs of all the source nodes |
443 | 443 |
///except for the last one will not be visited and distances will |
444 | 444 |
///also be wrong.) |
445 | 445 |
void addSource(Node s) |
446 | 446 |
{ |
447 | 447 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
448 | 448 |
if(!(*_reached)[s]) |
449 | 449 |
{ |
450 | 450 |
_reached->set(s,true); |
451 | 451 |
_pred->set(s,INVALID); |
452 | 452 |
OutArcIt e(*G,s); |
453 | 453 |
if(e!=INVALID) { |
454 | 454 |
_stack[++_stack_head]=e; |
455 | 455 |
_dist->set(s,_stack_head); |
456 | 456 |
} |
457 | 457 |
else { |
458 | 458 |
_processed->set(s,true); |
459 | 459 |
_dist->set(s,0); |
460 | 460 |
} |
461 | 461 |
} |
462 | 462 |
} |
463 | 463 |
|
464 | 464 |
///Processes the next arc. |
465 | 465 |
|
466 | 466 |
///Processes the next arc. |
467 | 467 |
/// |
468 | 468 |
///\return The processed arc. |
469 | 469 |
/// |
470 | 470 |
///\pre The stack must not be empty. |
471 | 471 |
Arc processNextArc() |
472 | 472 |
{ |
473 | 473 |
Node m; |
474 | 474 |
Arc e=_stack[_stack_head]; |
475 | 475 |
if(!(*_reached)[m=G->target(e)]) { |
476 | 476 |
_pred->set(m,e); |
477 | 477 |
_reached->set(m,true); |
478 | 478 |
++_stack_head; |
479 | 479 |
_stack[_stack_head] = OutArcIt(*G, m); |
480 | 480 |
_dist->set(m,_stack_head); |
481 | 481 |
} |
482 | 482 |
else { |
483 | 483 |
m=G->source(e); |
484 | 484 |
++_stack[_stack_head]; |
485 | 485 |
} |
486 | 486 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
487 | 487 |
_processed->set(m,true); |
488 | 488 |
--_stack_head; |
489 | 489 |
if(_stack_head>=0) { |
490 | 490 |
m=G->source(_stack[_stack_head]); |
491 | 491 |
++_stack[_stack_head]; |
492 | 492 |
} |
493 | 493 |
} |
494 | 494 |
return e; |
495 | 495 |
} |
496 | 496 |
|
497 | 497 |
///Next arc to be processed. |
498 | 498 |
|
499 | 499 |
///Next arc to be processed. |
500 | 500 |
/// |
501 | 501 |
///\return The next arc to be processed or \c INVALID if the stack |
502 | 502 |
///is empty. |
503 | 503 |
OutArcIt nextArc() const |
504 | 504 |
{ |
505 | 505 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
///Returns \c false if there are nodes to be processed. |
509 | 509 |
|
510 | 510 |
///Returns \c false if there are nodes to be processed |
511 | 511 |
///in the queue (stack). |
512 | 512 |
bool emptyQueue() const { return _stack_head<0; } |
513 | 513 |
|
514 | 514 |
///Returns the number of the nodes to be processed. |
515 | 515 |
|
516 | 516 |
///Returns the number of the nodes to be processed |
517 | 517 |
///in the queue (stack). |
518 | 518 |
int queueSize() const { return _stack_head+1; } |
519 | 519 |
|
520 | 520 |
///Executes the algorithm. |
521 | 521 |
|
522 | 522 |
///Executes the algorithm. |
523 | 523 |
/// |
524 | 524 |
///This method runs the %DFS algorithm from the root node |
525 | 525 |
///in order to compute the DFS path to each node. |
526 | 526 |
/// |
527 | 527 |
/// The algorithm computes |
528 | 528 |
///- the %DFS tree, |
529 | 529 |
///- the distance of each node from the root in the %DFS tree. |
530 | 530 |
/// |
531 | 531 |
///\pre init() must be called and a root node should be |
532 | 532 |
///added with addSource() before using this function. |
533 | 533 |
/// |
534 | 534 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
535 | 535 |
///\code |
536 | 536 |
/// while ( !d.emptyQueue() ) { |
537 | 537 |
/// d.processNextArc(); |
538 | 538 |
/// } |
539 | 539 |
///\endcode |
540 | 540 |
void start() |
541 | 541 |
{ |
542 | 542 |
while ( !emptyQueue() ) processNextArc(); |
543 | 543 |
} |
544 | 544 |
|
545 | 545 |
///Executes the algorithm until the given target node is reached. |
546 | 546 |
|
547 | 547 |
///Executes the algorithm until the given target node is reached. |
548 | 548 |
/// |
549 | 549 |
///This method runs the %DFS algorithm from the root node |
550 | 550 |
///in order to compute the DFS path to \c t. |
551 | 551 |
/// |
552 | 552 |
///The algorithm computes |
553 | 553 |
///- the %DFS path to \c t, |
554 | 554 |
///- the distance of \c t from the root in the %DFS tree. |
555 | 555 |
/// |
556 | 556 |
///\pre init() must be called and a root node should be |
557 | 557 |
///added with addSource() before using this function. |
558 | 558 |
void start(Node t) |
559 | 559 |
{ |
560 |
while ( !emptyQueue() && |
|
560 |
while ( !emptyQueue() && !(*_reached)[t] ) |
|
561 | 561 |
processNextArc(); |
562 | 562 |
} |
563 | 563 |
|
564 | 564 |
///Executes the algorithm until a condition is met. |
565 | 565 |
|
566 | 566 |
///Executes the algorithm until a condition is met. |
567 | 567 |
/// |
568 | 568 |
///This method runs the %DFS algorithm from the root node |
569 | 569 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
570 | 570 |
/// |
571 | 571 |
///\param am A \c bool (or convertible) arc map. The algorithm |
572 | 572 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
573 | 573 |
/// |
574 | 574 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
575 | 575 |
///\c INVALID if no such arc was found. |
576 | 576 |
/// |
577 | 577 |
///\pre init() must be called and a root node should be |
578 | 578 |
///added with addSource() before using this function. |
579 | 579 |
/// |
580 | 580 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
581 | 581 |
///not a node map. |
582 | 582 |
template<class ArcBoolMap> |
583 | 583 |
Arc start(const ArcBoolMap &am) |
584 | 584 |
{ |
585 | 585 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
586 | 586 |
processNextArc(); |
587 | 587 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
588 | 588 |
} |
589 | 589 |
|
590 | 590 |
///Runs the algorithm from the given source node. |
591 | 591 |
|
592 | 592 |
///This method runs the %DFS algorithm from node \c s |
593 | 593 |
///in order to compute the DFS path to each node. |
594 | 594 |
/// |
595 | 595 |
///The algorithm computes |
596 | 596 |
///- the %DFS tree, |
597 | 597 |
///- the distance of each node from the root in the %DFS tree. |
598 | 598 |
/// |
599 | 599 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
600 | 600 |
///\code |
601 | 601 |
/// d.init(); |
602 | 602 |
/// d.addSource(s); |
603 | 603 |
/// d.start(); |
604 | 604 |
///\endcode |
605 | 605 |
void run(Node s) { |
606 | 606 |
init(); |
607 | 607 |
addSource(s); |
608 | 608 |
start(); |
609 | 609 |
} |
610 | 610 |
|
611 | 611 |
///Finds the %DFS path between \c s and \c t. |
612 | 612 |
|
613 | 613 |
///This method runs the %DFS algorithm from node \c s |
614 | 614 |
///in order to compute the DFS path to node \c t |
615 | 615 |
///(it stops searching when \c t is processed) |
616 | 616 |
/// |
617 | 617 |
///\return \c true if \c t is reachable form \c s. |
618 | 618 |
/// |
619 | 619 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
620 | 620 |
///just a shortcut of the following code. |
621 | 621 |
///\code |
622 | 622 |
/// d.init(); |
623 | 623 |
/// d.addSource(s); |
624 | 624 |
/// d.start(t); |
625 | 625 |
///\endcode |
626 | 626 |
bool run(Node s,Node t) { |
627 | 627 |
init(); |
628 | 628 |
addSource(s); |
629 | 629 |
start(t); |
630 | 630 |
return reached(t); |
631 | 631 |
} |
632 | 632 |
|
633 | 633 |
///Runs the algorithm to visit all nodes in the digraph. |
634 | 634 |
|
635 | 635 |
///This method runs the %DFS algorithm in order to compute the |
636 | 636 |
///%DFS path to each node. |
637 | 637 |
/// |
638 | 638 |
///The algorithm computes |
639 | 639 |
///- the %DFS tree (forest), |
640 | 640 |
///- the distance of each node from the root(s) in the %DFS tree. |
641 | 641 |
/// |
642 | 642 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
643 | 643 |
///\code |
644 | 644 |
/// d.init(); |
645 | 645 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
646 | 646 |
/// if (!d.reached(n)) { |
647 | 647 |
/// d.addSource(n); |
648 | 648 |
/// d.start(); |
649 | 649 |
/// } |
650 | 650 |
/// } |
651 | 651 |
///\endcode |
652 | 652 |
void run() { |
653 | 653 |
init(); |
654 | 654 |
for (NodeIt it(*G); it != INVALID; ++it) { |
655 | 655 |
if (!reached(it)) { |
656 | 656 |
addSource(it); |
657 | 657 |
start(); |
658 | 658 |
} |
659 | 659 |
} |
660 | 660 |
} |
661 | 661 |
|
662 | 662 |
///@} |
663 | 663 |
|
664 | 664 |
///\name Query Functions |
665 | 665 |
///The results of the DFS algorithm can be obtained using these |
666 | 666 |
///functions.\n |
667 | 667 |
///Either \ref run(Node) "run()" or \ref start() should be called |
668 | 668 |
///before using them. |
669 | 669 |
|
670 | 670 |
///@{ |
671 | 671 |
|
672 | 672 |
///The DFS path to a node. |
673 | 673 |
|
674 | 674 |
///Returns the DFS path to a node. |
675 | 675 |
/// |
676 | 676 |
///\warning \c t should be reached from the root(s). |
677 | 677 |
/// |
678 | 678 |
///\pre Either \ref run(Node) "run()" or \ref init() |
679 | 679 |
///must be called before using this function. |
680 | 680 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
681 | 681 |
|
682 | 682 |
///The distance of a node from the root(s). |
683 | 683 |
|
684 | 684 |
///Returns the distance of a node from the root(s). |
685 | 685 |
/// |
686 | 686 |
///\warning If node \c v is not reached from the root(s), then |
687 | 687 |
///the return value of this function is undefined. |
688 | 688 |
/// |
689 | 689 |
///\pre Either \ref run(Node) "run()" or \ref init() |
690 | 690 |
///must be called before using this function. |
691 | 691 |
int dist(Node v) const { return (*_dist)[v]; } |
692 | 692 |
|
693 | 693 |
///Returns the 'previous arc' of the %DFS tree for a node. |
694 | 694 |
|
695 | 695 |
///This function returns the 'previous arc' of the %DFS tree for the |
696 | 696 |
///node \c v, i.e. it returns the last arc of a %DFS path from a |
697 | 697 |
///root to \c v. It is \c INVALID if \c v is not reached from the |
698 | 698 |
///root(s) or if \c v is a root. |
699 | 699 |
/// |
700 | 700 |
///The %DFS tree used here is equal to the %DFS tree used in |
701 | 701 |
///\ref predNode(). |
702 | 702 |
/// |
703 | 703 |
///\pre Either \ref run(Node) "run()" or \ref init() |
704 | 704 |
///must be called before using this function. |
705 | 705 |
Arc predArc(Node v) const { return (*_pred)[v];} |
706 | 706 |
|
707 | 707 |
///Returns the 'previous node' of the %DFS tree. |
708 | 708 |
|
709 | 709 |
///This function returns the 'previous node' of the %DFS |
710 | 710 |
///tree for the node \c v, i.e. it returns the last but one node |
711 | 711 |
///from a %DFS path from a root to \c v. It is \c INVALID |
712 | 712 |
///if \c v is not reached from the root(s) or if \c v is a root. |
713 | 713 |
/// |
714 | 714 |
///The %DFS tree used here is equal to the %DFS tree used in |
715 | 715 |
///\ref predArc(). |
716 | 716 |
/// |
717 | 717 |
///\pre Either \ref run(Node) "run()" or \ref init() |
718 | 718 |
///must be called before using this function. |
719 | 719 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
720 | 720 |
G->source((*_pred)[v]); } |
721 | 721 |
|
722 | 722 |
///\brief Returns a const reference to the node map that stores the |
723 | 723 |
///distances of the nodes. |
724 | 724 |
/// |
725 | 725 |
///Returns a const reference to the node map that stores the |
726 | 726 |
///distances of the nodes calculated by the algorithm. |
727 | 727 |
/// |
728 | 728 |
///\pre Either \ref run(Node) "run()" or \ref init() |
729 | 729 |
///must be called before using this function. |
730 | 730 |
const DistMap &distMap() const { return *_dist;} |
731 | 731 |
|
732 | 732 |
///\brief Returns a const reference to the node map that stores the |
733 | 733 |
///predecessor arcs. |
734 | 734 |
/// |
735 | 735 |
///Returns a const reference to the node map that stores the predecessor |
736 | 736 |
///arcs, which form the DFS tree. |
737 | 737 |
/// |
738 | 738 |
///\pre Either \ref run(Node) "run()" or \ref init() |
739 | 739 |
///must be called before using this function. |
740 | 740 |
const PredMap &predMap() const { return *_pred;} |
741 | 741 |
|
742 | 742 |
///Checks if a node is reached from the root(s). |
743 | 743 |
|
744 | 744 |
///Returns \c true if \c v is reached from the root(s). |
745 | 745 |
/// |
746 | 746 |
///\pre Either \ref run(Node) "run()" or \ref init() |
747 | 747 |
///must be called before using this function. |
748 | 748 |
bool reached(Node v) const { return (*_reached)[v]; } |
749 | 749 |
|
750 | 750 |
///@} |
751 | 751 |
}; |
752 | 752 |
|
753 | 753 |
///Default traits class of dfs() function. |
754 | 754 |
|
755 | 755 |
///Default traits class of dfs() function. |
756 | 756 |
///\tparam GR Digraph type. |
757 | 757 |
template<class GR> |
758 | 758 |
struct DfsWizardDefaultTraits |
759 | 759 |
{ |
760 | 760 |
///The type of the digraph the algorithm runs on. |
761 | 761 |
typedef GR Digraph; |
762 | 762 |
|
763 | 763 |
///\brief The type of the map that stores the predecessor |
764 | 764 |
///arcs of the %DFS paths. |
765 | 765 |
/// |
766 | 766 |
///The type of the map that stores the predecessor |
767 | 767 |
///arcs of the %DFS paths. |
768 | 768 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
769 | 769 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
770 | 770 |
///Instantiates a PredMap. |
771 | 771 |
|
772 | 772 |
///This function instantiates a PredMap. |
773 | 773 |
///\param g is the digraph, to which we would like to define the |
774 | 774 |
///PredMap. |
775 | 775 |
static PredMap *createPredMap(const Digraph &g) |
776 | 776 |
{ |
777 | 777 |
return new PredMap(g); |
778 | 778 |
} |
779 | 779 |
|
780 | 780 |
///The type of the map that indicates which nodes are processed. |
781 | 781 |
|
782 | 782 |
///The type of the map that indicates which nodes are processed. |
783 | 783 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
784 | 784 |
///By default it is a NullMap. |
785 | 785 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
786 | 786 |
///Instantiates a ProcessedMap. |
787 | 787 |
|
788 | 788 |
///This function instantiates a ProcessedMap. |
789 | 789 |
///\param g is the digraph, to which |
790 | 790 |
///we would like to define the ProcessedMap. |
791 | 791 |
#ifdef DOXYGEN |
792 | 792 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
793 | 793 |
#else |
794 | 794 |
static ProcessedMap *createProcessedMap(const Digraph &) |
795 | 795 |
#endif |
796 | 796 |
{ |
797 | 797 |
return new ProcessedMap(); |
798 | 798 |
} |
799 | 799 |
|
800 | 800 |
///The type of the map that indicates which nodes are reached. |
801 | 801 |
|
802 | 802 |
///The type of the map that indicates which nodes are reached. |
803 | 803 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
804 | 804 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
805 | 805 |
///Instantiates a ReachedMap. |
806 | 806 |
|
807 | 807 |
///This function instantiates a ReachedMap. |
808 | 808 |
///\param g is the digraph, to which |
809 | 809 |
///we would like to define the ReachedMap. |
810 | 810 |
static ReachedMap *createReachedMap(const Digraph &g) |
811 | 811 |
{ |
812 | 812 |
return new ReachedMap(g); |
813 | 813 |
} |
814 | 814 |
|
815 | 815 |
///The type of the map that stores the distances of the nodes. |
816 | 816 |
|
817 | 817 |
///The type of the map that stores the distances of the nodes. |
818 | 818 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
819 | 819 |
typedef typename Digraph::template NodeMap<int> DistMap; |
820 | 820 |
///Instantiates a DistMap. |
821 | 821 |
|
822 | 822 |
///This function instantiates a DistMap. |
823 | 823 |
///\param g is the digraph, to which we would like to define |
824 | 824 |
///the DistMap |
825 | 825 |
static DistMap *createDistMap(const Digraph &g) |
826 | 826 |
{ |
827 | 827 |
return new DistMap(g); |
828 | 828 |
} |
829 | 829 |
|
830 | 830 |
///The type of the DFS paths. |
831 | 831 |
|
832 | 832 |
///The type of the DFS paths. |
833 | 833 |
///It must meet the \ref concepts::Path "Path" concept. |
834 | 834 |
typedef lemon::Path<Digraph> Path; |
835 | 835 |
}; |
836 | 836 |
|
837 | 837 |
/// Default traits class used by DfsWizard |
838 | 838 |
|
839 | 839 |
/// To make it easier to use Dfs algorithm |
840 | 840 |
/// we have created a wizard class. |
841 | 841 |
/// This \ref DfsWizard class needs default traits, |
842 | 842 |
/// as well as the \ref Dfs class. |
843 | 843 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
844 | 844 |
/// \ref DfsWizard class. |
845 | 845 |
template<class GR> |
846 | 846 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
847 | 847 |
{ |
848 | 848 |
|
849 | 849 |
typedef DfsWizardDefaultTraits<GR> Base; |
850 | 850 |
protected: |
851 | 851 |
//The type of the nodes in the digraph. |
852 | 852 |
typedef typename Base::Digraph::Node Node; |
853 | 853 |
|
854 | 854 |
//Pointer to the digraph the algorithm runs on. |
855 | 855 |
void *_g; |
856 | 856 |
//Pointer to the map of reached nodes. |
857 | 857 |
void *_reached; |
858 | 858 |
//Pointer to the map of processed nodes. |
859 | 859 |
void *_processed; |
860 | 860 |
//Pointer to the map of predecessors arcs. |
861 | 861 |
void *_pred; |
862 | 862 |
//Pointer to the map of distances. |
863 | 863 |
void *_dist; |
864 | 864 |
//Pointer to the DFS path to the target node. |
865 | 865 |
void *_path; |
866 | 866 |
//Pointer to the distance of the target node. |
867 | 867 |
int *_di; |
868 | 868 |
|
869 | 869 |
public: |
870 | 870 |
/// Constructor. |
871 | 871 |
|
872 | 872 |
/// This constructor does not require parameters, therefore it initiates |
873 | 873 |
/// all of the attributes to \c 0. |
874 | 874 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
875 | 875 |
_dist(0), _path(0), _di(0) {} |
876 | 876 |
|
877 | 877 |
/// Constructor. |
878 | 878 |
|
879 | 879 |
/// This constructor requires one parameter, |
880 | 880 |
/// others are initiated to \c 0. |
881 | 881 |
/// \param g The digraph the algorithm runs on. |
882 | 882 |
DfsWizardBase(const GR &g) : |
883 | 883 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
884 | 884 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
885 | 885 |
|
886 | 886 |
}; |
887 | 887 |
|
888 | 888 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
889 | 889 |
|
890 | 890 |
/// This auxiliary class is created to implement the |
891 | 891 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
892 | 892 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
893 | 893 |
/// functions and features of the plain \ref Dfs. |
894 | 894 |
/// |
895 | 895 |
/// This class should only be used through the \ref dfs() function, |
896 | 896 |
/// which makes it easier to use the algorithm. |
897 | 897 |
template<class TR> |
898 | 898 |
class DfsWizard : public TR |
899 | 899 |
{ |
900 | 900 |
typedef TR Base; |
901 | 901 |
|
902 | 902 |
///The type of the digraph the algorithm runs on. |
903 | 903 |
typedef typename TR::Digraph Digraph; |
904 | 904 |
|
905 | 905 |
typedef typename Digraph::Node Node; |
906 | 906 |
typedef typename Digraph::NodeIt NodeIt; |
907 | 907 |
typedef typename Digraph::Arc Arc; |
908 | 908 |
typedef typename Digraph::OutArcIt OutArcIt; |
909 | 909 |
|
910 | 910 |
///\brief The type of the map that stores the predecessor |
911 | 911 |
///arcs of the DFS paths. |
912 | 912 |
typedef typename TR::PredMap PredMap; |
913 | 913 |
///\brief The type of the map that stores the distances of the nodes. |
914 | 914 |
typedef typename TR::DistMap DistMap; |
915 | 915 |
///\brief The type of the map that indicates which nodes are reached. |
916 | 916 |
typedef typename TR::ReachedMap ReachedMap; |
917 | 917 |
///\brief The type of the map that indicates which nodes are processed. |
918 | 918 |
typedef typename TR::ProcessedMap ProcessedMap; |
919 | 919 |
///The type of the DFS paths |
920 | 920 |
typedef typename TR::Path Path; |
921 | 921 |
|
922 | 922 |
public: |
923 | 923 |
|
924 | 924 |
/// Constructor. |
925 | 925 |
DfsWizard() : TR() {} |
926 | 926 |
|
927 | 927 |
/// Constructor that requires parameters. |
928 | 928 |
|
929 | 929 |
/// Constructor that requires parameters. |
930 | 930 |
/// These parameters will be the default values for the traits class. |
931 | 931 |
/// \param g The digraph the algorithm runs on. |
932 | 932 |
DfsWizard(const Digraph &g) : |
933 | 933 |
TR(g) {} |
934 | 934 |
|
935 | 935 |
///Copy constructor |
936 | 936 |
DfsWizard(const TR &b) : TR(b) {} |
937 | 937 |
|
938 | 938 |
~DfsWizard() {} |
939 | 939 |
|
940 | 940 |
///Runs DFS algorithm from the given source node. |
941 | 941 |
|
942 | 942 |
///This method runs DFS algorithm from node \c s |
943 | 943 |
///in order to compute the DFS path to each node. |
944 | 944 |
void run(Node s) |
945 | 945 |
{ |
946 | 946 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
947 | 947 |
if (Base::_pred) |
948 | 948 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
949 | 949 |
if (Base::_dist) |
950 | 950 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
951 | 951 |
if (Base::_reached) |
952 | 952 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
953 | 953 |
if (Base::_processed) |
954 | 954 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
955 | 955 |
if (s!=INVALID) |
956 | 956 |
alg.run(s); |
957 | 957 |
else |
958 | 958 |
alg.run(); |
959 | 959 |
} |
960 | 960 |
|
961 | 961 |
///Finds the DFS path between \c s and \c t. |
962 | 962 |
|
963 | 963 |
///This method runs DFS algorithm from node \c s |
964 | 964 |
///in order to compute the DFS path to node \c t |
965 | 965 |
///(it stops searching when \c t is processed). |
966 | 966 |
/// |
967 | 967 |
///\return \c true if \c t is reachable form \c s. |
968 | 968 |
bool run(Node s, Node t) |
969 | 969 |
{ |
970 | 970 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
971 | 971 |
if (Base::_pred) |
972 | 972 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
973 | 973 |
if (Base::_dist) |
974 | 974 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
975 | 975 |
if (Base::_reached) |
976 | 976 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
977 | 977 |
if (Base::_processed) |
978 | 978 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
979 | 979 |
alg.run(s,t); |
980 | 980 |
if (Base::_path) |
981 | 981 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
982 | 982 |
if (Base::_di) |
983 | 983 |
*Base::_di = alg.dist(t); |
984 | 984 |
return alg.reached(t); |
985 | 985 |
} |
986 | 986 |
|
987 | 987 |
///Runs DFS algorithm to visit all nodes in the digraph. |
988 | 988 |
|
989 | 989 |
///This method runs DFS algorithm in order to compute |
990 | 990 |
///the DFS path to each node. |
991 | 991 |
void run() |
992 | 992 |
{ |
993 | 993 |
run(INVALID); |
994 | 994 |
} |
995 | 995 |
|
996 | 996 |
template<class T> |
997 | 997 |
struct SetPredMapBase : public Base { |
998 | 998 |
typedef T PredMap; |
999 | 999 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1000 | 1000 |
SetPredMapBase(const TR &b) : TR(b) {} |
1001 | 1001 |
}; |
1002 | 1002 |
///\brief \ref named-func-param "Named parameter" |
1003 | 1003 |
///for setting PredMap object. |
1004 | 1004 |
/// |
1005 | 1005 |
///\ref named-func-param "Named parameter" |
1006 | 1006 |
///for setting PredMap object. |
1007 | 1007 |
template<class T> |
1008 | 1008 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1009 | 1009 |
{ |
1010 | 1010 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1011 | 1011 |
return DfsWizard<SetPredMapBase<T> >(*this); |
1012 | 1012 |
} |
1013 | 1013 |
|
1014 | 1014 |
template<class T> |
1015 | 1015 |
struct SetReachedMapBase : public Base { |
1016 | 1016 |
typedef T ReachedMap; |
1017 | 1017 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1018 | 1018 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1019 | 1019 |
}; |
1020 | 1020 |
///\brief \ref named-func-param "Named parameter" |
1021 | 1021 |
///for setting ReachedMap object. |
1022 | 1022 |
/// |
1023 | 1023 |
/// \ref named-func-param "Named parameter" |
1024 | 1024 |
///for setting ReachedMap object. |
1025 | 1025 |
template<class T> |
1026 | 1026 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1027 | 1027 |
{ |
1028 | 1028 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1029 | 1029 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
1030 | 1030 |
} |
1031 | 1031 |
|
1032 | 1032 |
template<class T> |
1033 | 1033 |
struct SetDistMapBase : public Base { |
1034 | 1034 |
typedef T DistMap; |
1035 | 1035 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1036 | 1036 |
SetDistMapBase(const TR &b) : TR(b) {} |
1037 | 1037 |
}; |
1038 | 1038 |
///\brief \ref named-func-param "Named parameter" |
1039 | 1039 |
///for setting DistMap object. |
1040 | 1040 |
/// |
1041 | 1041 |
/// \ref named-func-param "Named parameter" |
1042 | 1042 |
///for setting DistMap object. |
1043 | 1043 |
template<class T> |
1044 | 1044 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1045 | 1045 |
{ |
1046 | 1046 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1047 | 1047 |
return DfsWizard<SetDistMapBase<T> >(*this); |
1048 | 1048 |
} |
1049 | 1049 |
|
1050 | 1050 |
template<class T> |
1051 | 1051 |
struct SetProcessedMapBase : public Base { |
1052 | 1052 |
typedef T ProcessedMap; |
1053 | 1053 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1054 | 1054 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1055 | 1055 |
}; |
1056 | 1056 |
///\brief \ref named-func-param "Named parameter" |
1057 | 1057 |
///for setting ProcessedMap object. |
1058 | 1058 |
/// |
1059 | 1059 |
/// \ref named-func-param "Named parameter" |
1060 | 1060 |
///for setting ProcessedMap object. |
1061 | 1061 |
template<class T> |
1062 | 1062 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1063 | 1063 |
{ |
1064 | 1064 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1065 | 1065 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
1066 | 1066 |
} |
1067 | 1067 |
|
1068 | 1068 |
template<class T> |
1069 | 1069 |
struct SetPathBase : public Base { |
1070 | 1070 |
typedef T Path; |
1071 | 1071 |
SetPathBase(const TR &b) : TR(b) {} |
1072 | 1072 |
}; |
1073 | 1073 |
///\brief \ref named-func-param "Named parameter" |
1074 | 1074 |
///for getting the DFS path to the target node. |
1075 | 1075 |
/// |
1076 | 1076 |
///\ref named-func-param "Named parameter" |
1077 | 1077 |
///for getting the DFS path to the target node. |
1078 | 1078 |
template<class T> |
1079 | 1079 |
DfsWizard<SetPathBase<T> > path(const T &t) |
1080 | 1080 |
{ |
1081 | 1081 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1082 | 1082 |
return DfsWizard<SetPathBase<T> >(*this); |
1083 | 1083 |
} |
1084 | 1084 |
|
1085 | 1085 |
///\brief \ref named-func-param "Named parameter" |
1086 | 1086 |
///for getting the distance of the target node. |
1087 | 1087 |
/// |
1088 | 1088 |
///\ref named-func-param "Named parameter" |
1089 | 1089 |
///for getting the distance of the target node. |
1090 | 1090 |
DfsWizard dist(const int &d) |
1091 | 1091 |
{ |
1092 | 1092 |
Base::_di=const_cast<int*>(&d); |
1093 | 1093 |
return *this; |
1094 | 1094 |
} |
1095 | 1095 |
|
1096 | 1096 |
}; |
1097 | 1097 |
|
1098 | 1098 |
///Function-type interface for DFS algorithm. |
1099 | 1099 |
|
1100 | 1100 |
///\ingroup search |
1101 | 1101 |
///Function-type interface for DFS algorithm. |
1102 | 1102 |
/// |
1103 | 1103 |
///This function also has several \ref named-func-param "named parameters", |
1104 | 1104 |
///they are declared as the members of class \ref DfsWizard. |
1105 | 1105 |
///The following examples show how to use these parameters. |
1106 | 1106 |
///\code |
1107 | 1107 |
/// // Compute the DFS tree |
1108 | 1108 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
1109 | 1109 |
/// |
1110 | 1110 |
/// // Compute the DFS path from s to t |
1111 | 1111 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
1112 | 1112 |
///\endcode |
1113 | 1113 |
///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()" |
1114 | 1114 |
///to the end of the parameter list. |
1115 | 1115 |
///\sa DfsWizard |
1116 | 1116 |
///\sa Dfs |
1117 | 1117 |
template<class GR> |
1118 | 1118 |
DfsWizard<DfsWizardBase<GR> > |
1119 | 1119 |
dfs(const GR &digraph) |
1120 | 1120 |
{ |
1121 | 1121 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
1122 | 1122 |
} |
1123 | 1123 |
|
1124 | 1124 |
#ifdef DOXYGEN |
1125 | 1125 |
/// \brief Visitor class for DFS. |
1126 | 1126 |
/// |
1127 | 1127 |
/// This class defines the interface of the DfsVisit events, and |
1128 | 1128 |
/// it could be the base of a real visitor class. |
1129 | 1129 |
template <typename GR> |
1130 | 1130 |
struct DfsVisitor { |
1131 | 1131 |
typedef GR Digraph; |
1132 | 1132 |
typedef typename Digraph::Arc Arc; |
1133 | 1133 |
typedef typename Digraph::Node Node; |
1134 | 1134 |
/// \brief Called for the source node of the DFS. |
1135 | 1135 |
/// |
1136 | 1136 |
/// This function is called for the source node of the DFS. |
1137 | 1137 |
void start(const Node& node) {} |
1138 | 1138 |
/// \brief Called when the source node is leaved. |
1139 | 1139 |
/// |
1140 | 1140 |
/// This function is called when the source node is leaved. |
1141 | 1141 |
void stop(const Node& node) {} |
1142 | 1142 |
/// \brief Called when a node is reached first time. |
1143 | 1143 |
/// |
1144 | 1144 |
/// This function is called when a node is reached first time. |
1145 | 1145 |
void reach(const Node& node) {} |
1146 | 1146 |
/// \brief Called when an arc reaches a new node. |
1147 | 1147 |
/// |
1148 | 1148 |
/// This function is called when the DFS finds an arc whose target node |
1149 | 1149 |
/// is not reached yet. |
1150 | 1150 |
void discover(const Arc& arc) {} |
1151 | 1151 |
/// \brief Called when an arc is examined but its target node is |
1152 | 1152 |
/// already discovered. |
1153 | 1153 |
/// |
1154 | 1154 |
/// This function is called when an arc is examined but its target node is |
1155 | 1155 |
/// already discovered. |
1156 | 1156 |
void examine(const Arc& arc) {} |
1157 | 1157 |
/// \brief Called when the DFS steps back from a node. |
1158 | 1158 |
/// |
1159 | 1159 |
/// This function is called when the DFS steps back from a node. |
1160 | 1160 |
void leave(const Node& node) {} |
1161 | 1161 |
/// \brief Called when the DFS steps back on an arc. |
1162 | 1162 |
/// |
1163 | 1163 |
/// This function is called when the DFS steps back on an arc. |
1164 | 1164 |
void backtrack(const Arc& arc) {} |
1165 | 1165 |
}; |
1166 | 1166 |
#else |
1167 | 1167 |
template <typename GR> |
1168 | 1168 |
struct DfsVisitor { |
1169 | 1169 |
typedef GR Digraph; |
1170 | 1170 |
typedef typename Digraph::Arc Arc; |
1171 | 1171 |
typedef typename Digraph::Node Node; |
1172 | 1172 |
void start(const Node&) {} |
1173 | 1173 |
void stop(const Node&) {} |
1174 | 1174 |
void reach(const Node&) {} |
1175 | 1175 |
void discover(const Arc&) {} |
1176 | 1176 |
void examine(const Arc&) {} |
1177 | 1177 |
void leave(const Node&) {} |
1178 | 1178 |
void backtrack(const Arc&) {} |
1179 | 1179 |
|
1180 | 1180 |
template <typename _Visitor> |
1181 | 1181 |
struct Constraints { |
1182 | 1182 |
void constraints() { |
1183 | 1183 |
Arc arc; |
1184 | 1184 |
Node node; |
1185 | 1185 |
visitor.start(node); |
1186 | 1186 |
visitor.stop(arc); |
1187 | 1187 |
visitor.reach(node); |
1188 | 1188 |
visitor.discover(arc); |
1189 | 1189 |
visitor.examine(arc); |
1190 | 1190 |
visitor.leave(node); |
1191 | 1191 |
visitor.backtrack(arc); |
1192 | 1192 |
} |
1193 | 1193 |
_Visitor& visitor; |
1194 | 1194 |
}; |
1195 | 1195 |
}; |
1196 | 1196 |
#endif |
1197 | 1197 |
|
1198 | 1198 |
/// \brief Default traits class of DfsVisit class. |
1199 | 1199 |
/// |
1200 | 1200 |
/// Default traits class of DfsVisit class. |
1201 | 1201 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1202 | 1202 |
template<class GR> |
1203 | 1203 |
struct DfsVisitDefaultTraits { |
1204 | 1204 |
|
1205 | 1205 |
/// \brief The type of the digraph the algorithm runs on. |
1206 | 1206 |
typedef GR Digraph; |
1207 | 1207 |
|
1208 | 1208 |
/// \brief The type of the map that indicates which nodes are reached. |
1209 | 1209 |
/// |
1210 | 1210 |
/// The type of the map that indicates which nodes are reached. |
1211 | 1211 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1212 | 1212 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1213 | 1213 |
|
1214 | 1214 |
/// \brief Instantiates a ReachedMap. |
1215 | 1215 |
/// |
1216 | 1216 |
/// This function instantiates a ReachedMap. |
1217 | 1217 |
/// \param digraph is the digraph, to which |
1218 | 1218 |
/// we would like to define the ReachedMap. |
1219 | 1219 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1220 | 1220 |
return new ReachedMap(digraph); |
1221 | 1221 |
} |
1222 | 1222 |
|
1223 | 1223 |
}; |
1224 | 1224 |
|
1225 | 1225 |
/// \ingroup search |
1226 | 1226 |
/// |
1227 | 1227 |
/// \brief DFS algorithm class with visitor interface. |
1228 | 1228 |
/// |
1229 | 1229 |
/// This class provides an efficient implementation of the DFS algorithm |
1230 | 1230 |
/// with visitor interface. |
1231 | 1231 |
/// |
1232 | 1232 |
/// The DfsVisit class provides an alternative interface to the Dfs |
1233 | 1233 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1234 | 1234 |
/// the member functions of the \c Visitor class on every DFS event. |
1235 | 1235 |
/// |
1236 | 1236 |
/// This interface of the DFS algorithm should be used in special cases |
1237 | 1237 |
/// when extra actions have to be performed in connection with certain |
1238 | 1238 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
1239 | 1239 |
/// instead. |
1240 | 1240 |
/// |
1241 | 1241 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1242 | 1242 |
/// The default type is \ref ListDigraph. |
1243 | 1243 |
/// The value of GR is not used directly by \ref DfsVisit, |
1244 | 1244 |
/// it is only passed to \ref DfsVisitDefaultTraits. |
1245 | 1245 |
/// \tparam VS The Visitor type that is used by the algorithm. |
1246 | 1246 |
/// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which |
1247 | 1247 |
/// does not observe the DFS events. If you want to observe the DFS |
1248 | 1248 |
/// events, you should implement your own visitor class. |
1249 | 1249 |
/// \tparam TR Traits class to set various data types used by the |
1250 | 1250 |
/// algorithm. The default traits class is |
1251 | 1251 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>". |
1252 | 1252 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
1253 | 1253 |
/// a DFS visit traits class. |
1254 | 1254 |
#ifdef DOXYGEN |
1255 | 1255 |
template <typename GR, typename VS, typename TR> |
1256 | 1256 |
#else |
1257 | 1257 |
template <typename GR = ListDigraph, |
1258 | 1258 |
typename VS = DfsVisitor<GR>, |
1259 | 1259 |
typename TR = DfsVisitDefaultTraits<GR> > |
1260 | 1260 |
#endif |
1261 | 1261 |
class DfsVisit { |
1262 | 1262 |
public: |
1263 | 1263 |
|
1264 | 1264 |
///The traits class. |
1265 | 1265 |
typedef TR Traits; |
1266 | 1266 |
|
1267 | 1267 |
///The type of the digraph the algorithm runs on. |
1268 | 1268 |
typedef typename Traits::Digraph Digraph; |
1269 | 1269 |
|
1270 | 1270 |
///The visitor type used by the algorithm. |
1271 | 1271 |
typedef VS Visitor; |
1272 | 1272 |
|
1273 | 1273 |
///The type of the map that indicates which nodes are reached. |
1274 | 1274 |
typedef typename Traits::ReachedMap ReachedMap; |
1275 | 1275 |
|
1276 | 1276 |
private: |
1277 | 1277 |
|
1278 | 1278 |
typedef typename Digraph::Node Node; |
1279 | 1279 |
typedef typename Digraph::NodeIt NodeIt; |
1280 | 1280 |
typedef typename Digraph::Arc Arc; |
1281 | 1281 |
typedef typename Digraph::OutArcIt OutArcIt; |
1282 | 1282 |
|
1283 | 1283 |
//Pointer to the underlying digraph. |
1284 | 1284 |
const Digraph *_digraph; |
1285 | 1285 |
//Pointer to the visitor object. |
1286 | 1286 |
Visitor *_visitor; |
1287 | 1287 |
//Pointer to the map of reached status of the nodes. |
1288 | 1288 |
ReachedMap *_reached; |
1289 | 1289 |
//Indicates if _reached is locally allocated (true) or not. |
1290 | 1290 |
bool local_reached; |
1291 | 1291 |
|
1292 | 1292 |
std::vector<typename Digraph::Arc> _stack; |
1293 | 1293 |
int _stack_head; |
1294 | 1294 |
|
1295 | 1295 |
//Creates the maps if necessary. |
1296 | 1296 |
void create_maps() { |
1297 | 1297 |
if(!_reached) { |
1298 | 1298 |
local_reached = true; |
1299 | 1299 |
_reached = Traits::createReachedMap(*_digraph); |
1300 | 1300 |
} |
1301 | 1301 |
} |
1302 | 1302 |
|
1303 | 1303 |
protected: |
1304 | 1304 |
|
1305 | 1305 |
DfsVisit() {} |
1306 | 1306 |
|
1307 | 1307 |
public: |
1308 | 1308 |
|
1309 | 1309 |
typedef DfsVisit Create; |
1310 | 1310 |
|
1311 | 1311 |
/// \name Named Template Parameters |
1312 | 1312 |
|
1313 | 1313 |
///@{ |
1314 | 1314 |
template <class T> |
1315 | 1315 |
struct SetReachedMapTraits : public Traits { |
1316 | 1316 |
typedef T ReachedMap; |
1317 | 1317 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1318 | 1318 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
1319 | 1319 |
return 0; // ignore warnings |
1320 | 1320 |
} |
1321 | 1321 |
}; |
1322 | 1322 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1323 | 1323 |
/// ReachedMap type. |
1324 | 1324 |
/// |
1325 | 1325 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1326 | 1326 |
template <class T> |
1327 | 1327 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
1328 | 1328 |
SetReachedMapTraits<T> > { |
1329 | 1329 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1330 | 1330 |
}; |
1331 | 1331 |
///@} |
1332 | 1332 |
|
1333 | 1333 |
public: |
1334 | 1334 |
|
1335 | 1335 |
/// \brief Constructor. |
1336 | 1336 |
/// |
1337 | 1337 |
/// Constructor. |
1338 | 1338 |
/// |
1339 | 1339 |
/// \param digraph The digraph the algorithm runs on. |
1340 | 1340 |
/// \param visitor The visitor object of the algorithm. |
1341 | 1341 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1342 | 1342 |
: _digraph(&digraph), _visitor(&visitor), |
1343 | 1343 |
_reached(0), local_reached(false) {} |
1344 | 1344 |
|
1345 | 1345 |
/// \brief Destructor. |
1346 | 1346 |
~DfsVisit() { |
1347 | 1347 |
if(local_reached) delete _reached; |
1348 | 1348 |
} |
1349 | 1349 |
|
1350 | 1350 |
/// \brief Sets the map that indicates which nodes are reached. |
1351 | 1351 |
/// |
1352 | 1352 |
/// Sets the map that indicates which nodes are reached. |
1353 | 1353 |
/// If you don't use this function before calling \ref run(Node) "run()" |
1354 | 1354 |
/// or \ref init(), an instance will be allocated automatically. |
1355 | 1355 |
/// The destructor deallocates this automatically allocated map, |
1356 | 1356 |
/// of course. |
1357 | 1357 |
/// \return <tt> (*this) </tt> |
1358 | 1358 |
DfsVisit &reachedMap(ReachedMap &m) { |
1359 | 1359 |
if(local_reached) { |
1360 | 1360 |
delete _reached; |
1361 | 1361 |
local_reached=false; |
1362 | 1362 |
} |
1363 | 1363 |
_reached = &m; |
1364 | 1364 |
return *this; |
1365 | 1365 |
} |
1366 | 1366 |
|
1367 | 1367 |
public: |
1368 | 1368 |
|
1369 | 1369 |
/// \name Execution Control |
1370 | 1370 |
/// The simplest way to execute the DFS algorithm is to use one of the |
1371 | 1371 |
/// member functions called \ref run(Node) "run()".\n |
1372 | 1372 |
/// If you need more control on the execution, first you have to call |
1373 | 1373 |
/// \ref init(), then you can add a source node with \ref addSource() |
1374 | 1374 |
/// and perform the actual computation with \ref start(). |
1375 | 1375 |
/// This procedure can be repeated if there are nodes that have not |
1376 | 1376 |
/// been reached. |
1377 | 1377 |
|
1378 | 1378 |
/// @{ |
1379 | 1379 |
|
1380 | 1380 |
/// \brief Initializes the internal data structures. |
1381 | 1381 |
/// |
1382 | 1382 |
/// Initializes the internal data structures. |
1383 | 1383 |
void init() { |
1384 | 1384 |
create_maps(); |
1385 | 1385 |
_stack.resize(countNodes(*_digraph)); |
1386 | 1386 |
_stack_head = -1; |
1387 | 1387 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1388 | 1388 |
_reached->set(u, false); |
1389 | 1389 |
} |
1390 | 1390 |
} |
1391 | 1391 |
|
1392 | 1392 |
/// \brief Adds a new source node. |
1393 | 1393 |
/// |
1394 | 1394 |
/// Adds a new source node to the set of nodes to be processed. |
1395 | 1395 |
/// |
1396 | 1396 |
/// \pre The stack must be empty. Otherwise the algorithm gives |
1397 | 1397 |
/// wrong results. (One of the outgoing arcs of all the source nodes |
1398 | 1398 |
/// except for the last one will not be visited and distances will |
1399 | 1399 |
/// also be wrong.) |
1400 | 1400 |
void addSource(Node s) |
1401 | 1401 |
{ |
1402 | 1402 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1403 | 1403 |
if(!(*_reached)[s]) { |
1404 | 1404 |
_reached->set(s,true); |
1405 | 1405 |
_visitor->start(s); |
1406 | 1406 |
_visitor->reach(s); |
1407 | 1407 |
Arc e; |
1408 | 1408 |
_digraph->firstOut(e, s); |
1409 | 1409 |
if (e != INVALID) { |
1410 | 1410 |
_stack[++_stack_head] = e; |
1411 | 1411 |
} else { |
1412 | 1412 |
_visitor->leave(s); |
1413 | 1413 |
_visitor->stop(s); |
1414 | 1414 |
} |
1415 | 1415 |
} |
1416 | 1416 |
} |
1417 | 1417 |
|
1418 | 1418 |
/// \brief Processes the next arc. |
1419 | 1419 |
/// |
1420 | 1420 |
/// Processes the next arc. |
1421 | 1421 |
/// |
1422 | 1422 |
/// \return The processed arc. |
1423 | 1423 |
/// |
1424 | 1424 |
/// \pre The stack must not be empty. |
1425 | 1425 |
Arc processNextArc() { |
1426 | 1426 |
Arc e = _stack[_stack_head]; |
1427 | 1427 |
Node m = _digraph->target(e); |
1428 | 1428 |
if(!(*_reached)[m]) { |
1429 | 1429 |
_visitor->discover(e); |
1430 | 1430 |
_visitor->reach(m); |
1431 | 1431 |
_reached->set(m, true); |
1432 | 1432 |
_digraph->firstOut(_stack[++_stack_head], m); |
1433 | 1433 |
} else { |
1434 | 1434 |
_visitor->examine(e); |
1435 | 1435 |
m = _digraph->source(e); |
1436 | 1436 |
_digraph->nextOut(_stack[_stack_head]); |
1437 | 1437 |
} |
1438 | 1438 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1439 | 1439 |
_visitor->leave(m); |
1440 | 1440 |
--_stack_head; |
1441 | 1441 |
if (_stack_head >= 0) { |
1442 | 1442 |
_visitor->backtrack(_stack[_stack_head]); |
1443 | 1443 |
m = _digraph->source(_stack[_stack_head]); |
1444 | 1444 |
_digraph->nextOut(_stack[_stack_head]); |
1445 | 1445 |
} else { |
1446 | 1446 |
_visitor->stop(m); |
1447 | 1447 |
} |
1448 | 1448 |
} |
1449 | 1449 |
return e; |
1450 | 1450 |
} |
1451 | 1451 |
|
1452 | 1452 |
/// \brief Next arc to be processed. |
1453 | 1453 |
/// |
1454 | 1454 |
/// Next arc to be processed. |
1455 | 1455 |
/// |
1456 | 1456 |
/// \return The next arc to be processed or INVALID if the stack is |
1457 | 1457 |
/// empty. |
1458 | 1458 |
Arc nextArc() const { |
1459 | 1459 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1460 | 1460 |
} |
1461 | 1461 |
|
1462 | 1462 |
/// \brief Returns \c false if there are nodes |
1463 | 1463 |
/// to be processed. |
1464 | 1464 |
/// |
1465 | 1465 |
/// Returns \c false if there are nodes |
1466 | 1466 |
/// to be processed in the queue (stack). |
1467 | 1467 |
bool emptyQueue() const { return _stack_head < 0; } |
1468 | 1468 |
|
1469 | 1469 |
/// \brief Returns the number of the nodes to be processed. |
1470 | 1470 |
/// |
1471 | 1471 |
/// Returns the number of the nodes to be processed in the queue (stack). |
1472 | 1472 |
int queueSize() const { return _stack_head + 1; } |
1473 | 1473 |
|
1474 | 1474 |
/// \brief Executes the algorithm. |
1475 | 1475 |
/// |
1476 | 1476 |
/// Executes the algorithm. |
1477 | 1477 |
/// |
1478 | 1478 |
/// This method runs the %DFS algorithm from the root node |
1479 | 1479 |
/// in order to compute the %DFS path to each node. |
1480 | 1480 |
/// |
1481 | 1481 |
/// The algorithm computes |
1482 | 1482 |
/// - the %DFS tree, |
1483 | 1483 |
/// - the distance of each node from the root in the %DFS tree. |
1484 | 1484 |
/// |
1485 | 1485 |
/// \pre init() must be called and a root node should be |
1486 | 1486 |
/// added with addSource() before using this function. |
1487 | 1487 |
/// |
1488 | 1488 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
1489 | 1489 |
/// \code |
1490 | 1490 |
/// while ( !d.emptyQueue() ) { |
1491 | 1491 |
/// d.processNextArc(); |
1492 | 1492 |
/// } |
1493 | 1493 |
/// \endcode |
1494 | 1494 |
void start() { |
1495 | 1495 |
while ( !emptyQueue() ) processNextArc(); |
1496 | 1496 |
} |
1497 | 1497 |
|
1498 | 1498 |
/// \brief Executes the algorithm until the given target node is reached. |
1499 | 1499 |
/// |
1500 | 1500 |
/// Executes the algorithm until the given target node is reached. |
1501 | 1501 |
/// |
1502 | 1502 |
/// This method runs the %DFS algorithm from the root node |
1503 | 1503 |
/// in order to compute the DFS path to \c t. |
1504 | 1504 |
/// |
1505 | 1505 |
/// The algorithm computes |
1506 | 1506 |
/// - the %DFS path to \c t, |
1507 | 1507 |
/// - the distance of \c t from the root in the %DFS tree. |
1508 | 1508 |
/// |
1509 | 1509 |
/// \pre init() must be called and a root node should be added |
1510 | 1510 |
/// with addSource() before using this function. |
1511 | 1511 |
void start(Node t) { |
1512 |
while ( !emptyQueue() && |
|
1512 |
while ( !emptyQueue() && !(*_reached)[t] ) |
|
1513 | 1513 |
processNextArc(); |
1514 | 1514 |
} |
1515 | 1515 |
|
1516 | 1516 |
/// \brief Executes the algorithm until a condition is met. |
1517 | 1517 |
/// |
1518 | 1518 |
/// Executes the algorithm until a condition is met. |
1519 | 1519 |
/// |
1520 | 1520 |
/// This method runs the %DFS algorithm from the root node |
1521 | 1521 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
1522 | 1522 |
/// |
1523 | 1523 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
1524 | 1524 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
1525 | 1525 |
/// |
1526 | 1526 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
1527 | 1527 |
/// \c INVALID if no such arc was found. |
1528 | 1528 |
/// |
1529 | 1529 |
/// \pre init() must be called and a root node should be added |
1530 | 1530 |
/// with addSource() before using this function. |
1531 | 1531 |
/// |
1532 | 1532 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
1533 | 1533 |
/// not a node map. |
1534 | 1534 |
template <typename AM> |
1535 | 1535 |
Arc start(const AM &am) { |
1536 | 1536 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
1537 | 1537 |
processNextArc(); |
1538 | 1538 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1539 | 1539 |
} |
1540 | 1540 |
|
1541 | 1541 |
/// \brief Runs the algorithm from the given source node. |
1542 | 1542 |
/// |
1543 | 1543 |
/// This method runs the %DFS algorithm from node \c s. |
1544 | 1544 |
/// in order to compute the DFS path to each node. |
1545 | 1545 |
/// |
1546 | 1546 |
/// The algorithm computes |
1547 | 1547 |
/// - the %DFS tree, |
1548 | 1548 |
/// - the distance of each node from the root in the %DFS tree. |
1549 | 1549 |
/// |
1550 | 1550 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
1551 | 1551 |
///\code |
1552 | 1552 |
/// d.init(); |
1553 | 1553 |
/// d.addSource(s); |
1554 | 1554 |
/// d.start(); |
1555 | 1555 |
///\endcode |
1556 | 1556 |
void run(Node s) { |
1557 | 1557 |
init(); |
1558 | 1558 |
addSource(s); |
1559 | 1559 |
start(); |
1560 | 1560 |
} |
1561 | 1561 |
|
1562 | 1562 |
/// \brief Finds the %DFS path between \c s and \c t. |
1563 | 1563 |
|
1564 | 1564 |
/// This method runs the %DFS algorithm from node \c s |
1565 | 1565 |
/// in order to compute the DFS path to node \c t |
1566 | 1566 |
/// (it stops searching when \c t is processed). |
1567 | 1567 |
/// |
1568 | 1568 |
/// \return \c true if \c t is reachable form \c s. |
1569 | 1569 |
/// |
1570 | 1570 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
1571 | 1571 |
/// just a shortcut of the following code. |
1572 | 1572 |
///\code |
1573 | 1573 |
/// d.init(); |
1574 | 1574 |
/// d.addSource(s); |
1575 | 1575 |
/// d.start(t); |
1576 | 1576 |
///\endcode |
1577 | 1577 |
bool run(Node s,Node t) { |
1578 | 1578 |
init(); |
1579 | 1579 |
addSource(s); |
1580 | 1580 |
start(t); |
1581 | 1581 |
return reached(t); |
1582 | 1582 |
} |
1583 | 1583 |
|
1584 | 1584 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1585 | 1585 |
|
1586 | 1586 |
/// This method runs the %DFS algorithm in order to |
1587 | 1587 |
/// compute the %DFS path to each node. |
1588 | 1588 |
/// |
1589 | 1589 |
/// The algorithm computes |
1590 | 1590 |
/// - the %DFS tree (forest), |
1591 | 1591 |
/// - the distance of each node from the root(s) in the %DFS tree. |
1592 | 1592 |
/// |
1593 | 1593 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
1594 | 1594 |
///\code |
1595 | 1595 |
/// d.init(); |
1596 | 1596 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
1597 | 1597 |
/// if (!d.reached(n)) { |
1598 | 1598 |
/// d.addSource(n); |
1599 | 1599 |
/// d.start(); |
1600 | 1600 |
/// } |
1601 | 1601 |
/// } |
1602 | 1602 |
///\endcode |
1603 | 1603 |
void run() { |
1604 | 1604 |
init(); |
1605 | 1605 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1606 | 1606 |
if (!reached(it)) { |
1607 | 1607 |
addSource(it); |
1608 | 1608 |
start(); |
1609 | 1609 |
} |
1610 | 1610 |
} |
1611 | 1611 |
} |
1612 | 1612 |
|
1613 | 1613 |
///@} |
1614 | 1614 |
|
1615 | 1615 |
/// \name Query Functions |
1616 | 1616 |
/// The results of the DFS algorithm can be obtained using these |
1617 | 1617 |
/// functions.\n |
1618 | 1618 |
/// Either \ref run(Node) "run()" or \ref start() should be called |
1619 | 1619 |
/// before using them. |
1620 | 1620 |
|
1621 | 1621 |
///@{ |
1622 | 1622 |
|
1623 | 1623 |
/// \brief Checks if a node is reached from the root(s). |
1624 | 1624 |
/// |
1625 | 1625 |
/// Returns \c true if \c v is reached from the root(s). |
1626 | 1626 |
/// |
1627 | 1627 |
/// \pre Either \ref run(Node) "run()" or \ref init() |
1628 | 1628 |
/// must be called before using this function. |
1629 | 1629 |
bool reached(Node v) const { return (*_reached)[v]; } |
1630 | 1630 |
|
1631 | 1631 |
///@} |
1632 | 1632 |
|
1633 | 1633 |
}; |
1634 | 1634 |
|
1635 | 1635 |
} //END OF NAMESPACE LEMON |
1636 | 1636 |
|
1637 | 1637 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<fstream> |
24 | 24 |
#include<sstream> |
25 | 25 |
#include<algorithm> |
26 | 26 |
#include<vector> |
27 | 27 |
|
28 | 28 |
#ifndef WIN32 |
29 | 29 |
#include<sys/time.h> |
30 | 30 |
#include<ctime> |
31 | 31 |
#else |
32 | 32 |
#include<lemon/bits/windows.h> |
33 | 33 |
#endif |
34 | 34 |
|
35 | 35 |
#include<lemon/math.h> |
36 | 36 |
#include<lemon/core.h> |
37 | 37 |
#include<lemon/dim2.h> |
38 | 38 |
#include<lemon/maps.h> |
39 | 39 |
#include<lemon/color.h> |
40 | 40 |
#include<lemon/bits/bezier.h> |
41 | 41 |
#include<lemon/error.h> |
42 | 42 |
|
43 | 43 |
|
44 | 44 |
///\ingroup eps_io |
45 | 45 |
///\file |
46 | 46 |
///\brief A well configurable tool for visualizing graphs |
47 | 47 |
|
48 | 48 |
namespace lemon { |
49 | 49 |
|
50 | 50 |
namespace _graph_to_eps_bits { |
51 | 51 |
template<class MT> |
52 | 52 |
class _NegY { |
53 | 53 |
public: |
54 | 54 |
typedef typename MT::Key Key; |
55 | 55 |
typedef typename MT::Value Value; |
56 | 56 |
const MT ↦ |
57 | 57 |
int yscale; |
58 | 58 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
59 | 59 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
60 | 60 |
}; |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
///Default traits class of GraphToEps |
64 | 64 |
|
65 | 65 |
///Default traits class of \ref GraphToEps. |
66 | 66 |
/// |
67 | 67 |
///\param GR is the type of the underlying graph. |
68 | 68 |
template<class GR> |
69 | 69 |
struct DefaultGraphToEpsTraits |
70 | 70 |
{ |
71 | 71 |
typedef GR Graph; |
72 | 72 |
typedef GR Digraph; |
73 | 73 |
typedef typename Graph::Node Node; |
74 | 74 |
typedef typename Graph::NodeIt NodeIt; |
75 | 75 |
typedef typename Graph::Arc Arc; |
76 | 76 |
typedef typename Graph::ArcIt ArcIt; |
77 | 77 |
typedef typename Graph::InArcIt InArcIt; |
78 | 78 |
typedef typename Graph::OutArcIt OutArcIt; |
79 | 79 |
|
80 | 80 |
|
81 | 81 |
const Graph &g; |
82 | 82 |
|
83 | 83 |
std::ostream& os; |
84 | 84 |
|
85 | 85 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
86 | 86 |
CoordsMapType _coords; |
87 | 87 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
88 | 88 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
89 | 89 |
|
90 | 90 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
91 | 91 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
92 | 92 |
|
93 | 93 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
94 | 94 |
|
95 | 95 |
double _arcWidthScale; |
96 | 96 |
|
97 | 97 |
double _nodeScale; |
98 | 98 |
double _xBorder, _yBorder; |
99 | 99 |
double _scale; |
100 | 100 |
double _nodeBorderQuotient; |
101 | 101 |
|
102 | 102 |
bool _drawArrows; |
103 | 103 |
double _arrowLength, _arrowWidth; |
104 | 104 |
|
105 | 105 |
bool _showNodes, _showArcs; |
106 | 106 |
|
107 | 107 |
bool _enableParallel; |
108 | 108 |
double _parArcDist; |
109 | 109 |
|
110 | 110 |
bool _showNodeText; |
111 | 111 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
112 | 112 |
double _nodeTextSize; |
113 | 113 |
|
114 | 114 |
bool _showNodePsText; |
115 | 115 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
116 | 116 |
char *_nodePsTextsPreamble; |
117 | 117 |
|
118 | 118 |
bool _undirected; |
119 | 119 |
|
120 | 120 |
bool _pleaseRemoveOsStream; |
121 | 121 |
|
122 | 122 |
bool _scaleToA4; |
123 | 123 |
|
124 | 124 |
std::string _title; |
125 | 125 |
std::string _copyright; |
126 | 126 |
|
127 | 127 |
enum NodeTextColorType |
128 | 128 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
129 | 129 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
130 | 130 |
|
131 | 131 |
bool _autoNodeScale; |
132 | 132 |
bool _autoArcWidthScale; |
133 | 133 |
|
134 | 134 |
bool _absoluteNodeSizes; |
135 | 135 |
bool _absoluteArcWidths; |
136 | 136 |
|
137 | 137 |
bool _negY; |
138 | 138 |
|
139 | 139 |
bool _preScale; |
140 | 140 |
///Constructor |
141 | 141 |
|
142 | 142 |
///Constructor |
143 | 143 |
///\param gr Reference to the graph to be printed. |
144 | 144 |
///\param ost Reference to the output stream. |
145 | 145 |
///By default it is <tt>std::cout</tt>. |
146 | 146 |
///\param pros If it is \c true, then the \c ostream referenced by \c os |
147 | 147 |
///will be explicitly deallocated by the destructor. |
148 | 148 |
DefaultGraphToEpsTraits(const GR &gr, std::ostream& ost = std::cout, |
149 | 149 |
bool pros = false) : |
150 | 150 |
g(gr), os(ost), |
151 | 151 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
152 | 152 |
_nodeColors(WHITE), _arcColors(BLACK), |
153 | 153 |
_arcWidths(1.0), _arcWidthScale(0.003), |
154 | 154 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
155 | 155 |
_nodeBorderQuotient(.1), |
156 | 156 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
157 | 157 |
_showNodes(true), _showArcs(true), |
158 | 158 |
_enableParallel(false), _parArcDist(1), |
159 | 159 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
160 | 160 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
161 | 161 |
_undirected(lemon::UndirectedTagIndicator<GR>::value), |
162 | 162 |
_pleaseRemoveOsStream(pros), _scaleToA4(false), |
163 | 163 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
164 | 164 |
_autoNodeScale(false), |
165 | 165 |
_autoArcWidthScale(false), |
166 | 166 |
_absoluteNodeSizes(false), |
167 | 167 |
_absoluteArcWidths(false), |
168 | 168 |
_negY(false), |
169 | 169 |
_preScale(true) |
170 | 170 |
{} |
171 | 171 |
}; |
172 | 172 |
|
173 | 173 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
174 | 174 |
|
175 | 175 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
176 | 176 |
/// |
177 | 177 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
178 | 178 |
template<class T> class GraphToEps : public T |
179 | 179 |
{ |
180 | 180 |
// Can't believe it is required by the C++ standard |
181 | 181 |
using T::g; |
182 | 182 |
using T::os; |
183 | 183 |
|
184 | 184 |
using T::_coords; |
185 | 185 |
using T::_nodeSizes; |
186 | 186 |
using T::_nodeShapes; |
187 | 187 |
using T::_nodeColors; |
188 | 188 |
using T::_arcColors; |
189 | 189 |
using T::_arcWidths; |
190 | 190 |
|
191 | 191 |
using T::_arcWidthScale; |
192 | 192 |
using T::_nodeScale; |
193 | 193 |
using T::_xBorder; |
194 | 194 |
using T::_yBorder; |
195 | 195 |
using T::_scale; |
196 | 196 |
using T::_nodeBorderQuotient; |
197 | 197 |
|
198 | 198 |
using T::_drawArrows; |
199 | 199 |
using T::_arrowLength; |
200 | 200 |
using T::_arrowWidth; |
201 | 201 |
|
202 | 202 |
using T::_showNodes; |
203 | 203 |
using T::_showArcs; |
204 | 204 |
|
205 | 205 |
using T::_enableParallel; |
206 | 206 |
using T::_parArcDist; |
207 | 207 |
|
208 | 208 |
using T::_showNodeText; |
209 | 209 |
using T::_nodeTexts; |
210 | 210 |
using T::_nodeTextSize; |
211 | 211 |
|
212 | 212 |
using T::_showNodePsText; |
213 | 213 |
using T::_nodePsTexts; |
214 | 214 |
using T::_nodePsTextsPreamble; |
215 | 215 |
|
216 | 216 |
using T::_undirected; |
217 | 217 |
|
218 | 218 |
using T::_pleaseRemoveOsStream; |
219 | 219 |
|
220 | 220 |
using T::_scaleToA4; |
221 | 221 |
|
222 | 222 |
using T::_title; |
223 | 223 |
using T::_copyright; |
224 | 224 |
|
225 | 225 |
using T::NodeTextColorType; |
226 | 226 |
using T::CUST_COL; |
227 | 227 |
using T::DIST_COL; |
228 | 228 |
using T::DIST_BW; |
229 | 229 |
using T::_nodeTextColorType; |
230 | 230 |
using T::_nodeTextColors; |
231 | 231 |
|
232 | 232 |
using T::_autoNodeScale; |
233 | 233 |
using T::_autoArcWidthScale; |
234 | 234 |
|
235 | 235 |
using T::_absoluteNodeSizes; |
236 | 236 |
using T::_absoluteArcWidths; |
237 | 237 |
|
238 | 238 |
|
239 | 239 |
using T::_negY; |
240 | 240 |
using T::_preScale; |
241 | 241 |
|
242 | 242 |
// dradnats ++C eht yb deriuqer si ti eveileb t'naC |
243 | 243 |
|
244 | 244 |
typedef typename T::Graph Graph; |
245 | 245 |
typedef typename T::Digraph Digraph; |
246 | 246 |
typedef typename Graph::Node Node; |
247 | 247 |
typedef typename Graph::NodeIt NodeIt; |
248 | 248 |
typedef typename Graph::Arc Arc; |
249 | 249 |
typedef typename Graph::ArcIt ArcIt; |
250 | 250 |
typedef typename Graph::InArcIt InArcIt; |
251 | 251 |
typedef typename Graph::OutArcIt OutArcIt; |
252 | 252 |
|
253 | 253 |
static const int INTERPOL_PREC; |
254 | 254 |
static const double A4HEIGHT; |
255 | 255 |
static const double A4WIDTH; |
256 | 256 |
static const double A4BORDER; |
257 | 257 |
|
258 | 258 |
bool dontPrint; |
259 | 259 |
|
260 | 260 |
public: |
261 | 261 |
///Node shapes |
262 | 262 |
|
263 | 263 |
///Node shapes. |
264 | 264 |
/// |
265 | 265 |
enum NodeShapes { |
266 | 266 |
/// = 0 |
267 | 267 |
///\image html nodeshape_0.png |
268 | 268 |
///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm |
269 | 269 |
CIRCLE=0, |
270 | 270 |
/// = 1 |
271 | 271 |
///\image html nodeshape_1.png |
272 | 272 |
///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm |
273 | 273 |
SQUARE=1, |
274 | 274 |
/// = 2 |
275 | 275 |
///\image html nodeshape_2.png |
276 | 276 |
///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm |
277 | 277 |
DIAMOND=2, |
278 | 278 |
/// = 3 |
279 | 279 |
///\image html nodeshape_3.png |
280 | 280 |
///\image latex nodeshape_3.eps "MALE shape (3)" width=2cm |
281 | 281 |
MALE=3, |
282 | 282 |
/// = 4 |
283 | 283 |
///\image html nodeshape_4.png |
284 | 284 |
///\image latex nodeshape_4.eps "FEMALE shape (4)" width=2cm |
285 | 285 |
FEMALE=4 |
286 | 286 |
}; |
287 | 287 |
|
288 | 288 |
private: |
289 | 289 |
class arcLess { |
290 | 290 |
const Graph &g; |
291 | 291 |
public: |
292 | 292 |
arcLess(const Graph &_g) : g(_g) {} |
293 | 293 |
bool operator()(Arc a,Arc b) const |
294 | 294 |
{ |
295 | 295 |
Node ai=std::min(g.source(a),g.target(a)); |
296 | 296 |
Node aa=std::max(g.source(a),g.target(a)); |
297 | 297 |
Node bi=std::min(g.source(b),g.target(b)); |
298 | 298 |
Node ba=std::max(g.source(b),g.target(b)); |
299 | 299 |
return ai<bi || |
300 | 300 |
(ai==bi && (aa < ba || |
301 | 301 |
(aa==ba && ai==g.source(a) && bi==g.target(b)))); |
302 | 302 |
} |
303 | 303 |
}; |
304 | 304 |
bool isParallel(Arc e,Arc f) const |
305 | 305 |
{ |
306 | 306 |
return (g.source(e)==g.source(f)&& |
307 | 307 |
g.target(e)==g.target(f)) || |
308 | 308 |
(g.source(e)==g.target(f)&& |
309 | 309 |
g.target(e)==g.source(f)); |
310 | 310 |
} |
311 | 311 |
template<class TT> |
312 | 312 |
static std::string psOut(const dim2::Point<TT> &p) |
313 | 313 |
{ |
314 | 314 |
std::ostringstream os; |
315 | 315 |
os << p.x << ' ' << p.y; |
316 | 316 |
return os.str(); |
317 | 317 |
} |
318 | 318 |
static std::string psOut(const Color &c) |
319 | 319 |
{ |
320 | 320 |
std::ostringstream os; |
321 | 321 |
os << c.red() << ' ' << c.green() << ' ' << c.blue(); |
322 | 322 |
return os.str(); |
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
public: |
326 | 326 |
GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
327 | 327 |
|
328 | 328 |
template<class X> struct CoordsTraits : public T { |
329 | 329 |
typedef X CoordsMapType; |
330 | 330 |
const X &_coords; |
331 | 331 |
CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
332 | 332 |
}; |
333 | 333 |
///Sets the map of the node coordinates |
334 | 334 |
|
335 | 335 |
///Sets the map of the node coordinates. |
336 | 336 |
///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or |
337 | 337 |
///\ref dim2::Point "dim2::Point<int>" values. |
338 | 338 |
template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
339 | 339 |
dontPrint=true; |
340 | 340 |
return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
341 | 341 |
} |
342 | 342 |
template<class X> struct NodeSizesTraits : public T { |
343 | 343 |
const X &_nodeSizes; |
344 | 344 |
NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
345 | 345 |
}; |
346 | 346 |
///Sets the map of the node sizes |
347 | 347 |
|
348 | 348 |
///Sets the map of the node sizes. |
349 | 349 |
///\param x must be a node map with \c double (or convertible) values. |
350 | 350 |
template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
351 | 351 |
{ |
352 | 352 |
dontPrint=true; |
353 | 353 |
return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
354 | 354 |
} |
355 | 355 |
template<class X> struct NodeShapesTraits : public T { |
356 | 356 |
const X &_nodeShapes; |
357 | 357 |
NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} |
358 | 358 |
}; |
359 | 359 |
///Sets the map of the node shapes |
360 | 360 |
|
361 | 361 |
///Sets the map of the node shapes. |
362 | 362 |
///The available shape values |
363 | 363 |
///can be found in \ref NodeShapes "enum NodeShapes". |
364 | 364 |
///\param x must be a node map with \c int (or convertible) values. |
365 | 365 |
///\sa NodeShapes |
366 | 366 |
template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
367 | 367 |
{ |
368 | 368 |
dontPrint=true; |
369 | 369 |
return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
370 | 370 |
} |
371 | 371 |
template<class X> struct NodeTextsTraits : public T { |
372 | 372 |
const X &_nodeTexts; |
373 | 373 |
NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
374 | 374 |
}; |
375 | 375 |
///Sets the text printed on the nodes |
376 | 376 |
|
377 | 377 |
///Sets the text printed on the nodes. |
378 | 378 |
///\param x must be a node map with type that can be pushed to a standard |
379 | 379 |
///\c ostream. |
380 | 380 |
template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
381 | 381 |
{ |
382 | 382 |
dontPrint=true; |
383 | 383 |
_showNodeText=true; |
384 | 384 |
return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
385 | 385 |
} |
386 | 386 |
template<class X> struct NodePsTextsTraits : public T { |
387 | 387 |
const X &_nodePsTexts; |
388 | 388 |
NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} |
389 | 389 |
}; |
390 | 390 |
///Inserts a PostScript block to the nodes |
391 | 391 |
|
392 | 392 |
///With this command it is possible to insert a verbatim PostScript |
393 | 393 |
///block to the nodes. |
394 | 394 |
///The PS current point will be moved to the center of the node before |
395 | 395 |
///the PostScript block inserted. |
396 | 396 |
/// |
397 | 397 |
///Before and after the block a newline character is inserted so you |
398 | 398 |
///don't have to bother with the separators. |
399 | 399 |
/// |
400 | 400 |
///\param x must be a node map with type that can be pushed to a standard |
401 | 401 |
///\c ostream. |
402 | 402 |
/// |
403 | 403 |
///\sa nodePsTextsPreamble() |
404 | 404 |
template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
405 | 405 |
{ |
406 | 406 |
dontPrint=true; |
407 | 407 |
_showNodePsText=true; |
408 | 408 |
return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
409 | 409 |
} |
410 | 410 |
template<class X> struct ArcWidthsTraits : public T { |
411 | 411 |
const X &_arcWidths; |
412 | 412 |
ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} |
413 | 413 |
}; |
414 | 414 |
///Sets the map of the arc widths |
415 | 415 |
|
416 | 416 |
///Sets the map of the arc widths. |
417 | 417 |
///\param x must be an arc map with \c double (or convertible) values. |
418 | 418 |
template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x) |
419 | 419 |
{ |
420 | 420 |
dontPrint=true; |
421 | 421 |
return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x)); |
422 | 422 |
} |
423 | 423 |
|
424 | 424 |
template<class X> struct NodeColorsTraits : public T { |
425 | 425 |
const X &_nodeColors; |
426 | 426 |
NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
427 | 427 |
}; |
428 | 428 |
///Sets the map of the node colors |
429 | 429 |
|
430 | 430 |
///Sets the map of the node colors. |
431 | 431 |
///\param x must be a node map with \ref Color values. |
432 | 432 |
/// |
433 | 433 |
///\sa Palette |
434 | 434 |
template<class X> GraphToEps<NodeColorsTraits<X> > |
435 | 435 |
nodeColors(const X &x) |
436 | 436 |
{ |
437 | 437 |
dontPrint=true; |
438 | 438 |
return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
439 | 439 |
} |
440 | 440 |
template<class X> struct NodeTextColorsTraits : public T { |
441 | 441 |
const X &_nodeTextColors; |
442 | 442 |
NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} |
443 | 443 |
}; |
444 | 444 |
///Sets the map of the node text colors |
445 | 445 |
|
446 | 446 |
///Sets the map of the node text colors. |
447 | 447 |
///\param x must be a node map with \ref Color values. |
448 | 448 |
/// |
449 | 449 |
///\sa Palette |
450 | 450 |
template<class X> GraphToEps<NodeTextColorsTraits<X> > |
451 | 451 |
nodeTextColors(const X &x) |
452 | 452 |
{ |
453 | 453 |
dontPrint=true; |
454 | 454 |
_nodeTextColorType=CUST_COL; |
455 | 455 |
return GraphToEps<NodeTextColorsTraits<X> > |
456 | 456 |
(NodeTextColorsTraits<X>(*this,x)); |
457 | 457 |
} |
458 | 458 |
template<class X> struct ArcColorsTraits : public T { |
459 | 459 |
const X &_arcColors; |
460 | 460 |
ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} |
461 | 461 |
}; |
462 | 462 |
///Sets the map of the arc colors |
463 | 463 |
|
464 | 464 |
///Sets the map of the arc colors. |
465 | 465 |
///\param x must be an arc map with \ref Color values. |
466 | 466 |
/// |
467 | 467 |
///\sa Palette |
468 | 468 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
469 | 469 |
arcColors(const X &x) |
470 | 470 |
{ |
471 | 471 |
dontPrint=true; |
472 | 472 |
return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x)); |
473 | 473 |
} |
474 | 474 |
///Sets a global scale factor for node sizes |
475 | 475 |
|
476 | 476 |
///Sets a global scale factor for node sizes. |
477 | 477 |
/// |
478 | 478 |
/// If nodeSizes() is not given, this function simply sets the node |
479 | 479 |
/// sizes to \c d. If nodeSizes() is given, but |
480 | 480 |
/// autoNodeScale() is not, then the node size given by |
481 | 481 |
/// nodeSizes() will be multiplied by the value \c d. |
482 | 482 |
/// If both nodeSizes() and autoNodeScale() are used, then the |
483 | 483 |
/// node sizes will be scaled in such a way that the greatest size will be |
484 | 484 |
/// equal to \c d. |
485 | 485 |
/// \sa nodeSizes() |
486 | 486 |
/// \sa autoNodeScale() |
487 | 487 |
GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;} |
488 | 488 |
///Turns on/off the automatic node size scaling. |
489 | 489 |
|
490 | 490 |
///Turns on/off the automatic node size scaling. |
491 | 491 |
/// |
492 | 492 |
///\sa nodeScale() |
493 | 493 |
/// |
494 | 494 |
GraphToEps<T> &autoNodeScale(bool b=true) { |
495 | 495 |
_autoNodeScale=b;return *this; |
496 | 496 |
} |
497 | 497 |
|
498 | 498 |
///Turns on/off the absolutematic node size scaling. |
499 | 499 |
|
500 | 500 |
///Turns on/off the absolutematic node size scaling. |
501 | 501 |
/// |
502 | 502 |
///\sa nodeScale() |
503 | 503 |
/// |
504 | 504 |
GraphToEps<T> &absoluteNodeSizes(bool b=true) { |
505 | 505 |
_absoluteNodeSizes=b;return *this; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
///Negates the Y coordinates. |
509 | 509 |
GraphToEps<T> &negateY(bool b=true) { |
510 | 510 |
_negY=b;return *this; |
511 | 511 |
} |
512 | 512 |
|
513 | 513 |
///Turn on/off pre-scaling |
514 | 514 |
|
515 | 515 |
///By default graphToEps() rescales the whole image in order to avoid |
516 | 516 |
///very big or very small bounding boxes. |
517 | 517 |
/// |
518 | 518 |
///This (p)rescaling can be turned off with this function. |
519 | 519 |
/// |
520 | 520 |
GraphToEps<T> &preScale(bool b=true) { |
521 | 521 |
_preScale=b;return *this; |
522 | 522 |
} |
523 | 523 |
|
524 | 524 |
///Sets a global scale factor for arc widths |
525 | 525 |
|
526 | 526 |
/// Sets a global scale factor for arc widths. |
527 | 527 |
/// |
528 | 528 |
/// If arcWidths() is not given, this function simply sets the arc |
529 | 529 |
/// widths to \c d. If arcWidths() is given, but |
530 | 530 |
/// autoArcWidthScale() is not, then the arc withs given by |
531 | 531 |
/// arcWidths() will be multiplied by the value \c d. |
532 | 532 |
/// If both arcWidths() and autoArcWidthScale() are used, then the |
533 | 533 |
/// arc withs will be scaled in such a way that the greatest width will be |
534 | 534 |
/// equal to \c d. |
535 | 535 |
GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} |
536 | 536 |
///Turns on/off the automatic arc width scaling. |
537 | 537 |
|
538 | 538 |
///Turns on/off the automatic arc width scaling. |
539 | 539 |
/// |
540 | 540 |
///\sa arcWidthScale() |
541 | 541 |
/// |
542 | 542 |
GraphToEps<T> &autoArcWidthScale(bool b=true) { |
543 | 543 |
_autoArcWidthScale=b;return *this; |
544 | 544 |
} |
545 | 545 |
///Turns on/off the absolutematic arc width scaling. |
546 | 546 |
|
547 | 547 |
///Turns on/off the absolutematic arc width scaling. |
548 | 548 |
/// |
549 | 549 |
///\sa arcWidthScale() |
550 | 550 |
/// |
551 | 551 |
GraphToEps<T> &absoluteArcWidths(bool b=true) { |
552 | 552 |
_absoluteArcWidths=b;return *this; |
553 | 553 |
} |
554 | 554 |
///Sets a global scale factor for the whole picture |
555 | 555 |
GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
556 | 556 |
///Sets the width of the border around the picture |
557 | 557 |
GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;} |
558 | 558 |
///Sets the width of the border around the picture |
559 | 559 |
GraphToEps<T> &border(double x, double y) { |
560 | 560 |
_xBorder=x;_yBorder=y;return *this; |
561 | 561 |
} |
562 | 562 |
///Sets whether to draw arrows |
563 | 563 |
GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
564 | 564 |
///Sets the length of the arrowheads |
565 | 565 |
GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;} |
566 | 566 |
///Sets the width of the arrowheads |
567 | 567 |
GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;} |
568 | 568 |
|
569 | 569 |
///Scales the drawing to fit to A4 page |
570 | 570 |
GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;} |
571 | 571 |
|
572 | 572 |
///Enables parallel arcs |
573 | 573 |
GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
574 | 574 |
|
575 | 575 |
///Sets the distance between parallel arcs |
576 | 576 |
GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;} |
577 | 577 |
|
578 | 578 |
///Hides the arcs |
579 | 579 |
GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;} |
580 | 580 |
///Hides the nodes |
581 | 581 |
GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
582 | 582 |
|
583 | 583 |
///Sets the size of the node texts |
584 | 584 |
GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
585 | 585 |
|
586 | 586 |
///Sets the color of the node texts to be different from the node color |
587 | 587 |
|
588 | 588 |
///Sets the color of the node texts to be as different from the node color |
589 | 589 |
///as it is possible. |
590 | 590 |
GraphToEps<T> &distantColorNodeTexts() |
591 | 591 |
{_nodeTextColorType=DIST_COL;return *this;} |
592 | 592 |
///Sets the color of the node texts to be black or white and always visible. |
593 | 593 |
|
594 | 594 |
///Sets the color of the node texts to be black or white according to |
595 | 595 |
///which is more different from the node color. |
596 | 596 |
GraphToEps<T> &distantBWNodeTexts() |
597 | 597 |
{_nodeTextColorType=DIST_BW;return *this;} |
598 | 598 |
|
599 | 599 |
///Gives a preamble block for node Postscript block. |
600 | 600 |
|
601 | 601 |
///Gives a preamble block for node Postscript block. |
602 | 602 |
/// |
603 | 603 |
///\sa nodePsTexts() |
604 | 604 |
GraphToEps<T> & nodePsTextsPreamble(const char *str) { |
605 | 605 |
_nodePsTextsPreamble=str ;return *this; |
606 | 606 |
} |
607 | 607 |
///Sets whether the graph is undirected |
608 | 608 |
|
609 | 609 |
///Sets whether the graph is undirected. |
610 | 610 |
/// |
611 | 611 |
///This setting is the default for undirected graphs. |
612 | 612 |
/// |
613 | 613 |
///\sa directed() |
614 | 614 |
GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;} |
615 | 615 |
|
616 | 616 |
///Sets whether the graph is directed |
617 | 617 |
|
618 | 618 |
///Sets whether the graph is directed. |
619 | 619 |
///Use it to show the edges as a pair of directed ones. |
620 | 620 |
/// |
621 | 621 |
///This setting is the default for digraphs. |
622 | 622 |
/// |
623 | 623 |
///\sa undirected() |
624 | 624 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;} |
625 | 625 |
|
626 | 626 |
///Sets the title. |
627 | 627 |
|
628 | 628 |
///Sets the title of the generated image, |
629 | 629 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
630 | 630 |
///the EPS file. |
631 | 631 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;} |
632 | 632 |
///Sets the copyright statement. |
633 | 633 |
|
634 | 634 |
///Sets the copyright statement of the generated image, |
635 | 635 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
636 | 636 |
///the EPS file. |
637 | 637 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;} |
638 | 638 |
|
639 | 639 |
protected: |
640 | 640 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
641 | 641 |
{ |
642 | 642 |
switch(t) { |
643 | 643 |
case CIRCLE: |
644 | 644 |
case MALE: |
645 | 645 |
case FEMALE: |
646 | 646 |
return p.normSquare()<=r*r; |
647 | 647 |
case SQUARE: |
648 | 648 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
649 | 649 |
case DIAMOND: |
650 | 650 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
651 | 651 |
} |
652 | 652 |
return false; |
653 | 653 |
} |
654 | 654 |
|
655 | 655 |
public: |
656 | 656 |
~GraphToEps() { } |
657 | 657 |
|
658 | 658 |
///Draws the graph. |
659 | 659 |
|
660 | 660 |
///Like other functions using |
661 | 661 |
///\ref named-templ-func-param "named template parameters", |
662 | 662 |
///this function calls the algorithm itself, i.e. in this case |
663 | 663 |
///it draws the graph. |
664 | 664 |
void run() { |
665 | 665 |
const double EPSILON=1e-9; |
666 | 666 |
if(dontPrint) return; |
667 | 667 |
|
668 | 668 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
669 | 669 |
mycoords(_coords,_negY); |
670 | 670 |
|
671 | 671 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
672 | 672 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
673 | 673 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
674 | 674 |
os << "%%Creator: LEMON, graphToEps()\n"; |
675 | 675 |
|
676 | 676 |
{ |
677 | 677 |
os << "%%CreationDate: "; |
678 | 678 |
#ifndef WIN32 |
679 | 679 |
timeval tv; |
680 | 680 |
gettimeofday(&tv, 0); |
681 | 681 |
|
682 | 682 |
char cbuf[26]; |
683 | 683 |
ctime_r(&tv.tv_sec,cbuf); |
684 | 684 |
os << cbuf; |
685 | 685 |
#else |
686 | 686 |
os << bits::getWinFormattedDate(); |
687 |
os << std::endl; |
|
687 | 688 |
#endif |
688 | 689 |
} |
689 |
os << std::endl; |
|
690 | 690 |
|
691 | 691 |
if (_autoArcWidthScale) { |
692 | 692 |
double max_w=0; |
693 | 693 |
for(ArcIt e(g);e!=INVALID;++e) |
694 | 694 |
max_w=std::max(double(_arcWidths[e]),max_w); |
695 | 695 |
if(max_w>EPSILON) { |
696 | 696 |
_arcWidthScale/=max_w; |
697 | 697 |
} |
698 | 698 |
} |
699 | 699 |
|
700 | 700 |
if (_autoNodeScale) { |
701 | 701 |
double max_s=0; |
702 | 702 |
for(NodeIt n(g);n!=INVALID;++n) |
703 | 703 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
704 | 704 |
if(max_s>EPSILON) { |
705 | 705 |
_nodeScale/=max_s; |
706 | 706 |
} |
707 | 707 |
} |
708 | 708 |
|
709 | 709 |
double diag_len = 1; |
710 | 710 |
if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { |
711 | 711 |
dim2::Box<double> bb; |
712 | 712 |
for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
713 | 713 |
if (bb.empty()) { |
714 | 714 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
715 | 715 |
} |
716 | 716 |
diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
717 | 717 |
if(diag_len<EPSILON) diag_len = 1; |
718 | 718 |
if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
719 | 719 |
if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
720 | 720 |
} |
721 | 721 |
|
722 | 722 |
dim2::Box<double> bb; |
723 | 723 |
for(NodeIt n(g);n!=INVALID;++n) { |
724 | 724 |
double ns=_nodeSizes[n]*_nodeScale; |
725 | 725 |
dim2::Point<double> p(ns,ns); |
726 | 726 |
switch(_nodeShapes[n]) { |
727 | 727 |
case CIRCLE: |
728 | 728 |
case SQUARE: |
729 | 729 |
case DIAMOND: |
730 | 730 |
bb.add(p+mycoords[n]); |
731 | 731 |
bb.add(-p+mycoords[n]); |
732 | 732 |
break; |
733 | 733 |
case MALE: |
734 | 734 |
bb.add(-p+mycoords[n]); |
735 | 735 |
bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
736 | 736 |
break; |
737 | 737 |
case FEMALE: |
738 | 738 |
bb.add(p+mycoords[n]); |
739 | 739 |
bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
740 | 740 |
break; |
741 | 741 |
} |
742 | 742 |
} |
743 | 743 |
if (bb.empty()) { |
744 | 744 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
745 | 745 |
} |
746 | 746 |
|
747 | 747 |
if(_scaleToA4) |
748 | 748 |
os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
749 | 749 |
else { |
750 | 750 |
if(_preScale) { |
751 | 751 |
//Rescale so that BoundingBox won't be neither to big nor too small. |
752 | 752 |
while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; |
753 | 753 |
while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; |
754 | 754 |
} |
755 | 755 |
|
756 | 756 |
os << "%%BoundingBox: " |
757 | 757 |
<< int(floor(bb.left() * _scale - _xBorder)) << ' ' |
758 | 758 |
<< int(floor(bb.bottom() * _scale - _yBorder)) << ' ' |
759 | 759 |
<< int(ceil(bb.right() * _scale + _xBorder)) << ' ' |
760 | 760 |
<< int(ceil(bb.top() * _scale + _yBorder)) << '\n'; |
761 | 761 |
} |
762 | 762 |
|
763 | 763 |
os << "%%EndComments\n"; |
764 | 764 |
|
765 | 765 |
//x1 y1 x2 y2 x3 y3 cr cg cb w |
766 | 766 |
os << "/lb { setlinewidth setrgbcolor newpath moveto\n" |
767 | 767 |
<< " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
768 | 768 |
os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }" |
769 | 769 |
<< " bind def\n"; |
770 | 770 |
//x y r |
771 | 771 |
os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }" |
772 | 772 |
<< " bind def\n"; |
773 | 773 |
//x y r |
774 | 774 |
os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" |
775 | 775 |
<< " 2 index 1 index sub 2 index 2 index add lineto\n" |
776 | 776 |
<< " 2 index 1 index sub 2 index 2 index sub lineto\n" |
777 | 777 |
<< " 2 index 1 index add 2 index 2 index sub lineto\n" |
778 | 778 |
<< " closepath pop pop pop} bind def\n"; |
779 | 779 |
//x y r |
780 | 780 |
os << "/di { newpath 2 index 1 index add 2 index moveto\n" |
781 | 781 |
<< " 2 index 2 index 2 index add lineto\n" |
782 | 782 |
<< " 2 index 1 index sub 2 index lineto\n" |
783 | 783 |
<< " 2 index 2 index 2 index sub lineto\n" |
784 | 784 |
<< " closepath pop pop pop} bind def\n"; |
785 | 785 |
// x y r cr cg cb |
786 | 786 |
os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n" |
787 | 787 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
788 | 788 |
<< " } bind def\n"; |
789 | 789 |
os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n" |
790 | 790 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" |
791 | 791 |
<< " } bind def\n"; |
792 | 792 |
os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n" |
793 | 793 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" |
794 | 794 |
<< " } bind def\n"; |
795 | 795 |
os << "/nfemale { 0 0 0 setrgbcolor 3 index " |
796 | 796 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
797 | 797 |
<< " 1.5 mul mul setlinewidth\n" |
798 | 798 |
<< " newpath 5 index 5 index moveto " |
799 | 799 |
<< "5 index 5 index 5 index 3.01 mul sub\n" |
800 | 800 |
<< " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub" |
801 | 801 |
<< " moveto\n" |
802 | 802 |
<< " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto " |
803 | 803 |
<< "stroke\n" |
804 | 804 |
<< " 5 index 5 index 5 index c fill\n" |
805 | 805 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
806 | 806 |
<< " } bind def\n"; |
807 | 807 |
os << "/nmale {\n" |
808 | 808 |
<< " 0 0 0 setrgbcolor 3 index " |
809 | 809 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
810 | 810 |
<<" 1.5 mul mul setlinewidth\n" |
811 | 811 |
<< " newpath 5 index 5 index moveto\n" |
812 | 812 |
<< " 5 index 4 index 1 mul 1.5 mul add\n" |
813 | 813 |
<< " 5 index 5 index 3 sqrt 1.5 mul mul add\n" |
814 | 814 |
<< " 1 index 1 index lineto\n" |
815 | 815 |
<< " 1 index 1 index 7 index sub moveto\n" |
816 | 816 |
<< " 1 index 1 index lineto\n" |
817 | 817 |
<< " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub" |
818 | 818 |
<< " lineto\n" |
819 | 819 |
<< " stroke\n" |
820 | 820 |
<< " 5 index 5 index 5 index c fill\n" |
821 | 821 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
822 | 822 |
<< " } bind def\n"; |
823 | 823 |
|
824 | 824 |
|
825 | 825 |
os << "/arrl " << _arrowLength << " def\n"; |
826 | 826 |
os << "/arrw " << _arrowWidth << " def\n"; |
827 | 827 |
// l dx_norm dy_norm |
828 | 828 |
os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
829 | 829 |
//len w dx_norm dy_norm x1 y1 cr cg cb |
830 | 830 |
os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx " |
831 | 831 |
<< "exch def\n" |
832 | 832 |
<< " /w exch def /len exch def\n" |
833 | 833 |
//<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
834 | 834 |
<< " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
835 | 835 |
<< " len w sub arrl sub dx dy lrl\n" |
836 | 836 |
<< " arrw dy dx neg lrl\n" |
837 | 837 |
<< " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
838 | 838 |
<< " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
839 | 839 |
<< " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
840 | 840 |
<< " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
841 | 841 |
<< " arrw dy dx neg lrl\n" |
842 | 842 |
<< " len w sub arrl sub neg dx dy lrl\n" |
843 | 843 |
<< " closepath fill } bind def\n"; |
844 | 844 |
os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
845 | 845 |
<< " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
846 | 846 |
|
847 | 847 |
os << "\ngsave\n"; |
848 | 848 |
if(_scaleToA4) |
849 | 849 |
if(bb.height()>bb.width()) { |
850 | 850 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), |
851 | 851 |
(A4WIDTH-2*A4BORDER)/bb.width()); |
852 | 852 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' |
853 | 853 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER |
854 | 854 |
<< " translate\n" |
855 | 855 |
<< sc << " dup scale\n" |
856 | 856 |
<< -bb.left() << ' ' << -bb.bottom() << " translate\n"; |
857 | 857 |
} |
858 | 858 |
else { |
859 | 859 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), |
860 | 860 |
(A4WIDTH-2*A4BORDER)/bb.height()); |
861 | 861 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' |
862 | 862 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER |
863 | 863 |
<< " translate\n" |
864 | 864 |
<< sc << " dup scale\n90 rotate\n" |
865 | 865 |
<< -bb.left() << ' ' << -bb.top() << " translate\n"; |
866 | 866 |
} |
867 | 867 |
else if(_scale!=1.0) os << _scale << " dup scale\n"; |
868 | 868 |
|
869 | 869 |
if(_showArcs) { |
870 | 870 |
os << "%Arcs:\ngsave\n"; |
871 | 871 |
if(_enableParallel) { |
872 | 872 |
std::vector<Arc> el; |
873 | 873 |
for(ArcIt e(g);e!=INVALID;++e) |
874 | 874 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
875 | 875 |
&&g.source(e)!=g.target(e)) |
876 | 876 |
el.push_back(e); |
877 | 877 |
std::sort(el.begin(),el.end(),arcLess(g)); |
878 | 878 |
|
879 | 879 |
typename std::vector<Arc>::iterator j; |
880 | 880 |
for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) { |
881 | 881 |
for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
882 | 882 |
|
883 | 883 |
double sw=0; |
884 | 884 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) |
885 | 885 |
sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; |
886 | 886 |
sw-=_parArcDist; |
887 | 887 |
sw/=-2.0; |
888 | 888 |
dim2::Point<double> |
889 | 889 |
dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); |
890 | 890 |
double l=std::sqrt(dvec.normSquare()); |
891 | 891 |
dim2::Point<double> d(dvec/std::max(l,EPSILON)); |
892 | 892 |
dim2::Point<double> m; |
893 | 893 |
// m=dim2::Point<double>(mycoords[g.target(*i)]+ |
894 | 894 |
// mycoords[g.source(*i)])/2.0; |
895 | 895 |
|
896 | 896 |
// m=dim2::Point<double>(mycoords[g.source(*i)])+ |
897 | 897 |
// dvec*(double(_nodeSizes[g.source(*i)])/ |
898 | 898 |
// (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
899 | 899 |
|
900 | 900 |
m=dim2::Point<double>(mycoords[g.source(*i)])+ |
901 | 901 |
d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
902 | 902 |
|
903 | 903 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) { |
904 | 904 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0; |
905 | 905 |
dim2::Point<double> mm=m+rot90(d)*sw/.75; |
906 | 906 |
if(_drawArrows) { |
907 | 907 |
int node_shape; |
908 | 908 |
dim2::Point<double> s=mycoords[g.source(*e)]; |
909 | 909 |
dim2::Point<double> t=mycoords[g.target(*e)]; |
910 | 910 |
double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
911 | 911 |
node_shape=_nodeShapes[g.target(*e)]; |
912 | 912 |
dim2::Bezier3 bez(s,mm,mm,t); |
913 | 913 |
double t1=0,t2=1; |
914 | 914 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
915 | 915 |
if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
916 | 916 |
else t1=(t1+t2)/2; |
917 | 917 |
dim2::Point<double> apoint=bez((t1+t2)/2); |
918 | 918 |
rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; |
919 | 919 |
rn*=rn; |
920 | 920 |
t2=(t1+t2)/2;t1=0; |
921 | 921 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
922 | 922 |
if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
923 | 923 |
else t2=(t1+t2)/2; |
924 | 924 |
dim2::Point<double> linend=bez((t1+t2)/2); |
925 | 925 |
bez=bez.before((t1+t2)/2); |
926 | 926 |
// rn=_nodeSizes[g.source(*e)]*_nodeScale; |
927 | 927 |
// node_shape=_nodeShapes[g.source(*e)]; |
928 | 928 |
// t1=0;t2=1; |
929 | 929 |
// for(int i=0;i<INTERPOL_PREC;++i) |
930 | 930 |
// if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) |
931 | 931 |
// t1=(t1+t2)/2; |
932 | 932 |
// else t2=(t1+t2)/2; |
933 | 933 |
// bez=bez.after((t1+t2)/2); |
934 | 934 |
os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
935 | 935 |
<< _arcColors[*e].red() << ' ' |
936 | 936 |
<< _arcColors[*e].green() << ' ' |
937 | 937 |
<< _arcColors[*e].blue() << " setrgbcolor newpath\n" |
938 | 938 |
<< bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
939 | 939 |
<< bez.p2.x << ' ' << bez.p2.y << ' ' |
940 | 940 |
<< bez.p3.x << ' ' << bez.p3.y << ' ' |
941 | 941 |
<< bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
942 | 942 |
dim2::Point<double> dd(rot90(linend-apoint)); |
943 | 943 |
dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ |
944 | 944 |
std::sqrt(dd.normSquare()); |
945 | 945 |
os << "newpath " << psOut(apoint) << " moveto " |
946 | 946 |
<< psOut(linend+dd) << " lineto " |
947 | 947 |
<< psOut(linend-dd) << " lineto closepath fill\n"; |
948 | 948 |
} |
949 | 949 |
else { |
950 | 950 |
os << mycoords[g.source(*e)].x << ' ' |
951 | 951 |
<< mycoords[g.source(*e)].y << ' ' |
952 | 952 |
<< mm.x << ' ' << mm.y << ' ' |
953 | 953 |
<< mycoords[g.target(*e)].x << ' ' |
954 | 954 |
<< mycoords[g.target(*e)].y << ' ' |
955 | 955 |
<< _arcColors[*e].red() << ' ' |
956 | 956 |
<< _arcColors[*e].green() << ' ' |
957 | 957 |
<< _arcColors[*e].blue() << ' ' |
958 | 958 |
<< _arcWidths[*e]*_arcWidthScale << " lb\n"; |
959 | 959 |
} |
960 | 960 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; |
961 | 961 |
} |
962 | 962 |
} |
963 | 963 |
} |
964 | 964 |
else for(ArcIt e(g);e!=INVALID;++e) |
965 | 965 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
966 | 966 |
&&g.source(e)!=g.target(e)) { |
967 | 967 |
if(_drawArrows) { |
968 | 968 |
dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]); |
969 | 969 |
double rn=_nodeSizes[g.target(e)]*_nodeScale; |
970 | 970 |
int node_shape=_nodeShapes[g.target(e)]; |
971 | 971 |
double t1=0,t2=1; |
972 | 972 |
for(int i=0;i<INTERPOL_PREC;++i) |
973 | 973 |
if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2; |
974 | 974 |
else t2=(t1+t2)/2; |
975 | 975 |
double l=std::sqrt(d.normSquare()); |
976 | 976 |
d/=l; |
977 | 977 |
|
978 | 978 |
os << l*(1-(t1+t2)/2) << ' ' |
979 | 979 |
<< _arcWidths[e]*_arcWidthScale << ' ' |
980 | 980 |
<< d.x << ' ' << d.y << ' ' |
981 | 981 |
<< mycoords[g.source(e)].x << ' ' |
982 | 982 |
<< mycoords[g.source(e)].y << ' ' |
983 | 983 |
<< _arcColors[e].red() << ' ' |
984 | 984 |
<< _arcColors[e].green() << ' ' |
985 | 985 |
<< _arcColors[e].blue() << " arr\n"; |
986 | 986 |
} |
987 | 987 |
else os << mycoords[g.source(e)].x << ' ' |
988 | 988 |
<< mycoords[g.source(e)].y << ' ' |
989 | 989 |
<< mycoords[g.target(e)].x << ' ' |
990 | 990 |
<< mycoords[g.target(e)].y << ' ' |
991 | 991 |
<< _arcColors[e].red() << ' ' |
992 | 992 |
<< _arcColors[e].green() << ' ' |
993 | 993 |
<< _arcColors[e].blue() << ' ' |
994 | 994 |
<< _arcWidths[e]*_arcWidthScale << " l\n"; |
995 | 995 |
} |
996 | 996 |
os << "grestore\n"; |
997 | 997 |
} |
998 | 998 |
if(_showNodes) { |
999 | 999 |
os << "%Nodes:\ngsave\n"; |
1000 | 1000 |
for(NodeIt n(g);n!=INVALID;++n) { |
1001 | 1001 |
os << mycoords[n].x << ' ' << mycoords[n].y << ' ' |
1002 | 1002 |
<< _nodeSizes[n]*_nodeScale << ' ' |
1003 | 1003 |
<< _nodeColors[n].red() << ' ' |
1004 | 1004 |
<< _nodeColors[n].green() << ' ' |
1005 | 1005 |
<< _nodeColors[n].blue() << ' '; |
1006 | 1006 |
switch(_nodeShapes[n]) { |
1007 | 1007 |
case CIRCLE: |
1008 | 1008 |
os<< "nc";break; |
1009 | 1009 |
case SQUARE: |
1010 | 1010 |
os<< "nsq";break; |
1011 | 1011 |
case DIAMOND: |
1012 | 1012 |
os<< "ndi";break; |
1013 | 1013 |
case MALE: |
1014 | 1014 |
os<< "nmale";break; |
1015 | 1015 |
case FEMALE: |
1016 | 1016 |
os<< "nfemale";break; |
1017 | 1017 |
} |
1018 | 1018 |
os<<'\n'; |
1019 | 1019 |
} |
1020 | 1020 |
os << "grestore\n"; |
1021 | 1021 |
} |
1022 | 1022 |
if(_showNodeText) { |
1023 | 1023 |
os << "%Node texts:\ngsave\n"; |
1024 | 1024 |
os << "/fosi " << _nodeTextSize << " def\n"; |
1025 | 1025 |
os << "(Helvetica) findfont fosi scalefont setfont\n"; |
1026 | 1026 |
for(NodeIt n(g);n!=INVALID;++n) { |
1027 | 1027 |
switch(_nodeTextColorType) { |
1028 | 1028 |
case DIST_COL: |
1029 | 1029 |
os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n"; |
1030 | 1030 |
break; |
1031 | 1031 |
case DIST_BW: |
1032 | 1032 |
os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n"; |
1033 | 1033 |
break; |
1034 | 1034 |
case CUST_COL: |
1035 | 1035 |
os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n"; |
1036 | 1036 |
break; |
1037 | 1037 |
default: |
1038 | 1038 |
os << "0 0 0 setrgbcolor\n"; |
1039 | 1039 |
} |
1040 | 1040 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1041 | 1041 |
<< " (" << _nodeTexts[n] << ") cshow\n"; |
1042 | 1042 |
} |
1043 | 1043 |
os << "grestore\n"; |
1044 | 1044 |
} |
1045 | 1045 |
if(_showNodePsText) { |
1046 | 1046 |
os << "%Node PS blocks:\ngsave\n"; |
1047 | 1047 |
for(NodeIt n(g);n!=INVALID;++n) |
1048 | 1048 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1049 | 1049 |
<< " moveto\n" << _nodePsTexts[n] << "\n"; |
1050 | 1050 |
os << "grestore\n"; |
1051 | 1051 |
} |
1052 | 1052 |
|
1053 | 1053 |
os << "grestore\nshowpage\n"; |
1054 | 1054 |
|
1055 | 1055 |
//CleanUp: |
1056 | 1056 |
if(_pleaseRemoveOsStream) {delete &os;} |
1057 | 1057 |
} |
1058 | 1058 |
|
1059 | 1059 |
///\name Aliases |
1060 | 1060 |
///These are just some aliases to other parameter setting functions. |
1061 | 1061 |
|
1062 | 1062 |
///@{ |
1063 | 1063 |
|
1064 | 1064 |
///An alias for arcWidths() |
1065 | 1065 |
template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x) |
1066 | 1066 |
{ |
1067 | 1067 |
return arcWidths(x); |
1068 | 1068 |
} |
1069 | 1069 |
|
1070 | 1070 |
///An alias for arcColors() |
1071 | 1071 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
1072 | 1072 |
edgeColors(const X &x) |
1073 | 1073 |
{ |
1074 | 1074 |
return arcColors(x); |
1075 | 1075 |
} |
1076 | 1076 |
|
1077 | 1077 |
///An alias for arcWidthScale() |
1078 | 1078 |
GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);} |
1079 | 1079 |
|
1080 | 1080 |
///An alias for autoArcWidthScale() |
1081 | 1081 |
GraphToEps<T> &autoEdgeWidthScale(bool b=true) |
1082 | 1082 |
{ |
1083 | 1083 |
return autoArcWidthScale(b); |
1084 | 1084 |
} |
1085 | 1085 |
|
1086 | 1086 |
///An alias for absoluteArcWidths() |
1087 | 1087 |
GraphToEps<T> &absoluteEdgeWidths(bool b=true) |
1088 | 1088 |
{ |
1089 | 1089 |
return absoluteArcWidths(b); |
1090 | 1090 |
} |
1091 | 1091 |
|
1092 | 1092 |
///An alias for parArcDist() |
1093 | 1093 |
GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);} |
1094 | 1094 |
|
1095 | 1095 |
///An alias for hideArcs() |
1096 | 1096 |
GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);} |
1097 | 1097 |
|
1098 | 1098 |
///@} |
1099 | 1099 |
}; |
1100 | 1100 |
|
1101 | 1101 |
template<class T> |
1102 | 1102 |
const int GraphToEps<T>::INTERPOL_PREC = 20; |
1103 | 1103 |
template<class T> |
1104 | 1104 |
const double GraphToEps<T>::A4HEIGHT = 841.8897637795276; |
1105 | 1105 |
template<class T> |
1106 | 1106 |
const double GraphToEps<T>::A4WIDTH = 595.275590551181; |
1107 | 1107 |
template<class T> |
1108 | 1108 |
const double GraphToEps<T>::A4BORDER = 15; |
1109 | 1109 |
|
1110 | 1110 |
|
1111 | 1111 |
///Generates an EPS file from a graph |
1112 | 1112 |
|
1113 | 1113 |
///\ingroup eps_io |
1114 | 1114 |
///Generates an EPS file from a graph. |
1115 | 1115 |
///\param g Reference to the graph to be printed. |
1116 | 1116 |
///\param os Reference to the output stream. |
1117 | 1117 |
///By default it is <tt>std::cout</tt>. |
1118 | 1118 |
/// |
1119 | 1119 |
///This function also has a lot of |
1120 | 1120 |
///\ref named-templ-func-param "named parameters", |
1121 | 1121 |
///they are declared as the members of class \ref GraphToEps. The following |
1122 | 1122 |
///example shows how to use these parameters. |
1123 | 1123 |
///\code |
1124 | 1124 |
/// graphToEps(g,os).scale(10).coords(coords) |
1125 | 1125 |
/// .nodeScale(2).nodeSizes(sizes) |
1126 | 1126 |
/// .arcWidthScale(.4).run(); |
1127 | 1127 |
///\endcode |
1128 | 1128 |
/// |
1129 | 1129 |
///For more detailed examples see the \ref graph_to_eps_demo.cc demo file. |
1130 | 1130 |
/// |
1131 | 1131 |
///\warning Don't forget to put the \ref GraphToEps::run() "run()" |
1132 | 1132 |
///to the end of the parameter list. |
1133 | 1133 |
///\sa GraphToEps |
1134 | 1134 |
///\sa graphToEps(GR &g, const char *file_name) |
1135 | 1135 |
template<class GR> |
1136 | 1136 |
GraphToEps<DefaultGraphToEpsTraits<GR> > |
1137 | 1137 |
graphToEps(GR &g, std::ostream& os=std::cout) |
1138 | 1138 |
{ |
1139 | 1139 |
return |
1140 | 1140 |
GraphToEps<DefaultGraphToEpsTraits<GR> >(DefaultGraphToEpsTraits<GR>(g,os)); |
1141 | 1141 |
} |
1142 | 1142 |
|
1143 | 1143 |
///Generates an EPS file from a graph |
1144 | 1144 |
|
1145 | 1145 |
///\ingroup eps_io |
1146 | 1146 |
///This function does the same as |
1147 | 1147 |
///\ref graphToEps(GR &g,std::ostream& os) |
1148 | 1148 |
///but it writes its output into the file \c file_name |
1149 | 1149 |
///instead of a stream. |
1150 | 1150 |
///\sa graphToEps(GR &g, std::ostream& os) |
1151 | 1151 |
template<class GR> |
1152 | 1152 |
GraphToEps<DefaultGraphToEpsTraits<GR> > |
1153 | 1153 |
graphToEps(GR &g,const char *file_name) |
1154 | 1154 |
{ |
1155 | 1155 |
std::ostream* os = new std::ofstream(file_name); |
1156 | 1156 |
if (!(*os)) { |
1157 | 1157 |
delete os; |
1158 | 1158 |
throw IoError("Cannot write file", file_name); |
1159 | 1159 |
} |
1160 | 1160 |
return GraphToEps<DefaultGraphToEpsTraits<GR> > |
1161 | 1161 |
(DefaultGraphToEpsTraits<GR>(g,*os,true)); |
1162 | 1162 |
} |
1163 | 1163 |
|
1164 | 1164 |
///Generates an EPS file from a graph |
1165 | 1165 |
|
1166 | 1166 |
///\ingroup eps_io |
1167 | 1167 |
///This function does the same as |
1168 | 1168 |
///\ref graphToEps(GR &g,std::ostream& os) |
1169 | 1169 |
///but it writes its output into the file \c file_name |
1170 | 1170 |
///instead of a stream. |
1171 | 1171 |
///\sa graphToEps(GR &g, std::ostream& os) |
1172 | 1172 |
template<class GR> |
1173 | 1173 |
GraphToEps<DefaultGraphToEpsTraits<GR> > |
1174 | 1174 |
graphToEps(GR &g,const std::string& file_name) |
1175 | 1175 |
{ |
1176 | 1176 |
std::ostream* os = new std::ofstream(file_name.c_str()); |
1177 | 1177 |
if (!(*os)) { |
1178 | 1178 |
delete os; |
1179 | 1179 |
throw IoError("Cannot write file", file_name); |
1180 | 1180 |
} |
1181 | 1181 |
return GraphToEps<DefaultGraphToEpsTraits<GR> > |
1182 | 1182 |
(DefaultGraphToEpsTraits<GR>(g,*os,true)); |
1183 | 1183 |
} |
1184 | 1184 |
|
1185 | 1185 |
} //END OF NAMESPACE LEMON |
1186 | 1186 |
|
1187 | 1187 |
#endif // LEMON_GRAPH_TO_EPS_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup lemon_io |
20 | 20 |
///\file |
21 | 21 |
///\brief \ref lgf-format "LEMON Graph Format" reader. |
22 | 22 |
|
23 | 23 |
|
24 | 24 |
#ifndef LEMON_LGF_READER_H |
25 | 25 |
#define LEMON_LGF_READER_H |
26 | 26 |
|
27 | 27 |
#include <iostream> |
28 | 28 |
#include <fstream> |
29 | 29 |
#include <sstream> |
30 | 30 |
|
31 | 31 |
#include <set> |
32 | 32 |
#include <map> |
33 | 33 |
|
34 | 34 |
#include <lemon/core.h> |
35 | 35 |
|
36 | 36 |
#include <lemon/lgf_writer.h> |
37 | 37 |
|
38 | 38 |
#include <lemon/concept_check.h> |
39 | 39 |
#include <lemon/concepts/maps.h> |
40 | 40 |
|
41 | 41 |
namespace lemon { |
42 | 42 |
|
43 | 43 |
namespace _reader_bits { |
44 | 44 |
|
45 | 45 |
template <typename Value> |
46 | 46 |
struct DefaultConverter { |
47 | 47 |
Value operator()(const std::string& str) { |
48 | 48 |
std::istringstream is(str); |
49 | 49 |
Value value; |
50 | 50 |
if (!(is >> value)) { |
51 | 51 |
throw FormatError("Cannot read token"); |
52 | 52 |
} |
53 | 53 |
|
54 | 54 |
char c; |
55 | 55 |
if (is >> std::ws >> c) { |
56 | 56 |
throw FormatError("Remaining characters in token"); |
57 | 57 |
} |
58 | 58 |
return value; |
59 | 59 |
} |
60 | 60 |
}; |
61 | 61 |
|
62 | 62 |
template <> |
63 | 63 |
struct DefaultConverter<std::string> { |
64 | 64 |
std::string operator()(const std::string& str) { |
65 | 65 |
return str; |
66 | 66 |
} |
67 | 67 |
}; |
68 | 68 |
|
69 | 69 |
template <typename _Item> |
70 | 70 |
class MapStorageBase { |
71 | 71 |
public: |
72 | 72 |
typedef _Item Item; |
73 | 73 |
|
74 | 74 |
public: |
75 | 75 |
MapStorageBase() {} |
76 | 76 |
virtual ~MapStorageBase() {} |
77 | 77 |
|
78 | 78 |
virtual void set(const Item& item, const std::string& value) = 0; |
79 | 79 |
|
80 | 80 |
}; |
81 | 81 |
|
82 | 82 |
template <typename _Item, typename _Map, |
83 | 83 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
84 | 84 |
class MapStorage : public MapStorageBase<_Item> { |
85 | 85 |
public: |
86 | 86 |
typedef _Map Map; |
87 | 87 |
typedef _Converter Converter; |
88 | 88 |
typedef _Item Item; |
89 | 89 |
|
90 | 90 |
private: |
91 | 91 |
Map& _map; |
92 | 92 |
Converter _converter; |
93 | 93 |
|
94 | 94 |
public: |
95 | 95 |
MapStorage(Map& map, const Converter& converter = Converter()) |
96 | 96 |
: _map(map), _converter(converter) {} |
97 | 97 |
virtual ~MapStorage() {} |
98 | 98 |
|
99 | 99 |
virtual void set(const Item& item ,const std::string& value) { |
100 | 100 |
_map.set(item, _converter(value)); |
101 | 101 |
} |
102 | 102 |
}; |
103 | 103 |
|
104 | 104 |
template <typename _GR, bool _dir, typename _Map, |
105 | 105 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
106 | 106 |
class GraphArcMapStorage : public MapStorageBase<typename _GR::Edge> { |
107 | 107 |
public: |
108 | 108 |
typedef _Map Map; |
109 | 109 |
typedef _Converter Converter; |
110 | 110 |
typedef _GR GR; |
111 | 111 |
typedef typename GR::Edge Item; |
112 | 112 |
static const bool dir = _dir; |
113 | 113 |
|
114 | 114 |
private: |
115 | 115 |
const GR& _graph; |
116 | 116 |
Map& _map; |
117 | 117 |
Converter _converter; |
118 | 118 |
|
119 | 119 |
public: |
120 | 120 |
GraphArcMapStorage(const GR& graph, Map& map, |
121 | 121 |
const Converter& converter = Converter()) |
122 | 122 |
: _graph(graph), _map(map), _converter(converter) {} |
123 | 123 |
virtual ~GraphArcMapStorage() {} |
124 | 124 |
|
125 | 125 |
virtual void set(const Item& item ,const std::string& value) { |
126 | 126 |
_map.set(_graph.direct(item, dir), _converter(value)); |
127 | 127 |
} |
128 | 128 |
}; |
129 | 129 |
|
130 | 130 |
class ValueStorageBase { |
131 | 131 |
public: |
132 | 132 |
ValueStorageBase() {} |
133 | 133 |
virtual ~ValueStorageBase() {} |
134 | 134 |
|
135 | 135 |
virtual void set(const std::string&) = 0; |
136 | 136 |
}; |
137 | 137 |
|
138 | 138 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
139 | 139 |
class ValueStorage : public ValueStorageBase { |
140 | 140 |
public: |
141 | 141 |
typedef _Value Value; |
142 | 142 |
typedef _Converter Converter; |
143 | 143 |
|
144 | 144 |
private: |
145 | 145 |
Value& _value; |
146 | 146 |
Converter _converter; |
147 | 147 |
|
148 | 148 |
public: |
149 | 149 |
ValueStorage(Value& value, const Converter& converter = Converter()) |
150 | 150 |
: _value(value), _converter(converter) {} |
151 | 151 |
|
152 | 152 |
virtual void set(const std::string& value) { |
153 | 153 |
_value = _converter(value); |
154 | 154 |
} |
155 | 155 |
}; |
156 | 156 |
|
157 | 157 |
template <typename Value> |
158 | 158 |
struct MapLookUpConverter { |
159 | 159 |
const std::map<std::string, Value>& _map; |
160 | 160 |
|
161 | 161 |
MapLookUpConverter(const std::map<std::string, Value>& map) |
162 | 162 |
: _map(map) {} |
163 | 163 |
|
164 | 164 |
Value operator()(const std::string& str) { |
165 | 165 |
typename std::map<std::string, Value>::const_iterator it = |
166 | 166 |
_map.find(str); |
167 | 167 |
if (it == _map.end()) { |
168 | 168 |
std::ostringstream msg; |
169 | 169 |
msg << "Item not found: " << str; |
170 | 170 |
throw FormatError(msg.str()); |
171 | 171 |
} |
172 | 172 |
return it->second; |
173 | 173 |
} |
174 | 174 |
}; |
175 | 175 |
|
176 | 176 |
template <typename GR> |
177 | 177 |
struct GraphArcLookUpConverter { |
178 | 178 |
const GR& _graph; |
179 | 179 |
const std::map<std::string, typename GR::Edge>& _map; |
180 | 180 |
|
181 | 181 |
GraphArcLookUpConverter(const GR& graph, |
182 | 182 |
const std::map<std::string, |
183 | 183 |
typename GR::Edge>& map) |
184 | 184 |
: _graph(graph), _map(map) {} |
185 | 185 |
|
186 | 186 |
typename GR::Arc operator()(const std::string& str) { |
187 | 187 |
if (str.empty() || (str[0] != '+' && str[0] != '-')) { |
188 | 188 |
throw FormatError("Item must start with '+' or '-'"); |
189 | 189 |
} |
190 | 190 |
typename std::map<std::string, typename GR::Edge> |
191 | 191 |
::const_iterator it = _map.find(str.substr(1)); |
192 | 192 |
if (it == _map.end()) { |
193 | 193 |
throw FormatError("Item not found"); |
194 | 194 |
} |
195 | 195 |
return _graph.direct(it->second, str[0] == '+'); |
196 | 196 |
} |
197 | 197 |
}; |
198 | 198 |
|
199 | 199 |
inline bool isWhiteSpace(char c) { |
200 | 200 |
return c == ' ' || c == '\t' || c == '\v' || |
201 | 201 |
c == '\n' || c == '\r' || c == '\f'; |
202 | 202 |
} |
203 | 203 |
|
204 | 204 |
inline bool isOct(char c) { |
205 | 205 |
return '0' <= c && c <='7'; |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
inline int valueOct(char c) { |
209 | 209 |
LEMON_ASSERT(isOct(c), "The character is not octal."); |
210 | 210 |
return c - '0'; |
211 | 211 |
} |
212 | 212 |
|
213 | 213 |
inline bool isHex(char c) { |
214 | 214 |
return ('0' <= c && c <= '9') || |
215 | 215 |
('a' <= c && c <= 'z') || |
216 | 216 |
('A' <= c && c <= 'Z'); |
217 | 217 |
} |
218 | 218 |
|
219 | 219 |
inline int valueHex(char c) { |
220 | 220 |
LEMON_ASSERT(isHex(c), "The character is not hexadecimal."); |
221 | 221 |
if ('0' <= c && c <= '9') return c - '0'; |
222 | 222 |
if ('a' <= c && c <= 'z') return c - 'a' + 10; |
223 | 223 |
return c - 'A' + 10; |
224 | 224 |
} |
225 | 225 |
|
226 | 226 |
inline bool isIdentifierFirstChar(char c) { |
227 | 227 |
return ('a' <= c && c <= 'z') || |
228 | 228 |
('A' <= c && c <= 'Z') || c == '_'; |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
inline bool isIdentifierChar(char c) { |
232 | 232 |
return isIdentifierFirstChar(c) || |
233 | 233 |
('0' <= c && c <= '9'); |
234 | 234 |
} |
235 | 235 |
|
236 | 236 |
inline char readEscape(std::istream& is) { |
237 | 237 |
char c; |
238 | 238 |
if (!is.get(c)) |
239 | 239 |
throw FormatError("Escape format error"); |
240 | 240 |
|
241 | 241 |
switch (c) { |
242 | 242 |
case '\\': |
243 | 243 |
return '\\'; |
244 | 244 |
case '\"': |
245 | 245 |
return '\"'; |
246 | 246 |
case '\'': |
247 | 247 |
return '\''; |
248 | 248 |
case '\?': |
249 | 249 |
return '\?'; |
250 | 250 |
case 'a': |
251 | 251 |
return '\a'; |
252 | 252 |
case 'b': |
253 | 253 |
return '\b'; |
254 | 254 |
case 'f': |
255 | 255 |
return '\f'; |
256 | 256 |
case 'n': |
257 | 257 |
return '\n'; |
258 | 258 |
case 'r': |
259 | 259 |
return '\r'; |
260 | 260 |
case 't': |
261 | 261 |
return '\t'; |
262 | 262 |
case 'v': |
263 | 263 |
return '\v'; |
264 | 264 |
case 'x': |
265 | 265 |
{ |
266 | 266 |
int code; |
267 | 267 |
if (!is.get(c) || !isHex(c)) |
268 | 268 |
throw FormatError("Escape format error"); |
269 | 269 |
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); |
270 | 270 |
else code = code * 16 + valueHex(c); |
271 | 271 |
return code; |
272 | 272 |
} |
273 | 273 |
default: |
274 | 274 |
{ |
275 | 275 |
int code; |
276 | 276 |
if (!isOct(c)) |
277 | 277 |
throw FormatError("Escape format error"); |
278 | 278 |
else if (code = valueOct(c), !is.get(c) || !isOct(c)) |
279 | 279 |
is.putback(c); |
280 | 280 |
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) |
281 | 281 |
is.putback(c); |
282 | 282 |
else code = code * 8 + valueOct(c); |
283 | 283 |
return code; |
284 | 284 |
} |
285 | 285 |
} |
286 | 286 |
} |
287 | 287 |
|
288 | 288 |
inline std::istream& readToken(std::istream& is, std::string& str) { |
289 | 289 |
std::ostringstream os; |
290 | 290 |
|
291 | 291 |
char c; |
292 | 292 |
is >> std::ws; |
293 | 293 |
|
294 | 294 |
if (!is.get(c)) |
295 | 295 |
return is; |
296 | 296 |
|
297 | 297 |
if (c == '\"') { |
298 | 298 |
while (is.get(c) && c != '\"') { |
299 | 299 |
if (c == '\\') |
300 | 300 |
c = readEscape(is); |
301 | 301 |
os << c; |
302 | 302 |
} |
303 | 303 |
if (!is) |
304 | 304 |
throw FormatError("Quoted format error"); |
305 | 305 |
} else { |
306 | 306 |
is.putback(c); |
307 | 307 |
while (is.get(c) && !isWhiteSpace(c)) { |
308 | 308 |
if (c == '\\') |
309 | 309 |
c = readEscape(is); |
310 | 310 |
os << c; |
311 | 311 |
} |
312 | 312 |
if (!is) { |
313 | 313 |
is.clear(); |
314 | 314 |
} else { |
315 | 315 |
is.putback(c); |
316 | 316 |
} |
317 | 317 |
} |
318 | 318 |
str = os.str(); |
319 | 319 |
return is; |
320 | 320 |
} |
321 | 321 |
|
322 | 322 |
class Section { |
323 | 323 |
public: |
324 | 324 |
virtual ~Section() {} |
325 | 325 |
virtual void process(std::istream& is, int& line_num) = 0; |
326 | 326 |
}; |
327 | 327 |
|
328 | 328 |
template <typename Functor> |
329 | 329 |
class LineSection : public Section { |
330 | 330 |
private: |
331 | 331 |
|
332 | 332 |
Functor _functor; |
333 | 333 |
|
334 | 334 |
public: |
335 | 335 |
|
336 | 336 |
LineSection(const Functor& functor) : _functor(functor) {} |
337 | 337 |
virtual ~LineSection() {} |
338 | 338 |
|
339 | 339 |
virtual void process(std::istream& is, int& line_num) { |
340 | 340 |
char c; |
341 | 341 |
std::string line; |
342 | 342 |
while (is.get(c) && c != '@') { |
343 | 343 |
if (c == '\n') { |
344 | 344 |
++line_num; |
345 | 345 |
} else if (c == '#') { |
346 | 346 |
getline(is, line); |
347 | 347 |
++line_num; |
348 | 348 |
} else if (!isWhiteSpace(c)) { |
349 | 349 |
is.putback(c); |
350 | 350 |
getline(is, line); |
351 | 351 |
_functor(line); |
352 | 352 |
++line_num; |
353 | 353 |
} |
354 | 354 |
} |
355 | 355 |
if (is) is.putback(c); |
356 | 356 |
else if (is.eof()) is.clear(); |
357 | 357 |
} |
358 | 358 |
}; |
359 | 359 |
|
360 | 360 |
template <typename Functor> |
361 | 361 |
class StreamSection : public Section { |
362 | 362 |
private: |
363 | 363 |
|
364 | 364 |
Functor _functor; |
365 | 365 |
|
366 | 366 |
public: |
367 | 367 |
|
368 | 368 |
StreamSection(const Functor& functor) : _functor(functor) {} |
369 | 369 |
virtual ~StreamSection() {} |
370 | 370 |
|
371 | 371 |
virtual void process(std::istream& is, int& line_num) { |
372 | 372 |
_functor(is, line_num); |
373 | 373 |
char c; |
374 | 374 |
std::string line; |
375 | 375 |
while (is.get(c) && c != '@') { |
376 | 376 |
if (c == '\n') { |
377 | 377 |
++line_num; |
378 | 378 |
} else if (!isWhiteSpace(c)) { |
379 | 379 |
getline(is, line); |
380 | 380 |
++line_num; |
381 | 381 |
} |
382 | 382 |
} |
383 | 383 |
if (is) is.putback(c); |
384 | 384 |
else if (is.eof()) is.clear(); |
385 | 385 |
} |
386 | 386 |
}; |
387 | 387 |
|
388 | 388 |
} |
389 | 389 |
|
390 | 390 |
template <typename DGR> |
391 | 391 |
class DigraphReader; |
392 | 392 |
|
393 | 393 |
template <typename TDGR> |
394 | 394 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is = std::cin); |
395 | 395 |
template <typename TDGR> |
396 | 396 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn); |
397 | 397 |
template <typename TDGR> |
398 | 398 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn); |
399 | 399 |
|
400 | 400 |
/// \ingroup lemon_io |
401 | 401 |
/// |
402 | 402 |
/// \brief \ref lgf-format "LGF" reader for directed graphs |
403 | 403 |
/// |
404 | 404 |
/// This utility reads an \ref lgf-format "LGF" file. |
405 | 405 |
/// |
406 | 406 |
/// The reading method does a batch processing. The user creates a |
407 | 407 |
/// reader object, then various reading rules can be added to the |
408 | 408 |
/// reader, and eventually the reading is executed with the \c run() |
409 | 409 |
/// member function. A map reading rule can be added to the reader |
410 | 410 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
411 | 411 |
/// converter parameter can also be added as a standard functor |
412 | 412 |
/// converting from \c std::string to the value type of the map. If it |
413 | 413 |
/// is set, it will determine how the tokens in the file should be |
414 | 414 |
/// converted to the value type of the map. If the functor is not set, |
415 | 415 |
/// then a default conversion will be used. One map can be read into |
416 | 416 |
/// multiple map objects at the same time. The \c attribute(), \c |
417 | 417 |
/// node() and \c arc() functions are used to add attribute reading |
418 | 418 |
/// rules. |
419 | 419 |
/// |
420 | 420 |
///\code |
421 | 421 |
/// DigraphReader<DGR>(digraph, std::cin). |
422 | 422 |
/// nodeMap("coordinates", coord_map). |
423 | 423 |
/// arcMap("capacity", cap_map). |
424 | 424 |
/// node("source", src). |
425 | 425 |
/// node("target", trg). |
426 | 426 |
/// attribute("caption", caption). |
427 | 427 |
/// run(); |
428 | 428 |
///\endcode |
429 | 429 |
/// |
430 | 430 |
/// By default the reader uses the first section in the file of the |
431 | 431 |
/// proper type. If a section has an optional name, then it can be |
432 | 432 |
/// selected for reading by giving an optional name parameter to the |
433 | 433 |
/// \c nodes(), \c arcs() or \c attributes() functions. |
434 | 434 |
/// |
435 | 435 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader |
436 | 436 |
/// that the nodes or arcs should not be constructed (added to the |
437 | 437 |
/// graph) during the reading, but instead the label map of the items |
438 | 438 |
/// are given as a parameter of these functions. An |
439 | 439 |
/// application of these functions is multipass reading, which is |
440 | 440 |
/// important if two \c \@arcs sections must be read from the |
441 | 441 |
/// file. In this case the first phase would read the node set and one |
442 | 442 |
/// of the arc sets, while the second phase would read the second arc |
443 | 443 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
444 | 444 |
/// The previously read label node map should be passed to the \c |
445 | 445 |
/// useNodes() functions. Another application of multipass reading when |
446 | 446 |
/// paths are given as a node map or an arc map. |
447 | 447 |
/// It is impossible to read this in |
448 | 448 |
/// a single pass, because the arcs are not constructed when the node |
449 | 449 |
/// maps are read. |
450 | 450 |
template <typename DGR> |
451 | 451 |
class DigraphReader { |
452 | 452 |
public: |
453 | 453 |
|
454 | 454 |
typedef DGR Digraph; |
455 | 455 |
|
456 | 456 |
private: |
457 | 457 |
|
458 | 458 |
TEMPLATE_DIGRAPH_TYPEDEFS(DGR); |
459 | 459 |
|
460 | 460 |
std::istream* _is; |
461 | 461 |
bool local_is; |
462 | 462 |
std::string _filename; |
463 | 463 |
|
464 | 464 |
DGR& _digraph; |
465 | 465 |
|
466 | 466 |
std::string _nodes_caption; |
467 | 467 |
std::string _arcs_caption; |
468 | 468 |
std::string _attributes_caption; |
469 | 469 |
|
470 | 470 |
typedef std::map<std::string, Node> NodeIndex; |
471 | 471 |
NodeIndex _node_index; |
472 | 472 |
typedef std::map<std::string, Arc> ArcIndex; |
473 | 473 |
ArcIndex _arc_index; |
474 | 474 |
|
475 | 475 |
typedef std::vector<std::pair<std::string, |
476 | 476 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
477 | 477 |
NodeMaps _node_maps; |
478 | 478 |
|
479 | 479 |
typedef std::vector<std::pair<std::string, |
480 | 480 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
481 | 481 |
ArcMaps _arc_maps; |
482 | 482 |
|
483 | 483 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
484 | 484 |
Attributes; |
485 | 485 |
Attributes _attributes; |
486 | 486 |
|
487 | 487 |
bool _use_nodes; |
488 | 488 |
bool _use_arcs; |
489 | 489 |
|
490 | 490 |
bool _skip_nodes; |
491 | 491 |
bool _skip_arcs; |
492 | 492 |
|
493 | 493 |
int line_num; |
494 | 494 |
std::istringstream line; |
495 | 495 |
|
496 | 496 |
public: |
497 | 497 |
|
498 | 498 |
/// \brief Constructor |
499 | 499 |
/// |
500 | 500 |
/// Construct a directed graph reader, which reads from the given |
501 | 501 |
/// input stream. |
502 | 502 |
DigraphReader(DGR& digraph, std::istream& is = std::cin) |
503 | 503 |
: _is(&is), local_is(false), _digraph(digraph), |
504 | 504 |
_use_nodes(false), _use_arcs(false), |
505 | 505 |
_skip_nodes(false), _skip_arcs(false) {} |
506 | 506 |
|
507 | 507 |
/// \brief Constructor |
508 | 508 |
/// |
509 | 509 |
/// Construct a directed graph reader, which reads from the given |
510 | 510 |
/// file. |
511 | 511 |
DigraphReader(DGR& digraph, const std::string& fn) |
512 | 512 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
513 | 513 |
_filename(fn), _digraph(digraph), |
514 | 514 |
_use_nodes(false), _use_arcs(false), |
515 | 515 |
_skip_nodes(false), _skip_arcs(false) { |
516 | 516 |
if (!(*_is)) { |
517 | 517 |
delete _is; |
518 | 518 |
throw IoError("Cannot open file", fn); |
519 | 519 |
} |
520 | 520 |
} |
521 | 521 |
|
522 | 522 |
/// \brief Constructor |
523 | 523 |
/// |
524 | 524 |
/// Construct a directed graph reader, which reads from the given |
525 | 525 |
/// file. |
526 | 526 |
DigraphReader(DGR& digraph, const char* fn) |
527 | 527 |
: _is(new std::ifstream(fn)), local_is(true), |
528 | 528 |
_filename(fn), _digraph(digraph), |
529 | 529 |
_use_nodes(false), _use_arcs(false), |
530 | 530 |
_skip_nodes(false), _skip_arcs(false) { |
531 | 531 |
if (!(*_is)) { |
532 | 532 |
delete _is; |
533 | 533 |
throw IoError("Cannot open file", fn); |
534 | 534 |
} |
535 | 535 |
} |
536 | 536 |
|
537 | 537 |
/// \brief Destructor |
538 | 538 |
~DigraphReader() { |
539 | 539 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
540 | 540 |
it != _node_maps.end(); ++it) { |
541 | 541 |
delete it->second; |
542 | 542 |
} |
543 | 543 |
|
544 | 544 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
545 | 545 |
it != _arc_maps.end(); ++it) { |
546 | 546 |
delete it->second; |
547 | 547 |
} |
548 | 548 |
|
549 | 549 |
for (typename Attributes::iterator it = _attributes.begin(); |
550 | 550 |
it != _attributes.end(); ++it) { |
551 | 551 |
delete it->second; |
552 | 552 |
} |
553 | 553 |
|
554 | 554 |
if (local_is) { |
555 | 555 |
delete _is; |
556 | 556 |
} |
557 | 557 |
|
558 | 558 |
} |
559 | 559 |
|
560 | 560 |
private: |
561 | 561 |
|
562 | 562 |
template <typename TDGR> |
563 | 563 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is); |
564 | 564 |
template <typename TDGR> |
565 | 565 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, |
566 | 566 |
const std::string& fn); |
567 | 567 |
template <typename TDGR> |
568 | 568 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn); |
569 | 569 |
|
570 | 570 |
DigraphReader(DigraphReader& other) |
571 | 571 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
572 | 572 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
573 | 573 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { |
574 | 574 |
|
575 | 575 |
other._is = 0; |
576 | 576 |
other.local_is = false; |
577 | 577 |
|
578 | 578 |
_node_index.swap(other._node_index); |
579 | 579 |
_arc_index.swap(other._arc_index); |
580 | 580 |
|
581 | 581 |
_node_maps.swap(other._node_maps); |
582 | 582 |
_arc_maps.swap(other._arc_maps); |
583 | 583 |
_attributes.swap(other._attributes); |
584 | 584 |
|
585 | 585 |
_nodes_caption = other._nodes_caption; |
586 | 586 |
_arcs_caption = other._arcs_caption; |
587 | 587 |
_attributes_caption = other._attributes_caption; |
588 | 588 |
|
589 | 589 |
} |
590 | 590 |
|
591 | 591 |
DigraphReader& operator=(const DigraphReader&); |
592 | 592 |
|
593 | 593 |
public: |
594 | 594 |
|
595 | 595 |
/// \name Reading Rules |
596 | 596 |
/// @{ |
597 | 597 |
|
598 | 598 |
/// \brief Node map reading rule |
599 | 599 |
/// |
600 | 600 |
/// Add a node map reading rule to the reader. |
601 | 601 |
template <typename Map> |
602 | 602 |
DigraphReader& nodeMap(const std::string& caption, Map& map) { |
603 | 603 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
604 | 604 |
_reader_bits::MapStorageBase<Node>* storage = |
605 | 605 |
new _reader_bits::MapStorage<Node, Map>(map); |
606 | 606 |
_node_maps.push_back(std::make_pair(caption, storage)); |
607 | 607 |
return *this; |
608 | 608 |
} |
609 | 609 |
|
610 | 610 |
/// \brief Node map reading rule |
611 | 611 |
/// |
612 | 612 |
/// Add a node map reading rule with specialized converter to the |
613 | 613 |
/// reader. |
614 | 614 |
template <typename Map, typename Converter> |
615 | 615 |
DigraphReader& nodeMap(const std::string& caption, Map& map, |
616 | 616 |
const Converter& converter = Converter()) { |
617 | 617 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
618 | 618 |
_reader_bits::MapStorageBase<Node>* storage = |
619 | 619 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
620 | 620 |
_node_maps.push_back(std::make_pair(caption, storage)); |
621 | 621 |
return *this; |
622 | 622 |
} |
623 | 623 |
|
624 | 624 |
/// \brief Arc map reading rule |
625 | 625 |
/// |
626 | 626 |
/// Add an arc map reading rule to the reader. |
627 | 627 |
template <typename Map> |
628 | 628 |
DigraphReader& arcMap(const std::string& caption, Map& map) { |
629 | 629 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
630 | 630 |
_reader_bits::MapStorageBase<Arc>* storage = |
631 | 631 |
new _reader_bits::MapStorage<Arc, Map>(map); |
632 | 632 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
633 | 633 |
return *this; |
634 | 634 |
} |
635 | 635 |
|
636 | 636 |
/// \brief Arc map reading rule |
637 | 637 |
/// |
638 | 638 |
/// Add an arc map reading rule with specialized converter to the |
639 | 639 |
/// reader. |
640 | 640 |
template <typename Map, typename Converter> |
641 | 641 |
DigraphReader& arcMap(const std::string& caption, Map& map, |
642 | 642 |
const Converter& converter = Converter()) { |
643 | 643 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
644 | 644 |
_reader_bits::MapStorageBase<Arc>* storage = |
645 | 645 |
new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter); |
646 | 646 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
647 | 647 |
return *this; |
648 | 648 |
} |
649 | 649 |
|
650 | 650 |
/// \brief Attribute reading rule |
651 | 651 |
/// |
652 | 652 |
/// Add an attribute reading rule to the reader. |
653 | 653 |
template <typename Value> |
654 | 654 |
DigraphReader& attribute(const std::string& caption, Value& value) { |
655 | 655 |
_reader_bits::ValueStorageBase* storage = |
656 | 656 |
new _reader_bits::ValueStorage<Value>(value); |
657 | 657 |
_attributes.insert(std::make_pair(caption, storage)); |
658 | 658 |
return *this; |
659 | 659 |
} |
660 | 660 |
|
661 | 661 |
/// \brief Attribute reading rule |
662 | 662 |
/// |
663 | 663 |
/// Add an attribute reading rule with specialized converter to the |
664 | 664 |
/// reader. |
665 | 665 |
template <typename Value, typename Converter> |
666 | 666 |
DigraphReader& attribute(const std::string& caption, Value& value, |
667 | 667 |
const Converter& converter = Converter()) { |
668 | 668 |
_reader_bits::ValueStorageBase* storage = |
669 | 669 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
670 | 670 |
_attributes.insert(std::make_pair(caption, storage)); |
671 | 671 |
return *this; |
672 | 672 |
} |
673 | 673 |
|
674 | 674 |
/// \brief Node reading rule |
675 | 675 |
/// |
676 | 676 |
/// Add a node reading rule to reader. |
677 | 677 |
DigraphReader& node(const std::string& caption, Node& node) { |
678 | 678 |
typedef _reader_bits::MapLookUpConverter<Node> Converter; |
679 | 679 |
Converter converter(_node_index); |
680 | 680 |
_reader_bits::ValueStorageBase* storage = |
681 | 681 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
682 | 682 |
_attributes.insert(std::make_pair(caption, storage)); |
683 | 683 |
return *this; |
684 | 684 |
} |
685 | 685 |
|
686 | 686 |
/// \brief Arc reading rule |
687 | 687 |
/// |
688 | 688 |
/// Add an arc reading rule to reader. |
689 | 689 |
DigraphReader& arc(const std::string& caption, Arc& arc) { |
690 | 690 |
typedef _reader_bits::MapLookUpConverter<Arc> Converter; |
691 | 691 |
Converter converter(_arc_index); |
692 | 692 |
_reader_bits::ValueStorageBase* storage = |
693 | 693 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
694 | 694 |
_attributes.insert(std::make_pair(caption, storage)); |
695 | 695 |
return *this; |
696 | 696 |
} |
697 | 697 |
|
698 | 698 |
/// @} |
699 | 699 |
|
700 | 700 |
/// \name Select Section by Name |
701 | 701 |
/// @{ |
702 | 702 |
|
703 | 703 |
/// \brief Set \c \@nodes section to be read |
704 | 704 |
/// |
705 | 705 |
/// Set \c \@nodes section to be read |
706 | 706 |
DigraphReader& nodes(const std::string& caption) { |
707 | 707 |
_nodes_caption = caption; |
708 | 708 |
return *this; |
709 | 709 |
} |
710 | 710 |
|
711 | 711 |
/// \brief Set \c \@arcs section to be read |
712 | 712 |
/// |
713 | 713 |
/// Set \c \@arcs section to be read |
714 | 714 |
DigraphReader& arcs(const std::string& caption) { |
715 | 715 |
_arcs_caption = caption; |
716 | 716 |
return *this; |
717 | 717 |
} |
718 | 718 |
|
719 | 719 |
/// \brief Set \c \@attributes section to be read |
720 | 720 |
/// |
721 | 721 |
/// Set \c \@attributes section to be read |
722 | 722 |
DigraphReader& attributes(const std::string& caption) { |
723 | 723 |
_attributes_caption = caption; |
724 | 724 |
return *this; |
725 | 725 |
} |
726 | 726 |
|
727 | 727 |
/// @} |
728 | 728 |
|
729 | 729 |
/// \name Using Previously Constructed Node or Arc Set |
730 | 730 |
/// @{ |
731 | 731 |
|
732 | 732 |
/// \brief Use previously constructed node set |
733 | 733 |
/// |
734 | 734 |
/// Use previously constructed node set, and specify the node |
735 | 735 |
/// label map. |
736 | 736 |
template <typename Map> |
737 | 737 |
DigraphReader& useNodes(const Map& map) { |
738 | 738 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
739 | 739 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
740 | 740 |
_use_nodes = true; |
741 | 741 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
742 | 742 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
743 | 743 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
744 | 744 |
} |
745 | 745 |
return *this; |
746 | 746 |
} |
747 | 747 |
|
748 | 748 |
/// \brief Use previously constructed node set |
749 | 749 |
/// |
750 | 750 |
/// Use previously constructed node set, and specify the node |
751 | 751 |
/// label map and a functor which converts the label map values to |
752 | 752 |
/// \c std::string. |
753 | 753 |
template <typename Map, typename Converter> |
754 | 754 |
DigraphReader& useNodes(const Map& map, |
755 | 755 |
const Converter& converter = Converter()) { |
756 | 756 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
757 | 757 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
758 | 758 |
_use_nodes = true; |
759 | 759 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
760 | 760 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
761 | 761 |
} |
762 | 762 |
return *this; |
763 | 763 |
} |
764 | 764 |
|
765 | 765 |
/// \brief Use previously constructed arc set |
766 | 766 |
/// |
767 | 767 |
/// Use previously constructed arc set, and specify the arc |
768 | 768 |
/// label map. |
769 | 769 |
template <typename Map> |
770 | 770 |
DigraphReader& useArcs(const Map& map) { |
771 | 771 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
772 | 772 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
773 | 773 |
_use_arcs = true; |
774 | 774 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
775 | 775 |
for (ArcIt a(_digraph); a != INVALID; ++a) { |
776 | 776 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
777 | 777 |
} |
778 | 778 |
return *this; |
779 | 779 |
} |
780 | 780 |
|
781 | 781 |
/// \brief Use previously constructed arc set |
782 | 782 |
/// |
783 | 783 |
/// Use previously constructed arc set, and specify the arc |
784 | 784 |
/// label map and a functor which converts the label map values to |
785 | 785 |
/// \c std::string. |
786 | 786 |
template <typename Map, typename Converter> |
787 | 787 |
DigraphReader& useArcs(const Map& map, |
788 | 788 |
const Converter& converter = Converter()) { |
789 | 789 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
790 | 790 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
791 | 791 |
_use_arcs = true; |
792 | 792 |
for (ArcIt a(_digraph); a != INVALID; ++a) { |
793 | 793 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
794 | 794 |
} |
795 | 795 |
return *this; |
796 | 796 |
} |
797 | 797 |
|
798 | 798 |
/// \brief Skips the reading of node section |
799 | 799 |
/// |
800 | 800 |
/// Omit the reading of the node section. This implies that each node |
801 | 801 |
/// map reading rule will be abandoned, and the nodes of the graph |
802 | 802 |
/// will not be constructed, which usually cause that the arc set |
803 | 803 |
/// could not be read due to lack of node name resolving. |
804 | 804 |
/// Therefore \c skipArcs() function should also be used, or |
805 | 805 |
/// \c useNodes() should be used to specify the label of the nodes. |
806 | 806 |
DigraphReader& skipNodes() { |
807 | 807 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
808 | 808 |
_skip_nodes = true; |
809 | 809 |
return *this; |
810 | 810 |
} |
811 | 811 |
|
812 | 812 |
/// \brief Skips the reading of arc section |
813 | 813 |
/// |
814 | 814 |
/// Omit the reading of the arc section. This implies that each arc |
815 | 815 |
/// map reading rule will be abandoned, and the arcs of the graph |
816 | 816 |
/// will not be constructed. |
817 | 817 |
DigraphReader& skipArcs() { |
818 | 818 |
LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); |
819 | 819 |
_skip_arcs = true; |
820 | 820 |
return *this; |
821 | 821 |
} |
822 | 822 |
|
823 | 823 |
/// @} |
824 | 824 |
|
825 | 825 |
private: |
826 | 826 |
|
827 | 827 |
bool readLine() { |
828 | 828 |
std::string str; |
829 | 829 |
while(++line_num, std::getline(*_is, str)) { |
830 | 830 |
line.clear(); line.str(str); |
831 | 831 |
char c; |
832 | 832 |
if (line >> std::ws >> c && c != '#') { |
833 | 833 |
line.putback(c); |
834 | 834 |
return true; |
835 | 835 |
} |
836 | 836 |
} |
837 | 837 |
return false; |
838 | 838 |
} |
839 | 839 |
|
840 | 840 |
bool readSuccess() { |
841 | 841 |
return static_cast<bool>(*_is); |
842 | 842 |
} |
843 | 843 |
|
844 | 844 |
void skipSection() { |
845 | 845 |
char c; |
846 | 846 |
while (readSuccess() && line >> c && c != '@') { |
847 | 847 |
readLine(); |
848 | 848 |
} |
849 | 849 |
if (readSuccess()) { |
850 | 850 |
line.putback(c); |
851 | 851 |
} |
852 | 852 |
} |
853 | 853 |
|
854 | 854 |
void readNodes() { |
855 | 855 |
|
856 | 856 |
std::vector<int> map_index(_node_maps.size()); |
857 | 857 |
int map_num, label_index; |
858 | 858 |
|
859 | 859 |
char c; |
860 | 860 |
if (!readLine() || !(line >> c) || c == '@') { |
861 | 861 |
if (readSuccess() && line) line.putback(c); |
862 | 862 |
if (!_node_maps.empty()) |
863 | 863 |
throw FormatError("Cannot find map names"); |
864 | 864 |
return; |
865 | 865 |
} |
866 | 866 |
line.putback(c); |
867 | 867 |
|
868 | 868 |
{ |
869 | 869 |
std::map<std::string, int> maps; |
870 | 870 |
|
871 | 871 |
std::string map; |
872 | 872 |
int index = 0; |
873 | 873 |
while (_reader_bits::readToken(line, map)) { |
874 | 874 |
if (maps.find(map) != maps.end()) { |
875 | 875 |
std::ostringstream msg; |
876 | 876 |
msg << "Multiple occurence of node map: " << map; |
877 | 877 |
throw FormatError(msg.str()); |
878 | 878 |
} |
879 | 879 |
maps.insert(std::make_pair(map, index)); |
880 | 880 |
++index; |
881 | 881 |
} |
882 | 882 |
|
883 | 883 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
884 | 884 |
std::map<std::string, int>::iterator jt = |
885 | 885 |
maps.find(_node_maps[i].first); |
886 | 886 |
if (jt == maps.end()) { |
887 | 887 |
std::ostringstream msg; |
888 | 888 |
msg << "Map not found: " << _node_maps[i].first; |
889 | 889 |
throw FormatError(msg.str()); |
890 | 890 |
} |
891 | 891 |
map_index[i] = jt->second; |
892 | 892 |
} |
893 | 893 |
|
894 | 894 |
{ |
895 | 895 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
896 | 896 |
if (jt != maps.end()) { |
897 | 897 |
label_index = jt->second; |
898 | 898 |
} else { |
899 | 899 |
label_index = -1; |
900 | 900 |
} |
901 | 901 |
} |
902 | 902 |
map_num = maps.size(); |
903 | 903 |
} |
904 | 904 |
|
905 | 905 |
while (readLine() && line >> c && c != '@') { |
906 | 906 |
line.putback(c); |
907 | 907 |
|
908 | 908 |
std::vector<std::string> tokens(map_num); |
909 | 909 |
for (int i = 0; i < map_num; ++i) { |
910 | 910 |
if (!_reader_bits::readToken(line, tokens[i])) { |
911 | 911 |
std::ostringstream msg; |
912 | 912 |
msg << "Column not found (" << i + 1 << ")"; |
913 | 913 |
throw FormatError(msg.str()); |
914 | 914 |
} |
915 | 915 |
} |
916 | 916 |
if (line >> std::ws >> c) |
917 | 917 |
throw FormatError("Extra character at the end of line"); |
918 | 918 |
|
919 | 919 |
Node n; |
920 | 920 |
if (!_use_nodes) { |
921 | 921 |
n = _digraph.addNode(); |
922 | 922 |
if (label_index != -1) |
923 | 923 |
_node_index.insert(std::make_pair(tokens[label_index], n)); |
924 | 924 |
} else { |
925 | 925 |
if (label_index == -1) |
926 | 926 |
throw FormatError("Label map not found"); |
927 | 927 |
typename std::map<std::string, Node>::iterator it = |
928 | 928 |
_node_index.find(tokens[label_index]); |
929 | 929 |
if (it == _node_index.end()) { |
930 | 930 |
std::ostringstream msg; |
931 | 931 |
msg << "Node with label not found: " << tokens[label_index]; |
932 | 932 |
throw FormatError(msg.str()); |
933 | 933 |
} |
934 | 934 |
n = it->second; |
935 | 935 |
} |
936 | 936 |
|
937 | 937 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
938 | 938 |
_node_maps[i].second->set(n, tokens[map_index[i]]); |
939 | 939 |
} |
940 | 940 |
|
941 | 941 |
} |
942 | 942 |
if (readSuccess()) { |
943 | 943 |
line.putback(c); |
944 | 944 |
} |
945 | 945 |
} |
946 | 946 |
|
947 | 947 |
void readArcs() { |
948 | 948 |
|
949 | 949 |
std::vector<int> map_index(_arc_maps.size()); |
950 | 950 |
int map_num, label_index; |
951 | 951 |
|
952 | 952 |
char c; |
953 | 953 |
if (!readLine() || !(line >> c) || c == '@') { |
954 | 954 |
if (readSuccess() && line) line.putback(c); |
955 | 955 |
if (!_arc_maps.empty()) |
956 | 956 |
throw FormatError("Cannot find map names"); |
957 | 957 |
return; |
958 | 958 |
} |
959 | 959 |
line.putback(c); |
960 | 960 |
|
961 | 961 |
{ |
962 | 962 |
std::map<std::string, int> maps; |
963 | 963 |
|
964 | 964 |
std::string map; |
965 | 965 |
int index = 0; |
966 | 966 |
while (_reader_bits::readToken(line, map)) { |
967 |
if(map == "-") { |
|
968 |
if(index!=0) |
|
969 |
throw FormatError("'-' is not allowed as a map name"); |
|
970 |
else if (line >> std::ws >> c) |
|
971 |
throw FormatError("Extra character at the end of line"); |
|
972 |
else break; |
|
973 |
} |
|
967 | 974 |
if (maps.find(map) != maps.end()) { |
968 | 975 |
std::ostringstream msg; |
969 | 976 |
msg << "Multiple occurence of arc map: " << map; |
970 | 977 |
throw FormatError(msg.str()); |
971 | 978 |
} |
972 | 979 |
maps.insert(std::make_pair(map, index)); |
973 | 980 |
++index; |
974 | 981 |
} |
975 | 982 |
|
976 | 983 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
977 | 984 |
std::map<std::string, int>::iterator jt = |
978 | 985 |
maps.find(_arc_maps[i].first); |
979 | 986 |
if (jt == maps.end()) { |
980 | 987 |
std::ostringstream msg; |
981 | 988 |
msg << "Map not found: " << _arc_maps[i].first; |
982 | 989 |
throw FormatError(msg.str()); |
983 | 990 |
} |
984 | 991 |
map_index[i] = jt->second; |
985 | 992 |
} |
986 | 993 |
|
987 | 994 |
{ |
988 | 995 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
989 | 996 |
if (jt != maps.end()) { |
990 | 997 |
label_index = jt->second; |
991 | 998 |
} else { |
992 | 999 |
label_index = -1; |
993 | 1000 |
} |
994 | 1001 |
} |
995 | 1002 |
map_num = maps.size(); |
996 | 1003 |
} |
997 | 1004 |
|
998 | 1005 |
while (readLine() && line >> c && c != '@') { |
999 | 1006 |
line.putback(c); |
1000 | 1007 |
|
1001 | 1008 |
std::string source_token; |
1002 | 1009 |
std::string target_token; |
1003 | 1010 |
|
1004 | 1011 |
if (!_reader_bits::readToken(line, source_token)) |
1005 | 1012 |
throw FormatError("Source not found"); |
1006 | 1013 |
|
1007 | 1014 |
if (!_reader_bits::readToken(line, target_token)) |
1008 | 1015 |
throw FormatError("Target not found"); |
1009 | 1016 |
|
1010 | 1017 |
std::vector<std::string> tokens(map_num); |
1011 | 1018 |
for (int i = 0; i < map_num; ++i) { |
1012 | 1019 |
if (!_reader_bits::readToken(line, tokens[i])) { |
1013 | 1020 |
std::ostringstream msg; |
1014 | 1021 |
msg << "Column not found (" << i + 1 << ")"; |
1015 | 1022 |
throw FormatError(msg.str()); |
1016 | 1023 |
} |
1017 | 1024 |
} |
1018 | 1025 |
if (line >> std::ws >> c) |
1019 | 1026 |
throw FormatError("Extra character at the end of line"); |
1020 | 1027 |
|
1021 | 1028 |
Arc a; |
1022 | 1029 |
if (!_use_arcs) { |
1023 | 1030 |
|
1024 | 1031 |
typename NodeIndex::iterator it; |
1025 | 1032 |
|
1026 | 1033 |
it = _node_index.find(source_token); |
1027 | 1034 |
if (it == _node_index.end()) { |
1028 | 1035 |
std::ostringstream msg; |
1029 | 1036 |
msg << "Item not found: " << source_token; |
1030 | 1037 |
throw FormatError(msg.str()); |
1031 | 1038 |
} |
1032 | 1039 |
Node source = it->second; |
1033 | 1040 |
|
1034 | 1041 |
it = _node_index.find(target_token); |
1035 | 1042 |
if (it == _node_index.end()) { |
1036 | 1043 |
std::ostringstream msg; |
1037 | 1044 |
msg << "Item not found: " << target_token; |
1038 | 1045 |
throw FormatError(msg.str()); |
1039 | 1046 |
} |
1040 | 1047 |
Node target = it->second; |
1041 | 1048 |
|
1042 | 1049 |
a = _digraph.addArc(source, target); |
1043 | 1050 |
if (label_index != -1) |
1044 | 1051 |
_arc_index.insert(std::make_pair(tokens[label_index], a)); |
1045 | 1052 |
} else { |
1046 | 1053 |
if (label_index == -1) |
1047 | 1054 |
throw FormatError("Label map not found"); |
1048 | 1055 |
typename std::map<std::string, Arc>::iterator it = |
1049 | 1056 |
_arc_index.find(tokens[label_index]); |
1050 | 1057 |
if (it == _arc_index.end()) { |
1051 | 1058 |
std::ostringstream msg; |
1052 | 1059 |
msg << "Arc with label not found: " << tokens[label_index]; |
1053 | 1060 |
throw FormatError(msg.str()); |
1054 | 1061 |
} |
1055 | 1062 |
a = it->second; |
1056 | 1063 |
} |
1057 | 1064 |
|
1058 | 1065 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
1059 | 1066 |
_arc_maps[i].second->set(a, tokens[map_index[i]]); |
1060 | 1067 |
} |
1061 | 1068 |
|
1062 | 1069 |
} |
1063 | 1070 |
if (readSuccess()) { |
1064 | 1071 |
line.putback(c); |
1065 | 1072 |
} |
1066 | 1073 |
} |
1067 | 1074 |
|
1068 | 1075 |
void readAttributes() { |
1069 | 1076 |
|
1070 | 1077 |
std::set<std::string> read_attr; |
1071 | 1078 |
|
1072 | 1079 |
char c; |
1073 | 1080 |
while (readLine() && line >> c && c != '@') { |
1074 | 1081 |
line.putback(c); |
1075 | 1082 |
|
1076 | 1083 |
std::string attr, token; |
1077 | 1084 |
if (!_reader_bits::readToken(line, attr)) |
1078 | 1085 |
throw FormatError("Attribute name not found"); |
1079 | 1086 |
if (!_reader_bits::readToken(line, token)) |
1080 | 1087 |
throw FormatError("Attribute value not found"); |
1081 | 1088 |
if (line >> c) |
1082 | 1089 |
throw FormatError("Extra character at the end of line"); |
1083 | 1090 |
|
1084 | 1091 |
{ |
1085 | 1092 |
std::set<std::string>::iterator it = read_attr.find(attr); |
1086 | 1093 |
if (it != read_attr.end()) { |
1087 | 1094 |
std::ostringstream msg; |
1088 | 1095 |
msg << "Multiple occurence of attribute: " << attr; |
1089 | 1096 |
throw FormatError(msg.str()); |
1090 | 1097 |
} |
1091 | 1098 |
read_attr.insert(attr); |
1092 | 1099 |
} |
1093 | 1100 |
|
1094 | 1101 |
{ |
1095 | 1102 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
1096 | 1103 |
while (it != _attributes.end() && it->first == attr) { |
1097 | 1104 |
it->second->set(token); |
1098 | 1105 |
++it; |
1099 | 1106 |
} |
1100 | 1107 |
} |
1101 | 1108 |
|
1102 | 1109 |
} |
1103 | 1110 |
if (readSuccess()) { |
1104 | 1111 |
line.putback(c); |
1105 | 1112 |
} |
1106 | 1113 |
for (typename Attributes::iterator it = _attributes.begin(); |
1107 | 1114 |
it != _attributes.end(); ++it) { |
1108 | 1115 |
if (read_attr.find(it->first) == read_attr.end()) { |
1109 | 1116 |
std::ostringstream msg; |
1110 | 1117 |
msg << "Attribute not found: " << it->first; |
1111 | 1118 |
throw FormatError(msg.str()); |
1112 | 1119 |
} |
1113 | 1120 |
} |
1114 | 1121 |
} |
1115 | 1122 |
|
1116 | 1123 |
public: |
1117 | 1124 |
|
1118 | 1125 |
/// \name Execution of the Reader |
1119 | 1126 |
/// @{ |
1120 | 1127 |
|
1121 | 1128 |
/// \brief Start the batch processing |
1122 | 1129 |
/// |
1123 | 1130 |
/// This function starts the batch processing |
1124 | 1131 |
void run() { |
1125 | 1132 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
1126 | 1133 |
|
1127 | 1134 |
bool nodes_done = _skip_nodes; |
1128 | 1135 |
bool arcs_done = _skip_arcs; |
1129 | 1136 |
bool attributes_done = false; |
1130 | 1137 |
|
1131 | 1138 |
line_num = 0; |
1132 | 1139 |
readLine(); |
1133 | 1140 |
skipSection(); |
1134 | 1141 |
|
1135 | 1142 |
while (readSuccess()) { |
1136 | 1143 |
try { |
1137 | 1144 |
char c; |
1138 | 1145 |
std::string section, caption; |
1139 | 1146 |
line >> c; |
1140 | 1147 |
_reader_bits::readToken(line, section); |
1141 | 1148 |
_reader_bits::readToken(line, caption); |
1142 | 1149 |
|
1143 | 1150 |
if (line >> c) |
1144 | 1151 |
throw FormatError("Extra character at the end of line"); |
1145 | 1152 |
|
1146 | 1153 |
if (section == "nodes" && !nodes_done) { |
1147 | 1154 |
if (_nodes_caption.empty() || _nodes_caption == caption) { |
1148 | 1155 |
readNodes(); |
1149 | 1156 |
nodes_done = true; |
1150 | 1157 |
} |
1151 | 1158 |
} else if ((section == "arcs" || section == "edges") && |
1152 | 1159 |
!arcs_done) { |
1153 | 1160 |
if (_arcs_caption.empty() || _arcs_caption == caption) { |
1154 | 1161 |
readArcs(); |
1155 | 1162 |
arcs_done = true; |
1156 | 1163 |
} |
1157 | 1164 |
} else if (section == "attributes" && !attributes_done) { |
1158 | 1165 |
if (_attributes_caption.empty() || _attributes_caption == caption) { |
1159 | 1166 |
readAttributes(); |
1160 | 1167 |
attributes_done = true; |
1161 | 1168 |
} |
1162 | 1169 |
} else { |
1163 | 1170 |
readLine(); |
1164 | 1171 |
skipSection(); |
1165 | 1172 |
} |
1166 | 1173 |
} catch (FormatError& error) { |
1167 | 1174 |
error.line(line_num); |
1168 | 1175 |
error.file(_filename); |
1169 | 1176 |
throw; |
1170 | 1177 |
} |
1171 | 1178 |
} |
1172 | 1179 |
|
1173 | 1180 |
if (!nodes_done) { |
1174 | 1181 |
throw FormatError("Section @nodes not found"); |
1175 | 1182 |
} |
1176 | 1183 |
|
1177 | 1184 |
if (!arcs_done) { |
1178 | 1185 |
throw FormatError("Section @arcs not found"); |
1179 | 1186 |
} |
1180 | 1187 |
|
1181 | 1188 |
if (!attributes_done && !_attributes.empty()) { |
1182 | 1189 |
throw FormatError("Section @attributes not found"); |
1183 | 1190 |
} |
1184 | 1191 |
|
1185 | 1192 |
} |
1186 | 1193 |
|
1187 | 1194 |
/// @} |
1188 | 1195 |
|
1189 | 1196 |
}; |
1190 | 1197 |
|
1191 | 1198 |
/// \ingroup lemon_io |
1192 | 1199 |
/// |
1193 | 1200 |
/// \brief Return a \ref DigraphReader class |
1194 | 1201 |
/// |
1195 | 1202 |
/// This function just returns a \ref DigraphReader class. |
1196 | 1203 |
/// |
1197 | 1204 |
/// With this function a digraph can be read from an |
1198 | 1205 |
/// \ref lgf-format "LGF" file or input stream with several maps and |
1199 | 1206 |
/// attributes. For example, there is network flow problem on a |
1200 | 1207 |
/// digraph, i.e. a digraph with a \e capacity map on the arcs and |
1201 | 1208 |
/// \e source and \e target nodes. This digraph can be read with the |
1202 | 1209 |
/// following code: |
1203 | 1210 |
/// |
1204 | 1211 |
///\code |
1205 | 1212 |
///ListDigraph digraph; |
1206 | 1213 |
///ListDigraph::ArcMap<int> cm(digraph); |
1207 | 1214 |
///ListDigraph::Node src, trg; |
1208 | 1215 |
///digraphReader(digraph, std::cin). |
1209 | 1216 |
/// arcMap("capacity", cap). |
1210 | 1217 |
/// node("source", src). |
1211 | 1218 |
/// node("target", trg). |
1212 | 1219 |
/// run(); |
1213 | 1220 |
///\endcode |
1214 | 1221 |
/// |
1215 | 1222 |
/// For a complete documentation, please see the \ref DigraphReader |
1216 | 1223 |
/// class documentation. |
1217 | 1224 |
/// \warning Don't forget to put the \ref DigraphReader::run() "run()" |
1218 | 1225 |
/// to the end of the parameter list. |
1219 | 1226 |
/// \relates DigraphReader |
1220 | 1227 |
/// \sa digraphReader(TDGR& digraph, const std::string& fn) |
1221 | 1228 |
/// \sa digraphReader(TDGR& digraph, const char* fn) |
1222 | 1229 |
template <typename TDGR> |
1223 | 1230 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is) { |
1224 | 1231 |
DigraphReader<TDGR> tmp(digraph, is); |
1225 | 1232 |
return tmp; |
1226 | 1233 |
} |
1227 | 1234 |
|
1228 | 1235 |
/// \brief Return a \ref DigraphReader class |
1229 | 1236 |
/// |
1230 | 1237 |
/// This function just returns a \ref DigraphReader class. |
1231 | 1238 |
/// \relates DigraphReader |
1232 | 1239 |
/// \sa digraphReader(TDGR& digraph, std::istream& is) |
1233 | 1240 |
template <typename TDGR> |
1234 | 1241 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn) { |
1235 | 1242 |
DigraphReader<TDGR> tmp(digraph, fn); |
1236 | 1243 |
return tmp; |
1237 | 1244 |
} |
1238 | 1245 |
|
1239 | 1246 |
/// \brief Return a \ref DigraphReader class |
1240 | 1247 |
/// |
1241 | 1248 |
/// This function just returns a \ref DigraphReader class. |
1242 | 1249 |
/// \relates DigraphReader |
1243 | 1250 |
/// \sa digraphReader(TDGR& digraph, std::istream& is) |
1244 | 1251 |
template <typename TDGR> |
1245 | 1252 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, const char* fn) { |
1246 | 1253 |
DigraphReader<TDGR> tmp(digraph, fn); |
1247 | 1254 |
return tmp; |
1248 | 1255 |
} |
1249 | 1256 |
|
1250 | 1257 |
template <typename GR> |
1251 | 1258 |
class GraphReader; |
1252 | 1259 |
|
1253 | 1260 |
template <typename TGR> |
1254 | 1261 |
GraphReader<TGR> graphReader(TGR& graph, std::istream& is = std::cin); |
1255 | 1262 |
template <typename TGR> |
1256 | 1263 |
GraphReader<TGR> graphReader(TGR& graph, const std::string& fn); |
1257 | 1264 |
template <typename TGR> |
1258 | 1265 |
GraphReader<TGR> graphReader(TGR& graph, const char *fn); |
1259 | 1266 |
|
1260 | 1267 |
/// \ingroup lemon_io |
1261 | 1268 |
/// |
1262 | 1269 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs |
1263 | 1270 |
/// |
1264 | 1271 |
/// This utility reads an \ref lgf-format "LGF" file. |
1265 | 1272 |
/// |
1266 | 1273 |
/// It can be used almost the same way as \c DigraphReader. |
1267 | 1274 |
/// The only difference is that this class can handle edges and |
1268 | 1275 |
/// edge maps as well as arcs and arc maps. |
1269 | 1276 |
/// |
1270 | 1277 |
/// The columns in the \c \@edges (or \c \@arcs) section are the |
1271 | 1278 |
/// edge maps. However, if there are two maps with the same name |
1272 | 1279 |
/// prefixed with \c '+' and \c '-', then these can be read into an |
1273 | 1280 |
/// arc map. Similarly, an attribute can be read into an arc, if |
1274 | 1281 |
/// it's value is an edge label prefixed with \c '+' or \c '-'. |
1275 | 1282 |
template <typename GR> |
1276 | 1283 |
class GraphReader { |
1277 | 1284 |
public: |
1278 | 1285 |
|
1279 | 1286 |
typedef GR Graph; |
1280 | 1287 |
|
1281 | 1288 |
private: |
1282 | 1289 |
|
1283 | 1290 |
TEMPLATE_GRAPH_TYPEDEFS(GR); |
1284 | 1291 |
|
1285 | 1292 |
std::istream* _is; |
1286 | 1293 |
bool local_is; |
1287 | 1294 |
std::string _filename; |
1288 | 1295 |
|
1289 | 1296 |
GR& _graph; |
1290 | 1297 |
|
1291 | 1298 |
std::string _nodes_caption; |
1292 | 1299 |
std::string _edges_caption; |
1293 | 1300 |
std::string _attributes_caption; |
1294 | 1301 |
|
1295 | 1302 |
typedef std::map<std::string, Node> NodeIndex; |
1296 | 1303 |
NodeIndex _node_index; |
1297 | 1304 |
typedef std::map<std::string, Edge> EdgeIndex; |
1298 | 1305 |
EdgeIndex _edge_index; |
1299 | 1306 |
|
1300 | 1307 |
typedef std::vector<std::pair<std::string, |
1301 | 1308 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
1302 | 1309 |
NodeMaps _node_maps; |
1303 | 1310 |
|
1304 | 1311 |
typedef std::vector<std::pair<std::string, |
1305 | 1312 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
1306 | 1313 |
EdgeMaps _edge_maps; |
1307 | 1314 |
|
1308 | 1315 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
1309 | 1316 |
Attributes; |
1310 | 1317 |
Attributes _attributes; |
1311 | 1318 |
|
1312 | 1319 |
bool _use_nodes; |
1313 | 1320 |
bool _use_edges; |
1314 | 1321 |
|
1315 | 1322 |
bool _skip_nodes; |
1316 | 1323 |
bool _skip_edges; |
1317 | 1324 |
|
1318 | 1325 |
int line_num; |
1319 | 1326 |
std::istringstream line; |
1320 | 1327 |
|
1321 | 1328 |
public: |
1322 | 1329 |
|
1323 | 1330 |
/// \brief Constructor |
1324 | 1331 |
/// |
1325 | 1332 |
/// Construct an undirected graph reader, which reads from the given |
1326 | 1333 |
/// input stream. |
1327 | 1334 |
GraphReader(GR& graph, std::istream& is = std::cin) |
1328 | 1335 |
: _is(&is), local_is(false), _graph(graph), |
1329 | 1336 |
_use_nodes(false), _use_edges(false), |
1330 | 1337 |
_skip_nodes(false), _skip_edges(false) {} |
1331 | 1338 |
|
1332 | 1339 |
/// \brief Constructor |
1333 | 1340 |
/// |
1334 | 1341 |
/// Construct an undirected graph reader, which reads from the given |
1335 | 1342 |
/// file. |
1336 | 1343 |
GraphReader(GR& graph, const std::string& fn) |
1337 | 1344 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
1338 | 1345 |
_filename(fn), _graph(graph), |
1339 | 1346 |
_use_nodes(false), _use_edges(false), |
1340 | 1347 |
_skip_nodes(false), _skip_edges(false) { |
1341 | 1348 |
if (!(*_is)) { |
1342 | 1349 |
delete _is; |
1343 | 1350 |
throw IoError("Cannot open file", fn); |
1344 | 1351 |
} |
1345 | 1352 |
} |
1346 | 1353 |
|
1347 | 1354 |
/// \brief Constructor |
1348 | 1355 |
/// |
1349 | 1356 |
/// Construct an undirected graph reader, which reads from the given |
1350 | 1357 |
/// file. |
1351 | 1358 |
GraphReader(GR& graph, const char* fn) |
1352 | 1359 |
: _is(new std::ifstream(fn)), local_is(true), |
1353 | 1360 |
_filename(fn), _graph(graph), |
1354 | 1361 |
_use_nodes(false), _use_edges(false), |
1355 | 1362 |
_skip_nodes(false), _skip_edges(false) { |
1356 | 1363 |
if (!(*_is)) { |
1357 | 1364 |
delete _is; |
1358 | 1365 |
throw IoError("Cannot open file", fn); |
1359 | 1366 |
} |
1360 | 1367 |
} |
1361 | 1368 |
|
1362 | 1369 |
/// \brief Destructor |
1363 | 1370 |
~GraphReader() { |
1364 | 1371 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
1365 | 1372 |
it != _node_maps.end(); ++it) { |
1366 | 1373 |
delete it->second; |
1367 | 1374 |
} |
1368 | 1375 |
|
1369 | 1376 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
1370 | 1377 |
it != _edge_maps.end(); ++it) { |
1371 | 1378 |
delete it->second; |
1372 | 1379 |
} |
1373 | 1380 |
|
1374 | 1381 |
for (typename Attributes::iterator it = _attributes.begin(); |
1375 | 1382 |
it != _attributes.end(); ++it) { |
1376 | 1383 |
delete it->second; |
1377 | 1384 |
} |
1378 | 1385 |
|
1379 | 1386 |
if (local_is) { |
1380 | 1387 |
delete _is; |
1381 | 1388 |
} |
1382 | 1389 |
|
1383 | 1390 |
} |
1384 | 1391 |
|
1385 | 1392 |
private: |
1386 | 1393 |
template <typename TGR> |
1387 | 1394 |
friend GraphReader<TGR> graphReader(TGR& graph, std::istream& is); |
1388 | 1395 |
template <typename TGR> |
1389 | 1396 |
friend GraphReader<TGR> graphReader(TGR& graph, const std::string& fn); |
1390 | 1397 |
template <typename TGR> |
1391 | 1398 |
friend GraphReader<TGR> graphReader(TGR& graph, const char *fn); |
1392 | 1399 |
|
1393 | 1400 |
GraphReader(GraphReader& other) |
1394 | 1401 |
: _is(other._is), local_is(other.local_is), _graph(other._graph), |
1395 | 1402 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges), |
1396 | 1403 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
1397 | 1404 |
|
1398 | 1405 |
other._is = 0; |
1399 | 1406 |
other.local_is = false; |
1400 | 1407 |
|
1401 | 1408 |
_node_index.swap(other._node_index); |
1402 | 1409 |
_edge_index.swap(other._edge_index); |
1403 | 1410 |
|
1404 | 1411 |
_node_maps.swap(other._node_maps); |
1405 | 1412 |
_edge_maps.swap(other._edge_maps); |
1406 | 1413 |
_attributes.swap(other._attributes); |
1407 | 1414 |
|
1408 | 1415 |
_nodes_caption = other._nodes_caption; |
1409 | 1416 |
_edges_caption = other._edges_caption; |
1410 | 1417 |
_attributes_caption = other._attributes_caption; |
1411 | 1418 |
|
1412 | 1419 |
} |
1413 | 1420 |
|
1414 | 1421 |
GraphReader& operator=(const GraphReader&); |
1415 | 1422 |
|
1416 | 1423 |
public: |
1417 | 1424 |
|
1418 | 1425 |
/// \name Reading Rules |
1419 | 1426 |
/// @{ |
1420 | 1427 |
|
1421 | 1428 |
/// \brief Node map reading rule |
1422 | 1429 |
/// |
1423 | 1430 |
/// Add a node map reading rule to the reader. |
1424 | 1431 |
template <typename Map> |
1425 | 1432 |
GraphReader& nodeMap(const std::string& caption, Map& map) { |
1426 | 1433 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
1427 | 1434 |
_reader_bits::MapStorageBase<Node>* storage = |
1428 | 1435 |
new _reader_bits::MapStorage<Node, Map>(map); |
1429 | 1436 |
_node_maps.push_back(std::make_pair(caption, storage)); |
1430 | 1437 |
return *this; |
1431 | 1438 |
} |
1432 | 1439 |
|
1433 | 1440 |
/// \brief Node map reading rule |
1434 | 1441 |
/// |
1435 | 1442 |
/// Add a node map reading rule with specialized converter to the |
1436 | 1443 |
/// reader. |
1437 | 1444 |
template <typename Map, typename Converter> |
1438 | 1445 |
GraphReader& nodeMap(const std::string& caption, Map& map, |
1439 | 1446 |
const Converter& converter = Converter()) { |
1440 | 1447 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
1441 | 1448 |
_reader_bits::MapStorageBase<Node>* storage = |
1442 | 1449 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
1443 | 1450 |
_node_maps.push_back(std::make_pair(caption, storage)); |
1444 | 1451 |
return *this; |
1445 | 1452 |
} |
1446 | 1453 |
|
1447 | 1454 |
/// \brief Edge map reading rule |
1448 | 1455 |
/// |
1449 | 1456 |
/// Add an edge map reading rule to the reader. |
1450 | 1457 |
template <typename Map> |
1451 | 1458 |
GraphReader& edgeMap(const std::string& caption, Map& map) { |
1452 | 1459 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
1453 | 1460 |
_reader_bits::MapStorageBase<Edge>* storage = |
1454 | 1461 |
new _reader_bits::MapStorage<Edge, Map>(map); |
1455 | 1462 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
1456 | 1463 |
return *this; |
1457 | 1464 |
} |
1458 | 1465 |
|
1459 | 1466 |
/// \brief Edge map reading rule |
1460 | 1467 |
/// |
1461 | 1468 |
/// Add an edge map reading rule with specialized converter to the |
1462 | 1469 |
/// reader. |
1463 | 1470 |
template <typename Map, typename Converter> |
1464 | 1471 |
GraphReader& edgeMap(const std::string& caption, Map& map, |
1465 | 1472 |
const Converter& converter = Converter()) { |
1466 | 1473 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
1467 | 1474 |
_reader_bits::MapStorageBase<Edge>* storage = |
1468 | 1475 |
new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter); |
1469 | 1476 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
1470 | 1477 |
return *this; |
1471 | 1478 |
} |
1472 | 1479 |
|
1473 | 1480 |
/// \brief Arc map reading rule |
1474 | 1481 |
/// |
1475 | 1482 |
/// Add an arc map reading rule to the reader. |
1476 | 1483 |
template <typename Map> |
1477 | 1484 |
GraphReader& arcMap(const std::string& caption, Map& map) { |
1478 | 1485 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
1479 | 1486 |
_reader_bits::MapStorageBase<Edge>* forward_storage = |
1480 | 1487 |
new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
1481 | 1488 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
1482 | 1489 |
_reader_bits::MapStorageBase<Edge>* backward_storage = |
1483 | 1490 |
new _reader_bits::GraphArcMapStorage<GR, false, Map>(_graph, map); |
1484 | 1491 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
1485 | 1492 |
return *this; |
1486 | 1493 |
} |
1487 | 1494 |
|
1488 | 1495 |
/// \brief Arc map reading rule |
1489 | 1496 |
/// |
1490 | 1497 |
/// Add an arc map reading rule with specialized converter to the |
1491 | 1498 |
/// reader. |
1492 | 1499 |
template <typename Map, typename Converter> |
1493 | 1500 |
GraphReader& arcMap(const std::string& caption, Map& map, |
1494 | 1501 |
const Converter& converter = Converter()) { |
1495 | 1502 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
1496 | 1503 |
_reader_bits::MapStorageBase<Edge>* forward_storage = |
1497 | 1504 |
new _reader_bits::GraphArcMapStorage<GR, true, Map, Converter> |
1498 | 1505 |
(_graph, map, converter); |
1499 | 1506 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
1500 | 1507 |
_reader_bits::MapStorageBase<Edge>* backward_storage = |
1501 | 1508 |
new _reader_bits::GraphArcMapStorage<GR, false, Map, Converter> |
1502 | 1509 |
(_graph, map, converter); |
1503 | 1510 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
1504 | 1511 |
return *this; |
1505 | 1512 |
} |
1506 | 1513 |
|
1507 | 1514 |
/// \brief Attribute reading rule |
1508 | 1515 |
/// |
1509 | 1516 |
/// Add an attribute reading rule to the reader. |
1510 | 1517 |
template <typename Value> |
1511 | 1518 |
GraphReader& attribute(const std::string& caption, Value& value) { |
1512 | 1519 |
_reader_bits::ValueStorageBase* storage = |
1513 | 1520 |
new _reader_bits::ValueStorage<Value>(value); |
1514 | 1521 |
_attributes.insert(std::make_pair(caption, storage)); |
1515 | 1522 |
return *this; |
1516 | 1523 |
} |
1517 | 1524 |
|
1518 | 1525 |
/// \brief Attribute reading rule |
1519 | 1526 |
/// |
1520 | 1527 |
/// Add an attribute reading rule with specialized converter to the |
1521 | 1528 |
/// reader. |
1522 | 1529 |
template <typename Value, typename Converter> |
1523 | 1530 |
GraphReader& attribute(const std::string& caption, Value& value, |
1524 | 1531 |
const Converter& converter = Converter()) { |
1525 | 1532 |
_reader_bits::ValueStorageBase* storage = |
1526 | 1533 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
1527 | 1534 |
_attributes.insert(std::make_pair(caption, storage)); |
1528 | 1535 |
return *this; |
1529 | 1536 |
} |
1530 | 1537 |
|
1531 | 1538 |
/// \brief Node reading rule |
1532 | 1539 |
/// |
1533 | 1540 |
/// Add a node reading rule to reader. |
1534 | 1541 |
GraphReader& node(const std::string& caption, Node& node) { |
1535 | 1542 |
typedef _reader_bits::MapLookUpConverter<Node> Converter; |
1536 | 1543 |
Converter converter(_node_index); |
1537 | 1544 |
_reader_bits::ValueStorageBase* storage = |
1538 | 1545 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
1539 | 1546 |
_attributes.insert(std::make_pair(caption, storage)); |
1540 | 1547 |
return *this; |
1541 | 1548 |
} |
1542 | 1549 |
|
1543 | 1550 |
/// \brief Edge reading rule |
1544 | 1551 |
/// |
1545 | 1552 |
/// Add an edge reading rule to reader. |
1546 | 1553 |
GraphReader& edge(const std::string& caption, Edge& edge) { |
1547 | 1554 |
typedef _reader_bits::MapLookUpConverter<Edge> Converter; |
1548 | 1555 |
Converter converter(_edge_index); |
1549 | 1556 |
_reader_bits::ValueStorageBase* storage = |
1550 | 1557 |
new _reader_bits::ValueStorage<Edge, Converter>(edge, converter); |
1551 | 1558 |
_attributes.insert(std::make_pair(caption, storage)); |
1552 | 1559 |
return *this; |
1553 | 1560 |
} |
1554 | 1561 |
|
1555 | 1562 |
/// \brief Arc reading rule |
1556 | 1563 |
/// |
1557 | 1564 |
/// Add an arc reading rule to reader. |
1558 | 1565 |
GraphReader& arc(const std::string& caption, Arc& arc) { |
1559 | 1566 |
typedef _reader_bits::GraphArcLookUpConverter<GR> Converter; |
1560 | 1567 |
Converter converter(_graph, _edge_index); |
1561 | 1568 |
_reader_bits::ValueStorageBase* storage = |
1562 | 1569 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
1563 | 1570 |
_attributes.insert(std::make_pair(caption, storage)); |
1564 | 1571 |
return *this; |
1565 | 1572 |
} |
1566 | 1573 |
|
1567 | 1574 |
/// @} |
1568 | 1575 |
|
1569 | 1576 |
/// \name Select Section by Name |
1570 | 1577 |
/// @{ |
1571 | 1578 |
|
1572 | 1579 |
/// \brief Set \c \@nodes section to be read |
1573 | 1580 |
/// |
1574 | 1581 |
/// Set \c \@nodes section to be read. |
1575 | 1582 |
GraphReader& nodes(const std::string& caption) { |
1576 | 1583 |
_nodes_caption = caption; |
1577 | 1584 |
return *this; |
1578 | 1585 |
} |
1579 | 1586 |
|
1580 | 1587 |
/// \brief Set \c \@edges section to be read |
1581 | 1588 |
/// |
1582 | 1589 |
/// Set \c \@edges section to be read. |
1583 | 1590 |
GraphReader& edges(const std::string& caption) { |
1584 | 1591 |
_edges_caption = caption; |
1585 | 1592 |
return *this; |
1586 | 1593 |
} |
1587 | 1594 |
|
1588 | 1595 |
/// \brief Set \c \@attributes section to be read |
1589 | 1596 |
/// |
1590 | 1597 |
/// Set \c \@attributes section to be read. |
1591 | 1598 |
GraphReader& attributes(const std::string& caption) { |
1592 | 1599 |
_attributes_caption = caption; |
1593 | 1600 |
return *this; |
1594 | 1601 |
} |
1595 | 1602 |
|
1596 | 1603 |
/// @} |
1597 | 1604 |
|
1598 | 1605 |
/// \name Using Previously Constructed Node or Edge Set |
1599 | 1606 |
/// @{ |
1600 | 1607 |
|
1601 | 1608 |
/// \brief Use previously constructed node set |
1602 | 1609 |
/// |
1603 | 1610 |
/// Use previously constructed node set, and specify the node |
1604 | 1611 |
/// label map. |
1605 | 1612 |
template <typename Map> |
1606 | 1613 |
GraphReader& useNodes(const Map& map) { |
1607 | 1614 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
1608 | 1615 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
1609 | 1616 |
_use_nodes = true; |
1610 | 1617 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
1611 | 1618 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
1612 | 1619 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
1613 | 1620 |
} |
1614 | 1621 |
return *this; |
1615 | 1622 |
} |
1616 | 1623 |
|
1617 | 1624 |
/// \brief Use previously constructed node set |
1618 | 1625 |
/// |
1619 | 1626 |
/// Use previously constructed node set, and specify the node |
1620 | 1627 |
/// label map and a functor which converts the label map values to |
1621 | 1628 |
/// \c std::string. |
1622 | 1629 |
template <typename Map, typename Converter> |
1623 | 1630 |
GraphReader& useNodes(const Map& map, |
1624 | 1631 |
const Converter& converter = Converter()) { |
1625 | 1632 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
1626 | 1633 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
1627 | 1634 |
_use_nodes = true; |
1628 | 1635 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
1629 | 1636 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
1630 | 1637 |
} |
1631 | 1638 |
return *this; |
1632 | 1639 |
} |
1633 | 1640 |
|
1634 | 1641 |
/// \brief Use previously constructed edge set |
1635 | 1642 |
/// |
1636 | 1643 |
/// Use previously constructed edge set, and specify the edge |
1637 | 1644 |
/// label map. |
1638 | 1645 |
template <typename Map> |
1639 | 1646 |
GraphReader& useEdges(const Map& map) { |
1640 | 1647 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
1641 | 1648 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
1642 | 1649 |
_use_edges = true; |
1643 | 1650 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
1644 | 1651 |
for (EdgeIt a(_graph); a != INVALID; ++a) { |
1645 | 1652 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
1646 | 1653 |
} |
1647 | 1654 |
return *this; |
1648 | 1655 |
} |
1649 | 1656 |
|
1650 | 1657 |
/// \brief Use previously constructed edge set |
1651 | 1658 |
/// |
1652 | 1659 |
/// Use previously constructed edge set, and specify the edge |
1653 | 1660 |
/// label map and a functor which converts the label map values to |
1654 | 1661 |
/// \c std::string. |
1655 | 1662 |
template <typename Map, typename Converter> |
1656 | 1663 |
GraphReader& useEdges(const Map& map, |
1657 | 1664 |
const Converter& converter = Converter()) { |
1658 | 1665 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
1659 | 1666 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
1660 | 1667 |
_use_edges = true; |
1661 | 1668 |
for (EdgeIt a(_graph); a != INVALID; ++a) { |
1662 | 1669 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
1663 | 1670 |
} |
1664 | 1671 |
return *this; |
1665 | 1672 |
} |
1666 | 1673 |
|
1667 | 1674 |
/// \brief Skip the reading of node section |
1668 | 1675 |
/// |
1669 | 1676 |
/// Omit the reading of the node section. This implies that each node |
1670 | 1677 |
/// map reading rule will be abandoned, and the nodes of the graph |
1671 | 1678 |
/// will not be constructed, which usually cause that the edge set |
1672 | 1679 |
/// could not be read due to lack of node name |
1673 | 1680 |
/// could not be read due to lack of node name resolving. |
1674 | 1681 |
/// Therefore \c skipEdges() function should also be used, or |
1675 | 1682 |
/// \c useNodes() should be used to specify the label of the nodes. |
1676 | 1683 |
GraphReader& skipNodes() { |
1677 | 1684 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
1678 | 1685 |
_skip_nodes = true; |
1679 | 1686 |
return *this; |
1680 | 1687 |
} |
1681 | 1688 |
|
1682 | 1689 |
/// \brief Skip the reading of edge section |
1683 | 1690 |
/// |
1684 | 1691 |
/// Omit the reading of the edge section. This implies that each edge |
1685 | 1692 |
/// map reading rule will be abandoned, and the edges of the graph |
1686 | 1693 |
/// will not be constructed. |
1687 | 1694 |
GraphReader& skipEdges() { |
1688 | 1695 |
LEMON_ASSERT(!_skip_edges, "Skip edges already set"); |
1689 | 1696 |
_skip_edges = true; |
1690 | 1697 |
return *this; |
1691 | 1698 |
} |
1692 | 1699 |
|
1693 | 1700 |
/// @} |
1694 | 1701 |
|
1695 | 1702 |
private: |
1696 | 1703 |
|
1697 | 1704 |
bool readLine() { |
1698 | 1705 |
std::string str; |
1699 | 1706 |
while(++line_num, std::getline(*_is, str)) { |
1700 | 1707 |
line.clear(); line.str(str); |
1701 | 1708 |
char c; |
1702 | 1709 |
if (line >> std::ws >> c && c != '#') { |
1703 | 1710 |
line.putback(c); |
1704 | 1711 |
return true; |
1705 | 1712 |
} |
1706 | 1713 |
} |
1707 | 1714 |
return false; |
1708 | 1715 |
} |
1709 | 1716 |
|
1710 | 1717 |
bool readSuccess() { |
1711 | 1718 |
return static_cast<bool>(*_is); |
1712 | 1719 |
} |
1713 | 1720 |
|
1714 | 1721 |
void skipSection() { |
1715 | 1722 |
char c; |
1716 | 1723 |
while (readSuccess() && line >> c && c != '@') { |
1717 | 1724 |
readLine(); |
1718 | 1725 |
} |
1719 | 1726 |
if (readSuccess()) { |
1720 | 1727 |
line.putback(c); |
1721 | 1728 |
} |
1722 | 1729 |
} |
1723 | 1730 |
|
1724 | 1731 |
void readNodes() { |
1725 | 1732 |
|
1726 | 1733 |
std::vector<int> map_index(_node_maps.size()); |
1727 | 1734 |
int map_num, label_index; |
1728 | 1735 |
|
1729 | 1736 |
char c; |
1730 | 1737 |
if (!readLine() || !(line >> c) || c == '@') { |
1731 | 1738 |
if (readSuccess() && line) line.putback(c); |
1732 | 1739 |
if (!_node_maps.empty()) |
1733 | 1740 |
throw FormatError("Cannot find map names"); |
1734 | 1741 |
return; |
1735 | 1742 |
} |
1736 | 1743 |
line.putback(c); |
1737 | 1744 |
|
1738 | 1745 |
{ |
1739 | 1746 |
std::map<std::string, int> maps; |
1740 | 1747 |
|
1741 | 1748 |
std::string map; |
1742 | 1749 |
int index = 0; |
1743 | 1750 |
while (_reader_bits::readToken(line, map)) { |
1744 | 1751 |
if (maps.find(map) != maps.end()) { |
1745 | 1752 |
std::ostringstream msg; |
1746 | 1753 |
msg << "Multiple occurence of node map: " << map; |
1747 | 1754 |
throw FormatError(msg.str()); |
1748 | 1755 |
} |
1749 | 1756 |
maps.insert(std::make_pair(map, index)); |
1750 | 1757 |
++index; |
1751 | 1758 |
} |
1752 | 1759 |
|
1753 | 1760 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
1754 | 1761 |
std::map<std::string, int>::iterator jt = |
1755 | 1762 |
maps.find(_node_maps[i].first); |
1756 | 1763 |
if (jt == maps.end()) { |
1757 | 1764 |
std::ostringstream msg; |
1758 | 1765 |
msg << "Map not found: " << _node_maps[i].first; |
1759 | 1766 |
throw FormatError(msg.str()); |
1760 | 1767 |
} |
1761 | 1768 |
map_index[i] = jt->second; |
1762 | 1769 |
} |
1763 | 1770 |
|
1764 | 1771 |
{ |
1765 | 1772 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
1766 | 1773 |
if (jt != maps.end()) { |
1767 | 1774 |
label_index = jt->second; |
1768 | 1775 |
} else { |
1769 | 1776 |
label_index = -1; |
1770 | 1777 |
} |
1771 | 1778 |
} |
1772 | 1779 |
map_num = maps.size(); |
1773 | 1780 |
} |
1774 | 1781 |
|
1775 | 1782 |
while (readLine() && line >> c && c != '@') { |
1776 | 1783 |
line.putback(c); |
1777 | 1784 |
|
1778 | 1785 |
std::vector<std::string> tokens(map_num); |
1779 | 1786 |
for (int i = 0; i < map_num; ++i) { |
1780 | 1787 |
if (!_reader_bits::readToken(line, tokens[i])) { |
1781 | 1788 |
std::ostringstream msg; |
1782 | 1789 |
msg << "Column not found (" << i + 1 << ")"; |
1783 | 1790 |
throw FormatError(msg.str()); |
1784 | 1791 |
} |
1785 | 1792 |
} |
1786 | 1793 |
if (line >> std::ws >> c) |
1787 | 1794 |
throw FormatError("Extra character at the end of line"); |
1788 | 1795 |
|
1789 | 1796 |
Node n; |
1790 | 1797 |
if (!_use_nodes) { |
1791 | 1798 |
n = _graph.addNode(); |
1792 | 1799 |
if (label_index != -1) |
1793 | 1800 |
_node_index.insert(std::make_pair(tokens[label_index], n)); |
1794 | 1801 |
} else { |
1795 | 1802 |
if (label_index == -1) |
1796 | 1803 |
throw FormatError("Label map not found"); |
1797 | 1804 |
typename std::map<std::string, Node>::iterator it = |
1798 | 1805 |
_node_index.find(tokens[label_index]); |
1799 | 1806 |
if (it == _node_index.end()) { |
1800 | 1807 |
std::ostringstream msg; |
1801 | 1808 |
msg << "Node with label not found: " << tokens[label_index]; |
1802 | 1809 |
throw FormatError(msg.str()); |
1803 | 1810 |
} |
1804 | 1811 |
n = it->second; |
1805 | 1812 |
} |
1806 | 1813 |
|
1807 | 1814 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
1808 | 1815 |
_node_maps[i].second->set(n, tokens[map_index[i]]); |
1809 | 1816 |
} |
1810 | 1817 |
|
1811 | 1818 |
} |
1812 | 1819 |
if (readSuccess()) { |
1813 | 1820 |
line.putback(c); |
1814 | 1821 |
} |
1815 | 1822 |
} |
1816 | 1823 |
|
1817 | 1824 |
void readEdges() { |
1818 | 1825 |
|
1819 | 1826 |
std::vector<int> map_index(_edge_maps.size()); |
1820 | 1827 |
int map_num, label_index; |
1821 | 1828 |
|
1822 | 1829 |
char c; |
1823 | 1830 |
if (!readLine() || !(line >> c) || c == '@') { |
1824 | 1831 |
if (readSuccess() && line) line.putback(c); |
1825 | 1832 |
if (!_edge_maps.empty()) |
1826 | 1833 |
throw FormatError("Cannot find map names"); |
1827 | 1834 |
return; |
1828 | 1835 |
} |
1829 | 1836 |
line.putback(c); |
1830 | 1837 |
|
1831 | 1838 |
{ |
1832 | 1839 |
std::map<std::string, int> maps; |
1833 | 1840 |
|
1834 | 1841 |
std::string map; |
1835 | 1842 |
int index = 0; |
1836 | 1843 |
while (_reader_bits::readToken(line, map)) { |
1844 |
if(map == "-") { |
|
1845 |
if(index!=0) |
|
1846 |
throw FormatError("'-' is not allowed as a map name"); |
|
1847 |
else if (line >> std::ws >> c) |
|
1848 |
throw FormatError("Extra character at the end of line"); |
|
1849 |
else break; |
|
1850 |
} |
|
1837 | 1851 |
if (maps.find(map) != maps.end()) { |
1838 | 1852 |
std::ostringstream msg; |
1839 | 1853 |
msg << "Multiple occurence of edge map: " << map; |
1840 | 1854 |
throw FormatError(msg.str()); |
1841 | 1855 |
} |
1842 | 1856 |
maps.insert(std::make_pair(map, index)); |
1843 | 1857 |
++index; |
1844 | 1858 |
} |
1845 | 1859 |
|
1846 | 1860 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
1847 | 1861 |
std::map<std::string, int>::iterator jt = |
1848 | 1862 |
maps.find(_edge_maps[i].first); |
1849 | 1863 |
if (jt == maps.end()) { |
1850 | 1864 |
std::ostringstream msg; |
1851 | 1865 |
msg << "Map not found: " << _edge_maps[i].first; |
1852 | 1866 |
throw FormatError(msg.str()); |
1853 | 1867 |
} |
1854 | 1868 |
map_index[i] = jt->second; |
1855 | 1869 |
} |
1856 | 1870 |
|
1857 | 1871 |
{ |
1858 | 1872 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
1859 | 1873 |
if (jt != maps.end()) { |
1860 | 1874 |
label_index = jt->second; |
1861 | 1875 |
} else { |
1862 | 1876 |
label_index = -1; |
1863 | 1877 |
} |
1864 | 1878 |
} |
1865 | 1879 |
map_num = maps.size(); |
1866 | 1880 |
} |
1867 | 1881 |
|
1868 | 1882 |
while (readLine() && line >> c && c != '@') { |
1869 | 1883 |
line.putback(c); |
1870 | 1884 |
|
1871 | 1885 |
std::string source_token; |
1872 | 1886 |
std::string target_token; |
1873 | 1887 |
|
1874 | 1888 |
if (!_reader_bits::readToken(line, source_token)) |
1875 | 1889 |
throw FormatError("Node u not found"); |
1876 | 1890 |
|
1877 | 1891 |
if (!_reader_bits::readToken(line, target_token)) |
1878 | 1892 |
throw FormatError("Node v not found"); |
1879 | 1893 |
|
1880 | 1894 |
std::vector<std::string> tokens(map_num); |
1881 | 1895 |
for (int i = 0; i < map_num; ++i) { |
1882 | 1896 |
if (!_reader_bits::readToken(line, tokens[i])) { |
1883 | 1897 |
std::ostringstream msg; |
1884 | 1898 |
msg << "Column not found (" << i + 1 << ")"; |
1885 | 1899 |
throw FormatError(msg.str()); |
1886 | 1900 |
} |
1887 | 1901 |
} |
1888 | 1902 |
if (line >> std::ws >> c) |
1889 | 1903 |
throw FormatError("Extra character at the end of line"); |
1890 | 1904 |
|
1891 | 1905 |
Edge e; |
1892 | 1906 |
if (!_use_edges) { |
1893 | 1907 |
|
1894 | 1908 |
typename NodeIndex::iterator it; |
1895 | 1909 |
|
1896 | 1910 |
it = _node_index.find(source_token); |
1897 | 1911 |
if (it == _node_index.end()) { |
1898 | 1912 |
std::ostringstream msg; |
1899 | 1913 |
msg << "Item not found: " << source_token; |
1900 | 1914 |
throw FormatError(msg.str()); |
1901 | 1915 |
} |
1902 | 1916 |
Node source = it->second; |
1903 | 1917 |
|
1904 | 1918 |
it = _node_index.find(target_token); |
1905 | 1919 |
if (it == _node_index.end()) { |
1906 | 1920 |
std::ostringstream msg; |
1907 | 1921 |
msg << "Item not found: " << target_token; |
1908 | 1922 |
throw FormatError(msg.str()); |
1909 | 1923 |
} |
1910 | 1924 |
Node target = it->second; |
1911 | 1925 |
|
1912 | 1926 |
e = _graph.addEdge(source, target); |
1913 | 1927 |
if (label_index != -1) |
1914 | 1928 |
_edge_index.insert(std::make_pair(tokens[label_index], e)); |
1915 | 1929 |
} else { |
1916 | 1930 |
if (label_index == -1) |
1917 | 1931 |
throw FormatError("Label map not found"); |
1918 | 1932 |
typename std::map<std::string, Edge>::iterator it = |
1919 | 1933 |
_edge_index.find(tokens[label_index]); |
1920 | 1934 |
if (it == _edge_index.end()) { |
1921 | 1935 |
std::ostringstream msg; |
1922 | 1936 |
msg << "Edge with label not found: " << tokens[label_index]; |
1923 | 1937 |
throw FormatError(msg.str()); |
1924 | 1938 |
} |
1925 | 1939 |
e = it->second; |
1926 | 1940 |
} |
1927 | 1941 |
|
1928 | 1942 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
1929 | 1943 |
_edge_maps[i].second->set(e, tokens[map_index[i]]); |
1930 | 1944 |
} |
1931 | 1945 |
|
1932 | 1946 |
} |
1933 | 1947 |
if (readSuccess()) { |
1934 | 1948 |
line.putback(c); |
1935 | 1949 |
} |
1936 | 1950 |
} |
1937 | 1951 |
|
1938 | 1952 |
void readAttributes() { |
1939 | 1953 |
|
1940 | 1954 |
std::set<std::string> read_attr; |
1941 | 1955 |
|
1942 | 1956 |
char c; |
1943 | 1957 |
while (readLine() && line >> c && c != '@') { |
1944 | 1958 |
line.putback(c); |
1945 | 1959 |
|
1946 | 1960 |
std::string attr, token; |
1947 | 1961 |
if (!_reader_bits::readToken(line, attr)) |
1948 | 1962 |
throw FormatError("Attribute name not found"); |
1949 | 1963 |
if (!_reader_bits::readToken(line, token)) |
1950 | 1964 |
throw FormatError("Attribute value not found"); |
1951 | 1965 |
if (line >> c) |
1952 | 1966 |
throw FormatError("Extra character at the end of line"); |
1953 | 1967 |
|
1954 | 1968 |
{ |
1955 | 1969 |
std::set<std::string>::iterator it = read_attr.find(attr); |
1956 | 1970 |
if (it != read_attr.end()) { |
1957 | 1971 |
std::ostringstream msg; |
1958 | 1972 |
msg << "Multiple occurence of attribute: " << attr; |
1959 | 1973 |
throw FormatError(msg.str()); |
1960 | 1974 |
} |
1961 | 1975 |
read_attr.insert(attr); |
1962 | 1976 |
} |
1963 | 1977 |
|
1964 | 1978 |
{ |
1965 | 1979 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
1966 | 1980 |
while (it != _attributes.end() && it->first == attr) { |
1967 | 1981 |
it->second->set(token); |
1968 | 1982 |
++it; |
1969 | 1983 |
} |
1970 | 1984 |
} |
1971 | 1985 |
|
1972 | 1986 |
} |
1973 | 1987 |
if (readSuccess()) { |
1974 | 1988 |
line.putback(c); |
1975 | 1989 |
} |
1976 | 1990 |
for (typename Attributes::iterator it = _attributes.begin(); |
1977 | 1991 |
it != _attributes.end(); ++it) { |
1978 | 1992 |
if (read_attr.find(it->first) == read_attr.end()) { |
1979 | 1993 |
std::ostringstream msg; |
1980 | 1994 |
msg << "Attribute not found: " << it->first; |
1981 | 1995 |
throw FormatError(msg.str()); |
1982 | 1996 |
} |
1983 | 1997 |
} |
1984 | 1998 |
} |
1985 | 1999 |
|
1986 | 2000 |
public: |
1987 | 2001 |
|
1988 | 2002 |
/// \name Execution of the Reader |
1989 | 2003 |
/// @{ |
1990 | 2004 |
|
1991 | 2005 |
/// \brief Start the batch processing |
1992 | 2006 |
/// |
1993 | 2007 |
/// This function starts the batch processing |
1994 | 2008 |
void run() { |
1995 | 2009 |
|
1996 | 2010 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
1997 | 2011 |
|
1998 | 2012 |
bool nodes_done = _skip_nodes; |
1999 | 2013 |
bool edges_done = _skip_edges; |
2000 | 2014 |
bool attributes_done = false; |
2001 | 2015 |
|
2002 | 2016 |
line_num = 0; |
2003 | 2017 |
readLine(); |
2004 | 2018 |
skipSection(); |
2005 | 2019 |
|
2006 | 2020 |
while (readSuccess()) { |
2007 | 2021 |
try { |
2008 | 2022 |
char c; |
2009 | 2023 |
std::string section, caption; |
2010 | 2024 |
line >> c; |
2011 | 2025 |
_reader_bits::readToken(line, section); |
2012 | 2026 |
_reader_bits::readToken(line, caption); |
2013 | 2027 |
|
2014 | 2028 |
if (line >> c) |
2015 | 2029 |
throw FormatError("Extra character at the end of line"); |
2016 | 2030 |
|
2017 | 2031 |
if (section == "nodes" && !nodes_done) { |
2018 | 2032 |
if (_nodes_caption.empty() || _nodes_caption == caption) { |
2019 | 2033 |
readNodes(); |
2020 | 2034 |
nodes_done = true; |
2021 | 2035 |
} |
2022 | 2036 |
} else if ((section == "edges" || section == "arcs") && |
2023 | 2037 |
!edges_done) { |
2024 | 2038 |
if (_edges_caption.empty() || _edges_caption == caption) { |
2025 | 2039 |
readEdges(); |
2026 | 2040 |
edges_done = true; |
2027 | 2041 |
} |
2028 | 2042 |
} else if (section == "attributes" && !attributes_done) { |
2029 | 2043 |
if (_attributes_caption.empty() || _attributes_caption == caption) { |
2030 | 2044 |
readAttributes(); |
2031 | 2045 |
attributes_done = true; |
2032 | 2046 |
} |
2033 | 2047 |
} else { |
2034 | 2048 |
readLine(); |
2035 | 2049 |
skipSection(); |
2036 | 2050 |
} |
2037 | 2051 |
} catch (FormatError& error) { |
2038 | 2052 |
error.line(line_num); |
2039 | 2053 |
error.file(_filename); |
2040 | 2054 |
throw; |
2041 | 2055 |
} |
2042 | 2056 |
} |
2043 | 2057 |
|
2044 | 2058 |
if (!nodes_done) { |
2045 | 2059 |
throw FormatError("Section @nodes not found"); |
2046 | 2060 |
} |
2047 | 2061 |
|
2048 | 2062 |
if (!edges_done) { |
2049 | 2063 |
throw FormatError("Section @edges not found"); |
2050 | 2064 |
} |
2051 | 2065 |
|
2052 | 2066 |
if (!attributes_done && !_attributes.empty()) { |
2053 | 2067 |
throw FormatError("Section @attributes not found"); |
2054 | 2068 |
} |
2055 | 2069 |
|
2056 | 2070 |
} |
2057 | 2071 |
|
2058 | 2072 |
/// @} |
2059 | 2073 |
|
2060 | 2074 |
}; |
2061 | 2075 |
|
2062 | 2076 |
/// \ingroup lemon_io |
2063 | 2077 |
/// |
2064 | 2078 |
/// \brief Return a \ref GraphReader class |
2065 | 2079 |
/// |
2066 | 2080 |
/// This function just returns a \ref GraphReader class. |
2067 | 2081 |
/// |
2068 | 2082 |
/// With this function a graph can be read from an |
2069 | 2083 |
/// \ref lgf-format "LGF" file or input stream with several maps and |
2070 | 2084 |
/// attributes. For example, there is weighted matching problem on a |
2071 | 2085 |
/// graph, i.e. a graph with a \e weight map on the edges. This |
2072 | 2086 |
/// graph can be read with the following code: |
2073 | 2087 |
/// |
2074 | 2088 |
///\code |
2075 | 2089 |
///ListGraph graph; |
2076 | 2090 |
///ListGraph::EdgeMap<int> weight(graph); |
2077 | 2091 |
///graphReader(graph, std::cin). |
2078 | 2092 |
/// edgeMap("weight", weight). |
2079 | 2093 |
/// run(); |
2080 | 2094 |
///\endcode |
2081 | 2095 |
/// |
2082 | 2096 |
/// For a complete documentation, please see the \ref GraphReader |
2083 | 2097 |
/// class documentation. |
2084 | 2098 |
/// \warning Don't forget to put the \ref GraphReader::run() "run()" |
2085 | 2099 |
/// to the end of the parameter list. |
2086 | 2100 |
/// \relates GraphReader |
2087 | 2101 |
/// \sa graphReader(TGR& graph, const std::string& fn) |
2088 | 2102 |
/// \sa graphReader(TGR& graph, const char* fn) |
2089 | 2103 |
template <typename TGR> |
2090 | 2104 |
GraphReader<TGR> graphReader(TGR& graph, std::istream& is) { |
2091 | 2105 |
GraphReader<TGR> tmp(graph, is); |
2092 | 2106 |
return tmp; |
2093 | 2107 |
} |
2094 | 2108 |
|
2095 | 2109 |
/// \brief Return a \ref GraphReader class |
2096 | 2110 |
/// |
2097 | 2111 |
/// This function just returns a \ref GraphReader class. |
2098 | 2112 |
/// \relates GraphReader |
2099 | 2113 |
/// \sa graphReader(TGR& graph, std::istream& is) |
2100 | 2114 |
template <typename TGR> |
2101 | 2115 |
GraphReader<TGR> graphReader(TGR& graph, const std::string& fn) { |
2102 | 2116 |
GraphReader<TGR> tmp(graph, fn); |
2103 | 2117 |
return tmp; |
2104 | 2118 |
} |
2105 | 2119 |
|
2106 | 2120 |
/// \brief Return a \ref GraphReader class |
2107 | 2121 |
/// |
2108 | 2122 |
/// This function just returns a \ref GraphReader class. |
2109 | 2123 |
/// \relates GraphReader |
2110 | 2124 |
/// \sa graphReader(TGR& graph, std::istream& is) |
2111 | 2125 |
template <typename TGR> |
2112 | 2126 |
GraphReader<TGR> graphReader(TGR& graph, const char* fn) { |
2113 | 2127 |
GraphReader<TGR> tmp(graph, fn); |
2114 | 2128 |
return tmp; |
2115 | 2129 |
} |
2116 | 2130 |
|
2117 | 2131 |
class SectionReader; |
2118 | 2132 |
|
2119 | 2133 |
SectionReader sectionReader(std::istream& is); |
2120 | 2134 |
SectionReader sectionReader(const std::string& fn); |
2121 | 2135 |
SectionReader sectionReader(const char* fn); |
2122 | 2136 |
|
2123 | 2137 |
/// \ingroup lemon_io |
2124 | 2138 |
/// |
2125 | 2139 |
/// \brief Section reader class |
2126 | 2140 |
/// |
2127 | 2141 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
2128 | 2142 |
/// which contain any data in arbitrary format. Such sections can be |
2129 | 2143 |
/// read with this class. A reading rule can be added to the class |
2130 | 2144 |
/// with two different functions. With the \c sectionLines() function a |
2131 | 2145 |
/// functor can process the section line-by-line, while with the \c |
2132 | 2146 |
/// sectionStream() member the section can be read from an input |
2133 | 2147 |
/// stream. |
2134 | 2148 |
class SectionReader { |
2135 | 2149 |
private: |
2136 | 2150 |
|
2137 | 2151 |
std::istream* _is; |
2138 | 2152 |
bool local_is; |
2139 | 2153 |
std::string _filename; |
2140 | 2154 |
|
2141 | 2155 |
typedef std::map<std::string, _reader_bits::Section*> Sections; |
2142 | 2156 |
Sections _sections; |
2143 | 2157 |
|
2144 | 2158 |
int line_num; |
2145 | 2159 |
std::istringstream line; |
2146 | 2160 |
|
2147 | 2161 |
public: |
2148 | 2162 |
|
2149 | 2163 |
/// \brief Constructor |
2150 | 2164 |
/// |
2151 | 2165 |
/// Construct a section reader, which reads from the given input |
2152 | 2166 |
/// stream. |
2153 | 2167 |
SectionReader(std::istream& is) |
2154 | 2168 |
: _is(&is), local_is(false) {} |
2155 | 2169 |
|
2156 | 2170 |
/// \brief Constructor |
2157 | 2171 |
/// |
2158 | 2172 |
/// Construct a section reader, which reads from the given file. |
2159 | 2173 |
SectionReader(const std::string& fn) |
2160 | 2174 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
2161 | 2175 |
_filename(fn) { |
2162 | 2176 |
if (!(*_is)) { |
2163 | 2177 |
delete _is; |
2164 | 2178 |
throw IoError("Cannot open file", fn); |
2165 | 2179 |
} |
2166 | 2180 |
} |
2167 | 2181 |
|
2168 | 2182 |
/// \brief Constructor |
2169 | 2183 |
/// |
2170 | 2184 |
/// Construct a section reader, which reads from the given file. |
2171 | 2185 |
SectionReader(const char* fn) |
2172 | 2186 |
: _is(new std::ifstream(fn)), local_is(true), |
2173 | 2187 |
_filename(fn) { |
2174 | 2188 |
if (!(*_is)) { |
2175 | 2189 |
delete _is; |
2176 | 2190 |
throw IoError("Cannot open file", fn); |
2177 | 2191 |
} |
2178 | 2192 |
} |
2179 | 2193 |
|
2180 | 2194 |
/// \brief Destructor |
2181 | 2195 |
~SectionReader() { |
2182 | 2196 |
for (Sections::iterator it = _sections.begin(); |
2183 | 2197 |
it != _sections.end(); ++it) { |
2184 | 2198 |
delete it->second; |
2185 | 2199 |
} |
2186 | 2200 |
|
2187 | 2201 |
if (local_is) { |
2188 | 2202 |
delete _is; |
2189 | 2203 |
} |
2190 | 2204 |
|
2191 | 2205 |
} |
2192 | 2206 |
|
2193 | 2207 |
private: |
2194 | 2208 |
|
2195 | 2209 |
friend SectionReader sectionReader(std::istream& is); |
2196 | 2210 |
friend SectionReader sectionReader(const std::string& fn); |
2197 | 2211 |
friend SectionReader sectionReader(const char* fn); |
2198 | 2212 |
|
2199 | 2213 |
SectionReader(SectionReader& other) |
2200 | 2214 |
: _is(other._is), local_is(other.local_is) { |
2201 | 2215 |
|
2202 | 2216 |
other._is = 0; |
2203 | 2217 |
other.local_is = false; |
2204 | 2218 |
|
2205 | 2219 |
_sections.swap(other._sections); |
2206 | 2220 |
} |
2207 | 2221 |
|
2208 | 2222 |
SectionReader& operator=(const SectionReader&); |
2209 | 2223 |
|
2210 | 2224 |
public: |
2211 | 2225 |
|
2212 | 2226 |
/// \name Section Readers |
2213 | 2227 |
/// @{ |
2214 | 2228 |
|
2215 | 2229 |
/// \brief Add a section processor with line oriented reading |
2216 | 2230 |
/// |
2217 | 2231 |
/// The first parameter is the type descriptor of the section, the |
2218 | 2232 |
/// second is a functor, which takes just one \c std::string |
2219 | 2233 |
/// parameter. At the reading process, each line of the section |
2220 | 2234 |
/// will be given to the functor object. However, the empty lines |
2221 | 2235 |
/// and the comment lines are filtered out, and the leading |
2222 | 2236 |
/// whitespaces are trimmed from each processed string. |
2223 | 2237 |
/// |
2224 | 2238 |
/// For example let's see a section, which contain several |
2225 | 2239 |
/// integers, which should be inserted into a vector. |
2226 | 2240 |
///\code |
2227 | 2241 |
/// @numbers |
2228 | 2242 |
/// 12 45 23 |
2229 | 2243 |
/// 4 |
2230 | 2244 |
/// 23 6 |
2231 | 2245 |
///\endcode |
2232 | 2246 |
/// |
2233 | 2247 |
/// The functor is implemented as a struct: |
2234 | 2248 |
///\code |
2235 | 2249 |
/// struct NumberSection { |
2236 | 2250 |
/// std::vector<int>& _data; |
2237 | 2251 |
/// NumberSection(std::vector<int>& data) : _data(data) {} |
2238 | 2252 |
/// void operator()(const std::string& line) { |
2239 | 2253 |
/// std::istringstream ls(line); |
2240 | 2254 |
/// int value; |
2241 | 2255 |
/// while (ls >> value) _data.push_back(value); |
2242 | 2256 |
/// } |
2243 | 2257 |
/// }; |
2244 | 2258 |
/// |
2245 | 2259 |
/// // ... |
2246 | 2260 |
/// |
2247 | 2261 |
/// reader.sectionLines("numbers", NumberSection(vec)); |
2248 | 2262 |
///\endcode |
2249 | 2263 |
template <typename Functor> |
2250 | 2264 |
SectionReader& sectionLines(const std::string& type, Functor functor) { |
2251 | 2265 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
2252 | 2266 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
2253 | 2267 |
"Multiple reading of section."); |
2254 | 2268 |
_sections.insert(std::make_pair(type, |
2255 | 2269 |
new _reader_bits::LineSection<Functor>(functor))); |
2256 | 2270 |
return *this; |
2257 | 2271 |
} |
2258 | 2272 |
|
2259 | 2273 |
|
2260 | 2274 |
/// \brief Add a section processor with stream oriented reading |
2261 | 2275 |
/// |
2262 | 2276 |
/// The first parameter is the type of the section, the second is |
2263 | 2277 |
/// a functor, which takes an \c std::istream& and an \c int& |
2264 | 2278 |
/// parameter, the latter regard to the line number of stream. The |
2265 | 2279 |
/// functor can read the input while the section go on, and the |
2266 | 2280 |
/// line number should be modified accordingly. |
2267 | 2281 |
template <typename Functor> |
2268 | 2282 |
SectionReader& sectionStream(const std::string& type, Functor functor) { |
2269 | 2283 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
2270 | 2284 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
2271 | 2285 |
"Multiple reading of section."); |
2272 | 2286 |
_sections.insert(std::make_pair(type, |
2273 | 2287 |
new _reader_bits::StreamSection<Functor>(functor))); |
2274 | 2288 |
return *this; |
2275 | 2289 |
} |
2276 | 2290 |
|
2277 | 2291 |
/// @} |
2278 | 2292 |
|
2279 | 2293 |
private: |
2280 | 2294 |
|
2281 | 2295 |
bool readLine() { |
2282 | 2296 |
std::string str; |
2283 | 2297 |
while(++line_num, std::getline(*_is, str)) { |
2284 | 2298 |
line.clear(); line.str(str); |
2285 | 2299 |
char c; |
2286 | 2300 |
if (line >> std::ws >> c && c != '#') { |
2287 | 2301 |
line.putback(c); |
2288 | 2302 |
return true; |
2289 | 2303 |
} |
2290 | 2304 |
} |
2291 | 2305 |
return false; |
2292 | 2306 |
} |
2293 | 2307 |
|
2294 | 2308 |
bool readSuccess() { |
2295 | 2309 |
return static_cast<bool>(*_is); |
2296 | 2310 |
} |
2297 | 2311 |
|
2298 | 2312 |
void skipSection() { |
2299 | 2313 |
char c; |
2300 | 2314 |
while (readSuccess() && line >> c && c != '@') { |
2301 | 2315 |
readLine(); |
2302 | 2316 |
} |
2303 | 2317 |
if (readSuccess()) { |
2304 | 2318 |
line.putback(c); |
2305 | 2319 |
} |
2306 | 2320 |
} |
2307 | 2321 |
|
2308 | 2322 |
public: |
2309 | 2323 |
|
2310 | 2324 |
|
2311 | 2325 |
/// \name Execution of the Reader |
2312 | 2326 |
/// @{ |
2313 | 2327 |
|
2314 | 2328 |
/// \brief Start the batch processing |
2315 | 2329 |
/// |
2316 | 2330 |
/// This function starts the batch processing. |
2317 | 2331 |
void run() { |
2318 | 2332 |
|
2319 | 2333 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
2320 | 2334 |
|
2321 | 2335 |
std::set<std::string> extra_sections; |
2322 | 2336 |
|
2323 | 2337 |
line_num = 0; |
2324 | 2338 |
readLine(); |
2325 | 2339 |
skipSection(); |
2326 | 2340 |
|
2327 | 2341 |
while (readSuccess()) { |
2328 | 2342 |
try { |
2329 | 2343 |
char c; |
2330 | 2344 |
std::string section, caption; |
2331 | 2345 |
line >> c; |
2332 | 2346 |
_reader_bits::readToken(line, section); |
2333 | 2347 |
_reader_bits::readToken(line, caption); |
2334 | 2348 |
|
2335 | 2349 |
if (line >> c) |
2336 | 2350 |
throw FormatError("Extra character at the end of line"); |
2337 | 2351 |
|
2338 | 2352 |
if (extra_sections.find(section) != extra_sections.end()) { |
2339 | 2353 |
std::ostringstream msg; |
2340 | 2354 |
msg << "Multiple occurence of section: " << section; |
2341 | 2355 |
throw FormatError(msg.str()); |
2342 | 2356 |
} |
2343 | 2357 |
Sections::iterator it = _sections.find(section); |
2344 | 2358 |
if (it != _sections.end()) { |
2345 | 2359 |
extra_sections.insert(section); |
2346 | 2360 |
it->second->process(*_is, line_num); |
2347 | 2361 |
} |
2348 | 2362 |
readLine(); |
2349 | 2363 |
skipSection(); |
2350 | 2364 |
} catch (FormatError& error) { |
2351 | 2365 |
error.line(line_num); |
2352 | 2366 |
error.file(_filename); |
2353 | 2367 |
throw; |
2354 | 2368 |
} |
2355 | 2369 |
} |
2356 | 2370 |
for (Sections::iterator it = _sections.begin(); |
2357 | 2371 |
it != _sections.end(); ++it) { |
2358 | 2372 |
if (extra_sections.find(it->first) == extra_sections.end()) { |
2359 | 2373 |
std::ostringstream os; |
2360 | 2374 |
os << "Cannot find section: " << it->first; |
2361 | 2375 |
throw FormatError(os.str()); |
2362 | 2376 |
} |
2363 | 2377 |
} |
2364 | 2378 |
} |
2365 | 2379 |
|
2366 | 2380 |
/// @} |
2367 | 2381 |
|
2368 | 2382 |
}; |
2369 | 2383 |
|
2370 | 2384 |
/// \ingroup lemon_io |
2371 | 2385 |
/// |
2372 | 2386 |
/// \brief Return a \ref SectionReader class |
2373 | 2387 |
/// |
2374 | 2388 |
/// This function just returns a \ref SectionReader class. |
2375 | 2389 |
/// |
2376 | 2390 |
/// Please see SectionReader documentation about the custom section |
2377 | 2391 |
/// input. |
2378 | 2392 |
/// |
2379 | 2393 |
/// \relates SectionReader |
2380 | 2394 |
/// \sa sectionReader(const std::string& fn) |
2381 | 2395 |
/// \sa sectionReader(const char *fn) |
2382 | 2396 |
inline SectionReader sectionReader(std::istream& is) { |
2383 | 2397 |
SectionReader tmp(is); |
2384 | 2398 |
return tmp; |
2385 | 2399 |
} |
2386 | 2400 |
|
2387 | 2401 |
/// \brief Return a \ref SectionReader class |
2388 | 2402 |
/// |
2389 | 2403 |
/// This function just returns a \ref SectionReader class. |
2390 | 2404 |
/// \relates SectionReader |
2391 | 2405 |
/// \sa sectionReader(std::istream& is) |
2392 | 2406 |
inline SectionReader sectionReader(const std::string& fn) { |
2393 | 2407 |
SectionReader tmp(fn); |
2394 | 2408 |
return tmp; |
2395 | 2409 |
} |
2396 | 2410 |
|
2397 | 2411 |
/// \brief Return a \ref SectionReader class |
2398 | 2412 |
/// |
2399 | 2413 |
/// This function just returns a \ref SectionReader class. |
2400 | 2414 |
/// \relates SectionReader |
2401 | 2415 |
/// \sa sectionReader(std::istream& is) |
2402 | 2416 |
inline SectionReader sectionReader(const char* fn) { |
2403 | 2417 |
SectionReader tmp(fn); |
2404 | 2418 |
return tmp; |
2405 | 2419 |
} |
2406 | 2420 |
|
2407 | 2421 |
/// \ingroup lemon_io |
2408 | 2422 |
/// |
2409 | 2423 |
/// \brief Reader for the contents of the \ref lgf-format "LGF" file |
2410 | 2424 |
/// |
2411 | 2425 |
/// This class can be used to read the sections, the map names and |
2412 | 2426 |
/// the attributes from a file. Usually, the LEMON programs know |
2413 | 2427 |
/// that, which type of graph, which maps and which attributes |
2414 | 2428 |
/// should be read from a file, but in general tools (like glemon) |
2415 | 2429 |
/// the contents of an LGF file should be guessed somehow. This class |
2416 | 2430 |
/// reads the graph and stores the appropriate information for |
2417 | 2431 |
/// reading the graph. |
2418 | 2432 |
/// |
2419 | 2433 |
///\code |
2420 | 2434 |
/// LgfContents contents("graph.lgf"); |
2421 | 2435 |
/// contents.run(); |
2422 | 2436 |
/// |
2423 | 2437 |
/// // Does it contain any node section and arc section? |
2424 | 2438 |
/// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) { |
2425 | 2439 |
/// std::cerr << "Failure, cannot find graph." << std::endl; |
2426 | 2440 |
/// return -1; |
2427 | 2441 |
/// } |
2428 | 2442 |
/// std::cout << "The name of the default node section: " |
2429 | 2443 |
/// << contents.nodeSection(0) << std::endl; |
2430 | 2444 |
/// std::cout << "The number of the arc maps: " |
2431 | 2445 |
/// << contents.arcMaps(0).size() << std::endl; |
2432 | 2446 |
/// std::cout << "The name of second arc map: " |
2433 | 2447 |
/// << contents.arcMaps(0)[1] << std::endl; |
2434 | 2448 |
///\endcode |
2435 | 2449 |
class LgfContents { |
2436 | 2450 |
private: |
2437 | 2451 |
|
2438 | 2452 |
std::istream* _is; |
2439 | 2453 |
bool local_is; |
2440 | 2454 |
|
2441 | 2455 |
std::vector<std::string> _node_sections; |
2442 | 2456 |
std::vector<std::string> _edge_sections; |
2443 | 2457 |
std::vector<std::string> _attribute_sections; |
2444 | 2458 |
std::vector<std::string> _extra_sections; |
2445 | 2459 |
|
2446 | 2460 |
std::vector<bool> _arc_sections; |
2447 | 2461 |
|
2448 | 2462 |
std::vector<std::vector<std::string> > _node_maps; |
2449 | 2463 |
std::vector<std::vector<std::string> > _edge_maps; |
2450 | 2464 |
|
2451 | 2465 |
std::vector<std::vector<std::string> > _attributes; |
2452 | 2466 |
|
2453 | 2467 |
|
2454 | 2468 |
int line_num; |
2455 | 2469 |
std::istringstream line; |
2456 | 2470 |
|
2457 | 2471 |
public: |
2458 | 2472 |
|
2459 | 2473 |
/// \brief Constructor |
2460 | 2474 |
/// |
2461 | 2475 |
/// Construct an \e LGF contents reader, which reads from the given |
2462 | 2476 |
/// input stream. |
2463 | 2477 |
LgfContents(std::istream& is) |
2464 | 2478 |
: _is(&is), local_is(false) {} |
2465 | 2479 |
|
2466 | 2480 |
/// \brief Constructor |
2467 | 2481 |
/// |
2468 | 2482 |
/// Construct an \e LGF contents reader, which reads from the given |
2469 | 2483 |
/// file. |
2470 | 2484 |
LgfContents(const std::string& fn) |
2471 | 2485 |
: _is(new std::ifstream(fn.c_str())), local_is(true) { |
2472 | 2486 |
if (!(*_is)) { |
2473 | 2487 |
delete _is; |
2474 | 2488 |
throw IoError("Cannot open file", fn); |
2475 | 2489 |
} |
2476 | 2490 |
} |
2477 | 2491 |
|
2478 | 2492 |
/// \brief Constructor |
2479 | 2493 |
/// |
2480 | 2494 |
/// Construct an \e LGF contents reader, which reads from the given |
2481 | 2495 |
/// file. |
2482 | 2496 |
LgfContents(const char* fn) |
2483 | 2497 |
: _is(new std::ifstream(fn)), local_is(true) { |
2484 | 2498 |
if (!(*_is)) { |
2485 | 2499 |
delete _is; |
2486 | 2500 |
throw IoError("Cannot open file", fn); |
2487 | 2501 |
} |
2488 | 2502 |
} |
2489 | 2503 |
|
2490 | 2504 |
/// \brief Destructor |
2491 | 2505 |
~LgfContents() { |
2492 | 2506 |
if (local_is) delete _is; |
2493 | 2507 |
} |
2494 | 2508 |
|
2495 | 2509 |
private: |
2496 | 2510 |
|
2497 | 2511 |
LgfContents(const LgfContents&); |
2498 | 2512 |
LgfContents& operator=(const LgfContents&); |
2499 | 2513 |
|
2500 | 2514 |
public: |
2501 | 2515 |
|
2502 | 2516 |
|
2503 | 2517 |
/// \name Node Sections |
2504 | 2518 |
/// @{ |
2505 | 2519 |
|
2506 | 2520 |
/// \brief Gives back the number of node sections in the file. |
2507 | 2521 |
/// |
2508 | 2522 |
/// Gives back the number of node sections in the file. |
2509 | 2523 |
int nodeSectionNum() const { |
2510 | 2524 |
return _node_sections.size(); |
2511 | 2525 |
} |
2512 | 2526 |
|
2513 | 2527 |
/// \brief Returns the node section name at the given position. |
2514 | 2528 |
/// |
2515 | 2529 |
/// Returns the node section name at the given position. |
2516 | 2530 |
const std::string& nodeSection(int i) const { |
2517 | 2531 |
return _node_sections[i]; |
2518 | 2532 |
} |
2519 | 2533 |
|
2520 | 2534 |
/// \brief Gives back the node maps for the given section. |
2521 | 2535 |
/// |
2522 | 2536 |
/// Gives back the node maps for the given section. |
2523 | 2537 |
const std::vector<std::string>& nodeMapNames(int i) const { |
2524 | 2538 |
return _node_maps[i]; |
2525 | 2539 |
} |
2526 | 2540 |
|
2527 | 2541 |
/// @} |
2528 | 2542 |
|
2529 | 2543 |
/// \name Arc/Edge Sections |
2530 | 2544 |
/// @{ |
2531 | 2545 |
|
2532 | 2546 |
/// \brief Gives back the number of arc/edge sections in the file. |
2533 | 2547 |
/// |
2534 | 2548 |
/// Gives back the number of arc/edge sections in the file. |
2535 | 2549 |
/// \note It is synonym of \c edgeSectionNum(). |
2536 | 2550 |
int arcSectionNum() const { |
2537 | 2551 |
return _edge_sections.size(); |
2538 | 2552 |
} |
2539 | 2553 |
|
2540 | 2554 |
/// \brief Returns the arc/edge section name at the given position. |
2541 | 2555 |
/// |
2542 | 2556 |
/// Returns the arc/edge section name at the given position. |
2543 | 2557 |
/// \note It is synonym of \c edgeSection(). |
2544 | 2558 |
const std::string& arcSection(int i) const { |
2545 | 2559 |
return _edge_sections[i]; |
2546 | 2560 |
} |
2547 | 2561 |
|
2548 | 2562 |
/// \brief Gives back the arc/edge maps for the given section. |
2549 | 2563 |
/// |
2550 | 2564 |
/// Gives back the arc/edge maps for the given section. |
2551 | 2565 |
/// \note It is synonym of \c edgeMapNames(). |
2552 | 2566 |
const std::vector<std::string>& arcMapNames(int i) const { |
2553 | 2567 |
return _edge_maps[i]; |
2554 | 2568 |
} |
2555 | 2569 |
|
2556 | 2570 |
/// @} |
2557 | 2571 |
|
2558 | 2572 |
/// \name Synonyms |
2559 | 2573 |
/// @{ |
2560 | 2574 |
|
2561 | 2575 |
/// \brief Gives back the number of arc/edge sections in the file. |
2562 | 2576 |
/// |
2563 | 2577 |
/// Gives back the number of arc/edge sections in the file. |
2564 | 2578 |
/// \note It is synonym of \c arcSectionNum(). |
2565 | 2579 |
int edgeSectionNum() const { |
2566 | 2580 |
return _edge_sections.size(); |
2567 | 2581 |
} |
2568 | 2582 |
|
2569 | 2583 |
/// \brief Returns the section name at the given position. |
2570 | 2584 |
/// |
2571 | 2585 |
/// Returns the section name at the given position. |
2572 | 2586 |
/// \note It is synonym of \c arcSection(). |
2573 | 2587 |
const std::string& edgeSection(int i) const { |
2574 | 2588 |
return _edge_sections[i]; |
2575 | 2589 |
} |
2576 | 2590 |
|
2577 | 2591 |
/// \brief Gives back the edge maps for the given section. |
2578 | 2592 |
/// |
2579 | 2593 |
/// Gives back the edge maps for the given section. |
2580 | 2594 |
/// \note It is synonym of \c arcMapNames(). |
2581 | 2595 |
const std::vector<std::string>& edgeMapNames(int i) const { |
2582 | 2596 |
return _edge_maps[i]; |
2583 | 2597 |
} |
2584 | 2598 |
|
2585 | 2599 |
/// @} |
2586 | 2600 |
|
2587 | 2601 |
/// \name Attribute Sections |
2588 | 2602 |
/// @{ |
2589 | 2603 |
|
2590 | 2604 |
/// \brief Gives back the number of attribute sections in the file. |
2591 | 2605 |
/// |
2592 | 2606 |
/// Gives back the number of attribute sections in the file. |
2593 | 2607 |
int attributeSectionNum() const { |
2594 | 2608 |
return _attribute_sections.size(); |
2595 | 2609 |
} |
2596 | 2610 |
|
2597 | 2611 |
/// \brief Returns the attribute section name at the given position. |
2598 | 2612 |
/// |
2599 | 2613 |
/// Returns the attribute section name at the given position. |
2600 | 2614 |
const std::string& attributeSectionNames(int i) const { |
2601 | 2615 |
return _attribute_sections[i]; |
2602 | 2616 |
} |
2603 | 2617 |
|
2604 | 2618 |
/// \brief Gives back the attributes for the given section. |
1 | 1 |
INCLUDE_DIRECTORIES( |
2 | 2 |
${PROJECT_SOURCE_DIR} |
3 | 3 |
${PROJECT_BINARY_DIR} |
4 | 4 |
) |
5 | 5 |
|
6 | 6 |
LINK_DIRECTORIES( |
7 | 7 |
${PROJECT_BINARY_DIR}/lemon |
8 | 8 |
) |
9 | 9 |
|
10 | 10 |
SET(TEST_WITH_VALGRIND "NO" CACHE STRING |
11 | 11 |
"Run the test with valgrind (YES/NO).") |
12 | 12 |
SET(VALGRIND_FLAGS "" CACHE STRING "Valgrind flags used by the tests.") |
13 | 13 |
|
14 | 14 |
SET(TESTS |
15 | 15 |
adaptors_test |
16 | 16 |
bfs_test |
17 | 17 |
circulation_test |
18 | 18 |
connectivity_test |
19 | 19 |
counter_test |
20 | 20 |
dfs_test |
21 | 21 |
digraph_test |
22 | 22 |
dijkstra_test |
23 | 23 |
dim_test |
24 | 24 |
edge_set_test |
25 | 25 |
error_test |
26 | 26 |
euler_test |
27 | 27 |
gomory_hu_test |
28 | 28 |
graph_copy_test |
29 | 29 |
graph_test |
30 | 30 |
graph_utils_test |
31 | 31 |
hao_orlin_test |
32 | 32 |
heap_test |
33 | 33 |
kruskal_test |
34 |
lgf_test |
|
34 | 35 |
maps_test |
35 | 36 |
matching_test |
36 | 37 |
min_cost_arborescence_test |
37 | 38 |
min_cost_flow_test |
38 | 39 |
path_test |
39 | 40 |
preflow_test |
40 | 41 |
radix_sort_test |
41 | 42 |
random_test |
42 | 43 |
suurballe_test |
43 | 44 |
time_measure_test |
44 | 45 |
unionfind_test |
45 | 46 |
) |
46 | 47 |
|
47 | 48 |
IF(LEMON_HAVE_LP) |
48 | 49 |
IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer") |
49 | 50 |
ADD_EXECUTABLE(lp_test lp_test.cc) |
50 | 51 |
ELSE() |
51 | 52 |
ADD_EXECUTABLE(lp_test EXCLUDE_FROM_ALL lp_test.cc) |
52 | 53 |
ENDIF() |
53 | 54 |
|
54 | 55 |
SET(LP_TEST_LIBS lemon) |
55 | 56 |
|
56 | 57 |
IF(LEMON_HAVE_GLPK) |
57 | 58 |
SET(LP_TEST_LIBS ${LP_TEST_LIBS} ${GLPK_LIBRARIES}) |
58 | 59 |
ENDIF() |
59 | 60 |
IF(LEMON_HAVE_CPLEX) |
60 | 61 |
SET(LP_TEST_LIBS ${LP_TEST_LIBS} ${CPLEX_LIBRARIES}) |
61 | 62 |
ENDIF() |
62 | 63 |
IF(LEMON_HAVE_CLP) |
63 | 64 |
SET(LP_TEST_LIBS ${LP_TEST_LIBS} ${COIN_CLP_LIBRARIES}) |
64 | 65 |
ENDIF() |
65 | 66 |
|
66 | 67 |
TARGET_LINK_LIBRARIES(lp_test ${LP_TEST_LIBS}) |
67 | 68 |
ADD_TEST(lp_test lp_test) |
68 | 69 |
ADD_DEPENDENCIES(check lp_test) |
69 | 70 |
|
70 | 71 |
IF(WIN32 AND LEMON_HAVE_GLPK) |
71 | 72 |
GET_TARGET_PROPERTY(TARGET_LOC lp_test LOCATION) |
72 | 73 |
GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH) |
73 | 74 |
ADD_CUSTOM_COMMAND(TARGET lp_test POST_BUILD |
74 | 75 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/glpk.dll ${TARGET_PATH} |
75 | 76 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/libltdl3.dll ${TARGET_PATH} |
76 | 77 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/zlib1.dll ${TARGET_PATH} |
77 | 78 |
) |
78 | 79 |
ENDIF() |
79 | 80 |
|
80 | 81 |
IF(WIN32 AND LEMON_HAVE_CPLEX) |
81 | 82 |
GET_TARGET_PROPERTY(TARGET_LOC lp_test LOCATION) |
82 | 83 |
GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH) |
83 | 84 |
ADD_CUSTOM_COMMAND(TARGET lp_test POST_BUILD |
84 | 85 |
COMMAND ${CMAKE_COMMAND} -E copy ${CPLEX_BIN_DIR}/cplex91.dll ${TARGET_PATH} |
85 | 86 |
) |
86 | 87 |
ENDIF() |
87 | 88 |
ENDIF() |
88 | 89 |
|
89 | 90 |
IF(LEMON_HAVE_MIP) |
90 | 91 |
IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer") |
91 | 92 |
ADD_EXECUTABLE(mip_test mip_test.cc) |
92 | 93 |
ELSE() |
93 | 94 |
ADD_EXECUTABLE(mip_test EXCLUDE_FROM_ALL mip_test.cc) |
94 | 95 |
ENDIF() |
95 | 96 |
|
96 | 97 |
SET(MIP_TEST_LIBS lemon) |
97 | 98 |
|
98 | 99 |
IF(LEMON_HAVE_GLPK) |
99 | 100 |
SET(MIP_TEST_LIBS ${MIP_TEST_LIBS} ${GLPK_LIBRARIES}) |
100 | 101 |
ENDIF() |
101 | 102 |
IF(LEMON_HAVE_CPLEX) |
102 | 103 |
SET(MIP_TEST_LIBS ${MIP_TEST_LIBS} ${CPLEX_LIBRARIES}) |
103 | 104 |
ENDIF() |
104 | 105 |
IF(LEMON_HAVE_CBC) |
105 | 106 |
SET(MIP_TEST_LIBS ${MIP_TEST_LIBS} ${COIN_CBC_LIBRARIES}) |
106 | 107 |
ENDIF() |
107 | 108 |
|
108 | 109 |
TARGET_LINK_LIBRARIES(mip_test ${MIP_TEST_LIBS}) |
109 | 110 |
ADD_TEST(mip_test mip_test) |
110 | 111 |
ADD_DEPENDENCIES(check mip_test) |
111 | 112 |
|
112 | 113 |
IF(WIN32 AND LEMON_HAVE_GLPK) |
113 | 114 |
GET_TARGET_PROPERTY(TARGET_LOC mip_test LOCATION) |
114 | 115 |
GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH) |
115 | 116 |
ADD_CUSTOM_COMMAND(TARGET mip_test POST_BUILD |
116 | 117 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/glpk.dll ${TARGET_PATH} |
117 | 118 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/libltdl3.dll ${TARGET_PATH} |
118 | 119 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/zlib1.dll ${TARGET_PATH} |
119 | 120 |
) |
120 | 121 |
ENDIF() |
121 | 122 |
|
122 | 123 |
IF(WIN32 AND LEMON_HAVE_CPLEX) |
123 | 124 |
GET_TARGET_PROPERTY(TARGET_LOC mip_test LOCATION) |
124 | 125 |
GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH) |
125 | 126 |
ADD_CUSTOM_COMMAND(TARGET mip_test POST_BUILD |
126 | 127 |
COMMAND ${CMAKE_COMMAND} -E copy ${CPLEX_BIN_DIR}/cplex91.dll ${TARGET_PATH} |
127 | 128 |
) |
128 | 129 |
ENDIF() |
129 | 130 |
ENDIF() |
130 | 131 |
|
131 | 132 |
FOREACH(TEST_NAME ${TESTS}) |
132 | 133 |
IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer") |
133 | 134 |
ADD_EXECUTABLE(${TEST_NAME} ${TEST_NAME}.cc) |
134 | 135 |
ELSE() |
135 | 136 |
ADD_EXECUTABLE(${TEST_NAME} EXCLUDE_FROM_ALL ${TEST_NAME}.cc) |
136 | 137 |
ENDIF() |
137 | 138 |
TARGET_LINK_LIBRARIES(${TEST_NAME} lemon) |
138 | 139 |
IF(TEST_WITH_VALGRIND) |
139 | 140 |
ADD_TEST(${TEST_NAME} |
140 | 141 |
valgrind --error-exitcode=1 ${VALGRIND_FLAGS} |
141 | 142 |
${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME} ) |
142 | 143 |
ELSE() |
143 | 144 |
ADD_TEST(${TEST_NAME} ${TEST_NAME}) |
144 | 145 |
ENDIF() |
145 | 146 |
ADD_DEPENDENCIES(check ${TEST_NAME}) |
146 | 147 |
ENDFOREACH() |
1 | 1 |
EXTRA_DIST += \ |
2 | 2 |
test/CMakeLists.txt |
3 | 3 |
|
4 | 4 |
noinst_HEADERS += \ |
5 | 5 |
test/graph_test.h \ |
6 | 6 |
test/test_tools.h |
7 | 7 |
|
8 | 8 |
check_PROGRAMS += \ |
9 | 9 |
test/adaptors_test \ |
10 | 10 |
test/bfs_test \ |
11 | 11 |
test/circulation_test \ |
12 | 12 |
test/connectivity_test \ |
13 | 13 |
test/counter_test \ |
14 | 14 |
test/dfs_test \ |
15 | 15 |
test/digraph_test \ |
16 | 16 |
test/dijkstra_test \ |
17 | 17 |
test/dim_test \ |
18 | 18 |
test/edge_set_test \ |
19 | 19 |
test/error_test \ |
20 | 20 |
test/euler_test \ |
21 | 21 |
test/gomory_hu_test \ |
22 | 22 |
test/graph_copy_test \ |
23 | 23 |
test/graph_test \ |
24 | 24 |
test/graph_utils_test \ |
25 | 25 |
test/hao_orlin_test \ |
26 | 26 |
test/heap_test \ |
27 | 27 |
test/kruskal_test \ |
28 |
test/lgf_test \ |
|
28 | 29 |
test/maps_test \ |
29 | 30 |
test/matching_test \ |
30 | 31 |
test/min_cost_arborescence_test \ |
31 | 32 |
test/min_cost_flow_test \ |
32 | 33 |
test/path_test \ |
33 | 34 |
test/preflow_test \ |
34 | 35 |
test/radix_sort_test \ |
35 | 36 |
test/random_test \ |
36 | 37 |
test/suurballe_test \ |
37 | 38 |
test/test_tools_fail \ |
38 | 39 |
test/test_tools_pass \ |
39 | 40 |
test/time_measure_test \ |
40 | 41 |
test/unionfind_test |
41 | 42 |
|
42 | 43 |
test_test_tools_pass_DEPENDENCIES = demo |
43 | 44 |
|
44 | 45 |
if HAVE_LP |
45 | 46 |
check_PROGRAMS += test/lp_test |
46 | 47 |
endif HAVE_LP |
47 | 48 |
if HAVE_MIP |
48 | 49 |
check_PROGRAMS += test/mip_test |
49 | 50 |
endif HAVE_MIP |
50 | 51 |
|
51 | 52 |
TESTS += $(check_PROGRAMS) |
52 | 53 |
XFAIL_TESTS += test/test_tools_fail$(EXEEXT) |
53 | 54 |
|
54 | 55 |
test_adaptors_test_SOURCES = test/adaptors_test.cc |
55 | 56 |
test_bfs_test_SOURCES = test/bfs_test.cc |
56 | 57 |
test_circulation_test_SOURCES = test/circulation_test.cc |
57 | 58 |
test_counter_test_SOURCES = test/counter_test.cc |
58 | 59 |
test_connectivity_test_SOURCES = test/connectivity_test.cc |
59 | 60 |
test_dfs_test_SOURCES = test/dfs_test.cc |
60 | 61 |
test_digraph_test_SOURCES = test/digraph_test.cc |
61 | 62 |
test_dijkstra_test_SOURCES = test/dijkstra_test.cc |
62 | 63 |
test_dim_test_SOURCES = test/dim_test.cc |
63 | 64 |
test_edge_set_test_SOURCES = test/edge_set_test.cc |
64 | 65 |
test_error_test_SOURCES = test/error_test.cc |
65 | 66 |
test_euler_test_SOURCES = test/euler_test.cc |
66 | 67 |
test_gomory_hu_test_SOURCES = test/gomory_hu_test.cc |
67 | 68 |
test_graph_copy_test_SOURCES = test/graph_copy_test.cc |
68 | 69 |
test_graph_test_SOURCES = test/graph_test.cc |
69 | 70 |
test_graph_utils_test_SOURCES = test/graph_utils_test.cc |
70 | 71 |
test_heap_test_SOURCES = test/heap_test.cc |
71 | 72 |
test_kruskal_test_SOURCES = test/kruskal_test.cc |
72 | 73 |
test_hao_orlin_test_SOURCES = test/hao_orlin_test.cc |
74 |
test_lgf_test_SOURCES = test/lgf_test.cc |
|
73 | 75 |
test_lp_test_SOURCES = test/lp_test.cc |
74 | 76 |
test_maps_test_SOURCES = test/maps_test.cc |
75 | 77 |
test_mip_test_SOURCES = test/mip_test.cc |
76 | 78 |
test_matching_test_SOURCES = test/matching_test.cc |
77 | 79 |
test_min_cost_arborescence_test_SOURCES = test/min_cost_arborescence_test.cc |
78 | 80 |
test_min_cost_flow_test_SOURCES = test/min_cost_flow_test.cc |
79 | 81 |
test_path_test_SOURCES = test/path_test.cc |
80 | 82 |
test_preflow_test_SOURCES = test/preflow_test.cc |
81 | 83 |
test_radix_sort_test_SOURCES = test/radix_sort_test.cc |
82 | 84 |
test_suurballe_test_SOURCES = test/suurballe_test.cc |
83 | 85 |
test_random_test_SOURCES = test/random_test.cc |
84 | 86 |
test_test_tools_fail_SOURCES = test/test_tools_fail.cc |
85 | 87 |
test_test_tools_pass_SOURCES = test/test_tools_pass.cc |
86 | 88 |
test_time_measure_test_SOURCES = test/time_measure_test.cc |
87 | 89 |
test_unionfind_test_SOURCES = test/unionfind_test.cc |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/concepts/digraph.h> |
20 | 20 |
#include <lemon/smart_graph.h> |
21 | 21 |
#include <lemon/list_graph.h> |
22 | 22 |
#include <lemon/lgf_reader.h> |
23 | 23 |
#include <lemon/dfs.h> |
24 | 24 |
#include <lemon/path.h> |
25 | 25 |
|
26 | 26 |
#include "graph_test.h" |
27 | 27 |
#include "test_tools.h" |
28 | 28 |
|
29 | 29 |
using namespace lemon; |
30 | 30 |
|
31 | 31 |
char test_lgf[] = |
32 | 32 |
"@nodes\n" |
33 | 33 |
"label\n" |
34 | 34 |
"0\n" |
35 | 35 |
"1\n" |
36 | 36 |
"2\n" |
37 | 37 |
"3\n" |
38 | 38 |
"4\n" |
39 | 39 |
"5\n" |
40 | 40 |
"6\n" |
41 | 41 |
"@arcs\n" |
42 | 42 |
" label\n" |
43 | 43 |
"0 1 0\n" |
44 | 44 |
"1 2 1\n" |
45 | 45 |
"2 3 2\n" |
46 | 46 |
"1 4 3\n" |
47 | 47 |
"4 2 4\n" |
48 | 48 |
"4 5 5\n" |
49 | 49 |
"5 0 6\n" |
50 | 50 |
"6 3 7\n" |
51 | 51 |
"@attributes\n" |
52 | 52 |
"source 0\n" |
53 |
"target 5\n" |
|
53 |
"target 5\n" |
|
54 |
"source1 6\n" |
|
55 |
"target1 3\n"; |
|
56 |
|
|
54 | 57 |
|
55 | 58 |
void checkDfsCompile() |
56 | 59 |
{ |
57 | 60 |
typedef concepts::Digraph Digraph; |
58 | 61 |
typedef Dfs<Digraph> DType; |
59 | 62 |
typedef Digraph::Node Node; |
60 | 63 |
typedef Digraph::Arc Arc; |
61 | 64 |
|
62 | 65 |
Digraph G; |
63 | 66 |
Node s, t; |
64 | 67 |
Arc e; |
65 | 68 |
int l, i; |
66 | 69 |
bool b; |
67 | 70 |
DType::DistMap d(G); |
68 | 71 |
DType::PredMap p(G); |
69 | 72 |
Path<Digraph> pp; |
70 | 73 |
concepts::ReadMap<Arc,bool> am; |
71 | 74 |
|
72 | 75 |
{ |
73 | 76 |
DType dfs_test(G); |
74 | 77 |
const DType& const_dfs_test = dfs_test; |
75 | 78 |
|
76 | 79 |
dfs_test.run(s); |
77 | 80 |
dfs_test.run(s,t); |
78 | 81 |
dfs_test.run(); |
79 | 82 |
|
80 | 83 |
dfs_test.init(); |
81 | 84 |
dfs_test.addSource(s); |
82 | 85 |
e = dfs_test.processNextArc(); |
83 | 86 |
e = const_dfs_test.nextArc(); |
84 | 87 |
b = const_dfs_test.emptyQueue(); |
85 | 88 |
i = const_dfs_test.queueSize(); |
86 | 89 |
|
87 | 90 |
dfs_test.start(); |
88 | 91 |
dfs_test.start(t); |
89 | 92 |
dfs_test.start(am); |
90 | 93 |
|
91 | 94 |
l = const_dfs_test.dist(t); |
92 | 95 |
e = const_dfs_test.predArc(t); |
93 | 96 |
s = const_dfs_test.predNode(t); |
94 | 97 |
b = const_dfs_test.reached(t); |
95 | 98 |
d = const_dfs_test.distMap(); |
96 | 99 |
p = const_dfs_test.predMap(); |
97 | 100 |
pp = const_dfs_test.path(t); |
98 | 101 |
} |
99 | 102 |
{ |
100 | 103 |
DType |
101 | 104 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
102 | 105 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
103 | 106 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
104 | 107 |
::SetStandardProcessedMap |
105 | 108 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
106 | 109 |
::Create dfs_test(G); |
107 | 110 |
|
108 | 111 |
concepts::ReadWriteMap<Node,Arc> pred_map; |
109 | 112 |
concepts::ReadWriteMap<Node,int> dist_map; |
110 | 113 |
concepts::ReadWriteMap<Node,bool> reached_map; |
111 | 114 |
concepts::WriteMap<Node,bool> processed_map; |
112 | 115 |
|
113 | 116 |
dfs_test |
114 | 117 |
.predMap(pred_map) |
115 | 118 |
.distMap(dist_map) |
116 | 119 |
.reachedMap(reached_map) |
117 | 120 |
.processedMap(processed_map); |
118 | 121 |
|
119 | 122 |
dfs_test.run(s); |
120 | 123 |
dfs_test.run(s,t); |
121 | 124 |
dfs_test.run(); |
122 | 125 |
dfs_test.init(); |
123 | 126 |
|
124 | 127 |
dfs_test.addSource(s); |
125 | 128 |
e = dfs_test.processNextArc(); |
126 | 129 |
e = dfs_test.nextArc(); |
127 | 130 |
b = dfs_test.emptyQueue(); |
128 | 131 |
i = dfs_test.queueSize(); |
129 | 132 |
|
130 | 133 |
dfs_test.start(); |
131 | 134 |
dfs_test.start(t); |
132 | 135 |
dfs_test.start(am); |
133 | 136 |
|
134 | 137 |
l = dfs_test.dist(t); |
135 | 138 |
e = dfs_test.predArc(t); |
136 | 139 |
s = dfs_test.predNode(t); |
137 | 140 |
b = dfs_test.reached(t); |
138 | 141 |
pp = dfs_test.path(t); |
139 | 142 |
} |
140 | 143 |
} |
141 | 144 |
|
142 | 145 |
void checkDfsFunctionCompile() |
143 | 146 |
{ |
144 | 147 |
typedef int VType; |
145 | 148 |
typedef concepts::Digraph Digraph; |
146 | 149 |
typedef Digraph::Arc Arc; |
147 | 150 |
typedef Digraph::Node Node; |
148 | 151 |
|
149 | 152 |
Digraph g; |
150 | 153 |
bool b; |
151 | 154 |
dfs(g).run(Node()); |
152 | 155 |
b=dfs(g).run(Node(),Node()); |
153 | 156 |
dfs(g).run(); |
154 | 157 |
dfs(g) |
155 | 158 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
156 | 159 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
157 | 160 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
158 | 161 |
.processedMap(concepts::WriteMap<Node,bool>()) |
159 | 162 |
.run(Node()); |
160 | 163 |
b=dfs(g) |
161 | 164 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
162 | 165 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
163 | 166 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
164 | 167 |
.processedMap(concepts::WriteMap<Node,bool>()) |
165 | 168 |
.path(concepts::Path<Digraph>()) |
166 | 169 |
.dist(VType()) |
167 | 170 |
.run(Node(),Node()); |
168 | 171 |
dfs(g) |
169 | 172 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
170 | 173 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
171 | 174 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
172 | 175 |
.processedMap(concepts::WriteMap<Node,bool>()) |
173 | 176 |
.run(); |
174 | 177 |
} |
175 | 178 |
|
176 | 179 |
template <class Digraph> |
177 | 180 |
void checkDfs() { |
178 | 181 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
179 | 182 |
|
180 | 183 |
Digraph G; |
181 | 184 |
Node s, t; |
185 |
Node s1, t1; |
|
182 | 186 |
|
183 | 187 |
std::istringstream input(test_lgf); |
184 | 188 |
digraphReader(G, input). |
185 | 189 |
node("source", s). |
186 | 190 |
node("target", t). |
191 |
node("source1", s1). |
|
192 |
node("target1", t1). |
|
187 | 193 |
run(); |
188 | 194 |
|
189 | 195 |
Dfs<Digraph> dfs_test(G); |
190 | 196 |
dfs_test.run(s); |
191 | 197 |
|
192 | 198 |
Path<Digraph> p = dfs_test.path(t); |
193 | 199 |
check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
194 | 200 |
check(checkPath(G, p),"path() found a wrong path."); |
195 | 201 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
196 | 202 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
197 | 203 |
|
198 | 204 |
for(NodeIt v(G); v!=INVALID; ++v) { |
199 | 205 |
if (dfs_test.reached(v)) { |
200 | 206 |
check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
201 | 207 |
if (dfs_test.predArc(v)!=INVALID ) { |
202 | 208 |
Arc e=dfs_test.predArc(v); |
203 | 209 |
Node u=G.source(e); |
204 | 210 |
check(u==dfs_test.predNode(v),"Wrong tree."); |
205 | 211 |
check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
206 | 212 |
"Wrong distance. (" << dfs_test.dist(u) << "->" |
207 | 213 |
<< dfs_test.dist(v) << ")"); |
208 | 214 |
} |
209 | 215 |
} |
210 | 216 |
} |
211 | 217 |
|
212 | 218 |
{ |
219 |
Dfs<Digraph> dfs(G); |
|
220 |
check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6."); |
|
221 |
} |
|
222 |
|
|
223 |
{ |
|
213 | 224 |
NullMap<Node,Arc> myPredMap; |
214 | 225 |
dfs(G).predMap(myPredMap).run(s); |
215 | 226 |
} |
216 | 227 |
} |
217 | 228 |
|
218 | 229 |
int main() |
219 | 230 |
{ |
220 | 231 |
checkDfs<ListDigraph>(); |
221 | 232 |
checkDfs<SmartDigraph>(); |
222 | 233 |
return 0; |
223 | 234 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/smart_graph.h> |
20 | 20 |
#include <lemon/list_graph.h> |
21 | 21 |
#include <lemon/lgf_reader.h> |
22 | 22 |
#include <lemon/error.h> |
23 | 23 |
|
24 | 24 |
#include "test_tools.h" |
25 | 25 |
|
26 | 26 |
using namespace std; |
27 | 27 |
using namespace lemon; |
28 | 28 |
|
29 | 29 |
void digraph_copy_test() { |
30 | 30 |
const int nn = 10; |
31 | 31 |
|
32 |
// Build a digraph |
|
32 | 33 |
SmartDigraph from; |
33 | 34 |
SmartDigraph::NodeMap<int> fnm(from); |
34 | 35 |
SmartDigraph::ArcMap<int> fam(from); |
35 | 36 |
SmartDigraph::Node fn = INVALID; |
36 | 37 |
SmartDigraph::Arc fa = INVALID; |
37 | 38 |
|
38 | 39 |
std::vector<SmartDigraph::Node> fnv; |
39 | 40 |
for (int i = 0; i < nn; ++i) { |
40 | 41 |
SmartDigraph::Node node = from.addNode(); |
41 | 42 |
fnv.push_back(node); |
42 | 43 |
fnm[node] = i * i; |
43 | 44 |
if (i == 0) fn = node; |
44 | 45 |
} |
45 | 46 |
|
46 | 47 |
for (int i = 0; i < nn; ++i) { |
47 | 48 |
for (int j = 0; j < nn; ++j) { |
48 | 49 |
SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]); |
49 | 50 |
fam[arc] = i + j * j; |
50 | 51 |
if (i == 0 && j == 0) fa = arc; |
51 | 52 |
} |
52 | 53 |
} |
53 | 54 |
|
55 |
// Test digraph copy |
|
54 | 56 |
ListDigraph to; |
55 | 57 |
ListDigraph::NodeMap<int> tnm(to); |
56 | 58 |
ListDigraph::ArcMap<int> tam(to); |
57 | 59 |
ListDigraph::Node tn; |
58 | 60 |
ListDigraph::Arc ta; |
59 | 61 |
|
60 | 62 |
SmartDigraph::NodeMap<ListDigraph::Node> nr(from); |
61 | 63 |
SmartDigraph::ArcMap<ListDigraph::Arc> er(from); |
62 | 64 |
|
63 | 65 |
ListDigraph::NodeMap<SmartDigraph::Node> ncr(to); |
64 | 66 |
ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to); |
65 | 67 |
|
66 | 68 |
digraphCopy(from, to). |
67 | 69 |
nodeMap(fnm, tnm).arcMap(fam, tam). |
68 | 70 |
nodeRef(nr).arcRef(er). |
69 | 71 |
nodeCrossRef(ncr).arcCrossRef(ecr). |
70 | 72 |
node(fn, tn).arc(fa, ta).run(); |
73 |
|
|
74 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
|
75 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
|
71 | 76 |
|
72 | 77 |
for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) { |
73 | 78 |
check(ncr[nr[it]] == it, "Wrong copy."); |
74 | 79 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
75 | 80 |
} |
76 | 81 |
|
77 | 82 |
for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) { |
78 | 83 |
check(ecr[er[it]] == it, "Wrong copy."); |
79 | 84 |
check(fam[it] == tam[er[it]], "Wrong copy."); |
80 | 85 |
check(nr[from.source(it)] == to.source(er[it]), "Wrong copy."); |
81 | 86 |
check(nr[from.target(it)] == to.target(er[it]), "Wrong copy."); |
82 | 87 |
} |
83 | 88 |
|
84 | 89 |
for (ListDigraph::NodeIt it(to); it != INVALID; ++it) { |
85 | 90 |
check(nr[ncr[it]] == it, "Wrong copy."); |
86 | 91 |
} |
87 | 92 |
|
88 | 93 |
for (ListDigraph::ArcIt it(to); it != INVALID; ++it) { |
89 | 94 |
check(er[ecr[it]] == it, "Wrong copy."); |
90 | 95 |
} |
91 | 96 |
check(tn == nr[fn], "Wrong copy."); |
92 | 97 |
check(ta == er[fa], "Wrong copy."); |
98 |
|
|
99 |
// Test repeated copy |
|
100 |
digraphCopy(from, to).run(); |
|
101 |
|
|
102 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
|
103 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
|
93 | 104 |
} |
94 | 105 |
|
95 | 106 |
void graph_copy_test() { |
96 | 107 |
const int nn = 10; |
97 | 108 |
|
109 |
// Build a graph |
|
98 | 110 |
SmartGraph from; |
99 | 111 |
SmartGraph::NodeMap<int> fnm(from); |
100 | 112 |
SmartGraph::ArcMap<int> fam(from); |
101 | 113 |
SmartGraph::EdgeMap<int> fem(from); |
102 | 114 |
SmartGraph::Node fn = INVALID; |
103 | 115 |
SmartGraph::Arc fa = INVALID; |
104 | 116 |
SmartGraph::Edge fe = INVALID; |
105 | 117 |
|
106 | 118 |
std::vector<SmartGraph::Node> fnv; |
107 | 119 |
for (int i = 0; i < nn; ++i) { |
108 | 120 |
SmartGraph::Node node = from.addNode(); |
109 | 121 |
fnv.push_back(node); |
110 | 122 |
fnm[node] = i * i; |
111 | 123 |
if (i == 0) fn = node; |
112 | 124 |
} |
113 | 125 |
|
114 | 126 |
for (int i = 0; i < nn; ++i) { |
115 | 127 |
for (int j = 0; j < nn; ++j) { |
116 | 128 |
SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]); |
117 | 129 |
fem[edge] = i * i + j * j; |
118 | 130 |
fam[from.direct(edge, true)] = i + j * j; |
119 | 131 |
fam[from.direct(edge, false)] = i * i + j; |
120 | 132 |
if (i == 0 && j == 0) fa = from.direct(edge, true); |
121 | 133 |
if (i == 0 && j == 0) fe = edge; |
122 | 134 |
} |
123 | 135 |
} |
124 | 136 |
|
137 |
// Test graph copy |
|
125 | 138 |
ListGraph to; |
126 | 139 |
ListGraph::NodeMap<int> tnm(to); |
127 | 140 |
ListGraph::ArcMap<int> tam(to); |
128 | 141 |
ListGraph::EdgeMap<int> tem(to); |
129 | 142 |
ListGraph::Node tn; |
130 | 143 |
ListGraph::Arc ta; |
131 | 144 |
ListGraph::Edge te; |
132 | 145 |
|
133 | 146 |
SmartGraph::NodeMap<ListGraph::Node> nr(from); |
134 | 147 |
SmartGraph::ArcMap<ListGraph::Arc> ar(from); |
135 | 148 |
SmartGraph::EdgeMap<ListGraph::Edge> er(from); |
136 | 149 |
|
137 | 150 |
ListGraph::NodeMap<SmartGraph::Node> ncr(to); |
138 | 151 |
ListGraph::ArcMap<SmartGraph::Arc> acr(to); |
139 | 152 |
ListGraph::EdgeMap<SmartGraph::Edge> ecr(to); |
140 | 153 |
|
141 | 154 |
graphCopy(from, to). |
142 | 155 |
nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem). |
143 | 156 |
nodeRef(nr).arcRef(ar).edgeRef(er). |
144 | 157 |
nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr). |
145 | 158 |
node(fn, tn).arc(fa, ta).edge(fe, te).run(); |
146 | 159 |
|
160 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
|
161 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
|
162 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
|
163 |
|
|
147 | 164 |
for (SmartGraph::NodeIt it(from); it != INVALID; ++it) { |
148 | 165 |
check(ncr[nr[it]] == it, "Wrong copy."); |
149 | 166 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
150 | 167 |
} |
151 | 168 |
|
152 | 169 |
for (SmartGraph::ArcIt it(from); it != INVALID; ++it) { |
153 | 170 |
check(acr[ar[it]] == it, "Wrong copy."); |
154 | 171 |
check(fam[it] == tam[ar[it]], "Wrong copy."); |
155 | 172 |
check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy."); |
156 | 173 |
check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy."); |
157 | 174 |
} |
158 | 175 |
|
159 | 176 |
for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) { |
160 | 177 |
check(ecr[er[it]] == it, "Wrong copy."); |
161 | 178 |
check(fem[it] == tem[er[it]], "Wrong copy."); |
162 | 179 |
check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), |
163 | 180 |
"Wrong copy."); |
164 | 181 |
check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), |
165 | 182 |
"Wrong copy."); |
166 | 183 |
check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), |
167 | 184 |
"Wrong copy."); |
168 | 185 |
} |
169 | 186 |
|
170 | 187 |
for (ListGraph::NodeIt it(to); it != INVALID; ++it) { |
171 | 188 |
check(nr[ncr[it]] == it, "Wrong copy."); |
172 | 189 |
} |
173 | 190 |
|
174 | 191 |
for (ListGraph::ArcIt it(to); it != INVALID; ++it) { |
175 | 192 |
check(ar[acr[it]] == it, "Wrong copy."); |
176 | 193 |
} |
177 | 194 |
for (ListGraph::EdgeIt it(to); it != INVALID; ++it) { |
178 | 195 |
check(er[ecr[it]] == it, "Wrong copy."); |
179 | 196 |
} |
180 | 197 |
check(tn == nr[fn], "Wrong copy."); |
181 | 198 |
check(ta == ar[fa], "Wrong copy."); |
182 | 199 |
check(te == er[fe], "Wrong copy."); |
200 |
|
|
201 |
// Test repeated copy |
|
202 |
graphCopy(from, to).run(); |
|
203 |
|
|
204 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
|
205 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
|
206 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
|
183 | 207 |
} |
184 | 208 |
|
185 | 209 |
|
186 | 210 |
int main() { |
187 | 211 |
digraph_copy_test(); |
188 | 212 |
graph_copy_test(); |
189 | 213 |
|
190 | 214 |
return 0; |
191 | 215 |
} |
0 comments (0 inline)