1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
// This file contains a modified version of the enable_if library from BOOST. |
|
20 |
// See the appropriate copyright notice below. |
|
21 |
|
|
22 |
// Boost enable_if library |
|
23 |
|
|
24 |
// Copyright 2003 (c) The Trustees of Indiana University. |
|
25 |
|
|
26 |
// Use, modification, and distribution is subject to the Boost Software |
|
27 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|
28 |
// http://www.boost.org/LICENSE_1_0.txt) |
|
29 |
|
|
30 |
// Authors: Jaakko Jarvi (jajarvi at osl.iu.edu) |
|
31 |
// Jeremiah Willcock (jewillco at osl.iu.edu) |
|
32 |
// Andrew Lumsdaine (lums at osl.iu.edu) |
|
33 |
|
|
34 |
|
|
35 |
#ifndef LEMON_BITS_ENABLE_IF_H |
|
36 |
#define LEMON_BITS_ENABLE_IF_H |
|
37 |
|
|
38 |
///\file |
|
39 |
///\brief Miscellaneous basic utilities |
|
40 |
|
|
41 |
namespace lemon |
|
42 |
{ |
|
43 |
|
|
44 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
45 |
|
|
46 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
47 |
/// |
|
48 |
///\sa False |
|
49 |
struct True { |
|
50 |
///\e |
|
51 |
static const bool value = true; |
|
52 |
}; |
|
53 |
|
|
54 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
55 |
|
|
56 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
57 |
/// |
|
58 |
///\sa True |
|
59 |
struct False { |
|
60 |
///\e |
|
61 |
static const bool value = false; |
|
62 |
}; |
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
template <typename T> |
|
67 |
struct Wrap { |
|
68 |
const T &value; |
|
69 |
Wrap(const T &t) : value(t) {} |
|
70 |
}; |
|
71 |
|
|
72 |
/**************** dummy class to avoid ambiguity ****************/ |
|
73 |
|
|
74 |
template<int T> struct dummy { dummy(int) {} }; |
|
75 |
|
|
76 |
/**************** enable_if from BOOST ****************/ |
|
77 |
|
|
78 |
template <typename Type, typename T = void> |
|
79 |
struct exists { |
|
80 |
typedef T type; |
|
81 |
}; |
|
82 |
|
|
83 |
|
|
84 |
template <bool B, class T = void> |
|
85 |
struct enable_if_c { |
|
86 |
typedef T type; |
|
87 |
}; |
|
88 |
|
|
89 |
template <class T> |
|
90 |
struct enable_if_c<false, T> {}; |
|
91 |
|
|
92 |
template <class Cond, class T = void> |
|
93 |
struct enable_if : public enable_if_c<Cond::value, T> {}; |
|
94 |
|
|
95 |
template <bool B, class T> |
|
96 |
struct lazy_enable_if_c { |
|
97 |
typedef typename T::type type; |
|
98 |
}; |
|
99 |
|
|
100 |
template <class T> |
|
101 |
struct lazy_enable_if_c<false, T> {}; |
|
102 |
|
|
103 |
template <class Cond, class T> |
|
104 |
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {}; |
|
105 |
|
|
106 |
|
|
107 |
template <bool B, class T = void> |
|
108 |
struct disable_if_c { |
|
109 |
typedef T type; |
|
110 |
}; |
|
111 |
|
|
112 |
template <class T> |
|
113 |
struct disable_if_c<true, T> {}; |
|
114 |
|
|
115 |
template <class Cond, class T = void> |
|
116 |
struct disable_if : public disable_if_c<Cond::value, T> {}; |
|
117 |
|
|
118 |
template <bool B, class T> |
|
119 |
struct lazy_disable_if_c { |
|
120 |
typedef typename T::type type; |
|
121 |
}; |
|
122 |
|
|
123 |
template <class T> |
|
124 |
struct lazy_disable_if_c<true, T> {}; |
|
125 |
|
|
126 |
template <class Cond, class T> |
|
127 |
struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {}; |
|
128 |
|
|
129 |
} // namespace lemon |
|
130 |
|
|
131 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
/// \ingroup demos |
20 | 20 |
/// \file |
21 | 21 |
/// \brief Demo of the graph drawing function \ref graphToEps() |
22 | 22 |
/// |
23 | 23 |
/// This demo program shows examples how to use the function \ref |
24 | 24 |
/// graphToEps(). It takes no input but simply creates 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 \ref maps-page "graph maps". |
30 | 30 |
/// |
31 | 31 |
/// \include graph_to_eps_demo.cc |
32 | 32 |
|
33 | 33 |
#include<lemon/list_graph.h> |
34 |
#include<lemon/graph_utils.h> |
|
35 | 34 |
#include<lemon/graph_to_eps.h> |
36 | 35 |
#include<lemon/math.h> |
37 | 36 |
|
38 | 37 |
using namespace std; |
39 | 38 |
using namespace lemon; |
40 | 39 |
|
41 | 40 |
int main() |
42 | 41 |
{ |
43 | 42 |
Palette palette; |
44 | 43 |
Palette paletteW(true); |
45 | 44 |
|
46 | 45 |
// Create a small digraph |
47 | 46 |
ListDigraph g; |
48 | 47 |
typedef ListDigraph::Node Node; |
49 | 48 |
typedef ListDigraph::NodeIt NodeIt; |
50 | 49 |
typedef ListDigraph::Arc Arc; |
51 | 50 |
typedef dim2::Point<int> Point; |
52 | 51 |
|
53 | 52 |
Node n1=g.addNode(); |
54 | 53 |
Node n2=g.addNode(); |
55 | 54 |
Node n3=g.addNode(); |
56 | 55 |
Node n4=g.addNode(); |
57 | 56 |
Node n5=g.addNode(); |
58 | 57 |
|
59 | 58 |
ListDigraph::NodeMap<Point> coords(g); |
60 | 59 |
ListDigraph::NodeMap<double> sizes(g); |
61 | 60 |
ListDigraph::NodeMap<int> colors(g); |
62 | 61 |
ListDigraph::NodeMap<int> shapes(g); |
63 | 62 |
ListDigraph::ArcMap<int> acolors(g); |
64 | 63 |
ListDigraph::ArcMap<int> widths(g); |
65 | 64 |
|
66 | 65 |
coords[n1]=Point(50,50); sizes[n1]=1; colors[n1]=1; shapes[n1]=0; |
67 | 66 |
coords[n2]=Point(50,70); sizes[n2]=2; colors[n2]=2; shapes[n2]=2; |
68 | 67 |
coords[n3]=Point(70,70); sizes[n3]=1; colors[n3]=3; shapes[n3]=0; |
69 | 68 |
coords[n4]=Point(70,50); sizes[n4]=2; colors[n4]=4; shapes[n4]=1; |
70 | 69 |
coords[n5]=Point(85,60); sizes[n5]=3; colors[n5]=5; shapes[n5]=2; |
71 | 70 |
|
72 | 71 |
Arc a; |
73 | 72 |
|
74 | 73 |
a=g.addArc(n1,n2); acolors[a]=0; widths[a]=1; |
75 | 74 |
a=g.addArc(n2,n3); acolors[a]=0; widths[a]=1; |
76 | 75 |
a=g.addArc(n3,n5); acolors[a]=0; widths[a]=3; |
77 | 76 |
a=g.addArc(n5,n4); acolors[a]=0; widths[a]=1; |
78 | 77 |
a=g.addArc(n4,n1); acolors[a]=0; widths[a]=1; |
79 | 78 |
a=g.addArc(n2,n4); acolors[a]=1; widths[a]=2; |
80 | 79 |
a=g.addArc(n3,n4); acolors[a]=2; widths[a]=1; |
81 | 80 |
|
82 | 81 |
IdMap<ListDigraph,Node> id(g); |
83 | 82 |
|
84 | 83 |
// Create .eps files showing the digraph with different options |
85 | 84 |
cout << "Create 'graph_to_eps_demo_out_1_pure.eps'" << endl; |
86 | 85 |
graphToEps(g,"graph_to_eps_demo_out_1_pure.eps"). |
87 | 86 |
coords(coords). |
88 | 87 |
title("Sample .eps figure"). |
89 | 88 |
copyright("(C) 2003-2008 LEMON Project"). |
90 | 89 |
run(); |
91 | 90 |
|
92 | 91 |
cout << "Create 'graph_to_eps_demo_out_2.eps'" << endl; |
93 | 92 |
graphToEps(g,"graph_to_eps_demo_out_2.eps"). |
94 | 93 |
coords(coords). |
95 | 94 |
title("Sample .eps figure"). |
96 | 95 |
copyright("(C) 2003-2008 LEMON Project"). |
97 | 96 |
absoluteNodeSizes().absoluteArcWidths(). |
98 | 97 |
nodeScale(2).nodeSizes(sizes). |
99 | 98 |
nodeShapes(shapes). |
100 | 99 |
nodeColors(composeMap(palette,colors)). |
101 | 100 |
arcColors(composeMap(palette,acolors)). |
102 | 101 |
arcWidthScale(.4).arcWidths(widths). |
103 | 102 |
nodeTexts(id).nodeTextSize(3). |
104 | 103 |
run(); |
105 | 104 |
|
106 | 105 |
cout << "Create 'graph_to_eps_demo_out_3_arr.eps'" << endl; |
107 | 106 |
graphToEps(g,"graph_to_eps_demo_out_3_arr.eps"). |
108 | 107 |
title("Sample .eps figure (with arrowheads)"). |
109 | 108 |
copyright("(C) 2003-2008 LEMON Project"). |
110 | 109 |
absoluteNodeSizes().absoluteArcWidths(). |
111 | 110 |
nodeColors(composeMap(palette,colors)). |
112 | 111 |
coords(coords). |
113 | 112 |
nodeScale(2).nodeSizes(sizes). |
114 | 113 |
nodeShapes(shapes). |
115 | 114 |
arcColors(composeMap(palette,acolors)). |
116 | 115 |
arcWidthScale(.4).arcWidths(widths). |
117 | 116 |
nodeTexts(id).nodeTextSize(3). |
118 | 117 |
drawArrows().arrowWidth(2).arrowLength(2). |
119 | 118 |
run(); |
120 | 119 |
|
121 | 120 |
// Add more arcs to the digraph |
122 | 121 |
a=g.addArc(n1,n4); acolors[a]=2; widths[a]=1; |
123 | 122 |
a=g.addArc(n4,n1); acolors[a]=1; widths[a]=2; |
124 | 123 |
|
125 | 124 |
a=g.addArc(n1,n2); acolors[a]=1; widths[a]=1; |
126 | 125 |
a=g.addArc(n1,n2); acolors[a]=2; widths[a]=1; |
127 | 126 |
a=g.addArc(n1,n2); acolors[a]=3; widths[a]=1; |
128 | 127 |
a=g.addArc(n1,n2); acolors[a]=4; widths[a]=1; |
129 | 128 |
a=g.addArc(n1,n2); acolors[a]=5; widths[a]=1; |
130 | 129 |
a=g.addArc(n1,n2); acolors[a]=6; widths[a]=1; |
131 | 130 |
a=g.addArc(n1,n2); acolors[a]=7; widths[a]=1; |
132 | 131 |
|
133 | 132 |
cout << "Create 'graph_to_eps_demo_out_4_par.eps'" << endl; |
134 | 133 |
graphToEps(g,"graph_to_eps_demo_out_4_par.eps"). |
135 | 134 |
title("Sample .eps figure (parallel arcs)"). |
136 | 135 |
copyright("(C) 2003-2008 LEMON Project"). |
137 | 136 |
absoluteNodeSizes().absoluteArcWidths(). |
138 | 137 |
nodeShapes(shapes). |
139 | 138 |
coords(coords). |
140 | 139 |
nodeScale(2).nodeSizes(sizes). |
141 | 140 |
nodeColors(composeMap(palette,colors)). |
142 | 141 |
arcColors(composeMap(palette,acolors)). |
143 | 142 |
arcWidthScale(.4).arcWidths(widths). |
144 | 143 |
nodeTexts(id).nodeTextSize(3). |
145 | 144 |
enableParallel().parArcDist(1.5). |
146 | 145 |
run(); |
147 | 146 |
|
148 | 147 |
cout << "Create 'graph_to_eps_demo_out_5_par_arr.eps'" << endl; |
149 | 148 |
graphToEps(g,"graph_to_eps_demo_out_5_par_arr.eps"). |
150 | 149 |
title("Sample .eps figure (parallel arcs and arrowheads)"). |
151 | 150 |
copyright("(C) 2003-2008 LEMON Project"). |
152 | 151 |
absoluteNodeSizes().absoluteArcWidths(). |
153 | 152 |
nodeScale(2).nodeSizes(sizes). |
154 | 153 |
coords(coords). |
155 | 154 |
nodeShapes(shapes). |
156 | 155 |
nodeColors(composeMap(palette,colors)). |
157 | 156 |
arcColors(composeMap(palette,acolors)). |
158 | 157 |
arcWidthScale(.3).arcWidths(widths). |
159 | 158 |
nodeTexts(id).nodeTextSize(3). |
160 | 159 |
enableParallel().parArcDist(1). |
161 | 160 |
drawArrows().arrowWidth(1).arrowLength(1). |
162 | 161 |
run(); |
163 | 162 |
|
164 | 163 |
cout << "Create 'graph_to_eps_demo_out_6_par_arr_a4.eps'" << endl; |
165 | 164 |
graphToEps(g,"graph_to_eps_demo_out_6_par_arr_a4.eps"). |
166 | 165 |
title("Sample .eps figure (fits to A4)"). |
167 | 166 |
copyright("(C) 2003-2008 LEMON Project"). |
168 | 167 |
scaleToA4(). |
169 | 168 |
absoluteNodeSizes().absoluteArcWidths(). |
170 | 169 |
nodeScale(2).nodeSizes(sizes). |
171 | 170 |
coords(coords). |
172 | 171 |
nodeShapes(shapes). |
173 | 172 |
nodeColors(composeMap(palette,colors)). |
174 | 173 |
arcColors(composeMap(palette,acolors)). |
175 | 174 |
arcWidthScale(.3).arcWidths(widths). |
176 | 175 |
nodeTexts(id).nodeTextSize(3). |
177 | 176 |
enableParallel().parArcDist(1). |
178 | 177 |
drawArrows().arrowWidth(1).arrowLength(1). |
179 | 178 |
run(); |
180 | 179 |
|
181 | 180 |
// Create an .eps file showing the colors of a default Palette |
182 | 181 |
ListDigraph h; |
183 | 182 |
ListDigraph::NodeMap<int> hcolors(h); |
184 | 183 |
ListDigraph::NodeMap<Point> hcoords(h); |
185 | 184 |
|
186 | 185 |
int cols=int(sqrt(double(palette.size()))); |
187 | 186 |
for(int i=0;i<int(paletteW.size());i++) { |
188 | 187 |
Node n=h.addNode(); |
189 | 188 |
hcoords[n]=Point(1+i%cols,1+i/cols); |
190 | 189 |
hcolors[n]=i; |
191 | 190 |
} |
192 | 191 |
|
193 | 192 |
cout << "Create 'graph_to_eps_demo_out_7_colors.eps'" << endl; |
194 | 193 |
graphToEps(h,"graph_to_eps_demo_out_7_colors.eps"). |
195 | 194 |
scale(60). |
196 | 195 |
title("Sample .eps figure (Palette demo)"). |
197 | 196 |
copyright("(C) 2003-2008 LEMON Project"). |
198 | 197 |
coords(hcoords). |
199 | 198 |
absoluteNodeSizes().absoluteArcWidths(). |
200 | 199 |
nodeScale(.45). |
201 | 200 |
distantColorNodeTexts(). |
202 | 201 |
nodeTexts(hcolors).nodeTextSize(.6). |
203 | 202 |
nodeColors(composeMap(paletteW,hcolors)). |
204 | 203 |
run(); |
205 | 204 |
|
206 | 205 |
return 0; |
207 | 206 |
} |
1 | 1 |
EXTRA_DIST += \ |
2 | 2 |
lemon/lemon.pc.in \ |
3 | 3 |
lemon/CMakeLists.txt |
4 | 4 |
|
5 | 5 |
pkgconfig_DATA += lemon/lemon.pc |
6 | 6 |
|
7 | 7 |
lib_LTLIBRARIES += lemon/libemon.la |
8 | 8 |
|
9 | 9 |
lemon_libemon_la_SOURCES = \ |
10 | 10 |
lemon/arg_parser.cc \ |
11 | 11 |
lemon/base.cc \ |
12 | 12 |
lemon/color.cc \ |
13 | 13 |
lemon/random.cc |
14 | 14 |
|
15 | 15 |
|
16 | 16 |
lemon_libemon_la_CXXFLAGS = $(GLPK_CFLAGS) $(CPLEX_CFLAGS) $(SOPLEX_CXXFLAGS) |
17 | 17 |
lemon_libemon_la_LDFLAGS = $(GLPK_LIBS) $(CPLEX_LIBS) $(SOPLEX_LIBS) |
18 | 18 |
|
19 | 19 |
lemon_HEADERS += \ |
20 | 20 |
lemon/arg_parser.h \ |
21 | 21 |
lemon/assert.h \ |
22 | 22 |
lemon/bfs.h \ |
23 | 23 |
lemon/bin_heap.h \ |
24 | 24 |
lemon/color.h \ |
25 | 25 |
lemon/concept_check.h \ |
26 | 26 |
lemon/counter.h \ |
27 |
lemon/core.h \ |
|
27 | 28 |
lemon/dfs.h \ |
28 | 29 |
lemon/dijkstra.h \ |
29 | 30 |
lemon/dim2.h \ |
30 | 31 |
lemon/error.h \ |
31 | 32 |
lemon/graph_to_eps.h \ |
32 |
lemon/graph_utils.h \ |
|
33 | 33 |
lemon/kruskal.h \ |
34 | 34 |
lemon/lgf_reader.h \ |
35 | 35 |
lemon/lgf_writer.h \ |
36 | 36 |
lemon/list_graph.h \ |
37 | 37 |
lemon/maps.h \ |
38 | 38 |
lemon/math.h \ |
39 | 39 |
lemon/path.h \ |
40 | 40 |
lemon/random.h \ |
41 | 41 |
lemon/smart_graph.h \ |
42 | 42 |
lemon/time_measure.h \ |
43 | 43 |
lemon/tolerance.h \ |
44 | 44 |
lemon/unionfind.h |
45 | 45 |
|
46 | 46 |
bits_HEADERS += \ |
47 | 47 |
lemon/bits/alteration_notifier.h \ |
48 | 48 |
lemon/bits/array_map.h \ |
49 | 49 |
lemon/bits/base_extender.h \ |
50 | 50 |
lemon/bits/bezier.h \ |
51 | 51 |
lemon/bits/default_map.h \ |
52 |
lemon/bits/enable_if.h \ |
|
52 | 53 |
lemon/bits/graph_extender.h \ |
53 |
lemon/bits/invalid.h \ |
|
54 | 54 |
lemon/bits/map_extender.h \ |
55 | 55 |
lemon/bits/path_dump.h \ |
56 | 56 |
lemon/bits/traits.h \ |
57 |
lemon/bits/utility.h \ |
|
58 | 57 |
lemon/bits/vector_map.h |
59 | 58 |
|
60 | 59 |
concept_HEADERS += \ |
61 | 60 |
lemon/concepts/digraph.h \ |
62 | 61 |
lemon/concepts/graph.h \ |
63 | 62 |
lemon/concepts/graph_components.h \ |
64 | 63 |
lemon/concepts/heap.h \ |
65 | 64 |
lemon/concepts/maps.h \ |
66 | 65 |
lemon/concepts/path.h |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\file |
20 | 20 |
///\brief Some basic non-inline functions and static global data. |
21 | 21 |
|
22 | 22 |
#include<lemon/tolerance.h> |
23 |
#include<lemon/ |
|
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 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief Bfs algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 |
#include <lemon/graph_utils.h> |
|
28 | 27 |
#include <lemon/bits/path_dump.h> |
29 |
#include <lemon/ |
|
28 |
#include <lemon/core.h> |
|
30 | 29 |
#include <lemon/error.h> |
31 | 30 |
#include <lemon/maps.h> |
32 | 31 |
|
33 | 32 |
namespace lemon { |
34 | 33 |
|
35 | 34 |
|
36 | 35 |
|
37 | 36 |
///Default traits class of Bfs class. |
38 | 37 |
|
39 | 38 |
///Default traits class of Bfs class. |
40 | 39 |
///\tparam GR Digraph type. |
41 | 40 |
template<class GR> |
42 | 41 |
struct BfsDefaultTraits |
43 | 42 |
{ |
44 | 43 |
///The digraph type the algorithm runs on. |
45 | 44 |
typedef GR Digraph; |
46 | 45 |
///\brief The type of the map that stores the last |
47 | 46 |
///arcs of the shortest paths. |
48 | 47 |
/// |
49 | 48 |
///The type of the map that stores the last |
50 | 49 |
///arcs of the shortest paths. |
51 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
52 | 51 |
/// |
53 | 52 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
54 | 53 |
///Instantiates a PredMap. |
55 | 54 |
|
56 | 55 |
///This function instantiates a \ref PredMap. |
57 | 56 |
///\param G is the digraph, to which we would like to define the PredMap. |
58 | 57 |
///\todo The digraph alone may be insufficient to initialize |
59 | 58 |
static PredMap *createPredMap(const GR &G) |
60 | 59 |
{ |
61 | 60 |
return new PredMap(G); |
62 | 61 |
} |
63 | 62 |
///The type of the map that indicates which nodes are processed. |
64 | 63 |
|
65 | 64 |
///The type of the map that indicates which nodes are processed. |
66 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
67 | 66 |
///\todo named parameter to set this type, function to read and write. |
68 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
69 | 68 |
///Instantiates a ProcessedMap. |
70 | 69 |
|
71 | 70 |
///This function instantiates a \ref ProcessedMap. |
72 | 71 |
///\param g is the digraph, to which |
73 | 72 |
///we would like to define the \ref ProcessedMap |
74 | 73 |
#ifdef DOXYGEN |
75 | 74 |
static ProcessedMap *createProcessedMap(const GR &g) |
76 | 75 |
#else |
77 | 76 |
static ProcessedMap *createProcessedMap(const GR &) |
78 | 77 |
#endif |
79 | 78 |
{ |
80 | 79 |
return new ProcessedMap(); |
81 | 80 |
} |
82 | 81 |
///The type of the map that indicates which nodes are reached. |
83 | 82 |
|
84 | 83 |
///The type of the map that indicates which nodes are reached. |
85 | 84 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
86 | 85 |
///\todo named parameter to set this type, function to read and write. |
87 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
88 | 87 |
///Instantiates a ReachedMap. |
89 | 88 |
|
90 | 89 |
///This function instantiates a \ref ReachedMap. |
91 | 90 |
///\param G is the digraph, to which |
92 | 91 |
///we would like to define the \ref ReachedMap. |
93 | 92 |
static ReachedMap *createReachedMap(const GR &G) |
94 | 93 |
{ |
95 | 94 |
return new ReachedMap(G); |
96 | 95 |
} |
97 | 96 |
///The type of the map that stores the dists of the nodes. |
98 | 97 |
|
99 | 98 |
///The type of the map that stores the dists of the nodes. |
100 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
101 | 100 |
/// |
102 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
103 | 102 |
///Instantiates a DistMap. |
104 | 103 |
|
105 | 104 |
///This function instantiates a \ref DistMap. |
106 | 105 |
///\param G is the digraph, to which we would like to define |
107 | 106 |
///the \ref DistMap |
108 | 107 |
static DistMap *createDistMap(const GR &G) |
109 | 108 |
{ |
110 | 109 |
return new DistMap(G); |
111 | 110 |
} |
112 | 111 |
}; |
113 | 112 |
|
114 | 113 |
///%BFS algorithm class. |
115 | 114 |
|
116 | 115 |
///\ingroup search |
117 | 116 |
///This class provides an efficient implementation of the %BFS algorithm. |
118 | 117 |
/// |
119 | 118 |
///\tparam GR The digraph type the algorithm runs on. The default value is |
120 | 119 |
///\ref ListDigraph. The value of GR is not used directly by Bfs, it |
121 | 120 |
///is only passed to \ref BfsDefaultTraits. |
122 | 121 |
///\tparam TR Traits class to set various data types used by the algorithm. |
123 | 122 |
///The default traits class is |
124 | 123 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
125 | 124 |
///See \ref BfsDefaultTraits for the documentation of |
126 | 125 |
///a Bfs traits class. |
127 | 126 |
|
128 | 127 |
#ifdef DOXYGEN |
129 | 128 |
template <typename GR, |
130 | 129 |
typename TR> |
131 | 130 |
#else |
132 | 131 |
template <typename GR=ListDigraph, |
133 | 132 |
typename TR=BfsDefaultTraits<GR> > |
134 | 133 |
#endif |
135 | 134 |
class Bfs { |
136 | 135 |
public: |
137 | 136 |
/** |
138 | 137 |
* \brief \ref Exception for uninitialized parameters. |
139 | 138 |
* |
140 | 139 |
* This error represents problems in the initialization |
141 | 140 |
* of the parameters of the algorithms. |
142 | 141 |
*/ |
143 | 142 |
class UninitializedParameter : public lemon::UninitializedParameter { |
144 | 143 |
public: |
145 | 144 |
virtual const char* what() const throw() { |
146 | 145 |
return "lemon::Bfs::UninitializedParameter"; |
147 | 146 |
} |
148 | 147 |
}; |
149 | 148 |
|
150 | 149 |
typedef TR Traits; |
151 | 150 |
///The type of the underlying digraph. |
152 | 151 |
typedef typename TR::Digraph Digraph; |
153 | 152 |
|
154 | 153 |
///\brief The type of the map that stores the last |
155 | 154 |
///arcs of the shortest paths. |
156 | 155 |
typedef typename TR::PredMap PredMap; |
157 | 156 |
///The type of the map indicating which nodes are reached. |
158 | 157 |
typedef typename TR::ReachedMap ReachedMap; |
159 | 158 |
///The type of the map indicating which nodes are processed. |
160 | 159 |
typedef typename TR::ProcessedMap ProcessedMap; |
161 | 160 |
///The type of the map that stores the dists of the nodes. |
162 | 161 |
typedef typename TR::DistMap DistMap; |
163 | 162 |
private: |
164 | 163 |
|
165 | 164 |
typedef typename Digraph::Node Node; |
166 | 165 |
typedef typename Digraph::NodeIt NodeIt; |
167 | 166 |
typedef typename Digraph::Arc Arc; |
168 | 167 |
typedef typename Digraph::OutArcIt OutArcIt; |
169 | 168 |
|
170 | 169 |
/// Pointer to the underlying digraph. |
171 | 170 |
const Digraph *G; |
172 | 171 |
///Pointer to the map of predecessors arcs. |
173 | 172 |
PredMap *_pred; |
174 | 173 |
///Indicates if \ref _pred is locally allocated (\c true) or not. |
175 | 174 |
bool local_pred; |
176 | 175 |
///Pointer to the map of distances. |
177 | 176 |
DistMap *_dist; |
178 | 177 |
///Indicates if \ref _dist is locally allocated (\c true) or not. |
179 | 178 |
bool local_dist; |
180 | 179 |
///Pointer to the map of reached status of the nodes. |
181 | 180 |
ReachedMap *_reached; |
182 | 181 |
///Indicates if \ref _reached is locally allocated (\c true) or not. |
183 | 182 |
bool local_reached; |
184 | 183 |
///Pointer to the map of processed status of the nodes. |
185 | 184 |
ProcessedMap *_processed; |
186 | 185 |
///Indicates if \ref _processed is locally allocated (\c true) or not. |
187 | 186 |
bool local_processed; |
188 | 187 |
|
189 | 188 |
std::vector<typename Digraph::Node> _queue; |
190 | 189 |
int _queue_head,_queue_tail,_queue_next_dist; |
191 | 190 |
int _curr_dist; |
192 | 191 |
|
193 | 192 |
///Creates the maps if necessary. |
194 | 193 |
|
195 | 194 |
///\todo Better memory allocation (instead of new). |
196 | 195 |
void create_maps() |
197 | 196 |
{ |
198 | 197 |
if(!_pred) { |
199 | 198 |
local_pred = true; |
200 | 199 |
_pred = Traits::createPredMap(*G); |
201 | 200 |
} |
202 | 201 |
if(!_dist) { |
203 | 202 |
local_dist = true; |
204 | 203 |
_dist = Traits::createDistMap(*G); |
205 | 204 |
} |
206 | 205 |
if(!_reached) { |
207 | 206 |
local_reached = true; |
208 | 207 |
_reached = Traits::createReachedMap(*G); |
209 | 208 |
} |
210 | 209 |
if(!_processed) { |
211 | 210 |
local_processed = true; |
212 | 211 |
_processed = Traits::createProcessedMap(*G); |
213 | 212 |
} |
214 | 213 |
} |
215 | 214 |
|
216 | 215 |
protected: |
217 | 216 |
|
218 | 217 |
Bfs() {} |
219 | 218 |
|
220 | 219 |
public: |
221 | 220 |
|
222 | 221 |
typedef Bfs Create; |
223 | 222 |
|
224 | 223 |
///\name Named template parameters |
225 | 224 |
|
226 | 225 |
///@{ |
227 | 226 |
|
228 | 227 |
template <class T> |
229 | 228 |
struct DefPredMapTraits : public Traits { |
230 | 229 |
typedef T PredMap; |
231 | 230 |
static PredMap *createPredMap(const Digraph &) |
232 | 231 |
{ |
233 | 232 |
throw UninitializedParameter(); |
234 | 233 |
} |
235 | 234 |
}; |
236 | 235 |
///\brief \ref named-templ-param "Named parameter" for setting |
237 | 236 |
///PredMap type |
238 | 237 |
/// |
239 | 238 |
///\ref named-templ-param "Named parameter" for setting PredMap type |
240 | 239 |
/// |
241 | 240 |
template <class T> |
242 | 241 |
struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > { |
243 | 242 |
typedef Bfs< Digraph, DefPredMapTraits<T> > Create; |
244 | 243 |
}; |
245 | 244 |
|
246 | 245 |
template <class T> |
247 | 246 |
struct DefDistMapTraits : public Traits { |
248 | 247 |
typedef T DistMap; |
249 | 248 |
static DistMap *createDistMap(const Digraph &) |
250 | 249 |
{ |
251 | 250 |
throw UninitializedParameter(); |
252 | 251 |
} |
253 | 252 |
}; |
254 | 253 |
///\brief \ref named-templ-param "Named parameter" for setting |
255 | 254 |
///DistMap type |
256 | 255 |
/// |
257 | 256 |
///\ref named-templ-param "Named parameter" for setting DistMap type |
258 | 257 |
/// |
259 | 258 |
template <class T> |
260 | 259 |
struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > { |
261 | 260 |
typedef Bfs< Digraph, DefDistMapTraits<T> > Create; |
262 | 261 |
}; |
263 | 262 |
|
264 | 263 |
template <class T> |
265 | 264 |
struct DefReachedMapTraits : public Traits { |
266 | 265 |
typedef T ReachedMap; |
267 | 266 |
static ReachedMap *createReachedMap(const Digraph &) |
268 | 267 |
{ |
269 | 268 |
throw UninitializedParameter(); |
270 | 269 |
} |
271 | 270 |
}; |
272 | 271 |
///\brief \ref named-templ-param "Named parameter" for setting |
273 | 272 |
///ReachedMap type |
274 | 273 |
/// |
275 | 274 |
///\ref named-templ-param "Named parameter" for setting ReachedMap type |
276 | 275 |
/// |
277 | 276 |
template <class T> |
278 | 277 |
struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > { |
279 | 278 |
typedef Bfs< Digraph, DefReachedMapTraits<T> > Create; |
280 | 279 |
}; |
281 | 280 |
|
282 | 281 |
template <class T> |
283 | 282 |
struct DefProcessedMapTraits : public Traits { |
284 | 283 |
typedef T ProcessedMap; |
285 | 284 |
static ProcessedMap *createProcessedMap(const Digraph &) |
286 | 285 |
{ |
287 | 286 |
throw UninitializedParameter(); |
288 | 287 |
} |
289 | 288 |
}; |
290 | 289 |
///\brief \ref named-templ-param "Named parameter" for setting |
291 | 290 |
///ProcessedMap type |
292 | 291 |
/// |
293 | 292 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
294 | 293 |
/// |
295 | 294 |
template <class T> |
296 | 295 |
struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > { |
297 | 296 |
typedef Bfs< Digraph, DefProcessedMapTraits<T> > Create; |
298 | 297 |
}; |
299 | 298 |
|
300 | 299 |
struct DefDigraphProcessedMapTraits : public Traits { |
301 | 300 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
302 | 301 |
static ProcessedMap *createProcessedMap(const Digraph &G) |
303 | 302 |
{ |
304 | 303 |
return new ProcessedMap(G); |
305 | 304 |
} |
306 | 305 |
}; |
307 | 306 |
///\brief \ref named-templ-param "Named parameter" |
308 | 307 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
309 | 308 |
/// |
310 | 309 |
///\ref named-templ-param "Named parameter" |
311 | 310 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
312 | 311 |
///If you don't set it explicitly, it will be automatically allocated. |
313 | 312 |
template <class T> |
314 | 313 |
struct DefProcessedMapToBeDefaultMap : |
315 | 314 |
public Bfs< Digraph, DefDigraphProcessedMapTraits> { |
316 | 315 |
typedef Bfs< Digraph, DefDigraphProcessedMapTraits> Create; |
317 | 316 |
}; |
318 | 317 |
|
319 | 318 |
///@} |
320 | 319 |
|
321 | 320 |
public: |
322 | 321 |
|
323 | 322 |
///Constructor. |
324 | 323 |
|
325 | 324 |
///\param _G the digraph the algorithm will run on. |
326 | 325 |
/// |
327 | 326 |
Bfs(const Digraph& _G) : |
328 | 327 |
G(&_G), |
329 | 328 |
_pred(NULL), local_pred(false), |
330 | 329 |
_dist(NULL), local_dist(false), |
331 | 330 |
_reached(NULL), local_reached(false), |
332 | 331 |
_processed(NULL), local_processed(false) |
333 | 332 |
{ } |
334 | 333 |
|
335 | 334 |
///Destructor. |
336 | 335 |
~Bfs() |
337 | 336 |
{ |
338 | 337 |
if(local_pred) delete _pred; |
339 | 338 |
if(local_dist) delete _dist; |
340 | 339 |
if(local_reached) delete _reached; |
341 | 340 |
if(local_processed) delete _processed; |
342 | 341 |
} |
343 | 342 |
|
344 | 343 |
///Sets the map storing the predecessor arcs. |
345 | 344 |
|
346 | 345 |
///Sets the map storing the predecessor arcs. |
347 | 346 |
///If you don't use this function before calling \ref run(), |
348 | 347 |
///it will allocate one. The destructor deallocates this |
349 | 348 |
///automatically allocated map, of course. |
350 | 349 |
///\return <tt> (*this) </tt> |
351 | 350 |
Bfs &predMap(PredMap &m) |
352 | 351 |
{ |
353 | 352 |
if(local_pred) { |
354 | 353 |
delete _pred; |
355 | 354 |
local_pred=false; |
356 | 355 |
} |
357 | 356 |
_pred = &m; |
358 | 357 |
return *this; |
359 | 358 |
} |
360 | 359 |
|
361 | 360 |
///Sets the map indicating the reached nodes. |
362 | 361 |
|
363 | 362 |
///Sets the map indicating the reached nodes. |
364 | 363 |
///If you don't use this function before calling \ref run(), |
365 | 364 |
///it will allocate one. The destructor deallocates this |
366 | 365 |
///automatically allocated map, of course. |
367 | 366 |
///\return <tt> (*this) </tt> |
368 | 367 |
Bfs &reachedMap(ReachedMap &m) |
369 | 368 |
{ |
370 | 369 |
if(local_reached) { |
371 | 370 |
delete _reached; |
372 | 371 |
local_reached=false; |
373 | 372 |
} |
374 | 373 |
_reached = &m; |
375 | 374 |
return *this; |
376 | 375 |
} |
377 | 376 |
|
378 | 377 |
///Sets the map indicating the processed nodes. |
379 | 378 |
|
380 | 379 |
///Sets the map indicating the processed nodes. |
381 | 380 |
///If you don't use this function before calling \ref run(), |
382 | 381 |
///it will allocate one. The destructor deallocates this |
383 | 382 |
///automatically allocated map, of course. |
384 | 383 |
///\return <tt> (*this) </tt> |
385 | 384 |
Bfs &processedMap(ProcessedMap &m) |
386 | 385 |
{ |
387 | 386 |
if(local_processed) { |
388 | 387 |
delete _processed; |
389 | 388 |
local_processed=false; |
390 | 389 |
} |
391 | 390 |
_processed = &m; |
392 | 391 |
return *this; |
393 | 392 |
} |
394 | 393 |
|
395 | 394 |
///Sets the map storing the distances calculated by the algorithm. |
396 | 395 |
|
397 | 396 |
///Sets the map storing the distances calculated by the algorithm. |
398 | 397 |
///If you don't use this function before calling \ref run(), |
399 | 398 |
///it will allocate one. The destructor deallocates this |
400 | 399 |
///automatically allocated map, of course. |
401 | 400 |
///\return <tt> (*this) </tt> |
402 | 401 |
Bfs &distMap(DistMap &m) |
403 | 402 |
{ |
404 | 403 |
if(local_dist) { |
405 | 404 |
delete _dist; |
406 | 405 |
local_dist=false; |
407 | 406 |
} |
408 | 407 |
_dist = &m; |
409 | 408 |
return *this; |
410 | 409 |
} |
411 | 410 |
|
412 | 411 |
public: |
413 | 412 |
///\name Execution control |
414 | 413 |
///The simplest way to execute the algorithm is to use |
415 | 414 |
///one of the member functions called \c run(...). |
416 | 415 |
///\n |
417 | 416 |
///If you need more control on the execution, |
418 | 417 |
///first you must call \ref init(), then you can add several source nodes |
419 | 418 |
///with \ref addSource(). |
420 | 419 |
///Finally \ref start() will perform the actual path |
421 | 420 |
///computation. |
422 | 421 |
|
423 | 422 |
///@{ |
424 | 423 |
|
425 | 424 |
///\brief Initializes the internal data structures. |
426 | 425 |
/// |
427 | 426 |
///Initializes the internal data structures. |
428 | 427 |
/// |
429 | 428 |
void init() |
430 | 429 |
{ |
431 | 430 |
create_maps(); |
432 | 431 |
_queue.resize(countNodes(*G)); |
433 | 432 |
_queue_head=_queue_tail=0; |
434 | 433 |
_curr_dist=1; |
435 | 434 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
436 | 435 |
_pred->set(u,INVALID); |
437 | 436 |
_reached->set(u,false); |
438 | 437 |
_processed->set(u,false); |
439 | 438 |
} |
440 | 439 |
} |
441 | 440 |
|
442 | 441 |
///Adds a new source node. |
443 | 442 |
|
444 | 443 |
///Adds a new source node to the set of nodes to be processed. |
445 | 444 |
/// |
446 | 445 |
void addSource(Node s) |
447 | 446 |
{ |
448 | 447 |
if(!(*_reached)[s]) |
449 | 448 |
{ |
450 | 449 |
_reached->set(s,true); |
451 | 450 |
_pred->set(s,INVALID); |
452 | 451 |
_dist->set(s,0); |
453 | 452 |
_queue[_queue_head++]=s; |
454 | 453 |
_queue_next_dist=_queue_head; |
455 | 454 |
} |
456 | 455 |
} |
457 | 456 |
|
458 | 457 |
///Processes the next node. |
459 | 458 |
|
460 | 459 |
///Processes the next node. |
461 | 460 |
/// |
462 | 461 |
///\return The processed node. |
463 | 462 |
/// |
464 | 463 |
///\warning The queue must not be empty! |
465 | 464 |
Node processNextNode() |
466 | 465 |
{ |
467 | 466 |
if(_queue_tail==_queue_next_dist) { |
468 | 467 |
_curr_dist++; |
469 | 468 |
_queue_next_dist=_queue_head; |
470 | 469 |
} |
471 | 470 |
Node n=_queue[_queue_tail++]; |
472 | 471 |
_processed->set(n,true); |
473 | 472 |
Node m; |
474 | 473 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
475 | 474 |
if(!(*_reached)[m=G->target(e)]) { |
476 | 475 |
_queue[_queue_head++]=m; |
477 | 476 |
_reached->set(m,true); |
478 | 477 |
_pred->set(m,e); |
479 | 478 |
_dist->set(m,_curr_dist); |
480 | 479 |
} |
481 | 480 |
return n; |
482 | 481 |
} |
483 | 482 |
|
484 | 483 |
///Processes the next node. |
485 | 484 |
|
486 | 485 |
///Processes the next node. And checks that the given target node |
487 | 486 |
///is reached. If the target node is reachable from the processed |
488 | 487 |
///node then the reached parameter will be set true. The reached |
489 | 488 |
///parameter should be initially false. |
490 | 489 |
/// |
491 | 490 |
///\param target The target node. |
492 | 491 |
///\retval reach Indicates that the target node is reached. |
493 | 492 |
///\return The processed node. |
494 | 493 |
/// |
495 | 494 |
///\warning The queue must not be empty! |
496 | 495 |
Node processNextNode(Node target, bool& reach) |
497 | 496 |
{ |
498 | 497 |
if(_queue_tail==_queue_next_dist) { |
499 | 498 |
_curr_dist++; |
500 | 499 |
_queue_next_dist=_queue_head; |
501 | 500 |
} |
502 | 501 |
Node n=_queue[_queue_tail++]; |
503 | 502 |
_processed->set(n,true); |
504 | 503 |
Node m; |
505 | 504 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
506 | 505 |
if(!(*_reached)[m=G->target(e)]) { |
507 | 506 |
_queue[_queue_head++]=m; |
508 | 507 |
_reached->set(m,true); |
509 | 508 |
_pred->set(m,e); |
510 | 509 |
_dist->set(m,_curr_dist); |
511 | 510 |
reach = reach || (target == m); |
512 | 511 |
} |
513 | 512 |
return n; |
514 | 513 |
} |
515 | 514 |
|
516 | 515 |
///Processes the next node. |
517 | 516 |
|
518 | 517 |
///Processes the next node. And checks that at least one of |
519 | 518 |
///reached node has true value in the \c nm node map. If one node |
520 | 519 |
///with true value is reachable from the processed node then the |
521 | 520 |
///rnode parameter will be set to the first of such nodes. |
522 | 521 |
/// |
523 | 522 |
///\param nm The node map of possible targets. |
524 | 523 |
///\retval rnode The reached target node. |
525 | 524 |
///\return The processed node. |
526 | 525 |
/// |
527 | 526 |
///\warning The queue must not be empty! |
528 | 527 |
template<class NM> |
529 | 528 |
Node processNextNode(const NM& nm, Node& rnode) |
530 | 529 |
{ |
531 | 530 |
if(_queue_tail==_queue_next_dist) { |
532 | 531 |
_curr_dist++; |
533 | 532 |
_queue_next_dist=_queue_head; |
534 | 533 |
} |
535 | 534 |
Node n=_queue[_queue_tail++]; |
536 | 535 |
_processed->set(n,true); |
537 | 536 |
Node m; |
538 | 537 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
539 | 538 |
if(!(*_reached)[m=G->target(e)]) { |
540 | 539 |
_queue[_queue_head++]=m; |
541 | 540 |
_reached->set(m,true); |
542 | 541 |
_pred->set(m,e); |
543 | 542 |
_dist->set(m,_curr_dist); |
544 | 543 |
if (nm[m] && rnode == INVALID) rnode = m; |
545 | 544 |
} |
546 | 545 |
return n; |
547 | 546 |
} |
548 | 547 |
|
549 | 548 |
///Next node to be processed. |
550 | 549 |
|
551 | 550 |
///Next node to be processed. |
552 | 551 |
/// |
553 | 552 |
///\return The next node to be processed or INVALID if the queue is |
554 | 553 |
/// empty. |
555 | 554 |
Node nextNode() |
556 | 555 |
{ |
557 | 556 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
558 | 557 |
} |
559 | 558 |
|
560 | 559 |
///\brief Returns \c false if there are nodes |
561 | 560 |
///to be processed in the queue |
562 | 561 |
/// |
563 | 562 |
///Returns \c false if there are nodes |
564 | 563 |
///to be processed in the queue |
565 | 564 |
bool emptyQueue() { return _queue_tail==_queue_head; } |
566 | 565 |
///Returns the number of the nodes to be processed. |
567 | 566 |
|
568 | 567 |
///Returns the number of the nodes to be processed in the queue. |
569 | 568 |
int queueSize() { return _queue_head-_queue_tail; } |
570 | 569 |
|
571 | 570 |
///Executes the algorithm. |
572 | 571 |
|
573 | 572 |
///Executes the algorithm. |
574 | 573 |
/// |
575 | 574 |
///\pre init() must be called and at least one node should be added |
576 | 575 |
///with addSource() before using this function. |
577 | 576 |
/// |
578 | 577 |
///This method runs the %BFS algorithm from the root node(s) |
579 | 578 |
///in order to |
580 | 579 |
///compute the |
581 | 580 |
///shortest path to each node. The algorithm computes |
582 | 581 |
///- The shortest path tree. |
583 | 582 |
///- The distance of each node from the root(s). |
584 | 583 |
void start() |
585 | 584 |
{ |
586 | 585 |
while ( !emptyQueue() ) processNextNode(); |
587 | 586 |
} |
588 | 587 |
|
589 | 588 |
///Executes the algorithm until \c dest is reached. |
590 | 589 |
|
591 | 590 |
///Executes the algorithm until \c dest is reached. |
592 | 591 |
/// |
593 | 592 |
///\pre init() must be called and at least one node should be added |
594 | 593 |
///with addSource() before using this function. |
595 | 594 |
/// |
596 | 595 |
///This method runs the %BFS algorithm from the root node(s) |
597 | 596 |
///in order to compute the shortest path to \c dest. |
598 | 597 |
///The algorithm computes |
599 | 598 |
///- The shortest path to \c dest. |
600 | 599 |
///- The distance of \c dest from the root(s). |
601 | 600 |
void start(Node dest) |
602 | 601 |
{ |
603 | 602 |
bool reach = false; |
604 | 603 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
605 | 604 |
} |
606 | 605 |
|
607 | 606 |
///Executes the algorithm until a condition is met. |
608 | 607 |
|
609 | 608 |
///Executes the algorithm until a condition is met. |
610 | 609 |
/// |
611 | 610 |
///\pre init() must be called and at least one node should be added |
612 | 611 |
///with addSource() before using this function. |
613 | 612 |
/// |
614 | 613 |
///\param nm must be a bool (or convertible) node map. The |
615 | 614 |
///algorithm will stop when it reaches a node \c v with |
616 | 615 |
/// <tt>nm[v]</tt> true. |
617 | 616 |
/// |
618 | 617 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
619 | 618 |
///\c INVALID if no such node was found. |
620 | 619 |
template<class NM> |
621 | 620 |
Node start(const NM &nm) |
622 | 621 |
{ |
623 | 622 |
Node rnode = INVALID; |
624 | 623 |
while ( !emptyQueue() && rnode == INVALID ) { |
625 | 624 |
processNextNode(nm, rnode); |
626 | 625 |
} |
627 | 626 |
return rnode; |
628 | 627 |
} |
629 | 628 |
|
630 | 629 |
///Runs %BFS algorithm from node \c s. |
631 | 630 |
|
632 | 631 |
///This method runs the %BFS algorithm from a root node \c s |
633 | 632 |
///in order to |
634 | 633 |
///compute the |
635 | 634 |
///shortest path to each node. The algorithm computes |
636 | 635 |
///- The shortest path tree. |
637 | 636 |
///- The distance of each node from the root. |
638 | 637 |
/// |
639 | 638 |
///\note b.run(s) is just a shortcut of the following code. |
640 | 639 |
///\code |
641 | 640 |
/// b.init(); |
642 | 641 |
/// b.addSource(s); |
643 | 642 |
/// b.start(); |
644 | 643 |
///\endcode |
645 | 644 |
void run(Node s) { |
646 | 645 |
init(); |
647 | 646 |
addSource(s); |
648 | 647 |
start(); |
649 | 648 |
} |
650 | 649 |
|
651 | 650 |
///Finds the shortest path between \c s and \c t. |
652 | 651 |
|
653 | 652 |
///Finds the shortest path between \c s and \c t. |
654 | 653 |
/// |
655 | 654 |
///\return The length of the shortest s---t path if there exists one, |
656 | 655 |
///0 otherwise. |
657 | 656 |
///\note Apart from the return value, b.run(s) is |
658 | 657 |
///just a shortcut of the following code. |
659 | 658 |
///\code |
660 | 659 |
/// b.init(); |
661 | 660 |
/// b.addSource(s); |
662 | 661 |
/// b.start(t); |
663 | 662 |
///\endcode |
664 | 663 |
int run(Node s,Node t) { |
665 | 664 |
init(); |
666 | 665 |
addSource(s); |
667 | 666 |
start(t); |
668 | 667 |
return reached(t) ? _curr_dist : 0; |
669 | 668 |
} |
670 | 669 |
|
671 | 670 |
///@} |
672 | 671 |
|
673 | 672 |
///\name Query Functions |
674 | 673 |
///The result of the %BFS algorithm can be obtained using these |
675 | 674 |
///functions.\n |
676 | 675 |
///Before the use of these functions, |
677 | 676 |
///either run() or start() must be calleb. |
678 | 677 |
|
679 | 678 |
///@{ |
680 | 679 |
|
681 | 680 |
typedef PredMapPath<Digraph, PredMap> Path; |
682 | 681 |
|
683 | 682 |
///Gives back the shortest path. |
684 | 683 |
|
685 | 684 |
///Gives back the shortest path. |
686 | 685 |
///\pre The \c t should be reachable from the source. |
687 | 686 |
Path path(Node t) |
688 | 687 |
{ |
689 | 688 |
return Path(*G, *_pred, t); |
690 | 689 |
} |
691 | 690 |
|
692 | 691 |
///The distance of a node from the root(s). |
693 | 692 |
|
694 | 693 |
///Returns the distance of a node from the root(s). |
695 | 694 |
///\pre \ref run() must be called before using this function. |
696 | 695 |
///\warning If node \c v in unreachable from the root(s) the return value |
697 | 696 |
///of this function is undefined. |
698 | 697 |
int dist(Node v) const { return (*_dist)[v]; } |
699 | 698 |
|
700 | 699 |
///Returns the 'previous arc' of the shortest path tree. |
701 | 700 |
|
702 | 701 |
///For a node \c v it returns the 'previous arc' |
703 | 702 |
///of the shortest path tree, |
704 | 703 |
///i.e. it returns the last arc of a shortest path from the root(s) to \c |
705 | 704 |
///v. It is \ref INVALID |
706 | 705 |
///if \c v is unreachable from the root(s) or \c v is a root. The |
707 | 706 |
///shortest path tree used here is equal to the shortest path tree used in |
708 | 707 |
///\ref predNode(). |
709 | 708 |
///\pre Either \ref run() or \ref start() must be called before using |
710 | 709 |
///this function. |
711 | 710 |
Arc predArc(Node v) const { return (*_pred)[v];} |
712 | 711 |
|
713 | 712 |
///Returns the 'previous node' of the shortest path tree. |
714 | 713 |
|
715 | 714 |
///For a node \c v it returns the 'previous node' |
716 | 715 |
///of the shortest path tree, |
717 | 716 |
///i.e. it returns the last but one node from a shortest path from the |
718 | 717 |
///root(a) to \c /v. |
719 | 718 |
///It is INVALID if \c v is unreachable from the root(s) or |
720 | 719 |
///if \c v itself a root. |
721 | 720 |
///The shortest path tree used here is equal to the shortest path |
722 | 721 |
///tree used in \ref predArc(). |
723 | 722 |
///\pre Either \ref run() or \ref start() must be called before |
724 | 723 |
///using this function. |
725 | 724 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
726 | 725 |
G->source((*_pred)[v]); } |
727 | 726 |
|
728 | 727 |
///Returns a reference to the NodeMap of distances. |
729 | 728 |
|
730 | 729 |
///Returns a reference to the NodeMap of distances. |
731 | 730 |
///\pre Either \ref run() or \ref init() must |
732 | 731 |
///be called before using this function. |
733 | 732 |
const DistMap &distMap() const { return *_dist;} |
734 | 733 |
|
735 | 734 |
///Returns a reference to the shortest path tree map. |
736 | 735 |
|
737 | 736 |
///Returns a reference to the NodeMap of the arcs of the |
738 | 737 |
///shortest path tree. |
739 | 738 |
///\pre Either \ref run() or \ref init() |
740 | 739 |
///must be called before using this function. |
741 | 740 |
const PredMap &predMap() const { return *_pred;} |
742 | 741 |
|
743 | 742 |
///Checks if a node is reachable from the root. |
744 | 743 |
|
745 | 744 |
///Returns \c true if \c v is reachable from the root. |
746 | 745 |
///\warning The source nodes are indicated as unreached. |
747 | 746 |
///\pre Either \ref run() or \ref start() |
748 | 747 |
///must be called before using this function. |
749 | 748 |
/// |
750 | 749 |
bool reached(Node v) { return (*_reached)[v]; } |
751 | 750 |
|
752 | 751 |
///@} |
753 | 752 |
}; |
754 | 753 |
|
755 | 754 |
///Default traits class of Bfs function. |
756 | 755 |
|
757 | 756 |
///Default traits class of Bfs function. |
758 | 757 |
///\tparam GR Digraph type. |
759 | 758 |
template<class GR> |
760 | 759 |
struct BfsWizardDefaultTraits |
761 | 760 |
{ |
762 | 761 |
///The digraph type the algorithm runs on. |
763 | 762 |
typedef GR Digraph; |
764 | 763 |
///\brief The type of the map that stores the last |
765 | 764 |
///arcs of the shortest paths. |
766 | 765 |
/// |
767 | 766 |
///The type of the map that stores the last |
768 | 767 |
///arcs of the shortest paths. |
769 | 768 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
770 | 769 |
/// |
771 | 770 |
typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap; |
772 | 771 |
///Instantiates a PredMap. |
773 | 772 |
|
774 | 773 |
///This function instantiates a \ref PredMap. |
775 | 774 |
///\param g is the digraph, to which we would like to define the PredMap. |
776 | 775 |
///\todo The digraph alone may be insufficient to initialize |
777 | 776 |
#ifdef DOXYGEN |
778 | 777 |
static PredMap *createPredMap(const GR &g) |
779 | 778 |
#else |
780 | 779 |
static PredMap *createPredMap(const GR &) |
781 | 780 |
#endif |
782 | 781 |
{ |
783 | 782 |
return new PredMap(); |
784 | 783 |
} |
785 | 784 |
|
786 | 785 |
///The type of the map that indicates which nodes are processed. |
787 | 786 |
|
788 | 787 |
///The type of the map that indicates which nodes are processed. |
789 | 788 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
790 | 789 |
///\todo named parameter to set this type, function to read and write. |
791 | 790 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
792 | 791 |
///Instantiates a ProcessedMap. |
793 | 792 |
|
794 | 793 |
///This function instantiates a \ref ProcessedMap. |
795 | 794 |
///\param g is the digraph, to which |
796 | 795 |
///we would like to define the \ref ProcessedMap |
797 | 796 |
#ifdef DOXYGEN |
798 | 797 |
static ProcessedMap *createProcessedMap(const GR &g) |
799 | 798 |
#else |
800 | 799 |
static ProcessedMap *createProcessedMap(const GR &) |
801 | 800 |
#endif |
802 | 801 |
{ |
803 | 802 |
return new ProcessedMap(); |
804 | 803 |
} |
805 | 804 |
///The type of the map that indicates which nodes are reached. |
806 | 805 |
|
807 | 806 |
///The type of the map that indicates which nodes are reached. |
808 | 807 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
809 | 808 |
///\todo named parameter to set this type, function to read and write. |
810 | 809 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
811 | 810 |
///Instantiates a ReachedMap. |
812 | 811 |
|
813 | 812 |
///This function instantiates a \ref ReachedMap. |
814 | 813 |
///\param G is the digraph, to which |
815 | 814 |
///we would like to define the \ref ReachedMap. |
816 | 815 |
static ReachedMap *createReachedMap(const GR &G) |
817 | 816 |
{ |
818 | 817 |
return new ReachedMap(G); |
819 | 818 |
} |
820 | 819 |
///The type of the map that stores the dists of the nodes. |
821 | 820 |
|
822 | 821 |
///The type of the map that stores the dists of the nodes. |
823 | 822 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
824 | 823 |
/// |
825 | 824 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
826 | 825 |
///Instantiates a DistMap. |
827 | 826 |
|
828 | 827 |
///This function instantiates a \ref DistMap. |
829 | 828 |
///\param g is the digraph, to which we would like to define |
830 | 829 |
///the \ref DistMap |
831 | 830 |
#ifdef DOXYGEN |
832 | 831 |
static DistMap *createDistMap(const GR &g) |
833 | 832 |
#else |
834 | 833 |
static DistMap *createDistMap(const GR &) |
835 | 834 |
#endif |
836 | 835 |
{ |
837 | 836 |
return new DistMap(); |
838 | 837 |
} |
839 | 838 |
}; |
840 | 839 |
|
841 | 840 |
/// Default traits used by \ref BfsWizard |
842 | 841 |
|
843 | 842 |
/// To make it easier to use Bfs algorithm |
844 | 843 |
///we have created a wizard class. |
845 | 844 |
/// This \ref BfsWizard class needs default traits, |
846 | 845 |
///as well as the \ref Bfs class. |
847 | 846 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
848 | 847 |
/// \ref BfsWizard class. |
849 | 848 |
template<class GR> |
850 | 849 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
851 | 850 |
{ |
852 | 851 |
|
853 | 852 |
typedef BfsWizardDefaultTraits<GR> Base; |
854 | 853 |
protected: |
855 | 854 |
/// Type of the nodes in the digraph. |
856 | 855 |
typedef typename Base::Digraph::Node Node; |
857 | 856 |
|
858 | 857 |
/// Pointer to the underlying digraph. |
859 | 858 |
void *_g; |
860 | 859 |
///Pointer to the map of reached nodes. |
861 | 860 |
void *_reached; |
862 | 861 |
///Pointer to the map of processed nodes. |
863 | 862 |
void *_processed; |
864 | 863 |
///Pointer to the map of predecessors arcs. |
865 | 864 |
void *_pred; |
866 | 865 |
///Pointer to the map of distances. |
867 | 866 |
void *_dist; |
868 | 867 |
///Pointer to the source node. |
869 | 868 |
Node _source; |
870 | 869 |
|
871 | 870 |
public: |
872 | 871 |
/// Constructor. |
873 | 872 |
|
874 | 873 |
/// This constructor does not require parameters, therefore it initiates |
875 | 874 |
/// all of the attributes to default values (0, INVALID). |
876 | 875 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
877 | 876 |
_dist(0), _source(INVALID) {} |
878 | 877 |
|
879 | 878 |
/// Constructor. |
880 | 879 |
|
881 | 880 |
/// This constructor requires some parameters, |
882 | 881 |
/// listed in the parameters list. |
883 | 882 |
/// Others are initiated to 0. |
884 | 883 |
/// \param g is the initial value of \ref _g |
885 | 884 |
/// \param s is the initial value of \ref _source |
886 | 885 |
BfsWizardBase(const GR &g, Node s=INVALID) : |
887 | 886 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
888 | 887 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
889 | 888 |
|
890 | 889 |
}; |
891 | 890 |
|
892 | 891 |
/// A class to make the usage of Bfs algorithm easier |
893 | 892 |
|
894 | 893 |
/// This class is created to make it easier to use Bfs algorithm. |
895 | 894 |
/// It uses the functions and features of the plain \ref Bfs, |
896 | 895 |
/// but it is much simpler to use it. |
897 | 896 |
/// |
898 | 897 |
/// Simplicity means that the way to change the types defined |
899 | 898 |
/// in the traits class is based on functions that returns the new class |
900 | 899 |
/// and not on templatable built-in classes. |
901 | 900 |
/// When using the plain \ref Bfs |
902 | 901 |
/// the new class with the modified type comes from |
903 | 902 |
/// the original class by using the :: |
904 | 903 |
/// operator. In the case of \ref BfsWizard only |
905 | 904 |
/// a function have to be called and it will |
906 | 905 |
/// return the needed class. |
907 | 906 |
/// |
908 | 907 |
/// It does not have own \ref run method. When its \ref run method is called |
909 | 908 |
/// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run |
910 | 909 |
/// method of it. |
911 | 910 |
template<class TR> |
912 | 911 |
class BfsWizard : public TR |
913 | 912 |
{ |
914 | 913 |
typedef TR Base; |
915 | 914 |
|
916 | 915 |
///The type of the underlying digraph. |
917 | 916 |
typedef typename TR::Digraph Digraph; |
918 | 917 |
//\e |
919 | 918 |
typedef typename Digraph::Node Node; |
920 | 919 |
//\e |
921 | 920 |
typedef typename Digraph::NodeIt NodeIt; |
922 | 921 |
//\e |
923 | 922 |
typedef typename Digraph::Arc Arc; |
924 | 923 |
//\e |
925 | 924 |
typedef typename Digraph::OutArcIt OutArcIt; |
926 | 925 |
|
927 | 926 |
///\brief The type of the map that stores |
928 | 927 |
///the reached nodes |
929 | 928 |
typedef typename TR::ReachedMap ReachedMap; |
930 | 929 |
///\brief The type of the map that stores |
931 | 930 |
///the processed nodes |
932 | 931 |
typedef typename TR::ProcessedMap ProcessedMap; |
933 | 932 |
///\brief The type of the map that stores the last |
934 | 933 |
///arcs of the shortest paths. |
935 | 934 |
typedef typename TR::PredMap PredMap; |
936 | 935 |
///The type of the map that stores the dists of the nodes. |
937 | 936 |
typedef typename TR::DistMap DistMap; |
938 | 937 |
|
939 | 938 |
public: |
940 | 939 |
/// Constructor. |
941 | 940 |
BfsWizard() : TR() {} |
942 | 941 |
|
943 | 942 |
/// Constructor that requires parameters. |
944 | 943 |
|
945 | 944 |
/// Constructor that requires parameters. |
946 | 945 |
/// These parameters will be the default values for the traits class. |
947 | 946 |
BfsWizard(const Digraph &g, Node s=INVALID) : |
948 | 947 |
TR(g,s) {} |
949 | 948 |
|
950 | 949 |
///Copy constructor |
951 | 950 |
BfsWizard(const TR &b) : TR(b) {} |
952 | 951 |
|
953 | 952 |
~BfsWizard() {} |
954 | 953 |
|
955 | 954 |
///Runs Bfs algorithm from a given node. |
956 | 955 |
|
957 | 956 |
///Runs Bfs algorithm from a given node. |
958 | 957 |
///The node can be given by the \ref source function. |
959 | 958 |
void run() |
960 | 959 |
{ |
961 | 960 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
962 | 961 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
963 | 962 |
if(Base::_reached) |
964 | 963 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
965 | 964 |
if(Base::_processed) |
966 | 965 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
967 | 966 |
if(Base::_pred) |
968 | 967 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
969 | 968 |
if(Base::_dist) |
970 | 969 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
971 | 970 |
alg.run(Base::_source); |
972 | 971 |
} |
973 | 972 |
|
974 | 973 |
///Runs Bfs algorithm from the given node. |
975 | 974 |
|
976 | 975 |
///Runs Bfs algorithm from the given node. |
977 | 976 |
///\param s is the given source. |
978 | 977 |
void run(Node s) |
979 | 978 |
{ |
980 | 979 |
Base::_source=s; |
981 | 980 |
run(); |
982 | 981 |
} |
983 | 982 |
|
984 | 983 |
template<class T> |
985 | 984 |
struct DefPredMapBase : public Base { |
986 | 985 |
typedef T PredMap; |
987 | 986 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
988 | 987 |
DefPredMapBase(const TR &b) : TR(b) {} |
989 | 988 |
}; |
990 | 989 |
|
991 | 990 |
///\brief \ref named-templ-param "Named parameter" |
992 | 991 |
///function for setting PredMap |
993 | 992 |
/// |
994 | 993 |
/// \ref named-templ-param "Named parameter" |
995 | 994 |
///function for setting PredMap |
996 | 995 |
/// |
997 | 996 |
template<class T> |
998 | 997 |
BfsWizard<DefPredMapBase<T> > predMap(const T &t) |
999 | 998 |
{ |
1000 | 999 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1001 | 1000 |
return BfsWizard<DefPredMapBase<T> >(*this); |
1002 | 1001 |
} |
1003 | 1002 |
|
1004 | 1003 |
|
1005 | 1004 |
template<class T> |
1006 | 1005 |
struct DefReachedMapBase : public Base { |
1007 | 1006 |
typedef T ReachedMap; |
1008 | 1007 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1009 | 1008 |
DefReachedMapBase(const TR &b) : TR(b) {} |
1010 | 1009 |
}; |
1011 | 1010 |
|
1012 | 1011 |
///\brief \ref named-templ-param "Named parameter" |
1013 | 1012 |
///function for setting ReachedMap |
1014 | 1013 |
/// |
1015 | 1014 |
/// \ref named-templ-param "Named parameter" |
1016 | 1015 |
///function for setting ReachedMap |
1017 | 1016 |
/// |
1018 | 1017 |
template<class T> |
1019 | 1018 |
BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
1020 | 1019 |
{ |
1021 | 1020 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1022 | 1021 |
return BfsWizard<DefReachedMapBase<T> >(*this); |
1023 | 1022 |
} |
1024 | 1023 |
|
1025 | 1024 |
|
1026 | 1025 |
template<class T> |
1027 | 1026 |
struct DefProcessedMapBase : public Base { |
1028 | 1027 |
typedef T ProcessedMap; |
1029 | 1028 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1030 | 1029 |
DefProcessedMapBase(const TR &b) : TR(b) {} |
1031 | 1030 |
}; |
1032 | 1031 |
|
1033 | 1032 |
///\brief \ref named-templ-param "Named parameter" |
1034 | 1033 |
///function for setting ProcessedMap |
1035 | 1034 |
/// |
1036 | 1035 |
/// \ref named-templ-param "Named parameter" |
1037 | 1036 |
///function for setting ProcessedMap |
1038 | 1037 |
/// |
1039 | 1038 |
template<class T> |
1040 | 1039 |
BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
1041 | 1040 |
{ |
1042 | 1041 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1043 | 1042 |
return BfsWizard<DefProcessedMapBase<T> >(*this); |
1044 | 1043 |
} |
1045 | 1044 |
|
1046 | 1045 |
|
1047 | 1046 |
template<class T> |
1048 | 1047 |
struct DefDistMapBase : public Base { |
1049 | 1048 |
typedef T DistMap; |
1050 | 1049 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1051 | 1050 |
DefDistMapBase(const TR &b) : TR(b) {} |
1052 | 1051 |
}; |
1053 | 1052 |
|
1054 | 1053 |
///\brief \ref named-templ-param "Named parameter" |
1055 | 1054 |
///function for setting DistMap type |
1056 | 1055 |
/// |
1057 | 1056 |
/// \ref named-templ-param "Named parameter" |
1058 | 1057 |
///function for setting DistMap type |
1059 | 1058 |
/// |
1060 | 1059 |
template<class T> |
1061 | 1060 |
BfsWizard<DefDistMapBase<T> > distMap(const T &t) |
1062 | 1061 |
{ |
1063 | 1062 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1064 | 1063 |
return BfsWizard<DefDistMapBase<T> >(*this); |
1065 | 1064 |
} |
1066 | 1065 |
|
1067 | 1066 |
/// Sets the source node, from which the Bfs algorithm runs. |
1068 | 1067 |
|
1069 | 1068 |
/// Sets the source node, from which the Bfs algorithm runs. |
1070 | 1069 |
/// \param s is the source node. |
1071 | 1070 |
BfsWizard<TR> &source(Node s) |
1072 | 1071 |
{ |
1073 | 1072 |
Base::_source=s; |
1074 | 1073 |
return *this; |
1075 | 1074 |
} |
1076 | 1075 |
|
1077 | 1076 |
}; |
1078 | 1077 |
|
1079 | 1078 |
///Function type interface for Bfs algorithm. |
1080 | 1079 |
|
1081 | 1080 |
/// \ingroup search |
1082 | 1081 |
///Function type interface for Bfs algorithm. |
1083 | 1082 |
/// |
1084 | 1083 |
///This function also has several |
1085 | 1084 |
///\ref named-templ-func-param "named parameters", |
1086 | 1085 |
///they are declared as the members of class \ref BfsWizard. |
1087 | 1086 |
///The following |
1088 | 1087 |
///example shows how to use these parameters. |
1089 | 1088 |
///\code |
1090 | 1089 |
/// bfs(g,source).predMap(preds).run(); |
1091 | 1090 |
///\endcode |
1092 | 1091 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
1093 | 1092 |
///to the end of the parameter list. |
1094 | 1093 |
///\sa BfsWizard |
1095 | 1094 |
///\sa Bfs |
1096 | 1095 |
template<class GR> |
1097 | 1096 |
BfsWizard<BfsWizardBase<GR> > |
1098 | 1097 |
bfs(const GR &g,typename GR::Node s=INVALID) |
1099 | 1098 |
{ |
1100 | 1099 |
return BfsWizard<BfsWizardBase<GR> >(g,s); |
1101 | 1100 |
} |
1102 | 1101 |
|
1103 | 1102 |
#ifdef DOXYGEN |
1104 | 1103 |
/// \brief Visitor class for bfs. |
1105 | 1104 |
/// |
1106 | 1105 |
/// This class defines the interface of the BfsVisit events, and |
1107 | 1106 |
/// it could be the base of a real Visitor class. |
1108 | 1107 |
template <typename _Digraph> |
1109 | 1108 |
struct BfsVisitor { |
1110 | 1109 |
typedef _Digraph Digraph; |
1111 | 1110 |
typedef typename Digraph::Arc Arc; |
1112 | 1111 |
typedef typename Digraph::Node Node; |
1113 | 1112 |
/// \brief Called when the arc reach a node. |
1114 | 1113 |
/// |
1115 | 1114 |
/// It is called when the bfs find an arc which target is not |
1116 | 1115 |
/// reached yet. |
1117 | 1116 |
void discover(const Arc& arc) {} |
1118 | 1117 |
/// \brief Called when the node reached first time. |
1119 | 1118 |
/// |
1120 | 1119 |
/// It is Called when the node reached first time. |
1121 | 1120 |
void reach(const Node& node) {} |
1122 | 1121 |
/// \brief Called when the arc examined but target of the arc |
1123 | 1122 |
/// already discovered. |
1124 | 1123 |
/// |
1125 | 1124 |
/// It called when the arc examined but the target of the arc |
1126 | 1125 |
/// already discovered. |
1127 | 1126 |
void examine(const Arc& arc) {} |
1128 | 1127 |
/// \brief Called for the source node of the bfs. |
1129 | 1128 |
/// |
1130 | 1129 |
/// It is called for the source node of the bfs. |
1131 | 1130 |
void start(const Node& node) {} |
1132 | 1131 |
/// \brief Called when the node processed. |
1133 | 1132 |
/// |
1134 | 1133 |
/// It is Called when the node processed. |
1135 | 1134 |
void process(const Node& node) {} |
1136 | 1135 |
}; |
1137 | 1136 |
#else |
1138 | 1137 |
template <typename _Digraph> |
1139 | 1138 |
struct BfsVisitor { |
1140 | 1139 |
typedef _Digraph Digraph; |
1141 | 1140 |
typedef typename Digraph::Arc Arc; |
1142 | 1141 |
typedef typename Digraph::Node Node; |
1143 | 1142 |
void discover(const Arc&) {} |
1144 | 1143 |
void reach(const Node&) {} |
1145 | 1144 |
void examine(const Arc&) {} |
1146 | 1145 |
void start(const Node&) {} |
1147 | 1146 |
void process(const Node&) {} |
1148 | 1147 |
|
1149 | 1148 |
template <typename _Visitor> |
1150 | 1149 |
struct Constraints { |
1151 | 1150 |
void constraints() { |
1152 | 1151 |
Arc arc; |
1153 | 1152 |
Node node; |
1154 | 1153 |
visitor.discover(arc); |
1155 | 1154 |
visitor.reach(node); |
1156 | 1155 |
visitor.examine(arc); |
1157 | 1156 |
visitor.start(node); |
1158 | 1157 |
visitor.process(node); |
1159 | 1158 |
} |
1160 | 1159 |
_Visitor& visitor; |
1161 | 1160 |
}; |
1162 | 1161 |
}; |
1163 | 1162 |
#endif |
1164 | 1163 |
|
1165 | 1164 |
/// \brief Default traits class of BfsVisit class. |
1166 | 1165 |
/// |
1167 | 1166 |
/// Default traits class of BfsVisit class. |
1168 | 1167 |
/// \tparam _Digraph Digraph type. |
1169 | 1168 |
template<class _Digraph> |
1170 | 1169 |
struct BfsVisitDefaultTraits { |
1171 | 1170 |
|
1172 | 1171 |
/// \brief The digraph type the algorithm runs on. |
1173 | 1172 |
typedef _Digraph Digraph; |
1174 | 1173 |
|
1175 | 1174 |
/// \brief The type of the map that indicates which nodes are reached. |
1176 | 1175 |
/// |
1177 | 1176 |
/// The type of the map that indicates which nodes are reached. |
1178 | 1177 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1179 | 1178 |
/// \todo named parameter to set this type, function to read and write. |
1180 | 1179 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1181 | 1180 |
|
1182 | 1181 |
/// \brief Instantiates a ReachedMap. |
1183 | 1182 |
/// |
1184 | 1183 |
/// This function instantiates a \ref ReachedMap. |
1185 | 1184 |
/// \param digraph is the digraph, to which |
1186 | 1185 |
/// we would like to define the \ref ReachedMap. |
1187 | 1186 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1188 | 1187 |
return new ReachedMap(digraph); |
1189 | 1188 |
} |
1190 | 1189 |
|
1191 | 1190 |
}; |
1192 | 1191 |
|
1193 | 1192 |
/// \ingroup search |
1194 | 1193 |
/// |
1195 | 1194 |
/// \brief %BFS Visit algorithm class. |
1196 | 1195 |
/// |
1197 | 1196 |
/// This class provides an efficient implementation of the %BFS algorithm |
1198 | 1197 |
/// with visitor interface. |
1199 | 1198 |
/// |
1200 | 1199 |
/// The %BfsVisit class provides an alternative interface to the Bfs |
1201 | 1200 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1202 | 1201 |
/// on every bfs event the \c Visitor class member functions. |
1203 | 1202 |
/// |
1204 | 1203 |
/// \tparam _Digraph The digraph type the algorithm runs on. |
1205 | 1204 |
/// The default value is |
1206 | 1205 |
/// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it |
1207 | 1206 |
/// is only passed to \ref BfsDefaultTraits. |
1208 | 1207 |
/// \tparam _Visitor The Visitor object for the algorithm. The |
1209 | 1208 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitor which |
1210 | 1209 |
/// does not observe the Bfs events. If you want to observe the bfs |
1211 | 1210 |
/// events you should implement your own Visitor class. |
1212 | 1211 |
/// \tparam _Traits Traits class to set various data types used by the |
1213 | 1212 |
/// algorithm. The default traits class is |
1214 | 1213 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". |
1215 | 1214 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
1216 | 1215 |
/// a Bfs visit traits class. |
1217 | 1216 |
#ifdef DOXYGEN |
1218 | 1217 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1219 | 1218 |
#else |
1220 | 1219 |
template <typename _Digraph = ListDigraph, |
1221 | 1220 |
typename _Visitor = BfsVisitor<_Digraph>, |
1222 | 1221 |
typename _Traits = BfsDefaultTraits<_Digraph> > |
1223 | 1222 |
#endif |
1224 | 1223 |
class BfsVisit { |
1225 | 1224 |
public: |
1226 | 1225 |
|
1227 | 1226 |
/// \brief \ref Exception for uninitialized parameters. |
1228 | 1227 |
/// |
1229 | 1228 |
/// This error represents problems in the initialization |
1230 | 1229 |
/// of the parameters of the algorithms. |
1231 | 1230 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1232 | 1231 |
public: |
1233 | 1232 |
virtual const char* what() const throw() |
1234 | 1233 |
{ |
1235 | 1234 |
return "lemon::BfsVisit::UninitializedParameter"; |
1236 | 1235 |
} |
1237 | 1236 |
}; |
1238 | 1237 |
|
1239 | 1238 |
typedef _Traits Traits; |
1240 | 1239 |
|
1241 | 1240 |
typedef typename Traits::Digraph Digraph; |
1242 | 1241 |
|
1243 | 1242 |
typedef _Visitor Visitor; |
1244 | 1243 |
|
1245 | 1244 |
///The type of the map indicating which nodes are reached. |
1246 | 1245 |
typedef typename Traits::ReachedMap ReachedMap; |
1247 | 1246 |
|
1248 | 1247 |
private: |
1249 | 1248 |
|
1250 | 1249 |
typedef typename Digraph::Node Node; |
1251 | 1250 |
typedef typename Digraph::NodeIt NodeIt; |
1252 | 1251 |
typedef typename Digraph::Arc Arc; |
1253 | 1252 |
typedef typename Digraph::OutArcIt OutArcIt; |
1254 | 1253 |
|
1255 | 1254 |
/// Pointer to the underlying digraph. |
1256 | 1255 |
const Digraph *_digraph; |
1257 | 1256 |
/// Pointer to the visitor object. |
1258 | 1257 |
Visitor *_visitor; |
1259 | 1258 |
///Pointer to the map of reached status of the nodes. |
1260 | 1259 |
ReachedMap *_reached; |
1261 | 1260 |
///Indicates if \ref _reached is locally allocated (\c true) or not. |
1262 | 1261 |
bool local_reached; |
1263 | 1262 |
|
1264 | 1263 |
std::vector<typename Digraph::Node> _list; |
1265 | 1264 |
int _list_front, _list_back; |
1266 | 1265 |
|
1267 | 1266 |
/// \brief Creates the maps if necessary. |
1268 | 1267 |
/// |
1269 | 1268 |
/// Creates the maps if necessary. |
1270 | 1269 |
void create_maps() { |
1271 | 1270 |
if(!_reached) { |
1272 | 1271 |
local_reached = true; |
1273 | 1272 |
_reached = Traits::createReachedMap(*_digraph); |
1274 | 1273 |
} |
1275 | 1274 |
} |
1276 | 1275 |
|
1277 | 1276 |
protected: |
1278 | 1277 |
|
1279 | 1278 |
BfsVisit() {} |
1280 | 1279 |
|
1281 | 1280 |
public: |
1282 | 1281 |
|
1283 | 1282 |
typedef BfsVisit Create; |
1284 | 1283 |
|
1285 | 1284 |
/// \name Named template parameters |
1286 | 1285 |
|
1287 | 1286 |
///@{ |
1288 | 1287 |
template <class T> |
1289 | 1288 |
struct DefReachedMapTraits : public Traits { |
1290 | 1289 |
typedef T ReachedMap; |
1291 | 1290 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1292 | 1291 |
throw UninitializedParameter(); |
1293 | 1292 |
} |
1294 | 1293 |
}; |
1295 | 1294 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1296 | 1295 |
/// ReachedMap type |
1297 | 1296 |
/// |
1298 | 1297 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type |
1299 | 1298 |
template <class T> |
1300 | 1299 |
struct DefReachedMap : public BfsVisit< Digraph, Visitor, |
1301 | 1300 |
DefReachedMapTraits<T> > { |
1302 | 1301 |
typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create; |
1303 | 1302 |
}; |
1304 | 1303 |
///@} |
1305 | 1304 |
|
1306 | 1305 |
public: |
1307 | 1306 |
|
1308 | 1307 |
/// \brief Constructor. |
1309 | 1308 |
/// |
1310 | 1309 |
/// Constructor. |
1311 | 1310 |
/// |
1312 | 1311 |
/// \param digraph the digraph the algorithm will run on. |
1313 | 1312 |
/// \param visitor The visitor of the algorithm. |
1314 | 1313 |
/// |
1315 | 1314 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1316 | 1315 |
: _digraph(&digraph), _visitor(&visitor), |
1317 | 1316 |
_reached(0), local_reached(false) {} |
1318 | 1317 |
|
1319 | 1318 |
/// \brief Destructor. |
1320 | 1319 |
/// |
1321 | 1320 |
/// Destructor. |
1322 | 1321 |
~BfsVisit() { |
1323 | 1322 |
if(local_reached) delete _reached; |
1324 | 1323 |
} |
1325 | 1324 |
|
1326 | 1325 |
/// \brief Sets the map indicating if a node is reached. |
1327 | 1326 |
/// |
1328 | 1327 |
/// Sets the map indicating if a node is reached. |
1329 | 1328 |
/// If you don't use this function before calling \ref run(), |
1330 | 1329 |
/// it will allocate one. The destuctor deallocates this |
1331 | 1330 |
/// automatically allocated map, of course. |
1332 | 1331 |
/// \return <tt> (*this) </tt> |
1333 | 1332 |
BfsVisit &reachedMap(ReachedMap &m) { |
1334 | 1333 |
if(local_reached) { |
1335 | 1334 |
delete _reached; |
1336 | 1335 |
local_reached = false; |
1337 | 1336 |
} |
1338 | 1337 |
_reached = &m; |
1339 | 1338 |
return *this; |
1340 | 1339 |
} |
1341 | 1340 |
|
1342 | 1341 |
public: |
1343 | 1342 |
/// \name Execution control |
1344 | 1343 |
/// The simplest way to execute the algorithm is to use |
1345 | 1344 |
/// one of the member functions called \c run(...). |
1346 | 1345 |
/// \n |
1347 | 1346 |
/// If you need more control on the execution, |
1348 | 1347 |
/// first you must call \ref init(), then you can adda source node |
1349 | 1348 |
/// with \ref addSource(). |
1350 | 1349 |
/// Finally \ref start() will perform the actual path |
1351 | 1350 |
/// computation. |
1352 | 1351 |
|
1353 | 1352 |
/// @{ |
1354 | 1353 |
/// \brief Initializes the internal data structures. |
1355 | 1354 |
/// |
1356 | 1355 |
/// Initializes the internal data structures. |
1357 | 1356 |
/// |
1358 | 1357 |
void init() { |
1359 | 1358 |
create_maps(); |
1360 | 1359 |
_list.resize(countNodes(*_digraph)); |
1361 | 1360 |
_list_front = _list_back = -1; |
1362 | 1361 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1363 | 1362 |
_reached->set(u, false); |
1364 | 1363 |
} |
1365 | 1364 |
} |
1366 | 1365 |
|
1367 | 1366 |
/// \brief Adds a new source node. |
1368 | 1367 |
/// |
1369 | 1368 |
/// Adds a new source node to the set of nodes to be processed. |
1370 | 1369 |
void addSource(Node s) { |
1371 | 1370 |
if(!(*_reached)[s]) { |
1372 | 1371 |
_reached->set(s,true); |
1373 | 1372 |
_visitor->start(s); |
1374 | 1373 |
_visitor->reach(s); |
1375 | 1374 |
_list[++_list_back] = s; |
1376 | 1375 |
} |
1377 | 1376 |
} |
1378 | 1377 |
|
1379 | 1378 |
/// \brief Processes the next node. |
1380 | 1379 |
/// |
1381 | 1380 |
/// Processes the next node. |
1382 | 1381 |
/// |
1383 | 1382 |
/// \return The processed node. |
1384 | 1383 |
/// |
1385 | 1384 |
/// \pre The queue must not be empty! |
1386 | 1385 |
Node processNextNode() { |
1387 | 1386 |
Node n = _list[++_list_front]; |
1388 | 1387 |
_visitor->process(n); |
1389 | 1388 |
Arc e; |
1390 | 1389 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1391 | 1390 |
Node m = _digraph->target(e); |
1392 | 1391 |
if (!(*_reached)[m]) { |
1393 | 1392 |
_visitor->discover(e); |
1394 | 1393 |
_visitor->reach(m); |
1395 | 1394 |
_reached->set(m, true); |
1396 | 1395 |
_list[++_list_back] = m; |
1397 | 1396 |
} else { |
1398 | 1397 |
_visitor->examine(e); |
1399 | 1398 |
} |
1400 | 1399 |
} |
1401 | 1400 |
return n; |
1402 | 1401 |
} |
1403 | 1402 |
|
1404 | 1403 |
/// \brief Processes the next node. |
1405 | 1404 |
/// |
1406 | 1405 |
/// Processes the next node. And checks that the given target node |
1407 | 1406 |
/// is reached. If the target node is reachable from the processed |
1408 | 1407 |
/// node then the reached parameter will be set true. The reached |
1409 | 1408 |
/// parameter should be initially false. |
1410 | 1409 |
/// |
1411 | 1410 |
/// \param target The target node. |
1412 | 1411 |
/// \retval reach Indicates that the target node is reached. |
1413 | 1412 |
/// \return The processed node. |
1414 | 1413 |
/// |
1415 | 1414 |
/// \warning The queue must not be empty! |
1416 | 1415 |
Node processNextNode(Node target, bool& reach) { |
1417 | 1416 |
Node n = _list[++_list_front]; |
1418 | 1417 |
_visitor->process(n); |
1419 | 1418 |
Arc e; |
1420 | 1419 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1421 | 1420 |
Node m = _digraph->target(e); |
1422 | 1421 |
if (!(*_reached)[m]) { |
1423 | 1422 |
_visitor->discover(e); |
1424 | 1423 |
_visitor->reach(m); |
1425 | 1424 |
_reached->set(m, true); |
1426 | 1425 |
_list[++_list_back] = m; |
1427 | 1426 |
reach = reach || (target == m); |
1428 | 1427 |
} else { |
1429 | 1428 |
_visitor->examine(e); |
1430 | 1429 |
} |
1431 | 1430 |
} |
1432 | 1431 |
return n; |
1433 | 1432 |
} |
1434 | 1433 |
|
1435 | 1434 |
/// \brief Processes the next node. |
1436 | 1435 |
/// |
1437 | 1436 |
/// Processes the next node. And checks that at least one of |
1438 | 1437 |
/// reached node has true value in the \c nm node map. If one node |
1439 | 1438 |
/// with true value is reachable from the processed node then the |
1440 | 1439 |
/// rnode parameter will be set to the first of such nodes. |
1441 | 1440 |
/// |
1442 | 1441 |
/// \param nm The node map of possible targets. |
1443 | 1442 |
/// \retval rnode The reached target node. |
1444 | 1443 |
/// \return The processed node. |
1445 | 1444 |
/// |
1446 | 1445 |
/// \warning The queue must not be empty! |
1447 | 1446 |
template <typename NM> |
1448 | 1447 |
Node processNextNode(const NM& nm, Node& rnode) { |
1449 | 1448 |
Node n = _list[++_list_front]; |
1450 | 1449 |
_visitor->process(n); |
1451 | 1450 |
Arc e; |
1452 | 1451 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1453 | 1452 |
Node m = _digraph->target(e); |
1454 | 1453 |
if (!(*_reached)[m]) { |
1455 | 1454 |
_visitor->discover(e); |
1456 | 1455 |
_visitor->reach(m); |
1457 | 1456 |
_reached->set(m, true); |
1458 | 1457 |
_list[++_list_back] = m; |
1459 | 1458 |
if (nm[m] && rnode == INVALID) rnode = m; |
1460 | 1459 |
} else { |
1461 | 1460 |
_visitor->examine(e); |
1462 | 1461 |
} |
1463 | 1462 |
} |
1464 | 1463 |
return n; |
1465 | 1464 |
} |
1466 | 1465 |
|
1467 | 1466 |
/// \brief Next node to be processed. |
1468 | 1467 |
/// |
1469 | 1468 |
/// Next node to be processed. |
1470 | 1469 |
/// |
1471 | 1470 |
/// \return The next node to be processed or INVALID if the stack is |
1472 | 1471 |
/// empty. |
1473 | 1472 |
Node nextNode() { |
1474 | 1473 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1475 | 1474 |
} |
1476 | 1475 |
|
1477 | 1476 |
/// \brief Returns \c false if there are nodes |
1478 | 1477 |
/// to be processed in the queue |
1479 | 1478 |
/// |
1480 | 1479 |
/// Returns \c false if there are nodes |
1481 | 1480 |
/// to be processed in the queue |
1482 | 1481 |
bool emptyQueue() { return _list_front == _list_back; } |
1483 | 1482 |
|
1484 | 1483 |
/// \brief Returns the number of the nodes to be processed. |
1485 | 1484 |
/// |
1486 | 1485 |
/// Returns the number of the nodes to be processed in the queue. |
1487 | 1486 |
int queueSize() { return _list_back - _list_front; } |
1488 | 1487 |
|
1489 | 1488 |
/// \brief Executes the algorithm. |
1490 | 1489 |
/// |
1491 | 1490 |
/// Executes the algorithm. |
1492 | 1491 |
/// |
1493 | 1492 |
/// \pre init() must be called and at least one node should be added |
1494 | 1493 |
/// with addSource() before using this function. |
1495 | 1494 |
void start() { |
1496 | 1495 |
while ( !emptyQueue() ) processNextNode(); |
1497 | 1496 |
} |
1498 | 1497 |
|
1499 | 1498 |
/// \brief Executes the algorithm until \c dest is reached. |
1500 | 1499 |
/// |
1501 | 1500 |
/// Executes the algorithm until \c dest is reached. |
1502 | 1501 |
/// |
1503 | 1502 |
/// \pre init() must be called and at least one node should be added |
1504 | 1503 |
/// with addSource() before using this function. |
1505 | 1504 |
void start(Node dest) { |
1506 | 1505 |
bool reach = false; |
1507 | 1506 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
1508 | 1507 |
} |
1509 | 1508 |
|
1510 | 1509 |
/// \brief Executes the algorithm until a condition is met. |
1511 | 1510 |
/// |
1512 | 1511 |
/// Executes the algorithm until a condition is met. |
1513 | 1512 |
/// |
1514 | 1513 |
/// \pre init() must be called and at least one node should be added |
1515 | 1514 |
/// with addSource() before using this function. |
1516 | 1515 |
/// |
1517 | 1516 |
///\param nm must be a bool (or convertible) node map. The |
1518 | 1517 |
///algorithm will stop when it reaches a node \c v with |
1519 | 1518 |
/// <tt>nm[v]</tt> true. |
1520 | 1519 |
/// |
1521 | 1520 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
1522 | 1521 |
///\c INVALID if no such node was found. |
1523 | 1522 |
template <typename NM> |
1524 | 1523 |
Node start(const NM &nm) { |
1525 | 1524 |
Node rnode = INVALID; |
1526 | 1525 |
while ( !emptyQueue() && rnode == INVALID ) { |
1527 | 1526 |
processNextNode(nm, rnode); |
1528 | 1527 |
} |
1529 | 1528 |
return rnode; |
1530 | 1529 |
} |
1531 | 1530 |
|
1532 | 1531 |
/// \brief Runs %BFSVisit algorithm from node \c s. |
1533 | 1532 |
/// |
1534 | 1533 |
/// This method runs the %BFS algorithm from a root node \c s. |
1535 | 1534 |
/// \note b.run(s) is just a shortcut of the following code. |
1536 | 1535 |
///\code |
1537 | 1536 |
/// b.init(); |
1538 | 1537 |
/// b.addSource(s); |
1539 | 1538 |
/// b.start(); |
1540 | 1539 |
///\endcode |
1541 | 1540 |
void run(Node s) { |
1542 | 1541 |
init(); |
1543 | 1542 |
addSource(s); |
1544 | 1543 |
start(); |
1545 | 1544 |
} |
1546 | 1545 |
|
1547 | 1546 |
/// \brief Runs %BFSVisit algorithm to visit all nodes in the digraph. |
1548 | 1547 |
/// |
1549 | 1548 |
/// This method runs the %BFS algorithm in order to |
1550 | 1549 |
/// compute the %BFS path to each node. The algorithm computes |
1551 | 1550 |
/// - The %BFS tree. |
1552 | 1551 |
/// - The distance of each node from the root in the %BFS tree. |
1553 | 1552 |
/// |
1554 | 1553 |
///\note b.run() is just a shortcut of the following code. |
1555 | 1554 |
///\code |
1556 | 1555 |
/// b.init(); |
1557 | 1556 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
1558 | 1557 |
/// if (!b.reached(it)) { |
1559 | 1558 |
/// b.addSource(it); |
1560 | 1559 |
/// b.start(); |
1561 | 1560 |
/// } |
1562 | 1561 |
/// } |
1563 | 1562 |
///\endcode |
1564 | 1563 |
void run() { |
1565 | 1564 |
init(); |
1566 | 1565 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1567 | 1566 |
if (!reached(it)) { |
1568 | 1567 |
addSource(it); |
1569 | 1568 |
start(); |
1570 | 1569 |
} |
1571 | 1570 |
} |
1572 | 1571 |
} |
1573 | 1572 |
///@} |
1574 | 1573 |
|
1575 | 1574 |
/// \name Query Functions |
1576 | 1575 |
/// The result of the %BFS algorithm can be obtained using these |
1577 | 1576 |
/// functions.\n |
1578 | 1577 |
/// Before the use of these functions, |
1579 | 1578 |
/// either run() or start() must be called. |
1580 | 1579 |
///@{ |
1581 | 1580 |
|
1582 | 1581 |
/// \brief Checks if a node is reachable from the root. |
1583 | 1582 |
/// |
1584 | 1583 |
/// Returns \c true if \c v is reachable from the root(s). |
1585 | 1584 |
/// \warning The source nodes are inditated as unreachable. |
1586 | 1585 |
/// \pre Either \ref run() or \ref start() |
1587 | 1586 |
/// must be called before using this function. |
1588 | 1587 |
/// |
1589 | 1588 |
bool reached(Node v) { return (*_reached)[v]; } |
1590 | 1589 |
///@} |
1591 | 1590 |
}; |
1592 | 1591 |
|
1593 | 1592 |
} //END OF NAMESPACE LEMON |
1594 | 1593 |
|
1595 | 1594 |
#endif |
1596 | 1595 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H |
20 | 20 |
#define LEMON_BITS_ALTERATION_NOTIFIER_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <list> |
24 | 24 |
|
25 |
#include <lemon/ |
|
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 graph's can be refered as two containers, one node container |
39 | 39 |
/// and one edge container. But they are not standard containers they |
40 | 40 |
/// does not store values directly they are just key continars for more |
41 | 41 |
/// value containers which are the node and edge maps. |
42 | 42 |
/// |
43 | 43 |
/// The graph's node and edge sets can be changed as we add or erase |
44 | 44 |
/// nodes and edges in the graph. Lemon would like to handle easily |
45 | 45 |
/// that the node and edge maps should contain values for all nodes or |
46 | 46 |
/// edges. If we want to check on every indicing if the map contains |
47 | 47 |
/// the current indicing key that cause a drawback in the performance |
48 | 48 |
/// in the library. We use another solution we notify all maps about |
49 | 49 |
/// an alteration in the graph, which cause only drawback on the |
50 | 50 |
/// alteration of the graph. |
51 | 51 |
/// |
52 | 52 |
/// This class provides an interface to the container. The \e first() and \e |
53 | 53 |
/// next() member functions make possible to iterate on the keys of the |
54 | 54 |
/// container. The \e id() function returns an integer id for each key. |
55 | 55 |
/// The \e maxId() function gives back an upper bound of the ids. |
56 | 56 |
/// |
57 | 57 |
/// For the proper functonality of this class, we should notify it |
58 | 58 |
/// about each alteration in the container. The alterations have four type |
59 | 59 |
/// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and |
60 | 60 |
/// \e erase() signals that only one or few items added or erased to or |
61 | 61 |
/// from the graph. If all items are erased from the graph or from an empty |
62 | 62 |
/// graph a new graph is builded then it can be signaled with the |
63 | 63 |
/// clear() and build() members. Important rule that if we erase items |
64 | 64 |
/// from graph we should first signal the alteration and after that erase |
65 | 65 |
/// them from the container, on the other way on item addition we should |
66 | 66 |
/// first extend the container and just after that signal the alteration. |
67 | 67 |
/// |
68 | 68 |
/// The alteration can be observed with a class inherited from the |
69 | 69 |
/// \e ObserverBase nested class. The signals can be handled with |
70 | 70 |
/// overriding the virtual functions defined in the base class. The |
71 | 71 |
/// observer base can be attached to the notifier with the |
72 | 72 |
/// \e attach() member and can be detached with detach() function. The |
73 | 73 |
/// alteration handlers should not call any function which signals |
74 | 74 |
/// an other alteration in the same notifier and should not |
75 | 75 |
/// detach any observer from the notifier. |
76 | 76 |
/// |
77 | 77 |
/// Alteration observers try to be exception safe. If an \e add() or |
78 | 78 |
/// a \e clear() function throws an exception then the remaining |
79 | 79 |
/// observeres will not be notified and the fulfilled additions will |
80 | 80 |
/// be rolled back by calling the \e erase() or \e clear() |
81 | 81 |
/// functions. Thence the \e erase() and \e clear() should not throw |
82 | 82 |
/// exception. Actullay, it can be throw only |
83 | 83 |
/// \ref AlterationObserver::ImmediateDetach ImmediateDetach |
84 | 84 |
/// exception which detach the observer from the notifier. |
85 | 85 |
/// |
86 | 86 |
/// There are some place when the alteration observing is not completly |
87 | 87 |
/// reliable. If we want to carry out the node degree in the graph |
88 | 88 |
/// as in the \ref InDegMap and we use the reverseEdge that cause |
89 | 89 |
/// unreliable functionality. Because the alteration observing signals |
90 | 90 |
/// only erasing and adding but not the reversing it will stores bad |
91 | 91 |
/// degrees. The sub graph adaptors cannot signal the alterations because |
92 | 92 |
/// just a setting in the filter map can modify the graph and this cannot |
93 | 93 |
/// be watched in any way. |
94 | 94 |
/// |
95 | 95 |
/// \param _Container The container which is observed. |
96 | 96 |
/// \param _Item The item type which is obserbved. |
97 | 97 |
|
98 | 98 |
template <typename _Container, typename _Item> |
99 | 99 |
class AlterationNotifier { |
100 | 100 |
public: |
101 | 101 |
|
102 | 102 |
typedef True Notifier; |
103 | 103 |
|
104 | 104 |
typedef _Container Container; |
105 | 105 |
typedef _Item Item; |
106 | 106 |
|
107 | 107 |
/// \brief Exception which can be called from \e clear() and |
108 | 108 |
/// \e erase(). |
109 | 109 |
/// |
110 | 110 |
/// From the \e clear() and \e erase() function only this |
111 | 111 |
/// exception is allowed to throw. The exception immediatly |
112 | 112 |
/// detaches the current observer from the notifier. Because the |
113 | 113 |
/// \e clear() and \e erase() should not throw other exceptions |
114 | 114 |
/// it can be used to invalidate the observer. |
115 | 115 |
struct ImmediateDetach {}; |
116 | 116 |
|
117 | 117 |
/// \brief ObserverBase is the base class for the observers. |
118 | 118 |
/// |
119 | 119 |
/// ObserverBase is the abstract base class for the observers. |
120 | 120 |
/// It will be notified about an item was inserted into or |
121 | 121 |
/// erased from the graph. |
122 | 122 |
/// |
123 | 123 |
/// The observer interface contains some pure virtual functions |
124 | 124 |
/// to override. The add() and erase() functions are |
125 | 125 |
/// to notify the oberver when one item is added or |
126 | 126 |
/// erased. |
127 | 127 |
/// |
128 | 128 |
/// The build() and clear() members are to notify the observer |
129 | 129 |
/// about the container is built from an empty container or |
130 | 130 |
/// is cleared to an empty container. |
131 | 131 |
|
132 | 132 |
class ObserverBase { |
133 | 133 |
protected: |
134 | 134 |
typedef AlterationNotifier Notifier; |
135 | 135 |
|
136 | 136 |
friend class AlterationNotifier; |
137 | 137 |
|
138 | 138 |
/// \brief Default constructor. |
139 | 139 |
/// |
140 | 140 |
/// Default constructor for ObserverBase. |
141 | 141 |
/// |
142 | 142 |
ObserverBase() : _notifier(0) {} |
143 | 143 |
|
144 | 144 |
/// \brief Constructor which attach the observer into notifier. |
145 | 145 |
/// |
146 | 146 |
/// Constructor which attach the observer into notifier. |
147 | 147 |
ObserverBase(AlterationNotifier& nf) { |
148 | 148 |
attach(nf); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
/// \brief Constructor which attach the obserever to the same notifier. |
152 | 152 |
/// |
153 | 153 |
/// Constructor which attach the obserever to the same notifier as |
154 | 154 |
/// the other observer is attached to. |
155 | 155 |
ObserverBase(const ObserverBase& copy) { |
156 | 156 |
if (copy.attached()) { |
157 | 157 |
attach(*copy.notifier()); |
158 | 158 |
} |
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
/// \brief Destructor |
162 | 162 |
virtual ~ObserverBase() { |
163 | 163 |
if (attached()) { |
164 | 164 |
detach(); |
165 | 165 |
} |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
/// \brief Attaches the observer into an AlterationNotifier. |
169 | 169 |
/// |
170 | 170 |
/// This member attaches the observer into an AlterationNotifier. |
171 | 171 |
/// |
172 | 172 |
void attach(AlterationNotifier& nf) { |
173 | 173 |
nf.attach(*this); |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
/// \brief Detaches the observer into an AlterationNotifier. |
177 | 177 |
/// |
178 | 178 |
/// This member detaches the observer from an AlterationNotifier. |
179 | 179 |
/// |
180 | 180 |
void detach() { |
181 | 181 |
_notifier->detach(*this); |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
/// \brief Gives back a pointer to the notifier which the map |
185 | 185 |
/// attached into. |
186 | 186 |
/// |
187 | 187 |
/// This function gives back a pointer to the notifier which the map |
188 | 188 |
/// attached into. |
189 | 189 |
/// |
190 | 190 |
Notifier* notifier() const { return const_cast<Notifier*>(_notifier); } |
191 | 191 |
|
192 | 192 |
/// Gives back true when the observer is attached into a notifier. |
193 | 193 |
bool attached() const { return _notifier != 0; } |
194 | 194 |
|
195 | 195 |
private: |
196 | 196 |
|
197 | 197 |
ObserverBase& operator=(const ObserverBase& copy); |
198 | 198 |
|
199 | 199 |
protected: |
200 | 200 |
|
201 | 201 |
Notifier* _notifier; |
202 | 202 |
typename std::list<ObserverBase*>::iterator _index; |
203 | 203 |
|
204 | 204 |
/// \brief The member function to notificate the observer about an |
205 | 205 |
/// item is added to the container. |
206 | 206 |
/// |
207 | 207 |
/// The add() member function notificates the observer about an item |
208 | 208 |
/// is added to the container. It have to be overrided in the |
209 | 209 |
/// subclasses. |
210 | 210 |
virtual void add(const Item&) = 0; |
211 | 211 |
|
212 | 212 |
/// \brief The member function to notificate the observer about |
213 | 213 |
/// more item is added to the container. |
214 | 214 |
/// |
215 | 215 |
/// The add() member function notificates the observer about more item |
216 | 216 |
/// is added to the container. It have to be overrided in the |
217 | 217 |
/// subclasses. |
218 | 218 |
virtual void add(const std::vector<Item>& items) = 0; |
219 | 219 |
|
220 | 220 |
/// \brief The member function to notificate the observer about an |
221 | 221 |
/// item is erased from the container. |
222 | 222 |
/// |
223 | 223 |
/// The erase() member function notificates the observer about an |
224 | 224 |
/// item is erased from the container. It have to be overrided in |
225 | 225 |
/// the subclasses. |
226 | 226 |
virtual void erase(const Item&) = 0; |
227 | 227 |
|
228 | 228 |
/// \brief The member function to notificate the observer about |
229 | 229 |
/// more item is erased from the container. |
230 | 230 |
/// |
231 | 231 |
/// The erase() member function notificates the observer about more item |
232 | 232 |
/// is erased from the container. It have to be overrided in the |
233 | 233 |
/// subclasses. |
234 | 234 |
virtual void erase(const std::vector<Item>& items) = 0; |
235 | 235 |
|
236 | 236 |
/// \brief The member function to notificate the observer about the |
237 | 237 |
/// container is built. |
238 | 238 |
/// |
239 | 239 |
/// The build() member function notificates the observer about the |
240 | 240 |
/// container is built from an empty container. It have to be |
241 | 241 |
/// overrided in the subclasses. |
242 | 242 |
|
243 | 243 |
virtual void build() = 0; |
244 | 244 |
|
245 | 245 |
/// \brief The member function to notificate the observer about all |
246 | 246 |
/// items are erased from the container. |
247 | 247 |
/// |
248 | 248 |
/// The clear() member function notificates the observer about all |
249 | 249 |
/// items are erased from the container. It have to be overrided in |
250 | 250 |
/// the subclasses. |
251 | 251 |
virtual void clear() = 0; |
252 | 252 |
|
253 | 253 |
}; |
254 | 254 |
|
255 | 255 |
protected: |
256 | 256 |
|
257 | 257 |
const Container* container; |
258 | 258 |
|
259 | 259 |
typedef std::list<ObserverBase*> Observers; |
260 | 260 |
Observers _observers; |
261 | 261 |
|
262 | 262 |
|
263 | 263 |
public: |
264 | 264 |
|
265 | 265 |
/// \brief Default constructor. |
266 | 266 |
/// |
267 | 267 |
/// The default constructor of the AlterationNotifier. |
268 | 268 |
/// It creates an empty notifier. |
269 | 269 |
AlterationNotifier() |
270 | 270 |
: container(0) {} |
271 | 271 |
|
272 | 272 |
/// \brief Constructor. |
273 | 273 |
/// |
274 | 274 |
/// Constructor with the observed container parameter. |
275 | 275 |
AlterationNotifier(const Container& _container) |
276 | 276 |
: container(&_container) {} |
277 | 277 |
|
278 | 278 |
/// \brief Copy Constructor of the AlterationNotifier. |
279 | 279 |
/// |
280 | 280 |
/// Copy constructor of the AlterationNotifier. |
281 | 281 |
/// It creates only an empty notifier because the copiable |
282 | 282 |
/// notifier's observers have to be registered still into that notifier. |
283 | 283 |
AlterationNotifier(const AlterationNotifier& _notifier) |
284 | 284 |
: container(_notifier.container) {} |
285 | 285 |
|
286 | 286 |
/// \brief Destructor. |
287 | 287 |
/// |
288 | 288 |
/// Destructor of the AlterationNotifier. |
289 | 289 |
/// |
290 | 290 |
~AlterationNotifier() { |
291 | 291 |
typename Observers::iterator it; |
292 | 292 |
for (it = _observers.begin(); it != _observers.end(); ++it) { |
293 | 293 |
(*it)->_notifier = 0; |
294 | 294 |
} |
295 | 295 |
} |
296 | 296 |
|
297 | 297 |
/// \brief Sets the container. |
298 | 298 |
/// |
299 | 299 |
/// Sets the container. |
300 | 300 |
void setContainer(const Container& _container) { |
301 | 301 |
container = &_container; |
302 | 302 |
} |
303 | 303 |
|
304 | 304 |
protected: |
305 | 305 |
|
306 | 306 |
AlterationNotifier& operator=(const AlterationNotifier&); |
307 | 307 |
|
308 | 308 |
public: |
309 | 309 |
|
310 | 310 |
|
311 | 311 |
|
312 | 312 |
/// \brief First item in the container. |
313 | 313 |
/// |
314 | 314 |
/// Returns the first item in the container. It is |
315 | 315 |
/// for start the iteration on the container. |
316 | 316 |
void first(Item& item) const { |
317 | 317 |
container->first(item); |
318 | 318 |
} |
319 | 319 |
|
320 | 320 |
/// \brief Next item in the container. |
321 | 321 |
/// |
322 | 322 |
/// Returns the next item in the container. It is |
323 | 323 |
/// for iterate on the container. |
324 | 324 |
void next(Item& item) const { |
325 | 325 |
container->next(item); |
326 | 326 |
} |
327 | 327 |
|
328 | 328 |
/// \brief Returns the id of the item. |
329 | 329 |
/// |
330 | 330 |
/// Returns the id of the item provided by the container. |
331 | 331 |
int id(const Item& item) const { |
332 | 332 |
return container->id(item); |
333 | 333 |
} |
334 | 334 |
|
335 | 335 |
/// \brief Returns the maximum id of the container. |
336 | 336 |
/// |
337 | 337 |
/// Returns the maximum id of the container. |
338 | 338 |
int maxId() const { |
339 | 339 |
return container->maxId(Item()); |
340 | 340 |
} |
341 | 341 |
|
342 | 342 |
protected: |
343 | 343 |
|
344 | 344 |
void attach(ObserverBase& observer) { |
345 | 345 |
observer._index = _observers.insert(_observers.begin(), &observer); |
346 | 346 |
observer._notifier = this; |
347 | 347 |
} |
348 | 348 |
|
349 | 349 |
void detach(ObserverBase& observer) { |
350 | 350 |
_observers.erase(observer._index); |
351 | 351 |
observer._index = _observers.end(); |
352 | 352 |
observer._notifier = 0; |
353 | 353 |
} |
354 | 354 |
|
355 | 355 |
public: |
356 | 356 |
|
357 | 357 |
/// \brief Notifies all the registed observers about an item added to |
358 | 358 |
/// the container. |
359 | 359 |
/// |
360 | 360 |
/// It notifies all the registed observers about an item added to |
361 | 361 |
/// the container. |
362 | 362 |
/// |
363 | 363 |
void add(const Item& item) { |
364 | 364 |
typename Observers::reverse_iterator it; |
365 | 365 |
try { |
366 | 366 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) { |
367 | 367 |
(*it)->add(item); |
368 | 368 |
} |
369 | 369 |
} catch (...) { |
370 | 370 |
typename Observers::iterator jt; |
371 | 371 |
for (jt = it.base(); jt != _observers.end(); ++jt) { |
372 | 372 |
(*jt)->erase(item); |
373 | 373 |
} |
374 | 374 |
throw; |
375 | 375 |
} |
376 | 376 |
} |
377 | 377 |
|
378 | 378 |
/// \brief Notifies all the registed observers about more item added to |
379 | 379 |
/// the container. |
380 | 380 |
/// |
381 | 381 |
/// It notifies all the registed observers about more item added to |
382 | 382 |
/// the container. |
383 | 383 |
/// |
384 | 384 |
void add(const std::vector<Item>& items) { |
385 | 385 |
typename Observers::reverse_iterator it; |
386 | 386 |
try { |
387 | 387 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) { |
388 | 388 |
(*it)->add(items); |
389 | 389 |
} |
390 | 390 |
} catch (...) { |
391 | 391 |
typename Observers::iterator jt; |
392 | 392 |
for (jt = it.base(); jt != _observers.end(); ++jt) { |
393 | 393 |
(*jt)->erase(items); |
394 | 394 |
} |
395 | 395 |
throw; |
396 | 396 |
} |
397 | 397 |
} |
398 | 398 |
|
399 | 399 |
/// \brief Notifies all the registed observers about an item erased from |
400 | 400 |
/// the container. |
401 | 401 |
/// |
402 | 402 |
/// It notifies all the registed observers about an item erased from |
403 | 403 |
/// the container. |
404 | 404 |
/// |
405 | 405 |
void erase(const Item& item) throw() { |
406 | 406 |
typename Observers::iterator it = _observers.begin(); |
407 | 407 |
while (it != _observers.end()) { |
408 | 408 |
try { |
409 | 409 |
(*it)->erase(item); |
410 | 410 |
++it; |
411 | 411 |
} catch (const ImmediateDetach&) { |
412 | 412 |
it = _observers.erase(it); |
413 | 413 |
(*it)->_index = _observers.end(); |
414 | 414 |
(*it)->_notifier = 0; |
415 | 415 |
} |
416 | 416 |
} |
417 | 417 |
} |
418 | 418 |
|
419 | 419 |
/// \brief Notifies all the registed observers about more item erased |
420 | 420 |
/// from the container. |
421 | 421 |
/// |
422 | 422 |
/// It notifies all the registed observers about more item erased from |
423 | 423 |
/// the container. |
424 | 424 |
/// |
425 | 425 |
void erase(const std::vector<Item>& items) { |
426 | 426 |
typename Observers::iterator it = _observers.begin(); |
427 | 427 |
while (it != _observers.end()) { |
428 | 428 |
try { |
429 | 429 |
(*it)->erase(items); |
430 | 430 |
++it; |
431 | 431 |
} catch (const ImmediateDetach&) { |
432 | 432 |
it = _observers.erase(it); |
433 | 433 |
(*it)->_index = _observers.end(); |
434 | 434 |
(*it)->_notifier = 0; |
435 | 435 |
} |
436 | 436 |
} |
437 | 437 |
} |
438 | 438 |
|
439 | 439 |
/// \brief Notifies all the registed observers about the container is |
440 | 440 |
/// built. |
441 | 441 |
/// |
442 | 442 |
/// Notifies all the registed observers about the container is built |
443 | 443 |
/// from an empty container. |
444 | 444 |
void build() { |
445 | 445 |
typename Observers::reverse_iterator it; |
446 | 446 |
try { |
447 | 447 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) { |
448 | 448 |
(*it)->build(); |
449 | 449 |
} |
450 | 450 |
} catch (...) { |
451 | 451 |
typename Observers::iterator jt; |
452 | 452 |
for (jt = it.base(); jt != _observers.end(); ++jt) { |
453 | 453 |
(*jt)->clear(); |
454 | 454 |
} |
455 | 455 |
throw; |
456 | 456 |
} |
457 | 457 |
} |
458 | 458 |
|
459 | 459 |
/// \brief Notifies all the registed observers about all items are |
460 | 460 |
/// erased. |
461 | 461 |
/// |
462 | 462 |
/// Notifies all the registed observers about all items are erased |
463 | 463 |
/// from the container. |
464 | 464 |
void clear() { |
465 | 465 |
typename Observers::iterator it = _observers.begin(); |
466 | 466 |
while (it != _observers.end()) { |
467 | 467 |
try { |
468 | 468 |
(*it)->clear(); |
469 | 469 |
++it; |
470 | 470 |
} catch (const ImmediateDetach&) { |
471 | 471 |
it = _observers.erase(it); |
472 | 472 |
(*it)->_index = _observers.end(); |
473 | 473 |
(*it)->_notifier = 0; |
474 | 474 |
} |
475 | 475 |
} |
476 | 476 |
} |
477 | 477 |
}; |
478 | 478 |
|
479 | 479 |
} |
480 | 480 |
|
481 | 481 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_BASE_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_BASE_EXTENDER_H |
21 | 21 |
|
22 |
#include <lemon/ |
|
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 digraph types |
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \ingroup digraphbits |
37 | 37 |
/// |
38 | 38 |
/// \brief BaseDigraph to BaseGraph extender |
39 | 39 |
template <typename Base> |
40 | 40 |
class UndirDigraphExtender : public Base { |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef Base Parent; |
45 | 45 |
typedef typename Parent::Arc Edge; |
46 | 46 |
typedef typename Parent::Node Node; |
47 | 47 |
|
48 | 48 |
typedef True UndirectedTag; |
49 | 49 |
|
50 | 50 |
class Arc : public Edge { |
51 | 51 |
friend class UndirDigraphExtender; |
52 | 52 |
|
53 | 53 |
protected: |
54 | 54 |
bool forward; |
55 | 55 |
|
56 | 56 |
Arc(const Edge &ue, bool _forward) : |
57 | 57 |
Edge(ue), forward(_forward) {} |
58 | 58 |
|
59 | 59 |
public: |
60 | 60 |
Arc() {} |
61 | 61 |
|
62 | 62 |
/// Invalid arc constructor |
63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {} |
64 | 64 |
|
65 | 65 |
bool operator==(const Arc &that) const { |
66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
67 | 67 |
} |
68 | 68 |
bool operator!=(const Arc &that) const { |
69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
70 | 70 |
} |
71 | 71 |
bool operator<(const Arc &that) const { |
72 | 72 |
return forward<that.forward || |
73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
74 | 74 |
} |
75 | 75 |
}; |
76 | 76 |
|
77 | 77 |
|
78 | 78 |
|
79 | 79 |
using Parent::source; |
80 | 80 |
|
81 | 81 |
/// Source of the given Arc. |
82 | 82 |
Node source(const Arc &e) const { |
83 | 83 |
return e.forward ? Parent::source(e) : Parent::target(e); |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
using Parent::target; |
87 | 87 |
|
88 | 88 |
/// Target of the given Arc. |
89 | 89 |
Node target(const Arc &e) const { |
90 | 90 |
return e.forward ? Parent::target(e) : Parent::source(e); |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
/// \brief Directed arc from an edge. |
94 | 94 |
/// |
95 | 95 |
/// Returns a directed arc corresponding to the specified Edge. |
96 | 96 |
/// If the given bool is true the given edge and the |
97 | 97 |
/// returned arc have the same source node. |
98 | 98 |
static Arc direct(const Edge &ue, bool d) { |
99 | 99 |
return Arc(ue, d); |
100 | 100 |
} |
101 | 101 |
|
102 | 102 |
/// Returns whether the given directed arc is same orientation as the |
103 | 103 |
/// corresponding edge. |
104 | 104 |
/// |
105 | 105 |
/// \todo reference to the corresponding point of the undirected digraph |
106 | 106 |
/// concept. "What does the direction of an edge mean?" |
107 | 107 |
static bool direction(const Arc &e) { return e.forward; } |
108 | 108 |
|
109 | 109 |
|
110 | 110 |
using Parent::first; |
111 | 111 |
using Parent::next; |
112 | 112 |
|
113 | 113 |
void first(Arc &e) const { |
114 | 114 |
Parent::first(e); |
115 | 115 |
e.forward=true; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
void next(Arc &e) const { |
119 | 119 |
if( e.forward ) { |
120 | 120 |
e.forward = false; |
121 | 121 |
} |
122 | 122 |
else { |
123 | 123 |
Parent::next(e); |
124 | 124 |
e.forward = true; |
125 | 125 |
} |
126 | 126 |
} |
127 | 127 |
|
128 | 128 |
void firstOut(Arc &e, const Node &n) const { |
129 | 129 |
Parent::firstIn(e,n); |
130 | 130 |
if( Edge(e) != INVALID ) { |
131 | 131 |
e.forward = false; |
132 | 132 |
} |
133 | 133 |
else { |
134 | 134 |
Parent::firstOut(e,n); |
135 | 135 |
e.forward = true; |
136 | 136 |
} |
137 | 137 |
} |
138 | 138 |
void nextOut(Arc &e) const { |
139 | 139 |
if( ! e.forward ) { |
140 | 140 |
Node n = Parent::target(e); |
141 | 141 |
Parent::nextIn(e); |
142 | 142 |
if( Edge(e) == INVALID ) { |
143 | 143 |
Parent::firstOut(e, n); |
144 | 144 |
e.forward = true; |
145 | 145 |
} |
146 | 146 |
} |
147 | 147 |
else { |
148 | 148 |
Parent::nextOut(e); |
149 | 149 |
} |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
void firstIn(Arc &e, const Node &n) const { |
153 | 153 |
Parent::firstOut(e,n); |
154 | 154 |
if( Edge(e) != INVALID ) { |
155 | 155 |
e.forward = false; |
156 | 156 |
} |
157 | 157 |
else { |
158 | 158 |
Parent::firstIn(e,n); |
159 | 159 |
e.forward = true; |
160 | 160 |
} |
161 | 161 |
} |
162 | 162 |
void nextIn(Arc &e) const { |
163 | 163 |
if( ! e.forward ) { |
164 | 164 |
Node n = Parent::source(e); |
165 | 165 |
Parent::nextOut(e); |
166 | 166 |
if( Edge(e) == INVALID ) { |
167 | 167 |
Parent::firstIn(e, n); |
168 | 168 |
e.forward = true; |
169 | 169 |
} |
170 | 170 |
} |
171 | 171 |
else { |
172 | 172 |
Parent::nextIn(e); |
173 | 173 |
} |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
177 | 177 |
d = true; |
178 | 178 |
Parent::firstOut(e, n); |
179 | 179 |
if (e != INVALID) return; |
180 | 180 |
d = false; |
181 | 181 |
Parent::firstIn(e, n); |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
void nextInc(Edge &e, bool &d) const { |
185 | 185 |
if (d) { |
186 | 186 |
Node s = Parent::source(e); |
187 | 187 |
Parent::nextOut(e); |
188 | 188 |
if (e != INVALID) return; |
189 | 189 |
d = false; |
190 | 190 |
Parent::firstIn(e, s); |
191 | 191 |
} else { |
192 | 192 |
Parent::nextIn(e); |
193 | 193 |
} |
194 | 194 |
} |
195 | 195 |
|
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 |
|
233 | 233 |
int arcNum() const { |
234 | 234 |
return 2 * Parent::arcNum(); |
235 | 235 |
} |
236 | 236 |
|
237 | 237 |
int edgeNum() const { |
238 | 238 |
return Parent::arcNum(); |
239 | 239 |
} |
240 | 240 |
|
241 | 241 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
242 | 242 |
if (p == INVALID) { |
243 | 243 |
Edge arc = Parent::findArc(s, t); |
244 | 244 |
if (arc != INVALID) return direct(arc, true); |
245 | 245 |
arc = Parent::findArc(t, s); |
246 | 246 |
if (arc != INVALID) return direct(arc, false); |
247 | 247 |
} else if (direction(p)) { |
248 | 248 |
Edge arc = Parent::findArc(s, t, p); |
249 | 249 |
if (arc != INVALID) return direct(arc, true); |
250 | 250 |
arc = Parent::findArc(t, s); |
251 | 251 |
if (arc != INVALID) return direct(arc, false); |
252 | 252 |
} else { |
253 | 253 |
Edge arc = Parent::findArc(t, s, p); |
254 | 254 |
if (arc != INVALID) return direct(arc, false); |
255 | 255 |
} |
256 | 256 |
return INVALID; |
257 | 257 |
} |
258 | 258 |
|
259 | 259 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
260 | 260 |
if (s != t) { |
261 | 261 |
if (p == INVALID) { |
262 | 262 |
Edge arc = Parent::findArc(s, t); |
263 | 263 |
if (arc != INVALID) return arc; |
264 | 264 |
arc = Parent::findArc(t, s); |
265 | 265 |
if (arc != INVALID) return arc; |
266 | 266 |
} else if (Parent::s(p) == s) { |
267 | 267 |
Edge arc = Parent::findArc(s, t, p); |
268 | 268 |
if (arc != INVALID) return arc; |
269 | 269 |
arc = Parent::findArc(t, s); |
270 | 270 |
if (arc != INVALID) return arc; |
271 | 271 |
} else { |
272 | 272 |
Edge arc = Parent::findArc(t, s, p); |
273 | 273 |
if (arc != INVALID) return arc; |
274 | 274 |
} |
275 | 275 |
} else { |
276 | 276 |
return Parent::findArc(s, t, p); |
277 | 277 |
} |
278 | 278 |
return INVALID; |
279 | 279 |
} |
280 | 280 |
}; |
281 | 281 |
|
282 | 282 |
template <typename Base> |
283 | 283 |
class BidirBpGraphExtender : public Base { |
284 | 284 |
public: |
285 | 285 |
typedef Base Parent; |
286 | 286 |
typedef BidirBpGraphExtender Digraph; |
287 | 287 |
|
288 | 288 |
typedef typename Parent::Node Node; |
289 | 289 |
typedef typename Parent::Edge Edge; |
290 | 290 |
|
291 | 291 |
|
292 | 292 |
using Parent::first; |
293 | 293 |
using Parent::next; |
294 | 294 |
|
295 | 295 |
using Parent::id; |
296 | 296 |
|
297 | 297 |
class Red : public Node { |
298 | 298 |
friend class BidirBpGraphExtender; |
299 | 299 |
public: |
300 | 300 |
Red() {} |
301 | 301 |
Red(const Node& node) : Node(node) { |
302 | 302 |
LEMON_ASSERT(Parent::red(node) || node == INVALID, |
303 | 303 |
typename Parent::NodeSetError()); |
304 | 304 |
} |
305 | 305 |
Red& operator=(const Node& node) { |
306 | 306 |
LEMON_ASSERT(Parent::red(node) || node == INVALID, |
307 | 307 |
typename Parent::NodeSetError()); |
308 | 308 |
Node::operator=(node); |
309 | 309 |
return *this; |
310 | 310 |
} |
311 | 311 |
Red(Invalid) : Node(INVALID) {} |
312 | 312 |
Red& operator=(Invalid) { |
313 | 313 |
Node::operator=(INVALID); |
314 | 314 |
return *this; |
315 | 315 |
} |
316 | 316 |
}; |
317 | 317 |
|
318 | 318 |
void first(Red& node) const { |
319 | 319 |
Parent::firstRed(static_cast<Node&>(node)); |
320 | 320 |
} |
321 | 321 |
void next(Red& node) const { |
322 | 322 |
Parent::nextRed(static_cast<Node&>(node)); |
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
int id(const Red& node) const { |
326 | 326 |
return Parent::redId(node); |
327 | 327 |
} |
328 | 328 |
|
329 | 329 |
class Blue : public Node { |
330 | 330 |
friend class BidirBpGraphExtender; |
331 | 331 |
public: |
332 | 332 |
Blue() {} |
333 | 333 |
Blue(const Node& node) : Node(node) { |
334 | 334 |
LEMON_ASSERT(Parent::blue(node) || node == INVALID, |
335 | 335 |
typename Parent::NodeSetError()); |
336 | 336 |
} |
337 | 337 |
Blue& operator=(const Node& node) { |
338 | 338 |
LEMON_ASSERT(Parent::blue(node) || node == INVALID, |
339 | 339 |
typename Parent::NodeSetError()); |
340 | 340 |
Node::operator=(node); |
341 | 341 |
return *this; |
342 | 342 |
} |
343 | 343 |
Blue(Invalid) : Node(INVALID) {} |
344 | 344 |
Blue& operator=(Invalid) { |
345 | 345 |
Node::operator=(INVALID); |
346 | 346 |
return *this; |
347 | 347 |
} |
348 | 348 |
}; |
349 | 349 |
|
350 | 350 |
void first(Blue& node) const { |
351 | 351 |
Parent::firstBlue(static_cast<Node&>(node)); |
352 | 352 |
} |
353 | 353 |
void next(Blue& node) const { |
354 | 354 |
Parent::nextBlue(static_cast<Node&>(node)); |
355 | 355 |
} |
356 | 356 |
|
357 | 357 |
int id(const Blue& node) const { |
358 | 358 |
return Parent::redId(node); |
359 | 359 |
} |
360 | 360 |
|
361 | 361 |
Node source(const Edge& arc) const { |
362 | 362 |
return red(arc); |
363 | 363 |
} |
364 | 364 |
Node target(const Edge& arc) const { |
365 | 365 |
return blue(arc); |
366 | 366 |
} |
367 | 367 |
|
368 | 368 |
void firstInc(Edge& arc, bool& dir, const Node& node) const { |
369 | 369 |
if (Parent::red(node)) { |
370 | 370 |
Parent::firstFromRed(arc, node); |
371 | 371 |
dir = true; |
372 | 372 |
} else { |
373 | 373 |
Parent::firstFromBlue(arc, node); |
374 | 374 |
dir = static_cast<Edge&>(arc) == INVALID; |
375 | 375 |
} |
376 | 376 |
} |
377 | 377 |
void nextInc(Edge& arc, bool& dir) const { |
378 | 378 |
if (dir) { |
379 | 379 |
Parent::nextFromRed(arc); |
380 | 380 |
} else { |
381 | 381 |
Parent::nextFromBlue(arc); |
382 | 382 |
if (arc == INVALID) dir = true; |
383 | 383 |
} |
384 | 384 |
} |
385 | 385 |
|
386 | 386 |
class Arc : public Edge { |
387 | 387 |
friend class BidirBpGraphExtender; |
388 | 388 |
protected: |
389 | 389 |
bool forward; |
390 | 390 |
|
391 | 391 |
Arc(const Edge& arc, bool _forward) |
392 | 392 |
: Edge(arc), forward(_forward) {} |
393 | 393 |
|
394 | 394 |
public: |
395 | 395 |
Arc() {} |
396 | 396 |
Arc (Invalid) : Edge(INVALID), forward(true) {} |
397 | 397 |
bool operator==(const Arc& i) const { |
398 | 398 |
return Edge::operator==(i) && forward == i.forward; |
399 | 399 |
} |
400 | 400 |
bool operator!=(const Arc& i) const { |
401 | 401 |
return Edge::operator!=(i) || forward != i.forward; |
402 | 402 |
} |
403 | 403 |
bool operator<(const Arc& i) const { |
404 | 404 |
return Edge::operator<(i) || |
405 | 405 |
(!(i.forward<forward) && Edge(*this)<Edge(i)); |
406 | 406 |
} |
407 | 407 |
}; |
408 | 408 |
|
409 | 409 |
void first(Arc& arc) const { |
410 | 410 |
Parent::first(static_cast<Edge&>(arc)); |
411 | 411 |
arc.forward = true; |
412 | 412 |
} |
413 | 413 |
|
414 | 414 |
void next(Arc& arc) const { |
415 | 415 |
if (!arc.forward) { |
416 | 416 |
Parent::next(static_cast<Edge&>(arc)); |
417 | 417 |
} |
418 | 418 |
arc.forward = !arc.forward; |
419 | 419 |
} |
420 | 420 |
|
421 | 421 |
void firstOut(Arc& arc, const Node& node) const { |
422 | 422 |
if (Parent::red(node)) { |
423 | 423 |
Parent::firstFromRed(arc, node); |
424 | 424 |
arc.forward = true; |
425 | 425 |
} else { |
426 | 426 |
Parent::firstFromBlue(arc, node); |
427 | 427 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
428 | 428 |
} |
429 | 429 |
} |
430 | 430 |
void nextOut(Arc& arc) const { |
431 | 431 |
if (arc.forward) { |
432 | 432 |
Parent::nextFromRed(arc); |
433 | 433 |
} else { |
434 | 434 |
Parent::nextFromBlue(arc); |
435 | 435 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
436 | 436 |
} |
437 | 437 |
} |
438 | 438 |
|
439 | 439 |
void firstIn(Arc& arc, const Node& node) const { |
440 | 440 |
if (Parent::blue(node)) { |
441 | 441 |
Parent::firstFromBlue(arc, node); |
442 | 442 |
arc.forward = true; |
443 | 443 |
} else { |
444 | 444 |
Parent::firstFromRed(arc, node); |
445 | 445 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
446 | 446 |
} |
447 | 447 |
} |
448 | 448 |
void nextIn(Arc& arc) const { |
449 | 449 |
if (arc.forward) { |
450 | 450 |
Parent::nextFromBlue(arc); |
451 | 451 |
} else { |
452 | 452 |
Parent::nextFromRed(arc); |
453 | 453 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
454 | 454 |
} |
455 | 455 |
} |
456 | 456 |
|
457 | 457 |
Node source(const Arc& arc) const { |
458 | 458 |
return arc.forward ? Parent::red(arc) : Parent::blue(arc); |
459 | 459 |
} |
460 | 460 |
Node target(const Arc& arc) const { |
461 | 461 |
return arc.forward ? Parent::blue(arc) : Parent::red(arc); |
462 | 462 |
} |
463 | 463 |
|
464 | 464 |
int id(const Arc& arc) const { |
465 | 465 |
return (Parent::id(static_cast<const Edge&>(arc)) << 1) + |
466 | 466 |
(arc.forward ? 0 : 1); |
467 | 467 |
} |
468 | 468 |
Arc arcFromId(int ix) const { |
469 | 469 |
return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0); |
470 | 470 |
} |
471 | 471 |
int maxArcId() const { |
472 | 472 |
return (Parent::maxEdgeId() << 1) + 1; |
473 | 473 |
} |
474 | 474 |
|
475 | 475 |
bool direction(const Arc& arc) const { |
476 | 476 |
return arc.forward; |
477 | 477 |
} |
478 | 478 |
|
479 | 479 |
Arc direct(const Edge& arc, bool dir) const { |
480 | 480 |
return Arc(arc, dir); |
481 | 481 |
} |
482 | 482 |
|
483 | 483 |
int arcNum() const { |
484 | 484 |
return 2 * Parent::edgeNum(); |
485 | 485 |
} |
486 | 486 |
|
487 | 487 |
int edgeNum() const { |
488 | 488 |
return Parent::edgeNum(); |
489 | 489 |
} |
490 | 490 |
|
491 | 491 |
|
492 | 492 |
}; |
493 | 493 |
} |
494 | 494 |
|
495 | 495 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_GRAPH_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_GRAPH_EXTENDER_H |
21 | 21 |
|
22 |
#include <lemon/bits/invalid.h> |
|
23 |
#include <lemon/bits/utility.h> |
|
22 |
#include <lemon/core.h> |
|
24 | 23 |
|
25 | 24 |
#include <lemon/bits/map_extender.h> |
26 | 25 |
#include <lemon/bits/default_map.h> |
27 | 26 |
|
28 | 27 |
#include <lemon/concept_check.h> |
29 | 28 |
#include <lemon/concepts/maps.h> |
30 | 29 |
|
31 | 30 |
///\ingroup graphbits |
32 | 31 |
///\file |
33 | 32 |
///\brief Extenders for the digraph types |
34 | 33 |
namespace lemon { |
35 | 34 |
|
36 | 35 |
/// \ingroup graphbits |
37 | 36 |
/// |
38 | 37 |
/// \brief Extender for the Digraphs |
39 | 38 |
template <typename Base> |
40 | 39 |
class DigraphExtender : public Base { |
41 | 40 |
public: |
42 | 41 |
|
43 | 42 |
typedef Base Parent; |
44 | 43 |
typedef DigraphExtender Digraph; |
45 | 44 |
|
46 | 45 |
// Base extensions |
47 | 46 |
|
48 | 47 |
typedef typename Parent::Node Node; |
49 | 48 |
typedef typename Parent::Arc Arc; |
50 | 49 |
|
51 | 50 |
int maxId(Node) const { |
52 | 51 |
return Parent::maxNodeId(); |
53 | 52 |
} |
54 | 53 |
|
55 | 54 |
int maxId(Arc) const { |
56 | 55 |
return Parent::maxArcId(); |
57 | 56 |
} |
58 | 57 |
|
59 | 58 |
Node fromId(int id, Node) const { |
60 | 59 |
return Parent::nodeFromId(id); |
61 | 60 |
} |
62 | 61 |
|
63 | 62 |
Arc fromId(int id, Arc) const { |
64 | 63 |
return Parent::arcFromId(id); |
65 | 64 |
} |
66 | 65 |
|
67 | 66 |
Node oppositeNode(const Node &node, const Arc &arc) const { |
68 | 67 |
if (node == Parent::source(arc)) |
69 | 68 |
return Parent::target(arc); |
70 | 69 |
else if(node == Parent::target(arc)) |
71 | 70 |
return Parent::source(arc); |
72 | 71 |
else |
73 | 72 |
return INVALID; |
74 | 73 |
} |
75 | 74 |
|
76 | 75 |
// Alterable extension |
77 | 76 |
|
78 | 77 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
79 | 78 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
80 | 79 |
|
81 | 80 |
|
82 | 81 |
protected: |
83 | 82 |
|
84 | 83 |
mutable NodeNotifier node_notifier; |
85 | 84 |
mutable ArcNotifier arc_notifier; |
86 | 85 |
|
87 | 86 |
public: |
88 | 87 |
|
89 | 88 |
NodeNotifier& notifier(Node) const { |
90 | 89 |
return node_notifier; |
91 | 90 |
} |
92 | 91 |
|
93 | 92 |
ArcNotifier& notifier(Arc) const { |
94 | 93 |
return arc_notifier; |
95 | 94 |
} |
96 | 95 |
|
97 | 96 |
class NodeIt : public Node { |
98 | 97 |
const Digraph* _digraph; |
99 | 98 |
public: |
100 | 99 |
|
101 | 100 |
NodeIt() {} |
102 | 101 |
|
103 | 102 |
NodeIt(Invalid i) : Node(i) { } |
104 | 103 |
|
105 | 104 |
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) { |
106 | 105 |
_digraph->first(static_cast<Node&>(*this)); |
107 | 106 |
} |
108 | 107 |
|
109 | 108 |
NodeIt(const Digraph& digraph, const Node& node) |
110 | 109 |
: Node(node), _digraph(&digraph) {} |
111 | 110 |
|
112 | 111 |
NodeIt& operator++() { |
113 | 112 |
_digraph->next(*this); |
114 | 113 |
return *this; |
115 | 114 |
} |
116 | 115 |
|
117 | 116 |
}; |
118 | 117 |
|
119 | 118 |
|
120 | 119 |
class ArcIt : public Arc { |
121 | 120 |
const Digraph* _digraph; |
122 | 121 |
public: |
123 | 122 |
|
124 | 123 |
ArcIt() { } |
125 | 124 |
|
126 | 125 |
ArcIt(Invalid i) : Arc(i) { } |
127 | 126 |
|
128 | 127 |
explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) { |
129 | 128 |
_digraph->first(static_cast<Arc&>(*this)); |
130 | 129 |
} |
131 | 130 |
|
132 | 131 |
ArcIt(const Digraph& digraph, const Arc& arc) : |
133 | 132 |
Arc(arc), _digraph(&digraph) { } |
134 | 133 |
|
135 | 134 |
ArcIt& operator++() { |
136 | 135 |
_digraph->next(*this); |
137 | 136 |
return *this; |
138 | 137 |
} |
139 | 138 |
|
140 | 139 |
}; |
141 | 140 |
|
142 | 141 |
|
143 | 142 |
class OutArcIt : public Arc { |
144 | 143 |
const Digraph* _digraph; |
145 | 144 |
public: |
146 | 145 |
|
147 | 146 |
OutArcIt() { } |
148 | 147 |
|
149 | 148 |
OutArcIt(Invalid i) : Arc(i) { } |
150 | 149 |
|
151 | 150 |
OutArcIt(const Digraph& digraph, const Node& node) |
152 | 151 |
: _digraph(&digraph) { |
153 | 152 |
_digraph->firstOut(*this, node); |
154 | 153 |
} |
155 | 154 |
|
156 | 155 |
OutArcIt(const Digraph& digraph, const Arc& arc) |
157 | 156 |
: Arc(arc), _digraph(&digraph) {} |
158 | 157 |
|
159 | 158 |
OutArcIt& operator++() { |
160 | 159 |
_digraph->nextOut(*this); |
161 | 160 |
return *this; |
162 | 161 |
} |
163 | 162 |
|
164 | 163 |
}; |
165 | 164 |
|
166 | 165 |
|
167 | 166 |
class InArcIt : public Arc { |
168 | 167 |
const Digraph* _digraph; |
169 | 168 |
public: |
170 | 169 |
|
171 | 170 |
InArcIt() { } |
172 | 171 |
|
173 | 172 |
InArcIt(Invalid i) : Arc(i) { } |
174 | 173 |
|
175 | 174 |
InArcIt(const Digraph& digraph, const Node& node) |
176 | 175 |
: _digraph(&digraph) { |
177 | 176 |
_digraph->firstIn(*this, node); |
178 | 177 |
} |
179 | 178 |
|
180 | 179 |
InArcIt(const Digraph& digraph, const Arc& arc) : |
181 | 180 |
Arc(arc), _digraph(&digraph) {} |
182 | 181 |
|
183 | 182 |
InArcIt& operator++() { |
184 | 183 |
_digraph->nextIn(*this); |
185 | 184 |
return *this; |
186 | 185 |
} |
187 | 186 |
|
188 | 187 |
}; |
189 | 188 |
|
190 | 189 |
/// \brief Base node of the iterator |
191 | 190 |
/// |
192 | 191 |
/// Returns the base node (i.e. the source in this case) of the iterator |
193 | 192 |
Node baseNode(const OutArcIt &arc) const { |
194 | 193 |
return Parent::source(arc); |
195 | 194 |
} |
196 | 195 |
/// \brief Running node of the iterator |
197 | 196 |
/// |
198 | 197 |
/// Returns the running node (i.e. the target in this case) of the |
199 | 198 |
/// iterator |
200 | 199 |
Node runningNode(const OutArcIt &arc) const { |
201 | 200 |
return Parent::target(arc); |
202 | 201 |
} |
203 | 202 |
|
204 | 203 |
/// \brief Base node of the iterator |
205 | 204 |
/// |
206 | 205 |
/// Returns the base node (i.e. the target in this case) of the iterator |
207 | 206 |
Node baseNode(const InArcIt &arc) const { |
208 | 207 |
return Parent::target(arc); |
209 | 208 |
} |
210 | 209 |
/// \brief Running node of the iterator |
211 | 210 |
/// |
212 | 211 |
/// Returns the running node (i.e. the source in this case) of the |
213 | 212 |
/// iterator |
214 | 213 |
Node runningNode(const InArcIt &arc) const { |
215 | 214 |
return Parent::source(arc); |
216 | 215 |
} |
217 | 216 |
|
218 | 217 |
|
219 | 218 |
template <typename _Value> |
220 | 219 |
class NodeMap |
221 | 220 |
: public MapExtender<DefaultMap<Digraph, Node, _Value> > { |
222 | 221 |
public: |
223 | 222 |
typedef DigraphExtender Digraph; |
224 | 223 |
typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent; |
225 | 224 |
|
226 | 225 |
explicit NodeMap(const Digraph& digraph) |
227 | 226 |
: Parent(digraph) {} |
228 | 227 |
NodeMap(const Digraph& digraph, const _Value& value) |
229 | 228 |
: Parent(digraph, value) {} |
230 | 229 |
|
231 | 230 |
NodeMap& operator=(const NodeMap& cmap) { |
232 | 231 |
return operator=<NodeMap>(cmap); |
233 | 232 |
} |
234 | 233 |
|
235 | 234 |
template <typename CMap> |
236 | 235 |
NodeMap& operator=(const CMap& cmap) { |
237 | 236 |
Parent::operator=(cmap); |
238 | 237 |
return *this; |
239 | 238 |
} |
240 | 239 |
|
241 | 240 |
}; |
242 | 241 |
|
243 | 242 |
template <typename _Value> |
244 | 243 |
class ArcMap |
245 | 244 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
246 | 245 |
public: |
247 | 246 |
typedef DigraphExtender Digraph; |
248 | 247 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
249 | 248 |
|
250 | 249 |
explicit ArcMap(const Digraph& digraph) |
251 | 250 |
: Parent(digraph) {} |
252 | 251 |
ArcMap(const Digraph& digraph, const _Value& value) |
253 | 252 |
: Parent(digraph, value) {} |
254 | 253 |
|
255 | 254 |
ArcMap& operator=(const ArcMap& cmap) { |
256 | 255 |
return operator=<ArcMap>(cmap); |
257 | 256 |
} |
258 | 257 |
|
259 | 258 |
template <typename CMap> |
260 | 259 |
ArcMap& operator=(const CMap& cmap) { |
261 | 260 |
Parent::operator=(cmap); |
262 | 261 |
return *this; |
263 | 262 |
} |
264 | 263 |
}; |
265 | 264 |
|
266 | 265 |
|
267 | 266 |
Node addNode() { |
268 | 267 |
Node node = Parent::addNode(); |
269 | 268 |
notifier(Node()).add(node); |
270 | 269 |
return node; |
271 | 270 |
} |
272 | 271 |
|
273 | 272 |
Arc addArc(const Node& from, const Node& to) { |
274 | 273 |
Arc arc = Parent::addArc(from, to); |
275 | 274 |
notifier(Arc()).add(arc); |
276 | 275 |
return arc; |
277 | 276 |
} |
278 | 277 |
|
279 | 278 |
void clear() { |
280 | 279 |
notifier(Arc()).clear(); |
281 | 280 |
notifier(Node()).clear(); |
282 | 281 |
Parent::clear(); |
283 | 282 |
} |
284 | 283 |
|
285 | 284 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
286 | 285 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) { |
287 | 286 |
Parent::build(digraph, nodeRef, arcRef); |
288 | 287 |
notifier(Node()).build(); |
289 | 288 |
notifier(Arc()).build(); |
290 | 289 |
} |
291 | 290 |
|
292 | 291 |
void erase(const Node& node) { |
293 | 292 |
Arc arc; |
294 | 293 |
Parent::firstOut(arc, node); |
295 | 294 |
while (arc != INVALID ) { |
296 | 295 |
erase(arc); |
297 | 296 |
Parent::firstOut(arc, node); |
298 | 297 |
} |
299 | 298 |
|
300 | 299 |
Parent::firstIn(arc, node); |
301 | 300 |
while (arc != INVALID ) { |
302 | 301 |
erase(arc); |
303 | 302 |
Parent::firstIn(arc, node); |
304 | 303 |
} |
305 | 304 |
|
306 | 305 |
notifier(Node()).erase(node); |
307 | 306 |
Parent::erase(node); |
308 | 307 |
} |
309 | 308 |
|
310 | 309 |
void erase(const Arc& arc) { |
311 | 310 |
notifier(Arc()).erase(arc); |
312 | 311 |
Parent::erase(arc); |
313 | 312 |
} |
314 | 313 |
|
315 | 314 |
DigraphExtender() { |
316 | 315 |
node_notifier.setContainer(*this); |
317 | 316 |
arc_notifier.setContainer(*this); |
318 | 317 |
} |
319 | 318 |
|
320 | 319 |
|
321 | 320 |
~DigraphExtender() { |
322 | 321 |
arc_notifier.clear(); |
323 | 322 |
node_notifier.clear(); |
324 | 323 |
} |
325 | 324 |
}; |
326 | 325 |
|
327 | 326 |
/// \ingroup _graphbits |
328 | 327 |
/// |
329 | 328 |
/// \brief Extender for the Graphs |
330 | 329 |
template <typename Base> |
331 | 330 |
class GraphExtender : public Base { |
332 | 331 |
public: |
333 | 332 |
|
334 | 333 |
typedef Base Parent; |
335 | 334 |
typedef GraphExtender Graph; |
336 | 335 |
|
337 | 336 |
typedef True UndirectedTag; |
338 | 337 |
|
339 | 338 |
typedef typename Parent::Node Node; |
340 | 339 |
typedef typename Parent::Arc Arc; |
341 | 340 |
typedef typename Parent::Edge Edge; |
342 | 341 |
|
343 | 342 |
// Graph extension |
344 | 343 |
|
345 | 344 |
int maxId(Node) const { |
346 | 345 |
return Parent::maxNodeId(); |
347 | 346 |
} |
348 | 347 |
|
349 | 348 |
int maxId(Arc) const { |
350 | 349 |
return Parent::maxArcId(); |
351 | 350 |
} |
352 | 351 |
|
353 | 352 |
int maxId(Edge) const { |
354 | 353 |
return Parent::maxEdgeId(); |
355 | 354 |
} |
356 | 355 |
|
357 | 356 |
Node fromId(int id, Node) const { |
358 | 357 |
return Parent::nodeFromId(id); |
359 | 358 |
} |
360 | 359 |
|
361 | 360 |
Arc fromId(int id, Arc) const { |
362 | 361 |
return Parent::arcFromId(id); |
363 | 362 |
} |
364 | 363 |
|
365 | 364 |
Edge fromId(int id, Edge) const { |
366 | 365 |
return Parent::edgeFromId(id); |
367 | 366 |
} |
368 | 367 |
|
369 | 368 |
Node oppositeNode(const Node &n, const Edge &e) const { |
370 | 369 |
if( n == Parent::u(e)) |
371 | 370 |
return Parent::v(e); |
372 | 371 |
else if( n == Parent::v(e)) |
373 | 372 |
return Parent::u(e); |
374 | 373 |
else |
375 | 374 |
return INVALID; |
376 | 375 |
} |
377 | 376 |
|
378 | 377 |
Arc oppositeArc(const Arc &arc) const { |
379 | 378 |
return Parent::direct(arc, !Parent::direction(arc)); |
380 | 379 |
} |
381 | 380 |
|
382 | 381 |
using Parent::direct; |
383 | 382 |
Arc direct(const Edge &edge, const Node &node) const { |
384 | 383 |
return Parent::direct(edge, Parent::u(edge) == node); |
385 | 384 |
} |
386 | 385 |
|
387 | 386 |
// Alterable extension |
388 | 387 |
|
389 | 388 |
typedef AlterationNotifier<GraphExtender, Node> NodeNotifier; |
390 | 389 |
typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier; |
391 | 390 |
typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier; |
392 | 391 |
|
393 | 392 |
|
394 | 393 |
protected: |
395 | 394 |
|
396 | 395 |
mutable NodeNotifier node_notifier; |
397 | 396 |
mutable ArcNotifier arc_notifier; |
398 | 397 |
mutable EdgeNotifier edge_notifier; |
399 | 398 |
|
400 | 399 |
public: |
401 | 400 |
|
402 | 401 |
NodeNotifier& notifier(Node) const { |
403 | 402 |
return node_notifier; |
404 | 403 |
} |
405 | 404 |
|
406 | 405 |
ArcNotifier& notifier(Arc) const { |
407 | 406 |
return arc_notifier; |
408 | 407 |
} |
409 | 408 |
|
410 | 409 |
EdgeNotifier& notifier(Edge) const { |
411 | 410 |
return edge_notifier; |
412 | 411 |
} |
413 | 412 |
|
414 | 413 |
|
415 | 414 |
|
416 | 415 |
class NodeIt : public Node { |
417 | 416 |
const Graph* _graph; |
418 | 417 |
public: |
419 | 418 |
|
420 | 419 |
NodeIt() {} |
421 | 420 |
|
422 | 421 |
NodeIt(Invalid i) : Node(i) { } |
423 | 422 |
|
424 | 423 |
explicit NodeIt(const Graph& graph) : _graph(&graph) { |
425 | 424 |
_graph->first(static_cast<Node&>(*this)); |
426 | 425 |
} |
427 | 426 |
|
428 | 427 |
NodeIt(const Graph& graph, const Node& node) |
429 | 428 |
: Node(node), _graph(&graph) {} |
430 | 429 |
|
431 | 430 |
NodeIt& operator++() { |
432 | 431 |
_graph->next(*this); |
433 | 432 |
return *this; |
434 | 433 |
} |
435 | 434 |
|
436 | 435 |
}; |
437 | 436 |
|
438 | 437 |
|
439 | 438 |
class ArcIt : public Arc { |
440 | 439 |
const Graph* _graph; |
441 | 440 |
public: |
442 | 441 |
|
443 | 442 |
ArcIt() { } |
444 | 443 |
|
445 | 444 |
ArcIt(Invalid i) : Arc(i) { } |
446 | 445 |
|
447 | 446 |
explicit ArcIt(const Graph& graph) : _graph(&graph) { |
448 | 447 |
_graph->first(static_cast<Arc&>(*this)); |
449 | 448 |
} |
450 | 449 |
|
451 | 450 |
ArcIt(const Graph& graph, const Arc& arc) : |
452 | 451 |
Arc(arc), _graph(&graph) { } |
453 | 452 |
|
454 | 453 |
ArcIt& operator++() { |
455 | 454 |
_graph->next(*this); |
456 | 455 |
return *this; |
457 | 456 |
} |
458 | 457 |
|
459 | 458 |
}; |
460 | 459 |
|
461 | 460 |
|
462 | 461 |
class OutArcIt : public Arc { |
463 | 462 |
const Graph* _graph; |
464 | 463 |
public: |
465 | 464 |
|
466 | 465 |
OutArcIt() { } |
467 | 466 |
|
468 | 467 |
OutArcIt(Invalid i) : Arc(i) { } |
469 | 468 |
|
470 | 469 |
OutArcIt(const Graph& graph, const Node& node) |
471 | 470 |
: _graph(&graph) { |
472 | 471 |
_graph->firstOut(*this, node); |
473 | 472 |
} |
474 | 473 |
|
475 | 474 |
OutArcIt(const Graph& graph, const Arc& arc) |
476 | 475 |
: Arc(arc), _graph(&graph) {} |
477 | 476 |
|
478 | 477 |
OutArcIt& operator++() { |
479 | 478 |
_graph->nextOut(*this); |
480 | 479 |
return *this; |
481 | 480 |
} |
482 | 481 |
|
483 | 482 |
}; |
484 | 483 |
|
485 | 484 |
|
486 | 485 |
class InArcIt : public Arc { |
487 | 486 |
const Graph* _graph; |
488 | 487 |
public: |
489 | 488 |
|
490 | 489 |
InArcIt() { } |
491 | 490 |
|
492 | 491 |
InArcIt(Invalid i) : Arc(i) { } |
493 | 492 |
|
494 | 493 |
InArcIt(const Graph& graph, const Node& node) |
495 | 494 |
: _graph(&graph) { |
496 | 495 |
_graph->firstIn(*this, node); |
497 | 496 |
} |
498 | 497 |
|
499 | 498 |
InArcIt(const Graph& graph, const Arc& arc) : |
500 | 499 |
Arc(arc), _graph(&graph) {} |
501 | 500 |
|
502 | 501 |
InArcIt& operator++() { |
503 | 502 |
_graph->nextIn(*this); |
504 | 503 |
return *this; |
505 | 504 |
} |
506 | 505 |
|
507 | 506 |
}; |
508 | 507 |
|
509 | 508 |
|
510 | 509 |
class EdgeIt : public Parent::Edge { |
511 | 510 |
const Graph* _graph; |
512 | 511 |
public: |
513 | 512 |
|
514 | 513 |
EdgeIt() { } |
515 | 514 |
|
516 | 515 |
EdgeIt(Invalid i) : Edge(i) { } |
517 | 516 |
|
518 | 517 |
explicit EdgeIt(const Graph& graph) : _graph(&graph) { |
519 | 518 |
_graph->first(static_cast<Edge&>(*this)); |
520 | 519 |
} |
521 | 520 |
|
522 | 521 |
EdgeIt(const Graph& graph, const Edge& edge) : |
523 | 522 |
Edge(edge), _graph(&graph) { } |
524 | 523 |
|
525 | 524 |
EdgeIt& operator++() { |
526 | 525 |
_graph->next(*this); |
527 | 526 |
return *this; |
528 | 527 |
} |
529 | 528 |
|
530 | 529 |
}; |
531 | 530 |
|
532 | 531 |
class IncEdgeIt : public Parent::Edge { |
533 | 532 |
friend class GraphExtender; |
534 | 533 |
const Graph* _graph; |
535 | 534 |
bool _direction; |
536 | 535 |
public: |
537 | 536 |
|
538 | 537 |
IncEdgeIt() { } |
539 | 538 |
|
540 | 539 |
IncEdgeIt(Invalid i) : Edge(i), _direction(false) { } |
541 | 540 |
|
542 | 541 |
IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) { |
543 | 542 |
_graph->firstInc(*this, _direction, node); |
544 | 543 |
} |
545 | 544 |
|
546 | 545 |
IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node) |
547 | 546 |
: _graph(&graph), Edge(edge) { |
548 | 547 |
_direction = (_graph->source(edge) == node); |
549 | 548 |
} |
550 | 549 |
|
551 | 550 |
IncEdgeIt& operator++() { |
552 | 551 |
_graph->nextInc(*this, _direction); |
553 | 552 |
return *this; |
554 | 553 |
} |
555 | 554 |
}; |
556 | 555 |
|
557 | 556 |
/// \brief Base node of the iterator |
558 | 557 |
/// |
559 | 558 |
/// Returns the base node (ie. the source in this case) of the iterator |
560 | 559 |
Node baseNode(const OutArcIt &arc) const { |
561 | 560 |
return Parent::source(static_cast<const Arc&>(arc)); |
562 | 561 |
} |
563 | 562 |
/// \brief Running node of the iterator |
564 | 563 |
/// |
565 | 564 |
/// Returns the running node (ie. the target in this case) of the |
566 | 565 |
/// iterator |
567 | 566 |
Node runningNode(const OutArcIt &arc) const { |
568 | 567 |
return Parent::target(static_cast<const Arc&>(arc)); |
569 | 568 |
} |
570 | 569 |
|
571 | 570 |
/// \brief Base node of the iterator |
572 | 571 |
/// |
573 | 572 |
/// Returns the base node (ie. the target in this case) of the iterator |
574 | 573 |
Node baseNode(const InArcIt &arc) const { |
575 | 574 |
return Parent::target(static_cast<const Arc&>(arc)); |
576 | 575 |
} |
577 | 576 |
/// \brief Running node of the iterator |
578 | 577 |
/// |
579 | 578 |
/// Returns the running node (ie. the source in this case) of the |
580 | 579 |
/// iterator |
581 | 580 |
Node runningNode(const InArcIt &arc) const { |
582 | 581 |
return Parent::source(static_cast<const Arc&>(arc)); |
583 | 582 |
} |
584 | 583 |
|
585 | 584 |
/// Base node of the iterator |
586 | 585 |
/// |
587 | 586 |
/// Returns the base node of the iterator |
588 | 587 |
Node baseNode(const IncEdgeIt &edge) const { |
589 | 588 |
return edge._direction ? u(edge) : v(edge); |
590 | 589 |
} |
591 | 590 |
/// Running node of the iterator |
592 | 591 |
/// |
593 | 592 |
/// Returns the running node of the iterator |
594 | 593 |
Node runningNode(const IncEdgeIt &edge) const { |
595 | 594 |
return edge._direction ? v(edge) : u(edge); |
596 | 595 |
} |
597 | 596 |
|
598 | 597 |
// Mappable extension |
599 | 598 |
|
600 | 599 |
template <typename _Value> |
601 | 600 |
class NodeMap |
602 | 601 |
: public MapExtender<DefaultMap<Graph, Node, _Value> > { |
603 | 602 |
public: |
604 | 603 |
typedef GraphExtender Graph; |
605 | 604 |
typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent; |
606 | 605 |
|
607 | 606 |
NodeMap(const Graph& graph) |
608 | 607 |
: Parent(graph) {} |
609 | 608 |
NodeMap(const Graph& graph, const _Value& value) |
610 | 609 |
: Parent(graph, value) {} |
611 | 610 |
|
612 | 611 |
NodeMap& operator=(const NodeMap& cmap) { |
613 | 612 |
return operator=<NodeMap>(cmap); |
614 | 613 |
} |
615 | 614 |
|
616 | 615 |
template <typename CMap> |
617 | 616 |
NodeMap& operator=(const CMap& cmap) { |
618 | 617 |
Parent::operator=(cmap); |
619 | 618 |
return *this; |
620 | 619 |
} |
621 | 620 |
|
622 | 621 |
}; |
623 | 622 |
|
624 | 623 |
template <typename _Value> |
625 | 624 |
class ArcMap |
626 | 625 |
: public MapExtender<DefaultMap<Graph, Arc, _Value> > { |
627 | 626 |
public: |
628 | 627 |
typedef GraphExtender Graph; |
629 | 628 |
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent; |
630 | 629 |
|
631 | 630 |
ArcMap(const Graph& graph) |
632 | 631 |
: Parent(graph) {} |
633 | 632 |
ArcMap(const Graph& graph, const _Value& value) |
634 | 633 |
: Parent(graph, value) {} |
635 | 634 |
|
636 | 635 |
ArcMap& operator=(const ArcMap& cmap) { |
637 | 636 |
return operator=<ArcMap>(cmap); |
638 | 637 |
} |
639 | 638 |
|
640 | 639 |
template <typename CMap> |
641 | 640 |
ArcMap& operator=(const CMap& cmap) { |
642 | 641 |
Parent::operator=(cmap); |
643 | 642 |
return *this; |
644 | 643 |
} |
645 | 644 |
}; |
646 | 645 |
|
647 | 646 |
|
648 | 647 |
template <typename _Value> |
649 | 648 |
class EdgeMap |
650 | 649 |
: public MapExtender<DefaultMap<Graph, Edge, _Value> > { |
651 | 650 |
public: |
652 | 651 |
typedef GraphExtender Graph; |
653 | 652 |
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
654 | 653 |
|
655 | 654 |
EdgeMap(const Graph& graph) |
656 | 655 |
: Parent(graph) {} |
657 | 656 |
|
658 | 657 |
EdgeMap(const Graph& graph, const _Value& value) |
659 | 658 |
: Parent(graph, value) {} |
660 | 659 |
|
661 | 660 |
EdgeMap& operator=(const EdgeMap& cmap) { |
662 | 661 |
return operator=<EdgeMap>(cmap); |
663 | 662 |
} |
664 | 663 |
|
665 | 664 |
template <typename CMap> |
666 | 665 |
EdgeMap& operator=(const CMap& cmap) { |
667 | 666 |
Parent::operator=(cmap); |
668 | 667 |
return *this; |
669 | 668 |
} |
670 | 669 |
|
671 | 670 |
}; |
672 | 671 |
|
673 | 672 |
// Alteration extension |
674 | 673 |
|
675 | 674 |
Node addNode() { |
676 | 675 |
Node node = Parent::addNode(); |
677 | 676 |
notifier(Node()).add(node); |
678 | 677 |
return node; |
679 | 678 |
} |
680 | 679 |
|
681 | 680 |
Edge addEdge(const Node& from, const Node& to) { |
682 | 681 |
Edge edge = Parent::addEdge(from, to); |
683 | 682 |
notifier(Edge()).add(edge); |
684 | 683 |
std::vector<Arc> ev; |
685 | 684 |
ev.push_back(Parent::direct(edge, true)); |
686 | 685 |
ev.push_back(Parent::direct(edge, false)); |
687 | 686 |
notifier(Arc()).add(ev); |
688 | 687 |
return edge; |
689 | 688 |
} |
690 | 689 |
|
691 | 690 |
void clear() { |
692 | 691 |
notifier(Arc()).clear(); |
693 | 692 |
notifier(Edge()).clear(); |
694 | 693 |
notifier(Node()).clear(); |
695 | 694 |
Parent::clear(); |
696 | 695 |
} |
697 | 696 |
|
698 | 697 |
template <typename Graph, typename NodeRefMap, typename EdgeRefMap> |
699 | 698 |
void build(const Graph& graph, NodeRefMap& nodeRef, |
700 | 699 |
EdgeRefMap& edgeRef) { |
701 | 700 |
Parent::build(graph, nodeRef, edgeRef); |
702 | 701 |
notifier(Node()).build(); |
703 | 702 |
notifier(Edge()).build(); |
704 | 703 |
notifier(Arc()).build(); |
705 | 704 |
} |
706 | 705 |
|
707 | 706 |
void erase(const Node& node) { |
708 | 707 |
Arc arc; |
709 | 708 |
Parent::firstOut(arc, node); |
710 | 709 |
while (arc != INVALID ) { |
711 | 710 |
erase(arc); |
712 | 711 |
Parent::firstOut(arc, node); |
713 | 712 |
} |
714 | 713 |
|
715 | 714 |
Parent::firstIn(arc, node); |
716 | 715 |
while (arc != INVALID ) { |
717 | 716 |
erase(arc); |
718 | 717 |
Parent::firstIn(arc, node); |
719 | 718 |
} |
720 | 719 |
|
721 | 720 |
notifier(Node()).erase(node); |
722 | 721 |
Parent::erase(node); |
723 | 722 |
} |
724 | 723 |
|
725 | 724 |
void erase(const Edge& edge) { |
726 | 725 |
std::vector<Arc> av; |
727 | 726 |
av.push_back(Parent::direct(edge, true)); |
728 | 727 |
av.push_back(Parent::direct(edge, false)); |
729 | 728 |
notifier(Arc()).erase(av); |
730 | 729 |
notifier(Edge()).erase(edge); |
731 | 730 |
Parent::erase(edge); |
732 | 731 |
} |
733 | 732 |
|
734 | 733 |
GraphExtender() { |
735 | 734 |
node_notifier.setContainer(*this); |
736 | 735 |
arc_notifier.setContainer(*this); |
737 | 736 |
edge_notifier.setContainer(*this); |
738 | 737 |
} |
739 | 738 |
|
740 | 739 |
~GraphExtender() { |
741 | 740 |
edge_notifier.clear(); |
742 | 741 |
arc_notifier.clear(); |
743 | 742 |
node_notifier.clear(); |
744 | 743 |
} |
745 | 744 |
|
746 | 745 |
}; |
747 | 746 |
|
748 | 747 |
} |
749 | 748 |
|
750 | 749 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_TRAITS_H |
20 | 20 |
#define LEMON_BITS_TRAITS_H |
21 | 21 |
|
22 |
#include <lemon/bits/utility.h> |
|
23 |
|
|
24 | 22 |
///\file |
25 | 23 |
///\brief Traits for graphs and maps |
26 | 24 |
/// |
27 | 25 |
|
26 |
#include <lemon/bits/enable_if.h> |
|
27 |
|
|
28 | 28 |
namespace lemon { |
29 |
|
|
30 |
struct InvalidType {}; |
|
31 |
|
|
29 | 32 |
template <typename _Graph, typename _Item> |
30 | 33 |
class ItemSetTraits {}; |
31 | 34 |
|
32 | 35 |
|
33 | 36 |
template <typename Graph, typename Enable = void> |
34 | 37 |
struct NodeNotifierIndicator { |
35 | 38 |
typedef InvalidType Type; |
36 | 39 |
}; |
37 | 40 |
template <typename Graph> |
38 | 41 |
struct NodeNotifierIndicator< |
39 | 42 |
Graph, |
40 | 43 |
typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type |
41 | 44 |
> { |
42 | 45 |
typedef typename Graph::NodeNotifier Type; |
43 | 46 |
}; |
44 | 47 |
|
45 | 48 |
template <typename _Graph> |
46 | 49 |
class ItemSetTraits<_Graph, typename _Graph::Node> { |
47 | 50 |
public: |
48 | 51 |
|
49 | 52 |
typedef _Graph Graph; |
50 | 53 |
|
51 | 54 |
typedef typename Graph::Node Item; |
52 | 55 |
typedef typename Graph::NodeIt ItemIt; |
53 | 56 |
|
54 | 57 |
typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier; |
55 | 58 |
|
56 | 59 |
template <typename _Value> |
57 | 60 |
class Map : public Graph::template NodeMap<_Value> { |
58 | 61 |
public: |
59 | 62 |
typedef typename Graph::template NodeMap<_Value> Parent; |
60 | 63 |
typedef typename Graph::template NodeMap<_Value> Type; |
61 | 64 |
typedef typename Parent::Value Value; |
62 | 65 |
|
63 | 66 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
64 | 67 |
Map(const Graph& _digraph, const Value& _value) |
65 | 68 |
: Parent(_digraph, _value) {} |
66 | 69 |
|
67 | 70 |
}; |
68 | 71 |
|
69 | 72 |
}; |
70 | 73 |
|
71 | 74 |
template <typename Graph, typename Enable = void> |
72 | 75 |
struct ArcNotifierIndicator { |
73 | 76 |
typedef InvalidType Type; |
74 | 77 |
}; |
75 | 78 |
template <typename Graph> |
76 | 79 |
struct ArcNotifierIndicator< |
77 | 80 |
Graph, |
78 | 81 |
typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type |
79 | 82 |
> { |
80 | 83 |
typedef typename Graph::ArcNotifier Type; |
81 | 84 |
}; |
82 | 85 |
|
83 | 86 |
template <typename _Graph> |
84 | 87 |
class ItemSetTraits<_Graph, typename _Graph::Arc> { |
85 | 88 |
public: |
86 | 89 |
|
87 | 90 |
typedef _Graph Graph; |
88 | 91 |
|
89 | 92 |
typedef typename Graph::Arc Item; |
90 | 93 |
typedef typename Graph::ArcIt ItemIt; |
91 | 94 |
|
92 | 95 |
typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier; |
93 | 96 |
|
94 | 97 |
template <typename _Value> |
95 | 98 |
class Map : public Graph::template ArcMap<_Value> { |
96 | 99 |
public: |
97 | 100 |
typedef typename Graph::template ArcMap<_Value> Parent; |
98 | 101 |
typedef typename Graph::template ArcMap<_Value> Type; |
99 | 102 |
typedef typename Parent::Value Value; |
100 | 103 |
|
101 | 104 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
102 | 105 |
Map(const Graph& _digraph, const Value& _value) |
103 | 106 |
: Parent(_digraph, _value) {} |
104 | 107 |
}; |
105 | 108 |
|
106 | 109 |
}; |
107 | 110 |
|
108 | 111 |
template <typename Graph, typename Enable = void> |
109 | 112 |
struct EdgeNotifierIndicator { |
110 | 113 |
typedef InvalidType Type; |
111 | 114 |
}; |
112 | 115 |
template <typename Graph> |
113 | 116 |
struct EdgeNotifierIndicator< |
114 | 117 |
Graph, |
115 | 118 |
typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type |
116 | 119 |
> { |
117 | 120 |
typedef typename Graph::EdgeNotifier Type; |
118 | 121 |
}; |
119 | 122 |
|
120 | 123 |
template <typename _Graph> |
121 | 124 |
class ItemSetTraits<_Graph, typename _Graph::Edge> { |
122 | 125 |
public: |
123 | 126 |
|
124 | 127 |
typedef _Graph Graph; |
125 | 128 |
|
126 | 129 |
typedef typename Graph::Edge Item; |
127 | 130 |
typedef typename Graph::EdgeIt ItemIt; |
128 | 131 |
|
129 | 132 |
typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier; |
130 | 133 |
|
131 | 134 |
template <typename _Value> |
132 | 135 |
class Map : public Graph::template EdgeMap<_Value> { |
133 | 136 |
public: |
134 | 137 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
135 | 138 |
typedef typename Graph::template EdgeMap<_Value> Type; |
136 | 139 |
typedef typename Parent::Value Value; |
137 | 140 |
|
138 | 141 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
139 | 142 |
Map(const Graph& _digraph, const Value& _value) |
140 | 143 |
: Parent(_digraph, _value) {} |
141 | 144 |
}; |
142 | 145 |
|
143 | 146 |
}; |
144 | 147 |
|
145 | 148 |
template <typename Map, typename Enable = void> |
146 | 149 |
struct MapTraits { |
147 | 150 |
typedef False ReferenceMapTag; |
148 | 151 |
|
149 | 152 |
typedef typename Map::Key Key; |
150 | 153 |
typedef typename Map::Value Value; |
151 | 154 |
|
152 | 155 |
typedef Value ConstReturnValue; |
153 | 156 |
typedef Value ReturnValue; |
154 | 157 |
}; |
155 | 158 |
|
156 | 159 |
template <typename Map> |
157 | 160 |
struct MapTraits< |
158 | 161 |
Map, typename enable_if<typename Map::ReferenceMapTag, void>::type > |
159 | 162 |
{ |
160 | 163 |
typedef True ReferenceMapTag; |
161 | 164 |
|
162 | 165 |
typedef typename Map::Key Key; |
163 | 166 |
typedef typename Map::Value Value; |
164 | 167 |
|
165 | 168 |
typedef typename Map::ConstReference ConstReturnValue; |
166 | 169 |
typedef typename Map::Reference ReturnValue; |
167 | 170 |
|
168 | 171 |
typedef typename Map::ConstReference ConstReference; |
169 | 172 |
typedef typename Map::Reference Reference; |
170 | 173 |
}; |
171 | 174 |
|
172 | 175 |
template <typename MatrixMap, typename Enable = void> |
173 | 176 |
struct MatrixMapTraits { |
174 | 177 |
typedef False ReferenceMapTag; |
175 | 178 |
|
176 | 179 |
typedef typename MatrixMap::FirstKey FirstKey; |
177 | 180 |
typedef typename MatrixMap::SecondKey SecondKey; |
178 | 181 |
typedef typename MatrixMap::Value Value; |
179 | 182 |
|
180 | 183 |
typedef Value ConstReturnValue; |
181 | 184 |
typedef Value ReturnValue; |
182 | 185 |
}; |
183 | 186 |
|
184 | 187 |
template <typename MatrixMap> |
185 | 188 |
struct MatrixMapTraits< |
186 | 189 |
MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag, |
187 | 190 |
void>::type > |
188 | 191 |
{ |
189 | 192 |
typedef True ReferenceMapTag; |
190 | 193 |
|
191 | 194 |
typedef typename MatrixMap::FirstKey FirstKey; |
192 | 195 |
typedef typename MatrixMap::SecondKey SecondKey; |
193 | 196 |
typedef typename MatrixMap::Value Value; |
194 | 197 |
|
195 | 198 |
typedef typename MatrixMap::ConstReference ConstReturnValue; |
196 | 199 |
typedef typename MatrixMap::Reference ReturnValue; |
197 | 200 |
|
198 | 201 |
typedef typename MatrixMap::ConstReference ConstReference; |
199 | 202 |
typedef typename MatrixMap::Reference Reference; |
200 | 203 |
}; |
201 | 204 |
|
202 | 205 |
// Indicators for the tags |
203 | 206 |
|
204 | 207 |
template <typename Graph, typename Enable = void> |
205 | 208 |
struct NodeNumTagIndicator { |
206 | 209 |
static const bool value = false; |
207 | 210 |
}; |
208 | 211 |
|
209 | 212 |
template <typename Graph> |
210 | 213 |
struct NodeNumTagIndicator< |
211 | 214 |
Graph, |
212 | 215 |
typename enable_if<typename Graph::NodeNumTag, void>::type |
213 | 216 |
> { |
214 | 217 |
static const bool value = true; |
215 | 218 |
}; |
216 | 219 |
|
217 | 220 |
template <typename Graph, typename Enable = void> |
218 | 221 |
struct EdgeNumTagIndicator { |
219 | 222 |
static const bool value = false; |
220 | 223 |
}; |
221 | 224 |
|
222 | 225 |
template <typename Graph> |
223 | 226 |
struct EdgeNumTagIndicator< |
224 | 227 |
Graph, |
225 | 228 |
typename enable_if<typename Graph::EdgeNumTag, void>::type |
226 | 229 |
> { |
227 | 230 |
static const bool value = true; |
228 | 231 |
}; |
229 | 232 |
|
230 | 233 |
template <typename Graph, typename Enable = void> |
231 | 234 |
struct FindEdgeTagIndicator { |
232 | 235 |
static const bool value = false; |
233 | 236 |
}; |
234 | 237 |
|
235 | 238 |
template <typename Graph> |
236 | 239 |
struct FindEdgeTagIndicator< |
237 | 240 |
Graph, |
238 | 241 |
typename enable_if<typename Graph::FindEdgeTag, void>::type |
239 | 242 |
> { |
240 | 243 |
static const bool value = true; |
241 | 244 |
}; |
242 | 245 |
|
243 | 246 |
template <typename Graph, typename Enable = void> |
244 | 247 |
struct UndirectedTagIndicator { |
245 | 248 |
static const bool value = false; |
246 | 249 |
}; |
247 | 250 |
|
248 | 251 |
template <typename Graph> |
249 | 252 |
struct UndirectedTagIndicator< |
250 | 253 |
Graph, |
251 | 254 |
typename enable_if<typename Graph::UndirectedTag, void>::type |
252 | 255 |
> { |
253 | 256 |
static const bool value = true; |
254 | 257 |
}; |
255 | 258 |
|
256 | 259 |
template <typename Graph, typename Enable = void> |
257 | 260 |
struct BuildTagIndicator { |
258 | 261 |
static const bool value = false; |
259 | 262 |
}; |
260 | 263 |
|
261 | 264 |
template <typename Graph> |
262 | 265 |
struct BuildTagIndicator< |
263 | 266 |
Graph, |
264 | 267 |
typename enable_if<typename Graph::BuildTag, void>::type |
265 | 268 |
> { |
266 | 269 |
static const bool value = true; |
267 | 270 |
}; |
268 | 271 |
|
269 | 272 |
} |
270 | 273 |
|
271 | 274 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_VECTOR_MAP_H |
20 | 20 |
#define LEMON_BITS_VECTOR_MAP_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 |
#include <lemon/bits/traits.h> |
|
26 |
#include <lemon/bits/utility.h> |
|
27 |
|
|
25 |
#include <lemon/core.h> |
|
28 | 26 |
#include <lemon/bits/alteration_notifier.h> |
29 | 27 |
|
30 | 28 |
#include <lemon/concept_check.h> |
31 | 29 |
#include <lemon/concepts/maps.h> |
32 | 30 |
|
33 | 31 |
///\ingroup graphbits |
34 | 32 |
/// |
35 | 33 |
///\file |
36 | 34 |
///\brief Vector based graph maps. |
37 | 35 |
namespace lemon { |
38 | 36 |
|
39 | 37 |
/// \ingroup graphbits |
40 | 38 |
/// |
41 | 39 |
/// \brief Graph map based on the std::vector storage. |
42 | 40 |
/// |
43 | 41 |
/// The VectorMap template class is graph map structure what |
44 | 42 |
/// automatically updates the map when a key is added to or erased from |
45 | 43 |
/// the map. This map type uses the std::vector to store the values. |
46 | 44 |
/// |
47 | 45 |
/// \tparam _Notifier The AlterationNotifier that will notify this map. |
48 | 46 |
/// \tparam _Item The item type of the graph items. |
49 | 47 |
/// \tparam _Value The value type of the map. |
50 | 48 |
/// \todo Fix the doc: there is _Graph parameter instead of _Notifier. |
51 | 49 |
template <typename _Graph, typename _Item, typename _Value> |
52 | 50 |
class VectorMap |
53 | 51 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
54 | 52 |
private: |
55 | 53 |
|
56 | 54 |
/// The container type of the map. |
57 | 55 |
typedef std::vector<_Value> Container; |
58 | 56 |
|
59 | 57 |
public: |
60 | 58 |
|
61 | 59 |
/// The graph type of the map. |
62 | 60 |
typedef _Graph Graph; |
63 | 61 |
/// The item type of the map. |
64 | 62 |
typedef _Item Item; |
65 | 63 |
/// The reference map tag. |
66 | 64 |
typedef True ReferenceMapTag; |
67 | 65 |
|
68 | 66 |
/// The key type of the map. |
69 | 67 |
typedef _Item Key; |
70 | 68 |
/// The value type of the map. |
71 | 69 |
typedef _Value Value; |
72 | 70 |
|
73 | 71 |
/// The notifier type. |
74 | 72 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
75 | 73 |
|
76 | 74 |
/// The map type. |
77 | 75 |
typedef VectorMap Map; |
78 | 76 |
/// The base class of the map. |
79 | 77 |
typedef typename Notifier::ObserverBase Parent; |
80 | 78 |
|
81 | 79 |
/// The reference type of the map; |
82 | 80 |
typedef typename Container::reference Reference; |
83 | 81 |
/// The const reference type of the map; |
84 | 82 |
typedef typename Container::const_reference ConstReference; |
85 | 83 |
|
86 | 84 |
|
87 | 85 |
/// \brief Constructor to attach the new map into the notifier. |
88 | 86 |
/// |
89 | 87 |
/// It constructs a map and attachs it into the notifier. |
90 | 88 |
/// It adds all the items of the graph to the map. |
91 | 89 |
VectorMap(const Graph& graph) { |
92 | 90 |
Parent::attach(graph.notifier(Item())); |
93 | 91 |
container.resize(Parent::notifier()->maxId() + 1); |
94 | 92 |
} |
95 | 93 |
|
96 | 94 |
/// \brief Constructor uses given value to initialize the map. |
97 | 95 |
/// |
98 | 96 |
/// It constructs a map uses a given value to initialize the map. |
99 | 97 |
/// It adds all the items of the graph to the map. |
100 | 98 |
VectorMap(const Graph& graph, const Value& value) { |
101 | 99 |
Parent::attach(graph.notifier(Item())); |
102 | 100 |
container.resize(Parent::notifier()->maxId() + 1, value); |
103 | 101 |
} |
104 | 102 |
|
105 | 103 |
/// \brief Copy constructor |
106 | 104 |
/// |
107 | 105 |
/// Copy constructor. |
108 | 106 |
VectorMap(const VectorMap& _copy) : Parent() { |
109 | 107 |
if (_copy.attached()) { |
110 | 108 |
Parent::attach(*_copy.notifier()); |
111 | 109 |
container = _copy.container; |
112 | 110 |
} |
113 | 111 |
} |
114 | 112 |
|
115 | 113 |
/// \brief Assign operator. |
116 | 114 |
/// |
117 | 115 |
/// This operator assigns for each item in the map the |
118 | 116 |
/// value mapped to the same item in the copied map. |
119 | 117 |
/// The parameter map should be indiced with the same |
120 | 118 |
/// itemset because this assign operator does not change |
121 | 119 |
/// the container of the map. |
122 | 120 |
VectorMap& operator=(const VectorMap& cmap) { |
123 | 121 |
return operator=<VectorMap>(cmap); |
124 | 122 |
} |
125 | 123 |
|
126 | 124 |
|
127 | 125 |
/// \brief Template assign operator. |
128 | 126 |
/// |
129 | 127 |
/// The given parameter should be conform to the ReadMap |
130 | 128 |
/// concecpt and could be indiced by the current item set of |
131 | 129 |
/// the NodeMap. In this case the value for each item |
132 | 130 |
/// is assigned by the value of the given ReadMap. |
133 | 131 |
template <typename CMap> |
134 | 132 |
VectorMap& operator=(const CMap& cmap) { |
135 | 133 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
136 | 134 |
const typename Parent::Notifier* nf = Parent::notifier(); |
137 | 135 |
Item it; |
138 | 136 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
139 | 137 |
set(it, cmap[it]); |
140 | 138 |
} |
141 | 139 |
return *this; |
142 | 140 |
} |
143 | 141 |
|
144 | 142 |
public: |
145 | 143 |
|
146 | 144 |
/// \brief The subcript operator. |
147 | 145 |
/// |
148 | 146 |
/// The subscript operator. The map can be subscripted by the |
149 | 147 |
/// actual items of the graph. |
150 | 148 |
Reference operator[](const Key& key) { |
151 | 149 |
return container[Parent::notifier()->id(key)]; |
152 | 150 |
} |
153 | 151 |
|
154 | 152 |
/// \brief The const subcript operator. |
155 | 153 |
/// |
156 | 154 |
/// The const subscript operator. The map can be subscripted by the |
157 | 155 |
/// actual items of the graph. |
158 | 156 |
ConstReference operator[](const Key& key) const { |
159 | 157 |
return container[Parent::notifier()->id(key)]; |
160 | 158 |
} |
161 | 159 |
|
162 | 160 |
|
163 | 161 |
/// \brief The setter function of the map. |
164 | 162 |
/// |
165 | 163 |
/// It the same as operator[](key) = value expression. |
166 | 164 |
void set(const Key& key, const Value& value) { |
167 | 165 |
(*this)[key] = value; |
168 | 166 |
} |
169 | 167 |
|
170 | 168 |
protected: |
171 | 169 |
|
172 | 170 |
/// \brief Adds a new key to the map. |
173 | 171 |
/// |
174 | 172 |
/// It adds a new key to the map. It called by the observer notifier |
175 | 173 |
/// and it overrides the add() member function of the observer base. |
176 | 174 |
virtual void add(const Key& key) { |
177 | 175 |
int id = Parent::notifier()->id(key); |
178 | 176 |
if (id >= int(container.size())) { |
179 | 177 |
container.resize(id + 1); |
180 | 178 |
} |
181 | 179 |
} |
182 | 180 |
|
183 | 181 |
/// \brief Adds more new keys to the map. |
184 | 182 |
/// |
185 | 183 |
/// It adds more new keys to the map. It called by the observer notifier |
186 | 184 |
/// and it overrides the add() member function of the observer base. |
187 | 185 |
virtual void add(const std::vector<Key>& keys) { |
188 | 186 |
int max = container.size() - 1; |
189 | 187 |
for (int i = 0; i < int(keys.size()); ++i) { |
190 | 188 |
int id = Parent::notifier()->id(keys[i]); |
191 | 189 |
if (id >= max) { |
192 | 190 |
max = id; |
193 | 191 |
} |
194 | 192 |
} |
195 | 193 |
container.resize(max + 1); |
196 | 194 |
} |
197 | 195 |
|
198 | 196 |
/// \brief Erase a key from the map. |
199 | 197 |
/// |
200 | 198 |
/// Erase a key from the map. It called by the observer notifier |
201 | 199 |
/// and it overrides the erase() member function of the observer base. |
202 | 200 |
virtual void erase(const Key& key) { |
203 | 201 |
container[Parent::notifier()->id(key)] = Value(); |
204 | 202 |
} |
205 | 203 |
|
206 | 204 |
/// \brief Erase more keys from the map. |
207 | 205 |
/// |
208 | 206 |
/// Erase more keys from the map. It called by the observer notifier |
209 | 207 |
/// and it overrides the erase() member function of the observer base. |
210 | 208 |
virtual void erase(const std::vector<Key>& keys) { |
211 | 209 |
for (int i = 0; i < int(keys.size()); ++i) { |
212 | 210 |
container[Parent::notifier()->id(keys[i])] = Value(); |
213 | 211 |
} |
214 | 212 |
} |
215 | 213 |
|
216 | 214 |
/// \brief Buildes the map. |
217 | 215 |
/// |
218 | 216 |
/// It buildes the map. It called by the observer notifier |
219 | 217 |
/// and it overrides the build() member function of the observer base. |
220 | 218 |
virtual void build() { |
221 | 219 |
int size = Parent::notifier()->maxId() + 1; |
222 | 220 |
container.reserve(size); |
223 | 221 |
container.resize(size); |
224 | 222 |
} |
225 | 223 |
|
226 | 224 |
/// \brief Clear the map. |
227 | 225 |
/// |
228 | 226 |
/// It erase all items from the map. It called by the observer notifier |
229 | 227 |
/// and it overrides the clear() member function of the observer base. |
230 | 228 |
virtual void clear() { |
231 | 229 |
container.clear(); |
232 | 230 |
} |
233 | 231 |
|
234 | 232 |
private: |
235 | 233 |
|
236 | 234 |
Container container; |
237 | 235 |
|
238 | 236 |
}; |
239 | 237 |
|
240 | 238 |
} |
241 | 239 |
|
242 | 240 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONCEPT_DIGRAPH_H |
20 | 20 |
#define LEMON_CONCEPT_DIGRAPH_H |
21 | 21 |
|
22 | 22 |
///\ingroup graph_concepts |
23 | 23 |
///\file |
24 | 24 |
///\brief The concept of directed graphs. |
25 | 25 |
|
26 |
#include <lemon/bits/invalid.h> |
|
27 |
#include <lemon/bits/utility.h> |
|
26 |
#include <lemon/core.h> |
|
28 | 27 |
#include <lemon/concepts/maps.h> |
29 | 28 |
#include <lemon/concept_check.h> |
30 | 29 |
#include <lemon/concepts/graph_components.h> |
31 | 30 |
|
32 | 31 |
namespace lemon { |
33 | 32 |
namespace concepts { |
34 | 33 |
|
35 | 34 |
/// \ingroup graph_concepts |
36 | 35 |
/// |
37 | 36 |
/// \brief Class describing the concept of directed graphs. |
38 | 37 |
/// |
39 | 38 |
/// This class describes the \ref concept "concept" of the |
40 | 39 |
/// immutable directed digraphs. |
41 | 40 |
/// |
42 | 41 |
/// Note that actual digraph implementation like @ref ListDigraph or |
43 | 42 |
/// @ref SmartDigraph may have several additional functionality. |
44 | 43 |
/// |
45 | 44 |
/// \sa concept |
46 | 45 |
class Digraph { |
47 | 46 |
private: |
48 | 47 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
49 | 48 |
|
50 | 49 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
51 | 50 |
/// |
52 | 51 |
Digraph(const Digraph &) {}; |
53 | 52 |
///\brief Assignment of \ref Digraph "Digraph"s to another ones are |
54 | 53 |
///\e not allowed. Use DigraphCopy() instead. |
55 | 54 |
|
56 | 55 |
///Assignment of \ref Digraph "Digraph"s to another ones are |
57 | 56 |
///\e not allowed. Use DigraphCopy() instead. |
58 | 57 |
|
59 | 58 |
void operator=(const Digraph &) {} |
60 | 59 |
public: |
61 | 60 |
///\e |
62 | 61 |
|
63 | 62 |
/// Defalult constructor. |
64 | 63 |
|
65 | 64 |
/// Defalult constructor. |
66 | 65 |
/// |
67 | 66 |
Digraph() { } |
68 | 67 |
/// Class for identifying a node of the digraph |
69 | 68 |
|
70 | 69 |
/// This class identifies a node of the digraph. It also serves |
71 | 70 |
/// as a base class of the node iterators, |
72 | 71 |
/// thus they will convert to this type. |
73 | 72 |
class Node { |
74 | 73 |
public: |
75 | 74 |
/// Default constructor |
76 | 75 |
|
77 | 76 |
/// @warning The default constructor sets the iterator |
78 | 77 |
/// to an undefined value. |
79 | 78 |
Node() { } |
80 | 79 |
/// Copy constructor. |
81 | 80 |
|
82 | 81 |
/// Copy constructor. |
83 | 82 |
/// |
84 | 83 |
Node(const Node&) { } |
85 | 84 |
|
86 | 85 |
/// Invalid constructor \& conversion. |
87 | 86 |
|
88 | 87 |
/// This constructor initializes the iterator to be invalid. |
89 | 88 |
/// \sa Invalid for more details. |
90 | 89 |
Node(Invalid) { } |
91 | 90 |
/// Equality operator |
92 | 91 |
|
93 | 92 |
/// Two iterators are equal if and only if they point to the |
94 | 93 |
/// same object or both are invalid. |
95 | 94 |
bool operator==(Node) const { return true; } |
96 | 95 |
|
97 | 96 |
/// Inequality operator |
98 | 97 |
|
99 | 98 |
/// \sa operator==(Node n) |
100 | 99 |
/// |
101 | 100 |
bool operator!=(Node) const { return true; } |
102 | 101 |
|
103 | 102 |
/// Artificial ordering operator. |
104 | 103 |
|
105 | 104 |
/// To allow the use of digraph descriptors as key type in std::map or |
106 | 105 |
/// similar associative container we require this. |
107 | 106 |
/// |
108 | 107 |
/// \note This operator only have to define some strict ordering of |
109 | 108 |
/// the items; this order has nothing to do with the iteration |
110 | 109 |
/// ordering of the items. |
111 | 110 |
bool operator<(Node) const { return false; } |
112 | 111 |
|
113 | 112 |
}; |
114 | 113 |
|
115 | 114 |
/// This iterator goes through each node. |
116 | 115 |
|
117 | 116 |
/// This iterator goes through each node. |
118 | 117 |
/// Its usage is quite simple, for example you can count the number |
119 | 118 |
/// of nodes in digraph \c g of type \c Digraph like this: |
120 | 119 |
///\code |
121 | 120 |
/// int count=0; |
122 | 121 |
/// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count; |
123 | 122 |
///\endcode |
124 | 123 |
class NodeIt : public Node { |
125 | 124 |
public: |
126 | 125 |
/// Default constructor |
127 | 126 |
|
128 | 127 |
/// @warning The default constructor sets the iterator |
129 | 128 |
/// to an undefined value. |
130 | 129 |
NodeIt() { } |
131 | 130 |
/// Copy constructor. |
132 | 131 |
|
133 | 132 |
/// Copy constructor. |
134 | 133 |
/// |
135 | 134 |
NodeIt(const NodeIt& n) : Node(n) { } |
136 | 135 |
/// Invalid constructor \& conversion. |
137 | 136 |
|
138 | 137 |
/// Initialize the iterator to be invalid. |
139 | 138 |
/// \sa Invalid for more details. |
140 | 139 |
NodeIt(Invalid) { } |
141 | 140 |
/// Sets the iterator to the first node. |
142 | 141 |
|
143 | 142 |
/// Sets the iterator to the first node of \c g. |
144 | 143 |
/// |
145 | 144 |
NodeIt(const Digraph&) { } |
146 | 145 |
/// Node -> NodeIt conversion. |
147 | 146 |
|
148 | 147 |
/// Sets the iterator to the node of \c the digraph pointed by |
149 | 148 |
/// the trivial iterator. |
150 | 149 |
/// This feature necessitates that each time we |
151 | 150 |
/// iterate the arc-set, the iteration order is the same. |
152 | 151 |
NodeIt(const Digraph&, const Node&) { } |
153 | 152 |
/// Next node. |
154 | 153 |
|
155 | 154 |
/// Assign the iterator to the next node. |
156 | 155 |
/// |
157 | 156 |
NodeIt& operator++() { return *this; } |
158 | 157 |
}; |
159 | 158 |
|
160 | 159 |
|
161 | 160 |
/// Class for identifying an arc of the digraph |
162 | 161 |
|
163 | 162 |
/// This class identifies an arc of the digraph. It also serves |
164 | 163 |
/// as a base class of the arc iterators, |
165 | 164 |
/// thus they will convert to this type. |
166 | 165 |
class Arc { |
167 | 166 |
public: |
168 | 167 |
/// Default constructor |
169 | 168 |
|
170 | 169 |
/// @warning The default constructor sets the iterator |
171 | 170 |
/// to an undefined value. |
172 | 171 |
Arc() { } |
173 | 172 |
/// Copy constructor. |
174 | 173 |
|
175 | 174 |
/// Copy constructor. |
176 | 175 |
/// |
177 | 176 |
Arc(const Arc&) { } |
178 | 177 |
/// Initialize the iterator to be invalid. |
179 | 178 |
|
180 | 179 |
/// Initialize the iterator to be invalid. |
181 | 180 |
/// |
182 | 181 |
Arc(Invalid) { } |
183 | 182 |
/// Equality operator |
184 | 183 |
|
185 | 184 |
/// Two iterators are equal if and only if they point to the |
186 | 185 |
/// same object or both are invalid. |
187 | 186 |
bool operator==(Arc) const { return true; } |
188 | 187 |
/// Inequality operator |
189 | 188 |
|
190 | 189 |
/// \sa operator==(Arc n) |
191 | 190 |
/// |
192 | 191 |
bool operator!=(Arc) const { return true; } |
193 | 192 |
|
194 | 193 |
/// Artificial ordering operator. |
195 | 194 |
|
196 | 195 |
/// To allow the use of digraph descriptors as key type in std::map or |
197 | 196 |
/// similar associative container we require this. |
198 | 197 |
/// |
199 | 198 |
/// \note This operator only have to define some strict ordering of |
200 | 199 |
/// the items; this order has nothing to do with the iteration |
201 | 200 |
/// ordering of the items. |
202 | 201 |
bool operator<(Arc) const { return false; } |
203 | 202 |
}; |
204 | 203 |
|
205 | 204 |
/// This iterator goes trough the outgoing arcs of a node. |
206 | 205 |
|
207 | 206 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
208 | 207 |
/// of a digraph. |
209 | 208 |
/// Its usage is quite simple, for example you can count the number |
210 | 209 |
/// of outgoing arcs of a node \c n |
211 | 210 |
/// in digraph \c g of type \c Digraph as follows. |
212 | 211 |
///\code |
213 | 212 |
/// int count=0; |
214 | 213 |
/// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count; |
215 | 214 |
///\endcode |
216 | 215 |
|
217 | 216 |
class OutArcIt : public Arc { |
218 | 217 |
public: |
219 | 218 |
/// Default constructor |
220 | 219 |
|
221 | 220 |
/// @warning The default constructor sets the iterator |
222 | 221 |
/// to an undefined value. |
223 | 222 |
OutArcIt() { } |
224 | 223 |
/// Copy constructor. |
225 | 224 |
|
226 | 225 |
/// Copy constructor. |
227 | 226 |
/// |
228 | 227 |
OutArcIt(const OutArcIt& e) : Arc(e) { } |
229 | 228 |
/// Initialize the iterator to be invalid. |
230 | 229 |
|
231 | 230 |
/// Initialize the iterator to be invalid. |
232 | 231 |
/// |
233 | 232 |
OutArcIt(Invalid) { } |
234 | 233 |
/// This constructor sets the iterator to the first outgoing arc. |
235 | 234 |
|
236 | 235 |
/// This constructor sets the iterator to the first outgoing arc of |
237 | 236 |
/// the node. |
238 | 237 |
OutArcIt(const Digraph&, const Node&) { } |
239 | 238 |
/// Arc -> OutArcIt conversion |
240 | 239 |
|
241 | 240 |
/// Sets the iterator to the value of the trivial iterator. |
242 | 241 |
/// This feature necessitates that each time we |
243 | 242 |
/// iterate the arc-set, the iteration order is the same. |
244 | 243 |
OutArcIt(const Digraph&, const Arc&) { } |
245 | 244 |
///Next outgoing arc |
246 | 245 |
|
247 | 246 |
/// Assign the iterator to the next |
248 | 247 |
/// outgoing arc of the corresponding node. |
249 | 248 |
OutArcIt& operator++() { return *this; } |
250 | 249 |
}; |
251 | 250 |
|
252 | 251 |
/// This iterator goes trough the incoming arcs of a node. |
253 | 252 |
|
254 | 253 |
/// This iterator goes trough the \e incoming arcs of a certain node |
255 | 254 |
/// of a digraph. |
256 | 255 |
/// Its usage is quite simple, for example you can count the number |
257 | 256 |
/// of outgoing arcs of a node \c n |
258 | 257 |
/// in digraph \c g of type \c Digraph as follows. |
259 | 258 |
///\code |
260 | 259 |
/// int count=0; |
261 | 260 |
/// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count; |
262 | 261 |
///\endcode |
263 | 262 |
|
264 | 263 |
class InArcIt : public Arc { |
265 | 264 |
public: |
266 | 265 |
/// Default constructor |
267 | 266 |
|
268 | 267 |
/// @warning The default constructor sets the iterator |
269 | 268 |
/// to an undefined value. |
270 | 269 |
InArcIt() { } |
271 | 270 |
/// Copy constructor. |
272 | 271 |
|
273 | 272 |
/// Copy constructor. |
274 | 273 |
/// |
275 | 274 |
InArcIt(const InArcIt& e) : Arc(e) { } |
276 | 275 |
/// Initialize the iterator to be invalid. |
277 | 276 |
|
278 | 277 |
/// Initialize the iterator to be invalid. |
279 | 278 |
/// |
280 | 279 |
InArcIt(Invalid) { } |
281 | 280 |
/// This constructor sets the iterator to first incoming arc. |
282 | 281 |
|
283 | 282 |
/// This constructor set the iterator to the first incoming arc of |
284 | 283 |
/// the node. |
285 | 284 |
InArcIt(const Digraph&, const Node&) { } |
286 | 285 |
/// Arc -> InArcIt conversion |
287 | 286 |
|
288 | 287 |
/// Sets the iterator to the value of the trivial iterator \c e. |
289 | 288 |
/// This feature necessitates that each time we |
290 | 289 |
/// iterate the arc-set, the iteration order is the same. |
291 | 290 |
InArcIt(const Digraph&, const Arc&) { } |
292 | 291 |
/// Next incoming arc |
293 | 292 |
|
294 | 293 |
/// Assign the iterator to the next inarc of the corresponding node. |
295 | 294 |
/// |
296 | 295 |
InArcIt& operator++() { return *this; } |
297 | 296 |
}; |
298 | 297 |
/// This iterator goes through each arc. |
299 | 298 |
|
300 | 299 |
/// This iterator goes through each arc of a digraph. |
301 | 300 |
/// Its usage is quite simple, for example you can count the number |
302 | 301 |
/// of arcs in a digraph \c g of type \c Digraph as follows: |
303 | 302 |
///\code |
304 | 303 |
/// int count=0; |
305 | 304 |
/// for(Digraph::ArcIt e(g); e!=INVALID; ++e) ++count; |
306 | 305 |
///\endcode |
307 | 306 |
class ArcIt : public Arc { |
308 | 307 |
public: |
309 | 308 |
/// Default constructor |
310 | 309 |
|
311 | 310 |
/// @warning The default constructor sets the iterator |
312 | 311 |
/// to an undefined value. |
313 | 312 |
ArcIt() { } |
314 | 313 |
/// Copy constructor. |
315 | 314 |
|
316 | 315 |
/// Copy constructor. |
317 | 316 |
/// |
318 | 317 |
ArcIt(const ArcIt& e) : Arc(e) { } |
319 | 318 |
/// Initialize the iterator to be invalid. |
320 | 319 |
|
321 | 320 |
/// Initialize the iterator to be invalid. |
322 | 321 |
/// |
323 | 322 |
ArcIt(Invalid) { } |
324 | 323 |
/// This constructor sets the iterator to the first arc. |
325 | 324 |
|
326 | 325 |
/// This constructor sets the iterator to the first arc of \c g. |
327 | 326 |
///@param g the digraph |
328 | 327 |
ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); } |
329 | 328 |
/// Arc -> ArcIt conversion |
330 | 329 |
|
331 | 330 |
/// Sets the iterator to the value of the trivial iterator \c e. |
332 | 331 |
/// This feature necessitates that each time we |
333 | 332 |
/// iterate the arc-set, the iteration order is the same. |
334 | 333 |
ArcIt(const Digraph&, const Arc&) { } |
335 | 334 |
///Next arc |
336 | 335 |
|
337 | 336 |
/// Assign the iterator to the next arc. |
338 | 337 |
ArcIt& operator++() { return *this; } |
339 | 338 |
}; |
340 | 339 |
///Gives back the target node of an arc. |
341 | 340 |
|
342 | 341 |
///Gives back the target node of an arc. |
343 | 342 |
/// |
344 | 343 |
Node target(Arc) const { return INVALID; } |
345 | 344 |
///Gives back the source node of an arc. |
346 | 345 |
|
347 | 346 |
///Gives back the source node of an arc. |
348 | 347 |
/// |
349 | 348 |
Node source(Arc) const { return INVALID; } |
350 | 349 |
|
351 | 350 |
/// \brief Returns the ID of the node. |
352 | 351 |
int id(Node) const { return -1; } |
353 | 352 |
|
354 | 353 |
/// \brief Returns the ID of the arc. |
355 | 354 |
int id(Arc) const { return -1; } |
356 | 355 |
|
357 | 356 |
/// \brief Returns the node with the given ID. |
358 | 357 |
/// |
359 | 358 |
/// \pre The argument should be a valid node ID in the graph. |
360 | 359 |
Node nodeFromId(int) const { return INVALID; } |
361 | 360 |
|
362 | 361 |
/// \brief Returns the arc with the given ID. |
363 | 362 |
/// |
364 | 363 |
/// \pre The argument should be a valid arc ID in the graph. |
365 | 364 |
Arc arcFromId(int) const { return INVALID; } |
366 | 365 |
|
367 | 366 |
/// \brief Returns an upper bound on the node IDs. |
368 | 367 |
int maxNodeId() const { return -1; } |
369 | 368 |
|
370 | 369 |
/// \brief Returns an upper bound on the arc IDs. |
371 | 370 |
int maxArcId() const { return -1; } |
372 | 371 |
|
373 | 372 |
void first(Node&) const {} |
374 | 373 |
void next(Node&) const {} |
375 | 374 |
|
376 | 375 |
void first(Arc&) const {} |
377 | 376 |
void next(Arc&) const {} |
378 | 377 |
|
379 | 378 |
|
380 | 379 |
void firstIn(Arc&, const Node&) const {} |
381 | 380 |
void nextIn(Arc&) const {} |
382 | 381 |
|
383 | 382 |
void firstOut(Arc&, const Node&) const {} |
384 | 383 |
void nextOut(Arc&) const {} |
385 | 384 |
|
386 | 385 |
// The second parameter is dummy. |
387 | 386 |
Node fromId(int, Node) const { return INVALID; } |
388 | 387 |
// The second parameter is dummy. |
389 | 388 |
Arc fromId(int, Arc) const { return INVALID; } |
390 | 389 |
|
391 | 390 |
// Dummy parameter. |
392 | 391 |
int maxId(Node) const { return -1; } |
393 | 392 |
// Dummy parameter. |
394 | 393 |
int maxId(Arc) const { return -1; } |
395 | 394 |
|
396 | 395 |
/// \brief The base node of the iterator. |
397 | 396 |
/// |
398 | 397 |
/// Gives back the base node of the iterator. |
399 | 398 |
/// It is always the target of the pointed arc. |
400 | 399 |
Node baseNode(const InArcIt&) const { return INVALID; } |
401 | 400 |
|
402 | 401 |
/// \brief The running node of the iterator. |
403 | 402 |
/// |
404 | 403 |
/// Gives back the running node of the iterator. |
405 | 404 |
/// It is always the source of the pointed arc. |
406 | 405 |
Node runningNode(const InArcIt&) const { return INVALID; } |
407 | 406 |
|
408 | 407 |
/// \brief The base node of the iterator. |
409 | 408 |
/// |
410 | 409 |
/// Gives back the base node of the iterator. |
411 | 410 |
/// It is always the source of the pointed arc. |
412 | 411 |
Node baseNode(const OutArcIt&) const { return INVALID; } |
413 | 412 |
|
414 | 413 |
/// \brief The running node of the iterator. |
415 | 414 |
/// |
416 | 415 |
/// Gives back the running node of the iterator. |
417 | 416 |
/// It is always the target of the pointed arc. |
418 | 417 |
Node runningNode(const OutArcIt&) const { return INVALID; } |
419 | 418 |
|
420 | 419 |
/// \brief The opposite node on the given arc. |
421 | 420 |
/// |
422 | 421 |
/// Gives back the opposite node on the given arc. |
423 | 422 |
Node oppositeNode(const Node&, const Arc&) const { return INVALID; } |
424 | 423 |
|
425 | 424 |
/// \brief Read write map of the nodes to type \c T. |
426 | 425 |
/// |
427 | 426 |
/// ReadWrite map of the nodes to type \c T. |
428 | 427 |
/// \sa Reference |
429 | 428 |
template<class T> |
430 | 429 |
class NodeMap : public ReadWriteMap< Node, T > { |
431 | 430 |
public: |
432 | 431 |
|
433 | 432 |
///\e |
434 | 433 |
NodeMap(const Digraph&) { } |
435 | 434 |
///\e |
436 | 435 |
NodeMap(const Digraph&, T) { } |
437 | 436 |
|
438 | 437 |
///Copy constructor |
439 | 438 |
NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { } |
440 | 439 |
///Assignment operator |
441 | 440 |
template <typename CMap> |
442 | 441 |
NodeMap& operator=(const CMap&) { |
443 | 442 |
checkConcept<ReadMap<Node, T>, CMap>(); |
444 | 443 |
return *this; |
445 | 444 |
} |
446 | 445 |
}; |
447 | 446 |
|
448 | 447 |
/// \brief Read write map of the arcs to type \c T. |
449 | 448 |
/// |
450 | 449 |
/// Reference map of the arcs to type \c T. |
451 | 450 |
/// \sa Reference |
452 | 451 |
template<class T> |
453 | 452 |
class ArcMap : public ReadWriteMap<Arc,T> { |
454 | 453 |
public: |
455 | 454 |
|
456 | 455 |
///\e |
457 | 456 |
ArcMap(const Digraph&) { } |
458 | 457 |
///\e |
459 | 458 |
ArcMap(const Digraph&, T) { } |
460 | 459 |
///Copy constructor |
461 | 460 |
ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { } |
462 | 461 |
///Assignment operator |
463 | 462 |
template <typename CMap> |
464 | 463 |
ArcMap& operator=(const CMap&) { |
465 | 464 |
checkConcept<ReadMap<Arc, T>, CMap>(); |
466 | 465 |
return *this; |
467 | 466 |
} |
468 | 467 |
}; |
469 | 468 |
|
470 | 469 |
template <typename _Digraph> |
471 | 470 |
struct Constraints { |
472 | 471 |
void constraints() { |
473 | 472 |
checkConcept<IterableDigraphComponent<>, _Digraph>(); |
474 | 473 |
checkConcept<IDableDigraphComponent<>, _Digraph>(); |
475 | 474 |
checkConcept<MappableDigraphComponent<>, _Digraph>(); |
476 | 475 |
} |
477 | 476 |
}; |
478 | 477 |
|
479 | 478 |
}; |
480 | 479 |
|
481 | 480 |
} //namespace concepts |
482 | 481 |
} //namespace lemon |
483 | 482 |
|
484 | 483 |
|
485 | 484 |
|
486 | 485 |
#endif // LEMON_CONCEPT_DIGRAPH_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of Undirected Graphs. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPT_GRAPH_H |
24 | 24 |
#define LEMON_CONCEPT_GRAPH_H |
25 | 25 |
|
26 | 26 |
#include <lemon/concepts/graph_components.h> |
27 | 27 |
#include <lemon/concepts/graph.h> |
28 |
#include <lemon/ |
|
28 |
#include <lemon/core.h> |
|
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \ingroup graph_concepts |
34 | 34 |
/// |
35 | 35 |
/// \brief Class describing the concept of Undirected Graphs. |
36 | 36 |
/// |
37 | 37 |
/// This class describes the common interface of all Undirected |
38 | 38 |
/// Graphs. |
39 | 39 |
/// |
40 | 40 |
/// As all concept describing classes it provides only interface |
41 | 41 |
/// without any sensible implementation. So any algorithm for |
42 | 42 |
/// undirected graph should compile with this class, but it will not |
43 | 43 |
/// run properly, of course. |
44 | 44 |
/// |
45 | 45 |
/// The LEMON undirected graphs also fulfill the concept of |
46 | 46 |
/// directed graphs (\ref lemon::concepts::Digraph "Digraph |
47 | 47 |
/// Concept"). Each edges can be seen as two opposite |
48 | 48 |
/// directed arc and consequently the undirected graph can be |
49 | 49 |
/// seen as the direceted graph of these directed arcs. The |
50 | 50 |
/// Graph has the Edge inner class for the edges and |
51 | 51 |
/// the Arc type for the directed arcs. The Arc type is |
52 | 52 |
/// convertible to Edge or inherited from it so from a directed |
53 | 53 |
/// arc we can get the represented edge. |
54 | 54 |
/// |
55 | 55 |
/// In the sense of the LEMON each edge has a default |
56 | 56 |
/// direction (it should be in every computer implementation, |
57 | 57 |
/// because the order of edge's nodes defines an |
58 | 58 |
/// orientation). With the default orientation we can define that |
59 | 59 |
/// the directed arc is forward or backward directed. With the \c |
60 | 60 |
/// direction() and \c direct() function we can get the direction |
61 | 61 |
/// of the directed arc and we can direct an edge. |
62 | 62 |
/// |
63 | 63 |
/// The EdgeIt is an iterator for the edges. We can use |
64 | 64 |
/// the EdgeMap to map values for the edges. The InArcIt and |
65 | 65 |
/// OutArcIt iterates on the same edges but with opposite |
66 | 66 |
/// direction. The IncEdgeIt iterates also on the same edges |
67 | 67 |
/// as the OutArcIt and InArcIt but it is not convertible to Arc just |
68 | 68 |
/// to Edge. |
69 | 69 |
class Graph { |
70 | 70 |
public: |
71 | 71 |
/// \brief The undirected graph should be tagged by the |
72 | 72 |
/// UndirectedTag. |
73 | 73 |
/// |
74 | 74 |
/// The undirected graph should be tagged by the UndirectedTag. This |
75 | 75 |
/// tag helps the enable_if technics to make compile time |
76 | 76 |
/// specializations for undirected graphs. |
77 | 77 |
typedef True UndirectedTag; |
78 | 78 |
|
79 | 79 |
/// \brief The base type of node iterators, |
80 | 80 |
/// or in other words, the trivial node iterator. |
81 | 81 |
/// |
82 | 82 |
/// This is the base type of each node iterator, |
83 | 83 |
/// thus each kind of node iterator converts to this. |
84 | 84 |
/// More precisely each kind of node iterator should be inherited |
85 | 85 |
/// from the trivial node iterator. |
86 | 86 |
class Node { |
87 | 87 |
public: |
88 | 88 |
/// Default constructor |
89 | 89 |
|
90 | 90 |
/// @warning The default constructor sets the iterator |
91 | 91 |
/// to an undefined value. |
92 | 92 |
Node() { } |
93 | 93 |
/// Copy constructor. |
94 | 94 |
|
95 | 95 |
/// Copy constructor. |
96 | 96 |
/// |
97 | 97 |
Node(const Node&) { } |
98 | 98 |
|
99 | 99 |
/// Invalid constructor \& conversion. |
100 | 100 |
|
101 | 101 |
/// This constructor initializes the iterator to be invalid. |
102 | 102 |
/// \sa Invalid for more details. |
103 | 103 |
Node(Invalid) { } |
104 | 104 |
/// Equality operator |
105 | 105 |
|
106 | 106 |
/// Two iterators are equal if and only if they point to the |
107 | 107 |
/// same object or both are invalid. |
108 | 108 |
bool operator==(Node) const { return true; } |
109 | 109 |
|
110 | 110 |
/// Inequality operator |
111 | 111 |
|
112 | 112 |
/// \sa operator==(Node n) |
113 | 113 |
/// |
114 | 114 |
bool operator!=(Node) const { return true; } |
115 | 115 |
|
116 | 116 |
/// Artificial ordering operator. |
117 | 117 |
|
118 | 118 |
/// To allow the use of graph descriptors as key type in std::map or |
119 | 119 |
/// similar associative container we require this. |
120 | 120 |
/// |
121 | 121 |
/// \note This operator only have to define some strict ordering of |
122 | 122 |
/// the items; this order has nothing to do with the iteration |
123 | 123 |
/// ordering of the items. |
124 | 124 |
bool operator<(Node) const { return false; } |
125 | 125 |
|
126 | 126 |
}; |
127 | 127 |
|
128 | 128 |
/// This iterator goes through each node. |
129 | 129 |
|
130 | 130 |
/// This iterator goes through each node. |
131 | 131 |
/// Its usage is quite simple, for example you can count the number |
132 | 132 |
/// of nodes in graph \c g of type \c Graph like this: |
133 | 133 |
///\code |
134 | 134 |
/// int count=0; |
135 | 135 |
/// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count; |
136 | 136 |
///\endcode |
137 | 137 |
class NodeIt : public Node { |
138 | 138 |
public: |
139 | 139 |
/// Default constructor |
140 | 140 |
|
141 | 141 |
/// @warning The default constructor sets the iterator |
142 | 142 |
/// to an undefined value. |
143 | 143 |
NodeIt() { } |
144 | 144 |
/// Copy constructor. |
145 | 145 |
|
146 | 146 |
/// Copy constructor. |
147 | 147 |
/// |
148 | 148 |
NodeIt(const NodeIt& n) : Node(n) { } |
149 | 149 |
/// Invalid constructor \& conversion. |
150 | 150 |
|
151 | 151 |
/// Initialize the iterator to be invalid. |
152 | 152 |
/// \sa Invalid for more details. |
153 | 153 |
NodeIt(Invalid) { } |
154 | 154 |
/// Sets the iterator to the first node. |
155 | 155 |
|
156 | 156 |
/// Sets the iterator to the first node of \c g. |
157 | 157 |
/// |
158 | 158 |
NodeIt(const Graph&) { } |
159 | 159 |
/// Node -> NodeIt conversion. |
160 | 160 |
|
161 | 161 |
/// Sets the iterator to the node of \c the graph pointed by |
162 | 162 |
/// the trivial iterator. |
163 | 163 |
/// This feature necessitates that each time we |
164 | 164 |
/// iterate the arc-set, the iteration order is the same. |
165 | 165 |
NodeIt(const Graph&, const Node&) { } |
166 | 166 |
/// Next node. |
167 | 167 |
|
168 | 168 |
/// Assign the iterator to the next node. |
169 | 169 |
/// |
170 | 170 |
NodeIt& operator++() { return *this; } |
171 | 171 |
}; |
172 | 172 |
|
173 | 173 |
|
174 | 174 |
/// The base type of the edge iterators. |
175 | 175 |
|
176 | 176 |
/// The base type of the edge iterators. |
177 | 177 |
/// |
178 | 178 |
class Edge { |
179 | 179 |
public: |
180 | 180 |
/// Default constructor |
181 | 181 |
|
182 | 182 |
/// @warning The default constructor sets the iterator |
183 | 183 |
/// to an undefined value. |
184 | 184 |
Edge() { } |
185 | 185 |
/// Copy constructor. |
186 | 186 |
|
187 | 187 |
/// Copy constructor. |
188 | 188 |
/// |
189 | 189 |
Edge(const Edge&) { } |
190 | 190 |
/// Initialize the iterator to be invalid. |
191 | 191 |
|
192 | 192 |
/// Initialize the iterator to be invalid. |
193 | 193 |
/// |
194 | 194 |
Edge(Invalid) { } |
195 | 195 |
/// Equality operator |
196 | 196 |
|
197 | 197 |
/// Two iterators are equal if and only if they point to the |
198 | 198 |
/// same object or both are invalid. |
199 | 199 |
bool operator==(Edge) const { return true; } |
200 | 200 |
/// Inequality operator |
201 | 201 |
|
202 | 202 |
/// \sa operator==(Edge n) |
203 | 203 |
/// |
204 | 204 |
bool operator!=(Edge) const { return true; } |
205 | 205 |
|
206 | 206 |
/// Artificial ordering operator. |
207 | 207 |
|
208 | 208 |
/// To allow the use of graph descriptors as key type in std::map or |
209 | 209 |
/// similar associative container we require this. |
210 | 210 |
/// |
211 | 211 |
/// \note This operator only have to define some strict ordering of |
212 | 212 |
/// the items; this order has nothing to do with the iteration |
213 | 213 |
/// ordering of the items. |
214 | 214 |
bool operator<(Edge) const { return false; } |
215 | 215 |
}; |
216 | 216 |
|
217 | 217 |
/// This iterator goes through each edge. |
218 | 218 |
|
219 | 219 |
/// This iterator goes through each edge of a graph. |
220 | 220 |
/// Its usage is quite simple, for example you can count the number |
221 | 221 |
/// of edges in a graph \c g of type \c Graph as follows: |
222 | 222 |
///\code |
223 | 223 |
/// int count=0; |
224 | 224 |
/// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count; |
225 | 225 |
///\endcode |
226 | 226 |
class EdgeIt : public Edge { |
227 | 227 |
public: |
228 | 228 |
/// Default constructor |
229 | 229 |
|
230 | 230 |
/// @warning The default constructor sets the iterator |
231 | 231 |
/// to an undefined value. |
232 | 232 |
EdgeIt() { } |
233 | 233 |
/// Copy constructor. |
234 | 234 |
|
235 | 235 |
/// Copy constructor. |
236 | 236 |
/// |
237 | 237 |
EdgeIt(const EdgeIt& e) : Edge(e) { } |
238 | 238 |
/// Initialize the iterator to be invalid. |
239 | 239 |
|
240 | 240 |
/// Initialize the iterator to be invalid. |
241 | 241 |
/// |
242 | 242 |
EdgeIt(Invalid) { } |
243 | 243 |
/// This constructor sets the iterator to the first edge. |
244 | 244 |
|
245 | 245 |
/// This constructor sets the iterator to the first edge. |
246 | 246 |
EdgeIt(const Graph&) { } |
247 | 247 |
/// Edge -> EdgeIt conversion |
248 | 248 |
|
249 | 249 |
/// Sets the iterator to the value of the trivial iterator. |
250 | 250 |
/// This feature necessitates that each time we |
251 | 251 |
/// iterate the edge-set, the iteration order is the |
252 | 252 |
/// same. |
253 | 253 |
EdgeIt(const Graph&, const Edge&) { } |
254 | 254 |
/// Next edge |
255 | 255 |
|
256 | 256 |
/// Assign the iterator to the next edge. |
257 | 257 |
EdgeIt& operator++() { return *this; } |
258 | 258 |
}; |
259 | 259 |
|
260 | 260 |
/// \brief This iterator goes trough the incident undirected |
261 | 261 |
/// arcs of a node. |
262 | 262 |
/// |
263 | 263 |
/// This iterator goes trough the incident edges |
264 | 264 |
/// of a certain node of a graph. You should assume that the |
265 | 265 |
/// loop arcs will be iterated twice. |
266 | 266 |
/// |
267 | 267 |
/// Its usage is quite simple, for example you can compute the |
268 | 268 |
/// degree (i.e. count the number of incident arcs of a node \c n |
269 | 269 |
/// in graph \c g of type \c Graph as follows. |
270 | 270 |
/// |
271 | 271 |
///\code |
272 | 272 |
/// int count=0; |
273 | 273 |
/// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count; |
274 | 274 |
///\endcode |
275 | 275 |
class IncEdgeIt : public Edge { |
276 | 276 |
public: |
277 | 277 |
/// Default constructor |
278 | 278 |
|
279 | 279 |
/// @warning The default constructor sets the iterator |
280 | 280 |
/// to an undefined value. |
281 | 281 |
IncEdgeIt() { } |
282 | 282 |
/// Copy constructor. |
283 | 283 |
|
284 | 284 |
/// Copy constructor. |
285 | 285 |
/// |
286 | 286 |
IncEdgeIt(const IncEdgeIt& e) : Edge(e) { } |
287 | 287 |
/// Initialize the iterator to be invalid. |
288 | 288 |
|
289 | 289 |
/// Initialize the iterator to be invalid. |
290 | 290 |
/// |
291 | 291 |
IncEdgeIt(Invalid) { } |
292 | 292 |
/// This constructor sets the iterator to first incident arc. |
293 | 293 |
|
294 | 294 |
/// This constructor set the iterator to the first incident arc of |
295 | 295 |
/// the node. |
296 | 296 |
IncEdgeIt(const Graph&, const Node&) { } |
297 | 297 |
/// Edge -> IncEdgeIt conversion |
298 | 298 |
|
299 | 299 |
/// Sets the iterator to the value of the trivial iterator \c e. |
300 | 300 |
/// This feature necessitates that each time we |
301 | 301 |
/// iterate the arc-set, the iteration order is the same. |
302 | 302 |
IncEdgeIt(const Graph&, const Edge&) { } |
303 | 303 |
/// Next incident arc |
304 | 304 |
|
305 | 305 |
/// Assign the iterator to the next incident arc |
306 | 306 |
/// of the corresponding node. |
307 | 307 |
IncEdgeIt& operator++() { return *this; } |
308 | 308 |
}; |
309 | 309 |
|
310 | 310 |
/// The directed arc type. |
311 | 311 |
|
312 | 312 |
/// The directed arc type. It can be converted to the |
313 | 313 |
/// edge or it should be inherited from the undirected |
314 | 314 |
/// arc. |
315 | 315 |
class Arc : public Edge { |
316 | 316 |
public: |
317 | 317 |
/// Default constructor |
318 | 318 |
|
319 | 319 |
/// @warning The default constructor sets the iterator |
320 | 320 |
/// to an undefined value. |
321 | 321 |
Arc() { } |
322 | 322 |
/// Copy constructor. |
323 | 323 |
|
324 | 324 |
/// Copy constructor. |
325 | 325 |
/// |
326 | 326 |
Arc(const Arc& e) : Edge(e) { } |
327 | 327 |
/// Initialize the iterator to be invalid. |
328 | 328 |
|
329 | 329 |
/// Initialize the iterator to be invalid. |
330 | 330 |
/// |
331 | 331 |
Arc(Invalid) { } |
332 | 332 |
/// Equality operator |
333 | 333 |
|
334 | 334 |
/// Two iterators are equal if and only if they point to the |
335 | 335 |
/// same object or both are invalid. |
336 | 336 |
bool operator==(Arc) const { return true; } |
337 | 337 |
/// Inequality operator |
338 | 338 |
|
339 | 339 |
/// \sa operator==(Arc n) |
340 | 340 |
/// |
341 | 341 |
bool operator!=(Arc) const { return true; } |
342 | 342 |
|
343 | 343 |
/// Artificial ordering operator. |
344 | 344 |
|
345 | 345 |
/// To allow the use of graph descriptors as key type in std::map or |
346 | 346 |
/// similar associative container we require this. |
347 | 347 |
/// |
348 | 348 |
/// \note This operator only have to define some strict ordering of |
349 | 349 |
/// the items; this order has nothing to do with the iteration |
350 | 350 |
/// ordering of the items. |
351 | 351 |
bool operator<(Arc) const { return false; } |
352 | 352 |
|
353 | 353 |
}; |
354 | 354 |
/// This iterator goes through each directed arc. |
355 | 355 |
|
356 | 356 |
/// This iterator goes through each arc of a graph. |
357 | 357 |
/// Its usage is quite simple, for example you can count the number |
358 | 358 |
/// of arcs in a graph \c g of type \c Graph as follows: |
359 | 359 |
///\code |
360 | 360 |
/// int count=0; |
361 | 361 |
/// for(Graph::ArcIt e(g); e!=INVALID; ++e) ++count; |
362 | 362 |
///\endcode |
363 | 363 |
class ArcIt : public Arc { |
364 | 364 |
public: |
365 | 365 |
/// Default constructor |
366 | 366 |
|
367 | 367 |
/// @warning The default constructor sets the iterator |
368 | 368 |
/// to an undefined value. |
369 | 369 |
ArcIt() { } |
370 | 370 |
/// Copy constructor. |
371 | 371 |
|
372 | 372 |
/// Copy constructor. |
373 | 373 |
/// |
374 | 374 |
ArcIt(const ArcIt& e) : Arc(e) { } |
375 | 375 |
/// Initialize the iterator to be invalid. |
376 | 376 |
|
377 | 377 |
/// Initialize the iterator to be invalid. |
378 | 378 |
/// |
379 | 379 |
ArcIt(Invalid) { } |
380 | 380 |
/// This constructor sets the iterator to the first arc. |
381 | 381 |
|
382 | 382 |
/// This constructor sets the iterator to the first arc of \c g. |
383 | 383 |
///@param g the graph |
384 | 384 |
ArcIt(const Graph &g) { ignore_unused_variable_warning(g); } |
385 | 385 |
/// Arc -> ArcIt conversion |
386 | 386 |
|
387 | 387 |
/// Sets the iterator to the value of the trivial iterator \c e. |
388 | 388 |
/// This feature necessitates that each time we |
389 | 389 |
/// iterate the arc-set, the iteration order is the same. |
390 | 390 |
ArcIt(const Graph&, const Arc&) { } |
391 | 391 |
///Next arc |
392 | 392 |
|
393 | 393 |
/// Assign the iterator to the next arc. |
394 | 394 |
ArcIt& operator++() { return *this; } |
395 | 395 |
}; |
396 | 396 |
|
397 | 397 |
/// This iterator goes trough the outgoing directed arcs of a node. |
398 | 398 |
|
399 | 399 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
400 | 400 |
/// of a graph. |
401 | 401 |
/// Its usage is quite simple, for example you can count the number |
402 | 402 |
/// of outgoing arcs of a node \c n |
403 | 403 |
/// in graph \c g of type \c Graph as follows. |
404 | 404 |
///\code |
405 | 405 |
/// int count=0; |
406 | 406 |
/// for (Graph::OutArcIt e(g, n); e!=INVALID; ++e) ++count; |
407 | 407 |
///\endcode |
408 | 408 |
|
409 | 409 |
class OutArcIt : public Arc { |
410 | 410 |
public: |
411 | 411 |
/// Default constructor |
412 | 412 |
|
413 | 413 |
/// @warning The default constructor sets the iterator |
414 | 414 |
/// to an undefined value. |
415 | 415 |
OutArcIt() { } |
416 | 416 |
/// Copy constructor. |
417 | 417 |
|
418 | 418 |
/// Copy constructor. |
419 | 419 |
/// |
420 | 420 |
OutArcIt(const OutArcIt& e) : Arc(e) { } |
421 | 421 |
/// Initialize the iterator to be invalid. |
422 | 422 |
|
423 | 423 |
/// Initialize the iterator to be invalid. |
424 | 424 |
/// |
425 | 425 |
OutArcIt(Invalid) { } |
426 | 426 |
/// This constructor sets the iterator to the first outgoing arc. |
427 | 427 |
|
428 | 428 |
/// This constructor sets the iterator to the first outgoing arc of |
429 | 429 |
/// the node. |
430 | 430 |
///@param n the node |
431 | 431 |
///@param g the graph |
432 | 432 |
OutArcIt(const Graph& n, const Node& g) { |
433 | 433 |
ignore_unused_variable_warning(n); |
434 | 434 |
ignore_unused_variable_warning(g); |
435 | 435 |
} |
436 | 436 |
/// Arc -> OutArcIt conversion |
437 | 437 |
|
438 | 438 |
/// Sets the iterator to the value of the trivial iterator. |
439 | 439 |
/// This feature necessitates that each time we |
440 | 440 |
/// iterate the arc-set, the iteration order is the same. |
441 | 441 |
OutArcIt(const Graph&, const Arc&) { } |
442 | 442 |
///Next outgoing arc |
443 | 443 |
|
444 | 444 |
/// Assign the iterator to the next |
445 | 445 |
/// outgoing arc of the corresponding node. |
446 | 446 |
OutArcIt& operator++() { return *this; } |
447 | 447 |
}; |
448 | 448 |
|
449 | 449 |
/// This iterator goes trough the incoming directed arcs of a node. |
450 | 450 |
|
451 | 451 |
/// This iterator goes trough the \e incoming arcs of a certain node |
452 | 452 |
/// of a graph. |
453 | 453 |
/// Its usage is quite simple, for example you can count the number |
454 | 454 |
/// of outgoing arcs of a node \c n |
455 | 455 |
/// in graph \c g of type \c Graph as follows. |
456 | 456 |
///\code |
457 | 457 |
/// int count=0; |
458 | 458 |
/// for(Graph::InArcIt e(g, n); e!=INVALID; ++e) ++count; |
459 | 459 |
///\endcode |
460 | 460 |
|
461 | 461 |
class InArcIt : public Arc { |
462 | 462 |
public: |
463 | 463 |
/// Default constructor |
464 | 464 |
|
465 | 465 |
/// @warning The default constructor sets the iterator |
466 | 466 |
/// to an undefined value. |
467 | 467 |
InArcIt() { } |
468 | 468 |
/// Copy constructor. |
469 | 469 |
|
470 | 470 |
/// Copy constructor. |
471 | 471 |
/// |
472 | 472 |
InArcIt(const InArcIt& e) : Arc(e) { } |
473 | 473 |
/// Initialize the iterator to be invalid. |
474 | 474 |
|
475 | 475 |
/// Initialize the iterator to be invalid. |
476 | 476 |
/// |
477 | 477 |
InArcIt(Invalid) { } |
478 | 478 |
/// This constructor sets the iterator to first incoming arc. |
479 | 479 |
|
480 | 480 |
/// This constructor set the iterator to the first incoming arc of |
481 | 481 |
/// the node. |
482 | 482 |
///@param n the node |
483 | 483 |
///@param g the graph |
484 | 484 |
InArcIt(const Graph& g, const Node& n) { |
485 | 485 |
ignore_unused_variable_warning(n); |
486 | 486 |
ignore_unused_variable_warning(g); |
487 | 487 |
} |
488 | 488 |
/// Arc -> InArcIt conversion |
489 | 489 |
|
490 | 490 |
/// Sets the iterator to the value of the trivial iterator \c e. |
491 | 491 |
/// This feature necessitates that each time we |
492 | 492 |
/// iterate the arc-set, the iteration order is the same. |
493 | 493 |
InArcIt(const Graph&, const Arc&) { } |
494 | 494 |
/// Next incoming arc |
495 | 495 |
|
496 | 496 |
/// Assign the iterator to the next inarc of the corresponding node. |
497 | 497 |
/// |
498 | 498 |
InArcIt& operator++() { return *this; } |
499 | 499 |
}; |
500 | 500 |
|
501 | 501 |
/// \brief Read write map of the nodes to type \c T. |
502 | 502 |
/// |
503 | 503 |
/// ReadWrite map of the nodes to type \c T. |
504 | 504 |
/// \sa Reference |
505 | 505 |
template<class T> |
506 | 506 |
class NodeMap : public ReadWriteMap< Node, T > |
507 | 507 |
{ |
508 | 508 |
public: |
509 | 509 |
|
510 | 510 |
///\e |
511 | 511 |
NodeMap(const Graph&) { } |
512 | 512 |
///\e |
513 | 513 |
NodeMap(const Graph&, T) { } |
514 | 514 |
|
515 | 515 |
///Copy constructor |
516 | 516 |
NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { } |
517 | 517 |
///Assignment operator |
518 | 518 |
template <typename CMap> |
519 | 519 |
NodeMap& operator=(const CMap&) { |
520 | 520 |
checkConcept<ReadMap<Node, T>, CMap>(); |
521 | 521 |
return *this; |
522 | 522 |
} |
523 | 523 |
}; |
524 | 524 |
|
525 | 525 |
/// \brief Read write map of the directed arcs to type \c T. |
526 | 526 |
/// |
527 | 527 |
/// Reference map of the directed arcs to type \c T. |
528 | 528 |
/// \sa Reference |
529 | 529 |
template<class T> |
530 | 530 |
class ArcMap : public ReadWriteMap<Arc,T> |
531 | 531 |
{ |
532 | 532 |
public: |
533 | 533 |
|
534 | 534 |
///\e |
535 | 535 |
ArcMap(const Graph&) { } |
536 | 536 |
///\e |
537 | 537 |
ArcMap(const Graph&, T) { } |
538 | 538 |
///Copy constructor |
539 | 539 |
ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { } |
540 | 540 |
///Assignment operator |
541 | 541 |
template <typename CMap> |
542 | 542 |
ArcMap& operator=(const CMap&) { |
543 | 543 |
checkConcept<ReadMap<Arc, T>, CMap>(); |
544 | 544 |
return *this; |
545 | 545 |
} |
546 | 546 |
}; |
547 | 547 |
|
548 | 548 |
/// Read write map of the edges to type \c T. |
549 | 549 |
|
550 | 550 |
/// Reference map of the arcs to type \c T. |
551 | 551 |
/// \sa Reference |
552 | 552 |
template<class T> |
553 | 553 |
class EdgeMap : public ReadWriteMap<Edge,T> |
554 | 554 |
{ |
555 | 555 |
public: |
556 | 556 |
|
557 | 557 |
///\e |
558 | 558 |
EdgeMap(const Graph&) { } |
559 | 559 |
///\e |
560 | 560 |
EdgeMap(const Graph&, T) { } |
561 | 561 |
///Copy constructor |
562 | 562 |
EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) {} |
563 | 563 |
///Assignment operator |
564 | 564 |
template <typename CMap> |
565 | 565 |
EdgeMap& operator=(const CMap&) { |
566 | 566 |
checkConcept<ReadMap<Edge, T>, CMap>(); |
567 | 567 |
return *this; |
568 | 568 |
} |
569 | 569 |
}; |
570 | 570 |
|
571 | 571 |
/// \brief Direct the given edge. |
572 | 572 |
/// |
573 | 573 |
/// Direct the given edge. The returned arc source |
574 | 574 |
/// will be the given node. |
575 | 575 |
Arc direct(const Edge&, const Node&) const { |
576 | 576 |
return INVALID; |
577 | 577 |
} |
578 | 578 |
|
579 | 579 |
/// \brief Direct the given edge. |
580 | 580 |
/// |
581 | 581 |
/// Direct the given edge. The returned arc |
582 | 582 |
/// represents the given edge and the direction comes |
583 | 583 |
/// from the bool parameter. The source of the edge and |
584 | 584 |
/// the directed arc is the same when the given bool is true. |
585 | 585 |
Arc direct(const Edge&, bool) const { |
586 | 586 |
return INVALID; |
587 | 587 |
} |
588 | 588 |
|
589 | 589 |
/// \brief Returns true if the arc has default orientation. |
590 | 590 |
/// |
591 | 591 |
/// Returns whether the given directed arc is same orientation as |
592 | 592 |
/// the corresponding edge's default orientation. |
593 | 593 |
bool direction(Arc) const { return true; } |
594 | 594 |
|
595 | 595 |
/// \brief Returns the opposite directed arc. |
596 | 596 |
/// |
597 | 597 |
/// Returns the opposite directed arc. |
598 | 598 |
Arc oppositeArc(Arc) const { return INVALID; } |
599 | 599 |
|
600 | 600 |
/// \brief Opposite node on an arc |
601 | 601 |
/// |
602 | 602 |
/// \return the opposite of the given Node on the given Edge |
603 | 603 |
Node oppositeNode(Node, Edge) const { return INVALID; } |
604 | 604 |
|
605 | 605 |
/// \brief First node of the edge. |
606 | 606 |
/// |
607 | 607 |
/// \return the first node of the given Edge. |
608 | 608 |
/// |
609 | 609 |
/// Naturally edges don't have direction and thus |
610 | 610 |
/// don't have source and target node. But we use these two methods |
611 | 611 |
/// to query the two nodes of the arc. The direction of the arc |
612 | 612 |
/// which arises this way is called the inherent direction of the |
613 | 613 |
/// edge, and is used to define the "default" direction |
614 | 614 |
/// of the directed versions of the arcs. |
615 | 615 |
/// \sa direction |
616 | 616 |
Node u(Edge) const { return INVALID; } |
617 | 617 |
|
618 | 618 |
/// \brief Second node of the edge. |
619 | 619 |
Node v(Edge) const { return INVALID; } |
620 | 620 |
|
621 | 621 |
/// \brief Source node of the directed arc. |
622 | 622 |
Node source(Arc) const { return INVALID; } |
623 | 623 |
|
624 | 624 |
/// \brief Target node of the directed arc. |
625 | 625 |
Node target(Arc) const { return INVALID; } |
626 | 626 |
|
627 | 627 |
/// \brief Returns the id of the node. |
628 | 628 |
int id(Node) const { return -1; } |
629 | 629 |
|
630 | 630 |
/// \brief Returns the id of the edge. |
631 | 631 |
int id(Edge) const { return -1; } |
632 | 632 |
|
633 | 633 |
/// \brief Returns the id of the arc. |
634 | 634 |
int id(Arc) const { return -1; } |
635 | 635 |
|
636 | 636 |
/// \brief Returns the node with the given id. |
637 | 637 |
/// |
638 | 638 |
/// \pre The argument should be a valid node id in the graph. |
639 | 639 |
Node nodeFromId(int) const { return INVALID; } |
640 | 640 |
|
641 | 641 |
/// \brief Returns the edge with the given id. |
642 | 642 |
/// |
643 | 643 |
/// \pre The argument should be a valid edge id in the graph. |
644 | 644 |
Edge edgeFromId(int) const { return INVALID; } |
645 | 645 |
|
646 | 646 |
/// \brief Returns the arc with the given id. |
647 | 647 |
/// |
648 | 648 |
/// \pre The argument should be a valid arc id in the graph. |
649 | 649 |
Arc arcFromId(int) const { return INVALID; } |
650 | 650 |
|
651 | 651 |
/// \brief Returns an upper bound on the node IDs. |
652 | 652 |
int maxNodeId() const { return -1; } |
653 | 653 |
|
654 | 654 |
/// \brief Returns an upper bound on the edge IDs. |
655 | 655 |
int maxEdgeId() const { return -1; } |
656 | 656 |
|
657 | 657 |
/// \brief Returns an upper bound on the arc IDs. |
658 | 658 |
int maxArcId() const { return -1; } |
659 | 659 |
|
660 | 660 |
void first(Node&) const {} |
661 | 661 |
void next(Node&) const {} |
662 | 662 |
|
663 | 663 |
void first(Edge&) const {} |
664 | 664 |
void next(Edge&) const {} |
665 | 665 |
|
666 | 666 |
void first(Arc&) const {} |
667 | 667 |
void next(Arc&) const {} |
668 | 668 |
|
669 | 669 |
void firstOut(Arc&, Node) const {} |
670 | 670 |
void nextOut(Arc&) const {} |
671 | 671 |
|
672 | 672 |
void firstIn(Arc&, Node) const {} |
673 | 673 |
void nextIn(Arc&) const {} |
674 | 674 |
|
675 | 675 |
void firstInc(Edge &, bool &, const Node &) const {} |
676 | 676 |
void nextInc(Edge &, bool &) const {} |
677 | 677 |
|
678 | 678 |
// The second parameter is dummy. |
679 | 679 |
Node fromId(int, Node) const { return INVALID; } |
680 | 680 |
// The second parameter is dummy. |
681 | 681 |
Edge fromId(int, Edge) const { return INVALID; } |
682 | 682 |
// The second parameter is dummy. |
683 | 683 |
Arc fromId(int, Arc) const { return INVALID; } |
684 | 684 |
|
685 | 685 |
// Dummy parameter. |
686 | 686 |
int maxId(Node) const { return -1; } |
687 | 687 |
// Dummy parameter. |
688 | 688 |
int maxId(Edge) const { return -1; } |
689 | 689 |
// Dummy parameter. |
690 | 690 |
int maxId(Arc) const { return -1; } |
691 | 691 |
|
692 | 692 |
/// \brief Base node of the iterator |
693 | 693 |
/// |
694 | 694 |
/// Returns the base node (the source in this case) of the iterator |
695 | 695 |
Node baseNode(OutArcIt e) const { |
696 | 696 |
return source(e); |
697 | 697 |
} |
698 | 698 |
/// \brief Running node of the iterator |
699 | 699 |
/// |
700 | 700 |
/// Returns the running node (the target in this case) of the |
701 | 701 |
/// iterator |
702 | 702 |
Node runningNode(OutArcIt e) const { |
703 | 703 |
return target(e); |
704 | 704 |
} |
705 | 705 |
|
706 | 706 |
/// \brief Base node of the iterator |
707 | 707 |
/// |
708 | 708 |
/// Returns the base node (the target in this case) of the iterator |
709 | 709 |
Node baseNode(InArcIt e) const { |
710 | 710 |
return target(e); |
711 | 711 |
} |
712 | 712 |
/// \brief Running node of the iterator |
713 | 713 |
/// |
714 | 714 |
/// Returns the running node (the source in this case) of the |
715 | 715 |
/// iterator |
716 | 716 |
Node runningNode(InArcIt e) const { |
717 | 717 |
return source(e); |
718 | 718 |
} |
719 | 719 |
|
720 | 720 |
/// \brief Base node of the iterator |
721 | 721 |
/// |
722 | 722 |
/// Returns the base node of the iterator |
723 | 723 |
Node baseNode(IncEdgeIt) const { |
724 | 724 |
return INVALID; |
725 | 725 |
} |
726 | 726 |
|
727 | 727 |
/// \brief Running node of the iterator |
728 | 728 |
/// |
729 | 729 |
/// Returns the running node of the iterator |
730 | 730 |
Node runningNode(IncEdgeIt) const { |
731 | 731 |
return INVALID; |
732 | 732 |
} |
733 | 733 |
|
734 | 734 |
template <typename _Graph> |
735 | 735 |
struct Constraints { |
736 | 736 |
void constraints() { |
737 | 737 |
checkConcept<IterableGraphComponent<>, _Graph>(); |
738 | 738 |
checkConcept<IDableGraphComponent<>, _Graph>(); |
739 | 739 |
checkConcept<MappableGraphComponent<>, _Graph>(); |
740 | 740 |
} |
741 | 741 |
}; |
742 | 742 |
|
743 | 743 |
}; |
744 | 744 |
|
745 | 745 |
} |
746 | 746 |
|
747 | 747 |
} |
748 | 748 |
|
749 | 749 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of graph components. |
22 | 22 |
|
23 | 23 |
|
24 | 24 |
#ifndef LEMON_CONCEPT_GRAPH_COMPONENTS_H |
25 | 25 |
#define LEMON_CONCEPT_GRAPH_COMPONENTS_H |
26 | 26 |
|
27 |
#include <lemon/ |
|
27 |
#include <lemon/core.h> |
|
28 | 28 |
#include <lemon/concepts/maps.h> |
29 | 29 |
|
30 | 30 |
#include <lemon/bits/alteration_notifier.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
namespace concepts { |
34 | 34 |
|
35 | 35 |
/// \brief Skeleton class for graph Node and Arc types |
36 | 36 |
/// |
37 | 37 |
/// This class describes the interface of Node and Arc (and Edge |
38 | 38 |
/// in undirected graphs) subtypes of graph types. |
39 | 39 |
/// |
40 | 40 |
/// \note This class is a template class so that we can use it to |
41 | 41 |
/// create graph skeleton classes. The reason for this is than Node |
42 | 42 |
/// and Arc types should \em not derive from the same base class. |
43 | 43 |
/// For Node you should instantiate it with character 'n' and for Arc |
44 | 44 |
/// with 'a'. |
45 | 45 |
|
46 | 46 |
#ifndef DOXYGEN |
47 | 47 |
template <char _selector = '0'> |
48 | 48 |
#endif |
49 | 49 |
class GraphItem { |
50 | 50 |
public: |
51 | 51 |
/// \brief Default constructor. |
52 | 52 |
/// |
53 | 53 |
/// \warning The default constructor is not required to set |
54 | 54 |
/// the item to some well-defined value. So you should consider it |
55 | 55 |
/// as uninitialized. |
56 | 56 |
GraphItem() {} |
57 | 57 |
/// \brief Copy constructor. |
58 | 58 |
/// |
59 | 59 |
/// Copy constructor. |
60 | 60 |
/// |
61 | 61 |
GraphItem(const GraphItem &) {} |
62 | 62 |
/// \brief Invalid constructor \& conversion. |
63 | 63 |
/// |
64 | 64 |
/// This constructor initializes the item to be invalid. |
65 | 65 |
/// \sa Invalid for more details. |
66 | 66 |
GraphItem(Invalid) {} |
67 | 67 |
/// \brief Assign operator for nodes. |
68 | 68 |
/// |
69 | 69 |
/// The nodes are assignable. |
70 | 70 |
/// |
71 | 71 |
GraphItem& operator=(GraphItem const&) { return *this; } |
72 | 72 |
/// \brief Equality operator. |
73 | 73 |
/// |
74 | 74 |
/// Two iterators are equal if and only if they represents the |
75 | 75 |
/// same node in the graph or both are invalid. |
76 | 76 |
bool operator==(GraphItem) const { return false; } |
77 | 77 |
/// \brief Inequality operator. |
78 | 78 |
/// |
79 | 79 |
/// \sa operator==(const Node& n) |
80 | 80 |
/// |
81 | 81 |
bool operator!=(GraphItem) const { return false; } |
82 | 82 |
|
83 | 83 |
/// \brief Artificial ordering operator. |
84 | 84 |
/// |
85 | 85 |
/// To allow the use of graph descriptors as key type in std::map or |
86 | 86 |
/// similar associative container we require this. |
87 | 87 |
/// |
88 | 88 |
/// \note This operator only have to define some strict ordering of |
89 | 89 |
/// the items; this order has nothing to do with the iteration |
90 | 90 |
/// ordering of the items. |
91 | 91 |
bool operator<(GraphItem) const { return false; } |
92 | 92 |
|
93 | 93 |
template<typename _GraphItem> |
94 | 94 |
struct Constraints { |
95 | 95 |
void constraints() { |
96 | 96 |
_GraphItem i1; |
97 | 97 |
_GraphItem i2 = i1; |
98 | 98 |
_GraphItem i3 = INVALID; |
99 | 99 |
|
100 | 100 |
i1 = i2 = i3; |
101 | 101 |
|
102 | 102 |
bool b; |
103 | 103 |
// b = (ia == ib) && (ia != ib) && (ia < ib); |
104 | 104 |
b = (ia == ib) && (ia != ib); |
105 | 105 |
b = (ia == INVALID) && (ib != INVALID); |
106 | 106 |
b = (ia < ib); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
const _GraphItem &ia; |
110 | 110 |
const _GraphItem &ib; |
111 | 111 |
}; |
112 | 112 |
}; |
113 | 113 |
|
114 | 114 |
/// \brief An empty base directed graph class. |
115 | 115 |
/// |
116 | 116 |
/// This class provides the minimal set of features needed for a |
117 | 117 |
/// directed graph structure. All digraph concepts have to be |
118 | 118 |
/// conform to this base directed graph. It just provides types |
119 | 119 |
/// for nodes and arcs and functions to get the source and the |
120 | 120 |
/// target of the arcs. |
121 | 121 |
class BaseDigraphComponent { |
122 | 122 |
public: |
123 | 123 |
|
124 | 124 |
typedef BaseDigraphComponent Digraph; |
125 | 125 |
|
126 | 126 |
/// \brief Node class of the digraph. |
127 | 127 |
/// |
128 | 128 |
/// This class represents the Nodes of the digraph. |
129 | 129 |
/// |
130 | 130 |
typedef GraphItem<'n'> Node; |
131 | 131 |
|
132 | 132 |
/// \brief Arc class of the digraph. |
133 | 133 |
/// |
134 | 134 |
/// This class represents the Arcs of the digraph. |
135 | 135 |
/// |
136 | 136 |
typedef GraphItem<'e'> Arc; |
137 | 137 |
|
138 | 138 |
/// \brief Gives back the target node of an arc. |
139 | 139 |
/// |
140 | 140 |
/// Gives back the target node of an arc. |
141 | 141 |
/// |
142 | 142 |
Node target(const Arc&) const { return INVALID;} |
143 | 143 |
|
144 | 144 |
/// \brief Gives back the source node of an arc. |
145 | 145 |
/// |
146 | 146 |
/// Gives back the source node of an arc. |
147 | 147 |
/// |
148 | 148 |
Node source(const Arc&) const { return INVALID;} |
149 | 149 |
|
150 | 150 |
/// \brief Gives back the opposite node on the given arc. |
151 | 151 |
/// |
152 | 152 |
/// Gives back the opposite node on the given arc. |
153 | 153 |
Node oppositeNode(const Node&, const Arc&) const { |
154 | 154 |
return INVALID; |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
template <typename _Digraph> |
158 | 158 |
struct Constraints { |
159 | 159 |
typedef typename _Digraph::Node Node; |
160 | 160 |
typedef typename _Digraph::Arc Arc; |
161 | 161 |
|
162 | 162 |
void constraints() { |
163 | 163 |
checkConcept<GraphItem<'n'>, Node>(); |
164 | 164 |
checkConcept<GraphItem<'a'>, Arc>(); |
165 | 165 |
{ |
166 | 166 |
Node n; |
167 | 167 |
Arc e(INVALID); |
168 | 168 |
n = digraph.source(e); |
169 | 169 |
n = digraph.target(e); |
170 | 170 |
n = digraph.oppositeNode(n, e); |
171 | 171 |
} |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
const _Digraph& digraph; |
175 | 175 |
}; |
176 | 176 |
}; |
177 | 177 |
|
178 | 178 |
/// \brief An empty base undirected graph class. |
179 | 179 |
/// |
180 | 180 |
/// This class provides the minimal set of features needed for an |
181 | 181 |
/// undirected graph structure. All undirected graph concepts have |
182 | 182 |
/// to be conform to this base graph. It just provides types for |
183 | 183 |
/// nodes, arcs and edges and functions to get the |
184 | 184 |
/// source and the target of the arcs and edges, |
185 | 185 |
/// conversion from arcs to edges and function to get |
186 | 186 |
/// both direction of the edges. |
187 | 187 |
class BaseGraphComponent : public BaseDigraphComponent { |
188 | 188 |
public: |
189 | 189 |
typedef BaseDigraphComponent::Node Node; |
190 | 190 |
typedef BaseDigraphComponent::Arc Arc; |
191 | 191 |
/// \brief Undirected arc class of the graph. |
192 | 192 |
/// |
193 | 193 |
/// This class represents the edges of the graph. |
194 | 194 |
/// The undirected graphs can be used as a directed graph which |
195 | 195 |
/// for each arc contains the opposite arc too so the graph is |
196 | 196 |
/// bidirected. The edge represents two opposite |
197 | 197 |
/// directed arcs. |
198 | 198 |
class Edge : public GraphItem<'u'> { |
199 | 199 |
public: |
200 | 200 |
typedef GraphItem<'u'> Parent; |
201 | 201 |
/// \brief Default constructor. |
202 | 202 |
/// |
203 | 203 |
/// \warning The default constructor is not required to set |
204 | 204 |
/// the item to some well-defined value. So you should consider it |
205 | 205 |
/// as uninitialized. |
206 | 206 |
Edge() {} |
207 | 207 |
/// \brief Copy constructor. |
208 | 208 |
/// |
209 | 209 |
/// Copy constructor. |
210 | 210 |
/// |
211 | 211 |
Edge(const Edge &) : Parent() {} |
212 | 212 |
/// \brief Invalid constructor \& conversion. |
213 | 213 |
/// |
214 | 214 |
/// This constructor initializes the item to be invalid. |
215 | 215 |
/// \sa Invalid for more details. |
216 | 216 |
Edge(Invalid) {} |
217 | 217 |
/// \brief Converter from arc to edge. |
218 | 218 |
/// |
219 | 219 |
/// Besides the core graph item functionality each arc should |
220 | 220 |
/// be convertible to the represented edge. |
221 | 221 |
Edge(const Arc&) {} |
222 | 222 |
/// \brief Assign arc to edge. |
223 | 223 |
/// |
224 | 224 |
/// Besides the core graph item functionality each arc should |
225 | 225 |
/// be convertible to the represented edge. |
226 | 226 |
Edge& operator=(const Arc&) { return *this; } |
227 | 227 |
}; |
228 | 228 |
|
229 | 229 |
/// \brief Returns the direction of the arc. |
230 | 230 |
/// |
231 | 231 |
/// Returns the direction of the arc. Each arc represents an |
232 | 232 |
/// edge with a direction. It gives back the |
233 | 233 |
/// direction. |
234 | 234 |
bool direction(const Arc&) const { return true; } |
235 | 235 |
|
236 | 236 |
/// \brief Returns the directed arc. |
237 | 237 |
/// |
238 | 238 |
/// Returns the directed arc from its direction and the |
239 | 239 |
/// represented edge. |
240 | 240 |
Arc direct(const Edge&, bool) const { return INVALID;} |
241 | 241 |
|
242 | 242 |
/// \brief Returns the directed arc. |
243 | 243 |
/// |
244 | 244 |
/// Returns the directed arc from its source and the |
245 | 245 |
/// represented edge. |
246 | 246 |
Arc direct(const Edge&, const Node&) const { return INVALID;} |
247 | 247 |
|
248 | 248 |
/// \brief Returns the opposite arc. |
249 | 249 |
/// |
250 | 250 |
/// Returns the opposite arc. It is the arc representing the |
251 | 251 |
/// same edge and has opposite direction. |
252 | 252 |
Arc oppositeArc(const Arc&) const { return INVALID;} |
253 | 253 |
|
254 | 254 |
/// \brief Gives back one ending of an edge. |
255 | 255 |
/// |
256 | 256 |
/// Gives back one ending of an edge. |
257 | 257 |
Node u(const Edge&) const { return INVALID;} |
258 | 258 |
|
259 | 259 |
/// \brief Gives back the other ending of an edge. |
260 | 260 |
/// |
261 | 261 |
/// Gives back the other ending of an edge. |
262 | 262 |
Node v(const Edge&) const { return INVALID;} |
263 | 263 |
|
264 | 264 |
template <typename _Graph> |
265 | 265 |
struct Constraints { |
266 | 266 |
typedef typename _Graph::Node Node; |
267 | 267 |
typedef typename _Graph::Arc Arc; |
268 | 268 |
typedef typename _Graph::Edge Edge; |
269 | 269 |
|
270 | 270 |
void constraints() { |
271 | 271 |
checkConcept<BaseDigraphComponent, _Graph>(); |
272 | 272 |
checkConcept<GraphItem<'u'>, Edge>(); |
273 | 273 |
{ |
274 | 274 |
Node n; |
275 | 275 |
Edge ue(INVALID); |
276 | 276 |
Arc e; |
277 | 277 |
n = graph.u(ue); |
278 | 278 |
n = graph.v(ue); |
279 | 279 |
e = graph.direct(ue, true); |
280 | 280 |
e = graph.direct(ue, n); |
281 | 281 |
e = graph.oppositeArc(e); |
282 | 282 |
ue = e; |
283 | 283 |
bool d = graph.direction(e); |
284 | 284 |
ignore_unused_variable_warning(d); |
285 | 285 |
} |
286 | 286 |
} |
287 | 287 |
|
288 | 288 |
const _Graph& graph; |
289 | 289 |
}; |
290 | 290 |
|
291 | 291 |
}; |
292 | 292 |
|
293 | 293 |
/// \brief An empty idable base digraph class. |
294 | 294 |
/// |
295 | 295 |
/// This class provides beside the core digraph features |
296 | 296 |
/// core id functions for the digraph structure. |
297 | 297 |
/// The most of the base digraphs should be conform to this concept. |
298 | 298 |
/// The id's are unique and immutable. |
299 | 299 |
template <typename _Base = BaseDigraphComponent> |
300 | 300 |
class IDableDigraphComponent : public _Base { |
301 | 301 |
public: |
302 | 302 |
|
303 | 303 |
typedef _Base Base; |
304 | 304 |
typedef typename Base::Node Node; |
305 | 305 |
typedef typename Base::Arc Arc; |
306 | 306 |
|
307 | 307 |
/// \brief Gives back an unique integer id for the Node. |
308 | 308 |
/// |
309 | 309 |
/// Gives back an unique integer id for the Node. |
310 | 310 |
/// |
311 | 311 |
int id(const Node&) const { return -1;} |
312 | 312 |
|
313 | 313 |
/// \brief Gives back the node by the unique id. |
314 | 314 |
/// |
315 | 315 |
/// Gives back the node by the unique id. |
316 | 316 |
/// If the digraph does not contain node with the given id |
317 | 317 |
/// then the result of the function is undetermined. |
318 | 318 |
Node nodeFromId(int) const { return INVALID;} |
319 | 319 |
|
320 | 320 |
/// \brief Gives back an unique integer id for the Arc. |
321 | 321 |
/// |
322 | 322 |
/// Gives back an unique integer id for the Arc. |
323 | 323 |
/// |
324 | 324 |
int id(const Arc&) const { return -1;} |
325 | 325 |
|
326 | 326 |
/// \brief Gives back the arc by the unique id. |
327 | 327 |
/// |
328 | 328 |
/// Gives back the arc by the unique id. |
329 | 329 |
/// If the digraph does not contain arc with the given id |
330 | 330 |
/// then the result of the function is undetermined. |
331 | 331 |
Arc arcFromId(int) const { return INVALID;} |
332 | 332 |
|
333 | 333 |
/// \brief Gives back an integer greater or equal to the maximum |
334 | 334 |
/// Node id. |
335 | 335 |
/// |
336 | 336 |
/// Gives back an integer greater or equal to the maximum Node |
337 | 337 |
/// id. |
338 | 338 |
int maxNodeId() const { return -1;} |
339 | 339 |
|
340 | 340 |
/// \brief Gives back an integer greater or equal to the maximum |
341 | 341 |
/// Arc id. |
342 | 342 |
/// |
343 | 343 |
/// Gives back an integer greater or equal to the maximum Arc |
344 | 344 |
/// id. |
345 | 345 |
int maxArcId() const { return -1;} |
346 | 346 |
|
347 | 347 |
template <typename _Digraph> |
348 | 348 |
struct Constraints { |
349 | 349 |
|
350 | 350 |
void constraints() { |
351 | 351 |
checkConcept<Base, _Digraph >(); |
352 | 352 |
typename _Digraph::Node node; |
353 | 353 |
int nid = digraph.id(node); |
354 | 354 |
nid = digraph.id(node); |
355 | 355 |
node = digraph.nodeFromId(nid); |
356 | 356 |
typename _Digraph::Arc arc; |
357 | 357 |
int eid = digraph.id(arc); |
358 | 358 |
eid = digraph.id(arc); |
359 | 359 |
arc = digraph.arcFromId(eid); |
360 | 360 |
|
361 | 361 |
nid = digraph.maxNodeId(); |
362 | 362 |
ignore_unused_variable_warning(nid); |
363 | 363 |
eid = digraph.maxArcId(); |
364 | 364 |
ignore_unused_variable_warning(eid); |
365 | 365 |
} |
366 | 366 |
|
367 | 367 |
const _Digraph& digraph; |
368 | 368 |
}; |
369 | 369 |
}; |
370 | 370 |
|
371 | 371 |
/// \brief An empty idable base undirected graph class. |
372 | 372 |
/// |
373 | 373 |
/// This class provides beside the core undirected graph features |
374 | 374 |
/// core id functions for the undirected graph structure. The |
375 | 375 |
/// most of the base undirected graphs should be conform to this |
376 | 376 |
/// concept. The id's are unique and immutable. |
377 | 377 |
template <typename _Base = BaseGraphComponent> |
378 | 378 |
class IDableGraphComponent : public IDableDigraphComponent<_Base> { |
379 | 379 |
public: |
380 | 380 |
|
381 | 381 |
typedef _Base Base; |
382 | 382 |
typedef typename Base::Edge Edge; |
383 | 383 |
|
384 | 384 |
using IDableDigraphComponent<_Base>::id; |
385 | 385 |
|
386 | 386 |
/// \brief Gives back an unique integer id for the Edge. |
387 | 387 |
/// |
388 | 388 |
/// Gives back an unique integer id for the Edge. |
389 | 389 |
/// |
390 | 390 |
int id(const Edge&) const { return -1;} |
391 | 391 |
|
392 | 392 |
/// \brief Gives back the edge by the unique id. |
393 | 393 |
/// |
394 | 394 |
/// Gives back the edge by the unique id. If the |
395 | 395 |
/// graph does not contain arc with the given id then the |
396 | 396 |
/// result of the function is undetermined. |
397 | 397 |
Edge edgeFromId(int) const { return INVALID;} |
398 | 398 |
|
399 | 399 |
/// \brief Gives back an integer greater or equal to the maximum |
400 | 400 |
/// Edge id. |
401 | 401 |
/// |
402 | 402 |
/// Gives back an integer greater or equal to the maximum Edge |
403 | 403 |
/// id. |
404 | 404 |
int maxEdgeId() const { return -1;} |
405 | 405 |
|
406 | 406 |
template <typename _Graph> |
407 | 407 |
struct Constraints { |
408 | 408 |
|
409 | 409 |
void constraints() { |
410 | 410 |
checkConcept<Base, _Graph >(); |
411 | 411 |
checkConcept<IDableDigraphComponent<Base>, _Graph >(); |
412 | 412 |
typename _Graph::Edge edge; |
413 | 413 |
int ueid = graph.id(edge); |
414 | 414 |
ueid = graph.id(edge); |
415 | 415 |
edge = graph.edgeFromId(ueid); |
416 | 416 |
ueid = graph.maxEdgeId(); |
417 | 417 |
ignore_unused_variable_warning(ueid); |
418 | 418 |
} |
419 | 419 |
|
420 | 420 |
const _Graph& graph; |
421 | 421 |
}; |
422 | 422 |
}; |
423 | 423 |
|
424 | 424 |
/// \brief Skeleton class for graph NodeIt and ArcIt |
425 | 425 |
/// |
426 | 426 |
/// Skeleton class for graph NodeIt and ArcIt. |
427 | 427 |
/// |
428 | 428 |
template <typename _Graph, typename _Item> |
429 | 429 |
class GraphItemIt : public _Item { |
430 | 430 |
public: |
431 | 431 |
/// \brief Default constructor. |
432 | 432 |
/// |
433 | 433 |
/// @warning The default constructor sets the iterator |
434 | 434 |
/// to an undefined value. |
435 | 435 |
GraphItemIt() {} |
436 | 436 |
/// \brief Copy constructor. |
437 | 437 |
/// |
438 | 438 |
/// Copy constructor. |
439 | 439 |
/// |
440 | 440 |
GraphItemIt(const GraphItemIt& ) {} |
441 | 441 |
/// \brief Sets the iterator to the first item. |
442 | 442 |
/// |
443 | 443 |
/// Sets the iterator to the first item of \c the graph. |
444 | 444 |
/// |
445 | 445 |
explicit GraphItemIt(const _Graph&) {} |
446 | 446 |
/// \brief Invalid constructor \& conversion. |
447 | 447 |
/// |
448 | 448 |
/// This constructor initializes the item to be invalid. |
449 | 449 |
/// \sa Invalid for more details. |
450 | 450 |
GraphItemIt(Invalid) {} |
451 | 451 |
/// \brief Assign operator for items. |
452 | 452 |
/// |
453 | 453 |
/// The items are assignable. |
454 | 454 |
/// |
455 | 455 |
GraphItemIt& operator=(const GraphItemIt&) { return *this; } |
456 | 456 |
/// \brief Next item. |
457 | 457 |
/// |
458 | 458 |
/// Assign the iterator to the next item. |
459 | 459 |
/// |
460 | 460 |
GraphItemIt& operator++() { return *this; } |
461 | 461 |
/// \brief Equality operator |
462 | 462 |
/// |
463 | 463 |
/// Two iterators are equal if and only if they point to the |
464 | 464 |
/// same object or both are invalid. |
465 | 465 |
bool operator==(const GraphItemIt&) const { return true;} |
466 | 466 |
/// \brief Inequality operator |
467 | 467 |
/// |
468 | 468 |
/// \sa operator==(Node n) |
469 | 469 |
/// |
470 | 470 |
bool operator!=(const GraphItemIt&) const { return true;} |
471 | 471 |
|
472 | 472 |
template<typename _GraphItemIt> |
473 | 473 |
struct Constraints { |
474 | 474 |
void constraints() { |
475 | 475 |
_GraphItemIt it1(g); |
476 | 476 |
_GraphItemIt it2; |
477 | 477 |
|
478 | 478 |
it2 = ++it1; |
479 | 479 |
++it2 = it1; |
480 | 480 |
++(++it1); |
481 | 481 |
|
482 | 482 |
_Item bi = it1; |
483 | 483 |
bi = it2; |
484 | 484 |
} |
485 | 485 |
_Graph& g; |
486 | 486 |
}; |
487 | 487 |
}; |
488 | 488 |
|
489 | 489 |
/// \brief Skeleton class for graph InArcIt and OutArcIt |
490 | 490 |
/// |
491 | 491 |
/// \note Because InArcIt and OutArcIt may not inherit from the same |
492 | 492 |
/// base class, the _selector is a additional template parameter. For |
493 | 493 |
/// InArcIt you should instantiate it with character 'i' and for |
494 | 494 |
/// OutArcIt with 'o'. |
495 | 495 |
template <typename _Graph, |
496 | 496 |
typename _Item = typename _Graph::Arc, |
497 | 497 |
typename _Base = typename _Graph::Node, |
498 | 498 |
char _selector = '0'> |
499 | 499 |
class GraphIncIt : public _Item { |
500 | 500 |
public: |
501 | 501 |
/// \brief Default constructor. |
502 | 502 |
/// |
503 | 503 |
/// @warning The default constructor sets the iterator |
504 | 504 |
/// to an undefined value. |
505 | 505 |
GraphIncIt() {} |
506 | 506 |
/// \brief Copy constructor. |
507 | 507 |
/// |
508 | 508 |
/// Copy constructor. |
509 | 509 |
/// |
510 | 510 |
GraphIncIt(GraphIncIt const& gi) : _Item(gi) {} |
511 | 511 |
/// \brief Sets the iterator to the first arc incoming into or outgoing |
512 | 512 |
/// from the node. |
513 | 513 |
/// |
514 | 514 |
/// Sets the iterator to the first arc incoming into or outgoing |
515 | 515 |
/// from the node. |
516 | 516 |
/// |
517 | 517 |
explicit GraphIncIt(const _Graph&, const _Base&) {} |
518 | 518 |
/// \brief Invalid constructor \& conversion. |
519 | 519 |
/// |
520 | 520 |
/// This constructor initializes the item to be invalid. |
521 | 521 |
/// \sa Invalid for more details. |
522 | 522 |
GraphIncIt(Invalid) {} |
523 | 523 |
/// \brief Assign operator for iterators. |
524 | 524 |
/// |
525 | 525 |
/// The iterators are assignable. |
526 | 526 |
/// |
527 | 527 |
GraphIncIt& operator=(GraphIncIt const&) { return *this; } |
528 | 528 |
/// \brief Next item. |
529 | 529 |
/// |
530 | 530 |
/// Assign the iterator to the next item. |
531 | 531 |
/// |
532 | 532 |
GraphIncIt& operator++() { return *this; } |
533 | 533 |
|
534 | 534 |
/// \brief Equality operator |
535 | 535 |
/// |
536 | 536 |
/// Two iterators are equal if and only if they point to the |
537 | 537 |
/// same object or both are invalid. |
538 | 538 |
bool operator==(const GraphIncIt&) const { return true;} |
539 | 539 |
|
540 | 540 |
/// \brief Inequality operator |
541 | 541 |
/// |
542 | 542 |
/// \sa operator==(Node n) |
543 | 543 |
/// |
544 | 544 |
bool operator!=(const GraphIncIt&) const { return true;} |
545 | 545 |
|
546 | 546 |
template <typename _GraphIncIt> |
547 | 547 |
struct Constraints { |
548 | 548 |
void constraints() { |
549 | 549 |
checkConcept<GraphItem<_selector>, _GraphIncIt>(); |
550 | 550 |
_GraphIncIt it1(graph, node); |
551 | 551 |
_GraphIncIt it2; |
552 | 552 |
|
553 | 553 |
it2 = ++it1; |
554 | 554 |
++it2 = it1; |
555 | 555 |
++(++it1); |
556 | 556 |
_Item e = it1; |
557 | 557 |
e = it2; |
558 | 558 |
|
559 | 559 |
} |
560 | 560 |
|
561 | 561 |
_Item arc; |
562 | 562 |
_Base node; |
563 | 563 |
_Graph graph; |
564 | 564 |
_GraphIncIt it; |
565 | 565 |
}; |
566 | 566 |
}; |
567 | 567 |
|
568 | 568 |
|
569 | 569 |
/// \brief An empty iterable digraph class. |
570 | 570 |
/// |
571 | 571 |
/// This class provides beside the core digraph features |
572 | 572 |
/// iterator based iterable interface for the digraph structure. |
573 | 573 |
/// This concept is part of the Digraph concept. |
574 | 574 |
template <typename _Base = BaseDigraphComponent> |
575 | 575 |
class IterableDigraphComponent : public _Base { |
576 | 576 |
|
577 | 577 |
public: |
578 | 578 |
|
579 | 579 |
typedef _Base Base; |
580 | 580 |
typedef typename Base::Node Node; |
581 | 581 |
typedef typename Base::Arc Arc; |
582 | 582 |
|
583 | 583 |
typedef IterableDigraphComponent Digraph; |
584 | 584 |
|
585 | 585 |
/// \name Base iteration |
586 | 586 |
/// |
587 | 587 |
/// This interface provides functions for iteration on digraph items |
588 | 588 |
/// |
589 | 589 |
/// @{ |
590 | 590 |
|
591 | 591 |
/// \brief Gives back the first node in the iterating order. |
592 | 592 |
/// |
593 | 593 |
/// Gives back the first node in the iterating order. |
594 | 594 |
/// |
595 | 595 |
void first(Node&) const {} |
596 | 596 |
|
597 | 597 |
/// \brief Gives back the next node in the iterating order. |
598 | 598 |
/// |
599 | 599 |
/// Gives back the next node in the iterating order. |
600 | 600 |
/// |
601 | 601 |
void next(Node&) const {} |
602 | 602 |
|
603 | 603 |
/// \brief Gives back the first arc in the iterating order. |
604 | 604 |
/// |
605 | 605 |
/// Gives back the first arc in the iterating order. |
606 | 606 |
/// |
607 | 607 |
void first(Arc&) const {} |
608 | 608 |
|
609 | 609 |
/// \brief Gives back the next arc in the iterating order. |
610 | 610 |
/// |
611 | 611 |
/// Gives back the next arc in the iterating order. |
612 | 612 |
/// |
613 | 613 |
void next(Arc&) const {} |
614 | 614 |
|
615 | 615 |
|
616 | 616 |
/// \brief Gives back the first of the arcs point to the given |
617 | 617 |
/// node. |
618 | 618 |
/// |
619 | 619 |
/// Gives back the first of the arcs point to the given node. |
620 | 620 |
/// |
621 | 621 |
void firstIn(Arc&, const Node&) const {} |
622 | 622 |
|
623 | 623 |
/// \brief Gives back the next of the arcs points to the given |
624 | 624 |
/// node. |
625 | 625 |
/// |
626 | 626 |
/// Gives back the next of the arcs points to the given node. |
627 | 627 |
/// |
628 | 628 |
void nextIn(Arc&) const {} |
629 | 629 |
|
630 | 630 |
/// \brief Gives back the first of the arcs start from the |
631 | 631 |
/// given node. |
632 | 632 |
/// |
633 | 633 |
/// Gives back the first of the arcs start from the given node. |
634 | 634 |
/// |
635 | 635 |
void firstOut(Arc&, const Node&) const {} |
636 | 636 |
|
637 | 637 |
/// \brief Gives back the next of the arcs start from the given |
638 | 638 |
/// node. |
639 | 639 |
/// |
640 | 640 |
/// Gives back the next of the arcs start from the given node. |
641 | 641 |
/// |
642 | 642 |
void nextOut(Arc&) const {} |
643 | 643 |
|
644 | 644 |
/// @} |
645 | 645 |
|
646 | 646 |
/// \name Class based iteration |
647 | 647 |
/// |
648 | 648 |
/// This interface provides functions for iteration on digraph items |
649 | 649 |
/// |
650 | 650 |
/// @{ |
651 | 651 |
|
652 | 652 |
/// \brief This iterator goes through each node. |
653 | 653 |
/// |
654 | 654 |
/// This iterator goes through each node. |
655 | 655 |
/// |
656 | 656 |
typedef GraphItemIt<Digraph, Node> NodeIt; |
657 | 657 |
|
658 | 658 |
/// \brief This iterator goes through each node. |
659 | 659 |
/// |
660 | 660 |
/// This iterator goes through each node. |
661 | 661 |
/// |
662 | 662 |
typedef GraphItemIt<Digraph, Arc> ArcIt; |
663 | 663 |
|
664 | 664 |
/// \brief This iterator goes trough the incoming arcs of a node. |
665 | 665 |
/// |
666 | 666 |
/// This iterator goes trough the \e inccoming arcs of a certain node |
667 | 667 |
/// of a digraph. |
668 | 668 |
typedef GraphIncIt<Digraph, Arc, Node, 'i'> InArcIt; |
669 | 669 |
|
670 | 670 |
/// \brief This iterator goes trough the outgoing arcs of a node. |
671 | 671 |
/// |
672 | 672 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
673 | 673 |
/// of a digraph. |
674 | 674 |
typedef GraphIncIt<Digraph, Arc, Node, 'o'> OutArcIt; |
675 | 675 |
|
676 | 676 |
/// \brief The base node of the iterator. |
677 | 677 |
/// |
678 | 678 |
/// Gives back the base node of the iterator. |
679 | 679 |
/// It is always the target of the pointed arc. |
680 | 680 |
Node baseNode(const InArcIt&) const { return INVALID; } |
681 | 681 |
|
682 | 682 |
/// \brief The running node of the iterator. |
683 | 683 |
/// |
684 | 684 |
/// Gives back the running node of the iterator. |
685 | 685 |
/// It is always the source of the pointed arc. |
686 | 686 |
Node runningNode(const InArcIt&) const { return INVALID; } |
687 | 687 |
|
688 | 688 |
/// \brief The base node of the iterator. |
689 | 689 |
/// |
690 | 690 |
/// Gives back the base node of the iterator. |
691 | 691 |
/// It is always the source of the pointed arc. |
692 | 692 |
Node baseNode(const OutArcIt&) const { return INVALID; } |
693 | 693 |
|
694 | 694 |
/// \brief The running node of the iterator. |
695 | 695 |
/// |
696 | 696 |
/// Gives back the running node of the iterator. |
697 | 697 |
/// It is always the target of the pointed arc. |
698 | 698 |
Node runningNode(const OutArcIt&) const { return INVALID; } |
699 | 699 |
|
700 | 700 |
/// @} |
701 | 701 |
|
702 | 702 |
template <typename _Digraph> |
703 | 703 |
struct Constraints { |
704 | 704 |
void constraints() { |
705 | 705 |
checkConcept<Base, _Digraph>(); |
706 | 706 |
|
707 | 707 |
{ |
708 | 708 |
typename _Digraph::Node node(INVALID); |
709 | 709 |
typename _Digraph::Arc arc(INVALID); |
710 | 710 |
{ |
711 | 711 |
digraph.first(node); |
712 | 712 |
digraph.next(node); |
713 | 713 |
} |
714 | 714 |
{ |
715 | 715 |
digraph.first(arc); |
716 | 716 |
digraph.next(arc); |
717 | 717 |
} |
718 | 718 |
{ |
719 | 719 |
digraph.firstIn(arc, node); |
720 | 720 |
digraph.nextIn(arc); |
721 | 721 |
} |
722 | 722 |
{ |
723 | 723 |
digraph.firstOut(arc, node); |
724 | 724 |
digraph.nextOut(arc); |
725 | 725 |
} |
726 | 726 |
} |
727 | 727 |
|
728 | 728 |
{ |
729 | 729 |
checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>, |
730 | 730 |
typename _Digraph::ArcIt >(); |
731 | 731 |
checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>, |
732 | 732 |
typename _Digraph::NodeIt >(); |
733 | 733 |
checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, |
734 | 734 |
typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>(); |
735 | 735 |
checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, |
736 | 736 |
typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>(); |
737 | 737 |
|
738 | 738 |
typename _Digraph::Node n; |
739 | 739 |
typename _Digraph::InArcIt ieit(INVALID); |
740 | 740 |
typename _Digraph::OutArcIt oeit(INVALID); |
741 | 741 |
n = digraph.baseNode(ieit); |
742 | 742 |
n = digraph.runningNode(ieit); |
743 | 743 |
n = digraph.baseNode(oeit); |
744 | 744 |
n = digraph.runningNode(oeit); |
745 | 745 |
ignore_unused_variable_warning(n); |
746 | 746 |
} |
747 | 747 |
} |
748 | 748 |
|
749 | 749 |
const _Digraph& digraph; |
750 | 750 |
|
751 | 751 |
}; |
752 | 752 |
}; |
753 | 753 |
|
754 | 754 |
/// \brief An empty iterable undirected graph class. |
755 | 755 |
/// |
756 | 756 |
/// This class provides beside the core graph features iterator |
757 | 757 |
/// based iterable interface for the undirected graph structure. |
758 | 758 |
/// This concept is part of the Graph concept. |
759 | 759 |
template <typename _Base = BaseGraphComponent> |
760 | 760 |
class IterableGraphComponent : public IterableDigraphComponent<_Base> { |
761 | 761 |
public: |
762 | 762 |
|
763 | 763 |
typedef _Base Base; |
764 | 764 |
typedef typename Base::Node Node; |
765 | 765 |
typedef typename Base::Arc Arc; |
766 | 766 |
typedef typename Base::Edge Edge; |
767 | 767 |
|
768 | 768 |
|
769 | 769 |
typedef IterableGraphComponent Graph; |
770 | 770 |
|
771 | 771 |
/// \name Base iteration |
772 | 772 |
/// |
773 | 773 |
/// This interface provides functions for iteration on graph items |
774 | 774 |
/// @{ |
775 | 775 |
|
776 | 776 |
using IterableDigraphComponent<_Base>::first; |
777 | 777 |
using IterableDigraphComponent<_Base>::next; |
778 | 778 |
|
779 | 779 |
/// \brief Gives back the first edge in the iterating |
780 | 780 |
/// order. |
781 | 781 |
/// |
782 | 782 |
/// Gives back the first edge in the iterating order. |
783 | 783 |
/// |
784 | 784 |
void first(Edge&) const {} |
785 | 785 |
|
786 | 786 |
/// \brief Gives back the next edge in the iterating |
787 | 787 |
/// order. |
788 | 788 |
/// |
789 | 789 |
/// Gives back the next edge in the iterating order. |
790 | 790 |
/// |
791 | 791 |
void next(Edge&) const {} |
792 | 792 |
|
793 | 793 |
|
794 | 794 |
/// \brief Gives back the first of the edges from the |
795 | 795 |
/// given node. |
796 | 796 |
/// |
797 | 797 |
/// Gives back the first of the edges from the given |
798 | 798 |
/// node. The bool parameter gives back that direction which |
799 | 799 |
/// gives a good direction of the edge so the source of the |
800 | 800 |
/// directed arc is the given node. |
801 | 801 |
void firstInc(Edge&, bool&, const Node&) const {} |
802 | 802 |
|
803 | 803 |
/// \brief Gives back the next of the edges from the |
804 | 804 |
/// given node. |
805 | 805 |
/// |
806 | 806 |
/// Gives back the next of the edges from the given |
807 | 807 |
/// node. The bool parameter should be used as the \c firstInc() |
808 | 808 |
/// use it. |
809 | 809 |
void nextInc(Edge&, bool&) const {} |
810 | 810 |
|
811 | 811 |
using IterableDigraphComponent<_Base>::baseNode; |
812 | 812 |
using IterableDigraphComponent<_Base>::runningNode; |
813 | 813 |
|
814 | 814 |
/// @} |
815 | 815 |
|
816 | 816 |
/// \name Class based iteration |
817 | 817 |
/// |
818 | 818 |
/// This interface provides functions for iteration on graph items |
819 | 819 |
/// |
820 | 820 |
/// @{ |
821 | 821 |
|
822 | 822 |
/// \brief This iterator goes through each node. |
823 | 823 |
/// |
824 | 824 |
/// This iterator goes through each node. |
825 | 825 |
typedef GraphItemIt<Graph, Edge> EdgeIt; |
826 | 826 |
/// \brief This iterator goes trough the incident arcs of a |
827 | 827 |
/// node. |
828 | 828 |
/// |
829 | 829 |
/// This iterator goes trough the incident arcs of a certain |
830 | 830 |
/// node of a graph. |
831 | 831 |
typedef GraphIncIt<Graph, Edge, Node, 'u'> IncEdgeIt; |
832 | 832 |
/// \brief The base node of the iterator. |
833 | 833 |
/// |
834 | 834 |
/// Gives back the base node of the iterator. |
835 | 835 |
Node baseNode(const IncEdgeIt&) const { return INVALID; } |
836 | 836 |
|
837 | 837 |
/// \brief The running node of the iterator. |
838 | 838 |
/// |
839 | 839 |
/// Gives back the running node of the iterator. |
840 | 840 |
Node runningNode(const IncEdgeIt&) const { return INVALID; } |
841 | 841 |
|
842 | 842 |
/// @} |
843 | 843 |
|
844 | 844 |
template <typename _Graph> |
845 | 845 |
struct Constraints { |
846 | 846 |
void constraints() { |
847 | 847 |
checkConcept<IterableDigraphComponent<Base>, _Graph>(); |
848 | 848 |
|
849 | 849 |
{ |
850 | 850 |
typename _Graph::Node node(INVALID); |
851 | 851 |
typename _Graph::Edge edge(INVALID); |
852 | 852 |
bool dir; |
853 | 853 |
{ |
854 | 854 |
graph.first(edge); |
855 | 855 |
graph.next(edge); |
856 | 856 |
} |
857 | 857 |
{ |
858 | 858 |
graph.firstInc(edge, dir, node); |
859 | 859 |
graph.nextInc(edge, dir); |
860 | 860 |
} |
861 | 861 |
|
862 | 862 |
} |
863 | 863 |
|
864 | 864 |
{ |
865 | 865 |
checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>, |
866 | 866 |
typename _Graph::EdgeIt >(); |
867 | 867 |
checkConcept<GraphIncIt<_Graph, typename _Graph::Edge, |
868 | 868 |
typename _Graph::Node, 'u'>, typename _Graph::IncEdgeIt>(); |
869 | 869 |
|
870 | 870 |
typename _Graph::Node n; |
871 | 871 |
typename _Graph::IncEdgeIt ueit(INVALID); |
872 | 872 |
n = graph.baseNode(ueit); |
873 | 873 |
n = graph.runningNode(ueit); |
874 | 874 |
} |
875 | 875 |
} |
876 | 876 |
|
877 | 877 |
const _Graph& graph; |
878 | 878 |
|
879 | 879 |
}; |
880 | 880 |
}; |
881 | 881 |
|
882 | 882 |
/// \brief An empty alteration notifier digraph class. |
883 | 883 |
/// |
884 | 884 |
/// This class provides beside the core digraph features alteration |
885 | 885 |
/// notifier interface for the digraph structure. This implements |
886 | 886 |
/// an observer-notifier pattern for each digraph item. More |
887 | 887 |
/// obsevers can be registered into the notifier and whenever an |
888 | 888 |
/// alteration occured in the digraph all the observers will |
889 | 889 |
/// notified about it. |
890 | 890 |
template <typename _Base = BaseDigraphComponent> |
891 | 891 |
class AlterableDigraphComponent : public _Base { |
892 | 892 |
public: |
893 | 893 |
|
894 | 894 |
typedef _Base Base; |
895 | 895 |
typedef typename Base::Node Node; |
896 | 896 |
typedef typename Base::Arc Arc; |
897 | 897 |
|
898 | 898 |
|
899 | 899 |
/// The node observer registry. |
900 | 900 |
typedef AlterationNotifier<AlterableDigraphComponent, Node> |
901 | 901 |
NodeNotifier; |
902 | 902 |
/// The arc observer registry. |
903 | 903 |
typedef AlterationNotifier<AlterableDigraphComponent, Arc> |
904 | 904 |
ArcNotifier; |
905 | 905 |
|
906 | 906 |
/// \brief Gives back the node alteration notifier. |
907 | 907 |
/// |
908 | 908 |
/// Gives back the node alteration notifier. |
909 | 909 |
NodeNotifier& notifier(Node) const { |
910 | 910 |
return NodeNotifier(); |
911 | 911 |
} |
912 | 912 |
|
913 | 913 |
/// \brief Gives back the arc alteration notifier. |
914 | 914 |
/// |
915 | 915 |
/// Gives back the arc alteration notifier. |
916 | 916 |
ArcNotifier& notifier(Arc) const { |
917 | 917 |
return ArcNotifier(); |
918 | 918 |
} |
919 | 919 |
|
920 | 920 |
template <typename _Digraph> |
921 | 921 |
struct Constraints { |
922 | 922 |
void constraints() { |
923 | 923 |
checkConcept<Base, _Digraph>(); |
924 | 924 |
typename _Digraph::NodeNotifier& nn |
925 | 925 |
= digraph.notifier(typename _Digraph::Node()); |
926 | 926 |
|
927 | 927 |
typename _Digraph::ArcNotifier& en |
928 | 928 |
= digraph.notifier(typename _Digraph::Arc()); |
929 | 929 |
|
930 | 930 |
ignore_unused_variable_warning(nn); |
931 | 931 |
ignore_unused_variable_warning(en); |
932 | 932 |
} |
933 | 933 |
|
934 | 934 |
const _Digraph& digraph; |
935 | 935 |
|
936 | 936 |
}; |
937 | 937 |
|
938 | 938 |
}; |
939 | 939 |
|
940 | 940 |
/// \brief An empty alteration notifier undirected graph class. |
941 | 941 |
/// |
942 | 942 |
/// This class provides beside the core graph features alteration |
943 | 943 |
/// notifier interface for the graph structure. This implements |
944 | 944 |
/// an observer-notifier pattern for each graph item. More |
945 | 945 |
/// obsevers can be registered into the notifier and whenever an |
946 | 946 |
/// alteration occured in the graph all the observers will |
947 | 947 |
/// notified about it. |
948 | 948 |
template <typename _Base = BaseGraphComponent> |
949 | 949 |
class AlterableGraphComponent : public AlterableDigraphComponent<_Base> { |
950 | 950 |
public: |
951 | 951 |
|
952 | 952 |
typedef _Base Base; |
953 | 953 |
typedef typename Base::Edge Edge; |
954 | 954 |
|
955 | 955 |
|
956 | 956 |
/// The arc observer registry. |
957 | 957 |
typedef AlterationNotifier<AlterableGraphComponent, Edge> |
958 | 958 |
EdgeNotifier; |
959 | 959 |
|
960 | 960 |
/// \brief Gives back the arc alteration notifier. |
961 | 961 |
/// |
962 | 962 |
/// Gives back the arc alteration notifier. |
963 | 963 |
EdgeNotifier& notifier(Edge) const { |
964 | 964 |
return EdgeNotifier(); |
965 | 965 |
} |
966 | 966 |
|
967 | 967 |
template <typename _Graph> |
968 | 968 |
struct Constraints { |
969 | 969 |
void constraints() { |
970 | 970 |
checkConcept<AlterableGraphComponent<Base>, _Graph>(); |
971 | 971 |
typename _Graph::EdgeNotifier& uen |
972 | 972 |
= graph.notifier(typename _Graph::Edge()); |
973 | 973 |
ignore_unused_variable_warning(uen); |
974 | 974 |
} |
975 | 975 |
|
976 | 976 |
const _Graph& graph; |
977 | 977 |
|
978 | 978 |
}; |
979 | 979 |
|
980 | 980 |
}; |
981 | 981 |
|
982 | 982 |
/// \brief Class describing the concept of graph maps |
983 | 983 |
/// |
984 | 984 |
/// This class describes the common interface of the graph maps |
985 | 985 |
/// (NodeMap, ArcMap), that is \ref maps-page "maps" which can be used to |
986 | 986 |
/// associate data to graph descriptors (nodes or arcs). |
987 | 987 |
template <typename _Graph, typename _Item, typename _Value> |
988 | 988 |
class GraphMap : public ReadWriteMap<_Item, _Value> { |
989 | 989 |
public: |
990 | 990 |
|
991 | 991 |
typedef ReadWriteMap<_Item, _Value> Parent; |
992 | 992 |
|
993 | 993 |
/// The graph type of the map. |
994 | 994 |
typedef _Graph Graph; |
995 | 995 |
/// The key type of the map. |
996 | 996 |
typedef _Item Key; |
997 | 997 |
/// The value type of the map. |
998 | 998 |
typedef _Value Value; |
999 | 999 |
|
1000 | 1000 |
/// \brief Construct a new map. |
1001 | 1001 |
/// |
1002 | 1002 |
/// Construct a new map for the graph. |
1003 | 1003 |
explicit GraphMap(const Graph&) {} |
1004 | 1004 |
/// \brief Construct a new map with default value. |
1005 | 1005 |
/// |
1006 | 1006 |
/// Construct a new map for the graph and initalise the values. |
1007 | 1007 |
GraphMap(const Graph&, const Value&) {} |
1008 | 1008 |
/// \brief Copy constructor. |
1009 | 1009 |
/// |
1010 | 1010 |
/// Copy Constructor. |
1011 | 1011 |
GraphMap(const GraphMap&) : Parent() {} |
1012 | 1012 |
|
1013 | 1013 |
/// \brief Assign operator. |
1014 | 1014 |
/// |
1015 | 1015 |
/// Assign operator. It does not mofify the underlying graph, |
1016 | 1016 |
/// it just iterates on the current item set and set the map |
1017 | 1017 |
/// with the value returned by the assigned map. |
1018 | 1018 |
template <typename CMap> |
1019 | 1019 |
GraphMap& operator=(const CMap&) { |
1020 | 1020 |
checkConcept<ReadMap<Key, Value>, CMap>(); |
1021 | 1021 |
return *this; |
1022 | 1022 |
} |
1023 | 1023 |
|
1024 | 1024 |
template<typename _Map> |
1025 | 1025 |
struct Constraints { |
1026 | 1026 |
void constraints() { |
1027 | 1027 |
checkConcept<ReadWriteMap<Key, Value>, _Map >(); |
1028 | 1028 |
// Construction with a graph parameter |
1029 | 1029 |
_Map a(g); |
1030 | 1030 |
// Constructor with a graph and a default value parameter |
1031 | 1031 |
_Map a2(g,t); |
1032 | 1032 |
// Copy constructor. |
1033 | 1033 |
_Map b(c); |
1034 | 1034 |
|
1035 | 1035 |
ReadMap<Key, Value> cmap; |
1036 | 1036 |
b = cmap; |
1037 | 1037 |
|
1038 | 1038 |
ignore_unused_variable_warning(a2); |
1039 | 1039 |
ignore_unused_variable_warning(b); |
1040 | 1040 |
} |
1041 | 1041 |
|
1042 | 1042 |
const _Map &c; |
1043 | 1043 |
const Graph &g; |
1044 | 1044 |
const typename GraphMap::Value &t; |
1045 | 1045 |
}; |
1046 | 1046 |
|
1047 | 1047 |
}; |
1048 | 1048 |
|
1049 | 1049 |
/// \brief An empty mappable digraph class. |
1050 | 1050 |
/// |
1051 | 1051 |
/// This class provides beside the core digraph features |
1052 | 1052 |
/// map interface for the digraph structure. |
1053 | 1053 |
/// This concept is part of the Digraph concept. |
1054 | 1054 |
template <typename _Base = BaseDigraphComponent> |
1055 | 1055 |
class MappableDigraphComponent : public _Base { |
1056 | 1056 |
public: |
1057 | 1057 |
|
1058 | 1058 |
typedef _Base Base; |
1059 | 1059 |
typedef typename Base::Node Node; |
1060 | 1060 |
typedef typename Base::Arc Arc; |
1061 | 1061 |
|
1062 | 1062 |
typedef MappableDigraphComponent Digraph; |
1063 | 1063 |
|
1064 | 1064 |
/// \brief ReadWrite map of the nodes. |
1065 | 1065 |
/// |
1066 | 1066 |
/// ReadWrite map of the nodes. |
1067 | 1067 |
/// |
1068 | 1068 |
template <typename _Value> |
1069 | 1069 |
class NodeMap : public GraphMap<Digraph, Node, _Value> { |
1070 | 1070 |
public: |
1071 | 1071 |
typedef GraphMap<MappableDigraphComponent, Node, _Value> Parent; |
1072 | 1072 |
|
1073 | 1073 |
/// \brief Construct a new map. |
1074 | 1074 |
/// |
1075 | 1075 |
/// Construct a new map for the digraph. |
1076 | 1076 |
explicit NodeMap(const MappableDigraphComponent& digraph) |
1077 | 1077 |
: Parent(digraph) {} |
1078 | 1078 |
|
1079 | 1079 |
/// \brief Construct a new map with default value. |
1080 | 1080 |
/// |
1081 | 1081 |
/// Construct a new map for the digraph and initalise the values. |
1082 | 1082 |
NodeMap(const MappableDigraphComponent& digraph, const _Value& value) |
1083 | 1083 |
: Parent(digraph, value) {} |
1084 | 1084 |
|
1085 | 1085 |
/// \brief Copy constructor. |
1086 | 1086 |
/// |
1087 | 1087 |
/// Copy Constructor. |
1088 | 1088 |
NodeMap(const NodeMap& nm) : Parent(nm) {} |
1089 | 1089 |
|
1090 | 1090 |
/// \brief Assign operator. |
1091 | 1091 |
/// |
1092 | 1092 |
/// Assign operator. |
1093 | 1093 |
template <typename CMap> |
1094 | 1094 |
NodeMap& operator=(const CMap&) { |
1095 | 1095 |
checkConcept<ReadMap<Node, _Value>, CMap>(); |
1096 | 1096 |
return *this; |
1097 | 1097 |
} |
1098 | 1098 |
|
1099 | 1099 |
}; |
1100 | 1100 |
|
1101 | 1101 |
/// \brief ReadWrite map of the arcs. |
1102 | 1102 |
/// |
1103 | 1103 |
/// ReadWrite map of the arcs. |
1104 | 1104 |
/// |
1105 | 1105 |
template <typename _Value> |
1106 | 1106 |
class ArcMap : public GraphMap<Digraph, Arc, _Value> { |
1107 | 1107 |
public: |
1108 | 1108 |
typedef GraphMap<MappableDigraphComponent, Arc, _Value> Parent; |
1109 | 1109 |
|
1110 | 1110 |
/// \brief Construct a new map. |
1111 | 1111 |
/// |
1112 | 1112 |
/// Construct a new map for the digraph. |
1113 | 1113 |
explicit ArcMap(const MappableDigraphComponent& digraph) |
1114 | 1114 |
: Parent(digraph) {} |
1115 | 1115 |
|
1116 | 1116 |
/// \brief Construct a new map with default value. |
1117 | 1117 |
/// |
1118 | 1118 |
/// Construct a new map for the digraph and initalise the values. |
1119 | 1119 |
ArcMap(const MappableDigraphComponent& digraph, const _Value& value) |
1120 | 1120 |
: Parent(digraph, value) {} |
1121 | 1121 |
|
1122 | 1122 |
/// \brief Copy constructor. |
1123 | 1123 |
/// |
1124 | 1124 |
/// Copy Constructor. |
1125 | 1125 |
ArcMap(const ArcMap& nm) : Parent(nm) {} |
1126 | 1126 |
|
1127 | 1127 |
/// \brief Assign operator. |
1128 | 1128 |
/// |
1129 | 1129 |
/// Assign operator. |
1130 | 1130 |
template <typename CMap> |
1131 | 1131 |
ArcMap& operator=(const CMap&) { |
1132 | 1132 |
checkConcept<ReadMap<Arc, _Value>, CMap>(); |
1133 | 1133 |
return *this; |
1134 | 1134 |
} |
1135 | 1135 |
|
1136 | 1136 |
}; |
1137 | 1137 |
|
1138 | 1138 |
|
1139 | 1139 |
template <typename _Digraph> |
1140 | 1140 |
struct Constraints { |
1141 | 1141 |
|
1142 | 1142 |
struct Dummy { |
1143 | 1143 |
int value; |
1144 | 1144 |
Dummy() : value(0) {} |
1145 | 1145 |
Dummy(int _v) : value(_v) {} |
1146 | 1146 |
}; |
1147 | 1147 |
|
1148 | 1148 |
void constraints() { |
1149 | 1149 |
checkConcept<Base, _Digraph>(); |
1150 | 1150 |
{ // int map test |
1151 | 1151 |
typedef typename _Digraph::template NodeMap<int> IntNodeMap; |
1152 | 1152 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>, |
1153 | 1153 |
IntNodeMap >(); |
1154 | 1154 |
} { // bool map test |
1155 | 1155 |
typedef typename _Digraph::template NodeMap<bool> BoolNodeMap; |
1156 | 1156 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>, |
1157 | 1157 |
BoolNodeMap >(); |
1158 | 1158 |
} { // Dummy map test |
1159 | 1159 |
typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap; |
1160 | 1160 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>, |
1161 | 1161 |
DummyNodeMap >(); |
1162 | 1162 |
} |
1163 | 1163 |
|
1164 | 1164 |
{ // int map test |
1165 | 1165 |
typedef typename _Digraph::template ArcMap<int> IntArcMap; |
1166 | 1166 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>, |
1167 | 1167 |
IntArcMap >(); |
1168 | 1168 |
} { // bool map test |
1169 | 1169 |
typedef typename _Digraph::template ArcMap<bool> BoolArcMap; |
1170 | 1170 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>, |
1171 | 1171 |
BoolArcMap >(); |
1172 | 1172 |
} { // Dummy map test |
1173 | 1173 |
typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap; |
1174 | 1174 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>, |
1175 | 1175 |
DummyArcMap >(); |
1176 | 1176 |
} |
1177 | 1177 |
} |
1178 | 1178 |
|
1179 | 1179 |
_Digraph& digraph; |
1180 | 1180 |
}; |
1181 | 1181 |
}; |
1182 | 1182 |
|
1183 | 1183 |
/// \brief An empty mappable base bipartite graph class. |
1184 | 1184 |
/// |
1185 | 1185 |
/// This class provides beside the core graph features |
1186 | 1186 |
/// map interface for the graph structure. |
1187 | 1187 |
/// This concept is part of the Graph concept. |
1188 | 1188 |
template <typename _Base = BaseGraphComponent> |
1189 | 1189 |
class MappableGraphComponent : public MappableDigraphComponent<_Base> { |
1190 | 1190 |
public: |
1191 | 1191 |
|
1192 | 1192 |
typedef _Base Base; |
1193 | 1193 |
typedef typename Base::Edge Edge; |
1194 | 1194 |
|
1195 | 1195 |
typedef MappableGraphComponent Graph; |
1196 | 1196 |
|
1197 | 1197 |
/// \brief ReadWrite map of the edges. |
1198 | 1198 |
/// |
1199 | 1199 |
/// ReadWrite map of the edges. |
1200 | 1200 |
/// |
1201 | 1201 |
template <typename _Value> |
1202 | 1202 |
class EdgeMap : public GraphMap<Graph, Edge, _Value> { |
1203 | 1203 |
public: |
1204 | 1204 |
typedef GraphMap<MappableGraphComponent, Edge, _Value> Parent; |
1205 | 1205 |
|
1206 | 1206 |
/// \brief Construct a new map. |
1207 | 1207 |
/// |
1208 | 1208 |
/// Construct a new map for the graph. |
1209 | 1209 |
explicit EdgeMap(const MappableGraphComponent& graph) |
1210 | 1210 |
: Parent(graph) {} |
1211 | 1211 |
|
1212 | 1212 |
/// \brief Construct a new map with default value. |
1213 | 1213 |
/// |
1214 | 1214 |
/// Construct a new map for the graph and initalise the values. |
1215 | 1215 |
EdgeMap(const MappableGraphComponent& graph, const _Value& value) |
1216 | 1216 |
: Parent(graph, value) {} |
1217 | 1217 |
|
1218 | 1218 |
/// \brief Copy constructor. |
1219 | 1219 |
/// |
1220 | 1220 |
/// Copy Constructor. |
1221 | 1221 |
EdgeMap(const EdgeMap& nm) : Parent(nm) {} |
1222 | 1222 |
|
1223 | 1223 |
/// \brief Assign operator. |
1224 | 1224 |
/// |
1225 | 1225 |
/// Assign operator. |
1226 | 1226 |
template <typename CMap> |
1227 | 1227 |
EdgeMap& operator=(const CMap&) { |
1228 | 1228 |
checkConcept<ReadMap<Edge, _Value>, CMap>(); |
1229 | 1229 |
return *this; |
1230 | 1230 |
} |
1231 | 1231 |
|
1232 | 1232 |
}; |
1233 | 1233 |
|
1234 | 1234 |
|
1235 | 1235 |
template <typename _Graph> |
1236 | 1236 |
struct Constraints { |
1237 | 1237 |
|
1238 | 1238 |
struct Dummy { |
1239 | 1239 |
int value; |
1240 | 1240 |
Dummy() : value(0) {} |
1241 | 1241 |
Dummy(int _v) : value(_v) {} |
1242 | 1242 |
}; |
1243 | 1243 |
|
1244 | 1244 |
void constraints() { |
1245 | 1245 |
checkConcept<MappableGraphComponent<Base>, _Graph>(); |
1246 | 1246 |
|
1247 | 1247 |
{ // int map test |
1248 | 1248 |
typedef typename _Graph::template EdgeMap<int> IntEdgeMap; |
1249 | 1249 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>, |
1250 | 1250 |
IntEdgeMap >(); |
1251 | 1251 |
} { // bool map test |
1252 | 1252 |
typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap; |
1253 | 1253 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>, |
1254 | 1254 |
BoolEdgeMap >(); |
1255 | 1255 |
} { // Dummy map test |
1256 | 1256 |
typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap; |
1257 | 1257 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>, |
1258 | 1258 |
DummyEdgeMap >(); |
1259 | 1259 |
} |
1260 | 1260 |
} |
1261 | 1261 |
|
1262 | 1262 |
_Graph& graph; |
1263 | 1263 |
}; |
1264 | 1264 |
}; |
1265 | 1265 |
|
1266 | 1266 |
/// \brief An empty extendable digraph class. |
1267 | 1267 |
/// |
1268 | 1268 |
/// This class provides beside the core digraph features digraph |
1269 | 1269 |
/// extendable interface for the digraph structure. The main |
1270 | 1270 |
/// difference between the base and this interface is that the |
1271 | 1271 |
/// digraph alterations should handled already on this level. |
1272 | 1272 |
template <typename _Base = BaseDigraphComponent> |
1273 | 1273 |
class ExtendableDigraphComponent : public _Base { |
1274 | 1274 |
public: |
1275 | 1275 |
typedef _Base Base; |
1276 | 1276 |
|
1277 | 1277 |
typedef typename _Base::Node Node; |
1278 | 1278 |
typedef typename _Base::Arc Arc; |
1279 | 1279 |
|
1280 | 1280 |
/// \brief Adds a new node to the digraph. |
1281 | 1281 |
/// |
1282 | 1282 |
/// Adds a new node to the digraph. |
1283 | 1283 |
/// |
1284 | 1284 |
Node addNode() { |
1285 | 1285 |
return INVALID; |
1286 | 1286 |
} |
1287 | 1287 |
|
1288 | 1288 |
/// \brief Adds a new arc connects the given two nodes. |
1289 | 1289 |
/// |
1290 | 1290 |
/// Adds a new arc connects the the given two nodes. |
1291 | 1291 |
Arc addArc(const Node&, const Node&) { |
1292 | 1292 |
return INVALID; |
1293 | 1293 |
} |
1294 | 1294 |
|
1295 | 1295 |
template <typename _Digraph> |
1296 | 1296 |
struct Constraints { |
1297 | 1297 |
void constraints() { |
1298 | 1298 |
checkConcept<Base, _Digraph>(); |
1299 | 1299 |
typename _Digraph::Node node_a, node_b; |
1300 | 1300 |
node_a = digraph.addNode(); |
1301 | 1301 |
node_b = digraph.addNode(); |
1302 | 1302 |
typename _Digraph::Arc arc; |
1303 | 1303 |
arc = digraph.addArc(node_a, node_b); |
1304 | 1304 |
} |
1305 | 1305 |
|
1306 | 1306 |
_Digraph& digraph; |
1307 | 1307 |
}; |
1308 | 1308 |
}; |
1309 | 1309 |
|
1310 | 1310 |
/// \brief An empty extendable base undirected graph class. |
1311 | 1311 |
/// |
1312 | 1312 |
/// This class provides beside the core undirected graph features |
1313 | 1313 |
/// core undircted graph extend interface for the graph structure. |
1314 | 1314 |
/// The main difference between the base and this interface is |
1315 | 1315 |
/// that the graph alterations should handled already on this |
1316 | 1316 |
/// level. |
1317 | 1317 |
template <typename _Base = BaseGraphComponent> |
1318 | 1318 |
class ExtendableGraphComponent : public _Base { |
1319 | 1319 |
public: |
1320 | 1320 |
|
1321 | 1321 |
typedef _Base Base; |
1322 | 1322 |
typedef typename _Base::Node Node; |
1323 | 1323 |
typedef typename _Base::Edge Edge; |
1324 | 1324 |
|
1325 | 1325 |
/// \brief Adds a new node to the graph. |
1326 | 1326 |
/// |
1327 | 1327 |
/// Adds a new node to the graph. |
1328 | 1328 |
/// |
1329 | 1329 |
Node addNode() { |
1330 | 1330 |
return INVALID; |
1331 | 1331 |
} |
1332 | 1332 |
|
1333 | 1333 |
/// \brief Adds a new arc connects the given two nodes. |
1334 | 1334 |
/// |
1335 | 1335 |
/// Adds a new arc connects the the given two nodes. |
1336 | 1336 |
Edge addArc(const Node&, const Node&) { |
1337 | 1337 |
return INVALID; |
1338 | 1338 |
} |
1339 | 1339 |
|
1340 | 1340 |
template <typename _Graph> |
1341 | 1341 |
struct Constraints { |
1342 | 1342 |
void constraints() { |
1343 | 1343 |
checkConcept<Base, _Graph>(); |
1344 | 1344 |
typename _Graph::Node node_a, node_b; |
1345 | 1345 |
node_a = graph.addNode(); |
1346 | 1346 |
node_b = graph.addNode(); |
1347 | 1347 |
typename _Graph::Edge edge; |
1348 | 1348 |
edge = graph.addEdge(node_a, node_b); |
1349 | 1349 |
} |
1350 | 1350 |
|
1351 | 1351 |
_Graph& graph; |
1352 | 1352 |
}; |
1353 | 1353 |
}; |
1354 | 1354 |
|
1355 | 1355 |
/// \brief An empty erasable digraph class. |
1356 | 1356 |
/// |
1357 | 1357 |
/// This class provides beside the core digraph features core erase |
1358 | 1358 |
/// functions for the digraph structure. The main difference between |
1359 | 1359 |
/// the base and this interface is that the digraph alterations |
1360 | 1360 |
/// should handled already on this level. |
1361 | 1361 |
template <typename _Base = BaseDigraphComponent> |
1362 | 1362 |
class ErasableDigraphComponent : public _Base { |
1363 | 1363 |
public: |
1364 | 1364 |
|
1365 | 1365 |
typedef _Base Base; |
1366 | 1366 |
typedef typename Base::Node Node; |
1367 | 1367 |
typedef typename Base::Arc Arc; |
1368 | 1368 |
|
1369 | 1369 |
/// \brief Erase a node from the digraph. |
1370 | 1370 |
/// |
1371 | 1371 |
/// Erase a node from the digraph. This function should |
1372 | 1372 |
/// erase all arcs connecting to the node. |
1373 | 1373 |
void erase(const Node&) {} |
1374 | 1374 |
|
1375 | 1375 |
/// \brief Erase an arc from the digraph. |
1376 | 1376 |
/// |
1377 | 1377 |
/// Erase an arc from the digraph. |
1378 | 1378 |
/// |
1379 | 1379 |
void erase(const Arc&) {} |
1380 | 1380 |
|
1381 | 1381 |
template <typename _Digraph> |
1382 | 1382 |
struct Constraints { |
1383 | 1383 |
void constraints() { |
1384 | 1384 |
checkConcept<Base, _Digraph>(); |
1385 | 1385 |
typename _Digraph::Node node; |
1386 | 1386 |
digraph.erase(node); |
1387 | 1387 |
typename _Digraph::Arc arc; |
1388 | 1388 |
digraph.erase(arc); |
1389 | 1389 |
} |
1390 | 1390 |
|
1391 | 1391 |
_Digraph& digraph; |
1392 | 1392 |
}; |
1393 | 1393 |
}; |
1394 | 1394 |
|
1395 | 1395 |
/// \brief An empty erasable base undirected graph class. |
1396 | 1396 |
/// |
1397 | 1397 |
/// This class provides beside the core undirected graph features |
1398 | 1398 |
/// core erase functions for the undirceted graph structure. The |
1399 | 1399 |
/// main difference between the base and this interface is that |
1400 | 1400 |
/// the graph alterations should handled already on this level. |
1401 | 1401 |
template <typename _Base = BaseGraphComponent> |
1402 | 1402 |
class ErasableGraphComponent : public _Base { |
1403 | 1403 |
public: |
1404 | 1404 |
|
1405 | 1405 |
typedef _Base Base; |
1406 | 1406 |
typedef typename Base::Node Node; |
1407 | 1407 |
typedef typename Base::Edge Edge; |
1408 | 1408 |
|
1409 | 1409 |
/// \brief Erase a node from the graph. |
1410 | 1410 |
/// |
1411 | 1411 |
/// Erase a node from the graph. This function should erase |
1412 | 1412 |
/// arcs connecting to the node. |
1413 | 1413 |
void erase(const Node&) {} |
1414 | 1414 |
|
1415 | 1415 |
/// \brief Erase an arc from the graph. |
1416 | 1416 |
/// |
1417 | 1417 |
/// Erase an arc from the graph. |
1418 | 1418 |
/// |
1419 | 1419 |
void erase(const Edge&) {} |
1420 | 1420 |
|
1421 | 1421 |
template <typename _Graph> |
1422 | 1422 |
struct Constraints { |
1423 | 1423 |
void constraints() { |
1424 | 1424 |
checkConcept<Base, _Graph>(); |
1425 | 1425 |
typename _Graph::Node node; |
1426 | 1426 |
graph.erase(node); |
1427 | 1427 |
typename _Graph::Edge edge; |
1428 | 1428 |
graph.erase(edge); |
1429 | 1429 |
} |
1430 | 1430 |
|
1431 | 1431 |
_Graph& graph; |
1432 | 1432 |
}; |
1433 | 1433 |
}; |
1434 | 1434 |
|
1435 | 1435 |
/// \brief An empty clearable base digraph class. |
1436 | 1436 |
/// |
1437 | 1437 |
/// This class provides beside the core digraph features core clear |
1438 | 1438 |
/// functions for the digraph structure. The main difference between |
1439 | 1439 |
/// the base and this interface is that the digraph alterations |
1440 | 1440 |
/// should handled already on this level. |
1441 | 1441 |
template <typename _Base = BaseDigraphComponent> |
1442 | 1442 |
class ClearableDigraphComponent : public _Base { |
1443 | 1443 |
public: |
1444 | 1444 |
|
1445 | 1445 |
typedef _Base Base; |
1446 | 1446 |
|
1447 | 1447 |
/// \brief Erase all nodes and arcs from the digraph. |
1448 | 1448 |
/// |
1449 | 1449 |
/// Erase all nodes and arcs from the digraph. |
1450 | 1450 |
/// |
1451 | 1451 |
void clear() {} |
1452 | 1452 |
|
1453 | 1453 |
template <typename _Digraph> |
1454 | 1454 |
struct Constraints { |
1455 | 1455 |
void constraints() { |
1456 | 1456 |
checkConcept<Base, _Digraph>(); |
1457 | 1457 |
digraph.clear(); |
1458 | 1458 |
} |
1459 | 1459 |
|
1460 | 1460 |
_Digraph digraph; |
1461 | 1461 |
}; |
1462 | 1462 |
}; |
1463 | 1463 |
|
1464 | 1464 |
/// \brief An empty clearable base undirected graph class. |
1465 | 1465 |
/// |
1466 | 1466 |
/// This class provides beside the core undirected graph features |
1467 | 1467 |
/// core clear functions for the undirected graph structure. The |
1468 | 1468 |
/// main difference between the base and this interface is that |
1469 | 1469 |
/// the graph alterations should handled already on this level. |
1470 | 1470 |
template <typename _Base = BaseGraphComponent> |
1471 | 1471 |
class ClearableGraphComponent : public ClearableDigraphComponent<_Base> { |
1472 | 1472 |
public: |
1473 | 1473 |
|
1474 | 1474 |
typedef _Base Base; |
1475 | 1475 |
|
1476 | 1476 |
template <typename _Graph> |
1477 | 1477 |
struct Constraints { |
1478 | 1478 |
void constraints() { |
1479 | 1479 |
checkConcept<ClearableGraphComponent<Base>, _Graph>(); |
1480 | 1480 |
} |
1481 | 1481 |
|
1482 | 1482 |
_Graph graph; |
1483 | 1483 |
}; |
1484 | 1484 |
}; |
1485 | 1485 |
|
1486 | 1486 |
} |
1487 | 1487 |
|
1488 | 1488 |
} |
1489 | 1489 |
|
1490 | 1490 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of heaps. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPT_HEAP_H |
24 | 24 |
#define LEMON_CONCEPT_HEAP_H |
25 | 25 |
|
26 |
#include <lemon/ |
|
26 |
#include <lemon/core.h> |
|
27 | 27 |
|
28 | 28 |
namespace lemon { |
29 | 29 |
|
30 | 30 |
namespace concepts { |
31 | 31 |
|
32 | 32 |
/// \addtogroup concept |
33 | 33 |
/// @{ |
34 | 34 |
|
35 | 35 |
/// \brief The heap concept. |
36 | 36 |
/// |
37 | 37 |
/// Concept class describing the main interface of heaps. |
38 | 38 |
template <typename Priority, typename ItemIntMap> |
39 | 39 |
class Heap { |
40 | 40 |
public: |
41 | 41 |
|
42 | 42 |
/// Type of the items stored in the heap. |
43 | 43 |
typedef typename ItemIntMap::Key Item; |
44 | 44 |
|
45 | 45 |
/// Type of the priorities. |
46 | 46 |
typedef Priority Prio; |
47 | 47 |
|
48 | 48 |
/// \brief Type to represent the states of the items. |
49 | 49 |
/// |
50 | 50 |
/// Each item has a state associated to it. It can be "in heap", |
51 | 51 |
/// "pre heap" or "post heap". The later two are indifferent |
52 | 52 |
/// from the point of view of the heap, but may be useful for |
53 | 53 |
/// the user. |
54 | 54 |
/// |
55 | 55 |
/// The \c ItemIntMap must be initialized in such a way, that it |
56 | 56 |
/// assigns \c PRE_HEAP (<tt>-1</tt>) to every item. |
57 | 57 |
enum State { |
58 | 58 |
IN_HEAP = 0, |
59 | 59 |
PRE_HEAP = -1, |
60 | 60 |
POST_HEAP = -2 |
61 | 61 |
}; |
62 | 62 |
|
63 | 63 |
/// \brief The constructor. |
64 | 64 |
/// |
65 | 65 |
/// The constructor. |
66 | 66 |
/// \param map A map that assigns \c int values to keys of type |
67 | 67 |
/// \c Item. It is used internally by the heap implementations to |
68 | 68 |
/// handle the cross references. The assigned value must be |
69 | 69 |
/// \c PRE_HEAP (<tt>-1</tt>) for every item. |
70 | 70 |
explicit Heap(ItemIntMap &map) {} |
71 | 71 |
|
72 | 72 |
/// \brief The number of items stored in the heap. |
73 | 73 |
/// |
74 | 74 |
/// Returns the number of items stored in the heap. |
75 | 75 |
int size() const { return 0; } |
76 | 76 |
|
77 | 77 |
/// \brief Checks if the heap is empty. |
78 | 78 |
/// |
79 | 79 |
/// Returns \c true if the heap is empty. |
80 | 80 |
bool empty() const { return false; } |
81 | 81 |
|
82 | 82 |
/// \brief Makes the heap empty. |
83 | 83 |
/// |
84 | 84 |
/// Makes the heap empty. |
85 | 85 |
void clear(); |
86 | 86 |
|
87 | 87 |
/// \brief Inserts an item into the heap with the given priority. |
88 | 88 |
/// |
89 | 89 |
/// Inserts the given item into the heap with the given priority. |
90 | 90 |
/// \param i The item to insert. |
91 | 91 |
/// \param p The priority of the item. |
92 | 92 |
void push(const Item &i, const Prio &p) {} |
93 | 93 |
|
94 | 94 |
/// \brief Returns the item having minimum priority. |
95 | 95 |
/// |
96 | 96 |
/// Returns the item having minimum priority. |
97 | 97 |
/// \pre The heap must be non-empty. |
98 | 98 |
Item top() const {} |
99 | 99 |
|
100 | 100 |
/// \brief The minimum priority. |
101 | 101 |
/// |
102 | 102 |
/// Returns the minimum priority. |
103 | 103 |
/// \pre The heap must be non-empty. |
104 | 104 |
Prio prio() const {} |
105 | 105 |
|
106 | 106 |
/// \brief Removes the item having minimum priority. |
107 | 107 |
/// |
108 | 108 |
/// Removes the item having minimum priority. |
109 | 109 |
/// \pre The heap must be non-empty. |
110 | 110 |
void pop() {} |
111 | 111 |
|
112 | 112 |
/// \brief Removes an item from the heap. |
113 | 113 |
/// |
114 | 114 |
/// Removes the given item from the heap if it is already stored. |
115 | 115 |
/// \param i The item to delete. |
116 | 116 |
void erase(const Item &i) {} |
117 | 117 |
|
118 | 118 |
/// \brief The priority of an item. |
119 | 119 |
/// |
120 | 120 |
/// Returns the priority of the given item. |
121 | 121 |
/// \pre \c i must be in the heap. |
122 | 122 |
/// \param i The item. |
123 | 123 |
Prio operator[](const Item &i) const {} |
124 | 124 |
|
125 | 125 |
/// \brief Sets the priority of an item or inserts it, if it is |
126 | 126 |
/// not stored in the heap. |
127 | 127 |
/// |
128 | 128 |
/// This method sets the priority of the given item if it is |
129 | 129 |
/// already stored in the heap. |
130 | 130 |
/// Otherwise it inserts the given item with the given priority. |
131 | 131 |
/// |
132 | 132 |
/// It may throw an \ref UnderflowPriorityException. |
133 | 133 |
/// \param i The item. |
134 | 134 |
/// \param p The priority. |
135 | 135 |
void set(const Item &i, const Prio &p) {} |
136 | 136 |
|
137 | 137 |
/// \brief Decreases the priority of an item to the given value. |
138 | 138 |
/// |
139 | 139 |
/// Decreases the priority of an item to the given value. |
140 | 140 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
141 | 141 |
/// \param i The item. |
142 | 142 |
/// \param p The priority. |
143 | 143 |
void decrease(const Item &i, const Prio &p) {} |
144 | 144 |
|
145 | 145 |
/// \brief Increases the priority of an item to the given value. |
146 | 146 |
/// |
147 | 147 |
/// Increases the priority of an item to the given value. |
148 | 148 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
149 | 149 |
/// \param i The item. |
150 | 150 |
/// \param p The priority. |
151 | 151 |
void increase(const Item &i, const Prio &p) {} |
152 | 152 |
|
153 | 153 |
/// \brief Returns if an item is in, has already been in, or has |
154 | 154 |
/// never been in the heap. |
155 | 155 |
/// |
156 | 156 |
/// This method returns \c PRE_HEAP if the given item has never |
157 | 157 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
158 | 158 |
/// and \c POST_HEAP otherwise. |
159 | 159 |
/// In the latter case it is possible that the item will get back |
160 | 160 |
/// to the heap again. |
161 | 161 |
/// \param i The item. |
162 | 162 |
State state(const Item &i) const {} |
163 | 163 |
|
164 | 164 |
/// \brief Sets the state of an item in the heap. |
165 | 165 |
/// |
166 | 166 |
/// Sets the state of the given item in the heap. It can be used |
167 | 167 |
/// to manually clear the heap when it is important to achive the |
168 | 168 |
/// better time complexity. |
169 | 169 |
/// \param i The item. |
170 | 170 |
/// \param st The state. It should not be \c IN_HEAP. |
171 | 171 |
void state(const Item& i, State st) {} |
172 | 172 |
|
173 | 173 |
|
174 | 174 |
template <typename _Heap> |
175 | 175 |
struct Constraints { |
176 | 176 |
public: |
177 | 177 |
void constraints() { |
178 | 178 |
typedef typename _Heap::Item OwnItem; |
179 | 179 |
typedef typename _Heap::Prio OwnPrio; |
180 | 180 |
typedef typename _Heap::State OwnState; |
181 | 181 |
|
182 | 182 |
Item item; |
183 | 183 |
Prio prio; |
184 | 184 |
item=Item(); |
185 | 185 |
prio=Prio(); |
186 | 186 |
ignore_unused_variable_warning(item); |
187 | 187 |
ignore_unused_variable_warning(prio); |
188 | 188 |
|
189 | 189 |
OwnItem own_item; |
190 | 190 |
OwnPrio own_prio; |
191 | 191 |
OwnState own_state; |
192 | 192 |
own_item=Item(); |
193 | 193 |
own_prio=Prio(); |
194 | 194 |
ignore_unused_variable_warning(own_item); |
195 | 195 |
ignore_unused_variable_warning(own_prio); |
196 | 196 |
ignore_unused_variable_warning(own_state); |
197 | 197 |
|
198 | 198 |
_Heap heap1(map); |
199 | 199 |
_Heap heap2 = heap1; |
200 | 200 |
ignore_unused_variable_warning(heap1); |
201 | 201 |
ignore_unused_variable_warning(heap2); |
202 | 202 |
|
203 | 203 |
int s = heap.size(); |
204 | 204 |
ignore_unused_variable_warning(s); |
205 | 205 |
bool e = heap.empty(); |
206 | 206 |
ignore_unused_variable_warning(e); |
207 | 207 |
|
208 | 208 |
prio = heap.prio(); |
209 | 209 |
item = heap.top(); |
210 | 210 |
prio = heap[item]; |
211 | 211 |
own_prio = heap.prio(); |
212 | 212 |
own_item = heap.top(); |
213 | 213 |
own_prio = heap[own_item]; |
214 | 214 |
|
215 | 215 |
heap.push(item, prio); |
216 | 216 |
heap.push(own_item, own_prio); |
217 | 217 |
heap.pop(); |
218 | 218 |
|
219 | 219 |
heap.set(item, prio); |
220 | 220 |
heap.decrease(item, prio); |
221 | 221 |
heap.increase(item, prio); |
222 | 222 |
heap.set(own_item, own_prio); |
223 | 223 |
heap.decrease(own_item, own_prio); |
224 | 224 |
heap.increase(own_item, own_prio); |
225 | 225 |
|
226 | 226 |
heap.erase(item); |
227 | 227 |
heap.erase(own_item); |
228 | 228 |
heap.clear(); |
229 | 229 |
|
230 | 230 |
own_state = heap.state(own_item); |
231 | 231 |
heap.state(own_item, own_state); |
232 | 232 |
|
233 | 233 |
own_state = _Heap::PRE_HEAP; |
234 | 234 |
own_state = _Heap::IN_HEAP; |
235 | 235 |
own_state = _Heap::POST_HEAP; |
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
_Heap& heap; |
239 | 239 |
ItemIntMap& map; |
240 | 240 |
}; |
241 | 241 |
}; |
242 | 242 |
|
243 | 243 |
/// @} |
244 | 244 |
} // namespace lemon |
245 | 245 |
} |
246 | 246 |
#endif // LEMON_CONCEPT_PATH_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONCEPT_MAPS_H |
20 | 20 |
#define LEMON_CONCEPT_MAPS_H |
21 | 21 |
|
22 |
#include <lemon/ |
|
22 |
#include <lemon/core.h> |
|
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 | 25 |
///\ingroup concept |
26 | 26 |
///\file |
27 | 27 |
///\brief The concept of maps. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup concept |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// Readable map concept |
37 | 37 |
|
38 | 38 |
/// Readable map concept. |
39 | 39 |
/// |
40 | 40 |
template<typename K, typename T> |
41 | 41 |
class ReadMap |
42 | 42 |
{ |
43 | 43 |
public: |
44 | 44 |
/// The key type of the map. |
45 | 45 |
typedef K Key; |
46 | 46 |
/// \brief The value type of the map. |
47 | 47 |
/// (The type of objects associated with the keys). |
48 | 48 |
typedef T Value; |
49 | 49 |
|
50 | 50 |
/// Returns the value associated with the given key. |
51 | 51 |
Value operator[](const Key &) const { |
52 | 52 |
return *static_cast<Value *>(0); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
template<typename _ReadMap> |
56 | 56 |
struct Constraints { |
57 | 57 |
void constraints() { |
58 | 58 |
Value val = m[key]; |
59 | 59 |
val = m[key]; |
60 | 60 |
typename _ReadMap::Value own_val = m[own_key]; |
61 | 61 |
own_val = m[own_key]; |
62 | 62 |
|
63 | 63 |
ignore_unused_variable_warning(key); |
64 | 64 |
ignore_unused_variable_warning(val); |
65 | 65 |
ignore_unused_variable_warning(own_key); |
66 | 66 |
ignore_unused_variable_warning(own_val); |
67 | 67 |
} |
68 | 68 |
const Key& key; |
69 | 69 |
const typename _ReadMap::Key& own_key; |
70 | 70 |
const _ReadMap& m; |
71 | 71 |
}; |
72 | 72 |
|
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
/// Writable map concept |
77 | 77 |
|
78 | 78 |
/// Writable map concept. |
79 | 79 |
/// |
80 | 80 |
template<typename K, typename T> |
81 | 81 |
class WriteMap |
82 | 82 |
{ |
83 | 83 |
public: |
84 | 84 |
/// The key type of the map. |
85 | 85 |
typedef K Key; |
86 | 86 |
/// \brief The value type of the map. |
87 | 87 |
/// (The type of objects associated with the keys). |
88 | 88 |
typedef T Value; |
89 | 89 |
|
90 | 90 |
/// Sets the value associated with the given key. |
91 | 91 |
void set(const Key &, const Value &) {} |
92 | 92 |
|
93 | 93 |
/// Default constructor. |
94 | 94 |
WriteMap() {} |
95 | 95 |
|
96 | 96 |
template <typename _WriteMap> |
97 | 97 |
struct Constraints { |
98 | 98 |
void constraints() { |
99 | 99 |
m.set(key, val); |
100 | 100 |
m.set(own_key, own_val); |
101 | 101 |
|
102 | 102 |
ignore_unused_variable_warning(key); |
103 | 103 |
ignore_unused_variable_warning(val); |
104 | 104 |
ignore_unused_variable_warning(own_key); |
105 | 105 |
ignore_unused_variable_warning(own_val); |
106 | 106 |
} |
107 | 107 |
const Key& key; |
108 | 108 |
const Value& val; |
109 | 109 |
const typename _WriteMap::Key& own_key; |
110 | 110 |
const typename _WriteMap::Value& own_val; |
111 | 111 |
_WriteMap& m; |
112 | 112 |
}; |
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
/// Read/writable map concept |
116 | 116 |
|
117 | 117 |
/// Read/writable map concept. |
118 | 118 |
/// |
119 | 119 |
template<typename K, typename T> |
120 | 120 |
class ReadWriteMap : public ReadMap<K,T>, |
121 | 121 |
public WriteMap<K,T> |
122 | 122 |
{ |
123 | 123 |
public: |
124 | 124 |
/// The key type of the map. |
125 | 125 |
typedef K Key; |
126 | 126 |
/// \brief The value type of the map. |
127 | 127 |
/// (The type of objects associated with the keys). |
128 | 128 |
typedef T Value; |
129 | 129 |
|
130 | 130 |
/// Returns the value associated with the given key. |
131 | 131 |
Value operator[](const Key &) const { |
132 | 132 |
return *static_cast<Value *>(0); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
/// Sets the value associated with the given key. |
136 | 136 |
void set(const Key &, const Value &) {} |
137 | 137 |
|
138 | 138 |
template<typename _ReadWriteMap> |
139 | 139 |
struct Constraints { |
140 | 140 |
void constraints() { |
141 | 141 |
checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
142 | 142 |
checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
143 | 143 |
} |
144 | 144 |
}; |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
|
148 | 148 |
/// Dereferable map concept |
149 | 149 |
|
150 | 150 |
/// Dereferable map concept. |
151 | 151 |
/// |
152 | 152 |
template<typename K, typename T, typename R, typename CR> |
153 | 153 |
class ReferenceMap : public ReadWriteMap<K,T> |
154 | 154 |
{ |
155 | 155 |
public: |
156 | 156 |
/// Tag for reference maps. |
157 | 157 |
typedef True ReferenceMapTag; |
158 | 158 |
/// The key type of the map. |
159 | 159 |
typedef K Key; |
160 | 160 |
/// \brief The value type of the map. |
161 | 161 |
/// (The type of objects associated with the keys). |
162 | 162 |
typedef T Value; |
163 | 163 |
/// The reference type of the map. |
164 | 164 |
typedef R Reference; |
165 | 165 |
/// The const reference type of the map. |
166 | 166 |
typedef CR ConstReference; |
167 | 167 |
|
168 | 168 |
public: |
169 | 169 |
|
170 | 170 |
/// Returns a reference to the value associated with the given key. |
171 | 171 |
Reference operator[](const Key &) { |
172 | 172 |
return *static_cast<Value *>(0); |
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
/// Returns a const reference to the value associated with the given key. |
176 | 176 |
ConstReference operator[](const Key &) const { |
177 | 177 |
return *static_cast<Value *>(0); |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
/// Sets the value associated with the given key. |
181 | 181 |
void set(const Key &k,const Value &t) { operator[](k)=t; } |
182 | 182 |
|
183 | 183 |
template<typename _ReferenceMap> |
184 | 184 |
struct Constraints { |
185 | 185 |
void constraints() { |
186 | 186 |
checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
187 | 187 |
ref = m[key]; |
188 | 188 |
m[key] = val; |
189 | 189 |
m[key] = ref; |
190 | 190 |
m[key] = cref; |
191 | 191 |
own_ref = m[own_key]; |
192 | 192 |
m[own_key] = own_val; |
193 | 193 |
m[own_key] = own_ref; |
194 | 194 |
m[own_key] = own_cref; |
195 | 195 |
m[key] = m[own_key]; |
196 | 196 |
m[own_key] = m[key]; |
197 | 197 |
} |
198 | 198 |
const Key& key; |
199 | 199 |
Value& val; |
200 | 200 |
Reference ref; |
201 | 201 |
ConstReference cref; |
202 | 202 |
const typename _ReferenceMap::Key& own_key; |
203 | 203 |
typename _ReferenceMap::Value& own_val; |
204 | 204 |
typename _ReferenceMap::Reference own_ref; |
205 | 205 |
typename _ReferenceMap::ConstReference own_cref; |
206 | 206 |
_ReferenceMap& m; |
207 | 207 |
}; |
208 | 208 |
}; |
209 | 209 |
|
210 | 210 |
// @} |
211 | 211 |
|
212 | 212 |
} //namespace concepts |
213 | 213 |
|
214 | 214 |
} //namespace lemon |
215 | 215 |
|
216 | 216 |
#endif // LEMON_CONCEPT_MAPS_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 | 23 |
///\todo Iterators have obsolete style |
24 | 24 |
|
25 | 25 |
#ifndef LEMON_CONCEPT_PATH_H |
26 | 26 |
#define LEMON_CONCEPT_PATH_H |
27 | 27 |
|
28 |
#include <lemon/bits/invalid.h> |
|
29 |
#include <lemon/bits/utility.h> |
|
28 |
#include <lemon/core.h> |
|
30 | 29 |
#include <lemon/concept_check.h> |
31 | 30 |
|
32 | 31 |
namespace lemon { |
33 | 32 |
namespace concepts { |
34 | 33 |
|
35 | 34 |
/// \addtogroup concept |
36 | 35 |
/// @{ |
37 | 36 |
|
38 | 37 |
/// \brief A skeleton structure for representing directed paths in |
39 | 38 |
/// a digraph. |
40 | 39 |
/// |
41 | 40 |
/// A skeleton structure for representing directed paths in a |
42 | 41 |
/// digraph. |
43 | 42 |
/// \tparam _Digraph The digraph type in which the path is. |
44 | 43 |
/// |
45 | 44 |
/// In a sense, the path can be treated as a list of arcs. The |
46 | 45 |
/// lemon path type stores just this list. As a consequence it |
47 | 46 |
/// cannot enumerate the nodes in the path and the zero length |
48 | 47 |
/// paths cannot store the source. |
49 | 48 |
/// |
50 | 49 |
template <typename _Digraph> |
51 | 50 |
class Path { |
52 | 51 |
public: |
53 | 52 |
|
54 | 53 |
/// Type of the underlying digraph. |
55 | 54 |
typedef _Digraph Digraph; |
56 | 55 |
/// Arc type of the underlying digraph. |
57 | 56 |
typedef typename Digraph::Arc Arc; |
58 | 57 |
|
59 | 58 |
class ArcIt; |
60 | 59 |
|
61 | 60 |
/// \brief Default constructor |
62 | 61 |
Path() {} |
63 | 62 |
|
64 | 63 |
/// \brief Template constructor |
65 | 64 |
template <typename CPath> |
66 | 65 |
Path(const CPath& cpath) {} |
67 | 66 |
|
68 | 67 |
/// \brief Template assigment |
69 | 68 |
template <typename CPath> |
70 | 69 |
Path& operator=(const CPath& cpath) {} |
71 | 70 |
|
72 | 71 |
/// Length of the path ie. the number of arcs in the path. |
73 | 72 |
int length() const { return 0;} |
74 | 73 |
|
75 | 74 |
/// Returns whether the path is empty. |
76 | 75 |
bool empty() const { return true;} |
77 | 76 |
|
78 | 77 |
/// Resets the path to an empty path. |
79 | 78 |
void clear() {} |
80 | 79 |
|
81 | 80 |
/// \brief Lemon style iterator for path arcs |
82 | 81 |
/// |
83 | 82 |
/// This class is used to iterate on the arcs of the paths. |
84 | 83 |
class ArcIt { |
85 | 84 |
public: |
86 | 85 |
/// Default constructor |
87 | 86 |
ArcIt() {} |
88 | 87 |
/// Invalid constructor |
89 | 88 |
ArcIt(Invalid) {} |
90 | 89 |
/// Constructor for first arc |
91 | 90 |
ArcIt(const Path &) {} |
92 | 91 |
|
93 | 92 |
/// Conversion to Arc |
94 | 93 |
operator Arc() const { return INVALID; } |
95 | 94 |
|
96 | 95 |
/// Next arc |
97 | 96 |
ArcIt& operator++() {return *this;} |
98 | 97 |
|
99 | 98 |
/// Comparison operator |
100 | 99 |
bool operator==(const ArcIt&) const {return true;} |
101 | 100 |
/// Comparison operator |
102 | 101 |
bool operator!=(const ArcIt&) const {return true;} |
103 | 102 |
/// Comparison operator |
104 | 103 |
bool operator<(const ArcIt&) const {return false;} |
105 | 104 |
|
106 | 105 |
}; |
107 | 106 |
|
108 | 107 |
template <typename _Path> |
109 | 108 |
struct Constraints { |
110 | 109 |
void constraints() { |
111 | 110 |
Path<Digraph> pc; |
112 | 111 |
_Path p, pp(pc); |
113 | 112 |
int l = p.length(); |
114 | 113 |
int e = p.empty(); |
115 | 114 |
p.clear(); |
116 | 115 |
|
117 | 116 |
p = pc; |
118 | 117 |
|
119 | 118 |
typename _Path::ArcIt id, ii(INVALID), i(p); |
120 | 119 |
|
121 | 120 |
++i; |
122 | 121 |
typename Digraph::Arc ed = i; |
123 | 122 |
|
124 | 123 |
e = (i == ii); |
125 | 124 |
e = (i != ii); |
126 | 125 |
e = (i < ii); |
127 | 126 |
|
128 | 127 |
ignore_unused_variable_warning(l); |
129 | 128 |
ignore_unused_variable_warning(pp); |
130 | 129 |
ignore_unused_variable_warning(e); |
131 | 130 |
ignore_unused_variable_warning(id); |
132 | 131 |
ignore_unused_variable_warning(ii); |
133 | 132 |
ignore_unused_variable_warning(ed); |
134 | 133 |
} |
135 | 134 |
}; |
136 | 135 |
|
137 | 136 |
}; |
138 | 137 |
|
139 | 138 |
namespace _path_bits { |
140 | 139 |
|
141 | 140 |
template <typename _Digraph, typename _Path, typename RevPathTag = void> |
142 | 141 |
struct PathDumperConstraints { |
143 | 142 |
void constraints() { |
144 | 143 |
int l = p.length(); |
145 | 144 |
int e = p.empty(); |
146 | 145 |
|
147 | 146 |
typename _Path::ArcIt id, i(p); |
148 | 147 |
|
149 | 148 |
++i; |
150 | 149 |
typename _Digraph::Arc ed = i; |
151 | 150 |
|
152 | 151 |
e = (i == INVALID); |
153 | 152 |
e = (i != INVALID); |
154 | 153 |
|
155 | 154 |
ignore_unused_variable_warning(l); |
156 | 155 |
ignore_unused_variable_warning(e); |
157 | 156 |
ignore_unused_variable_warning(id); |
158 | 157 |
ignore_unused_variable_warning(ed); |
159 | 158 |
} |
160 | 159 |
_Path& p; |
161 | 160 |
}; |
162 | 161 |
|
163 | 162 |
template <typename _Digraph, typename _Path> |
164 | 163 |
struct PathDumperConstraints< |
165 | 164 |
_Digraph, _Path, |
166 | 165 |
typename enable_if<typename _Path::RevPathTag, void>::type |
167 | 166 |
> { |
168 | 167 |
void constraints() { |
169 | 168 |
int l = p.length(); |
170 | 169 |
int e = p.empty(); |
171 | 170 |
|
172 | 171 |
typename _Path::RevArcIt id, i(p); |
173 | 172 |
|
174 | 173 |
++i; |
175 | 174 |
typename _Digraph::Arc ed = i; |
176 | 175 |
|
177 | 176 |
e = (i == INVALID); |
178 | 177 |
e = (i != INVALID); |
179 | 178 |
|
180 | 179 |
ignore_unused_variable_warning(l); |
181 | 180 |
ignore_unused_variable_warning(e); |
182 | 181 |
ignore_unused_variable_warning(id); |
183 | 182 |
ignore_unused_variable_warning(ed); |
184 | 183 |
} |
185 | 184 |
_Path& p; |
186 | 185 |
}; |
187 | 186 |
|
188 | 187 |
} |
189 | 188 |
|
190 | 189 |
|
191 | 190 |
/// \brief A skeleton structure for path dumpers. |
192 | 191 |
/// |
193 | 192 |
/// A skeleton structure for path dumpers. The path dumpers are |
194 | 193 |
/// the generalization of the paths. The path dumpers can |
195 | 194 |
/// enumerate the arcs of the path wheter in forward or in |
196 | 195 |
/// backward order. In most time these classes are not used |
197 | 196 |
/// directly rather it used to assign a dumped class to a real |
198 | 197 |
/// path type. |
199 | 198 |
/// |
200 | 199 |
/// The main purpose of this concept is that the shortest path |
201 | 200 |
/// algorithms can enumerate easily the arcs in reverse order. |
202 | 201 |
/// If we would like to give back a real path from these |
203 | 202 |
/// algorithms then we should create a temporarly path object. In |
204 | 203 |
/// Lemon such algorithms gives back a path dumper what can |
205 | 204 |
/// assigned to a real path and the dumpers can be implemented as |
206 | 205 |
/// an adaptor class to the predecessor map. |
207 | 206 |
|
208 | 207 |
/// \tparam _Digraph The digraph type in which the path is. |
209 | 208 |
/// |
210 | 209 |
/// The paths can be constructed from any path type by a |
211 | 210 |
/// template constructor or a template assignment operator. |
212 | 211 |
/// |
213 | 212 |
template <typename _Digraph> |
214 | 213 |
class PathDumper { |
215 | 214 |
public: |
216 | 215 |
|
217 | 216 |
/// Type of the underlying digraph. |
218 | 217 |
typedef _Digraph Digraph; |
219 | 218 |
/// Arc type of the underlying digraph. |
220 | 219 |
typedef typename Digraph::Arc Arc; |
221 | 220 |
|
222 | 221 |
/// Length of the path ie. the number of arcs in the path. |
223 | 222 |
int length() const { return 0;} |
224 | 223 |
|
225 | 224 |
/// Returns whether the path is empty. |
226 | 225 |
bool empty() const { return true;} |
227 | 226 |
|
228 | 227 |
/// \brief Forward or reverse dumping |
229 | 228 |
/// |
230 | 229 |
/// If the RevPathTag is defined and true then reverse dumping |
231 | 230 |
/// is provided in the path dumper. In this case instead of the |
232 | 231 |
/// ArcIt the RevArcIt iterator should be implemented in the |
233 | 232 |
/// dumper. |
234 | 233 |
typedef False RevPathTag; |
235 | 234 |
|
236 | 235 |
/// \brief Lemon style iterator for path arcs |
237 | 236 |
/// |
238 | 237 |
/// This class is used to iterate on the arcs of the paths. |
239 | 238 |
class ArcIt { |
240 | 239 |
public: |
241 | 240 |
/// Default constructor |
242 | 241 |
ArcIt() {} |
243 | 242 |
/// Invalid constructor |
244 | 243 |
ArcIt(Invalid) {} |
245 | 244 |
/// Constructor for first arc |
246 | 245 |
ArcIt(const PathDumper&) {} |
247 | 246 |
|
248 | 247 |
/// Conversion to Arc |
249 | 248 |
operator Arc() const { return INVALID; } |
250 | 249 |
|
251 | 250 |
/// Next arc |
252 | 251 |
ArcIt& operator++() {return *this;} |
253 | 252 |
|
254 | 253 |
/// Comparison operator |
255 | 254 |
bool operator==(const ArcIt&) const {return true;} |
256 | 255 |
/// Comparison operator |
257 | 256 |
bool operator!=(const ArcIt&) const {return true;} |
258 | 257 |
/// Comparison operator |
259 | 258 |
bool operator<(const ArcIt&) const {return false;} |
260 | 259 |
|
261 | 260 |
}; |
262 | 261 |
|
263 | 262 |
/// \brief Lemon style iterator for path arcs |
264 | 263 |
/// |
265 | 264 |
/// This class is used to iterate on the arcs of the paths in |
266 | 265 |
/// reverse direction. |
267 | 266 |
class RevArcIt { |
268 | 267 |
public: |
269 | 268 |
/// Default constructor |
270 | 269 |
RevArcIt() {} |
271 | 270 |
/// Invalid constructor |
272 | 271 |
RevArcIt(Invalid) {} |
273 | 272 |
/// Constructor for first arc |
274 | 273 |
RevArcIt(const PathDumper &) {} |
275 | 274 |
|
276 | 275 |
/// Conversion to Arc |
277 | 276 |
operator Arc() const { return INVALID; } |
278 | 277 |
|
279 | 278 |
/// Next arc |
280 | 279 |
RevArcIt& operator++() {return *this;} |
281 | 280 |
|
282 | 281 |
/// Comparison operator |
283 | 282 |
bool operator==(const RevArcIt&) const {return true;} |
284 | 283 |
/// Comparison operator |
285 | 284 |
bool operator!=(const RevArcIt&) const {return true;} |
286 | 285 |
/// Comparison operator |
287 | 286 |
bool operator<(const RevArcIt&) const {return false;} |
288 | 287 |
|
289 | 288 |
}; |
290 | 289 |
|
291 | 290 |
template <typename _Path> |
292 | 291 |
struct Constraints { |
293 | 292 |
void constraints() { |
294 | 293 |
function_requires<_path_bits:: |
295 | 294 |
PathDumperConstraints<Digraph, _Path> >(); |
296 | 295 |
} |
297 | 296 |
}; |
298 | 297 |
|
299 | 298 |
}; |
300 | 299 |
|
301 | 300 |
|
302 | 301 |
///@} |
303 | 302 |
} |
304 | 303 |
|
305 | 304 |
} // namespace lemon |
306 | 305 |
|
307 | 306 |
#endif // LEMON_CONCEPT_PATH_H |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BITS_INVALID_H |
|
20 |
#define LEMON_BITS_INVALID_H |
|
21 |
|
|
22 |
///\file |
|
23 |
///\brief Definition of INVALID. |
|
24 |
|
|
25 |
namespace lemon { |
|
26 |
|
|
27 |
/// \brief Dummy type to make it easier to create invalid iterators. |
|
28 |
/// |
|
29 |
/// Dummy type to make it easier to create invalid iterators. |
|
30 |
/// See \ref INVALID for the usage. |
|
31 |
struct Invalid { |
|
32 |
public: |
|
33 |
bool operator==(Invalid) { return true; } |
|
34 |
bool operator!=(Invalid) { return false; } |
|
35 |
bool operator< (Invalid) { return false; } |
|
36 |
}; |
|
37 |
|
|
38 |
/// \brief Invalid iterators. |
|
39 |
/// |
|
40 |
/// \ref Invalid is a global type that converts to each iterator |
|
41 |
/// in such a way that the value of the target iterator will be invalid. |
|
42 |
|
|
43 |
//Some people didn't like this: |
|
44 |
//const Invalid &INVALID = *(Invalid *)0; |
|
45 |
|
|
46 |
#ifdef LEMON_ONLY_TEMPLATES |
|
47 |
const Invalid INVALID = Invalid(); |
|
48 |
#else |
|
49 |
extern const Invalid INVALID; |
|
50 |
#endif |
|
51 |
|
|
52 |
} //namespace lemon |
|
53 |
|
|
54 |
#endif |
|
55 |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
// This file contains a modified version of the enable_if library from BOOST. |
|
20 |
// See the appropriate copyright notice below. |
|
21 |
|
|
22 |
// Boost enable_if library |
|
23 |
|
|
24 |
// Copyright 2003 (c) The Trustees of Indiana University. |
|
25 |
|
|
26 |
// Use, modification, and distribution is subject to the Boost Software |
|
27 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|
28 |
// http://www.boost.org/LICENSE_1_0.txt) |
|
29 |
|
|
30 |
// Authors: Jaakko Jarvi (jajarvi at osl.iu.edu) |
|
31 |
// Jeremiah Willcock (jewillco at osl.iu.edu) |
|
32 |
// Andrew Lumsdaine (lums at osl.iu.edu) |
|
33 |
|
|
34 |
|
|
35 |
#ifndef LEMON_BITS_UTILITY_H |
|
36 |
#define LEMON_BITS_UTILITY_H |
|
37 |
|
|
38 |
///\file |
|
39 |
///\brief Miscellaneous basic utilities |
|
40 |
/// |
|
41 |
///\todo Please rethink the organisation of the basic files like this. |
|
42 |
///E.g. this file might be merged with invalid.h. |
|
43 |
|
|
44 |
|
|
45 |
namespace lemon |
|
46 |
{ |
|
47 |
|
|
48 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
49 |
|
|
50 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
51 |
/// |
|
52 |
///\sa False |
|
53 |
/// |
|
54 |
/// \todo This should go to a separate "basic_types.h" (or something) |
|
55 |
/// file. |
|
56 |
struct True { |
|
57 |
///\e |
|
58 |
static const bool value = true; |
|
59 |
}; |
|
60 |
|
|
61 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
62 |
|
|
63 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
64 |
/// |
|
65 |
///\sa True |
|
66 |
struct False { |
|
67 |
///\e |
|
68 |
static const bool value = false; |
|
69 |
}; |
|
70 |
|
|
71 |
|
|
72 |
struct InvalidType { |
|
73 |
}; |
|
74 |
|
|
75 |
template <typename T> |
|
76 |
struct Wrap { |
|
77 |
const T &value; |
|
78 |
Wrap(const T &t) : value(t) {} |
|
79 |
}; |
|
80 |
|
|
81 |
/**************** dummy class to avoid ambiguity ****************/ |
|
82 |
|
|
83 |
template<int T> struct dummy { dummy(int) {} }; |
|
84 |
|
|
85 |
/**************** enable_if from BOOST ****************/ |
|
86 |
|
|
87 |
template <typename Type, typename T = void> |
|
88 |
struct exists { |
|
89 |
typedef T type; |
|
90 |
}; |
|
91 |
|
|
92 |
|
|
93 |
template <bool B, class T = void> |
|
94 |
struct enable_if_c { |
|
95 |
typedef T type; |
|
96 |
}; |
|
97 |
|
|
98 |
template <class T> |
|
99 |
struct enable_if_c<false, T> {}; |
|
100 |
|
|
101 |
template <class Cond, class T = void> |
|
102 |
struct enable_if : public enable_if_c<Cond::value, T> {}; |
|
103 |
|
|
104 |
template <bool B, class T> |
|
105 |
struct lazy_enable_if_c { |
|
106 |
typedef typename T::type type; |
|
107 |
}; |
|
108 |
|
|
109 |
template <class T> |
|
110 |
struct lazy_enable_if_c<false, T> {}; |
|
111 |
|
|
112 |
template <class Cond, class T> |
|
113 |
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {}; |
|
114 |
|
|
115 |
|
|
116 |
template <bool B, class T = void> |
|
117 |
struct disable_if_c { |
|
118 |
typedef T type; |
|
119 |
}; |
|
120 |
|
|
121 |
template <class T> |
|
122 |
struct disable_if_c<true, T> {}; |
|
123 |
|
|
124 |
template <class Cond, class T = void> |
|
125 |
struct disable_if : public disable_if_c<Cond::value, T> {}; |
|
126 |
|
|
127 |
template <bool B, class T> |
|
128 |
struct lazy_disable_if_c { |
|
129 |
typedef typename T::type type; |
|
130 |
}; |
|
131 |
|
|
132 |
template <class T> |
|
133 |
struct lazy_disable_if_c<true, T> {}; |
|
134 |
|
|
135 |
template <class Cond, class T> |
|
136 |
struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {}; |
|
137 |
|
|
138 |
} // namespace lemon |
|
139 |
|
|
140 |
#endif |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)