1
38
2
1
1
4
14
35
48
1
1
46
46
13
13
46
46
34
34
19
35
48
68
46
68
130
124
1
1
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 |
* |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 |
* |
|
| 5 |
* Copyright (C) 2003-2008 |
|
| 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
| 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
| 8 |
* |
|
| 9 |
* Permission to use, modify and distribute this software is granted |
|
| 10 |
* provided that this copyright notice appears in all copies. For |
|
| 11 |
* precise terms see the accompanying LICENSE file. |
|
| 12 |
* |
|
| 13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
| 14 |
* express or implied, and with no claim as to its suitability for any |
|
| 15 |
* purpose. |
|
| 16 |
* |
|
| 17 |
*/ |
|
| 18 |
|
|
| 19 |
namespace lemon {
|
|
| 20 |
/*! |
|
| 21 |
|
|
| 22 |
\page migration Migration from the 0.x Series |
|
| 23 |
|
|
| 24 |
This guide gives an in depth description on what has changed compared |
|
| 25 |
to the 0.x release series. |
|
| 26 |
|
|
| 27 |
Many of these changes adjusted automatically by the |
|
| 28 |
<tt>script/lemon-0.x-to-1.x.sh</tt> tool. Those requiring manual |
|
| 29 |
update are typeset <b>boldface</b>. |
|
| 30 |
|
|
| 31 |
\section migration-graph Graph Related Name Changes |
|
| 32 |
|
|
| 33 |
- \ref concepts::Digraph "Directed graphs" are called \c Digraph and |
|
| 34 |
they have <tt>Arc</tt>s (instead of <tt>Edge</tt>s), while |
|
| 35 |
\ref concepts::Graph "undirected graphs" are called \c Graph |
|
| 36 |
(instead of \c UGraph) and they have <tt>Edge</tt>s (instead of |
|
| 37 |
<tt>UEdge</tt>s). These changes reflected thoroughly everywhere in |
|
| 38 |
the library. Namely, |
|
| 39 |
- \c Graph -> \c Digraph |
|
| 40 |
- \c %ListGraph -> \c ListDigraph, \c %SmartGraph -> \c SmartDigraph etc. |
|
| 41 |
- \c UGraph -> \c Graph |
|
| 42 |
- \c ListUGraph -> \c ListGraph, \c SmartUGraph -> \c SmartGraph etc. |
|
| 43 |
- \c Edge -> \c Arc, \c UEdge -> \c Edge |
|
| 44 |
- \c EdgeMap -> \c ArcMap, \c UEdgeMap -> \c EdgeMap |
|
| 45 |
- \c EdgeIt -> \c ArcIt, \c UEdgeIt -> \c EdgeIt |
|
| 46 |
- Class names and function names containing the words \c graph, |
|
| 47 |
\c ugraph, \e edge or \e arc should also be updated. |
|
| 48 |
- <b>The two endpoints of an (\e undirected) \c Edge can be obtained by the |
|
| 49 |
<tt>u()</tt> and <tt>v()</tt> member function of the graph |
|
| 50 |
(instead of <tt>source()</tt> and <tt>target()</tt>). This change |
|
| 51 |
must be done by hand.</b> |
|
| 52 |
\n Of course, you can still use <tt>source()</tt> and <tt>target()</tt> |
|
| 53 |
for <tt>Arc</tt>s (directed edges). |
|
| 54 |
|
|
| 55 |
\warning |
|
| 56 |
<b>The <tt>script/lemon-0.x-to-1.x.sh</tt> tool replaces all instances of |
|
| 57 |
the words \c graph, \c digraph, \c edge and \c arc, so it replaces them |
|
| 58 |
in strings, comments etc. as well as in all identifiers.</b> |
|
| 59 |
|
|
| 60 |
\section migration-lgf LGF tools |
|
| 61 |
- The \ref lgf-format "LGF file format" has changed, |
|
| 62 |
<tt>\@nodeset</tt> has changed to <tt>\@nodes</tt>, |
|
| 63 |
<tt>\@edgeset</tt> and <tt>\@uedgeset</tt> to <tt>\@arcs</tt> or |
|
| 64 |
<tt>\@edges</tt>, which become completely equivalents. The |
|
| 65 |
<tt>\@nodes</tt>, <tt>\@edges</tt> and <tt>\@uedges</tt> sections are |
|
| 66 |
removed from the format, the content of them should be |
|
| 67 |
the part of <tt>\@attributes</tt> section. The data fields in |
|
| 68 |
the sections must follow a strict format, they must be either character |
|
| 69 |
sequences without whitespaces or quoted strings. |
|
| 70 |
- The <tt>LemonReader</tt> and <tt>LemonWriter</tt> core interfaces |
|
| 71 |
are no longer available. |
|
| 72 |
- The implementation of the general section readers and writers has changed |
|
| 73 |
they are simple functors now. Beside the old |
|
| 74 |
stream based section handling, currently line oriented section |
|
| 75 |
reading and writing are also supported. In the |
|
| 76 |
section readers the lines must be counted manually. The sections |
|
| 77 |
should be read and written with the SectionWriter and SectionReader |
|
| 78 |
classes. |
|
| 79 |
- Instead of the item readers and writers, item converters should be |
|
| 80 |
used. The converters are functors, which map the type to |
|
| 81 |
std::string or std::string to the type. The converters for standard |
|
| 82 |
containers hasn't yet been implemented in the new LEMON. The converters |
|
| 83 |
can return strings in any format, because if it is necessary, the LGF |
|
| 84 |
writer and reader will quote and unquote the given value. |
|
| 85 |
- The DigraphReader and DigraphWriter can used similarly to the |
|
| 86 |
0.x series, however the <tt>read</tt> or <tt>write</tt> prefix of |
|
| 87 |
the member functions are removed. |
|
| 88 |
- The new LEMON supports the function like interface, the \c |
|
| 89 |
digraphReader and \c digraphWriter functions are more convenient than |
|
| 90 |
using the classes directly. |
|
| 91 |
|
|
| 92 |
\section migration-search BFS, DFS and Dijkstra |
|
| 93 |
- <b>Using the function interface of BFS, DFS and %Dijkstra both source and |
|
| 94 |
target nodes can be given as parameters of the <tt>run()</tt> function |
|
| 95 |
(instead of \c bfs(), \c dfs() or \c dijkstra() itself).</b> |
|
| 96 |
- \ref named-templ-param "Named class template parameters" of \c Bfs, |
|
| 97 |
\c Dfs, \c Dijkstra, \c BfsVisit, \c DfsVisit are renamed to start |
|
| 98 |
with "Set" instead of "Def". Namely, |
|
| 99 |
- \c DefPredMap -> \c SetPredMap |
|
| 100 |
- \c DefDistMap -> \c SetDistMap |
|
| 101 |
- \c DefReachedMap -> \c SetReachedMap |
|
| 102 |
- \c DefProcessedMap -> \c SetProcessedMap |
|
| 103 |
- \c DefHeap -> \c SetHeap |
|
| 104 |
- \c DefStandardHeap -> \c SetStandardHeap |
|
| 105 |
- \c DefOperationTraits -> \c SetOperationTraits |
|
| 106 |
- \c DefProcessedMapToBeDefaultMap -> \c SetStandardProcessedMap |
|
| 107 |
|
|
| 108 |
\section migration-error Exceptions and Debug tools |
|
| 109 |
|
|
| 110 |
<b>The class hierarchy of exceptions has largely been simplified. Now, |
|
| 111 |
only the i/o related tools may throw exceptions. All other exceptions |
|
| 112 |
have been replaced with either the \c LEMON_ASSERT or the \c LEMON_DEBUG |
|
| 113 |
macros.</b> |
|
| 114 |
|
|
| 115 |
<b>On the other hand, the parameter order of constructors of the |
|
| 116 |
exceptions has been changed. See \ref IoError and \ref FormatError for |
|
| 117 |
more details.</b> |
|
| 118 |
|
|
| 119 |
\section migration-other Others |
|
| 120 |
- <b>The contents of <tt>graph_utils.h</tt> are moved to <tt>core.h</tt> |
|
| 121 |
and <tt>maps.h</tt>. <tt>core.h</tt> is included by all graph types, |
|
| 122 |
therefore it usually do not have to be included directly.</b> |
|
| 123 |
- <b><tt>path_utils.h</tt> is merged to \c path.h.</b> |
|
| 124 |
- <b>The semantic of the assignment operations and copy constructors of maps |
|
| 125 |
are still under discussion. So, you must copy them by hand (i.e. copy |
|
| 126 |
each entry one-by-one)</b> |
|
| 127 |
- <b>The parameters of the graph copying tools (i.e. \c GraphCopy, |
|
| 128 |
\c DigraphCopy) have to be given in the from-to order.</b> |
|
| 129 |
- \c copyDigraph() and \c copyGraph() are renamed to \c digraphCopy() |
|
| 130 |
and \c graphCopy(), respectively. |
|
| 131 |
- <b>The interface of \ref DynArcLookUp has changed. It is now the same as |
|
| 132 |
of \ref ArcLookUp and \ref AllArcLookUp</b> |
|
| 133 |
- Some map types should also been renamed. Namely, |
|
| 134 |
- \c IntegerMap -> \c RangeMap |
|
| 135 |
- \c StdMap -> \c SparseMap |
|
| 136 |
- \c FunctorMap -> \c FunctorToMap |
|
| 137 |
- \c MapFunctor -> \c MapToFunctor |
|
| 138 |
- \c ForkWriteMap -> \c ForkMap |
|
| 139 |
- \c StoreBoolMap -> \c LoggerBoolMap |
|
| 140 |
- \c dim2::BoundingBox -> \c dim2::Box |
|
| 141 |
|
|
| 142 |
*/ |
|
| 143 |
} |
| 1 |
#!/bin/bash |
|
| 2 |
|
|
| 3 |
set -e |
|
| 4 |
|
|
| 5 |
if [ $# -eq 0 -o x$1 = "x-h" -o x$1 = "x-help" -o x$1 = "x--help" ]; then |
|
| 6 |
echo "Usage:" |
|
| 7 |
echo " $0 source-file" |
|
| 8 |
exit |
|
| 9 |
fi |
|
| 10 |
|
|
| 11 |
TMP=`mktemp` |
|
| 12 |
|
|
| 13 |
sed -e "s/undirected graph/_gr_aph_label_/g"\ |
|
| 14 |
-e "s/undirected edge/_ed_ge_label_/g"\ |
|
| 15 |
-e "s/graph_/_gr_aph_label__/g"\ |
|
| 16 |
-e "s/_graph/__gr_aph_label_/g"\ |
|
| 17 |
-e "s/UGraph/_Gr_aph_label_/g"\ |
|
| 18 |
-e "s/uGraph/_gr_aph_label_/g"\ |
|
| 19 |
-e "s/ugraph/_gr_aph_label_/g"\ |
|
| 20 |
-e "s/Graph/_Digr_aph_label_/g"\ |
|
| 21 |
-e "s/graph/_digr_aph_label_/g"\ |
|
| 22 |
-e "s/UEdge/_Ed_ge_label_/g"\ |
|
| 23 |
-e "s/uEdge/_ed_ge_label_/g"\ |
|
| 24 |
-e "s/uedge/_ed_ge_label_/g"\ |
|
| 25 |
-e "s/IncEdgeIt/_In_cEd_geIt_label_/g"\ |
|
| 26 |
-e "s/Edge/_Ar_c_label_/g"\ |
|
| 27 |
-e "s/edge/_ar_c_label_/g"\ |
|
| 28 |
-e "s/ANode/_Re_d_label_/g"\ |
|
| 29 |
-e "s/BNode/_Blu_e_label_/g"\ |
|
| 30 |
-e "s/A-Node/_Re_d_label_/g"\ |
|
| 31 |
-e "s/B-Node/_Blu_e_label_/g"\ |
|
| 32 |
-e "s/anode/_re_d_label_/g"\ |
|
| 33 |
-e "s/bnode/_blu_e_label_/g"\ |
|
| 34 |
-e "s/aNode/_re_d_label_/g"\ |
|
| 35 |
-e "s/bNode/_blu_e_label_/g"\ |
|
| 36 |
-e "s/_Digr_aph_label_/Digraph/g"\ |
|
| 37 |
-e "s/_digr_aph_label_/digraph/g"\ |
|
| 38 |
-e "s/_Gr_aph_label_/Graph/g"\ |
|
| 39 |
-e "s/_gr_aph_label_/graph/g"\ |
|
| 40 |
-e "s/_Ar_c_label_/Arc/g"\ |
|
| 41 |
-e "s/_ar_c_label_/arc/g"\ |
|
| 42 |
-e "s/_Ed_ge_label_/Edge/g"\ |
|
| 43 |
-e "s/_ed_ge_label_/edge/g"\ |
|
| 44 |
-e "s/_In_cEd_geIt_label_/IncEdgeIt/g"\ |
|
| 45 |
-e "s/_Re_d_label_/Red/g"\ |
|
| 46 |
-e "s/_Blu_e_label_/Blue/g"\ |
|
| 47 |
-e "s/_re_d_label_/red/g"\ |
|
| 48 |
-e "s/_blu_e_label_/blue/g"\ |
|
| 49 |
-e "s/\(\W\)DefPredMap\(\W\)/\1SetPredMap\2/g"\ |
|
| 50 |
-e "s/\(\W\)DefPredMap$/\1SetPredMap/g"\ |
|
| 51 |
-e "s/^DefPredMap\(\W\)/SetPredMap\1/g"\ |
|
| 52 |
-e "s/^DefPredMap$/SetPredMap/g"\ |
|
| 53 |
-e "s/\(\W\)DefDistMap\(\W\)/\1SetDistMap\2/g"\ |
|
| 54 |
-e "s/\(\W\)DefDistMap$/\1SetDistMap/g"\ |
|
| 55 |
-e "s/^DefDistMap\(\W\)/SetDistMap\1/g"\ |
|
| 56 |
-e "s/^DefDistMap$/SetDistMap/g"\ |
|
| 57 |
-e "s/\(\W\)DefReachedMap\(\W\)/\1SetReachedMap\2/g"\ |
|
| 58 |
-e "s/\(\W\)DefReachedMap$/\1SetReachedMap/g"\ |
|
| 59 |
-e "s/^DefReachedMap\(\W\)/SetReachedMap\1/g"\ |
|
| 60 |
-e "s/^DefReachedMap$/SetReachedMap/g"\ |
|
| 61 |
-e "s/\(\W\)DefProcessedMap\(\W\)/\1SetProcessedMap\2/g"\ |
|
| 62 |
-e "s/\(\W\)DefProcessedMap$/\1SetProcessedMap/g"\ |
|
| 63 |
-e "s/^DefProcessedMap\(\W\)/SetProcessedMap\1/g"\ |
|
| 64 |
-e "s/^DefProcessedMap$/SetProcessedMap/g"\ |
|
| 65 |
-e "s/\(\W\)DefHeap\(\W\)/\1SetHeap\2/g"\ |
|
| 66 |
-e "s/\(\W\)DefHeap$/\1SetHeap/g"\ |
|
| 67 |
-e "s/^DefHeap\(\W\)/SetHeap\1/g"\ |
|
| 68 |
-e "s/^DefHeap$/SetHeap/g"\ |
|
| 69 |
-e "s/\(\W\)DefStandardHeap\(\W\)/\1SetStandradHeap\2/g"\ |
|
| 70 |
-e "s/\(\W\)DefStandardHeap$/\1SetStandradHeap/g"\ |
|
| 71 |
-e "s/^DefStandardHeap\(\W\)/SetStandradHeap\1/g"\ |
|
| 72 |
-e "s/^DefStandardHeap$/SetStandradHeap/g"\ |
|
| 73 |
-e "s/\(\W\)DefOperationTraits\(\W\)/\1SetOperationTraits\2/g"\ |
|
| 74 |
-e "s/\(\W\)DefOperationTraits$/\1SetOperationTraits/g"\ |
|
| 75 |
-e "s/^DefOperationTraits\(\W\)/SetOperationTraits\1/g"\ |
|
| 76 |
-e "s/^DefOperationTraits$/SetOperationTraits/g"\ |
|
| 77 |
-e "s/\(\W\)DefProcessedMapToBeDefaultMap\(\W\)/\1SetStandardProcessedMap\2/g"\ |
|
| 78 |
-e "s/\(\W\)DefProcessedMapToBeDefaultMap$/\1SetStandardProcessedMap/g"\ |
|
| 79 |
-e "s/^DefProcessedMapToBeDefaultMap\(\W\)/SetStandardProcessedMap\1/g"\ |
|
| 80 |
-e "s/^DefProcessedMapToBeDefaultMap$/SetStandardProcessedMap/g"\ |
|
| 81 |
-e "s/\(\W\)IntegerMap\(\W\)/\1RangeMap\2/g"\ |
|
| 82 |
-e "s/\(\W\)IntegerMap$/\1RangeMap/g"\ |
|
| 83 |
-e "s/^IntegerMap\(\W\)/RangeMap\1/g"\ |
|
| 84 |
-e "s/^IntegerMap$/RangeMap/g"\ |
|
| 85 |
-e "s/\(\W\)integerMap\(\W\)/\1rangeMap\2/g"\ |
|
| 86 |
-e "s/\(\W\)integerMap$/\1rangeMap/g"\ |
|
| 87 |
-e "s/^integerMap\(\W\)/rangeMap\1/g"\ |
|
| 88 |
-e "s/^integerMap$/rangeMap/g"\ |
|
| 89 |
-e "s/\(\W\)copyGraph\(\W\)/\1graphCopy\2/g"\ |
|
| 90 |
-e "s/\(\W\)copyGraph$/\1graphCopy/g"\ |
|
| 91 |
-e "s/^copyGraph\(\W\)/graphCopy\1/g"\ |
|
| 92 |
-e "s/^copyGraph$/graphCopy/g"\ |
|
| 93 |
-e "s/\(\W\)copyDigraph\(\W\)/\1digraphCopy\2/g"\ |
|
| 94 |
-e "s/\(\W\)copyDigraph$/\1digraphCopy/g"\ |
|
| 95 |
-e "s/^copyDigraph\(\W\)/digraphCopy\1/g"\ |
|
| 96 |
-e "s/^copyDigraph$/digraphCopy/g"\ |
|
| 97 |
-e "s/\(\W\)\([sS]\)tdMap\(\W\)/\1\2parseMap\3/g"\ |
|
| 98 |
-e "s/\(\W\)\([sS]\)tdMap$/\1\2parseMap/g"\ |
|
| 99 |
-e "s/^\([sS]\)tdMap\(\W\)/\1parseMap\2/g"\ |
|
| 100 |
-e "s/^\([sS]\)tdMap$/\1parseMap/g"\ |
|
| 101 |
-e "s/\(\W\)\([Ff]\)unctorMap\(\W\)/\1\2unctorToMap\3/g"\ |
|
| 102 |
-e "s/\(\W\)\([Ff]\)unctorMap$/\1\2unctorToMap/g"\ |
|
| 103 |
-e "s/^\([Ff]\)unctorMap\(\W\)/\1unctorToMap\2/g"\ |
|
| 104 |
-e "s/^\([Ff]\)unctorMap$/\1unctorToMap/g"\ |
|
| 105 |
-e "s/\(\W\)\([Mm]\)apFunctor\(\W\)/\1\2apToFunctor\3/g"\ |
|
| 106 |
-e "s/\(\W\)\([Mm]\)apFunctor$/\1\2apToFunctor/g"\ |
|
| 107 |
-e "s/^\([Mm]\)apFunctor\(\W\)/\1apToFunctor\2/g"\ |
|
| 108 |
-e "s/^\([Mm]\)apFunctor$/\1apToFunctor/g"\ |
|
| 109 |
-e "s/\(\W\)\([Ff]\)orkWriteMap\(\W\)/\1\2orkMap\3/g"\ |
|
| 110 |
-e "s/\(\W\)\([Ff]\)orkWriteMap$/\1\2orkMap/g"\ |
|
| 111 |
-e "s/^\([Ff]\)orkWriteMap\(\W\)/\1orkMap\2/g"\ |
|
| 112 |
-e "s/^\([Ff]\)orkWriteMap$/\1orkMap/g"\ |
|
| 113 |
-e "s/\(\W\)StoreBoolMap\(\W\)/\1LoggerBoolMap\2/g"\ |
|
| 114 |
-e "s/\(\W\)StoreBoolMap$/\1LoggerBoolMap/g"\ |
|
| 115 |
-e "s/^StoreBoolMap\(\W\)/LoggerBoolMap\1/g"\ |
|
| 116 |
-e "s/^StoreBoolMap$/LoggerBoolMap/g"\ |
|
| 117 |
-e "s/\(\W\)storeBoolMap\(\W\)/\1loggerBoolMap\2/g"\ |
|
| 118 |
-e "s/\(\W\)storeBoolMap$/\1loggerBoolMap/g"\ |
|
| 119 |
-e "s/^storeBoolMap\(\W\)/loggerBoolMap\1/g"\ |
|
| 120 |
-e "s/^storeBoolMap$/loggerBoolMap/g"\ |
|
| 121 |
-e "s/\(\W\)BoundingBox\(\W\)/\1Box\2/g"\ |
|
| 122 |
-e "s/\(\W\)BoundingBox$/\1Box/g"\ |
|
| 123 |
-e "s/^BoundingBox\(\W\)/Box\1/g"\ |
|
| 124 |
-e "s/^BoundingBox$/Box/g"\ |
|
| 125 |
<$1 > $TMP |
|
| 126 |
|
|
| 127 |
mv $TMP $1 |
|
| ... | ... |
No newline at end of file |
| 1 | 1 |
ACLOCAL_AMFLAGS = -I m4 |
| 2 | 2 |
|
| 3 | 3 |
AM_CPPFLAGS = -I$(top_srcdir) -I$(top_builddir) |
| 4 | 4 |
LDADD = $(top_builddir)/lemon/libemon.la |
| 5 | 5 |
|
| 6 | 6 |
EXTRA_DIST = \ |
| 7 | 7 |
LICENSE \ |
| 8 | 8 |
m4/lx_check_cplex.m4 \ |
| 9 | 9 |
m4/lx_check_glpk.m4 \ |
| 10 | 10 |
m4/lx_check_soplex.m4 \ |
| 11 | 11 |
CMakeLists.txt \ |
| 12 | 12 |
cmake |
| 13 | 13 |
|
| 14 | 14 |
pkgconfigdir = $(libdir)/pkgconfig |
| 15 | 15 |
lemondir = $(pkgincludedir) |
| 16 | 16 |
bitsdir = $(lemondir)/bits |
| 17 | 17 |
conceptdir = $(lemondir)/concepts |
| 18 | 18 |
pkgconfig_DATA = |
| 19 | 19 |
lib_LTLIBRARIES = |
| 20 | 20 |
lemon_HEADERS = |
| 21 | 21 |
bits_HEADERS = |
| 22 | 22 |
concept_HEADERS = |
| 23 | 23 |
noinst_HEADERS = |
| 24 | 24 |
noinst_PROGRAMS = |
| 25 | 25 |
bin_PROGRAMS = |
| 26 | 26 |
check_PROGRAMS = |
| 27 |
dist_bin_SCRIPTS = |
|
| 27 | 28 |
TESTS = |
| 28 | 29 |
XFAIL_TESTS = |
| 29 | 30 |
|
| 30 | 31 |
include lemon/Makefile.am |
| 31 | 32 |
include test/Makefile.am |
| 32 | 33 |
include doc/Makefile.am |
| 33 | 34 |
include demo/Makefile.am |
| 34 |
include benchmark/Makefile.am |
|
| 35 | 35 |
include tools/Makefile.am |
| 36 | 36 |
|
| 37 | 37 |
MRPROPERFILES = \ |
| 38 | 38 |
aclocal.m4 \ |
| 39 | 39 |
config.h.in \ |
| 40 | 40 |
config.h.in~ \ |
| 41 | 41 |
configure \ |
| 42 | 42 |
Makefile.in \ |
| 43 | 43 |
build-aux/config.guess \ |
| 44 | 44 |
build-aux/config.sub \ |
| 45 | 45 |
build-aux/depcomp \ |
| 46 | 46 |
build-aux/install-sh \ |
| 47 | 47 |
build-aux/ltmain.sh \ |
| 48 | 48 |
build-aux/missing \ |
| 49 | 49 |
doc/doxygen.log |
| 50 | 50 |
|
| 51 | 51 |
mrproper: |
| 52 | 52 |
$(MAKE) $(AM_MAKEFLAGS) maintainer-clean |
| 53 | 53 |
-rm -f $(MRPROPERFILES) |
| 54 | 54 |
|
| 55 | 55 |
dist-bz2: dist |
| 56 | 56 |
zcat $(PACKAGE)-$(VERSION).tar.gz | \ |
| 57 | 57 |
bzip2 --best -c > $(PACKAGE)-$(VERSION).tar.bz2 |
| 58 | 58 |
|
| 59 | 59 |
distcheck-bz2: distcheck |
| 60 | 60 |
zcat $(PACKAGE)-$(VERSION).tar.gz | \ |
| 61 | 61 |
bzip2 --best -c > $(PACKAGE)-$(VERSION).tar.bz2 |
| 62 | 62 |
|
| 63 | 63 |
.PHONY: mrproper dist-bz2 distcheck-bz2 |
| 1 | 1 |
================================================================== |
| 2 | 2 |
LEMON - a Library of Efficient Models and Optimization in Networks |
| 3 | 3 |
================================================================== |
| 4 | 4 |
|
| 5 | 5 |
LEMON is an open source library written in C++. It provides |
| 6 | 6 |
easy-to-use implementations of common data structures and algorithms |
| 7 | 7 |
in the area of optimization and helps implementing new ones. The main |
| 8 | 8 |
focus is on graphs and graph algorithms, thus it is especially |
| 9 | 9 |
suitable for solving design and optimization problems of |
| 10 | 10 |
telecommunication networks. To achieve wide usability its data |
| 11 | 11 |
structures and algorithms provide generic interfaces. |
| 12 | 12 |
|
| 13 | 13 |
Contents |
| 14 | 14 |
======== |
| 15 | 15 |
|
| 16 | 16 |
LICENSE |
| 17 | 17 |
|
| 18 | 18 |
Copying, distribution and modification conditions and terms. |
| 19 | 19 |
|
| 20 | 20 |
INSTALL |
| 21 | 21 |
|
| 22 | 22 |
General building and installation instructions. |
| 23 | 23 |
|
| 24 | 24 |
lemon/ |
| 25 | 25 |
|
| 26 | 26 |
Source code of LEMON library. |
| 27 | 27 |
|
| 28 | 28 |
doc/ |
| 29 | 29 |
|
| 30 | 30 |
Documentation of LEMON. The starting page is doc/html/index.html. |
| 31 | 31 |
|
| 32 | 32 |
demo/ |
| 33 | 33 |
|
| 34 | 34 |
Some example programs to make you easier to get familiar with LEMON. |
| 35 | 35 |
|
| 36 | 36 |
test/ |
| 37 | 37 |
|
| 38 | 38 |
Contains programs to check the integrity and correctness of LEMON. |
| 39 | 39 |
|
| 40 |
benchmark/ |
|
| 41 |
|
|
| 42 |
Contains programs for measuring the performance of algorithms. |
|
| 43 |
|
|
| 44 | 40 |
tools/ |
| 45 | 41 |
|
| 46 | 42 |
Various utilities related to LEMON. |
| 1 | 1 |
dnl Process this file with autoconf to produce a configure script. |
| 2 | 2 |
|
| 3 | 3 |
dnl Version information. |
| 4 | 4 |
m4_define([lemon_version_number], |
| 5 | 5 |
[m4_normalize(esyscmd([echo ${LEMON_VERSION}]))])
|
| 6 | 6 |
dnl m4_define([lemon_version_number], []) |
| 7 | 7 |
m4_define([lemon_hg_path], [m4_normalize(esyscmd([./scripts/chg-len.py]))]) |
| 8 | 8 |
m4_define([lemon_hg_revision], [m4_normalize(esyscmd([hg id -i]))]) |
| 9 | 9 |
m4_define([lemon_version], [ifelse(lemon_version_number(), |
| 10 | 10 |
[], |
| 11 | 11 |
[lemon_hg_path().lemon_hg_revision()], |
| 12 | 12 |
[lemon_version_number()])]) |
| 13 | 13 |
|
| 14 | 14 |
AC_PREREQ([2.59]) |
| 15 | 15 |
AC_INIT([LEMON], [lemon_version()], [lemon-user@lemon.cs.elte.hu], [lemon]) |
| 16 | 16 |
AC_CONFIG_AUX_DIR([build-aux]) |
| 17 | 17 |
AC_CONFIG_MACRO_DIR([m4]) |
| 18 | 18 |
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects nostdinc]) |
| 19 | 19 |
AC_CONFIG_SRCDIR([lemon/list_graph.h]) |
| 20 | 20 |
AC_CONFIG_HEADERS([config.h lemon/config.h]) |
| 21 | 21 |
|
| 22 | 22 |
lx_cmdline_cxxflags_set=${CXXFLAGS+set}
|
| 23 | 23 |
|
| 24 | 24 |
dnl Do compilation tests using the C++ compiler. |
| 25 | 25 |
AC_LANG([C++]) |
| 26 | 26 |
|
| 27 | 27 |
dnl Checks for programs. |
| 28 | 28 |
AC_PROG_CXX |
| 29 | 29 |
AC_PROG_CXXCPP |
| 30 | 30 |
AC_PROG_INSTALL |
| 31 | 31 |
AC_DISABLE_SHARED |
| 32 | 32 |
AC_PROG_LIBTOOL |
| 33 | 33 |
|
| 34 | 34 |
AC_CHECK_PROG([doxygen_found],[doxygen],[yes],[no]) |
| 35 | 35 |
AC_CHECK_PROG([gs_found],[gs],[yes],[no]) |
| 36 | 36 |
|
| 37 | 37 |
dnl Detect Intel compiler. |
| 38 | 38 |
AC_MSG_CHECKING([whether we are using the Intel C++ compiler]) |
| 39 | 39 |
AC_COMPILE_IFELSE([#ifndef __INTEL_COMPILER |
| 40 | 40 |
choke me |
| 41 | 41 |
#endif], [ICC=[yes]], [ICC=[no]]) |
| 42 | 42 |
if test x"$ICC" = x"yes"; then |
| 43 | 43 |
AC_MSG_RESULT([yes]) |
| 44 | 44 |
else |
| 45 | 45 |
AC_MSG_RESULT([no]) |
| 46 | 46 |
fi |
| 47 | 47 |
|
| 48 | 48 |
dnl Set custom compiler flags when using g++. |
| 49 | 49 |
if test x"$lx_cmdline_cxxflags_set" != x"set" -a "$GXX" = yes -a "$ICC" = no; then |
| 50 | 50 |
CXXFLAGS="$CXXFLAGS -Wall -W -Wall -W -Wunused -Wformat=2 -Wctor-dtor-privacy -Wnon-virtual-dtor -Wno-char-subscripts -Wwrite-strings -Wno-char-subscripts -Wreturn-type -Wcast-qual -Wcast-align -Wsign-promo -Woverloaded-virtual -Woverloaded-virtual -ansi -fno-strict-aliasing -Wold-style-cast -Wno-unknown-pragmas" |
| 51 | 51 |
fi |
| 52 | 52 |
|
| 53 | 53 |
dnl Checks for libraries. |
| 54 | 54 |
#LX_CHECK_GLPK |
| 55 | 55 |
#LX_CHECK_CPLEX |
| 56 | 56 |
#LX_CHECK_SOPLEX |
| 57 | 57 |
|
| 58 | 58 |
dnl Disable/enable building the demo programs. |
| 59 | 59 |
AC_ARG_ENABLE([demo], |
| 60 | 60 |
AS_HELP_STRING([--enable-demo], [build the demo programs]) |
| 61 | 61 |
AS_HELP_STRING([--disable-demo], [do not build the demo programs @<:@default@:>@]), |
| 62 | 62 |
[], [enable_demo=no]) |
| 63 | 63 |
AC_MSG_CHECKING([whether to build the demo programs]) |
| 64 | 64 |
if test x"$enable_demo" != x"no"; then |
| 65 | 65 |
AC_MSG_RESULT([yes]) |
| 66 | 66 |
else |
| 67 | 67 |
AC_MSG_RESULT([no]) |
| 68 | 68 |
fi |
| 69 | 69 |
AM_CONDITIONAL([WANT_DEMO], [test x"$enable_demo" != x"no"]) |
| 70 | 70 |
|
| 71 | 71 |
dnl Disable/enable building the binary tools. |
| 72 | 72 |
AC_ARG_ENABLE([tools], |
| 73 | 73 |
AS_HELP_STRING([--enable-tools], [build additional tools @<:@default@:>@]) |
| 74 | 74 |
AS_HELP_STRING([--disable-tools], [do not build additional tools]), |
| 75 | 75 |
[], [enable_tools=yes]) |
| 76 | 76 |
AC_MSG_CHECKING([whether to build the additional tools]) |
| 77 | 77 |
if test x"$enable_tools" != x"no"; then |
| 78 | 78 |
AC_MSG_RESULT([yes]) |
| 79 | 79 |
else |
| 80 | 80 |
AC_MSG_RESULT([no]) |
| 81 | 81 |
fi |
| 82 | 82 |
AM_CONDITIONAL([WANT_TOOLS], [test x"$enable_tools" != x"no"]) |
| 83 | 83 |
|
| 84 |
dnl Disable/enable building the benchmarks. |
|
| 85 |
AC_ARG_ENABLE([benchmark], |
|
| 86 |
AS_HELP_STRING([--enable-benchmark], [build the benchmarks]) |
|
| 87 |
AS_HELP_STRING([--disable-benchmark], [do not build the benchmarks @<:@default@:>@]), |
|
| 88 |
[], [enable_benchmark=no]) |
|
| 89 |
AC_MSG_CHECKING([whether to build the benchmarks]) |
|
| 90 |
if test x"$enable_benchmark" != x"no"; then |
|
| 91 |
AC_MSG_RESULT([yes]) |
|
| 92 |
else |
|
| 93 |
AC_MSG_RESULT([no]) |
|
| 94 |
fi |
|
| 95 |
AM_CONDITIONAL([WANT_BENCHMARK], [test x"$enable_benchmark" != x"no"]) |
|
| 96 |
|
|
| 97 | 84 |
dnl Checks for header files. |
| 98 | 85 |
AC_CHECK_HEADERS(limits.h sys/time.h sys/times.h unistd.h) |
| 99 | 86 |
|
| 100 | 87 |
dnl Checks for typedefs, structures, and compiler characteristics. |
| 101 | 88 |
AC_C_CONST |
| 102 | 89 |
AC_C_INLINE |
| 103 | 90 |
AC_TYPE_SIZE_T |
| 104 | 91 |
AC_HEADER_TIME |
| 105 | 92 |
AC_STRUCT_TM |
| 106 | 93 |
|
| 107 | 94 |
dnl Checks for library functions. |
| 108 | 95 |
AC_HEADER_STDC |
| 109 | 96 |
AC_CHECK_FUNCS(gettimeofday times ctime_r) |
| 110 | 97 |
|
| 111 | 98 |
dnl Add dependencies on files generated by configure. |
| 112 | 99 |
AC_SUBST([CONFIG_STATUS_DEPENDENCIES], |
| 113 | 100 |
['$(top_srcdir)/doc/Doxyfile.in $(top_srcdir)/lemon/lemon.pc.in']) |
| 114 | 101 |
|
| 115 | 102 |
AC_CONFIG_FILES([ |
| 116 | 103 |
Makefile |
| 117 | 104 |
doc/Doxyfile |
| 118 | 105 |
lemon/lemon.pc |
| 119 | 106 |
]) |
| 120 | 107 |
|
| 121 | 108 |
AC_OUTPUT |
| 122 | 109 |
|
| 123 | 110 |
echo |
| 124 | 111 |
echo '****************************** SUMMARY ******************************' |
| 125 | 112 |
echo |
| 126 | 113 |
echo Package version............... : $PACKAGE-$VERSION |
| 127 | 114 |
echo |
| 128 | 115 |
echo C++ compiler.................. : $CXX |
| 129 | 116 |
echo C++ compiles flags............ : $CXXFLAGS |
| 130 | 117 |
echo |
| 131 | 118 |
#echo GLPK support.................. : $lx_glpk_found |
| 132 | 119 |
#echo CPLEX support................. : $lx_cplex_found |
| 133 | 120 |
#echo SOPLEX support................ : $lx_soplex_found |
| 134 | 121 |
#echo |
| 135 |
echo Build benchmarks.............. : $enable_benchmark |
|
| 136 | 122 |
echo Build demo programs........... : $enable_demo |
| 137 | 123 |
echo Build additional tools........ : $enable_tools |
| 138 | 124 |
echo |
| 139 | 125 |
echo The packace will be installed in |
| 140 | 126 |
echo -n ' ' |
| 141 | 127 |
echo $prefix. |
| 142 | 128 |
echo |
| 143 | 129 |
echo '*********************************************************************' |
| 144 | 130 |
|
| 145 | 131 |
echo |
| 146 | 132 |
echo Configure complete, now type \'make\' and then \'make install\'. |
| 147 | 133 |
echo |
| 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 Argument parser demo |
| 22 | 22 |
/// |
| 23 | 23 |
/// This example shows how the argument parser can be used. |
| 24 | 24 |
/// |
| 25 | 25 |
/// \include arg_parser_demo.cc |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/arg_parser.h> |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 |
int main(int argc, |
|
| 30 |
int main(int argc, char **argv) |
|
| 31 | 31 |
{
|
| 32 | 32 |
// Initialize the argument parser |
| 33 | 33 |
ArgParser ap(argc, argv); |
| 34 | 34 |
int i; |
| 35 | 35 |
std::string s; |
| 36 | 36 |
double d = 1.0; |
| 37 | 37 |
bool b, nh; |
| 38 | 38 |
bool g1, g2, g3; |
| 39 | 39 |
|
| 40 | 40 |
// Add a mandatory integer option with storage reference |
| 41 | 41 |
ap.refOption("n", "An integer input.", i, true);
|
| 42 | 42 |
// Add a double option with storage reference (the default value is 1.0) |
| 43 | 43 |
ap.refOption("val", "A double input.", d);
|
| 44 | 44 |
// Add a double option without storage reference (the default value is 3.14) |
| 45 | 45 |
ap.doubleOption("val2", "A double input.", 3.14);
|
| 46 | 46 |
// Set synonym for -val option |
| 47 | 47 |
ap.synonym("vals", "val");
|
| 48 | 48 |
// Add a string option |
| 49 | 49 |
ap.refOption("name", "A string input.", s);
|
| 50 | 50 |
// Add bool options |
| 51 | 51 |
ap.refOption("f", "A switch.", b)
|
| 52 | 52 |
.refOption("nohelp", "", nh)
|
| 53 | 53 |
.refOption("gra", "Choice A", g1)
|
| 54 | 54 |
.refOption("grb", "Choice B", g2)
|
| 55 | 55 |
.refOption("grc", "Choice C", g3);
|
| 56 | 56 |
// Bundle -gr* options into a group |
| 57 | 57 |
ap.optionGroup("gr", "gra")
|
| 58 | 58 |
.optionGroup("gr", "grb")
|
| 59 | 59 |
.optionGroup("gr", "grc");
|
| 60 | 60 |
// Set the group mandatory |
| 61 | 61 |
ap.mandatoryGroup("gr");
|
| 62 | 62 |
// Set the options of the group exclusive (only one option can be given) |
| 63 | 63 |
ap.onlyOneGroup("gr");
|
| 64 | 64 |
// Add non-parsed arguments (e.g. input files) |
| 65 | 65 |
ap.other("infile", "The input file.")
|
| 66 | 66 |
.other("...");
|
| 67 | 67 |
|
| 68 | 68 |
// Perform the parsing process |
| 69 | 69 |
// (in case of any error it terminates the program) |
| 70 | 70 |
ap.parse(); |
| 71 | 71 |
|
| 72 | 72 |
// Check each option if it has been given and print its value |
| 73 | 73 |
std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
| 74 | 74 |
|
| 75 | 75 |
std::cout << " Value of -n: " << i << std::endl; |
| 76 | 76 |
if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl;
|
| 77 | 77 |
if(ap.given("val2")) {
|
| 78 | 78 |
d = ap["val2"]; |
| 79 | 79 |
std::cout << " Value of -val2: " << d << std::endl; |
| 80 | 80 |
} |
| 81 | 81 |
if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl;
|
| 82 | 82 |
if(ap.given("f")) std::cout << " -f is given\n";
|
| 83 | 83 |
if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh << std::endl;
|
| 84 | 84 |
if(ap.given("gra")) std::cout << " -gra is given\n";
|
| 85 | 85 |
if(ap.given("grb")) std::cout << " -grb is given\n";
|
| 86 | 86 |
if(ap.given("grc")) std::cout << " -grc is given\n";
|
| 87 | 87 |
|
| 88 | 88 |
switch(ap.files().size()) {
|
| 89 | 89 |
case 0: |
| 90 | 90 |
std::cout << " No file argument was given.\n"; |
| 91 | 91 |
break; |
| 92 | 92 |
case 1: |
| 93 | 93 |
std::cout << " 1 file argument was given. It is:\n"; |
| 94 | 94 |
break; |
| 95 | 95 |
default: |
| 96 | 96 |
std::cout << " " |
| 97 | 97 |
<< ap.files().size() << " file arguments were given. They are:\n"; |
| 98 | 98 |
} |
| 99 | 99 |
for(unsigned int i=0;i<ap.files().size();++i) |
| 100 | 100 |
std::cout << " '" << ap.files()[i] << "'\n"; |
| 101 | 101 |
|
| 102 | 102 |
return 0; |
| 103 | 103 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 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; |
| 1 | 1 |
EXTRA_DIST += \ |
| 2 | 2 |
doc/Doxyfile.in \ |
| 3 | 3 |
doc/DoxygenLayout.xml \ |
| 4 | 4 |
doc/coding_style.dox \ |
| 5 | 5 |
doc/dirs.dox \ |
| 6 | 6 |
doc/groups.dox \ |
| 7 | 7 |
doc/lgf.dox \ |
| 8 | 8 |
doc/license.dox \ |
| 9 | 9 |
doc/mainpage.dox \ |
| 10 |
doc/migration.dox \ |
|
| 10 | 11 |
doc/named-param.dox \ |
| 11 | 12 |
doc/namespaces.dox \ |
| 12 | 13 |
doc/html \ |
| 13 | 14 |
doc/CMakeLists.txt |
| 14 | 15 |
|
| 15 | 16 |
DOC_EPS_IMAGES18 = \ |
| 16 | 17 |
nodeshape_0.eps \ |
| 17 | 18 |
nodeshape_1.eps \ |
| 18 | 19 |
nodeshape_2.eps \ |
| 19 | 20 |
nodeshape_3.eps \ |
| 20 | 21 |
nodeshape_4.eps |
| 21 | 22 |
|
| 22 | 23 |
DOC_EPS_IMAGES = \ |
| 23 | 24 |
$(DOC_EPS_IMAGES18) |
| 24 | 25 |
|
| 25 | 26 |
DOC_PNG_IMAGES = \ |
| 26 | 27 |
$(DOC_EPS_IMAGES:%.eps=doc/gen-images/%.png) |
| 27 | 28 |
|
| 28 | 29 |
EXTRA_DIST += $(DOC_EPS_IMAGES:%=doc/images/%) |
| 29 | 30 |
|
| 30 | 31 |
doc/html: |
| 31 | 32 |
$(MAKE) $(AM_MAKEFLAGS) html |
| 32 | 33 |
|
| 33 | 34 |
GS_COMMAND=gs -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 |
| 34 | 35 |
|
| 35 | 36 |
$(DOC_EPS_IMAGES18:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps |
| 36 | 37 |
-mkdir doc/gen-images |
| 37 | 38 |
if test ${gs_found} = yes; then \
|
| 38 | 39 |
$(GS_COMMAND) -sDEVICE=pngalpha -r18 -sOutputFile=$@ $<; \ |
| 39 | 40 |
else \ |
| 40 | 41 |
echo; \ |
| 41 | 42 |
echo "Ghostscript not found."; \ |
| 42 | 43 |
echo; \ |
| 43 | 44 |
exit 1; \ |
| 44 | 45 |
fi |
| 45 | 46 |
|
| 46 | 47 |
html-local: $(DOC_PNG_IMAGES) |
| 47 | 48 |
if test ${doxygen_found} = yes; then \
|
| 48 | 49 |
cd doc; \ |
| 49 | 50 |
doxygen Doxyfile; \ |
| 50 | 51 |
cd ..; \ |
| 51 | 52 |
else \ |
| 52 | 53 |
echo; \ |
| 53 | 54 |
echo "Doxygen not found."; \ |
| 54 | 55 |
echo; \ |
| 55 | 56 |
exit 1; \ |
| 56 | 57 |
fi |
| 57 | 58 |
|
| 58 | 59 |
clean-local: |
| 59 | 60 |
-rm -rf doc/html |
| 60 | 61 |
-rm -f doc/doxygen.log |
| 61 | 62 |
-rm -f $(DOC_PNG_IMAGES) |
| 62 | 63 |
-rm -rf doc/gen-images |
| 63 | 64 |
|
| 64 | 65 |
update-external-tags: |
| 65 | 66 |
wget -O doc/libstdc++.tag.tmp http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/libstdc++.tag && \ |
| 66 | 67 |
mv doc/libstdc++.tag.tmp doc/libstdc++.tag || \ |
| 67 | 68 |
rm doc/libstdc++.tag.tmp |
| 68 | 69 |
|
| 69 | 70 |
install-html-local: doc/html |
| 70 | 71 |
@$(NORMAL_INSTALL) |
| 71 | 72 |
$(mkinstalldirs) $(DESTDIR)$(htmldir)/docs |
| 72 | 73 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \
|
| 73 | 74 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
| 74 | 75 |
echo " $(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/docs/$$f"; \ |
| 75 | 76 |
$(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/docs/$$f; \ |
| 76 | 77 |
done |
| 77 | 78 |
|
| 78 | 79 |
uninstall-local: |
| 79 | 80 |
@$(NORMAL_UNINSTALL) |
| 80 | 81 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \
|
| 81 | 82 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
| 82 | 83 |
echo " rm -f $(DESTDIR)$(htmldir)/docs/$$f"; \ |
| 83 | 84 |
rm -f $(DESTDIR)$(htmldir)/docs/$$f; \ |
| 84 | 85 |
done |
| 85 | 86 |
|
| 86 | 87 |
.PHONY: update-external-tags |
| 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 |
/** |
| 20 | 20 |
@defgroup datas Data Structures |
| 21 | 21 |
This group describes the several data structures implemented in LEMON. |
| 22 | 22 |
*/ |
| 23 | 23 |
|
| 24 | 24 |
/** |
| 25 | 25 |
@defgroup graphs Graph Structures |
| 26 | 26 |
@ingroup datas |
| 27 | 27 |
\brief Graph structures implemented in LEMON. |
| 28 | 28 |
|
| 29 | 29 |
The implementation of combinatorial algorithms heavily relies on |
| 30 | 30 |
efficient graph implementations. LEMON offers data structures which are |
| 31 | 31 |
planned to be easily used in an experimental phase of implementation studies, |
| 32 | 32 |
and thereafter the program code can be made efficient by small modifications. |
| 33 | 33 |
|
| 34 | 34 |
The most efficient implementation of diverse applications require the |
| 35 | 35 |
usage of different physical graph implementations. These differences |
| 36 | 36 |
appear in the size of graph we require to handle, memory or time usage |
| 37 | 37 |
limitations or in the set of operations through which the graph can be |
| 38 | 38 |
accessed. LEMON provides several physical graph structures to meet |
| 39 | 39 |
the diverging requirements of the possible users. In order to save on |
| 40 | 40 |
running time or on memory usage, some structures may fail to provide |
| 41 | 41 |
some graph features like arc/edge or node deletion. |
| 42 | 42 |
|
| 43 | 43 |
Alteration of standard containers need a very limited number of |
| 44 | 44 |
operations, these together satisfy the everyday requirements. |
| 45 | 45 |
In the case of graph structures, different operations are needed which do |
| 46 | 46 |
not alter the physical graph, but gives another view. If some nodes or |
| 47 | 47 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
| 48 | 48 |
this is the case. It also may happen that in a flow implementation |
| 49 | 49 |
the residual graph can be accessed by another algorithm, or a node-set |
| 50 | 50 |
is to be shrunk for another algorithm. |
| 51 | 51 |
LEMON also provides a variety of graphs for these requirements called |
| 52 | 52 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
| 53 | 53 |
in conjunction with other graph representations. |
| 54 | 54 |
|
| 55 | 55 |
You are free to use the graph structure that fit your requirements |
| 56 | 56 |
the best, most graph algorithms and auxiliary data structures can be used |
| 57 |
with any graph |
|
| 57 |
with any graph structure. |
|
| 58 |
|
|
| 59 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
|
| 58 | 60 |
*/ |
| 59 | 61 |
|
| 60 | 62 |
/** |
| 61 | 63 |
@defgroup semi_adaptors Semi-Adaptor Classes for Graphs |
| 62 | 64 |
@ingroup graphs |
| 63 | 65 |
\brief Graph types between real graphs and graph adaptors. |
| 64 | 66 |
|
| 65 | 67 |
This group describes some graph types between real graphs and graph adaptors. |
| 66 | 68 |
These classes wrap graphs to give new functionality as the adaptors do it. |
| 67 | 69 |
On the other hand they are not light-weight structures as the adaptors. |
| 68 | 70 |
*/ |
| 69 | 71 |
|
| 70 | 72 |
/** |
| 71 | 73 |
@defgroup maps Maps |
| 72 | 74 |
@ingroup datas |
| 73 | 75 |
\brief Map structures implemented in LEMON. |
| 74 | 76 |
|
| 75 | 77 |
This group describes the map structures implemented in LEMON. |
| 76 | 78 |
|
| 77 |
LEMON provides several special purpose maps that e.g. combine |
|
| 79 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
|
| 78 | 80 |
new maps from existing ones. |
| 81 |
|
|
| 82 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
|
| 79 | 83 |
*/ |
| 80 | 84 |
|
| 81 | 85 |
/** |
| 82 | 86 |
@defgroup graph_maps Graph Maps |
| 83 | 87 |
@ingroup maps |
| 84 | 88 |
\brief Special graph-related maps. |
| 85 | 89 |
|
| 86 | 90 |
This group describes maps that are specifically designed to assign |
| 87 | 91 |
values to the nodes and arcs of graphs. |
| 88 | 92 |
*/ |
| 89 | 93 |
|
| 90 |
|
|
| 91 | 94 |
/** |
| 92 | 95 |
\defgroup map_adaptors Map Adaptors |
| 93 | 96 |
\ingroup maps |
| 94 | 97 |
\brief Tools to create new maps from existing ones |
| 95 | 98 |
|
| 96 | 99 |
This group describes map adaptors that are used to create "implicit" |
| 97 | 100 |
maps from other maps. |
| 98 | 101 |
|
| 99 | 102 |
Most of them are \ref lemon::concepts::ReadMap "read-only maps". |
| 100 | 103 |
They can make arithmetic and logical operations between one or two maps |
| 101 | 104 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
| 102 | 105 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
| 103 | 106 |
|
| 104 | 107 |
The typical usage of this classes is passing implicit maps to |
| 105 | 108 |
algorithms. If a function type algorithm is called then the function |
| 106 | 109 |
type map adaptors can be used comfortable. For example let's see the |
| 107 |
usage of map adaptors with the \c |
|
| 110 |
usage of map adaptors with the \c graphToEps() function. |
|
| 108 | 111 |
\code |
| 109 | 112 |
Color nodeColor(int deg) {
|
| 110 | 113 |
if (deg >= 2) {
|
| 111 | 114 |
return Color(0.5, 0.0, 0.5); |
| 112 | 115 |
} else if (deg == 1) {
|
| 113 | 116 |
return Color(1.0, 0.5, 1.0); |
| 114 | 117 |
} else {
|
| 115 | 118 |
return Color(0.0, 0.0, 0.0); |
| 116 | 119 |
} |
| 117 | 120 |
} |
| 118 | 121 |
|
| 119 | 122 |
Digraph::NodeMap<int> degree_map(graph); |
| 120 | 123 |
|
| 121 |
|
|
| 124 |
graphToEps(graph, "graph.eps") |
|
| 122 | 125 |
.coords(coords).scaleToA4().undirected() |
| 123 | 126 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
| 124 | 127 |
.run(); |
| 125 | 128 |
\endcode |
| 126 | 129 |
The \c functorToMap() function makes an \c int to \c Color map from the |
| 127 |
\ |
|
| 130 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
|
| 128 | 131 |
and the previously created map. The composed map is a proper function to |
| 129 | 132 |
get the color of each node. |
| 130 | 133 |
|
| 131 | 134 |
The usage with class type algorithms is little bit harder. In this |
| 132 | 135 |
case the function type map adaptors can not be used, because the |
| 133 | 136 |
function map adaptors give back temporary objects. |
| 134 | 137 |
\code |
| 135 | 138 |
Digraph graph; |
| 136 | 139 |
|
| 137 | 140 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
| 138 | 141 |
DoubleArcMap length(graph); |
| 139 | 142 |
DoubleArcMap speed(graph); |
| 140 | 143 |
|
| 141 | 144 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
| 142 | 145 |
TimeMap time(length, speed); |
| 143 | 146 |
|
| 144 | 147 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
| 145 | 148 |
dijkstra.run(source, target); |
| 146 | 149 |
\endcode |
| 147 | 150 |
We have a length map and a maximum speed map on the arcs of a digraph. |
| 148 | 151 |
The minimum time to pass the arc can be calculated as the division of |
| 149 | 152 |
the two maps which can be done implicitly with the \c DivMap template |
| 150 | 153 |
class. We use the implicit minimum time map as the length map of the |
| 151 | 154 |
\c Dijkstra algorithm. |
| 152 | 155 |
*/ |
| 153 | 156 |
|
| 154 | 157 |
/** |
| 155 | 158 |
@defgroup matrices Matrices |
| 156 | 159 |
@ingroup datas |
| 157 | 160 |
\brief Two dimensional data storages implemented in LEMON. |
| 158 | 161 |
|
| 159 | 162 |
This group describes two dimensional data storages implemented in LEMON. |
| 160 | 163 |
*/ |
| 161 | 164 |
|
| 162 | 165 |
/** |
| 163 | 166 |
@defgroup paths Path Structures |
| 164 | 167 |
@ingroup datas |
| 165 | 168 |
\brief Path structures implemented in LEMON. |
| 166 | 169 |
|
| 167 | 170 |
This group describes the path structures implemented in LEMON. |
| 168 | 171 |
|
| 169 | 172 |
LEMON provides flexible data structures to work with paths. |
| 170 | 173 |
All of them have similar interfaces and they can be copied easily with |
| 171 | 174 |
assignment operators and copy constructors. This makes it easy and |
| 172 | 175 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
| 173 | 176 |
any kind of path structure. |
| 174 | 177 |
|
| 175 | 178 |
\sa lemon::concepts::Path |
| 176 |
|
|
| 177 | 179 |
*/ |
| 178 | 180 |
|
| 179 | 181 |
/** |
| 180 | 182 |
@defgroup auxdat Auxiliary Data Structures |
| 181 | 183 |
@ingroup datas |
| 182 | 184 |
\brief Auxiliary data structures implemented in LEMON. |
| 183 | 185 |
|
| 184 | 186 |
This group describes some data structures implemented in LEMON in |
| 185 | 187 |
order to make it easier to implement combinatorial algorithms. |
| 186 | 188 |
*/ |
| 187 | 189 |
|
| 188 |
|
|
| 189 | 190 |
/** |
| 190 | 191 |
@defgroup algs Algorithms |
| 191 | 192 |
\brief This group describes the several algorithms |
| 192 | 193 |
implemented in LEMON. |
| 193 | 194 |
|
| 194 | 195 |
This group describes the several algorithms |
| 195 | 196 |
implemented in LEMON. |
| 196 | 197 |
*/ |
| 197 | 198 |
|
| 198 | 199 |
/** |
| 199 | 200 |
@defgroup search Graph Search |
| 200 | 201 |
@ingroup algs |
| 201 | 202 |
\brief Common graph search algorithms. |
| 202 | 203 |
|
| 203 | 204 |
This group describes the common graph search algorithms like |
| 204 |
Breadth- |
|
| 205 |
Breadth-First Search (BFS) and Depth-First Search (DFS). |
|
| 205 | 206 |
*/ |
| 206 | 207 |
|
| 207 | 208 |
/** |
| 208 |
@defgroup shortest_path Shortest Path |
|
| 209 |
@defgroup shortest_path Shortest Path Algorithms |
|
| 209 | 210 |
@ingroup algs |
| 210 | 211 |
\brief Algorithms for finding shortest paths. |
| 211 | 212 |
|
| 212 | 213 |
This group describes the algorithms for finding shortest paths in graphs. |
| 213 | 214 |
*/ |
| 214 | 215 |
|
| 215 | 216 |
/** |
| 216 |
@defgroup max_flow Maximum Flow |
|
| 217 |
@defgroup max_flow Maximum Flow Algorithms |
|
| 217 | 218 |
@ingroup algs |
| 218 | 219 |
\brief Algorithms for finding maximum flows. |
| 219 | 220 |
|
| 220 | 221 |
This group describes the algorithms for finding maximum flows and |
| 221 | 222 |
feasible circulations. |
| 222 | 223 |
|
| 223 | 224 |
The maximum flow problem is to find a flow between a single source and |
| 224 | 225 |
a single target that is maximum. Formally, there is a \f$G=(V,A)\f$ |
| 225 | 226 |
directed graph, an \f$c_a:A\rightarrow\mathbf{R}^+_0\f$ capacity
|
| 226 | 227 |
function and given \f$s, t \in V\f$ source and target node. The |
| 227 | 228 |
maximum flow is the \f$f_a\f$ solution of the next optimization problem: |
| 228 | 229 |
|
| 229 | 230 |
\f[ 0 \le f_a \le c_a \f] |
| 230 | 231 |
\f[ \sum_{v\in\delta^{-}(u)}f_{vu}=\sum_{v\in\delta^{+}(u)}f_{uv}
|
| 231 | 232 |
\qquad \forall u \in V \setminus \{s,t\}\f]
|
| 232 | 233 |
\f[ \max \sum_{v\in\delta^{+}(s)}f_{uv} - \sum_{v\in\delta^{-}(s)}f_{vu}\f]
|
| 233 | 234 |
|
| 234 | 235 |
LEMON contains several algorithms for solving maximum flow problems: |
| 235 | 236 |
- \ref lemon::EdmondsKarp "Edmonds-Karp" |
| 236 | 237 |
- \ref lemon::Preflow "Goldberg's Preflow algorithm" |
| 237 | 238 |
- \ref lemon::DinitzSleatorTarjan "Dinitz's blocking flow algorithm with dynamic trees" |
| 238 | 239 |
- \ref lemon::GoldbergTarjan "Preflow algorithm with dynamic trees" |
| 239 | 240 |
|
| 240 | 241 |
In most cases the \ref lemon::Preflow "Preflow" algorithm provides the |
| 241 | 242 |
fastest method to compute the maximum flow. All impelementations |
| 242 | 243 |
provides functions to query the minimum cut, which is the dual linear |
| 243 | 244 |
programming problem of the maximum flow. |
| 244 |
|
|
| 245 | 245 |
*/ |
| 246 | 246 |
|
| 247 | 247 |
/** |
| 248 |
@defgroup min_cost_flow Minimum Cost Flow |
|
| 248 |
@defgroup min_cost_flow Minimum Cost Flow Algorithms |
|
| 249 | 249 |
@ingroup algs |
| 250 | 250 |
|
| 251 | 251 |
\brief Algorithms for finding minimum cost flows and circulations. |
| 252 | 252 |
|
| 253 | 253 |
This group describes the algorithms for finding minimum cost flows and |
| 254 | 254 |
circulations. |
| 255 | 255 |
*/ |
| 256 | 256 |
|
| 257 | 257 |
/** |
| 258 |
@defgroup min_cut Minimum Cut |
|
| 258 |
@defgroup min_cut Minimum Cut Algorithms |
|
| 259 | 259 |
@ingroup algs |
| 260 | 260 |
|
| 261 | 261 |
\brief Algorithms for finding minimum cut in graphs. |
| 262 | 262 |
|
| 263 | 263 |
This group describes the algorithms for finding minimum cut in graphs. |
| 264 | 264 |
|
| 265 | 265 |
The minimum cut problem is to find a non-empty and non-complete |
| 266 | 266 |
\f$X\f$ subset of the vertices with minimum overall capacity on |
| 267 | 267 |
outgoing arcs. Formally, there is \f$G=(V,A)\f$ directed graph, an |
| 268 | 268 |
\f$c_a:A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum
|
| 269 | 269 |
cut is the \f$X\f$ solution of the next optimization problem: |
| 270 | 270 |
|
| 271 | 271 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
|
| 272 | 272 |
\sum_{uv\in A, u\in X, v\not\in X}c_{uv}\f]
|
| 273 | 273 |
|
| 274 | 274 |
LEMON contains several algorithms related to minimum cut problems: |
| 275 | 275 |
|
| 276 | 276 |
- \ref lemon::HaoOrlin "Hao-Orlin algorithm" to calculate minimum cut |
| 277 | 277 |
in directed graphs |
| 278 | 278 |
- \ref lemon::NagamochiIbaraki "Nagamochi-Ibaraki algorithm" to |
| 279 | 279 |
calculate minimum cut in undirected graphs |
| 280 | 280 |
- \ref lemon::GomoryHuTree "Gomory-Hu tree computation" to calculate all |
| 281 | 281 |
pairs minimum cut in undirected graphs |
| 282 | 282 |
|
| 283 | 283 |
If you want to find minimum cut just between two distinict nodes, |
| 284 | 284 |
please see the \ref max_flow "Maximum Flow page". |
| 285 |
|
|
| 286 | 285 |
*/ |
| 287 | 286 |
|
| 288 | 287 |
/** |
| 289 |
@defgroup graph_prop Connectivity and |
|
| 288 |
@defgroup graph_prop Connectivity and Other Graph Properties |
|
| 290 | 289 |
@ingroup algs |
| 291 | 290 |
\brief Algorithms for discovering the graph properties |
| 292 | 291 |
|
| 293 | 292 |
This group describes the algorithms for discovering the graph properties |
| 294 | 293 |
like connectivity, bipartiteness, euler property, simplicity etc. |
| 295 | 294 |
|
| 296 | 295 |
\image html edge_biconnected_components.png |
| 297 | 296 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
| 298 | 297 |
*/ |
| 299 | 298 |
|
| 300 | 299 |
/** |
| 301 |
@defgroup planar Planarity |
|
| 300 |
@defgroup planar Planarity Embedding and Drawing |
|
| 302 | 301 |
@ingroup algs |
| 303 | 302 |
\brief Algorithms for planarity checking, embedding and drawing |
| 304 | 303 |
|
| 305 | 304 |
This group describes the algorithms for planarity checking, |
| 306 | 305 |
embedding and drawing. |
| 307 | 306 |
|
| 308 | 307 |
\image html planar.png |
| 309 | 308 |
\image latex planar.eps "Plane graph" width=\textwidth |
| 310 | 309 |
*/ |
| 311 | 310 |
|
| 312 | 311 |
/** |
| 313 |
@defgroup matching Matching |
|
| 312 |
@defgroup matching Matching Algorithms |
|
| 314 | 313 |
@ingroup algs |
| 315 | 314 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
| 316 | 315 |
|
| 317 | 316 |
This group contains algorithm objects and functions to calculate |
| 318 | 317 |
matchings in graphs and bipartite graphs. The general matching problem is |
| 319 | 318 |
finding a subset of the arcs which does not shares common endpoints. |
| 320 | 319 |
|
| 321 | 320 |
There are several different algorithms for calculate matchings in |
| 322 | 321 |
graphs. The matching problems in bipartite graphs are generally |
| 323 | 322 |
easier than in general graphs. The goal of the matching optimization |
| 324 | 323 |
can be the finding maximum cardinality, maximum weight or minimum cost |
| 325 | 324 |
matching. The search can be constrained to find perfect or |
| 326 | 325 |
maximum cardinality matching. |
| 327 | 326 |
|
| 328 | 327 |
LEMON contains the next algorithms: |
| 329 | 328 |
- \ref lemon::MaxBipartiteMatching "MaxBipartiteMatching" Hopcroft-Karp |
| 330 | 329 |
augmenting path algorithm for calculate maximum cardinality matching in |
| 331 | 330 |
bipartite graphs |
| 332 | 331 |
- \ref lemon::PrBipartiteMatching "PrBipartiteMatching" Push-Relabel |
| 333 | 332 |
algorithm for calculate maximum cardinality matching in bipartite graphs |
| 334 | 333 |
- \ref lemon::MaxWeightedBipartiteMatching "MaxWeightedBipartiteMatching" |
| 335 | 334 |
Successive shortest path algorithm for calculate maximum weighted matching |
| 336 | 335 |
and maximum weighted bipartite matching in bipartite graph |
| 337 | 336 |
- \ref lemon::MinCostMaxBipartiteMatching "MinCostMaxBipartiteMatching" |
| 338 | 337 |
Successive shortest path algorithm for calculate minimum cost maximum |
| 339 | 338 |
matching in bipartite graph |
| 340 | 339 |
- \ref lemon::MaxMatching "MaxMatching" Edmond's blossom shrinking algorithm |
| 341 | 340 |
for calculate maximum cardinality matching in general graph |
| 342 | 341 |
- \ref lemon::MaxWeightedMatching "MaxWeightedMatching" Edmond's blossom |
| 343 | 342 |
shrinking algorithm for calculate maximum weighted matching in general |
| 344 | 343 |
graph |
| 345 | 344 |
- \ref lemon::MaxWeightedPerfectMatching "MaxWeightedPerfectMatching" |
| 346 | 345 |
Edmond's blossom shrinking algorithm for calculate maximum weighted |
| 347 | 346 |
perfect matching in general graph |
| 348 | 347 |
|
| 349 | 348 |
\image html bipartite_matching.png |
| 350 | 349 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
| 351 |
|
|
| 352 | 350 |
*/ |
| 353 | 351 |
|
| 354 | 352 |
/** |
| 355 |
@defgroup spantree Minimum Spanning Tree |
|
| 353 |
@defgroup spantree Minimum Spanning Tree Algorithms |
|
| 356 | 354 |
@ingroup algs |
| 357 | 355 |
\brief Algorithms for finding a minimum cost spanning tree in a graph. |
| 358 | 356 |
|
| 359 | 357 |
This group describes the algorithms for finding a minimum cost spanning |
| 360 | 358 |
tree in a graph |
| 361 | 359 |
*/ |
| 362 | 360 |
|
| 363 |
|
|
| 364 | 361 |
/** |
| 365 |
@defgroup auxalg Auxiliary |
|
| 362 |
@defgroup auxalg Auxiliary Algorithms |
|
| 366 | 363 |
@ingroup algs |
| 367 | 364 |
\brief Auxiliary algorithms implemented in LEMON. |
| 368 | 365 |
|
| 369 | 366 |
This group describes some algorithms implemented in LEMON |
| 370 | 367 |
in order to make it easier to implement complex algorithms. |
| 371 | 368 |
*/ |
| 372 | 369 |
|
| 373 | 370 |
/** |
| 374 |
@defgroup approx Approximation |
|
| 371 |
@defgroup approx Approximation Algorithms |
|
| 372 |
@ingroup algs |
|
| 375 | 373 |
\brief Approximation algorithms. |
| 376 | 374 |
|
| 377 | 375 |
This group describes the approximation and heuristic algorithms |
| 378 | 376 |
implemented in LEMON. |
| 379 | 377 |
*/ |
| 380 | 378 |
|
| 381 | 379 |
/** |
| 382 | 380 |
@defgroup gen_opt_group General Optimization Tools |
| 383 | 381 |
\brief This group describes some general optimization frameworks |
| 384 | 382 |
implemented in LEMON. |
| 385 | 383 |
|
| 386 | 384 |
This group describes some general optimization frameworks |
| 387 | 385 |
implemented in LEMON. |
| 388 |
|
|
| 389 | 386 |
*/ |
| 390 | 387 |
|
| 391 | 388 |
/** |
| 392 |
@defgroup lp_group Lp and Mip |
|
| 389 |
@defgroup lp_group Lp and Mip Solvers |
|
| 393 | 390 |
@ingroup gen_opt_group |
| 394 | 391 |
\brief Lp and Mip solver interfaces for LEMON. |
| 395 | 392 |
|
| 396 | 393 |
This group describes Lp and Mip solver interfaces for LEMON. The |
| 397 | 394 |
various LP solvers could be used in the same manner with this |
| 398 | 395 |
interface. |
| 399 |
|
|
| 400 | 396 |
*/ |
| 401 | 397 |
|
| 402 | 398 |
/** |
| 403 |
@defgroup lp_utils Tools for Lp and Mip |
|
| 399 |
@defgroup lp_utils Tools for Lp and Mip Solvers |
|
| 404 | 400 |
@ingroup lp_group |
| 405 | 401 |
\brief Helper tools to the Lp and Mip solvers. |
| 406 | 402 |
|
| 407 | 403 |
This group adds some helper tools to general optimization framework |
| 408 | 404 |
implemented in LEMON. |
| 409 | 405 |
*/ |
| 410 | 406 |
|
| 411 | 407 |
/** |
| 412 | 408 |
@defgroup metah Metaheuristics |
| 413 | 409 |
@ingroup gen_opt_group |
| 414 | 410 |
\brief Metaheuristics for LEMON library. |
| 415 | 411 |
|
| 416 | 412 |
This group describes some metaheuristic optimization tools. |
| 417 | 413 |
*/ |
| 418 | 414 |
|
| 419 | 415 |
/** |
| 420 | 416 |
@defgroup utils Tools and Utilities |
| 421 | 417 |
\brief Tools and utilities for programming in LEMON |
| 422 | 418 |
|
| 423 | 419 |
Tools and utilities for programming in LEMON. |
| 424 | 420 |
*/ |
| 425 | 421 |
|
| 426 | 422 |
/** |
| 427 | 423 |
@defgroup gutils Basic Graph Utilities |
| 428 | 424 |
@ingroup utils |
| 429 | 425 |
\brief Simple basic graph utilities. |
| 430 | 426 |
|
| 431 | 427 |
This group describes some simple basic graph utilities. |
| 432 | 428 |
*/ |
| 433 | 429 |
|
| 434 | 430 |
/** |
| 435 | 431 |
@defgroup misc Miscellaneous Tools |
| 436 | 432 |
@ingroup utils |
| 437 | 433 |
\brief Tools for development, debugging and testing. |
| 438 | 434 |
|
| 439 | 435 |
This group describes several useful tools for development, |
| 440 | 436 |
debugging and testing. |
| 441 | 437 |
*/ |
| 442 | 438 |
|
| 443 | 439 |
/** |
| 444 |
@defgroup timecount Time |
|
| 440 |
@defgroup timecount Time Measuring and Counting |
|
| 445 | 441 |
@ingroup misc |
| 446 | 442 |
\brief Simple tools for measuring the performance of algorithms. |
| 447 | 443 |
|
| 448 | 444 |
This group describes simple tools for measuring the performance |
| 449 | 445 |
of algorithms. |
| 450 | 446 |
*/ |
| 451 | 447 |
|
| 452 | 448 |
/** |
| 453 |
@defgroup graphbits Tools for Graph Implementation |
|
| 454 |
@ingroup utils |
|
| 455 |
\brief Tools to make it easier to create graphs. |
|
| 456 |
|
|
| 457 |
This group describes the tools that makes it easier to create graphs and |
|
| 458 |
the maps that dynamically update with the graph changes. |
|
| 459 |
*/ |
|
| 460 |
|
|
| 461 |
/** |
|
| 462 | 449 |
@defgroup exceptions Exceptions |
| 463 | 450 |
@ingroup utils |
| 464 | 451 |
\brief Exceptions defined in LEMON. |
| 465 | 452 |
|
| 466 | 453 |
This group describes the exceptions defined in LEMON. |
| 467 | 454 |
*/ |
| 468 | 455 |
|
| 469 | 456 |
/** |
| 470 | 457 |
@defgroup io_group Input-Output |
| 471 | 458 |
\brief Graph Input-Output methods |
| 472 | 459 |
|
| 473 | 460 |
This group describes the tools for importing and exporting graphs |
| 474 |
and graph related data. Now it supports the LEMON format, the |
|
| 475 |
\c DIMACS format and the encapsulated postscript (EPS) format. |
|
| 461 |
and graph related data. Now it supports the \ref lgf-format |
|
| 462 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
|
| 463 |
postscript (EPS) format. |
|
| 476 | 464 |
*/ |
| 477 | 465 |
|
| 478 | 466 |
/** |
| 479 | 467 |
@defgroup lemon_io LEMON Input-Output |
| 480 | 468 |
@ingroup io_group |
| 481 |
\brief Reading and writing |
|
| 469 |
\brief Reading and writing LEMON Graph Format. |
|
| 482 | 470 |
|
| 483 | 471 |
This group describes methods for reading and writing |
| 484 | 472 |
\ref lgf-format "LEMON Graph Format". |
| 485 | 473 |
*/ |
| 486 | 474 |
|
| 487 | 475 |
/** |
| 488 |
@defgroup eps_io Postscript |
|
| 476 |
@defgroup eps_io Postscript Exporting |
|
| 489 | 477 |
@ingroup io_group |
| 490 | 478 |
\brief General \c EPS drawer and graph exporter |
| 491 | 479 |
|
| 492 | 480 |
This group describes general \c EPS drawing methods and special |
| 493 | 481 |
graph exporting tools. |
| 494 | 482 |
*/ |
| 495 | 483 |
|
| 496 |
|
|
| 497 | 484 |
/** |
| 498 | 485 |
@defgroup concept Concepts |
| 499 | 486 |
\brief Skeleton classes and concept checking classes |
| 500 | 487 |
|
| 501 | 488 |
This group describes the data/algorithm skeletons and concept checking |
| 502 | 489 |
classes implemented in LEMON. |
| 503 | 490 |
|
| 504 | 491 |
The purpose of the classes in this group is fourfold. |
| 505 | 492 |
|
| 506 | 493 |
- These classes contain the documentations of the concepts. In order |
| 507 | 494 |
to avoid document multiplications, an implementation of a concept |
| 508 | 495 |
simply refers to the corresponding concept class. |
| 509 | 496 |
|
| 510 | 497 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
| 511 | 498 |
implementation of the concepts should provide, however completely |
| 512 | 499 |
without implementations and real data structures behind the |
| 513 | 500 |
interface. On the other hand they should provide nothing else. All |
| 514 | 501 |
the algorithms working on a data structure meeting a certain concept |
| 515 | 502 |
should compile with these classes. (Though it will not run properly, |
| 516 | 503 |
of course.) In this way it is easily to check if an algorithm |
| 517 | 504 |
doesn't use any extra feature of a certain implementation. |
| 518 | 505 |
|
| 519 | 506 |
- The concept descriptor classes also provide a <em>checker class</em> |
| 520 | 507 |
that makes it possible to check whether a certain implementation of a |
| 521 | 508 |
concept indeed provides all the required features. |
| 522 | 509 |
|
| 523 | 510 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
| 524 |
|
|
| 525 | 511 |
*/ |
| 526 | 512 |
|
| 527 |
|
|
| 528 | 513 |
/** |
| 529 | 514 |
@defgroup graph_concepts Graph Structure Concepts |
| 530 | 515 |
@ingroup concept |
| 531 | 516 |
\brief Skeleton and concept checking classes for graph structures |
| 532 | 517 |
|
| 533 | 518 |
This group describes the skeletons and concept checking classes of LEMON's |
| 534 | 519 |
graph structures and helper classes used to implement these. |
| 535 | 520 |
*/ |
| 536 | 521 |
|
| 537 |
/* --- Unused group |
|
| 538 |
@defgroup experimental Experimental Structures and Algorithms |
|
| 539 |
This group describes some Experimental structures and algorithms. |
|
| 540 |
The stuff here is subject to change. |
|
| 522 |
/** |
|
| 523 |
@defgroup map_concepts Map Concepts |
|
| 524 |
@ingroup concept |
|
| 525 |
\brief Skeleton and concept checking classes for maps |
|
| 526 |
|
|
| 527 |
This group describes the skeletons and concept checking classes of maps. |
|
| 541 | 528 |
*/ |
| 542 | 529 |
|
| 543 | 530 |
/** |
| 544 | 531 |
\anchor demoprograms |
| 545 | 532 |
|
| 546 | 533 |
@defgroup demos Demo programs |
| 547 | 534 |
|
| 548 | 535 |
Some demo programs are listed here. Their full source codes can be found in |
| 549 | 536 |
the \c demo subdirectory of the source tree. |
| 550 | 537 |
|
| 551 | 538 |
It order to compile them, use <tt>--enable-demo</tt> configure option when |
| 552 | 539 |
build the library. |
| 553 | 540 |
*/ |
| 554 | 541 |
|
| 555 | 542 |
/** |
| 556 | 543 |
@defgroup tools Standalone utility applications |
| 557 | 544 |
|
| 558 | 545 |
Some utility applications are listed here. |
| 559 | 546 |
|
| 560 | 547 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
| 561 | 548 |
them, as well. |
| 562 | 549 |
*/ |
| 563 | 550 |
| 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 |
/** |
| 20 | 20 |
\mainpage LEMON Documentation |
| 21 | 21 |
|
| 22 | 22 |
\section intro Introduction |
| 23 | 23 |
|
| 24 | 24 |
\subsection whatis What is LEMON |
| 25 | 25 |
|
| 26 | 26 |
LEMON stands for |
| 27 | 27 |
<b>L</b>ibrary of <b>E</b>fficient <b>M</b>odels |
| 28 | 28 |
and <b>O</b>ptimization in <b>N</b>etworks. |
| 29 | 29 |
It is a C++ template |
| 30 | 30 |
library aimed at combinatorial optimization tasks which |
| 31 | 31 |
often involve in working |
| 32 | 32 |
with graphs. |
| 33 | 33 |
|
| 34 | 34 |
<b> |
| 35 | 35 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
| 36 | 36 |
project. |
| 37 | 37 |
You are free to use it in your commercial or |
| 38 | 38 |
non-commercial applications under very permissive |
| 39 | 39 |
\ref license "license terms". |
| 40 | 40 |
</b> |
| 41 | 41 |
|
| 42 | 42 |
\subsection howtoread How to read the documentation |
| 43 | 43 |
|
| 44 | 44 |
If you want to get a quick start and see the most important features then |
| 45 | 45 |
take a look at our \ref quicktour |
| 46 | 46 |
"Quick Tour to LEMON" which will guide you along. |
| 47 | 47 |
|
| 48 | 48 |
If you already feel like using our library, see the page that tells you |
| 49 | 49 |
\ref getstart "How to start using LEMON". |
| 50 | 50 |
|
| 51 | 51 |
If you |
| 52 | 52 |
want to see how LEMON works, see |
| 53 |
some \ref demoprograms "demo programs" |
|
| 53 |
some \ref demoprograms "demo programs". |
|
| 54 | 54 |
|
| 55 | 55 |
If you know what you are looking for then try to find it under the |
| 56 | 56 |
<a class="el" href="modules.html">Modules</a> |
| 57 | 57 |
section. |
| 58 | 58 |
|
| 59 |
|
|
| 59 |
If you are a user of the old (0.x) series of LEMON, please check out the |
|
| 60 |
\ref migration "Migration Guide" for the backward incompatibilities. |
|
| 60 | 61 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/arg_parser.h> |
| 20 | 20 |
|
| 21 | 21 |
namespace lemon {
|
| 22 | 22 |
|
| 23 | 23 |
void ArgParser::_showHelp(void *p) |
| 24 | 24 |
{
|
| 25 | 25 |
(static_cast<ArgParser*>(p))->showHelp(); |
| 26 | 26 |
exit(1); |
| 27 | 27 |
} |
| 28 | 28 |
|
| 29 |
ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv), |
|
| 30 |
_command_name(argv[0]) {
|
|
| 29 |
ArgParser::ArgParser(int argc, const char * const *argv) |
|
| 30 |
:_argc(argc), _argv(argv), _command_name(argv[0]) {
|
|
| 31 | 31 |
funcOption("-help","Print a short help message",_showHelp,this);
|
| 32 | 32 |
synonym("help","-help");
|
| 33 | 33 |
synonym("h","-help");
|
| 34 |
|
|
| 35 | 34 |
} |
| 36 | 35 |
|
| 37 | 36 |
ArgParser::~ArgParser() |
| 38 | 37 |
{
|
| 39 | 38 |
for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) |
| 40 | 39 |
if(i->second.self_delete) |
| 41 | 40 |
switch(i->second.type) {
|
| 42 | 41 |
case BOOL: |
| 43 | 42 |
delete i->second.bool_p; |
| 44 | 43 |
break; |
| 45 | 44 |
case STRING: |
| 46 | 45 |
delete i->second.string_p; |
| 47 | 46 |
break; |
| 48 | 47 |
case DOUBLE: |
| 49 | 48 |
delete i->second.double_p; |
| 50 | 49 |
break; |
| 51 | 50 |
case INTEGER: |
| 52 | 51 |
delete i->second.int_p; |
| 53 | 52 |
break; |
| 54 | 53 |
case UNKNOWN: |
| 55 | 54 |
break; |
| 56 | 55 |
case FUNC: |
| 57 | 56 |
break; |
| 58 | 57 |
} |
| 59 | 58 |
} |
| 60 | 59 |
|
| 61 | 60 |
|
| 62 | 61 |
ArgParser &ArgParser::intOption(const std::string &name, |
| 63 | 62 |
const std::string &help, |
| 64 | 63 |
int value, bool obl) |
| 65 | 64 |
{
|
| 66 | 65 |
ParData p; |
| 67 | 66 |
p.int_p=new int(value); |
| 68 | 67 |
p.self_delete=true; |
| 69 | 68 |
p.help=help; |
| 70 | 69 |
p.type=INTEGER; |
| 71 | 70 |
p.mandatory=obl; |
| 72 | 71 |
_opts[name]=p; |
| 73 | 72 |
return *this; |
| 74 | 73 |
} |
| 75 | 74 |
|
| 76 | 75 |
ArgParser &ArgParser::doubleOption(const std::string &name, |
| 77 | 76 |
const std::string &help, |
| 78 | 77 |
double value, bool obl) |
| 79 | 78 |
{
|
| 80 | 79 |
ParData p; |
| 81 | 80 |
p.double_p=new double(value); |
| 82 | 81 |
p.self_delete=true; |
| 83 | 82 |
p.help=help; |
| 84 | 83 |
p.type=DOUBLE; |
| 85 | 84 |
p.mandatory=obl; |
| 86 | 85 |
_opts[name]=p; |
| 87 | 86 |
return *this; |
| 88 | 87 |
} |
| 89 | 88 |
|
| 90 | 89 |
ArgParser &ArgParser::boolOption(const std::string &name, |
| 91 | 90 |
const std::string &help, |
| 92 | 91 |
bool value, bool obl) |
| 93 | 92 |
{
|
| 94 | 93 |
ParData p; |
| 95 | 94 |
p.bool_p=new bool(value); |
| 96 | 95 |
p.self_delete=true; |
| 97 | 96 |
p.help=help; |
| 98 | 97 |
p.type=BOOL; |
| 99 | 98 |
p.mandatory=obl; |
| 100 | 99 |
_opts[name]=p; |
| 101 | 100 |
return *this; |
| 102 | 101 |
} |
| 103 | 102 |
|
| 104 | 103 |
ArgParser &ArgParser::stringOption(const std::string &name, |
| 105 | 104 |
const std::string &help, |
| 106 | 105 |
std::string value, bool obl) |
| 107 | 106 |
{
|
| 108 | 107 |
ParData p; |
| 109 | 108 |
p.string_p=new std::string(value); |
| 110 | 109 |
p.self_delete=true; |
| 111 | 110 |
p.help=help; |
| 112 | 111 |
p.type=STRING; |
| 113 | 112 |
p.mandatory=obl; |
| 114 | 113 |
_opts[name]=p; |
| 115 | 114 |
return *this; |
| 116 | 115 |
} |
| 117 | 116 |
|
| 118 | 117 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 119 | 118 |
const std::string &help, |
| 120 | 119 |
int &ref, bool obl) |
| 121 | 120 |
{
|
| 122 | 121 |
ParData p; |
| 123 | 122 |
p.int_p=&ref; |
| 124 | 123 |
p.self_delete=false; |
| 125 | 124 |
p.help=help; |
| 126 | 125 |
p.type=INTEGER; |
| 127 | 126 |
p.mandatory=obl; |
| 128 | 127 |
_opts[name]=p; |
| 129 | 128 |
return *this; |
| 130 | 129 |
} |
| 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_ARG_PARSER_H |
| 20 | 20 |
#define LEMON_ARG_PARSER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <map> |
| 24 | 24 |
#include <list> |
| 25 | 25 |
#include <string> |
| 26 | 26 |
#include <iostream> |
| 27 | 27 |
#include <sstream> |
| 28 | 28 |
#include <algorithm> |
| 29 | 29 |
#include <lemon/assert.h> |
| 30 | 30 |
|
| 31 | 31 |
///\ingroup misc |
| 32 | 32 |
///\file |
| 33 | 33 |
///\brief A tool to parse command line arguments. |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
///Command line arguments parser |
| 38 | 38 |
|
| 39 | 39 |
///\ingroup misc |
| 40 | 40 |
///Command line arguments parser. |
| 41 | 41 |
/// |
| 42 | 42 |
///For a complete example see the \ref arg_parser_demo.cc demo file. |
| 43 | 43 |
class ArgParser {
|
| 44 | 44 |
|
| 45 | 45 |
static void _showHelp(void *p); |
| 46 | 46 |
protected: |
| 47 | 47 |
|
| 48 | 48 |
int _argc; |
| 49 |
const char **_argv; |
|
| 49 |
const char * const *_argv; |
|
| 50 | 50 |
|
| 51 | 51 |
enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
|
| 52 | 52 |
|
| 53 | 53 |
class ParData {
|
| 54 | 54 |
public: |
| 55 | 55 |
union {
|
| 56 | 56 |
bool *bool_p; |
| 57 | 57 |
int *int_p; |
| 58 | 58 |
double *double_p; |
| 59 | 59 |
std::string *string_p; |
| 60 | 60 |
struct {
|
| 61 | 61 |
void (*p)(void *); |
| 62 | 62 |
void *data; |
| 63 | 63 |
} func_p; |
| 64 | 64 |
|
| 65 | 65 |
}; |
| 66 | 66 |
std::string help; |
| 67 | 67 |
bool mandatory; |
| 68 | 68 |
OptType type; |
| 69 | 69 |
bool set; |
| 70 | 70 |
bool ingroup; |
| 71 | 71 |
bool has_syn; |
| 72 | 72 |
bool syn; |
| 73 | 73 |
bool self_delete; |
| 74 | 74 |
ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false), |
| 75 | 75 |
has_syn(false), syn(false), self_delete(false) {}
|
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
typedef std::map<std::string,ParData> Opts; |
| 79 | 79 |
Opts _opts; |
| 80 | 80 |
|
| 81 | 81 |
class GroupData |
| 82 | 82 |
{
|
| 83 | 83 |
public: |
| 84 | 84 |
typedef std::list<std::string> Opts; |
| 85 | 85 |
Opts opts; |
| 86 | 86 |
bool only_one; |
| 87 | 87 |
bool mandatory; |
| 88 | 88 |
GroupData() :only_one(false), mandatory(false) {}
|
| 89 | 89 |
}; |
| 90 | 90 |
|
| 91 | 91 |
typedef std::map<std::string,GroupData> Groups; |
| 92 | 92 |
Groups _groups; |
| 93 | 93 |
|
| 94 | 94 |
struct OtherArg |
| 95 | 95 |
{
|
| 96 | 96 |
std::string name; |
| 97 | 97 |
std::string help; |
| 98 | 98 |
OtherArg(std::string n, std::string h) :name(n), help(h) {}
|
| 99 | 99 |
|
| 100 | 100 |
}; |
| 101 | 101 |
|
| 102 | 102 |
std::vector<OtherArg> _others_help; |
| 103 | 103 |
std::vector<std::string> _file_args; |
| 104 | 104 |
std::string _command_name; |
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
private: |
| 108 | 108 |
//Bind a function to an option. |
| 109 | 109 |
|
| 110 | 110 |
//\param name The name of the option. The leading '-' must be omitted. |
| 111 | 111 |
//\param help A help string. |
| 112 | 112 |
//\retval func The function to be called when the option is given. It |
| 113 | 113 |
// must be of type "void f(void *)" |
| 114 | 114 |
//\param data Data to be passed to \c func |
| 115 | 115 |
ArgParser &funcOption(const std::string &name, |
| 116 | 116 |
const std::string &help, |
| 117 | 117 |
void (*func)(void *),void *data); |
| 118 | 118 |
|
| 119 | 119 |
public: |
| 120 | 120 |
|
| 121 | 121 |
///Constructor |
| 122 |
ArgParser(int argc, const char **argv); |
|
| 122 |
ArgParser(int argc, const char * const *argv); |
|
| 123 | 123 |
|
| 124 | 124 |
~ArgParser(); |
| 125 | 125 |
|
| 126 | 126 |
///\name Options |
| 127 | 127 |
/// |
| 128 | 128 |
|
| 129 | 129 |
///@{
|
| 130 | 130 |
|
| 131 | 131 |
///Add a new integer type option |
| 132 | 132 |
|
| 133 | 133 |
///Add a new integer type option. |
| 134 | 134 |
///\param name The name of the option. The leading '-' must be omitted. |
| 135 | 135 |
///\param help A help string. |
| 136 | 136 |
///\param value A default value for the option. |
| 137 | 137 |
///\param obl Indicate if the option is mandatory. |
| 138 | 138 |
ArgParser &intOption(const std::string &name, |
| 139 | 139 |
const std::string &help, |
| 140 | 140 |
int value=0, bool obl=false); |
| 141 | 141 |
|
| 142 | 142 |
///Add a new floating point type option |
| 143 | 143 |
|
| 144 | 144 |
///Add a new floating point type option. |
| 145 | 145 |
///\param name The name of the option. The leading '-' must be omitted. |
| 146 | 146 |
///\param help A help string. |
| 147 | 147 |
///\param value A default value for the option. |
| 148 | 148 |
///\param obl Indicate if the option is mandatory. |
| 149 | 149 |
ArgParser &doubleOption(const std::string &name, |
| 150 | 150 |
const std::string &help, |
| 151 | 151 |
double value=0, bool obl=false); |
| 152 | 152 |
|
| 153 | 153 |
///Add a new bool type option |
| 154 | 154 |
|
| 155 | 155 |
///Add a new bool type option. |
| 156 | 156 |
///\param name The name of the option. The leading '-' must be omitted. |
| 157 | 157 |
///\param help A help string. |
| 158 | 158 |
///\param value A default value for the option. |
| 159 | 159 |
///\param obl Indicate if the option is mandatory. |
| 160 | 160 |
///\note A mandatory bool obtion is of very little use. |
| 161 | 161 |
ArgParser &boolOption(const std::string &name, |
| 162 | 162 |
const std::string &help, |
| 163 | 163 |
bool value=false, bool obl=false); |
| 164 | 164 |
|
| 165 | 165 |
///Add a new string type option |
| 166 | 166 |
|
| 167 | 167 |
///Add a new string type option. |
| 168 | 168 |
///\param name The name of the option. The leading '-' must be omitted. |
| 169 | 169 |
///\param help A help string. |
| 170 | 170 |
///\param value A default value for the option. |
| 171 | 171 |
///\param obl Indicate if the option is mandatory. |
| 172 | 172 |
ArgParser &stringOption(const std::string &name, |
| 173 | 173 |
const std::string &help, |
| 174 | 174 |
std::string value="", bool obl=false); |
| 175 | 175 |
|
| 176 | 176 |
///Give help string for non-parsed arguments. |
| 177 | 177 |
|
| 178 | 178 |
///With this function you can give help string for non-parsed arguments. |
| 179 | 179 |
///The parameter \c name will be printed in the short usage line, while |
| 180 | 180 |
///\c help gives a more detailed description. |
| 181 | 181 |
ArgParser &other(const std::string &name, |
| 182 | 182 |
const std::string &help=""); |
| 183 | 183 |
|
| 184 | 184 |
///@} |
| 185 | 185 |
|
| 186 | 186 |
///\name Options with External Storage |
| 187 | 187 |
///Using this functions, the value of the option will be directly written |
| 188 | 188 |
///into a variable once the option appears in the command line. |
| 189 | 189 |
|
| 190 | 190 |
///@{
|
| 191 | 191 |
|
| 192 | 192 |
///Add a new integer type option with a storage reference |
| 193 | 193 |
|
| 194 | 194 |
///Add a new integer type option with a storage reference. |
| 195 | 195 |
///\param name The name of the option. The leading '-' must be omitted. |
| 196 | 196 |
///\param help A help string. |
| 197 | 197 |
///\param obl Indicate if the option is mandatory. |
| 198 | 198 |
///\retval ref The value of the argument will be written to this variable. |
| 199 | 199 |
ArgParser &refOption(const std::string &name, |
| 200 | 200 |
const std::string &help, |
| 201 | 201 |
int &ref, bool obl=false); |
| 202 | 202 |
|
| 203 | 203 |
///Add a new floating type option with a storage reference |
| 204 | 204 |
|
| 205 | 205 |
///Add a new floating type option with a storage reference. |
| 206 | 206 |
///\param name The name of the option. The leading '-' must be omitted. |
| 207 | 207 |
///\param help A help string. |
| 208 | 208 |
///\param obl Indicate if the option is mandatory. |
| 209 | 209 |
///\retval ref The value of the argument will be written to this variable. |
| 210 | 210 |
ArgParser &refOption(const std::string &name, |
| 211 | 211 |
const std::string &help, |
| 212 | 212 |
double &ref, bool obl=false); |
| 213 | 213 |
|
| 214 | 214 |
///Add a new bool type option with a storage reference |
| 215 | 215 |
|
| 216 | 216 |
///Add a new bool type option with a storage reference. |
| 217 | 217 |
///\param name The name of the option. The leading '-' must be omitted. |
| 218 | 218 |
///\param help A help string. |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BFS_H |
| 20 | 20 |
#define LEMON_BFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief BFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/path.h> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
///Default traits class of Bfs class. |
| 36 | 36 |
|
| 37 | 37 |
///Default traits class of Bfs class. |
| 38 | 38 |
///\tparam GR Digraph type. |
| 39 | 39 |
template<class GR> |
| 40 | 40 |
struct BfsDefaultTraits |
| 41 | 41 |
{
|
| 42 | 42 |
///The type of the digraph the algorithm runs on. |
| 43 | 43 |
typedef GR Digraph; |
| 44 | 44 |
|
| 45 | 45 |
///\brief The type of the map that stores the predecessor |
| 46 | 46 |
///arcs of the shortest paths. |
| 47 | 47 |
/// |
| 48 | 48 |
///The type of the map that stores the predecessor |
| 49 | 49 |
///arcs of the shortest paths. |
| 50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 52 |
///Instantiates a |
|
| 52 |
///Instantiates a PredMap. |
|
| 53 | 53 |
|
| 54 |
///This function instantiates a |
|
| 54 |
///This function instantiates a PredMap. |
|
| 55 | 55 |
///\param g is the digraph, to which we would like to define the |
| 56 |
/// |
|
| 56 |
///PredMap. |
|
| 57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
| 58 | 58 |
{
|
| 59 | 59 |
return new PredMap(g); |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
///The type of the map that indicates which nodes are processed. |
| 63 | 63 |
|
| 64 | 64 |
///The type of the map that indicates which nodes are processed. |
| 65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 67 |
///Instantiates a |
|
| 67 |
///Instantiates a ProcessedMap. |
|
| 68 | 68 |
|
| 69 |
///This function instantiates a |
|
| 69 |
///This function instantiates a ProcessedMap. |
|
| 70 | 70 |
///\param g is the digraph, to which |
| 71 |
///we would like to define the |
|
| 71 |
///we would like to define the ProcessedMap |
|
| 72 | 72 |
#ifdef DOXYGEN |
| 73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 74 | 74 |
#else |
| 75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 76 | 76 |
#endif |
| 77 | 77 |
{
|
| 78 | 78 |
return new ProcessedMap(); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
///The type of the map that indicates which nodes are reached. |
| 82 | 82 |
|
| 83 | 83 |
///The type of the map that indicates which nodes are reached. |
| 84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 86 |
///Instantiates a |
|
| 86 |
///Instantiates a ReachedMap. |
|
| 87 | 87 |
|
| 88 |
///This function instantiates a |
|
| 88 |
///This function instantiates a ReachedMap. |
|
| 89 | 89 |
///\param g is the digraph, to which |
| 90 |
///we would like to define the |
|
| 90 |
///we would like to define the ReachedMap. |
|
| 91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 92 | 92 |
{
|
| 93 | 93 |
return new ReachedMap(g); |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
///The type of the map that stores the distances of the nodes. |
| 97 | 97 |
|
| 98 | 98 |
///The type of the map that stores the distances of the nodes. |
| 99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 101 |
///Instantiates a |
|
| 101 |
///Instantiates a DistMap. |
|
| 102 | 102 |
|
| 103 |
///This function instantiates a |
|
| 103 |
///This function instantiates a DistMap. |
|
| 104 | 104 |
///\param g is the digraph, to which we would like to define the |
| 105 |
/// |
|
| 105 |
///DistMap. |
|
| 106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
| 107 | 107 |
{
|
| 108 | 108 |
return new DistMap(g); |
| 109 | 109 |
} |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
///%BFS algorithm class. |
| 113 | 113 |
|
| 114 | 114 |
///\ingroup search |
| 115 | 115 |
///This class provides an efficient implementation of the %BFS algorithm. |
| 116 | 116 |
/// |
| 117 | 117 |
///There is also a \ref bfs() "function-type interface" for the BFS |
| 118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
| 119 | 119 |
///used easier. |
| 120 | 120 |
/// |
| 121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 122 | 122 |
///The default value is \ref ListDigraph. The value of GR is not used |
| 123 | 123 |
///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits. |
| 124 | 124 |
///\tparam TR Traits class to set various data types used by the algorithm. |
| 125 | 125 |
///The default traits class is |
| 126 | 126 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
| 127 | 127 |
///See \ref BfsDefaultTraits for the documentation of |
| 128 | 128 |
///a Bfs traits class. |
| 129 | 129 |
#ifdef DOXYGEN |
| 130 | 130 |
template <typename GR, |
| 131 | 131 |
typename TR> |
| 132 | 132 |
#else |
| 133 | 133 |
template <typename GR=ListDigraph, |
| 134 | 134 |
typename TR=BfsDefaultTraits<GR> > |
| 135 | 135 |
#endif |
| 136 | 136 |
class Bfs {
|
| 137 | 137 |
public: |
| 138 | 138 |
|
| 139 | 139 |
///The type of the digraph the algorithm runs on. |
| 140 | 140 |
typedef typename TR::Digraph Digraph; |
| 141 | 141 |
|
| 142 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
| 143 | 143 |
///shortest paths. |
| 144 | 144 |
typedef typename TR::PredMap PredMap; |
| 145 | 145 |
///The type of the map that stores the distances of the nodes. |
| 146 | 146 |
typedef typename TR::DistMap DistMap; |
| 147 | 147 |
///The type of the map that indicates which nodes are reached. |
| 148 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
| 149 | 149 |
///The type of the map that indicates which nodes are processed. |
| 150 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 151 | 151 |
///The type of the paths. |
| 152 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 153 | 153 |
|
| 154 | 154 |
///The traits class. |
| 155 | 155 |
typedef TR Traits; |
| 156 | 156 |
|
| 157 | 157 |
private: |
| 158 | 158 |
|
| 159 | 159 |
typedef typename Digraph::Node Node; |
| 160 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
| 161 | 161 |
typedef typename Digraph::Arc Arc; |
| 162 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 163 | 163 |
|
| 164 | 164 |
//Pointer to the underlying digraph. |
| 165 | 165 |
const Digraph *G; |
| 166 | 166 |
//Pointer to the map of predecessor arcs. |
| 167 | 167 |
PredMap *_pred; |
| 168 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
| 169 | 169 |
bool local_pred; |
| 170 | 170 |
//Pointer to the map of distances. |
| 171 | 171 |
DistMap *_dist; |
| 172 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
| 173 | 173 |
bool local_dist; |
| 174 | 174 |
//Pointer to the map of reached status of the nodes. |
| 175 | 175 |
ReachedMap *_reached; |
| 176 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
| 177 | 177 |
bool local_reached; |
| 178 | 178 |
//Pointer to the map of processed status of the nodes. |
| 179 | 179 |
ProcessedMap *_processed; |
| 180 | 180 |
//Indicates if _processed is locally allocated (true) or not. |
| 181 | 181 |
bool local_processed; |
| 182 | 182 |
|
| 183 | 183 |
std::vector<typename Digraph::Node> _queue; |
| 184 | 184 |
int _queue_head,_queue_tail,_queue_next_dist; |
| 185 | 185 |
int _curr_dist; |
| 186 | 186 |
|
| 187 | 187 |
//Creates the maps if necessary. |
| 188 | 188 |
void create_maps() |
| 189 | 189 |
{
|
| 190 | 190 |
if(!_pred) {
|
| 191 | 191 |
local_pred = true; |
| 192 | 192 |
_pred = Traits::createPredMap(*G); |
| 193 | 193 |
} |
| 194 | 194 |
if(!_dist) {
|
| 195 | 195 |
local_dist = true; |
| 196 | 196 |
_dist = Traits::createDistMap(*G); |
| 197 | 197 |
} |
| 198 | 198 |
if(!_reached) {
|
| 199 | 199 |
local_reached = true; |
| 200 | 200 |
_reached = Traits::createReachedMap(*G); |
| 201 | 201 |
} |
| 202 | 202 |
if(!_processed) {
|
| 203 | 203 |
local_processed = true; |
| 204 | 204 |
_processed = Traits::createProcessedMap(*G); |
| 205 | 205 |
} |
| 206 | 206 |
} |
| 207 | 207 |
|
| 208 | 208 |
protected: |
| 209 | 209 |
|
| 210 | 210 |
Bfs() {}
|
| 211 | 211 |
|
| 212 | 212 |
public: |
| 213 | 213 |
|
| 214 | 214 |
typedef Bfs Create; |
| 215 | 215 |
|
| 216 | 216 |
///\name Named template parameters |
| 217 | 217 |
|
| 218 | 218 |
///@{
|
| 219 | 219 |
|
| 220 | 220 |
template <class T> |
| 221 | 221 |
struct SetPredMapTraits : public Traits {
|
| 222 | 222 |
typedef T PredMap; |
| 223 | 223 |
static PredMap *createPredMap(const Digraph &) |
| 224 | 224 |
{
|
| 225 | 225 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 226 | 226 |
return 0; // ignore warnings |
| 227 | 227 |
} |
| 228 | 228 |
}; |
| 229 | 229 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 230 |
/// |
|
| 230 |
///PredMap type. |
|
| 231 | 231 |
/// |
| 232 | 232 |
///\ref named-templ-param "Named parameter" for setting |
| 233 |
/// |
|
| 233 |
///PredMap type. |
|
| 234 | 234 |
template <class T> |
| 235 | 235 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
|
| 236 | 236 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
| 237 | 237 |
}; |
| 238 | 238 |
|
| 239 | 239 |
template <class T> |
| 240 | 240 |
struct SetDistMapTraits : public Traits {
|
| 241 | 241 |
typedef T DistMap; |
| 242 | 242 |
static DistMap *createDistMap(const Digraph &) |
| 243 | 243 |
{
|
| 244 | 244 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 245 | 245 |
return 0; // ignore warnings |
| 246 | 246 |
} |
| 247 | 247 |
}; |
| 248 | 248 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 249 |
/// |
|
| 249 |
///DistMap type. |
|
| 250 | 250 |
/// |
| 251 | 251 |
///\ref named-templ-param "Named parameter" for setting |
| 252 |
/// |
|
| 252 |
///DistMap type. |
|
| 253 | 253 |
template <class T> |
| 254 | 254 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
|
| 255 | 255 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
| 256 | 256 |
}; |
| 257 | 257 |
|
| 258 | 258 |
template <class T> |
| 259 | 259 |
struct SetReachedMapTraits : public Traits {
|
| 260 | 260 |
typedef T ReachedMap; |
| 261 | 261 |
static ReachedMap *createReachedMap(const Digraph &) |
| 262 | 262 |
{
|
| 263 | 263 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 264 | 264 |
return 0; // ignore warnings |
| 265 | 265 |
} |
| 266 | 266 |
}; |
| 267 | 267 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 268 |
/// |
|
| 268 |
///ReachedMap type. |
|
| 269 | 269 |
/// |
| 270 | 270 |
///\ref named-templ-param "Named parameter" for setting |
| 271 |
/// |
|
| 271 |
///ReachedMap type. |
|
| 272 | 272 |
template <class T> |
| 273 | 273 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
|
| 274 | 274 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
| 275 | 275 |
}; |
| 276 | 276 |
|
| 277 | 277 |
template <class T> |
| 278 | 278 |
struct SetProcessedMapTraits : public Traits {
|
| 279 | 279 |
typedef T ProcessedMap; |
| 280 | 280 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 281 | 281 |
{
|
| 282 | 282 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 283 | 283 |
return 0; // ignore warnings |
| 284 | 284 |
} |
| 285 | 285 |
}; |
| 286 | 286 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 287 |
/// |
|
| 287 |
///ProcessedMap type. |
|
| 288 | 288 |
/// |
| 289 | 289 |
///\ref named-templ-param "Named parameter" for setting |
| 290 |
/// |
|
| 290 |
///ProcessedMap type. |
|
| 291 | 291 |
template <class T> |
| 292 | 292 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
|
| 293 | 293 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
| 294 | 294 |
}; |
| 295 | 295 |
|
| 296 | 296 |
struct SetStandardProcessedMapTraits : public Traits {
|
| 297 | 297 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
| 298 | 298 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 299 | 299 |
{
|
| 300 | 300 |
return new ProcessedMap(g); |
| 301 | 301 |
return 0; // ignore warnings |
| 302 | 302 |
} |
| 303 | 303 |
}; |
| 304 | 304 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 305 |
/// |
|
| 305 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 306 | 306 |
/// |
| 307 | 307 |
///\ref named-templ-param "Named parameter" for setting |
| 308 |
/// |
|
| 308 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 309 | 309 |
///If you don't set it explicitly, it will be automatically allocated. |
| 310 | 310 |
struct SetStandardProcessedMap : |
| 311 | 311 |
public Bfs< Digraph, SetStandardProcessedMapTraits > {
|
| 312 | 312 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
| 313 | 313 |
}; |
| 314 | 314 |
|
| 315 | 315 |
///@} |
| 316 | 316 |
|
| 317 | 317 |
public: |
| 318 | 318 |
|
| 319 | 319 |
///Constructor. |
| 320 | 320 |
|
| 321 | 321 |
///Constructor. |
| 322 | 322 |
///\param g The digraph the algorithm runs on. |
| 323 | 323 |
Bfs(const Digraph &g) : |
| 324 | 324 |
G(&g), |
| 325 | 325 |
_pred(NULL), local_pred(false), |
| 326 | 326 |
_dist(NULL), local_dist(false), |
| 327 | 327 |
_reached(NULL), local_reached(false), |
| 328 | 328 |
_processed(NULL), local_processed(false) |
| 329 | 329 |
{ }
|
| 330 | 330 |
|
| 331 | 331 |
///Destructor. |
| 332 | 332 |
~Bfs() |
| 333 | 333 |
{
|
| 334 | 334 |
if(local_pred) delete _pred; |
| 335 | 335 |
if(local_dist) delete _dist; |
| 336 | 336 |
if(local_reached) delete _reached; |
| 337 | 337 |
if(local_processed) delete _processed; |
| 338 | 338 |
} |
| 339 | 339 |
|
| 340 | 340 |
///Sets the map that stores the predecessor arcs. |
| 341 | 341 |
|
| 342 | 342 |
///Sets the map that stores the predecessor arcs. |
| 343 | 343 |
///If you don't use this function before calling \ref run(), |
| 344 | 344 |
///it will allocate one. The destructor deallocates this |
| 345 | 345 |
///automatically allocated map, of course. |
| 346 | 346 |
///\return <tt> (*this) </tt> |
| 347 | 347 |
Bfs &predMap(PredMap &m) |
| 348 | 348 |
{
|
| 349 | 349 |
if(local_pred) {
|
| 350 | 350 |
delete _pred; |
| 351 | 351 |
local_pred=false; |
| 352 | 352 |
} |
| 353 | 353 |
_pred = &m; |
| 354 | 354 |
return *this; |
| 355 | 355 |
} |
| 356 | 356 |
|
| 357 | 357 |
///Sets the map that indicates which nodes are reached. |
| 358 | 358 |
|
| 359 | 359 |
///Sets the map that indicates which nodes are reached. |
| 360 | 360 |
///If you don't use this function before calling \ref run(), |
| 361 | 361 |
///it will allocate one. The destructor deallocates this |
| 362 | 362 |
///automatically allocated map, of course. |
| 363 | 363 |
///\return <tt> (*this) </tt> |
| 364 | 364 |
Bfs &reachedMap(ReachedMap &m) |
| 365 | 365 |
{
|
| 366 | 366 |
if(local_reached) {
|
| 367 | 367 |
delete _reached; |
| 368 | 368 |
local_reached=false; |
| 369 | 369 |
} |
| 370 | 370 |
_reached = &m; |
| 371 | 371 |
return *this; |
| 372 | 372 |
} |
| 373 | 373 |
|
| 374 | 374 |
///Sets the map that indicates which nodes are processed. |
| 375 | 375 |
|
| 376 | 376 |
///Sets the map that indicates which nodes are processed. |
| 377 | 377 |
///If you don't use this function before calling \ref run(), |
| 378 | 378 |
///it will allocate one. The destructor deallocates this |
| 379 | 379 |
///automatically allocated map, of course. |
| 380 | 380 |
///\return <tt> (*this) </tt> |
| 381 | 381 |
Bfs &processedMap(ProcessedMap &m) |
| 382 | 382 |
{
|
| 383 | 383 |
if(local_processed) {
|
| 384 | 384 |
delete _processed; |
| 385 | 385 |
local_processed=false; |
| 386 | 386 |
} |
| 387 | 387 |
_processed = &m; |
| 388 | 388 |
return *this; |
| 389 | 389 |
} |
| 390 | 390 |
|
| 391 | 391 |
///Sets the map that stores the distances of the nodes. |
| 392 | 392 |
|
| 393 | 393 |
///Sets the map that stores the distances of the nodes calculated by |
| 394 | 394 |
///the algorithm. |
| 395 | 395 |
///If you don't use this function before calling \ref run(), |
| 396 | 396 |
///it will allocate one. The destructor deallocates this |
| 397 | 397 |
///automatically allocated map, of course. |
| 398 | 398 |
///\return <tt> (*this) </tt> |
| 399 | 399 |
Bfs &distMap(DistMap &m) |
| 400 | 400 |
{
|
| 401 | 401 |
if(local_dist) {
|
| 402 | 402 |
delete _dist; |
| 403 | 403 |
local_dist=false; |
| 404 | 404 |
} |
| ... | ... |
@@ -742,629 +742,629 @@ |
| 742 | 742 |
|
| 743 | 743 |
///Returns the shortest path to a node. |
| 744 | 744 |
/// |
| 745 | 745 |
///\warning \c t should be reachable from the root(s). |
| 746 | 746 |
/// |
| 747 | 747 |
///\pre Either \ref run() or \ref start() must be called before |
| 748 | 748 |
///using this function. |
| 749 | 749 |
Path path(Node t) const { return Path(*G, *_pred, t); }
|
| 750 | 750 |
|
| 751 | 751 |
///The distance of a node from the root(s). |
| 752 | 752 |
|
| 753 | 753 |
///Returns the distance of a node from the root(s). |
| 754 | 754 |
/// |
| 755 | 755 |
///\warning If node \c v is not reachable from the root(s), then |
| 756 | 756 |
///the return value of this function is undefined. |
| 757 | 757 |
/// |
| 758 | 758 |
///\pre Either \ref run() or \ref start() must be called before |
| 759 | 759 |
///using this function. |
| 760 | 760 |
int dist(Node v) const { return (*_dist)[v]; }
|
| 761 | 761 |
|
| 762 | 762 |
///Returns the 'previous arc' of the shortest path tree for a node. |
| 763 | 763 |
|
| 764 | 764 |
///This function returns the 'previous arc' of the shortest path |
| 765 | 765 |
///tree for the node \c v, i.e. it returns the last arc of a |
| 766 | 766 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
| 767 | 767 |
///is not reachable from the root(s) or if \c v is a root. |
| 768 | 768 |
/// |
| 769 | 769 |
///The shortest path tree used here is equal to the shortest path |
| 770 | 770 |
///tree used in \ref predNode(). |
| 771 | 771 |
/// |
| 772 | 772 |
///\pre Either \ref run() or \ref start() must be called before |
| 773 | 773 |
///using this function. |
| 774 | 774 |
Arc predArc(Node v) const { return (*_pred)[v];}
|
| 775 | 775 |
|
| 776 | 776 |
///Returns the 'previous node' of the shortest path tree for a node. |
| 777 | 777 |
|
| 778 | 778 |
///This function returns the 'previous node' of the shortest path |
| 779 | 779 |
///tree for the node \c v, i.e. it returns the last but one node |
| 780 | 780 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
| 781 | 781 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
| 782 | 782 |
/// |
| 783 | 783 |
///The shortest path tree used here is equal to the shortest path |
| 784 | 784 |
///tree used in \ref predArc(). |
| 785 | 785 |
/// |
| 786 | 786 |
///\pre Either \ref run() or \ref start() must be called before |
| 787 | 787 |
///using this function. |
| 788 | 788 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
|
| 789 | 789 |
G->source((*_pred)[v]); } |
| 790 | 790 |
|
| 791 | 791 |
///\brief Returns a const reference to the node map that stores the |
| 792 | 792 |
/// distances of the nodes. |
| 793 | 793 |
/// |
| 794 | 794 |
///Returns a const reference to the node map that stores the distances |
| 795 | 795 |
///of the nodes calculated by the algorithm. |
| 796 | 796 |
/// |
| 797 | 797 |
///\pre Either \ref run() or \ref init() |
| 798 | 798 |
///must be called before using this function. |
| 799 | 799 |
const DistMap &distMap() const { return *_dist;}
|
| 800 | 800 |
|
| 801 | 801 |
///\brief Returns a const reference to the node map that stores the |
| 802 | 802 |
///predecessor arcs. |
| 803 | 803 |
/// |
| 804 | 804 |
///Returns a const reference to the node map that stores the predecessor |
| 805 | 805 |
///arcs, which form the shortest path tree. |
| 806 | 806 |
/// |
| 807 | 807 |
///\pre Either \ref run() or \ref init() |
| 808 | 808 |
///must be called before using this function. |
| 809 | 809 |
const PredMap &predMap() const { return *_pred;}
|
| 810 | 810 |
|
| 811 | 811 |
///Checks if a node is reachable from the root(s). |
| 812 | 812 |
|
| 813 | 813 |
///Returns \c true if \c v is reachable from the root(s). |
| 814 | 814 |
///\pre Either \ref run() or \ref start() |
| 815 | 815 |
///must be called before using this function. |
| 816 | 816 |
bool reached(Node v) const { return (*_reached)[v]; }
|
| 817 | 817 |
|
| 818 | 818 |
///@} |
| 819 | 819 |
}; |
| 820 | 820 |
|
| 821 | 821 |
///Default traits class of bfs() function. |
| 822 | 822 |
|
| 823 | 823 |
///Default traits class of bfs() function. |
| 824 | 824 |
///\tparam GR Digraph type. |
| 825 | 825 |
template<class GR> |
| 826 | 826 |
struct BfsWizardDefaultTraits |
| 827 | 827 |
{
|
| 828 | 828 |
///The type of the digraph the algorithm runs on. |
| 829 | 829 |
typedef GR Digraph; |
| 830 | 830 |
|
| 831 | 831 |
///\brief The type of the map that stores the predecessor |
| 832 | 832 |
///arcs of the shortest paths. |
| 833 | 833 |
/// |
| 834 | 834 |
///The type of the map that stores the predecessor |
| 835 | 835 |
///arcs of the shortest paths. |
| 836 | 836 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 837 | 837 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 838 |
///Instantiates a |
|
| 838 |
///Instantiates a PredMap. |
|
| 839 | 839 |
|
| 840 |
///This function instantiates a |
|
| 840 |
///This function instantiates a PredMap. |
|
| 841 | 841 |
///\param g is the digraph, to which we would like to define the |
| 842 |
/// |
|
| 842 |
///PredMap. |
|
| 843 | 843 |
static PredMap *createPredMap(const Digraph &g) |
| 844 | 844 |
{
|
| 845 | 845 |
return new PredMap(g); |
| 846 | 846 |
} |
| 847 | 847 |
|
| 848 | 848 |
///The type of the map that indicates which nodes are processed. |
| 849 | 849 |
|
| 850 | 850 |
///The type of the map that indicates which nodes are processed. |
| 851 | 851 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 852 | 852 |
///By default it is a NullMap. |
| 853 | 853 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 854 |
///Instantiates a |
|
| 854 |
///Instantiates a ProcessedMap. |
|
| 855 | 855 |
|
| 856 |
///This function instantiates a |
|
| 856 |
///This function instantiates a ProcessedMap. |
|
| 857 | 857 |
///\param g is the digraph, to which |
| 858 |
///we would like to define the |
|
| 858 |
///we would like to define the ProcessedMap. |
|
| 859 | 859 |
#ifdef DOXYGEN |
| 860 | 860 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 861 | 861 |
#else |
| 862 | 862 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 863 | 863 |
#endif |
| 864 | 864 |
{
|
| 865 | 865 |
return new ProcessedMap(); |
| 866 | 866 |
} |
| 867 | 867 |
|
| 868 | 868 |
///The type of the map that indicates which nodes are reached. |
| 869 | 869 |
|
| 870 | 870 |
///The type of the map that indicates which nodes are reached. |
| 871 | 871 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 872 | 872 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 873 |
///Instantiates a |
|
| 873 |
///Instantiates a ReachedMap. |
|
| 874 | 874 |
|
| 875 |
///This function instantiates a |
|
| 875 |
///This function instantiates a ReachedMap. |
|
| 876 | 876 |
///\param g is the digraph, to which |
| 877 |
///we would like to define the |
|
| 877 |
///we would like to define the ReachedMap. |
|
| 878 | 878 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 879 | 879 |
{
|
| 880 | 880 |
return new ReachedMap(g); |
| 881 | 881 |
} |
| 882 | 882 |
|
| 883 | 883 |
///The type of the map that stores the distances of the nodes. |
| 884 | 884 |
|
| 885 | 885 |
///The type of the map that stores the distances of the nodes. |
| 886 | 886 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 887 | 887 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 888 |
///Instantiates a |
|
| 888 |
///Instantiates a DistMap. |
|
| 889 | 889 |
|
| 890 |
///This function instantiates a |
|
| 890 |
///This function instantiates a DistMap. |
|
| 891 | 891 |
///\param g is the digraph, to which we would like to define |
| 892 |
///the |
|
| 892 |
///the DistMap |
|
| 893 | 893 |
static DistMap *createDistMap(const Digraph &g) |
| 894 | 894 |
{
|
| 895 | 895 |
return new DistMap(g); |
| 896 | 896 |
} |
| 897 | 897 |
|
| 898 | 898 |
///The type of the shortest paths. |
| 899 | 899 |
|
| 900 | 900 |
///The type of the shortest paths. |
| 901 | 901 |
///It must meet the \ref concepts::Path "Path" concept. |
| 902 | 902 |
typedef lemon::Path<Digraph> Path; |
| 903 | 903 |
}; |
| 904 | 904 |
|
| 905 |
/// Default traits class used by |
|
| 905 |
/// Default traits class used by BfsWizard |
|
| 906 | 906 |
|
| 907 | 907 |
/// To make it easier to use Bfs algorithm |
| 908 | 908 |
/// we have created a wizard class. |
| 909 | 909 |
/// This \ref BfsWizard class needs default traits, |
| 910 | 910 |
/// as well as the \ref Bfs class. |
| 911 | 911 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
| 912 | 912 |
/// \ref BfsWizard class. |
| 913 | 913 |
template<class GR> |
| 914 | 914 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
| 915 | 915 |
{
|
| 916 | 916 |
|
| 917 | 917 |
typedef BfsWizardDefaultTraits<GR> Base; |
| 918 | 918 |
protected: |
| 919 | 919 |
//The type of the nodes in the digraph. |
| 920 | 920 |
typedef typename Base::Digraph::Node Node; |
| 921 | 921 |
|
| 922 | 922 |
//Pointer to the digraph the algorithm runs on. |
| 923 | 923 |
void *_g; |
| 924 | 924 |
//Pointer to the map of reached nodes. |
| 925 | 925 |
void *_reached; |
| 926 | 926 |
//Pointer to the map of processed nodes. |
| 927 | 927 |
void *_processed; |
| 928 | 928 |
//Pointer to the map of predecessors arcs. |
| 929 | 929 |
void *_pred; |
| 930 | 930 |
//Pointer to the map of distances. |
| 931 | 931 |
void *_dist; |
| 932 | 932 |
//Pointer to the shortest path to the target node. |
| 933 | 933 |
void *_path; |
| 934 | 934 |
//Pointer to the distance of the target node. |
| 935 | 935 |
int *_di; |
| 936 | 936 |
|
| 937 | 937 |
public: |
| 938 | 938 |
/// Constructor. |
| 939 | 939 |
|
| 940 | 940 |
/// This constructor does not require parameters, therefore it initiates |
| 941 | 941 |
/// all of the attributes to \c 0. |
| 942 | 942 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
| 943 | 943 |
_dist(0), _path(0), _di(0) {}
|
| 944 | 944 |
|
| 945 | 945 |
/// Constructor. |
| 946 | 946 |
|
| 947 | 947 |
/// This constructor requires one parameter, |
| 948 | 948 |
/// others are initiated to \c 0. |
| 949 | 949 |
/// \param g The digraph the algorithm runs on. |
| 950 | 950 |
BfsWizardBase(const GR &g) : |
| 951 | 951 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
| 952 | 952 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
| 953 | 953 |
|
| 954 | 954 |
}; |
| 955 | 955 |
|
| 956 | 956 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
| 957 | 957 |
|
| 958 | 958 |
/// This auxiliary class is created to implement the |
| 959 | 959 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
| 960 | 960 |
/// It does not have own \ref run() method, it uses the functions |
| 961 | 961 |
/// and features of the plain \ref Bfs. |
| 962 | 962 |
/// |
| 963 | 963 |
/// This class should only be used through the \ref bfs() function, |
| 964 | 964 |
/// which makes it easier to use the algorithm. |
| 965 | 965 |
template<class TR> |
| 966 | 966 |
class BfsWizard : public TR |
| 967 | 967 |
{
|
| 968 | 968 |
typedef TR Base; |
| 969 | 969 |
|
| 970 | 970 |
///The type of the digraph the algorithm runs on. |
| 971 | 971 |
typedef typename TR::Digraph Digraph; |
| 972 | 972 |
|
| 973 | 973 |
typedef typename Digraph::Node Node; |
| 974 | 974 |
typedef typename Digraph::NodeIt NodeIt; |
| 975 | 975 |
typedef typename Digraph::Arc Arc; |
| 976 | 976 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 977 | 977 |
|
| 978 | 978 |
///\brief The type of the map that stores the predecessor |
| 979 | 979 |
///arcs of the shortest paths. |
| 980 | 980 |
typedef typename TR::PredMap PredMap; |
| 981 | 981 |
///\brief The type of the map that stores the distances of the nodes. |
| 982 | 982 |
typedef typename TR::DistMap DistMap; |
| 983 | 983 |
///\brief The type of the map that indicates which nodes are reached. |
| 984 | 984 |
typedef typename TR::ReachedMap ReachedMap; |
| 985 | 985 |
///\brief The type of the map that indicates which nodes are processed. |
| 986 | 986 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 987 | 987 |
///The type of the shortest paths |
| 988 | 988 |
typedef typename TR::Path Path; |
| 989 | 989 |
|
| 990 | 990 |
public: |
| 991 | 991 |
|
| 992 | 992 |
/// Constructor. |
| 993 | 993 |
BfsWizard() : TR() {}
|
| 994 | 994 |
|
| 995 | 995 |
/// Constructor that requires parameters. |
| 996 | 996 |
|
| 997 | 997 |
/// Constructor that requires parameters. |
| 998 | 998 |
/// These parameters will be the default values for the traits class. |
| 999 | 999 |
/// \param g The digraph the algorithm runs on. |
| 1000 | 1000 |
BfsWizard(const Digraph &g) : |
| 1001 | 1001 |
TR(g) {}
|
| 1002 | 1002 |
|
| 1003 | 1003 |
///Copy constructor |
| 1004 | 1004 |
BfsWizard(const TR &b) : TR(b) {}
|
| 1005 | 1005 |
|
| 1006 | 1006 |
~BfsWizard() {}
|
| 1007 | 1007 |
|
| 1008 | 1008 |
///Runs BFS algorithm from the given source node. |
| 1009 | 1009 |
|
| 1010 | 1010 |
///This method runs BFS algorithm from node \c s |
| 1011 | 1011 |
///in order to compute the shortest path to each node. |
| 1012 | 1012 |
void run(Node s) |
| 1013 | 1013 |
{
|
| 1014 | 1014 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 1015 | 1015 |
if (Base::_pred) |
| 1016 | 1016 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1017 | 1017 |
if (Base::_dist) |
| 1018 | 1018 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1019 | 1019 |
if (Base::_reached) |
| 1020 | 1020 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 1021 | 1021 |
if (Base::_processed) |
| 1022 | 1022 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1023 | 1023 |
if (s!=INVALID) |
| 1024 | 1024 |
alg.run(s); |
| 1025 | 1025 |
else |
| 1026 | 1026 |
alg.run(); |
| 1027 | 1027 |
} |
| 1028 | 1028 |
|
| 1029 | 1029 |
///Finds the shortest path between \c s and \c t. |
| 1030 | 1030 |
|
| 1031 | 1031 |
///This method runs BFS algorithm from node \c s |
| 1032 | 1032 |
///in order to compute the shortest path to node \c t |
| 1033 | 1033 |
///(it stops searching when \c t is processed). |
| 1034 | 1034 |
/// |
| 1035 | 1035 |
///\return \c true if \c t is reachable form \c s. |
| 1036 | 1036 |
bool run(Node s, Node t) |
| 1037 | 1037 |
{
|
| 1038 | 1038 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 1039 | 1039 |
if (Base::_pred) |
| 1040 | 1040 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1041 | 1041 |
if (Base::_dist) |
| 1042 | 1042 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1043 | 1043 |
if (Base::_reached) |
| 1044 | 1044 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 1045 | 1045 |
if (Base::_processed) |
| 1046 | 1046 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1047 | 1047 |
alg.run(s,t); |
| 1048 | 1048 |
if (Base::_path) |
| 1049 | 1049 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
| 1050 | 1050 |
if (Base::_di) |
| 1051 | 1051 |
*Base::_di = alg.dist(t); |
| 1052 | 1052 |
return alg.reached(t); |
| 1053 | 1053 |
} |
| 1054 | 1054 |
|
| 1055 | 1055 |
///Runs BFS algorithm to visit all nodes in the digraph. |
| 1056 | 1056 |
|
| 1057 | 1057 |
///This method runs BFS algorithm in order to compute |
| 1058 | 1058 |
///the shortest path to each node. |
| 1059 | 1059 |
void run() |
| 1060 | 1060 |
{
|
| 1061 | 1061 |
run(INVALID); |
| 1062 | 1062 |
} |
| 1063 | 1063 |
|
| 1064 | 1064 |
template<class T> |
| 1065 | 1065 |
struct SetPredMapBase : public Base {
|
| 1066 | 1066 |
typedef T PredMap; |
| 1067 | 1067 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1068 | 1068 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1069 | 1069 |
}; |
| 1070 | 1070 |
///\brief \ref named-func-param "Named parameter" |
| 1071 |
///for setting |
|
| 1071 |
///for setting PredMap object. |
|
| 1072 | 1072 |
/// |
| 1073 | 1073 |
///\ref named-func-param "Named parameter" |
| 1074 |
///for setting |
|
| 1074 |
///for setting PredMap object. |
|
| 1075 | 1075 |
template<class T> |
| 1076 | 1076 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
| 1077 | 1077 |
{
|
| 1078 | 1078 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1079 | 1079 |
return BfsWizard<SetPredMapBase<T> >(*this); |
| 1080 | 1080 |
} |
| 1081 | 1081 |
|
| 1082 | 1082 |
template<class T> |
| 1083 | 1083 |
struct SetReachedMapBase : public Base {
|
| 1084 | 1084 |
typedef T ReachedMap; |
| 1085 | 1085 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; };
|
| 1086 | 1086 |
SetReachedMapBase(const TR &b) : TR(b) {}
|
| 1087 | 1087 |
}; |
| 1088 | 1088 |
///\brief \ref named-func-param "Named parameter" |
| 1089 |
///for setting |
|
| 1089 |
///for setting ReachedMap object. |
|
| 1090 | 1090 |
/// |
| 1091 | 1091 |
/// \ref named-func-param "Named parameter" |
| 1092 |
///for setting |
|
| 1092 |
///for setting ReachedMap object. |
|
| 1093 | 1093 |
template<class T> |
| 1094 | 1094 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
| 1095 | 1095 |
{
|
| 1096 | 1096 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1097 | 1097 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
| 1098 | 1098 |
} |
| 1099 | 1099 |
|
| 1100 | 1100 |
template<class T> |
| 1101 | 1101 |
struct SetDistMapBase : public Base {
|
| 1102 | 1102 |
typedef T DistMap; |
| 1103 | 1103 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1104 | 1104 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1105 | 1105 |
}; |
| 1106 | 1106 |
///\brief \ref named-func-param "Named parameter" |
| 1107 |
///for setting |
|
| 1107 |
///for setting DistMap object. |
|
| 1108 | 1108 |
/// |
| 1109 | 1109 |
/// \ref named-func-param "Named parameter" |
| 1110 |
///for setting |
|
| 1110 |
///for setting DistMap object. |
|
| 1111 | 1111 |
template<class T> |
| 1112 | 1112 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
| 1113 | 1113 |
{
|
| 1114 | 1114 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1115 | 1115 |
return BfsWizard<SetDistMapBase<T> >(*this); |
| 1116 | 1116 |
} |
| 1117 | 1117 |
|
| 1118 | 1118 |
template<class T> |
| 1119 | 1119 |
struct SetProcessedMapBase : public Base {
|
| 1120 | 1120 |
typedef T ProcessedMap; |
| 1121 | 1121 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
| 1122 | 1122 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
| 1123 | 1123 |
}; |
| 1124 | 1124 |
///\brief \ref named-func-param "Named parameter" |
| 1125 |
///for setting |
|
| 1125 |
///for setting ProcessedMap object. |
|
| 1126 | 1126 |
/// |
| 1127 | 1127 |
/// \ref named-func-param "Named parameter" |
| 1128 |
///for setting |
|
| 1128 |
///for setting ProcessedMap object. |
|
| 1129 | 1129 |
template<class T> |
| 1130 | 1130 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
| 1131 | 1131 |
{
|
| 1132 | 1132 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1133 | 1133 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
| 1134 | 1134 |
} |
| 1135 | 1135 |
|
| 1136 | 1136 |
template<class T> |
| 1137 | 1137 |
struct SetPathBase : public Base {
|
| 1138 | 1138 |
typedef T Path; |
| 1139 | 1139 |
SetPathBase(const TR &b) : TR(b) {}
|
| 1140 | 1140 |
}; |
| 1141 | 1141 |
///\brief \ref named-func-param "Named parameter" |
| 1142 | 1142 |
///for getting the shortest path to the target node. |
| 1143 | 1143 |
/// |
| 1144 | 1144 |
///\ref named-func-param "Named parameter" |
| 1145 | 1145 |
///for getting the shortest path to the target node. |
| 1146 | 1146 |
template<class T> |
| 1147 | 1147 |
BfsWizard<SetPathBase<T> > path(const T &t) |
| 1148 | 1148 |
{
|
| 1149 | 1149 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1150 | 1150 |
return BfsWizard<SetPathBase<T> >(*this); |
| 1151 | 1151 |
} |
| 1152 | 1152 |
|
| 1153 | 1153 |
///\brief \ref named-func-param "Named parameter" |
| 1154 | 1154 |
///for getting the distance of the target node. |
| 1155 | 1155 |
/// |
| 1156 | 1156 |
///\ref named-func-param "Named parameter" |
| 1157 | 1157 |
///for getting the distance of the target node. |
| 1158 | 1158 |
BfsWizard dist(const int &d) |
| 1159 | 1159 |
{
|
| 1160 | 1160 |
Base::_di=const_cast<int*>(&d); |
| 1161 | 1161 |
return *this; |
| 1162 | 1162 |
} |
| 1163 | 1163 |
|
| 1164 | 1164 |
}; |
| 1165 | 1165 |
|
| 1166 | 1166 |
///Function-type interface for BFS algorithm. |
| 1167 | 1167 |
|
| 1168 | 1168 |
/// \ingroup search |
| 1169 | 1169 |
///Function-type interface for BFS algorithm. |
| 1170 | 1170 |
/// |
| 1171 | 1171 |
///This function also has several \ref named-func-param "named parameters", |
| 1172 | 1172 |
///they are declared as the members of class \ref BfsWizard. |
| 1173 | 1173 |
///The following examples show how to use these parameters. |
| 1174 | 1174 |
///\code |
| 1175 | 1175 |
/// // Compute shortest path from node s to each node |
| 1176 | 1176 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
| 1177 | 1177 |
/// |
| 1178 | 1178 |
/// // Compute shortest path from s to t |
| 1179 | 1179 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t); |
| 1180 | 1180 |
///\endcode |
| 1181 | 1181 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
| 1182 | 1182 |
///to the end of the parameter list. |
| 1183 | 1183 |
///\sa BfsWizard |
| 1184 | 1184 |
///\sa Bfs |
| 1185 | 1185 |
template<class GR> |
| 1186 | 1186 |
BfsWizard<BfsWizardBase<GR> > |
| 1187 | 1187 |
bfs(const GR &digraph) |
| 1188 | 1188 |
{
|
| 1189 | 1189 |
return BfsWizard<BfsWizardBase<GR> >(digraph); |
| 1190 | 1190 |
} |
| 1191 | 1191 |
|
| 1192 | 1192 |
#ifdef DOXYGEN |
| 1193 | 1193 |
/// \brief Visitor class for BFS. |
| 1194 | 1194 |
/// |
| 1195 | 1195 |
/// This class defines the interface of the BfsVisit events, and |
| 1196 | 1196 |
/// it could be the base of a real visitor class. |
| 1197 | 1197 |
template <typename _Digraph> |
| 1198 | 1198 |
struct BfsVisitor {
|
| 1199 | 1199 |
typedef _Digraph Digraph; |
| 1200 | 1200 |
typedef typename Digraph::Arc Arc; |
| 1201 | 1201 |
typedef typename Digraph::Node Node; |
| 1202 | 1202 |
/// \brief Called for the source node(s) of the BFS. |
| 1203 | 1203 |
/// |
| 1204 | 1204 |
/// This function is called for the source node(s) of the BFS. |
| 1205 | 1205 |
void start(const Node& node) {}
|
| 1206 | 1206 |
/// \brief Called when a node is reached first time. |
| 1207 | 1207 |
/// |
| 1208 | 1208 |
/// This function is called when a node is reached first time. |
| 1209 | 1209 |
void reach(const Node& node) {}
|
| 1210 | 1210 |
/// \brief Called when a node is processed. |
| 1211 | 1211 |
/// |
| 1212 | 1212 |
/// This function is called when a node is processed. |
| 1213 | 1213 |
void process(const Node& node) {}
|
| 1214 | 1214 |
/// \brief Called when an arc reaches a new node. |
| 1215 | 1215 |
/// |
| 1216 | 1216 |
/// This function is called when the BFS finds an arc whose target node |
| 1217 | 1217 |
/// is not reached yet. |
| 1218 | 1218 |
void discover(const Arc& arc) {}
|
| 1219 | 1219 |
/// \brief Called when an arc is examined but its target node is |
| 1220 | 1220 |
/// already discovered. |
| 1221 | 1221 |
/// |
| 1222 | 1222 |
/// This function is called when an arc is examined but its target node is |
| 1223 | 1223 |
/// already discovered. |
| 1224 | 1224 |
void examine(const Arc& arc) {}
|
| 1225 | 1225 |
}; |
| 1226 | 1226 |
#else |
| 1227 | 1227 |
template <typename _Digraph> |
| 1228 | 1228 |
struct BfsVisitor {
|
| 1229 | 1229 |
typedef _Digraph Digraph; |
| 1230 | 1230 |
typedef typename Digraph::Arc Arc; |
| 1231 | 1231 |
typedef typename Digraph::Node Node; |
| 1232 | 1232 |
void start(const Node&) {}
|
| 1233 | 1233 |
void reach(const Node&) {}
|
| 1234 | 1234 |
void process(const Node&) {}
|
| 1235 | 1235 |
void discover(const Arc&) {}
|
| 1236 | 1236 |
void examine(const Arc&) {}
|
| 1237 | 1237 |
|
| 1238 | 1238 |
template <typename _Visitor> |
| 1239 | 1239 |
struct Constraints {
|
| 1240 | 1240 |
void constraints() {
|
| 1241 | 1241 |
Arc arc; |
| 1242 | 1242 |
Node node; |
| 1243 | 1243 |
visitor.start(node); |
| 1244 | 1244 |
visitor.reach(node); |
| 1245 | 1245 |
visitor.process(node); |
| 1246 | 1246 |
visitor.discover(arc); |
| 1247 | 1247 |
visitor.examine(arc); |
| 1248 | 1248 |
} |
| 1249 | 1249 |
_Visitor& visitor; |
| 1250 | 1250 |
}; |
| 1251 | 1251 |
}; |
| 1252 | 1252 |
#endif |
| 1253 | 1253 |
|
| 1254 | 1254 |
/// \brief Default traits class of BfsVisit class. |
| 1255 | 1255 |
/// |
| 1256 | 1256 |
/// Default traits class of BfsVisit class. |
| 1257 | 1257 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1258 | 1258 |
template<class _Digraph> |
| 1259 | 1259 |
struct BfsVisitDefaultTraits {
|
| 1260 | 1260 |
|
| 1261 | 1261 |
/// \brief The type of the digraph the algorithm runs on. |
| 1262 | 1262 |
typedef _Digraph Digraph; |
| 1263 | 1263 |
|
| 1264 | 1264 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1265 | 1265 |
/// |
| 1266 | 1266 |
/// The type of the map that indicates which nodes are reached. |
| 1267 | 1267 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 1268 | 1268 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1269 | 1269 |
|
| 1270 |
/// \brief Instantiates a |
|
| 1270 |
/// \brief Instantiates a ReachedMap. |
|
| 1271 | 1271 |
/// |
| 1272 |
/// This function instantiates a |
|
| 1272 |
/// This function instantiates a ReachedMap. |
|
| 1273 | 1273 |
/// \param digraph is the digraph, to which |
| 1274 |
/// we would like to define the |
|
| 1274 |
/// we would like to define the ReachedMap. |
|
| 1275 | 1275 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1276 | 1276 |
return new ReachedMap(digraph); |
| 1277 | 1277 |
} |
| 1278 | 1278 |
|
| 1279 | 1279 |
}; |
| 1280 | 1280 |
|
| 1281 | 1281 |
/// \ingroup search |
| 1282 | 1282 |
/// |
| 1283 | 1283 |
/// \brief %BFS algorithm class with visitor interface. |
| 1284 | 1284 |
/// |
| 1285 | 1285 |
/// This class provides an efficient implementation of the %BFS algorithm |
| 1286 | 1286 |
/// with visitor interface. |
| 1287 | 1287 |
/// |
| 1288 | 1288 |
/// The %BfsVisit class provides an alternative interface to the Bfs |
| 1289 | 1289 |
/// class. It works with callback mechanism, the BfsVisit object calls |
| 1290 | 1290 |
/// the member functions of the \c Visitor class on every BFS event. |
| 1291 | 1291 |
/// |
| 1292 | 1292 |
/// This interface of the BFS algorithm should be used in special cases |
| 1293 | 1293 |
/// when extra actions have to be performed in connection with certain |
| 1294 | 1294 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
| 1295 | 1295 |
/// instead. |
| 1296 | 1296 |
/// |
| 1297 | 1297 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1298 | 1298 |
/// The default value is |
| 1299 | 1299 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
| 1300 | 1300 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. |
| 1301 | 1301 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
| 1302 | 1302 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which |
| 1303 | 1303 |
/// does not observe the BFS events. If you want to observe the BFS |
| 1304 | 1304 |
/// events, you should implement your own visitor class. |
| 1305 | 1305 |
/// \tparam _Traits Traits class to set various data types used by the |
| 1306 | 1306 |
/// algorithm. The default traits class is |
| 1307 | 1307 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". |
| 1308 | 1308 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
| 1309 | 1309 |
/// a BFS visit traits class. |
| 1310 | 1310 |
#ifdef DOXYGEN |
| 1311 | 1311 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
| 1312 | 1312 |
#else |
| 1313 | 1313 |
template <typename _Digraph = ListDigraph, |
| 1314 | 1314 |
typename _Visitor = BfsVisitor<_Digraph>, |
| 1315 | 1315 |
typename _Traits = BfsVisitDefaultTraits<_Digraph> > |
| 1316 | 1316 |
#endif |
| 1317 | 1317 |
class BfsVisit {
|
| 1318 | 1318 |
public: |
| 1319 | 1319 |
|
| 1320 | 1320 |
///The traits class. |
| 1321 | 1321 |
typedef _Traits Traits; |
| 1322 | 1322 |
|
| 1323 | 1323 |
///The type of the digraph the algorithm runs on. |
| 1324 | 1324 |
typedef typename Traits::Digraph Digraph; |
| 1325 | 1325 |
|
| 1326 | 1326 |
///The visitor type used by the algorithm. |
| 1327 | 1327 |
typedef _Visitor Visitor; |
| 1328 | 1328 |
|
| 1329 | 1329 |
///The type of the map that indicates which nodes are reached. |
| 1330 | 1330 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1331 | 1331 |
|
| 1332 | 1332 |
private: |
| 1333 | 1333 |
|
| 1334 | 1334 |
typedef typename Digraph::Node Node; |
| 1335 | 1335 |
typedef typename Digraph::NodeIt NodeIt; |
| 1336 | 1336 |
typedef typename Digraph::Arc Arc; |
| 1337 | 1337 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1338 | 1338 |
|
| 1339 | 1339 |
//Pointer to the underlying digraph. |
| 1340 | 1340 |
const Digraph *_digraph; |
| 1341 | 1341 |
//Pointer to the visitor object. |
| 1342 | 1342 |
Visitor *_visitor; |
| 1343 | 1343 |
//Pointer to the map of reached status of the nodes. |
| 1344 | 1344 |
ReachedMap *_reached; |
| 1345 | 1345 |
//Indicates if _reached is locally allocated (true) or not. |
| 1346 | 1346 |
bool local_reached; |
| 1347 | 1347 |
|
| 1348 | 1348 |
std::vector<typename Digraph::Node> _list; |
| 1349 | 1349 |
int _list_front, _list_back; |
| 1350 | 1350 |
|
| 1351 | 1351 |
//Creates the maps if necessary. |
| 1352 | 1352 |
void create_maps() {
|
| 1353 | 1353 |
if(!_reached) {
|
| 1354 | 1354 |
local_reached = true; |
| 1355 | 1355 |
_reached = Traits::createReachedMap(*_digraph); |
| 1356 | 1356 |
} |
| 1357 | 1357 |
} |
| 1358 | 1358 |
|
| 1359 | 1359 |
protected: |
| 1360 | 1360 |
|
| 1361 | 1361 |
BfsVisit() {}
|
| 1362 | 1362 |
|
| 1363 | 1363 |
public: |
| 1364 | 1364 |
|
| 1365 | 1365 |
typedef BfsVisit Create; |
| 1366 | 1366 |
|
| 1367 | 1367 |
/// \name Named template parameters |
| 1368 | 1368 |
|
| 1369 | 1369 |
///@{
|
| 1370 | 1370 |
template <class T> |
| 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 |
///\ingroup graphbits |
|
| 28 |
///\file |
|
| 29 |
|
|
| 27 |
//\ingroup graphbits |
|
| 28 |
//\file |
|
| 29 |
//\brief Observer notifier for graph alteration observers. |
|
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 |
/// \ingroup graphbits |
|
| 34 |
/// |
|
| 35 |
/// \brief Notifier class to notify observes about alterations in |
|
| 36 |
/// a container. |
|
| 37 |
/// |
|
| 38 |
/// The simple graph's can be refered as two containers, one node container |
|
| 39 |
/// and one edge container. But they are not standard containers they |
|
| 40 |
/// does not store values directly they are just key continars for more |
|
| 41 |
/// value containers which are the node and edge maps. |
|
| 42 |
/// |
|
| 43 |
/// The graph's node and edge sets can be changed as we add or erase |
|
| 44 |
/// nodes and edges in the graph. LEMON would like to handle easily |
|
| 45 |
/// that the node and edge maps should contain values for all nodes or |
|
| 46 |
/// edges. If we want to check on every indicing if the map contains |
|
| 47 |
/// the current indicing key that cause a drawback in the performance |
|
| 48 |
/// in the library. We use another solution we notify all maps about |
|
| 49 |
/// an alteration in the graph, which cause only drawback on the |
|
| 50 |
/// alteration of the graph. |
|
| 51 |
/// |
|
| 52 |
/// This class provides an interface to the container. The \e first() and \e |
|
| 53 |
/// next() member functions make possible to iterate on the keys of the |
|
| 54 |
/// container. The \e id() function returns an integer id for each key. |
|
| 55 |
/// The \e maxId() function gives back an upper bound of the ids. |
|
| 56 |
/// |
|
| 57 |
/// For the proper functonality of this class, we should notify it |
|
| 58 |
/// about each alteration in the container. The alterations have four type |
|
| 59 |
/// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and |
|
| 60 |
/// \e erase() signals that only one or few items added or erased to or |
|
| 61 |
/// from the graph. If all items are erased from the graph or from an empty |
|
| 62 |
/// graph a new graph is builded then it can be signaled with the |
|
| 63 |
/// clear() and build() members. Important rule that if we erase items |
|
| 64 |
/// from graph we should first signal the alteration and after that erase |
|
| 65 |
/// them from the container, on the other way on item addition we should |
|
| 66 |
/// first extend the container and just after that signal the alteration. |
|
| 67 |
/// |
|
| 68 |
/// The alteration can be observed with a class inherited from the |
|
| 69 |
/// \e ObserverBase nested class. The signals can be handled with |
|
| 70 |
/// overriding the virtual functions defined in the base class. The |
|
| 71 |
/// observer base can be attached to the notifier with the |
|
| 72 |
/// \e attach() member and can be detached with detach() function. The |
|
| 73 |
/// alteration handlers should not call any function which signals |
|
| 74 |
/// an other alteration in the same notifier and should not |
|
| 75 |
/// detach any observer from the notifier. |
|
| 76 |
/// |
|
| 77 |
/// Alteration observers try to be exception safe. If an \e add() or |
|
| 78 |
/// a \e clear() function throws an exception then the remaining |
|
| 79 |
/// observeres will not be notified and the fulfilled additions will |
|
| 80 |
/// be rolled back by calling the \e erase() or \e clear() |
|
| 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 |
|
| 84 |
/// exception which detach the observer from the notifier. |
|
| 85 |
/// |
|
| 86 |
/// There are some place when the alteration observing is not completly |
|
| 87 |
/// reliable. If we want to carry out the node degree in the graph |
|
| 88 |
/// as in the \ref InDegMap and we use the reverseEdge that cause |
|
| 89 |
/// unreliable functionality. Because the alteration observing signals |
|
| 90 |
/// only erasing and adding but not the reversing it will stores bad |
|
| 91 |
/// degrees. The sub graph adaptors cannot signal the alterations because |
|
| 92 |
/// just a setting in the filter map can modify the graph and this cannot |
|
| 93 |
/// be watched in any way. |
|
| 94 |
/// |
|
| 95 |
/// \param _Container The container which is observed. |
|
| 96 |
/// \param _Item The item type which is obserbved. |
|
| 33 |
// \ingroup graphbits |
|
| 34 |
// |
|
| 35 |
// \brief Notifier class to notify observes about alterations in |
|
| 36 |
// a container. |
|
| 37 |
// |
|
| 38 |
// The simple graph's can be refered as two containers, one node container |
|
| 39 |
// and one edge container. But they are not standard containers they |
|
| 40 |
// does not store values directly they are just key continars for more |
|
| 41 |
// value containers which are the node and edge maps. |
|
| 42 |
// |
|
| 43 |
// The graph's node and edge sets can be changed as we add or erase |
|
| 44 |
// nodes and edges in the graph. LEMON would like to handle easily |
|
| 45 |
// that the node and edge maps should contain values for all nodes or |
|
| 46 |
// edges. If we want to check on every indicing if the map contains |
|
| 47 |
// the current indicing key that cause a drawback in the performance |
|
| 48 |
// in the library. We use another solution we notify all maps about |
|
| 49 |
// an alteration in the graph, which cause only drawback on the |
|
| 50 |
// alteration of the graph. |
|
| 51 |
// |
|
| 52 |
// This class provides an interface to the container. The \e first() and \e |
|
| 53 |
// next() member functions make possible to iterate on the keys of the |
|
| 54 |
// container. The \e id() function returns an integer id for each key. |
|
| 55 |
// The \e maxId() function gives back an upper bound of the ids. |
|
| 56 |
// |
|
| 57 |
// For the proper functonality of this class, we should notify it |
|
| 58 |
// about each alteration in the container. The alterations have four type |
|
| 59 |
// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and |
|
| 60 |
// \e erase() signals that only one or few items added or erased to or |
|
| 61 |
// from the graph. If all items are erased from the graph or from an empty |
|
| 62 |
// graph a new graph is builded then it can be signaled with the |
|
| 63 |
// clear() and build() members. Important rule that if we erase items |
|
| 64 |
// from graph we should first signal the alteration and after that erase |
|
| 65 |
// them from the container, on the other way on item addition we should |
|
| 66 |
// first extend the container and just after that signal the alteration. |
|
| 67 |
// |
|
| 68 |
// The alteration can be observed with a class inherited from the |
|
| 69 |
// \e ObserverBase nested class. The signals can be handled with |
|
| 70 |
// overriding the virtual functions defined in the base class. The |
|
| 71 |
// observer base can be attached to the notifier with the |
|
| 72 |
// \e attach() member and can be detached with detach() function. The |
|
| 73 |
// alteration handlers should not call any function which signals |
|
| 74 |
// an other alteration in the same notifier and should not |
|
| 75 |
// detach any observer from the notifier. |
|
| 76 |
// |
|
| 77 |
// Alteration observers try to be exception safe. If an \e add() or |
|
| 78 |
// a \e clear() function throws an exception then the remaining |
|
| 79 |
// observeres will not be notified and the fulfilled additions will |
|
| 80 |
// be rolled back by calling the \e erase() or \e clear() |
|
| 81 |
// functions. Thence the \e erase() and \e clear() should not throw |
|
| 82 |
// exception. Actullay, it can be throw only \ref ImmediateDetach |
|
| 83 |
// exception which detach the observer from the notifier. |
|
| 84 |
// |
|
| 85 |
// There are some place when the alteration observing is not completly |
|
| 86 |
// reliable. If we want to carry out the node degree in the graph |
|
| 87 |
// as in the \ref InDegMap and we use the reverseEdge that cause |
|
| 88 |
// unreliable functionality. Because the alteration observing signals |
|
| 89 |
// only erasing and adding but not the reversing it will stores bad |
|
| 90 |
// degrees. The sub graph adaptors cannot signal the alterations because |
|
| 91 |
// just a setting in the filter map can modify the graph and this cannot |
|
| 92 |
// be watched in any way. |
|
| 93 |
// |
|
| 94 |
// \param _Container The container which is observed. |
|
| 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 |
/// \brief Exception which can be called from \e clear() and |
|
| 108 |
/// \e erase(). |
|
| 109 |
/// |
|
| 110 |
/// From the \e clear() and \e erase() function only this |
|
| 111 |
/// exception is allowed to throw. The exception immediatly |
|
| 112 |
/// detaches the current observer from the notifier. Because the |
|
| 113 |
/// \e clear() and \e erase() should not throw other exceptions |
|
| 114 |
/// it can be used to invalidate the observer. |
|
| 106 |
// \brief Exception which can be called from \e clear() and |
|
| 107 |
// \e erase(). |
|
| 108 |
// |
|
| 109 |
// From the \e clear() and \e erase() function only this |
|
| 110 |
// exception is allowed to throw. The exception immediatly |
|
| 111 |
// detaches the current observer from the notifier. Because the |
|
| 112 |
// \e clear() and \e erase() should not throw other exceptions |
|
| 113 |
// it can be used to invalidate the observer. |
|
| 115 | 114 |
struct ImmediateDetach {};
|
| 116 | 115 |
|
| 117 |
/// \brief ObserverBase is the base class for the observers. |
|
| 118 |
/// |
|
| 119 |
/// ObserverBase is the abstract base class for the observers. |
|
| 120 |
/// It will be notified about an item was inserted into or |
|
| 121 |
/// erased from the graph. |
|
| 122 |
/// |
|
| 123 |
/// The observer interface contains some pure virtual functions |
|
| 124 |
/// to override. The add() and erase() functions are |
|
| 125 |
/// to notify the oberver when one item is added or |
|
| 126 |
/// erased. |
|
| 127 |
/// |
|
| 128 |
/// The build() and clear() members are to notify the observer |
|
| 129 |
/// about the container is built from an empty container or |
|
| 130 |
/// is cleared to an empty container. |
|
| 131 |
|
|
| 116 |
// \brief ObserverBase is the base class for the observers. |
|
| 117 |
// |
|
| 118 |
// ObserverBase is the abstract base class for the observers. |
|
| 119 |
// It will be notified about an item was inserted into or |
|
| 120 |
// erased from the graph. |
|
| 121 |
// |
|
| 122 |
// The observer interface contains some pure virtual functions |
|
| 123 |
// to override. The add() and erase() functions are |
|
| 124 |
// to notify the oberver when one item is added or |
|
| 125 |
// erased. |
|
| 126 |
// |
|
| 127 |
// The build() and clear() members are to notify the observer |
|
| 128 |
// about the container is built from an empty container or |
|
| 129 |
// is cleared to an empty container. |
|
| 132 | 130 |
class ObserverBase {
|
| 133 | 131 |
protected: |
| 134 | 132 |
typedef AlterationNotifier Notifier; |
| 135 | 133 |
|
| 136 | 134 |
friend class AlterationNotifier; |
| 137 | 135 |
|
| 138 |
/// \brief Default constructor. |
|
| 139 |
/// |
|
| 140 |
/// Default constructor for ObserverBase. |
|
| 141 |
/// |
|
| 136 |
// \brief Default constructor. |
|
| 137 |
// |
|
| 138 |
// Default constructor for ObserverBase. |
|
| 142 | 139 |
ObserverBase() : _notifier(0) {}
|
| 143 | 140 |
|
| 144 |
/// \brief Constructor which attach the observer into notifier. |
|
| 145 |
/// |
|
| 146 |
// |
|
| 141 |
// \brief Constructor which attach the observer into notifier. |
|
| 142 |
// |
|
| 143 |
// Constructor which attach the observer into notifier. |
|
| 147 | 144 |
ObserverBase(AlterationNotifier& nf) {
|
| 148 | 145 |
attach(nf); |
| 149 | 146 |
} |
| 150 | 147 |
|
| 151 |
/// \brief Constructor which attach the obserever to the same notifier. |
|
| 152 |
/// |
|
| 153 |
/// Constructor which attach the obserever to the same notifier as |
|
| 154 |
/// the other observer is attached to. |
|
| 148 |
// \brief Constructor which attach the obserever to the same notifier. |
|
| 149 |
// |
|
| 150 |
// Constructor which attach the obserever to the same notifier as |
|
| 151 |
// the other observer is attached to. |
|
| 155 | 152 |
ObserverBase(const ObserverBase& copy) {
|
| 156 | 153 |
if (copy.attached()) {
|
| 157 | 154 |
attach(*copy.notifier()); |
| 158 | 155 |
} |
| 159 | 156 |
} |
| 160 | 157 |
|
| 161 |
// |
|
| 158 |
// \brief Destructor |
|
| 162 | 159 |
virtual ~ObserverBase() {
|
| 163 | 160 |
if (attached()) {
|
| 164 | 161 |
detach(); |
| 165 | 162 |
} |
| 166 | 163 |
} |
| 167 | 164 |
|
| 168 |
/// \brief Attaches the observer into an AlterationNotifier. |
|
| 169 |
/// |
|
| 170 |
/// This member attaches the observer into an AlterationNotifier. |
|
| 171 |
/// |
|
| 165 |
// \brief Attaches the observer into an AlterationNotifier. |
|
| 166 |
// |
|
| 167 |
// This member attaches the observer into an AlterationNotifier. |
|
| 172 | 168 |
void attach(AlterationNotifier& nf) {
|
| 173 | 169 |
nf.attach(*this); |
| 174 | 170 |
} |
| 175 | 171 |
|
| 176 |
/// \brief Detaches the observer into an AlterationNotifier. |
|
| 177 |
/// |
|
| 178 |
/// This member detaches the observer from an AlterationNotifier. |
|
| 179 |
/// |
|
| 172 |
// \brief Detaches the observer into an AlterationNotifier. |
|
| 173 |
// |
|
| 174 |
// This member detaches the observer from an AlterationNotifier. |
|
| 180 | 175 |
void detach() {
|
| 181 | 176 |
_notifier->detach(*this); |
| 182 | 177 |
} |
| 183 | 178 |
|
| 184 |
/// \brief Gives back a pointer to the notifier which the map |
|
| 185 |
/// attached into. |
|
| 186 |
/// |
|
| 187 |
/// This function gives back a pointer to the notifier which the map |
|
| 188 |
/// attached into. |
|
| 189 |
/// |
|
| 179 |
// \brief Gives back a pointer to the notifier which the map |
|
| 180 |
// attached into. |
|
| 181 |
// |
|
| 182 |
// This function gives back a pointer to the notifier which the map |
|
| 183 |
// attached into. |
|
| 190 | 184 |
Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
|
| 191 | 185 |
|
| 192 |
|
|
| 186 |
// Gives back true when the observer is attached into a notifier. |
|
| 193 | 187 |
bool attached() const { return _notifier != 0; }
|
| 194 | 188 |
|
| 195 | 189 |
private: |
| 196 | 190 |
|
| 197 | 191 |
ObserverBase& operator=(const ObserverBase& copy); |
| 198 | 192 |
|
| 199 | 193 |
protected: |
| 200 | 194 |
|
| 201 | 195 |
Notifier* _notifier; |
| 202 | 196 |
typename std::list<ObserverBase*>::iterator _index; |
| 203 | 197 |
|
| 204 |
/// \brief The member function to notificate the observer about an |
|
| 205 |
/// item is added to the container. |
|
| 206 |
/// |
|
| 207 |
/// The add() member function notificates the observer about an item |
|
| 208 |
/// is added to the container. It have to be overrided in the |
|
| 209 |
/// subclasses. |
|
| 198 |
// \brief The member function to notificate the observer about an |
|
| 199 |
// item is added to the container. |
|
| 200 |
// |
|
| 201 |
// The add() member function notificates the observer about an item |
|
| 202 |
// is added to the container. It have to be overrided in the |
|
| 203 |
// subclasses. |
|
| 210 | 204 |
virtual void add(const Item&) = 0; |
| 211 | 205 |
|
| 212 |
/// \brief The member function to notificate the observer about |
|
| 213 |
/// more item is added to the container. |
|
| 214 |
/// |
|
| 215 |
/// The add() member function notificates the observer about more item |
|
| 216 |
/// is added to the container. It have to be overrided in the |
|
| 217 |
/// subclasses. |
|
| 206 |
// \brief The member function to notificate the observer about |
|
| 207 |
// more item is added to the container. |
|
| 208 |
// |
|
| 209 |
// The add() member function notificates the observer about more item |
|
| 210 |
// is added to the container. It have to be overrided in the |
|
| 211 |
// subclasses. |
|
| 218 | 212 |
virtual void add(const std::vector<Item>& items) = 0; |
| 219 | 213 |
|
| 220 |
/// \brief The member function to notificate the observer about an |
|
| 221 |
/// item is erased from the container. |
|
| 222 |
/// |
|
| 223 |
/// The erase() member function notificates the observer about an |
|
| 224 |
/// item is erased from the container. It have to be overrided in |
|
| 225 |
/// the subclasses. |
|
| 214 |
// \brief The member function to notificate the observer about an |
|
| 215 |
// item is erased from the container. |
|
| 216 |
// |
|
| 217 |
// The erase() member function notificates the observer about an |
|
| 218 |
// item is erased from the container. It have to be overrided in |
|
| 219 |
// the subclasses. |
|
| 226 | 220 |
virtual void erase(const Item&) = 0; |
| 227 | 221 |
|
| 228 |
/// \brief The member function to notificate the observer about |
|
| 229 |
/// more item is erased from the container. |
|
| 230 |
/// |
|
| 231 |
/// The erase() member function notificates the observer about more item |
|
| 232 |
/// is erased from the container. It have to be overrided in the |
|
| 233 |
/// subclasses. |
|
| 222 |
// \brief The member function to notificate the observer about |
|
| 223 |
// more item is erased from the container. |
|
| 224 |
// |
|
| 225 |
// The erase() member function notificates the observer about more item |
|
| 226 |
// is erased from the container. It have to be overrided in the |
|
| 227 |
// subclasses. |
|
| 234 | 228 |
virtual void erase(const std::vector<Item>& items) = 0; |
| 235 | 229 |
|
| 236 |
/// \brief The member function to notificate the observer about the |
|
| 237 |
/// container is built. |
|
| 238 |
/// |
|
| 239 |
/// The build() member function notificates the observer about the |
|
| 240 |
/// container is built from an empty container. It have to be |
|
| 241 |
/// overrided in the subclasses. |
|
| 242 |
|
|
| 230 |
// \brief The member function to notificate the observer about the |
|
| 231 |
// container is built. |
|
| 232 |
// |
|
| 233 |
// The build() member function notificates the observer about the |
|
| 234 |
// container is built from an empty container. It have to be |
|
| 235 |
// overrided in the subclasses. |
|
| 243 | 236 |
virtual void build() = 0; |
| 244 | 237 |
|
| 245 |
/// \brief The member function to notificate the observer about all |
|
| 246 |
/// items are erased from the container. |
|
| 247 |
/// |
|
| 248 |
/// The clear() member function notificates the observer about all |
|
| 249 |
/// items are erased from the container. It have to be overrided in |
|
| 250 |
/// the subclasses. |
|
| 238 |
// \brief The member function to notificate the observer about all |
|
| 239 |
// items are erased from the container. |
|
| 240 |
// |
|
| 241 |
// The clear() member function notificates the observer about all |
|
| 242 |
// items are erased from the container. It have to be overrided in |
|
| 243 |
// the subclasses. |
|
| 251 | 244 |
virtual void clear() = 0; |
| 252 | 245 |
|
| 253 | 246 |
}; |
| 254 | 247 |
|
| 255 | 248 |
protected: |
| 256 | 249 |
|
| 257 | 250 |
const Container* container; |
| 258 | 251 |
|
| 259 | 252 |
typedef std::list<ObserverBase*> Observers; |
| 260 | 253 |
Observers _observers; |
| 261 | 254 |
|
| 262 | 255 |
|
| 263 | 256 |
public: |
| 264 | 257 |
|
| 265 |
/// \brief Default constructor. |
|
| 266 |
/// |
|
| 267 |
/// The default constructor of the AlterationNotifier. |
|
| 268 |
/// It creates an empty notifier. |
|
| 258 |
// \brief Default constructor. |
|
| 259 |
// |
|
| 260 |
// The default constructor of the AlterationNotifier. |
|
| 261 |
// It creates an empty notifier. |
|
| 269 | 262 |
AlterationNotifier() |
| 270 | 263 |
: container(0) {}
|
| 271 | 264 |
|
| 272 |
/// \brief Constructor. |
|
| 273 |
/// |
|
| 274 |
// |
|
| 265 |
// \brief Constructor. |
|
| 266 |
// |
|
| 267 |
// Constructor with the observed container parameter. |
|
| 275 | 268 |
AlterationNotifier(const Container& _container) |
| 276 | 269 |
: container(&_container) {}
|
| 277 | 270 |
|
| 278 |
/// \brief Copy Constructor of the AlterationNotifier. |
|
| 279 |
/// |
|
| 280 |
/// Copy constructor of the AlterationNotifier. |
|
| 281 |
/// It creates only an empty notifier because the copiable |
|
| 282 |
// |
|
| 271 |
// \brief Copy Constructor of the AlterationNotifier. |
|
| 272 |
// |
|
| 273 |
// Copy constructor of the AlterationNotifier. |
|
| 274 |
// It creates only an empty notifier because the copiable |
|
| 275 |
// notifier's observers have to be registered still into that notifier. |
|
| 283 | 276 |
AlterationNotifier(const AlterationNotifier& _notifier) |
| 284 | 277 |
: container(_notifier.container) {}
|
| 285 | 278 |
|
| 286 |
/// \brief Destructor. |
|
| 287 |
/// |
|
| 288 |
/// Destructor of the AlterationNotifier. |
|
| 289 |
/// |
|
| 279 |
// \brief Destructor. |
|
| 280 |
// |
|
| 281 |
// Destructor of the AlterationNotifier. |
|
| 290 | 282 |
~AlterationNotifier() {
|
| 291 | 283 |
typename Observers::iterator it; |
| 292 | 284 |
for (it = _observers.begin(); it != _observers.end(); ++it) {
|
| 293 | 285 |
(*it)->_notifier = 0; |
| 294 | 286 |
} |
| 295 | 287 |
} |
| 296 | 288 |
|
| 297 |
/// \brief Sets the container. |
|
| 298 |
/// |
|
| 299 |
// |
|
| 289 |
// \brief Sets the container. |
|
| 290 |
// |
|
| 291 |
// Sets the container. |
|
| 300 | 292 |
void setContainer(const Container& _container) {
|
| 301 | 293 |
container = &_container; |
| 302 | 294 |
} |
| 303 | 295 |
|
| 304 | 296 |
protected: |
| 305 | 297 |
|
| 306 | 298 |
AlterationNotifier& operator=(const AlterationNotifier&); |
| 307 | 299 |
|
| 308 | 300 |
public: |
| 309 | 301 |
|
| 310 |
|
|
| 311 |
|
|
| 312 |
/// \brief First item in the container. |
|
| 313 |
/// |
|
| 314 |
/// Returns the first item in the container. It is |
|
| 315 |
/// for start the iteration on the container. |
|
| 302 |
// \brief First item in the container. |
|
| 303 |
// |
|
| 304 |
// Returns the first item in the container. It is |
|
| 305 |
// for start the iteration on the container. |
|
| 316 | 306 |
void first(Item& item) const {
|
| 317 | 307 |
container->first(item); |
| 318 | 308 |
} |
| 319 | 309 |
|
| 320 |
/// \brief Next item in the container. |
|
| 321 |
/// |
|
| 322 |
/// Returns the next item in the container. It is |
|
| 323 |
/// for iterate on the container. |
|
| 310 |
// \brief Next item in the container. |
|
| 311 |
// |
|
| 312 |
// Returns the next item in the container. It is |
|
| 313 |
// for iterate on the container. |
|
| 324 | 314 |
void next(Item& item) const {
|
| 325 | 315 |
container->next(item); |
| 326 | 316 |
} |
| 327 | 317 |
|
| 328 |
/// \brief Returns the id of the item. |
|
| 329 |
/// |
|
| 330 |
// |
|
| 318 |
// \brief Returns the id of the item. |
|
| 319 |
// |
|
| 320 |
// Returns the id of the item provided by the container. |
|
| 331 | 321 |
int id(const Item& item) const {
|
| 332 | 322 |
return container->id(item); |
| 333 | 323 |
} |
| 334 | 324 |
|
| 335 |
/// \brief Returns the maximum id of the container. |
|
| 336 |
/// |
|
| 337 |
// |
|
| 325 |
// \brief Returns the maximum id of the container. |
|
| 326 |
// |
|
| 327 |
// Returns the maximum id of the container. |
|
| 338 | 328 |
int maxId() const {
|
| 339 | 329 |
return container->maxId(Item()); |
| 340 | 330 |
} |
| 341 | 331 |
|
| 342 | 332 |
protected: |
| 343 | 333 |
|
| 344 | 334 |
void attach(ObserverBase& observer) {
|
| 345 | 335 |
observer._index = _observers.insert(_observers.begin(), &observer); |
| 346 | 336 |
observer._notifier = this; |
| 347 | 337 |
} |
| 348 | 338 |
|
| 349 | 339 |
void detach(ObserverBase& observer) {
|
| 350 | 340 |
_observers.erase(observer._index); |
| 351 | 341 |
observer._index = _observers.end(); |
| 352 | 342 |
observer._notifier = 0; |
| 353 | 343 |
} |
| 354 | 344 |
|
| 355 | 345 |
public: |
| 356 | 346 |
|
| 357 |
/// \brief Notifies all the registed observers about an item added to |
|
| 358 |
/// the container. |
|
| 359 |
/// |
|
| 360 |
/// It notifies all the registed observers about an item added to |
|
| 361 |
/// the container. |
|
| 362 |
/// |
|
| 347 |
// \brief Notifies all the registed observers about an item added to |
|
| 348 |
// the container. |
|
| 349 |
// |
|
| 350 |
// It notifies all the registed observers about an item added to |
|
| 351 |
// the container. |
|
| 363 | 352 |
void add(const Item& item) {
|
| 364 | 353 |
typename Observers::reverse_iterator it; |
| 365 | 354 |
try {
|
| 366 | 355 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
|
| 367 | 356 |
(*it)->add(item); |
| 368 | 357 |
} |
| 369 | 358 |
} catch (...) {
|
| 370 | 359 |
typename Observers::iterator jt; |
| 371 | 360 |
for (jt = it.base(); jt != _observers.end(); ++jt) {
|
| 372 | 361 |
(*jt)->erase(item); |
| 373 | 362 |
} |
| 374 | 363 |
throw; |
| 375 | 364 |
} |
| 376 | 365 |
} |
| 377 | 366 |
|
| 378 |
/// \brief Notifies all the registed observers about more item added to |
|
| 379 |
/// the container. |
|
| 380 |
/// |
|
| 381 |
/// It notifies all the registed observers about more item added to |
|
| 382 |
/// the container. |
|
| 383 |
/// |
|
| 367 |
// \brief Notifies all the registed observers about more item added to |
|
| 368 |
// the container. |
|
| 369 |
// |
|
| 370 |
// It notifies all the registed observers about more item added to |
|
| 371 |
// the container. |
|
| 384 | 372 |
void add(const std::vector<Item>& items) {
|
| 385 | 373 |
typename Observers::reverse_iterator it; |
| 386 | 374 |
try {
|
| 387 | 375 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
|
| 388 | 376 |
(*it)->add(items); |
| 389 | 377 |
} |
| 390 | 378 |
} catch (...) {
|
| 391 | 379 |
typename Observers::iterator jt; |
| 392 | 380 |
for (jt = it.base(); jt != _observers.end(); ++jt) {
|
| 393 | 381 |
(*jt)->erase(items); |
| 394 | 382 |
} |
| 395 | 383 |
throw; |
| 396 | 384 |
} |
| 397 | 385 |
} |
| 398 | 386 |
|
| 399 |
/// \brief Notifies all the registed observers about an item erased from |
|
| 400 |
/// the container. |
|
| 401 |
/// |
|
| 402 |
/// It notifies all the registed observers about an item erased from |
|
| 403 |
/// the container. |
|
| 404 |
/// |
|
| 387 |
// \brief Notifies all the registed observers about an item erased from |
|
| 388 |
// the container. |
|
| 389 |
// |
|
| 390 |
// It notifies all the registed observers about an item erased from |
|
| 391 |
// the container. |
|
| 405 | 392 |
void erase(const Item& item) throw() {
|
| 406 | 393 |
typename Observers::iterator it = _observers.begin(); |
| 407 | 394 |
while (it != _observers.end()) {
|
| 408 | 395 |
try {
|
| 409 | 396 |
(*it)->erase(item); |
| 410 | 397 |
++it; |
| 411 | 398 |
} catch (const ImmediateDetach&) {
|
| 412 | 399 |
(*it)->_index = _observers.end(); |
| 413 | 400 |
(*it)->_notifier = 0; |
| 414 | 401 |
it = _observers.erase(it); |
| 415 | 402 |
} |
| 416 | 403 |
} |
| 417 | 404 |
} |
| 418 | 405 |
|
| 419 |
/// \brief Notifies all the registed observers about more item erased |
|
| 420 |
/// from the container. |
|
| 421 |
/// |
|
| 422 |
/// It notifies all the registed observers about more item erased from |
|
| 423 |
/// the container. |
|
| 424 |
/// |
|
| 406 |
// \brief Notifies all the registed observers about more item erased |
|
| 407 |
// from the container. |
|
| 408 |
// |
|
| 409 |
// It notifies all the registed observers about more item erased from |
|
| 410 |
// the container. |
|
| 425 | 411 |
void erase(const std::vector<Item>& items) {
|
| 426 | 412 |
typename Observers::iterator it = _observers.begin(); |
| 427 | 413 |
while (it != _observers.end()) {
|
| 428 | 414 |
try {
|
| 429 | 415 |
(*it)->erase(items); |
| 430 | 416 |
++it; |
| 431 | 417 |
} catch (const ImmediateDetach&) {
|
| 432 | 418 |
(*it)->_index = _observers.end(); |
| 433 | 419 |
(*it)->_notifier = 0; |
| 434 | 420 |
it = _observers.erase(it); |
| 435 | 421 |
} |
| 436 | 422 |
} |
| 437 | 423 |
} |
| 438 | 424 |
|
| 439 |
/// \brief Notifies all the registed observers about the container is |
|
| 440 |
/// built. |
|
| 441 |
/// |
|
| 442 |
/// Notifies all the registed observers about the container is built |
|
| 443 |
// |
|
| 425 |
// \brief Notifies all the registed observers about the container is |
|
| 426 |
// built. |
|
| 427 |
// |
|
| 428 |
// Notifies all the registed observers about the container is built |
|
| 429 |
// from an empty container. |
|
| 444 | 430 |
void build() {
|
| 445 | 431 |
typename Observers::reverse_iterator it; |
| 446 | 432 |
try {
|
| 447 | 433 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
|
| 448 | 434 |
(*it)->build(); |
| 449 | 435 |
} |
| 450 | 436 |
} catch (...) {
|
| 451 | 437 |
typename Observers::iterator jt; |
| 452 | 438 |
for (jt = it.base(); jt != _observers.end(); ++jt) {
|
| 453 | 439 |
(*jt)->clear(); |
| 454 | 440 |
} |
| 455 | 441 |
throw; |
| 456 | 442 |
} |
| 457 | 443 |
} |
| 458 | 444 |
|
| 459 |
/// \brief Notifies all the registed observers about all items are |
|
| 460 |
/// erased. |
|
| 461 |
/// |
|
| 462 |
/// Notifies all the registed observers about all items are erased |
|
| 463 |
// |
|
| 445 |
// \brief Notifies all the registed observers about all items are |
|
| 446 |
// erased. |
|
| 447 |
// |
|
| 448 |
// Notifies all the registed observers about all items are erased |
|
| 449 |
// from the container. |
|
| 464 | 450 |
void clear() {
|
| 465 | 451 |
typename Observers::iterator it = _observers.begin(); |
| 466 | 452 |
while (it != _observers.end()) {
|
| 467 | 453 |
try {
|
| 468 | 454 |
(*it)->clear(); |
| 469 | 455 |
++it; |
| 470 | 456 |
} catch (const ImmediateDetach&) {
|
| 471 | 457 |
(*it)->_index = _observers.end(); |
| 472 | 458 |
(*it)->_notifier = 0; |
| 473 | 459 |
it = _observers.erase(it); |
| 474 | 460 |
} |
| 475 | 461 |
} |
| 476 | 462 |
} |
| 477 | 463 |
}; |
| 478 | 464 |
|
| 479 | 465 |
} |
| 480 | 466 |
|
| 481 | 467 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_ARRAY_MAP_H |
| 20 | 20 |
#define LEMON_BITS_ARRAY_MAP_H |
| 21 | 21 |
|
| 22 | 22 |
#include <memory> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/bits/traits.h> |
| 25 | 25 |
#include <lemon/bits/alteration_notifier.h> |
| 26 | 26 |
#include <lemon/concept_check.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
|
| 29 |
/// \ingroup graphbits |
|
| 30 |
/// \file |
|
| 31 |
|
|
| 29 |
// \ingroup graphbits |
|
| 30 |
// \file |
|
| 31 |
// \brief Graph map based on the array storage. |
|
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 |
/// \ingroup graphbits |
|
| 36 |
/// |
|
| 37 |
/// \brief Graph map based on the array storage. |
|
| 38 |
/// |
|
| 39 |
/// The ArrayMap template class is graph map structure what |
|
| 40 |
/// automatically updates the map when a key is added to or erased from |
|
| 41 |
/// the map. This map uses the allocators to implement |
|
| 42 |
/// the container functionality. |
|
| 43 |
/// |
|
| 44 |
/// The template parameters are the Graph the current Item type and |
|
| 45 |
// |
|
| 35 |
// \ingroup graphbits |
|
| 36 |
// |
|
| 37 |
// \brief Graph map based on the array storage. |
|
| 38 |
// |
|
| 39 |
// The ArrayMap template class is graph map structure what |
|
| 40 |
// automatically updates the map when a key is added to or erased from |
|
| 41 |
// the map. This map uses the allocators to implement |
|
| 42 |
// the container functionality. |
|
| 43 |
// |
|
| 44 |
// The template parameters are the Graph the current Item type and |
|
| 45 |
// the Value type of the map. |
|
| 46 | 46 |
template <typename _Graph, typename _Item, typename _Value> |
| 47 | 47 |
class ArrayMap |
| 48 | 48 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
|
| 49 | 49 |
public: |
| 50 |
|
|
| 50 |
// The graph type of the maps. |
|
| 51 | 51 |
typedef _Graph Graph; |
| 52 |
|
|
| 52 |
// The item type of the map. |
|
| 53 | 53 |
typedef _Item Item; |
| 54 |
|
|
| 54 |
// The reference map tag. |
|
| 55 | 55 |
typedef True ReferenceMapTag; |
| 56 | 56 |
|
| 57 |
|
|
| 57 |
// The key type of the maps. |
|
| 58 | 58 |
typedef _Item Key; |
| 59 |
|
|
| 59 |
// The value type of the map. |
|
| 60 | 60 |
typedef _Value Value; |
| 61 | 61 |
|
| 62 |
|
|
| 62 |
// The const reference type of the map. |
|
| 63 | 63 |
typedef const _Value& ConstReference; |
| 64 |
|
|
| 64 |
// The reference type of the map. |
|
| 65 | 65 |
typedef _Value& Reference; |
| 66 | 66 |
|
| 67 |
|
|
| 67 |
// The notifier type. |
|
| 68 | 68 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
| 69 | 69 |
|
| 70 |
|
|
| 70 |
// The MapBase of the Map which imlements the core regisitry function. |
|
| 71 | 71 |
typedef typename Notifier::ObserverBase Parent; |
| 72 | 72 |
|
| 73 | 73 |
private: |
| 74 | 74 |
typedef std::allocator<Value> Allocator; |
| 75 | 75 |
|
| 76 | 76 |
public: |
| 77 | 77 |
|
| 78 |
/// \brief Graph initialized map constructor. |
|
| 79 |
/// |
|
| 80 |
// |
|
| 78 |
// \brief Graph initialized map constructor. |
|
| 79 |
// |
|
| 80 |
// Graph initialized map constructor. |
|
| 81 | 81 |
explicit ArrayMap(const Graph& graph) {
|
| 82 | 82 |
Parent::attach(graph.notifier(Item())); |
| 83 | 83 |
allocate_memory(); |
| 84 | 84 |
Notifier* nf = Parent::notifier(); |
| 85 | 85 |
Item it; |
| 86 | 86 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 87 | 87 |
int id = nf->id(it);; |
| 88 | 88 |
allocator.construct(&(values[id]), Value()); |
| 89 | 89 |
} |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 |
/// \brief Constructor to use default value to initialize the map. |
|
| 93 |
/// |
|
| 94 |
// |
|
| 92 |
// \brief Constructor to use default value to initialize the map. |
|
| 93 |
// |
|
| 94 |
// It constructs a map and initialize all of the the map. |
|
| 95 | 95 |
ArrayMap(const Graph& graph, const Value& value) {
|
| 96 | 96 |
Parent::attach(graph.notifier(Item())); |
| 97 | 97 |
allocate_memory(); |
| 98 | 98 |
Notifier* nf = Parent::notifier(); |
| 99 | 99 |
Item it; |
| 100 | 100 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 101 | 101 |
int id = nf->id(it);; |
| 102 | 102 |
allocator.construct(&(values[id]), value); |
| 103 | 103 |
} |
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 | 106 |
private: |
| 107 |
/// \brief Constructor to copy a map of the same map type. |
|
| 108 |
/// |
|
| 109 |
// |
|
| 107 |
// \brief Constructor to copy a map of the same map type. |
|
| 108 |
// |
|
| 109 |
// Constructor to copy a map of the same map type. |
|
| 110 | 110 |
ArrayMap(const ArrayMap& copy) : Parent() {
|
| 111 | 111 |
if (copy.attached()) {
|
| 112 | 112 |
attach(*copy.notifier()); |
| 113 | 113 |
} |
| 114 | 114 |
capacity = copy.capacity; |
| 115 | 115 |
if (capacity == 0) return; |
| 116 | 116 |
values = allocator.allocate(capacity); |
| 117 | 117 |
Notifier* nf = Parent::notifier(); |
| 118 | 118 |
Item it; |
| 119 | 119 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 120 | 120 |
int id = nf->id(it);; |
| 121 | 121 |
allocator.construct(&(values[id]), copy.values[id]); |
| 122 | 122 |
} |
| 123 | 123 |
} |
| 124 | 124 |
|
| 125 |
/// \brief Assign operator. |
|
| 126 |
/// |
|
| 127 |
/// This operator assigns for each item in the map the |
|
| 128 |
/// value mapped to the same item in the copied map. |
|
| 129 |
/// The parameter map should be indiced with the same |
|
| 130 |
/// itemset because this assign operator does not change |
|
| 131 |
// |
|
| 125 |
// \brief Assign operator. |
|
| 126 |
// |
|
| 127 |
// This operator assigns for each item in the map the |
|
| 128 |
// value mapped to the same item in the copied map. |
|
| 129 |
// The parameter map should be indiced with the same |
|
| 130 |
// itemset because this assign operator does not change |
|
| 131 |
// the container of the map. |
|
| 132 | 132 |
ArrayMap& operator=(const ArrayMap& cmap) {
|
| 133 | 133 |
return operator=<ArrayMap>(cmap); |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
|
| 137 |
/// \brief Template assign operator. |
|
| 138 |
/// |
|
| 139 |
/// The given parameter should be conform to the ReadMap |
|
| 140 |
/// concecpt and could be indiced by the current item set of |
|
| 141 |
/// the NodeMap. In this case the value for each item |
|
| 142 |
/// is assigned by the value of the given ReadMap. |
|
| 137 |
// \brief Template assign operator. |
|
| 138 |
// |
|
| 139 |
// The given parameter should be conform to the ReadMap |
|
| 140 |
// concecpt and could be indiced by the current item set of |
|
| 141 |
// the NodeMap. In this case the value for each item |
|
| 142 |
// is assigned by the value of the given ReadMap. |
|
| 143 | 143 |
template <typename CMap> |
| 144 | 144 |
ArrayMap& operator=(const CMap& cmap) {
|
| 145 | 145 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
| 146 | 146 |
const typename Parent::Notifier* nf = Parent::notifier(); |
| 147 | 147 |
Item it; |
| 148 | 148 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 149 | 149 |
set(it, cmap[it]); |
| 150 | 150 |
} |
| 151 | 151 |
return *this; |
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
public: |
| 155 |
/// \brief The destructor of the map. |
|
| 156 |
/// |
|
| 157 |
// |
|
| 155 |
// \brief The destructor of the map. |
|
| 156 |
// |
|
| 157 |
// The destructor of the map. |
|
| 158 | 158 |
virtual ~ArrayMap() {
|
| 159 | 159 |
if (attached()) {
|
| 160 | 160 |
clear(); |
| 161 | 161 |
detach(); |
| 162 | 162 |
} |
| 163 | 163 |
} |
| 164 | 164 |
|
| 165 | 165 |
protected: |
| 166 | 166 |
|
| 167 | 167 |
using Parent::attach; |
| 168 | 168 |
using Parent::detach; |
| 169 | 169 |
using Parent::attached; |
| 170 | 170 |
|
| 171 | 171 |
public: |
| 172 | 172 |
|
| 173 |
/// \brief The subscript operator. |
|
| 174 |
/// |
|
| 175 |
/// The subscript operator. The map can be subscripted by the |
|
| 176 |
/// actual keys of the graph. |
|
| 173 |
// \brief The subscript operator. |
|
| 174 |
// |
|
| 175 |
// The subscript operator. The map can be subscripted by the |
|
| 176 |
// actual keys of the graph. |
|
| 177 | 177 |
Value& operator[](const Key& key) {
|
| 178 | 178 |
int id = Parent::notifier()->id(key); |
| 179 | 179 |
return values[id]; |
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 |
/// \brief The const subscript operator. |
|
| 183 |
/// |
|
| 184 |
/// The const subscript operator. The map can be subscripted by the |
|
| 185 |
/// actual keys of the graph. |
|
| 182 |
// \brief The const subscript operator. |
|
| 183 |
// |
|
| 184 |
// The const subscript operator. The map can be subscripted by the |
|
| 185 |
// actual keys of the graph. |
|
| 186 | 186 |
const Value& operator[](const Key& key) const {
|
| 187 | 187 |
int id = Parent::notifier()->id(key); |
| 188 | 188 |
return values[id]; |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 |
/// \brief Setter function of the map. |
|
| 192 |
/// |
|
| 193 |
/// Setter function of the map. Equivalent with map[key] = val. |
|
| 194 |
/// This is a compatibility feature with the not dereferable maps. |
|
| 191 |
// \brief Setter function of the map. |
|
| 192 |
// |
|
| 193 |
// Setter function of the map. Equivalent with map[key] = val. |
|
| 194 |
// This is a compatibility feature with the not dereferable maps. |
|
| 195 | 195 |
void set(const Key& key, const Value& val) {
|
| 196 | 196 |
(*this)[key] = val; |
| 197 | 197 |
} |
| 198 | 198 |
|
| 199 | 199 |
protected: |
| 200 | 200 |
|
| 201 |
/// \brief Adds a new key to the map. |
|
| 202 |
/// |
|
| 203 |
/// It adds a new key to the map. It called by the observer notifier |
|
| 204 |
/// and it overrides the add() member function of the observer base. |
|
| 201 |
// \brief Adds a new key to the map. |
|
| 202 |
// |
|
| 203 |
// It adds a new key to the map. It called by the observer notifier |
|
| 204 |
// and it overrides the add() member function of the observer base. |
|
| 205 | 205 |
virtual void add(const Key& key) {
|
| 206 | 206 |
Notifier* nf = Parent::notifier(); |
| 207 | 207 |
int id = nf->id(key); |
| 208 | 208 |
if (id >= capacity) {
|
| 209 | 209 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
| 210 | 210 |
while (new_capacity <= id) {
|
| 211 | 211 |
new_capacity <<= 1; |
| 212 | 212 |
} |
| 213 | 213 |
Value* new_values = allocator.allocate(new_capacity); |
| 214 | 214 |
Item it; |
| 215 | 215 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 216 | 216 |
int jd = nf->id(it);; |
| 217 | 217 |
if (id != jd) {
|
| 218 | 218 |
allocator.construct(&(new_values[jd]), values[jd]); |
| 219 | 219 |
allocator.destroy(&(values[jd])); |
| 220 | 220 |
} |
| 221 | 221 |
} |
| 222 | 222 |
if (capacity != 0) allocator.deallocate(values, capacity); |
| 223 | 223 |
values = new_values; |
| 224 | 224 |
capacity = new_capacity; |
| 225 | 225 |
} |
| 226 | 226 |
allocator.construct(&(values[id]), Value()); |
| 227 | 227 |
} |
| 228 | 228 |
|
| 229 |
/// \brief Adds more new keys to the map. |
|
| 230 |
/// |
|
| 231 |
/// It adds more new keys to the map. It called by the observer notifier |
|
| 232 |
/// and it overrides the add() member function of the observer base. |
|
| 229 |
// \brief Adds more new keys to the map. |
|
| 230 |
// |
|
| 231 |
// It adds more new keys to the map. It called by the observer notifier |
|
| 232 |
// and it overrides the add() member function of the observer base. |
|
| 233 | 233 |
virtual void add(const std::vector<Key>& keys) {
|
| 234 | 234 |
Notifier* nf = Parent::notifier(); |
| 235 | 235 |
int max_id = -1; |
| 236 | 236 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 237 | 237 |
int id = nf->id(keys[i]); |
| 238 | 238 |
if (id > max_id) {
|
| 239 | 239 |
max_id = id; |
| 240 | 240 |
} |
| 241 | 241 |
} |
| 242 | 242 |
if (max_id >= capacity) {
|
| 243 | 243 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
| 244 | 244 |
while (new_capacity <= max_id) {
|
| 245 | 245 |
new_capacity <<= 1; |
| 246 | 246 |
} |
| 247 | 247 |
Value* new_values = allocator.allocate(new_capacity); |
| 248 | 248 |
Item it; |
| 249 | 249 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 250 | 250 |
int id = nf->id(it); |
| 251 | 251 |
bool found = false; |
| 252 | 252 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 253 | 253 |
int jd = nf->id(keys[i]); |
| 254 | 254 |
if (id == jd) {
|
| 255 | 255 |
found = true; |
| 256 | 256 |
break; |
| 257 | 257 |
} |
| 258 | 258 |
} |
| 259 | 259 |
if (found) continue; |
| 260 | 260 |
allocator.construct(&(new_values[id]), values[id]); |
| 261 | 261 |
allocator.destroy(&(values[id])); |
| 262 | 262 |
} |
| 263 | 263 |
if (capacity != 0) allocator.deallocate(values, capacity); |
| 264 | 264 |
values = new_values; |
| 265 | 265 |
capacity = new_capacity; |
| 266 | 266 |
} |
| 267 | 267 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 268 | 268 |
int id = nf->id(keys[i]); |
| 269 | 269 |
allocator.construct(&(values[id]), Value()); |
| 270 | 270 |
} |
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 |
/// \brief Erase a key from the map. |
|
| 274 |
/// |
|
| 275 |
/// Erase a key from the map. It called by the observer notifier |
|
| 276 |
/// and it overrides the erase() member function of the observer base. |
|
| 273 |
// \brief Erase a key from the map. |
|
| 274 |
// |
|
| 275 |
// Erase a key from the map. It called by the observer notifier |
|
| 276 |
// and it overrides the erase() member function of the observer base. |
|
| 277 | 277 |
virtual void erase(const Key& key) {
|
| 278 | 278 |
int id = Parent::notifier()->id(key); |
| 279 | 279 |
allocator.destroy(&(values[id])); |
| 280 | 280 |
} |
| 281 | 281 |
|
| 282 |
/// \brief Erase more keys from the map. |
|
| 283 |
/// |
|
| 284 |
/// Erase more keys from the map. It called by the observer notifier |
|
| 285 |
/// and it overrides the erase() member function of the observer base. |
|
| 282 |
// \brief Erase more keys from the map. |
|
| 283 |
// |
|
| 284 |
// Erase more keys from the map. It called by the observer notifier |
|
| 285 |
// and it overrides the erase() member function of the observer base. |
|
| 286 | 286 |
virtual void erase(const std::vector<Key>& keys) {
|
| 287 | 287 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 288 | 288 |
int id = Parent::notifier()->id(keys[i]); |
| 289 | 289 |
allocator.destroy(&(values[id])); |
| 290 | 290 |
} |
| 291 | 291 |
} |
| 292 | 292 |
|
| 293 |
/// \brief Buildes the map. |
|
| 294 |
/// |
|
| 295 |
/// It buildes the map. It called by the observer notifier |
|
| 296 |
/// and it overrides the build() member function of the observer base. |
|
| 293 |
// \brief Buildes the map. |
|
| 294 |
// |
|
| 295 |
// It buildes the map. It called by the observer notifier |
|
| 296 |
// and it overrides the build() member function of the observer base. |
|
| 297 | 297 |
virtual void build() {
|
| 298 | 298 |
Notifier* nf = Parent::notifier(); |
| 299 | 299 |
allocate_memory(); |
| 300 | 300 |
Item it; |
| 301 | 301 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 302 | 302 |
int id = nf->id(it);; |
| 303 | 303 |
allocator.construct(&(values[id]), Value()); |
| 304 | 304 |
} |
| 305 | 305 |
} |
| 306 | 306 |
|
| 307 |
/// \brief Clear the map. |
|
| 308 |
/// |
|
| 309 |
/// It erase all items from the map. It called by the observer notifier |
|
| 310 |
/// and it overrides the clear() member function of the observer base. |
|
| 307 |
// \brief Clear the map. |
|
| 308 |
// |
|
| 309 |
// It erase all items from the map. It called by the observer notifier |
|
| 310 |
// and it overrides the clear() member function of the observer base. |
|
| 311 | 311 |
virtual void clear() {
|
| 312 | 312 |
Notifier* nf = Parent::notifier(); |
| 313 | 313 |
if (capacity != 0) {
|
| 314 | 314 |
Item it; |
| 315 | 315 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 316 | 316 |
int id = nf->id(it); |
| 317 | 317 |
allocator.destroy(&(values[id])); |
| 318 | 318 |
} |
| 319 | 319 |
allocator.deallocate(values, capacity); |
| 320 | 320 |
capacity = 0; |
| 321 | 321 |
} |
| 322 | 322 |
} |
| 323 | 323 |
|
| 324 | 324 |
private: |
| 325 | 325 |
|
| 326 | 326 |
void allocate_memory() {
|
| 327 | 327 |
int max_id = Parent::notifier()->maxId(); |
| 328 | 328 |
if (max_id == -1) {
|
| 329 | 329 |
capacity = 0; |
| 330 | 330 |
values = 0; |
| 331 | 331 |
return; |
| 332 | 332 |
} |
| 333 | 333 |
capacity = 1; |
| 334 | 334 |
while (capacity <= max_id) {
|
| 335 | 335 |
capacity <<= 1; |
| 336 | 336 |
} |
| 337 | 337 |
values = allocator.allocate(capacity); |
| 338 | 338 |
} |
| 339 | 339 |
|
| 340 | 340 |
int capacity; |
| 341 | 341 |
Value* values; |
| 342 | 342 |
Allocator allocator; |
| 343 | 343 |
|
| 344 | 344 |
}; |
| 345 | 345 |
|
| 346 | 346 |
} |
| 347 | 347 |
|
| 348 | 348 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_BASE_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_BASE_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/error.h> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/bits/map_extender.h> |
| 26 | 26 |
#include <lemon/bits/default_map.h> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
#include <lemon/concepts/maps.h> |
| 30 | 30 |
|
| 31 |
///\ingroup digraphbits |
|
| 32 |
///\file |
|
| 33 |
|
|
| 31 |
//\ingroup digraphbits |
|
| 32 |
//\file |
|
| 33 |
//\brief Extenders for the digraph types |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 |
/// \ingroup digraphbits |
|
| 37 |
/// |
|
| 38 |
// |
|
| 36 |
// \ingroup digraphbits |
|
| 37 |
// |
|
| 38 |
// \brief BaseDigraph to BaseGraph extender |
|
| 39 | 39 |
template <typename Base> |
| 40 | 40 |
class UndirDigraphExtender : public Base {
|
| 41 | 41 |
|
| 42 | 42 |
public: |
| 43 | 43 |
|
| 44 | 44 |
typedef Base Parent; |
| 45 | 45 |
typedef typename Parent::Arc Edge; |
| 46 | 46 |
typedef typename Parent::Node Node; |
| 47 | 47 |
|
| 48 | 48 |
typedef True UndirectedTag; |
| 49 | 49 |
|
| 50 | 50 |
class Arc : public Edge {
|
| 51 | 51 |
friend class UndirDigraphExtender; |
| 52 | 52 |
|
| 53 | 53 |
protected: |
| 54 | 54 |
bool forward; |
| 55 | 55 |
|
| 56 | 56 |
Arc(const Edge &ue, bool _forward) : |
| 57 | 57 |
Edge(ue), forward(_forward) {}
|
| 58 | 58 |
|
| 59 | 59 |
public: |
| 60 | 60 |
Arc() {}
|
| 61 | 61 |
|
| 62 | 62 |
// Invalid arc constructor |
| 63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {}
|
| 64 | 64 |
|
| 65 | 65 |
bool operator==(const Arc &that) const {
|
| 66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
| 67 | 67 |
} |
| 68 | 68 |
bool operator!=(const Arc &that) const {
|
| 69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
| 70 | 70 |
} |
| 71 | 71 |
bool operator<(const Arc &that) const {
|
| 72 | 72 |
return forward<that.forward || |
| 73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
| 74 | 74 |
} |
| 75 | 75 |
}; |
| 76 | 76 |
|
| 77 |
|
|
| 77 |
// First node of the edge |
|
| 78 | 78 |
Node u(const Edge &e) const {
|
| 79 | 79 |
return Parent::source(e); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 |
|
|
| 82 |
// Source of the given arc |
|
| 83 | 83 |
Node source(const Arc &e) const {
|
| 84 | 84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
| 85 | 85 |
} |
| 86 | 86 |
|
| 87 |
|
|
| 87 |
// Second node of the edge |
|
| 88 | 88 |
Node v(const Edge &e) const {
|
| 89 | 89 |
return Parent::target(e); |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 |
|
|
| 92 |
// Target of the given arc |
|
| 93 | 93 |
Node target(const Arc &e) const {
|
| 94 | 94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 |
/// \brief Directed arc from an edge. |
|
| 98 |
/// |
|
| 99 |
/// Returns a directed arc corresponding to the specified edge. |
|
| 100 |
/// If the given bool is true, the first node of the given edge and |
|
| 101 |
// |
|
| 97 |
// \brief Directed arc from an edge. |
|
| 98 |
// |
|
| 99 |
// Returns a directed arc corresponding to the specified edge. |
|
| 100 |
// If the given bool is true, the first node of the given edge and |
|
| 101 |
// the source node of the returned arc are the same. |
|
| 102 | 102 |
static Arc direct(const Edge &e, bool d) {
|
| 103 | 103 |
return Arc(e, d); |
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 |
/// Returns whether the given directed arc has the same orientation |
|
| 107 |
/// as the corresponding edge. |
|
| 106 |
// Returns whether the given directed arc has the same orientation |
|
| 107 |
// as the corresponding edge. |
|
| 108 | 108 |
static bool direction(const Arc &a) { return a.forward; }
|
| 109 | 109 |
|
| 110 | 110 |
using Parent::first; |
| 111 | 111 |
using Parent::next; |
| 112 | 112 |
|
| 113 | 113 |
void first(Arc &e) const {
|
| 114 | 114 |
Parent::first(e); |
| 115 | 115 |
e.forward=true; |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
void next(Arc &e) const {
|
| 119 | 119 |
if( e.forward ) {
|
| 120 | 120 |
e.forward = false; |
| 121 | 121 |
} |
| 122 | 122 |
else {
|
| 123 | 123 |
Parent::next(e); |
| 124 | 124 |
e.forward = true; |
| 125 | 125 |
} |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
void firstOut(Arc &e, const Node &n) const {
|
| 129 | 129 |
Parent::firstIn(e,n); |
| 130 | 130 |
if( Edge(e) != INVALID ) {
|
| 131 | 131 |
e.forward = false; |
| 132 | 132 |
} |
| 133 | 133 |
else {
|
| 134 | 134 |
Parent::firstOut(e,n); |
| 135 | 135 |
e.forward = true; |
| 136 | 136 |
} |
| 137 | 137 |
} |
| 138 | 138 |
void nextOut(Arc &e) const {
|
| 139 | 139 |
if( ! e.forward ) {
|
| 140 | 140 |
Node n = Parent::target(e); |
| 141 | 141 |
Parent::nextIn(e); |
| 142 | 142 |
if( Edge(e) == INVALID ) {
|
| 143 | 143 |
Parent::firstOut(e, n); |
| 144 | 144 |
e.forward = true; |
| 145 | 145 |
} |
| 146 | 146 |
} |
| 147 | 147 |
else {
|
| 148 | 148 |
Parent::nextOut(e); |
| 149 | 149 |
} |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
void firstIn(Arc &e, const Node &n) const {
|
| 153 | 153 |
Parent::firstOut(e,n); |
| 154 | 154 |
if( Edge(e) != INVALID ) {
|
| 155 | 155 |
e.forward = false; |
| 156 | 156 |
} |
| 157 | 157 |
else {
|
| 158 | 158 |
Parent::firstIn(e,n); |
| 159 | 159 |
e.forward = true; |
| 160 | 160 |
} |
| 161 | 161 |
} |
| 162 | 162 |
void nextIn(Arc &e) const {
|
| 163 | 163 |
if( ! e.forward ) {
|
| 164 | 164 |
Node n = Parent::source(e); |
| 165 | 165 |
Parent::nextOut(e); |
| 166 | 166 |
if( Edge(e) == INVALID ) {
|
| 167 | 167 |
Parent::firstIn(e, n); |
| 168 | 168 |
e.forward = true; |
| 169 | 169 |
} |
| 170 | 170 |
} |
| 171 | 171 |
else {
|
| 172 | 172 |
Parent::nextIn(e); |
| 173 | 173 |
} |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
void firstInc(Edge &e, bool &d, const Node &n) const {
|
| 177 | 177 |
d = true; |
| 178 | 178 |
Parent::firstOut(e, n); |
| 179 | 179 |
if (e != INVALID) return; |
| 180 | 180 |
d = false; |
| 181 | 181 |
Parent::firstIn(e, n); |
| 182 | 182 |
} |
| 183 | 183 |
|
| 184 | 184 |
void nextInc(Edge &e, bool &d) const {
|
| 185 | 185 |
if (d) {
|
| 186 | 186 |
Node s = Parent::source(e); |
| 187 | 187 |
Parent::nextOut(e); |
| 188 | 188 |
if (e != INVALID) return; |
| 189 | 189 |
d = false; |
| 190 | 190 |
Parent::firstIn(e, s); |
| 191 | 191 |
} else {
|
| 192 | 192 |
Parent::nextIn(e); |
| 193 | 193 |
} |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 | 196 |
Node nodeFromId(int ix) const {
|
| 197 | 197 |
return Parent::nodeFromId(ix); |
| 198 | 198 |
} |
| 199 | 199 |
|
| 200 | 200 |
Arc arcFromId(int ix) const {
|
| 201 | 201 |
return direct(Parent::arcFromId(ix >> 1), bool(ix & 1)); |
| 202 | 202 |
} |
| 203 | 203 |
| 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_BEZIER_H |
| 20 | 20 |
#define LEMON_BEZIER_H |
| 21 | 21 |
|
| 22 |
///\ingroup misc |
|
| 23 |
///\file |
|
| 24 |
///\brief Classes to compute with Bezier curves. |
|
| 25 |
/// |
|
| 26 |
// |
|
| 22 |
//\ingroup misc |
|
| 23 |
//\file |
|
| 24 |
//\brief Classes to compute with Bezier curves. |
|
| 25 |
// |
|
| 26 |
//Up to now this file is used internally by \ref graph_to_eps.h |
|
| 27 | 27 |
|
| 28 | 28 |
#include<lemon/dim2.h> |
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
namespace dim2 {
|
| 32 | 32 |
|
| 33 | 33 |
class BezierBase {
|
| 34 | 34 |
public: |
| 35 | 35 |
typedef lemon::dim2::Point<double> Point; |
| 36 | 36 |
protected: |
| 37 | 37 |
static Point conv(Point x,Point y,double t) {return (1-t)*x+t*y;}
|
| 38 | 38 |
}; |
| 39 | 39 |
|
| 40 | 40 |
class Bezier1 : public BezierBase |
| 41 | 41 |
{
|
| 42 | 42 |
public: |
| 43 | 43 |
Point p1,p2; |
| 44 | 44 |
|
| 45 | 45 |
Bezier1() {}
|
| 46 | 46 |
Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {}
|
| 47 | 47 |
|
| 48 | 48 |
Point operator()(double t) const |
| 49 | 49 |
{
|
| 50 | 50 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
| 51 | 51 |
return conv(p1,p2,t); |
| 52 | 52 |
} |
| 53 | 53 |
Bezier1 before(double t) const |
| 54 | 54 |
{
|
| 55 | 55 |
return Bezier1(p1,conv(p1,p2,t)); |
| 56 | 56 |
} |
| 57 | 57 |
|
| 58 | 58 |
Bezier1 after(double t) const |
| 59 | 59 |
{
|
| 60 | 60 |
return Bezier1(conv(p1,p2,t),p2); |
| 61 | 61 |
} |
| 62 | 62 |
|
| 63 | 63 |
Bezier1 revert() const { return Bezier1(p2,p1);}
|
| 64 | 64 |
Bezier1 operator()(double a,double b) const { return before(b).after(a/b); }
|
| 65 | 65 |
Point grad() const { return p2-p1; }
|
| 66 | 66 |
Point norm() const { return rot90(p2-p1); }
|
| 67 | 67 |
Point grad(double) const { return grad(); }
|
| 68 | 68 |
Point norm(double t) const { return rot90(grad(t)); }
|
| 69 | 69 |
}; |
| 70 | 70 |
|
| 71 | 71 |
class Bezier2 : public BezierBase |
| 72 | 72 |
{
|
| 73 | 73 |
public: |
| 74 | 74 |
Point p1,p2,p3; |
| 75 | 75 |
|
| 76 | 76 |
Bezier2() {}
|
| 77 | 77 |
Bezier2(Point _p1, Point _p2, Point _p3) :p1(_p1), p2(_p2), p3(_p3) {}
|
| 78 | 78 |
Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
|
| 79 | 79 |
Point operator()(double t) const |
| 80 | 80 |
{
|
| 81 | 81 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
| 82 | 82 |
return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3; |
| 83 | 83 |
} |
| 84 | 84 |
Bezier2 before(double t) const |
| 85 | 85 |
{
|
| 86 | 86 |
Point q(conv(p1,p2,t)); |
| 87 | 87 |
Point r(conv(p2,p3,t)); |
| 88 | 88 |
return Bezier2(p1,q,conv(q,r,t)); |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 | 91 |
Bezier2 after(double t) const |
| 92 | 92 |
{
|
| 93 | 93 |
Point q(conv(p1,p2,t)); |
| 94 | 94 |
Point r(conv(p2,p3,t)); |
| 95 | 95 |
return Bezier2(conv(q,r,t),r,p3); |
| 96 | 96 |
} |
| 97 | 97 |
Bezier2 revert() const { return Bezier2(p3,p2,p1);}
|
| 98 | 98 |
Bezier2 operator()(double a,double b) const { return before(b).after(a/b); }
|
| 99 | 99 |
Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); }
|
| 100 | 100 |
Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); }
|
| 101 | 101 |
Point grad(double t) const { return grad()(t); }
|
| 102 | 102 |
Point norm(double t) const { return rot90(grad(t)); }
|
| 103 | 103 |
}; |
| 104 | 104 |
|
| 105 | 105 |
class Bezier3 : public BezierBase |
| 106 | 106 |
{
|
| 107 | 107 |
public: |
| 108 | 108 |
Point p1,p2,p3,p4; |
| 109 | 109 |
|
| 110 | 110 |
Bezier3() {}
|
| 111 | 111 |
Bezier3(Point _p1, Point _p2, Point _p3, Point _p4) |
| 112 | 112 |
: p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
|
| 113 | 113 |
Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), |
| 114 | 114 |
p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
|
| 115 | 115 |
Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)), |
| 116 | 116 |
p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
|
| 117 | 117 |
|
| 118 | 118 |
Point operator()(double t) const |
| 119 | 119 |
{
|
| 120 | 120 |
// return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t); |
| 121 | 121 |
return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+ |
| 122 | 122 |
(3*t*t*(1-t))*p3+(t*t*t)*p4; |
| 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_DEFAULT_MAP_H |
| 20 | 20 |
#define LEMON_BITS_DEFAULT_MAP_H |
| 21 | 21 |
|
| 22 |
|
|
| 23 | 22 |
#include <lemon/bits/array_map.h> |
| 24 | 23 |
#include <lemon/bits/vector_map.h> |
| 25 | 24 |
//#include <lemon/bits/debug_map.h> |
| 26 | 25 |
|
| 27 |
///\ingroup graphbits |
|
| 28 |
///\file |
|
| 29 |
|
|
| 26 |
//\ingroup graphbits |
|
| 27 |
//\file |
|
| 28 |
//\brief Graph maps that construct and destruct their elements dynamically. |
|
| 30 | 29 |
|
| 31 | 30 |
namespace lemon {
|
| 32 | 31 |
|
| 33 | 32 |
|
| 34 | 33 |
//#ifndef LEMON_USE_DEBUG_MAP |
| 35 | 34 |
|
| 36 | 35 |
template <typename _Graph, typename _Item, typename _Value> |
| 37 | 36 |
struct DefaultMapSelector {
|
| 38 | 37 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
| 39 | 38 |
}; |
| 40 | 39 |
|
| 41 | 40 |
// bool |
| 42 | 41 |
template <typename _Graph, typename _Item> |
| 43 | 42 |
struct DefaultMapSelector<_Graph, _Item, bool> {
|
| 44 | 43 |
typedef VectorMap<_Graph, _Item, bool> Map; |
| 45 | 44 |
}; |
| 46 | 45 |
|
| 47 | 46 |
// char |
| 48 | 47 |
template <typename _Graph, typename _Item> |
| 49 | 48 |
struct DefaultMapSelector<_Graph, _Item, char> {
|
| 50 | 49 |
typedef VectorMap<_Graph, _Item, char> Map; |
| 51 | 50 |
}; |
| 52 | 51 |
|
| 53 | 52 |
template <typename _Graph, typename _Item> |
| 54 | 53 |
struct DefaultMapSelector<_Graph, _Item, signed char> {
|
| 55 | 54 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
| 56 | 55 |
}; |
| 57 | 56 |
|
| 58 | 57 |
template <typename _Graph, typename _Item> |
| 59 | 58 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> {
|
| 60 | 59 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
| 61 | 60 |
}; |
| 62 | 61 |
|
| 63 | 62 |
|
| 64 | 63 |
// int |
| 65 | 64 |
template <typename _Graph, typename _Item> |
| 66 | 65 |
struct DefaultMapSelector<_Graph, _Item, signed int> {
|
| 67 | 66 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
| 68 | 67 |
}; |
| 69 | 68 |
|
| 70 | 69 |
template <typename _Graph, typename _Item> |
| 71 | 70 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> {
|
| 72 | 71 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
| 73 | 72 |
}; |
| 74 | 73 |
|
| 75 | 74 |
|
| 76 | 75 |
// short |
| 77 | 76 |
template <typename _Graph, typename _Item> |
| 78 | 77 |
struct DefaultMapSelector<_Graph, _Item, signed short> {
|
| 79 | 78 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
| 80 | 79 |
}; |
| 81 | 80 |
|
| 82 | 81 |
template <typename _Graph, typename _Item> |
| 83 | 82 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> {
|
| 84 | 83 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
| 85 | 84 |
}; |
| 86 | 85 |
|
| 87 | 86 |
|
| 88 | 87 |
// long |
| 89 | 88 |
template <typename _Graph, typename _Item> |
| 90 | 89 |
struct DefaultMapSelector<_Graph, _Item, signed long> {
|
| 91 | 90 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
| 92 | 91 |
}; |
| 93 | 92 |
|
| 94 | 93 |
template <typename _Graph, typename _Item> |
| 95 | 94 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> {
|
| 96 | 95 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
| 97 | 96 |
}; |
| 98 | 97 |
|
| 99 | 98 |
|
| 100 | 99 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
| 101 | 100 |
|
| 102 | 101 |
// long long |
| 103 | 102 |
template <typename _Graph, typename _Item> |
| 104 | 103 |
struct DefaultMapSelector<_Graph, _Item, signed long long> {
|
| 105 | 104 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
| 106 | 105 |
}; |
| 107 | 106 |
|
| 108 | 107 |
template <typename _Graph, typename _Item> |
| 109 | 108 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
|
| 110 | 109 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
| 111 | 110 |
}; |
| 112 | 111 |
|
| 113 | 112 |
#endif |
| 114 | 113 |
|
| 115 | 114 |
|
| 116 | 115 |
// float |
| 117 | 116 |
template <typename _Graph, typename _Item> |
| 118 | 117 |
struct DefaultMapSelector<_Graph, _Item, float> {
|
| 119 | 118 |
typedef VectorMap<_Graph, _Item, float> Map; |
| 120 | 119 |
}; |
| 121 | 120 |
|
| 122 | 121 |
|
| 123 | 122 |
// double |
| 124 | 123 |
template <typename _Graph, typename _Item> |
| 125 | 124 |
struct DefaultMapSelector<_Graph, _Item, double> {
|
| 126 | 125 |
typedef VectorMap<_Graph, _Item, double> Map; |
| 127 | 126 |
}; |
| 128 | 127 |
|
| 129 | 128 |
|
| 130 | 129 |
// long double |
| 131 | 130 |
template <typename _Graph, typename _Item> |
| 132 | 131 |
struct DefaultMapSelector<_Graph, _Item, long double> {
|
| 133 | 132 |
typedef VectorMap<_Graph, _Item, long double> Map; |
| 134 | 133 |
}; |
| 135 | 134 |
|
| 136 | 135 |
|
| 137 | 136 |
// pointer |
| 138 | 137 |
template <typename _Graph, typename _Item, typename _Ptr> |
| 139 | 138 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
|
| 140 | 139 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
| 141 | 140 |
}; |
| 142 | 141 |
|
| 143 | 142 |
// #else |
| 144 | 143 |
|
| 145 | 144 |
// template <typename _Graph, typename _Item, typename _Value> |
| 146 | 145 |
// struct DefaultMapSelector {
|
| 147 | 146 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
| 148 | 147 |
// }; |
| 149 | 148 |
|
| 150 | 149 |
// #endif |
| 151 | 150 |
|
| 152 |
// |
|
| 151 |
// DefaultMap class |
|
| 153 | 152 |
template <typename _Graph, typename _Item, typename _Value> |
| 154 | 153 |
class DefaultMap |
| 155 | 154 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map {
|
| 156 | 155 |
public: |
| 157 | 156 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
| 158 | 157 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
| 159 | 158 |
|
| 160 | 159 |
typedef typename Parent::Graph Graph; |
| 161 | 160 |
typedef typename Parent::Value Value; |
| 162 | 161 |
|
| 163 | 162 |
explicit DefaultMap(const Graph& graph) : Parent(graph) {}
|
| 164 | 163 |
DefaultMap(const Graph& graph, const Value& value) |
| 165 | 164 |
: Parent(graph, value) {}
|
| 166 | 165 |
|
| 167 | 166 |
DefaultMap& operator=(const DefaultMap& cmap) {
|
| 168 | 167 |
return operator=<DefaultMap>(cmap); |
| 169 | 168 |
} |
| 170 | 169 |
|
| 171 | 170 |
template <typename CMap> |
| 172 | 171 |
DefaultMap& operator=(const CMap& cmap) {
|
| 173 | 172 |
Parent::operator=(cmap); |
| 174 | 173 |
return *this; |
| 175 | 174 |
} |
| 176 | 175 |
|
| 177 | 176 |
}; |
| 178 | 177 |
|
| 179 | 178 |
} |
| 180 | 179 |
|
| 181 | 180 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
// This file contains a modified version of the enable_if library from BOOST. |
| 20 | 20 |
// See the appropriate copyright notice below. |
| 21 | 21 |
|
| 22 | 22 |
// Boost enable_if library |
| 23 | 23 |
|
| 24 | 24 |
// Copyright 2003 (c) The Trustees of Indiana University. |
| 25 | 25 |
|
| 26 | 26 |
// Use, modification, and distribution is subject to the Boost Software |
| 27 | 27 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 28 | 28 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 29 | 29 |
|
| 30 | 30 |
// Authors: Jaakko Jarvi (jajarvi at osl.iu.edu) |
| 31 | 31 |
// Jeremiah Willcock (jewillco at osl.iu.edu) |
| 32 | 32 |
// Andrew Lumsdaine (lums at osl.iu.edu) |
| 33 | 33 |
|
| 34 | 34 |
|
| 35 | 35 |
#ifndef LEMON_BITS_ENABLE_IF_H |
| 36 | 36 |
#define LEMON_BITS_ENABLE_IF_H |
| 37 | 37 |
|
| 38 |
///\file |
|
| 39 |
///\brief Miscellaneous basic utilities |
|
| 38 |
//\file |
|
| 39 |
//\brief Miscellaneous basic utilities |
|
| 40 | 40 |
|
| 41 | 41 |
namespace lemon |
| 42 | 42 |
{
|
| 43 | 43 |
|
| 44 |
|
|
| 44 |
// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
| 45 | 45 |
|
| 46 |
/// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
| 47 |
/// |
|
| 48 |
// |
|
| 46 |
// Basic type for defining "tags". A "YES" condition for \c enable_if. |
|
| 47 |
// |
|
| 48 |
//\sa False |
|
| 49 | 49 |
struct True {
|
| 50 |
// |
|
| 50 |
//\e |
|
| 51 | 51 |
static const bool value = true; |
| 52 | 52 |
}; |
| 53 | 53 |
|
| 54 |
|
|
| 54 |
// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
| 55 | 55 |
|
| 56 |
/// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
| 57 |
/// |
|
| 58 |
// |
|
| 56 |
// Basic type for defining "tags". A "NO" condition for \c enable_if. |
|
| 57 |
// |
|
| 58 |
//\sa True |
|
| 59 | 59 |
struct False {
|
| 60 |
// |
|
| 60 |
//\e |
|
| 61 | 61 |
static const bool value = false; |
| 62 | 62 |
}; |
| 63 | 63 |
|
| 64 | 64 |
|
| 65 | 65 |
|
| 66 | 66 |
template <typename T> |
| 67 | 67 |
struct Wrap {
|
| 68 | 68 |
const T &value; |
| 69 | 69 |
Wrap(const T &t) : value(t) {}
|
| 70 | 70 |
}; |
| 71 | 71 |
|
| 72 | 72 |
/**************** dummy class to avoid ambiguity ****************/ |
| 73 | 73 |
|
| 74 | 74 |
template<int T> struct dummy { dummy(int) {} };
|
| 75 | 75 |
|
| 76 | 76 |
/**************** enable_if from BOOST ****************/ |
| 77 | 77 |
|
| 78 | 78 |
template <typename Type, typename T = void> |
| 79 | 79 |
struct exists {
|
| 80 | 80 |
typedef T type; |
| 81 | 81 |
}; |
| 82 | 82 |
|
| 83 | 83 |
|
| 84 | 84 |
template <bool B, class T = void> |
| 85 | 85 |
struct enable_if_c {
|
| 86 | 86 |
typedef T type; |
| 87 | 87 |
}; |
| 88 | 88 |
|
| 89 | 89 |
template <class T> |
| 90 | 90 |
struct enable_if_c<false, T> {};
|
| 91 | 91 |
|
| 92 | 92 |
template <class Cond, class T = void> |
| 93 | 93 |
struct enable_if : public enable_if_c<Cond::value, T> {};
|
| 94 | 94 |
|
| 95 | 95 |
template <bool B, class T> |
| 96 | 96 |
struct lazy_enable_if_c {
|
| 97 | 97 |
typedef typename T::type type; |
| 98 | 98 |
}; |
| 99 | 99 |
|
| 100 | 100 |
template <class T> |
| 101 | 101 |
struct lazy_enable_if_c<false, T> {};
|
| 102 | 102 |
|
| 103 | 103 |
template <class Cond, class T> |
| 104 | 104 |
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
|
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
template <bool B, class T = void> |
| 108 | 108 |
struct disable_if_c {
|
| 109 | 109 |
typedef T type; |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
template <class T> |
| 113 | 113 |
struct disable_if_c<true, T> {};
|
| 114 | 114 |
|
| 115 | 115 |
template <class Cond, class T = void> |
| 116 | 116 |
struct disable_if : public disable_if_c<Cond::value, T> {};
|
| 117 | 117 |
|
| 118 | 118 |
template <bool B, class T> |
| 119 | 119 |
struct lazy_disable_if_c {
|
| 120 | 120 |
typedef typename T::type type; |
| 121 | 121 |
}; |
| 122 | 122 |
|
| 123 | 123 |
template <class T> |
| 124 | 124 |
struct lazy_disable_if_c<true, T> {};
|
| 125 | 125 |
|
| 126 | 126 |
template <class Cond, class T> |
| 127 | 127 |
struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
|
| 128 | 128 |
|
| 129 | 129 |
} // namespace lemon |
| 130 | 130 |
|
| 131 | 131 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_GRAPH_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_GRAPH_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/bits/map_extender.h> |
| 25 | 25 |
#include <lemon/bits/default_map.h> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/concept_check.h> |
| 28 | 28 |
#include <lemon/concepts/maps.h> |
| 29 | 29 |
|
| 30 |
///\ingroup graphbits |
|
| 31 |
///\file |
|
| 32 |
|
|
| 30 |
//\ingroup graphbits |
|
| 31 |
//\file |
|
| 32 |
//\brief Extenders for the digraph types |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 |
/// \ingroup graphbits |
|
| 36 |
/// |
|
| 37 |
// |
|
| 35 |
// \ingroup graphbits |
|
| 36 |
// |
|
| 37 |
// \brief Extender for the Digraphs |
|
| 38 | 38 |
template <typename Base> |
| 39 | 39 |
class DigraphExtender : public Base {
|
| 40 | 40 |
public: |
| 41 | 41 |
|
| 42 | 42 |
typedef Base Parent; |
| 43 | 43 |
typedef DigraphExtender Digraph; |
| 44 | 44 |
|
| 45 | 45 |
// Base extensions |
| 46 | 46 |
|
| 47 | 47 |
typedef typename Parent::Node Node; |
| 48 | 48 |
typedef typename Parent::Arc Arc; |
| 49 | 49 |
|
| 50 | 50 |
int maxId(Node) const {
|
| 51 | 51 |
return Parent::maxNodeId(); |
| 52 | 52 |
} |
| 53 | 53 |
|
| 54 | 54 |
int maxId(Arc) const {
|
| 55 | 55 |
return Parent::maxArcId(); |
| 56 | 56 |
} |
| 57 | 57 |
|
| 58 | 58 |
Node fromId(int id, Node) const {
|
| 59 | 59 |
return Parent::nodeFromId(id); |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
Arc fromId(int id, Arc) const {
|
| 63 | 63 |
return Parent::arcFromId(id); |
| 64 | 64 |
} |
| 65 | 65 |
|
| 66 | 66 |
Node oppositeNode(const Node &node, const Arc &arc) const {
|
| 67 | 67 |
if (node == Parent::source(arc)) |
| 68 | 68 |
return Parent::target(arc); |
| 69 | 69 |
else if(node == Parent::target(arc)) |
| 70 | 70 |
return Parent::source(arc); |
| 71 | 71 |
else |
| 72 | 72 |
return INVALID; |
| 73 | 73 |
} |
| 74 | 74 |
|
| 75 | 75 |
// Alterable extension |
| 76 | 76 |
|
| 77 | 77 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
| 78 | 78 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
| 79 | 79 |
|
| 80 | 80 |
|
| 81 | 81 |
protected: |
| 82 | 82 |
|
| 83 | 83 |
mutable NodeNotifier node_notifier; |
| 84 | 84 |
mutable ArcNotifier arc_notifier; |
| 85 | 85 |
|
| 86 | 86 |
public: |
| 87 | 87 |
|
| 88 | 88 |
NodeNotifier& notifier(Node) const {
|
| 89 | 89 |
return node_notifier; |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 | 92 |
ArcNotifier& notifier(Arc) const {
|
| 93 | 93 |
return arc_notifier; |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
class NodeIt : public Node {
|
| 97 | 97 |
const Digraph* _digraph; |
| 98 | 98 |
public: |
| 99 | 99 |
|
| 100 | 100 |
NodeIt() {}
|
| 101 | 101 |
|
| 102 | 102 |
NodeIt(Invalid i) : Node(i) { }
|
| 103 | 103 |
|
| 104 | 104 |
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
|
| 105 | 105 |
_digraph->first(static_cast<Node&>(*this)); |
| 106 | 106 |
} |
| 107 | 107 |
|
| 108 | 108 |
NodeIt(const Digraph& digraph, const Node& node) |
| 109 | 109 |
: Node(node), _digraph(&digraph) {}
|
| 110 | 110 |
|
| 111 | 111 |
NodeIt& operator++() {
|
| 112 | 112 |
_digraph->next(*this); |
| 113 | 113 |
return *this; |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 | 116 |
}; |
| 117 | 117 |
|
| 118 | 118 |
|
| 119 | 119 |
class ArcIt : public Arc {
|
| 120 | 120 |
const Digraph* _digraph; |
| 121 | 121 |
public: |
| 122 | 122 |
|
| 123 | 123 |
ArcIt() { }
|
| 124 | 124 |
|
| 125 | 125 |
ArcIt(Invalid i) : Arc(i) { }
|
| 126 | 126 |
|
| 127 | 127 |
explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
|
| 128 | 128 |
_digraph->first(static_cast<Arc&>(*this)); |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
ArcIt(const Digraph& digraph, const Arc& arc) : |
| 132 | 132 |
Arc(arc), _digraph(&digraph) { }
|
| 133 | 133 |
|
| 134 | 134 |
ArcIt& operator++() {
|
| 135 | 135 |
_digraph->next(*this); |
| 136 | 136 |
return *this; |
| 137 | 137 |
} |
| 138 | 138 |
|
| 139 | 139 |
}; |
| 140 | 140 |
|
| 141 | 141 |
|
| 142 | 142 |
class OutArcIt : public Arc {
|
| 143 | 143 |
const Digraph* _digraph; |
| 144 | 144 |
public: |
| 145 | 145 |
|
| 146 | 146 |
OutArcIt() { }
|
| 147 | 147 |
|
| 148 | 148 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 149 | 149 |
|
| 150 | 150 |
OutArcIt(const Digraph& digraph, const Node& node) |
| 151 | 151 |
: _digraph(&digraph) {
|
| 152 | 152 |
_digraph->firstOut(*this, node); |
| 153 | 153 |
} |
| 154 | 154 |
|
| 155 | 155 |
OutArcIt(const Digraph& digraph, const Arc& arc) |
| 156 | 156 |
: Arc(arc), _digraph(&digraph) {}
|
| 157 | 157 |
|
| 158 | 158 |
OutArcIt& operator++() {
|
| 159 | 159 |
_digraph->nextOut(*this); |
| 160 | 160 |
return *this; |
| 161 | 161 |
} |
| 162 | 162 |
|
| 163 | 163 |
}; |
| 164 | 164 |
|
| 165 | 165 |
|
| 166 | 166 |
class InArcIt : public Arc {
|
| 167 | 167 |
const Digraph* _digraph; |
| 168 | 168 |
public: |
| 169 | 169 |
|
| 170 | 170 |
InArcIt() { }
|
| 171 | 171 |
|
| 172 | 172 |
InArcIt(Invalid i) : Arc(i) { }
|
| 173 | 173 |
|
| 174 | 174 |
InArcIt(const Digraph& digraph, const Node& node) |
| 175 | 175 |
: _digraph(&digraph) {
|
| 176 | 176 |
_digraph->firstIn(*this, node); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
InArcIt(const Digraph& digraph, const Arc& arc) : |
| 180 | 180 |
Arc(arc), _digraph(&digraph) {}
|
| 181 | 181 |
|
| 182 | 182 |
InArcIt& operator++() {
|
| 183 | 183 |
_digraph->nextIn(*this); |
| 184 | 184 |
return *this; |
| 185 | 185 |
} |
| 186 | 186 |
|
| 187 | 187 |
}; |
| 188 | 188 |
|
| 189 |
/// \brief Base node of the iterator |
|
| 190 |
/// |
|
| 191 |
// |
|
| 189 |
// \brief Base node of the iterator |
|
| 190 |
// |
|
| 191 |
// Returns the base node (i.e. the source in this case) of the iterator |
|
| 192 | 192 |
Node baseNode(const OutArcIt &arc) const {
|
| 193 | 193 |
return Parent::source(arc); |
| 194 | 194 |
} |
| 195 |
/// \brief Running node of the iterator |
|
| 196 |
/// |
|
| 197 |
/// Returns the running node (i.e. the target in this case) of the |
|
| 198 |
/// iterator |
|
| 195 |
// \brief Running node of the iterator |
|
| 196 |
// |
|
| 197 |
// Returns the running node (i.e. the target in this case) of the |
|
| 198 |
// iterator |
|
| 199 | 199 |
Node runningNode(const OutArcIt &arc) const {
|
| 200 | 200 |
return Parent::target(arc); |
| 201 | 201 |
} |
| 202 | 202 |
|
| 203 |
/// \brief Base node of the iterator |
|
| 204 |
/// |
|
| 205 |
// |
|
| 203 |
// \brief Base node of the iterator |
|
| 204 |
// |
|
| 205 |
// Returns the base node (i.e. the target in this case) of the iterator |
|
| 206 | 206 |
Node baseNode(const InArcIt &arc) const {
|
| 207 | 207 |
return Parent::target(arc); |
| 208 | 208 |
} |
| 209 |
/// \brief Running node of the iterator |
|
| 210 |
/// |
|
| 211 |
/// Returns the running node (i.e. the source in this case) of the |
|
| 212 |
/// iterator |
|
| 209 |
// \brief Running node of the iterator |
|
| 210 |
// |
|
| 211 |
// Returns the running node (i.e. the source in this case) of the |
|
| 212 |
// iterator |
|
| 213 | 213 |
Node runningNode(const InArcIt &arc) const {
|
| 214 | 214 |
return Parent::source(arc); |
| 215 | 215 |
} |
| 216 | 216 |
|
| 217 | 217 |
|
| 218 | 218 |
template <typename _Value> |
| 219 | 219 |
class NodeMap |
| 220 | 220 |
: public MapExtender<DefaultMap<Digraph, Node, _Value> > {
|
| 221 | 221 |
public: |
| 222 | 222 |
typedef DigraphExtender Digraph; |
| 223 | 223 |
typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent; |
| 224 | 224 |
|
| 225 | 225 |
explicit NodeMap(const Digraph& digraph) |
| 226 | 226 |
: Parent(digraph) {}
|
| 227 | 227 |
NodeMap(const Digraph& digraph, const _Value& value) |
| 228 | 228 |
: Parent(digraph, value) {}
|
| 229 | 229 |
|
| 230 | 230 |
private: |
| 231 | 231 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 232 | 232 |
return operator=<NodeMap>(cmap); |
| 233 | 233 |
} |
| 234 | 234 |
|
| 235 | 235 |
template <typename CMap> |
| 236 | 236 |
NodeMap& operator=(const CMap& cmap) {
|
| 237 | 237 |
Parent::operator=(cmap); |
| 238 | 238 |
return *this; |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
}; |
| 242 | 242 |
|
| 243 | 243 |
template <typename _Value> |
| 244 | 244 |
class ArcMap |
| 245 | 245 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
|
| 246 | 246 |
public: |
| 247 | 247 |
typedef DigraphExtender Digraph; |
| 248 | 248 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
| 249 | 249 |
|
| 250 | 250 |
explicit ArcMap(const Digraph& digraph) |
| 251 | 251 |
: Parent(digraph) {}
|
| 252 | 252 |
ArcMap(const Digraph& digraph, const _Value& value) |
| 253 | 253 |
: Parent(digraph, value) {}
|
| 254 | 254 |
|
| 255 | 255 |
private: |
| 256 | 256 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 257 | 257 |
return operator=<ArcMap>(cmap); |
| 258 | 258 |
} |
| 259 | 259 |
|
| 260 | 260 |
template <typename CMap> |
| 261 | 261 |
ArcMap& operator=(const CMap& cmap) {
|
| 262 | 262 |
Parent::operator=(cmap); |
| 263 | 263 |
return *this; |
| 264 | 264 |
} |
| 265 | 265 |
}; |
| 266 | 266 |
|
| 267 | 267 |
|
| 268 | 268 |
Node addNode() {
|
| 269 | 269 |
Node node = Parent::addNode(); |
| 270 | 270 |
notifier(Node()).add(node); |
| 271 | 271 |
return node; |
| 272 | 272 |
} |
| 273 | 273 |
|
| 274 | 274 |
Arc addArc(const Node& from, const Node& to) {
|
| 275 | 275 |
Arc arc = Parent::addArc(from, to); |
| 276 | 276 |
notifier(Arc()).add(arc); |
| 277 | 277 |
return arc; |
| 278 | 278 |
} |
| 279 | 279 |
|
| 280 | 280 |
void clear() {
|
| 281 | 281 |
notifier(Arc()).clear(); |
| 282 | 282 |
notifier(Node()).clear(); |
| 283 | 283 |
Parent::clear(); |
| 284 | 284 |
} |
| 285 | 285 |
|
| 286 | 286 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
| 287 | 287 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
| 288 | 288 |
Parent::build(digraph, nodeRef, arcRef); |
| 289 | 289 |
notifier(Node()).build(); |
| 290 | 290 |
notifier(Arc()).build(); |
| 291 | 291 |
} |
| 292 | 292 |
|
| 293 | 293 |
void erase(const Node& node) {
|
| 294 | 294 |
Arc arc; |
| 295 | 295 |
Parent::firstOut(arc, node); |
| 296 | 296 |
while (arc != INVALID ) {
|
| 297 | 297 |
erase(arc); |
| 298 | 298 |
Parent::firstOut(arc, node); |
| 299 | 299 |
} |
| 300 | 300 |
|
| 301 | 301 |
Parent::firstIn(arc, node); |
| 302 | 302 |
while (arc != INVALID ) {
|
| 303 | 303 |
erase(arc); |
| 304 | 304 |
Parent::firstIn(arc, node); |
| 305 | 305 |
} |
| 306 | 306 |
|
| 307 | 307 |
notifier(Node()).erase(node); |
| 308 | 308 |
Parent::erase(node); |
| 309 | 309 |
} |
| 310 | 310 |
|
| 311 | 311 |
void erase(const Arc& arc) {
|
| 312 | 312 |
notifier(Arc()).erase(arc); |
| 313 | 313 |
Parent::erase(arc); |
| 314 | 314 |
} |
| 315 | 315 |
|
| 316 | 316 |
DigraphExtender() {
|
| 317 | 317 |
node_notifier.setContainer(*this); |
| 318 | 318 |
arc_notifier.setContainer(*this); |
| 319 | 319 |
} |
| 320 | 320 |
|
| 321 | 321 |
|
| 322 | 322 |
~DigraphExtender() {
|
| 323 | 323 |
arc_notifier.clear(); |
| 324 | 324 |
node_notifier.clear(); |
| 325 | 325 |
} |
| 326 | 326 |
}; |
| 327 | 327 |
|
| 328 |
/// \ingroup _graphbits |
|
| 329 |
/// |
|
| 330 |
// |
|
| 328 |
// \ingroup _graphbits |
|
| 329 |
// |
|
| 330 |
// \brief Extender for the Graphs |
|
| 331 | 331 |
template <typename Base> |
| 332 | 332 |
class GraphExtender : public Base {
|
| 333 | 333 |
public: |
| 334 | 334 |
|
| 335 | 335 |
typedef Base Parent; |
| 336 | 336 |
typedef GraphExtender Graph; |
| 337 | 337 |
|
| 338 | 338 |
typedef True UndirectedTag; |
| 339 | 339 |
|
| 340 | 340 |
typedef typename Parent::Node Node; |
| 341 | 341 |
typedef typename Parent::Arc Arc; |
| 342 | 342 |
typedef typename Parent::Edge Edge; |
| 343 | 343 |
|
| 344 | 344 |
// Graph extension |
| 345 | 345 |
|
| 346 | 346 |
int maxId(Node) const {
|
| 347 | 347 |
return Parent::maxNodeId(); |
| 348 | 348 |
} |
| 349 | 349 |
|
| 350 | 350 |
int maxId(Arc) const {
|
| 351 | 351 |
return Parent::maxArcId(); |
| 352 | 352 |
} |
| 353 | 353 |
|
| 354 | 354 |
int maxId(Edge) const {
|
| 355 | 355 |
return Parent::maxEdgeId(); |
| 356 | 356 |
} |
| 357 | 357 |
|
| 358 | 358 |
Node fromId(int id, Node) const {
|
| 359 | 359 |
return Parent::nodeFromId(id); |
| 360 | 360 |
} |
| 361 | 361 |
|
| 362 | 362 |
Arc fromId(int id, Arc) const {
|
| 363 | 363 |
return Parent::arcFromId(id); |
| 364 | 364 |
} |
| 365 | 365 |
|
| 366 | 366 |
Edge fromId(int id, Edge) const {
|
| 367 | 367 |
return Parent::edgeFromId(id); |
| 368 | 368 |
} |
| 369 | 369 |
|
| 370 | 370 |
Node oppositeNode(const Node &n, const Edge &e) const {
|
| 371 | 371 |
if( n == Parent::u(e)) |
| 372 | 372 |
return Parent::v(e); |
| 373 | 373 |
else if( n == Parent::v(e)) |
| 374 | 374 |
return Parent::u(e); |
| 375 | 375 |
else |
| 376 | 376 |
return INVALID; |
| 377 | 377 |
} |
| 378 | 378 |
|
| 379 | 379 |
Arc oppositeArc(const Arc &arc) const {
|
| 380 | 380 |
return Parent::direct(arc, !Parent::direction(arc)); |
| 381 | 381 |
} |
| 382 | 382 |
|
| 383 | 383 |
using Parent::direct; |
| 384 | 384 |
Arc direct(const Edge &edge, const Node &node) const {
|
| 385 | 385 |
return Parent::direct(edge, Parent::u(edge) == node); |
| 386 | 386 |
} |
| 387 | 387 |
|
| 388 | 388 |
// Alterable extension |
| 389 | 389 |
|
| 390 | 390 |
typedef AlterationNotifier<GraphExtender, Node> NodeNotifier; |
| 391 | 391 |
typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier; |
| 392 | 392 |
typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier; |
| 393 | 393 |
|
| 394 | 394 |
|
| 395 | 395 |
protected: |
| 396 | 396 |
|
| 397 | 397 |
mutable NodeNotifier node_notifier; |
| 398 | 398 |
mutable ArcNotifier arc_notifier; |
| 399 | 399 |
mutable EdgeNotifier edge_notifier; |
| 400 | 400 |
|
| 401 | 401 |
public: |
| 402 | 402 |
|
| 403 | 403 |
NodeNotifier& notifier(Node) const {
|
| 404 | 404 |
return node_notifier; |
| 405 | 405 |
} |
| 406 | 406 |
|
| 407 | 407 |
ArcNotifier& notifier(Arc) const {
|
| 408 | 408 |
return arc_notifier; |
| 409 | 409 |
} |
| 410 | 410 |
|
| 411 | 411 |
EdgeNotifier& notifier(Edge) const {
|
| 412 | 412 |
return edge_notifier; |
| 413 | 413 |
} |
| 414 | 414 |
|
| 415 | 415 |
|
| 416 | 416 |
|
| 417 | 417 |
class NodeIt : public Node {
|
| 418 | 418 |
const Graph* _graph; |
| 419 | 419 |
public: |
| 420 | 420 |
|
| 421 | 421 |
NodeIt() {}
|
| 422 | 422 |
|
| 423 | 423 |
NodeIt(Invalid i) : Node(i) { }
|
| 424 | 424 |
|
| 425 | 425 |
explicit NodeIt(const Graph& graph) : _graph(&graph) {
|
| 426 | 426 |
_graph->first(static_cast<Node&>(*this)); |
| ... | ... |
@@ -462,229 +462,229 @@ |
| 462 | 462 |
|
| 463 | 463 |
class OutArcIt : public Arc {
|
| 464 | 464 |
const Graph* _graph; |
| 465 | 465 |
public: |
| 466 | 466 |
|
| 467 | 467 |
OutArcIt() { }
|
| 468 | 468 |
|
| 469 | 469 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 470 | 470 |
|
| 471 | 471 |
OutArcIt(const Graph& graph, const Node& node) |
| 472 | 472 |
: _graph(&graph) {
|
| 473 | 473 |
_graph->firstOut(*this, node); |
| 474 | 474 |
} |
| 475 | 475 |
|
| 476 | 476 |
OutArcIt(const Graph& graph, const Arc& arc) |
| 477 | 477 |
: Arc(arc), _graph(&graph) {}
|
| 478 | 478 |
|
| 479 | 479 |
OutArcIt& operator++() {
|
| 480 | 480 |
_graph->nextOut(*this); |
| 481 | 481 |
return *this; |
| 482 | 482 |
} |
| 483 | 483 |
|
| 484 | 484 |
}; |
| 485 | 485 |
|
| 486 | 486 |
|
| 487 | 487 |
class InArcIt : public Arc {
|
| 488 | 488 |
const Graph* _graph; |
| 489 | 489 |
public: |
| 490 | 490 |
|
| 491 | 491 |
InArcIt() { }
|
| 492 | 492 |
|
| 493 | 493 |
InArcIt(Invalid i) : Arc(i) { }
|
| 494 | 494 |
|
| 495 | 495 |
InArcIt(const Graph& graph, const Node& node) |
| 496 | 496 |
: _graph(&graph) {
|
| 497 | 497 |
_graph->firstIn(*this, node); |
| 498 | 498 |
} |
| 499 | 499 |
|
| 500 | 500 |
InArcIt(const Graph& graph, const Arc& arc) : |
| 501 | 501 |
Arc(arc), _graph(&graph) {}
|
| 502 | 502 |
|
| 503 | 503 |
InArcIt& operator++() {
|
| 504 | 504 |
_graph->nextIn(*this); |
| 505 | 505 |
return *this; |
| 506 | 506 |
} |
| 507 | 507 |
|
| 508 | 508 |
}; |
| 509 | 509 |
|
| 510 | 510 |
|
| 511 | 511 |
class EdgeIt : public Parent::Edge {
|
| 512 | 512 |
const Graph* _graph; |
| 513 | 513 |
public: |
| 514 | 514 |
|
| 515 | 515 |
EdgeIt() { }
|
| 516 | 516 |
|
| 517 | 517 |
EdgeIt(Invalid i) : Edge(i) { }
|
| 518 | 518 |
|
| 519 | 519 |
explicit EdgeIt(const Graph& graph) : _graph(&graph) {
|
| 520 | 520 |
_graph->first(static_cast<Edge&>(*this)); |
| 521 | 521 |
} |
| 522 | 522 |
|
| 523 | 523 |
EdgeIt(const Graph& graph, const Edge& edge) : |
| 524 | 524 |
Edge(edge), _graph(&graph) { }
|
| 525 | 525 |
|
| 526 | 526 |
EdgeIt& operator++() {
|
| 527 | 527 |
_graph->next(*this); |
| 528 | 528 |
return *this; |
| 529 | 529 |
} |
| 530 | 530 |
|
| 531 | 531 |
}; |
| 532 | 532 |
|
| 533 | 533 |
class IncEdgeIt : public Parent::Edge {
|
| 534 | 534 |
friend class GraphExtender; |
| 535 | 535 |
const Graph* _graph; |
| 536 | 536 |
bool _direction; |
| 537 | 537 |
public: |
| 538 | 538 |
|
| 539 | 539 |
IncEdgeIt() { }
|
| 540 | 540 |
|
| 541 | 541 |
IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
|
| 542 | 542 |
|
| 543 | 543 |
IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
|
| 544 | 544 |
_graph->firstInc(*this, _direction, node); |
| 545 | 545 |
} |
| 546 | 546 |
|
| 547 | 547 |
IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node) |
| 548 | 548 |
: _graph(&graph), Edge(edge) {
|
| 549 | 549 |
_direction = (_graph->source(edge) == node); |
| 550 | 550 |
} |
| 551 | 551 |
|
| 552 | 552 |
IncEdgeIt& operator++() {
|
| 553 | 553 |
_graph->nextInc(*this, _direction); |
| 554 | 554 |
return *this; |
| 555 | 555 |
} |
| 556 | 556 |
}; |
| 557 | 557 |
|
| 558 |
/// \brief Base node of the iterator |
|
| 559 |
/// |
|
| 560 |
// |
|
| 558 |
// \brief Base node of the iterator |
|
| 559 |
// |
|
| 560 |
// Returns the base node (ie. the source in this case) of the iterator |
|
| 561 | 561 |
Node baseNode(const OutArcIt &arc) const {
|
| 562 | 562 |
return Parent::source(static_cast<const Arc&>(arc)); |
| 563 | 563 |
} |
| 564 |
/// \brief Running node of the iterator |
|
| 565 |
/// |
|
| 566 |
/// Returns the running node (ie. the target in this case) of the |
|
| 567 |
/// iterator |
|
| 564 |
// \brief Running node of the iterator |
|
| 565 |
// |
|
| 566 |
// Returns the running node (ie. the target in this case) of the |
|
| 567 |
// iterator |
|
| 568 | 568 |
Node runningNode(const OutArcIt &arc) const {
|
| 569 | 569 |
return Parent::target(static_cast<const Arc&>(arc)); |
| 570 | 570 |
} |
| 571 | 571 |
|
| 572 |
/// \brief Base node of the iterator |
|
| 573 |
/// |
|
| 574 |
// |
|
| 572 |
// \brief Base node of the iterator |
|
| 573 |
// |
|
| 574 |
// Returns the base node (ie. the target in this case) of the iterator |
|
| 575 | 575 |
Node baseNode(const InArcIt &arc) const {
|
| 576 | 576 |
return Parent::target(static_cast<const Arc&>(arc)); |
| 577 | 577 |
} |
| 578 |
/// \brief Running node of the iterator |
|
| 579 |
/// |
|
| 580 |
/// Returns the running node (ie. the source in this case) of the |
|
| 581 |
/// iterator |
|
| 578 |
// \brief Running node of the iterator |
|
| 579 |
// |
|
| 580 |
// Returns the running node (ie. the source in this case) of the |
|
| 581 |
// iterator |
|
| 582 | 582 |
Node runningNode(const InArcIt &arc) const {
|
| 583 | 583 |
return Parent::source(static_cast<const Arc&>(arc)); |
| 584 | 584 |
} |
| 585 | 585 |
|
| 586 |
/// Base node of the iterator |
|
| 587 |
/// |
|
| 588 |
// |
|
| 586 |
// Base node of the iterator |
|
| 587 |
// |
|
| 588 |
// Returns the base node of the iterator |
|
| 589 | 589 |
Node baseNode(const IncEdgeIt &edge) const {
|
| 590 | 590 |
return edge._direction ? u(edge) : v(edge); |
| 591 | 591 |
} |
| 592 |
/// Running node of the iterator |
|
| 593 |
/// |
|
| 594 |
// |
|
| 592 |
// Running node of the iterator |
|
| 593 |
// |
|
| 594 |
// Returns the running node of the iterator |
|
| 595 | 595 |
Node runningNode(const IncEdgeIt &edge) const {
|
| 596 | 596 |
return edge._direction ? v(edge) : u(edge); |
| 597 | 597 |
} |
| 598 | 598 |
|
| 599 | 599 |
// Mappable extension |
| 600 | 600 |
|
| 601 | 601 |
template <typename _Value> |
| 602 | 602 |
class NodeMap |
| 603 | 603 |
: public MapExtender<DefaultMap<Graph, Node, _Value> > {
|
| 604 | 604 |
public: |
| 605 | 605 |
typedef GraphExtender Graph; |
| 606 | 606 |
typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent; |
| 607 | 607 |
|
| 608 | 608 |
NodeMap(const Graph& graph) |
| 609 | 609 |
: Parent(graph) {}
|
| 610 | 610 |
NodeMap(const Graph& graph, const _Value& value) |
| 611 | 611 |
: Parent(graph, value) {}
|
| 612 | 612 |
|
| 613 | 613 |
private: |
| 614 | 614 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 615 | 615 |
return operator=<NodeMap>(cmap); |
| 616 | 616 |
} |
| 617 | 617 |
|
| 618 | 618 |
template <typename CMap> |
| 619 | 619 |
NodeMap& operator=(const CMap& cmap) {
|
| 620 | 620 |
Parent::operator=(cmap); |
| 621 | 621 |
return *this; |
| 622 | 622 |
} |
| 623 | 623 |
|
| 624 | 624 |
}; |
| 625 | 625 |
|
| 626 | 626 |
template <typename _Value> |
| 627 | 627 |
class ArcMap |
| 628 | 628 |
: public MapExtender<DefaultMap<Graph, Arc, _Value> > {
|
| 629 | 629 |
public: |
| 630 | 630 |
typedef GraphExtender Graph; |
| 631 | 631 |
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent; |
| 632 | 632 |
|
| 633 | 633 |
ArcMap(const Graph& graph) |
| 634 | 634 |
: Parent(graph) {}
|
| 635 | 635 |
ArcMap(const Graph& graph, const _Value& value) |
| 636 | 636 |
: Parent(graph, value) {}
|
| 637 | 637 |
|
| 638 | 638 |
private: |
| 639 | 639 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 640 | 640 |
return operator=<ArcMap>(cmap); |
| 641 | 641 |
} |
| 642 | 642 |
|
| 643 | 643 |
template <typename CMap> |
| 644 | 644 |
ArcMap& operator=(const CMap& cmap) {
|
| 645 | 645 |
Parent::operator=(cmap); |
| 646 | 646 |
return *this; |
| 647 | 647 |
} |
| 648 | 648 |
}; |
| 649 | 649 |
|
| 650 | 650 |
|
| 651 | 651 |
template <typename _Value> |
| 652 | 652 |
class EdgeMap |
| 653 | 653 |
: public MapExtender<DefaultMap<Graph, Edge, _Value> > {
|
| 654 | 654 |
public: |
| 655 | 655 |
typedef GraphExtender Graph; |
| 656 | 656 |
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
| 657 | 657 |
|
| 658 | 658 |
EdgeMap(const Graph& graph) |
| 659 | 659 |
: Parent(graph) {}
|
| 660 | 660 |
|
| 661 | 661 |
EdgeMap(const Graph& graph, const _Value& value) |
| 662 | 662 |
: Parent(graph, value) {}
|
| 663 | 663 |
|
| 664 | 664 |
private: |
| 665 | 665 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 666 | 666 |
return operator=<EdgeMap>(cmap); |
| 667 | 667 |
} |
| 668 | 668 |
|
| 669 | 669 |
template <typename CMap> |
| 670 | 670 |
EdgeMap& operator=(const CMap& cmap) {
|
| 671 | 671 |
Parent::operator=(cmap); |
| 672 | 672 |
return *this; |
| 673 | 673 |
} |
| 674 | 674 |
|
| 675 | 675 |
}; |
| 676 | 676 |
|
| 677 | 677 |
// Alteration extension |
| 678 | 678 |
|
| 679 | 679 |
Node addNode() {
|
| 680 | 680 |
Node node = Parent::addNode(); |
| 681 | 681 |
notifier(Node()).add(node); |
| 682 | 682 |
return node; |
| 683 | 683 |
} |
| 684 | 684 |
|
| 685 | 685 |
Edge addEdge(const Node& from, const Node& to) {
|
| 686 | 686 |
Edge edge = Parent::addEdge(from, to); |
| 687 | 687 |
notifier(Edge()).add(edge); |
| 688 | 688 |
std::vector<Arc> ev; |
| 689 | 689 |
ev.push_back(Parent::direct(edge, true)); |
| 690 | 690 |
ev.push_back(Parent::direct(edge, false)); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_MAP_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_MAP_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iterator> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/bits/traits.h> |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/concept_check.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
|
| 29 |
///\file |
|
| 30 |
///\brief Extenders for iterable maps. |
|
| 29 |
//\file |
|
| 30 |
//\brief Extenders for iterable maps. |
|
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 |
/// \ingroup graphbits |
|
| 35 |
/// |
|
| 36 |
// |
|
| 34 |
// \ingroup graphbits |
|
| 35 |
// |
|
| 36 |
// \brief Extender for maps |
|
| 37 | 37 |
template <typename _Map> |
| 38 | 38 |
class MapExtender : public _Map {
|
| 39 | 39 |
public: |
| 40 | 40 |
|
| 41 | 41 |
typedef _Map Parent; |
| 42 | 42 |
typedef MapExtender Map; |
| 43 | 43 |
|
| 44 | 44 |
|
| 45 | 45 |
typedef typename Parent::Graph Graph; |
| 46 | 46 |
typedef typename Parent::Key Item; |
| 47 | 47 |
|
| 48 | 48 |
typedef typename Parent::Key Key; |
| 49 | 49 |
typedef typename Parent::Value Value; |
| 50 | 50 |
|
| 51 | 51 |
class MapIt; |
| 52 | 52 |
class ConstMapIt; |
| 53 | 53 |
|
| 54 | 54 |
friend class MapIt; |
| 55 | 55 |
friend class ConstMapIt; |
| 56 | 56 |
|
| 57 | 57 |
public: |
| 58 | 58 |
|
| 59 | 59 |
MapExtender(const Graph& graph) |
| 60 | 60 |
: Parent(graph) {}
|
| 61 | 61 |
|
| 62 | 62 |
MapExtender(const Graph& graph, const Value& value) |
| 63 | 63 |
: Parent(graph, value) {}
|
| 64 | 64 |
|
| 65 | 65 |
private: |
| 66 | 66 |
MapExtender& operator=(const MapExtender& cmap) {
|
| 67 | 67 |
return operator=<MapExtender>(cmap); |
| 68 | 68 |
} |
| 69 | 69 |
|
| 70 | 70 |
template <typename CMap> |
| 71 | 71 |
MapExtender& operator=(const CMap& cmap) {
|
| 72 | 72 |
Parent::operator=(cmap); |
| 73 | 73 |
return *this; |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
public: |
| 77 | 77 |
class MapIt : public Item {
|
| 78 | 78 |
public: |
| 79 | 79 |
|
| 80 | 80 |
typedef Item Parent; |
| 81 | 81 |
typedef typename Map::Value Value; |
| 82 | 82 |
|
| 83 | 83 |
MapIt() {}
|
| 84 | 84 |
|
| 85 | 85 |
MapIt(Invalid i) : Parent(i) { }
|
| 86 | 86 |
|
| 87 | 87 |
explicit MapIt(Map& _map) : map(_map) {
|
| 88 | 88 |
map.notifier()->first(*this); |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 | 91 |
MapIt(const Map& _map, const Item& item) |
| 92 | 92 |
: Parent(item), map(_map) {}
|
| 93 | 93 |
|
| 94 | 94 |
MapIt& operator++() {
|
| 95 | 95 |
map.notifier()->next(*this); |
| 96 | 96 |
return *this; |
| 97 | 97 |
} |
| 98 | 98 |
|
| 99 | 99 |
typename MapTraits<Map>::ConstReturnValue operator*() const {
|
| 100 | 100 |
return map[*this]; |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
typename MapTraits<Map>::ReturnValue operator*() {
|
| 104 | 104 |
return map[*this]; |
| 105 | 105 |
} |
| 106 | 106 |
|
| 107 | 107 |
void set(const Value& value) {
|
| 108 | 108 |
map.set(*this, value); |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
protected: |
| 112 | 112 |
Map& map; |
| 113 | 113 |
|
| 114 | 114 |
}; |
| 115 | 115 |
|
| 116 | 116 |
class ConstMapIt : public Item {
|
| 117 | 117 |
public: |
| 118 | 118 |
|
| 119 | 119 |
typedef Item Parent; |
| 120 | 120 |
|
| 121 | 121 |
typedef typename Map::Value Value; |
| 122 | 122 |
|
| 123 | 123 |
ConstMapIt() {}
|
| 124 | 124 |
|
| 125 | 125 |
ConstMapIt(Invalid i) : Parent(i) { }
|
| 126 | 126 |
|
| 127 | 127 |
explicit ConstMapIt(Map& _map) : map(_map) {
|
| 128 | 128 |
map.notifier()->first(*this); |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
ConstMapIt(const Map& _map, const Item& item) |
| 132 | 132 |
: Parent(item), map(_map) {}
|
| 133 | 133 |
|
| 134 | 134 |
ConstMapIt& operator++() {
|
| 135 | 135 |
map.notifier()->next(*this); |
| 136 | 136 |
return *this; |
| 137 | 137 |
} |
| 138 | 138 |
|
| 139 | 139 |
typename MapTraits<Map>::ConstReturnValue operator*() const {
|
| 140 | 140 |
return map[*this]; |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
protected: |
| 144 | 144 |
const Map& map; |
| 145 | 145 |
}; |
| 146 | 146 |
|
| 147 | 147 |
class ItemIt : public Item {
|
| 148 | 148 |
public: |
| 149 | 149 |
|
| 150 | 150 |
typedef Item Parent; |
| 151 | 151 |
|
| 152 | 152 |
ItemIt() {}
|
| 153 | 153 |
|
| 154 | 154 |
ItemIt(Invalid i) : Parent(i) { }
|
| 155 | 155 |
|
| 156 | 156 |
explicit ItemIt(Map& _map) : map(_map) {
|
| 157 | 157 |
map.notifier()->first(*this); |
| 158 | 158 |
} |
| 159 | 159 |
|
| 160 | 160 |
ItemIt(const Map& _map, const Item& item) |
| 161 | 161 |
: Parent(item), map(_map) {}
|
| 162 | 162 |
|
| 163 | 163 |
ItemIt& operator++() {
|
| 164 | 164 |
map.notifier()->next(*this); |
| 165 | 165 |
return *this; |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 | 168 |
protected: |
| 169 | 169 |
const Map& map; |
| 170 | 170 |
|
| 171 | 171 |
}; |
| 172 | 172 |
}; |
| 173 | 173 |
|
| 174 |
/// \ingroup graphbits |
|
| 175 |
/// |
|
| 176 |
// |
|
| 174 |
// \ingroup graphbits |
|
| 175 |
// |
|
| 176 |
// \brief Extender for maps which use a subset of the items. |
|
| 177 | 177 |
template <typename _Graph, typename _Map> |
| 178 | 178 |
class SubMapExtender : public _Map {
|
| 179 | 179 |
public: |
| 180 | 180 |
|
| 181 | 181 |
typedef _Map Parent; |
| 182 | 182 |
typedef SubMapExtender Map; |
| 183 | 183 |
|
| 184 | 184 |
typedef _Graph Graph; |
| 185 | 185 |
|
| 186 | 186 |
typedef typename Parent::Key Item; |
| 187 | 187 |
|
| 188 | 188 |
typedef typename Parent::Key Key; |
| 189 | 189 |
typedef typename Parent::Value Value; |
| 190 | 190 |
|
| 191 | 191 |
class MapIt; |
| 192 | 192 |
class ConstMapIt; |
| 193 | 193 |
|
| 194 | 194 |
friend class MapIt; |
| 195 | 195 |
friend class ConstMapIt; |
| 196 | 196 |
|
| 197 | 197 |
public: |
| 198 | 198 |
|
| 199 | 199 |
SubMapExtender(const Graph& _graph) |
| 200 | 200 |
: Parent(_graph), graph(_graph) {}
|
| 201 | 201 |
|
| 202 | 202 |
SubMapExtender(const Graph& _graph, const Value& _value) |
| 203 | 203 |
: Parent(_graph, _value), graph(_graph) {}
|
| 204 | 204 |
|
| 205 | 205 |
private: |
| 206 | 206 |
SubMapExtender& operator=(const SubMapExtender& cmap) {
|
| 207 | 207 |
return operator=<MapExtender>(cmap); |
| 208 | 208 |
} |
| 209 | 209 |
|
| 210 | 210 |
template <typename CMap> |
| 211 | 211 |
SubMapExtender& operator=(const CMap& cmap) {
|
| 212 | 212 |
checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
| 213 | 213 |
Item it; |
| 214 | 214 |
for (graph.first(it); it != INVALID; graph.next(it)) {
|
| 215 | 215 |
Parent::set(it, cmap[it]); |
| 216 | 216 |
} |
| 217 | 217 |
return *this; |
| 218 | 218 |
} |
| 219 | 219 |
|
| 220 | 220 |
public: |
| 221 | 221 |
class MapIt : public Item {
|
| 222 | 222 |
public: |
| 223 | 223 |
|
| 224 | 224 |
typedef Item Parent; |
| 225 | 225 |
typedef typename Map::Value Value; |
| 226 | 226 |
|
| 227 | 227 |
MapIt() {}
|
| 228 | 228 |
|
| 229 | 229 |
MapIt(Invalid i) : Parent(i) { }
|
| 230 | 230 |
|
| 231 | 231 |
explicit MapIt(Map& _map) : map(_map) {
|
| 232 | 232 |
map.graph.first(*this); |
| 233 | 233 |
} |
| 234 | 234 |
|
| 235 | 235 |
MapIt(const Map& _map, const Item& item) |
| 236 | 236 |
: Parent(item), map(_map) {}
|
| 237 | 237 |
|
| 238 | 238 |
MapIt& operator++() {
|
| 239 | 239 |
map.graph.next(*this); |
| 240 | 240 |
return *this; |
| 241 | 241 |
} |
| 242 | 242 |
|
| 243 | 243 |
typename MapTraits<Map>::ConstReturnValue operator*() const {
|
| 244 | 244 |
return map[*this]; |
| 245 | 245 |
} |
| 246 | 246 |
|
| 247 | 247 |
typename MapTraits<Map>::ReturnValue operator*() {
|
| 248 | 248 |
return map[*this]; |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 | 251 |
void set(const Value& value) {
|
| 252 | 252 |
map.set(*this, value); |
| 253 | 253 |
} |
| 254 | 254 |
|
| 255 | 255 |
protected: |
| 256 | 256 |
Map& map; |
| 257 | 257 |
|
| 258 | 258 |
}; |
| 259 | 259 |
|
| 260 | 260 |
class ConstMapIt : public Item {
|
| 261 | 261 |
public: |
| 262 | 262 |
|
| 263 | 263 |
typedef Item Parent; |
| 264 | 264 |
|
| 265 | 265 |
typedef typename Map::Value Value; |
| 266 | 266 |
|
| 267 | 267 |
ConstMapIt() {}
|
| 268 | 268 |
|
| 269 | 269 |
ConstMapIt(Invalid i) : Parent(i) { }
|
| 270 | 270 |
|
| 271 | 271 |
explicit ConstMapIt(Map& _map) : map(_map) {
|
| 272 | 272 |
map.graph.first(*this); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_TRAITS_H |
| 20 | 20 |
#define LEMON_BITS_TRAITS_H |
| 21 | 21 |
|
| 22 |
///\file |
|
| 23 |
///\brief Traits for graphs and maps |
|
| 24 |
// |
|
| 22 |
//\file |
|
| 23 |
//\brief Traits for graphs and maps |
|
| 24 |
// |
|
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/bits/enable_if.h> |
| 27 | 27 |
|
| 28 | 28 |
namespace lemon {
|
| 29 | 29 |
|
| 30 | 30 |
struct InvalidType {};
|
| 31 | 31 |
|
| 32 | 32 |
template <typename _Graph, typename _Item> |
| 33 | 33 |
class ItemSetTraits {};
|
| 34 | 34 |
|
| 35 | 35 |
|
| 36 | 36 |
template <typename Graph, typename Enable = void> |
| 37 | 37 |
struct NodeNotifierIndicator {
|
| 38 | 38 |
typedef InvalidType Type; |
| 39 | 39 |
}; |
| 40 | 40 |
template <typename Graph> |
| 41 | 41 |
struct NodeNotifierIndicator< |
| 42 | 42 |
Graph, |
| 43 | 43 |
typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type |
| 44 | 44 |
> {
|
| 45 | 45 |
typedef typename Graph::NodeNotifier Type; |
| 46 | 46 |
}; |
| 47 | 47 |
|
| 48 | 48 |
template <typename _Graph> |
| 49 | 49 |
class ItemSetTraits<_Graph, typename _Graph::Node> {
|
| 50 | 50 |
public: |
| 51 | 51 |
|
| 52 | 52 |
typedef _Graph Graph; |
| 53 | 53 |
|
| 54 | 54 |
typedef typename Graph::Node Item; |
| 55 | 55 |
typedef typename Graph::NodeIt ItemIt; |
| 56 | 56 |
|
| 57 | 57 |
typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier; |
| 58 | 58 |
|
| 59 | 59 |
template <typename _Value> |
| 60 | 60 |
class Map : public Graph::template NodeMap<_Value> {
|
| 61 | 61 |
public: |
| 62 | 62 |
typedef typename Graph::template NodeMap<_Value> Parent; |
| 63 | 63 |
typedef typename Graph::template NodeMap<_Value> Type; |
| 64 | 64 |
typedef typename Parent::Value Value; |
| 65 | 65 |
|
| 66 | 66 |
Map(const Graph& _digraph) : Parent(_digraph) {}
|
| 67 | 67 |
Map(const Graph& _digraph, const Value& _value) |
| 68 | 68 |
: Parent(_digraph, _value) {}
|
| 69 | 69 |
|
| 70 | 70 |
}; |
| 71 | 71 |
|
| 72 | 72 |
}; |
| 73 | 73 |
|
| 74 | 74 |
template <typename Graph, typename Enable = void> |
| 75 | 75 |
struct ArcNotifierIndicator {
|
| 76 | 76 |
typedef InvalidType Type; |
| 77 | 77 |
}; |
| 78 | 78 |
template <typename Graph> |
| 79 | 79 |
struct ArcNotifierIndicator< |
| 80 | 80 |
Graph, |
| 81 | 81 |
typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type |
| 82 | 82 |
> {
|
| 83 | 83 |
typedef typename Graph::ArcNotifier Type; |
| 84 | 84 |
}; |
| 85 | 85 |
|
| 86 | 86 |
template <typename _Graph> |
| 87 | 87 |
class ItemSetTraits<_Graph, typename _Graph::Arc> {
|
| 88 | 88 |
public: |
| 89 | 89 |
|
| 90 | 90 |
typedef _Graph Graph; |
| 91 | 91 |
|
| 92 | 92 |
typedef typename Graph::Arc Item; |
| 93 | 93 |
typedef typename Graph::ArcIt ItemIt; |
| 94 | 94 |
|
| 95 | 95 |
typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier; |
| 96 | 96 |
|
| 97 | 97 |
template <typename _Value> |
| 98 | 98 |
class Map : public Graph::template ArcMap<_Value> {
|
| 99 | 99 |
public: |
| 100 | 100 |
typedef typename Graph::template ArcMap<_Value> Parent; |
| 101 | 101 |
typedef typename Graph::template ArcMap<_Value> Type; |
| 102 | 102 |
typedef typename Parent::Value Value; |
| 103 | 103 |
|
| 104 | 104 |
Map(const Graph& _digraph) : Parent(_digraph) {}
|
| 105 | 105 |
Map(const Graph& _digraph, const Value& _value) |
| 106 | 106 |
: Parent(_digraph, _value) {}
|
| 107 | 107 |
}; |
| 108 | 108 |
|
| 109 | 109 |
}; |
| 110 | 110 |
|
| 111 | 111 |
template <typename Graph, typename Enable = void> |
| 112 | 112 |
struct EdgeNotifierIndicator {
|
| 113 | 113 |
typedef InvalidType Type; |
| 114 | 114 |
}; |
| 115 | 115 |
template <typename Graph> |
| 116 | 116 |
struct EdgeNotifierIndicator< |
| 117 | 117 |
Graph, |
| 118 | 118 |
typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type |
| 119 | 119 |
> {
|
| 120 | 120 |
typedef typename Graph::EdgeNotifier Type; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_VECTOR_MAP_H |
| 20 | 20 |
#define LEMON_BITS_VECTOR_MAP_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <algorithm> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/core.h> |
| 26 | 26 |
#include <lemon/bits/alteration_notifier.h> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
#include <lemon/concepts/maps.h> |
| 30 | 30 |
|
| 31 |
///\ingroup graphbits |
|
| 32 |
/// |
|
| 33 |
///\file |
|
| 34 |
///\brief Vector based graph maps. |
|
| 31 |
//\ingroup graphbits |
|
| 32 |
// |
|
| 33 |
//\file |
|
| 34 |
//\brief Vector based graph maps. |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 |
/// \ingroup graphbits |
|
| 38 |
/// |
|
| 39 |
/// \brief Graph map based on the std::vector storage. |
|
| 40 |
/// |
|
| 41 |
/// The VectorMap template class is graph map structure what |
|
| 42 |
/// automatically updates the map when a key is added to or erased from |
|
| 43 |
/// the map. This map type uses the std::vector to store the values. |
|
| 44 |
/// |
|
| 45 |
/// \tparam _Graph The graph this map is attached to. |
|
| 46 |
/// \tparam _Item The item type of the graph items. |
|
| 47 |
// |
|
| 37 |
// \ingroup graphbits |
|
| 38 |
// |
|
| 39 |
// \brief Graph map based on the std::vector storage. |
|
| 40 |
// |
|
| 41 |
// The VectorMap template class is graph map structure what |
|
| 42 |
// automatically updates the map when a key is added to or erased from |
|
| 43 |
// the map. This map type uses the std::vector to store the values. |
|
| 44 |
// |
|
| 45 |
// \tparam _Graph The graph this map is attached to. |
|
| 46 |
// \tparam _Item The item type of the graph items. |
|
| 47 |
// \tparam _Value The value type of the map. |
|
| 48 | 48 |
template <typename _Graph, typename _Item, typename _Value> |
| 49 | 49 |
class VectorMap |
| 50 | 50 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
|
| 51 | 51 |
private: |
| 52 | 52 |
|
| 53 |
|
|
| 53 |
// The container type of the map. |
|
| 54 | 54 |
typedef std::vector<_Value> Container; |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 |
|
|
| 58 |
// The graph type of the map. |
|
| 59 | 59 |
typedef _Graph Graph; |
| 60 |
|
|
| 60 |
// The item type of the map. |
|
| 61 | 61 |
typedef _Item Item; |
| 62 |
|
|
| 62 |
// The reference map tag. |
|
| 63 | 63 |
typedef True ReferenceMapTag; |
| 64 | 64 |
|
| 65 |
|
|
| 65 |
// The key type of the map. |
|
| 66 | 66 |
typedef _Item Key; |
| 67 |
|
|
| 67 |
// The value type of the map. |
|
| 68 | 68 |
typedef _Value Value; |
| 69 | 69 |
|
| 70 |
|
|
| 70 |
// The notifier type. |
|
| 71 | 71 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
| 72 | 72 |
|
| 73 |
|
|
| 73 |
// The map type. |
|
| 74 | 74 |
typedef VectorMap Map; |
| 75 |
|
|
| 75 |
// The base class of the map. |
|
| 76 | 76 |
typedef typename Notifier::ObserverBase Parent; |
| 77 | 77 |
|
| 78 |
|
|
| 78 |
// The reference type of the map; |
|
| 79 | 79 |
typedef typename Container::reference Reference; |
| 80 |
|
|
| 80 |
// The const reference type of the map; |
|
| 81 | 81 |
typedef typename Container::const_reference ConstReference; |
| 82 | 82 |
|
| 83 | 83 |
|
| 84 |
/// \brief Constructor to attach the new map into the notifier. |
|
| 85 |
/// |
|
| 86 |
/// It constructs a map and attachs it into the notifier. |
|
| 87 |
/// It adds all the items of the graph to the map. |
|
| 84 |
// \brief Constructor to attach the new map into the notifier. |
|
| 85 |
// |
|
| 86 |
// It constructs a map and attachs it into the notifier. |
|
| 87 |
// It adds all the items of the graph to the map. |
|
| 88 | 88 |
VectorMap(const Graph& graph) {
|
| 89 | 89 |
Parent::attach(graph.notifier(Item())); |
| 90 | 90 |
container.resize(Parent::notifier()->maxId() + 1); |
| 91 | 91 |
} |
| 92 | 92 |
|
| 93 |
/// \brief Constructor uses given value to initialize the map. |
|
| 94 |
/// |
|
| 95 |
/// It constructs a map uses a given value to initialize the map. |
|
| 96 |
/// It adds all the items of the graph to the map. |
|
| 93 |
// \brief Constructor uses given value to initialize the map. |
|
| 94 |
// |
|
| 95 |
// It constructs a map uses a given value to initialize the map. |
|
| 96 |
// It adds all the items of the graph to the map. |
|
| 97 | 97 |
VectorMap(const Graph& graph, const Value& value) {
|
| 98 | 98 |
Parent::attach(graph.notifier(Item())); |
| 99 | 99 |
container.resize(Parent::notifier()->maxId() + 1, value); |
| 100 | 100 |
} |
| 101 | 101 |
|
| 102 | 102 |
private: |
| 103 |
/// \brief Copy constructor |
|
| 104 |
/// |
|
| 105 |
// |
|
| 103 |
// \brief Copy constructor |
|
| 104 |
// |
|
| 105 |
// Copy constructor. |
|
| 106 | 106 |
VectorMap(const VectorMap& _copy) : Parent() {
|
| 107 | 107 |
if (_copy.attached()) {
|
| 108 | 108 |
Parent::attach(*_copy.notifier()); |
| 109 | 109 |
container = _copy.container; |
| 110 | 110 |
} |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 |
/// \brief Assign operator. |
|
| 114 |
/// |
|
| 115 |
/// This operator assigns for each item in the map the |
|
| 116 |
/// value mapped to the same item in the copied map. |
|
| 117 |
/// The parameter map should be indiced with the same |
|
| 118 |
/// itemset because this assign operator does not change |
|
| 119 |
// |
|
| 113 |
// \brief Assign operator. |
|
| 114 |
// |
|
| 115 |
// This operator assigns for each item in the map the |
|
| 116 |
// value mapped to the same item in the copied map. |
|
| 117 |
// The parameter map should be indiced with the same |
|
| 118 |
// itemset because this assign operator does not change |
|
| 119 |
// the container of the map. |
|
| 120 | 120 |
VectorMap& operator=(const VectorMap& cmap) {
|
| 121 | 121 |
return operator=<VectorMap>(cmap); |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
|
| 125 |
/// \brief Template assign operator. |
|
| 126 |
/// |
|
| 127 |
/// The given parameter should be conform to the ReadMap |
|
| 128 |
/// concecpt and could be indiced by the current item set of |
|
| 129 |
/// the NodeMap. In this case the value for each item |
|
| 130 |
/// is assigned by the value of the given ReadMap. |
|
| 125 |
// \brief Template assign operator. |
|
| 126 |
// |
|
| 127 |
// The given parameter should be conform to the ReadMap |
|
| 128 |
// concecpt and could be indiced by the current item set of |
|
| 129 |
// the NodeMap. In this case the value for each item |
|
| 130 |
// is assigned by the value of the given ReadMap. |
|
| 131 | 131 |
template <typename CMap> |
| 132 | 132 |
VectorMap& operator=(const CMap& cmap) {
|
| 133 | 133 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
| 134 | 134 |
const typename Parent::Notifier* nf = Parent::notifier(); |
| 135 | 135 |
Item it; |
| 136 | 136 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 137 | 137 |
set(it, cmap[it]); |
| 138 | 138 |
} |
| 139 | 139 |
return *this; |
| 140 | 140 |
} |
| 141 | 141 |
|
| 142 | 142 |
public: |
| 143 | 143 |
|
| 144 |
/// \brief The subcript operator. |
|
| 145 |
/// |
|
| 146 |
/// The subscript operator. The map can be subscripted by the |
|
| 147 |
/// actual items of the graph. |
|
| 144 |
// \brief The subcript operator. |
|
| 145 |
// |
|
| 146 |
// The subscript operator. The map can be subscripted by the |
|
| 147 |
// actual items of the graph. |
|
| 148 | 148 |
Reference operator[](const Key& key) {
|
| 149 | 149 |
return container[Parent::notifier()->id(key)]; |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 |
/// \brief The const subcript operator. |
|
| 153 |
/// |
|
| 154 |
/// The const subscript operator. The map can be subscripted by the |
|
| 155 |
/// actual items of the graph. |
|
| 152 |
// \brief The const subcript operator. |
|
| 153 |
// |
|
| 154 |
// The const subscript operator. The map can be subscripted by the |
|
| 155 |
// actual items of the graph. |
|
| 156 | 156 |
ConstReference operator[](const Key& key) const {
|
| 157 | 157 |
return container[Parent::notifier()->id(key)]; |
| 158 | 158 |
} |
| 159 | 159 |
|
| 160 | 160 |
|
| 161 |
/// \brief The setter function of the map. |
|
| 162 |
/// |
|
| 163 |
// |
|
| 161 |
// \brief The setter function of the map. |
|
| 162 |
// |
|
| 163 |
// It the same as operator[](key) = value expression. |
|
| 164 | 164 |
void set(const Key& key, const Value& value) {
|
| 165 | 165 |
(*this)[key] = value; |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 | 168 |
protected: |
| 169 | 169 |
|
| 170 |
/// \brief Adds a new key to the map. |
|
| 171 |
/// |
|
| 172 |
/// It adds a new key to the map. It called by the observer notifier |
|
| 173 |
/// and it overrides the add() member function of the observer base. |
|
| 170 |
// \brief Adds a new key to the map. |
|
| 171 |
// |
|
| 172 |
// It adds a new key to the map. It called by the observer notifier |
|
| 173 |
// and it overrides the add() member function of the observer base. |
|
| 174 | 174 |
virtual void add(const Key& key) {
|
| 175 | 175 |
int id = Parent::notifier()->id(key); |
| 176 | 176 |
if (id >= int(container.size())) {
|
| 177 | 177 |
container.resize(id + 1); |
| 178 | 178 |
} |
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 |
/// \brief Adds more new keys to the map. |
|
| 182 |
/// |
|
| 183 |
/// It adds more new keys to the map. It called by the observer notifier |
|
| 184 |
/// and it overrides the add() member function of the observer base. |
|
| 181 |
// \brief Adds more new keys to the map. |
|
| 182 |
// |
|
| 183 |
// It adds more new keys to the map. It called by the observer notifier |
|
| 184 |
// and it overrides the add() member function of the observer base. |
|
| 185 | 185 |
virtual void add(const std::vector<Key>& keys) {
|
| 186 | 186 |
int max = container.size() - 1; |
| 187 | 187 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 188 | 188 |
int id = Parent::notifier()->id(keys[i]); |
| 189 | 189 |
if (id >= max) {
|
| 190 | 190 |
max = id; |
| 191 | 191 |
} |
| 192 | 192 |
} |
| 193 | 193 |
container.resize(max + 1); |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 |
/// \brief Erase a key from the map. |
|
| 197 |
/// |
|
| 198 |
/// Erase a key from the map. It called by the observer notifier |
|
| 199 |
/// and it overrides the erase() member function of the observer base. |
|
| 196 |
// \brief Erase a key from the map. |
|
| 197 |
// |
|
| 198 |
// Erase a key from the map. It called by the observer notifier |
|
| 199 |
// and it overrides the erase() member function of the observer base. |
|
| 200 | 200 |
virtual void erase(const Key& key) {
|
| 201 | 201 |
container[Parent::notifier()->id(key)] = Value(); |
| 202 | 202 |
} |
| 203 | 203 |
|
| 204 |
/// \brief Erase more keys from the map. |
|
| 205 |
/// |
|
| 206 |
/// Erase more keys from the map. It called by the observer notifier |
|
| 207 |
/// and it overrides the erase() member function of the observer base. |
|
| 204 |
// \brief Erase more keys from the map. |
|
| 205 |
// |
|
| 206 |
// Erase more keys from the map. It called by the observer notifier |
|
| 207 |
// and it overrides the erase() member function of the observer base. |
|
| 208 | 208 |
virtual void erase(const std::vector<Key>& keys) {
|
| 209 | 209 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 210 | 210 |
container[Parent::notifier()->id(keys[i])] = Value(); |
| 211 | 211 |
} |
| 212 | 212 |
} |
| 213 | 213 |
|
| 214 |
/// \brief Buildes the map. |
|
| 215 |
/// |
|
| 216 |
/// It buildes the map. It called by the observer notifier |
|
| 217 |
/// and it overrides the build() member function of the observer base. |
|
| 214 |
// \brief Buildes the map. |
|
| 215 |
// |
|
| 216 |
// It buildes the map. It called by the observer notifier |
|
| 217 |
// and it overrides the build() member function of the observer base. |
|
| 218 | 218 |
virtual void build() {
|
| 219 | 219 |
int size = Parent::notifier()->maxId() + 1; |
| 220 | 220 |
container.reserve(size); |
| 221 | 221 |
container.resize(size); |
| 222 | 222 |
} |
| 223 | 223 |
|
| 224 |
/// \brief Clear the map. |
|
| 225 |
/// |
|
| 226 |
/// It erase all items from the map. It called by the observer notifier |
|
| 227 |
/// and it overrides the clear() member function of the observer base. |
|
| 224 |
// \brief Clear the map. |
|
| 225 |
// |
|
| 226 |
// It erase all items from the map. It called by the observer notifier |
|
| 227 |
// and it overrides the clear() member function of the observer base. |
|
| 228 | 228 |
virtual void clear() {
|
| 229 | 229 |
container.clear(); |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
private: |
| 233 | 233 |
|
| 234 | 234 |
Container container; |
| 235 | 235 |
|
| 236 | 236 |
}; |
| 237 | 237 |
|
| 238 | 238 |
} |
| 239 | 239 |
|
| 240 | 240 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 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); |
| ... | ... |
@@ -889,193 +889,193 @@ |
| 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) {}
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CONCEPT_MAPS_H |
| 20 | 20 |
#define LEMON_CONCEPT_MAPS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/concept_check.h> |
| 24 | 24 |
|
| 25 |
///\ingroup |
|
| 25 |
///\ingroup map_concepts |
|
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief The concept of maps. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
namespace concepts {
|
| 32 | 32 |
|
| 33 |
/// \addtogroup |
|
| 33 |
/// \addtogroup map_concepts |
|
| 34 | 34 |
/// @{
|
| 35 | 35 |
|
| 36 | 36 |
/// Readable map concept |
| 37 | 37 |
|
| 38 | 38 |
/// Readable map concept. |
| 39 | 39 |
/// |
| 40 | 40 |
template<typename K, typename T> |
| 41 | 41 |
class ReadMap |
| 42 | 42 |
{
|
| 43 | 43 |
public: |
| 44 | 44 |
/// The key type of the map. |
| 45 | 45 |
typedef K Key; |
| 46 | 46 |
/// \brief The value type of the map. |
| 47 | 47 |
/// (The type of objects associated with the keys). |
| 48 | 48 |
typedef T Value; |
| 49 | 49 |
|
| 50 | 50 |
/// Returns the value associated with the given key. |
| 51 | 51 |
Value operator[](const Key &) const {
|
| 52 | 52 |
return *static_cast<Value *>(0); |
| 53 | 53 |
} |
| 54 | 54 |
|
| 55 | 55 |
template<typename _ReadMap> |
| 56 | 56 |
struct Constraints {
|
| 57 | 57 |
void constraints() {
|
| 58 | 58 |
Value val = m[key]; |
| 59 | 59 |
val = m[key]; |
| 60 | 60 |
typename _ReadMap::Value own_val = m[own_key]; |
| 61 | 61 |
own_val = m[own_key]; |
| 62 | 62 |
|
| 63 | 63 |
ignore_unused_variable_warning(key); |
| 64 | 64 |
ignore_unused_variable_warning(val); |
| 65 | 65 |
ignore_unused_variable_warning(own_key); |
| 66 | 66 |
ignore_unused_variable_warning(own_val); |
| 67 | 67 |
} |
| 68 | 68 |
const Key& key; |
| 69 | 69 |
const typename _ReadMap::Key& own_key; |
| 70 | 70 |
const _ReadMap& m; |
| 71 | 71 |
}; |
| 72 | 72 |
|
| 73 | 73 |
}; |
| 74 | 74 |
|
| 75 | 75 |
|
| 76 | 76 |
/// Writable map concept |
| 77 | 77 |
|
| 78 | 78 |
/// Writable map concept. |
| 79 | 79 |
/// |
| 80 | 80 |
template<typename K, typename T> |
| 81 | 81 |
class WriteMap |
| 82 | 82 |
{
|
| 83 | 83 |
public: |
| 84 | 84 |
/// The key type of the map. |
| 85 | 85 |
typedef K Key; |
| 86 | 86 |
/// \brief The value type of the map. |
| 87 | 87 |
/// (The type of objects associated with the keys). |
| 88 | 88 |
typedef T Value; |
| 89 | 89 |
|
| 90 | 90 |
/// Sets the value associated with the given key. |
| 91 | 91 |
void set(const Key &, const Value &) {}
|
| 92 | 92 |
|
| 93 | 93 |
/// Default constructor. |
| 94 | 94 |
WriteMap() {}
|
| 95 | 95 |
|
| 96 | 96 |
template <typename _WriteMap> |
| 97 | 97 |
struct Constraints {
|
| 98 | 98 |
void constraints() {
|
| 99 | 99 |
m.set(key, val); |
| 100 | 100 |
m.set(own_key, own_val); |
| 101 | 101 |
|
| 102 | 102 |
ignore_unused_variable_warning(key); |
| 103 | 103 |
ignore_unused_variable_warning(val); |
| 104 | 104 |
ignore_unused_variable_warning(own_key); |
| 105 | 105 |
ignore_unused_variable_warning(own_val); |
| 106 | 106 |
} |
| 107 | 107 |
const Key& key; |
| 108 | 108 |
const Value& val; |
| 109 | 109 |
const typename _WriteMap::Key& own_key; |
| 110 | 110 |
const typename _WriteMap::Value& own_val; |
| 111 | 111 |
_WriteMap& m; |
| 112 | 112 |
}; |
| 113 | 113 |
}; |
| 114 | 114 |
|
| 115 | 115 |
/// Read/writable map concept |
| 116 | 116 |
|
| 117 | 117 |
/// Read/writable map concept. |
| 118 | 118 |
/// |
| 119 | 119 |
template<typename K, typename T> |
| 120 | 120 |
class ReadWriteMap : public ReadMap<K,T>, |
| 121 | 121 |
public WriteMap<K,T> |
| 122 | 122 |
{
|
| 123 | 123 |
public: |
| 124 | 124 |
/// The key type of the map. |
| 125 | 125 |
typedef K Key; |
| 126 | 126 |
/// \brief The value type of the map. |
| 127 | 127 |
/// (The type of objects associated with the keys). |
| 128 | 128 |
typedef T Value; |
| 129 | 129 |
| 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_CORE_H |
| 20 | 20 |
#define LEMON_CORE_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <algorithm> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/bits/enable_if.h> |
| 26 | 26 |
#include <lemon/bits/traits.h> |
| 27 | 27 |
|
| 28 | 28 |
///\file |
| 29 | 29 |
///\brief LEMON core utilities. |
| 30 | 30 |
/// |
| 31 | 31 |
///This header file contains core utilities for LEMON. |
| 32 | 32 |
///It is automatically included by all graph types, therefore it usually |
| 33 | 33 |
///do not have to be included directly. |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
/// \brief Dummy type to make it easier to create invalid iterators. |
| 38 | 38 |
/// |
| 39 | 39 |
/// Dummy type to make it easier to create invalid iterators. |
| 40 | 40 |
/// See \ref INVALID for the usage. |
| 41 | 41 |
struct Invalid {
|
| 42 | 42 |
public: |
| 43 | 43 |
bool operator==(Invalid) { return true; }
|
| 44 | 44 |
bool operator!=(Invalid) { return false; }
|
| 45 | 45 |
bool operator< (Invalid) { return false; }
|
| 46 | 46 |
}; |
| 47 | 47 |
|
| 48 | 48 |
/// \brief Invalid iterators. |
| 49 | 49 |
/// |
| 50 | 50 |
/// \ref Invalid is a global type that converts to each iterator |
| 51 | 51 |
/// in such a way that the value of the target iterator will be invalid. |
| 52 | 52 |
#ifdef LEMON_ONLY_TEMPLATES |
| 53 | 53 |
const Invalid INVALID = Invalid(); |
| 54 | 54 |
#else |
| 55 | 55 |
extern const Invalid INVALID; |
| 56 | 56 |
#endif |
| 57 | 57 |
|
| 58 | 58 |
/// \addtogroup gutils |
| 59 | 59 |
/// @{
|
| 60 | 60 |
|
| 61 |
///Create |
|
| 61 |
///Create convenience typedefs for the digraph types and iterators |
|
| 62 | 62 |
|
| 63 | 63 |
///This \c \#define creates convenient type definitions for the following |
| 64 | 64 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
| 65 | 65 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
| 66 | 66 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
| 67 | 67 |
/// |
| 68 | 68 |
///\note If the graph type is a dependent type, ie. the graph type depend |
| 69 | 69 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
| 70 | 70 |
///macro. |
| 71 | 71 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
| 72 | 72 |
typedef Digraph::Node Node; \ |
| 73 | 73 |
typedef Digraph::NodeIt NodeIt; \ |
| 74 | 74 |
typedef Digraph::Arc Arc; \ |
| 75 | 75 |
typedef Digraph::ArcIt ArcIt; \ |
| 76 | 76 |
typedef Digraph::InArcIt InArcIt; \ |
| 77 | 77 |
typedef Digraph::OutArcIt OutArcIt; \ |
| 78 | 78 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
| 79 | 79 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
| 80 | 80 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
| 81 | 81 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
| 82 | 82 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
| 83 |
typedef Digraph::ArcMap<double> DoubleArcMap |
|
| 83 |
typedef Digraph::ArcMap<double> DoubleArcMap |
|
| 84 | 84 |
|
| 85 |
///Create |
|
| 85 |
///Create convenience typedefs for the digraph types and iterators |
|
| 86 | 86 |
|
| 87 | 87 |
///\see DIGRAPH_TYPEDEFS |
| 88 | 88 |
/// |
| 89 | 89 |
///\note Use this macro, if the graph type is a dependent type, |
| 90 | 90 |
///ie. the graph type depend on a template parameter. |
| 91 | 91 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
| 92 | 92 |
typedef typename Digraph::Node Node; \ |
| 93 | 93 |
typedef typename Digraph::NodeIt NodeIt; \ |
| 94 | 94 |
typedef typename Digraph::Arc Arc; \ |
| 95 | 95 |
typedef typename Digraph::ArcIt ArcIt; \ |
| 96 | 96 |
typedef typename Digraph::InArcIt InArcIt; \ |
| 97 | 97 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
| 98 | 98 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
| 99 | 99 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
| 100 | 100 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
| 101 | 101 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
| 102 | 102 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
| 103 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
|
| 103 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
|
| 104 | 104 |
|
| 105 |
///Create |
|
| 105 |
///Create convenience typedefs for the graph types and iterators |
|
| 106 | 106 |
|
| 107 | 107 |
///This \c \#define creates the same convenient type definitions as defined |
| 108 | 108 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
| 109 | 109 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
| 110 | 110 |
///\c DoubleEdgeMap. |
| 111 | 111 |
/// |
| 112 | 112 |
///\note If the graph type is a dependent type, ie. the graph type depend |
| 113 | 113 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
| 114 | 114 |
///macro. |
| 115 | 115 |
#define GRAPH_TYPEDEFS(Graph) \ |
| 116 | 116 |
DIGRAPH_TYPEDEFS(Graph); \ |
| 117 | 117 |
typedef Graph::Edge Edge; \ |
| 118 | 118 |
typedef Graph::EdgeIt EdgeIt; \ |
| 119 | 119 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
| 120 | 120 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
| 121 | 121 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
| 122 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
|
| 122 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
|
| 123 | 123 |
|
| 124 |
///Create |
|
| 124 |
///Create convenience typedefs for the graph types and iterators |
|
| 125 | 125 |
|
| 126 | 126 |
///\see GRAPH_TYPEDEFS |
| 127 | 127 |
/// |
| 128 | 128 |
///\note Use this macro, if the graph type is a dependent type, |
| 129 | 129 |
///ie. the graph type depend on a template parameter. |
| 130 | 130 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
| 131 | 131 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
| 132 | 132 |
typedef typename Graph::Edge Edge; \ |
| 133 | 133 |
typedef typename Graph::EdgeIt EdgeIt; \ |
| 134 | 134 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
| 135 | 135 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
| 136 | 136 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
| 137 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
|
| 137 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
|
| 138 | 138 |
|
| 139 | 139 |
/// \brief Function to count the items in a graph. |
| 140 | 140 |
/// |
| 141 | 141 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
| 142 | 142 |
/// The complexity of the function is linear because |
| 143 | 143 |
/// it iterates on all of the items. |
| 144 | 144 |
template <typename Graph, typename Item> |
| 145 | 145 |
inline int countItems(const Graph& g) {
|
| 146 | 146 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
| 147 | 147 |
int num = 0; |
| 148 | 148 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 149 | 149 |
++num; |
| 150 | 150 |
} |
| 151 | 151 |
return num; |
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
// Node counting: |
| 155 | 155 |
|
| 156 | 156 |
namespace _core_bits {
|
| 157 | 157 |
|
| 158 | 158 |
template <typename Graph, typename Enable = void> |
| 159 | 159 |
struct CountNodesSelector {
|
| 160 | 160 |
static int count(const Graph &g) {
|
| 161 | 161 |
return countItems<Graph, typename Graph::Node>(g); |
| 162 | 162 |
} |
| 163 | 163 |
}; |
| 164 | 164 |
|
| 165 | 165 |
template <typename Graph> |
| 166 | 166 |
struct CountNodesSelector< |
| 167 | 167 |
Graph, typename |
| 168 | 168 |
enable_if<typename Graph::NodeNumTag, void>::type> |
| 169 | 169 |
{
|
| 170 | 170 |
static int count(const Graph &g) {
|
| 171 | 171 |
return g.nodeNum(); |
| 172 | 172 |
} |
| 173 | 173 |
}; |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
/// \brief Function to count the nodes in the graph. |
| 177 | 177 |
/// |
| 178 | 178 |
/// This function counts the nodes in the graph. |
| 179 | 179 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
| 180 | 180 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 181 | 181 |
/// |
| 182 | 182 |
/// \note If the graph contains a \c nodeNum() member function and a |
| 183 | 183 |
/// \c NodeNumTag tag then this function calls directly the member |
| 184 | 184 |
/// function to query the cardinality of the node set. |
| 185 | 185 |
template <typename Graph> |
| 186 | 186 |
inline int countNodes(const Graph& g) {
|
| 187 | 187 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
| 188 | 188 |
} |
| 189 | 189 |
|
| 190 | 190 |
// Arc counting: |
| 191 | 191 |
|
| 192 | 192 |
namespace _core_bits {
|
| 193 | 193 |
|
| 194 | 194 |
template <typename Graph, typename Enable = void> |
| 195 | 195 |
struct CountArcsSelector {
|
| 196 | 196 |
static int count(const Graph &g) {
|
| 197 | 197 |
return countItems<Graph, typename Graph::Arc>(g); |
| 198 | 198 |
} |
| 199 | 199 |
}; |
| 200 | 200 |
|
| 201 | 201 |
template <typename Graph> |
| 202 | 202 |
struct CountArcsSelector< |
| 203 | 203 |
Graph, |
| 204 | 204 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
| 205 | 205 |
{
|
| 206 | 206 |
static int count(const Graph &g) {
|
| 207 | 207 |
return g.arcNum(); |
| 208 | 208 |
} |
| 209 | 209 |
}; |
| 210 | 210 |
} |
| 211 | 211 |
|
| 212 | 212 |
/// \brief Function to count the arcs in the graph. |
| 213 | 213 |
/// |
| 214 | 214 |
/// This function counts the arcs in the graph. |
| 215 | 215 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
| 216 | 216 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 217 | 217 |
/// |
| 218 | 218 |
/// \note If the graph contains a \c arcNum() member function and a |
| 219 | 219 |
/// \c ArcNumTag tag then this function calls directly the member |
| 220 | 220 |
/// function to query the cardinality of the arc set. |
| 221 | 221 |
template <typename Graph> |
| 222 | 222 |
inline int countArcs(const Graph& g) {
|
| 223 | 223 |
return _core_bits::CountArcsSelector<Graph>::count(g); |
| 224 | 224 |
} |
| 225 | 225 |
|
| 226 | 226 |
// Edge counting: |
| 227 | 227 |
|
| 228 | 228 |
namespace _core_bits {
|
| 229 | 229 |
|
| 230 | 230 |
template <typename Graph, typename Enable = void> |
| 231 | 231 |
struct CountEdgesSelector {
|
| 232 | 232 |
static int count(const Graph &g) {
|
| 233 | 233 |
return countItems<Graph, typename Graph::Edge>(g); |
| ... | ... |
@@ -1461,384 +1461,384 @@ |
| 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 |
| 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_DFS_H |
| 20 | 20 |
#define LEMON_DFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief DFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/assert.h> |
| 31 | 31 |
#include <lemon/maps.h> |
| 32 | 32 |
#include <lemon/path.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
///Default traits class of Dfs class. |
| 37 | 37 |
|
| 38 | 38 |
///Default traits class of Dfs class. |
| 39 | 39 |
///\tparam GR Digraph type. |
| 40 | 40 |
template<class GR> |
| 41 | 41 |
struct DfsDefaultTraits |
| 42 | 42 |
{
|
| 43 | 43 |
///The type of the digraph the algorithm runs on. |
| 44 | 44 |
typedef GR Digraph; |
| 45 | 45 |
|
| 46 | 46 |
///\brief The type of the map that stores the predecessor |
| 47 | 47 |
///arcs of the %DFS paths. |
| 48 | 48 |
/// |
| 49 | 49 |
///The type of the map that stores the predecessor |
| 50 | 50 |
///arcs of the %DFS paths. |
| 51 | 51 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 52 | 52 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 53 |
///Instantiates a |
|
| 53 |
///Instantiates a PredMap. |
|
| 54 | 54 |
|
| 55 |
///This function instantiates a |
|
| 55 |
///This function instantiates a PredMap. |
|
| 56 | 56 |
///\param g is the digraph, to which we would like to define the |
| 57 |
/// |
|
| 57 |
///PredMap. |
|
| 58 | 58 |
static PredMap *createPredMap(const Digraph &g) |
| 59 | 59 |
{
|
| 60 | 60 |
return new PredMap(g); |
| 61 | 61 |
} |
| 62 | 62 |
|
| 63 | 63 |
///The type of the map that indicates which nodes are processed. |
| 64 | 64 |
|
| 65 | 65 |
///The type of the map that indicates which nodes are processed. |
| 66 | 66 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 68 |
///Instantiates a |
|
| 68 |
///Instantiates a ProcessedMap. |
|
| 69 | 69 |
|
| 70 |
///This function instantiates a |
|
| 70 |
///This function instantiates a ProcessedMap. |
|
| 71 | 71 |
///\param g is the digraph, to which |
| 72 |
///we would like to define the |
|
| 72 |
///we would like to define the ProcessedMap |
|
| 73 | 73 |
#ifdef DOXYGEN |
| 74 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 75 | 75 |
#else |
| 76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 77 | 77 |
#endif |
| 78 | 78 |
{
|
| 79 | 79 |
return new ProcessedMap(); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
///The type of the map that indicates which nodes are reached. |
| 83 | 83 |
|
| 84 | 84 |
///The type of the map that indicates which nodes are reached. |
| 85 | 85 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 87 |
///Instantiates a |
|
| 87 |
///Instantiates a ReachedMap. |
|
| 88 | 88 |
|
| 89 |
///This function instantiates a |
|
| 89 |
///This function instantiates a ReachedMap. |
|
| 90 | 90 |
///\param g is the digraph, to which |
| 91 |
///we would like to define the |
|
| 91 |
///we would like to define the ReachedMap. |
|
| 92 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 93 | 93 |
{
|
| 94 | 94 |
return new ReachedMap(g); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
///The type of the map that stores the distances of the nodes. |
| 98 | 98 |
|
| 99 | 99 |
///The type of the map that stores the distances of the nodes. |
| 100 | 100 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 102 |
///Instantiates a |
|
| 102 |
///Instantiates a DistMap. |
|
| 103 | 103 |
|
| 104 |
///This function instantiates a |
|
| 104 |
///This function instantiates a DistMap. |
|
| 105 | 105 |
///\param g is the digraph, to which we would like to define the |
| 106 |
/// |
|
| 106 |
///DistMap. |
|
| 107 | 107 |
static DistMap *createDistMap(const Digraph &g) |
| 108 | 108 |
{
|
| 109 | 109 |
return new DistMap(g); |
| 110 | 110 |
} |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
///%DFS algorithm class. |
| 114 | 114 |
|
| 115 | 115 |
///\ingroup search |
| 116 | 116 |
///This class provides an efficient implementation of the %DFS algorithm. |
| 117 | 117 |
/// |
| 118 | 118 |
///There is also a \ref dfs() "function-type interface" for the DFS |
| 119 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
| 120 | 120 |
///used easier. |
| 121 | 121 |
/// |
| 122 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 123 | 123 |
///The default value is \ref ListDigraph. The value of GR is not used |
| 124 | 124 |
///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. |
| 125 | 125 |
///\tparam TR Traits class to set various data types used by the algorithm. |
| 126 | 126 |
///The default traits class is |
| 127 | 127 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
| 128 | 128 |
///See \ref DfsDefaultTraits for the documentation of |
| 129 | 129 |
///a Dfs traits class. |
| 130 | 130 |
#ifdef DOXYGEN |
| 131 | 131 |
template <typename GR, |
| 132 | 132 |
typename TR> |
| 133 | 133 |
#else |
| 134 | 134 |
template <typename GR=ListDigraph, |
| 135 | 135 |
typename TR=DfsDefaultTraits<GR> > |
| 136 | 136 |
#endif |
| 137 | 137 |
class Dfs {
|
| 138 | 138 |
public: |
| 139 | 139 |
|
| 140 | 140 |
///The type of the digraph the algorithm runs on. |
| 141 | 141 |
typedef typename TR::Digraph Digraph; |
| 142 | 142 |
|
| 143 | 143 |
///\brief The type of the map that stores the predecessor arcs of the |
| 144 | 144 |
///DFS paths. |
| 145 | 145 |
typedef typename TR::PredMap PredMap; |
| 146 | 146 |
///The type of the map that stores the distances of the nodes. |
| 147 | 147 |
typedef typename TR::DistMap DistMap; |
| 148 | 148 |
///The type of the map that indicates which nodes are reached. |
| 149 | 149 |
typedef typename TR::ReachedMap ReachedMap; |
| 150 | 150 |
///The type of the map that indicates which nodes are processed. |
| 151 | 151 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 152 | 152 |
///The type of the paths. |
| 153 | 153 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 154 | 154 |
|
| 155 | 155 |
///The traits class. |
| 156 | 156 |
typedef TR Traits; |
| 157 | 157 |
|
| 158 | 158 |
private: |
| 159 | 159 |
|
| 160 | 160 |
typedef typename Digraph::Node Node; |
| 161 | 161 |
typedef typename Digraph::NodeIt NodeIt; |
| 162 | 162 |
typedef typename Digraph::Arc Arc; |
| 163 | 163 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 164 | 164 |
|
| 165 | 165 |
//Pointer to the underlying digraph. |
| 166 | 166 |
const Digraph *G; |
| 167 | 167 |
//Pointer to the map of predecessor arcs. |
| 168 | 168 |
PredMap *_pred; |
| 169 | 169 |
//Indicates if _pred is locally allocated (true) or not. |
| 170 | 170 |
bool local_pred; |
| 171 | 171 |
//Pointer to the map of distances. |
| 172 | 172 |
DistMap *_dist; |
| 173 | 173 |
//Indicates if _dist is locally allocated (true) or not. |
| 174 | 174 |
bool local_dist; |
| 175 | 175 |
//Pointer to the map of reached status of the nodes. |
| 176 | 176 |
ReachedMap *_reached; |
| 177 | 177 |
//Indicates if _reached is locally allocated (true) or not. |
| 178 | 178 |
bool local_reached; |
| 179 | 179 |
//Pointer to the map of processed status of the nodes. |
| 180 | 180 |
ProcessedMap *_processed; |
| 181 | 181 |
//Indicates if _processed is locally allocated (true) or not. |
| 182 | 182 |
bool local_processed; |
| 183 | 183 |
|
| 184 | 184 |
std::vector<typename Digraph::OutArcIt> _stack; |
| 185 | 185 |
int _stack_head; |
| 186 | 186 |
|
| 187 | 187 |
//Creates the maps if necessary. |
| 188 | 188 |
void create_maps() |
| 189 | 189 |
{
|
| 190 | 190 |
if(!_pred) {
|
| 191 | 191 |
local_pred = true; |
| 192 | 192 |
_pred = Traits::createPredMap(*G); |
| 193 | 193 |
} |
| 194 | 194 |
if(!_dist) {
|
| 195 | 195 |
local_dist = true; |
| 196 | 196 |
_dist = Traits::createDistMap(*G); |
| 197 | 197 |
} |
| 198 | 198 |
if(!_reached) {
|
| 199 | 199 |
local_reached = true; |
| 200 | 200 |
_reached = Traits::createReachedMap(*G); |
| 201 | 201 |
} |
| 202 | 202 |
if(!_processed) {
|
| 203 | 203 |
local_processed = true; |
| 204 | 204 |
_processed = Traits::createProcessedMap(*G); |
| 205 | 205 |
} |
| 206 | 206 |
} |
| 207 | 207 |
|
| 208 | 208 |
protected: |
| 209 | 209 |
|
| 210 | 210 |
Dfs() {}
|
| 211 | 211 |
|
| 212 | 212 |
public: |
| 213 | 213 |
|
| 214 | 214 |
typedef Dfs Create; |
| 215 | 215 |
|
| 216 | 216 |
///\name Named template parameters |
| 217 | 217 |
|
| 218 | 218 |
///@{
|
| 219 | 219 |
|
| 220 | 220 |
template <class T> |
| 221 | 221 |
struct SetPredMapTraits : public Traits {
|
| 222 | 222 |
typedef T PredMap; |
| 223 | 223 |
static PredMap *createPredMap(const Digraph &) |
| 224 | 224 |
{
|
| 225 | 225 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 226 | 226 |
return 0; // ignore warnings |
| 227 | 227 |
} |
| 228 | 228 |
}; |
| 229 | 229 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 230 |
/// |
|
| 230 |
///PredMap type. |
|
| 231 | 231 |
/// |
| 232 | 232 |
///\ref named-templ-param "Named parameter" for setting |
| 233 |
/// |
|
| 233 |
///PredMap type. |
|
| 234 | 234 |
template <class T> |
| 235 | 235 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
|
| 236 | 236 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
| 237 | 237 |
}; |
| 238 | 238 |
|
| 239 | 239 |
template <class T> |
| 240 | 240 |
struct SetDistMapTraits : public Traits {
|
| 241 | 241 |
typedef T DistMap; |
| 242 | 242 |
static DistMap *createDistMap(const Digraph &) |
| 243 | 243 |
{
|
| 244 | 244 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 245 | 245 |
return 0; // ignore warnings |
| 246 | 246 |
} |
| 247 | 247 |
}; |
| 248 | 248 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 249 |
/// |
|
| 249 |
///DistMap type. |
|
| 250 | 250 |
/// |
| 251 | 251 |
///\ref named-templ-param "Named parameter" for setting |
| 252 |
/// |
|
| 252 |
///DistMap type. |
|
| 253 | 253 |
template <class T> |
| 254 | 254 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
|
| 255 | 255 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
| 256 | 256 |
}; |
| 257 | 257 |
|
| 258 | 258 |
template <class T> |
| 259 | 259 |
struct SetReachedMapTraits : public Traits {
|
| 260 | 260 |
typedef T ReachedMap; |
| 261 | 261 |
static ReachedMap *createReachedMap(const Digraph &) |
| 262 | 262 |
{
|
| 263 | 263 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 264 | 264 |
return 0; // ignore warnings |
| 265 | 265 |
} |
| 266 | 266 |
}; |
| 267 | 267 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 268 |
/// |
|
| 268 |
///ReachedMap type. |
|
| 269 | 269 |
/// |
| 270 | 270 |
///\ref named-templ-param "Named parameter" for setting |
| 271 |
/// |
|
| 271 |
///ReachedMap type. |
|
| 272 | 272 |
template <class T> |
| 273 | 273 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
|
| 274 | 274 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
| 275 | 275 |
}; |
| 276 | 276 |
|
| 277 | 277 |
template <class T> |
| 278 | 278 |
struct SetProcessedMapTraits : public Traits {
|
| 279 | 279 |
typedef T ProcessedMap; |
| 280 | 280 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 281 | 281 |
{
|
| 282 | 282 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 283 | 283 |
return 0; // ignore warnings |
| 284 | 284 |
} |
| 285 | 285 |
}; |
| 286 | 286 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 287 |
/// |
|
| 287 |
///ProcessedMap type. |
|
| 288 | 288 |
/// |
| 289 | 289 |
///\ref named-templ-param "Named parameter" for setting |
| 290 |
/// |
|
| 290 |
///ProcessedMap type. |
|
| 291 | 291 |
template <class T> |
| 292 | 292 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
|
| 293 | 293 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
| 294 | 294 |
}; |
| 295 | 295 |
|
| 296 | 296 |
struct SetStandardProcessedMapTraits : public Traits {
|
| 297 | 297 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
| 298 | 298 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 299 | 299 |
{
|
| 300 | 300 |
return new ProcessedMap(g); |
| 301 | 301 |
} |
| 302 | 302 |
}; |
| 303 | 303 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 304 |
/// |
|
| 304 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 305 | 305 |
/// |
| 306 | 306 |
///\ref named-templ-param "Named parameter" for setting |
| 307 |
/// |
|
| 307 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 308 | 308 |
///If you don't set it explicitly, it will be automatically allocated. |
| 309 | 309 |
struct SetStandardProcessedMap : |
| 310 | 310 |
public Dfs< Digraph, SetStandardProcessedMapTraits > {
|
| 311 | 311 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
| 312 | 312 |
}; |
| 313 | 313 |
|
| 314 | 314 |
///@} |
| 315 | 315 |
|
| 316 | 316 |
public: |
| 317 | 317 |
|
| 318 | 318 |
///Constructor. |
| 319 | 319 |
|
| 320 | 320 |
///Constructor. |
| 321 | 321 |
///\param g The digraph the algorithm runs on. |
| 322 | 322 |
Dfs(const Digraph &g) : |
| 323 | 323 |
G(&g), |
| 324 | 324 |
_pred(NULL), local_pred(false), |
| 325 | 325 |
_dist(NULL), local_dist(false), |
| 326 | 326 |
_reached(NULL), local_reached(false), |
| 327 | 327 |
_processed(NULL), local_processed(false) |
| 328 | 328 |
{ }
|
| 329 | 329 |
|
| 330 | 330 |
///Destructor. |
| 331 | 331 |
~Dfs() |
| 332 | 332 |
{
|
| 333 | 333 |
if(local_pred) delete _pred; |
| 334 | 334 |
if(local_dist) delete _dist; |
| 335 | 335 |
if(local_reached) delete _reached; |
| 336 | 336 |
if(local_processed) delete _processed; |
| 337 | 337 |
} |
| 338 | 338 |
|
| 339 | 339 |
///Sets the map that stores the predecessor arcs. |
| 340 | 340 |
|
| 341 | 341 |
///Sets the map that stores the predecessor arcs. |
| 342 | 342 |
///If you don't use this function before calling \ref run(), |
| 343 | 343 |
///it will allocate one. The destructor deallocates this |
| 344 | 344 |
///automatically allocated map, of course. |
| 345 | 345 |
///\return <tt> (*this) </tt> |
| 346 | 346 |
Dfs &predMap(PredMap &m) |
| 347 | 347 |
{
|
| 348 | 348 |
if(local_pred) {
|
| 349 | 349 |
delete _pred; |
| 350 | 350 |
local_pred=false; |
| 351 | 351 |
} |
| 352 | 352 |
_pred = &m; |
| 353 | 353 |
return *this; |
| 354 | 354 |
} |
| 355 | 355 |
|
| 356 | 356 |
///Sets the map that indicates which nodes are reached. |
| 357 | 357 |
|
| 358 | 358 |
///Sets the map that indicates which nodes are reached. |
| 359 | 359 |
///If you don't use this function before calling \ref run(), |
| 360 | 360 |
///it will allocate one. The destructor deallocates this |
| 361 | 361 |
///automatically allocated map, of course. |
| 362 | 362 |
///\return <tt> (*this) </tt> |
| 363 | 363 |
Dfs &reachedMap(ReachedMap &m) |
| 364 | 364 |
{
|
| 365 | 365 |
if(local_reached) {
|
| 366 | 366 |
delete _reached; |
| 367 | 367 |
local_reached=false; |
| 368 | 368 |
} |
| 369 | 369 |
_reached = &m; |
| 370 | 370 |
return *this; |
| 371 | 371 |
} |
| 372 | 372 |
|
| 373 | 373 |
///Sets the map that indicates which nodes are processed. |
| 374 | 374 |
|
| 375 | 375 |
///Sets the map that indicates which nodes are processed. |
| 376 | 376 |
///If you don't use this function before calling \ref run(), |
| 377 | 377 |
///it will allocate one. The destructor deallocates this |
| 378 | 378 |
///automatically allocated map, of course. |
| 379 | 379 |
///\return <tt> (*this) </tt> |
| 380 | 380 |
Dfs &processedMap(ProcessedMap &m) |
| 381 | 381 |
{
|
| 382 | 382 |
if(local_processed) {
|
| 383 | 383 |
delete _processed; |
| 384 | 384 |
local_processed=false; |
| 385 | 385 |
} |
| 386 | 386 |
_processed = &m; |
| 387 | 387 |
return *this; |
| 388 | 388 |
} |
| 389 | 389 |
|
| 390 | 390 |
///Sets the map that stores the distances of the nodes. |
| 391 | 391 |
|
| 392 | 392 |
///Sets the map that stores the distances of the nodes calculated by |
| 393 | 393 |
///the algorithm. |
| 394 | 394 |
///If you don't use this function before calling \ref run(), |
| 395 | 395 |
///it will allocate one. The destructor deallocates this |
| 396 | 396 |
///automatically allocated map, of course. |
| 397 | 397 |
///\return <tt> (*this) </tt> |
| 398 | 398 |
Dfs &distMap(DistMap &m) |
| 399 | 399 |
{
|
| 400 | 400 |
if(local_dist) {
|
| 401 | 401 |
delete _dist; |
| 402 | 402 |
local_dist=false; |
| 403 | 403 |
} |
| ... | ... |
@@ -675,642 +675,642 @@ |
| 675 | 675 |
|
| 676 | 676 |
///Returns the DFS path to a node. |
| 677 | 677 |
/// |
| 678 | 678 |
///\warning \c t should be reachable from the root. |
| 679 | 679 |
/// |
| 680 | 680 |
///\pre Either \ref run() or \ref start() must be called before |
| 681 | 681 |
///using this function. |
| 682 | 682 |
Path path(Node t) const { return Path(*G, *_pred, t); }
|
| 683 | 683 |
|
| 684 | 684 |
///The distance of a node from the root. |
| 685 | 685 |
|
| 686 | 686 |
///Returns the distance of a node from the root. |
| 687 | 687 |
/// |
| 688 | 688 |
///\warning If node \c v is not reachable from the root, then |
| 689 | 689 |
///the return value of this function is undefined. |
| 690 | 690 |
/// |
| 691 | 691 |
///\pre Either \ref run() or \ref start() must be called before |
| 692 | 692 |
///using this function. |
| 693 | 693 |
int dist(Node v) const { return (*_dist)[v]; }
|
| 694 | 694 |
|
| 695 | 695 |
///Returns the 'previous arc' of the %DFS tree for a node. |
| 696 | 696 |
|
| 697 | 697 |
///This function returns the 'previous arc' of the %DFS tree for the |
| 698 | 698 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
| 699 | 699 |
///root to \c v. It is \c INVALID |
| 700 | 700 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
| 701 | 701 |
/// |
| 702 | 702 |
///The %DFS tree used here is equal to the %DFS tree used in |
| 703 | 703 |
///\ref predNode(). |
| 704 | 704 |
/// |
| 705 | 705 |
///\pre Either \ref run() or \ref start() must be called before using |
| 706 | 706 |
///this function. |
| 707 | 707 |
Arc predArc(Node v) const { return (*_pred)[v];}
|
| 708 | 708 |
|
| 709 | 709 |
///Returns the 'previous node' of the %DFS tree. |
| 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 |
///Instantiates a |
|
| 771 |
///Instantiates a PredMap. |
|
| 772 | 772 |
|
| 773 |
///This function instantiates a |
|
| 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 |
///Instantiates a |
|
| 787 |
///Instantiates a ProcessedMap. |
|
| 788 | 788 |
|
| 789 |
///This function instantiates a |
|
| 789 |
///This function instantiates a ProcessedMap. |
|
| 790 | 790 |
///\param g is the digraph, to which |
| 791 |
///we would like to define the |
|
| 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 |
///Instantiates a |
|
| 806 |
///Instantiates a ReachedMap. |
|
| 807 | 807 |
|
| 808 |
///This function instantiates a |
|
| 808 |
///This function instantiates a ReachedMap. |
|
| 809 | 809 |
///\param g is the digraph, to which |
| 810 |
///we would like to define the |
|
| 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 |
///Instantiates a |
|
| 821 |
///Instantiates a DistMap. |
|
| 822 | 822 |
|
| 823 |
///This function instantiates a |
|
| 823 |
///This function instantiates a DistMap. |
|
| 824 | 824 |
///\param g is the digraph, to which we would like to define |
| 825 |
///the |
|
| 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). |
| 967 | 967 |
/// |
| 968 | 968 |
///\return \c true if \c t is reachable form \c s. |
| 969 | 969 |
bool run(Node s, Node t) |
| 970 | 970 |
{
|
| 971 | 971 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 972 | 972 |
if (Base::_pred) |
| 973 | 973 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 974 | 974 |
if (Base::_dist) |
| 975 | 975 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 976 | 976 |
if (Base::_reached) |
| 977 | 977 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 978 | 978 |
if (Base::_processed) |
| 979 | 979 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 980 | 980 |
alg.run(s,t); |
| 981 | 981 |
if (Base::_path) |
| 982 | 982 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
| 983 | 983 |
if (Base::_di) |
| 984 | 984 |
*Base::_di = alg.dist(t); |
| 985 | 985 |
return alg.reached(t); |
| 986 | 986 |
} |
| 987 | 987 |
|
| 988 | 988 |
///Runs DFS algorithm to visit all nodes in the digraph. |
| 989 | 989 |
|
| 990 | 990 |
///This method runs DFS algorithm in order to compute |
| 991 | 991 |
///the DFS path to each node. |
| 992 | 992 |
void run() |
| 993 | 993 |
{
|
| 994 | 994 |
run(INVALID); |
| 995 | 995 |
} |
| 996 | 996 |
|
| 997 | 997 |
template<class T> |
| 998 | 998 |
struct SetPredMapBase : public Base {
|
| 999 | 999 |
typedef T PredMap; |
| 1000 | 1000 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1001 | 1001 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1002 | 1002 |
}; |
| 1003 | 1003 |
///\brief \ref named-func-param "Named parameter" |
| 1004 |
///for setting |
|
| 1004 |
///for setting PredMap object. |
|
| 1005 | 1005 |
/// |
| 1006 | 1006 |
///\ref named-func-param "Named parameter" |
| 1007 |
///for setting |
|
| 1007 |
///for setting PredMap object. |
|
| 1008 | 1008 |
template<class T> |
| 1009 | 1009 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
| 1010 | 1010 |
{
|
| 1011 | 1011 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1012 | 1012 |
return DfsWizard<SetPredMapBase<T> >(*this); |
| 1013 | 1013 |
} |
| 1014 | 1014 |
|
| 1015 | 1015 |
template<class T> |
| 1016 | 1016 |
struct SetReachedMapBase : public Base {
|
| 1017 | 1017 |
typedef T ReachedMap; |
| 1018 | 1018 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; };
|
| 1019 | 1019 |
SetReachedMapBase(const TR &b) : TR(b) {}
|
| 1020 | 1020 |
}; |
| 1021 | 1021 |
///\brief \ref named-func-param "Named parameter" |
| 1022 |
///for setting |
|
| 1022 |
///for setting ReachedMap object. |
|
| 1023 | 1023 |
/// |
| 1024 | 1024 |
/// \ref named-func-param "Named parameter" |
| 1025 |
///for setting |
|
| 1025 |
///for setting ReachedMap object. |
|
| 1026 | 1026 |
template<class T> |
| 1027 | 1027 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
| 1028 | 1028 |
{
|
| 1029 | 1029 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1030 | 1030 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
| 1031 | 1031 |
} |
| 1032 | 1032 |
|
| 1033 | 1033 |
template<class T> |
| 1034 | 1034 |
struct SetDistMapBase : public Base {
|
| 1035 | 1035 |
typedef T DistMap; |
| 1036 | 1036 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1037 | 1037 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1038 | 1038 |
}; |
| 1039 | 1039 |
///\brief \ref named-func-param "Named parameter" |
| 1040 |
///for setting |
|
| 1040 |
///for setting DistMap object. |
|
| 1041 | 1041 |
/// |
| 1042 | 1042 |
/// \ref named-func-param "Named parameter" |
| 1043 |
///for setting |
|
| 1043 |
///for setting DistMap object. |
|
| 1044 | 1044 |
template<class T> |
| 1045 | 1045 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
| 1046 | 1046 |
{
|
| 1047 | 1047 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1048 | 1048 |
return DfsWizard<SetDistMapBase<T> >(*this); |
| 1049 | 1049 |
} |
| 1050 | 1050 |
|
| 1051 | 1051 |
template<class T> |
| 1052 | 1052 |
struct SetProcessedMapBase : public Base {
|
| 1053 | 1053 |
typedef T ProcessedMap; |
| 1054 | 1054 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
| 1055 | 1055 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
| 1056 | 1056 |
}; |
| 1057 | 1057 |
///\brief \ref named-func-param "Named parameter" |
| 1058 |
///for setting |
|
| 1058 |
///for setting ProcessedMap object. |
|
| 1059 | 1059 |
/// |
| 1060 | 1060 |
/// \ref named-func-param "Named parameter" |
| 1061 |
///for setting |
|
| 1061 |
///for setting ProcessedMap object. |
|
| 1062 | 1062 |
template<class T> |
| 1063 | 1063 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
| 1064 | 1064 |
{
|
| 1065 | 1065 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1066 | 1066 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
| 1067 | 1067 |
} |
| 1068 | 1068 |
|
| 1069 | 1069 |
template<class T> |
| 1070 | 1070 |
struct SetPathBase : public Base {
|
| 1071 | 1071 |
typedef T Path; |
| 1072 | 1072 |
SetPathBase(const TR &b) : TR(b) {}
|
| 1073 | 1073 |
}; |
| 1074 | 1074 |
///\brief \ref named-func-param "Named parameter" |
| 1075 | 1075 |
///for getting the DFS path to the target node. |
| 1076 | 1076 |
/// |
| 1077 | 1077 |
///\ref named-func-param "Named parameter" |
| 1078 | 1078 |
///for getting the DFS path to the target node. |
| 1079 | 1079 |
template<class T> |
| 1080 | 1080 |
DfsWizard<SetPathBase<T> > path(const T &t) |
| 1081 | 1081 |
{
|
| 1082 | 1082 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1083 | 1083 |
return DfsWizard<SetPathBase<T> >(*this); |
| 1084 | 1084 |
} |
| 1085 | 1085 |
|
| 1086 | 1086 |
///\brief \ref named-func-param "Named parameter" |
| 1087 | 1087 |
///for getting the distance of the target node. |
| 1088 | 1088 |
/// |
| 1089 | 1089 |
///\ref named-func-param "Named parameter" |
| 1090 | 1090 |
///for getting the distance of the target node. |
| 1091 | 1091 |
DfsWizard dist(const int &d) |
| 1092 | 1092 |
{
|
| 1093 | 1093 |
Base::_di=const_cast<int*>(&d); |
| 1094 | 1094 |
return *this; |
| 1095 | 1095 |
} |
| 1096 | 1096 |
|
| 1097 | 1097 |
}; |
| 1098 | 1098 |
|
| 1099 | 1099 |
///Function-type interface for DFS algorithm. |
| 1100 | 1100 |
|
| 1101 | 1101 |
///\ingroup search |
| 1102 | 1102 |
///Function-type interface for DFS algorithm. |
| 1103 | 1103 |
/// |
| 1104 | 1104 |
///This function also has several \ref named-func-param "named parameters", |
| 1105 | 1105 |
///they are declared as the members of class \ref DfsWizard. |
| 1106 | 1106 |
///The following examples show how to use these parameters. |
| 1107 | 1107 |
///\code |
| 1108 | 1108 |
/// // Compute the DFS tree |
| 1109 | 1109 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
| 1110 | 1110 |
/// |
| 1111 | 1111 |
/// // Compute the DFS path from s to t |
| 1112 | 1112 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
| 1113 | 1113 |
///\endcode |
| 1114 | 1114 |
|
| 1115 | 1115 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
| 1116 | 1116 |
///to the end of the parameter list. |
| 1117 | 1117 |
///\sa DfsWizard |
| 1118 | 1118 |
///\sa Dfs |
| 1119 | 1119 |
template<class GR> |
| 1120 | 1120 |
DfsWizard<DfsWizardBase<GR> > |
| 1121 | 1121 |
dfs(const GR &digraph) |
| 1122 | 1122 |
{
|
| 1123 | 1123 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
| 1124 | 1124 |
} |
| 1125 | 1125 |
|
| 1126 | 1126 |
#ifdef DOXYGEN |
| 1127 | 1127 |
/// \brief Visitor class for DFS. |
| 1128 | 1128 |
/// |
| 1129 | 1129 |
/// This class defines the interface of the DfsVisit events, and |
| 1130 | 1130 |
/// it could be the base of a real visitor class. |
| 1131 | 1131 |
template <typename _Digraph> |
| 1132 | 1132 |
struct DfsVisitor {
|
| 1133 | 1133 |
typedef _Digraph Digraph; |
| 1134 | 1134 |
typedef typename Digraph::Arc Arc; |
| 1135 | 1135 |
typedef typename Digraph::Node Node; |
| 1136 | 1136 |
/// \brief Called for the source node of the DFS. |
| 1137 | 1137 |
/// |
| 1138 | 1138 |
/// This function is called for the source node of the DFS. |
| 1139 | 1139 |
void start(const Node& node) {}
|
| 1140 | 1140 |
/// \brief Called when the source node is leaved. |
| 1141 | 1141 |
/// |
| 1142 | 1142 |
/// This function is called when the source node is leaved. |
| 1143 | 1143 |
void stop(const Node& node) {}
|
| 1144 | 1144 |
/// \brief Called when a node is reached first time. |
| 1145 | 1145 |
/// |
| 1146 | 1146 |
/// This function is called when a node is reached first time. |
| 1147 | 1147 |
void reach(const Node& node) {}
|
| 1148 | 1148 |
/// \brief Called when an arc reaches a new node. |
| 1149 | 1149 |
/// |
| 1150 | 1150 |
/// This function is called when the DFS finds an arc whose target node |
| 1151 | 1151 |
/// is not reached yet. |
| 1152 | 1152 |
void discover(const Arc& arc) {}
|
| 1153 | 1153 |
/// \brief Called when an arc is examined but its target node is |
| 1154 | 1154 |
/// already discovered. |
| 1155 | 1155 |
/// |
| 1156 | 1156 |
/// This function is called when an arc is examined but its target node is |
| 1157 | 1157 |
/// already discovered. |
| 1158 | 1158 |
void examine(const Arc& arc) {}
|
| 1159 | 1159 |
/// \brief Called when the DFS steps back from a node. |
| 1160 | 1160 |
/// |
| 1161 | 1161 |
/// This function is called when the DFS steps back from a node. |
| 1162 | 1162 |
void leave(const Node& node) {}
|
| 1163 | 1163 |
/// \brief Called when the DFS steps back on an arc. |
| 1164 | 1164 |
/// |
| 1165 | 1165 |
/// This function is called when the DFS steps back on an arc. |
| 1166 | 1166 |
void backtrack(const Arc& arc) {}
|
| 1167 | 1167 |
}; |
| 1168 | 1168 |
#else |
| 1169 | 1169 |
template <typename _Digraph> |
| 1170 | 1170 |
struct DfsVisitor {
|
| 1171 | 1171 |
typedef _Digraph Digraph; |
| 1172 | 1172 |
typedef typename Digraph::Arc Arc; |
| 1173 | 1173 |
typedef typename Digraph::Node Node; |
| 1174 | 1174 |
void start(const Node&) {}
|
| 1175 | 1175 |
void stop(const Node&) {}
|
| 1176 | 1176 |
void reach(const Node&) {}
|
| 1177 | 1177 |
void discover(const Arc&) {}
|
| 1178 | 1178 |
void examine(const Arc&) {}
|
| 1179 | 1179 |
void leave(const Node&) {}
|
| 1180 | 1180 |
void backtrack(const Arc&) {}
|
| 1181 | 1181 |
|
| 1182 | 1182 |
template <typename _Visitor> |
| 1183 | 1183 |
struct Constraints {
|
| 1184 | 1184 |
void constraints() {
|
| 1185 | 1185 |
Arc arc; |
| 1186 | 1186 |
Node node; |
| 1187 | 1187 |
visitor.start(node); |
| 1188 | 1188 |
visitor.stop(arc); |
| 1189 | 1189 |
visitor.reach(node); |
| 1190 | 1190 |
visitor.discover(arc); |
| 1191 | 1191 |
visitor.examine(arc); |
| 1192 | 1192 |
visitor.leave(node); |
| 1193 | 1193 |
visitor.backtrack(arc); |
| 1194 | 1194 |
} |
| 1195 | 1195 |
_Visitor& visitor; |
| 1196 | 1196 |
}; |
| 1197 | 1197 |
}; |
| 1198 | 1198 |
#endif |
| 1199 | 1199 |
|
| 1200 | 1200 |
/// \brief Default traits class of DfsVisit class. |
| 1201 | 1201 |
/// |
| 1202 | 1202 |
/// Default traits class of DfsVisit class. |
| 1203 | 1203 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1204 | 1204 |
template<class _Digraph> |
| 1205 | 1205 |
struct DfsVisitDefaultTraits {
|
| 1206 | 1206 |
|
| 1207 | 1207 |
/// \brief The type of the digraph the algorithm runs on. |
| 1208 | 1208 |
typedef _Digraph Digraph; |
| 1209 | 1209 |
|
| 1210 | 1210 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1211 | 1211 |
/// |
| 1212 | 1212 |
/// The type of the map that indicates which nodes are reached. |
| 1213 | 1213 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 1214 | 1214 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1215 | 1215 |
|
| 1216 |
/// \brief Instantiates a |
|
| 1216 |
/// \brief Instantiates a ReachedMap. |
|
| 1217 | 1217 |
/// |
| 1218 |
/// This function instantiates a |
|
| 1218 |
/// This function instantiates a ReachedMap. |
|
| 1219 | 1219 |
/// \param digraph is the digraph, to which |
| 1220 |
/// we would like to define the |
|
| 1220 |
/// we would like to define the ReachedMap. |
|
| 1221 | 1221 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1222 | 1222 |
return new ReachedMap(digraph); |
| 1223 | 1223 |
} |
| 1224 | 1224 |
|
| 1225 | 1225 |
}; |
| 1226 | 1226 |
|
| 1227 | 1227 |
/// \ingroup search |
| 1228 | 1228 |
/// |
| 1229 | 1229 |
/// \brief %DFS algorithm class with visitor interface. |
| 1230 | 1230 |
/// |
| 1231 | 1231 |
/// This class provides an efficient implementation of the %DFS algorithm |
| 1232 | 1232 |
/// with visitor interface. |
| 1233 | 1233 |
/// |
| 1234 | 1234 |
/// The %DfsVisit class provides an alternative interface to the Dfs |
| 1235 | 1235 |
/// class. It works with callback mechanism, the DfsVisit object calls |
| 1236 | 1236 |
/// the member functions of the \c Visitor class on every DFS event. |
| 1237 | 1237 |
/// |
| 1238 | 1238 |
/// This interface of the DFS algorithm should be used in special cases |
| 1239 | 1239 |
/// when extra actions have to be performed in connection with certain |
| 1240 | 1240 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
| 1241 | 1241 |
/// instead. |
| 1242 | 1242 |
/// |
| 1243 | 1243 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1244 | 1244 |
/// The default value is |
| 1245 | 1245 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
| 1246 | 1246 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
| 1247 | 1247 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
| 1248 | 1248 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
| 1249 | 1249 |
/// does not observe the DFS events. If you want to observe the DFS |
| 1250 | 1250 |
/// events, you should implement your own visitor class. |
| 1251 | 1251 |
/// \tparam _Traits Traits class to set various data types used by the |
| 1252 | 1252 |
/// algorithm. The default traits class is |
| 1253 | 1253 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". |
| 1254 | 1254 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
| 1255 | 1255 |
/// a DFS visit traits class. |
| 1256 | 1256 |
#ifdef DOXYGEN |
| 1257 | 1257 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
| 1258 | 1258 |
#else |
| 1259 | 1259 |
template <typename _Digraph = ListDigraph, |
| 1260 | 1260 |
typename _Visitor = DfsVisitor<_Digraph>, |
| 1261 | 1261 |
typename _Traits = DfsVisitDefaultTraits<_Digraph> > |
| 1262 | 1262 |
#endif |
| 1263 | 1263 |
class DfsVisit {
|
| 1264 | 1264 |
public: |
| 1265 | 1265 |
|
| 1266 | 1266 |
///The traits class. |
| 1267 | 1267 |
typedef _Traits Traits; |
| 1268 | 1268 |
|
| 1269 | 1269 |
///The type of the digraph the algorithm runs on. |
| 1270 | 1270 |
typedef typename Traits::Digraph Digraph; |
| 1271 | 1271 |
|
| 1272 | 1272 |
///The visitor type used by the algorithm. |
| 1273 | 1273 |
typedef _Visitor Visitor; |
| 1274 | 1274 |
|
| 1275 | 1275 |
///The type of the map that indicates which nodes are reached. |
| 1276 | 1276 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1277 | 1277 |
|
| 1278 | 1278 |
private: |
| 1279 | 1279 |
|
| 1280 | 1280 |
typedef typename Digraph::Node Node; |
| 1281 | 1281 |
typedef typename Digraph::NodeIt NodeIt; |
| 1282 | 1282 |
typedef typename Digraph::Arc Arc; |
| 1283 | 1283 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1284 | 1284 |
|
| 1285 | 1285 |
//Pointer to the underlying digraph. |
| 1286 | 1286 |
const Digraph *_digraph; |
| 1287 | 1287 |
//Pointer to the visitor object. |
| 1288 | 1288 |
Visitor *_visitor; |
| 1289 | 1289 |
//Pointer to the map of reached status of the nodes. |
| 1290 | 1290 |
ReachedMap *_reached; |
| 1291 | 1291 |
//Indicates if _reached is locally allocated (true) or not. |
| 1292 | 1292 |
bool local_reached; |
| 1293 | 1293 |
|
| 1294 | 1294 |
std::vector<typename Digraph::Arc> _stack; |
| 1295 | 1295 |
int _stack_head; |
| 1296 | 1296 |
|
| 1297 | 1297 |
//Creates the maps if necessary. |
| 1298 | 1298 |
void create_maps() {
|
| 1299 | 1299 |
if(!_reached) {
|
| 1300 | 1300 |
local_reached = true; |
| 1301 | 1301 |
_reached = Traits::createReachedMap(*_digraph); |
| 1302 | 1302 |
} |
| 1303 | 1303 |
} |
| 1304 | 1304 |
|
| 1305 | 1305 |
protected: |
| 1306 | 1306 |
|
| 1307 | 1307 |
DfsVisit() {}
|
| 1308 | 1308 |
|
| 1309 | 1309 |
public: |
| 1310 | 1310 |
|
| 1311 | 1311 |
typedef DfsVisit Create; |
| 1312 | 1312 |
|
| 1313 | 1313 |
/// \name Named template parameters |
| 1314 | 1314 |
|
| 1315 | 1315 |
///@{
|
| 1316 | 1316 |
template <class T> |
| ... | ... |
@@ -46,507 +46,507 @@ |
| 46 | 46 |
} |
| 47 | 47 |
/// \brief Gives back the sum of the given two elements. |
| 48 | 48 |
static Value plus(const Value& left, const Value& right) {
|
| 49 | 49 |
return left + right; |
| 50 | 50 |
} |
| 51 | 51 |
/// \brief Gives back true only if the first value is less than the second. |
| 52 | 52 |
static bool less(const Value& left, const Value& right) {
|
| 53 | 53 |
return left < right; |
| 54 | 54 |
} |
| 55 | 55 |
}; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief Widest path operation traits for the Dijkstra algorithm class. |
| 58 | 58 |
/// |
| 59 | 59 |
/// This operation traits class defines all computational operations and |
| 60 | 60 |
/// constants which are used in the Dijkstra algorithm for widest path |
| 61 | 61 |
/// computation. |
| 62 | 62 |
/// |
| 63 | 63 |
/// \see DijkstraDefaultOperationTraits |
| 64 | 64 |
template <typename Value> |
| 65 | 65 |
struct DijkstraWidestPathOperationTraits {
|
| 66 | 66 |
/// \brief Gives back the maximum value of the type. |
| 67 | 67 |
static Value zero() {
|
| 68 | 68 |
return std::numeric_limits<Value>::max(); |
| 69 | 69 |
} |
| 70 | 70 |
/// \brief Gives back the minimum of the given two elements. |
| 71 | 71 |
static Value plus(const Value& left, const Value& right) {
|
| 72 | 72 |
return std::min(left, right); |
| 73 | 73 |
} |
| 74 | 74 |
/// \brief Gives back true only if the first value is less than the second. |
| 75 | 75 |
static bool less(const Value& left, const Value& right) {
|
| 76 | 76 |
return left < right; |
| 77 | 77 |
} |
| 78 | 78 |
}; |
| 79 | 79 |
|
| 80 | 80 |
///Default traits class of Dijkstra class. |
| 81 | 81 |
|
| 82 | 82 |
///Default traits class of Dijkstra class. |
| 83 | 83 |
///\tparam GR The type of the digraph. |
| 84 | 84 |
///\tparam LM The type of the length map. |
| 85 | 85 |
template<class GR, class LM> |
| 86 | 86 |
struct DijkstraDefaultTraits |
| 87 | 87 |
{
|
| 88 | 88 |
///The type of the digraph the algorithm runs on. |
| 89 | 89 |
typedef GR Digraph; |
| 90 | 90 |
|
| 91 | 91 |
///The type of the map that stores the arc lengths. |
| 92 | 92 |
|
| 93 | 93 |
///The type of the map that stores the arc lengths. |
| 94 | 94 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 95 | 95 |
typedef LM LengthMap; |
| 96 | 96 |
///The type of the length of the arcs. |
| 97 | 97 |
typedef typename LM::Value Value; |
| 98 | 98 |
|
| 99 | 99 |
/// Operation traits for Dijkstra algorithm. |
| 100 | 100 |
|
| 101 | 101 |
/// This class defines the operations that are used in the algorithm. |
| 102 | 102 |
/// \see DijkstraDefaultOperationTraits |
| 103 | 103 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
| 104 | 104 |
|
| 105 | 105 |
/// The cross reference type used by the heap. |
| 106 | 106 |
|
| 107 | 107 |
/// The cross reference type used by the heap. |
| 108 | 108 |
/// Usually it is \c Digraph::NodeMap<int>. |
| 109 | 109 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
| 110 | 110 |
///Instantiates a \ref HeapCrossRef. |
| 111 | 111 |
|
| 112 | 112 |
///This function instantiates a \ref HeapCrossRef. |
| 113 | 113 |
/// \param g is the digraph, to which we would like to define the |
| 114 | 114 |
/// \ref HeapCrossRef. |
| 115 | 115 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
| 116 | 116 |
{
|
| 117 | 117 |
return new HeapCrossRef(g); |
| 118 | 118 |
} |
| 119 | 119 |
|
| 120 | 120 |
///The heap type used by the Dijkstra algorithm. |
| 121 | 121 |
|
| 122 | 122 |
///The heap type used by the Dijkstra algorithm. |
| 123 | 123 |
/// |
| 124 | 124 |
///\sa BinHeap |
| 125 | 125 |
///\sa Dijkstra |
| 126 | 126 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
| 127 | 127 |
///Instantiates a \ref Heap. |
| 128 | 128 |
|
| 129 | 129 |
///This function instantiates a \ref Heap. |
| 130 | 130 |
static Heap *createHeap(HeapCrossRef& r) |
| 131 | 131 |
{
|
| 132 | 132 |
return new Heap(r); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
///\brief The type of the map that stores the predecessor |
| 136 | 136 |
///arcs of the shortest paths. |
| 137 | 137 |
/// |
| 138 | 138 |
///The type of the map that stores the predecessor |
| 139 | 139 |
///arcs of the shortest paths. |
| 140 | 140 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 141 | 141 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 142 |
///Instantiates a |
|
| 142 |
///Instantiates a PredMap. |
|
| 143 | 143 |
|
| 144 |
///This function instantiates a |
|
| 144 |
///This function instantiates a PredMap. |
|
| 145 | 145 |
///\param g is the digraph, to which we would like to define the |
| 146 |
/// |
|
| 146 |
///PredMap. |
|
| 147 | 147 |
static PredMap *createPredMap(const Digraph &g) |
| 148 | 148 |
{
|
| 149 | 149 |
return new PredMap(g); |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
///The type of the map that indicates which nodes are processed. |
| 153 | 153 |
|
| 154 | 154 |
///The type of the map that indicates which nodes are processed. |
| 155 | 155 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 156 | 156 |
///By default it is a NullMap. |
| 157 | 157 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 158 |
///Instantiates a |
|
| 158 |
///Instantiates a ProcessedMap. |
|
| 159 | 159 |
|
| 160 |
///This function instantiates a |
|
| 160 |
///This function instantiates a ProcessedMap. |
|
| 161 | 161 |
///\param g is the digraph, to which |
| 162 |
///we would like to define the |
|
| 162 |
///we would like to define the ProcessedMap |
|
| 163 | 163 |
#ifdef DOXYGEN |
| 164 | 164 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 165 | 165 |
#else |
| 166 | 166 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 167 | 167 |
#endif |
| 168 | 168 |
{
|
| 169 | 169 |
return new ProcessedMap(); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
///The type of the map that stores the distances of the nodes. |
| 173 | 173 |
|
| 174 | 174 |
///The type of the map that stores the distances of the nodes. |
| 175 | 175 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 176 | 176 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
| 177 |
///Instantiates a |
|
| 177 |
///Instantiates a DistMap. |
|
| 178 | 178 |
|
| 179 |
///This function instantiates a |
|
| 179 |
///This function instantiates a DistMap. |
|
| 180 | 180 |
///\param g is the digraph, to which we would like to define |
| 181 |
///the |
|
| 181 |
///the DistMap |
|
| 182 | 182 |
static DistMap *createDistMap(const Digraph &g) |
| 183 | 183 |
{
|
| 184 | 184 |
return new DistMap(g); |
| 185 | 185 |
} |
| 186 | 186 |
}; |
| 187 | 187 |
|
| 188 | 188 |
///%Dijkstra algorithm class. |
| 189 | 189 |
|
| 190 | 190 |
/// \ingroup shortest_path |
| 191 | 191 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
| 192 | 192 |
/// |
| 193 | 193 |
///The arc lengths are passed to the algorithm using a |
| 194 | 194 |
///\ref concepts::ReadMap "ReadMap", |
| 195 | 195 |
///so it is easy to change it to any kind of length. |
| 196 | 196 |
///The type of the length is determined by the |
| 197 | 197 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
| 198 | 198 |
///It is also possible to change the underlying priority heap. |
| 199 | 199 |
/// |
| 200 | 200 |
///There is also a \ref dijkstra() "function-type interface" for the |
| 201 | 201 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
| 202 | 202 |
///it can be used easier. |
| 203 | 203 |
/// |
| 204 | 204 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 205 | 205 |
///The default value is \ref ListDigraph. |
| 206 | 206 |
///The value of GR is not used directly by \ref Dijkstra, it is only |
| 207 | 207 |
///passed to \ref DijkstraDefaultTraits. |
| 208 | 208 |
///\tparam LM A readable arc map that determines the lengths of the |
| 209 | 209 |
///arcs. It is read once for each arc, so the map may involve in |
| 210 | 210 |
///relatively time consuming process to compute the arc lengths if |
| 211 | 211 |
///it is necessary. The default map type is \ref |
| 212 | 212 |
///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
| 213 | 213 |
///The value of LM is not used directly by \ref Dijkstra, it is only |
| 214 | 214 |
///passed to \ref DijkstraDefaultTraits. |
| 215 | 215 |
///\tparam TR Traits class to set various data types used by the algorithm. |
| 216 | 216 |
///The default traits class is \ref DijkstraDefaultTraits |
| 217 | 217 |
///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits |
| 218 | 218 |
///for the documentation of a Dijkstra traits class. |
| 219 | 219 |
#ifdef DOXYGEN |
| 220 | 220 |
template <typename GR, typename LM, typename TR> |
| 221 | 221 |
#else |
| 222 | 222 |
template <typename GR=ListDigraph, |
| 223 | 223 |
typename LM=typename GR::template ArcMap<int>, |
| 224 | 224 |
typename TR=DijkstraDefaultTraits<GR,LM> > |
| 225 | 225 |
#endif |
| 226 | 226 |
class Dijkstra {
|
| 227 | 227 |
public: |
| 228 | 228 |
|
| 229 | 229 |
///The type of the digraph the algorithm runs on. |
| 230 | 230 |
typedef typename TR::Digraph Digraph; |
| 231 | 231 |
|
| 232 | 232 |
///The type of the length of the arcs. |
| 233 | 233 |
typedef typename TR::LengthMap::Value Value; |
| 234 | 234 |
///The type of the map that stores the arc lengths. |
| 235 | 235 |
typedef typename TR::LengthMap LengthMap; |
| 236 | 236 |
///\brief The type of the map that stores the predecessor arcs of the |
| 237 | 237 |
///shortest paths. |
| 238 | 238 |
typedef typename TR::PredMap PredMap; |
| 239 | 239 |
///The type of the map that stores the distances of the nodes. |
| 240 | 240 |
typedef typename TR::DistMap DistMap; |
| 241 | 241 |
///The type of the map that indicates which nodes are processed. |
| 242 | 242 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 243 | 243 |
///The type of the paths. |
| 244 | 244 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 245 | 245 |
///The cross reference type used for the current heap. |
| 246 | 246 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
| 247 | 247 |
///The heap type used by the algorithm. |
| 248 | 248 |
typedef typename TR::Heap Heap; |
| 249 | 249 |
///The operation traits class. |
| 250 | 250 |
typedef typename TR::OperationTraits OperationTraits; |
| 251 | 251 |
|
| 252 | 252 |
///The traits class. |
| 253 | 253 |
typedef TR Traits; |
| 254 | 254 |
|
| 255 | 255 |
private: |
| 256 | 256 |
|
| 257 | 257 |
typedef typename Digraph::Node Node; |
| 258 | 258 |
typedef typename Digraph::NodeIt NodeIt; |
| 259 | 259 |
typedef typename Digraph::Arc Arc; |
| 260 | 260 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 261 | 261 |
|
| 262 | 262 |
//Pointer to the underlying digraph. |
| 263 | 263 |
const Digraph *G; |
| 264 | 264 |
//Pointer to the length map. |
| 265 | 265 |
const LengthMap *length; |
| 266 | 266 |
//Pointer to the map of predecessors arcs. |
| 267 | 267 |
PredMap *_pred; |
| 268 | 268 |
//Indicates if _pred is locally allocated (true) or not. |
| 269 | 269 |
bool local_pred; |
| 270 | 270 |
//Pointer to the map of distances. |
| 271 | 271 |
DistMap *_dist; |
| 272 | 272 |
//Indicates if _dist is locally allocated (true) or not. |
| 273 | 273 |
bool local_dist; |
| 274 | 274 |
//Pointer to the map of processed status of the nodes. |
| 275 | 275 |
ProcessedMap *_processed; |
| 276 | 276 |
//Indicates if _processed is locally allocated (true) or not. |
| 277 | 277 |
bool local_processed; |
| 278 | 278 |
//Pointer to the heap cross references. |
| 279 | 279 |
HeapCrossRef *_heap_cross_ref; |
| 280 | 280 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
| 281 | 281 |
bool local_heap_cross_ref; |
| 282 | 282 |
//Pointer to the heap. |
| 283 | 283 |
Heap *_heap; |
| 284 | 284 |
//Indicates if _heap is locally allocated (true) or not. |
| 285 | 285 |
bool local_heap; |
| 286 | 286 |
|
| 287 | 287 |
//Creates the maps if necessary. |
| 288 | 288 |
void create_maps() |
| 289 | 289 |
{
|
| 290 | 290 |
if(!_pred) {
|
| 291 | 291 |
local_pred = true; |
| 292 | 292 |
_pred = Traits::createPredMap(*G); |
| 293 | 293 |
} |
| 294 | 294 |
if(!_dist) {
|
| 295 | 295 |
local_dist = true; |
| 296 | 296 |
_dist = Traits::createDistMap(*G); |
| 297 | 297 |
} |
| 298 | 298 |
if(!_processed) {
|
| 299 | 299 |
local_processed = true; |
| 300 | 300 |
_processed = Traits::createProcessedMap(*G); |
| 301 | 301 |
} |
| 302 | 302 |
if (!_heap_cross_ref) {
|
| 303 | 303 |
local_heap_cross_ref = true; |
| 304 | 304 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
| 305 | 305 |
} |
| 306 | 306 |
if (!_heap) {
|
| 307 | 307 |
local_heap = true; |
| 308 | 308 |
_heap = Traits::createHeap(*_heap_cross_ref); |
| 309 | 309 |
} |
| 310 | 310 |
} |
| 311 | 311 |
|
| 312 | 312 |
public: |
| 313 | 313 |
|
| 314 | 314 |
typedef Dijkstra Create; |
| 315 | 315 |
|
| 316 | 316 |
///\name Named template parameters |
| 317 | 317 |
|
| 318 | 318 |
///@{
|
| 319 | 319 |
|
| 320 | 320 |
template <class T> |
| 321 | 321 |
struct SetPredMapTraits : public Traits {
|
| 322 | 322 |
typedef T PredMap; |
| 323 | 323 |
static PredMap *createPredMap(const Digraph &) |
| 324 | 324 |
{
|
| 325 | 325 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 326 | 326 |
return 0; // ignore warnings |
| 327 | 327 |
} |
| 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 |
{
|
| ... | ... |
@@ -893,414 +893,414 @@ |
| 893 | 893 |
///Checks if a node is reachable from the root(s). |
| 894 | 894 |
|
| 895 | 895 |
///Returns \c true if \c v is reachable from the root(s). |
| 896 | 896 |
///\pre Either \ref run() or \ref start() |
| 897 | 897 |
///must be called before using this function. |
| 898 | 898 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] !=
|
| 899 | 899 |
Heap::PRE_HEAP; } |
| 900 | 900 |
|
| 901 | 901 |
///Checks if a node is processed. |
| 902 | 902 |
|
| 903 | 903 |
///Returns \c true if \c v is processed, i.e. the shortest |
| 904 | 904 |
///path to \c v has already found. |
| 905 | 905 |
///\pre Either \ref run() or \ref init() |
| 906 | 906 |
///must be called before using this function. |
| 907 | 907 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] ==
|
| 908 | 908 |
Heap::POST_HEAP; } |
| 909 | 909 |
|
| 910 | 910 |
///The current distance of a node from the root(s). |
| 911 | 911 |
|
| 912 | 912 |
///Returns the current distance of a node from the root(s). |
| 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 |
///Instantiates a |
|
| 989 |
///Instantiates a PredMap. |
|
| 990 | 990 |
|
| 991 |
///This function instantiates a |
|
| 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 |
///Instantiates a |
|
| 1005 |
///Instantiates a ProcessedMap. |
|
| 1006 | 1006 |
|
| 1007 |
///This function instantiates a |
|
| 1007 |
///This function instantiates a ProcessedMap. |
|
| 1008 | 1008 |
///\param g is the digraph, to which |
| 1009 |
///we would like to define the |
|
| 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 |
///Instantiates a |
|
| 1024 |
///Instantiates a DistMap. |
|
| 1025 | 1025 |
|
| 1026 |
///This function instantiates a |
|
| 1026 |
///This function instantiates a DistMap. |
|
| 1027 | 1027 |
///\param g is the digraph, to which we would like to define |
| 1028 |
///the |
|
| 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 |
|
| 1170 | 1170 |
///This method runs the %Dijkstra algorithm from node \c s |
| 1171 | 1171 |
///in order to compute the shortest path to node \c t |
| 1172 | 1172 |
///(it stops searching when \c t is processed). |
| 1173 | 1173 |
/// |
| 1174 | 1174 |
///\return \c true if \c t is reachable form \c s. |
| 1175 | 1175 |
bool run(Node s, Node t) |
| 1176 | 1176 |
{
|
| 1177 | 1177 |
Dijkstra<Digraph,LengthMap,TR> |
| 1178 | 1178 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
| 1179 | 1179 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
| 1180 | 1180 |
if (Base::_pred) |
| 1181 | 1181 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1182 | 1182 |
if (Base::_dist) |
| 1183 | 1183 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1184 | 1184 |
if (Base::_processed) |
| 1185 | 1185 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1186 | 1186 |
dijk.run(s,t); |
| 1187 | 1187 |
if (Base::_path) |
| 1188 | 1188 |
*reinterpret_cast<Path*>(Base::_path) = dijk.path(t); |
| 1189 | 1189 |
if (Base::_di) |
| 1190 | 1190 |
*reinterpret_cast<Value*>(Base::_di) = dijk.dist(t); |
| 1191 | 1191 |
return dijk.reached(t); |
| 1192 | 1192 |
} |
| 1193 | 1193 |
|
| 1194 | 1194 |
template<class T> |
| 1195 | 1195 |
struct SetPredMapBase : public Base {
|
| 1196 | 1196 |
typedef T PredMap; |
| 1197 | 1197 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1198 | 1198 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1199 | 1199 |
}; |
| 1200 | 1200 |
///\brief \ref named-func-param "Named parameter" |
| 1201 |
///for setting |
|
| 1201 |
///for setting PredMap object. |
|
| 1202 | 1202 |
/// |
| 1203 | 1203 |
///\ref named-func-param "Named parameter" |
| 1204 |
///for setting |
|
| 1204 |
///for setting PredMap object. |
|
| 1205 | 1205 |
template<class T> |
| 1206 | 1206 |
DijkstraWizard<SetPredMapBase<T> > predMap(const T &t) |
| 1207 | 1207 |
{
|
| 1208 | 1208 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1209 | 1209 |
return DijkstraWizard<SetPredMapBase<T> >(*this); |
| 1210 | 1210 |
} |
| 1211 | 1211 |
|
| 1212 | 1212 |
template<class T> |
| 1213 | 1213 |
struct SetDistMapBase : public Base {
|
| 1214 | 1214 |
typedef T DistMap; |
| 1215 | 1215 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1216 | 1216 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1217 | 1217 |
}; |
| 1218 | 1218 |
///\brief \ref named-func-param "Named parameter" |
| 1219 |
///for setting |
|
| 1219 |
///for setting DistMap object. |
|
| 1220 | 1220 |
/// |
| 1221 | 1221 |
///\ref named-func-param "Named parameter" |
| 1222 |
///for setting |
|
| 1222 |
///for setting DistMap object. |
|
| 1223 | 1223 |
template<class T> |
| 1224 | 1224 |
DijkstraWizard<SetDistMapBase<T> > distMap(const T &t) |
| 1225 | 1225 |
{
|
| 1226 | 1226 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1227 | 1227 |
return DijkstraWizard<SetDistMapBase<T> >(*this); |
| 1228 | 1228 |
} |
| 1229 | 1229 |
|
| 1230 | 1230 |
template<class T> |
| 1231 | 1231 |
struct SetProcessedMapBase : public Base {
|
| 1232 | 1232 |
typedef T ProcessedMap; |
| 1233 | 1233 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
| 1234 | 1234 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
| 1235 | 1235 |
}; |
| 1236 | 1236 |
///\brief \ref named-func-param "Named parameter" |
| 1237 |
///for setting |
|
| 1237 |
///for setting ProcessedMap object. |
|
| 1238 | 1238 |
/// |
| 1239 | 1239 |
/// \ref named-func-param "Named parameter" |
| 1240 |
///for setting |
|
| 1240 |
///for setting ProcessedMap object. |
|
| 1241 | 1241 |
template<class T> |
| 1242 | 1242 |
DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
| 1243 | 1243 |
{
|
| 1244 | 1244 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1245 | 1245 |
return DijkstraWizard<SetProcessedMapBase<T> >(*this); |
| 1246 | 1246 |
} |
| 1247 | 1247 |
|
| 1248 | 1248 |
template<class T> |
| 1249 | 1249 |
struct SetPathBase : public Base {
|
| 1250 | 1250 |
typedef T Path; |
| 1251 | 1251 |
SetPathBase(const TR &b) : TR(b) {}
|
| 1252 | 1252 |
}; |
| 1253 | 1253 |
///\brief \ref named-func-param "Named parameter" |
| 1254 | 1254 |
///for getting the shortest path to the target node. |
| 1255 | 1255 |
/// |
| 1256 | 1256 |
///\ref named-func-param "Named parameter" |
| 1257 | 1257 |
///for getting the shortest path to the target node. |
| 1258 | 1258 |
template<class T> |
| 1259 | 1259 |
DijkstraWizard<SetPathBase<T> > path(const T &t) |
| 1260 | 1260 |
{
|
| 1261 | 1261 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1262 | 1262 |
return DijkstraWizard<SetPathBase<T> >(*this); |
| 1263 | 1263 |
} |
| 1264 | 1264 |
|
| 1265 | 1265 |
///\brief \ref named-func-param "Named parameter" |
| 1266 | 1266 |
///for getting the distance of the target node. |
| 1267 | 1267 |
/// |
| 1268 | 1268 |
///\ref named-func-param "Named parameter" |
| 1269 | 1269 |
///for getting the distance of the target node. |
| 1270 | 1270 |
DijkstraWizard dist(const Value &d) |
| 1271 | 1271 |
{
|
| 1272 | 1272 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
| 1273 | 1273 |
return *this; |
| 1274 | 1274 |
} |
| 1275 | 1275 |
|
| 1276 | 1276 |
}; |
| 1277 | 1277 |
|
| 1278 | 1278 |
///Function-type interface for Dijkstra algorithm. |
| 1279 | 1279 |
|
| 1280 | 1280 |
/// \ingroup shortest_path |
| 1281 | 1281 |
///Function-type interface for Dijkstra algorithm. |
| 1282 | 1282 |
/// |
| 1283 | 1283 |
///This function also has several \ref named-func-param "named parameters", |
| 1284 | 1284 |
///they are declared as the members of class \ref DijkstraWizard. |
| 1285 | 1285 |
///The following examples show how to use these parameters. |
| 1286 | 1286 |
///\code |
| 1287 | 1287 |
/// // Compute shortest path from node s to each node |
| 1288 | 1288 |
/// dijkstra(g,length).predMap(preds).distMap(dists).run(s); |
| 1289 | 1289 |
/// |
| 1290 | 1290 |
/// // Compute shortest path from s to t |
| 1291 | 1291 |
/// bool reached = dijkstra(g,length).path(p).dist(d).run(s,t); |
| 1292 | 1292 |
///\endcode |
| 1293 | 1293 |
///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
| 1294 | 1294 |
///to the end of the parameter list. |
| 1295 | 1295 |
///\sa DijkstraWizard |
| 1296 | 1296 |
///\sa Dijkstra |
| 1297 | 1297 |
template<class GR, class LM> |
| 1298 | 1298 |
DijkstraWizard<DijkstraWizardBase<GR,LM> > |
| 1299 | 1299 |
dijkstra(const GR &digraph, const LM &length) |
| 1300 | 1300 |
{
|
| 1301 | 1301 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length); |
| 1302 | 1302 |
} |
| 1303 | 1303 |
|
| 1304 | 1304 |
} //END OF NAMESPACE LEMON |
| 1305 | 1305 |
|
| 1306 | 1306 |
#endif |
| ... | ... |
@@ -166,196 +166,196 @@ |
| 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. |
| ... | ... |
@@ -480,269 +480,253 @@ |
| 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 |
///\ingroup maps |
|
| 579 | 578 |
///Map of x-coordinates of a \ref Point "Point"-map. |
| 580 | 579 |
/// |
| 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 \ref XMap class. |
|
| 598 |
/// |
|
| 599 |
/// |
|
| 596 |
///This function just returns an XMap class. |
|
| 600 | 597 |
///\relates XMap |
| 601 | 598 |
template<class M> |
| 602 | 599 |
inline XMap<M> xMap(M &m) |
| 603 | 600 |
{
|
| 604 | 601 |
return XMap<M>(m); |
| 605 | 602 |
} |
| 606 | 603 |
|
| 607 | 604 |
template<class M> |
| 608 | 605 |
inline XMap<M> xMap(const M &m) |
| 609 | 606 |
{
|
| 610 | 607 |
return XMap<M>(m); |
| 611 | 608 |
} |
| 612 | 609 |
|
| 613 |
///Constant (read only) version of |
|
| 610 |
///Constant (read only) version of XMap |
|
| 614 | 611 |
|
| 615 |
///\ingroup maps |
|
| 616 |
///Constant (read only) version of \ref XMap |
|
| 612 |
///Constant (read only) version of XMap. |
|
| 617 | 613 |
/// |
| 618 | 614 |
template<class M> |
| 619 | 615 |
class ConstXMap |
| 620 | 616 |
{
|
| 621 | 617 |
const M& _map; |
| 622 | 618 |
public: |
| 623 | 619 |
|
| 624 | 620 |
typedef typename M::Value::Value Value; |
| 625 | 621 |
typedef typename M::Key Key; |
| 626 | 622 |
///\e |
| 627 | 623 |
ConstXMap(const M &map) : _map(map) {}
|
| 628 | 624 |
Value operator[](Key k) const {return _map[k].x;}
|
| 629 | 625 |
}; |
| 630 | 626 |
|
| 631 |
///Returns a |
|
| 627 |
///Returns a ConstXMap class |
|
| 632 | 628 |
|
| 633 |
///This function just returns a \ref ConstXMap class. |
|
| 634 |
/// |
|
| 635 |
/// |
|
| 629 |
///This function just returns a ConstXMap class. |
|
| 636 | 630 |
///\relates ConstXMap |
| 637 | 631 |
template<class M> |
| 638 | 632 |
inline ConstXMap<M> xMap(const M &m) |
| 639 | 633 |
{
|
| 640 | 634 |
return ConstXMap<M>(m); |
| 641 | 635 |
} |
| 642 | 636 |
|
| 643 |
///Map of y-coordinates of a |
|
| 637 |
///Map of y-coordinates of a <tt>Point</tt>-map |
|
| 644 | 638 |
|
| 645 |
///\ingroup maps |
|
| 646 | 639 |
///Map of y-coordinates of a \ref Point "Point"-map. |
| 647 | 640 |
/// |
| 648 | 641 |
template<class M> |
| 649 | 642 |
class YMap |
| 650 | 643 |
{
|
| 651 | 644 |
M& _map; |
| 652 | 645 |
public: |
| 653 | 646 |
|
| 654 | 647 |
typedef typename M::Value::Value Value; |
| 655 | 648 |
typedef typename M::Key Key; |
| 656 | 649 |
///\e |
| 657 | 650 |
YMap(M& map) : _map(map) {}
|
| 658 | 651 |
Value operator[](Key k) const {return _map[k].y;}
|
| 659 | 652 |
void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
|
| 660 | 653 |
}; |
| 661 | 654 |
|
| 662 |
///Returns a |
|
| 655 |
///Returns a YMap class |
|
| 663 | 656 |
|
| 664 |
///This function just returns a \ref YMap class. |
|
| 665 |
/// |
|
| 666 |
/// |
|
| 657 |
///This function just returns a YMap class. |
|
| 667 | 658 |
///\relates YMap |
| 668 | 659 |
template<class M> |
| 669 | 660 |
inline YMap<M> yMap(M &m) |
| 670 | 661 |
{
|
| 671 | 662 |
return YMap<M>(m); |
| 672 | 663 |
} |
| 673 | 664 |
|
| 674 | 665 |
template<class M> |
| 675 | 666 |
inline YMap<M> yMap(const M &m) |
| 676 | 667 |
{
|
| 677 | 668 |
return YMap<M>(m); |
| 678 | 669 |
} |
| 679 | 670 |
|
| 680 |
///Constant (read only) version of |
|
| 671 |
///Constant (read only) version of YMap |
|
| 681 | 672 |
|
| 682 |
///\ingroup maps |
|
| 683 |
///Constant (read only) version of \ref YMap |
|
| 673 |
///Constant (read only) version of YMap. |
|
| 684 | 674 |
/// |
| 685 | 675 |
template<class M> |
| 686 | 676 |
class ConstYMap |
| 687 | 677 |
{
|
| 688 | 678 |
const M& _map; |
| 689 | 679 |
public: |
| 690 | 680 |
|
| 691 | 681 |
typedef typename M::Value::Value Value; |
| 692 | 682 |
typedef typename M::Key Key; |
| 693 | 683 |
///\e |
| 694 | 684 |
ConstYMap(const M &map) : _map(map) {}
|
| 695 | 685 |
Value operator[](Key k) const {return _map[k].y;}
|
| 696 | 686 |
}; |
| 697 | 687 |
|
| 698 |
///Returns a |
|
| 688 |
///Returns a ConstYMap class |
|
| 699 | 689 |
|
| 700 |
///This function just returns a \ref ConstYMap class. |
|
| 701 |
/// |
|
| 702 |
/// |
|
| 690 |
///This function just returns a ConstYMap class. |
|
| 703 | 691 |
///\relates ConstYMap |
| 704 | 692 |
template<class M> |
| 705 | 693 |
inline ConstYMap<M> yMap(const M &m) |
| 706 | 694 |
{
|
| 707 | 695 |
return ConstYMap<M>(m); |
| 708 | 696 |
} |
| 709 | 697 |
|
| 710 | 698 |
|
| 711 |
///\brief Map of the \ref Point::normSquare() "normSquare()" |
|
| 712 |
///of a \ref Point "Point"-map |
|
| 699 |
///\brief Map of the normSquare() of a <tt>Point</tt>-map |
|
| 713 | 700 |
/// |
| 714 | 701 |
///Map of the \ref Point::normSquare() "normSquare()" |
| 715 | 702 |
///of a \ref Point "Point"-map. |
| 716 |
///\ingroup maps |
|
| 717 | 703 |
template<class M> |
| 718 | 704 |
class NormSquareMap |
| 719 | 705 |
{
|
| 720 | 706 |
const M& _map; |
| 721 | 707 |
public: |
| 722 | 708 |
|
| 723 | 709 |
typedef typename M::Value::Value Value; |
| 724 | 710 |
typedef typename M::Key Key; |
| 725 | 711 |
///\e |
| 726 | 712 |
NormSquareMap(const M &map) : _map(map) {}
|
| 727 | 713 |
Value operator[](Key k) const {return _map[k].normSquare();}
|
| 728 | 714 |
}; |
| 729 | 715 |
|
| 730 |
///Returns a |
|
| 716 |
///Returns a NormSquareMap class |
|
| 731 | 717 |
|
| 732 |
///This function just returns a \ref NormSquareMap class. |
|
| 733 |
/// |
|
| 734 |
/// |
|
| 718 |
///This function just returns a NormSquareMap class. |
|
| 735 | 719 |
///\relates NormSquareMap |
| 736 | 720 |
template<class M> |
| 737 | 721 |
inline NormSquareMap<M> normSquareMap(const M &m) |
| 738 | 722 |
{
|
| 739 | 723 |
return NormSquareMap<M>(m); |
| 740 | 724 |
} |
| 741 | 725 |
|
| 742 | 726 |
/// @} |
| 743 | 727 |
|
| 744 | 728 |
} //namespce dim2 |
| 745 | 729 |
|
| 746 | 730 |
} //namespace lemon |
| 747 | 731 |
|
| 748 | 732 |
#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), |
| ... | ... |
@@ -298,201 +298,223 @@ |
| 298 | 298 |
if (c == '\"') {
|
| 299 | 299 |
while (is.get(c) && c != '\"') {
|
| 300 | 300 |
if (c == '\\') |
| 301 | 301 |
c = readEscape(is); |
| 302 | 302 |
os << c; |
| 303 | 303 |
} |
| 304 | 304 |
if (!is) |
| 305 | 305 |
throw FormatError("Quoted format error");
|
| 306 | 306 |
} else {
|
| 307 | 307 |
is.putback(c); |
| 308 | 308 |
while (is.get(c) && !isWhiteSpace(c)) {
|
| 309 | 309 |
if (c == '\\') |
| 310 | 310 |
c = readEscape(is); |
| 311 | 311 |
os << c; |
| 312 | 312 |
} |
| 313 | 313 |
if (!is) {
|
| 314 | 314 |
is.clear(); |
| 315 | 315 |
} else {
|
| 316 | 316 |
is.putback(c); |
| 317 | 317 |
} |
| 318 | 318 |
} |
| 319 | 319 |
str = os.str(); |
| 320 | 320 |
return is; |
| 321 | 321 |
} |
| 322 | 322 |
|
| 323 | 323 |
class Section {
|
| 324 | 324 |
public: |
| 325 | 325 |
virtual ~Section() {}
|
| 326 | 326 |
virtual void process(std::istream& is, int& line_num) = 0; |
| 327 | 327 |
}; |
| 328 | 328 |
|
| 329 | 329 |
template <typename Functor> |
| 330 | 330 |
class LineSection : public Section {
|
| 331 | 331 |
private: |
| 332 | 332 |
|
| 333 | 333 |
Functor _functor; |
| 334 | 334 |
|
| 335 | 335 |
public: |
| 336 | 336 |
|
| 337 | 337 |
LineSection(const Functor& functor) : _functor(functor) {}
|
| 338 | 338 |
virtual ~LineSection() {}
|
| 339 | 339 |
|
| 340 | 340 |
virtual void process(std::istream& is, int& line_num) {
|
| 341 | 341 |
char c; |
| 342 | 342 |
std::string line; |
| 343 | 343 |
while (is.get(c) && c != '@') {
|
| 344 | 344 |
if (c == '\n') {
|
| 345 | 345 |
++line_num; |
| 346 | 346 |
} else if (c == '#') {
|
| 347 | 347 |
getline(is, line); |
| 348 | 348 |
++line_num; |
| 349 | 349 |
} else if (!isWhiteSpace(c)) {
|
| 350 | 350 |
is.putback(c); |
| 351 | 351 |
getline(is, line); |
| 352 | 352 |
_functor(line); |
| 353 | 353 |
++line_num; |
| 354 | 354 |
} |
| 355 | 355 |
} |
| 356 | 356 |
if (is) is.putback(c); |
| 357 | 357 |
else if (is.eof()) is.clear(); |
| 358 | 358 |
} |
| 359 | 359 |
}; |
| 360 | 360 |
|
| 361 | 361 |
template <typename Functor> |
| 362 | 362 |
class StreamSection : public Section {
|
| 363 | 363 |
private: |
| 364 | 364 |
|
| 365 | 365 |
Functor _functor; |
| 366 | 366 |
|
| 367 | 367 |
public: |
| 368 | 368 |
|
| 369 | 369 |
StreamSection(const Functor& functor) : _functor(functor) {}
|
| 370 | 370 |
virtual ~StreamSection() {}
|
| 371 | 371 |
|
| 372 | 372 |
virtual void process(std::istream& is, int& line_num) {
|
| 373 | 373 |
_functor(is, line_num); |
| 374 | 374 |
char c; |
| 375 | 375 |
std::string line; |
| 376 | 376 |
while (is.get(c) && c != '@') {
|
| 377 | 377 |
if (c == '\n') {
|
| 378 | 378 |
++line_num; |
| 379 | 379 |
} else if (!isWhiteSpace(c)) {
|
| 380 | 380 |
getline(is, line); |
| 381 | 381 |
++line_num; |
| 382 | 382 |
} |
| 383 | 383 |
} |
| 384 | 384 |
if (is) is.putback(c); |
| 385 | 385 |
else if (is.eof()) is.clear(); |
| 386 | 386 |
} |
| 387 | 387 |
}; |
| 388 | 388 |
|
| 389 | 389 |
} |
| 390 | 390 |
|
| 391 | 391 |
template <typename Digraph> |
| 392 | 392 |
class DigraphReader; |
| 393 | 393 |
|
| 394 |
/// \brief Return a \ref DigraphReader class |
|
| 395 |
/// |
|
| 396 |
/// This function just returns a \ref DigraphReader class. |
|
| 397 |
/// \relates DigraphReader |
|
| 394 | 398 |
template <typename Digraph> |
| 395 | 399 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
| 396 |
std::istream& is = std::cin); |
|
| 397 |
|
|
| 400 |
std::istream& is = std::cin) {
|
|
| 401 |
DigraphReader<Digraph> tmp(digraph, is); |
|
| 402 |
return tmp; |
|
| 403 |
} |
|
| 404 |
|
|
| 405 |
/// \brief Return a \ref DigraphReader class |
|
| 406 |
/// |
|
| 407 |
/// This function just returns a \ref DigraphReader class. |
|
| 408 |
/// \relates DigraphReader |
|
| 398 | 409 |
template <typename Digraph> |
| 399 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const std::string& fn); |
|
| 400 |
|
|
| 410 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
|
| 411 |
const std::string& fn) {
|
|
| 412 |
DigraphReader<Digraph> tmp(digraph, fn); |
|
| 413 |
return tmp; |
|
| 414 |
} |
|
| 415 |
|
|
| 416 |
/// \brief Return a \ref DigraphReader class |
|
| 417 |
/// |
|
| 418 |
/// This function just returns a \ref DigraphReader class. |
|
| 419 |
/// \relates DigraphReader |
|
| 401 | 420 |
template <typename Digraph> |
| 402 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const char |
|
| 421 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const char* fn) {
|
|
| 422 |
DigraphReader<Digraph> tmp(digraph, fn); |
|
| 423 |
return tmp; |
|
| 424 |
} |
|
| 403 | 425 |
|
| 404 | 426 |
/// \ingroup lemon_io |
| 405 | 427 |
/// |
| 406 | 428 |
/// \brief \ref lgf-format "LGF" reader for directed graphs |
| 407 | 429 |
/// |
| 408 | 430 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 409 | 431 |
/// |
| 410 | 432 |
/// The reading method does a batch processing. The user creates a |
| 411 | 433 |
/// reader object, then various reading rules can be added to the |
| 412 | 434 |
/// reader, and eventually the reading is executed with the \c run() |
| 413 | 435 |
/// member function. A map reading rule can be added to the reader |
| 414 | 436 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
| 415 | 437 |
/// converter parameter can also be added as a standard functor |
| 416 | 438 |
/// converting from \c std::string to the value type of the map. If it |
| 417 | 439 |
/// is set, it will determine how the tokens in the file should be |
| 418 | 440 |
/// converted to the value type of the map. If the functor is not set, |
| 419 | 441 |
/// then a default conversion will be used. One map can be read into |
| 420 | 442 |
/// multiple map objects at the same time. The \c attribute(), \c |
| 421 | 443 |
/// node() and \c arc() functions are used to add attribute reading |
| 422 | 444 |
/// rules. |
| 423 | 445 |
/// |
| 424 | 446 |
///\code |
| 425 | 447 |
/// DigraphReader<Digraph>(digraph, std::cin). |
| 426 | 448 |
/// nodeMap("coordinates", coord_map).
|
| 427 | 449 |
/// arcMap("capacity", cap_map).
|
| 428 | 450 |
/// node("source", src).
|
| 429 | 451 |
/// node("target", trg).
|
| 430 | 452 |
/// attribute("caption", caption).
|
| 431 | 453 |
/// run(); |
| 432 | 454 |
///\endcode |
| 433 | 455 |
/// |
| 434 | 456 |
/// By default the reader uses the first section in the file of the |
| 435 | 457 |
/// proper type. If a section has an optional name, then it can be |
| 436 | 458 |
/// selected for reading by giving an optional name parameter to the |
| 437 | 459 |
/// \c nodes(), \c arcs() or \c attributes() functions. |
| 438 | 460 |
/// |
| 439 | 461 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader |
| 440 | 462 |
/// that the nodes or arcs should not be constructed (added to the |
| 441 | 463 |
/// graph) during the reading, but instead the label map of the items |
| 442 | 464 |
/// are given as a parameter of these functions. An |
| 443 | 465 |
/// application of these functions is multipass reading, which is |
| 444 | 466 |
/// important if two \c \@arcs sections must be read from the |
| 445 | 467 |
/// file. In this case the first phase would read the node set and one |
| 446 | 468 |
/// of the arc sets, while the second phase would read the second arc |
| 447 | 469 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
| 448 | 470 |
/// The previously read label node map should be passed to the \c |
| 449 | 471 |
/// useNodes() functions. Another application of multipass reading when |
| 450 | 472 |
/// paths are given as a node map or an arc map. |
| 451 | 473 |
/// It is impossible to read this in |
| 452 | 474 |
/// a single pass, because the arcs are not constructed when the node |
| 453 | 475 |
/// maps are read. |
| 454 | 476 |
template <typename _Digraph> |
| 455 | 477 |
class DigraphReader {
|
| 456 | 478 |
public: |
| 457 | 479 |
|
| 458 | 480 |
typedef _Digraph Digraph; |
| 459 | 481 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 460 | 482 |
|
| 461 | 483 |
private: |
| 462 | 484 |
|
| 463 | 485 |
|
| 464 | 486 |
std::istream* _is; |
| 465 | 487 |
bool local_is; |
| 466 | 488 |
std::string _filename; |
| 467 | 489 |
|
| 468 | 490 |
Digraph& _digraph; |
| 469 | 491 |
|
| 470 | 492 |
std::string _nodes_caption; |
| 471 | 493 |
std::string _arcs_caption; |
| 472 | 494 |
std::string _attributes_caption; |
| 473 | 495 |
|
| 474 | 496 |
typedef std::map<std::string, Node> NodeIndex; |
| 475 | 497 |
NodeIndex _node_index; |
| 476 | 498 |
typedef std::map<std::string, Arc> ArcIndex; |
| 477 | 499 |
ArcIndex _arc_index; |
| 478 | 500 |
|
| 479 | 501 |
typedef std::vector<std::pair<std::string, |
| 480 | 502 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 481 | 503 |
NodeMaps _node_maps; |
| 482 | 504 |
|
| 483 | 505 |
typedef std::vector<std::pair<std::string, |
| 484 | 506 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
| 485 | 507 |
ArcMaps _arc_maps; |
| 486 | 508 |
|
| 487 | 509 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 488 | 510 |
Attributes; |
| 489 | 511 |
Attributes _attributes; |
| 490 | 512 |
|
| 491 | 513 |
bool _use_nodes; |
| 492 | 514 |
bool _use_arcs; |
| 493 | 515 |
|
| 494 | 516 |
bool _skip_nodes; |
| 495 | 517 |
bool _skip_arcs; |
| 496 | 518 |
|
| 497 | 519 |
int line_num; |
| 498 | 520 |
std::istringstream line; |
| ... | ... |
@@ -1096,237 +1118,225 @@ |
| 1096 | 1118 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 1097 | 1119 |
while (it != _attributes.end() && it->first == attr) {
|
| 1098 | 1120 |
it->second->set(token); |
| 1099 | 1121 |
++it; |
| 1100 | 1122 |
} |
| 1101 | 1123 |
} |
| 1102 | 1124 |
|
| 1103 | 1125 |
} |
| 1104 | 1126 |
if (readSuccess()) {
|
| 1105 | 1127 |
line.putback(c); |
| 1106 | 1128 |
} |
| 1107 | 1129 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1108 | 1130 |
it != _attributes.end(); ++it) {
|
| 1109 | 1131 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1110 | 1132 |
std::ostringstream msg; |
| 1111 | 1133 |
msg << "Attribute not found: " << it->first; |
| 1112 | 1134 |
throw FormatError(msg.str()); |
| 1113 | 1135 |
} |
| 1114 | 1136 |
} |
| 1115 | 1137 |
} |
| 1116 | 1138 |
|
| 1117 | 1139 |
public: |
| 1118 | 1140 |
|
| 1119 | 1141 |
/// \name Execution of the reader |
| 1120 | 1142 |
/// @{
|
| 1121 | 1143 |
|
| 1122 | 1144 |
/// \brief Start the batch processing |
| 1123 | 1145 |
/// |
| 1124 | 1146 |
/// This function starts the batch processing |
| 1125 | 1147 |
void run() {
|
| 1126 | 1148 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 1127 | 1149 |
|
| 1128 | 1150 |
bool nodes_done = _skip_nodes; |
| 1129 | 1151 |
bool arcs_done = _skip_arcs; |
| 1130 | 1152 |
bool attributes_done = false; |
| 1131 | 1153 |
|
| 1132 | 1154 |
line_num = 0; |
| 1133 | 1155 |
readLine(); |
| 1134 | 1156 |
skipSection(); |
| 1135 | 1157 |
|
| 1136 | 1158 |
while (readSuccess()) {
|
| 1137 | 1159 |
try {
|
| 1138 | 1160 |
char c; |
| 1139 | 1161 |
std::string section, caption; |
| 1140 | 1162 |
line >> c; |
| 1141 | 1163 |
_reader_bits::readToken(line, section); |
| 1142 | 1164 |
_reader_bits::readToken(line, caption); |
| 1143 | 1165 |
|
| 1144 | 1166 |
if (line >> c) |
| 1145 | 1167 |
throw FormatError("Extra character at the end of line");
|
| 1146 | 1168 |
|
| 1147 | 1169 |
if (section == "nodes" && !nodes_done) {
|
| 1148 | 1170 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 1149 | 1171 |
readNodes(); |
| 1150 | 1172 |
nodes_done = true; |
| 1151 | 1173 |
} |
| 1152 | 1174 |
} else if ((section == "arcs" || section == "edges") && |
| 1153 | 1175 |
!arcs_done) {
|
| 1154 | 1176 |
if (_arcs_caption.empty() || _arcs_caption == caption) {
|
| 1155 | 1177 |
readArcs(); |
| 1156 | 1178 |
arcs_done = true; |
| 1157 | 1179 |
} |
| 1158 | 1180 |
} else if (section == "attributes" && !attributes_done) {
|
| 1159 | 1181 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 1160 | 1182 |
readAttributes(); |
| 1161 | 1183 |
attributes_done = true; |
| 1162 | 1184 |
} |
| 1163 | 1185 |
} else {
|
| 1164 | 1186 |
readLine(); |
| 1165 | 1187 |
skipSection(); |
| 1166 | 1188 |
} |
| 1167 | 1189 |
} catch (FormatError& error) {
|
| 1168 | 1190 |
error.line(line_num); |
| 1169 | 1191 |
error.file(_filename); |
| 1170 | 1192 |
throw; |
| 1171 | 1193 |
} |
| 1172 | 1194 |
} |
| 1173 | 1195 |
|
| 1174 | 1196 |
if (!nodes_done) {
|
| 1175 | 1197 |
throw FormatError("Section @nodes not found");
|
| 1176 | 1198 |
} |
| 1177 | 1199 |
|
| 1178 | 1200 |
if (!arcs_done) {
|
| 1179 | 1201 |
throw FormatError("Section @arcs not found");
|
| 1180 | 1202 |
} |
| 1181 | 1203 |
|
| 1182 | 1204 |
if (!attributes_done && !_attributes.empty()) {
|
| 1183 | 1205 |
throw FormatError("Section @attributes not found");
|
| 1184 | 1206 |
} |
| 1185 | 1207 |
|
| 1186 | 1208 |
} |
| 1187 | 1209 |
|
| 1188 | 1210 |
/// @} |
| 1189 | 1211 |
|
| 1190 | 1212 |
}; |
| 1191 | 1213 |
|
| 1192 |
|
|
| 1214 |
template <typename Graph> |
|
| 1215 |
class GraphReader; |
|
| 1216 |
|
|
| 1217 |
/// \brief Return a \ref GraphReader class |
|
| 1193 | 1218 |
/// |
| 1194 |
/// This function just returns a \ref DigraphReader class. |
|
| 1195 |
/// \relates DigraphReader |
|
| 1196 |
template <typename Digraph> |
|
| 1197 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
|
| 1198 |
std::istream& is = std::cin) {
|
|
| 1199 |
DigraphReader<Digraph> tmp(digraph, is); |
|
| 1219 |
/// This function just returns a \ref GraphReader class. |
|
| 1220 |
/// \relates GraphReader |
|
| 1221 |
template <typename Graph> |
|
| 1222 |
GraphReader<Graph> graphReader(Graph& graph, std::istream& is = std::cin) {
|
|
| 1223 |
GraphReader<Graph> tmp(graph, is); |
|
| 1200 | 1224 |
return tmp; |
| 1201 | 1225 |
} |
| 1202 | 1226 |
|
| 1203 |
/// \brief Return a \ref |
|
| 1227 |
/// \brief Return a \ref GraphReader class |
|
| 1204 | 1228 |
/// |
| 1205 |
/// This function just returns a \ref DigraphReader class. |
|
| 1206 |
/// \relates DigraphReader |
|
| 1207 |
template <typename Digraph> |
|
| 1208 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
|
| 1209 |
const std::string& fn) {
|
|
| 1210 |
DigraphReader<Digraph> tmp(digraph, fn); |
|
| 1229 |
/// This function just returns a \ref GraphReader class. |
|
| 1230 |
/// \relates GraphReader |
|
| 1231 |
template <typename Graph> |
|
| 1232 |
GraphReader<Graph> graphReader(Graph& graph, const std::string& fn) {
|
|
| 1233 |
GraphReader<Graph> tmp(graph, fn); |
|
| 1211 | 1234 |
return tmp; |
| 1212 | 1235 |
} |
| 1213 | 1236 |
|
| 1214 |
/// \brief Return a \ref |
|
| 1237 |
/// \brief Return a \ref GraphReader class |
|
| 1215 | 1238 |
/// |
| 1216 |
/// This function just returns a \ref DigraphReader class. |
|
| 1217 |
/// \relates DigraphReader |
|
| 1218 |
template <typename Digraph> |
|
| 1219 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const char* fn) {
|
|
| 1220 |
|
|
| 1239 |
/// This function just returns a \ref GraphReader class. |
|
| 1240 |
/// \relates GraphReader |
|
| 1241 |
template <typename Graph> |
|
| 1242 |
GraphReader<Graph> graphReader(Graph& graph, const char* fn) {
|
|
| 1243 |
GraphReader<Graph> tmp(graph, fn); |
|
| 1221 | 1244 |
return tmp; |
| 1222 | 1245 |
} |
| 1223 | 1246 |
|
| 1224 |
template <typename Graph> |
|
| 1225 |
class GraphReader; |
|
| 1226 |
|
|
| 1227 |
template <typename Graph> |
|
| 1228 |
GraphReader<Graph> graphReader(Graph& graph, |
|
| 1229 |
std::istream& is = std::cin); |
|
| 1230 |
|
|
| 1231 |
template <typename Graph> |
|
| 1232 |
GraphReader<Graph> graphReader(Graph& graph, const std::string& fn); |
|
| 1233 |
|
|
| 1234 |
template <typename Graph> |
|
| 1235 |
GraphReader<Graph> graphReader(Graph& graph, const char *fn); |
|
| 1236 |
|
|
| 1237 | 1247 |
/// \ingroup lemon_io |
| 1238 | 1248 |
/// |
| 1239 | 1249 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs |
| 1240 | 1250 |
/// |
| 1241 | 1251 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 1242 | 1252 |
/// |
| 1243 | 1253 |
/// It can be used almost the same way as \c DigraphReader. |
| 1244 | 1254 |
/// The only difference is that this class can handle edges and |
| 1245 | 1255 |
/// edge maps as well as arcs and arc maps. |
| 1246 | 1256 |
/// |
| 1247 | 1257 |
/// The columns in the \c \@edges (or \c \@arcs) section are the |
| 1248 | 1258 |
/// edge maps. However, if there are two maps with the same name |
| 1249 | 1259 |
/// prefixed with \c '+' and \c '-', then these can be read into an |
| 1250 | 1260 |
/// arc map. Similarly, an attribute can be read into an arc, if |
| 1251 | 1261 |
/// it's value is an edge label prefixed with \c '+' or \c '-'. |
| 1252 | 1262 |
template <typename _Graph> |
| 1253 | 1263 |
class GraphReader {
|
| 1254 | 1264 |
public: |
| 1255 | 1265 |
|
| 1256 | 1266 |
typedef _Graph Graph; |
| 1257 | 1267 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 1258 | 1268 |
|
| 1259 | 1269 |
private: |
| 1260 | 1270 |
|
| 1261 | 1271 |
std::istream* _is; |
| 1262 | 1272 |
bool local_is; |
| 1263 | 1273 |
std::string _filename; |
| 1264 | 1274 |
|
| 1265 | 1275 |
Graph& _graph; |
| 1266 | 1276 |
|
| 1267 | 1277 |
std::string _nodes_caption; |
| 1268 | 1278 |
std::string _edges_caption; |
| 1269 | 1279 |
std::string _attributes_caption; |
| 1270 | 1280 |
|
| 1271 | 1281 |
typedef std::map<std::string, Node> NodeIndex; |
| 1272 | 1282 |
NodeIndex _node_index; |
| 1273 | 1283 |
typedef std::map<std::string, Edge> EdgeIndex; |
| 1274 | 1284 |
EdgeIndex _edge_index; |
| 1275 | 1285 |
|
| 1276 | 1286 |
typedef std::vector<std::pair<std::string, |
| 1277 | 1287 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 1278 | 1288 |
NodeMaps _node_maps; |
| 1279 | 1289 |
|
| 1280 | 1290 |
typedef std::vector<std::pair<std::string, |
| 1281 | 1291 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
| 1282 | 1292 |
EdgeMaps _edge_maps; |
| 1283 | 1293 |
|
| 1284 | 1294 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 1285 | 1295 |
Attributes; |
| 1286 | 1296 |
Attributes _attributes; |
| 1287 | 1297 |
|
| 1288 | 1298 |
bool _use_nodes; |
| 1289 | 1299 |
bool _use_edges; |
| 1290 | 1300 |
|
| 1291 | 1301 |
bool _skip_nodes; |
| 1292 | 1302 |
bool _skip_edges; |
| 1293 | 1303 |
|
| 1294 | 1304 |
int line_num; |
| 1295 | 1305 |
std::istringstream line; |
| 1296 | 1306 |
|
| 1297 | 1307 |
public: |
| 1298 | 1308 |
|
| 1299 | 1309 |
/// \brief Constructor |
| 1300 | 1310 |
/// |
| 1301 | 1311 |
/// Construct an undirected graph reader, which reads from the given |
| 1302 | 1312 |
/// input stream. |
| 1303 | 1313 |
GraphReader(Graph& graph, std::istream& is = std::cin) |
| 1304 | 1314 |
: _is(&is), local_is(false), _graph(graph), |
| 1305 | 1315 |
_use_nodes(false), _use_edges(false), |
| 1306 | 1316 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1307 | 1317 |
|
| 1308 | 1318 |
/// \brief Constructor |
| 1309 | 1319 |
/// |
| 1310 | 1320 |
/// Construct an undirected graph reader, which reads from the given |
| 1311 | 1321 |
/// file. |
| 1312 | 1322 |
GraphReader(Graph& graph, const std::string& fn) |
| 1313 | 1323 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
| 1314 | 1324 |
_filename(fn), _graph(graph), |
| 1315 | 1325 |
_use_nodes(false), _use_edges(false), |
| 1316 | 1326 |
_skip_nodes(false), _skip_edges(false) {
|
| 1317 | 1327 |
if (!(*_is)) {
|
| 1318 | 1328 |
delete _is; |
| 1319 | 1329 |
throw IoError("Cannot open file", fn);
|
| 1320 | 1330 |
} |
| 1321 | 1331 |
} |
| 1322 | 1332 |
|
| 1323 | 1333 |
/// \brief Constructor |
| 1324 | 1334 |
/// |
| 1325 | 1335 |
/// Construct an undirected graph reader, which reads from the given |
| 1326 | 1336 |
/// file. |
| 1327 | 1337 |
GraphReader(Graph& graph, const char* fn) |
| 1328 | 1338 |
: _is(new std::ifstream(fn)), local_is(true), |
| 1329 | 1339 |
_filename(fn), _graph(graph), |
| 1330 | 1340 |
_use_nodes(false), _use_edges(false), |
| 1331 | 1341 |
_skip_nodes(false), _skip_edges(false) {
|
| 1332 | 1342 |
if (!(*_is)) {
|
| ... | ... |
@@ -1938,222 +1948,192 @@ |
| 1938 | 1948 |
while (it != _attributes.end() && it->first == attr) {
|
| 1939 | 1949 |
it->second->set(token); |
| 1940 | 1950 |
++it; |
| 1941 | 1951 |
} |
| 1942 | 1952 |
} |
| 1943 | 1953 |
|
| 1944 | 1954 |
} |
| 1945 | 1955 |
if (readSuccess()) {
|
| 1946 | 1956 |
line.putback(c); |
| 1947 | 1957 |
} |
| 1948 | 1958 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1949 | 1959 |
it != _attributes.end(); ++it) {
|
| 1950 | 1960 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1951 | 1961 |
std::ostringstream msg; |
| 1952 | 1962 |
msg << "Attribute not found: " << it->first; |
| 1953 | 1963 |
throw FormatError(msg.str()); |
| 1954 | 1964 |
} |
| 1955 | 1965 |
} |
| 1956 | 1966 |
} |
| 1957 | 1967 |
|
| 1958 | 1968 |
public: |
| 1959 | 1969 |
|
| 1960 | 1970 |
/// \name Execution of the reader |
| 1961 | 1971 |
/// @{
|
| 1962 | 1972 |
|
| 1963 | 1973 |
/// \brief Start the batch processing |
| 1964 | 1974 |
/// |
| 1965 | 1975 |
/// This function starts the batch processing |
| 1966 | 1976 |
void run() {
|
| 1967 | 1977 |
|
| 1968 | 1978 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 1969 | 1979 |
|
| 1970 | 1980 |
bool nodes_done = _skip_nodes; |
| 1971 | 1981 |
bool edges_done = _skip_edges; |
| 1972 | 1982 |
bool attributes_done = false; |
| 1973 | 1983 |
|
| 1974 | 1984 |
line_num = 0; |
| 1975 | 1985 |
readLine(); |
| 1976 | 1986 |
skipSection(); |
| 1977 | 1987 |
|
| 1978 | 1988 |
while (readSuccess()) {
|
| 1979 | 1989 |
try {
|
| 1980 | 1990 |
char c; |
| 1981 | 1991 |
std::string section, caption; |
| 1982 | 1992 |
line >> c; |
| 1983 | 1993 |
_reader_bits::readToken(line, section); |
| 1984 | 1994 |
_reader_bits::readToken(line, caption); |
| 1985 | 1995 |
|
| 1986 | 1996 |
if (line >> c) |
| 1987 | 1997 |
throw FormatError("Extra character at the end of line");
|
| 1988 | 1998 |
|
| 1989 | 1999 |
if (section == "nodes" && !nodes_done) {
|
| 1990 | 2000 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 1991 | 2001 |
readNodes(); |
| 1992 | 2002 |
nodes_done = true; |
| 1993 | 2003 |
} |
| 1994 | 2004 |
} else if ((section == "edges" || section == "arcs") && |
| 1995 | 2005 |
!edges_done) {
|
| 1996 | 2006 |
if (_edges_caption.empty() || _edges_caption == caption) {
|
| 1997 | 2007 |
readEdges(); |
| 1998 | 2008 |
edges_done = true; |
| 1999 | 2009 |
} |
| 2000 | 2010 |
} else if (section == "attributes" && !attributes_done) {
|
| 2001 | 2011 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 2002 | 2012 |
readAttributes(); |
| 2003 | 2013 |
attributes_done = true; |
| 2004 | 2014 |
} |
| 2005 | 2015 |
} else {
|
| 2006 | 2016 |
readLine(); |
| 2007 | 2017 |
skipSection(); |
| 2008 | 2018 |
} |
| 2009 | 2019 |
} catch (FormatError& error) {
|
| 2010 | 2020 |
error.line(line_num); |
| 2011 | 2021 |
error.file(_filename); |
| 2012 | 2022 |
throw; |
| 2013 | 2023 |
} |
| 2014 | 2024 |
} |
| 2015 | 2025 |
|
| 2016 | 2026 |
if (!nodes_done) {
|
| 2017 | 2027 |
throw FormatError("Section @nodes not found");
|
| 2018 | 2028 |
} |
| 2019 | 2029 |
|
| 2020 | 2030 |
if (!edges_done) {
|
| 2021 | 2031 |
throw FormatError("Section @edges not found");
|
| 2022 | 2032 |
} |
| 2023 | 2033 |
|
| 2024 | 2034 |
if (!attributes_done && !_attributes.empty()) {
|
| 2025 | 2035 |
throw FormatError("Section @attributes not found");
|
| 2026 | 2036 |
} |
| 2027 | 2037 |
|
| 2028 | 2038 |
} |
| 2029 | 2039 |
|
| 2030 | 2040 |
/// @} |
| 2031 | 2041 |
|
| 2032 | 2042 |
}; |
| 2033 | 2043 |
|
| 2034 |
/// \brief Return a \ref GraphReader class |
|
| 2035 |
/// |
|
| 2036 |
/// This function just returns a \ref GraphReader class. |
|
| 2037 |
/// \relates GraphReader |
|
| 2038 |
template <typename Graph> |
|
| 2039 |
GraphReader<Graph> graphReader(Graph& graph, std::istream& is = std::cin) {
|
|
| 2040 |
GraphReader<Graph> tmp(graph, is); |
|
| 2041 |
return tmp; |
|
| 2042 |
} |
|
| 2043 |
|
|
| 2044 |
/// \brief Return a \ref GraphReader class |
|
| 2045 |
/// |
|
| 2046 |
/// This function just returns a \ref GraphReader class. |
|
| 2047 |
/// \relates GraphReader |
|
| 2048 |
template <typename Graph> |
|
| 2049 |
GraphReader<Graph> graphReader(Graph& graph, const std::string& fn) {
|
|
| 2050 |
GraphReader<Graph> tmp(graph, fn); |
|
| 2051 |
return tmp; |
|
| 2052 |
} |
|
| 2053 |
|
|
| 2054 |
/// \brief Return a \ref GraphReader class |
|
| 2055 |
/// |
|
| 2056 |
/// This function just returns a \ref GraphReader class. |
|
| 2057 |
/// \relates GraphReader |
|
| 2058 |
template <typename Graph> |
|
| 2059 |
GraphReader<Graph> graphReader(Graph& graph, const char* fn) {
|
|
| 2060 |
GraphReader<Graph> tmp(graph, fn); |
|
| 2061 |
return tmp; |
|
| 2062 |
} |
|
| 2063 |
|
|
| 2064 | 2044 |
class SectionReader; |
| 2065 | 2045 |
|
| 2066 | 2046 |
SectionReader sectionReader(std::istream& is); |
| 2067 | 2047 |
SectionReader sectionReader(const std::string& fn); |
| 2068 | 2048 |
SectionReader sectionReader(const char* fn); |
| 2069 | 2049 |
|
| 2070 | 2050 |
/// \ingroup lemon_io |
| 2071 | 2051 |
/// |
| 2072 | 2052 |
/// \brief Section reader class |
| 2073 | 2053 |
/// |
| 2074 | 2054 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
| 2075 | 2055 |
/// which contain any data in arbitrary format. Such sections can be |
| 2076 | 2056 |
/// read with this class. A reading rule can be added to the class |
| 2077 | 2057 |
/// with two different functions. With the \c sectionLines() function a |
| 2078 | 2058 |
/// functor can process the section line-by-line, while with the \c |
| 2079 | 2059 |
/// sectionStream() member the section can be read from an input |
| 2080 | 2060 |
/// stream. |
| 2081 | 2061 |
class SectionReader {
|
| 2082 | 2062 |
private: |
| 2083 | 2063 |
|
| 2084 | 2064 |
std::istream* _is; |
| 2085 | 2065 |
bool local_is; |
| 2086 | 2066 |
std::string _filename; |
| 2087 | 2067 |
|
| 2088 | 2068 |
typedef std::map<std::string, _reader_bits::Section*> Sections; |
| 2089 | 2069 |
Sections _sections; |
| 2090 | 2070 |
|
| 2091 | 2071 |
int line_num; |
| 2092 | 2072 |
std::istringstream line; |
| 2093 | 2073 |
|
| 2094 | 2074 |
public: |
| 2095 | 2075 |
|
| 2096 | 2076 |
/// \brief Constructor |
| 2097 | 2077 |
/// |
| 2098 | 2078 |
/// Construct a section reader, which reads from the given input |
| 2099 | 2079 |
/// stream. |
| 2100 | 2080 |
SectionReader(std::istream& is) |
| 2101 | 2081 |
: _is(&is), local_is(false) {}
|
| 2102 | 2082 |
|
| 2103 | 2083 |
/// \brief Constructor |
| 2104 | 2084 |
/// |
| 2105 | 2085 |
/// Construct a section reader, which reads from the given file. |
| 2106 | 2086 |
SectionReader(const std::string& fn) |
| 2107 | 2087 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
| 2108 | 2088 |
_filename(fn) {
|
| 2109 | 2089 |
if (!(*_is)) {
|
| 2110 | 2090 |
delete _is; |
| 2111 | 2091 |
throw IoError("Cannot open file", fn);
|
| 2112 | 2092 |
} |
| 2113 | 2093 |
} |
| 2114 | 2094 |
|
| 2115 | 2095 |
/// \brief Constructor |
| 2116 | 2096 |
/// |
| 2117 | 2097 |
/// Construct a section reader, which reads from the given file. |
| 2118 | 2098 |
SectionReader(const char* fn) |
| 2119 | 2099 |
: _is(new std::ifstream(fn)), local_is(true), |
| 2120 | 2100 |
_filename(fn) {
|
| 2121 | 2101 |
if (!(*_is)) {
|
| 2122 | 2102 |
delete _is; |
| 2123 | 2103 |
throw IoError("Cannot open file", fn);
|
| 2124 | 2104 |
} |
| 2125 | 2105 |
} |
| 2126 | 2106 |
|
| 2127 | 2107 |
/// \brief Destructor |
| 2128 | 2108 |
~SectionReader() {
|
| 2129 | 2109 |
for (Sections::iterator it = _sections.begin(); |
| 2130 | 2110 |
it != _sections.end(); ++it) {
|
| 2131 | 2111 |
delete it->second; |
| 2132 | 2112 |
} |
| 2133 | 2113 |
|
| 2134 | 2114 |
if (local_is) {
|
| 2135 | 2115 |
delete _is; |
| 2136 | 2116 |
} |
| 2137 | 2117 |
|
| 2138 | 2118 |
} |
| 2139 | 2119 |
|
| 2140 | 2120 |
private: |
| 2141 | 2121 |
|
| 2142 | 2122 |
friend SectionReader sectionReader(std::istream& is); |
| 2143 | 2123 |
friend SectionReader sectionReader(const std::string& fn); |
| 2144 | 2124 |
friend SectionReader sectionReader(const char* fn); |
| 2145 | 2125 |
|
| 2146 | 2126 |
SectionReader(SectionReader& other) |
| 2147 | 2127 |
: _is(other._is), local_is(other.local_is) {
|
| 2148 | 2128 |
|
| 2149 | 2129 |
other._is = 0; |
| 2150 | 2130 |
other.local_is = false; |
| 2151 | 2131 |
|
| 2152 | 2132 |
_sections.swap(other._sections); |
| 2153 | 2133 |
} |
| 2154 | 2134 |
|
| 2155 | 2135 |
SectionReader& operator=(const SectionReader&); |
| 2156 | 2136 |
|
| 2157 | 2137 |
public: |
| 2158 | 2138 |
|
| 2159 | 2139 |
/// \name Section readers |
| ... | ... |
@@ -258,203 +258,224 @@ |
| 258 | 258 |
return; |
| 259 | 259 |
case '\r': |
| 260 | 260 |
os << "\\r"; |
| 261 | 261 |
return; |
| 262 | 262 |
case '\n': |
| 263 | 263 |
os << "\\n"; |
| 264 | 264 |
return; |
| 265 | 265 |
case '\t': |
| 266 | 266 |
os << "\\t"; |
| 267 | 267 |
return; |
| 268 | 268 |
case '\v': |
| 269 | 269 |
os << "\\v"; |
| 270 | 270 |
return; |
| 271 | 271 |
default: |
| 272 | 272 |
if (c < 0x20) {
|
| 273 | 273 |
std::ios::fmtflags flags = os.flags(); |
| 274 | 274 |
os << '\\' << std::oct << static_cast<int>(c); |
| 275 | 275 |
os.flags(flags); |
| 276 | 276 |
} else {
|
| 277 | 277 |
os << c; |
| 278 | 278 |
} |
| 279 | 279 |
return; |
| 280 | 280 |
} |
| 281 | 281 |
} |
| 282 | 282 |
|
| 283 | 283 |
inline bool requireEscape(const std::string& str) {
|
| 284 | 284 |
if (str.empty() || str[0] == '@') return true; |
| 285 | 285 |
std::istringstream is(str); |
| 286 | 286 |
char c; |
| 287 | 287 |
while (is.get(c)) {
|
| 288 | 288 |
if (isWhiteSpace(c) || isEscaped(c)) {
|
| 289 | 289 |
return true; |
| 290 | 290 |
} |
| 291 | 291 |
} |
| 292 | 292 |
return false; |
| 293 | 293 |
} |
| 294 | 294 |
|
| 295 | 295 |
inline std::ostream& writeToken(std::ostream& os, const std::string& str) {
|
| 296 | 296 |
|
| 297 | 297 |
if (requireEscape(str)) {
|
| 298 | 298 |
os << '\"'; |
| 299 | 299 |
for (std::string::const_iterator it = str.begin(); |
| 300 | 300 |
it != str.end(); ++it) {
|
| 301 | 301 |
writeEscape(os, *it); |
| 302 | 302 |
} |
| 303 | 303 |
os << '\"'; |
| 304 | 304 |
} else {
|
| 305 | 305 |
os << str; |
| 306 | 306 |
} |
| 307 | 307 |
return os; |
| 308 | 308 |
} |
| 309 | 309 |
|
| 310 | 310 |
class Section {
|
| 311 | 311 |
public: |
| 312 | 312 |
virtual ~Section() {}
|
| 313 | 313 |
virtual void process(std::ostream& os) = 0; |
| 314 | 314 |
}; |
| 315 | 315 |
|
| 316 | 316 |
template <typename Functor> |
| 317 | 317 |
class LineSection : public Section {
|
| 318 | 318 |
private: |
| 319 | 319 |
|
| 320 | 320 |
Functor _functor; |
| 321 | 321 |
|
| 322 | 322 |
public: |
| 323 | 323 |
|
| 324 | 324 |
LineSection(const Functor& functor) : _functor(functor) {}
|
| 325 | 325 |
virtual ~LineSection() {}
|
| 326 | 326 |
|
| 327 | 327 |
virtual void process(std::ostream& os) {
|
| 328 | 328 |
std::string line; |
| 329 | 329 |
while (!(line = _functor()).empty()) os << line << std::endl; |
| 330 | 330 |
} |
| 331 | 331 |
}; |
| 332 | 332 |
|
| 333 | 333 |
template <typename Functor> |
| 334 | 334 |
class StreamSection : public Section {
|
| 335 | 335 |
private: |
| 336 | 336 |
|
| 337 | 337 |
Functor _functor; |
| 338 | 338 |
|
| 339 | 339 |
public: |
| 340 | 340 |
|
| 341 | 341 |
StreamSection(const Functor& functor) : _functor(functor) {}
|
| 342 | 342 |
virtual ~StreamSection() {}
|
| 343 | 343 |
|
| 344 | 344 |
virtual void process(std::ostream& os) {
|
| 345 | 345 |
_functor(os); |
| 346 | 346 |
} |
| 347 | 347 |
}; |
| 348 | 348 |
|
| 349 | 349 |
} |
| 350 | 350 |
|
| 351 | 351 |
template <typename Digraph> |
| 352 | 352 |
class DigraphWriter; |
| 353 | 353 |
|
| 354 |
/// \brief Return a \ref DigraphWriter class |
|
| 355 |
/// |
|
| 356 |
/// This function just returns a \ref DigraphWriter class. |
|
| 357 |
/// \relates DigraphWriter |
|
| 354 | 358 |
template <typename Digraph> |
| 355 | 359 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 356 |
std::ostream& os = std::cout) |
|
| 360 |
std::ostream& os = std::cout) {
|
|
| 361 |
DigraphWriter<Digraph> tmp(digraph, os); |
|
| 362 |
return tmp; |
|
| 363 |
} |
|
| 357 | 364 |
|
| 365 |
/// \brief Return a \ref DigraphWriter class |
|
| 366 |
/// |
|
| 367 |
/// This function just returns a \ref DigraphWriter class. |
|
| 368 |
/// \relates DigraphWriter |
|
| 358 | 369 |
template <typename Digraph> |
| 359 | 370 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 360 |
const std::string& fn) |
|
| 371 |
const std::string& fn) {
|
|
| 372 |
DigraphWriter<Digraph> tmp(digraph, fn); |
|
| 373 |
return tmp; |
|
| 374 |
} |
|
| 361 | 375 |
|
| 376 |
/// \brief Return a \ref DigraphWriter class |
|
| 377 |
/// |
|
| 378 |
/// This function just returns a \ref DigraphWriter class. |
|
| 379 |
/// \relates DigraphWriter |
|
| 362 | 380 |
template <typename Digraph> |
| 363 | 381 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 364 |
const char |
|
| 382 |
const char* fn) {
|
|
| 383 |
DigraphWriter<Digraph> tmp(digraph, fn); |
|
| 384 |
return tmp; |
|
| 385 |
} |
|
| 365 | 386 |
|
| 366 | 387 |
/// \ingroup lemon_io |
| 367 | 388 |
/// |
| 368 | 389 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
| 369 | 390 |
/// |
| 370 | 391 |
/// This utility writes an \ref lgf-format "LGF" file. |
| 371 | 392 |
/// |
| 372 | 393 |
/// The writing method does a batch processing. The user creates a |
| 373 | 394 |
/// writer object, then various writing rules can be added to the |
| 374 | 395 |
/// writer, and eventually the writing is executed with the \c run() |
| 375 | 396 |
/// member function. A map writing rule can be added to the writer |
| 376 | 397 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
| 377 | 398 |
/// converter parameter can also be added as a standard functor |
| 378 | 399 |
/// converting from the value type of the map to \c std::string. If it |
| 379 | 400 |
/// is set, it will determine how the value type of the map is written to |
| 380 | 401 |
/// the output stream. If the functor is not set, then a default |
| 381 | 402 |
/// conversion will be used. The \c attribute(), \c node() and \c |
| 382 | 403 |
/// arc() functions are used to add attribute writing rules. |
| 383 | 404 |
/// |
| 384 | 405 |
///\code |
| 385 | 406 |
/// DigraphWriter<Digraph>(digraph, std::cout). |
| 386 | 407 |
/// nodeMap("coordinates", coord_map).
|
| 387 | 408 |
/// nodeMap("size", size).
|
| 388 | 409 |
/// nodeMap("title", title).
|
| 389 | 410 |
/// arcMap("capacity", cap_map).
|
| 390 | 411 |
/// node("source", src).
|
| 391 | 412 |
/// node("target", trg).
|
| 392 | 413 |
/// attribute("caption", caption).
|
| 393 | 414 |
/// run(); |
| 394 | 415 |
///\endcode |
| 395 | 416 |
/// |
| 396 | 417 |
/// |
| 397 | 418 |
/// By default, the writer does not write additional captions to the |
| 398 | 419 |
/// sections, but they can be give as an optional parameter of |
| 399 | 420 |
/// the \c nodes(), \c arcs() or \c |
| 400 | 421 |
/// attributes() functions. |
| 401 | 422 |
/// |
| 402 | 423 |
/// The \c skipNodes() and \c skipArcs() functions forbid the |
| 403 | 424 |
/// writing of the sections. If two arc sections should be written |
| 404 | 425 |
/// to the output, it can be done in two passes, the first pass |
| 405 | 426 |
/// writes the node section and the first arc section, then the |
| 406 | 427 |
/// second pass skips the node section and writes just the arc |
| 407 | 428 |
/// section to the stream. The output stream can be retrieved with |
| 408 | 429 |
/// the \c ostream() function, hence the second pass can append its |
| 409 | 430 |
/// output to the output of the first pass. |
| 410 | 431 |
template <typename _Digraph> |
| 411 | 432 |
class DigraphWriter {
|
| 412 | 433 |
public: |
| 413 | 434 |
|
| 414 | 435 |
typedef _Digraph Digraph; |
| 415 | 436 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 416 | 437 |
|
| 417 | 438 |
private: |
| 418 | 439 |
|
| 419 | 440 |
|
| 420 | 441 |
std::ostream* _os; |
| 421 | 442 |
bool local_os; |
| 422 | 443 |
|
| 423 | 444 |
const Digraph& _digraph; |
| 424 | 445 |
|
| 425 | 446 |
std::string _nodes_caption; |
| 426 | 447 |
std::string _arcs_caption; |
| 427 | 448 |
std::string _attributes_caption; |
| 428 | 449 |
|
| 429 | 450 |
typedef std::map<Node, std::string> NodeIndex; |
| 430 | 451 |
NodeIndex _node_index; |
| 431 | 452 |
typedef std::map<Arc, std::string> ArcIndex; |
| 432 | 453 |
ArcIndex _arc_index; |
| 433 | 454 |
|
| 434 | 455 |
typedef std::vector<std::pair<std::string, |
| 435 | 456 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
| 436 | 457 |
NodeMaps _node_maps; |
| 437 | 458 |
|
| 438 | 459 |
typedef std::vector<std::pair<std::string, |
| 439 | 460 |
_writer_bits::MapStorageBase<Arc>* > >ArcMaps; |
| 440 | 461 |
ArcMaps _arc_maps; |
| 441 | 462 |
|
| 442 | 463 |
typedef std::vector<std::pair<std::string, |
| 443 | 464 |
_writer_bits::ValueStorageBase*> > Attributes; |
| 444 | 465 |
Attributes _attributes; |
| 445 | 466 |
|
| 446 | 467 |
bool _skip_nodes; |
| 447 | 468 |
bool _skip_arcs; |
| 448 | 469 |
|
| 449 | 470 |
public: |
| 450 | 471 |
|
| 451 | 472 |
/// \brief Constructor |
| 452 | 473 |
/// |
| 453 | 474 |
/// Construct a directed graph writer, which writes to the given |
| 454 | 475 |
/// output stream. |
| 455 | 476 |
DigraphWriter(const Digraph& digraph, std::ostream& os = std::cout) |
| 456 | 477 |
: _os(&os), local_os(false), _digraph(digraph), |
| 457 | 478 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 458 | 479 |
|
| 459 | 480 |
/// \brief Constructor |
| 460 | 481 |
/// |
| ... | ... |
@@ -820,238 +841,226 @@ |
| 820 | 841 |
find(_digraph.source(a))->second); |
| 821 | 842 |
*_os << '\t'; |
| 822 | 843 |
_writer_bits::writeToken(*_os, _node_index. |
| 823 | 844 |
find(_digraph.target(a))->second); |
| 824 | 845 |
*_os << '\t'; |
| 825 | 846 |
if (label == 0) {
|
| 826 | 847 |
std::ostringstream os; |
| 827 | 848 |
os << _digraph.id(a); |
| 828 | 849 |
_writer_bits::writeToken(*_os, os.str()); |
| 829 | 850 |
*_os << '\t'; |
| 830 | 851 |
_arc_index.insert(std::make_pair(a, os.str())); |
| 831 | 852 |
} |
| 832 | 853 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 833 | 854 |
it != _arc_maps.end(); ++it) {
|
| 834 | 855 |
std::string value = it->second->get(a); |
| 835 | 856 |
_writer_bits::writeToken(*_os, value); |
| 836 | 857 |
if (it->first == "label") {
|
| 837 | 858 |
_arc_index.insert(std::make_pair(a, value)); |
| 838 | 859 |
} |
| 839 | 860 |
*_os << '\t'; |
| 840 | 861 |
} |
| 841 | 862 |
*_os << std::endl; |
| 842 | 863 |
} |
| 843 | 864 |
} |
| 844 | 865 |
|
| 845 | 866 |
void createArcIndex() {
|
| 846 | 867 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
| 847 | 868 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 848 | 869 |
it != _arc_maps.end(); ++it) {
|
| 849 | 870 |
if (it->first == "label") {
|
| 850 | 871 |
label = it->second; |
| 851 | 872 |
break; |
| 852 | 873 |
} |
| 853 | 874 |
} |
| 854 | 875 |
|
| 855 | 876 |
if (label == 0) {
|
| 856 | 877 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 857 | 878 |
std::ostringstream os; |
| 858 | 879 |
os << _digraph.id(a); |
| 859 | 880 |
_arc_index.insert(std::make_pair(a, os.str())); |
| 860 | 881 |
} |
| 861 | 882 |
} else {
|
| 862 | 883 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 863 | 884 |
std::string value = label->get(a); |
| 864 | 885 |
_arc_index.insert(std::make_pair(a, value)); |
| 865 | 886 |
} |
| 866 | 887 |
} |
| 867 | 888 |
} |
| 868 | 889 |
|
| 869 | 890 |
void writeAttributes() {
|
| 870 | 891 |
if (_attributes.empty()) return; |
| 871 | 892 |
*_os << "@attributes"; |
| 872 | 893 |
if (!_attributes_caption.empty()) {
|
| 873 | 894 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
| 874 | 895 |
} |
| 875 | 896 |
*_os << std::endl; |
| 876 | 897 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 877 | 898 |
it != _attributes.end(); ++it) {
|
| 878 | 899 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
| 879 | 900 |
_writer_bits::writeToken(*_os, it->second->get()); |
| 880 | 901 |
*_os << std::endl; |
| 881 | 902 |
} |
| 882 | 903 |
} |
| 883 | 904 |
|
| 884 | 905 |
public: |
| 885 | 906 |
|
| 886 | 907 |
/// \name Execution of the writer |
| 887 | 908 |
/// @{
|
| 888 | 909 |
|
| 889 | 910 |
/// \brief Start the batch processing |
| 890 | 911 |
/// |
| 891 | 912 |
/// This function starts the batch processing. |
| 892 | 913 |
void run() {
|
| 893 | 914 |
if (!_skip_nodes) {
|
| 894 | 915 |
writeNodes(); |
| 895 | 916 |
} else {
|
| 896 | 917 |
createNodeIndex(); |
| 897 | 918 |
} |
| 898 | 919 |
if (!_skip_arcs) {
|
| 899 | 920 |
writeArcs(); |
| 900 | 921 |
} else {
|
| 901 | 922 |
createArcIndex(); |
| 902 | 923 |
} |
| 903 | 924 |
writeAttributes(); |
| 904 | 925 |
} |
| 905 | 926 |
|
| 906 | 927 |
/// \brief Give back the stream of the writer |
| 907 | 928 |
/// |
| 908 | 929 |
/// Give back the stream of the writer. |
| 909 | 930 |
std::ostream& ostream() {
|
| 910 | 931 |
return *_os; |
| 911 | 932 |
} |
| 912 | 933 |
|
| 913 | 934 |
/// @} |
| 914 | 935 |
}; |
| 915 | 936 |
|
| 916 |
|
|
| 937 |
template <typename Graph> |
|
| 938 |
class GraphWriter; |
|
| 939 |
|
|
| 940 |
/// \brief Return a \ref GraphWriter class |
|
| 917 | 941 |
/// |
| 918 |
/// This function just returns a \ref DigraphWriter class. |
|
| 919 |
/// \relates DigraphWriter |
|
| 920 |
template <typename Digraph> |
|
| 921 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
|
| 922 |
std::ostream& os = std::cout) {
|
|
| 923 |
DigraphWriter<Digraph> tmp(digraph, os); |
|
| 942 |
/// This function just returns a \ref GraphWriter class. |
|
| 943 |
/// \relates GraphWriter |
|
| 944 |
template <typename Graph> |
|
| 945 |
GraphWriter<Graph> graphWriter(const Graph& graph, |
|
| 946 |
std::ostream& os = std::cout) {
|
|
| 947 |
GraphWriter<Graph> tmp(graph, os); |
|
| 924 | 948 |
return tmp; |
| 925 | 949 |
} |
| 926 | 950 |
|
| 927 |
/// \brief Return a \ref |
|
| 951 |
/// \brief Return a \ref GraphWriter class |
|
| 928 | 952 |
/// |
| 929 |
/// This function just returns a \ref DigraphWriter class. |
|
| 930 |
/// \relates DigraphWriter |
|
| 931 |
template <typename Digraph> |
|
| 932 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
|
| 933 |
const std::string& fn) {
|
|
| 934 |
DigraphWriter<Digraph> tmp(digraph, fn); |
|
| 953 |
/// This function just returns a \ref GraphWriter class. |
|
| 954 |
/// \relates GraphWriter |
|
| 955 |
template <typename Graph> |
|
| 956 |
GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn) {
|
|
| 957 |
GraphWriter<Graph> tmp(graph, fn); |
|
| 935 | 958 |
return tmp; |
| 936 | 959 |
} |
| 937 | 960 |
|
| 938 |
/// \brief Return a \ref |
|
| 961 |
/// \brief Return a \ref GraphWriter class |
|
| 939 | 962 |
/// |
| 940 |
/// This function just returns a \ref DigraphWriter class. |
|
| 941 |
/// \relates DigraphWriter |
|
| 942 |
template <typename Digraph> |
|
| 943 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
|
| 944 |
const char* fn) {
|
|
| 945 |
DigraphWriter<Digraph> tmp(digraph, fn); |
|
| 963 |
/// This function just returns a \ref GraphWriter class. |
|
| 964 |
/// \relates GraphWriter |
|
| 965 |
template <typename Graph> |
|
| 966 |
GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) {
|
|
| 967 |
GraphWriter<Graph> tmp(graph, fn); |
|
| 946 | 968 |
return tmp; |
| 947 | 969 |
} |
| 948 | 970 |
|
| 949 |
template <typename Graph> |
|
| 950 |
class GraphWriter; |
|
| 951 |
|
|
| 952 |
template <typename Graph> |
|
| 953 |
GraphWriter<Graph> graphWriter(const Graph& graph, |
|
| 954 |
std::ostream& os = std::cout); |
|
| 955 |
|
|
| 956 |
template <typename Graph> |
|
| 957 |
GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn); |
|
| 958 |
|
|
| 959 |
template <typename Graph> |
|
| 960 |
GraphWriter<Graph> graphWriter(const Graph& graph, const char *fn); |
|
| 961 |
|
|
| 962 | 971 |
/// \ingroup lemon_io |
| 963 | 972 |
/// |
| 964 | 973 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
| 965 | 974 |
/// |
| 966 | 975 |
/// This utility writes an \ref lgf-format "LGF" file. |
| 967 | 976 |
/// |
| 968 | 977 |
/// It can be used almost the same way as \c DigraphWriter. |
| 969 | 978 |
/// The only difference is that this class can handle edges and |
| 970 | 979 |
/// edge maps as well as arcs and arc maps. |
| 971 | 980 |
/// |
| 972 | 981 |
/// The arc maps are written into the file as two columns, the |
| 973 | 982 |
/// caption of the columns are the name of the map prefixed with \c |
| 974 | 983 |
/// '+' and \c '-'. The arcs are written into the \c \@attributes |
| 975 | 984 |
/// section as a \c '+' or a \c '-' prefix (depends on the direction |
| 976 | 985 |
/// of the arc) and the label of corresponding edge. |
| 977 | 986 |
template <typename _Graph> |
| 978 | 987 |
class GraphWriter {
|
| 979 | 988 |
public: |
| 980 | 989 |
|
| 981 | 990 |
typedef _Graph Graph; |
| 982 | 991 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 983 | 992 |
|
| 984 | 993 |
private: |
| 985 | 994 |
|
| 986 | 995 |
|
| 987 | 996 |
std::ostream* _os; |
| 988 | 997 |
bool local_os; |
| 989 | 998 |
|
| 990 | 999 |
const Graph& _graph; |
| 991 | 1000 |
|
| 992 | 1001 |
std::string _nodes_caption; |
| 993 | 1002 |
std::string _edges_caption; |
| 994 | 1003 |
std::string _attributes_caption; |
| 995 | 1004 |
|
| 996 | 1005 |
typedef std::map<Node, std::string> NodeIndex; |
| 997 | 1006 |
NodeIndex _node_index; |
| 998 | 1007 |
typedef std::map<Edge, std::string> EdgeIndex; |
| 999 | 1008 |
EdgeIndex _edge_index; |
| 1000 | 1009 |
|
| 1001 | 1010 |
typedef std::vector<std::pair<std::string, |
| 1002 | 1011 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
| 1003 | 1012 |
NodeMaps _node_maps; |
| 1004 | 1013 |
|
| 1005 | 1014 |
typedef std::vector<std::pair<std::string, |
| 1006 | 1015 |
_writer_bits::MapStorageBase<Edge>* > >EdgeMaps; |
| 1007 | 1016 |
EdgeMaps _edge_maps; |
| 1008 | 1017 |
|
| 1009 | 1018 |
typedef std::vector<std::pair<std::string, |
| 1010 | 1019 |
_writer_bits::ValueStorageBase*> > Attributes; |
| 1011 | 1020 |
Attributes _attributes; |
| 1012 | 1021 |
|
| 1013 | 1022 |
bool _skip_nodes; |
| 1014 | 1023 |
bool _skip_edges; |
| 1015 | 1024 |
|
| 1016 | 1025 |
public: |
| 1017 | 1026 |
|
| 1018 | 1027 |
/// \brief Constructor |
| 1019 | 1028 |
/// |
| 1020 | 1029 |
/// Construct a directed graph writer, which writes to the given |
| 1021 | 1030 |
/// output stream. |
| 1022 | 1031 |
GraphWriter(const Graph& graph, std::ostream& os = std::cout) |
| 1023 | 1032 |
: _os(&os), local_os(false), _graph(graph), |
| 1024 | 1033 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1025 | 1034 |
|
| 1026 | 1035 |
/// \brief Constructor |
| 1027 | 1036 |
/// |
| 1028 | 1037 |
/// Construct a directed graph writer, which writes to the given |
| 1029 | 1038 |
/// output file. |
| 1030 | 1039 |
GraphWriter(const Graph& graph, const std::string& fn) |
| 1031 | 1040 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), |
| 1032 | 1041 |
_skip_nodes(false), _skip_edges(false) {
|
| 1033 | 1042 |
if (!(*_os)) {
|
| 1034 | 1043 |
delete _os; |
| 1035 | 1044 |
throw IoError("Cannot write file", fn);
|
| 1036 | 1045 |
} |
| 1037 | 1046 |
} |
| 1038 | 1047 |
|
| 1039 | 1048 |
/// \brief Constructor |
| 1040 | 1049 |
/// |
| 1041 | 1050 |
/// Construct a directed graph writer, which writes to the given |
| 1042 | 1051 |
/// output file. |
| 1043 | 1052 |
GraphWriter(const Graph& graph, const char* fn) |
| 1044 | 1053 |
: _os(new std::ofstream(fn)), local_os(true), _graph(graph), |
| 1045 | 1054 |
_skip_nodes(false), _skip_edges(false) {
|
| 1046 | 1055 |
if (!(*_os)) {
|
| 1047 | 1056 |
delete _os; |
| 1048 | 1057 |
throw IoError("Cannot write file", fn);
|
| 1049 | 1058 |
} |
| 1050 | 1059 |
} |
| 1051 | 1060 |
|
| 1052 | 1061 |
/// \brief Destructor |
| 1053 | 1062 |
~GraphWriter() {
|
| 1054 | 1063 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1055 | 1064 |
it != _node_maps.end(); ++it) {
|
| 1056 | 1065 |
delete it->second; |
| 1057 | 1066 |
} |
| ... | ... |
@@ -1433,223 +1442,192 @@ |
| 1433 | 1442 |
find(_graph.u(e))->second); |
| 1434 | 1443 |
*_os << '\t'; |
| 1435 | 1444 |
_writer_bits::writeToken(*_os, _node_index. |
| 1436 | 1445 |
find(_graph.v(e))->second); |
| 1437 | 1446 |
*_os << '\t'; |
| 1438 | 1447 |
if (label == 0) {
|
| 1439 | 1448 |
std::ostringstream os; |
| 1440 | 1449 |
os << _graph.id(e); |
| 1441 | 1450 |
_writer_bits::writeToken(*_os, os.str()); |
| 1442 | 1451 |
*_os << '\t'; |
| 1443 | 1452 |
_edge_index.insert(std::make_pair(e, os.str())); |
| 1444 | 1453 |
} |
| 1445 | 1454 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1446 | 1455 |
it != _edge_maps.end(); ++it) {
|
| 1447 | 1456 |
std::string value = it->second->get(e); |
| 1448 | 1457 |
_writer_bits::writeToken(*_os, value); |
| 1449 | 1458 |
if (it->first == "label") {
|
| 1450 | 1459 |
_edge_index.insert(std::make_pair(e, value)); |
| 1451 | 1460 |
} |
| 1452 | 1461 |
*_os << '\t'; |
| 1453 | 1462 |
} |
| 1454 | 1463 |
*_os << std::endl; |
| 1455 | 1464 |
} |
| 1456 | 1465 |
} |
| 1457 | 1466 |
|
| 1458 | 1467 |
void createEdgeIndex() {
|
| 1459 | 1468 |
_writer_bits::MapStorageBase<Edge>* label = 0; |
| 1460 | 1469 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1461 | 1470 |
it != _edge_maps.end(); ++it) {
|
| 1462 | 1471 |
if (it->first == "label") {
|
| 1463 | 1472 |
label = it->second; |
| 1464 | 1473 |
break; |
| 1465 | 1474 |
} |
| 1466 | 1475 |
} |
| 1467 | 1476 |
|
| 1468 | 1477 |
if (label == 0) {
|
| 1469 | 1478 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1470 | 1479 |
std::ostringstream os; |
| 1471 | 1480 |
os << _graph.id(e); |
| 1472 | 1481 |
_edge_index.insert(std::make_pair(e, os.str())); |
| 1473 | 1482 |
} |
| 1474 | 1483 |
} else {
|
| 1475 | 1484 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1476 | 1485 |
std::string value = label->get(e); |
| 1477 | 1486 |
_edge_index.insert(std::make_pair(e, value)); |
| 1478 | 1487 |
} |
| 1479 | 1488 |
} |
| 1480 | 1489 |
} |
| 1481 | 1490 |
|
| 1482 | 1491 |
void writeAttributes() {
|
| 1483 | 1492 |
if (_attributes.empty()) return; |
| 1484 | 1493 |
*_os << "@attributes"; |
| 1485 | 1494 |
if (!_attributes_caption.empty()) {
|
| 1486 | 1495 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
| 1487 | 1496 |
} |
| 1488 | 1497 |
*_os << std::endl; |
| 1489 | 1498 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1490 | 1499 |
it != _attributes.end(); ++it) {
|
| 1491 | 1500 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
| 1492 | 1501 |
_writer_bits::writeToken(*_os, it->second->get()); |
| 1493 | 1502 |
*_os << std::endl; |
| 1494 | 1503 |
} |
| 1495 | 1504 |
} |
| 1496 | 1505 |
|
| 1497 | 1506 |
public: |
| 1498 | 1507 |
|
| 1499 | 1508 |
/// \name Execution of the writer |
| 1500 | 1509 |
/// @{
|
| 1501 | 1510 |
|
| 1502 | 1511 |
/// \brief Start the batch processing |
| 1503 | 1512 |
/// |
| 1504 | 1513 |
/// This function starts the batch processing. |
| 1505 | 1514 |
void run() {
|
| 1506 | 1515 |
if (!_skip_nodes) {
|
| 1507 | 1516 |
writeNodes(); |
| 1508 | 1517 |
} else {
|
| 1509 | 1518 |
createNodeIndex(); |
| 1510 | 1519 |
} |
| 1511 | 1520 |
if (!_skip_edges) {
|
| 1512 | 1521 |
writeEdges(); |
| 1513 | 1522 |
} else {
|
| 1514 | 1523 |
createEdgeIndex(); |
| 1515 | 1524 |
} |
| 1516 | 1525 |
writeAttributes(); |
| 1517 | 1526 |
} |
| 1518 | 1527 |
|
| 1519 | 1528 |
/// \brief Give back the stream of the writer |
| 1520 | 1529 |
/// |
| 1521 | 1530 |
/// Give back the stream of the writer |
| 1522 | 1531 |
std::ostream& ostream() {
|
| 1523 | 1532 |
return *_os; |
| 1524 | 1533 |
} |
| 1525 | 1534 |
|
| 1526 | 1535 |
/// @} |
| 1527 | 1536 |
}; |
| 1528 | 1537 |
|
| 1529 |
/// \brief Return a \ref GraphWriter class |
|
| 1530 |
/// |
|
| 1531 |
/// This function just returns a \ref GraphWriter class. |
|
| 1532 |
/// \relates GraphWriter |
|
| 1533 |
template <typename Graph> |
|
| 1534 |
GraphWriter<Graph> graphWriter(const Graph& graph, |
|
| 1535 |
std::ostream& os = std::cout) {
|
|
| 1536 |
GraphWriter<Graph> tmp(graph, os); |
|
| 1537 |
return tmp; |
|
| 1538 |
} |
|
| 1539 |
|
|
| 1540 |
/// \brief Return a \ref GraphWriter class |
|
| 1541 |
/// |
|
| 1542 |
/// This function just returns a \ref GraphWriter class. |
|
| 1543 |
/// \relates GraphWriter |
|
| 1544 |
template <typename Graph> |
|
| 1545 |
GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn) {
|
|
| 1546 |
GraphWriter<Graph> tmp(graph, fn); |
|
| 1547 |
return tmp; |
|
| 1548 |
} |
|
| 1549 |
|
|
| 1550 |
/// \brief Return a \ref GraphWriter class |
|
| 1551 |
/// |
|
| 1552 |
/// This function just returns a \ref GraphWriter class. |
|
| 1553 |
/// \relates GraphWriter |
|
| 1554 |
template <typename Graph> |
|
| 1555 |
GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) {
|
|
| 1556 |
GraphWriter<Graph> tmp(graph, fn); |
|
| 1557 |
return tmp; |
|
| 1558 |
} |
|
| 1559 |
|
|
| 1560 | 1538 |
class SectionWriter; |
| 1561 | 1539 |
|
| 1562 | 1540 |
SectionWriter sectionWriter(std::istream& is); |
| 1563 | 1541 |
SectionWriter sectionWriter(const std::string& fn); |
| 1564 | 1542 |
SectionWriter sectionWriter(const char* fn); |
| 1565 | 1543 |
|
| 1566 | 1544 |
/// \ingroup lemon_io |
| 1567 | 1545 |
/// |
| 1568 | 1546 |
/// \brief Section writer class |
| 1569 | 1547 |
/// |
| 1570 | 1548 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
| 1571 | 1549 |
/// which contain any data in arbitrary format. Such sections can be |
| 1572 | 1550 |
/// written with this class. A writing rule can be added to the |
| 1573 | 1551 |
/// class with two different functions. With the \c sectionLines() |
| 1574 | 1552 |
/// function a generator can write the section line-by-line, while |
| 1575 | 1553 |
/// with the \c sectionStream() member the section can be written to |
| 1576 | 1554 |
/// an output stream. |
| 1577 | 1555 |
class SectionWriter {
|
| 1578 | 1556 |
private: |
| 1579 | 1557 |
|
| 1580 | 1558 |
std::ostream* _os; |
| 1581 | 1559 |
bool local_os; |
| 1582 | 1560 |
|
| 1583 | 1561 |
typedef std::vector<std::pair<std::string, _writer_bits::Section*> > |
| 1584 | 1562 |
Sections; |
| 1585 | 1563 |
|
| 1586 | 1564 |
Sections _sections; |
| 1587 | 1565 |
|
| 1588 | 1566 |
public: |
| 1589 | 1567 |
|
| 1590 | 1568 |
/// \brief Constructor |
| 1591 | 1569 |
/// |
| 1592 | 1570 |
/// Construct a section writer, which writes to the given output |
| 1593 | 1571 |
/// stream. |
| 1594 | 1572 |
SectionWriter(std::ostream& os) |
| 1595 | 1573 |
: _os(&os), local_os(false) {}
|
| 1596 | 1574 |
|
| 1597 | 1575 |
/// \brief Constructor |
| 1598 | 1576 |
/// |
| 1599 | 1577 |
/// Construct a section writer, which writes into the given file. |
| 1600 | 1578 |
SectionWriter(const std::string& fn) |
| 1601 | 1579 |
: _os(new std::ofstream(fn.c_str())), local_os(true) {
|
| 1602 | 1580 |
if (!(*_os)) {
|
| 1603 | 1581 |
delete _os; |
| 1604 | 1582 |
throw IoError("Cannot write file", fn);
|
| 1605 | 1583 |
} |
| 1606 | 1584 |
} |
| 1607 | 1585 |
|
| 1608 | 1586 |
/// \brief Constructor |
| 1609 | 1587 |
/// |
| 1610 | 1588 |
/// Construct a section writer, which writes into the given file. |
| 1611 | 1589 |
SectionWriter(const char* fn) |
| 1612 | 1590 |
: _os(new std::ofstream(fn)), local_os(true) {
|
| 1613 | 1591 |
if (!(*_os)) {
|
| 1614 | 1592 |
delete _os; |
| 1615 | 1593 |
throw IoError("Cannot write file", fn);
|
| 1616 | 1594 |
} |
| 1617 | 1595 |
} |
| 1618 | 1596 |
|
| 1619 | 1597 |
/// \brief Destructor |
| 1620 | 1598 |
~SectionWriter() {
|
| 1621 | 1599 |
for (Sections::iterator it = _sections.begin(); |
| 1622 | 1600 |
it != _sections.end(); ++it) {
|
| 1623 | 1601 |
delete it->second; |
| 1624 | 1602 |
} |
| 1625 | 1603 |
|
| 1626 | 1604 |
if (local_os) {
|
| 1627 | 1605 |
delete _os; |
| 1628 | 1606 |
} |
| 1629 | 1607 |
|
| 1630 | 1608 |
} |
| 1631 | 1609 |
|
| 1632 | 1610 |
private: |
| 1633 | 1611 |
|
| 1634 | 1612 |
friend SectionWriter sectionWriter(std::ostream& os); |
| 1635 | 1613 |
friend SectionWriter sectionWriter(const std::string& fn); |
| 1636 | 1614 |
friend SectionWriter sectionWriter(const char* fn); |
| 1637 | 1615 |
|
| 1638 | 1616 |
SectionWriter(SectionWriter& other) |
| 1639 | 1617 |
: _os(other._os), local_os(other.local_os) {
|
| 1640 | 1618 |
|
| 1641 | 1619 |
other._os = 0; |
| 1642 | 1620 |
other.local_os = false; |
| 1643 | 1621 |
|
| 1644 | 1622 |
_sections.swap(other._sections); |
| 1645 | 1623 |
} |
| 1646 | 1624 |
|
| 1647 | 1625 |
SectionWriter& operator=(const SectionWriter&); |
| 1648 | 1626 |
|
| 1649 | 1627 |
public: |
| 1650 | 1628 |
|
| 1651 | 1629 |
/// \name Section writers |
| 1652 | 1630 |
/// @{
|
| 1653 | 1631 |
|
| 1654 | 1632 |
/// \brief Add a section writer with line oriented writing |
| 1655 | 1633 |
/// |
| ... | ... |
@@ -320,193 +320,193 @@ |
| 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; |
| 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 |
/// Returns a \ref NullMap class |
|
| 77 |
|
|
| 78 |
/// |
|
| 76 |
/// Returns a \c NullMap class |
|
| 77 |
|
|
| 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 |
/// In other aspects it is equivalent to \ |
|
| 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 |
/// Returns a \ref ConstMap class |
|
| 137 |
|
|
| 138 |
/// |
|
| 136 |
/// Returns a \c ConstMap class |
|
| 137 |
|
|
| 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 |
/// In other aspects it is equivalent to \ |
|
| 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 |
|
| 175 | 175 |
/// Constructor. |
| 176 | 176 |
ConstMap() {}
|
| 177 | 177 |
|
| 178 | 178 |
/// Gives back the specified value. |
| 179 | 179 |
Value operator[](const Key&) const { return v; }
|
| 180 | 180 |
|
| 181 | 181 |
/// Absorbs the value. |
| 182 | 182 |
void set(const Key&, const Value&) {}
|
| 183 | 183 |
}; |
| 184 | 184 |
|
| 185 |
/// Returns a \ref ConstMap class with inlined constant value |
|
| 186 |
|
|
| 187 |
/// |
|
| 185 |
/// Returns a \c ConstMap class with inlined constant value |
|
| 186 |
|
|
| 187 |
/// This function just returns a \c ConstMap class with inlined |
|
| 188 | 188 |
/// constant value. |
| 189 | 189 |
/// \relates ConstMap |
| 190 | 190 |
template<typename K, typename V, V v> |
| 191 | 191 |
inline ConstMap<K, Const<V, v> > constMap() {
|
| 192 | 192 |
return ConstMap<K, Const<V, v> >(); |
| 193 | 193 |
} |
| 194 | 194 |
|
| 195 | 195 |
|
| 196 | 196 |
/// Identity map. |
| 197 | 197 |
|
| 198 | 198 |
/// This \ref concepts::ReadMap "read-only map" gives back the given |
| 199 | 199 |
/// key as value without any modification. |
| 200 | 200 |
/// |
| 201 | 201 |
/// \sa ConstMap |
| 202 | 202 |
template <typename T> |
| 203 | 203 |
class IdentityMap : public MapBase<T, T> {
|
| 204 | 204 |
public: |
| 205 | 205 |
typedef MapBase<T, T> Parent; |
| 206 | 206 |
typedef typename Parent::Key Key; |
| 207 | 207 |
typedef typename Parent::Value Value; |
| 208 | 208 |
|
| 209 | 209 |
/// Gives back the given value without any modification. |
| 210 | 210 |
Value operator[](const Key &k) const {
|
| 211 | 211 |
return k; |
| 212 | 212 |
} |
| 213 | 213 |
}; |
| 214 | 214 |
|
| 215 |
/// Returns an \ref IdentityMap class |
|
| 216 |
|
|
| 217 |
/// |
|
| 215 |
/// Returns an \c IdentityMap class |
|
| 216 |
|
|
| 217 |
/// This function just returns an \c IdentityMap class. |
|
| 218 | 218 |
/// \relates IdentityMap |
| 219 | 219 |
template<typename T> |
| 220 | 220 |
inline IdentityMap<T> identityMap() {
|
| 221 | 221 |
return IdentityMap<T>(); |
| 222 | 222 |
} |
| 223 | 223 |
|
| 224 | 224 |
|
| 225 | 225 |
/// \brief Map for storing values for integer keys from the range |
| 226 | 226 |
/// <tt>[0..size-1]</tt>. |
| 227 | 227 |
/// |
| 228 | 228 |
/// This map is essentially a wrapper for \c std::vector. It assigns |
| 229 | 229 |
/// values to integer keys from the range <tt>[0..size-1]</tt>. |
| 230 | 230 |
/// It can be used with some data structures, for example |
| 231 |
/// \ |
|
| 231 |
/// \c UnionFind, \c BinHeap, when the used items are small |
|
| 232 | 232 |
/// integers. This map conforms the \ref concepts::ReferenceMap |
| 233 | 233 |
/// "ReferenceMap" concept. |
| 234 | 234 |
/// |
| 235 | 235 |
/// The simplest way of using this map is through the rangeMap() |
| 236 | 236 |
/// function. |
| 237 | 237 |
template <typename V> |
| 238 | 238 |
class RangeMap : public MapBase<int, V> {
|
| 239 | 239 |
template <typename V1> |
| 240 | 240 |
friend class RangeMap; |
| 241 | 241 |
private: |
| 242 | 242 |
|
| 243 | 243 |
typedef std::vector<V> Vector; |
| 244 | 244 |
Vector _vector; |
| 245 | 245 |
|
| 246 | 246 |
public: |
| 247 | 247 |
|
| 248 | 248 |
typedef MapBase<int, V> Parent; |
| 249 | 249 |
/// Key type |
| 250 | 250 |
typedef typename Parent::Key Key; |
| 251 | 251 |
/// Value type |
| 252 | 252 |
typedef typename Parent::Value Value; |
| 253 | 253 |
/// Reference type |
| 254 | 254 |
typedef typename Vector::reference Reference; |
| 255 | 255 |
/// Const reference type |
| 256 | 256 |
typedef typename Vector::const_reference ConstReference; |
| 257 | 257 |
|
| 258 | 258 |
typedef True ReferenceMapTag; |
| 259 | 259 |
|
| 260 | 260 |
public: |
| 261 | 261 |
|
| 262 | 262 |
/// Constructor with specified default value. |
| 263 | 263 |
RangeMap(int size = 0, const Value &value = Value()) |
| 264 | 264 |
: _vector(size, value) {}
|
| 265 | 265 |
|
| 266 | 266 |
/// Constructs the map from an appropriate \c std::vector. |
| 267 | 267 |
template <typename V1> |
| 268 | 268 |
RangeMap(const std::vector<V1>& vector) |
| 269 | 269 |
: _vector(vector.begin(), vector.end()) {}
|
| 270 | 270 |
|
| 271 |
/// Constructs the map from another \ |
|
| 271 |
/// Constructs the map from another \c RangeMap. |
|
| 272 | 272 |
template <typename V1> |
| 273 | 273 |
RangeMap(const RangeMap<V1> &c) |
| 274 | 274 |
: _vector(c._vector.begin(), c._vector.end()) {}
|
| 275 | 275 |
|
| 276 | 276 |
/// Returns the size of the map. |
| 277 | 277 |
int size() {
|
| 278 | 278 |
return _vector.size(); |
| 279 | 279 |
} |
| 280 | 280 |
|
| 281 | 281 |
/// Resizes the map. |
| 282 | 282 |
|
| 283 | 283 |
/// Resizes the underlying \c std::vector container, so changes the |
| 284 | 284 |
/// keyset of the map. |
| 285 | 285 |
/// \param size The new size of the map. The new keyset will be the |
| 286 | 286 |
/// range <tt>[0..size-1]</tt>. |
| 287 | 287 |
/// \param value The default value to assign to the new keys. |
| 288 | 288 |
void resize(int size, const Value &value = Value()) {
|
| 289 | 289 |
_vector.resize(size, value); |
| 290 | 290 |
} |
| 291 | 291 |
|
| 292 | 292 |
private: |
| 293 | 293 |
|
| 294 | 294 |
RangeMap& operator=(const RangeMap&); |
| 295 | 295 |
|
| 296 | 296 |
public: |
| 297 | 297 |
|
| 298 | 298 |
///\e |
| 299 | 299 |
Reference operator[](const Key &k) {
|
| 300 | 300 |
return _vector[k]; |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 | 303 |
///\e |
| 304 | 304 |
ConstReference operator[](const Key &k) const {
|
| 305 | 305 |
return _vector[k]; |
| 306 | 306 |
} |
| 307 | 307 |
|
| 308 | 308 |
///\e |
| 309 | 309 |
void set(const Key &k, const Value &v) {
|
| 310 | 310 |
_vector[k] = v; |
| 311 | 311 |
} |
| 312 | 312 |
}; |
| 313 | 313 |
|
| 314 |
/// Returns a \ref RangeMap class |
|
| 315 |
|
|
| 316 |
/// |
|
| 314 |
/// Returns a \c RangeMap class |
|
| 315 |
|
|
| 316 |
/// This function just returns a \c RangeMap class. |
|
| 317 | 317 |
/// \relates RangeMap |
| 318 | 318 |
template<typename V> |
| 319 | 319 |
inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
|
| 320 | 320 |
return RangeMap<V>(size, value); |
| 321 | 321 |
} |
| 322 | 322 |
|
| 323 |
/// \brief Returns a \ |
|
| 323 |
/// \brief Returns a \c RangeMap class created from an appropriate |
|
| 324 | 324 |
/// \c std::vector |
| 325 | 325 |
|
| 326 |
/// This function just returns a \ |
|
| 326 |
/// This function just returns a \c RangeMap class created from an |
|
| 327 | 327 |
/// appropriate \c std::vector. |
| 328 | 328 |
/// \relates RangeMap |
| 329 | 329 |
template<typename V> |
| 330 | 330 |
inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
|
| 331 | 331 |
return RangeMap<V>(vector); |
| 332 | 332 |
} |
| 333 | 333 |
|
| 334 | 334 |
|
| 335 | 335 |
/// Map type based on \c std::map |
| 336 | 336 |
|
| 337 | 337 |
/// This map is essentially a wrapper for \c std::map with addition |
| 338 | 338 |
/// that you can specify a default value for the keys that are not |
| 339 | 339 |
/// stored actually. This value can be different from the default |
| 340 | 340 |
/// contructed value (i.e. \c %Value()). |
| 341 | 341 |
/// This type conforms the \ref concepts::ReferenceMap "ReferenceMap" |
| 342 | 342 |
/// concept. |
| 343 | 343 |
/// |
| 344 | 344 |
/// This map is useful if a default value should be assigned to most of |
| 345 | 345 |
/// the keys and different values should be assigned only to a few |
| 346 | 346 |
/// keys (i.e. the map is "sparse"). |
| 347 | 347 |
/// The name of this type also refers to this important usage. |
| 348 | 348 |
/// |
| 349 | 349 |
/// Apart form that this map can be used in many other cases since it |
| 350 | 350 |
/// is based on \c std::map, which is a general associative container. |
| 351 | 351 |
/// However keep in mind that it is usually not as efficient as other |
| 352 | 352 |
/// maps. |
| 353 | 353 |
/// |
| 354 | 354 |
/// The simplest way of using this map is through the sparseMap() |
| 355 | 355 |
/// function. |
| 356 | 356 |
template <typename K, typename V, typename Compare = std::less<K> > |
| 357 | 357 |
class SparseMap : public MapBase<K, V> {
|
| 358 | 358 |
template <typename K1, typename V1, typename C1> |
| 359 | 359 |
friend class SparseMap; |
| 360 | 360 |
public: |
| 361 | 361 |
|
| 362 | 362 |
typedef MapBase<K, V> Parent; |
| 363 | 363 |
/// Key type |
| 364 | 364 |
typedef typename Parent::Key Key; |
| 365 | 365 |
/// Value type |
| 366 | 366 |
typedef typename Parent::Value Value; |
| 367 | 367 |
/// Reference type |
| 368 | 368 |
typedef Value& Reference; |
| 369 | 369 |
/// Const reference type |
| 370 | 370 |
typedef const Value& ConstReference; |
| 371 | 371 |
|
| 372 | 372 |
typedef True ReferenceMapTag; |
| 373 | 373 |
|
| 374 | 374 |
private: |
| 375 | 375 |
|
| 376 | 376 |
typedef std::map<K, V, Compare> Map; |
| 377 | 377 |
Map _map; |
| 378 | 378 |
Value _value; |
| 379 | 379 |
|
| 380 | 380 |
public: |
| 381 | 381 |
|
| 382 | 382 |
/// \brief Constructor with specified default value. |
| 383 | 383 |
SparseMap(const Value &value = Value()) : _value(value) {}
|
| 384 | 384 |
/// \brief Constructs the map from an appropriate \c std::map, and |
| 385 | 385 |
/// explicitly specifies a default value. |
| 386 | 386 |
template <typename V1, typename Comp1> |
| 387 | 387 |
SparseMap(const std::map<Key, V1, Comp1> &map, |
| 388 | 388 |
const Value &value = Value()) |
| 389 | 389 |
: _map(map.begin(), map.end()), _value(value) {}
|
| 390 | 390 |
|
| 391 |
/// \brief Constructs the map from another \ |
|
| 391 |
/// \brief Constructs the map from another \c SparseMap. |
|
| 392 | 392 |
template<typename V1, typename Comp1> |
| 393 | 393 |
SparseMap(const SparseMap<Key, V1, Comp1> &c) |
| 394 | 394 |
: _map(c._map.begin(), c._map.end()), _value(c._value) {}
|
| 395 | 395 |
|
| 396 | 396 |
private: |
| 397 | 397 |
|
| 398 | 398 |
SparseMap& operator=(const SparseMap&); |
| 399 | 399 |
|
| 400 | 400 |
public: |
| 401 | 401 |
|
| 402 | 402 |
///\e |
| 403 | 403 |
Reference operator[](const Key &k) {
|
| 404 | 404 |
typename Map::iterator it = _map.lower_bound(k); |
| 405 | 405 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
| 406 | 406 |
return it->second; |
| 407 | 407 |
else |
| 408 | 408 |
return _map.insert(it, std::make_pair(k, _value))->second; |
| 409 | 409 |
} |
| 410 | 410 |
|
| 411 | 411 |
///\e |
| 412 | 412 |
ConstReference operator[](const Key &k) const {
|
| 413 | 413 |
typename Map::const_iterator it = _map.find(k); |
| 414 | 414 |
if (it != _map.end()) |
| 415 | 415 |
return it->second; |
| 416 | 416 |
else |
| 417 | 417 |
return _value; |
| 418 | 418 |
} |
| 419 | 419 |
|
| 420 | 420 |
///\e |
| 421 | 421 |
void set(const Key &k, const Value &v) {
|
| 422 | 422 |
typename Map::iterator it = _map.lower_bound(k); |
| 423 | 423 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
| 424 | 424 |
it->second = v; |
| 425 | 425 |
else |
| 426 | 426 |
_map.insert(it, std::make_pair(k, v)); |
| 427 | 427 |
} |
| 428 | 428 |
|
| 429 | 429 |
///\e |
| 430 | 430 |
void setAll(const Value &v) {
|
| 431 | 431 |
_value = v; |
| 432 | 432 |
_map.clear(); |
| 433 | 433 |
} |
| 434 | 434 |
}; |
| 435 | 435 |
|
| 436 |
/// Returns a \ref SparseMap class |
|
| 437 |
|
|
| 438 |
/// |
|
| 436 |
/// Returns a \c SparseMap class |
|
| 437 |
|
|
| 438 |
/// This function just returns a \c SparseMap class with specified |
|
| 439 | 439 |
/// default value. |
| 440 | 440 |
/// \relates SparseMap |
| 441 | 441 |
template<typename K, typename V, typename Compare> |
| 442 | 442 |
inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
|
| 443 | 443 |
return SparseMap<K, V, Compare>(value); |
| 444 | 444 |
} |
| 445 | 445 |
|
| 446 | 446 |
template<typename K, typename V> |
| 447 | 447 |
inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
|
| 448 | 448 |
return SparseMap<K, V, std::less<K> >(value); |
| 449 | 449 |
} |
| 450 | 450 |
|
| 451 |
/// \brief Returns a \ |
|
| 451 |
/// \brief Returns a \c SparseMap class created from an appropriate |
|
| 452 | 452 |
/// \c std::map |
| 453 | 453 |
|
| 454 |
/// This function just returns a \ |
|
| 454 |
/// This function just returns a \c SparseMap class created from an |
|
| 455 | 455 |
/// appropriate \c std::map. |
| 456 | 456 |
/// \relates SparseMap |
| 457 | 457 |
template<typename K, typename V, typename Compare> |
| 458 | 458 |
inline SparseMap<K, V, Compare> |
| 459 | 459 |
sparseMap(const std::map<K, V, Compare> &map, const V& value = V()) |
| 460 | 460 |
{
|
| 461 | 461 |
return SparseMap<K, V, Compare>(map, value); |
| 462 | 462 |
} |
| 463 | 463 |
|
| 464 | 464 |
/// @} |
| 465 | 465 |
|
| 466 | 466 |
/// \addtogroup map_adaptors |
| 467 | 467 |
/// @{
|
| 468 | 468 |
|
| 469 | 469 |
/// Composition of two maps |
| 470 | 470 |
|
| 471 | 471 |
/// This \ref concepts::ReadMap "read-only map" returns the |
| 472 | 472 |
/// composition of two given maps. That is to say, if \c m1 is of |
| 473 | 473 |
/// type \c M1 and \c m2 is of \c M2, then for |
| 474 | 474 |
/// \code |
| 475 | 475 |
/// ComposeMap<M1, M2> cm(m1,m2); |
| 476 | 476 |
/// \endcode |
| 477 | 477 |
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
| 478 | 478 |
/// |
| 479 | 479 |
/// The \c Key type of the map is inherited from \c M2 and the |
| 480 | 480 |
/// \c Value type is from \c M1. |
| 481 | 481 |
/// \c M2::Value must be convertible to \c M1::Key. |
| 482 | 482 |
/// |
| 483 | 483 |
/// The simplest way of using this map is through the composeMap() |
| 484 | 484 |
/// function. |
| 485 | 485 |
/// |
| 486 | 486 |
/// \sa CombineMap |
| 487 | 487 |
template <typename M1, typename M2> |
| 488 | 488 |
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
|
| 489 | 489 |
const M1 &_m1; |
| 490 | 490 |
const M2 &_m2; |
| 491 | 491 |
public: |
| 492 | 492 |
typedef MapBase<typename M2::Key, typename M1::Value> Parent; |
| 493 | 493 |
typedef typename Parent::Key Key; |
| 494 | 494 |
typedef typename Parent::Value Value; |
| 495 | 495 |
|
| 496 | 496 |
/// Constructor |
| 497 | 497 |
ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 498 | 498 |
|
| 499 | 499 |
/// \e |
| 500 | 500 |
typename MapTraits<M1>::ConstReturnValue |
| 501 | 501 |
operator[](const Key &k) const { return _m1[_m2[k]]; }
|
| 502 | 502 |
}; |
| 503 | 503 |
|
| 504 |
/// Returns a \ref ComposeMap class |
|
| 505 |
|
|
| 506 |
/// |
|
| 504 |
/// Returns a \c ComposeMap class |
|
| 505 |
|
|
| 506 |
/// This function just returns a \c ComposeMap class. |
|
| 507 | 507 |
/// |
| 508 | 508 |
/// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is |
| 509 | 509 |
/// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt> |
| 510 | 510 |
/// will be equal to <tt>m1[m2[x]]</tt>. |
| 511 | 511 |
/// |
| 512 | 512 |
/// \relates ComposeMap |
| 513 | 513 |
template <typename M1, typename M2> |
| 514 | 514 |
inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
|
| 515 | 515 |
return ComposeMap<M1, M2>(m1, m2); |
| 516 | 516 |
} |
| 517 | 517 |
|
| 518 | 518 |
|
| 519 | 519 |
/// Combination of two maps using an STL (binary) functor. |
| 520 | 520 |
|
| 521 | 521 |
/// This \ref concepts::ReadMap "read-only map" takes two maps and a |
| 522 | 522 |
/// binary functor and returns the combination of the two given maps |
| 523 | 523 |
/// using the functor. |
| 524 | 524 |
/// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2 |
| 525 | 525 |
/// and \c f is of \c F, then for |
| 526 | 526 |
/// \code |
| 527 | 527 |
/// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
| 528 | 528 |
/// \endcode |
| 529 | 529 |
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>. |
| 530 | 530 |
/// |
| 531 | 531 |
/// The \c Key type of the map is inherited from \c M1 (\c M1::Key |
| 532 | 532 |
/// must be convertible to \c M2::Key) and the \c Value type is \c V. |
| 533 | 533 |
/// \c M2::Value and \c M1::Value must be convertible to the |
| 534 | 534 |
/// corresponding input parameter of \c F and the return type of \c F |
| 535 | 535 |
/// must be convertible to \c V. |
| 536 | 536 |
/// |
| 537 | 537 |
/// The simplest way of using this map is through the combineMap() |
| 538 | 538 |
/// function. |
| 539 | 539 |
/// |
| 540 | 540 |
/// \sa ComposeMap |
| 541 | 541 |
template<typename M1, typename M2, typename F, |
| 542 | 542 |
typename V = typename F::result_type> |
| 543 | 543 |
class CombineMap : public MapBase<typename M1::Key, V> {
|
| 544 | 544 |
const M1 &_m1; |
| 545 | 545 |
const M2 &_m2; |
| 546 | 546 |
F _f; |
| 547 | 547 |
public: |
| 548 | 548 |
typedef MapBase<typename M1::Key, V> Parent; |
| 549 | 549 |
typedef typename Parent::Key Key; |
| 550 | 550 |
typedef typename Parent::Value Value; |
| 551 | 551 |
|
| 552 | 552 |
/// Constructor |
| 553 | 553 |
CombineMap(const M1 &m1, const M2 &m2, const F &f = F()) |
| 554 | 554 |
: _m1(m1), _m2(m2), _f(f) {}
|
| 555 | 555 |
/// \e |
| 556 | 556 |
Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
|
| 557 | 557 |
}; |
| 558 | 558 |
|
| 559 |
/// Returns a \ref CombineMap class |
|
| 560 |
|
|
| 561 |
/// |
|
| 559 |
/// Returns a \c CombineMap class |
|
| 560 |
|
|
| 561 |
/// This function just returns a \c CombineMap class. |
|
| 562 | 562 |
/// |
| 563 | 563 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
| 564 | 564 |
/// values, then |
| 565 | 565 |
/// \code |
| 566 | 566 |
/// combineMap(m1,m2,std::plus<double>()) |
| 567 | 567 |
/// \endcode |
| 568 | 568 |
/// is equivalent to |
| 569 | 569 |
/// \code |
| 570 | 570 |
/// addMap(m1,m2) |
| 571 | 571 |
/// \endcode |
| 572 | 572 |
/// |
| 573 | 573 |
/// This function is specialized for adaptable binary function |
| 574 | 574 |
/// classes and C++ functions. |
| 575 | 575 |
/// |
| 576 | 576 |
/// \relates CombineMap |
| 577 | 577 |
template<typename M1, typename M2, typename F, typename V> |
| 578 | 578 |
inline CombineMap<M1, M2, F, V> |
| 579 | 579 |
combineMap(const M1 &m1, const M2 &m2, const F &f) {
|
| 580 | 580 |
return CombineMap<M1, M2, F, V>(m1,m2,f); |
| 581 | 581 |
} |
| 582 | 582 |
|
| 583 | 583 |
template<typename M1, typename M2, typename F> |
| 584 | 584 |
inline CombineMap<M1, M2, F, typename F::result_type> |
| 585 | 585 |
combineMap(const M1 &m1, const M2 &m2, const F &f) {
|
| 586 | 586 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
| 587 | 587 |
} |
| 588 | 588 |
|
| 589 | 589 |
template<typename M1, typename M2, typename K1, typename K2, typename V> |
| 590 | 590 |
inline CombineMap<M1, M2, V (*)(K1, K2), V> |
| 591 | 591 |
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
|
| 592 | 592 |
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
| 593 | 593 |
} |
| 594 | 594 |
|
| 595 | 595 |
|
| 596 | 596 |
/// Converts an STL style (unary) functor to a map |
| 597 | 597 |
|
| 598 | 598 |
/// This \ref concepts::ReadMap "read-only map" returns the value |
| 599 | 599 |
/// of a given functor. Actually, it just wraps the functor and |
| 600 | 600 |
/// provides the \c Key and \c Value typedefs. |
| 601 | 601 |
/// |
| 602 | 602 |
/// Template parameters \c K and \c V will become its \c Key and |
| 603 | 603 |
/// \c Value. In most cases they have to be given explicitly because |
| 604 | 604 |
/// a functor typically does not provide \c argument_type and |
| 605 | 605 |
/// \c result_type typedefs. |
| 606 | 606 |
/// Parameter \c F is the type of the used functor. |
| 607 | 607 |
/// |
| 608 | 608 |
/// The simplest way of using this map is through the functorToMap() |
| 609 | 609 |
/// function. |
| 610 | 610 |
/// |
| 611 | 611 |
/// \sa MapToFunctor |
| 612 | 612 |
template<typename F, |
| 613 | 613 |
typename K = typename F::argument_type, |
| 614 | 614 |
typename V = typename F::result_type> |
| 615 | 615 |
class FunctorToMap : public MapBase<K, V> {
|
| 616 | 616 |
F _f; |
| 617 | 617 |
public: |
| 618 | 618 |
typedef MapBase<K, V> Parent; |
| 619 | 619 |
typedef typename Parent::Key Key; |
| 620 | 620 |
typedef typename Parent::Value Value; |
| 621 | 621 |
|
| 622 | 622 |
/// Constructor |
| 623 | 623 |
FunctorToMap(const F &f = F()) : _f(f) {}
|
| 624 | 624 |
/// \e |
| 625 | 625 |
Value operator[](const Key &k) const { return _f(k); }
|
| 626 | 626 |
}; |
| 627 | 627 |
|
| 628 |
/// Returns a \ref FunctorToMap class |
|
| 629 |
|
|
| 630 |
/// |
|
| 628 |
/// Returns a \c FunctorToMap class |
|
| 629 |
|
|
| 630 |
/// This function just returns a \c FunctorToMap class. |
|
| 631 | 631 |
/// |
| 632 | 632 |
/// This function is specialized for adaptable binary function |
| 633 | 633 |
/// classes and C++ functions. |
| 634 | 634 |
/// |
| 635 | 635 |
/// \relates FunctorToMap |
| 636 | 636 |
template<typename K, typename V, typename F> |
| 637 | 637 |
inline FunctorToMap<F, K, V> functorToMap(const F &f) {
|
| 638 | 638 |
return FunctorToMap<F, K, V>(f); |
| 639 | 639 |
} |
| 640 | 640 |
|
| 641 | 641 |
template <typename F> |
| 642 | 642 |
inline FunctorToMap<F, typename F::argument_type, typename F::result_type> |
| 643 | 643 |
functorToMap(const F &f) |
| 644 | 644 |
{
|
| 645 | 645 |
return FunctorToMap<F, typename F::argument_type, |
| 646 | 646 |
typename F::result_type>(f); |
| 647 | 647 |
} |
| 648 | 648 |
|
| 649 | 649 |
template <typename K, typename V> |
| 650 | 650 |
inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
|
| 651 | 651 |
return FunctorToMap<V (*)(K), K, V>(f); |
| 652 | 652 |
} |
| 653 | 653 |
|
| 654 | 654 |
|
| 655 | 655 |
/// Converts a map to an STL style (unary) functor |
| 656 | 656 |
|
| 657 | 657 |
/// This class converts a map to an STL style (unary) functor. |
| 658 | 658 |
/// That is it provides an <tt>operator()</tt> to read its values. |
| 659 | 659 |
/// |
| 660 | 660 |
/// For the sake of convenience it also works as a usual |
| 661 | 661 |
/// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt> |
| 662 | 662 |
/// and the \c Key and \c Value typedefs also exist. |
| 663 | 663 |
/// |
| 664 | 664 |
/// The simplest way of using this map is through the mapToFunctor() |
| 665 | 665 |
/// function. |
| 666 | 666 |
/// |
| 667 | 667 |
///\sa FunctorToMap |
| 668 | 668 |
template <typename M> |
| 669 | 669 |
class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
|
| 670 | 670 |
const M &_m; |
| 671 | 671 |
public: |
| 672 | 672 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 673 | 673 |
typedef typename Parent::Key Key; |
| 674 | 674 |
typedef typename Parent::Value Value; |
| 675 | 675 |
|
| 676 | 676 |
typedef typename Parent::Key argument_type; |
| 677 | 677 |
typedef typename Parent::Value result_type; |
| 678 | 678 |
|
| 679 | 679 |
/// Constructor |
| 680 | 680 |
MapToFunctor(const M &m) : _m(m) {}
|
| 681 | 681 |
/// \e |
| 682 | 682 |
Value operator()(const Key &k) const { return _m[k]; }
|
| 683 | 683 |
/// \e |
| 684 | 684 |
Value operator[](const Key &k) const { return _m[k]; }
|
| 685 | 685 |
}; |
| 686 | 686 |
|
| 687 |
/// Returns a \ref MapToFunctor class |
|
| 688 |
|
|
| 689 |
/// |
|
| 687 |
/// Returns a \c MapToFunctor class |
|
| 688 |
|
|
| 689 |
/// This function just returns a \c MapToFunctor class. |
|
| 690 | 690 |
/// \relates MapToFunctor |
| 691 | 691 |
template<typename M> |
| 692 | 692 |
inline MapToFunctor<M> mapToFunctor(const M &m) {
|
| 693 | 693 |
return MapToFunctor<M>(m); |
| 694 | 694 |
} |
| 695 | 695 |
|
| 696 | 696 |
|
| 697 | 697 |
/// \brief Map adaptor to convert the \c Value type of a map to |
| 698 | 698 |
/// another type using the default conversion. |
| 699 | 699 |
|
| 700 | 700 |
/// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap |
| 701 | 701 |
/// "readable map" to another type using the default conversion. |
| 702 | 702 |
/// The \c Key type of it is inherited from \c M and the \c Value |
| 703 | 703 |
/// type is \c V. |
| 704 | 704 |
/// This type conforms the \ref concepts::ReadMap "ReadMap" concept. |
| 705 | 705 |
/// |
| 706 | 706 |
/// The simplest way of using this map is through the convertMap() |
| 707 | 707 |
/// function. |
| 708 | 708 |
template <typename M, typename V> |
| 709 | 709 |
class ConvertMap : public MapBase<typename M::Key, V> {
|
| 710 | 710 |
const M &_m; |
| 711 | 711 |
public: |
| 712 | 712 |
typedef MapBase<typename M::Key, V> Parent; |
| 713 | 713 |
typedef typename Parent::Key Key; |
| 714 | 714 |
typedef typename Parent::Value Value; |
| 715 | 715 |
|
| 716 | 716 |
/// Constructor |
| 717 | 717 |
|
| 718 | 718 |
/// Constructor. |
| 719 | 719 |
/// \param m The underlying map. |
| 720 | 720 |
ConvertMap(const M &m) : _m(m) {}
|
| 721 | 721 |
|
| 722 | 722 |
/// \e |
| 723 | 723 |
Value operator[](const Key &k) const { return _m[k]; }
|
| 724 | 724 |
}; |
| 725 | 725 |
|
| 726 |
/// Returns a \ref ConvertMap class |
|
| 727 |
|
|
| 728 |
/// |
|
| 726 |
/// Returns a \c ConvertMap class |
|
| 727 |
|
|
| 728 |
/// This function just returns a \c ConvertMap class. |
|
| 729 | 729 |
/// \relates ConvertMap |
| 730 | 730 |
template<typename V, typename M> |
| 731 | 731 |
inline ConvertMap<M, V> convertMap(const M &map) {
|
| 732 | 732 |
return ConvertMap<M, V>(map); |
| 733 | 733 |
} |
| 734 | 734 |
|
| 735 | 735 |
|
| 736 | 736 |
/// Applies all map setting operations to two maps |
| 737 | 737 |
|
| 738 | 738 |
/// This map has two \ref concepts::WriteMap "writable map" parameters |
| 739 | 739 |
/// and each write request will be passed to both of them. |
| 740 | 740 |
/// If \c M1 is also \ref concepts::ReadMap "readable", then the read |
| 741 | 741 |
/// operations will return the corresponding values of \c M1. |
| 742 | 742 |
/// |
| 743 | 743 |
/// The \c Key and \c Value types are inherited from \c M1. |
| 744 | 744 |
/// The \c Key and \c Value of \c M2 must be convertible from those |
| 745 | 745 |
/// of \c M1. |
| 746 | 746 |
/// |
| 747 | 747 |
/// The simplest way of using this map is through the forkMap() |
| 748 | 748 |
/// function. |
| 749 | 749 |
template<typename M1, typename M2> |
| 750 | 750 |
class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 751 | 751 |
M1 &_m1; |
| 752 | 752 |
M2 &_m2; |
| 753 | 753 |
public: |
| 754 | 754 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
| 755 | 755 |
typedef typename Parent::Key Key; |
| 756 | 756 |
typedef typename Parent::Value Value; |
| 757 | 757 |
|
| 758 | 758 |
/// Constructor |
| 759 | 759 |
ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
|
| 760 | 760 |
/// Returns the value associated with the given key in the first map. |
| 761 | 761 |
Value operator[](const Key &k) const { return _m1[k]; }
|
| 762 | 762 |
/// Sets the value associated with the given key in both maps. |
| 763 | 763 |
void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
|
| 764 | 764 |
}; |
| 765 | 765 |
|
| 766 |
/// Returns a \ref ForkMap class |
|
| 767 |
|
|
| 768 |
/// |
|
| 766 |
/// Returns a \c ForkMap class |
|
| 767 |
|
|
| 768 |
/// This function just returns a \c ForkMap class. |
|
| 769 | 769 |
/// \relates ForkMap |
| 770 | 770 |
template <typename M1, typename M2> |
| 771 | 771 |
inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
|
| 772 | 772 |
return ForkMap<M1,M2>(m1,m2); |
| 773 | 773 |
} |
| 774 | 774 |
|
| 775 | 775 |
|
| 776 | 776 |
/// Sum of two maps |
| 777 | 777 |
|
| 778 | 778 |
/// This \ref concepts::ReadMap "read-only map" returns the sum |
| 779 | 779 |
/// of the values of the two given maps. |
| 780 | 780 |
/// Its \c Key and \c Value types are inherited from \c M1. |
| 781 | 781 |
/// The \c Key and \c Value of \c M2 must be convertible to those of |
| 782 | 782 |
/// \c M1. |
| 783 | 783 |
/// |
| 784 | 784 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 785 | 785 |
/// \code |
| 786 | 786 |
/// AddMap<M1,M2> am(m1,m2); |
| 787 | 787 |
/// \endcode |
| 788 | 788 |
/// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>. |
| 789 | 789 |
/// |
| 790 | 790 |
/// The simplest way of using this map is through the addMap() |
| 791 | 791 |
/// function. |
| 792 | 792 |
/// |
| 793 | 793 |
/// \sa SubMap, MulMap, DivMap |
| 794 | 794 |
/// \sa ShiftMap, ShiftWriteMap |
| 795 | 795 |
template<typename M1, typename M2> |
| 796 | 796 |
class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 797 | 797 |
const M1 &_m1; |
| 798 | 798 |
const M2 &_m2; |
| 799 | 799 |
public: |
| 800 | 800 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
| 801 | 801 |
typedef typename Parent::Key Key; |
| 802 | 802 |
typedef typename Parent::Value Value; |
| 803 | 803 |
|
| 804 | 804 |
/// Constructor |
| 805 | 805 |
AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 806 | 806 |
/// \e |
| 807 | 807 |
Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
|
| 808 | 808 |
}; |
| 809 | 809 |
|
| 810 |
/// Returns an \ref AddMap class |
|
| 811 |
|
|
| 812 |
/// |
|
| 810 |
/// Returns an \c AddMap class |
|
| 811 |
|
|
| 812 |
/// This function just returns an \c AddMap class. |
|
| 813 | 813 |
/// |
| 814 | 814 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
| 815 | 815 |
/// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to |
| 816 | 816 |
/// <tt>m1[x]+m2[x]</tt>. |
| 817 | 817 |
/// |
| 818 | 818 |
/// \relates AddMap |
| 819 | 819 |
template<typename M1, typename M2> |
| 820 | 820 |
inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
|
| 821 | 821 |
return AddMap<M1, M2>(m1,m2); |
| 822 | 822 |
} |
| 823 | 823 |
|
| 824 | 824 |
|
| 825 | 825 |
/// Difference of two maps |
| 826 | 826 |
|
| 827 | 827 |
/// This \ref concepts::ReadMap "read-only map" returns the difference |
| 828 | 828 |
/// of the values of the two given maps. |
| 829 | 829 |
/// Its \c Key and \c Value types are inherited from \c M1. |
| 830 | 830 |
/// The \c Key and \c Value of \c M2 must be convertible to those of |
| 831 | 831 |
/// \c M1. |
| 832 | 832 |
/// |
| 833 | 833 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 834 | 834 |
/// \code |
| 835 | 835 |
/// SubMap<M1,M2> sm(m1,m2); |
| 836 | 836 |
/// \endcode |
| 837 | 837 |
/// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>. |
| 838 | 838 |
/// |
| 839 | 839 |
/// The simplest way of using this map is through the subMap() |
| 840 | 840 |
/// function. |
| 841 | 841 |
/// |
| 842 | 842 |
/// \sa AddMap, MulMap, DivMap |
| 843 | 843 |
template<typename M1, typename M2> |
| 844 | 844 |
class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 845 | 845 |
const M1 &_m1; |
| 846 | 846 |
const M2 &_m2; |
| 847 | 847 |
public: |
| 848 | 848 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
| 849 | 849 |
typedef typename Parent::Key Key; |
| 850 | 850 |
typedef typename Parent::Value Value; |
| 851 | 851 |
|
| 852 | 852 |
/// Constructor |
| 853 | 853 |
SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 854 | 854 |
/// \e |
| 855 | 855 |
Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
|
| 856 | 856 |
}; |
| 857 | 857 |
|
| 858 |
/// Returns a \ref SubMap class |
|
| 859 |
|
|
| 860 |
/// |
|
| 858 |
/// Returns a \c SubMap class |
|
| 859 |
|
|
| 860 |
/// This function just returns a \c SubMap class. |
|
| 861 | 861 |
/// |
| 862 | 862 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
| 863 | 863 |
/// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to |
| 864 | 864 |
/// <tt>m1[x]-m2[x]</tt>. |
| 865 | 865 |
/// |
| 866 | 866 |
/// \relates SubMap |
| 867 | 867 |
template<typename M1, typename M2> |
| 868 | 868 |
inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
|
| 869 | 869 |
return SubMap<M1, M2>(m1,m2); |
| 870 | 870 |
} |
| 871 | 871 |
|
| 872 | 872 |
|
| 873 | 873 |
/// Product of two maps |
| 874 | 874 |
|
| 875 | 875 |
/// This \ref concepts::ReadMap "read-only map" returns the product |
| 876 | 876 |
/// of the values of the two given maps. |
| 877 | 877 |
/// Its \c Key and \c Value types are inherited from \c M1. |
| 878 | 878 |
/// The \c Key and \c Value of \c M2 must be convertible to those of |
| 879 | 879 |
/// \c M1. |
| 880 | 880 |
/// |
| 881 | 881 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 882 | 882 |
/// \code |
| 883 | 883 |
/// MulMap<M1,M2> mm(m1,m2); |
| 884 | 884 |
/// \endcode |
| 885 | 885 |
/// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>. |
| 886 | 886 |
/// |
| 887 | 887 |
/// The simplest way of using this map is through the mulMap() |
| 888 | 888 |
/// function. |
| 889 | 889 |
/// |
| 890 | 890 |
/// \sa AddMap, SubMap, DivMap |
| 891 | 891 |
/// \sa ScaleMap, ScaleWriteMap |
| 892 | 892 |
template<typename M1, typename M2> |
| 893 | 893 |
class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 894 | 894 |
const M1 &_m1; |
| 895 | 895 |
const M2 &_m2; |
| 896 | 896 |
public: |
| 897 | 897 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
| 898 | 898 |
typedef typename Parent::Key Key; |
| 899 | 899 |
typedef typename Parent::Value Value; |
| 900 | 900 |
|
| 901 | 901 |
/// Constructor |
| 902 | 902 |
MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 903 | 903 |
/// \e |
| 904 | 904 |
Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
|
| 905 | 905 |
}; |
| 906 | 906 |
|
| 907 |
/// Returns a \ref MulMap class |
|
| 908 |
|
|
| 909 |
/// |
|
| 907 |
/// Returns a \c MulMap class |
|
| 908 |
|
|
| 909 |
/// This function just returns a \c MulMap class. |
|
| 910 | 910 |
/// |
| 911 | 911 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
| 912 | 912 |
/// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to |
| 913 | 913 |
/// <tt>m1[x]*m2[x]</tt>. |
| 914 | 914 |
/// |
| 915 | 915 |
/// \relates MulMap |
| 916 | 916 |
template<typename M1, typename M2> |
| 917 | 917 |
inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
|
| 918 | 918 |
return MulMap<M1, M2>(m1,m2); |
| 919 | 919 |
} |
| 920 | 920 |
|
| 921 | 921 |
|
| 922 | 922 |
/// Quotient of two maps |
| 923 | 923 |
|
| 924 | 924 |
/// This \ref concepts::ReadMap "read-only map" returns the quotient |
| 925 | 925 |
/// of the values of the two given maps. |
| 926 | 926 |
/// Its \c Key and \c Value types are inherited from \c M1. |
| 927 | 927 |
/// The \c Key and \c Value of \c M2 must be convertible to those of |
| 928 | 928 |
/// \c M1. |
| 929 | 929 |
/// |
| 930 | 930 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 931 | 931 |
/// \code |
| 932 | 932 |
/// DivMap<M1,M2> dm(m1,m2); |
| 933 | 933 |
/// \endcode |
| 934 | 934 |
/// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>. |
| 935 | 935 |
/// |
| 936 | 936 |
/// The simplest way of using this map is through the divMap() |
| 937 | 937 |
/// function. |
| 938 | 938 |
/// |
| 939 | 939 |
/// \sa AddMap, SubMap, MulMap |
| 940 | 940 |
template<typename M1, typename M2> |
| 941 | 941 |
class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 942 | 942 |
const M1 &_m1; |
| 943 | 943 |
const M2 &_m2; |
| 944 | 944 |
public: |
| 945 | 945 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
| 946 | 946 |
typedef typename Parent::Key Key; |
| 947 | 947 |
typedef typename Parent::Value Value; |
| 948 | 948 |
|
| 949 | 949 |
/// Constructor |
| 950 | 950 |
DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 951 | 951 |
/// \e |
| 952 | 952 |
Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
|
| 953 | 953 |
}; |
| 954 | 954 |
|
| 955 |
/// Returns a \ref DivMap class |
|
| 956 |
|
|
| 957 |
/// |
|
| 955 |
/// Returns a \c DivMap class |
|
| 956 |
|
|
| 957 |
/// This function just returns a \c DivMap class. |
|
| 958 | 958 |
/// |
| 959 | 959 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
| 960 | 960 |
/// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to |
| 961 | 961 |
/// <tt>m1[x]/m2[x]</tt>. |
| 962 | 962 |
/// |
| 963 | 963 |
/// \relates DivMap |
| 964 | 964 |
template<typename M1, typename M2> |
| 965 | 965 |
inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
|
| 966 | 966 |
return DivMap<M1, M2>(m1,m2); |
| 967 | 967 |
} |
| 968 | 968 |
|
| 969 | 969 |
|
| 970 | 970 |
/// Shifts a map with a constant. |
| 971 | 971 |
|
| 972 | 972 |
/// This \ref concepts::ReadMap "read-only map" returns the sum of |
| 973 | 973 |
/// the given map and a constant value (i.e. it shifts the map with |
| 974 | 974 |
/// the constant). Its \c Key and \c Value are inherited from \c M. |
| 975 | 975 |
/// |
| 976 | 976 |
/// Actually, |
| 977 | 977 |
/// \code |
| 978 | 978 |
/// ShiftMap<M> sh(m,v); |
| 979 | 979 |
/// \endcode |
| 980 | 980 |
/// is equivalent to |
| 981 | 981 |
/// \code |
| 982 | 982 |
/// ConstMap<M::Key, M::Value> cm(v); |
| 983 | 983 |
/// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm); |
| 984 | 984 |
/// \endcode |
| 985 | 985 |
/// |
| 986 | 986 |
/// The simplest way of using this map is through the shiftMap() |
| 987 | 987 |
/// function. |
| 988 | 988 |
/// |
| 989 | 989 |
/// \sa ShiftWriteMap |
| 990 | 990 |
template<typename M, typename C = typename M::Value> |
| 991 | 991 |
class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
|
| 992 | 992 |
const M &_m; |
| 993 | 993 |
C _v; |
| 994 | 994 |
public: |
| 995 | 995 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 996 | 996 |
typedef typename Parent::Key Key; |
| 997 | 997 |
typedef typename Parent::Value Value; |
| 998 | 998 |
|
| 999 | 999 |
/// Constructor |
| 1000 | 1000 |
|
| 1001 | 1001 |
/// Constructor. |
| 1002 | 1002 |
/// \param m The undelying map. |
| 1003 | 1003 |
/// \param v The constant value. |
| 1004 | 1004 |
ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
|
| 1005 | 1005 |
/// \e |
| 1006 | 1006 |
Value operator[](const Key &k) const { return _m[k]+_v; }
|
| 1007 | 1007 |
}; |
| 1008 | 1008 |
|
| 1009 | 1009 |
/// Shifts a map with a constant (read-write version). |
| 1010 | 1010 |
|
| 1011 | 1011 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the sum |
| 1012 | 1012 |
/// of the given map and a constant value (i.e. it shifts the map with |
| 1013 | 1013 |
/// the constant). Its \c Key and \c Value are inherited from \c M. |
| 1014 | 1014 |
/// It makes also possible to write the map. |
| 1015 | 1015 |
/// |
| 1016 | 1016 |
/// The simplest way of using this map is through the shiftWriteMap() |
| 1017 | 1017 |
/// function. |
| 1018 | 1018 |
/// |
| 1019 | 1019 |
/// \sa ShiftMap |
| 1020 | 1020 |
template<typename M, typename C = typename M::Value> |
| 1021 | 1021 |
class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1022 | 1022 |
M &_m; |
| 1023 | 1023 |
C _v; |
| 1024 | 1024 |
public: |
| 1025 | 1025 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 1026 | 1026 |
typedef typename Parent::Key Key; |
| 1027 | 1027 |
typedef typename Parent::Value Value; |
| 1028 | 1028 |
|
| 1029 | 1029 |
/// Constructor |
| 1030 | 1030 |
|
| 1031 | 1031 |
/// Constructor. |
| 1032 | 1032 |
/// \param m The undelying map. |
| 1033 | 1033 |
/// \param v The constant value. |
| 1034 | 1034 |
ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
|
| 1035 | 1035 |
/// \e |
| 1036 | 1036 |
Value operator[](const Key &k) const { return _m[k]+_v; }
|
| 1037 | 1037 |
/// \e |
| 1038 | 1038 |
void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
|
| 1039 | 1039 |
}; |
| 1040 | 1040 |
|
| 1041 |
/// Returns a \ref ShiftMap class |
|
| 1042 |
|
|
| 1043 |
/// |
|
| 1041 |
/// Returns a \c ShiftMap class |
|
| 1042 |
|
|
| 1043 |
/// This function just returns a \c ShiftMap class. |
|
| 1044 | 1044 |
/// |
| 1045 | 1045 |
/// For example, if \c m is a map with \c double values and \c v is |
| 1046 | 1046 |
/// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to |
| 1047 | 1047 |
/// <tt>m[x]+v</tt>. |
| 1048 | 1048 |
/// |
| 1049 | 1049 |
/// \relates ShiftMap |
| 1050 | 1050 |
template<typename M, typename C> |
| 1051 | 1051 |
inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
|
| 1052 | 1052 |
return ShiftMap<M, C>(m,v); |
| 1053 | 1053 |
} |
| 1054 | 1054 |
|
| 1055 |
/// Returns a \ref ShiftWriteMap class |
|
| 1056 |
|
|
| 1057 |
/// |
|
| 1055 |
/// Returns a \c ShiftWriteMap class |
|
| 1056 |
|
|
| 1057 |
/// This function just returns a \c ShiftWriteMap class. |
|
| 1058 | 1058 |
/// |
| 1059 | 1059 |
/// For example, if \c m is a map with \c double values and \c v is |
| 1060 | 1060 |
/// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to |
| 1061 | 1061 |
/// <tt>m[x]+v</tt>. |
| 1062 | 1062 |
/// Moreover it makes also possible to write the map. |
| 1063 | 1063 |
/// |
| 1064 | 1064 |
/// \relates ShiftWriteMap |
| 1065 | 1065 |
template<typename M, typename C> |
| 1066 | 1066 |
inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
|
| 1067 | 1067 |
return ShiftWriteMap<M, C>(m,v); |
| 1068 | 1068 |
} |
| 1069 | 1069 |
|
| 1070 | 1070 |
|
| 1071 | 1071 |
/// Scales a map with a constant. |
| 1072 | 1072 |
|
| 1073 | 1073 |
/// This \ref concepts::ReadMap "read-only map" returns the value of |
| 1074 | 1074 |
/// the given map multiplied from the left side with a constant value. |
| 1075 | 1075 |
/// Its \c Key and \c Value are inherited from \c M. |
| 1076 | 1076 |
/// |
| 1077 | 1077 |
/// Actually, |
| 1078 | 1078 |
/// \code |
| 1079 | 1079 |
/// ScaleMap<M> sc(m,v); |
| 1080 | 1080 |
/// \endcode |
| 1081 | 1081 |
/// is equivalent to |
| 1082 | 1082 |
/// \code |
| 1083 | 1083 |
/// ConstMap<M::Key, M::Value> cm(v); |
| 1084 | 1084 |
/// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m); |
| 1085 | 1085 |
/// \endcode |
| 1086 | 1086 |
/// |
| 1087 | 1087 |
/// The simplest way of using this map is through the scaleMap() |
| 1088 | 1088 |
/// function. |
| 1089 | 1089 |
/// |
| 1090 | 1090 |
/// \sa ScaleWriteMap |
| 1091 | 1091 |
template<typename M, typename C = typename M::Value> |
| 1092 | 1092 |
class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1093 | 1093 |
const M &_m; |
| 1094 | 1094 |
C _v; |
| 1095 | 1095 |
public: |
| 1096 | 1096 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 1097 | 1097 |
typedef typename Parent::Key Key; |
| 1098 | 1098 |
typedef typename Parent::Value Value; |
| 1099 | 1099 |
|
| 1100 | 1100 |
/// Constructor |
| 1101 | 1101 |
|
| 1102 | 1102 |
/// Constructor. |
| 1103 | 1103 |
/// \param m The undelying map. |
| 1104 | 1104 |
/// \param v The constant value. |
| 1105 | 1105 |
ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
|
| 1106 | 1106 |
/// \e |
| 1107 | 1107 |
Value operator[](const Key &k) const { return _v*_m[k]; }
|
| 1108 | 1108 |
}; |
| 1109 | 1109 |
|
| 1110 | 1110 |
/// Scales a map with a constant (read-write version). |
| 1111 | 1111 |
|
| 1112 | 1112 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the value of |
| 1113 | 1113 |
/// the given map multiplied from the left side with a constant value. |
| 1114 | 1114 |
/// Its \c Key and \c Value are inherited from \c M. |
| 1115 | 1115 |
/// It can also be used as write map if the \c / operator is defined |
| 1116 | 1116 |
/// between \c Value and \c C and the given multiplier is not zero. |
| 1117 | 1117 |
/// |
| 1118 | 1118 |
/// The simplest way of using this map is through the scaleWriteMap() |
| 1119 | 1119 |
/// function. |
| 1120 | 1120 |
/// |
| 1121 | 1121 |
/// \sa ScaleMap |
| 1122 | 1122 |
template<typename M, typename C = typename M::Value> |
| 1123 | 1123 |
class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1124 | 1124 |
M &_m; |
| 1125 | 1125 |
C _v; |
| 1126 | 1126 |
public: |
| 1127 | 1127 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 1128 | 1128 |
typedef typename Parent::Key Key; |
| 1129 | 1129 |
typedef typename Parent::Value Value; |
| 1130 | 1130 |
|
| 1131 | 1131 |
/// Constructor |
| 1132 | 1132 |
|
| 1133 | 1133 |
/// Constructor. |
| 1134 | 1134 |
/// \param m The undelying map. |
| 1135 | 1135 |
/// \param v The constant value. |
| 1136 | 1136 |
ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
|
| 1137 | 1137 |
/// \e |
| 1138 | 1138 |
Value operator[](const Key &k) const { return _v*_m[k]; }
|
| 1139 | 1139 |
/// \e |
| 1140 | 1140 |
void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
|
| 1141 | 1141 |
}; |
| 1142 | 1142 |
|
| 1143 |
/// Returns a \ref ScaleMap class |
|
| 1144 |
|
|
| 1145 |
/// |
|
| 1143 |
/// Returns a \c ScaleMap class |
|
| 1144 |
|
|
| 1145 |
/// This function just returns a \c ScaleMap class. |
|
| 1146 | 1146 |
/// |
| 1147 | 1147 |
/// For example, if \c m is a map with \c double values and \c v is |
| 1148 | 1148 |
/// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to |
| 1149 | 1149 |
/// <tt>v*m[x]</tt>. |
| 1150 | 1150 |
/// |
| 1151 | 1151 |
/// \relates ScaleMap |
| 1152 | 1152 |
template<typename M, typename C> |
| 1153 | 1153 |
inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
|
| 1154 | 1154 |
return ScaleMap<M, C>(m,v); |
| 1155 | 1155 |
} |
| 1156 | 1156 |
|
| 1157 |
/// Returns a \ref ScaleWriteMap class |
|
| 1158 |
|
|
| 1159 |
/// |
|
| 1157 |
/// Returns a \c ScaleWriteMap class |
|
| 1158 |
|
|
| 1159 |
/// This function just returns a \c ScaleWriteMap class. |
|
| 1160 | 1160 |
/// |
| 1161 | 1161 |
/// For example, if \c m is a map with \c double values and \c v is |
| 1162 | 1162 |
/// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to |
| 1163 | 1163 |
/// <tt>v*m[x]</tt>. |
| 1164 | 1164 |
/// Moreover it makes also possible to write the map. |
| 1165 | 1165 |
/// |
| 1166 | 1166 |
/// \relates ScaleWriteMap |
| 1167 | 1167 |
template<typename M, typename C> |
| 1168 | 1168 |
inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
|
| 1169 | 1169 |
return ScaleWriteMap<M, C>(m,v); |
| 1170 | 1170 |
} |
| 1171 | 1171 |
|
| 1172 | 1172 |
|
| 1173 | 1173 |
/// Negative of a map |
| 1174 | 1174 |
|
| 1175 | 1175 |
/// This \ref concepts::ReadMap "read-only map" returns the negative |
| 1176 | 1176 |
/// of the values of the given map (using the unary \c - operator). |
| 1177 | 1177 |
/// Its \c Key and \c Value are inherited from \c M. |
| 1178 | 1178 |
/// |
| 1179 | 1179 |
/// If M::Value is \c int, \c double etc., then |
| 1180 | 1180 |
/// \code |
| 1181 | 1181 |
/// NegMap<M> neg(m); |
| 1182 | 1182 |
/// \endcode |
| 1183 | 1183 |
/// is equivalent to |
| 1184 | 1184 |
/// \code |
| 1185 | 1185 |
/// ScaleMap<M> neg(m,-1); |
| 1186 | 1186 |
/// \endcode |
| 1187 | 1187 |
/// |
| 1188 | 1188 |
/// The simplest way of using this map is through the negMap() |
| 1189 | 1189 |
/// function. |
| 1190 | 1190 |
/// |
| 1191 | 1191 |
/// \sa NegWriteMap |
| 1192 | 1192 |
template<typename M> |
| 1193 | 1193 |
class NegMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1194 | 1194 |
const M& _m; |
| 1195 | 1195 |
public: |
| 1196 | 1196 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 1197 | 1197 |
typedef typename Parent::Key Key; |
| 1198 | 1198 |
typedef typename Parent::Value Value; |
| 1199 | 1199 |
|
| 1200 | 1200 |
/// Constructor |
| 1201 | 1201 |
NegMap(const M &m) : _m(m) {}
|
| 1202 | 1202 |
/// \e |
| 1203 | 1203 |
Value operator[](const Key &k) const { return -_m[k]; }
|
| 1204 | 1204 |
}; |
| 1205 | 1205 |
|
| 1206 | 1206 |
/// Negative of a map (read-write version) |
| 1207 | 1207 |
|
| 1208 | 1208 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the |
| 1209 | 1209 |
/// negative of the values of the given map (using the unary \c - |
| 1210 | 1210 |
/// operator). |
| 1211 | 1211 |
/// Its \c Key and \c Value are inherited from \c M. |
| 1212 | 1212 |
/// It makes also possible to write the map. |
| 1213 | 1213 |
/// |
| 1214 | 1214 |
/// If M::Value is \c int, \c double etc., then |
| 1215 | 1215 |
/// \code |
| 1216 | 1216 |
/// NegWriteMap<M> neg(m); |
| 1217 | 1217 |
/// \endcode |
| 1218 | 1218 |
/// is equivalent to |
| 1219 | 1219 |
/// \code |
| 1220 | 1220 |
/// ScaleWriteMap<M> neg(m,-1); |
| 1221 | 1221 |
/// \endcode |
| 1222 | 1222 |
/// |
| 1223 | 1223 |
/// The simplest way of using this map is through the negWriteMap() |
| 1224 | 1224 |
/// function. |
| 1225 | 1225 |
/// |
| 1226 | 1226 |
/// \sa NegMap |
| 1227 | 1227 |
template<typename M> |
| 1228 | 1228 |
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1229 | 1229 |
M &_m; |
| 1230 | 1230 |
public: |
| 1231 | 1231 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 1232 | 1232 |
typedef typename Parent::Key Key; |
| 1233 | 1233 |
typedef typename Parent::Value Value; |
| 1234 | 1234 |
|
| 1235 | 1235 |
/// Constructor |
| 1236 | 1236 |
NegWriteMap(M &m) : _m(m) {}
|
| 1237 | 1237 |
/// \e |
| 1238 | 1238 |
Value operator[](const Key &k) const { return -_m[k]; }
|
| 1239 | 1239 |
/// \e |
| 1240 | 1240 |
void set(const Key &k, const Value &v) { _m.set(k, -v); }
|
| 1241 | 1241 |
}; |
| 1242 | 1242 |
|
| 1243 |
/// Returns a \ref NegMap class |
|
| 1244 |
|
|
| 1245 |
/// |
|
| 1243 |
/// Returns a \c NegMap class |
|
| 1244 |
|
|
| 1245 |
/// This function just returns a \c NegMap class. |
|
| 1246 | 1246 |
/// |
| 1247 | 1247 |
/// For example, if \c m is a map with \c double values, then |
| 1248 | 1248 |
/// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
| 1249 | 1249 |
/// |
| 1250 | 1250 |
/// \relates NegMap |
| 1251 | 1251 |
template <typename M> |
| 1252 | 1252 |
inline NegMap<M> negMap(const M &m) {
|
| 1253 | 1253 |
return NegMap<M>(m); |
| 1254 | 1254 |
} |
| 1255 | 1255 |
|
| 1256 |
/// Returns a \ref NegWriteMap class |
|
| 1257 |
|
|
| 1258 |
/// |
|
| 1256 |
/// Returns a \c NegWriteMap class |
|
| 1257 |
|
|
| 1258 |
/// This function just returns a \c NegWriteMap class. |
|
| 1259 | 1259 |
/// |
| 1260 | 1260 |
/// For example, if \c m is a map with \c double values, then |
| 1261 | 1261 |
/// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
| 1262 | 1262 |
/// Moreover it makes also possible to write the map. |
| 1263 | 1263 |
/// |
| 1264 | 1264 |
/// \relates NegWriteMap |
| 1265 | 1265 |
template <typename M> |
| 1266 | 1266 |
inline NegWriteMap<M> negWriteMap(M &m) {
|
| 1267 | 1267 |
return NegWriteMap<M>(m); |
| 1268 | 1268 |
} |
| 1269 | 1269 |
|
| 1270 | 1270 |
|
| 1271 | 1271 |
/// Absolute value of a map |
| 1272 | 1272 |
|
| 1273 | 1273 |
/// This \ref concepts::ReadMap "read-only map" returns the absolute |
| 1274 | 1274 |
/// value of the values of the given map. |
| 1275 | 1275 |
/// Its \c Key and \c Value are inherited from \c M. |
| 1276 | 1276 |
/// \c Value must be comparable to \c 0 and the unary \c - |
| 1277 | 1277 |
/// operator must be defined for it, of course. |
| 1278 | 1278 |
/// |
| 1279 | 1279 |
/// The simplest way of using this map is through the absMap() |
| 1280 | 1280 |
/// function. |
| 1281 | 1281 |
template<typename M> |
| 1282 | 1282 |
class AbsMap : public MapBase<typename M::Key, typename M::Value> {
|
| 1283 | 1283 |
const M &_m; |
| 1284 | 1284 |
public: |
| 1285 | 1285 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 1286 | 1286 |
typedef typename Parent::Key Key; |
| 1287 | 1287 |
typedef typename Parent::Value Value; |
| 1288 | 1288 |
|
| 1289 | 1289 |
/// Constructor |
| 1290 | 1290 |
AbsMap(const M &m) : _m(m) {}
|
| 1291 | 1291 |
/// \e |
| 1292 | 1292 |
Value operator[](const Key &k) const {
|
| 1293 | 1293 |
Value tmp = _m[k]; |
| 1294 | 1294 |
return tmp >= 0 ? tmp : -tmp; |
| 1295 | 1295 |
} |
| 1296 | 1296 |
|
| 1297 | 1297 |
}; |
| 1298 | 1298 |
|
| 1299 |
/// Returns an \ref AbsMap class |
|
| 1300 |
|
|
| 1301 |
/// |
|
| 1299 |
/// Returns an \c AbsMap class |
|
| 1300 |
|
|
| 1301 |
/// This function just returns an \c AbsMap class. |
|
| 1302 | 1302 |
/// |
| 1303 | 1303 |
/// For example, if \c m is a map with \c double values, then |
| 1304 | 1304 |
/// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if |
| 1305 | 1305 |
/// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is |
| 1306 | 1306 |
/// negative. |
| 1307 | 1307 |
/// |
| 1308 | 1308 |
/// \relates AbsMap |
| 1309 | 1309 |
template<typename M> |
| 1310 | 1310 |
inline AbsMap<M> absMap(const M &m) {
|
| 1311 | 1311 |
return AbsMap<M>(m); |
| 1312 | 1312 |
} |
| 1313 | 1313 |
|
| 1314 | 1314 |
/// @} |
| 1315 | 1315 |
|
| 1316 | 1316 |
// Logical maps and map adaptors: |
| 1317 | 1317 |
|
| 1318 | 1318 |
/// \addtogroup maps |
| 1319 | 1319 |
/// @{
|
| 1320 | 1320 |
|
| 1321 | 1321 |
/// Constant \c true map. |
| 1322 | 1322 |
|
| 1323 | 1323 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
| 1324 | 1324 |
/// each key. |
| 1325 | 1325 |
/// |
| 1326 | 1326 |
/// Note that |
| 1327 | 1327 |
/// \code |
| 1328 | 1328 |
/// TrueMap<K> tm; |
| 1329 | 1329 |
/// \endcode |
| 1330 | 1330 |
/// is equivalent to |
| 1331 | 1331 |
/// \code |
| 1332 | 1332 |
/// ConstMap<K,bool> tm(true); |
| 1333 | 1333 |
/// \endcode |
| 1334 | 1334 |
/// |
| 1335 | 1335 |
/// \sa FalseMap |
| 1336 | 1336 |
/// \sa ConstMap |
| 1337 | 1337 |
template <typename K> |
| 1338 | 1338 |
class TrueMap : public MapBase<K, bool> {
|
| 1339 | 1339 |
public: |
| 1340 | 1340 |
typedef MapBase<K, bool> Parent; |
| 1341 | 1341 |
typedef typename Parent::Key Key; |
| 1342 | 1342 |
typedef typename Parent::Value Value; |
| 1343 | 1343 |
|
| 1344 | 1344 |
/// Gives back \c true. |
| 1345 | 1345 |
Value operator[](const Key&) const { return true; }
|
| 1346 | 1346 |
}; |
| 1347 | 1347 |
|
| 1348 |
/// Returns a \ref TrueMap class |
|
| 1349 |
|
|
| 1350 |
/// |
|
| 1348 |
/// Returns a \c TrueMap class |
|
| 1349 |
|
|
| 1350 |
/// This function just returns a \c TrueMap class. |
|
| 1351 | 1351 |
/// \relates TrueMap |
| 1352 | 1352 |
template<typename K> |
| 1353 | 1353 |
inline TrueMap<K> trueMap() {
|
| 1354 | 1354 |
return TrueMap<K>(); |
| 1355 | 1355 |
} |
| 1356 | 1356 |
|
| 1357 | 1357 |
|
| 1358 | 1358 |
/// Constant \c false map. |
| 1359 | 1359 |
|
| 1360 | 1360 |
/// This \ref concepts::ReadMap "read-only map" assigns \c false to |
| 1361 | 1361 |
/// each key. |
| 1362 | 1362 |
/// |
| 1363 | 1363 |
/// Note that |
| 1364 | 1364 |
/// \code |
| 1365 | 1365 |
/// FalseMap<K> fm; |
| 1366 | 1366 |
/// \endcode |
| 1367 | 1367 |
/// is equivalent to |
| 1368 | 1368 |
/// \code |
| 1369 | 1369 |
/// ConstMap<K,bool> fm(false); |
| 1370 | 1370 |
/// \endcode |
| 1371 | 1371 |
/// |
| 1372 | 1372 |
/// \sa TrueMap |
| 1373 | 1373 |
/// \sa ConstMap |
| 1374 | 1374 |
template <typename K> |
| 1375 | 1375 |
class FalseMap : public MapBase<K, bool> {
|
| 1376 | 1376 |
public: |
| 1377 | 1377 |
typedef MapBase<K, bool> Parent; |
| 1378 | 1378 |
typedef typename Parent::Key Key; |
| 1379 | 1379 |
typedef typename Parent::Value Value; |
| 1380 | 1380 |
|
| 1381 | 1381 |
/// Gives back \c false. |
| 1382 | 1382 |
Value operator[](const Key&) const { return false; }
|
| 1383 | 1383 |
}; |
| 1384 | 1384 |
|
| 1385 |
/// Returns a \ref FalseMap class |
|
| 1386 |
|
|
| 1387 |
/// |
|
| 1385 |
/// Returns a \c FalseMap class |
|
| 1386 |
|
|
| 1387 |
/// This function just returns a \c FalseMap class. |
|
| 1388 | 1388 |
/// \relates FalseMap |
| 1389 | 1389 |
template<typename K> |
| 1390 | 1390 |
inline FalseMap<K> falseMap() {
|
| 1391 | 1391 |
return FalseMap<K>(); |
| 1392 | 1392 |
} |
| 1393 | 1393 |
|
| 1394 | 1394 |
/// @} |
| 1395 | 1395 |
|
| 1396 | 1396 |
/// \addtogroup map_adaptors |
| 1397 | 1397 |
/// @{
|
| 1398 | 1398 |
|
| 1399 | 1399 |
/// Logical 'and' of two maps |
| 1400 | 1400 |
|
| 1401 | 1401 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
| 1402 | 1402 |
/// 'and' of the values of the two given maps. |
| 1403 | 1403 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
| 1404 | 1404 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
| 1405 | 1405 |
/// |
| 1406 | 1406 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 1407 | 1407 |
/// \code |
| 1408 | 1408 |
/// AndMap<M1,M2> am(m1,m2); |
| 1409 | 1409 |
/// \endcode |
| 1410 | 1410 |
/// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>. |
| 1411 | 1411 |
/// |
| 1412 | 1412 |
/// The simplest way of using this map is through the andMap() |
| 1413 | 1413 |
/// function. |
| 1414 | 1414 |
/// |
| 1415 | 1415 |
/// \sa OrMap |
| 1416 | 1416 |
/// \sa NotMap, NotWriteMap |
| 1417 | 1417 |
template<typename M1, typename M2> |
| 1418 | 1418 |
class AndMap : public MapBase<typename M1::Key, bool> {
|
| 1419 | 1419 |
const M1 &_m1; |
| 1420 | 1420 |
const M2 &_m2; |
| 1421 | 1421 |
public: |
| 1422 | 1422 |
typedef MapBase<typename M1::Key, bool> Parent; |
| 1423 | 1423 |
typedef typename Parent::Key Key; |
| 1424 | 1424 |
typedef typename Parent::Value Value; |
| 1425 | 1425 |
|
| 1426 | 1426 |
/// Constructor |
| 1427 | 1427 |
AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1428 | 1428 |
/// \e |
| 1429 | 1429 |
Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
|
| 1430 | 1430 |
}; |
| 1431 | 1431 |
|
| 1432 |
/// Returns an \ref AndMap class |
|
| 1433 |
|
|
| 1434 |
/// |
|
| 1432 |
/// Returns an \c AndMap class |
|
| 1433 |
|
|
| 1434 |
/// This function just returns an \c AndMap class. |
|
| 1435 | 1435 |
/// |
| 1436 | 1436 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values, |
| 1437 | 1437 |
/// then <tt>andMap(m1,m2)[x]</tt> will be equal to |
| 1438 | 1438 |
/// <tt>m1[x]&&m2[x]</tt>. |
| 1439 | 1439 |
/// |
| 1440 | 1440 |
/// \relates AndMap |
| 1441 | 1441 |
template<typename M1, typename M2> |
| 1442 | 1442 |
inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
|
| 1443 | 1443 |
return AndMap<M1, M2>(m1,m2); |
| 1444 | 1444 |
} |
| 1445 | 1445 |
|
| 1446 | 1446 |
|
| 1447 | 1447 |
/// Logical 'or' of two maps |
| 1448 | 1448 |
|
| 1449 | 1449 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
| 1450 | 1450 |
/// 'or' of the values of the two given maps. |
| 1451 | 1451 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
| 1452 | 1452 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
| 1453 | 1453 |
/// |
| 1454 | 1454 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 1455 | 1455 |
/// \code |
| 1456 | 1456 |
/// OrMap<M1,M2> om(m1,m2); |
| 1457 | 1457 |
/// \endcode |
| 1458 | 1458 |
/// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>. |
| 1459 | 1459 |
/// |
| 1460 | 1460 |
/// The simplest way of using this map is through the orMap() |
| 1461 | 1461 |
/// function. |
| 1462 | 1462 |
/// |
| 1463 | 1463 |
/// \sa AndMap |
| 1464 | 1464 |
/// \sa NotMap, NotWriteMap |
| 1465 | 1465 |
template<typename M1, typename M2> |
| 1466 | 1466 |
class OrMap : public MapBase<typename M1::Key, bool> {
|
| 1467 | 1467 |
const M1 &_m1; |
| 1468 | 1468 |
const M2 &_m2; |
| 1469 | 1469 |
public: |
| 1470 | 1470 |
typedef MapBase<typename M1::Key, bool> Parent; |
| 1471 | 1471 |
typedef typename Parent::Key Key; |
| 1472 | 1472 |
typedef typename Parent::Value Value; |
| 1473 | 1473 |
|
| 1474 | 1474 |
/// Constructor |
| 1475 | 1475 |
OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1476 | 1476 |
/// \e |
| 1477 | 1477 |
Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
|
| 1478 | 1478 |
}; |
| 1479 | 1479 |
|
| 1480 |
/// Returns an \ref OrMap class |
|
| 1481 |
|
|
| 1482 |
/// |
|
| 1480 |
/// Returns an \c OrMap class |
|
| 1481 |
|
|
| 1482 |
/// This function just returns an \c OrMap class. |
|
| 1483 | 1483 |
/// |
| 1484 | 1484 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values, |
| 1485 | 1485 |
/// then <tt>orMap(m1,m2)[x]</tt> will be equal to |
| 1486 | 1486 |
/// <tt>m1[x]||m2[x]</tt>. |
| 1487 | 1487 |
/// |
| 1488 | 1488 |
/// \relates OrMap |
| 1489 | 1489 |
template<typename M1, typename M2> |
| 1490 | 1490 |
inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
|
| 1491 | 1491 |
return OrMap<M1, M2>(m1,m2); |
| 1492 | 1492 |
} |
| 1493 | 1493 |
|
| 1494 | 1494 |
|
| 1495 | 1495 |
/// Logical 'not' of a map |
| 1496 | 1496 |
|
| 1497 | 1497 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
| 1498 | 1498 |
/// negation of the values of the given map. |
| 1499 | 1499 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool. |
| 1500 | 1500 |
/// |
| 1501 | 1501 |
/// The simplest way of using this map is through the notMap() |
| 1502 | 1502 |
/// function. |
| 1503 | 1503 |
/// |
| 1504 | 1504 |
/// \sa NotWriteMap |
| 1505 | 1505 |
template <typename M> |
| 1506 | 1506 |
class NotMap : public MapBase<typename M::Key, bool> {
|
| 1507 | 1507 |
const M &_m; |
| 1508 | 1508 |
public: |
| 1509 | 1509 |
typedef MapBase<typename M::Key, bool> Parent; |
| 1510 | 1510 |
typedef typename Parent::Key Key; |
| 1511 | 1511 |
typedef typename Parent::Value Value; |
| 1512 | 1512 |
|
| 1513 | 1513 |
/// Constructor |
| 1514 | 1514 |
NotMap(const M &m) : _m(m) {}
|
| 1515 | 1515 |
/// \e |
| 1516 | 1516 |
Value operator[](const Key &k) const { return !_m[k]; }
|
| 1517 | 1517 |
}; |
| 1518 | 1518 |
|
| 1519 | 1519 |
/// Logical 'not' of a map (read-write version) |
| 1520 | 1520 |
|
| 1521 | 1521 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the |
| 1522 | 1522 |
/// logical negation of the values of the given map. |
| 1523 | 1523 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool. |
| 1524 | 1524 |
/// It makes also possible to write the map. When a value is set, |
| 1525 | 1525 |
/// the opposite value is set to the original map. |
| 1526 | 1526 |
/// |
| 1527 | 1527 |
/// The simplest way of using this map is through the notWriteMap() |
| 1528 | 1528 |
/// function. |
| 1529 | 1529 |
/// |
| 1530 | 1530 |
/// \sa NotMap |
| 1531 | 1531 |
template <typename M> |
| 1532 | 1532 |
class NotWriteMap : public MapBase<typename M::Key, bool> {
|
| 1533 | 1533 |
M &_m; |
| 1534 | 1534 |
public: |
| 1535 | 1535 |
typedef MapBase<typename M::Key, bool> Parent; |
| 1536 | 1536 |
typedef typename Parent::Key Key; |
| 1537 | 1537 |
typedef typename Parent::Value Value; |
| 1538 | 1538 |
|
| 1539 | 1539 |
/// Constructor |
| 1540 | 1540 |
NotWriteMap(M &m) : _m(m) {}
|
| 1541 | 1541 |
/// \e |
| 1542 | 1542 |
Value operator[](const Key &k) const { return !_m[k]; }
|
| 1543 | 1543 |
/// \e |
| 1544 | 1544 |
void set(const Key &k, bool v) { _m.set(k, !v); }
|
| 1545 | 1545 |
}; |
| 1546 | 1546 |
|
| 1547 |
/// Returns a \ref NotMap class |
|
| 1548 |
|
|
| 1549 |
/// |
|
| 1547 |
/// Returns a \c NotMap class |
|
| 1548 |
|
|
| 1549 |
/// This function just returns a \c NotMap class. |
|
| 1550 | 1550 |
/// |
| 1551 | 1551 |
/// For example, if \c m is a map with \c bool values, then |
| 1552 | 1552 |
/// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
| 1553 | 1553 |
/// |
| 1554 | 1554 |
/// \relates NotMap |
| 1555 | 1555 |
template <typename M> |
| 1556 | 1556 |
inline NotMap<M> notMap(const M &m) {
|
| 1557 | 1557 |
return NotMap<M>(m); |
| 1558 | 1558 |
} |
| 1559 | 1559 |
|
| 1560 |
/// Returns a \ref NotWriteMap class |
|
| 1561 |
|
|
| 1562 |
/// |
|
| 1560 |
/// Returns a \c NotWriteMap class |
|
| 1561 |
|
|
| 1562 |
/// This function just returns a \c NotWriteMap class. |
|
| 1563 | 1563 |
/// |
| 1564 | 1564 |
/// For example, if \c m is a map with \c bool values, then |
| 1565 | 1565 |
/// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
| 1566 | 1566 |
/// Moreover it makes also possible to write the map. |
| 1567 | 1567 |
/// |
| 1568 | 1568 |
/// \relates NotWriteMap |
| 1569 | 1569 |
template <typename M> |
| 1570 | 1570 |
inline NotWriteMap<M> notWriteMap(M &m) {
|
| 1571 | 1571 |
return NotWriteMap<M>(m); |
| 1572 | 1572 |
} |
| 1573 | 1573 |
|
| 1574 | 1574 |
|
| 1575 | 1575 |
/// Combination of two maps using the \c == operator |
| 1576 | 1576 |
|
| 1577 | 1577 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
| 1578 | 1578 |
/// the keys for which the corresponding values of the two maps are |
| 1579 | 1579 |
/// equal. |
| 1580 | 1580 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
| 1581 | 1581 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
| 1582 | 1582 |
/// |
| 1583 | 1583 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 1584 | 1584 |
/// \code |
| 1585 | 1585 |
/// EqualMap<M1,M2> em(m1,m2); |
| 1586 | 1586 |
/// \endcode |
| 1587 | 1587 |
/// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>. |
| 1588 | 1588 |
/// |
| 1589 | 1589 |
/// The simplest way of using this map is through the equalMap() |
| 1590 | 1590 |
/// function. |
| 1591 | 1591 |
/// |
| 1592 | 1592 |
/// \sa LessMap |
| 1593 | 1593 |
template<typename M1, typename M2> |
| 1594 | 1594 |
class EqualMap : public MapBase<typename M1::Key, bool> {
|
| 1595 | 1595 |
const M1 &_m1; |
| 1596 | 1596 |
const M2 &_m2; |
| 1597 | 1597 |
public: |
| 1598 | 1598 |
typedef MapBase<typename M1::Key, bool> Parent; |
| 1599 | 1599 |
typedef typename Parent::Key Key; |
| 1600 | 1600 |
typedef typename Parent::Value Value; |
| 1601 | 1601 |
|
| 1602 | 1602 |
/// Constructor |
| 1603 | 1603 |
EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1604 | 1604 |
/// \e |
| 1605 | 1605 |
Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
|
| 1606 | 1606 |
}; |
| 1607 | 1607 |
|
| 1608 |
/// Returns an \ref EqualMap class |
|
| 1609 |
|
|
| 1610 |
/// |
|
| 1608 |
/// Returns an \c EqualMap class |
|
| 1609 |
|
|
| 1610 |
/// This function just returns an \c EqualMap class. |
|
| 1611 | 1611 |
/// |
| 1612 | 1612 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
| 1613 | 1613 |
/// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to |
| 1614 | 1614 |
/// <tt>m1[x]==m2[x]</tt>. |
| 1615 | 1615 |
/// |
| 1616 | 1616 |
/// \relates EqualMap |
| 1617 | 1617 |
template<typename M1, typename M2> |
| 1618 | 1618 |
inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
|
| 1619 | 1619 |
return EqualMap<M1, M2>(m1,m2); |
| 1620 | 1620 |
} |
| 1621 | 1621 |
|
| 1622 | 1622 |
|
| 1623 | 1623 |
/// Combination of two maps using the \c < operator |
| 1624 | 1624 |
|
| 1625 | 1625 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
| 1626 | 1626 |
/// the keys for which the corresponding value of the first map is |
| 1627 | 1627 |
/// less then the value of the second map. |
| 1628 | 1628 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
| 1629 | 1629 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
| 1630 | 1630 |
/// |
| 1631 | 1631 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
| 1632 | 1632 |
/// \code |
| 1633 | 1633 |
/// LessMap<M1,M2> lm(m1,m2); |
| 1634 | 1634 |
/// \endcode |
| 1635 | 1635 |
/// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>. |
| 1636 | 1636 |
/// |
| 1637 | 1637 |
/// The simplest way of using this map is through the lessMap() |
| 1638 | 1638 |
/// function. |
| 1639 | 1639 |
/// |
| 1640 | 1640 |
/// \sa EqualMap |
| 1641 | 1641 |
template<typename M1, typename M2> |
| 1642 | 1642 |
class LessMap : public MapBase<typename M1::Key, bool> {
|
| 1643 | 1643 |
const M1 &_m1; |
| 1644 | 1644 |
const M2 &_m2; |
| 1645 | 1645 |
public: |
| 1646 | 1646 |
typedef MapBase<typename M1::Key, bool> Parent; |
| 1647 | 1647 |
typedef typename Parent::Key Key; |
| 1648 | 1648 |
typedef typename Parent::Value Value; |
| 1649 | 1649 |
|
| 1650 | 1650 |
/// Constructor |
| 1651 | 1651 |
LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
|
| 1652 | 1652 |
/// \e |
| 1653 | 1653 |
Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
|
| 1654 | 1654 |
}; |
| 1655 | 1655 |
|
| 1656 |
/// Returns an \ref LessMap class |
|
| 1657 |
|
|
| 1658 |
/// |
|
| 1656 |
/// Returns an \c LessMap class |
|
| 1657 |
|
|
| 1658 |
/// This function just returns an \c LessMap class. |
|
| 1659 | 1659 |
/// |
| 1660 | 1660 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
| 1661 | 1661 |
/// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to |
| 1662 | 1662 |
/// <tt>m1[x]<m2[x]</tt>. |
| 1663 | 1663 |
/// |
| 1664 | 1664 |
/// \relates LessMap |
| 1665 | 1665 |
template<typename M1, typename M2> |
| 1666 | 1666 |
inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
|
| 1667 | 1667 |
return LessMap<M1, M2>(m1,m2); |
| 1668 | 1668 |
} |
| 1669 | 1669 |
|
| 1670 | 1670 |
namespace _maps_bits {
|
| 1671 | 1671 |
|
| 1672 | 1672 |
template <typename _Iterator, typename Enable = void> |
| 1673 | 1673 |
struct IteratorTraits {
|
| 1674 | 1674 |
typedef typename std::iterator_traits<_Iterator>::value_type Value; |
| 1675 | 1675 |
}; |
| 1676 | 1676 |
|
| 1677 | 1677 |
template <typename _Iterator> |
| 1678 | 1678 |
struct IteratorTraits<_Iterator, |
| 1679 | 1679 |
typename exists<typename _Iterator::container_type>::type> |
| 1680 | 1680 |
{
|
| 1681 | 1681 |
typedef typename _Iterator::container_type::value_type Value; |
| 1682 | 1682 |
}; |
| 1683 | 1683 |
|
| 1684 | 1684 |
} |
| 1685 | 1685 |
|
| 1686 |
/// @} |
|
| 1687 |
|
|
| 1688 |
/// \addtogroup maps |
|
| 1689 |
/// @{
|
|
| 1690 |
|
|
| 1686 | 1691 |
/// \brief Writable bool map for logging each \c true assigned element |
| 1687 | 1692 |
/// |
| 1688 | 1693 |
/// A \ref concepts::WriteMap "writable" bool map for logging |
| 1689 | 1694 |
/// each \c true assigned element, i.e it copies subsequently each |
| 1690 | 1695 |
/// keys set to \c true to the given iterator. |
| 1691 | 1696 |
/// The most important usage of it is storing certain nodes or arcs |
| 1692 | 1697 |
/// that were marked \c true by an algorithm. |
| 1693 | 1698 |
/// |
| 1694 | 1699 |
/// There are several algorithms that provide solutions through bool |
| 1695 | 1700 |
/// maps and most of them assign \c true at most once for each key. |
| 1696 | 1701 |
/// In these cases it is a natural request to store each \c true |
| 1697 | 1702 |
/// assigned elements (in order of the assignment), which can be |
| 1698 | 1703 |
/// easily done with LoggerBoolMap. |
| 1699 | 1704 |
/// |
| 1700 | 1705 |
/// The simplest way of using this map is through the loggerBoolMap() |
| 1701 | 1706 |
/// function. |
| 1702 | 1707 |
/// |
| 1703 | 1708 |
/// \tparam It The type of the iterator. |
| 1704 | 1709 |
/// \tparam Ke The key type of the map. The default value set |
| 1705 | 1710 |
/// according to the iterator type should work in most cases. |
| 1706 | 1711 |
/// |
| 1707 | 1712 |
/// \note The container of the iterator must contain enough space |
| 1708 | 1713 |
/// for the elements or the iterator should be an inserter iterator. |
| 1709 | 1714 |
#ifdef DOXYGEN |
| 1710 | 1715 |
template <typename It, typename Ke> |
| 1711 | 1716 |
#else |
| 1712 | 1717 |
template <typename It, |
| 1713 | 1718 |
typename Ke=typename _maps_bits::IteratorTraits<It>::Value> |
| 1714 | 1719 |
#endif |
| 1715 | 1720 |
class LoggerBoolMap {
|
| 1716 | 1721 |
public: |
| 1717 | 1722 |
typedef It Iterator; |
| 1718 | 1723 |
|
| 1719 | 1724 |
typedef Ke Key; |
| 1720 | 1725 |
typedef bool Value; |
| 1721 | 1726 |
|
| 1722 | 1727 |
/// Constructor |
| 1723 | 1728 |
LoggerBoolMap(Iterator it) |
| 1724 | 1729 |
: _begin(it), _end(it) {}
|
| 1725 | 1730 |
|
| 1726 | 1731 |
/// Gives back the given iterator set for the first key |
| 1727 | 1732 |
Iterator begin() const {
|
| 1728 | 1733 |
return _begin; |
| 1729 | 1734 |
} |
| 1730 | 1735 |
|
| 1731 | 1736 |
/// Gives back the the 'after the last' iterator |
| 1732 | 1737 |
Iterator end() const {
|
| 1733 | 1738 |
return _end; |
| 1734 | 1739 |
} |
| 1735 | 1740 |
|
| 1736 | 1741 |
/// The set function of the map |
| 1737 | 1742 |
void set(const Key& key, Value value) {
|
| 1738 | 1743 |
if (value) {
|
| 1739 | 1744 |
*_end++ = key; |
| 1740 | 1745 |
} |
| 1741 | 1746 |
} |
| 1742 | 1747 |
|
| 1743 | 1748 |
private: |
| 1744 | 1749 |
Iterator _begin; |
| 1745 | 1750 |
Iterator _end; |
| 1746 | 1751 |
}; |
| 1747 | 1752 |
|
| 1748 |
/// Returns a \ref LoggerBoolMap class |
|
| 1749 |
|
|
| 1750 |
/// |
|
| 1753 |
/// Returns a \c LoggerBoolMap class |
|
| 1754 |
|
|
| 1755 |
/// This function just returns a \c LoggerBoolMap class. |
|
| 1751 | 1756 |
/// |
| 1752 | 1757 |
/// The most important usage of it is storing certain nodes or arcs |
| 1753 | 1758 |
/// that were marked \c true by an algorithm. |
| 1754 | 1759 |
/// For example it makes easier to store the nodes in the processing |
| 1755 | 1760 |
/// order of Dfs algorithm, as the following examples show. |
| 1756 | 1761 |
/// \code |
| 1757 | 1762 |
/// std::vector<Node> v; |
| 1758 | 1763 |
/// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run(); |
| 1759 | 1764 |
/// \endcode |
| 1760 | 1765 |
/// \code |
| 1761 | 1766 |
/// std::vector<Node> v(countNodes(g)); |
| 1762 | 1767 |
/// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run(); |
| 1763 | 1768 |
/// \endcode |
| 1764 | 1769 |
/// |
| 1765 | 1770 |
/// \note The container of the iterator must contain enough space |
| 1766 | 1771 |
/// for the elements or the iterator should be an inserter iterator. |
| 1767 | 1772 |
/// |
| 1768 | 1773 |
/// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so |
| 1769 | 1774 |
/// it cannot be used when a readable map is needed, for example as |
| 1770 |
/// \c ReachedMap for \ |
|
| 1775 |
/// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms. |
|
| 1771 | 1776 |
/// |
| 1772 | 1777 |
/// \relates LoggerBoolMap |
| 1773 | 1778 |
template<typename Iterator> |
| 1774 | 1779 |
inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
|
| 1775 | 1780 |
return LoggerBoolMap<Iterator>(it); |
| 1776 | 1781 |
} |
| 1777 | 1782 |
|
| 1783 |
/// @} |
|
| 1784 |
|
|
| 1785 |
/// \addtogroup graph_maps |
|
| 1786 |
/// @{
|
|
| 1787 |
|
|
| 1778 | 1788 |
/// Provides an immutable and unique id for each item in the graph. |
| 1779 | 1789 |
|
| 1780 | 1790 |
/// The IdMap class provides a unique and immutable id for each item of the |
| 1781 | 1791 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
| 1782 | 1792 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
| 1783 | 1793 |
/// item (node) does not change (even if you delete other nodes). </ul> |
| 1784 | 1794 |
/// Through this map you get access (i.e. can read) the inner id values of |
| 1785 | 1795 |
/// the items stored in the graph. This map can be inverted with its member |
| 1786 | 1796 |
/// class \c InverseMap or with the \c operator() member. |
| 1787 | 1797 |
/// |
| 1788 | 1798 |
template <typename _Graph, typename _Item> |
| 1789 | 1799 |
class IdMap {
|
| 1790 | 1800 |
public: |
| 1791 | 1801 |
typedef _Graph Graph; |
| 1792 | 1802 |
typedef int Value; |
| 1793 | 1803 |
typedef _Item Item; |
| 1794 | 1804 |
typedef _Item Key; |
| 1795 | 1805 |
|
| 1796 | 1806 |
/// \brief Constructor. |
| 1797 | 1807 |
/// |
| 1798 | 1808 |
/// Constructor of the map. |
| 1799 | 1809 |
explicit IdMap(const Graph& graph) : _graph(&graph) {}
|
| 1800 | 1810 |
|
| 1801 | 1811 |
/// \brief Gives back the \e id of the item. |
| 1802 | 1812 |
/// |
| 1803 | 1813 |
/// Gives back the immutable and unique \e id of the item. |
| 1804 | 1814 |
int operator[](const Item& item) const { return _graph->id(item);}
|
| 1805 | 1815 |
|
| 1806 | 1816 |
/// \brief Gives back the item by its id. |
| 1807 | 1817 |
/// |
| 1808 | 1818 |
/// Gives back the item by its id. |
| 1809 | 1819 |
Item operator()(int id) { return _graph->fromId(id, Item()); }
|
| 1810 | 1820 |
|
| 1811 | 1821 |
private: |
| 1812 | 1822 |
const Graph* _graph; |
| 1813 | 1823 |
|
| 1814 | 1824 |
public: |
| 1815 | 1825 |
|
| 1816 | 1826 |
/// \brief The class represents the inverse of its owner (IdMap). |
| 1817 | 1827 |
/// |
| 1818 | 1828 |
/// The class represents the inverse of its owner (IdMap). |
| 1819 | 1829 |
/// \see inverse() |
| 1820 | 1830 |
class InverseMap {
|
| 1821 | 1831 |
public: |
| 1822 | 1832 |
|
| 1823 | 1833 |
/// \brief Constructor. |
| 1824 | 1834 |
/// |
| 1825 | 1835 |
/// Constructor for creating an id-to-item map. |
| 1826 | 1836 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {}
|
| 1827 | 1837 |
|
| 1828 | 1838 |
/// \brief Constructor. |
| 1829 | 1839 |
/// |
| 1830 | 1840 |
/// Constructor for creating an id-to-item map. |
| 1831 | 1841 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
|
| 1832 | 1842 |
|
| 1833 | 1843 |
/// \brief Gives back the given item from its id. |
| 1834 | 1844 |
/// |
| 1835 | 1845 |
/// Gives back the given item from its id. |
| 1836 | 1846 |
/// |
| 1837 | 1847 |
Item operator[](int id) const { return _graph->fromId(id, Item());}
|
| 1838 | 1848 |
|
| 1839 | 1849 |
private: |
| 1840 | 1850 |
const Graph* _graph; |
| 1841 | 1851 |
}; |
| 1842 | 1852 |
|
| 1843 | 1853 |
/// \brief Gives back the inverse of the map. |
| 1844 | 1854 |
/// |
| 1845 | 1855 |
/// Gives back the inverse of the IdMap. |
| 1846 | 1856 |
InverseMap inverse() const { return InverseMap(*_graph);}
|
| 1847 | 1857 |
|
| 1848 | 1858 |
}; |
| 1849 | 1859 |
|
| 1850 | 1860 |
|
| 1851 | 1861 |
/// \brief General invertable graph-map type. |
| 1852 | 1862 |
|
| 1853 | 1863 |
/// This type provides simple invertable graph-maps. |
| 1854 | 1864 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
| 1855 | 1865 |
/// and if a key is set to a new value then store it |
| 1856 | 1866 |
/// in the inverse map. |
| 1857 | 1867 |
/// |
| 1858 | 1868 |
/// The values of the map can be accessed |
| 1859 | 1869 |
/// with stl compatible forward iterator. |
| 1860 | 1870 |
/// |
| 1861 | 1871 |
/// \tparam _Graph The graph type. |
| 1862 | 1872 |
/// \tparam _Item The item type of the graph. |
| 1863 | 1873 |
/// \tparam _Value The value type of the map. |
| 1864 | 1874 |
/// |
| 1865 | 1875 |
/// \see IterableValueMap |
| 1866 | 1876 |
template <typename _Graph, typename _Item, typename _Value> |
| 1867 | 1877 |
class InvertableMap |
| 1868 | 1878 |
: protected ItemSetTraits<_Graph, _Item>::template Map<_Value>::Type {
|
| 1869 | 1879 |
private: |
| 1870 | 1880 |
|
| 1871 | 1881 |
typedef typename ItemSetTraits<_Graph, _Item>:: |
| 1872 | 1882 |
template Map<_Value>::Type Map; |
| 1873 | 1883 |
typedef _Graph Graph; |
| 1874 | 1884 |
|
| 1875 | 1885 |
typedef std::map<_Value, _Item> Container; |
| 1876 | 1886 |
Container _inv_map; |
| 1877 | 1887 |
|
| 1878 | 1888 |
public: |
| 1879 | 1889 |
|
| 1880 | 1890 |
/// The key type of InvertableMap (Node, Arc, Edge). |
| 1881 | 1891 |
typedef typename Map::Key Key; |
| 1882 | 1892 |
/// The value type of the InvertableMap. |
| 1883 | 1893 |
typedef typename Map::Value Value; |
| 1884 | 1894 |
|
| 1885 |
|
|
| 1886 |
|
|
| 1887 | 1895 |
/// \brief Constructor. |
| 1888 | 1896 |
/// |
| 1889 | 1897 |
/// Construct a new InvertableMap for the graph. |
| 1890 | 1898 |
/// |
| 1891 | 1899 |
explicit InvertableMap(const Graph& graph) : Map(graph) {}
|
| 1892 | 1900 |
|
| 1893 | 1901 |
/// \brief Forward iterator for values. |
| 1894 | 1902 |
/// |
| 1895 | 1903 |
/// This iterator is an stl compatible forward |
| 1896 | 1904 |
/// iterator on the values of the map. The values can |
| 1897 | 1905 |
/// be accessed in the [beginValue, endValue) range. |
| 1898 | 1906 |
/// |
| 1899 | 1907 |
class ValueIterator |
| 1900 | 1908 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 1901 | 1909 |
friend class InvertableMap; |
| 1902 | 1910 |
private: |
| 1903 | 1911 |
ValueIterator(typename Container::const_iterator _it) |
| 1904 | 1912 |
: it(_it) {}
|
| 1905 | 1913 |
public: |
| 1906 | 1914 |
|
| 1907 | 1915 |
ValueIterator() {}
|
| 1908 | 1916 |
|
| 1909 | 1917 |
ValueIterator& operator++() { ++it; return *this; }
|
| 1910 | 1918 |
ValueIterator operator++(int) {
|
| 1911 | 1919 |
ValueIterator tmp(*this); |
| 1912 | 1920 |
operator++(); |
| 1913 | 1921 |
return tmp; |
| 1914 | 1922 |
} |
| 1915 | 1923 |
|
| 1916 | 1924 |
const Value& operator*() const { return it->first; }
|
| 1917 | 1925 |
const Value* operator->() const { return &(it->first); }
|
| 1918 | 1926 |
|
| 1919 | 1927 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
| 1920 | 1928 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
| 1921 | 1929 |
|
| 1922 | 1930 |
private: |
| 1923 | 1931 |
typename Container::const_iterator it; |
| 1924 | 1932 |
}; |
| 1925 | 1933 |
|
| 1926 | 1934 |
/// \brief Returns an iterator to the first value. |
| 1927 | 1935 |
/// |
| 1928 | 1936 |
/// Returns an stl compatible iterator to the |
| 1929 | 1937 |
/// first value of the map. The values of the |
| 1930 | 1938 |
/// map can be accessed in the [beginValue, endValue) |
| 1931 | 1939 |
/// range. |
| 1932 | 1940 |
ValueIterator beginValue() const {
|
| 1933 | 1941 |
return ValueIterator(_inv_map.begin()); |
| 1934 | 1942 |
} |
| 1935 | 1943 |
|
| 1936 | 1944 |
/// \brief Returns an iterator after the last value. |
| 1937 | 1945 |
/// |
| 1938 | 1946 |
/// Returns an stl compatible iterator after the |
| 1939 | 1947 |
/// last value of the map. The values of the |
| 1940 | 1948 |
/// map can be accessed in the [beginValue, endValue) |
| 1941 | 1949 |
/// range. |
| 1942 | 1950 |
ValueIterator endValue() const {
|
| 1943 | 1951 |
return ValueIterator(_inv_map.end()); |
| 1944 | 1952 |
} |
| 1945 | 1953 |
|
| 1946 | 1954 |
/// \brief The setter function of the map. |
| 1947 | 1955 |
/// |
| 1948 | 1956 |
/// Sets the mapped value. |
| 1949 | 1957 |
void set(const Key& key, const Value& val) {
|
| 1950 | 1958 |
Value oldval = Map::operator[](key); |
| 1951 | 1959 |
typename Container::iterator it = _inv_map.find(oldval); |
| 1952 | 1960 |
if (it != _inv_map.end() && it->second == key) {
|
| 1953 | 1961 |
_inv_map.erase(it); |
| 1954 | 1962 |
} |
| 1955 | 1963 |
_inv_map.insert(make_pair(val, key)); |
| 1956 | 1964 |
Map::set(key, val); |
| 1957 | 1965 |
} |
| 1958 | 1966 |
|
| 1959 | 1967 |
/// \brief The getter function of the map. |
| 1960 | 1968 |
/// |
| 1961 | 1969 |
/// It gives back the value associated with the key. |
| 1962 | 1970 |
typename MapTraits<Map>::ConstReturnValue |
| 1963 | 1971 |
operator[](const Key& key) const {
|
| 1964 | 1972 |
return Map::operator[](key); |
| 1965 | 1973 |
} |
| 1966 | 1974 |
|
| 1967 | 1975 |
/// \brief Gives back the item by its value. |
| 1968 | 1976 |
/// |
| 1969 | 1977 |
/// Gives back the item by its value. |
| 1970 | 1978 |
Key operator()(const Value& key) const {
|
| 1971 | 1979 |
typename Container::const_iterator it = _inv_map.find(key); |
| 1972 | 1980 |
return it != _inv_map.end() ? it->second : INVALID; |
| 1973 | 1981 |
} |
| 1974 | 1982 |
|
| 1975 | 1983 |
protected: |
| 1976 | 1984 |
|
| 1977 | 1985 |
/// \brief Erase the key from the map. |
| 1978 | 1986 |
/// |
| 1979 | 1987 |
/// Erase the key to the map. It is called by the |
| 1980 | 1988 |
/// \c AlterationNotifier. |
| 1981 | 1989 |
virtual void erase(const Key& key) {
|
| 1982 | 1990 |
Value val = Map::operator[](key); |
| 1983 | 1991 |
typename Container::iterator it = _inv_map.find(val); |
| 1984 | 1992 |
if (it != _inv_map.end() && it->second == key) {
|
| 1985 | 1993 |
_inv_map.erase(it); |
| 1986 | 1994 |
} |
| 1987 | 1995 |
Map::erase(key); |
| 1988 | 1996 |
} |
| 1989 | 1997 |
|
| 1990 | 1998 |
/// \brief Erase more keys from the map. |
| 1991 | 1999 |
/// |
| 1992 | 2000 |
/// Erase more keys from the map. It is called by the |
| 1993 | 2001 |
/// \c AlterationNotifier. |
| 1994 | 2002 |
virtual void erase(const std::vector<Key>& keys) {
|
| 1995 | 2003 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 1996 | 2004 |
Value val = Map::operator[](keys[i]); |
| 1997 | 2005 |
typename Container::iterator it = _inv_map.find(val); |
| 1998 | 2006 |
if (it != _inv_map.end() && it->second == keys[i]) {
|
| 1999 | 2007 |
_inv_map.erase(it); |
| 2000 | 2008 |
} |
| 2001 | 2009 |
} |
| 2002 | 2010 |
Map::erase(keys); |
| 2003 | 2011 |
} |
| 2004 | 2012 |
|
| 2005 | 2013 |
/// \brief Clear the keys from the map and inverse map. |
| 2006 | 2014 |
/// |
| 2007 | 2015 |
/// Clear the keys from the map and inverse map. It is called by the |
| 2008 | 2016 |
/// \c AlterationNotifier. |
| 2009 | 2017 |
virtual void clear() {
|
| 2010 | 2018 |
_inv_map.clear(); |
| 2011 | 2019 |
Map::clear(); |
| 2012 | 2020 |
} |
| 2013 | 2021 |
|
| 2014 | 2022 |
public: |
| 2015 | 2023 |
|
| 2016 | 2024 |
/// \brief The inverse map type. |
| 2017 | 2025 |
/// |
| 2018 | 2026 |
/// The inverse of this map. The subscript operator of the map |
| 2019 | 2027 |
/// gives back always the item what was last assigned to the value. |
| 2020 | 2028 |
class InverseMap {
|
| 2021 | 2029 |
public: |
| 2022 | 2030 |
/// \brief Constructor of the InverseMap. |
| 2023 | 2031 |
/// |
| 2024 | 2032 |
/// Constructor of the InverseMap. |
| 2025 | 2033 |
explicit InverseMap(const InvertableMap& inverted) |
| 2026 | 2034 |
: _inverted(inverted) {}
|
| 2027 | 2035 |
|
| 2028 | 2036 |
/// The value type of the InverseMap. |
| 2029 | 2037 |
typedef typename InvertableMap::Key Value; |
| 2030 | 2038 |
/// The key type of the InverseMap. |
| 2031 | 2039 |
typedef typename InvertableMap::Value Key; |
| 2032 | 2040 |
|
| 2033 | 2041 |
/// \brief Subscript operator. |
| 2034 | 2042 |
/// |
| 2035 | 2043 |
/// Subscript operator. It gives back always the item |
| 2036 | 2044 |
/// what was last assigned to the value. |
| 2037 | 2045 |
Value operator[](const Key& key) const {
|
| 2038 | 2046 |
return _inverted(key); |
| 2039 | 2047 |
} |
| 2040 | 2048 |
|
| 2041 | 2049 |
private: |
| 2042 | 2050 |
const InvertableMap& _inverted; |
| 2043 | 2051 |
}; |
| 2044 | 2052 |
|
| 2045 | 2053 |
/// \brief It gives back the just readable inverse map. |
| 2046 | 2054 |
/// |
| 2047 | 2055 |
/// It gives back the just readable inverse map. |
| 2048 | 2056 |
InverseMap inverse() const {
|
| 2049 | 2057 |
return InverseMap(*this); |
| 2050 | 2058 |
} |
| 2051 | 2059 |
|
| 2052 |
|
|
| 2053 |
|
|
| 2054 | 2060 |
}; |
| 2055 | 2061 |
|
| 2056 | 2062 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
| 2057 | 2063 |
/// item in the graph. |
| 2058 | 2064 |
/// |
| 2059 | 2065 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
| 2060 | 2066 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
| 2061 | 2067 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get |
| 2062 | 2068 |
/// different ids <li>\b continuous: the range of the ids is the set of |
| 2063 | 2069 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
| 2064 | 2070 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
| 2065 | 2071 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
| 2066 | 2072 |
/// with its member class \c InverseMap, or with the \c operator() member. |
| 2067 | 2073 |
/// |
| 2068 | 2074 |
/// \tparam _Graph The graph class the \c DescriptorMap belongs to. |
| 2069 | 2075 |
/// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or |
| 2070 | 2076 |
/// Edge. |
| 2071 | 2077 |
template <typename _Graph, typename _Item> |
| 2072 | 2078 |
class DescriptorMap |
| 2073 | 2079 |
: protected ItemSetTraits<_Graph, _Item>::template Map<int>::Type {
|
| 2074 | 2080 |
|
| 2075 | 2081 |
typedef _Item Item; |
| 2076 | 2082 |
typedef typename ItemSetTraits<_Graph, _Item>::template Map<int>::Type Map; |
| 2077 | 2083 |
|
| 2078 | 2084 |
public: |
| 2079 | 2085 |
/// The graph class of DescriptorMap. |
| 2080 | 2086 |
typedef _Graph Graph; |
| 2081 | 2087 |
|
| 2082 | 2088 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
| 2083 | 2089 |
typedef typename Map::Key Key; |
| 2084 | 2090 |
/// The value type of DescriptorMap. |
| 2085 | 2091 |
typedef typename Map::Value Value; |
| 2086 | 2092 |
|
| 2087 | 2093 |
/// \brief Constructor. |
| 2088 | 2094 |
/// |
| 2089 | 2095 |
/// Constructor for descriptor map. |
| 2090 | 2096 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
|
| 2091 | 2097 |
Item it; |
| 2092 | 2098 |
const typename Map::Notifier* nf = Map::notifier(); |
| 2093 | 2099 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2094 | 2100 |
Map::set(it, _inv_map.size()); |
| 2095 | 2101 |
_inv_map.push_back(it); |
| 2096 | 2102 |
} |
| 2097 | 2103 |
} |
| 2098 | 2104 |
|
| 2099 | 2105 |
protected: |
| 2100 | 2106 |
|
| 2101 | 2107 |
/// \brief Add a new key to the map. |
| 2102 | 2108 |
/// |
| 2103 | 2109 |
/// Add a new key to the map. It is called by the |
| 2104 | 2110 |
/// \c AlterationNotifier. |
| 2105 | 2111 |
virtual void add(const Item& item) {
|
| 2106 | 2112 |
Map::add(item); |
| 2107 | 2113 |
Map::set(item, _inv_map.size()); |
| 2108 | 2114 |
_inv_map.push_back(item); |
| 2109 | 2115 |
} |
| 2110 | 2116 |
|
| 2111 | 2117 |
/// \brief Add more new keys to the map. |
| 2112 | 2118 |
/// |
| 2113 | 2119 |
/// Add more new keys to the map. It is called by the |
| 2114 | 2120 |
/// \c AlterationNotifier. |
| 2115 | 2121 |
virtual void add(const std::vector<Item>& items) {
|
| 2116 | 2122 |
Map::add(items); |
| 2117 | 2123 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 2118 | 2124 |
Map::set(items[i], _inv_map.size()); |
| 2119 | 2125 |
_inv_map.push_back(items[i]); |
| 2120 | 2126 |
} |
| 2121 | 2127 |
} |
| 2122 | 2128 |
|
| 2123 | 2129 |
/// \brief Erase the key from the map. |
| 2124 | 2130 |
/// |
| 2125 | 2131 |
/// Erase the key from the map. It is called by the |
| 2126 | 2132 |
/// \c AlterationNotifier. |
| 2127 | 2133 |
virtual void erase(const Item& item) {
|
| 2128 | 2134 |
Map::set(_inv_map.back(), Map::operator[](item)); |
| 2129 | 2135 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
| 2130 | 2136 |
_inv_map.pop_back(); |
| 2131 | 2137 |
Map::erase(item); |
| 2132 | 2138 |
} |
| 2133 | 2139 |
|
| 2134 | 2140 |
/// \brief Erase more keys from the map. |
| 2135 | 2141 |
/// |
| 2136 | 2142 |
/// Erase more keys from the map. It is called by the |
| 2137 | 2143 |
/// \c AlterationNotifier. |
| 2138 | 2144 |
virtual void erase(const std::vector<Item>& items) {
|
| 2139 | 2145 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 2140 | 2146 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
| 2141 | 2147 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
| 2142 | 2148 |
_inv_map.pop_back(); |
| 2143 | 2149 |
} |
| 2144 | 2150 |
Map::erase(items); |
| 2145 | 2151 |
} |
| 2146 | 2152 |
|
| 2147 | 2153 |
/// \brief Build the unique map. |
| 2148 | 2154 |
/// |
| 2149 | 2155 |
/// Build the unique map. It is called by the |
| ... | ... |
@@ -2173,328 +2179,328 @@ |
| 2173 | 2179 |
/// |
| 2174 | 2180 |
/// Returns the maximal value plus one in the map. |
| 2175 | 2181 |
unsigned int size() const {
|
| 2176 | 2182 |
return _inv_map.size(); |
| 2177 | 2183 |
} |
| 2178 | 2184 |
|
| 2179 | 2185 |
/// \brief Swaps the position of the two items in the map. |
| 2180 | 2186 |
/// |
| 2181 | 2187 |
/// Swaps the position of the two items in the map. |
| 2182 | 2188 |
void swap(const Item& p, const Item& q) {
|
| 2183 | 2189 |
int pi = Map::operator[](p); |
| 2184 | 2190 |
int qi = Map::operator[](q); |
| 2185 | 2191 |
Map::set(p, qi); |
| 2186 | 2192 |
_inv_map[qi] = p; |
| 2187 | 2193 |
Map::set(q, pi); |
| 2188 | 2194 |
_inv_map[pi] = q; |
| 2189 | 2195 |
} |
| 2190 | 2196 |
|
| 2191 | 2197 |
/// \brief Gives back the \e descriptor of the item. |
| 2192 | 2198 |
/// |
| 2193 | 2199 |
/// Gives back the mutable and unique \e descriptor of the map. |
| 2194 | 2200 |
int operator[](const Item& item) const {
|
| 2195 | 2201 |
return Map::operator[](item); |
| 2196 | 2202 |
} |
| 2197 | 2203 |
|
| 2198 | 2204 |
/// \brief Gives back the item by its descriptor. |
| 2199 | 2205 |
/// |
| 2200 | 2206 |
/// Gives back th item by its descriptor. |
| 2201 | 2207 |
Item operator()(int id) const {
|
| 2202 | 2208 |
return _inv_map[id]; |
| 2203 | 2209 |
} |
| 2204 | 2210 |
|
| 2205 | 2211 |
private: |
| 2206 | 2212 |
|
| 2207 | 2213 |
typedef std::vector<Item> Container; |
| 2208 | 2214 |
Container _inv_map; |
| 2209 | 2215 |
|
| 2210 | 2216 |
public: |
| 2211 | 2217 |
/// \brief The inverse map type of DescriptorMap. |
| 2212 | 2218 |
/// |
| 2213 | 2219 |
/// The inverse map type of DescriptorMap. |
| 2214 | 2220 |
class InverseMap {
|
| 2215 | 2221 |
public: |
| 2216 | 2222 |
/// \brief Constructor of the InverseMap. |
| 2217 | 2223 |
/// |
| 2218 | 2224 |
/// Constructor of the InverseMap. |
| 2219 | 2225 |
explicit InverseMap(const DescriptorMap& inverted) |
| 2220 | 2226 |
: _inverted(inverted) {}
|
| 2221 | 2227 |
|
| 2222 | 2228 |
|
| 2223 | 2229 |
/// The value type of the InverseMap. |
| 2224 | 2230 |
typedef typename DescriptorMap::Key Value; |
| 2225 | 2231 |
/// The key type of the InverseMap. |
| 2226 | 2232 |
typedef typename DescriptorMap::Value Key; |
| 2227 | 2233 |
|
| 2228 | 2234 |
/// \brief Subscript operator. |
| 2229 | 2235 |
/// |
| 2230 | 2236 |
/// Subscript operator. It gives back the item |
| 2231 | 2237 |
/// that the descriptor belongs to currently. |
| 2232 | 2238 |
Value operator[](const Key& key) const {
|
| 2233 | 2239 |
return _inverted(key); |
| 2234 | 2240 |
} |
| 2235 | 2241 |
|
| 2236 | 2242 |
/// \brief Size of the map. |
| 2237 | 2243 |
/// |
| 2238 | 2244 |
/// Returns the size of the map. |
| 2239 | 2245 |
unsigned int size() const {
|
| 2240 | 2246 |
return _inverted.size(); |
| 2241 | 2247 |
} |
| 2242 | 2248 |
|
| 2243 | 2249 |
private: |
| 2244 | 2250 |
const DescriptorMap& _inverted; |
| 2245 | 2251 |
}; |
| 2246 | 2252 |
|
| 2247 | 2253 |
/// \brief Gives back the inverse of the map. |
| 2248 | 2254 |
/// |
| 2249 | 2255 |
/// Gives back the inverse of the map. |
| 2250 | 2256 |
const InverseMap inverse() const {
|
| 2251 | 2257 |
return InverseMap(*this); |
| 2252 | 2258 |
} |
| 2253 | 2259 |
}; |
| 2254 | 2260 |
|
| 2255 | 2261 |
/// \brief Returns the source of the given arc. |
| 2256 | 2262 |
/// |
| 2257 | 2263 |
/// The SourceMap gives back the source Node of the given arc. |
| 2258 | 2264 |
/// \see TargetMap |
| 2259 | 2265 |
template <typename Digraph> |
| 2260 | 2266 |
class SourceMap {
|
| 2261 | 2267 |
public: |
| 2262 | 2268 |
|
| 2263 | 2269 |
typedef typename Digraph::Node Value; |
| 2264 | 2270 |
typedef typename Digraph::Arc Key; |
| 2265 | 2271 |
|
| 2266 | 2272 |
/// \brief Constructor |
| 2267 | 2273 |
/// |
| 2268 | 2274 |
/// Constructor |
| 2269 |
/// \param |
|
| 2275 |
/// \param digraph The digraph that the map belongs to. |
|
| 2270 | 2276 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {}
|
| 2271 | 2277 |
|
| 2272 | 2278 |
/// \brief The subscript operator. |
| 2273 | 2279 |
/// |
| 2274 | 2280 |
/// The subscript operator. |
| 2275 | 2281 |
/// \param arc The arc |
| 2276 | 2282 |
/// \return The source of the arc |
| 2277 | 2283 |
Value operator[](const Key& arc) const {
|
| 2278 | 2284 |
return _digraph.source(arc); |
| 2279 | 2285 |
} |
| 2280 | 2286 |
|
| 2281 | 2287 |
private: |
| 2282 | 2288 |
const Digraph& _digraph; |
| 2283 | 2289 |
}; |
| 2284 | 2290 |
|
| 2285 |
/// \brief Returns a \ |
|
| 2291 |
/// \brief Returns a \c SourceMap class. |
|
| 2286 | 2292 |
/// |
| 2287 |
/// This function just returns an \ |
|
| 2293 |
/// This function just returns an \c SourceMap class. |
|
| 2288 | 2294 |
/// \relates SourceMap |
| 2289 | 2295 |
template <typename Digraph> |
| 2290 | 2296 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
|
| 2291 | 2297 |
return SourceMap<Digraph>(digraph); |
| 2292 | 2298 |
} |
| 2293 | 2299 |
|
| 2294 | 2300 |
/// \brief Returns the target of the given arc. |
| 2295 | 2301 |
/// |
| 2296 | 2302 |
/// The TargetMap gives back the target Node of the given arc. |
| 2297 | 2303 |
/// \see SourceMap |
| 2298 | 2304 |
template <typename Digraph> |
| 2299 | 2305 |
class TargetMap {
|
| 2300 | 2306 |
public: |
| 2301 | 2307 |
|
| 2302 | 2308 |
typedef typename Digraph::Node Value; |
| 2303 | 2309 |
typedef typename Digraph::Arc Key; |
| 2304 | 2310 |
|
| 2305 | 2311 |
/// \brief Constructor |
| 2306 | 2312 |
/// |
| 2307 | 2313 |
/// Constructor |
| 2308 |
/// \param |
|
| 2314 |
/// \param digraph The digraph that the map belongs to. |
|
| 2309 | 2315 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {}
|
| 2310 | 2316 |
|
| 2311 | 2317 |
/// \brief The subscript operator. |
| 2312 | 2318 |
/// |
| 2313 | 2319 |
/// The subscript operator. |
| 2314 | 2320 |
/// \param e The arc |
| 2315 | 2321 |
/// \return The target of the arc |
| 2316 | 2322 |
Value operator[](const Key& e) const {
|
| 2317 | 2323 |
return _digraph.target(e); |
| 2318 | 2324 |
} |
| 2319 | 2325 |
|
| 2320 | 2326 |
private: |
| 2321 | 2327 |
const Digraph& _digraph; |
| 2322 | 2328 |
}; |
| 2323 | 2329 |
|
| 2324 |
/// \brief Returns a \ |
|
| 2330 |
/// \brief Returns a \c TargetMap class. |
|
| 2325 | 2331 |
/// |
| 2326 |
/// This function just returns a \ |
|
| 2332 |
/// This function just returns a \c TargetMap class. |
|
| 2327 | 2333 |
/// \relates TargetMap |
| 2328 | 2334 |
template <typename Digraph> |
| 2329 | 2335 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
|
| 2330 | 2336 |
return TargetMap<Digraph>(digraph); |
| 2331 | 2337 |
} |
| 2332 | 2338 |
|
| 2333 | 2339 |
/// \brief Returns the "forward" directed arc view of an edge. |
| 2334 | 2340 |
/// |
| 2335 | 2341 |
/// Returns the "forward" directed arc view of an edge. |
| 2336 | 2342 |
/// \see BackwardMap |
| 2337 | 2343 |
template <typename Graph> |
| 2338 | 2344 |
class ForwardMap {
|
| 2339 | 2345 |
public: |
| 2340 | 2346 |
|
| 2341 | 2347 |
typedef typename Graph::Arc Value; |
| 2342 | 2348 |
typedef typename Graph::Edge Key; |
| 2343 | 2349 |
|
| 2344 | 2350 |
/// \brief Constructor |
| 2345 | 2351 |
/// |
| 2346 | 2352 |
/// Constructor |
| 2347 |
/// \param |
|
| 2353 |
/// \param graph The graph that the map belongs to. |
|
| 2348 | 2354 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {}
|
| 2349 | 2355 |
|
| 2350 | 2356 |
/// \brief The subscript operator. |
| 2351 | 2357 |
/// |
| 2352 | 2358 |
/// The subscript operator. |
| 2353 | 2359 |
/// \param key An edge |
| 2354 | 2360 |
/// \return The "forward" directed arc view of edge |
| 2355 | 2361 |
Value operator[](const Key& key) const {
|
| 2356 | 2362 |
return _graph.direct(key, true); |
| 2357 | 2363 |
} |
| 2358 | 2364 |
|
| 2359 | 2365 |
private: |
| 2360 | 2366 |
const Graph& _graph; |
| 2361 | 2367 |
}; |
| 2362 | 2368 |
|
| 2363 |
/// \brief Returns a \ |
|
| 2369 |
/// \brief Returns a \c ForwardMap class. |
|
| 2364 | 2370 |
/// |
| 2365 |
/// This function just returns an \ |
|
| 2371 |
/// This function just returns an \c ForwardMap class. |
|
| 2366 | 2372 |
/// \relates ForwardMap |
| 2367 | 2373 |
template <typename Graph> |
| 2368 | 2374 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) {
|
| 2369 | 2375 |
return ForwardMap<Graph>(graph); |
| 2370 | 2376 |
} |
| 2371 | 2377 |
|
| 2372 | 2378 |
/// \brief Returns the "backward" directed arc view of an edge. |
| 2373 | 2379 |
/// |
| 2374 | 2380 |
/// Returns the "backward" directed arc view of an edge. |
| 2375 | 2381 |
/// \see ForwardMap |
| 2376 | 2382 |
template <typename Graph> |
| 2377 | 2383 |
class BackwardMap {
|
| 2378 | 2384 |
public: |
| 2379 | 2385 |
|
| 2380 | 2386 |
typedef typename Graph::Arc Value; |
| 2381 | 2387 |
typedef typename Graph::Edge Key; |
| 2382 | 2388 |
|
| 2383 | 2389 |
/// \brief Constructor |
| 2384 | 2390 |
/// |
| 2385 | 2391 |
/// Constructor |
| 2386 |
/// \param |
|
| 2392 |
/// \param graph The graph that the map belongs to. |
|
| 2387 | 2393 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {}
|
| 2388 | 2394 |
|
| 2389 | 2395 |
/// \brief The subscript operator. |
| 2390 | 2396 |
/// |
| 2391 | 2397 |
/// The subscript operator. |
| 2392 | 2398 |
/// \param key An edge |
| 2393 | 2399 |
/// \return The "backward" directed arc view of edge |
| 2394 | 2400 |
Value operator[](const Key& key) const {
|
| 2395 | 2401 |
return _graph.direct(key, false); |
| 2396 | 2402 |
} |
| 2397 | 2403 |
|
| 2398 | 2404 |
private: |
| 2399 | 2405 |
const Graph& _graph; |
| 2400 | 2406 |
}; |
| 2401 | 2407 |
|
| 2402 |
/// \brief Returns a \ref BackwardMap class |
|
| 2403 |
|
|
| 2404 |
/// |
|
| 2408 |
/// \brief Returns a \c BackwardMap class |
|
| 2409 |
|
|
| 2410 |
/// This function just returns a \c BackwardMap class. |
|
| 2405 | 2411 |
/// \relates BackwardMap |
| 2406 | 2412 |
template <typename Graph> |
| 2407 | 2413 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) {
|
| 2408 | 2414 |
return BackwardMap<Graph>(graph); |
| 2409 | 2415 |
} |
| 2410 | 2416 |
|
| 2411 | 2417 |
/// \brief Potential difference map |
| 2412 | 2418 |
/// |
| 2413 | 2419 |
/// If there is an potential map on the nodes then we |
| 2414 | 2420 |
/// can get an arc map as we get the substraction of the |
| 2415 | 2421 |
/// values of the target and source. |
| 2416 | 2422 |
template <typename Digraph, typename NodeMap> |
| 2417 | 2423 |
class PotentialDifferenceMap {
|
| 2418 | 2424 |
public: |
| 2419 | 2425 |
typedef typename Digraph::Arc Key; |
| 2420 | 2426 |
typedef typename NodeMap::Value Value; |
| 2421 | 2427 |
|
| 2422 | 2428 |
/// \brief Constructor |
| 2423 | 2429 |
/// |
| 2424 | 2430 |
/// Contructor of the map |
| 2425 | 2431 |
explicit PotentialDifferenceMap(const Digraph& digraph, |
| 2426 | 2432 |
const NodeMap& potential) |
| 2427 | 2433 |
: _digraph(digraph), _potential(potential) {}
|
| 2428 | 2434 |
|
| 2429 | 2435 |
/// \brief Const subscription operator |
| 2430 | 2436 |
/// |
| 2431 | 2437 |
/// Const subscription operator |
| 2432 | 2438 |
Value operator[](const Key& arc) const {
|
| 2433 | 2439 |
return _potential[_digraph.target(arc)] - |
| 2434 | 2440 |
_potential[_digraph.source(arc)]; |
| 2435 | 2441 |
} |
| 2436 | 2442 |
|
| 2437 | 2443 |
private: |
| 2438 | 2444 |
const Digraph& _digraph; |
| 2439 | 2445 |
const NodeMap& _potential; |
| 2440 | 2446 |
}; |
| 2441 | 2447 |
|
| 2442 | 2448 |
/// \brief Returns a PotentialDifferenceMap. |
| 2443 | 2449 |
/// |
| 2444 | 2450 |
/// This function just returns a PotentialDifferenceMap. |
| 2445 | 2451 |
/// \relates PotentialDifferenceMap |
| 2446 | 2452 |
template <typename Digraph, typename NodeMap> |
| 2447 | 2453 |
PotentialDifferenceMap<Digraph, NodeMap> |
| 2448 | 2454 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
|
| 2449 | 2455 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential); |
| 2450 | 2456 |
} |
| 2451 | 2457 |
|
| 2452 | 2458 |
/// \brief Map of the node in-degrees. |
| 2453 | 2459 |
/// |
| 2454 | 2460 |
/// This map returns the in-degree of a node. Once it is constructed, |
| 2455 | 2461 |
/// the degrees are stored in a standard NodeMap, so each query is done |
| 2456 | 2462 |
/// in constant time. On the other hand, the values are updated automatically |
| 2457 | 2463 |
/// whenever the digraph changes. |
| 2458 | 2464 |
/// |
| 2459 | 2465 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
| 2460 | 2466 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap |
| 2461 | 2467 |
/// is not guarantied if these additional features are used. For example |
| 2462 | 2468 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
| 2463 | 2469 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
| 2464 | 2470 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
| 2465 | 2471 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
| 2466 | 2472 |
/// |
| 2467 | 2473 |
/// \sa OutDegMap |
| 2468 | 2474 |
|
| 2469 | 2475 |
template <typename _Digraph> |
| 2470 | 2476 |
class InDegMap |
| 2471 | 2477 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
| 2472 | 2478 |
::ItemNotifier::ObserverBase {
|
| 2473 | 2479 |
|
| 2474 | 2480 |
public: |
| 2475 | 2481 |
|
| 2476 | 2482 |
typedef _Digraph Digraph; |
| 2477 | 2483 |
typedef int Value; |
| 2478 | 2484 |
typedef typename Digraph::Node Key; |
| 2479 | 2485 |
|
| 2480 | 2486 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
| 2481 | 2487 |
::ItemNotifier::ObserverBase Parent; |
| 2482 | 2488 |
|
| 2483 | 2489 |
private: |
| 2484 | 2490 |
|
| 2485 | 2491 |
class AutoNodeMap |
| 2486 | 2492 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
|
| 2487 | 2493 |
public: |
| 2488 | 2494 |
|
| 2489 | 2495 |
typedef typename ItemSetTraits<Digraph, Key>:: |
| 2490 | 2496 |
template Map<int>::Type Parent; |
| 2491 | 2497 |
|
| 2492 | 2498 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
| 2493 | 2499 |
|
| 2494 | 2500 |
virtual void add(const Key& key) {
|
| 2495 | 2501 |
Parent::add(key); |
| 2496 | 2502 |
Parent::set(key, 0); |
| 2497 | 2503 |
} |
| 2498 | 2504 |
|
| 2499 | 2505 |
virtual void add(const std::vector<Key>& keys) {
|
| 2500 | 2506 |
Parent::add(keys); |
| ... | ... |
@@ -756,193 +756,193 @@ |
| 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); |
| ... | ... |
@@ -272,205 +272,205 @@ |
| 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 |
}; |
| ... | ... |
@@ -682,131 +682,131 @@ |
| 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 |
| ... | ... |
@@ -218,357 +218,356 @@ |
| 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)