!
!
!
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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, const 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 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 six |
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 \ref maps-page "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_utils.h> |
35 | 35 |
#include<lemon/graph_to_eps.h> |
36 | 36 |
#include<lemon/math.h> |
37 | 37 |
|
38 | 38 |
using namespace std; |
39 | 39 |
using namespace lemon; |
40 | 40 |
|
41 | 41 |
int main() |
42 | 42 |
{ |
43 | 43 |
Palette palette; |
44 | 44 |
Palette paletteW(true); |
45 | 45 |
|
46 | 46 |
// Create a small digraph |
47 | 47 |
ListDigraph g; |
48 | 48 |
typedef ListDigraph::Node Node; |
49 | 49 |
typedef ListDigraph::NodeIt NodeIt; |
50 | 50 |
typedef ListDigraph::Arc Arc; |
51 | 51 |
typedef dim2::Point<int> Point; |
52 | 52 |
|
53 | 53 |
Node n1=g.addNode(); |
54 | 54 |
Node n2=g.addNode(); |
55 | 55 |
Node n3=g.addNode(); |
56 | 56 |
Node n4=g.addNode(); |
57 | 57 |
Node n5=g.addNode(); |
58 | 58 |
|
59 | 59 |
ListDigraph::NodeMap<Point> coords(g); |
60 | 60 |
ListDigraph::NodeMap<double> sizes(g); |
61 | 61 |
ListDigraph::NodeMap<int> colors(g); |
62 | 62 |
ListDigraph::NodeMap<int> shapes(g); |
63 | 63 |
ListDigraph::ArcMap<int> acolors(g); |
64 | 64 |
ListDigraph::ArcMap<int> widths(g); |
65 | 65 |
|
66 | 66 |
coords[n1]=Point(50,50); sizes[n1]=1; colors[n1]=1; shapes[n1]=0; |
67 | 67 |
coords[n2]=Point(50,70); sizes[n2]=2; colors[n2]=2; shapes[n2]=2; |
68 | 68 |
coords[n3]=Point(70,70); sizes[n3]=1; colors[n3]=3; shapes[n3]=0; |
69 | 69 |
coords[n4]=Point(70,50); sizes[n4]=2; colors[n4]=4; shapes[n4]=1; |
70 | 70 |
coords[n5]=Point(85,60); sizes[n5]=3; colors[n5]=5; shapes[n5]=2; |
71 | 71 |
|
72 | 72 |
Arc a; |
73 | 73 |
|
74 | 74 |
a=g.addArc(n1,n2); acolors[a]=0; widths[a]=1; |
75 | 75 |
a=g.addArc(n2,n3); acolors[a]=0; widths[a]=1; |
76 | 76 |
a=g.addArc(n3,n5); acolors[a]=0; widths[a]=3; |
77 | 77 |
a=g.addArc(n5,n4); acolors[a]=0; widths[a]=1; |
78 | 78 |
a=g.addArc(n4,n1); acolors[a]=0; widths[a]=1; |
79 | 79 |
a=g.addArc(n2,n4); acolors[a]=1; widths[a]=2; |
80 | 80 |
a=g.addArc(n3,n4); acolors[a]=2; widths[a]=1; |
81 | 81 |
|
82 | 82 |
IdMap<ListDigraph,Node> id(g); |
83 | 83 |
|
84 | 84 |
// Create five .eps files showing the digraph with different options |
85 | 85 |
cout << "Create 'graph_to_eps_demo_out_1_pure.eps'" << endl; |
86 | 86 |
graphToEps(g,"graph_to_eps_demo_out_1_pure.eps"). |
87 | 87 |
coords(coords). |
88 | 88 |
title("Sample .eps figure"). |
89 | 89 |
copyright("(C) 2003-2008 LEMON Project"). |
90 | 90 |
run(); |
91 | 91 |
|
92 | 92 |
cout << "Create 'graph_to_eps_demo_out_2.eps'" << endl; |
93 | 93 |
graphToEps(g,"graph_to_eps_demo_out_2.eps"). |
94 | 94 |
coords(coords). |
95 | 95 |
title("Sample .eps figure"). |
96 | 96 |
copyright("(C) 2003-2008 LEMON Project"). |
97 | 97 |
absoluteNodeSizes().absoluteArcWidths(). |
98 | 98 |
nodeScale(2).nodeSizes(sizes). |
99 | 99 |
nodeShapes(shapes). |
100 | 100 |
nodeColors(composeMap(palette,colors)). |
101 | 101 |
arcColors(composeMap(palette,acolors)). |
102 | 102 |
arcWidthScale(.4).arcWidths(widths). |
103 | 103 |
nodeTexts(id).nodeTextSize(3). |
104 | 104 |
run(); |
105 | 105 |
|
106 | 106 |
cout << "Create 'graph_to_eps_demo_out_3_arr.eps'" << endl; |
107 | 107 |
graphToEps(g,"graph_to_eps_demo_out_3_arr.eps"). |
108 | 108 |
title("Sample .eps figure (with arrowheads)"). |
109 | 109 |
copyright("(C) 2003-2008 LEMON Project"). |
110 | 110 |
absoluteNodeSizes().absoluteArcWidths(). |
111 | 111 |
nodeColors(composeMap(palette,colors)). |
112 | 112 |
coords(coords). |
113 | 113 |
nodeScale(2).nodeSizes(sizes). |
114 | 114 |
nodeShapes(shapes). |
115 | 115 |
arcColors(composeMap(palette,acolors)). |
116 | 116 |
arcWidthScale(.4).arcWidths(widths). |
117 | 117 |
nodeTexts(id).nodeTextSize(3). |
118 | 118 |
drawArrows().arrowWidth(2).arrowLength(2). |
119 | 119 |
run(); |
120 | 120 |
|
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_par.eps'" << endl; |
133 | 133 |
graphToEps(g,"graph_to_eps_demo_out_par.eps"). |
134 | 134 |
//scale(10). |
135 | 135 |
title("Sample .eps figure (parallel arcs)"). |
136 | 136 |
copyright("(C) 2003-2008 LEMON Project"). |
137 | 137 |
absoluteNodeSizes().absoluteArcWidths(). |
138 | 138 |
nodeShapes(shapes). |
139 | 139 |
coords(coords). |
140 | 140 |
nodeScale(2).nodeSizes(sizes). |
141 | 141 |
nodeColors(composeMap(palette,colors)). |
142 | 142 |
arcColors(composeMap(palette,acolors)). |
143 | 143 |
arcWidthScale(.4).arcWidths(widths). |
144 | 144 |
nodeTexts(id).nodeTextSize(3). |
145 | 145 |
enableParallel().parArcDist(1.5). |
146 | 146 |
run(); |
147 | 147 |
|
148 | 148 |
cout << "Create 'graph_to_eps_demo_out_4_par_arr.eps'" << endl; |
149 | 149 |
graphToEps(g,"graph_to_eps_demo_out_4_par_arr.eps"). |
150 | 150 |
title("Sample .eps figure (parallel arcs and arrowheads)"). |
151 | 151 |
copyright("(C) 2003-2008 LEMON Project"). |
152 | 152 |
absoluteNodeSizes().absoluteArcWidths(). |
153 | 153 |
nodeScale(2).nodeSizes(sizes). |
154 | 154 |
coords(coords). |
155 | 155 |
nodeShapes(shapes). |
156 | 156 |
nodeColors(composeMap(palette,colors)). |
157 | 157 |
arcColors(composeMap(palette,acolors)). |
158 | 158 |
arcWidthScale(.3).arcWidths(widths). |
159 | 159 |
nodeTexts(id).nodeTextSize(3). |
160 | 160 |
enableParallel().parArcDist(1). |
161 | 161 |
drawArrows().arrowWidth(1).arrowLength(1). |
162 | 162 |
run(); |
163 | 163 |
|
164 | 164 |
cout << "Create 'graph_to_eps_demo_out_5_par_arr_a4.eps'" << endl; |
165 | 165 |
graphToEps(g,"graph_to_eps_demo_out_5_par_arr_a4.eps"). |
166 | 166 |
title("Sample .eps figure (fits to A4)"). |
167 | 167 |
copyright("(C) 2003-2008 LEMON Project"). |
168 | 168 |
scaleToA4(). |
169 | 169 |
absoluteNodeSizes().absoluteArcWidths(). |
170 | 170 |
nodeScale(2).nodeSizes(sizes). |
171 | 171 |
coords(coords). |
172 | 172 |
nodeShapes(shapes). |
173 | 173 |
nodeColors(composeMap(palette,colors)). |
174 | 174 |
arcColors(composeMap(palette,acolors)). |
175 | 175 |
arcWidthScale(.3).arcWidths(widths). |
176 | 176 |
nodeTexts(id).nodeTextSize(3). |
177 | 177 |
enableParallel().parArcDist(1). |
178 | 178 |
drawArrows().arrowWidth(1).arrowLength(1). |
179 | 179 |
run(); |
180 | 180 |
|
181 | 181 |
// Create an .eps file showing the colors of a default Palette |
182 | 182 |
ListDigraph h; |
183 | 183 |
ListDigraph::NodeMap<int> hcolors(h); |
184 | 184 |
ListDigraph::NodeMap<Point> hcoords(h); |
185 | 185 |
|
186 | 186 |
int cols=int(sqrt(double(palette.size()))); |
187 | 187 |
for(int i=0;i<int(paletteW.size());i++) { |
188 | 188 |
Node n=h.addNode(); |
189 | 189 |
hcoords[n]=Point(1+i%cols,1+i/cols); |
190 | 190 |
hcolors[n]=i; |
191 | 191 |
} |
192 | 192 |
|
193 | 193 |
cout << "Create 'graph_to_eps_demo_out_6_colors.eps'" << endl; |
194 | 194 |
graphToEps(h,"graph_to_eps_demo_out_6_colors.eps"). |
195 | 195 |
scale(60). |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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("digraph.lgf", g). // 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 (DataFormatError& 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(std::cout, 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 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 (=variables used locally in methods) should look like the following. |
93 | 93 |
|
94 | 94 |
\code |
95 | 95 |
all_lower_case_with_underscores |
96 | 96 |
\endcode |
97 | 97 |
|
98 | 98 |
\subsection pri-loc-var Private member variables |
99 | 99 |
|
100 | 100 |
Private member variables should start with underscore |
101 | 101 |
|
102 | 102 |
\code |
103 | 103 |
_start_with_underscores |
104 | 104 |
\endcode |
105 | 105 |
|
106 | 106 |
\subsection cs-excep Exceptions |
107 | 107 |
|
108 | 108 |
When writing exceptions please comply the following naming conventions. |
109 | 109 |
|
110 | 110 |
\code |
111 | 111 |
ClassNameEndsWithException |
112 | 112 |
\endcode |
113 | 113 |
|
114 | 114 |
or |
115 | 115 |
|
116 | 116 |
\code |
117 | 117 |
ClassNameEndsWithError |
118 | 118 |
\endcode |
119 | 119 |
|
120 | 120 |
\section header-template Template Header File |
121 | 121 |
|
122 | 122 |
Each LEMON header file should look like this: |
123 | 123 |
|
124 | 124 |
\include template.h |
125 | 125 |
|
126 | 126 |
*/ |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 application. |
22 | 22 |
|
23 | 23 |
This directory contains several simple demo application, 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 |
Auxiliary (and the whole generated) documentation. |
32 | 32 |
*/ |
33 | 33 |
|
34 | 34 |
/** |
35 | 35 |
\dir test |
36 | 36 |
\brief Test programs. |
37 | 37 |
|
38 | 38 |
This directory contains several test programs that check the consistency |
39 | 39 |
of the code. |
40 | 40 |
*/ |
41 | 41 |
|
42 | 42 |
/** |
43 | 43 |
\dir tools |
44 | 44 |
\brief Some useful executables |
45 | 45 |
|
46 | 46 |
This directory contains the sources of some useful complete executables. |
47 | 47 |
|
48 | 48 |
*/ |
49 | 49 |
|
50 | 50 |
|
51 | 51 |
|
52 | 52 |
/** |
53 | 53 |
\dir lemon |
54 | 54 |
\brief Base include directory of LEMON |
55 | 55 |
|
56 | 56 |
This is the base directory of lemon includes, so each include file must be |
57 | 57 |
prefixed with this, e.g. |
58 | 58 |
\code |
59 | 59 |
#include<lemon/list_graph.h> |
60 | 60 |
#include<lemon/dijkstra.h> |
61 | 61 |
\endcode |
62 | 62 |
*/ |
63 | 63 |
|
64 | 64 |
/** |
65 | 65 |
\dir concepts |
66 | 66 |
\brief Concept descriptors and checking classes |
67 | 67 |
|
68 | 68 |
This directory contains the concept descriptors and concept checkers. As a user |
69 | 69 |
you typically don't have to deal with these files. |
70 | 70 |
*/ |
71 | 71 |
|
72 | 72 |
/** |
73 | 73 |
\dir bits |
74 | 74 |
\brief Implementation helper files |
75 | 75 |
|
76 | 76 |
This directory contains some helper classes to implement graphs, maps and |
77 | 77 |
some other classes. As a user you typically don't have to deal with these |
78 | 78 |
files. |
79 | 79 |
*/ |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
@defgroup datas Data Structures |
21 | 21 |
This group describes the several data structures implemented in LEMON. |
22 | 22 |
*/ |
23 | 23 |
|
24 | 24 |
/** |
25 | 25 |
@defgroup graphs Graph Structures |
26 | 26 |
@ingroup datas |
27 | 27 |
\brief Graph structures implemented in LEMON. |
28 | 28 |
|
29 | 29 |
The implementation of combinatorial algorithms heavily relies on |
30 | 30 |
efficient graph implementations. LEMON offers data structures which are |
31 | 31 |
planned to be easily used in an experimental phase of implementation studies, |
32 | 32 |
and thereafter the program code can be made efficient by small modifications. |
33 | 33 |
|
34 | 34 |
The most efficient implementation of diverse applications require the |
35 | 35 |
usage of different physical graph implementations. These differences |
36 | 36 |
appear in the size of graph we require to handle, memory or time usage |
37 | 37 |
limitations or in the set of operations through which the graph can be |
38 | 38 |
accessed. LEMON provides several physical graph structures to meet |
39 | 39 |
the diverging requirements of the possible users. In order to save on |
40 | 40 |
running time or on memory usage, some structures may fail to provide |
41 | 41 |
some graph features like arc/edge or node deletion. |
42 | 42 |
|
43 | 43 |
Alteration of standard containers need a very limited number of |
44 | 44 |
operations, these together satisfy the everyday requirements. |
45 | 45 |
In the case of graph structures, different operations are needed which do |
46 | 46 |
not alter the physical graph, but gives another view. If some nodes or |
47 | 47 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
48 | 48 |
this is the case. It also may happen that in a flow implementation |
49 | 49 |
the residual graph can be accessed by another algorithm, or a node-set |
50 | 50 |
is to be shrunk for another algorithm. |
51 | 51 |
LEMON also provides a variety of graphs for these requirements called |
52 | 52 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
53 | 53 |
in conjunction with other graph representations. |
54 | 54 |
|
55 | 55 |
You are free to use the graph structure that fit your requirements |
56 | 56 |
the best, most graph algorithms and auxiliary data structures can be used |
57 | 57 |
with any graph structures. |
58 | 58 |
*/ |
59 | 59 |
|
60 | 60 |
/** |
61 | 61 |
@defgroup semi_adaptors Semi-Adaptor Classes for Graphs |
62 | 62 |
@ingroup graphs |
63 | 63 |
\brief Graph types between real graphs and graph adaptors. |
64 | 64 |
|
65 | 65 |
This group describes some graph types between real graphs and graph adaptors. |
66 | 66 |
These classes wrap graphs to give new functionality as the adaptors do it. |
67 | 67 |
On the other hand they are not light-weight structures as the adaptors. |
68 | 68 |
*/ |
69 | 69 |
|
70 | 70 |
/** |
71 | 71 |
@defgroup maps Maps |
72 | 72 |
@ingroup datas |
73 | 73 |
\brief Map structures implemented in LEMON. |
74 | 74 |
|
75 | 75 |
This group describes the map structures implemented in LEMON. |
76 | 76 |
|
77 | 77 |
LEMON provides several special purpose maps that e.g. combine |
78 | 78 |
new maps from existing ones. |
79 | 79 |
*/ |
80 | 80 |
|
81 | 81 |
/** |
82 | 82 |
@defgroup graph_maps Graph Maps |
83 | 83 |
@ingroup maps |
84 | 84 |
\brief Special graph-related maps. |
85 | 85 |
|
86 | 86 |
This group describes maps that are specifically designed to assign |
87 | 87 |
values to the nodes and arcs of graphs. |
88 | 88 |
*/ |
89 | 89 |
|
90 | 90 |
|
91 | 91 |
/** |
92 | 92 |
\defgroup map_adaptors Map Adaptors |
93 | 93 |
\ingroup maps |
94 | 94 |
\brief Tools to create new maps from existing ones |
95 | 95 |
|
96 | 96 |
This group describes map adaptors that are used to create "implicit" |
97 | 97 |
maps from other maps. |
98 | 98 |
|
99 | 99 |
Most of them are \ref lemon::concepts::ReadMap "read-only maps". |
100 | 100 |
They can make arithmetic and logical operations between one or two maps |
101 | 101 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
102 | 102 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
103 | 103 |
|
104 | 104 |
The typical usage of this classes is passing implicit maps to |
105 | 105 |
algorithms. If a function type algorithm is called then the function |
106 | 106 |
type map adaptors can be used comfortable. For example let's see the |
107 | 107 |
usage of map adaptors with the \c digraphToEps() function. |
108 | 108 |
\code |
109 | 109 |
Color nodeColor(int deg) { |
110 | 110 |
if (deg >= 2) { |
111 | 111 |
return Color(0.5, 0.0, 0.5); |
112 | 112 |
} else if (deg == 1) { |
113 | 113 |
return Color(1.0, 0.5, 1.0); |
114 | 114 |
} else { |
115 | 115 |
return Color(0.0, 0.0, 0.0); |
116 | 116 |
} |
117 | 117 |
} |
118 | 118 |
|
119 | 119 |
Digraph::NodeMap<int> degree_map(graph); |
120 | 120 |
|
121 | 121 |
digraphToEps(graph, "graph.eps") |
122 | 122 |
.coords(coords).scaleToA4().undirected() |
123 | 123 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
124 | 124 |
.run(); |
125 | 125 |
\endcode |
126 | 126 |
The \c functorToMap() function makes an \c int to \c Color map from the |
127 | 127 |
\e nodeColor() function. The \c composeMap() compose the \e degree_map |
128 | 128 |
and the previously created map. The composed map is a proper function to |
129 | 129 |
get the color of each node. |
130 | 130 |
|
131 | 131 |
The usage with class type algorithms is little bit harder. In this |
132 | 132 |
case the function type map adaptors can not be used, because the |
133 | 133 |
function map adaptors give back temporary objects. |
134 | 134 |
\code |
135 | 135 |
Digraph graph; |
136 | 136 |
|
137 | 137 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
138 | 138 |
DoubleArcMap length(graph); |
139 | 139 |
DoubleArcMap speed(graph); |
140 | 140 |
|
141 | 141 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
142 | 142 |
TimeMap time(length, speed); |
143 | 143 |
|
144 | 144 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
145 | 145 |
dijkstra.run(source, target); |
146 | 146 |
\endcode |
147 | 147 |
We have a length map and a maximum speed map on the arcs of a digraph. |
148 | 148 |
The minimum time to pass the arc can be calculated as the division of |
149 | 149 |
the two maps which can be done implicitly with the \c DivMap template |
150 | 150 |
class. We use the implicit minimum time map as the length map of the |
151 | 151 |
\c Dijkstra algorithm. |
152 | 152 |
*/ |
153 | 153 |
|
154 | 154 |
/** |
155 | 155 |
@defgroup matrices Matrices |
156 | 156 |
@ingroup datas |
157 | 157 |
\brief Two dimensional data storages implemented in LEMON. |
158 | 158 |
|
159 | 159 |
This group describes two dimensional data storages implemented in LEMON. |
160 | 160 |
*/ |
161 | 161 |
|
162 | 162 |
/** |
163 | 163 |
@defgroup paths Path Structures |
164 | 164 |
@ingroup datas |
165 | 165 |
\brief Path structures implemented in LEMON. |
166 | 166 |
|
167 | 167 |
This group describes the path structures implemented in LEMON. |
168 | 168 |
|
169 | 169 |
LEMON provides flexible data structures to work with paths. |
170 | 170 |
All of them have similar interfaces and they can be copied easily with |
171 | 171 |
assignment operators and copy constructors. This makes it easy and |
172 | 172 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
173 | 173 |
any kind of path structure. |
174 | 174 |
|
175 | 175 |
\sa lemon::concepts::Path |
176 | 176 |
|
177 | 177 |
*/ |
178 | 178 |
|
179 | 179 |
/** |
180 | 180 |
@defgroup auxdat Auxiliary Data Structures |
181 | 181 |
@ingroup datas |
182 | 182 |
\brief Auxiliary data structures implemented in LEMON. |
183 | 183 |
|
184 | 184 |
This group describes some data structures implemented in LEMON in |
185 | 185 |
order to make it easier to implement combinatorial algorithms. |
186 | 186 |
*/ |
187 | 187 |
|
188 | 188 |
|
189 | 189 |
/** |
190 | 190 |
@defgroup algs Algorithms |
191 | 191 |
\brief This group describes the several algorithms |
192 | 192 |
implemented in LEMON. |
193 | 193 |
|
194 | 194 |
This group describes the several algorithms |
195 | 195 |
implemented in LEMON. |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
|
60 | 60 |
*/ |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 **argv) :_argc(argc), _argv(argv), |
30 | 30 |
_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 |
|
37 | 37 |
ArgParser::~ArgParser() |
38 | 38 |
{ |
39 | 39 |
for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) |
40 | 40 |
if(i->second.self_delete) |
41 | 41 |
switch(i->second.type) { |
42 | 42 |
case BOOL: |
43 | 43 |
delete i->second.bool_p; |
44 | 44 |
break; |
45 | 45 |
case STRING: |
46 | 46 |
delete i->second.string_p; |
47 | 47 |
break; |
48 | 48 |
case DOUBLE: |
49 | 49 |
delete i->second.double_p; |
50 | 50 |
break; |
51 | 51 |
case INTEGER: |
52 | 52 |
delete i->second.int_p; |
53 | 53 |
break; |
54 | 54 |
case UNKNOWN: |
55 | 55 |
break; |
56 | 56 |
case FUNC: |
57 | 57 |
break; |
58 | 58 |
} |
59 | 59 |
} |
60 | 60 |
|
61 | 61 |
|
62 | 62 |
ArgParser &ArgParser::intOption(const std::string &name, |
63 | 63 |
const std::string &help, |
64 | 64 |
int value, bool obl) |
65 | 65 |
{ |
66 | 66 |
ParData p; |
67 | 67 |
p.int_p=new int(value); |
68 | 68 |
p.self_delete=true; |
69 | 69 |
p.help=help; |
70 | 70 |
p.type=INTEGER; |
71 | 71 |
p.mandatory=obl; |
72 | 72 |
_opts[name]=p; |
73 | 73 |
return *this; |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
ArgParser &ArgParser::doubleOption(const std::string &name, |
77 | 77 |
const std::string &help, |
78 | 78 |
double value, bool obl) |
79 | 79 |
{ |
80 | 80 |
ParData p; |
81 | 81 |
p.double_p=new double(value); |
82 | 82 |
p.self_delete=true; |
83 | 83 |
p.help=help; |
84 | 84 |
p.type=DOUBLE; |
85 | 85 |
p.mandatory=obl; |
86 | 86 |
_opts[name]=p; |
87 | 87 |
return *this; |
88 | 88 |
} |
89 | 89 |
|
90 | 90 |
ArgParser &ArgParser::boolOption(const std::string &name, |
91 | 91 |
const std::string &help, |
92 | 92 |
bool value, bool obl) |
93 | 93 |
{ |
94 | 94 |
ParData p; |
95 | 95 |
p.bool_p=new bool(value); |
96 | 96 |
p.self_delete=true; |
97 | 97 |
p.help=help; |
98 | 98 |
p.type=BOOL; |
99 | 99 |
p.mandatory=obl; |
100 | 100 |
_opts[name]=p; |
101 | 101 |
return *this; |
102 | 102 |
} |
103 | 103 |
|
104 | 104 |
ArgParser &ArgParser::stringOption(const std::string &name, |
105 | 105 |
const std::string &help, |
106 | 106 |
std::string value, bool obl) |
107 | 107 |
{ |
108 | 108 |
ParData p; |
109 | 109 |
p.string_p=new std::string(value); |
110 | 110 |
p.self_delete=true; |
111 | 111 |
p.help=help; |
112 | 112 |
p.type=STRING; |
113 | 113 |
p.mandatory=obl; |
114 | 114 |
_opts[name]=p; |
115 | 115 |
return *this; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
ArgParser &ArgParser::refOption(const std::string &name, |
119 | 119 |
const std::string &help, |
120 | 120 |
int &ref, bool obl) |
121 | 121 |
{ |
122 | 122 |
ParData p; |
123 | 123 |
p.int_p=&ref; |
124 | 124 |
p.self_delete=false; |
125 | 125 |
p.help=help; |
126 | 126 |
p.type=INTEGER; |
127 | 127 |
p.mandatory=obl; |
128 | 128 |
_opts[name]=p; |
129 | 129 |
return *this; |
130 | 130 |
} |
131 | 131 |
|
132 | 132 |
ArgParser &ArgParser::refOption(const std::string &name, |
133 | 133 |
const std::string &help, |
134 | 134 |
double &ref, bool obl) |
135 | 135 |
{ |
136 | 136 |
ParData p; |
137 | 137 |
p.double_p=&ref; |
138 | 138 |
p.self_delete=false; |
139 | 139 |
p.help=help; |
140 | 140 |
p.type=DOUBLE; |
141 | 141 |
p.mandatory=obl; |
142 | 142 |
_opts[name]=p; |
143 | 143 |
return *this; |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
ArgParser &ArgParser::refOption(const std::string &name, |
147 | 147 |
const std::string &help, |
148 | 148 |
bool &ref, bool obl) |
149 | 149 |
{ |
150 | 150 |
ParData p; |
151 | 151 |
p.bool_p=&ref; |
152 | 152 |
p.self_delete=false; |
153 | 153 |
p.help=help; |
154 | 154 |
p.type=BOOL; |
155 | 155 |
p.mandatory=obl; |
156 | 156 |
_opts[name]=p; |
157 | 157 |
|
158 | 158 |
ref = false; |
159 | 159 |
|
160 | 160 |
return *this; |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
ArgParser &ArgParser::refOption(const std::string &name, |
164 | 164 |
const std::string &help, |
165 | 165 |
std::string &ref, bool obl) |
166 | 166 |
{ |
167 | 167 |
ParData p; |
168 | 168 |
p.string_p=&ref; |
169 | 169 |
p.self_delete=false; |
170 | 170 |
p.help=help; |
171 | 171 |
p.type=STRING; |
172 | 172 |
p.mandatory=obl; |
173 | 173 |
_opts[name]=p; |
174 | 174 |
return *this; |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
ArgParser &ArgParser::funcOption(const std::string &name, |
178 | 178 |
const std::string &help, |
179 | 179 |
void (*func)(void *),void *data) |
180 | 180 |
{ |
181 | 181 |
ParData p; |
182 | 182 |
p.func_p.p=func; |
183 | 183 |
p.func_p.data=data; |
184 | 184 |
p.self_delete=false; |
185 | 185 |
p.help=help; |
186 | 186 |
p.type=FUNC; |
187 | 187 |
p.mandatory=false; |
188 | 188 |
_opts[name]=p; |
189 | 189 |
return *this; |
190 | 190 |
} |
191 | 191 |
|
192 | 192 |
ArgParser &ArgParser::optionGroup(const std::string &group, |
193 | 193 |
const std::string &opt) |
194 | 194 |
{ |
195 | 195 |
Opts::iterator i = _opts.find(opt); |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
20 | 20 |
#define LEMON_ARG_PARSER |
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 **_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 **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. |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_log(const char *file, int line, const char *function, |
31 | 31 |
const char *message, const char *assertion) |
32 | 32 |
{ |
33 | 33 |
std::cerr << file << ":" << line << ": "; |
34 | 34 |
if (function) |
35 | 35 |
std::cerr << function << ": "; |
36 | 36 |
std::cerr << message; |
37 | 37 |
if (assertion) |
38 | 38 |
std::cerr << " (assertion '" << assertion << "' failed)"; |
39 | 39 |
std::cerr << std::endl; |
40 | 40 |
} |
41 | 41 |
|
42 | 42 |
inline void assert_fail_abort(const char *file, int line, |
43 | 43 |
const char *function, const char* message, |
44 | 44 |
const char *assertion) |
45 | 45 |
{ |
46 | 46 |
assert_fail_log(file, line, function, message, assertion); |
47 | 47 |
std::abort(); |
48 | 48 |
} |
49 | 49 |
|
50 | 50 |
namespace _assert_bits { |
51 | 51 |
|
52 | 52 |
|
53 | 53 |
inline const char* cstringify(const std::string& str) { |
54 | 54 |
return str.c_str(); |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
inline const char* cstringify(const char* str) { |
58 | 58 |
return str; |
59 | 59 |
} |
60 | 60 |
} |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
#endif // LEMON_ASSERT_H |
64 | 64 |
|
65 | 65 |
#undef LEMON_ASSERT |
66 | 66 |
#undef LEMON_FIXME |
67 | 67 |
#undef LEMON_DEBUG |
68 | 68 |
|
69 | 69 |
#if (defined(LEMON_ASSERT_LOG) ? 1 : 0) + \ |
70 | 70 |
(defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
71 | 71 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) > 1 |
72 | 72 |
#error "LEMON assertion system is not set properly" |
73 | 73 |
#endif |
74 | 74 |
|
75 | 75 |
#if ((defined(LEMON_ASSERT_LOG) ? 1 : 0) + \ |
76 | 76 |
(defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
77 | 77 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) == 1 || \ |
78 | 78 |
defined(LEMON_ENABLE_ASSERTS)) && \ |
79 | 79 |
(defined(LEMON_DISABLE_ASSERTS) || \ |
80 | 80 |
defined(NDEBUG)) |
81 | 81 |
#error "LEMON assertion system is not set properly" |
82 | 82 |
#endif |
83 | 83 |
|
84 | 84 |
|
85 | 85 |
#if defined LEMON_ASSERT_LOG |
86 | 86 |
# undef LEMON_ASSERT_HANDLER |
87 | 87 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_log |
88 | 88 |
#elif defined LEMON_ASSERT_ABORT |
89 | 89 |
# undef LEMON_ASSERT_HANDLER |
90 | 90 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
91 | 91 |
#elif defined LEMON_ASSERT_CUSTOM |
92 | 92 |
# undef LEMON_ASSERT_HANDLER |
93 | 93 |
# ifndef LEMON_CUSTOM_ASSERT_HANDLER |
94 | 94 |
# error "LEMON_CUSTOM_ASSERT_HANDLER is not set" |
95 | 95 |
# endif |
96 | 96 |
# define LEMON_ASSERT_HANDLER LEMON_CUSTOM_ASSERT_HANDLER |
97 | 97 |
#elif defined LEMON_DISABLE_ASSERTS |
98 | 98 |
# undef LEMON_ASSERT_HANDLER |
99 | 99 |
#elif defined NDEBUG |
100 | 100 |
# undef LEMON_ASSERT_HANDLER |
101 | 101 |
#else |
102 | 102 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
103 | 103 |
#endif |
104 | 104 |
|
105 | 105 |
#ifndef LEMON_FUNCTION_NAME |
106 | 106 |
# if defined __GNUC__ |
107 | 107 |
# define LEMON_FUNCTION_NAME (__PRETTY_FUNCTION__) |
108 | 108 |
# elif defined _MSC_VER |
109 | 109 |
# define LEMON_FUNCTION_NAME (__FUNCSIG__) |
110 | 110 |
# else |
111 | 111 |
# define LEMON_FUNCTION_NAME (__func__) |
112 | 112 |
# endif |
113 | 113 |
#endif |
114 | 114 |
|
115 | 115 |
#ifdef DOXYGEN |
116 | 116 |
|
117 | 117 |
/// \ingroup exceptions |
118 | 118 |
/// |
119 | 119 |
/// \brief Macro for assertion with customizable message |
120 | 120 |
/// |
121 | 121 |
/// Macro for assertion with customizable message. \param exp An |
122 | 122 |
/// expression that must be convertible to \c bool. If it is \c |
123 | 123 |
/// false, then an assertion is raised. The concrete behaviour depends |
124 | 124 |
/// on the settings of the assertion system. \param msg A <tt>const |
125 | 125 |
/// char*</tt> parameter, which can be used to provide information |
126 | 126 |
/// about the circumstances of the failed assertion. |
127 | 127 |
/// |
128 | 128 |
/// The assertions are enabled in the default behaviour. |
129 | 129 |
/// You can disable them with the following code: |
130 | 130 |
/// \code |
131 | 131 |
/// #define LEMON_DISABLE_ASSERTS |
132 | 132 |
/// \endcode |
133 | 133 |
/// or with compilation parameters: |
134 | 134 |
/// \code |
135 | 135 |
/// g++ -DLEMON_DISABLE_ASSERTS |
136 | 136 |
/// make CXXFLAGS='-DLEMON_DISABLE_ASSERTS' |
137 | 137 |
/// \endcode |
138 | 138 |
/// The checking is also disabled when the standard macro \c NDEBUG is defined. |
139 | 139 |
/// |
140 | 140 |
/// The LEMON assertion system has a wide range of customization |
141 | 141 |
/// properties. As a default behaviour the failed assertion prints a |
142 | 142 |
/// short log message to the standard error and aborts the execution. |
143 | 143 |
/// |
144 | 144 |
/// The following modes can be used in the assertion system: |
145 | 145 |
/// |
146 | 146 |
/// - \c LEMON_ASSERT_LOG The failed assertion prints a short log |
147 | 147 |
/// message to the standard error and continues the execution. |
148 | 148 |
/// - \c LEMON_ASSERT_ABORT This mode is similar to the \c |
149 | 149 |
/// LEMON_ASSERT_LOG, but it aborts the program. It is the default |
150 | 150 |
/// behaviour. |
151 | 151 |
/// - \c LEMON_ASSERT_CUSTOM The user can define own assertion handler |
152 | 152 |
/// function. |
153 | 153 |
/// \code |
154 | 154 |
/// void custom_assert_handler(const char* file, int line, const char* function, |
155 | 155 |
/// const char* message, const char* assertion); |
156 | 156 |
/// \endcode |
157 | 157 |
/// The name of the function should be defined as the \c |
158 | 158 |
/// LEMON_CUSTOM_ASSERT_HANDLER macro name. |
159 | 159 |
/// \code |
160 | 160 |
/// #define LEMON_CUSTOM_ASSERT_HANDLER custom_assert_handler |
161 | 161 |
/// \endcode |
162 | 162 |
/// Whenever an assertion is occured, the custom assertion |
163 | 163 |
/// handler is called with appropiate parameters. |
164 | 164 |
/// |
165 | 165 |
/// The assertion mode can also be changed within one compilation unit. |
166 | 166 |
/// If the macros are redefined with other settings and the |
167 | 167 |
/// \ref lemon/assert.h "assert.h" file is reincluded, then the |
168 | 168 |
/// behaviour is changed appropiately to the new settings. |
169 | 169 |
# define LEMON_ASSERT(exp, msg) \ |
170 | 170 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
171 | 171 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
172 | 172 |
LEMON_FUNCTION_NAME, \ |
173 | 173 |
::lemon::_assert_bits::cstringify(msg), #exp), 0))) |
174 | 174 |
|
175 | 175 |
/// \ingroup exceptions |
176 | 176 |
/// |
177 | 177 |
/// \brief Macro for mark not yet implemented features. |
178 | 178 |
/// |
179 | 179 |
/// Macro for mark not yet implemented features and outstanding bugs. |
180 | 180 |
/// It is close to be the shortcut of the following code: |
181 | 181 |
/// \code |
182 | 182 |
/// LEMON_ASSERT(false, msg); |
183 | 183 |
/// \endcode |
184 | 184 |
/// |
185 | 185 |
/// \see LEMON_ASSERT |
186 | 186 |
# define LEMON_FIXME(msg) \ |
187 | 187 |
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME, \ |
188 | 188 |
::lemon::_assert_bits::cstringify(msg), \ |
189 | 189 |
static_cast<const char*>(0))) |
190 | 190 |
|
191 | 191 |
/// \ingroup exceptions |
192 | 192 |
/// |
193 | 193 |
/// \brief Macro for internal assertions |
194 | 194 |
/// |
195 | 195 |
/// Macro for internal assertions, it is used in the library to check |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/invalid.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 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/graph_utils.h> |
28 | 28 |
#include <lemon/bits/path_dump.h> |
29 | 29 |
#include <lemon/bits/invalid.h> |
30 | 30 |
#include <lemon/error.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
|
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
|
39 | 39 |
///Default traits class of Bfs class. |
40 | 40 |
///\tparam GR Digraph type. |
41 | 41 |
template<class GR> |
42 | 42 |
struct BfsDefaultTraits |
43 | 43 |
{ |
44 | 44 |
///The digraph type the algorithm runs on. |
45 | 45 |
typedef GR Digraph; |
46 | 46 |
///\brief The type of the map that stores the last |
47 | 47 |
///arcs of the shortest paths. |
48 | 48 |
/// |
49 | 49 |
///The type of the map that stores the last |
50 | 50 |
///arcs of the shortest paths. |
51 | 51 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
52 | 52 |
/// |
53 | 53 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
54 | 54 |
///Instantiates a PredMap. |
55 | 55 |
|
56 | 56 |
///This function instantiates a \ref PredMap. |
57 | 57 |
///\param G is the digraph, to which we would like to define the PredMap. |
58 | 58 |
///\todo The digraph alone may be insufficient to initialize |
59 | 59 |
static PredMap *createPredMap(const GR &G) |
60 | 60 |
{ |
61 | 61 |
return new PredMap(G); |
62 | 62 |
} |
63 | 63 |
///The type of the map that indicates which nodes are processed. |
64 | 64 |
|
65 | 65 |
///The type of the map that indicates which nodes are processed. |
66 | 66 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
67 | 67 |
///\todo named parameter to set this type, function to read and write. |
68 | 68 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
69 | 69 |
///Instantiates a ProcessedMap. |
70 | 70 |
|
71 | 71 |
///This function instantiates a \ref ProcessedMap. |
72 | 72 |
///\param g is the digraph, to which |
73 | 73 |
///we would like to define the \ref ProcessedMap |
74 | 74 |
#ifdef DOXYGEN |
75 | 75 |
static ProcessedMap *createProcessedMap(const GR &g) |
76 | 76 |
#else |
77 | 77 |
static ProcessedMap *createProcessedMap(const GR &) |
78 | 78 |
#endif |
79 | 79 |
{ |
80 | 80 |
return new ProcessedMap(); |
81 | 81 |
} |
82 | 82 |
///The type of the map that indicates which nodes are reached. |
83 | 83 |
|
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
86 | 86 |
///\todo named parameter to set this type, function to read and write. |
87 | 87 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
88 | 88 |
///Instantiates a ReachedMap. |
89 | 89 |
|
90 | 90 |
///This function instantiates a \ref ReachedMap. |
91 | 91 |
///\param G is the digraph, to which |
92 | 92 |
///we would like to define the \ref ReachedMap. |
93 | 93 |
static ReachedMap *createReachedMap(const GR &G) |
94 | 94 |
{ |
95 | 95 |
return new ReachedMap(G); |
96 | 96 |
} |
97 | 97 |
///The type of the map that stores the dists of the nodes. |
98 | 98 |
|
99 | 99 |
///The type of the map that stores the dists of the nodes. |
100 | 100 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
101 | 101 |
/// |
102 | 102 |
typedef typename Digraph::template NodeMap<int> DistMap; |
103 | 103 |
///Instantiates a DistMap. |
104 | 104 |
|
105 | 105 |
///This function instantiates a \ref DistMap. |
106 | 106 |
///\param G is the digraph, to which we would like to define the \ref DistMap |
107 | 107 |
static DistMap *createDistMap(const GR &G) |
108 | 108 |
{ |
109 | 109 |
return new DistMap(G); |
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
///%BFS algorithm class. |
114 | 114 |
|
115 | 115 |
///\ingroup search |
116 | 116 |
///This class provides an efficient implementation of the %BFS algorithm. |
117 | 117 |
/// |
118 | 118 |
///\tparam GR The digraph type the algorithm runs on. The default value is |
119 | 119 |
///\ref ListDigraph. The value of GR is not used directly by Bfs, it |
120 | 120 |
///is only passed to \ref BfsDefaultTraits. |
121 | 121 |
///\tparam TR Traits class to set various data types used by the algorithm. |
122 | 122 |
///The default traits class is |
123 | 123 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
124 | 124 |
///See \ref BfsDefaultTraits for the documentation of |
125 | 125 |
///a Bfs traits class. |
126 | 126 |
|
127 | 127 |
#ifdef DOXYGEN |
128 | 128 |
template <typename GR, |
129 | 129 |
typename TR> |
130 | 130 |
#else |
131 | 131 |
template <typename GR=ListDigraph, |
132 | 132 |
typename TR=BfsDefaultTraits<GR> > |
133 | 133 |
#endif |
134 | 134 |
class Bfs { |
135 | 135 |
public: |
136 | 136 |
/** |
137 | 137 |
* \brief \ref Exception for uninitialized parameters. |
138 | 138 |
* |
139 | 139 |
* This error represents problems in the initialization |
140 | 140 |
* of the parameters of the algorithms. |
141 | 141 |
*/ |
142 | 142 |
class UninitializedParameter : public lemon::UninitializedParameter { |
143 | 143 |
public: |
144 | 144 |
virtual const char* what() const throw() { |
145 | 145 |
return "lemon::Bfs::UninitializedParameter"; |
146 | 146 |
} |
147 | 147 |
}; |
148 | 148 |
|
149 | 149 |
typedef TR Traits; |
150 | 150 |
///The type of the underlying digraph. |
151 | 151 |
typedef typename TR::Digraph Digraph; |
152 | 152 |
|
153 | 153 |
///\brief The type of the map that stores the last |
154 | 154 |
///arcs of the shortest paths. |
155 | 155 |
typedef typename TR::PredMap PredMap; |
156 | 156 |
///The type of the map indicating which nodes are reached. |
157 | 157 |
typedef typename TR::ReachedMap ReachedMap; |
158 | 158 |
///The type of the map indicating which nodes are processed. |
159 | 159 |
typedef typename TR::ProcessedMap ProcessedMap; |
160 | 160 |
///The type of the map that stores the dists of the nodes. |
161 | 161 |
typedef typename TR::DistMap DistMap; |
162 | 162 |
private: |
163 | 163 |
|
164 | 164 |
typedef typename Digraph::Node Node; |
165 | 165 |
typedef typename Digraph::NodeIt NodeIt; |
166 | 166 |
typedef typename Digraph::Arc Arc; |
167 | 167 |
typedef typename Digraph::OutArcIt OutArcIt; |
168 | 168 |
|
169 | 169 |
/// Pointer to the underlying digraph. |
170 | 170 |
const Digraph *G; |
171 | 171 |
///Pointer to the map of predecessors arcs. |
172 | 172 |
PredMap *_pred; |
173 | 173 |
///Indicates if \ref _pred is locally allocated (\c true) or not. |
174 | 174 |
bool local_pred; |
175 | 175 |
///Pointer to the map of distances. |
176 | 176 |
DistMap *_dist; |
177 | 177 |
///Indicates if \ref _dist is locally allocated (\c true) or not. |
178 | 178 |
bool local_dist; |
179 | 179 |
///Pointer to the map of reached status of the nodes. |
180 | 180 |
ReachedMap *_reached; |
181 | 181 |
///Indicates if \ref _reached is locally allocated (\c true) or not. |
182 | 182 |
bool local_reached; |
183 | 183 |
///Pointer to the map of processed status of the nodes. |
184 | 184 |
ProcessedMap *_processed; |
185 | 185 |
///Indicates if \ref _processed is locally allocated (\c true) or not. |
186 | 186 |
bool local_processed; |
187 | 187 |
|
188 | 188 |
std::vector<typename Digraph::Node> _queue; |
189 | 189 |
int _queue_head,_queue_tail,_queue_next_dist; |
190 | 190 |
int _curr_dist; |
191 | 191 |
|
192 | 192 |
///Creates the maps if necessary. |
193 | 193 |
|
194 | 194 |
///\todo Better memory allocation (instead of new). |
195 | 195 |
void create_maps() |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 { |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/utility.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 graph's can be refered as two containers, one node container |
39 | 39 |
/// and one edge container. But they are not standard containers they |
40 | 40 |
/// does not store values directly they are just key continars for more |
41 | 41 |
/// value containers which are the node and edge maps. |
42 | 42 |
/// |
43 | 43 |
/// The graph's node and edge sets 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 the container. The \e first() and \e |
53 | 53 |
/// next() member functions make possible to iterate on the keys of the |
54 | 54 |
/// container. The \e id() function returns an integer id for each key. |
55 | 55 |
/// The \e maxId() function gives back an upper bound of the ids. |
56 | 56 |
/// |
57 | 57 |
/// For the proper functonality of this class, we should notify it |
58 | 58 |
/// about each alteration in the container. The alterations have four type |
59 | 59 |
/// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and |
60 | 60 |
/// \e erase() signals that only one or few items added or erased to or |
61 | 61 |
/// from the graph. If all items are erased from the graph or from an empty |
62 | 62 |
/// graph a new graph is builded then it can be signaled with the |
63 | 63 |
/// clear() and build() members. Important rule that if we erase items |
64 | 64 |
/// from graph we should first signal the alteration and after that erase |
65 | 65 |
/// them from the container, on the other way on item addition we should |
66 | 66 |
/// first extend the container and just after that signal the alteration. |
67 | 67 |
/// |
68 | 68 |
/// The alteration can be observed with a class inherited from the |
69 | 69 |
/// \e ObserverBase nested class. The signals can be handled with |
70 | 70 |
/// overriding the virtual functions defined in the base class. The |
71 | 71 |
/// observer base can be attached to the notifier with the |
72 | 72 |
/// \e attach() member and can be detached with detach() function. The |
73 | 73 |
/// alteration handlers should not call any function which signals |
74 | 74 |
/// an other alteration in the same notifier and should not |
75 | 75 |
/// detach any observer from the notifier. |
76 | 76 |
/// |
77 | 77 |
/// Alteration observers try to be exception safe. If an \e add() or |
78 | 78 |
/// a \e clear() function throws an exception then the remaining |
79 | 79 |
/// observeres will not be notified and the fulfilled additions will |
80 | 80 |
/// be rolled back by calling the \e erase() or \e clear() |
81 | 81 |
/// functions. Thence the \e erase() and \e clear() should not throw |
82 | 82 |
/// exception. Actullay, it can be throw only |
83 | 83 |
/// \ref AlterationObserver::ImmediateDetach ImmediateDetach |
84 | 84 |
/// exception which detach the observer from the notifier. |
85 | 85 |
/// |
86 | 86 |
/// There are some place 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 reverseEdge that 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. The sub graph adaptors cannot signal the alterations because |
92 | 92 |
/// just a setting in the filter map can modify the graph and this cannot |
93 | 93 |
/// 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 \e clear() and |
108 | 108 |
/// \e erase(). |
109 | 109 |
/// |
110 | 110 |
/// From the \e clear() and \e 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 |
/// \e clear() and \e 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 |
126 | 126 |
/// erased. |
127 | 127 |
/// |
128 | 128 |
/// The build() and clear() members are to notify the observer |
129 | 129 |
/// about the container is built from an empty container or |
130 | 130 |
/// is cleared to an empty container. |
131 | 131 |
|
132 | 132 |
class ObserverBase { |
133 | 133 |
protected: |
134 | 134 |
typedef AlterationNotifier Notifier; |
135 | 135 |
|
136 | 136 |
friend class AlterationNotifier; |
137 | 137 |
|
138 | 138 |
/// \brief Default constructor. |
139 | 139 |
/// |
140 | 140 |
/// Default constructor for ObserverBase. |
141 | 141 |
/// |
142 | 142 |
ObserverBase() : _notifier(0) {} |
143 | 143 |
|
144 | 144 |
/// \brief Constructor which attach the observer into notifier. |
145 | 145 |
/// |
146 | 146 |
/// Constructor which attach the observer into notifier. |
147 | 147 |
ObserverBase(AlterationNotifier& nf) { |
148 | 148 |
attach(nf); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
/// \brief Constructor which attach the obserever to the same notifier. |
152 | 152 |
/// |
153 | 153 |
/// Constructor which attach the obserever to the same notifier as |
154 | 154 |
/// the other observer is attached to. |
155 | 155 |
ObserverBase(const ObserverBase& copy) { |
156 | 156 |
if (copy.attached()) { |
157 | 157 |
attach(*copy.notifier()); |
158 | 158 |
} |
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
/// \brief Destructor |
162 | 162 |
virtual ~ObserverBase() { |
163 | 163 |
if (attached()) { |
164 | 164 |
detach(); |
165 | 165 |
} |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
/// \brief Attaches the observer into an AlterationNotifier. |
169 | 169 |
/// |
170 | 170 |
/// This member attaches the observer into an AlterationNotifier. |
171 | 171 |
/// |
172 | 172 |
void attach(AlterationNotifier& nf) { |
173 | 173 |
nf.attach(*this); |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
/// \brief Detaches the observer into an AlterationNotifier. |
177 | 177 |
/// |
178 | 178 |
/// This member detaches the observer from an AlterationNotifier. |
179 | 179 |
/// |
180 | 180 |
void detach() { |
181 | 181 |
_notifier->detach(*this); |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
/// \brief Gives back a pointer to the notifier which the map |
185 | 185 |
/// attached into. |
186 | 186 |
/// |
187 | 187 |
/// This function gives back a pointer to the notifier which the map |
188 | 188 |
/// attached into. |
189 | 189 |
/// |
190 | 190 |
Notifier* notifier() const { return const_cast<Notifier*>(_notifier); } |
191 | 191 |
|
192 | 192 |
/// Gives back true when the observer is attached into a notifier. |
193 | 193 |
bool attached() const { return _notifier != 0; } |
194 | 194 |
|
195 | 195 |
private: |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 what |
40 | 40 |
/// automatically updates the map when a key is added to or erased from |
41 | 41 |
/// the map. This map uses the allocators to implement |
42 | 42 |
/// the container functionality. |
43 | 43 |
/// |
44 | 44 |
/// The template parameters are the Graph the current Item type and |
45 | 45 |
/// the Value type of the map. |
46 | 46 |
template <typename _Graph, typename _Item, typename _Value> |
47 | 47 |
class ArrayMap |
48 | 48 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
49 | 49 |
public: |
50 | 50 |
/// The graph type of the maps. |
51 | 51 |
typedef _Graph Graph; |
52 | 52 |
/// The item type of the map. |
53 | 53 |
typedef _Item Item; |
54 | 54 |
/// The reference map tag. |
55 | 55 |
typedef True ReferenceMapTag; |
56 | 56 |
|
57 | 57 |
/// The key type of the maps. |
58 | 58 |
typedef _Item Key; |
59 | 59 |
/// The value type of the map. |
60 | 60 |
typedef _Value Value; |
61 | 61 |
|
62 | 62 |
/// The const reference type of the map. |
63 | 63 |
typedef const _Value& ConstReference; |
64 | 64 |
/// The reference type of the map. |
65 | 65 |
typedef _Value& Reference; |
66 | 66 |
|
67 | 67 |
/// The notifier type. |
68 | 68 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
69 | 69 |
|
70 | 70 |
/// The MapBase of the Map which imlements the core regisitry function. |
71 | 71 |
typedef typename Notifier::ObserverBase Parent; |
72 | 72 |
|
73 | 73 |
private: |
74 | 74 |
typedef std::allocator<Value> Allocator; |
75 | 75 |
|
76 | 76 |
public: |
77 | 77 |
|
78 | 78 |
/// \brief Graph initialized map constructor. |
79 | 79 |
/// |
80 | 80 |
/// Graph initialized map constructor. |
81 | 81 |
explicit ArrayMap(const Graph& graph) { |
82 | 82 |
Parent::attach(graph.notifier(Item())); |
83 | 83 |
allocate_memory(); |
84 | 84 |
Notifier* nf = Parent::notifier(); |
85 | 85 |
Item it; |
86 | 86 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
87 | 87 |
int id = nf->id(it);; |
88 | 88 |
allocator.construct(&(values[id]), Value()); |
89 | 89 |
} |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
/// \brief Constructor to use default value to initialize the map. |
93 | 93 |
/// |
94 | 94 |
/// It constructs a map and initialize all of the the map. |
95 | 95 |
ArrayMap(const Graph& graph, const Value& value) { |
96 | 96 |
Parent::attach(graph.notifier(Item())); |
97 | 97 |
allocate_memory(); |
98 | 98 |
Notifier* nf = Parent::notifier(); |
99 | 99 |
Item it; |
100 | 100 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
101 | 101 |
int id = nf->id(it);; |
102 | 102 |
allocator.construct(&(values[id]), value); |
103 | 103 |
} |
104 | 104 |
} |
105 | 105 |
|
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 |
/// \brief The destructor of the map. |
154 | 154 |
/// |
155 | 155 |
/// The destructor of the map. |
156 | 156 |
virtual ~ArrayMap() { |
157 | 157 |
if (attached()) { |
158 | 158 |
clear(); |
159 | 159 |
detach(); |
160 | 160 |
} |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
protected: |
164 | 164 |
|
165 | 165 |
using Parent::attach; |
166 | 166 |
using Parent::detach; |
167 | 167 |
using Parent::attached; |
168 | 168 |
|
169 | 169 |
public: |
170 | 170 |
|
171 | 171 |
/// \brief The subscript operator. |
172 | 172 |
/// |
173 | 173 |
/// The subscript operator. The map can be subscripted by the |
174 | 174 |
/// actual keys of the graph. |
175 | 175 |
Value& operator[](const Key& key) { |
176 | 176 |
int id = Parent::notifier()->id(key); |
177 | 177 |
return values[id]; |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
/// \brief The const subscript operator. |
181 | 181 |
/// |
182 | 182 |
/// The const subscript operator. The map can be subscripted by the |
183 | 183 |
/// actual keys of the graph. |
184 | 184 |
const Value& operator[](const Key& key) const { |
185 | 185 |
int id = Parent::notifier()->id(key); |
186 | 186 |
return values[id]; |
187 | 187 |
} |
188 | 188 |
|
189 | 189 |
/// \brief Setter function of the map. |
190 | 190 |
/// |
191 | 191 |
/// Setter function of the map. Equivalent with map[key] = val. |
192 | 192 |
/// This is a compatibility feature with the not dereferable maps. |
193 | 193 |
void set(const Key& key, const Value& val) { |
194 | 194 |
(*this)[key] = val; |
195 | 195 |
} |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/invalid.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 digraph 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 |
|
78 | 78 |
|
79 | 79 |
using Parent::source; |
80 | 80 |
|
81 | 81 |
/// Source of the given Arc. |
82 | 82 |
Node source(const Arc &e) const { |
83 | 83 |
return e.forward ? Parent::source(e) : Parent::target(e); |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
using Parent::target; |
87 | 87 |
|
88 | 88 |
/// Target of the given Arc. |
89 | 89 |
Node target(const Arc &e) const { |
90 | 90 |
return e.forward ? Parent::target(e) : Parent::source(e); |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
/// \brief Directed arc from an edge. |
94 | 94 |
/// |
95 | 95 |
/// Returns a directed arc corresponding to the specified Edge. |
96 | 96 |
/// If the given bool is true the given edge and the |
97 | 97 |
/// returned arc have the same source node. |
98 | 98 |
static Arc direct(const Edge &ue, bool d) { |
99 | 99 |
return Arc(ue, d); |
100 | 100 |
} |
101 | 101 |
|
102 | 102 |
/// Returns whether the given directed arc is same orientation as the |
103 | 103 |
/// corresponding edge. |
104 | 104 |
/// |
105 | 105 |
/// \todo reference to the corresponding point of the undirected digraph |
106 | 106 |
/// concept. "What does the direction of an edge mean?" |
107 | 107 |
static bool direction(const Arc &e) { return e.forward; } |
108 | 108 |
|
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 |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
|
23 | 23 |
#include <lemon/bits/array_map.h> |
24 | 24 |
#include <lemon/bits/vector_map.h> |
25 | 25 |
//#include <lemon/bits/debug_map.h> |
26 | 26 |
|
27 | 27 |
///\ingroup graphbits |
28 | 28 |
///\file |
29 | 29 |
///\brief Graph maps that construct and destruct their elements dynamically. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
|
34 | 34 |
//#ifndef LEMON_USE_DEBUG_MAP |
35 | 35 |
|
36 | 36 |
template <typename _Graph, typename _Item, typename _Value> |
37 | 37 |
struct DefaultMapSelector { |
38 | 38 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
39 | 39 |
}; |
40 | 40 |
|
41 | 41 |
// bool |
42 | 42 |
template <typename _Graph, typename _Item> |
43 | 43 |
struct DefaultMapSelector<_Graph, _Item, bool> { |
44 | 44 |
typedef VectorMap<_Graph, _Item, bool> Map; |
45 | 45 |
}; |
46 | 46 |
|
47 | 47 |
// char |
48 | 48 |
template <typename _Graph, typename _Item> |
49 | 49 |
struct DefaultMapSelector<_Graph, _Item, char> { |
50 | 50 |
typedef VectorMap<_Graph, _Item, char> Map; |
51 | 51 |
}; |
52 | 52 |
|
53 | 53 |
template <typename _Graph, typename _Item> |
54 | 54 |
struct DefaultMapSelector<_Graph, _Item, signed char> { |
55 | 55 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
56 | 56 |
}; |
57 | 57 |
|
58 | 58 |
template <typename _Graph, typename _Item> |
59 | 59 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> { |
60 | 60 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
61 | 61 |
}; |
62 | 62 |
|
63 | 63 |
|
64 | 64 |
// int |
65 | 65 |
template <typename _Graph, typename _Item> |
66 | 66 |
struct DefaultMapSelector<_Graph, _Item, signed int> { |
67 | 67 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
68 | 68 |
}; |
69 | 69 |
|
70 | 70 |
template <typename _Graph, typename _Item> |
71 | 71 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> { |
72 | 72 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
// short |
77 | 77 |
template <typename _Graph, typename _Item> |
78 | 78 |
struct DefaultMapSelector<_Graph, _Item, signed short> { |
79 | 79 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
80 | 80 |
}; |
81 | 81 |
|
82 | 82 |
template <typename _Graph, typename _Item> |
83 | 83 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> { |
84 | 84 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
85 | 85 |
}; |
86 | 86 |
|
87 | 87 |
|
88 | 88 |
// long |
89 | 89 |
template <typename _Graph, typename _Item> |
90 | 90 |
struct DefaultMapSelector<_Graph, _Item, signed long> { |
91 | 91 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
92 | 92 |
}; |
93 | 93 |
|
94 | 94 |
template <typename _Graph, typename _Item> |
95 | 95 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> { |
96 | 96 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
97 | 97 |
}; |
98 | 98 |
|
99 | 99 |
|
100 | 100 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
101 | 101 |
|
102 | 102 |
// long long |
103 | 103 |
template <typename _Graph, typename _Item> |
104 | 104 |
struct DefaultMapSelector<_Graph, _Item, signed long long> { |
105 | 105 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename _Graph, typename _Item> |
109 | 109 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> { |
110 | 110 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
#endif |
114 | 114 |
|
115 | 115 |
|
116 | 116 |
// float |
117 | 117 |
template <typename _Graph, typename _Item> |
118 | 118 |
struct DefaultMapSelector<_Graph, _Item, float> { |
119 | 119 |
typedef VectorMap<_Graph, _Item, float> Map; |
120 | 120 |
}; |
121 | 121 |
|
122 | 122 |
|
123 | 123 |
// double |
124 | 124 |
template <typename _Graph, typename _Item> |
125 | 125 |
struct DefaultMapSelector<_Graph, _Item, double> { |
126 | 126 |
typedef VectorMap<_Graph, _Item, double> Map; |
127 | 127 |
}; |
128 | 128 |
|
129 | 129 |
|
130 | 130 |
// long double |
131 | 131 |
template <typename _Graph, typename _Item> |
132 | 132 |
struct DefaultMapSelector<_Graph, _Item, long double> { |
133 | 133 |
typedef VectorMap<_Graph, _Item, long double> Map; |
134 | 134 |
}; |
135 | 135 |
|
136 | 136 |
|
137 | 137 |
// pointer |
138 | 138 |
template <typename _Graph, typename _Item, typename _Ptr> |
139 | 139 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> { |
140 | 140 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
141 | 141 |
}; |
142 | 142 |
|
143 | 143 |
// #else |
144 | 144 |
|
145 | 145 |
// template <typename _Graph, typename _Item, typename _Value> |
146 | 146 |
// struct DefaultMapSelector { |
147 | 147 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
148 | 148 |
// }; |
149 | 149 |
|
150 | 150 |
// #endif |
151 | 151 |
|
152 | 152 |
/// \e |
153 | 153 |
template <typename _Graph, typename _Item, typename _Value> |
154 | 154 |
class DefaultMap |
155 | 155 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map { |
156 | 156 |
public: |
157 | 157 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
158 | 158 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
159 | 159 |
|
160 | 160 |
typedef typename Parent::Graph Graph; |
161 | 161 |
typedef typename Parent::Value Value; |
162 | 162 |
|
163 | 163 |
explicit DefaultMap(const Graph& graph) : Parent(graph) {} |
164 | 164 |
DefaultMap(const Graph& graph, const Value& value) |
165 | 165 |
: Parent(graph, value) {} |
166 | 166 |
|
167 | 167 |
DefaultMap& operator=(const DefaultMap& cmap) { |
168 | 168 |
return operator=<DefaultMap>(cmap); |
169 | 169 |
} |
170 | 170 |
|
171 | 171 |
template <typename CMap> |
172 | 172 |
DefaultMap& operator=(const CMap& cmap) { |
173 | 173 |
Parent::operator=(cmap); |
174 | 174 |
return *this; |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
}; |
178 | 178 |
|
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
#endif |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/invalid.h> |
23 | 23 |
#include <lemon/bits/utility.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 graphbits |
32 | 32 |
///\file |
33 | 33 |
///\brief Extenders for the digraph types |
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \ingroup graphbits |
37 | 37 |
/// |
38 | 38 |
/// \brief Extender for the Digraphs |
39 | 39 |
template <typename Base> |
40 | 40 |
class DigraphExtender : public Base { |
41 | 41 |
public: |
42 | 42 |
|
43 | 43 |
typedef Base Parent; |
44 | 44 |
typedef DigraphExtender Digraph; |
45 | 45 |
|
46 | 46 |
// Base extensions |
47 | 47 |
|
48 | 48 |
typedef typename Parent::Node Node; |
49 | 49 |
typedef typename Parent::Arc Arc; |
50 | 50 |
|
51 | 51 |
int maxId(Node) const { |
52 | 52 |
return Parent::maxNodeId(); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
int maxId(Arc) const { |
56 | 56 |
return Parent::maxArcId(); |
57 | 57 |
} |
58 | 58 |
|
59 | 59 |
Node fromId(int id, Node) const { |
60 | 60 |
return Parent::nodeFromId(id); |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
Arc fromId(int id, Arc) const { |
64 | 64 |
return Parent::arcFromId(id); |
65 | 65 |
} |
66 | 66 |
|
67 | 67 |
Node oppositeNode(const Node &node, const Arc &arc) const { |
68 | 68 |
if (node == Parent::source(arc)) |
69 | 69 |
return Parent::target(arc); |
70 | 70 |
else if(node == Parent::target(arc)) |
71 | 71 |
return Parent::source(arc); |
72 | 72 |
else |
73 | 73 |
return INVALID; |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
// Alterable extension |
77 | 77 |
|
78 | 78 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
79 | 79 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
80 | 80 |
|
81 | 81 |
|
82 | 82 |
protected: |
83 | 83 |
|
84 | 84 |
mutable NodeNotifier node_notifier; |
85 | 85 |
mutable ArcNotifier arc_notifier; |
86 | 86 |
|
87 | 87 |
public: |
88 | 88 |
|
89 | 89 |
NodeNotifier& notifier(Node) const { |
90 | 90 |
return node_notifier; |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
ArcNotifier& notifier(Arc) const { |
94 | 94 |
return arc_notifier; |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
class NodeIt : public Node { |
98 | 98 |
const Digraph* _digraph; |
99 | 99 |
public: |
100 | 100 |
|
101 | 101 |
NodeIt() {} |
102 | 102 |
|
103 | 103 |
NodeIt(Invalid i) : Node(i) { } |
104 | 104 |
|
105 | 105 |
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) { |
106 | 106 |
_digraph->first(static_cast<Node&>(*this)); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
NodeIt(const Digraph& digraph, const Node& node) |
110 | 110 |
: Node(node), _digraph(&digraph) {} |
111 | 111 |
|
112 | 112 |
NodeIt& operator++() { |
113 | 113 |
_digraph->next(*this); |
114 | 114 |
return *this; |
115 | 115 |
} |
116 | 116 |
|
117 | 117 |
}; |
118 | 118 |
|
119 | 119 |
|
120 | 120 |
class ArcIt : public Arc { |
121 | 121 |
const Digraph* _digraph; |
122 | 122 |
public: |
123 | 123 |
|
124 | 124 |
ArcIt() { } |
125 | 125 |
|
126 | 126 |
ArcIt(Invalid i) : Arc(i) { } |
127 | 127 |
|
128 | 128 |
explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) { |
129 | 129 |
_digraph->first(static_cast<Arc&>(*this)); |
130 | 130 |
} |
131 | 131 |
|
132 | 132 |
ArcIt(const Digraph& digraph, const Arc& arc) : |
133 | 133 |
Arc(arc), _digraph(&digraph) { } |
134 | 134 |
|
135 | 135 |
ArcIt& operator++() { |
136 | 136 |
_digraph->next(*this); |
137 | 137 |
return *this; |
138 | 138 |
} |
139 | 139 |
|
140 | 140 |
}; |
141 | 141 |
|
142 | 142 |
|
143 | 143 |
class OutArcIt : public Arc { |
144 | 144 |
const Digraph* _digraph; |
145 | 145 |
public: |
146 | 146 |
|
147 | 147 |
OutArcIt() { } |
148 | 148 |
|
149 | 149 |
OutArcIt(Invalid i) : Arc(i) { } |
150 | 150 |
|
151 | 151 |
OutArcIt(const Digraph& digraph, const Node& node) |
152 | 152 |
: _digraph(&digraph) { |
153 | 153 |
_digraph->firstOut(*this, node); |
154 | 154 |
} |
155 | 155 |
|
156 | 156 |
OutArcIt(const Digraph& digraph, const Arc& arc) |
157 | 157 |
: Arc(arc), _digraph(&digraph) {} |
158 | 158 |
|
159 | 159 |
OutArcIt& operator++() { |
160 | 160 |
_digraph->nextOut(*this); |
161 | 161 |
return *this; |
162 | 162 |
} |
163 | 163 |
|
164 | 164 |
}; |
165 | 165 |
|
166 | 166 |
|
167 | 167 |
class InArcIt : public Arc { |
168 | 168 |
const Digraph* _digraph; |
169 | 169 |
public: |
170 | 170 |
|
171 | 171 |
InArcIt() { } |
172 | 172 |
|
173 | 173 |
InArcIt(Invalid i) : Arc(i) { } |
174 | 174 |
|
175 | 175 |
InArcIt(const Digraph& digraph, const Node& node) |
176 | 176 |
: _digraph(&digraph) { |
177 | 177 |
_digraph->firstIn(*this, node); |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
InArcIt(const Digraph& digraph, const Arc& arc) : |
181 | 181 |
Arc(arc), _digraph(&digraph) {} |
182 | 182 |
|
183 | 183 |
InArcIt& operator++() { |
184 | 184 |
_digraph->nextIn(*this); |
185 | 185 |
return *this; |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
}; |
189 | 189 |
|
190 | 190 |
/// \brief Base node of the iterator |
191 | 191 |
/// |
192 | 192 |
/// Returns the base node (i.e. the source in this case) of the iterator |
193 | 193 |
Node baseNode(const OutArcIt &arc) const { |
194 | 194 |
return Parent::source(arc); |
195 | 195 |
} |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_INVALID_H |
20 | 20 |
#define LEMON_BITS_INVALID_H |
21 | 21 |
|
22 | 22 |
///\file |
23 | 23 |
///\brief Definition of INVALID. |
24 | 24 |
|
25 | 25 |
namespace lemon { |
26 | 26 |
|
27 | 27 |
/// \brief Dummy type to make it easier to create invalid iterators. |
28 | 28 |
/// |
29 | 29 |
/// Dummy type to make it easier to create invalid iterators. |
30 | 30 |
/// See \ref INVALID for the usage. |
31 | 31 |
struct Invalid { |
32 | 32 |
public: |
33 | 33 |
bool operator==(Invalid) { return true; } |
34 | 34 |
bool operator!=(Invalid) { return false; } |
35 | 35 |
bool operator< (Invalid) { return false; } |
36 | 36 |
}; |
37 | 37 |
|
38 | 38 |
/// \brief Invalid iterators. |
39 | 39 |
/// |
40 | 40 |
/// \ref Invalid is a global type that converts to each iterator |
41 | 41 |
/// in such a way that the value of the target iterator will be invalid. |
42 | 42 |
|
43 | 43 |
//Some people didn't like this: |
44 | 44 |
//const Invalid &INVALID = *(Invalid *)0; |
45 | 45 |
|
46 | 46 |
#ifdef LEMON_ONLY_TEMPLATES |
47 | 47 |
const Invalid INVALID = Invalid(); |
48 | 48 |
#else |
49 | 49 |
extern const Invalid INVALID; |
50 | 50 |
#endif |
51 | 51 |
|
52 | 52 |
} //namespace lemon |
53 | 53 |
|
54 | 54 |
#endif |
55 | 55 |
|
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
MapExtender& operator=(const MapExtender& cmap) { |
66 | 66 |
return operator=<MapExtender>(cmap); |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
template <typename CMap> |
70 | 70 |
MapExtender& operator=(const CMap& cmap) { |
71 | 71 |
Parent::operator=(cmap); |
72 | 72 |
return *this; |
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
class MapIt : public Item { |
76 | 76 |
public: |
77 | 77 |
|
78 | 78 |
typedef Item Parent; |
79 | 79 |
typedef typename Map::Value Value; |
80 | 80 |
|
81 | 81 |
MapIt() {} |
82 | 82 |
|
83 | 83 |
MapIt(Invalid i) : Parent(i) { } |
84 | 84 |
|
85 | 85 |
explicit MapIt(Map& _map) : map(_map) { |
86 | 86 |
map.notifier()->first(*this); |
87 | 87 |
} |
88 | 88 |
|
89 | 89 |
MapIt(const Map& _map, const Item& item) |
90 | 90 |
: Parent(item), map(_map) {} |
91 | 91 |
|
92 | 92 |
MapIt& operator++() { |
93 | 93 |
map.notifier()->next(*this); |
94 | 94 |
return *this; |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
98 | 98 |
return map[*this]; |
99 | 99 |
} |
100 | 100 |
|
101 | 101 |
typename MapTraits<Map>::ReturnValue operator*() { |
102 | 102 |
return map[*this]; |
103 | 103 |
} |
104 | 104 |
|
105 | 105 |
void set(const Value& value) { |
106 | 106 |
map.set(*this, value); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
protected: |
110 | 110 |
Map& map; |
111 | 111 |
|
112 | 112 |
}; |
113 | 113 |
|
114 | 114 |
class ConstMapIt : public Item { |
115 | 115 |
public: |
116 | 116 |
|
117 | 117 |
typedef Item Parent; |
118 | 118 |
|
119 | 119 |
typedef typename Map::Value Value; |
120 | 120 |
|
121 | 121 |
ConstMapIt() {} |
122 | 122 |
|
123 | 123 |
ConstMapIt(Invalid i) : Parent(i) { } |
124 | 124 |
|
125 | 125 |
explicit ConstMapIt(Map& _map) : map(_map) { |
126 | 126 |
map.notifier()->first(*this); |
127 | 127 |
} |
128 | 128 |
|
129 | 129 |
ConstMapIt(const Map& _map, const Item& item) |
130 | 130 |
: Parent(item), map(_map) {} |
131 | 131 |
|
132 | 132 |
ConstMapIt& operator++() { |
133 | 133 |
map.notifier()->next(*this); |
134 | 134 |
return *this; |
135 | 135 |
} |
136 | 136 |
|
137 | 137 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
138 | 138 |
return map[*this]; |
139 | 139 |
} |
140 | 140 |
|
141 | 141 |
protected: |
142 | 142 |
const Map& map; |
143 | 143 |
}; |
144 | 144 |
|
145 | 145 |
class ItemIt : public Item { |
146 | 146 |
public: |
147 | 147 |
|
148 | 148 |
typedef Item Parent; |
149 | 149 |
|
150 | 150 |
ItemIt() {} |
151 | 151 |
|
152 | 152 |
ItemIt(Invalid i) : Parent(i) { } |
153 | 153 |
|
154 | 154 |
explicit ItemIt(Map& _map) : map(_map) { |
155 | 155 |
map.notifier()->first(*this); |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
ItemIt(const Map& _map, const Item& item) |
159 | 159 |
: Parent(item), map(_map) {} |
160 | 160 |
|
161 | 161 |
ItemIt& operator++() { |
162 | 162 |
map.notifier()->next(*this); |
163 | 163 |
return *this; |
164 | 164 |
} |
165 | 165 |
|
166 | 166 |
protected: |
167 | 167 |
const Map& map; |
168 | 168 |
|
169 | 169 |
}; |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
/// \ingroup graphbits |
173 | 173 |
/// |
174 | 174 |
/// \brief Extender for maps which use a subset of the items. |
175 | 175 |
template <typename _Graph, typename _Map> |
176 | 176 |
class SubMapExtender : public _Map { |
177 | 177 |
public: |
178 | 178 |
|
179 | 179 |
typedef _Map Parent; |
180 | 180 |
typedef SubMapExtender Map; |
181 | 181 |
|
182 | 182 |
typedef _Graph Graph; |
183 | 183 |
|
184 | 184 |
typedef typename Parent::Key Item; |
185 | 185 |
|
186 | 186 |
typedef typename Parent::Key Key; |
187 | 187 |
typedef typename Parent::Value Value; |
188 | 188 |
|
189 | 189 |
class MapIt; |
190 | 190 |
class ConstMapIt; |
191 | 191 |
|
192 | 192 |
friend class MapIt; |
193 | 193 |
friend class ConstMapIt; |
194 | 194 |
|
195 | 195 |
public: |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
|
|
2 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
3 | 2 |
* |
4 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
5 | 4 |
* |
6 | 5 |
* Copyright (C) 2003-2008 |
7 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
8 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
9 | 8 |
* |
10 | 9 |
* Permission to use, modify and distribute this software is granted |
11 | 10 |
* provided that this copyright notice appears in all copies. For |
12 | 11 |
* precise terms see the accompanying LICENSE file. |
13 | 12 |
* |
14 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
15 | 14 |
* express or implied, and with no claim as to its suitability for any |
16 | 15 |
* purpose. |
17 | 16 |
* |
18 | 17 |
*/ |
19 | 18 |
|
20 | 19 |
#ifndef LEMON_BITS_TRAITS_H |
21 | 20 |
#define LEMON_BITS_TRAITS_H |
22 | 21 |
|
23 | 22 |
#include <lemon/bits/utility.h> |
24 | 23 |
|
25 | 24 |
///\file |
26 | 25 |
///\brief Traits for graphs and maps |
27 | 26 |
/// |
28 | 27 |
|
29 | 28 |
namespace lemon { |
30 | 29 |
template <typename _Graph, typename _Item> |
31 | 30 |
class ItemSetTraits {}; |
32 | 31 |
|
33 | 32 |
|
34 | 33 |
template <typename Graph, typename Enable = void> |
35 | 34 |
struct NodeNotifierIndicator { |
36 | 35 |
typedef InvalidType Type; |
37 | 36 |
}; |
38 | 37 |
template <typename Graph> |
39 | 38 |
struct NodeNotifierIndicator< |
40 | 39 |
Graph, |
41 | 40 |
typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type |
42 | 41 |
> { |
43 | 42 |
typedef typename Graph::NodeNotifier Type; |
44 | 43 |
}; |
45 | 44 |
|
46 | 45 |
template <typename _Graph> |
47 | 46 |
class ItemSetTraits<_Graph, typename _Graph::Node> { |
48 | 47 |
public: |
49 | 48 |
|
50 | 49 |
typedef _Graph Graph; |
51 | 50 |
|
52 | 51 |
typedef typename Graph::Node Item; |
53 | 52 |
typedef typename Graph::NodeIt ItemIt; |
54 | 53 |
|
55 | 54 |
typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier; |
56 | 55 |
|
57 | 56 |
template <typename _Value> |
58 | 57 |
class Map : public Graph::template NodeMap<_Value> { |
59 | 58 |
public: |
60 | 59 |
typedef typename Graph::template NodeMap<_Value> Parent; |
61 | 60 |
typedef typename Graph::template NodeMap<_Value> Type; |
62 | 61 |
typedef typename Parent::Value Value; |
63 | 62 |
|
64 | 63 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
65 | 64 |
Map(const Graph& _digraph, const Value& _value) |
66 | 65 |
: Parent(_digraph, _value) {} |
67 | 66 |
|
68 | 67 |
}; |
69 | 68 |
|
70 | 69 |
}; |
71 | 70 |
|
72 | 71 |
template <typename Graph, typename Enable = void> |
73 | 72 |
struct ArcNotifierIndicator { |
74 | 73 |
typedef InvalidType Type; |
75 | 74 |
}; |
76 | 75 |
template <typename Graph> |
77 | 76 |
struct ArcNotifierIndicator< |
78 | 77 |
Graph, |
79 | 78 |
typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type |
80 | 79 |
> { |
81 | 80 |
typedef typename Graph::ArcNotifier Type; |
82 | 81 |
}; |
83 | 82 |
|
84 | 83 |
template <typename _Graph> |
85 | 84 |
class ItemSetTraits<_Graph, typename _Graph::Arc> { |
86 | 85 |
public: |
87 | 86 |
|
88 | 87 |
typedef _Graph Graph; |
89 | 88 |
|
90 | 89 |
typedef typename Graph::Arc Item; |
91 | 90 |
typedef typename Graph::ArcIt ItemIt; |
92 | 91 |
|
93 | 92 |
typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier; |
94 | 93 |
|
95 | 94 |
template <typename _Value> |
96 | 95 |
class Map : public Graph::template ArcMap<_Value> { |
97 | 96 |
public: |
98 | 97 |
typedef typename Graph::template ArcMap<_Value> Parent; |
99 | 98 |
typedef typename Graph::template ArcMap<_Value> Type; |
100 | 99 |
typedef typename Parent::Value Value; |
101 | 100 |
|
102 | 101 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
103 | 102 |
Map(const Graph& _digraph, const Value& _value) |
104 | 103 |
: Parent(_digraph, _value) {} |
105 | 104 |
}; |
106 | 105 |
|
107 | 106 |
}; |
108 | 107 |
|
109 | 108 |
template <typename Graph, typename Enable = void> |
110 | 109 |
struct EdgeNotifierIndicator { |
111 | 110 |
typedef InvalidType Type; |
112 | 111 |
}; |
113 | 112 |
template <typename Graph> |
114 | 113 |
struct EdgeNotifierIndicator< |
115 | 114 |
Graph, |
116 | 115 |
typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type |
117 | 116 |
> { |
118 | 117 |
typedef typename Graph::EdgeNotifier Type; |
119 | 118 |
}; |
120 | 119 |
|
121 | 120 |
template <typename _Graph> |
122 | 121 |
class ItemSetTraits<_Graph, typename _Graph::Edge> { |
123 | 122 |
public: |
124 | 123 |
|
125 | 124 |
typedef _Graph Graph; |
126 | 125 |
|
127 | 126 |
typedef typename Graph::Edge Item; |
128 | 127 |
typedef typename Graph::EdgeIt ItemIt; |
129 | 128 |
|
130 | 129 |
typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier; |
131 | 130 |
|
132 | 131 |
template <typename _Value> |
133 | 132 |
class Map : public Graph::template EdgeMap<_Value> { |
134 | 133 |
public: |
135 | 134 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
136 | 135 |
typedef typename Graph::template EdgeMap<_Value> Type; |
137 | 136 |
typedef typename Parent::Value Value; |
138 | 137 |
|
139 | 138 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
140 | 139 |
Map(const Graph& _digraph, const Value& _value) |
141 | 140 |
: Parent(_digraph, _value) {} |
142 | 141 |
}; |
143 | 142 |
|
144 | 143 |
}; |
145 | 144 |
|
146 | 145 |
template <typename Map, typename Enable = void> |
147 | 146 |
struct MapTraits { |
148 | 147 |
typedef False ReferenceMapTag; |
149 | 148 |
|
150 | 149 |
typedef typename Map::Key Key; |
151 | 150 |
typedef typename Map::Value Value; |
152 | 151 |
|
153 | 152 |
typedef Value ConstReturnValue; |
154 | 153 |
typedef Value ReturnValue; |
155 | 154 |
}; |
156 | 155 |
|
157 | 156 |
template <typename Map> |
158 | 157 |
struct MapTraits< |
159 | 158 |
Map, typename enable_if<typename Map::ReferenceMapTag, void>::type > |
160 | 159 |
{ |
161 | 160 |
typedef True ReferenceMapTag; |
162 | 161 |
|
163 | 162 |
typedef typename Map::Key Key; |
164 | 163 |
typedef typename Map::Value Value; |
165 | 164 |
|
166 | 165 |
typedef typename Map::ConstReference ConstReturnValue; |
167 | 166 |
typedef typename Map::Reference ReturnValue; |
168 | 167 |
|
169 | 168 |
typedef typename Map::ConstReference ConstReference; |
170 | 169 |
typedef typename Map::Reference Reference; |
171 | 170 |
}; |
172 | 171 |
|
173 | 172 |
template <typename MatrixMap, typename Enable = void> |
174 | 173 |
struct MatrixMapTraits { |
175 | 174 |
typedef False ReferenceMapTag; |
176 | 175 |
|
177 | 176 |
typedef typename MatrixMap::FirstKey FirstKey; |
178 | 177 |
typedef typename MatrixMap::SecondKey SecondKey; |
179 | 178 |
typedef typename MatrixMap::Value Value; |
180 | 179 |
|
181 | 180 |
typedef Value ConstReturnValue; |
182 | 181 |
typedef Value ReturnValue; |
183 | 182 |
}; |
184 | 183 |
|
185 | 184 |
template <typename MatrixMap> |
186 | 185 |
struct MatrixMapTraits< |
187 | 186 |
MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag, |
188 | 187 |
void>::type > |
189 | 188 |
{ |
190 | 189 |
typedef True ReferenceMapTag; |
191 | 190 |
|
192 | 191 |
typedef typename MatrixMap::FirstKey FirstKey; |
193 | 192 |
typedef typename MatrixMap::SecondKey SecondKey; |
194 | 193 |
typedef typename MatrixMap::Value Value; |
195 | 194 |
|
196 | 195 |
typedef typename MatrixMap::ConstReference ConstReturnValue; |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_UTILITY_H |
36 | 36 |
#define LEMON_BITS_UTILITY_H |
37 | 37 |
|
38 | 38 |
///\file |
39 | 39 |
///\brief Miscellaneous basic utilities |
40 | 40 |
/// |
41 | 41 |
///\todo Please rethink the organisation of the basic files like this. |
42 | 42 |
///E.g. this file might be merged with invalid.h. |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
namespace lemon |
46 | 46 |
{ |
47 | 47 |
|
48 | 48 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
49 | 49 |
|
50 | 50 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
51 | 51 |
/// |
52 | 52 |
///\sa False |
53 | 53 |
/// |
54 | 54 |
/// \todo This should go to a separate "basic_types.h" (or something) |
55 | 55 |
/// file. |
56 | 56 |
struct True { |
57 | 57 |
///\e |
58 | 58 |
static const bool value = true; |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
62 | 62 |
|
63 | 63 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
64 | 64 |
/// |
65 | 65 |
///\sa True |
66 | 66 |
struct False { |
67 | 67 |
///\e |
68 | 68 |
static const bool value = false; |
69 | 69 |
}; |
70 | 70 |
|
71 | 71 |
|
72 | 72 |
struct InvalidType { |
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
template <typename T> |
76 | 76 |
struct Wrap { |
77 | 77 |
const T &value; |
78 | 78 |
Wrap(const T &t) : value(t) {} |
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
/**************** dummy class to avoid ambiguity ****************/ |
82 | 82 |
|
83 | 83 |
template<int T> struct dummy { dummy(int) {} }; |
84 | 84 |
|
85 | 85 |
/**************** enable_if from BOOST ****************/ |
86 | 86 |
|
87 | 87 |
template <typename Type, typename T = void> |
88 | 88 |
struct exists { |
89 | 89 |
typedef T type; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
template <bool B, class T = void> |
94 | 94 |
struct enable_if_c { |
95 | 95 |
typedef T type; |
96 | 96 |
}; |
97 | 97 |
|
98 | 98 |
template <class T> |
99 | 99 |
struct enable_if_c<false, T> {}; |
100 | 100 |
|
101 | 101 |
template <class Cond, class T = void> |
102 | 102 |
struct enable_if : public enable_if_c<Cond::value, T> {}; |
103 | 103 |
|
104 | 104 |
template <bool B, class T> |
105 | 105 |
struct lazy_enable_if_c { |
106 | 106 |
typedef typename T::type type; |
107 | 107 |
}; |
108 | 108 |
|
109 | 109 |
template <class T> |
110 | 110 |
struct lazy_enable_if_c<false, T> {}; |
111 | 111 |
|
112 | 112 |
template <class Cond, class T> |
113 | 113 |
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {}; |
114 | 114 |
|
115 | 115 |
|
116 | 116 |
template <bool B, class T = void> |
117 | 117 |
struct disable_if_c { |
118 | 118 |
typedef T type; |
119 | 119 |
}; |
120 | 120 |
|
121 | 121 |
template <class T> |
122 | 122 |
struct disable_if_c<true, T> {}; |
123 | 123 |
|
124 | 124 |
template <class Cond, class T = void> |
125 | 125 |
struct disable_if : public disable_if_c<Cond::value, T> {}; |
126 | 126 |
|
127 | 127 |
template <bool B, class T> |
128 | 128 |
struct lazy_disable_if_c { |
129 | 129 |
typedef typename T::type type; |
130 | 130 |
}; |
131 | 131 |
|
132 | 132 |
template <class T> |
133 | 133 |
struct lazy_disable_if_c<true, T> {}; |
134 | 134 |
|
135 | 135 |
template <class Cond, class T> |
136 | 136 |
struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {}; |
137 | 137 |
|
138 | 138 |
} // namespace lemon |
139 | 139 |
|
140 | 140 |
#endif |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/traits.h> |
26 | 26 |
#include <lemon/bits/utility.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/alteration_notifier.h> |
29 | 29 |
|
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
#include <lemon/concepts/maps.h> |
32 | 32 |
|
33 | 33 |
///\ingroup graphbits |
34 | 34 |
/// |
35 | 35 |
///\file |
36 | 36 |
///\brief Vector based graph maps. |
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \ingroup graphbits |
40 | 40 |
/// |
41 | 41 |
/// \brief Graph map based on the std::vector storage. |
42 | 42 |
/// |
43 | 43 |
/// The VectorMap template class is graph map structure what |
44 | 44 |
/// automatically updates the map when a key is added to or erased from |
45 | 45 |
/// the map. This map type uses the std::vector to store the values. |
46 | 46 |
/// |
47 | 47 |
/// \tparam _Notifier The AlterationNotifier that will notify this map. |
48 | 48 |
/// \tparam _Item The item type of the graph items. |
49 | 49 |
/// \tparam _Value The value type of the map. |
50 | 50 |
/// \todo Fix the doc: there is _Graph parameter instead of _Notifier. |
51 | 51 |
template <typename _Graph, typename _Item, typename _Value> |
52 | 52 |
class VectorMap |
53 | 53 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
54 | 54 |
private: |
55 | 55 |
|
56 | 56 |
/// The container type of the map. |
57 | 57 |
typedef std::vector<_Value> Container; |
58 | 58 |
|
59 | 59 |
public: |
60 | 60 |
|
61 | 61 |
/// The graph type of the map. |
62 | 62 |
typedef _Graph Graph; |
63 | 63 |
/// The item type of the map. |
64 | 64 |
typedef _Item Item; |
65 | 65 |
/// The reference map tag. |
66 | 66 |
typedef True ReferenceMapTag; |
67 | 67 |
|
68 | 68 |
/// The key type of the map. |
69 | 69 |
typedef _Item Key; |
70 | 70 |
/// The value type of the map. |
71 | 71 |
typedef _Value Value; |
72 | 72 |
|
73 | 73 |
/// The notifier type. |
74 | 74 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
75 | 75 |
|
76 | 76 |
/// The map type. |
77 | 77 |
typedef VectorMap Map; |
78 | 78 |
/// The base class of the map. |
79 | 79 |
typedef typename Notifier::ObserverBase Parent; |
80 | 80 |
|
81 | 81 |
/// The reference type of the map; |
82 | 82 |
typedef typename Container::reference Reference; |
83 | 83 |
/// The const reference type of the map; |
84 | 84 |
typedef typename Container::const_reference ConstReference; |
85 | 85 |
|
86 | 86 |
|
87 | 87 |
/// \brief Constructor to attach the new map into the notifier. |
88 | 88 |
/// |
89 | 89 |
/// It constructs a map and attachs it into the notifier. |
90 | 90 |
/// It adds all the items of the graph to the map. |
91 | 91 |
VectorMap(const Graph& graph) { |
92 | 92 |
Parent::attach(graph.notifier(Item())); |
93 | 93 |
container.resize(Parent::notifier()->maxId() + 1); |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
/// \brief Constructor uses given value to initialize the map. |
97 | 97 |
/// |
98 | 98 |
/// It constructs a map uses a given value to initialize the map. |
99 | 99 |
/// It adds all the items of the graph to the map. |
100 | 100 |
VectorMap(const Graph& graph, const Value& value) { |
101 | 101 |
Parent::attach(graph.notifier(Item())); |
102 | 102 |
container.resize(Parent::notifier()->maxId() + 1, value); |
103 | 103 |
} |
104 | 104 |
|
105 | 105 |
/// \brief Copy constructor |
106 | 106 |
/// |
107 | 107 |
/// Copy constructor. |
108 | 108 |
VectorMap(const VectorMap& _copy) : Parent() { |
109 | 109 |
if (_copy.attached()) { |
110 | 110 |
Parent::attach(*_copy.notifier()); |
111 | 111 |
container = _copy.container; |
112 | 112 |
} |
113 | 113 |
} |
114 | 114 |
|
115 | 115 |
/// \brief Assign operator. |
116 | 116 |
/// |
117 | 117 |
/// This operator assigns for each item in the map the |
118 | 118 |
/// value mapped to the same item in the copied map. |
119 | 119 |
/// The parameter map should be indiced with the same |
120 | 120 |
/// itemset because this assign operator does not change |
121 | 121 |
/// the container of the map. |
122 | 122 |
VectorMap& operator=(const VectorMap& cmap) { |
123 | 123 |
return operator=<VectorMap>(cmap); |
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
|
127 | 127 |
/// \brief Template assign operator. |
128 | 128 |
/// |
129 | 129 |
/// The given parameter should be conform to the ReadMap |
130 | 130 |
/// concecpt and could be indiced by the current item set of |
131 | 131 |
/// the NodeMap. In this case the value for each item |
132 | 132 |
/// is assigned by the value of the given ReadMap. |
133 | 133 |
template <typename CMap> |
134 | 134 |
VectorMap& operator=(const CMap& cmap) { |
135 | 135 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
136 | 136 |
const typename Parent::Notifier* nf = Parent::notifier(); |
137 | 137 |
Item it; |
138 | 138 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
139 | 139 |
set(it, cmap[it]); |
140 | 140 |
} |
141 | 141 |
return *this; |
142 | 142 |
} |
143 | 143 |
|
144 | 144 |
public: |
145 | 145 |
|
146 | 146 |
/// \brief The subcript operator. |
147 | 147 |
/// |
148 | 148 |
/// The subscript operator. The map can be subscripted by the |
149 | 149 |
/// actual items of the graph. |
150 | 150 |
Reference operator[](const Key& key) { |
151 | 151 |
return container[Parent::notifier()->id(key)]; |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
/// \brief The const subcript operator. |
155 | 155 |
/// |
156 | 156 |
/// The const subscript operator. The map can be subscripted by the |
157 | 157 |
/// actual items of the graph. |
158 | 158 |
ConstReference operator[](const Key& key) const { |
159 | 159 |
return container[Parent::notifier()->id(key)]; |
160 | 160 |
} |
161 | 161 |
|
162 | 162 |
|
163 | 163 |
/// \brief The setter function of the map. |
164 | 164 |
/// |
165 | 165 |
/// It the same as operator[](key) = value expression. |
166 | 166 |
void set(const Key& key, const Value& value) { |
167 | 167 |
(*this)[key] = value; |
168 | 168 |
} |
169 | 169 |
|
170 | 170 |
protected: |
171 | 171 |
|
172 | 172 |
/// \brief Adds a new key to the map. |
173 | 173 |
/// |
174 | 174 |
/// It adds a new key to the map. It called by the observer notifier |
175 | 175 |
/// and it overrides the add() member function of the observer base. |
176 | 176 |
virtual void add(const Key& key) { |
177 | 177 |
int id = Parent::notifier()->id(key); |
178 | 178 |
if (id >= int(container.size())) { |
179 | 179 |
container.resize(id + 1); |
180 | 180 |
} |
181 | 181 |
} |
182 | 182 |
|
183 | 183 |
/// \brief Adds more new keys to the map. |
184 | 184 |
/// |
185 | 185 |
/// It adds more new keys to the map. It called by the observer notifier |
186 | 186 |
/// and it overrides the add() member function of the observer base. |
187 | 187 |
virtual void add(const std::vector<Key>& keys) { |
188 | 188 |
int max = container.size() - 1; |
189 | 189 |
for (int i = 0; i < int(keys.size()); ++i) { |
190 | 190 |
int id = Parent::notifier()->id(keys[i]); |
191 | 191 |
if (id >= max) { |
192 | 192 |
max = id; |
193 | 193 |
} |
194 | 194 |
} |
195 | 195 |
container.resize(max + 1); |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 \ref Color "Color"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. |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 concept checking |
20 | 20 |
// utility from BOOST. |
21 | 21 |
// See the appropriate copyright notice below. |
22 | 22 |
|
23 | 23 |
// (C) Copyright Jeremy Siek 2000. |
24 | 24 |
// Distributed under the Boost Software License, Version 1.0. (See |
25 | 25 |
// accompanying file LICENSE_1_0.txt or copy at |
26 | 26 |
// http://www.boost.org/LICENSE_1_0.txt) |
27 | 27 |
// |
28 | 28 |
// Revision History: |
29 | 29 |
// 05 May 2001: Workarounds for HP aCC from Thomas Matelich. (Jeremy Siek) |
30 | 30 |
// 02 April 2001: Removed limits header altogether. (Jeremy Siek) |
31 | 31 |
// 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock) |
32 | 32 |
// |
33 | 33 |
|
34 | 34 |
// See http://www.boost.org/libs/concept_check for documentation. |
35 | 35 |
|
36 | 36 |
///\file |
37 | 37 |
///\brief Basic utilities for concept checking. |
38 | 38 |
/// |
39 | 39 |
///\todo Are we still using BOOST concept checking utility? |
40 | 40 |
///Is the BOOST copyright notice necessary? |
41 | 41 |
|
42 | 42 |
#ifndef LEMON_CONCEPT_CHECK_H |
43 | 43 |
#define LEMON_CONCEPT_CHECK_H |
44 | 44 |
|
45 | 45 |
namespace lemon { |
46 | 46 |
|
47 | 47 |
/* |
48 | 48 |
"inline" is used for ignore_unused_variable_warning() |
49 | 49 |
and function_requires() to make sure there is no |
50 | 50 |
overtarget with g++. |
51 | 51 |
*/ |
52 | 52 |
|
53 | 53 |
template <class T> inline void ignore_unused_variable_warning(const T&) { } |
54 | 54 |
|
55 | 55 |
///\e |
56 | 56 |
template <class Concept> |
57 | 57 |
inline void function_requires() |
58 | 58 |
{ |
59 | 59 |
#if !defined(NDEBUG) |
60 | 60 |
void (Concept::*x)() = & Concept::constraints; |
61 | 61 |
ignore_unused_variable_warning(x); |
62 | 62 |
#endif |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
///\e |
66 | 66 |
template <typename Concept, typename Type> |
67 | 67 |
inline void checkConcept() { |
68 | 68 |
#if !defined(NDEBUG) |
69 | 69 |
typedef typename Concept::template Constraints<Type> ConceptCheck; |
70 | 70 |
void (ConceptCheck::*x)() = & ConceptCheck::constraints; |
71 | 71 |
ignore_unused_variable_warning(x); |
72 | 72 |
#endif |
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
} // namespace lemon |
76 | 76 |
|
77 | 77 |
#endif // LEMON_CONCEPT_CHECK_H |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/invalid.h> |
27 | 27 |
#include <lemon/bits/utility.h> |
28 | 28 |
#include <lemon/concepts/maps.h> |
29 | 29 |
#include <lemon/concept_check.h> |
30 | 30 |
#include <lemon/concepts/graph_components.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
namespace concepts { |
34 | 34 |
|
35 | 35 |
/// \ingroup graph_concepts |
36 | 36 |
/// |
37 | 37 |
/// \brief Class describing the concept of directed graphs. |
38 | 38 |
/// |
39 | 39 |
/// This class describes the \ref concept "concept" of the |
40 | 40 |
/// immutable directed digraphs. |
41 | 41 |
/// |
42 | 42 |
/// Note that actual digraph implementation like @ref ListDigraph or |
43 | 43 |
/// @ref SmartDigraph may have several additional functionality. |
44 | 44 |
/// |
45 | 45 |
/// \sa concept |
46 | 46 |
class Digraph { |
47 | 47 |
private: |
48 | 48 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
49 | 49 |
|
50 | 50 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
51 | 51 |
/// |
52 | 52 |
Digraph(const Digraph &) {}; |
53 | 53 |
///\brief Assignment of \ref Digraph "Digraph"s to another ones are |
54 | 54 |
///\e not allowed. Use DigraphCopy() instead. |
55 | 55 |
|
56 | 56 |
///Assignment of \ref Digraph "Digraph"s to another ones are |
57 | 57 |
///\e not allowed. Use DigraphCopy() instead. |
58 | 58 |
|
59 | 59 |
void operator=(const Digraph &) {} |
60 | 60 |
public: |
61 | 61 |
///\e |
62 | 62 |
|
63 | 63 |
/// Defalult constructor. |
64 | 64 |
|
65 | 65 |
/// Defalult constructor. |
66 | 66 |
/// |
67 | 67 |
Digraph() { } |
68 | 68 |
/// Class for identifying a node of the digraph |
69 | 69 |
|
70 | 70 |
/// This class identifies a node of the digraph. It also serves |
71 | 71 |
/// as a base class of the node iterators, |
72 | 72 |
/// thus they will convert to this type. |
73 | 73 |
class Node { |
74 | 74 |
public: |
75 | 75 |
/// Default constructor |
76 | 76 |
|
77 | 77 |
/// @warning The default constructor sets the iterator |
78 | 78 |
/// to an undefined value. |
79 | 79 |
Node() { } |
80 | 80 |
/// Copy constructor. |
81 | 81 |
|
82 | 82 |
/// Copy constructor. |
83 | 83 |
/// |
84 | 84 |
Node(const Node&) { } |
85 | 85 |
|
86 | 86 |
/// Invalid constructor \& conversion. |
87 | 87 |
|
88 | 88 |
/// This constructor initializes the iterator to be invalid. |
89 | 89 |
/// \sa Invalid for more details. |
90 | 90 |
Node(Invalid) { } |
91 | 91 |
/// Equality operator |
92 | 92 |
|
93 | 93 |
/// Two iterators are equal if and only if they point to the |
94 | 94 |
/// same object or both are invalid. |
95 | 95 |
bool operator==(Node) const { return true; } |
96 | 96 |
|
97 | 97 |
/// Inequality operator |
98 | 98 |
|
99 | 99 |
/// \sa operator==(Node n) |
100 | 100 |
/// |
101 | 101 |
bool operator!=(Node) const { return true; } |
102 | 102 |
|
103 | 103 |
/// Artificial ordering operator. |
104 | 104 |
|
105 | 105 |
/// To allow the use of digraph descriptors as key type in std::map or |
106 | 106 |
/// similar associative container we require this. |
107 | 107 |
/// |
108 | 108 |
/// \note This operator only have to define some strict ordering of |
109 | 109 |
/// the items; this order has nothing to do with the iteration |
110 | 110 |
/// ordering of the items. |
111 | 111 |
bool operator<(Node) const { return false; } |
112 | 112 |
|
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
/// This iterator goes through each node. |
116 | 116 |
|
117 | 117 |
/// This iterator goes through each node. |
118 | 118 |
/// Its usage is quite simple, for example you can count the number |
119 | 119 |
/// of nodes in digraph \c g of type \c Digraph like this: |
120 | 120 |
///\code |
121 | 121 |
/// int count=0; |
122 | 122 |
/// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count; |
123 | 123 |
///\endcode |
124 | 124 |
class NodeIt : public Node { |
125 | 125 |
public: |
126 | 126 |
/// Default constructor |
127 | 127 |
|
128 | 128 |
/// @warning The default constructor sets the iterator |
129 | 129 |
/// to an undefined value. |
130 | 130 |
NodeIt() { } |
131 | 131 |
/// Copy constructor. |
132 | 132 |
|
133 | 133 |
/// Copy constructor. |
134 | 134 |
/// |
135 | 135 |
NodeIt(const NodeIt& n) : Node(n) { } |
136 | 136 |
/// Invalid constructor \& conversion. |
137 | 137 |
|
138 | 138 |
/// Initialize the iterator to be invalid. |
139 | 139 |
/// \sa Invalid for more details. |
140 | 140 |
NodeIt(Invalid) { } |
141 | 141 |
/// Sets the iterator to the first node. |
142 | 142 |
|
143 | 143 |
/// Sets the iterator to the first node of \c g. |
144 | 144 |
/// |
145 | 145 |
NodeIt(const Digraph&) { } |
146 | 146 |
/// Node -> NodeIt conversion. |
147 | 147 |
|
148 | 148 |
/// Sets the iterator to the node of \c the digraph pointed by |
149 | 149 |
/// the trivial iterator. |
150 | 150 |
/// This feature necessitates that each time we |
151 | 151 |
/// iterate the arc-set, the iteration order is the same. |
152 | 152 |
NodeIt(const Digraph&, const Node&) { } |
153 | 153 |
/// Next node. |
154 | 154 |
|
155 | 155 |
/// Assign the iterator to the next node. |
156 | 156 |
/// |
157 | 157 |
NodeIt& operator++() { return *this; } |
158 | 158 |
}; |
159 | 159 |
|
160 | 160 |
|
161 | 161 |
/// Class for identifying an arc of the digraph |
162 | 162 |
|
163 | 163 |
/// This class identifies an arc of the digraph. It also serves |
164 | 164 |
/// as a base class of the arc iterators, |
165 | 165 |
/// thus they will convert to this type. |
166 | 166 |
class Arc { |
167 | 167 |
public: |
168 | 168 |
/// Default constructor |
169 | 169 |
|
170 | 170 |
/// @warning The default constructor sets the iterator |
171 | 171 |
/// to an undefined value. |
172 | 172 |
Arc() { } |
173 | 173 |
/// Copy constructor. |
174 | 174 |
|
175 | 175 |
/// Copy constructor. |
176 | 176 |
/// |
177 | 177 |
Arc(const Arc&) { } |
178 | 178 |
/// Initialize the iterator to be invalid. |
179 | 179 |
|
180 | 180 |
/// Initialize the iterator to be invalid. |
181 | 181 |
/// |
182 | 182 |
Arc(Invalid) { } |
183 | 183 |
/// Equality operator |
184 | 184 |
|
185 | 185 |
/// Two iterators are equal if and only if they point to the |
186 | 186 |
/// same object or both are invalid. |
187 | 187 |
bool operator==(Arc) const { return true; } |
188 | 188 |
/// Inequality operator |
189 | 189 |
|
190 | 190 |
/// \sa operator==(Arc n) |
191 | 191 |
/// |
192 | 192 |
bool operator!=(Arc) const { return true; } |
193 | 193 |
|
194 | 194 |
/// Artificial ordering operator. |
195 | 195 |
|
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/utility.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 |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/invalid.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 |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/invalid.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 |
/// It may throw an \ref UnderflowPriorityException. |
133 | 133 |
/// \param i The item. |
134 | 134 |
/// \param p The priority. |
135 | 135 |
void set(const Item &i, const Prio &p) {} |
136 | 136 |
|
137 | 137 |
/// \brief Decreases the priority of an item to the given value. |
138 | 138 |
/// |
139 | 139 |
/// Decreases the priority of an item to the given value. |
140 | 140 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
141 | 141 |
/// \param i The item. |
142 | 142 |
/// \param p The priority. |
143 | 143 |
void decrease(const Item &i, const Prio &p) {} |
144 | 144 |
|
145 | 145 |
/// \brief Increases the priority of an item to the given value. |
146 | 146 |
/// |
147 | 147 |
/// Increases the priority of an item to the given value. |
148 | 148 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
149 | 149 |
/// \param i The item. |
150 | 150 |
/// \param p The priority. |
151 | 151 |
void increase(const Item &i, const Prio &p) {} |
152 | 152 |
|
153 | 153 |
/// \brief Returns if an item is in, has already been in, or has |
154 | 154 |
/// never been in the heap. |
155 | 155 |
/// |
156 | 156 |
/// This method returns \c PRE_HEAP if the given item has never |
157 | 157 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
158 | 158 |
/// and \c POST_HEAP otherwise. |
159 | 159 |
/// In the latter case it is possible that the item will get back |
160 | 160 |
/// to the heap again. |
161 | 161 |
/// \param i The item. |
162 | 162 |
State state(const Item &i) const {} |
163 | 163 |
|
164 | 164 |
/// \brief Sets the state of an item in the heap. |
165 | 165 |
/// |
166 | 166 |
/// Sets the state of the given item in the heap. It can be used |
167 | 167 |
/// to manually clear the heap when it is important to achive the |
168 | 168 |
/// better time complexity. |
169 | 169 |
/// \param i The item. |
170 | 170 |
/// \param st The state. It should not be \c IN_HEAP. |
171 | 171 |
void state(const Item& i, State st) {} |
172 | 172 |
|
173 | 173 |
|
174 | 174 |
template <typename _Heap> |
175 | 175 |
struct Constraints { |
176 | 176 |
public: |
177 | 177 |
void constraints() { |
178 | 178 |
typedef typename _Heap::Item OwnItem; |
179 | 179 |
typedef typename _Heap::Prio OwnPrio; |
180 | 180 |
typedef typename _Heap::State OwnState; |
181 | 181 |
|
182 | 182 |
Item item; |
183 | 183 |
Prio prio; |
184 | 184 |
item=Item(); |
185 | 185 |
prio=Prio(); |
186 | 186 |
ignore_unused_variable_warning(item); |
187 | 187 |
ignore_unused_variable_warning(prio); |
188 | 188 |
|
189 | 189 |
OwnItem own_item; |
190 | 190 |
OwnPrio own_prio; |
191 | 191 |
OwnState own_state; |
192 | 192 |
own_item=Item(); |
193 | 193 |
own_prio=Prio(); |
194 | 194 |
ignore_unused_variable_warning(own_item); |
195 | 195 |
ignore_unused_variable_warning(own_prio); |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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/bits/utility.h> |
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 | 25 |
///\ingroup concept |
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 concept |
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 |
/// The value type of the map. (The type of objects associated with the keys). |
47 | 47 |
typedef T Value; |
48 | 48 |
|
49 | 49 |
/// Returns the value associated with the given key. |
50 | 50 |
Value operator[](const Key &) const { |
51 | 51 |
return *static_cast<Value *>(0); |
52 | 52 |
} |
53 | 53 |
|
54 | 54 |
template<typename _ReadMap> |
55 | 55 |
struct Constraints { |
56 | 56 |
void constraints() { |
57 | 57 |
Value val = m[key]; |
58 | 58 |
val = m[key]; |
59 | 59 |
typename _ReadMap::Value own_val = m[own_key]; |
60 | 60 |
own_val = m[own_key]; |
61 | 61 |
|
62 | 62 |
ignore_unused_variable_warning(key); |
63 | 63 |
ignore_unused_variable_warning(val); |
64 | 64 |
ignore_unused_variable_warning(own_key); |
65 | 65 |
ignore_unused_variable_warning(own_val); |
66 | 66 |
} |
67 | 67 |
const Key& key; |
68 | 68 |
const typename _ReadMap::Key& own_key; |
69 | 69 |
const _ReadMap& m; |
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
}; |
73 | 73 |
|
74 | 74 |
|
75 | 75 |
/// Writable map concept |
76 | 76 |
|
77 | 77 |
/// Writable map concept. |
78 | 78 |
/// |
79 | 79 |
template<typename K, typename T> |
80 | 80 |
class WriteMap |
81 | 81 |
{ |
82 | 82 |
public: |
83 | 83 |
/// The key type of the map. |
84 | 84 |
typedef K Key; |
85 | 85 |
/// The value type of the map. (The type of objects associated with the keys). |
86 | 86 |
typedef T Value; |
87 | 87 |
|
88 | 88 |
/// Sets the value associated with the given key. |
89 | 89 |
void set(const Key &, const Value &) {} |
90 | 90 |
|
91 | 91 |
/// Default constructor. |
92 | 92 |
WriteMap() {} |
93 | 93 |
|
94 | 94 |
template <typename _WriteMap> |
95 | 95 |
struct Constraints { |
96 | 96 |
void constraints() { |
97 | 97 |
m.set(key, val); |
98 | 98 |
m.set(own_key, own_val); |
99 | 99 |
|
100 | 100 |
ignore_unused_variable_warning(key); |
101 | 101 |
ignore_unused_variable_warning(val); |
102 | 102 |
ignore_unused_variable_warning(own_key); |
103 | 103 |
ignore_unused_variable_warning(own_val); |
104 | 104 |
} |
105 | 105 |
const Key& key; |
106 | 106 |
const Value& val; |
107 | 107 |
const typename _WriteMap::Key& own_key; |
108 | 108 |
const typename _WriteMap::Value& own_val; |
109 | 109 |
_WriteMap& m; |
110 | 110 |
}; |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
/// Read/writable map concept |
114 | 114 |
|
115 | 115 |
/// Read/writable map concept. |
116 | 116 |
/// |
117 | 117 |
template<typename K, typename T> |
118 | 118 |
class ReadWriteMap : public ReadMap<K,T>, |
119 | 119 |
public WriteMap<K,T> |
120 | 120 |
{ |
121 | 121 |
public: |
122 | 122 |
/// The key type of the map. |
123 | 123 |
typedef K Key; |
124 | 124 |
/// The value type of the map. (The type of objects associated with the keys). |
125 | 125 |
typedef T Value; |
126 | 126 |
|
127 | 127 |
/// Returns the value associated with the given key. |
128 | 128 |
Value operator[](const Key &) const { |
129 | 129 |
return *static_cast<Value *>(0); |
130 | 130 |
} |
131 | 131 |
|
132 | 132 |
/// Sets the value associated with the given key. |
133 | 133 |
void set(const Key &, const Value &) {} |
134 | 134 |
|
135 | 135 |
template<typename _ReadWriteMap> |
136 | 136 |
struct Constraints { |
137 | 137 |
void constraints() { |
138 | 138 |
checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
139 | 139 |
checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
140 | 140 |
} |
141 | 141 |
}; |
142 | 142 |
}; |
143 | 143 |
|
144 | 144 |
|
145 | 145 |
/// Dereferable map concept |
146 | 146 |
|
147 | 147 |
/// Dereferable map concept. |
148 | 148 |
/// |
149 | 149 |
template<typename K, typename T, typename R, typename CR> |
150 | 150 |
class ReferenceMap : public ReadWriteMap<K,T> |
151 | 151 |
{ |
152 | 152 |
public: |
153 | 153 |
/// Tag for reference maps. |
154 | 154 |
typedef True ReferenceMapTag; |
155 | 155 |
/// The key type of the map. |
156 | 156 |
typedef K Key; |
157 | 157 |
/// The value type of the map. (The type of objects associated with the keys). |
158 | 158 |
typedef T Value; |
159 | 159 |
/// The reference type of the map. |
160 | 160 |
typedef R Reference; |
161 | 161 |
/// The const reference type of the map. |
162 | 162 |
typedef CR ConstReference; |
163 | 163 |
|
164 | 164 |
public: |
165 | 165 |
|
166 | 166 |
/// Returns a reference to the value associated with the given key. |
167 | 167 |
Reference operator[](const Key &) { |
168 | 168 |
return *static_cast<Value *>(0); |
169 | 169 |
} |
170 | 170 |
|
171 | 171 |
/// Returns a const reference to the value associated with the given key. |
172 | 172 |
ConstReference operator[](const Key &) const { |
173 | 173 |
return *static_cast<Value *>(0); |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
/// Sets the value associated with the given key. |
177 | 177 |
void set(const Key &k,const Value &t) { operator[](k)=t; } |
178 | 178 |
|
179 | 179 |
template<typename _ReferenceMap> |
180 | 180 |
struct Constraints { |
181 | 181 |
void constraints() { |
182 | 182 |
checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
183 | 183 |
ref = m[key]; |
184 | 184 |
m[key] = val; |
185 | 185 |
m[key] = ref; |
186 | 186 |
m[key] = cref; |
187 | 187 |
own_ref = m[own_key]; |
188 | 188 |
m[own_key] = own_val; |
189 | 189 |
m[own_key] = own_ref; |
190 | 190 |
m[own_key] = own_cref; |
191 | 191 |
m[key] = m[own_key]; |
192 | 192 |
m[own_key] = m[key]; |
193 | 193 |
} |
194 | 194 |
const Key& key; |
195 | 195 |
Value& val; |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
///\todo Iterators have obsolete style |
24 | 24 |
|
25 | 25 |
#ifndef LEMON_CONCEPT_PATH_H |
26 | 26 |
#define LEMON_CONCEPT_PATH_H |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/invalid.h> |
29 | 29 |
#include <lemon/bits/utility.h> |
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
namespace concepts { |
34 | 34 |
|
35 | 35 |
/// \addtogroup concept |
36 | 36 |
/// @{ |
37 | 37 |
|
38 | 38 |
/// \brief A skeleton structure for representing directed paths in |
39 | 39 |
/// a digraph. |
40 | 40 |
/// |
41 | 41 |
/// A skeleton structure for representing directed paths in a |
42 | 42 |
/// digraph. |
43 | 43 |
/// \tparam _Digraph The digraph type in which the path is. |
44 | 44 |
/// |
45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
46 | 46 |
/// lemon path type stores just this list. As a consequence it |
47 | 47 |
/// cannot enumerate the nodes in the path and the zero length |
48 | 48 |
/// paths cannot store the source. |
49 | 49 |
/// |
50 | 50 |
template <typename _Digraph> |
51 | 51 |
class Path { |
52 | 52 |
public: |
53 | 53 |
|
54 | 54 |
/// Type of the underlying digraph. |
55 | 55 |
typedef _Digraph Digraph; |
56 | 56 |
/// Arc type of the underlying digraph. |
57 | 57 |
typedef typename Digraph::Arc Arc; |
58 | 58 |
|
59 | 59 |
class ArcIt; |
60 | 60 |
|
61 | 61 |
/// \brief Default constructor |
62 | 62 |
Path() {} |
63 | 63 |
|
64 | 64 |
/// \brief Template constructor |
65 | 65 |
template <typename CPath> |
66 | 66 |
Path(const CPath& cpath) {} |
67 | 67 |
|
68 | 68 |
/// \brief Template assigment |
69 | 69 |
template <typename CPath> |
70 | 70 |
Path& operator=(const CPath& cpath) {} |
71 | 71 |
|
72 | 72 |
/// Length of the path ie. the number of arcs in the path. |
73 | 73 |
int length() const { return 0;} |
74 | 74 |
|
75 | 75 |
/// Returns whether the path is empty. |
76 | 76 |
bool empty() const { return true;} |
77 | 77 |
|
78 | 78 |
/// Resets the path to an empty path. |
79 | 79 |
void clear() {} |
80 | 80 |
|
81 | 81 |
/// \brief Lemon style iterator for path arcs |
82 | 82 |
/// |
83 | 83 |
/// This class is used to iterate on the arcs of the paths. |
84 | 84 |
class ArcIt { |
85 | 85 |
public: |
86 | 86 |
/// Default constructor |
87 | 87 |
ArcIt() {} |
88 | 88 |
/// Invalid constructor |
89 | 89 |
ArcIt(Invalid) {} |
90 | 90 |
/// Constructor for first arc |
91 | 91 |
ArcIt(const Path &) {} |
92 | 92 |
|
93 | 93 |
/// Conversion to Arc |
94 | 94 |
operator Arc() const { return INVALID; } |
95 | 95 |
|
96 | 96 |
/// Next arc |
97 | 97 |
ArcIt& operator++() {return *this;} |
98 | 98 |
|
99 | 99 |
/// Comparison operator |
100 | 100 |
bool operator==(const ArcIt&) const {return true;} |
101 | 101 |
/// Comparison operator |
102 | 102 |
bool operator!=(const ArcIt&) const {return true;} |
103 | 103 |
/// Comparison operator |
104 | 104 |
bool operator<(const ArcIt&) const {return false;} |
105 | 105 |
|
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename _Path> |
109 | 109 |
struct Constraints { |
110 | 110 |
void constraints() { |
111 | 111 |
Path<Digraph> pc; |
112 | 112 |
_Path p, pp(pc); |
113 | 113 |
int l = p.length(); |
114 | 114 |
int e = p.empty(); |
115 | 115 |
p.clear(); |
116 | 116 |
|
117 | 117 |
p = pc; |
118 | 118 |
|
119 | 119 |
typename _Path::ArcIt id, ii(INVALID), i(p); |
120 | 120 |
|
121 | 121 |
++i; |
122 | 122 |
typename Digraph::Arc ed = i; |
123 | 123 |
|
124 | 124 |
e = (i == ii); |
125 | 125 |
e = (i != ii); |
126 | 126 |
e = (i < ii); |
127 | 127 |
|
128 | 128 |
ignore_unused_variable_warning(l); |
129 | 129 |
ignore_unused_variable_warning(pp); |
130 | 130 |
ignore_unused_variable_warning(e); |
131 | 131 |
ignore_unused_variable_warning(id); |
132 | 132 |
ignore_unused_variable_warning(ii); |
133 | 133 |
ignore_unused_variable_warning(ed); |
134 | 134 |
} |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
}; |
138 | 138 |
|
139 | 139 |
namespace _path_bits { |
140 | 140 |
|
141 | 141 |
template <typename _Digraph, typename _Path, typename RevPathTag = void> |
142 | 142 |
struct PathDumperConstraints { |
143 | 143 |
void constraints() { |
144 | 144 |
int l = p.length(); |
145 | 145 |
int e = p.empty(); |
146 | 146 |
|
147 | 147 |
typename _Path::ArcIt id, i(p); |
148 | 148 |
|
149 | 149 |
++i; |
150 | 150 |
typename _Digraph::Arc ed = i; |
151 | 151 |
|
152 | 152 |
e = (i == INVALID); |
153 | 153 |
e = (i != INVALID); |
154 | 154 |
|
155 | 155 |
ignore_unused_variable_warning(l); |
156 | 156 |
ignore_unused_variable_warning(e); |
157 | 157 |
ignore_unused_variable_warning(id); |
158 | 158 |
ignore_unused_variable_warning(ed); |
159 | 159 |
} |
160 | 160 |
_Path& p; |
161 | 161 |
}; |
162 | 162 |
|
163 | 163 |
template <typename _Digraph, typename _Path> |
164 | 164 |
struct PathDumperConstraints< |
165 | 165 |
_Digraph, _Path, |
166 | 166 |
typename enable_if<typename _Path::RevPathTag, void>::type |
167 | 167 |
> { |
168 | 168 |
void constraints() { |
169 | 169 |
int l = p.length(); |
170 | 170 |
int e = p.empty(); |
171 | 171 |
|
172 | 172 |
typename _Path::RevArcIt id, i(p); |
173 | 173 |
|
174 | 174 |
++i; |
175 | 175 |
typename _Digraph::Arc ed = i; |
176 | 176 |
|
177 | 177 |
e = (i == INVALID); |
178 | 178 |
e = (i != INVALID); |
179 | 179 |
|
180 | 180 |
ignore_unused_variable_warning(l); |
181 | 181 |
ignore_unused_variable_warning(e); |
182 | 182 |
ignore_unused_variable_warning(id); |
183 | 183 |
ignore_unused_variable_warning(ed); |
184 | 184 |
} |
185 | 185 |
_Path& p; |
186 | 186 |
}; |
187 | 187 |
|
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
|
191 | 191 |
/// \brief A skeleton structure for path dumpers. |
192 | 192 |
/// |
193 | 193 |
/// A skeleton structure for path dumpers. The path dumpers are |
194 | 194 |
/// the generalization of the paths. The path dumpers can |
195 | 195 |
/// enumerate the arcs of the path wheter in forward or in |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_COUNTER_H |
20 | 20 |
#define LEMON_COUNTER_H |
21 | 21 |
|
22 | 22 |
#include <string> |
23 | 23 |
#include <iostream> |
24 | 24 |
|
25 | 25 |
///\ingroup timecount |
26 | 26 |
///\file |
27 | 27 |
///\brief Tools for counting steps and events |
28 | 28 |
|
29 | 29 |
namespace lemon |
30 | 30 |
{ |
31 | 31 |
|
32 | 32 |
template<class P> class _NoSubCounter; |
33 | 33 |
|
34 | 34 |
template<class P> |
35 | 35 |
class _SubCounter |
36 | 36 |
{ |
37 | 37 |
P &_parent; |
38 | 38 |
std::string _title; |
39 | 39 |
std::ostream &_os; |
40 | 40 |
int count; |
41 | 41 |
public: |
42 | 42 |
|
43 | 43 |
typedef _SubCounter<_SubCounter<P> > SubCounter; |
44 | 44 |
typedef _NoSubCounter<_SubCounter<P> > NoSubCounter; |
45 | 45 |
|
46 | 46 |
_SubCounter(P &parent) |
47 | 47 |
: _parent(parent), _title(), _os(std::cerr), count(0) {} |
48 | 48 |
_SubCounter(P &parent,std::string title,std::ostream &os=std::cerr) |
49 | 49 |
: _parent(parent), _title(title), _os(os), count(0) {} |
50 | 50 |
_SubCounter(P &parent,const char *title,std::ostream &os=std::cerr) |
51 | 51 |
: _parent(parent), _title(title), _os(os), count(0) {} |
52 | 52 |
~_SubCounter() { |
53 | 53 |
_os << _title << count <<std::endl; |
54 | 54 |
_parent+=count; |
55 | 55 |
} |
56 | 56 |
_SubCounter &operator++() { count++; return *this;} |
57 | 57 |
int operator++(int) { return count++; } |
58 | 58 |
_SubCounter &operator--() { count--; return *this;} |
59 | 59 |
int operator--(int) { return count--; } |
60 | 60 |
_SubCounter &operator+=(int c) { count+=c; return *this;} |
61 | 61 |
_SubCounter &operator-=(int c) { count-=c; return *this;} |
62 | 62 |
operator int() {return count;} |
63 | 63 |
}; |
64 | 64 |
|
65 | 65 |
template<class P> |
66 | 66 |
class _NoSubCounter |
67 | 67 |
{ |
68 | 68 |
P &_parent; |
69 | 69 |
public: |
70 | 70 |
typedef _NoSubCounter<_NoSubCounter<P> > SubCounter; |
71 | 71 |
typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter; |
72 | 72 |
|
73 | 73 |
_NoSubCounter(P &parent) :_parent(parent) {} |
74 | 74 |
_NoSubCounter(P &parent,std::string,std::ostream &) |
75 | 75 |
:_parent(parent) {} |
76 | 76 |
_NoSubCounter(P &parent,std::string) |
77 | 77 |
:_parent(parent) {} |
78 | 78 |
_NoSubCounter(P &parent,const char *,std::ostream &) |
79 | 79 |
:_parent(parent) {} |
80 | 80 |
_NoSubCounter(P &parent,const char *) |
81 | 81 |
:_parent(parent) {} |
82 | 82 |
~_NoSubCounter() {} |
83 | 83 |
_NoSubCounter &operator++() { ++_parent; return *this;} |
84 | 84 |
int operator++(int) { _parent++; return 0;} |
85 | 85 |
_NoSubCounter &operator--() { --_parent; return *this;} |
86 | 86 |
int operator--(int) { _parent--; return 0;} |
87 | 87 |
_NoSubCounter &operator+=(int c) { _parent+=c; return *this;} |
88 | 88 |
_NoSubCounter &operator-=(int c) { _parent-=c; return *this;} |
89 | 89 |
operator int() {return 0;} |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup timecount |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// A counter class |
97 | 97 |
|
98 | 98 |
/// This class makes it easier to count certain events (e.g. for debug |
99 | 99 |
/// reasons). |
100 | 100 |
/// You can increment or decrement the counter using \c operator++, |
101 | 101 |
/// \c operator--, \c operator+= and \c operator-=. You can also |
102 | 102 |
/// define subcounters for the different phases of the algorithm or |
103 | 103 |
/// for different types of operations. |
104 | 104 |
/// A report containing the given title and the value of the counter |
105 | 105 |
/// is automatically printed on destruction. |
106 | 106 |
/// |
107 | 107 |
/// The following example shows the usage of counters and subcounters. |
108 | 108 |
/// \code |
109 | 109 |
/// // Bubble sort |
110 | 110 |
/// std::vector<T> v; |
111 | 111 |
/// ... |
112 | 112 |
/// Counter op("Operations: "); |
113 | 113 |
/// Counter::SubCounter as(op, "Assignments: "); |
114 | 114 |
/// Counter::SubCounter co(op, "Comparisons: "); |
115 | 115 |
/// for (int i = v.size()-1; i > 0; --i) { |
116 | 116 |
/// for (int j = 0; j < i; ++j) { |
117 | 117 |
/// if (v[j] > v[j+1]) { |
118 | 118 |
/// T tmp = v[j]; |
119 | 119 |
/// v[j] = v[j+1]; |
120 | 120 |
/// v[j+1] = tmp; |
121 | 121 |
/// as += 3; // three assignments |
122 | 122 |
/// } |
123 | 123 |
/// ++co; // one comparison |
124 | 124 |
/// } |
125 | 125 |
/// } |
126 | 126 |
/// \endcode |
127 | 127 |
/// |
128 | 128 |
/// This code prints out something like that: |
129 | 129 |
/// \code |
130 | 130 |
/// Comparisons: 45 |
131 | 131 |
/// Assignments: 57 |
132 | 132 |
/// Operations: 102 |
133 | 133 |
/// \endcode |
134 | 134 |
/// |
135 | 135 |
/// \sa NoCounter |
136 | 136 |
class Counter |
137 | 137 |
{ |
138 | 138 |
std::string _title; |
139 | 139 |
std::ostream &_os; |
140 | 140 |
int count; |
141 | 141 |
public: |
142 | 142 |
|
143 | 143 |
/// SubCounter class |
144 | 144 |
|
145 | 145 |
/// This class can be used to setup subcounters for a \ref Counter |
146 | 146 |
/// to have finer reports. A subcounter provides exactly the same |
147 | 147 |
/// operations as the main \ref Counter, but it also increments and |
148 | 148 |
/// decrements the value of its parent. |
149 | 149 |
/// Subcounters can also have subcounters. |
150 | 150 |
/// |
151 | 151 |
/// The parent counter must be given as the first parameter of the |
152 | 152 |
/// constructor. Apart from that a title and an \c ostream object |
153 | 153 |
/// can also be given just like for the main \ref Counter. |
154 | 154 |
/// |
155 | 155 |
/// A report containing the given title and the value of the |
156 | 156 |
/// subcounter is automatically printed on destruction. If you |
157 | 157 |
/// would like to turn off this report, use \ref NoSubCounter |
158 | 158 |
/// instead. |
159 | 159 |
/// |
160 | 160 |
/// \sa NoSubCounter |
161 | 161 |
typedef _SubCounter<Counter> SubCounter; |
162 | 162 |
|
163 | 163 |
/// SubCounter class without printing report on destruction |
164 | 164 |
|
165 | 165 |
/// This class can be used to setup subcounters for a \ref Counter. |
166 | 166 |
/// It is the same as \ref SubCounter but it does not print report |
167 | 167 |
/// on destruction. (It modifies the value of its parent, so 'No' |
168 | 168 |
/// only means 'do not print'.) |
169 | 169 |
/// |
170 | 170 |
/// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter |
171 | 171 |
/// "NoSubCounter"s makes it possible to turn off reporting |
172 | 172 |
/// subcounter values without actually removing the definitions |
173 | 173 |
/// and the increment or decrement operators. |
174 | 174 |
/// |
175 | 175 |
/// \sa SubCounter |
176 | 176 |
typedef _NoSubCounter<Counter> NoSubCounter; |
177 | 177 |
|
178 | 178 |
/// Constructor. |
179 | 179 |
Counter() : _title(), _os(std::cerr), count(0) {} |
180 | 180 |
/// Constructor. |
181 | 181 |
Counter(std::string title,std::ostream &os=std::cerr) |
182 | 182 |
: _title(title), _os(os), count(0) {} |
183 | 183 |
/// Constructor. |
184 | 184 |
Counter(const char *title,std::ostream &os=std::cerr) |
185 | 185 |
: _title(title), _os(os), count(0) {} |
186 | 186 |
/// Destructor. Prints the given title and the value of the counter. |
187 | 187 |
~Counter() { |
188 | 188 |
_os << _title << count <<std::endl; |
189 | 189 |
} |
190 | 190 |
///\e |
191 | 191 |
Counter &operator++() { count++; return *this;} |
192 | 192 |
///\e |
193 | 193 |
int operator++(int) { return count++;} |
194 | 194 |
///\e |
195 | 195 |
Counter &operator--() { count--; return *this;} |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief Dfs algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/graph_utils.h> |
28 | 28 |
#include <lemon/bits/path_dump.h> |
29 | 29 |
#include <lemon/bits/invalid.h> |
30 | 30 |
#include <lemon/error.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/concept_check.h> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
|
38 | 38 |
///Default traits class of Dfs class. |
39 | 39 |
|
40 | 40 |
///Default traits class of Dfs class. |
41 | 41 |
///\tparam GR Digraph type. |
42 | 42 |
template<class GR> |
43 | 43 |
struct DfsDefaultTraits |
44 | 44 |
{ |
45 | 45 |
///The digraph type the algorithm runs on. |
46 | 46 |
typedef GR Digraph; |
47 | 47 |
///\brief The type of the map that stores the last |
48 | 48 |
///arcs of the %DFS paths. |
49 | 49 |
/// |
50 | 50 |
///The type of the map that stores the last |
51 | 51 |
///arcs of the %DFS paths. |
52 | 52 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
53 | 53 |
/// |
54 | 54 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
55 | 55 |
///Instantiates a PredMap. |
56 | 56 |
|
57 | 57 |
///This function instantiates a \ref PredMap. |
58 | 58 |
///\param G is the digraph, to which we would like to define the PredMap. |
59 | 59 |
///\todo The digraph alone may be insufficient to initialize |
60 | 60 |
static PredMap *createPredMap(const GR &G) |
61 | 61 |
{ |
62 | 62 |
return new PredMap(G); |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
///The type of the map that indicates which nodes are processed. |
66 | 66 |
|
67 | 67 |
///The type of the map that indicates which nodes are processed. |
68 | 68 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
69 | 69 |
///\todo named parameter to set this type, function to read and write. |
70 | 70 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
71 | 71 |
///Instantiates a ProcessedMap. |
72 | 72 |
|
73 | 73 |
///This function instantiates a \ref ProcessedMap. |
74 | 74 |
///\param g is the digraph, to which |
75 | 75 |
///we would like to define the \ref ProcessedMap |
76 | 76 |
#ifdef DOXYGEN |
77 | 77 |
static ProcessedMap *createProcessedMap(const GR &g) |
78 | 78 |
#else |
79 | 79 |
static ProcessedMap *createProcessedMap(const GR &) |
80 | 80 |
#endif |
81 | 81 |
{ |
82 | 82 |
return new ProcessedMap(); |
83 | 83 |
} |
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
|
86 | 86 |
///The type of the map that indicates which nodes are reached. |
87 | 87 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
88 | 88 |
///\todo named parameter to set this type, function to read and write. |
89 | 89 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
90 | 90 |
///Instantiates a ReachedMap. |
91 | 91 |
|
92 | 92 |
///This function instantiates a \ref ReachedMap. |
93 | 93 |
///\param G is the digraph, to which |
94 | 94 |
///we would like to define the \ref ReachedMap. |
95 | 95 |
static ReachedMap *createReachedMap(const GR &G) |
96 | 96 |
{ |
97 | 97 |
return new ReachedMap(G); |
98 | 98 |
} |
99 | 99 |
///The type of the map that stores the dists of the nodes. |
100 | 100 |
|
101 | 101 |
///The type of the map that stores the dists of the nodes. |
102 | 102 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
103 | 103 |
/// |
104 | 104 |
typedef typename Digraph::template NodeMap<int> DistMap; |
105 | 105 |
///Instantiates a DistMap. |
106 | 106 |
|
107 | 107 |
///This function instantiates a \ref DistMap. |
108 | 108 |
///\param G is the digraph, to which we would like to define the \ref DistMap |
109 | 109 |
static DistMap *createDistMap(const GR &G) |
110 | 110 |
{ |
111 | 111 |
return new DistMap(G); |
112 | 112 |
} |
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
///%DFS algorithm class. |
116 | 116 |
|
117 | 117 |
///\ingroup search |
118 | 118 |
///This class provides an efficient implementation of the %DFS algorithm. |
119 | 119 |
/// |
120 | 120 |
///\tparam GR The digraph type the algorithm runs on. The default value is |
121 | 121 |
///\ref ListDigraph. The value of GR is not used directly by Dfs, it |
122 | 122 |
///is only passed to \ref DfsDefaultTraits. |
123 | 123 |
///\tparam TR Traits class to set various data types used by the algorithm. |
124 | 124 |
///The default traits class is |
125 | 125 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
126 | 126 |
///See \ref DfsDefaultTraits for the documentation of |
127 | 127 |
///a Dfs traits class. |
128 | 128 |
#ifdef DOXYGEN |
129 | 129 |
template <typename GR, |
130 | 130 |
typename TR> |
131 | 131 |
#else |
132 | 132 |
template <typename GR=ListDigraph, |
133 | 133 |
typename TR=DfsDefaultTraits<GR> > |
134 | 134 |
#endif |
135 | 135 |
class Dfs { |
136 | 136 |
public: |
137 | 137 |
/** |
138 | 138 |
* \brief \ref Exception for uninitialized parameters. |
139 | 139 |
* |
140 | 140 |
* This error represents problems in the initialization |
141 | 141 |
* of the parameters of the algorithms. |
142 | 142 |
*/ |
143 | 143 |
class UninitializedParameter : public lemon::UninitializedParameter { |
144 | 144 |
public: |
145 | 145 |
virtual const char* what() const throw() { |
146 | 146 |
return "lemon::Dfs::UninitializedParameter"; |
147 | 147 |
} |
148 | 148 |
}; |
149 | 149 |
|
150 | 150 |
typedef TR Traits; |
151 | 151 |
///The type of the underlying digraph. |
152 | 152 |
typedef typename TR::Digraph Digraph; |
153 | 153 |
///\e |
154 | 154 |
typedef typename Digraph::Node Node; |
155 | 155 |
///\e |
156 | 156 |
typedef typename Digraph::NodeIt NodeIt; |
157 | 157 |
///\e |
158 | 158 |
typedef typename Digraph::Arc Arc; |
159 | 159 |
///\e |
160 | 160 |
typedef typename Digraph::OutArcIt OutArcIt; |
161 | 161 |
|
162 | 162 |
///\brief The type of the map that stores the last |
163 | 163 |
///arcs of the %DFS paths. |
164 | 164 |
typedef typename TR::PredMap PredMap; |
165 | 165 |
///The type of the map indicating which nodes are reached. |
166 | 166 |
typedef typename TR::ReachedMap ReachedMap; |
167 | 167 |
///The type of the map indicating which nodes are processed. |
168 | 168 |
typedef typename TR::ProcessedMap ProcessedMap; |
169 | 169 |
///The type of the map that stores the dists of the nodes. |
170 | 170 |
typedef typename TR::DistMap DistMap; |
171 | 171 |
private: |
172 | 172 |
/// Pointer to the underlying digraph. |
173 | 173 |
const Digraph *G; |
174 | 174 |
///Pointer to the map of predecessors arcs. |
175 | 175 |
PredMap *_pred; |
176 | 176 |
///Indicates if \ref _pred is locally allocated (\c true) or not. |
177 | 177 |
bool local_pred; |
178 | 178 |
///Pointer to the map of distances. |
179 | 179 |
DistMap *_dist; |
180 | 180 |
///Indicates if \ref _dist is locally allocated (\c true) or not. |
181 | 181 |
bool local_dist; |
182 | 182 |
///Pointer to the map of reached status of the nodes. |
183 | 183 |
ReachedMap *_reached; |
184 | 184 |
///Indicates if \ref _reached is locally allocated (\c true) or not. |
185 | 185 |
bool local_reached; |
186 | 186 |
///Pointer to the map of processed status of the nodes. |
187 | 187 |
ProcessedMap *_processed; |
188 | 188 |
///Indicates if \ref _processed is locally allocated (\c true) or not. |
189 | 189 |
bool local_processed; |
190 | 190 |
|
191 | 191 |
std::vector<typename Digraph::OutArcIt> _stack; |
192 | 192 |
int _stack_head; |
193 | 193 |
|
194 | 194 |
///Creates the maps if necessary. |
195 | 195 |
|
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_DIJKSTRA_H |
20 | 20 |
#define LEMON_DIJKSTRA_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief Dijkstra algorithm. |
25 | 25 |
|
26 | 26 |
#include <limits> |
27 | 27 |
#include <lemon/list_graph.h> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/bits/path_dump.h> |
30 | 30 |
#include <lemon/bits/invalid.h> |
31 | 31 |
#include <lemon/error.h> |
32 | 32 |
#include <lemon/maps.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \brief Default OperationTraits for the Dijkstra algorithm class. |
37 | 37 |
/// |
38 | 38 |
/// It defines all computational operations and constants which are |
39 | 39 |
/// used in the Dijkstra algorithm. |
40 | 40 |
template <typename Value> |
41 | 41 |
struct DijkstraDefaultOperationTraits { |
42 | 42 |
/// \brief Gives back the zero value of the type. |
43 | 43 |
static Value zero() { |
44 | 44 |
return static_cast<Value>(0); |
45 | 45 |
} |
46 | 46 |
/// \brief Gives back the sum of the given two elements. |
47 | 47 |
static Value plus(const Value& left, const Value& right) { |
48 | 48 |
return left + right; |
49 | 49 |
} |
50 | 50 |
/// \brief Gives back true only if the first value less than the second. |
51 | 51 |
static bool less(const Value& left, const Value& right) { |
52 | 52 |
return left < right; |
53 | 53 |
} |
54 | 54 |
}; |
55 | 55 |
|
56 | 56 |
/// \brief Widest path OperationTraits for the Dijkstra algorithm class. |
57 | 57 |
/// |
58 | 58 |
/// It defines all computational operations and constants which are |
59 | 59 |
/// used in the Dijkstra algorithm for widest path computation. |
60 | 60 |
template <typename Value> |
61 | 61 |
struct DijkstraWidestPathOperationTraits { |
62 | 62 |
/// \brief Gives back the maximum value of the type. |
63 | 63 |
static Value zero() { |
64 | 64 |
return std::numeric_limits<Value>::max(); |
65 | 65 |
} |
66 | 66 |
/// \brief Gives back the minimum of the given two elements. |
67 | 67 |
static Value plus(const Value& left, const Value& right) { |
68 | 68 |
return std::min(left, right); |
69 | 69 |
} |
70 | 70 |
/// \brief Gives back true only if the first value less than the second. |
71 | 71 |
static bool less(const Value& left, const Value& right) { |
72 | 72 |
return left < right; |
73 | 73 |
} |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
///Default traits class of Dijkstra class. |
77 | 77 |
|
78 | 78 |
///Default traits class of Dijkstra class. |
79 | 79 |
///\tparam GR Digraph type. |
80 | 80 |
///\tparam LM Type of length map. |
81 | 81 |
template<class GR, class LM> |
82 | 82 |
struct DijkstraDefaultTraits |
83 | 83 |
{ |
84 | 84 |
///The digraph type the algorithm runs on. |
85 | 85 |
typedef GR Digraph; |
86 | 86 |
///The type of the map that stores the arc lengths. |
87 | 87 |
|
88 | 88 |
///The type of the map that stores the arc lengths. |
89 | 89 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
90 | 90 |
typedef LM LengthMap; |
91 | 91 |
//The type of the length of the arcs. |
92 | 92 |
typedef typename LM::Value Value; |
93 | 93 |
/// Operation traits for Dijkstra algorithm. |
94 | 94 |
|
95 | 95 |
/// It defines the used operation by the algorithm. |
96 | 96 |
/// \see DijkstraDefaultOperationTraits |
97 | 97 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
98 | 98 |
/// The cross reference type used by heap. |
99 | 99 |
|
100 | 100 |
|
101 | 101 |
/// The cross reference type used by heap. |
102 | 102 |
/// Usually it is \c Digraph::NodeMap<int>. |
103 | 103 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
104 | 104 |
///Instantiates a HeapCrossRef. |
105 | 105 |
|
106 | 106 |
///This function instantiates a \c HeapCrossRef. |
107 | 107 |
/// \param G is the digraph, to which we would like to define the |
108 | 108 |
/// HeapCrossRef. |
109 | 109 |
static HeapCrossRef *createHeapCrossRef(const GR &G) |
110 | 110 |
{ |
111 | 111 |
return new HeapCrossRef(G); |
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
///The heap type used by Dijkstra algorithm. |
115 | 115 |
|
116 | 116 |
///The heap type used by Dijkstra algorithm. |
117 | 117 |
/// |
118 | 118 |
///\sa BinHeap |
119 | 119 |
///\sa Dijkstra |
120 | 120 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
121 | 121 |
|
122 | 122 |
static Heap *createHeap(HeapCrossRef& R) |
123 | 123 |
{ |
124 | 124 |
return new Heap(R); |
125 | 125 |
} |
126 | 126 |
|
127 | 127 |
///\brief The type of the map that stores the last |
128 | 128 |
///arcs of the shortest paths. |
129 | 129 |
/// |
130 | 130 |
///The type of the map that stores the last |
131 | 131 |
///arcs of the shortest paths. |
132 | 132 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
133 | 133 |
/// |
134 | 134 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
135 | 135 |
///Instantiates a PredMap. |
136 | 136 |
|
137 | 137 |
///This function instantiates a \c PredMap. |
138 | 138 |
///\param G is the digraph, to which we would like to define the PredMap. |
139 | 139 |
///\todo The digraph alone may be insufficient for the initialization |
140 | 140 |
static PredMap *createPredMap(const GR &G) |
141 | 141 |
{ |
142 | 142 |
return new PredMap(G); |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
///The type of the map that stores whether a nodes is processed. |
146 | 146 |
|
147 | 147 |
///The type of the map that stores whether a nodes is processed. |
148 | 148 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
149 | 149 |
///By default it is a NullMap. |
150 | 150 |
///\todo If it is set to a real map, |
151 | 151 |
///Dijkstra::processed() should read this. |
152 | 152 |
///\todo named parameter to set this type, function to read and write. |
153 | 153 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
154 | 154 |
///Instantiates a ProcessedMap. |
155 | 155 |
|
156 | 156 |
///This function instantiates a \c ProcessedMap. |
157 | 157 |
///\param g is the digraph, to which |
158 | 158 |
///we would like to define the \c ProcessedMap |
159 | 159 |
#ifdef DOXYGEN |
160 | 160 |
static ProcessedMap *createProcessedMap(const GR &g) |
161 | 161 |
#else |
162 | 162 |
static ProcessedMap *createProcessedMap(const GR &) |
163 | 163 |
#endif |
164 | 164 |
{ |
165 | 165 |
return new ProcessedMap(); |
166 | 166 |
} |
167 | 167 |
///The type of the map that stores the dists of the nodes. |
168 | 168 |
|
169 | 169 |
///The type of the map that stores the dists of the nodes. |
170 | 170 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
171 | 171 |
/// |
172 | 172 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
173 | 173 |
///Instantiates a DistMap. |
174 | 174 |
|
175 | 175 |
///This function instantiates a \ref DistMap. |
176 | 176 |
///\param G is the digraph, to which we would like to define the \ref DistMap |
177 | 177 |
static DistMap *createDistMap(const GR &G) |
178 | 178 |
{ |
179 | 179 |
return new DistMap(G); |
180 | 180 |
} |
181 | 181 |
}; |
182 | 182 |
|
183 | 183 |
///%Dijkstra algorithm class. |
184 | 184 |
|
185 | 185 |
/// \ingroup shortest_path |
186 | 186 |
///This class provides an efficient implementation of %Dijkstra algorithm. |
187 | 187 |
///The arc lengths are passed to the algorithm using a |
188 | 188 |
///\ref concepts::ReadMap "ReadMap", |
189 | 189 |
///so it is easy to change it to any kind of length. |
190 | 190 |
/// |
191 | 191 |
///The type of the length is determined by the |
192 | 192 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
193 | 193 |
/// |
194 | 194 |
///It is also possible to change the underlying priority heap. |
195 | 195 |
/// |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_DIM2_H |
20 | 20 |
#define LEMON_DIM2_H |
21 | 21 |
|
22 | 22 |
#include <iostream> |
23 | 23 |
#include <lemon/bits/utility.h> |
24 | 24 |
|
25 | 25 |
///\ingroup misc |
26 | 26 |
///\file |
27 | 27 |
///\brief A simple two dimensional vector and a bounding box implementation |
28 | 28 |
/// |
29 | 29 |
/// The class \ref lemon::dim2::Point "dim2::Point" implements |
30 | 30 |
/// a two dimensional vector with the usual operations. |
31 | 31 |
/// |
32 | 32 |
/// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox" |
33 | 33 |
/// can be used to determine |
34 | 34 |
/// the rectangular bounding box of a set of |
35 | 35 |
/// \ref lemon::dim2::Point "dim2::Point"'s. |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
///Tools for handling two dimensional coordinates |
40 | 40 |
|
41 | 41 |
///This namespace is a storage of several |
42 | 42 |
///tools for handling two dimensional coordinates |
43 | 43 |
namespace dim2 { |
44 | 44 |
|
45 | 45 |
/// \addtogroup misc |
46 | 46 |
/// @{ |
47 | 47 |
|
48 | 48 |
/// A simple two dimensional vector (plainvector) implementation |
49 | 49 |
|
50 | 50 |
/// A simple two dimensional vector (plainvector) implementation |
51 | 51 |
/// with the usual vector operations. |
52 | 52 |
template<typename T> |
53 | 53 |
class Point { |
54 | 54 |
|
55 | 55 |
public: |
56 | 56 |
|
57 | 57 |
typedef T Value; |
58 | 58 |
|
59 | 59 |
///First coordinate |
60 | 60 |
T x; |
61 | 61 |
///Second coordinate |
62 | 62 |
T y; |
63 | 63 |
|
64 | 64 |
///Default constructor |
65 | 65 |
Point() {} |
66 | 66 |
|
67 | 67 |
///Construct an instance from coordinates |
68 | 68 |
Point(T a, T b) : x(a), y(b) { } |
69 | 69 |
|
70 | 70 |
///Returns the dimension of the vector (i.e. returns 2). |
71 | 71 |
|
72 | 72 |
///The dimension of the vector. |
73 | 73 |
///This function always returns 2. |
74 | 74 |
int size() const { return 2; } |
75 | 75 |
|
76 | 76 |
///Subscripting operator |
77 | 77 |
|
78 | 78 |
///\c p[0] is \c p.x and \c p[1] is \c p.y |
79 | 79 |
/// |
80 | 80 |
T& operator[](int idx) { return idx == 0 ? x : y; } |
81 | 81 |
|
82 | 82 |
///Const subscripting operator |
83 | 83 |
|
84 | 84 |
///\c p[0] is \c p.x and \c p[1] is \c p.y |
85 | 85 |
/// |
86 | 86 |
const T& operator[](int idx) const { return idx == 0 ? x : y; } |
87 | 87 |
|
88 | 88 |
///Conversion constructor |
89 | 89 |
template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {} |
90 | 90 |
|
91 | 91 |
///Give back the square of the norm of the vector |
92 | 92 |
T normSquare() const { |
93 | 93 |
return x*x+y*y; |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
///Increment the left hand side by \c u |
97 | 97 |
Point<T>& operator +=(const Point<T>& u) { |
98 | 98 |
x += u.x; |
99 | 99 |
y += u.y; |
100 | 100 |
return *this; |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
///Decrement the left hand side by \c u |
104 | 104 |
Point<T>& operator -=(const Point<T>& u) { |
105 | 105 |
x -= u.x; |
106 | 106 |
y -= u.y; |
107 | 107 |
return *this; |
108 | 108 |
} |
109 | 109 |
|
110 | 110 |
///Multiply the left hand side with a scalar |
111 | 111 |
Point<T>& operator *=(const T &u) { |
112 | 112 |
x *= u; |
113 | 113 |
y *= u; |
114 | 114 |
return *this; |
115 | 115 |
} |
116 | 116 |
|
117 | 117 |
///Divide the left hand side by a scalar |
118 | 118 |
Point<T>& operator /=(const T &u) { |
119 | 119 |
x /= u; |
120 | 120 |
y /= u; |
121 | 121 |
return *this; |
122 | 122 |
} |
123 | 123 |
|
124 | 124 |
///Return the scalar product of two vectors |
125 | 125 |
T operator *(const Point<T>& u) const { |
126 | 126 |
return x*u.x+y*u.y; |
127 | 127 |
} |
128 | 128 |
|
129 | 129 |
///Return the sum of two vectors |
130 | 130 |
Point<T> operator+(const Point<T> &u) const { |
131 | 131 |
Point<T> b=*this; |
132 | 132 |
return b+=u; |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
///Return the negative of the vector |
136 | 136 |
Point<T> operator-() const { |
137 | 137 |
Point<T> b=*this; |
138 | 138 |
b.x=-b.x; b.y=-b.y; |
139 | 139 |
return b; |
140 | 140 |
} |
141 | 141 |
|
142 | 142 |
///Return the difference of two vectors |
143 | 143 |
Point<T> operator-(const Point<T> &u) const { |
144 | 144 |
Point<T> b=*this; |
145 | 145 |
return b-=u; |
146 | 146 |
} |
147 | 147 |
|
148 | 148 |
///Return a vector multiplied by a scalar |
149 | 149 |
Point<T> operator*(const T &u) const { |
150 | 150 |
Point<T> b=*this; |
151 | 151 |
return b*=u; |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
///Return a vector divided by a scalar |
155 | 155 |
Point<T> operator/(const T &u) const { |
156 | 156 |
Point<T> b=*this; |
157 | 157 |
return b/=u; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
///Test equality |
161 | 161 |
bool operator==(const Point<T> &u) const { |
162 | 162 |
return (x==u.x) && (y==u.y); |
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
///Test inequality |
166 | 166 |
bool operator!=(Point u) const { |
167 | 167 |
return (x!=u.x) || (y!=u.y); |
168 | 168 |
} |
169 | 169 |
|
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
///Return a Point |
173 | 173 |
|
174 | 174 |
///Return a Point. |
175 | 175 |
///\relates Point |
176 | 176 |
template <typename T> |
177 | 177 |
inline Point<T> makePoint(const T& x, const T& y) { |
178 | 178 |
return Point<T>(x, y); |
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
///Return a vector multiplied by a scalar |
182 | 182 |
|
183 | 183 |
///Return a vector multiplied by a scalar. |
184 | 184 |
///\relates Point |
185 | 185 |
template<typename T> Point<T> operator*(const T &u,const Point<T> &x) { |
186 | 186 |
return x*u; |
187 | 187 |
} |
188 | 188 |
|
189 | 189 |
///Read a plainvector from a stream |
190 | 190 |
|
191 | 191 |
///Read a plainvector from a stream. |
192 | 192 |
///\relates Point |
193 | 193 |
/// |
194 | 194 |
template<typename T> |
195 | 195 |
inline std::istream& operator>>(std::istream &is, Point<T> &z) { |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_ERROR_H |
20 | 20 |
#define LEMON_ERROR_H |
21 | 21 |
|
22 | 22 |
/// \ingroup exceptions |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Basic exception classes and error handling. |
25 | 25 |
|
26 | 26 |
#include <exception> |
27 | 27 |
#include <string> |
28 | 28 |
#include <sstream> |
29 | 29 |
#include <iostream> |
30 | 30 |
#include <cstdlib> |
31 | 31 |
#include <memory> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
/// \addtogroup exceptions |
36 | 36 |
/// @{ |
37 | 37 |
|
38 | 38 |
/// \brief Exception safe wrapper class. |
39 | 39 |
/// |
40 | 40 |
/// Exception safe wrapper class to implement the members of exceptions. |
41 | 41 |
template <typename _Type> |
42 | 42 |
class ExceptionMember { |
43 | 43 |
public: |
44 | 44 |
typedef _Type Type; |
45 | 45 |
|
46 | 46 |
ExceptionMember() throw() { |
47 | 47 |
try { |
48 | 48 |
ptr.reset(new Type()); |
49 | 49 |
} catch (...) {} |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
ExceptionMember(const Type& type) throw() { |
53 | 53 |
try { |
54 | 54 |
ptr.reset(new Type()); |
55 | 55 |
if (ptr.get() == 0) return; |
56 | 56 |
*ptr = type; |
57 | 57 |
} catch (...) {} |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
ExceptionMember(const ExceptionMember& copy) throw() { |
61 | 61 |
try { |
62 | 62 |
if (!copy.valid()) return; |
63 | 63 |
ptr.reset(new Type()); |
64 | 64 |
if (ptr.get() == 0) return; |
65 | 65 |
*ptr = copy.get(); |
66 | 66 |
} catch (...) {} |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
ExceptionMember& operator=(const ExceptionMember& copy) throw() { |
70 | 70 |
if (ptr.get() == 0) return; |
71 | 71 |
try { |
72 | 72 |
if (!copy.valid()) return; |
73 | 73 |
*ptr = copy.get(); |
74 | 74 |
} catch (...) {} |
75 | 75 |
} |
76 | 76 |
|
77 | 77 |
void set(const Type& type) throw() { |
78 | 78 |
if (ptr.get() == 0) return; |
79 | 79 |
try { |
80 | 80 |
*ptr = type; |
81 | 81 |
} catch (...) {} |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
const Type& get() const { |
85 | 85 |
return *ptr; |
86 | 86 |
} |
87 | 87 |
|
88 | 88 |
bool valid() const throw() { |
89 | 89 |
return ptr.get() != 0; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
private: |
93 | 93 |
std::auto_ptr<_Type> ptr; |
94 | 94 |
}; |
95 | 95 |
|
96 | 96 |
/// Exception-safe convenient error message builder class. |
97 | 97 |
|
98 | 98 |
/// Helper class which provides a convenient ostream-like (operator << |
99 | 99 |
/// based) interface to create a string message. Mostly useful in |
100 | 100 |
/// exception classes (therefore the name). |
101 | 101 |
class ErrorMessage { |
102 | 102 |
protected: |
103 | 103 |
///\e |
104 | 104 |
|
105 | 105 |
///\todo The good solution is boost::shared_ptr... |
106 | 106 |
/// |
107 | 107 |
mutable std::auto_ptr<std::ostringstream> buf; |
108 | 108 |
|
109 | 109 |
///\e |
110 | 110 |
bool init() throw() { |
111 | 111 |
try { |
112 | 112 |
buf.reset(new std::ostringstream); |
113 | 113 |
} |
114 | 114 |
catch(...) { |
115 | 115 |
buf.reset(); |
116 | 116 |
} |
117 | 117 |
return buf.get(); |
118 | 118 |
} |
119 | 119 |
|
120 | 120 |
public: |
121 | 121 |
|
122 | 122 |
///\e |
123 | 123 |
ErrorMessage() throw() { init(); } |
124 | 124 |
|
125 | 125 |
ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { } |
126 | 126 |
|
127 | 127 |
///\e |
128 | 128 |
ErrorMessage(const char *msg) throw() { |
129 | 129 |
init(); |
130 | 130 |
*this << msg; |
131 | 131 |
} |
132 | 132 |
|
133 | 133 |
///\e |
134 | 134 |
ErrorMessage(const std::string &msg) throw() { |
135 | 135 |
init(); |
136 | 136 |
*this << msg; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
///\e |
140 | 140 |
template <typename T> |
141 | 141 |
ErrorMessage& operator<<(const T &t) throw() { |
142 | 142 |
if( ! buf.get() ) return *this; |
143 | 143 |
|
144 | 144 |
try { |
145 | 145 |
*buf << t; |
146 | 146 |
} |
147 | 147 |
catch(...) { |
148 | 148 |
buf.reset(); |
149 | 149 |
} |
150 | 150 |
return *this; |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
///\e |
154 | 154 |
const char* message() throw() { |
155 | 155 |
if( ! buf.get() ) return 0; |
156 | 156 |
|
157 | 157 |
const char* mes = 0; |
158 | 158 |
try { |
159 | 159 |
mes = buf->str().c_str(); |
160 | 160 |
} |
161 | 161 |
catch(...) {} |
162 | 162 |
return mes; |
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
}; |
166 | 166 |
|
167 | 167 |
/// Generic exception class. |
168 | 168 |
|
169 | 169 |
/// Base class for exceptions used in LEMON. |
170 | 170 |
/// |
171 | 171 |
class Exception : public std::exception { |
172 | 172 |
public: |
173 | 173 |
///\e |
174 | 174 |
Exception() {} |
175 | 175 |
///\e |
176 | 176 |
virtual ~Exception() throw() {} |
177 | 177 |
///\e |
178 | 178 |
virtual const char* what() const throw() { |
179 | 179 |
return "lemon::Exception"; |
180 | 180 |
} |
181 | 181 |
}; |
182 | 182 |
|
183 | 183 |
/// One of the two main subclasses of \ref Exception. |
184 | 184 |
|
185 | 185 |
/// Logic errors represent problems in the internal logic of a program; |
186 | 186 |
/// in theory, these are preventable, and even detectable before the |
187 | 187 |
/// program runs (e.g. violations of class invariants). |
188 | 188 |
/// |
189 | 189 |
/// A typical example for this is \ref UninitializedParameter. |
190 | 190 |
class LogicError : public Exception { |
191 | 191 |
public: |
192 | 192 |
virtual const char* what() const throw() { |
193 | 193 |
return "lemon::LogicError"; |
194 | 194 |
} |
195 | 195 |
}; |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<fstream> |
24 | 24 |
#include<sstream> |
25 | 25 |
#include<algorithm> |
26 | 26 |
#include<vector> |
27 | 27 |
|
28 | 28 |
#ifndef WIN32 |
29 | 29 |
#include<sys/time.h> |
30 | 30 |
#include<ctime> |
31 | 31 |
#else |
32 | 32 |
#define WIN32_LEAN_AND_MEAN |
33 | 33 |
#define NOMINMAX |
34 | 34 |
#include<windows.h> |
35 | 35 |
#endif |
36 | 36 |
|
37 | 37 |
#include<lemon/math.h> |
38 | 38 |
#include<lemon/bits/invalid.h> |
39 | 39 |
#include<lemon/dim2.h> |
40 | 40 |
#include<lemon/maps.h> |
41 | 41 |
#include<lemon/color.h> |
42 | 42 |
#include<lemon/bits/bezier.h> |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
///\ingroup eps_io |
46 | 46 |
///\file |
47 | 47 |
///\brief A well configurable tool for visualizing graphs |
48 | 48 |
|
49 | 49 |
namespace lemon { |
50 | 50 |
|
51 | 51 |
namespace _graph_to_eps_bits { |
52 | 52 |
template<class MT> |
53 | 53 |
class _NegY { |
54 | 54 |
public: |
55 | 55 |
typedef typename MT::Key Key; |
56 | 56 |
typedef typename MT::Value Value; |
57 | 57 |
const MT ↦ |
58 | 58 |
int yscale; |
59 | 59 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
60 | 60 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
61 | 61 |
}; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
///Default traits class of \ref GraphToEps |
65 | 65 |
|
66 | 66 |
///Default traits class of \ref GraphToEps. |
67 | 67 |
/// |
68 | 68 |
///\c G is the type of the underlying graph. |
69 | 69 |
template<class G> |
70 | 70 |
struct DefaultGraphToEpsTraits |
71 | 71 |
{ |
72 | 72 |
typedef G Graph; |
73 | 73 |
typedef typename Graph::Node Node; |
74 | 74 |
typedef typename Graph::NodeIt NodeIt; |
75 | 75 |
typedef typename Graph::Arc Arc; |
76 | 76 |
typedef typename Graph::ArcIt ArcIt; |
77 | 77 |
typedef typename Graph::InArcIt InArcIt; |
78 | 78 |
typedef typename Graph::OutArcIt OutArcIt; |
79 | 79 |
|
80 | 80 |
|
81 | 81 |
const Graph &g; |
82 | 82 |
|
83 | 83 |
std::ostream& os; |
84 | 84 |
|
85 | 85 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
86 | 86 |
CoordsMapType _coords; |
87 | 87 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
88 | 88 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
89 | 89 |
|
90 | 90 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
91 | 91 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
92 | 92 |
|
93 | 93 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
94 | 94 |
|
95 | 95 |
double _arcWidthScale; |
96 | 96 |
|
97 | 97 |
double _nodeScale; |
98 | 98 |
double _xBorder, _yBorder; |
99 | 99 |
double _scale; |
100 | 100 |
double _nodeBorderQuotient; |
101 | 101 |
|
102 | 102 |
bool _drawArrows; |
103 | 103 |
double _arrowLength, _arrowWidth; |
104 | 104 |
|
105 | 105 |
bool _showNodes, _showArcs; |
106 | 106 |
|
107 | 107 |
bool _enableParallel; |
108 | 108 |
double _parArcDist; |
109 | 109 |
|
110 | 110 |
bool _showNodeText; |
111 | 111 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
112 | 112 |
double _nodeTextSize; |
113 | 113 |
|
114 | 114 |
bool _showNodePsText; |
115 | 115 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
116 | 116 |
char *_nodePsTextsPreamble; |
117 | 117 |
|
118 | 118 |
bool _undirected; |
119 | 119 |
|
120 | 120 |
bool _pleaseRemoveOsStream; |
121 | 121 |
|
122 | 122 |
bool _scaleToA4; |
123 | 123 |
|
124 | 124 |
std::string _title; |
125 | 125 |
std::string _copyright; |
126 | 126 |
|
127 | 127 |
enum NodeTextColorType |
128 | 128 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
129 | 129 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
130 | 130 |
|
131 | 131 |
bool _autoNodeScale; |
132 | 132 |
bool _autoArcWidthScale; |
133 | 133 |
|
134 | 134 |
bool _absoluteNodeSizes; |
135 | 135 |
bool _absoluteArcWidths; |
136 | 136 |
|
137 | 137 |
bool _negY; |
138 | 138 |
|
139 | 139 |
bool _preScale; |
140 | 140 |
///Constructor |
141 | 141 |
|
142 | 142 |
///Constructor |
143 | 143 |
///\param _g Reference to the graph to be printed. |
144 | 144 |
///\param _os Reference to the output stream. |
145 | 145 |
///\param _os Reference to the output stream. By default it is <tt>std::cout</tt>. |
146 | 146 |
///\param _pros If it is \c true, then the \c ostream referenced by \c _os |
147 | 147 |
///will be explicitly deallocated by the destructor. |
148 | 148 |
DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
149 | 149 |
bool _pros=false) : |
150 | 150 |
g(_g), os(_os), |
151 | 151 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
152 | 152 |
_nodeColors(WHITE), _arcColors(BLACK), |
153 | 153 |
_arcWidths(1.0), _arcWidthScale(0.003), |
154 | 154 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
155 | 155 |
_nodeBorderQuotient(.1), |
156 | 156 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
157 | 157 |
_showNodes(true), _showArcs(true), |
158 | 158 |
_enableParallel(false), _parArcDist(1), |
159 | 159 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
160 | 160 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
161 | 161 |
_undirected(lemon::UndirectedTagIndicator<G>::value), |
162 | 162 |
_pleaseRemoveOsStream(_pros), _scaleToA4(false), |
163 | 163 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
164 | 164 |
_autoNodeScale(false), |
165 | 165 |
_autoArcWidthScale(false), |
166 | 166 |
_absoluteNodeSizes(false), |
167 | 167 |
_absoluteArcWidths(false), |
168 | 168 |
_negY(false), |
169 | 169 |
_preScale(true) |
170 | 170 |
{} |
171 | 171 |
}; |
172 | 172 |
|
173 | 173 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
174 | 174 |
|
175 | 175 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
176 | 176 |
/// |
177 | 177 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
178 | 178 |
template<class T> class GraphToEps : public T |
179 | 179 |
{ |
180 | 180 |
// Can't believe it is required by the C++ standard |
181 | 181 |
using T::g; |
182 | 182 |
using T::os; |
183 | 183 |
|
184 | 184 |
using T::_coords; |
185 | 185 |
using T::_nodeSizes; |
186 | 186 |
using T::_nodeShapes; |
187 | 187 |
using T::_nodeColors; |
188 | 188 |
using T::_arcColors; |
189 | 189 |
using T::_arcWidths; |
190 | 190 |
|
191 | 191 |
using T::_arcWidthScale; |
192 | 192 |
using T::_nodeScale; |
193 | 193 |
using T::_xBorder; |
194 | 194 |
using T::_yBorder; |
195 | 195 |
using T::_scale; |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_GRAPH_UTILS_H |
20 | 20 |
#define LEMON_GRAPH_UTILS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <vector> |
24 | 24 |
#include <map> |
25 | 25 |
#include <cmath> |
26 | 26 |
#include <algorithm> |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/invalid.h> |
29 | 29 |
#include <lemon/bits/utility.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/bits/traits.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/bits/alteration_notifier.h> |
34 | 34 |
#include <lemon/bits/default_map.h> |
35 | 35 |
|
36 | 36 |
///\ingroup gutils |
37 | 37 |
///\file |
38 | 38 |
///\brief Graph utilities. |
39 | 39 |
|
40 | 40 |
namespace lemon { |
41 | 41 |
|
42 | 42 |
/// \addtogroup gutils |
43 | 43 |
/// @{ |
44 | 44 |
|
45 | 45 |
///Creates convenience typedefs for the digraph types and iterators |
46 | 46 |
|
47 | 47 |
///This \c \#define creates convenience typedefs for the following types |
48 | 48 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
49 | 49 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
50 | 50 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
51 | 51 |
/// |
52 | 52 |
///\note If the graph type is a dependent type, ie. the graph type depend |
53 | 53 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
54 | 54 |
///macro. |
55 | 55 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
56 | 56 |
typedef Digraph::Node Node; \ |
57 | 57 |
typedef Digraph::NodeIt NodeIt; \ |
58 | 58 |
typedef Digraph::Arc Arc; \ |
59 | 59 |
typedef Digraph::ArcIt ArcIt; \ |
60 | 60 |
typedef Digraph::InArcIt InArcIt; \ |
61 | 61 |
typedef Digraph::OutArcIt OutArcIt; \ |
62 | 62 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
63 | 63 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
64 | 64 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
65 | 65 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
66 | 66 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
67 | 67 |
typedef Digraph::ArcMap<double> DoubleArcMap |
68 | 68 |
|
69 | 69 |
///Creates convenience typedefs for the digraph types and iterators |
70 | 70 |
|
71 | 71 |
///\see DIGRAPH_TYPEDEFS |
72 | 72 |
/// |
73 | 73 |
///\note Use this macro, if the graph type is a dependent type, |
74 | 74 |
///ie. the graph type depend on a template parameter. |
75 | 75 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
76 | 76 |
typedef typename Digraph::Node Node; \ |
77 | 77 |
typedef typename Digraph::NodeIt NodeIt; \ |
78 | 78 |
typedef typename Digraph::Arc Arc; \ |
79 | 79 |
typedef typename Digraph::ArcIt ArcIt; \ |
80 | 80 |
typedef typename Digraph::InArcIt InArcIt; \ |
81 | 81 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
82 | 82 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
83 | 83 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
84 | 84 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
85 | 85 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
86 | 86 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
87 | 87 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
88 | 88 |
|
89 | 89 |
///Creates convenience typedefs for the graph types and iterators |
90 | 90 |
|
91 | 91 |
///This \c \#define creates the same convenience typedefs as defined |
92 | 92 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
93 | 93 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
94 | 94 |
///\c DoubleEdgeMap. |
95 | 95 |
/// |
96 | 96 |
///\note If the graph type is a dependent type, ie. the graph type depend |
97 | 97 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
98 | 98 |
///macro. |
99 | 99 |
#define GRAPH_TYPEDEFS(Graph) \ |
100 | 100 |
DIGRAPH_TYPEDEFS(Graph); \ |
101 | 101 |
typedef Graph::Edge Edge; \ |
102 | 102 |
typedef Graph::EdgeIt EdgeIt; \ |
103 | 103 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
104 | 104 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
105 | 105 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
106 | 106 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
107 | 107 |
|
108 | 108 |
///Creates convenience typedefs for the graph types and iterators |
109 | 109 |
|
110 | 110 |
///\see GRAPH_TYPEDEFS |
111 | 111 |
/// |
112 | 112 |
///\note Use this macro, if the graph type is a dependent type, |
113 | 113 |
///ie. the graph type depend on a template parameter. |
114 | 114 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
115 | 115 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
116 | 116 |
typedef typename Graph::Edge Edge; \ |
117 | 117 |
typedef typename Graph::EdgeIt EdgeIt; \ |
118 | 118 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
119 | 119 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
120 | 120 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
121 | 121 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
122 | 122 |
|
123 | 123 |
/// \brief Function to count the items in the graph. |
124 | 124 |
/// |
125 | 125 |
/// This function counts the items (nodes, arcs etc) in the graph. |
126 | 126 |
/// The complexity of the function is O(n) because |
127 | 127 |
/// it iterates on all of the items. |
128 | 128 |
template <typename Graph, typename Item> |
129 | 129 |
inline int countItems(const Graph& g) { |
130 | 130 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
131 | 131 |
int num = 0; |
132 | 132 |
for (ItemIt it(g); it != INVALID; ++it) { |
133 | 133 |
++num; |
134 | 134 |
} |
135 | 135 |
return num; |
136 | 136 |
} |
137 | 137 |
|
138 | 138 |
// Node counting: |
139 | 139 |
|
140 | 140 |
namespace _graph_utils_bits { |
141 | 141 |
|
142 | 142 |
template <typename Graph, typename Enable = void> |
143 | 143 |
struct CountNodesSelector { |
144 | 144 |
static int count(const Graph &g) { |
145 | 145 |
return countItems<Graph, typename Graph::Node>(g); |
146 | 146 |
} |
147 | 147 |
}; |
148 | 148 |
|
149 | 149 |
template <typename Graph> |
150 | 150 |
struct CountNodesSelector< |
151 | 151 |
Graph, typename |
152 | 152 |
enable_if<typename Graph::NodeNumTag, void>::type> |
153 | 153 |
{ |
154 | 154 |
static int count(const Graph &g) { |
155 | 155 |
return g.nodeNum(); |
156 | 156 |
} |
157 | 157 |
}; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
/// \brief Function to count the nodes in the graph. |
161 | 161 |
/// |
162 | 162 |
/// This function counts the nodes in the graph. |
163 | 163 |
/// The complexity of the function is O(n) but for some |
164 | 164 |
/// graph structures it is specialized to run in O(1). |
165 | 165 |
/// |
166 | 166 |
/// If the graph contains a \e nodeNum() member function and a |
167 | 167 |
/// \e NodeNumTag tag then this function calls directly the member |
168 | 168 |
/// function to query the cardinality of the node set. |
169 | 169 |
template <typename Graph> |
170 | 170 |
inline int countNodes(const Graph& g) { |
171 | 171 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g); |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
// Arc counting: |
175 | 175 |
|
176 | 176 |
namespace _graph_utils_bits { |
177 | 177 |
|
178 | 178 |
template <typename Graph, typename Enable = void> |
179 | 179 |
struct CountArcsSelector { |
180 | 180 |
static int count(const Graph &g) { |
181 | 181 |
return countItems<Graph, typename Graph::Arc>(g); |
182 | 182 |
} |
183 | 183 |
}; |
184 | 184 |
|
185 | 185 |
template <typename Graph> |
186 | 186 |
struct CountArcsSelector< |
187 | 187 |
Graph, |
188 | 188 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
189 | 189 |
{ |
190 | 190 |
static int count(const Graph &g) { |
191 | 191 |
return g.arcNum(); |
192 | 192 |
} |
193 | 193 |
}; |
194 | 194 |
} |
195 | 195 |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_KRUSKAL_H |
20 | 20 |
#define LEMON_KRUSKAL_H |
21 | 21 |
|
22 | 22 |
#include <algorithm> |
23 | 23 |
#include <vector> |
24 | 24 |
#include <lemon/unionfind.h> |
25 | 25 |
// #include <lemon/graph_utils.h> |
26 | 26 |
#include <lemon/maps.h> |
27 | 27 |
|
28 | 28 |
// #include <lemon/radix_sort.h> |
29 | 29 |
|
30 | 30 |
#include <lemon/bits/utility.h> |
31 | 31 |
#include <lemon/bits/traits.h> |
32 | 32 |
|
33 | 33 |
///\ingroup spantree |
34 | 34 |
///\file |
35 | 35 |
///\brief Kruskal's algorithm to compute a minimum cost spanning tree |
36 | 36 |
/// |
37 | 37 |
///Kruskal's algorithm to compute a minimum cost spanning tree. |
38 | 38 |
/// |
39 | 39 |
|
40 | 40 |
namespace lemon { |
41 | 41 |
|
42 | 42 |
namespace _kruskal_bits { |
43 | 43 |
|
44 | 44 |
// Kruskal for directed graphs. |
45 | 45 |
|
46 | 46 |
template <typename Digraph, typename In, typename Out> |
47 | 47 |
typename disable_if<lemon::UndirectedTagIndicator<Digraph>, |
48 | 48 |
typename In::value_type::second_type >::type |
49 | 49 |
kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) { |
50 | 50 |
typedef typename In::value_type::second_type Value; |
51 | 51 |
typedef typename Digraph::template NodeMap<int> IndexMap; |
52 | 52 |
typedef typename Digraph::Node Node; |
53 | 53 |
|
54 | 54 |
IndexMap index(digraph); |
55 | 55 |
UnionFind<IndexMap> uf(index); |
56 | 56 |
for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
57 | 57 |
uf.insert(it); |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
Value tree_value = 0; |
61 | 61 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { |
62 | 62 |
if (uf.join(digraph.target(it->first),digraph.source(it->first))) { |
63 | 63 |
out.set(it->first, true); |
64 | 64 |
tree_value += it->second; |
65 | 65 |
} |
66 | 66 |
else { |
67 | 67 |
out.set(it->first, false); |
68 | 68 |
} |
69 | 69 |
} |
70 | 70 |
return tree_value; |
71 | 71 |
} |
72 | 72 |
|
73 | 73 |
// Kruskal for undirected graphs. |
74 | 74 |
|
75 | 75 |
template <typename Graph, typename In, typename Out> |
76 | 76 |
typename enable_if<lemon::UndirectedTagIndicator<Graph>, |
77 | 77 |
typename In::value_type::second_type >::type |
78 | 78 |
kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) { |
79 | 79 |
typedef typename In::value_type::second_type Value; |
80 | 80 |
typedef typename Graph::template NodeMap<int> IndexMap; |
81 | 81 |
typedef typename Graph::Node Node; |
82 | 82 |
|
83 | 83 |
IndexMap index(graph); |
84 | 84 |
UnionFind<IndexMap> uf(index); |
85 | 85 |
for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { |
86 | 86 |
uf.insert(it); |
87 | 87 |
} |
88 | 88 |
|
89 | 89 |
Value tree_value = 0; |
90 | 90 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { |
91 | 91 |
if (uf.join(graph.u(it->first),graph.v(it->first))) { |
92 | 92 |
out.set(it->first, true); |
93 | 93 |
tree_value += it->second; |
94 | 94 |
} |
95 | 95 |
else { |
96 | 96 |
out.set(it->first, false); |
97 | 97 |
} |
98 | 98 |
} |
99 | 99 |
return tree_value; |
100 | 100 |
} |
101 | 101 |
|
102 | 102 |
|
103 | 103 |
template <typename Sequence> |
104 | 104 |
struct PairComp { |
105 | 105 |
typedef typename Sequence::value_type Value; |
106 | 106 |
bool operator()(const Value& left, const Value& right) { |
107 | 107 |
return left.second < right.second; |
108 | 108 |
} |
109 | 109 |
}; |
110 | 110 |
|
111 | 111 |
template <typename In, typename Enable = void> |
112 | 112 |
struct SequenceInputIndicator { |
113 | 113 |
static const bool value = false; |
114 | 114 |
}; |
115 | 115 |
|
116 | 116 |
template <typename In> |
117 | 117 |
struct SequenceInputIndicator<In, |
118 | 118 |
typename exists<typename In::value_type::first_type>::type> { |
119 | 119 |
static const bool value = true; |
120 | 120 |
}; |
121 | 121 |
|
122 | 122 |
template <typename In, typename Enable = void> |
123 | 123 |
struct MapInputIndicator { |
124 | 124 |
static const bool value = false; |
125 | 125 |
}; |
126 | 126 |
|
127 | 127 |
template <typename In> |
128 | 128 |
struct MapInputIndicator<In, |
129 | 129 |
typename exists<typename In::Value>::type> { |
130 | 130 |
static const bool value = true; |
131 | 131 |
}; |
132 | 132 |
|
133 | 133 |
template <typename In, typename Enable = void> |
134 | 134 |
struct SequenceOutputIndicator { |
135 | 135 |
static const bool value = false; |
136 | 136 |
}; |
137 | 137 |
|
138 | 138 |
template <typename Out> |
139 | 139 |
struct SequenceOutputIndicator<Out, |
140 | 140 |
typename exists<typename Out::value_type>::type> { |
141 | 141 |
static const bool value = true; |
142 | 142 |
}; |
143 | 143 |
|
144 | 144 |
template <typename Out, typename Enable = void> |
145 | 145 |
struct MapOutputIndicator { |
146 | 146 |
static const bool value = false; |
147 | 147 |
}; |
148 | 148 |
|
149 | 149 |
template <typename Out> |
150 | 150 |
struct MapOutputIndicator<Out, |
151 | 151 |
typename exists<typename Out::Value>::type> { |
152 | 152 |
static const bool value = true; |
153 | 153 |
}; |
154 | 154 |
|
155 | 155 |
template <typename In, typename InEnable = void> |
156 | 156 |
struct KruskalValueSelector {}; |
157 | 157 |
|
158 | 158 |
template <typename In> |
159 | 159 |
struct KruskalValueSelector<In, |
160 | 160 |
typename enable_if<SequenceInputIndicator<In>, void>::type> |
161 | 161 |
{ |
162 | 162 |
typedef typename In::value_type::second_type Value; |
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
template <typename In> |
166 | 166 |
struct KruskalValueSelector<In, |
167 | 167 |
typename enable_if<MapInputIndicator<In>, void>::type> |
168 | 168 |
{ |
169 | 169 |
typedef typename In::Value Value; |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
template <typename Graph, typename In, typename Out, |
173 | 173 |
typename InEnable = void> |
174 | 174 |
struct KruskalInputSelector {}; |
175 | 175 |
|
176 | 176 |
template <typename Graph, typename In, typename Out, |
177 | 177 |
typename InEnable = void> |
178 | 178 |
struct KruskalOutputSelector {}; |
179 | 179 |
|
180 | 180 |
template <typename Graph, typename In, typename Out> |
181 | 181 |
struct KruskalInputSelector<Graph, In, Out, |
182 | 182 |
typename enable_if<SequenceInputIndicator<In>, void>::type > |
183 | 183 |
{ |
184 | 184 |
typedef typename In::value_type::second_type Value; |
185 | 185 |
|
186 | 186 |
static Value kruskal(const Graph& graph, const In& in, Out& out) { |
187 | 187 |
return KruskalOutputSelector<Graph, In, Out>:: |
188 | 188 |
kruskal(graph, in, out); |
189 | 189 |
} |
190 | 190 |
|
191 | 191 |
}; |
192 | 192 |
|
193 | 193 |
template <typename Graph, typename In, typename Out> |
194 | 194 |
struct KruskalInputSelector<Graph, In, Out, |
195 | 195 |
typename enable_if<MapInputIndicator<In>, void>::type > |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup lemon_io |
20 | 20 |
///\file |
21 | 21 |
///\brief \ref lgf-format "Lemon Graph Format" reader. |
22 | 22 |
|
23 | 23 |
|
24 | 24 |
#ifndef LEMON_LGF_READER_H |
25 | 25 |
#define LEMON_LGF_READER_H |
26 | 26 |
|
27 | 27 |
#include <iostream> |
28 | 28 |
#include <fstream> |
29 | 29 |
#include <sstream> |
30 | 30 |
|
31 | 31 |
#include <set> |
32 | 32 |
#include <map> |
33 | 33 |
|
34 | 34 |
#include <lemon/assert.h> |
35 | 35 |
#include <lemon/graph_utils.h> |
36 | 36 |
|
37 | 37 |
#include <lemon/lgf_writer.h> |
38 | 38 |
|
39 | 39 |
#include <lemon/concept_check.h> |
40 | 40 |
#include <lemon/concepts/maps.h> |
41 | 41 |
|
42 | 42 |
namespace lemon { |
43 | 43 |
|
44 | 44 |
namespace _reader_bits { |
45 | 45 |
|
46 | 46 |
template <typename Value> |
47 | 47 |
struct DefaultConverter { |
48 | 48 |
Value operator()(const std::string& str) { |
49 | 49 |
std::istringstream is(str); |
50 | 50 |
Value value; |
51 | 51 |
is >> value; |
52 | 52 |
|
53 | 53 |
char c; |
54 | 54 |
if (is >> std::ws >> c) { |
55 | 55 |
throw DataFormatError("Remaining characters in token"); |
56 | 56 |
} |
57 | 57 |
return value; |
58 | 58 |
} |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
template <> |
62 | 62 |
struct DefaultConverter<std::string> { |
63 | 63 |
std::string operator()(const std::string& str) { |
64 | 64 |
return str; |
65 | 65 |
} |
66 | 66 |
}; |
67 | 67 |
|
68 | 68 |
template <typename _Item> |
69 | 69 |
class MapStorageBase { |
70 | 70 |
public: |
71 | 71 |
typedef _Item Item; |
72 | 72 |
|
73 | 73 |
public: |
74 | 74 |
MapStorageBase() {} |
75 | 75 |
virtual ~MapStorageBase() {} |
76 | 76 |
|
77 | 77 |
virtual void set(const Item& item, const std::string& value) = 0; |
78 | 78 |
|
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
template <typename _Item, typename _Map, |
82 | 82 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
83 | 83 |
class MapStorage : public MapStorageBase<_Item> { |
84 | 84 |
public: |
85 | 85 |
typedef _Map Map; |
86 | 86 |
typedef _Converter Converter; |
87 | 87 |
typedef _Item Item; |
88 | 88 |
|
89 | 89 |
private: |
90 | 90 |
Map& _map; |
91 | 91 |
Converter _converter; |
92 | 92 |
|
93 | 93 |
public: |
94 | 94 |
MapStorage(Map& map, const Converter& converter = Converter()) |
95 | 95 |
: _map(map), _converter(converter) {} |
96 | 96 |
virtual ~MapStorage() {} |
97 | 97 |
|
98 | 98 |
virtual void set(const Item& item ,const std::string& value) { |
99 | 99 |
_map.set(item, _converter(value)); |
100 | 100 |
} |
101 | 101 |
}; |
102 | 102 |
|
103 | 103 |
template <typename _Graph, bool _dir, typename _Map, |
104 | 104 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
105 | 105 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> { |
106 | 106 |
public: |
107 | 107 |
typedef _Map Map; |
108 | 108 |
typedef _Converter Converter; |
109 | 109 |
typedef _Graph Graph; |
110 | 110 |
typedef typename Graph::Edge Item; |
111 | 111 |
static const bool dir = _dir; |
112 | 112 |
|
113 | 113 |
private: |
114 | 114 |
const Graph& _graph; |
115 | 115 |
Map& _map; |
116 | 116 |
Converter _converter; |
117 | 117 |
|
118 | 118 |
public: |
119 | 119 |
GraphArcMapStorage(const Graph& graph, Map& map, |
120 | 120 |
const Converter& converter = Converter()) |
121 | 121 |
: _graph(graph), _map(map), _converter(converter) {} |
122 | 122 |
virtual ~GraphArcMapStorage() {} |
123 | 123 |
|
124 | 124 |
virtual void set(const Item& item ,const std::string& value) { |
125 | 125 |
_map.set(_graph.direct(item, dir), _converter(value)); |
126 | 126 |
} |
127 | 127 |
}; |
128 | 128 |
|
129 | 129 |
class ValueStorageBase { |
130 | 130 |
public: |
131 | 131 |
ValueStorageBase() {} |
132 | 132 |
virtual ~ValueStorageBase() {} |
133 | 133 |
|
134 | 134 |
virtual void set(const std::string&) = 0; |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
138 | 138 |
class ValueStorage : public ValueStorageBase { |
139 | 139 |
public: |
140 | 140 |
typedef _Value Value; |
141 | 141 |
typedef _Converter Converter; |
142 | 142 |
|
143 | 143 |
private: |
144 | 144 |
Value& _value; |
145 | 145 |
Converter _converter; |
146 | 146 |
|
147 | 147 |
public: |
148 | 148 |
ValueStorage(Value& value, const Converter& converter = Converter()) |
149 | 149 |
: _value(value), _converter(converter) {} |
150 | 150 |
|
151 | 151 |
virtual void set(const std::string& value) { |
152 | 152 |
_value = _converter(value); |
153 | 153 |
} |
154 | 154 |
}; |
155 | 155 |
|
156 | 156 |
template <typename Value> |
157 | 157 |
struct MapLookUpConverter { |
158 | 158 |
const std::map<std::string, Value>& _map; |
159 | 159 |
|
160 | 160 |
MapLookUpConverter(const std::map<std::string, Value>& map) |
161 | 161 |
: _map(map) {} |
162 | 162 |
|
163 | 163 |
Value operator()(const std::string& str) { |
164 | 164 |
typename std::map<std::string, Value>::const_iterator it = |
165 | 165 |
_map.find(str); |
166 | 166 |
if (it == _map.end()) { |
167 | 167 |
std::ostringstream msg; |
168 | 168 |
msg << "Item not found: " << str; |
169 | 169 |
throw DataFormatError(msg.str().c_str()); |
170 | 170 |
} |
171 | 171 |
return it->second; |
172 | 172 |
} |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
template <typename Graph> |
176 | 176 |
struct GraphArcLookUpConverter { |
177 | 177 |
const Graph& _graph; |
178 | 178 |
const std::map<std::string, typename Graph::Edge>& _map; |
179 | 179 |
|
180 | 180 |
GraphArcLookUpConverter(const Graph& graph, |
181 | 181 |
const std::map<std::string, |
182 | 182 |
typename Graph::Edge>& map) |
183 | 183 |
: _graph(graph), _map(map) {} |
184 | 184 |
|
185 | 185 |
typename Graph::Arc operator()(const std::string& str) { |
186 | 186 |
if (str.empty() || (str[0] != '+' && str[0] != '-')) { |
187 | 187 |
throw DataFormatError("Item must start with '+' or '-'"); |
188 | 188 |
} |
189 | 189 |
typename std::map<std::string, typename Graph::Edge> |
190 | 190 |
::const_iterator it = _map.find(str.substr(1)); |
191 | 191 |
if (it == _map.end()) { |
192 | 192 |
throw DataFormatError("Item not found"); |
193 | 193 |
} |
194 | 194 |
return _graph.direct(it->second, str[0] == '+'); |
195 | 195 |
} |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup lemon_io |
20 | 20 |
///\file |
21 | 21 |
///\brief \ref lgf-format "Lemon Graph Format" writer. |
22 | 22 |
|
23 | 23 |
|
24 | 24 |
#ifndef LEMON_LGF_WRITER_H |
25 | 25 |
#define LEMON_LGF_WRITER_H |
26 | 26 |
|
27 | 27 |
#include <iostream> |
28 | 28 |
#include <fstream> |
29 | 29 |
#include <sstream> |
30 | 30 |
|
31 | 31 |
#include <algorithm> |
32 | 32 |
|
33 | 33 |
#include <vector> |
34 | 34 |
#include <functional> |
35 | 35 |
|
36 | 36 |
#include <lemon/assert.h> |
37 | 37 |
#include <lemon/graph_utils.h> |
38 | 38 |
|
39 | 39 |
namespace lemon { |
40 | 40 |
|
41 | 41 |
namespace _writer_bits { |
42 | 42 |
|
43 | 43 |
template <typename Value> |
44 | 44 |
struct DefaultConverter { |
45 | 45 |
std::string operator()(const Value& value) { |
46 | 46 |
std::ostringstream os; |
47 | 47 |
os << value; |
48 | 48 |
return os.str(); |
49 | 49 |
} |
50 | 50 |
}; |
51 | 51 |
|
52 | 52 |
template <typename T> |
53 | 53 |
bool operator<(const T&, const T&) { |
54 | 54 |
throw DataFormatError("Label map is not comparable"); |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
template <typename _Map> |
58 | 58 |
class MapLess { |
59 | 59 |
public: |
60 | 60 |
typedef _Map Map; |
61 | 61 |
typedef typename Map::Key Item; |
62 | 62 |
|
63 | 63 |
private: |
64 | 64 |
const Map& _map; |
65 | 65 |
|
66 | 66 |
public: |
67 | 67 |
MapLess(const Map& map) : _map(map) {} |
68 | 68 |
|
69 | 69 |
bool operator()(const Item& left, const Item& right) { |
70 | 70 |
return _map[left] < _map[right]; |
71 | 71 |
} |
72 | 72 |
}; |
73 | 73 |
|
74 | 74 |
template <typename _Graph, bool _dir, typename _Map> |
75 | 75 |
class GraphArcMapLess { |
76 | 76 |
public: |
77 | 77 |
typedef _Map Map; |
78 | 78 |
typedef _Graph Graph; |
79 | 79 |
typedef typename Graph::Edge Item; |
80 | 80 |
|
81 | 81 |
private: |
82 | 82 |
const Graph& _graph; |
83 | 83 |
const Map& _map; |
84 | 84 |
|
85 | 85 |
public: |
86 | 86 |
GraphArcMapLess(const Graph& graph, const Map& map) |
87 | 87 |
: _graph(graph), _map(map) {} |
88 | 88 |
|
89 | 89 |
bool operator()(const Item& left, const Item& right) { |
90 | 90 |
return _map[_graph.direct(left, _dir)] < |
91 | 91 |
_map[_graph.direct(right, _dir)]; |
92 | 92 |
} |
93 | 93 |
}; |
94 | 94 |
|
95 | 95 |
template <typename _Item> |
96 | 96 |
class MapStorageBase { |
97 | 97 |
public: |
98 | 98 |
typedef _Item Item; |
99 | 99 |
|
100 | 100 |
public: |
101 | 101 |
MapStorageBase() {} |
102 | 102 |
virtual ~MapStorageBase() {} |
103 | 103 |
|
104 | 104 |
virtual std::string get(const Item& item) = 0; |
105 | 105 |
virtual void sort(std::vector<Item>&) = 0; |
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename _Item, typename _Map, |
109 | 109 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
110 | 110 |
class MapStorage : public MapStorageBase<_Item> { |
111 | 111 |
public: |
112 | 112 |
typedef _Map Map; |
113 | 113 |
typedef _Converter Converter; |
114 | 114 |
typedef _Item Item; |
115 | 115 |
|
116 | 116 |
private: |
117 | 117 |
const Map& _map; |
118 | 118 |
Converter _converter; |
119 | 119 |
|
120 | 120 |
public: |
121 | 121 |
MapStorage(const Map& map, const Converter& converter = Converter()) |
122 | 122 |
: _map(map), _converter(converter) {} |
123 | 123 |
virtual ~MapStorage() {} |
124 | 124 |
|
125 | 125 |
virtual std::string get(const Item& item) { |
126 | 126 |
return _converter(_map[item]); |
127 | 127 |
} |
128 | 128 |
virtual void sort(std::vector<Item>& items) { |
129 | 129 |
MapLess<Map> less(_map); |
130 | 130 |
std::sort(items.begin(), items.end(), less); |
131 | 131 |
} |
132 | 132 |
}; |
133 | 133 |
|
134 | 134 |
template <typename _Graph, bool _dir, typename _Map, |
135 | 135 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
136 | 136 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> { |
137 | 137 |
public: |
138 | 138 |
typedef _Map Map; |
139 | 139 |
typedef _Converter Converter; |
140 | 140 |
typedef _Graph Graph; |
141 | 141 |
typedef typename Graph::Edge Item; |
142 | 142 |
static const bool dir = _dir; |
143 | 143 |
|
144 | 144 |
private: |
145 | 145 |
const Graph& _graph; |
146 | 146 |
const Map& _map; |
147 | 147 |
Converter _converter; |
148 | 148 |
|
149 | 149 |
public: |
150 | 150 |
GraphArcMapStorage(const Graph& graph, const Map& map, |
151 | 151 |
const Converter& converter = Converter()) |
152 | 152 |
: _graph(graph), _map(map), _converter(converter) {} |
153 | 153 |
virtual ~GraphArcMapStorage() {} |
154 | 154 |
|
155 | 155 |
virtual std::string get(const Item& item) { |
156 | 156 |
return _converter(_map[_graph.direct(item, dir)]); |
157 | 157 |
} |
158 | 158 |
virtual void sort(std::vector<Item>& items) { |
159 | 159 |
GraphArcMapLess<Graph, dir, Map> less(_graph, _map); |
160 | 160 |
std::sort(items.begin(), items.end(), less); |
161 | 161 |
} |
162 | 162 |
}; |
163 | 163 |
|
164 | 164 |
class ValueStorageBase { |
165 | 165 |
public: |
166 | 166 |
ValueStorageBase() {} |
167 | 167 |
virtual ~ValueStorageBase() {} |
168 | 168 |
|
169 | 169 |
virtual std::string get() = 0; |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
173 | 173 |
class ValueStorage : public ValueStorageBase { |
174 | 174 |
public: |
175 | 175 |
typedef _Value Value; |
176 | 176 |
typedef _Converter Converter; |
177 | 177 |
|
178 | 178 |
private: |
179 | 179 |
const Value& _value; |
180 | 180 |
Converter _converter; |
181 | 181 |
|
182 | 182 |
public: |
183 | 183 |
ValueStorage(const Value& value, const Converter& converter = Converter()) |
184 | 184 |
: _value(value), _converter(converter) {} |
185 | 185 |
|
186 | 186 |
virtual std::string get() { |
187 | 187 |
return _converter(_value); |
188 | 188 |
} |
189 | 189 |
}; |
190 | 190 |
|
191 | 191 |
template <typename Value> |
192 | 192 |
struct MapLookUpConverter { |
193 | 193 |
const std::map<Value, std::string>& _map; |
194 | 194 |
|
195 | 195 |
MapLookUpConverter(const std::map<Value, std::string>& map) |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_LIST_GRAPH_H |
20 | 20 |
#define LEMON_LIST_GRAPH_H |
21 | 21 |
|
22 | 22 |
///\ingroup graphs |
23 | 23 |
///\file |
24 | 24 |
///\brief ListDigraph, ListGraph classes. |
25 | 25 |
|
26 | 26 |
#include <lemon/bits/graph_extender.h> |
27 | 27 |
|
28 | 28 |
#include <vector> |
29 | 29 |
#include <list> |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
class ListDigraphBase { |
34 | 34 |
|
35 | 35 |
protected: |
36 | 36 |
struct NodeT { |
37 | 37 |
int first_in, first_out; |
38 | 38 |
int prev, next; |
39 | 39 |
}; |
40 | 40 |
|
41 | 41 |
struct ArcT { |
42 | 42 |
int target, source; |
43 | 43 |
int prev_in, prev_out; |
44 | 44 |
int next_in, next_out; |
45 | 45 |
}; |
46 | 46 |
|
47 | 47 |
std::vector<NodeT> nodes; |
48 | 48 |
|
49 | 49 |
int first_node; |
50 | 50 |
|
51 | 51 |
int first_free_node; |
52 | 52 |
|
53 | 53 |
std::vector<ArcT> arcs; |
54 | 54 |
|
55 | 55 |
int first_free_arc; |
56 | 56 |
|
57 | 57 |
public: |
58 | 58 |
|
59 | 59 |
typedef ListDigraphBase Digraph; |
60 | 60 |
|
61 | 61 |
class Node { |
62 | 62 |
friend class ListDigraphBase; |
63 | 63 |
protected: |
64 | 64 |
|
65 | 65 |
int id; |
66 | 66 |
explicit Node(int pid) { id = pid;} |
67 | 67 |
|
68 | 68 |
public: |
69 | 69 |
Node() {} |
70 | 70 |
Node (Invalid) { id = -1; } |
71 | 71 |
bool operator==(const Node& node) const {return id == node.id;} |
72 | 72 |
bool operator!=(const Node& node) const {return id != node.id;} |
73 | 73 |
bool operator<(const Node& node) const {return id < node.id;} |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
class Arc { |
77 | 77 |
friend class ListDigraphBase; |
78 | 78 |
protected: |
79 | 79 |
|
80 | 80 |
int id; |
81 | 81 |
explicit Arc(int pid) { id = pid;} |
82 | 82 |
|
83 | 83 |
public: |
84 | 84 |
Arc() {} |
85 | 85 |
Arc (Invalid) { id = -1; } |
86 | 86 |
bool operator==(const Arc& arc) const {return id == arc.id;} |
87 | 87 |
bool operator!=(const Arc& arc) const {return id != arc.id;} |
88 | 88 |
bool operator<(const Arc& arc) const {return id < arc.id;} |
89 | 89 |
}; |
90 | 90 |
|
91 | 91 |
|
92 | 92 |
|
93 | 93 |
ListDigraphBase() |
94 | 94 |
: nodes(), first_node(-1), |
95 | 95 |
first_free_node(-1), arcs(), first_free_arc(-1) {} |
96 | 96 |
|
97 | 97 |
|
98 | 98 |
int maxNodeId() const { return nodes.size()-1; } |
99 | 99 |
int maxArcId() const { return arcs.size()-1; } |
100 | 100 |
|
101 | 101 |
Node source(Arc e) const { return Node(arcs[e.id].source); } |
102 | 102 |
Node target(Arc e) const { return Node(arcs[e.id].target); } |
103 | 103 |
|
104 | 104 |
|
105 | 105 |
void first(Node& node) const { |
106 | 106 |
node.id = first_node; |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
void next(Node& node) const { |
110 | 110 |
node.id = nodes[node.id].next; |
111 | 111 |
} |
112 | 112 |
|
113 | 113 |
|
114 | 114 |
void first(Arc& arc) const { |
115 | 115 |
int n; |
116 | 116 |
for(n = first_node; |
117 | 117 |
n!=-1 && nodes[n].first_in == -1; |
118 | 118 |
n = nodes[n].next) {} |
119 | 119 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
120 | 120 |
} |
121 | 121 |
|
122 | 122 |
void next(Arc& arc) const { |
123 | 123 |
if (arcs[arc.id].next_in != -1) { |
124 | 124 |
arc.id = arcs[arc.id].next_in; |
125 | 125 |
} else { |
126 | 126 |
int n; |
127 | 127 |
for(n = nodes[arcs[arc.id].target].next; |
128 | 128 |
n!=-1 && nodes[n].first_in == -1; |
129 | 129 |
n = nodes[n].next) {} |
130 | 130 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
131 | 131 |
} |
132 | 132 |
} |
133 | 133 |
|
134 | 134 |
void firstOut(Arc &e, const Node& v) const { |
135 | 135 |
e.id = nodes[v.id].first_out; |
136 | 136 |
} |
137 | 137 |
void nextOut(Arc &e) const { |
138 | 138 |
e.id=arcs[e.id].next_out; |
139 | 139 |
} |
140 | 140 |
|
141 | 141 |
void firstIn(Arc &e, const Node& v) const { |
142 | 142 |
e.id = nodes[v.id].first_in; |
143 | 143 |
} |
144 | 144 |
void nextIn(Arc &e) const { |
145 | 145 |
e.id=arcs[e.id].next_in; |
146 | 146 |
} |
147 | 147 |
|
148 | 148 |
|
149 | 149 |
static int id(Node v) { return v.id; } |
150 | 150 |
static int id(Arc e) { return e.id; } |
151 | 151 |
|
152 | 152 |
static Node nodeFromId(int id) { return Node(id);} |
153 | 153 |
static Arc arcFromId(int id) { return Arc(id);} |
154 | 154 |
|
155 | 155 |
bool valid(Node n) const { |
156 | 156 |
return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
157 | 157 |
nodes[n.id].prev != -2; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
bool valid(Arc a) const { |
161 | 161 |
return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
162 | 162 |
arcs[a.id].prev_in != -2; |
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
Node addNode() { |
166 | 166 |
int n; |
167 | 167 |
|
168 | 168 |
if(first_free_node==-1) { |
169 | 169 |
n = nodes.size(); |
170 | 170 |
nodes.push_back(NodeT()); |
171 | 171 |
} else { |
172 | 172 |
n = first_free_node; |
173 | 173 |
first_free_node = nodes[n].next; |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
nodes[n].next = first_node; |
177 | 177 |
if(first_node != -1) nodes[first_node].prev = n; |
178 | 178 |
first_node = n; |
179 | 179 |
nodes[n].prev = -1; |
180 | 180 |
|
181 | 181 |
nodes[n].first_in = nodes[n].first_out = -1; |
182 | 182 |
|
183 | 183 |
return Node(n); |
184 | 184 |
} |
185 | 185 |
|
186 | 186 |
Arc addArc(Node u, Node v) { |
187 | 187 |
int n; |
188 | 188 |
|
189 | 189 |
if (first_free_arc == -1) { |
190 | 190 |
n = arcs.size(); |
191 | 191 |
arcs.push_back(ArcT()); |
192 | 192 |
} else { |
193 | 193 |
n = first_free_arc; |
194 | 194 |
first_free_arc = arcs[n].next_in; |
195 | 195 |
} |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_MAPS_H |
20 | 20 |
#define LEMON_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <functional> |
24 | 24 |
#include <vector> |
25 | 25 |
|
26 | 26 |
#include <lemon/bits/utility.h> |
27 | 27 |
#include <lemon/bits/traits.h> |
28 | 28 |
|
29 | 29 |
///\file |
30 | 30 |
///\ingroup maps |
31 | 31 |
///\brief Miscellaneous property maps |
32 | 32 |
|
33 | 33 |
#include <map> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \addtogroup maps |
38 | 38 |
/// @{ |
39 | 39 |
|
40 | 40 |
/// Base class of maps. |
41 | 41 |
|
42 | 42 |
/// Base class of maps. It provides the necessary type definitions |
43 | 43 |
/// required by the map %concepts. |
44 | 44 |
template<typename K, typename V> |
45 | 45 |
class MapBase { |
46 | 46 |
public: |
47 | 47 |
/// \biref The key type of the map. |
48 | 48 |
typedef K Key; |
49 | 49 |
/// \brief The value type of the map. |
50 | 50 |
/// (The type of objects associated with the keys). |
51 | 51 |
typedef V Value; |
52 | 52 |
}; |
53 | 53 |
|
54 | 54 |
|
55 | 55 |
/// Null map. (a.k.a. DoNothingMap) |
56 | 56 |
|
57 | 57 |
/// This map can be used if you have to provide a map only for |
58 | 58 |
/// its type definitions, or if you have to provide a writable map, |
59 | 59 |
/// but data written to it is not required (i.e. it will be sent to |
60 | 60 |
/// <tt>/dev/null</tt>). |
61 | 61 |
/// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
62 | 62 |
/// |
63 | 63 |
/// \sa ConstMap |
64 | 64 |
template<typename K, typename V> |
65 | 65 |
class NullMap : public MapBase<K, V> { |
66 | 66 |
public: |
67 | 67 |
typedef MapBase<K, V> Parent; |
68 | 68 |
typedef typename Parent::Key Key; |
69 | 69 |
typedef typename Parent::Value Value; |
70 | 70 |
|
71 | 71 |
/// Gives back a default constructed element. |
72 | 72 |
Value operator[](const Key&) const { return Value(); } |
73 | 73 |
/// Absorbs the value. |
74 | 74 |
void set(const Key&, const Value&) {} |
75 | 75 |
}; |
76 | 76 |
|
77 | 77 |
/// Returns a \ref NullMap class |
78 | 78 |
|
79 | 79 |
/// This function just returns a \ref NullMap class. |
80 | 80 |
/// \relates NullMap |
81 | 81 |
template <typename K, typename V> |
82 | 82 |
NullMap<K, V> nullMap() { |
83 | 83 |
return NullMap<K, V>(); |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
|
87 | 87 |
/// Constant map. |
88 | 88 |
|
89 | 89 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
90 | 90 |
/// value to each key. |
91 | 91 |
/// |
92 | 92 |
/// In other aspects it is equivalent to \ref NullMap. |
93 | 93 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
94 | 94 |
/// concept, but it absorbs the data written to it. |
95 | 95 |
/// |
96 | 96 |
/// The simplest way of using this map is through the constMap() |
97 | 97 |
/// function. |
98 | 98 |
/// |
99 | 99 |
/// \sa NullMap |
100 | 100 |
/// \sa IdentityMap |
101 | 101 |
template<typename K, typename V> |
102 | 102 |
class ConstMap : public MapBase<K, V> { |
103 | 103 |
private: |
104 | 104 |
V _value; |
105 | 105 |
public: |
106 | 106 |
typedef MapBase<K, V> Parent; |
107 | 107 |
typedef typename Parent::Key Key; |
108 | 108 |
typedef typename Parent::Value Value; |
109 | 109 |
|
110 | 110 |
/// Default constructor |
111 | 111 |
|
112 | 112 |
/// Default constructor. |
113 | 113 |
/// The value of the map will be default constructed. |
114 | 114 |
ConstMap() {} |
115 | 115 |
|
116 | 116 |
/// Constructor with specified initial value |
117 | 117 |
|
118 | 118 |
/// Constructor with specified initial value. |
119 | 119 |
/// \param v The initial value of the map. |
120 | 120 |
ConstMap(const Value &v) : _value(v) {} |
121 | 121 |
|
122 | 122 |
/// Gives back the specified value. |
123 | 123 |
Value operator[](const Key&) const { return _value; } |
124 | 124 |
|
125 | 125 |
/// Absorbs the value. |
126 | 126 |
void set(const Key&, const Value&) {} |
127 | 127 |
|
128 | 128 |
/// Sets the value that is assigned to each key. |
129 | 129 |
void setAll(const Value &v) { |
130 | 130 |
_value = v; |
131 | 131 |
} |
132 | 132 |
|
133 | 133 |
template<typename V1> |
134 | 134 |
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {} |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
/// Returns a \ref ConstMap class |
138 | 138 |
|
139 | 139 |
/// This function just returns a \ref ConstMap class. |
140 | 140 |
/// \relates ConstMap |
141 | 141 |
template<typename K, typename V> |
142 | 142 |
inline ConstMap<K, V> constMap(const V &v) { |
143 | 143 |
return ConstMap<K, V>(v); |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
template<typename K, typename V> |
147 | 147 |
inline ConstMap<K, V> constMap() { |
148 | 148 |
return ConstMap<K, V>(); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
|
152 | 152 |
template<typename T, T v> |
153 | 153 |
struct Const {}; |
154 | 154 |
|
155 | 155 |
/// Constant map with inlined constant value. |
156 | 156 |
|
157 | 157 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
158 | 158 |
/// value to each key. |
159 | 159 |
/// |
160 | 160 |
/// In other aspects it is equivalent to \ref NullMap. |
161 | 161 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
162 | 162 |
/// concept, but it absorbs the data written to it. |
163 | 163 |
/// |
164 | 164 |
/// The simplest way of using this map is through the constMap() |
165 | 165 |
/// function. |
166 | 166 |
/// |
167 | 167 |
/// \sa NullMap |
168 | 168 |
/// \sa IdentityMap |
169 | 169 |
template<typename K, typename V, V v> |
170 | 170 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
171 | 171 |
public: |
172 | 172 |
typedef MapBase<K, V> Parent; |
173 | 173 |
typedef typename Parent::Key Key; |
174 | 174 |
typedef typename Parent::Value Value; |
175 | 175 |
|
176 | 176 |
/// Constructor. |
177 | 177 |
ConstMap() {} |
178 | 178 |
|
179 | 179 |
/// Gives back the specified value. |
180 | 180 |
Value operator[](const Key&) const { return v; } |
181 | 181 |
|
182 | 182 |
/// Absorbs the value. |
183 | 183 |
void set(const Key&, const Value&) {} |
184 | 184 |
}; |
185 | 185 |
|
186 | 186 |
/// Returns a \ref ConstMap class with inlined constant value |
187 | 187 |
|
188 | 188 |
/// This function just returns a \ref ConstMap class with inlined |
189 | 189 |
/// constant value. |
190 | 190 |
/// \relates ConstMap |
191 | 191 |
template<typename K, typename V, V v> |
192 | 192 |
inline ConstMap<K, Const<V, v> > constMap() { |
193 | 193 |
return ConstMap<K, Const<V, v> >(); |
194 | 194 |
} |
195 | 195 |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_MATH_H |
20 | 20 |
#define LEMON_MATH_H |
21 | 21 |
|
22 | 22 |
///\ingroup misc |
23 | 23 |
///\file |
24 | 24 |
///\brief Some extensions to the standard \c cmath library. |
25 | 25 |
/// |
26 | 26 |
///Some extensions to the standard \c cmath library. |
27 | 27 |
/// |
28 | 28 |
///This file includes the standard math library (cmath). |
29 | 29 |
|
30 | 30 |
#include<cmath> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \addtogroup misc |
35 | 35 |
/// @{ |
36 | 36 |
|
37 | 37 |
/// The Euler constant |
38 | 38 |
const long double E = 2.7182818284590452353602874713526625L; |
39 | 39 |
/// log_2(e) |
40 | 40 |
const long double LOG2E = 1.4426950408889634073599246810018921L; |
41 | 41 |
/// log_10(e) |
42 | 42 |
const long double LOG10E = 0.4342944819032518276511289189166051L; |
43 | 43 |
/// ln(2) |
44 | 44 |
const long double LN2 = 0.6931471805599453094172321214581766L; |
45 | 45 |
/// ln(10) |
46 | 46 |
const long double LN10 = 2.3025850929940456840179914546843642L; |
47 | 47 |
/// pi |
48 | 48 |
const long double PI = 3.1415926535897932384626433832795029L; |
49 | 49 |
/// pi/2 |
50 | 50 |
const long double PI_2 = 1.5707963267948966192313216916397514L; |
51 | 51 |
/// pi/4 |
52 | 52 |
const long double PI_4 = 0.7853981633974483096156608458198757L; |
53 | 53 |
/// sqrt(2) |
54 | 54 |
const long double SQRT2 = 1.4142135623730950488016887242096981L; |
55 | 55 |
/// 1/sqrt(2) |
56 | 56 |
const long double SQRT1_2 = 0.7071067811865475244008443621048490L; |
57 | 57 |
|
58 | 58 |
|
59 | 59 |
/// @} |
60 | 60 |
|
61 | 61 |
} //namespace lemon |
62 | 62 |
|
63 | 63 |
#endif //LEMON_TOLERANCE_H |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 paths |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 | 23 |
|
24 | 24 |
#ifndef LEMON_PATH_H |
25 | 25 |
#define LEMON_PATH_H |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <algorithm> |
29 | 29 |
|
30 | 30 |
#include <lemon/error.h> |
31 | 31 |
#include <lemon/bits/invalid.h> |
32 | 32 |
#include <lemon/concepts/path.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup paths |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
|
40 | 40 |
/// \brief A structure for representing directed paths in a digraph. |
41 | 41 |
/// |
42 | 42 |
/// A structure for representing directed path in a digraph. |
43 | 43 |
/// \tparam _Digraph The digraph type in which the path is. |
44 | 44 |
/// |
45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
46 | 46 |
/// lemon path type stores just this list. As a consequence, it |
47 | 47 |
/// cannot enumerate the nodes of the path and the source node of |
48 | 48 |
/// a zero length path is undefined. |
49 | 49 |
/// |
50 | 50 |
/// This implementation is a back and front insertable and erasable |
51 | 51 |
/// path type. It can be indexed in O(1) time. The front and back |
52 | 52 |
/// insertion and erase is done in O(1) (amortized) time. The |
53 | 53 |
/// implementation uses two vectors for storing the front and back |
54 | 54 |
/// insertions. |
55 | 55 |
template <typename _Digraph> |
56 | 56 |
class Path { |
57 | 57 |
public: |
58 | 58 |
|
59 | 59 |
typedef _Digraph Digraph; |
60 | 60 |
typedef typename Digraph::Arc Arc; |
61 | 61 |
|
62 | 62 |
/// \brief Default constructor |
63 | 63 |
/// |
64 | 64 |
/// Default constructor |
65 | 65 |
Path() {} |
66 | 66 |
|
67 | 67 |
/// \brief Template copy constructor |
68 | 68 |
/// |
69 | 69 |
/// This constuctor initializes the path from any other path type. |
70 | 70 |
/// It simply makes a copy of the given path. |
71 | 71 |
template <typename CPath> |
72 | 72 |
Path(const CPath& cpath) { |
73 | 73 |
copyPath(*this, cpath); |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
/// \brief Template copy assignment |
77 | 77 |
/// |
78 | 78 |
/// This operator makes a copy of a path of any other type. |
79 | 79 |
template <typename CPath> |
80 | 80 |
Path& operator=(const CPath& cpath) { |
81 | 81 |
copyPath(*this, cpath); |
82 | 82 |
return *this; |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
/// \brief Lemon style iterator for path arcs |
86 | 86 |
/// |
87 | 87 |
/// This class is used to iterate on the arcs of the paths. |
88 | 88 |
class ArcIt { |
89 | 89 |
friend class Path; |
90 | 90 |
public: |
91 | 91 |
/// \brief Default constructor |
92 | 92 |
ArcIt() {} |
93 | 93 |
/// \brief Invalid constructor |
94 | 94 |
ArcIt(Invalid) : path(0), idx(-1) {} |
95 | 95 |
/// \brief Initializate the iterator to the first arc of path |
96 | 96 |
ArcIt(const Path &_path) |
97 | 97 |
: path(&_path), idx(_path.empty() ? -1 : 0) {} |
98 | 98 |
|
99 | 99 |
private: |
100 | 100 |
|
101 | 101 |
ArcIt(const Path &_path, int _idx) |
102 | 102 |
: path(&_path), idx(_idx) {} |
103 | 103 |
|
104 | 104 |
public: |
105 | 105 |
|
106 | 106 |
/// \brief Conversion to Arc |
107 | 107 |
operator const Arc&() const { |
108 | 108 |
return path->nth(idx); |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
/// \brief Next arc |
112 | 112 |
ArcIt& operator++() { |
113 | 113 |
++idx; |
114 | 114 |
if (idx >= path->length()) idx = -1; |
115 | 115 |
return *this; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
/// \brief Comparison operator |
119 | 119 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
120 | 120 |
/// \brief Comparison operator |
121 | 121 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
122 | 122 |
/// \brief Comparison operator |
123 | 123 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
124 | 124 |
|
125 | 125 |
private: |
126 | 126 |
const Path *path; |
127 | 127 |
int idx; |
128 | 128 |
}; |
129 | 129 |
|
130 | 130 |
/// \brief Length of the path. |
131 | 131 |
int length() const { return head.size() + tail.size(); } |
132 | 132 |
/// \brief Return whether the path is empty. |
133 | 133 |
bool empty() const { return head.empty() && tail.empty(); } |
134 | 134 |
|
135 | 135 |
/// \brief Reset the path to an empty one. |
136 | 136 |
void clear() { head.clear(); tail.clear(); } |
137 | 137 |
|
138 | 138 |
/// \brief The nth arc. |
139 | 139 |
/// |
140 | 140 |
/// \pre n is in the [0..length() - 1] range |
141 | 141 |
const Arc& nth(int n) const { |
142 | 142 |
return n < int(head.size()) ? *(head.rbegin() + n) : |
143 | 143 |
*(tail.begin() + (n - head.size())); |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
/// \brief Initialize arc iterator to point to the nth arc |
147 | 147 |
/// |
148 | 148 |
/// \pre n is in the [0..length() - 1] range |
149 | 149 |
ArcIt nthIt(int n) const { |
150 | 150 |
return ArcIt(*this, n); |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
/// \brief The first arc of the path |
154 | 154 |
const Arc& front() const { |
155 | 155 |
return head.empty() ? tail.front() : head.back(); |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
/// \brief Add a new arc before the current path |
159 | 159 |
void addFront(const Arc& arc) { |
160 | 160 |
head.push_back(arc); |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
/// \brief Erase the first arc of the path |
164 | 164 |
void eraseFront() { |
165 | 165 |
if (!head.empty()) { |
166 | 166 |
head.pop_back(); |
167 | 167 |
} else { |
168 | 168 |
head.clear(); |
169 | 169 |
int halfsize = tail.size() / 2; |
170 | 170 |
head.resize(halfsize); |
171 | 171 |
std::copy(tail.begin() + 1, tail.begin() + halfsize + 1, |
172 | 172 |
head.rbegin()); |
173 | 173 |
std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin()); |
174 | 174 |
tail.resize(tail.size() - halfsize - 1); |
175 | 175 |
} |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
/// \brief The last arc of the path |
179 | 179 |
const Arc& back() const { |
180 | 180 |
return tail.empty() ? head.front() : tail.back(); |
181 | 181 |
} |
182 | 182 |
|
183 | 183 |
/// \brief Add a new arc behind the current path |
184 | 184 |
void addBack(const Arc& arc) { |
185 | 185 |
tail.push_back(arc); |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
/// \brief Erase the last arc of the path |
189 | 189 |
void eraseBack() { |
190 | 190 |
if (!tail.empty()) { |
191 | 191 |
tail.pop_back(); |
192 | 192 |
} else { |
193 | 193 |
int halfsize = head.size() / 2; |
194 | 194 |
tail.resize(halfsize); |
195 | 195 |
std::copy(head.begin() + 1, head.begin() + halfsize + 1, |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 Instantiation of the Random class. |
21 | 21 |
|
22 | 22 |
#include <lemon/random.h> |
23 | 23 |
|
24 | 24 |
namespace lemon { |
25 | 25 |
/// \brief Global random number generator instance |
26 | 26 |
/// |
27 | 27 |
/// A global Mersenne Twister random number generator instance. |
28 | 28 |
Random rnd; |
29 | 29 |
} |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)