| 1 | 1 |
LEMON code without an explicit copyright is covered by the following |
| 2 | 2 |
copyright/license. |
| 3 | 3 |
|
| 4 |
Copyright (C) 2003- |
|
| 4 |
Copyright (C) 2003-2009 Egervary Jeno Kombinatorikus Optimalizalasi |
|
| 5 | 5 |
Kutatocsoport (Egervary Combinatorial Optimization Research Group, |
| 6 | 6 |
EGRES). |
| 7 | 7 |
|
| 8 | 8 |
Permission is hereby granted, free of charge, to any person or organization |
| 9 | 9 |
obtaining a copy of the software and accompanying documentation covered by |
| 10 | 10 |
this license (the "Software") to use, reproduce, display, distribute, |
| 11 | 11 |
execute, and transmit the Software, and to prepare derivative works of the |
| 12 | 12 |
Software, and to permit third-parties to whom the Software is furnished to |
| 13 | 13 |
do so, all subject to the following: |
| 14 | 14 |
|
| 15 | 15 |
The copyright notices in the Software and this entire statement, including |
| 16 | 16 |
the above license grant, this restriction and the following disclaimer, |
| 17 | 17 |
must be included in all copies of the Software, in whole or in part, and |
| 18 | 18 |
all derivative works of the Software, unless such copies or derivative |
| 19 | 19 |
works are solely in the form of machine-executable object code generated by |
| 20 | 20 |
a source language processor. |
| 21 | 21 |
|
| 22 | 22 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | 23 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | 24 |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 25 | 25 |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 26 | 26 |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 27 | 27 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 28 | 28 |
DEALINGS IN THE SOFTWARE. |
| 29 | 29 |
|
| 30 | 30 |
=========================================================================== |
| 31 | 31 |
This license is a verbatim copy of the Boost Software License, Version 1.0. |
| 32 | 32 |
|
| 33 | 33 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup demos |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Argument parser demo |
| 22 | 22 |
/// |
| 23 | 23 |
/// This example shows how the argument parser can be used. |
| 24 | 24 |
/// |
| 25 | 25 |
/// \include arg_parser_demo.cc |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/arg_parser.h> |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
int main(int argc, char **argv) |
| 31 | 31 |
{
|
| 32 | 32 |
// Initialize the argument parser |
| 33 | 33 |
ArgParser ap(argc, argv); |
| 34 | 34 |
int i; |
| 35 | 35 |
std::string s; |
| 36 | 36 |
double d = 1.0; |
| 37 | 37 |
bool b, nh; |
| 38 | 38 |
bool g1, g2, g3; |
| 39 | 39 |
|
| 40 | 40 |
// Add a mandatory integer option with storage reference |
| 41 | 41 |
ap.refOption("n", "An integer input.", i, true);
|
| 42 | 42 |
// Add a double option with storage reference (the default value is 1.0) |
| 43 | 43 |
ap.refOption("val", "A double input.", d);
|
| 44 | 44 |
// Add a double option without storage reference (the default value is 3.14) |
| 45 | 45 |
ap.doubleOption("val2", "A double input.", 3.14);
|
| 46 | 46 |
// Set synonym for -val option |
| 47 | 47 |
ap.synonym("vals", "val");
|
| 48 | 48 |
// Add a string option |
| 49 | 49 |
ap.refOption("name", "A string input.", s);
|
| 50 | 50 |
// Add bool options |
| 51 | 51 |
ap.refOption("f", "A switch.", b)
|
| 52 | 52 |
.refOption("nohelp", "", nh)
|
| 53 | 53 |
.refOption("gra", "Choice A", g1)
|
| 54 | 54 |
.refOption("grb", "Choice B", g2)
|
| 55 | 55 |
.refOption("grc", "Choice C", g3);
|
| 56 | 56 |
// Bundle -gr* options into a group |
| 57 | 57 |
ap.optionGroup("gr", "gra")
|
| 58 | 58 |
.optionGroup("gr", "grb")
|
| 59 | 59 |
.optionGroup("gr", "grc");
|
| 60 | 60 |
// Set the group mandatory |
| 61 | 61 |
ap.mandatoryGroup("gr");
|
| 62 | 62 |
// Set the options of the group exclusive (only one option can be given) |
| 63 | 63 |
ap.onlyOneGroup("gr");
|
| 64 | 64 |
// Add non-parsed arguments (e.g. input files) |
| 65 | 65 |
ap.other("infile", "The input file.")
|
| 66 | 66 |
.other("...");
|
| 67 | 67 |
|
| 68 | 68 |
// Perform the parsing process |
| 69 | 69 |
// (in case of any error it terminates the program) |
| 70 | 70 |
ap.parse(); |
| 71 | 71 |
|
| 72 | 72 |
// Check each option if it has been given and print its value |
| 73 | 73 |
std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
| 74 | 74 |
|
| 75 | 75 |
std::cout << " Value of -n: " << i << std::endl; |
| 76 | 76 |
if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl;
|
| 77 | 77 |
if(ap.given("val2")) {
|
| 78 | 78 |
d = ap["val2"]; |
| 79 | 79 |
std::cout << " Value of -val2: " << d << std::endl; |
| 80 | 80 |
} |
| 81 | 81 |
if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl;
|
| 82 | 82 |
if(ap.given("f")) std::cout << " -f is given\n";
|
| 83 | 83 |
if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh << std::endl;
|
| 84 | 84 |
if(ap.given("gra")) std::cout << " -gra is given\n";
|
| 85 | 85 |
if(ap.given("grb")) std::cout << " -grb is given\n";
|
| 86 | 86 |
if(ap.given("grc")) std::cout << " -grc is given\n";
|
| 87 | 87 |
|
| 88 | 88 |
switch(ap.files().size()) {
|
| 89 | 89 |
case 0: |
| 90 | 90 |
std::cout << " No file argument was given.\n"; |
| 91 | 91 |
break; |
| 92 | 92 |
case 1: |
| 93 | 93 |
std::cout << " 1 file argument was given. It is:\n"; |
| 94 | 94 |
break; |
| 95 | 95 |
default: |
| 96 | 96 |
std::cout << " " |
| 97 | 97 |
<< ap.files().size() << " file arguments were given. They are:\n"; |
| 98 | 98 |
} |
| 99 | 99 |
for(unsigned int i=0;i<ap.files().size();++i) |
| 100 | 100 |
std::cout << " '" << ap.files()[i] << "'\n"; |
| 101 | 101 |
|
| 102 | 102 |
return 0; |
| 103 | 103 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/// \ingroup demos |
| 20 | 20 |
/// \file |
| 21 | 21 |
/// \brief Demo of the graph drawing function \ref graphToEps() |
| 22 | 22 |
/// |
| 23 | 23 |
/// This demo program shows examples how to use the function \ref |
| 24 | 24 |
/// graphToEps(). It takes no input but simply creates seven |
| 25 | 25 |
/// <tt>.eps</tt> files demonstrating the capability of \ref |
| 26 | 26 |
/// graphToEps(), and showing how to draw directed graphs, |
| 27 | 27 |
/// how to handle parallel egdes, how to change the properties (like |
| 28 | 28 |
/// color, shape, size, title etc.) of nodes and arcs individually |
| 29 | 29 |
/// using appropriate graph maps. |
| 30 | 30 |
/// |
| 31 | 31 |
/// \include graph_to_eps_demo.cc |
| 32 | 32 |
|
| 33 | 33 |
#include<lemon/list_graph.h> |
| 34 | 34 |
#include<lemon/graph_to_eps.h> |
| 35 | 35 |
#include<lemon/math.h> |
| 36 | 36 |
|
| 37 | 37 |
using namespace std; |
| 38 | 38 |
using namespace lemon; |
| 39 | 39 |
|
| 40 | 40 |
int main() |
| 41 | 41 |
{
|
| 42 | 42 |
Palette palette; |
| 43 | 43 |
Palette paletteW(true); |
| 44 | 44 |
|
| 45 | 45 |
// Create a small digraph |
| 46 | 46 |
ListDigraph g; |
| 47 | 47 |
typedef ListDigraph::Node Node; |
| 48 | 48 |
typedef ListDigraph::NodeIt NodeIt; |
| 49 | 49 |
typedef ListDigraph::Arc Arc; |
| 50 | 50 |
typedef dim2::Point<int> Point; |
| 51 | 51 |
|
| 52 | 52 |
Node n1=g.addNode(); |
| 53 | 53 |
Node n2=g.addNode(); |
| 54 | 54 |
Node n3=g.addNode(); |
| 55 | 55 |
Node n4=g.addNode(); |
| 56 | 56 |
Node n5=g.addNode(); |
| 57 | 57 |
|
| 58 | 58 |
ListDigraph::NodeMap<Point> coords(g); |
| 59 | 59 |
ListDigraph::NodeMap<double> sizes(g); |
| 60 | 60 |
ListDigraph::NodeMap<int> colors(g); |
| 61 | 61 |
ListDigraph::NodeMap<int> shapes(g); |
| 62 | 62 |
ListDigraph::ArcMap<int> acolors(g); |
| 63 | 63 |
ListDigraph::ArcMap<int> widths(g); |
| 64 | 64 |
|
| 65 | 65 |
coords[n1]=Point(50,50); sizes[n1]=1; colors[n1]=1; shapes[n1]=0; |
| 66 | 66 |
coords[n2]=Point(50,70); sizes[n2]=2; colors[n2]=2; shapes[n2]=2; |
| 67 | 67 |
coords[n3]=Point(70,70); sizes[n3]=1; colors[n3]=3; shapes[n3]=0; |
| 68 | 68 |
coords[n4]=Point(70,50); sizes[n4]=2; colors[n4]=4; shapes[n4]=1; |
| 69 | 69 |
coords[n5]=Point(85,60); sizes[n5]=3; colors[n5]=5; shapes[n5]=2; |
| 70 | 70 |
|
| 71 | 71 |
Arc a; |
| 72 | 72 |
|
| 73 | 73 |
a=g.addArc(n1,n2); acolors[a]=0; widths[a]=1; |
| 74 | 74 |
a=g.addArc(n2,n3); acolors[a]=0; widths[a]=1; |
| 75 | 75 |
a=g.addArc(n3,n5); acolors[a]=0; widths[a]=3; |
| 76 | 76 |
a=g.addArc(n5,n4); acolors[a]=0; widths[a]=1; |
| 77 | 77 |
a=g.addArc(n4,n1); acolors[a]=0; widths[a]=1; |
| 78 | 78 |
a=g.addArc(n2,n4); acolors[a]=1; widths[a]=2; |
| 79 | 79 |
a=g.addArc(n3,n4); acolors[a]=2; widths[a]=1; |
| 80 | 80 |
|
| 81 | 81 |
IdMap<ListDigraph,Node> id(g); |
| 82 | 82 |
|
| 83 | 83 |
// Create .eps files showing the digraph with different options |
| 84 | 84 |
cout << "Create 'graph_to_eps_demo_out_1_pure.eps'" << endl; |
| 85 | 85 |
graphToEps(g,"graph_to_eps_demo_out_1_pure.eps"). |
| 86 | 86 |
coords(coords). |
| 87 | 87 |
title("Sample .eps figure").
|
| 88 |
copyright("(C) 2003-
|
|
| 88 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 89 | 89 |
run(); |
| 90 | 90 |
|
| 91 | 91 |
cout << "Create 'graph_to_eps_demo_out_2.eps'" << endl; |
| 92 | 92 |
graphToEps(g,"graph_to_eps_demo_out_2.eps"). |
| 93 | 93 |
coords(coords). |
| 94 | 94 |
title("Sample .eps figure").
|
| 95 |
copyright("(C) 2003-
|
|
| 95 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 96 | 96 |
absoluteNodeSizes().absoluteArcWidths(). |
| 97 | 97 |
nodeScale(2).nodeSizes(sizes). |
| 98 | 98 |
nodeShapes(shapes). |
| 99 | 99 |
nodeColors(composeMap(palette,colors)). |
| 100 | 100 |
arcColors(composeMap(palette,acolors)). |
| 101 | 101 |
arcWidthScale(.4).arcWidths(widths). |
| 102 | 102 |
nodeTexts(id).nodeTextSize(3). |
| 103 | 103 |
run(); |
| 104 | 104 |
|
| 105 | 105 |
cout << "Create 'graph_to_eps_demo_out_3_arr.eps'" << endl; |
| 106 | 106 |
graphToEps(g,"graph_to_eps_demo_out_3_arr.eps"). |
| 107 | 107 |
title("Sample .eps figure (with arrowheads)").
|
| 108 |
copyright("(C) 2003-
|
|
| 108 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 109 | 109 |
absoluteNodeSizes().absoluteArcWidths(). |
| 110 | 110 |
nodeColors(composeMap(palette,colors)). |
| 111 | 111 |
coords(coords). |
| 112 | 112 |
nodeScale(2).nodeSizes(sizes). |
| 113 | 113 |
nodeShapes(shapes). |
| 114 | 114 |
arcColors(composeMap(palette,acolors)). |
| 115 | 115 |
arcWidthScale(.4).arcWidths(widths). |
| 116 | 116 |
nodeTexts(id).nodeTextSize(3). |
| 117 | 117 |
drawArrows().arrowWidth(2).arrowLength(2). |
| 118 | 118 |
run(); |
| 119 | 119 |
|
| 120 | 120 |
// Add more arcs to the digraph |
| 121 | 121 |
a=g.addArc(n1,n4); acolors[a]=2; widths[a]=1; |
| 122 | 122 |
a=g.addArc(n4,n1); acolors[a]=1; widths[a]=2; |
| 123 | 123 |
|
| 124 | 124 |
a=g.addArc(n1,n2); acolors[a]=1; widths[a]=1; |
| 125 | 125 |
a=g.addArc(n1,n2); acolors[a]=2; widths[a]=1; |
| 126 | 126 |
a=g.addArc(n1,n2); acolors[a]=3; widths[a]=1; |
| 127 | 127 |
a=g.addArc(n1,n2); acolors[a]=4; widths[a]=1; |
| 128 | 128 |
a=g.addArc(n1,n2); acolors[a]=5; widths[a]=1; |
| 129 | 129 |
a=g.addArc(n1,n2); acolors[a]=6; widths[a]=1; |
| 130 | 130 |
a=g.addArc(n1,n2); acolors[a]=7; widths[a]=1; |
| 131 | 131 |
|
| 132 | 132 |
cout << "Create 'graph_to_eps_demo_out_4_par.eps'" << endl; |
| 133 | 133 |
graphToEps(g,"graph_to_eps_demo_out_4_par.eps"). |
| 134 | 134 |
title("Sample .eps figure (parallel arcs)").
|
| 135 |
copyright("(C) 2003-
|
|
| 135 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 136 | 136 |
absoluteNodeSizes().absoluteArcWidths(). |
| 137 | 137 |
nodeShapes(shapes). |
| 138 | 138 |
coords(coords). |
| 139 | 139 |
nodeScale(2).nodeSizes(sizes). |
| 140 | 140 |
nodeColors(composeMap(palette,colors)). |
| 141 | 141 |
arcColors(composeMap(palette,acolors)). |
| 142 | 142 |
arcWidthScale(.4).arcWidths(widths). |
| 143 | 143 |
nodeTexts(id).nodeTextSize(3). |
| 144 | 144 |
enableParallel().parArcDist(1.5). |
| 145 | 145 |
run(); |
| 146 | 146 |
|
| 147 | 147 |
cout << "Create 'graph_to_eps_demo_out_5_par_arr.eps'" << endl; |
| 148 | 148 |
graphToEps(g,"graph_to_eps_demo_out_5_par_arr.eps"). |
| 149 | 149 |
title("Sample .eps figure (parallel arcs and arrowheads)").
|
| 150 |
copyright("(C) 2003-
|
|
| 150 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 151 | 151 |
absoluteNodeSizes().absoluteArcWidths(). |
| 152 | 152 |
nodeScale(2).nodeSizes(sizes). |
| 153 | 153 |
coords(coords). |
| 154 | 154 |
nodeShapes(shapes). |
| 155 | 155 |
nodeColors(composeMap(palette,colors)). |
| 156 | 156 |
arcColors(composeMap(palette,acolors)). |
| 157 | 157 |
arcWidthScale(.3).arcWidths(widths). |
| 158 | 158 |
nodeTexts(id).nodeTextSize(3). |
| 159 | 159 |
enableParallel().parArcDist(1). |
| 160 | 160 |
drawArrows().arrowWidth(1).arrowLength(1). |
| 161 | 161 |
run(); |
| 162 | 162 |
|
| 163 | 163 |
cout << "Create 'graph_to_eps_demo_out_6_par_arr_a4.eps'" << endl; |
| 164 | 164 |
graphToEps(g,"graph_to_eps_demo_out_6_par_arr_a4.eps"). |
| 165 | 165 |
title("Sample .eps figure (fits to A4)").
|
| 166 |
copyright("(C) 2003-
|
|
| 166 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 167 | 167 |
scaleToA4(). |
| 168 | 168 |
absoluteNodeSizes().absoluteArcWidths(). |
| 169 | 169 |
nodeScale(2).nodeSizes(sizes). |
| 170 | 170 |
coords(coords). |
| 171 | 171 |
nodeShapes(shapes). |
| 172 | 172 |
nodeColors(composeMap(palette,colors)). |
| 173 | 173 |
arcColors(composeMap(palette,acolors)). |
| 174 | 174 |
arcWidthScale(.3).arcWidths(widths). |
| 175 | 175 |
nodeTexts(id).nodeTextSize(3). |
| 176 | 176 |
enableParallel().parArcDist(1). |
| 177 | 177 |
drawArrows().arrowWidth(1).arrowLength(1). |
| 178 | 178 |
run(); |
| 179 | 179 |
|
| 180 | 180 |
// Create an .eps file showing the colors of a default Palette |
| 181 | 181 |
ListDigraph h; |
| 182 | 182 |
ListDigraph::NodeMap<int> hcolors(h); |
| 183 | 183 |
ListDigraph::NodeMap<Point> hcoords(h); |
| 184 | 184 |
|
| 185 | 185 |
int cols=int(sqrt(double(palette.size()))); |
| 186 | 186 |
for(int i=0;i<int(paletteW.size());i++) {
|
| 187 | 187 |
Node n=h.addNode(); |
| 188 | 188 |
hcoords[n]=Point(1+i%cols,1+i/cols); |
| 189 | 189 |
hcolors[n]=i; |
| 190 | 190 |
} |
| 191 | 191 |
|
| 192 | 192 |
cout << "Create 'graph_to_eps_demo_out_7_colors.eps'" << endl; |
| 193 | 193 |
graphToEps(h,"graph_to_eps_demo_out_7_colors.eps"). |
| 194 | 194 |
scale(60). |
| 195 | 195 |
title("Sample .eps figure (Palette demo)").
|
| 196 |
copyright("(C) 2003-
|
|
| 196 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 197 | 197 |
coords(hcoords). |
| 198 | 198 |
absoluteNodeSizes().absoluteArcWidths(). |
| 199 | 199 |
nodeScale(.45). |
| 200 | 200 |
distantColorNodeTexts(). |
| 201 | 201 |
nodeTexts(hcolors).nodeTextSize(.6). |
| 202 | 202 |
nodeColors(composeMap(paletteW,hcolors)). |
| 203 | 203 |
run(); |
| 204 | 204 |
|
| 205 | 205 |
return 0; |
| 206 | 206 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup demos |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Demonstrating graph input and output |
| 22 | 22 |
/// |
| 23 | 23 |
/// This program gives an example of how to read and write a digraph |
| 24 | 24 |
/// and additional maps from/to a stream or a file using the |
| 25 | 25 |
/// \ref lgf-format "LGF" format. |
| 26 | 26 |
/// |
| 27 | 27 |
/// The \c "digraph.lgf" file: |
| 28 | 28 |
/// \include digraph.lgf |
| 29 | 29 |
/// |
| 30 | 30 |
/// And the program which reads it and prints the digraph to the |
| 31 | 31 |
/// standard output: |
| 32 | 32 |
/// \include lgf_demo.cc |
| 33 | 33 |
|
| 34 | 34 |
#include <iostream> |
| 35 | 35 |
#include <lemon/smart_graph.h> |
| 36 | 36 |
#include <lemon/lgf_reader.h> |
| 37 | 37 |
#include <lemon/lgf_writer.h> |
| 38 | 38 |
|
| 39 | 39 |
using namespace lemon; |
| 40 | 40 |
|
| 41 | 41 |
int main() {
|
| 42 | 42 |
SmartDigraph g; |
| 43 | 43 |
SmartDigraph::ArcMap<int> cap(g); |
| 44 | 44 |
SmartDigraph::Node s, t; |
| 45 | 45 |
|
| 46 | 46 |
try {
|
| 47 | 47 |
digraphReader(g, "digraph.lgf"). // read the directed graph into g |
| 48 | 48 |
arcMap("capacity", cap). // read the 'capacity' arc map into cap
|
| 49 | 49 |
node("source", s). // read 'source' node to s
|
| 50 | 50 |
node("target", t). // read 'target' node to t
|
| 51 | 51 |
run(); |
| 52 | 52 |
} catch (Exception& error) { // check if there was any error
|
| 53 | 53 |
std::cerr << "Error: " << error.what() << std::endl; |
| 54 | 54 |
return -1; |
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 | 57 |
std::cout << "A digraph is read from 'digraph.lgf'." << std::endl; |
| 58 | 58 |
std::cout << "Number of nodes: " << countNodes(g) << std::endl; |
| 59 | 59 |
std::cout << "Number of arcs: " << countArcs(g) << std::endl; |
| 60 | 60 |
|
| 61 | 61 |
std::cout << "We can write it to the standard output:" << std::endl; |
| 62 | 62 |
|
| 63 | 63 |
digraphWriter(g). // write g to the standard output |
| 64 | 64 |
arcMap("capacity", cap). // write cap into 'capacity'
|
| 65 | 65 |
node("source", s). // write s to 'source'
|
| 66 | 66 |
node("target", t). // write t to 'target'
|
| 67 | 67 |
run(); |
| 68 | 68 |
|
| 69 | 69 |
return 0; |
| 70 | 70 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/*! |
| 20 | 20 |
|
| 21 | 21 |
\page coding_style LEMON Coding Style |
| 22 | 22 |
|
| 23 | 23 |
\section naming_conv Naming Conventions |
| 24 | 24 |
|
| 25 | 25 |
In order to make development easier we have made some conventions |
| 26 | 26 |
according to coding style. These include names of types, classes, |
| 27 | 27 |
functions, variables, constants and exceptions. If these conventions |
| 28 | 28 |
are met in one's code then it is easier to read and maintain |
| 29 | 29 |
it. Please comply with these conventions if you want to contribute |
| 30 | 30 |
developing LEMON library. |
| 31 | 31 |
|
| 32 | 32 |
\note When the coding style requires the capitalization of an abbreviation, |
| 33 | 33 |
only the first letter should be upper case. |
| 34 | 34 |
|
| 35 | 35 |
\code |
| 36 | 36 |
XmlReader |
| 37 | 37 |
\endcode |
| 38 | 38 |
|
| 39 | 39 |
|
| 40 | 40 |
\warning In some cases we diverge from these rules. |
| 41 | 41 |
This is primary done because STL uses different naming convention and |
| 42 | 42 |
in certain cases |
| 43 | 43 |
it is beneficial to provide STL compatible interface. |
| 44 | 44 |
|
| 45 | 45 |
\subsection cs-files File Names |
| 46 | 46 |
|
| 47 | 47 |
The header file names should look like the following. |
| 48 | 48 |
|
| 49 | 49 |
\code |
| 50 | 50 |
header_file.h |
| 51 | 51 |
\endcode |
| 52 | 52 |
|
| 53 | 53 |
Note that all standard LEMON headers are located in the \c lemon subdirectory, |
| 54 | 54 |
so you should include them from C++ source like this: |
| 55 | 55 |
|
| 56 | 56 |
\code |
| 57 | 57 |
#include <lemon/header_file.h> |
| 58 | 58 |
\endcode |
| 59 | 59 |
|
| 60 | 60 |
The source code files use the same style and they have '.cc' extension. |
| 61 | 61 |
|
| 62 | 62 |
\code |
| 63 | 63 |
source_code.cc |
| 64 | 64 |
\endcode |
| 65 | 65 |
|
| 66 | 66 |
\subsection cs-class Classes and other types |
| 67 | 67 |
|
| 68 | 68 |
The name of a class or any type should look like the following. |
| 69 | 69 |
|
| 70 | 70 |
\code |
| 71 | 71 |
AllWordsCapitalizedWithoutUnderscores |
| 72 | 72 |
\endcode |
| 73 | 73 |
|
| 74 | 74 |
\subsection cs-func Methods and other functions |
| 75 | 75 |
|
| 76 | 76 |
The name of a function should look like the following. |
| 77 | 77 |
|
| 78 | 78 |
\code |
| 79 | 79 |
firstWordLowerCaseRestCapitalizedWithoutUnderscores |
| 80 | 80 |
\endcode |
| 81 | 81 |
|
| 82 | 82 |
\subsection cs-funcs Constants, Macros |
| 83 | 83 |
|
| 84 | 84 |
The names of constants and macros should look like the following. |
| 85 | 85 |
|
| 86 | 86 |
\code |
| 87 | 87 |
ALL_UPPER_CASE_WITH_UNDERSCORES |
| 88 | 88 |
\endcode |
| 89 | 89 |
|
| 90 | 90 |
\subsection cs-loc-var Class and instance member variables, auto variables |
| 91 | 91 |
|
| 92 | 92 |
The names of class and instance member variables and auto variables |
| 93 | 93 |
(=variables used locally in methods) should look like the following. |
| 94 | 94 |
|
| 95 | 95 |
\code |
| 96 | 96 |
all_lower_case_with_underscores |
| 97 | 97 |
\endcode |
| 98 | 98 |
|
| 99 | 99 |
\subsection pri-loc-var Private member variables |
| 100 | 100 |
|
| 101 | 101 |
Private member variables should start with underscore |
| 102 | 102 |
|
| 103 | 103 |
\code |
| 104 | 104 |
_start_with_underscores |
| 105 | 105 |
\endcode |
| 106 | 106 |
|
| 107 | 107 |
\subsection cs-excep Exceptions |
| 108 | 108 |
|
| 109 | 109 |
When writing exceptions please comply the following naming conventions. |
| 110 | 110 |
|
| 111 | 111 |
\code |
| 112 | 112 |
ClassNameEndsWithException |
| 113 | 113 |
\endcode |
| 114 | 114 |
|
| 115 | 115 |
or |
| 116 | 116 |
|
| 117 | 117 |
\code |
| 118 | 118 |
ClassNameEndsWithError |
| 119 | 119 |
\endcode |
| 120 | 120 |
|
| 121 | 121 |
\section header-template Template Header File |
| 122 | 122 |
|
| 123 | 123 |
Each LEMON header file should look like this: |
| 124 | 124 |
|
| 125 | 125 |
\include template.h |
| 126 | 126 |
|
| 127 | 127 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/** |
| 20 | 20 |
\dir demo |
| 21 | 21 |
\brief A collection of demo applications. |
| 22 | 22 |
|
| 23 | 23 |
This directory contains several simple demo applications, mainly |
| 24 | 24 |
for educational purposes. |
| 25 | 25 |
*/ |
| 26 | 26 |
|
| 27 | 27 |
/** |
| 28 | 28 |
\dir doc |
| 29 | 29 |
\brief Auxiliary (and the whole generated) documentation. |
| 30 | 30 |
|
| 31 | 31 |
This directory contains some auxiliary pages and the whole generated |
| 32 | 32 |
documentation. |
| 33 | 33 |
*/ |
| 34 | 34 |
|
| 35 | 35 |
/** |
| 36 | 36 |
\dir test |
| 37 | 37 |
\brief Test programs. |
| 38 | 38 |
|
| 39 | 39 |
This directory contains several test programs that check the consistency |
| 40 | 40 |
of the code. |
| 41 | 41 |
*/ |
| 42 | 42 |
|
| 43 | 43 |
/** |
| 44 | 44 |
\dir tools |
| 45 | 45 |
\brief Some useful executables. |
| 46 | 46 |
|
| 47 | 47 |
This directory contains the sources of some useful complete executables. |
| 48 | 48 |
*/ |
| 49 | 49 |
|
| 50 | 50 |
/** |
| 51 | 51 |
\dir lemon |
| 52 | 52 |
\brief Base include directory of LEMON. |
| 53 | 53 |
|
| 54 | 54 |
This is the base directory of LEMON includes, so each include file must be |
| 55 | 55 |
prefixed with this, e.g. |
| 56 | 56 |
\code |
| 57 | 57 |
#include<lemon/list_graph.h> |
| 58 | 58 |
#include<lemon/dijkstra.h> |
| 59 | 59 |
\endcode |
| 60 | 60 |
*/ |
| 61 | 61 |
|
| 62 | 62 |
/** |
| 63 | 63 |
\dir concepts |
| 64 | 64 |
\brief Concept descriptors and checking classes. |
| 65 | 65 |
|
| 66 | 66 |
This directory contains the concept descriptors and concept checking tools. |
| 67 | 67 |
For more information see the \ref concept "Concepts" module. |
| 68 | 68 |
*/ |
| 69 | 69 |
|
| 70 | 70 |
/** |
| 71 | 71 |
\dir bits |
| 72 | 72 |
\brief Auxiliary tools for implementation. |
| 73 | 73 |
|
| 74 | 74 |
This directory contains some auxiliary classes for implementing graphs, |
| 75 | 75 |
maps and some other classes. |
| 76 | 76 |
As a user you typically don't have to deal with these files. |
| 77 | 77 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
namespace lemon {
|
| 20 | 20 |
|
| 21 | 21 |
/** |
| 22 | 22 |
@defgroup datas Data Structures |
| 23 | 23 |
This group describes the several data structures implemented in LEMON. |
| 24 | 24 |
*/ |
| 25 | 25 |
|
| 26 | 26 |
/** |
| 27 | 27 |
@defgroup graphs Graph Structures |
| 28 | 28 |
@ingroup datas |
| 29 | 29 |
\brief Graph structures implemented in LEMON. |
| 30 | 30 |
|
| 31 | 31 |
The implementation of combinatorial algorithms heavily relies on |
| 32 | 32 |
efficient graph implementations. LEMON offers data structures which are |
| 33 | 33 |
planned to be easily used in an experimental phase of implementation studies, |
| 34 | 34 |
and thereafter the program code can be made efficient by small modifications. |
| 35 | 35 |
|
| 36 | 36 |
The most efficient implementation of diverse applications require the |
| 37 | 37 |
usage of different physical graph implementations. These differences |
| 38 | 38 |
appear in the size of graph we require to handle, memory or time usage |
| 39 | 39 |
limitations or in the set of operations through which the graph can be |
| 40 | 40 |
accessed. LEMON provides several physical graph structures to meet |
| 41 | 41 |
the diverging requirements of the possible users. In order to save on |
| 42 | 42 |
running time or on memory usage, some structures may fail to provide |
| 43 | 43 |
some graph features like arc/edge or node deletion. |
| 44 | 44 |
|
| 45 | 45 |
Alteration of standard containers need a very limited number of |
| 46 | 46 |
operations, these together satisfy the everyday requirements. |
| 47 | 47 |
In the case of graph structures, different operations are needed which do |
| 48 | 48 |
not alter the physical graph, but gives another view. If some nodes or |
| 49 | 49 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
| 50 | 50 |
this is the case. It also may happen that in a flow implementation |
| 51 | 51 |
the residual graph can be accessed by another algorithm, or a node-set |
| 52 | 52 |
is to be shrunk for another algorithm. |
| 53 | 53 |
LEMON also provides a variety of graphs for these requirements called |
| 54 | 54 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
| 55 | 55 |
in conjunction with other graph representations. |
| 56 | 56 |
|
| 57 | 57 |
You are free to use the graph structure that fit your requirements |
| 58 | 58 |
the best, most graph algorithms and auxiliary data structures can be used |
| 59 | 59 |
with any graph structure. |
| 60 | 60 |
|
| 61 | 61 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
| 62 | 62 |
*/ |
| 63 | 63 |
|
| 64 | 64 |
/** |
| 65 | 65 |
@defgroup graph_adaptors Adaptor Classes for graphs |
| 66 | 66 |
@ingroup graphs |
| 67 | 67 |
\brief This group contains several adaptor classes for digraphs and graphs |
| 68 | 68 |
|
| 69 | 69 |
The main parts of LEMON are the different graph structures, generic |
| 70 | 70 |
graph algorithms, graph concepts which couple these, and graph |
| 71 | 71 |
adaptors. While the previous notions are more or less clear, the |
| 72 | 72 |
latter one needs further explanation. Graph adaptors are graph classes |
| 73 | 73 |
which serve for considering graph structures in different ways. |
| 74 | 74 |
|
| 75 | 75 |
A short example makes this much clearer. Suppose that we have an |
| 76 | 76 |
instance \c g of a directed graph type say ListDigraph and an algorithm |
| 77 | 77 |
\code |
| 78 | 78 |
template <typename Digraph> |
| 79 | 79 |
int algorithm(const Digraph&); |
| 80 | 80 |
\endcode |
| 81 | 81 |
is needed to run on the reverse oriented graph. It may be expensive |
| 82 | 82 |
(in time or in memory usage) to copy \c g with the reversed |
| 83 | 83 |
arcs. In this case, an adaptor class is used, which (according |
| 84 | 84 |
to LEMON digraph concepts) works as a digraph. The adaptor uses the |
| 85 | 85 |
original digraph structure and digraph operations when methods of the |
| 86 | 86 |
reversed oriented graph are called. This means that the adaptor have |
| 87 | 87 |
minor memory usage, and do not perform sophisticated algorithmic |
| 88 | 88 |
actions. The purpose of it is to give a tool for the cases when a |
| 89 | 89 |
graph have to be used in a specific alteration. If this alteration is |
| 90 | 90 |
obtained by a usual construction like filtering the arc-set or |
| 91 | 91 |
considering a new orientation, then an adaptor is worthwhile to use. |
| 92 | 92 |
To come back to the reverse oriented graph, in this situation |
| 93 | 93 |
\code |
| 94 | 94 |
template<typename Digraph> class ReverseDigraph; |
| 95 | 95 |
\endcode |
| 96 | 96 |
template class can be used. The code looks as follows |
| 97 | 97 |
\code |
| 98 | 98 |
ListDigraph g; |
| 99 | 99 |
ReverseDigraph<ListGraph> rg(g); |
| 100 | 100 |
int result = algorithm(rg); |
| 101 | 101 |
\endcode |
| 102 | 102 |
After running the algorithm, the original graph \c g is untouched. |
| 103 | 103 |
This techniques gives rise to an elegant code, and based on stable |
| 104 | 104 |
graph adaptors, complex algorithms can be implemented easily. |
| 105 | 105 |
|
| 106 | 106 |
In flow, circulation and bipartite matching problems, the residual |
| 107 | 107 |
graph is of particular importance. Combining an adaptor implementing |
| 108 | 108 |
this, shortest path algorithms and minimum mean cycle algorithms, |
| 109 | 109 |
a range of weighted and cardinality optimization algorithms can be |
| 110 | 110 |
obtained. For other examples, the interested user is referred to the |
| 111 | 111 |
detailed documentation of particular adaptors. |
| 112 | 112 |
|
| 113 | 113 |
The behavior of graph adaptors can be very different. Some of them keep |
| 114 | 114 |
capabilities of the original graph while in other cases this would be |
| 115 | 115 |
meaningless. This means that the concepts that they are models of depend |
| 116 | 116 |
on the graph adaptor, and the wrapped graph(s). |
| 117 | 117 |
If an arc of \c rg is deleted, this is carried out by deleting the |
| 118 | 118 |
corresponding arc of \c g, thus the adaptor modifies the original graph. |
| 119 | 119 |
|
| 120 | 120 |
But for a residual graph, this operation has no sense. |
| 121 | 121 |
Let us stand one more example here to simplify your work. |
| 122 | 122 |
RevGraphAdaptor has constructor |
| 123 | 123 |
\code |
| 124 | 124 |
ReverseDigraph(Digraph& digraph); |
| 125 | 125 |
\endcode |
| 126 | 126 |
This means that in a situation, when a <tt>const ListDigraph&</tt> |
| 127 | 127 |
reference to a graph is given, then it have to be instantiated with |
| 128 | 128 |
<tt>Digraph=const ListDigraph</tt>. |
| 129 | 129 |
\code |
| 130 | 130 |
int algorithm1(const ListDigraph& g) {
|
| 131 | 131 |
RevGraphAdaptor<const ListDigraph> rg(g); |
| 132 | 132 |
return algorithm2(rg); |
| 133 | 133 |
} |
| 134 | 134 |
\endcode |
| 135 | 135 |
*/ |
| 136 | 136 |
|
| 137 | 137 |
/** |
| 138 | 138 |
@defgroup semi_adaptors Semi-Adaptor Classes for Graphs |
| 139 | 139 |
@ingroup graphs |
| 140 | 140 |
\brief Graph types between real graphs and graph adaptors. |
| 141 | 141 |
|
| 142 | 142 |
This group describes some graph types between real graphs and graph adaptors. |
| 143 | 143 |
These classes wrap graphs to give new functionality as the adaptors do it. |
| 144 | 144 |
On the other hand they are not light-weight structures as the adaptors. |
| 145 | 145 |
*/ |
| 146 | 146 |
|
| 147 | 147 |
/** |
| 148 | 148 |
@defgroup maps Maps |
| 149 | 149 |
@ingroup datas |
| 150 | 150 |
\brief Map structures implemented in LEMON. |
| 151 | 151 |
|
| 152 | 152 |
This group describes the map structures implemented in LEMON. |
| 153 | 153 |
|
| 154 | 154 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
| 155 | 155 |
new maps from existing ones. |
| 156 | 156 |
|
| 157 | 157 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
| 158 | 158 |
*/ |
| 159 | 159 |
|
| 160 | 160 |
/** |
| 161 | 161 |
@defgroup graph_maps Graph Maps |
| 162 | 162 |
@ingroup maps |
| 163 | 163 |
\brief Special graph-related maps. |
| 164 | 164 |
|
| 165 | 165 |
This group describes maps that are specifically designed to assign |
| 166 | 166 |
values to the nodes and arcs/edges of graphs. |
| 167 | 167 |
|
| 168 | 168 |
If you are looking for the standard graph maps (\c NodeMap, \c ArcMap, |
| 169 | 169 |
\c EdgeMap), see the \ref graph_concepts "Graph Structure Concepts". |
| 170 | 170 |
*/ |
| 171 | 171 |
|
| 172 | 172 |
/** |
| 173 | 173 |
\defgroup map_adaptors Map Adaptors |
| 174 | 174 |
\ingroup maps |
| 175 | 175 |
\brief Tools to create new maps from existing ones |
| 176 | 176 |
|
| 177 | 177 |
This group describes map adaptors that are used to create "implicit" |
| 178 | 178 |
maps from other maps. |
| 179 | 179 |
|
| 180 | 180 |
Most of them are \ref concepts::ReadMap "read-only maps". |
| 181 | 181 |
They can make arithmetic and logical operations between one or two maps |
| 182 | 182 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
| 183 | 183 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
| 184 | 184 |
|
| 185 | 185 |
The typical usage of this classes is passing implicit maps to |
| 186 | 186 |
algorithms. If a function type algorithm is called then the function |
| 187 | 187 |
type map adaptors can be used comfortable. For example let's see the |
| 188 | 188 |
usage of map adaptors with the \c graphToEps() function. |
| 189 | 189 |
\code |
| 190 | 190 |
Color nodeColor(int deg) {
|
| 191 | 191 |
if (deg >= 2) {
|
| 192 | 192 |
return Color(0.5, 0.0, 0.5); |
| 193 | 193 |
} else if (deg == 1) {
|
| 194 | 194 |
return Color(1.0, 0.5, 1.0); |
| 195 | 195 |
} else {
|
| 196 | 196 |
return Color(0.0, 0.0, 0.0); |
| 197 | 197 |
} |
| 198 | 198 |
} |
| 199 | 199 |
|
| 200 | 200 |
Digraph::NodeMap<int> degree_map(graph); |
| 201 | 201 |
|
| 202 | 202 |
graphToEps(graph, "graph.eps") |
| 203 | 203 |
.coords(coords).scaleToA4().undirected() |
| 204 | 204 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
| 205 | 205 |
.run(); |
| 206 | 206 |
\endcode |
| 207 | 207 |
The \c functorToMap() function makes an \c int to \c Color map from the |
| 208 | 208 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
| 209 | 209 |
and the previously created map. The composed map is a proper function to |
| 210 | 210 |
get the color of each node. |
| 211 | 211 |
|
| 212 | 212 |
The usage with class type algorithms is little bit harder. In this |
| 213 | 213 |
case the function type map adaptors can not be used, because the |
| 214 | 214 |
function map adaptors give back temporary objects. |
| 215 | 215 |
\code |
| 216 | 216 |
Digraph graph; |
| 217 | 217 |
|
| 218 | 218 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
| 219 | 219 |
DoubleArcMap length(graph); |
| 220 | 220 |
DoubleArcMap speed(graph); |
| 221 | 221 |
|
| 222 | 222 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
| 223 | 223 |
TimeMap time(length, speed); |
| 224 | 224 |
|
| 225 | 225 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
| 226 | 226 |
dijkstra.run(source, target); |
| 227 | 227 |
\endcode |
| 228 | 228 |
We have a length map and a maximum speed map on the arcs of a digraph. |
| 229 | 229 |
The minimum time to pass the arc can be calculated as the division of |
| 230 | 230 |
the two maps which can be done implicitly with the \c DivMap template |
| 231 | 231 |
class. We use the implicit minimum time map as the length map of the |
| 232 | 232 |
\c Dijkstra algorithm. |
| 233 | 233 |
*/ |
| 234 | 234 |
|
| 235 | 235 |
/** |
| 236 | 236 |
@defgroup matrices Matrices |
| 237 | 237 |
@ingroup datas |
| 238 | 238 |
\brief Two dimensional data storages implemented in LEMON. |
| 239 | 239 |
|
| 240 | 240 |
This group describes two dimensional data storages implemented in LEMON. |
| 241 | 241 |
*/ |
| 242 | 242 |
|
| 243 | 243 |
/** |
| 244 | 244 |
@defgroup paths Path Structures |
| 245 | 245 |
@ingroup datas |
| 246 | 246 |
\brief %Path structures implemented in LEMON. |
| 247 | 247 |
|
| 248 | 248 |
This group describes the path structures implemented in LEMON. |
| 249 | 249 |
|
| 250 | 250 |
LEMON provides flexible data structures to work with paths. |
| 251 | 251 |
All of them have similar interfaces and they can be copied easily with |
| 252 | 252 |
assignment operators and copy constructors. This makes it easy and |
| 253 | 253 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
| 254 | 254 |
any kind of path structure. |
| 255 | 255 |
|
| 256 | 256 |
\sa lemon::concepts::Path |
| 257 | 257 |
*/ |
| 258 | 258 |
|
| 259 | 259 |
/** |
| 260 | 260 |
@defgroup auxdat Auxiliary Data Structures |
| 261 | 261 |
@ingroup datas |
| 262 | 262 |
\brief Auxiliary data structures implemented in LEMON. |
| 263 | 263 |
|
| 264 | 264 |
This group describes some data structures implemented in LEMON in |
| 265 | 265 |
order to make it easier to implement combinatorial algorithms. |
| 266 | 266 |
*/ |
| 267 | 267 |
|
| 268 | 268 |
/** |
| 269 | 269 |
@defgroup algs Algorithms |
| 270 | 270 |
\brief This group describes the several algorithms |
| 271 | 271 |
implemented in LEMON. |
| 272 | 272 |
|
| 273 | 273 |
This group describes the several algorithms |
| 274 | 274 |
implemented in LEMON. |
| 275 | 275 |
*/ |
| 276 | 276 |
|
| 277 | 277 |
/** |
| 278 | 278 |
@defgroup search Graph Search |
| 279 | 279 |
@ingroup algs |
| 280 | 280 |
\brief Common graph search algorithms. |
| 281 | 281 |
|
| 282 | 282 |
This group describes the common graph search algorithms, namely |
| 283 | 283 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS). |
| 284 | 284 |
*/ |
| 285 | 285 |
|
| 286 | 286 |
/** |
| 287 | 287 |
@defgroup shortest_path Shortest Path Algorithms |
| 288 | 288 |
@ingroup algs |
| 289 | 289 |
\brief Algorithms for finding shortest paths. |
| 290 | 290 |
|
| 291 | 291 |
This group describes the algorithms for finding shortest paths in digraphs. |
| 292 | 292 |
|
| 293 | 293 |
- \ref Dijkstra algorithm for finding shortest paths from a source node |
| 294 | 294 |
when all arc lengths are non-negative. |
| 295 | 295 |
- \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths |
| 296 | 296 |
from a source node when arc lenghts can be either positive or negative, |
| 297 | 297 |
but the digraph should not contain directed cycles with negative total |
| 298 | 298 |
length. |
| 299 | 299 |
- \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms |
| 300 | 300 |
for solving the \e all-pairs \e shortest \e paths \e problem when arc |
| 301 | 301 |
lenghts can be either positive or negative, but the digraph should |
| 302 | 302 |
not contain directed cycles with negative total length. |
| 303 | 303 |
- \ref Suurballe A successive shortest path algorithm for finding |
| 304 | 304 |
arc-disjoint paths between two nodes having minimum total length. |
| 305 | 305 |
*/ |
| 306 | 306 |
|
| 307 | 307 |
/** |
| 308 | 308 |
@defgroup max_flow Maximum Flow Algorithms |
| 309 | 309 |
@ingroup algs |
| 310 | 310 |
\brief Algorithms for finding maximum flows. |
| 311 | 311 |
|
| 312 | 312 |
This group describes the algorithms for finding maximum flows and |
| 313 | 313 |
feasible circulations. |
| 314 | 314 |
|
| 315 | 315 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
| 316 | 316 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
| 317 | 317 |
digraph, a \f$cap:A\rightarrow\mathbf{R}^+_0\f$ capacity function and
|
| 318 | 318 |
\f$s, t \in V\f$ source and target nodes. |
| 319 | 319 |
A maximum flow is an \f$f:A\rightarrow\mathbf{R}^+_0\f$ solution of the
|
| 320 | 320 |
following optimization problem. |
| 321 | 321 |
|
| 322 | 322 |
\f[ \max\sum_{a\in\delta_{out}(s)}f(a) - \sum_{a\in\delta_{in}(s)}f(a) \f]
|
| 323 | 323 |
\f[ \sum_{a\in\delta_{out}(v)} f(a) = \sum_{a\in\delta_{in}(v)} f(a)
|
| 324 | 324 |
\qquad \forall v\in V\setminus\{s,t\} \f]
|
| 325 | 325 |
\f[ 0 \leq f(a) \leq cap(a) \qquad \forall a\in A \f] |
| 326 | 326 |
|
| 327 | 327 |
LEMON contains several algorithms for solving maximum flow problems: |
| 328 | 328 |
- \ref EdmondsKarp Edmonds-Karp algorithm. |
| 329 | 329 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm. |
| 330 | 330 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees. |
| 331 | 331 |
- \ref GoldbergTarjan Preflow push-relabel algorithm with dynamic trees. |
| 332 | 332 |
|
| 333 | 333 |
In most cases the \ref Preflow "Preflow" algorithm provides the |
| 334 | 334 |
fastest method for computing a maximum flow. All implementations |
| 335 | 335 |
provides functions to also query the minimum cut, which is the dual |
| 336 | 336 |
problem of the maximum flow. |
| 337 | 337 |
*/ |
| 338 | 338 |
|
| 339 | 339 |
/** |
| 340 | 340 |
@defgroup min_cost_flow Minimum Cost Flow Algorithms |
| 341 | 341 |
@ingroup algs |
| 342 | 342 |
|
| 343 | 343 |
\brief Algorithms for finding minimum cost flows and circulations. |
| 344 | 344 |
|
| 345 | 345 |
This group describes the algorithms for finding minimum cost flows and |
| 346 | 346 |
circulations. |
| 347 | 347 |
|
| 348 | 348 |
The \e minimum \e cost \e flow \e problem is to find a feasible flow of |
| 349 | 349 |
minimum total cost from a set of supply nodes to a set of demand nodes |
| 350 | 350 |
in a network with capacity constraints and arc costs. |
| 351 | 351 |
Formally, let \f$G=(V,A)\f$ be a digraph, |
| 352 | 352 |
\f$lower, upper: A\rightarrow\mathbf{Z}^+_0\f$ denote the lower and
|
| 353 | 353 |
upper bounds for the flow values on the arcs, |
| 354 | 354 |
\f$cost: A\rightarrow\mathbf{Z}^+_0\f$ denotes the cost per unit flow
|
| 355 | 355 |
on the arcs, and |
| 356 | 356 |
\f$supply: V\rightarrow\mathbf{Z}\f$ denotes the supply/demand values
|
| 357 | 357 |
of the nodes. |
| 358 | 358 |
A minimum cost flow is an \f$f:A\rightarrow\mathbf{R}^+_0\f$ solution of
|
| 359 | 359 |
the following optimization problem. |
| 360 | 360 |
|
| 361 | 361 |
\f[ \min\sum_{a\in A} f(a) cost(a) \f]
|
| 362 | 362 |
\f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a) =
|
| 363 | 363 |
supply(v) \qquad \forall v\in V \f] |
| 364 | 364 |
\f[ lower(a) \leq f(a) \leq upper(a) \qquad \forall a\in A \f] |
| 365 | 365 |
|
| 366 | 366 |
LEMON contains several algorithms for solving minimum cost flow problems: |
| 367 | 367 |
- \ref CycleCanceling Cycle-canceling algorithms. |
| 368 | 368 |
- \ref CapacityScaling Successive shortest path algorithm with optional |
| 369 | 369 |
capacity scaling. |
| 370 | 370 |
- \ref CostScaling Push-relabel and augment-relabel algorithms based on |
| 371 | 371 |
cost scaling. |
| 372 | 372 |
- \ref NetworkSimplex Primal network simplex algorithm with various |
| 373 | 373 |
pivot strategies. |
| 374 | 374 |
*/ |
| 375 | 375 |
|
| 376 | 376 |
/** |
| 377 | 377 |
@defgroup min_cut Minimum Cut Algorithms |
| 378 | 378 |
@ingroup algs |
| 379 | 379 |
|
| 380 | 380 |
\brief Algorithms for finding minimum cut in graphs. |
| 381 | 381 |
|
| 382 | 382 |
This group describes the algorithms for finding minimum cut in graphs. |
| 383 | 383 |
|
| 384 | 384 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
| 385 | 385 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
| 386 | 386 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
| 387 | 387 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum
|
| 388 | 388 |
cut is the \f$X\f$ solution of the next optimization problem: |
| 389 | 389 |
|
| 390 | 390 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
|
| 391 | 391 |
\sum_{uv\in A, u\in X, v\not\in X}cap(uv) \f]
|
| 392 | 392 |
|
| 393 | 393 |
LEMON contains several algorithms related to minimum cut problems: |
| 394 | 394 |
|
| 395 | 395 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
| 396 | 396 |
in directed graphs. |
| 397 | 397 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
| 398 | 398 |
calculating minimum cut in undirected graphs. |
| 399 | 399 |
- \ref GomoryHuTree "Gomory-Hu tree computation" for calculating |
| 400 | 400 |
all-pairs minimum cut in undirected graphs. |
| 401 | 401 |
|
| 402 | 402 |
If you want to find minimum cut just between two distinict nodes, |
| 403 | 403 |
see the \ref max_flow "maximum flow problem". |
| 404 | 404 |
*/ |
| 405 | 405 |
|
| 406 | 406 |
/** |
| 407 | 407 |
@defgroup graph_prop Connectivity and Other Graph Properties |
| 408 | 408 |
@ingroup algs |
| 409 | 409 |
\brief Algorithms for discovering the graph properties |
| 410 | 410 |
|
| 411 | 411 |
This group describes the algorithms for discovering the graph properties |
| 412 | 412 |
like connectivity, bipartiteness, euler property, simplicity etc. |
| 413 | 413 |
|
| 414 | 414 |
\image html edge_biconnected_components.png |
| 415 | 415 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
| 416 | 416 |
*/ |
| 417 | 417 |
|
| 418 | 418 |
/** |
| 419 | 419 |
@defgroup planar Planarity Embedding and Drawing |
| 420 | 420 |
@ingroup algs |
| 421 | 421 |
\brief Algorithms for planarity checking, embedding and drawing |
| 422 | 422 |
|
| 423 | 423 |
This group describes the algorithms for planarity checking, |
| 424 | 424 |
embedding and drawing. |
| 425 | 425 |
|
| 426 | 426 |
\image html planar.png |
| 427 | 427 |
\image latex planar.eps "Plane graph" width=\textwidth |
| 428 | 428 |
*/ |
| 429 | 429 |
|
| 430 | 430 |
/** |
| 431 | 431 |
@defgroup matching Matching Algorithms |
| 432 | 432 |
@ingroup algs |
| 433 | 433 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
| 434 | 434 |
|
| 435 | 435 |
This group contains algorithm objects and functions to calculate |
| 436 | 436 |
matchings in graphs and bipartite graphs. The general matching problem is |
| 437 | 437 |
finding a subset of the arcs which does not shares common endpoints. |
| 438 | 438 |
|
| 439 | 439 |
There are several different algorithms for calculate matchings in |
| 440 | 440 |
graphs. The matching problems in bipartite graphs are generally |
| 441 | 441 |
easier than in general graphs. The goal of the matching optimization |
| 442 | 442 |
can be finding maximum cardinality, maximum weight or minimum cost |
| 443 | 443 |
matching. The search can be constrained to find perfect or |
| 444 | 444 |
maximum cardinality matching. |
| 445 | 445 |
|
| 446 | 446 |
The matching algorithms implemented in LEMON: |
| 447 | 447 |
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm |
| 448 | 448 |
for calculating maximum cardinality matching in bipartite graphs. |
| 449 | 449 |
- \ref PrBipartiteMatching Push-relabel algorithm |
| 450 | 450 |
for calculating maximum cardinality matching in bipartite graphs. |
| 451 | 451 |
- \ref MaxWeightedBipartiteMatching |
| 452 | 452 |
Successive shortest path algorithm for calculating maximum weighted |
| 453 | 453 |
matching and maximum weighted bipartite matching in bipartite graphs. |
| 454 | 454 |
- \ref MinCostMaxBipartiteMatching |
| 455 | 455 |
Successive shortest path algorithm for calculating minimum cost maximum |
| 456 | 456 |
matching in bipartite graphs. |
| 457 | 457 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
| 458 | 458 |
maximum cardinality matching in general graphs. |
| 459 | 459 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
| 460 | 460 |
maximum weighted matching in general graphs. |
| 461 | 461 |
- \ref MaxWeightedPerfectMatching |
| 462 | 462 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
| 463 | 463 |
perfect matching in general graphs. |
| 464 | 464 |
|
| 465 | 465 |
\image html bipartite_matching.png |
| 466 | 466 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
| 467 | 467 |
*/ |
| 468 | 468 |
|
| 469 | 469 |
/** |
| 470 | 470 |
@defgroup spantree Minimum Spanning Tree Algorithms |
| 471 | 471 |
@ingroup algs |
| 472 | 472 |
\brief Algorithms for finding a minimum cost spanning tree in a graph. |
| 473 | 473 |
|
| 474 | 474 |
This group describes the algorithms for finding a minimum cost spanning |
| 475 | 475 |
tree in a graph. |
| 476 | 476 |
*/ |
| 477 | 477 |
|
| 478 | 478 |
/** |
| 479 | 479 |
@defgroup auxalg Auxiliary Algorithms |
| 480 | 480 |
@ingroup algs |
| 481 | 481 |
\brief Auxiliary algorithms implemented in LEMON. |
| 482 | 482 |
|
| 483 | 483 |
This group describes some algorithms implemented in LEMON |
| 484 | 484 |
in order to make it easier to implement complex algorithms. |
| 485 | 485 |
*/ |
| 486 | 486 |
|
| 487 | 487 |
/** |
| 488 | 488 |
@defgroup approx Approximation Algorithms |
| 489 | 489 |
@ingroup algs |
| 490 | 490 |
\brief Approximation algorithms. |
| 491 | 491 |
|
| 492 | 492 |
This group describes the approximation and heuristic algorithms |
| 493 | 493 |
implemented in LEMON. |
| 494 | 494 |
*/ |
| 495 | 495 |
|
| 496 | 496 |
/** |
| 497 | 497 |
@defgroup gen_opt_group General Optimization Tools |
| 498 | 498 |
\brief This group describes some general optimization frameworks |
| 499 | 499 |
implemented in LEMON. |
| 500 | 500 |
|
| 501 | 501 |
This group describes some general optimization frameworks |
| 502 | 502 |
implemented in LEMON. |
| 503 | 503 |
*/ |
| 504 | 504 |
|
| 505 | 505 |
/** |
| 506 | 506 |
@defgroup lp_group Lp and Mip Solvers |
| 507 | 507 |
@ingroup gen_opt_group |
| 508 | 508 |
\brief Lp and Mip solver interfaces for LEMON. |
| 509 | 509 |
|
| 510 | 510 |
This group describes Lp and Mip solver interfaces for LEMON. The |
| 511 | 511 |
various LP solvers could be used in the same manner with this |
| 512 | 512 |
interface. |
| 513 | 513 |
*/ |
| 514 | 514 |
|
| 515 | 515 |
/** |
| 516 | 516 |
@defgroup lp_utils Tools for Lp and Mip Solvers |
| 517 | 517 |
@ingroup lp_group |
| 518 | 518 |
\brief Helper tools to the Lp and Mip solvers. |
| 519 | 519 |
|
| 520 | 520 |
This group adds some helper tools to general optimization framework |
| 521 | 521 |
implemented in LEMON. |
| 522 | 522 |
*/ |
| 523 | 523 |
|
| 524 | 524 |
/** |
| 525 | 525 |
@defgroup metah Metaheuristics |
| 526 | 526 |
@ingroup gen_opt_group |
| 527 | 527 |
\brief Metaheuristics for LEMON library. |
| 528 | 528 |
|
| 529 | 529 |
This group describes some metaheuristic optimization tools. |
| 530 | 530 |
*/ |
| 531 | 531 |
|
| 532 | 532 |
/** |
| 533 | 533 |
@defgroup utils Tools and Utilities |
| 534 | 534 |
\brief Tools and utilities for programming in LEMON |
| 535 | 535 |
|
| 536 | 536 |
Tools and utilities for programming in LEMON. |
| 537 | 537 |
*/ |
| 538 | 538 |
|
| 539 | 539 |
/** |
| 540 | 540 |
@defgroup gutils Basic Graph Utilities |
| 541 | 541 |
@ingroup utils |
| 542 | 542 |
\brief Simple basic graph utilities. |
| 543 | 543 |
|
| 544 | 544 |
This group describes some simple basic graph utilities. |
| 545 | 545 |
*/ |
| 546 | 546 |
|
| 547 | 547 |
/** |
| 548 | 548 |
@defgroup misc Miscellaneous Tools |
| 549 | 549 |
@ingroup utils |
| 550 | 550 |
\brief Tools for development, debugging and testing. |
| 551 | 551 |
|
| 552 | 552 |
This group describes several useful tools for development, |
| 553 | 553 |
debugging and testing. |
| 554 | 554 |
*/ |
| 555 | 555 |
|
| 556 | 556 |
/** |
| 557 | 557 |
@defgroup timecount Time Measuring and Counting |
| 558 | 558 |
@ingroup misc |
| 559 | 559 |
\brief Simple tools for measuring the performance of algorithms. |
| 560 | 560 |
|
| 561 | 561 |
This group describes simple tools for measuring the performance |
| 562 | 562 |
of algorithms. |
| 563 | 563 |
*/ |
| 564 | 564 |
|
| 565 | 565 |
/** |
| 566 | 566 |
@defgroup exceptions Exceptions |
| 567 | 567 |
@ingroup utils |
| 568 | 568 |
\brief Exceptions defined in LEMON. |
| 569 | 569 |
|
| 570 | 570 |
This group describes the exceptions defined in LEMON. |
| 571 | 571 |
*/ |
| 572 | 572 |
|
| 573 | 573 |
/** |
| 574 | 574 |
@defgroup io_group Input-Output |
| 575 | 575 |
\brief Graph Input-Output methods |
| 576 | 576 |
|
| 577 | 577 |
This group describes the tools for importing and exporting graphs |
| 578 | 578 |
and graph related data. Now it supports the \ref lgf-format |
| 579 | 579 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
| 580 | 580 |
postscript (EPS) format. |
| 581 | 581 |
*/ |
| 582 | 582 |
|
| 583 | 583 |
/** |
| 584 | 584 |
@defgroup lemon_io LEMON Graph Format |
| 585 | 585 |
@ingroup io_group |
| 586 | 586 |
\brief Reading and writing LEMON Graph Format. |
| 587 | 587 |
|
| 588 | 588 |
This group describes methods for reading and writing |
| 589 | 589 |
\ref lgf-format "LEMON Graph Format". |
| 590 | 590 |
*/ |
| 591 | 591 |
|
| 592 | 592 |
/** |
| 593 | 593 |
@defgroup eps_io Postscript Exporting |
| 594 | 594 |
@ingroup io_group |
| 595 | 595 |
\brief General \c EPS drawer and graph exporter |
| 596 | 596 |
|
| 597 | 597 |
This group describes general \c EPS drawing methods and special |
| 598 | 598 |
graph exporting tools. |
| 599 | 599 |
*/ |
| 600 | 600 |
|
| 601 | 601 |
/** |
| 602 | 602 |
@defgroup dimacs_group DIMACS format |
| 603 | 603 |
@ingroup io_group |
| 604 | 604 |
\brief Read and write files in DIMACS format |
| 605 | 605 |
|
| 606 | 606 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
| 607 | 607 |
*/ |
| 608 | 608 |
|
| 609 | 609 |
/** |
| 610 | 610 |
@defgroup nauty_group NAUTY Format |
| 611 | 611 |
@ingroup io_group |
| 612 | 612 |
\brief Read \e Nauty format |
| 613 | 613 |
|
| 614 | 614 |
Tool to read graphs from \e Nauty format data. |
| 615 | 615 |
*/ |
| 616 | 616 |
|
| 617 | 617 |
/** |
| 618 | 618 |
@defgroup concept Concepts |
| 619 | 619 |
\brief Skeleton classes and concept checking classes |
| 620 | 620 |
|
| 621 | 621 |
This group describes the data/algorithm skeletons and concept checking |
| 622 | 622 |
classes implemented in LEMON. |
| 623 | 623 |
|
| 624 | 624 |
The purpose of the classes in this group is fourfold. |
| 625 | 625 |
|
| 626 | 626 |
- These classes contain the documentations of the %concepts. In order |
| 627 | 627 |
to avoid document multiplications, an implementation of a concept |
| 628 | 628 |
simply refers to the corresponding concept class. |
| 629 | 629 |
|
| 630 | 630 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
| 631 | 631 |
implementation of the %concepts should provide, however completely |
| 632 | 632 |
without implementations and real data structures behind the |
| 633 | 633 |
interface. On the other hand they should provide nothing else. All |
| 634 | 634 |
the algorithms working on a data structure meeting a certain concept |
| 635 | 635 |
should compile with these classes. (Though it will not run properly, |
| 636 | 636 |
of course.) In this way it is easily to check if an algorithm |
| 637 | 637 |
doesn't use any extra feature of a certain implementation. |
| 638 | 638 |
|
| 639 | 639 |
- The concept descriptor classes also provide a <em>checker class</em> |
| 640 | 640 |
that makes it possible to check whether a certain implementation of a |
| 641 | 641 |
concept indeed provides all the required features. |
| 642 | 642 |
|
| 643 | 643 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
| 644 | 644 |
*/ |
| 645 | 645 |
|
| 646 | 646 |
/** |
| 647 | 647 |
@defgroup graph_concepts Graph Structure Concepts |
| 648 | 648 |
@ingroup concept |
| 649 | 649 |
\brief Skeleton and concept checking classes for graph structures |
| 650 | 650 |
|
| 651 | 651 |
This group describes the skeletons and concept checking classes of LEMON's |
| 652 | 652 |
graph structures and helper classes used to implement these. |
| 653 | 653 |
*/ |
| 654 | 654 |
|
| 655 | 655 |
/** |
| 656 | 656 |
@defgroup map_concepts Map Concepts |
| 657 | 657 |
@ingroup concept |
| 658 | 658 |
\brief Skeleton and concept checking classes for maps |
| 659 | 659 |
|
| 660 | 660 |
This group describes the skeletons and concept checking classes of maps. |
| 661 | 661 |
*/ |
| 662 | 662 |
|
| 663 | 663 |
/** |
| 664 | 664 |
\anchor demoprograms |
| 665 | 665 |
|
| 666 | 666 |
@defgroup demos Demo Programs |
| 667 | 667 |
|
| 668 | 668 |
Some demo programs are listed here. Their full source codes can be found in |
| 669 | 669 |
the \c demo subdirectory of the source tree. |
| 670 | 670 |
|
| 671 | 671 |
It order to compile them, use <tt>--enable-demo</tt> configure option when |
| 672 | 672 |
build the library. |
| 673 | 673 |
*/ |
| 674 | 674 |
|
| 675 | 675 |
/** |
| 676 | 676 |
@defgroup tools Standalone Utility Applications |
| 677 | 677 |
|
| 678 | 678 |
Some utility applications are listed here. |
| 679 | 679 |
|
| 680 | 680 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
| 681 | 681 |
them, as well. |
| 682 | 682 |
*/ |
| 683 | 683 |
|
| 684 | 684 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
namespace lemon {
|
| 20 | 20 |
/*! |
| 21 | 21 |
|
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
\page lgf-format LEMON Graph Format (LGF) |
| 25 | 25 |
|
| 26 | 26 |
The \e LGF is a <em>column oriented</em> |
| 27 | 27 |
file format for storing graphs and associated data like |
| 28 | 28 |
node and edge maps. |
| 29 | 29 |
|
| 30 | 30 |
Each line with \c '#' first non-whitespace |
| 31 | 31 |
character is considered as a comment line. |
| 32 | 32 |
|
| 33 | 33 |
Otherwise the file consists of sections starting with |
| 34 | 34 |
a header line. The header lines starts with an \c '@' character followed by the |
| 35 | 35 |
type of section. The standard section types are \c \@nodes, \c |
| 36 | 36 |
\@arcs and \c \@edges |
| 37 | 37 |
and \@attributes. Each header line may also have an optional |
| 38 | 38 |
\e name, which can be use to distinguish the sections of the same |
| 39 | 39 |
type. |
| 40 | 40 |
|
| 41 | 41 |
The standard sections are column oriented, each line consists of |
| 42 | 42 |
<em>token</em>s separated by whitespaces. A token can be \e plain or |
| 43 | 43 |
\e quoted. A plain token is just a sequence of non-whitespace characters, |
| 44 | 44 |
while a quoted token is a |
| 45 | 45 |
character sequence surrounded by double quotes, and it can also |
| 46 | 46 |
contain whitespaces and escape sequences. |
| 47 | 47 |
|
| 48 | 48 |
The \c \@nodes section describes a set of nodes and associated |
| 49 | 49 |
maps. The first is a header line, its columns are the names of the |
| 50 | 50 |
maps appearing in the following lines. |
| 51 | 51 |
One of the maps must be called \c |
| 52 | 52 |
"label", which plays special role in the file. |
| 53 | 53 |
The following |
| 54 | 54 |
non-empty lines until the next section describes nodes of the |
| 55 | 55 |
graph. Each line contains the values of the node maps |
| 56 | 56 |
associated to the current node. |
| 57 | 57 |
|
| 58 | 58 |
\code |
| 59 | 59 |
@nodes |
| 60 | 60 |
label coordinates size title |
| 61 | 61 |
1 (10,20) 10 "First node" |
| 62 | 62 |
2 (80,80) 8 "Second node" |
| 63 | 63 |
3 (40,10) 10 "Third node" |
| 64 | 64 |
\endcode |
| 65 | 65 |
|
| 66 | 66 |
The \c \@arcs section is very similar to the \c \@nodes section, |
| 67 | 67 |
it again starts with a header line describing the names of the maps, |
| 68 | 68 |
but the \c "label" map is not obligatory here. The following lines |
| 69 | 69 |
describe the arcs. The first two tokens of each line are |
| 70 | 70 |
the source and the target node of the arc, respectively, then come the map |
| 71 | 71 |
values. The source and target tokens must be node labels. |
| 72 | 72 |
|
| 73 | 73 |
\code |
| 74 | 74 |
@arcs |
| 75 | 75 |
capacity |
| 76 | 76 |
1 2 16 |
| 77 | 77 |
1 3 12 |
| 78 | 78 |
2 3 18 |
| 79 | 79 |
\endcode |
| 80 | 80 |
|
| 81 | 81 |
The \c \@edges is just a synonym of \c \@arcs. The \@arcs section can |
| 82 | 82 |
also store the edge set of an undirected graph. In such case there is |
| 83 | 83 |
a conventional method for store arc maps in the file, if two columns |
| 84 | 84 |
has the same caption with \c '+' and \c '-' prefix, then these columns |
| 85 | 85 |
can be regarded as the values of an arc map. |
| 86 | 86 |
|
| 87 | 87 |
The \c \@attributes section contains key-value pairs, each line |
| 88 | 88 |
consists of two tokens, an attribute name, and then an attribute |
| 89 | 89 |
value. The value of the attribute could be also a label value of a |
| 90 | 90 |
node or an edge, or even an edge label prefixed with \c '+' or \c '-', |
| 91 | 91 |
which regards to the forward or backward directed arc of the |
| 92 | 92 |
corresponding edge. |
| 93 | 93 |
|
| 94 | 94 |
\code |
| 95 | 95 |
@attributes |
| 96 | 96 |
source 1 |
| 97 | 97 |
target 3 |
| 98 | 98 |
caption "LEMON test digraph" |
| 99 | 99 |
\endcode |
| 100 | 100 |
|
| 101 | 101 |
The \e LGF can contain extra sections, but there is no restriction on |
| 102 | 102 |
the format of such sections. |
| 103 | 103 |
|
| 104 | 104 |
*/ |
| 105 | 105 |
} |
| 106 | 106 |
|
| 107 | 107 |
// LocalWords: whitespace whitespaces |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/** |
| 20 | 20 |
|
| 21 | 21 |
\page license License Terms |
| 22 | 22 |
|
| 23 | 23 |
\verbinclude LICENSE |
| 24 | 24 |
|
| 25 | 25 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/** |
| 20 | 20 |
\mainpage LEMON Documentation |
| 21 | 21 |
|
| 22 | 22 |
\section intro Introduction |
| 23 | 23 |
|
| 24 | 24 |
\subsection whatis What is LEMON |
| 25 | 25 |
|
| 26 | 26 |
LEMON stands for |
| 27 | 27 |
<b>L</b>ibrary of <b>E</b>fficient <b>M</b>odels |
| 28 | 28 |
and <b>O</b>ptimization in <b>N</b>etworks. |
| 29 | 29 |
It is a C++ template |
| 30 | 30 |
library aimed at combinatorial optimization tasks which |
| 31 | 31 |
often involve in working |
| 32 | 32 |
with graphs. |
| 33 | 33 |
|
| 34 | 34 |
<b> |
| 35 | 35 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
| 36 | 36 |
project. |
| 37 | 37 |
You are free to use it in your commercial or |
| 38 | 38 |
non-commercial applications under very permissive |
| 39 | 39 |
\ref license "license terms". |
| 40 | 40 |
</b> |
| 41 | 41 |
|
| 42 | 42 |
\subsection howtoread How to read the documentation |
| 43 | 43 |
|
| 44 | 44 |
If you want to get a quick start and see the most important features then |
| 45 | 45 |
take a look at our \ref quicktour |
| 46 | 46 |
"Quick Tour to LEMON" which will guide you along. |
| 47 | 47 |
|
| 48 | 48 |
If you already feel like using our library, see the page that tells you |
| 49 | 49 |
\ref getstart "How to start using LEMON". |
| 50 | 50 |
|
| 51 | 51 |
If you |
| 52 | 52 |
want to see how LEMON works, see |
| 53 | 53 |
some \ref demoprograms "demo programs". |
| 54 | 54 |
|
| 55 | 55 |
If you know what you are looking for then try to find it under the |
| 56 | 56 |
<a class="el" href="modules.html">Modules</a> |
| 57 | 57 |
section. |
| 58 | 58 |
|
| 59 | 59 |
If you are a user of the old (0.x) series of LEMON, please check out the |
| 60 | 60 |
\ref migration "Migration Guide" for the backward incompatibilities. |
| 61 | 61 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
namespace lemon {
|
| 20 | 20 |
/*! |
| 21 | 21 |
|
| 22 | 22 |
\page migration Migration from the 0.x Series |
| 23 | 23 |
|
| 24 | 24 |
This guide gives an in depth description on what has changed compared |
| 25 | 25 |
to the 0.x release series. |
| 26 | 26 |
|
| 27 | 27 |
Many of these changes adjusted automatically by the |
| 28 | 28 |
<tt>lemon-0.x-to-1.x.sh</tt> tool. Those requiring manual |
| 29 | 29 |
update are typeset <b>boldface</b>. |
| 30 | 30 |
|
| 31 | 31 |
\section migration-graph Graph Related Name Changes |
| 32 | 32 |
|
| 33 | 33 |
- \ref concepts::Digraph "Directed graphs" are called \c Digraph and |
| 34 | 34 |
they have <tt>Arc</tt>s (instead of <tt>Edge</tt>s), while |
| 35 | 35 |
\ref concepts::Graph "undirected graphs" are called \c Graph |
| 36 | 36 |
(instead of \c UGraph) and they have <tt>Edge</tt>s (instead of |
| 37 | 37 |
<tt>UEdge</tt>s). These changes reflected thoroughly everywhere in |
| 38 | 38 |
the library. Namely, |
| 39 | 39 |
- \c Graph -> \c Digraph |
| 40 | 40 |
- \c %ListGraph -> \c ListDigraph, \c %SmartGraph -> \c SmartDigraph etc. |
| 41 | 41 |
- \c UGraph -> \c Graph |
| 42 | 42 |
- \c ListUGraph -> \c ListGraph, \c SmartUGraph -> \c SmartGraph etc. |
| 43 | 43 |
- \c Edge -> \c Arc, \c UEdge -> \c Edge |
| 44 | 44 |
- \c EdgeMap -> \c ArcMap, \c UEdgeMap -> \c EdgeMap |
| 45 | 45 |
- \c EdgeIt -> \c ArcIt, \c UEdgeIt -> \c EdgeIt |
| 46 | 46 |
- Class names and function names containing the words \c graph, |
| 47 | 47 |
\c ugraph, \e edge or \e arc should also be updated. |
| 48 | 48 |
- <b>The two endpoints of an (\e undirected) \c Edge can be obtained by the |
| 49 | 49 |
<tt>u()</tt> and <tt>v()</tt> member function of the graph |
| 50 | 50 |
(instead of <tt>source()</tt> and <tt>target()</tt>). This change |
| 51 | 51 |
must be done by hand.</b> |
| 52 | 52 |
\n Of course, you can still use <tt>source()</tt> and <tt>target()</tt> |
| 53 | 53 |
for <tt>Arc</tt>s (directed edges). |
| 54 | 54 |
|
| 55 | 55 |
\warning |
| 56 | 56 |
<b>The <tt>lemon-0.x-to-1.x.sh</tt> script replaces the words \c graph, |
| 57 | 57 |
\c ugraph, \c edge and \c uedge in your own identifiers and in |
| 58 | 58 |
strings, comments etc. as well as in all LEMON specific identifiers. |
| 59 | 59 |
So use the script carefully and make a backup copy of your source files |
| 60 | 60 |
before applying the script to them.</b> |
| 61 | 61 |
|
| 62 | 62 |
\section migration-lgf LGF tools |
| 63 | 63 |
- The \ref lgf-format "LGF file format" has changed, |
| 64 | 64 |
<tt>\@nodeset</tt> has changed to <tt>\@nodes</tt>, |
| 65 | 65 |
<tt>\@edgeset</tt> and <tt>\@uedgeset</tt> to <tt>\@arcs</tt> or |
| 66 | 66 |
<tt>\@edges</tt>, which become completely equivalents. The |
| 67 | 67 |
<tt>\@nodes</tt>, <tt>\@edges</tt> and <tt>\@uedges</tt> sections are |
| 68 | 68 |
removed from the format, the content of them should be |
| 69 | 69 |
the part of <tt>\@attributes</tt> section. The data fields in |
| 70 | 70 |
the sections must follow a strict format, they must be either character |
| 71 | 71 |
sequences without whitespaces or quoted strings. |
| 72 | 72 |
- The <tt>LemonReader</tt> and <tt>LemonWriter</tt> core interfaces |
| 73 | 73 |
are no longer available. |
| 74 | 74 |
- The implementation of the general section readers and writers has changed |
| 75 | 75 |
they are simple functors now. Beside the old |
| 76 | 76 |
stream based section handling, currently line oriented section |
| 77 | 77 |
reading and writing are also supported. In the |
| 78 | 78 |
section readers the lines must be counted manually. The sections |
| 79 | 79 |
should be read and written with the SectionWriter and SectionReader |
| 80 | 80 |
classes. |
| 81 | 81 |
- Instead of the item readers and writers, item converters should be |
| 82 | 82 |
used. The converters are functors, which map the type to |
| 83 | 83 |
std::string or std::string to the type. The converters for standard |
| 84 | 84 |
containers hasn't yet been implemented in the new LEMON. The converters |
| 85 | 85 |
can return strings in any format, because if it is necessary, the LGF |
| 86 | 86 |
writer and reader will quote and unquote the given value. |
| 87 | 87 |
- The DigraphReader and DigraphWriter can used similarly to the |
| 88 | 88 |
0.x series, however the <tt>read</tt> or <tt>write</tt> prefix of |
| 89 | 89 |
the member functions are removed. |
| 90 | 90 |
- The new LEMON supports the function like interface, the \c |
| 91 | 91 |
digraphReader and \c digraphWriter functions are more convenient than |
| 92 | 92 |
using the classes directly. |
| 93 | 93 |
|
| 94 | 94 |
\section migration-search BFS, DFS and Dijkstra |
| 95 | 95 |
- <b>Using the function interface of BFS, DFS and %Dijkstra both source and |
| 96 | 96 |
target nodes can be given as parameters of the <tt>run()</tt> function |
| 97 | 97 |
(instead of \c bfs(), \c dfs() or \c dijkstra() itself).</b> |
| 98 | 98 |
- \ref named-templ-param "Named class template parameters" of \c Bfs, |
| 99 | 99 |
\c Dfs, \c Dijkstra, \c BfsVisit, \c DfsVisit are renamed to start |
| 100 | 100 |
with "Set" instead of "Def". Namely, |
| 101 | 101 |
- \c DefPredMap -> \c SetPredMap |
| 102 | 102 |
- \c DefDistMap -> \c SetDistMap |
| 103 | 103 |
- \c DefReachedMap -> \c SetReachedMap |
| 104 | 104 |
- \c DefProcessedMap -> \c SetProcessedMap |
| 105 | 105 |
- \c DefHeap -> \c SetHeap |
| 106 | 106 |
- \c DefStandardHeap -> \c SetStandardHeap |
| 107 | 107 |
- \c DefOperationTraits -> \c SetOperationTraits |
| 108 | 108 |
- \c DefProcessedMapToBeDefaultMap -> \c SetStandardProcessedMap |
| 109 | 109 |
|
| 110 | 110 |
\section migration-error Exceptions and Debug tools |
| 111 | 111 |
|
| 112 | 112 |
<b>The class hierarchy of exceptions has largely been simplified. Now, |
| 113 | 113 |
only the i/o related tools may throw exceptions. All other exceptions |
| 114 | 114 |
have been replaced with either the \c LEMON_ASSERT or the \c LEMON_DEBUG |
| 115 | 115 |
macros.</b> |
| 116 | 116 |
|
| 117 | 117 |
<b>On the other hand, the parameter order of constructors of the |
| 118 | 118 |
exceptions has been changed. See \ref IoError and \ref FormatError for |
| 119 | 119 |
more details.</b> |
| 120 | 120 |
|
| 121 | 121 |
\section migration-other Others |
| 122 | 122 |
- <b>The contents of <tt>graph_utils.h</tt> are moved to <tt>core.h</tt> |
| 123 | 123 |
and <tt>maps.h</tt>. <tt>core.h</tt> is included by all graph types, |
| 124 | 124 |
therefore it usually do not have to be included directly.</b> |
| 125 | 125 |
- <b><tt>path_utils.h</tt> is merged to \c path.h.</b> |
| 126 | 126 |
- <b>The semantic of the assignment operations and copy constructors of maps |
| 127 | 127 |
are still under discussion. So, you must copy them by hand (i.e. copy |
| 128 | 128 |
each entry one-by-one)</b> |
| 129 | 129 |
- <b>The parameters of the graph copying tools (i.e. \c GraphCopy, |
| 130 | 130 |
\c DigraphCopy) have to be given in the from-to order.</b> |
| 131 | 131 |
- \c copyDigraph() and \c copyGraph() are renamed to \c digraphCopy() |
| 132 | 132 |
and \c graphCopy(), respectively. |
| 133 | 133 |
- <b>The interface of \ref DynArcLookUp has changed. It is now the same as |
| 134 | 134 |
of \ref ArcLookUp and \ref AllArcLookUp</b> |
| 135 | 135 |
- Some map types should also been renamed. Namely, |
| 136 | 136 |
- \c IntegerMap -> \c RangeMap |
| 137 | 137 |
- \c StdMap -> \c SparseMap |
| 138 | 138 |
- \c FunctorMap -> \c FunctorToMap |
| 139 | 139 |
- \c MapFunctor -> \c MapToFunctor |
| 140 | 140 |
- \c ForkWriteMap -> \c ForkMap |
| 141 | 141 |
- \c StoreBoolMap -> \c LoggerBoolMap |
| 142 | 142 |
- \c dim2::BoundingBox -> \c dim2::Box |
| 143 | 143 |
|
| 144 | 144 |
*/ |
| 145 | 145 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/*! |
| 20 | 20 |
|
| 21 | 21 |
\page named-param Named Parameters |
| 22 | 22 |
|
| 23 | 23 |
\section named-func-param Named Function Parameters |
| 24 | 24 |
|
| 25 | 25 |
Several modern languages provide a convenient way to refer the |
| 26 | 26 |
function parameters by name also when you call the function. It is |
| 27 | 27 |
especially comfortable in case of a function having tons of parameters |
| 28 | 28 |
with natural default values. Sadly, C++ lack this amenity. |
| 29 | 29 |
|
| 30 | 30 |
However, with a crafty trick and with some little |
| 31 | 31 |
inconvenience, it is possible to emulate is. |
| 32 | 32 |
The example below shows how to do it. |
| 33 | 33 |
|
| 34 | 34 |
\code |
| 35 | 35 |
class namedFn |
| 36 | 36 |
{
|
| 37 | 37 |
int _id; |
| 38 | 38 |
double _val; |
| 39 | 39 |
int _dim; |
| 40 | 40 |
|
| 41 | 41 |
public: |
| 42 | 42 |
namedFn() : _id(0), _val(1), _dim(2) {}
|
| 43 | 43 |
namedFn& id(int p) { _id = p ; return *this; }
|
| 44 | 44 |
namedFn& val(double p) { _val = p ; return *this; }
|
| 45 | 45 |
namedFn& dim(int p) { _dim = p ; return *this; }
|
| 46 | 46 |
|
| 47 | 47 |
run() {
|
| 48 | 48 |
std::cout << "Here comes the function itself\n" << |
| 49 | 49 |
<< "With parameters " |
| 50 | 50 |
<< _id << ", " << _val << ", " << _dim << std::endl; |
| 51 | 51 |
} |
| 52 | 52 |
}; |
| 53 | 53 |
\endcode |
| 54 | 54 |
|
| 55 | 55 |
Then you can use it like this. |
| 56 | 56 |
|
| 57 | 57 |
\code |
| 58 | 58 |
namedFn().id(3).val(2).run(); |
| 59 | 59 |
\endcode |
| 60 | 60 |
|
| 61 | 61 |
The trick is obvious, each "named parameter" changes one component of |
| 62 | 62 |
the underlying class, then gives back a reference to it. Finally, |
| 63 | 63 |
<tt>run()</tt> executes the algorithm itself. |
| 64 | 64 |
|
| 65 | 65 |
\note Although it is a class, namedFn is used pretty much like as it were |
| 66 | 66 |
a function. That it why we called it namedFn instead of \c NamedFn. |
| 67 | 67 |
|
| 68 | 68 |
\note In fact, the final <tt>.run()</tt> could be made unnecessary, |
| 69 | 69 |
because the algorithm could also be implemented in the destructor of |
| 70 | 70 |
\c namedFn instead. This however would make it impossible to implement |
| 71 | 71 |
functions with return values, and would also cause serious problems when |
| 72 | 72 |
implementing \ref named-templ-func-param "named template parameters". |
| 73 | 73 |
<b>Therefore, by convention, <tt>.run()</tt> must be used |
| 74 | 74 |
explicitly to execute a function having named parameters |
| 75 | 75 |
everywhere in LEMON.</b> |
| 76 | 76 |
|
| 77 | 77 |
\section named-templ-func-param Named Function Template Parameters |
| 78 | 78 |
|
| 79 | 79 |
A named parameter can also be a template function. The usage is |
| 80 | 80 |
exactly the same, but the implementation behind is a kind of black |
| 81 | 81 |
magic and they are the dirtiest part of the LEMON code. |
| 82 | 82 |
|
| 83 | 83 |
You will probably never need to know how it works, but if you really |
| 84 | 84 |
committed, have a look at \ref lemon/graph_to_eps.h for an example. |
| 85 | 85 |
|
| 86 | 86 |
\section traits-classes Traits Classes |
| 87 | 87 |
|
| 88 | 88 |
A similar game can also be played when defining classes. In this case |
| 89 | 89 |
the type of the class attributes can be changed. Initially we have to |
| 90 | 90 |
define a special class called <em>Traits Class</em> defining the |
| 91 | 91 |
default type of the attributes. Then the types of these attributes can |
| 92 | 92 |
be changed in the same way as described in the next section. |
| 93 | 93 |
|
| 94 | 94 |
See \ref lemon::DijkstraDefaultTraits for an |
| 95 | 95 |
example how a traits class implementation looks like. |
| 96 | 96 |
|
| 97 | 97 |
\section named-templ-param Named Class Template Parameters |
| 98 | 98 |
|
| 99 | 99 |
If we would like to change the type of an attribute in a class that |
| 100 | 100 |
was instantiated by using a traits class as a template parameter, and |
| 101 | 101 |
the class contains named parameters, we do not have to instantiate again |
| 102 | 102 |
the class with new traits class, but instead adaptor classes can |
| 103 | 103 |
be used as shown in the following example. |
| 104 | 104 |
|
| 105 | 105 |
\code |
| 106 | 106 |
Dijkstra<>::SetPredMap<NullMap<Node,Arc> >::Create |
| 107 | 107 |
\endcode |
| 108 | 108 |
|
| 109 | 109 |
It can also be used in conjunction with other named template |
| 110 | 110 |
parameters in arbitrary order. |
| 111 | 111 |
|
| 112 | 112 |
\code |
| 113 | 113 |
Dijkstra<>::SetDistMap<MyMap>::SetPredMap<NullMap<Node,Arc> >::Create |
| 114 | 114 |
\endcode |
| 115 | 115 |
|
| 116 | 116 |
The result will be an instantiated Dijkstra class, in which the |
| 117 | 117 |
DistMap and the PredMap is modified. |
| 118 | 118 |
|
| 119 | 119 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/// The namespace of LEMON |
| 20 | 20 |
|
| 21 | 21 |
/// The namespace of LEMON |
| 22 | 22 |
/// |
| 23 | 23 |
namespace lemon {
|
| 24 | 24 |
|
| 25 | 25 |
/// The namespace of LEMON concepts and concept checking classes |
| 26 | 26 |
|
| 27 | 27 |
/// The namespace of LEMON concepts and concept checking classes |
| 28 | 28 |
/// |
| 29 | 29 |
namespace concepts {}
|
| 30 | 30 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_TEMPLATE_H |
| 20 | 20 |
#define LEMON_TEMPLATE_H |
| 21 | 21 |
|
| 22 | 22 |
#endif // LEMON_TEMPLATE_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ADAPTORS_H |
| 20 | 20 |
#define LEMON_ADAPTORS_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup graph_adaptors |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Several graph adaptors |
| 25 | 25 |
/// |
| 26 | 26 |
/// This file contains several useful adaptors for digraphs and graphs. |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/maps.h> |
| 30 | 30 |
#include <lemon/bits/variant.h> |
| 31 | 31 |
|
| 32 | 32 |
#include <lemon/bits/graph_adaptor_extender.h> |
| 33 | 33 |
#include <lemon/tolerance.h> |
| 34 | 34 |
|
| 35 | 35 |
#include <algorithm> |
| 36 | 36 |
|
| 37 | 37 |
namespace lemon {
|
| 38 | 38 |
|
| 39 | 39 |
template<typename _Digraph> |
| 40 | 40 |
class DigraphAdaptorBase {
|
| 41 | 41 |
public: |
| 42 | 42 |
typedef _Digraph Digraph; |
| 43 | 43 |
typedef DigraphAdaptorBase Adaptor; |
| 44 | 44 |
typedef Digraph ParentDigraph; |
| 45 | 45 |
|
| 46 | 46 |
protected: |
| 47 | 47 |
Digraph* _digraph; |
| 48 | 48 |
DigraphAdaptorBase() : _digraph(0) { }
|
| 49 | 49 |
void setDigraph(Digraph& digraph) { _digraph = &digraph; }
|
| 50 | 50 |
|
| 51 | 51 |
public: |
| 52 | 52 |
DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { }
|
| 53 | 53 |
|
| 54 | 54 |
typedef typename Digraph::Node Node; |
| 55 | 55 |
typedef typename Digraph::Arc Arc; |
| 56 | 56 |
|
| 57 | 57 |
void first(Node& i) const { _digraph->first(i); }
|
| 58 | 58 |
void first(Arc& i) const { _digraph->first(i); }
|
| 59 | 59 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); }
|
| 60 | 60 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); }
|
| 61 | 61 |
|
| 62 | 62 |
void next(Node& i) const { _digraph->next(i); }
|
| 63 | 63 |
void next(Arc& i) const { _digraph->next(i); }
|
| 64 | 64 |
void nextIn(Arc& i) const { _digraph->nextIn(i); }
|
| 65 | 65 |
void nextOut(Arc& i) const { _digraph->nextOut(i); }
|
| 66 | 66 |
|
| 67 | 67 |
Node source(const Arc& a) const { return _digraph->source(a); }
|
| 68 | 68 |
Node target(const Arc& a) const { return _digraph->target(a); }
|
| 69 | 69 |
|
| 70 | 70 |
typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
| 71 | 71 |
int nodeNum() const { return _digraph->nodeNum(); }
|
| 72 | 72 |
|
| 73 | 73 |
typedef EdgeNumTagIndicator<Digraph> EdgeNumTag; |
| 74 | 74 |
int arcNum() const { return _digraph->arcNum(); }
|
| 75 | 75 |
|
| 76 | 76 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
| 77 | 77 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) {
|
| 78 | 78 |
return _digraph->findArc(u, v, prev); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
Node addNode() { return _digraph->addNode(); }
|
| 82 | 82 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
|
| 83 | 83 |
|
| 84 | 84 |
void erase(const Node& n) const { _digraph->erase(n); }
|
| 85 | 85 |
void erase(const Arc& a) const { _digraph->erase(a); }
|
| 86 | 86 |
|
| 87 | 87 |
void clear() const { _digraph->clear(); }
|
| 88 | 88 |
|
| 89 | 89 |
int id(const Node& n) const { return _digraph->id(n); }
|
| 90 | 90 |
int id(const Arc& a) const { return _digraph->id(a); }
|
| 91 | 91 |
|
| 92 | 92 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
|
| 93 | 93 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); }
|
| 94 | 94 |
|
| 95 | 95 |
int maxNodeId() const { return _digraph->maxNodeId(); }
|
| 96 | 96 |
int maxArcId() const { return _digraph->maxArcId(); }
|
| 97 | 97 |
|
| 98 | 98 |
typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier; |
| 99 | 99 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
|
| 100 | 100 |
|
| 101 | 101 |
typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier; |
| 102 | 102 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
|
| 103 | 103 |
|
| 104 | 104 |
template <typename _Value> |
| 105 | 105 |
class NodeMap : public Digraph::template NodeMap<_Value> {
|
| 106 | 106 |
public: |
| 107 | 107 |
|
| 108 | 108 |
typedef typename Digraph::template NodeMap<_Value> Parent; |
| 109 | 109 |
|
| 110 | 110 |
explicit NodeMap(const Adaptor& adaptor) |
| 111 | 111 |
: Parent(*adaptor._digraph) {}
|
| 112 | 112 |
|
| 113 | 113 |
NodeMap(const Adaptor& adaptor, const _Value& value) |
| 114 | 114 |
: Parent(*adaptor._digraph, value) { }
|
| 115 | 115 |
|
| 116 | 116 |
private: |
| 117 | 117 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 118 | 118 |
return operator=<NodeMap>(cmap); |
| 119 | 119 |
} |
| 120 | 120 |
|
| 121 | 121 |
template <typename CMap> |
| 122 | 122 |
NodeMap& operator=(const CMap& cmap) {
|
| 123 | 123 |
Parent::operator=(cmap); |
| 124 | 124 |
return *this; |
| 125 | 125 |
} |
| 126 | 126 |
|
| 127 | 127 |
}; |
| 128 | 128 |
|
| 129 | 129 |
template <typename _Value> |
| 130 | 130 |
class ArcMap : public Digraph::template ArcMap<_Value> {
|
| 131 | 131 |
public: |
| 132 | 132 |
|
| 133 | 133 |
typedef typename Digraph::template ArcMap<_Value> Parent; |
| 134 | 134 |
|
| 135 | 135 |
explicit ArcMap(const Adaptor& adaptor) |
| 136 | 136 |
: Parent(*adaptor._digraph) {}
|
| 137 | 137 |
|
| 138 | 138 |
ArcMap(const Adaptor& adaptor, const _Value& value) |
| 139 | 139 |
: Parent(*adaptor._digraph, value) {}
|
| 140 | 140 |
|
| 141 | 141 |
private: |
| 142 | 142 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 143 | 143 |
return operator=<ArcMap>(cmap); |
| 144 | 144 |
} |
| 145 | 145 |
|
| 146 | 146 |
template <typename CMap> |
| 147 | 147 |
ArcMap& operator=(const CMap& cmap) {
|
| 148 | 148 |
Parent::operator=(cmap); |
| 149 | 149 |
return *this; |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
}; |
| 153 | 153 |
|
| 154 | 154 |
}; |
| 155 | 155 |
|
| 156 | 156 |
template<typename _Graph> |
| 157 | 157 |
class GraphAdaptorBase {
|
| 158 | 158 |
public: |
| 159 | 159 |
typedef _Graph Graph; |
| 160 | 160 |
typedef Graph ParentGraph; |
| 161 | 161 |
|
| 162 | 162 |
protected: |
| 163 | 163 |
Graph* _graph; |
| 164 | 164 |
|
| 165 | 165 |
GraphAdaptorBase() : _graph(0) {}
|
| 166 | 166 |
|
| 167 | 167 |
void setGraph(Graph& graph) { _graph = &graph; }
|
| 168 | 168 |
|
| 169 | 169 |
public: |
| 170 | 170 |
GraphAdaptorBase(Graph& graph) : _graph(&graph) {}
|
| 171 | 171 |
|
| 172 | 172 |
typedef typename Graph::Node Node; |
| 173 | 173 |
typedef typename Graph::Arc Arc; |
| 174 | 174 |
typedef typename Graph::Edge Edge; |
| 175 | 175 |
|
| 176 | 176 |
void first(Node& i) const { _graph->first(i); }
|
| 177 | 177 |
void first(Arc& i) const { _graph->first(i); }
|
| 178 | 178 |
void first(Edge& i) const { _graph->first(i); }
|
| 179 | 179 |
void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
|
| 180 | 180 |
void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
|
| 181 | 181 |
void firstInc(Edge &i, bool &d, const Node &n) const {
|
| 182 | 182 |
_graph->firstInc(i, d, n); |
| 183 | 183 |
} |
| 184 | 184 |
|
| 185 | 185 |
void next(Node& i) const { _graph->next(i); }
|
| 186 | 186 |
void next(Arc& i) const { _graph->next(i); }
|
| 187 | 187 |
void next(Edge& i) const { _graph->next(i); }
|
| 188 | 188 |
void nextIn(Arc& i) const { _graph->nextIn(i); }
|
| 189 | 189 |
void nextOut(Arc& i) const { _graph->nextOut(i); }
|
| 190 | 190 |
void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
|
| 191 | 191 |
|
| 192 | 192 |
Node u(const Edge& e) const { return _graph->u(e); }
|
| 193 | 193 |
Node v(const Edge& e) const { return _graph->v(e); }
|
| 194 | 194 |
|
| 195 | 195 |
Node source(const Arc& a) const { return _graph->source(a); }
|
| 196 | 196 |
Node target(const Arc& a) const { return _graph->target(a); }
|
| 197 | 197 |
|
| 198 | 198 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
| 199 | 199 |
int nodeNum() const { return _graph->nodeNum(); }
|
| 200 | 200 |
|
| 201 | 201 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
| 202 | 202 |
int arcNum() const { return _graph->arcNum(); }
|
| 203 | 203 |
int edgeNum() const { return _graph->edgeNum(); }
|
| 204 | 204 |
|
| 205 | 205 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
| 206 | 206 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) {
|
| 207 | 207 |
return _graph->findArc(u, v, prev); |
| 208 | 208 |
} |
| 209 | 209 |
Edge findEdge(const Node& u, const Node& v, const Edge& prev = INVALID) {
|
| 210 | 210 |
return _graph->findEdge(u, v, prev); |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
Node addNode() { return _graph->addNode(); }
|
| 214 | 214 |
Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); }
|
| 215 | 215 |
|
| 216 | 216 |
void erase(const Node& i) { _graph->erase(i); }
|
| 217 | 217 |
void erase(const Edge& i) { _graph->erase(i); }
|
| 218 | 218 |
|
| 219 | 219 |
void clear() { _graph->clear(); }
|
| 220 | 220 |
|
| 221 | 221 |
bool direction(const Arc& a) const { return _graph->direction(a); }
|
| 222 | 222 |
Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); }
|
| 223 | 223 |
|
| 224 | 224 |
int id(const Node& v) const { return _graph->id(v); }
|
| 225 | 225 |
int id(const Arc& a) const { return _graph->id(a); }
|
| 226 | 226 |
int id(const Edge& e) const { return _graph->id(e); }
|
| 227 | 227 |
|
| 228 | 228 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
|
| 229 | 229 |
Arc arcFromId(int ix) const { return _graph->arcFromId(ix); }
|
| 230 | 230 |
Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); }
|
| 231 | 231 |
|
| 232 | 232 |
int maxNodeId() const { return _graph->maxNodeId(); }
|
| 233 | 233 |
int maxArcId() const { return _graph->maxArcId(); }
|
| 234 | 234 |
int maxEdgeId() const { return _graph->maxEdgeId(); }
|
| 235 | 235 |
|
| 236 | 236 |
typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier; |
| 237 | 237 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
|
| 238 | 238 |
|
| 239 | 239 |
typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier; |
| 240 | 240 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
|
| 241 | 241 |
|
| 242 | 242 |
typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier; |
| 243 | 243 |
EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
|
| 244 | 244 |
|
| 245 | 245 |
template <typename _Value> |
| 246 | 246 |
class NodeMap : public Graph::template NodeMap<_Value> {
|
| 247 | 247 |
public: |
| 248 | 248 |
typedef typename Graph::template NodeMap<_Value> Parent; |
| 249 | 249 |
explicit NodeMap(const GraphAdaptorBase<Graph>& adapter) |
| 250 | 250 |
: Parent(*adapter._graph) {}
|
| 251 | 251 |
NodeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
| 252 | 252 |
: Parent(*adapter._graph, value) {}
|
| 253 | 253 |
|
| 254 | 254 |
private: |
| 255 | 255 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 256 | 256 |
return operator=<NodeMap>(cmap); |
| 257 | 257 |
} |
| 258 | 258 |
|
| 259 | 259 |
template <typename CMap> |
| 260 | 260 |
NodeMap& operator=(const CMap& cmap) {
|
| 261 | 261 |
Parent::operator=(cmap); |
| 262 | 262 |
return *this; |
| 263 | 263 |
} |
| 264 | 264 |
|
| 265 | 265 |
}; |
| 266 | 266 |
|
| 267 | 267 |
template <typename _Value> |
| 268 | 268 |
class ArcMap : public Graph::template ArcMap<_Value> {
|
| 269 | 269 |
public: |
| 270 | 270 |
typedef typename Graph::template ArcMap<_Value> Parent; |
| 271 | 271 |
explicit ArcMap(const GraphAdaptorBase<Graph>& adapter) |
| 272 | 272 |
: Parent(*adapter._graph) {}
|
| 273 | 273 |
ArcMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
| 274 | 274 |
: Parent(*adapter._graph, value) {}
|
| 275 | 275 |
|
| 276 | 276 |
private: |
| 277 | 277 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 278 | 278 |
return operator=<ArcMap>(cmap); |
| 279 | 279 |
} |
| 280 | 280 |
|
| 281 | 281 |
template <typename CMap> |
| 282 | 282 |
ArcMap& operator=(const CMap& cmap) {
|
| 283 | 283 |
Parent::operator=(cmap); |
| 284 | 284 |
return *this; |
| 285 | 285 |
} |
| 286 | 286 |
}; |
| 287 | 287 |
|
| 288 | 288 |
template <typename _Value> |
| 289 | 289 |
class EdgeMap : public Graph::template EdgeMap<_Value> {
|
| 290 | 290 |
public: |
| 291 | 291 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
| 292 | 292 |
explicit EdgeMap(const GraphAdaptorBase<Graph>& adapter) |
| 293 | 293 |
: Parent(*adapter._graph) {}
|
| 294 | 294 |
EdgeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
| 295 | 295 |
: Parent(*adapter._graph, value) {}
|
| 296 | 296 |
|
| 297 | 297 |
private: |
| 298 | 298 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 299 | 299 |
return operator=<EdgeMap>(cmap); |
| 300 | 300 |
} |
| 301 | 301 |
|
| 302 | 302 |
template <typename CMap> |
| 303 | 303 |
EdgeMap& operator=(const CMap& cmap) {
|
| 304 | 304 |
Parent::operator=(cmap); |
| 305 | 305 |
return *this; |
| 306 | 306 |
} |
| 307 | 307 |
}; |
| 308 | 308 |
|
| 309 | 309 |
}; |
| 310 | 310 |
|
| 311 | 311 |
template <typename _Digraph> |
| 312 | 312 |
class ReverseDigraphBase : public DigraphAdaptorBase<_Digraph> {
|
| 313 | 313 |
public: |
| 314 | 314 |
typedef _Digraph Digraph; |
| 315 | 315 |
typedef DigraphAdaptorBase<_Digraph> Parent; |
| 316 | 316 |
protected: |
| 317 | 317 |
ReverseDigraphBase() : Parent() { }
|
| 318 | 318 |
public: |
| 319 | 319 |
typedef typename Parent::Node Node; |
| 320 | 320 |
typedef typename Parent::Arc Arc; |
| 321 | 321 |
|
| 322 | 322 |
void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); }
|
| 323 | 323 |
void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); }
|
| 324 | 324 |
|
| 325 | 325 |
void nextIn(Arc& a) const { Parent::nextOut(a); }
|
| 326 | 326 |
void nextOut(Arc& a) const { Parent::nextIn(a); }
|
| 327 | 327 |
|
| 328 | 328 |
Node source(const Arc& a) const { return Parent::target(a); }
|
| 329 | 329 |
Node target(const Arc& a) const { return Parent::source(a); }
|
| 330 | 330 |
|
| 331 | 331 |
Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
|
| 332 | 332 |
|
| 333 | 333 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
| 334 | 334 |
Arc findArc(const Node& u, const Node& v, |
| 335 | 335 |
const Arc& prev = INVALID) {
|
| 336 | 336 |
return Parent::findArc(v, u, prev); |
| 337 | 337 |
} |
| 338 | 338 |
|
| 339 | 339 |
}; |
| 340 | 340 |
|
| 341 | 341 |
/// \ingroup graph_adaptors |
| 342 | 342 |
/// |
| 343 | 343 |
/// \brief A digraph adaptor which reverses the orientation of the arcs. |
| 344 | 344 |
/// |
| 345 | 345 |
/// ReverseDigraph reverses the arcs in the adapted digraph. The |
| 346 | 346 |
/// SubDigraph is conform to the \ref concepts::Digraph |
| 347 | 347 |
/// "Digraph concept". |
| 348 | 348 |
/// |
| 349 | 349 |
/// \tparam _Digraph It must be conform to the \ref concepts::Digraph |
| 350 | 350 |
/// "Digraph concept". The type can be specified to be const. |
| 351 | 351 |
template<typename _Digraph> |
| 352 | 352 |
class ReverseDigraph : |
| 353 | 353 |
public DigraphAdaptorExtender<ReverseDigraphBase<_Digraph> > {
|
| 354 | 354 |
public: |
| 355 | 355 |
typedef _Digraph Digraph; |
| 356 | 356 |
typedef DigraphAdaptorExtender< |
| 357 | 357 |
ReverseDigraphBase<_Digraph> > Parent; |
| 358 | 358 |
protected: |
| 359 | 359 |
ReverseDigraph() { }
|
| 360 | 360 |
public: |
| 361 | 361 |
|
| 362 | 362 |
/// \brief Constructor |
| 363 | 363 |
/// |
| 364 | 364 |
/// Creates a reverse digraph adaptor for the given digraph |
| 365 | 365 |
explicit ReverseDigraph(Digraph& digraph) {
|
| 366 | 366 |
Parent::setDigraph(digraph); |
| 367 | 367 |
} |
| 368 | 368 |
}; |
| 369 | 369 |
|
| 370 | 370 |
/// \brief Just gives back a reverse digraph adaptor |
| 371 | 371 |
/// |
| 372 | 372 |
/// Just gives back a reverse digraph adaptor |
| 373 | 373 |
template<typename Digraph> |
| 374 | 374 |
ReverseDigraph<const Digraph> reverseDigraph(const Digraph& digraph) {
|
| 375 | 375 |
return ReverseDigraph<const Digraph>(digraph); |
| 376 | 376 |
} |
| 377 | 377 |
|
| 378 | 378 |
template <typename _Digraph, typename _NodeFilterMap, |
| 379 | 379 |
typename _ArcFilterMap, bool _checked = true> |
| 380 | 380 |
class SubDigraphBase : public DigraphAdaptorBase<_Digraph> {
|
| 381 | 381 |
public: |
| 382 | 382 |
typedef _Digraph Digraph; |
| 383 | 383 |
typedef _NodeFilterMap NodeFilterMap; |
| 384 | 384 |
typedef _ArcFilterMap ArcFilterMap; |
| 385 | 385 |
|
| 386 | 386 |
typedef SubDigraphBase Adaptor; |
| 387 | 387 |
typedef DigraphAdaptorBase<_Digraph> Parent; |
| 388 | 388 |
protected: |
| 389 | 389 |
NodeFilterMap* _node_filter; |
| 390 | 390 |
ArcFilterMap* _arc_filter; |
| 391 | 391 |
SubDigraphBase() |
| 392 | 392 |
: Parent(), _node_filter(0), _arc_filter(0) { }
|
| 393 | 393 |
|
| 394 | 394 |
void setNodeFilterMap(NodeFilterMap& node_filter) {
|
| 395 | 395 |
_node_filter = &node_filter; |
| 396 | 396 |
} |
| 397 | 397 |
void setArcFilterMap(ArcFilterMap& arc_filter) {
|
| 398 | 398 |
_arc_filter = &arc_filter; |
| 399 | 399 |
} |
| 400 | 400 |
|
| 401 | 401 |
public: |
| 402 | 402 |
|
| 403 | 403 |
typedef typename Parent::Node Node; |
| 404 | 404 |
typedef typename Parent::Arc Arc; |
| 405 | 405 |
|
| 406 | 406 |
void first(Node& i) const {
|
| 407 | 407 |
Parent::first(i); |
| 408 | 408 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 409 | 409 |
} |
| 410 | 410 |
|
| 411 | 411 |
void first(Arc& i) const {
|
| 412 | 412 |
Parent::first(i); |
| 413 | 413 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 414 | 414 |
|| !(*_node_filter)[Parent::source(i)] |
| 415 | 415 |
|| !(*_node_filter)[Parent::target(i)])) |
| 416 | 416 |
Parent::next(i); |
| 417 | 417 |
} |
| 418 | 418 |
|
| 419 | 419 |
void firstIn(Arc& i, const Node& n) const {
|
| 420 | 420 |
Parent::firstIn(i, n); |
| 421 | 421 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 422 | 422 |
|| !(*_node_filter)[Parent::source(i)])) |
| 423 | 423 |
Parent::nextIn(i); |
| 424 | 424 |
} |
| 425 | 425 |
|
| 426 | 426 |
void firstOut(Arc& i, const Node& n) const {
|
| 427 | 427 |
Parent::firstOut(i, n); |
| 428 | 428 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 429 | 429 |
|| !(*_node_filter)[Parent::target(i)])) |
| 430 | 430 |
Parent::nextOut(i); |
| 431 | 431 |
} |
| 432 | 432 |
|
| 433 | 433 |
void next(Node& i) const {
|
| 434 | 434 |
Parent::next(i); |
| 435 | 435 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 436 | 436 |
} |
| 437 | 437 |
|
| 438 | 438 |
void next(Arc& i) const {
|
| 439 | 439 |
Parent::next(i); |
| 440 | 440 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 441 | 441 |
|| !(*_node_filter)[Parent::source(i)] |
| 442 | 442 |
|| !(*_node_filter)[Parent::target(i)])) |
| 443 | 443 |
Parent::next(i); |
| 444 | 444 |
} |
| 445 | 445 |
|
| 446 | 446 |
void nextIn(Arc& i) const {
|
| 447 | 447 |
Parent::nextIn(i); |
| 448 | 448 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 449 | 449 |
|| !(*_node_filter)[Parent::source(i)])) |
| 450 | 450 |
Parent::nextIn(i); |
| 451 | 451 |
} |
| 452 | 452 |
|
| 453 | 453 |
void nextOut(Arc& i) const {
|
| 454 | 454 |
Parent::nextOut(i); |
| 455 | 455 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 456 | 456 |
|| !(*_node_filter)[Parent::target(i)])) |
| 457 | 457 |
Parent::nextOut(i); |
| 458 | 458 |
} |
| 459 | 459 |
|
| 460 | 460 |
void hide(const Node& n) const { _node_filter->set(n, false); }
|
| 461 | 461 |
void hide(const Arc& a) const { _arc_filter->set(a, false); }
|
| 462 | 462 |
|
| 463 | 463 |
void unHide(const Node& n) const { _node_filter->set(n, true); }
|
| 464 | 464 |
void unHide(const Arc& a) const { _arc_filter->set(a, true); }
|
| 465 | 465 |
|
| 466 | 466 |
bool hidden(const Node& n) const { return !(*_node_filter)[n]; }
|
| 467 | 467 |
bool hidden(const Arc& a) const { return !(*_arc_filter)[a]; }
|
| 468 | 468 |
|
| 469 | 469 |
typedef False NodeNumTag; |
| 470 | 470 |
typedef False EdgeNumTag; |
| 471 | 471 |
|
| 472 | 472 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
| 473 | 473 |
Arc findArc(const Node& source, const Node& target, |
| 474 | 474 |
const Arc& prev = INVALID) {
|
| 475 | 475 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
|
| 476 | 476 |
return INVALID; |
| 477 | 477 |
} |
| 478 | 478 |
Arc arc = Parent::findArc(source, target, prev); |
| 479 | 479 |
while (arc != INVALID && !(*_arc_filter)[arc]) {
|
| 480 | 480 |
arc = Parent::findArc(source, target, arc); |
| 481 | 481 |
} |
| 482 | 482 |
return arc; |
| 483 | 483 |
} |
| 484 | 484 |
|
| 485 | 485 |
template <typename _Value> |
| 486 | 486 |
class NodeMap : public SubMapExtender<Adaptor, |
| 487 | 487 |
typename Parent::template NodeMap<_Value> > {
|
| 488 | 488 |
public: |
| 489 | 489 |
typedef _Value Value; |
| 490 | 490 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 491 | 491 |
template NodeMap<Value> > MapParent; |
| 492 | 492 |
|
| 493 | 493 |
NodeMap(const Adaptor& adaptor) |
| 494 | 494 |
: MapParent(adaptor) {}
|
| 495 | 495 |
NodeMap(const Adaptor& adaptor, const Value& value) |
| 496 | 496 |
: MapParent(adaptor, value) {}
|
| 497 | 497 |
|
| 498 | 498 |
private: |
| 499 | 499 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 500 | 500 |
return operator=<NodeMap>(cmap); |
| 501 | 501 |
} |
| 502 | 502 |
|
| 503 | 503 |
template <typename CMap> |
| 504 | 504 |
NodeMap& operator=(const CMap& cmap) {
|
| 505 | 505 |
MapParent::operator=(cmap); |
| 506 | 506 |
return *this; |
| 507 | 507 |
} |
| 508 | 508 |
}; |
| 509 | 509 |
|
| 510 | 510 |
template <typename _Value> |
| 511 | 511 |
class ArcMap : public SubMapExtender<Adaptor, |
| 512 | 512 |
typename Parent::template ArcMap<_Value> > {
|
| 513 | 513 |
public: |
| 514 | 514 |
typedef _Value Value; |
| 515 | 515 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 516 | 516 |
template ArcMap<Value> > MapParent; |
| 517 | 517 |
|
| 518 | 518 |
ArcMap(const Adaptor& adaptor) |
| 519 | 519 |
: MapParent(adaptor) {}
|
| 520 | 520 |
ArcMap(const Adaptor& adaptor, const Value& value) |
| 521 | 521 |
: MapParent(adaptor, value) {}
|
| 522 | 522 |
|
| 523 | 523 |
private: |
| 524 | 524 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 525 | 525 |
return operator=<ArcMap>(cmap); |
| 526 | 526 |
} |
| 527 | 527 |
|
| 528 | 528 |
template <typename CMap> |
| 529 | 529 |
ArcMap& operator=(const CMap& cmap) {
|
| 530 | 530 |
MapParent::operator=(cmap); |
| 531 | 531 |
return *this; |
| 532 | 532 |
} |
| 533 | 533 |
}; |
| 534 | 534 |
|
| 535 | 535 |
}; |
| 536 | 536 |
|
| 537 | 537 |
template <typename _Digraph, typename _NodeFilterMap, typename _ArcFilterMap> |
| 538 | 538 |
class SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false> |
| 539 | 539 |
: public DigraphAdaptorBase<_Digraph> {
|
| 540 | 540 |
public: |
| 541 | 541 |
typedef _Digraph Digraph; |
| 542 | 542 |
typedef _NodeFilterMap NodeFilterMap; |
| 543 | 543 |
typedef _ArcFilterMap ArcFilterMap; |
| 544 | 544 |
|
| 545 | 545 |
typedef SubDigraphBase Adaptor; |
| 546 | 546 |
typedef DigraphAdaptorBase<Digraph> Parent; |
| 547 | 547 |
protected: |
| 548 | 548 |
NodeFilterMap* _node_filter; |
| 549 | 549 |
ArcFilterMap* _arc_filter; |
| 550 | 550 |
SubDigraphBase() |
| 551 | 551 |
: Parent(), _node_filter(0), _arc_filter(0) { }
|
| 552 | 552 |
|
| 553 | 553 |
void setNodeFilterMap(NodeFilterMap& node_filter) {
|
| 554 | 554 |
_node_filter = &node_filter; |
| 555 | 555 |
} |
| 556 | 556 |
void setArcFilterMap(ArcFilterMap& arc_filter) {
|
| 557 | 557 |
_arc_filter = &arc_filter; |
| 558 | 558 |
} |
| 559 | 559 |
|
| 560 | 560 |
public: |
| 561 | 561 |
|
| 562 | 562 |
typedef typename Parent::Node Node; |
| 563 | 563 |
typedef typename Parent::Arc Arc; |
| 564 | 564 |
|
| 565 | 565 |
void first(Node& i) const {
|
| 566 | 566 |
Parent::first(i); |
| 567 | 567 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 568 | 568 |
} |
| 569 | 569 |
|
| 570 | 570 |
void first(Arc& i) const {
|
| 571 | 571 |
Parent::first(i); |
| 572 | 572 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
| 573 | 573 |
} |
| 574 | 574 |
|
| 575 | 575 |
void firstIn(Arc& i, const Node& n) const {
|
| 576 | 576 |
Parent::firstIn(i, n); |
| 577 | 577 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
| 578 | 578 |
} |
| 579 | 579 |
|
| 580 | 580 |
void firstOut(Arc& i, const Node& n) const {
|
| 581 | 581 |
Parent::firstOut(i, n); |
| 582 | 582 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
| 583 | 583 |
} |
| 584 | 584 |
|
| 585 | 585 |
void next(Node& i) const {
|
| 586 | 586 |
Parent::next(i); |
| 587 | 587 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 588 | 588 |
} |
| 589 | 589 |
void next(Arc& i) const {
|
| 590 | 590 |
Parent::next(i); |
| 591 | 591 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
| 592 | 592 |
} |
| 593 | 593 |
void nextIn(Arc& i) const {
|
| 594 | 594 |
Parent::nextIn(i); |
| 595 | 595 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
| 596 | 596 |
} |
| 597 | 597 |
|
| 598 | 598 |
void nextOut(Arc& i) const {
|
| 599 | 599 |
Parent::nextOut(i); |
| 600 | 600 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
| 601 | 601 |
} |
| 602 | 602 |
|
| 603 | 603 |
void hide(const Node& n) const { _node_filter->set(n, false); }
|
| 604 | 604 |
void hide(const Arc& e) const { _arc_filter->set(e, false); }
|
| 605 | 605 |
|
| 606 | 606 |
void unHide(const Node& n) const { _node_filter->set(n, true); }
|
| 607 | 607 |
void unHide(const Arc& e) const { _arc_filter->set(e, true); }
|
| 608 | 608 |
|
| 609 | 609 |
bool hidden(const Node& n) const { return !(*_node_filter)[n]; }
|
| 610 | 610 |
bool hidden(const Arc& e) const { return !(*_arc_filter)[e]; }
|
| 611 | 611 |
|
| 612 | 612 |
typedef False NodeNumTag; |
| 613 | 613 |
typedef False EdgeNumTag; |
| 614 | 614 |
|
| 615 | 615 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
| 616 | 616 |
Arc findArc(const Node& source, const Node& target, |
| 617 | 617 |
const Arc& prev = INVALID) {
|
| 618 | 618 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
|
| 619 | 619 |
return INVALID; |
| 620 | 620 |
} |
| 621 | 621 |
Arc arc = Parent::findArc(source, target, prev); |
| 622 | 622 |
while (arc != INVALID && !(*_arc_filter)[arc]) {
|
| 623 | 623 |
arc = Parent::findArc(source, target, arc); |
| 624 | 624 |
} |
| 625 | 625 |
return arc; |
| 626 | 626 |
} |
| 627 | 627 |
|
| 628 | 628 |
template <typename _Value> |
| 629 | 629 |
class NodeMap : public SubMapExtender<Adaptor, |
| 630 | 630 |
typename Parent::template NodeMap<_Value> > {
|
| 631 | 631 |
public: |
| 632 | 632 |
typedef _Value Value; |
| 633 | 633 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 634 | 634 |
template NodeMap<Value> > MapParent; |
| 635 | 635 |
|
| 636 | 636 |
NodeMap(const Adaptor& adaptor) |
| 637 | 637 |
: MapParent(adaptor) {}
|
| 638 | 638 |
NodeMap(const Adaptor& adaptor, const Value& value) |
| 639 | 639 |
: MapParent(adaptor, value) {}
|
| 640 | 640 |
|
| 641 | 641 |
private: |
| 642 | 642 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 643 | 643 |
return operator=<NodeMap>(cmap); |
| 644 | 644 |
} |
| 645 | 645 |
|
| 646 | 646 |
template <typename CMap> |
| 647 | 647 |
NodeMap& operator=(const CMap& cmap) {
|
| 648 | 648 |
MapParent::operator=(cmap); |
| 649 | 649 |
return *this; |
| 650 | 650 |
} |
| 651 | 651 |
}; |
| 652 | 652 |
|
| 653 | 653 |
template <typename _Value> |
| 654 | 654 |
class ArcMap : public SubMapExtender<Adaptor, |
| 655 | 655 |
typename Parent::template ArcMap<_Value> > {
|
| 656 | 656 |
public: |
| 657 | 657 |
typedef _Value Value; |
| 658 | 658 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 659 | 659 |
template ArcMap<Value> > MapParent; |
| 660 | 660 |
|
| 661 | 661 |
ArcMap(const Adaptor& adaptor) |
| 662 | 662 |
: MapParent(adaptor) {}
|
| 663 | 663 |
ArcMap(const Adaptor& adaptor, const Value& value) |
| 664 | 664 |
: MapParent(adaptor, value) {}
|
| 665 | 665 |
|
| 666 | 666 |
private: |
| 667 | 667 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 668 | 668 |
return operator=<ArcMap>(cmap); |
| 669 | 669 |
} |
| 670 | 670 |
|
| 671 | 671 |
template <typename CMap> |
| 672 | 672 |
ArcMap& operator=(const CMap& cmap) {
|
| 673 | 673 |
MapParent::operator=(cmap); |
| 674 | 674 |
return *this; |
| 675 | 675 |
} |
| 676 | 676 |
}; |
| 677 | 677 |
|
| 678 | 678 |
}; |
| 679 | 679 |
|
| 680 | 680 |
/// \ingroup graph_adaptors |
| 681 | 681 |
/// |
| 682 | 682 |
/// \brief An adaptor for hiding nodes and arcs in a digraph |
| 683 | 683 |
/// |
| 684 | 684 |
/// SubDigraph hides nodes and arcs in a digraph. A bool node map |
| 685 | 685 |
/// and a bool arc map must be specified, which define the filters |
| 686 | 686 |
/// for nodes and arcs. Just the nodes and arcs with true value are |
| 687 | 687 |
/// shown in the subdigraph. The SubDigraph is conform to the \ref |
| 688 | 688 |
/// concepts::Digraph "Digraph concept". If the \c _checked parameter |
| 689 | 689 |
/// is true, then the arcs incident to filtered nodes are also |
| 690 | 690 |
/// filtered out. |
| 691 | 691 |
/// |
| 692 | 692 |
/// \tparam _Digraph It must be conform to the \ref |
| 693 | 693 |
/// concepts::Digraph "Digraph concept". The type can be specified |
| 694 | 694 |
/// to const. |
| 695 | 695 |
/// \tparam _NodeFilterMap A bool valued node map of the the adapted digraph. |
| 696 | 696 |
/// \tparam _ArcFilterMap A bool valued arc map of the the adapted digraph. |
| 697 | 697 |
/// \tparam _checked If the parameter is false then the arc filtering |
| 698 | 698 |
/// is not checked with respect to node filter. Otherwise, each arc |
| 699 | 699 |
/// is automatically filtered, which is incident to a filtered node. |
| 700 | 700 |
/// |
| 701 | 701 |
/// \see FilterNodes |
| 702 | 702 |
/// \see FilterArcs |
| 703 | 703 |
template<typename _Digraph, |
| 704 | 704 |
typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, |
| 705 | 705 |
typename _ArcFilterMap = typename _Digraph::template ArcMap<bool>, |
| 706 | 706 |
bool _checked = true> |
| 707 | 707 |
class SubDigraph |
| 708 | 708 |
: public DigraphAdaptorExtender< |
| 709 | 709 |
SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, _checked> > {
|
| 710 | 710 |
public: |
| 711 | 711 |
typedef _Digraph Digraph; |
| 712 | 712 |
typedef _NodeFilterMap NodeFilterMap; |
| 713 | 713 |
typedef _ArcFilterMap ArcFilterMap; |
| 714 | 714 |
|
| 715 | 715 |
typedef DigraphAdaptorExtender< |
| 716 | 716 |
SubDigraphBase<Digraph, NodeFilterMap, ArcFilterMap, _checked> > |
| 717 | 717 |
Parent; |
| 718 | 718 |
|
| 719 | 719 |
typedef typename Parent::Node Node; |
| 720 | 720 |
typedef typename Parent::Arc Arc; |
| 721 | 721 |
|
| 722 | 722 |
protected: |
| 723 | 723 |
SubDigraph() { }
|
| 724 | 724 |
public: |
| 725 | 725 |
|
| 726 | 726 |
/// \brief Constructor |
| 727 | 727 |
/// |
| 728 | 728 |
/// Creates a subdigraph for the given digraph with |
| 729 | 729 |
/// given node and arc map filters. |
| 730 | 730 |
SubDigraph(Digraph& digraph, NodeFilterMap& node_filter, |
| 731 | 731 |
ArcFilterMap& arc_filter) {
|
| 732 | 732 |
setDigraph(digraph); |
| 733 | 733 |
setNodeFilterMap(node_filter); |
| 734 | 734 |
setArcFilterMap(arc_filter); |
| 735 | 735 |
} |
| 736 | 736 |
|
| 737 | 737 |
/// \brief Hides the node of the graph |
| 738 | 738 |
/// |
| 739 | 739 |
/// This function hides \c n in the digraph, i.e. the iteration |
| 740 | 740 |
/// jumps over it. This is done by simply setting the value of \c n |
| 741 | 741 |
/// to be false in the corresponding node-map. |
| 742 | 742 |
void hide(const Node& n) const { Parent::hide(n); }
|
| 743 | 743 |
|
| 744 | 744 |
/// \brief Hides the arc of the graph |
| 745 | 745 |
/// |
| 746 | 746 |
/// This function hides \c a in the digraph, i.e. the iteration |
| 747 | 747 |
/// jumps over it. This is done by simply setting the value of \c a |
| 748 | 748 |
/// to be false in the corresponding arc-map. |
| 749 | 749 |
void hide(const Arc& a) const { Parent::hide(a); }
|
| 750 | 750 |
|
| 751 | 751 |
/// \brief Unhides the node of the graph |
| 752 | 752 |
/// |
| 753 | 753 |
/// The value of \c n is set to be true in the node-map which stores |
| 754 | 754 |
/// hide information. If \c n was hidden previuosly, then it is shown |
| 755 | 755 |
/// again |
| 756 | 756 |
void unHide(const Node& n) const { Parent::unHide(n); }
|
| 757 | 757 |
|
| 758 | 758 |
/// \brief Unhides the arc of the graph |
| 759 | 759 |
/// |
| 760 | 760 |
/// The value of \c a is set to be true in the arc-map which stores |
| 761 | 761 |
/// hide information. If \c a was hidden previuosly, then it is shown |
| 762 | 762 |
/// again |
| 763 | 763 |
void unHide(const Arc& a) const { Parent::unHide(a); }
|
| 764 | 764 |
|
| 765 | 765 |
/// \brief Returns true if \c n is hidden. |
| 766 | 766 |
/// |
| 767 | 767 |
/// Returns true if \c n is hidden. |
| 768 | 768 |
/// |
| 769 | 769 |
bool hidden(const Node& n) const { return Parent::hidden(n); }
|
| 770 | 770 |
|
| 771 | 771 |
/// \brief Returns true if \c a is hidden. |
| 772 | 772 |
/// |
| 773 | 773 |
/// Returns true if \c a is hidden. |
| 774 | 774 |
/// |
| 775 | 775 |
bool hidden(const Arc& a) const { return Parent::hidden(a); }
|
| 776 | 776 |
|
| 777 | 777 |
}; |
| 778 | 778 |
|
| 779 | 779 |
/// \brief Just gives back a subdigraph |
| 780 | 780 |
/// |
| 781 | 781 |
/// Just gives back a subdigraph |
| 782 | 782 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
| 783 | 783 |
SubDigraph<const Digraph, NodeFilterMap, ArcFilterMap> |
| 784 | 784 |
subDigraph(const Digraph& digraph, NodeFilterMap& nfm, ArcFilterMap& afm) {
|
| 785 | 785 |
return SubDigraph<const Digraph, NodeFilterMap, ArcFilterMap> |
| 786 | 786 |
(digraph, nfm, afm); |
| 787 | 787 |
} |
| 788 | 788 |
|
| 789 | 789 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
| 790 | 790 |
SubDigraph<const Digraph, const NodeFilterMap, ArcFilterMap> |
| 791 | 791 |
subDigraph(const Digraph& digraph, |
| 792 | 792 |
const NodeFilterMap& nfm, ArcFilterMap& afm) {
|
| 793 | 793 |
return SubDigraph<const Digraph, const NodeFilterMap, ArcFilterMap> |
| 794 | 794 |
(digraph, nfm, afm); |
| 795 | 795 |
} |
| 796 | 796 |
|
| 797 | 797 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
| 798 | 798 |
SubDigraph<const Digraph, NodeFilterMap, const ArcFilterMap> |
| 799 | 799 |
subDigraph(const Digraph& digraph, |
| 800 | 800 |
NodeFilterMap& nfm, const ArcFilterMap& afm) {
|
| 801 | 801 |
return SubDigraph<const Digraph, NodeFilterMap, const ArcFilterMap> |
| 802 | 802 |
(digraph, nfm, afm); |
| 803 | 803 |
} |
| 804 | 804 |
|
| 805 | 805 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
| 806 | 806 |
SubDigraph<const Digraph, const NodeFilterMap, const ArcFilterMap> |
| 807 | 807 |
subDigraph(const Digraph& digraph, |
| 808 | 808 |
const NodeFilterMap& nfm, const ArcFilterMap& afm) {
|
| 809 | 809 |
return SubDigraph<const Digraph, const NodeFilterMap, |
| 810 | 810 |
const ArcFilterMap>(digraph, nfm, afm); |
| 811 | 811 |
} |
| 812 | 812 |
|
| 813 | 813 |
|
| 814 | 814 |
template <typename _Graph, typename NodeFilterMap, |
| 815 | 815 |
typename EdgeFilterMap, bool _checked = true> |
| 816 | 816 |
class SubGraphBase : public GraphAdaptorBase<_Graph> {
|
| 817 | 817 |
public: |
| 818 | 818 |
typedef _Graph Graph; |
| 819 | 819 |
typedef SubGraphBase Adaptor; |
| 820 | 820 |
typedef GraphAdaptorBase<_Graph> Parent; |
| 821 | 821 |
protected: |
| 822 | 822 |
|
| 823 | 823 |
NodeFilterMap* _node_filter_map; |
| 824 | 824 |
EdgeFilterMap* _edge_filter_map; |
| 825 | 825 |
|
| 826 | 826 |
SubGraphBase() |
| 827 | 827 |
: Parent(), _node_filter_map(0), _edge_filter_map(0) { }
|
| 828 | 828 |
|
| 829 | 829 |
void setNodeFilterMap(NodeFilterMap& node_filter_map) {
|
| 830 | 830 |
_node_filter_map=&node_filter_map; |
| 831 | 831 |
} |
| 832 | 832 |
void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
|
| 833 | 833 |
_edge_filter_map=&edge_filter_map; |
| 834 | 834 |
} |
| 835 | 835 |
|
| 836 | 836 |
public: |
| 837 | 837 |
|
| 838 | 838 |
typedef typename Parent::Node Node; |
| 839 | 839 |
typedef typename Parent::Arc Arc; |
| 840 | 840 |
typedef typename Parent::Edge Edge; |
| 841 | 841 |
|
| 842 | 842 |
void first(Node& i) const {
|
| 843 | 843 |
Parent::first(i); |
| 844 | 844 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
| 845 | 845 |
} |
| 846 | 846 |
|
| 847 | 847 |
void first(Arc& i) const {
|
| 848 | 848 |
Parent::first(i); |
| 849 | 849 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 850 | 850 |
|| !(*_node_filter_map)[Parent::source(i)] |
| 851 | 851 |
|| !(*_node_filter_map)[Parent::target(i)])) |
| 852 | 852 |
Parent::next(i); |
| 853 | 853 |
} |
| 854 | 854 |
|
| 855 | 855 |
void first(Edge& i) const {
|
| 856 | 856 |
Parent::first(i); |
| 857 | 857 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 858 | 858 |
|| !(*_node_filter_map)[Parent::u(i)] |
| 859 | 859 |
|| !(*_node_filter_map)[Parent::v(i)])) |
| 860 | 860 |
Parent::next(i); |
| 861 | 861 |
} |
| 862 | 862 |
|
| 863 | 863 |
void firstIn(Arc& i, const Node& n) const {
|
| 864 | 864 |
Parent::firstIn(i, n); |
| 865 | 865 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 866 | 866 |
|| !(*_node_filter_map)[Parent::source(i)])) |
| 867 | 867 |
Parent::nextIn(i); |
| 868 | 868 |
} |
| 869 | 869 |
|
| 870 | 870 |
void firstOut(Arc& i, const Node& n) const {
|
| 871 | 871 |
Parent::firstOut(i, n); |
| 872 | 872 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 873 | 873 |
|| !(*_node_filter_map)[Parent::target(i)])) |
| 874 | 874 |
Parent::nextOut(i); |
| 875 | 875 |
} |
| 876 | 876 |
|
| 877 | 877 |
void firstInc(Edge& i, bool& d, const Node& n) const {
|
| 878 | 878 |
Parent::firstInc(i, d, n); |
| 879 | 879 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 880 | 880 |
|| !(*_node_filter_map)[Parent::u(i)] |
| 881 | 881 |
|| !(*_node_filter_map)[Parent::v(i)])) |
| 882 | 882 |
Parent::nextInc(i, d); |
| 883 | 883 |
} |
| 884 | 884 |
|
| 885 | 885 |
void next(Node& i) const {
|
| 886 | 886 |
Parent::next(i); |
| 887 | 887 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
| 888 | 888 |
} |
| 889 | 889 |
|
| 890 | 890 |
void next(Arc& i) const {
|
| 891 | 891 |
Parent::next(i); |
| 892 | 892 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 893 | 893 |
|| !(*_node_filter_map)[Parent::source(i)] |
| 894 | 894 |
|| !(*_node_filter_map)[Parent::target(i)])) |
| 895 | 895 |
Parent::next(i); |
| 896 | 896 |
} |
| 897 | 897 |
|
| 898 | 898 |
void next(Edge& i) const {
|
| 899 | 899 |
Parent::next(i); |
| 900 | 900 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 901 | 901 |
|| !(*_node_filter_map)[Parent::u(i)] |
| 902 | 902 |
|| !(*_node_filter_map)[Parent::v(i)])) |
| 903 | 903 |
Parent::next(i); |
| 904 | 904 |
} |
| 905 | 905 |
|
| 906 | 906 |
void nextIn(Arc& i) const {
|
| 907 | 907 |
Parent::nextIn(i); |
| 908 | 908 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 909 | 909 |
|| !(*_node_filter_map)[Parent::source(i)])) |
| 910 | 910 |
Parent::nextIn(i); |
| 911 | 911 |
} |
| 912 | 912 |
|
| 913 | 913 |
void nextOut(Arc& i) const {
|
| 914 | 914 |
Parent::nextOut(i); |
| 915 | 915 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 916 | 916 |
|| !(*_node_filter_map)[Parent::target(i)])) |
| 917 | 917 |
Parent::nextOut(i); |
| 918 | 918 |
} |
| 919 | 919 |
|
| 920 | 920 |
void nextInc(Edge& i, bool& d) const {
|
| 921 | 921 |
Parent::nextInc(i, d); |
| 922 | 922 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
| 923 | 923 |
|| !(*_node_filter_map)[Parent::u(i)] |
| 924 | 924 |
|| !(*_node_filter_map)[Parent::v(i)])) |
| 925 | 925 |
Parent::nextInc(i, d); |
| 926 | 926 |
} |
| 927 | 927 |
|
| 928 | 928 |
void hide(const Node& n) const { _node_filter_map->set(n, false); }
|
| 929 | 929 |
void hide(const Edge& e) const { _edge_filter_map->set(e, false); }
|
| 930 | 930 |
|
| 931 | 931 |
void unHide(const Node& n) const { _node_filter_map->set(n, true); }
|
| 932 | 932 |
void unHide(const Edge& e) const { _edge_filter_map->set(e, true); }
|
| 933 | 933 |
|
| 934 | 934 |
bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; }
|
| 935 | 935 |
bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; }
|
| 936 | 936 |
|
| 937 | 937 |
typedef False NodeNumTag; |
| 938 | 938 |
typedef False EdgeNumTag; |
| 939 | 939 |
|
| 940 | 940 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
| 941 | 941 |
Arc findArc(const Node& u, const Node& v, |
| 942 | 942 |
const Arc& prev = INVALID) {
|
| 943 | 943 |
if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
|
| 944 | 944 |
return INVALID; |
| 945 | 945 |
} |
| 946 | 946 |
Arc arc = Parent::findArc(u, v, prev); |
| 947 | 947 |
while (arc != INVALID && !(*_edge_filter_map)[arc]) {
|
| 948 | 948 |
arc = Parent::findArc(u, v, arc); |
| 949 | 949 |
} |
| 950 | 950 |
return arc; |
| 951 | 951 |
} |
| 952 | 952 |
Edge findEdge(const Node& u, const Node& v, |
| 953 | 953 |
const Edge& prev = INVALID) {
|
| 954 | 954 |
if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
|
| 955 | 955 |
return INVALID; |
| 956 | 956 |
} |
| 957 | 957 |
Edge edge = Parent::findEdge(u, v, prev); |
| 958 | 958 |
while (edge != INVALID && !(*_edge_filter_map)[edge]) {
|
| 959 | 959 |
edge = Parent::findEdge(u, v, edge); |
| 960 | 960 |
} |
| 961 | 961 |
return edge; |
| 962 | 962 |
} |
| 963 | 963 |
|
| 964 | 964 |
template <typename _Value> |
| 965 | 965 |
class NodeMap : public SubMapExtender<Adaptor, |
| 966 | 966 |
typename Parent::template NodeMap<_Value> > {
|
| 967 | 967 |
public: |
| 968 | 968 |
typedef _Value Value; |
| 969 | 969 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 970 | 970 |
template NodeMap<Value> > MapParent; |
| 971 | 971 |
|
| 972 | 972 |
NodeMap(const Adaptor& adaptor) |
| 973 | 973 |
: MapParent(adaptor) {}
|
| 974 | 974 |
NodeMap(const Adaptor& adaptor, const Value& value) |
| 975 | 975 |
: MapParent(adaptor, value) {}
|
| 976 | 976 |
|
| 977 | 977 |
private: |
| 978 | 978 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 979 | 979 |
return operator=<NodeMap>(cmap); |
| 980 | 980 |
} |
| 981 | 981 |
|
| 982 | 982 |
template <typename CMap> |
| 983 | 983 |
NodeMap& operator=(const CMap& cmap) {
|
| 984 | 984 |
MapParent::operator=(cmap); |
| 985 | 985 |
return *this; |
| 986 | 986 |
} |
| 987 | 987 |
}; |
| 988 | 988 |
|
| 989 | 989 |
template <typename _Value> |
| 990 | 990 |
class ArcMap : public SubMapExtender<Adaptor, |
| 991 | 991 |
typename Parent::template ArcMap<_Value> > {
|
| 992 | 992 |
public: |
| 993 | 993 |
typedef _Value Value; |
| 994 | 994 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 995 | 995 |
template ArcMap<Value> > MapParent; |
| 996 | 996 |
|
| 997 | 997 |
ArcMap(const Adaptor& adaptor) |
| 998 | 998 |
: MapParent(adaptor) {}
|
| 999 | 999 |
ArcMap(const Adaptor& adaptor, const Value& value) |
| 1000 | 1000 |
: MapParent(adaptor, value) {}
|
| 1001 | 1001 |
|
| 1002 | 1002 |
private: |
| 1003 | 1003 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 1004 | 1004 |
return operator=<ArcMap>(cmap); |
| 1005 | 1005 |
} |
| 1006 | 1006 |
|
| 1007 | 1007 |
template <typename CMap> |
| 1008 | 1008 |
ArcMap& operator=(const CMap& cmap) {
|
| 1009 | 1009 |
MapParent::operator=(cmap); |
| 1010 | 1010 |
return *this; |
| 1011 | 1011 |
} |
| 1012 | 1012 |
}; |
| 1013 | 1013 |
|
| 1014 | 1014 |
template <typename _Value> |
| 1015 | 1015 |
class EdgeMap : public SubMapExtender<Adaptor, |
| 1016 | 1016 |
typename Parent::template EdgeMap<_Value> > {
|
| 1017 | 1017 |
public: |
| 1018 | 1018 |
typedef _Value Value; |
| 1019 | 1019 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 1020 | 1020 |
template EdgeMap<Value> > MapParent; |
| 1021 | 1021 |
|
| 1022 | 1022 |
EdgeMap(const Adaptor& adaptor) |
| 1023 | 1023 |
: MapParent(adaptor) {}
|
| 1024 | 1024 |
|
| 1025 | 1025 |
EdgeMap(const Adaptor& adaptor, const Value& value) |
| 1026 | 1026 |
: MapParent(adaptor, value) {}
|
| 1027 | 1027 |
|
| 1028 | 1028 |
private: |
| 1029 | 1029 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 1030 | 1030 |
return operator=<EdgeMap>(cmap); |
| 1031 | 1031 |
} |
| 1032 | 1032 |
|
| 1033 | 1033 |
template <typename CMap> |
| 1034 | 1034 |
EdgeMap& operator=(const CMap& cmap) {
|
| 1035 | 1035 |
MapParent::operator=(cmap); |
| 1036 | 1036 |
return *this; |
| 1037 | 1037 |
} |
| 1038 | 1038 |
}; |
| 1039 | 1039 |
|
| 1040 | 1040 |
}; |
| 1041 | 1041 |
|
| 1042 | 1042 |
template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap> |
| 1043 | 1043 |
class SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap, false> |
| 1044 | 1044 |
: public GraphAdaptorBase<_Graph> {
|
| 1045 | 1045 |
public: |
| 1046 | 1046 |
typedef _Graph Graph; |
| 1047 | 1047 |
typedef SubGraphBase Adaptor; |
| 1048 | 1048 |
typedef GraphAdaptorBase<_Graph> Parent; |
| 1049 | 1049 |
protected: |
| 1050 | 1050 |
NodeFilterMap* _node_filter_map; |
| 1051 | 1051 |
EdgeFilterMap* _edge_filter_map; |
| 1052 | 1052 |
SubGraphBase() : Parent(), |
| 1053 | 1053 |
_node_filter_map(0), _edge_filter_map(0) { }
|
| 1054 | 1054 |
|
| 1055 | 1055 |
void setNodeFilterMap(NodeFilterMap& node_filter_map) {
|
| 1056 | 1056 |
_node_filter_map=&node_filter_map; |
| 1057 | 1057 |
} |
| 1058 | 1058 |
void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
|
| 1059 | 1059 |
_edge_filter_map=&edge_filter_map; |
| 1060 | 1060 |
} |
| 1061 | 1061 |
|
| 1062 | 1062 |
public: |
| 1063 | 1063 |
|
| 1064 | 1064 |
typedef typename Parent::Node Node; |
| 1065 | 1065 |
typedef typename Parent::Arc Arc; |
| 1066 | 1066 |
typedef typename Parent::Edge Edge; |
| 1067 | 1067 |
|
| 1068 | 1068 |
void first(Node& i) const {
|
| 1069 | 1069 |
Parent::first(i); |
| 1070 | 1070 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
| 1071 | 1071 |
} |
| 1072 | 1072 |
|
| 1073 | 1073 |
void first(Arc& i) const {
|
| 1074 | 1074 |
Parent::first(i); |
| 1075 | 1075 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
| 1076 | 1076 |
} |
| 1077 | 1077 |
|
| 1078 | 1078 |
void first(Edge& i) const {
|
| 1079 | 1079 |
Parent::first(i); |
| 1080 | 1080 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
| 1081 | 1081 |
} |
| 1082 | 1082 |
|
| 1083 | 1083 |
void firstIn(Arc& i, const Node& n) const {
|
| 1084 | 1084 |
Parent::firstIn(i, n); |
| 1085 | 1085 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); |
| 1086 | 1086 |
} |
| 1087 | 1087 |
|
| 1088 | 1088 |
void firstOut(Arc& i, const Node& n) const {
|
| 1089 | 1089 |
Parent::firstOut(i, n); |
| 1090 | 1090 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); |
| 1091 | 1091 |
} |
| 1092 | 1092 |
|
| 1093 | 1093 |
void firstInc(Edge& i, bool& d, const Node& n) const {
|
| 1094 | 1094 |
Parent::firstInc(i, d, n); |
| 1095 | 1095 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); |
| 1096 | 1096 |
} |
| 1097 | 1097 |
|
| 1098 | 1098 |
void next(Node& i) const {
|
| 1099 | 1099 |
Parent::next(i); |
| 1100 | 1100 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
| 1101 | 1101 |
} |
| 1102 | 1102 |
void next(Arc& i) const {
|
| 1103 | 1103 |
Parent::next(i); |
| 1104 | 1104 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
| 1105 | 1105 |
} |
| 1106 | 1106 |
void next(Edge& i) const {
|
| 1107 | 1107 |
Parent::next(i); |
| 1108 | 1108 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
| 1109 | 1109 |
} |
| 1110 | 1110 |
void nextIn(Arc& i) const {
|
| 1111 | 1111 |
Parent::nextIn(i); |
| 1112 | 1112 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); |
| 1113 | 1113 |
} |
| 1114 | 1114 |
|
| 1115 | 1115 |
void nextOut(Arc& i) const {
|
| 1116 | 1116 |
Parent::nextOut(i); |
| 1117 | 1117 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); |
| 1118 | 1118 |
} |
| 1119 | 1119 |
void nextInc(Edge& i, bool& d) const {
|
| 1120 | 1120 |
Parent::nextInc(i, d); |
| 1121 | 1121 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); |
| 1122 | 1122 |
} |
| 1123 | 1123 |
|
| 1124 | 1124 |
void hide(const Node& n) const { _node_filter_map->set(n, false); }
|
| 1125 | 1125 |
void hide(const Edge& e) const { _edge_filter_map->set(e, false); }
|
| 1126 | 1126 |
|
| 1127 | 1127 |
void unHide(const Node& n) const { _node_filter_map->set(n, true); }
|
| 1128 | 1128 |
void unHide(const Edge& e) const { _edge_filter_map->set(e, true); }
|
| 1129 | 1129 |
|
| 1130 | 1130 |
bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; }
|
| 1131 | 1131 |
bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; }
|
| 1132 | 1132 |
|
| 1133 | 1133 |
typedef False NodeNumTag; |
| 1134 | 1134 |
typedef False EdgeNumTag; |
| 1135 | 1135 |
|
| 1136 | 1136 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
| 1137 | 1137 |
Arc findArc(const Node& u, const Node& v, |
| 1138 | 1138 |
const Arc& prev = INVALID) {
|
| 1139 | 1139 |
Arc arc = Parent::findArc(u, v, prev); |
| 1140 | 1140 |
while (arc != INVALID && !(*_edge_filter_map)[arc]) {
|
| 1141 | 1141 |
arc = Parent::findArc(u, v, arc); |
| 1142 | 1142 |
} |
| 1143 | 1143 |
return arc; |
| 1144 | 1144 |
} |
| 1145 | 1145 |
Edge findEdge(const Node& u, const Node& v, |
| 1146 | 1146 |
const Edge& prev = INVALID) {
|
| 1147 | 1147 |
Edge edge = Parent::findEdge(u, v, prev); |
| 1148 | 1148 |
while (edge != INVALID && !(*_edge_filter_map)[edge]) {
|
| 1149 | 1149 |
edge = Parent::findEdge(u, v, edge); |
| 1150 | 1150 |
} |
| 1151 | 1151 |
return edge; |
| 1152 | 1152 |
} |
| 1153 | 1153 |
|
| 1154 | 1154 |
template <typename _Value> |
| 1155 | 1155 |
class NodeMap : public SubMapExtender<Adaptor, |
| 1156 | 1156 |
typename Parent::template NodeMap<_Value> > {
|
| 1157 | 1157 |
public: |
| 1158 | 1158 |
typedef _Value Value; |
| 1159 | 1159 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 1160 | 1160 |
template NodeMap<Value> > MapParent; |
| 1161 | 1161 |
|
| 1162 | 1162 |
NodeMap(const Adaptor& adaptor) |
| 1163 | 1163 |
: MapParent(adaptor) {}
|
| 1164 | 1164 |
NodeMap(const Adaptor& adaptor, const Value& value) |
| 1165 | 1165 |
: MapParent(adaptor, value) {}
|
| 1166 | 1166 |
|
| 1167 | 1167 |
private: |
| 1168 | 1168 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 1169 | 1169 |
return operator=<NodeMap>(cmap); |
| 1170 | 1170 |
} |
| 1171 | 1171 |
|
| 1172 | 1172 |
template <typename CMap> |
| 1173 | 1173 |
NodeMap& operator=(const CMap& cmap) {
|
| 1174 | 1174 |
MapParent::operator=(cmap); |
| 1175 | 1175 |
return *this; |
| 1176 | 1176 |
} |
| 1177 | 1177 |
}; |
| 1178 | 1178 |
|
| 1179 | 1179 |
template <typename _Value> |
| 1180 | 1180 |
class ArcMap : public SubMapExtender<Adaptor, |
| 1181 | 1181 |
typename Parent::template ArcMap<_Value> > {
|
| 1182 | 1182 |
public: |
| 1183 | 1183 |
typedef _Value Value; |
| 1184 | 1184 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 1185 | 1185 |
template ArcMap<Value> > MapParent; |
| 1186 | 1186 |
|
| 1187 | 1187 |
ArcMap(const Adaptor& adaptor) |
| 1188 | 1188 |
: MapParent(adaptor) {}
|
| 1189 | 1189 |
ArcMap(const Adaptor& adaptor, const Value& value) |
| 1190 | 1190 |
: MapParent(adaptor, value) {}
|
| 1191 | 1191 |
|
| 1192 | 1192 |
private: |
| 1193 | 1193 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 1194 | 1194 |
return operator=<ArcMap>(cmap); |
| 1195 | 1195 |
} |
| 1196 | 1196 |
|
| 1197 | 1197 |
template <typename CMap> |
| 1198 | 1198 |
ArcMap& operator=(const CMap& cmap) {
|
| 1199 | 1199 |
MapParent::operator=(cmap); |
| 1200 | 1200 |
return *this; |
| 1201 | 1201 |
} |
| 1202 | 1202 |
}; |
| 1203 | 1203 |
|
| 1204 | 1204 |
template <typename _Value> |
| 1205 | 1205 |
class EdgeMap : public SubMapExtender<Adaptor, |
| 1206 | 1206 |
typename Parent::template EdgeMap<_Value> > {
|
| 1207 | 1207 |
public: |
| 1208 | 1208 |
typedef _Value Value; |
| 1209 | 1209 |
typedef SubMapExtender<Adaptor, typename Parent:: |
| 1210 | 1210 |
template EdgeMap<Value> > MapParent; |
| 1211 | 1211 |
|
| 1212 | 1212 |
EdgeMap(const Adaptor& adaptor) |
| 1213 | 1213 |
: MapParent(adaptor) {}
|
| 1214 | 1214 |
|
| 1215 | 1215 |
EdgeMap(const Adaptor& adaptor, const _Value& value) |
| 1216 | 1216 |
: MapParent(adaptor, value) {}
|
| 1217 | 1217 |
|
| 1218 | 1218 |
private: |
| 1219 | 1219 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 1220 | 1220 |
return operator=<EdgeMap>(cmap); |
| 1221 | 1221 |
} |
| 1222 | 1222 |
|
| 1223 | 1223 |
template <typename CMap> |
| 1224 | 1224 |
EdgeMap& operator=(const CMap& cmap) {
|
| 1225 | 1225 |
MapParent::operator=(cmap); |
| 1226 | 1226 |
return *this; |
| 1227 | 1227 |
} |
| 1228 | 1228 |
}; |
| 1229 | 1229 |
|
| 1230 | 1230 |
}; |
| 1231 | 1231 |
|
| 1232 | 1232 |
/// \ingroup graph_adaptors |
| 1233 | 1233 |
/// |
| 1234 | 1234 |
/// \brief A graph adaptor for hiding nodes and edges in an |
| 1235 | 1235 |
/// undirected graph. |
| 1236 | 1236 |
/// |
| 1237 | 1237 |
/// SubGraph hides nodes and edges in a graph. A bool node map and a |
| 1238 | 1238 |
/// bool edge map must be specified, which define the filters for |
| 1239 | 1239 |
/// nodes and edges. Just the nodes and edges with true value are |
| 1240 | 1240 |
/// shown in the subgraph. The SubGraph is conform to the \ref |
| 1241 | 1241 |
/// concepts::Graph "Graph concept". If the \c _checked parameter is |
| 1242 | 1242 |
/// true, then the edges incident to filtered nodes are also |
| 1243 | 1243 |
/// filtered out. |
| 1244 | 1244 |
/// |
| 1245 | 1245 |
/// \tparam _Graph It must be conform to the \ref |
| 1246 | 1246 |
/// concepts::Graph "Graph concept". The type can be specified |
| 1247 | 1247 |
/// to const. |
| 1248 | 1248 |
/// \tparam _NodeFilterMap A bool valued node map of the the adapted graph. |
| 1249 | 1249 |
/// \tparam _EdgeFilterMap A bool valued edge map of the the adapted graph. |
| 1250 | 1250 |
/// \tparam _checked If the parameter is false then the edge filtering |
| 1251 | 1251 |
/// is not checked with respect to node filter. Otherwise, each edge |
| 1252 | 1252 |
/// is automatically filtered, which is incident to a filtered node. |
| 1253 | 1253 |
/// |
| 1254 | 1254 |
/// \see FilterNodes |
| 1255 | 1255 |
/// \see FilterEdges |
| 1256 | 1256 |
template<typename _Graph, typename NodeFilterMap, |
| 1257 | 1257 |
typename EdgeFilterMap, bool _checked = true> |
| 1258 | 1258 |
class SubGraph |
| 1259 | 1259 |
: public GraphAdaptorExtender< |
| 1260 | 1260 |
SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap, _checked> > {
|
| 1261 | 1261 |
public: |
| 1262 | 1262 |
typedef _Graph Graph; |
| 1263 | 1263 |
typedef GraphAdaptorExtender< |
| 1264 | 1264 |
SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent; |
| 1265 | 1265 |
|
| 1266 | 1266 |
typedef typename Parent::Node Node; |
| 1267 | 1267 |
typedef typename Parent::Edge Edge; |
| 1268 | 1268 |
|
| 1269 | 1269 |
protected: |
| 1270 | 1270 |
SubGraph() { }
|
| 1271 | 1271 |
public: |
| 1272 | 1272 |
|
| 1273 | 1273 |
/// \brief Constructor |
| 1274 | 1274 |
/// |
| 1275 | 1275 |
/// Creates a subgraph for the given graph with given node and |
| 1276 | 1276 |
/// edge map filters. |
| 1277 | 1277 |
SubGraph(Graph& _graph, NodeFilterMap& node_filter_map, |
| 1278 | 1278 |
EdgeFilterMap& edge_filter_map) {
|
| 1279 | 1279 |
setGraph(_graph); |
| 1280 | 1280 |
setNodeFilterMap(node_filter_map); |
| 1281 | 1281 |
setEdgeFilterMap(edge_filter_map); |
| 1282 | 1282 |
} |
| 1283 | 1283 |
|
| 1284 | 1284 |
/// \brief Hides the node of the graph |
| 1285 | 1285 |
/// |
| 1286 | 1286 |
/// This function hides \c n in the graph, i.e. the iteration |
| 1287 | 1287 |
/// jumps over it. This is done by simply setting the value of \c n |
| 1288 | 1288 |
/// to be false in the corresponding node-map. |
| 1289 | 1289 |
void hide(const Node& n) const { Parent::hide(n); }
|
| 1290 | 1290 |
|
| 1291 | 1291 |
/// \brief Hides the edge of the graph |
| 1292 | 1292 |
/// |
| 1293 | 1293 |
/// This function hides \c e in the graph, i.e. the iteration |
| 1294 | 1294 |
/// jumps over it. This is done by simply setting the value of \c e |
| 1295 | 1295 |
/// to be false in the corresponding edge-map. |
| 1296 | 1296 |
void hide(const Edge& e) const { Parent::hide(e); }
|
| 1297 | 1297 |
|
| 1298 | 1298 |
/// \brief Unhides the node of the graph |
| 1299 | 1299 |
/// |
| 1300 | 1300 |
/// The value of \c n is set to be true in the node-map which stores |
| 1301 | 1301 |
/// hide information. If \c n was hidden previuosly, then it is shown |
| 1302 | 1302 |
/// again |
| 1303 | 1303 |
void unHide(const Node& n) const { Parent::unHide(n); }
|
| 1304 | 1304 |
|
| 1305 | 1305 |
/// \brief Unhides the edge of the graph |
| 1306 | 1306 |
/// |
| 1307 | 1307 |
/// The value of \c e is set to be true in the edge-map which stores |
| 1308 | 1308 |
/// hide information. If \c e was hidden previuosly, then it is shown |
| 1309 | 1309 |
/// again |
| 1310 | 1310 |
void unHide(const Edge& e) const { Parent::unHide(e); }
|
| 1311 | 1311 |
|
| 1312 | 1312 |
/// \brief Returns true if \c n is hidden. |
| 1313 | 1313 |
/// |
| 1314 | 1314 |
/// Returns true if \c n is hidden. |
| 1315 | 1315 |
/// |
| 1316 | 1316 |
bool hidden(const Node& n) const { return Parent::hidden(n); }
|
| 1317 | 1317 |
|
| 1318 | 1318 |
/// \brief Returns true if \c e is hidden. |
| 1319 | 1319 |
/// |
| 1320 | 1320 |
/// Returns true if \c e is hidden. |
| 1321 | 1321 |
/// |
| 1322 | 1322 |
bool hidden(const Edge& e) const { return Parent::hidden(e); }
|
| 1323 | 1323 |
}; |
| 1324 | 1324 |
|
| 1325 | 1325 |
/// \brief Just gives back a subgraph |
| 1326 | 1326 |
/// |
| 1327 | 1327 |
/// Just gives back a subgraph |
| 1328 | 1328 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
| 1329 | 1329 |
SubGraph<const Graph, NodeFilterMap, ArcFilterMap> |
| 1330 | 1330 |
subGraph(const Graph& graph, NodeFilterMap& nfm, ArcFilterMap& efm) {
|
| 1331 | 1331 |
return SubGraph<const Graph, NodeFilterMap, ArcFilterMap>(graph, nfm, efm); |
| 1332 | 1332 |
} |
| 1333 | 1333 |
|
| 1334 | 1334 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
| 1335 | 1335 |
SubGraph<const Graph, const NodeFilterMap, ArcFilterMap> |
| 1336 | 1336 |
subGraph(const Graph& graph, |
| 1337 | 1337 |
const NodeFilterMap& nfm, ArcFilterMap& efm) {
|
| 1338 | 1338 |
return SubGraph<const Graph, const NodeFilterMap, ArcFilterMap> |
| 1339 | 1339 |
(graph, nfm, efm); |
| 1340 | 1340 |
} |
| 1341 | 1341 |
|
| 1342 | 1342 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
| 1343 | 1343 |
SubGraph<const Graph, NodeFilterMap, const ArcFilterMap> |
| 1344 | 1344 |
subGraph(const Graph& graph, |
| 1345 | 1345 |
NodeFilterMap& nfm, const ArcFilterMap& efm) {
|
| 1346 | 1346 |
return SubGraph<const Graph, NodeFilterMap, const ArcFilterMap> |
| 1347 | 1347 |
(graph, nfm, efm); |
| 1348 | 1348 |
} |
| 1349 | 1349 |
|
| 1350 | 1350 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
| 1351 | 1351 |
SubGraph<const Graph, const NodeFilterMap, const ArcFilterMap> |
| 1352 | 1352 |
subGraph(const Graph& graph, |
| 1353 | 1353 |
const NodeFilterMap& nfm, const ArcFilterMap& efm) {
|
| 1354 | 1354 |
return SubGraph<const Graph, const NodeFilterMap, const ArcFilterMap> |
| 1355 | 1355 |
(graph, nfm, efm); |
| 1356 | 1356 |
} |
| 1357 | 1357 |
|
| 1358 | 1358 |
/// \ingroup graph_adaptors |
| 1359 | 1359 |
/// |
| 1360 | 1360 |
/// \brief An adaptor for hiding nodes from a digraph or a graph. |
| 1361 | 1361 |
/// |
| 1362 | 1362 |
/// FilterNodes adaptor hides nodes in a graph or a digraph. A bool |
| 1363 | 1363 |
/// node map must be specified, which defines the filters for |
| 1364 | 1364 |
/// nodes. Just the unfiltered nodes and the arcs or edges incident |
| 1365 | 1365 |
/// to unfiltered nodes are shown in the subdigraph or subgraph. The |
| 1366 | 1366 |
/// FilterNodes is conform to the \ref concepts::Digraph |
| 1367 | 1367 |
/// "Digraph concept" or \ref concepts::Graph "Graph concept" depending |
| 1368 | 1368 |
/// on the \c _Digraph template parameter. If the \c _checked |
| 1369 | 1369 |
/// parameter is true, then the arc or edges incident to filtered nodes |
| 1370 | 1370 |
/// are also filtered out. |
| 1371 | 1371 |
/// |
| 1372 | 1372 |
/// \tparam _Digraph It must be conform to the \ref |
| 1373 | 1373 |
/// concepts::Digraph "Digraph concept" or \ref concepts::Graph |
| 1374 | 1374 |
/// "Graph concept". The type can be specified to be const. |
| 1375 | 1375 |
/// \tparam _NodeFilterMap A bool valued node map of the the adapted graph. |
| 1376 | 1376 |
/// \tparam _checked If the parameter is false then the arc or edge |
| 1377 | 1377 |
/// filtering is not checked with respect to node filter. In this |
| 1378 | 1378 |
/// case just isolated nodes can be filtered out from the |
| 1379 | 1379 |
/// graph. |
| 1380 | 1380 |
#ifdef DOXYGEN |
| 1381 | 1381 |
template<typename _Digraph, |
| 1382 | 1382 |
typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, |
| 1383 | 1383 |
bool _checked = true> |
| 1384 | 1384 |
#else |
| 1385 | 1385 |
template<typename _Digraph, |
| 1386 | 1386 |
typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, |
| 1387 | 1387 |
bool _checked = true, |
| 1388 | 1388 |
typename Enable = void> |
| 1389 | 1389 |
#endif |
| 1390 | 1390 |
class FilterNodes |
| 1391 | 1391 |
: public SubDigraph<_Digraph, _NodeFilterMap, |
| 1392 | 1392 |
ConstMap<typename _Digraph::Arc, bool>, _checked> {
|
| 1393 | 1393 |
public: |
| 1394 | 1394 |
|
| 1395 | 1395 |
typedef _Digraph Digraph; |
| 1396 | 1396 |
typedef _NodeFilterMap NodeFilterMap; |
| 1397 | 1397 |
|
| 1398 | 1398 |
typedef SubDigraph<Digraph, NodeFilterMap, |
| 1399 | 1399 |
ConstMap<typename Digraph::Arc, bool>, _checked> |
| 1400 | 1400 |
Parent; |
| 1401 | 1401 |
|
| 1402 | 1402 |
typedef typename Parent::Node Node; |
| 1403 | 1403 |
|
| 1404 | 1404 |
protected: |
| 1405 | 1405 |
ConstMap<typename Digraph::Arc, bool> const_true_map; |
| 1406 | 1406 |
|
| 1407 | 1407 |
FilterNodes() : const_true_map(true) {
|
| 1408 | 1408 |
Parent::setArcFilterMap(const_true_map); |
| 1409 | 1409 |
} |
| 1410 | 1410 |
|
| 1411 | 1411 |
public: |
| 1412 | 1412 |
|
| 1413 | 1413 |
/// \brief Constructor |
| 1414 | 1414 |
/// |
| 1415 | 1415 |
/// Creates an adaptor for the given digraph or graph with |
| 1416 | 1416 |
/// given node filter map. |
| 1417 | 1417 |
FilterNodes(Digraph& _digraph, NodeFilterMap& node_filter) : |
| 1418 | 1418 |
Parent(), const_true_map(true) {
|
| 1419 | 1419 |
Parent::setDigraph(_digraph); |
| 1420 | 1420 |
Parent::setNodeFilterMap(node_filter); |
| 1421 | 1421 |
Parent::setArcFilterMap(const_true_map); |
| 1422 | 1422 |
} |
| 1423 | 1423 |
|
| 1424 | 1424 |
/// \brief Hides the node of the graph |
| 1425 | 1425 |
/// |
| 1426 | 1426 |
/// This function hides \c n in the digraph or graph, i.e. the iteration |
| 1427 | 1427 |
/// jumps over it. This is done by simply setting the value of \c n |
| 1428 | 1428 |
/// to be false in the corresponding node map. |
| 1429 | 1429 |
void hide(const Node& n) const { Parent::hide(n); }
|
| 1430 | 1430 |
|
| 1431 | 1431 |
/// \brief Unhides the node of the graph |
| 1432 | 1432 |
/// |
| 1433 | 1433 |
/// The value of \c n is set to be true in the node-map which stores |
| 1434 | 1434 |
/// hide information. If \c n was hidden previuosly, then it is shown |
| 1435 | 1435 |
/// again |
| 1436 | 1436 |
void unHide(const Node& n) const { Parent::unHide(n); }
|
| 1437 | 1437 |
|
| 1438 | 1438 |
/// \brief Returns true if \c n is hidden. |
| 1439 | 1439 |
/// |
| 1440 | 1440 |
/// Returns true if \c n is hidden. |
| 1441 | 1441 |
/// |
| 1442 | 1442 |
bool hidden(const Node& n) const { return Parent::hidden(n); }
|
| 1443 | 1443 |
|
| 1444 | 1444 |
}; |
| 1445 | 1445 |
|
| 1446 | 1446 |
template<typename _Graph, typename _NodeFilterMap, bool _checked> |
| 1447 | 1447 |
class FilterNodes<_Graph, _NodeFilterMap, _checked, |
| 1448 | 1448 |
typename enable_if<UndirectedTagIndicator<_Graph> >::type> |
| 1449 | 1449 |
: public SubGraph<_Graph, _NodeFilterMap, |
| 1450 | 1450 |
ConstMap<typename _Graph::Edge, bool>, _checked> {
|
| 1451 | 1451 |
public: |
| 1452 | 1452 |
typedef _Graph Graph; |
| 1453 | 1453 |
typedef _NodeFilterMap NodeFilterMap; |
| 1454 | 1454 |
typedef SubGraph<Graph, NodeFilterMap, |
| 1455 | 1455 |
ConstMap<typename Graph::Edge, bool> > Parent; |
| 1456 | 1456 |
|
| 1457 | 1457 |
typedef typename Parent::Node Node; |
| 1458 | 1458 |
protected: |
| 1459 | 1459 |
ConstMap<typename Graph::Edge, bool> const_true_map; |
| 1460 | 1460 |
|
| 1461 | 1461 |
FilterNodes() : const_true_map(true) {
|
| 1462 | 1462 |
Parent::setEdgeFilterMap(const_true_map); |
| 1463 | 1463 |
} |
| 1464 | 1464 |
|
| 1465 | 1465 |
public: |
| 1466 | 1466 |
|
| 1467 | 1467 |
FilterNodes(Graph& _graph, NodeFilterMap& node_filter_map) : |
| 1468 | 1468 |
Parent(), const_true_map(true) {
|
| 1469 | 1469 |
Parent::setGraph(_graph); |
| 1470 | 1470 |
Parent::setNodeFilterMap(node_filter_map); |
| 1471 | 1471 |
Parent::setEdgeFilterMap(const_true_map); |
| 1472 | 1472 |
} |
| 1473 | 1473 |
|
| 1474 | 1474 |
void hide(const Node& n) const { Parent::hide(n); }
|
| 1475 | 1475 |
void unHide(const Node& n) const { Parent::unHide(n); }
|
| 1476 | 1476 |
bool hidden(const Node& n) const { return Parent::hidden(n); }
|
| 1477 | 1477 |
|
| 1478 | 1478 |
}; |
| 1479 | 1479 |
|
| 1480 | 1480 |
|
| 1481 | 1481 |
/// \brief Just gives back a FilterNodes adaptor |
| 1482 | 1482 |
/// |
| 1483 | 1483 |
/// Just gives back a FilterNodes adaptor |
| 1484 | 1484 |
template<typename Digraph, typename NodeFilterMap> |
| 1485 | 1485 |
FilterNodes<const Digraph, NodeFilterMap> |
| 1486 | 1486 |
filterNodes(const Digraph& digraph, NodeFilterMap& nfm) {
|
| 1487 | 1487 |
return FilterNodes<const Digraph, NodeFilterMap>(digraph, nfm); |
| 1488 | 1488 |
} |
| 1489 | 1489 |
|
| 1490 | 1490 |
template<typename Digraph, typename NodeFilterMap> |
| 1491 | 1491 |
FilterNodes<const Digraph, const NodeFilterMap> |
| 1492 | 1492 |
filterNodes(const Digraph& digraph, const NodeFilterMap& nfm) {
|
| 1493 | 1493 |
return FilterNodes<const Digraph, const NodeFilterMap>(digraph, nfm); |
| 1494 | 1494 |
} |
| 1495 | 1495 |
|
| 1496 | 1496 |
/// \ingroup graph_adaptors |
| 1497 | 1497 |
/// |
| 1498 | 1498 |
/// \brief An adaptor for hiding arcs from a digraph. |
| 1499 | 1499 |
/// |
| 1500 | 1500 |
/// FilterArcs adaptor hides arcs in a digraph. A bool arc map must |
| 1501 | 1501 |
/// be specified, which defines the filters for arcs. Just the |
| 1502 | 1502 |
/// unfiltered arcs are shown in the subdigraph. The FilterArcs is |
| 1503 | 1503 |
/// conform to the \ref concepts::Digraph "Digraph concept". |
| 1504 | 1504 |
/// |
| 1505 | 1505 |
/// \tparam _Digraph It must be conform to the \ref concepts::Digraph |
| 1506 | 1506 |
/// "Digraph concept". The type can be specified to be const. |
| 1507 | 1507 |
/// \tparam _ArcFilterMap A bool valued arc map of the the adapted |
| 1508 | 1508 |
/// graph. |
| 1509 | 1509 |
template<typename _Digraph, typename _ArcFilterMap> |
| 1510 | 1510 |
class FilterArcs : |
| 1511 | 1511 |
public SubDigraph<_Digraph, ConstMap<typename _Digraph::Node, bool>, |
| 1512 | 1512 |
_ArcFilterMap, false> {
|
| 1513 | 1513 |
public: |
| 1514 | 1514 |
typedef _Digraph Digraph; |
| 1515 | 1515 |
typedef _ArcFilterMap ArcFilterMap; |
| 1516 | 1516 |
|
| 1517 | 1517 |
typedef SubDigraph<Digraph, ConstMap<typename Digraph::Node, bool>, |
| 1518 | 1518 |
ArcFilterMap, false> Parent; |
| 1519 | 1519 |
|
| 1520 | 1520 |
typedef typename Parent::Arc Arc; |
| 1521 | 1521 |
|
| 1522 | 1522 |
protected: |
| 1523 | 1523 |
ConstMap<typename Digraph::Node, bool> const_true_map; |
| 1524 | 1524 |
|
| 1525 | 1525 |
FilterArcs() : const_true_map(true) {
|
| 1526 | 1526 |
Parent::setNodeFilterMap(const_true_map); |
| 1527 | 1527 |
} |
| 1528 | 1528 |
|
| 1529 | 1529 |
public: |
| 1530 | 1530 |
|
| 1531 | 1531 |
/// \brief Constructor |
| 1532 | 1532 |
/// |
| 1533 | 1533 |
/// Creates a FilterArcs adaptor for the given graph with |
| 1534 | 1534 |
/// given arc map filter. |
| 1535 | 1535 |
FilterArcs(Digraph& digraph, ArcFilterMap& arc_filter) |
| 1536 | 1536 |
: Parent(), const_true_map(true) {
|
| 1537 | 1537 |
Parent::setDigraph(digraph); |
| 1538 | 1538 |
Parent::setNodeFilterMap(const_true_map); |
| 1539 | 1539 |
Parent::setArcFilterMap(arc_filter); |
| 1540 | 1540 |
} |
| 1541 | 1541 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/arg_parser.h> |
| 20 | 20 |
|
| 21 | 21 |
namespace lemon {
|
| 22 | 22 |
|
| 23 | 23 |
void ArgParser::_showHelp(void *p) |
| 24 | 24 |
{
|
| 25 | 25 |
(static_cast<ArgParser*>(p))->showHelp(); |
| 26 | 26 |
exit(1); |
| 27 | 27 |
} |
| 28 | 28 |
|
| 29 | 29 |
ArgParser::ArgParser(int argc, const char * const *argv) |
| 30 | 30 |
:_argc(argc), _argv(argv), _command_name(argv[0]) {
|
| 31 | 31 |
funcOption("-help","Print a short help message",_showHelp,this);
|
| 32 | 32 |
synonym("help","-help");
|
| 33 | 33 |
synonym("h","-help");
|
| 34 | 34 |
} |
| 35 | 35 |
|
| 36 | 36 |
ArgParser::~ArgParser() |
| 37 | 37 |
{
|
| 38 | 38 |
for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) |
| 39 | 39 |
if(i->second.self_delete) |
| 40 | 40 |
switch(i->second.type) {
|
| 41 | 41 |
case BOOL: |
| 42 | 42 |
delete i->second.bool_p; |
| 43 | 43 |
break; |
| 44 | 44 |
case STRING: |
| 45 | 45 |
delete i->second.string_p; |
| 46 | 46 |
break; |
| 47 | 47 |
case DOUBLE: |
| 48 | 48 |
delete i->second.double_p; |
| 49 | 49 |
break; |
| 50 | 50 |
case INTEGER: |
| 51 | 51 |
delete i->second.int_p; |
| 52 | 52 |
break; |
| 53 | 53 |
case UNKNOWN: |
| 54 | 54 |
break; |
| 55 | 55 |
case FUNC: |
| 56 | 56 |
break; |
| 57 | 57 |
} |
| 58 | 58 |
} |
| 59 | 59 |
|
| 60 | 60 |
|
| 61 | 61 |
ArgParser &ArgParser::intOption(const std::string &name, |
| 62 | 62 |
const std::string &help, |
| 63 | 63 |
int value, bool obl) |
| 64 | 64 |
{
|
| 65 | 65 |
ParData p; |
| 66 | 66 |
p.int_p=new int(value); |
| 67 | 67 |
p.self_delete=true; |
| 68 | 68 |
p.help=help; |
| 69 | 69 |
p.type=INTEGER; |
| 70 | 70 |
p.mandatory=obl; |
| 71 | 71 |
_opts[name]=p; |
| 72 | 72 |
return *this; |
| 73 | 73 |
} |
| 74 | 74 |
|
| 75 | 75 |
ArgParser &ArgParser::doubleOption(const std::string &name, |
| 76 | 76 |
const std::string &help, |
| 77 | 77 |
double value, bool obl) |
| 78 | 78 |
{
|
| 79 | 79 |
ParData p; |
| 80 | 80 |
p.double_p=new double(value); |
| 81 | 81 |
p.self_delete=true; |
| 82 | 82 |
p.help=help; |
| 83 | 83 |
p.type=DOUBLE; |
| 84 | 84 |
p.mandatory=obl; |
| 85 | 85 |
_opts[name]=p; |
| 86 | 86 |
return *this; |
| 87 | 87 |
} |
| 88 | 88 |
|
| 89 | 89 |
ArgParser &ArgParser::boolOption(const std::string &name, |
| 90 | 90 |
const std::string &help, |
| 91 | 91 |
bool value, bool obl) |
| 92 | 92 |
{
|
| 93 | 93 |
ParData p; |
| 94 | 94 |
p.bool_p=new bool(value); |
| 95 | 95 |
p.self_delete=true; |
| 96 | 96 |
p.help=help; |
| 97 | 97 |
p.type=BOOL; |
| 98 | 98 |
p.mandatory=obl; |
| 99 | 99 |
_opts[name]=p; |
| 100 | 100 |
return *this; |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
ArgParser &ArgParser::stringOption(const std::string &name, |
| 104 | 104 |
const std::string &help, |
| 105 | 105 |
std::string value, bool obl) |
| 106 | 106 |
{
|
| 107 | 107 |
ParData p; |
| 108 | 108 |
p.string_p=new std::string(value); |
| 109 | 109 |
p.self_delete=true; |
| 110 | 110 |
p.help=help; |
| 111 | 111 |
p.type=STRING; |
| 112 | 112 |
p.mandatory=obl; |
| 113 | 113 |
_opts[name]=p; |
| 114 | 114 |
return *this; |
| 115 | 115 |
} |
| 116 | 116 |
|
| 117 | 117 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 118 | 118 |
const std::string &help, |
| 119 | 119 |
int &ref, bool obl) |
| 120 | 120 |
{
|
| 121 | 121 |
ParData p; |
| 122 | 122 |
p.int_p=&ref; |
| 123 | 123 |
p.self_delete=false; |
| 124 | 124 |
p.help=help; |
| 125 | 125 |
p.type=INTEGER; |
| 126 | 126 |
p.mandatory=obl; |
| 127 | 127 |
_opts[name]=p; |
| 128 | 128 |
return *this; |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 132 | 132 |
const std::string &help, |
| 133 | 133 |
double &ref, bool obl) |
| 134 | 134 |
{
|
| 135 | 135 |
ParData p; |
| 136 | 136 |
p.double_p=&ref; |
| 137 | 137 |
p.self_delete=false; |
| 138 | 138 |
p.help=help; |
| 139 | 139 |
p.type=DOUBLE; |
| 140 | 140 |
p.mandatory=obl; |
| 141 | 141 |
_opts[name]=p; |
| 142 | 142 |
return *this; |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 146 | 146 |
const std::string &help, |
| 147 | 147 |
bool &ref, bool obl) |
| 148 | 148 |
{
|
| 149 | 149 |
ParData p; |
| 150 | 150 |
p.bool_p=&ref; |
| 151 | 151 |
p.self_delete=false; |
| 152 | 152 |
p.help=help; |
| 153 | 153 |
p.type=BOOL; |
| 154 | 154 |
p.mandatory=obl; |
| 155 | 155 |
_opts[name]=p; |
| 156 | 156 |
|
| 157 | 157 |
ref = false; |
| 158 | 158 |
|
| 159 | 159 |
return *this; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 163 | 163 |
const std::string &help, |
| 164 | 164 |
std::string &ref, bool obl) |
| 165 | 165 |
{
|
| 166 | 166 |
ParData p; |
| 167 | 167 |
p.string_p=&ref; |
| 168 | 168 |
p.self_delete=false; |
| 169 | 169 |
p.help=help; |
| 170 | 170 |
p.type=STRING; |
| 171 | 171 |
p.mandatory=obl; |
| 172 | 172 |
_opts[name]=p; |
| 173 | 173 |
return *this; |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
ArgParser &ArgParser::funcOption(const std::string &name, |
| 177 | 177 |
const std::string &help, |
| 178 | 178 |
void (*func)(void *),void *data) |
| 179 | 179 |
{
|
| 180 | 180 |
ParData p; |
| 181 | 181 |
p.func_p.p=func; |
| 182 | 182 |
p.func_p.data=data; |
| 183 | 183 |
p.self_delete=false; |
| 184 | 184 |
p.help=help; |
| 185 | 185 |
p.type=FUNC; |
| 186 | 186 |
p.mandatory=false; |
| 187 | 187 |
_opts[name]=p; |
| 188 | 188 |
return *this; |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
ArgParser &ArgParser::optionGroup(const std::string &group, |
| 192 | 192 |
const std::string &opt) |
| 193 | 193 |
{
|
| 194 | 194 |
Opts::iterator i = _opts.find(opt); |
| 195 | 195 |
LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'"); |
| 196 | 196 |
LEMON_ASSERT(!(i->second.ingroup), |
| 197 | 197 |
"Option already in option group: '"+opt+"'"); |
| 198 | 198 |
GroupData &g=_groups[group]; |
| 199 | 199 |
g.opts.push_back(opt); |
| 200 | 200 |
i->second.ingroup=true; |
| 201 | 201 |
return *this; |
| 202 | 202 |
} |
| 203 | 203 |
|
| 204 | 204 |
ArgParser &ArgParser::onlyOneGroup(const std::string &group) |
| 205 | 205 |
{
|
| 206 | 206 |
GroupData &g=_groups[group]; |
| 207 | 207 |
g.only_one=true; |
| 208 | 208 |
return *this; |
| 209 | 209 |
} |
| 210 | 210 |
|
| 211 | 211 |
ArgParser &ArgParser::synonym(const std::string &syn, |
| 212 | 212 |
const std::string &opt) |
| 213 | 213 |
{
|
| 214 | 214 |
Opts::iterator o = _opts.find(opt); |
| 215 | 215 |
Opts::iterator s = _opts.find(syn); |
| 216 | 216 |
LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'"); |
| 217 | 217 |
LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'"); |
| 218 | 218 |
ParData p; |
| 219 | 219 |
p.help=opt; |
| 220 | 220 |
p.mandatory=false; |
| 221 | 221 |
p.syn=true; |
| 222 | 222 |
_opts[syn]=p; |
| 223 | 223 |
o->second.has_syn=true; |
| 224 | 224 |
return *this; |
| 225 | 225 |
} |
| 226 | 226 |
|
| 227 | 227 |
ArgParser &ArgParser::mandatoryGroup(const std::string &group) |
| 228 | 228 |
{
|
| 229 | 229 |
GroupData &g=_groups[group]; |
| 230 | 230 |
g.mandatory=true; |
| 231 | 231 |
return *this; |
| 232 | 232 |
} |
| 233 | 233 |
|
| 234 | 234 |
ArgParser &ArgParser::other(const std::string &name, |
| 235 | 235 |
const std::string &help) |
| 236 | 236 |
{
|
| 237 | 237 |
_others_help.push_back(OtherArg(name,help)); |
| 238 | 238 |
return *this; |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
void ArgParser::show(std::ostream &os,Opts::const_iterator i) const |
| 242 | 242 |
{
|
| 243 | 243 |
os << "-" << i->first; |
| 244 | 244 |
if(i->second.has_syn) |
| 245 | 245 |
for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j) |
| 246 | 246 |
if(j->second.syn&&j->second.help==i->first) |
| 247 | 247 |
os << "|-" << j->first; |
| 248 | 248 |
switch(i->second.type) {
|
| 249 | 249 |
case STRING: |
| 250 | 250 |
os << " str"; |
| 251 | 251 |
break; |
| 252 | 252 |
case INTEGER: |
| 253 | 253 |
os << " int"; |
| 254 | 254 |
break; |
| 255 | 255 |
case DOUBLE: |
| 256 | 256 |
os << " num"; |
| 257 | 257 |
break; |
| 258 | 258 |
default: |
| 259 | 259 |
break; |
| 260 | 260 |
} |
| 261 | 261 |
} |
| 262 | 262 |
|
| 263 | 263 |
void ArgParser::show(std::ostream &os,Groups::const_iterator i) const |
| 264 | 264 |
{
|
| 265 | 265 |
GroupData::Opts::const_iterator o=i->second.opts.begin(); |
| 266 | 266 |
while(o!=i->second.opts.end()) {
|
| 267 | 267 |
show(os,_opts.find(*o)); |
| 268 | 268 |
++o; |
| 269 | 269 |
if(o!=i->second.opts.end()) os<<'|'; |
| 270 | 270 |
} |
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 | 273 |
void ArgParser::showHelp(Opts::const_iterator i) const |
| 274 | 274 |
{
|
| 275 | 275 |
if(i->second.help.size()==0||i->second.syn) return; |
| 276 | 276 |
std::cerr << " "; |
| 277 | 277 |
show(std::cerr,i); |
| 278 | 278 |
std::cerr << std::endl; |
| 279 | 279 |
std::cerr << " " << i->second.help << std::endl; |
| 280 | 280 |
} |
| 281 | 281 |
void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i) |
| 282 | 282 |
const |
| 283 | 283 |
{
|
| 284 | 284 |
if(i->help.size()==0) return; |
| 285 | 285 |
std::cerr << " " << i->name << std::endl |
| 286 | 286 |
<< " " << i->help << std::endl; |
| 287 | 287 |
} |
| 288 | 288 |
|
| 289 | 289 |
void ArgParser::shortHelp() const |
| 290 | 290 |
{
|
| 291 | 291 |
const unsigned int LINE_LEN=77; |
| 292 | 292 |
const std::string indent(" ");
|
| 293 | 293 |
std::cerr << "Usage:\n " << _command_name; |
| 294 | 294 |
int pos=_command_name.size()+2; |
| 295 | 295 |
for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
|
| 296 | 296 |
std::ostringstream cstr; |
| 297 | 297 |
cstr << ' '; |
| 298 | 298 |
if(!g->second.mandatory) cstr << '['; |
| 299 | 299 |
show(cstr,g); |
| 300 | 300 |
if(!g->second.mandatory) cstr << ']'; |
| 301 | 301 |
if(pos+cstr.str().size()>LINE_LEN) {
|
| 302 | 302 |
std::cerr << std::endl << indent; |
| 303 | 303 |
pos=indent.size(); |
| 304 | 304 |
} |
| 305 | 305 |
std::cerr << cstr.str(); |
| 306 | 306 |
pos+=cstr.str().size(); |
| 307 | 307 |
} |
| 308 | 308 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) |
| 309 | 309 |
if(!i->second.ingroup&&!i->second.syn) {
|
| 310 | 310 |
std::ostringstream cstr; |
| 311 | 311 |
cstr << ' '; |
| 312 | 312 |
if(!i->second.mandatory) cstr << '['; |
| 313 | 313 |
show(cstr,i); |
| 314 | 314 |
if(!i->second.mandatory) cstr << ']'; |
| 315 | 315 |
if(pos+cstr.str().size()>LINE_LEN) {
|
| 316 | 316 |
std::cerr << std::endl << indent; |
| 317 | 317 |
pos=indent.size(); |
| 318 | 318 |
} |
| 319 | 319 |
std::cerr << cstr.str(); |
| 320 | 320 |
pos+=cstr.str().size(); |
| 321 | 321 |
} |
| 322 | 322 |
for(std::vector<OtherArg>::const_iterator i=_others_help.begin(); |
| 323 | 323 |
i!=_others_help.end();++i) |
| 324 | 324 |
{
|
| 325 | 325 |
std::ostringstream cstr; |
| 326 | 326 |
cstr << ' ' << i->name; |
| 327 | 327 |
|
| 328 | 328 |
if(pos+cstr.str().size()>LINE_LEN) {
|
| 329 | 329 |
std::cerr << std::endl << indent; |
| 330 | 330 |
pos=indent.size(); |
| 331 | 331 |
} |
| 332 | 332 |
std::cerr << cstr.str(); |
| 333 | 333 |
pos+=cstr.str().size(); |
| 334 | 334 |
} |
| 335 | 335 |
std::cerr << std::endl; |
| 336 | 336 |
} |
| 337 | 337 |
|
| 338 | 338 |
void ArgParser::showHelp() const |
| 339 | 339 |
{
|
| 340 | 340 |
shortHelp(); |
| 341 | 341 |
std::cerr << "Where:\n"; |
| 342 | 342 |
for(std::vector<OtherArg>::const_iterator i=_others_help.begin(); |
| 343 | 343 |
i!=_others_help.end();++i) showHelp(i); |
| 344 | 344 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i); |
| 345 | 345 |
exit(1); |
| 346 | 346 |
} |
| 347 | 347 |
|
| 348 | 348 |
|
| 349 | 349 |
void ArgParser::unknownOpt(std::string arg) const |
| 350 | 350 |
{
|
| 351 | 351 |
std::cerr << "\nUnknown option: " << arg << "\n"; |
| 352 | 352 |
std::cerr << "\nType '" << _command_name << |
| 353 | 353 |
" --help' to obtain a short summary on the usage.\n\n"; |
| 354 | 354 |
exit(1); |
| 355 | 355 |
} |
| 356 | 356 |
|
| 357 | 357 |
void ArgParser::requiresValue(std::string arg, OptType t) const |
| 358 | 358 |
{
|
| 359 | 359 |
std::cerr << "Argument '" << arg << "' requires a"; |
| 360 | 360 |
switch(t) {
|
| 361 | 361 |
case STRING: |
| 362 | 362 |
std::cerr << " string"; |
| 363 | 363 |
break; |
| 364 | 364 |
case INTEGER: |
| 365 | 365 |
std::cerr << "n integer"; |
| 366 | 366 |
break; |
| 367 | 367 |
case DOUBLE: |
| 368 | 368 |
std::cerr << " floating point"; |
| 369 | 369 |
break; |
| 370 | 370 |
default: |
| 371 | 371 |
break; |
| 372 | 372 |
} |
| 373 | 373 |
std::cerr << " value\n\n"; |
| 374 | 374 |
showHelp(); |
| 375 | 375 |
} |
| 376 | 376 |
|
| 377 | 377 |
|
| 378 | 378 |
void ArgParser::checkMandatories() const |
| 379 | 379 |
{
|
| 380 | 380 |
bool ok=true; |
| 381 | 381 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) |
| 382 | 382 |
if(i->second.mandatory&&!i->second.set) |
| 383 | 383 |
{
|
| 384 | 384 |
if(ok) |
| 385 | 385 |
std::cerr << _command_name |
| 386 | 386 |
<< ": The following mandatory arguments are missing.\n"; |
| 387 | 387 |
ok=false; |
| 388 | 388 |
showHelp(i); |
| 389 | 389 |
} |
| 390 | 390 |
for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i) |
| 391 | 391 |
if(i->second.mandatory||i->second.only_one) |
| 392 | 392 |
{
|
| 393 | 393 |
int set=0; |
| 394 | 394 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
| 395 | 395 |
o!=i->second.opts.end();++o) |
| 396 | 396 |
if(_opts.find(*o)->second.set) ++set; |
| 397 | 397 |
if(i->second.mandatory&&!set) {
|
| 398 | 398 |
std::cerr << _command_name << |
| 399 | 399 |
": At least one of the following arguments is mandatory.\n"; |
| 400 | 400 |
ok=false; |
| 401 | 401 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
| 402 | 402 |
o!=i->second.opts.end();++o) |
| 403 | 403 |
showHelp(_opts.find(*o)); |
| 404 | 404 |
} |
| 405 | 405 |
if(i->second.only_one&&set>1) {
|
| 406 | 406 |
std::cerr << _command_name << |
| 407 | 407 |
": At most one of the following arguments can be given.\n"; |
| 408 | 408 |
ok=false; |
| 409 | 409 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
| 410 | 410 |
o!=i->second.opts.end();++o) |
| 411 | 411 |
showHelp(_opts.find(*o)); |
| 412 | 412 |
} |
| 413 | 413 |
} |
| 414 | 414 |
if(!ok) {
|
| 415 | 415 |
std::cerr << "\nType '" << _command_name << |
| 416 | 416 |
" --help' to obtain a short summary on the usage.\n\n"; |
| 417 | 417 |
exit(1); |
| 418 | 418 |
} |
| 419 | 419 |
} |
| 420 | 420 |
|
| 421 | 421 |
ArgParser &ArgParser::parse() |
| 422 | 422 |
{
|
| 423 | 423 |
for(int ar=1; ar<_argc; ++ar) {
|
| 424 | 424 |
std::string arg(_argv[ar]); |
| 425 | 425 |
if (arg[0] != '-' || arg.size() == 1) {
|
| 426 | 426 |
_file_args.push_back(arg); |
| 427 | 427 |
} |
| 428 | 428 |
else {
|
| 429 | 429 |
Opts::iterator i = _opts.find(arg.substr(1)); |
| 430 | 430 |
if(i==_opts.end()) unknownOpt(arg); |
| 431 | 431 |
else {
|
| 432 | 432 |
if(i->second.syn) i=_opts.find(i->second.help); |
| 433 | 433 |
ParData &p(i->second); |
| 434 | 434 |
if (p.type==BOOL) *p.bool_p=true; |
| 435 | 435 |
else if (p.type==FUNC) p.func_p.p(p.func_p.data); |
| 436 | 436 |
else if(++ar==_argc) requiresValue(arg, p.type); |
| 437 | 437 |
else {
|
| 438 | 438 |
std::string val(_argv[ar]); |
| 439 | 439 |
std::istringstream vals(val); |
| 440 | 440 |
switch(p.type) {
|
| 441 | 441 |
case STRING: |
| 442 | 442 |
*p.string_p=val; |
| 443 | 443 |
break; |
| 444 | 444 |
case INTEGER: |
| 445 | 445 |
vals >> *p.int_p; |
| 446 | 446 |
break; |
| 447 | 447 |
case DOUBLE: |
| 448 | 448 |
vals >> *p.double_p; |
| 449 | 449 |
break; |
| 450 | 450 |
default: |
| 451 | 451 |
break; |
| 452 | 452 |
} |
| 453 | 453 |
if(p.type!=STRING&&(!vals||!vals.eof())) |
| 454 | 454 |
requiresValue(arg, p.type); |
| 455 | 455 |
} |
| 456 | 456 |
p.set = true; |
| 457 | 457 |
} |
| 458 | 458 |
} |
| 459 | 459 |
} |
| 460 | 460 |
checkMandatories(); |
| 461 | 461 |
|
| 462 | 462 |
return *this; |
| 463 | 463 |
} |
| 464 | 464 |
|
| 465 | 465 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ARG_PARSER_H |
| 20 | 20 |
#define LEMON_ARG_PARSER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <map> |
| 24 | 24 |
#include <list> |
| 25 | 25 |
#include <string> |
| 26 | 26 |
#include <iostream> |
| 27 | 27 |
#include <sstream> |
| 28 | 28 |
#include <algorithm> |
| 29 | 29 |
#include <lemon/assert.h> |
| 30 | 30 |
|
| 31 | 31 |
///\ingroup misc |
| 32 | 32 |
///\file |
| 33 | 33 |
///\brief A tool to parse command line arguments. |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
///Command line arguments parser |
| 38 | 38 |
|
| 39 | 39 |
///\ingroup misc |
| 40 | 40 |
///Command line arguments parser. |
| 41 | 41 |
/// |
| 42 | 42 |
///For a complete example see the \ref arg_parser_demo.cc demo file. |
| 43 | 43 |
class ArgParser {
|
| 44 | 44 |
|
| 45 | 45 |
static void _showHelp(void *p); |
| 46 | 46 |
protected: |
| 47 | 47 |
|
| 48 | 48 |
int _argc; |
| 49 | 49 |
const char * const *_argv; |
| 50 | 50 |
|
| 51 | 51 |
enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
|
| 52 | 52 |
|
| 53 | 53 |
class ParData {
|
| 54 | 54 |
public: |
| 55 | 55 |
union {
|
| 56 | 56 |
bool *bool_p; |
| 57 | 57 |
int *int_p; |
| 58 | 58 |
double *double_p; |
| 59 | 59 |
std::string *string_p; |
| 60 | 60 |
struct {
|
| 61 | 61 |
void (*p)(void *); |
| 62 | 62 |
void *data; |
| 63 | 63 |
} func_p; |
| 64 | 64 |
|
| 65 | 65 |
}; |
| 66 | 66 |
std::string help; |
| 67 | 67 |
bool mandatory; |
| 68 | 68 |
OptType type; |
| 69 | 69 |
bool set; |
| 70 | 70 |
bool ingroup; |
| 71 | 71 |
bool has_syn; |
| 72 | 72 |
bool syn; |
| 73 | 73 |
bool self_delete; |
| 74 | 74 |
ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false), |
| 75 | 75 |
has_syn(false), syn(false), self_delete(false) {}
|
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
typedef std::map<std::string,ParData> Opts; |
| 79 | 79 |
Opts _opts; |
| 80 | 80 |
|
| 81 | 81 |
class GroupData |
| 82 | 82 |
{
|
| 83 | 83 |
public: |
| 84 | 84 |
typedef std::list<std::string> Opts; |
| 85 | 85 |
Opts opts; |
| 86 | 86 |
bool only_one; |
| 87 | 87 |
bool mandatory; |
| 88 | 88 |
GroupData() :only_one(false), mandatory(false) {}
|
| 89 | 89 |
}; |
| 90 | 90 |
|
| 91 | 91 |
typedef std::map<std::string,GroupData> Groups; |
| 92 | 92 |
Groups _groups; |
| 93 | 93 |
|
| 94 | 94 |
struct OtherArg |
| 95 | 95 |
{
|
| 96 | 96 |
std::string name; |
| 97 | 97 |
std::string help; |
| 98 | 98 |
OtherArg(std::string n, std::string h) :name(n), help(h) {}
|
| 99 | 99 |
|
| 100 | 100 |
}; |
| 101 | 101 |
|
| 102 | 102 |
std::vector<OtherArg> _others_help; |
| 103 | 103 |
std::vector<std::string> _file_args; |
| 104 | 104 |
std::string _command_name; |
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
private: |
| 108 | 108 |
//Bind a function to an option. |
| 109 | 109 |
|
| 110 | 110 |
//\param name The name of the option. The leading '-' must be omitted. |
| 111 | 111 |
//\param help A help string. |
| 112 | 112 |
//\retval func The function to be called when the option is given. It |
| 113 | 113 |
// must be of type "void f(void *)" |
| 114 | 114 |
//\param data Data to be passed to \c func |
| 115 | 115 |
ArgParser &funcOption(const std::string &name, |
| 116 | 116 |
const std::string &help, |
| 117 | 117 |
void (*func)(void *),void *data); |
| 118 | 118 |
|
| 119 | 119 |
public: |
| 120 | 120 |
|
| 121 | 121 |
///Constructor |
| 122 | 122 |
ArgParser(int argc, const char * const *argv); |
| 123 | 123 |
|
| 124 | 124 |
~ArgParser(); |
| 125 | 125 |
|
| 126 | 126 |
///\name Options |
| 127 | 127 |
/// |
| 128 | 128 |
|
| 129 | 129 |
///@{
|
| 130 | 130 |
|
| 131 | 131 |
///Add a new integer type option |
| 132 | 132 |
|
| 133 | 133 |
///Add a new integer type option. |
| 134 | 134 |
///\param name The name of the option. The leading '-' must be omitted. |
| 135 | 135 |
///\param help A help string. |
| 136 | 136 |
///\param value A default value for the option. |
| 137 | 137 |
///\param obl Indicate if the option is mandatory. |
| 138 | 138 |
ArgParser &intOption(const std::string &name, |
| 139 | 139 |
const std::string &help, |
| 140 | 140 |
int value=0, bool obl=false); |
| 141 | 141 |
|
| 142 | 142 |
///Add a new floating point type option |
| 143 | 143 |
|
| 144 | 144 |
///Add a new floating point type option. |
| 145 | 145 |
///\param name The name of the option. The leading '-' must be omitted. |
| 146 | 146 |
///\param help A help string. |
| 147 | 147 |
///\param value A default value for the option. |
| 148 | 148 |
///\param obl Indicate if the option is mandatory. |
| 149 | 149 |
ArgParser &doubleOption(const std::string &name, |
| 150 | 150 |
const std::string &help, |
| 151 | 151 |
double value=0, bool obl=false); |
| 152 | 152 |
|
| 153 | 153 |
///Add a new bool type option |
| 154 | 154 |
|
| 155 | 155 |
///Add a new bool type option. |
| 156 | 156 |
///\param name The name of the option. The leading '-' must be omitted. |
| 157 | 157 |
///\param help A help string. |
| 158 | 158 |
///\param value A default value for the option. |
| 159 | 159 |
///\param obl Indicate if the option is mandatory. |
| 160 | 160 |
///\note A mandatory bool obtion is of very little use. |
| 161 | 161 |
ArgParser &boolOption(const std::string &name, |
| 162 | 162 |
const std::string &help, |
| 163 | 163 |
bool value=false, bool obl=false); |
| 164 | 164 |
|
| 165 | 165 |
///Add a new string type option |
| 166 | 166 |
|
| 167 | 167 |
///Add a new string type option. |
| 168 | 168 |
///\param name The name of the option. The leading '-' must be omitted. |
| 169 | 169 |
///\param help A help string. |
| 170 | 170 |
///\param value A default value for the option. |
| 171 | 171 |
///\param obl Indicate if the option is mandatory. |
| 172 | 172 |
ArgParser &stringOption(const std::string &name, |
| 173 | 173 |
const std::string &help, |
| 174 | 174 |
std::string value="", bool obl=false); |
| 175 | 175 |
|
| 176 | 176 |
///Give help string for non-parsed arguments. |
| 177 | 177 |
|
| 178 | 178 |
///With this function you can give help string for non-parsed arguments. |
| 179 | 179 |
///The parameter \c name will be printed in the short usage line, while |
| 180 | 180 |
///\c help gives a more detailed description. |
| 181 | 181 |
ArgParser &other(const std::string &name, |
| 182 | 182 |
const std::string &help=""); |
| 183 | 183 |
|
| 184 | 184 |
///@} |
| 185 | 185 |
|
| 186 | 186 |
///\name Options with External Storage |
| 187 | 187 |
///Using this functions, the value of the option will be directly written |
| 188 | 188 |
///into a variable once the option appears in the command line. |
| 189 | 189 |
|
| 190 | 190 |
///@{
|
| 191 | 191 |
|
| 192 | 192 |
///Add a new integer type option with a storage reference |
| 193 | 193 |
|
| 194 | 194 |
///Add a new integer type option with a storage reference. |
| 195 | 195 |
///\param name The name of the option. The leading '-' must be omitted. |
| 196 | 196 |
///\param help A help string. |
| 197 | 197 |
///\param obl Indicate if the option is mandatory. |
| 198 | 198 |
///\retval ref The value of the argument will be written to this variable. |
| 199 | 199 |
ArgParser &refOption(const std::string &name, |
| 200 | 200 |
const std::string &help, |
| 201 | 201 |
int &ref, bool obl=false); |
| 202 | 202 |
|
| 203 | 203 |
///Add a new floating type option with a storage reference |
| 204 | 204 |
|
| 205 | 205 |
///Add a new floating type option with a storage reference. |
| 206 | 206 |
///\param name The name of the option. The leading '-' must be omitted. |
| 207 | 207 |
///\param help A help string. |
| 208 | 208 |
///\param obl Indicate if the option is mandatory. |
| 209 | 209 |
///\retval ref The value of the argument will be written to this variable. |
| 210 | 210 |
ArgParser &refOption(const std::string &name, |
| 211 | 211 |
const std::string &help, |
| 212 | 212 |
double &ref, bool obl=false); |
| 213 | 213 |
|
| 214 | 214 |
///Add a new bool type option with a storage reference |
| 215 | 215 |
|
| 216 | 216 |
///Add a new bool type option with a storage reference. |
| 217 | 217 |
///\param name The name of the option. The leading '-' must be omitted. |
| 218 | 218 |
///\param help A help string. |
| 219 | 219 |
///\param obl Indicate if the option is mandatory. |
| 220 | 220 |
///\retval ref The value of the argument will be written to this variable. |
| 221 | 221 |
///\note A mandatory bool obtion is of very little use. |
| 222 | 222 |
ArgParser &refOption(const std::string &name, |
| 223 | 223 |
const std::string &help, |
| 224 | 224 |
bool &ref, bool obl=false); |
| 225 | 225 |
|
| 226 | 226 |
///Add a new string type option with a storage reference |
| 227 | 227 |
|
| 228 | 228 |
///Add a new string type option with a storage reference. |
| 229 | 229 |
///\param name The name of the option. The leading '-' must be omitted. |
| 230 | 230 |
///\param help A help string. |
| 231 | 231 |
///\param obl Indicate if the option is mandatory. |
| 232 | 232 |
///\retval ref The value of the argument will be written to this variable. |
| 233 | 233 |
ArgParser &refOption(const std::string &name, |
| 234 | 234 |
const std::string &help, |
| 235 | 235 |
std::string &ref, bool obl=false); |
| 236 | 236 |
|
| 237 | 237 |
///@} |
| 238 | 238 |
|
| 239 | 239 |
///\name Option Groups and Synonyms |
| 240 | 240 |
/// |
| 241 | 241 |
|
| 242 | 242 |
///@{
|
| 243 | 243 |
|
| 244 | 244 |
///Bundle some options into a group |
| 245 | 245 |
|
| 246 | 246 |
/// You can group some option by calling this function repeatedly for each |
| 247 | 247 |
/// option to be grouped with the same groupname. |
| 248 | 248 |
///\param group The group name. |
| 249 | 249 |
///\param opt The option name. |
| 250 | 250 |
ArgParser &optionGroup(const std::string &group, |
| 251 | 251 |
const std::string &opt); |
| 252 | 252 |
|
| 253 | 253 |
///Make the members of a group exclusive |
| 254 | 254 |
|
| 255 | 255 |
///If you call this function for a group, than at most one of them can be |
| 256 | 256 |
///given at the same time. |
| 257 | 257 |
ArgParser &onlyOneGroup(const std::string &group); |
| 258 | 258 |
|
| 259 | 259 |
///Make a group mandatory |
| 260 | 260 |
|
| 261 | 261 |
///Using this function, at least one of the members of \c group |
| 262 | 262 |
///must be given. |
| 263 | 263 |
ArgParser &mandatoryGroup(const std::string &group); |
| 264 | 264 |
|
| 265 | 265 |
///Create synonym to an option |
| 266 | 266 |
|
| 267 | 267 |
///With this function you can create a synonym \c syn of the |
| 268 | 268 |
///option \c opt. |
| 269 | 269 |
ArgParser &synonym(const std::string &syn, |
| 270 | 270 |
const std::string &opt); |
| 271 | 271 |
|
| 272 | 272 |
///@} |
| 273 | 273 |
|
| 274 | 274 |
private: |
| 275 | 275 |
void show(std::ostream &os,Opts::const_iterator i) const; |
| 276 | 276 |
void show(std::ostream &os,Groups::const_iterator i) const; |
| 277 | 277 |
void showHelp(Opts::const_iterator i) const; |
| 278 | 278 |
void showHelp(std::vector<OtherArg>::const_iterator i) const; |
| 279 | 279 |
|
| 280 | 280 |
void unknownOpt(std::string arg) const; |
| 281 | 281 |
|
| 282 | 282 |
void requiresValue(std::string arg, OptType t) const; |
| 283 | 283 |
void checkMandatories() const; |
| 284 | 284 |
|
| 285 | 285 |
void shortHelp() const; |
| 286 | 286 |
void showHelp() const; |
| 287 | 287 |
public: |
| 288 | 288 |
|
| 289 | 289 |
///Start the parsing process |
| 290 | 290 |
ArgParser &parse(); |
| 291 | 291 |
|
| 292 | 292 |
/// Synonym for parse() |
| 293 | 293 |
ArgParser &run() |
| 294 | 294 |
{
|
| 295 | 295 |
return parse(); |
| 296 | 296 |
} |
| 297 | 297 |
|
| 298 | 298 |
///Give back the command name (the 0th argument) |
| 299 | 299 |
const std::string &commandName() const { return _command_name; }
|
| 300 | 300 |
|
| 301 | 301 |
///Check if an opion has been given to the command. |
| 302 | 302 |
bool given(std::string op) const |
| 303 | 303 |
{
|
| 304 | 304 |
Opts::const_iterator i = _opts.find(op); |
| 305 | 305 |
return i!=_opts.end()?i->second.set:false; |
| 306 | 306 |
} |
| 307 | 307 |
|
| 308 | 308 |
|
| 309 | 309 |
///Magic type for operator[] |
| 310 | 310 |
|
| 311 | 311 |
///This is the type of the return value of ArgParser::operator[](). |
| 312 | 312 |
///It automatically converts to \c int, \c double, \c bool or |
| 313 | 313 |
///\c std::string if the type of the option matches, which is checked |
| 314 | 314 |
///with an \ref LEMON_ASSERT "assertion" (i.e. it performs runtime |
| 315 | 315 |
///type checking). |
| 316 | 316 |
class RefType |
| 317 | 317 |
{
|
| 318 | 318 |
const ArgParser &_parser; |
| 319 | 319 |
std::string _name; |
| 320 | 320 |
public: |
| 321 | 321 |
///\e |
| 322 | 322 |
RefType(const ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
|
| 323 | 323 |
///\e |
| 324 | 324 |
operator bool() |
| 325 | 325 |
{
|
| 326 | 326 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 327 | 327 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 328 | 328 |
std::string()+"Unkown option: '"+_name+"'"); |
| 329 | 329 |
LEMON_ASSERT(i->second.type==ArgParser::BOOL, |
| 330 | 330 |
std::string()+"'"+_name+"' is a bool option"); |
| 331 | 331 |
return *(i->second.bool_p); |
| 332 | 332 |
} |
| 333 | 333 |
///\e |
| 334 | 334 |
operator std::string() |
| 335 | 335 |
{
|
| 336 | 336 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 337 | 337 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 338 | 338 |
std::string()+"Unkown option: '"+_name+"'"); |
| 339 | 339 |
LEMON_ASSERT(i->second.type==ArgParser::STRING, |
| 340 | 340 |
std::string()+"'"+_name+"' is a string option"); |
| 341 | 341 |
return *(i->second.string_p); |
| 342 | 342 |
} |
| 343 | 343 |
///\e |
| 344 | 344 |
operator double() |
| 345 | 345 |
{
|
| 346 | 346 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 347 | 347 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 348 | 348 |
std::string()+"Unkown option: '"+_name+"'"); |
| 349 | 349 |
LEMON_ASSERT(i->second.type==ArgParser::DOUBLE || |
| 350 | 350 |
i->second.type==ArgParser::INTEGER, |
| 351 | 351 |
std::string()+"'"+_name+"' is a floating point option"); |
| 352 | 352 |
return i->second.type==ArgParser::DOUBLE ? |
| 353 | 353 |
*(i->second.double_p) : *(i->second.int_p); |
| 354 | 354 |
} |
| 355 | 355 |
///\e |
| 356 | 356 |
operator int() |
| 357 | 357 |
{
|
| 358 | 358 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 359 | 359 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 360 | 360 |
std::string()+"Unkown option: '"+_name+"'"); |
| 361 | 361 |
LEMON_ASSERT(i->second.type==ArgParser::INTEGER, |
| 362 | 362 |
std::string()+"'"+_name+"' is an integer option"); |
| 363 | 363 |
return *(i->second.int_p); |
| 364 | 364 |
} |
| 365 | 365 |
|
| 366 | 366 |
}; |
| 367 | 367 |
|
| 368 | 368 |
///Give back the value of an option |
| 369 | 369 |
|
| 370 | 370 |
///Give back the value of an option. |
| 371 | 371 |
///\sa RefType |
| 372 | 372 |
RefType operator[](const std::string &n) const |
| 373 | 373 |
{
|
| 374 | 374 |
return RefType(*this, n); |
| 375 | 375 |
} |
| 376 | 376 |
|
| 377 | 377 |
///Give back the non-option type arguments. |
| 378 | 378 |
|
| 379 | 379 |
///Give back a reference to a vector consisting of the program arguments |
| 380 | 380 |
///not starting with a '-' character. |
| 381 | 381 |
const std::vector<std::string> &files() const { return _file_args; }
|
| 382 | 382 |
|
| 383 | 383 |
}; |
| 384 | 384 |
} |
| 385 | 385 |
|
| 386 | 386 |
#endif // LEMON_ARG_PARSER_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ASSERT_H |
| 20 | 20 |
#define LEMON_ASSERT_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup exceptions |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Extended assertion handling |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/error.h> |
| 27 | 27 |
|
| 28 | 28 |
namespace lemon {
|
| 29 | 29 |
|
| 30 | 30 |
inline void assert_fail_abort(const char *file, int line, |
| 31 | 31 |
const char *function, const char* message, |
| 32 | 32 |
const char *assertion) |
| 33 | 33 |
{
|
| 34 | 34 |
std::cerr << file << ":" << line << ": "; |
| 35 | 35 |
if (function) |
| 36 | 36 |
std::cerr << function << ": "; |
| 37 | 37 |
std::cerr << message; |
| 38 | 38 |
if (assertion) |
| 39 | 39 |
std::cerr << " (assertion '" << assertion << "' failed)"; |
| 40 | 40 |
std::cerr << std::endl; |
| 41 | 41 |
std::abort(); |
| 42 | 42 |
} |
| 43 | 43 |
|
| 44 | 44 |
namespace _assert_bits {
|
| 45 | 45 |
|
| 46 | 46 |
|
| 47 | 47 |
inline const char* cstringify(const std::string& str) {
|
| 48 | 48 |
return str.c_str(); |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 | 51 |
inline const char* cstringify(const char* str) {
|
| 52 | 52 |
return str; |
| 53 | 53 |
} |
| 54 | 54 |
} |
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 | 57 |
#endif // LEMON_ASSERT_H |
| 58 | 58 |
|
| 59 | 59 |
#undef LEMON_ASSERT |
| 60 | 60 |
#undef LEMON_DEBUG |
| 61 | 61 |
|
| 62 | 62 |
#if (defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
| 63 | 63 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) > 1 |
| 64 | 64 |
#error "LEMON assertion system is not set properly" |
| 65 | 65 |
#endif |
| 66 | 66 |
|
| 67 | 67 |
#if ((defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
| 68 | 68 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) == 1 || \ |
| 69 | 69 |
defined(LEMON_ENABLE_ASSERTS)) && \ |
| 70 | 70 |
(defined(LEMON_DISABLE_ASSERTS) || \ |
| 71 | 71 |
defined(NDEBUG)) |
| 72 | 72 |
#error "LEMON assertion system is not set properly" |
| 73 | 73 |
#endif |
| 74 | 74 |
|
| 75 | 75 |
|
| 76 | 76 |
#if defined LEMON_ASSERT_ABORT |
| 77 | 77 |
# undef LEMON_ASSERT_HANDLER |
| 78 | 78 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
| 79 | 79 |
#elif defined LEMON_ASSERT_CUSTOM |
| 80 | 80 |
# undef LEMON_ASSERT_HANDLER |
| 81 | 81 |
# ifndef LEMON_CUSTOM_ASSERT_HANDLER |
| 82 | 82 |
# error "LEMON_CUSTOM_ASSERT_HANDLER is not set" |
| 83 | 83 |
# endif |
| 84 | 84 |
# define LEMON_ASSERT_HANDLER LEMON_CUSTOM_ASSERT_HANDLER |
| 85 | 85 |
#elif defined LEMON_DISABLE_ASSERTS |
| 86 | 86 |
# undef LEMON_ASSERT_HANDLER |
| 87 | 87 |
#elif defined NDEBUG |
| 88 | 88 |
# undef LEMON_ASSERT_HANDLER |
| 89 | 89 |
#else |
| 90 | 90 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
| 91 | 91 |
#endif |
| 92 | 92 |
|
| 93 | 93 |
#ifndef LEMON_FUNCTION_NAME |
| 94 | 94 |
# if defined __GNUC__ |
| 95 | 95 |
# define LEMON_FUNCTION_NAME (__PRETTY_FUNCTION__) |
| 96 | 96 |
# elif defined _MSC_VER |
| 97 | 97 |
# define LEMON_FUNCTION_NAME (__FUNCSIG__) |
| 98 | 98 |
# elif __STDC_VERSION__ >= 199901L |
| 99 | 99 |
# define LEMON_FUNCTION_NAME (__func__) |
| 100 | 100 |
# else |
| 101 | 101 |
# define LEMON_FUNCTION_NAME ("<unknown>")
|
| 102 | 102 |
# endif |
| 103 | 103 |
#endif |
| 104 | 104 |
|
| 105 | 105 |
#ifdef DOXYGEN |
| 106 | 106 |
|
| 107 | 107 |
/// \ingroup exceptions |
| 108 | 108 |
/// |
| 109 | 109 |
/// \brief Macro for assertion with customizable message |
| 110 | 110 |
/// |
| 111 | 111 |
/// Macro for assertion with customizable message. |
| 112 | 112 |
/// \param exp An expression that must be convertible to \c bool. If it is \c |
| 113 | 113 |
/// false, then an assertion is raised. The concrete behaviour depends on the |
| 114 | 114 |
/// settings of the assertion system. |
| 115 | 115 |
/// \param msg A <tt>const char*</tt> parameter, which can be used to provide |
| 116 | 116 |
/// information about the circumstances of the failed assertion. |
| 117 | 117 |
/// |
| 118 | 118 |
/// The assertions are enabled in the default behaviour. |
| 119 | 119 |
/// You can disable them with the following code: |
| 120 | 120 |
/// \code |
| 121 | 121 |
/// #define LEMON_DISABLE_ASSERTS |
| 122 | 122 |
/// \endcode |
| 123 | 123 |
/// or with compilation parameters: |
| 124 | 124 |
/// \code |
| 125 | 125 |
/// g++ -DLEMON_DISABLE_ASSERTS |
| 126 | 126 |
/// make CXXFLAGS='-DLEMON_DISABLE_ASSERTS' |
| 127 | 127 |
/// \endcode |
| 128 | 128 |
/// The checking is also disabled when the standard macro \c NDEBUG is defined. |
| 129 | 129 |
/// |
| 130 | 130 |
/// As a default behaviour the failed assertion prints a short log message to |
| 131 | 131 |
/// the standard error and aborts the execution. |
| 132 | 132 |
/// |
| 133 | 133 |
/// However, the following modes can be used in the assertion system: |
| 134 | 134 |
/// - \c LEMON_ASSERT_ABORT The failed assertion prints a short log message to |
| 135 | 135 |
/// the standard error and aborts the program. It is the default behaviour. |
| 136 | 136 |
/// - \c LEMON_ASSERT_CUSTOM The user can define own assertion handler |
| 137 | 137 |
/// function. |
| 138 | 138 |
/// \code |
| 139 | 139 |
/// void custom_assert_handler(const char* file, int line, |
| 140 | 140 |
/// const char* function, const char* message, |
| 141 | 141 |
/// const char* assertion); |
| 142 | 142 |
/// \endcode |
| 143 | 143 |
/// The name of the function should be defined as the \c |
| 144 | 144 |
/// LEMON_CUSTOM_ASSERT_HANDLER macro name. |
| 145 | 145 |
/// \code |
| 146 | 146 |
/// #define LEMON_CUSTOM_ASSERT_HANDLER custom_assert_handler |
| 147 | 147 |
/// \endcode |
| 148 | 148 |
/// Whenever an assertion is occured, the custom assertion |
| 149 | 149 |
/// handler is called with appropiate parameters. |
| 150 | 150 |
/// |
| 151 | 151 |
/// The assertion mode can also be changed within one compilation unit. |
| 152 | 152 |
/// If the macros are redefined with other settings and the |
| 153 | 153 |
/// \ref lemon/assert.h "assert.h" file is reincluded, then the |
| 154 | 154 |
/// behaviour is changed appropiately to the new settings. |
| 155 | 155 |
# define LEMON_ASSERT(exp, msg) \ |
| 156 | 156 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
| 157 | 157 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
| 158 | 158 |
LEMON_FUNCTION_NAME, \ |
| 159 | 159 |
::lemon::_assert_bits::cstringify(msg), #exp), 0))) |
| 160 | 160 |
|
| 161 | 161 |
/// \ingroup exceptions |
| 162 | 162 |
/// |
| 163 | 163 |
/// \brief Macro for internal assertions |
| 164 | 164 |
/// |
| 165 | 165 |
/// Macro for internal assertions, it is used in the library to check |
| 166 | 166 |
/// the consistency of results of algorithms, several pre- and |
| 167 | 167 |
/// postconditions and invariants. The checking is disabled by |
| 168 | 168 |
/// default, but it can be turned on with the macro \c |
| 169 | 169 |
/// LEMON_ENABLE_DEBUG. |
| 170 | 170 |
/// \code |
| 171 | 171 |
/// #define LEMON_ENABLE_DEBUG |
| 172 | 172 |
/// \endcode |
| 173 | 173 |
/// or with compilation parameters: |
| 174 | 174 |
/// \code |
| 175 | 175 |
/// g++ -DLEMON_ENABLE_DEBUG |
| 176 | 176 |
/// make CXXFLAGS='-DLEMON_ENABLE_DEBUG' |
| 177 | 177 |
/// \endcode |
| 178 | 178 |
/// |
| 179 | 179 |
/// This macro works like the \c LEMON_ASSERT macro, therefore the |
| 180 | 180 |
/// current behaviour depends on the settings of \c LEMON_ASSERT |
| 181 | 181 |
/// macro. |
| 182 | 182 |
/// |
| 183 | 183 |
/// \see LEMON_ASSERT |
| 184 | 184 |
# define LEMON_DEBUG(exp, msg) \ |
| 185 | 185 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
| 186 | 186 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
| 187 | 187 |
LEMON_FUNCTION_NAME, \ |
| 188 | 188 |
::lemon::_assert_bits::cstringify(msg), #exp), 0))) |
| 189 | 189 |
|
| 190 | 190 |
#else |
| 191 | 191 |
|
| 192 | 192 |
# ifndef LEMON_ASSERT_HANDLER |
| 193 | 193 |
# define LEMON_ASSERT(exp, msg) (static_cast<void>(0)) |
| 194 | 194 |
# define LEMON_DEBUG(exp, msg) (static_cast<void>(0)) |
| 195 | 195 |
# else |
| 196 | 196 |
# define LEMON_ASSERT(exp, msg) \ |
| 197 | 197 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
| 198 | 198 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
| 199 | 199 |
LEMON_FUNCTION_NAME, \ |
| 200 | 200 |
::lemon::_assert_bits::cstringify(msg), \ |
| 201 | 201 |
#exp), 0))) |
| 202 | 202 |
# if LEMON_ENABLE_DEBUG |
| 203 | 203 |
# define LEMON_DEBUG(exp, msg) \ |
| 204 | 204 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
| 205 | 205 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
| 206 | 206 |
LEMON_FUNCTION_NAME, \ |
| 207 | 207 |
::lemon::_assert_bits::cstringify(msg), \ |
| 208 | 208 |
#exp), 0))) |
| 209 | 209 |
# else |
| 210 | 210 |
# define LEMON_DEBUG(exp, msg) (static_cast<void>(0)) |
| 211 | 211 |
# endif |
| 212 | 212 |
# endif |
| 213 | 213 |
|
| 214 | 214 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\file |
| 20 | 20 |
///\brief Some basic non-inline functions and static global data. |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/tolerance.h> |
| 23 | 23 |
#include<lemon/core.h> |
| 24 | 24 |
namespace lemon {
|
| 25 | 25 |
|
| 26 | 26 |
float Tolerance<float>::def_epsilon = 1e-4; |
| 27 | 27 |
double Tolerance<double>::def_epsilon = 1e-10; |
| 28 | 28 |
long double Tolerance<long double>::def_epsilon = 1e-14; |
| 29 | 29 |
|
| 30 | 30 |
#ifndef LEMON_ONLY_TEMPLATES |
| 31 | 31 |
const Invalid INVALID = Invalid(); |
| 32 | 32 |
#endif |
| 33 | 33 |
|
| 34 | 34 |
} //namespace lemon |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BFS_H |
| 20 | 20 |
#define LEMON_BFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief BFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/path.h> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
///Default traits class of Bfs class. |
| 36 | 36 |
|
| 37 | 37 |
///Default traits class of Bfs class. |
| 38 | 38 |
///\tparam GR Digraph type. |
| 39 | 39 |
template<class GR> |
| 40 | 40 |
struct BfsDefaultTraits |
| 41 | 41 |
{
|
| 42 | 42 |
///The type of the digraph the algorithm runs on. |
| 43 | 43 |
typedef GR Digraph; |
| 44 | 44 |
|
| 45 | 45 |
///\brief The type of the map that stores the predecessor |
| 46 | 46 |
///arcs of the shortest paths. |
| 47 | 47 |
/// |
| 48 | 48 |
///The type of the map that stores the predecessor |
| 49 | 49 |
///arcs of the shortest paths. |
| 50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 52 | 52 |
///Instantiates a PredMap. |
| 53 | 53 |
|
| 54 | 54 |
///This function instantiates a PredMap. |
| 55 | 55 |
///\param g is the digraph, to which we would like to define the |
| 56 | 56 |
///PredMap. |
| 57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
| 58 | 58 |
{
|
| 59 | 59 |
return new PredMap(g); |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
///The type of the map that indicates which nodes are processed. |
| 63 | 63 |
|
| 64 | 64 |
///The type of the map that indicates which nodes are processed. |
| 65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 67 | 67 |
///Instantiates a ProcessedMap. |
| 68 | 68 |
|
| 69 | 69 |
///This function instantiates a ProcessedMap. |
| 70 | 70 |
///\param g is the digraph, to which |
| 71 | 71 |
///we would like to define the ProcessedMap |
| 72 | 72 |
#ifdef DOXYGEN |
| 73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 74 | 74 |
#else |
| 75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 76 | 76 |
#endif |
| 77 | 77 |
{
|
| 78 | 78 |
return new ProcessedMap(); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
///The type of the map that indicates which nodes are reached. |
| 82 | 82 |
|
| 83 | 83 |
///The type of the map that indicates which nodes are reached. |
| 84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 86 | 86 |
///Instantiates a ReachedMap. |
| 87 | 87 |
|
| 88 | 88 |
///This function instantiates a ReachedMap. |
| 89 | 89 |
///\param g is the digraph, to which |
| 90 | 90 |
///we would like to define the ReachedMap. |
| 91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 92 | 92 |
{
|
| 93 | 93 |
return new ReachedMap(g); |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
///The type of the map that stores the distances of the nodes. |
| 97 | 97 |
|
| 98 | 98 |
///The type of the map that stores the distances of the nodes. |
| 99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 101 | 101 |
///Instantiates a DistMap. |
| 102 | 102 |
|
| 103 | 103 |
///This function instantiates a DistMap. |
| 104 | 104 |
///\param g is the digraph, to which we would like to define the |
| 105 | 105 |
///DistMap. |
| 106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
| 107 | 107 |
{
|
| 108 | 108 |
return new DistMap(g); |
| 109 | 109 |
} |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
///%BFS algorithm class. |
| 113 | 113 |
|
| 114 | 114 |
///\ingroup search |
| 115 | 115 |
///This class provides an efficient implementation of the %BFS algorithm. |
| 116 | 116 |
/// |
| 117 | 117 |
///There is also a \ref bfs() "function-type interface" for the BFS |
| 118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
| 119 | 119 |
///used easier. |
| 120 | 120 |
/// |
| 121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 122 | 122 |
///The default type is \ref ListDigraph. |
| 123 | 123 |
#ifdef DOXYGEN |
| 124 | 124 |
template <typename GR, |
| 125 | 125 |
typename TR> |
| 126 | 126 |
#else |
| 127 | 127 |
template <typename GR=ListDigraph, |
| 128 | 128 |
typename TR=BfsDefaultTraits<GR> > |
| 129 | 129 |
#endif |
| 130 | 130 |
class Bfs {
|
| 131 | 131 |
public: |
| 132 | 132 |
|
| 133 | 133 |
///The type of the digraph the algorithm runs on. |
| 134 | 134 |
typedef typename TR::Digraph Digraph; |
| 135 | 135 |
|
| 136 | 136 |
///\brief The type of the map that stores the predecessor arcs of the |
| 137 | 137 |
///shortest paths. |
| 138 | 138 |
typedef typename TR::PredMap PredMap; |
| 139 | 139 |
///The type of the map that stores the distances of the nodes. |
| 140 | 140 |
typedef typename TR::DistMap DistMap; |
| 141 | 141 |
///The type of the map that indicates which nodes are reached. |
| 142 | 142 |
typedef typename TR::ReachedMap ReachedMap; |
| 143 | 143 |
///The type of the map that indicates which nodes are processed. |
| 144 | 144 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 145 | 145 |
///The type of the paths. |
| 146 | 146 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 147 | 147 |
|
| 148 | 148 |
///The \ref BfsDefaultTraits "traits class" of the algorithm. |
| 149 | 149 |
typedef TR Traits; |
| 150 | 150 |
|
| 151 | 151 |
private: |
| 152 | 152 |
|
| 153 | 153 |
typedef typename Digraph::Node Node; |
| 154 | 154 |
typedef typename Digraph::NodeIt NodeIt; |
| 155 | 155 |
typedef typename Digraph::Arc Arc; |
| 156 | 156 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 157 | 157 |
|
| 158 | 158 |
//Pointer to the underlying digraph. |
| 159 | 159 |
const Digraph *G; |
| 160 | 160 |
//Pointer to the map of predecessor arcs. |
| 161 | 161 |
PredMap *_pred; |
| 162 | 162 |
//Indicates if _pred is locally allocated (true) or not. |
| 163 | 163 |
bool local_pred; |
| 164 | 164 |
//Pointer to the map of distances. |
| 165 | 165 |
DistMap *_dist; |
| 166 | 166 |
//Indicates if _dist is locally allocated (true) or not. |
| 167 | 167 |
bool local_dist; |
| 168 | 168 |
//Pointer to the map of reached status of the nodes. |
| 169 | 169 |
ReachedMap *_reached; |
| 170 | 170 |
//Indicates if _reached is locally allocated (true) or not. |
| 171 | 171 |
bool local_reached; |
| 172 | 172 |
//Pointer to the map of processed status of the nodes. |
| 173 | 173 |
ProcessedMap *_processed; |
| 174 | 174 |
//Indicates if _processed is locally allocated (true) or not. |
| 175 | 175 |
bool local_processed; |
| 176 | 176 |
|
| 177 | 177 |
std::vector<typename Digraph::Node> _queue; |
| 178 | 178 |
int _queue_head,_queue_tail,_queue_next_dist; |
| 179 | 179 |
int _curr_dist; |
| 180 | 180 |
|
| 181 | 181 |
//Creates the maps if necessary. |
| 182 | 182 |
void create_maps() |
| 183 | 183 |
{
|
| 184 | 184 |
if(!_pred) {
|
| 185 | 185 |
local_pred = true; |
| 186 | 186 |
_pred = Traits::createPredMap(*G); |
| 187 | 187 |
} |
| 188 | 188 |
if(!_dist) {
|
| 189 | 189 |
local_dist = true; |
| 190 | 190 |
_dist = Traits::createDistMap(*G); |
| 191 | 191 |
} |
| 192 | 192 |
if(!_reached) {
|
| 193 | 193 |
local_reached = true; |
| 194 | 194 |
_reached = Traits::createReachedMap(*G); |
| 195 | 195 |
} |
| 196 | 196 |
if(!_processed) {
|
| 197 | 197 |
local_processed = true; |
| 198 | 198 |
_processed = Traits::createProcessedMap(*G); |
| 199 | 199 |
} |
| 200 | 200 |
} |
| 201 | 201 |
|
| 202 | 202 |
protected: |
| 203 | 203 |
|
| 204 | 204 |
Bfs() {}
|
| 205 | 205 |
|
| 206 | 206 |
public: |
| 207 | 207 |
|
| 208 | 208 |
typedef Bfs Create; |
| 209 | 209 |
|
| 210 | 210 |
///\name Named Template Parameters |
| 211 | 211 |
|
| 212 | 212 |
///@{
|
| 213 | 213 |
|
| 214 | 214 |
template <class T> |
| 215 | 215 |
struct SetPredMapTraits : public Traits {
|
| 216 | 216 |
typedef T PredMap; |
| 217 | 217 |
static PredMap *createPredMap(const Digraph &) |
| 218 | 218 |
{
|
| 219 | 219 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 220 | 220 |
return 0; // ignore warnings |
| 221 | 221 |
} |
| 222 | 222 |
}; |
| 223 | 223 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 224 | 224 |
///PredMap type. |
| 225 | 225 |
/// |
| 226 | 226 |
///\ref named-templ-param "Named parameter" for setting |
| 227 | 227 |
///PredMap type. |
| 228 | 228 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 229 | 229 |
template <class T> |
| 230 | 230 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
|
| 231 | 231 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
| 232 | 232 |
}; |
| 233 | 233 |
|
| 234 | 234 |
template <class T> |
| 235 | 235 |
struct SetDistMapTraits : public Traits {
|
| 236 | 236 |
typedef T DistMap; |
| 237 | 237 |
static DistMap *createDistMap(const Digraph &) |
| 238 | 238 |
{
|
| 239 | 239 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 240 | 240 |
return 0; // ignore warnings |
| 241 | 241 |
} |
| 242 | 242 |
}; |
| 243 | 243 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 244 | 244 |
///DistMap type. |
| 245 | 245 |
/// |
| 246 | 246 |
///\ref named-templ-param "Named parameter" for setting |
| 247 | 247 |
///DistMap type. |
| 248 | 248 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 249 | 249 |
template <class T> |
| 250 | 250 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
|
| 251 | 251 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
| 252 | 252 |
}; |
| 253 | 253 |
|
| 254 | 254 |
template <class T> |
| 255 | 255 |
struct SetReachedMapTraits : public Traits {
|
| 256 | 256 |
typedef T ReachedMap; |
| 257 | 257 |
static ReachedMap *createReachedMap(const Digraph &) |
| 258 | 258 |
{
|
| 259 | 259 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 260 | 260 |
return 0; // ignore warnings |
| 261 | 261 |
} |
| 262 | 262 |
}; |
| 263 | 263 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 264 | 264 |
///ReachedMap type. |
| 265 | 265 |
/// |
| 266 | 266 |
///\ref named-templ-param "Named parameter" for setting |
| 267 | 267 |
///ReachedMap type. |
| 268 | 268 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 269 | 269 |
template <class T> |
| 270 | 270 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
|
| 271 | 271 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
| 272 | 272 |
}; |
| 273 | 273 |
|
| 274 | 274 |
template <class T> |
| 275 | 275 |
struct SetProcessedMapTraits : public Traits {
|
| 276 | 276 |
typedef T ProcessedMap; |
| 277 | 277 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 278 | 278 |
{
|
| 279 | 279 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 280 | 280 |
return 0; // ignore warnings |
| 281 | 281 |
} |
| 282 | 282 |
}; |
| 283 | 283 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 284 | 284 |
///ProcessedMap type. |
| 285 | 285 |
/// |
| 286 | 286 |
///\ref named-templ-param "Named parameter" for setting |
| 287 | 287 |
///ProcessedMap type. |
| 288 | 288 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 289 | 289 |
template <class T> |
| 290 | 290 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
|
| 291 | 291 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
| 292 | 292 |
}; |
| 293 | 293 |
|
| 294 | 294 |
struct SetStandardProcessedMapTraits : public Traits {
|
| 295 | 295 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
| 296 | 296 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 297 | 297 |
{
|
| 298 | 298 |
return new ProcessedMap(g); |
| 299 | 299 |
return 0; // ignore warnings |
| 300 | 300 |
} |
| 301 | 301 |
}; |
| 302 | 302 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 303 | 303 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
| 304 | 304 |
/// |
| 305 | 305 |
///\ref named-templ-param "Named parameter" for setting |
| 306 | 306 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
| 307 | 307 |
///If you don't set it explicitly, it will be automatically allocated. |
| 308 | 308 |
struct SetStandardProcessedMap : |
| 309 | 309 |
public Bfs< Digraph, SetStandardProcessedMapTraits > {
|
| 310 | 310 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
| 311 | 311 |
}; |
| 312 | 312 |
|
| 313 | 313 |
///@} |
| 314 | 314 |
|
| 315 | 315 |
public: |
| 316 | 316 |
|
| 317 | 317 |
///Constructor. |
| 318 | 318 |
|
| 319 | 319 |
///Constructor. |
| 320 | 320 |
///\param g The digraph the algorithm runs on. |
| 321 | 321 |
Bfs(const Digraph &g) : |
| 322 | 322 |
G(&g), |
| 323 | 323 |
_pred(NULL), local_pred(false), |
| 324 | 324 |
_dist(NULL), local_dist(false), |
| 325 | 325 |
_reached(NULL), local_reached(false), |
| 326 | 326 |
_processed(NULL), local_processed(false) |
| 327 | 327 |
{ }
|
| 328 | 328 |
|
| 329 | 329 |
///Destructor. |
| 330 | 330 |
~Bfs() |
| 331 | 331 |
{
|
| 332 | 332 |
if(local_pred) delete _pred; |
| 333 | 333 |
if(local_dist) delete _dist; |
| 334 | 334 |
if(local_reached) delete _reached; |
| 335 | 335 |
if(local_processed) delete _processed; |
| 336 | 336 |
} |
| 337 | 337 |
|
| 338 | 338 |
///Sets the map that stores the predecessor arcs. |
| 339 | 339 |
|
| 340 | 340 |
///Sets the map that stores the predecessor arcs. |
| 341 | 341 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 342 | 342 |
///or \ref init(), an instance will be allocated automatically. |
| 343 | 343 |
///The destructor deallocates this automatically allocated map, |
| 344 | 344 |
///of course. |
| 345 | 345 |
///\return <tt> (*this) </tt> |
| 346 | 346 |
Bfs &predMap(PredMap &m) |
| 347 | 347 |
{
|
| 348 | 348 |
if(local_pred) {
|
| 349 | 349 |
delete _pred; |
| 350 | 350 |
local_pred=false; |
| 351 | 351 |
} |
| 352 | 352 |
_pred = &m; |
| 353 | 353 |
return *this; |
| 354 | 354 |
} |
| 355 | 355 |
|
| 356 | 356 |
///Sets the map that indicates which nodes are reached. |
| 357 | 357 |
|
| 358 | 358 |
///Sets the map that indicates which nodes are reached. |
| 359 | 359 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 360 | 360 |
///or \ref init(), an instance will be allocated automatically. |
| 361 | 361 |
///The destructor deallocates this automatically allocated map, |
| 362 | 362 |
///of course. |
| 363 | 363 |
///\return <tt> (*this) </tt> |
| 364 | 364 |
Bfs &reachedMap(ReachedMap &m) |
| 365 | 365 |
{
|
| 366 | 366 |
if(local_reached) {
|
| 367 | 367 |
delete _reached; |
| 368 | 368 |
local_reached=false; |
| 369 | 369 |
} |
| 370 | 370 |
_reached = &m; |
| 371 | 371 |
return *this; |
| 372 | 372 |
} |
| 373 | 373 |
|
| 374 | 374 |
///Sets the map that indicates which nodes are processed. |
| 375 | 375 |
|
| 376 | 376 |
///Sets the map that indicates which nodes are processed. |
| 377 | 377 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 378 | 378 |
///or \ref init(), an instance will be allocated automatically. |
| 379 | 379 |
///The destructor deallocates this automatically allocated map, |
| 380 | 380 |
///of course. |
| 381 | 381 |
///\return <tt> (*this) </tt> |
| 382 | 382 |
Bfs &processedMap(ProcessedMap &m) |
| 383 | 383 |
{
|
| 384 | 384 |
if(local_processed) {
|
| 385 | 385 |
delete _processed; |
| 386 | 386 |
local_processed=false; |
| 387 | 387 |
} |
| 388 | 388 |
_processed = &m; |
| 389 | 389 |
return *this; |
| 390 | 390 |
} |
| 391 | 391 |
|
| 392 | 392 |
///Sets the map that stores the distances of the nodes. |
| 393 | 393 |
|
| 394 | 394 |
///Sets the map that stores the distances of the nodes calculated by |
| 395 | 395 |
///the algorithm. |
| 396 | 396 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 397 | 397 |
///or \ref init(), an instance will be allocated automatically. |
| 398 | 398 |
///The destructor deallocates this automatically allocated map, |
| 399 | 399 |
///of course. |
| 400 | 400 |
///\return <tt> (*this) </tt> |
| 401 | 401 |
Bfs &distMap(DistMap &m) |
| 402 | 402 |
{
|
| 403 | 403 |
if(local_dist) {
|
| 404 | 404 |
delete _dist; |
| 405 | 405 |
local_dist=false; |
| 406 | 406 |
} |
| 407 | 407 |
_dist = &m; |
| 408 | 408 |
return *this; |
| 409 | 409 |
} |
| 410 | 410 |
|
| 411 | 411 |
public: |
| 412 | 412 |
|
| 413 | 413 |
///\name Execution Control |
| 414 | 414 |
///The simplest way to execute the BFS algorithm is to use one of the |
| 415 | 415 |
///member functions called \ref run(Node) "run()".\n |
| 416 | 416 |
///If you need more control on the execution, first you have to call |
| 417 | 417 |
///\ref init(), then you can add several source nodes with |
| 418 | 418 |
///\ref addSource(). Finally the actual path computation can be |
| 419 | 419 |
///performed with one of the \ref start() functions. |
| 420 | 420 |
|
| 421 | 421 |
///@{
|
| 422 | 422 |
|
| 423 | 423 |
///\brief Initializes the internal data structures. |
| 424 | 424 |
/// |
| 425 | 425 |
///Initializes the internal data structures. |
| 426 | 426 |
void init() |
| 427 | 427 |
{
|
| 428 | 428 |
create_maps(); |
| 429 | 429 |
_queue.resize(countNodes(*G)); |
| 430 | 430 |
_queue_head=_queue_tail=0; |
| 431 | 431 |
_curr_dist=1; |
| 432 | 432 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
| 433 | 433 |
_pred->set(u,INVALID); |
| 434 | 434 |
_reached->set(u,false); |
| 435 | 435 |
_processed->set(u,false); |
| 436 | 436 |
} |
| 437 | 437 |
} |
| 438 | 438 |
|
| 439 | 439 |
///Adds a new source node. |
| 440 | 440 |
|
| 441 | 441 |
///Adds a new source node to the set of nodes to be processed. |
| 442 | 442 |
/// |
| 443 | 443 |
void addSource(Node s) |
| 444 | 444 |
{
|
| 445 | 445 |
if(!(*_reached)[s]) |
| 446 | 446 |
{
|
| 447 | 447 |
_reached->set(s,true); |
| 448 | 448 |
_pred->set(s,INVALID); |
| 449 | 449 |
_dist->set(s,0); |
| 450 | 450 |
_queue[_queue_head++]=s; |
| 451 | 451 |
_queue_next_dist=_queue_head; |
| 452 | 452 |
} |
| 453 | 453 |
} |
| 454 | 454 |
|
| 455 | 455 |
///Processes the next node. |
| 456 | 456 |
|
| 457 | 457 |
///Processes the next node. |
| 458 | 458 |
/// |
| 459 | 459 |
///\return The processed node. |
| 460 | 460 |
/// |
| 461 | 461 |
///\pre The queue must not be empty. |
| 462 | 462 |
Node processNextNode() |
| 463 | 463 |
{
|
| 464 | 464 |
if(_queue_tail==_queue_next_dist) {
|
| 465 | 465 |
_curr_dist++; |
| 466 | 466 |
_queue_next_dist=_queue_head; |
| 467 | 467 |
} |
| 468 | 468 |
Node n=_queue[_queue_tail++]; |
| 469 | 469 |
_processed->set(n,true); |
| 470 | 470 |
Node m; |
| 471 | 471 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
| 472 | 472 |
if(!(*_reached)[m=G->target(e)]) {
|
| 473 | 473 |
_queue[_queue_head++]=m; |
| 474 | 474 |
_reached->set(m,true); |
| 475 | 475 |
_pred->set(m,e); |
| 476 | 476 |
_dist->set(m,_curr_dist); |
| 477 | 477 |
} |
| 478 | 478 |
return n; |
| 479 | 479 |
} |
| 480 | 480 |
|
| 481 | 481 |
///Processes the next node. |
| 482 | 482 |
|
| 483 | 483 |
///Processes the next node and checks if the given target node |
| 484 | 484 |
///is reached. If the target node is reachable from the processed |
| 485 | 485 |
///node, then the \c reach parameter will be set to \c true. |
| 486 | 486 |
/// |
| 487 | 487 |
///\param target The target node. |
| 488 | 488 |
///\retval reach Indicates if the target node is reached. |
| 489 | 489 |
///It should be initially \c false. |
| 490 | 490 |
/// |
| 491 | 491 |
///\return The processed node. |
| 492 | 492 |
/// |
| 493 | 493 |
///\pre The queue must not be empty. |
| 494 | 494 |
Node processNextNode(Node target, bool& reach) |
| 495 | 495 |
{
|
| 496 | 496 |
if(_queue_tail==_queue_next_dist) {
|
| 497 | 497 |
_curr_dist++; |
| 498 | 498 |
_queue_next_dist=_queue_head; |
| 499 | 499 |
} |
| 500 | 500 |
Node n=_queue[_queue_tail++]; |
| 501 | 501 |
_processed->set(n,true); |
| 502 | 502 |
Node m; |
| 503 | 503 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
| 504 | 504 |
if(!(*_reached)[m=G->target(e)]) {
|
| 505 | 505 |
_queue[_queue_head++]=m; |
| 506 | 506 |
_reached->set(m,true); |
| 507 | 507 |
_pred->set(m,e); |
| 508 | 508 |
_dist->set(m,_curr_dist); |
| 509 | 509 |
reach = reach || (target == m); |
| 510 | 510 |
} |
| 511 | 511 |
return n; |
| 512 | 512 |
} |
| 513 | 513 |
|
| 514 | 514 |
///Processes the next node. |
| 515 | 515 |
|
| 516 | 516 |
///Processes the next node and checks if at least one of reached |
| 517 | 517 |
///nodes has \c true value in the \c nm node map. If one node |
| 518 | 518 |
///with \c true value is reachable from the processed node, then the |
| 519 | 519 |
///\c rnode parameter will be set to the first of such nodes. |
| 520 | 520 |
/// |
| 521 | 521 |
///\param nm A \c bool (or convertible) node map that indicates the |
| 522 | 522 |
///possible targets. |
| 523 | 523 |
///\retval rnode The reached target node. |
| 524 | 524 |
///It should be initially \c INVALID. |
| 525 | 525 |
/// |
| 526 | 526 |
///\return The processed node. |
| 527 | 527 |
/// |
| 528 | 528 |
///\pre The queue must not be empty. |
| 529 | 529 |
template<class NM> |
| 530 | 530 |
Node processNextNode(const NM& nm, Node& rnode) |
| 531 | 531 |
{
|
| 532 | 532 |
if(_queue_tail==_queue_next_dist) {
|
| 533 | 533 |
_curr_dist++; |
| 534 | 534 |
_queue_next_dist=_queue_head; |
| 535 | 535 |
} |
| 536 | 536 |
Node n=_queue[_queue_tail++]; |
| 537 | 537 |
_processed->set(n,true); |
| 538 | 538 |
Node m; |
| 539 | 539 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
| 540 | 540 |
if(!(*_reached)[m=G->target(e)]) {
|
| 541 | 541 |
_queue[_queue_head++]=m; |
| 542 | 542 |
_reached->set(m,true); |
| 543 | 543 |
_pred->set(m,e); |
| 544 | 544 |
_dist->set(m,_curr_dist); |
| 545 | 545 |
if (nm[m] && rnode == INVALID) rnode = m; |
| 546 | 546 |
} |
| 547 | 547 |
return n; |
| 548 | 548 |
} |
| 549 | 549 |
|
| 550 | 550 |
///The next node to be processed. |
| 551 | 551 |
|
| 552 | 552 |
///Returns the next node to be processed or \c INVALID if the queue |
| 553 | 553 |
///is empty. |
| 554 | 554 |
Node nextNode() const |
| 555 | 555 |
{
|
| 556 | 556 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
| 557 | 557 |
} |
| 558 | 558 |
|
| 559 | 559 |
///Returns \c false if there are nodes to be processed. |
| 560 | 560 |
|
| 561 | 561 |
///Returns \c false if there are nodes to be processed |
| 562 | 562 |
///in the queue. |
| 563 | 563 |
bool emptyQueue() const { return _queue_tail==_queue_head; }
|
| 564 | 564 |
|
| 565 | 565 |
///Returns the number of the nodes to be processed. |
| 566 | 566 |
|
| 567 | 567 |
///Returns the number of the nodes to be processed |
| 568 | 568 |
///in the queue. |
| 569 | 569 |
int queueSize() const { return _queue_head-_queue_tail; }
|
| 570 | 570 |
|
| 571 | 571 |
///Executes the algorithm. |
| 572 | 572 |
|
| 573 | 573 |
///Executes the algorithm. |
| 574 | 574 |
/// |
| 575 | 575 |
///This method runs the %BFS algorithm from the root node(s) |
| 576 | 576 |
///in order to compute the shortest path to each node. |
| 577 | 577 |
/// |
| 578 | 578 |
///The algorithm computes |
| 579 | 579 |
///- the shortest path tree (forest), |
| 580 | 580 |
///- the distance of each node from the root(s). |
| 581 | 581 |
/// |
| 582 | 582 |
///\pre init() must be called and at least one root node should be |
| 583 | 583 |
///added with addSource() before using this function. |
| 584 | 584 |
/// |
| 585 | 585 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
| 586 | 586 |
///\code |
| 587 | 587 |
/// while ( !b.emptyQueue() ) {
|
| 588 | 588 |
/// b.processNextNode(); |
| 589 | 589 |
/// } |
| 590 | 590 |
///\endcode |
| 591 | 591 |
void start() |
| 592 | 592 |
{
|
| 593 | 593 |
while ( !emptyQueue() ) processNextNode(); |
| 594 | 594 |
} |
| 595 | 595 |
|
| 596 | 596 |
///Executes the algorithm until the given target node is reached. |
| 597 | 597 |
|
| 598 | 598 |
///Executes the algorithm until the given target node is reached. |
| 599 | 599 |
/// |
| 600 | 600 |
///This method runs the %BFS algorithm from the root node(s) |
| 601 | 601 |
///in order to compute the shortest path to \c t. |
| 602 | 602 |
/// |
| 603 | 603 |
///The algorithm computes |
| 604 | 604 |
///- the shortest path to \c t, |
| 605 | 605 |
///- the distance of \c t from the root(s). |
| 606 | 606 |
/// |
| 607 | 607 |
///\pre init() must be called and at least one root node should be |
| 608 | 608 |
///added with addSource() before using this function. |
| 609 | 609 |
/// |
| 610 | 610 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
| 611 | 611 |
///\code |
| 612 | 612 |
/// bool reach = false; |
| 613 | 613 |
/// while ( !b.emptyQueue() && !reach ) {
|
| 614 | 614 |
/// b.processNextNode(t, reach); |
| 615 | 615 |
/// } |
| 616 | 616 |
///\endcode |
| 617 | 617 |
void start(Node t) |
| 618 | 618 |
{
|
| 619 | 619 |
bool reach = false; |
| 620 | 620 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
| 621 | 621 |
} |
| 622 | 622 |
|
| 623 | 623 |
///Executes the algorithm until a condition is met. |
| 624 | 624 |
|
| 625 | 625 |
///Executes the algorithm until a condition is met. |
| 626 | 626 |
/// |
| 627 | 627 |
///This method runs the %BFS algorithm from the root node(s) in |
| 628 | 628 |
///order to compute the shortest path to a node \c v with |
| 629 | 629 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
| 630 | 630 |
/// |
| 631 | 631 |
///\param nm A \c bool (or convertible) node map. The algorithm |
| 632 | 632 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
| 633 | 633 |
/// |
| 634 | 634 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
| 635 | 635 |
///\c INVALID if no such node was found. |
| 636 | 636 |
/// |
| 637 | 637 |
///\pre init() must be called and at least one root node should be |
| 638 | 638 |
///added with addSource() before using this function. |
| 639 | 639 |
/// |
| 640 | 640 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
| 641 | 641 |
///\code |
| 642 | 642 |
/// Node rnode = INVALID; |
| 643 | 643 |
/// while ( !b.emptyQueue() && rnode == INVALID ) {
|
| 644 | 644 |
/// b.processNextNode(nm, rnode); |
| 645 | 645 |
/// } |
| 646 | 646 |
/// return rnode; |
| 647 | 647 |
///\endcode |
| 648 | 648 |
template<class NodeBoolMap> |
| 649 | 649 |
Node start(const NodeBoolMap &nm) |
| 650 | 650 |
{
|
| 651 | 651 |
Node rnode = INVALID; |
| 652 | 652 |
while ( !emptyQueue() && rnode == INVALID ) {
|
| 653 | 653 |
processNextNode(nm, rnode); |
| 654 | 654 |
} |
| 655 | 655 |
return rnode; |
| 656 | 656 |
} |
| 657 | 657 |
|
| 658 | 658 |
///Runs the algorithm from the given source node. |
| 659 | 659 |
|
| 660 | 660 |
///This method runs the %BFS algorithm from node \c s |
| 661 | 661 |
///in order to compute the shortest path to each node. |
| 662 | 662 |
/// |
| 663 | 663 |
///The algorithm computes |
| 664 | 664 |
///- the shortest path tree, |
| 665 | 665 |
///- the distance of each node from the root. |
| 666 | 666 |
/// |
| 667 | 667 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
| 668 | 668 |
///\code |
| 669 | 669 |
/// b.init(); |
| 670 | 670 |
/// b.addSource(s); |
| 671 | 671 |
/// b.start(); |
| 672 | 672 |
///\endcode |
| 673 | 673 |
void run(Node s) {
|
| 674 | 674 |
init(); |
| 675 | 675 |
addSource(s); |
| 676 | 676 |
start(); |
| 677 | 677 |
} |
| 678 | 678 |
|
| 679 | 679 |
///Finds the shortest path between \c s and \c t. |
| 680 | 680 |
|
| 681 | 681 |
///This method runs the %BFS algorithm from node \c s |
| 682 | 682 |
///in order to compute the shortest path to node \c t |
| 683 | 683 |
///(it stops searching when \c t is processed). |
| 684 | 684 |
/// |
| 685 | 685 |
///\return \c true if \c t is reachable form \c s. |
| 686 | 686 |
/// |
| 687 | 687 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
| 688 | 688 |
///shortcut of the following code. |
| 689 | 689 |
///\code |
| 690 | 690 |
/// b.init(); |
| 691 | 691 |
/// b.addSource(s); |
| 692 | 692 |
/// b.start(t); |
| 693 | 693 |
///\endcode |
| 694 | 694 |
bool run(Node s,Node t) {
|
| 695 | 695 |
init(); |
| 696 | 696 |
addSource(s); |
| 697 | 697 |
start(t); |
| 698 | 698 |
return reached(t); |
| 699 | 699 |
} |
| 700 | 700 |
|
| 701 | 701 |
///Runs the algorithm to visit all nodes in the digraph. |
| 702 | 702 |
|
| 703 | 703 |
///This method runs the %BFS algorithm in order to |
| 704 | 704 |
///compute the shortest path to each node. |
| 705 | 705 |
/// |
| 706 | 706 |
///The algorithm computes |
| 707 | 707 |
///- the shortest path tree (forest), |
| 708 | 708 |
///- the distance of each node from the root(s). |
| 709 | 709 |
/// |
| 710 | 710 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
| 711 | 711 |
///\code |
| 712 | 712 |
/// b.init(); |
| 713 | 713 |
/// for (NodeIt n(gr); n != INVALID; ++n) {
|
| 714 | 714 |
/// if (!b.reached(n)) {
|
| 715 | 715 |
/// b.addSource(n); |
| 716 | 716 |
/// b.start(); |
| 717 | 717 |
/// } |
| 718 | 718 |
/// } |
| 719 | 719 |
///\endcode |
| 720 | 720 |
void run() {
|
| 721 | 721 |
init(); |
| 722 | 722 |
for (NodeIt n(*G); n != INVALID; ++n) {
|
| 723 | 723 |
if (!reached(n)) {
|
| 724 | 724 |
addSource(n); |
| 725 | 725 |
start(); |
| 726 | 726 |
} |
| 727 | 727 |
} |
| 728 | 728 |
} |
| 729 | 729 |
|
| 730 | 730 |
///@} |
| 731 | 731 |
|
| 732 | 732 |
///\name Query Functions |
| 733 | 733 |
///The results of the BFS algorithm can be obtained using these |
| 734 | 734 |
///functions.\n |
| 735 | 735 |
///Either \ref run(Node) "run()" or \ref start() should be called |
| 736 | 736 |
///before using them. |
| 737 | 737 |
|
| 738 | 738 |
///@{
|
| 739 | 739 |
|
| 740 | 740 |
///The shortest path to a node. |
| 741 | 741 |
|
| 742 | 742 |
///Returns the shortest path to a node. |
| 743 | 743 |
/// |
| 744 | 744 |
///\warning \c t should be reached from the root(s). |
| 745 | 745 |
/// |
| 746 | 746 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 747 | 747 |
///must be called before using this function. |
| 748 | 748 |
Path path(Node t) const { return Path(*G, *_pred, t); }
|
| 749 | 749 |
|
| 750 | 750 |
///The distance of a node from the root(s). |
| 751 | 751 |
|
| 752 | 752 |
///Returns the distance of a node from the root(s). |
| 753 | 753 |
/// |
| 754 | 754 |
///\warning If node \c v is not reached from the root(s), then |
| 755 | 755 |
///the return value of this function is undefined. |
| 756 | 756 |
/// |
| 757 | 757 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 758 | 758 |
///must be called before using this function. |
| 759 | 759 |
int dist(Node v) const { return (*_dist)[v]; }
|
| 760 | 760 |
|
| 761 | 761 |
///Returns the 'previous arc' of the shortest path tree for a node. |
| 762 | 762 |
|
| 763 | 763 |
///This function returns the 'previous arc' of the shortest path |
| 764 | 764 |
///tree for the node \c v, i.e. it returns the last arc of a |
| 765 | 765 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
| 766 | 766 |
///is not reached from the root(s) or if \c v is a root. |
| 767 | 767 |
/// |
| 768 | 768 |
///The shortest path tree used here is equal to the shortest path |
| 769 | 769 |
///tree used in \ref predNode(). |
| 770 | 770 |
/// |
| 771 | 771 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 772 | 772 |
///must be called before using this function. |
| 773 | 773 |
Arc predArc(Node v) const { return (*_pred)[v];}
|
| 774 | 774 |
|
| 775 | 775 |
///Returns the 'previous node' of the shortest path tree for a node. |
| 776 | 776 |
|
| 777 | 777 |
///This function returns the 'previous node' of the shortest path |
| 778 | 778 |
///tree for the node \c v, i.e. it returns the last but one node |
| 779 | 779 |
///from a shortest path from a root to \c v. It is \c INVALID |
| 780 | 780 |
///if \c v is not reached from the root(s) or if \c v is a root. |
| 781 | 781 |
/// |
| 782 | 782 |
///The shortest path tree used here is equal to the shortest path |
| 783 | 783 |
///tree used in \ref predArc(). |
| 784 | 784 |
/// |
| 785 | 785 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 786 | 786 |
///must be called before using this function. |
| 787 | 787 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
|
| 788 | 788 |
G->source((*_pred)[v]); } |
| 789 | 789 |
|
| 790 | 790 |
///\brief Returns a const reference to the node map that stores the |
| 791 | 791 |
/// distances of the nodes. |
| 792 | 792 |
/// |
| 793 | 793 |
///Returns a const reference to the node map that stores the distances |
| 794 | 794 |
///of the nodes calculated by the algorithm. |
| 795 | 795 |
/// |
| 796 | 796 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 797 | 797 |
///must be called before using this function. |
| 798 | 798 |
const DistMap &distMap() const { return *_dist;}
|
| 799 | 799 |
|
| 800 | 800 |
///\brief Returns a const reference to the node map that stores the |
| 801 | 801 |
///predecessor arcs. |
| 802 | 802 |
/// |
| 803 | 803 |
///Returns a const reference to the node map that stores the predecessor |
| 804 | 804 |
///arcs, which form the shortest path tree. |
| 805 | 805 |
/// |
| 806 | 806 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 807 | 807 |
///must be called before using this function. |
| 808 | 808 |
const PredMap &predMap() const { return *_pred;}
|
| 809 | 809 |
|
| 810 | 810 |
///Checks if a node is reached from the root(s). |
| 811 | 811 |
|
| 812 | 812 |
///Returns \c true if \c v is reached from the root(s). |
| 813 | 813 |
/// |
| 814 | 814 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 815 | 815 |
///must be called before using this function. |
| 816 | 816 |
bool reached(Node v) const { return (*_reached)[v]; }
|
| 817 | 817 |
|
| 818 | 818 |
///@} |
| 819 | 819 |
}; |
| 820 | 820 |
|
| 821 | 821 |
///Default traits class of bfs() function. |
| 822 | 822 |
|
| 823 | 823 |
///Default traits class of bfs() function. |
| 824 | 824 |
///\tparam GR Digraph type. |
| 825 | 825 |
template<class GR> |
| 826 | 826 |
struct BfsWizardDefaultTraits |
| 827 | 827 |
{
|
| 828 | 828 |
///The type of the digraph the algorithm runs on. |
| 829 | 829 |
typedef GR Digraph; |
| 830 | 830 |
|
| 831 | 831 |
///\brief The type of the map that stores the predecessor |
| 832 | 832 |
///arcs of the shortest paths. |
| 833 | 833 |
/// |
| 834 | 834 |
///The type of the map that stores the predecessor |
| 835 | 835 |
///arcs of the shortest paths. |
| 836 | 836 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 837 | 837 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 838 | 838 |
///Instantiates a PredMap. |
| 839 | 839 |
|
| 840 | 840 |
///This function instantiates a PredMap. |
| 841 | 841 |
///\param g is the digraph, to which we would like to define the |
| 842 | 842 |
///PredMap. |
| 843 | 843 |
static PredMap *createPredMap(const Digraph &g) |
| 844 | 844 |
{
|
| 845 | 845 |
return new PredMap(g); |
| 846 | 846 |
} |
| 847 | 847 |
|
| 848 | 848 |
///The type of the map that indicates which nodes are processed. |
| 849 | 849 |
|
| 850 | 850 |
///The type of the map that indicates which nodes are processed. |
| 851 | 851 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 852 | 852 |
///By default it is a NullMap. |
| 853 | 853 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 854 | 854 |
///Instantiates a ProcessedMap. |
| 855 | 855 |
|
| 856 | 856 |
///This function instantiates a ProcessedMap. |
| 857 | 857 |
///\param g is the digraph, to which |
| 858 | 858 |
///we would like to define the ProcessedMap. |
| 859 | 859 |
#ifdef DOXYGEN |
| 860 | 860 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 861 | 861 |
#else |
| 862 | 862 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 863 | 863 |
#endif |
| 864 | 864 |
{
|
| 865 | 865 |
return new ProcessedMap(); |
| 866 | 866 |
} |
| 867 | 867 |
|
| 868 | 868 |
///The type of the map that indicates which nodes are reached. |
| 869 | 869 |
|
| 870 | 870 |
///The type of the map that indicates which nodes are reached. |
| 871 | 871 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 872 | 872 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 873 | 873 |
///Instantiates a ReachedMap. |
| 874 | 874 |
|
| 875 | 875 |
///This function instantiates a ReachedMap. |
| 876 | 876 |
///\param g is the digraph, to which |
| 877 | 877 |
///we would like to define the ReachedMap. |
| 878 | 878 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 879 | 879 |
{
|
| 880 | 880 |
return new ReachedMap(g); |
| 881 | 881 |
} |
| 882 | 882 |
|
| 883 | 883 |
///The type of the map that stores the distances of the nodes. |
| 884 | 884 |
|
| 885 | 885 |
///The type of the map that stores the distances of the nodes. |
| 886 | 886 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 887 | 887 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 888 | 888 |
///Instantiates a DistMap. |
| 889 | 889 |
|
| 890 | 890 |
///This function instantiates a DistMap. |
| 891 | 891 |
///\param g is the digraph, to which we would like to define |
| 892 | 892 |
///the DistMap |
| 893 | 893 |
static DistMap *createDistMap(const Digraph &g) |
| 894 | 894 |
{
|
| 895 | 895 |
return new DistMap(g); |
| 896 | 896 |
} |
| 897 | 897 |
|
| 898 | 898 |
///The type of the shortest paths. |
| 899 | 899 |
|
| 900 | 900 |
///The type of the shortest paths. |
| 901 | 901 |
///It must meet the \ref concepts::Path "Path" concept. |
| 902 | 902 |
typedef lemon::Path<Digraph> Path; |
| 903 | 903 |
}; |
| 904 | 904 |
|
| 905 | 905 |
/// Default traits class used by BfsWizard |
| 906 | 906 |
|
| 907 | 907 |
/// To make it easier to use Bfs algorithm |
| 908 | 908 |
/// we have created a wizard class. |
| 909 | 909 |
/// This \ref BfsWizard class needs default traits, |
| 910 | 910 |
/// as well as the \ref Bfs class. |
| 911 | 911 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
| 912 | 912 |
/// \ref BfsWizard class. |
| 913 | 913 |
template<class GR> |
| 914 | 914 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
| 915 | 915 |
{
|
| 916 | 916 |
|
| 917 | 917 |
typedef BfsWizardDefaultTraits<GR> Base; |
| 918 | 918 |
protected: |
| 919 | 919 |
//The type of the nodes in the digraph. |
| 920 | 920 |
typedef typename Base::Digraph::Node Node; |
| 921 | 921 |
|
| 922 | 922 |
//Pointer to the digraph the algorithm runs on. |
| 923 | 923 |
void *_g; |
| 924 | 924 |
//Pointer to the map of reached nodes. |
| 925 | 925 |
void *_reached; |
| 926 | 926 |
//Pointer to the map of processed nodes. |
| 927 | 927 |
void *_processed; |
| 928 | 928 |
//Pointer to the map of predecessors arcs. |
| 929 | 929 |
void *_pred; |
| 930 | 930 |
//Pointer to the map of distances. |
| 931 | 931 |
void *_dist; |
| 932 | 932 |
//Pointer to the shortest path to the target node. |
| 933 | 933 |
void *_path; |
| 934 | 934 |
//Pointer to the distance of the target node. |
| 935 | 935 |
int *_di; |
| 936 | 936 |
|
| 937 | 937 |
public: |
| 938 | 938 |
/// Constructor. |
| 939 | 939 |
|
| 940 | 940 |
/// This constructor does not require parameters, therefore it initiates |
| 941 | 941 |
/// all of the attributes to \c 0. |
| 942 | 942 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
| 943 | 943 |
_dist(0), _path(0), _di(0) {}
|
| 944 | 944 |
|
| 945 | 945 |
/// Constructor. |
| 946 | 946 |
|
| 947 | 947 |
/// This constructor requires one parameter, |
| 948 | 948 |
/// others are initiated to \c 0. |
| 949 | 949 |
/// \param g The digraph the algorithm runs on. |
| 950 | 950 |
BfsWizardBase(const GR &g) : |
| 951 | 951 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
| 952 | 952 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
| 953 | 953 |
|
| 954 | 954 |
}; |
| 955 | 955 |
|
| 956 | 956 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
| 957 | 957 |
|
| 958 | 958 |
/// This auxiliary class is created to implement the |
| 959 | 959 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
| 960 | 960 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
| 961 | 961 |
/// functions and features of the plain \ref Bfs. |
| 962 | 962 |
/// |
| 963 | 963 |
/// This class should only be used through the \ref bfs() function, |
| 964 | 964 |
/// which makes it easier to use the algorithm. |
| 965 | 965 |
template<class TR> |
| 966 | 966 |
class BfsWizard : public TR |
| 967 | 967 |
{
|
| 968 | 968 |
typedef TR Base; |
| 969 | 969 |
|
| 970 | 970 |
///The type of the digraph the algorithm runs on. |
| 971 | 971 |
typedef typename TR::Digraph Digraph; |
| 972 | 972 |
|
| 973 | 973 |
typedef typename Digraph::Node Node; |
| 974 | 974 |
typedef typename Digraph::NodeIt NodeIt; |
| 975 | 975 |
typedef typename Digraph::Arc Arc; |
| 976 | 976 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 977 | 977 |
|
| 978 | 978 |
///\brief The type of the map that stores the predecessor |
| 979 | 979 |
///arcs of the shortest paths. |
| 980 | 980 |
typedef typename TR::PredMap PredMap; |
| 981 | 981 |
///\brief The type of the map that stores the distances of the nodes. |
| 982 | 982 |
typedef typename TR::DistMap DistMap; |
| 983 | 983 |
///\brief The type of the map that indicates which nodes are reached. |
| 984 | 984 |
typedef typename TR::ReachedMap ReachedMap; |
| 985 | 985 |
///\brief The type of the map that indicates which nodes are processed. |
| 986 | 986 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 987 | 987 |
///The type of the shortest paths |
| 988 | 988 |
typedef typename TR::Path Path; |
| 989 | 989 |
|
| 990 | 990 |
public: |
| 991 | 991 |
|
| 992 | 992 |
/// Constructor. |
| 993 | 993 |
BfsWizard() : TR() {}
|
| 994 | 994 |
|
| 995 | 995 |
/// Constructor that requires parameters. |
| 996 | 996 |
|
| 997 | 997 |
/// Constructor that requires parameters. |
| 998 | 998 |
/// These parameters will be the default values for the traits class. |
| 999 | 999 |
/// \param g The digraph the algorithm runs on. |
| 1000 | 1000 |
BfsWizard(const Digraph &g) : |
| 1001 | 1001 |
TR(g) {}
|
| 1002 | 1002 |
|
| 1003 | 1003 |
///Copy constructor |
| 1004 | 1004 |
BfsWizard(const TR &b) : TR(b) {}
|
| 1005 | 1005 |
|
| 1006 | 1006 |
~BfsWizard() {}
|
| 1007 | 1007 |
|
| 1008 | 1008 |
///Runs BFS algorithm from the given source node. |
| 1009 | 1009 |
|
| 1010 | 1010 |
///This method runs BFS algorithm from node \c s |
| 1011 | 1011 |
///in order to compute the shortest path to each node. |
| 1012 | 1012 |
void run(Node s) |
| 1013 | 1013 |
{
|
| 1014 | 1014 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 1015 | 1015 |
if (Base::_pred) |
| 1016 | 1016 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1017 | 1017 |
if (Base::_dist) |
| 1018 | 1018 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1019 | 1019 |
if (Base::_reached) |
| 1020 | 1020 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 1021 | 1021 |
if (Base::_processed) |
| 1022 | 1022 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1023 | 1023 |
if (s!=INVALID) |
| 1024 | 1024 |
alg.run(s); |
| 1025 | 1025 |
else |
| 1026 | 1026 |
alg.run(); |
| 1027 | 1027 |
} |
| 1028 | 1028 |
|
| 1029 | 1029 |
///Finds the shortest path between \c s and \c t. |
| 1030 | 1030 |
|
| 1031 | 1031 |
///This method runs BFS algorithm from node \c s |
| 1032 | 1032 |
///in order to compute the shortest path to node \c t |
| 1033 | 1033 |
///(it stops searching when \c t is processed). |
| 1034 | 1034 |
/// |
| 1035 | 1035 |
///\return \c true if \c t is reachable form \c s. |
| 1036 | 1036 |
bool run(Node s, Node t) |
| 1037 | 1037 |
{
|
| 1038 | 1038 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 1039 | 1039 |
if (Base::_pred) |
| 1040 | 1040 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1041 | 1041 |
if (Base::_dist) |
| 1042 | 1042 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1043 | 1043 |
if (Base::_reached) |
| 1044 | 1044 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 1045 | 1045 |
if (Base::_processed) |
| 1046 | 1046 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1047 | 1047 |
alg.run(s,t); |
| 1048 | 1048 |
if (Base::_path) |
| 1049 | 1049 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
| 1050 | 1050 |
if (Base::_di) |
| 1051 | 1051 |
*Base::_di = alg.dist(t); |
| 1052 | 1052 |
return alg.reached(t); |
| 1053 | 1053 |
} |
| 1054 | 1054 |
|
| 1055 | 1055 |
///Runs BFS algorithm to visit all nodes in the digraph. |
| 1056 | 1056 |
|
| 1057 | 1057 |
///This method runs BFS algorithm in order to compute |
| 1058 | 1058 |
///the shortest path to each node. |
| 1059 | 1059 |
void run() |
| 1060 | 1060 |
{
|
| 1061 | 1061 |
run(INVALID); |
| 1062 | 1062 |
} |
| 1063 | 1063 |
|
| 1064 | 1064 |
template<class T> |
| 1065 | 1065 |
struct SetPredMapBase : public Base {
|
| 1066 | 1066 |
typedef T PredMap; |
| 1067 | 1067 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1068 | 1068 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1069 | 1069 |
}; |
| 1070 | 1070 |
///\brief \ref named-func-param "Named parameter" |
| 1071 | 1071 |
///for setting PredMap object. |
| 1072 | 1072 |
/// |
| 1073 | 1073 |
///\ref named-func-param "Named parameter" |
| 1074 | 1074 |
///for setting PredMap object. |
| 1075 | 1075 |
template<class T> |
| 1076 | 1076 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
| 1077 | 1077 |
{
|
| 1078 | 1078 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1079 | 1079 |
return BfsWizard<SetPredMapBase<T> >(*this); |
| 1080 | 1080 |
} |
| 1081 | 1081 |
|
| 1082 | 1082 |
template<class T> |
| 1083 | 1083 |
struct SetReachedMapBase : public Base {
|
| 1084 | 1084 |
typedef T ReachedMap; |
| 1085 | 1085 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; };
|
| 1086 | 1086 |
SetReachedMapBase(const TR &b) : TR(b) {}
|
| 1087 | 1087 |
}; |
| 1088 | 1088 |
///\brief \ref named-func-param "Named parameter" |
| 1089 | 1089 |
///for setting ReachedMap object. |
| 1090 | 1090 |
/// |
| 1091 | 1091 |
/// \ref named-func-param "Named parameter" |
| 1092 | 1092 |
///for setting ReachedMap object. |
| 1093 | 1093 |
template<class T> |
| 1094 | 1094 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
| 1095 | 1095 |
{
|
| 1096 | 1096 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1097 | 1097 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
| 1098 | 1098 |
} |
| 1099 | 1099 |
|
| 1100 | 1100 |
template<class T> |
| 1101 | 1101 |
struct SetDistMapBase : public Base {
|
| 1102 | 1102 |
typedef T DistMap; |
| 1103 | 1103 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1104 | 1104 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1105 | 1105 |
}; |
| 1106 | 1106 |
///\brief \ref named-func-param "Named parameter" |
| 1107 | 1107 |
///for setting DistMap object. |
| 1108 | 1108 |
/// |
| 1109 | 1109 |
/// \ref named-func-param "Named parameter" |
| 1110 | 1110 |
///for setting DistMap object. |
| 1111 | 1111 |
template<class T> |
| 1112 | 1112 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
| 1113 | 1113 |
{
|
| 1114 | 1114 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1115 | 1115 |
return BfsWizard<SetDistMapBase<T> >(*this); |
| 1116 | 1116 |
} |
| 1117 | 1117 |
|
| 1118 | 1118 |
template<class T> |
| 1119 | 1119 |
struct SetProcessedMapBase : public Base {
|
| 1120 | 1120 |
typedef T ProcessedMap; |
| 1121 | 1121 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
| 1122 | 1122 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
| 1123 | 1123 |
}; |
| 1124 | 1124 |
///\brief \ref named-func-param "Named parameter" |
| 1125 | 1125 |
///for setting ProcessedMap object. |
| 1126 | 1126 |
/// |
| 1127 | 1127 |
/// \ref named-func-param "Named parameter" |
| 1128 | 1128 |
///for setting ProcessedMap object. |
| 1129 | 1129 |
template<class T> |
| 1130 | 1130 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
| 1131 | 1131 |
{
|
| 1132 | 1132 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1133 | 1133 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
| 1134 | 1134 |
} |
| 1135 | 1135 |
|
| 1136 | 1136 |
template<class T> |
| 1137 | 1137 |
struct SetPathBase : public Base {
|
| 1138 | 1138 |
typedef T Path; |
| 1139 | 1139 |
SetPathBase(const TR &b) : TR(b) {}
|
| 1140 | 1140 |
}; |
| 1141 | 1141 |
///\brief \ref named-func-param "Named parameter" |
| 1142 | 1142 |
///for getting the shortest path to the target node. |
| 1143 | 1143 |
/// |
| 1144 | 1144 |
///\ref named-func-param "Named parameter" |
| 1145 | 1145 |
///for getting the shortest path to the target node. |
| 1146 | 1146 |
template<class T> |
| 1147 | 1147 |
BfsWizard<SetPathBase<T> > path(const T &t) |
| 1148 | 1148 |
{
|
| 1149 | 1149 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1150 | 1150 |
return BfsWizard<SetPathBase<T> >(*this); |
| 1151 | 1151 |
} |
| 1152 | 1152 |
|
| 1153 | 1153 |
///\brief \ref named-func-param "Named parameter" |
| 1154 | 1154 |
///for getting the distance of the target node. |
| 1155 | 1155 |
/// |
| 1156 | 1156 |
///\ref named-func-param "Named parameter" |
| 1157 | 1157 |
///for getting the distance of the target node. |
| 1158 | 1158 |
BfsWizard dist(const int &d) |
| 1159 | 1159 |
{
|
| 1160 | 1160 |
Base::_di=const_cast<int*>(&d); |
| 1161 | 1161 |
return *this; |
| 1162 | 1162 |
} |
| 1163 | 1163 |
|
| 1164 | 1164 |
}; |
| 1165 | 1165 |
|
| 1166 | 1166 |
///Function-type interface for BFS algorithm. |
| 1167 | 1167 |
|
| 1168 | 1168 |
/// \ingroup search |
| 1169 | 1169 |
///Function-type interface for BFS algorithm. |
| 1170 | 1170 |
/// |
| 1171 | 1171 |
///This function also has several \ref named-func-param "named parameters", |
| 1172 | 1172 |
///they are declared as the members of class \ref BfsWizard. |
| 1173 | 1173 |
///The following examples show how to use these parameters. |
| 1174 | 1174 |
///\code |
| 1175 | 1175 |
/// // Compute shortest path from node s to each node |
| 1176 | 1176 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
| 1177 | 1177 |
/// |
| 1178 | 1178 |
/// // Compute shortest path from s to t |
| 1179 | 1179 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t); |
| 1180 | 1180 |
///\endcode |
| 1181 | 1181 |
///\warning Don't forget to put the \ref BfsWizard::run(Node) "run()" |
| 1182 | 1182 |
///to the end of the parameter list. |
| 1183 | 1183 |
///\sa BfsWizard |
| 1184 | 1184 |
///\sa Bfs |
| 1185 | 1185 |
template<class GR> |
| 1186 | 1186 |
BfsWizard<BfsWizardBase<GR> > |
| 1187 | 1187 |
bfs(const GR &digraph) |
| 1188 | 1188 |
{
|
| 1189 | 1189 |
return BfsWizard<BfsWizardBase<GR> >(digraph); |
| 1190 | 1190 |
} |
| 1191 | 1191 |
|
| 1192 | 1192 |
#ifdef DOXYGEN |
| 1193 | 1193 |
/// \brief Visitor class for BFS. |
| 1194 | 1194 |
/// |
| 1195 | 1195 |
/// This class defines the interface of the BfsVisit events, and |
| 1196 | 1196 |
/// it could be the base of a real visitor class. |
| 1197 | 1197 |
template <typename _Digraph> |
| 1198 | 1198 |
struct BfsVisitor {
|
| 1199 | 1199 |
typedef _Digraph Digraph; |
| 1200 | 1200 |
typedef typename Digraph::Arc Arc; |
| 1201 | 1201 |
typedef typename Digraph::Node Node; |
| 1202 | 1202 |
/// \brief Called for the source node(s) of the BFS. |
| 1203 | 1203 |
/// |
| 1204 | 1204 |
/// This function is called for the source node(s) of the BFS. |
| 1205 | 1205 |
void start(const Node& node) {}
|
| 1206 | 1206 |
/// \brief Called when a node is reached first time. |
| 1207 | 1207 |
/// |
| 1208 | 1208 |
/// This function is called when a node is reached first time. |
| 1209 | 1209 |
void reach(const Node& node) {}
|
| 1210 | 1210 |
/// \brief Called when a node is processed. |
| 1211 | 1211 |
/// |
| 1212 | 1212 |
/// This function is called when a node is processed. |
| 1213 | 1213 |
void process(const Node& node) {}
|
| 1214 | 1214 |
/// \brief Called when an arc reaches a new node. |
| 1215 | 1215 |
/// |
| 1216 | 1216 |
/// This function is called when the BFS finds an arc whose target node |
| 1217 | 1217 |
/// is not reached yet. |
| 1218 | 1218 |
void discover(const Arc& arc) {}
|
| 1219 | 1219 |
/// \brief Called when an arc is examined but its target node is |
| 1220 | 1220 |
/// already discovered. |
| 1221 | 1221 |
/// |
| 1222 | 1222 |
/// This function is called when an arc is examined but its target node is |
| 1223 | 1223 |
/// already discovered. |
| 1224 | 1224 |
void examine(const Arc& arc) {}
|
| 1225 | 1225 |
}; |
| 1226 | 1226 |
#else |
| 1227 | 1227 |
template <typename _Digraph> |
| 1228 | 1228 |
struct BfsVisitor {
|
| 1229 | 1229 |
typedef _Digraph Digraph; |
| 1230 | 1230 |
typedef typename Digraph::Arc Arc; |
| 1231 | 1231 |
typedef typename Digraph::Node Node; |
| 1232 | 1232 |
void start(const Node&) {}
|
| 1233 | 1233 |
void reach(const Node&) {}
|
| 1234 | 1234 |
void process(const Node&) {}
|
| 1235 | 1235 |
void discover(const Arc&) {}
|
| 1236 | 1236 |
void examine(const Arc&) {}
|
| 1237 | 1237 |
|
| 1238 | 1238 |
template <typename _Visitor> |
| 1239 | 1239 |
struct Constraints {
|
| 1240 | 1240 |
void constraints() {
|
| 1241 | 1241 |
Arc arc; |
| 1242 | 1242 |
Node node; |
| 1243 | 1243 |
visitor.start(node); |
| 1244 | 1244 |
visitor.reach(node); |
| 1245 | 1245 |
visitor.process(node); |
| 1246 | 1246 |
visitor.discover(arc); |
| 1247 | 1247 |
visitor.examine(arc); |
| 1248 | 1248 |
} |
| 1249 | 1249 |
_Visitor& visitor; |
| 1250 | 1250 |
}; |
| 1251 | 1251 |
}; |
| 1252 | 1252 |
#endif |
| 1253 | 1253 |
|
| 1254 | 1254 |
/// \brief Default traits class of BfsVisit class. |
| 1255 | 1255 |
/// |
| 1256 | 1256 |
/// Default traits class of BfsVisit class. |
| 1257 | 1257 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1258 | 1258 |
template<class _Digraph> |
| 1259 | 1259 |
struct BfsVisitDefaultTraits {
|
| 1260 | 1260 |
|
| 1261 | 1261 |
/// \brief The type of the digraph the algorithm runs on. |
| 1262 | 1262 |
typedef _Digraph Digraph; |
| 1263 | 1263 |
|
| 1264 | 1264 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1265 | 1265 |
/// |
| 1266 | 1266 |
/// The type of the map that indicates which nodes are reached. |
| 1267 | 1267 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 1268 | 1268 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1269 | 1269 |
|
| 1270 | 1270 |
/// \brief Instantiates a ReachedMap. |
| 1271 | 1271 |
/// |
| 1272 | 1272 |
/// This function instantiates a ReachedMap. |
| 1273 | 1273 |
/// \param digraph is the digraph, to which |
| 1274 | 1274 |
/// we would like to define the ReachedMap. |
| 1275 | 1275 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1276 | 1276 |
return new ReachedMap(digraph); |
| 1277 | 1277 |
} |
| 1278 | 1278 |
|
| 1279 | 1279 |
}; |
| 1280 | 1280 |
|
| 1281 | 1281 |
/// \ingroup search |
| 1282 | 1282 |
/// |
| 1283 | 1283 |
/// \brief %BFS algorithm class with visitor interface. |
| 1284 | 1284 |
/// |
| 1285 | 1285 |
/// This class provides an efficient implementation of the %BFS algorithm |
| 1286 | 1286 |
/// with visitor interface. |
| 1287 | 1287 |
/// |
| 1288 | 1288 |
/// The %BfsVisit class provides an alternative interface to the Bfs |
| 1289 | 1289 |
/// class. It works with callback mechanism, the BfsVisit object calls |
| 1290 | 1290 |
/// the member functions of the \c Visitor class on every BFS event. |
| 1291 | 1291 |
/// |
| 1292 | 1292 |
/// This interface of the BFS algorithm should be used in special cases |
| 1293 | 1293 |
/// when extra actions have to be performed in connection with certain |
| 1294 | 1294 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
| 1295 | 1295 |
/// instead. |
| 1296 | 1296 |
/// |
| 1297 | 1297 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1298 | 1298 |
/// The default value is |
| 1299 | 1299 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
| 1300 | 1300 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. |
| 1301 | 1301 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
| 1302 | 1302 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which |
| 1303 | 1303 |
/// does not observe the BFS events. If you want to observe the BFS |
| 1304 | 1304 |
/// events, you should implement your own visitor class. |
| 1305 | 1305 |
/// \tparam _Traits Traits class to set various data types used by the |
| 1306 | 1306 |
/// algorithm. The default traits class is |
| 1307 | 1307 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". |
| 1308 | 1308 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
| 1309 | 1309 |
/// a BFS visit traits class. |
| 1310 | 1310 |
#ifdef DOXYGEN |
| 1311 | 1311 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
| 1312 | 1312 |
#else |
| 1313 | 1313 |
template <typename _Digraph = ListDigraph, |
| 1314 | 1314 |
typename _Visitor = BfsVisitor<_Digraph>, |
| 1315 | 1315 |
typename _Traits = BfsVisitDefaultTraits<_Digraph> > |
| 1316 | 1316 |
#endif |
| 1317 | 1317 |
class BfsVisit {
|
| 1318 | 1318 |
public: |
| 1319 | 1319 |
|
| 1320 | 1320 |
///The traits class. |
| 1321 | 1321 |
typedef _Traits Traits; |
| 1322 | 1322 |
|
| 1323 | 1323 |
///The type of the digraph the algorithm runs on. |
| 1324 | 1324 |
typedef typename Traits::Digraph Digraph; |
| 1325 | 1325 |
|
| 1326 | 1326 |
///The visitor type used by the algorithm. |
| 1327 | 1327 |
typedef _Visitor Visitor; |
| 1328 | 1328 |
|
| 1329 | 1329 |
///The type of the map that indicates which nodes are reached. |
| 1330 | 1330 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1331 | 1331 |
|
| 1332 | 1332 |
private: |
| 1333 | 1333 |
|
| 1334 | 1334 |
typedef typename Digraph::Node Node; |
| 1335 | 1335 |
typedef typename Digraph::NodeIt NodeIt; |
| 1336 | 1336 |
typedef typename Digraph::Arc Arc; |
| 1337 | 1337 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1338 | 1338 |
|
| 1339 | 1339 |
//Pointer to the underlying digraph. |
| 1340 | 1340 |
const Digraph *_digraph; |
| 1341 | 1341 |
//Pointer to the visitor object. |
| 1342 | 1342 |
Visitor *_visitor; |
| 1343 | 1343 |
//Pointer to the map of reached status of the nodes. |
| 1344 | 1344 |
ReachedMap *_reached; |
| 1345 | 1345 |
//Indicates if _reached is locally allocated (true) or not. |
| 1346 | 1346 |
bool local_reached; |
| 1347 | 1347 |
|
| 1348 | 1348 |
std::vector<typename Digraph::Node> _list; |
| 1349 | 1349 |
int _list_front, _list_back; |
| 1350 | 1350 |
|
| 1351 | 1351 |
//Creates the maps if necessary. |
| 1352 | 1352 |
void create_maps() {
|
| 1353 | 1353 |
if(!_reached) {
|
| 1354 | 1354 |
local_reached = true; |
| 1355 | 1355 |
_reached = Traits::createReachedMap(*_digraph); |
| 1356 | 1356 |
} |
| 1357 | 1357 |
} |
| 1358 | 1358 |
|
| 1359 | 1359 |
protected: |
| 1360 | 1360 |
|
| 1361 | 1361 |
BfsVisit() {}
|
| 1362 | 1362 |
|
| 1363 | 1363 |
public: |
| 1364 | 1364 |
|
| 1365 | 1365 |
typedef BfsVisit Create; |
| 1366 | 1366 |
|
| 1367 | 1367 |
/// \name Named Template Parameters |
| 1368 | 1368 |
|
| 1369 | 1369 |
///@{
|
| 1370 | 1370 |
template <class T> |
| 1371 | 1371 |
struct SetReachedMapTraits : public Traits {
|
| 1372 | 1372 |
typedef T ReachedMap; |
| 1373 | 1373 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1374 | 1374 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1375 | 1375 |
return 0; // ignore warnings |
| 1376 | 1376 |
} |
| 1377 | 1377 |
}; |
| 1378 | 1378 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1379 | 1379 |
/// ReachedMap type. |
| 1380 | 1380 |
/// |
| 1381 | 1381 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1382 | 1382 |
template <class T> |
| 1383 | 1383 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
| 1384 | 1384 |
SetReachedMapTraits<T> > {
|
| 1385 | 1385 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1386 | 1386 |
}; |
| 1387 | 1387 |
///@} |
| 1388 | 1388 |
|
| 1389 | 1389 |
public: |
| 1390 | 1390 |
|
| 1391 | 1391 |
/// \brief Constructor. |
| 1392 | 1392 |
/// |
| 1393 | 1393 |
/// Constructor. |
| 1394 | 1394 |
/// |
| 1395 | 1395 |
/// \param digraph The digraph the algorithm runs on. |
| 1396 | 1396 |
/// \param visitor The visitor object of the algorithm. |
| 1397 | 1397 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1398 | 1398 |
: _digraph(&digraph), _visitor(&visitor), |
| 1399 | 1399 |
_reached(0), local_reached(false) {}
|
| 1400 | 1400 |
|
| 1401 | 1401 |
/// \brief Destructor. |
| 1402 | 1402 |
~BfsVisit() {
|
| 1403 | 1403 |
if(local_reached) delete _reached; |
| 1404 | 1404 |
} |
| 1405 | 1405 |
|
| 1406 | 1406 |
/// \brief Sets the map that indicates which nodes are reached. |
| 1407 | 1407 |
/// |
| 1408 | 1408 |
/// Sets the map that indicates which nodes are reached. |
| 1409 | 1409 |
/// If you don't use this function before calling \ref run(Node) "run()" |
| 1410 | 1410 |
/// or \ref init(), an instance will be allocated automatically. |
| 1411 | 1411 |
/// The destructor deallocates this automatically allocated map, |
| 1412 | 1412 |
/// of course. |
| 1413 | 1413 |
/// \return <tt> (*this) </tt> |
| 1414 | 1414 |
BfsVisit &reachedMap(ReachedMap &m) {
|
| 1415 | 1415 |
if(local_reached) {
|
| 1416 | 1416 |
delete _reached; |
| 1417 | 1417 |
local_reached = false; |
| 1418 | 1418 |
} |
| 1419 | 1419 |
_reached = &m; |
| 1420 | 1420 |
return *this; |
| 1421 | 1421 |
} |
| 1422 | 1422 |
|
| 1423 | 1423 |
public: |
| 1424 | 1424 |
|
| 1425 | 1425 |
/// \name Execution Control |
| 1426 | 1426 |
/// The simplest way to execute the BFS algorithm is to use one of the |
| 1427 | 1427 |
/// member functions called \ref run(Node) "run()".\n |
| 1428 | 1428 |
/// If you need more control on the execution, first you have to call |
| 1429 | 1429 |
/// \ref init(), then you can add several source nodes with |
| 1430 | 1430 |
/// \ref addSource(). Finally the actual path computation can be |
| 1431 | 1431 |
/// performed with one of the \ref start() functions. |
| 1432 | 1432 |
|
| 1433 | 1433 |
/// @{
|
| 1434 | 1434 |
|
| 1435 | 1435 |
/// \brief Initializes the internal data structures. |
| 1436 | 1436 |
/// |
| 1437 | 1437 |
/// Initializes the internal data structures. |
| 1438 | 1438 |
void init() {
|
| 1439 | 1439 |
create_maps(); |
| 1440 | 1440 |
_list.resize(countNodes(*_digraph)); |
| 1441 | 1441 |
_list_front = _list_back = -1; |
| 1442 | 1442 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
|
| 1443 | 1443 |
_reached->set(u, false); |
| 1444 | 1444 |
} |
| 1445 | 1445 |
} |
| 1446 | 1446 |
|
| 1447 | 1447 |
/// \brief Adds a new source node. |
| 1448 | 1448 |
/// |
| 1449 | 1449 |
/// Adds a new source node to the set of nodes to be processed. |
| 1450 | 1450 |
void addSource(Node s) {
|
| 1451 | 1451 |
if(!(*_reached)[s]) {
|
| 1452 | 1452 |
_reached->set(s,true); |
| 1453 | 1453 |
_visitor->start(s); |
| 1454 | 1454 |
_visitor->reach(s); |
| 1455 | 1455 |
_list[++_list_back] = s; |
| 1456 | 1456 |
} |
| 1457 | 1457 |
} |
| 1458 | 1458 |
|
| 1459 | 1459 |
/// \brief Processes the next node. |
| 1460 | 1460 |
/// |
| 1461 | 1461 |
/// Processes the next node. |
| 1462 | 1462 |
/// |
| 1463 | 1463 |
/// \return The processed node. |
| 1464 | 1464 |
/// |
| 1465 | 1465 |
/// \pre The queue must not be empty. |
| 1466 | 1466 |
Node processNextNode() {
|
| 1467 | 1467 |
Node n = _list[++_list_front]; |
| 1468 | 1468 |
_visitor->process(n); |
| 1469 | 1469 |
Arc e; |
| 1470 | 1470 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
| 1471 | 1471 |
Node m = _digraph->target(e); |
| 1472 | 1472 |
if (!(*_reached)[m]) {
|
| 1473 | 1473 |
_visitor->discover(e); |
| 1474 | 1474 |
_visitor->reach(m); |
| 1475 | 1475 |
_reached->set(m, true); |
| 1476 | 1476 |
_list[++_list_back] = m; |
| 1477 | 1477 |
} else {
|
| 1478 | 1478 |
_visitor->examine(e); |
| 1479 | 1479 |
} |
| 1480 | 1480 |
} |
| 1481 | 1481 |
return n; |
| 1482 | 1482 |
} |
| 1483 | 1483 |
|
| 1484 | 1484 |
/// \brief Processes the next node. |
| 1485 | 1485 |
/// |
| 1486 | 1486 |
/// Processes the next node and checks if the given target node |
| 1487 | 1487 |
/// is reached. If the target node is reachable from the processed |
| 1488 | 1488 |
/// node, then the \c reach parameter will be set to \c true. |
| 1489 | 1489 |
/// |
| 1490 | 1490 |
/// \param target The target node. |
| 1491 | 1491 |
/// \retval reach Indicates if the target node is reached. |
| 1492 | 1492 |
/// It should be initially \c false. |
| 1493 | 1493 |
/// |
| 1494 | 1494 |
/// \return The processed node. |
| 1495 | 1495 |
/// |
| 1496 | 1496 |
/// \pre The queue must not be empty. |
| 1497 | 1497 |
Node processNextNode(Node target, bool& reach) {
|
| 1498 | 1498 |
Node n = _list[++_list_front]; |
| 1499 | 1499 |
_visitor->process(n); |
| 1500 | 1500 |
Arc e; |
| 1501 | 1501 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
| 1502 | 1502 |
Node m = _digraph->target(e); |
| 1503 | 1503 |
if (!(*_reached)[m]) {
|
| 1504 | 1504 |
_visitor->discover(e); |
| 1505 | 1505 |
_visitor->reach(m); |
| 1506 | 1506 |
_reached->set(m, true); |
| 1507 | 1507 |
_list[++_list_back] = m; |
| 1508 | 1508 |
reach = reach || (target == m); |
| 1509 | 1509 |
} else {
|
| 1510 | 1510 |
_visitor->examine(e); |
| 1511 | 1511 |
} |
| 1512 | 1512 |
} |
| 1513 | 1513 |
return n; |
| 1514 | 1514 |
} |
| 1515 | 1515 |
|
| 1516 | 1516 |
/// \brief Processes the next node. |
| 1517 | 1517 |
/// |
| 1518 | 1518 |
/// Processes the next node and checks if at least one of reached |
| 1519 | 1519 |
/// nodes has \c true value in the \c nm node map. If one node |
| 1520 | 1520 |
/// with \c true value is reachable from the processed node, then the |
| 1521 | 1521 |
/// \c rnode parameter will be set to the first of such nodes. |
| 1522 | 1522 |
/// |
| 1523 | 1523 |
/// \param nm A \c bool (or convertible) node map that indicates the |
| 1524 | 1524 |
/// possible targets. |
| 1525 | 1525 |
/// \retval rnode The reached target node. |
| 1526 | 1526 |
/// It should be initially \c INVALID. |
| 1527 | 1527 |
/// |
| 1528 | 1528 |
/// \return The processed node. |
| 1529 | 1529 |
/// |
| 1530 | 1530 |
/// \pre The queue must not be empty. |
| 1531 | 1531 |
template <typename NM> |
| 1532 | 1532 |
Node processNextNode(const NM& nm, Node& rnode) {
|
| 1533 | 1533 |
Node n = _list[++_list_front]; |
| 1534 | 1534 |
_visitor->process(n); |
| 1535 | 1535 |
Arc e; |
| 1536 | 1536 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
| 1537 | 1537 |
Node m = _digraph->target(e); |
| 1538 | 1538 |
if (!(*_reached)[m]) {
|
| 1539 | 1539 |
_visitor->discover(e); |
| 1540 | 1540 |
_visitor->reach(m); |
| 1541 | 1541 |
_reached->set(m, true); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BIN_HEAP_H |
| 20 | 20 |
#define LEMON_BIN_HEAP_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup auxdat |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Binary Heap implementation. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <utility> |
| 28 | 28 |
#include <functional> |
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
|
| 32 | 32 |
///\ingroup auxdat |
| 33 | 33 |
/// |
| 34 | 34 |
///\brief A Binary Heap implementation. |
| 35 | 35 |
/// |
| 36 | 36 |
///This class implements the \e binary \e heap data structure. A \e heap |
| 37 | 37 |
///is a data structure for storing items with specified values called \e |
| 38 | 38 |
///priorities in such a way that finding the item with minimum priority is |
| 39 | 39 |
///efficient. \c Compare specifies the ordering of the priorities. In a heap |
| 40 | 40 |
///one can change the priority of an item, add or erase an item, etc. |
| 41 | 41 |
/// |
| 42 | 42 |
///\tparam _Prio Type of the priority of the items. |
| 43 | 43 |
///\tparam _ItemIntMap A read and writable Item int map, used internally |
| 44 | 44 |
///to handle the cross references. |
| 45 | 45 |
///\tparam _Compare A class for the ordering of the priorities. The |
| 46 | 46 |
///default is \c std::less<_Prio>. |
| 47 | 47 |
/// |
| 48 | 48 |
///\sa FibHeap |
| 49 | 49 |
///\sa Dijkstra |
| 50 | 50 |
template <typename _Prio, typename _ItemIntMap, |
| 51 | 51 |
typename _Compare = std::less<_Prio> > |
| 52 | 52 |
class BinHeap {
|
| 53 | 53 |
|
| 54 | 54 |
public: |
| 55 | 55 |
///\e |
| 56 | 56 |
typedef _ItemIntMap ItemIntMap; |
| 57 | 57 |
///\e |
| 58 | 58 |
typedef _Prio Prio; |
| 59 | 59 |
///\e |
| 60 | 60 |
typedef typename ItemIntMap::Key Item; |
| 61 | 61 |
///\e |
| 62 | 62 |
typedef std::pair<Item,Prio> Pair; |
| 63 | 63 |
///\e |
| 64 | 64 |
typedef _Compare Compare; |
| 65 | 65 |
|
| 66 | 66 |
/// \brief Type to represent the items states. |
| 67 | 67 |
/// |
| 68 | 68 |
/// Each Item element have a state associated to it. It may be "in heap", |
| 69 | 69 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
| 70 | 70 |
/// heap's point of view, but may be useful to the user. |
| 71 | 71 |
/// |
| 72 | 72 |
/// The ItemIntMap \e should be initialized in such way that it maps |
| 73 | 73 |
/// PRE_HEAP (-1) to any element to be put in the heap... |
| 74 | 74 |
enum State {
|
| 75 | 75 |
IN_HEAP = 0, |
| 76 | 76 |
PRE_HEAP = -1, |
| 77 | 77 |
POST_HEAP = -2 |
| 78 | 78 |
}; |
| 79 | 79 |
|
| 80 | 80 |
private: |
| 81 | 81 |
std::vector<Pair> data; |
| 82 | 82 |
Compare comp; |
| 83 | 83 |
ItemIntMap &iim; |
| 84 | 84 |
|
| 85 | 85 |
public: |
| 86 | 86 |
/// \brief The constructor. |
| 87 | 87 |
/// |
| 88 | 88 |
/// The constructor. |
| 89 | 89 |
/// \param _iim should be given to the constructor, since it is used |
| 90 | 90 |
/// internally to handle the cross references. The value of the map |
| 91 | 91 |
/// should be PRE_HEAP (-1) for each element. |
| 92 | 92 |
explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
|
| 93 | 93 |
|
| 94 | 94 |
/// \brief The constructor. |
| 95 | 95 |
/// |
| 96 | 96 |
/// The constructor. |
| 97 | 97 |
/// \param _iim should be given to the constructor, since it is used |
| 98 | 98 |
/// internally to handle the cross references. The value of the map |
| 99 | 99 |
/// should be PRE_HEAP (-1) for each element. |
| 100 | 100 |
/// |
| 101 | 101 |
/// \param _comp The comparator function object. |
| 102 | 102 |
BinHeap(ItemIntMap &_iim, const Compare &_comp) |
| 103 | 103 |
: iim(_iim), comp(_comp) {}
|
| 104 | 104 |
|
| 105 | 105 |
|
| 106 | 106 |
/// The number of items stored in the heap. |
| 107 | 107 |
/// |
| 108 | 108 |
/// \brief Returns the number of items stored in the heap. |
| 109 | 109 |
int size() const { return data.size(); }
|
| 110 | 110 |
|
| 111 | 111 |
/// \brief Checks if the heap stores no items. |
| 112 | 112 |
/// |
| 113 | 113 |
/// Returns \c true if and only if the heap stores no items. |
| 114 | 114 |
bool empty() const { return data.empty(); }
|
| 115 | 115 |
|
| 116 | 116 |
/// \brief Make empty this heap. |
| 117 | 117 |
/// |
| 118 | 118 |
/// Make empty this heap. It does not change the cross reference map. |
| 119 | 119 |
/// If you want to reuse what is not surely empty you should first clear |
| 120 | 120 |
/// the heap and after that you should set the cross reference map for |
| 121 | 121 |
/// each item to \c PRE_HEAP. |
| 122 | 122 |
void clear() {
|
| 123 | 123 |
data.clear(); |
| 124 | 124 |
} |
| 125 | 125 |
|
| 126 | 126 |
private: |
| 127 | 127 |
static int parent(int i) { return (i-1)/2; }
|
| 128 | 128 |
|
| 129 | 129 |
static int second_child(int i) { return 2*i+2; }
|
| 130 | 130 |
bool less(const Pair &p1, const Pair &p2) const {
|
| 131 | 131 |
return comp(p1.second, p2.second); |
| 132 | 132 |
} |
| 133 | 133 |
|
| 134 | 134 |
int bubble_up(int hole, Pair p) {
|
| 135 | 135 |
int par = parent(hole); |
| 136 | 136 |
while( hole>0 && less(p,data[par]) ) {
|
| 137 | 137 |
move(data[par],hole); |
| 138 | 138 |
hole = par; |
| 139 | 139 |
par = parent(hole); |
| 140 | 140 |
} |
| 141 | 141 |
move(p, hole); |
| 142 | 142 |
return hole; |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
int bubble_down(int hole, Pair p, int length) {
|
| 146 | 146 |
int child = second_child(hole); |
| 147 | 147 |
while(child < length) {
|
| 148 | 148 |
if( less(data[child-1], data[child]) ) {
|
| 149 | 149 |
--child; |
| 150 | 150 |
} |
| 151 | 151 |
if( !less(data[child], p) ) |
| 152 | 152 |
goto ok; |
| 153 | 153 |
move(data[child], hole); |
| 154 | 154 |
hole = child; |
| 155 | 155 |
child = second_child(hole); |
| 156 | 156 |
} |
| 157 | 157 |
child--; |
| 158 | 158 |
if( child<length && less(data[child], p) ) {
|
| 159 | 159 |
move(data[child], hole); |
| 160 | 160 |
hole=child; |
| 161 | 161 |
} |
| 162 | 162 |
ok: |
| 163 | 163 |
move(p, hole); |
| 164 | 164 |
return hole; |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
void move(const Pair &p, int i) {
|
| 168 | 168 |
data[i] = p; |
| 169 | 169 |
iim.set(p.first, i); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
public: |
| 173 | 173 |
/// \brief Insert a pair of item and priority into the heap. |
| 174 | 174 |
/// |
| 175 | 175 |
/// Adds \c p.first to the heap with priority \c p.second. |
| 176 | 176 |
/// \param p The pair to insert. |
| 177 | 177 |
void push(const Pair &p) {
|
| 178 | 178 |
int n = data.size(); |
| 179 | 179 |
data.resize(n+1); |
| 180 | 180 |
bubble_up(n, p); |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
/// \brief Insert an item into the heap with the given heap. |
| 184 | 184 |
/// |
| 185 | 185 |
/// Adds \c i to the heap with priority \c p. |
| 186 | 186 |
/// \param i The item to insert. |
| 187 | 187 |
/// \param p The priority of the item. |
| 188 | 188 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
|
| 189 | 189 |
|
| 190 | 190 |
/// \brief Returns the item with minimum priority relative to \c Compare. |
| 191 | 191 |
/// |
| 192 | 192 |
/// This method returns the item with minimum priority relative to \c |
| 193 | 193 |
/// Compare. |
| 194 | 194 |
/// \pre The heap must be nonempty. |
| 195 | 195 |
Item top() const {
|
| 196 | 196 |
return data[0].first; |
| 197 | 197 |
} |
| 198 | 198 |
|
| 199 | 199 |
/// \brief Returns the minimum priority relative to \c Compare. |
| 200 | 200 |
/// |
| 201 | 201 |
/// It returns the minimum priority relative to \c Compare. |
| 202 | 202 |
/// \pre The heap must be nonempty. |
| 203 | 203 |
Prio prio() const {
|
| 204 | 204 |
return data[0].second; |
| 205 | 205 |
} |
| 206 | 206 |
|
| 207 | 207 |
/// \brief Deletes the item with minimum priority relative to \c Compare. |
| 208 | 208 |
/// |
| 209 | 209 |
/// This method deletes the item with minimum priority relative to \c |
| 210 | 210 |
/// Compare from the heap. |
| 211 | 211 |
/// \pre The heap must be non-empty. |
| 212 | 212 |
void pop() {
|
| 213 | 213 |
int n = data.size()-1; |
| 214 | 214 |
iim.set(data[0].first, POST_HEAP); |
| 215 | 215 |
if (n > 0) {
|
| 216 | 216 |
bubble_down(0, data[n], n); |
| 217 | 217 |
} |
| 218 | 218 |
data.pop_back(); |
| 219 | 219 |
} |
| 220 | 220 |
|
| 221 | 221 |
/// \brief Deletes \c i from the heap. |
| 222 | 222 |
/// |
| 223 | 223 |
/// This method deletes item \c i from the heap. |
| 224 | 224 |
/// \param i The item to erase. |
| 225 | 225 |
/// \pre The item should be in the heap. |
| 226 | 226 |
void erase(const Item &i) {
|
| 227 | 227 |
int h = iim[i]; |
| 228 | 228 |
int n = data.size()-1; |
| 229 | 229 |
iim.set(data[h].first, POST_HEAP); |
| 230 | 230 |
if( h < n ) {
|
| 231 | 231 |
if ( bubble_up(h, data[n]) == h) {
|
| 232 | 232 |
bubble_down(h, data[n], n); |
| 233 | 233 |
} |
| 234 | 234 |
} |
| 235 | 235 |
data.pop_back(); |
| 236 | 236 |
} |
| 237 | 237 |
|
| 238 | 238 |
|
| 239 | 239 |
/// \brief Returns the priority of \c i. |
| 240 | 240 |
/// |
| 241 | 241 |
/// This function returns the priority of item \c i. |
| 242 | 242 |
/// \pre \c i must be in the heap. |
| 243 | 243 |
/// \param i The item. |
| 244 | 244 |
Prio operator[](const Item &i) const {
|
| 245 | 245 |
int idx = iim[i]; |
| 246 | 246 |
return data[idx].second; |
| 247 | 247 |
} |
| 248 | 248 |
|
| 249 | 249 |
/// \brief \c i gets to the heap with priority \c p independently |
| 250 | 250 |
/// if \c i was already there. |
| 251 | 251 |
/// |
| 252 | 252 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
| 253 | 253 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
| 254 | 254 |
/// \param i The item. |
| 255 | 255 |
/// \param p The priority. |
| 256 | 256 |
void set(const Item &i, const Prio &p) {
|
| 257 | 257 |
int idx = iim[i]; |
| 258 | 258 |
if( idx < 0 ) {
|
| 259 | 259 |
push(i,p); |
| 260 | 260 |
} |
| 261 | 261 |
else if( comp(p, data[idx].second) ) {
|
| 262 | 262 |
bubble_up(idx, Pair(i,p)); |
| 263 | 263 |
} |
| 264 | 264 |
else {
|
| 265 | 265 |
bubble_down(idx, Pair(i,p), data.size()); |
| 266 | 266 |
} |
| 267 | 267 |
} |
| 268 | 268 |
|
| 269 | 269 |
/// \brief Decreases the priority of \c i to \c p. |
| 270 | 270 |
/// |
| 271 | 271 |
/// This method decreases the priority of item \c i to \c p. |
| 272 | 272 |
/// \pre \c i must be stored in the heap with priority at least \c |
| 273 | 273 |
/// p relative to \c Compare. |
| 274 | 274 |
/// \param i The item. |
| 275 | 275 |
/// \param p The priority. |
| 276 | 276 |
void decrease(const Item &i, const Prio &p) {
|
| 277 | 277 |
int idx = iim[i]; |
| 278 | 278 |
bubble_up(idx, Pair(i,p)); |
| 279 | 279 |
} |
| 280 | 280 |
|
| 281 | 281 |
/// \brief Increases the priority of \c i to \c p. |
| 282 | 282 |
/// |
| 283 | 283 |
/// This method sets the priority of item \c i to \c p. |
| 284 | 284 |
/// \pre \c i must be stored in the heap with priority at most \c |
| 285 | 285 |
/// p relative to \c Compare. |
| 286 | 286 |
/// \param i The item. |
| 287 | 287 |
/// \param p The priority. |
| 288 | 288 |
void increase(const Item &i, const Prio &p) {
|
| 289 | 289 |
int idx = iim[i]; |
| 290 | 290 |
bubble_down(idx, Pair(i,p), data.size()); |
| 291 | 291 |
} |
| 292 | 292 |
|
| 293 | 293 |
/// \brief Returns if \c item is in, has already been in, or has |
| 294 | 294 |
/// never been in the heap. |
| 295 | 295 |
/// |
| 296 | 296 |
/// This method returns PRE_HEAP if \c item has never been in the |
| 297 | 297 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
| 298 | 298 |
/// otherwise. In the latter case it is possible that \c item will |
| 299 | 299 |
/// get back to the heap again. |
| 300 | 300 |
/// \param i The item. |
| 301 | 301 |
State state(const Item &i) const {
|
| 302 | 302 |
int s = iim[i]; |
| 303 | 303 |
if( s>=0 ) |
| 304 | 304 |
s=0; |
| 305 | 305 |
return State(s); |
| 306 | 306 |
} |
| 307 | 307 |
|
| 308 | 308 |
/// \brief Sets the state of the \c item in the heap. |
| 309 | 309 |
/// |
| 310 | 310 |
/// Sets the state of the \c item in the heap. It can be used to |
| 311 | 311 |
/// manually clear the heap when it is important to achive the |
| 312 | 312 |
/// better time complexity. |
| 313 | 313 |
/// \param i The item. |
| 314 | 314 |
/// \param st The state. It should not be \c IN_HEAP. |
| 315 | 315 |
void state(const Item& i, State st) {
|
| 316 | 316 |
switch (st) {
|
| 317 | 317 |
case POST_HEAP: |
| 318 | 318 |
case PRE_HEAP: |
| 319 | 319 |
if (state(i) == IN_HEAP) {
|
| 320 | 320 |
erase(i); |
| 321 | 321 |
} |
| 322 | 322 |
iim[i] = st; |
| 323 | 323 |
break; |
| 324 | 324 |
case IN_HEAP: |
| 325 | 325 |
break; |
| 326 | 326 |
} |
| 327 | 327 |
} |
| 328 | 328 |
|
| 329 | 329 |
/// \brief Replaces an item in the heap. |
| 330 | 330 |
/// |
| 331 | 331 |
/// The \c i item is replaced with \c j item. The \c i item should |
| 332 | 332 |
/// be in the heap, while the \c j should be out of the heap. The |
| 333 | 333 |
/// \c i item will out of the heap and \c j will be in the heap |
| 334 | 334 |
/// with the same prioriority as prevoiusly the \c i item. |
| 335 | 335 |
void replace(const Item& i, const Item& j) {
|
| 336 | 336 |
int idx = iim[i]; |
| 337 | 337 |
iim.set(i, iim[j]); |
| 338 | 338 |
iim.set(j, idx); |
| 339 | 339 |
data[idx].first = j; |
| 340 | 340 |
} |
| 341 | 341 |
|
| 342 | 342 |
}; // class BinHeap |
| 343 | 343 |
|
| 344 | 344 |
} // namespace lemon |
| 345 | 345 |
|
| 346 | 346 |
#endif // LEMON_BIN_HEAP_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H |
| 20 | 20 |
#define LEMON_BITS_ALTERATION_NOTIFIER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <list> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/core.h> |
| 26 | 26 |
|
| 27 | 27 |
//\ingroup graphbits |
| 28 | 28 |
//\file |
| 29 | 29 |
//\brief Observer notifier for graph alteration observers. |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
// \ingroup graphbits |
| 34 | 34 |
// |
| 35 | 35 |
// \brief Notifier class to notify observes about alterations in |
| 36 | 36 |
// a container. |
| 37 | 37 |
// |
| 38 | 38 |
// The simple graphs can be refered as two containers: a node container |
| 39 | 39 |
// and an edge container. But they do not store values directly, they |
| 40 | 40 |
// are just key continars for more value containers, which are the |
| 41 | 41 |
// node and edge maps. |
| 42 | 42 |
// |
| 43 | 43 |
// The node and edge sets of the graphs can be changed as we add or erase |
| 44 | 44 |
// nodes and edges in the graph. LEMON would like to handle easily |
| 45 | 45 |
// that the node and edge maps should contain values for all nodes or |
| 46 | 46 |
// edges. If we want to check on every indicing if the map contains |
| 47 | 47 |
// the current indicing key that cause a drawback in the performance |
| 48 | 48 |
// in the library. We use another solution: we notify all maps about |
| 49 | 49 |
// an alteration in the graph, which cause only drawback on the |
| 50 | 50 |
// alteration of the graph. |
| 51 | 51 |
// |
| 52 | 52 |
// This class provides an interface to a node or edge container. |
| 53 | 53 |
// The first() and next() member functions make possible |
| 54 | 54 |
// to iterate on the keys of the container. |
| 55 | 55 |
// The id() function returns an integer id for each key. |
| 56 | 56 |
// The maxId() function gives back an upper bound of the ids. |
| 57 | 57 |
// |
| 58 | 58 |
// For the proper functonality of this class, we should notify it |
| 59 | 59 |
// about each alteration in the container. The alterations have four type: |
| 60 | 60 |
// add(), erase(), build() and clear(). The add() and |
| 61 | 61 |
// erase() signal that only one or few items added or erased to or |
| 62 | 62 |
// from the graph. If all items are erased from the graph or if a new graph |
| 63 | 63 |
// is built from an empty graph, then it can be signaled with the |
| 64 | 64 |
// clear() and build() members. Important rule that if we erase items |
| 65 | 65 |
// from graphs we should first signal the alteration and after that erase |
| 66 | 66 |
// them from the container, on the other way on item addition we should |
| 67 | 67 |
// first extend the container and just after that signal the alteration. |
| 68 | 68 |
// |
| 69 | 69 |
// The alteration can be observed with a class inherited from the |
| 70 | 70 |
// ObserverBase nested class. The signals can be handled with |
| 71 | 71 |
// overriding the virtual functions defined in the base class. The |
| 72 | 72 |
// observer base can be attached to the notifier with the |
| 73 | 73 |
// attach() member and can be detached with detach() function. The |
| 74 | 74 |
// alteration handlers should not call any function which signals |
| 75 | 75 |
// an other alteration in the same notifier and should not |
| 76 | 76 |
// detach any observer from the notifier. |
| 77 | 77 |
// |
| 78 | 78 |
// Alteration observers try to be exception safe. If an add() or |
| 79 | 79 |
// a clear() function throws an exception then the remaining |
| 80 | 80 |
// observeres will not be notified and the fulfilled additions will |
| 81 | 81 |
// be rolled back by calling the erase() or clear() functions. |
| 82 | 82 |
// Hence erase() and clear() should not throw exception. |
| 83 | 83 |
// Actullay, they can throw only \ref ImmediateDetach exception, |
| 84 | 84 |
// which detach the observer from the notifier. |
| 85 | 85 |
// |
| 86 | 86 |
// There are some cases, when the alteration observing is not completly |
| 87 | 87 |
// reliable. If we want to carry out the node degree in the graph |
| 88 | 88 |
// as in the \ref InDegMap and we use the reverseArc(), then it cause |
| 89 | 89 |
// unreliable functionality. Because the alteration observing signals |
| 90 | 90 |
// only erasing and adding but not the reversing, it will stores bad |
| 91 | 91 |
// degrees. Apart form that the subgraph adaptors cannot even signal |
| 92 | 92 |
// the alterations because just a setting in the filter map can modify |
| 93 | 93 |
// the graph and this cannot be watched in any way. |
| 94 | 94 |
// |
| 95 | 95 |
// \param _Container The container which is observed. |
| 96 | 96 |
// \param _Item The item type which is obserbved. |
| 97 | 97 |
|
| 98 | 98 |
template <typename _Container, typename _Item> |
| 99 | 99 |
class AlterationNotifier {
|
| 100 | 100 |
public: |
| 101 | 101 |
|
| 102 | 102 |
typedef True Notifier; |
| 103 | 103 |
|
| 104 | 104 |
typedef _Container Container; |
| 105 | 105 |
typedef _Item Item; |
| 106 | 106 |
|
| 107 | 107 |
// \brief Exception which can be called from clear() and |
| 108 | 108 |
// erase(). |
| 109 | 109 |
// |
| 110 | 110 |
// From the clear() and erase() function only this |
| 111 | 111 |
// exception is allowed to throw. The exception immediatly |
| 112 | 112 |
// detaches the current observer from the notifier. Because the |
| 113 | 113 |
// clear() and erase() should not throw other exceptions |
| 114 | 114 |
// it can be used to invalidate the observer. |
| 115 | 115 |
struct ImmediateDetach {};
|
| 116 | 116 |
|
| 117 | 117 |
// \brief ObserverBase is the base class for the observers. |
| 118 | 118 |
// |
| 119 | 119 |
// ObserverBase is the abstract base class for the observers. |
| 120 | 120 |
// It will be notified about an item was inserted into or |
| 121 | 121 |
// erased from the graph. |
| 122 | 122 |
// |
| 123 | 123 |
// The observer interface contains some pure virtual functions |
| 124 | 124 |
// to override. The add() and erase() functions are |
| 125 | 125 |
// to notify the oberver when one item is added or erased. |
| 126 | 126 |
// |
| 127 | 127 |
// The build() and clear() members are to notify the observer |
| 128 | 128 |
// about the container is built from an empty container or |
| 129 | 129 |
// is cleared to an empty container. |
| 130 | 130 |
class ObserverBase {
|
| 131 | 131 |
protected: |
| 132 | 132 |
typedef AlterationNotifier Notifier; |
| 133 | 133 |
|
| 134 | 134 |
friend class AlterationNotifier; |
| 135 | 135 |
|
| 136 | 136 |
// \brief Default constructor. |
| 137 | 137 |
// |
| 138 | 138 |
// Default constructor for ObserverBase. |
| 139 | 139 |
ObserverBase() : _notifier(0) {}
|
| 140 | 140 |
|
| 141 | 141 |
// \brief Constructor which attach the observer into notifier. |
| 142 | 142 |
// |
| 143 | 143 |
// Constructor which attach the observer into notifier. |
| 144 | 144 |
ObserverBase(AlterationNotifier& nf) {
|
| 145 | 145 |
attach(nf); |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
// \brief Constructor which attach the obserever to the same notifier. |
| 149 | 149 |
// |
| 150 | 150 |
// Constructor which attach the obserever to the same notifier as |
| 151 | 151 |
// the other observer is attached to. |
| 152 | 152 |
ObserverBase(const ObserverBase& copy) {
|
| 153 | 153 |
if (copy.attached()) {
|
| 154 | 154 |
attach(*copy.notifier()); |
| 155 | 155 |
} |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
// \brief Destructor |
| 159 | 159 |
virtual ~ObserverBase() {
|
| 160 | 160 |
if (attached()) {
|
| 161 | 161 |
detach(); |
| 162 | 162 |
} |
| 163 | 163 |
} |
| 164 | 164 |
|
| 165 | 165 |
// \brief Attaches the observer into an AlterationNotifier. |
| 166 | 166 |
// |
| 167 | 167 |
// This member attaches the observer into an AlterationNotifier. |
| 168 | 168 |
void attach(AlterationNotifier& nf) {
|
| 169 | 169 |
nf.attach(*this); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
// \brief Detaches the observer into an AlterationNotifier. |
| 173 | 173 |
// |
| 174 | 174 |
// This member detaches the observer from an AlterationNotifier. |
| 175 | 175 |
void detach() {
|
| 176 | 176 |
_notifier->detach(*this); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
// \brief Gives back a pointer to the notifier which the map |
| 180 | 180 |
// attached into. |
| 181 | 181 |
// |
| 182 | 182 |
// This function gives back a pointer to the notifier which the map |
| 183 | 183 |
// attached into. |
| 184 | 184 |
Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
|
| 185 | 185 |
|
| 186 | 186 |
// Gives back true when the observer is attached into a notifier. |
| 187 | 187 |
bool attached() const { return _notifier != 0; }
|
| 188 | 188 |
|
| 189 | 189 |
private: |
| 190 | 190 |
|
| 191 | 191 |
ObserverBase& operator=(const ObserverBase& copy); |
| 192 | 192 |
|
| 193 | 193 |
protected: |
| 194 | 194 |
|
| 195 | 195 |
Notifier* _notifier; |
| 196 | 196 |
typename std::list<ObserverBase*>::iterator _index; |
| 197 | 197 |
|
| 198 | 198 |
// \brief The member function to notificate the observer about an |
| 199 | 199 |
// item is added to the container. |
| 200 | 200 |
// |
| 201 | 201 |
// The add() member function notificates the observer about an item |
| 202 | 202 |
// is added to the container. It have to be overrided in the |
| 203 | 203 |
// subclasses. |
| 204 | 204 |
virtual void add(const Item&) = 0; |
| 205 | 205 |
|
| 206 | 206 |
// \brief The member function to notificate the observer about |
| 207 | 207 |
// more item is added to the container. |
| 208 | 208 |
// |
| 209 | 209 |
// The add() member function notificates the observer about more item |
| 210 | 210 |
// is added to the container. It have to be overrided in the |
| 211 | 211 |
// subclasses. |
| 212 | 212 |
virtual void add(const std::vector<Item>& items) = 0; |
| 213 | 213 |
|
| 214 | 214 |
// \brief The member function to notificate the observer about an |
| 215 | 215 |
// item is erased from the container. |
| 216 | 216 |
// |
| 217 | 217 |
// The erase() member function notificates the observer about an |
| 218 | 218 |
// item is erased from the container. It have to be overrided in |
| 219 | 219 |
// the subclasses. |
| 220 | 220 |
virtual void erase(const Item&) = 0; |
| 221 | 221 |
|
| 222 | 222 |
// \brief The member function to notificate the observer about |
| 223 | 223 |
// more item is erased from the container. |
| 224 | 224 |
// |
| 225 | 225 |
// The erase() member function notificates the observer about more item |
| 226 | 226 |
// is erased from the container. It have to be overrided in the |
| 227 | 227 |
// subclasses. |
| 228 | 228 |
virtual void erase(const std::vector<Item>& items) = 0; |
| 229 | 229 |
|
| 230 | 230 |
// \brief The member function to notificate the observer about the |
| 231 | 231 |
// container is built. |
| 232 | 232 |
// |
| 233 | 233 |
// The build() member function notificates the observer about the |
| 234 | 234 |
// container is built from an empty container. It have to be |
| 235 | 235 |
// overrided in the subclasses. |
| 236 | 236 |
virtual void build() = 0; |
| 237 | 237 |
|
| 238 | 238 |
// \brief The member function to notificate the observer about all |
| 239 | 239 |
// items are erased from the container. |
| 240 | 240 |
// |
| 241 | 241 |
// The clear() member function notificates the observer about all |
| 242 | 242 |
// items are erased from the container. It have to be overrided in |
| 243 | 243 |
// the subclasses. |
| 244 | 244 |
virtual void clear() = 0; |
| 245 | 245 |
|
| 246 | 246 |
}; |
| 247 | 247 |
|
| 248 | 248 |
protected: |
| 249 | 249 |
|
| 250 | 250 |
const Container* container; |
| 251 | 251 |
|
| 252 | 252 |
typedef std::list<ObserverBase*> Observers; |
| 253 | 253 |
Observers _observers; |
| 254 | 254 |
|
| 255 | 255 |
|
| 256 | 256 |
public: |
| 257 | 257 |
|
| 258 | 258 |
// \brief Default constructor. |
| 259 | 259 |
// |
| 260 | 260 |
// The default constructor of the AlterationNotifier. |
| 261 | 261 |
// It creates an empty notifier. |
| 262 | 262 |
AlterationNotifier() |
| 263 | 263 |
: container(0) {}
|
| 264 | 264 |
|
| 265 | 265 |
// \brief Constructor. |
| 266 | 266 |
// |
| 267 | 267 |
// Constructor with the observed container parameter. |
| 268 | 268 |
AlterationNotifier(const Container& _container) |
| 269 | 269 |
: container(&_container) {}
|
| 270 | 270 |
|
| 271 | 271 |
// \brief Copy Constructor of the AlterationNotifier. |
| 272 | 272 |
// |
| 273 | 273 |
// Copy constructor of the AlterationNotifier. |
| 274 | 274 |
// It creates only an empty notifier because the copiable |
| 275 | 275 |
// notifier's observers have to be registered still into that notifier. |
| 276 | 276 |
AlterationNotifier(const AlterationNotifier& _notifier) |
| 277 | 277 |
: container(_notifier.container) {}
|
| 278 | 278 |
|
| 279 | 279 |
// \brief Destructor. |
| 280 | 280 |
// |
| 281 | 281 |
// Destructor of the AlterationNotifier. |
| 282 | 282 |
~AlterationNotifier() {
|
| 283 | 283 |
typename Observers::iterator it; |
| 284 | 284 |
for (it = _observers.begin(); it != _observers.end(); ++it) {
|
| 285 | 285 |
(*it)->_notifier = 0; |
| 286 | 286 |
} |
| 287 | 287 |
} |
| 288 | 288 |
|
| 289 | 289 |
// \brief Sets the container. |
| 290 | 290 |
// |
| 291 | 291 |
// Sets the container. |
| 292 | 292 |
void setContainer(const Container& _container) {
|
| 293 | 293 |
container = &_container; |
| 294 | 294 |
} |
| 295 | 295 |
|
| 296 | 296 |
protected: |
| 297 | 297 |
|
| 298 | 298 |
AlterationNotifier& operator=(const AlterationNotifier&); |
| 299 | 299 |
|
| 300 | 300 |
public: |
| 301 | 301 |
|
| 302 | 302 |
// \brief First item in the container. |
| 303 | 303 |
// |
| 304 | 304 |
// Returns the first item in the container. It is |
| 305 | 305 |
// for start the iteration on the container. |
| 306 | 306 |
void first(Item& item) const {
|
| 307 | 307 |
container->first(item); |
| 308 | 308 |
} |
| 309 | 309 |
|
| 310 | 310 |
// \brief Next item in the container. |
| 311 | 311 |
// |
| 312 | 312 |
// Returns the next item in the container. It is |
| 313 | 313 |
// for iterate on the container. |
| 314 | 314 |
void next(Item& item) const {
|
| 315 | 315 |
container->next(item); |
| 316 | 316 |
} |
| 317 | 317 |
|
| 318 | 318 |
// \brief Returns the id of the item. |
| 319 | 319 |
// |
| 320 | 320 |
// Returns the id of the item provided by the container. |
| 321 | 321 |
int id(const Item& item) const {
|
| 322 | 322 |
return container->id(item); |
| 323 | 323 |
} |
| 324 | 324 |
|
| 325 | 325 |
// \brief Returns the maximum id of the container. |
| 326 | 326 |
// |
| 327 | 327 |
// Returns the maximum id of the container. |
| 328 | 328 |
int maxId() const {
|
| 329 | 329 |
return container->maxId(Item()); |
| 330 | 330 |
} |
| 331 | 331 |
|
| 332 | 332 |
protected: |
| 333 | 333 |
|
| 334 | 334 |
void attach(ObserverBase& observer) {
|
| 335 | 335 |
observer._index = _observers.insert(_observers.begin(), &observer); |
| 336 | 336 |
observer._notifier = this; |
| 337 | 337 |
} |
| 338 | 338 |
|
| 339 | 339 |
void detach(ObserverBase& observer) {
|
| 340 | 340 |
_observers.erase(observer._index); |
| 341 | 341 |
observer._index = _observers.end(); |
| 342 | 342 |
observer._notifier = 0; |
| 343 | 343 |
} |
| 344 | 344 |
|
| 345 | 345 |
public: |
| 346 | 346 |
|
| 347 | 347 |
// \brief Notifies all the registed observers about an item added to |
| 348 | 348 |
// the container. |
| 349 | 349 |
// |
| 350 | 350 |
// It notifies all the registed observers about an item added to |
| 351 | 351 |
// the container. |
| 352 | 352 |
void add(const Item& item) {
|
| 353 | 353 |
typename Observers::reverse_iterator it; |
| 354 | 354 |
try {
|
| 355 | 355 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
|
| 356 | 356 |
(*it)->add(item); |
| 357 | 357 |
} |
| 358 | 358 |
} catch (...) {
|
| 359 | 359 |
typename Observers::iterator jt; |
| 360 | 360 |
for (jt = it.base(); jt != _observers.end(); ++jt) {
|
| 361 | 361 |
(*jt)->erase(item); |
| 362 | 362 |
} |
| 363 | 363 |
throw; |
| 364 | 364 |
} |
| 365 | 365 |
} |
| 366 | 366 |
|
| 367 | 367 |
// \brief Notifies all the registed observers about more item added to |
| 368 | 368 |
// the container. |
| 369 | 369 |
// |
| 370 | 370 |
// It notifies all the registed observers about more item added to |
| 371 | 371 |
// the container. |
| 372 | 372 |
void add(const std::vector<Item>& items) {
|
| 373 | 373 |
typename Observers::reverse_iterator it; |
| 374 | 374 |
try {
|
| 375 | 375 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
|
| 376 | 376 |
(*it)->add(items); |
| 377 | 377 |
} |
| 378 | 378 |
} catch (...) {
|
| 379 | 379 |
typename Observers::iterator jt; |
| 380 | 380 |
for (jt = it.base(); jt != _observers.end(); ++jt) {
|
| 381 | 381 |
(*jt)->erase(items); |
| 382 | 382 |
} |
| 383 | 383 |
throw; |
| 384 | 384 |
} |
| 385 | 385 |
} |
| 386 | 386 |
|
| 387 | 387 |
// \brief Notifies all the registed observers about an item erased from |
| 388 | 388 |
// the container. |
| 389 | 389 |
// |
| 390 | 390 |
// It notifies all the registed observers about an item erased from |
| 391 | 391 |
// the container. |
| 392 | 392 |
void erase(const Item& item) throw() {
|
| 393 | 393 |
typename Observers::iterator it = _observers.begin(); |
| 394 | 394 |
while (it != _observers.end()) {
|
| 395 | 395 |
try {
|
| 396 | 396 |
(*it)->erase(item); |
| 397 | 397 |
++it; |
| 398 | 398 |
} catch (const ImmediateDetach&) {
|
| 399 | 399 |
(*it)->_index = _observers.end(); |
| 400 | 400 |
(*it)->_notifier = 0; |
| 401 | 401 |
it = _observers.erase(it); |
| 402 | 402 |
} |
| 403 | 403 |
} |
| 404 | 404 |
} |
| 405 | 405 |
|
| 406 | 406 |
// \brief Notifies all the registed observers about more item erased |
| 407 | 407 |
// from the container. |
| 408 | 408 |
// |
| 409 | 409 |
// It notifies all the registed observers about more item erased from |
| 410 | 410 |
// the container. |
| 411 | 411 |
void erase(const std::vector<Item>& items) {
|
| 412 | 412 |
typename Observers::iterator it = _observers.begin(); |
| 413 | 413 |
while (it != _observers.end()) {
|
| 414 | 414 |
try {
|
| 415 | 415 |
(*it)->erase(items); |
| 416 | 416 |
++it; |
| 417 | 417 |
} catch (const ImmediateDetach&) {
|
| 418 | 418 |
(*it)->_index = _observers.end(); |
| 419 | 419 |
(*it)->_notifier = 0; |
| 420 | 420 |
it = _observers.erase(it); |
| 421 | 421 |
} |
| 422 | 422 |
} |
| 423 | 423 |
} |
| 424 | 424 |
|
| 425 | 425 |
// \brief Notifies all the registed observers about the container is |
| 426 | 426 |
// built. |
| 427 | 427 |
// |
| 428 | 428 |
// Notifies all the registed observers about the container is built |
| 429 | 429 |
// from an empty container. |
| 430 | 430 |
void build() {
|
| 431 | 431 |
typename Observers::reverse_iterator it; |
| 432 | 432 |
try {
|
| 433 | 433 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
|
| 434 | 434 |
(*it)->build(); |
| 435 | 435 |
} |
| 436 | 436 |
} catch (...) {
|
| 437 | 437 |
typename Observers::iterator jt; |
| 438 | 438 |
for (jt = it.base(); jt != _observers.end(); ++jt) {
|
| 439 | 439 |
(*jt)->clear(); |
| 440 | 440 |
} |
| 441 | 441 |
throw; |
| 442 | 442 |
} |
| 443 | 443 |
} |
| 444 | 444 |
|
| 445 | 445 |
// \brief Notifies all the registed observers about all items are |
| 446 | 446 |
// erased. |
| 447 | 447 |
// |
| 448 | 448 |
// Notifies all the registed observers about all items are erased |
| 449 | 449 |
// from the container. |
| 450 | 450 |
void clear() {
|
| 451 | 451 |
typename Observers::iterator it = _observers.begin(); |
| 452 | 452 |
while (it != _observers.end()) {
|
| 453 | 453 |
try {
|
| 454 | 454 |
(*it)->clear(); |
| 455 | 455 |
++it; |
| 456 | 456 |
} catch (const ImmediateDetach&) {
|
| 457 | 457 |
(*it)->_index = _observers.end(); |
| 458 | 458 |
(*it)->_notifier = 0; |
| 459 | 459 |
it = _observers.erase(it); |
| 460 | 460 |
} |
| 461 | 461 |
} |
| 462 | 462 |
} |
| 463 | 463 |
}; |
| 464 | 464 |
|
| 465 | 465 |
} |
| 466 | 466 |
|
| 467 | 467 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_ARRAY_MAP_H |
| 20 | 20 |
#define LEMON_BITS_ARRAY_MAP_H |
| 21 | 21 |
|
| 22 | 22 |
#include <memory> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/bits/traits.h> |
| 25 | 25 |
#include <lemon/bits/alteration_notifier.h> |
| 26 | 26 |
#include <lemon/concept_check.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
|
| 29 | 29 |
// \ingroup graphbits |
| 30 | 30 |
// \file |
| 31 | 31 |
// \brief Graph map based on the array storage. |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
// \ingroup graphbits |
| 36 | 36 |
// |
| 37 | 37 |
// \brief Graph map based on the array storage. |
| 38 | 38 |
// |
| 39 | 39 |
// The ArrayMap template class is graph map structure that automatically |
| 40 | 40 |
// updates the map when a key is added to or erased from the graph. |
| 41 | 41 |
// This map uses the allocators to implement the container functionality. |
| 42 | 42 |
// |
| 43 | 43 |
// The template parameters are the Graph, the current Item type and |
| 44 | 44 |
// the Value type of the map. |
| 45 | 45 |
template <typename _Graph, typename _Item, typename _Value> |
| 46 | 46 |
class ArrayMap |
| 47 | 47 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
|
| 48 | 48 |
public: |
| 49 | 49 |
// The graph type. |
| 50 | 50 |
typedef _Graph Graph; |
| 51 | 51 |
// The item type. |
| 52 | 52 |
typedef _Item Item; |
| 53 | 53 |
// The reference map tag. |
| 54 | 54 |
typedef True ReferenceMapTag; |
| 55 | 55 |
|
| 56 | 56 |
// The key type of the map. |
| 57 | 57 |
typedef _Item Key; |
| 58 | 58 |
// The value type of the map. |
| 59 | 59 |
typedef _Value Value; |
| 60 | 60 |
|
| 61 | 61 |
// The const reference type of the map. |
| 62 | 62 |
typedef const _Value& ConstReference; |
| 63 | 63 |
// The reference type of the map. |
| 64 | 64 |
typedef _Value& Reference; |
| 65 | 65 |
|
| 66 | 66 |
// The notifier type. |
| 67 | 67 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
| 68 | 68 |
|
| 69 | 69 |
// The MapBase of the Map which imlements the core regisitry function. |
| 70 | 70 |
typedef typename Notifier::ObserverBase Parent; |
| 71 | 71 |
|
| 72 | 72 |
private: |
| 73 | 73 |
typedef std::allocator<Value> Allocator; |
| 74 | 74 |
|
| 75 | 75 |
public: |
| 76 | 76 |
|
| 77 | 77 |
// \brief Graph initialized map constructor. |
| 78 | 78 |
// |
| 79 | 79 |
// Graph initialized map constructor. |
| 80 | 80 |
explicit ArrayMap(const Graph& graph) {
|
| 81 | 81 |
Parent::attach(graph.notifier(Item())); |
| 82 | 82 |
allocate_memory(); |
| 83 | 83 |
Notifier* nf = Parent::notifier(); |
| 84 | 84 |
Item it; |
| 85 | 85 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 86 | 86 |
int id = nf->id(it);; |
| 87 | 87 |
allocator.construct(&(values[id]), Value()); |
| 88 | 88 |
} |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 | 91 |
// \brief Constructor to use default value to initialize the map. |
| 92 | 92 |
// |
| 93 | 93 |
// It constructs a map and initialize all of the the map. |
| 94 | 94 |
ArrayMap(const Graph& graph, const Value& value) {
|
| 95 | 95 |
Parent::attach(graph.notifier(Item())); |
| 96 | 96 |
allocate_memory(); |
| 97 | 97 |
Notifier* nf = Parent::notifier(); |
| 98 | 98 |
Item it; |
| 99 | 99 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 100 | 100 |
int id = nf->id(it);; |
| 101 | 101 |
allocator.construct(&(values[id]), value); |
| 102 | 102 |
} |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
private: |
| 106 | 106 |
// \brief Constructor to copy a map of the same map type. |
| 107 | 107 |
// |
| 108 | 108 |
// Constructor to copy a map of the same map type. |
| 109 | 109 |
ArrayMap(const ArrayMap& copy) : Parent() {
|
| 110 | 110 |
if (copy.attached()) {
|
| 111 | 111 |
attach(*copy.notifier()); |
| 112 | 112 |
} |
| 113 | 113 |
capacity = copy.capacity; |
| 114 | 114 |
if (capacity == 0) return; |
| 115 | 115 |
values = allocator.allocate(capacity); |
| 116 | 116 |
Notifier* nf = Parent::notifier(); |
| 117 | 117 |
Item it; |
| 118 | 118 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 119 | 119 |
int id = nf->id(it);; |
| 120 | 120 |
allocator.construct(&(values[id]), copy.values[id]); |
| 121 | 121 |
} |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
// \brief Assign operator. |
| 125 | 125 |
// |
| 126 | 126 |
// This operator assigns for each item in the map the |
| 127 | 127 |
// value mapped to the same item in the copied map. |
| 128 | 128 |
// The parameter map should be indiced with the same |
| 129 | 129 |
// itemset because this assign operator does not change |
| 130 | 130 |
// the container of the map. |
| 131 | 131 |
ArrayMap& operator=(const ArrayMap& cmap) {
|
| 132 | 132 |
return operator=<ArrayMap>(cmap); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
|
| 136 | 136 |
// \brief Template assign operator. |
| 137 | 137 |
// |
| 138 | 138 |
// The given parameter should be conform to the ReadMap |
| 139 | 139 |
// concecpt and could be indiced by the current item set of |
| 140 | 140 |
// the NodeMap. In this case the value for each item |
| 141 | 141 |
// is assigned by the value of the given ReadMap. |
| 142 | 142 |
template <typename CMap> |
| 143 | 143 |
ArrayMap& operator=(const CMap& cmap) {
|
| 144 | 144 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
| 145 | 145 |
const typename Parent::Notifier* nf = Parent::notifier(); |
| 146 | 146 |
Item it; |
| 147 | 147 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 148 | 148 |
set(it, cmap[it]); |
| 149 | 149 |
} |
| 150 | 150 |
return *this; |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
public: |
| 154 | 154 |
// \brief The destructor of the map. |
| 155 | 155 |
// |
| 156 | 156 |
// The destructor of the map. |
| 157 | 157 |
virtual ~ArrayMap() {
|
| 158 | 158 |
if (attached()) {
|
| 159 | 159 |
clear(); |
| 160 | 160 |
detach(); |
| 161 | 161 |
} |
| 162 | 162 |
} |
| 163 | 163 |
|
| 164 | 164 |
protected: |
| 165 | 165 |
|
| 166 | 166 |
using Parent::attach; |
| 167 | 167 |
using Parent::detach; |
| 168 | 168 |
using Parent::attached; |
| 169 | 169 |
|
| 170 | 170 |
public: |
| 171 | 171 |
|
| 172 | 172 |
// \brief The subscript operator. |
| 173 | 173 |
// |
| 174 | 174 |
// The subscript operator. The map can be subscripted by the |
| 175 | 175 |
// actual keys of the graph. |
| 176 | 176 |
Value& operator[](const Key& key) {
|
| 177 | 177 |
int id = Parent::notifier()->id(key); |
| 178 | 178 |
return values[id]; |
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 | 181 |
// \brief The const subscript operator. |
| 182 | 182 |
// |
| 183 | 183 |
// The const subscript operator. The map can be subscripted by the |
| 184 | 184 |
// actual keys of the graph. |
| 185 | 185 |
const Value& operator[](const Key& key) const {
|
| 186 | 186 |
int id = Parent::notifier()->id(key); |
| 187 | 187 |
return values[id]; |
| 188 | 188 |
} |
| 189 | 189 |
|
| 190 | 190 |
// \brief Setter function of the map. |
| 191 | 191 |
// |
| 192 | 192 |
// Setter function of the map. Equivalent with map[key] = val. |
| 193 | 193 |
// This is a compatibility feature with the not dereferable maps. |
| 194 | 194 |
void set(const Key& key, const Value& val) {
|
| 195 | 195 |
(*this)[key] = val; |
| 196 | 196 |
} |
| 197 | 197 |
|
| 198 | 198 |
protected: |
| 199 | 199 |
|
| 200 | 200 |
// \brief Adds a new key to the map. |
| 201 | 201 |
// |
| 202 | 202 |
// It adds a new key to the map. It is called by the observer notifier |
| 203 | 203 |
// and it overrides the add() member function of the observer base. |
| 204 | 204 |
virtual void add(const Key& key) {
|
| 205 | 205 |
Notifier* nf = Parent::notifier(); |
| 206 | 206 |
int id = nf->id(key); |
| 207 | 207 |
if (id >= capacity) {
|
| 208 | 208 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
| 209 | 209 |
while (new_capacity <= id) {
|
| 210 | 210 |
new_capacity <<= 1; |
| 211 | 211 |
} |
| 212 | 212 |
Value* new_values = allocator.allocate(new_capacity); |
| 213 | 213 |
Item it; |
| 214 | 214 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 215 | 215 |
int jd = nf->id(it);; |
| 216 | 216 |
if (id != jd) {
|
| 217 | 217 |
allocator.construct(&(new_values[jd]), values[jd]); |
| 218 | 218 |
allocator.destroy(&(values[jd])); |
| 219 | 219 |
} |
| 220 | 220 |
} |
| 221 | 221 |
if (capacity != 0) allocator.deallocate(values, capacity); |
| 222 | 222 |
values = new_values; |
| 223 | 223 |
capacity = new_capacity; |
| 224 | 224 |
} |
| 225 | 225 |
allocator.construct(&(values[id]), Value()); |
| 226 | 226 |
} |
| 227 | 227 |
|
| 228 | 228 |
// \brief Adds more new keys to the map. |
| 229 | 229 |
// |
| 230 | 230 |
// It adds more new keys to the map. It is called by the observer notifier |
| 231 | 231 |
// and it overrides the add() member function of the observer base. |
| 232 | 232 |
virtual void add(const std::vector<Key>& keys) {
|
| 233 | 233 |
Notifier* nf = Parent::notifier(); |
| 234 | 234 |
int max_id = -1; |
| 235 | 235 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 236 | 236 |
int id = nf->id(keys[i]); |
| 237 | 237 |
if (id > max_id) {
|
| 238 | 238 |
max_id = id; |
| 239 | 239 |
} |
| 240 | 240 |
} |
| 241 | 241 |
if (max_id >= capacity) {
|
| 242 | 242 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
| 243 | 243 |
while (new_capacity <= max_id) {
|
| 244 | 244 |
new_capacity <<= 1; |
| 245 | 245 |
} |
| 246 | 246 |
Value* new_values = allocator.allocate(new_capacity); |
| 247 | 247 |
Item it; |
| 248 | 248 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 249 | 249 |
int id = nf->id(it); |
| 250 | 250 |
bool found = false; |
| 251 | 251 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 252 | 252 |
int jd = nf->id(keys[i]); |
| 253 | 253 |
if (id == jd) {
|
| 254 | 254 |
found = true; |
| 255 | 255 |
break; |
| 256 | 256 |
} |
| 257 | 257 |
} |
| 258 | 258 |
if (found) continue; |
| 259 | 259 |
allocator.construct(&(new_values[id]), values[id]); |
| 260 | 260 |
allocator.destroy(&(values[id])); |
| 261 | 261 |
} |
| 262 | 262 |
if (capacity != 0) allocator.deallocate(values, capacity); |
| 263 | 263 |
values = new_values; |
| 264 | 264 |
capacity = new_capacity; |
| 265 | 265 |
} |
| 266 | 266 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 267 | 267 |
int id = nf->id(keys[i]); |
| 268 | 268 |
allocator.construct(&(values[id]), Value()); |
| 269 | 269 |
} |
| 270 | 270 |
} |
| 271 | 271 |
|
| 272 | 272 |
// \brief Erase a key from the map. |
| 273 | 273 |
// |
| 274 | 274 |
// Erase a key from the map. It is called by the observer notifier |
| 275 | 275 |
// and it overrides the erase() member function of the observer base. |
| 276 | 276 |
virtual void erase(const Key& key) {
|
| 277 | 277 |
int id = Parent::notifier()->id(key); |
| 278 | 278 |
allocator.destroy(&(values[id])); |
| 279 | 279 |
} |
| 280 | 280 |
|
| 281 | 281 |
// \brief Erase more keys from the map. |
| 282 | 282 |
// |
| 283 | 283 |
// Erase more keys from the map. It is called by the observer notifier |
| 284 | 284 |
// and it overrides the erase() member function of the observer base. |
| 285 | 285 |
virtual void erase(const std::vector<Key>& keys) {
|
| 286 | 286 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 287 | 287 |
int id = Parent::notifier()->id(keys[i]); |
| 288 | 288 |
allocator.destroy(&(values[id])); |
| 289 | 289 |
} |
| 290 | 290 |
} |
| 291 | 291 |
|
| 292 | 292 |
// \brief Builds the map. |
| 293 | 293 |
// |
| 294 | 294 |
// It builds the map. It is called by the observer notifier |
| 295 | 295 |
// and it overrides the build() member function of the observer base. |
| 296 | 296 |
virtual void build() {
|
| 297 | 297 |
Notifier* nf = Parent::notifier(); |
| 298 | 298 |
allocate_memory(); |
| 299 | 299 |
Item it; |
| 300 | 300 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 301 | 301 |
int id = nf->id(it);; |
| 302 | 302 |
allocator.construct(&(values[id]), Value()); |
| 303 | 303 |
} |
| 304 | 304 |
} |
| 305 | 305 |
|
| 306 | 306 |
// \brief Clear the map. |
| 307 | 307 |
// |
| 308 | 308 |
// It erase all items from the map. It is called by the observer notifier |
| 309 | 309 |
// and it overrides the clear() member function of the observer base. |
| 310 | 310 |
virtual void clear() {
|
| 311 | 311 |
Notifier* nf = Parent::notifier(); |
| 312 | 312 |
if (capacity != 0) {
|
| 313 | 313 |
Item it; |
| 314 | 314 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 315 | 315 |
int id = nf->id(it); |
| 316 | 316 |
allocator.destroy(&(values[id])); |
| 317 | 317 |
} |
| 318 | 318 |
allocator.deallocate(values, capacity); |
| 319 | 319 |
capacity = 0; |
| 320 | 320 |
} |
| 321 | 321 |
} |
| 322 | 322 |
|
| 323 | 323 |
private: |
| 324 | 324 |
|
| 325 | 325 |
void allocate_memory() {
|
| 326 | 326 |
int max_id = Parent::notifier()->maxId(); |
| 327 | 327 |
if (max_id == -1) {
|
| 328 | 328 |
capacity = 0; |
| 329 | 329 |
values = 0; |
| 330 | 330 |
return; |
| 331 | 331 |
} |
| 332 | 332 |
capacity = 1; |
| 333 | 333 |
while (capacity <= max_id) {
|
| 334 | 334 |
capacity <<= 1; |
| 335 | 335 |
} |
| 336 | 336 |
values = allocator.allocate(capacity); |
| 337 | 337 |
} |
| 338 | 338 |
|
| 339 | 339 |
int capacity; |
| 340 | 340 |
Value* values; |
| 341 | 341 |
Allocator allocator; |
| 342 | 342 |
|
| 343 | 343 |
}; |
| 344 | 344 |
|
| 345 | 345 |
} |
| 346 | 346 |
|
| 347 | 347 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_BASE_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_BASE_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/error.h> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/bits/map_extender.h> |
| 26 | 26 |
#include <lemon/bits/default_map.h> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
#include <lemon/concepts/maps.h> |
| 30 | 30 |
|
| 31 | 31 |
//\ingroup digraphbits |
| 32 | 32 |
//\file |
| 33 | 33 |
//\brief Extenders for the graph types |
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
// \ingroup digraphbits |
| 37 | 37 |
// |
| 38 | 38 |
// \brief BaseDigraph to BaseGraph extender |
| 39 | 39 |
template <typename Base> |
| 40 | 40 |
class UndirDigraphExtender : public Base {
|
| 41 | 41 |
|
| 42 | 42 |
public: |
| 43 | 43 |
|
| 44 | 44 |
typedef Base Parent; |
| 45 | 45 |
typedef typename Parent::Arc Edge; |
| 46 | 46 |
typedef typename Parent::Node Node; |
| 47 | 47 |
|
| 48 | 48 |
typedef True UndirectedTag; |
| 49 | 49 |
|
| 50 | 50 |
class Arc : public Edge {
|
| 51 | 51 |
friend class UndirDigraphExtender; |
| 52 | 52 |
|
| 53 | 53 |
protected: |
| 54 | 54 |
bool forward; |
| 55 | 55 |
|
| 56 | 56 |
Arc(const Edge &ue, bool _forward) : |
| 57 | 57 |
Edge(ue), forward(_forward) {}
|
| 58 | 58 |
|
| 59 | 59 |
public: |
| 60 | 60 |
Arc() {}
|
| 61 | 61 |
|
| 62 | 62 |
// Invalid arc constructor |
| 63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {}
|
| 64 | 64 |
|
| 65 | 65 |
bool operator==(const Arc &that) const {
|
| 66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
| 67 | 67 |
} |
| 68 | 68 |
bool operator!=(const Arc &that) const {
|
| 69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
| 70 | 70 |
} |
| 71 | 71 |
bool operator<(const Arc &that) const {
|
| 72 | 72 |
return forward<that.forward || |
| 73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
| 74 | 74 |
} |
| 75 | 75 |
}; |
| 76 | 76 |
|
| 77 | 77 |
// First node of the edge |
| 78 | 78 |
Node u(const Edge &e) const {
|
| 79 | 79 |
return Parent::source(e); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
// Source of the given arc |
| 83 | 83 |
Node source(const Arc &e) const {
|
| 84 | 84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
| 85 | 85 |
} |
| 86 | 86 |
|
| 87 | 87 |
// Second node of the edge |
| 88 | 88 |
Node v(const Edge &e) const {
|
| 89 | 89 |
return Parent::target(e); |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 | 92 |
// Target of the given arc |
| 93 | 93 |
Node target(const Arc &e) const {
|
| 94 | 94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
// \brief Directed arc from an edge. |
| 98 | 98 |
// |
| 99 | 99 |
// Returns a directed arc corresponding to the specified edge. |
| 100 | 100 |
// If the given bool is true, the first node of the given edge and |
| 101 | 101 |
// the source node of the returned arc are the same. |
| 102 | 102 |
static Arc direct(const Edge &e, bool d) {
|
| 103 | 103 |
return Arc(e, d); |
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 | 106 |
// Returns whether the given directed arc has the same orientation |
| 107 | 107 |
// as the corresponding edge. |
| 108 | 108 |
static bool direction(const Arc &a) { return a.forward; }
|
| 109 | 109 |
|
| 110 | 110 |
using Parent::first; |
| 111 | 111 |
using Parent::next; |
| 112 | 112 |
|
| 113 | 113 |
void first(Arc &e) const {
|
| 114 | 114 |
Parent::first(e); |
| 115 | 115 |
e.forward=true; |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
void next(Arc &e) const {
|
| 119 | 119 |
if( e.forward ) {
|
| 120 | 120 |
e.forward = false; |
| 121 | 121 |
} |
| 122 | 122 |
else {
|
| 123 | 123 |
Parent::next(e); |
| 124 | 124 |
e.forward = true; |
| 125 | 125 |
} |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
void firstOut(Arc &e, const Node &n) const {
|
| 129 | 129 |
Parent::firstIn(e,n); |
| 130 | 130 |
if( Edge(e) != INVALID ) {
|
| 131 | 131 |
e.forward = false; |
| 132 | 132 |
} |
| 133 | 133 |
else {
|
| 134 | 134 |
Parent::firstOut(e,n); |
| 135 | 135 |
e.forward = true; |
| 136 | 136 |
} |
| 137 | 137 |
} |
| 138 | 138 |
void nextOut(Arc &e) const {
|
| 139 | 139 |
if( ! e.forward ) {
|
| 140 | 140 |
Node n = Parent::target(e); |
| 141 | 141 |
Parent::nextIn(e); |
| 142 | 142 |
if( Edge(e) == INVALID ) {
|
| 143 | 143 |
Parent::firstOut(e, n); |
| 144 | 144 |
e.forward = true; |
| 145 | 145 |
} |
| 146 | 146 |
} |
| 147 | 147 |
else {
|
| 148 | 148 |
Parent::nextOut(e); |
| 149 | 149 |
} |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
void firstIn(Arc &e, const Node &n) const {
|
| 153 | 153 |
Parent::firstOut(e,n); |
| 154 | 154 |
if( Edge(e) != INVALID ) {
|
| 155 | 155 |
e.forward = false; |
| 156 | 156 |
} |
| 157 | 157 |
else {
|
| 158 | 158 |
Parent::firstIn(e,n); |
| 159 | 159 |
e.forward = true; |
| 160 | 160 |
} |
| 161 | 161 |
} |
| 162 | 162 |
void nextIn(Arc &e) const {
|
| 163 | 163 |
if( ! e.forward ) {
|
| 164 | 164 |
Node n = Parent::source(e); |
| 165 | 165 |
Parent::nextOut(e); |
| 166 | 166 |
if( Edge(e) == INVALID ) {
|
| 167 | 167 |
Parent::firstIn(e, n); |
| 168 | 168 |
e.forward = true; |
| 169 | 169 |
} |
| 170 | 170 |
} |
| 171 | 171 |
else {
|
| 172 | 172 |
Parent::nextIn(e); |
| 173 | 173 |
} |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
void firstInc(Edge &e, bool &d, const Node &n) const {
|
| 177 | 177 |
d = true; |
| 178 | 178 |
Parent::firstOut(e, n); |
| 179 | 179 |
if (e != INVALID) return; |
| 180 | 180 |
d = false; |
| 181 | 181 |
Parent::firstIn(e, n); |
| 182 | 182 |
} |
| 183 | 183 |
|
| 184 | 184 |
void nextInc(Edge &e, bool &d) const {
|
| 185 | 185 |
if (d) {
|
| 186 | 186 |
Node s = Parent::source(e); |
| 187 | 187 |
Parent::nextOut(e); |
| 188 | 188 |
if (e != INVALID) return; |
| 189 | 189 |
d = false; |
| 190 | 190 |
Parent::firstIn(e, s); |
| 191 | 191 |
} else {
|
| 192 | 192 |
Parent::nextIn(e); |
| 193 | 193 |
} |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 | 196 |
Node nodeFromId(int ix) const {
|
| 197 | 197 |
return Parent::nodeFromId(ix); |
| 198 | 198 |
} |
| 199 | 199 |
|
| 200 | 200 |
Arc arcFromId(int ix) const {
|
| 201 | 201 |
return direct(Parent::arcFromId(ix >> 1), bool(ix & 1)); |
| 202 | 202 |
} |
| 203 | 203 |
|
| 204 | 204 |
Edge edgeFromId(int ix) const {
|
| 205 | 205 |
return Parent::arcFromId(ix); |
| 206 | 206 |
} |
| 207 | 207 |
|
| 208 | 208 |
int id(const Node &n) const {
|
| 209 | 209 |
return Parent::id(n); |
| 210 | 210 |
} |
| 211 | 211 |
|
| 212 | 212 |
int id(const Edge &e) const {
|
| 213 | 213 |
return Parent::id(e); |
| 214 | 214 |
} |
| 215 | 215 |
|
| 216 | 216 |
int id(const Arc &e) const {
|
| 217 | 217 |
return 2 * Parent::id(e) + int(e.forward); |
| 218 | 218 |
} |
| 219 | 219 |
|
| 220 | 220 |
int maxNodeId() const {
|
| 221 | 221 |
return Parent::maxNodeId(); |
| 222 | 222 |
} |
| 223 | 223 |
|
| 224 | 224 |
int maxArcId() const {
|
| 225 | 225 |
return 2 * Parent::maxArcId() + 1; |
| 226 | 226 |
} |
| 227 | 227 |
|
| 228 | 228 |
int maxEdgeId() const {
|
| 229 | 229 |
return Parent::maxArcId(); |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
int arcNum() const {
|
| 233 | 233 |
return 2 * Parent::arcNum(); |
| 234 | 234 |
} |
| 235 | 235 |
|
| 236 | 236 |
int edgeNum() const {
|
| 237 | 237 |
return Parent::arcNum(); |
| 238 | 238 |
} |
| 239 | 239 |
|
| 240 | 240 |
Arc findArc(Node s, Node t, Arc p = INVALID) const {
|
| 241 | 241 |
if (p == INVALID) {
|
| 242 | 242 |
Edge arc = Parent::findArc(s, t); |
| 243 | 243 |
if (arc != INVALID) return direct(arc, true); |
| 244 | 244 |
arc = Parent::findArc(t, s); |
| 245 | 245 |
if (arc != INVALID) return direct(arc, false); |
| 246 | 246 |
} else if (direction(p)) {
|
| 247 | 247 |
Edge arc = Parent::findArc(s, t, p); |
| 248 | 248 |
if (arc != INVALID) return direct(arc, true); |
| 249 | 249 |
arc = Parent::findArc(t, s); |
| 250 | 250 |
if (arc != INVALID) return direct(arc, false); |
| 251 | 251 |
} else {
|
| 252 | 252 |
Edge arc = Parent::findArc(t, s, p); |
| 253 | 253 |
if (arc != INVALID) return direct(arc, false); |
| 254 | 254 |
} |
| 255 | 255 |
return INVALID; |
| 256 | 256 |
} |
| 257 | 257 |
|
| 258 | 258 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const {
|
| 259 | 259 |
if (s != t) {
|
| 260 | 260 |
if (p == INVALID) {
|
| 261 | 261 |
Edge arc = Parent::findArc(s, t); |
| 262 | 262 |
if (arc != INVALID) return arc; |
| 263 | 263 |
arc = Parent::findArc(t, s); |
| 264 | 264 |
if (arc != INVALID) return arc; |
| 265 | 265 |
} else if (Parent::s(p) == s) {
|
| 266 | 266 |
Edge arc = Parent::findArc(s, t, p); |
| 267 | 267 |
if (arc != INVALID) return arc; |
| 268 | 268 |
arc = Parent::findArc(t, s); |
| 269 | 269 |
if (arc != INVALID) return arc; |
| 270 | 270 |
} else {
|
| 271 | 271 |
Edge arc = Parent::findArc(t, s, p); |
| 272 | 272 |
if (arc != INVALID) return arc; |
| 273 | 273 |
} |
| 274 | 274 |
} else {
|
| 275 | 275 |
return Parent::findArc(s, t, p); |
| 276 | 276 |
} |
| 277 | 277 |
return INVALID; |
| 278 | 278 |
} |
| 279 | 279 |
}; |
| 280 | 280 |
|
| 281 | 281 |
template <typename Base> |
| 282 | 282 |
class BidirBpGraphExtender : public Base {
|
| 283 | 283 |
public: |
| 284 | 284 |
typedef Base Parent; |
| 285 | 285 |
typedef BidirBpGraphExtender Digraph; |
| 286 | 286 |
|
| 287 | 287 |
typedef typename Parent::Node Node; |
| 288 | 288 |
typedef typename Parent::Edge Edge; |
| 289 | 289 |
|
| 290 | 290 |
|
| 291 | 291 |
using Parent::first; |
| 292 | 292 |
using Parent::next; |
| 293 | 293 |
|
| 294 | 294 |
using Parent::id; |
| 295 | 295 |
|
| 296 | 296 |
class Red : public Node {
|
| 297 | 297 |
friend class BidirBpGraphExtender; |
| 298 | 298 |
public: |
| 299 | 299 |
Red() {}
|
| 300 | 300 |
Red(const Node& node) : Node(node) {
|
| 301 | 301 |
LEMON_DEBUG(Parent::red(node) || node == INVALID, |
| 302 | 302 |
typename Parent::NodeSetError()); |
| 303 | 303 |
} |
| 304 | 304 |
Red& operator=(const Node& node) {
|
| 305 | 305 |
LEMON_DEBUG(Parent::red(node) || node == INVALID, |
| 306 | 306 |
typename Parent::NodeSetError()); |
| 307 | 307 |
Node::operator=(node); |
| 308 | 308 |
return *this; |
| 309 | 309 |
} |
| 310 | 310 |
Red(Invalid) : Node(INVALID) {}
|
| 311 | 311 |
Red& operator=(Invalid) {
|
| 312 | 312 |
Node::operator=(INVALID); |
| 313 | 313 |
return *this; |
| 314 | 314 |
} |
| 315 | 315 |
}; |
| 316 | 316 |
|
| 317 | 317 |
void first(Red& node) const {
|
| 318 | 318 |
Parent::firstRed(static_cast<Node&>(node)); |
| 319 | 319 |
} |
| 320 | 320 |
void next(Red& node) const {
|
| 321 | 321 |
Parent::nextRed(static_cast<Node&>(node)); |
| 322 | 322 |
} |
| 323 | 323 |
|
| 324 | 324 |
int id(const Red& node) const {
|
| 325 | 325 |
return Parent::redId(node); |
| 326 | 326 |
} |
| 327 | 327 |
|
| 328 | 328 |
class Blue : public Node {
|
| 329 | 329 |
friend class BidirBpGraphExtender; |
| 330 | 330 |
public: |
| 331 | 331 |
Blue() {}
|
| 332 | 332 |
Blue(const Node& node) : Node(node) {
|
| 333 | 333 |
LEMON_DEBUG(Parent::blue(node) || node == INVALID, |
| 334 | 334 |
typename Parent::NodeSetError()); |
| 335 | 335 |
} |
| 336 | 336 |
Blue& operator=(const Node& node) {
|
| 337 | 337 |
LEMON_DEBUG(Parent::blue(node) || node == INVALID, |
| 338 | 338 |
typename Parent::NodeSetError()); |
| 339 | 339 |
Node::operator=(node); |
| 340 | 340 |
return *this; |
| 341 | 341 |
} |
| 342 | 342 |
Blue(Invalid) : Node(INVALID) {}
|
| 343 | 343 |
Blue& operator=(Invalid) {
|
| 344 | 344 |
Node::operator=(INVALID); |
| 345 | 345 |
return *this; |
| 346 | 346 |
} |
| 347 | 347 |
}; |
| 348 | 348 |
|
| 349 | 349 |
void first(Blue& node) const {
|
| 350 | 350 |
Parent::firstBlue(static_cast<Node&>(node)); |
| 351 | 351 |
} |
| 352 | 352 |
void next(Blue& node) const {
|
| 353 | 353 |
Parent::nextBlue(static_cast<Node&>(node)); |
| 354 | 354 |
} |
| 355 | 355 |
|
| 356 | 356 |
int id(const Blue& node) const {
|
| 357 | 357 |
return Parent::redId(node); |
| 358 | 358 |
} |
| 359 | 359 |
|
| 360 | 360 |
Node source(const Edge& arc) const {
|
| 361 | 361 |
return red(arc); |
| 362 | 362 |
} |
| 363 | 363 |
Node target(const Edge& arc) const {
|
| 364 | 364 |
return blue(arc); |
| 365 | 365 |
} |
| 366 | 366 |
|
| 367 | 367 |
void firstInc(Edge& arc, bool& dir, const Node& node) const {
|
| 368 | 368 |
if (Parent::red(node)) {
|
| 369 | 369 |
Parent::firstFromRed(arc, node); |
| 370 | 370 |
dir = true; |
| 371 | 371 |
} else {
|
| 372 | 372 |
Parent::firstFromBlue(arc, node); |
| 373 | 373 |
dir = static_cast<Edge&>(arc) == INVALID; |
| 374 | 374 |
} |
| 375 | 375 |
} |
| 376 | 376 |
void nextInc(Edge& arc, bool& dir) const {
|
| 377 | 377 |
if (dir) {
|
| 378 | 378 |
Parent::nextFromRed(arc); |
| 379 | 379 |
} else {
|
| 380 | 380 |
Parent::nextFromBlue(arc); |
| 381 | 381 |
if (arc == INVALID) dir = true; |
| 382 | 382 |
} |
| 383 | 383 |
} |
| 384 | 384 |
|
| 385 | 385 |
class Arc : public Edge {
|
| 386 | 386 |
friend class BidirBpGraphExtender; |
| 387 | 387 |
protected: |
| 388 | 388 |
bool forward; |
| 389 | 389 |
|
| 390 | 390 |
Arc(const Edge& arc, bool _forward) |
| 391 | 391 |
: Edge(arc), forward(_forward) {}
|
| 392 | 392 |
|
| 393 | 393 |
public: |
| 394 | 394 |
Arc() {}
|
| 395 | 395 |
Arc (Invalid) : Edge(INVALID), forward(true) {}
|
| 396 | 396 |
bool operator==(const Arc& i) const {
|
| 397 | 397 |
return Edge::operator==(i) && forward == i.forward; |
| 398 | 398 |
} |
| 399 | 399 |
bool operator!=(const Arc& i) const {
|
| 400 | 400 |
return Edge::operator!=(i) || forward != i.forward; |
| 401 | 401 |
} |
| 402 | 402 |
bool operator<(const Arc& i) const {
|
| 403 | 403 |
return Edge::operator<(i) || |
| 404 | 404 |
(!(i.forward<forward) && Edge(*this)<Edge(i)); |
| 405 | 405 |
} |
| 406 | 406 |
}; |
| 407 | 407 |
|
| 408 | 408 |
void first(Arc& arc) const {
|
| 409 | 409 |
Parent::first(static_cast<Edge&>(arc)); |
| 410 | 410 |
arc.forward = true; |
| 411 | 411 |
} |
| 412 | 412 |
|
| 413 | 413 |
void next(Arc& arc) const {
|
| 414 | 414 |
if (!arc.forward) {
|
| 415 | 415 |
Parent::next(static_cast<Edge&>(arc)); |
| 416 | 416 |
} |
| 417 | 417 |
arc.forward = !arc.forward; |
| 418 | 418 |
} |
| 419 | 419 |
|
| 420 | 420 |
void firstOut(Arc& arc, const Node& node) const {
|
| 421 | 421 |
if (Parent::red(node)) {
|
| 422 | 422 |
Parent::firstFromRed(arc, node); |
| 423 | 423 |
arc.forward = true; |
| 424 | 424 |
} else {
|
| 425 | 425 |
Parent::firstFromBlue(arc, node); |
| 426 | 426 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
| 427 | 427 |
} |
| 428 | 428 |
} |
| 429 | 429 |
void nextOut(Arc& arc) const {
|
| 430 | 430 |
if (arc.forward) {
|
| 431 | 431 |
Parent::nextFromRed(arc); |
| 432 | 432 |
} else {
|
| 433 | 433 |
Parent::nextFromBlue(arc); |
| 434 | 434 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
| 435 | 435 |
} |
| 436 | 436 |
} |
| 437 | 437 |
|
| 438 | 438 |
void firstIn(Arc& arc, const Node& node) const {
|
| 439 | 439 |
if (Parent::blue(node)) {
|
| 440 | 440 |
Parent::firstFromBlue(arc, node); |
| 441 | 441 |
arc.forward = true; |
| 442 | 442 |
} else {
|
| 443 | 443 |
Parent::firstFromRed(arc, node); |
| 444 | 444 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
| 445 | 445 |
} |
| 446 | 446 |
} |
| 447 | 447 |
void nextIn(Arc& arc) const {
|
| 448 | 448 |
if (arc.forward) {
|
| 449 | 449 |
Parent::nextFromBlue(arc); |
| 450 | 450 |
} else {
|
| 451 | 451 |
Parent::nextFromRed(arc); |
| 452 | 452 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
| 453 | 453 |
} |
| 454 | 454 |
} |
| 455 | 455 |
|
| 456 | 456 |
Node source(const Arc& arc) const {
|
| 457 | 457 |
return arc.forward ? Parent::red(arc) : Parent::blue(arc); |
| 458 | 458 |
} |
| 459 | 459 |
Node target(const Arc& arc) const {
|
| 460 | 460 |
return arc.forward ? Parent::blue(arc) : Parent::red(arc); |
| 461 | 461 |
} |
| 462 | 462 |
|
| 463 | 463 |
int id(const Arc& arc) const {
|
| 464 | 464 |
return (Parent::id(static_cast<const Edge&>(arc)) << 1) + |
| 465 | 465 |
(arc.forward ? 0 : 1); |
| 466 | 466 |
} |
| 467 | 467 |
Arc arcFromId(int ix) const {
|
| 468 | 468 |
return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0); |
| 469 | 469 |
} |
| 470 | 470 |
int maxArcId() const {
|
| 471 | 471 |
return (Parent::maxEdgeId() << 1) + 1; |
| 472 | 472 |
} |
| 473 | 473 |
|
| 474 | 474 |
bool direction(const Arc& arc) const {
|
| 475 | 475 |
return arc.forward; |
| 476 | 476 |
} |
| 477 | 477 |
|
| 478 | 478 |
Arc direct(const Edge& arc, bool dir) const {
|
| 479 | 479 |
return Arc(arc, dir); |
| 480 | 480 |
} |
| 481 | 481 |
|
| 482 | 482 |
int arcNum() const {
|
| 483 | 483 |
return 2 * Parent::edgeNum(); |
| 484 | 484 |
} |
| 485 | 485 |
|
| 486 | 486 |
int edgeNum() const {
|
| 487 | 487 |
return Parent::edgeNum(); |
| 488 | 488 |
} |
| 489 | 489 |
|
| 490 | 490 |
|
| 491 | 491 |
}; |
| 492 | 492 |
} |
| 493 | 493 |
|
| 494 | 494 |
#endif |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)