1
38
2
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 | 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; |
126 | 126 |
a=g.addArc(n1,n2); acolors[a]=3; widths[a]=1; |
127 | 127 |
a=g.addArc(n1,n2); acolors[a]=4; widths[a]=1; |
128 | 128 |
a=g.addArc(n1,n2); acolors[a]=5; widths[a]=1; |
129 | 129 |
a=g.addArc(n1,n2); acolors[a]=6; widths[a]=1; |
130 | 130 |
a=g.addArc(n1,n2); acolors[a]=7; widths[a]=1; |
131 | 131 |
|
132 | 132 |
cout << "Create 'graph_to_eps_demo_out_4_par.eps'" << endl; |
133 | 133 |
graphToEps(g,"graph_to_eps_demo_out_4_par.eps"). |
134 | 134 |
title("Sample .eps figure (parallel arcs)"). |
135 | 135 |
copyright("(C) 2003-2008 LEMON Project"). |
136 | 136 |
absoluteNodeSizes().absoluteArcWidths(). |
137 | 137 |
nodeShapes(shapes). |
138 | 138 |
coords(coords). |
139 | 139 |
nodeScale(2).nodeSizes(sizes). |
140 | 140 |
nodeColors(composeMap(palette,colors)). |
141 | 141 |
arcColors(composeMap(palette,acolors)). |
142 | 142 |
arcWidthScale(.4).arcWidths(widths). |
143 | 143 |
nodeTexts(id).nodeTextSize(3). |
144 | 144 |
enableParallel().parArcDist(1.5). |
145 | 145 |
run(); |
146 | 146 |
|
147 | 147 |
cout << "Create 'graph_to_eps_demo_out_5_par_arr.eps'" << endl; |
148 | 148 |
graphToEps(g,"graph_to_eps_demo_out_5_par_arr.eps"). |
149 | 149 |
title("Sample .eps figure (parallel arcs and arrowheads)"). |
150 | 150 |
copyright("(C) 2003-2008 LEMON Project"). |
151 | 151 |
absoluteNodeSizes().absoluteArcWidths(). |
152 | 152 |
nodeScale(2).nodeSizes(sizes). |
153 | 153 |
coords(coords). |
154 | 154 |
nodeShapes(shapes). |
155 | 155 |
nodeColors(composeMap(palette,colors)). |
156 | 156 |
arcColors(composeMap(palette,acolors)). |
157 | 157 |
arcWidthScale(.3).arcWidths(widths). |
158 | 158 |
nodeTexts(id).nodeTextSize(3). |
159 | 159 |
enableParallel().parArcDist(1). |
160 | 160 |
drawArrows().arrowWidth(1).arrowLength(1). |
161 | 161 |
run(); |
162 | 162 |
|
163 | 163 |
cout << "Create 'graph_to_eps_demo_out_6_par_arr_a4.eps'" << endl; |
164 | 164 |
graphToEps(g,"graph_to_eps_demo_out_6_par_arr_a4.eps"). |
165 | 165 |
title("Sample .eps figure (fits to A4)"). |
166 | 166 |
copyright("(C) 2003-2008 LEMON Project"). |
167 | 167 |
scaleToA4(). |
168 | 168 |
absoluteNodeSizes().absoluteArcWidths(). |
169 | 169 |
nodeScale(2).nodeSizes(sizes). |
170 | 170 |
coords(coords). |
171 | 171 |
nodeShapes(shapes). |
172 | 172 |
nodeColors(composeMap(palette,colors)). |
173 | 173 |
arcColors(composeMap(palette,acolors)). |
174 | 174 |
arcWidthScale(.3).arcWidths(widths). |
175 | 175 |
nodeTexts(id).nodeTextSize(3). |
176 | 176 |
enableParallel().parArcDist(1). |
177 | 177 |
drawArrows().arrowWidth(1).arrowLength(1). |
178 | 178 |
run(); |
179 | 179 |
|
180 | 180 |
// Create an .eps file showing the colors of a default Palette |
181 | 181 |
ListDigraph h; |
182 | 182 |
ListDigraph::NodeMap<int> hcolors(h); |
183 | 183 |
ListDigraph::NodeMap<Point> hcoords(h); |
184 | 184 |
|
185 | 185 |
int cols=int(sqrt(double(palette.size()))); |
186 | 186 |
for(int i=0;i<int(paletteW.size());i++) { |
187 | 187 |
Node n=h.addNode(); |
188 | 188 |
hcoords[n]=Point(1+i%cols,1+i/cols); |
189 | 189 |
hcolors[n]=i; |
190 | 190 |
} |
191 | 191 |
|
192 | 192 |
cout << "Create 'graph_to_eps_demo_out_7_colors.eps'" << endl; |
193 | 193 |
graphToEps(h,"graph_to_eps_demo_out_7_colors.eps"). |
194 | 194 |
scale(60). |
195 | 195 |
title("Sample .eps figure (Palette demo)"). |
196 | 196 |
copyright("(C) 2003-2008 LEMON Project"). |
197 | 197 |
coords(hcoords). |
198 | 198 |
absoluteNodeSizes().absoluteArcWidths(). |
199 | 199 |
nodeScale(.45). |
200 | 200 |
distantColorNodeTexts(). |
201 | 201 |
nodeTexts(hcolors).nodeTextSize(.6). |
202 | 202 |
nodeColors(composeMap(paletteW,hcolors)). |
203 | 203 |
run(); |
204 | 204 |
|
205 | 205 |
return 0; |
206 | 206 |
} |
1 | 1 |
EXTRA_DIST += \ |
2 | 2 |
doc/Doxyfile.in \ |
3 | 3 |
doc/coding_style.dox \ |
4 | 4 |
doc/dirs.dox \ |
5 | 5 |
doc/groups.dox \ |
6 | 6 |
doc/lgf.dox \ |
7 | 7 |
doc/license.dox \ |
8 | 8 |
doc/mainpage.dox \ |
9 |
doc/migration.dox \ |
|
9 | 10 |
doc/named-param.dox \ |
10 | 11 |
doc/namespaces.dox \ |
11 | 12 |
doc/html \ |
12 | 13 |
doc/CMakeLists.txt |
13 | 14 |
|
14 | 15 |
DOC_EPS_IMAGES18 = \ |
15 | 16 |
nodeshape_0.eps \ |
16 | 17 |
nodeshape_1.eps \ |
17 | 18 |
nodeshape_2.eps \ |
18 | 19 |
nodeshape_3.eps \ |
19 | 20 |
nodeshape_4.eps |
20 | 21 |
|
21 | 22 |
DOC_EPS_IMAGES = \ |
22 | 23 |
$(DOC_EPS_IMAGES18) |
23 | 24 |
|
24 | 25 |
DOC_PNG_IMAGES = \ |
25 | 26 |
$(DOC_EPS_IMAGES:%.eps=doc/gen-images/%.png) |
26 | 27 |
|
27 | 28 |
EXTRA_DIST += $(DOC_EPS_IMAGES:%=doc/images/%) |
28 | 29 |
|
29 | 30 |
doc/html: |
30 | 31 |
$(MAKE) $(AM_MAKEFLAGS) html |
31 | 32 |
|
32 | 33 |
GS_COMMAND=gs -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 |
33 | 34 |
|
34 | 35 |
$(DOC_EPS_IMAGES18:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps |
35 | 36 |
-mkdir doc/gen-images |
36 | 37 |
if test ${gs_found} = yes; then \ |
37 | 38 |
$(GS_COMMAND) -sDEVICE=pngalpha -r18 -sOutputFile=$@ $<; \ |
38 | 39 |
else \ |
39 | 40 |
echo; \ |
40 | 41 |
echo "Ghostscript not found."; \ |
41 | 42 |
echo; \ |
42 | 43 |
exit 1; \ |
43 | 44 |
fi |
44 | 45 |
|
45 | 46 |
html-local: $(DOC_PNG_IMAGES) |
46 | 47 |
if test ${doxygen_found} = yes; then \ |
47 | 48 |
cd doc; \ |
48 | 49 |
doxygen Doxyfile; \ |
49 | 50 |
cd ..; \ |
50 | 51 |
else \ |
51 | 52 |
echo; \ |
52 | 53 |
echo "Doxygen not found."; \ |
53 | 54 |
echo; \ |
54 | 55 |
exit 1; \ |
55 | 56 |
fi |
56 | 57 |
|
57 | 58 |
clean-local: |
58 | 59 |
-rm -rf doc/html |
59 | 60 |
-rm -f doc/doxygen.log |
60 | 61 |
-rm -f $(DOC_PNG_IMAGES) |
61 | 62 |
-rm -rf doc/gen-images |
62 | 63 |
|
63 | 64 |
update-external-tags: |
64 | 65 |
wget -O doc/libstdc++.tag.tmp http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/libstdc++.tag && \ |
65 | 66 |
mv doc/libstdc++.tag.tmp doc/libstdc++.tag || \ |
66 | 67 |
rm doc/libstdc++.tag.tmp |
67 | 68 |
|
68 | 69 |
install-html-local: doc/html |
69 | 70 |
@$(NORMAL_INSTALL) |
70 | 71 |
$(mkinstalldirs) $(DESTDIR)$(htmldir)/docs |
71 | 72 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \ |
72 | 73 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
73 | 74 |
echo " $(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/docs/$$f"; \ |
74 | 75 |
$(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/docs/$$f; \ |
75 | 76 |
done |
76 | 77 |
|
77 | 78 |
uninstall-local: |
78 | 79 |
@$(NORMAL_UNINSTALL) |
79 | 80 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \ |
80 | 81 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
81 | 82 |
echo " rm -f $(DESTDIR)$(htmldir)/docs/$$f"; \ |
82 | 83 |
rm -f $(DESTDIR)$(htmldir)/docs/$$f; \ |
83 | 84 |
done |
84 | 85 |
|
85 | 86 |
.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 |
You are free to use the graph structure that fit your requirements |
44 | 44 |
the best, most graph algorithms and auxiliary data structures can be used |
45 |
with any graph |
|
45 |
with any graph structure. |
|
46 |
|
|
47 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
|
46 | 48 |
*/ |
47 | 49 |
|
48 | 50 |
/** |
49 | 51 |
@defgroup maps Maps |
50 | 52 |
@ingroup datas |
51 | 53 |
\brief Map structures implemented in LEMON. |
52 | 54 |
|
53 | 55 |
This group describes the map structures implemented in LEMON. |
54 | 56 |
|
55 |
LEMON provides several special purpose maps that e.g. combine |
|
57 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
|
56 | 58 |
new maps from existing ones. |
59 |
|
|
60 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
|
57 | 61 |
*/ |
58 | 62 |
|
59 | 63 |
/** |
60 | 64 |
@defgroup graph_maps Graph Maps |
61 | 65 |
@ingroup maps |
62 | 66 |
\brief Special graph-related maps. |
63 | 67 |
|
64 | 68 |
This group describes maps that are specifically designed to assign |
65 | 69 |
values to the nodes and arcs of graphs. |
66 | 70 |
*/ |
67 | 71 |
|
68 |
|
|
69 | 72 |
/** |
70 | 73 |
\defgroup map_adaptors Map Adaptors |
71 | 74 |
\ingroup maps |
72 | 75 |
\brief Tools to create new maps from existing ones |
73 | 76 |
|
74 | 77 |
This group describes map adaptors that are used to create "implicit" |
75 | 78 |
maps from other maps. |
76 | 79 |
|
77 | 80 |
Most of them are \ref lemon::concepts::ReadMap "read-only maps". |
78 | 81 |
They can make arithmetic and logical operations between one or two maps |
79 | 82 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
80 | 83 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
81 | 84 |
|
82 | 85 |
The typical usage of this classes is passing implicit maps to |
83 | 86 |
algorithms. If a function type algorithm is called then the function |
84 | 87 |
type map adaptors can be used comfortable. For example let's see the |
85 |
usage of map adaptors with the \c |
|
88 |
usage of map adaptors with the \c graphToEps() function. |
|
86 | 89 |
\code |
87 | 90 |
Color nodeColor(int deg) { |
88 | 91 |
if (deg >= 2) { |
89 | 92 |
return Color(0.5, 0.0, 0.5); |
90 | 93 |
} else if (deg == 1) { |
91 | 94 |
return Color(1.0, 0.5, 1.0); |
92 | 95 |
} else { |
93 | 96 |
return Color(0.0, 0.0, 0.0); |
94 | 97 |
} |
95 | 98 |
} |
96 | 99 |
|
97 | 100 |
Digraph::NodeMap<int> degree_map(graph); |
98 | 101 |
|
99 |
|
|
102 |
graphToEps(graph, "graph.eps") |
|
100 | 103 |
.coords(coords).scaleToA4().undirected() |
101 | 104 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
102 | 105 |
.run(); |
103 | 106 |
\endcode |
104 | 107 |
The \c functorToMap() function makes an \c int to \c Color map from the |
105 |
\ |
|
108 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
|
106 | 109 |
and the previously created map. The composed map is a proper function to |
107 | 110 |
get the color of each node. |
108 | 111 |
|
109 | 112 |
The usage with class type algorithms is little bit harder. In this |
110 | 113 |
case the function type map adaptors can not be used, because the |
111 | 114 |
function map adaptors give back temporary objects. |
112 | 115 |
\code |
113 | 116 |
Digraph graph; |
114 | 117 |
|
115 | 118 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
116 | 119 |
DoubleArcMap length(graph); |
117 | 120 |
DoubleArcMap speed(graph); |
118 | 121 |
|
119 | 122 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
120 | 123 |
TimeMap time(length, speed); |
121 | 124 |
|
122 | 125 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
123 | 126 |
dijkstra.run(source, target); |
124 | 127 |
\endcode |
125 | 128 |
We have a length map and a maximum speed map on the arcs of a digraph. |
126 | 129 |
The minimum time to pass the arc can be calculated as the division of |
127 | 130 |
the two maps which can be done implicitly with the \c DivMap template |
128 | 131 |
class. We use the implicit minimum time map as the length map of the |
129 | 132 |
\c Dijkstra algorithm. |
130 | 133 |
*/ |
131 | 134 |
|
132 | 135 |
/** |
133 | 136 |
@defgroup paths Path Structures |
134 | 137 |
@ingroup datas |
135 | 138 |
\brief Path structures implemented in LEMON. |
136 | 139 |
|
137 | 140 |
This group describes the path structures implemented in LEMON. |
138 | 141 |
|
139 | 142 |
LEMON provides flexible data structures to work with paths. |
140 | 143 |
All of them have similar interfaces and they can be copied easily with |
141 | 144 |
assignment operators and copy constructors. This makes it easy and |
142 | 145 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
143 | 146 |
any kind of path structure. |
144 | 147 |
|
145 | 148 |
\sa lemon::concepts::Path |
146 |
|
|
147 | 149 |
*/ |
148 | 150 |
|
149 | 151 |
/** |
150 | 152 |
@defgroup auxdat Auxiliary Data Structures |
151 | 153 |
@ingroup datas |
152 | 154 |
\brief Auxiliary data structures implemented in LEMON. |
153 | 155 |
|
154 | 156 |
This group describes some data structures implemented in LEMON in |
155 | 157 |
order to make it easier to implement combinatorial algorithms. |
156 | 158 |
*/ |
157 | 159 |
|
158 |
|
|
159 | 160 |
/** |
160 | 161 |
@defgroup algs Algorithms |
161 | 162 |
\brief This group describes the several algorithms |
162 | 163 |
implemented in LEMON. |
163 | 164 |
|
164 | 165 |
This group describes the several algorithms |
165 | 166 |
implemented in LEMON. |
166 | 167 |
*/ |
167 | 168 |
|
168 | 169 |
/** |
169 | 170 |
@defgroup search Graph Search |
170 | 171 |
@ingroup algs |
171 | 172 |
\brief Common graph search algorithms. |
172 | 173 |
|
173 | 174 |
This group describes the common graph search algorithms like |
174 |
Breadth- |
|
175 |
Breadth-First Search (BFS) and Depth-First Search (DFS). |
|
175 | 176 |
*/ |
176 | 177 |
|
177 | 178 |
/** |
178 |
@defgroup shortest_path Shortest Path |
|
179 |
@defgroup shortest_path Shortest Path Algorithms |
|
179 | 180 |
@ingroup algs |
180 | 181 |
\brief Algorithms for finding shortest paths. |
181 | 182 |
|
182 | 183 |
This group describes the algorithms for finding shortest paths in graphs. |
183 | 184 |
*/ |
184 | 185 |
|
185 | 186 |
/** |
186 |
@defgroup spantree Minimum Spanning Tree |
|
187 |
@defgroup spantree Minimum Spanning Tree Algorithms |
|
187 | 188 |
@ingroup algs |
188 | 189 |
\brief Algorithms for finding a minimum cost spanning tree in a graph. |
189 | 190 |
|
190 | 191 |
This group describes the algorithms for finding a minimum cost spanning |
191 | 192 |
tree in a graph |
192 | 193 |
*/ |
193 | 194 |
|
195 |
@ingroup algs |
|
194 | 196 |
/** |
195 | 197 |
@defgroup utils Tools and Utilities |
196 | 198 |
\brief Tools and utilities for programming in LEMON |
197 | 199 |
|
198 | 200 |
Tools and utilities for programming in LEMON. |
199 | 201 |
*/ |
200 | 202 |
|
201 | 203 |
/** |
202 | 204 |
@defgroup gutils Basic Graph Utilities |
203 | 205 |
@ingroup utils |
204 | 206 |
\brief Simple basic graph utilities. |
205 | 207 |
|
206 | 208 |
This group describes some simple basic graph utilities. |
207 | 209 |
*/ |
208 | 210 |
|
209 | 211 |
/** |
210 | 212 |
@defgroup misc Miscellaneous Tools |
211 | 213 |
@ingroup utils |
212 | 214 |
\brief Tools for development, debugging and testing. |
213 | 215 |
|
214 | 216 |
This group describes several useful tools for development, |
215 | 217 |
debugging and testing. |
216 | 218 |
*/ |
217 | 219 |
|
218 | 220 |
/** |
219 |
@defgroup timecount Time |
|
221 |
@defgroup timecount Time Measuring and Counting |
|
220 | 222 |
@ingroup misc |
221 | 223 |
\brief Simple tools for measuring the performance of algorithms. |
222 | 224 |
|
223 | 225 |
This group describes simple tools for measuring the performance |
224 | 226 |
of algorithms. |
225 | 227 |
*/ |
226 | 228 |
|
227 | 229 |
/** |
228 | 230 |
@defgroup exceptions Exceptions |
229 | 231 |
@ingroup utils |
230 | 232 |
\brief Exceptions defined in LEMON. |
231 | 233 |
|
232 | 234 |
This group describes the exceptions defined in LEMON. |
233 | 235 |
*/ |
234 | 236 |
|
235 | 237 |
/** |
236 | 238 |
@defgroup io_group Input-Output |
237 | 239 |
\brief Graph Input-Output methods |
238 | 240 |
|
239 | 241 |
This group describes the tools for importing and exporting graphs |
240 | 242 |
and graph related data. Now it supports the LEMON format |
241 | 243 |
and the encapsulated postscript (EPS) format. |
244 |
postscript (EPS) format. |
|
242 | 245 |
*/ |
243 | 246 |
|
244 | 247 |
/** |
245 | 248 |
@defgroup lemon_io LEMON Input-Output |
246 | 249 |
@ingroup io_group |
247 |
\brief Reading and writing |
|
250 |
\brief Reading and writing LEMON Graph Format. |
|
248 | 251 |
|
249 | 252 |
This group describes methods for reading and writing |
250 | 253 |
\ref lgf-format "LEMON Graph Format". |
251 | 254 |
*/ |
252 | 255 |
|
253 | 256 |
/** |
254 |
@defgroup eps_io Postscript |
|
257 |
@defgroup eps_io Postscript Exporting |
|
255 | 258 |
@ingroup io_group |
256 | 259 |
\brief General \c EPS drawer and graph exporter |
257 | 260 |
|
258 | 261 |
This group describes general \c EPS drawing methods and special |
259 | 262 |
graph exporting tools. |
260 | 263 |
*/ |
261 | 264 |
|
262 |
|
|
263 | 265 |
/** |
264 | 266 |
@defgroup concept Concepts |
265 | 267 |
\brief Skeleton classes and concept checking classes |
266 | 268 |
|
267 | 269 |
This group describes the data/algorithm skeletons and concept checking |
268 | 270 |
classes implemented in LEMON. |
269 | 271 |
|
270 | 272 |
The purpose of the classes in this group is fourfold. |
271 | 273 |
|
272 | 274 |
- These classes contain the documentations of the concepts. In order |
273 | 275 |
to avoid document multiplications, an implementation of a concept |
274 | 276 |
simply refers to the corresponding concept class. |
275 | 277 |
|
276 | 278 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
277 | 279 |
implementation of the concepts should provide, however completely |
278 | 280 |
without implementations and real data structures behind the |
279 | 281 |
interface. On the other hand they should provide nothing else. All |
280 | 282 |
the algorithms working on a data structure meeting a certain concept |
281 | 283 |
should compile with these classes. (Though it will not run properly, |
282 | 284 |
of course.) In this way it is easily to check if an algorithm |
283 | 285 |
doesn't use any extra feature of a certain implementation. |
284 | 286 |
|
285 | 287 |
- The concept descriptor classes also provide a <em>checker class</em> |
286 | 288 |
that makes it possible to check whether a certain implementation of a |
287 | 289 |
concept indeed provides all the required features. |
288 | 290 |
|
289 | 291 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
290 |
|
|
291 | 292 |
*/ |
292 | 293 |
|
293 |
|
|
294 | 294 |
/** |
295 | 295 |
@defgroup graph_concepts Graph Structure Concepts |
296 | 296 |
@ingroup concept |
297 | 297 |
\brief Skeleton and concept checking classes for graph structures |
298 | 298 |
|
299 | 299 |
This group describes the skeletons and concept checking classes of LEMON's |
300 | 300 |
graph structures and helper classes used to implement these. |
301 | 301 |
*/ |
302 | 302 |
|
303 |
|
|
304 |
This group describes the skeletons and concept checking classes of maps. |
|
303 | 305 |
/** |
304 | 306 |
\anchor demoprograms |
305 | 307 |
|
306 | 308 |
@defgroup demos Demo programs |
307 | 309 |
|
308 | 310 |
Some demo programs are listed here. Their full source codes can be found in |
309 | 311 |
the \c demo subdirectory of the source tree. |
310 | 312 |
|
311 | 313 |
It order to compile them, use <tt>--enable-demo</tt> configure option when |
312 | 314 |
build the library. |
313 | 315 |
*/ |
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 |
45 | 45 |
want to see how LEMON works, see |
46 |
some \ref demoprograms "demo programs" |
|
46 |
some \ref demoprograms "demo programs". |
|
47 | 47 |
|
48 | 48 |
If you know what you are looking for then try to find it under the |
49 | 49 |
<a class="el" href="modules.html">Modules</a> |
50 | 50 |
section. |
51 | 51 |
|
52 |
|
|
52 |
If you are a user of the old (0.x) series of LEMON, please check out the |
|
53 |
\ref migration "Migration Guide" for the backward incompatibilities. |
|
53 | 54 |
*/ |
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 |
} |
131 | 130 |
|
132 | 131 |
ArgParser &ArgParser::refOption(const std::string &name, |
133 | 132 |
const std::string &help, |
134 | 133 |
double &ref, bool obl) |
135 | 134 |
{ |
136 | 135 |
ParData p; |
137 | 136 |
p.double_p=&ref; |
138 | 137 |
p.self_delete=false; |
139 | 138 |
p.help=help; |
140 | 139 |
p.type=DOUBLE; |
141 | 140 |
p.mandatory=obl; |
142 | 141 |
_opts[name]=p; |
143 | 142 |
return *this; |
144 | 143 |
} |
145 | 144 |
|
146 | 145 |
ArgParser &ArgParser::refOption(const std::string &name, |
147 | 146 |
const std::string &help, |
148 | 147 |
bool &ref, bool obl) |
149 | 148 |
{ |
150 | 149 |
ParData p; |
151 | 150 |
p.bool_p=&ref; |
152 | 151 |
p.self_delete=false; |
153 | 152 |
p.help=help; |
154 | 153 |
p.type=BOOL; |
155 | 154 |
p.mandatory=obl; |
156 | 155 |
_opts[name]=p; |
157 | 156 |
|
158 | 157 |
ref = false; |
159 | 158 |
|
160 | 159 |
return *this; |
161 | 160 |
} |
162 | 161 |
|
163 | 162 |
ArgParser &ArgParser::refOption(const std::string &name, |
164 | 163 |
const std::string &help, |
165 | 164 |
std::string &ref, bool obl) |
166 | 165 |
{ |
167 | 166 |
ParData p; |
168 | 167 |
p.string_p=&ref; |
169 | 168 |
p.self_delete=false; |
170 | 169 |
p.help=help; |
171 | 170 |
p.type=STRING; |
172 | 171 |
p.mandatory=obl; |
173 | 172 |
_opts[name]=p; |
174 | 173 |
return *this; |
175 | 174 |
} |
176 | 175 |
|
177 | 176 |
ArgParser &ArgParser::funcOption(const std::string &name, |
178 | 177 |
const std::string &help, |
179 | 178 |
void (*func)(void *),void *data) |
180 | 179 |
{ |
181 | 180 |
ParData p; |
182 | 181 |
p.func_p.p=func; |
183 | 182 |
p.func_p.data=data; |
184 | 183 |
p.self_delete=false; |
185 | 184 |
p.help=help; |
186 | 185 |
p.type=FUNC; |
187 | 186 |
p.mandatory=false; |
188 | 187 |
_opts[name]=p; |
189 | 188 |
return *this; |
190 | 189 |
} |
191 | 190 |
|
192 | 191 |
ArgParser &ArgParser::optionGroup(const std::string &group, |
193 | 192 |
const std::string &opt) |
194 | 193 |
{ |
195 | 194 |
Opts::iterator i = _opts.find(opt); |
196 | 195 |
LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'"); |
197 | 196 |
LEMON_ASSERT(!(i->second.ingroup), |
198 | 197 |
"Option already in option group: '"+opt+"'"); |
199 | 198 |
GroupData &g=_groups[group]; |
200 | 199 |
g.opts.push_back(opt); |
201 | 200 |
i->second.ingroup=true; |
202 | 201 |
return *this; |
203 | 202 |
} |
204 | 203 |
|
205 | 204 |
ArgParser &ArgParser::onlyOneGroup(const std::string &group) |
206 | 205 |
{ |
207 | 206 |
GroupData &g=_groups[group]; |
208 | 207 |
g.only_one=true; |
209 | 208 |
return *this; |
210 | 209 |
} |
211 | 210 |
|
212 | 211 |
ArgParser &ArgParser::synonym(const std::string &syn, |
213 | 212 |
const std::string &opt) |
214 | 213 |
{ |
215 | 214 |
Opts::iterator o = _opts.find(opt); |
216 | 215 |
Opts::iterator s = _opts.find(syn); |
217 | 216 |
LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'"); |
218 | 217 |
LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'"); |
219 | 218 |
ParData p; |
220 | 219 |
p.help=opt; |
221 | 220 |
p.mandatory=false; |
222 | 221 |
p.syn=true; |
223 | 222 |
_opts[syn]=p; |
224 | 223 |
o->second.has_syn=true; |
225 | 224 |
return *this; |
226 | 225 |
} |
227 | 226 |
|
228 | 227 |
ArgParser &ArgParser::mandatoryGroup(const std::string &group) |
229 | 228 |
{ |
230 | 229 |
GroupData &g=_groups[group]; |
231 | 230 |
g.mandatory=true; |
232 | 231 |
return *this; |
233 | 232 |
} |
234 | 233 |
|
235 | 234 |
ArgParser &ArgParser::other(const std::string &name, |
236 | 235 |
const std::string &help) |
237 | 236 |
{ |
238 | 237 |
_others_help.push_back(OtherArg(name,help)); |
239 | 238 |
return *this; |
240 | 239 |
} |
241 | 240 |
|
242 | 241 |
void ArgParser::show(std::ostream &os,Opts::const_iterator i) const |
243 | 242 |
{ |
244 | 243 |
os << "-" << i->first; |
245 | 244 |
if(i->second.has_syn) |
246 | 245 |
for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j) |
247 | 246 |
if(j->second.syn&&j->second.help==i->first) |
248 | 247 |
os << "|-" << j->first; |
249 | 248 |
switch(i->second.type) { |
250 | 249 |
case STRING: |
251 | 250 |
os << " str"; |
252 | 251 |
break; |
253 | 252 |
case INTEGER: |
254 | 253 |
os << " int"; |
255 | 254 |
break; |
256 | 255 |
case DOUBLE: |
257 | 256 |
os << " num"; |
258 | 257 |
break; |
259 | 258 |
default: |
260 | 259 |
break; |
261 | 260 |
} |
262 | 261 |
} |
263 | 262 |
|
264 | 263 |
void ArgParser::show(std::ostream &os,Groups::const_iterator i) const |
265 | 264 |
{ |
266 | 265 |
GroupData::Opts::const_iterator o=i->second.opts.begin(); |
267 | 266 |
while(o!=i->second.opts.end()) { |
268 | 267 |
show(os,_opts.find(*o)); |
269 | 268 |
++o; |
270 | 269 |
if(o!=i->second.opts.end()) os<<'|'; |
271 | 270 |
} |
272 | 271 |
} |
273 | 272 |
|
274 | 273 |
void ArgParser::showHelp(Opts::const_iterator i) const |
275 | 274 |
{ |
276 | 275 |
if(i->second.help.size()==0||i->second.syn) return; |
277 | 276 |
std::cerr << " "; |
278 | 277 |
show(std::cerr,i); |
279 | 278 |
std::cerr << std::endl; |
280 | 279 |
std::cerr << " " << i->second.help << std::endl; |
281 | 280 |
} |
282 | 281 |
void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i) |
283 | 282 |
const |
284 | 283 |
{ |
285 | 284 |
if(i->help.size()==0) return; |
286 | 285 |
std::cerr << " " << i->name << std::endl |
287 | 286 |
<< " " << i->help << std::endl; |
288 | 287 |
} |
289 | 288 |
|
290 | 289 |
void ArgParser::shortHelp() const |
291 | 290 |
{ |
292 | 291 |
const unsigned int LINE_LEN=77; |
293 | 292 |
const std::string indent(" "); |
294 | 293 |
std::cerr << "Usage:\n " << _command_name; |
295 | 294 |
int pos=_command_name.size()+2; |
296 | 295 |
for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) { |
297 | 296 |
std::ostringstream cstr; |
298 | 297 |
cstr << ' '; |
299 | 298 |
if(!g->second.mandatory) cstr << '['; |
300 | 299 |
show(cstr,g); |
301 | 300 |
if(!g->second.mandatory) cstr << ']'; |
302 | 301 |
if(pos+cstr.str().size()>LINE_LEN) { |
303 | 302 |
std::cerr << std::endl << indent; |
304 | 303 |
pos=indent.size(); |
305 | 304 |
} |
306 | 305 |
std::cerr << cstr.str(); |
307 | 306 |
pos+=cstr.str().size(); |
308 | 307 |
} |
309 | 308 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) |
310 | 309 |
if(!i->second.ingroup&&!i->second.syn) { |
311 | 310 |
std::ostringstream cstr; |
312 | 311 |
cstr << ' '; |
313 | 312 |
if(!i->second.mandatory) cstr << '['; |
314 | 313 |
show(cstr,i); |
315 | 314 |
if(!i->second.mandatory) cstr << ']'; |
316 | 315 |
if(pos+cstr.str().size()>LINE_LEN) { |
317 | 316 |
std::cerr << std::endl << indent; |
318 | 317 |
pos=indent.size(); |
319 | 318 |
} |
320 | 319 |
std::cerr << cstr.str(); |
321 | 320 |
pos+=cstr.str().size(); |
322 | 321 |
} |
323 | 322 |
for(std::vector<OtherArg>::const_iterator i=_others_help.begin(); |
324 | 323 |
i!=_others_help.end();++i) |
325 | 324 |
{ |
326 | 325 |
std::ostringstream cstr; |
327 | 326 |
cstr << ' ' << i->name; |
328 | 327 |
|
329 | 328 |
if(pos+cstr.str().size()>LINE_LEN) { |
330 | 329 |
std::cerr << std::endl << indent; |
331 | 330 |
pos=indent.size(); |
332 | 331 |
} |
333 | 332 |
std::cerr << cstr.str(); |
334 | 333 |
pos+=cstr.str().size(); |
335 | 334 |
} |
336 | 335 |
std::cerr << std::endl; |
337 | 336 |
} |
338 | 337 |
|
339 | 338 |
void ArgParser::showHelp() const |
340 | 339 |
{ |
341 | 340 |
shortHelp(); |
342 | 341 |
std::cerr << "Where:\n"; |
343 | 342 |
for(std::vector<OtherArg>::const_iterator i=_others_help.begin(); |
344 | 343 |
i!=_others_help.end();++i) showHelp(i); |
345 | 344 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i); |
346 | 345 |
exit(1); |
347 | 346 |
} |
348 | 347 |
|
349 | 348 |
|
350 | 349 |
void ArgParser::unknownOpt(std::string arg) const |
351 | 350 |
{ |
352 | 351 |
std::cerr << "\nUnknown option: " << arg << "\n"; |
353 | 352 |
std::cerr << "\nType '" << _command_name << |
354 | 353 |
" --help' to obtain a short summary on the usage.\n\n"; |
355 | 354 |
exit(1); |
356 | 355 |
} |
357 | 356 |
|
358 | 357 |
void ArgParser::requiresValue(std::string arg, OptType t) const |
359 | 358 |
{ |
360 | 359 |
std::cerr << "Argument '" << arg << "' requires a"; |
361 | 360 |
switch(t) { |
362 | 361 |
case STRING: |
363 | 362 |
std::cerr << " string"; |
364 | 363 |
break; |
365 | 364 |
case INTEGER: |
366 | 365 |
std::cerr << "n integer"; |
367 | 366 |
break; |
368 | 367 |
case DOUBLE: |
369 | 368 |
std::cerr << " floating point"; |
370 | 369 |
break; |
371 | 370 |
default: |
372 | 371 |
break; |
373 | 372 |
} |
374 | 373 |
std::cerr << " value\n\n"; |
375 | 374 |
showHelp(); |
376 | 375 |
} |
377 | 376 |
|
378 | 377 |
|
379 | 378 |
void ArgParser::checkMandatories() const |
380 | 379 |
{ |
381 | 380 |
bool ok=true; |
382 | 381 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) |
383 | 382 |
if(i->second.mandatory&&!i->second.set) |
384 | 383 |
{ |
385 | 384 |
if(ok) |
386 | 385 |
std::cerr << _command_name |
387 | 386 |
<< ": The following mandatory arguments are missing.\n"; |
388 | 387 |
ok=false; |
389 | 388 |
showHelp(i); |
390 | 389 |
} |
391 | 390 |
for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i) |
392 | 391 |
if(i->second.mandatory||i->second.only_one) |
393 | 392 |
{ |
394 | 393 |
int set=0; |
395 | 394 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
396 | 395 |
o!=i->second.opts.end();++o) |
397 | 396 |
if(_opts.find(*o)->second.set) ++set; |
398 | 397 |
if(i->second.mandatory&&!set) { |
399 | 398 |
std::cerr << _command_name << |
400 | 399 |
": At least one of the following arguments is mandatory.\n"; |
401 | 400 |
ok=false; |
402 | 401 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
403 | 402 |
o!=i->second.opts.end();++o) |
404 | 403 |
showHelp(_opts.find(*o)); |
405 | 404 |
} |
406 | 405 |
if(i->second.only_one&&set>1) { |
407 | 406 |
std::cerr << _command_name << |
408 | 407 |
": At most one of the following arguments can be given.\n"; |
409 | 408 |
ok=false; |
410 | 409 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
411 | 410 |
o!=i->second.opts.end();++o) |
412 | 411 |
showHelp(_opts.find(*o)); |
413 | 412 |
} |
414 | 413 |
} |
415 | 414 |
if(!ok) { |
416 | 415 |
std::cerr << "\nType '" << _command_name << |
417 | 416 |
" --help' to obtain a short summary on the usage.\n\n"; |
418 | 417 |
exit(1); |
419 | 418 |
} |
420 | 419 |
} |
421 | 420 |
|
422 | 421 |
ArgParser &ArgParser::parse() |
423 | 422 |
{ |
424 | 423 |
for(int ar=1; ar<_argc; ++ar) { |
425 | 424 |
std::string arg(_argv[ar]); |
426 | 425 |
if (arg[0] != '-' || arg.size() == 1) { |
427 | 426 |
_file_args.push_back(arg); |
428 | 427 |
} |
429 | 428 |
else { |
430 | 429 |
Opts::iterator i = _opts.find(arg.substr(1)); |
431 | 430 |
if(i==_opts.end()) unknownOpt(arg); |
432 | 431 |
else { |
433 | 432 |
if(i->second.syn) i=_opts.find(i->second.help); |
434 | 433 |
ParData &p(i->second); |
435 | 434 |
if (p.type==BOOL) *p.bool_p=true; |
436 | 435 |
else if (p.type==FUNC) p.func_p.p(p.func_p.data); |
437 | 436 |
else if(++ar==_argc) requiresValue(arg, p.type); |
438 | 437 |
else { |
439 | 438 |
std::string val(_argv[ar]); |
440 | 439 |
std::istringstream vals(val); |
441 | 440 |
switch(p.type) { |
442 | 441 |
case STRING: |
443 | 442 |
*p.string_p=val; |
444 | 443 |
break; |
445 | 444 |
case INTEGER: |
446 | 445 |
vals >> *p.int_p; |
447 | 446 |
break; |
448 | 447 |
case DOUBLE: |
449 | 448 |
vals >> *p.double_p; |
450 | 449 |
break; |
451 | 450 |
default: |
452 | 451 |
break; |
453 | 452 |
} |
454 | 453 |
if(p.type!=STRING&&(!vals||!vals.eof())) |
455 | 454 |
requiresValue(arg, p.type); |
456 | 455 |
} |
457 | 456 |
p.set = true; |
458 | 457 |
} |
459 | 458 |
} |
460 | 459 |
} |
461 | 460 |
checkMandatories(); |
462 | 461 |
|
463 | 462 |
return *this; |
464 | 463 |
} |
465 | 464 |
|
466 | 465 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 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. |
219 | 219 |
///\param obl Indicate if the option is mandatory. |
220 | 220 |
///\retval ref The value of the argument will be written to this variable. |
221 | 221 |
///\note A mandatory bool obtion is of very little use. |
222 | 222 |
ArgParser &refOption(const std::string &name, |
223 | 223 |
const std::string &help, |
224 | 224 |
bool &ref, bool obl=false); |
225 | 225 |
|
226 | 226 |
///Add a new string type option with a storage reference |
227 | 227 |
|
228 | 228 |
///Add a new string type option with a storage reference. |
229 | 229 |
///\param name The name of the option. The leading '-' must be omitted. |
230 | 230 |
///\param help A help string. |
231 | 231 |
///\param obl Indicate if the option is mandatory. |
232 | 232 |
///\retval ref The value of the argument will be written to this variable. |
233 | 233 |
ArgParser &refOption(const std::string &name, |
234 | 234 |
const std::string &help, |
235 | 235 |
std::string &ref, bool obl=false); |
236 | 236 |
|
237 | 237 |
///@} |
238 | 238 |
|
239 | 239 |
///\name Option Groups and Synonyms |
240 | 240 |
/// |
241 | 241 |
|
242 | 242 |
///@{ |
243 | 243 |
|
244 | 244 |
///Bundle some options into a group |
245 | 245 |
|
246 | 246 |
/// You can group some option by calling this function repeatedly for each |
247 | 247 |
/// option to be grouped with the same groupname. |
248 | 248 |
///\param group The group name. |
249 | 249 |
///\param opt The option name. |
250 | 250 |
ArgParser &optionGroup(const std::string &group, |
251 | 251 |
const std::string &opt); |
252 | 252 |
|
253 | 253 |
///Make the members of a group exclusive |
254 | 254 |
|
255 | 255 |
///If you call this function for a group, than at most one of them can be |
256 | 256 |
///given at the same time. |
257 | 257 |
ArgParser &onlyOneGroup(const std::string &group); |
258 | 258 |
|
259 | 259 |
///Make a group mandatory |
260 | 260 |
|
261 | 261 |
///Using this function, at least one of the members of \c group |
262 | 262 |
///must be given. |
263 | 263 |
ArgParser &mandatoryGroup(const std::string &group); |
264 | 264 |
|
265 | 265 |
///Create synonym to an option |
266 | 266 |
|
267 | 267 |
///With this function you can create a synonym \c syn of the |
268 | 268 |
///option \c opt. |
269 | 269 |
ArgParser &synonym(const std::string &syn, |
270 | 270 |
const std::string &opt); |
271 | 271 |
|
272 | 272 |
///@} |
273 | 273 |
|
274 | 274 |
private: |
275 | 275 |
void show(std::ostream &os,Opts::const_iterator i) const; |
276 | 276 |
void show(std::ostream &os,Groups::const_iterator i) const; |
277 | 277 |
void showHelp(Opts::const_iterator i) const; |
278 | 278 |
void showHelp(std::vector<OtherArg>::const_iterator i) const; |
279 | 279 |
|
280 | 280 |
void unknownOpt(std::string arg) const; |
281 | 281 |
|
282 | 282 |
void requiresValue(std::string arg, OptType t) const; |
283 | 283 |
void checkMandatories() const; |
284 | 284 |
|
285 | 285 |
void shortHelp() const; |
286 | 286 |
void showHelp() const; |
287 | 287 |
public: |
288 | 288 |
|
289 | 289 |
///Start the parsing process |
290 | 290 |
ArgParser &parse(); |
291 | 291 |
|
292 | 292 |
/// Synonym for parse() |
293 | 293 |
ArgParser &run() |
294 | 294 |
{ |
295 | 295 |
return parse(); |
296 | 296 |
} |
297 | 297 |
|
298 | 298 |
///Give back the command name (the 0th argument) |
299 | 299 |
const std::string &commandName() const { return _command_name; } |
300 | 300 |
|
301 | 301 |
///Check if an opion has been given to the command. |
302 | 302 |
bool given(std::string op) const |
303 | 303 |
{ |
304 | 304 |
Opts::const_iterator i = _opts.find(op); |
305 | 305 |
return i!=_opts.end()?i->second.set:false; |
306 | 306 |
} |
307 | 307 |
|
308 | 308 |
|
309 | 309 |
///Magic type for operator[] |
310 | 310 |
|
311 | 311 |
///This is the type of the return value of ArgParser::operator[](). |
312 | 312 |
///It automatically converts to \c int, \c double, \c bool or |
313 | 313 |
///\c std::string if the type of the option matches, which is checked |
314 | 314 |
///with an \ref LEMON_ASSERT "assertion" (i.e. it performs runtime |
315 | 315 |
///type checking). |
316 | 316 |
class RefType |
317 | 317 |
{ |
318 | 318 |
const ArgParser &_parser; |
319 | 319 |
std::string _name; |
320 | 320 |
public: |
321 | 321 |
///\e |
322 | 322 |
RefType(const ArgParser &p,const std::string &n) :_parser(p),_name(n) {} |
323 | 323 |
///\e |
324 | 324 |
operator bool() |
325 | 325 |
{ |
326 | 326 |
Opts::const_iterator i = _parser._opts.find(_name); |
327 | 327 |
LEMON_ASSERT(i!=_parser._opts.end(), |
328 | 328 |
std::string()+"Unkown option: '"+_name+"'"); |
329 | 329 |
LEMON_ASSERT(i->second.type==ArgParser::BOOL, |
330 | 330 |
std::string()+"'"+_name+"' is a bool option"); |
331 | 331 |
return *(i->second.bool_p); |
332 | 332 |
} |
333 | 333 |
///\e |
334 | 334 |
operator std::string() |
335 | 335 |
{ |
336 | 336 |
Opts::const_iterator i = _parser._opts.find(_name); |
337 | 337 |
LEMON_ASSERT(i!=_parser._opts.end(), |
338 | 338 |
std::string()+"Unkown option: '"+_name+"'"); |
339 | 339 |
LEMON_ASSERT(i->second.type==ArgParser::STRING, |
340 | 340 |
std::string()+"'"+_name+"' is a string option"); |
341 | 341 |
return *(i->second.string_p); |
342 | 342 |
} |
343 | 343 |
///\e |
344 | 344 |
operator double() |
345 | 345 |
{ |
346 | 346 |
Opts::const_iterator i = _parser._opts.find(_name); |
347 | 347 |
LEMON_ASSERT(i!=_parser._opts.end(), |
348 | 348 |
std::string()+"Unkown option: '"+_name+"'"); |
349 | 349 |
LEMON_ASSERT(i->second.type==ArgParser::DOUBLE || |
350 | 350 |
i->second.type==ArgParser::INTEGER, |
351 | 351 |
std::string()+"'"+_name+"' is a floating point option"); |
352 | 352 |
return i->second.type==ArgParser::DOUBLE ? |
353 | 353 |
*(i->second.double_p) : *(i->second.int_p); |
354 | 354 |
} |
355 | 355 |
///\e |
356 | 356 |
operator int() |
357 | 357 |
{ |
358 | 358 |
Opts::const_iterator i = _parser._opts.find(_name); |
359 | 359 |
LEMON_ASSERT(i!=_parser._opts.end(), |
360 | 360 |
std::string()+"Unkown option: '"+_name+"'"); |
361 | 361 |
LEMON_ASSERT(i->second.type==ArgParser::INTEGER, |
362 | 362 |
std::string()+"'"+_name+"' is an integer option"); |
363 | 363 |
return *(i->second.int_p); |
364 | 364 |
} |
365 | 365 |
|
366 | 366 |
}; |
367 | 367 |
|
368 | 368 |
///Give back the value of an option |
369 | 369 |
|
370 | 370 |
///Give back the value of an option. |
371 | 371 |
///\sa RefType |
372 | 372 |
RefType operator[](const std::string &n) const |
373 | 373 |
{ |
374 | 374 |
return RefType(*this, n); |
375 | 375 |
} |
376 | 376 |
|
377 | 377 |
///Give back the non-option type arguments. |
378 | 378 |
|
379 | 379 |
///Give back a reference to a vector consisting of the program arguments |
380 | 380 |
///not starting with a '-' character. |
381 | 381 |
const std::vector<std::string> &files() const { return _file_args; } |
382 | 382 |
|
383 | 383 |
}; |
384 | 384 |
} |
385 | 385 |
|
386 | 386 |
#endif // LEMON_ARG_PARSER_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 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 |
} |
405 | 405 |
_dist = &m; |
406 | 406 |
return *this; |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
public: |
410 | 410 |
|
411 | 411 |
///\name Execution control |
412 | 412 |
///The simplest way to execute the algorithm is to use |
413 | 413 |
///one of the member functions called \ref lemon::Bfs::run() "run()". |
414 | 414 |
///\n |
415 | 415 |
///If you need more control on the execution, first you must call |
416 | 416 |
///\ref lemon::Bfs::init() "init()", then you can add several source |
417 | 417 |
///nodes with \ref lemon::Bfs::addSource() "addSource()". |
418 | 418 |
///Finally \ref lemon::Bfs::start() "start()" will perform the |
419 | 419 |
///actual path computation. |
420 | 420 |
|
421 | 421 |
///@{ |
422 | 422 |
|
423 | 423 |
///Initializes the internal data structures. |
424 | 424 |
|
425 | 425 |
///Initializes the internal data structures. |
426 | 426 |
/// |
427 | 427 |
void init() |
428 | 428 |
{ |
429 | 429 |
create_maps(); |
430 | 430 |
_queue.resize(countNodes(*G)); |
431 | 431 |
_queue_head=_queue_tail=0; |
432 | 432 |
_curr_dist=1; |
433 | 433 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
434 | 434 |
_pred->set(u,INVALID); |
435 | 435 |
_reached->set(u,false); |
436 | 436 |
_processed->set(u,false); |
437 | 437 |
} |
438 | 438 |
} |
439 | 439 |
|
440 | 440 |
///Adds a new source node. |
441 | 441 |
|
442 | 442 |
///Adds a new source node to the set of nodes to be processed. |
443 | 443 |
/// |
444 | 444 |
void addSource(Node s) |
445 | 445 |
{ |
446 | 446 |
if(!(*_reached)[s]) |
447 | 447 |
{ |
448 | 448 |
_reached->set(s,true); |
449 | 449 |
_pred->set(s,INVALID); |
450 | 450 |
_dist->set(s,0); |
451 | 451 |
_queue[_queue_head++]=s; |
452 | 452 |
_queue_next_dist=_queue_head; |
453 | 453 |
} |
454 | 454 |
} |
455 | 455 |
|
456 | 456 |
///Processes the next node. |
457 | 457 |
|
458 | 458 |
///Processes the next node. |
459 | 459 |
/// |
460 | 460 |
///\return The processed node. |
461 | 461 |
/// |
462 | 462 |
///\pre The queue must not be empty. |
463 | 463 |
Node processNextNode() |
464 | 464 |
{ |
465 | 465 |
if(_queue_tail==_queue_next_dist) { |
466 | 466 |
_curr_dist++; |
467 | 467 |
_queue_next_dist=_queue_head; |
468 | 468 |
} |
469 | 469 |
Node n=_queue[_queue_tail++]; |
470 | 470 |
_processed->set(n,true); |
471 | 471 |
Node m; |
472 | 472 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
473 | 473 |
if(!(*_reached)[m=G->target(e)]) { |
474 | 474 |
_queue[_queue_head++]=m; |
475 | 475 |
_reached->set(m,true); |
476 | 476 |
_pred->set(m,e); |
477 | 477 |
_dist->set(m,_curr_dist); |
478 | 478 |
} |
479 | 479 |
return n; |
480 | 480 |
} |
481 | 481 |
|
482 | 482 |
///Processes the next node. |
483 | 483 |
|
484 | 484 |
///Processes the next node and checks if the given target node |
485 | 485 |
///is reached. If the target node is reachable from the processed |
486 | 486 |
///node, then the \c reach parameter will be set to \c true. |
487 | 487 |
/// |
488 | 488 |
///\param target The target node. |
489 | 489 |
///\retval reach Indicates if the target node is reached. |
490 | 490 |
///It should be initially \c false. |
491 | 491 |
/// |
492 | 492 |
///\return The processed node. |
493 | 493 |
/// |
494 | 494 |
///\pre The queue must not be empty. |
495 | 495 |
Node processNextNode(Node target, bool& reach) |
496 | 496 |
{ |
497 | 497 |
if(_queue_tail==_queue_next_dist) { |
498 | 498 |
_curr_dist++; |
499 | 499 |
_queue_next_dist=_queue_head; |
500 | 500 |
} |
501 | 501 |
Node n=_queue[_queue_tail++]; |
502 | 502 |
_processed->set(n,true); |
503 | 503 |
Node m; |
504 | 504 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
505 | 505 |
if(!(*_reached)[m=G->target(e)]) { |
506 | 506 |
_queue[_queue_head++]=m; |
507 | 507 |
_reached->set(m,true); |
508 | 508 |
_pred->set(m,e); |
509 | 509 |
_dist->set(m,_curr_dist); |
510 | 510 |
reach = reach || (target == m); |
511 | 511 |
} |
512 | 512 |
return n; |
513 | 513 |
} |
514 | 514 |
|
515 | 515 |
///Processes the next node. |
516 | 516 |
|
517 | 517 |
///Processes the next node and checks if at least one of reached |
518 | 518 |
///nodes has \c true value in the \c nm node map. If one node |
519 | 519 |
///with \c true value is reachable from the processed node, then the |
520 | 520 |
///\c rnode parameter will be set to the first of such nodes. |
521 | 521 |
/// |
522 | 522 |
///\param nm A \c bool (or convertible) node map that indicates the |
523 | 523 |
///possible targets. |
524 | 524 |
///\retval rnode The reached target node. |
525 | 525 |
///It should be initially \c INVALID. |
526 | 526 |
/// |
527 | 527 |
///\return The processed node. |
528 | 528 |
/// |
529 | 529 |
///\pre The queue must not be empty. |
530 | 530 |
template<class NM> |
531 | 531 |
Node processNextNode(const NM& nm, Node& rnode) |
532 | 532 |
{ |
533 | 533 |
if(_queue_tail==_queue_next_dist) { |
534 | 534 |
_curr_dist++; |
535 | 535 |
_queue_next_dist=_queue_head; |
536 | 536 |
} |
537 | 537 |
Node n=_queue[_queue_tail++]; |
538 | 538 |
_processed->set(n,true); |
539 | 539 |
Node m; |
540 | 540 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
541 | 541 |
if(!(*_reached)[m=G->target(e)]) { |
542 | 542 |
_queue[_queue_head++]=m; |
543 | 543 |
_reached->set(m,true); |
544 | 544 |
_pred->set(m,e); |
545 | 545 |
_dist->set(m,_curr_dist); |
546 | 546 |
if (nm[m] && rnode == INVALID) rnode = m; |
547 | 547 |
} |
548 | 548 |
return n; |
549 | 549 |
} |
550 | 550 |
|
551 | 551 |
///The next node to be processed. |
552 | 552 |
|
553 | 553 |
///Returns the next node to be processed or \c INVALID if the queue |
554 | 554 |
///is empty. |
555 | 555 |
Node nextNode() const |
556 | 556 |
{ |
557 | 557 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
558 | 558 |
} |
559 | 559 |
|
560 | 560 |
///\brief Returns \c false if there are nodes |
561 | 561 |
///to be processed. |
562 | 562 |
/// |
563 | 563 |
///Returns \c false if there are nodes |
564 | 564 |
///to be processed in the queue. |
565 | 565 |
bool emptyQueue() const { return _queue_tail==_queue_head; } |
566 | 566 |
|
567 | 567 |
///Returns the number of the nodes to be processed. |
568 | 568 |
|
569 | 569 |
///Returns the number of the nodes to be processed in the queue. |
570 | 570 |
int queueSize() const { return _queue_head-_queue_tail; } |
571 | 571 |
|
572 | 572 |
///Executes the algorithm. |
573 | 573 |
|
574 | 574 |
///Executes the algorithm. |
575 | 575 |
/// |
576 | 576 |
///This method runs the %BFS algorithm from the root node(s) |
577 | 577 |
///in order to compute the shortest path to each node. |
578 | 578 |
/// |
579 | 579 |
///The algorithm computes |
580 | 580 |
///- the shortest path tree (forest), |
581 | 581 |
///- the distance of each node from the root(s). |
582 | 582 |
/// |
583 | 583 |
///\pre init() must be called and at least one root node should be |
584 | 584 |
///added with addSource() before using this function. |
585 | 585 |
/// |
586 | 586 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
587 | 587 |
///\code |
588 | 588 |
/// while ( !b.emptyQueue() ) { |
589 | 589 |
/// b.processNextNode(); |
590 | 590 |
/// } |
591 | 591 |
///\endcode |
592 | 592 |
void start() |
593 | 593 |
{ |
594 | 594 |
while ( !emptyQueue() ) processNextNode(); |
595 | 595 |
} |
596 | 596 |
|
597 | 597 |
///Executes the algorithm until the given target node is reached. |
598 | 598 |
|
599 | 599 |
///Executes the algorithm until the given target node is reached. |
600 | 600 |
/// |
601 | 601 |
///This method runs the %BFS algorithm from the root node(s) |
602 | 602 |
///in order to compute the shortest path to \c t. |
603 | 603 |
/// |
604 | 604 |
///The algorithm computes |
605 | 605 |
///- the shortest path to \c t, |
606 | 606 |
///- the distance of \c t from the root(s). |
607 | 607 |
/// |
608 | 608 |
///\pre init() must be called and at least one root node should be |
609 | 609 |
///added with addSource() before using this function. |
610 | 610 |
/// |
611 | 611 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
612 | 612 |
///\code |
613 | 613 |
/// bool reach = false; |
614 | 614 |
/// while ( !b.emptyQueue() && !reach ) { |
615 | 615 |
/// b.processNextNode(t, reach); |
616 | 616 |
/// } |
617 | 617 |
///\endcode |
618 | 618 |
void start(Node t) |
619 | 619 |
{ |
620 | 620 |
bool reach = false; |
621 | 621 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
622 | 622 |
} |
623 | 623 |
|
624 | 624 |
///Executes the algorithm until a condition is met. |
625 | 625 |
|
626 | 626 |
///Executes the algorithm until a condition is met. |
627 | 627 |
/// |
628 | 628 |
///This method runs the %BFS algorithm from the root node(s) in |
629 | 629 |
///order to compute the shortest path to a node \c v with |
630 | 630 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
631 | 631 |
/// |
632 | 632 |
///\param nm A \c bool (or convertible) node map. The algorithm |
633 | 633 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
634 | 634 |
/// |
635 | 635 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
636 | 636 |
///\c INVALID if no such node was found. |
637 | 637 |
/// |
638 | 638 |
///\pre init() must be called and at least one root node should be |
639 | 639 |
///added with addSource() before using this function. |
640 | 640 |
/// |
641 | 641 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
642 | 642 |
///\code |
643 | 643 |
/// Node rnode = INVALID; |
644 | 644 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
645 | 645 |
/// b.processNextNode(nm, rnode); |
646 | 646 |
/// } |
647 | 647 |
/// return rnode; |
648 | 648 |
///\endcode |
649 | 649 |
template<class NodeBoolMap> |
650 | 650 |
Node start(const NodeBoolMap &nm) |
651 | 651 |
{ |
652 | 652 |
Node rnode = INVALID; |
653 | 653 |
while ( !emptyQueue() && rnode == INVALID ) { |
654 | 654 |
processNextNode(nm, rnode); |
655 | 655 |
} |
656 | 656 |
return rnode; |
657 | 657 |
} |
658 | 658 |
|
659 | 659 |
///Runs the algorithm from the given source node. |
660 | 660 |
|
661 | 661 |
///This method runs the %BFS algorithm from node \c s |
662 | 662 |
///in order to compute the shortest path to each node. |
663 | 663 |
/// |
664 | 664 |
///The algorithm computes |
665 | 665 |
///- the shortest path tree, |
666 | 666 |
///- the distance of each node from the root. |
667 | 667 |
/// |
668 | 668 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
669 | 669 |
///\code |
670 | 670 |
/// b.init(); |
671 | 671 |
/// b.addSource(s); |
672 | 672 |
/// b.start(); |
673 | 673 |
///\endcode |
674 | 674 |
void run(Node s) { |
675 | 675 |
init(); |
676 | 676 |
addSource(s); |
677 | 677 |
start(); |
678 | 678 |
} |
679 | 679 |
|
680 | 680 |
///Finds the shortest path between \c s and \c t. |
681 | 681 |
|
682 | 682 |
///This method runs the %BFS algorithm from node \c s |
683 | 683 |
///in order to compute the shortest path to node \c t |
684 | 684 |
///(it stops searching when \c t is processed). |
685 | 685 |
/// |
686 | 686 |
///\return \c true if \c t is reachable form \c s. |
687 | 687 |
/// |
688 | 688 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
689 | 689 |
///shortcut of the following code. |
690 | 690 |
///\code |
691 | 691 |
/// b.init(); |
692 | 692 |
/// b.addSource(s); |
693 | 693 |
/// b.start(t); |
694 | 694 |
///\endcode |
695 | 695 |
bool run(Node s,Node t) { |
696 | 696 |
init(); |
697 | 697 |
addSource(s); |
698 | 698 |
start(t); |
699 | 699 |
return reached(t); |
700 | 700 |
} |
701 | 701 |
|
702 | 702 |
///Runs the algorithm to visit all nodes in the digraph. |
703 | 703 |
|
704 | 704 |
///This method runs the %BFS algorithm in order to |
705 | 705 |
///compute the shortest path to each node. |
706 | 706 |
/// |
707 | 707 |
///The algorithm computes |
708 | 708 |
///- the shortest path tree (forest), |
709 | 709 |
///- the distance of each node from the root(s). |
710 | 710 |
/// |
711 | 711 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
712 | 712 |
///\code |
713 | 713 |
/// b.init(); |
714 | 714 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
715 | 715 |
/// if (!b.reached(n)) { |
716 | 716 |
/// b.addSource(n); |
717 | 717 |
/// b.start(); |
718 | 718 |
/// } |
719 | 719 |
/// } |
720 | 720 |
///\endcode |
721 | 721 |
void run() { |
722 | 722 |
init(); |
723 | 723 |
for (NodeIt n(*G); n != INVALID; ++n) { |
724 | 724 |
if (!reached(n)) { |
725 | 725 |
addSource(n); |
726 | 726 |
start(); |
727 | 727 |
} |
728 | 728 |
} |
729 | 729 |
} |
730 | 730 |
|
731 | 731 |
///@} |
732 | 732 |
|
733 | 733 |
///\name Query Functions |
734 | 734 |
///The result of the %BFS algorithm can be obtained using these |
735 | 735 |
///functions.\n |
736 | 736 |
///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start() |
737 | 737 |
///"start()" must be called before using them. |
738 | 738 |
|
739 | 739 |
///@{ |
740 | 740 |
|
741 | 741 |
///The shortest path to a node. |
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> |
1371 | 1371 |
struct SetReachedMapTraits : public Traits { |
1372 | 1372 |
typedef T ReachedMap; |
1373 | 1373 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1374 | 1374 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
1375 | 1375 |
return 0; // ignore warnings |
1376 | 1376 |
} |
1377 | 1377 |
}; |
1378 | 1378 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1379 | 1379 |
/// ReachedMap type. |
1380 | 1380 |
/// |
1381 | 1381 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1382 | 1382 |
template <class T> |
1383 | 1383 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
1384 | 1384 |
SetReachedMapTraits<T> > { |
1385 | 1385 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1386 | 1386 |
}; |
1387 | 1387 |
///@} |
1388 | 1388 |
|
1389 | 1389 |
public: |
1390 | 1390 |
|
1391 | 1391 |
/// \brief Constructor. |
1392 | 1392 |
/// |
1393 | 1393 |
/// Constructor. |
1394 | 1394 |
/// |
1395 | 1395 |
/// \param digraph The digraph the algorithm runs on. |
1396 | 1396 |
/// \param visitor The visitor object of the algorithm. |
1397 | 1397 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1398 | 1398 |
: _digraph(&digraph), _visitor(&visitor), |
1399 | 1399 |
_reached(0), local_reached(false) {} |
1400 | 1400 |
|
1401 | 1401 |
/// \brief Destructor. |
1402 | 1402 |
~BfsVisit() { |
1403 | 1403 |
if(local_reached) delete _reached; |
1404 | 1404 |
} |
1405 | 1405 |
|
1406 | 1406 |
/// \brief Sets the map that indicates which nodes are reached. |
1407 | 1407 |
/// |
1408 | 1408 |
/// Sets the map that indicates which nodes are reached. |
1409 | 1409 |
/// If you don't use this function before calling \ref run(), |
1410 | 1410 |
/// it will allocate one. The destructor deallocates this |
1411 | 1411 |
/// automatically allocated map, of course. |
1412 | 1412 |
/// \return <tt> (*this) </tt> |
1413 | 1413 |
BfsVisit &reachedMap(ReachedMap &m) { |
1414 | 1414 |
if(local_reached) { |
1415 | 1415 |
delete _reached; |
1416 | 1416 |
local_reached = false; |
1417 | 1417 |
} |
1418 | 1418 |
_reached = &m; |
1419 | 1419 |
return *this; |
1420 | 1420 |
} |
1421 | 1421 |
|
1422 | 1422 |
public: |
1423 | 1423 |
|
1424 | 1424 |
/// \name Execution control |
1425 | 1425 |
/// The simplest way to execute the algorithm is to use |
1426 | 1426 |
/// one of the member functions called \ref lemon::BfsVisit::run() |
1427 | 1427 |
/// "run()". |
1428 | 1428 |
/// \n |
1429 | 1429 |
/// If you need more control on the execution, first you must call |
1430 | 1430 |
/// \ref lemon::BfsVisit::init() "init()", then you can add several |
1431 | 1431 |
/// source nodes with \ref lemon::BfsVisit::addSource() "addSource()". |
1432 | 1432 |
/// Finally \ref lemon::BfsVisit::start() "start()" will perform the |
1433 | 1433 |
/// actual path computation. |
1434 | 1434 |
|
1435 | 1435 |
/// @{ |
1436 | 1436 |
|
1437 | 1437 |
/// \brief Initializes the internal data structures. |
1438 | 1438 |
/// |
1439 | 1439 |
/// Initializes the internal data structures. |
1440 | 1440 |
void init() { |
1441 | 1441 |
create_maps(); |
1442 | 1442 |
_list.resize(countNodes(*_digraph)); |
1443 | 1443 |
_list_front = _list_back = -1; |
1444 | 1444 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1445 | 1445 |
_reached->set(u, false); |
1446 | 1446 |
} |
1447 | 1447 |
} |
1448 | 1448 |
|
1449 | 1449 |
/// \brief Adds a new source node. |
1450 | 1450 |
/// |
1451 | 1451 |
/// Adds a new source node to the set of nodes to be processed. |
1452 | 1452 |
void addSource(Node s) { |
1453 | 1453 |
if(!(*_reached)[s]) { |
1454 | 1454 |
_reached->set(s,true); |
1455 | 1455 |
_visitor->start(s); |
1456 | 1456 |
_visitor->reach(s); |
1457 | 1457 |
_list[++_list_back] = s; |
1458 | 1458 |
} |
1459 | 1459 |
} |
1460 | 1460 |
|
1461 | 1461 |
/// \brief Processes the next node. |
1462 | 1462 |
/// |
1463 | 1463 |
/// Processes the next node. |
1464 | 1464 |
/// |
1465 | 1465 |
/// \return The processed node. |
1466 | 1466 |
/// |
1467 | 1467 |
/// \pre The queue must not be empty. |
1468 | 1468 |
Node processNextNode() { |
1469 | 1469 |
Node n = _list[++_list_front]; |
1470 | 1470 |
_visitor->process(n); |
1471 | 1471 |
Arc e; |
1472 | 1472 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1473 | 1473 |
Node m = _digraph->target(e); |
1474 | 1474 |
if (!(*_reached)[m]) { |
1475 | 1475 |
_visitor->discover(e); |
1476 | 1476 |
_visitor->reach(m); |
1477 | 1477 |
_reached->set(m, true); |
1478 | 1478 |
_list[++_list_back] = m; |
1479 | 1479 |
} else { |
1480 | 1480 |
_visitor->examine(e); |
1481 | 1481 |
} |
1482 | 1482 |
} |
1483 | 1483 |
return n; |
1484 | 1484 |
} |
1485 | 1485 |
|
1486 | 1486 |
/// \brief Processes the next node. |
1487 | 1487 |
/// |
1488 | 1488 |
/// Processes the next node and checks if the given target node |
1489 | 1489 |
/// is reached. If the target node is reachable from the processed |
1490 | 1490 |
/// node, then the \c reach parameter will be set to \c true. |
1491 | 1491 |
/// |
1492 | 1492 |
/// \param target The target node. |
1493 | 1493 |
/// \retval reach Indicates if the target node is reached. |
1494 | 1494 |
/// It should be initially \c false. |
1495 | 1495 |
/// |
1496 | 1496 |
/// \return The processed node. |
1497 | 1497 |
/// |
1498 | 1498 |
/// \pre The queue must not be empty. |
1499 | 1499 |
Node processNextNode(Node target, bool& reach) { |
1500 | 1500 |
Node n = _list[++_list_front]; |
1501 | 1501 |
_visitor->process(n); |
1502 | 1502 |
Arc e; |
1503 | 1503 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1504 | 1504 |
Node m = _digraph->target(e); |
1505 | 1505 |
if (!(*_reached)[m]) { |
1506 | 1506 |
_visitor->discover(e); |
1507 | 1507 |
_visitor->reach(m); |
1508 | 1508 |
_reached->set(m, true); |
1509 | 1509 |
_list[++_list_back] = m; |
1510 | 1510 |
reach = reach || (target == m); |
1511 | 1511 |
} else { |
1512 | 1512 |
_visitor->examine(e); |
1513 | 1513 |
} |
1514 | 1514 |
} |
1515 | 1515 |
return n; |
1516 | 1516 |
} |
1517 | 1517 |
|
1518 | 1518 |
/// \brief Processes the next node. |
1519 | 1519 |
/// |
1520 | 1520 |
/// Processes the next node and checks if at least one of reached |
1521 | 1521 |
/// nodes has \c true value in the \c nm node map. If one node |
1522 | 1522 |
/// with \c true value is reachable from the processed node, then the |
1523 | 1523 |
/// \c rnode parameter will be set to the first of such nodes. |
1524 | 1524 |
/// |
1525 | 1525 |
/// \param nm A \c bool (or convertible) node map that indicates the |
1526 | 1526 |
/// possible targets. |
1527 | 1527 |
/// \retval rnode The reached target node. |
1528 | 1528 |
/// It should be initially \c INVALID. |
1529 | 1529 |
/// |
1530 | 1530 |
/// \return The processed node. |
1531 | 1531 |
/// |
1532 | 1532 |
/// \pre The queue must not be empty. |
1533 | 1533 |
template <typename NM> |
1534 | 1534 |
Node processNextNode(const NM& nm, Node& rnode) { |
1535 | 1535 |
Node n = _list[++_list_front]; |
1536 | 1536 |
_visitor->process(n); |
1537 | 1537 |
Arc e; |
1538 | 1538 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1539 | 1539 |
Node m = _digraph->target(e); |
1540 | 1540 |
if (!(*_reached)[m]) { |
1541 | 1541 |
_visitor->discover(e); |
1542 | 1542 |
_visitor->reach(m); |
1543 | 1543 |
_reached->set(m, true); |
1544 | 1544 |
_list[++_list_back] = m; |
1545 | 1545 |
if (nm[m] && rnode == INVALID) rnode = m; |
1546 | 1546 |
} else { |
1547 | 1547 |
_visitor->examine(e); |
1548 | 1548 |
} |
1549 | 1549 |
} |
1550 | 1550 |
return n; |
1551 | 1551 |
} |
1552 | 1552 |
|
1553 | 1553 |
/// \brief The next node to be processed. |
1554 | 1554 |
/// |
1555 | 1555 |
/// Returns the next node to be processed or \c INVALID if the queue |
1556 | 1556 |
/// is empty. |
1557 | 1557 |
Node nextNode() const { |
1558 | 1558 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1559 | 1559 |
} |
1560 | 1560 |
|
1561 | 1561 |
/// \brief Returns \c false if there are nodes |
1562 | 1562 |
/// to be processed. |
1563 | 1563 |
/// |
1564 | 1564 |
/// Returns \c false if there are nodes |
1565 | 1565 |
/// to be processed in the queue. |
1566 | 1566 |
bool emptyQueue() const { return _list_front == _list_back; } |
1567 | 1567 |
|
1568 | 1568 |
/// \brief Returns the number of the nodes to be processed. |
1569 | 1569 |
/// |
1570 | 1570 |
/// Returns the number of the nodes to be processed in the queue. |
1571 | 1571 |
int queueSize() const { return _list_back - _list_front; } |
1572 | 1572 |
|
1573 | 1573 |
/// \brief Executes the algorithm. |
1574 | 1574 |
/// |
1575 | 1575 |
/// Executes the algorithm. |
1576 | 1576 |
/// |
1577 | 1577 |
/// This method runs the %BFS algorithm from the root node(s) |
1578 | 1578 |
/// in order to compute the shortest path to each node. |
1579 | 1579 |
/// |
1580 | 1580 |
/// The algorithm computes |
1581 | 1581 |
/// - the shortest path tree (forest), |
1582 | 1582 |
/// - the distance of each node from the root(s). |
1583 | 1583 |
/// |
1584 | 1584 |
/// \pre init() must be called and at least one root node should be added |
1585 | 1585 |
/// with addSource() before using this function. |
1586 | 1586 |
/// |
1587 | 1587 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code. |
1588 | 1588 |
/// \code |
1589 | 1589 |
/// while ( !b.emptyQueue() ) { |
1590 | 1590 |
/// b.processNextNode(); |
1591 | 1591 |
/// } |
1592 | 1592 |
/// \endcode |
1593 | 1593 |
void start() { |
1594 | 1594 |
while ( !emptyQueue() ) processNextNode(); |
1595 | 1595 |
} |
1596 | 1596 |
|
1597 | 1597 |
/// \brief Executes the algorithm until the given target node is reached. |
1598 | 1598 |
/// |
1599 | 1599 |
/// Executes the algorithm until the given target node is reached. |
1600 | 1600 |
/// |
1601 | 1601 |
/// This method runs the %BFS algorithm from the root node(s) |
1602 | 1602 |
/// in order to compute the shortest path to \c t. |
1603 | 1603 |
/// |
1604 | 1604 |
/// The algorithm computes |
1605 | 1605 |
/// - the shortest path to \c t, |
1606 | 1606 |
/// - the distance of \c t from the root(s). |
1607 | 1607 |
/// |
1608 | 1608 |
/// \pre init() must be called and at least one root node should be |
1609 | 1609 |
/// added with addSource() before using this function. |
1610 | 1610 |
/// |
1611 | 1611 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
1612 | 1612 |
/// \code |
1613 | 1613 |
/// bool reach = false; |
1614 | 1614 |
/// while ( !b.emptyQueue() && !reach ) { |
1615 | 1615 |
/// b.processNextNode(t, reach); |
1616 | 1616 |
/// } |
1617 | 1617 |
/// \endcode |
1618 | 1618 |
void start(Node t) { |
1619 | 1619 |
bool reach = false; |
1620 | 1620 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
1621 | 1621 |
} |
1622 | 1622 |
|
1623 | 1623 |
/// \brief Executes the algorithm until a condition is met. |
1624 | 1624 |
/// |
1625 | 1625 |
/// Executes the algorithm until a condition is met. |
1626 | 1626 |
/// |
1627 | 1627 |
/// This method runs the %BFS algorithm from the root node(s) in |
1628 | 1628 |
/// order to compute the shortest path to a node \c v with |
1629 | 1629 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
1630 | 1630 |
/// |
1631 | 1631 |
/// \param nm must be a bool (or convertible) node map. The |
1632 | 1632 |
/// algorithm will stop when it reaches a node \c v with |
1633 | 1633 |
/// <tt>nm[v]</tt> true. |
1634 | 1634 |
/// |
1635 | 1635 |
/// \return The reached node \c v with <tt>nm[v]</tt> true or |
1636 | 1636 |
/// \c INVALID if no such node was found. |
1637 | 1637 |
/// |
1638 | 1638 |
/// \pre init() must be called and at least one root node should be |
1639 | 1639 |
/// added with addSource() before using this function. |
1640 | 1640 |
/// |
1641 | 1641 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
1642 | 1642 |
/// \code |
1643 | 1643 |
/// Node rnode = INVALID; |
1644 | 1644 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
1645 | 1645 |
/// b.processNextNode(nm, rnode); |
1646 | 1646 |
/// } |
1647 | 1647 |
/// return rnode; |
1648 | 1648 |
/// \endcode |
1649 | 1649 |
template <typename NM> |
1650 | 1650 |
Node start(const NM &nm) { |
1651 | 1651 |
Node rnode = INVALID; |
1652 | 1652 |
while ( !emptyQueue() && rnode == INVALID ) { |
1653 | 1653 |
processNextNode(nm, rnode); |
1654 | 1654 |
} |
1655 | 1655 |
return rnode; |
1656 | 1656 |
} |
1657 | 1657 |
|
1658 | 1658 |
/// \brief Runs the algorithm from the given source node. |
1659 | 1659 |
/// |
1660 | 1660 |
/// This method runs the %BFS algorithm from node \c s |
1661 | 1661 |
/// in order to compute the shortest path to each node. |
1662 | 1662 |
/// |
1663 | 1663 |
/// The algorithm computes |
1664 | 1664 |
/// - the shortest path tree, |
1665 | 1665 |
/// - the distance of each node from the root. |
1666 | 1666 |
/// |
1667 | 1667 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1668 | 1668 |
///\code |
1669 | 1669 |
/// b.init(); |
1670 | 1670 |
/// b.addSource(s); |
1671 | 1671 |
/// b.start(); |
1672 | 1672 |
///\endcode |
1673 | 1673 |
void run(Node s) { |
1674 | 1674 |
init(); |
1675 | 1675 |
addSource(s); |
1676 | 1676 |
start(); |
1677 | 1677 |
} |
1678 | 1678 |
|
1679 | 1679 |
/// \brief Finds the shortest path between \c s and \c t. |
1680 | 1680 |
/// |
1681 | 1681 |
/// This method runs the %BFS algorithm from node \c s |
1682 | 1682 |
/// in order to compute the shortest path to node \c t |
1683 | 1683 |
/// (it stops searching when \c t is processed). |
1684 | 1684 |
/// |
1685 | 1685 |
/// \return \c true if \c t is reachable form \c s. |
1686 | 1686 |
/// |
1687 | 1687 |
/// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
1688 | 1688 |
/// shortcut of the following code. |
1689 | 1689 |
///\code |
1690 | 1690 |
/// b.init(); |
1691 | 1691 |
/// b.addSource(s); |
1692 | 1692 |
/// b.start(t); |
1693 | 1693 |
///\endcode |
1694 | 1694 |
bool run(Node s,Node t) { |
1695 | 1695 |
init(); |
1696 | 1696 |
addSource(s); |
1697 | 1697 |
start(t); |
1698 | 1698 |
return reached(t); |
1699 | 1699 |
} |
1700 | 1700 |
|
1701 | 1701 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1702 | 1702 |
/// |
1703 | 1703 |
/// This method runs the %BFS algorithm in order to |
1704 | 1704 |
/// compute the shortest path to each node. |
1705 | 1705 |
/// |
1706 | 1706 |
/// The algorithm computes |
1707 | 1707 |
/// - the shortest path tree (forest), |
1708 | 1708 |
/// - the distance of each node from the root(s). |
1709 | 1709 |
/// |
1710 | 1710 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1711 | 1711 |
///\code |
1712 | 1712 |
/// b.init(); |
1713 | 1713 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1714 | 1714 |
/// if (!b.reached(n)) { |
1715 | 1715 |
/// b.addSource(n); |
1716 | 1716 |
/// b.start(); |
1717 | 1717 |
/// } |
1718 | 1718 |
/// } |
1719 | 1719 |
///\endcode |
1720 | 1720 |
void run() { |
1721 | 1721 |
init(); |
1722 | 1722 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1723 | 1723 |
if (!reached(it)) { |
1724 | 1724 |
addSource(it); |
1725 | 1725 |
start(); |
1726 | 1726 |
} |
1727 | 1727 |
} |
1728 | 1728 |
} |
1729 | 1729 |
|
1730 | 1730 |
///@} |
1731 | 1731 |
|
1732 | 1732 |
/// \name Query Functions |
1733 | 1733 |
/// The result of the %BFS algorithm can be obtained using these |
1734 | 1734 |
/// functions.\n |
1735 | 1735 |
/// Either \ref lemon::BfsVisit::run() "run()" or |
1736 | 1736 |
/// \ref lemon::BfsVisit::start() "start()" must be called before |
1737 | 1737 |
/// using them. |
1738 | 1738 |
///@{ |
1739 | 1739 |
|
1740 | 1740 |
/// \brief Checks if a node is reachable from the root(s). |
1741 | 1741 |
/// |
1742 | 1742 |
/// Returns \c true if \c v is reachable from the root(s). |
1743 | 1743 |
/// \pre Either \ref run() or \ref start() |
1744 | 1744 |
/// must be called before using this function. |
1745 | 1745 |
bool reached(Node v) { return (*_reached)[v]; } |
1746 | 1746 |
|
1747 | 1747 |
///@} |
1748 | 1748 |
|
1749 | 1749 |
}; |
1750 | 1750 |
|
1751 | 1751 |
} //END OF NAMESPACE LEMON |
1752 | 1752 |
|
1753 | 1753 |
#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_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 |
|
204 | 204 |
Edge edgeFromId(int ix) const { |
205 | 205 |
return Parent::arcFromId(ix); |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
int id(const Node &n) const { |
209 | 209 |
return Parent::id(n); |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
int id(const Edge &e) const { |
213 | 213 |
return Parent::id(e); |
214 | 214 |
} |
215 | 215 |
|
216 | 216 |
int id(const Arc &e) const { |
217 | 217 |
return 2 * Parent::id(e) + int(e.forward); |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
int maxNodeId() const { |
221 | 221 |
return Parent::maxNodeId(); |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
int maxArcId() const { |
225 | 225 |
return 2 * Parent::maxArcId() + 1; |
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
int maxEdgeId() const { |
229 | 229 |
return Parent::maxArcId(); |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
int arcNum() const { |
233 | 233 |
return 2 * Parent::arcNum(); |
234 | 234 |
} |
235 | 235 |
|
236 | 236 |
int edgeNum() const { |
237 | 237 |
return Parent::arcNum(); |
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
241 | 241 |
if (p == INVALID) { |
242 | 242 |
Edge arc = Parent::findArc(s, t); |
243 | 243 |
if (arc != INVALID) return direct(arc, true); |
244 | 244 |
arc = Parent::findArc(t, s); |
245 | 245 |
if (arc != INVALID) return direct(arc, false); |
246 | 246 |
} else if (direction(p)) { |
247 | 247 |
Edge arc = Parent::findArc(s, t, p); |
248 | 248 |
if (arc != INVALID) return direct(arc, true); |
249 | 249 |
arc = Parent::findArc(t, s); |
250 | 250 |
if (arc != INVALID) return direct(arc, false); |
251 | 251 |
} else { |
252 | 252 |
Edge arc = Parent::findArc(t, s, p); |
253 | 253 |
if (arc != INVALID) return direct(arc, false); |
254 | 254 |
} |
255 | 255 |
return INVALID; |
256 | 256 |
} |
257 | 257 |
|
258 | 258 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
259 | 259 |
if (s != t) { |
260 | 260 |
if (p == INVALID) { |
261 | 261 |
Edge arc = Parent::findArc(s, t); |
262 | 262 |
if (arc != INVALID) return arc; |
263 | 263 |
arc = Parent::findArc(t, s); |
264 | 264 |
if (arc != INVALID) return arc; |
265 | 265 |
} else if (Parent::s(p) == s) { |
266 | 266 |
Edge arc = Parent::findArc(s, t, p); |
267 | 267 |
if (arc != INVALID) return arc; |
268 | 268 |
arc = Parent::findArc(t, s); |
269 | 269 |
if (arc != INVALID) return arc; |
270 | 270 |
} else { |
271 | 271 |
Edge arc = Parent::findArc(t, s, p); |
272 | 272 |
if (arc != INVALID) return arc; |
273 | 273 |
} |
274 | 274 |
} else { |
275 | 275 |
return Parent::findArc(s, t, p); |
276 | 276 |
} |
277 | 277 |
return INVALID; |
278 | 278 |
} |
279 | 279 |
}; |
280 | 280 |
|
281 | 281 |
template <typename Base> |
282 | 282 |
class BidirBpGraphExtender : public Base { |
283 | 283 |
public: |
284 | 284 |
typedef Base Parent; |
285 | 285 |
typedef BidirBpGraphExtender Digraph; |
286 | 286 |
|
287 | 287 |
typedef typename Parent::Node Node; |
288 | 288 |
typedef typename Parent::Edge Edge; |
289 | 289 |
|
290 | 290 |
|
291 | 291 |
using Parent::first; |
292 | 292 |
using Parent::next; |
293 | 293 |
|
294 | 294 |
using Parent::id; |
295 | 295 |
|
296 | 296 |
class Red : public Node { |
297 | 297 |
friend class BidirBpGraphExtender; |
298 | 298 |
public: |
299 | 299 |
Red() {} |
300 | 300 |
Red(const Node& node) : Node(node) { |
301 | 301 |
LEMON_DEBUG(Parent::red(node) || node == INVALID, |
302 | 302 |
typename Parent::NodeSetError()); |
303 | 303 |
} |
304 | 304 |
Red& operator=(const Node& node) { |
305 | 305 |
LEMON_DEBUG(Parent::red(node) || node == INVALID, |
306 | 306 |
typename Parent::NodeSetError()); |
307 | 307 |
Node::operator=(node); |
308 | 308 |
return *this; |
309 | 309 |
} |
310 | 310 |
Red(Invalid) : Node(INVALID) {} |
311 | 311 |
Red& operator=(Invalid) { |
312 | 312 |
Node::operator=(INVALID); |
313 | 313 |
return *this; |
314 | 314 |
} |
315 | 315 |
}; |
316 | 316 |
|
317 | 317 |
void first(Red& node) const { |
318 | 318 |
Parent::firstRed(static_cast<Node&>(node)); |
319 | 319 |
} |
320 | 320 |
void next(Red& node) const { |
321 | 321 |
Parent::nextRed(static_cast<Node&>(node)); |
322 | 322 |
} |
323 | 323 |
|
324 | 324 |
int id(const Red& node) const { |
325 | 325 |
return Parent::redId(node); |
326 | 326 |
} |
327 | 327 |
|
328 | 328 |
class Blue : public Node { |
329 | 329 |
friend class BidirBpGraphExtender; |
330 | 330 |
public: |
331 | 331 |
Blue() {} |
332 | 332 |
Blue(const Node& node) : Node(node) { |
333 | 333 |
LEMON_DEBUG(Parent::blue(node) || node == INVALID, |
334 | 334 |
typename Parent::NodeSetError()); |
335 | 335 |
} |
336 | 336 |
Blue& operator=(const Node& node) { |
337 | 337 |
LEMON_DEBUG(Parent::blue(node) || node == INVALID, |
338 | 338 |
typename Parent::NodeSetError()); |
339 | 339 |
Node::operator=(node); |
340 | 340 |
return *this; |
341 | 341 |
} |
342 | 342 |
Blue(Invalid) : Node(INVALID) {} |
343 | 343 |
Blue& operator=(Invalid) { |
344 | 344 |
Node::operator=(INVALID); |
345 | 345 |
return *this; |
346 | 346 |
} |
347 | 347 |
}; |
348 | 348 |
|
349 | 349 |
void first(Blue& node) const { |
350 | 350 |
Parent::firstBlue(static_cast<Node&>(node)); |
351 | 351 |
} |
352 | 352 |
void next(Blue& node) const { |
353 | 353 |
Parent::nextBlue(static_cast<Node&>(node)); |
354 | 354 |
} |
355 | 355 |
|
356 | 356 |
int id(const Blue& node) const { |
357 | 357 |
return Parent::redId(node); |
358 | 358 |
} |
359 | 359 |
|
360 | 360 |
Node source(const Edge& arc) const { |
361 | 361 |
return red(arc); |
362 | 362 |
} |
363 | 363 |
Node target(const Edge& arc) const { |
364 | 364 |
return blue(arc); |
365 | 365 |
} |
366 | 366 |
|
367 | 367 |
void firstInc(Edge& arc, bool& dir, const Node& node) const { |
368 | 368 |
if (Parent::red(node)) { |
369 | 369 |
Parent::firstFromRed(arc, node); |
370 | 370 |
dir = true; |
371 | 371 |
} else { |
372 | 372 |
Parent::firstFromBlue(arc, node); |
373 | 373 |
dir = static_cast<Edge&>(arc) == INVALID; |
374 | 374 |
} |
375 | 375 |
} |
376 | 376 |
void nextInc(Edge& arc, bool& dir) const { |
377 | 377 |
if (dir) { |
378 | 378 |
Parent::nextFromRed(arc); |
379 | 379 |
} else { |
380 | 380 |
Parent::nextFromBlue(arc); |
381 | 381 |
if (arc == INVALID) dir = true; |
382 | 382 |
} |
383 | 383 |
} |
384 | 384 |
|
385 | 385 |
class Arc : public Edge { |
386 | 386 |
friend class BidirBpGraphExtender; |
387 | 387 |
protected: |
388 | 388 |
bool forward; |
389 | 389 |
|
390 | 390 |
Arc(const Edge& arc, bool _forward) |
391 | 391 |
: Edge(arc), forward(_forward) {} |
392 | 392 |
|
393 | 393 |
public: |
394 | 394 |
Arc() {} |
395 | 395 |
Arc (Invalid) : Edge(INVALID), forward(true) {} |
396 | 396 |
bool operator==(const Arc& i) const { |
397 | 397 |
return Edge::operator==(i) && forward == i.forward; |
398 | 398 |
} |
399 | 399 |
bool operator!=(const Arc& i) const { |
400 | 400 |
return Edge::operator!=(i) || forward != i.forward; |
401 | 401 |
} |
402 | 402 |
bool operator<(const Arc& i) const { |
403 | 403 |
return Edge::operator<(i) || |
404 | 404 |
(!(i.forward<forward) && Edge(*this)<Edge(i)); |
405 | 405 |
} |
406 | 406 |
}; |
407 | 407 |
|
408 | 408 |
void first(Arc& arc) const { |
409 | 409 |
Parent::first(static_cast<Edge&>(arc)); |
410 | 410 |
arc.forward = true; |
411 | 411 |
} |
412 | 412 |
|
413 | 413 |
void next(Arc& arc) const { |
414 | 414 |
if (!arc.forward) { |
415 | 415 |
Parent::next(static_cast<Edge&>(arc)); |
416 | 416 |
} |
417 | 417 |
arc.forward = !arc.forward; |
418 | 418 |
} |
419 | 419 |
|
420 | 420 |
void firstOut(Arc& arc, const Node& node) const { |
421 | 421 |
if (Parent::red(node)) { |
422 | 422 |
Parent::firstFromRed(arc, node); |
423 | 423 |
arc.forward = true; |
424 | 424 |
} else { |
425 | 425 |
Parent::firstFromBlue(arc, node); |
426 | 426 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
427 | 427 |
} |
428 | 428 |
} |
429 | 429 |
void nextOut(Arc& arc) const { |
430 | 430 |
if (arc.forward) { |
431 | 431 |
Parent::nextFromRed(arc); |
432 | 432 |
} else { |
433 | 433 |
Parent::nextFromBlue(arc); |
434 | 434 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
435 | 435 |
} |
436 | 436 |
} |
437 | 437 |
|
438 | 438 |
void firstIn(Arc& arc, const Node& node) const { |
439 | 439 |
if (Parent::blue(node)) { |
440 | 440 |
Parent::firstFromBlue(arc, node); |
441 | 441 |
arc.forward = true; |
442 | 442 |
} else { |
443 | 443 |
Parent::firstFromRed(arc, node); |
444 | 444 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
445 | 445 |
} |
446 | 446 |
} |
447 | 447 |
void nextIn(Arc& arc) const { |
448 | 448 |
if (arc.forward) { |
449 | 449 |
Parent::nextFromBlue(arc); |
450 | 450 |
} else { |
451 | 451 |
Parent::nextFromRed(arc); |
452 | 452 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
453 | 453 |
} |
454 | 454 |
} |
455 | 455 |
|
456 | 456 |
Node source(const Arc& arc) const { |
457 | 457 |
return arc.forward ? Parent::red(arc) : Parent::blue(arc); |
458 | 458 |
} |
459 | 459 |
Node target(const Arc& arc) const { |
460 | 460 |
return arc.forward ? Parent::blue(arc) : Parent::red(arc); |
461 | 461 |
} |
462 | 462 |
|
463 | 463 |
int id(const Arc& arc) const { |
464 | 464 |
return (Parent::id(static_cast<const Edge&>(arc)) << 1) + |
465 | 465 |
(arc.forward ? 0 : 1); |
466 | 466 |
} |
467 | 467 |
Arc arcFromId(int ix) const { |
468 | 468 |
return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0); |
469 | 469 |
} |
470 | 470 |
int maxArcId() const { |
471 | 471 |
return (Parent::maxEdgeId() << 1) + 1; |
472 | 472 |
} |
473 | 473 |
|
474 | 474 |
bool direction(const Arc& arc) const { |
475 | 475 |
return arc.forward; |
476 | 476 |
} |
477 | 477 |
|
478 | 478 |
Arc direct(const Edge& arc, bool dir) const { |
479 | 479 |
return Arc(arc, dir); |
480 | 480 |
} |
481 | 481 |
|
482 | 482 |
int arcNum() const { |
483 | 483 |
return 2 * Parent::edgeNum(); |
484 | 484 |
} |
485 | 485 |
|
486 | 486 |
int edgeNum() const { |
487 | 487 |
return Parent::edgeNum(); |
488 | 488 |
} |
489 | 489 |
|
490 | 490 |
|
491 | 491 |
}; |
492 | 492 |
} |
493 | 493 |
|
494 | 494 |
#endif |
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; |
123 | 123 |
} |
124 | 124 |
Bezier3 before(double t) const |
125 | 125 |
{ |
126 | 126 |
Point p(conv(p1,p2,t)); |
127 | 127 |
Point q(conv(p2,p3,t)); |
128 | 128 |
Point r(conv(p3,p4,t)); |
129 | 129 |
Point a(conv(p,q,t)); |
130 | 130 |
Point b(conv(q,r,t)); |
131 | 131 |
Point c(conv(a,b,t)); |
132 | 132 |
return Bezier3(p1,p,a,c); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
Bezier3 after(double t) const |
136 | 136 |
{ |
137 | 137 |
Point p(conv(p1,p2,t)); |
138 | 138 |
Point q(conv(p2,p3,t)); |
139 | 139 |
Point r(conv(p3,p4,t)); |
140 | 140 |
Point a(conv(p,q,t)); |
141 | 141 |
Point b(conv(q,r,t)); |
142 | 142 |
Point c(conv(a,b,t)); |
143 | 143 |
return Bezier3(c,b,r,p4); |
144 | 144 |
} |
145 | 145 |
Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);} |
146 | 146 |
Bezier3 operator()(double a,double b) const { return before(b).after(a/b); } |
147 | 147 |
Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); } |
148 | 148 |
Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1), |
149 | 149 |
3.0*rot90(p3-p2), |
150 | 150 |
3.0*rot90(p4-p3)); } |
151 | 151 |
Point grad(double t) const { return grad()(t); } |
152 | 152 |
Point norm(double t) const { return rot90(grad(t)); } |
153 | 153 |
|
154 | 154 |
template<class R,class F,class S,class D> |
155 | 155 |
R recSplit(F &_f,const S &_s,D _d) const |
156 | 156 |
{ |
157 | 157 |
const Point a=(p1+p2)/2; |
158 | 158 |
const Point b=(p2+p3)/2; |
159 | 159 |
const Point c=(p3+p4)/2; |
160 | 160 |
const Point d=(a+b)/2; |
161 | 161 |
const Point e=(b+c)/2; |
162 | 162 |
const Point f=(d+e)/2; |
163 | 163 |
R f1=_f(Bezier3(p1,a,d,e),_d); |
164 | 164 |
R f2=_f(Bezier3(e,d,c,p4),_d); |
165 | 165 |
return _s(f1,f2); |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
}; |
169 | 169 |
|
170 | 170 |
|
171 | 171 |
} //END OF NAMESPACE dim2 |
172 | 172 |
} //END OF NAMESPACE lemon |
173 | 173 |
|
174 | 174 |
#endif // LEMON_BEZIER_H |
1 | 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)); |
427 | 427 |
} |
428 | 428 |
|
429 | 429 |
NodeIt(const Graph& graph, const Node& node) |
430 | 430 |
: Node(node), _graph(&graph) {} |
431 | 431 |
|
432 | 432 |
NodeIt& operator++() { |
433 | 433 |
_graph->next(*this); |
434 | 434 |
return *this; |
435 | 435 |
} |
436 | 436 |
|
437 | 437 |
}; |
438 | 438 |
|
439 | 439 |
|
440 | 440 |
class ArcIt : public Arc { |
441 | 441 |
const Graph* _graph; |
442 | 442 |
public: |
443 | 443 |
|
444 | 444 |
ArcIt() { } |
445 | 445 |
|
446 | 446 |
ArcIt(Invalid i) : Arc(i) { } |
447 | 447 |
|
448 | 448 |
explicit ArcIt(const Graph& graph) : _graph(&graph) { |
449 | 449 |
_graph->first(static_cast<Arc&>(*this)); |
450 | 450 |
} |
451 | 451 |
|
452 | 452 |
ArcIt(const Graph& graph, const Arc& arc) : |
453 | 453 |
Arc(arc), _graph(&graph) { } |
454 | 454 |
|
455 | 455 |
ArcIt& operator++() { |
456 | 456 |
_graph->next(*this); |
457 | 457 |
return *this; |
458 | 458 |
} |
459 | 459 |
|
460 | 460 |
}; |
461 | 461 |
|
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)); |
691 | 691 |
notifier(Arc()).add(ev); |
692 | 692 |
return edge; |
693 | 693 |
} |
694 | 694 |
|
695 | 695 |
void clear() { |
696 | 696 |
notifier(Arc()).clear(); |
697 | 697 |
notifier(Edge()).clear(); |
698 | 698 |
notifier(Node()).clear(); |
699 | 699 |
Parent::clear(); |
700 | 700 |
} |
701 | 701 |
|
702 | 702 |
template <typename Graph, typename NodeRefMap, typename EdgeRefMap> |
703 | 703 |
void build(const Graph& graph, NodeRefMap& nodeRef, |
704 | 704 |
EdgeRefMap& edgeRef) { |
705 | 705 |
Parent::build(graph, nodeRef, edgeRef); |
706 | 706 |
notifier(Node()).build(); |
707 | 707 |
notifier(Edge()).build(); |
708 | 708 |
notifier(Arc()).build(); |
709 | 709 |
} |
710 | 710 |
|
711 | 711 |
void erase(const Node& node) { |
712 | 712 |
Arc arc; |
713 | 713 |
Parent::firstOut(arc, node); |
714 | 714 |
while (arc != INVALID ) { |
715 | 715 |
erase(arc); |
716 | 716 |
Parent::firstOut(arc, node); |
717 | 717 |
} |
718 | 718 |
|
719 | 719 |
Parent::firstIn(arc, node); |
720 | 720 |
while (arc != INVALID ) { |
721 | 721 |
erase(arc); |
722 | 722 |
Parent::firstIn(arc, node); |
723 | 723 |
} |
724 | 724 |
|
725 | 725 |
notifier(Node()).erase(node); |
726 | 726 |
Parent::erase(node); |
727 | 727 |
} |
728 | 728 |
|
729 | 729 |
void erase(const Edge& edge) { |
730 | 730 |
std::vector<Arc> av; |
731 | 731 |
av.push_back(Parent::direct(edge, true)); |
732 | 732 |
av.push_back(Parent::direct(edge, false)); |
733 | 733 |
notifier(Arc()).erase(av); |
734 | 734 |
notifier(Edge()).erase(edge); |
735 | 735 |
Parent::erase(edge); |
736 | 736 |
} |
737 | 737 |
|
738 | 738 |
GraphExtender() { |
739 | 739 |
node_notifier.setContainer(*this); |
740 | 740 |
arc_notifier.setContainer(*this); |
741 | 741 |
edge_notifier.setContainer(*this); |
742 | 742 |
} |
743 | 743 |
|
744 | 744 |
~GraphExtender() { |
745 | 745 |
edge_notifier.clear(); |
746 | 746 |
arc_notifier.clear(); |
747 | 747 |
node_notifier.clear(); |
748 | 748 |
} |
749 | 749 |
|
750 | 750 |
}; |
751 | 751 |
|
752 | 752 |
} |
753 | 753 |
|
754 | 754 |
#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_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); |
273 | 273 |
} |
274 | 274 |
|
275 | 275 |
ConstMapIt(const Map& _map, const Item& item) |
276 | 276 |
: Parent(item), map(_map) {} |
277 | 277 |
|
278 | 278 |
ConstMapIt& operator++() { |
279 | 279 |
map.graph.next(*this); |
280 | 280 |
return *this; |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
284 | 284 |
return map[*this]; |
285 | 285 |
} |
286 | 286 |
|
287 | 287 |
protected: |
288 | 288 |
const Map& map; |
289 | 289 |
}; |
290 | 290 |
|
291 | 291 |
class ItemIt : public Item { |
292 | 292 |
public: |
293 | 293 |
|
294 | 294 |
typedef Item Parent; |
295 | 295 |
|
296 | 296 |
ItemIt() {} |
297 | 297 |
|
298 | 298 |
ItemIt(Invalid i) : Parent(i) { } |
299 | 299 |
|
300 | 300 |
explicit ItemIt(Map& _map) : map(_map) { |
301 | 301 |
map.graph.first(*this); |
302 | 302 |
} |
303 | 303 |
|
304 | 304 |
ItemIt(const Map& _map, const Item& item) |
305 | 305 |
: Parent(item), map(_map) {} |
306 | 306 |
|
307 | 307 |
ItemIt& operator++() { |
308 | 308 |
map.graph.next(*this); |
309 | 309 |
return *this; |
310 | 310 |
} |
311 | 311 |
|
312 | 312 |
protected: |
313 | 313 |
const Map& map; |
314 | 314 |
|
315 | 315 |
}; |
316 | 316 |
|
317 | 317 |
private: |
318 | 318 |
|
319 | 319 |
const Graph& graph; |
320 | 320 |
|
321 | 321 |
}; |
322 | 322 |
|
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_TRAITS_H |
20 | 20 |
#define LEMON_BITS_TRAITS_H |
21 | 21 |
|
22 |
///\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; |
121 | 121 |
}; |
122 | 122 |
|
123 | 123 |
template <typename _Graph> |
124 | 124 |
class ItemSetTraits<_Graph, typename _Graph::Edge> { |
125 | 125 |
public: |
126 | 126 |
|
127 | 127 |
typedef _Graph Graph; |
128 | 128 |
|
129 | 129 |
typedef typename Graph::Edge Item; |
130 | 130 |
typedef typename Graph::EdgeIt ItemIt; |
131 | 131 |
|
132 | 132 |
typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier; |
133 | 133 |
|
134 | 134 |
template <typename _Value> |
135 | 135 |
class Map : public Graph::template EdgeMap<_Value> { |
136 | 136 |
public: |
137 | 137 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
138 | 138 |
typedef typename Graph::template EdgeMap<_Value> Type; |
139 | 139 |
typedef typename Parent::Value Value; |
140 | 140 |
|
141 | 141 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
142 | 142 |
Map(const Graph& _digraph, const Value& _value) |
143 | 143 |
: Parent(_digraph, _value) {} |
144 | 144 |
}; |
145 | 145 |
|
146 | 146 |
}; |
147 | 147 |
|
148 | 148 |
template <typename Map, typename Enable = void> |
149 | 149 |
struct MapTraits { |
150 | 150 |
typedef False ReferenceMapTag; |
151 | 151 |
|
152 | 152 |
typedef typename Map::Key Key; |
153 | 153 |
typedef typename Map::Value Value; |
154 | 154 |
|
155 | 155 |
typedef Value ConstReturnValue; |
156 | 156 |
typedef Value ReturnValue; |
157 | 157 |
}; |
158 | 158 |
|
159 | 159 |
template <typename Map> |
160 | 160 |
struct MapTraits< |
161 | 161 |
Map, typename enable_if<typename Map::ReferenceMapTag, void>::type > |
162 | 162 |
{ |
163 | 163 |
typedef True ReferenceMapTag; |
164 | 164 |
|
165 | 165 |
typedef typename Map::Key Key; |
166 | 166 |
typedef typename Map::Value Value; |
167 | 167 |
|
168 | 168 |
typedef typename Map::ConstReference ConstReturnValue; |
169 | 169 |
typedef typename Map::Reference ReturnValue; |
170 | 170 |
|
171 | 171 |
typedef typename Map::ConstReference ConstReference; |
172 | 172 |
typedef typename Map::Reference Reference; |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
template <typename MatrixMap, typename Enable = void> |
176 | 176 |
struct MatrixMapTraits { |
177 | 177 |
typedef False ReferenceMapTag; |
178 | 178 |
|
179 | 179 |
typedef typename MatrixMap::FirstKey FirstKey; |
180 | 180 |
typedef typename MatrixMap::SecondKey SecondKey; |
181 | 181 |
typedef typename MatrixMap::Value Value; |
182 | 182 |
|
183 | 183 |
typedef Value ConstReturnValue; |
184 | 184 |
typedef Value ReturnValue; |
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
template <typename MatrixMap> |
188 | 188 |
struct MatrixMapTraits< |
189 | 189 |
MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag, |
190 | 190 |
void>::type > |
191 | 191 |
{ |
192 | 192 |
typedef True ReferenceMapTag; |
193 | 193 |
|
194 | 194 |
typedef typename MatrixMap::FirstKey FirstKey; |
195 | 195 |
typedef typename MatrixMap::SecondKey SecondKey; |
196 | 196 |
typedef typename MatrixMap::Value Value; |
197 | 197 |
|
198 | 198 |
typedef typename MatrixMap::ConstReference ConstReturnValue; |
199 | 199 |
typedef typename MatrixMap::Reference ReturnValue; |
200 | 200 |
|
201 | 201 |
typedef typename MatrixMap::ConstReference ConstReference; |
202 | 202 |
typedef typename MatrixMap::Reference Reference; |
203 | 203 |
}; |
204 | 204 |
|
205 | 205 |
// Indicators for the tags |
206 | 206 |
|
207 | 207 |
template <typename Graph, typename Enable = void> |
208 | 208 |
struct NodeNumTagIndicator { |
209 | 209 |
static const bool value = false; |
210 | 210 |
}; |
211 | 211 |
|
212 | 212 |
template <typename Graph> |
213 | 213 |
struct NodeNumTagIndicator< |
214 | 214 |
Graph, |
215 | 215 |
typename enable_if<typename Graph::NodeNumTag, void>::type |
216 | 216 |
> { |
217 | 217 |
static const bool value = true; |
218 | 218 |
}; |
219 | 219 |
|
220 | 220 |
template <typename Graph, typename Enable = void> |
221 | 221 |
struct EdgeNumTagIndicator { |
222 | 222 |
static const bool value = false; |
223 | 223 |
}; |
224 | 224 |
|
225 | 225 |
template <typename Graph> |
226 | 226 |
struct EdgeNumTagIndicator< |
227 | 227 |
Graph, |
228 | 228 |
typename enable_if<typename Graph::EdgeNumTag, void>::type |
229 | 229 |
> { |
230 | 230 |
static const bool value = true; |
231 | 231 |
}; |
232 | 232 |
|
233 | 233 |
template <typename Graph, typename Enable = void> |
234 | 234 |
struct FindEdgeTagIndicator { |
235 | 235 |
static const bool value = false; |
236 | 236 |
}; |
237 | 237 |
|
238 | 238 |
template <typename Graph> |
239 | 239 |
struct FindEdgeTagIndicator< |
240 | 240 |
Graph, |
241 | 241 |
typename enable_if<typename Graph::FindEdgeTag, void>::type |
242 | 242 |
> { |
243 | 243 |
static const bool value = true; |
244 | 244 |
}; |
245 | 245 |
|
246 | 246 |
template <typename Graph, typename Enable = void> |
247 | 247 |
struct UndirectedTagIndicator { |
248 | 248 |
static const bool value = false; |
249 | 249 |
}; |
250 | 250 |
|
251 | 251 |
template <typename Graph> |
252 | 252 |
struct UndirectedTagIndicator< |
253 | 253 |
Graph, |
254 | 254 |
typename enable_if<typename Graph::UndirectedTag, void>::type |
255 | 255 |
> { |
256 | 256 |
static const bool value = true; |
257 | 257 |
}; |
258 | 258 |
|
259 | 259 |
template <typename Graph, typename Enable = void> |
260 | 260 |
struct BuildTagIndicator { |
261 | 261 |
static const bool value = false; |
262 | 262 |
}; |
263 | 263 |
|
264 | 264 |
template <typename Graph> |
265 | 265 |
struct BuildTagIndicator< |
266 | 266 |
Graph, |
267 | 267 |
typename enable_if<typename Graph::BuildTag, void>::type |
268 | 268 |
> { |
269 | 269 |
static const bool value = true; |
270 | 270 |
}; |
271 | 271 |
|
272 | 272 |
} |
273 | 273 |
|
274 | 274 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_VECTOR_MAP_H |
20 | 20 |
#define LEMON_BITS_VECTOR_MAP_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 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); |
192 | 192 |
} |
193 | 193 |
///Returns black for light colors and white for the dark ones. |
194 | 194 |
|
195 | 195 |
///Returns black for light colors and white for the dark ones. |
196 | 196 |
inline Color distantBW(const Color &c){ |
197 | 197 |
return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK; |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
/// @} |
201 | 201 |
|
202 | 202 |
} //END OF NAMESPACE LEMON |
203 | 203 |
|
204 | 204 |
#endif // LEMON_COLOR_H |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)