0
11
0
5
5
26
36
36
36
22
22
... | ... |
@@ -9,193 +9,193 @@ |
9 | 9 |
OUTPUT_DIRECTORY = |
10 | 10 |
CREATE_SUBDIRS = NO |
11 | 11 |
OUTPUT_LANGUAGE = English |
12 | 12 |
BRIEF_MEMBER_DESC = YES |
13 | 13 |
REPEAT_BRIEF = NO |
14 | 14 |
ABBREVIATE_BRIEF = |
15 | 15 |
ALWAYS_DETAILED_SEC = NO |
16 | 16 |
INLINE_INHERITED_MEMB = NO |
17 | 17 |
FULL_PATH_NAMES = YES |
18 | 18 |
STRIP_FROM_PATH = @abs_top_srcdir@ |
19 | 19 |
STRIP_FROM_INC_PATH = @abs_top_srcdir@ |
20 | 20 |
SHORT_NAMES = YES |
21 | 21 |
JAVADOC_AUTOBRIEF = NO |
22 | 22 |
QT_AUTOBRIEF = NO |
23 | 23 |
MULTILINE_CPP_IS_BRIEF = NO |
24 | 24 |
DETAILS_AT_TOP = YES |
25 | 25 |
INHERIT_DOCS = NO |
26 | 26 |
SEPARATE_MEMBER_PAGES = NO |
27 | 27 |
TAB_SIZE = 8 |
28 | 28 |
ALIASES = |
29 | 29 |
OPTIMIZE_OUTPUT_FOR_C = NO |
30 | 30 |
OPTIMIZE_OUTPUT_JAVA = NO |
31 | 31 |
OPTIMIZE_FOR_FORTRAN = NO |
32 | 32 |
OPTIMIZE_OUTPUT_VHDL = NO |
33 | 33 |
BUILTIN_STL_SUPPORT = YES |
34 | 34 |
CPP_CLI_SUPPORT = NO |
35 | 35 |
SIP_SUPPORT = NO |
36 | 36 |
DISTRIBUTE_GROUP_DOC = NO |
37 | 37 |
SUBGROUPING = YES |
38 | 38 |
TYPEDEF_HIDES_STRUCT = NO |
39 | 39 |
#--------------------------------------------------------------------------- |
40 | 40 |
# Build related configuration options |
41 | 41 |
#--------------------------------------------------------------------------- |
42 | 42 |
EXTRACT_ALL = NO |
43 | 43 |
EXTRACT_PRIVATE = YES |
44 | 44 |
EXTRACT_STATIC = YES |
45 | 45 |
EXTRACT_LOCAL_CLASSES = NO |
46 | 46 |
EXTRACT_LOCAL_METHODS = NO |
47 | 47 |
EXTRACT_ANON_NSPACES = NO |
48 | 48 |
HIDE_UNDOC_MEMBERS = YES |
49 | 49 |
HIDE_UNDOC_CLASSES = YES |
50 | 50 |
HIDE_FRIEND_COMPOUNDS = NO |
51 | 51 |
HIDE_IN_BODY_DOCS = NO |
52 | 52 |
INTERNAL_DOCS = NO |
53 | 53 |
CASE_SENSE_NAMES = YES |
54 | 54 |
HIDE_SCOPE_NAMES = YES |
55 | 55 |
SHOW_INCLUDE_FILES = YES |
56 | 56 |
INLINE_INFO = YES |
57 | 57 |
SORT_MEMBER_DOCS = NO |
58 | 58 |
SORT_BRIEF_DOCS = NO |
59 | 59 |
SORT_GROUP_NAMES = NO |
60 | 60 |
SORT_BY_SCOPE_NAME = NO |
61 | 61 |
GENERATE_TODOLIST = YES |
62 | 62 |
GENERATE_TESTLIST = YES |
63 | 63 |
GENERATE_BUGLIST = YES |
64 | 64 |
GENERATE_DEPRECATEDLIST= YES |
65 | 65 |
ENABLED_SECTIONS = |
66 | 66 |
MAX_INITIALIZER_LINES = 5 |
67 | 67 |
SHOW_USED_FILES = YES |
68 | 68 |
SHOW_DIRECTORIES = YES |
69 | 69 |
FILE_VERSION_FILTER = |
70 | 70 |
#--------------------------------------------------------------------------- |
71 | 71 |
# configuration options related to warning and progress messages |
72 | 72 |
#--------------------------------------------------------------------------- |
73 | 73 |
QUIET = NO |
74 | 74 |
WARNINGS = YES |
75 | 75 |
WARN_IF_UNDOCUMENTED = YES |
76 | 76 |
WARN_IF_DOC_ERROR = YES |
77 | 77 |
WARN_NO_PARAMDOC = NO |
78 | 78 |
WARN_FORMAT = "$file:$line: $text " |
79 | 79 |
WARN_LOGFILE = doxygen.log |
80 | 80 |
#--------------------------------------------------------------------------- |
81 | 81 |
# configuration options related to the input files |
82 | 82 |
#--------------------------------------------------------------------------- |
83 | 83 |
INPUT = @abs_top_srcdir@/doc \ |
84 | 84 |
@abs_top_srcdir@/lemon \ |
85 | 85 |
@abs_top_srcdir@/lemon/bits \ |
86 | 86 |
@abs_top_srcdir@/lemon/concepts \ |
87 | 87 |
@abs_top_srcdir@/demo \ |
88 | 88 |
@abs_top_srcdir@/tools \ |
89 | 89 |
@abs_top_srcdir@/test/test_tools.h |
90 | 90 |
INPUT_ENCODING = UTF-8 |
91 | 91 |
FILE_PATTERNS = *.h \ |
92 | 92 |
*.cc \ |
93 | 93 |
*.dox |
94 | 94 |
RECURSIVE = NO |
95 | 95 |
EXCLUDE = |
96 | 96 |
EXCLUDE_SYMLINKS = NO |
97 | 97 |
EXCLUDE_PATTERNS = |
98 | 98 |
EXCLUDE_SYMBOLS = |
99 | 99 |
EXAMPLE_PATH = @abs_top_srcdir@/demo \ |
100 | 100 |
@abs_top_srcdir@/LICENSE \ |
101 | 101 |
@abs_top_srcdir@/doc |
102 | 102 |
EXAMPLE_PATTERNS = |
103 | 103 |
EXAMPLE_RECURSIVE = NO |
104 | 104 |
IMAGE_PATH = @abs_top_srcdir@/doc/images \ |
105 |
|
|
105 |
@abs_top_builddir@/doc/gen-images |
|
106 | 106 |
INPUT_FILTER = |
107 | 107 |
FILTER_PATTERNS = |
108 | 108 |
FILTER_SOURCE_FILES = NO |
109 | 109 |
#--------------------------------------------------------------------------- |
110 | 110 |
# configuration options related to source browsing |
111 | 111 |
#--------------------------------------------------------------------------- |
112 | 112 |
SOURCE_BROWSER = NO |
113 | 113 |
INLINE_SOURCES = NO |
114 | 114 |
STRIP_CODE_COMMENTS = YES |
115 | 115 |
REFERENCED_BY_RELATION = NO |
116 | 116 |
REFERENCES_RELATION = NO |
117 | 117 |
REFERENCES_LINK_SOURCE = YES |
118 | 118 |
USE_HTAGS = NO |
119 | 119 |
VERBATIM_HEADERS = NO |
120 | 120 |
#--------------------------------------------------------------------------- |
121 | 121 |
# configuration options related to the alphabetical class index |
122 | 122 |
#--------------------------------------------------------------------------- |
123 | 123 |
ALPHABETICAL_INDEX = YES |
124 | 124 |
COLS_IN_ALPHA_INDEX = 2 |
125 | 125 |
IGNORE_PREFIX = |
126 | 126 |
#--------------------------------------------------------------------------- |
127 | 127 |
# configuration options related to the HTML output |
128 | 128 |
#--------------------------------------------------------------------------- |
129 | 129 |
GENERATE_HTML = YES |
130 | 130 |
HTML_OUTPUT = html |
131 | 131 |
HTML_FILE_EXTENSION = .html |
132 | 132 |
HTML_HEADER = |
133 | 133 |
HTML_FOOTER = |
134 | 134 |
HTML_STYLESHEET = |
135 | 135 |
HTML_ALIGN_MEMBERS = YES |
136 | 136 |
GENERATE_HTMLHELP = NO |
137 | 137 |
GENERATE_DOCSET = NO |
138 | 138 |
DOCSET_FEEDNAME = "Doxygen generated docs" |
139 | 139 |
DOCSET_BUNDLE_ID = org.doxygen.Project |
140 | 140 |
HTML_DYNAMIC_SECTIONS = NO |
141 | 141 |
CHM_FILE = |
142 | 142 |
HHC_LOCATION = |
143 | 143 |
GENERATE_CHI = NO |
144 | 144 |
BINARY_TOC = NO |
145 | 145 |
TOC_EXPAND = NO |
146 | 146 |
DISABLE_INDEX = NO |
147 | 147 |
ENUM_VALUES_PER_LINE = 4 |
148 | 148 |
GENERATE_TREEVIEW = YES |
149 | 149 |
TREEVIEW_WIDTH = 250 |
150 | 150 |
#--------------------------------------------------------------------------- |
151 | 151 |
# configuration options related to the LaTeX output |
152 | 152 |
#--------------------------------------------------------------------------- |
153 | 153 |
GENERATE_LATEX = NO |
154 | 154 |
LATEX_OUTPUT = latex |
155 | 155 |
LATEX_CMD_NAME = latex |
156 | 156 |
MAKEINDEX_CMD_NAME = makeindex |
157 | 157 |
COMPACT_LATEX = YES |
158 | 158 |
PAPER_TYPE = a4wide |
159 | 159 |
EXTRA_PACKAGES = amsmath \ |
160 | 160 |
amssymb |
161 | 161 |
LATEX_HEADER = |
162 | 162 |
PDF_HYPERLINKS = YES |
163 | 163 |
USE_PDFLATEX = YES |
164 | 164 |
LATEX_BATCHMODE = NO |
165 | 165 |
LATEX_HIDE_INDICES = NO |
166 | 166 |
#--------------------------------------------------------------------------- |
167 | 167 |
# configuration options related to the RTF output |
168 | 168 |
#--------------------------------------------------------------------------- |
169 | 169 |
GENERATE_RTF = NO |
170 | 170 |
RTF_OUTPUT = rtf |
171 | 171 |
COMPACT_RTF = NO |
172 | 172 |
RTF_HYPERLINKS = NO |
173 | 173 |
RTF_STYLESHEET_FILE = |
174 | 174 |
RTF_EXTENSIONS_FILE = |
175 | 175 |
#--------------------------------------------------------------------------- |
176 | 176 |
# configuration options related to the man page output |
177 | 177 |
#--------------------------------------------------------------------------- |
178 | 178 |
GENERATE_MAN = NO |
179 | 179 |
MAN_OUTPUT = man |
180 | 180 |
MAN_EXTENSION = .3 |
181 | 181 |
MAN_LINKS = NO |
182 | 182 |
#--------------------------------------------------------------------------- |
183 | 183 |
# configuration options related to the XML output |
184 | 184 |
#--------------------------------------------------------------------------- |
185 | 185 |
GENERATE_XML = NO |
186 | 186 |
XML_OUTPUT = xml |
187 | 187 |
XML_SCHEMA = |
188 | 188 |
XML_DTD = |
189 | 189 |
XML_PROGRAMLISTING = YES |
190 | 190 |
#--------------------------------------------------------------------------- |
191 | 191 |
# configuration options for the AutoGen Definitions output |
192 | 192 |
#--------------------------------------------------------------------------- |
193 | 193 |
GENERATE_AUTOGEN_DEF = NO |
194 | 194 |
#--------------------------------------------------------------------------- |
195 | 195 |
# configuration options related to the Perl module output |
196 | 196 |
#--------------------------------------------------------------------------- |
197 | 197 |
GENERATE_PERLMOD = NO |
198 | 198 |
PERLMOD_LATEX = NO |
199 | 199 |
PERLMOD_PRETTY = YES |
200 | 200 |
PERLMOD_MAKEVAR_PREFIX = |
201 | 201 |
#--------------------------------------------------------------------------- |
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 |
label coordinates size title |
|
61 |
1 (10,20) 10 "First node" |
|
62 |
2 (80,80) 8 "Second node" |
|
63 |
3 (40,10) 10 "Third node" |
|
60 |
label coordinates size title |
|
61 |
1 (10,20) 10 "First node" |
|
62 |
2 (80,80) 8 "Second node" |
|
63 |
3 (40,10) 10 "Third node" |
|
64 | 64 |
\endcode |
65 | 65 |
|
66 | 66 |
The \c \@arcs section is very similar to the \c \@nodes section, |
67 | 67 |
it again starts with a header line describing the names of the maps, |
68 | 68 |
but the \c "label" map is not obligatory here. The following lines |
69 | 69 |
describe the arcs. The first two tokens of each line are |
70 | 70 |
the source and the target node of the arc, respectively, then come the map |
71 | 71 |
values. The source and target tokens must be node labels. |
72 | 72 |
|
73 | 73 |
\code |
74 | 74 |
@arcs |
75 |
|
|
75 |
capacity |
|
76 | 76 |
1 2 16 |
77 | 77 |
1 3 12 |
78 | 78 |
2 3 18 |
79 | 79 |
\endcode |
80 | 80 |
|
81 | 81 |
The \c \@edges is just a synonym of \c \@arcs. The @arcs section can |
82 | 82 |
also store the edge set of an undirected graph. In such case there is |
83 | 83 |
a conventional method for store arc maps in the file, if two columns |
84 | 84 |
has the same caption with \c '+' and \c '-' prefix, then these columns |
85 | 85 |
can be regarded as the values of an arc map. |
86 | 86 |
|
87 | 87 |
The \c \@attributes section contains key-value pairs, each line |
88 | 88 |
consists of two tokens, an attribute name, and then an attribute |
89 | 89 |
value. The value of the attribute could be also a label value of a |
90 | 90 |
node or an edge, or even an edge label prefixed with \c '+' or \c '-', |
91 | 91 |
which regards to the forward or backward directed arc of the |
92 | 92 |
corresponding edge. |
93 | 93 |
|
94 | 94 |
\code |
95 | 95 |
@attributes |
96 | 96 |
source 1 |
97 | 97 |
target 3 |
98 | 98 |
caption "LEMON test digraph" |
99 | 99 |
\endcode |
100 | 100 |
|
101 | 101 |
The \e LGF can contain extra sections, but there is no restriction on |
102 | 102 |
the format of such sections. |
103 | 103 |
|
104 | 104 |
*/ |
105 | 105 |
} |
106 | 106 |
|
107 | 107 |
// LocalWords: whitespace whitespaces |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 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_ASSERT_H |
20 | 20 |
#define LEMON_ASSERT_H |
21 | 21 |
|
22 | 22 |
/// \ingroup exceptions |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Extended assertion handling |
25 | 25 |
|
26 | 26 |
#include <lemon/error.h> |
27 | 27 |
|
28 | 28 |
namespace lemon { |
29 | 29 |
|
30 | 30 |
inline void assert_fail_log(const char *file, int line, const char *function, |
31 | 31 |
const char *message, const char *assertion) |
32 | 32 |
{ |
33 | 33 |
std::cerr << file << ":" << line << ": "; |
34 | 34 |
if (function) |
35 | 35 |
std::cerr << function << ": "; |
36 | 36 |
std::cerr << message; |
37 | 37 |
if (assertion) |
38 | 38 |
std::cerr << " (assertion '" << assertion << "' failed)"; |
39 | 39 |
std::cerr << std::endl; |
40 | 40 |
} |
41 | 41 |
|
42 | 42 |
inline void assert_fail_abort(const char *file, int line, |
43 | 43 |
const char *function, const char* message, |
44 | 44 |
const char *assertion) |
45 | 45 |
{ |
46 | 46 |
assert_fail_log(file, line, function, message, assertion); |
47 | 47 |
std::abort(); |
48 | 48 |
} |
49 | 49 |
|
50 | 50 |
namespace _assert_bits { |
51 | 51 |
|
52 | 52 |
|
53 | 53 |
inline const char* cstringify(const std::string& str) { |
54 | 54 |
return str.c_str(); |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
inline const char* cstringify(const char* str) { |
58 | 58 |
return str; |
59 | 59 |
} |
60 | 60 |
} |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
#endif // LEMON_ASSERT_H |
64 | 64 |
|
65 | 65 |
#undef LEMON_ASSERT |
66 | 66 |
#undef LEMON_FIXME |
67 | 67 |
#undef LEMON_DEBUG |
68 | 68 |
|
69 |
#if (defined(LEMON_ASSERT_LOG) ? 1 : 0) + \ |
|
70 |
(defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
|
69 |
#if (defined(LEMON_ASSERT_LOG) ? 1 : 0) + \ |
|
70 |
(defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
|
71 | 71 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) > 1 |
72 | 72 |
#error "LEMON assertion system is not set properly" |
73 | 73 |
#endif |
74 | 74 |
|
75 |
#if ((defined(LEMON_ASSERT_LOG) ? 1 : 0) + \ |
|
76 |
(defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
|
77 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) == 1 || \ |
|
78 |
defined(LEMON_ENABLE_ASSERTS)) && \ |
|
79 |
|
|
75 |
#if ((defined(LEMON_ASSERT_LOG) ? 1 : 0) + \ |
|
76 |
(defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
|
77 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) == 1 || \ |
|
78 |
defined(LEMON_ENABLE_ASSERTS)) && \ |
|
79 |
(defined(LEMON_DISABLE_ASSERTS) || \ |
|
80 | 80 |
defined(NDEBUG)) |
81 | 81 |
#error "LEMON assertion system is not set properly" |
82 | 82 |
#endif |
83 | 83 |
|
84 | 84 |
|
85 | 85 |
#if defined LEMON_ASSERT_LOG |
86 | 86 |
# undef LEMON_ASSERT_HANDLER |
87 | 87 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_log |
88 | 88 |
#elif defined LEMON_ASSERT_ABORT |
89 | 89 |
# undef LEMON_ASSERT_HANDLER |
90 | 90 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
91 | 91 |
#elif defined LEMON_ASSERT_CUSTOM |
92 | 92 |
# undef LEMON_ASSERT_HANDLER |
93 | 93 |
# ifndef LEMON_CUSTOM_ASSERT_HANDLER |
94 | 94 |
# error "LEMON_CUSTOM_ASSERT_HANDLER is not set" |
95 | 95 |
# endif |
96 | 96 |
# define LEMON_ASSERT_HANDLER LEMON_CUSTOM_ASSERT_HANDLER |
97 | 97 |
#elif defined LEMON_DISABLE_ASSERTS |
98 | 98 |
# undef LEMON_ASSERT_HANDLER |
99 | 99 |
#elif defined NDEBUG |
100 | 100 |
# undef LEMON_ASSERT_HANDLER |
101 | 101 |
#else |
102 | 102 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
103 | 103 |
#endif |
104 | 104 |
|
105 | 105 |
#ifndef LEMON_FUNCTION_NAME |
106 | 106 |
# if defined __GNUC__ |
107 | 107 |
# define LEMON_FUNCTION_NAME (__PRETTY_FUNCTION__) |
108 | 108 |
# elif defined _MSC_VER |
109 | 109 |
# define LEMON_FUNCTION_NAME (__FUNCSIG__) |
110 | 110 |
# else |
111 | 111 |
# define LEMON_FUNCTION_NAME (__func__) |
112 | 112 |
# endif |
113 | 113 |
#endif |
114 | 114 |
|
115 | 115 |
#ifdef DOXYGEN |
116 | 116 |
|
117 | 117 |
/// \ingroup exceptions |
118 | 118 |
/// |
119 | 119 |
/// \brief Macro for assertion with customizable message |
120 | 120 |
/// |
121 | 121 |
/// Macro for assertion with customizable message. \param exp An |
122 | 122 |
/// expression that must be convertible to \c bool. If it is \c |
123 | 123 |
/// false, then an assertion is raised. The concrete behaviour depends |
124 | 124 |
/// on the settings of the assertion system. \param msg A <tt>const |
125 | 125 |
/// char*</tt> parameter, which can be used to provide information |
126 | 126 |
/// about the circumstances of the failed assertion. |
127 | 127 |
/// |
128 | 128 |
/// The assertions are enabled in the default behaviour. |
129 | 129 |
/// You can disable them with the following code: |
130 | 130 |
/// \code |
131 | 131 |
/// #define LEMON_DISABLE_ASSERTS |
132 | 132 |
/// \endcode |
133 | 133 |
/// or with compilation parameters: |
134 | 134 |
/// \code |
135 | 135 |
/// g++ -DLEMON_DISABLE_ASSERTS |
136 | 136 |
/// make CXXFLAGS='-DLEMON_DISABLE_ASSERTS' |
137 | 137 |
/// \endcode |
138 | 138 |
/// The checking is also disabled when the standard macro \c NDEBUG is defined. |
139 | 139 |
/// |
140 | 140 |
/// The LEMON assertion system has a wide range of customization |
141 | 141 |
/// properties. As a default behaviour the failed assertion prints a |
142 | 142 |
/// short log message to the standard error and aborts the execution. |
143 | 143 |
/// |
144 | 144 |
/// The following modes can be used in the assertion system: |
145 | 145 |
/// |
146 | 146 |
/// - \c LEMON_ASSERT_LOG The failed assertion prints a short log |
147 | 147 |
/// message to the standard error and continues the execution. |
148 | 148 |
/// - \c LEMON_ASSERT_ABORT This mode is similar to the \c |
149 | 149 |
/// LEMON_ASSERT_LOG, but it aborts the program. It is the default |
150 | 150 |
/// behaviour. |
151 | 151 |
/// - \c LEMON_ASSERT_CUSTOM The user can define own assertion handler |
152 | 152 |
/// function. |
153 | 153 |
/// \code |
154 | 154 |
/// void custom_assert_handler(const char* file, int line, |
155 | 155 |
/// const char* function, const char* message, |
156 | 156 |
/// const char* assertion); |
157 | 157 |
/// \endcode |
158 | 158 |
/// The name of the function should be defined as the \c |
159 | 159 |
/// LEMON_CUSTOM_ASSERT_HANDLER macro name. |
160 | 160 |
/// \code |
161 | 161 |
/// #define LEMON_CUSTOM_ASSERT_HANDLER custom_assert_handler |
162 | 162 |
/// \endcode |
163 | 163 |
/// Whenever an assertion is occured, the custom assertion |
164 | 164 |
/// handler is called with appropiate parameters. |
165 | 165 |
/// |
166 | 166 |
/// The assertion mode can also be changed within one compilation unit. |
167 | 167 |
/// If the macros are redefined with other settings and the |
168 | 168 |
/// \ref lemon/assert.h "assert.h" file is reincluded, then the |
169 | 169 |
/// behaviour is changed appropiately to the new settings. |
170 | 170 |
# define LEMON_ASSERT(exp, msg) \ |
171 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
|
172 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
|
173 |
|
|
171 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
|
172 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
|
173 |
LEMON_FUNCTION_NAME, \ |
|
174 | 174 |
::lemon::_assert_bits::cstringify(msg), #exp), 0))) |
175 | 175 |
|
176 | 176 |
/// \ingroup exceptions |
177 | 177 |
/// |
178 | 178 |
/// \brief Macro for mark not yet implemented features. |
179 | 179 |
/// |
180 | 180 |
/// Macro for mark not yet implemented features and outstanding bugs. |
181 | 181 |
/// It is close to be the shortcut of the following code: |
182 | 182 |
/// \code |
183 | 183 |
/// LEMON_ASSERT(false, msg); |
184 | 184 |
/// \endcode |
185 | 185 |
/// |
186 | 186 |
/// \see LEMON_ASSERT |
187 |
# define LEMON_FIXME(msg) |
|
187 |
# define LEMON_FIXME(msg) \ |
|
188 | 188 |
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME, \ |
189 |
::lemon::_assert_bits::cstringify(msg), |
|
189 |
::lemon::_assert_bits::cstringify(msg), \ |
|
190 | 190 |
static_cast<const char*>(0))) |
191 | 191 |
|
192 | 192 |
/// \ingroup exceptions |
193 | 193 |
/// |
194 | 194 |
/// \brief Macro for internal assertions |
195 | 195 |
/// |
196 | 196 |
/// Macro for internal assertions, it is used in the library to check |
197 | 197 |
/// the consistency of results of algorithms, several pre- and |
198 | 198 |
/// postconditions and invariants. The checking is disabled by |
199 | 199 |
/// default, but it can be turned on with the macro \c |
200 | 200 |
/// LEMON_ENABLE_DEBUG. |
201 | 201 |
/// \code |
202 | 202 |
/// #define LEMON_ENABLE_DEBUG |
203 | 203 |
/// \endcode |
204 | 204 |
/// or with compilation parameters: |
205 | 205 |
/// \code |
206 | 206 |
/// g++ -DLEMON_ENABLE_DEBUG |
207 | 207 |
/// make CXXFLAGS='-DLEMON_ENABLE_DEBUG' |
208 | 208 |
/// \endcode |
209 | 209 |
/// |
210 | 210 |
/// This macro works like the \c LEMON_ASSERT macro, therefore the |
211 | 211 |
/// current behaviour depends on the settings of \c LEMON_ASSERT |
212 | 212 |
/// macro. |
213 | 213 |
/// |
214 | 214 |
/// \see LEMON_ASSERT |
215 |
# define LEMON_DEBUG(exp, msg) \ |
|
216 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
|
215 |
# define LEMON_DEBUG(exp, msg) \ |
|
216 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
|
217 | 217 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
218 |
LEMON_FUNCTION_NAME, |
|
218 |
LEMON_FUNCTION_NAME, \ |
|
219 | 219 |
::lemon::_assert_bits::cstringify(msg), #exp), 0))) |
220 | 220 |
|
221 | 221 |
#else |
222 | 222 |
|
223 | 223 |
# ifndef LEMON_ASSERT_HANDLER |
224 | 224 |
# define LEMON_ASSERT(exp, msg) (static_cast<void>(0)) |
225 | 225 |
# define LEMON_FIXME(msg) (static_cast<void>(0)) |
226 | 226 |
# define LEMON_DEBUG(exp, msg) (static_cast<void>(0)) |
227 | 227 |
# else |
228 |
# define LEMON_ASSERT(exp, msg) \ |
|
229 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
|
228 |
# define LEMON_ASSERT(exp, msg) \ |
|
229 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
|
230 | 230 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
231 |
LEMON_FUNCTION_NAME, \ |
|
232 |
::lemon::_assert_bits::cstringify(msg), \ |
|
231 |
LEMON_FUNCTION_NAME, \ |
|
232 |
::lemon::_assert_bits::cstringify(msg), \ |
|
233 | 233 |
#exp), 0))) |
234 |
# define LEMON_FIXME(msg) \ |
|
235 |
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME, \ |
|
236 |
|
|
234 |
# define LEMON_FIXME(msg) \ |
|
235 |
(LEMON_ASSERT_HANDLER(__FILE__, __LINE__, LEMON_FUNCTION_NAME, \ |
|
236 |
::lemon::_assert_bits::cstringify(msg), \ |
|
237 | 237 |
static_cast<const char*>(0))) |
238 | 238 |
|
239 | 239 |
# if LEMON_ENABLE_DEBUG |
240 | 240 |
# define LEMON_DEBUG(exp, msg) |
241 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
|
242 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
|
243 |
LEMON_FUNCTION_NAME, \ |
|
244 |
::lemon::_assert_bits::cstringify(msg), \ |
|
241 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
|
242 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
|
243 |
LEMON_FUNCTION_NAME, \ |
|
244 |
::lemon::_assert_bits::cstringify(msg), \ |
|
245 | 245 |
#exp), 0))) |
246 | 246 |
# else |
247 | 247 |
# define LEMON_DEBUG(exp, msg) (static_cast<void>(0)) |
248 | 248 |
# endif |
249 | 249 |
# endif |
250 | 250 |
|
251 | 251 |
#endif |
252 |
|
|
253 |
#ifdef DOXYGEN |
|
254 |
|
|
255 |
|
|
256 |
#else |
|
257 |
|
|
258 |
|
|
259 |
#endif |
|
260 |
|
|
261 |
... | ... |
@@ -7,301 +7,301 @@ |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 | 23 |
///\todo Iterators have obsolete style |
24 | 24 |
|
25 | 25 |
#ifndef LEMON_CONCEPT_PATH_H |
26 | 26 |
#define LEMON_CONCEPT_PATH_H |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/invalid.h> |
29 | 29 |
#include <lemon/bits/utility.h> |
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
namespace concepts { |
34 | 34 |
|
35 | 35 |
/// \addtogroup concept |
36 | 36 |
/// @{ |
37 | 37 |
|
38 | 38 |
/// \brief A skeleton structure for representing directed paths in |
39 | 39 |
/// a digraph. |
40 | 40 |
/// |
41 | 41 |
/// A skeleton structure for representing directed paths in a |
42 | 42 |
/// digraph. |
43 | 43 |
/// \tparam _Digraph The digraph type in which the path is. |
44 | 44 |
/// |
45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
46 | 46 |
/// lemon path type stores just this list. As a consequence it |
47 | 47 |
/// cannot enumerate the nodes in the path and the zero length |
48 | 48 |
/// paths cannot store the source. |
49 | 49 |
/// |
50 | 50 |
template <typename _Digraph> |
51 | 51 |
class Path { |
52 | 52 |
public: |
53 | 53 |
|
54 | 54 |
/// Type of the underlying digraph. |
55 | 55 |
typedef _Digraph Digraph; |
56 | 56 |
/// Arc type of the underlying digraph. |
57 | 57 |
typedef typename Digraph::Arc Arc; |
58 | 58 |
|
59 | 59 |
class ArcIt; |
60 | 60 |
|
61 | 61 |
/// \brief Default constructor |
62 | 62 |
Path() {} |
63 | 63 |
|
64 | 64 |
/// \brief Template constructor |
65 | 65 |
template <typename CPath> |
66 | 66 |
Path(const CPath& cpath) {} |
67 | 67 |
|
68 | 68 |
/// \brief Template assigment |
69 | 69 |
template <typename CPath> |
70 | 70 |
Path& operator=(const CPath& cpath) {} |
71 | 71 |
|
72 | 72 |
/// Length of the path ie. the number of arcs in the path. |
73 | 73 |
int length() const { return 0;} |
74 | 74 |
|
75 | 75 |
/// Returns whether the path is empty. |
76 | 76 |
bool empty() const { return true;} |
77 | 77 |
|
78 | 78 |
/// Resets the path to an empty path. |
79 | 79 |
void clear() {} |
80 | 80 |
|
81 | 81 |
/// \brief Lemon style iterator for path arcs |
82 | 82 |
/// |
83 | 83 |
/// This class is used to iterate on the arcs of the paths. |
84 | 84 |
class ArcIt { |
85 | 85 |
public: |
86 | 86 |
/// Default constructor |
87 | 87 |
ArcIt() {} |
88 | 88 |
/// Invalid constructor |
89 | 89 |
ArcIt(Invalid) {} |
90 | 90 |
/// Constructor for first arc |
91 | 91 |
ArcIt(const Path &) {} |
92 | 92 |
|
93 | 93 |
/// Conversion to Arc |
94 | 94 |
operator Arc() const { return INVALID; } |
95 | 95 |
|
96 | 96 |
/// Next arc |
97 | 97 |
ArcIt& operator++() {return *this;} |
98 | 98 |
|
99 | 99 |
/// Comparison operator |
100 | 100 |
bool operator==(const ArcIt&) const {return true;} |
101 | 101 |
/// Comparison operator |
102 | 102 |
bool operator!=(const ArcIt&) const {return true;} |
103 |
/// Comparison operator |
|
104 |
bool operator<(const ArcIt&) const {return false;} |
|
103 |
/// Comparison operator |
|
104 |
bool operator<(const ArcIt&) const {return false;} |
|
105 | 105 |
|
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename _Path> |
109 | 109 |
struct Constraints { |
110 | 110 |
void constraints() { |
111 | 111 |
Path<Digraph> pc; |
112 | 112 |
_Path p, pp(pc); |
113 | 113 |
int l = p.length(); |
114 | 114 |
int e = p.empty(); |
115 | 115 |
p.clear(); |
116 | 116 |
|
117 | 117 |
p = pc; |
118 | 118 |
|
119 | 119 |
typename _Path::ArcIt id, ii(INVALID), i(p); |
120 | 120 |
|
121 | 121 |
++i; |
122 | 122 |
typename Digraph::Arc ed = i; |
123 | 123 |
|
124 | 124 |
e = (i == ii); |
125 | 125 |
e = (i != ii); |
126 | 126 |
e = (i < ii); |
127 | 127 |
|
128 | 128 |
ignore_unused_variable_warning(l); |
129 | 129 |
ignore_unused_variable_warning(pp); |
130 | 130 |
ignore_unused_variable_warning(e); |
131 | 131 |
ignore_unused_variable_warning(id); |
132 | 132 |
ignore_unused_variable_warning(ii); |
133 | 133 |
ignore_unused_variable_warning(ed); |
134 | 134 |
} |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
}; |
138 | 138 |
|
139 | 139 |
namespace _path_bits { |
140 | 140 |
|
141 | 141 |
template <typename _Digraph, typename _Path, typename RevPathTag = void> |
142 | 142 |
struct PathDumperConstraints { |
143 | 143 |
void constraints() { |
144 | 144 |
int l = p.length(); |
145 | 145 |
int e = p.empty(); |
146 | 146 |
|
147 | 147 |
typename _Path::ArcIt id, i(p); |
148 | 148 |
|
149 | 149 |
++i; |
150 | 150 |
typename _Digraph::Arc ed = i; |
151 | 151 |
|
152 | 152 |
e = (i == INVALID); |
153 | 153 |
e = (i != INVALID); |
154 | 154 |
|
155 | 155 |
ignore_unused_variable_warning(l); |
156 | 156 |
ignore_unused_variable_warning(e); |
157 | 157 |
ignore_unused_variable_warning(id); |
158 | 158 |
ignore_unused_variable_warning(ed); |
159 | 159 |
} |
160 | 160 |
_Path& p; |
161 | 161 |
}; |
162 | 162 |
|
163 | 163 |
template <typename _Digraph, typename _Path> |
164 | 164 |
struct PathDumperConstraints< |
165 | 165 |
_Digraph, _Path, |
166 | 166 |
typename enable_if<typename _Path::RevPathTag, void>::type |
167 | 167 |
> { |
168 | 168 |
void constraints() { |
169 | 169 |
int l = p.length(); |
170 | 170 |
int e = p.empty(); |
171 | 171 |
|
172 | 172 |
typename _Path::RevArcIt id, i(p); |
173 | 173 |
|
174 | 174 |
++i; |
175 | 175 |
typename _Digraph::Arc ed = i; |
176 | 176 |
|
177 | 177 |
e = (i == INVALID); |
178 | 178 |
e = (i != INVALID); |
179 | 179 |
|
180 | 180 |
ignore_unused_variable_warning(l); |
181 | 181 |
ignore_unused_variable_warning(e); |
182 | 182 |
ignore_unused_variable_warning(id); |
183 | 183 |
ignore_unused_variable_warning(ed); |
184 | 184 |
} |
185 | 185 |
_Path& p; |
186 | 186 |
}; |
187 | 187 |
|
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
|
191 | 191 |
/// \brief A skeleton structure for path dumpers. |
192 | 192 |
/// |
193 | 193 |
/// A skeleton structure for path dumpers. The path dumpers are |
194 | 194 |
/// the generalization of the paths. The path dumpers can |
195 | 195 |
/// enumerate the arcs of the path wheter in forward or in |
196 | 196 |
/// backward order. In most time these classes are not used |
197 | 197 |
/// directly rather it used to assign a dumped class to a real |
198 | 198 |
/// path type. |
199 | 199 |
/// |
200 | 200 |
/// The main purpose of this concept is that the shortest path |
201 | 201 |
/// algorithms can enumerate easily the arcs in reverse order. |
202 | 202 |
/// If we would like to give back a real path from these |
203 | 203 |
/// algorithms then we should create a temporarly path object. In |
204 | 204 |
/// Lemon such algorithms gives back a path dumper what can |
205 | 205 |
/// assigned to a real path and the dumpers can be implemented as |
206 | 206 |
/// an adaptor class to the predecessor map. |
207 | 207 |
|
208 | 208 |
/// \tparam _Digraph The digraph type in which the path is. |
209 | 209 |
/// |
210 | 210 |
/// The paths can be constructed from any path type by a |
211 | 211 |
/// template constructor or a template assignment operator. |
212 | 212 |
/// |
213 | 213 |
template <typename _Digraph> |
214 | 214 |
class PathDumper { |
215 | 215 |
public: |
216 | 216 |
|
217 | 217 |
/// Type of the underlying digraph. |
218 | 218 |
typedef _Digraph Digraph; |
219 | 219 |
/// Arc type of the underlying digraph. |
220 | 220 |
typedef typename Digraph::Arc Arc; |
221 | 221 |
|
222 | 222 |
/// Length of the path ie. the number of arcs in the path. |
223 | 223 |
int length() const { return 0;} |
224 | 224 |
|
225 | 225 |
/// Returns whether the path is empty. |
226 | 226 |
bool empty() const { return true;} |
227 | 227 |
|
228 | 228 |
/// \brief Forward or reverse dumping |
229 | 229 |
/// |
230 | 230 |
/// If the RevPathTag is defined and true then reverse dumping |
231 | 231 |
/// is provided in the path dumper. In this case instead of the |
232 | 232 |
/// ArcIt the RevArcIt iterator should be implemented in the |
233 | 233 |
/// dumper. |
234 | 234 |
typedef False RevPathTag; |
235 | 235 |
|
236 | 236 |
/// \brief Lemon style iterator for path arcs |
237 | 237 |
/// |
238 | 238 |
/// This class is used to iterate on the arcs of the paths. |
239 | 239 |
class ArcIt { |
240 | 240 |
public: |
241 | 241 |
/// Default constructor |
242 | 242 |
ArcIt() {} |
243 | 243 |
/// Invalid constructor |
244 | 244 |
ArcIt(Invalid) {} |
245 | 245 |
/// Constructor for first arc |
246 | 246 |
ArcIt(const PathDumper&) {} |
247 | 247 |
|
248 | 248 |
/// Conversion to Arc |
249 | 249 |
operator Arc() const { return INVALID; } |
250 | 250 |
|
251 | 251 |
/// Next arc |
252 | 252 |
ArcIt& operator++() {return *this;} |
253 | 253 |
|
254 | 254 |
/// Comparison operator |
255 | 255 |
bool operator==(const ArcIt&) const {return true;} |
256 | 256 |
/// Comparison operator |
257 | 257 |
bool operator!=(const ArcIt&) const {return true;} |
258 |
/// Comparison operator |
|
259 |
bool operator<(const ArcIt&) const {return false;} |
|
258 |
/// Comparison operator |
|
259 |
bool operator<(const ArcIt&) const {return false;} |
|
260 | 260 |
|
261 | 261 |
}; |
262 | 262 |
|
263 | 263 |
/// \brief Lemon style iterator for path arcs |
264 | 264 |
/// |
265 | 265 |
/// This class is used to iterate on the arcs of the paths in |
266 | 266 |
/// reverse direction. |
267 | 267 |
class RevArcIt { |
268 | 268 |
public: |
269 | 269 |
/// Default constructor |
270 | 270 |
RevArcIt() {} |
271 | 271 |
/// Invalid constructor |
272 | 272 |
RevArcIt(Invalid) {} |
273 | 273 |
/// Constructor for first arc |
274 | 274 |
RevArcIt(const PathDumper &) {} |
275 | 275 |
|
276 | 276 |
/// Conversion to Arc |
277 | 277 |
operator Arc() const { return INVALID; } |
278 | 278 |
|
279 | 279 |
/// Next arc |
280 | 280 |
RevArcIt& operator++() {return *this;} |
281 | 281 |
|
282 | 282 |
/// Comparison operator |
283 | 283 |
bool operator==(const RevArcIt&) const {return true;} |
284 | 284 |
/// Comparison operator |
285 | 285 |
bool operator!=(const RevArcIt&) const {return true;} |
286 |
/// Comparison operator |
|
287 |
bool operator<(const RevArcIt&) const {return false;} |
|
286 |
/// Comparison operator |
|
287 |
bool operator<(const RevArcIt&) const {return false;} |
|
288 | 288 |
|
289 | 289 |
}; |
290 | 290 |
|
291 | 291 |
template <typename _Path> |
292 | 292 |
struct Constraints { |
293 | 293 |
void constraints() { |
294 | 294 |
function_requires<_path_bits:: |
295 | 295 |
PathDumperConstraints<Digraph, _Path> >(); |
296 | 296 |
} |
297 | 297 |
}; |
298 | 298 |
|
299 | 299 |
}; |
300 | 300 |
|
301 | 301 |
|
302 | 302 |
///@} |
303 | 303 |
} |
304 | 304 |
|
305 | 305 |
} // namespace lemon |
306 | 306 |
|
307 | 307 |
#endif // LEMON_CONCEPT_PATH_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_ERROR_H |
20 | 20 |
#define LEMON_ERROR_H |
21 | 21 |
|
22 | 22 |
/// \ingroup exceptions |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Basic exception classes and error handling. |
25 | 25 |
|
26 | 26 |
#include <exception> |
27 | 27 |
#include <string> |
28 | 28 |
#include <sstream> |
29 | 29 |
#include <iostream> |
30 | 30 |
#include <cstdlib> |
31 | 31 |
#include <memory> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
/// \addtogroup exceptions |
36 | 36 |
/// @{ |
37 | 37 |
|
38 | 38 |
/// \brief Exception safe wrapper class. |
39 | 39 |
/// |
40 | 40 |
/// Exception safe wrapper class to implement the members of exceptions. |
41 | 41 |
template <typename _Type> |
42 | 42 |
class ExceptionMember { |
43 | 43 |
public: |
44 | 44 |
typedef _Type Type; |
45 | 45 |
|
46 | 46 |
ExceptionMember() throw() { |
47 | 47 |
try { |
48 | 48 |
ptr.reset(new Type()); |
49 | 49 |
} catch (...) {} |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
ExceptionMember(const Type& type) throw() { |
53 | 53 |
try { |
54 | 54 |
ptr.reset(new Type()); |
55 | 55 |
if (ptr.get() == 0) return; |
56 | 56 |
*ptr = type; |
57 | 57 |
} catch (...) {} |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
ExceptionMember(const ExceptionMember& copy) throw() { |
61 | 61 |
try { |
62 | 62 |
if (!copy.valid()) return; |
63 | 63 |
ptr.reset(new Type()); |
64 | 64 |
if (ptr.get() == 0) return; |
65 | 65 |
*ptr = copy.get(); |
66 | 66 |
} catch (...) {} |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
ExceptionMember& operator=(const ExceptionMember& copy) throw() { |
70 | 70 |
if (ptr.get() == 0) return; |
71 | 71 |
try { |
72 | 72 |
if (!copy.valid()) return; |
73 |
|
|
73 |
*ptr = copy.get(); |
|
74 | 74 |
} catch (...) {} |
75 | 75 |
} |
76 | 76 |
|
77 | 77 |
void set(const Type& type) throw() { |
78 | 78 |
if (ptr.get() == 0) return; |
79 | 79 |
try { |
80 | 80 |
*ptr = type; |
81 | 81 |
} catch (...) {} |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
const Type& get() const { |
85 | 85 |
return *ptr; |
86 | 86 |
} |
87 | 87 |
|
88 | 88 |
bool valid() const throw() { |
89 | 89 |
return ptr.get() != 0; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
private: |
93 | 93 |
std::auto_ptr<_Type> ptr; |
94 | 94 |
}; |
95 | 95 |
|
96 | 96 |
/// Exception-safe convenient error message builder class. |
97 | 97 |
|
98 | 98 |
/// Helper class which provides a convenient ostream-like (operator << |
99 | 99 |
/// based) interface to create a string message. Mostly useful in |
100 | 100 |
/// exception classes (therefore the name). |
101 | 101 |
class ErrorMessage { |
102 | 102 |
protected: |
103 | 103 |
///\e |
104 | 104 |
|
105 | 105 |
///\todo The good solution is boost::shared_ptr... |
106 | 106 |
/// |
107 | 107 |
mutable std::auto_ptr<std::ostringstream> buf; |
108 | 108 |
|
109 | 109 |
///\e |
110 | 110 |
bool init() throw() { |
111 | 111 |
try { |
112 | 112 |
buf.reset(new std::ostringstream); |
113 | 113 |
} |
114 | 114 |
catch(...) { |
115 | 115 |
buf.reset(); |
116 | 116 |
} |
117 | 117 |
return buf.get(); |
118 | 118 |
} |
119 | 119 |
|
120 | 120 |
public: |
121 | 121 |
|
122 | 122 |
///\e |
123 | 123 |
ErrorMessage() throw() { init(); } |
124 | 124 |
|
125 | 125 |
ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { } |
126 | 126 |
|
127 | 127 |
///\e |
128 | 128 |
ErrorMessage(const char *msg) throw() { |
129 | 129 |
init(); |
130 | 130 |
*this << msg; |
131 | 131 |
} |
132 | 132 |
|
133 | 133 |
///\e |
134 | 134 |
ErrorMessage(const std::string &msg) throw() { |
135 | 135 |
init(); |
136 | 136 |
*this << msg; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
///\e |
140 | 140 |
template <typename T> |
141 | 141 |
ErrorMessage& operator<<(const T &t) throw() { |
142 | 142 |
if( ! buf.get() ) return *this; |
143 | 143 |
|
144 | 144 |
try { |
145 | 145 |
*buf << t; |
146 | 146 |
} |
147 | 147 |
catch(...) { |
148 | 148 |
buf.reset(); |
149 | 149 |
} |
150 | 150 |
return *this; |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
///\e |
154 | 154 |
const char* message() throw() { |
155 | 155 |
if( ! buf.get() ) return 0; |
156 | 156 |
|
157 | 157 |
const char* mes = 0; |
158 | 158 |
try { |
159 | 159 |
mes = buf->str().c_str(); |
160 | 160 |
} |
161 | 161 |
catch(...) {} |
162 | 162 |
return mes; |
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
}; |
166 | 166 |
|
167 | 167 |
/// Generic exception class. |
168 | 168 |
|
169 | 169 |
/// Base class for exceptions used in LEMON. |
... | ... |
@@ -815,201 +815,201 @@ |
815 | 815 |
<< " newpath 5 index 5 index moveto " |
816 | 816 |
<< "5 index 5 index 5 index 3.01 mul sub\n" |
817 | 817 |
<< " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub" |
818 | 818 |
<< " moveto\n" |
819 | 819 |
<< " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto " |
820 | 820 |
<< "stroke\n" |
821 | 821 |
<< " 5 index 5 index 5 index c fill\n" |
822 | 822 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
823 | 823 |
<< " } bind def\n"; |
824 | 824 |
os << "/nmale {\n" |
825 | 825 |
<< " 0 0 0 setrgbcolor 3 index " |
826 | 826 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
827 | 827 |
<<" 1.5 mul mul setlinewidth\n" |
828 | 828 |
<< " newpath 5 index 5 index moveto\n" |
829 | 829 |
<< " 5 index 4 index 1 mul 1.5 mul add\n" |
830 | 830 |
<< " 5 index 5 index 3 sqrt 1.5 mul mul add\n" |
831 | 831 |
<< " 1 index 1 index lineto\n" |
832 | 832 |
<< " 1 index 1 index 7 index sub moveto\n" |
833 | 833 |
<< " 1 index 1 index lineto\n" |
834 | 834 |
<< " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub" |
835 | 835 |
<< " lineto\n" |
836 | 836 |
<< " stroke\n" |
837 | 837 |
<< " 5 index 5 index 5 index c fill\n" |
838 | 838 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
839 | 839 |
<< " } bind def\n"; |
840 | 840 |
|
841 | 841 |
|
842 | 842 |
os << "/arrl " << _arrowLength << " def\n"; |
843 | 843 |
os << "/arrw " << _arrowWidth << " def\n"; |
844 | 844 |
// l dx_norm dy_norm |
845 | 845 |
os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
846 | 846 |
//len w dx_norm dy_norm x1 y1 cr cg cb |
847 | 847 |
os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx " |
848 | 848 |
<< "exch def\n" |
849 | 849 |
<< " /w exch def /len exch def\n" |
850 | 850 |
//<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
851 | 851 |
<< " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
852 | 852 |
<< " len w sub arrl sub dx dy lrl\n" |
853 | 853 |
<< " arrw dy dx neg lrl\n" |
854 | 854 |
<< " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
855 | 855 |
<< " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
856 | 856 |
<< " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
857 | 857 |
<< " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
858 | 858 |
<< " arrw dy dx neg lrl\n" |
859 | 859 |
<< " len w sub arrl sub neg dx dy lrl\n" |
860 | 860 |
<< " closepath fill } bind def\n"; |
861 | 861 |
os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
862 | 862 |
<< " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
863 | 863 |
|
864 | 864 |
os << "\ngsave\n"; |
865 | 865 |
if(_scaleToA4) |
866 | 866 |
if(bb.height()>bb.width()) { |
867 | 867 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), |
868 | 868 |
(A4WIDTH-2*A4BORDER)/bb.width()); |
869 | 869 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' |
870 | 870 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER |
871 | 871 |
<< " translate\n" |
872 | 872 |
<< sc << " dup scale\n" |
873 | 873 |
<< -bb.left() << ' ' << -bb.bottom() << " translate\n"; |
874 | 874 |
} |
875 | 875 |
else { |
876 | 876 |
//\todo Verify centering |
877 | 877 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), |
878 | 878 |
(A4WIDTH-2*A4BORDER)/bb.height()); |
879 | 879 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' |
880 | 880 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER |
881 | 881 |
<< " translate\n" |
882 | 882 |
<< sc << " dup scale\n90 rotate\n" |
883 | 883 |
<< -bb.left() << ' ' << -bb.top() << " translate\n"; |
884 | 884 |
} |
885 | 885 |
else if(_scale!=1.0) os << _scale << " dup scale\n"; |
886 | 886 |
|
887 | 887 |
if(_showArcs) { |
888 | 888 |
os << "%Arcs:\ngsave\n"; |
889 | 889 |
if(_enableParallel) { |
890 | 890 |
std::vector<Arc> el; |
891 | 891 |
for(ArcIt e(g);e!=INVALID;++e) |
892 | 892 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
893 | 893 |
&&g.source(e)!=g.target(e)) |
894 | 894 |
el.push_back(e); |
895 | 895 |
std::sort(el.begin(),el.end(),arcLess(g)); |
896 | 896 |
|
897 | 897 |
typename std::vector<Arc>::iterator j; |
898 | 898 |
for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) { |
899 | 899 |
for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
900 | 900 |
|
901 | 901 |
double sw=0; |
902 | 902 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) |
903 | 903 |
sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; |
904 | 904 |
sw-=_parArcDist; |
905 | 905 |
sw/=-2.0; |
906 | 906 |
dim2::Point<double> |
907 | 907 |
dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); |
908 | 908 |
double l=std::sqrt(dvec.normSquare()); |
909 | 909 |
//\todo better 'epsilon' would be nice here. |
910 | 910 |
dim2::Point<double> d(dvec/std::max(l,EPSILON)); |
911 |
|
|
911 |
dim2::Point<double> m; |
|
912 | 912 |
// m=dim2::Point<double>(mycoords[g.target(*i)]+ |
913 | 913 |
// mycoords[g.source(*i)])/2.0; |
914 | 914 |
|
915 | 915 |
// m=dim2::Point<double>(mycoords[g.source(*i)])+ |
916 | 916 |
// dvec*(double(_nodeSizes[g.source(*i)])/ |
917 | 917 |
// (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
918 | 918 |
|
919 |
|
|
919 |
m=dim2::Point<double>(mycoords[g.source(*i)])+ |
|
920 | 920 |
d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
921 | 921 |
|
922 | 922 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) { |
923 | 923 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0; |
924 | 924 |
dim2::Point<double> mm=m+rot90(d)*sw/.75; |
925 | 925 |
if(_drawArrows) { |
926 | 926 |
int node_shape; |
927 | 927 |
dim2::Point<double> s=mycoords[g.source(*e)]; |
928 | 928 |
dim2::Point<double> t=mycoords[g.target(*e)]; |
929 | 929 |
double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
930 | 930 |
node_shape=_nodeShapes[g.target(*e)]; |
931 | 931 |
dim2::Bezier3 bez(s,mm,mm,t); |
932 | 932 |
double t1=0,t2=1; |
933 | 933 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
934 | 934 |
if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
935 | 935 |
else t1=(t1+t2)/2; |
936 | 936 |
dim2::Point<double> apoint=bez((t1+t2)/2); |
937 | 937 |
rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; |
938 | 938 |
rn*=rn; |
939 | 939 |
t2=(t1+t2)/2;t1=0; |
940 | 940 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
941 | 941 |
if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
942 | 942 |
else t2=(t1+t2)/2; |
943 | 943 |
dim2::Point<double> linend=bez((t1+t2)/2); |
944 | 944 |
bez=bez.before((t1+t2)/2); |
945 | 945 |
// rn=_nodeSizes[g.source(*e)]*_nodeScale; |
946 | 946 |
// node_shape=_nodeShapes[g.source(*e)]; |
947 | 947 |
// t1=0;t2=1; |
948 | 948 |
// for(int i=0;i<INTERPOL_PREC;++i) |
949 | 949 |
// if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) |
950 | 950 |
// t1=(t1+t2)/2; |
951 | 951 |
// else t2=(t1+t2)/2; |
952 | 952 |
// bez=bez.after((t1+t2)/2); |
953 | 953 |
os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
954 | 954 |
<< _arcColors[*e].red() << ' ' |
955 | 955 |
<< _arcColors[*e].green() << ' ' |
956 | 956 |
<< _arcColors[*e].blue() << " setrgbcolor newpath\n" |
957 | 957 |
<< bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
958 | 958 |
<< bez.p2.x << ' ' << bez.p2.y << ' ' |
959 | 959 |
<< bez.p3.x << ' ' << bez.p3.y << ' ' |
960 | 960 |
<< bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
961 | 961 |
dim2::Point<double> dd(rot90(linend-apoint)); |
962 | 962 |
dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ |
963 | 963 |
std::sqrt(dd.normSquare()); |
964 | 964 |
os << "newpath " << psOut(apoint) << " moveto " |
965 | 965 |
<< psOut(linend+dd) << " lineto " |
966 | 966 |
<< psOut(linend-dd) << " lineto closepath fill\n"; |
967 | 967 |
} |
968 | 968 |
else { |
969 | 969 |
os << mycoords[g.source(*e)].x << ' ' |
970 | 970 |
<< mycoords[g.source(*e)].y << ' ' |
971 | 971 |
<< mm.x << ' ' << mm.y << ' ' |
972 | 972 |
<< mycoords[g.target(*e)].x << ' ' |
973 | 973 |
<< mycoords[g.target(*e)].y << ' ' |
974 | 974 |
<< _arcColors[*e].red() << ' ' |
975 | 975 |
<< _arcColors[*e].green() << ' ' |
976 | 976 |
<< _arcColors[*e].blue() << ' ' |
977 | 977 |
<< _arcWidths[*e]*_arcWidthScale << " lb\n"; |
978 | 978 |
} |
979 | 979 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; |
980 | 980 |
} |
981 | 981 |
} |
982 | 982 |
} |
983 | 983 |
else for(ArcIt e(g);e!=INVALID;++e) |
984 | 984 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
985 | 985 |
&&g.source(e)!=g.target(e)) { |
986 | 986 |
if(_drawArrows) { |
987 | 987 |
dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]); |
988 | 988 |
double rn=_nodeSizes[g.target(e)]*_nodeScale; |
989 | 989 |
int node_shape=_nodeShapes[g.target(e)]; |
990 | 990 |
double t1=0,t2=1; |
991 | 991 |
for(int i=0;i<INTERPOL_PREC;++i) |
992 | 992 |
if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2; |
993 | 993 |
else t2=(t1+t2)/2; |
994 | 994 |
double l=std::sqrt(d.normSquare()); |
995 | 995 |
d/=l; |
996 | 996 |
|
997 | 997 |
os << l*(1-(t1+t2)/2) << ' ' |
998 | 998 |
<< _arcWidths[e]*_arcWidthScale << ' ' |
999 | 999 |
<< d.x << ' ' << d.y << ' ' |
1000 | 1000 |
<< mycoords[g.source(e)].x << ' ' |
1001 | 1001 |
<< mycoords[g.source(e)].y << ' ' |
1002 | 1002 |
<< _arcColors[e].red() << ' ' |
1003 | 1003 |
<< _arcColors[e].green() << ' ' |
1004 | 1004 |
<< _arcColors[e].blue() << " arr\n"; |
1005 | 1005 |
} |
1006 | 1006 |
else os << mycoords[g.source(e)].x << ' ' |
1007 | 1007 |
<< mycoords[g.source(e)].y << ' ' |
1008 | 1008 |
<< mycoords[g.target(e)].x << ' ' |
1009 | 1009 |
<< mycoords[g.target(e)].y << ' ' |
1010 | 1010 |
<< _arcColors[e].red() << ' ' |
1011 | 1011 |
<< _arcColors[e].green() << ' ' |
1012 | 1012 |
<< _arcColors[e].blue() << ' ' |
1013 | 1013 |
<< _arcWidths[e]*_arcWidthScale << " l\n"; |
1014 | 1014 |
} |
1015 | 1015 |
os << "grestore\n"; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_GRAPH_UTILS_H |
20 | 20 |
#define LEMON_GRAPH_UTILS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <vector> |
24 | 24 |
#include <map> |
25 | 25 |
#include <cmath> |
26 | 26 |
#include <algorithm> |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/invalid.h> |
29 | 29 |
#include <lemon/bits/utility.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/bits/traits.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/bits/alteration_notifier.h> |
34 | 34 |
#include <lemon/bits/default_map.h> |
35 | 35 |
|
36 | 36 |
///\ingroup gutils |
37 | 37 |
///\file |
38 | 38 |
///\brief Graph utilities. |
39 | 39 |
|
40 | 40 |
namespace lemon { |
41 | 41 |
|
42 | 42 |
/// \addtogroup gutils |
43 | 43 |
/// @{ |
44 | 44 |
|
45 | 45 |
///Creates convenience typedefs for the digraph types and iterators |
46 | 46 |
|
47 | 47 |
///This \c \#define creates convenience typedefs for the following types |
48 | 48 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
49 | 49 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
50 | 50 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
51 | 51 |
/// |
52 | 52 |
///\note If the graph type is a dependent type, ie. the graph type depend |
53 | 53 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
54 | 54 |
///macro. |
55 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
|
56 |
typedef Digraph::Node Node; \ |
|
57 |
typedef Digraph::NodeIt NodeIt; \ |
|
58 |
typedef Digraph::Arc Arc; \ |
|
59 |
typedef Digraph::ArcIt ArcIt; \ |
|
60 |
typedef Digraph::InArcIt InArcIt; \ |
|
61 |
typedef Digraph::OutArcIt OutArcIt; \ |
|
62 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
|
63 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
|
64 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
|
65 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
|
66 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
|
55 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
|
56 |
typedef Digraph::Node Node; \ |
|
57 |
typedef Digraph::NodeIt NodeIt; \ |
|
58 |
typedef Digraph::Arc Arc; \ |
|
59 |
typedef Digraph::ArcIt ArcIt; \ |
|
60 |
typedef Digraph::InArcIt InArcIt; \ |
|
61 |
typedef Digraph::OutArcIt OutArcIt; \ |
|
62 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
|
63 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
|
64 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
|
65 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
|
66 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
|
67 | 67 |
typedef Digraph::ArcMap<double> DoubleArcMap |
68 | 68 |
|
69 | 69 |
///Creates convenience typedefs for the digraph types and iterators |
70 | 70 |
|
71 | 71 |
///\see DIGRAPH_TYPEDEFS |
72 | 72 |
/// |
73 | 73 |
///\note Use this macro, if the graph type is a dependent type, |
74 | 74 |
///ie. the graph type depend on a template parameter. |
75 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
|
76 |
typedef typename Digraph::Node Node; \ |
|
77 |
typedef typename Digraph::NodeIt NodeIt; \ |
|
78 |
typedef typename Digraph::Arc Arc; \ |
|
75 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
|
76 |
typedef typename Digraph::Node Node; \ |
|
77 |
typedef typename Digraph::NodeIt NodeIt; \ |
|
78 |
typedef typename Digraph::Arc Arc; \ |
|
79 | 79 |
typedef typename Digraph::ArcIt ArcIt; \ |
80 |
typedef typename Digraph::InArcIt InArcIt; \ |
|
81 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
|
82 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
|
83 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
|
84 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
|
85 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
|
86 |
typedef typename Digraph:: |
|
80 |
typedef typename Digraph::InArcIt InArcIt; \ |
|
81 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
|
82 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
|
83 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
|
84 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
|
85 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
|
86 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
|
87 | 87 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
88 | 88 |
|
89 | 89 |
///Creates convenience typedefs for the graph types and iterators |
90 | 90 |
|
91 | 91 |
///This \c \#define creates the same convenience typedefs as defined |
92 | 92 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
93 | 93 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
94 | 94 |
///\c DoubleEdgeMap. |
95 | 95 |
/// |
96 | 96 |
///\note If the graph type is a dependent type, ie. the graph type depend |
97 | 97 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
98 | 98 |
///macro. |
99 |
#define GRAPH_TYPEDEFS(Graph) \ |
|
100 |
DIGRAPH_TYPEDEFS(Graph); \ |
|
101 |
typedef Graph::Edge Edge; \ |
|
102 |
typedef Graph::EdgeIt EdgeIt; \ |
|
103 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
|
104 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
|
105 |
|
|
99 |
#define GRAPH_TYPEDEFS(Graph) \ |
|
100 |
DIGRAPH_TYPEDEFS(Graph); \ |
|
101 |
typedef Graph::Edge Edge; \ |
|
102 |
typedef Graph::EdgeIt EdgeIt; \ |
|
103 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
|
104 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
|
105 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
|
106 | 106 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
107 | 107 |
|
108 | 108 |
///Creates convenience typedefs for the graph types and iterators |
109 | 109 |
|
110 | 110 |
///\see GRAPH_TYPEDEFS |
111 | 111 |
/// |
112 | 112 |
///\note Use this macro, if the graph type is a dependent type, |
113 | 113 |
///ie. the graph type depend on a template parameter. |
114 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
|
115 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
|
116 |
|
|
114 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
|
115 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
|
116 |
typedef typename Graph::Edge Edge; \ |
|
117 | 117 |
typedef typename Graph::EdgeIt EdgeIt; \ |
118 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
|
119 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
|
120 |
typedef typename Graph:: |
|
118 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
|
119 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
|
120 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
|
121 | 121 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
122 | 122 |
|
123 | 123 |
/// \brief Function to count the items in the graph. |
124 | 124 |
/// |
125 | 125 |
/// This function counts the items (nodes, arcs etc) in the graph. |
126 | 126 |
/// The complexity of the function is O(n) because |
127 | 127 |
/// it iterates on all of the items. |
128 | 128 |
template <typename Graph, typename Item> |
129 | 129 |
inline int countItems(const Graph& g) { |
130 | 130 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
131 | 131 |
int num = 0; |
132 | 132 |
for (ItemIt it(g); it != INVALID; ++it) { |
133 | 133 |
++num; |
134 | 134 |
} |
135 | 135 |
return num; |
136 | 136 |
} |
137 | 137 |
|
138 | 138 |
// Node counting: |
139 | 139 |
|
140 | 140 |
namespace _graph_utils_bits { |
141 | 141 |
|
142 | 142 |
template <typename Graph, typename Enable = void> |
143 | 143 |
struct CountNodesSelector { |
144 | 144 |
static int count(const Graph &g) { |
145 | 145 |
return countItems<Graph, typename Graph::Node>(g); |
146 | 146 |
} |
147 | 147 |
}; |
148 | 148 |
|
149 | 149 |
template <typename Graph> |
150 | 150 |
struct CountNodesSelector< |
151 | 151 |
Graph, typename |
152 | 152 |
enable_if<typename Graph::NodeNumTag, void>::type> |
153 | 153 |
{ |
154 | 154 |
static int count(const Graph &g) { |
155 | 155 |
return g.nodeNum(); |
156 | 156 |
} |
157 | 157 |
}; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
/// \brief Function to count the nodes in the graph. |
161 | 161 |
/// |
162 | 162 |
/// This function counts the nodes in the graph. |
163 | 163 |
/// The complexity of the function is O(n) but for some |
164 | 164 |
/// graph structures it is specialized to run in O(1). |
165 | 165 |
/// |
166 | 166 |
/// If the graph contains a \e nodeNum() member function and a |
167 | 167 |
/// \e NodeNumTag tag then this function calls directly the member |
168 | 168 |
/// function to query the cardinality of the node set. |
169 | 169 |
template <typename Graph> |
170 | 170 |
inline int countNodes(const Graph& g) { |
171 | 171 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g); |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
// Arc counting: |
175 | 175 |
|
176 | 176 |
namespace _graph_utils_bits { |
177 | 177 |
|
178 | 178 |
template <typename Graph, typename Enable = void> |
179 | 179 |
struct CountArcsSelector { |
180 | 180 |
static int count(const Graph &g) { |
181 | 181 |
return countItems<Graph, typename Graph::Arc>(g); |
182 | 182 |
} |
183 | 183 |
}; |
184 | 184 |
|
185 | 185 |
template <typename Graph> |
186 | 186 |
struct CountArcsSelector< |
187 | 187 |
Graph, |
188 | 188 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
189 | 189 |
{ |
190 | 190 |
static int count(const Graph &g) { |
191 | 191 |
return g.arcNum(); |
192 | 192 |
} |
193 | 193 |
}; |
194 | 194 |
} |
195 | 195 |
|
196 | 196 |
/// \brief Function to count the arcs in the graph. |
197 | 197 |
/// |
198 | 198 |
/// This function counts the arcs in the graph. |
199 | 199 |
/// The complexity of the function is O(e) but for some |
200 | 200 |
/// graph structures it is specialized to run in O(1). |
201 | 201 |
/// |
202 | 202 |
/// If the graph contains a \e arcNum() member function and a |
203 | 203 |
/// \e EdgeNumTag tag then this function calls directly the member |
204 | 204 |
/// function to query the cardinality of the arc set. |
205 | 205 |
template <typename Graph> |
206 | 206 |
inline int countArcs(const Graph& g) { |
207 | 207 |
return _graph_utils_bits::CountArcsSelector<Graph>::count(g); |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
// Edge counting: |
211 | 211 |
namespace _graph_utils_bits { |
212 | 212 |
|
213 | 213 |
template <typename Graph, typename Enable = void> |
214 | 214 |
struct CountEdgesSelector { |
215 | 215 |
static int count(const Graph &g) { |
216 | 216 |
return countItems<Graph, typename Graph::Edge>(g); |
... | ... |
@@ -53,193 +53,193 @@ |
53 | 53 |
char c; |
54 | 54 |
if (is >> std::ws >> c) { |
55 | 55 |
throw DataFormatError("Remaining characters in token"); |
56 | 56 |
} |
57 | 57 |
return value; |
58 | 58 |
} |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
template <> |
62 | 62 |
struct DefaultConverter<std::string> { |
63 | 63 |
std::string operator()(const std::string& str) { |
64 | 64 |
return str; |
65 | 65 |
} |
66 | 66 |
}; |
67 | 67 |
|
68 | 68 |
template <typename _Item> |
69 | 69 |
class MapStorageBase { |
70 | 70 |
public: |
71 | 71 |
typedef _Item Item; |
72 | 72 |
|
73 | 73 |
public: |
74 | 74 |
MapStorageBase() {} |
75 | 75 |
virtual ~MapStorageBase() {} |
76 | 76 |
|
77 | 77 |
virtual void set(const Item& item, const std::string& value) = 0; |
78 | 78 |
|
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
template <typename _Item, typename _Map, |
82 | 82 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
83 | 83 |
class MapStorage : public MapStorageBase<_Item> { |
84 | 84 |
public: |
85 | 85 |
typedef _Map Map; |
86 | 86 |
typedef _Converter Converter; |
87 | 87 |
typedef _Item Item; |
88 | 88 |
|
89 | 89 |
private: |
90 | 90 |
Map& _map; |
91 | 91 |
Converter _converter; |
92 | 92 |
|
93 | 93 |
public: |
94 | 94 |
MapStorage(Map& map, const Converter& converter = Converter()) |
95 | 95 |
: _map(map), _converter(converter) {} |
96 | 96 |
virtual ~MapStorage() {} |
97 | 97 |
|
98 | 98 |
virtual void set(const Item& item ,const std::string& value) { |
99 | 99 |
_map.set(item, _converter(value)); |
100 | 100 |
} |
101 | 101 |
}; |
102 | 102 |
|
103 | 103 |
template <typename _Graph, bool _dir, typename _Map, |
104 | 104 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
105 | 105 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> { |
106 | 106 |
public: |
107 | 107 |
typedef _Map Map; |
108 | 108 |
typedef _Converter Converter; |
109 | 109 |
typedef _Graph Graph; |
110 | 110 |
typedef typename Graph::Edge Item; |
111 | 111 |
static const bool dir = _dir; |
112 | 112 |
|
113 | 113 |
private: |
114 | 114 |
const Graph& _graph; |
115 | 115 |
Map& _map; |
116 | 116 |
Converter _converter; |
117 | 117 |
|
118 | 118 |
public: |
119 | 119 |
GraphArcMapStorage(const Graph& graph, Map& map, |
120 | 120 |
const Converter& converter = Converter()) |
121 | 121 |
: _graph(graph), _map(map), _converter(converter) {} |
122 | 122 |
virtual ~GraphArcMapStorage() {} |
123 | 123 |
|
124 | 124 |
virtual void set(const Item& item ,const std::string& value) { |
125 | 125 |
_map.set(_graph.direct(item, dir), _converter(value)); |
126 | 126 |
} |
127 | 127 |
}; |
128 | 128 |
|
129 | 129 |
class ValueStorageBase { |
130 | 130 |
public: |
131 | 131 |
ValueStorageBase() {} |
132 | 132 |
virtual ~ValueStorageBase() {} |
133 | 133 |
|
134 | 134 |
virtual void set(const std::string&) = 0; |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
138 | 138 |
class ValueStorage : public ValueStorageBase { |
139 | 139 |
public: |
140 | 140 |
typedef _Value Value; |
141 | 141 |
typedef _Converter Converter; |
142 | 142 |
|
143 | 143 |
private: |
144 | 144 |
Value& _value; |
145 | 145 |
Converter _converter; |
146 | 146 |
|
147 | 147 |
public: |
148 | 148 |
ValueStorage(Value& value, const Converter& converter = Converter()) |
149 |
|
|
149 |
: _value(value), _converter(converter) {} |
|
150 | 150 |
|
151 | 151 |
virtual void set(const std::string& value) { |
152 | 152 |
_value = _converter(value); |
153 | 153 |
} |
154 | 154 |
}; |
155 | 155 |
|
156 | 156 |
template <typename Value> |
157 | 157 |
struct MapLookUpConverter { |
158 | 158 |
const std::map<std::string, Value>& _map; |
159 | 159 |
|
160 | 160 |
MapLookUpConverter(const std::map<std::string, Value>& map) |
161 | 161 |
: _map(map) {} |
162 | 162 |
|
163 | 163 |
Value operator()(const std::string& str) { |
164 | 164 |
typename std::map<std::string, Value>::const_iterator it = |
165 | 165 |
_map.find(str); |
166 | 166 |
if (it == _map.end()) { |
167 | 167 |
std::ostringstream msg; |
168 | 168 |
msg << "Item not found: " << str; |
169 | 169 |
throw DataFormatError(msg.str().c_str()); |
170 | 170 |
} |
171 | 171 |
return it->second; |
172 | 172 |
} |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
template <typename Graph> |
176 | 176 |
struct GraphArcLookUpConverter { |
177 | 177 |
const Graph& _graph; |
178 | 178 |
const std::map<std::string, typename Graph::Edge>& _map; |
179 | 179 |
|
180 | 180 |
GraphArcLookUpConverter(const Graph& graph, |
181 | 181 |
const std::map<std::string, |
182 | 182 |
typename Graph::Edge>& map) |
183 | 183 |
: _graph(graph), _map(map) {} |
184 | 184 |
|
185 | 185 |
typename Graph::Arc operator()(const std::string& str) { |
186 | 186 |
if (str.empty() || (str[0] != '+' && str[0] != '-')) { |
187 | 187 |
throw DataFormatError("Item must start with '+' or '-'"); |
188 | 188 |
} |
189 | 189 |
typename std::map<std::string, typename Graph::Edge> |
190 | 190 |
::const_iterator it = _map.find(str.substr(1)); |
191 | 191 |
if (it == _map.end()) { |
192 | 192 |
throw DataFormatError("Item not found"); |
193 | 193 |
} |
194 | 194 |
return _graph.direct(it->second, str[0] == '+'); |
195 | 195 |
} |
196 | 196 |
}; |
197 | 197 |
|
198 | 198 |
inline bool isWhiteSpace(char c) { |
199 | 199 |
return c == ' ' || c == '\t' || c == '\v' || |
200 | 200 |
c == '\n' || c == '\r' || c == '\f'; |
201 | 201 |
} |
202 | 202 |
|
203 | 203 |
inline bool isOct(char c) { |
204 | 204 |
return '0' <= c && c <='7'; |
205 | 205 |
} |
206 | 206 |
|
207 | 207 |
inline int valueOct(char c) { |
208 | 208 |
LEMON_ASSERT(isOct(c), "The character is not octal."); |
209 | 209 |
return c - '0'; |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
inline bool isHex(char c) { |
213 | 213 |
return ('0' <= c && c <= '9') || |
214 | 214 |
('a' <= c && c <= 'z') || |
215 | 215 |
('A' <= c && c <= 'Z'); |
216 | 216 |
} |
217 | 217 |
|
218 | 218 |
inline int valueHex(char c) { |
219 | 219 |
LEMON_ASSERT(isHex(c), "The character is not hexadecimal."); |
220 | 220 |
if ('0' <= c && c <= '9') return c - '0'; |
221 | 221 |
if ('a' <= c && c <= 'z') return c - 'a' + 10; |
222 | 222 |
return c - 'A' + 10; |
223 | 223 |
} |
224 | 224 |
|
225 | 225 |
inline bool isIdentifierFirstChar(char c) { |
226 | 226 |
return ('a' <= c && c <= 'z') || |
227 | 227 |
('A' <= c && c <= 'Z') || c == '_'; |
228 | 228 |
} |
229 | 229 |
|
230 | 230 |
inline bool isIdentifierChar(char c) { |
231 | 231 |
return isIdentifierFirstChar(c) || |
232 | 232 |
('0' <= c && c <= '9'); |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
inline char readEscape(std::istream& is) { |
236 | 236 |
char c; |
237 | 237 |
if (!is.get(c)) |
238 | 238 |
throw DataFormatError("Escape format error"); |
239 | 239 |
|
240 | 240 |
switch (c) { |
241 | 241 |
case '\\': |
242 | 242 |
return '\\'; |
243 | 243 |
case '\"': |
244 | 244 |
return '\"'; |
245 | 245 |
case '\'': |
... | ... |
@@ -417,202 +417,202 @@ |
417 | 417 |
/// multiple map objects at the same time. The \c attribute(), \c |
418 | 418 |
/// node() and \c arc() functions are used to add attribute reading |
419 | 419 |
/// rules. |
420 | 420 |
/// |
421 | 421 |
///\code |
422 | 422 |
/// DigraphReader<Digraph>(std::cin, digraph). |
423 | 423 |
/// nodeMap("coordinates", coord_map). |
424 | 424 |
/// arcMap("capacity", cap_map). |
425 | 425 |
/// node("source", src). |
426 | 426 |
/// node("target", trg). |
427 | 427 |
/// attribute("caption", caption). |
428 | 428 |
/// run(); |
429 | 429 |
///\endcode |
430 | 430 |
/// |
431 | 431 |
/// By default the reader uses the first section in the file of the |
432 | 432 |
/// proper type. If a section has an optional name, then it can be |
433 | 433 |
/// selected for reading by giving an optional name parameter to the |
434 | 434 |
/// \c nodes(), \c arcs() or \c attributes() functions. |
435 | 435 |
/// |
436 | 436 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader |
437 | 437 |
/// that the nodes or arcs should not be constructed (added to the |
438 | 438 |
/// graph) during the reading, but instead the label map of the items |
439 | 439 |
/// are given as a parameter of these functions. An |
440 | 440 |
/// application of these functions is multipass reading, which is |
441 | 441 |
/// important if two \c \@arcs sections must be read from the |
442 | 442 |
/// file. In this case the first phase would read the node set and one |
443 | 443 |
/// of the arc sets, while the second phase would read the second arc |
444 | 444 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
445 | 445 |
/// The previously read label node map should be passed to the \c |
446 | 446 |
/// useNodes() functions. Another application of multipass reading when |
447 | 447 |
/// paths are given as a node map or an arc map. |
448 | 448 |
/// It is impossible to read this in |
449 | 449 |
/// a single pass, because the arcs are not constructed when the node |
450 | 450 |
/// maps are read. |
451 | 451 |
template <typename _Digraph> |
452 | 452 |
class DigraphReader { |
453 | 453 |
public: |
454 | 454 |
|
455 | 455 |
typedef _Digraph Digraph; |
456 | 456 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
457 | 457 |
|
458 | 458 |
private: |
459 | 459 |
|
460 | 460 |
|
461 | 461 |
std::istream* _is; |
462 | 462 |
bool local_is; |
463 | 463 |
|
464 | 464 |
Digraph& _digraph; |
465 | 465 |
|
466 | 466 |
std::string _nodes_caption; |
467 | 467 |
std::string _arcs_caption; |
468 | 468 |
std::string _attributes_caption; |
469 | 469 |
|
470 | 470 |
typedef std::map<std::string, Node> NodeIndex; |
471 | 471 |
NodeIndex _node_index; |
472 | 472 |
typedef std::map<std::string, Arc> ArcIndex; |
473 | 473 |
ArcIndex _arc_index; |
474 | 474 |
|
475 | 475 |
typedef std::vector<std::pair<std::string, |
476 | 476 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
477 | 477 |
NodeMaps _node_maps; |
478 | 478 |
|
479 | 479 |
typedef std::vector<std::pair<std::string, |
480 | 480 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
481 | 481 |
ArcMaps _arc_maps; |
482 | 482 |
|
483 | 483 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
484 | 484 |
Attributes; |
485 | 485 |
Attributes _attributes; |
486 | 486 |
|
487 | 487 |
bool _use_nodes; |
488 | 488 |
bool _use_arcs; |
489 | 489 |
|
490 | 490 |
bool _skip_nodes; |
491 | 491 |
bool _skip_arcs; |
492 | 492 |
|
493 | 493 |
int line_num; |
494 | 494 |
std::istringstream line; |
495 | 495 |
|
496 | 496 |
public: |
497 | 497 |
|
498 | 498 |
/// \brief Constructor |
499 | 499 |
/// |
500 | 500 |
/// Construct a directed graph reader, which reads from the given |
501 | 501 |
/// input stream. |
502 | 502 |
DigraphReader(std::istream& is, Digraph& digraph) |
503 | 503 |
: _is(&is), local_is(false), _digraph(digraph), |
504 | 504 |
_use_nodes(false), _use_arcs(false), |
505 | 505 |
_skip_nodes(false), _skip_arcs(false) {} |
506 | 506 |
|
507 | 507 |
/// \brief Constructor |
508 | 508 |
/// |
509 | 509 |
/// Construct a directed graph reader, which reads from the given |
510 | 510 |
/// file. |
511 | 511 |
DigraphReader(const std::string& fn, Digraph& digraph) |
512 | 512 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph), |
513 |
|
|
513 |
_use_nodes(false), _use_arcs(false), |
|
514 | 514 |
_skip_nodes(false), _skip_arcs(false) {} |
515 | 515 |
|
516 | 516 |
/// \brief Constructor |
517 | 517 |
/// |
518 | 518 |
/// Construct a directed graph reader, which reads from the given |
519 | 519 |
/// file. |
520 | 520 |
DigraphReader(const char* fn, Digraph& digraph) |
521 | 521 |
: _is(new std::ifstream(fn)), local_is(true), _digraph(digraph), |
522 |
|
|
522 |
_use_nodes(false), _use_arcs(false), |
|
523 | 523 |
_skip_nodes(false), _skip_arcs(false) {} |
524 | 524 |
|
525 | 525 |
/// \brief Destructor |
526 | 526 |
~DigraphReader() { |
527 | 527 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
528 | 528 |
it != _node_maps.end(); ++it) { |
529 | 529 |
delete it->second; |
530 | 530 |
} |
531 | 531 |
|
532 | 532 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
533 | 533 |
it != _arc_maps.end(); ++it) { |
534 | 534 |
delete it->second; |
535 | 535 |
} |
536 | 536 |
|
537 | 537 |
for (typename Attributes::iterator it = _attributes.begin(); |
538 | 538 |
it != _attributes.end(); ++it) { |
539 | 539 |
delete it->second; |
540 | 540 |
} |
541 | 541 |
|
542 | 542 |
if (local_is) { |
543 | 543 |
delete _is; |
544 | 544 |
} |
545 | 545 |
|
546 | 546 |
} |
547 | 547 |
|
548 | 548 |
private: |
549 | 549 |
|
550 | 550 |
friend DigraphReader<Digraph> digraphReader<>(std::istream& is, |
551 | 551 |
Digraph& digraph); |
552 | 552 |
friend DigraphReader<Digraph> digraphReader<>(const std::string& fn, |
553 | 553 |
Digraph& digraph); |
554 | 554 |
friend DigraphReader<Digraph> digraphReader<>(const char *fn, |
555 | 555 |
Digraph& digraph); |
556 | 556 |
|
557 | 557 |
DigraphReader(DigraphReader& other) |
558 | 558 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
559 | 559 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
560 | 560 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { |
561 | 561 |
|
562 | 562 |
other._is = 0; |
563 | 563 |
other.local_is = false; |
564 | 564 |
|
565 | 565 |
_node_index.swap(other._node_index); |
566 | 566 |
_arc_index.swap(other._arc_index); |
567 | 567 |
|
568 | 568 |
_node_maps.swap(other._node_maps); |
569 | 569 |
_arc_maps.swap(other._arc_maps); |
570 | 570 |
_attributes.swap(other._attributes); |
571 | 571 |
|
572 | 572 |
_nodes_caption = other._nodes_caption; |
573 | 573 |
_arcs_caption = other._arcs_caption; |
574 | 574 |
_attributes_caption = other._attributes_caption; |
575 | 575 |
|
576 | 576 |
} |
577 | 577 |
|
578 | 578 |
DigraphReader& operator=(const DigraphReader&); |
579 | 579 |
|
580 | 580 |
public: |
581 | 581 |
|
582 | 582 |
/// \name Reading rules |
583 | 583 |
/// @{ |
584 | 584 |
|
585 | 585 |
/// \brief Node map reading rule |
586 | 586 |
/// |
587 | 587 |
/// Add a node map reading rule to the reader. |
588 | 588 |
template <typename Map> |
589 | 589 |
DigraphReader& nodeMap(const std::string& caption, Map& map) { |
590 | 590 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
591 | 591 |
_reader_bits::MapStorageBase<Node>* storage = |
592 | 592 |
new _reader_bits::MapStorage<Node, Map>(map); |
593 | 593 |
_node_maps.push_back(std::make_pair(caption, storage)); |
594 | 594 |
return *this; |
595 | 595 |
} |
596 | 596 |
|
597 | 597 |
/// \brief Node map reading rule |
598 | 598 |
/// |
599 | 599 |
/// Add a node map reading rule with specialized converter to the |
600 | 600 |
/// reader. |
601 | 601 |
template <typename Map, typename Converter> |
602 | 602 |
DigraphReader& nodeMap(const std::string& caption, Map& map, |
603 | 603 |
const Converter& converter = Converter()) { |
604 | 604 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
605 | 605 |
_reader_bits::MapStorageBase<Node>* storage = |
606 | 606 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
607 | 607 |
_node_maps.push_back(std::make_pair(caption, storage)); |
608 | 608 |
return *this; |
609 | 609 |
} |
610 | 610 |
|
611 | 611 |
/// \brief Arc map reading rule |
612 | 612 |
/// |
613 | 613 |
/// Add an arc map reading rule to the reader. |
614 | 614 |
template <typename Map> |
615 | 615 |
DigraphReader& arcMap(const std::string& caption, Map& map) { |
616 | 616 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
617 | 617 |
_reader_bits::MapStorageBase<Arc>* storage = |
618 | 618 |
new _reader_bits::MapStorage<Arc, Map>(map); |
... | ... |
@@ -1201,202 +1201,202 @@ |
1201 | 1201 |
/// This function just returns a \ref DigraphReader class. |
1202 | 1202 |
/// \relates DigraphReader |
1203 | 1203 |
template <typename Digraph> |
1204 | 1204 |
DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) { |
1205 | 1205 |
DigraphReader<Digraph> tmp(fn, digraph); |
1206 | 1206 |
return tmp; |
1207 | 1207 |
} |
1208 | 1208 |
|
1209 | 1209 |
template <typename Graph> |
1210 | 1210 |
class GraphReader; |
1211 | 1211 |
|
1212 | 1212 |
template <typename Graph> |
1213 | 1213 |
GraphReader<Graph> graphReader(std::istream& is, Graph& graph); |
1214 | 1214 |
|
1215 | 1215 |
template <typename Graph> |
1216 | 1216 |
GraphReader<Graph> graphReader(const std::string& fn, Graph& graph); |
1217 | 1217 |
|
1218 | 1218 |
template <typename Graph> |
1219 | 1219 |
GraphReader<Graph> graphReader(const char *fn, Graph& graph); |
1220 | 1220 |
|
1221 | 1221 |
/// \ingroup lemon_io |
1222 | 1222 |
/// |
1223 | 1223 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs |
1224 | 1224 |
/// |
1225 | 1225 |
/// This utility reads an \ref lgf-format "LGF" file. |
1226 | 1226 |
/// |
1227 | 1227 |
/// It can be used almost the same way as \c DigraphReader. |
1228 | 1228 |
/// The only difference is that this class can handle edges and |
1229 | 1229 |
/// edge maps as well as arcs and arc maps. |
1230 | 1230 |
/// |
1231 | 1231 |
/// The columns in the \c \@edges (or \c \@arcs) section are the |
1232 | 1232 |
/// edge maps. However, if there are two maps with the same name |
1233 | 1233 |
/// prefixed with \c '+' and \c '-', then these can be read into an |
1234 | 1234 |
/// arc map. Similarly, an attribute can be read into an arc, if |
1235 | 1235 |
/// it's value is an edge label prefixed with \c '+' or \c '-'. |
1236 | 1236 |
template <typename _Graph> |
1237 | 1237 |
class GraphReader { |
1238 | 1238 |
public: |
1239 | 1239 |
|
1240 | 1240 |
typedef _Graph Graph; |
1241 | 1241 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
1242 | 1242 |
|
1243 | 1243 |
private: |
1244 | 1244 |
|
1245 | 1245 |
std::istream* _is; |
1246 | 1246 |
bool local_is; |
1247 | 1247 |
|
1248 | 1248 |
Graph& _graph; |
1249 | 1249 |
|
1250 | 1250 |
std::string _nodes_caption; |
1251 | 1251 |
std::string _edges_caption; |
1252 | 1252 |
std::string _attributes_caption; |
1253 | 1253 |
|
1254 | 1254 |
typedef std::map<std::string, Node> NodeIndex; |
1255 | 1255 |
NodeIndex _node_index; |
1256 | 1256 |
typedef std::map<std::string, Edge> EdgeIndex; |
1257 | 1257 |
EdgeIndex _edge_index; |
1258 | 1258 |
|
1259 | 1259 |
typedef std::vector<std::pair<std::string, |
1260 | 1260 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
1261 | 1261 |
NodeMaps _node_maps; |
1262 | 1262 |
|
1263 | 1263 |
typedef std::vector<std::pair<std::string, |
1264 | 1264 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
1265 | 1265 |
EdgeMaps _edge_maps; |
1266 | 1266 |
|
1267 | 1267 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
1268 | 1268 |
Attributes; |
1269 | 1269 |
Attributes _attributes; |
1270 | 1270 |
|
1271 | 1271 |
bool _use_nodes; |
1272 | 1272 |
bool _use_edges; |
1273 | 1273 |
|
1274 | 1274 |
bool _skip_nodes; |
1275 | 1275 |
bool _skip_edges; |
1276 | 1276 |
|
1277 | 1277 |
int line_num; |
1278 | 1278 |
std::istringstream line; |
1279 | 1279 |
|
1280 | 1280 |
public: |
1281 | 1281 |
|
1282 | 1282 |
/// \brief Constructor |
1283 | 1283 |
/// |
1284 | 1284 |
/// Construct an undirected graph reader, which reads from the given |
1285 | 1285 |
/// input stream. |
1286 | 1286 |
GraphReader(std::istream& is, Graph& graph) |
1287 | 1287 |
: _is(&is), local_is(false), _graph(graph), |
1288 | 1288 |
_use_nodes(false), _use_edges(false), |
1289 | 1289 |
_skip_nodes(false), _skip_edges(false) {} |
1290 | 1290 |
|
1291 | 1291 |
/// \brief Constructor |
1292 | 1292 |
/// |
1293 | 1293 |
/// Construct an undirected graph reader, which reads from the given |
1294 | 1294 |
/// file. |
1295 | 1295 |
GraphReader(const std::string& fn, Graph& graph) |
1296 | 1296 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph), |
1297 |
|
|
1297 |
_use_nodes(false), _use_edges(false), |
|
1298 | 1298 |
_skip_nodes(false), _skip_edges(false) {} |
1299 | 1299 |
|
1300 | 1300 |
/// \brief Constructor |
1301 | 1301 |
/// |
1302 | 1302 |
/// Construct an undirected graph reader, which reads from the given |
1303 | 1303 |
/// file. |
1304 | 1304 |
GraphReader(const char* fn, Graph& graph) |
1305 | 1305 |
: _is(new std::ifstream(fn)), local_is(true), _graph(graph), |
1306 |
|
|
1306 |
_use_nodes(false), _use_edges(false), |
|
1307 | 1307 |
_skip_nodes(false), _skip_edges(false) {} |
1308 | 1308 |
|
1309 | 1309 |
/// \brief Destructor |
1310 | 1310 |
~GraphReader() { |
1311 | 1311 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
1312 | 1312 |
it != _node_maps.end(); ++it) { |
1313 | 1313 |
delete it->second; |
1314 | 1314 |
} |
1315 | 1315 |
|
1316 | 1316 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
1317 | 1317 |
it != _edge_maps.end(); ++it) { |
1318 | 1318 |
delete it->second; |
1319 | 1319 |
} |
1320 | 1320 |
|
1321 | 1321 |
for (typename Attributes::iterator it = _attributes.begin(); |
1322 | 1322 |
it != _attributes.end(); ++it) { |
1323 | 1323 |
delete it->second; |
1324 | 1324 |
} |
1325 | 1325 |
|
1326 | 1326 |
if (local_is) { |
1327 | 1327 |
delete _is; |
1328 | 1328 |
} |
1329 | 1329 |
|
1330 | 1330 |
} |
1331 | 1331 |
|
1332 | 1332 |
private: |
1333 | 1333 |
friend GraphReader<Graph> graphReader<>(std::istream& is, Graph& graph); |
1334 | 1334 |
friend GraphReader<Graph> graphReader<>(const std::string& fn, |
1335 | 1335 |
Graph& graph); |
1336 | 1336 |
friend GraphReader<Graph> graphReader<>(const char *fn, Graph& graph); |
1337 | 1337 |
|
1338 | 1338 |
GraphReader(GraphReader& other) |
1339 | 1339 |
: _is(other._is), local_is(other.local_is), _graph(other._graph), |
1340 | 1340 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges), |
1341 | 1341 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
1342 | 1342 |
|
1343 | 1343 |
other._is = 0; |
1344 | 1344 |
other.local_is = false; |
1345 | 1345 |
|
1346 | 1346 |
_node_index.swap(other._node_index); |
1347 | 1347 |
_edge_index.swap(other._edge_index); |
1348 | 1348 |
|
1349 | 1349 |
_node_maps.swap(other._node_maps); |
1350 | 1350 |
_edge_maps.swap(other._edge_maps); |
1351 | 1351 |
_attributes.swap(other._attributes); |
1352 | 1352 |
|
1353 | 1353 |
_nodes_caption = other._nodes_caption; |
1354 | 1354 |
_edges_caption = other._edges_caption; |
1355 | 1355 |
_attributes_caption = other._attributes_caption; |
1356 | 1356 |
|
1357 | 1357 |
} |
1358 | 1358 |
|
1359 | 1359 |
GraphReader& operator=(const GraphReader&); |
1360 | 1360 |
|
1361 | 1361 |
public: |
1362 | 1362 |
|
1363 | 1363 |
/// \name Reading rules |
1364 | 1364 |
/// @{ |
1365 | 1365 |
|
1366 | 1366 |
/// \brief Node map reading rule |
1367 | 1367 |
/// |
1368 | 1368 |
/// Add a node map reading rule to the reader. |
1369 | 1369 |
template <typename Map> |
1370 | 1370 |
GraphReader& nodeMap(const std::string& caption, Map& map) { |
1371 | 1371 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
1372 | 1372 |
_reader_bits::MapStorageBase<Node>* storage = |
1373 | 1373 |
new _reader_bits::MapStorage<Node, Map>(map); |
1374 | 1374 |
_node_maps.push_back(std::make_pair(caption, storage)); |
1375 | 1375 |
return *this; |
1376 | 1376 |
} |
1377 | 1377 |
|
1378 | 1378 |
/// \brief Node map reading rule |
1379 | 1379 |
/// |
1380 | 1380 |
/// Add a node map reading rule with specialized converter to the |
1381 | 1381 |
/// reader. |
1382 | 1382 |
template <typename Map, typename Converter> |
1383 | 1383 |
GraphReader& nodeMap(const std::string& caption, Map& map, |
1384 | 1384 |
const Converter& converter = Converter()) { |
1385 | 1385 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
1386 | 1386 |
_reader_bits::MapStorageBase<Node>* storage = |
1387 | 1387 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
1388 | 1388 |
_node_maps.push_back(std::make_pair(caption, storage)); |
1389 | 1389 |
return *this; |
1390 | 1390 |
} |
1391 | 1391 |
|
1392 | 1392 |
/// \brief Edge map reading rule |
1393 | 1393 |
/// |
1394 | 1394 |
/// Add an edge map reading rule to the reader. |
1395 | 1395 |
template <typename Map> |
1396 | 1396 |
GraphReader& edgeMap(const std::string& caption, Map& map) { |
1397 | 1397 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
1398 | 1398 |
_reader_bits::MapStorageBase<Edge>* storage = |
1399 | 1399 |
new _reader_bits::MapStorage<Edge, Map>(map); |
1400 | 1400 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
1401 | 1401 |
return *this; |
1402 | 1402 |
} |
... | ... |
@@ -88,193 +88,193 @@ |
88 | 88 |
|
89 | 89 |
bool operator()(const Item& left, const Item& right) { |
90 | 90 |
return _map[_graph.direct(left, _dir)] < |
91 | 91 |
_map[_graph.direct(right, _dir)]; |
92 | 92 |
} |
93 | 93 |
}; |
94 | 94 |
|
95 | 95 |
template <typename _Item> |
96 | 96 |
class MapStorageBase { |
97 | 97 |
public: |
98 | 98 |
typedef _Item Item; |
99 | 99 |
|
100 | 100 |
public: |
101 | 101 |
MapStorageBase() {} |
102 | 102 |
virtual ~MapStorageBase() {} |
103 | 103 |
|
104 | 104 |
virtual std::string get(const Item& item) = 0; |
105 | 105 |
virtual void sort(std::vector<Item>&) = 0; |
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename _Item, typename _Map, |
109 | 109 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
110 | 110 |
class MapStorage : public MapStorageBase<_Item> { |
111 | 111 |
public: |
112 | 112 |
typedef _Map Map; |
113 | 113 |
typedef _Converter Converter; |
114 | 114 |
typedef _Item Item; |
115 | 115 |
|
116 | 116 |
private: |
117 | 117 |
const Map& _map; |
118 | 118 |
Converter _converter; |
119 | 119 |
|
120 | 120 |
public: |
121 | 121 |
MapStorage(const Map& map, const Converter& converter = Converter()) |
122 | 122 |
: _map(map), _converter(converter) {} |
123 | 123 |
virtual ~MapStorage() {} |
124 | 124 |
|
125 | 125 |
virtual std::string get(const Item& item) { |
126 | 126 |
return _converter(_map[item]); |
127 | 127 |
} |
128 | 128 |
virtual void sort(std::vector<Item>& items) { |
129 | 129 |
MapLess<Map> less(_map); |
130 | 130 |
std::sort(items.begin(), items.end(), less); |
131 | 131 |
} |
132 | 132 |
}; |
133 | 133 |
|
134 | 134 |
template <typename _Graph, bool _dir, typename _Map, |
135 | 135 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
136 | 136 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> { |
137 | 137 |
public: |
138 | 138 |
typedef _Map Map; |
139 | 139 |
typedef _Converter Converter; |
140 | 140 |
typedef _Graph Graph; |
141 | 141 |
typedef typename Graph::Edge Item; |
142 | 142 |
static const bool dir = _dir; |
143 | 143 |
|
144 | 144 |
private: |
145 | 145 |
const Graph& _graph; |
146 | 146 |
const Map& _map; |
147 | 147 |
Converter _converter; |
148 | 148 |
|
149 | 149 |
public: |
150 | 150 |
GraphArcMapStorage(const Graph& graph, const Map& map, |
151 | 151 |
const Converter& converter = Converter()) |
152 | 152 |
: _graph(graph), _map(map), _converter(converter) {} |
153 | 153 |
virtual ~GraphArcMapStorage() {} |
154 | 154 |
|
155 | 155 |
virtual std::string get(const Item& item) { |
156 | 156 |
return _converter(_map[_graph.direct(item, dir)]); |
157 | 157 |
} |
158 | 158 |
virtual void sort(std::vector<Item>& items) { |
159 | 159 |
GraphArcMapLess<Graph, dir, Map> less(_graph, _map); |
160 | 160 |
std::sort(items.begin(), items.end(), less); |
161 | 161 |
} |
162 | 162 |
}; |
163 | 163 |
|
164 | 164 |
class ValueStorageBase { |
165 | 165 |
public: |
166 | 166 |
ValueStorageBase() {} |
167 | 167 |
virtual ~ValueStorageBase() {} |
168 | 168 |
|
169 | 169 |
virtual std::string get() = 0; |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
173 | 173 |
class ValueStorage : public ValueStorageBase { |
174 | 174 |
public: |
175 | 175 |
typedef _Value Value; |
176 | 176 |
typedef _Converter Converter; |
177 | 177 |
|
178 | 178 |
private: |
179 | 179 |
const Value& _value; |
180 | 180 |
Converter _converter; |
181 | 181 |
|
182 | 182 |
public: |
183 | 183 |
ValueStorage(const Value& value, const Converter& converter = Converter()) |
184 |
|
|
184 |
: _value(value), _converter(converter) {} |
|
185 | 185 |
|
186 | 186 |
virtual std::string get() { |
187 | 187 |
return _converter(_value); |
188 | 188 |
} |
189 | 189 |
}; |
190 | 190 |
|
191 | 191 |
template <typename Value> |
192 | 192 |
struct MapLookUpConverter { |
193 | 193 |
const std::map<Value, std::string>& _map; |
194 | 194 |
|
195 | 195 |
MapLookUpConverter(const std::map<Value, std::string>& map) |
196 | 196 |
: _map(map) {} |
197 | 197 |
|
198 | 198 |
std::string operator()(const Value& str) { |
199 | 199 |
typename std::map<Value, std::string>::const_iterator it = |
200 | 200 |
_map.find(str); |
201 | 201 |
if (it == _map.end()) { |
202 | 202 |
throw DataFormatError("Item not found"); |
203 | 203 |
} |
204 | 204 |
return it->second; |
205 | 205 |
} |
206 | 206 |
}; |
207 | 207 |
|
208 | 208 |
template <typename Graph> |
209 | 209 |
struct GraphArcLookUpConverter { |
210 | 210 |
const Graph& _graph; |
211 | 211 |
const std::map<typename Graph::Edge, std::string>& _map; |
212 | 212 |
|
213 | 213 |
GraphArcLookUpConverter(const Graph& graph, |
214 | 214 |
const std::map<typename Graph::Edge, |
215 | 215 |
std::string>& map) |
216 | 216 |
: _graph(graph), _map(map) {} |
217 | 217 |
|
218 | 218 |
std::string operator()(const typename Graph::Arc& val) { |
219 | 219 |
typename std::map<typename Graph::Edge, std::string> |
220 | 220 |
::const_iterator it = _map.find(val); |
221 | 221 |
if (it == _map.end()) { |
222 | 222 |
throw DataFormatError("Item not found"); |
223 | 223 |
} |
224 | 224 |
return (_graph.direction(val) ? '+' : '-') + it->second; |
225 | 225 |
} |
226 | 226 |
}; |
227 | 227 |
|
228 | 228 |
inline bool isWhiteSpace(char c) { |
229 | 229 |
return c == ' ' || c == '\t' || c == '\v' || |
230 | 230 |
c == '\n' || c == '\r' || c == '\f'; |
231 | 231 |
} |
232 | 232 |
|
233 | 233 |
inline bool isEscaped(char c) { |
234 | 234 |
return c == '\\' || c == '\"' || c == '\'' || |
235 | 235 |
c == '\a' || c == '\b'; |
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
inline static void writeEscape(std::ostream& os, char c) { |
239 | 239 |
switch (c) { |
240 | 240 |
case '\\': |
241 | 241 |
os << "\\\\"; |
242 | 242 |
return; |
243 | 243 |
case '\"': |
244 | 244 |
os << "\\\""; |
245 | 245 |
return; |
246 | 246 |
case '\a': |
247 | 247 |
os << "\\a"; |
248 | 248 |
return; |
249 | 249 |
case '\b': |
250 | 250 |
os << "\\b"; |
251 | 251 |
return; |
252 | 252 |
case '\f': |
253 | 253 |
os << "\\f"; |
254 | 254 |
return; |
255 | 255 |
case '\r': |
256 | 256 |
os << "\\r"; |
257 | 257 |
return; |
258 | 258 |
case '\n': |
259 | 259 |
os << "\\n"; |
260 | 260 |
return; |
261 | 261 |
case '\t': |
262 | 262 |
os << "\\t"; |
263 | 263 |
return; |
264 | 264 |
case '\v': |
265 | 265 |
os << "\\v"; |
266 | 266 |
return; |
267 | 267 |
default: |
268 | 268 |
if (c < 0x20) { |
269 | 269 |
std::ios::fmtflags flags = os.flags(); |
270 | 270 |
os << '\\' << std::oct << static_cast<int>(c); |
271 | 271 |
os.flags(flags); |
272 | 272 |
} else { |
273 | 273 |
os << c; |
274 | 274 |
} |
275 | 275 |
return; |
276 | 276 |
} |
277 | 277 |
} |
278 | 278 |
|
279 | 279 |
inline bool requireEscape(const std::string& str) { |
280 | 280 |
if (str.empty() || str[0] == '@') return true; |
... | ... |
@@ -401,193 +401,193 @@ |
401 | 401 |
///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s referencing |
402 | 402 |
///the changed arc remain valid. However <tt>OutArcIt</tt>s are |
403 | 403 |
///invalidated. |
404 | 404 |
/// |
405 | 405 |
///\warning This functionality cannot be used together with the Snapshot |
406 | 406 |
///feature. |
407 | 407 |
void changeSource(Arc e, Node n) { |
408 | 408 |
Parent::changeSource(e,n); |
409 | 409 |
} |
410 | 410 |
|
411 | 411 |
/// Invert the direction of an arc. |
412 | 412 |
|
413 | 413 |
///\note The <tt>ArcIt</tt>s referencing the changed arc remain |
414 | 414 |
///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are |
415 | 415 |
///invalidated. |
416 | 416 |
/// |
417 | 417 |
///\warning This functionality cannot be used together with the Snapshot |
418 | 418 |
///feature. |
419 | 419 |
void reverseArc(Arc e) { |
420 | 420 |
Node t=target(e); |
421 | 421 |
changeTarget(e,source(e)); |
422 | 422 |
changeSource(e,t); |
423 | 423 |
} |
424 | 424 |
|
425 | 425 |
/// Reserve memory for nodes. |
426 | 426 |
|
427 | 427 |
/// Using this function it is possible to avoid the superfluous memory |
428 | 428 |
/// allocation: if you know that the digraph you want to build will |
429 | 429 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
430 | 430 |
/// then it is worth reserving space for this amount before starting |
431 | 431 |
/// to build the digraph. |
432 | 432 |
/// \sa reserveArc |
433 | 433 |
void reserveNode(int n) { nodes.reserve(n); }; |
434 | 434 |
|
435 | 435 |
/// Reserve memory for arcs. |
436 | 436 |
|
437 | 437 |
/// Using this function it is possible to avoid the superfluous memory |
438 | 438 |
/// allocation: if you know that the digraph you want to build will |
439 | 439 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
440 | 440 |
/// then it is worth reserving space for this amount before starting |
441 | 441 |
/// to build the digraph. |
442 | 442 |
/// \sa reserveNode |
443 | 443 |
void reserveArc(int m) { arcs.reserve(m); }; |
444 | 444 |
|
445 | 445 |
///Contract two nodes. |
446 | 446 |
|
447 | 447 |
///This function contracts two nodes. |
448 | 448 |
///Node \p b will be removed but instead of deleting |
449 | 449 |
///incident arcs, they will be joined to \p a. |
450 | 450 |
///The last parameter \p r controls whether to remove loops. \c true |
451 | 451 |
///means that loops will be removed. |
452 | 452 |
/// |
453 | 453 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
454 | 454 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s |
455 | 455 |
///may be invalidated. |
456 | 456 |
/// |
457 | 457 |
///\warning This functionality cannot be used together with the Snapshot |
458 | 458 |
///feature. |
459 | 459 |
void contract(Node a, Node b, bool r = true) |
460 | 460 |
{ |
461 | 461 |
for(OutArcIt e(*this,b);e!=INVALID;) { |
462 | 462 |
OutArcIt f=e; |
463 | 463 |
++f; |
464 | 464 |
if(r && target(e)==a) erase(e); |
465 | 465 |
else changeSource(e,a); |
466 | 466 |
e=f; |
467 | 467 |
} |
468 | 468 |
for(InArcIt e(*this,b);e!=INVALID;) { |
469 | 469 |
InArcIt f=e; |
470 | 470 |
++f; |
471 | 471 |
if(r && source(e)==a) erase(e); |
472 | 472 |
else changeTarget(e,a); |
473 | 473 |
e=f; |
474 | 474 |
} |
475 | 475 |
erase(b); |
476 | 476 |
} |
477 | 477 |
|
478 | 478 |
///Split a node. |
479 | 479 |
|
480 | 480 |
///This function splits a node. First a new node is added to the digraph, |
481 | 481 |
///then the source of each outgoing arc of \c n is moved to this new node. |
482 | 482 |
///If \c connect is \c true (this is the default value), then a new arc |
483 | 483 |
///from \c n to the newly created node is also added. |
484 | 484 |
///\return The newly created node. |
485 | 485 |
/// |
486 | 486 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
487 | 487 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may |
488 | 488 |
///be invalidated. |
489 | 489 |
/// |
490 | 490 |
///\warning This functionality cannot be used together with the |
491 | 491 |
///Snapshot feature. |
492 | 492 |
/// |
493 | 493 |
///\todo It could be implemented in a bit faster way. |
494 | 494 |
Node split(Node n, bool connect = true) { |
495 | 495 |
Node b = addNode(); |
496 | 496 |
for(OutArcIt e(*this,n);e!=INVALID;) { |
497 |
|
|
497 |
OutArcIt f=e; |
|
498 | 498 |
++f; |
499 | 499 |
changeSource(e,b); |
500 | 500 |
e=f; |
501 | 501 |
} |
502 | 502 |
if (connect) addArc(n,b); |
503 | 503 |
return b; |
504 | 504 |
} |
505 | 505 |
|
506 | 506 |
///Split an arc. |
507 | 507 |
|
508 | 508 |
///This function splits an arc. First a new node \c b is added to |
509 | 509 |
///the digraph, then the original arc is re-targeted to \c |
510 | 510 |
///b. Finally an arc from \c b to the original target is added. |
511 | 511 |
/// |
512 | 512 |
///\return The newly created node. |
513 | 513 |
/// |
514 | 514 |
///\warning This functionality cannot be used together with the |
515 | 515 |
///Snapshot feature. |
516 | 516 |
Node split(Arc e) { |
517 | 517 |
Node b = addNode(); |
518 | 518 |
addArc(b,target(e)); |
519 | 519 |
changeTarget(e,b); |
520 | 520 |
return b; |
521 | 521 |
} |
522 | 522 |
|
523 | 523 |
/// \brief Class to make a snapshot of the digraph and restore |
524 | 524 |
/// it later. |
525 | 525 |
/// |
526 | 526 |
/// Class to make a snapshot of the digraph and restore it later. |
527 | 527 |
/// |
528 | 528 |
/// The newly added nodes and arcs can be removed using the |
529 | 529 |
/// restore() function. |
530 | 530 |
/// |
531 | 531 |
/// \warning Arc and node deletions and other modifications (e.g. |
532 | 532 |
/// contracting, splitting, reversing arcs or nodes) cannot be |
533 | 533 |
/// restored. These events invalidate the snapshot. |
534 | 534 |
class Snapshot { |
535 | 535 |
protected: |
536 | 536 |
|
537 | 537 |
typedef Parent::NodeNotifier NodeNotifier; |
538 | 538 |
|
539 | 539 |
class NodeObserverProxy : public NodeNotifier::ObserverBase { |
540 | 540 |
public: |
541 | 541 |
|
542 | 542 |
NodeObserverProxy(Snapshot& _snapshot) |
543 | 543 |
: snapshot(_snapshot) {} |
544 | 544 |
|
545 | 545 |
using NodeNotifier::ObserverBase::attach; |
546 | 546 |
using NodeNotifier::ObserverBase::detach; |
547 | 547 |
using NodeNotifier::ObserverBase::attached; |
548 | 548 |
|
549 | 549 |
protected: |
550 | 550 |
|
551 | 551 |
virtual void add(const Node& node) { |
552 | 552 |
snapshot.addNode(node); |
553 | 553 |
} |
554 | 554 |
virtual void add(const std::vector<Node>& nodes) { |
555 | 555 |
for (int i = nodes.size() - 1; i >= 0; ++i) { |
556 | 556 |
snapshot.addNode(nodes[i]); |
557 | 557 |
} |
558 | 558 |
} |
559 | 559 |
virtual void erase(const Node& node) { |
560 | 560 |
snapshot.eraseNode(node); |
561 | 561 |
} |
562 | 562 |
virtual void erase(const std::vector<Node>& nodes) { |
563 | 563 |
for (int i = 0; i < int(nodes.size()); ++i) { |
564 | 564 |
snapshot.eraseNode(nodes[i]); |
565 | 565 |
} |
566 | 566 |
} |
567 | 567 |
virtual void build() { |
568 | 568 |
Node node; |
569 | 569 |
std::vector<Node> nodes; |
570 | 570 |
for (notifier()->first(node); node != INVALID; |
571 | 571 |
notifier()->next(node)) { |
572 | 572 |
nodes.push_back(node); |
573 | 573 |
} |
574 | 574 |
for (int i = nodes.size() - 1; i >= 0; --i) { |
575 | 575 |
snapshot.addNode(nodes[i]); |
576 | 576 |
} |
577 | 577 |
} |
578 | 578 |
virtual void clear() { |
579 | 579 |
Node node; |
580 | 580 |
for (notifier()->first(node); node != INVALID; |
581 | 581 |
notifier()->next(node)) { |
582 | 582 |
snapshot.eraseNode(node); |
583 | 583 |
} |
584 | 584 |
} |
585 | 585 |
|
586 | 586 |
Snapshot& snapshot; |
587 | 587 |
}; |
588 | 588 |
|
589 | 589 |
class ArcObserverProxy : public ArcNotifier::ObserverBase { |
590 | 590 |
public: |
591 | 591 |
|
592 | 592 |
ArcObserverProxy(Snapshot& _snapshot) |
593 | 593 |
: snapshot(_snapshot) {} |
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 <iostream> |
20 | 20 |
#include <fstream> |
21 | 21 |
#include <string> |
22 | 22 |
#include <vector> |
23 | 23 |
|
24 | 24 |
#include <lemon/concept_check.h> |
25 | 25 |
#include <lemon/concepts/heap.h> |
26 | 26 |
|
27 | 27 |
#include <lemon/smart_graph.h> |
28 | 28 |
|
29 | 29 |
#include <lemon/lgf_reader.h> |
30 | 30 |
#include <lemon/dijkstra.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/bin_heap.h> |
34 | 34 |
|
35 | 35 |
#include "test_tools.h" |
36 | 36 |
|
37 | 37 |
using namespace lemon; |
38 | 38 |
using namespace lemon::concepts; |
39 | 39 |
|
40 | 40 |
typedef ListDigraph Digraph; |
41 | 41 |
DIGRAPH_TYPEDEFS(Digraph); |
42 | 42 |
|
43 | 43 |
char test_lgf[] = |
44 | 44 |
"@nodes\n" |
45 | 45 |
"label\n" |
46 | 46 |
"0\n" |
47 | 47 |
"1\n" |
48 | 48 |
"2\n" |
49 | 49 |
"3\n" |
50 | 50 |
"4\n" |
51 | 51 |
"5\n" |
52 | 52 |
"6\n" |
53 | 53 |
"7\n" |
54 | 54 |
"8\n" |
55 | 55 |
"9\n" |
56 | 56 |
"@arcs\n" |
57 |
" label capacity\n" |
|
58 |
"0 5 0 94\n" |
|
59 |
"3 9 1 11\n" |
|
60 |
"8 7 2 83\n" |
|
61 |
"1 2 3 94\n" |
|
62 |
"5 7 4 35\n" |
|
63 |
"7 4 5 84\n" |
|
64 |
"9 5 6 38\n" |
|
65 |
"0 4 7 96\n" |
|
66 |
"6 7 8 6\n" |
|
67 |
"3 1 9 27\n" |
|
68 |
"5 2 10 77\n" |
|
69 |
"5 6 11 69\n" |
|
70 |
"6 5 12 41\n" |
|
71 |
"4 6 13 70\n" |
|
72 |
"3 2 14 45\n" |
|
73 |
"7 9 15 93\n" |
|
74 |
"5 9 16 50\n" |
|
75 |
"9 0 17 94\n" |
|
76 |
"9 6 18 67\n" |
|
77 |
" |
|
57 |
" label capacity\n" |
|
58 |
"0 5 0 94\n" |
|
59 |
"3 9 1 11\n" |
|
60 |
"8 7 2 83\n" |
|
61 |
"1 2 3 94\n" |
|
62 |
"5 7 4 35\n" |
|
63 |
"7 4 5 84\n" |
|
64 |
"9 5 6 38\n" |
|
65 |
"0 4 7 96\n" |
|
66 |
"6 7 8 6\n" |
|
67 |
"3 1 9 27\n" |
|
68 |
"5 2 10 77\n" |
|
69 |
"5 6 11 69\n" |
|
70 |
"6 5 12 41\n" |
|
71 |
"4 6 13 70\n" |
|
72 |
"3 2 14 45\n" |
|
73 |
"7 9 15 93\n" |
|
74 |
"5 9 16 50\n" |
|
75 |
"9 0 17 94\n" |
|
76 |
"9 6 18 67\n" |
|
77 |
"0 9 19 86\n" |
|
78 | 78 |
"@attributes\n" |
79 | 79 |
"source 3\n"; |
80 | 80 |
|
81 | 81 |
int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26, 1, 9, 4, 34}; |
82 | 82 |
int test_inc[] = {20, 28, 34, 16, 0, 46, 44, 0, 42, 32, 14, 8, 6, 37}; |
83 | 83 |
|
84 | 84 |
int test_len = sizeof(test_seq) / sizeof(test_seq[0]); |
85 | 85 |
|
86 | 86 |
template <typename Heap> |
87 | 87 |
void heapSortTest() { |
88 | 88 |
RangeMap<int> map(test_len, -1); |
89 | 89 |
|
90 | 90 |
Heap heap(map); |
91 | 91 |
|
92 | 92 |
std::vector<int> v(test_len); |
93 | 93 |
|
94 | 94 |
for (int i = 0; i < test_len; ++i) { |
95 | 95 |
v[i] = test_seq[i]; |
96 | 96 |
heap.push(i, v[i]); |
97 | 97 |
} |
98 | 98 |
std::sort(v.begin(), v.end()); |
99 | 99 |
for (int i = 0; i < test_len; ++i) { |
100 | 100 |
check(v[i] == heap.prio() ,"Wrong order in heap sort."); |
101 | 101 |
heap.pop(); |
102 | 102 |
} |
103 | 103 |
} |
104 | 104 |
|
105 | 105 |
template <typename Heap> |
106 | 106 |
void heapIncreaseTest() { |
107 | 107 |
RangeMap<int> map(test_len, -1); |
108 | 108 |
|
109 | 109 |
Heap heap(map); |
110 | 110 |
|
111 | 111 |
std::vector<int> v(test_len); |
112 | 112 |
|
113 | 113 |
for (int i = 0; i < test_len; ++i) { |
114 | 114 |
v[i] = test_seq[i]; |
115 | 115 |
heap.push(i, v[i]); |
116 | 116 |
} |
117 | 117 |
for (int i = 0; i < test_len; ++i) { |
118 | 118 |
v[i] += test_inc[i]; |
119 | 119 |
heap.increase(i, v[i]); |
120 | 120 |
} |
121 | 121 |
std::sort(v.begin(), v.end()); |
122 | 122 |
for (int i = 0; i < test_len; ++i) { |
123 | 123 |
check(v[i] == heap.prio() ,"Wrong order in heap increase test."); |
124 | 124 |
heap.pop(); |
125 | 125 |
} |
126 | 126 |
} |
127 | 127 |
|
128 | 128 |
|
129 | 129 |
|
130 | 130 |
template <typename Heap> |
131 | 131 |
void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length, |
132 | 132 |
Node source) { |
133 | 133 |
|
134 | 134 |
typename Dijkstra<Digraph, IntArcMap>::template DefStandardHeap<Heap>:: |
135 | 135 |
Create dijkstra(digraph, length); |
136 | 136 |
|
137 | 137 |
dijkstra.run(source); |
138 | 138 |
|
139 | 139 |
for(ArcIt a(digraph); a != INVALID; ++a) { |
140 | 140 |
Node s = digraph.source(a); |
141 | 141 |
Node t = digraph.target(a); |
142 | 142 |
if (dijkstra.reached(s)) { |
143 | 143 |
check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a], |
144 |
|
|
144 |
"Error in a shortest path tree!"); |
|
145 | 145 |
} |
146 | 146 |
} |
147 | 147 |
|
148 | 148 |
for(NodeIt n(digraph); n != INVALID; ++n) { |
149 | 149 |
if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) { |
150 | 150 |
Arc a = dijkstra.predArc(n); |
151 | 151 |
Node s = digraph.source(a); |
152 | 152 |
check( dijkstra.dist(n) - dijkstra.dist(s) == length[a], |
153 | 153 |
"Error in a shortest path tree!"); |
154 | 154 |
} |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
int main() { |
160 | 160 |
|
161 | 161 |
typedef int Item; |
162 | 162 |
typedef int Prio; |
163 | 163 |
typedef RangeMap<int> ItemIntMap; |
164 | 164 |
|
165 | 165 |
Digraph digraph; |
166 | 166 |
IntArcMap length(digraph); |
167 | 167 |
Node source; |
168 | 168 |
|
169 | 169 |
std::istringstream input(test_lgf); |
170 | 170 |
digraphReader(input, digraph). |
171 | 171 |
arcMap("capacity", length). |
172 | 172 |
node("source", source). |
173 | 173 |
run(); |
174 | 174 |
|
175 | 175 |
{ |
176 | 176 |
typedef BinHeap<Prio, ItemIntMap> IntHeap; |
177 | 177 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
178 | 178 |
heapSortTest<IntHeap>(); |
179 | 179 |
heapIncreaseTest<IntHeap>(); |
180 | 180 |
|
181 | 181 |
typedef BinHeap<Prio, IntNodeMap > NodeHeap; |
182 | 182 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
183 | 183 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
184 | 184 |
} |
185 | 185 |
|
186 | 186 |
return 0; |
187 | 187 |
} |
0 comments (0 inline)