0
16
0
1
1
5
5
1
1
21
26
5
5
1
1
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 |
/// using appropriate |
|
29 |
/// using appropriate graph maps. |
|
30 | 30 |
/// |
31 | 31 |
/// \include graph_to_eps_demo.cc |
32 | 32 |
|
33 | 33 |
#include<lemon/list_graph.h> |
34 | 34 |
#include<lemon/graph_to_eps.h> |
35 | 35 |
#include<lemon/math.h> |
36 | 36 |
|
37 | 37 |
using namespace std; |
38 | 38 |
using namespace lemon; |
39 | 39 |
|
40 | 40 |
int main() |
41 | 41 |
{ |
42 | 42 |
Palette palette; |
43 | 43 |
Palette paletteW(true); |
44 | 44 |
|
45 | 45 |
// Create a small digraph |
46 | 46 |
ListDigraph g; |
47 | 47 |
typedef ListDigraph::Node Node; |
48 | 48 |
typedef ListDigraph::NodeIt NodeIt; |
49 | 49 |
typedef ListDigraph::Arc Arc; |
50 | 50 |
typedef dim2::Point<int> Point; |
51 | 51 |
|
52 | 52 |
Node n1=g.addNode(); |
53 | 53 |
Node n2=g.addNode(); |
54 | 54 |
Node n3=g.addNode(); |
55 | 55 |
Node n4=g.addNode(); |
56 | 56 |
Node n5=g.addNode(); |
57 | 57 |
|
58 | 58 |
ListDigraph::NodeMap<Point> coords(g); |
59 | 59 |
ListDigraph::NodeMap<double> sizes(g); |
60 | 60 |
ListDigraph::NodeMap<int> colors(g); |
61 | 61 |
ListDigraph::NodeMap<int> shapes(g); |
62 | 62 |
ListDigraph::ArcMap<int> acolors(g); |
63 | 63 |
ListDigraph::ArcMap<int> widths(g); |
64 | 64 |
|
65 | 65 |
coords[n1]=Point(50,50); sizes[n1]=1; colors[n1]=1; shapes[n1]=0; |
66 | 66 |
coords[n2]=Point(50,70); sizes[n2]=2; colors[n2]=2; shapes[n2]=2; |
67 | 67 |
coords[n3]=Point(70,70); sizes[n3]=1; colors[n3]=3; shapes[n3]=0; |
68 | 68 |
coords[n4]=Point(70,50); sizes[n4]=2; colors[n4]=4; shapes[n4]=1; |
69 | 69 |
coords[n5]=Point(85,60); sizes[n5]=3; colors[n5]=5; shapes[n5]=2; |
70 | 70 |
|
71 | 71 |
Arc a; |
72 | 72 |
|
73 | 73 |
a=g.addArc(n1,n2); acolors[a]=0; widths[a]=1; |
74 | 74 |
a=g.addArc(n2,n3); acolors[a]=0; widths[a]=1; |
75 | 75 |
a=g.addArc(n3,n5); acolors[a]=0; widths[a]=3; |
76 | 76 |
a=g.addArc(n5,n4); acolors[a]=0; widths[a]=1; |
77 | 77 |
a=g.addArc(n4,n1); acolors[a]=0; widths[a]=1; |
78 | 78 |
a=g.addArc(n2,n4); acolors[a]=1; widths[a]=2; |
79 | 79 |
a=g.addArc(n3,n4); acolors[a]=2; widths[a]=1; |
80 | 80 |
|
81 | 81 |
IdMap<ListDigraph,Node> id(g); |
82 | 82 |
|
83 | 83 |
// Create .eps files showing the digraph with different options |
84 | 84 |
cout << "Create 'graph_to_eps_demo_out_1_pure.eps'" << endl; |
85 | 85 |
graphToEps(g,"graph_to_eps_demo_out_1_pure.eps"). |
86 | 86 |
coords(coords). |
87 | 87 |
title("Sample .eps figure"). |
88 | 88 |
copyright("(C) 2003-2008 LEMON Project"). |
89 | 89 |
run(); |
90 | 90 |
|
91 | 91 |
cout << "Create 'graph_to_eps_demo_out_2.eps'" << endl; |
92 | 92 |
graphToEps(g,"graph_to_eps_demo_out_2.eps"). |
93 | 93 |
coords(coords). |
94 | 94 |
title("Sample .eps figure"). |
95 | 95 |
copyright("(C) 2003-2008 LEMON Project"). |
96 | 96 |
absoluteNodeSizes().absoluteArcWidths(). |
97 | 97 |
nodeScale(2).nodeSizes(sizes). |
98 | 98 |
nodeShapes(shapes). |
99 | 99 |
nodeColors(composeMap(palette,colors)). |
100 | 100 |
arcColors(composeMap(palette,acolors)). |
101 | 101 |
arcWidthScale(.4).arcWidths(widths). |
102 | 102 |
nodeTexts(id).nodeTextSize(3). |
103 | 103 |
run(); |
104 | 104 |
|
105 | 105 |
cout << "Create 'graph_to_eps_demo_out_3_arr.eps'" << endl; |
106 | 106 |
graphToEps(g,"graph_to_eps_demo_out_3_arr.eps"). |
107 | 107 |
title("Sample .eps figure (with arrowheads)"). |
108 | 108 |
copyright("(C) 2003-2008 LEMON Project"). |
109 | 109 |
absoluteNodeSizes().absoluteArcWidths(). |
110 | 110 |
nodeColors(composeMap(palette,colors)). |
111 | 111 |
coords(coords). |
112 | 112 |
nodeScale(2).nodeSizes(sizes). |
113 | 113 |
nodeShapes(shapes). |
114 | 114 |
arcColors(composeMap(palette,acolors)). |
115 | 115 |
arcWidthScale(.4).arcWidths(widths). |
116 | 116 |
nodeTexts(id).nodeTextSize(3). |
117 | 117 |
drawArrows().arrowWidth(2).arrowLength(2). |
118 | 118 |
run(); |
119 | 119 |
|
120 | 120 |
// Add more arcs to the digraph |
121 | 121 |
a=g.addArc(n1,n4); acolors[a]=2; widths[a]=1; |
122 | 122 |
a=g.addArc(n4,n1); acolors[a]=1; widths[a]=2; |
123 | 123 |
|
124 | 124 |
a=g.addArc(n1,n2); acolors[a]=1; widths[a]=1; |
125 | 125 |
a=g.addArc(n1,n2); acolors[a]=2; widths[a]=1; |
126 | 126 |
a=g.addArc(n1,n2); acolors[a]=3; widths[a]=1; |
127 | 127 |
a=g.addArc(n1,n2); acolors[a]=4; widths[a]=1; |
128 | 128 |
a=g.addArc(n1,n2); acolors[a]=5; widths[a]=1; |
129 | 129 |
a=g.addArc(n1,n2); acolors[a]=6; widths[a]=1; |
130 | 130 |
a=g.addArc(n1,n2); acolors[a]=7; widths[a]=1; |
131 | 131 |
|
132 | 132 |
cout << "Create 'graph_to_eps_demo_out_4_par.eps'" << endl; |
133 | 133 |
graphToEps(g,"graph_to_eps_demo_out_4_par.eps"). |
134 | 134 |
title("Sample .eps figure (parallel arcs)"). |
135 | 135 |
copyright("(C) 2003-2008 LEMON Project"). |
136 | 136 |
absoluteNodeSizes().absoluteArcWidths(). |
137 | 137 |
nodeShapes(shapes). |
138 | 138 |
coords(coords). |
139 | 139 |
nodeScale(2).nodeSizes(sizes). |
140 | 140 |
nodeColors(composeMap(palette,colors)). |
141 | 141 |
arcColors(composeMap(palette,acolors)). |
142 | 142 |
arcWidthScale(.4).arcWidths(widths). |
143 | 143 |
nodeTexts(id).nodeTextSize(3). |
144 | 144 |
enableParallel().parArcDist(1.5). |
145 | 145 |
run(); |
146 | 146 |
|
147 | 147 |
cout << "Create 'graph_to_eps_demo_out_5_par_arr.eps'" << endl; |
148 | 148 |
graphToEps(g,"graph_to_eps_demo_out_5_par_arr.eps"). |
149 | 149 |
title("Sample .eps figure (parallel arcs and arrowheads)"). |
150 | 150 |
copyright("(C) 2003-2008 LEMON Project"). |
151 | 151 |
absoluteNodeSizes().absoluteArcWidths(). |
152 | 152 |
nodeScale(2).nodeSizes(sizes). |
153 | 153 |
coords(coords). |
154 | 154 |
nodeShapes(shapes). |
155 | 155 |
nodeColors(composeMap(palette,colors)). |
156 | 156 |
arcColors(composeMap(palette,acolors)). |
157 | 157 |
arcWidthScale(.3).arcWidths(widths). |
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 |
namespace lemon { |
20 | 20 |
/*! |
21 | 21 |
|
22 | 22 |
|
23 | 23 |
|
24 | 24 |
\page lgf-format LEMON Graph Format (LGF) |
25 | 25 |
|
26 | 26 |
The \e LGF is a <em>column oriented</em> |
27 | 27 |
file format for storing graphs and associated data like |
28 | 28 |
node and edge maps. |
29 | 29 |
|
30 | 30 |
Each line with \c '#' first non-whitespace |
31 | 31 |
character is considered as a comment line. |
32 | 32 |
|
33 | 33 |
Otherwise the file consists of sections starting with |
34 | 34 |
a header line. The header lines starts with an \c '@' character followed by the |
35 | 35 |
type of section. The standard section types are \c \@nodes, \c |
36 | 36 |
\@arcs and \c \@edges |
37 | 37 |
and \@attributes. Each header line may also have an optional |
38 | 38 |
\e name, which can be use to distinguish the sections of the same |
39 | 39 |
type. |
40 | 40 |
|
41 | 41 |
The standard sections are column oriented, each line consists of |
42 | 42 |
<em>token</em>s separated by whitespaces. A token can be \e plain or |
43 | 43 |
\e quoted. A plain token is just a sequence of non-whitespace characters, |
44 | 44 |
while a quoted token is a |
45 | 45 |
character sequence surrounded by double quotes, and it can also |
46 | 46 |
contain whitespaces and escape sequences. |
47 | 47 |
|
48 | 48 |
The \c \@nodes section describes a set of nodes and associated |
49 | 49 |
maps. The first is a header line, its columns are the names of the |
50 | 50 |
maps appearing in the following lines. |
51 | 51 |
One of the maps must be called \c |
52 | 52 |
"label", which plays special role in the file. |
53 | 53 |
The following |
54 | 54 |
non-empty lines until the next section describes nodes of the |
55 | 55 |
graph. Each line contains the values of the node maps |
56 | 56 |
associated to the current node. |
57 | 57 |
|
58 | 58 |
\code |
59 | 59 |
@nodes |
60 | 60 |
label coordinates size title |
61 | 61 |
1 (10,20) 10 "First node" |
62 | 62 |
2 (80,80) 8 "Second node" |
63 | 63 |
3 (40,10) 10 "Third node" |
64 | 64 |
\endcode |
65 | 65 |
|
66 | 66 |
The \c \@arcs section is very similar to the \c \@nodes section, |
67 | 67 |
it again starts with a header line describing the names of the maps, |
68 | 68 |
but the \c "label" map is not obligatory here. The following lines |
69 | 69 |
describe the arcs. The first two tokens of each line are |
70 | 70 |
the source and the target node of the arc, respectively, then come the map |
71 | 71 |
values. The source and target tokens must be node labels. |
72 | 72 |
|
73 | 73 |
\code |
74 | 74 |
@arcs |
75 | 75 |
capacity |
76 | 76 |
1 2 16 |
77 | 77 |
1 3 12 |
78 | 78 |
2 3 18 |
79 | 79 |
\endcode |
80 | 80 |
|
81 |
The \c \@edges is just a synonym of \c \@arcs. The @arcs section can |
|
81 |
The \c \@edges is just a synonym of \c \@arcs. The \@arcs section can |
|
82 | 82 |
also store the edge set of an undirected graph. In such case there is |
83 | 83 |
a conventional method for store arc maps in the file, if two columns |
84 | 84 |
has the same caption with \c '+' and \c '-' prefix, then these columns |
85 | 85 |
can be regarded as the values of an arc map. |
86 | 86 |
|
87 | 87 |
The \c \@attributes section contains key-value pairs, each line |
88 | 88 |
consists of two tokens, an attribute name, and then an attribute |
89 | 89 |
value. The value of the attribute could be also a label value of a |
90 | 90 |
node or an edge, or even an edge label prefixed with \c '+' or \c '-', |
91 | 91 |
which regards to the forward or backward directed arc of the |
92 | 92 |
corresponding edge. |
93 | 93 |
|
94 | 94 |
\code |
95 | 95 |
@attributes |
96 | 96 |
source 1 |
97 | 97 |
target 3 |
98 | 98 |
caption "LEMON test digraph" |
99 | 99 |
\endcode |
100 | 100 |
|
101 | 101 |
The \e LGF can contain extra sections, but there is no restriction on |
102 | 102 |
the format of such sections. |
103 | 103 |
|
104 | 104 |
*/ |
105 | 105 |
} |
106 | 106 |
|
107 | 107 |
// LocalWords: whitespace whitespaces |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H |
20 | 20 |
#define LEMON_BITS_ALTERATION_NOTIFIER_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <list> |
24 | 24 |
|
25 | 25 |
#include <lemon/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 |
/// exception. Actullay, it can be throw only |
|
83 |
/// \ref AlterationObserver::ImmediateDetach ImmediateDetach |
|
82 |
/// exception. Actullay, it can be throw only \ref ImmediateDetach |
|
84 | 83 |
/// exception which detach the observer from the notifier. |
85 | 84 |
/// |
86 | 85 |
/// There are some place when the alteration observing is not completly |
87 | 86 |
/// reliable. If we want to carry out the node degree in the graph |
88 | 87 |
/// as in the \ref InDegMap and we use the reverseEdge that cause |
89 | 88 |
/// unreliable functionality. Because the alteration observing signals |
90 | 89 |
/// only erasing and adding but not the reversing it will stores bad |
91 | 90 |
/// degrees. The sub graph adaptors cannot signal the alterations because |
92 | 91 |
/// just a setting in the filter map can modify the graph and this cannot |
93 | 92 |
/// be watched in any way. |
94 | 93 |
/// |
95 | 94 |
/// \param _Container The container which is observed. |
96 | 95 |
/// \param _Item The item type which is obserbved. |
97 | 96 |
|
98 | 97 |
template <typename _Container, typename _Item> |
99 | 98 |
class AlterationNotifier { |
100 | 99 |
public: |
101 | 100 |
|
102 | 101 |
typedef True Notifier; |
103 | 102 |
|
104 | 103 |
typedef _Container Container; |
105 | 104 |
typedef _Item Item; |
106 | 105 |
|
107 | 106 |
/// \brief Exception which can be called from \e clear() and |
108 | 107 |
/// \e erase(). |
109 | 108 |
/// |
110 | 109 |
/// From the \e clear() and \e erase() function only this |
111 | 110 |
/// exception is allowed to throw. The exception immediatly |
112 | 111 |
/// detaches the current observer from the notifier. Because the |
113 | 112 |
/// \e clear() and \e erase() should not throw other exceptions |
114 | 113 |
/// it can be used to invalidate the observer. |
115 | 114 |
struct ImmediateDetach {}; |
116 | 115 |
|
117 | 116 |
/// \brief ObserverBase is the base class for the observers. |
118 | 117 |
/// |
119 | 118 |
/// ObserverBase is the abstract base class for the observers. |
120 | 119 |
/// It will be notified about an item was inserted into or |
121 | 120 |
/// erased from the graph. |
122 | 121 |
/// |
123 | 122 |
/// The observer interface contains some pure virtual functions |
124 | 123 |
/// to override. The add() and erase() functions are |
125 | 124 |
/// to notify the oberver when one item is added or |
126 | 125 |
/// erased. |
127 | 126 |
/// |
128 | 127 |
/// The build() and clear() members are to notify the observer |
129 | 128 |
/// about the container is built from an empty container or |
130 | 129 |
/// is cleared to an empty container. |
131 | 130 |
|
132 | 131 |
class ObserverBase { |
133 | 132 |
protected: |
134 | 133 |
typedef AlterationNotifier Notifier; |
135 | 134 |
|
136 | 135 |
friend class AlterationNotifier; |
137 | 136 |
|
138 | 137 |
/// \brief Default constructor. |
139 | 138 |
/// |
140 | 139 |
/// Default constructor for ObserverBase. |
141 | 140 |
/// |
142 | 141 |
ObserverBase() : _notifier(0) {} |
143 | 142 |
|
144 | 143 |
/// \brief Constructor which attach the observer into notifier. |
145 | 144 |
/// |
146 | 145 |
/// Constructor which attach the observer into notifier. |
147 | 146 |
ObserverBase(AlterationNotifier& nf) { |
148 | 147 |
attach(nf); |
149 | 148 |
} |
150 | 149 |
|
151 | 150 |
/// \brief Constructor which attach the obserever to the same notifier. |
152 | 151 |
/// |
153 | 152 |
/// Constructor which attach the obserever to the same notifier as |
154 | 153 |
/// the other observer is attached to. |
155 | 154 |
ObserverBase(const ObserverBase& copy) { |
156 | 155 |
if (copy.attached()) { |
157 | 156 |
attach(*copy.notifier()); |
158 | 157 |
} |
159 | 158 |
} |
160 | 159 |
|
161 | 160 |
/// \brief Destructor |
162 | 161 |
virtual ~ObserverBase() { |
163 | 162 |
if (attached()) { |
164 | 163 |
detach(); |
165 | 164 |
} |
166 | 165 |
} |
167 | 166 |
|
168 | 167 |
/// \brief Attaches the observer into an AlterationNotifier. |
169 | 168 |
/// |
170 | 169 |
/// This member attaches the observer into an AlterationNotifier. |
171 | 170 |
/// |
172 | 171 |
void attach(AlterationNotifier& nf) { |
173 | 172 |
nf.attach(*this); |
174 | 173 |
} |
175 | 174 |
|
176 | 175 |
/// \brief Detaches the observer into an AlterationNotifier. |
177 | 176 |
/// |
178 | 177 |
/// This member detaches the observer from an AlterationNotifier. |
179 | 178 |
/// |
180 | 179 |
void detach() { |
181 | 180 |
_notifier->detach(*this); |
182 | 181 |
} |
183 | 182 |
|
184 | 183 |
/// \brief Gives back a pointer to the notifier which the map |
185 | 184 |
/// attached into. |
186 | 185 |
/// |
187 | 186 |
/// This function gives back a pointer to the notifier which the map |
188 | 187 |
/// attached into. |
189 | 188 |
/// |
190 | 189 |
Notifier* notifier() const { return const_cast<Notifier*>(_notifier); } |
191 | 190 |
|
192 | 191 |
/// Gives back true when the observer is attached into a notifier. |
193 | 192 |
bool attached() const { return _notifier != 0; } |
194 | 193 |
|
195 | 194 |
private: |
196 | 195 |
|
197 | 196 |
ObserverBase& operator=(const ObserverBase& copy); |
198 | 197 |
|
199 | 198 |
protected: |
200 | 199 |
|
201 | 200 |
Notifier* _notifier; |
202 | 201 |
typename std::list<ObserverBase*>::iterator _index; |
203 | 202 |
|
204 | 203 |
/// \brief The member function to notificate the observer about an |
205 | 204 |
/// item is added to the container. |
206 | 205 |
/// |
207 | 206 |
/// The add() member function notificates the observer about an item |
208 | 207 |
/// is added to the container. It have to be overrided in the |
209 | 208 |
/// subclasses. |
210 | 209 |
virtual void add(const Item&) = 0; |
211 | 210 |
... | ... |
@@ -24,158 +24,158 @@ |
24 | 24 |
#include <lemon/bits/vector_map.h> |
25 | 25 |
//#include <lemon/bits/debug_map.h> |
26 | 26 |
|
27 | 27 |
///\ingroup graphbits |
28 | 28 |
///\file |
29 | 29 |
///\brief Graph maps that construct and destruct their elements dynamically. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
|
34 | 34 |
//#ifndef LEMON_USE_DEBUG_MAP |
35 | 35 |
|
36 | 36 |
template <typename _Graph, typename _Item, typename _Value> |
37 | 37 |
struct DefaultMapSelector { |
38 | 38 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
39 | 39 |
}; |
40 | 40 |
|
41 | 41 |
// bool |
42 | 42 |
template <typename _Graph, typename _Item> |
43 | 43 |
struct DefaultMapSelector<_Graph, _Item, bool> { |
44 | 44 |
typedef VectorMap<_Graph, _Item, bool> Map; |
45 | 45 |
}; |
46 | 46 |
|
47 | 47 |
// char |
48 | 48 |
template <typename _Graph, typename _Item> |
49 | 49 |
struct DefaultMapSelector<_Graph, _Item, char> { |
50 | 50 |
typedef VectorMap<_Graph, _Item, char> Map; |
51 | 51 |
}; |
52 | 52 |
|
53 | 53 |
template <typename _Graph, typename _Item> |
54 | 54 |
struct DefaultMapSelector<_Graph, _Item, signed char> { |
55 | 55 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
56 | 56 |
}; |
57 | 57 |
|
58 | 58 |
template <typename _Graph, typename _Item> |
59 | 59 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> { |
60 | 60 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
61 | 61 |
}; |
62 | 62 |
|
63 | 63 |
|
64 | 64 |
// int |
65 | 65 |
template <typename _Graph, typename _Item> |
66 | 66 |
struct DefaultMapSelector<_Graph, _Item, signed int> { |
67 | 67 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
68 | 68 |
}; |
69 | 69 |
|
70 | 70 |
template <typename _Graph, typename _Item> |
71 | 71 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> { |
72 | 72 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
// short |
77 | 77 |
template <typename _Graph, typename _Item> |
78 | 78 |
struct DefaultMapSelector<_Graph, _Item, signed short> { |
79 | 79 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
80 | 80 |
}; |
81 | 81 |
|
82 | 82 |
template <typename _Graph, typename _Item> |
83 | 83 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> { |
84 | 84 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
85 | 85 |
}; |
86 | 86 |
|
87 | 87 |
|
88 | 88 |
// long |
89 | 89 |
template <typename _Graph, typename _Item> |
90 | 90 |
struct DefaultMapSelector<_Graph, _Item, signed long> { |
91 | 91 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
92 | 92 |
}; |
93 | 93 |
|
94 | 94 |
template <typename _Graph, typename _Item> |
95 | 95 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> { |
96 | 96 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
97 | 97 |
}; |
98 | 98 |
|
99 | 99 |
|
100 | 100 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
101 | 101 |
|
102 | 102 |
// long long |
103 | 103 |
template <typename _Graph, typename _Item> |
104 | 104 |
struct DefaultMapSelector<_Graph, _Item, signed long long> { |
105 | 105 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename _Graph, typename _Item> |
109 | 109 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> { |
110 | 110 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
#endif |
114 | 114 |
|
115 | 115 |
|
116 | 116 |
// float |
117 | 117 |
template <typename _Graph, typename _Item> |
118 | 118 |
struct DefaultMapSelector<_Graph, _Item, float> { |
119 | 119 |
typedef VectorMap<_Graph, _Item, float> Map; |
120 | 120 |
}; |
121 | 121 |
|
122 | 122 |
|
123 | 123 |
// double |
124 | 124 |
template <typename _Graph, typename _Item> |
125 | 125 |
struct DefaultMapSelector<_Graph, _Item, double> { |
126 | 126 |
typedef VectorMap<_Graph, _Item, double> Map; |
127 | 127 |
}; |
128 | 128 |
|
129 | 129 |
|
130 | 130 |
// long double |
131 | 131 |
template <typename _Graph, typename _Item> |
132 | 132 |
struct DefaultMapSelector<_Graph, _Item, long double> { |
133 | 133 |
typedef VectorMap<_Graph, _Item, long double> Map; |
134 | 134 |
}; |
135 | 135 |
|
136 | 136 |
|
137 | 137 |
// pointer |
138 | 138 |
template <typename _Graph, typename _Item, typename _Ptr> |
139 | 139 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> { |
140 | 140 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
141 | 141 |
}; |
142 | 142 |
|
143 | 143 |
// #else |
144 | 144 |
|
145 | 145 |
// template <typename _Graph, typename _Item, typename _Value> |
146 | 146 |
// struct DefaultMapSelector { |
147 | 147 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
148 | 148 |
// }; |
149 | 149 |
|
150 | 150 |
// #endif |
151 | 151 |
|
152 |
/// |
|
152 |
/// DefaultMap class |
|
153 | 153 |
template <typename _Graph, typename _Item, typename _Value> |
154 | 154 |
class DefaultMap |
155 | 155 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map { |
156 | 156 |
public: |
157 | 157 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
158 | 158 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
159 | 159 |
|
160 | 160 |
typedef typename Parent::Graph Graph; |
161 | 161 |
typedef typename Parent::Value Value; |
162 | 162 |
|
163 | 163 |
explicit DefaultMap(const Graph& graph) : Parent(graph) {} |
164 | 164 |
DefaultMap(const Graph& graph, const Value& value) |
165 | 165 |
: Parent(graph, value) {} |
166 | 166 |
|
167 | 167 |
DefaultMap& operator=(const DefaultMap& cmap) { |
168 | 168 |
return operator=<DefaultMap>(cmap); |
169 | 169 |
} |
170 | 170 |
|
171 | 171 |
template <typename CMap> |
172 | 172 |
DefaultMap& operator=(const CMap& cmap) { |
173 | 173 |
Parent::operator=(cmap); |
174 | 174 |
return *this; |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
}; |
178 | 178 |
|
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
#endif |
1 | 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_COLOR_H |
20 | 20 |
#define LEMON_COLOR_H |
21 | 21 |
|
22 | 22 |
#include<vector> |
23 | 23 |
#include<lemon/math.h> |
24 | 24 |
#include<lemon/maps.h> |
25 | 25 |
|
26 | 26 |
|
27 | 27 |
///\ingroup misc |
28 | 28 |
///\file |
29 | 29 |
///\brief Tools to manage RGB colors. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
|
34 | 34 |
/// \addtogroup misc |
35 | 35 |
/// @{ |
36 | 36 |
|
37 | 37 |
///Data structure representing RGB colors. |
38 | 38 |
|
39 | 39 |
///Data structure representing RGB colors. |
40 | 40 |
class Color |
41 | 41 |
{ |
42 | 42 |
double _r,_g,_b; |
43 | 43 |
public: |
44 | 44 |
///Default constructor |
45 | 45 |
Color() {} |
46 | 46 |
///Constructor |
47 | 47 |
Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; |
48 | 48 |
///Set the red component |
49 | 49 |
double & red() {return _r;} |
50 | 50 |
///Return the red component |
51 | 51 |
const double & red() const {return _r;} |
52 | 52 |
///Set the green component |
53 | 53 |
double & green() {return _g;} |
54 | 54 |
///Return the green component |
55 | 55 |
const double & green() const {return _g;} |
56 | 56 |
///Set the blue component |
57 | 57 |
double & blue() {return _b;} |
58 | 58 |
///Return the blue component |
59 | 59 |
const double & blue() const {return _b;} |
60 | 60 |
///Set the color components |
61 | 61 |
void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; |
62 | 62 |
}; |
63 | 63 |
|
64 | 64 |
/// White color constant |
65 | 65 |
extern const Color WHITE; |
66 | 66 |
/// Black color constant |
67 | 67 |
extern const Color BLACK; |
68 | 68 |
/// Red color constant |
69 | 69 |
extern const Color RED; |
70 | 70 |
/// Green color constant |
71 | 71 |
extern const Color GREEN; |
72 | 72 |
/// Blue color constant |
73 | 73 |
extern const Color BLUE; |
74 | 74 |
/// Yellow color constant |
75 | 75 |
extern const Color YELLOW; |
76 | 76 |
/// Magenta color constant |
77 | 77 |
extern const Color MAGENTA; |
78 | 78 |
/// Cyan color constant |
79 | 79 |
extern const Color CYAN; |
80 | 80 |
/// Grey color constant |
81 | 81 |
extern const Color GREY; |
82 | 82 |
/// Dark red color constant |
83 | 83 |
extern const Color DARK_RED; |
84 | 84 |
/// Dark green color constant |
85 | 85 |
extern const Color DARK_GREEN; |
86 | 86 |
/// Drak blue color constant |
87 | 87 |
extern const Color DARK_BLUE; |
88 | 88 |
/// Dark yellow color constant |
89 | 89 |
extern const Color DARK_YELLOW; |
90 | 90 |
/// Dark magenta color constant |
91 | 91 |
extern const Color DARK_MAGENTA; |
92 | 92 |
/// Dark cyan color constant |
93 | 93 |
extern const Color DARK_CYAN; |
94 | 94 |
|
95 |
///Map <tt>int</tt>s to different |
|
95 |
///Map <tt>int</tt>s to different <tt>Color</tt>s |
|
96 | 96 |
|
97 | 97 |
///This map assigns one of the predefined \ref Color "Color"s to |
98 | 98 |
///each <tt>int</tt>. It is possible to change the colors as well as |
99 | 99 |
///their number. The integer range is cyclically mapped to the |
100 | 100 |
///provided set of colors. |
101 | 101 |
/// |
102 | 102 |
///This is a true \ref concepts::ReferenceMap "reference map", so |
103 | 103 |
///you can also change the actual colors. |
104 | 104 |
|
105 | 105 |
class Palette : public MapBase<int,Color> |
106 | 106 |
{ |
107 | 107 |
std::vector<Color> colors; |
108 | 108 |
public: |
109 | 109 |
///Constructor |
110 | 110 |
|
111 | 111 |
///Constructor. |
112 | 112 |
///\param have_white Indicates whether white is among the |
113 | 113 |
///provided initial colors (\c true) or not (\c false). If it is true, |
114 | 114 |
///white will be assigned to \c 0. |
115 | 115 |
///\param num The number of the allocated colors. If it is \c -1, |
116 | 116 |
///the default color configuration is set up (26 color plus optionaly the |
117 | 117 |
///white). If \c num is less then 26/27 then the default color |
118 | 118 |
///list is cut. Otherwise the color list is filled repeatedly with |
119 | 119 |
///the default color list. (The colors can be changed later on.) |
120 | 120 |
Palette(bool have_white=false,int num=-1) |
121 | 121 |
{ |
122 | 122 |
if (num==0) return; |
123 | 123 |
do { |
124 | 124 |
if(have_white) colors.push_back(Color(1,1,1)); |
125 | 125 |
|
126 | 126 |
colors.push_back(Color(0,0,0)); |
127 | 127 |
colors.push_back(Color(1,0,0)); |
128 | 128 |
colors.push_back(Color(0,1,0)); |
129 | 129 |
colors.push_back(Color(0,0,1)); |
130 | 130 |
colors.push_back(Color(1,1,0)); |
131 | 131 |
colors.push_back(Color(1,0,1)); |
132 | 132 |
colors.push_back(Color(0,1,1)); |
133 | 133 |
|
134 | 134 |
colors.push_back(Color(.5,0,0)); |
135 | 135 |
colors.push_back(Color(0,.5,0)); |
136 | 136 |
colors.push_back(Color(0,0,.5)); |
137 | 137 |
colors.push_back(Color(.5,.5,0)); |
138 | 138 |
colors.push_back(Color(.5,0,.5)); |
139 | 139 |
colors.push_back(Color(0,.5,.5)); |
140 | 140 |
|
141 | 141 |
colors.push_back(Color(.5,.5,.5)); |
142 | 142 |
colors.push_back(Color(1,.5,.5)); |
143 | 143 |
colors.push_back(Color(.5,1,.5)); |
144 | 144 |
colors.push_back(Color(.5,.5,1)); |
145 | 145 |
colors.push_back(Color(1,1,.5)); |
146 | 146 |
colors.push_back(Color(1,.5,1)); |
147 | 147 |
colors.push_back(Color(.5,1,1)); |
148 | 148 |
|
149 | 149 |
colors.push_back(Color(1,.5,0)); |
150 | 150 |
colors.push_back(Color(.5,1,0)); |
151 | 151 |
colors.push_back(Color(1,0,.5)); |
152 | 152 |
colors.push_back(Color(0,1,.5)); |
153 | 153 |
colors.push_back(Color(0,.5,1)); |
154 | 154 |
colors.push_back(Color(.5,0,1)); |
155 | 155 |
} while(int(colors.size())<num); |
156 | 156 |
if(num>=0) colors.resize(num); |
157 | 157 |
} |
158 | 158 |
///\e |
159 | 159 |
Color &operator[](int i) |
160 | 160 |
{ |
161 | 161 |
return colors[i%colors.size()]; |
162 | 162 |
} |
163 | 163 |
///\e |
164 | 164 |
const Color &operator[](int i) const |
165 | 165 |
{ |
166 | 166 |
return colors[i%colors.size()]; |
167 | 167 |
} |
168 | 168 |
///\e |
169 | 169 |
void set(int i,const Color &c) |
170 | 170 |
{ |
171 | 171 |
colors[i%colors.size()]=c; |
172 | 172 |
} |
173 | 173 |
///Adds a new color to the end of the color list. |
174 | 174 |
void add(const Color &c) |
175 | 175 |
{ |
176 | 176 |
colors.push_back(c); |
177 | 177 |
} |
178 | 178 |
|
179 | 179 |
///Sets the number of the existing colors. |
180 | 180 |
void resize(int s) { colors.resize(s);} |
181 | 181 |
///Returns the number of the existing colors. |
182 | 182 |
int size() const { return int(colors.size());} |
183 | 183 |
}; |
184 | 184 |
|
185 | 185 |
///Returns a visibly distinct \ref Color |
186 | 186 |
|
187 | 187 |
///Returns a \ref Color which is as different from the given parameter |
188 | 188 |
///as it is possible. |
189 | 189 |
inline Color distantColor(const Color &c) |
190 | 190 |
{ |
191 | 191 |
return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0); |
192 | 192 |
} |
193 | 193 |
///Returns black for light colors and white for the dark ones. |
194 | 194 |
|
195 | 195 |
///Returns black for light colors and white for the dark ones. |
196 | 196 |
inline Color distantBW(const Color &c){ |
197 | 197 |
return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK; |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
/// @} |
201 | 201 |
|
202 | 202 |
} //END OF NAMESPACE LEMON |
203 | 203 |
|
204 | 204 |
#endif // LEMON_COLOR_H |
... | ... |
@@ -857,257 +857,257 @@ |
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 |
/// (NodeMap, ArcMap), that is |
|
985 |
/// (NodeMap, ArcMap), that is maps that 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 |
|
1009 | 1009 |
private: |
1010 | 1010 |
/// \brief Copy constructor. |
1011 | 1011 |
/// |
1012 | 1012 |
/// Copy Constructor. |
1013 | 1013 |
GraphMap(const GraphMap&) : Parent() {} |
1014 | 1014 |
|
1015 | 1015 |
/// \brief Assign operator. |
1016 | 1016 |
/// |
1017 | 1017 |
/// Assign operator. It does not mofify the underlying graph, |
1018 | 1018 |
/// it just iterates on the current item set and set the map |
1019 | 1019 |
/// with the value returned by the assigned map. |
1020 | 1020 |
template <typename CMap> |
1021 | 1021 |
GraphMap& operator=(const CMap&) { |
1022 | 1022 |
checkConcept<ReadMap<Key, Value>, CMap>(); |
1023 | 1023 |
return *this; |
1024 | 1024 |
} |
1025 | 1025 |
|
1026 | 1026 |
public: |
1027 | 1027 |
template<typename _Map> |
1028 | 1028 |
struct Constraints { |
1029 | 1029 |
void constraints() { |
1030 | 1030 |
checkConcept<ReadWriteMap<Key, Value>, _Map >(); |
1031 | 1031 |
// Construction with a graph parameter |
1032 | 1032 |
_Map a(g); |
1033 | 1033 |
// Constructor with a graph and a default value parameter |
1034 | 1034 |
_Map a2(g,t); |
1035 | 1035 |
// Copy constructor. |
1036 | 1036 |
// _Map b(c); |
1037 | 1037 |
|
1038 | 1038 |
// ReadMap<Key, Value> cmap; |
1039 | 1039 |
// b = cmap; |
1040 | 1040 |
|
1041 | 1041 |
ignore_unused_variable_warning(a); |
1042 | 1042 |
ignore_unused_variable_warning(a2); |
1043 | 1043 |
// ignore_unused_variable_warning(b); |
1044 | 1044 |
} |
1045 | 1045 |
|
1046 | 1046 |
const _Map &c; |
1047 | 1047 |
const Graph &g; |
1048 | 1048 |
const typename GraphMap::Value &t; |
1049 | 1049 |
}; |
1050 | 1050 |
|
1051 | 1051 |
}; |
1052 | 1052 |
|
1053 | 1053 |
/// \brief An empty mappable digraph class. |
1054 | 1054 |
/// |
1055 | 1055 |
/// This class provides beside the core digraph features |
1056 | 1056 |
/// map interface for the digraph structure. |
1057 | 1057 |
/// This concept is part of the Digraph concept. |
1058 | 1058 |
template <typename _Base = BaseDigraphComponent> |
1059 | 1059 |
class MappableDigraphComponent : public _Base { |
1060 | 1060 |
public: |
1061 | 1061 |
|
1062 | 1062 |
typedef _Base Base; |
1063 | 1063 |
typedef typename Base::Node Node; |
1064 | 1064 |
typedef typename Base::Arc Arc; |
1065 | 1065 |
|
1066 | 1066 |
typedef MappableDigraphComponent Digraph; |
1067 | 1067 |
|
1068 | 1068 |
/// \brief ReadWrite map of the nodes. |
1069 | 1069 |
/// |
1070 | 1070 |
/// ReadWrite map of the nodes. |
1071 | 1071 |
/// |
1072 | 1072 |
template <typename _Value> |
1073 | 1073 |
class NodeMap : public GraphMap<Digraph, Node, _Value> { |
1074 | 1074 |
public: |
1075 | 1075 |
typedef GraphMap<MappableDigraphComponent, Node, _Value> Parent; |
1076 | 1076 |
|
1077 | 1077 |
/// \brief Construct a new map. |
1078 | 1078 |
/// |
1079 | 1079 |
/// Construct a new map for the digraph. |
1080 | 1080 |
explicit NodeMap(const MappableDigraphComponent& digraph) |
1081 | 1081 |
: Parent(digraph) {} |
1082 | 1082 |
|
1083 | 1083 |
/// \brief Construct a new map with default value. |
1084 | 1084 |
/// |
1085 | 1085 |
/// Construct a new map for the digraph and initalise the values. |
1086 | 1086 |
NodeMap(const MappableDigraphComponent& digraph, const _Value& value) |
1087 | 1087 |
: Parent(digraph, value) {} |
1088 | 1088 |
|
1089 | 1089 |
private: |
1090 | 1090 |
/// \brief Copy constructor. |
1091 | 1091 |
/// |
1092 | 1092 |
/// Copy Constructor. |
1093 | 1093 |
NodeMap(const NodeMap& nm) : Parent(nm) {} |
1094 | 1094 |
|
1095 | 1095 |
/// \brief Assign operator. |
1096 | 1096 |
/// |
1097 | 1097 |
/// Assign operator. |
1098 | 1098 |
template <typename CMap> |
1099 | 1099 |
NodeMap& operator=(const CMap&) { |
1100 | 1100 |
checkConcept<ReadMap<Node, _Value>, CMap>(); |
1101 | 1101 |
return *this; |
1102 | 1102 |
} |
1103 | 1103 |
|
1104 | 1104 |
}; |
1105 | 1105 |
|
1106 | 1106 |
/// \brief ReadWrite map of the arcs. |
1107 | 1107 |
/// |
1108 | 1108 |
/// ReadWrite map of the arcs. |
1109 | 1109 |
/// |
1110 | 1110 |
template <typename _Value> |
1111 | 1111 |
class ArcMap : public GraphMap<Digraph, Arc, _Value> { |
1112 | 1112 |
public: |
1113 | 1113 |
typedef GraphMap<MappableDigraphComponent, Arc, _Value> Parent; |
... | ... |
@@ -1429,416 +1429,416 @@ |
1429 | 1429 |
if (a < m) { |
1430 | 1430 |
Arc left = refreshRec(v,a,m-1); |
1431 | 1431 |
_left.set(me, left); |
1432 | 1432 |
_parent.set(left, me); |
1433 | 1433 |
} else { |
1434 | 1434 |
_left.set(me, INVALID); |
1435 | 1435 |
} |
1436 | 1436 |
if (m < b) { |
1437 | 1437 |
Arc right = refreshRec(v,m+1,b); |
1438 | 1438 |
_right.set(me, right); |
1439 | 1439 |
_parent.set(right, me); |
1440 | 1440 |
} else { |
1441 | 1441 |
_right.set(me, INVALID); |
1442 | 1442 |
} |
1443 | 1443 |
return me; |
1444 | 1444 |
} |
1445 | 1445 |
|
1446 | 1446 |
void refresh() { |
1447 | 1447 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1448 | 1448 |
std::vector<Arc> v; |
1449 | 1449 |
for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a); |
1450 | 1450 |
if (!v.empty()) { |
1451 | 1451 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
1452 | 1452 |
Arc head = refreshRec(v,0,v.size()-1); |
1453 | 1453 |
_head.set(n, head); |
1454 | 1454 |
_parent.set(head, INVALID); |
1455 | 1455 |
} |
1456 | 1456 |
else _head.set(n, INVALID); |
1457 | 1457 |
} |
1458 | 1458 |
} |
1459 | 1459 |
|
1460 | 1460 |
void zig(Arc v) { |
1461 | 1461 |
Arc w = _parent[v]; |
1462 | 1462 |
_parent.set(v, _parent[w]); |
1463 | 1463 |
_parent.set(w, v); |
1464 | 1464 |
_left.set(w, _right[v]); |
1465 | 1465 |
_right.set(v, w); |
1466 | 1466 |
if (_parent[v] != INVALID) { |
1467 | 1467 |
if (_right[_parent[v]] == w) { |
1468 | 1468 |
_right.set(_parent[v], v); |
1469 | 1469 |
} else { |
1470 | 1470 |
_left.set(_parent[v], v); |
1471 | 1471 |
} |
1472 | 1472 |
} |
1473 | 1473 |
if (_left[w] != INVALID){ |
1474 | 1474 |
_parent.set(_left[w], w); |
1475 | 1475 |
} |
1476 | 1476 |
} |
1477 | 1477 |
|
1478 | 1478 |
void zag(Arc v) { |
1479 | 1479 |
Arc w = _parent[v]; |
1480 | 1480 |
_parent.set(v, _parent[w]); |
1481 | 1481 |
_parent.set(w, v); |
1482 | 1482 |
_right.set(w, _left[v]); |
1483 | 1483 |
_left.set(v, w); |
1484 | 1484 |
if (_parent[v] != INVALID){ |
1485 | 1485 |
if (_left[_parent[v]] == w) { |
1486 | 1486 |
_left.set(_parent[v], v); |
1487 | 1487 |
} else { |
1488 | 1488 |
_right.set(_parent[v], v); |
1489 | 1489 |
} |
1490 | 1490 |
} |
1491 | 1491 |
if (_right[w] != INVALID){ |
1492 | 1492 |
_parent.set(_right[w], w); |
1493 | 1493 |
} |
1494 | 1494 |
} |
1495 | 1495 |
|
1496 | 1496 |
void splay(Arc v) { |
1497 | 1497 |
while (_parent[v] != INVALID) { |
1498 | 1498 |
if (v == _left[_parent[v]]) { |
1499 | 1499 |
if (_parent[_parent[v]] == INVALID) { |
1500 | 1500 |
zig(v); |
1501 | 1501 |
} else { |
1502 | 1502 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1503 | 1503 |
zig(_parent[v]); |
1504 | 1504 |
zig(v); |
1505 | 1505 |
} else { |
1506 | 1506 |
zig(v); |
1507 | 1507 |
zag(v); |
1508 | 1508 |
} |
1509 | 1509 |
} |
1510 | 1510 |
} else { |
1511 | 1511 |
if (_parent[_parent[v]] == INVALID) { |
1512 | 1512 |
zag(v); |
1513 | 1513 |
} else { |
1514 | 1514 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1515 | 1515 |
zag(v); |
1516 | 1516 |
zig(v); |
1517 | 1517 |
} else { |
1518 | 1518 |
zag(_parent[v]); |
1519 | 1519 |
zag(v); |
1520 | 1520 |
} |
1521 | 1521 |
} |
1522 | 1522 |
} |
1523 | 1523 |
} |
1524 | 1524 |
_head[_g.source(v)] = v; |
1525 | 1525 |
} |
1526 | 1526 |
|
1527 | 1527 |
|
1528 | 1528 |
public: |
1529 | 1529 |
|
1530 | 1530 |
///Find an arc between two nodes. |
1531 | 1531 |
|
1532 | 1532 |
///Find an arc between two nodes. |
1533 | 1533 |
///\param s The source node. |
1534 | 1534 |
///\param t The target node. |
1535 | 1535 |
///\param p The previous arc between \c s and \c t. It it is INVALID or |
1536 | 1536 |
///not given, the operator finds the first appropriate arc. |
1537 | 1537 |
///\return An arc from \c s to \c t after \c p or |
1538 | 1538 |
///\ref INVALID if there is no more. |
1539 | 1539 |
/// |
1540 | 1540 |
///For example, you can count the number of arcs from \c u to \c v in the |
1541 | 1541 |
///following way. |
1542 | 1542 |
///\code |
1543 | 1543 |
///DynArcLookUp<ListDigraph> ae(g); |
1544 | 1544 |
///... |
1545 | 1545 |
///int n = 0; |
1546 | 1546 |
///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++; |
1547 | 1547 |
///\endcode |
1548 | 1548 |
/// |
1549 | 1549 |
///Finding the arcs take at most <em>O</em>(log<em>d</em>) |
1550 | 1550 |
///amortized time, specifically, the time complexity of the lookups |
1551 | 1551 |
///is equal to the optimal search tree implementation for the |
1552 | 1552 |
///current query distribution in a constant factor. |
1553 | 1553 |
/// |
1554 | 1554 |
///\note This is a dynamic data structure, therefore the data |
1555 | 1555 |
///structure is updated after each graph alteration. Thus although |
1556 | 1556 |
///this data structure is theoretically faster than \ref ArcLookUp |
1557 |
///and \ref |
|
1557 |
///and \ref AllArcLookUp, it often provides worse performance than |
|
1558 | 1558 |
///them. |
1559 | 1559 |
Arc operator()(Node s, Node t, Arc p = INVALID) const { |
1560 | 1560 |
if (p == INVALID) { |
1561 | 1561 |
Arc a = _head[s]; |
1562 | 1562 |
if (a == INVALID) return INVALID; |
1563 | 1563 |
Arc r = INVALID; |
1564 | 1564 |
while (true) { |
1565 | 1565 |
if (_g.target(a) < t) { |
1566 | 1566 |
if (_right[a] == INVALID) { |
1567 | 1567 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1568 | 1568 |
return r; |
1569 | 1569 |
} else { |
1570 | 1570 |
a = _right[a]; |
1571 | 1571 |
} |
1572 | 1572 |
} else { |
1573 | 1573 |
if (_g.target(a) == t) { |
1574 | 1574 |
r = a; |
1575 | 1575 |
} |
1576 | 1576 |
if (_left[a] == INVALID) { |
1577 | 1577 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1578 | 1578 |
return r; |
1579 | 1579 |
} else { |
1580 | 1580 |
a = _left[a]; |
1581 | 1581 |
} |
1582 | 1582 |
} |
1583 | 1583 |
} |
1584 | 1584 |
} else { |
1585 | 1585 |
Arc a = p; |
1586 | 1586 |
if (_right[a] != INVALID) { |
1587 | 1587 |
a = _right[a]; |
1588 | 1588 |
while (_left[a] != INVALID) { |
1589 | 1589 |
a = _left[a]; |
1590 | 1590 |
} |
1591 | 1591 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1592 | 1592 |
} else { |
1593 | 1593 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
1594 | 1594 |
a = _parent[a]; |
1595 | 1595 |
} |
1596 | 1596 |
if (_parent[a] == INVALID) { |
1597 | 1597 |
return INVALID; |
1598 | 1598 |
} else { |
1599 | 1599 |
a = _parent[a]; |
1600 | 1600 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1601 | 1601 |
} |
1602 | 1602 |
} |
1603 | 1603 |
if (_g.target(a) == t) return a; |
1604 | 1604 |
else return INVALID; |
1605 | 1605 |
} |
1606 | 1606 |
} |
1607 | 1607 |
|
1608 | 1608 |
}; |
1609 | 1609 |
|
1610 | 1610 |
///Fast arc look-up between given endpoints. |
1611 | 1611 |
|
1612 | 1612 |
///Using this class, you can find an arc in a digraph from a given |
1613 | 1613 |
///source to a given target in time <em>O</em>(log<em>d</em>), |
1614 | 1614 |
///where <em>d</em> is the out-degree of the source node. |
1615 | 1615 |
/// |
1616 | 1616 |
///It is not possible to find \e all parallel arcs between two nodes. |
1617 | 1617 |
///Use \ref AllArcLookUp for this purpose. |
1618 | 1618 |
/// |
1619 | 1619 |
///\warning This class is static, so you should call refresh() (or at |
1620 | 1620 |
///least refresh(Node)) to refresh this data structure whenever the |
1621 | 1621 |
///digraph changes. This is a time consuming (superlinearly proportional |
1622 | 1622 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
1623 | 1623 |
/// |
1624 | 1624 |
///\tparam G The type of the underlying digraph. |
1625 | 1625 |
/// |
1626 | 1626 |
///\sa DynArcLookUp |
1627 | 1627 |
///\sa AllArcLookUp |
1628 | 1628 |
template<class G> |
1629 | 1629 |
class ArcLookUp |
1630 | 1630 |
{ |
1631 | 1631 |
public: |
1632 | 1632 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1633 | 1633 |
typedef G Digraph; |
1634 | 1634 |
|
1635 | 1635 |
protected: |
1636 | 1636 |
const Digraph &_g; |
1637 | 1637 |
typename Digraph::template NodeMap<Arc> _head; |
1638 | 1638 |
typename Digraph::template ArcMap<Arc> _left; |
1639 | 1639 |
typename Digraph::template ArcMap<Arc> _right; |
1640 | 1640 |
|
1641 | 1641 |
class ArcLess { |
1642 | 1642 |
const Digraph &g; |
1643 | 1643 |
public: |
1644 | 1644 |
ArcLess(const Digraph &_g) : g(_g) {} |
1645 | 1645 |
bool operator()(Arc a,Arc b) const |
1646 | 1646 |
{ |
1647 | 1647 |
return g.target(a)<g.target(b); |
1648 | 1648 |
} |
1649 | 1649 |
}; |
1650 | 1650 |
|
1651 | 1651 |
public: |
1652 | 1652 |
|
1653 | 1653 |
///Constructor |
1654 | 1654 |
|
1655 | 1655 |
///Constructor. |
1656 | 1656 |
/// |
1657 | 1657 |
///It builds up the search database, which remains valid until the digraph |
1658 | 1658 |
///changes. |
1659 | 1659 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
1660 | 1660 |
|
1661 | 1661 |
private: |
1662 | 1662 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
1663 | 1663 |
{ |
1664 | 1664 |
int m=(a+b)/2; |
1665 | 1665 |
Arc me=v[m]; |
1666 | 1666 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
1667 | 1667 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
1668 | 1668 |
return me; |
1669 | 1669 |
} |
1670 | 1670 |
public: |
1671 | 1671 |
///Refresh the search data structure at a node. |
1672 | 1672 |
|
1673 | 1673 |
///Build up the search database of node \c n. |
1674 | 1674 |
/// |
1675 | 1675 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> |
1676 | 1676 |
///is the number of the outgoing arcs of \c n. |
1677 | 1677 |
void refresh(Node n) |
1678 | 1678 |
{ |
1679 | 1679 |
std::vector<Arc> v; |
1680 | 1680 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
1681 | 1681 |
if(v.size()) { |
1682 | 1682 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
1683 | 1683 |
_head[n]=refreshRec(v,0,v.size()-1); |
1684 | 1684 |
} |
1685 | 1685 |
else _head[n]=INVALID; |
1686 | 1686 |
} |
1687 | 1687 |
///Refresh the full data structure. |
1688 | 1688 |
|
1689 | 1689 |
///Build up the full search database. In fact, it simply calls |
1690 | 1690 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1691 | 1691 |
/// |
1692 | 1692 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
1693 | 1693 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
1694 | 1694 |
///out-degree of the digraph. |
1695 | 1695 |
void refresh() |
1696 | 1696 |
{ |
1697 | 1697 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
1698 | 1698 |
} |
1699 | 1699 |
|
1700 | 1700 |
///Find an arc between two nodes. |
1701 | 1701 |
|
1702 |
///Find an arc between two nodes in time <em>O</em>(log<em>d</em>), where |
|
1703 |
///<em>d</em> is the number of outgoing arcs of \c s. |
|
1702 |
///Find an arc between two nodes in time <em>O</em>(log<em>d</em>), |
|
1703 |
///where <em>d</em> is the number of outgoing arcs of \c s. |
|
1704 | 1704 |
///\param s The source node. |
1705 | 1705 |
///\param t The target node. |
1706 | 1706 |
///\return An arc from \c s to \c t if there exists, |
1707 | 1707 |
///\ref INVALID otherwise. |
1708 | 1708 |
/// |
1709 | 1709 |
///\warning If you change the digraph, refresh() must be called before using |
1710 | 1710 |
///this operator. If you change the outgoing arcs of |
1711 | 1711 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
1712 | 1712 |
Arc operator()(Node s, Node t) const |
1713 | 1713 |
{ |
1714 | 1714 |
Arc e; |
1715 | 1715 |
for(e=_head[s]; |
1716 | 1716 |
e!=INVALID&&_g.target(e)!=t; |
1717 | 1717 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
1718 | 1718 |
return e; |
1719 | 1719 |
} |
1720 | 1720 |
|
1721 | 1721 |
}; |
1722 | 1722 |
|
1723 | 1723 |
///Fast look-up of all arcs between given endpoints. |
1724 | 1724 |
|
1725 | 1725 |
///This class is the same as \ref ArcLookUp, with the addition |
1726 | 1726 |
///that it makes it possible to find all parallel arcs between given |
1727 | 1727 |
///endpoints. |
1728 | 1728 |
/// |
1729 | 1729 |
///\warning This class is static, so you should call refresh() (or at |
1730 | 1730 |
///least refresh(Node)) to refresh this data structure whenever the |
1731 | 1731 |
///digraph changes. This is a time consuming (superlinearly proportional |
1732 | 1732 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
1733 | 1733 |
/// |
1734 | 1734 |
///\tparam G The type of the underlying digraph. |
1735 | 1735 |
/// |
1736 | 1736 |
///\sa DynArcLookUp |
1737 | 1737 |
///\sa ArcLookUp |
1738 | 1738 |
template<class G> |
1739 | 1739 |
class AllArcLookUp : public ArcLookUp<G> |
1740 | 1740 |
{ |
1741 | 1741 |
using ArcLookUp<G>::_g; |
1742 | 1742 |
using ArcLookUp<G>::_right; |
1743 | 1743 |
using ArcLookUp<G>::_left; |
1744 | 1744 |
using ArcLookUp<G>::_head; |
1745 | 1745 |
|
1746 | 1746 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1747 | 1747 |
typedef G Digraph; |
1748 | 1748 |
|
1749 | 1749 |
typename Digraph::template ArcMap<Arc> _next; |
1750 | 1750 |
|
1751 | 1751 |
Arc refreshNext(Arc head,Arc next=INVALID) |
1752 | 1752 |
{ |
1753 | 1753 |
if(head==INVALID) return next; |
1754 | 1754 |
else { |
1755 | 1755 |
next=refreshNext(_right[head],next); |
1756 | 1756 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
1757 | 1757 |
? next : INVALID; |
1758 | 1758 |
return refreshNext(_left[head],head); |
1759 | 1759 |
} |
1760 | 1760 |
} |
1761 | 1761 |
|
1762 | 1762 |
void refreshNext() |
1763 | 1763 |
{ |
1764 | 1764 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
1765 | 1765 |
} |
1766 | 1766 |
|
1767 | 1767 |
public: |
1768 | 1768 |
///Constructor |
1769 | 1769 |
|
1770 | 1770 |
///Constructor. |
1771 | 1771 |
/// |
1772 | 1772 |
///It builds up the search database, which remains valid until the digraph |
1773 | 1773 |
///changes. |
1774 | 1774 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();} |
1775 | 1775 |
|
1776 | 1776 |
///Refresh the data structure at a node. |
1777 | 1777 |
|
1778 | 1778 |
///Build up the search database of node \c n. |
1779 | 1779 |
/// |
1780 | 1780 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is |
1781 | 1781 |
///the number of the outgoing arcs of \c n. |
1782 | 1782 |
void refresh(Node n) |
1783 | 1783 |
{ |
1784 | 1784 |
ArcLookUp<G>::refresh(n); |
1785 | 1785 |
refreshNext(_head[n]); |
1786 | 1786 |
} |
1787 | 1787 |
|
1788 | 1788 |
///Refresh the full data structure. |
1789 | 1789 |
|
1790 | 1790 |
///Build up the full search database. In fact, it simply calls |
1791 | 1791 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1792 | 1792 |
/// |
1793 | 1793 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
1794 | 1794 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
1795 | 1795 |
///out-degree of the digraph. |
1796 | 1796 |
void refresh() |
1797 | 1797 |
{ |
1798 | 1798 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
1799 | 1799 |
} |
1800 | 1800 |
|
1801 | 1801 |
///Find an arc between two nodes. |
1802 | 1802 |
|
1803 | 1803 |
///Find an arc between two nodes. |
1804 | 1804 |
///\param s The source node. |
1805 | 1805 |
///\param t The target node. |
1806 | 1806 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
1807 | 1807 |
///not given, the operator finds the first appropriate arc. |
1808 | 1808 |
///\return An arc from \c s to \c t after \c prev or |
1809 | 1809 |
///\ref INVALID if there is no more. |
1810 | 1810 |
/// |
1811 | 1811 |
///For example, you can count the number of arcs from \c u to \c v in the |
1812 | 1812 |
///following way. |
1813 | 1813 |
///\code |
1814 | 1814 |
///AllArcLookUp<ListDigraph> ae(g); |
1815 | 1815 |
///... |
1816 | 1816 |
///int n = 0; |
1817 | 1817 |
///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++; |
1818 | 1818 |
///\endcode |
1819 | 1819 |
/// |
1820 |
///Finding the first arc take <em>O</em>(log<em>d</em>) time, where |
|
1821 |
///<em>d</em> is the number of outgoing arcs of \c s. Then, the |
|
1820 |
///Finding the first arc take <em>O</em>(log<em>d</em>) time, |
|
1821 |
///where <em>d</em> is the number of outgoing arcs of \c s. Then the |
|
1822 | 1822 |
///consecutive arcs are found in constant time. |
1823 | 1823 |
/// |
1824 | 1824 |
///\warning If you change the digraph, refresh() must be called before using |
1825 | 1825 |
///this operator. If you change the outgoing arcs of |
1826 | 1826 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
1827 | 1827 |
/// |
1828 | 1828 |
#ifdef DOXYGEN |
1829 | 1829 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {} |
1830 | 1830 |
#else |
1831 | 1831 |
using ArcLookUp<G>::operator() ; |
1832 | 1832 |
Arc operator()(Node s, Node t, Arc prev) const |
1833 | 1833 |
{ |
1834 | 1834 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
1835 | 1835 |
} |
1836 | 1836 |
#endif |
1837 | 1837 |
|
1838 | 1838 |
}; |
1839 | 1839 |
|
1840 | 1840 |
/// @} |
1841 | 1841 |
|
1842 | 1842 |
} //namespace lemon |
1843 | 1843 |
|
1844 | 1844 |
#endif |
... | ... |
@@ -710,257 +710,257 @@ |
710 | 710 |
|
711 | 711 |
///This function returns the 'previous node' of the %DFS |
712 | 712 |
///tree for the node \c v, i.e. it returns the last but one node |
713 | 713 |
///from a %DFS path from the root to \c v. It is \c INVALID |
714 | 714 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
715 | 715 |
/// |
716 | 716 |
///The %DFS tree used here is equal to the %DFS tree used in |
717 | 717 |
///\ref predArc(). |
718 | 718 |
/// |
719 | 719 |
///\pre Either \ref run() or \ref start() must be called before |
720 | 720 |
///using this function. |
721 | 721 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
722 | 722 |
G->source((*_pred)[v]); } |
723 | 723 |
|
724 | 724 |
///\brief Returns a const reference to the node map that stores the |
725 | 725 |
///distances of the nodes. |
726 | 726 |
/// |
727 | 727 |
///Returns a const reference to the node map that stores the |
728 | 728 |
///distances of the nodes calculated by the algorithm. |
729 | 729 |
/// |
730 | 730 |
///\pre Either \ref run() or \ref init() |
731 | 731 |
///must be called before using this function. |
732 | 732 |
const DistMap &distMap() const { return *_dist;} |
733 | 733 |
|
734 | 734 |
///\brief Returns a const reference to the node map that stores the |
735 | 735 |
///predecessor arcs. |
736 | 736 |
/// |
737 | 737 |
///Returns a const reference to the node map that stores the predecessor |
738 | 738 |
///arcs, which form the DFS tree. |
739 | 739 |
/// |
740 | 740 |
///\pre Either \ref run() or \ref init() |
741 | 741 |
///must be called before using this function. |
742 | 742 |
const PredMap &predMap() const { return *_pred;} |
743 | 743 |
|
744 | 744 |
///Checks if a node is reachable from the root(s). |
745 | 745 |
|
746 | 746 |
///Returns \c true if \c v is reachable from the root(s). |
747 | 747 |
///\pre Either \ref run() or \ref start() |
748 | 748 |
///must be called before using this function. |
749 | 749 |
bool reached(Node v) const { return (*_reached)[v]; } |
750 | 750 |
|
751 | 751 |
///@} |
752 | 752 |
}; |
753 | 753 |
|
754 | 754 |
///Default traits class of dfs() function. |
755 | 755 |
|
756 | 756 |
///Default traits class of dfs() function. |
757 | 757 |
///\tparam GR Digraph type. |
758 | 758 |
template<class GR> |
759 | 759 |
struct DfsWizardDefaultTraits |
760 | 760 |
{ |
761 | 761 |
///The type of the digraph the algorithm runs on. |
762 | 762 |
typedef GR Digraph; |
763 | 763 |
|
764 | 764 |
///\brief The type of the map that stores the predecessor |
765 | 765 |
///arcs of the %DFS paths. |
766 | 766 |
/// |
767 | 767 |
///The type of the map that stores the predecessor |
768 | 768 |
///arcs of the %DFS paths. |
769 | 769 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
770 | 770 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
771 | 771 |
///Instantiates a PredMap. |
772 | 772 |
|
773 | 773 |
///This function instantiates a PredMap. |
774 | 774 |
///\param g is the digraph, to which we would like to define the |
775 | 775 |
///PredMap. |
776 | 776 |
static PredMap *createPredMap(const Digraph &g) |
777 | 777 |
{ |
778 | 778 |
return new PredMap(g); |
779 | 779 |
} |
780 | 780 |
|
781 | 781 |
///The type of the map that indicates which nodes are processed. |
782 | 782 |
|
783 | 783 |
///The type of the map that indicates which nodes are processed. |
784 | 784 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
785 | 785 |
///By default it is a NullMap. |
786 | 786 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
787 | 787 |
///Instantiates a ProcessedMap. |
788 | 788 |
|
789 | 789 |
///This function instantiates a ProcessedMap. |
790 | 790 |
///\param g is the digraph, to which |
791 | 791 |
///we would like to define the ProcessedMap. |
792 | 792 |
#ifdef DOXYGEN |
793 | 793 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
794 | 794 |
#else |
795 | 795 |
static ProcessedMap *createProcessedMap(const Digraph &) |
796 | 796 |
#endif |
797 | 797 |
{ |
798 | 798 |
return new ProcessedMap(); |
799 | 799 |
} |
800 | 800 |
|
801 | 801 |
///The type of the map that indicates which nodes are reached. |
802 | 802 |
|
803 | 803 |
///The type of the map that indicates which nodes are reached. |
804 | 804 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
805 | 805 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
806 | 806 |
///Instantiates a ReachedMap. |
807 | 807 |
|
808 | 808 |
///This function instantiates a ReachedMap. |
809 | 809 |
///\param g is the digraph, to which |
810 | 810 |
///we would like to define the ReachedMap. |
811 | 811 |
static ReachedMap *createReachedMap(const Digraph &g) |
812 | 812 |
{ |
813 | 813 |
return new ReachedMap(g); |
814 | 814 |
} |
815 | 815 |
|
816 | 816 |
///The type of the map that stores the distances of the nodes. |
817 | 817 |
|
818 | 818 |
///The type of the map that stores the distances of the nodes. |
819 | 819 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
820 | 820 |
typedef typename Digraph::template NodeMap<int> DistMap; |
821 | 821 |
///Instantiates a DistMap. |
822 | 822 |
|
823 | 823 |
///This function instantiates a DistMap. |
824 | 824 |
///\param g is the digraph, to which we would like to define |
825 | 825 |
///the DistMap |
826 | 826 |
static DistMap *createDistMap(const Digraph &g) |
827 | 827 |
{ |
828 | 828 |
return new DistMap(g); |
829 | 829 |
} |
830 | 830 |
|
831 | 831 |
///The type of the DFS paths. |
832 | 832 |
|
833 | 833 |
///The type of the DFS paths. |
834 | 834 |
///It must meet the \ref concepts::Path "Path" concept. |
835 | 835 |
typedef lemon::Path<Digraph> Path; |
836 | 836 |
}; |
837 | 837 |
|
838 |
/// Default traits class used by |
|
838 |
/// Default traits class used by DfsWizard |
|
839 | 839 |
|
840 | 840 |
/// To make it easier to use Dfs algorithm |
841 | 841 |
/// we have created a wizard class. |
842 | 842 |
/// This \ref DfsWizard class needs default traits, |
843 | 843 |
/// as well as the \ref Dfs class. |
844 | 844 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
845 | 845 |
/// \ref DfsWizard class. |
846 | 846 |
template<class GR> |
847 | 847 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
848 | 848 |
{ |
849 | 849 |
|
850 | 850 |
typedef DfsWizardDefaultTraits<GR> Base; |
851 | 851 |
protected: |
852 | 852 |
//The type of the nodes in the digraph. |
853 | 853 |
typedef typename Base::Digraph::Node Node; |
854 | 854 |
|
855 | 855 |
//Pointer to the digraph the algorithm runs on. |
856 | 856 |
void *_g; |
857 | 857 |
//Pointer to the map of reached nodes. |
858 | 858 |
void *_reached; |
859 | 859 |
//Pointer to the map of processed nodes. |
860 | 860 |
void *_processed; |
861 | 861 |
//Pointer to the map of predecessors arcs. |
862 | 862 |
void *_pred; |
863 | 863 |
//Pointer to the map of distances. |
864 | 864 |
void *_dist; |
865 | 865 |
//Pointer to the DFS path to the target node. |
866 | 866 |
void *_path; |
867 | 867 |
//Pointer to the distance of the target node. |
868 | 868 |
int *_di; |
869 | 869 |
|
870 | 870 |
public: |
871 | 871 |
/// Constructor. |
872 | 872 |
|
873 | 873 |
/// This constructor does not require parameters, therefore it initiates |
874 | 874 |
/// all of the attributes to \c 0. |
875 | 875 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
876 | 876 |
_dist(0), _path(0), _di(0) {} |
877 | 877 |
|
878 | 878 |
/// Constructor. |
879 | 879 |
|
880 | 880 |
/// This constructor requires one parameter, |
881 | 881 |
/// others are initiated to \c 0. |
882 | 882 |
/// \param g The digraph the algorithm runs on. |
883 | 883 |
DfsWizardBase(const GR &g) : |
884 | 884 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
885 | 885 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
886 | 886 |
|
887 | 887 |
}; |
888 | 888 |
|
889 | 889 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
890 | 890 |
|
891 | 891 |
/// This auxiliary class is created to implement the |
892 | 892 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
893 | 893 |
/// It does not have own \ref run() method, it uses the functions |
894 | 894 |
/// and features of the plain \ref Dfs. |
895 | 895 |
/// |
896 | 896 |
/// This class should only be used through the \ref dfs() function, |
897 | 897 |
/// which makes it easier to use the algorithm. |
898 | 898 |
template<class TR> |
899 | 899 |
class DfsWizard : public TR |
900 | 900 |
{ |
901 | 901 |
typedef TR Base; |
902 | 902 |
|
903 | 903 |
///The type of the digraph the algorithm runs on. |
904 | 904 |
typedef typename TR::Digraph Digraph; |
905 | 905 |
|
906 | 906 |
typedef typename Digraph::Node Node; |
907 | 907 |
typedef typename Digraph::NodeIt NodeIt; |
908 | 908 |
typedef typename Digraph::Arc Arc; |
909 | 909 |
typedef typename Digraph::OutArcIt OutArcIt; |
910 | 910 |
|
911 | 911 |
///\brief The type of the map that stores the predecessor |
912 | 912 |
///arcs of the DFS paths. |
913 | 913 |
typedef typename TR::PredMap PredMap; |
914 | 914 |
///\brief The type of the map that stores the distances of the nodes. |
915 | 915 |
typedef typename TR::DistMap DistMap; |
916 | 916 |
///\brief The type of the map that indicates which nodes are reached. |
917 | 917 |
typedef typename TR::ReachedMap ReachedMap; |
918 | 918 |
///\brief The type of the map that indicates which nodes are processed. |
919 | 919 |
typedef typename TR::ProcessedMap ProcessedMap; |
920 | 920 |
///The type of the DFS paths |
921 | 921 |
typedef typename TR::Path Path; |
922 | 922 |
|
923 | 923 |
public: |
924 | 924 |
|
925 | 925 |
/// Constructor. |
926 | 926 |
DfsWizard() : TR() {} |
927 | 927 |
|
928 | 928 |
/// Constructor that requires parameters. |
929 | 929 |
|
930 | 930 |
/// Constructor that requires parameters. |
931 | 931 |
/// These parameters will be the default values for the traits class. |
932 | 932 |
/// \param g The digraph the algorithm runs on. |
933 | 933 |
DfsWizard(const Digraph &g) : |
934 | 934 |
TR(g) {} |
935 | 935 |
|
936 | 936 |
///Copy constructor |
937 | 937 |
DfsWizard(const TR &b) : TR(b) {} |
938 | 938 |
|
939 | 939 |
~DfsWizard() {} |
940 | 940 |
|
941 | 941 |
///Runs DFS algorithm from the given source node. |
942 | 942 |
|
943 | 943 |
///This method runs DFS algorithm from node \c s |
944 | 944 |
///in order to compute the DFS path to each node. |
945 | 945 |
void run(Node s) |
946 | 946 |
{ |
947 | 947 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
948 | 948 |
if (Base::_pred) |
949 | 949 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
950 | 950 |
if (Base::_dist) |
951 | 951 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
952 | 952 |
if (Base::_reached) |
953 | 953 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
954 | 954 |
if (Base::_processed) |
955 | 955 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
956 | 956 |
if (s!=INVALID) |
957 | 957 |
alg.run(s); |
958 | 958 |
else |
959 | 959 |
alg.run(); |
960 | 960 |
} |
961 | 961 |
|
962 | 962 |
///Finds the DFS path between \c s and \c t. |
963 | 963 |
|
964 | 964 |
///This method runs DFS algorithm from node \c s |
965 | 965 |
///in order to compute the DFS path to node \c t |
966 | 966 |
///(it stops searching when \c t is processed). |
... | ... |
@@ -328,257 +328,257 @@ |
328 | 328 |
}; |
329 | 329 |
///\brief \ref named-templ-param "Named parameter" for setting |
330 | 330 |
///PredMap type. |
331 | 331 |
/// |
332 | 332 |
///\ref named-templ-param "Named parameter" for setting |
333 | 333 |
///PredMap type. |
334 | 334 |
template <class T> |
335 | 335 |
struct SetPredMap |
336 | 336 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
337 | 337 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
338 | 338 |
}; |
339 | 339 |
|
340 | 340 |
template <class T> |
341 | 341 |
struct SetDistMapTraits : public Traits { |
342 | 342 |
typedef T DistMap; |
343 | 343 |
static DistMap *createDistMap(const Digraph &) |
344 | 344 |
{ |
345 | 345 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
346 | 346 |
return 0; // ignore warnings |
347 | 347 |
} |
348 | 348 |
}; |
349 | 349 |
///\brief \ref named-templ-param "Named parameter" for setting |
350 | 350 |
///DistMap type. |
351 | 351 |
/// |
352 | 352 |
///\ref named-templ-param "Named parameter" for setting |
353 | 353 |
///DistMap type. |
354 | 354 |
template <class T> |
355 | 355 |
struct SetDistMap |
356 | 356 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > { |
357 | 357 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
358 | 358 |
}; |
359 | 359 |
|
360 | 360 |
template <class T> |
361 | 361 |
struct SetProcessedMapTraits : public Traits { |
362 | 362 |
typedef T ProcessedMap; |
363 | 363 |
static ProcessedMap *createProcessedMap(const Digraph &) |
364 | 364 |
{ |
365 | 365 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
366 | 366 |
return 0; // ignore warnings |
367 | 367 |
} |
368 | 368 |
}; |
369 | 369 |
///\brief \ref named-templ-param "Named parameter" for setting |
370 | 370 |
///ProcessedMap type. |
371 | 371 |
/// |
372 | 372 |
///\ref named-templ-param "Named parameter" for setting |
373 | 373 |
///ProcessedMap type. |
374 | 374 |
template <class T> |
375 | 375 |
struct SetProcessedMap |
376 | 376 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > { |
377 | 377 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
378 | 378 |
}; |
379 | 379 |
|
380 | 380 |
struct SetStandardProcessedMapTraits : public Traits { |
381 | 381 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
382 | 382 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
383 | 383 |
{ |
384 | 384 |
return new ProcessedMap(g); |
385 | 385 |
} |
386 | 386 |
}; |
387 | 387 |
///\brief \ref named-templ-param "Named parameter" for setting |
388 | 388 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
389 | 389 |
/// |
390 | 390 |
///\ref named-templ-param "Named parameter" for setting |
391 | 391 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
392 | 392 |
///If you don't set it explicitly, it will be automatically allocated. |
393 | 393 |
struct SetStandardProcessedMap |
394 | 394 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { |
395 | 395 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
396 | 396 |
Create; |
397 | 397 |
}; |
398 | 398 |
|
399 | 399 |
template <class H, class CR> |
400 | 400 |
struct SetHeapTraits : public Traits { |
401 | 401 |
typedef CR HeapCrossRef; |
402 | 402 |
typedef H Heap; |
403 | 403 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
404 | 404 |
LEMON_ASSERT(false, "HeapCrossRef is not initialized"); |
405 | 405 |
return 0; // ignore warnings |
406 | 406 |
} |
407 | 407 |
static Heap *createHeap(HeapCrossRef &) |
408 | 408 |
{ |
409 | 409 |
LEMON_ASSERT(false, "Heap is not initialized"); |
410 | 410 |
return 0; // ignore warnings |
411 | 411 |
} |
412 | 412 |
}; |
413 | 413 |
///\brief \ref named-templ-param "Named parameter" for setting |
414 | 414 |
///heap and cross reference type |
415 | 415 |
/// |
416 | 416 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
417 | 417 |
///reference type. |
418 | 418 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
419 | 419 |
struct SetHeap |
420 | 420 |
: public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > { |
421 | 421 |
typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create; |
422 | 422 |
}; |
423 | 423 |
|
424 | 424 |
template <class H, class CR> |
425 | 425 |
struct SetStandardHeapTraits : public Traits { |
426 | 426 |
typedef CR HeapCrossRef; |
427 | 427 |
typedef H Heap; |
428 | 428 |
static HeapCrossRef *createHeapCrossRef(const Digraph &G) { |
429 | 429 |
return new HeapCrossRef(G); |
430 | 430 |
} |
431 | 431 |
static Heap *createHeap(HeapCrossRef &R) |
432 | 432 |
{ |
433 | 433 |
return new Heap(R); |
434 | 434 |
} |
435 | 435 |
}; |
436 | 436 |
///\brief \ref named-templ-param "Named parameter" for setting |
437 | 437 |
///heap and cross reference type with automatic allocation |
438 | 438 |
/// |
439 | 439 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
440 | 440 |
///reference type. It can allocate the heap and the cross reference |
441 | 441 |
///object if the cross reference's constructor waits for the digraph as |
442 | 442 |
///parameter and the heap's constructor waits for the cross reference. |
443 | 443 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
444 | 444 |
struct SetStandardHeap |
445 | 445 |
: public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > { |
446 | 446 |
typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > |
447 | 447 |
Create; |
448 | 448 |
}; |
449 | 449 |
|
450 | 450 |
template <class T> |
451 | 451 |
struct SetOperationTraitsTraits : public Traits { |
452 | 452 |
typedef T OperationTraits; |
453 | 453 |
}; |
454 | 454 |
|
455 | 455 |
/// \brief \ref named-templ-param "Named parameter" for setting |
456 |
///\ |
|
456 |
///\c OperationTraits type |
|
457 | 457 |
/// |
458 | 458 |
///\ref named-templ-param "Named parameter" for setting |
459 | 459 |
///\ref OperationTraits type. |
460 | 460 |
template <class T> |
461 | 461 |
struct SetOperationTraits |
462 | 462 |
: public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
463 | 463 |
typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > |
464 | 464 |
Create; |
465 | 465 |
}; |
466 | 466 |
|
467 | 467 |
///@} |
468 | 468 |
|
469 | 469 |
protected: |
470 | 470 |
|
471 | 471 |
Dijkstra() {} |
472 | 472 |
|
473 | 473 |
public: |
474 | 474 |
|
475 | 475 |
///Constructor. |
476 | 476 |
|
477 | 477 |
///Constructor. |
478 | 478 |
///\param _g The digraph the algorithm runs on. |
479 | 479 |
///\param _length The length map used by the algorithm. |
480 | 480 |
Dijkstra(const Digraph& _g, const LengthMap& _length) : |
481 | 481 |
G(&_g), length(&_length), |
482 | 482 |
_pred(NULL), local_pred(false), |
483 | 483 |
_dist(NULL), local_dist(false), |
484 | 484 |
_processed(NULL), local_processed(false), |
485 | 485 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
486 | 486 |
_heap(NULL), local_heap(false) |
487 | 487 |
{ } |
488 | 488 |
|
489 | 489 |
///Destructor. |
490 | 490 |
~Dijkstra() |
491 | 491 |
{ |
492 | 492 |
if(local_pred) delete _pred; |
493 | 493 |
if(local_dist) delete _dist; |
494 | 494 |
if(local_processed) delete _processed; |
495 | 495 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
496 | 496 |
if(local_heap) delete _heap; |
497 | 497 |
} |
498 | 498 |
|
499 | 499 |
///Sets the length map. |
500 | 500 |
|
501 | 501 |
///Sets the length map. |
502 | 502 |
///\return <tt> (*this) </tt> |
503 | 503 |
Dijkstra &lengthMap(const LengthMap &m) |
504 | 504 |
{ |
505 | 505 |
length = &m; |
506 | 506 |
return *this; |
507 | 507 |
} |
508 | 508 |
|
509 | 509 |
///Sets the map that stores the predecessor arcs. |
510 | 510 |
|
511 | 511 |
///Sets the map that stores the predecessor arcs. |
512 | 512 |
///If you don't use this function before calling \ref run(), |
513 | 513 |
///it will allocate one. The destructor deallocates this |
514 | 514 |
///automatically allocated map, of course. |
515 | 515 |
///\return <tt> (*this) </tt> |
516 | 516 |
Dijkstra &predMap(PredMap &m) |
517 | 517 |
{ |
518 | 518 |
if(local_pred) { |
519 | 519 |
delete _pred; |
520 | 520 |
local_pred=false; |
521 | 521 |
} |
522 | 522 |
_pred = &m; |
523 | 523 |
return *this; |
524 | 524 |
} |
525 | 525 |
|
526 | 526 |
///Sets the map that indicates which nodes are processed. |
527 | 527 |
|
528 | 528 |
///Sets the map that indicates which nodes are processed. |
529 | 529 |
///If you don't use this function before calling \ref run(), |
530 | 530 |
///it will allocate one. The destructor deallocates this |
531 | 531 |
///automatically allocated map, of course. |
532 | 532 |
///\return <tt> (*this) </tt> |
533 | 533 |
Dijkstra &processedMap(ProcessedMap &m) |
534 | 534 |
{ |
535 | 535 |
if(local_processed) { |
536 | 536 |
delete _processed; |
537 | 537 |
local_processed=false; |
538 | 538 |
} |
539 | 539 |
_processed = &m; |
540 | 540 |
return *this; |
541 | 541 |
} |
542 | 542 |
|
543 | 543 |
///Sets the map that stores the distances of the nodes. |
544 | 544 |
|
545 | 545 |
///Sets the map that stores the distances of the nodes calculated by the |
546 | 546 |
///algorithm. |
547 | 547 |
///If you don't use this function before calling \ref run(), |
548 | 548 |
///it will allocate one. The destructor deallocates this |
549 | 549 |
///automatically allocated map, of course. |
550 | 550 |
///\return <tt> (*this) </tt> |
551 | 551 |
Dijkstra &distMap(DistMap &m) |
552 | 552 |
{ |
553 | 553 |
if(local_dist) { |
554 | 554 |
delete _dist; |
555 | 555 |
local_dist=false; |
556 | 556 |
} |
557 | 557 |
_dist = &m; |
558 | 558 |
return *this; |
559 | 559 |
} |
560 | 560 |
|
561 | 561 |
///Sets the heap and the cross reference used by algorithm. |
562 | 562 |
|
563 | 563 |
///Sets the heap and the cross reference used by algorithm. |
564 | 564 |
///If you don't use this function before calling \ref run(), |
565 | 565 |
///it will allocate one. The destructor deallocates this |
566 | 566 |
///automatically allocated heap and cross reference, of course. |
567 | 567 |
///\return <tt> (*this) </tt> |
568 | 568 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
569 | 569 |
{ |
570 | 570 |
if(local_heap_cross_ref) { |
571 | 571 |
delete _heap_cross_ref; |
572 | 572 |
local_heap_cross_ref=false; |
573 | 573 |
} |
574 | 574 |
_heap_cross_ref = &cr; |
575 | 575 |
if(local_heap) { |
576 | 576 |
delete _heap; |
577 | 577 |
local_heap=false; |
578 | 578 |
} |
579 | 579 |
_heap = &hp; |
580 | 580 |
return *this; |
581 | 581 |
} |
582 | 582 |
|
583 | 583 |
private: |
584 | 584 |
|
... | ... |
@@ -913,257 +913,257 @@ |
913 | 913 |
///It may be decreased in the following processes. |
914 | 914 |
///\pre Either \ref run() or \ref init() |
915 | 915 |
///must be called before using this function and |
916 | 916 |
///node \c v must be reached but not necessarily processed. |
917 | 917 |
Value currentDist(Node v) const { |
918 | 918 |
return processed(v) ? (*_dist)[v] : (*_heap)[v]; |
919 | 919 |
} |
920 | 920 |
|
921 | 921 |
///@} |
922 | 922 |
}; |
923 | 923 |
|
924 | 924 |
|
925 | 925 |
///Default traits class of dijkstra() function. |
926 | 926 |
|
927 | 927 |
///Default traits class of dijkstra() function. |
928 | 928 |
///\tparam GR The type of the digraph. |
929 | 929 |
///\tparam LM The type of the length map. |
930 | 930 |
template<class GR, class LM> |
931 | 931 |
struct DijkstraWizardDefaultTraits |
932 | 932 |
{ |
933 | 933 |
///The type of the digraph the algorithm runs on. |
934 | 934 |
typedef GR Digraph; |
935 | 935 |
///The type of the map that stores the arc lengths. |
936 | 936 |
|
937 | 937 |
///The type of the map that stores the arc lengths. |
938 | 938 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
939 | 939 |
typedef LM LengthMap; |
940 | 940 |
///The type of the length of the arcs. |
941 | 941 |
typedef typename LM::Value Value; |
942 | 942 |
|
943 | 943 |
/// Operation traits for Dijkstra algorithm. |
944 | 944 |
|
945 | 945 |
/// This class defines the operations that are used in the algorithm. |
946 | 946 |
/// \see DijkstraDefaultOperationTraits |
947 | 947 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
948 | 948 |
|
949 | 949 |
/// The cross reference type used by the heap. |
950 | 950 |
|
951 | 951 |
/// The cross reference type used by the heap. |
952 | 952 |
/// Usually it is \c Digraph::NodeMap<int>. |
953 | 953 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
954 | 954 |
///Instantiates a \ref HeapCrossRef. |
955 | 955 |
|
956 | 956 |
///This function instantiates a \ref HeapCrossRef. |
957 | 957 |
/// \param g is the digraph, to which we would like to define the |
958 | 958 |
/// HeapCrossRef. |
959 | 959 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
960 | 960 |
{ |
961 | 961 |
return new HeapCrossRef(g); |
962 | 962 |
} |
963 | 963 |
|
964 | 964 |
///The heap type used by the Dijkstra algorithm. |
965 | 965 |
|
966 | 966 |
///The heap type used by the Dijkstra algorithm. |
967 | 967 |
/// |
968 | 968 |
///\sa BinHeap |
969 | 969 |
///\sa Dijkstra |
970 | 970 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
971 | 971 |
std::less<Value> > Heap; |
972 | 972 |
|
973 | 973 |
///Instantiates a \ref Heap. |
974 | 974 |
|
975 | 975 |
///This function instantiates a \ref Heap. |
976 | 976 |
/// \param r is the HeapCrossRef which is used. |
977 | 977 |
static Heap *createHeap(HeapCrossRef& r) |
978 | 978 |
{ |
979 | 979 |
return new Heap(r); |
980 | 980 |
} |
981 | 981 |
|
982 | 982 |
///\brief The type of the map that stores the predecessor |
983 | 983 |
///arcs of the shortest paths. |
984 | 984 |
/// |
985 | 985 |
///The type of the map that stores the predecessor |
986 | 986 |
///arcs of the shortest paths. |
987 | 987 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
988 | 988 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
989 | 989 |
///Instantiates a PredMap. |
990 | 990 |
|
991 | 991 |
///This function instantiates a PredMap. |
992 | 992 |
///\param g is the digraph, to which we would like to define the |
993 | 993 |
///PredMap. |
994 | 994 |
static PredMap *createPredMap(const Digraph &g) |
995 | 995 |
{ |
996 | 996 |
return new PredMap(g); |
997 | 997 |
} |
998 | 998 |
|
999 | 999 |
///The type of the map that indicates which nodes are processed. |
1000 | 1000 |
|
1001 | 1001 |
///The type of the map that indicates which nodes are processed. |
1002 | 1002 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1003 | 1003 |
///By default it is a NullMap. |
1004 | 1004 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
1005 | 1005 |
///Instantiates a ProcessedMap. |
1006 | 1006 |
|
1007 | 1007 |
///This function instantiates a ProcessedMap. |
1008 | 1008 |
///\param g is the digraph, to which |
1009 | 1009 |
///we would like to define the ProcessedMap. |
1010 | 1010 |
#ifdef DOXYGEN |
1011 | 1011 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
1012 | 1012 |
#else |
1013 | 1013 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1014 | 1014 |
#endif |
1015 | 1015 |
{ |
1016 | 1016 |
return new ProcessedMap(); |
1017 | 1017 |
} |
1018 | 1018 |
|
1019 | 1019 |
///The type of the map that stores the distances of the nodes. |
1020 | 1020 |
|
1021 | 1021 |
///The type of the map that stores the distances of the nodes. |
1022 | 1022 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1023 | 1023 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
1024 | 1024 |
///Instantiates a DistMap. |
1025 | 1025 |
|
1026 | 1026 |
///This function instantiates a DistMap. |
1027 | 1027 |
///\param g is the digraph, to which we would like to define |
1028 | 1028 |
///the DistMap |
1029 | 1029 |
static DistMap *createDistMap(const Digraph &g) |
1030 | 1030 |
{ |
1031 | 1031 |
return new DistMap(g); |
1032 | 1032 |
} |
1033 | 1033 |
|
1034 | 1034 |
///The type of the shortest paths. |
1035 | 1035 |
|
1036 | 1036 |
///The type of the shortest paths. |
1037 | 1037 |
///It must meet the \ref concepts::Path "Path" concept. |
1038 | 1038 |
typedef lemon::Path<Digraph> Path; |
1039 | 1039 |
}; |
1040 | 1040 |
|
1041 |
/// Default traits class used by |
|
1041 |
/// Default traits class used by DijkstraWizard |
|
1042 | 1042 |
|
1043 | 1043 |
/// To make it easier to use Dijkstra algorithm |
1044 | 1044 |
/// we have created a wizard class. |
1045 | 1045 |
/// This \ref DijkstraWizard class needs default traits, |
1046 | 1046 |
/// as well as the \ref Dijkstra class. |
1047 | 1047 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
1048 | 1048 |
/// \ref DijkstraWizard class. |
1049 | 1049 |
template<class GR,class LM> |
1050 | 1050 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1051 | 1051 |
{ |
1052 | 1052 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1053 | 1053 |
protected: |
1054 | 1054 |
//The type of the nodes in the digraph. |
1055 | 1055 |
typedef typename Base::Digraph::Node Node; |
1056 | 1056 |
|
1057 | 1057 |
//Pointer to the digraph the algorithm runs on. |
1058 | 1058 |
void *_g; |
1059 | 1059 |
//Pointer to the length map. |
1060 | 1060 |
void *_length; |
1061 | 1061 |
//Pointer to the map of processed nodes. |
1062 | 1062 |
void *_processed; |
1063 | 1063 |
//Pointer to the map of predecessors arcs. |
1064 | 1064 |
void *_pred; |
1065 | 1065 |
//Pointer to the map of distances. |
1066 | 1066 |
void *_dist; |
1067 | 1067 |
//Pointer to the shortest path to the target node. |
1068 | 1068 |
void *_path; |
1069 | 1069 |
//Pointer to the distance of the target node. |
1070 | 1070 |
void *_di; |
1071 | 1071 |
|
1072 | 1072 |
public: |
1073 | 1073 |
/// Constructor. |
1074 | 1074 |
|
1075 | 1075 |
/// This constructor does not require parameters, therefore it initiates |
1076 | 1076 |
/// all of the attributes to \c 0. |
1077 | 1077 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1078 | 1078 |
_dist(0), _path(0), _di(0) {} |
1079 | 1079 |
|
1080 | 1080 |
/// Constructor. |
1081 | 1081 |
|
1082 | 1082 |
/// This constructor requires two parameters, |
1083 | 1083 |
/// others are initiated to \c 0. |
1084 | 1084 |
/// \param g The digraph the algorithm runs on. |
1085 | 1085 |
/// \param l The length map. |
1086 | 1086 |
DijkstraWizardBase(const GR &g,const LM &l) : |
1087 | 1087 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1088 | 1088 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1089 | 1089 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
1090 | 1090 |
|
1091 | 1091 |
}; |
1092 | 1092 |
|
1093 | 1093 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
1094 | 1094 |
|
1095 | 1095 |
/// This auxiliary class is created to implement the |
1096 | 1096 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
1097 | 1097 |
/// It does not have own \ref run() method, it uses the functions |
1098 | 1098 |
/// and features of the plain \ref Dijkstra. |
1099 | 1099 |
/// |
1100 | 1100 |
/// This class should only be used through the \ref dijkstra() function, |
1101 | 1101 |
/// which makes it easier to use the algorithm. |
1102 | 1102 |
template<class TR> |
1103 | 1103 |
class DijkstraWizard : public TR |
1104 | 1104 |
{ |
1105 | 1105 |
typedef TR Base; |
1106 | 1106 |
|
1107 | 1107 |
///The type of the digraph the algorithm runs on. |
1108 | 1108 |
typedef typename TR::Digraph Digraph; |
1109 | 1109 |
|
1110 | 1110 |
typedef typename Digraph::Node Node; |
1111 | 1111 |
typedef typename Digraph::NodeIt NodeIt; |
1112 | 1112 |
typedef typename Digraph::Arc Arc; |
1113 | 1113 |
typedef typename Digraph::OutArcIt OutArcIt; |
1114 | 1114 |
|
1115 | 1115 |
///The type of the map that stores the arc lengths. |
1116 | 1116 |
typedef typename TR::LengthMap LengthMap; |
1117 | 1117 |
///The type of the length of the arcs. |
1118 | 1118 |
typedef typename LengthMap::Value Value; |
1119 | 1119 |
///\brief The type of the map that stores the predecessor |
1120 | 1120 |
///arcs of the shortest paths. |
1121 | 1121 |
typedef typename TR::PredMap PredMap; |
1122 | 1122 |
///The type of the map that stores the distances of the nodes. |
1123 | 1123 |
typedef typename TR::DistMap DistMap; |
1124 | 1124 |
///The type of the map that indicates which nodes are processed. |
1125 | 1125 |
typedef typename TR::ProcessedMap ProcessedMap; |
1126 | 1126 |
///The type of the shortest paths |
1127 | 1127 |
typedef typename TR::Path Path; |
1128 | 1128 |
///The heap type used by the dijkstra algorithm. |
1129 | 1129 |
typedef typename TR::Heap Heap; |
1130 | 1130 |
|
1131 | 1131 |
public: |
1132 | 1132 |
|
1133 | 1133 |
/// Constructor. |
1134 | 1134 |
DijkstraWizard() : TR() {} |
1135 | 1135 |
|
1136 | 1136 |
/// Constructor that requires parameters. |
1137 | 1137 |
|
1138 | 1138 |
/// Constructor that requires parameters. |
1139 | 1139 |
/// These parameters will be the default values for the traits class. |
1140 | 1140 |
/// \param g The digraph the algorithm runs on. |
1141 | 1141 |
/// \param l The length map. |
1142 | 1142 |
DijkstraWizard(const Digraph &g, const LengthMap &l) : |
1143 | 1143 |
TR(g,l) {} |
1144 | 1144 |
|
1145 | 1145 |
///Copy constructor |
1146 | 1146 |
DijkstraWizard(const TR &b) : TR(b) {} |
1147 | 1147 |
|
1148 | 1148 |
~DijkstraWizard() {} |
1149 | 1149 |
|
1150 | 1150 |
///Runs Dijkstra algorithm from the given source node. |
1151 | 1151 |
|
1152 | 1152 |
///This method runs %Dijkstra algorithm from the given source node |
1153 | 1153 |
///in order to compute the shortest path to each node. |
1154 | 1154 |
void run(Node s) |
1155 | 1155 |
{ |
1156 | 1156 |
Dijkstra<Digraph,LengthMap,TR> |
1157 | 1157 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
1158 | 1158 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1159 | 1159 |
if (Base::_pred) |
1160 | 1160 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1161 | 1161 |
if (Base::_dist) |
1162 | 1162 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1163 | 1163 |
if (Base::_processed) |
1164 | 1164 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1165 | 1165 |
dijk.run(s); |
1166 | 1166 |
} |
1167 | 1167 |
|
1168 | 1168 |
///Finds the shortest path between \c s and \c t. |
1169 | 1169 |
... | ... |
@@ -134,260 +134,260 @@ |
134 | 134 |
Point<T> operator-() const { |
135 | 135 |
Point<T> b=*this; |
136 | 136 |
b.x=-b.x; b.y=-b.y; |
137 | 137 |
return b; |
138 | 138 |
} |
139 | 139 |
|
140 | 140 |
///Return the difference of two vectors |
141 | 141 |
Point<T> operator-(const Point<T> &u) const { |
142 | 142 |
Point<T> b=*this; |
143 | 143 |
return b-=u; |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
///Return a vector multiplied by a scalar |
147 | 147 |
Point<T> operator*(const T &u) const { |
148 | 148 |
Point<T> b=*this; |
149 | 149 |
return b*=u; |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
///Return a vector divided by a scalar |
153 | 153 |
Point<T> operator/(const T &u) const { |
154 | 154 |
Point<T> b=*this; |
155 | 155 |
return b/=u; |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
///Test equality |
159 | 159 |
bool operator==(const Point<T> &u) const { |
160 | 160 |
return (x==u.x) && (y==u.y); |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
///Test inequality |
164 | 164 |
bool operator!=(Point u) const { |
165 | 165 |
return (x!=u.x) || (y!=u.y); |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
}; |
169 | 169 |
|
170 | 170 |
///Return a Point |
171 | 171 |
|
172 | 172 |
///Return a Point. |
173 | 173 |
///\relates Point |
174 | 174 |
template <typename T> |
175 | 175 |
inline Point<T> makePoint(const T& x, const T& y) { |
176 | 176 |
return Point<T>(x, y); |
177 | 177 |
} |
178 | 178 |
|
179 | 179 |
///Return a vector multiplied by a scalar |
180 | 180 |
|
181 | 181 |
///Return a vector multiplied by a scalar. |
182 | 182 |
///\relates Point |
183 | 183 |
template<typename T> Point<T> operator*(const T &u,const Point<T> &x) { |
184 | 184 |
return x*u; |
185 | 185 |
} |
186 | 186 |
|
187 | 187 |
///Read a plain vector from a stream |
188 | 188 |
|
189 | 189 |
///Read a plain vector from a stream. |
190 | 190 |
///\relates Point |
191 | 191 |
/// |
192 | 192 |
template<typename T> |
193 | 193 |
inline std::istream& operator>>(std::istream &is, Point<T> &z) { |
194 | 194 |
char c; |
195 | 195 |
if (is >> c) { |
196 | 196 |
if (c != '(') is.putback(c); |
197 | 197 |
} else { |
198 | 198 |
is.clear(); |
199 | 199 |
} |
200 | 200 |
if (!(is >> z.x)) return is; |
201 | 201 |
if (is >> c) { |
202 | 202 |
if (c != ',') is.putback(c); |
203 | 203 |
} else { |
204 | 204 |
is.clear(); |
205 | 205 |
} |
206 | 206 |
if (!(is >> z.y)) return is; |
207 | 207 |
if (is >> c) { |
208 | 208 |
if (c != ')') is.putback(c); |
209 | 209 |
} else { |
210 | 210 |
is.clear(); |
211 | 211 |
} |
212 | 212 |
return is; |
213 | 213 |
} |
214 | 214 |
|
215 | 215 |
///Write a plain vector to a stream |
216 | 216 |
|
217 | 217 |
///Write a plain vector to a stream. |
218 | 218 |
///\relates Point |
219 | 219 |
/// |
220 | 220 |
template<typename T> |
221 | 221 |
inline std::ostream& operator<<(std::ostream &os, const Point<T>& z) |
222 | 222 |
{ |
223 | 223 |
os << "(" << z.x << "," << z.y << ")"; |
224 | 224 |
return os; |
225 | 225 |
} |
226 | 226 |
|
227 | 227 |
///Rotate by 90 degrees |
228 | 228 |
|
229 | 229 |
///Returns the parameter rotated by 90 degrees in positive direction. |
230 | 230 |
///\relates Point |
231 | 231 |
/// |
232 | 232 |
template<typename T> |
233 | 233 |
inline Point<T> rot90(const Point<T> &z) |
234 | 234 |
{ |
235 | 235 |
return Point<T>(-z.y,z.x); |
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
///Rotate by 180 degrees |
239 | 239 |
|
240 | 240 |
///Returns the parameter rotated by 180 degrees. |
241 | 241 |
///\relates Point |
242 | 242 |
/// |
243 | 243 |
template<typename T> |
244 | 244 |
inline Point<T> rot180(const Point<T> &z) |
245 | 245 |
{ |
246 | 246 |
return Point<T>(-z.x,-z.y); |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
///Rotate by 270 degrees |
250 | 250 |
|
251 | 251 |
///Returns the parameter rotated by 90 degrees in negative direction. |
252 | 252 |
///\relates Point |
253 | 253 |
/// |
254 | 254 |
template<typename T> |
255 | 255 |
inline Point<T> rot270(const Point<T> &z) |
256 | 256 |
{ |
257 | 257 |
return Point<T>(z.y,-z.x); |
258 | 258 |
} |
259 | 259 |
|
260 | 260 |
|
261 | 261 |
|
262 |
/// Bounding box of plain vectors ( |
|
262 |
/// Bounding box of plain vectors (points). |
|
263 | 263 |
|
264 | 264 |
/// A class to calculate or store the bounding box of plain vectors |
265 |
/// (\ref Point points). |
|
265 |
/// (\ref Point "points"). |
|
266 | 266 |
template<typename T> |
267 | 267 |
class Box { |
268 | 268 |
Point<T> _bottom_left, _top_right; |
269 | 269 |
bool _empty; |
270 | 270 |
public: |
271 | 271 |
|
272 | 272 |
///Default constructor: creates an empty box |
273 | 273 |
Box() { _empty = true; } |
274 | 274 |
|
275 | 275 |
///Construct a box from one point |
276 | 276 |
Box(Point<T> a) { |
277 | 277 |
_bottom_left = _top_right = a; |
278 | 278 |
_empty = false; |
279 | 279 |
} |
280 | 280 |
|
281 | 281 |
///Construct a box from two points |
282 | 282 |
|
283 | 283 |
///Construct a box from two points. |
284 | 284 |
///\param a The bottom left corner. |
285 | 285 |
///\param b The top right corner. |
286 | 286 |
///\warning The coordinates of the bottom left corner must be no more |
287 | 287 |
///than those of the top right one. |
288 | 288 |
Box(Point<T> a,Point<T> b) |
289 | 289 |
{ |
290 | 290 |
_bottom_left = a; |
291 | 291 |
_top_right = b; |
292 | 292 |
_empty = false; |
293 | 293 |
} |
294 | 294 |
|
295 | 295 |
///Construct a box from four numbers |
296 | 296 |
|
297 | 297 |
///Construct a box from four numbers. |
298 | 298 |
///\param l The left side of the box. |
299 | 299 |
///\param b The bottom of the box. |
300 | 300 |
///\param r The right side of the box. |
301 | 301 |
///\param t The top of the box. |
302 | 302 |
///\warning The left side must be no more than the right side and |
303 | 303 |
///bottom must be no more than the top. |
304 | 304 |
Box(T l,T b,T r,T t) |
305 | 305 |
{ |
306 | 306 |
_bottom_left=Point<T>(l,b); |
307 | 307 |
_top_right=Point<T>(r,t); |
308 | 308 |
_empty = false; |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
///Return \c true if the box is empty. |
312 | 312 |
|
313 | 313 |
///Return \c true if the box is empty (i.e. return \c false |
314 | 314 |
///if at least one point was added to the box or the coordinates of |
315 | 315 |
///the box were set). |
316 | 316 |
/// |
317 | 317 |
///The coordinates of an empty box are not defined. |
318 | 318 |
bool empty() const { |
319 | 319 |
return _empty; |
320 | 320 |
} |
321 | 321 |
|
322 | 322 |
///Make the box empty |
323 | 323 |
void clear() { |
324 | 324 |
_empty = true; |
325 | 325 |
} |
326 | 326 |
|
327 | 327 |
///Give back the bottom left corner of the box |
328 | 328 |
|
329 | 329 |
///Give back the bottom left corner of the box. |
330 | 330 |
///If the box is empty, then the return value is not defined. |
331 | 331 |
Point<T> bottomLeft() const { |
332 | 332 |
return _bottom_left; |
333 | 333 |
} |
334 | 334 |
|
335 | 335 |
///Set the bottom left corner of the box |
336 | 336 |
|
337 | 337 |
///Set the bottom left corner of the box. |
338 | 338 |
///\pre The box must not be empty. |
339 | 339 |
void bottomLeft(Point<T> p) { |
340 | 340 |
_bottom_left = p; |
341 | 341 |
} |
342 | 342 |
|
343 | 343 |
///Give back the top right corner of the box |
344 | 344 |
|
345 | 345 |
///Give back the top right corner of the box. |
346 | 346 |
///If the box is empty, then the return value is not defined. |
347 | 347 |
Point<T> topRight() const { |
348 | 348 |
return _top_right; |
349 | 349 |
} |
350 | 350 |
|
351 | 351 |
///Set the top right corner of the box |
352 | 352 |
|
353 | 353 |
///Set the top right corner of the box. |
354 | 354 |
///\pre The box must not be empty. |
355 | 355 |
void topRight(Point<T> p) { |
356 | 356 |
_top_right = p; |
357 | 357 |
} |
358 | 358 |
|
359 | 359 |
///Give back the bottom right corner of the box |
360 | 360 |
|
361 | 361 |
///Give back the bottom right corner of the box. |
362 | 362 |
///If the box is empty, then the return value is not defined. |
363 | 363 |
Point<T> bottomRight() const { |
364 | 364 |
return Point<T>(_top_right.x,_bottom_left.y); |
365 | 365 |
} |
366 | 366 |
|
367 | 367 |
///Set the bottom right corner of the box |
368 | 368 |
|
369 | 369 |
///Set the bottom right corner of the box. |
370 | 370 |
///\pre The box must not be empty. |
371 | 371 |
void bottomRight(Point<T> p) { |
372 | 372 |
_top_right.x = p.x; |
373 | 373 |
_bottom_left.y = p.y; |
374 | 374 |
} |
375 | 375 |
|
376 | 376 |
///Give back the top left corner of the box |
377 | 377 |
|
378 | 378 |
///Give back the top left corner of the box. |
379 | 379 |
///If the box is empty, then the return value is not defined. |
380 | 380 |
Point<T> topLeft() const { |
381 | 381 |
return Point<T>(_bottom_left.x,_top_right.y); |
382 | 382 |
} |
383 | 383 |
|
384 | 384 |
///Set the top left corner of the box |
385 | 385 |
|
386 | 386 |
///Set the top left corner of the box. |
387 | 387 |
///\pre The box must not be empty. |
388 | 388 |
void topLeft(Point<T> p) { |
389 | 389 |
_top_right.y = p.y; |
390 | 390 |
_bottom_left.x = p.x; |
391 | 391 |
} |
392 | 392 |
|
393 | 393 |
///Give back the bottom of the box |
... | ... |
@@ -448,301 +448,296 @@ |
448 | 448 |
|
449 | 449 |
///Set the right side of the box |
450 | 450 |
|
451 | 451 |
///Set the right side of the box. |
452 | 452 |
///\pre The box must not be empty. |
453 | 453 |
void right(T t) { |
454 | 454 |
_top_right.x = t; |
455 | 455 |
} |
456 | 456 |
|
457 | 457 |
///Give back the height of the box |
458 | 458 |
|
459 | 459 |
///Give back the height of the box. |
460 | 460 |
///If the box is empty, then the return value is not defined. |
461 | 461 |
T height() const { |
462 | 462 |
return _top_right.y-_bottom_left.y; |
463 | 463 |
} |
464 | 464 |
|
465 | 465 |
///Give back the width of the box |
466 | 466 |
|
467 | 467 |
///Give back the width of the box. |
468 | 468 |
///If the box is empty, then the return value is not defined. |
469 | 469 |
T width() const { |
470 | 470 |
return _top_right.x-_bottom_left.x; |
471 | 471 |
} |
472 | 472 |
|
473 | 473 |
///Checks whether a point is inside the box |
474 | 474 |
bool inside(const Point<T>& u) const { |
475 | 475 |
if (_empty) |
476 | 476 |
return false; |
477 | 477 |
else { |
478 | 478 |
return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 && |
479 | 479 |
(u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 ); |
480 | 480 |
} |
481 | 481 |
} |
482 | 482 |
|
483 | 483 |
///Increments the box with a point |
484 | 484 |
|
485 | 485 |
///Increments the box with a point. |
486 | 486 |
/// |
487 | 487 |
Box& add(const Point<T>& u){ |
488 | 488 |
if (_empty) { |
489 | 489 |
_bottom_left = _top_right = u; |
490 | 490 |
_empty = false; |
491 | 491 |
} |
492 | 492 |
else { |
493 | 493 |
if (_bottom_left.x > u.x) _bottom_left.x = u.x; |
494 | 494 |
if (_bottom_left.y > u.y) _bottom_left.y = u.y; |
495 | 495 |
if (_top_right.x < u.x) _top_right.x = u.x; |
496 | 496 |
if (_top_right.y < u.y) _top_right.y = u.y; |
497 | 497 |
} |
498 | 498 |
return *this; |
499 | 499 |
} |
500 | 500 |
|
501 | 501 |
///Increments the box to contain another box |
502 | 502 |
|
503 | 503 |
///Increments the box to contain another box. |
504 | 504 |
/// |
505 | 505 |
Box& add(const Box &u){ |
506 | 506 |
if ( !u.empty() ){ |
507 | 507 |
add(u._bottom_left); |
508 | 508 |
add(u._top_right); |
509 | 509 |
} |
510 | 510 |
return *this; |
511 | 511 |
} |
512 | 512 |
|
513 | 513 |
///Intersection of two boxes |
514 | 514 |
|
515 | 515 |
///Intersection of two boxes. |
516 | 516 |
/// |
517 | 517 |
Box operator&(const Box& u) const { |
518 | 518 |
Box b; |
519 | 519 |
if (_empty || u._empty) { |
520 | 520 |
b._empty = true; |
521 | 521 |
} else { |
522 | 522 |
b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x); |
523 | 523 |
b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y); |
524 | 524 |
b._top_right.x = std::min(_top_right.x, u._top_right.x); |
525 | 525 |
b._top_right.y = std::min(_top_right.y, u._top_right.y); |
526 | 526 |
b._empty = b._bottom_left.x > b._top_right.x || |
527 | 527 |
b._bottom_left.y > b._top_right.y; |
528 | 528 |
} |
529 | 529 |
return b; |
530 | 530 |
} |
531 | 531 |
|
532 | 532 |
};//class Box |
533 | 533 |
|
534 | 534 |
|
535 | 535 |
///Read a box from a stream |
536 | 536 |
|
537 | 537 |
///Read a box from a stream. |
538 | 538 |
///\relates Box |
539 | 539 |
template<typename T> |
540 | 540 |
inline std::istream& operator>>(std::istream &is, Box<T>& b) { |
541 | 541 |
char c; |
542 | 542 |
Point<T> p; |
543 | 543 |
if (is >> c) { |
544 | 544 |
if (c != '(') is.putback(c); |
545 | 545 |
} else { |
546 | 546 |
is.clear(); |
547 | 547 |
} |
548 | 548 |
if (!(is >> p)) return is; |
549 | 549 |
b.bottomLeft(p); |
550 | 550 |
if (is >> c) { |
551 | 551 |
if (c != ',') is.putback(c); |
552 | 552 |
} else { |
553 | 553 |
is.clear(); |
554 | 554 |
} |
555 | 555 |
if (!(is >> p)) return is; |
556 | 556 |
b.topRight(p); |
557 | 557 |
if (is >> c) { |
558 | 558 |
if (c != ')') is.putback(c); |
559 | 559 |
} else { |
560 | 560 |
is.clear(); |
561 | 561 |
} |
562 | 562 |
return is; |
563 | 563 |
} |
564 | 564 |
|
565 | 565 |
///Write a box to a stream |
566 | 566 |
|
567 | 567 |
///Write a box to a stream. |
568 | 568 |
///\relates Box |
569 | 569 |
template<typename T> |
570 | 570 |
inline std::ostream& operator<<(std::ostream &os, const Box<T>& b) |
571 | 571 |
{ |
572 | 572 |
os << "(" << b.bottomLeft() << "," << b.topRight() << ")"; |
573 | 573 |
return os; |
574 | 574 |
} |
575 | 575 |
|
576 |
///Map of x-coordinates of a |
|
576 |
///Map of x-coordinates of a <tt>Point</tt>-map |
|
577 | 577 |
|
578 |
///Map of x-coordinates of a \ref Point "Point"-map. |
|
578 | 579 |
///\ingroup maps |
579 |
///Map of x-coordinates of a \ref Point "Point"-map. |
|
580 |
/// |
|
581 | 580 |
template<class M> |
582 | 581 |
class XMap |
583 | 582 |
{ |
584 | 583 |
M& _map; |
585 | 584 |
public: |
586 | 585 |
|
587 | 586 |
typedef typename M::Value::Value Value; |
588 | 587 |
typedef typename M::Key Key; |
589 | 588 |
///\e |
590 | 589 |
XMap(M& map) : _map(map) {} |
591 | 590 |
Value operator[](Key k) const {return _map[k].x;} |
592 | 591 |
void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));} |
593 | 592 |
}; |
594 | 593 |
|
595 |
///Returns an |
|
594 |
///Returns an XMap class |
|
596 | 595 |
|
597 |
///This function just returns an |
|
596 |
///This function just returns an XMap class. |
|
598 | 597 |
/// |
599 | 598 |
///\ingroup maps |
600 | 599 |
///\relates XMap |
601 | 600 |
template<class M> |
602 | 601 |
inline XMap<M> xMap(M &m) |
603 | 602 |
{ |
604 | 603 |
return XMap<M>(m); |
605 | 604 |
} |
606 | 605 |
|
607 | 606 |
template<class M> |
608 | 607 |
inline XMap<M> xMap(const M &m) |
609 | 608 |
{ |
610 | 609 |
return XMap<M>(m); |
611 | 610 |
} |
612 | 611 |
|
613 |
///Constant (read only) version of |
|
612 |
///Constant (read only) version of XMap |
|
614 | 613 |
|
614 |
///Constant (read only) version of XMap. |
|
615 | 615 |
///\ingroup maps |
616 |
///Constant (read only) version of \ref XMap |
|
617 |
/// |
|
618 | 616 |
template<class M> |
619 | 617 |
class ConstXMap |
620 | 618 |
{ |
621 | 619 |
const M& _map; |
622 | 620 |
public: |
623 | 621 |
|
624 | 622 |
typedef typename M::Value::Value Value; |
625 | 623 |
typedef typename M::Key Key; |
626 | 624 |
///\e |
627 | 625 |
ConstXMap(const M &map) : _map(map) {} |
628 | 626 |
Value operator[](Key k) const {return _map[k].x;} |
629 | 627 |
}; |
630 | 628 |
|
631 |
///Returns a |
|
629 |
///Returns a ConstXMap class |
|
632 | 630 |
|
633 |
///This function just returns a |
|
631 |
///This function just returns a ConstXMap class. |
|
634 | 632 |
/// |
635 | 633 |
///\ingroup maps |
636 | 634 |
///\relates ConstXMap |
637 | 635 |
template<class M> |
638 | 636 |
inline ConstXMap<M> xMap(const M &m) |
639 | 637 |
{ |
640 | 638 |
return ConstXMap<M>(m); |
641 | 639 |
} |
642 | 640 |
|
643 |
///Map of y-coordinates of a |
|
641 |
///Map of y-coordinates of a <tt>Point</tt>-map |
|
644 | 642 |
|
643 |
///Map of y-coordinates of a \ref Point "Point"-map. |
|
645 | 644 |
///\ingroup maps |
646 |
///Map of y-coordinates of a \ref Point "Point"-map. |
|
647 |
/// |
|
648 | 645 |
template<class M> |
649 | 646 |
class YMap |
650 | 647 |
{ |
651 | 648 |
M& _map; |
652 | 649 |
public: |
653 | 650 |
|
654 | 651 |
typedef typename M::Value::Value Value; |
655 | 652 |
typedef typename M::Key Key; |
656 | 653 |
///\e |
657 | 654 |
YMap(M& map) : _map(map) {} |
658 | 655 |
Value operator[](Key k) const {return _map[k].y;} |
659 | 656 |
void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));} |
660 | 657 |
}; |
661 | 658 |
|
662 |
///Returns a |
|
659 |
///Returns a YMap class |
|
663 | 660 |
|
664 |
///This function just returns a |
|
661 |
///This function just returns a YMap class. |
|
665 | 662 |
/// |
666 | 663 |
///\ingroup maps |
667 | 664 |
///\relates YMap |
668 | 665 |
template<class M> |
669 | 666 |
inline YMap<M> yMap(M &m) |
670 | 667 |
{ |
671 | 668 |
return YMap<M>(m); |
672 | 669 |
} |
673 | 670 |
|
674 | 671 |
template<class M> |
675 | 672 |
inline YMap<M> yMap(const M &m) |
676 | 673 |
{ |
677 | 674 |
return YMap<M>(m); |
678 | 675 |
} |
679 | 676 |
|
680 |
///Constant (read only) version of |
|
677 |
///Constant (read only) version of YMap |
|
681 | 678 |
|
679 |
///Constant (read only) version of YMap. |
|
682 | 680 |
///\ingroup maps |
683 |
///Constant (read only) version of \ref YMap |
|
684 |
/// |
|
685 | 681 |
template<class M> |
686 | 682 |
class ConstYMap |
687 | 683 |
{ |
688 | 684 |
const M& _map; |
689 | 685 |
public: |
690 | 686 |
|
691 | 687 |
typedef typename M::Value::Value Value; |
692 | 688 |
typedef typename M::Key Key; |
693 | 689 |
///\e |
694 | 690 |
ConstYMap(const M &map) : _map(map) {} |
695 | 691 |
Value operator[](Key k) const {return _map[k].y;} |
696 | 692 |
}; |
697 | 693 |
|
698 |
///Returns a |
|
694 |
///Returns a ConstYMap class |
|
699 | 695 |
|
700 |
///This function just returns a |
|
696 |
///This function just returns a ConstYMap class. |
|
701 | 697 |
/// |
702 | 698 |
///\ingroup maps |
703 | 699 |
///\relates ConstYMap |
704 | 700 |
template<class M> |
705 | 701 |
inline ConstYMap<M> yMap(const M &m) |
706 | 702 |
{ |
707 | 703 |
return ConstYMap<M>(m); |
708 | 704 |
} |
709 | 705 |
|
710 | 706 |
|
711 |
///\brief Map of the \ref Point::normSquare() "normSquare()" |
|
712 |
///of a \ref Point "Point"-map |
|
707 |
///\brief Map of the normSquare() of a <tt>Point</tt>-map |
|
713 | 708 |
/// |
714 | 709 |
///Map of the \ref Point::normSquare() "normSquare()" |
715 | 710 |
///of a \ref Point "Point"-map. |
716 | 711 |
///\ingroup maps |
717 | 712 |
template<class M> |
718 | 713 |
class NormSquareMap |
719 | 714 |
{ |
720 | 715 |
const M& _map; |
721 | 716 |
public: |
722 | 717 |
|
723 | 718 |
typedef typename M::Value::Value Value; |
724 | 719 |
typedef typename M::Key Key; |
725 | 720 |
///\e |
726 | 721 |
NormSquareMap(const M &map) : _map(map) {} |
727 | 722 |
Value operator[](Key k) const {return _map[k].normSquare();} |
728 | 723 |
}; |
729 | 724 |
|
730 |
///Returns a |
|
725 |
///Returns a NormSquareMap class |
|
731 | 726 |
|
732 |
///This function just returns a |
|
727 |
///This function just returns a NormSquareMap class. |
|
733 | 728 |
/// |
734 | 729 |
///\ingroup maps |
735 | 730 |
///\relates NormSquareMap |
736 | 731 |
template<class M> |
737 | 732 |
inline NormSquareMap<M> normSquareMap(const M &m) |
738 | 733 |
{ |
739 | 734 |
return NormSquareMap<M>(m); |
740 | 735 |
} |
741 | 736 |
|
742 | 737 |
/// @} |
743 | 738 |
|
744 | 739 |
} //namespce dim2 |
745 | 740 |
|
746 | 741 |
} //namespace lemon |
747 | 742 |
|
748 | 743 |
#endif //LEMON_DIM2_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_GRAPH_TO_EPS_H |
20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<fstream> |
24 | 24 |
#include<sstream> |
25 | 25 |
#include<algorithm> |
26 | 26 |
#include<vector> |
27 | 27 |
|
28 | 28 |
#ifndef WIN32 |
29 | 29 |
#include<sys/time.h> |
30 | 30 |
#include<ctime> |
31 | 31 |
#else |
32 | 32 |
#define WIN32_LEAN_AND_MEAN |
33 | 33 |
#define NOMINMAX |
34 | 34 |
#include<windows.h> |
35 | 35 |
#endif |
36 | 36 |
|
37 | 37 |
#include<lemon/math.h> |
38 | 38 |
#include<lemon/core.h> |
39 | 39 |
#include<lemon/dim2.h> |
40 | 40 |
#include<lemon/maps.h> |
41 | 41 |
#include<lemon/color.h> |
42 | 42 |
#include<lemon/bits/bezier.h> |
43 | 43 |
#include<lemon/error.h> |
44 | 44 |
|
45 | 45 |
|
46 | 46 |
///\ingroup eps_io |
47 | 47 |
///\file |
48 | 48 |
///\brief A well configurable tool for visualizing graphs |
49 | 49 |
|
50 | 50 |
namespace lemon { |
51 | 51 |
|
52 | 52 |
namespace _graph_to_eps_bits { |
53 | 53 |
template<class MT> |
54 | 54 |
class _NegY { |
55 | 55 |
public: |
56 | 56 |
typedef typename MT::Key Key; |
57 | 57 |
typedef typename MT::Value Value; |
58 | 58 |
const MT ↦ |
59 | 59 |
int yscale; |
60 | 60 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
61 | 61 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
62 | 62 |
}; |
63 | 63 |
} |
64 | 64 |
|
65 |
///Default traits class of |
|
65 |
///Default traits class of GraphToEps |
|
66 | 66 |
|
67 | 67 |
///Default traits class of \ref GraphToEps. |
68 | 68 |
/// |
69 | 69 |
///\c G is the type of the underlying graph. |
70 | 70 |
template<class G> |
71 | 71 |
struct DefaultGraphToEpsTraits |
72 | 72 |
{ |
73 | 73 |
typedef G Graph; |
74 | 74 |
typedef typename Graph::Node Node; |
75 | 75 |
typedef typename Graph::NodeIt NodeIt; |
76 | 76 |
typedef typename Graph::Arc Arc; |
77 | 77 |
typedef typename Graph::ArcIt ArcIt; |
78 | 78 |
typedef typename Graph::InArcIt InArcIt; |
79 | 79 |
typedef typename Graph::OutArcIt OutArcIt; |
80 | 80 |
|
81 | 81 |
|
82 | 82 |
const Graph &g; |
83 | 83 |
|
84 | 84 |
std::ostream& os; |
85 | 85 |
|
86 | 86 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
87 | 87 |
CoordsMapType _coords; |
88 | 88 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
89 | 89 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
90 | 90 |
|
91 | 91 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
92 | 92 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
93 | 93 |
|
94 | 94 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
95 | 95 |
|
96 | 96 |
double _arcWidthScale; |
97 | 97 |
|
98 | 98 |
double _nodeScale; |
99 | 99 |
double _xBorder, _yBorder; |
100 | 100 |
double _scale; |
101 | 101 |
double _nodeBorderQuotient; |
102 | 102 |
|
103 | 103 |
bool _drawArrows; |
104 | 104 |
double _arrowLength, _arrowWidth; |
105 | 105 |
|
106 | 106 |
bool _showNodes, _showArcs; |
107 | 107 |
|
108 | 108 |
bool _enableParallel; |
109 | 109 |
double _parArcDist; |
110 | 110 |
|
111 | 111 |
bool _showNodeText; |
112 | 112 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
113 | 113 |
double _nodeTextSize; |
114 | 114 |
|
115 | 115 |
bool _showNodePsText; |
116 | 116 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
117 | 117 |
char *_nodePsTextsPreamble; |
118 | 118 |
|
119 | 119 |
bool _undirected; |
120 | 120 |
|
121 | 121 |
bool _pleaseRemoveOsStream; |
122 | 122 |
|
123 | 123 |
bool _scaleToA4; |
124 | 124 |
|
125 | 125 |
std::string _title; |
126 | 126 |
std::string _copyright; |
127 | 127 |
|
128 | 128 |
enum NodeTextColorType |
129 | 129 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
130 | 130 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
131 | 131 |
|
132 | 132 |
bool _autoNodeScale; |
133 | 133 |
bool _autoArcWidthScale; |
134 | 134 |
|
135 | 135 |
bool _absoluteNodeSizes; |
136 | 136 |
bool _absoluteArcWidths; |
137 | 137 |
|
138 | 138 |
bool _negY; |
139 | 139 |
|
140 | 140 |
bool _preScale; |
141 | 141 |
///Constructor |
142 | 142 |
|
143 | 143 |
///Constructor |
144 | 144 |
///\param _g Reference to the graph to be printed. |
145 | 145 |
///\param _os Reference to the output stream. |
146 | 146 |
///\param _os Reference to the output stream. |
147 | 147 |
///By default it is <tt>std::cout</tt>. |
148 | 148 |
///\param _pros If it is \c true, then the \c ostream referenced by \c _os |
149 | 149 |
///will be explicitly deallocated by the destructor. |
150 | 150 |
DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
151 | 151 |
bool _pros=false) : |
152 | 152 |
g(_g), os(_os), |
153 | 153 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
154 | 154 |
_nodeColors(WHITE), _arcColors(BLACK), |
155 | 155 |
_arcWidths(1.0), _arcWidthScale(0.003), |
156 | 156 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
157 | 157 |
_nodeBorderQuotient(.1), |
158 | 158 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
159 | 159 |
_showNodes(true), _showArcs(true), |
160 | 160 |
_enableParallel(false), _parArcDist(1), |
161 | 161 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
162 | 162 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
163 | 163 |
_undirected(lemon::UndirectedTagIndicator<G>::value), |
164 | 164 |
_pleaseRemoveOsStream(_pros), _scaleToA4(false), |
165 | 165 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
166 | 166 |
_autoNodeScale(false), |
167 | 167 |
_autoArcWidthScale(false), |
168 | 168 |
_absoluteNodeSizes(false), |
169 | 169 |
_absoluteArcWidths(false), |
170 | 170 |
_negY(false), |
171 | 171 |
_preScale(true) |
172 | 172 |
{} |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
176 | 176 |
|
177 | 177 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
178 | 178 |
/// |
179 | 179 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
180 | 180 |
template<class T> class GraphToEps : public T |
181 | 181 |
{ |
182 | 182 |
// Can't believe it is required by the C++ standard |
183 | 183 |
using T::g; |
184 | 184 |
using T::os; |
185 | 185 |
|
186 | 186 |
using T::_coords; |
187 | 187 |
using T::_nodeSizes; |
188 | 188 |
using T::_nodeShapes; |
189 | 189 |
using T::_nodeColors; |
190 | 190 |
using T::_arcColors; |
191 | 191 |
using T::_arcWidths; |
192 | 192 |
|
193 | 193 |
using T::_arcWidthScale; |
... | ... |
@@ -288,257 +288,257 @@ |
288 | 288 |
} |
289 | 289 |
void changeSource(Arc e, Node n) |
290 | 290 |
{ |
291 | 291 |
if(arcs[e.id].next_out != -1) |
292 | 292 |
arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out; |
293 | 293 |
if(arcs[e.id].prev_out != -1) |
294 | 294 |
arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out; |
295 | 295 |
else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out; |
296 | 296 |
if (nodes[n.id].first_out != -1) { |
297 | 297 |
arcs[nodes[n.id].first_out].prev_out = e.id; |
298 | 298 |
} |
299 | 299 |
arcs[e.id].source = n.id; |
300 | 300 |
arcs[e.id].prev_out = -1; |
301 | 301 |
arcs[e.id].next_out = nodes[n.id].first_out; |
302 | 302 |
nodes[n.id].first_out = e.id; |
303 | 303 |
} |
304 | 304 |
|
305 | 305 |
}; |
306 | 306 |
|
307 | 307 |
typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
308 | 308 |
|
309 | 309 |
/// \addtogroup graphs |
310 | 310 |
/// @{ |
311 | 311 |
|
312 | 312 |
///A general directed graph structure. |
313 | 313 |
|
314 | 314 |
///\ref ListDigraph is a simple and fast <em>directed graph</em> |
315 | 315 |
///implementation based on static linked lists that are stored in |
316 | 316 |
///\c std::vector structures. |
317 | 317 |
/// |
318 | 318 |
///It conforms to the \ref concepts::Digraph "Digraph concept" and it |
319 | 319 |
///also provides several useful additional functionalities. |
320 | 320 |
///Most of the member functions and nested classes are documented |
321 | 321 |
///only in the concept class. |
322 | 322 |
/// |
323 | 323 |
///An important extra feature of this digraph implementation is that |
324 | 324 |
///its maps are real \ref concepts::ReferenceMap "reference map"s. |
325 | 325 |
/// |
326 | 326 |
///\sa concepts::Digraph |
327 | 327 |
|
328 | 328 |
class ListDigraph : public ExtendedListDigraphBase { |
329 | 329 |
private: |
330 | 330 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
331 | 331 |
|
332 | 332 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
333 | 333 |
/// |
334 | 334 |
ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {}; |
335 | 335 |
///\brief Assignment of ListDigraph to another one is \e not allowed. |
336 | 336 |
///Use copyDigraph() instead. |
337 | 337 |
|
338 | 338 |
///Assignment of ListDigraph to another one is \e not allowed. |
339 | 339 |
///Use copyDigraph() instead. |
340 | 340 |
void operator=(const ListDigraph &) {} |
341 | 341 |
public: |
342 | 342 |
|
343 | 343 |
typedef ExtendedListDigraphBase Parent; |
344 | 344 |
|
345 | 345 |
/// Constructor |
346 | 346 |
|
347 | 347 |
/// Constructor. |
348 | 348 |
/// |
349 | 349 |
ListDigraph() {} |
350 | 350 |
|
351 | 351 |
///Add a new node to the digraph. |
352 | 352 |
|
353 | 353 |
///Add a new node to the digraph. |
354 | 354 |
///\return the new node. |
355 | 355 |
Node addNode() { return Parent::addNode(); } |
356 | 356 |
|
357 | 357 |
///Add a new arc to the digraph. |
358 | 358 |
|
359 | 359 |
///Add a new arc to the digraph with source node \c s |
360 | 360 |
///and target node \c t. |
361 | 361 |
///\return the new arc. |
362 | 362 |
Arc addArc(const Node& s, const Node& t) { |
363 | 363 |
return Parent::addArc(s, t); |
364 | 364 |
} |
365 | 365 |
|
366 | 366 |
///\brief Erase a node from the digraph. |
367 | 367 |
/// |
368 | 368 |
///Erase a node from the digraph. |
369 | 369 |
/// |
370 | 370 |
void erase(const Node& n) { Parent::erase(n); } |
371 | 371 |
|
372 | 372 |
///\brief Erase an arc from the digraph. |
373 | 373 |
/// |
374 | 374 |
///Erase an arc from the digraph. |
375 | 375 |
/// |
376 | 376 |
void erase(const Arc& a) { Parent::erase(a); } |
377 | 377 |
|
378 | 378 |
/// Node validity check |
379 | 379 |
|
380 | 380 |
/// This function gives back true if the given node is valid, |
381 | 381 |
/// ie. it is a real node of the graph. |
382 | 382 |
/// |
383 | 383 |
/// \warning A Node pointing to a removed item |
384 | 384 |
/// could become valid again later if new nodes are |
385 | 385 |
/// added to the graph. |
386 | 386 |
bool valid(Node n) const { return Parent::valid(n); } |
387 | 387 |
|
388 | 388 |
/// Arc validity check |
389 | 389 |
|
390 | 390 |
/// This function gives back true if the given arc is valid, |
391 | 391 |
/// ie. it is a real arc of the graph. |
392 | 392 |
/// |
393 | 393 |
/// \warning An Arc pointing to a removed item |
394 | 394 |
/// could become valid again later if new nodes are |
395 | 395 |
/// added to the graph. |
396 | 396 |
bool valid(Arc a) const { return Parent::valid(a); } |
397 | 397 |
|
398 | 398 |
/// Change the target of \c a to \c n |
399 | 399 |
|
400 | 400 |
/// Change the target of \c a to \c n |
401 | 401 |
/// |
402 | 402 |
///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing |
403 | 403 |
///the changed arc remain valid. However <tt>InArcIt</tt>s are |
404 | 404 |
///invalidated. |
405 | 405 |
/// |
406 | 406 |
///\warning This functionality cannot be used together with the Snapshot |
407 | 407 |
///feature. |
408 | 408 |
void changeTarget(Arc a, Node n) { |
409 | 409 |
Parent::changeTarget(a,n); |
410 | 410 |
} |
411 | 411 |
/// Change the source of \c a to \c n |
412 | 412 |
|
413 | 413 |
/// Change the source of \c a to \c n |
414 | 414 |
/// |
415 | 415 |
///\note The <tt>InArcIt</tt>s referencing the changed arc remain |
416 |
///valid. However the <tt>ArcIt<tt>s and <tt>OutArcIt</tt>s are |
|
416 |
///valid. However the <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s are |
|
417 | 417 |
///invalidated. |
418 | 418 |
/// |
419 | 419 |
///\warning This functionality cannot be used together with the Snapshot |
420 | 420 |
///feature. |
421 | 421 |
void changeSource(Arc a, Node n) { |
422 | 422 |
Parent::changeSource(a,n); |
423 | 423 |
} |
424 | 424 |
|
425 | 425 |
/// Invert the direction of an arc. |
426 | 426 |
|
427 | 427 |
///\note The <tt>ArcIt</tt>s referencing the changed arc remain |
428 | 428 |
///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are |
429 | 429 |
///invalidated. |
430 | 430 |
/// |
431 | 431 |
///\warning This functionality cannot be used together with the Snapshot |
432 | 432 |
///feature. |
433 | 433 |
void reverseArc(Arc e) { |
434 | 434 |
Node t=target(e); |
435 | 435 |
changeTarget(e,source(e)); |
436 | 436 |
changeSource(e,t); |
437 | 437 |
} |
438 | 438 |
|
439 | 439 |
/// Reserve memory for nodes. |
440 | 440 |
|
441 | 441 |
/// Using this function it is possible to avoid the superfluous memory |
442 | 442 |
/// allocation: if you know that the digraph you want to build will |
443 | 443 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
444 | 444 |
/// then it is worth reserving space for this amount before starting |
445 | 445 |
/// to build the digraph. |
446 | 446 |
/// \sa reserveArc |
447 | 447 |
void reserveNode(int n) { nodes.reserve(n); }; |
448 | 448 |
|
449 | 449 |
/// Reserve memory for arcs. |
450 | 450 |
|
451 | 451 |
/// Using this function it is possible to avoid the superfluous memory |
452 | 452 |
/// allocation: if you know that the digraph you want to build will |
453 | 453 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
454 | 454 |
/// then it is worth reserving space for this amount before starting |
455 | 455 |
/// to build the digraph. |
456 | 456 |
/// \sa reserveNode |
457 | 457 |
void reserveArc(int m) { arcs.reserve(m); }; |
458 | 458 |
|
459 | 459 |
///Contract two nodes. |
460 | 460 |
|
461 | 461 |
///This function contracts two nodes. |
462 | 462 |
///Node \p b will be removed but instead of deleting |
463 | 463 |
///incident arcs, they will be joined to \p a. |
464 | 464 |
///The last parameter \p r controls whether to remove loops. \c true |
465 | 465 |
///means that loops will be removed. |
466 | 466 |
/// |
467 | 467 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
468 | 468 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s |
469 | 469 |
///may be invalidated. |
470 | 470 |
/// |
471 | 471 |
///\warning This functionality cannot be used together with the Snapshot |
472 | 472 |
///feature. |
473 | 473 |
void contract(Node a, Node b, bool r = true) |
474 | 474 |
{ |
475 | 475 |
for(OutArcIt e(*this,b);e!=INVALID;) { |
476 | 476 |
OutArcIt f=e; |
477 | 477 |
++f; |
478 | 478 |
if(r && target(e)==a) erase(e); |
479 | 479 |
else changeSource(e,a); |
480 | 480 |
e=f; |
481 | 481 |
} |
482 | 482 |
for(InArcIt e(*this,b);e!=INVALID;) { |
483 | 483 |
InArcIt f=e; |
484 | 484 |
++f; |
485 | 485 |
if(r && source(e)==a) erase(e); |
486 | 486 |
else changeTarget(e,a); |
487 | 487 |
e=f; |
488 | 488 |
} |
489 | 489 |
erase(b); |
490 | 490 |
} |
491 | 491 |
|
492 | 492 |
///Split a node. |
493 | 493 |
|
494 | 494 |
///This function splits a node. First a new node is added to the digraph, |
495 | 495 |
///then the source of each outgoing arc of \c n is moved to this new node. |
496 | 496 |
///If \c connect is \c true (this is the default value), then a new arc |
497 | 497 |
///from \c n to the newly created node is also added. |
498 | 498 |
///\return The newly created node. |
499 | 499 |
/// |
500 | 500 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
501 | 501 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may |
502 | 502 |
///be invalidated. |
503 | 503 |
/// |
504 | 504 |
///\warning This functionality cannot be used in conjunction with the |
505 | 505 |
///Snapshot feature. |
506 | 506 |
Node split(Node n, bool connect = true) { |
507 | 507 |
Node b = addNode(); |
508 | 508 |
for(OutArcIt e(*this,n);e!=INVALID;) { |
509 | 509 |
OutArcIt f=e; |
510 | 510 |
++f; |
511 | 511 |
changeSource(e,b); |
512 | 512 |
e=f; |
513 | 513 |
} |
514 | 514 |
if (connect) addArc(n,b); |
515 | 515 |
return b; |
516 | 516 |
} |
517 | 517 |
|
518 | 518 |
///Split an arc. |
519 | 519 |
|
520 | 520 |
///This function splits an arc. First a new node \c b is added to |
521 | 521 |
///the digraph, then the original arc is re-targeted to \c |
522 | 522 |
///b. Finally an arc from \c b to the original target is added. |
523 | 523 |
/// |
524 | 524 |
///\return The newly created node. |
525 | 525 |
/// |
526 | 526 |
///\warning This functionality cannot be used together with the |
527 | 527 |
///Snapshot feature. |
528 | 528 |
Node split(Arc e) { |
529 | 529 |
Node b = addNode(); |
530 | 530 |
addArc(b,target(e)); |
531 | 531 |
changeTarget(e,b); |
532 | 532 |
return b; |
533 | 533 |
} |
534 | 534 |
|
535 | 535 |
/// \brief Class to make a snapshot of the digraph and restore |
536 | 536 |
/// it later. |
537 | 537 |
/// |
538 | 538 |
/// Class to make a snapshot of the digraph and restore it later. |
539 | 539 |
/// |
540 | 540 |
/// The newly added nodes and arcs can be removed using the |
541 | 541 |
/// restore() function. |
542 | 542 |
/// |
543 | 543 |
/// \warning Arc and node deletions and other modifications (e.g. |
544 | 544 |
/// contracting, splitting, reversing arcs or nodes) cannot be |
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_MAPS_H |
20 | 20 |
#define LEMON_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <functional> |
24 | 24 |
#include <vector> |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
|
28 | 28 |
///\file |
29 | 29 |
///\ingroup maps |
30 | 30 |
///\brief Miscellaneous property maps |
31 | 31 |
|
32 | 32 |
#include <map> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup maps |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
/// Base class of maps. |
40 | 40 |
|
41 | 41 |
/// Base class of maps. It provides the necessary type definitions |
42 | 42 |
/// required by the map %concepts. |
43 | 43 |
template<typename K, typename V> |
44 | 44 |
class MapBase { |
45 | 45 |
public: |
46 |
/// \ |
|
46 |
/// \brief The key type of the map. |
|
47 | 47 |
typedef K Key; |
48 | 48 |
/// \brief The value type of the map. |
49 | 49 |
/// (The type of objects associated with the keys). |
50 | 50 |
typedef V Value; |
51 | 51 |
}; |
52 | 52 |
|
53 | 53 |
|
54 | 54 |
/// Null map. (a.k.a. DoNothingMap) |
55 | 55 |
|
56 | 56 |
/// This map can be used if you have to provide a map only for |
57 | 57 |
/// its type definitions, or if you have to provide a writable map, |
58 | 58 |
/// but data written to it is not required (i.e. it will be sent to |
59 | 59 |
/// <tt>/dev/null</tt>). |
60 | 60 |
/// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
61 | 61 |
/// |
62 | 62 |
/// \sa ConstMap |
63 | 63 |
template<typename K, typename V> |
64 | 64 |
class NullMap : public MapBase<K, V> { |
65 | 65 |
public: |
66 | 66 |
typedef MapBase<K, V> Parent; |
67 | 67 |
typedef typename Parent::Key Key; |
68 | 68 |
typedef typename Parent::Value Value; |
69 | 69 |
|
70 | 70 |
/// Gives back a default constructed element. |
71 | 71 |
Value operator[](const Key&) const { return Value(); } |
72 | 72 |
/// Absorbs the value. |
73 | 73 |
void set(const Key&, const Value&) {} |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
/// Returns a \c NullMap class |
77 | 77 |
|
78 | 78 |
/// This function just returns a \c NullMap class. |
79 | 79 |
/// \relates NullMap |
80 | 80 |
template <typename K, typename V> |
81 | 81 |
NullMap<K, V> nullMap() { |
82 | 82 |
return NullMap<K, V>(); |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
|
86 | 86 |
/// Constant map. |
87 | 87 |
|
88 | 88 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
89 | 89 |
/// value to each key. |
90 | 90 |
/// |
91 | 91 |
/// In other aspects it is equivalent to \c NullMap. |
92 | 92 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
93 | 93 |
/// concept, but it absorbs the data written to it. |
94 | 94 |
/// |
95 | 95 |
/// The simplest way of using this map is through the constMap() |
96 | 96 |
/// function. |
97 | 97 |
/// |
98 | 98 |
/// \sa NullMap |
99 | 99 |
/// \sa IdentityMap |
100 | 100 |
template<typename K, typename V> |
101 | 101 |
class ConstMap : public MapBase<K, V> { |
102 | 102 |
private: |
103 | 103 |
V _value; |
104 | 104 |
public: |
105 | 105 |
typedef MapBase<K, V> Parent; |
106 | 106 |
typedef typename Parent::Key Key; |
107 | 107 |
typedef typename Parent::Value Value; |
108 | 108 |
|
109 | 109 |
/// Default constructor |
110 | 110 |
|
111 | 111 |
/// Default constructor. |
112 | 112 |
/// The value of the map will be default constructed. |
113 | 113 |
ConstMap() {} |
114 | 114 |
|
115 | 115 |
/// Constructor with specified initial value |
116 | 116 |
|
117 | 117 |
/// Constructor with specified initial value. |
118 | 118 |
/// \param v The initial value of the map. |
119 | 119 |
ConstMap(const Value &v) : _value(v) {} |
120 | 120 |
|
121 | 121 |
/// Gives back the specified value. |
122 | 122 |
Value operator[](const Key&) const { return _value; } |
123 | 123 |
|
124 | 124 |
/// Absorbs the value. |
125 | 125 |
void set(const Key&, const Value&) {} |
126 | 126 |
|
127 | 127 |
/// Sets the value that is assigned to each key. |
128 | 128 |
void setAll(const Value &v) { |
129 | 129 |
_value = v; |
130 | 130 |
} |
131 | 131 |
|
132 | 132 |
template<typename V1> |
133 | 133 |
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {} |
134 | 134 |
}; |
135 | 135 |
|
136 | 136 |
/// Returns a \c ConstMap class |
137 | 137 |
|
138 | 138 |
/// This function just returns a \c ConstMap class. |
139 | 139 |
/// \relates ConstMap |
140 | 140 |
template<typename K, typename V> |
141 | 141 |
inline ConstMap<K, V> constMap(const V &v) { |
142 | 142 |
return ConstMap<K, V>(v); |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
template<typename K, typename V> |
146 | 146 |
inline ConstMap<K, V> constMap() { |
147 | 147 |
return ConstMap<K, V>(); |
148 | 148 |
} |
149 | 149 |
|
150 | 150 |
|
151 | 151 |
template<typename T, T v> |
152 | 152 |
struct Const {}; |
153 | 153 |
|
154 | 154 |
/// Constant map with inlined constant value. |
155 | 155 |
|
156 | 156 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
157 | 157 |
/// value to each key. |
158 | 158 |
/// |
159 | 159 |
/// In other aspects it is equivalent to \c NullMap. |
160 | 160 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
161 | 161 |
/// concept, but it absorbs the data written to it. |
162 | 162 |
/// |
163 | 163 |
/// The simplest way of using this map is through the constMap() |
164 | 164 |
/// function. |
165 | 165 |
/// |
166 | 166 |
/// \sa NullMap |
167 | 167 |
/// \sa IdentityMap |
168 | 168 |
template<typename K, typename V, V v> |
169 | 169 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
170 | 170 |
public: |
171 | 171 |
typedef MapBase<K, V> Parent; |
172 | 172 |
typedef typename Parent::Key Key; |
173 | 173 |
typedef typename Parent::Value Value; |
174 | 174 |
|
... | ... |
@@ -2141,374 +2141,374 @@ |
2141 | 2141 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
2142 | 2142 |
_inv_map.pop_back(); |
2143 | 2143 |
} |
2144 | 2144 |
Map::erase(items); |
2145 | 2145 |
} |
2146 | 2146 |
|
2147 | 2147 |
/// \brief Build the unique map. |
2148 | 2148 |
/// |
2149 | 2149 |
/// Build the unique map. It is called by the |
2150 | 2150 |
/// \c AlterationNotifier. |
2151 | 2151 |
virtual void build() { |
2152 | 2152 |
Map::build(); |
2153 | 2153 |
Item it; |
2154 | 2154 |
const typename Map::Notifier* nf = Map::notifier(); |
2155 | 2155 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2156 | 2156 |
Map::set(it, _inv_map.size()); |
2157 | 2157 |
_inv_map.push_back(it); |
2158 | 2158 |
} |
2159 | 2159 |
} |
2160 | 2160 |
|
2161 | 2161 |
/// \brief Clear the keys from the map. |
2162 | 2162 |
/// |
2163 | 2163 |
/// Clear the keys from the map. It is called by the |
2164 | 2164 |
/// \c AlterationNotifier. |
2165 | 2165 |
virtual void clear() { |
2166 | 2166 |
_inv_map.clear(); |
2167 | 2167 |
Map::clear(); |
2168 | 2168 |
} |
2169 | 2169 |
|
2170 | 2170 |
public: |
2171 | 2171 |
|
2172 | 2172 |
/// \brief Returns the maximal value plus one. |
2173 | 2173 |
/// |
2174 | 2174 |
/// Returns the maximal value plus one in the map. |
2175 | 2175 |
unsigned int size() const { |
2176 | 2176 |
return _inv_map.size(); |
2177 | 2177 |
} |
2178 | 2178 |
|
2179 | 2179 |
/// \brief Swaps the position of the two items in the map. |
2180 | 2180 |
/// |
2181 | 2181 |
/// Swaps the position of the two items in the map. |
2182 | 2182 |
void swap(const Item& p, const Item& q) { |
2183 | 2183 |
int pi = Map::operator[](p); |
2184 | 2184 |
int qi = Map::operator[](q); |
2185 | 2185 |
Map::set(p, qi); |
2186 | 2186 |
_inv_map[qi] = p; |
2187 | 2187 |
Map::set(q, pi); |
2188 | 2188 |
_inv_map[pi] = q; |
2189 | 2189 |
} |
2190 | 2190 |
|
2191 | 2191 |
/// \brief Gives back the \e descriptor of the item. |
2192 | 2192 |
/// |
2193 | 2193 |
/// Gives back the mutable and unique \e descriptor of the map. |
2194 | 2194 |
int operator[](const Item& item) const { |
2195 | 2195 |
return Map::operator[](item); |
2196 | 2196 |
} |
2197 | 2197 |
|
2198 | 2198 |
/// \brief Gives back the item by its descriptor. |
2199 | 2199 |
/// |
2200 | 2200 |
/// Gives back th item by its descriptor. |
2201 | 2201 |
Item operator()(int id) const { |
2202 | 2202 |
return _inv_map[id]; |
2203 | 2203 |
} |
2204 | 2204 |
|
2205 | 2205 |
private: |
2206 | 2206 |
|
2207 | 2207 |
typedef std::vector<Item> Container; |
2208 | 2208 |
Container _inv_map; |
2209 | 2209 |
|
2210 | 2210 |
public: |
2211 | 2211 |
/// \brief The inverse map type of DescriptorMap. |
2212 | 2212 |
/// |
2213 | 2213 |
/// The inverse map type of DescriptorMap. |
2214 | 2214 |
class InverseMap { |
2215 | 2215 |
public: |
2216 | 2216 |
/// \brief Constructor of the InverseMap. |
2217 | 2217 |
/// |
2218 | 2218 |
/// Constructor of the InverseMap. |
2219 | 2219 |
explicit InverseMap(const DescriptorMap& inverted) |
2220 | 2220 |
: _inverted(inverted) {} |
2221 | 2221 |
|
2222 | 2222 |
|
2223 | 2223 |
/// The value type of the InverseMap. |
2224 | 2224 |
typedef typename DescriptorMap::Key Value; |
2225 | 2225 |
/// The key type of the InverseMap. |
2226 | 2226 |
typedef typename DescriptorMap::Value Key; |
2227 | 2227 |
|
2228 | 2228 |
/// \brief Subscript operator. |
2229 | 2229 |
/// |
2230 | 2230 |
/// Subscript operator. It gives back the item |
2231 | 2231 |
/// that the descriptor belongs to currently. |
2232 | 2232 |
Value operator[](const Key& key) const { |
2233 | 2233 |
return _inverted(key); |
2234 | 2234 |
} |
2235 | 2235 |
|
2236 | 2236 |
/// \brief Size of the map. |
2237 | 2237 |
/// |
2238 | 2238 |
/// Returns the size of the map. |
2239 | 2239 |
unsigned int size() const { |
2240 | 2240 |
return _inverted.size(); |
2241 | 2241 |
} |
2242 | 2242 |
|
2243 | 2243 |
private: |
2244 | 2244 |
const DescriptorMap& _inverted; |
2245 | 2245 |
}; |
2246 | 2246 |
|
2247 | 2247 |
/// \brief Gives back the inverse of the map. |
2248 | 2248 |
/// |
2249 | 2249 |
/// Gives back the inverse of the map. |
2250 | 2250 |
const InverseMap inverse() const { |
2251 | 2251 |
return InverseMap(*this); |
2252 | 2252 |
} |
2253 | 2253 |
}; |
2254 | 2254 |
|
2255 | 2255 |
/// \brief Returns the source of the given arc. |
2256 | 2256 |
/// |
2257 | 2257 |
/// The SourceMap gives back the source Node of the given arc. |
2258 | 2258 |
/// \see TargetMap |
2259 | 2259 |
template <typename Digraph> |
2260 | 2260 |
class SourceMap { |
2261 | 2261 |
public: |
2262 | 2262 |
|
2263 | 2263 |
typedef typename Digraph::Node Value; |
2264 | 2264 |
typedef typename Digraph::Arc Key; |
2265 | 2265 |
|
2266 | 2266 |
/// \brief Constructor |
2267 | 2267 |
/// |
2268 | 2268 |
/// Constructor |
2269 |
/// \param |
|
2269 |
/// \param digraph The digraph that the map belongs to. |
|
2270 | 2270 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {} |
2271 | 2271 |
|
2272 | 2272 |
/// \brief The subscript operator. |
2273 | 2273 |
/// |
2274 | 2274 |
/// The subscript operator. |
2275 | 2275 |
/// \param arc The arc |
2276 | 2276 |
/// \return The source of the arc |
2277 | 2277 |
Value operator[](const Key& arc) const { |
2278 | 2278 |
return _digraph.source(arc); |
2279 | 2279 |
} |
2280 | 2280 |
|
2281 | 2281 |
private: |
2282 | 2282 |
const Digraph& _digraph; |
2283 | 2283 |
}; |
2284 | 2284 |
|
2285 | 2285 |
/// \brief Returns a \c SourceMap class. |
2286 | 2286 |
/// |
2287 | 2287 |
/// This function just returns an \c SourceMap class. |
2288 | 2288 |
/// \relates SourceMap |
2289 | 2289 |
template <typename Digraph> |
2290 | 2290 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) { |
2291 | 2291 |
return SourceMap<Digraph>(digraph); |
2292 | 2292 |
} |
2293 | 2293 |
|
2294 | 2294 |
/// \brief Returns the target of the given arc. |
2295 | 2295 |
/// |
2296 | 2296 |
/// The TargetMap gives back the target Node of the given arc. |
2297 | 2297 |
/// \see SourceMap |
2298 | 2298 |
template <typename Digraph> |
2299 | 2299 |
class TargetMap { |
2300 | 2300 |
public: |
2301 | 2301 |
|
2302 | 2302 |
typedef typename Digraph::Node Value; |
2303 | 2303 |
typedef typename Digraph::Arc Key; |
2304 | 2304 |
|
2305 | 2305 |
/// \brief Constructor |
2306 | 2306 |
/// |
2307 | 2307 |
/// Constructor |
2308 |
/// \param |
|
2308 |
/// \param digraph The digraph that the map belongs to. |
|
2309 | 2309 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {} |
2310 | 2310 |
|
2311 | 2311 |
/// \brief The subscript operator. |
2312 | 2312 |
/// |
2313 | 2313 |
/// The subscript operator. |
2314 | 2314 |
/// \param e The arc |
2315 | 2315 |
/// \return The target of the arc |
2316 | 2316 |
Value operator[](const Key& e) const { |
2317 | 2317 |
return _digraph.target(e); |
2318 | 2318 |
} |
2319 | 2319 |
|
2320 | 2320 |
private: |
2321 | 2321 |
const Digraph& _digraph; |
2322 | 2322 |
}; |
2323 | 2323 |
|
2324 | 2324 |
/// \brief Returns a \c TargetMap class. |
2325 | 2325 |
/// |
2326 | 2326 |
/// This function just returns a \c TargetMap class. |
2327 | 2327 |
/// \relates TargetMap |
2328 | 2328 |
template <typename Digraph> |
2329 | 2329 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) { |
2330 | 2330 |
return TargetMap<Digraph>(digraph); |
2331 | 2331 |
} |
2332 | 2332 |
|
2333 | 2333 |
/// \brief Returns the "forward" directed arc view of an edge. |
2334 | 2334 |
/// |
2335 | 2335 |
/// Returns the "forward" directed arc view of an edge. |
2336 | 2336 |
/// \see BackwardMap |
2337 | 2337 |
template <typename Graph> |
2338 | 2338 |
class ForwardMap { |
2339 | 2339 |
public: |
2340 | 2340 |
|
2341 | 2341 |
typedef typename Graph::Arc Value; |
2342 | 2342 |
typedef typename Graph::Edge Key; |
2343 | 2343 |
|
2344 | 2344 |
/// \brief Constructor |
2345 | 2345 |
/// |
2346 | 2346 |
/// Constructor |
2347 |
/// \param |
|
2347 |
/// \param graph The graph that the map belongs to. |
|
2348 | 2348 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {} |
2349 | 2349 |
|
2350 | 2350 |
/// \brief The subscript operator. |
2351 | 2351 |
/// |
2352 | 2352 |
/// The subscript operator. |
2353 | 2353 |
/// \param key An edge |
2354 | 2354 |
/// \return The "forward" directed arc view of edge |
2355 | 2355 |
Value operator[](const Key& key) const { |
2356 | 2356 |
return _graph.direct(key, true); |
2357 | 2357 |
} |
2358 | 2358 |
|
2359 | 2359 |
private: |
2360 | 2360 |
const Graph& _graph; |
2361 | 2361 |
}; |
2362 | 2362 |
|
2363 | 2363 |
/// \brief Returns a \c ForwardMap class. |
2364 | 2364 |
/// |
2365 | 2365 |
/// This function just returns an \c ForwardMap class. |
2366 | 2366 |
/// \relates ForwardMap |
2367 | 2367 |
template <typename Graph> |
2368 | 2368 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
2369 | 2369 |
return ForwardMap<Graph>(graph); |
2370 | 2370 |
} |
2371 | 2371 |
|
2372 | 2372 |
/// \brief Returns the "backward" directed arc view of an edge. |
2373 | 2373 |
/// |
2374 | 2374 |
/// Returns the "backward" directed arc view of an edge. |
2375 | 2375 |
/// \see ForwardMap |
2376 | 2376 |
template <typename Graph> |
2377 | 2377 |
class BackwardMap { |
2378 | 2378 |
public: |
2379 | 2379 |
|
2380 | 2380 |
typedef typename Graph::Arc Value; |
2381 | 2381 |
typedef typename Graph::Edge Key; |
2382 | 2382 |
|
2383 | 2383 |
/// \brief Constructor |
2384 | 2384 |
/// |
2385 | 2385 |
/// Constructor |
2386 |
/// \param |
|
2386 |
/// \param graph The graph that the map belongs to. |
|
2387 | 2387 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {} |
2388 | 2388 |
|
2389 | 2389 |
/// \brief The subscript operator. |
2390 | 2390 |
/// |
2391 | 2391 |
/// The subscript operator. |
2392 | 2392 |
/// \param key An edge |
2393 | 2393 |
/// \return The "backward" directed arc view of edge |
2394 | 2394 |
Value operator[](const Key& key) const { |
2395 | 2395 |
return _graph.direct(key, false); |
2396 | 2396 |
} |
2397 | 2397 |
|
2398 | 2398 |
private: |
2399 | 2399 |
const Graph& _graph; |
2400 | 2400 |
}; |
2401 | 2401 |
|
2402 | 2402 |
/// \brief Returns a \c BackwardMap class |
2403 | 2403 |
|
2404 | 2404 |
/// This function just returns a \c BackwardMap class. |
2405 | 2405 |
/// \relates BackwardMap |
2406 | 2406 |
template <typename Graph> |
2407 | 2407 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
2408 | 2408 |
return BackwardMap<Graph>(graph); |
2409 | 2409 |
} |
2410 | 2410 |
|
2411 | 2411 |
/// \brief Potential difference map |
2412 | 2412 |
/// |
2413 | 2413 |
/// If there is an potential map on the nodes then we |
2414 | 2414 |
/// can get an arc map as we get the substraction of the |
2415 | 2415 |
/// values of the target and source. |
2416 | 2416 |
template <typename Digraph, typename NodeMap> |
2417 | 2417 |
class PotentialDifferenceMap { |
2418 | 2418 |
public: |
2419 | 2419 |
typedef typename Digraph::Arc Key; |
2420 | 2420 |
typedef typename NodeMap::Value Value; |
2421 | 2421 |
|
2422 | 2422 |
/// \brief Constructor |
2423 | 2423 |
/// |
2424 | 2424 |
/// Contructor of the map |
2425 | 2425 |
explicit PotentialDifferenceMap(const Digraph& digraph, |
2426 | 2426 |
const NodeMap& potential) |
2427 | 2427 |
: _digraph(digraph), _potential(potential) {} |
2428 | 2428 |
|
2429 | 2429 |
/// \brief Const subscription operator |
2430 | 2430 |
/// |
2431 | 2431 |
/// Const subscription operator |
2432 | 2432 |
Value operator[](const Key& arc) const { |
2433 | 2433 |
return _potential[_digraph.target(arc)] - |
2434 | 2434 |
_potential[_digraph.source(arc)]; |
2435 | 2435 |
} |
2436 | 2436 |
|
2437 | 2437 |
private: |
2438 | 2438 |
const Digraph& _digraph; |
2439 | 2439 |
const NodeMap& _potential; |
2440 | 2440 |
}; |
2441 | 2441 |
|
2442 | 2442 |
/// \brief Returns a PotentialDifferenceMap. |
2443 | 2443 |
/// |
2444 | 2444 |
/// This function just returns a PotentialDifferenceMap. |
2445 | 2445 |
/// \relates PotentialDifferenceMap |
2446 | 2446 |
template <typename Digraph, typename NodeMap> |
2447 | 2447 |
PotentialDifferenceMap<Digraph, NodeMap> |
2448 | 2448 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) { |
2449 | 2449 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential); |
2450 | 2450 |
} |
2451 | 2451 |
|
2452 | 2452 |
/// \brief Map of the node in-degrees. |
2453 | 2453 |
/// |
2454 | 2454 |
/// This map returns the in-degree of a node. Once it is constructed, |
2455 | 2455 |
/// the degrees are stored in a standard NodeMap, so each query is done |
2456 | 2456 |
/// in constant time. On the other hand, the values are updated automatically |
2457 | 2457 |
/// whenever the digraph changes. |
2458 | 2458 |
/// |
2459 | 2459 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
2460 | 2460 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap |
2461 | 2461 |
/// is not guarantied if these additional features are used. For example |
2462 | 2462 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
2463 | 2463 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
2464 | 2464 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
2465 | 2465 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
2466 | 2466 |
/// |
2467 | 2467 |
/// \sa OutDegMap |
2468 | 2468 |
|
2469 | 2469 |
template <typename _Digraph> |
2470 | 2470 |
class InDegMap |
2471 | 2471 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
2472 | 2472 |
::ItemNotifier::ObserverBase { |
2473 | 2473 |
|
2474 | 2474 |
public: |
2475 | 2475 |
|
2476 | 2476 |
typedef _Digraph Digraph; |
2477 | 2477 |
typedef int Value; |
2478 | 2478 |
typedef typename Digraph::Node Key; |
2479 | 2479 |
|
2480 | 2480 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
2481 | 2481 |
::ItemNotifier::ObserverBase Parent; |
2482 | 2482 |
|
2483 | 2483 |
private: |
2484 | 2484 |
|
2485 | 2485 |
class AutoNodeMap |
2486 | 2486 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
2487 | 2487 |
public: |
2488 | 2488 |
|
2489 | 2489 |
typedef typename ItemSetTraits<Digraph, Key>:: |
2490 | 2490 |
template Map<int>::Type Parent; |
2491 | 2491 |
|
2492 | 2492 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
2493 | 2493 |
|
2494 | 2494 |
virtual void add(const Key& key) { |
2495 | 2495 |
Parent::add(key); |
2496 | 2496 |
Parent::set(key, 0); |
2497 | 2497 |
} |
2498 | 2498 |
|
2499 | 2499 |
virtual void add(const std::vector<Key>& keys) { |
2500 | 2500 |
Parent::add(keys); |
2501 | 2501 |
for (int i = 0; i < int(keys.size()); ++i) { |
2502 | 2502 |
Parent::set(keys[i], 0); |
2503 | 2503 |
} |
2504 | 2504 |
} |
2505 | 2505 |
|
2506 | 2506 |
virtual void build() { |
2507 | 2507 |
Parent::build(); |
2508 | 2508 |
Key it; |
2509 | 2509 |
typename Parent::Notifier* nf = Parent::notifier(); |
2510 | 2510 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2511 | 2511 |
Parent::set(it, 0); |
2512 | 2512 |
} |
2513 | 2513 |
} |
2514 | 2514 |
}; |
... | ... |
@@ -724,257 +724,257 @@ |
724 | 724 |
void buildRev(const CPath& path) { |
725 | 725 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
726 | 726 |
addFront(it); |
727 | 727 |
} |
728 | 728 |
} |
729 | 729 |
|
730 | 730 |
}; |
731 | 731 |
|
732 | 732 |
/// \brief A structure for representing directed paths in a digraph. |
733 | 733 |
/// |
734 | 734 |
/// A structure for representing directed path in a digraph. |
735 | 735 |
/// \tparam _Digraph The digraph type in which the path is. |
736 | 736 |
/// |
737 | 737 |
/// In a sense, the path can be treated as a list of arcs. The |
738 | 738 |
/// lemon path type stores just this list. As a consequence it |
739 | 739 |
/// cannot enumerate the nodes in the path and the source node of |
740 | 740 |
/// a zero length path is undefined. |
741 | 741 |
/// |
742 | 742 |
/// This implementation is completly static, i.e. it can be copy constucted |
743 | 743 |
/// or copy assigned from another path, but otherwise it cannot be |
744 | 744 |
/// modified. |
745 | 745 |
/// |
746 | 746 |
/// Being the the most memory efficient path type in LEMON, |
747 | 747 |
/// it is intented to be |
748 | 748 |
/// used when you want to store a large number of paths. |
749 | 749 |
template <typename _Digraph> |
750 | 750 |
class StaticPath { |
751 | 751 |
public: |
752 | 752 |
|
753 | 753 |
typedef _Digraph Digraph; |
754 | 754 |
typedef typename Digraph::Arc Arc; |
755 | 755 |
|
756 | 756 |
/// \brief Default constructor |
757 | 757 |
/// |
758 | 758 |
/// Default constructor |
759 | 759 |
StaticPath() : len(0), arcs(0) {} |
760 | 760 |
|
761 | 761 |
/// \brief Template copy constructor |
762 | 762 |
/// |
763 | 763 |
/// This path can be initialized from any other path type. |
764 | 764 |
template <typename CPath> |
765 | 765 |
StaticPath(const CPath& cpath) : arcs(0) { |
766 | 766 |
copyPath(*this, cpath); |
767 | 767 |
} |
768 | 768 |
|
769 | 769 |
/// \brief Destructor of the path |
770 | 770 |
/// |
771 | 771 |
/// Destructor of the path |
772 | 772 |
~StaticPath() { |
773 | 773 |
if (arcs) delete[] arcs; |
774 | 774 |
} |
775 | 775 |
|
776 | 776 |
/// \brief Template copy assignment |
777 | 777 |
/// |
778 | 778 |
/// This path can be made equal to any other path type. It simply |
779 | 779 |
/// makes a copy of the given path. |
780 | 780 |
template <typename CPath> |
781 | 781 |
StaticPath& operator=(const CPath& cpath) { |
782 | 782 |
copyPath(*this, cpath); |
783 | 783 |
return *this; |
784 | 784 |
} |
785 | 785 |
|
786 | 786 |
/// \brief Iterator class to iterate on the arcs of the paths |
787 | 787 |
/// |
788 | 788 |
/// This class is used to iterate on the arcs of the paths |
789 | 789 |
/// |
790 | 790 |
/// Of course it converts to Digraph::Arc |
791 | 791 |
class ArcIt { |
792 | 792 |
friend class StaticPath; |
793 | 793 |
public: |
794 | 794 |
/// Default constructor |
795 | 795 |
ArcIt() {} |
796 | 796 |
/// Invalid constructor |
797 | 797 |
ArcIt(Invalid) : path(0), idx(-1) {} |
798 | 798 |
/// Initializate the constructor to the first arc of path |
799 | 799 |
ArcIt(const StaticPath &_path) |
800 | 800 |
: path(&_path), idx(_path.empty() ? -1 : 0) {} |
801 | 801 |
|
802 | 802 |
private: |
803 | 803 |
|
804 | 804 |
/// Constructor with starting point |
805 | 805 |
ArcIt(const StaticPath &_path, int _idx) |
806 | 806 |
: idx(_idx), path(&_path) {} |
807 | 807 |
|
808 | 808 |
public: |
809 | 809 |
|
810 | 810 |
///Conversion to Digraph::Arc |
811 | 811 |
operator const Arc&() const { |
812 | 812 |
return path->nth(idx); |
813 | 813 |
} |
814 | 814 |
|
815 | 815 |
/// Next arc |
816 | 816 |
ArcIt& operator++() { |
817 | 817 |
++idx; |
818 | 818 |
if (idx >= path->length()) idx = -1; |
819 | 819 |
return *this; |
820 | 820 |
} |
821 | 821 |
|
822 | 822 |
/// Comparison operator |
823 | 823 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
824 | 824 |
/// Comparison operator |
825 | 825 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
826 | 826 |
/// Comparison operator |
827 | 827 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
828 | 828 |
|
829 | 829 |
private: |
830 | 830 |
const StaticPath *path; |
831 | 831 |
int idx; |
832 | 832 |
}; |
833 | 833 |
|
834 | 834 |
/// \brief The nth arc. |
835 | 835 |
/// |
836 | 836 |
/// \pre n is in the [0..length() - 1] range |
837 | 837 |
const Arc& nth(int n) const { |
838 | 838 |
return arcs[n]; |
839 | 839 |
} |
840 | 840 |
|
841 | 841 |
/// \brief The arc iterator pointing to the nth arc. |
842 | 842 |
ArcIt nthIt(int n) const { |
843 | 843 |
return ArcIt(*this, n); |
844 | 844 |
} |
845 | 845 |
|
846 | 846 |
/// \brief The length of the path. |
847 | 847 |
int length() const { return len; } |
848 | 848 |
|
849 | 849 |
/// \brief Return true when the path is empty. |
850 | 850 |
int empty() const { return len == 0; } |
851 | 851 |
|
852 |
/// \ |
|
852 |
/// \brief Erase all arcs in the digraph. |
|
853 | 853 |
void clear() { |
854 | 854 |
len = 0; |
855 | 855 |
if (arcs) delete[] arcs; |
856 | 856 |
arcs = 0; |
857 | 857 |
} |
858 | 858 |
|
859 | 859 |
/// \brief The first arc of the path. |
860 | 860 |
const Arc& front() const { |
861 | 861 |
return arcs[0]; |
862 | 862 |
} |
863 | 863 |
|
864 | 864 |
/// \brief The last arc of the path. |
865 | 865 |
const Arc& back() const { |
866 | 866 |
return arcs[len - 1]; |
867 | 867 |
} |
868 | 868 |
|
869 | 869 |
|
870 | 870 |
typedef True BuildTag; |
871 | 871 |
|
872 | 872 |
template <typename CPath> |
873 | 873 |
void build(const CPath& path) { |
874 | 874 |
len = path.length(); |
875 | 875 |
arcs = new Arc[len]; |
876 | 876 |
int index = 0; |
877 | 877 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
878 | 878 |
arcs[index] = it; |
879 | 879 |
++index; |
880 | 880 |
} |
881 | 881 |
} |
882 | 882 |
|
883 | 883 |
template <typename CPath> |
884 | 884 |
void buildRev(const CPath& path) { |
885 | 885 |
len = path.length(); |
886 | 886 |
arcs = new Arc[len]; |
887 | 887 |
int index = len; |
888 | 888 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
889 | 889 |
--index; |
890 | 890 |
arcs[index] = it; |
891 | 891 |
} |
892 | 892 |
} |
893 | 893 |
|
894 | 894 |
private: |
895 | 895 |
int len; |
896 | 896 |
Arc* arcs; |
897 | 897 |
}; |
898 | 898 |
|
899 | 899 |
/////////////////////////////////////////////////////////////////////// |
900 | 900 |
// Additional utilities |
901 | 901 |
/////////////////////////////////////////////////////////////////////// |
902 | 902 |
|
903 | 903 |
namespace _path_bits { |
904 | 904 |
|
905 | 905 |
template <typename Path, typename Enable = void> |
906 | 906 |
struct RevPathTagIndicator { |
907 | 907 |
static const bool value = false; |
908 | 908 |
}; |
909 | 909 |
|
910 | 910 |
template <typename Path> |
911 | 911 |
struct RevPathTagIndicator< |
912 | 912 |
Path, |
913 | 913 |
typename enable_if<typename Path::RevPathTag, void>::type |
914 | 914 |
> { |
915 | 915 |
static const bool value = true; |
916 | 916 |
}; |
917 | 917 |
|
918 | 918 |
template <typename Path, typename Enable = void> |
919 | 919 |
struct BuildTagIndicator { |
920 | 920 |
static const bool value = false; |
921 | 921 |
}; |
922 | 922 |
|
923 | 923 |
template <typename Path> |
924 | 924 |
struct BuildTagIndicator< |
925 | 925 |
Path, |
926 | 926 |
typename enable_if<typename Path::BuildTag, void>::type |
927 | 927 |
> { |
928 | 928 |
static const bool value = true; |
929 | 929 |
}; |
930 | 930 |
|
931 | 931 |
template <typename Target, typename Source, |
932 | 932 |
bool buildEnable = BuildTagIndicator<Target>::value, |
933 | 933 |
bool revEnable = RevPathTagIndicator<Source>::value> |
934 | 934 |
struct PathCopySelector { |
935 | 935 |
static void copy(Target& target, const Source& source) { |
936 | 936 |
target.clear(); |
937 | 937 |
for (typename Source::ArcIt it(source); it != INVALID; ++it) { |
938 | 938 |
target.addBack(it); |
939 | 939 |
} |
940 | 940 |
} |
941 | 941 |
}; |
942 | 942 |
|
943 | 943 |
template <typename Target, typename Source> |
944 | 944 |
struct PathCopySelector<Target, Source, false, true> { |
945 | 945 |
static void copy(Target& target, const Source& source) { |
946 | 946 |
target.clear(); |
947 | 947 |
for (typename Source::RevArcIt it(source); it != INVALID; ++it) { |
948 | 948 |
target.addFront(it); |
949 | 949 |
} |
950 | 950 |
} |
951 | 951 |
}; |
952 | 952 |
|
953 | 953 |
template <typename Target, typename Source> |
954 | 954 |
struct PathCopySelector<Target, Source, true, false> { |
955 | 955 |
static void copy(Target& target, const Source& source) { |
956 | 956 |
target.clear(); |
957 | 957 |
target.build(source); |
958 | 958 |
} |
959 | 959 |
}; |
960 | 960 |
|
961 | 961 |
template <typename Target, typename Source> |
962 | 962 |
struct PathCopySelector<Target, Source, true, true> { |
963 | 963 |
static void copy(Target& target, const Source& source) { |
964 | 964 |
target.clear(); |
965 | 965 |
target.buildRev(source); |
966 | 966 |
} |
967 | 967 |
}; |
968 | 968 |
|
969 | 969 |
} |
970 | 970 |
|
971 | 971 |
|
972 | 972 |
/// \brief Make a copy of a path. |
973 | 973 |
/// |
974 | 974 |
/// This function makes a copy of a path. |
975 | 975 |
template <typename Target, typename Source> |
976 | 976 |
void copyPath(Target& target, const Source& source) { |
977 | 977 |
checkConcept<concepts::PathDumper<typename Source::Digraph>, Source>(); |
978 | 978 |
_path_bits::PathCopySelector<Target, Source>::copy(target, source); |
979 | 979 |
} |
980 | 980 |
... | ... |
@@ -240,269 +240,269 @@ |
240 | 240 |
|
241 | 241 |
/// \brief Using this it is possible to avoid the superfluous memory |
242 | 242 |
/// allocation. |
243 | 243 |
|
244 | 244 |
/// Using this it is possible to avoid the superfluous memory |
245 | 245 |
/// allocation: if you know that the digraph you want to build will |
246 | 246 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
247 | 247 |
/// then it is worth reserving space for this amount before starting |
248 | 248 |
/// to build the digraph. |
249 | 249 |
/// \sa reserveArc |
250 | 250 |
void reserveNode(int n) { nodes.reserve(n); }; |
251 | 251 |
|
252 | 252 |
/// \brief Using this it is possible to avoid the superfluous memory |
253 | 253 |
/// allocation. |
254 | 254 |
|
255 | 255 |
/// Using this it is possible to avoid the superfluous memory |
256 | 256 |
/// allocation: if you know that the digraph you want to build will |
257 | 257 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
258 | 258 |
/// then it is worth reserving space for this amount before starting |
259 | 259 |
/// to build the digraph. |
260 | 260 |
/// \sa reserveNode |
261 | 261 |
void reserveArc(int m) { arcs.reserve(m); }; |
262 | 262 |
|
263 | 263 |
/// \brief Node validity check |
264 | 264 |
/// |
265 | 265 |
/// This function gives back true if the given node is valid, |
266 | 266 |
/// ie. it is a real node of the graph. |
267 | 267 |
/// |
268 | 268 |
/// \warning A removed node (using Snapshot) could become valid again |
269 | 269 |
/// when new nodes are added to the graph. |
270 | 270 |
bool valid(Node n) const { return Parent::valid(n); } |
271 | 271 |
|
272 | 272 |
/// \brief Arc validity check |
273 | 273 |
/// |
274 | 274 |
/// This function gives back true if the given arc is valid, |
275 | 275 |
/// ie. it is a real arc of the graph. |
276 | 276 |
/// |
277 | 277 |
/// \warning A removed arc (using Snapshot) could become valid again |
278 | 278 |
/// when new arcs are added to the graph. |
279 | 279 |
bool valid(Arc a) const { return Parent::valid(a); } |
280 | 280 |
|
281 | 281 |
///Clear the digraph. |
282 | 282 |
|
283 | 283 |
///Erase all the nodes and arcs from the digraph. |
284 | 284 |
/// |
285 | 285 |
void clear() { |
286 | 286 |
Parent::clear(); |
287 | 287 |
} |
288 | 288 |
|
289 | 289 |
///Split a node. |
290 | 290 |
|
291 | 291 |
///This function splits a node. First a new node is added to the digraph, |
292 | 292 |
///then the source of each outgoing arc of \c n is moved to this new node. |
293 | 293 |
///If \c connect is \c true (this is the default value), then a new arc |
294 | 294 |
///from \c n to the newly created node is also added. |
295 | 295 |
///\return The newly created node. |
296 | 296 |
/// |
297 | 297 |
///\note The <tt>Arc</tt>s |
298 | 298 |
///referencing a moved arc remain |
299 | 299 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
300 | 300 |
///may be invalidated. |
301 | 301 |
///\warning This functionality cannot be used together with the Snapshot |
302 | 302 |
///feature. |
303 | 303 |
Node split(Node n, bool connect = true) |
304 | 304 |
{ |
305 | 305 |
Node b = addNode(); |
306 | 306 |
nodes[b._id].first_out=nodes[n._id].first_out; |
307 | 307 |
nodes[n._id].first_out=-1; |
308 | 308 |
for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id; |
309 | 309 |
if(connect) addArc(n,b); |
310 | 310 |
return b; |
311 | 311 |
} |
312 | 312 |
|
313 | 313 |
public: |
314 | 314 |
|
315 | 315 |
class Snapshot; |
316 | 316 |
|
317 | 317 |
protected: |
318 | 318 |
|
319 | 319 |
void restoreSnapshot(const Snapshot &s) |
320 | 320 |
{ |
321 | 321 |
while(s.arc_num<arcs.size()) { |
322 | 322 |
Arc arc = arcFromId(arcs.size()-1); |
323 | 323 |
Parent::notifier(Arc()).erase(arc); |
324 | 324 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
325 | 325 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
326 | 326 |
arcs.pop_back(); |
327 | 327 |
} |
328 | 328 |
while(s.node_num<nodes.size()) { |
329 | 329 |
Node node = nodeFromId(nodes.size()-1); |
330 | 330 |
Parent::notifier(Node()).erase(node); |
331 | 331 |
nodes.pop_back(); |
332 | 332 |
} |
333 | 333 |
} |
334 | 334 |
|
335 | 335 |
public: |
336 | 336 |
|
337 | 337 |
///Class to make a snapshot of the digraph and to restrore to it later. |
338 | 338 |
|
339 | 339 |
///Class to make a snapshot of the digraph and to restrore to it later. |
340 | 340 |
/// |
341 | 341 |
///The newly added nodes and arcs can be removed using the |
342 | 342 |
///restore() function. |
343 | 343 |
///\note After you restore a state, you cannot restore |
344 | 344 |
///a later state, in other word you cannot add again the arcs deleted |
345 | 345 |
///by restore() using another one Snapshot instance. |
346 | 346 |
/// |
347 | 347 |
///\warning If you do not use correctly the snapshot that can cause |
348 | 348 |
///either broken program, invalid state of the digraph, valid but |
349 | 349 |
///not the restored digraph or no change. Because the runtime performance |
350 | 350 |
///the validity of the snapshot is not stored. |
351 | 351 |
class Snapshot |
352 | 352 |
{ |
353 | 353 |
SmartDigraph *_graph; |
354 | 354 |
protected: |
355 | 355 |
friend class SmartDigraph; |
356 | 356 |
unsigned int node_num; |
357 | 357 |
unsigned int arc_num; |
358 | 358 |
public: |
359 | 359 |
///Default constructor. |
360 | 360 |
|
361 | 361 |
///Default constructor. |
362 | 362 |
///To actually make a snapshot you must call save(). |
363 | 363 |
/// |
364 | 364 |
Snapshot() : _graph(0) {} |
365 | 365 |
///Constructor that immediately makes a snapshot |
366 | 366 |
|
367 | 367 |
///This constructor immediately makes a snapshot of the digraph. |
368 |
///\param |
|
368 |
///\param graph The digraph we make a snapshot of. |
|
369 | 369 |
Snapshot(SmartDigraph &graph) : _graph(&graph) { |
370 | 370 |
node_num=_graph->nodes.size(); |
371 | 371 |
arc_num=_graph->arcs.size(); |
372 | 372 |
} |
373 | 373 |
|
374 | 374 |
///Make a snapshot. |
375 | 375 |
|
376 | 376 |
///Make a snapshot of the digraph. |
377 | 377 |
/// |
378 | 378 |
///This function can be called more than once. In case of a repeated |
379 | 379 |
///call, the previous snapshot gets lost. |
380 |
///\param |
|
380 |
///\param graph The digraph we make the snapshot of. |
|
381 | 381 |
void save(SmartDigraph &graph) |
382 | 382 |
{ |
383 | 383 |
_graph=&graph; |
384 | 384 |
node_num=_graph->nodes.size(); |
385 | 385 |
arc_num=_graph->arcs.size(); |
386 | 386 |
} |
387 | 387 |
|
388 | 388 |
///Undo the changes until a snapshot. |
389 | 389 |
|
390 | 390 |
///Undo the changes until a snapshot created by save(). |
391 | 391 |
/// |
392 | 392 |
///\note After you restored a state, you cannot restore |
393 | 393 |
///a later state, in other word you cannot add again the arcs deleted |
394 | 394 |
///by restore(). |
395 | 395 |
void restore() |
396 | 396 |
{ |
397 | 397 |
_graph->restoreSnapshot(*this); |
398 | 398 |
} |
399 | 399 |
}; |
400 | 400 |
}; |
401 | 401 |
|
402 | 402 |
|
403 | 403 |
class SmartGraphBase { |
404 | 404 |
|
405 | 405 |
protected: |
406 | 406 |
|
407 | 407 |
struct NodeT { |
408 | 408 |
int first_out; |
409 | 409 |
}; |
410 | 410 |
|
411 | 411 |
struct ArcT { |
412 | 412 |
int target; |
413 | 413 |
int next_out; |
414 | 414 |
}; |
415 | 415 |
|
416 | 416 |
std::vector<NodeT> nodes; |
417 | 417 |
std::vector<ArcT> arcs; |
418 | 418 |
|
419 | 419 |
int first_free_arc; |
420 | 420 |
|
421 | 421 |
public: |
422 | 422 |
|
423 | 423 |
typedef SmartGraphBase Digraph; |
424 | 424 |
|
425 | 425 |
class Node; |
426 | 426 |
class Arc; |
427 | 427 |
class Edge; |
428 | 428 |
|
429 | 429 |
class Node { |
430 | 430 |
friend class SmartGraphBase; |
431 | 431 |
protected: |
432 | 432 |
|
433 | 433 |
int _id; |
434 | 434 |
explicit Node(int id) { _id = id;} |
435 | 435 |
|
436 | 436 |
public: |
437 | 437 |
Node() {} |
438 | 438 |
Node (Invalid) { _id = -1; } |
439 | 439 |
bool operator==(const Node& node) const {return _id == node._id;} |
440 | 440 |
bool operator!=(const Node& node) const {return _id != node._id;} |
441 | 441 |
bool operator<(const Node& node) const {return _id < node._id;} |
442 | 442 |
}; |
443 | 443 |
|
444 | 444 |
class Edge { |
445 | 445 |
friend class SmartGraphBase; |
446 | 446 |
protected: |
447 | 447 |
|
448 | 448 |
int _id; |
449 | 449 |
explicit Edge(int id) { _id = id;} |
450 | 450 |
|
451 | 451 |
public: |
452 | 452 |
Edge() {} |
453 | 453 |
Edge (Invalid) { _id = -1; } |
454 | 454 |
bool operator==(const Edge& arc) const {return _id == arc._id;} |
455 | 455 |
bool operator!=(const Edge& arc) const {return _id != arc._id;} |
456 | 456 |
bool operator<(const Edge& arc) const {return _id < arc._id;} |
457 | 457 |
}; |
458 | 458 |
|
459 | 459 |
class Arc { |
460 | 460 |
friend class SmartGraphBase; |
461 | 461 |
protected: |
462 | 462 |
|
463 | 463 |
int _id; |
464 | 464 |
explicit Arc(int id) { _id = id;} |
465 | 465 |
|
466 | 466 |
public: |
467 | 467 |
operator Edge() const { |
468 | 468 |
return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
469 | 469 |
} |
470 | 470 |
|
471 | 471 |
Arc() {} |
472 | 472 |
Arc (Invalid) { _id = -1; } |
473 | 473 |
bool operator==(const Arc& arc) const {return _id == arc._id;} |
474 | 474 |
bool operator!=(const Arc& arc) const {return _id != arc._id;} |
475 | 475 |
bool operator<(const Arc& arc) const {return _id < arc._id;} |
476 | 476 |
}; |
477 | 477 |
|
478 | 478 |
|
479 | 479 |
|
480 | 480 |
SmartGraphBase() |
481 | 481 |
: nodes(), arcs() {} |
482 | 482 |
|
483 | 483 |
|
484 | 484 |
int maxNodeId() const { return nodes.size()-1; } |
485 | 485 |
int maxEdgeId() const { return arcs.size() / 2 - 1; } |
486 | 486 |
int maxArcId() const { return arcs.size()-1; } |
487 | 487 |
|
488 | 488 |
Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); } |
489 | 489 |
Node target(Arc e) const { return Node(arcs[e._id].target); } |
490 | 490 |
|
491 | 491 |
Node u(Edge e) const { return Node(arcs[2 * e._id].target); } |
492 | 492 |
Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); } |
493 | 493 |
|
494 | 494 |
static bool direction(Arc e) { |
495 | 495 |
return (e._id & 1) == 1; |
496 | 496 |
} |
497 | 497 |
|
498 | 498 |
static Arc direct(Edge e, bool d) { |
499 | 499 |
return Arc(e._id * 2 + (d ? 1 : 0)); |
500 | 500 |
} |
501 | 501 |
|
502 | 502 |
void first(Node& node) const { |
503 | 503 |
node._id = nodes.size() - 1; |
504 | 504 |
} |
505 | 505 |
|
506 | 506 |
void next(Node& node) const { |
507 | 507 |
--node._id; |
508 | 508 |
} |
... | ... |
@@ -650,163 +650,163 @@ |
650 | 650 |
typedef ExtendedSmartGraphBase Parent; |
651 | 651 |
|
652 | 652 |
/// Constructor |
653 | 653 |
|
654 | 654 |
/// Constructor. |
655 | 655 |
/// |
656 | 656 |
SmartGraph() {} |
657 | 657 |
|
658 | 658 |
///Add a new node to the graph. |
659 | 659 |
|
660 | 660 |
/// \return the new node. |
661 | 661 |
/// |
662 | 662 |
Node addNode() { return Parent::addNode(); } |
663 | 663 |
|
664 | 664 |
///Add a new edge to the graph. |
665 | 665 |
|
666 | 666 |
///Add a new edge to the graph with node \c s |
667 | 667 |
///and \c t. |
668 | 668 |
///\return the new edge. |
669 | 669 |
Edge addEdge(const Node& s, const Node& t) { |
670 | 670 |
return Parent::addEdge(s, t); |
671 | 671 |
} |
672 | 672 |
|
673 | 673 |
/// \brief Node validity check |
674 | 674 |
/// |
675 | 675 |
/// This function gives back true if the given node is valid, |
676 | 676 |
/// ie. it is a real node of the graph. |
677 | 677 |
/// |
678 | 678 |
/// \warning A removed node (using Snapshot) could become valid again |
679 | 679 |
/// when new nodes are added to the graph. |
680 | 680 |
bool valid(Node n) const { return Parent::valid(n); } |
681 | 681 |
|
682 | 682 |
/// \brief Arc validity check |
683 | 683 |
/// |
684 | 684 |
/// This function gives back true if the given arc is valid, |
685 | 685 |
/// ie. it is a real arc of the graph. |
686 | 686 |
/// |
687 | 687 |
/// \warning A removed arc (using Snapshot) could become valid again |
688 | 688 |
/// when new edges are added to the graph. |
689 | 689 |
bool valid(Arc a) const { return Parent::valid(a); } |
690 | 690 |
|
691 | 691 |
/// \brief Edge validity check |
692 | 692 |
/// |
693 | 693 |
/// This function gives back true if the given edge is valid, |
694 | 694 |
/// ie. it is a real edge of the graph. |
695 | 695 |
/// |
696 | 696 |
/// \warning A removed edge (using Snapshot) could become valid again |
697 | 697 |
/// when new edges are added to the graph. |
698 | 698 |
bool valid(Edge e) const { return Parent::valid(e); } |
699 | 699 |
|
700 | 700 |
///Clear the graph. |
701 | 701 |
|
702 | 702 |
///Erase all the nodes and edges from the graph. |
703 | 703 |
/// |
704 | 704 |
void clear() { |
705 | 705 |
Parent::clear(); |
706 | 706 |
} |
707 | 707 |
|
708 | 708 |
public: |
709 | 709 |
|
710 | 710 |
class Snapshot; |
711 | 711 |
|
712 | 712 |
protected: |
713 | 713 |
|
714 | 714 |
void saveSnapshot(Snapshot &s) |
715 | 715 |
{ |
716 | 716 |
s._graph = this; |
717 | 717 |
s.node_num = nodes.size(); |
718 | 718 |
s.arc_num = arcs.size(); |
719 | 719 |
} |
720 | 720 |
|
721 | 721 |
void restoreSnapshot(const Snapshot &s) |
722 | 722 |
{ |
723 | 723 |
while(s.arc_num<arcs.size()) { |
724 | 724 |
int n=arcs.size()-1; |
725 | 725 |
Edge arc=edgeFromId(n/2); |
726 | 726 |
Parent::notifier(Edge()).erase(arc); |
727 | 727 |
std::vector<Arc> dir; |
728 | 728 |
dir.push_back(arcFromId(n)); |
729 | 729 |
dir.push_back(arcFromId(n-1)); |
730 | 730 |
Parent::notifier(Arc()).erase(dir); |
731 | 731 |
nodes[arcs[n].target].first_out=arcs[n].next_out; |
732 | 732 |
nodes[arcs[n-1].target].first_out=arcs[n-1].next_out; |
733 | 733 |
arcs.pop_back(); |
734 | 734 |
arcs.pop_back(); |
735 | 735 |
} |
736 | 736 |
while(s.node_num<nodes.size()) { |
737 | 737 |
int n=nodes.size()-1; |
738 | 738 |
Node node = nodeFromId(n); |
739 | 739 |
Parent::notifier(Node()).erase(node); |
740 | 740 |
nodes.pop_back(); |
741 | 741 |
} |
742 | 742 |
} |
743 | 743 |
|
744 | 744 |
public: |
745 | 745 |
|
746 | 746 |
///Class to make a snapshot of the digraph and to restrore to it later. |
747 | 747 |
|
748 | 748 |
///Class to make a snapshot of the digraph and to restrore to it later. |
749 | 749 |
/// |
750 | 750 |
///The newly added nodes and arcs can be removed using the |
751 | 751 |
///restore() function. |
752 | 752 |
/// |
753 | 753 |
///\note After you restore a state, you cannot restore |
754 | 754 |
///a later state, in other word you cannot add again the arcs deleted |
755 | 755 |
///by restore() using another one Snapshot instance. |
756 | 756 |
/// |
757 | 757 |
///\warning If you do not use correctly the snapshot that can cause |
758 | 758 |
///either broken program, invalid state of the digraph, valid but |
759 | 759 |
///not the restored digraph or no change. Because the runtime performance |
760 | 760 |
///the validity of the snapshot is not stored. |
761 | 761 |
class Snapshot |
762 | 762 |
{ |
763 | 763 |
SmartGraph *_graph; |
764 | 764 |
protected: |
765 | 765 |
friend class SmartGraph; |
766 | 766 |
unsigned int node_num; |
767 | 767 |
unsigned int arc_num; |
768 | 768 |
public: |
769 | 769 |
///Default constructor. |
770 | 770 |
|
771 | 771 |
///Default constructor. |
772 | 772 |
///To actually make a snapshot you must call save(). |
773 | 773 |
/// |
774 | 774 |
Snapshot() : _graph(0) {} |
775 | 775 |
///Constructor that immediately makes a snapshot |
776 | 776 |
|
777 | 777 |
///This constructor immediately makes a snapshot of the digraph. |
778 |
///\param |
|
778 |
///\param graph The digraph we make a snapshot of. |
|
779 | 779 |
Snapshot(SmartGraph &graph) { |
780 | 780 |
graph.saveSnapshot(*this); |
781 | 781 |
} |
782 | 782 |
|
783 | 783 |
///Make a snapshot. |
784 | 784 |
|
785 | 785 |
///Make a snapshot of the graph. |
786 | 786 |
/// |
787 | 787 |
///This function can be called more than once. In case of a repeated |
788 | 788 |
///call, the previous snapshot gets lost. |
789 |
///\param |
|
789 |
///\param graph The digraph we make the snapshot of. |
|
790 | 790 |
void save(SmartGraph &graph) |
791 | 791 |
{ |
792 | 792 |
graph.saveSnapshot(*this); |
793 | 793 |
} |
794 | 794 |
|
795 | 795 |
///Undo the changes until a snapshot. |
796 | 796 |
|
797 | 797 |
///Undo the changes until a snapshot created by save(). |
798 | 798 |
/// |
799 | 799 |
///\note After you restored a state, you cannot restore |
800 | 800 |
///a later state, in other word you cannot add again the arcs deleted |
801 | 801 |
///by restore(). |
802 | 802 |
void restore() |
803 | 803 |
{ |
804 | 804 |
_graph->restoreSnapshot(*this); |
805 | 805 |
} |
806 | 806 |
}; |
807 | 807 |
}; |
808 | 808 |
|
809 | 809 |
} //namespace lemon |
810 | 810 |
|
811 | 811 |
|
812 | 812 |
#endif //LEMON_SMART_GRAPH_H |
... | ... |
@@ -186,389 +186,388 @@ |
186 | 186 |
} |
187 | 187 |
///The time ellapsed since the last call of stamp() |
188 | 188 |
TimeStamp ellapsed() const |
189 | 189 |
{ |
190 | 190 |
TimeStamp t(NULL); |
191 | 191 |
return t-*this; |
192 | 192 |
} |
193 | 193 |
|
194 | 194 |
friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
195 | 195 |
|
196 | 196 |
///Gives back the user time of the process |
197 | 197 |
double userTime() const |
198 | 198 |
{ |
199 | 199 |
return utime; |
200 | 200 |
} |
201 | 201 |
///Gives back the system time of the process |
202 | 202 |
double systemTime() const |
203 | 203 |
{ |
204 | 204 |
return stime; |
205 | 205 |
} |
206 | 206 |
///Gives back the user time of the process' children |
207 | 207 |
|
208 | 208 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
209 | 209 |
/// |
210 | 210 |
double cUserTime() const |
211 | 211 |
{ |
212 | 212 |
return cutime; |
213 | 213 |
} |
214 | 214 |
///Gives back the user time of the process' children |
215 | 215 |
|
216 | 216 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
217 | 217 |
/// |
218 | 218 |
double cSystemTime() const |
219 | 219 |
{ |
220 | 220 |
return cstime; |
221 | 221 |
} |
222 | 222 |
///Gives back the real time |
223 | 223 |
double realTime() const {return rtime;} |
224 | 224 |
}; |
225 | 225 |
|
226 | 226 |
TimeStamp operator*(double b,const TimeStamp &t) |
227 | 227 |
{ |
228 | 228 |
return t*b; |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
///Prints the time counters |
232 | 232 |
|
233 | 233 |
///Prints the time counters in the following form: |
234 | 234 |
/// |
235 | 235 |
/// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
236 | 236 |
/// |
237 | 237 |
/// where the values are the |
238 | 238 |
/// \li \c u: user cpu time, |
239 | 239 |
/// \li \c s: system cpu time, |
240 | 240 |
/// \li \c cu: user cpu time of children, |
241 | 241 |
/// \li \c cs: system cpu time of children, |
242 | 242 |
/// \li \c real: real time. |
243 | 243 |
/// \relates TimeStamp |
244 | 244 |
/// \note On <tt>WIN32</tt> platform the cummulative values are not |
245 | 245 |
/// calculated. |
246 | 246 |
inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
247 | 247 |
{ |
248 | 248 |
os << "u: " << t.userTime() << |
249 | 249 |
"s, s: " << t.systemTime() << |
250 | 250 |
"s, cu: " << t.cUserTime() << |
251 | 251 |
"s, cs: " << t.cSystemTime() << |
252 | 252 |
"s, real: " << t.realTime() << "s"; |
253 | 253 |
return os; |
254 | 254 |
} |
255 | 255 |
|
256 | 256 |
///Class for measuring the cpu time and real time usage of the process |
257 | 257 |
|
258 | 258 |
///Class for measuring the cpu time and real time usage of the process. |
259 | 259 |
///It is quite easy-to-use, here is a short example. |
260 | 260 |
///\code |
261 | 261 |
/// #include<lemon/time_measure.h> |
262 | 262 |
/// #include<iostream> |
263 | 263 |
/// |
264 | 264 |
/// int main() |
265 | 265 |
/// { |
266 | 266 |
/// |
267 | 267 |
/// ... |
268 | 268 |
/// |
269 | 269 |
/// Timer t; |
270 | 270 |
/// doSomething(); |
271 | 271 |
/// std::cout << t << '\n'; |
272 | 272 |
/// t.restart(); |
273 | 273 |
/// doSomethingElse(); |
274 | 274 |
/// std::cout << t << '\n'; |
275 | 275 |
/// |
276 | 276 |
/// ... |
277 | 277 |
/// |
278 | 278 |
/// } |
279 | 279 |
///\endcode |
280 | 280 |
/// |
281 | 281 |
///The \ref Timer can also be \ref stop() "stopped" and |
282 | 282 |
///\ref start() "started" again, so it is possible to compute collected |
283 | 283 |
///running times. |
284 | 284 |
/// |
285 | 285 |
///\warning Depending on the operation system and its actual configuration |
286 | 286 |
///the time counters have a certain (10ms on a typical Linux system) |
287 | 287 |
///granularity. |
288 | 288 |
///Therefore this tool is not appropriate to measure very short times. |
289 | 289 |
///Also, if you start and stop the timer very frequently, it could lead to |
290 | 290 |
///distorted results. |
291 | 291 |
/// |
292 | 292 |
///\note If you want to measure the running time of the execution of a certain |
293 | 293 |
///function, consider the usage of \ref TimeReport instead. |
294 | 294 |
/// |
295 | 295 |
///\sa TimeReport |
296 | 296 |
class Timer |
297 | 297 |
{ |
298 | 298 |
int _running; //Timer is running iff _running>0; (_running>=0 always holds) |
299 | 299 |
TimeStamp start_time; //This is the relativ start-time if the timer |
300 | 300 |
//is _running, the collected _running time otherwise. |
301 | 301 |
|
302 | 302 |
void _reset() {if(_running) start_time.stamp(); else start_time.reset();} |
303 | 303 |
|
304 | 304 |
public: |
305 | 305 |
///Constructor. |
306 | 306 |
|
307 | 307 |
///\param run indicates whether or not the timer starts immediately. |
308 | 308 |
/// |
309 | 309 |
Timer(bool run=true) :_running(run) {_reset();} |
310 | 310 |
|
311 | 311 |
///\name Control the state of the timer |
312 | 312 |
///Basically a Timer can be either running or stopped, |
313 | 313 |
///but it provides a bit finer control on the execution. |
314 |
///The \ref Timer also counts the number of \ref start() |
|
315 |
///executions, and is stops only after the same amount (or more) |
|
316 |
///\ref stop() "stop()"s. This can be useful e.g. to compute |
|
317 |
///the running time |
|
314 |
///The \ref lemon::Timer "Timer" also counts the number of |
|
315 |
///\ref lemon::Timer::start() "start()" executions, and it stops |
|
316 |
///only after the same amount (or more) \ref lemon::Timer::stop() |
|
317 |
///"stop()"s. This can be useful e.g. to compute the running time |
|
318 | 318 |
///of recursive functions. |
319 |
/// |
|
320 | 319 |
|
321 | 320 |
///@{ |
322 | 321 |
|
323 | 322 |
///Reset and stop the time counters |
324 | 323 |
|
325 | 324 |
///This function resets and stops the time counters |
326 | 325 |
///\sa restart() |
327 | 326 |
void reset() |
328 | 327 |
{ |
329 | 328 |
_running=0; |
330 | 329 |
_reset(); |
331 | 330 |
} |
332 | 331 |
|
333 | 332 |
///Start the time counters |
334 | 333 |
|
335 | 334 |
///This function starts the time counters. |
336 | 335 |
/// |
337 | 336 |
///If the timer is started more than ones, it will remain running |
338 | 337 |
///until the same amount of \ref stop() is called. |
339 | 338 |
///\sa stop() |
340 | 339 |
void start() |
341 | 340 |
{ |
342 | 341 |
if(_running) _running++; |
343 | 342 |
else { |
344 | 343 |
_running=1; |
345 | 344 |
TimeStamp t; |
346 | 345 |
t.stamp(); |
347 | 346 |
start_time=t-start_time; |
348 | 347 |
} |
349 | 348 |
} |
350 | 349 |
|
351 | 350 |
|
352 | 351 |
///Stop the time counters |
353 | 352 |
|
354 | 353 |
///This function stops the time counters. If start() was executed more than |
355 | 354 |
///once, then the same number of stop() execution is necessary the really |
356 | 355 |
///stop the timer. |
357 | 356 |
/// |
358 | 357 |
///\sa halt() |
359 | 358 |
///\sa start() |
360 | 359 |
///\sa restart() |
361 | 360 |
///\sa reset() |
362 | 361 |
|
363 | 362 |
void stop() |
364 | 363 |
{ |
365 | 364 |
if(_running && !--_running) { |
366 | 365 |
TimeStamp t; |
367 | 366 |
t.stamp(); |
368 | 367 |
start_time=t-start_time; |
369 | 368 |
} |
370 | 369 |
} |
371 | 370 |
|
372 | 371 |
///Halt (i.e stop immediately) the time counters |
373 | 372 |
|
374 | 373 |
///This function stops immediately the time counters, i.e. <tt>t.halt()</tt> |
375 | 374 |
///is a faster |
376 | 375 |
///equivalent of the following. |
377 | 376 |
///\code |
378 | 377 |
/// while(t.running()) t.stop() |
379 | 378 |
///\endcode |
380 | 379 |
/// |
381 | 380 |
/// |
382 | 381 |
///\sa stop() |
383 | 382 |
///\sa restart() |
384 | 383 |
///\sa reset() |
385 | 384 |
|
386 | 385 |
void halt() |
387 | 386 |
{ |
388 | 387 |
if(_running) { |
389 | 388 |
_running=0; |
390 | 389 |
TimeStamp t; |
391 | 390 |
t.stamp(); |
392 | 391 |
start_time=t-start_time; |
393 | 392 |
} |
394 | 393 |
} |
395 | 394 |
|
396 | 395 |
///Returns the running state of the timer |
397 | 396 |
|
398 | 397 |
///This function returns the number of stop() exections that is |
399 | 398 |
///necessary to really stop the timer. |
400 | 399 |
///For example the timer |
401 | 400 |
///is running if and only if the return value is \c true |
402 | 401 |
///(i.e. greater than |
403 | 402 |
///zero). |
404 | 403 |
int running() { return _running; } |
405 | 404 |
|
406 | 405 |
|
407 | 406 |
///Restart the time counters |
408 | 407 |
|
409 | 408 |
///This function is a shorthand for |
410 | 409 |
///a reset() and a start() calls. |
411 | 410 |
/// |
412 | 411 |
void restart() |
413 | 412 |
{ |
414 | 413 |
reset(); |
415 | 414 |
start(); |
416 | 415 |
} |
417 | 416 |
|
418 | 417 |
///@} |
419 | 418 |
|
420 | 419 |
///\name Query Functions for the ellapsed time |
421 | 420 |
|
422 | 421 |
///@{ |
423 | 422 |
|
424 | 423 |
///Gives back the ellapsed user time of the process |
425 | 424 |
double userTime() const |
426 | 425 |
{ |
427 | 426 |
return operator TimeStamp().userTime(); |
428 | 427 |
} |
429 | 428 |
///Gives back the ellapsed system time of the process |
430 | 429 |
double systemTime() const |
431 | 430 |
{ |
432 | 431 |
return operator TimeStamp().systemTime(); |
433 | 432 |
} |
434 | 433 |
///Gives back the ellapsed user time of the process' children |
435 | 434 |
|
436 | 435 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
437 | 436 |
/// |
438 | 437 |
double cUserTime() const |
439 | 438 |
{ |
440 | 439 |
return operator TimeStamp().cUserTime(); |
441 | 440 |
} |
442 | 441 |
///Gives back the ellapsed user time of the process' children |
443 | 442 |
|
444 | 443 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
445 | 444 |
/// |
446 | 445 |
double cSystemTime() const |
447 | 446 |
{ |
448 | 447 |
return operator TimeStamp().cSystemTime(); |
449 | 448 |
} |
450 | 449 |
///Gives back the ellapsed real time |
451 | 450 |
double realTime() const |
452 | 451 |
{ |
453 | 452 |
return operator TimeStamp().realTime(); |
454 | 453 |
} |
455 | 454 |
///Computes the ellapsed time |
456 | 455 |
|
457 | 456 |
///This conversion computes the ellapsed time, therefore you can print |
458 | 457 |
///the ellapsed time like this. |
459 | 458 |
///\code |
460 | 459 |
/// Timer t; |
461 | 460 |
/// doSomething(); |
462 | 461 |
/// std::cout << t << '\n'; |
463 | 462 |
///\endcode |
464 | 463 |
operator TimeStamp () const |
465 | 464 |
{ |
466 | 465 |
TimeStamp t; |
467 | 466 |
t.stamp(); |
468 | 467 |
return _running?t-start_time:start_time; |
469 | 468 |
} |
470 | 469 |
|
471 | 470 |
|
472 | 471 |
///@} |
473 | 472 |
}; |
474 | 473 |
|
475 |
///Same as |
|
474 |
///Same as Timer but prints a report on destruction. |
|
476 | 475 |
|
477 | 476 |
///Same as \ref Timer but prints a report on destruction. |
478 | 477 |
///This example shows its usage. |
479 | 478 |
///\code |
480 | 479 |
/// void myAlg(ListGraph &g,int n) |
481 | 480 |
/// { |
482 | 481 |
/// TimeReport tr("Running time of myAlg: "); |
483 | 482 |
/// ... //Here comes the algorithm |
484 | 483 |
/// } |
485 | 484 |
///\endcode |
486 | 485 |
/// |
487 | 486 |
///\sa Timer |
488 | 487 |
///\sa NoTimeReport |
489 | 488 |
class TimeReport : public Timer |
490 | 489 |
{ |
491 | 490 |
std::string _title; |
492 | 491 |
std::ostream &_os; |
493 | 492 |
public: |
494 |
/// |
|
493 |
///Constructor |
|
495 | 494 |
|
495 |
///Constructor. |
|
496 | 496 |
///\param title This text will be printed before the ellapsed time. |
497 | 497 |
///\param os The stream to print the report to. |
498 | 498 |
///\param run Sets whether the timer should start immediately. |
499 |
|
|
500 | 499 |
TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) |
501 | 500 |
: Timer(run), _title(title), _os(os){} |
502 |
/// |
|
501 |
///Destructor that prints the ellapsed time |
|
503 | 502 |
~TimeReport() |
504 | 503 |
{ |
505 | 504 |
_os << _title << *this << std::endl; |
506 | 505 |
} |
507 | 506 |
}; |
508 | 507 |
|
509 |
///'Do nothing' version of |
|
508 |
///'Do nothing' version of TimeReport |
|
510 | 509 |
|
511 | 510 |
///\sa TimeReport |
512 | 511 |
/// |
513 | 512 |
class NoTimeReport |
514 | 513 |
{ |
515 | 514 |
public: |
516 | 515 |
///\e |
517 | 516 |
NoTimeReport(std::string,std::ostream &,bool) {} |
518 | 517 |
///\e |
519 | 518 |
NoTimeReport(std::string,std::ostream &) {} |
520 | 519 |
///\e |
521 | 520 |
NoTimeReport(std::string) {} |
522 | 521 |
///\e Do nothing. |
523 | 522 |
~NoTimeReport() {} |
524 | 523 |
|
525 | 524 |
operator TimeStamp () const { return TimeStamp(); } |
526 | 525 |
void reset() {} |
527 | 526 |
void start() {} |
528 | 527 |
void stop() {} |
529 | 528 |
void halt() {} |
530 | 529 |
int running() { return 0; } |
531 | 530 |
void restart() {} |
532 | 531 |
double userTime() const { return 0; } |
533 | 532 |
double systemTime() const { return 0; } |
534 | 533 |
double cUserTime() const { return 0; } |
535 | 534 |
double cSystemTime() const { return 0; } |
536 | 535 |
double realTime() const { return 0; } |
537 | 536 |
}; |
538 | 537 |
|
539 | 538 |
///Tool to measure the running time more exactly. |
540 | 539 |
|
541 | 540 |
///This function calls \c f several times and returns the average |
542 | 541 |
///running time. The number of the executions will be choosen in such a way |
543 | 542 |
///that the full real running time will be roughly between \c min_time |
544 | 543 |
///and <tt>2*min_time</tt>. |
545 | 544 |
///\param f the function object to be measured. |
546 | 545 |
///\param min_time the minimum total running time. |
547 | 546 |
///\retval num if it is not \c NULL, then the actual |
548 | 547 |
/// number of execution of \c f will be written into <tt>*num</tt>. |
549 | 548 |
///\retval full_time if it is not \c NULL, then the actual |
550 | 549 |
/// total running time will be written into <tt>*full_time</tt>. |
551 | 550 |
///\return The average running time of \c f. |
552 | 551 |
|
553 | 552 |
template<class F> |
554 | 553 |
TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL, |
555 | 554 |
TimeStamp *full_time=NULL) |
556 | 555 |
{ |
557 | 556 |
TimeStamp full; |
558 | 557 |
unsigned int total=0; |
559 | 558 |
Timer t; |
560 | 559 |
for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) { |
561 | 560 |
for(;total<tn;total++) f(); |
562 | 561 |
full=t; |
563 | 562 |
} |
564 | 563 |
if(num) *num=total; |
565 | 564 |
if(full_time) *full_time=full; |
566 | 565 |
return full/total; |
567 | 566 |
} |
568 | 567 |
|
569 | 568 |
/// @} |
570 | 569 |
|
571 | 570 |
|
572 | 571 |
} //namespace lemon |
573 | 572 |
|
574 | 573 |
#endif //LEMON_TIME_MEASURE_H |
0 comments (0 inline)