1 | 1 |
LEMON code without an explicit copyright is covered by the following |
2 | 2 |
copyright/license. |
3 | 3 |
|
4 |
Copyright (C) 2003- |
|
4 |
Copyright (C) 2003-2009 Egervary Jeno Kombinatorikus Optimalizalasi |
|
5 | 5 |
Kutatocsoport (Egervary Combinatorial Optimization Research Group, |
6 | 6 |
EGRES). |
7 | 7 |
|
8 | 8 |
Permission is hereby granted, free of charge, to any person or organization |
9 | 9 |
obtaining a copy of the software and accompanying documentation covered by |
10 | 10 |
this license (the "Software") to use, reproduce, display, distribute, |
11 | 11 |
execute, and transmit the Software, and to prepare derivative works of the |
12 | 12 |
Software, and to permit third-parties to whom the Software is furnished to |
13 | 13 |
do so, all subject to the following: |
14 | 14 |
|
15 | 15 |
The copyright notices in the Software and this entire statement, including |
16 | 16 |
the above license grant, this restriction and the following disclaimer, |
17 | 17 |
must be included in all copies of the Software, in whole or in part, and |
18 | 18 |
all derivative works of the Software, unless such copies or derivative |
19 | 19 |
works are solely in the form of machine-executable object code generated by |
20 | 20 |
a source language processor. |
21 | 21 |
|
22 | 22 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23 | 23 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24 | 24 |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
25 | 25 |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
26 | 26 |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
27 | 27 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
28 | 28 |
DEALINGS IN THE SOFTWARE. |
29 | 29 |
|
30 | 30 |
=========================================================================== |
31 | 31 |
This license is a verbatim copy of the Boost Software License, Version 1.0. |
32 | 32 |
|
33 | 33 |
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-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 |
///\ingroup demos |
20 | 20 |
///\file |
21 | 21 |
///\brief Argument parser demo |
22 | 22 |
/// |
23 | 23 |
/// This example shows how the argument parser can be used. |
24 | 24 |
/// |
25 | 25 |
/// \include arg_parser_demo.cc |
26 | 26 |
|
27 | 27 |
#include <lemon/arg_parser.h> |
28 | 28 |
|
29 | 29 |
using namespace lemon; |
30 | 30 |
int main(int argc, char **argv) |
31 | 31 |
{ |
32 | 32 |
// Initialize the argument parser |
33 | 33 |
ArgParser ap(argc, argv); |
34 | 34 |
int i; |
35 | 35 |
std::string s; |
36 | 36 |
double d = 1.0; |
37 | 37 |
bool b, nh; |
38 | 38 |
bool g1, g2, g3; |
39 | 39 |
|
40 | 40 |
// Add a mandatory integer option with storage reference |
41 | 41 |
ap.refOption("n", "An integer input.", i, true); |
42 | 42 |
// Add a double option with storage reference (the default value is 1.0) |
43 | 43 |
ap.refOption("val", "A double input.", d); |
44 | 44 |
// Add a double option without storage reference (the default value is 3.14) |
45 | 45 |
ap.doubleOption("val2", "A double input.", 3.14); |
46 | 46 |
// Set synonym for -val option |
47 | 47 |
ap.synonym("vals", "val"); |
48 | 48 |
// Add a string option |
49 | 49 |
ap.refOption("name", "A string input.", s); |
50 | 50 |
// Add bool options |
51 | 51 |
ap.refOption("f", "A switch.", b) |
52 | 52 |
.refOption("nohelp", "", nh) |
53 | 53 |
.refOption("gra", "Choice A", g1) |
54 | 54 |
.refOption("grb", "Choice B", g2) |
55 | 55 |
.refOption("grc", "Choice C", g3); |
56 | 56 |
// Bundle -gr* options into a group |
57 | 57 |
ap.optionGroup("gr", "gra") |
58 | 58 |
.optionGroup("gr", "grb") |
59 | 59 |
.optionGroup("gr", "grc"); |
60 | 60 |
// Set the group mandatory |
61 | 61 |
ap.mandatoryGroup("gr"); |
62 | 62 |
// Set the options of the group exclusive (only one option can be given) |
63 | 63 |
ap.onlyOneGroup("gr"); |
64 | 64 |
// Add non-parsed arguments (e.g. input files) |
65 | 65 |
ap.other("infile", "The input file.") |
66 | 66 |
.other("..."); |
67 | 67 |
|
68 | 68 |
// Perform the parsing process |
69 | 69 |
// (in case of any error it terminates the program) |
70 | 70 |
ap.parse(); |
71 | 71 |
|
72 | 72 |
// Check each option if it has been given and print its value |
73 | 73 |
std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
74 | 74 |
|
75 | 75 |
std::cout << " Value of -n: " << i << std::endl; |
76 | 76 |
if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl; |
77 | 77 |
if(ap.given("val2")) { |
78 | 78 |
d = ap["val2"]; |
79 | 79 |
std::cout << " Value of -val2: " << d << std::endl; |
80 | 80 |
} |
81 | 81 |
if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl; |
82 | 82 |
if(ap.given("f")) std::cout << " -f is given\n"; |
83 | 83 |
if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh << std::endl; |
84 | 84 |
if(ap.given("gra")) std::cout << " -gra is given\n"; |
85 | 85 |
if(ap.given("grb")) std::cout << " -grb is given\n"; |
86 | 86 |
if(ap.given("grc")) std::cout << " -grc is given\n"; |
87 | 87 |
|
88 | 88 |
switch(ap.files().size()) { |
89 | 89 |
case 0: |
90 | 90 |
std::cout << " No file argument was given.\n"; |
91 | 91 |
break; |
92 | 92 |
case 1: |
93 | 93 |
std::cout << " 1 file argument was given. It is:\n"; |
94 | 94 |
break; |
95 | 95 |
default: |
96 | 96 |
std::cout << " " |
97 | 97 |
<< ap.files().size() << " file arguments were given. They are:\n"; |
98 | 98 |
} |
99 | 99 |
for(unsigned int i=0;i<ap.files().size();++i) |
100 | 100 |
std::cout << " '" << ap.files()[i] << "'\n"; |
101 | 101 |
|
102 | 102 |
return 0; |
103 | 103 |
} |
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-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 |
/// \ingroup demos |
20 | 20 |
/// \file |
21 | 21 |
/// \brief Demo of the graph drawing function \ref graphToEps() |
22 | 22 |
/// |
23 | 23 |
/// This demo program shows examples how to use the function \ref |
24 | 24 |
/// graphToEps(). It takes no input but simply creates seven |
25 | 25 |
/// <tt>.eps</tt> files demonstrating the capability of \ref |
26 | 26 |
/// graphToEps(), and showing how to draw directed graphs, |
27 | 27 |
/// how to handle parallel egdes, how to change the properties (like |
28 | 28 |
/// color, shape, size, title etc.) of nodes and arcs individually |
29 | 29 |
/// using appropriate graph maps. |
30 | 30 |
/// |
31 | 31 |
/// \include graph_to_eps_demo.cc |
32 | 32 |
|
33 | 33 |
#include<lemon/list_graph.h> |
34 | 34 |
#include<lemon/graph_to_eps.h> |
35 | 35 |
#include<lemon/math.h> |
36 | 36 |
|
37 | 37 |
using namespace std; |
38 | 38 |
using namespace lemon; |
39 | 39 |
|
40 | 40 |
int main() |
41 | 41 |
{ |
42 | 42 |
Palette palette; |
43 | 43 |
Palette paletteW(true); |
44 | 44 |
|
45 | 45 |
// Create a small digraph |
46 | 46 |
ListDigraph g; |
47 | 47 |
typedef ListDigraph::Node Node; |
48 | 48 |
typedef ListDigraph::NodeIt NodeIt; |
49 | 49 |
typedef ListDigraph::Arc Arc; |
50 | 50 |
typedef dim2::Point<int> Point; |
51 | 51 |
|
52 | 52 |
Node n1=g.addNode(); |
53 | 53 |
Node n2=g.addNode(); |
54 | 54 |
Node n3=g.addNode(); |
55 | 55 |
Node n4=g.addNode(); |
56 | 56 |
Node n5=g.addNode(); |
57 | 57 |
|
58 | 58 |
ListDigraph::NodeMap<Point> coords(g); |
59 | 59 |
ListDigraph::NodeMap<double> sizes(g); |
60 | 60 |
ListDigraph::NodeMap<int> colors(g); |
61 | 61 |
ListDigraph::NodeMap<int> shapes(g); |
62 | 62 |
ListDigraph::ArcMap<int> acolors(g); |
63 | 63 |
ListDigraph::ArcMap<int> widths(g); |
64 | 64 |
|
65 | 65 |
coords[n1]=Point(50,50); sizes[n1]=1; colors[n1]=1; shapes[n1]=0; |
66 | 66 |
coords[n2]=Point(50,70); sizes[n2]=2; colors[n2]=2; shapes[n2]=2; |
67 | 67 |
coords[n3]=Point(70,70); sizes[n3]=1; colors[n3]=3; shapes[n3]=0; |
68 | 68 |
coords[n4]=Point(70,50); sizes[n4]=2; colors[n4]=4; shapes[n4]=1; |
69 | 69 |
coords[n5]=Point(85,60); sizes[n5]=3; colors[n5]=5; shapes[n5]=2; |
70 | 70 |
|
71 | 71 |
Arc a; |
72 | 72 |
|
73 | 73 |
a=g.addArc(n1,n2); acolors[a]=0; widths[a]=1; |
74 | 74 |
a=g.addArc(n2,n3); acolors[a]=0; widths[a]=1; |
75 | 75 |
a=g.addArc(n3,n5); acolors[a]=0; widths[a]=3; |
76 | 76 |
a=g.addArc(n5,n4); acolors[a]=0; widths[a]=1; |
77 | 77 |
a=g.addArc(n4,n1); acolors[a]=0; widths[a]=1; |
78 | 78 |
a=g.addArc(n2,n4); acolors[a]=1; widths[a]=2; |
79 | 79 |
a=g.addArc(n3,n4); acolors[a]=2; widths[a]=1; |
80 | 80 |
|
81 | 81 |
IdMap<ListDigraph,Node> id(g); |
82 | 82 |
|
83 | 83 |
// Create .eps files showing the digraph with different options |
84 | 84 |
cout << "Create 'graph_to_eps_demo_out_1_pure.eps'" << endl; |
85 | 85 |
graphToEps(g,"graph_to_eps_demo_out_1_pure.eps"). |
86 | 86 |
coords(coords). |
87 | 87 |
title("Sample .eps figure"). |
88 |
copyright("(C) 2003- |
|
88 |
copyright("(C) 2003-2009 LEMON Project"). |
|
89 | 89 |
run(); |
90 | 90 |
|
91 | 91 |
cout << "Create 'graph_to_eps_demo_out_2.eps'" << endl; |
92 | 92 |
graphToEps(g,"graph_to_eps_demo_out_2.eps"). |
93 | 93 |
coords(coords). |
94 | 94 |
title("Sample .eps figure"). |
95 |
copyright("(C) 2003- |
|
95 |
copyright("(C) 2003-2009 LEMON Project"). |
|
96 | 96 |
absoluteNodeSizes().absoluteArcWidths(). |
97 | 97 |
nodeScale(2).nodeSizes(sizes). |
98 | 98 |
nodeShapes(shapes). |
99 | 99 |
nodeColors(composeMap(palette,colors)). |
100 | 100 |
arcColors(composeMap(palette,acolors)). |
101 | 101 |
arcWidthScale(.4).arcWidths(widths). |
102 | 102 |
nodeTexts(id).nodeTextSize(3). |
103 | 103 |
run(); |
104 | 104 |
|
105 | 105 |
cout << "Create 'graph_to_eps_demo_out_3_arr.eps'" << endl; |
106 | 106 |
graphToEps(g,"graph_to_eps_demo_out_3_arr.eps"). |
107 | 107 |
title("Sample .eps figure (with arrowheads)"). |
108 |
copyright("(C) 2003- |
|
108 |
copyright("(C) 2003-2009 LEMON Project"). |
|
109 | 109 |
absoluteNodeSizes().absoluteArcWidths(). |
110 | 110 |
nodeColors(composeMap(palette,colors)). |
111 | 111 |
coords(coords). |
112 | 112 |
nodeScale(2).nodeSizes(sizes). |
113 | 113 |
nodeShapes(shapes). |
114 | 114 |
arcColors(composeMap(palette,acolors)). |
115 | 115 |
arcWidthScale(.4).arcWidths(widths). |
116 | 116 |
nodeTexts(id).nodeTextSize(3). |
117 | 117 |
drawArrows().arrowWidth(2).arrowLength(2). |
118 | 118 |
run(); |
119 | 119 |
|
120 | 120 |
// Add more arcs to the digraph |
121 | 121 |
a=g.addArc(n1,n4); acolors[a]=2; widths[a]=1; |
122 | 122 |
a=g.addArc(n4,n1); acolors[a]=1; widths[a]=2; |
123 | 123 |
|
124 | 124 |
a=g.addArc(n1,n2); acolors[a]=1; widths[a]=1; |
125 | 125 |
a=g.addArc(n1,n2); acolors[a]=2; widths[a]=1; |
126 | 126 |
a=g.addArc(n1,n2); acolors[a]=3; widths[a]=1; |
127 | 127 |
a=g.addArc(n1,n2); acolors[a]=4; widths[a]=1; |
128 | 128 |
a=g.addArc(n1,n2); acolors[a]=5; widths[a]=1; |
129 | 129 |
a=g.addArc(n1,n2); acolors[a]=6; widths[a]=1; |
130 | 130 |
a=g.addArc(n1,n2); acolors[a]=7; widths[a]=1; |
131 | 131 |
|
132 | 132 |
cout << "Create 'graph_to_eps_demo_out_4_par.eps'" << endl; |
133 | 133 |
graphToEps(g,"graph_to_eps_demo_out_4_par.eps"). |
134 | 134 |
title("Sample .eps figure (parallel arcs)"). |
135 |
copyright("(C) 2003- |
|
135 |
copyright("(C) 2003-2009 LEMON Project"). |
|
136 | 136 |
absoluteNodeSizes().absoluteArcWidths(). |
137 | 137 |
nodeShapes(shapes). |
138 | 138 |
coords(coords). |
139 | 139 |
nodeScale(2).nodeSizes(sizes). |
140 | 140 |
nodeColors(composeMap(palette,colors)). |
141 | 141 |
arcColors(composeMap(palette,acolors)). |
142 | 142 |
arcWidthScale(.4).arcWidths(widths). |
143 | 143 |
nodeTexts(id).nodeTextSize(3). |
144 | 144 |
enableParallel().parArcDist(1.5). |
145 | 145 |
run(); |
146 | 146 |
|
147 | 147 |
cout << "Create 'graph_to_eps_demo_out_5_par_arr.eps'" << endl; |
148 | 148 |
graphToEps(g,"graph_to_eps_demo_out_5_par_arr.eps"). |
149 | 149 |
title("Sample .eps figure (parallel arcs and arrowheads)"). |
150 |
copyright("(C) 2003- |
|
150 |
copyright("(C) 2003-2009 LEMON Project"). |
|
151 | 151 |
absoluteNodeSizes().absoluteArcWidths(). |
152 | 152 |
nodeScale(2).nodeSizes(sizes). |
153 | 153 |
coords(coords). |
154 | 154 |
nodeShapes(shapes). |
155 | 155 |
nodeColors(composeMap(palette,colors)). |
156 | 156 |
arcColors(composeMap(palette,acolors)). |
157 | 157 |
arcWidthScale(.3).arcWidths(widths). |
158 | 158 |
nodeTexts(id).nodeTextSize(3). |
159 | 159 |
enableParallel().parArcDist(1). |
160 | 160 |
drawArrows().arrowWidth(1).arrowLength(1). |
161 | 161 |
run(); |
162 | 162 |
|
163 | 163 |
cout << "Create 'graph_to_eps_demo_out_6_par_arr_a4.eps'" << endl; |
164 | 164 |
graphToEps(g,"graph_to_eps_demo_out_6_par_arr_a4.eps"). |
165 | 165 |
title("Sample .eps figure (fits to A4)"). |
166 |
copyright("(C) 2003- |
|
166 |
copyright("(C) 2003-2009 LEMON Project"). |
|
167 | 167 |
scaleToA4(). |
168 | 168 |
absoluteNodeSizes().absoluteArcWidths(). |
169 | 169 |
nodeScale(2).nodeSizes(sizes). |
170 | 170 |
coords(coords). |
171 | 171 |
nodeShapes(shapes). |
172 | 172 |
nodeColors(composeMap(palette,colors)). |
173 | 173 |
arcColors(composeMap(palette,acolors)). |
174 | 174 |
arcWidthScale(.3).arcWidths(widths). |
175 | 175 |
nodeTexts(id).nodeTextSize(3). |
176 | 176 |
enableParallel().parArcDist(1). |
177 | 177 |
drawArrows().arrowWidth(1).arrowLength(1). |
178 | 178 |
run(); |
179 | 179 |
|
180 | 180 |
// Create an .eps file showing the colors of a default Palette |
181 | 181 |
ListDigraph h; |
182 | 182 |
ListDigraph::NodeMap<int> hcolors(h); |
183 | 183 |
ListDigraph::NodeMap<Point> hcoords(h); |
184 | 184 |
|
185 | 185 |
int cols=int(sqrt(double(palette.size()))); |
186 | 186 |
for(int i=0;i<int(paletteW.size());i++) { |
187 | 187 |
Node n=h.addNode(); |
188 | 188 |
hcoords[n]=Point(1+i%cols,1+i/cols); |
189 | 189 |
hcolors[n]=i; |
190 | 190 |
} |
191 | 191 |
|
192 | 192 |
cout << "Create 'graph_to_eps_demo_out_7_colors.eps'" << endl; |
193 | 193 |
graphToEps(h,"graph_to_eps_demo_out_7_colors.eps"). |
194 | 194 |
scale(60). |
195 | 195 |
title("Sample .eps figure (Palette demo)"). |
196 |
copyright("(C) 2003- |
|
196 |
copyright("(C) 2003-2009 LEMON Project"). |
|
197 | 197 |
coords(hcoords). |
198 | 198 |
absoluteNodeSizes().absoluteArcWidths(). |
199 | 199 |
nodeScale(.45). |
200 | 200 |
distantColorNodeTexts(). |
201 | 201 |
nodeTexts(hcolors).nodeTextSize(.6). |
202 | 202 |
nodeColors(composeMap(paletteW,hcolors)). |
203 | 203 |
run(); |
204 | 204 |
|
205 | 205 |
return 0; |
206 | 206 |
} |
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-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 |
///\ingroup demos |
20 | 20 |
///\file |
21 | 21 |
///\brief Demonstrating graph input and output |
22 | 22 |
/// |
23 | 23 |
/// This program gives an example of how to read and write a digraph |
24 | 24 |
/// and additional maps from/to a stream or a file using the |
25 | 25 |
/// \ref lgf-format "LGF" format. |
26 | 26 |
/// |
27 | 27 |
/// The \c "digraph.lgf" file: |
28 | 28 |
/// \include digraph.lgf |
29 | 29 |
/// |
30 | 30 |
/// And the program which reads it and prints the digraph to the |
31 | 31 |
/// standard output: |
32 | 32 |
/// \include lgf_demo.cc |
33 | 33 |
|
34 | 34 |
#include <iostream> |
35 | 35 |
#include <lemon/smart_graph.h> |
36 | 36 |
#include <lemon/lgf_reader.h> |
37 | 37 |
#include <lemon/lgf_writer.h> |
38 | 38 |
|
39 | 39 |
using namespace lemon; |
40 | 40 |
|
41 | 41 |
int main() { |
42 | 42 |
SmartDigraph g; |
43 | 43 |
SmartDigraph::ArcMap<int> cap(g); |
44 | 44 |
SmartDigraph::Node s, t; |
45 | 45 |
|
46 | 46 |
try { |
47 | 47 |
digraphReader(g, "digraph.lgf"). // read the directed graph into g |
48 | 48 |
arcMap("capacity", cap). // read the 'capacity' arc map into cap |
49 | 49 |
node("source", s). // read 'source' node to s |
50 | 50 |
node("target", t). // read 'target' node to t |
51 | 51 |
run(); |
52 | 52 |
} catch (Exception& error) { // check if there was any error |
53 | 53 |
std::cerr << "Error: " << error.what() << std::endl; |
54 | 54 |
return -1; |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
std::cout << "A digraph is read from 'digraph.lgf'." << std::endl; |
58 | 58 |
std::cout << "Number of nodes: " << countNodes(g) << std::endl; |
59 | 59 |
std::cout << "Number of arcs: " << countArcs(g) << std::endl; |
60 | 60 |
|
61 | 61 |
std::cout << "We can write it to the standard output:" << std::endl; |
62 | 62 |
|
63 | 63 |
digraphWriter(g). // write g to the standard output |
64 | 64 |
arcMap("capacity", cap). // write cap into 'capacity' |
65 | 65 |
node("source", s). // write s to 'source' |
66 | 66 |
node("target", t). // write t to 'target' |
67 | 67 |
run(); |
68 | 68 |
|
69 | 69 |
return 0; |
70 | 70 |
} |
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-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 |
/*! |
20 | 20 |
|
21 | 21 |
\page coding_style LEMON Coding Style |
22 | 22 |
|
23 | 23 |
\section naming_conv Naming Conventions |
24 | 24 |
|
25 | 25 |
In order to make development easier we have made some conventions |
26 | 26 |
according to coding style. These include names of types, classes, |
27 | 27 |
functions, variables, constants and exceptions. If these conventions |
28 | 28 |
are met in one's code then it is easier to read and maintain |
29 | 29 |
it. Please comply with these conventions if you want to contribute |
30 | 30 |
developing LEMON library. |
31 | 31 |
|
32 | 32 |
\note When the coding style requires the capitalization of an abbreviation, |
33 | 33 |
only the first letter should be upper case. |
34 | 34 |
|
35 | 35 |
\code |
36 | 36 |
XmlReader |
37 | 37 |
\endcode |
38 | 38 |
|
39 | 39 |
|
40 | 40 |
\warning In some cases we diverge from these rules. |
41 | 41 |
This is primary done because STL uses different naming convention and |
42 | 42 |
in certain cases |
43 | 43 |
it is beneficial to provide STL compatible interface. |
44 | 44 |
|
45 | 45 |
\subsection cs-files File Names |
46 | 46 |
|
47 | 47 |
The header file names should look like the following. |
48 | 48 |
|
49 | 49 |
\code |
50 | 50 |
header_file.h |
51 | 51 |
\endcode |
52 | 52 |
|
53 | 53 |
Note that all standard LEMON headers are located in the \c lemon subdirectory, |
54 | 54 |
so you should include them from C++ source like this: |
55 | 55 |
|
56 | 56 |
\code |
57 | 57 |
#include <lemon/header_file.h> |
58 | 58 |
\endcode |
59 | 59 |
|
60 | 60 |
The source code files use the same style and they have '.cc' extension. |
61 | 61 |
|
62 | 62 |
\code |
63 | 63 |
source_code.cc |
64 | 64 |
\endcode |
65 | 65 |
|
66 | 66 |
\subsection cs-class Classes and other types |
67 | 67 |
|
68 | 68 |
The name of a class or any type should look like the following. |
69 | 69 |
|
70 | 70 |
\code |
71 | 71 |
AllWordsCapitalizedWithoutUnderscores |
72 | 72 |
\endcode |
73 | 73 |
|
74 | 74 |
\subsection cs-func Methods and other functions |
75 | 75 |
|
76 | 76 |
The name of a function should look like the following. |
77 | 77 |
|
78 | 78 |
\code |
79 | 79 |
firstWordLowerCaseRestCapitalizedWithoutUnderscores |
80 | 80 |
\endcode |
81 | 81 |
|
82 | 82 |
\subsection cs-funcs Constants, Macros |
83 | 83 |
|
84 | 84 |
The names of constants and macros should look like the following. |
85 | 85 |
|
86 | 86 |
\code |
87 | 87 |
ALL_UPPER_CASE_WITH_UNDERSCORES |
88 | 88 |
\endcode |
89 | 89 |
|
90 | 90 |
\subsection cs-loc-var Class and instance member variables, auto variables |
91 | 91 |
|
92 | 92 |
The names of class and instance member variables and auto variables |
93 | 93 |
(=variables used locally in methods) should look like the following. |
94 | 94 |
|
95 | 95 |
\code |
96 | 96 |
all_lower_case_with_underscores |
97 | 97 |
\endcode |
98 | 98 |
|
99 | 99 |
\subsection pri-loc-var Private member variables |
100 | 100 |
|
101 | 101 |
Private member variables should start with underscore |
102 | 102 |
|
103 | 103 |
\code |
104 | 104 |
_start_with_underscores |
105 | 105 |
\endcode |
106 | 106 |
|
107 | 107 |
\subsection cs-excep Exceptions |
108 | 108 |
|
109 | 109 |
When writing exceptions please comply the following naming conventions. |
110 | 110 |
|
111 | 111 |
\code |
112 | 112 |
ClassNameEndsWithException |
113 | 113 |
\endcode |
114 | 114 |
|
115 | 115 |
or |
116 | 116 |
|
117 | 117 |
\code |
118 | 118 |
ClassNameEndsWithError |
119 | 119 |
\endcode |
120 | 120 |
|
121 | 121 |
\section header-template Template Header File |
122 | 122 |
|
123 | 123 |
Each LEMON header file should look like this: |
124 | 124 |
|
125 | 125 |
\include template.h |
126 | 126 |
|
127 | 127 |
*/ |
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-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 |
/** |
20 | 20 |
\dir demo |
21 | 21 |
\brief A collection of demo applications. |
22 | 22 |
|
23 | 23 |
This directory contains several simple demo applications, mainly |
24 | 24 |
for educational purposes. |
25 | 25 |
*/ |
26 | 26 |
|
27 | 27 |
/** |
28 | 28 |
\dir doc |
29 | 29 |
\brief Auxiliary (and the whole generated) documentation. |
30 | 30 |
|
31 | 31 |
This directory contains some auxiliary pages and the whole generated |
32 | 32 |
documentation. |
33 | 33 |
*/ |
34 | 34 |
|
35 | 35 |
/** |
36 | 36 |
\dir test |
37 | 37 |
\brief Test programs. |
38 | 38 |
|
39 | 39 |
This directory contains several test programs that check the consistency |
40 | 40 |
of the code. |
41 | 41 |
*/ |
42 | 42 |
|
43 | 43 |
/** |
44 | 44 |
\dir tools |
45 | 45 |
\brief Some useful executables. |
46 | 46 |
|
47 | 47 |
This directory contains the sources of some useful complete executables. |
48 | 48 |
*/ |
49 | 49 |
|
50 | 50 |
/** |
51 | 51 |
\dir lemon |
52 | 52 |
\brief Base include directory of LEMON. |
53 | 53 |
|
54 | 54 |
This is the base directory of LEMON includes, so each include file must be |
55 | 55 |
prefixed with this, e.g. |
56 | 56 |
\code |
57 | 57 |
#include<lemon/list_graph.h> |
58 | 58 |
#include<lemon/dijkstra.h> |
59 | 59 |
\endcode |
60 | 60 |
*/ |
61 | 61 |
|
62 | 62 |
/** |
63 | 63 |
\dir concepts |
64 | 64 |
\brief Concept descriptors and checking classes. |
65 | 65 |
|
66 | 66 |
This directory contains the concept descriptors and concept checking tools. |
67 | 67 |
For more information see the \ref concept "Concepts" module. |
68 | 68 |
*/ |
69 | 69 |
|
70 | 70 |
/** |
71 | 71 |
\dir bits |
72 | 72 |
\brief Auxiliary tools for implementation. |
73 | 73 |
|
74 |
This directory contains some auxiliary classes for implementing graphs, |
|
74 |
This directory contains some auxiliary classes for implementing graphs, |
|
75 | 75 |
maps and some other classes. |
76 | 76 |
As a user you typically don't have to deal with these files. |
77 | 77 |
*/ |
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-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 |
@defgroup datas Data Structures |
23 | 23 |
This group describes the several data structures implemented in LEMON. |
24 | 24 |
*/ |
25 | 25 |
|
26 | 26 |
/** |
27 | 27 |
@defgroup graphs Graph Structures |
28 | 28 |
@ingroup datas |
29 | 29 |
\brief Graph structures implemented in LEMON. |
30 | 30 |
|
31 | 31 |
The implementation of combinatorial algorithms heavily relies on |
32 | 32 |
efficient graph implementations. LEMON offers data structures which are |
33 | 33 |
planned to be easily used in an experimental phase of implementation studies, |
34 | 34 |
and thereafter the program code can be made efficient by small modifications. |
35 | 35 |
|
36 | 36 |
The most efficient implementation of diverse applications require the |
37 | 37 |
usage of different physical graph implementations. These differences |
38 | 38 |
appear in the size of graph we require to handle, memory or time usage |
39 | 39 |
limitations or in the set of operations through which the graph can be |
40 | 40 |
accessed. LEMON provides several physical graph structures to meet |
41 | 41 |
the diverging requirements of the possible users. In order to save on |
42 | 42 |
running time or on memory usage, some structures may fail to provide |
43 | 43 |
some graph features like arc/edge or node deletion. |
44 | 44 |
|
45 | 45 |
Alteration of standard containers need a very limited number of |
46 | 46 |
operations, these together satisfy the everyday requirements. |
47 | 47 |
In the case of graph structures, different operations are needed which do |
48 | 48 |
not alter the physical graph, but gives another view. If some nodes or |
49 | 49 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
50 | 50 |
this is the case. It also may happen that in a flow implementation |
51 | 51 |
the residual graph can be accessed by another algorithm, or a node-set |
52 | 52 |
is to be shrunk for another algorithm. |
53 | 53 |
LEMON also provides a variety of graphs for these requirements called |
54 | 54 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
55 | 55 |
in conjunction with other graph representations. |
56 | 56 |
|
57 | 57 |
You are free to use the graph structure that fit your requirements |
58 | 58 |
the best, most graph algorithms and auxiliary data structures can be used |
59 | 59 |
with any graph structure. |
60 | 60 |
|
61 | 61 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
62 | 62 |
*/ |
63 | 63 |
|
64 | 64 |
/** |
65 | 65 |
@defgroup graph_adaptors Adaptor Classes for graphs |
66 | 66 |
@ingroup graphs |
67 | 67 |
\brief This group contains several adaptor classes for digraphs and graphs |
68 | 68 |
|
69 | 69 |
The main parts of LEMON are the different graph structures, generic |
70 | 70 |
graph algorithms, graph concepts which couple these, and graph |
71 | 71 |
adaptors. While the previous notions are more or less clear, the |
72 | 72 |
latter one needs further explanation. Graph adaptors are graph classes |
73 | 73 |
which serve for considering graph structures in different ways. |
74 | 74 |
|
75 | 75 |
A short example makes this much clearer. Suppose that we have an |
76 | 76 |
instance \c g of a directed graph type say ListDigraph and an algorithm |
77 | 77 |
\code |
78 | 78 |
template <typename Digraph> |
79 | 79 |
int algorithm(const Digraph&); |
80 | 80 |
\endcode |
81 | 81 |
is needed to run on the reverse oriented graph. It may be expensive |
82 | 82 |
(in time or in memory usage) to copy \c g with the reversed |
83 | 83 |
arcs. In this case, an adaptor class is used, which (according |
84 | 84 |
to LEMON digraph concepts) works as a digraph. The adaptor uses the |
85 | 85 |
original digraph structure and digraph operations when methods of the |
86 | 86 |
reversed oriented graph are called. This means that the adaptor have |
87 | 87 |
minor memory usage, and do not perform sophisticated algorithmic |
88 | 88 |
actions. The purpose of it is to give a tool for the cases when a |
89 | 89 |
graph have to be used in a specific alteration. If this alteration is |
90 | 90 |
obtained by a usual construction like filtering the arc-set or |
91 | 91 |
considering a new orientation, then an adaptor is worthwhile to use. |
92 | 92 |
To come back to the reverse oriented graph, in this situation |
93 | 93 |
\code |
94 | 94 |
template<typename Digraph> class ReverseDigraph; |
95 | 95 |
\endcode |
96 | 96 |
template class can be used. The code looks as follows |
97 | 97 |
\code |
98 | 98 |
ListDigraph g; |
99 | 99 |
ReverseDigraph<ListGraph> rg(g); |
100 | 100 |
int result = algorithm(rg); |
101 | 101 |
\endcode |
102 | 102 |
After running the algorithm, the original graph \c g is untouched. |
103 | 103 |
This techniques gives rise to an elegant code, and based on stable |
104 | 104 |
graph adaptors, complex algorithms can be implemented easily. |
105 | 105 |
|
106 | 106 |
In flow, circulation and bipartite matching problems, the residual |
107 | 107 |
graph is of particular importance. Combining an adaptor implementing |
108 | 108 |
this, shortest path algorithms and minimum mean cycle algorithms, |
109 | 109 |
a range of weighted and cardinality optimization algorithms can be |
110 | 110 |
obtained. For other examples, the interested user is referred to the |
111 | 111 |
detailed documentation of particular adaptors. |
112 | 112 |
|
113 | 113 |
The behavior of graph adaptors can be very different. Some of them keep |
114 | 114 |
capabilities of the original graph while in other cases this would be |
115 | 115 |
meaningless. This means that the concepts that they are models of depend |
116 | 116 |
on the graph adaptor, and the wrapped graph(s). |
117 | 117 |
If an arc of \c rg is deleted, this is carried out by deleting the |
118 | 118 |
corresponding arc of \c g, thus the adaptor modifies the original graph. |
119 | 119 |
|
120 | 120 |
But for a residual graph, this operation has no sense. |
121 | 121 |
Let us stand one more example here to simplify your work. |
122 | 122 |
RevGraphAdaptor has constructor |
123 | 123 |
\code |
124 | 124 |
ReverseDigraph(Digraph& digraph); |
125 | 125 |
\endcode |
126 | 126 |
This means that in a situation, when a <tt>const ListDigraph&</tt> |
127 | 127 |
reference to a graph is given, then it have to be instantiated with |
128 | 128 |
<tt>Digraph=const ListDigraph</tt>. |
129 | 129 |
\code |
130 | 130 |
int algorithm1(const ListDigraph& g) { |
131 | 131 |
RevGraphAdaptor<const ListDigraph> rg(g); |
132 | 132 |
return algorithm2(rg); |
133 | 133 |
} |
134 | 134 |
\endcode |
135 | 135 |
*/ |
136 | 136 |
|
137 | 137 |
/** |
138 | 138 |
@defgroup semi_adaptors Semi-Adaptor Classes for Graphs |
139 | 139 |
@ingroup graphs |
140 | 140 |
\brief Graph types between real graphs and graph adaptors. |
141 | 141 |
|
142 | 142 |
This group describes some graph types between real graphs and graph adaptors. |
143 | 143 |
These classes wrap graphs to give new functionality as the adaptors do it. |
144 | 144 |
On the other hand they are not light-weight structures as the adaptors. |
145 | 145 |
*/ |
146 | 146 |
|
147 | 147 |
/** |
148 | 148 |
@defgroup maps Maps |
149 | 149 |
@ingroup datas |
150 | 150 |
\brief Map structures implemented in LEMON. |
151 | 151 |
|
152 | 152 |
This group describes the map structures implemented in LEMON. |
153 | 153 |
|
154 | 154 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
155 | 155 |
new maps from existing ones. |
156 | 156 |
|
157 | 157 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
158 | 158 |
*/ |
159 | 159 |
|
160 | 160 |
/** |
161 | 161 |
@defgroup graph_maps Graph Maps |
162 | 162 |
@ingroup maps |
163 | 163 |
\brief Special graph-related maps. |
164 | 164 |
|
165 | 165 |
This group describes maps that are specifically designed to assign |
166 | 166 |
values to the nodes and arcs/edges of graphs. |
167 | 167 |
|
168 | 168 |
If you are looking for the standard graph maps (\c NodeMap, \c ArcMap, |
169 | 169 |
\c EdgeMap), see the \ref graph_concepts "Graph Structure Concepts". |
170 | 170 |
*/ |
171 | 171 |
|
172 | 172 |
/** |
173 | 173 |
\defgroup map_adaptors Map Adaptors |
174 | 174 |
\ingroup maps |
175 | 175 |
\brief Tools to create new maps from existing ones |
176 | 176 |
|
177 | 177 |
This group describes map adaptors that are used to create "implicit" |
178 | 178 |
maps from other maps. |
179 | 179 |
|
180 | 180 |
Most of them are \ref concepts::ReadMap "read-only maps". |
181 | 181 |
They can make arithmetic and logical operations between one or two maps |
182 | 182 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
183 | 183 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
184 | 184 |
|
185 | 185 |
The typical usage of this classes is passing implicit maps to |
186 | 186 |
algorithms. If a function type algorithm is called then the function |
187 | 187 |
type map adaptors can be used comfortable. For example let's see the |
188 | 188 |
usage of map adaptors with the \c graphToEps() function. |
189 | 189 |
\code |
190 | 190 |
Color nodeColor(int deg) { |
191 | 191 |
if (deg >= 2) { |
192 | 192 |
return Color(0.5, 0.0, 0.5); |
193 | 193 |
} else if (deg == 1) { |
194 | 194 |
return Color(1.0, 0.5, 1.0); |
195 | 195 |
} else { |
196 | 196 |
return Color(0.0, 0.0, 0.0); |
197 | 197 |
} |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
Digraph::NodeMap<int> degree_map(graph); |
201 | 201 |
|
202 | 202 |
graphToEps(graph, "graph.eps") |
203 | 203 |
.coords(coords).scaleToA4().undirected() |
204 | 204 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
205 | 205 |
.run(); |
206 | 206 |
\endcode |
207 | 207 |
The \c functorToMap() function makes an \c int to \c Color map from the |
208 | 208 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
209 | 209 |
and the previously created map. The composed map is a proper function to |
210 | 210 |
get the color of each node. |
211 | 211 |
|
212 | 212 |
The usage with class type algorithms is little bit harder. In this |
213 | 213 |
case the function type map adaptors can not be used, because the |
214 | 214 |
function map adaptors give back temporary objects. |
215 | 215 |
\code |
216 | 216 |
Digraph graph; |
217 | 217 |
|
218 | 218 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
219 | 219 |
DoubleArcMap length(graph); |
220 | 220 |
DoubleArcMap speed(graph); |
221 | 221 |
|
222 | 222 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
223 | 223 |
TimeMap time(length, speed); |
224 | 224 |
|
225 | 225 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
226 | 226 |
dijkstra.run(source, target); |
227 | 227 |
\endcode |
228 | 228 |
We have a length map and a maximum speed map on the arcs of a digraph. |
229 | 229 |
The minimum time to pass the arc can be calculated as the division of |
230 | 230 |
the two maps which can be done implicitly with the \c DivMap template |
231 | 231 |
class. We use the implicit minimum time map as the length map of the |
232 | 232 |
\c Dijkstra algorithm. |
233 | 233 |
*/ |
234 | 234 |
|
235 | 235 |
/** |
236 | 236 |
@defgroup matrices Matrices |
237 | 237 |
@ingroup datas |
238 | 238 |
\brief Two dimensional data storages implemented in LEMON. |
239 | 239 |
|
240 | 240 |
This group describes two dimensional data storages implemented in LEMON. |
241 | 241 |
*/ |
242 | 242 |
|
243 | 243 |
/** |
244 | 244 |
@defgroup paths Path Structures |
245 | 245 |
@ingroup datas |
246 | 246 |
\brief %Path structures implemented in LEMON. |
247 | 247 |
|
248 | 248 |
This group describes the path structures implemented in LEMON. |
249 | 249 |
|
250 | 250 |
LEMON provides flexible data structures to work with paths. |
251 | 251 |
All of them have similar interfaces and they can be copied easily with |
252 | 252 |
assignment operators and copy constructors. This makes it easy and |
253 | 253 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
254 | 254 |
any kind of path structure. |
255 | 255 |
|
256 | 256 |
\sa lemon::concepts::Path |
257 | 257 |
*/ |
258 | 258 |
|
259 | 259 |
/** |
260 | 260 |
@defgroup auxdat Auxiliary Data Structures |
261 | 261 |
@ingroup datas |
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-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 | 66 |
The \c \@arcs section is very similar to the \c \@nodes section, |
67 | 67 |
it again starts with a header line describing the names of the maps, |
68 | 68 |
but the \c "label" map is not obligatory here. The following lines |
69 | 69 |
describe the arcs. The first two tokens of each line are |
70 | 70 |
the source 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 | 81 |
The \c \@edges is just a synonym of \c \@arcs. The \@arcs section can |
82 | 82 |
also store the edge set of an undirected graph. In such case there is |
83 | 83 |
a conventional method for store arc maps in the file, if two columns |
84 | 84 |
has the same caption with \c '+' and \c '-' prefix, then these columns |
85 | 85 |
can be regarded as the values of an arc map. |
86 | 86 |
|
87 | 87 |
The \c \@attributes section contains key-value pairs, each line |
88 | 88 |
consists of two tokens, an attribute name, and then an attribute |
89 | 89 |
value. The value of the attribute could be also a label value of a |
90 | 90 |
node or an edge, or even an edge label prefixed with \c '+' or \c '-', |
91 | 91 |
which regards to the forward or backward directed arc of the |
92 | 92 |
corresponding edge. |
93 | 93 |
|
94 | 94 |
\code |
95 | 95 |
@attributes |
96 | 96 |
source 1 |
97 | 97 |
target 3 |
98 | 98 |
caption "LEMON test digraph" |
99 | 99 |
\endcode |
100 | 100 |
|
101 | 101 |
The \e LGF can contain extra sections, but there is no restriction on |
102 | 102 |
the format of such sections. |
103 | 103 |
|
104 | 104 |
*/ |
105 | 105 |
} |
106 | 106 |
|
107 | 107 |
// LocalWords: whitespace whitespaces |
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-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 |
/** |
20 | 20 |
|
21 | 21 |
\page license License Terms |
22 | 22 |
|
23 | 23 |
\verbinclude LICENSE |
24 | 24 |
|
25 | 25 |
*/ |
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-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 |
/** |
20 | 20 |
\mainpage LEMON Documentation |
21 | 21 |
|
22 | 22 |
\section intro Introduction |
23 | 23 |
|
24 | 24 |
\subsection whatis What is LEMON |
25 | 25 |
|
26 | 26 |
LEMON stands for |
27 | 27 |
<b>L</b>ibrary of <b>E</b>fficient <b>M</b>odels |
28 | 28 |
and <b>O</b>ptimization in <b>N</b>etworks. |
29 | 29 |
It is a C++ template |
30 | 30 |
library aimed at combinatorial optimization tasks which |
31 | 31 |
often involve in working |
32 | 32 |
with graphs. |
33 | 33 |
|
34 | 34 |
<b> |
35 | 35 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
36 | 36 |
project. |
37 | 37 |
You are free to use it in your commercial or |
38 | 38 |
non-commercial applications under very permissive |
39 | 39 |
\ref license "license terms". |
40 | 40 |
</b> |
41 | 41 |
|
42 | 42 |
\subsection howtoread How to read the documentation |
43 | 43 |
|
44 | 44 |
If you want to get a quick start and see the most important features then |
45 | 45 |
take a look at our \ref quicktour |
46 | 46 |
"Quick Tour to LEMON" which will guide you along. |
47 | 47 |
|
48 | 48 |
If you already feel like using our library, see the page that tells you |
49 | 49 |
\ref getstart "How to start using LEMON". |
50 | 50 |
|
51 | 51 |
If you |
52 | 52 |
want to see how LEMON works, see |
53 | 53 |
some \ref demoprograms "demo programs". |
54 | 54 |
|
55 | 55 |
If you know what you are looking for then try to find it under the |
56 | 56 |
<a class="el" href="modules.html">Modules</a> |
57 | 57 |
section. |
58 | 58 |
|
59 | 59 |
If you are a user of the old (0.x) series of LEMON, please check out the |
60 | 60 |
\ref migration "Migration Guide" for the backward incompatibilities. |
61 | 61 |
*/ |
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-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 |
\page migration Migration from the 0.x Series |
23 | 23 |
|
24 | 24 |
This guide gives an in depth description on what has changed compared |
25 | 25 |
to the 0.x release series. |
26 | 26 |
|
27 | 27 |
Many of these changes adjusted automatically by the |
28 | 28 |
<tt>lemon-0.x-to-1.x.sh</tt> tool. Those requiring manual |
29 | 29 |
update are typeset <b>boldface</b>. |
30 | 30 |
|
31 | 31 |
\section migration-graph Graph Related Name Changes |
32 | 32 |
|
33 | 33 |
- \ref concepts::Digraph "Directed graphs" are called \c Digraph and |
34 | 34 |
they have <tt>Arc</tt>s (instead of <tt>Edge</tt>s), while |
35 | 35 |
\ref concepts::Graph "undirected graphs" are called \c Graph |
36 | 36 |
(instead of \c UGraph) and they have <tt>Edge</tt>s (instead of |
37 | 37 |
<tt>UEdge</tt>s). These changes reflected thoroughly everywhere in |
38 | 38 |
the library. Namely, |
39 | 39 |
- \c Graph -> \c Digraph |
40 | 40 |
- \c %ListGraph -> \c ListDigraph, \c %SmartGraph -> \c SmartDigraph etc. |
41 | 41 |
- \c UGraph -> \c Graph |
42 | 42 |
- \c ListUGraph -> \c ListGraph, \c SmartUGraph -> \c SmartGraph etc. |
43 | 43 |
- \c Edge -> \c Arc, \c UEdge -> \c Edge |
44 | 44 |
- \c EdgeMap -> \c ArcMap, \c UEdgeMap -> \c EdgeMap |
45 | 45 |
- \c EdgeIt -> \c ArcIt, \c UEdgeIt -> \c EdgeIt |
46 | 46 |
- Class names and function names containing the words \c graph, |
47 | 47 |
\c ugraph, \e edge or \e arc should also be updated. |
48 | 48 |
- <b>The two endpoints of an (\e undirected) \c Edge can be obtained by the |
49 | 49 |
<tt>u()</tt> and <tt>v()</tt> member function of the graph |
50 | 50 |
(instead of <tt>source()</tt> and <tt>target()</tt>). This change |
51 | 51 |
must be done by hand.</b> |
52 | 52 |
\n Of course, you can still use <tt>source()</tt> and <tt>target()</tt> |
53 | 53 |
for <tt>Arc</tt>s (directed edges). |
54 | 54 |
|
55 | 55 |
\warning |
56 | 56 |
<b>The <tt>lemon-0.x-to-1.x.sh</tt> script replaces the words \c graph, |
57 | 57 |
\c ugraph, \c edge and \c uedge in your own identifiers and in |
58 | 58 |
strings, comments etc. as well as in all LEMON specific identifiers. |
59 | 59 |
So use the script carefully and make a backup copy of your source files |
60 | 60 |
before applying the script to them.</b> |
61 | 61 |
|
62 | 62 |
\section migration-lgf LGF tools |
63 | 63 |
- The \ref lgf-format "LGF file format" has changed, |
64 | 64 |
<tt>\@nodeset</tt> has changed to <tt>\@nodes</tt>, |
65 | 65 |
<tt>\@edgeset</tt> and <tt>\@uedgeset</tt> to <tt>\@arcs</tt> or |
66 | 66 |
<tt>\@edges</tt>, which become completely equivalents. The |
67 | 67 |
<tt>\@nodes</tt>, <tt>\@edges</tt> and <tt>\@uedges</tt> sections are |
68 | 68 |
removed from the format, the content of them should be |
69 | 69 |
the part of <tt>\@attributes</tt> section. The data fields in |
70 | 70 |
the sections must follow a strict format, they must be either character |
71 | 71 |
sequences without whitespaces or quoted strings. |
72 | 72 |
- The <tt>LemonReader</tt> and <tt>LemonWriter</tt> core interfaces |
73 | 73 |
are no longer available. |
74 | 74 |
- The implementation of the general section readers and writers has changed |
75 | 75 |
they are simple functors now. Beside the old |
76 | 76 |
stream based section handling, currently line oriented section |
77 | 77 |
reading and writing are also supported. In the |
78 | 78 |
section readers the lines must be counted manually. The sections |
79 | 79 |
should be read and written with the SectionWriter and SectionReader |
80 | 80 |
classes. |
81 | 81 |
- Instead of the item readers and writers, item converters should be |
82 | 82 |
used. The converters are functors, which map the type to |
83 | 83 |
std::string or std::string to the type. The converters for standard |
84 | 84 |
containers hasn't yet been implemented in the new LEMON. The converters |
85 | 85 |
can return strings in any format, because if it is necessary, the LGF |
86 | 86 |
writer and reader will quote and unquote the given value. |
87 | 87 |
- The DigraphReader and DigraphWriter can used similarly to the |
88 | 88 |
0.x series, however the <tt>read</tt> or <tt>write</tt> prefix of |
89 | 89 |
the member functions are removed. |
90 | 90 |
- The new LEMON supports the function like interface, the \c |
91 | 91 |
digraphReader and \c digraphWriter functions are more convenient than |
92 | 92 |
using the classes directly. |
93 | 93 |
|
94 | 94 |
\section migration-search BFS, DFS and Dijkstra |
95 | 95 |
- <b>Using the function interface of BFS, DFS and %Dijkstra both source and |
96 | 96 |
target nodes can be given as parameters of the <tt>run()</tt> function |
97 | 97 |
(instead of \c bfs(), \c dfs() or \c dijkstra() itself).</b> |
98 | 98 |
- \ref named-templ-param "Named class template parameters" of \c Bfs, |
99 | 99 |
\c Dfs, \c Dijkstra, \c BfsVisit, \c DfsVisit are renamed to start |
100 | 100 |
with "Set" instead of "Def". Namely, |
101 | 101 |
- \c DefPredMap -> \c SetPredMap |
102 | 102 |
- \c DefDistMap -> \c SetDistMap |
103 | 103 |
- \c DefReachedMap -> \c SetReachedMap |
104 | 104 |
- \c DefProcessedMap -> \c SetProcessedMap |
105 | 105 |
- \c DefHeap -> \c SetHeap |
106 | 106 |
- \c DefStandardHeap -> \c SetStandardHeap |
107 | 107 |
- \c DefOperationTraits -> \c SetOperationTraits |
108 | 108 |
- \c DefProcessedMapToBeDefaultMap -> \c SetStandardProcessedMap |
109 | 109 |
|
110 | 110 |
\section migration-error Exceptions and Debug tools |
111 | 111 |
|
112 | 112 |
<b>The class hierarchy of exceptions has largely been simplified. Now, |
113 | 113 |
only the i/o related tools may throw exceptions. All other exceptions |
114 | 114 |
have been replaced with either the \c LEMON_ASSERT or the \c LEMON_DEBUG |
115 | 115 |
macros.</b> |
116 | 116 |
|
117 | 117 |
<b>On the other hand, the parameter order of constructors of the |
118 | 118 |
exceptions has been changed. See \ref IoError and \ref FormatError for |
119 | 119 |
more details.</b> |
120 | 120 |
|
121 | 121 |
\section migration-other Others |
122 | 122 |
- <b>The contents of <tt>graph_utils.h</tt> are moved to <tt>core.h</tt> |
123 | 123 |
and <tt>maps.h</tt>. <tt>core.h</tt> is included by all graph types, |
124 | 124 |
therefore it usually do not have to be included directly.</b> |
125 | 125 |
- <b><tt>path_utils.h</tt> is merged to \c path.h.</b> |
126 | 126 |
- <b>The semantic of the assignment operations and copy constructors of maps |
127 | 127 |
are still under discussion. So, you must copy them by hand (i.e. copy |
128 | 128 |
each entry one-by-one)</b> |
129 | 129 |
- <b>The parameters of the graph copying tools (i.e. \c GraphCopy, |
130 | 130 |
\c DigraphCopy) have to be given in the from-to order.</b> |
131 | 131 |
- \c copyDigraph() and \c copyGraph() are renamed to \c digraphCopy() |
132 | 132 |
and \c graphCopy(), respectively. |
133 | 133 |
- <b>The interface of \ref DynArcLookUp has changed. It is now the same as |
134 | 134 |
of \ref ArcLookUp and \ref AllArcLookUp</b> |
135 | 135 |
- Some map types should also been renamed. Namely, |
136 | 136 |
- \c IntegerMap -> \c RangeMap |
137 | 137 |
- \c StdMap -> \c SparseMap |
138 | 138 |
- \c FunctorMap -> \c FunctorToMap |
139 | 139 |
- \c MapFunctor -> \c MapToFunctor |
140 | 140 |
- \c ForkWriteMap -> \c ForkMap |
141 | 141 |
- \c StoreBoolMap -> \c LoggerBoolMap |
142 | 142 |
- \c dim2::BoundingBox -> \c dim2::Box |
143 | 143 |
|
144 | 144 |
*/ |
145 | 145 |
} |
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-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 |
/*! |
20 | 20 |
|
21 | 21 |
\page named-param Named Parameters |
22 | 22 |
|
23 | 23 |
\section named-func-param Named Function Parameters |
24 | 24 |
|
25 | 25 |
Several modern languages provide a convenient way to refer the |
26 | 26 |
function parameters by name also when you call the function. It is |
27 | 27 |
especially comfortable in case of a function having tons of parameters |
28 | 28 |
with natural default values. Sadly, C++ lack this amenity. |
29 | 29 |
|
30 | 30 |
However, with a crafty trick and with some little |
31 | 31 |
inconvenience, it is possible to emulate is. |
32 | 32 |
The example below shows how to do it. |
33 | 33 |
|
34 | 34 |
\code |
35 | 35 |
class namedFn |
36 | 36 |
{ |
37 | 37 |
int _id; |
38 | 38 |
double _val; |
39 | 39 |
int _dim; |
40 | 40 |
|
41 | 41 |
public: |
42 | 42 |
namedFn() : _id(0), _val(1), _dim(2) {} |
43 | 43 |
namedFn& id(int p) { _id = p ; return *this; } |
44 | 44 |
namedFn& val(double p) { _val = p ; return *this; } |
45 | 45 |
namedFn& dim(int p) { _dim = p ; return *this; } |
46 | 46 |
|
47 | 47 |
run() { |
48 | 48 |
std::cout << "Here comes the function itself\n" << |
49 | 49 |
<< "With parameters " |
50 | 50 |
<< _id << ", " << _val << ", " << _dim << std::endl; |
51 | 51 |
} |
52 | 52 |
}; |
53 | 53 |
\endcode |
54 | 54 |
|
55 | 55 |
Then you can use it like this. |
56 | 56 |
|
57 | 57 |
\code |
58 | 58 |
namedFn().id(3).val(2).run(); |
59 | 59 |
\endcode |
60 | 60 |
|
61 | 61 |
The trick is obvious, each "named parameter" changes one component of |
62 | 62 |
the underlying class, then gives back a reference to it. Finally, |
63 | 63 |
<tt>run()</tt> executes the algorithm itself. |
64 | 64 |
|
65 | 65 |
\note Although it is a class, namedFn is used pretty much like as it were |
66 | 66 |
a function. That it why we called it namedFn instead of \c NamedFn. |
67 | 67 |
|
68 | 68 |
\note In fact, the final <tt>.run()</tt> could be made unnecessary, |
69 | 69 |
because the algorithm could also be implemented in the destructor of |
70 | 70 |
\c namedFn instead. This however would make it impossible to implement |
71 | 71 |
functions with return values, and would also cause serious problems when |
72 | 72 |
implementing \ref named-templ-func-param "named template parameters". |
73 | 73 |
<b>Therefore, by convention, <tt>.run()</tt> must be used |
74 | 74 |
explicitly to execute a function having named parameters |
75 | 75 |
everywhere in LEMON.</b> |
76 | 76 |
|
77 | 77 |
\section named-templ-func-param Named Function Template Parameters |
78 | 78 |
|
79 | 79 |
A named parameter can also be a template function. The usage is |
80 | 80 |
exactly the same, but the implementation behind is a kind of black |
81 | 81 |
magic and they are the dirtiest part of the LEMON code. |
82 | 82 |
|
83 | 83 |
You will probably never need to know how it works, but if you really |
84 | 84 |
committed, have a look at \ref lemon/graph_to_eps.h for an example. |
85 | 85 |
|
86 | 86 |
\section traits-classes Traits Classes |
87 | 87 |
|
88 | 88 |
A similar game can also be played when defining classes. In this case |
89 | 89 |
the type of the class attributes can be changed. Initially we have to |
90 | 90 |
define a special class called <em>Traits Class</em> defining the |
91 | 91 |
default type of the attributes. Then the types of these attributes can |
92 | 92 |
be changed in the same way as described in the next section. |
93 | 93 |
|
94 | 94 |
See \ref lemon::DijkstraDefaultTraits for an |
95 | 95 |
example how a traits class implementation looks like. |
96 | 96 |
|
97 | 97 |
\section named-templ-param Named Class Template Parameters |
98 | 98 |
|
99 | 99 |
If we would like to change the type of an attribute in a class that |
100 | 100 |
was instantiated by using a traits class as a template parameter, and |
101 | 101 |
the class contains named parameters, we do not have to instantiate again |
102 | 102 |
the class with new traits class, but instead adaptor classes can |
103 | 103 |
be used as shown in the following example. |
104 | 104 |
|
105 | 105 |
\code |
106 | 106 |
Dijkstra<>::SetPredMap<NullMap<Node,Arc> >::Create |
107 | 107 |
\endcode |
108 | 108 |
|
109 | 109 |
It can also be used in conjunction with other named template |
110 | 110 |
parameters in arbitrary order. |
111 | 111 |
|
112 | 112 |
\code |
113 | 113 |
Dijkstra<>::SetDistMap<MyMap>::SetPredMap<NullMap<Node,Arc> >::Create |
114 | 114 |
\endcode |
115 | 115 |
|
116 | 116 |
The result will be an instantiated Dijkstra class, in which the |
117 | 117 |
DistMap and the PredMap is modified. |
118 | 118 |
|
119 | 119 |
*/ |
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-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 |
/// The namespace of LEMON |
20 | 20 |
|
21 | 21 |
/// The namespace of LEMON |
22 | 22 |
/// |
23 | 23 |
namespace lemon { |
24 | 24 |
|
25 | 25 |
/// The namespace of LEMON concepts and concept checking classes |
26 | 26 |
|
27 | 27 |
/// The namespace of LEMON concepts and concept checking classes |
28 | 28 |
/// |
29 | 29 |
namespace concepts {} |
30 | 30 |
} |
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-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_TEMPLATE_H |
20 | 20 |
#define LEMON_TEMPLATE_H |
21 | 21 |
|
22 | 22 |
#endif // LEMON_TEMPLATE_H |
1 | 1 |
EXTRA_DIST += \ |
2 | 2 |
lemon/lemon.pc.in \ |
3 | 3 |
lemon/CMakeLists.txt |
4 | 4 |
|
5 | 5 |
pkgconfig_DATA += lemon/lemon.pc |
6 | 6 |
|
7 | 7 |
lib_LTLIBRARIES += lemon/libemon.la |
8 | 8 |
|
9 | 9 |
lemon_libemon_la_SOURCES = \ |
10 |
lemon/arg_parser.cc \ |
|
11 |
lemon/base.cc \ |
|
12 |
lemon/color.cc \ |
|
13 |
lemon/random.cc |
|
10 |
lemon/arg_parser.cc \ |
|
11 |
lemon/base.cc \ |
|
12 |
lemon/color.cc \ |
|
13 |
lemon/random.cc |
|
14 | 14 |
|
15 | 15 |
#lemon_libemon_la_CXXFLAGS = $(GLPK_CFLAGS) $(CPLEX_CFLAGS) $(SOPLEX_CXXFLAGS) $(AM_CXXFLAGS) |
16 | 16 |
#lemon_libemon_la_LDFLAGS = $(GLPK_LIBS) $(CPLEX_LIBS) $(SOPLEX_LIBS) |
17 | 17 |
|
18 | 18 |
lemon_HEADERS += \ |
19 | 19 |
lemon/adaptors.h \ |
20 |
|
|
20 |
lemon/arg_parser.h \ |
|
21 | 21 |
lemon/assert.h \ |
22 |
lemon/bfs.h \ |
|
23 |
lemon/bin_heap.h \ |
|
24 |
lemon/circulation.h \ |
|
25 |
lemon/color.h \ |
|
22 |
lemon/bfs.h \ |
|
23 |
lemon/bin_heap.h \ |
|
24 |
lemon/circulation.h \ |
|
25 |
lemon/color.h \ |
|
26 | 26 |
lemon/concept_check.h \ |
27 |
|
|
27 |
lemon/counter.h \ |
|
28 | 28 |
lemon/core.h \ |
29 |
lemon/dfs.h \ |
|
30 |
lemon/dijkstra.h \ |
|
31 |
lemon/dim2.h \ |
|
32 |
lemon/dimacs.h \ |
|
29 |
lemon/dfs.h \ |
|
30 |
lemon/dijkstra.h \ |
|
31 |
lemon/dim2.h \ |
|
32 |
lemon/dimacs.h \ |
|
33 | 33 |
lemon/elevator.h \ |
34 | 34 |
lemon/error.h \ |
35 | 35 |
lemon/full_graph.h \ |
36 |
lemon/graph_to_eps.h \ |
|
37 |
lemon/grid_graph.h \ |
|
36 |
lemon/graph_to_eps.h \ |
|
37 |
lemon/grid_graph.h \ |
|
38 | 38 |
lemon/hypercube_graph.h \ |
39 | 39 |
lemon/kruskal.h \ |
40 | 40 |
lemon/hao_orlin.h \ |
41 | 41 |
lemon/lgf_reader.h \ |
42 | 42 |
lemon/lgf_writer.h \ |
43 | 43 |
lemon/list_graph.h \ |
44 | 44 |
lemon/maps.h \ |
45 | 45 |
lemon/math.h \ |
46 | 46 |
lemon/max_matching.h \ |
47 | 47 |
lemon/nauty_reader.h \ |
48 | 48 |
lemon/path.h \ |
49 | 49 |
lemon/preflow.h \ |
50 |
|
|
50 |
lemon/random.h \ |
|
51 | 51 |
lemon/smart_graph.h \ |
52 | 52 |
lemon/suurballe.h \ |
53 |
lemon/time_measure.h \ |
|
54 |
lemon/tolerance.h \ |
|
53 |
lemon/time_measure.h \ |
|
54 |
lemon/tolerance.h \ |
|
55 | 55 |
lemon/unionfind.h |
56 | 56 |
|
57 | 57 |
bits_HEADERS += \ |
58 | 58 |
lemon/bits/alteration_notifier.h \ |
59 | 59 |
lemon/bits/array_map.h \ |
60 | 60 |
lemon/bits/base_extender.h \ |
61 |
|
|
61 |
lemon/bits/bezier.h \ |
|
62 | 62 |
lemon/bits/default_map.h \ |
63 |
|
|
63 |
lemon/bits/enable_if.h \ |
|
64 | 64 |
lemon/bits/graph_adaptor_extender.h \ |
65 | 65 |
lemon/bits/graph_extender.h \ |
66 | 66 |
lemon/bits/map_extender.h \ |
67 | 67 |
lemon/bits/path_dump.h \ |
68 | 68 |
lemon/bits/traits.h \ |
69 | 69 |
lemon/bits/variant.h \ |
70 | 70 |
lemon/bits/vector_map.h |
71 | 71 |
|
72 | 72 |
concept_HEADERS += \ |
73 | 73 |
lemon/concepts/digraph.h \ |
74 | 74 |
lemon/concepts/graph.h \ |
75 | 75 |
lemon/concepts/graph_components.h \ |
76 | 76 |
lemon/concepts/heap.h \ |
77 | 77 |
lemon/concepts/maps.h \ |
78 | 78 |
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 |
* Copyright (C) 2003- |
|
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_ADAPTORS_H |
20 | 20 |
#define LEMON_ADAPTORS_H |
21 | 21 |
|
22 | 22 |
/// \ingroup graph_adaptors |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Several graph adaptors |
25 | 25 |
/// |
26 | 26 |
/// This file contains several useful adaptors for digraphs and graphs. |
27 | 27 |
|
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/maps.h> |
30 | 30 |
#include <lemon/bits/variant.h> |
31 | 31 |
|
32 | 32 |
#include <lemon/bits/graph_adaptor_extender.h> |
33 | 33 |
#include <lemon/tolerance.h> |
34 | 34 |
|
35 | 35 |
#include <algorithm> |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
template<typename _Digraph> |
40 | 40 |
class DigraphAdaptorBase { |
41 | 41 |
public: |
42 | 42 |
typedef _Digraph Digraph; |
43 | 43 |
typedef DigraphAdaptorBase Adaptor; |
44 | 44 |
typedef Digraph ParentDigraph; |
45 | 45 |
|
46 | 46 |
protected: |
47 | 47 |
Digraph* _digraph; |
48 | 48 |
DigraphAdaptorBase() : _digraph(0) { } |
49 | 49 |
void setDigraph(Digraph& digraph) { _digraph = &digraph; } |
50 | 50 |
|
51 | 51 |
public: |
52 | 52 |
DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { } |
53 | 53 |
|
54 | 54 |
typedef typename Digraph::Node Node; |
55 | 55 |
typedef typename Digraph::Arc Arc; |
56 | 56 |
|
57 | 57 |
void first(Node& i) const { _digraph->first(i); } |
58 | 58 |
void first(Arc& i) const { _digraph->first(i); } |
59 | 59 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } |
60 | 60 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } |
61 | 61 |
|
62 | 62 |
void next(Node& i) const { _digraph->next(i); } |
63 | 63 |
void next(Arc& i) const { _digraph->next(i); } |
64 | 64 |
void nextIn(Arc& i) const { _digraph->nextIn(i); } |
65 | 65 |
void nextOut(Arc& i) const { _digraph->nextOut(i); } |
66 | 66 |
|
67 | 67 |
Node source(const Arc& a) const { return _digraph->source(a); } |
68 | 68 |
Node target(const Arc& a) const { return _digraph->target(a); } |
69 | 69 |
|
70 | 70 |
typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
71 | 71 |
int nodeNum() const { return _digraph->nodeNum(); } |
72 | 72 |
|
73 | 73 |
typedef EdgeNumTagIndicator<Digraph> EdgeNumTag; |
74 | 74 |
int arcNum() const { return _digraph->arcNum(); } |
75 | 75 |
|
76 | 76 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
77 | 77 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { |
78 | 78 |
return _digraph->findArc(u, v, prev); |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
Node addNode() { return _digraph->addNode(); } |
82 | 82 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } |
83 | 83 |
|
84 | 84 |
void erase(const Node& n) const { _digraph->erase(n); } |
85 | 85 |
void erase(const Arc& a) const { _digraph->erase(a); } |
86 | 86 |
|
87 | 87 |
void clear() const { _digraph->clear(); } |
88 | 88 |
|
89 | 89 |
int id(const Node& n) const { return _digraph->id(n); } |
90 | 90 |
int id(const Arc& a) const { return _digraph->id(a); } |
91 | 91 |
|
92 | 92 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
93 | 93 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } |
94 | 94 |
|
95 | 95 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
96 | 96 |
int maxArcId() const { return _digraph->maxArcId(); } |
97 | 97 |
|
98 | 98 |
typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier; |
99 | 99 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
100 | 100 |
|
101 | 101 |
typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier; |
102 | 102 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } |
103 | 103 |
|
104 | 104 |
template <typename _Value> |
105 | 105 |
class NodeMap : public Digraph::template NodeMap<_Value> { |
106 | 106 |
public: |
107 | 107 |
|
108 | 108 |
typedef typename Digraph::template NodeMap<_Value> Parent; |
109 | 109 |
|
110 | 110 |
explicit NodeMap(const Adaptor& adaptor) |
111 | 111 |
: Parent(*adaptor._digraph) {} |
112 | 112 |
|
113 | 113 |
NodeMap(const Adaptor& adaptor, const _Value& value) |
114 | 114 |
: Parent(*adaptor._digraph, value) { } |
115 | 115 |
|
116 | 116 |
private: |
117 | 117 |
NodeMap& operator=(const NodeMap& cmap) { |
118 | 118 |
return operator=<NodeMap>(cmap); |
119 | 119 |
} |
120 | 120 |
|
121 | 121 |
template <typename CMap> |
122 | 122 |
NodeMap& operator=(const CMap& cmap) { |
123 | 123 |
Parent::operator=(cmap); |
124 | 124 |
return *this; |
125 | 125 |
} |
126 | 126 |
|
127 | 127 |
}; |
128 | 128 |
|
129 | 129 |
template <typename _Value> |
130 | 130 |
class ArcMap : public Digraph::template ArcMap<_Value> { |
131 | 131 |
public: |
132 | 132 |
|
133 | 133 |
typedef typename Digraph::template ArcMap<_Value> Parent; |
134 | 134 |
|
135 | 135 |
explicit ArcMap(const Adaptor& adaptor) |
136 | 136 |
: Parent(*adaptor._digraph) {} |
137 | 137 |
|
138 | 138 |
ArcMap(const Adaptor& adaptor, const _Value& value) |
139 | 139 |
: Parent(*adaptor._digraph, value) {} |
140 | 140 |
|
141 | 141 |
private: |
142 | 142 |
ArcMap& operator=(const ArcMap& cmap) { |
143 | 143 |
return operator=<ArcMap>(cmap); |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
template <typename CMap> |
147 | 147 |
ArcMap& operator=(const CMap& cmap) { |
148 | 148 |
Parent::operator=(cmap); |
149 | 149 |
return *this; |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
}; |
153 | 153 |
|
154 | 154 |
}; |
155 | 155 |
|
156 | 156 |
template<typename _Graph> |
157 | 157 |
class GraphAdaptorBase { |
158 | 158 |
public: |
159 | 159 |
typedef _Graph Graph; |
160 | 160 |
typedef Graph ParentGraph; |
161 | 161 |
|
162 | 162 |
protected: |
163 | 163 |
Graph* _graph; |
164 | 164 |
|
165 | 165 |
GraphAdaptorBase() : _graph(0) {} |
166 | 166 |
|
167 | 167 |
void setGraph(Graph& graph) { _graph = &graph; } |
168 | 168 |
|
169 | 169 |
public: |
170 | 170 |
GraphAdaptorBase(Graph& graph) : _graph(&graph) {} |
171 | 171 |
|
172 | 172 |
typedef typename Graph::Node Node; |
173 | 173 |
typedef typename Graph::Arc Arc; |
174 | 174 |
typedef typename Graph::Edge Edge; |
175 | 175 |
|
176 | 176 |
void first(Node& i) const { _graph->first(i); } |
177 | 177 |
void first(Arc& i) const { _graph->first(i); } |
178 | 178 |
void first(Edge& i) const { _graph->first(i); } |
179 | 179 |
void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); } |
180 | 180 |
void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); } |
181 | 181 |
void firstInc(Edge &i, bool &d, const Node &n) const { |
182 | 182 |
_graph->firstInc(i, d, n); |
183 | 183 |
} |
184 | 184 |
|
185 | 185 |
void next(Node& i) const { _graph->next(i); } |
186 | 186 |
void next(Arc& i) const { _graph->next(i); } |
187 | 187 |
void next(Edge& i) const { _graph->next(i); } |
188 | 188 |
void nextIn(Arc& i) const { _graph->nextIn(i); } |
189 | 189 |
void nextOut(Arc& i) const { _graph->nextOut(i); } |
190 | 190 |
void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); } |
191 | 191 |
|
192 | 192 |
Node u(const Edge& e) const { return _graph->u(e); } |
193 | 193 |
Node v(const Edge& e) const { return _graph->v(e); } |
194 | 194 |
|
195 | 195 |
Node source(const Arc& a) const { return _graph->source(a); } |
196 | 196 |
Node target(const Arc& a) const { return _graph->target(a); } |
197 | 197 |
|
198 | 198 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
199 | 199 |
int nodeNum() const { return _graph->nodeNum(); } |
200 | 200 |
|
201 | 201 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
202 | 202 |
int arcNum() const { return _graph->arcNum(); } |
203 | 203 |
int edgeNum() const { return _graph->edgeNum(); } |
204 | 204 |
|
205 | 205 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
206 | 206 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { |
207 | 207 |
return _graph->findArc(u, v, prev); |
208 | 208 |
} |
209 | 209 |
Edge findEdge(const Node& u, const Node& v, const Edge& prev = INVALID) { |
210 | 210 |
return _graph->findEdge(u, v, prev); |
211 | 211 |
} |
212 | 212 |
|
213 | 213 |
Node addNode() { return _graph->addNode(); } |
214 | 214 |
Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); } |
215 | 215 |
|
216 | 216 |
void erase(const Node& i) { _graph->erase(i); } |
217 | 217 |
void erase(const Edge& i) { _graph->erase(i); } |
218 | 218 |
|
219 | 219 |
void clear() { _graph->clear(); } |
220 | 220 |
|
221 | 221 |
bool direction(const Arc& a) const { return _graph->direction(a); } |
222 | 222 |
Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); } |
223 | 223 |
|
224 | 224 |
int id(const Node& v) const { return _graph->id(v); } |
225 | 225 |
int id(const Arc& a) const { return _graph->id(a); } |
226 | 226 |
int id(const Edge& e) const { return _graph->id(e); } |
227 | 227 |
|
228 | 228 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } |
229 | 229 |
Arc arcFromId(int ix) const { return _graph->arcFromId(ix); } |
230 | 230 |
Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); } |
231 | 231 |
|
232 | 232 |
int maxNodeId() const { return _graph->maxNodeId(); } |
233 | 233 |
int maxArcId() const { return _graph->maxArcId(); } |
234 | 234 |
int maxEdgeId() const { return _graph->maxEdgeId(); } |
235 | 235 |
|
236 | 236 |
typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier; |
237 | 237 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
238 | 238 |
|
239 | 239 |
typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier; |
240 | 240 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
241 | 241 |
|
242 | 242 |
typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier; |
243 | 243 |
EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); } |
244 | 244 |
|
245 | 245 |
template <typename _Value> |
246 | 246 |
class NodeMap : public Graph::template NodeMap<_Value> { |
247 | 247 |
public: |
248 | 248 |
typedef typename Graph::template NodeMap<_Value> Parent; |
249 | 249 |
explicit NodeMap(const GraphAdaptorBase<Graph>& adapter) |
250 | 250 |
: Parent(*adapter._graph) {} |
251 | 251 |
NodeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
252 | 252 |
: Parent(*adapter._graph, value) {} |
253 | 253 |
|
254 | 254 |
private: |
255 | 255 |
NodeMap& operator=(const NodeMap& cmap) { |
256 | 256 |
return operator=<NodeMap>(cmap); |
257 | 257 |
} |
258 | 258 |
|
259 | 259 |
template <typename CMap> |
260 | 260 |
NodeMap& operator=(const CMap& cmap) { |
261 | 261 |
Parent::operator=(cmap); |
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-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/arg_parser.h> |
20 | 20 |
|
21 | 21 |
namespace lemon { |
22 | 22 |
|
23 | 23 |
void ArgParser::_showHelp(void *p) |
24 | 24 |
{ |
25 | 25 |
(static_cast<ArgParser*>(p))->showHelp(); |
26 | 26 |
exit(1); |
27 | 27 |
} |
28 | 28 |
|
29 | 29 |
ArgParser::ArgParser(int argc, const char * const *argv) |
30 | 30 |
:_argc(argc), _argv(argv), _command_name(argv[0]) { |
31 | 31 |
funcOption("-help","Print a short help message",_showHelp,this); |
32 | 32 |
synonym("help","-help"); |
33 | 33 |
synonym("h","-help"); |
34 | 34 |
} |
35 | 35 |
|
36 | 36 |
ArgParser::~ArgParser() |
37 | 37 |
{ |
38 | 38 |
for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) |
39 | 39 |
if(i->second.self_delete) |
40 | 40 |
switch(i->second.type) { |
41 | 41 |
case BOOL: |
42 | 42 |
delete i->second.bool_p; |
43 | 43 |
break; |
44 | 44 |
case STRING: |
45 | 45 |
delete i->second.string_p; |
46 | 46 |
break; |
47 | 47 |
case DOUBLE: |
48 | 48 |
delete i->second.double_p; |
49 | 49 |
break; |
50 | 50 |
case INTEGER: |
51 | 51 |
delete i->second.int_p; |
52 | 52 |
break; |
53 | 53 |
case UNKNOWN: |
54 | 54 |
break; |
55 | 55 |
case FUNC: |
56 | 56 |
break; |
57 | 57 |
} |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
|
61 | 61 |
ArgParser &ArgParser::intOption(const std::string &name, |
62 | 62 |
const std::string &help, |
63 | 63 |
int value, bool obl) |
64 | 64 |
{ |
65 | 65 |
ParData p; |
66 | 66 |
p.int_p=new int(value); |
67 | 67 |
p.self_delete=true; |
68 | 68 |
p.help=help; |
69 | 69 |
p.type=INTEGER; |
70 | 70 |
p.mandatory=obl; |
71 | 71 |
_opts[name]=p; |
72 | 72 |
return *this; |
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
ArgParser &ArgParser::doubleOption(const std::string &name, |
76 | 76 |
const std::string &help, |
77 | 77 |
double value, bool obl) |
78 | 78 |
{ |
79 | 79 |
ParData p; |
80 | 80 |
p.double_p=new double(value); |
81 | 81 |
p.self_delete=true; |
82 | 82 |
p.help=help; |
83 | 83 |
p.type=DOUBLE; |
84 | 84 |
p.mandatory=obl; |
85 | 85 |
_opts[name]=p; |
86 | 86 |
return *this; |
87 | 87 |
} |
88 | 88 |
|
89 | 89 |
ArgParser &ArgParser::boolOption(const std::string &name, |
90 | 90 |
const std::string &help, |
91 | 91 |
bool value, bool obl) |
92 | 92 |
{ |
93 | 93 |
ParData p; |
94 | 94 |
p.bool_p=new bool(value); |
95 | 95 |
p.self_delete=true; |
96 | 96 |
p.help=help; |
97 | 97 |
p.type=BOOL; |
98 | 98 |
p.mandatory=obl; |
99 | 99 |
_opts[name]=p; |
100 | 100 |
return *this; |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
ArgParser &ArgParser::stringOption(const std::string &name, |
104 | 104 |
const std::string &help, |
105 | 105 |
std::string value, bool obl) |
106 | 106 |
{ |
107 | 107 |
ParData p; |
108 | 108 |
p.string_p=new std::string(value); |
109 | 109 |
p.self_delete=true; |
110 | 110 |
p.help=help; |
111 | 111 |
p.type=STRING; |
112 | 112 |
p.mandatory=obl; |
113 | 113 |
_opts[name]=p; |
114 | 114 |
return *this; |
115 | 115 |
} |
116 | 116 |
|
117 | 117 |
ArgParser &ArgParser::refOption(const std::string &name, |
118 | 118 |
const std::string &help, |
119 | 119 |
int &ref, bool obl) |
120 | 120 |
{ |
121 | 121 |
ParData p; |
122 | 122 |
p.int_p=&ref; |
123 | 123 |
p.self_delete=false; |
124 | 124 |
p.help=help; |
125 | 125 |
p.type=INTEGER; |
126 | 126 |
p.mandatory=obl; |
127 | 127 |
_opts[name]=p; |
128 | 128 |
return *this; |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
ArgParser &ArgParser::refOption(const std::string &name, |
132 | 132 |
const std::string &help, |
133 | 133 |
double &ref, bool obl) |
134 | 134 |
{ |
135 | 135 |
ParData p; |
136 | 136 |
p.double_p=&ref; |
137 | 137 |
p.self_delete=false; |
138 | 138 |
p.help=help; |
139 | 139 |
p.type=DOUBLE; |
140 | 140 |
p.mandatory=obl; |
141 | 141 |
_opts[name]=p; |
142 | 142 |
return *this; |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
ArgParser &ArgParser::refOption(const std::string &name, |
146 | 146 |
const std::string &help, |
147 | 147 |
bool &ref, bool obl) |
148 | 148 |
{ |
149 | 149 |
ParData p; |
150 | 150 |
p.bool_p=&ref; |
151 | 151 |
p.self_delete=false; |
152 | 152 |
p.help=help; |
153 | 153 |
p.type=BOOL; |
154 | 154 |
p.mandatory=obl; |
155 | 155 |
_opts[name]=p; |
156 | 156 |
|
157 | 157 |
ref = false; |
158 | 158 |
|
159 | 159 |
return *this; |
160 | 160 |
} |
161 | 161 |
|
162 | 162 |
ArgParser &ArgParser::refOption(const std::string &name, |
163 | 163 |
const std::string &help, |
164 | 164 |
std::string &ref, bool obl) |
165 | 165 |
{ |
166 | 166 |
ParData p; |
167 | 167 |
p.string_p=&ref; |
168 | 168 |
p.self_delete=false; |
169 | 169 |
p.help=help; |
170 | 170 |
p.type=STRING; |
171 | 171 |
p.mandatory=obl; |
172 | 172 |
_opts[name]=p; |
173 | 173 |
return *this; |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
ArgParser &ArgParser::funcOption(const std::string &name, |
177 | 177 |
const std::string &help, |
178 | 178 |
void (*func)(void *),void *data) |
179 | 179 |
{ |
180 | 180 |
ParData p; |
181 | 181 |
p.func_p.p=func; |
182 | 182 |
p.func_p.data=data; |
183 | 183 |
p.self_delete=false; |
184 | 184 |
p.help=help; |
185 | 185 |
p.type=FUNC; |
186 | 186 |
p.mandatory=false; |
187 | 187 |
_opts[name]=p; |
188 | 188 |
return *this; |
189 | 189 |
} |
190 | 190 |
|
191 | 191 |
ArgParser &ArgParser::optionGroup(const std::string &group, |
192 | 192 |
const std::string &opt) |
193 | 193 |
{ |
194 | 194 |
Opts::iterator i = _opts.find(opt); |
195 | 195 |
LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'"); |
196 | 196 |
LEMON_ASSERT(!(i->second.ingroup), |
197 | 197 |
"Option already in option group: '"+opt+"'"); |
198 | 198 |
GroupData &g=_groups[group]; |
199 | 199 |
g.opts.push_back(opt); |
200 | 200 |
i->second.ingroup=true; |
201 | 201 |
return *this; |
202 | 202 |
} |
203 | 203 |
|
204 | 204 |
ArgParser &ArgParser::onlyOneGroup(const std::string &group) |
205 | 205 |
{ |
206 | 206 |
GroupData &g=_groups[group]; |
207 | 207 |
g.only_one=true; |
208 | 208 |
return *this; |
209 | 209 |
} |
210 | 210 |
|
211 | 211 |
ArgParser &ArgParser::synonym(const std::string &syn, |
212 | 212 |
const std::string &opt) |
213 | 213 |
{ |
214 | 214 |
Opts::iterator o = _opts.find(opt); |
215 | 215 |
Opts::iterator s = _opts.find(syn); |
216 | 216 |
LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'"); |
217 | 217 |
LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'"); |
218 | 218 |
ParData p; |
219 | 219 |
p.help=opt; |
220 | 220 |
p.mandatory=false; |
221 | 221 |
p.syn=true; |
222 | 222 |
_opts[syn]=p; |
223 | 223 |
o->second.has_syn=true; |
224 | 224 |
return *this; |
225 | 225 |
} |
226 | 226 |
|
227 | 227 |
ArgParser &ArgParser::mandatoryGroup(const std::string &group) |
228 | 228 |
{ |
229 | 229 |
GroupData &g=_groups[group]; |
230 | 230 |
g.mandatory=true; |
231 | 231 |
return *this; |
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
ArgParser &ArgParser::other(const std::string &name, |
235 | 235 |
const std::string &help) |
236 | 236 |
{ |
237 | 237 |
_others_help.push_back(OtherArg(name,help)); |
238 | 238 |
return *this; |
239 | 239 |
} |
240 | 240 |
|
241 | 241 |
void ArgParser::show(std::ostream &os,Opts::const_iterator i) const |
242 | 242 |
{ |
243 | 243 |
os << "-" << i->first; |
244 | 244 |
if(i->second.has_syn) |
245 | 245 |
for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j) |
246 | 246 |
if(j->second.syn&&j->second.help==i->first) |
247 | 247 |
os << "|-" << j->first; |
248 | 248 |
switch(i->second.type) { |
249 | 249 |
case STRING: |
250 | 250 |
os << " str"; |
251 | 251 |
break; |
252 | 252 |
case INTEGER: |
253 | 253 |
os << " int"; |
254 | 254 |
break; |
255 | 255 |
case DOUBLE: |
256 | 256 |
os << " num"; |
257 | 257 |
break; |
258 | 258 |
default: |
259 | 259 |
break; |
260 | 260 |
} |
261 | 261 |
} |
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-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_ARG_PARSER_H |
20 | 20 |
#define LEMON_ARG_PARSER_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <map> |
24 | 24 |
#include <list> |
25 | 25 |
#include <string> |
26 | 26 |
#include <iostream> |
27 | 27 |
#include <sstream> |
28 | 28 |
#include <algorithm> |
29 | 29 |
#include <lemon/assert.h> |
30 | 30 |
|
31 | 31 |
///\ingroup misc |
32 | 32 |
///\file |
33 | 33 |
///\brief A tool to parse command line arguments. |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
///Command line arguments parser |
38 | 38 |
|
39 | 39 |
///\ingroup misc |
40 | 40 |
///Command line arguments parser. |
41 | 41 |
/// |
42 | 42 |
///For a complete example see the \ref arg_parser_demo.cc demo file. |
43 | 43 |
class ArgParser { |
44 | 44 |
|
45 | 45 |
static void _showHelp(void *p); |
46 | 46 |
protected: |
47 | 47 |
|
48 | 48 |
int _argc; |
49 | 49 |
const char * const *_argv; |
50 | 50 |
|
51 | 51 |
enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 }; |
52 | 52 |
|
53 | 53 |
class ParData { |
54 | 54 |
public: |
55 | 55 |
union { |
56 | 56 |
bool *bool_p; |
57 | 57 |
int *int_p; |
58 | 58 |
double *double_p; |
59 | 59 |
std::string *string_p; |
60 | 60 |
struct { |
61 | 61 |
void (*p)(void *); |
62 | 62 |
void *data; |
63 | 63 |
} func_p; |
64 | 64 |
|
65 | 65 |
}; |
66 | 66 |
std::string help; |
67 | 67 |
bool mandatory; |
68 | 68 |
OptType type; |
69 | 69 |
bool set; |
70 | 70 |
bool ingroup; |
71 | 71 |
bool has_syn; |
72 | 72 |
bool syn; |
73 | 73 |
bool self_delete; |
74 | 74 |
ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false), |
75 | 75 |
has_syn(false), syn(false), self_delete(false) {} |
76 | 76 |
}; |
77 | 77 |
|
78 | 78 |
typedef std::map<std::string,ParData> Opts; |
79 | 79 |
Opts _opts; |
80 | 80 |
|
81 | 81 |
class GroupData |
82 | 82 |
{ |
83 | 83 |
public: |
84 | 84 |
typedef std::list<std::string> Opts; |
85 | 85 |
Opts opts; |
86 | 86 |
bool only_one; |
87 | 87 |
bool mandatory; |
88 | 88 |
GroupData() :only_one(false), mandatory(false) {} |
89 | 89 |
}; |
90 | 90 |
|
91 | 91 |
typedef std::map<std::string,GroupData> Groups; |
92 | 92 |
Groups _groups; |
93 | 93 |
|
94 | 94 |
struct OtherArg |
95 | 95 |
{ |
96 | 96 |
std::string name; |
97 | 97 |
std::string help; |
98 | 98 |
OtherArg(std::string n, std::string h) :name(n), help(h) {} |
99 | 99 |
|
100 | 100 |
}; |
101 | 101 |
|
102 | 102 |
std::vector<OtherArg> _others_help; |
103 | 103 |
std::vector<std::string> _file_args; |
104 | 104 |
std::string _command_name; |
105 | 105 |
|
106 | 106 |
|
107 | 107 |
private: |
108 | 108 |
//Bind a function to an option. |
109 | 109 |
|
110 | 110 |
//\param name The name of the option. The leading '-' must be omitted. |
111 | 111 |
//\param help A help string. |
112 | 112 |
//\retval func The function to be called when the option is given. It |
113 | 113 |
// must be of type "void f(void *)" |
114 | 114 |
//\param data Data to be passed to \c func |
115 | 115 |
ArgParser &funcOption(const std::string &name, |
116 | 116 |
const std::string &help, |
117 | 117 |
void (*func)(void *),void *data); |
118 | 118 |
|
119 | 119 |
public: |
120 | 120 |
|
121 | 121 |
///Constructor |
122 | 122 |
ArgParser(int argc, const char * const *argv); |
123 | 123 |
|
124 | 124 |
~ArgParser(); |
125 | 125 |
|
126 | 126 |
///\name Options |
127 | 127 |
/// |
128 | 128 |
|
129 | 129 |
///@{ |
130 | 130 |
|
131 | 131 |
///Add a new integer type option |
132 | 132 |
|
133 | 133 |
///Add a new integer type option. |
134 | 134 |
///\param name The name of the option. The leading '-' must be omitted. |
135 | 135 |
///\param help A help string. |
136 | 136 |
///\param value A default value for the option. |
137 | 137 |
///\param obl Indicate if the option is mandatory. |
138 | 138 |
ArgParser &intOption(const std::string &name, |
139 | 139 |
const std::string &help, |
140 | 140 |
int value=0, bool obl=false); |
141 | 141 |
|
142 | 142 |
///Add a new floating point type option |
143 | 143 |
|
144 | 144 |
///Add a new floating point type option. |
145 | 145 |
///\param name The name of the option. The leading '-' must be omitted. |
146 | 146 |
///\param help A help string. |
147 | 147 |
///\param value A default value for the option. |
148 | 148 |
///\param obl Indicate if the option is mandatory. |
149 | 149 |
ArgParser &doubleOption(const std::string &name, |
150 | 150 |
const std::string &help, |
151 | 151 |
double value=0, bool obl=false); |
152 | 152 |
|
153 | 153 |
///Add a new bool type option |
154 | 154 |
|
155 | 155 |
///Add a new bool type option. |
156 | 156 |
///\param name The name of the option. The leading '-' must be omitted. |
157 | 157 |
///\param help A help string. |
158 | 158 |
///\param value A default value for the option. |
159 | 159 |
///\param obl Indicate if the option is mandatory. |
160 | 160 |
///\note A mandatory bool obtion is of very little use. |
161 | 161 |
ArgParser &boolOption(const std::string &name, |
162 | 162 |
const std::string &help, |
163 | 163 |
bool value=false, bool obl=false); |
164 | 164 |
|
165 | 165 |
///Add a new string type option |
166 | 166 |
|
167 | 167 |
///Add a new string type option. |
168 | 168 |
///\param name The name of the option. The leading '-' must be omitted. |
169 | 169 |
///\param help A help string. |
170 | 170 |
///\param value A default value for the option. |
171 | 171 |
///\param obl Indicate if the option is mandatory. |
172 | 172 |
ArgParser &stringOption(const std::string &name, |
173 | 173 |
const std::string &help, |
174 | 174 |
std::string value="", bool obl=false); |
175 | 175 |
|
176 | 176 |
///Give help string for non-parsed arguments. |
177 | 177 |
|
178 | 178 |
///With this function you can give help string for non-parsed arguments. |
179 | 179 |
///The parameter \c name will be printed in the short usage line, while |
180 | 180 |
///\c help gives a more detailed description. |
181 | 181 |
ArgParser &other(const std::string &name, |
182 | 182 |
const std::string &help=""); |
183 | 183 |
|
184 | 184 |
///@} |
185 | 185 |
|
186 | 186 |
///\name Options with External Storage |
187 | 187 |
///Using this functions, the value of the option will be directly written |
188 | 188 |
///into a variable once the option appears in the command line. |
189 | 189 |
|
190 | 190 |
///@{ |
191 | 191 |
|
192 | 192 |
///Add a new integer type option with a storage reference |
193 | 193 |
|
194 | 194 |
///Add a new integer type option with a storage reference. |
195 | 195 |
///\param name The name of the option. The leading '-' must be omitted. |
196 | 196 |
///\param help A help string. |
197 | 197 |
///\param obl Indicate if the option is mandatory. |
198 | 198 |
///\retval ref The value of the argument will be written to this variable. |
199 | 199 |
ArgParser &refOption(const std::string &name, |
200 | 200 |
const std::string &help, |
201 | 201 |
int &ref, bool obl=false); |
202 | 202 |
|
203 | 203 |
///Add a new floating type option with a storage reference |
204 | 204 |
|
205 | 205 |
///Add a new floating type option with a storage reference. |
206 | 206 |
///\param name The name of the option. The leading '-' must be omitted. |
207 | 207 |
///\param help A help string. |
208 | 208 |
///\param obl Indicate if the option is mandatory. |
209 | 209 |
///\retval ref The value of the argument will be written to this variable. |
210 | 210 |
ArgParser &refOption(const std::string &name, |
211 | 211 |
const std::string &help, |
212 | 212 |
double &ref, bool obl=false); |
213 | 213 |
|
214 | 214 |
///Add a new bool type option with a storage reference |
215 | 215 |
|
216 | 216 |
///Add a new bool type option with a storage reference. |
217 | 217 |
///\param name The name of the option. The leading '-' must be omitted. |
218 | 218 |
///\param help A help string. |
219 | 219 |
///\param obl Indicate if the option is mandatory. |
220 | 220 |
///\retval ref The value of the argument will be written to this variable. |
221 | 221 |
///\note A mandatory bool obtion is of very little use. |
222 | 222 |
ArgParser &refOption(const std::string &name, |
223 | 223 |
const std::string &help, |
224 | 224 |
bool &ref, bool obl=false); |
225 | 225 |
|
226 | 226 |
///Add a new string type option with a storage reference |
227 | 227 |
|
228 | 228 |
///Add a new string type option with a storage reference. |
229 | 229 |
///\param name The name of the option. The leading '-' must be omitted. |
230 | 230 |
///\param help A help string. |
231 | 231 |
///\param obl Indicate if the option is mandatory. |
232 | 232 |
///\retval ref The value of the argument will be written to this variable. |
233 | 233 |
ArgParser &refOption(const std::string &name, |
234 | 234 |
const std::string &help, |
235 | 235 |
std::string &ref, bool obl=false); |
236 | 236 |
|
237 | 237 |
///@} |
238 | 238 |
|
239 | 239 |
///\name Option Groups and Synonyms |
240 | 240 |
/// |
241 | 241 |
|
242 | 242 |
///@{ |
243 | 243 |
|
244 | 244 |
///Bundle some options into a group |
245 | 245 |
|
246 | 246 |
/// You can group some option by calling this function repeatedly for each |
247 | 247 |
/// option to be grouped with the same groupname. |
248 | 248 |
///\param group The group name. |
249 | 249 |
///\param opt The option name. |
250 | 250 |
ArgParser &optionGroup(const std::string &group, |
251 | 251 |
const std::string &opt); |
252 | 252 |
|
253 | 253 |
///Make the members of a group exclusive |
254 | 254 |
|
255 | 255 |
///If you call this function for a group, than at most one of them can be |
256 | 256 |
///given at the same time. |
257 | 257 |
ArgParser &onlyOneGroup(const std::string &group); |
258 | 258 |
|
259 | 259 |
///Make a group mandatory |
260 | 260 |
|
261 | 261 |
///Using this function, at least one of the members of \c group |
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-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_ASSERT_H |
20 | 20 |
#define LEMON_ASSERT_H |
21 | 21 |
|
22 | 22 |
/// \ingroup exceptions |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Extended assertion handling |
25 | 25 |
|
26 | 26 |
#include <lemon/error.h> |
27 | 27 |
|
28 | 28 |
namespace lemon { |
29 | 29 |
|
30 | 30 |
inline void assert_fail_abort(const char *file, int line, |
31 | 31 |
const char *function, const char* message, |
32 | 32 |
const char *assertion) |
33 | 33 |
{ |
34 | 34 |
std::cerr << file << ":" << line << ": "; |
35 | 35 |
if (function) |
36 | 36 |
std::cerr << function << ": "; |
37 | 37 |
std::cerr << message; |
38 | 38 |
if (assertion) |
39 | 39 |
std::cerr << " (assertion '" << assertion << "' failed)"; |
40 | 40 |
std::cerr << std::endl; |
41 | 41 |
std::abort(); |
42 | 42 |
} |
43 | 43 |
|
44 | 44 |
namespace _assert_bits { |
45 | 45 |
|
46 | 46 |
|
47 | 47 |
inline const char* cstringify(const std::string& str) { |
48 | 48 |
return str.c_str(); |
49 | 49 |
} |
50 | 50 |
|
51 | 51 |
inline const char* cstringify(const char* str) { |
52 | 52 |
return str; |
53 | 53 |
} |
54 | 54 |
} |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
#endif // LEMON_ASSERT_H |
58 | 58 |
|
59 | 59 |
#undef LEMON_ASSERT |
60 | 60 |
#undef LEMON_DEBUG |
61 | 61 |
|
62 | 62 |
#if (defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
63 | 63 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) > 1 |
64 | 64 |
#error "LEMON assertion system is not set properly" |
65 | 65 |
#endif |
66 | 66 |
|
67 | 67 |
#if ((defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
68 | 68 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) == 1 || \ |
69 | 69 |
defined(LEMON_ENABLE_ASSERTS)) && \ |
70 | 70 |
(defined(LEMON_DISABLE_ASSERTS) || \ |
71 | 71 |
defined(NDEBUG)) |
72 | 72 |
#error "LEMON assertion system is not set properly" |
73 | 73 |
#endif |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
#if defined LEMON_ASSERT_ABORT |
77 | 77 |
# undef LEMON_ASSERT_HANDLER |
78 | 78 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
79 | 79 |
#elif defined LEMON_ASSERT_CUSTOM |
80 | 80 |
# undef LEMON_ASSERT_HANDLER |
81 | 81 |
# ifndef LEMON_CUSTOM_ASSERT_HANDLER |
82 | 82 |
# error "LEMON_CUSTOM_ASSERT_HANDLER is not set" |
83 | 83 |
# endif |
84 | 84 |
# define LEMON_ASSERT_HANDLER LEMON_CUSTOM_ASSERT_HANDLER |
85 | 85 |
#elif defined LEMON_DISABLE_ASSERTS |
86 | 86 |
# undef LEMON_ASSERT_HANDLER |
87 | 87 |
#elif defined NDEBUG |
88 | 88 |
# undef LEMON_ASSERT_HANDLER |
89 | 89 |
#else |
90 | 90 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
91 | 91 |
#endif |
92 | 92 |
|
93 | 93 |
#ifndef LEMON_FUNCTION_NAME |
94 | 94 |
# if defined __GNUC__ |
95 | 95 |
# define LEMON_FUNCTION_NAME (__PRETTY_FUNCTION__) |
96 | 96 |
# elif defined _MSC_VER |
97 | 97 |
# define LEMON_FUNCTION_NAME (__FUNCSIG__) |
98 | 98 |
# elif __STDC_VERSION__ >= 199901L |
99 | 99 |
# define LEMON_FUNCTION_NAME (__func__) |
100 | 100 |
# else |
101 | 101 |
# define LEMON_FUNCTION_NAME ("<unknown>") |
102 | 102 |
# endif |
103 | 103 |
#endif |
104 | 104 |
|
105 | 105 |
#ifdef DOXYGEN |
106 | 106 |
|
107 | 107 |
/// \ingroup exceptions |
108 | 108 |
/// |
109 | 109 |
/// \brief Macro for assertion with customizable message |
110 | 110 |
/// |
111 | 111 |
/// Macro for assertion with customizable message. |
112 | 112 |
/// \param exp An expression that must be convertible to \c bool. If it is \c |
113 | 113 |
/// false, then an assertion is raised. The concrete behaviour depends on the |
114 | 114 |
/// settings of the assertion system. |
115 | 115 |
/// \param msg A <tt>const char*</tt> parameter, which can be used to provide |
116 | 116 |
/// information about the circumstances of the failed assertion. |
117 | 117 |
/// |
118 | 118 |
/// The assertions are enabled in the default behaviour. |
119 | 119 |
/// You can disable them with the following code: |
120 | 120 |
/// \code |
121 | 121 |
/// #define LEMON_DISABLE_ASSERTS |
122 | 122 |
/// \endcode |
123 | 123 |
/// or with compilation parameters: |
124 | 124 |
/// \code |
125 | 125 |
/// g++ -DLEMON_DISABLE_ASSERTS |
126 | 126 |
/// make CXXFLAGS='-DLEMON_DISABLE_ASSERTS' |
127 | 127 |
/// \endcode |
128 | 128 |
/// The checking is also disabled when the standard macro \c NDEBUG is defined. |
129 | 129 |
/// |
130 | 130 |
/// As a default behaviour the failed assertion prints a short log message to |
131 | 131 |
/// the standard error and aborts the execution. |
132 | 132 |
/// |
133 | 133 |
/// However, the following modes can be used in the assertion system: |
134 | 134 |
/// - \c LEMON_ASSERT_ABORT The failed assertion prints a short log message to |
135 | 135 |
/// the standard error and aborts the program. It is the default behaviour. |
136 | 136 |
/// - \c LEMON_ASSERT_CUSTOM The user can define own assertion handler |
137 | 137 |
/// function. |
138 | 138 |
/// \code |
139 | 139 |
/// void custom_assert_handler(const char* file, int line, |
140 | 140 |
/// const char* function, const char* message, |
141 | 141 |
/// const char* assertion); |
142 | 142 |
/// \endcode |
143 | 143 |
/// The name of the function should be defined as the \c |
144 | 144 |
/// LEMON_CUSTOM_ASSERT_HANDLER macro name. |
145 | 145 |
/// \code |
146 | 146 |
/// #define LEMON_CUSTOM_ASSERT_HANDLER custom_assert_handler |
147 | 147 |
/// \endcode |
148 | 148 |
/// Whenever an assertion is occured, the custom assertion |
149 | 149 |
/// handler is called with appropiate parameters. |
150 | 150 |
/// |
151 | 151 |
/// The assertion mode can also be changed within one compilation unit. |
152 | 152 |
/// If the macros are redefined with other settings and the |
153 | 153 |
/// \ref lemon/assert.h "assert.h" file is reincluded, then the |
154 | 154 |
/// behaviour is changed appropiately to the new settings. |
155 | 155 |
# define LEMON_ASSERT(exp, msg) \ |
156 | 156 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
157 | 157 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
158 | 158 |
LEMON_FUNCTION_NAME, \ |
159 | 159 |
::lemon::_assert_bits::cstringify(msg), #exp), 0))) |
160 | 160 |
|
161 | 161 |
/// \ingroup exceptions |
162 | 162 |
/// |
163 | 163 |
/// \brief Macro for internal assertions |
164 | 164 |
/// |
165 | 165 |
/// Macro for internal assertions, it is used in the library to check |
166 | 166 |
/// the consistency of results of algorithms, several pre- and |
167 | 167 |
/// postconditions and invariants. The checking is disabled by |
168 | 168 |
/// default, but it can be turned on with the macro \c |
169 | 169 |
/// LEMON_ENABLE_DEBUG. |
170 | 170 |
/// \code |
171 | 171 |
/// #define LEMON_ENABLE_DEBUG |
172 | 172 |
/// \endcode |
173 | 173 |
/// or with compilation parameters: |
174 | 174 |
/// \code |
175 | 175 |
/// g++ -DLEMON_ENABLE_DEBUG |
176 | 176 |
/// make CXXFLAGS='-DLEMON_ENABLE_DEBUG' |
177 | 177 |
/// \endcode |
178 | 178 |
/// |
179 | 179 |
/// This macro works like the \c LEMON_ASSERT macro, therefore the |
180 | 180 |
/// current behaviour depends on the settings of \c LEMON_ASSERT |
181 | 181 |
/// macro. |
182 | 182 |
/// |
183 | 183 |
/// \see LEMON_ASSERT |
184 | 184 |
# define LEMON_DEBUG(exp, msg) \ |
185 | 185 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
186 | 186 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
187 | 187 |
LEMON_FUNCTION_NAME, \ |
188 | 188 |
::lemon::_assert_bits::cstringify(msg), #exp), 0))) |
189 | 189 |
|
190 | 190 |
#else |
191 | 191 |
|
192 | 192 |
# ifndef LEMON_ASSERT_HANDLER |
193 | 193 |
# define LEMON_ASSERT(exp, msg) (static_cast<void>(0)) |
194 | 194 |
# define LEMON_DEBUG(exp, msg) (static_cast<void>(0)) |
195 | 195 |
# else |
196 | 196 |
# define LEMON_ASSERT(exp, msg) \ |
197 | 197 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
198 | 198 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
199 | 199 |
LEMON_FUNCTION_NAME, \ |
200 | 200 |
::lemon::_assert_bits::cstringify(msg), \ |
201 | 201 |
#exp), 0))) |
202 | 202 |
# if LEMON_ENABLE_DEBUG |
203 | 203 |
# define LEMON_DEBUG(exp, msg) \ |
204 | 204 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
205 | 205 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
206 | 206 |
LEMON_FUNCTION_NAME, \ |
207 | 207 |
::lemon::_assert_bits::cstringify(msg), \ |
208 | 208 |
#exp), 0))) |
209 | 209 |
# else |
210 | 210 |
# define LEMON_DEBUG(exp, msg) (static_cast<void>(0)) |
211 | 211 |
# endif |
212 | 212 |
# endif |
213 | 213 |
|
214 | 214 |
#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 |
* Copyright (C) 2003- |
|
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 |
///\file |
20 | 20 |
///\brief Some basic non-inline functions and static global data. |
21 | 21 |
|
22 | 22 |
#include<lemon/tolerance.h> |
23 | 23 |
#include<lemon/core.h> |
24 | 24 |
namespace lemon { |
25 | 25 |
|
26 | 26 |
float Tolerance<float>::def_epsilon = 1e-4; |
27 | 27 |
double Tolerance<double>::def_epsilon = 1e-10; |
28 | 28 |
long double Tolerance<long double>::def_epsilon = 1e-14; |
29 | 29 |
|
30 | 30 |
#ifndef LEMON_ONLY_TEMPLATES |
31 | 31 |
const Invalid INVALID = Invalid(); |
32 | 32 |
#endif |
33 | 33 |
|
34 | 34 |
} //namespace lemon |
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-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_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief BFS 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 Bfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct BfsDefaultTraits |
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 shortest paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the shortest 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 PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///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 ProcessedMap. |
68 | 68 |
|
69 | 69 |
///This function instantiates a ProcessedMap. |
70 | 70 |
///\param g is the digraph, to which |
71 | 71 |
///we would like to define the 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 ReachedMap. |
87 | 87 |
|
88 | 88 |
///This function instantiates a ReachedMap. |
89 | 89 |
///\param g is the digraph, to which |
90 | 90 |
///we would like to define the 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 DistMap. |
102 | 102 |
|
103 | 103 |
///This function instantiates a DistMap. |
104 | 104 |
///\param g is the digraph, to which we would like to define the |
105 | 105 |
///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 |
///%BFS algorithm class. |
113 | 113 |
|
114 | 114 |
///\ingroup search |
115 | 115 |
///This class provides an efficient implementation of the %BFS algorithm. |
116 | 116 |
/// |
117 | 117 |
///There is also a \ref bfs() "function-type interface" for the BFS |
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=BfsDefaultTraits<GR> > |
129 | 129 |
#endif |
130 | 130 |
class Bfs { |
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 |
///shortest 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 BfsDefaultTraits "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::Node> _queue; |
178 | 178 |
int _queue_head,_queue_tail,_queue_next_dist; |
179 | 179 |
int _curr_dist; |
180 | 180 |
|
181 | 181 |
//Creates the maps if necessary. |
182 | 182 |
void create_maps() |
183 | 183 |
{ |
184 | 184 |
if(!_pred) { |
185 | 185 |
local_pred = true; |
186 | 186 |
_pred = Traits::createPredMap(*G); |
187 | 187 |
} |
188 | 188 |
if(!_dist) { |
189 | 189 |
local_dist = true; |
190 | 190 |
_dist = Traits::createDistMap(*G); |
191 | 191 |
} |
192 | 192 |
if(!_reached) { |
193 | 193 |
local_reached = true; |
194 | 194 |
_reached = Traits::createReachedMap(*G); |
195 | 195 |
} |
196 | 196 |
if(!_processed) { |
197 | 197 |
local_processed = true; |
198 | 198 |
_processed = Traits::createProcessedMap(*G); |
199 | 199 |
} |
200 | 200 |
} |
201 | 201 |
|
202 | 202 |
protected: |
203 | 203 |
|
204 | 204 |
Bfs() {} |
205 | 205 |
|
206 | 206 |
public: |
207 | 207 |
|
208 | 208 |
typedef Bfs Create; |
209 | 209 |
|
210 | 210 |
///\name Named Template Parameters |
211 | 211 |
|
212 | 212 |
///@{ |
213 | 213 |
|
214 | 214 |
template <class T> |
215 | 215 |
struct SetPredMapTraits : public Traits { |
216 | 216 |
typedef T PredMap; |
217 | 217 |
static PredMap *createPredMap(const Digraph &) |
218 | 218 |
{ |
219 | 219 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
220 | 220 |
return 0; // ignore warnings |
221 | 221 |
} |
222 | 222 |
}; |
223 | 223 |
///\brief \ref named-templ-param "Named parameter" for setting |
224 | 224 |
///PredMap type. |
225 | 225 |
/// |
226 | 226 |
///\ref named-templ-param "Named parameter" for setting |
227 | 227 |
///PredMap type. |
228 | 228 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
229 | 229 |
template <class T> |
230 | 230 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
231 | 231 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
232 | 232 |
}; |
233 | 233 |
|
234 | 234 |
template <class T> |
235 | 235 |
struct SetDistMapTraits : public Traits { |
236 | 236 |
typedef T DistMap; |
237 | 237 |
static DistMap *createDistMap(const Digraph &) |
238 | 238 |
{ |
239 | 239 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
240 | 240 |
return 0; // ignore warnings |
241 | 241 |
} |
242 | 242 |
}; |
243 | 243 |
///\brief \ref named-templ-param "Named parameter" for setting |
244 | 244 |
///DistMap type. |
245 | 245 |
/// |
246 | 246 |
///\ref named-templ-param "Named parameter" for setting |
247 | 247 |
///DistMap type. |
248 | 248 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
249 | 249 |
template <class T> |
250 | 250 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
251 | 251 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
252 | 252 |
}; |
253 | 253 |
|
254 | 254 |
template <class T> |
255 | 255 |
struct SetReachedMapTraits : public Traits { |
256 | 256 |
typedef T ReachedMap; |
257 | 257 |
static ReachedMap *createReachedMap(const Digraph &) |
258 | 258 |
{ |
259 | 259 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
260 | 260 |
return 0; // ignore warnings |
261 | 261 |
} |
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-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_BIN_HEAP_H |
20 | 20 |
#define LEMON_BIN_HEAP_H |
21 | 21 |
|
22 | 22 |
///\ingroup auxdat |
23 | 23 |
///\file |
24 | 24 |
///\brief Binary Heap implementation. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
///\ingroup auxdat |
33 | 33 |
/// |
34 | 34 |
///\brief A Binary Heap implementation. |
35 | 35 |
/// |
36 | 36 |
///This class implements the \e binary \e heap data structure. A \e heap |
37 | 37 |
///is a data structure for storing items with specified values called \e |
38 | 38 |
///priorities in such a way that finding the item with minimum priority is |
39 | 39 |
///efficient. \c Compare specifies the ordering of the priorities. In a heap |
40 | 40 |
///one can change the priority of an item, add or erase an item, etc. |
41 | 41 |
/// |
42 | 42 |
///\tparam _Prio Type of the priority of the items. |
43 | 43 |
///\tparam _ItemIntMap A read and writable Item int map, used internally |
44 | 44 |
///to handle the cross references. |
45 | 45 |
///\tparam _Compare A class for the ordering of the priorities. The |
46 | 46 |
///default is \c std::less<_Prio>. |
47 | 47 |
/// |
48 | 48 |
///\sa FibHeap |
49 | 49 |
///\sa Dijkstra |
50 | 50 |
template <typename _Prio, typename _ItemIntMap, |
51 | 51 |
typename _Compare = std::less<_Prio> > |
52 | 52 |
class BinHeap { |
53 | 53 |
|
54 | 54 |
public: |
55 | 55 |
///\e |
56 | 56 |
typedef _ItemIntMap ItemIntMap; |
57 | 57 |
///\e |
58 | 58 |
typedef _Prio Prio; |
59 | 59 |
///\e |
60 | 60 |
typedef typename ItemIntMap::Key Item; |
61 | 61 |
///\e |
62 | 62 |
typedef std::pair<Item,Prio> Pair; |
63 | 63 |
///\e |
64 | 64 |
typedef _Compare Compare; |
65 | 65 |
|
66 | 66 |
/// \brief Type to represent the items states. |
67 | 67 |
/// |
68 | 68 |
/// Each Item element have a state associated to it. It may be "in heap", |
69 | 69 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
70 | 70 |
/// heap's point of view, but may be useful to the user. |
71 | 71 |
/// |
72 | 72 |
/// The ItemIntMap \e should be initialized in such way that it maps |
73 | 73 |
/// PRE_HEAP (-1) to any element to be put in the heap... |
74 | 74 |
enum State { |
75 | 75 |
IN_HEAP = 0, |
76 | 76 |
PRE_HEAP = -1, |
77 | 77 |
POST_HEAP = -2 |
78 | 78 |
}; |
79 | 79 |
|
80 | 80 |
private: |
81 | 81 |
std::vector<Pair> data; |
82 | 82 |
Compare comp; |
83 | 83 |
ItemIntMap &iim; |
84 | 84 |
|
85 | 85 |
public: |
86 | 86 |
/// \brief The constructor. |
87 | 87 |
/// |
88 | 88 |
/// The constructor. |
89 | 89 |
/// \param _iim should be given to the constructor, since it is used |
90 | 90 |
/// internally to handle the cross references. The value of the map |
91 | 91 |
/// should be PRE_HEAP (-1) for each element. |
92 | 92 |
explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {} |
93 | 93 |
|
94 | 94 |
/// \brief The constructor. |
95 | 95 |
/// |
96 | 96 |
/// The constructor. |
97 | 97 |
/// \param _iim should be given to the constructor, since it is used |
98 | 98 |
/// internally to handle the cross references. The value of the map |
99 | 99 |
/// should be PRE_HEAP (-1) for each element. |
100 | 100 |
/// |
101 | 101 |
/// \param _comp The comparator function object. |
102 | 102 |
BinHeap(ItemIntMap &_iim, const Compare &_comp) |
103 | 103 |
: iim(_iim), comp(_comp) {} |
104 | 104 |
|
105 | 105 |
|
106 | 106 |
/// The number of items stored in the heap. |
107 | 107 |
/// |
108 | 108 |
/// \brief Returns the number of items stored in the heap. |
109 | 109 |
int size() const { return data.size(); } |
110 | 110 |
|
111 | 111 |
/// \brief Checks if the heap stores no items. |
112 | 112 |
/// |
113 | 113 |
/// Returns \c true if and only if the heap stores no items. |
114 | 114 |
bool empty() const { return data.empty(); } |
115 | 115 |
|
116 | 116 |
/// \brief Make empty this heap. |
117 | 117 |
/// |
118 | 118 |
/// Make empty this heap. It does not change the cross reference map. |
119 | 119 |
/// If you want to reuse what is not surely empty you should first clear |
120 | 120 |
/// the heap and after that you should set the cross reference map for |
121 | 121 |
/// each item to \c PRE_HEAP. |
122 | 122 |
void clear() { |
123 | 123 |
data.clear(); |
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
private: |
127 | 127 |
static int parent(int i) { return (i-1)/2; } |
128 | 128 |
|
129 | 129 |
static int second_child(int i) { return 2*i+2; } |
130 | 130 |
bool less(const Pair &p1, const Pair &p2) const { |
131 | 131 |
return comp(p1.second, p2.second); |
132 | 132 |
} |
133 | 133 |
|
134 | 134 |
int bubble_up(int hole, Pair p) { |
135 | 135 |
int par = parent(hole); |
136 | 136 |
while( hole>0 && less(p,data[par]) ) { |
137 | 137 |
move(data[par],hole); |
138 | 138 |
hole = par; |
139 | 139 |
par = parent(hole); |
140 | 140 |
} |
141 | 141 |
move(p, hole); |
142 | 142 |
return hole; |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
int bubble_down(int hole, Pair p, int length) { |
146 | 146 |
int child = second_child(hole); |
147 | 147 |
while(child < length) { |
148 | 148 |
if( less(data[child-1], data[child]) ) { |
149 | 149 |
--child; |
150 | 150 |
} |
151 | 151 |
if( !less(data[child], p) ) |
152 | 152 |
goto ok; |
153 | 153 |
move(data[child], hole); |
154 | 154 |
hole = child; |
155 | 155 |
child = second_child(hole); |
156 | 156 |
} |
157 | 157 |
child--; |
158 | 158 |
if( child<length && less(data[child], p) ) { |
159 | 159 |
move(data[child], hole); |
160 | 160 |
hole=child; |
161 | 161 |
} |
162 | 162 |
ok: |
163 | 163 |
move(p, hole); |
164 | 164 |
return hole; |
165 | 165 |
} |
166 | 166 |
|
167 | 167 |
void move(const Pair &p, int i) { |
168 | 168 |
data[i] = p; |
169 | 169 |
iim.set(p.first, i); |
170 | 170 |
} |
171 | 171 |
|
172 | 172 |
public: |
173 | 173 |
/// \brief Insert a pair of item and priority into the heap. |
174 | 174 |
/// |
175 | 175 |
/// Adds \c p.first to the heap with priority \c p.second. |
176 | 176 |
/// \param p The pair to insert. |
177 | 177 |
void push(const Pair &p) { |
178 | 178 |
int n = data.size(); |
179 | 179 |
data.resize(n+1); |
180 | 180 |
bubble_up(n, p); |
181 | 181 |
} |
182 | 182 |
|
183 | 183 |
/// \brief Insert an item into the heap with the given heap. |
184 | 184 |
/// |
185 | 185 |
/// Adds \c i to the heap with priority \c p. |
186 | 186 |
/// \param i The item to insert. |
187 | 187 |
/// \param p The priority of the item. |
188 | 188 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); } |
189 | 189 |
|
190 | 190 |
/// \brief Returns the item with minimum priority relative to \c Compare. |
191 | 191 |
/// |
192 | 192 |
/// This method returns the item with minimum priority relative to \c |
193 | 193 |
/// Compare. |
194 | 194 |
/// \pre The heap must be nonempty. |
195 | 195 |
Item top() const { |
196 | 196 |
return data[0].first; |
197 | 197 |
} |
198 | 198 |
|
199 | 199 |
/// \brief Returns the minimum priority relative to \c Compare. |
200 | 200 |
/// |
201 | 201 |
/// It returns the minimum priority relative to \c Compare. |
202 | 202 |
/// \pre The heap must be nonempty. |
203 | 203 |
Prio prio() const { |
204 | 204 |
return data[0].second; |
205 | 205 |
} |
206 | 206 |
|
207 | 207 |
/// \brief Deletes the item with minimum priority relative to \c Compare. |
208 | 208 |
/// |
209 | 209 |
/// This method deletes the item with minimum priority relative to \c |
210 | 210 |
/// Compare from the heap. |
211 | 211 |
/// \pre The heap must be non-empty. |
212 | 212 |
void pop() { |
213 | 213 |
int n = data.size()-1; |
214 | 214 |
iim.set(data[0].first, POST_HEAP); |
215 | 215 |
if (n > 0) { |
216 | 216 |
bubble_down(0, data[n], n); |
217 | 217 |
} |
218 | 218 |
data.pop_back(); |
219 | 219 |
} |
220 | 220 |
|
221 | 221 |
/// \brief Deletes \c i from the heap. |
222 | 222 |
/// |
223 | 223 |
/// This method deletes item \c i from the heap. |
224 | 224 |
/// \param i The item to erase. |
225 | 225 |
/// \pre The item should be in the heap. |
226 | 226 |
void erase(const Item &i) { |
227 | 227 |
int h = iim[i]; |
228 | 228 |
int n = data.size()-1; |
229 | 229 |
iim.set(data[h].first, POST_HEAP); |
230 | 230 |
if( h < n ) { |
231 | 231 |
if ( bubble_up(h, data[n]) == h) { |
232 | 232 |
bubble_down(h, data[n], n); |
233 | 233 |
} |
234 | 234 |
} |
235 | 235 |
data.pop_back(); |
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
|
239 | 239 |
/// \brief Returns the priority of \c i. |
240 | 240 |
/// |
241 | 241 |
/// This function returns the priority of item \c i. |
242 | 242 |
/// \pre \c i must be in the heap. |
243 | 243 |
/// \param i The item. |
244 | 244 |
Prio operator[](const Item &i) const { |
245 | 245 |
int idx = iim[i]; |
246 | 246 |
return data[idx].second; |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
/// \brief \c i gets to the heap with priority \c p independently |
250 | 250 |
/// if \c i was already there. |
251 | 251 |
/// |
252 | 252 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
253 | 253 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
254 | 254 |
/// \param i The item. |
255 | 255 |
/// \param p The priority. |
256 | 256 |
void set(const Item &i, const Prio &p) { |
257 | 257 |
int idx = iim[i]; |
258 | 258 |
if( idx < 0 ) { |
259 | 259 |
push(i,p); |
260 | 260 |
} |
261 | 261 |
else if( comp(p, data[idx].second) ) { |
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-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_ALTERATION_NOTIFIER_H |
20 | 20 |
#define LEMON_BITS_ALTERATION_NOTIFIER_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <list> |
24 | 24 |
|
25 | 25 |
#include <lemon/core.h> |
26 | 26 |
|
27 | 27 |
//\ingroup graphbits |
28 | 28 |
//\file |
29 | 29 |
//\brief Observer notifier for graph alteration observers. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
// \ingroup graphbits |
34 | 34 |
// |
35 | 35 |
// \brief Notifier class to notify observes about alterations in |
36 | 36 |
// a container. |
37 | 37 |
// |
38 | 38 |
// The simple graphs can be refered as two containers: a node container |
39 | 39 |
// and an edge container. But they do not store values directly, they |
40 | 40 |
// are just key continars for more value containers, which are the |
41 | 41 |
// node and edge maps. |
42 | 42 |
// |
43 | 43 |
// The node and edge sets of the graphs can be changed as we add or erase |
44 | 44 |
// nodes and edges in the graph. LEMON would like to handle easily |
45 | 45 |
// that the node and edge maps should contain values for all nodes or |
46 | 46 |
// edges. If we want to check on every indicing if the map contains |
47 | 47 |
// the current indicing key that cause a drawback in the performance |
48 | 48 |
// in the library. We use another solution: we notify all maps about |
49 | 49 |
// an alteration in the graph, which cause only drawback on the |
50 | 50 |
// alteration of the graph. |
51 | 51 |
// |
52 | 52 |
// This class provides an interface to a node or edge container. |
53 | 53 |
// The first() and next() member functions make possible |
54 | 54 |
// to iterate on the keys of the container. |
55 | 55 |
// The id() function returns an integer id for each key. |
56 | 56 |
// The maxId() function gives back an upper bound of the ids. |
57 | 57 |
// |
58 | 58 |
// For the proper functonality of this class, we should notify it |
59 | 59 |
// about each alteration in the container. The alterations have four type: |
60 | 60 |
// add(), erase(), build() and clear(). The add() and |
61 | 61 |
// erase() signal that only one or few items added or erased to or |
62 | 62 |
// from the graph. If all items are erased from the graph or if a new graph |
63 | 63 |
// is built from an empty graph, then it can be signaled with the |
64 | 64 |
// clear() and build() members. Important rule that if we erase items |
65 | 65 |
// from graphs we should first signal the alteration and after that erase |
66 | 66 |
// them from the container, on the other way on item addition we should |
67 | 67 |
// first extend the container and just after that signal the alteration. |
68 | 68 |
// |
69 | 69 |
// The alteration can be observed with a class inherited from the |
70 | 70 |
// ObserverBase nested class. The signals can be handled with |
71 | 71 |
// overriding the virtual functions defined in the base class. The |
72 | 72 |
// observer base can be attached to the notifier with the |
73 | 73 |
// attach() member and can be detached with detach() function. The |
74 | 74 |
// alteration handlers should not call any function which signals |
75 | 75 |
// an other alteration in the same notifier and should not |
76 | 76 |
// detach any observer from the notifier. |
77 | 77 |
// |
78 | 78 |
// Alteration observers try to be exception safe. If an add() or |
79 | 79 |
// a clear() function throws an exception then the remaining |
80 | 80 |
// observeres will not be notified and the fulfilled additions will |
81 | 81 |
// be rolled back by calling the erase() or clear() functions. |
82 | 82 |
// Hence erase() and clear() should not throw exception. |
83 | 83 |
// Actullay, they can throw only \ref ImmediateDetach exception, |
84 | 84 |
// which detach the observer from the notifier. |
85 | 85 |
// |
86 | 86 |
// There are some cases, when the alteration observing is not completly |
87 | 87 |
// reliable. If we want to carry out the node degree in the graph |
88 | 88 |
// as in the \ref InDegMap and we use the reverseArc(), then it cause |
89 | 89 |
// unreliable functionality. Because the alteration observing signals |
90 | 90 |
// only erasing and adding but not the reversing, it will stores bad |
91 | 91 |
// degrees. Apart form that the subgraph adaptors cannot even signal |
92 | 92 |
// the alterations because just a setting in the filter map can modify |
93 | 93 |
// the graph and this cannot be watched in any way. |
94 | 94 |
// |
95 | 95 |
// \param _Container The container which is observed. |
96 | 96 |
// \param _Item The item type which is obserbved. |
97 | 97 |
|
98 | 98 |
template <typename _Container, typename _Item> |
99 | 99 |
class AlterationNotifier { |
100 | 100 |
public: |
101 | 101 |
|
102 | 102 |
typedef True Notifier; |
103 | 103 |
|
104 | 104 |
typedef _Container Container; |
105 | 105 |
typedef _Item Item; |
106 | 106 |
|
107 | 107 |
// \brief Exception which can be called from clear() and |
108 | 108 |
// erase(). |
109 | 109 |
// |
110 | 110 |
// From the clear() and erase() function only this |
111 | 111 |
// exception is allowed to throw. The exception immediatly |
112 | 112 |
// detaches the current observer from the notifier. Because the |
113 | 113 |
// clear() and erase() should not throw other exceptions |
114 | 114 |
// it can be used to invalidate the observer. |
115 | 115 |
struct ImmediateDetach {}; |
116 | 116 |
|
117 | 117 |
// \brief ObserverBase is the base class for the observers. |
118 | 118 |
// |
119 | 119 |
// ObserverBase is the abstract base class for the observers. |
120 | 120 |
// It will be notified about an item was inserted into or |
121 | 121 |
// erased from the graph. |
122 | 122 |
// |
123 | 123 |
// The observer interface contains some pure virtual functions |
124 | 124 |
// to override. The add() and erase() functions are |
125 | 125 |
// to notify the oberver when one item is added or erased. |
126 | 126 |
// |
127 | 127 |
// The build() and clear() members are to notify the observer |
128 | 128 |
// about the container is built from an empty container or |
129 | 129 |
// is cleared to an empty container. |
130 | 130 |
class ObserverBase { |
131 | 131 |
protected: |
132 | 132 |
typedef AlterationNotifier Notifier; |
133 | 133 |
|
134 | 134 |
friend class AlterationNotifier; |
135 | 135 |
|
136 | 136 |
// \brief Default constructor. |
137 | 137 |
// |
138 | 138 |
// Default constructor for ObserverBase. |
139 | 139 |
ObserverBase() : _notifier(0) {} |
140 | 140 |
|
141 | 141 |
// \brief Constructor which attach the observer into notifier. |
142 | 142 |
// |
143 | 143 |
// Constructor which attach the observer into notifier. |
144 | 144 |
ObserverBase(AlterationNotifier& nf) { |
145 | 145 |
attach(nf); |
146 | 146 |
} |
147 | 147 |
|
148 | 148 |
// \brief Constructor which attach the obserever to the same notifier. |
149 | 149 |
// |
150 | 150 |
// Constructor which attach the obserever to the same notifier as |
151 | 151 |
// the other observer is attached to. |
152 | 152 |
ObserverBase(const ObserverBase& copy) { |
153 | 153 |
if (copy.attached()) { |
154 | 154 |
attach(*copy.notifier()); |
155 | 155 |
} |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
// \brief Destructor |
159 | 159 |
virtual ~ObserverBase() { |
160 | 160 |
if (attached()) { |
161 | 161 |
detach(); |
162 | 162 |
} |
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
// \brief Attaches the observer into an AlterationNotifier. |
166 | 166 |
// |
167 | 167 |
// This member attaches the observer into an AlterationNotifier. |
168 | 168 |
void attach(AlterationNotifier& nf) { |
169 | 169 |
nf.attach(*this); |
170 | 170 |
} |
171 | 171 |
|
172 | 172 |
// \brief Detaches the observer into an AlterationNotifier. |
173 | 173 |
// |
174 | 174 |
// This member detaches the observer from an AlterationNotifier. |
175 | 175 |
void detach() { |
176 | 176 |
_notifier->detach(*this); |
177 | 177 |
} |
178 | 178 |
|
179 | 179 |
// \brief Gives back a pointer to the notifier which the map |
180 | 180 |
// attached into. |
181 | 181 |
// |
182 | 182 |
// This function gives back a pointer to the notifier which the map |
183 | 183 |
// attached into. |
184 | 184 |
Notifier* notifier() const { return const_cast<Notifier*>(_notifier); } |
185 | 185 |
|
186 | 186 |
// Gives back true when the observer is attached into a notifier. |
187 | 187 |
bool attached() const { return _notifier != 0; } |
188 | 188 |
|
189 | 189 |
private: |
190 | 190 |
|
191 | 191 |
ObserverBase& operator=(const ObserverBase& copy); |
192 | 192 |
|
193 | 193 |
protected: |
194 | 194 |
|
195 | 195 |
Notifier* _notifier; |
196 | 196 |
typename std::list<ObserverBase*>::iterator _index; |
197 | 197 |
|
198 | 198 |
// \brief The member function to notificate the observer about an |
199 | 199 |
// item is added to the container. |
200 | 200 |
// |
201 | 201 |
// The add() member function notificates the observer about an item |
202 | 202 |
// is added to the container. It have to be overrided in the |
203 | 203 |
// subclasses. |
204 | 204 |
virtual void add(const Item&) = 0; |
205 | 205 |
|
206 | 206 |
// \brief The member function to notificate the observer about |
207 | 207 |
// more item is added to the container. |
208 | 208 |
// |
209 | 209 |
// The add() member function notificates the observer about more item |
210 | 210 |
// is added to the container. It have to be overrided in the |
211 | 211 |
// subclasses. |
212 | 212 |
virtual void add(const std::vector<Item>& items) = 0; |
213 | 213 |
|
214 | 214 |
// \brief The member function to notificate the observer about an |
215 | 215 |
// item is erased from the container. |
216 | 216 |
// |
217 | 217 |
// The erase() member function notificates the observer about an |
218 | 218 |
// item is erased from the container. It have to be overrided in |
219 | 219 |
// the subclasses. |
220 | 220 |
virtual void erase(const Item&) = 0; |
221 | 221 |
|
222 | 222 |
// \brief The member function to notificate the observer about |
223 | 223 |
// more item is erased from the container. |
224 | 224 |
// |
225 | 225 |
// The erase() member function notificates the observer about more item |
226 | 226 |
// is erased from the container. It have to be overrided in the |
227 | 227 |
// subclasses. |
228 | 228 |
virtual void erase(const std::vector<Item>& items) = 0; |
229 | 229 |
|
230 | 230 |
// \brief The member function to notificate the observer about the |
231 | 231 |
// container is built. |
232 | 232 |
// |
233 | 233 |
// The build() member function notificates the observer about the |
234 | 234 |
// container is built from an empty container. It have to be |
235 | 235 |
// overrided in the subclasses. |
236 | 236 |
virtual void build() = 0; |
237 | 237 |
|
238 | 238 |
// \brief The member function to notificate the observer about all |
239 | 239 |
// items are erased from the container. |
240 | 240 |
// |
241 | 241 |
// The clear() member function notificates the observer about all |
242 | 242 |
// items are erased from the container. It have to be overrided in |
243 | 243 |
// the subclasses. |
244 | 244 |
virtual void clear() = 0; |
245 | 245 |
|
246 | 246 |
}; |
247 | 247 |
|
248 | 248 |
protected: |
249 | 249 |
|
250 | 250 |
const Container* container; |
251 | 251 |
|
252 | 252 |
typedef std::list<ObserverBase*> Observers; |
253 | 253 |
Observers _observers; |
254 | 254 |
|
255 | 255 |
|
256 | 256 |
public: |
257 | 257 |
|
258 | 258 |
// \brief Default constructor. |
259 | 259 |
// |
260 | 260 |
// The default constructor of the AlterationNotifier. |
261 | 261 |
// It creates an empty notifier. |
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-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_ARRAY_MAP_H |
20 | 20 |
#define LEMON_BITS_ARRAY_MAP_H |
21 | 21 |
|
22 | 22 |
#include <memory> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/traits.h> |
25 | 25 |
#include <lemon/bits/alteration_notifier.h> |
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
// \ingroup graphbits |
30 | 30 |
// \file |
31 | 31 |
// \brief Graph map based on the array storage. |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
// \ingroup graphbits |
36 | 36 |
// |
37 | 37 |
// \brief Graph map based on the array storage. |
38 | 38 |
// |
39 | 39 |
// The ArrayMap template class is graph map structure that automatically |
40 | 40 |
// updates the map when a key is added to or erased from the graph. |
41 | 41 |
// This map uses the allocators to implement the container functionality. |
42 | 42 |
// |
43 | 43 |
// The template parameters are the Graph, the current Item type and |
44 | 44 |
// the Value type of the map. |
45 | 45 |
template <typename _Graph, typename _Item, typename _Value> |
46 | 46 |
class ArrayMap |
47 | 47 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
48 | 48 |
public: |
49 | 49 |
// The graph type. |
50 | 50 |
typedef _Graph Graph; |
51 | 51 |
// The item type. |
52 | 52 |
typedef _Item Item; |
53 | 53 |
// The reference map tag. |
54 | 54 |
typedef True ReferenceMapTag; |
55 | 55 |
|
56 | 56 |
// The key type of the map. |
57 | 57 |
typedef _Item Key; |
58 | 58 |
// The value type of the map. |
59 | 59 |
typedef _Value Value; |
60 | 60 |
|
61 | 61 |
// The const reference type of the map. |
62 | 62 |
typedef const _Value& ConstReference; |
63 | 63 |
// The reference type of the map. |
64 | 64 |
typedef _Value& Reference; |
65 | 65 |
|
66 | 66 |
// The notifier type. |
67 | 67 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
68 | 68 |
|
69 | 69 |
// The MapBase of the Map which imlements the core regisitry function. |
70 | 70 |
typedef typename Notifier::ObserverBase Parent; |
71 | 71 |
|
72 | 72 |
private: |
73 | 73 |
typedef std::allocator<Value> Allocator; |
74 | 74 |
|
75 | 75 |
public: |
76 | 76 |
|
77 | 77 |
// \brief Graph initialized map constructor. |
78 | 78 |
// |
79 | 79 |
// Graph initialized map constructor. |
80 | 80 |
explicit ArrayMap(const Graph& graph) { |
81 | 81 |
Parent::attach(graph.notifier(Item())); |
82 | 82 |
allocate_memory(); |
83 | 83 |
Notifier* nf = Parent::notifier(); |
84 | 84 |
Item it; |
85 | 85 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
86 | 86 |
int id = nf->id(it);; |
87 | 87 |
allocator.construct(&(values[id]), Value()); |
88 | 88 |
} |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
// \brief Constructor to use default value to initialize the map. |
92 | 92 |
// |
93 | 93 |
// It constructs a map and initialize all of the the map. |
94 | 94 |
ArrayMap(const Graph& graph, const Value& value) { |
95 | 95 |
Parent::attach(graph.notifier(Item())); |
96 | 96 |
allocate_memory(); |
97 | 97 |
Notifier* nf = Parent::notifier(); |
98 | 98 |
Item it; |
99 | 99 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
100 | 100 |
int id = nf->id(it);; |
101 | 101 |
allocator.construct(&(values[id]), value); |
102 | 102 |
} |
103 | 103 |
} |
104 | 104 |
|
105 | 105 |
private: |
106 | 106 |
// \brief Constructor to copy a map of the same map type. |
107 | 107 |
// |
108 | 108 |
// Constructor to copy a map of the same map type. |
109 | 109 |
ArrayMap(const ArrayMap& copy) : Parent() { |
110 | 110 |
if (copy.attached()) { |
111 | 111 |
attach(*copy.notifier()); |
112 | 112 |
} |
113 | 113 |
capacity = copy.capacity; |
114 | 114 |
if (capacity == 0) return; |
115 | 115 |
values = allocator.allocate(capacity); |
116 | 116 |
Notifier* nf = Parent::notifier(); |
117 | 117 |
Item it; |
118 | 118 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
119 | 119 |
int id = nf->id(it);; |
120 | 120 |
allocator.construct(&(values[id]), copy.values[id]); |
121 | 121 |
} |
122 | 122 |
} |
123 | 123 |
|
124 | 124 |
// \brief Assign operator. |
125 | 125 |
// |
126 | 126 |
// This operator assigns for each item in the map the |
127 | 127 |
// value mapped to the same item in the copied map. |
128 | 128 |
// The parameter map should be indiced with the same |
129 | 129 |
// itemset because this assign operator does not change |
130 | 130 |
// the container of the map. |
131 | 131 |
ArrayMap& operator=(const ArrayMap& cmap) { |
132 | 132 |
return operator=<ArrayMap>(cmap); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
|
136 | 136 |
// \brief Template assign operator. |
137 | 137 |
// |
138 | 138 |
// The given parameter should be conform to the ReadMap |
139 | 139 |
// concecpt and could be indiced by the current item set of |
140 | 140 |
// the NodeMap. In this case the value for each item |
141 | 141 |
// is assigned by the value of the given ReadMap. |
142 | 142 |
template <typename CMap> |
143 | 143 |
ArrayMap& operator=(const CMap& cmap) { |
144 | 144 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
145 | 145 |
const typename Parent::Notifier* nf = Parent::notifier(); |
146 | 146 |
Item it; |
147 | 147 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
148 | 148 |
set(it, cmap[it]); |
149 | 149 |
} |
150 | 150 |
return *this; |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
public: |
154 | 154 |
// \brief The destructor of the map. |
155 | 155 |
// |
156 | 156 |
// The destructor of the map. |
157 | 157 |
virtual ~ArrayMap() { |
158 | 158 |
if (attached()) { |
159 | 159 |
clear(); |
160 | 160 |
detach(); |
161 | 161 |
} |
162 | 162 |
} |
163 | 163 |
|
164 | 164 |
protected: |
165 | 165 |
|
166 | 166 |
using Parent::attach; |
167 | 167 |
using Parent::detach; |
168 | 168 |
using Parent::attached; |
169 | 169 |
|
170 | 170 |
public: |
171 | 171 |
|
172 | 172 |
// \brief The subscript operator. |
173 | 173 |
// |
174 | 174 |
// The subscript operator. The map can be subscripted by the |
175 | 175 |
// actual keys of the graph. |
176 | 176 |
Value& operator[](const Key& key) { |
177 | 177 |
int id = Parent::notifier()->id(key); |
178 | 178 |
return values[id]; |
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
// \brief The const subscript operator. |
182 | 182 |
// |
183 | 183 |
// The const subscript operator. The map can be subscripted by the |
184 | 184 |
// actual keys of the graph. |
185 | 185 |
const Value& operator[](const Key& key) const { |
186 | 186 |
int id = Parent::notifier()->id(key); |
187 | 187 |
return values[id]; |
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
// \brief Setter function of the map. |
191 | 191 |
// |
192 | 192 |
// Setter function of the map. Equivalent with map[key] = val. |
193 | 193 |
// This is a compatibility feature with the not dereferable maps. |
194 | 194 |
void set(const Key& key, const Value& val) { |
195 | 195 |
(*this)[key] = val; |
196 | 196 |
} |
197 | 197 |
|
198 | 198 |
protected: |
199 | 199 |
|
200 | 200 |
// \brief Adds a new key to the map. |
201 | 201 |
// |
202 | 202 |
// It adds a new key to the map. It is called by the observer notifier |
203 | 203 |
// and it overrides the add() member function of the observer base. |
204 | 204 |
virtual void add(const Key& key) { |
205 | 205 |
Notifier* nf = Parent::notifier(); |
206 | 206 |
int id = nf->id(key); |
207 | 207 |
if (id >= capacity) { |
208 | 208 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
209 | 209 |
while (new_capacity <= id) { |
210 | 210 |
new_capacity <<= 1; |
211 | 211 |
} |
212 | 212 |
Value* new_values = allocator.allocate(new_capacity); |
213 | 213 |
Item it; |
214 | 214 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
215 | 215 |
int jd = nf->id(it);; |
216 | 216 |
if (id != jd) { |
217 | 217 |
allocator.construct(&(new_values[jd]), values[jd]); |
218 | 218 |
allocator.destroy(&(values[jd])); |
219 | 219 |
} |
220 | 220 |
} |
221 | 221 |
if (capacity != 0) allocator.deallocate(values, capacity); |
222 | 222 |
values = new_values; |
223 | 223 |
capacity = new_capacity; |
224 | 224 |
} |
225 | 225 |
allocator.construct(&(values[id]), Value()); |
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
// \brief Adds more new keys to the map. |
229 | 229 |
// |
230 | 230 |
// It adds more new keys to the map. It is called by the observer notifier |
231 | 231 |
// and it overrides the add() member function of the observer base. |
232 | 232 |
virtual void add(const std::vector<Key>& keys) { |
233 | 233 |
Notifier* nf = Parent::notifier(); |
234 | 234 |
int max_id = -1; |
235 | 235 |
for (int i = 0; i < int(keys.size()); ++i) { |
236 | 236 |
int id = nf->id(keys[i]); |
237 | 237 |
if (id > max_id) { |
238 | 238 |
max_id = id; |
239 | 239 |
} |
240 | 240 |
} |
241 | 241 |
if (max_id >= capacity) { |
242 | 242 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
243 | 243 |
while (new_capacity <= max_id) { |
244 | 244 |
new_capacity <<= 1; |
245 | 245 |
} |
246 | 246 |
Value* new_values = allocator.allocate(new_capacity); |
247 | 247 |
Item it; |
248 | 248 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
249 | 249 |
int id = nf->id(it); |
250 | 250 |
bool found = false; |
251 | 251 |
for (int i = 0; i < int(keys.size()); ++i) { |
252 | 252 |
int jd = nf->id(keys[i]); |
253 | 253 |
if (id == jd) { |
254 | 254 |
found = true; |
255 | 255 |
break; |
256 | 256 |
} |
257 | 257 |
} |
258 | 258 |
if (found) continue; |
259 | 259 |
allocator.construct(&(new_values[id]), values[id]); |
260 | 260 |
allocator.destroy(&(values[id])); |
261 | 261 |
} |
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-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_BASE_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_BASE_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/error.h> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/map_extender.h> |
26 | 26 |
#include <lemon/bits/default_map.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 | 31 |
//\ingroup digraphbits |
32 | 32 |
//\file |
33 | 33 |
//\brief Extenders for the graph types |
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
// \ingroup digraphbits |
37 | 37 |
// |
38 | 38 |
// \brief BaseDigraph to BaseGraph extender |
39 | 39 |
template <typename Base> |
40 | 40 |
class UndirDigraphExtender : public Base { |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef Base Parent; |
45 | 45 |
typedef typename Parent::Arc Edge; |
46 | 46 |
typedef typename Parent::Node Node; |
47 | 47 |
|
48 | 48 |
typedef True UndirectedTag; |
49 | 49 |
|
50 | 50 |
class Arc : public Edge { |
51 | 51 |
friend class UndirDigraphExtender; |
52 | 52 |
|
53 | 53 |
protected: |
54 | 54 |
bool forward; |
55 | 55 |
|
56 | 56 |
Arc(const Edge &ue, bool _forward) : |
57 | 57 |
Edge(ue), forward(_forward) {} |
58 | 58 |
|
59 | 59 |
public: |
60 | 60 |
Arc() {} |
61 | 61 |
|
62 | 62 |
// Invalid arc constructor |
63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {} |
64 | 64 |
|
65 | 65 |
bool operator==(const Arc &that) const { |
66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
67 | 67 |
} |
68 | 68 |
bool operator!=(const Arc &that) const { |
69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
70 | 70 |
} |
71 | 71 |
bool operator<(const Arc &that) const { |
72 | 72 |
return forward<that.forward || |
73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
74 | 74 |
} |
75 | 75 |
}; |
76 | 76 |
|
77 | 77 |
// First node of the edge |
78 | 78 |
Node u(const Edge &e) const { |
79 | 79 |
return Parent::source(e); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
// Source of the given arc |
83 | 83 |
Node source(const Arc &e) const { |
84 | 84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
// Second node of the edge |
88 | 88 |
Node v(const Edge &e) const { |
89 | 89 |
return Parent::target(e); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
// Target of the given arc |
93 | 93 |
Node target(const Arc &e) const { |
94 | 94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
// \brief Directed arc from an edge. |
98 | 98 |
// |
99 | 99 |
// Returns a directed arc corresponding to the specified edge. |
100 | 100 |
// If the given bool is true, the first node of the given edge and |
101 | 101 |
// the source node of the returned arc are the same. |
102 | 102 |
static Arc direct(const Edge &e, bool d) { |
103 | 103 |
return Arc(e, d); |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
// Returns whether the given directed arc has the same orientation |
107 | 107 |
// as the corresponding edge. |
108 | 108 |
static bool direction(const Arc &a) { return a.forward; } |
109 | 109 |
|
110 | 110 |
using Parent::first; |
111 | 111 |
using Parent::next; |
112 | 112 |
|
113 | 113 |
void first(Arc &e) const { |
114 | 114 |
Parent::first(e); |
115 | 115 |
e.forward=true; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
void next(Arc &e) const { |
119 | 119 |
if( e.forward ) { |
120 | 120 |
e.forward = false; |
121 | 121 |
} |
122 | 122 |
else { |
123 | 123 |
Parent::next(e); |
124 | 124 |
e.forward = true; |
125 | 125 |
} |
126 | 126 |
} |
127 | 127 |
|
128 | 128 |
void firstOut(Arc &e, const Node &n) const { |
129 | 129 |
Parent::firstIn(e,n); |
130 | 130 |
if( Edge(e) != INVALID ) { |
131 | 131 |
e.forward = false; |
132 | 132 |
} |
133 | 133 |
else { |
134 | 134 |
Parent::firstOut(e,n); |
135 | 135 |
e.forward = true; |
136 | 136 |
} |
137 | 137 |
} |
138 | 138 |
void nextOut(Arc &e) const { |
139 | 139 |
if( ! e.forward ) { |
140 | 140 |
Node n = Parent::target(e); |
141 | 141 |
Parent::nextIn(e); |
142 | 142 |
if( Edge(e) == INVALID ) { |
143 | 143 |
Parent::firstOut(e, n); |
144 | 144 |
e.forward = true; |
145 | 145 |
} |
146 | 146 |
} |
147 | 147 |
else { |
148 | 148 |
Parent::nextOut(e); |
149 | 149 |
} |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
void firstIn(Arc &e, const Node &n) const { |
153 | 153 |
Parent::firstOut(e,n); |
154 | 154 |
if( Edge(e) != INVALID ) { |
155 | 155 |
e.forward = false; |
156 | 156 |
} |
157 | 157 |
else { |
158 | 158 |
Parent::firstIn(e,n); |
159 | 159 |
e.forward = true; |
160 | 160 |
} |
161 | 161 |
} |
162 | 162 |
void nextIn(Arc &e) const { |
163 | 163 |
if( ! e.forward ) { |
164 | 164 |
Node n = Parent::source(e); |
165 | 165 |
Parent::nextOut(e); |
166 | 166 |
if( Edge(e) == INVALID ) { |
167 | 167 |
Parent::firstIn(e, n); |
168 | 168 |
e.forward = true; |
169 | 169 |
} |
170 | 170 |
} |
171 | 171 |
else { |
172 | 172 |
Parent::nextIn(e); |
173 | 173 |
} |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
177 | 177 |
d = true; |
178 | 178 |
Parent::firstOut(e, n); |
179 | 179 |
if (e != INVALID) return; |
180 | 180 |
d = false; |
181 | 181 |
Parent::firstIn(e, n); |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
void nextInc(Edge &e, bool &d) const { |
185 | 185 |
if (d) { |
186 | 186 |
Node s = Parent::source(e); |
187 | 187 |
Parent::nextOut(e); |
188 | 188 |
if (e != INVALID) return; |
189 | 189 |
d = false; |
190 | 190 |
Parent::firstIn(e, s); |
191 | 191 |
} else { |
192 | 192 |
Parent::nextIn(e); |
193 | 193 |
} |
194 | 194 |
} |
195 | 195 |
|
196 | 196 |
Node nodeFromId(int ix) const { |
197 | 197 |
return Parent::nodeFromId(ix); |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
Arc arcFromId(int ix) const { |
201 | 201 |
return direct(Parent::arcFromId(ix >> 1), bool(ix & 1)); |
202 | 202 |
} |
203 | 203 |
|
204 | 204 |
Edge edgeFromId(int ix) const { |
205 | 205 |
return Parent::arcFromId(ix); |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
int id(const Node &n) const { |
209 | 209 |
return Parent::id(n); |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
int id(const Edge &e) const { |
213 | 213 |
return Parent::id(e); |
214 | 214 |
} |
215 | 215 |
|
216 | 216 |
int id(const Arc &e) const { |
217 | 217 |
return 2 * Parent::id(e) + int(e.forward); |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
int maxNodeId() const { |
221 | 221 |
return Parent::maxNodeId(); |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
int maxArcId() const { |
225 | 225 |
return 2 * Parent::maxArcId() + 1; |
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
int maxEdgeId() const { |
229 | 229 |
return Parent::maxArcId(); |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
int arcNum() const { |
233 | 233 |
return 2 * Parent::arcNum(); |
234 | 234 |
} |
235 | 235 |
|
236 | 236 |
int edgeNum() const { |
237 | 237 |
return Parent::arcNum(); |
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
241 | 241 |
if (p == INVALID) { |
242 | 242 |
Edge arc = Parent::findArc(s, t); |
243 | 243 |
if (arc != INVALID) return direct(arc, true); |
244 | 244 |
arc = Parent::findArc(t, s); |
245 | 245 |
if (arc != INVALID) return direct(arc, false); |
246 | 246 |
} else if (direction(p)) { |
247 | 247 |
Edge arc = Parent::findArc(s, t, p); |
248 | 248 |
if (arc != INVALID) return direct(arc, true); |
249 | 249 |
arc = Parent::findArc(t, s); |
250 | 250 |
if (arc != INVALID) return direct(arc, false); |
251 | 251 |
} else { |
252 | 252 |
Edge arc = Parent::findArc(t, s, p); |
253 | 253 |
if (arc != INVALID) return direct(arc, false); |
254 | 254 |
} |
255 | 255 |
return INVALID; |
256 | 256 |
} |
257 | 257 |
|
258 | 258 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
259 | 259 |
if (s != t) { |
260 | 260 |
if (p == INVALID) { |
261 | 261 |
Edge arc = Parent::findArc(s, t); |
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-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_BEZIER_H |
20 | 20 |
#define LEMON_BEZIER_H |
21 | 21 |
|
22 | 22 |
//\ingroup misc |
23 | 23 |
//\file |
24 | 24 |
//\brief Classes to compute with Bezier curves. |
25 | 25 |
// |
26 | 26 |
//Up to now this file is used internally by \ref graph_to_eps.h |
27 | 27 |
|
28 | 28 |
#include<lemon/dim2.h> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
namespace dim2 { |
32 | 32 |
|
33 | 33 |
class BezierBase { |
34 | 34 |
public: |
35 | 35 |
typedef lemon::dim2::Point<double> Point; |
36 | 36 |
protected: |
37 | 37 |
static Point conv(Point x,Point y,double t) {return (1-t)*x+t*y;} |
38 | 38 |
}; |
39 | 39 |
|
40 | 40 |
class Bezier1 : public BezierBase |
41 | 41 |
{ |
42 | 42 |
public: |
43 | 43 |
Point p1,p2; |
44 | 44 |
|
45 | 45 |
Bezier1() {} |
46 | 46 |
Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {} |
47 | 47 |
|
48 | 48 |
Point operator()(double t) const |
49 | 49 |
{ |
50 | 50 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
51 | 51 |
return conv(p1,p2,t); |
52 | 52 |
} |
53 | 53 |
Bezier1 before(double t) const |
54 | 54 |
{ |
55 | 55 |
return Bezier1(p1,conv(p1,p2,t)); |
56 | 56 |
} |
57 | 57 |
|
58 | 58 |
Bezier1 after(double t) const |
59 | 59 |
{ |
60 | 60 |
return Bezier1(conv(p1,p2,t),p2); |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
Bezier1 revert() const { return Bezier1(p2,p1);} |
64 | 64 |
Bezier1 operator()(double a,double b) const { return before(b).after(a/b); } |
65 | 65 |
Point grad() const { return p2-p1; } |
66 | 66 |
Point norm() const { return rot90(p2-p1); } |
67 | 67 |
Point grad(double) const { return grad(); } |
68 | 68 |
Point norm(double t) const { return rot90(grad(t)); } |
69 | 69 |
}; |
70 | 70 |
|
71 | 71 |
class Bezier2 : public BezierBase |
72 | 72 |
{ |
73 | 73 |
public: |
74 | 74 |
Point p1,p2,p3; |
75 | 75 |
|
76 | 76 |
Bezier2() {} |
77 | 77 |
Bezier2(Point _p1, Point _p2, Point _p3) :p1(_p1), p2(_p2), p3(_p3) {} |
78 | 78 |
Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {} |
79 | 79 |
Point operator()(double t) const |
80 | 80 |
{ |
81 | 81 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
82 | 82 |
return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3; |
83 | 83 |
} |
84 | 84 |
Bezier2 before(double t) const |
85 | 85 |
{ |
86 | 86 |
Point q(conv(p1,p2,t)); |
87 | 87 |
Point r(conv(p2,p3,t)); |
88 | 88 |
return Bezier2(p1,q,conv(q,r,t)); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
Bezier2 after(double t) const |
92 | 92 |
{ |
93 | 93 |
Point q(conv(p1,p2,t)); |
94 | 94 |
Point r(conv(p2,p3,t)); |
95 | 95 |
return Bezier2(conv(q,r,t),r,p3); |
96 | 96 |
} |
97 | 97 |
Bezier2 revert() const { return Bezier2(p3,p2,p1);} |
98 | 98 |
Bezier2 operator()(double a,double b) const { return before(b).after(a/b); } |
99 | 99 |
Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); } |
100 | 100 |
Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); } |
101 | 101 |
Point grad(double t) const { return grad()(t); } |
102 | 102 |
Point norm(double t) const { return rot90(grad(t)); } |
103 | 103 |
}; |
104 | 104 |
|
105 | 105 |
class Bezier3 : public BezierBase |
106 | 106 |
{ |
107 | 107 |
public: |
108 | 108 |
Point p1,p2,p3,p4; |
109 | 109 |
|
110 | 110 |
Bezier3() {} |
111 | 111 |
Bezier3(Point _p1, Point _p2, Point _p3, Point _p4) |
112 | 112 |
: p1(_p1), p2(_p2), p3(_p3), p4(_p4) {} |
113 | 113 |
Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), |
114 | 114 |
p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {} |
115 | 115 |
Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)), |
116 | 116 |
p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {} |
117 | 117 |
|
118 | 118 |
Point operator()(double t) const |
119 | 119 |
{ |
120 | 120 |
// return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t); |
121 | 121 |
return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+ |
122 | 122 |
(3*t*t*(1-t))*p3+(t*t*t)*p4; |
123 | 123 |
} |
124 | 124 |
Bezier3 before(double t) const |
125 | 125 |
{ |
126 | 126 |
Point p(conv(p1,p2,t)); |
127 | 127 |
Point q(conv(p2,p3,t)); |
128 | 128 |
Point r(conv(p3,p4,t)); |
129 | 129 |
Point a(conv(p,q,t)); |
130 | 130 |
Point b(conv(q,r,t)); |
131 | 131 |
Point c(conv(a,b,t)); |
132 | 132 |
return Bezier3(p1,p,a,c); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
Bezier3 after(double t) const |
136 | 136 |
{ |
137 | 137 |
Point p(conv(p1,p2,t)); |
138 | 138 |
Point q(conv(p2,p3,t)); |
139 | 139 |
Point r(conv(p3,p4,t)); |
140 | 140 |
Point a(conv(p,q,t)); |
141 | 141 |
Point b(conv(q,r,t)); |
142 | 142 |
Point c(conv(a,b,t)); |
143 | 143 |
return Bezier3(c,b,r,p4); |
144 | 144 |
} |
145 | 145 |
Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);} |
146 | 146 |
Bezier3 operator()(double a,double b) const { return before(b).after(a/b); } |
147 | 147 |
Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); } |
148 | 148 |
Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1), |
149 | 149 |
3.0*rot90(p3-p2), |
150 | 150 |
3.0*rot90(p4-p3)); } |
151 | 151 |
Point grad(double t) const { return grad()(t); } |
152 | 152 |
Point norm(double t) const { return rot90(grad(t)); } |
153 | 153 |
|
154 | 154 |
template<class R,class F,class S,class D> |
155 | 155 |
R recSplit(F &_f,const S &_s,D _d) const |
156 | 156 |
{ |
157 | 157 |
const Point a=(p1+p2)/2; |
158 | 158 |
const Point b=(p2+p3)/2; |
159 | 159 |
const Point c=(p3+p4)/2; |
160 | 160 |
const Point d=(a+b)/2; |
161 | 161 |
const Point e=(b+c)/2; |
162 | 162 |
const Point f=(d+e)/2; |
163 | 163 |
R f1=_f(Bezier3(p1,a,d,e),_d); |
164 | 164 |
R f2=_f(Bezier3(e,d,c,p4),_d); |
165 | 165 |
return _s(f1,f2); |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
}; |
169 | 169 |
|
170 | 170 |
|
171 | 171 |
} //END OF NAMESPACE dim2 |
172 | 172 |
} //END OF NAMESPACE lemon |
173 | 173 |
|
174 | 174 |
#endif // LEMON_BEZIER_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-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_DEFAULT_MAP_H |
20 | 20 |
#define LEMON_BITS_DEFAULT_MAP_H |
21 | 21 |
|
22 | 22 |
#include <lemon/bits/array_map.h> |
23 | 23 |
#include <lemon/bits/vector_map.h> |
24 | 24 |
//#include <lemon/bits/debug_map.h> |
25 | 25 |
|
26 | 26 |
//\ingroup graphbits |
27 | 27 |
//\file |
28 | 28 |
//\brief Graph maps that construct and destruct their elements dynamically. |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
|
33 | 33 |
//#ifndef LEMON_USE_DEBUG_MAP |
34 | 34 |
|
35 | 35 |
template <typename _Graph, typename _Item, typename _Value> |
36 | 36 |
struct DefaultMapSelector { |
37 | 37 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
38 | 38 |
}; |
39 | 39 |
|
40 | 40 |
// bool |
41 | 41 |
template <typename _Graph, typename _Item> |
42 | 42 |
struct DefaultMapSelector<_Graph, _Item, bool> { |
43 | 43 |
typedef VectorMap<_Graph, _Item, bool> Map; |
44 | 44 |
}; |
45 | 45 |
|
46 | 46 |
// char |
47 | 47 |
template <typename _Graph, typename _Item> |
48 | 48 |
struct DefaultMapSelector<_Graph, _Item, char> { |
49 | 49 |
typedef VectorMap<_Graph, _Item, char> Map; |
50 | 50 |
}; |
51 | 51 |
|
52 | 52 |
template <typename _Graph, typename _Item> |
53 | 53 |
struct DefaultMapSelector<_Graph, _Item, signed char> { |
54 | 54 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
55 | 55 |
}; |
56 | 56 |
|
57 | 57 |
template <typename _Graph, typename _Item> |
58 | 58 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> { |
59 | 59 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
60 | 60 |
}; |
61 | 61 |
|
62 | 62 |
|
63 | 63 |
// int |
64 | 64 |
template <typename _Graph, typename _Item> |
65 | 65 |
struct DefaultMapSelector<_Graph, _Item, signed int> { |
66 | 66 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
67 | 67 |
}; |
68 | 68 |
|
69 | 69 |
template <typename _Graph, typename _Item> |
70 | 70 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> { |
71 | 71 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
72 | 72 |
}; |
73 | 73 |
|
74 | 74 |
|
75 | 75 |
// short |
76 | 76 |
template <typename _Graph, typename _Item> |
77 | 77 |
struct DefaultMapSelector<_Graph, _Item, signed short> { |
78 | 78 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
template <typename _Graph, typename _Item> |
82 | 82 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> { |
83 | 83 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
84 | 84 |
}; |
85 | 85 |
|
86 | 86 |
|
87 | 87 |
// long |
88 | 88 |
template <typename _Graph, typename _Item> |
89 | 89 |
struct DefaultMapSelector<_Graph, _Item, signed long> { |
90 | 90 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
91 | 91 |
}; |
92 | 92 |
|
93 | 93 |
template <typename _Graph, typename _Item> |
94 | 94 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> { |
95 | 95 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
96 | 96 |
}; |
97 | 97 |
|
98 | 98 |
|
99 | 99 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
100 | 100 |
|
101 | 101 |
// long long |
102 | 102 |
template <typename _Graph, typename _Item> |
103 | 103 |
struct DefaultMapSelector<_Graph, _Item, signed long long> { |
104 | 104 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
105 | 105 |
}; |
106 | 106 |
|
107 | 107 |
template <typename _Graph, typename _Item> |
108 | 108 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> { |
109 | 109 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
#endif |
113 | 113 |
|
114 | 114 |
|
115 | 115 |
// float |
116 | 116 |
template <typename _Graph, typename _Item> |
117 | 117 |
struct DefaultMapSelector<_Graph, _Item, float> { |
118 | 118 |
typedef VectorMap<_Graph, _Item, float> Map; |
119 | 119 |
}; |
120 | 120 |
|
121 | 121 |
|
122 | 122 |
// double |
123 | 123 |
template <typename _Graph, typename _Item> |
124 | 124 |
struct DefaultMapSelector<_Graph, _Item, double> { |
125 | 125 |
typedef VectorMap<_Graph, _Item, double> Map; |
126 | 126 |
}; |
127 | 127 |
|
128 | 128 |
|
129 | 129 |
// long double |
130 | 130 |
template <typename _Graph, typename _Item> |
131 | 131 |
struct DefaultMapSelector<_Graph, _Item, long double> { |
132 | 132 |
typedef VectorMap<_Graph, _Item, long double> Map; |
133 | 133 |
}; |
134 | 134 |
|
135 | 135 |
|
136 | 136 |
// pointer |
137 | 137 |
template <typename _Graph, typename _Item, typename _Ptr> |
138 | 138 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> { |
139 | 139 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
140 | 140 |
}; |
141 | 141 |
|
142 | 142 |
// #else |
143 | 143 |
|
144 | 144 |
// template <typename _Graph, typename _Item, typename _Value> |
145 | 145 |
// struct DefaultMapSelector { |
146 | 146 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
147 | 147 |
// }; |
148 | 148 |
|
149 | 149 |
// #endif |
150 | 150 |
|
151 | 151 |
// DefaultMap class |
152 | 152 |
template <typename _Graph, typename _Item, typename _Value> |
153 | 153 |
class DefaultMap |
154 | 154 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map { |
155 | 155 |
public: |
156 | 156 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
157 | 157 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
158 | 158 |
|
159 | 159 |
typedef typename Parent::Graph Graph; |
160 | 160 |
typedef typename Parent::Value Value; |
161 | 161 |
|
162 | 162 |
explicit DefaultMap(const Graph& graph) : Parent(graph) {} |
163 | 163 |
DefaultMap(const Graph& graph, const Value& value) |
164 | 164 |
: Parent(graph, value) {} |
165 | 165 |
|
166 | 166 |
DefaultMap& operator=(const DefaultMap& cmap) { |
167 | 167 |
return operator=<DefaultMap>(cmap); |
168 | 168 |
} |
169 | 169 |
|
170 | 170 |
template <typename CMap> |
171 | 171 |
DefaultMap& operator=(const CMap& cmap) { |
172 | 172 |
Parent::operator=(cmap); |
173 | 173 |
return *this; |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
}; |
177 | 177 |
|
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
#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 |
* Copyright (C) 2003- |
|
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 |
// This file contains a modified version of the enable_if library from BOOST. |
20 | 20 |
// See the appropriate copyright notice below. |
21 | 21 |
|
22 | 22 |
// Boost enable_if library |
23 | 23 |
|
24 | 24 |
// Copyright 2003 (c) The Trustees of Indiana University. |
25 | 25 |
|
26 | 26 |
// Use, modification, and distribution is subject to the Boost Software |
27 | 27 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
28 | 28 |
// http://www.boost.org/LICENSE_1_0.txt) |
29 | 29 |
|
30 | 30 |
// Authors: Jaakko Jarvi (jajarvi at osl.iu.edu) |
31 | 31 |
// Jeremiah Willcock (jewillco at osl.iu.edu) |
32 | 32 |
// Andrew Lumsdaine (lums at osl.iu.edu) |
33 | 33 |
|
34 | 34 |
|
35 | 35 |
#ifndef LEMON_BITS_ENABLE_IF_H |
36 | 36 |
#define LEMON_BITS_ENABLE_IF_H |
37 | 37 |
|
38 | 38 |
//\file |
39 | 39 |
//\brief Miscellaneous basic utilities |
40 | 40 |
|
41 | 41 |
namespace lemon |
42 | 42 |
{ |
43 | 43 |
|
44 | 44 |
// Basic type for defining "tags". A "YES" condition for \c enable_if. |
45 | 45 |
|
46 | 46 |
// Basic type for defining "tags". A "YES" condition for \c enable_if. |
47 | 47 |
// |
48 | 48 |
//\sa False |
49 | 49 |
struct True { |
50 | 50 |
//\e |
51 | 51 |
static const bool value = true; |
52 | 52 |
}; |
53 | 53 |
|
54 | 54 |
// Basic type for defining "tags". A "NO" condition for \c enable_if. |
55 | 55 |
|
56 | 56 |
// Basic type for defining "tags". A "NO" condition for \c enable_if. |
57 | 57 |
// |
58 | 58 |
//\sa True |
59 | 59 |
struct False { |
60 | 60 |
//\e |
61 | 61 |
static const bool value = false; |
62 | 62 |
}; |
63 | 63 |
|
64 | 64 |
|
65 | 65 |
|
66 | 66 |
template <typename T> |
67 | 67 |
struct Wrap { |
68 | 68 |
const T &value; |
69 | 69 |
Wrap(const T &t) : value(t) {} |
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
/**************** dummy class to avoid ambiguity ****************/ |
73 | 73 |
|
74 | 74 |
template<int T> struct dummy { dummy(int) {} }; |
75 | 75 |
|
76 | 76 |
/**************** enable_if from BOOST ****************/ |
77 | 77 |
|
78 | 78 |
template <typename Type, typename T = void> |
79 | 79 |
struct exists { |
80 | 80 |
typedef T type; |
81 | 81 |
}; |
82 | 82 |
|
83 | 83 |
|
84 | 84 |
template <bool B, class T = void> |
85 | 85 |
struct enable_if_c { |
86 | 86 |
typedef T type; |
87 | 87 |
}; |
88 | 88 |
|
89 | 89 |
template <class T> |
90 | 90 |
struct enable_if_c<false, T> {}; |
91 | 91 |
|
92 | 92 |
template <class Cond, class T = void> |
93 | 93 |
struct enable_if : public enable_if_c<Cond::value, T> {}; |
94 | 94 |
|
95 | 95 |
template <bool B, class T> |
96 | 96 |
struct lazy_enable_if_c { |
97 | 97 |
typedef typename T::type type; |
98 | 98 |
}; |
99 | 99 |
|
100 | 100 |
template <class T> |
101 | 101 |
struct lazy_enable_if_c<false, T> {}; |
102 | 102 |
|
103 | 103 |
template <class Cond, class T> |
104 | 104 |
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {}; |
105 | 105 |
|
106 | 106 |
|
107 | 107 |
template <bool B, class T = void> |
108 | 108 |
struct disable_if_c { |
109 | 109 |
typedef T type; |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
template <class T> |
113 | 113 |
struct disable_if_c<true, T> {}; |
114 | 114 |
|
115 | 115 |
template <class Cond, class T = void> |
116 | 116 |
struct disable_if : public disable_if_c<Cond::value, T> {}; |
117 | 117 |
|
118 | 118 |
template <bool B, class T> |
119 | 119 |
struct lazy_disable_if_c { |
120 | 120 |
typedef typename T::type type; |
121 | 121 |
}; |
122 | 122 |
|
123 | 123 |
template <class T> |
124 | 124 |
struct lazy_disable_if_c<true, T> {}; |
125 | 125 |
|
126 | 126 |
template <class Cond, class T> |
127 | 127 |
struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {}; |
128 | 128 |
|
129 | 129 |
} // namespace lemon |
130 | 130 |
|
131 | 131 |
#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 |
* Copyright (C) 2003- |
|
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_GRAPH_ADAPTOR_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/error.h> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/default_map.h> |
26 | 26 |
|
27 | 27 |
namespace lemon { |
28 | 28 |
|
29 | 29 |
template <typename _Digraph> |
30 | 30 |
class DigraphAdaptorExtender : public _Digraph { |
31 | 31 |
public: |
32 | 32 |
|
33 | 33 |
typedef _Digraph Parent; |
34 | 34 |
typedef _Digraph Digraph; |
35 | 35 |
typedef DigraphAdaptorExtender Adaptor; |
36 | 36 |
|
37 | 37 |
// Base extensions |
38 | 38 |
|
39 | 39 |
typedef typename Parent::Node Node; |
40 | 40 |
typedef typename Parent::Arc Arc; |
41 | 41 |
|
42 | 42 |
int maxId(Node) const { |
43 | 43 |
return Parent::maxNodeId(); |
44 | 44 |
} |
45 | 45 |
|
46 | 46 |
int maxId(Arc) const { |
47 | 47 |
return Parent::maxArcId(); |
48 | 48 |
} |
49 | 49 |
|
50 | 50 |
Node fromId(int id, Node) const { |
51 | 51 |
return Parent::nodeFromId(id); |
52 | 52 |
} |
53 | 53 |
|
54 | 54 |
Arc fromId(int id, Arc) const { |
55 | 55 |
return Parent::arcFromId(id); |
56 | 56 |
} |
57 | 57 |
|
58 | 58 |
Node oppositeNode(const Node &n, const Arc &e) const { |
59 | 59 |
if (n == Parent::source(e)) |
60 | 60 |
return Parent::target(e); |
61 | 61 |
else if(n==Parent::target(e)) |
62 | 62 |
return Parent::source(e); |
63 | 63 |
else |
64 | 64 |
return INVALID; |
65 | 65 |
} |
66 | 66 |
|
67 | 67 |
class NodeIt : public Node { |
68 | 68 |
const Adaptor* _adaptor; |
69 | 69 |
public: |
70 | 70 |
|
71 | 71 |
NodeIt() {} |
72 | 72 |
|
73 | 73 |
NodeIt(Invalid i) : Node(i) { } |
74 | 74 |
|
75 | 75 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
76 | 76 |
_adaptor->first(static_cast<Node&>(*this)); |
77 | 77 |
} |
78 | 78 |
|
79 | 79 |
NodeIt(const Adaptor& adaptor, const Node& node) |
80 | 80 |
: Node(node), _adaptor(&adaptor) {} |
81 | 81 |
|
82 | 82 |
NodeIt& operator++() { |
83 | 83 |
_adaptor->next(*this); |
84 | 84 |
return *this; |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
}; |
88 | 88 |
|
89 | 89 |
|
90 | 90 |
class ArcIt : public Arc { |
91 | 91 |
const Adaptor* _adaptor; |
92 | 92 |
public: |
93 | 93 |
|
94 | 94 |
ArcIt() { } |
95 | 95 |
|
96 | 96 |
ArcIt(Invalid i) : Arc(i) { } |
97 | 97 |
|
98 | 98 |
explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
99 | 99 |
_adaptor->first(static_cast<Arc&>(*this)); |
100 | 100 |
} |
101 | 101 |
|
102 | 102 |
ArcIt(const Adaptor& adaptor, const Arc& e) : |
103 | 103 |
Arc(e), _adaptor(&adaptor) { } |
104 | 104 |
|
105 | 105 |
ArcIt& operator++() { |
106 | 106 |
_adaptor->next(*this); |
107 | 107 |
return *this; |
108 | 108 |
} |
109 | 109 |
|
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
|
113 | 113 |
class OutArcIt : public Arc { |
114 | 114 |
const Adaptor* _adaptor; |
115 | 115 |
public: |
116 | 116 |
|
117 | 117 |
OutArcIt() { } |
118 | 118 |
|
119 | 119 |
OutArcIt(Invalid i) : Arc(i) { } |
120 | 120 |
|
121 | 121 |
OutArcIt(const Adaptor& adaptor, const Node& node) |
122 | 122 |
: _adaptor(&adaptor) { |
123 | 123 |
_adaptor->firstOut(*this, node); |
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
OutArcIt(const Adaptor& adaptor, const Arc& arc) |
127 | 127 |
: Arc(arc), _adaptor(&adaptor) {} |
128 | 128 |
|
129 | 129 |
OutArcIt& operator++() { |
130 | 130 |
_adaptor->nextOut(*this); |
131 | 131 |
return *this; |
132 | 132 |
} |
133 | 133 |
|
134 | 134 |
}; |
135 | 135 |
|
136 | 136 |
|
137 | 137 |
class InArcIt : public Arc { |
138 | 138 |
const Adaptor* _adaptor; |
139 | 139 |
public: |
140 | 140 |
|
141 | 141 |
InArcIt() { } |
142 | 142 |
|
143 | 143 |
InArcIt(Invalid i) : Arc(i) { } |
144 | 144 |
|
145 | 145 |
InArcIt(const Adaptor& adaptor, const Node& node) |
146 | 146 |
: _adaptor(&adaptor) { |
147 | 147 |
_adaptor->firstIn(*this, node); |
148 | 148 |
} |
149 | 149 |
|
150 | 150 |
InArcIt(const Adaptor& adaptor, const Arc& arc) : |
151 | 151 |
Arc(arc), _adaptor(&adaptor) {} |
152 | 152 |
|
153 | 153 |
InArcIt& operator++() { |
154 | 154 |
_adaptor->nextIn(*this); |
155 | 155 |
return *this; |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
}; |
159 | 159 |
|
160 | 160 |
Node baseNode(const OutArcIt &e) const { |
161 | 161 |
return Parent::source(e); |
162 | 162 |
} |
163 | 163 |
Node runningNode(const OutArcIt &e) const { |
164 | 164 |
return Parent::target(e); |
165 | 165 |
} |
166 | 166 |
|
167 | 167 |
Node baseNode(const InArcIt &e) const { |
168 | 168 |
return Parent::target(e); |
169 | 169 |
} |
170 | 170 |
Node runningNode(const InArcIt &e) const { |
171 | 171 |
return Parent::source(e); |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
}; |
175 | 175 |
|
176 | 176 |
|
177 | 177 |
/// \ingroup digraphbits |
178 | 178 |
/// |
179 | 179 |
/// \brief Extender for the GraphAdaptors |
180 | 180 |
template <typename _Graph> |
181 | 181 |
class GraphAdaptorExtender : public _Graph { |
182 | 182 |
public: |
183 | 183 |
|
184 | 184 |
typedef _Graph Parent; |
185 | 185 |
typedef _Graph Graph; |
186 | 186 |
typedef GraphAdaptorExtender Adaptor; |
187 | 187 |
|
188 | 188 |
typedef typename Parent::Node Node; |
189 | 189 |
typedef typename Parent::Arc Arc; |
190 | 190 |
typedef typename Parent::Edge Edge; |
191 | 191 |
|
192 | 192 |
// Graph extension |
193 | 193 |
|
194 | 194 |
int maxId(Node) const { |
195 | 195 |
return Parent::maxNodeId(); |
196 | 196 |
} |
197 | 197 |
|
198 | 198 |
int maxId(Arc) const { |
199 | 199 |
return Parent::maxArcId(); |
200 | 200 |
} |
201 | 201 |
|
202 | 202 |
int maxId(Edge) const { |
203 | 203 |
return Parent::maxEdgeId(); |
204 | 204 |
} |
205 | 205 |
|
206 | 206 |
Node fromId(int id, Node) const { |
207 | 207 |
return Parent::nodeFromId(id); |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
Arc fromId(int id, Arc) const { |
211 | 211 |
return Parent::arcFromId(id); |
212 | 212 |
} |
213 | 213 |
|
214 | 214 |
Edge fromId(int id, Edge) const { |
215 | 215 |
return Parent::edgeFromId(id); |
216 | 216 |
} |
217 | 217 |
|
218 | 218 |
Node oppositeNode(const Node &n, const Edge &e) const { |
219 | 219 |
if( n == Parent::u(e)) |
220 | 220 |
return Parent::v(e); |
221 | 221 |
else if( n == Parent::v(e)) |
222 | 222 |
return Parent::u(e); |
223 | 223 |
else |
224 | 224 |
return INVALID; |
225 | 225 |
} |
226 | 226 |
|
227 | 227 |
Arc oppositeArc(const Arc &a) const { |
228 | 228 |
return Parent::direct(a, !Parent::direction(a)); |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
using Parent::direct; |
232 | 232 |
Arc direct(const Edge &e, const Node &s) const { |
233 | 233 |
return Parent::direct(e, Parent::u(e) == s); |
234 | 234 |
} |
235 | 235 |
|
236 | 236 |
|
237 | 237 |
class NodeIt : public Node { |
238 | 238 |
const Adaptor* _adaptor; |
239 | 239 |
public: |
240 | 240 |
|
241 | 241 |
NodeIt() {} |
242 | 242 |
|
243 | 243 |
NodeIt(Invalid i) : Node(i) { } |
244 | 244 |
|
245 | 245 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
246 | 246 |
_adaptor->first(static_cast<Node&>(*this)); |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
NodeIt(const Adaptor& adaptor, const Node& node) |
250 | 250 |
: Node(node), _adaptor(&adaptor) {} |
251 | 251 |
|
252 | 252 |
NodeIt& operator++() { |
253 | 253 |
_adaptor->next(*this); |
254 | 254 |
return *this; |
255 | 255 |
} |
256 | 256 |
|
257 | 257 |
}; |
258 | 258 |
|
259 | 259 |
|
260 | 260 |
class ArcIt : public Arc { |
261 | 261 |
const Adaptor* _adaptor; |
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-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_GRAPH_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_GRAPH_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/map_extender.h> |
25 | 25 |
#include <lemon/bits/default_map.h> |
26 | 26 |
|
27 | 27 |
#include <lemon/concept_check.h> |
28 | 28 |
#include <lemon/concepts/maps.h> |
29 | 29 |
|
30 | 30 |
//\ingroup graphbits |
31 | 31 |
//\file |
32 | 32 |
//\brief Extenders for the graph types |
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
// \ingroup graphbits |
36 | 36 |
// |
37 | 37 |
// \brief Extender for the digraph implementations |
38 | 38 |
template <typename Base> |
39 | 39 |
class DigraphExtender : public Base { |
40 | 40 |
public: |
41 | 41 |
|
42 | 42 |
typedef Base Parent; |
43 | 43 |
typedef DigraphExtender Digraph; |
44 | 44 |
|
45 | 45 |
// Base extensions |
46 | 46 |
|
47 | 47 |
typedef typename Parent::Node Node; |
48 | 48 |
typedef typename Parent::Arc Arc; |
49 | 49 |
|
50 | 50 |
int maxId(Node) const { |
51 | 51 |
return Parent::maxNodeId(); |
52 | 52 |
} |
53 | 53 |
|
54 | 54 |
int maxId(Arc) const { |
55 | 55 |
return Parent::maxArcId(); |
56 | 56 |
} |
57 | 57 |
|
58 | 58 |
Node fromId(int id, Node) const { |
59 | 59 |
return Parent::nodeFromId(id); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
Arc fromId(int id, Arc) const { |
63 | 63 |
return Parent::arcFromId(id); |
64 | 64 |
} |
65 | 65 |
|
66 | 66 |
Node oppositeNode(const Node &node, const Arc &arc) const { |
67 | 67 |
if (node == Parent::source(arc)) |
68 | 68 |
return Parent::target(arc); |
69 | 69 |
else if(node == Parent::target(arc)) |
70 | 70 |
return Parent::source(arc); |
71 | 71 |
else |
72 | 72 |
return INVALID; |
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
// Alterable extension |
76 | 76 |
|
77 | 77 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
78 | 78 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
79 | 79 |
|
80 | 80 |
|
81 | 81 |
protected: |
82 | 82 |
|
83 | 83 |
mutable NodeNotifier node_notifier; |
84 | 84 |
mutable ArcNotifier arc_notifier; |
85 | 85 |
|
86 | 86 |
public: |
87 | 87 |
|
88 | 88 |
NodeNotifier& notifier(Node) const { |
89 | 89 |
return node_notifier; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
ArcNotifier& notifier(Arc) const { |
93 | 93 |
return arc_notifier; |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
class NodeIt : public Node { |
97 | 97 |
const Digraph* _digraph; |
98 | 98 |
public: |
99 | 99 |
|
100 | 100 |
NodeIt() {} |
101 | 101 |
|
102 | 102 |
NodeIt(Invalid i) : Node(i) { } |
103 | 103 |
|
104 | 104 |
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) { |
105 | 105 |
_digraph->first(static_cast<Node&>(*this)); |
106 | 106 |
} |
107 | 107 |
|
108 | 108 |
NodeIt(const Digraph& digraph, const Node& node) |
109 | 109 |
: Node(node), _digraph(&digraph) {} |
110 | 110 |
|
111 | 111 |
NodeIt& operator++() { |
112 | 112 |
_digraph->next(*this); |
113 | 113 |
return *this; |
114 | 114 |
} |
115 | 115 |
|
116 | 116 |
}; |
117 | 117 |
|
118 | 118 |
|
119 | 119 |
class ArcIt : public Arc { |
120 | 120 |
const Digraph* _digraph; |
121 | 121 |
public: |
122 | 122 |
|
123 | 123 |
ArcIt() { } |
124 | 124 |
|
125 | 125 |
ArcIt(Invalid i) : Arc(i) { } |
126 | 126 |
|
127 | 127 |
explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) { |
128 | 128 |
_digraph->first(static_cast<Arc&>(*this)); |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
ArcIt(const Digraph& digraph, const Arc& arc) : |
132 | 132 |
Arc(arc), _digraph(&digraph) { } |
133 | 133 |
|
134 | 134 |
ArcIt& operator++() { |
135 | 135 |
_digraph->next(*this); |
136 | 136 |
return *this; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
}; |
140 | 140 |
|
141 | 141 |
|
142 | 142 |
class OutArcIt : public Arc { |
143 | 143 |
const Digraph* _digraph; |
144 | 144 |
public: |
145 | 145 |
|
146 | 146 |
OutArcIt() { } |
147 | 147 |
|
148 | 148 |
OutArcIt(Invalid i) : Arc(i) { } |
149 | 149 |
|
150 | 150 |
OutArcIt(const Digraph& digraph, const Node& node) |
151 | 151 |
: _digraph(&digraph) { |
152 | 152 |
_digraph->firstOut(*this, node); |
153 | 153 |
} |
154 | 154 |
|
155 | 155 |
OutArcIt(const Digraph& digraph, const Arc& arc) |
156 | 156 |
: Arc(arc), _digraph(&digraph) {} |
157 | 157 |
|
158 | 158 |
OutArcIt& operator++() { |
159 | 159 |
_digraph->nextOut(*this); |
160 | 160 |
return *this; |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
|
166 | 166 |
class InArcIt : public Arc { |
167 | 167 |
const Digraph* _digraph; |
168 | 168 |
public: |
169 | 169 |
|
170 | 170 |
InArcIt() { } |
171 | 171 |
|
172 | 172 |
InArcIt(Invalid i) : Arc(i) { } |
173 | 173 |
|
174 | 174 |
InArcIt(const Digraph& digraph, const Node& node) |
175 | 175 |
: _digraph(&digraph) { |
176 | 176 |
_digraph->firstIn(*this, node); |
177 | 177 |
} |
178 | 178 |
|
179 | 179 |
InArcIt(const Digraph& digraph, const Arc& arc) : |
180 | 180 |
Arc(arc), _digraph(&digraph) {} |
181 | 181 |
|
182 | 182 |
InArcIt& operator++() { |
183 | 183 |
_digraph->nextIn(*this); |
184 | 184 |
return *this; |
185 | 185 |
} |
186 | 186 |
|
187 | 187 |
}; |
188 | 188 |
|
189 | 189 |
// \brief Base node of the iterator |
190 | 190 |
// |
191 | 191 |
// Returns the base node (i.e. the source in this case) of the iterator |
192 | 192 |
Node baseNode(const OutArcIt &arc) const { |
193 | 193 |
return Parent::source(arc); |
194 | 194 |
} |
195 | 195 |
// \brief Running node of the iterator |
196 | 196 |
// |
197 | 197 |
// Returns the running node (i.e. the target in this case) of the |
198 | 198 |
// iterator |
199 | 199 |
Node runningNode(const OutArcIt &arc) const { |
200 | 200 |
return Parent::target(arc); |
201 | 201 |
} |
202 | 202 |
|
203 | 203 |
// \brief Base node of the iterator |
204 | 204 |
// |
205 | 205 |
// Returns the base node (i.e. the target in this case) of the iterator |
206 | 206 |
Node baseNode(const InArcIt &arc) const { |
207 | 207 |
return Parent::target(arc); |
208 | 208 |
} |
209 | 209 |
// \brief Running node of the iterator |
210 | 210 |
// |
211 | 211 |
// Returns the running node (i.e. the source in this case) of the |
212 | 212 |
// iterator |
213 | 213 |
Node runningNode(const InArcIt &arc) const { |
214 | 214 |
return Parent::source(arc); |
215 | 215 |
} |
216 | 216 |
|
217 | 217 |
|
218 | 218 |
template <typename _Value> |
219 | 219 |
class NodeMap |
220 | 220 |
: public MapExtender<DefaultMap<Digraph, Node, _Value> > { |
221 | 221 |
public: |
222 | 222 |
typedef DigraphExtender Digraph; |
223 | 223 |
typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent; |
224 | 224 |
|
225 | 225 |
explicit NodeMap(const Digraph& digraph) |
226 | 226 |
: Parent(digraph) {} |
227 | 227 |
NodeMap(const Digraph& digraph, const _Value& value) |
228 | 228 |
: Parent(digraph, value) {} |
229 | 229 |
|
230 | 230 |
private: |
231 | 231 |
NodeMap& operator=(const NodeMap& cmap) { |
232 | 232 |
return operator=<NodeMap>(cmap); |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
template <typename CMap> |
236 | 236 |
NodeMap& operator=(const CMap& cmap) { |
237 | 237 |
Parent::operator=(cmap); |
238 | 238 |
return *this; |
239 | 239 |
} |
240 | 240 |
|
241 | 241 |
}; |
242 | 242 |
|
243 | 243 |
template <typename _Value> |
244 | 244 |
class ArcMap |
245 | 245 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
246 | 246 |
public: |
247 | 247 |
typedef DigraphExtender Digraph; |
248 | 248 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
249 | 249 |
|
250 | 250 |
explicit ArcMap(const Digraph& digraph) |
251 | 251 |
: Parent(digraph) {} |
252 | 252 |
ArcMap(const Digraph& digraph, const _Value& value) |
253 | 253 |
: Parent(digraph, value) {} |
254 | 254 |
|
255 | 255 |
private: |
256 | 256 |
ArcMap& operator=(const ArcMap& cmap) { |
257 | 257 |
return operator=<ArcMap>(cmap); |
258 | 258 |
} |
259 | 259 |
|
260 | 260 |
template <typename CMap> |
261 | 261 |
ArcMap& operator=(const CMap& cmap) { |
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-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_MAP_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_MAP_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/traits.h> |
25 | 25 |
|
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
//\file |
30 | 30 |
//\brief Extenders for iterable maps. |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
// \ingroup graphbits |
35 | 35 |
// |
36 | 36 |
// \brief Extender for maps |
37 | 37 |
template <typename _Map> |
38 | 38 |
class MapExtender : public _Map { |
39 | 39 |
public: |
40 | 40 |
|
41 | 41 |
typedef _Map Parent; |
42 | 42 |
typedef MapExtender Map; |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
typedef typename Parent::Graph Graph; |
46 | 46 |
typedef typename Parent::Key Item; |
47 | 47 |
|
48 | 48 |
typedef typename Parent::Key Key; |
49 | 49 |
typedef typename Parent::Value Value; |
50 | 50 |
|
51 | 51 |
class MapIt; |
52 | 52 |
class ConstMapIt; |
53 | 53 |
|
54 | 54 |
friend class MapIt; |
55 | 55 |
friend class ConstMapIt; |
56 | 56 |
|
57 | 57 |
public: |
58 | 58 |
|
59 | 59 |
MapExtender(const Graph& graph) |
60 | 60 |
: Parent(graph) {} |
61 | 61 |
|
62 | 62 |
MapExtender(const Graph& graph, const Value& value) |
63 | 63 |
: Parent(graph, value) {} |
64 | 64 |
|
65 | 65 |
private: |
66 | 66 |
MapExtender& operator=(const MapExtender& cmap) { |
67 | 67 |
return operator=<MapExtender>(cmap); |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
template <typename CMap> |
71 | 71 |
MapExtender& operator=(const CMap& cmap) { |
72 | 72 |
Parent::operator=(cmap); |
73 | 73 |
return *this; |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
public: |
77 | 77 |
class MapIt : public Item { |
78 | 78 |
public: |
79 | 79 |
|
80 | 80 |
typedef Item Parent; |
81 | 81 |
typedef typename Map::Value Value; |
82 | 82 |
|
83 | 83 |
MapIt() {} |
84 | 84 |
|
85 | 85 |
MapIt(Invalid i) : Parent(i) { } |
86 | 86 |
|
87 | 87 |
explicit MapIt(Map& _map) : map(_map) { |
88 | 88 |
map.notifier()->first(*this); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
MapIt(const Map& _map, const Item& item) |
92 | 92 |
: Parent(item), map(_map) {} |
93 | 93 |
|
94 | 94 |
MapIt& operator++() { |
95 | 95 |
map.notifier()->next(*this); |
96 | 96 |
return *this; |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
100 | 100 |
return map[*this]; |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
typename MapTraits<Map>::ReturnValue operator*() { |
104 | 104 |
return map[*this]; |
105 | 105 |
} |
106 | 106 |
|
107 | 107 |
void set(const Value& value) { |
108 | 108 |
map.set(*this, value); |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
protected: |
112 | 112 |
Map& map; |
113 | 113 |
|
114 | 114 |
}; |
115 | 115 |
|
116 | 116 |
class ConstMapIt : public Item { |
117 | 117 |
public: |
118 | 118 |
|
119 | 119 |
typedef Item Parent; |
120 | 120 |
|
121 | 121 |
typedef typename Map::Value Value; |
122 | 122 |
|
123 | 123 |
ConstMapIt() {} |
124 | 124 |
|
125 | 125 |
ConstMapIt(Invalid i) : Parent(i) { } |
126 | 126 |
|
127 | 127 |
explicit ConstMapIt(Map& _map) : map(_map) { |
128 | 128 |
map.notifier()->first(*this); |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
ConstMapIt(const Map& _map, const Item& item) |
132 | 132 |
: Parent(item), map(_map) {} |
133 | 133 |
|
134 | 134 |
ConstMapIt& operator++() { |
135 | 135 |
map.notifier()->next(*this); |
136 | 136 |
return *this; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
140 | 140 |
return map[*this]; |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
protected: |
144 | 144 |
const Map& map; |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
class ItemIt : public Item { |
148 | 148 |
public: |
149 | 149 |
|
150 | 150 |
typedef Item Parent; |
151 | 151 |
|
152 | 152 |
ItemIt() {} |
153 | 153 |
|
154 | 154 |
ItemIt(Invalid i) : Parent(i) { } |
155 | 155 |
|
156 | 156 |
explicit ItemIt(Map& _map) : map(_map) { |
157 | 157 |
map.notifier()->first(*this); |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
ItemIt(const Map& _map, const Item& item) |
161 | 161 |
: Parent(item), map(_map) {} |
162 | 162 |
|
163 | 163 |
ItemIt& operator++() { |
164 | 164 |
map.notifier()->next(*this); |
165 | 165 |
return *this; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
protected: |
169 | 169 |
const Map& map; |
170 | 170 |
|
171 | 171 |
}; |
172 | 172 |
}; |
173 | 173 |
|
174 | 174 |
// \ingroup graphbits |
175 | 175 |
// |
176 | 176 |
// \brief Extender for maps which use a subset of the items. |
177 | 177 |
template <typename _Graph, typename _Map> |
178 | 178 |
class SubMapExtender : public _Map { |
179 | 179 |
public: |
180 | 180 |
|
181 | 181 |
typedef _Map Parent; |
182 | 182 |
typedef SubMapExtender Map; |
183 | 183 |
|
184 | 184 |
typedef _Graph Graph; |
185 | 185 |
|
186 | 186 |
typedef typename Parent::Key Item; |
187 | 187 |
|
188 | 188 |
typedef typename Parent::Key Key; |
189 | 189 |
typedef typename Parent::Value Value; |
190 | 190 |
|
191 | 191 |
class MapIt; |
192 | 192 |
class ConstMapIt; |
193 | 193 |
|
194 | 194 |
friend class MapIt; |
195 | 195 |
friend class ConstMapIt; |
196 | 196 |
|
197 | 197 |
public: |
198 | 198 |
|
199 | 199 |
SubMapExtender(const Graph& _graph) |
200 | 200 |
: Parent(_graph), graph(_graph) {} |
201 | 201 |
|
202 | 202 |
SubMapExtender(const Graph& _graph, const Value& _value) |
203 | 203 |
: Parent(_graph, _value), graph(_graph) {} |
204 | 204 |
|
205 | 205 |
private: |
206 | 206 |
SubMapExtender& operator=(const SubMapExtender& cmap) { |
207 | 207 |
return operator=<MapExtender>(cmap); |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
template <typename CMap> |
211 | 211 |
SubMapExtender& operator=(const CMap& cmap) { |
212 | 212 |
checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
213 | 213 |
Item it; |
214 | 214 |
for (graph.first(it); it != INVALID; graph.next(it)) { |
215 | 215 |
Parent::set(it, cmap[it]); |
216 | 216 |
} |
217 | 217 |
return *this; |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
public: |
221 | 221 |
class MapIt : public Item { |
222 | 222 |
public: |
223 | 223 |
|
224 | 224 |
typedef Item Parent; |
225 | 225 |
typedef typename Map::Value Value; |
226 | 226 |
|
227 | 227 |
MapIt() {} |
228 | 228 |
|
229 | 229 |
MapIt(Invalid i) : Parent(i) { } |
230 | 230 |
|
231 | 231 |
explicit MapIt(Map& _map) : map(_map) { |
232 | 232 |
map.graph.first(*this); |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
MapIt(const Map& _map, const Item& item) |
236 | 236 |
: Parent(item), map(_map) {} |
237 | 237 |
|
238 | 238 |
MapIt& operator++() { |
239 | 239 |
map.graph.next(*this); |
240 | 240 |
return *this; |
241 | 241 |
} |
242 | 242 |
|
243 | 243 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
244 | 244 |
return map[*this]; |
245 | 245 |
} |
246 | 246 |
|
247 | 247 |
typename MapTraits<Map>::ReturnValue operator*() { |
248 | 248 |
return map[*this]; |
249 | 249 |
} |
250 | 250 |
|
251 | 251 |
void set(const Value& value) { |
252 | 252 |
map.set(*this, value); |
253 | 253 |
} |
254 | 254 |
|
255 | 255 |
protected: |
256 | 256 |
Map& map; |
257 | 257 |
|
258 | 258 |
}; |
259 | 259 |
|
260 | 260 |
class ConstMapIt : public Item { |
261 | 261 |
public: |
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-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_PRED_MAP_PATH_H |
20 | 20 |
#define LEMON_BITS_PRED_MAP_PATH_H |
21 | 21 |
|
22 | 22 |
namespace lemon { |
23 | 23 |
|
24 | 24 |
template <typename _Digraph, typename _PredMap> |
25 | 25 |
class PredMapPath { |
26 | 26 |
public: |
27 | 27 |
typedef True RevPathTag; |
28 | 28 |
|
29 | 29 |
typedef _Digraph Digraph; |
30 | 30 |
typedef typename Digraph::Arc Arc; |
31 | 31 |
typedef _PredMap PredMap; |
32 | 32 |
|
33 | 33 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
34 | 34 |
typename Digraph::Node _target) |
35 | 35 |
: digraph(_digraph), predMap(_predMap), target(_target) {} |
36 | 36 |
|
37 | 37 |
int length() const { |
38 | 38 |
int len = 0; |
39 | 39 |
typename Digraph::Node node = target; |
40 | 40 |
typename Digraph::Arc arc; |
41 | 41 |
while ((arc = predMap[node]) != INVALID) { |
42 | 42 |
node = digraph.source(arc); |
43 | 43 |
++len; |
44 | 44 |
} |
45 | 45 |
return len; |
46 | 46 |
} |
47 | 47 |
|
48 | 48 |
bool empty() const { |
49 | 49 |
return predMap[target] != INVALID; |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
class RevArcIt { |
53 | 53 |
public: |
54 | 54 |
RevArcIt() {} |
55 | 55 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
56 | 56 |
RevArcIt(const PredMapPath& _path) |
57 | 57 |
: path(&_path), current(_path.target) { |
58 | 58 |
if (path->predMap[current] == INVALID) current = INVALID; |
59 | 59 |
} |
60 | 60 |
|
61 | 61 |
operator const typename Digraph::Arc() const { |
62 | 62 |
return path->predMap[current]; |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
RevArcIt& operator++() { |
66 | 66 |
current = path->digraph.source(path->predMap[current]); |
67 | 67 |
if (path->predMap[current] == INVALID) current = INVALID; |
68 | 68 |
return *this; |
69 | 69 |
} |
70 | 70 |
|
71 | 71 |
bool operator==(const RevArcIt& e) const { |
72 | 72 |
return current == e.current; |
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
bool operator!=(const RevArcIt& e) const { |
76 | 76 |
return current != e.current; |
77 | 77 |
} |
78 | 78 |
|
79 | 79 |
bool operator<(const RevArcIt& e) const { |
80 | 80 |
return current < e.current; |
81 | 81 |
} |
82 | 82 |
|
83 | 83 |
private: |
84 | 84 |
const PredMapPath* path; |
85 | 85 |
typename Digraph::Node current; |
86 | 86 |
}; |
87 | 87 |
|
88 | 88 |
private: |
89 | 89 |
const Digraph& digraph; |
90 | 90 |
const PredMap& predMap; |
91 | 91 |
typename Digraph::Node target; |
92 | 92 |
}; |
93 | 93 |
|
94 | 94 |
|
95 | 95 |
template <typename _Digraph, typename _PredMatrixMap> |
96 | 96 |
class PredMatrixMapPath { |
97 | 97 |
public: |
98 | 98 |
typedef True RevPathTag; |
99 | 99 |
|
100 | 100 |
typedef _Digraph Digraph; |
101 | 101 |
typedef typename Digraph::Arc Arc; |
102 | 102 |
typedef _PredMatrixMap PredMatrixMap; |
103 | 103 |
|
104 | 104 |
PredMatrixMapPath(const Digraph& _digraph, |
105 | 105 |
const PredMatrixMap& _predMatrixMap, |
106 | 106 |
typename Digraph::Node _source, |
107 | 107 |
typename Digraph::Node _target) |
108 | 108 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
109 | 109 |
source(_source), target(_target) {} |
110 | 110 |
|
111 | 111 |
int length() const { |
112 | 112 |
int len = 0; |
113 | 113 |
typename Digraph::Node node = target; |
114 | 114 |
typename Digraph::Arc arc; |
115 | 115 |
while ((arc = predMatrixMap(source, node)) != INVALID) { |
116 | 116 |
node = digraph.source(arc); |
117 | 117 |
++len; |
118 | 118 |
} |
119 | 119 |
return len; |
120 | 120 |
} |
121 | 121 |
|
122 | 122 |
bool empty() const { |
123 | 123 |
return source != target; |
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
class RevArcIt { |
127 | 127 |
public: |
128 | 128 |
RevArcIt() {} |
129 | 129 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
130 | 130 |
RevArcIt(const PredMatrixMapPath& _path) |
131 | 131 |
: path(&_path), current(_path.target) { |
132 | 132 |
if (path->predMatrixMap(path->source, current) == INVALID) |
133 | 133 |
current = INVALID; |
134 | 134 |
} |
135 | 135 |
|
136 | 136 |
operator const typename Digraph::Arc() const { |
137 | 137 |
return path->predMatrixMap(path->source, current); |
138 | 138 |
} |
139 | 139 |
|
140 | 140 |
RevArcIt& operator++() { |
141 | 141 |
current = |
142 | 142 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
143 | 143 |
if (path->predMatrixMap(path->source, current) == INVALID) |
144 | 144 |
current = INVALID; |
145 | 145 |
return *this; |
146 | 146 |
} |
147 | 147 |
|
148 | 148 |
bool operator==(const RevArcIt& e) const { |
149 | 149 |
return current == e.current; |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
bool operator!=(const RevArcIt& e) const { |
153 | 153 |
return current != e.current; |
154 | 154 |
} |
155 | 155 |
|
156 | 156 |
bool operator<(const RevArcIt& e) const { |
157 | 157 |
return current < e.current; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
private: |
161 | 161 |
const PredMatrixMapPath* path; |
162 | 162 |
typename Digraph::Node current; |
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
private: |
166 | 166 |
const Digraph& digraph; |
167 | 167 |
const PredMatrixMap& predMatrixMap; |
168 | 168 |
typename Digraph::Node source; |
169 | 169 |
typename Digraph::Node target; |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
#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 |
* Copyright (C) 2003- |
|
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_TRAITS_H |
20 | 20 |
#define LEMON_BITS_TRAITS_H |
21 | 21 |
|
22 | 22 |
//\file |
23 | 23 |
//\brief Traits for graphs and maps |
24 | 24 |
// |
25 | 25 |
|
26 | 26 |
#include <lemon/bits/enable_if.h> |
27 | 27 |
|
28 | 28 |
namespace lemon { |
29 | 29 |
|
30 | 30 |
struct InvalidType {}; |
31 | 31 |
|
32 | 32 |
template <typename _Graph, typename _Item> |
33 | 33 |
class ItemSetTraits {}; |
34 | 34 |
|
35 | 35 |
|
36 | 36 |
template <typename Graph, typename Enable = void> |
37 | 37 |
struct NodeNotifierIndicator { |
38 | 38 |
typedef InvalidType Type; |
39 | 39 |
}; |
40 | 40 |
template <typename Graph> |
41 | 41 |
struct NodeNotifierIndicator< |
42 | 42 |
Graph, |
43 | 43 |
typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type |
44 | 44 |
> { |
45 | 45 |
typedef typename Graph::NodeNotifier Type; |
46 | 46 |
}; |
47 | 47 |
|
48 | 48 |
template <typename _Graph> |
49 | 49 |
class ItemSetTraits<_Graph, typename _Graph::Node> { |
50 | 50 |
public: |
51 | 51 |
|
52 | 52 |
typedef _Graph Graph; |
53 | 53 |
|
54 | 54 |
typedef typename Graph::Node Item; |
55 | 55 |
typedef typename Graph::NodeIt ItemIt; |
56 | 56 |
|
57 | 57 |
typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier; |
58 | 58 |
|
59 | 59 |
template <typename _Value> |
60 | 60 |
class Map : public Graph::template NodeMap<_Value> { |
61 | 61 |
public: |
62 | 62 |
typedef typename Graph::template NodeMap<_Value> Parent; |
63 | 63 |
typedef typename Graph::template NodeMap<_Value> Type; |
64 | 64 |
typedef typename Parent::Value Value; |
65 | 65 |
|
66 | 66 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
67 | 67 |
Map(const Graph& _digraph, const Value& _value) |
68 | 68 |
: Parent(_digraph, _value) {} |
69 | 69 |
|
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
}; |
73 | 73 |
|
74 | 74 |
template <typename Graph, typename Enable = void> |
75 | 75 |
struct ArcNotifierIndicator { |
76 | 76 |
typedef InvalidType Type; |
77 | 77 |
}; |
78 | 78 |
template <typename Graph> |
79 | 79 |
struct ArcNotifierIndicator< |
80 | 80 |
Graph, |
81 | 81 |
typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type |
82 | 82 |
> { |
83 | 83 |
typedef typename Graph::ArcNotifier Type; |
84 | 84 |
}; |
85 | 85 |
|
86 | 86 |
template <typename _Graph> |
87 | 87 |
class ItemSetTraits<_Graph, typename _Graph::Arc> { |
88 | 88 |
public: |
89 | 89 |
|
90 | 90 |
typedef _Graph Graph; |
91 | 91 |
|
92 | 92 |
typedef typename Graph::Arc Item; |
93 | 93 |
typedef typename Graph::ArcIt ItemIt; |
94 | 94 |
|
95 | 95 |
typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier; |
96 | 96 |
|
97 | 97 |
template <typename _Value> |
98 | 98 |
class Map : public Graph::template ArcMap<_Value> { |
99 | 99 |
public: |
100 | 100 |
typedef typename Graph::template ArcMap<_Value> Parent; |
101 | 101 |
typedef typename Graph::template ArcMap<_Value> Type; |
102 | 102 |
typedef typename Parent::Value Value; |
103 | 103 |
|
104 | 104 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
105 | 105 |
Map(const Graph& _digraph, const Value& _value) |
106 | 106 |
: Parent(_digraph, _value) {} |
107 | 107 |
}; |
108 | 108 |
|
109 | 109 |
}; |
110 | 110 |
|
111 | 111 |
template <typename Graph, typename Enable = void> |
112 | 112 |
struct EdgeNotifierIndicator { |
113 | 113 |
typedef InvalidType Type; |
114 | 114 |
}; |
115 | 115 |
template <typename Graph> |
116 | 116 |
struct EdgeNotifierIndicator< |
117 | 117 |
Graph, |
118 | 118 |
typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type |
119 | 119 |
> { |
120 | 120 |
typedef typename Graph::EdgeNotifier Type; |
121 | 121 |
}; |
122 | 122 |
|
123 | 123 |
template <typename _Graph> |
124 | 124 |
class ItemSetTraits<_Graph, typename _Graph::Edge> { |
125 | 125 |
public: |
126 | 126 |
|
127 | 127 |
typedef _Graph Graph; |
128 | 128 |
|
129 | 129 |
typedef typename Graph::Edge Item; |
130 | 130 |
typedef typename Graph::EdgeIt ItemIt; |
131 | 131 |
|
132 | 132 |
typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier; |
133 | 133 |
|
134 | 134 |
template <typename _Value> |
135 | 135 |
class Map : public Graph::template EdgeMap<_Value> { |
136 | 136 |
public: |
137 | 137 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
138 | 138 |
typedef typename Graph::template EdgeMap<_Value> Type; |
139 | 139 |
typedef typename Parent::Value Value; |
140 | 140 |
|
141 | 141 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
142 | 142 |
Map(const Graph& _digraph, const Value& _value) |
143 | 143 |
: Parent(_digraph, _value) {} |
144 | 144 |
}; |
145 | 145 |
|
146 | 146 |
}; |
147 | 147 |
|
148 | 148 |
template <typename Map, typename Enable = void> |
149 | 149 |
struct MapTraits { |
150 | 150 |
typedef False ReferenceMapTag; |
151 | 151 |
|
152 | 152 |
typedef typename Map::Key Key; |
153 | 153 |
typedef typename Map::Value Value; |
154 | 154 |
|
155 | 155 |
typedef Value ConstReturnValue; |
156 | 156 |
typedef Value ReturnValue; |
157 | 157 |
}; |
158 | 158 |
|
159 | 159 |
template <typename Map> |
160 | 160 |
struct MapTraits< |
161 | 161 |
Map, typename enable_if<typename Map::ReferenceMapTag, void>::type > |
162 | 162 |
{ |
163 | 163 |
typedef True ReferenceMapTag; |
164 | 164 |
|
165 | 165 |
typedef typename Map::Key Key; |
166 | 166 |
typedef typename Map::Value Value; |
167 | 167 |
|
168 | 168 |
typedef typename Map::ConstReference ConstReturnValue; |
169 | 169 |
typedef typename Map::Reference ReturnValue; |
170 | 170 |
|
171 | 171 |
typedef typename Map::ConstReference ConstReference; |
172 | 172 |
typedef typename Map::Reference Reference; |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
template <typename MatrixMap, typename Enable = void> |
176 | 176 |
struct MatrixMapTraits { |
177 | 177 |
typedef False ReferenceMapTag; |
178 | 178 |
|
179 | 179 |
typedef typename MatrixMap::FirstKey FirstKey; |
180 | 180 |
typedef typename MatrixMap::SecondKey SecondKey; |
181 | 181 |
typedef typename MatrixMap::Value Value; |
182 | 182 |
|
183 | 183 |
typedef Value ConstReturnValue; |
184 | 184 |
typedef Value ReturnValue; |
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
template <typename MatrixMap> |
188 | 188 |
struct MatrixMapTraits< |
189 | 189 |
MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag, |
190 | 190 |
void>::type > |
191 | 191 |
{ |
192 | 192 |
typedef True ReferenceMapTag; |
193 | 193 |
|
194 | 194 |
typedef typename MatrixMap::FirstKey FirstKey; |
195 | 195 |
typedef typename MatrixMap::SecondKey SecondKey; |
196 | 196 |
typedef typename MatrixMap::Value Value; |
197 | 197 |
|
198 | 198 |
typedef typename MatrixMap::ConstReference ConstReturnValue; |
199 | 199 |
typedef typename MatrixMap::Reference ReturnValue; |
200 | 200 |
|
201 | 201 |
typedef typename MatrixMap::ConstReference ConstReference; |
202 | 202 |
typedef typename MatrixMap::Reference Reference; |
203 | 203 |
}; |
204 | 204 |
|
205 | 205 |
// Indicators for the tags |
206 | 206 |
|
207 | 207 |
template <typename Graph, typename Enable = void> |
208 | 208 |
struct NodeNumTagIndicator { |
209 | 209 |
static const bool value = false; |
210 | 210 |
}; |
211 | 211 |
|
212 | 212 |
template <typename Graph> |
213 | 213 |
struct NodeNumTagIndicator< |
214 | 214 |
Graph, |
215 | 215 |
typename enable_if<typename Graph::NodeNumTag, void>::type |
216 | 216 |
> { |
217 | 217 |
static const bool value = true; |
218 | 218 |
}; |
219 | 219 |
|
220 | 220 |
template <typename Graph, typename Enable = void> |
221 | 221 |
struct ArcNumTagIndicator { |
222 | 222 |
static const bool value = false; |
223 | 223 |
}; |
224 | 224 |
|
225 | 225 |
template <typename Graph> |
226 | 226 |
struct ArcNumTagIndicator< |
227 | 227 |
Graph, |
228 | 228 |
typename enable_if<typename Graph::ArcNumTag, void>::type |
229 | 229 |
> { |
230 | 230 |
static const bool value = true; |
231 | 231 |
}; |
232 | 232 |
|
233 | 233 |
template <typename Graph, typename Enable = void> |
234 | 234 |
struct EdgeNumTagIndicator { |
235 | 235 |
static const bool value = false; |
236 | 236 |
}; |
237 | 237 |
|
238 | 238 |
template <typename Graph> |
239 | 239 |
struct EdgeNumTagIndicator< |
240 | 240 |
Graph, |
241 | 241 |
typename enable_if<typename Graph::EdgeNumTag, void>::type |
242 | 242 |
> { |
243 | 243 |
static const bool value = true; |
244 | 244 |
}; |
245 | 245 |
|
246 | 246 |
template <typename Graph, typename Enable = void> |
247 | 247 |
struct FindArcTagIndicator { |
248 | 248 |
static const bool value = false; |
249 | 249 |
}; |
250 | 250 |
|
251 | 251 |
template <typename Graph> |
252 | 252 |
struct FindArcTagIndicator< |
253 | 253 |
Graph, |
254 | 254 |
typename enable_if<typename Graph::FindArcTag, void>::type |
255 | 255 |
> { |
256 | 256 |
static const bool value = true; |
257 | 257 |
}; |
258 | 258 |
|
259 | 259 |
template <typename Graph, typename Enable = void> |
260 | 260 |
struct FindEdgeTagIndicator { |
261 | 261 |
static const bool value = false; |
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-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_VARIANT_H |
20 | 20 |
#define LEMON_BITS_VARIANT_H |
21 | 21 |
|
22 | 22 |
#include <lemon/assert.h> |
23 | 23 |
|
24 | 24 |
// \file |
25 | 25 |
// \brief Variant types |
26 | 26 |
|
27 | 27 |
namespace lemon { |
28 | 28 |
|
29 | 29 |
namespace _variant_bits { |
30 | 30 |
|
31 | 31 |
template <int left, int right> |
32 | 32 |
struct CTMax { |
33 | 33 |
static const int value = left < right ? right : left; |
34 | 34 |
}; |
35 | 35 |
|
36 | 36 |
} |
37 | 37 |
|
38 | 38 |
|
39 | 39 |
// \brief Simple Variant type for two types |
40 | 40 |
// |
41 | 41 |
// Simple Variant type for two types. The Variant type is a type-safe |
42 | 42 |
// union. C++ has strong limitations for using unions, for |
43 | 43 |
// example you cannot store a type with non-default constructor or |
44 | 44 |
// destructor in a union. This class always knowns the current |
45 | 45 |
// state of the variant and it cares for the proper construction |
46 | 46 |
// and destruction. |
47 | 47 |
template <typename _First, typename _Second> |
48 | 48 |
class BiVariant { |
49 | 49 |
public: |
50 | 50 |
|
51 | 51 |
// \brief The \c First type. |
52 | 52 |
typedef _First First; |
53 | 53 |
// \brief The \c Second type. |
54 | 54 |
typedef _Second Second; |
55 | 55 |
|
56 | 56 |
// \brief Constructor |
57 | 57 |
// |
58 | 58 |
// This constructor initalizes to the default value of the \c First |
59 | 59 |
// type. |
60 | 60 |
BiVariant() { |
61 | 61 |
flag = true; |
62 | 62 |
new(reinterpret_cast<First*>(data)) First(); |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
// \brief Constructor |
66 | 66 |
// |
67 | 67 |
// This constructor initalizes to the given value of the \c First |
68 | 68 |
// type. |
69 | 69 |
BiVariant(const First& f) { |
70 | 70 |
flag = true; |
71 | 71 |
new(reinterpret_cast<First*>(data)) First(f); |
72 | 72 |
} |
73 | 73 |
|
74 | 74 |
// \brief Constructor |
75 | 75 |
// |
76 | 76 |
// This constructor initalizes to the given value of the \c |
77 | 77 |
// Second type. |
78 | 78 |
BiVariant(const Second& s) { |
79 | 79 |
flag = false; |
80 | 80 |
new(reinterpret_cast<Second*>(data)) Second(s); |
81 | 81 |
} |
82 | 82 |
|
83 | 83 |
// \brief Copy constructor |
84 | 84 |
// |
85 | 85 |
// Copy constructor |
86 | 86 |
BiVariant(const BiVariant& bivariant) { |
87 | 87 |
flag = bivariant.flag; |
88 | 88 |
if (flag) { |
89 | 89 |
new(reinterpret_cast<First*>(data)) First(bivariant.first()); |
90 | 90 |
} else { |
91 | 91 |
new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); |
92 | 92 |
} |
93 | 93 |
} |
94 | 94 |
|
95 | 95 |
// \brief Destrcutor |
96 | 96 |
// |
97 | 97 |
// Destructor |
98 | 98 |
~BiVariant() { |
99 | 99 |
destroy(); |
100 | 100 |
} |
101 | 101 |
|
102 | 102 |
// \brief Set to the default value of the \c First type. |
103 | 103 |
// |
104 | 104 |
// This function sets the variant to the default value of the \c |
105 | 105 |
// First type. |
106 | 106 |
BiVariant& setFirst() { |
107 | 107 |
destroy(); |
108 | 108 |
flag = true; |
109 | 109 |
new(reinterpret_cast<First*>(data)) First(); |
110 | 110 |
return *this; |
111 | 111 |
} |
112 | 112 |
|
113 | 113 |
// \brief Set to the given value of the \c First type. |
114 | 114 |
// |
115 | 115 |
// This function sets the variant to the given value of the \c |
116 | 116 |
// First type. |
117 | 117 |
BiVariant& setFirst(const First& f) { |
118 | 118 |
destroy(); |
119 | 119 |
flag = true; |
120 | 120 |
new(reinterpret_cast<First*>(data)) First(f); |
121 | 121 |
return *this; |
122 | 122 |
} |
123 | 123 |
|
124 | 124 |
// \brief Set to the default value of the \c Second type. |
125 | 125 |
// |
126 | 126 |
// This function sets the variant to the default value of the \c |
127 | 127 |
// Second type. |
128 | 128 |
BiVariant& setSecond() { |
129 | 129 |
destroy(); |
130 | 130 |
flag = false; |
131 | 131 |
new(reinterpret_cast<Second*>(data)) Second(); |
132 | 132 |
return *this; |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
// \brief Set to the given value of the \c Second type. |
136 | 136 |
// |
137 | 137 |
// This function sets the variant to the given value of the \c |
138 | 138 |
// Second type. |
139 | 139 |
BiVariant& setSecond(const Second& s) { |
140 | 140 |
destroy(); |
141 | 141 |
flag = false; |
142 | 142 |
new(reinterpret_cast<Second*>(data)) Second(s); |
143 | 143 |
return *this; |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
// \brief Operator form of the \c setFirst() |
147 | 147 |
BiVariant& operator=(const First& f) { |
148 | 148 |
return setFirst(f); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
// \brief Operator form of the \c setSecond() |
152 | 152 |
BiVariant& operator=(const Second& s) { |
153 | 153 |
return setSecond(s); |
154 | 154 |
} |
155 | 155 |
|
156 | 156 |
// \brief Assign operator |
157 | 157 |
BiVariant& operator=(const BiVariant& bivariant) { |
158 | 158 |
if (this == &bivariant) return *this; |
159 | 159 |
destroy(); |
160 | 160 |
flag = bivariant.flag; |
161 | 161 |
if (flag) { |
162 | 162 |
new(reinterpret_cast<First*>(data)) First(bivariant.first()); |
163 | 163 |
} else { |
164 | 164 |
new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); |
165 | 165 |
} |
166 | 166 |
return *this; |
167 | 167 |
} |
168 | 168 |
|
169 | 169 |
// \brief Reference to the value |
170 | 170 |
// |
171 | 171 |
// Reference to the value of the \c First type. |
172 | 172 |
// \pre The BiVariant should store value of \c First type. |
173 | 173 |
First& first() { |
174 | 174 |
LEMON_DEBUG(flag, "Variant wrong state"); |
175 | 175 |
return *reinterpret_cast<First*>(data); |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
// \brief Const reference to the value |
179 | 179 |
// |
180 | 180 |
// Const reference to the value of the \c First type. |
181 | 181 |
// \pre The BiVariant should store value of \c First type. |
182 | 182 |
const First& first() const { |
183 | 183 |
LEMON_DEBUG(flag, "Variant wrong state"); |
184 | 184 |
return *reinterpret_cast<const First*>(data); |
185 | 185 |
} |
186 | 186 |
|
187 | 187 |
// \brief Operator form of the \c first() |
188 | 188 |
operator First&() { return first(); } |
189 | 189 |
// \brief Operator form of the const \c first() |
190 | 190 |
operator const First&() const { return first(); } |
191 | 191 |
|
192 | 192 |
// \brief Reference to the value |
193 | 193 |
// |
194 | 194 |
// Reference to the value of the \c Second type. |
195 | 195 |
// \pre The BiVariant should store value of \c Second type. |
196 | 196 |
Second& second() { |
197 | 197 |
LEMON_DEBUG(!flag, "Variant wrong state"); |
198 | 198 |
return *reinterpret_cast<Second*>(data); |
199 | 199 |
} |
200 | 200 |
|
201 | 201 |
// \brief Const reference to the value |
202 | 202 |
// |
203 | 203 |
// Const reference to the value of the \c Second type. |
204 | 204 |
// \pre The BiVariant should store value of \c Second type. |
205 | 205 |
const Second& second() const { |
206 | 206 |
LEMON_DEBUG(!flag, "Variant wrong state"); |
207 | 207 |
return *reinterpret_cast<const Second*>(data); |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
// \brief Operator form of the \c second() |
211 | 211 |
operator Second&() { return second(); } |
212 | 212 |
// \brief Operator form of the const \c second() |
213 | 213 |
operator const Second&() const { return second(); } |
214 | 214 |
|
215 | 215 |
// \brief %True when the variant is in the first state |
216 | 216 |
// |
217 | 217 |
// %True when the variant stores value of the \c First type. |
218 | 218 |
bool firstState() const { return flag; } |
219 | 219 |
|
220 | 220 |
// \brief %True when the variant is in the second state |
221 | 221 |
// |
222 | 222 |
// %True when the variant stores value of the \c Second type. |
223 | 223 |
bool secondState() const { return !flag; } |
224 | 224 |
|
225 | 225 |
private: |
226 | 226 |
|
227 | 227 |
void destroy() { |
228 | 228 |
if (flag) { |
229 | 229 |
reinterpret_cast<First*>(data)->~First(); |
230 | 230 |
} else { |
231 | 231 |
reinterpret_cast<Second*>(data)->~Second(); |
232 | 232 |
} |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
char data[_variant_bits::CTMax<sizeof(First), sizeof(Second)>::value]; |
236 | 236 |
bool flag; |
237 | 237 |
}; |
238 | 238 |
|
239 | 239 |
namespace _variant_bits { |
240 | 240 |
|
241 | 241 |
template <int _idx, typename _TypeMap> |
242 | 242 |
struct Memory { |
243 | 243 |
|
244 | 244 |
typedef typename _TypeMap::template Map<_idx>::Type Current; |
245 | 245 |
|
246 | 246 |
static void destroy(int index, char* place) { |
247 | 247 |
if (index == _idx) { |
248 | 248 |
reinterpret_cast<Current*>(place)->~Current(); |
249 | 249 |
} else { |
250 | 250 |
Memory<_idx - 1, _TypeMap>::destroy(index, place); |
251 | 251 |
} |
252 | 252 |
} |
253 | 253 |
|
254 | 254 |
static void copy(int index, char* to, const char* from) { |
255 | 255 |
if (index == _idx) { |
256 | 256 |
new (reinterpret_cast<Current*>(to)) |
257 | 257 |
Current(reinterpret_cast<const Current*>(from)); |
258 | 258 |
} else { |
259 | 259 |
Memory<_idx - 1, _TypeMap>::copy(index, to, from); |
260 | 260 |
} |
261 | 261 |
} |
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-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_VECTOR_MAP_H |
20 | 20 |
#define LEMON_BITS_VECTOR_MAP_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/core.h> |
26 | 26 |
#include <lemon/bits/alteration_notifier.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 | 31 |
//\ingroup graphbits |
32 | 32 |
// |
33 | 33 |
//\file |
34 | 34 |
//\brief Vector based graph maps. |
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
// \ingroup graphbits |
38 | 38 |
// |
39 | 39 |
// \brief Graph map based on the std::vector storage. |
40 | 40 |
// |
41 | 41 |
// The VectorMap template class is graph map structure that automatically |
42 | 42 |
// updates the map when a key is added to or erased from the graph. |
43 | 43 |
// This map type uses std::vector to store the values. |
44 | 44 |
// |
45 | 45 |
// \tparam _Graph The graph this map is attached to. |
46 | 46 |
// \tparam _Item The item type of the graph items. |
47 | 47 |
// \tparam _Value The value type of the map. |
48 | 48 |
template <typename _Graph, typename _Item, typename _Value> |
49 | 49 |
class VectorMap |
50 | 50 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
51 | 51 |
private: |
52 | 52 |
|
53 | 53 |
// The container type of the map. |
54 | 54 |
typedef std::vector<_Value> Container; |
55 | 55 |
|
56 | 56 |
public: |
57 | 57 |
|
58 | 58 |
// The graph type of the map. |
59 | 59 |
typedef _Graph Graph; |
60 | 60 |
// The item type of the map. |
61 | 61 |
typedef _Item Item; |
62 | 62 |
// The reference map tag. |
63 | 63 |
typedef True ReferenceMapTag; |
64 | 64 |
|
65 | 65 |
// The key type of the map. |
66 | 66 |
typedef _Item Key; |
67 | 67 |
// The value type of the map. |
68 | 68 |
typedef _Value Value; |
69 | 69 |
|
70 | 70 |
// The notifier type. |
71 | 71 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
72 | 72 |
|
73 | 73 |
// The map type. |
74 | 74 |
typedef VectorMap Map; |
75 | 75 |
// The base class of the map. |
76 | 76 |
typedef typename Notifier::ObserverBase Parent; |
77 | 77 |
|
78 | 78 |
// The reference type of the map; |
79 | 79 |
typedef typename Container::reference Reference; |
80 | 80 |
// The const reference type of the map; |
81 | 81 |
typedef typename Container::const_reference ConstReference; |
82 | 82 |
|
83 | 83 |
|
84 | 84 |
// \brief Constructor to attach the new map into the notifier. |
85 | 85 |
// |
86 | 86 |
// It constructs a map and attachs it into the notifier. |
87 | 87 |
// It adds all the items of the graph to the map. |
88 | 88 |
VectorMap(const Graph& graph) { |
89 | 89 |
Parent::attach(graph.notifier(Item())); |
90 | 90 |
container.resize(Parent::notifier()->maxId() + 1); |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
// \brief Constructor uses given value to initialize the map. |
94 | 94 |
// |
95 | 95 |
// It constructs a map uses a given value to initialize the map. |
96 | 96 |
// It adds all the items of the graph to the map. |
97 | 97 |
VectorMap(const Graph& graph, const Value& value) { |
98 | 98 |
Parent::attach(graph.notifier(Item())); |
99 | 99 |
container.resize(Parent::notifier()->maxId() + 1, value); |
100 | 100 |
} |
101 | 101 |
|
102 | 102 |
private: |
103 | 103 |
// \brief Copy constructor |
104 | 104 |
// |
105 | 105 |
// Copy constructor. |
106 | 106 |
VectorMap(const VectorMap& _copy) : Parent() { |
107 | 107 |
if (_copy.attached()) { |
108 | 108 |
Parent::attach(*_copy.notifier()); |
109 | 109 |
container = _copy.container; |
110 | 110 |
} |
111 | 111 |
} |
112 | 112 |
|
113 | 113 |
// \brief Assign operator. |
114 | 114 |
// |
115 | 115 |
// This operator assigns for each item in the map the |
116 | 116 |
// value mapped to the same item in the copied map. |
117 | 117 |
// The parameter map should be indiced with the same |
118 | 118 |
// itemset because this assign operator does not change |
119 | 119 |
// the container of the map. |
120 | 120 |
VectorMap& operator=(const VectorMap& cmap) { |
121 | 121 |
return operator=<VectorMap>(cmap); |
122 | 122 |
} |
123 | 123 |
|
124 | 124 |
|
125 | 125 |
// \brief Template assign operator. |
126 | 126 |
// |
127 | 127 |
// The given parameter should be conform to the ReadMap |
128 | 128 |
// concecpt and could be indiced by the current item set of |
129 | 129 |
// the NodeMap. In this case the value for each item |
130 | 130 |
// is assigned by the value of the given ReadMap. |
131 | 131 |
template <typename CMap> |
132 | 132 |
VectorMap& operator=(const CMap& cmap) { |
133 | 133 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
134 | 134 |
const typename Parent::Notifier* nf = Parent::notifier(); |
135 | 135 |
Item it; |
136 | 136 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
137 | 137 |
set(it, cmap[it]); |
138 | 138 |
} |
139 | 139 |
return *this; |
140 | 140 |
} |
141 | 141 |
|
142 | 142 |
public: |
143 | 143 |
|
144 | 144 |
// \brief The subcript operator. |
145 | 145 |
// |
146 | 146 |
// The subscript operator. The map can be subscripted by the |
147 | 147 |
// actual items of the graph. |
148 | 148 |
Reference operator[](const Key& key) { |
149 | 149 |
return container[Parent::notifier()->id(key)]; |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
// \brief The const subcript operator. |
153 | 153 |
// |
154 | 154 |
// The const subscript operator. The map can be subscripted by the |
155 | 155 |
// actual items of the graph. |
156 | 156 |
ConstReference operator[](const Key& key) const { |
157 | 157 |
return container[Parent::notifier()->id(key)]; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
|
161 | 161 |
// \brief The setter function of the map. |
162 | 162 |
// |
163 | 163 |
// It the same as operator[](key) = value expression. |
164 | 164 |
void set(const Key& key, const Value& value) { |
165 | 165 |
(*this)[key] = value; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
protected: |
169 | 169 |
|
170 | 170 |
// \brief Adds a new key to the map. |
171 | 171 |
// |
172 | 172 |
// It adds a new key to the map. It is called by the observer notifier |
173 | 173 |
// and it overrides the add() member function of the observer base. |
174 | 174 |
virtual void add(const Key& key) { |
175 | 175 |
int id = Parent::notifier()->id(key); |
176 | 176 |
if (id >= int(container.size())) { |
177 | 177 |
container.resize(id + 1); |
178 | 178 |
} |
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
// \brief Adds more new keys to the map. |
182 | 182 |
// |
183 | 183 |
// It adds more new keys to the map. It is called by the observer notifier |
184 | 184 |
// and it overrides the add() member function of the observer base. |
185 | 185 |
virtual void add(const std::vector<Key>& keys) { |
186 | 186 |
int max = container.size() - 1; |
187 | 187 |
for (int i = 0; i < int(keys.size()); ++i) { |
188 | 188 |
int id = Parent::notifier()->id(keys[i]); |
189 | 189 |
if (id >= max) { |
190 | 190 |
max = id; |
191 | 191 |
} |
192 | 192 |
} |
193 | 193 |
container.resize(max + 1); |
194 | 194 |
} |
195 | 195 |
|
196 | 196 |
// \brief Erase a key from the map. |
197 | 197 |
// |
198 | 198 |
// Erase a key from the map. It is called by the observer notifier |
199 | 199 |
// and it overrides the erase() member function of the observer base. |
200 | 200 |
virtual void erase(const Key& key) { |
201 | 201 |
container[Parent::notifier()->id(key)] = Value(); |
202 | 202 |
} |
203 | 203 |
|
204 | 204 |
// \brief Erase more keys from the map. |
205 | 205 |
// |
206 | 206 |
// It erases more keys from the map. It is called by the observer notifier |
207 | 207 |
// and it overrides the erase() member function of the observer base. |
208 | 208 |
virtual void erase(const std::vector<Key>& keys) { |
209 | 209 |
for (int i = 0; i < int(keys.size()); ++i) { |
210 | 210 |
container[Parent::notifier()->id(keys[i])] = Value(); |
211 | 211 |
} |
212 | 212 |
} |
213 | 213 |
|
214 | 214 |
// \brief Build the map. |
215 | 215 |
// |
216 | 216 |
// It builds the map. It is called by the observer notifier |
217 | 217 |
// and it overrides the build() member function of the observer base. |
218 | 218 |
virtual void build() { |
219 | 219 |
int size = Parent::notifier()->maxId() + 1; |
220 | 220 |
container.reserve(size); |
221 | 221 |
container.resize(size); |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
// \brief Clear the map. |
225 | 225 |
// |
226 | 226 |
// It erases all items from the map. It is called by the observer notifier |
227 | 227 |
// and it overrides the clear() member function of the observer base. |
228 | 228 |
virtual void clear() { |
229 | 229 |
container.clear(); |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
private: |
233 | 233 |
|
234 | 234 |
Container container; |
235 | 235 |
|
236 | 236 |
}; |
237 | 237 |
|
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
#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 |
* Copyright (C) 2003- |
|
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_CIRCULATION_H |
20 | 20 |
#define LEMON_CIRCULATION_H |
21 | 21 |
|
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
#include <lemon/elevator.h> |
24 | 24 |
|
25 | 25 |
///\ingroup max_flow |
26 | 26 |
///\file |
27 | 27 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
28 | 28 |
/// |
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
/// \brief Default traits class of Circulation class. |
32 | 32 |
/// |
33 | 33 |
/// Default traits class of Circulation class. |
34 | 34 |
/// \tparam _Diraph Digraph type. |
35 | 35 |
/// \tparam _LCapMap Lower bound capacity map type. |
36 | 36 |
/// \tparam _UCapMap Upper bound capacity map type. |
37 | 37 |
/// \tparam _DeltaMap Delta map type. |
38 | 38 |
template <typename _Diraph, typename _LCapMap, |
39 | 39 |
typename _UCapMap, typename _DeltaMap> |
40 | 40 |
struct CirculationDefaultTraits { |
41 | 41 |
|
42 | 42 |
/// \brief The type of the digraph the algorithm runs on. |
43 | 43 |
typedef _Diraph Digraph; |
44 | 44 |
|
45 | 45 |
/// \brief The type of the map that stores the circulation lower |
46 | 46 |
/// bound. |
47 | 47 |
/// |
48 | 48 |
/// The type of the map that stores the circulation lower bound. |
49 | 49 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
50 | 50 |
typedef _LCapMap LCapMap; |
51 | 51 |
|
52 | 52 |
/// \brief The type of the map that stores the circulation upper |
53 | 53 |
/// bound. |
54 | 54 |
/// |
55 | 55 |
/// The type of the map that stores the circulation upper bound. |
56 | 56 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
57 | 57 |
typedef _UCapMap UCapMap; |
58 | 58 |
|
59 | 59 |
/// \brief The type of the map that stores the lower bound for |
60 | 60 |
/// the supply of the nodes. |
61 | 61 |
/// |
62 | 62 |
/// The type of the map that stores the lower bound for the supply |
63 | 63 |
/// of the nodes. It must meet the \ref concepts::ReadMap "ReadMap" |
64 | 64 |
/// concept. |
65 | 65 |
typedef _DeltaMap DeltaMap; |
66 | 66 |
|
67 | 67 |
/// \brief The type of the flow values. |
68 | 68 |
typedef typename DeltaMap::Value Value; |
69 | 69 |
|
70 | 70 |
/// \brief The type of the map that stores the flow values. |
71 | 71 |
/// |
72 | 72 |
/// The type of the map that stores the flow values. |
73 | 73 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
74 | 74 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
75 | 75 |
|
76 | 76 |
/// \brief Instantiates a FlowMap. |
77 | 77 |
/// |
78 | 78 |
/// This function instantiates a \ref FlowMap. |
79 | 79 |
/// \param digraph The digraph, to which we would like to define |
80 | 80 |
/// the flow map. |
81 | 81 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
82 | 82 |
return new FlowMap(digraph); |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
/// \brief The elevator type used by the algorithm. |
86 | 86 |
/// |
87 | 87 |
/// The elevator type used by the algorithm. |
88 | 88 |
/// |
89 | 89 |
/// \sa Elevator |
90 | 90 |
/// \sa LinkedElevator |
91 | 91 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
92 | 92 |
|
93 | 93 |
/// \brief Instantiates an Elevator. |
94 | 94 |
/// |
95 | 95 |
/// This function instantiates an \ref Elevator. |
96 | 96 |
/// \param digraph The digraph, to which we would like to define |
97 | 97 |
/// the elevator. |
98 | 98 |
/// \param max_level The maximum level of the elevator. |
99 | 99 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
100 | 100 |
return new Elevator(digraph, max_level); |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
/// \brief The tolerance used by the algorithm |
104 | 104 |
/// |
105 | 105 |
/// The tolerance used by the algorithm to handle inexact computation. |
106 | 106 |
typedef lemon::Tolerance<Value> Tolerance; |
107 | 107 |
|
108 | 108 |
}; |
109 | 109 |
|
110 | 110 |
/** |
111 | 111 |
\brief Push-relabel algorithm for the network circulation problem. |
112 | 112 |
|
113 | 113 |
\ingroup max_flow |
114 | 114 |
This class implements a push-relabel algorithm for the network |
115 | 115 |
circulation problem. |
116 | 116 |
It is to find a feasible circulation when lower and upper bounds |
117 | 117 |
are given for the flow values on the arcs and lower bounds |
118 | 118 |
are given for the supply values of the nodes. |
119 | 119 |
|
120 | 120 |
The exact formulation of this problem is the following. |
121 | 121 |
Let \f$G=(V,A)\f$ be a digraph, |
122 | 122 |
\f$lower, upper: A\rightarrow\mathbf{R}^+_0\f$, |
123 | 123 |
\f$delta: V\rightarrow\mathbf{R}\f$. Find a feasible circulation |
124 | 124 |
\f$f: A\rightarrow\mathbf{R}^+_0\f$ so that |
125 | 125 |
\f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a) |
126 | 126 |
\geq delta(v) \quad \forall v\in V, \f] |
127 | 127 |
\f[ lower(a)\leq f(a) \leq upper(a) \quad \forall a\in A. \f] |
128 | 128 |
\note \f$delta(v)\f$ specifies a lower bound for the supply of node |
129 | 129 |
\f$v\f$. It can be either positive or negative, however note that |
130 | 130 |
\f$\sum_{v\in V}delta(v)\f$ should be zero or negative in order to |
131 | 131 |
have a feasible solution. |
132 | 132 |
|
133 | 133 |
\note A special case of this problem is when |
134 | 134 |
\f$\sum_{v\in V}delta(v) = 0\f$. Then the supply of each node \f$v\f$ |
135 | 135 |
will be \e equal \e to \f$delta(v)\f$, if a circulation can be found. |
136 | 136 |
Thus a feasible solution for the |
137 | 137 |
\ref min_cost_flow "minimum cost flow" problem can be calculated |
138 | 138 |
in this way. |
139 | 139 |
|
140 | 140 |
\tparam _Digraph The type of the digraph the algorithm runs on. |
141 | 141 |
\tparam _LCapMap The type of the lower bound capacity map. The default |
142 | 142 |
map type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>". |
143 | 143 |
\tparam _UCapMap The type of the upper bound capacity map. The default |
144 | 144 |
map type is \c _LCapMap. |
145 | 145 |
\tparam _DeltaMap The type of the map that stores the lower bound |
146 | 146 |
for the supply of the nodes. The default map type is |
147 | 147 |
\c _Digraph::ArcMap<_UCapMap::Value>. |
148 | 148 |
*/ |
149 | 149 |
#ifdef DOXYGEN |
150 | 150 |
template< typename _Digraph, |
151 | 151 |
typename _LCapMap, |
152 | 152 |
typename _UCapMap, |
153 | 153 |
typename _DeltaMap, |
154 | 154 |
typename _Traits > |
155 | 155 |
#else |
156 | 156 |
template< typename _Digraph, |
157 | 157 |
typename _LCapMap = typename _Digraph::template ArcMap<int>, |
158 | 158 |
typename _UCapMap = _LCapMap, |
159 | 159 |
typename _DeltaMap = typename _Digraph:: |
160 | 160 |
template NodeMap<typename _UCapMap::Value>, |
161 | 161 |
typename _Traits=CirculationDefaultTraits<_Digraph, _LCapMap, |
162 | 162 |
_UCapMap, _DeltaMap> > |
163 | 163 |
#endif |
164 | 164 |
class Circulation { |
165 | 165 |
public: |
166 | 166 |
|
167 | 167 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
168 | 168 |
typedef _Traits Traits; |
169 | 169 |
///The type of the digraph the algorithm runs on. |
170 | 170 |
typedef typename Traits::Digraph Digraph; |
171 | 171 |
///The type of the flow values. |
172 | 172 |
typedef typename Traits::Value Value; |
173 | 173 |
|
174 | 174 |
/// The type of the lower bound capacity map. |
175 | 175 |
typedef typename Traits::LCapMap LCapMap; |
176 | 176 |
/// The type of the upper bound capacity map. |
177 | 177 |
typedef typename Traits::UCapMap UCapMap; |
178 | 178 |
/// \brief The type of the map that stores the lower bound for |
179 | 179 |
/// the supply of the nodes. |
180 | 180 |
typedef typename Traits::DeltaMap DeltaMap; |
181 | 181 |
///The type of the flow map. |
182 | 182 |
typedef typename Traits::FlowMap FlowMap; |
183 | 183 |
|
184 | 184 |
///The type of the elevator. |
185 | 185 |
typedef typename Traits::Elevator Elevator; |
186 | 186 |
///The type of the tolerance. |
187 | 187 |
typedef typename Traits::Tolerance Tolerance; |
188 | 188 |
|
189 | 189 |
private: |
190 | 190 |
|
191 | 191 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
192 | 192 |
|
193 | 193 |
const Digraph &_g; |
194 | 194 |
int _node_num; |
195 | 195 |
|
196 | 196 |
const LCapMap *_lo; |
197 | 197 |
const UCapMap *_up; |
198 | 198 |
const DeltaMap *_delta; |
199 | 199 |
|
200 | 200 |
FlowMap *_flow; |
201 | 201 |
bool _local_flow; |
202 | 202 |
|
203 | 203 |
Elevator* _level; |
204 | 204 |
bool _local_level; |
205 | 205 |
|
206 | 206 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
207 | 207 |
ExcessMap* _excess; |
208 | 208 |
|
209 | 209 |
Tolerance _tol; |
210 | 210 |
int _el; |
211 | 211 |
|
212 | 212 |
public: |
213 | 213 |
|
214 | 214 |
typedef Circulation Create; |
215 | 215 |
|
216 | 216 |
///\name Named Template Parameters |
217 | 217 |
|
218 | 218 |
///@{ |
219 | 219 |
|
220 | 220 |
template <typename _FlowMap> |
221 | 221 |
struct SetFlowMapTraits : public Traits { |
222 | 222 |
typedef _FlowMap FlowMap; |
223 | 223 |
static FlowMap *createFlowMap(const Digraph&) { |
224 | 224 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
225 | 225 |
return 0; // ignore warnings |
226 | 226 |
} |
227 | 227 |
}; |
228 | 228 |
|
229 | 229 |
/// \brief \ref named-templ-param "Named parameter" for setting |
230 | 230 |
/// FlowMap type |
231 | 231 |
/// |
232 | 232 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
233 | 233 |
/// type. |
234 | 234 |
template <typename _FlowMap> |
235 | 235 |
struct SetFlowMap |
236 | 236 |
: public Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
237 | 237 |
SetFlowMapTraits<_FlowMap> > { |
238 | 238 |
typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
239 | 239 |
SetFlowMapTraits<_FlowMap> > Create; |
240 | 240 |
}; |
241 | 241 |
|
242 | 242 |
template <typename _Elevator> |
243 | 243 |
struct SetElevatorTraits : public Traits { |
244 | 244 |
typedef _Elevator Elevator; |
245 | 245 |
static Elevator *createElevator(const Digraph&, int) { |
246 | 246 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
247 | 247 |
return 0; // ignore warnings |
248 | 248 |
} |
249 | 249 |
}; |
250 | 250 |
|
251 | 251 |
/// \brief \ref named-templ-param "Named parameter" for setting |
252 | 252 |
/// Elevator type |
253 | 253 |
/// |
254 | 254 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
255 | 255 |
/// type. If this named parameter is used, then an external |
256 | 256 |
/// elevator object must be passed to the algorithm using the |
257 | 257 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
258 | 258 |
/// \ref run() or \ref init(). |
259 | 259 |
/// \sa SetStandardElevator |
260 | 260 |
template <typename _Elevator> |
261 | 261 |
struct SetElevator |
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-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 |
///\file |
20 | 20 |
///\brief Color constants |
21 | 21 |
|
22 | 22 |
#include<lemon/color.h> |
23 | 23 |
|
24 | 24 |
namespace lemon { |
25 | 25 |
|
26 | 26 |
const Color WHITE(1,1,1); |
27 | 27 |
|
28 | 28 |
const Color BLACK(0,0,0); |
29 | 29 |
const Color RED(1,0,0); |
30 | 30 |
const Color GREEN(0,1,0); |
31 | 31 |
const Color BLUE(0,0,1); |
32 | 32 |
const Color YELLOW(1,1,0); |
33 | 33 |
const Color MAGENTA(1,0,1); |
34 | 34 |
const Color CYAN(0,1,1); |
35 | 35 |
|
36 | 36 |
const Color GREY(0,0,0); |
37 | 37 |
const Color DARK_RED(.5,0,0); |
38 | 38 |
const Color DARK_GREEN(0,.5,0); |
39 | 39 |
const Color DARK_BLUE(0,0,.5); |
40 | 40 |
const Color DARK_YELLOW(.5,.5,0); |
41 | 41 |
const Color DARK_MAGENTA(.5,0,.5); |
42 | 42 |
const Color DARK_CYAN(0,.5,.5); |
43 | 43 |
|
44 | 44 |
} //namespace lemon |
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-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_COLOR_H |
20 | 20 |
#define LEMON_COLOR_H |
21 | 21 |
|
22 | 22 |
#include<vector> |
23 | 23 |
#include<lemon/math.h> |
24 | 24 |
#include<lemon/maps.h> |
25 | 25 |
|
26 | 26 |
|
27 | 27 |
///\ingroup misc |
28 | 28 |
///\file |
29 | 29 |
///\brief Tools to manage RGB colors. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
|
34 | 34 |
/// \addtogroup misc |
35 | 35 |
/// @{ |
36 | 36 |
|
37 | 37 |
///Data structure representing RGB colors. |
38 | 38 |
|
39 | 39 |
///Data structure representing RGB colors. |
40 | 40 |
class Color |
41 | 41 |
{ |
42 | 42 |
double _r,_g,_b; |
43 | 43 |
public: |
44 | 44 |
///Default constructor |
45 | 45 |
Color() {} |
46 | 46 |
///Constructor |
47 | 47 |
Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; |
48 | 48 |
///Set the red component |
49 | 49 |
double & red() {return _r;} |
50 | 50 |
///Return the red component |
51 | 51 |
const double & red() const {return _r;} |
52 | 52 |
///Set the green component |
53 | 53 |
double & green() {return _g;} |
54 | 54 |
///Return the green component |
55 | 55 |
const double & green() const {return _g;} |
56 | 56 |
///Set the blue component |
57 | 57 |
double & blue() {return _b;} |
58 | 58 |
///Return the blue component |
59 | 59 |
const double & blue() const {return _b;} |
60 | 60 |
///Set the color components |
61 | 61 |
void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; |
62 | 62 |
}; |
63 | 63 |
|
64 | 64 |
/// White color constant |
65 | 65 |
extern const Color WHITE; |
66 | 66 |
/// Black color constant |
67 | 67 |
extern const Color BLACK; |
68 | 68 |
/// Red color constant |
69 | 69 |
extern const Color RED; |
70 | 70 |
/// Green color constant |
71 | 71 |
extern const Color GREEN; |
72 | 72 |
/// Blue color constant |
73 | 73 |
extern const Color BLUE; |
74 | 74 |
/// Yellow color constant |
75 | 75 |
extern const Color YELLOW; |
76 | 76 |
/// Magenta color constant |
77 | 77 |
extern const Color MAGENTA; |
78 | 78 |
/// Cyan color constant |
79 | 79 |
extern const Color CYAN; |
80 | 80 |
/// Grey color constant |
81 | 81 |
extern const Color GREY; |
82 | 82 |
/// Dark red color constant |
83 | 83 |
extern const Color DARK_RED; |
84 | 84 |
/// Dark green color constant |
85 | 85 |
extern const Color DARK_GREEN; |
86 | 86 |
/// Drak blue color constant |
87 | 87 |
extern const Color DARK_BLUE; |
88 | 88 |
/// Dark yellow color constant |
89 | 89 |
extern const Color DARK_YELLOW; |
90 | 90 |
/// Dark magenta color constant |
91 | 91 |
extern const Color DARK_MAGENTA; |
92 | 92 |
/// Dark cyan color constant |
93 | 93 |
extern const Color DARK_CYAN; |
94 | 94 |
|
95 | 95 |
///Map <tt>int</tt>s to different <tt>Color</tt>s |
96 | 96 |
|
97 | 97 |
///This map assigns one of the predefined \ref Color "Color"s to |
98 | 98 |
///each <tt>int</tt>. It is possible to change the colors as well as |
99 | 99 |
///their number. The integer range is cyclically mapped to the |
100 | 100 |
///provided set of colors. |
101 | 101 |
/// |
102 | 102 |
///This is a true \ref concepts::ReferenceMap "reference map", so |
103 | 103 |
///you can also change the actual colors. |
104 | 104 |
|
105 | 105 |
class Palette : public MapBase<int,Color> |
106 | 106 |
{ |
107 | 107 |
std::vector<Color> colors; |
108 | 108 |
public: |
109 | 109 |
///Constructor |
110 | 110 |
|
111 | 111 |
///Constructor. |
112 | 112 |
///\param have_white Indicates whether white is among the |
113 | 113 |
///provided initial colors (\c true) or not (\c false). If it is true, |
114 | 114 |
///white will be assigned to \c 0. |
115 | 115 |
///\param num The number of the allocated colors. If it is \c -1, |
116 | 116 |
///the default color configuration is set up (26 color plus optionaly the |
117 | 117 |
///white). If \c num is less then 26/27 then the default color |
118 | 118 |
///list is cut. Otherwise the color list is filled repeatedly with |
119 | 119 |
///the default color list. (The colors can be changed later on.) |
120 | 120 |
Palette(bool have_white=false,int num=-1) |
121 | 121 |
{ |
122 | 122 |
if (num==0) return; |
123 | 123 |
do { |
124 | 124 |
if(have_white) colors.push_back(Color(1,1,1)); |
125 | 125 |
|
126 | 126 |
colors.push_back(Color(0,0,0)); |
127 | 127 |
colors.push_back(Color(1,0,0)); |
128 | 128 |
colors.push_back(Color(0,1,0)); |
129 | 129 |
colors.push_back(Color(0,0,1)); |
130 | 130 |
colors.push_back(Color(1,1,0)); |
131 | 131 |
colors.push_back(Color(1,0,1)); |
132 | 132 |
colors.push_back(Color(0,1,1)); |
133 | 133 |
|
134 | 134 |
colors.push_back(Color(.5,0,0)); |
135 | 135 |
colors.push_back(Color(0,.5,0)); |
136 | 136 |
colors.push_back(Color(0,0,.5)); |
137 | 137 |
colors.push_back(Color(.5,.5,0)); |
138 | 138 |
colors.push_back(Color(.5,0,.5)); |
139 | 139 |
colors.push_back(Color(0,.5,.5)); |
140 | 140 |
|
141 | 141 |
colors.push_back(Color(.5,.5,.5)); |
142 | 142 |
colors.push_back(Color(1,.5,.5)); |
143 | 143 |
colors.push_back(Color(.5,1,.5)); |
144 | 144 |
colors.push_back(Color(.5,.5,1)); |
145 | 145 |
colors.push_back(Color(1,1,.5)); |
146 | 146 |
colors.push_back(Color(1,.5,1)); |
147 | 147 |
colors.push_back(Color(.5,1,1)); |
148 | 148 |
|
149 | 149 |
colors.push_back(Color(1,.5,0)); |
150 | 150 |
colors.push_back(Color(.5,1,0)); |
151 | 151 |
colors.push_back(Color(1,0,.5)); |
152 | 152 |
colors.push_back(Color(0,1,.5)); |
153 | 153 |
colors.push_back(Color(0,.5,1)); |
154 | 154 |
colors.push_back(Color(.5,0,1)); |
155 | 155 |
} while(int(colors.size())<num); |
156 | 156 |
if(num>=0) colors.resize(num); |
157 | 157 |
} |
158 | 158 |
///\e |
159 | 159 |
Color &operator[](int i) |
160 | 160 |
{ |
161 | 161 |
return colors[i%colors.size()]; |
162 | 162 |
} |
163 | 163 |
///\e |
164 | 164 |
const Color &operator[](int i) const |
165 | 165 |
{ |
166 | 166 |
return colors[i%colors.size()]; |
167 | 167 |
} |
168 | 168 |
///\e |
169 | 169 |
void set(int i,const Color &c) |
170 | 170 |
{ |
171 | 171 |
colors[i%colors.size()]=c; |
172 | 172 |
} |
173 | 173 |
///Adds a new color to the end of the color list. |
174 | 174 |
void add(const Color &c) |
175 | 175 |
{ |
176 | 176 |
colors.push_back(c); |
177 | 177 |
} |
178 | 178 |
|
179 | 179 |
///Sets the number of the existing colors. |
180 | 180 |
void resize(int s) { colors.resize(s);} |
181 | 181 |
///Returns the number of the existing colors. |
182 | 182 |
int size() const { return int(colors.size());} |
183 | 183 |
}; |
184 | 184 |
|
185 | 185 |
///Returns a visibly distinct \ref Color |
186 | 186 |
|
187 | 187 |
///Returns a \ref Color which is as different from the given parameter |
188 | 188 |
///as it is possible. |
189 | 189 |
inline Color distantColor(const Color &c) |
190 | 190 |
{ |
191 | 191 |
return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0); |
192 | 192 |
} |
193 | 193 |
///Returns black for light colors and white for the dark ones. |
194 | 194 |
|
195 | 195 |
///Returns black for light colors and white for the dark ones. |
196 | 196 |
inline Color distantBW(const Color &c){ |
197 | 197 |
return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK; |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
/// @} |
201 | 201 |
|
202 | 202 |
} //END OF NAMESPACE LEMON |
203 | 203 |
|
204 | 204 |
#endif // LEMON_COLOR_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-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 |
// The contents of this file was inspired by the concept checking |
20 | 20 |
// utility of the BOOST library (http://www.boost.org). |
21 | 21 |
|
22 | 22 |
///\file |
23 | 23 |
///\brief Basic utilities for concept checking. |
24 | 24 |
/// |
25 | 25 |
|
26 | 26 |
#ifndef LEMON_CONCEPT_CHECK_H |
27 | 27 |
#define LEMON_CONCEPT_CHECK_H |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
/* |
32 | 32 |
"inline" is used for ignore_unused_variable_warning() |
33 | 33 |
and function_requires() to make sure there is no |
34 | 34 |
overtarget with g++. |
35 | 35 |
*/ |
36 | 36 |
|
37 | 37 |
template <class T> inline void ignore_unused_variable_warning(const T&) { } |
38 | 38 |
|
39 | 39 |
///\e |
40 | 40 |
template <class Concept> |
41 | 41 |
inline void function_requires() |
42 | 42 |
{ |
43 | 43 |
#if !defined(NDEBUG) |
44 | 44 |
void (Concept::*x)() = & Concept::constraints; |
45 | 45 |
ignore_unused_variable_warning(x); |
46 | 46 |
#endif |
47 | 47 |
} |
48 | 48 |
|
49 | 49 |
///\e |
50 | 50 |
template <typename Concept, typename Type> |
51 | 51 |
inline void checkConcept() { |
52 | 52 |
#if !defined(NDEBUG) |
53 | 53 |
typedef typename Concept::template Constraints<Type> ConceptCheck; |
54 | 54 |
void (ConceptCheck::*x)() = & ConceptCheck::constraints; |
55 | 55 |
ignore_unused_variable_warning(x); |
56 | 56 |
#endif |
57 | 57 |
} |
58 | 58 |
|
59 | 59 |
} // namespace lemon |
60 | 60 |
|
61 | 61 |
#endif // LEMON_CONCEPT_CHECK_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-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_CONCEPT_DIGRAPH_H |
20 | 20 |
#define LEMON_CONCEPT_DIGRAPH_H |
21 | 21 |
|
22 | 22 |
///\ingroup graph_concepts |
23 | 23 |
///\file |
24 | 24 |
///\brief The concept of directed graphs. |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/graph_components.h> |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
namespace concepts { |
33 | 33 |
|
34 | 34 |
/// \ingroup graph_concepts |
35 | 35 |
/// |
36 | 36 |
/// \brief Class describing the concept of directed graphs. |
37 | 37 |
/// |
38 | 38 |
/// This class describes the \ref concept "concept" of the |
39 | 39 |
/// immutable directed digraphs. |
40 | 40 |
/// |
41 | 41 |
/// Note that actual digraph implementation like @ref ListDigraph or |
42 | 42 |
/// @ref SmartDigraph may have several additional functionality. |
43 | 43 |
/// |
44 | 44 |
/// \sa concept |
45 | 45 |
class Digraph { |
46 | 46 |
private: |
47 | 47 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
48 | 48 |
|
49 | 49 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
50 | 50 |
/// |
51 | 51 |
Digraph(const Digraph &) {}; |
52 | 52 |
///\brief Assignment of \ref Digraph "Digraph"s to another ones are |
53 | 53 |
///\e not allowed. Use DigraphCopy() instead. |
54 | 54 |
|
55 | 55 |
///Assignment of \ref Digraph "Digraph"s to another ones are |
56 | 56 |
///\e not allowed. Use DigraphCopy() instead. |
57 | 57 |
|
58 | 58 |
void operator=(const Digraph &) {} |
59 | 59 |
public: |
60 | 60 |
///\e |
61 | 61 |
|
62 | 62 |
/// Defalult constructor. |
63 | 63 |
|
64 | 64 |
/// Defalult constructor. |
65 | 65 |
/// |
66 | 66 |
Digraph() { } |
67 | 67 |
/// Class for identifying a node of the digraph |
68 | 68 |
|
69 | 69 |
/// This class identifies a node of the digraph. It also serves |
70 | 70 |
/// as a base class of the node iterators, |
71 | 71 |
/// thus they will convert to this type. |
72 | 72 |
class Node { |
73 | 73 |
public: |
74 | 74 |
/// Default constructor |
75 | 75 |
|
76 | 76 |
/// @warning The default constructor sets the iterator |
77 | 77 |
/// to an undefined value. |
78 | 78 |
Node() { } |
79 | 79 |
/// Copy constructor. |
80 | 80 |
|
81 | 81 |
/// Copy constructor. |
82 | 82 |
/// |
83 | 83 |
Node(const Node&) { } |
84 | 84 |
|
85 | 85 |
/// Invalid constructor \& conversion. |
86 | 86 |
|
87 | 87 |
/// This constructor initializes the iterator to be invalid. |
88 | 88 |
/// \sa Invalid for more details. |
89 | 89 |
Node(Invalid) { } |
90 | 90 |
/// Equality operator |
91 | 91 |
|
92 | 92 |
/// Two iterators are equal if and only if they point to the |
93 | 93 |
/// same object or both are invalid. |
94 | 94 |
bool operator==(Node) const { return true; } |
95 | 95 |
|
96 | 96 |
/// Inequality operator |
97 | 97 |
|
98 | 98 |
/// \sa operator==(Node n) |
99 | 99 |
/// |
100 | 100 |
bool operator!=(Node) const { return true; } |
101 | 101 |
|
102 | 102 |
/// Artificial ordering operator. |
103 | 103 |
|
104 | 104 |
/// To allow the use of digraph descriptors as key type in std::map or |
105 | 105 |
/// similar associative container we require this. |
106 | 106 |
/// |
107 | 107 |
/// \note This operator only have to define some strict ordering of |
108 | 108 |
/// the items; this order has nothing to do with the iteration |
109 | 109 |
/// ordering of the items. |
110 | 110 |
bool operator<(Node) const { return false; } |
111 | 111 |
|
112 | 112 |
}; |
113 | 113 |
|
114 | 114 |
/// This iterator goes through each node. |
115 | 115 |
|
116 | 116 |
/// This iterator goes through each node. |
117 | 117 |
/// Its usage is quite simple, for example you can count the number |
118 | 118 |
/// of nodes in digraph \c g of type \c Digraph like this: |
119 | 119 |
///\code |
120 | 120 |
/// int count=0; |
121 | 121 |
/// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count; |
122 | 122 |
///\endcode |
123 | 123 |
class NodeIt : public Node { |
124 | 124 |
public: |
125 | 125 |
/// Default constructor |
126 | 126 |
|
127 | 127 |
/// @warning The default constructor sets the iterator |
128 | 128 |
/// to an undefined value. |
129 | 129 |
NodeIt() { } |
130 | 130 |
/// Copy constructor. |
131 | 131 |
|
132 | 132 |
/// Copy constructor. |
133 | 133 |
/// |
134 | 134 |
NodeIt(const NodeIt& n) : Node(n) { } |
135 | 135 |
/// Invalid constructor \& conversion. |
136 | 136 |
|
137 | 137 |
/// Initialize the iterator to be invalid. |
138 | 138 |
/// \sa Invalid for more details. |
139 | 139 |
NodeIt(Invalid) { } |
140 | 140 |
/// Sets the iterator to the first node. |
141 | 141 |
|
142 | 142 |
/// Sets the iterator to the first node of \c g. |
143 | 143 |
/// |
144 | 144 |
NodeIt(const Digraph&) { } |
145 | 145 |
/// Node -> NodeIt conversion. |
146 | 146 |
|
147 | 147 |
/// Sets the iterator to the node of \c the digraph pointed by |
148 | 148 |
/// the trivial iterator. |
149 | 149 |
/// This feature necessitates that each time we |
150 | 150 |
/// iterate the arc-set, the iteration order is the same. |
151 | 151 |
NodeIt(const Digraph&, const Node&) { } |
152 | 152 |
/// Next node. |
153 | 153 |
|
154 | 154 |
/// Assign the iterator to the next node. |
155 | 155 |
/// |
156 | 156 |
NodeIt& operator++() { return *this; } |
157 | 157 |
}; |
158 | 158 |
|
159 | 159 |
|
160 | 160 |
/// Class for identifying an arc of the digraph |
161 | 161 |
|
162 | 162 |
/// This class identifies an arc of the digraph. It also serves |
163 | 163 |
/// as a base class of the arc iterators, |
164 | 164 |
/// thus they will convert to this type. |
165 | 165 |
class Arc { |
166 | 166 |
public: |
167 | 167 |
/// Default constructor |
168 | 168 |
|
169 | 169 |
/// @warning The default constructor sets the iterator |
170 | 170 |
/// to an undefined value. |
171 | 171 |
Arc() { } |
172 | 172 |
/// Copy constructor. |
173 | 173 |
|
174 | 174 |
/// Copy constructor. |
175 | 175 |
/// |
176 | 176 |
Arc(const Arc&) { } |
177 | 177 |
/// Initialize the iterator to be invalid. |
178 | 178 |
|
179 | 179 |
/// Initialize the iterator to be invalid. |
180 | 180 |
/// |
181 | 181 |
Arc(Invalid) { } |
182 | 182 |
/// Equality operator |
183 | 183 |
|
184 | 184 |
/// Two iterators are equal if and only if they point to the |
185 | 185 |
/// same object or both are invalid. |
186 | 186 |
bool operator==(Arc) const { return true; } |
187 | 187 |
/// Inequality operator |
188 | 188 |
|
189 | 189 |
/// \sa operator==(Arc n) |
190 | 190 |
/// |
191 | 191 |
bool operator!=(Arc) const { return true; } |
192 | 192 |
|
193 | 193 |
/// Artificial ordering operator. |
194 | 194 |
|
195 | 195 |
/// To allow the use of digraph descriptors as key type in std::map or |
196 | 196 |
/// similar associative container we require this. |
197 | 197 |
/// |
198 | 198 |
/// \note This operator only have to define some strict ordering of |
199 | 199 |
/// the items; this order has nothing to do with the iteration |
200 | 200 |
/// ordering of the items. |
201 | 201 |
bool operator<(Arc) const { return false; } |
202 | 202 |
}; |
203 | 203 |
|
204 | 204 |
/// This iterator goes trough the outgoing arcs of a node. |
205 | 205 |
|
206 | 206 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
207 | 207 |
/// of a digraph. |
208 | 208 |
/// Its usage is quite simple, for example you can count the number |
209 | 209 |
/// of outgoing arcs of a node \c n |
210 | 210 |
/// in digraph \c g of type \c Digraph as follows. |
211 | 211 |
///\code |
212 | 212 |
/// int count=0; |
213 | 213 |
/// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count; |
214 | 214 |
///\endcode |
215 | 215 |
|
216 | 216 |
class OutArcIt : public Arc { |
217 | 217 |
public: |
218 | 218 |
/// Default constructor |
219 | 219 |
|
220 | 220 |
/// @warning The default constructor sets the iterator |
221 | 221 |
/// to an undefined value. |
222 | 222 |
OutArcIt() { } |
223 | 223 |
/// Copy constructor. |
224 | 224 |
|
225 | 225 |
/// Copy constructor. |
226 | 226 |
/// |
227 | 227 |
OutArcIt(const OutArcIt& e) : Arc(e) { } |
228 | 228 |
/// Initialize the iterator to be invalid. |
229 | 229 |
|
230 | 230 |
/// Initialize the iterator to be invalid. |
231 | 231 |
/// |
232 | 232 |
OutArcIt(Invalid) { } |
233 | 233 |
/// This constructor sets the iterator to the first outgoing arc. |
234 | 234 |
|
235 | 235 |
/// This constructor sets the iterator to the first outgoing arc of |
236 | 236 |
/// the node. |
237 | 237 |
OutArcIt(const Digraph&, const Node&) { } |
238 | 238 |
/// Arc -> OutArcIt conversion |
239 | 239 |
|
240 | 240 |
/// Sets the iterator to the value of the trivial iterator. |
241 | 241 |
/// This feature necessitates that each time we |
242 | 242 |
/// iterate the arc-set, the iteration order is the same. |
243 | 243 |
OutArcIt(const Digraph&, const Arc&) { } |
244 | 244 |
///Next outgoing arc |
245 | 245 |
|
246 | 246 |
/// Assign the iterator to the next |
247 | 247 |
/// outgoing arc of the corresponding node. |
248 | 248 |
OutArcIt& operator++() { return *this; } |
249 | 249 |
}; |
250 | 250 |
|
251 | 251 |
/// This iterator goes trough the incoming arcs of a node. |
252 | 252 |
|
253 | 253 |
/// This iterator goes trough the \e incoming arcs of a certain node |
254 | 254 |
/// of a digraph. |
255 | 255 |
/// Its usage is quite simple, for example you can count the number |
256 | 256 |
/// of outgoing arcs of a node \c n |
257 | 257 |
/// in digraph \c g of type \c Digraph as follows. |
258 | 258 |
///\code |
259 | 259 |
/// int count=0; |
260 | 260 |
/// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count; |
261 | 261 |
///\endcode |
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-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 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of Undirected Graphs. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPT_GRAPH_H |
24 | 24 |
#define LEMON_CONCEPT_GRAPH_H |
25 | 25 |
|
26 | 26 |
#include <lemon/concepts/graph_components.h> |
27 | 27 |
#include <lemon/concepts/graph.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \ingroup graph_concepts |
34 | 34 |
/// |
35 | 35 |
/// \brief Class describing the concept of Undirected Graphs. |
36 | 36 |
/// |
37 | 37 |
/// This class describes the common interface of all Undirected |
38 | 38 |
/// Graphs. |
39 | 39 |
/// |
40 | 40 |
/// As all concept describing classes it provides only interface |
41 | 41 |
/// without any sensible implementation. So any algorithm for |
42 | 42 |
/// undirected graph should compile with this class, but it will not |
43 | 43 |
/// run properly, of course. |
44 | 44 |
/// |
45 | 45 |
/// The LEMON undirected graphs also fulfill the concept of |
46 | 46 |
/// directed graphs (\ref lemon::concepts::Digraph "Digraph |
47 | 47 |
/// Concept"). Each edges can be seen as two opposite |
48 | 48 |
/// directed arc and consequently the undirected graph can be |
49 | 49 |
/// seen as the direceted graph of these directed arcs. The |
50 | 50 |
/// Graph has the Edge inner class for the edges and |
51 | 51 |
/// the Arc type for the directed arcs. The Arc type is |
52 | 52 |
/// convertible to Edge or inherited from it so from a directed |
53 | 53 |
/// arc we can get the represented edge. |
54 | 54 |
/// |
55 | 55 |
/// In the sense of the LEMON each edge has a default |
56 | 56 |
/// direction (it should be in every computer implementation, |
57 | 57 |
/// because the order of edge's nodes defines an |
58 | 58 |
/// orientation). With the default orientation we can define that |
59 | 59 |
/// the directed arc is forward or backward directed. With the \c |
60 | 60 |
/// direction() and \c direct() function we can get the direction |
61 | 61 |
/// of the directed arc and we can direct an edge. |
62 | 62 |
/// |
63 | 63 |
/// The EdgeIt is an iterator for the edges. We can use |
64 | 64 |
/// the EdgeMap to map values for the edges. The InArcIt and |
65 | 65 |
/// OutArcIt iterates on the same edges but with opposite |
66 | 66 |
/// direction. The IncEdgeIt iterates also on the same edges |
67 | 67 |
/// as the OutArcIt and InArcIt but it is not convertible to Arc just |
68 | 68 |
/// to Edge. |
69 | 69 |
class Graph { |
70 | 70 |
public: |
71 | 71 |
/// \brief The undirected graph should be tagged by the |
72 | 72 |
/// UndirectedTag. |
73 | 73 |
/// |
74 | 74 |
/// The undirected graph should be tagged by the UndirectedTag. This |
75 | 75 |
/// tag helps the enable_if technics to make compile time |
76 | 76 |
/// specializations for undirected graphs. |
77 | 77 |
typedef True UndirectedTag; |
78 | 78 |
|
79 | 79 |
/// \brief The base type of node iterators, |
80 | 80 |
/// or in other words, the trivial node iterator. |
81 | 81 |
/// |
82 | 82 |
/// This is the base type of each node iterator, |
83 | 83 |
/// thus each kind of node iterator converts to this. |
84 | 84 |
/// More precisely each kind of node iterator should be inherited |
85 | 85 |
/// from the trivial node iterator. |
86 | 86 |
class Node { |
87 | 87 |
public: |
88 | 88 |
/// Default constructor |
89 | 89 |
|
90 | 90 |
/// @warning The default constructor sets the iterator |
91 | 91 |
/// to an undefined value. |
92 | 92 |
Node() { } |
93 | 93 |
/// Copy constructor. |
94 | 94 |
|
95 | 95 |
/// Copy constructor. |
96 | 96 |
/// |
97 | 97 |
Node(const Node&) { } |
98 | 98 |
|
99 | 99 |
/// Invalid constructor \& conversion. |
100 | 100 |
|
101 | 101 |
/// This constructor initializes the iterator to be invalid. |
102 | 102 |
/// \sa Invalid for more details. |
103 | 103 |
Node(Invalid) { } |
104 | 104 |
/// Equality operator |
105 | 105 |
|
106 | 106 |
/// Two iterators are equal if and only if they point to the |
107 | 107 |
/// same object or both are invalid. |
108 | 108 |
bool operator==(Node) const { return true; } |
109 | 109 |
|
110 | 110 |
/// Inequality operator |
111 | 111 |
|
112 | 112 |
/// \sa operator==(Node n) |
113 | 113 |
/// |
114 | 114 |
bool operator!=(Node) const { return true; } |
115 | 115 |
|
116 | 116 |
/// Artificial ordering operator. |
117 | 117 |
|
118 | 118 |
/// To allow the use of graph descriptors as key type in std::map or |
119 | 119 |
/// similar associative container we require this. |
120 | 120 |
/// |
121 | 121 |
/// \note This operator only have to define some strict ordering of |
122 | 122 |
/// the items; this order has nothing to do with the iteration |
123 | 123 |
/// ordering of the items. |
124 | 124 |
bool operator<(Node) const { return false; } |
125 | 125 |
|
126 | 126 |
}; |
127 | 127 |
|
128 | 128 |
/// This iterator goes through each node. |
129 | 129 |
|
130 | 130 |
/// This iterator goes through each node. |
131 | 131 |
/// Its usage is quite simple, for example you can count the number |
132 | 132 |
/// of nodes in graph \c g of type \c Graph like this: |
133 | 133 |
///\code |
134 | 134 |
/// int count=0; |
135 | 135 |
/// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count; |
136 | 136 |
///\endcode |
137 | 137 |
class NodeIt : public Node { |
138 | 138 |
public: |
139 | 139 |
/// Default constructor |
140 | 140 |
|
141 | 141 |
/// @warning The default constructor sets the iterator |
142 | 142 |
/// to an undefined value. |
143 | 143 |
NodeIt() { } |
144 | 144 |
/// Copy constructor. |
145 | 145 |
|
146 | 146 |
/// Copy constructor. |
147 | 147 |
/// |
148 | 148 |
NodeIt(const NodeIt& n) : Node(n) { } |
149 | 149 |
/// Invalid constructor \& conversion. |
150 | 150 |
|
151 | 151 |
/// Initialize the iterator to be invalid. |
152 | 152 |
/// \sa Invalid for more details. |
153 | 153 |
NodeIt(Invalid) { } |
154 | 154 |
/// Sets the iterator to the first node. |
155 | 155 |
|
156 | 156 |
/// Sets the iterator to the first node of \c g. |
157 | 157 |
/// |
158 | 158 |
NodeIt(const Graph&) { } |
159 | 159 |
/// Node -> NodeIt conversion. |
160 | 160 |
|
161 | 161 |
/// Sets the iterator to the node of \c the graph pointed by |
162 | 162 |
/// the trivial iterator. |
163 | 163 |
/// This feature necessitates that each time we |
164 | 164 |
/// iterate the arc-set, the iteration order is the same. |
165 | 165 |
NodeIt(const Graph&, const Node&) { } |
166 | 166 |
/// Next node. |
167 | 167 |
|
168 | 168 |
/// Assign the iterator to the next node. |
169 | 169 |
/// |
170 | 170 |
NodeIt& operator++() { return *this; } |
171 | 171 |
}; |
172 | 172 |
|
173 | 173 |
|
174 | 174 |
/// The base type of the edge iterators. |
175 | 175 |
|
176 | 176 |
/// The base type of the edge iterators. |
177 | 177 |
/// |
178 | 178 |
class Edge { |
179 | 179 |
public: |
180 | 180 |
/// Default constructor |
181 | 181 |
|
182 | 182 |
/// @warning The default constructor sets the iterator |
183 | 183 |
/// to an undefined value. |
184 | 184 |
Edge() { } |
185 | 185 |
/// Copy constructor. |
186 | 186 |
|
187 | 187 |
/// Copy constructor. |
188 | 188 |
/// |
189 | 189 |
Edge(const Edge&) { } |
190 | 190 |
/// Initialize the iterator to be invalid. |
191 | 191 |
|
192 | 192 |
/// Initialize the iterator to be invalid. |
193 | 193 |
/// |
194 | 194 |
Edge(Invalid) { } |
195 | 195 |
/// Equality operator |
196 | 196 |
|
197 | 197 |
/// Two iterators are equal if and only if they point to the |
198 | 198 |
/// same object or both are invalid. |
199 | 199 |
bool operator==(Edge) const { return true; } |
200 | 200 |
/// Inequality operator |
201 | 201 |
|
202 | 202 |
/// \sa operator==(Edge n) |
203 | 203 |
/// |
204 | 204 |
bool operator!=(Edge) const { return true; } |
205 | 205 |
|
206 | 206 |
/// Artificial ordering operator. |
207 | 207 |
|
208 | 208 |
/// To allow the use of graph descriptors as key type in std::map or |
209 | 209 |
/// similar associative container we require this. |
210 | 210 |
/// |
211 | 211 |
/// \note This operator only have to define some strict ordering of |
212 | 212 |
/// the items; this order has nothing to do with the iteration |
213 | 213 |
/// ordering of the items. |
214 | 214 |
bool operator<(Edge) const { return false; } |
215 | 215 |
}; |
216 | 216 |
|
217 | 217 |
/// This iterator goes through each edge. |
218 | 218 |
|
219 | 219 |
/// This iterator goes through each edge of a graph. |
220 | 220 |
/// Its usage is quite simple, for example you can count the number |
221 | 221 |
/// of edges in a graph \c g of type \c Graph as follows: |
222 | 222 |
///\code |
223 | 223 |
/// int count=0; |
224 | 224 |
/// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count; |
225 | 225 |
///\endcode |
226 | 226 |
class EdgeIt : public Edge { |
227 | 227 |
public: |
228 | 228 |
/// Default constructor |
229 | 229 |
|
230 | 230 |
/// @warning The default constructor sets the iterator |
231 | 231 |
/// to an undefined value. |
232 | 232 |
EdgeIt() { } |
233 | 233 |
/// Copy constructor. |
234 | 234 |
|
235 | 235 |
/// Copy constructor. |
236 | 236 |
/// |
237 | 237 |
EdgeIt(const EdgeIt& e) : Edge(e) { } |
238 | 238 |
/// Initialize the iterator to be invalid. |
239 | 239 |
|
240 | 240 |
/// Initialize the iterator to be invalid. |
241 | 241 |
/// |
242 | 242 |
EdgeIt(Invalid) { } |
243 | 243 |
/// This constructor sets the iterator to the first edge. |
244 | 244 |
|
245 | 245 |
/// This constructor sets the iterator to the first edge. |
246 | 246 |
EdgeIt(const Graph&) { } |
247 | 247 |
/// Edge -> EdgeIt conversion |
248 | 248 |
|
249 | 249 |
/// Sets the iterator to the value of the trivial iterator. |
250 | 250 |
/// This feature necessitates that each time we |
251 | 251 |
/// iterate the edge-set, the iteration order is the |
252 | 252 |
/// same. |
253 | 253 |
EdgeIt(const Graph&, const Edge&) { } |
254 | 254 |
/// Next edge |
255 | 255 |
|
256 | 256 |
/// Assign the iterator to the next edge. |
257 | 257 |
EdgeIt& operator++() { return *this; } |
258 | 258 |
}; |
259 | 259 |
|
260 | 260 |
/// \brief This iterator goes trough the incident undirected |
261 | 261 |
/// arcs of a node. |
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-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 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of graph components. |
22 | 22 |
|
23 | 23 |
|
24 | 24 |
#ifndef LEMON_CONCEPT_GRAPH_COMPONENTS_H |
25 | 25 |
#define LEMON_CONCEPT_GRAPH_COMPONENTS_H |
26 | 26 |
|
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
#include <lemon/concepts/maps.h> |
29 | 29 |
|
30 | 30 |
#include <lemon/bits/alteration_notifier.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
namespace concepts { |
34 | 34 |
|
35 | 35 |
/// \brief Skeleton class for graph Node and Arc types |
36 | 36 |
/// |
37 | 37 |
/// This class describes the interface of Node and Arc (and Edge |
38 | 38 |
/// in undirected graphs) subtypes of graph types. |
39 | 39 |
/// |
40 | 40 |
/// \note This class is a template class so that we can use it to |
41 | 41 |
/// create graph skeleton classes. The reason for this is than Node |
42 | 42 |
/// and Arc types should \em not derive from the same base class. |
43 | 43 |
/// For Node you should instantiate it with character 'n' and for Arc |
44 | 44 |
/// with 'a'. |
45 | 45 |
|
46 | 46 |
#ifndef DOXYGEN |
47 | 47 |
template <char _selector = '0'> |
48 | 48 |
#endif |
49 | 49 |
class GraphItem { |
50 | 50 |
public: |
51 | 51 |
/// \brief Default constructor. |
52 | 52 |
/// |
53 | 53 |
/// \warning The default constructor is not required to set |
54 | 54 |
/// the item to some well-defined value. So you should consider it |
55 | 55 |
/// as uninitialized. |
56 | 56 |
GraphItem() {} |
57 | 57 |
/// \brief Copy constructor. |
58 | 58 |
/// |
59 | 59 |
/// Copy constructor. |
60 | 60 |
/// |
61 | 61 |
GraphItem(const GraphItem &) {} |
62 | 62 |
/// \brief Invalid constructor \& conversion. |
63 | 63 |
/// |
64 | 64 |
/// This constructor initializes the item to be invalid. |
65 | 65 |
/// \sa Invalid for more details. |
66 | 66 |
GraphItem(Invalid) {} |
67 | 67 |
/// \brief Assign operator for nodes. |
68 | 68 |
/// |
69 | 69 |
/// The nodes are assignable. |
70 | 70 |
/// |
71 | 71 |
GraphItem& operator=(GraphItem const&) { return *this; } |
72 | 72 |
/// \brief Equality operator. |
73 | 73 |
/// |
74 | 74 |
/// Two iterators are equal if and only if they represents the |
75 | 75 |
/// same node in the graph or both are invalid. |
76 | 76 |
bool operator==(GraphItem) const { return false; } |
77 | 77 |
/// \brief Inequality operator. |
78 | 78 |
/// |
79 | 79 |
/// \sa operator==(const Node& n) |
80 | 80 |
/// |
81 | 81 |
bool operator!=(GraphItem) const { return false; } |
82 | 82 |
|
83 | 83 |
/// \brief Artificial ordering operator. |
84 | 84 |
/// |
85 | 85 |
/// To allow the use of graph descriptors as key type in std::map or |
86 | 86 |
/// similar associative container we require this. |
87 | 87 |
/// |
88 | 88 |
/// \note This operator only have to define some strict ordering of |
89 | 89 |
/// the items; this order has nothing to do with the iteration |
90 | 90 |
/// ordering of the items. |
91 | 91 |
bool operator<(GraphItem) const { return false; } |
92 | 92 |
|
93 | 93 |
template<typename _GraphItem> |
94 | 94 |
struct Constraints { |
95 | 95 |
void constraints() { |
96 | 96 |
_GraphItem i1; |
97 | 97 |
_GraphItem i2 = i1; |
98 | 98 |
_GraphItem i3 = INVALID; |
99 | 99 |
|
100 | 100 |
i1 = i2 = i3; |
101 | 101 |
|
102 | 102 |
bool b; |
103 | 103 |
// b = (ia == ib) && (ia != ib) && (ia < ib); |
104 | 104 |
b = (ia == ib) && (ia != ib); |
105 | 105 |
b = (ia == INVALID) && (ib != INVALID); |
106 | 106 |
b = (ia < ib); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
const _GraphItem &ia; |
110 | 110 |
const _GraphItem &ib; |
111 | 111 |
}; |
112 | 112 |
}; |
113 | 113 |
|
114 | 114 |
/// \brief An empty base directed graph class. |
115 | 115 |
/// |
116 | 116 |
/// This class provides the minimal set of features needed for a |
117 | 117 |
/// directed graph structure. All digraph concepts have to be |
118 | 118 |
/// conform to this base directed graph. It just provides types |
119 | 119 |
/// for nodes and arcs and functions to get the source and the |
120 | 120 |
/// target of the arcs. |
121 | 121 |
class BaseDigraphComponent { |
122 | 122 |
public: |
123 | 123 |
|
124 | 124 |
typedef BaseDigraphComponent Digraph; |
125 | 125 |
|
126 | 126 |
/// \brief Node class of the digraph. |
127 | 127 |
/// |
128 | 128 |
/// This class represents the Nodes of the digraph. |
129 | 129 |
/// |
130 | 130 |
typedef GraphItem<'n'> Node; |
131 | 131 |
|
132 | 132 |
/// \brief Arc class of the digraph. |
133 | 133 |
/// |
134 | 134 |
/// This class represents the Arcs of the digraph. |
135 | 135 |
/// |
136 | 136 |
typedef GraphItem<'e'> Arc; |
137 | 137 |
|
138 | 138 |
/// \brief Gives back the target node of an arc. |
139 | 139 |
/// |
140 | 140 |
/// Gives back the target node of an arc. |
141 | 141 |
/// |
142 | 142 |
Node target(const Arc&) const { return INVALID;} |
143 | 143 |
|
144 | 144 |
/// \brief Gives back the source node of an arc. |
145 | 145 |
/// |
146 | 146 |
/// Gives back the source node of an arc. |
147 | 147 |
/// |
148 | 148 |
Node source(const Arc&) const { return INVALID;} |
149 | 149 |
|
150 | 150 |
/// \brief Gives back the opposite node on the given arc. |
151 | 151 |
/// |
152 | 152 |
/// Gives back the opposite node on the given arc. |
153 | 153 |
Node oppositeNode(const Node&, const Arc&) const { |
154 | 154 |
return INVALID; |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
template <typename _Digraph> |
158 | 158 |
struct Constraints { |
159 | 159 |
typedef typename _Digraph::Node Node; |
160 | 160 |
typedef typename _Digraph::Arc Arc; |
161 | 161 |
|
162 | 162 |
void constraints() { |
163 | 163 |
checkConcept<GraphItem<'n'>, Node>(); |
164 | 164 |
checkConcept<GraphItem<'a'>, Arc>(); |
165 | 165 |
{ |
166 | 166 |
Node n; |
167 | 167 |
Arc e(INVALID); |
168 | 168 |
n = digraph.source(e); |
169 | 169 |
n = digraph.target(e); |
170 | 170 |
n = digraph.oppositeNode(n, e); |
171 | 171 |
} |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
const _Digraph& digraph; |
175 | 175 |
}; |
176 | 176 |
}; |
177 | 177 |
|
178 | 178 |
/// \brief An empty base undirected graph class. |
179 | 179 |
/// |
180 | 180 |
/// This class provides the minimal set of features needed for an |
181 | 181 |
/// undirected graph structure. All undirected graph concepts have |
182 | 182 |
/// to be conform to this base graph. It just provides types for |
183 | 183 |
/// nodes, arcs and edges and functions to get the |
184 | 184 |
/// source and the target of the arcs and edges, |
185 | 185 |
/// conversion from arcs to edges and function to get |
186 | 186 |
/// both direction of the edges. |
187 | 187 |
class BaseGraphComponent : public BaseDigraphComponent { |
188 | 188 |
public: |
189 | 189 |
typedef BaseDigraphComponent::Node Node; |
190 | 190 |
typedef BaseDigraphComponent::Arc Arc; |
191 | 191 |
/// \brief Undirected arc class of the graph. |
192 | 192 |
/// |
193 | 193 |
/// This class represents the edges of the graph. |
194 | 194 |
/// The undirected graphs can be used as a directed graph which |
195 | 195 |
/// for each arc contains the opposite arc too so the graph is |
196 | 196 |
/// bidirected. The edge represents two opposite |
197 | 197 |
/// directed arcs. |
198 | 198 |
class Edge : public GraphItem<'u'> { |
199 | 199 |
public: |
200 | 200 |
typedef GraphItem<'u'> Parent; |
201 | 201 |
/// \brief Default constructor. |
202 | 202 |
/// |
203 | 203 |
/// \warning The default constructor is not required to set |
204 | 204 |
/// the item to some well-defined value. So you should consider it |
205 | 205 |
/// as uninitialized. |
206 | 206 |
Edge() {} |
207 | 207 |
/// \brief Copy constructor. |
208 | 208 |
/// |
209 | 209 |
/// Copy constructor. |
210 | 210 |
/// |
211 | 211 |
Edge(const Edge &) : Parent() {} |
212 | 212 |
/// \brief Invalid constructor \& conversion. |
213 | 213 |
/// |
214 | 214 |
/// This constructor initializes the item to be invalid. |
215 | 215 |
/// \sa Invalid for more details. |
216 | 216 |
Edge(Invalid) {} |
217 | 217 |
/// \brief Converter from arc to edge. |
218 | 218 |
/// |
219 | 219 |
/// Besides the core graph item functionality each arc should |
220 | 220 |
/// be convertible to the represented edge. |
221 | 221 |
Edge(const Arc&) {} |
222 | 222 |
/// \brief Assign arc to edge. |
223 | 223 |
/// |
224 | 224 |
/// Besides the core graph item functionality each arc should |
225 | 225 |
/// be convertible to the represented edge. |
226 | 226 |
Edge& operator=(const Arc&) { return *this; } |
227 | 227 |
}; |
228 | 228 |
|
229 | 229 |
/// \brief Returns the direction of the arc. |
230 | 230 |
/// |
231 | 231 |
/// Returns the direction of the arc. Each arc represents an |
232 | 232 |
/// edge with a direction. It gives back the |
233 | 233 |
/// direction. |
234 | 234 |
bool direction(const Arc&) const { return true; } |
235 | 235 |
|
236 | 236 |
/// \brief Returns the directed arc. |
237 | 237 |
/// |
238 | 238 |
/// Returns the directed arc from its direction and the |
239 | 239 |
/// represented edge. |
240 | 240 |
Arc direct(const Edge&, bool) const { return INVALID;} |
241 | 241 |
|
242 | 242 |
/// \brief Returns the directed arc. |
243 | 243 |
/// |
244 | 244 |
/// Returns the directed arc from its source and the |
245 | 245 |
/// represented edge. |
246 | 246 |
Arc direct(const Edge&, const Node&) const { return INVALID;} |
247 | 247 |
|
248 | 248 |
/// \brief Returns the opposite arc. |
249 | 249 |
/// |
250 | 250 |
/// Returns the opposite arc. It is the arc representing the |
251 | 251 |
/// same edge and has opposite direction. |
252 | 252 |
Arc oppositeArc(const Arc&) const { return INVALID;} |
253 | 253 |
|
254 | 254 |
/// \brief Gives back one ending of an edge. |
255 | 255 |
/// |
256 | 256 |
/// Gives back one ending of an edge. |
257 | 257 |
Node u(const Edge&) const { return INVALID;} |
258 | 258 |
|
259 | 259 |
/// \brief Gives back the other ending of an edge. |
260 | 260 |
/// |
261 | 261 |
/// Gives back the other ending of an edge. |
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-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 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of heaps. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPT_HEAP_H |
24 | 24 |
#define LEMON_CONCEPT_HEAP_H |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
|
28 | 28 |
namespace lemon { |
29 | 29 |
|
30 | 30 |
namespace concepts { |
31 | 31 |
|
32 | 32 |
/// \addtogroup concept |
33 | 33 |
/// @{ |
34 | 34 |
|
35 | 35 |
/// \brief The heap concept. |
36 | 36 |
/// |
37 | 37 |
/// Concept class describing the main interface of heaps. |
38 | 38 |
template <typename Priority, typename ItemIntMap> |
39 | 39 |
class Heap { |
40 | 40 |
public: |
41 | 41 |
|
42 | 42 |
/// Type of the items stored in the heap. |
43 | 43 |
typedef typename ItemIntMap::Key Item; |
44 | 44 |
|
45 | 45 |
/// Type of the priorities. |
46 | 46 |
typedef Priority Prio; |
47 | 47 |
|
48 | 48 |
/// \brief Type to represent the states of the items. |
49 | 49 |
/// |
50 | 50 |
/// Each item has a state associated to it. It can be "in heap", |
51 | 51 |
/// "pre heap" or "post heap". The later two are indifferent |
52 | 52 |
/// from the point of view of the heap, but may be useful for |
53 | 53 |
/// the user. |
54 | 54 |
/// |
55 | 55 |
/// The \c ItemIntMap must be initialized in such a way, that it |
56 | 56 |
/// assigns \c PRE_HEAP (<tt>-1</tt>) to every item. |
57 | 57 |
enum State { |
58 | 58 |
IN_HEAP = 0, |
59 | 59 |
PRE_HEAP = -1, |
60 | 60 |
POST_HEAP = -2 |
61 | 61 |
}; |
62 | 62 |
|
63 | 63 |
/// \brief The constructor. |
64 | 64 |
/// |
65 | 65 |
/// The constructor. |
66 | 66 |
/// \param map A map that assigns \c int values to keys of type |
67 | 67 |
/// \c Item. It is used internally by the heap implementations to |
68 | 68 |
/// handle the cross references. The assigned value must be |
69 | 69 |
/// \c PRE_HEAP (<tt>-1</tt>) for every item. |
70 | 70 |
explicit Heap(ItemIntMap &map) {} |
71 | 71 |
|
72 | 72 |
/// \brief The number of items stored in the heap. |
73 | 73 |
/// |
74 | 74 |
/// Returns the number of items stored in the heap. |
75 | 75 |
int size() const { return 0; } |
76 | 76 |
|
77 | 77 |
/// \brief Checks if the heap is empty. |
78 | 78 |
/// |
79 | 79 |
/// Returns \c true if the heap is empty. |
80 | 80 |
bool empty() const { return false; } |
81 | 81 |
|
82 | 82 |
/// \brief Makes the heap empty. |
83 | 83 |
/// |
84 | 84 |
/// Makes the heap empty. |
85 | 85 |
void clear(); |
86 | 86 |
|
87 | 87 |
/// \brief Inserts an item into the heap with the given priority. |
88 | 88 |
/// |
89 | 89 |
/// Inserts the given item into the heap with the given priority. |
90 | 90 |
/// \param i The item to insert. |
91 | 91 |
/// \param p The priority of the item. |
92 | 92 |
void push(const Item &i, const Prio &p) {} |
93 | 93 |
|
94 | 94 |
/// \brief Returns the item having minimum priority. |
95 | 95 |
/// |
96 | 96 |
/// Returns the item having minimum priority. |
97 | 97 |
/// \pre The heap must be non-empty. |
98 | 98 |
Item top() const {} |
99 | 99 |
|
100 | 100 |
/// \brief The minimum priority. |
101 | 101 |
/// |
102 | 102 |
/// Returns the minimum priority. |
103 | 103 |
/// \pre The heap must be non-empty. |
104 | 104 |
Prio prio() const {} |
105 | 105 |
|
106 | 106 |
/// \brief Removes the item having minimum priority. |
107 | 107 |
/// |
108 | 108 |
/// Removes the item having minimum priority. |
109 | 109 |
/// \pre The heap must be non-empty. |
110 | 110 |
void pop() {} |
111 | 111 |
|
112 | 112 |
/// \brief Removes an item from the heap. |
113 | 113 |
/// |
114 | 114 |
/// Removes the given item from the heap if it is already stored. |
115 | 115 |
/// \param i The item to delete. |
116 | 116 |
void erase(const Item &i) {} |
117 | 117 |
|
118 | 118 |
/// \brief The priority of an item. |
119 | 119 |
/// |
120 | 120 |
/// Returns the priority of the given item. |
121 | 121 |
/// \pre \c i must be in the heap. |
122 | 122 |
/// \param i The item. |
123 | 123 |
Prio operator[](const Item &i) const {} |
124 | 124 |
|
125 | 125 |
/// \brief Sets the priority of an item or inserts it, if it is |
126 | 126 |
/// not stored in the heap. |
127 | 127 |
/// |
128 | 128 |
/// This method sets the priority of the given item if it is |
129 | 129 |
/// already stored in the heap. |
130 | 130 |
/// Otherwise it inserts the given item with the given priority. |
131 | 131 |
/// |
132 | 132 |
/// \param i The item. |
133 | 133 |
/// \param p The priority. |
134 | 134 |
void set(const Item &i, const Prio &p) {} |
135 | 135 |
|
136 | 136 |
/// \brief Decreases the priority of an item to the given value. |
137 | 137 |
/// |
138 | 138 |
/// Decreases the priority of an item to the given value. |
139 | 139 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
140 | 140 |
/// \param i The item. |
141 | 141 |
/// \param p The priority. |
142 | 142 |
void decrease(const Item &i, const Prio &p) {} |
143 | 143 |
|
144 | 144 |
/// \brief Increases the priority of an item to the given value. |
145 | 145 |
/// |
146 | 146 |
/// Increases the priority of an item to the given value. |
147 | 147 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
148 | 148 |
/// \param i The item. |
149 | 149 |
/// \param p The priority. |
150 | 150 |
void increase(const Item &i, const Prio &p) {} |
151 | 151 |
|
152 | 152 |
/// \brief Returns if an item is in, has already been in, or has |
153 | 153 |
/// never been in the heap. |
154 | 154 |
/// |
155 | 155 |
/// This method returns \c PRE_HEAP if the given item has never |
156 | 156 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
157 | 157 |
/// and \c POST_HEAP otherwise. |
158 | 158 |
/// In the latter case it is possible that the item will get back |
159 | 159 |
/// to the heap again. |
160 | 160 |
/// \param i The item. |
161 | 161 |
State state(const Item &i) const {} |
162 | 162 |
|
163 | 163 |
/// \brief Sets the state of an item in the heap. |
164 | 164 |
/// |
165 | 165 |
/// Sets the state of the given item in the heap. It can be used |
166 | 166 |
/// to manually clear the heap when it is important to achive the |
167 | 167 |
/// better time complexity. |
168 | 168 |
/// \param i The item. |
169 | 169 |
/// \param st The state. It should not be \c IN_HEAP. |
170 | 170 |
void state(const Item& i, State st) {} |
171 | 171 |
|
172 | 172 |
|
173 | 173 |
template <typename _Heap> |
174 | 174 |
struct Constraints { |
175 | 175 |
public: |
176 | 176 |
void constraints() { |
177 | 177 |
typedef typename _Heap::Item OwnItem; |
178 | 178 |
typedef typename _Heap::Prio OwnPrio; |
179 | 179 |
typedef typename _Heap::State OwnState; |
180 | 180 |
|
181 | 181 |
Item item; |
182 | 182 |
Prio prio; |
183 | 183 |
item=Item(); |
184 | 184 |
prio=Prio(); |
185 | 185 |
ignore_unused_variable_warning(item); |
186 | 186 |
ignore_unused_variable_warning(prio); |
187 | 187 |
|
188 | 188 |
OwnItem own_item; |
189 | 189 |
OwnPrio own_prio; |
190 | 190 |
OwnState own_state; |
191 | 191 |
own_item=Item(); |
192 | 192 |
own_prio=Prio(); |
193 | 193 |
ignore_unused_variable_warning(own_item); |
194 | 194 |
ignore_unused_variable_warning(own_prio); |
195 | 195 |
ignore_unused_variable_warning(own_state); |
196 | 196 |
|
197 | 197 |
_Heap heap1(map); |
198 | 198 |
_Heap heap2 = heap1; |
199 | 199 |
ignore_unused_variable_warning(heap1); |
200 | 200 |
ignore_unused_variable_warning(heap2); |
201 | 201 |
|
202 | 202 |
int s = heap.size(); |
203 | 203 |
ignore_unused_variable_warning(s); |
204 | 204 |
bool e = heap.empty(); |
205 | 205 |
ignore_unused_variable_warning(e); |
206 | 206 |
|
207 | 207 |
prio = heap.prio(); |
208 | 208 |
item = heap.top(); |
209 | 209 |
prio = heap[item]; |
210 | 210 |
own_prio = heap.prio(); |
211 | 211 |
own_item = heap.top(); |
212 | 212 |
own_prio = heap[own_item]; |
213 | 213 |
|
214 | 214 |
heap.push(item, prio); |
215 | 215 |
heap.push(own_item, own_prio); |
216 | 216 |
heap.pop(); |
217 | 217 |
|
218 | 218 |
heap.set(item, prio); |
219 | 219 |
heap.decrease(item, prio); |
220 | 220 |
heap.increase(item, prio); |
221 | 221 |
heap.set(own_item, own_prio); |
222 | 222 |
heap.decrease(own_item, own_prio); |
223 | 223 |
heap.increase(own_item, own_prio); |
224 | 224 |
|
225 | 225 |
heap.erase(item); |
226 | 226 |
heap.erase(own_item); |
227 | 227 |
heap.clear(); |
228 | 228 |
|
229 | 229 |
own_state = heap.state(own_item); |
230 | 230 |
heap.state(own_item, own_state); |
231 | 231 |
|
232 | 232 |
own_state = _Heap::PRE_HEAP; |
233 | 233 |
own_state = _Heap::IN_HEAP; |
234 | 234 |
own_state = _Heap::POST_HEAP; |
235 | 235 |
} |
236 | 236 |
|
237 | 237 |
_Heap& heap; |
238 | 238 |
ItemIntMap& map; |
239 | 239 |
}; |
240 | 240 |
}; |
241 | 241 |
|
242 | 242 |
/// @} |
243 | 243 |
} // namespace lemon |
244 | 244 |
} |
245 | 245 |
#endif // LEMON_CONCEPT_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 |
* Copyright (C) 2003- |
|
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_CONCEPT_MAPS_H |
20 | 20 |
#define LEMON_CONCEPT_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 | 25 |
///\ingroup map_concepts |
26 | 26 |
///\file |
27 | 27 |
///\brief The concept of maps. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup map_concepts |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// Readable map concept |
37 | 37 |
|
38 | 38 |
/// Readable map concept. |
39 | 39 |
/// |
40 | 40 |
template<typename K, typename T> |
41 | 41 |
class ReadMap |
42 | 42 |
{ |
43 | 43 |
public: |
44 | 44 |
/// The key type of the map. |
45 | 45 |
typedef K Key; |
46 | 46 |
/// \brief The value type of the map. |
47 | 47 |
/// (The type of objects associated with the keys). |
48 | 48 |
typedef T Value; |
49 | 49 |
|
50 | 50 |
/// Returns the value associated with the given key. |
51 | 51 |
Value operator[](const Key &) const { |
52 | 52 |
return *static_cast<Value *>(0); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
template<typename _ReadMap> |
56 | 56 |
struct Constraints { |
57 | 57 |
void constraints() { |
58 | 58 |
Value val = m[key]; |
59 | 59 |
val = m[key]; |
60 | 60 |
typename _ReadMap::Value own_val = m[own_key]; |
61 | 61 |
own_val = m[own_key]; |
62 | 62 |
|
63 | 63 |
ignore_unused_variable_warning(key); |
64 | 64 |
ignore_unused_variable_warning(val); |
65 | 65 |
ignore_unused_variable_warning(own_key); |
66 | 66 |
ignore_unused_variable_warning(own_val); |
67 | 67 |
} |
68 | 68 |
const Key& key; |
69 | 69 |
const typename _ReadMap::Key& own_key; |
70 | 70 |
const _ReadMap& m; |
71 | 71 |
}; |
72 | 72 |
|
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
/// Writable map concept |
77 | 77 |
|
78 | 78 |
/// Writable map concept. |
79 | 79 |
/// |
80 | 80 |
template<typename K, typename T> |
81 | 81 |
class WriteMap |
82 | 82 |
{ |
83 | 83 |
public: |
84 | 84 |
/// The key type of the map. |
85 | 85 |
typedef K Key; |
86 | 86 |
/// \brief The value type of the map. |
87 | 87 |
/// (The type of objects associated with the keys). |
88 | 88 |
typedef T Value; |
89 | 89 |
|
90 | 90 |
/// Sets the value associated with the given key. |
91 | 91 |
void set(const Key &, const Value &) {} |
92 | 92 |
|
93 | 93 |
/// Default constructor. |
94 | 94 |
WriteMap() {} |
95 | 95 |
|
96 | 96 |
template <typename _WriteMap> |
97 | 97 |
struct Constraints { |
98 | 98 |
void constraints() { |
99 | 99 |
m.set(key, val); |
100 | 100 |
m.set(own_key, own_val); |
101 | 101 |
|
102 | 102 |
ignore_unused_variable_warning(key); |
103 | 103 |
ignore_unused_variable_warning(val); |
104 | 104 |
ignore_unused_variable_warning(own_key); |
105 | 105 |
ignore_unused_variable_warning(own_val); |
106 | 106 |
} |
107 | 107 |
const Key& key; |
108 | 108 |
const Value& val; |
109 | 109 |
const typename _WriteMap::Key& own_key; |
110 | 110 |
const typename _WriteMap::Value& own_val; |
111 | 111 |
_WriteMap& m; |
112 | 112 |
}; |
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
/// Read/writable map concept |
116 | 116 |
|
117 | 117 |
/// Read/writable map concept. |
118 | 118 |
/// |
119 | 119 |
template<typename K, typename T> |
120 | 120 |
class ReadWriteMap : public ReadMap<K,T>, |
121 | 121 |
public WriteMap<K,T> |
122 | 122 |
{ |
123 | 123 |
public: |
124 | 124 |
/// The key type of the map. |
125 | 125 |
typedef K Key; |
126 | 126 |
/// \brief The value type of the map. |
127 | 127 |
/// (The type of objects associated with the keys). |
128 | 128 |
typedef T Value; |
129 | 129 |
|
130 | 130 |
/// Returns the value associated with the given key. |
131 | 131 |
Value operator[](const Key &) const { |
132 | 132 |
return *static_cast<Value *>(0); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
/// Sets the value associated with the given key. |
136 | 136 |
void set(const Key &, const Value &) {} |
137 | 137 |
|
138 | 138 |
template<typename _ReadWriteMap> |
139 | 139 |
struct Constraints { |
140 | 140 |
void constraints() { |
141 | 141 |
checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
142 | 142 |
checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
143 | 143 |
} |
144 | 144 |
}; |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
|
148 | 148 |
/// Dereferable map concept |
149 | 149 |
|
150 | 150 |
/// Dereferable map concept. |
151 | 151 |
/// |
152 | 152 |
template<typename K, typename T, typename R, typename CR> |
153 | 153 |
class ReferenceMap : public ReadWriteMap<K,T> |
154 | 154 |
{ |
155 | 155 |
public: |
156 | 156 |
/// Tag for reference maps. |
157 | 157 |
typedef True ReferenceMapTag; |
158 | 158 |
/// The key type of the map. |
159 | 159 |
typedef K Key; |
160 | 160 |
/// \brief The value type of the map. |
161 | 161 |
/// (The type of objects associated with the keys). |
162 | 162 |
typedef T Value; |
163 | 163 |
/// The reference type of the map. |
164 | 164 |
typedef R Reference; |
165 | 165 |
/// The const reference type of the map. |
166 | 166 |
typedef CR ConstReference; |
167 | 167 |
|
168 | 168 |
public: |
169 | 169 |
|
170 | 170 |
/// Returns a reference to the value associated with the given key. |
171 | 171 |
Reference operator[](const Key &) { |
172 | 172 |
return *static_cast<Value *>(0); |
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
/// Returns a const reference to the value associated with the given key. |
176 | 176 |
ConstReference operator[](const Key &) const { |
177 | 177 |
return *static_cast<Value *>(0); |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
/// Sets the value associated with the given key. |
181 | 181 |
void set(const Key &k,const Value &t) { operator[](k)=t; } |
182 | 182 |
|
183 | 183 |
template<typename _ReferenceMap> |
184 | 184 |
struct Constraints { |
185 | 185 |
void constraints() { |
186 | 186 |
checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
187 | 187 |
ref = m[key]; |
188 | 188 |
m[key] = val; |
189 | 189 |
m[key] = ref; |
190 | 190 |
m[key] = cref; |
191 | 191 |
own_ref = m[own_key]; |
192 | 192 |
m[own_key] = own_val; |
193 | 193 |
m[own_key] = own_ref; |
194 | 194 |
m[own_key] = own_cref; |
195 | 195 |
m[key] = m[own_key]; |
196 | 196 |
m[own_key] = m[key]; |
197 | 197 |
} |
198 | 198 |
const Key& key; |
199 | 199 |
Value& val; |
200 | 200 |
Reference ref; |
201 | 201 |
ConstReference cref; |
202 | 202 |
const typename _ReferenceMap::Key& own_key; |
203 | 203 |
typename _ReferenceMap::Value& own_val; |
204 | 204 |
typename _ReferenceMap::Reference own_ref; |
205 | 205 |
typename _ReferenceMap::ConstReference own_cref; |
206 | 206 |
_ReferenceMap& m; |
207 | 207 |
}; |
208 | 208 |
}; |
209 | 209 |
|
210 | 210 |
// @} |
211 | 211 |
|
212 | 212 |
} //namespace concepts |
213 | 213 |
|
214 | 214 |
} //namespace lemon |
215 | 215 |
|
216 | 216 |
#endif // LEMON_CONCEPT_MAPS_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-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 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 | 23 |
|
24 | 24 |
#ifndef LEMON_CONCEPT_PATH_H |
25 | 25 |
#define LEMON_CONCEPT_PATH_H |
26 | 26 |
|
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup concept |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// \brief A skeleton structure for representing directed paths in |
37 | 37 |
/// a digraph. |
38 | 38 |
/// |
39 | 39 |
/// A skeleton structure for representing directed paths in a |
40 | 40 |
/// digraph. |
41 | 41 |
/// \tparam _Digraph The digraph type in which the path is. |
42 | 42 |
/// |
43 | 43 |
/// In a sense, the path can be treated as a list of arcs. The |
44 | 44 |
/// lemon path type stores just this list. As a consequence it |
45 | 45 |
/// cannot enumerate the nodes in the path and the zero length |
46 | 46 |
/// paths cannot store the source. |
47 | 47 |
/// |
48 | 48 |
template <typename _Digraph> |
49 | 49 |
class Path { |
50 | 50 |
public: |
51 | 51 |
|
52 | 52 |
/// Type of the underlying digraph. |
53 | 53 |
typedef _Digraph Digraph; |
54 | 54 |
/// Arc type of the underlying digraph. |
55 | 55 |
typedef typename Digraph::Arc Arc; |
56 | 56 |
|
57 | 57 |
class ArcIt; |
58 | 58 |
|
59 | 59 |
/// \brief Default constructor |
60 | 60 |
Path() {} |
61 | 61 |
|
62 | 62 |
/// \brief Template constructor |
63 | 63 |
template <typename CPath> |
64 | 64 |
Path(const CPath& cpath) {} |
65 | 65 |
|
66 | 66 |
/// \brief Template assigment |
67 | 67 |
template <typename CPath> |
68 | 68 |
Path& operator=(const CPath& cpath) { |
69 | 69 |
ignore_unused_variable_warning(cpath); |
70 | 70 |
return *this; |
71 | 71 |
} |
72 | 72 |
|
73 | 73 |
/// Length of the path ie. the number of arcs in the path. |
74 | 74 |
int length() const { return 0;} |
75 | 75 |
|
76 | 76 |
/// Returns whether the path is empty. |
77 | 77 |
bool empty() const { return true;} |
78 | 78 |
|
79 | 79 |
/// Resets the path to an empty path. |
80 | 80 |
void clear() {} |
81 | 81 |
|
82 | 82 |
/// \brief LEMON style iterator for path arcs |
83 | 83 |
/// |
84 | 84 |
/// This class is used to iterate on the arcs of the paths. |
85 | 85 |
class ArcIt { |
86 | 86 |
public: |
87 | 87 |
/// Default constructor |
88 | 88 |
ArcIt() {} |
89 | 89 |
/// Invalid constructor |
90 | 90 |
ArcIt(Invalid) {} |
91 | 91 |
/// Constructor for first arc |
92 | 92 |
ArcIt(const Path &) {} |
93 | 93 |
|
94 | 94 |
/// Conversion to Arc |
95 | 95 |
operator Arc() const { return INVALID; } |
96 | 96 |
|
97 | 97 |
/// Next arc |
98 | 98 |
ArcIt& operator++() {return *this;} |
99 | 99 |
|
100 | 100 |
/// Comparison operator |
101 | 101 |
bool operator==(const ArcIt&) const {return true;} |
102 | 102 |
/// Comparison operator |
103 | 103 |
bool operator!=(const ArcIt&) const {return true;} |
104 | 104 |
/// Comparison operator |
105 | 105 |
bool operator<(const ArcIt&) const {return false;} |
106 | 106 |
|
107 | 107 |
}; |
108 | 108 |
|
109 | 109 |
template <typename _Path> |
110 | 110 |
struct Constraints { |
111 | 111 |
void constraints() { |
112 | 112 |
Path<Digraph> pc; |
113 | 113 |
_Path p, pp(pc); |
114 | 114 |
int l = p.length(); |
115 | 115 |
int e = p.empty(); |
116 | 116 |
p.clear(); |
117 | 117 |
|
118 | 118 |
p = pc; |
119 | 119 |
|
120 | 120 |
typename _Path::ArcIt id, ii(INVALID), i(p); |
121 | 121 |
|
122 | 122 |
++i; |
123 | 123 |
typename Digraph::Arc ed = i; |
124 | 124 |
|
125 | 125 |
e = (i == ii); |
126 | 126 |
e = (i != ii); |
127 | 127 |
e = (i < ii); |
128 | 128 |
|
129 | 129 |
ignore_unused_variable_warning(l); |
130 | 130 |
ignore_unused_variable_warning(pp); |
131 | 131 |
ignore_unused_variable_warning(e); |
132 | 132 |
ignore_unused_variable_warning(id); |
133 | 133 |
ignore_unused_variable_warning(ii); |
134 | 134 |
ignore_unused_variable_warning(ed); |
135 | 135 |
} |
136 | 136 |
}; |
137 | 137 |
|
138 | 138 |
}; |
139 | 139 |
|
140 | 140 |
namespace _path_bits { |
141 | 141 |
|
142 | 142 |
template <typename _Digraph, typename _Path, typename RevPathTag = void> |
143 | 143 |
struct PathDumperConstraints { |
144 | 144 |
void constraints() { |
145 | 145 |
int l = p.length(); |
146 | 146 |
int e = p.empty(); |
147 | 147 |
|
148 | 148 |
typename _Path::ArcIt id, i(p); |
149 | 149 |
|
150 | 150 |
++i; |
151 | 151 |
typename _Digraph::Arc ed = i; |
152 | 152 |
|
153 | 153 |
e = (i == INVALID); |
154 | 154 |
e = (i != INVALID); |
155 | 155 |
|
156 | 156 |
ignore_unused_variable_warning(l); |
157 | 157 |
ignore_unused_variable_warning(e); |
158 | 158 |
ignore_unused_variable_warning(id); |
159 | 159 |
ignore_unused_variable_warning(ed); |
160 | 160 |
} |
161 | 161 |
_Path& p; |
162 | 162 |
}; |
163 | 163 |
|
164 | 164 |
template <typename _Digraph, typename _Path> |
165 | 165 |
struct PathDumperConstraints< |
166 | 166 |
_Digraph, _Path, |
167 | 167 |
typename enable_if<typename _Path::RevPathTag, void>::type |
168 | 168 |
> { |
169 | 169 |
void constraints() { |
170 | 170 |
int l = p.length(); |
171 | 171 |
int e = p.empty(); |
172 | 172 |
|
173 | 173 |
typename _Path::RevArcIt id, i(p); |
174 | 174 |
|
175 | 175 |
++i; |
176 | 176 |
typename _Digraph::Arc ed = i; |
177 | 177 |
|
178 | 178 |
e = (i == INVALID); |
179 | 179 |
e = (i != INVALID); |
180 | 180 |
|
181 | 181 |
ignore_unused_variable_warning(l); |
182 | 182 |
ignore_unused_variable_warning(e); |
183 | 183 |
ignore_unused_variable_warning(id); |
184 | 184 |
ignore_unused_variable_warning(ed); |
185 | 185 |
} |
186 | 186 |
_Path& p; |
187 | 187 |
}; |
188 | 188 |
|
189 | 189 |
} |
190 | 190 |
|
191 | 191 |
|
192 | 192 |
/// \brief A skeleton structure for path dumpers. |
193 | 193 |
/// |
194 | 194 |
/// A skeleton structure for path dumpers. The path dumpers are |
195 | 195 |
/// the generalization of the paths. The path dumpers can |
196 | 196 |
/// enumerate the arcs of the path wheter in forward or in |
197 | 197 |
/// backward order. In most time these classes are not used |
198 | 198 |
/// directly rather it used to assign a dumped class to a real |
199 | 199 |
/// path type. |
200 | 200 |
/// |
201 | 201 |
/// The main purpose of this concept is that the shortest path |
202 | 202 |
/// algorithms can enumerate easily the arcs in reverse order. |
203 | 203 |
/// If we would like to give back a real path from these |
204 | 204 |
/// algorithms then we should create a temporarly path object. In |
205 | 205 |
/// LEMON such algorithms gives back a path dumper what can |
206 | 206 |
/// assigned to a real path and the dumpers can be implemented as |
207 | 207 |
/// an adaptor class to the predecessor map. |
208 | 208 |
|
209 | 209 |
/// \tparam _Digraph The digraph type in which the path is. |
210 | 210 |
/// |
211 | 211 |
/// The paths can be constructed from any path type by a |
212 | 212 |
/// template constructor or a template assignment operator. |
213 | 213 |
/// |
214 | 214 |
template <typename _Digraph> |
215 | 215 |
class PathDumper { |
216 | 216 |
public: |
217 | 217 |
|
218 | 218 |
/// Type of the underlying digraph. |
219 | 219 |
typedef _Digraph Digraph; |
220 | 220 |
/// Arc type of the underlying digraph. |
221 | 221 |
typedef typename Digraph::Arc Arc; |
222 | 222 |
|
223 | 223 |
/// Length of the path ie. the number of arcs in the path. |
224 | 224 |
int length() const { return 0;} |
225 | 225 |
|
226 | 226 |
/// Returns whether the path is empty. |
227 | 227 |
bool empty() const { return true;} |
228 | 228 |
|
229 | 229 |
/// \brief Forward or reverse dumping |
230 | 230 |
/// |
231 | 231 |
/// If the RevPathTag is defined and true then reverse dumping |
232 | 232 |
/// is provided in the path dumper. In this case instead of the |
233 | 233 |
/// ArcIt the RevArcIt iterator should be implemented in the |
234 | 234 |
/// dumper. |
235 | 235 |
typedef False RevPathTag; |
236 | 236 |
|
237 | 237 |
/// \brief LEMON style iterator for path arcs |
238 | 238 |
/// |
239 | 239 |
/// This class is used to iterate on the arcs of the paths. |
240 | 240 |
class ArcIt { |
241 | 241 |
public: |
242 | 242 |
/// Default constructor |
243 | 243 |
ArcIt() {} |
244 | 244 |
/// Invalid constructor |
245 | 245 |
ArcIt(Invalid) {} |
246 | 246 |
/// Constructor for first arc |
247 | 247 |
ArcIt(const PathDumper&) {} |
248 | 248 |
|
249 | 249 |
/// Conversion to Arc |
250 | 250 |
operator Arc() const { return INVALID; } |
251 | 251 |
|
252 | 252 |
/// Next arc |
253 | 253 |
ArcIt& operator++() {return *this;} |
254 | 254 |
|
255 | 255 |
/// Comparison operator |
256 | 256 |
bool operator==(const ArcIt&) const {return true;} |
257 | 257 |
/// Comparison operator |
258 | 258 |
bool operator!=(const ArcIt&) const {return true;} |
259 | 259 |
/// Comparison operator |
260 | 260 |
bool operator<(const ArcIt&) const {return false;} |
261 | 261 |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)