gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge #293
4 113 50
merge default
4 files changed with 26470 insertions and 7259 deletions:
31
10
5
6
94
3
3
105
108
129
109
101
106
1490
427
↑ Collapse diff ↑
Ignore white space 6 line context
1
SET(COIN_ROOT_DIR "" CACHE PATH "COIN root directory")
2

	
3
FIND_PATH(COIN_INCLUDE_DIR coin/CoinUtilsConfig.h
4
  HINTS ${COIN_ROOT_DIR}/include
5
)
6
FIND_LIBRARY(COIN_CBC_LIBRARY
7
  NAMES Cbc libCbc
8
  HINTS ${COIN_ROOT_DIR}/lib
9
)
10
FIND_LIBRARY(COIN_CBC_SOLVER_LIBRARY
11
  NAMES CbcSolver libCbcSolver
12
  HINTS ${COIN_ROOT_DIR}/lib
13
)
14
FIND_LIBRARY(COIN_CGL_LIBRARY
15
  NAMES Cgl libCgl
16
  HINTS ${COIN_ROOT_DIR}/lib
17
)
18
FIND_LIBRARY(COIN_CLP_LIBRARY
19
  NAMES Clp libClp
20
  HINTS ${COIN_ROOT_DIR}/lib
21
)
22
FIND_LIBRARY(COIN_COIN_UTILS_LIBRARY
23
  NAMES CoinUtils libCoinUtils
24
  HINTS ${COIN_ROOT_DIR}/lib
25
)
26
FIND_LIBRARY(COIN_OSI_LIBRARY
27
  NAMES Osi libOsi
28
  HINTS ${COIN_ROOT_DIR}/lib
29
)
30
FIND_LIBRARY(COIN_OSI_CBC_LIBRARY
31
  NAMES OsiCbc libOsiCbc
32
  HINTS ${COIN_ROOT_DIR}/lib
33
)
34
FIND_LIBRARY(COIN_OSI_CLP_LIBRARY
35
  NAMES OsiClp libOsiClp
36
  HINTS ${COIN_ROOT_DIR}/lib
37
)
38
FIND_LIBRARY(COIN_OSI_VOL_LIBRARY
39
  NAMES OsiVol libOsiVol
40
  HINTS ${COIN_ROOT_DIR}/lib
41
)
42
FIND_LIBRARY(COIN_VOL_LIBRARY
43
  NAMES Vol libVol
44
  HINTS ${COIN_ROOT_DIR}/lib
45
)
46

	
47
INCLUDE(FindPackageHandleStandardArgs)
48
FIND_PACKAGE_HANDLE_STANDARD_ARGS(COIN DEFAULT_MSG
49
  COIN_INCLUDE_DIR
50
  COIN_CBC_LIBRARY
51
  COIN_CBC_SOLVER_LIBRARY
52
  COIN_CGL_LIBRARY
53
  COIN_CLP_LIBRARY
54
  COIN_COIN_UTILS_LIBRARY
55
  COIN_OSI_LIBRARY
56
  COIN_OSI_CBC_LIBRARY
57
  COIN_OSI_CLP_LIBRARY
58
  COIN_OSI_VOL_LIBRARY
59
  COIN_VOL_LIBRARY
60
)
61

	
62
IF(COIN_FOUND)
63
  SET(COIN_INCLUDE_DIRS ${COIN_INCLUDE_DIR})
64
  SET(COIN_LIBRARIES "${COIN_CBC_LIBRARY};${COIN_CBC_SOLVER_LIBRARY};${COIN_CGL_LIBRARY};${COIN_CLP_LIBRARY};${COIN_COIN_UTILS_LIBRARY};${COIN_OSI_LIBRARY};${COIN_OSI_CBC_LIBRARY};${COIN_OSI_CLP_LIBRARY};${COIN_OSI_VOL_LIBRARY};${COIN_VOL_LIBRARY}")
65
  SET(COIN_CLP_LIBRARIES "${COIN_CLP_LIBRARY};${COIN_COIN_UTILS_LIBRARY}")
66
  SET(COIN_CBC_LIBRARIES ${COIN_LIBRARIES})
67
ENDIF(COIN_FOUND)
68

	
69
MARK_AS_ADVANCED(
70
  COIN_INCLUDE_DIR
71
  COIN_CBC_LIBRARY
72
  COIN_CBC_SOLVER_LIBRARY
73
  COIN_CGL_LIBRARY
74
  COIN_CLP_LIBRARY
75
  COIN_COIN_UTILS_LIBRARY
76
  COIN_OSI_LIBRARY
77
  COIN_OSI_CBC_LIBRARY
78
  COIN_OSI_CLP_LIBRARY
79
  COIN_OSI_VOL_LIBRARY
80
  COIN_VOL_LIBRARY
81
)
82

	
83
IF(COIN_FOUND)
84
  SET(LEMON_HAVE_LP TRUE)
85
  SET(LEMON_HAVE_MIP TRUE)
86
  SET(LEMON_HAVE_CLP TRUE)
87
  SET(LEMON_HAVE_CBC TRUE)
88
ENDIF(COIN_FOUND)
Ignore white space 6 line context
1
SET(CPLEX_ROOT_DIR "" CACHE PATH "CPLEX root directory")
2

	
3
FIND_PATH(CPLEX_INCLUDE_DIR
4
  ilcplex/cplex.h
5
  PATHS "C:/ILOG/CPLEX91/include"
6
  PATHS "/opt/ilog/cplex91/include"
7
  HINTS ${CPLEX_ROOT_DIR}/include
8
)
9
FIND_LIBRARY(CPLEX_LIBRARY
10
  cplex91
11
  PATHS "C:/ILOG/CPLEX91/lib/msvc7/stat_mda"
12
  PATHS "/opt/ilog/cplex91/bin"
13
  HINTS ${CPLEX_ROOT_DIR}/bin
14
)
15

	
16
INCLUDE(FindPackageHandleStandardArgs)
17
FIND_PACKAGE_HANDLE_STANDARD_ARGS(CPLEX DEFAULT_MSG CPLEX_LIBRARY CPLEX_INCLUDE_DIR)
18

	
19
FIND_PATH(CPLEX_BIN_DIR
20
  cplex91.dll
21
  PATHS "C:/ILOG/CPLEX91/bin/x86_win32"
22
)
23

	
24
IF(CPLEX_FOUND)
25
  SET(CPLEX_INCLUDE_DIRS ${CPLEX_INCLUDE_DIR})
26
  SET(CPLEX_LIBRARIES ${CPLEX_LIBRARY})
27
  IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
28
    SET(CPLEX_LIBRARIES "${CPLEX_LIBRARIES};m;pthread")
29
  ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
30
ENDIF(CPLEX_FOUND)
31

	
32
MARK_AS_ADVANCED(CPLEX_LIBRARY CPLEX_INCLUDE_DIR CPLEX_BIN_DIR)
33

	
34
IF(CPLEX_FOUND)
35
  SET(LEMON_HAVE_LP TRUE)
36
  SET(LEMON_HAVE_MIP TRUE)
37
  SET(LEMON_HAVE_CPLEX TRUE)
38
ENDIF(CPLEX_FOUND)
Ignore white space 6 line context
1
SET(GLPK_ROOT_DIR "" CACHE PATH "GLPK root directory")
2

	
3
SET(GLPK_REGKEY "[HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\Glpk;InstallPath]")
4
GET_FILENAME_COMPONENT(GLPK_ROOT_PATH ${GLPK_REGKEY} ABSOLUTE)
5

	
6
FIND_PATH(GLPK_INCLUDE_DIR
7
  glpk.h
8
  PATHS ${GLPK_REGKEY}/include
9
  HINTS ${GLPK_ROOT_DIR}/include
10
)
11
FIND_LIBRARY(GLPK_LIBRARY
12
  glpk
13
  PATHS ${GLPK_REGKEY}/lib
14
  HINTS ${GLPK_ROOT_DIR}/lib
15
)
16

	
17
IF(GLPK_INCLUDE_DIR AND GLPK_LIBRARY)
18
  FILE(READ ${GLPK_INCLUDE_DIR}/glpk.h GLPK_GLPK_H)
19

	
20
  STRING(REGEX MATCH "define[ ]+GLP_MAJOR_VERSION[ ]+[0-9]+" GLPK_MAJOR_VERSION_LINE "${GLPK_GLPK_H}")
21
  STRING(REGEX REPLACE "define[ ]+GLP_MAJOR_VERSION[ ]+([0-9]+)" "\\1" GLPK_VERSION_MAJOR "${GLPK_MAJOR_VERSION_LINE}")
22

	
23
  STRING(REGEX MATCH "define[ ]+GLP_MINOR_VERSION[ ]+[0-9]+" GLPK_MINOR_VERSION_LINE "${GLPK_GLPK_H}")
24
  STRING(REGEX REPLACE "define[ ]+GLP_MINOR_VERSION[ ]+([0-9]+)" "\\1" GLPK_VERSION_MINOR "${GLPK_MINOR_VERSION_LINE}")
25

	
26
  SET(GLPK_VERSION_STRING "${GLPK_VERSION_MAJOR}.${GLPK_VERSION_MINOR}")
27

	
28
  IF(GLPK_FIND_VERSION)
29
    IF(GLPK_FIND_VERSION_COUNT GREATER 2)
30
      MESSAGE(SEND_ERROR "unexpected version string")
31
    ENDIF(GLPK_FIND_VERSION_COUNT GREATER 2)
32

	
33
    MATH(EXPR GLPK_REQUESTED_VERSION "${GLPK_FIND_VERSION_MAJOR}*100 + ${GLPK_FIND_VERSION_MINOR}")
34
    MATH(EXPR GLPK_FOUND_VERSION "${GLPK_VERSION_MAJOR}*100 + ${GLPK_VERSION_MINOR}")
35

	
36
    IF(GLPK_FOUND_VERSION LESS GLPK_REQUESTED_VERSION)
37
      SET(GLPK_PROPER_VERSION_FOUND FALSE)
38
    ELSE(GLPK_FOUND_VERSION LESS GLPK_REQUESTED_VERSION)
39
      SET(GLPK_PROPER_VERSION_FOUND TRUE)
40
    ENDIF(GLPK_FOUND_VERSION LESS GLPK_REQUESTED_VERSION)
41
  ELSE(GLPK_FIND_VERSION)
42
    SET(GLPK_PROPER_VERSION_FOUND TRUE)
43
  ENDIF(GLPK_FIND_VERSION)
44
ENDIF(GLPK_INCLUDE_DIR AND GLPK_LIBRARY)
45

	
46
INCLUDE(FindPackageHandleStandardArgs)
47
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLPK DEFAULT_MSG GLPK_LIBRARY GLPK_INCLUDE_DIR GLPK_PROPER_VERSION_FOUND)
48

	
49
IF(GLPK_FOUND)
50
  SET(GLPK_INCLUDE_DIRS ${GLPK_INCLUDE_DIR})
51
  SET(GLPK_LIBRARIES ${GLPK_LIBRARY})
52
  SET(GLPK_BIN_DIR ${GLPK_ROOT_PATH}/bin)
53
ENDIF(GLPK_FOUND)
54

	
55
MARK_AS_ADVANCED(GLPK_LIBRARY GLPK_INCLUDE_DIR GLPK_BIN_DIR)
56

	
57
IF(GLPK_FOUND)
58
  SET(LEMON_HAVE_LP TRUE)
59
  SET(LEMON_HAVE_MIP TRUE)
60
  SET(LEMON_HAVE_GLPK TRUE)
61
ENDIF(GLPK_FOUND)
Ignore white space 6 line context
1
SET(LEMON_INCLUDE_DIR "@CMAKE_INSTALL_PREFIX@/include" CACHE PATH "LEMON include directory")
2
SET(LEMON_INCLUDE_DIRS "${LEMON_INCLUDE_DIR}")
3

	
4
IF(UNIX)
5
  SET(LEMON_LIB_NAME "libemon.a")
6
ELSEIF(WIN32)
7
  SET(LEMON_LIB_NAME "lemon.lib")
8
ENDIF(UNIX)
9

	
10
SET(LEMON_LIBRARY "@CMAKE_INSTALL_PREFIX@/lib/${LEMON_LIB_NAME}" CACHE FILEPATH "LEMON library")
11
SET(LEMON_LIBRARIES "${LEMON_LIBRARY}")
12

	
13
MARK_AS_ADVANCED(LEMON_LIBRARY LEMON_INCLUDE_DIR)
Ignore white space 6 line context
1
SET(LEMON_VERSION "@PACKAGE_VERSION@" CACHE STRING "LEMON version string.")
Ignore white space 6 line context
1
%!PS-Adobe-3.0 EPSF-3.0
2
%%BoundingBox: 15 18 829 570
3
%%HiResBoundingBox: 15.1913 18.4493 828.078 569.438
4
%%Creator: Karbon14 EPS Exportfilter 0.5
5
%%CreationDate: (04/15/06 15:20:26)
6
%%For: (Balazs Dezso) ()
7
%%Title: ()
8

	
9
/N {newpath} def
10
/C {closepath} def
11
/m {moveto} def
12
/c {curveto} def
13
/l {lineto} def
14
/s {stroke} def
15
/f {fill} def
16
/w {setlinewidth} def
17
/d {setdash} def
18
/r {setrgbcolor} def
19
/S {gsave} def
20
/R {grestore} def
21

	
22
N
23
251.402 32.047 m
24
532.945 293.946 814.484 555.844 814.484 555.844 c
25
[] 0 d 1 0 0 r 3.92814 w s
26

	
27
N
28
749.012 32.047 m
29
742.465 293.946 735.918 555.844 735.918 555.844 c
30
[] 0 d 0 0 0 r 1.96407 w s
31

	
32
N
33
539.492 32.047 m
34
637.703 293.946 735.918 555.844 735.918 555.844 c
35
[] 0 d 0 0 0 r 1.96407 w s
36

	
37
N
38
172.832 32.047 m
39
454.375 293.946 735.918 555.844 735.918 555.844 c
40
[] 0 d 0 0 0 r 1.96407 w s
41

	
42
N
43
107.355 32.047 m
44
421.637 293.946 735.918 555.844 735.918 555.844 c
45
[] 0 d 1 0 0 r 3.92814 w s
46

	
47
N
48
644.25 555.844 m
49
696.633 293.946 749.012 32.047 749.012 32.047 c
50
[] 0 d 0 0 0 r 1.96407 w s
51

	
52
N
53
474.016 555.844 m
54
611.516 293.946 749.012 32.047 749.012 32.047 c
55
[] 0 d 1 0 0 r 3.92814 w s
56

	
57
N
58
683.535 32.047 m
59
663.894 293.946 644.25 555.844 644.25 555.844 c
60
[] 0 d 0 0 0 r 1.96407 w s
61

	
62
N
63
120.453 555.844 m
64
401.992 293.946 683.535 32.047 683.535 32.047 c
65
[] 0 d 0 0 0 r 1.96407 w s
66

	
67
N
68
28.7853 555.844 m
69
356.16 293.946 683.535 32.047 683.535 32.047 c
70
[] 0 d 1 0 0 r 3.92814 w s
71

	
72
N
73
539.492 32.047 m
74
546.039 293.946 552.586 555.844 552.586 555.844 c
75
[] 0 d 1 0 0 r 3.92814 w s
76

	
77
N
78
316.875 32.047 m
79
349.613 293.946 382.351 555.844 382.351 555.844 c
80
[] 0 d 1 0 0 r 3.92814 w s
81

	
82
N
83
107.355 32.047 m
84
244.855 293.946 382.351 555.844 382.351 555.844 c
85
[] 0 d 0 0 0 r 1.96407 w s
86

	
87
N
88
290.687 555.844 m
89
375.805 293.946 460.922 32.047 460.922 32.047 c
90
[] 0 d 1 0 0 r 3.92814 w s
91

	
92
N
93
120.453 555.844 m
94
290.687 293.946 460.922 32.047 460.922 32.047 c
95
[] 0 d 0 0 0 r 1.96407 w s
96

	
97
N
98
172.832 32.047 m
99
146.64 293.946 120.453 555.844 120.453 555.844 c
100
[] 0 d 1 0 0 r 3.92814 w s
101

	
102
N
103
15.6913 555.844 m
104
15.6913 555.844 l
105
15.6913 548.614 21.5553 542.75 28.7853 542.75 c
106
36.0163 542.75 41.8833 548.614 41.8833 555.844 c
107
41.8833 563.075 36.0163 568.938 28.7853 568.938 c
108
21.5553 568.938 15.6913 563.075 15.6913 555.844 c
109
15.6913 555.844 l
110
C
111
S 0 0 0 r f R
112

	
113
N
114
16.8833 555.844 m
115
16.8833 555.844 l
116
16.8833 549.27 22.2113 543.942 28.7853 543.942 c
117
35.3593 543.942 40.6913 549.27 40.6913 555.844 c
118
40.6913 562.418 35.3593 567.747 28.7853 567.747 c
119
22.2113 567.747 16.8833 562.418 16.8833 555.844 c
120
16.8833 555.844 l
121
C
122
S 1 0.5 1 r f R
123

	
124
N
125
107.355 555.844 m
126
107.355 555.844 l
127
107.355 548.614 113.223 542.75 120.453 542.75 c
128
127.683 542.75 133.547 548.614 133.547 555.844 c
129
133.547 563.075 127.683 568.938 120.453 568.938 c
130
113.223 568.938 107.355 563.075 107.355 555.844 c
131
107.355 555.844 l
132
C
133
S 0 0 0 r f R
134

	
135
N
136
108.547 555.844 m
137
108.547 555.844 l
138
108.547 549.27 113.879 543.942 120.453 543.942 c
139
127.027 543.942 132.355 549.27 132.355 555.844 c
140
132.355 562.418 127.027 567.747 120.453 567.747 c
141
113.879 567.747 108.547 562.418 108.547 555.844 c
142
108.547 555.844 l
143
C
144
S 1 0 1 r f R
145

	
146
N
147
199.019 555.844 m
148
199.019 555.844 l
149
199.019 548.614 204.887 542.75 212.117 542.75 c
150
219.348 542.75 225.211 548.614 225.211 555.844 c
151
225.211 563.075 219.348 568.938 212.117 568.938 c
152
204.887 568.938 199.019 563.075 199.019 555.844 c
153
199.019 555.844 l
154
C
155
S 0 0 0 r f R
156

	
157
N
158
200.211 555.844 m
159
200.211 555.844 l
160
200.211 549.27 205.543 543.942 212.117 543.942 c
161
218.691 543.942 224.019 549.27 224.019 555.844 c
162
224.019 562.418 218.691 567.747 212.117 567.747 c
163
205.543 567.747 200.211 562.418 200.211 555.844 c
164
200.211 555.844 l
165
C
166
S 1 0.5 1 r f R
167

	
168
N
169
277.59 555.844 m
170
277.59 555.844 l
171
277.59 548.614 283.457 542.75 290.687 542.75 c
172
297.918 542.75 303.781 548.614 303.781 555.844 c
173
303.781 563.075 297.918 568.938 290.687 568.938 c
174
283.457 568.938 277.59 563.075 277.59 555.844 c
175
277.59 555.844 l
176
C
177
S 0 0 0 r f R
178

	
179
N
180
278.781 555.844 m
181
278.781 555.844 l
182
278.781 549.27 284.113 543.942 290.687 543.942 c
183
297.262 543.942 302.59 549.27 302.59 555.844 c
184
302.59 562.418 297.262 567.747 290.687 567.747 c
185
284.113 567.747 278.781 562.418 278.781 555.844 c
186
278.781 555.844 l
187
C
188
S 1 0 1 r f R
189

	
190
N
191
369.258 555.844 m
192
369.258 555.844 l
193
369.258 548.614 375.121 542.75 382.351 542.75 c
194
389.582 542.75 395.445 548.614 395.445 555.844 c
195
395.445 563.075 389.582 568.938 382.351 568.938 c
196
375.121 568.938 369.258 563.075 369.258 555.844 c
197
369.258 555.844 l
198
C
199
S 0 0 0 r f R
200

	
201
N
202
370.445 555.844 m
203
370.445 555.844 l
204
370.445 549.27 375.777 543.942 382.351 543.942 c
205
388.926 543.942 394.258 549.27 394.258 555.844 c
206
394.258 562.418 388.926 567.747 382.351 567.747 c
207
375.777 567.747 370.445 562.418 370.445 555.844 c
208
370.445 555.844 l
209
C
210
S 1 0 1 r f R
211

	
212
N
213
460.922 555.844 m
214
460.922 555.844 l
215
460.922 548.614 466.785 542.75 474.016 542.75 c
216
481.246 542.75 487.109 548.614 487.109 555.844 c
217
487.109 563.075 481.246 568.938 474.016 568.938 c
218
466.785 568.938 460.922 563.075 460.922 555.844 c
219
460.922 555.844 l
220
C
221
S 0 0 0 r f R
222

	
223
N
224
462.113 555.844 m
225
462.113 555.844 l
226
462.113 549.27 467.441 543.942 474.016 543.942 c
227
480.59 543.942 485.922 549.27 485.922 555.844 c
228
485.922 562.418 480.59 567.747 474.016 567.747 c
229
467.441 567.747 462.113 562.418 462.113 555.844 c
230
462.113 555.844 l
231
C
232
S 1 0.5 1 r f R
233

	
234
N
235
539.492 555.844 m
236
539.492 555.844 l
237
539.492 548.614 545.355 542.75 552.586 542.75 c
238
559.816 542.75 565.68 548.614 565.68 555.844 c
239
565.68 563.075 559.816 568.938 552.586 568.938 c
240
545.355 568.938 539.492 563.075 539.492 555.844 c
241
539.492 555.844 l
242
C
243
S 0 0 0 r f R
244

	
245
N
246
540.683 555.844 m
247
540.683 555.844 l
248
540.683 549.27 546.012 543.942 552.586 543.942 c
249
559.16 543.942 564.492 549.27 564.492 555.844 c
250
564.492 562.418 559.16 567.747 552.586 567.747 c
251
546.012 567.747 540.683 562.418 540.683 555.844 c
252
540.683 555.844 l
253
C
254
S 1 0 1 r f R
255

	
256
N
257
631.156 555.844 m
258
631.156 555.844 l
259
631.156 548.614 637.019 542.75 644.25 542.75 c
260
651.48 542.75 657.348 548.614 657.348 555.844 c
261
657.348 563.075 651.48 568.938 644.25 568.938 c
262
637.019 568.938 631.156 563.075 631.156 555.844 c
263
631.156 555.844 l
264
C
265
S 0 0 0 r f R
266

	
267
N
268
632.348 555.844 m
269
632.348 555.844 l
270
632.348 549.27 637.676 543.942 644.25 543.942 c
271
650.824 543.942 656.156 549.27 656.156 555.844 c
272
656.156 562.418 650.824 567.747 644.25 567.747 c
273
637.676 567.747 632.348 562.418 632.348 555.844 c
274
632.348 555.844 l
275
C
276
S 1 0.5 1 r f R
277

	
278
N
279
722.82 555.844 m
280
722.82 555.844 l
281
722.82 548.614 728.687 542.75 735.918 542.75 c
282
743.149 542.75 749.012 548.614 749.012 555.844 c
283
749.012 563.075 743.149 568.938 735.918 568.938 c
284
728.687 568.938 722.82 563.075 722.82 555.844 c
285
722.82 555.844 l
286
C
287
S 0 0 0 r f R
288

	
289
N
290
724.012 555.844 m
291
724.012 555.844 l
292
724.012 549.27 729.344 543.942 735.918 543.942 c
293
742.492 543.942 747.82 549.27 747.82 555.844 c
294
747.82 562.418 742.492 567.747 735.918 567.747 c
295
729.344 567.747 724.012 562.418 724.012 555.844 c
296
724.012 555.844 l
297
C
298
S 1 0 1 r f R
299

	
300
N
301
801.391 555.844 m
302
801.391 555.844 l
303
801.391 548.614 807.254 542.75 814.484 542.75 c
304
821.715 542.75 827.578 548.614 827.578 555.844 c
305
827.578 563.075 821.715 568.938 814.484 568.938 c
306
807.254 568.938 801.391 563.075 801.391 555.844 c
307
801.391 555.844 l
308
C
309
S 0 0 0 r f R
310

	
311
N
312
802.582 555.844 m
313
802.582 555.844 l
314
802.582 549.27 807.91 543.942 814.484 543.942 c
315
821.059 543.942 826.387 549.27 826.387 555.844 c
316
826.387 562.418 821.059 567.747 814.484 567.747 c
317
807.91 567.747 802.582 562.418 802.582 555.844 c
318
802.582 555.844 l
319
C
320
S 1 0 1 r f R
321

	
322
N
323
15.6913 32.047 m
324
15.6913 32.047 l
325
15.6913 24.8165 21.5553 18.9493 28.7853 18.9493 c
326
36.0163 18.9493 41.8833 24.8165 41.8833 32.047 c
327
41.8833 39.2775 36.0163 45.1407 28.7853 45.1407 c
328
21.5553 45.1407 15.6913 39.2775 15.6913 32.047 c
329
15.6913 32.047 l
330
C
331
S 0 0 0 r f R
332

	
333
N
334
16.8833 32.047 m
335
16.8833 32.047 l
336
16.8833 25.4728 22.2113 20.1407 28.7853 20.1407 c
337
35.3593 20.1407 40.6913 25.4728 40.6913 32.047 c
338
40.6913 38.6212 35.3593 43.9493 28.7853 43.9493 c
339
22.2113 43.9493 16.8833 38.6212 16.8833 32.047 c
340
16.8833 32.047 l
341
C
342
S 0.5 0.5 1 r f R
343

	
344
N
345
94.2623 32.047 m
346
94.2623 32.047 l
347
94.2623 24.8165 100.125 18.9493 107.355 18.9493 c
348
114.586 18.9493 120.453 24.8165 120.453 32.047 c
349
120.453 39.2775 114.586 45.1407 107.355 45.1407 c
350
100.125 45.1407 94.2623 39.2775 94.2623 32.047 c
351
94.2623 32.047 l
352
C
353
S 0 0 0 r f R
354

	
355
N
356
95.4533 32.047 m
357
95.4533 32.047 l
358
95.4533 25.4728 100.781 20.1407 107.355 20.1407 c
359
113.93 20.1407 119.262 25.4728 119.262 32.047 c
360
119.262 38.6212 113.93 43.9493 107.355 43.9493 c
361
100.781 43.9493 95.4533 38.6212 95.4533 32.047 c
362
95.4533 32.047 l
363
C
364
S 0.5 0.5 1 r f R
365

	
366
N
367
159.734 32.047 m
368
159.734 32.047 l
369
159.734 24.8165 165.601 18.9493 172.832 18.9493 c
370
180.062 18.9493 185.926 24.8165 185.926 32.047 c
371
185.926 39.2775 180.062 45.1407 172.832 45.1407 c
372
165.601 45.1407 159.734 39.2775 159.734 32.047 c
373
159.734 32.047 l
374
C
375
S 0 0 0 r f R
376

	
377
N
378
160.926 32.047 m
379
160.926 32.047 l
380
160.926 25.4728 166.258 20.1407 172.832 20.1407 c
381
179.406 20.1407 184.734 25.4728 184.734 32.047 c
382
184.734 38.6212 179.406 43.9493 172.832 43.9493 c
383
166.258 43.9493 160.926 38.6212 160.926 32.047 c
384
160.926 32.047 l
385
C
386
S 0.5 0.5 1 r f R
387

	
388
N
389
238.305 32.047 m
390
238.305 32.047 l
391
238.305 24.8165 244.172 18.9493 251.402 18.9493 c
392
258.633 18.9493 264.496 24.8165 264.496 32.047 c
393
264.496 39.2775 258.633 45.1407 251.402 45.1407 c
394
244.172 45.1407 238.305 39.2775 238.305 32.047 c
395
238.305 32.047 l
396
C
397
S 0 0 0 r f R
398

	
399
N
400
239.496 32.047 m
401
239.496 32.047 l
402
239.496 25.4728 244.828 20.1407 251.402 20.1407 c
403
257.976 20.1407 263.305 25.4728 263.305 32.047 c
404
263.305 38.6212 257.976 43.9493 251.402 43.9493 c
405
244.828 43.9493 239.496 38.6212 239.496 32.047 c
406
239.496 32.047 l
407
C
408
S 0.5 0.5 1 r f R
409

	
410
N
411
303.781 32.047 m
412
303.781 32.047 l
413
303.781 24.8165 309.644 18.9493 316.875 18.9493 c
414
324.105 18.9493 329.973 24.8165 329.973 32.047 c
415
329.973 39.2775 324.105 45.1407 316.875 45.1407 c
416
309.644 45.1407 303.781 39.2775 303.781 32.047 c
417
303.781 32.047 l
418
C
419
S 0 0 0 r f R
420

	
421
N
422
304.973 32.047 m
423
304.973 32.047 l
424
304.973 25.4728 310.301 20.1407 316.875 20.1407 c
425
323.449 20.1407 328.781 25.4728 328.781 32.047 c
426
328.781 38.6212 323.449 43.9493 316.875 43.9493 c
427
310.301 43.9493 304.973 38.6212 304.973 32.047 c
428
304.973 32.047 l
429
C
430
S 0.5 0.5 1 r f R
431

	
432
N
433
382.351 32.047 m
434
382.351 32.047 l
435
382.351 24.8165 388.215 18.9493 395.445 18.9493 c
436
402.676 18.9493 408.543 24.8165 408.543 32.047 c
437
408.543 39.2775 402.676 45.1407 395.445 45.1407 c
438
388.215 45.1407 382.351 39.2775 382.351 32.047 c
439
382.351 32.047 l
440
C
441
S 0 0 0 r f R
442

	
443
N
444
383.543 32.047 m
445
383.543 32.047 l
446
383.543 25.4728 388.871 20.1407 395.445 20.1407 c
447
402.019 20.1407 407.351 25.4728 407.351 32.047 c
448
407.351 38.6212 402.019 43.9493 395.445 43.9493 c
449
388.871 43.9493 383.543 38.6212 383.543 32.047 c
450
383.543 32.047 l
451
C
452
S 0.5 0.5 1 r f R
453

	
454
N
455
447.828 32.047 m
456
447.828 32.047 l
457
447.828 24.8165 453.691 18.9493 460.922 18.9493 c
458
468.152 18.9493 474.016 24.8165 474.016 32.047 c
459
474.016 39.2775 468.152 45.1407 460.922 45.1407 c
460
453.691 45.1407 447.828 39.2775 447.828 32.047 c
461
447.828 32.047 l
462
C
463
S 0 0 0 r f R
464

	
465
N
466
449.016 32.047 m
467
449.016 32.047 l
468
449.016 25.4728 454.348 20.1407 460.922 20.1407 c
469
467.496 20.1407 472.824 25.4728 472.824 32.047 c
470
472.824 38.6212 467.496 43.9493 460.922 43.9493 c
471
454.348 43.9493 449.016 38.6212 449.016 32.047 c
472
449.016 32.047 l
473
C
474
S 0.5 0.5 1 r f R
475

	
476
N
477
526.394 32.047 m
478
526.394 32.047 l
479
526.394 24.8165 532.262 18.9493 539.492 18.9493 c
480
546.723 18.9493 552.586 24.8165 552.586 32.047 c
481
552.586 39.2775 546.723 45.1407 539.492 45.1407 c
482
532.262 45.1407 526.394 39.2775 526.394 32.047 c
483
526.394 32.047 l
484
C
485
S 0 0 0 r f R
486

	
487
N
488
527.586 32.047 m
489
527.586 32.047 l
490
527.586 25.4728 532.918 20.1407 539.492 20.1407 c
491
546.066 20.1407 551.394 25.4728 551.394 32.047 c
492
551.394 38.6212 546.066 43.9493 539.492 43.9493 c
493
532.918 43.9493 527.586 38.6212 527.586 32.047 c
494
527.586 32.047 l
495
C
496
S 0.5 0.5 1 r f R
497

	
498
N
499
591.871 32.047 m
500
591.871 32.047 l
501
591.871 24.8165 597.734 18.9493 604.965 18.9493 c
502
612.195 18.9493 618.062 24.8165 618.062 32.047 c
503
618.062 39.2775 612.195 45.1407 604.965 45.1407 c
504
597.734 45.1407 591.871 39.2775 591.871 32.047 c
505
591.871 32.047 l
506
C
507
S 0 0 0 r f R
508

	
509
N
510
593.062 32.047 m
511
593.062 32.047 l
512
593.062 25.4728 598.39 20.1407 604.965 20.1407 c
513
611.539 20.1407 616.871 25.4728 616.871 32.047 c
514
616.871 38.6212 611.539 43.9493 604.965 43.9493 c
515
598.39 43.9493 593.062 38.6212 593.062 32.047 c
516
593.062 32.047 l
517
C
518
S 0.5 0.5 1 r f R
519

	
520
N
521
670.441 32.047 m
522
670.441 32.047 l
523
670.441 24.8165 676.305 18.9493 683.535 18.9493 c
524
690.766 18.9493 696.633 24.8165 696.633 32.047 c
525
696.633 39.2775 690.766 45.1407 683.535 45.1407 c
526
676.305 45.1407 670.441 39.2775 670.441 32.047 c
527
670.441 32.047 l
528
C
529
S 0 0 0 r f R
530

	
531
N
532
671.633 32.047 m
533
671.633 32.047 l
534
671.633 25.4728 676.961 20.1407 683.535 20.1407 c
535
690.109 20.1407 695.441 25.4728 695.441 32.047 c
536
695.441 38.6212 690.109 43.9493 683.535 43.9493 c
537
676.961 43.9493 671.633 38.6212 671.633 32.047 c
538
671.633 32.047 l
539
C
540
S 0 0 1 r f R
541

	
542
N
543
735.918 32.047 m
544
735.918 32.047 l
545
735.918 24.8165 741.781 18.9493 749.012 18.9493 c
546
756.242 18.9493 762.106 24.8165 762.106 32.047 c
547
762.106 39.2775 756.242 45.1407 749.012 45.1407 c
548
741.781 45.1407 735.918 39.2775 735.918 32.047 c
549
735.918 32.047 l
550
C
551
S 0 0 0 r f R
552

	
553
N
554
737.105 32.047 m
555
737.105 32.047 l
556
737.105 25.4728 742.437 20.1407 749.012 20.1407 c
557
755.586 20.1407 760.914 25.4728 760.914 32.047 c
558
760.914 38.6212 755.586 43.9493 749.012 43.9493 c
559
742.437 43.9493 737.105 38.6212 737.105 32.047 c
560
737.105 32.047 l
561
C
562
S 0 0 1 r f R
563

	
564
N
565
801.391 32.047 m
566
801.391 32.047 l
567
801.391 24.8165 807.254 18.9493 814.484 18.9493 c
568
821.715 18.9493 827.578 24.8165 827.578 32.047 c
569
827.578 39.2775 821.715 45.1407 814.484 45.1407 c
570
807.254 45.1407 801.391 39.2775 801.391 32.047 c
571
801.391 32.047 l
572
C
573
S 0 0 0 r f R
574

	
575
N
576
802.582 32.047 m
577
802.582 32.047 l
578
802.582 25.4728 807.91 20.1407 814.484 20.1407 c
579
821.059 20.1407 826.387 25.4728 826.387 32.047 c
580
826.387 38.6212 821.059 43.9493 814.484 43.9493 c
581
807.91 43.9493 802.582 38.6212 802.582 32.047 c
582
802.582 32.047 l
583
C
584
S 0.5 0.5 1 r f R
585

	
586
%%EOF
1
%!PS-Adobe-2.0 EPSF-2.0
2
%%Creator: LEMON, graphToEps()
3
%%CreationDate: Tue Nov 15 16:51:43 2005
4
%%BoundingBox: 0 0 842 596
5
%%EndComments
6
/lb { setlinewidth setrgbcolor newpath moveto
7
      4 2 roll 1 index 1 index curveto stroke } bind def
8
/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def
9
/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def
10
/sq { newpath 2 index 1 index add 2 index 2 index add moveto
11
      2 index 1 index sub 2 index 2 index add lineto
12
      2 index 1 index sub 2 index 2 index sub lineto
13
      2 index 1 index add 2 index 2 index sub lineto
14
      closepath pop pop pop} bind def
15
/di { newpath 2 index 1 index add 2 index moveto
16
      2 index             2 index 2 index add lineto
17
      2 index 1 index sub 2 index             lineto
18
      2 index             2 index 2 index sub lineto
19
      closepath pop pop pop} bind def
20
/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill
21
     setrgbcolor 1.1 div c fill
22
   } bind def
23
/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill
24
     setrgbcolor 1.1 div sq fill
25
   } bind def
26
/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill
27
     setrgbcolor 1.1 div di fill
28
   } bind def
29
/arrl 1 def
30
/arrw 0.3 def
31
/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def
32
/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def
33
       /w exch def /len exch def
34
       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto
35
       len w sub arrl sub dx dy lrl
36
       arrw dy dx neg lrl
37
       dx arrl w add mul dy w 2 div arrw add mul sub
38
       dy arrl w add mul dx w 2 div arrw add mul add rlineto
39
       dx arrl w add mul neg dy w 2 div arrw add mul sub
40
       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto
41
       arrw dy dx neg lrl
42
       len w sub arrl sub neg dx dy lrl
43
       closepath fill } bind def
44
/cshow { 2 index 2 index moveto dup stringwidth pop
45
         neg 2 div fosi .35 mul neg rmoveto show pop pop} def
46

	
47
gsave
48
90 rotate
49
0 -842 translate
50
71.6378 15 translate
51
0.389093 dup scale
52
90 rotate
53
1197.47 -613.138 translate
54
%Edges:
55
gsave
56
513.857 -446.322 296.569 -487.43 79.2808 -528.539 0 0 0 2 lb
57
513.857 -446.322 575.52 -315.655 637.183 -184.989 0 0 0 2 lb
58
393.468 566.711 494.771 434.577 596.074 302.442 0 0 0 2 lb
59
393.468 566.711 155.625 579.925 -82.2171 593.138 0 0 0 2 lb
60
393.468 566.711 251.056 450.726 108.644 334.741 0 0 0 2 lb
61
869.153 52.8539 732.613 177.648 596.074 302.442 0 0 0 2 lb
62
869.153 52.8539 753.168 -66.0676 637.183 -184.989 0 0 0 2 lb
63
-82.2171 593.138 -91.0261 346.487 -99.8351 99.8351 0 0 0 2 lb
64
-663.61 546.157 -753.168 394.936 -842.726 243.715 0 0 0 2 lb
65
-663.61 546.157 -574.052 437.513 -484.494 328.869 0 0 0 2 lb
66
-1077.63 161.498 -960.178 202.606 -842.726 243.715 0 0 0 2 lb
67
-1077.63 161.498 -968.987 66.0674 -860.344 -29.3633 0 0 0 2 lb
68
-1177.47 -234.906 -1029.18 -381.722 -880.898 -528.539 0 0 0 2 lb
69
-1177.47 -234.906 -1018.91 -132.135 -860.344 -29.3633 0 0 0 2 lb
70
-880.898 -528.539 -744.359 -387.595 -607.82 -246.651 0 0 0 2 lb
71
-499.175 -499.175 -355.295 -475.685 -211.415 -452.194 0 0 0 2 lb
72
-499.175 -499.175 -553.498 -372.913 -607.82 -246.651 0 0 0 2 lb
73
-499.175 -499.175 -386.587 -315.087 -274 -131 0 0 0 2 lb
74
79.2808 -528.539 -66.0671 -490.366 -211.415 -452.194 0 0 0 2 lb
75
637.183 -184.989 421.363 -253.993 205.543 -322.996 0 0 0 2 lb
76
205.543 -322.996 162.966 -226.097 120.389 -129.198 0 0 0 2 lb
77
399.34 88.0898 259.865 -20.5541 120.389 -129.198 0 0 0 2 lb
78
399.34 88.0898 253.992 211.415 108.644 334.741 0 0 0 2 lb
79
-842.726 243.715 -471.281 171.775 -99.8351 99.8351 0 0 0 2 lb
80
-842.726 243.715 -558.363 56.3575 -274 -131 0 0 0 2 lb
81
-860.344 -29.3633 -734.082 -138.007 -607.82 -246.651 0 0 0 2 lb
82
-211.415 -452.194 -45.513 -290.696 120.389 -129.198 0 0 0 2 lb
83
-99.8351 99.8351 4.40445 217.288 108.644 334.741 0 0 0 2 lb
84
-99.8351 99.8351 -292.165 214.352 -484.494 328.869 0 0 0 2 lb
85
120.389 -129.198 -76.8055 -130.099 -274 -131 0 0 0 2 lb
86
grestore
87
%Nodes:
88
gsave
89
-274 -131 20 1 0 0 nc
90
-607.82 -246.651 20 1 0 0 nc
91
-484.494 328.869 20 0 0 1 nc
92
108.644 334.741 20 0 0 1 nc
93
120.389 -129.198 20 0 0 1 nc
94
-99.8351 99.8351 20 1 0 0 nc
95
-211.415 -452.194 20 1 0 0 nc
96
-860.344 -29.3633 20 0 0 1 nc
97
-842.726 243.715 20 0 0 1 nc
98
399.34 88.0898 20 1 0 0 nc
99
205.543 -322.996 20 1 0 0 nc
100
637.183 -184.989 20 0 0 1 nc
101
79.2808 -528.539 20 0 0 1 nc
102
-499.175 -499.175 20 0 0 1 nc
103
-880.898 -528.539 20 0 0 1 nc
104
-1177.47 -234.906 20 1 0 0 nc
105
-1077.63 161.498 20 1 0 0 nc
106
-663.61 546.157 20 1 0 0 nc
107
-82.2171 593.138 20 0 0 1 nc
108
596.074 302.442 20 0 0 1 nc
109
869.153 52.8539 20 1 0 0 nc
110
393.468 566.711 20 1 0 0 nc
111
513.857 -446.322 20 1 0 0 nc
112
grestore
113
grestore
114
showpage
1
%!PS-Adobe-2.0 EPSF-2.0
2
%%Creator: LEMON, graphToEps()
3
%%CreationDate: Fri Nov  4 13:47:12 2005
4
%%BoundingBox: 0 0 842 596
5
%%EndComments
6
/lb { setlinewidth setrgbcolor newpath moveto
7
      4 2 roll 1 index 1 index curveto stroke } bind def
8
/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def
9
/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def
10
/sq { newpath 2 index 1 index add 2 index 2 index add moveto
11
      2 index 1 index sub 2 index 2 index add lineto
12
      2 index 1 index sub 2 index 2 index sub lineto
13
      2 index 1 index add 2 index 2 index sub lineto
14
      closepath pop pop pop} bind def
15
/di { newpath 2 index 1 index add 2 index moveto
16
      2 index             2 index 2 index add lineto
17
      2 index 1 index sub 2 index             lineto
18
      2 index             2 index 2 index sub lineto
19
      closepath pop pop pop} bind def
20
/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill
21
     setrgbcolor 1.1 div c fill
22
   } bind def
23
/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill
24
     setrgbcolor 1.1 div sq fill
25
   } bind def
26
/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill
27
     setrgbcolor 1.1 div di fill
28
   } bind def
29
/arrl 1 def
30
/arrw 0.3 def
31
/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def
32
/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def
33
       /w exch def /len exch def
34
       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto
35
       len w sub arrl sub dx dy lrl
36
       arrw dy dx neg lrl
37
       dx arrl w add mul dy w 2 div arrw add mul sub
38
       dy arrl w add mul dx w 2 div arrw add mul add rlineto
39
       dx arrl w add mul neg dy w 2 div arrw add mul sub
40
       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto
41
       arrw dy dx neg lrl
42
       len w sub arrl sub neg dx dy lrl
43
       closepath fill } bind def
44
/cshow { 2 index 2 index moveto dup stringwidth pop
45
         neg 2 div fosi .35 mul neg rmoveto show pop pop} def
46

	
47
gsave
48
90 rotate
49
0 -842 translate
50
71.0944 15 translate
51
0.434694 dup scale
52
90 rotate
53
860.856 -588.349 translate
54
%Edges:
55
gsave
56
574.035 177.301 622.149 225.748 670.264 274.195 0 0 0 2 lb
57
694.579 115.483 682.421 194.839 670.264 274.195 0 0 0 2 lb
58
280.402 10.3938 246.402 -6.60595 212.403 -23.6057 0 0 0 2 lb
59
280.402 10.3938 283.493 -18.9695 286.584 -48.3327 0 0 0 2 lb
60
212.403 -23.6057 249.493 -35.9692 286.584 -48.3327 0 0 0 2 lb
61
286.584 -48.3327 326.765 -79.2414 366.947 -110.15 0 0 0 2 lb
62
286.584 -48.3327 278.857 -111.695 271.13 -175.058 0 0 0 2 lb
63
438.037 -88.514 417.946 -142.604 397.855 -196.694 0 0 0 2 lb
64
438.037 -88.514 402.492 -99.332 366.947 -110.15 0 0 0 2 lb
65
397.855 -196.694 382.401 -153.422 366.947 -110.15 0 0 0 2 lb
66
366.947 -110.15 319.038 -142.604 271.13 -175.058 0 0 0 2 lb
67
271.13 -175.058 274.221 -213.694 277.311 -252.33 0 0 0 2 lb
68
271.13 -175.058 238.675 -190.512 206.221 -205.967 0 0 0 2 lb
69
277.311 -252.33 241.766 -229.149 206.221 -205.967 0 0 0 2 lb
70
-840.856 -246.718 -804.351 -66.7145 -767.847 113.289 0 0 0 2 lb
71
-579.033 445.603 -673.44 279.446 -767.847 113.289 0 0 0 2 lb
72
-579.033 445.603 -524.906 302.104 -470.779 158.605 0 0 0 2 lb
73
-767.847 113.289 -619.313 135.947 -470.779 158.605 0 0 0 2 lb
74
906.312 201.403 946.592 42.798 986.873 -115.807 0 0 0 2 lb
75
906.312 201.403 834.562 91.8901 762.812 -17.6227 0 0 0 2 lb
76
986.873 -115.807 874.842 -66.7148 762.812 -17.6227 0 0 0 2 lb
77
-470.779 158.605 -390.218 50.3508 -309.657 -57.9033 0 0 0 2 lb
78
422.945 521.129 208.955 541.269 -5.03507 561.41 0 0 0 2 lb
79
422.945 521.129 376.371 417.911 329.797 314.692 0 0 0 2 lb
80
422.945 521.129 474.554 276.928 526.164 32.7279 0 0 0 2 lb
81
-5.03507 561.41 -36.5042 440.568 -67.9734 319.727 0 0 0 2 lb
82
329.797 314.692 130.912 317.209 -67.9734 319.727 0 0 0 2 lb
83
-67.9734 319.727 229.095 176.227 526.164 32.7279 0 0 0 2 lb
84
762.812 -17.6227 644.488 7.5526 526.164 32.7279 0 0 0 2 lb
85
762.812 -17.6227 746.448 -162.381 730.084 -307.139 0 0 0 2 lb
86
526.164 32.7279 470.779 -128.394 415.393 -289.516 0 0 0 2 lb
87
730.084 -307.139 572.738 -298.327 415.393 -289.516 0 0 0 2 lb
88
415.393 -289.516 173.71 -318.468 -67.9734 -347.42 0 0 0 2 lb
89
-67.9734 -347.42 -188.815 -202.662 -309.657 -57.9033 0 0 0 2 lb
90
-67.9734 -347.42 -195.758 -390.692 -323.543 -433.964 0 0 0 2 lb
91
-309.657 -57.9033 -424.775 -160.272 -539.894 -262.64 0 0 0 2 lb
92
-323.543 -433.964 -431.719 -348.302 -539.894 -262.64 0 0 0 2 lb
93
-26.6953 -19.9585 44.8558 -96.8093 116.407 -173.66 0 0 0 2 lb
94
-26.6953 -19.9585 87.2563 9.19185 201.208 38.3422 0 0 0 2 lb
95
-26.6953 -19.9585 -144.622 43.6422 -262.548 107.243 0 0 0 2 lb
96
-26.6953 -19.9585 -20.0703 56.8923 -13.4452 133.743 0 0 0 2 lb
97
116.407 -173.66 158.808 -67.6589 201.208 38.3422 0 0 0 2 lb
98
-262.548 107.243 -137.997 120.493 -13.4452 133.743 0 0 0 2 lb
99
-262.548 107.243 -221.472 176.144 -180.397 245.045 0 0 0 2 lb
100
-13.4452 133.743 -96.9211 189.394 -180.397 245.045 0 0 0 2 lb
101
-180.397 245.045 -142.256 345.099 -132.697 451.748 0 0 0 2 lb
102
-180.397 245.045 -170.838 351.694 -132.697 451.748 0 0 0 2 lb
103
-416.25 345.746 -274.474 398.747 -132.697 451.748 0 0 0 2 lb
104
-416.25 345.746 -393.725 457.048 -371.2 568.349 0 0 0 2 lb
105
-132.697 451.748 -251.948 510.048 -371.2 568.349 0 0 0 2 lb
106
670.264 274.195 629.188 409.347 588.113 544.499 0 0 0 2 lb
107
670.264 274.195 797.466 341.771 924.667 409.347 0 0 0 2 lb
108
588.113 544.499 756.39 476.923 924.667 409.347 0 0 0 2 lb
109
-689.204 -237.261 -614.799 -102.648 -567.302 43.6423 0 0 0 2 lb
110
-689.204 -237.261 -641.707 -90.9706 -567.302 43.6423 0 0 0 2 lb
111
grestore
112
%Nodes:
113
gsave
114
-567.302 43.6423 20 0 0 0 nc
115
-689.204 -237.261 20 0 0 0 nc
116
924.667 409.347 20 1 0 0 nc
117
588.113 544.499 20 1 0 0 nc
118
670.264 274.195 20 1 0 0 nc
119
-371.2 568.349 20 0 1 0 nc
120
-132.697 451.748 20 0 1 0 nc
121
-416.25 345.746 20 0 1 0 nc
122
-180.397 245.045 20 0 1 0 nc
123
-13.4452 133.743 20 0 1 0 nc
124
-262.548 107.243 20 0 1 0 nc
125
201.208 38.3422 20 0 1 0 nc
126
116.407 -173.66 20 0 1 0 nc
127
-26.6953 -19.9585 20 0 1 0 nc
128
-539.894 -262.64 20 0 0 1 nc
129
-323.543 -433.964 20 0 0 1 nc
130
-309.657 -57.9033 20 0 0 1 nc
131
-67.9734 -347.42 20 0 0 1 nc
132
415.393 -289.516 20 0 0 1 nc
133
730.084 -307.139 20 0 0 1 nc
134
526.164 32.7279 20 0 0 1 nc
135
762.812 -17.6227 20 0 0 1 nc
136
-67.9734 319.727 20 0 0 1 nc
137
329.797 314.692 20 0 0 1 nc
138
-5.03507 561.41 20 0 0 1 nc
139
422.945 521.129 20 0 0 1 nc
140
-470.779 158.605 20 0 0 1 nc
141
986.873 -115.807 20 0 0 1 nc
142
906.312 201.403 20 0 0 1 nc
143
-767.847 113.289 20 0 0 1 nc
144
-579.033 445.603 20 0 0 1 nc
145
-840.856 -246.718 20 0 0 1 nc
146
206.221 -205.967 20 1 1 0 nc
147
277.311 -252.33 20 1 1 0 nc
148
271.13 -175.058 20 1 1 0 nc
149
366.947 -110.15 20 1 1 0 nc
150
397.855 -196.694 20 1 1 0 nc
151
438.037 -88.514 20 1 1 0 nc
152
286.584 -48.3327 20 1 1 0 nc
153
212.403 -23.6057 20 1 1 0 nc
154
280.402 10.3938 20 1 1 0 nc
155
694.579 115.483 20 1 0 0 nc
156
574.035 177.301 20 1 0 0 nc
157
grestore
158
grestore
159
showpage
1
%!PS-Adobe-2.0 EPSF-2.0
2
%%Creator: LEMON, graphToEps()
3
%%CreationDate: Fri Nov  4 13:47:12 2005
4
%%BoundingBox: 0 0 842 596
5
%%EndComments
6
/lb { setlinewidth setrgbcolor newpath moveto
7
      4 2 roll 1 index 1 index curveto stroke } bind def
8
/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def
9
/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def
10
/sq { newpath 2 index 1 index add 2 index 2 index add moveto
11
      2 index 1 index sub 2 index 2 index add lineto
12
      2 index 1 index sub 2 index 2 index sub lineto
13
      2 index 1 index add 2 index 2 index sub lineto
14
      closepath pop pop pop} bind def
15
/di { newpath 2 index 1 index add 2 index moveto
16
      2 index             2 index 2 index add lineto
17
      2 index 1 index sub 2 index             lineto
18
      2 index             2 index 2 index sub lineto
19
      closepath pop pop pop} bind def
20
/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill
21
     setrgbcolor 1.1 div c fill
22
   } bind def
23
/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill
24
     setrgbcolor 1.1 div sq fill
25
   } bind def
26
/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill
27
     setrgbcolor 1.1 div di fill
28
   } bind def
29
/arrl 1 def
30
/arrw 0.3 def
31
/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def
32
/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def
33
       /w exch def /len exch def
34
       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto
35
       len w sub arrl sub dx dy lrl
36
       arrw dy dx neg lrl
37
       dx arrl w add mul dy w 2 div arrw add mul sub
38
       dy arrl w add mul dx w 2 div arrw add mul add rlineto
39
       dx arrl w add mul neg dy w 2 div arrw add mul sub
40
       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto
41
       arrw dy dx neg lrl
42
       len w sub arrl sub neg dx dy lrl
43
       closepath fill } bind def
44
/cshow { 2 index 2 index moveto dup stringwidth pop
45
         neg 2 div fosi .35 mul neg rmoveto show pop pop} def
46

	
47
gsave
48
90 rotate
49
0 -842 translate
50
71.0944 15 translate
51
0.434694 dup scale
52
90 rotate
53
860.856 -588.349 translate
54
%Edges:
55
gsave
56
574.035 177.301 622.149 225.748 670.264 274.195 1 0 0 2 lb
57
694.579 115.483 682.421 194.839 670.264 274.195 1 0 0 2 lb
58
280.402 10.3938 246.402 -6.60595 212.403 -23.6057 0 0 1 2 lb
59
280.402 10.3938 283.493 -18.9695 286.584 -48.3327 0 0 1 2 lb
60
212.403 -23.6057 249.493 -35.9692 286.584 -48.3327 0 0 1 2 lb
61
286.584 -48.3327 326.765 -79.2414 366.947 -110.15 0 0 1 2 lb
62
286.584 -48.3327 278.857 -111.695 271.13 -175.058 0 0 1 2 lb
63
438.037 -88.514 417.946 -142.604 397.855 -196.694 0 0 1 2 lb
64
438.037 -88.514 402.492 -99.332 366.947 -110.15 0 0 1 2 lb
65
397.855 -196.694 382.401 -153.422 366.947 -110.15 0 0 1 2 lb
66
366.947 -110.15 319.038 -142.604 271.13 -175.058 0 0 1 2 lb
67
271.13 -175.058 274.221 -213.694 277.311 -252.33 0 0 1 2 lb
68
271.13 -175.058 238.675 -190.512 206.221 -205.967 0 0 1 2 lb
69
277.311 -252.33 241.766 -229.149 206.221 -205.967 0 0 1 2 lb
70
-840.856 -246.718 -804.351 -66.7145 -767.847 113.289 1 0 0 2 lb
71
-579.033 445.603 -673.44 279.446 -767.847 113.289 0 0 1 2 lb
72
-579.033 445.603 -524.906 302.104 -470.779 158.605 0 0 1 2 lb
73
-767.847 113.289 -619.313 135.947 -470.779 158.605 0 0 1 2 lb
74
906.312 201.403 946.592 42.798 986.873 -115.807 0 0 1 2 lb
75
906.312 201.403 834.562 91.8901 762.812 -17.6227 0 0 1 2 lb
76
986.873 -115.807 874.842 -66.7148 762.812 -17.6227 0 0 1 2 lb
77
-470.779 158.605 -390.218 50.3508 -309.657 -57.9033 1 0 0 2 lb
78
422.945 521.129 208.955 541.269 -5.03507 561.41 0 0 1 2 lb
79
422.945 521.129 376.371 417.911 329.797 314.692 0 0 1 2 lb
80
422.945 521.129 474.554 276.928 526.164 32.7279 0 0 1 2 lb
81
-5.03507 561.41 -36.5042 440.568 -67.9734 319.727 0 0 1 2 lb
82
329.797 314.692 130.912 317.209 -67.9734 319.727 0 0 1 2 lb
83
-67.9734 319.727 229.095 176.227 526.164 32.7279 0 0 1 2 lb
84
762.812 -17.6227 644.488 7.5526 526.164 32.7279 0 0 1 2 lb
85
762.812 -17.6227 746.448 -162.381 730.084 -307.139 0 0 1 2 lb
86
526.164 32.7279 470.779 -128.394 415.393 -289.516 0 0 1 2 lb
87
730.084 -307.139 572.738 -298.327 415.393 -289.516 0 0 1 2 lb
88
415.393 -289.516 173.71 -318.468 -67.9734 -347.42 1 0 0 2 lb
89
-67.9734 -347.42 -188.815 -202.662 -309.657 -57.9033 0 0 1 2 lb
90
-67.9734 -347.42 -195.758 -390.692 -323.543 -433.964 0 0 1 2 lb
91
-309.657 -57.9033 -424.775 -160.272 -539.894 -262.64 0 0 1 2 lb
92
-323.543 -433.964 -431.719 -348.302 -539.894 -262.64 0 0 1 2 lb
93
-26.6953 -19.9585 44.8558 -96.8093 116.407 -173.66 0 0 1 2 lb
94
-26.6953 -19.9585 87.2563 9.19185 201.208 38.3422 0 0 1 2 lb
95
-26.6953 -19.9585 -144.622 43.6422 -262.548 107.243 0 0 1 2 lb
96
-26.6953 -19.9585 -20.0703 56.8923 -13.4452 133.743 0 0 1 2 lb
97
116.407 -173.66 158.808 -67.6589 201.208 38.3422 0 0 1 2 lb
98
-262.548 107.243 -137.997 120.493 -13.4452 133.743 0 0 1 2 lb
99
-262.548 107.243 -221.472 176.144 -180.397 245.045 0 0 1 2 lb
100
-13.4452 133.743 -96.9211 189.394 -180.397 245.045 0 0 1 2 lb
101
-180.397 245.045 -142.256 345.099 -132.697 451.748 0 0 1 2 lb
102
-180.397 245.045 -170.838 351.694 -132.697 451.748 0 0 1 2 lb
103
-416.25 345.746 -274.474 398.747 -132.697 451.748 0 0 1 2 lb
104
-416.25 345.746 -393.725 457.048 -371.2 568.349 0 0 1 2 lb
105
-132.697 451.748 -251.948 510.048 -371.2 568.349 0 0 1 2 lb
106
670.264 274.195 629.188 409.347 588.113 544.499 0 0 1 2 lb
107
670.264 274.195 797.466 341.771 924.667 409.347 0 0 1 2 lb
108
588.113 544.499 756.39 476.923 924.667 409.347 0 0 1 2 lb
109
-689.204 -237.261 -614.799 -102.648 -567.302 43.6423 0 0 1 2 lb
110
-689.204 -237.261 -641.707 -90.9706 -567.302 43.6423 0 0 1 2 lb
111
grestore
112
%Nodes:
113
gsave
114
-567.302 43.6423 20 0 0 0 nc
115
-689.204 -237.261 20 0 0 0 nc
116
924.667 409.347 20 0 0 1 nc
117
588.113 544.499 20 0 0 1 nc
118
670.264 274.195 20 0 0 1 nc
119
-371.2 568.349 20 1 1 0 nc
120
-132.697 451.748 20 1 1 0 nc
121
-416.25 345.746 20 1 1 0 nc
122
-180.397 245.045 20 1 1 0 nc
123
-13.4452 133.743 20 1 1 0 nc
124
-262.548 107.243 20 1 1 0 nc
125
201.208 38.3422 20 1 1 0 nc
126
116.407 -173.66 20 1 1 0 nc
127
-26.6953 -19.9585 20 1 1 0 nc
128
-539.894 -262.64 20 0 0.5 0 nc
129
-323.543 -433.964 20 0 0.5 0 nc
130
-309.657 -57.9033 20 0 0.5 0 nc
131
-67.9734 -347.42 20 0 0.5 0 nc
132
415.393 -289.516 20 0.5 0 0 nc
133
730.084 -307.139 20 0.5 0 0 nc
134
526.164 32.7279 20 0.5 0 0 nc
135
762.812 -17.6227 20 0.5 0 0 nc
136
-67.9734 319.727 20 0.5 0 0 nc
137
329.797 314.692 20 0.5 0 0 nc
138
-5.03507 561.41 20 0.5 0 0 nc
139
422.945 521.129 20 0.5 0 0 nc
140
-470.779 158.605 20 0 1 1 nc
141
986.873 -115.807 20 0.5 0 0 nc
142
906.312 201.403 20 0.5 0 0 nc
143
-767.847 113.289 20 0 1 1 nc
144
-579.033 445.603 20 0 1 1 nc
145
-840.856 -246.718 20 1 0 1 nc
146
206.221 -205.967 20 0 0 0.5 nc
147
277.311 -252.33 20 0 0 0.5 nc
148
271.13 -175.058 20 0 0 0.5 nc
149
366.947 -110.15 20 0 0 0.5 nc
150
397.855 -196.694 20 0 0 0.5 nc
151
438.037 -88.514 20 0 0 0.5 nc
152
286.584 -48.3327 20 0 0 0.5 nc
153
212.403 -23.6057 20 0 0 0.5 nc
154
280.402 10.3938 20 0 0 0.5 nc
155
694.579 115.483 20 1 0 0 nc
156
574.035 177.301 20 0 1 0 nc
157
grestore
158
grestore
159
showpage
1
%!PS-Adobe-2.0 EPSF-2.0
2
%%Creator: LEMON, graphToEps()
3
%%CreationDate: Fri Nov  4 13:47:12 2005
4
%%BoundingBox: 0 0 842 596
5
%%EndComments
6
/lb { setlinewidth setrgbcolor newpath moveto
7
      4 2 roll 1 index 1 index curveto stroke } bind def
8
/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def
9
/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def
10
/sq { newpath 2 index 1 index add 2 index 2 index add moveto
11
      2 index 1 index sub 2 index 2 index add lineto
12
      2 index 1 index sub 2 index 2 index sub lineto
13
      2 index 1 index add 2 index 2 index sub lineto
14
      closepath pop pop pop} bind def
15
/di { newpath 2 index 1 index add 2 index moveto
16
      2 index             2 index 2 index add lineto
17
      2 index 1 index sub 2 index             lineto
18
      2 index             2 index 2 index sub lineto
19
      closepath pop pop pop} bind def
20
/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill
21
     setrgbcolor 1.1 div c fill
22
   } bind def
23
/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill
24
     setrgbcolor 1.1 div sq fill
25
   } bind def
26
/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill
27
     setrgbcolor 1.1 div di fill
28
   } bind def
29
/arrl 1 def
30
/arrw 0.3 def
31
/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def
32
/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def
33
       /w exch def /len exch def
34
       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto
35
       len w sub arrl sub dx dy lrl
36
       arrw dy dx neg lrl
37
       dx arrl w add mul dy w 2 div arrw add mul sub
38
       dy arrl w add mul dx w 2 div arrw add mul add rlineto
39
       dx arrl w add mul neg dy w 2 div arrw add mul sub
40
       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto
41
       arrw dy dx neg lrl
42
       len w sub arrl sub neg dx dy lrl
43
       closepath fill } bind def
44
/cshow { 2 index 2 index moveto dup stringwidth pop
45
         neg 2 div fosi .35 mul neg rmoveto show pop pop} def
46

	
47
gsave
48
90 rotate
49
0 -842 translate
50
71.0944 15 translate
51
0.434694 dup scale
52
90 rotate
53
860.856 -588.349 translate
54
%Edges:
55
gsave
56
574.035 177.301 622.149 225.748 670.264 274.195 0 1 0 5 lb
57
694.579 115.483 682.421 194.839 670.264 274.195 1 0 0 5 lb
58
280.402 10.3938 246.402 -6.60595 212.403 -23.6057 1 1 0.5 5 lb
59
280.402 10.3938 283.493 -18.9695 286.584 -48.3327 1 1 0.5 5 lb
60
212.403 -23.6057 249.493 -35.9692 286.584 -48.3327 1 1 0.5 5 lb
61
286.584 -48.3327 326.765 -79.2414 366.947 -110.15 1 0.5 1 5 lb
62
286.584 -48.3327 278.857 -111.695 271.13 -175.058 1 0.5 1 5 lb
63
438.037 -88.514 417.946 -142.604 397.855 -196.694 0.5 0.5 1 5 lb
64
438.037 -88.514 402.492 -99.332 366.947 -110.15 0.5 0.5 1 5 lb
65
397.855 -196.694 382.401 -153.422 366.947 -110.15 0.5 0.5 1 5 lb
66
366.947 -110.15 319.038 -142.604 271.13 -175.058 1 0.5 1 5 lb
67
271.13 -175.058 274.221 -213.694 277.311 -252.33 0.5 1 1 5 lb
68
271.13 -175.058 238.675 -190.512 206.221 -205.967 0.5 1 1 5 lb
69
277.311 -252.33 241.766 -229.149 206.221 -205.967 0.5 1 1 5 lb
70
-840.856 -246.718 -804.351 -66.7145 -767.847 113.289 0 0.5 0 5 lb
71
-579.033 445.603 -673.44 279.446 -767.847 113.289 0 0 0.5 5 lb
72
-579.033 445.603 -524.906 302.104 -470.779 158.605 0 0 0.5 5 lb
73
-767.847 113.289 -619.313 135.947 -470.779 158.605 0 0 0.5 5 lb
74
906.312 201.403 946.592 42.798 986.873 -115.807 0 0.5 0.5 5 lb
75
906.312 201.403 834.562 91.8901 762.812 -17.6227 0 0.5 0.5 5 lb
76
986.873 -115.807 874.842 -66.7148 762.812 -17.6227 0 0.5 0.5 5 lb
77
-470.779 158.605 -390.218 50.3508 -309.657 -57.9033 0.5 0.5 0 5 lb
78
422.945 521.129 208.955 541.269 -5.03507 561.41 0.5 0 0.5 5 lb
79
422.945 521.129 376.371 417.911 329.797 314.692 0.5 0 0.5 5 lb
80
422.945 521.129 474.554 276.928 526.164 32.7279 0.5 0 0.5 5 lb
81
-5.03507 561.41 -36.5042 440.568 -67.9734 319.727 0.5 0 0.5 5 lb
82
329.797 314.692 130.912 317.209 -67.9734 319.727 0.5 0 0.5 5 lb
83
-67.9734 319.727 229.095 176.227 526.164 32.7279 0.5 0 0.5 5 lb
84
762.812 -17.6227 644.488 7.5526 526.164 32.7279 0.5 0.5 0.5 5 lb
85
762.812 -17.6227 746.448 -162.381 730.084 -307.139 0.5 0.5 0.5 5 lb
86
526.164 32.7279 470.779 -128.394 415.393 -289.516 0.5 0.5 0.5 5 lb
87
730.084 -307.139 572.738 -298.327 415.393 -289.516 0.5 0.5 0.5 5 lb
88
415.393 -289.516 173.71 -318.468 -67.9734 -347.42 1 0.5 0.5 5 lb
89
-67.9734 -347.42 -188.815 -202.662 -309.657 -57.9033 0.5 1 0.5 5 lb
90
-67.9734 -347.42 -195.758 -390.692 -323.543 -433.964 0.5 1 0.5 5 lb
91
-309.657 -57.9033 -424.775 -160.272 -539.894 -262.64 0.5 1 0.5 5 lb
92
-323.543 -433.964 -431.719 -348.302 -539.894 -262.64 0.5 1 0.5 5 lb
93
-26.6953 -19.9585 44.8558 -96.8093 116.407 -173.66 1 1 0 5 lb
94
-26.6953 -19.9585 87.2563 9.19185 201.208 38.3422 1 1 0 5 lb
95
-26.6953 -19.9585 -144.622 43.6422 -262.548 107.243 1 0 1 5 lb
96
-26.6953 -19.9585 -20.0703 56.8923 -13.4452 133.743 1 0 1 5 lb
97
116.407 -173.66 158.808 -67.6589 201.208 38.3422 1 1 0 5 lb
98
-262.548 107.243 -137.997 120.493 -13.4452 133.743 1 0 1 5 lb
99
-262.548 107.243 -221.472 176.144 -180.397 245.045 1 0 1 5 lb
100
-13.4452 133.743 -96.9211 189.394 -180.397 245.045 1 0 1 5 lb
101
-180.397 245.045 -140.307 344.649 -132.697 451.748 0 1 1 5 lb
102
-180.397 245.045 -172.787 352.144 -132.697 451.748 0 1 1 5 lb
103
-416.25 345.746 -274.474 398.747 -132.697 451.748 0.5 0 0 5 lb
104
-416.25 345.746 -393.725 457.048 -371.2 568.349 0.5 0 0 5 lb
105
-132.697 451.748 -251.948 510.048 -371.2 568.349 0.5 0 0 5 lb
106
670.264 274.195 629.188 409.347 588.113 544.499 0 0 1 5 lb
107
670.264 274.195 797.466 341.771 924.667 409.347 0 0 1 5 lb
108
588.113 544.499 756.39 476.923 924.667 409.347 0 0 1 5 lb
109
-689.204 -237.261 -612.964 -103.444 -567.302 43.6423 0 0 0 5 lb
110
-689.204 -237.261 -643.542 -90.1744 -567.302 43.6423 0 0 0 5 lb
111
grestore
112
%Nodes:
113
gsave
114
-567.302 43.6423 20 0 0 1 nc
115
-689.204 -237.261 20 0 0 1 nc
116
924.667 409.347 20 0 0 1 nc
117
588.113 544.499 20 0 0 1 nc
118
670.264 274.195 20 1 0 0 nc
119
-371.2 568.349 20 0 0 1 nc
120
-132.697 451.748 20 1 0 0 nc
121
-416.25 345.746 20 0 0 1 nc
122
-180.397 245.045 20 1 0 0 nc
123
-13.4452 133.743 20 0 0 1 nc
124
-262.548 107.243 20 0 0 1 nc
125
201.208 38.3422 20 0 0 1 nc
126
116.407 -173.66 20 0 0 1 nc
127
-26.6953 -19.9585 20 1 0 0 nc
128
-539.894 -262.64 20 0 0 1 nc
129
-323.543 -433.964 20 0 0 1 nc
130
-309.657 -57.9033 20 1 0 0 nc
131
-67.9734 -347.42 20 1 0 0 nc
132
415.393 -289.516 20 1 0 0 nc
133
730.084 -307.139 20 0 0 1 nc
134
526.164 32.7279 20 1 0 0 nc
135
762.812 -17.6227 20 1 0 0 nc
136
-67.9734 319.727 20 0 0 1 nc
137
329.797 314.692 20 0 0 1 nc
138
-5.03507 561.41 20 0 0 1 nc
139
422.945 521.129 20 0 0 1 nc
140
-470.779 158.605 20 1 0 0 nc
141
986.873 -115.807 20 0 0 1 nc
142
906.312 201.403 20 0 0 1 nc
143
-767.847 113.289 20 1 0 0 nc
144
-579.033 445.603 20 0 0 1 nc
145
-840.856 -246.718 20 0 0 1 nc
146
206.221 -205.967 20 0 0 1 nc
147
277.311 -252.33 20 0 0 1 nc
148
271.13 -175.058 20 1 0 0 nc
149
366.947 -110.15 20 1 0 0 nc
150
397.855 -196.694 20 0 0 1 nc
151
438.037 -88.514 20 0 0 1 nc
152
286.584 -48.3327 20 1 0 0 nc
153
212.403 -23.6057 20 0 0 1 nc
154
280.402 10.3938 20 0 0 1 nc
155
694.579 115.483 20 0 0 1 nc
156
574.035 177.301 20 0 0 1 nc
157
grestore
158
grestore
159
showpage
1
%!PS-Adobe-2.0 EPSF-2.0
2
%%Creator: LEMON, graphToEps()
3
%%CreationDate: Fri Nov  4 13:47:12 2005
4
%%BoundingBox: 0 0 842 596
5
%%EndComments
6
/lb { setlinewidth setrgbcolor newpath moveto
7
      4 2 roll 1 index 1 index curveto stroke } bind def
8
/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def
9
/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def
10
/sq { newpath 2 index 1 index add 2 index 2 index add moveto
11
      2 index 1 index sub 2 index 2 index add lineto
12
      2 index 1 index sub 2 index 2 index sub lineto
13
      2 index 1 index add 2 index 2 index sub lineto
14
      closepath pop pop pop} bind def
15
/di { newpath 2 index 1 index add 2 index moveto
16
      2 index             2 index 2 index add lineto
17
      2 index 1 index sub 2 index             lineto
18
      2 index             2 index 2 index sub lineto
19
      closepath pop pop pop} bind def
20
/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill
21
     setrgbcolor 1.1 div c fill
22
   } bind def
23
/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill
24
     setrgbcolor 1.1 div sq fill
25
   } bind def
26
/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill
27
     setrgbcolor 1.1 div di fill
28
   } bind def
29
/arrl 10 def
30
/arrw 3 def
31
/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def
32
/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def
33
       /w exch def /len exch def
34
       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto
35
       len w sub arrl sub dx dy lrl
36
       arrw dy dx neg lrl
37
       dx arrl w add mul dy w 2 div arrw add mul sub
38
       dy arrl w add mul dx w 2 div arrw add mul add rlineto
39
       dx arrl w add mul neg dy w 2 div arrw add mul sub
40
       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto
41
       arrw dy dx neg lrl
42
       len w sub arrl sub neg dx dy lrl
43
       closepath fill } bind def
44
/cshow { 2 index 2 index moveto dup stringwidth pop
45
         neg 2 div fosi .35 mul neg rmoveto show pop pop} def
46

	
47
gsave
48
90 rotate
49
0 -842 translate
50
77.1122 15 translate
51
0.585745 dup scale
52
90 rotate
53
695.963 -397.916 translate
54
%Edges:
55
gsave
56
2 setlinewidth 0 0 1 setrgbcolor newpath
57
218.178 27.2723 moveto
58
192.373 -40.1551 188.622 -49.9556 169.228 -100.631 curveto stroke
59
newpath 164.939 -111.838 moveto 165.492 -99.2013 lineto 172.964 -102.061 lineto closepath fill
60
2 setlinewidth 0 0 1 setrgbcolor newpath
61
44.8044 15.5841 moveto
62
119.293 20.6059 129.775 21.3125 186.25 25.1199 curveto stroke
63
newpath 198.223 25.927 moveto 186.519 21.1289 lineto 185.981 29.1108 lineto closepath fill
64
2 setlinewidth 1 0 0 setrgbcolor newpath
65
218.178 27.2723 moveto
66
285.395 -87.4449 290.763 -96.6058 348.102 -194.464 curveto stroke
67
newpath 354.169 -204.818 moveto 344.651 -196.487 lineto 351.554 -192.442 lineto closepath fill
68
2 setlinewidth 0 0 1 setrgbcolor newpath
69
157.79 -130.517 moveto
70
108.71 -67.0521 102.27 -58.7243 64.3804 -9.72954 curveto stroke
71
newpath 57.0394 -0.236898 moveto 67.5446 -7.28254 lineto 61.2162 -12.1765 lineto closepath fill
72
2 setlinewidth 1 0 0 setrgbcolor newpath
73
-105.193 -261.035 moveto
74
-35.6576 -132.801 -30.5923 -123.459 29.5506 -12.5464 curveto stroke
75
newpath 35.2708 -1.99743 moveto 33.0669 -14.4531 lineto 26.0343 -10.6397 lineto closepath fill
76
2 setlinewidth 0 0 1 setrgbcolor newpath
77
-465.576 -42.8564 moveto
78
-559.078 -25.5413 -569.47 -23.6169 -644.498 -9.72286 curveto stroke
79
newpath -656.297 -7.5378 moveto -643.77 -5.78973 lineto -645.226 -13.656 lineto closepath fill
80
2 setlinewidth 0 0 1 setrgbcolor newpath
81
-574.666 -153.893 moveto
82
-528.842 -107.252 -521.515 -99.794 -488.002 -65.683 curveto stroke
83
newpath -479.592 -57.123 moveto -485.149 -68.4863 lineto -490.856 -62.8797 lineto closepath fill
84
2 setlinewidth 1 0 0 setrgbcolor newpath
85
-490.901 120.777 moveto
86
-480.122 51.1328 -478.519 40.7713 -470.47 -11.2329 curveto stroke
87
newpath -468.635 -23.0917 moveto -474.423 -11.8447 lineto -466.517 -10.6212 lineto closepath fill
88
2 setlinewidth 0 0 1 setrgbcolor newpath
89
-675.963 -3.89604 moveto
90
-632.116 -68.8235 -626.228 -77.5422 -592.575 -127.374 curveto stroke
91
newpath -585.859 -137.319 moveto -595.89 -129.612 lineto -589.26 -125.135 lineto closepath fill
92
2 setlinewidth 0 0 1 setrgbcolor newpath
93
-490.901 120.777 moveto
94
-435.445 215.844 -430.107 224.995 -384.3 303.522 curveto stroke
95
newpath -378.253 313.887 moveto -380.845 301.507 lineto -387.755 305.537 lineto closepath fill
96
2 setlinewidth 0 0 1 setrgbcolor newpath
97
-266.879 114.933 moveto
98
-367.067 117.547 -377.642 117.822 -458.912 119.943 curveto stroke
99
newpath -470.908 120.255 moveto -458.807 123.941 lineto -459.016 115.944 lineto closepath fill
100
2 setlinewidth 0 0 1 setrgbcolor newpath
101
-368.176 331.163 moveto
102
-322.511 233.685 -318.018 224.095 -280.454 143.911 curveto stroke
103
newpath -275.364 133.044 moveto -284.076 142.214 lineto -276.832 145.608 lineto closepath fill
104
2 setlinewidth 1 0 0 setrgbcolor newpath
105
-266.879 114.933 moveto
106
-224.004 235.52 -220.448 245.52 -184.094 347.765 curveto stroke
107
newpath -180.074 359.072 moveto -180.325 346.425 lineto -187.863 349.105 lineto closepath fill
108
2 setlinewidth 0 0 1 setrgbcolor newpath
109
-251.294 -335.059 moveto
110
-189.25 -303.624 -179.902 -298.887 -133.738 -275.498 curveto stroke
111
newpath -123.034 -270.074 moveto -131.93 -279.066 lineto -135.546 -271.93 lineto closepath fill
112
2 setlinewidth 0 0 1 setrgbcolor newpath
113
-389.604 -136.361 moveto
114
-327.15 -226.083 -321.098 -234.777 -269.576 -308.795 curveto stroke
115
newpath -262.72 -318.644 moveto -272.859 -311.081 lineto -266.293 -306.51 lineto closepath fill
116
2 setlinewidth 1 0 0 setrgbcolor newpath
117
5.84406 175.322 moveto
118
-76.0754 267.926 -83.1051 275.873 -152.172 353.948 curveto stroke
119
newpath -160.122 362.936 moveto -149.176 356.598 lineto -155.168 351.298 lineto closepath fill
120
2 setlinewidth 0 0 1 setrgbcolor newpath
121
169.478 311.683 moveto
122
96.8003 251.119 88.6819 244.353 30.4273 195.808 curveto stroke
123
newpath 21.2086 188.126 moveto 27.8666 198.881 lineto 32.988 192.735 lineto closepath fill
124
2 setlinewidth 0 0 1 setrgbcolor newpath
125
342.851 111.037 moveto
126
263.766 202.563 256.831 210.589 190.4 287.47 curveto stroke
127
newpath 182.554 296.55 moveto 193.427 290.085 lineto 187.373 284.855 lineto closepath fill
128
2 setlinewidth 0 0 1 setrgbcolor newpath
129
5.84406 175.322 moveto
130
163.16 145.314 173.605 143.321 311.418 117.033 curveto stroke
131
newpath 323.205 114.784 moveto 310.668 113.104 lineto 312.167 120.962 lineto closepath fill
132
2 setlinewidth 0 0 1 setrgbcolor newpath
133
342.851 111.037 moveto
134
497.255 2.58683 505.964 -3.53033 643.932 -100.436 curveto stroke
135
newpath 653.752 -107.334 moveto 641.633 -103.71 lineto 646.231 -97.163 lineto closepath fill
136
2 setlinewidth 0 0 1 setrgbcolor newpath
137
364.28 -222.074 moveto
138
354.298 -66.9063 353.616 -56.2971 344.905 79.1029 curveto stroke
139
newpath 344.135 91.0781 moveto 348.897 79.3597 lineto 340.914 78.8461 lineto closepath fill
140
2 setlinewidth 0 0 1 setrgbcolor newpath
141
670.118 -118.829 moveto
142
528.037 -166.793 517.967 -170.192 394.599 -211.839 curveto stroke
143
newpath 383.229 -215.677 moveto 393.32 -208.049 lineto 395.878 -215.629 lineto closepath fill
144
2 setlinewidth 1 0 0 setrgbcolor newpath
145
-105.193 -261.035 moveto
146
118.401 -242.479 129.015 -241.598 332.39 -224.721 curveto stroke
147
newpath 344.348 -223.728 moveto 332.72 -228.707 lineto 332.059 -220.734 lineto closepath fill
148
2 setlinewidth 0 0 1 setrgbcolor newpath
149
-105.193 -261.035 moveto
150
-160.867 -161.176 -166.028 -151.918 -212.336 -68.858 curveto stroke
151
newpath -218.179 -58.3769 moveto -208.842 -66.9102 lineto -215.829 -70.8058 lineto closepath fill
152
2 setlinewidth 0 0 1 setrgbcolor newpath
153
-227.918 -40.9084 moveto
154
-298.35 -82.4884 -307.42 -87.8432 -362.048 -120.093 curveto stroke
155
newpath -372.381 -126.193 moveto -364.081 -116.648 lineto -360.014 -123.537 lineto closepath fill
156
grestore
157
%Nodes:
158
gsave
159
-389.604 -136.361 20 0 1 0 nc
160
-227.918 -40.9084 20 0 1 0 nc
161
-105.193 -261.035 20 0 1 0 nc
162
364.28 -222.074 20 1 1 0 nc
163
670.118 -118.829 20 1 1 0 nc
164
342.851 111.037 20 1 1 0 nc
165
5.84406 175.322 20 1 1 0 nc
166
169.478 311.683 20 1 1 0 nc
167
-173.374 377.916 20 1 0 1 nc
168
-251.294 -335.059 20 0 1 0 nc
169
-266.879 114.933 20 0 0 0 nc
170
-368.176 331.163 20 0 0 0 nc
171
-490.901 120.777 20 0 0 0 nc
172
-574.666 -153.893 20 1 0 0 nc
173
-675.963 -3.89604 20 1 0 0 nc
174
-465.576 -42.8564 20 1 0 0 nc
175
44.8044 15.5841 20 0 0 1 nc
176
157.79 -130.517 20 0 0 1 nc
177
218.178 27.2723 20 0 0 1 nc
178
grestore
179
grestore
180
showpage
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
namespace lemon {
20

	
21
/**
22
\page min_cost_flow Minimum Cost Flow Problem
23

	
24
\section mcf_def Definition (GEQ form)
25

	
26
The \e minimum \e cost \e flow \e problem is to find a feasible flow of
27
minimum total cost from a set of supply nodes to a set of demand nodes
28
in a network with capacity constraints (lower and upper bounds)
29
and arc costs \ref amo93networkflows.
30

	
31
Formally, let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$,
32
\f$upper: A\rightarrow\mathbf{R}\cup\{+\infty\}\f$ denote the lower and
33
upper bounds for the flow values on the arcs, for which
34
\f$lower(uv) \leq upper(uv)\f$ must hold for all \f$uv\in A\f$,
35
\f$cost: A\rightarrow\mathbf{R}\f$ denotes the cost per unit flow
36
on the arcs and \f$sup: V\rightarrow\mathbf{R}\f$ denotes the
37
signed supply values of the nodes.
38
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$
39
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with
40
\f$-sup(u)\f$ demand.
41
A minimum cost flow is an \f$f: A\rightarrow\mathbf{R}\f$ solution
42
of the following optimization problem.
43

	
44
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f]
45
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \geq
46
    sup(u) \quad \forall u\in V \f]
47
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f]
48

	
49
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be
50
zero or negative in order to have a feasible solution (since the sum
51
of the expressions on the left-hand side of the inequalities is zero).
52
It means that the total demand must be greater or equal to the total
53
supply and all the supplies have to be carried out from the supply nodes,
54
but there could be demands that are not satisfied.
55
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand
56
constraints have to be satisfied with equality, i.e. all demands
57
have to be satisfied and all supplies have to be used.
58

	
59

	
60
\section mcf_algs Algorithms
61

	
62
LEMON contains several algorithms for solving this problem, for more
63
information see \ref min_cost_flow_algs "Minimum Cost Flow Algorithms".
64

	
65
A feasible solution for this problem can be found using \ref Circulation.
66

	
67

	
68
\section mcf_dual Dual Solution
69

	
70
The dual solution of the minimum cost flow problem is represented by
71
node potentials \f$\pi: V\rightarrow\mathbf{R}\f$.
72
An \f$f: A\rightarrow\mathbf{R}\f$ primal feasible solution is optimal
73
if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ node potentials
74
the following \e complementary \e slackness optimality conditions hold.
75

	
76
 - For all \f$uv\in A\f$ arcs:
77
   - if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$;
78
   - if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$;
79
   - if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$.
80
 - For all \f$u\in V\f$ nodes:
81
   - \f$\pi(u)<=0\f$;
82
   - if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$,
83
     then \f$\pi(u)=0\f$.
84
 
85
Here \f$cost^\pi(uv)\f$ denotes the \e reduced \e cost of the arc
86
\f$uv\in A\f$ with respect to the potential function \f$\pi\f$, i.e.
87
\f[ cost^\pi(uv) = cost(uv) + \pi(u) - \pi(v).\f]
88

	
89
All algorithms provide dual solution (node potentials), as well,
90
if an optimal flow is found.
91

	
92

	
93
\section mcf_eq Equality Form
94

	
95
The above \ref mcf_def "definition" is actually more general than the
96
usual formulation of the minimum cost flow problem, in which strict
97
equalities are required in the supply/demand contraints.
98

	
99
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f]
100
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) =
101
    sup(u) \quad \forall u\in V \f]
102
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f]
103

	
104
However if the sum of the supply values is zero, then these two problems
105
are equivalent.
106
The \ref min_cost_flow_algs "algorithms" in LEMON support the general
107
form, so if you need the equality form, you have to ensure this additional
108
contraint manually.
109

	
110

	
111
\section mcf_leq Opposite Inequalites (LEQ Form)
112

	
113
Another possible definition of the minimum cost flow problem is
114
when there are <em>"less or equal"</em> (LEQ) supply/demand constraints,
115
instead of the <em>"greater or equal"</em> (GEQ) constraints.
116

	
117
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f]
118
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \leq
119
    sup(u) \quad \forall u\in V \f]
120
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f]
121

	
122
It means that the total demand must be less or equal to the 
123
total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or
124
positive) and all the demands have to be satisfied, but there
125
could be supplies that are not carried out from the supply
126
nodes.
127
The equality form is also a special case of this form, of course.
128

	
129
You could easily transform this case to the \ref mcf_def "GEQ form"
130
of the problem by reversing the direction of the arcs and taking the
131
negative of the supply values (e.g. using \ref ReverseDigraph and
132
\ref NegMap adaptors).
133
However \ref NetworkSimplex algorithm also supports this form directly
134
for the sake of convenience.
135

	
136
Note that the optimality conditions for this supply constraint type are
137
slightly differ from the conditions that are discussed for the GEQ form,
138
namely the potentials have to be non-negative instead of non-positive.
139
An \f$f: A\rightarrow\mathbf{R}\f$ feasible solution of this problem
140
is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$
141
node potentials the following conditions hold.
142

	
143
 - For all \f$uv\in A\f$ arcs:
144
   - if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$;
145
   - if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$;
146
   - if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$.
147
 - For all \f$u\in V\f$ nodes:
148
   - \f$\pi(u)>=0\f$;
149
   - if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$,
150
     then \f$\pi(u)=0\f$.
151

	
152
*/
153
}
Ignore white space 6 line context
1
%%%%% Defining LEMON %%%%%
2

	
3
@misc{lemon,
4
  key =          {LEMON},
5
  title =        {{LEMON} -- {L}ibrary for {E}fficient {M}odeling and
6
                  {O}ptimization in {N}etworks},
7
  howpublished = {\url{http://lemon.cs.elte.hu/}},
8
  year =         2009
9
}
10

	
11
@misc{egres,
12
  key =          {EGRES},
13
  title =        {{EGRES} -- {E}gerv{\'a}ry {R}esearch {G}roup on
14
                  {C}ombinatorial {O}ptimization},
15
  url =          {http://www.cs.elte.hu/egres/}
16
}
17

	
18
@misc{coinor,
19
  key =          {COIN-OR},
20
  title =        {{COIN-OR} -- {C}omputational {I}nfrastructure for
21
                  {O}perations {R}esearch},
22
  url =          {http://www.coin-or.org/}
23
}
24

	
25

	
26
%%%%% Other libraries %%%%%%
27

	
28
@misc{boost,
29
  key =          {Boost},
30
  title =        {{B}oost {C++} {L}ibraries},
31
  url =          {http://www.boost.org/}
32
}
33

	
34
@book{bglbook,
35
  author =       {Jeremy G. Siek and Lee-Quan Lee and Andrew
36
                  Lumsdaine},
37
  title =        {The Boost Graph Library: User Guide and Reference
38
                  Manual},
39
  publisher =    {Addison-Wesley},
40
  year =         2002
41
}
42

	
43
@misc{leda,
44
  key =          {LEDA},
45
  title =        {{LEDA} -- {L}ibrary of {E}fficient {D}ata {T}ypes and
46
                  {A}lgorithms},
47
  url =          {http://www.algorithmic-solutions.com/}
48
}
49

	
50
@book{ledabook,
51
  author =       {Kurt Mehlhorn and Stefan N{\"a}her},
52
  title =        {{LEDA}: {A} platform for combinatorial and geometric
53
                  computing},
54
  isbn =         {0-521-56329-1},
55
  publisher =    {Cambridge University Press},
56
  address =      {New York, NY, USA},
57
  year =         1999
58
}
59

	
60

	
61
%%%%% Tools that LEMON depends on %%%%%
62

	
63
@misc{cmake,
64
  key =          {CMake},
65
  title =        {{CMake} -- {C}ross {P}latform {M}ake},
66
  url =          {http://www.cmake.org/}
67
}
68

	
69
@misc{doxygen,
70
  key =          {Doxygen},
71
  title =        {{Doxygen} -- {S}ource code documentation generator
72
                  tool},
73
  url =          {http://www.doxygen.org/}
74
}
75

	
76

	
77
%%%%% LP/MIP libraries %%%%%
78

	
79
@misc{glpk,
80
  key =          {GLPK},
81
  title =        {{GLPK} -- {GNU} {L}inear {P}rogramming {K}it},
82
  url =          {http://www.gnu.org/software/glpk/}
83
}
84

	
85
@misc{clp,
86
  key =          {Clp},
87
  title =        {{Clp} -- {Coin-Or} {L}inear {P}rogramming},
88
  url =          {http://projects.coin-or.org/Clp/}
89
}
90

	
91
@misc{cbc,
92
  key =          {Cbc},
93
  title =        {{Cbc} -- {Coin-Or} {B}ranch and {C}ut},
94
  url =          {http://projects.coin-or.org/Cbc/}
95
}
96

	
97
@misc{cplex,
98
  key =          {CPLEX},
99
  title =        {{ILOG} {CPLEX}},
100
  url =          {http://www.ilog.com/}
101
}
102

	
103
@misc{soplex,
104
  key =          {SoPlex},
105
  title =        {{SoPlex} -- {T}he {S}equential {O}bject-{O}riented
106
                  {S}implex},
107
  url =          {http://soplex.zib.de/}
108
}
109

	
110

	
111
%%%%% General books %%%%%
112

	
113
@book{amo93networkflows,
114
  author =       {Ravindra K. Ahuja and Thomas L. Magnanti and James
115
                  B. Orlin},
116
  title =        {Network Flows: Theory, Algorithms, and Applications},
117
  publisher =    {Prentice-Hall, Inc.},
118
  year =         1993,
119
  month =        feb,
120
  isbn =         {978-0136175490}
121
}
122

	
123
@book{schrijver03combinatorial,
124
  author =       {Alexander Schrijver},
125
  title =        {Combinatorial Optimization: Polyhedra and Efficiency},
126
  publisher =    {Springer-Verlag},
127
  year =         2003,
128
  isbn =         {978-3540443896}
129
}
130

	
131
@book{clrs01algorithms,
132
  author =       {Thomas H. Cormen and Charles E. Leiserson and Ronald
133
                  L. Rivest and Clifford Stein},
134
  title =        {Introduction to Algorithms},
135
  publisher =    {The MIT Press},
136
  year =         2001,
137
  edition =      {2nd}
138
}
139

	
140
@book{stroustrup00cpp,
141
  author =       {Bjarne Stroustrup},
142
  title =        {The C++ Programming Language},
143
  edition =      {3rd},
144
  publisher =    {Addison-Wesley Professional},
145
  isbn =         0201700735,
146
  month =        {February},
147
  year =         2000
148
}
149

	
150

	
151
%%%%% Maximum flow algorithms %%%%%
152

	
153
@article{edmondskarp72theoretical,
154
  author =       {Jack Edmonds and Richard M. Karp},
155
  title =        {Theoretical improvements in algorithmic efficiency
156
                  for network flow problems},
157
  journal =      {Journal of the ACM},
158
  year =         1972,
159
  volume =       19,
160
  number =       2,
161
  pages =        {248-264}
162
}
163

	
164
@article{goldberg88newapproach,
165
  author =       {Andrew V. Goldberg and Robert E. Tarjan},
166
  title =        {A new approach to the maximum flow problem},
167
  journal =      {Journal of the ACM},
168
  year =         1988,
169
  volume =       35,
170
  number =       4,
171
  pages =        {921-940}
172
}
173

	
174
@article{dinic70algorithm,
175
  author =       {E. A. Dinic},
176
  title =        {Algorithm for solution of a problem of maximum flow
177
                  in a network with power estimation},
178
  journal =      {Soviet Math. Doklady},
179
  year =         1970,
180
  volume =       11,
181
  pages =        {1277-1280}
182
}
183

	
184
@article{goldberg08partial,
185
  author =       {Andrew V. Goldberg},
186
  title =        {The Partial Augment-Relabel Algorithm for the
187
                  Maximum Flow Problem},
188
  journal =      {16th Annual European Symposium on Algorithms},
189
  year =         2008,
190
  pages =        {466-477}
191
}
192

	
193
@article{sleator83dynamic,
194
  author =       {Daniel D. Sleator and Robert E. Tarjan},
195
  title =        {A data structure for dynamic trees},
196
  journal =      {Journal of Computer and System Sciences},
197
  year =         1983,
198
  volume =       26,
199
  number =       3,
200
  pages =        {362-391}
201
}
202

	
203

	
204
%%%%% Minimum mean cycle algorithms %%%%%
205

	
206
@article{karp78characterization,
207
  author =       {Richard M. Karp},
208
  title =        {A characterization of the minimum cycle mean in a
209
                  digraph},
210
  journal =      {Discrete Math.},
211
  year =         1978,
212
  volume =       23,
213
  pages =        {309-311}
214
}
215

	
216
@article{dasdan98minmeancycle,
217
  author =       {Ali Dasdan and Rajesh K. Gupta},
218
  title =        {Faster Maximum and Minimum Mean Cycle Alogrithms for
219
                  System Performance Analysis},
220
  journal =      {IEEE Transactions on Computer-Aided Design of
221
                  Integrated Circuits and Systems},
222
  year =         1998,
223
  volume =       17,
224
  number =       10,
225
  pages =        {889-899}
226
}
227

	
228

	
229
%%%%% Minimum cost flow algorithms %%%%%
230

	
231
@article{klein67primal,
232
  author =       {Morton Klein},
233
  title =        {A primal method for minimal cost flows with
234
                  applications to the assignment and transportation
235
                  problems},
236
  journal =      {Management Science},
237
  year =         1967,
238
  volume =       14,
239
  pages =        {205-220}
240
}
241

	
242
@article{goldberg89cyclecanceling,
243
  author =       {Andrew V. Goldberg and Robert E. Tarjan},
244
  title =        {Finding minimum-cost circulations by canceling
245
                  negative cycles},
246
  journal =      {Journal of the ACM},
247
  year =         1989,
248
  volume =       36,
249
  number =       4,
250
  pages =        {873-886}
251
}
252

	
253
@article{goldberg90approximation,
254
  author =       {Andrew V. Goldberg and Robert E. Tarjan},
255
  title =        {Finding Minimum-Cost Circulations by Successive
256
                  Approximation},
257
  journal =      {Mathematics of Operations Research},
258
  year =         1990,
259
  volume =       15,
260
  number =       3,
261
  pages =        {430-466}
262
}
263

	
264
@article{goldberg97efficient,
265
  author =       {Andrew V. Goldberg},
266
  title =        {An Efficient Implementation of a Scaling
267
                  Minimum-Cost Flow Algorithm},
268
  journal =      {Journal of Algorithms},
269
  year =         1997,
270
  volume =       22,
271
  number =       1,
272
  pages =        {1-29}
273
}
274

	
275
@article{bunnagel98efficient,
276
  author =       {Ursula B{\"u}nnagel and Bernhard Korte and Jens
277
                  Vygen},
278
  title =        {Efficient implementation of the {G}oldberg-{T}arjan
279
                  minimum-cost flow algorithm},
280
  journal =      {Optimization Methods and Software},
281
  year =         1998,
282
  volume =       10,
283
  pages =        {157-174}
284
}
285

	
286
@book{dantzig63linearprog,
287
  author =       {George B. Dantzig},
288
  title =        {Linear Programming and Extensions},
289
  publisher =    {Princeton University Press},
290
  year =         1963
291
}
292

	
293
@mastersthesis{kellyoneill91netsimplex,
294
  author =       {Damian J. Kelly and Garrett M. O'Neill},
295
  title =        {The Minimum Cost Flow Problem and The Network
296
                  Simplex Method},
297
  school =       {University College},
298
  address =      {Dublin, Ireland},
299
  year =         1991,
300
  month =        sep,
301
}
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BELLMAN_FORD_H
20
#define LEMON_BELLMAN_FORD_H
21

	
22
/// \ingroup shortest_path
23
/// \file
24
/// \brief Bellman-Ford algorithm.
25

	
26
#include <lemon/list_graph.h>
27
#include <lemon/bits/path_dump.h>
28
#include <lemon/core.h>
29
#include <lemon/error.h>
30
#include <lemon/maps.h>
31
#include <lemon/path.h>
32

	
33
#include <limits>
34

	
35
namespace lemon {
36

	
37
  /// \brief Default OperationTraits for the BellmanFord algorithm class.
38
  ///  
39
  /// This operation traits class defines all computational operations
40
  /// and constants that are used in the Bellman-Ford algorithm.
41
  /// The default implementation is based on the \c numeric_limits class.
42
  /// If the numeric type does not have infinity value, then the maximum
43
  /// value is used as extremal infinity value.
44
  template <
45
    typename V, 
46
    bool has_inf = std::numeric_limits<V>::has_infinity>
47
  struct BellmanFordDefaultOperationTraits {
48
    /// \e
49
    typedef V Value;
50
    /// \brief Gives back the zero value of the type.
51
    static Value zero() {
52
      return static_cast<Value>(0);
53
    }
54
    /// \brief Gives back the positive infinity value of the type.
55
    static Value infinity() {
56
      return std::numeric_limits<Value>::infinity();
57
    }
58
    /// \brief Gives back the sum of the given two elements.
59
    static Value plus(const Value& left, const Value& right) {
60
      return left + right;
61
    }
62
    /// \brief Gives back \c true only if the first value is less than
63
    /// the second.
64
    static bool less(const Value& left, const Value& right) {
65
      return left < right;
66
    }
67
  };
68

	
69
  template <typename V>
70
  struct BellmanFordDefaultOperationTraits<V, false> {
71
    typedef V Value;
72
    static Value zero() {
73
      return static_cast<Value>(0);
74
    }
75
    static Value infinity() {
76
      return std::numeric_limits<Value>::max();
77
    }
78
    static Value plus(const Value& left, const Value& right) {
79
      if (left == infinity() || right == infinity()) return infinity();
80
      return left + right;
81
    }
82
    static bool less(const Value& left, const Value& right) {
83
      return left < right;
84
    }
85
  };
86
  
87
  /// \brief Default traits class of BellmanFord class.
88
  ///
89
  /// Default traits class of BellmanFord class.
90
  /// \param GR The type of the digraph.
91
  /// \param LEN The type of the length map.
92
  template<typename GR, typename LEN>
93
  struct BellmanFordDefaultTraits {
94
    /// The type of the digraph the algorithm runs on. 
95
    typedef GR Digraph;
96

	
97
    /// \brief The type of the map that stores the arc lengths.
98
    ///
99
    /// The type of the map that stores the arc lengths.
100
    /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
101
    typedef LEN LengthMap;
102

	
103
    /// The type of the arc lengths.
104
    typedef typename LEN::Value Value;
105

	
106
    /// \brief Operation traits for Bellman-Ford algorithm.
107
    ///
108
    /// It defines the used operations and the infinity value for the
109
    /// given \c Value type.
110
    /// \see BellmanFordDefaultOperationTraits
111
    typedef BellmanFordDefaultOperationTraits<Value> OperationTraits;
112
 
113
    /// \brief The type of the map that stores the last arcs of the 
114
    /// shortest paths.
115
    /// 
116
    /// The type of the map that stores the last
117
    /// arcs of the shortest paths.
118
    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
119
    typedef typename GR::template NodeMap<typename GR::Arc> PredMap;
120

	
121
    /// \brief Instantiates a \c PredMap.
122
    /// 
123
    /// This function instantiates a \ref PredMap. 
124
    /// \param g is the digraph to which we would like to define the
125
    /// \ref PredMap.
126
    static PredMap *createPredMap(const GR& g) {
127
      return new PredMap(g);
128
    }
129

	
130
    /// \brief The type of the map that stores the distances of the nodes.
131
    ///
132
    /// The type of the map that stores the distances of the nodes.
133
    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
134
    typedef typename GR::template NodeMap<typename LEN::Value> DistMap;
135

	
136
    /// \brief Instantiates a \c DistMap.
137
    ///
138
    /// This function instantiates a \ref DistMap. 
139
    /// \param g is the digraph to which we would like to define the 
140
    /// \ref DistMap.
141
    static DistMap *createDistMap(const GR& g) {
142
      return new DistMap(g);
143
    }
144

	
145
  };
146
  
147
  /// \brief %BellmanFord algorithm class.
148
  ///
149
  /// \ingroup shortest_path
150
  /// This class provides an efficient implementation of the Bellman-Ford 
151
  /// algorithm. The maximum time complexity of the algorithm is
152
  /// <tt>O(ne)</tt>.
153
  ///
154
  /// The Bellman-Ford algorithm solves the single-source shortest path
155
  /// problem when the arcs can have negative lengths, but the digraph
156
  /// should not contain directed cycles with negative total length.
157
  /// If all arc costs are non-negative, consider to use the Dijkstra
158
  /// algorithm instead, since it is more efficient.
159
  ///
160
  /// The arc lengths are passed to the algorithm using a
161
  /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any 
162
  /// kind of length. The type of the length values is determined by the
163
  /// \ref concepts::ReadMap::Value "Value" type of the length map.
164
  ///
165
  /// There is also a \ref bellmanFord() "function-type interface" for the
166
  /// Bellman-Ford algorithm, which is convenient in the simplier cases and
167
  /// it can be used easier.
168
  ///
169
  /// \tparam GR The type of the digraph the algorithm runs on.
170
  /// The default type is \ref ListDigraph.
171
  /// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies
172
  /// the lengths of the arcs. The default map type is
173
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
174
#ifdef DOXYGEN
175
  template <typename GR, typename LEN, typename TR>
176
#else
177
  template <typename GR=ListDigraph,
178
            typename LEN=typename GR::template ArcMap<int>,
179
            typename TR=BellmanFordDefaultTraits<GR,LEN> >
180
#endif
181
  class BellmanFord {
182
  public:
183

	
184
    ///The type of the underlying digraph.
185
    typedef typename TR::Digraph Digraph;
186
    
187
    /// \brief The type of the arc lengths.
188
    typedef typename TR::LengthMap::Value Value;
189
    /// \brief The type of the map that stores the arc lengths.
190
    typedef typename TR::LengthMap LengthMap;
191
    /// \brief The type of the map that stores the last
192
    /// arcs of the shortest paths.
193
    typedef typename TR::PredMap PredMap;
194
    /// \brief The type of the map that stores the distances of the nodes.
195
    typedef typename TR::DistMap DistMap;
196
    /// The type of the paths.
197
    typedef PredMapPath<Digraph, PredMap> Path;
198
    ///\brief The \ref BellmanFordDefaultOperationTraits
199
    /// "operation traits class" of the algorithm.
200
    typedef typename TR::OperationTraits OperationTraits;
201

	
202
    ///The \ref BellmanFordDefaultTraits "traits class" of the algorithm.
203
    typedef TR Traits;
204

	
205
  private:
206

	
207
    typedef typename Digraph::Node Node;
208
    typedef typename Digraph::NodeIt NodeIt;
209
    typedef typename Digraph::Arc Arc;
210
    typedef typename Digraph::OutArcIt OutArcIt;
211

	
212
    // Pointer to the underlying digraph.
213
    const Digraph *_gr;
214
    // Pointer to the length map
215
    const LengthMap *_length;
216
    // Pointer to the map of predecessors arcs.
217
    PredMap *_pred;
218
    // Indicates if _pred is locally allocated (true) or not.
219
    bool _local_pred;
220
    // Pointer to the map of distances.
221
    DistMap *_dist;
222
    // Indicates if _dist is locally allocated (true) or not.
223
    bool _local_dist;
224

	
225
    typedef typename Digraph::template NodeMap<bool> MaskMap;
226
    MaskMap *_mask;
227

	
228
    std::vector<Node> _process;
229

	
230
    // Creates the maps if necessary.
231
    void create_maps() {
232
      if(!_pred) {
233
	_local_pred = true;
234
	_pred = Traits::createPredMap(*_gr);
235
      }
236
      if(!_dist) {
237
	_local_dist = true;
238
	_dist = Traits::createDistMap(*_gr);
239
      }
240
      _mask = new MaskMap(*_gr, false);
241
    }
242
    
243
  public :
244
 
245
    typedef BellmanFord Create;
246

	
247
    /// \name Named Template Parameters
248

	
249
    ///@{
250

	
251
    template <class T>
252
    struct SetPredMapTraits : public Traits {
253
      typedef T PredMap;
254
      static PredMap *createPredMap(const Digraph&) {
255
        LEMON_ASSERT(false, "PredMap is not initialized");
256
        return 0; // ignore warnings
257
      }
258
    };
259

	
260
    /// \brief \ref named-templ-param "Named parameter" for setting
261
    /// \c PredMap type.
262
    ///
263
    /// \ref named-templ-param "Named parameter" for setting
264
    /// \c PredMap type.
265
    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
266
    template <class T>
267
    struct SetPredMap 
268
      : public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > {
269
      typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create;
270
    };
271
    
272
    template <class T>
273
    struct SetDistMapTraits : public Traits {
274
      typedef T DistMap;
275
      static DistMap *createDistMap(const Digraph&) {
276
        LEMON_ASSERT(false, "DistMap is not initialized");
277
        return 0; // ignore warnings
278
      }
279
    };
280

	
281
    /// \brief \ref named-templ-param "Named parameter" for setting
282
    /// \c DistMap type.
283
    ///
284
    /// \ref named-templ-param "Named parameter" for setting
285
    /// \c DistMap type.
286
    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
287
    template <class T>
288
    struct SetDistMap 
289
      : public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > {
290
      typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create;
291
    };
292

	
293
    template <class T>
294
    struct SetOperationTraitsTraits : public Traits {
295
      typedef T OperationTraits;
296
    };
297
    
298
    /// \brief \ref named-templ-param "Named parameter" for setting 
299
    /// \c OperationTraits type.
300
    ///
301
    /// \ref named-templ-param "Named parameter" for setting
302
    /// \c OperationTraits type.
303
    /// For more information see \ref BellmanFordDefaultOperationTraits.
304
    template <class T>
305
    struct SetOperationTraits
306
      : public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > {
307
      typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> >
308
      Create;
309
    };
310
    
311
    ///@}
312

	
313
  protected:
314
    
315
    BellmanFord() {}
316

	
317
  public:      
318
    
319
    /// \brief Constructor.
320
    ///
321
    /// Constructor.
322
    /// \param g The digraph the algorithm runs on.
323
    /// \param length The length map used by the algorithm.
324
    BellmanFord(const Digraph& g, const LengthMap& length) :
325
      _gr(&g), _length(&length),
326
      _pred(0), _local_pred(false),
327
      _dist(0), _local_dist(false), _mask(0) {}
328
    
329
    ///Destructor.
330
    ~BellmanFord() {
331
      if(_local_pred) delete _pred;
332
      if(_local_dist) delete _dist;
333
      if(_mask) delete _mask;
334
    }
335

	
336
    /// \brief Sets the length map.
337
    ///
338
    /// Sets the length map.
339
    /// \return <tt>(*this)</tt>
340
    BellmanFord &lengthMap(const LengthMap &map) {
341
      _length = &map;
342
      return *this;
343
    }
344

	
345
    /// \brief Sets the map that stores the predecessor arcs.
346
    ///
347
    /// Sets the map that stores the predecessor arcs.
348
    /// If you don't use this function before calling \ref run()
349
    /// or \ref init(), an instance will be allocated automatically.
350
    /// The destructor deallocates this automatically allocated map,
351
    /// of course.
352
    /// \return <tt>(*this)</tt>
353
    BellmanFord &predMap(PredMap &map) {
354
      if(_local_pred) {
355
	delete _pred;
356
	_local_pred=false;
357
      }
358
      _pred = &map;
359
      return *this;
360
    }
361

	
362
    /// \brief Sets the map that stores the distances of the nodes.
363
    ///
364
    /// Sets the map that stores the distances of the nodes calculated
365
    /// by the algorithm.
366
    /// If you don't use this function before calling \ref run()
367
    /// or \ref init(), an instance will be allocated automatically.
368
    /// The destructor deallocates this automatically allocated map,
369
    /// of course.
370
    /// \return <tt>(*this)</tt>
371
    BellmanFord &distMap(DistMap &map) {
372
      if(_local_dist) {
373
	delete _dist;
374
	_local_dist=false;
375
      }
376
      _dist = &map;
377
      return *this;
378
    }
379

	
380
    /// \name Execution Control
381
    /// The simplest way to execute the Bellman-Ford algorithm is to use
382
    /// one of the member functions called \ref run().\n
383
    /// If you need better control on the execution, you have to call
384
    /// \ref init() first, then you can add several source nodes
385
    /// with \ref addSource(). Finally the actual path computation can be
386
    /// performed with \ref start(), \ref checkedStart() or
387
    /// \ref limitedStart().
388

	
389
    ///@{
390

	
391
    /// \brief Initializes the internal data structures.
392
    /// 
393
    /// Initializes the internal data structures. The optional parameter
394
    /// is the initial distance of each node.
395
    void init(const Value value = OperationTraits::infinity()) {
396
      create_maps();
397
      for (NodeIt it(*_gr); it != INVALID; ++it) {
398
	_pred->set(it, INVALID);
399
	_dist->set(it, value);
400
      }
401
      _process.clear();
402
      if (OperationTraits::less(value, OperationTraits::infinity())) {
403
	for (NodeIt it(*_gr); it != INVALID; ++it) {
404
	  _process.push_back(it);
405
	  _mask->set(it, true);
406
	}
407
      }
408
    }
409
    
410
    /// \brief Adds a new source node.
411
    ///
412
    /// This function adds a new source node. The optional second parameter
413
    /// is the initial distance of the node.
414
    void addSource(Node source, Value dst = OperationTraits::zero()) {
415
      _dist->set(source, dst);
416
      if (!(*_mask)[source]) {
417
	_process.push_back(source);
418
	_mask->set(source, true);
419
      }
420
    }
421

	
422
    /// \brief Executes one round from the Bellman-Ford algorithm.
423
    ///
424
    /// If the algoritm calculated the distances in the previous round
425
    /// exactly for the paths of at most \c k arcs, then this function
426
    /// will calculate the distances exactly for the paths of at most
427
    /// <tt>k+1</tt> arcs. Performing \c k iterations using this function
428
    /// calculates the shortest path distances exactly for the paths
429
    /// consisting of at most \c k arcs.
430
    ///
431
    /// \warning The paths with limited arc number cannot be retrieved
432
    /// easily with \ref path() or \ref predArc() functions. If you also
433
    /// need the shortest paths and not only the distances, you should
434
    /// store the \ref predMap() "predecessor map" after each iteration
435
    /// and build the path manually.
436
    ///
437
    /// \return \c true when the algorithm have not found more shorter
438
    /// paths.
439
    ///
440
    /// \see ActiveIt
441
    bool processNextRound() {
442
      for (int i = 0; i < int(_process.size()); ++i) {
443
	_mask->set(_process[i], false);
444
      }
445
      std::vector<Node> nextProcess;
446
      std::vector<Value> values(_process.size());
447
      for (int i = 0; i < int(_process.size()); ++i) {
448
	values[i] = (*_dist)[_process[i]];
449
      }
450
      for (int i = 0; i < int(_process.size()); ++i) {
451
	for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
452
	  Node target = _gr->target(it);
453
	  Value relaxed = OperationTraits::plus(values[i], (*_length)[it]);
454
	  if (OperationTraits::less(relaxed, (*_dist)[target])) {
455
	    _pred->set(target, it);
456
	    _dist->set(target, relaxed);
457
	    if (!(*_mask)[target]) {
458
	      _mask->set(target, true);
459
	      nextProcess.push_back(target);
460
	    }
461
	  }	  
462
	}
463
      }
464
      _process.swap(nextProcess);
465
      return _process.empty();
466
    }
467

	
468
    /// \brief Executes one weak round from the Bellman-Ford algorithm.
469
    ///
470
    /// If the algorithm calculated the distances in the previous round
471
    /// at least for the paths of at most \c k arcs, then this function
472
    /// will calculate the distances at least for the paths of at most
473
    /// <tt>k+1</tt> arcs.
474
    /// This function does not make it possible to calculate the shortest
475
    /// path distances exactly for paths consisting of at most \c k arcs,
476
    /// this is why it is called weak round.
477
    ///
478
    /// \return \c true when the algorithm have not found more shorter
479
    /// paths.
480
    ///
481
    /// \see ActiveIt
482
    bool processNextWeakRound() {
483
      for (int i = 0; i < int(_process.size()); ++i) {
484
	_mask->set(_process[i], false);
485
      }
486
      std::vector<Node> nextProcess;
487
      for (int i = 0; i < int(_process.size()); ++i) {
488
	for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
489
	  Node target = _gr->target(it);
490
	  Value relaxed = 
491
	    OperationTraits::plus((*_dist)[_process[i]], (*_length)[it]);
492
	  if (OperationTraits::less(relaxed, (*_dist)[target])) {
493
	    _pred->set(target, it);
494
	    _dist->set(target, relaxed);
495
	    if (!(*_mask)[target]) {
496
	      _mask->set(target, true);
497
	      nextProcess.push_back(target);
498
	    }
499
	  }	  
500
	}
501
      }
502
      _process.swap(nextProcess);
503
      return _process.empty();
504
    }
505

	
506
    /// \brief Executes the algorithm.
507
    ///
508
    /// Executes the algorithm.
509
    ///
510
    /// This method runs the Bellman-Ford algorithm from the root node(s)
511
    /// in order to compute the shortest path to each node.
512
    ///
513
    /// The algorithm computes
514
    /// - the shortest path tree (forest),
515
    /// - the distance of each node from the root(s).
516
    ///
517
    /// \pre init() must be called and at least one root node should be
518
    /// added with addSource() before using this function.
519
    void start() {
520
      int num = countNodes(*_gr) - 1;
521
      for (int i = 0; i < num; ++i) {
522
	if (processNextWeakRound()) break;
523
      }
524
    }
525

	
526
    /// \brief Executes the algorithm and checks the negative cycles.
527
    ///
528
    /// Executes the algorithm and checks the negative cycles.
529
    ///
530
    /// This method runs the Bellman-Ford algorithm from the root node(s)
531
    /// in order to compute the shortest path to each node and also checks
532
    /// if the digraph contains cycles with negative total length.
533
    ///
534
    /// The algorithm computes 
535
    /// - the shortest path tree (forest),
536
    /// - the distance of each node from the root(s).
537
    /// 
538
    /// \return \c false if there is a negative cycle in the digraph.
539
    ///
540
    /// \pre init() must be called and at least one root node should be
541
    /// added with addSource() before using this function. 
542
    bool checkedStart() {
543
      int num = countNodes(*_gr);
544
      for (int i = 0; i < num; ++i) {
545
	if (processNextWeakRound()) return true;
546
      }
547
      return _process.empty();
548
    }
549

	
550
    /// \brief Executes the algorithm with arc number limit.
551
    ///
552
    /// Executes the algorithm with arc number limit.
553
    ///
554
    /// This method runs the Bellman-Ford algorithm from the root node(s)
555
    /// in order to compute the shortest path distance for each node
556
    /// using only the paths consisting of at most \c num arcs.
557
    ///
558
    /// The algorithm computes
559
    /// - the limited distance of each node from the root(s),
560
    /// - the predecessor arc for each node.
561
    ///
562
    /// \warning The paths with limited arc number cannot be retrieved
563
    /// easily with \ref path() or \ref predArc() functions. If you also
564
    /// need the shortest paths and not only the distances, you should
565
    /// store the \ref predMap() "predecessor map" after each iteration
566
    /// and build the path manually.
567
    ///
568
    /// \pre init() must be called and at least one root node should be
569
    /// added with addSource() before using this function. 
570
    void limitedStart(int num) {
571
      for (int i = 0; i < num; ++i) {
572
	if (processNextRound()) break;
573
      }
574
    }
575
    
576
    /// \brief Runs the algorithm from the given root node.
577
    ///    
578
    /// This method runs the Bellman-Ford algorithm from the given root
579
    /// node \c s in order to compute the shortest path to each node.
580
    ///
581
    /// The algorithm computes
582
    /// - the shortest path tree (forest),
583
    /// - the distance of each node from the root(s).
584
    ///
585
    /// \note bf.run(s) is just a shortcut of the following code.
586
    /// \code
587
    ///   bf.init();
588
    ///   bf.addSource(s);
589
    ///   bf.start();
590
    /// \endcode
591
    void run(Node s) {
592
      init();
593
      addSource(s);
594
      start();
595
    }
596
    
597
    /// \brief Runs the algorithm from the given root node with arc
598
    /// number limit.
599
    ///    
600
    /// This method runs the Bellman-Ford algorithm from the given root
601
    /// node \c s in order to compute the shortest path distance for each
602
    /// node using only the paths consisting of at most \c num arcs.
603
    ///
604
    /// The algorithm computes
605
    /// - the limited distance of each node from the root(s),
606
    /// - the predecessor arc for each node.
607
    ///
608
    /// \warning The paths with limited arc number cannot be retrieved
609
    /// easily with \ref path() or \ref predArc() functions. If you also
610
    /// need the shortest paths and not only the distances, you should
611
    /// store the \ref predMap() "predecessor map" after each iteration
612
    /// and build the path manually.
613
    ///
614
    /// \note bf.run(s, num) is just a shortcut of the following code.
615
    /// \code
616
    ///   bf.init();
617
    ///   bf.addSource(s);
618
    ///   bf.limitedStart(num);
619
    /// \endcode
620
    void run(Node s, int num) {
621
      init();
622
      addSource(s);
623
      limitedStart(num);
624
    }
625
    
626
    ///@}
627

	
628
    /// \brief LEMON iterator for getting the active nodes.
629
    ///
630
    /// This class provides a common style LEMON iterator that traverses
631
    /// the active nodes of the Bellman-Ford algorithm after the last
632
    /// phase. These nodes should be checked in the next phase to
633
    /// find augmenting arcs outgoing from them.
634
    class ActiveIt {
635
    public:
636

	
637
      /// \brief Constructor.
638
      ///
639
      /// Constructor for getting the active nodes of the given BellmanFord
640
      /// instance. 
641
      ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm)
642
      {
643
        _index = _algorithm->_process.size() - 1;
644
      }
645

	
646
      /// \brief Invalid constructor.
647
      ///
648
      /// Invalid constructor.
649
      ActiveIt(Invalid) : _algorithm(0), _index(-1) {}
650

	
651
      /// \brief Conversion to \c Node.
652
      ///
653
      /// Conversion to \c Node.
654
      operator Node() const { 
655
        return _index >= 0 ? _algorithm->_process[_index] : INVALID;
656
      }
657

	
658
      /// \brief Increment operator.
659
      ///
660
      /// Increment operator.
661
      ActiveIt& operator++() {
662
        --_index;
663
        return *this; 
664
      }
665

	
666
      bool operator==(const ActiveIt& it) const { 
667
        return static_cast<Node>(*this) == static_cast<Node>(it); 
668
      }
669
      bool operator!=(const ActiveIt& it) const { 
670
        return static_cast<Node>(*this) != static_cast<Node>(it); 
671
      }
672
      bool operator<(const ActiveIt& it) const { 
673
        return static_cast<Node>(*this) < static_cast<Node>(it); 
674
      }
675
      
676
    private:
677
      const BellmanFord* _algorithm;
678
      int _index;
679
    };
680
    
681
    /// \name Query Functions
682
    /// The result of the Bellman-Ford algorithm can be obtained using these
683
    /// functions.\n
684
    /// Either \ref run() or \ref init() should be called before using them.
685
    
686
    ///@{
687

	
688
    /// \brief The shortest path to the given node.
689
    ///    
690
    /// Gives back the shortest path to the given node from the root(s).
691
    ///
692
    /// \warning \c t should be reached from the root(s).
693
    ///
694
    /// \pre Either \ref run() or \ref init() must be called before
695
    /// using this function.
696
    Path path(Node t) const
697
    {
698
      return Path(*_gr, *_pred, t);
699
    }
700
	  
701
    /// \brief The distance of the given node from the root(s).
702
    ///
703
    /// Returns the distance of the given node from the root(s).
704
    ///
705
    /// \warning If node \c v is not reached from the root(s), then
706
    /// the return value of this function is undefined.
707
    ///
708
    /// \pre Either \ref run() or \ref init() must be called before
709
    /// using this function.
710
    Value dist(Node v) const { return (*_dist)[v]; }
711

	
712
    /// \brief Returns the 'previous arc' of the shortest path tree for
713
    /// the given node.
714
    ///
715
    /// This function returns the 'previous arc' of the shortest path
716
    /// tree for node \c v, i.e. it returns the last arc of a
717
    /// shortest path from a root to \c v. It is \c INVALID if \c v
718
    /// is not reached from the root(s) or if \c v is a root.
719
    ///
720
    /// The shortest path tree used here is equal to the shortest path
721
    /// tree used in \ref predNode() and \predMap().
722
    ///
723
    /// \pre Either \ref run() or \ref init() must be called before
724
    /// using this function.
725
    Arc predArc(Node v) const { return (*_pred)[v]; }
726

	
727
    /// \brief Returns the 'previous node' of the shortest path tree for
728
    /// the given node.
729
    ///
730
    /// This function returns the 'previous node' of the shortest path
731
    /// tree for node \c v, i.e. it returns the last but one node of
732
    /// a shortest path from a root to \c v. It is \c INVALID if \c v
733
    /// is not reached from the root(s) or if \c v is a root.
734
    ///
735
    /// The shortest path tree used here is equal to the shortest path
736
    /// tree used in \ref predArc() and \predMap().
737
    ///
738
    /// \pre Either \ref run() or \ref init() must be called before
739
    /// using this function.
740
    Node predNode(Node v) const { 
741
      return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); 
742
    }
743
    
744
    /// \brief Returns a const reference to the node map that stores the
745
    /// distances of the nodes.
746
    ///
747
    /// Returns a const reference to the node map that stores the distances
748
    /// of the nodes calculated by the algorithm.
749
    ///
750
    /// \pre Either \ref run() or \ref init() must be called before
751
    /// using this function.
752
    const DistMap &distMap() const { return *_dist;}
753
 
754
    /// \brief Returns a const reference to the node map that stores the
755
    /// predecessor arcs.
756
    ///
757
    /// Returns a const reference to the node map that stores the predecessor
758
    /// arcs, which form the shortest path tree (forest).
759
    ///
760
    /// \pre Either \ref run() or \ref init() must be called before
761
    /// using this function.
762
    const PredMap &predMap() const { return *_pred; }
763
 
764
    /// \brief Checks if a node is reached from the root(s).
765
    ///
766
    /// Returns \c true if \c v is reached from the root(s).
767
    ///
768
    /// \pre Either \ref run() or \ref init() must be called before
769
    /// using this function.
770
    bool reached(Node v) const {
771
      return (*_dist)[v] != OperationTraits::infinity();
772
    }
773

	
774
    /// \brief Gives back a negative cycle.
775
    ///    
776
    /// This function gives back a directed cycle with negative total
777
    /// length if the algorithm has already found one.
778
    /// Otherwise it gives back an empty path.
779
    lemon::Path<Digraph> negativeCycle() const {
780
      typename Digraph::template NodeMap<int> state(*_gr, -1);
781
      lemon::Path<Digraph> cycle;
782
      for (int i = 0; i < int(_process.size()); ++i) {
783
        if (state[_process[i]] != -1) continue;
784
        for (Node v = _process[i]; (*_pred)[v] != INVALID;
785
             v = _gr->source((*_pred)[v])) {
786
          if (state[v] == i) {
787
            cycle.addFront((*_pred)[v]);
788
            for (Node u = _gr->source((*_pred)[v]); u != v;
789
                 u = _gr->source((*_pred)[u])) {
790
              cycle.addFront((*_pred)[u]);
791
            }
792
            return cycle;
793
          }
794
          else if (state[v] >= 0) {
795
            break;
796
          }
797
          state[v] = i;
798
        }
799
      }
800
      return cycle;
801
    }
802
    
803
    ///@}
804
  };
805
 
806
  /// \brief Default traits class of bellmanFord() function.
807
  ///
808
  /// Default traits class of bellmanFord() function.
809
  /// \tparam GR The type of the digraph.
810
  /// \tparam LEN The type of the length map.
811
  template <typename GR, typename LEN>
812
  struct BellmanFordWizardDefaultTraits {
813
    /// The type of the digraph the algorithm runs on. 
814
    typedef GR Digraph;
815

	
816
    /// \brief The type of the map that stores the arc lengths.
817
    ///
818
    /// The type of the map that stores the arc lengths.
819
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
820
    typedef LEN LengthMap;
821

	
822
    /// The type of the arc lengths.
823
    typedef typename LEN::Value Value;
824

	
825
    /// \brief Operation traits for Bellman-Ford algorithm.
826
    ///
827
    /// It defines the used operations and the infinity value for the
828
    /// given \c Value type.
829
    /// \see BellmanFordDefaultOperationTraits
830
    typedef BellmanFordDefaultOperationTraits<Value> OperationTraits;
831

	
832
    /// \brief The type of the map that stores the last
833
    /// arcs of the shortest paths.
834
    /// 
835
    /// The type of the map that stores the last arcs of the shortest paths.
836
    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
837
    typedef typename GR::template NodeMap<typename GR::Arc> PredMap;
838

	
839
    /// \brief Instantiates a \c PredMap.
840
    /// 
841
    /// This function instantiates a \ref PredMap.
842
    /// \param g is the digraph to which we would like to define the
843
    /// \ref PredMap.
844
    static PredMap *createPredMap(const GR &g) {
845
      return new PredMap(g);
846
    }
847

	
848
    /// \brief The type of the map that stores the distances of the nodes.
849
    ///
850
    /// The type of the map that stores the distances of the nodes.
851
    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
852
    typedef typename GR::template NodeMap<Value> DistMap;
853

	
854
    /// \brief Instantiates a \c DistMap.
855
    ///
856
    /// This function instantiates a \ref DistMap. 
857
    /// \param g is the digraph to which we would like to define the
858
    /// \ref DistMap.
859
    static DistMap *createDistMap(const GR &g) {
860
      return new DistMap(g);
861
    }
862

	
863
    ///The type of the shortest paths.
864

	
865
    ///The type of the shortest paths.
866
    ///It must meet the \ref concepts::Path "Path" concept.
867
    typedef lemon::Path<Digraph> Path;
868
  };
869
  
870
  /// \brief Default traits class used by BellmanFordWizard.
871
  ///
872
  /// Default traits class used by BellmanFordWizard.
873
  /// \tparam GR The type of the digraph.
874
  /// \tparam LEN The type of the length map.
875
  template <typename GR, typename LEN>
876
  class BellmanFordWizardBase 
877
    : public BellmanFordWizardDefaultTraits<GR, LEN> {
878

	
879
    typedef BellmanFordWizardDefaultTraits<GR, LEN> Base;
880
  protected:
881
    // Type of the nodes in the digraph.
882
    typedef typename Base::Digraph::Node Node;
883

	
884
    // Pointer to the underlying digraph.
885
    void *_graph;
886
    // Pointer to the length map
887
    void *_length;
888
    // Pointer to the map of predecessors arcs.
889
    void *_pred;
890
    // Pointer to the map of distances.
891
    void *_dist;
892
    //Pointer to the shortest path to the target node.
893
    void *_path;
894
    //Pointer to the distance of the target node.
895
    void *_di;
896

	
897
    public:
898
    /// Constructor.
899
    
900
    /// This constructor does not require parameters, it initiates
901
    /// all of the attributes to default values \c 0.
902
    BellmanFordWizardBase() :
903
      _graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {}
904

	
905
    /// Constructor.
906
    
907
    /// This constructor requires two parameters,
908
    /// others are initiated to \c 0.
909
    /// \param gr The digraph the algorithm runs on.
910
    /// \param len The length map.
911
    BellmanFordWizardBase(const GR& gr, 
912
			  const LEN& len) :
913
      _graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), 
914
      _length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), 
915
      _pred(0), _dist(0), _path(0), _di(0) {}
916

	
917
  };
918
  
919
  /// \brief Auxiliary class for the function-type interface of the
920
  /// \ref BellmanFord "Bellman-Ford" algorithm.
921
  ///
922
  /// This auxiliary class is created to implement the
923
  /// \ref bellmanFord() "function-type interface" of the
924
  /// \ref BellmanFord "Bellman-Ford" algorithm.
925
  /// It does not have own \ref run() method, it uses the
926
  /// functions and features of the plain \ref BellmanFord.
927
  ///
928
  /// This class should only be used through the \ref bellmanFord()
929
  /// function, which makes it easier to use the algorithm.
930
  template<class TR>
931
  class BellmanFordWizard : public TR {
932
    typedef TR Base;
933

	
934
    typedef typename TR::Digraph Digraph;
935

	
936
    typedef typename Digraph::Node Node;
937
    typedef typename Digraph::NodeIt NodeIt;
938
    typedef typename Digraph::Arc Arc;
939
    typedef typename Digraph::OutArcIt ArcIt;
940
    
941
    typedef typename TR::LengthMap LengthMap;
942
    typedef typename LengthMap::Value Value;
943
    typedef typename TR::PredMap PredMap;
944
    typedef typename TR::DistMap DistMap;
945
    typedef typename TR::Path Path;
946

	
947
  public:
948
    /// Constructor.
949
    BellmanFordWizard() : TR() {}
950

	
951
    /// \brief Constructor that requires parameters.
952
    ///
953
    /// Constructor that requires parameters.
954
    /// These parameters will be the default values for the traits class.
955
    /// \param gr The digraph the algorithm runs on.
956
    /// \param len The length map.
957
    BellmanFordWizard(const Digraph& gr, const LengthMap& len) 
958
      : TR(gr, len) {}
959

	
960
    /// \brief Copy constructor
961
    BellmanFordWizard(const TR &b) : TR(b) {}
962

	
963
    ~BellmanFordWizard() {}
964

	
965
    /// \brief Runs the Bellman-Ford algorithm from the given source node.
966
    ///    
967
    /// This method runs the Bellman-Ford algorithm from the given source
968
    /// node in order to compute the shortest path to each node.
969
    void run(Node s) {
970
      BellmanFord<Digraph,LengthMap,TR> 
971
	bf(*reinterpret_cast<const Digraph*>(Base::_graph), 
972
           *reinterpret_cast<const LengthMap*>(Base::_length));
973
      if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
974
      if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
975
      bf.run(s);
976
    }
977

	
978
    /// \brief Runs the Bellman-Ford algorithm to find the shortest path
979
    /// between \c s and \c t.
980
    ///
981
    /// This method runs the Bellman-Ford algorithm from node \c s
982
    /// in order to compute the shortest path to node \c t.
983
    /// Actually, it computes the shortest path to each node, but using
984
    /// this function you can retrieve the distance and the shortest path
985
    /// for a single target node easier.
986
    ///
987
    /// \return \c true if \c t is reachable form \c s.
988
    bool run(Node s, Node t) {
989
      BellmanFord<Digraph,LengthMap,TR>
990
        bf(*reinterpret_cast<const Digraph*>(Base::_graph),
991
           *reinterpret_cast<const LengthMap*>(Base::_length));
992
      if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
993
      if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
994
      bf.run(s);
995
      if (Base::_path) *reinterpret_cast<Path*>(Base::_path) = bf.path(t);
996
      if (Base::_di) *reinterpret_cast<Value*>(Base::_di) = bf.dist(t);
997
      return bf.reached(t);
998
    }
999

	
1000
    template<class T>
1001
    struct SetPredMapBase : public Base {
1002
      typedef T PredMap;
1003
      static PredMap *createPredMap(const Digraph &) { return 0; };
1004
      SetPredMapBase(const TR &b) : TR(b) {}
1005
    };
1006
    
1007
    /// \brief \ref named-templ-param "Named parameter" for setting
1008
    /// the predecessor map.
1009
    ///
1010
    /// \ref named-templ-param "Named parameter" for setting
1011
    /// the map that stores the predecessor arcs of the nodes.
1012
    template<class T>
1013
    BellmanFordWizard<SetPredMapBase<T> > predMap(const T &t) {
1014
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1015
      return BellmanFordWizard<SetPredMapBase<T> >(*this);
1016
    }
1017
    
1018
    template<class T>
1019
    struct SetDistMapBase : public Base {
1020
      typedef T DistMap;
1021
      static DistMap *createDistMap(const Digraph &) { return 0; };
1022
      SetDistMapBase(const TR &b) : TR(b) {}
1023
    };
1024
    
1025
    /// \brief \ref named-templ-param "Named parameter" for setting
1026
    /// the distance map.
1027
    ///
1028
    /// \ref named-templ-param "Named parameter" for setting
1029
    /// the map that stores the distances of the nodes calculated
1030
    /// by the algorithm.
1031
    template<class T>
1032
    BellmanFordWizard<SetDistMapBase<T> > distMap(const T &t) {
1033
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1034
      return BellmanFordWizard<SetDistMapBase<T> >(*this);
1035
    }
1036

	
1037
    template<class T>
1038
    struct SetPathBase : public Base {
1039
      typedef T Path;
1040
      SetPathBase(const TR &b) : TR(b) {}
1041
    };
1042

	
1043
    /// \brief \ref named-func-param "Named parameter" for getting
1044
    /// the shortest path to the target node.
1045
    ///
1046
    /// \ref named-func-param "Named parameter" for getting
1047
    /// the shortest path to the target node.
1048
    template<class T>
1049
    BellmanFordWizard<SetPathBase<T> > path(const T &t)
1050
    {
1051
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1052
      return BellmanFordWizard<SetPathBase<T> >(*this);
1053
    }
1054

	
1055
    /// \brief \ref named-func-param "Named parameter" for getting
1056
    /// the distance of the target node.
1057
    ///
1058
    /// \ref named-func-param "Named parameter" for getting
1059
    /// the distance of the target node.
1060
    BellmanFordWizard dist(const Value &d)
1061
    {
1062
      Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
1063
      return *this;
1064
    }
1065
    
1066
  };
1067
  
1068
  /// \brief Function type interface for the \ref BellmanFord "Bellman-Ford"
1069
  /// algorithm.
1070
  ///
1071
  /// \ingroup shortest_path
1072
  /// Function type interface for the \ref BellmanFord "Bellman-Ford"
1073
  /// algorithm.
1074
  ///
1075
  /// This function also has several \ref named-templ-func-param 
1076
  /// "named parameters", they are declared as the members of class 
1077
  /// \ref BellmanFordWizard.
1078
  /// The following examples show how to use these parameters.
1079
  /// \code
1080
  ///   // Compute shortest path from node s to each node
1081
  ///   bellmanFord(g,length).predMap(preds).distMap(dists).run(s);
1082
  ///
1083
  ///   // Compute shortest path from s to t
1084
  ///   bool reached = bellmanFord(g,length).path(p).dist(d).run(s,t);
1085
  /// \endcode
1086
  /// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()"
1087
  /// to the end of the parameter list.
1088
  /// \sa BellmanFordWizard
1089
  /// \sa BellmanFord
1090
  template<typename GR, typename LEN>
1091
  BellmanFordWizard<BellmanFordWizardBase<GR,LEN> >
1092
  bellmanFord(const GR& digraph,
1093
	      const LEN& length)
1094
  {
1095
    return BellmanFordWizard<BellmanFordWizardBase<GR,LEN> >(digraph, length);
1096
  }
1097

	
1098
} //END OF NAMESPACE LEMON
1099

	
1100
#endif
1101

	
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BINOM_HEAP_H
20
#define LEMON_BINOM_HEAP_H
21

	
22
///\file
23
///\ingroup heaps
24
///\brief Binomial Heap implementation.
25

	
26
#include <vector>
27
#include <utility>
28
#include <functional>
29
#include <lemon/math.h>
30
#include <lemon/counter.h>
31

	
32
namespace lemon {
33

	
34
  /// \ingroup heaps
35
  ///
36
  ///\brief Binomial heap data structure.
37
  ///
38
  /// This class implements the \e binomial \e heap data structure.
39
  /// It fully conforms to the \ref concepts::Heap "heap concept".
40
  ///
41
  /// The methods \ref increase() and \ref erase() are not efficient
42
  /// in a binomial heap. In case of many calls of these operations,
43
  /// it is better to use other heap structure, e.g. \ref BinHeap
44
  /// "binary heap".
45
  ///
46
  /// \tparam PR Type of the priorities of the items.
47
  /// \tparam IM A read-writable item map with \c int values, used
48
  /// internally to handle the cross references.
49
  /// \tparam CMP A functor class for comparing the priorities.
50
  /// The default is \c std::less<PR>.
51
#ifdef DOXYGEN
52
  template <typename PR, typename IM, typename CMP>
53
#else
54
  template <typename PR, typename IM, typename CMP = std::less<PR> >
55
#endif
56
  class BinomHeap {
57
  public:
58
    /// Type of the item-int map.
59
    typedef IM ItemIntMap;
60
    /// Type of the priorities.
61
    typedef PR Prio;
62
    /// Type of the items stored in the heap.
63
    typedef typename ItemIntMap::Key Item;
64
    /// Functor type for comparing the priorities.
65
    typedef CMP Compare;
66

	
67
    /// \brief Type to represent the states of the items.
68
    ///
69
    /// Each item has a state associated to it. It can be "in heap",
70
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
71
    /// heap's point of view, but may be useful to the user.
72
    ///
73
    /// The item-int map must be initialized in such way that it assigns
74
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
75
    enum State {
76
      IN_HEAP = 0,    ///< = 0.
77
      PRE_HEAP = -1,  ///< = -1.
78
      POST_HEAP = -2  ///< = -2.
79
    };
80

	
81
  private:
82
    class Store;
83

	
84
    std::vector<Store> _data;
85
    int _min, _head;
86
    ItemIntMap &_iim;
87
    Compare _comp;
88
    int _num_items;
89

	
90
  public:
91
    /// \brief Constructor.
92
    ///
93
    /// Constructor.
94
    /// \param map A map that assigns \c int values to the items.
95
    /// It is used internally to handle the cross references.
96
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
97
    explicit BinomHeap(ItemIntMap &map)
98
      : _min(0), _head(-1), _iim(map), _num_items(0) {}
99

	
100
    /// \brief Constructor.
101
    ///
102
    /// Constructor.
103
    /// \param map A map that assigns \c int values to the items.
104
    /// It is used internally to handle the cross references.
105
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
106
    /// \param comp The function object used for comparing the priorities.
107
    BinomHeap(ItemIntMap &map, const Compare &comp)
108
      : _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {}
109

	
110
    /// \brief The number of items stored in the heap.
111
    ///
112
    /// This function returns the number of items stored in the heap.
113
    int size() const { return _num_items; }
114

	
115
    /// \brief Check if the heap is empty.
116
    ///
117
    /// This function returns \c true if the heap is empty.
118
    bool empty() const { return _num_items==0; }
119

	
120
    /// \brief Make the heap empty.
121
    ///
122
    /// This functon makes the heap empty.
123
    /// It does not change the cross reference map. If you want to reuse
124
    /// a heap that is not surely empty, you should first clear it and
125
    /// then you should set the cross reference map to \c PRE_HEAP
126
    /// for each item.
127
    void clear() {
128
      _data.clear(); _min=0; _num_items=0; _head=-1;
129
    }
130

	
131
    /// \brief Set the priority of an item or insert it, if it is
132
    /// not stored in the heap.
133
    ///
134
    /// This method sets the priority of the given item if it is
135
    /// already stored in the heap. Otherwise it inserts the given
136
    /// item into the heap with the given priority.
137
    /// \param item The item.
138
    /// \param value The priority.
139
    void set (const Item& item, const Prio& value) {
140
      int i=_iim[item];
141
      if ( i >= 0 && _data[i].in ) {
142
        if ( _comp(value, _data[i].prio) ) decrease(item, value);
143
        if ( _comp(_data[i].prio, value) ) increase(item, value);
144
      } else push(item, value);
145
    }
146

	
147
    /// \brief Insert an item into the heap with the given priority.
148
    ///
149
    /// This function inserts the given item into the heap with the
150
    /// given priority.
151
    /// \param item The item to insert.
152
    /// \param value The priority of the item.
153
    /// \pre \e item must not be stored in the heap.
154
    void push (const Item& item, const Prio& value) {
155
      int i=_iim[item];
156
      if ( i<0 ) {
157
        int s=_data.size();
158
        _iim.set( item,s );
159
        Store st;
160
        st.name=item;
161
        st.prio=value;
162
        _data.push_back(st);
163
        i=s;
164
      }
165
      else {
166
        _data[i].parent=_data[i].right_neighbor=_data[i].child=-1;
167
        _data[i].degree=0;
168
        _data[i].in=true;
169
        _data[i].prio=value;
170
      }
171

	
172
      if( 0==_num_items ) {
173
        _head=i;
174
        _min=i;
175
      } else {
176
        merge(i);
177
        if( _comp(_data[i].prio, _data[_min].prio) ) _min=i;
178
      }
179
      ++_num_items;
180
    }
181

	
182
    /// \brief Return the item having minimum priority.
183
    ///
184
    /// This function returns the item having minimum priority.
185
    /// \pre The heap must be non-empty.
186
    Item top() const { return _data[_min].name; }
187

	
188
    /// \brief The minimum priority.
189
    ///
190
    /// This function returns the minimum priority.
191
    /// \pre The heap must be non-empty.
192
    Prio prio() const { return _data[_min].prio; }
193

	
194
    /// \brief The priority of the given item.
195
    ///
196
    /// This function returns the priority of the given item.
197
    /// \param item The item.
198
    /// \pre \e item must be in the heap.
199
    const Prio& operator[](const Item& item) const {
200
      return _data[_iim[item]].prio;
201
    }
202

	
203
    /// \brief Remove the item having minimum priority.
204
    ///
205
    /// This function removes the item having minimum priority.
206
    /// \pre The heap must be non-empty.
207
    void pop() {
208
      _data[_min].in=false;
209

	
210
      int head_child=-1;
211
      if ( _data[_min].child!=-1 ) {
212
        int child=_data[_min].child;
213
        int neighb;
214
        while( child!=-1 ) {
215
          neighb=_data[child].right_neighbor;
216
          _data[child].parent=-1;
217
          _data[child].right_neighbor=head_child;
218
          head_child=child;
219
          child=neighb;
220
        }
221
      }
222

	
223
      if ( _data[_head].right_neighbor==-1 ) {
224
        // there was only one root
225
        _head=head_child;
226
      }
227
      else {
228
        // there were more roots
229
        if( _head!=_min )  { unlace(_min); }
230
        else { _head=_data[_head].right_neighbor; }
231
        merge(head_child);
232
      }
233
      _min=findMin();
234
      --_num_items;
235
    }
236

	
237
    /// \brief Remove the given item from the heap.
238
    ///
239
    /// This function removes the given item from the heap if it is
240
    /// already stored.
241
    /// \param item The item to delete.
242
    /// \pre \e item must be in the heap.
243
    void erase (const Item& item) {
244
      int i=_iim[item];
245
      if ( i >= 0 && _data[i].in ) {
246
        decrease( item, _data[_min].prio-1 );
247
        pop();
248
      }
249
    }
250

	
251
    /// \brief Decrease the priority of an item to the given value.
252
    ///
253
    /// This function decreases the priority of an item to the given value.
254
    /// \param item The item.
255
    /// \param value The priority.
256
    /// \pre \e item must be stored in the heap with priority at least \e value.
257
    void decrease (Item item, const Prio& value) {
258
      int i=_iim[item];
259
      int p=_data[i].parent;
260
      _data[i].prio=value;
261
      
262
      while( p!=-1 && _comp(value, _data[p].prio) ) {
263
        _data[i].name=_data[p].name;
264
        _data[i].prio=_data[p].prio;
265
        _data[p].name=item;
266
        _data[p].prio=value;
267
        _iim[_data[i].name]=i;
268
        i=p;
269
        p=_data[p].parent;
270
      }
271
      _iim[item]=i;
272
      if ( _comp(value, _data[_min].prio) ) _min=i;
273
    }
274

	
275
    /// \brief Increase the priority of an item to the given value.
276
    ///
277
    /// This function increases the priority of an item to the given value.
278
    /// \param item The item.
279
    /// \param value The priority.
280
    /// \pre \e item must be stored in the heap with priority at most \e value.
281
    void increase (Item item, const Prio& value) {
282
      erase(item);
283
      push(item, value);
284
    }
285

	
286
    /// \brief Return the state of an item.
287
    ///
288
    /// This method returns \c PRE_HEAP if the given item has never
289
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
290
    /// and \c POST_HEAP otherwise.
291
    /// In the latter case it is possible that the item will get back
292
    /// to the heap again.
293
    /// \param item The item.
294
    State state(const Item &item) const {
295
      int i=_iim[item];
296
      if( i>=0 ) {
297
        if ( _data[i].in ) i=0;
298
        else i=-2;
299
      }
300
      return State(i);
301
    }
302

	
303
    /// \brief Set the state of an item in the heap.
304
    ///
305
    /// This function sets the state of the given item in the heap.
306
    /// It can be used to manually clear the heap when it is important
307
    /// to achive better time complexity.
308
    /// \param i The item.
309
    /// \param st The state. It should not be \c IN_HEAP.
310
    void state(const Item& i, State st) {
311
      switch (st) {
312
      case POST_HEAP:
313
      case PRE_HEAP:
314
        if (state(i) == IN_HEAP) {
315
          erase(i);
316
        }
317
        _iim[i] = st;
318
        break;
319
      case IN_HEAP:
320
        break;
321
      }
322
    }
323

	
324
  private:
325
    
326
    // Find the minimum of the roots
327
    int findMin() {
328
      if( _head!=-1 ) {
329
        int min_loc=_head, min_val=_data[_head].prio;
330
        for( int x=_data[_head].right_neighbor; x!=-1;
331
             x=_data[x].right_neighbor ) {
332
          if( _comp( _data[x].prio,min_val ) ) {
333
            min_val=_data[x].prio;
334
            min_loc=x;
335
          }
336
        }
337
        return min_loc;
338
      }
339
      else return -1;
340
    }
341

	
342
    // Merge the heap with another heap starting at the given position
343
    void merge(int a) {
344
      if( _head==-1 || a==-1 ) return;
345
      if( _data[a].right_neighbor==-1 &&
346
          _data[a].degree<=_data[_head].degree ) {
347
        _data[a].right_neighbor=_head;
348
        _head=a;
349
      } else {
350
        interleave(a);
351
      }
352
      if( _data[_head].right_neighbor==-1 ) return;
353
      
354
      int x=_head;
355
      int x_prev=-1, x_next=_data[x].right_neighbor;
356
      while( x_next!=-1 ) {
357
        if( _data[x].degree!=_data[x_next].degree ||
358
            ( _data[x_next].right_neighbor!=-1 &&
359
              _data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) {
360
          x_prev=x;
361
          x=x_next;
362
        }
363
        else {
364
          if( _comp(_data[x_next].prio,_data[x].prio) ) {
365
            if( x_prev==-1 ) {
366
              _head=x_next;
367
            } else {
368
              _data[x_prev].right_neighbor=x_next;
369
            }
370
            fuse(x,x_next);
371
            x=x_next;
372
          }
373
          else {
374
            _data[x].right_neighbor=_data[x_next].right_neighbor;
375
            fuse(x_next,x);
376
          }
377
        }
378
        x_next=_data[x].right_neighbor;
379
      }
380
    }
381

	
382
    // Interleave the elements of the given list into the list of the roots
383
    void interleave(int a) {
384
      int p=_head, q=a;
385
      int curr=_data.size();
386
      _data.push_back(Store());
387
      
388
      while( p!=-1 || q!=-1 ) {
389
        if( q==-1 || ( p!=-1 && _data[p].degree<_data[q].degree ) ) {
390
          _data[curr].right_neighbor=p;
391
          curr=p;
392
          p=_data[p].right_neighbor;
393
        }
394
        else {
395
          _data[curr].right_neighbor=q;
396
          curr=q;
397
          q=_data[q].right_neighbor;
398
        }
399
      }
400
      
401
      _head=_data.back().right_neighbor;
402
      _data.pop_back();
403
    }
404

	
405
    // Lace node a under node b
406
    void fuse(int a, int b) {
407
      _data[a].parent=b;
408
      _data[a].right_neighbor=_data[b].child;
409
      _data[b].child=a;
410

	
411
      ++_data[b].degree;
412
    }
413

	
414
    // Unlace node a (if it has siblings)
415
    void unlace(int a) {
416
      int neighb=_data[a].right_neighbor;
417
      int other=_head;
418

	
419
      while( _data[other].right_neighbor!=a )
420
        other=_data[other].right_neighbor;
421
      _data[other].right_neighbor=neighb;
422
    }
423

	
424
  private:
425

	
426
    class Store {
427
      friend class BinomHeap;
428

	
429
      Item name;
430
      int parent;
431
      int right_neighbor;
432
      int child;
433
      int degree;
434
      bool in;
435
      Prio prio;
436

	
437
      Store() : parent(-1), right_neighbor(-1), child(-1), degree(0),
438
        in(true) {}
439
    };
440
  };
441

	
442
} //namespace lemon
443

	
444
#endif //LEMON_BINOM_HEAP_H
445

	
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
///\file
20
///\brief Some basic non-inline functions and static global data.
21

	
22
#include<lemon/bits/windows.h>
23

	
24
#ifdef WIN32
25
#ifndef WIN32_LEAN_AND_MEAN
26
#define WIN32_LEAN_AND_MEAN
27
#endif
28
#ifndef NOMINMAX
29
#define NOMINMAX
30
#endif
31
#ifdef UNICODE
32
#undef UNICODE
33
#endif
34
#include <windows.h>
35
#ifdef LOCALE_INVARIANT
36
#define MY_LOCALE LOCALE_INVARIANT
37
#else
38
#define MY_LOCALE LOCALE_NEUTRAL
39
#endif
40
#else
41
#include <unistd.h>
42
#include <ctime>
43
#include <sys/times.h>
44
#include <sys/time.h>
45
#endif
46

	
47
#include <cmath>
48
#include <sstream>
49

	
50
namespace lemon {
51
  namespace bits {
52
    void getWinProcTimes(double &rtime,
53
                         double &utime, double &stime,
54
                         double &cutime, double &cstime)
55
    {
56
#ifdef WIN32
57
      static const double ch = 4294967296.0e-7;
58
      static const double cl = 1.0e-7;
59

	
60
      FILETIME system;
61
      GetSystemTimeAsFileTime(&system);
62
      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
63

	
64
      FILETIME create, exit, kernel, user;
65
      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
66
        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
67
        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
68
        cutime = 0;
69
        cstime = 0;
70
      } else {
71
        rtime = 0;
72
        utime = 0;
73
        stime = 0;
74
        cutime = 0;
75
        cstime = 0;
76
      }
77
#else
78
      timeval tv;
79
      gettimeofday(&tv, 0);
80
      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
81

	
82
      tms ts;
83
      double tck=sysconf(_SC_CLK_TCK);
84
      times(&ts);
85
      utime=ts.tms_utime/tck;
86
      stime=ts.tms_stime/tck;
87
      cutime=ts.tms_cutime/tck;
88
      cstime=ts.tms_cstime/tck;
89
#endif
90
    }
91

	
92
    std::string getWinFormattedDate()
93
    {
94
      std::ostringstream os;
95
#ifdef WIN32
96
      SYSTEMTIME time;
97
      GetSystemTime(&time);
98
      char buf1[11], buf2[9], buf3[5];
99
	  if (GetDateFormat(MY_LOCALE, 0, &time,
100
                        ("ddd MMM dd"), buf1, 11) &&
101
          GetTimeFormat(MY_LOCALE, 0, &time,
102
                        ("HH':'mm':'ss"), buf2, 9) &&
103
          GetDateFormat(MY_LOCALE, 0, &time,
104
                        ("yyyy"), buf3, 5)) {
105
        os << buf1 << ' ' << buf2 << ' ' << buf3;
106
      }
107
      else os << "unknown";
108
#else
109
      timeval tv;
110
      gettimeofday(&tv, 0);
111

	
112
      char cbuf[26];
113
      ctime_r(&tv.tv_sec,cbuf);
114
      os << cbuf;
115
#endif
116
      return os.str();
117
    }
118

	
119
    int getWinRndSeed()
120
    {
121
#ifdef WIN32
122
      FILETIME time;
123
      GetSystemTimeAsFileTime(&time);
124
      return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
125
#else
126
      timeval tv;
127
      gettimeofday(&tv, 0);
128
      return getpid() + tv.tv_sec + tv.tv_usec;
129
#endif
130
    }
131
  }
132
}
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_WINDOWS_H
20
#define LEMON_BITS_WINDOWS_H
21

	
22
#include <string>
23

	
24
namespace lemon {
25
  namespace bits {
26
    void getWinProcTimes(double &rtime,
27
                         double &utime, double &stime,
28
                         double &cutime, double &cstime);
29
    std::string getWinFormattedDate();
30
    int getWinRndSeed();
31
  }
32
}
33

	
34
#endif
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BUCKET_HEAP_H
20
#define LEMON_BUCKET_HEAP_H
21

	
22
///\ingroup heaps
23
///\file
24
///\brief Bucket heap implementation.
25

	
26
#include <vector>
27
#include <utility>
28
#include <functional>
29

	
30
namespace lemon {
31

	
32
  namespace _bucket_heap_bits {
33

	
34
    template <bool MIN>
35
    struct DirectionTraits {
36
      static bool less(int left, int right) {
37
        return left < right;
38
      }
39
      static void increase(int& value) {
40
        ++value;
41
      }
42
    };
43

	
44
    template <>
45
    struct DirectionTraits<false> {
46
      static bool less(int left, int right) {
47
        return left > right;
48
      }
49
      static void increase(int& value) {
50
        --value;
51
      }
52
    };
53

	
54
  }
55

	
56
  /// \ingroup heaps
57
  ///
58
  /// \brief Bucket heap data structure.
59
  ///
60
  /// This class implements the \e bucket \e heap data structure.
61
  /// It practically conforms to the \ref concepts::Heap "heap concept",
62
  /// but it has some limitations.
63
  ///
64
  /// The bucket heap is a very simple structure. It can store only
65
  /// \c int priorities and it maintains a list of items for each priority
66
  /// in the range <tt>[0..C)</tt>. So it should only be used when the
67
  /// priorities are small. It is not intended to use as a Dijkstra heap.
68
  ///
69
  /// \tparam IM A read-writable item map with \c int values, used
70
  /// internally to handle the cross references.
71
  /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
72
  /// The default is \e min-heap. If this parameter is set to \c false,
73
  /// then the comparison is reversed, so the top(), prio() and pop()
74
  /// functions deal with the item having maximum priority instead of the
75
  /// minimum.
76
  ///
77
  /// \sa SimpleBucketHeap
78
  template <typename IM, bool MIN = true>
79
  class BucketHeap {
80

	
81
  public:
82

	
83
    /// Type of the item-int map.
84
    typedef IM ItemIntMap;
85
    /// Type of the priorities.
86
    typedef int Prio;
87
    /// Type of the items stored in the heap.
88
    typedef typename ItemIntMap::Key Item;
89
    /// Type of the item-priority pairs.
90
    typedef std::pair<Item,Prio> Pair;
91

	
92
  private:
93

	
94
    typedef _bucket_heap_bits::DirectionTraits<MIN> Direction;
95

	
96
  public:
97

	
98
    /// \brief Type to represent the states of the items.
99
    ///
100
    /// Each item has a state associated to it. It can be "in heap",
101
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
102
    /// heap's point of view, but may be useful to the user.
103
    ///
104
    /// The item-int map must be initialized in such way that it assigns
105
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
106
    enum State {
107
      IN_HEAP = 0,    ///< = 0.
108
      PRE_HEAP = -1,  ///< = -1.
109
      POST_HEAP = -2  ///< = -2.
110
    };
111

	
112
  public:
113

	
114
    /// \brief Constructor.
115
    ///
116
    /// Constructor.
117
    /// \param map A map that assigns \c int values to the items.
118
    /// It is used internally to handle the cross references.
119
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
120
    explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {}
121

	
122
    /// \brief The number of items stored in the heap.
123
    ///
124
    /// This function returns the number of items stored in the heap.
125
    int size() const { return _data.size(); }
126

	
127
    /// \brief Check if the heap is empty.
128
    ///
129
    /// This function returns \c true if the heap is empty.
130
    bool empty() const { return _data.empty(); }
131

	
132
    /// \brief Make the heap empty.
133
    ///
134
    /// This functon makes the heap empty.
135
    /// It does not change the cross reference map. If you want to reuse
136
    /// a heap that is not surely empty, you should first clear it and
137
    /// then you should set the cross reference map to \c PRE_HEAP
138
    /// for each item.
139
    void clear() {
140
      _data.clear(); _first.clear(); _minimum = 0;
141
    }
142

	
143
  private:
144

	
145
    void relocateLast(int idx) {
146
      if (idx + 1 < int(_data.size())) {
147
        _data[idx] = _data.back();
148
        if (_data[idx].prev != -1) {
149
          _data[_data[idx].prev].next = idx;
150
        } else {
151
          _first[_data[idx].value] = idx;
152
        }
153
        if (_data[idx].next != -1) {
154
          _data[_data[idx].next].prev = idx;
155
        }
156
        _iim[_data[idx].item] = idx;
157
      }
158
      _data.pop_back();
159
    }
160

	
161
    void unlace(int idx) {
162
      if (_data[idx].prev != -1) {
163
        _data[_data[idx].prev].next = _data[idx].next;
164
      } else {
165
        _first[_data[idx].value] = _data[idx].next;
166
      }
167
      if (_data[idx].next != -1) {
168
        _data[_data[idx].next].prev = _data[idx].prev;
169
      }
170
    }
171

	
172
    void lace(int idx) {
173
      if (int(_first.size()) <= _data[idx].value) {
174
        _first.resize(_data[idx].value + 1, -1);
175
      }
176
      _data[idx].next = _first[_data[idx].value];
177
      if (_data[idx].next != -1) {
178
        _data[_data[idx].next].prev = idx;
179
      }
180
      _first[_data[idx].value] = idx;
181
      _data[idx].prev = -1;
182
    }
183

	
184
  public:
185

	
186
    /// \brief Insert a pair of item and priority into the heap.
187
    ///
188
    /// This function inserts \c p.first to the heap with priority
189
    /// \c p.second.
190
    /// \param p The pair to insert.
191
    /// \pre \c p.first must not be stored in the heap.
192
    void push(const Pair& p) {
193
      push(p.first, p.second);
194
    }
195

	
196
    /// \brief Insert an item into the heap with the given priority.
197
    ///
198
    /// This function inserts the given item into the heap with the
199
    /// given priority.
200
    /// \param i The item to insert.
201
    /// \param p The priority of the item.
202
    /// \pre \e i must not be stored in the heap.
203
    void push(const Item &i, const Prio &p) {
204
      int idx = _data.size();
205
      _iim[i] = idx;
206
      _data.push_back(BucketItem(i, p));
207
      lace(idx);
208
      if (Direction::less(p, _minimum)) {
209
        _minimum = p;
210
      }
211
    }
212

	
213
    /// \brief Return the item having minimum priority.
214
    ///
215
    /// This function returns the item having minimum priority.
216
    /// \pre The heap must be non-empty.
217
    Item top() const {
218
      while (_first[_minimum] == -1) {
219
        Direction::increase(_minimum);
220
      }
221
      return _data[_first[_minimum]].item;
222
    }
223

	
224
    /// \brief The minimum priority.
225
    ///
226
    /// This function returns the minimum priority.
227
    /// \pre The heap must be non-empty.
228
    Prio prio() const {
229
      while (_first[_minimum] == -1) {
230
        Direction::increase(_minimum);
231
      }
232
      return _minimum;
233
    }
234

	
235
    /// \brief Remove the item having minimum priority.
236
    ///
237
    /// This function removes the item having minimum priority.
238
    /// \pre The heap must be non-empty.
239
    void pop() {
240
      while (_first[_minimum] == -1) {
241
        Direction::increase(_minimum);
242
      }
243
      int idx = _first[_minimum];
244
      _iim[_data[idx].item] = -2;
245
      unlace(idx);
246
      relocateLast(idx);
247
    }
248

	
249
    /// \brief Remove the given item from the heap.
250
    ///
251
    /// This function removes the given item from the heap if it is
252
    /// already stored.
253
    /// \param i The item to delete.
254
    /// \pre \e i must be in the heap.
255
    void erase(const Item &i) {
256
      int idx = _iim[i];
257
      _iim[_data[idx].item] = -2;
258
      unlace(idx);
259
      relocateLast(idx);
260
    }
261

	
262
    /// \brief The priority of the given item.
263
    ///
264
    /// This function returns the priority of the given item.
265
    /// \param i The item.
266
    /// \pre \e i must be in the heap.
267
    Prio operator[](const Item &i) const {
268
      int idx = _iim[i];
269
      return _data[idx].value;
270
    }
271

	
272
    /// \brief Set the priority of an item or insert it, if it is
273
    /// not stored in the heap.
274
    ///
275
    /// This method sets the priority of the given item if it is
276
    /// already stored in the heap. Otherwise it inserts the given
277
    /// item into the heap with the given priority.
278
    /// \param i The item.
279
    /// \param p The priority.
280
    void set(const Item &i, const Prio &p) {
281
      int idx = _iim[i];
282
      if (idx < 0) {
283
        push(i, p);
284
      } else if (Direction::less(p, _data[idx].value)) {
285
        decrease(i, p);
286
      } else {
287
        increase(i, p);
288
      }
289
    }
290

	
291
    /// \brief Decrease the priority of an item to the given value.
292
    ///
293
    /// This function decreases the priority of an item to the given value.
294
    /// \param i The item.
295
    /// \param p The priority.
296
    /// \pre \e i must be stored in the heap with priority at least \e p.
297
    void decrease(const Item &i, const Prio &p) {
298
      int idx = _iim[i];
299
      unlace(idx);
300
      _data[idx].value = p;
301
      if (Direction::less(p, _minimum)) {
302
        _minimum = p;
303
      }
304
      lace(idx);
305
    }
306

	
307
    /// \brief Increase the priority of an item to the given value.
308
    ///
309
    /// This function increases the priority of an item to the given value.
310
    /// \param i The item.
311
    /// \param p The priority.
312
    /// \pre \e i must be stored in the heap with priority at most \e p.
313
    void increase(const Item &i, const Prio &p) {
314
      int idx = _iim[i];
315
      unlace(idx);
316
      _data[idx].value = p;
317
      lace(idx);
318
    }
319

	
320
    /// \brief Return the state of an item.
321
    ///
322
    /// This method returns \c PRE_HEAP if the given item has never
323
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
324
    /// and \c POST_HEAP otherwise.
325
    /// In the latter case it is possible that the item will get back
326
    /// to the heap again.
327
    /// \param i The item.
328
    State state(const Item &i) const {
329
      int idx = _iim[i];
330
      if (idx >= 0) idx = 0;
331
      return State(idx);
332
    }
333

	
334
    /// \brief Set the state of an item in the heap.
335
    ///
336
    /// This function sets the state of the given item in the heap.
337
    /// It can be used to manually clear the heap when it is important
338
    /// to achive better time complexity.
339
    /// \param i The item.
340
    /// \param st The state. It should not be \c IN_HEAP.
341
    void state(const Item& i, State st) {
342
      switch (st) {
343
      case POST_HEAP:
344
      case PRE_HEAP:
345
        if (state(i) == IN_HEAP) {
346
          erase(i);
347
        }
348
        _iim[i] = st;
349
        break;
350
      case IN_HEAP:
351
        break;
352
      }
353
    }
354

	
355
  private:
356

	
357
    struct BucketItem {
358
      BucketItem(const Item& _item, int _value)
359
        : item(_item), value(_value) {}
360

	
361
      Item item;
362
      int value;
363

	
364
      int prev, next;
365
    };
366

	
367
    ItemIntMap& _iim;
368
    std::vector<int> _first;
369
    std::vector<BucketItem> _data;
370
    mutable int _minimum;
371

	
372
  }; // class BucketHeap
373

	
374
  /// \ingroup heaps
375
  ///
376
  /// \brief Simplified bucket heap data structure.
377
  ///
378
  /// This class implements a simplified \e bucket \e heap data
379
  /// structure. It does not provide some functionality, but it is
380
  /// faster and simpler than BucketHeap. The main difference is
381
  /// that BucketHeap stores a doubly-linked list for each key while
382
  /// this class stores only simply-linked lists. It supports erasing
383
  /// only for the item having minimum priority and it does not support
384
  /// key increasing and decreasing.
385
  ///
386
  /// Note that this implementation does not conform to the
387
  /// \ref concepts::Heap "heap concept" due to the lack of some 
388
  /// functionality.
389
  ///
390
  /// \tparam IM A read-writable item map with \c int values, used
391
  /// internally to handle the cross references.
392
  /// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap.
393
  /// The default is \e min-heap. If this parameter is set to \c false,
394
  /// then the comparison is reversed, so the top(), prio() and pop()
395
  /// functions deal with the item having maximum priority instead of the
396
  /// minimum.
397
  ///
398
  /// \sa BucketHeap
399
  template <typename IM, bool MIN = true >
400
  class SimpleBucketHeap {
401

	
402
  public:
403

	
404
    /// Type of the item-int map.
405
    typedef IM ItemIntMap;
406
    /// Type of the priorities.
407
    typedef int Prio;
408
    /// Type of the items stored in the heap.
409
    typedef typename ItemIntMap::Key Item;
410
    /// Type of the item-priority pairs.
411
    typedef std::pair<Item,Prio> Pair;
412

	
413
  private:
414

	
415
    typedef _bucket_heap_bits::DirectionTraits<MIN> Direction;
416

	
417
  public:
418

	
419
    /// \brief Type to represent the states of the items.
420
    ///
421
    /// Each item has a state associated to it. It can be "in heap",
422
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
423
    /// heap's point of view, but may be useful to the user.
424
    ///
425
    /// The item-int map must be initialized in such way that it assigns
426
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
427
    enum State {
428
      IN_HEAP = 0,    ///< = 0.
429
      PRE_HEAP = -1,  ///< = -1.
430
      POST_HEAP = -2  ///< = -2.
431
    };
432

	
433
  public:
434

	
435
    /// \brief Constructor.
436
    ///
437
    /// Constructor.
438
    /// \param map A map that assigns \c int values to the items.
439
    /// It is used internally to handle the cross references.
440
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
441
    explicit SimpleBucketHeap(ItemIntMap &map)
442
      : _iim(map), _free(-1), _num(0), _minimum(0) {}
443

	
444
    /// \brief The number of items stored in the heap.
445
    ///
446
    /// This function returns the number of items stored in the heap.
447
    int size() const { return _num; }
448

	
449
    /// \brief Check if the heap is empty.
450
    ///
451
    /// This function returns \c true if the heap is empty.
452
    bool empty() const { return _num == 0; }
453

	
454
    /// \brief Make the heap empty.
455
    ///
456
    /// This functon makes the heap empty.
457
    /// It does not change the cross reference map. If you want to reuse
458
    /// a heap that is not surely empty, you should first clear it and
459
    /// then you should set the cross reference map to \c PRE_HEAP
460
    /// for each item.
461
    void clear() {
462
      _data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0;
463
    }
464

	
465
    /// \brief Insert a pair of item and priority into the heap.
466
    ///
467
    /// This function inserts \c p.first to the heap with priority
468
    /// \c p.second.
469
    /// \param p The pair to insert.
470
    /// \pre \c p.first must not be stored in the heap.
471
    void push(const Pair& p) {
472
      push(p.first, p.second);
473
    }
474

	
475
    /// \brief Insert an item into the heap with the given priority.
476
    ///
477
    /// This function inserts the given item into the heap with the
478
    /// given priority.
479
    /// \param i The item to insert.
480
    /// \param p The priority of the item.
481
    /// \pre \e i must not be stored in the heap.
482
    void push(const Item &i, const Prio &p) {
483
      int idx;
484
      if (_free == -1) {
485
        idx = _data.size();
486
        _data.push_back(BucketItem(i));
487
      } else {
488
        idx = _free;
489
        _free = _data[idx].next;
490
        _data[idx].item = i;
491
      }
492
      _iim[i] = idx;
493
      if (p >= int(_first.size())) _first.resize(p + 1, -1);
494
      _data[idx].next = _first[p];
495
      _first[p] = idx;
496
      if (Direction::less(p, _minimum)) {
497
        _minimum = p;
498
      }
499
      ++_num;
500
    }
501

	
502
    /// \brief Return the item having minimum priority.
503
    ///
504
    /// This function returns the item having minimum priority.
505
    /// \pre The heap must be non-empty.
506
    Item top() const {
507
      while (_first[_minimum] == -1) {
508
        Direction::increase(_minimum);
509
      }
510
      return _data[_first[_minimum]].item;
511
    }
512

	
513
    /// \brief The minimum priority.
514
    ///
515
    /// This function returns the minimum priority.
516
    /// \pre The heap must be non-empty.
517
    Prio prio() const {
518
      while (_first[_minimum] == -1) {
519
        Direction::increase(_minimum);
520
      }
521
      return _minimum;
522
    }
523

	
524
    /// \brief Remove the item having minimum priority.
525
    ///
526
    /// This function removes the item having minimum priority.
527
    /// \pre The heap must be non-empty.
528
    void pop() {
529
      while (_first[_minimum] == -1) {
530
        Direction::increase(_minimum);
531
      }
532
      int idx = _first[_minimum];
533
      _iim[_data[idx].item] = -2;
534
      _first[_minimum] = _data[idx].next;
535
      _data[idx].next = _free;
536
      _free = idx;
537
      --_num;
538
    }
539

	
540
    /// \brief The priority of the given item.
541
    ///
542
    /// This function returns the priority of the given item.
543
    /// \param i The item.
544
    /// \pre \e i must be in the heap.
545
    /// \warning This operator is not a constant time function because
546
    /// it scans the whole data structure to find the proper value.
547
    Prio operator[](const Item &i) const {
548
      for (int k = 0; k < int(_first.size()); ++k) {
549
        int idx = _first[k];
550
        while (idx != -1) {
551
          if (_data[idx].item == i) {
552
            return k;
553
          }
554
          idx = _data[idx].next;
555
        }
556
      }
557
      return -1;
558
    }
559

	
560
    /// \brief Return the state of an item.
561
    ///
562
    /// This method returns \c PRE_HEAP if the given item has never
563
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
564
    /// and \c POST_HEAP otherwise.
565
    /// In the latter case it is possible that the item will get back
566
    /// to the heap again.
567
    /// \param i The item.
568
    State state(const Item &i) const {
569
      int idx = _iim[i];
570
      if (idx >= 0) idx = 0;
571
      return State(idx);
572
    }
573

	
574
  private:
575

	
576
    struct BucketItem {
577
      BucketItem(const Item& _item)
578
        : item(_item) {}
579

	
580
      Item item;
581
      int next;
582
    };
583

	
584
    ItemIntMap& _iim;
585
    std::vector<int> _first;
586
    std::vector<BucketItem> _data;
587
    int _free, _num;
588
    mutable int _minimum;
589

	
590
  }; // class SimpleBucketHeap
591

	
592
}
593

	
594
#endif
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
///\file
20
///\brief Implementation of the CBC MIP solver interface.
21

	
22
#include "cbc.h"
23

	
24
#include <coin/CoinModel.hpp>
25
#include <coin/CbcModel.hpp>
26
#include <coin/OsiSolverInterface.hpp>
27

	
28
#ifdef COIN_HAS_CLP
29
#include "coin/OsiClpSolverInterface.hpp"
30
#endif
31
#ifdef COIN_HAS_OSL
32
#include "coin/OsiOslSolverInterface.hpp"
33
#endif
34

	
35
#include "coin/CbcCutGenerator.hpp"
36
#include "coin/CbcHeuristicLocal.hpp"
37
#include "coin/CbcHeuristicGreedy.hpp"
38
#include "coin/CbcHeuristicFPump.hpp"
39
#include "coin/CbcHeuristicRINS.hpp"
40

	
41
#include "coin/CglGomory.hpp"
42
#include "coin/CglProbing.hpp"
43
#include "coin/CglKnapsackCover.hpp"
44
#include "coin/CglOddHole.hpp"
45
#include "coin/CglClique.hpp"
46
#include "coin/CglFlowCover.hpp"
47
#include "coin/CglMixedIntegerRounding.hpp"
48

	
49
#include "coin/CbcHeuristic.hpp"
50

	
51
namespace lemon {
52

	
53
  CbcMip::CbcMip() {
54
    _prob = new CoinModel();
55
    _prob->setProblemName("LEMON");
56
    _osi_solver = 0;
57
    _cbc_model = 0;
58
    messageLevel(MESSAGE_NOTHING);
59
  }
60

	
61
  CbcMip::CbcMip(const CbcMip& other) {
62
    _prob = new CoinModel(*other._prob);
63
    _prob->setProblemName("LEMON");
64
    _osi_solver = 0;
65
    _cbc_model = 0;
66
    messageLevel(MESSAGE_NOTHING);
67
  }
68

	
69
  CbcMip::~CbcMip() {
70
    delete _prob;
71
    if (_osi_solver) delete _osi_solver;
72
    if (_cbc_model) delete _cbc_model;
73
  }
74

	
75
  const char* CbcMip::_solverName() const { return "CbcMip"; }
76

	
77
  int CbcMip::_addCol() {
78
    _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0, 0, false);
79
    return _prob->numberColumns() - 1;
80
  }
81

	
82
  CbcMip* CbcMip::newSolver() const {
83
    CbcMip* newlp = new CbcMip;
84
    return newlp;
85
  }
86

	
87
  CbcMip* CbcMip::cloneSolver() const {
88
    CbcMip* copylp = new CbcMip(*this);
89
    return copylp;
90
  }
91

	
92
  int CbcMip::_addRow() {
93
    _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
94
    return _prob->numberRows() - 1;
95
  }
96

	
97
  int CbcMip::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
98
    std::vector<int> indexes;
99
    std::vector<Value> values;
100

	
101
    for(ExprIterator it = b; it != e; ++it) {
102
      indexes.push_back(it->first);
103
      values.push_back(it->second);
104
    }
105

	
106
    _prob->addRow(values.size(), &indexes.front(), &values.front(), l, u);
107
    return _prob->numberRows() - 1;
108
  }
109

	
110
  void CbcMip::_eraseCol(int i) {
111
    _prob->deleteColumn(i);
112
  }
113

	
114
  void CbcMip::_eraseRow(int i) {
115
    _prob->deleteRow(i);
116
  }
117

	
118
  void CbcMip::_eraseColId(int i) {
119
    cols.eraseIndex(i);
120
  }
121

	
122
  void CbcMip::_eraseRowId(int i) {
123
    rows.eraseIndex(i);
124
  }
125

	
126
  void CbcMip::_getColName(int c, std::string& name) const {
127
    name = _prob->getColumnName(c);
128
  }
129

	
130
  void CbcMip::_setColName(int c, const std::string& name) {
131
    _prob->setColumnName(c, name.c_str());
132
  }
133

	
134
  int CbcMip::_colByName(const std::string& name) const {
135
    return _prob->column(name.c_str());
136
  }
137

	
138
  void CbcMip::_getRowName(int r, std::string& name) const {
139
    name = _prob->getRowName(r);
140
  }
141

	
142
  void CbcMip::_setRowName(int r, const std::string& name) {
143
    _prob->setRowName(r, name.c_str());
144
  }
145

	
146
  int CbcMip::_rowByName(const std::string& name) const {
147
    return _prob->row(name.c_str());
148
  }
149

	
150
  void CbcMip::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
151
    for (ExprIterator it = b; it != e; ++it) {
152
      _prob->setElement(i, it->first, it->second);
153
    }
154
  }
155

	
156
  void CbcMip::_getRowCoeffs(int ix, InsertIterator b) const {
157
    int length = _prob->numberRows();
158

	
159
    std::vector<int> indices(length);
160
    std::vector<Value> values(length);
161

	
162
    length = _prob->getRow(ix, &indices[0], &values[0]);
163

	
164
    for (int i = 0; i < length; ++i) {
165
      *b = std::make_pair(indices[i], values[i]);
166
      ++b;
167
    }
168
  }
169

	
170
  void CbcMip::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
171
    for (ExprIterator it = b; it != e; ++it) {
172
      _prob->setElement(it->first, ix, it->second);
173
    }
174
  }
175

	
176
  void CbcMip::_getColCoeffs(int ix, InsertIterator b) const {
177
    int length = _prob->numberColumns();
178

	
179
    std::vector<int> indices(length);
180
    std::vector<Value> values(length);
181

	
182
    length = _prob->getColumn(ix, &indices[0], &values[0]);
183

	
184
    for (int i = 0; i < length; ++i) {
185
      *b = std::make_pair(indices[i], values[i]);
186
      ++b;
187
    }
188
  }
189

	
190
  void CbcMip::_setCoeff(int ix, int jx, Value value) {
191
    _prob->setElement(ix, jx, value);
192
  }
193

	
194
  CbcMip::Value CbcMip::_getCoeff(int ix, int jx) const {
195
    return _prob->getElement(ix, jx);
196
  }
197

	
198

	
199
  void CbcMip::_setColLowerBound(int i, Value lo) {
200
    LEMON_ASSERT(lo != INF, "Invalid bound");
201
    _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
202
  }
203

	
204
  CbcMip::Value CbcMip::_getColLowerBound(int i) const {
205
    double val = _prob->getColumnLower(i);
206
    return val == - COIN_DBL_MAX ? - INF : val;
207
  }
208

	
209
  void CbcMip::_setColUpperBound(int i, Value up) {
210
    LEMON_ASSERT(up != -INF, "Invalid bound");
211
    _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
212
  }
213

	
214
  CbcMip::Value CbcMip::_getColUpperBound(int i) const {
215
    double val = _prob->getColumnUpper(i);
216
    return val == COIN_DBL_MAX ? INF : val;
217
  }
218

	
219
  void CbcMip::_setRowLowerBound(int i, Value lo) {
220
    LEMON_ASSERT(lo != INF, "Invalid bound");
221
    _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
222
  }
223

	
224
  CbcMip::Value CbcMip::_getRowLowerBound(int i) const {
225
    double val = _prob->getRowLower(i);
226
    return val == - COIN_DBL_MAX ? - INF : val;
227
  }
228

	
229
  void CbcMip::_setRowUpperBound(int i, Value up) {
230
    LEMON_ASSERT(up != -INF, "Invalid bound");
231
    _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
232
  }
233

	
234
  CbcMip::Value CbcMip::_getRowUpperBound(int i) const {
235
    double val = _prob->getRowUpper(i);
236
    return val == COIN_DBL_MAX ? INF : val;
237
  }
238

	
239
  void CbcMip::_setObjCoeffs(ExprIterator b, ExprIterator e) {
240
    int num = _prob->numberColumns();
241
    for (int i = 0; i < num; ++i) {
242
      _prob->setColumnObjective(i, 0.0);
243
    }
244
    for (ExprIterator it = b; it != e; ++it) {
245
      _prob->setColumnObjective(it->first, it->second);
246
    }
247
  }
248

	
249
  void CbcMip::_getObjCoeffs(InsertIterator b) const {
250
    int num = _prob->numberColumns();
251
    for (int i = 0; i < num; ++i) {
252
      Value coef = _prob->getColumnObjective(i);
253
      if (coef != 0.0) {
254
        *b = std::make_pair(i, coef);
255
        ++b;
256
      }
257
    }
258
  }
259

	
260
  void CbcMip::_setObjCoeff(int i, Value obj_coef) {
261
    _prob->setColumnObjective(i, obj_coef);
262
  }
263

	
264
  CbcMip::Value CbcMip::_getObjCoeff(int i) const {
265
    return _prob->getColumnObjective(i);
266
  }
267

	
268
  CbcMip::SolveExitStatus CbcMip::_solve() {
269

	
270
    if (_osi_solver) {
271
      delete _osi_solver;
272
    }
273
#ifdef COIN_HAS_CLP
274
    _osi_solver = new OsiClpSolverInterface();
275
#elif COIN_HAS_OSL
276
    _osi_solver = new OsiOslSolverInterface();
277
#else
278
#error Cannot instantiate Osi solver
279
#endif
280

	
281
    _osi_solver->loadFromCoinModel(*_prob);
282

	
283
    if (_cbc_model) {
284
      delete _cbc_model;
285
    }
286
    _cbc_model= new CbcModel(*_osi_solver);
287

	
288
    _osi_solver->messageHandler()->setLogLevel(_message_level);
289
    _cbc_model->setLogLevel(_message_level);
290

	
291
    _cbc_model->initialSolve();
292
    _cbc_model->solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
293

	
294
    if (!_cbc_model->isInitialSolveAbandoned() &&
295
        _cbc_model->isInitialSolveProvenOptimal() &&
296
        !_cbc_model->isInitialSolveProvenPrimalInfeasible() &&
297
        !_cbc_model->isInitialSolveProvenDualInfeasible()) {
298

	
299
      CglProbing generator1;
300
      generator1.setUsingObjective(true);
301
      generator1.setMaxPass(3);
302
      generator1.setMaxProbe(100);
303
      generator1.setMaxLook(50);
304
      generator1.setRowCuts(3);
305
      _cbc_model->addCutGenerator(&generator1, -1, "Probing");
306

	
307
      CglGomory generator2;
308
      generator2.setLimit(300);
309
      _cbc_model->addCutGenerator(&generator2, -1, "Gomory");
310

	
311
      CglKnapsackCover generator3;
312
      _cbc_model->addCutGenerator(&generator3, -1, "Knapsack");
313

	
314
      CglOddHole generator4;
315
      generator4.setMinimumViolation(0.005);
316
      generator4.setMinimumViolationPer(0.00002);
317
      generator4.setMaximumEntries(200);
318
      _cbc_model->addCutGenerator(&generator4, -1, "OddHole");
319

	
320
      CglClique generator5;
321
      generator5.setStarCliqueReport(false);
322
      generator5.setRowCliqueReport(false);
323
      _cbc_model->addCutGenerator(&generator5, -1, "Clique");
324

	
325
      CglMixedIntegerRounding mixedGen;
326
      _cbc_model->addCutGenerator(&mixedGen, -1, "MixedIntegerRounding");
327

	
328
      CglFlowCover flowGen;
329
      _cbc_model->addCutGenerator(&flowGen, -1, "FlowCover");
330

	
331
#ifdef COIN_HAS_CLP
332
      OsiClpSolverInterface* osiclp =
333
        dynamic_cast<OsiClpSolverInterface*>(_cbc_model->solver());
334
      if (osiclp->getNumRows() < 300 && osiclp->getNumCols() < 500) {
335
        osiclp->setupForRepeatedUse(2, 0);
336
      }
337
#endif
338

	
339
      CbcRounding heuristic1(*_cbc_model);
340
      heuristic1.setWhen(3);
341
      _cbc_model->addHeuristic(&heuristic1);
342

	
343
      CbcHeuristicLocal heuristic2(*_cbc_model);
344
      heuristic2.setWhen(3);
345
      _cbc_model->addHeuristic(&heuristic2);
346

	
347
      CbcHeuristicGreedyCover heuristic3(*_cbc_model);
348
      heuristic3.setAlgorithm(11);
349
      heuristic3.setWhen(3);
350
      _cbc_model->addHeuristic(&heuristic3);
351

	
352
      CbcHeuristicFPump heuristic4(*_cbc_model);
353
      heuristic4.setWhen(3);
354
      _cbc_model->addHeuristic(&heuristic4);
355

	
356
      CbcHeuristicRINS heuristic5(*_cbc_model);
357
      heuristic5.setWhen(3);
358
      _cbc_model->addHeuristic(&heuristic5);
359

	
360
      if (_cbc_model->getNumCols() < 500) {
361
        _cbc_model->setMaximumCutPassesAtRoot(-100);
362
      } else if (_cbc_model->getNumCols() < 5000) {
363
        _cbc_model->setMaximumCutPassesAtRoot(100);
364
      } else {
365
        _cbc_model->setMaximumCutPassesAtRoot(20);
366
      }
367

	
368
      if (_cbc_model->getNumCols() < 5000) {
369
        _cbc_model->setNumberStrong(10);
370
      }
371

	
372
      _cbc_model->solver()->setIntParam(OsiMaxNumIterationHotStart, 100);
373
      _cbc_model->branchAndBound();
374
    }
375

	
376
    if (_cbc_model->isAbandoned()) {
377
      return UNSOLVED;
378
    } else {
379
      return SOLVED;
380
    }
381
  }
382

	
383
  CbcMip::Value CbcMip::_getSol(int i) const {
384
    return _cbc_model->getColSolution()[i];
385
  }
386

	
387
  CbcMip::Value CbcMip::_getSolValue() const {
388
    return _cbc_model->getObjValue();
389
  }
390

	
391
  CbcMip::ProblemType CbcMip::_getType() const {
392
    if (_cbc_model->isProvenOptimal()) {
393
      return OPTIMAL;
394
    } else if (_cbc_model->isContinuousUnbounded()) {
395
      return UNBOUNDED;
396
    }
397
    return FEASIBLE;
398
  }
399

	
400
  void CbcMip::_setSense(Sense sense) {
401
    switch (sense) {
402
    case MIN:
403
      _prob->setOptimizationDirection(1.0);
404
      break;
405
    case MAX:
406
      _prob->setOptimizationDirection(- 1.0);
407
      break;
408
    }
409
  }
410

	
411
  CbcMip::Sense CbcMip::_getSense() const {
412
    if (_prob->optimizationDirection() > 0.0) {
413
      return MIN;
414
    } else if (_prob->optimizationDirection() < 0.0) {
415
      return MAX;
416
    } else {
417
      LEMON_ASSERT(false, "Wrong sense");
418
      return CbcMip::Sense();
419
    }
420
  }
421

	
422
  void CbcMip::_setColType(int i, CbcMip::ColTypes col_type) {
423
    switch (col_type){
424
    case INTEGER:
425
      _prob->setInteger(i);
426
      break;
427
    case REAL:
428
      _prob->setContinuous(i);
429
      break;
430
    default:;
431
      LEMON_ASSERT(false, "Wrong sense");
432
    }
433
  }
434

	
435
  CbcMip::ColTypes CbcMip::_getColType(int i) const {
436
    return _prob->getColumnIsInteger(i) ? INTEGER : REAL;
437
  }
438

	
439
  void CbcMip::_clear() {
440
    delete _prob;
441
    if (_osi_solver) {
442
      delete _osi_solver;
443
      _osi_solver = 0;
444
    }
445
    if (_cbc_model) {
446
      delete _cbc_model;
447
      _cbc_model = 0;
448
    }
449

	
450
    _prob = new CoinModel();
451
    rows.clear();
452
    cols.clear();
453
  }
454

	
455
  void CbcMip::_messageLevel(MessageLevel level) {
456
    switch (level) {
457
    case MESSAGE_NOTHING:
458
      _message_level = 0;
459
      break;
460
    case MESSAGE_ERROR:
461
      _message_level = 1;
462
      break;
463
    case MESSAGE_WARNING:
464
      _message_level = 1;
465
      break;
466
    case MESSAGE_NORMAL:
467
      _message_level = 2;
468
      break;
469
    case MESSAGE_VERBOSE:
470
      _message_level = 3;
471
      break;
472
    }
473
  }
474

	
475
} //END OF NAMESPACE LEMON
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
// -*- C++ -*-
20
#ifndef LEMON_CBC_H
21
#define LEMON_CBC_H
22

	
23
///\file
24
///\brief Header of the LEMON-CBC mip solver interface.
25
///\ingroup lp_group
26

	
27
#include <lemon/lp_base.h>
28

	
29
class CoinModel;
30
class OsiSolverInterface;
31
class CbcModel;
32

	
33
namespace lemon {
34

	
35
  /// \brief Interface for the CBC MIP solver
36
  ///
37
  /// This class implements an interface for the CBC MIP solver.
38
  ///\ingroup lp_group
39
  class CbcMip : public MipSolver {
40
  protected:
41

	
42
    CoinModel *_prob;
43
    OsiSolverInterface *_osi_solver;
44
    CbcModel *_cbc_model;
45

	
46
  public:
47

	
48
    /// \e
49
    CbcMip();
50
    /// \e
51
    CbcMip(const CbcMip&);
52
    /// \e
53
    ~CbcMip();
54
    /// \e
55
    virtual CbcMip* newSolver() const;
56
    /// \e
57
    virtual CbcMip* cloneSolver() const;
58

	
59
  protected:
60

	
61
    virtual const char* _solverName() const;
62

	
63
    virtual int _addCol();
64
    virtual int _addRow();
65
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
66

	
67
    virtual void _eraseCol(int i);
68
    virtual void _eraseRow(int i);
69

	
70
    virtual void _eraseColId(int i);
71
    virtual void _eraseRowId(int i);
72

	
73
    virtual void _getColName(int col, std::string& name) const;
74
    virtual void _setColName(int col, const std::string& name);
75
    virtual int _colByName(const std::string& name) const;
76

	
77
    virtual void _getRowName(int row, std::string& name) const;
78
    virtual void _setRowName(int row, const std::string& name);
79
    virtual int _rowByName(const std::string& name) const;
80

	
81
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
82
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
83

	
84
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
85
    virtual void _getColCoeffs(int i, InsertIterator b) const;
86

	
87
    virtual void _setCoeff(int row, int col, Value value);
88
    virtual Value _getCoeff(int row, int col) const;
89

	
90
    virtual void _setColLowerBound(int i, Value value);
91
    virtual Value _getColLowerBound(int i) const;
92
    virtual void _setColUpperBound(int i, Value value);
93
    virtual Value _getColUpperBound(int i) const;
94

	
95
    virtual void _setRowLowerBound(int i, Value value);
96
    virtual Value _getRowLowerBound(int i) const;
97
    virtual void _setRowUpperBound(int i, Value value);
98
    virtual Value _getRowUpperBound(int i) const;
99

	
100
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
101
    virtual void _getObjCoeffs(InsertIterator b) const;
102

	
103
    virtual void _setObjCoeff(int i, Value obj_coef);
104
    virtual Value _getObjCoeff(int i) const;
105

	
106
    virtual void _setSense(Sense sense);
107
    virtual Sense _getSense() const;
108

	
109
    virtual ColTypes _getColType(int col) const;
110
    virtual void _setColType(int col, ColTypes col_type);
111

	
112
    virtual SolveExitStatus _solve();
113
    virtual ProblemType _getType() const;
114
    virtual Value _getSol(int i) const;
115
    virtual Value _getSolValue() const;
116

	
117
    virtual void _clear();
118

	
119
    virtual void _messageLevel(MessageLevel level);
120
    void _applyMessageLevel();
121

	
122
    int _message_level;
123

	
124
    
125

	
126
  };
127

	
128
}
129

	
130
#endif
Ignore white space 6 line context
1
#define LEMON_VERSION "@PROJECT_VERSION@"
2
#cmakedefine LEMON_HAVE_LONG_LONG 1
3
#cmakedefine LEMON_HAVE_LP 1
4
#cmakedefine LEMON_HAVE_MIP 1
5
#cmakedefine LEMON_HAVE_GLPK 1
6
#cmakedefine LEMON_HAVE_CPLEX 1
7
#cmakedefine LEMON_HAVE_CLP 1
8
#cmakedefine LEMON_HAVE_CBC 1
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_EULER_H
20
#define LEMON_EULER_H
21

	
22
#include<lemon/core.h>
23
#include<lemon/adaptors.h>
24
#include<lemon/connectivity.h>
25
#include <list>
26

	
27
/// \ingroup graph_properties
28
/// \file
29
/// \brief Euler tour iterators and a function for checking the \e Eulerian 
30
/// property.
31
///
32
///This file provides Euler tour iterators and a function to check
33
///if a (di)graph is \e Eulerian.
34

	
35
namespace lemon {
36

	
37
  ///Euler tour iterator for digraphs.
38

	
39
  /// \ingroup graph_prop
40
  ///This iterator provides an Euler tour (Eulerian circuit) of a \e directed
41
  ///graph (if there exists) and it converts to the \c Arc type of the digraph.
42
  ///
43
  ///For example, if the given digraph has an Euler tour (i.e it has only one
44
  ///non-trivial component and the in-degree is equal to the out-degree 
45
  ///for all nodes), then the following code will put the arcs of \c g
46
  ///to the vector \c et according to an Euler tour of \c g.
47
  ///\code
48
  ///  std::vector<ListDigraph::Arc> et;
49
  ///  for(DiEulerIt<ListDigraph> e(g); e!=INVALID; ++e)
50
  ///    et.push_back(e);
51
  ///\endcode
52
  ///If \c g has no Euler tour, then the resulted walk will not be closed
53
  ///or not contain all arcs.
54
  ///\sa EulerIt
55
  template<typename GR>
56
  class DiEulerIt
57
  {
58
    typedef typename GR::Node Node;
59
    typedef typename GR::NodeIt NodeIt;
60
    typedef typename GR::Arc Arc;
61
    typedef typename GR::ArcIt ArcIt;
62
    typedef typename GR::OutArcIt OutArcIt;
63
    typedef typename GR::InArcIt InArcIt;
64

	
65
    const GR &g;
66
    typename GR::template NodeMap<OutArcIt> narc;
67
    std::list<Arc> euler;
68

	
69
  public:
70

	
71
    ///Constructor
72

	
73
    ///Constructor.
74
    ///\param gr A digraph.
75
    ///\param start The starting point of the tour. If it is not given,
76
    ///the tour will start from the first node that has an outgoing arc.
77
    DiEulerIt(const GR &gr, typename GR::Node start = INVALID)
78
      : g(gr), narc(g)
79
    {
80
      if (start==INVALID) {
81
        NodeIt n(g);
82
        while (n!=INVALID && OutArcIt(g,n)==INVALID) ++n;
83
        start=n;
84
      }
85
      if (start!=INVALID) {
86
        for (NodeIt n(g); n!=INVALID; ++n) narc[n]=OutArcIt(g,n);
87
        while (narc[start]!=INVALID) {
88
          euler.push_back(narc[start]);
89
          Node next=g.target(narc[start]);
90
          ++narc[start];
91
          start=next;
92
        }
93
      }
94
    }
95

	
96
    ///Arc conversion
97
    operator Arc() { return euler.empty()?INVALID:euler.front(); }
98
    ///Compare with \c INVALID
99
    bool operator==(Invalid) { return euler.empty(); }
100
    ///Compare with \c INVALID
101
    bool operator!=(Invalid) { return !euler.empty(); }
102

	
103
    ///Next arc of the tour
104

	
105
    ///Next arc of the tour
106
    ///
107
    DiEulerIt &operator++() {
108
      Node s=g.target(euler.front());
109
      euler.pop_front();
110
      typename std::list<Arc>::iterator next=euler.begin();
111
      while(narc[s]!=INVALID) {
112
        euler.insert(next,narc[s]);
113
        Node n=g.target(narc[s]);
114
        ++narc[s];
115
        s=n;
116
      }
117
      return *this;
118
    }
119
    ///Postfix incrementation
120

	
121
    /// Postfix incrementation.
122
    ///
123
    ///\warning This incrementation
124
    ///returns an \c Arc, not a \ref DiEulerIt, as one may
125
    ///expect.
126
    Arc operator++(int)
127
    {
128
      Arc e=*this;
129
      ++(*this);
130
      return e;
131
    }
132
  };
133

	
134
  ///Euler tour iterator for graphs.
135

	
136
  /// \ingroup graph_properties
137
  ///This iterator provides an Euler tour (Eulerian circuit) of an
138
  ///\e undirected graph (if there exists) and it converts to the \c Arc
139
  ///and \c Edge types of the graph.
140
  ///
141
  ///For example, if the given graph has an Euler tour (i.e it has only one 
142
  ///non-trivial component and the degree of each node is even),
143
  ///the following code will print the arc IDs according to an
144
  ///Euler tour of \c g.
145
  ///\code
146
  ///  for(EulerIt<ListGraph> e(g); e!=INVALID; ++e) {
147
  ///    std::cout << g.id(Edge(e)) << std::eol;
148
  ///  }
149
  ///\endcode
150
  ///Although this iterator is for undirected graphs, it still returns 
151
  ///arcs in order to indicate the direction of the tour.
152
  ///(But arcs convert to edges, of course.)
153
  ///
154
  ///If \c g has no Euler tour, then the resulted walk will not be closed
155
  ///or not contain all edges.
156
  template<typename GR>
157
  class EulerIt
158
  {
159
    typedef typename GR::Node Node;
160
    typedef typename GR::NodeIt NodeIt;
161
    typedef typename GR::Arc Arc;
162
    typedef typename GR::Edge Edge;
163
    typedef typename GR::ArcIt ArcIt;
164
    typedef typename GR::OutArcIt OutArcIt;
165
    typedef typename GR::InArcIt InArcIt;
166

	
167
    const GR &g;
168
    typename GR::template NodeMap<OutArcIt> narc;
169
    typename GR::template EdgeMap<bool> visited;
170
    std::list<Arc> euler;
171

	
172
  public:
173

	
174
    ///Constructor
175

	
176
    ///Constructor.
177
    ///\param gr A graph.
178
    ///\param start The starting point of the tour. If it is not given,
179
    ///the tour will start from the first node that has an incident edge.
180
    EulerIt(const GR &gr, typename GR::Node start = INVALID)
181
      : g(gr), narc(g), visited(g, false)
182
    {
183
      if (start==INVALID) {
184
        NodeIt n(g);
185
        while (n!=INVALID && OutArcIt(g,n)==INVALID) ++n;
186
        start=n;
187
      }
188
      if (start!=INVALID) {
189
        for (NodeIt n(g); n!=INVALID; ++n) narc[n]=OutArcIt(g,n);
190
        while(narc[start]!=INVALID) {
191
          euler.push_back(narc[start]);
192
          visited[narc[start]]=true;
193
          Node next=g.target(narc[start]);
194
          ++narc[start];
195
          start=next;
196
          while(narc[start]!=INVALID && visited[narc[start]]) ++narc[start];
197
        }
198
      }
199
    }
200

	
201
    ///Arc conversion
202
    operator Arc() const { return euler.empty()?INVALID:euler.front(); }
203
    ///Edge conversion
204
    operator Edge() const { return euler.empty()?INVALID:euler.front(); }
205
    ///Compare with \c INVALID
206
    bool operator==(Invalid) const { return euler.empty(); }
207
    ///Compare with \c INVALID
208
    bool operator!=(Invalid) const { return !euler.empty(); }
209

	
210
    ///Next arc of the tour
211

	
212
    ///Next arc of the tour
213
    ///
214
    EulerIt &operator++() {
215
      Node s=g.target(euler.front());
216
      euler.pop_front();
217
      typename std::list<Arc>::iterator next=euler.begin();
218
      while(narc[s]!=INVALID) {
219
        while(narc[s]!=INVALID && visited[narc[s]]) ++narc[s];
220
        if(narc[s]==INVALID) break;
221
        else {
222
          euler.insert(next,narc[s]);
223
          visited[narc[s]]=true;
224
          Node n=g.target(narc[s]);
225
          ++narc[s];
226
          s=n;
227
        }
228
      }
229
      return *this;
230
    }
231

	
232
    ///Postfix incrementation
233

	
234
    /// Postfix incrementation.
235
    ///
236
    ///\warning This incrementation returns an \c Arc (which converts to 
237
    ///an \c Edge), not an \ref EulerIt, as one may expect.
238
    Arc operator++(int)
239
    {
240
      Arc e=*this;
241
      ++(*this);
242
      return e;
243
    }
244
  };
245

	
246

	
247
  ///Check if the given graph is Eulerian
248

	
249
  /// \ingroup graph_properties
250
  ///This function checks if the given graph is Eulerian.
251
  ///It works for both directed and undirected graphs.
252
  ///
253
  ///By definition, a digraph is called \e Eulerian if
254
  ///and only if it is connected and the number of incoming and outgoing
255
  ///arcs are the same for each node.
256
  ///Similarly, an undirected graph is called \e Eulerian if
257
  ///and only if it is connected and the number of incident edges is even
258
  ///for each node.
259
  ///
260
  ///\note There are (di)graphs that are not Eulerian, but still have an
261
  /// Euler tour, since they may contain isolated nodes.
262
  ///
263
  ///\sa DiEulerIt, EulerIt
264
  template<typename GR>
265
#ifdef DOXYGEN
266
  bool
267
#else
268
  typename enable_if<UndirectedTagIndicator<GR>,bool>::type
269
  eulerian(const GR &g)
270
  {
271
    for(typename GR::NodeIt n(g);n!=INVALID;++n)
272
      if(countIncEdges(g,n)%2) return false;
273
    return connected(g);
274
  }
275
  template<class GR>
276
  typename disable_if<UndirectedTagIndicator<GR>,bool>::type
277
#endif
278
  eulerian(const GR &g)
279
  {
280
    for(typename GR::NodeIt n(g);n!=INVALID;++n)
281
      if(countInArcs(g,n)!=countOutArcs(g,n)) return false;
282
    return connected(undirector(g));
283
  }
284

	
285
}
286

	
287
#endif
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_FIB_HEAP_H
20
#define LEMON_FIB_HEAP_H
21

	
22
///\file
23
///\ingroup heaps
24
///\brief Fibonacci heap implementation.
25

	
26
#include <vector>
27
#include <utility>
28
#include <functional>
29
#include <lemon/math.h>
30

	
31
namespace lemon {
32

	
33
  /// \ingroup heaps
34
  ///
35
  /// \brief Fibonacci heap data structure.
36
  ///
37
  /// This class implements the \e Fibonacci \e heap data structure.
38
  /// It fully conforms to the \ref concepts::Heap "heap concept".
39
  ///
40
  /// The methods \ref increase() and \ref erase() are not efficient in a
41
  /// Fibonacci heap. In case of many calls of these operations, it is
42
  /// better to use other heap structure, e.g. \ref BinHeap "binary heap".
43
  ///
44
  /// \tparam PR Type of the priorities of the items.
45
  /// \tparam IM A read-writable item map with \c int values, used
46
  /// internally to handle the cross references.
47
  /// \tparam CMP A functor class for comparing the priorities.
48
  /// The default is \c std::less<PR>.
49
#ifdef DOXYGEN
50
  template <typename PR, typename IM, typename CMP>
51
#else
52
  template <typename PR, typename IM, typename CMP = std::less<PR> >
53
#endif
54
  class FibHeap {
55
  public:
56

	
57
    /// Type of the item-int map.
58
    typedef IM ItemIntMap;
59
    /// Type of the priorities.
60
    typedef PR Prio;
61
    /// Type of the items stored in the heap.
62
    typedef typename ItemIntMap::Key Item;
63
    /// Type of the item-priority pairs.
64
    typedef std::pair<Item,Prio> Pair;
65
    /// Functor type for comparing the priorities.
66
    typedef CMP Compare;
67

	
68
  private:
69
    class Store;
70

	
71
    std::vector<Store> _data;
72
    int _minimum;
73
    ItemIntMap &_iim;
74
    Compare _comp;
75
    int _num;
76

	
77
  public:
78

	
79
    /// \brief Type to represent the states of the items.
80
    ///
81
    /// Each item has a state associated to it. It can be "in heap",
82
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
83
    /// heap's point of view, but may be useful to the user.
84
    ///
85
    /// The item-int map must be initialized in such way that it assigns
86
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
87
    enum State {
88
      IN_HEAP = 0,    ///< = 0.
89
      PRE_HEAP = -1,  ///< = -1.
90
      POST_HEAP = -2  ///< = -2.
91
    };
92

	
93
    /// \brief Constructor.
94
    ///
95
    /// Constructor.
96
    /// \param map A map that assigns \c int values to the items.
97
    /// It is used internally to handle the cross references.
98
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
99
    explicit FibHeap(ItemIntMap &map)
100
      : _minimum(0), _iim(map), _num() {}
101

	
102
    /// \brief Constructor.
103
    ///
104
    /// Constructor.
105
    /// \param map A map that assigns \c int values to the items.
106
    /// It is used internally to handle the cross references.
107
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
108
    /// \param comp The function object used for comparing the priorities.
109
    FibHeap(ItemIntMap &map, const Compare &comp)
110
      : _minimum(0), _iim(map), _comp(comp), _num() {}
111

	
112
    /// \brief The number of items stored in the heap.
113
    ///
114
    /// This function returns the number of items stored in the heap.
115
    int size() const { return _num; }
116

	
117
    /// \brief Check if the heap is empty.
118
    ///
119
    /// This function returns \c true if the heap is empty.
120
    bool empty() const { return _num==0; }
121

	
122
    /// \brief Make the heap empty.
123
    ///
124
    /// This functon makes the heap empty.
125
    /// It does not change the cross reference map. If you want to reuse
126
    /// a heap that is not surely empty, you should first clear it and
127
    /// then you should set the cross reference map to \c PRE_HEAP
128
    /// for each item.
129
    void clear() {
130
      _data.clear(); _minimum = 0; _num = 0;
131
    }
132

	
133
    /// \brief Insert an item into the heap with the given priority.
134
    ///
135
    /// This function inserts the given item into the heap with the
136
    /// given priority.
137
    /// \param item The item to insert.
138
    /// \param prio The priority of the item.
139
    /// \pre \e item must not be stored in the heap.
140
    void push (const Item& item, const Prio& prio) {
141
      int i=_iim[item];
142
      if ( i < 0 ) {
143
        int s=_data.size();
144
        _iim.set( item, s );
145
        Store st;
146
        st.name=item;
147
        _data.push_back(st);
148
        i=s;
149
      } else {
150
        _data[i].parent=_data[i].child=-1;
151
        _data[i].degree=0;
152
        _data[i].in=true;
153
        _data[i].marked=false;
154
      }
155

	
156
      if ( _num ) {
157
        _data[_data[_minimum].right_neighbor].left_neighbor=i;
158
        _data[i].right_neighbor=_data[_minimum].right_neighbor;
159
        _data[_minimum].right_neighbor=i;
160
        _data[i].left_neighbor=_minimum;
161
        if ( _comp( prio, _data[_minimum].prio) ) _minimum=i;
162
      } else {
163
        _data[i].right_neighbor=_data[i].left_neighbor=i;
164
        _minimum=i;
165
      }
166
      _data[i].prio=prio;
167
      ++_num;
168
    }
169

	
170
    /// \brief Return the item having minimum priority.
171
    ///
172
    /// This function returns the item having minimum priority.
173
    /// \pre The heap must be non-empty.
174
    Item top() const { return _data[_minimum].name; }
175

	
176
    /// \brief The minimum priority.
177
    ///
178
    /// This function returns the minimum priority.
179
    /// \pre The heap must be non-empty.
180
    Prio prio() const { return _data[_minimum].prio; }
181

	
182
    /// \brief Remove the item having minimum priority.
183
    ///
184
    /// This function removes the item having minimum priority.
185
    /// \pre The heap must be non-empty.
186
    void pop() {
187
      /*The first case is that there are only one root.*/
188
      if ( _data[_minimum].left_neighbor==_minimum ) {
189
        _data[_minimum].in=false;
190
        if ( _data[_minimum].degree!=0 ) {
191
          makeRoot(_data[_minimum].child);
192
          _minimum=_data[_minimum].child;
193
          balance();
194
        }
195
      } else {
196
        int right=_data[_minimum].right_neighbor;
197
        unlace(_minimum);
198
        _data[_minimum].in=false;
199
        if ( _data[_minimum].degree > 0 ) {
200
          int left=_data[_minimum].left_neighbor;
201
          int child=_data[_minimum].child;
202
          int last_child=_data[child].left_neighbor;
203

	
204
          makeRoot(child);
205

	
206
          _data[left].right_neighbor=child;
207
          _data[child].left_neighbor=left;
208
          _data[right].left_neighbor=last_child;
209
          _data[last_child].right_neighbor=right;
210
        }
211
        _minimum=right;
212
        balance();
213
      } // the case where there are more roots
214
      --_num;
215
    }
216

	
217
    /// \brief Remove the given item from the heap.
218
    ///
219
    /// This function removes the given item from the heap if it is
220
    /// already stored.
221
    /// \param item The item to delete.
222
    /// \pre \e item must be in the heap.
223
    void erase (const Item& item) {
224
      int i=_iim[item];
225

	
226
      if ( i >= 0 && _data[i].in ) {
227
        if ( _data[i].parent!=-1 ) {
228
          int p=_data[i].parent;
229
          cut(i,p);
230
          cascade(p);
231
        }
232
        _minimum=i;     //As if its prio would be -infinity
233
        pop();
234
      }
235
    }
236

	
237
    /// \brief The priority of the given item.
238
    ///
239
    /// This function returns the priority of the given item.
240
    /// \param item The item.
241
    /// \pre \e item must be in the heap.
242
    Prio operator[](const Item& item) const {
243
      return _data[_iim[item]].prio;
244
    }
245

	
246
    /// \brief Set the priority of an item or insert it, if it is
247
    /// not stored in the heap.
248
    ///
249
    /// This method sets the priority of the given item if it is
250
    /// already stored in the heap. Otherwise it inserts the given
251
    /// item into the heap with the given priority.
252
    /// \param item The item.
253
    /// \param prio The priority.
254
    void set (const Item& item, const Prio& prio) {
255
      int i=_iim[item];
256
      if ( i >= 0 && _data[i].in ) {
257
        if ( _comp(prio, _data[i].prio) ) decrease(item, prio);
258
        if ( _comp(_data[i].prio, prio) ) increase(item, prio);
259
      } else push(item, prio);
260
    }
261

	
262
    /// \brief Decrease the priority of an item to the given value.
263
    ///
264
    /// This function decreases the priority of an item to the given value.
265
    /// \param item The item.
266
    /// \param prio The priority.
267
    /// \pre \e item must be stored in the heap with priority at least \e prio.
268
    void decrease (const Item& item, const Prio& prio) {
269
      int i=_iim[item];
270
      _data[i].prio=prio;
271
      int p=_data[i].parent;
272

	
273
      if ( p!=-1 && _comp(prio, _data[p].prio) ) {
274
        cut(i,p);
275
        cascade(p);
276
      }
277
      if ( _comp(prio, _data[_minimum].prio) ) _minimum=i;
278
    }
279

	
280
    /// \brief Increase the priority of an item to the given value.
281
    ///
282
    /// This function increases the priority of an item to the given value.
283
    /// \param item The item.
284
    /// \param prio The priority.
285
    /// \pre \e item must be stored in the heap with priority at most \e prio.
286
    void increase (const Item& item, const Prio& prio) {
287
      erase(item);
288
      push(item, prio);
289
    }
290

	
291
    /// \brief Return the state of an item.
292
    ///
293
    /// This method returns \c PRE_HEAP if the given item has never
294
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
295
    /// and \c POST_HEAP otherwise.
296
    /// In the latter case it is possible that the item will get back
297
    /// to the heap again.
298
    /// \param item The item.
299
    State state(const Item &item) const {
300
      int i=_iim[item];
301
      if( i>=0 ) {
302
        if ( _data[i].in ) i=0;
303
        else i=-2;
304
      }
305
      return State(i);
306
    }
307

	
308
    /// \brief Set the state of an item in the heap.
309
    ///
310
    /// This function sets the state of the given item in the heap.
311
    /// It can be used to manually clear the heap when it is important
312
    /// to achive better time complexity.
313
    /// \param i The item.
314
    /// \param st The state. It should not be \c IN_HEAP.
315
    void state(const Item& i, State st) {
316
      switch (st) {
317
      case POST_HEAP:
318
      case PRE_HEAP:
319
        if (state(i) == IN_HEAP) {
320
          erase(i);
321
        }
322
        _iim[i] = st;
323
        break;
324
      case IN_HEAP:
325
        break;
326
      }
327
    }
328

	
329
  private:
330

	
331
    void balance() {
332

	
333
      int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1;
334

	
335
      std::vector<int> A(maxdeg,-1);
336

	
337
      /*
338
       *Recall that now minimum does not point to the minimum prio element.
339
       *We set minimum to this during balance().
340
       */
341
      int anchor=_data[_minimum].left_neighbor;
342
      int next=_minimum;
343
      bool end=false;
344

	
345
      do {
346
        int active=next;
347
        if ( anchor==active ) end=true;
348
        int d=_data[active].degree;
349
        next=_data[active].right_neighbor;
350

	
351
        while (A[d]!=-1) {
352
          if( _comp(_data[active].prio, _data[A[d]].prio) ) {
353
            fuse(active,A[d]);
354
          } else {
355
            fuse(A[d],active);
356
            active=A[d];
357
          }
358
          A[d]=-1;
359
          ++d;
360
        }
361
        A[d]=active;
362
      } while ( !end );
363

	
364

	
365
      while ( _data[_minimum].parent >=0 )
366
        _minimum=_data[_minimum].parent;
367
      int s=_minimum;
368
      int m=_minimum;
369
      do {
370
        if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s;
371
        s=_data[s].right_neighbor;
372
      } while ( s != m );
373
    }
374

	
375
    void makeRoot(int c) {
376
      int s=c;
377
      do {
378
        _data[s].parent=-1;
379
        s=_data[s].right_neighbor;
380
      } while ( s != c );
381
    }
382

	
383
    void cut(int a, int b) {
384
      /*
385
       *Replacing a from the children of b.
386
       */
387
      --_data[b].degree;
388

	
389
      if ( _data[b].degree !=0 ) {
390
        int child=_data[b].child;
391
        if ( child==a )
392
          _data[b].child=_data[child].right_neighbor;
393
        unlace(a);
394
      }
395

	
396

	
397
      /*Lacing a to the roots.*/
398
      int right=_data[_minimum].right_neighbor;
399
      _data[_minimum].right_neighbor=a;
400
      _data[a].left_neighbor=_minimum;
401
      _data[a].right_neighbor=right;
402
      _data[right].left_neighbor=a;
403

	
404
      _data[a].parent=-1;
405
      _data[a].marked=false;
406
    }
407

	
408
    void cascade(int a) {
409
      if ( _data[a].parent!=-1 ) {
410
        int p=_data[a].parent;
411

	
412
        if ( _data[a].marked==false ) _data[a].marked=true;
413
        else {
414
          cut(a,p);
415
          cascade(p);
416
        }
417
      }
418
    }
419

	
420
    void fuse(int a, int b) {
421
      unlace(b);
422

	
423
      /*Lacing b under a.*/
424
      _data[b].parent=a;
425

	
426
      if (_data[a].degree==0) {
427
        _data[b].left_neighbor=b;
428
        _data[b].right_neighbor=b;
429
        _data[a].child=b;
430
      } else {
431
        int child=_data[a].child;
432
        int last_child=_data[child].left_neighbor;
433
        _data[child].left_neighbor=b;
434
        _data[b].right_neighbor=child;
435
        _data[last_child].right_neighbor=b;
436
        _data[b].left_neighbor=last_child;
437
      }
438

	
439
      ++_data[a].degree;
440

	
441
      _data[b].marked=false;
442
    }
443

	
444
    /*
445
     *It is invoked only if a has siblings.
446
     */
447
    void unlace(int a) {
448
      int leftn=_data[a].left_neighbor;
449
      int rightn=_data[a].right_neighbor;
450
      _data[leftn].right_neighbor=rightn;
451
      _data[rightn].left_neighbor=leftn;
452
    }
453

	
454

	
455
    class Store {
456
      friend class FibHeap;
457

	
458
      Item name;
459
      int parent;
460
      int left_neighbor;
461
      int right_neighbor;
462
      int child;
463
      int degree;
464
      bool marked;
465
      bool in;
466
      Prio prio;
467

	
468
      Store() : parent(-1), child(-1), degree(), marked(false), in(true) {}
469
    };
470
  };
471

	
472
} //namespace lemon
473

	
474
#endif //LEMON_FIB_HEAP_H
475

	
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_FOURARY_HEAP_H
20
#define LEMON_FOURARY_HEAP_H
21

	
22
///\ingroup heaps
23
///\file
24
///\brief Fourary heap implementation.
25

	
26
#include <vector>
27
#include <utility>
28
#include <functional>
29

	
30
namespace lemon {
31

	
32
  /// \ingroup heaps
33
  ///
34
  ///\brief Fourary heap data structure.
35
  ///
36
  /// This class implements the \e fourary \e heap data structure.
37
  /// It fully conforms to the \ref concepts::Heap "heap concept".
38
  ///
39
  /// The fourary heap is a specialization of the \ref KaryHeap "K-ary heap"
40
  /// for <tt>K=4</tt>. It is similar to the \ref BinHeap "binary heap",
41
  /// but its nodes have at most four children, instead of two.
42
  ///
43
  /// \tparam PR Type of the priorities of the items.
44
  /// \tparam IM A read-writable item map with \c int values, used
45
  /// internally to handle the cross references.
46
  /// \tparam CMP A functor class for comparing the priorities.
47
  /// The default is \c std::less<PR>.
48
  ///
49
  ///\sa BinHeap
50
  ///\sa KaryHeap
51
#ifdef DOXYGEN
52
  template <typename PR, typename IM, typename CMP>
53
#else
54
  template <typename PR, typename IM, typename CMP = std::less<PR> >
55
#endif
56
  class FouraryHeap {
57
  public:
58
    /// Type of the item-int map.
59
    typedef IM ItemIntMap;
60
    /// Type of the priorities.
61
    typedef PR Prio;
62
    /// Type of the items stored in the heap.
63
    typedef typename ItemIntMap::Key Item;
64
    /// Type of the item-priority pairs.
65
    typedef std::pair<Item,Prio> Pair;
66
    /// Functor type for comparing the priorities.
67
    typedef CMP Compare;
68

	
69
    /// \brief Type to represent the states of the items.
70
    ///
71
    /// Each item has a state associated to it. It can be "in heap",
72
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
73
    /// heap's point of view, but may be useful to the user.
74
    ///
75
    /// The item-int map must be initialized in such way that it assigns
76
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
77
    enum State {
78
      IN_HEAP = 0,    ///< = 0.
79
      PRE_HEAP = -1,  ///< = -1.
80
      POST_HEAP = -2  ///< = -2.
81
    };
82

	
83
  private:
84
    std::vector<Pair> _data;
85
    Compare _comp;
86
    ItemIntMap &_iim;
87

	
88
  public:
89
    /// \brief Constructor.
90
    ///
91
    /// Constructor.
92
    /// \param map A map that assigns \c int values to the items.
93
    /// It is used internally to handle the cross references.
94
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
95
    explicit FouraryHeap(ItemIntMap &map) : _iim(map) {}
96

	
97
    /// \brief Constructor.
98
    ///
99
    /// Constructor.
100
    /// \param map A map that assigns \c int values to the items.
101
    /// It is used internally to handle the cross references.
102
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
103
    /// \param comp The function object used for comparing the priorities.
104
    FouraryHeap(ItemIntMap &map, const Compare &comp)
105
      : _iim(map), _comp(comp) {}
106

	
107
    /// \brief The number of items stored in the heap.
108
    ///
109
    /// This function returns the number of items stored in the heap.
110
    int size() const { return _data.size(); }
111

	
112
    /// \brief Check if the heap is empty.
113
    ///
114
    /// This function returns \c true if the heap is empty.
115
    bool empty() const { return _data.empty(); }
116

	
117
    /// \brief Make the heap empty.
118
    ///
119
    /// This functon makes the heap empty.
120
    /// It does not change the cross reference map. If you want to reuse
121
    /// a heap that is not surely empty, you should first clear it and
122
    /// then you should set the cross reference map to \c PRE_HEAP
123
    /// for each item.
124
    void clear() { _data.clear(); }
125

	
126
  private:
127
    static int parent(int i) { return (i-1)/4; }
128
    static int firstChild(int i) { return 4*i+1; }
129

	
130
    bool less(const Pair &p1, const Pair &p2) const {
131
      return _comp(p1.second, p2.second);
132
    }
133

	
134
    void bubbleUp(int hole, Pair p) {
135
      int par = parent(hole);
136
      while( hole>0 && less(p,_data[par]) ) {
137
        move(_data[par],hole);
138
        hole = par;
139
        par = parent(hole);
140
      }
141
      move(p, hole);
142
    }
143

	
144
    void bubbleDown(int hole, Pair p, int length) {
145
      if( length>1 ) {
146
        int child = firstChild(hole);
147
        while( child+3<length ) {
148
          int min=child;
149
          if( less(_data[++child], _data[min]) ) min=child;
150
          if( less(_data[++child], _data[min]) ) min=child;
151
          if( less(_data[++child], _data[min]) ) min=child;
152
          if( !less(_data[min], p) )
153
            goto ok;
154
          move(_data[min], hole);
155
          hole = min;
156
          child = firstChild(hole);
157
        }
158
        if ( child<length ) {
159
          int min = child;
160
          if( ++child<length && less(_data[child], _data[min]) ) min=child;
161
          if( ++child<length && less(_data[child], _data[min]) ) min=child;
162
          if( less(_data[min], p) ) {
163
            move(_data[min], hole);
164
            hole = min;
165
          }
166
        }
167
      }
168
    ok:
169
      move(p, hole);
170
    }
171

	
172
    void move(const Pair &p, int i) {
173
      _data[i] = p;
174
      _iim.set(p.first, i);
175
    }
176

	
177
  public:
178
    /// \brief Insert a pair of item and priority into the heap.
179
    ///
180
    /// This function inserts \c p.first to the heap with priority
181
    /// \c p.second.
182
    /// \param p The pair to insert.
183
    /// \pre \c p.first must not be stored in the heap.
184
    void push(const Pair &p) {
185
      int n = _data.size();
186
      _data.resize(n+1);
187
      bubbleUp(n, p);
188
    }
189

	
190
    /// \brief Insert an item into the heap with the given priority.
191
    ///
192
    /// This function inserts the given item into the heap with the
193
    /// given priority.
194
    /// \param i The item to insert.
195
    /// \param p The priority of the item.
196
    /// \pre \e i must not be stored in the heap.
197
    void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
198

	
199
    /// \brief Return the item having minimum priority.
200
    ///
201
    /// This function returns the item having minimum priority.
202
    /// \pre The heap must be non-empty.
203
    Item top() const { return _data[0].first; }
204

	
205
    /// \brief The minimum priority.
206
    ///
207
    /// This function returns the minimum priority.
208
    /// \pre The heap must be non-empty.
209
    Prio prio() const { return _data[0].second; }
210

	
211
    /// \brief Remove the item having minimum priority.
212
    ///
213
    /// This function removes the item having minimum priority.
214
    /// \pre The heap must be non-empty.
215
    void pop() {
216
      int n = _data.size()-1;
217
      _iim.set(_data[0].first, POST_HEAP);
218
      if (n>0) bubbleDown(0, _data[n], n);
219
      _data.pop_back();
220
    }
221

	
222
    /// \brief Remove the given item from the heap.
223
    ///
224
    /// This function removes the given item from the heap if it is
225
    /// already stored.
226
    /// \param i The item to delete.
227
    /// \pre \e i must be in the heap.
228
    void erase(const Item &i) {
229
      int h = _iim[i];
230
      int n = _data.size()-1;
231
      _iim.set(_data[h].first, POST_HEAP);
232
      if( h<n ) {
233
        if( less(_data[parent(h)], _data[n]) )
234
          bubbleDown(h, _data[n], n);
235
        else
236
          bubbleUp(h, _data[n]);
237
      }
238
      _data.pop_back();
239
    }
240

	
241
    /// \brief The priority of the given item.
242
    ///
243
    /// This function returns the priority of the given item.
244
    /// \param i The item.
245
    /// \pre \e i must be in the heap.
246
    Prio operator[](const Item &i) const {
247
      int idx = _iim[i];
248
      return _data[idx].second;
249
    }
250

	
251
    /// \brief Set the priority of an item or insert it, if it is
252
    /// not stored in the heap.
253
    ///
254
    /// This method sets the priority of the given item if it is
255
    /// already stored in the heap. Otherwise it inserts the given
256
    /// item into the heap with the given priority.
257
    /// \param i The item.
258
    /// \param p The priority.
259
    void set(const Item &i, const Prio &p) {
260
      int idx = _iim[i];
261
      if( idx < 0 )
262
        push(i,p);
263
      else if( _comp(p, _data[idx].second) )
264
        bubbleUp(idx, Pair(i,p));
265
      else
266
        bubbleDown(idx, Pair(i,p), _data.size());
267
    }
268

	
269
    /// \brief Decrease the priority of an item to the given value.
270
    ///
271
    /// This function decreases the priority of an item to the given value.
272
    /// \param i The item.
273
    /// \param p The priority.
274
    /// \pre \e i must be stored in the heap with priority at least \e p.
275
    void decrease(const Item &i, const Prio &p) {
276
      int idx = _iim[i];
277
      bubbleUp(idx, Pair(i,p));
278
    }
279

	
280
    /// \brief Increase the priority of an item to the given value.
281
    ///
282
    /// This function increases the priority of an item to the given value.
283
    /// \param i The item.
284
    /// \param p The priority.
285
    /// \pre \e i must be stored in the heap with priority at most \e p.
286
    void increase(const Item &i, const Prio &p) {
287
      int idx = _iim[i];
288
      bubbleDown(idx, Pair(i,p), _data.size());
289
    }
290

	
291
    /// \brief Return the state of an item.
292
    ///
293
    /// This method returns \c PRE_HEAP if the given item has never
294
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
295
    /// and \c POST_HEAP otherwise.
296
    /// In the latter case it is possible that the item will get back
297
    /// to the heap again.
298
    /// \param i The item.
299
    State state(const Item &i) const {
300
      int s = _iim[i];
301
      if (s>=0) s=0;
302
      return State(s);
303
    }
304

	
305
    /// \brief Set the state of an item in the heap.
306
    ///
307
    /// This function sets the state of the given item in the heap.
308
    /// It can be used to manually clear the heap when it is important
309
    /// to achive better time complexity.
310
    /// \param i The item.
311
    /// \param st The state. It should not be \c IN_HEAP.
312
    void state(const Item& i, State st) {
313
      switch (st) {
314
        case POST_HEAP:
315
        case PRE_HEAP:
316
          if (state(i) == IN_HEAP) erase(i);
317
          _iim[i] = st;
318
          break;
319
        case IN_HEAP:
320
          break;
321
      }
322
    }
323

	
324
    /// \brief Replace an item in the heap.
325
    ///
326
    /// This function replaces item \c i with item \c j.
327
    /// Item \c i must be in the heap, while \c j must be out of the heap.
328
    /// After calling this method, item \c i will be out of the
329
    /// heap and \c j will be in the heap with the same prioriority
330
    /// as item \c i had before.
331
    void replace(const Item& i, const Item& j) {
332
      int idx = _iim[i];
333
      _iim.set(i, _iim[j]);
334
      _iim.set(j, idx);
335
      _data[idx].first = j;
336
    }
337

	
338
  }; // class FouraryHeap
339

	
340
} // namespace lemon
341

	
342
#endif // LEMON_FOURARY_HEAP_H
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_GOMORY_HU_TREE_H
20
#define LEMON_GOMORY_HU_TREE_H
21

	
22
#include <limits>
23

	
24
#include <lemon/core.h>
25
#include <lemon/preflow.h>
26
#include <lemon/concept_check.h>
27
#include <lemon/concepts/maps.h>
28

	
29
/// \ingroup min_cut
30
/// \file 
31
/// \brief Gomory-Hu cut tree in graphs.
32

	
33
namespace lemon {
34

	
35
  /// \ingroup min_cut
36
  ///
37
  /// \brief Gomory-Hu cut tree algorithm
38
  ///
39
  /// The Gomory-Hu tree is a tree on the node set of a given graph, but it
40
  /// may contain edges which are not in the original graph. It has the
41
  /// property that the minimum capacity edge of the path between two nodes 
42
  /// in this tree has the same weight as the minimum cut in the graph
43
  /// between these nodes. Moreover the components obtained by removing
44
  /// this edge from the tree determine the corresponding minimum cut.
45
  /// Therefore once this tree is computed, the minimum cut between any pair
46
  /// of nodes can easily be obtained.
47
  /// 
48
  /// The algorithm calculates \e n-1 distinct minimum cuts (currently with
49
  /// the \ref Preflow algorithm), thus it has \f$O(n^3\sqrt{e})\f$ overall
50
  /// time complexity. It calculates a rooted Gomory-Hu tree.
51
  /// The structure of the tree and the edge weights can be
52
  /// obtained using \c predNode(), \c predValue() and \c rootDist().
53
  /// The functions \c minCutMap() and \c minCutValue() calculate
54
  /// the minimum cut and the minimum cut value between any two nodes
55
  /// in the graph. You can also list (iterate on) the nodes and the
56
  /// edges of the cuts using \c MinCutNodeIt and \c MinCutEdgeIt.
57
  ///
58
  /// \tparam GR The type of the undirected graph the algorithm runs on.
59
  /// \tparam CAP The type of the edge map containing the capacities.
60
  /// The default map type is \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
61
#ifdef DOXYGEN
62
  template <typename GR,
63
	    typename CAP>
64
#else
65
  template <typename GR,
66
	    typename CAP = typename GR::template EdgeMap<int> >
67
#endif
68
  class GomoryHu {
69
  public:
70

	
71
    /// The graph type of the algorithm
72
    typedef GR Graph;
73
    /// The capacity map type of the algorithm
74
    typedef CAP Capacity;
75
    /// The value type of capacities
76
    typedef typename Capacity::Value Value;
77
    
78
  private:
79

	
80
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
81

	
82
    const Graph& _graph;
83
    const Capacity& _capacity;
84

	
85
    Node _root;
86
    typename Graph::template NodeMap<Node>* _pred;
87
    typename Graph::template NodeMap<Value>* _weight;
88
    typename Graph::template NodeMap<int>* _order;
89

	
90
    void createStructures() {
91
      if (!_pred) {
92
	_pred = new typename Graph::template NodeMap<Node>(_graph);
93
      }
94
      if (!_weight) {
95
	_weight = new typename Graph::template NodeMap<Value>(_graph);
96
      }
97
      if (!_order) {
98
	_order = new typename Graph::template NodeMap<int>(_graph);
99
      }
100
    }
101

	
102
    void destroyStructures() {
103
      if (_pred) {
104
	delete _pred;
105
      }
106
      if (_weight) {
107
	delete _weight;
108
      }
109
      if (_order) {
110
	delete _order;
111
      }
112
    }
113
  
114
  public:
115

	
116
    /// \brief Constructor
117
    ///
118
    /// Constructor.
119
    /// \param graph The undirected graph the algorithm runs on.
120
    /// \param capacity The edge capacity map.
121
    GomoryHu(const Graph& graph, const Capacity& capacity) 
122
      : _graph(graph), _capacity(capacity),
123
	_pred(0), _weight(0), _order(0) 
124
    {
125
      checkConcept<concepts::ReadMap<Edge, Value>, Capacity>();
126
    }
127

	
128

	
129
    /// \brief Destructor
130
    ///
131
    /// Destructor.
132
    ~GomoryHu() {
133
      destroyStructures();
134
    }
135

	
136
  private:
137
  
138
    // Initialize the internal data structures
139
    void init() {
140
      createStructures();
141

	
142
      _root = NodeIt(_graph);
143
      for (NodeIt n(_graph); n != INVALID; ++n) {
144
        (*_pred)[n] = _root;
145
        (*_order)[n] = -1;
146
      }
147
      (*_pred)[_root] = INVALID;
148
      (*_weight)[_root] = std::numeric_limits<Value>::max(); 
149
    }
150

	
151

	
152
    // Start the algorithm
153
    void start() {
154
      Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID);
155

	
156
      for (NodeIt n(_graph); n != INVALID; ++n) {
157
	if (n == _root) continue;
158

	
159
	Node pn = (*_pred)[n];
160
	fa.source(n);
161
	fa.target(pn);
162

	
163
	fa.runMinCut();
164

	
165
	(*_weight)[n] = fa.flowValue();
166

	
167
	for (NodeIt nn(_graph); nn != INVALID; ++nn) {
168
	  if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
169
	    (*_pred)[nn] = n;
170
	  }
171
	}
172
	if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
173
	  (*_pred)[n] = (*_pred)[pn];
174
	  (*_pred)[pn] = n;
175
	  (*_weight)[n] = (*_weight)[pn];
176
	  (*_weight)[pn] = fa.flowValue();
177
	}
178
      }
179

	
180
      (*_order)[_root] = 0;
181
      int index = 1;
182

	
183
      for (NodeIt n(_graph); n != INVALID; ++n) {
184
	std::vector<Node> st;
185
	Node nn = n;
186
	while ((*_order)[nn] == -1) {
187
	  st.push_back(nn);
188
	  nn = (*_pred)[nn];
189
	}
190
	while (!st.empty()) {
191
	  (*_order)[st.back()] = index++;
192
	  st.pop_back();
193
	}
194
      }
195
    }
196

	
197
  public:
198

	
199
    ///\name Execution Control
200
 
201
    ///@{
202

	
203
    /// \brief Run the Gomory-Hu algorithm.
204
    ///
205
    /// This function runs the Gomory-Hu algorithm.
206
    void run() {
207
      init();
208
      start();
209
    }
210
    
211
    /// @}
212

	
213
    ///\name Query Functions
214
    ///The results of the algorithm can be obtained using these
215
    ///functions.\n
216
    ///\ref run() should be called before using them.\n
217
    ///See also \ref MinCutNodeIt and \ref MinCutEdgeIt.
218

	
219
    ///@{
220

	
221
    /// \brief Return the predecessor node in the Gomory-Hu tree.
222
    ///
223
    /// This function returns the predecessor node of the given node
224
    /// in the Gomory-Hu tree.
225
    /// If \c node is the root of the tree, then it returns \c INVALID.
226
    ///
227
    /// \pre \ref run() must be called before using this function.
228
    Node predNode(const Node& node) const {
229
      return (*_pred)[node];
230
    }
231

	
232
    /// \brief Return the weight of the predecessor edge in the
233
    /// Gomory-Hu tree.
234
    ///
235
    /// This function returns the weight of the predecessor edge of the 
236
    /// given node in the Gomory-Hu tree.
237
    /// If \c node is the root of the tree, the result is undefined.
238
    ///
239
    /// \pre \ref run() must be called before using this function.
240
    Value predValue(const Node& node) const {
241
      return (*_weight)[node];
242
    }
243

	
244
    /// \brief Return the distance from the root node in the Gomory-Hu tree.
245
    ///
246
    /// This function returns the distance of the given node from the root
247
    /// node in the Gomory-Hu tree.
248
    ///
249
    /// \pre \ref run() must be called before using this function.
250
    int rootDist(const Node& node) const {
251
      return (*_order)[node];
252
    }
253

	
254
    /// \brief Return the minimum cut value between two nodes
255
    ///
256
    /// This function returns the minimum cut value between the nodes
257
    /// \c s and \c t. 
258
    /// It finds the nearest common ancestor of the given nodes in the
259
    /// Gomory-Hu tree and calculates the minimum weight edge on the
260
    /// paths to the ancestor.
261
    ///
262
    /// \pre \ref run() must be called before using this function.
263
    Value minCutValue(const Node& s, const Node& t) const {
264
      Node sn = s, tn = t;
265
      Value value = std::numeric_limits<Value>::max();
266
      
267
      while (sn != tn) {
268
	if ((*_order)[sn] < (*_order)[tn]) {
269
	  if ((*_weight)[tn] <= value) value = (*_weight)[tn];
270
	  tn = (*_pred)[tn];
271
	} else {
272
	  if ((*_weight)[sn] <= value) value = (*_weight)[sn];
273
	  sn = (*_pred)[sn];
274
	}
275
      }
276
      return value;
277
    }
278

	
279
    /// \brief Return the minimum cut between two nodes
280
    ///
281
    /// This function returns the minimum cut between the nodes \c s and \c t
282
    /// in the \c cutMap parameter by setting the nodes in the component of
283
    /// \c s to \c true and the other nodes to \c false.
284
    ///
285
    /// For higher level interfaces see MinCutNodeIt and MinCutEdgeIt.
286
    ///
287
    /// \param s The base node.
288
    /// \param t The node you want to separate from node \c s.
289
    /// \param cutMap The cut will be returned in this map.
290
    /// It must be a \c bool (or convertible) \ref concepts::ReadWriteMap
291
    /// "ReadWriteMap" on the graph nodes.
292
    ///
293
    /// \return The value of the minimum cut between \c s and \c t.
294
    ///
295
    /// \pre \ref run() must be called before using this function.
296
    template <typename CutMap>
297
    Value minCutMap(const Node& s, ///< 
298
                    const Node& t,
299
                    ///< 
300
                    CutMap& cutMap
301
                    ///< 
302
                    ) const {
303
      Node sn = s, tn = t;
304
      bool s_root=false;
305
      Node rn = INVALID;
306
      Value value = std::numeric_limits<Value>::max();
307
      
308
      while (sn != tn) {
309
	if ((*_order)[sn] < (*_order)[tn]) {
310
	  if ((*_weight)[tn] <= value) {
311
	    rn = tn;
312
            s_root = false;
313
	    value = (*_weight)[tn];
314
	  }
315
	  tn = (*_pred)[tn];
316
	} else {
317
	  if ((*_weight)[sn] <= value) {
318
	    rn = sn;
319
            s_root = true;
320
	    value = (*_weight)[sn];
321
	  }
322
	  sn = (*_pred)[sn];
323
	}
324
      }
325

	
326
      typename Graph::template NodeMap<bool> reached(_graph, false);
327
      reached[_root] = true;
328
      cutMap.set(_root, !s_root);
329
      reached[rn] = true;
330
      cutMap.set(rn, s_root);
331

	
332
      std::vector<Node> st;
333
      for (NodeIt n(_graph); n != INVALID; ++n) {
334
	st.clear();
335
        Node nn = n;
336
	while (!reached[nn]) {
337
	  st.push_back(nn);
338
	  nn = (*_pred)[nn];
339
	}
340
	while (!st.empty()) {
341
	  cutMap.set(st.back(), cutMap[nn]);
342
	  st.pop_back();
343
	}
344
      }
345
      
346
      return value;
347
    }
348

	
349
    ///@}
350

	
351
    friend class MinCutNodeIt;
352

	
353
    /// Iterate on the nodes of a minimum cut
354
    
355
    /// This iterator class lists the nodes of a minimum cut found by
356
    /// GomoryHu. Before using it, you must allocate a GomoryHu class
357
    /// and call its \ref GomoryHu::run() "run()" method.
358
    ///
359
    /// This example counts the nodes in the minimum cut separating \c s from
360
    /// \c t.
361
    /// \code
362
    /// GomoryHu<Graph> gom(g, capacities);
363
    /// gom.run();
364
    /// int cnt=0;
365
    /// for(GomoryHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt;
366
    /// \endcode
367
    class MinCutNodeIt
368
    {
369
      bool _side;
370
      typename Graph::NodeIt _node_it;
371
      typename Graph::template NodeMap<bool> _cut;
372
    public:
373
      /// Constructor
374

	
375
      /// Constructor.
376
      ///
377
      MinCutNodeIt(GomoryHu const &gomory,
378
                   ///< The GomoryHu class. You must call its
379
                   ///  run() method
380
                   ///  before initializing this iterator.
381
                   const Node& s, ///< The base node.
382
                   const Node& t,
383
                   ///< The node you want to separate from node \c s.
384
                   bool side=true
385
                   ///< If it is \c true (default) then the iterator lists
386
                   ///  the nodes of the component containing \c s,
387
                   ///  otherwise it lists the other component.
388
                   /// \note As the minimum cut is not always unique,
389
                   /// \code
390
                   /// MinCutNodeIt(gomory, s, t, true);
391
                   /// \endcode
392
                   /// and
393
                   /// \code
394
                   /// MinCutNodeIt(gomory, t, s, false);
395
                   /// \endcode
396
                   /// does not necessarily give the same set of nodes.
397
                   /// However it is ensured that
398
                   /// \code
399
                   /// MinCutNodeIt(gomory, s, t, true);
400
                   /// \endcode
401
                   /// and
402
                   /// \code
403
                   /// MinCutNodeIt(gomory, s, t, false);
404
                   /// \endcode
405
                   /// together list each node exactly once.
406
                   )
407
        : _side(side), _cut(gomory._graph)
408
      {
409
        gomory.minCutMap(s,t,_cut);
410
        for(_node_it=typename Graph::NodeIt(gomory._graph);
411
            _node_it!=INVALID && _cut[_node_it]!=_side;
412
            ++_node_it) {}
413
      }
414
      /// Conversion to \c Node
415

	
416
      /// Conversion to \c Node.
417
      ///
418
      operator typename Graph::Node() const
419
      {
420
        return _node_it;
421
      }
422
      bool operator==(Invalid) { return _node_it==INVALID; }
423
      bool operator!=(Invalid) { return _node_it!=INVALID; }
424
      /// Next node
425

	
426
      /// Next node.
427
      ///
428
      MinCutNodeIt &operator++()
429
      {
430
        for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {}
431
        return *this;
432
      }
433
      /// Postfix incrementation
434

	
435
      /// Postfix incrementation.
436
      ///
437
      /// \warning This incrementation
438
      /// returns a \c Node, not a \c MinCutNodeIt, as one may
439
      /// expect.
440
      typename Graph::Node operator++(int)
441
      {
442
        typename Graph::Node n=*this;
443
        ++(*this);
444
        return n;
445
      }
446
    };
447
    
448
    friend class MinCutEdgeIt;
449
    
450
    /// Iterate on the edges of a minimum cut
451
    
452
    /// This iterator class lists the edges of a minimum cut found by
453
    /// GomoryHu. Before using it, you must allocate a GomoryHu class
454
    /// and call its \ref GomoryHu::run() "run()" method.
455
    ///
456
    /// This example computes the value of the minimum cut separating \c s from
457
    /// \c t.
458
    /// \code
459
    /// GomoryHu<Graph> gom(g, capacities);
460
    /// gom.run();
461
    /// int value=0;
462
    /// for(GomoryHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e)
463
    ///   value+=capacities[e];
464
    /// \endcode
465
    /// The result will be the same as the value returned by
466
    /// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)".
467
    class MinCutEdgeIt
468
    {
469
      bool _side;
470
      const Graph &_graph;
471
      typename Graph::NodeIt _node_it;
472
      typename Graph::OutArcIt _arc_it;
473
      typename Graph::template NodeMap<bool> _cut;
474
      void step()
475
      {
476
        ++_arc_it;
477
        while(_node_it!=INVALID && _arc_it==INVALID)
478
          {
479
            for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {}
480
            if(_node_it!=INVALID)
481
              _arc_it=typename Graph::OutArcIt(_graph,_node_it);
482
          }
483
      }
484
      
485
    public:
486
      /// Constructor
487

	
488
      /// Constructor.
489
      ///
490
      MinCutEdgeIt(GomoryHu const &gomory,
491
                   ///< The GomoryHu class. You must call its
492
                   ///  run() method
493
                   ///  before initializing this iterator.
494
                   const Node& s,  ///< The base node.
495
                   const Node& t,
496
                   ///< The node you want to separate from node \c s.
497
                   bool side=true
498
                   ///< If it is \c true (default) then the listed arcs
499
                   ///  will be oriented from the
500
                   ///  nodes of the component containing \c s,
501
                   ///  otherwise they will be oriented in the opposite
502
                   ///  direction.
503
                   )
504
        : _graph(gomory._graph), _cut(_graph)
505
      {
506
        gomory.minCutMap(s,t,_cut);
507
        if(!side)
508
          for(typename Graph::NodeIt n(_graph);n!=INVALID;++n)
509
            _cut[n]=!_cut[n];
510

	
511
        for(_node_it=typename Graph::NodeIt(_graph);
512
            _node_it!=INVALID && !_cut[_node_it];
513
            ++_node_it) {}
514
        _arc_it = _node_it!=INVALID ?
515
          typename Graph::OutArcIt(_graph,_node_it) : INVALID;
516
        while(_node_it!=INVALID && _arc_it == INVALID)
517
          {
518
            for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {}
519
            if(_node_it!=INVALID)
520
              _arc_it= typename Graph::OutArcIt(_graph,_node_it);
521
          }
522
        while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
523
      }
524
      /// Conversion to \c Arc
525

	
526
      /// Conversion to \c Arc.
527
      ///
528
      operator typename Graph::Arc() const
529
      {
530
        return _arc_it;
531
      }
532
      /// Conversion to \c Edge
533

	
534
      /// Conversion to \c Edge.
535
      ///
536
      operator typename Graph::Edge() const
537
      {
538
        return _arc_it;
539
      }
540
      bool operator==(Invalid) { return _node_it==INVALID; }
541
      bool operator!=(Invalid) { return _node_it!=INVALID; }
542
      /// Next edge
543

	
544
      /// Next edge.
545
      ///
546
      MinCutEdgeIt &operator++()
547
      {
548
        step();
549
        while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
550
        return *this;
551
      }
552
      /// Postfix incrementation
553
      
554
      /// Postfix incrementation.
555
      ///
556
      /// \warning This incrementation
557
      /// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect.
558
      typename Graph::Arc operator++(int)
559
      {
560
        typename Graph::Arc e=*this;
561
        ++(*this);
562
        return e;
563
      }
564
    };
565

	
566
  };
567

	
568
}
569

	
570
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_HARTMANN_ORLIN_H
20
#define LEMON_HARTMANN_ORLIN_H
21

	
22
/// \ingroup min_mean_cycle
23
///
24
/// \file
25
/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle.
26

	
27
#include <vector>
28
#include <limits>
29
#include <lemon/core.h>
30
#include <lemon/path.h>
31
#include <lemon/tolerance.h>
32
#include <lemon/connectivity.h>
33

	
34
namespace lemon {
35

	
36
  /// \brief Default traits class of HartmannOrlin algorithm.
37
  ///
38
  /// Default traits class of HartmannOrlin algorithm.
39
  /// \tparam GR The type of the digraph.
40
  /// \tparam LEN The type of the length map.
41
  /// It must conform to the \ref concepts::Rea_data "Rea_data" concept.
42
#ifdef DOXYGEN
43
  template <typename GR, typename LEN>
44
#else
45
  template <typename GR, typename LEN,
46
    bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
47
#endif
48
  struct HartmannOrlinDefaultTraits
49
  {
50
    /// The type of the digraph
51
    typedef GR Digraph;
52
    /// The type of the length map
53
    typedef LEN LengthMap;
54
    /// The type of the arc lengths
55
    typedef typename LengthMap::Value Value;
56

	
57
    /// \brief The large value type used for internal computations
58
    ///
59
    /// The large value type used for internal computations.
60
    /// It is \c long \c long if the \c Value type is integer,
61
    /// otherwise it is \c double.
62
    /// \c Value must be convertible to \c LargeValue.
63
    typedef double LargeValue;
64

	
65
    /// The tolerance type used for internal computations
66
    typedef lemon::Tolerance<LargeValue> Tolerance;
67

	
68
    /// \brief The path type of the found cycles
69
    ///
70
    /// The path type of the found cycles.
71
    /// It must conform to the \ref lemon::concepts::Path "Path" concept
72
    /// and it must have an \c addFront() function.
73
    typedef lemon::Path<Digraph> Path;
74
  };
75

	
76
  // Default traits class for integer value types
77
  template <typename GR, typename LEN>
78
  struct HartmannOrlinDefaultTraits<GR, LEN, true>
79
  {
80
    typedef GR Digraph;
81
    typedef LEN LengthMap;
82
    typedef typename LengthMap::Value Value;
83
#ifdef LEMON_HAVE_LONG_LONG
84
    typedef long long LargeValue;
85
#else
86
    typedef long LargeValue;
87
#endif
88
    typedef lemon::Tolerance<LargeValue> Tolerance;
89
    typedef lemon::Path<Digraph> Path;
90
  };
91

	
92

	
93
  /// \addtogroup min_mean_cycle
94
  /// @{
95

	
96
  /// \brief Implementation of the Hartmann-Orlin algorithm for finding
97
  /// a minimum mean cycle.
98
  ///
99
  /// This class implements the Hartmann-Orlin algorithm for finding
100
  /// a directed cycle of minimum mean length (cost) in a digraph
101
  /// \ref amo93networkflows, \ref dasdan98minmeancycle.
102
  /// It is an improved version of \ref Karp "Karp"'s original algorithm,
103
  /// it applies an efficient early termination scheme.
104
  /// It runs in time O(ne) and uses space O(n<sup>2</sup>+e).
105
  ///
106
  /// \tparam GR The type of the digraph the algorithm runs on.
107
  /// \tparam LEN The type of the length map. The default
108
  /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
109
#ifdef DOXYGEN
110
  template <typename GR, typename LEN, typename TR>
111
#else
112
  template < typename GR,
113
             typename LEN = typename GR::template ArcMap<int>,
114
             typename TR = HartmannOrlinDefaultTraits<GR, LEN> >
115
#endif
116
  class HartmannOrlin
117
  {
118
  public:
119

	
120
    /// The type of the digraph
121
    typedef typename TR::Digraph Digraph;
122
    /// The type of the length map
123
    typedef typename TR::LengthMap LengthMap;
124
    /// The type of the arc lengths
125
    typedef typename TR::Value Value;
126

	
127
    /// \brief The large value type
128
    ///
129
    /// The large value type used for internal computations.
130
    /// Using the \ref HartmannOrlinDefaultTraits "default traits class",
131
    /// it is \c long \c long if the \c Value type is integer,
132
    /// otherwise it is \c double.
133
    typedef typename TR::LargeValue LargeValue;
134

	
135
    /// The tolerance type
136
    typedef typename TR::Tolerance Tolerance;
137

	
138
    /// \brief The path type of the found cycles
139
    ///
140
    /// The path type of the found cycles.
141
    /// Using the \ref HartmannOrlinDefaultTraits "default traits class",
142
    /// it is \ref lemon::Path "Path<Digraph>".
143
    typedef typename TR::Path Path;
144

	
145
    /// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm
146
    typedef TR Traits;
147

	
148
  private:
149

	
150
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
151

	
152
    // Data sturcture for path data
153
    struct PathData
154
    {
155
      LargeValue dist;
156
      Arc pred;
157
      PathData(LargeValue d, Arc p = INVALID) :
158
        dist(d), pred(p) {}
159
    };
160

	
161
    typedef typename Digraph::template NodeMap<std::vector<PathData> >
162
      PathDataNodeMap;
163

	
164
  private:
165

	
166
    // The digraph the algorithm runs on
167
    const Digraph &_gr;
168
    // The length of the arcs
169
    const LengthMap &_length;
170

	
171
    // Data for storing the strongly connected components
172
    int _comp_num;
173
    typename Digraph::template NodeMap<int> _comp;
174
    std::vector<std::vector<Node> > _comp_nodes;
175
    std::vector<Node>* _nodes;
176
    typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs;
177

	
178
    // Data for the found cycles
179
    bool _curr_found, _best_found;
180
    LargeValue _curr_length, _best_length;
181
    int _curr_size, _best_size;
182
    Node _curr_node, _best_node;
183
    int _curr_level, _best_level;
184

	
185
    Path *_cycle_path;
186
    bool _local_path;
187

	
188
    // Node map for storing path data
189
    PathDataNodeMap _data;
190
    // The processed nodes in the last round
191
    std::vector<Node> _process;
192

	
193
    Tolerance _tolerance;
194

	
195
    // Infinite constant
196
    const LargeValue INF;
197

	
198
  public:
199

	
200
    /// \name Named Template Parameters
201
    /// @{
202

	
203
    template <typename T>
204
    struct SetLargeValueTraits : public Traits {
205
      typedef T LargeValue;
206
      typedef lemon::Tolerance<T> Tolerance;
207
    };
208

	
209
    /// \brief \ref named-templ-param "Named parameter" for setting
210
    /// \c LargeValue type.
211
    ///
212
    /// \ref named-templ-param "Named parameter" for setting \c LargeValue
213
    /// type. It is used for internal computations in the algorithm.
214
    template <typename T>
215
    struct SetLargeValue
216
      : public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > {
217
      typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create;
218
    };
219

	
220
    template <typename T>
221
    struct SetPathTraits : public Traits {
222
      typedef T Path;
223
    };
224

	
225
    /// \brief \ref named-templ-param "Named parameter" for setting
226
    /// \c %Path type.
227
    ///
228
    /// \ref named-templ-param "Named parameter" for setting the \c %Path
229
    /// type of the found cycles.
230
    /// It must conform to the \ref lemon::concepts::Path "Path" concept
231
    /// and it must have an \c addFront() function.
232
    template <typename T>
233
    struct SetPath
234
      : public HartmannOrlin<GR, LEN, SetPathTraits<T> > {
235
      typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create;
236
    };
237

	
238
    /// @}
239

	
240
  public:
241

	
242
    /// \brief Constructor.
243
    ///
244
    /// The constructor of the class.
245
    ///
246
    /// \param digraph The digraph the algorithm runs on.
247
    /// \param length The lengths (costs) of the arcs.
248
    HartmannOrlin( const Digraph &digraph,
249
                   const LengthMap &length ) :
250
      _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
251
      _best_found(false), _best_length(0), _best_size(1),
252
      _cycle_path(NULL), _local_path(false), _data(digraph),
253
      INF(std::numeric_limits<LargeValue>::has_infinity ?
254
          std::numeric_limits<LargeValue>::infinity() :
255
          std::numeric_limits<LargeValue>::max())
256
    {}
257

	
258
    /// Destructor.
259
    ~HartmannOrlin() {
260
      if (_local_path) delete _cycle_path;
261
    }
262

	
263
    /// \brief Set the path structure for storing the found cycle.
264
    ///
265
    /// This function sets an external path structure for storing the
266
    /// found cycle.
267
    ///
268
    /// If you don't call this function before calling \ref run() or
269
    /// \ref findMinMean(), it will allocate a local \ref Path "path"
270
    /// structure. The destuctor deallocates this automatically
271
    /// allocated object, of course.
272
    ///
273
    /// \note The algorithm calls only the \ref lemon::Path::addFront()
274
    /// "addFront()" function of the given path structure.
275
    ///
276
    /// \return <tt>(*this)</tt>
277
    HartmannOrlin& cycle(Path &path) {
278
      if (_local_path) {
279
        delete _cycle_path;
280
        _local_path = false;
281
      }
282
      _cycle_path = &path;
283
      return *this;
284
    }
285

	
286
    /// \brief Set the tolerance used by the algorithm.
287
    ///
288
    /// This function sets the tolerance object used by the algorithm.
289
    ///
290
    /// \return <tt>(*this)</tt>
291
    HartmannOrlin& tolerance(const Tolerance& tolerance) {
292
      _tolerance = tolerance;
293
      return *this;
294
    }
295

	
296
    /// \brief Return a const reference to the tolerance.
297
    ///
298
    /// This function returns a const reference to the tolerance object
299
    /// used by the algorithm.
300
    const Tolerance& tolerance() const {
301
      return _tolerance;
302
    }
303

	
304
    /// \name Execution control
305
    /// The simplest way to execute the algorithm is to call the \ref run()
306
    /// function.\n
307
    /// If you only need the minimum mean length, you may call
308
    /// \ref findMinMean().
309

	
310
    /// @{
311

	
312
    /// \brief Run the algorithm.
313
    ///
314
    /// This function runs the algorithm.
315
    /// It can be called more than once (e.g. if the underlying digraph
316
    /// and/or the arc lengths have been modified).
317
    ///
318
    /// \return \c true if a directed cycle exists in the digraph.
319
    ///
320
    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
321
    /// \code
322
    ///   return mmc.findMinMean() && mmc.findCycle();
323
    /// \endcode
324
    bool run() {
325
      return findMinMean() && findCycle();
326
    }
327

	
328
    /// \brief Find the minimum cycle mean.
329
    ///
330
    /// This function finds the minimum mean length of the directed
331
    /// cycles in the digraph.
332
    ///
333
    /// \return \c true if a directed cycle exists in the digraph.
334
    bool findMinMean() {
335
      // Initialization and find strongly connected components
336
      init();
337
      findComponents();
338
      
339
      // Find the minimum cycle mean in the components
340
      for (int comp = 0; comp < _comp_num; ++comp) {
341
        if (!initComponent(comp)) continue;
342
        processRounds();
343
        
344
        // Update the best cycle (global minimum mean cycle)
345
        if ( _curr_found && (!_best_found || 
346
             _curr_length * _best_size < _best_length * _curr_size) ) {
347
          _best_found = true;
348
          _best_length = _curr_length;
349
          _best_size = _curr_size;
350
          _best_node = _curr_node;
351
          _best_level = _curr_level;
352
        }
353
      }
354
      return _best_found;
355
    }
356

	
357
    /// \brief Find a minimum mean directed cycle.
358
    ///
359
    /// This function finds a directed cycle of minimum mean length
360
    /// in the digraph using the data computed by findMinMean().
361
    ///
362
    /// \return \c true if a directed cycle exists in the digraph.
363
    ///
364
    /// \pre \ref findMinMean() must be called before using this function.
365
    bool findCycle() {
366
      if (!_best_found) return false;
367
      IntNodeMap reached(_gr, -1);
368
      int r = _best_level + 1;
369
      Node u = _best_node;
370
      while (reached[u] < 0) {
371
        reached[u] = --r;
372
        u = _gr.source(_data[u][r].pred);
373
      }
374
      r = reached[u];
375
      Arc e = _data[u][r].pred;
376
      _cycle_path->addFront(e);
377
      _best_length = _length[e];
378
      _best_size = 1;
379
      Node v;
380
      while ((v = _gr.source(e)) != u) {
381
        e = _data[v][--r].pred;
382
        _cycle_path->addFront(e);
383
        _best_length += _length[e];
384
        ++_best_size;
385
      }
386
      return true;
387
    }
388

	
389
    /// @}
390

	
391
    /// \name Query Functions
392
    /// The results of the algorithm can be obtained using these
393
    /// functions.\n
394
    /// The algorithm should be executed before using them.
395

	
396
    /// @{
397

	
398
    /// \brief Return the total length of the found cycle.
399
    ///
400
    /// This function returns the total length of the found cycle.
401
    ///
402
    /// \pre \ref run() or \ref findMinMean() must be called before
403
    /// using this function.
404
    LargeValue cycleLength() const {
405
      return _best_length;
406
    }
407

	
408
    /// \brief Return the number of arcs on the found cycle.
409
    ///
410
    /// This function returns the number of arcs on the found cycle.
411
    ///
412
    /// \pre \ref run() or \ref findMinMean() must be called before
413
    /// using this function.
414
    int cycleArcNum() const {
415
      return _best_size;
416
    }
417

	
418
    /// \brief Return the mean length of the found cycle.
419
    ///
420
    /// This function returns the mean length of the found cycle.
421
    ///
422
    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
423
    /// following code.
424
    /// \code
425
    ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
426
    /// \endcode
427
    ///
428
    /// \pre \ref run() or \ref findMinMean() must be called before
429
    /// using this function.
430
    double cycleMean() const {
431
      return static_cast<double>(_best_length) / _best_size;
432
    }
433

	
434
    /// \brief Return the found cycle.
435
    ///
436
    /// This function returns a const reference to the path structure
437
    /// storing the found cycle.
438
    ///
439
    /// \pre \ref run() or \ref findCycle() must be called before using
440
    /// this function.
441
    const Path& cycle() const {
442
      return *_cycle_path;
443
    }
444

	
445
    ///@}
446

	
447
  private:
448

	
449
    // Initialization
450
    void init() {
451
      if (!_cycle_path) {
452
        _local_path = true;
453
        _cycle_path = new Path;
454
      }
455
      _cycle_path->clear();
456
      _best_found = false;
457
      _best_length = 0;
458
      _best_size = 1;
459
      _cycle_path->clear();
460
      for (NodeIt u(_gr); u != INVALID; ++u)
461
        _data[u].clear();
462
    }
463

	
464
    // Find strongly connected components and initialize _comp_nodes
465
    // and _out_arcs
466
    void findComponents() {
467
      _comp_num = stronglyConnectedComponents(_gr, _comp);
468
      _comp_nodes.resize(_comp_num);
469
      if (_comp_num == 1) {
470
        _comp_nodes[0].clear();
471
        for (NodeIt n(_gr); n != INVALID; ++n) {
472
          _comp_nodes[0].push_back(n);
473
          _out_arcs[n].clear();
474
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
475
            _out_arcs[n].push_back(a);
476
          }
477
        }
478
      } else {
479
        for (int i = 0; i < _comp_num; ++i)
480
          _comp_nodes[i].clear();
481
        for (NodeIt n(_gr); n != INVALID; ++n) {
482
          int k = _comp[n];
483
          _comp_nodes[k].push_back(n);
484
          _out_arcs[n].clear();
485
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
486
            if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
487
          }
488
        }
489
      }
490
    }
491

	
492
    // Initialize path data for the current component
493
    bool initComponent(int comp) {
494
      _nodes = &(_comp_nodes[comp]);
495
      int n = _nodes->size();
496
      if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
497
        return false;
498
      }      
499
      for (int i = 0; i < n; ++i) {
500
        _data[(*_nodes)[i]].resize(n + 1, PathData(INF));
501
      }
502
      return true;
503
    }
504

	
505
    // Process all rounds of computing path data for the current component.
506
    // _data[v][k] is the length of a shortest directed walk from the root
507
    // node to node v containing exactly k arcs.
508
    void processRounds() {
509
      Node start = (*_nodes)[0];
510
      _data[start][0] = PathData(0);
511
      _process.clear();
512
      _process.push_back(start);
513

	
514
      int k, n = _nodes->size();
515
      int next_check = 4;
516
      bool terminate = false;
517
      for (k = 1; k <= n && int(_process.size()) < n && !terminate; ++k) {
518
        processNextBuildRound(k);
519
        if (k == next_check || k == n) {
520
          terminate = checkTermination(k);
521
          next_check = next_check * 3 / 2;
522
        }
523
      }
524
      for ( ; k <= n && !terminate; ++k) {
525
        processNextFullRound(k);
526
        if (k == next_check || k == n) {
527
          terminate = checkTermination(k);
528
          next_check = next_check * 3 / 2;
529
        }
530
      }
531
    }
532

	
533
    // Process one round and rebuild _process
534
    void processNextBuildRound(int k) {
535
      std::vector<Node> next;
536
      Node u, v;
537
      Arc e;
538
      LargeValue d;
539
      for (int i = 0; i < int(_process.size()); ++i) {
540
        u = _process[i];
541
        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
542
          e = _out_arcs[u][j];
543
          v = _gr.target(e);
544
          d = _data[u][k-1].dist + _length[e];
545
          if (_tolerance.less(d, _data[v][k].dist)) {
546
            if (_data[v][k].dist == INF) next.push_back(v);
547
            _data[v][k] = PathData(d, e);
548
          }
549
        }
550
      }
551
      _process.swap(next);
552
    }
553

	
554
    // Process one round using _nodes instead of _process
555
    void processNextFullRound(int k) {
556
      Node u, v;
557
      Arc e;
558
      LargeValue d;
559
      for (int i = 0; i < int(_nodes->size()); ++i) {
560
        u = (*_nodes)[i];
561
        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
562
          e = _out_arcs[u][j];
563
          v = _gr.target(e);
564
          d = _data[u][k-1].dist + _length[e];
565
          if (_tolerance.less(d, _data[v][k].dist)) {
566
            _data[v][k] = PathData(d, e);
567
          }
568
        }
569
      }
570
    }
571
    
572
    // Check early termination
573
    bool checkTermination(int k) {
574
      typedef std::pair<int, int> Pair;
575
      typename GR::template NodeMap<Pair> level(_gr, Pair(-1, 0));
576
      typename GR::template NodeMap<LargeValue> pi(_gr);
577
      int n = _nodes->size();
578
      LargeValue length;
579
      int size;
580
      Node u;
581
      
582
      // Search for cycles that are already found
583
      _curr_found = false;
584
      for (int i = 0; i < n; ++i) {
585
        u = (*_nodes)[i];
586
        if (_data[u][k].dist == INF) continue;
587
        for (int j = k; j >= 0; --j) {
588
          if (level[u].first == i && level[u].second > 0) {
589
            // A cycle is found
590
            length = _data[u][level[u].second].dist - _data[u][j].dist;
591
            size = level[u].second - j;
592
            if (!_curr_found || length * _curr_size < _curr_length * size) {
593
              _curr_length = length;
594
              _curr_size = size;
595
              _curr_node = u;
596
              _curr_level = level[u].second;
597
              _curr_found = true;
598
            }
599
          }
600
          level[u] = Pair(i, j);
601
          u = _gr.source(_data[u][j].pred);
602
        }
603
      }
604

	
605
      // If at least one cycle is found, check the optimality condition
606
      LargeValue d;
607
      if (_curr_found && k < n) {
608
        // Find node potentials
609
        for (int i = 0; i < n; ++i) {
610
          u = (*_nodes)[i];
611
          pi[u] = INF;
612
          for (int j = 0; j <= k; ++j) {
613
            if (_data[u][j].dist < INF) {
614
              d = _data[u][j].dist * _curr_size - j * _curr_length;
615
              if (_tolerance.less(d, pi[u])) pi[u] = d;
616
            }
617
          }
618
        }
619

	
620
        // Check the optimality condition for all arcs
621
        bool done = true;
622
        for (ArcIt a(_gr); a != INVALID; ++a) {
623
          if (_tolerance.less(_length[a] * _curr_size - _curr_length,
624
                              pi[_gr.target(a)] - pi[_gr.source(a)]) ) {
625
            done = false;
626
            break;
627
          }
628
        }
629
        return done;
630
      }
631
      return (k == n);
632
    }
633

	
634
  }; //class HartmannOrlin
635

	
636
  ///@}
637

	
638
} //namespace lemon
639

	
640
#endif //LEMON_HARTMANN_ORLIN_H
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_HOWARD_H
20
#define LEMON_HOWARD_H
21

	
22
/// \ingroup min_mean_cycle
23
///
24
/// \file
25
/// \brief Howard's algorithm for finding a minimum mean cycle.
26

	
27
#include <vector>
28
#include <limits>
29
#include <lemon/core.h>
30
#include <lemon/path.h>
31
#include <lemon/tolerance.h>
32
#include <lemon/connectivity.h>
33

	
34
namespace lemon {
35

	
36
  /// \brief Default traits class of Howard class.
37
  ///
38
  /// Default traits class of Howard class.
39
  /// \tparam GR The type of the digraph.
40
  /// \tparam LEN The type of the length map.
41
  /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
42
#ifdef DOXYGEN
43
  template <typename GR, typename LEN>
44
#else
45
  template <typename GR, typename LEN,
46
    bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
47
#endif
48
  struct HowardDefaultTraits
49
  {
50
    /// The type of the digraph
51
    typedef GR Digraph;
52
    /// The type of the length map
53
    typedef LEN LengthMap;
54
    /// The type of the arc lengths
55
    typedef typename LengthMap::Value Value;
56

	
57
    /// \brief The large value type used for internal computations
58
    ///
59
    /// The large value type used for internal computations.
60
    /// It is \c long \c long if the \c Value type is integer,
61
    /// otherwise it is \c double.
62
    /// \c Value must be convertible to \c LargeValue.
63
    typedef double LargeValue;
64

	
65
    /// The tolerance type used for internal computations
66
    typedef lemon::Tolerance<LargeValue> Tolerance;
67

	
68
    /// \brief The path type of the found cycles
69
    ///
70
    /// The path type of the found cycles.
71
    /// It must conform to the \ref lemon::concepts::Path "Path" concept
72
    /// and it must have an \c addBack() function.
73
    typedef lemon::Path<Digraph> Path;
74
  };
75

	
76
  // Default traits class for integer value types
77
  template <typename GR, typename LEN>
78
  struct HowardDefaultTraits<GR, LEN, true>
79
  {
80
    typedef GR Digraph;
81
    typedef LEN LengthMap;
82
    typedef typename LengthMap::Value Value;
83
#ifdef LEMON_HAVE_LONG_LONG
84
    typedef long long LargeValue;
85
#else
86
    typedef long LargeValue;
87
#endif
88
    typedef lemon::Tolerance<LargeValue> Tolerance;
89
    typedef lemon::Path<Digraph> Path;
90
  };
91

	
92

	
93
  /// \addtogroup min_mean_cycle
94
  /// @{
95

	
96
  /// \brief Implementation of Howard's algorithm for finding a minimum
97
  /// mean cycle.
98
  ///
99
  /// This class implements Howard's policy iteration algorithm for finding
100
  /// a directed cycle of minimum mean length (cost) in a digraph
101
  /// \ref amo93networkflows, \ref dasdan98minmeancycle.
102
  /// This class provides the most efficient algorithm for the
103
  /// minimum mean cycle problem, though the best known theoretical
104
  /// bound on its running time is exponential.
105
  ///
106
  /// \tparam GR The type of the digraph the algorithm runs on.
107
  /// \tparam LEN The type of the length map. The default
108
  /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
109
#ifdef DOXYGEN
110
  template <typename GR, typename LEN, typename TR>
111
#else
112
  template < typename GR,
113
             typename LEN = typename GR::template ArcMap<int>,
114
             typename TR = HowardDefaultTraits<GR, LEN> >
115
#endif
116
  class Howard
117
  {
118
  public:
119
  
120
    /// The type of the digraph
121
    typedef typename TR::Digraph Digraph;
122
    /// The type of the length map
123
    typedef typename TR::LengthMap LengthMap;
124
    /// The type of the arc lengths
125
    typedef typename TR::Value Value;
126

	
127
    /// \brief The large value type
128
    ///
129
    /// The large value type used for internal computations.
130
    /// Using the \ref HowardDefaultTraits "default traits class",
131
    /// it is \c long \c long if the \c Value type is integer,
132
    /// otherwise it is \c double.
133
    typedef typename TR::LargeValue LargeValue;
134

	
135
    /// The tolerance type
136
    typedef typename TR::Tolerance Tolerance;
137

	
138
    /// \brief The path type of the found cycles
139
    ///
140
    /// The path type of the found cycles.
141
    /// Using the \ref HowardDefaultTraits "default traits class",
142
    /// it is \ref lemon::Path "Path<Digraph>".
143
    typedef typename TR::Path Path;
144

	
145
    /// The \ref HowardDefaultTraits "traits class" of the algorithm
146
    typedef TR Traits;
147

	
148
  private:
149

	
150
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
151
  
152
    // The digraph the algorithm runs on
153
    const Digraph &_gr;
154
    // The length of the arcs
155
    const LengthMap &_length;
156

	
157
    // Data for the found cycles
158
    bool _curr_found, _best_found;
159
    LargeValue _curr_length, _best_length;
160
    int _curr_size, _best_size;
161
    Node _curr_node, _best_node;
162

	
163
    Path *_cycle_path;
164
    bool _local_path;
165

	
166
    // Internal data used by the algorithm
167
    typename Digraph::template NodeMap<Arc> _policy;
168
    typename Digraph::template NodeMap<bool> _reached;
169
    typename Digraph::template NodeMap<int> _level;
170
    typename Digraph::template NodeMap<LargeValue> _dist;
171

	
172
    // Data for storing the strongly connected components
173
    int _comp_num;
174
    typename Digraph::template NodeMap<int> _comp;
175
    std::vector<std::vector<Node> > _comp_nodes;
176
    std::vector<Node>* _nodes;
177
    typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs;
178
    
179
    // Queue used for BFS search
180
    std::vector<Node> _queue;
181
    int _qfront, _qback;
182

	
183
    Tolerance _tolerance;
184
  
185
    // Infinite constant
186
    const LargeValue INF;
187

	
188
  public:
189
  
190
    /// \name Named Template Parameters
191
    /// @{
192

	
193
    template <typename T>
194
    struct SetLargeValueTraits : public Traits {
195
      typedef T LargeValue;
196
      typedef lemon::Tolerance<T> Tolerance;
197
    };
198

	
199
    /// \brief \ref named-templ-param "Named parameter" for setting
200
    /// \c LargeValue type.
201
    ///
202
    /// \ref named-templ-param "Named parameter" for setting \c LargeValue
203
    /// type. It is used for internal computations in the algorithm.
204
    template <typename T>
205
    struct SetLargeValue
206
      : public Howard<GR, LEN, SetLargeValueTraits<T> > {
207
      typedef Howard<GR, LEN, SetLargeValueTraits<T> > Create;
208
    };
209

	
210
    template <typename T>
211
    struct SetPathTraits : public Traits {
212
      typedef T Path;
213
    };
214

	
215
    /// \brief \ref named-templ-param "Named parameter" for setting
216
    /// \c %Path type.
217
    ///
218
    /// \ref named-templ-param "Named parameter" for setting the \c %Path
219
    /// type of the found cycles.
220
    /// It must conform to the \ref lemon::concepts::Path "Path" concept
221
    /// and it must have an \c addBack() function.
222
    template <typename T>
223
    struct SetPath
224
      : public Howard<GR, LEN, SetPathTraits<T> > {
225
      typedef Howard<GR, LEN, SetPathTraits<T> > Create;
226
    };
227
    
228
    /// @}
229

	
230
  public:
231

	
232
    /// \brief Constructor.
233
    ///
234
    /// The constructor of the class.
235
    ///
236
    /// \param digraph The digraph the algorithm runs on.
237
    /// \param length The lengths (costs) of the arcs.
238
    Howard( const Digraph &digraph,
239
            const LengthMap &length ) :
240
      _gr(digraph), _length(length), _best_found(false),
241
      _best_length(0), _best_size(1), _cycle_path(NULL), _local_path(false),
242
      _policy(digraph), _reached(digraph), _level(digraph), _dist(digraph),
243
      _comp(digraph), _in_arcs(digraph),
244
      INF(std::numeric_limits<LargeValue>::has_infinity ?
245
          std::numeric_limits<LargeValue>::infinity() :
246
          std::numeric_limits<LargeValue>::max())
247
    {}
248

	
249
    /// Destructor.
250
    ~Howard() {
251
      if (_local_path) delete _cycle_path;
252
    }
253

	
254
    /// \brief Set the path structure for storing the found cycle.
255
    ///
256
    /// This function sets an external path structure for storing the
257
    /// found cycle.
258
    ///
259
    /// If you don't call this function before calling \ref run() or
260
    /// \ref findMinMean(), it will allocate a local \ref Path "path"
261
    /// structure. The destuctor deallocates this automatically
262
    /// allocated object, of course.
263
    ///
264
    /// \note The algorithm calls only the \ref lemon::Path::addBack()
265
    /// "addBack()" function of the given path structure.
266
    ///
267
    /// \return <tt>(*this)</tt>
268
    Howard& cycle(Path &path) {
269
      if (_local_path) {
270
        delete _cycle_path;
271
        _local_path = false;
272
      }
273
      _cycle_path = &path;
274
      return *this;
275
    }
276

	
277
    /// \brief Set the tolerance used by the algorithm.
278
    ///
279
    /// This function sets the tolerance object used by the algorithm.
280
    ///
281
    /// \return <tt>(*this)</tt>
282
    Howard& tolerance(const Tolerance& tolerance) {
283
      _tolerance = tolerance;
284
      return *this;
285
    }
286

	
287
    /// \brief Return a const reference to the tolerance.
288
    ///
289
    /// This function returns a const reference to the tolerance object
290
    /// used by the algorithm.
291
    const Tolerance& tolerance() const {
292
      return _tolerance;
293
    }
294

	
295
    /// \name Execution control
296
    /// The simplest way to execute the algorithm is to call the \ref run()
297
    /// function.\n
298
    /// If you only need the minimum mean length, you may call
299
    /// \ref findMinMean().
300

	
301
    /// @{
302

	
303
    /// \brief Run the algorithm.
304
    ///
305
    /// This function runs the algorithm.
306
    /// It can be called more than once (e.g. if the underlying digraph
307
    /// and/or the arc lengths have been modified).
308
    ///
309
    /// \return \c true if a directed cycle exists in the digraph.
310
    ///
311
    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
312
    /// \code
313
    ///   return mmc.findMinMean() && mmc.findCycle();
314
    /// \endcode
315
    bool run() {
316
      return findMinMean() && findCycle();
317
    }
318

	
319
    /// \brief Find the minimum cycle mean.
320
    ///
321
    /// This function finds the minimum mean length of the directed
322
    /// cycles in the digraph.
323
    ///
324
    /// \return \c true if a directed cycle exists in the digraph.
325
    bool findMinMean() {
326
      // Initialize and find strongly connected components
327
      init();
328
      findComponents();
329
      
330
      // Find the minimum cycle mean in the components
331
      for (int comp = 0; comp < _comp_num; ++comp) {
332
        // Find the minimum mean cycle in the current component
333
        if (!buildPolicyGraph(comp)) continue;
334
        while (true) {
335
          findPolicyCycle();
336
          if (!computeNodeDistances()) break;
337
        }
338
        // Update the best cycle (global minimum mean cycle)
339
        if ( _curr_found && (!_best_found ||
340
             _curr_length * _best_size < _best_length * _curr_size) ) {
341
          _best_found = true;
342
          _best_length = _curr_length;
343
          _best_size = _curr_size;
344
          _best_node = _curr_node;
345
        }
346
      }
347
      return _best_found;
348
    }
349

	
350
    /// \brief Find a minimum mean directed cycle.
351
    ///
352
    /// This function finds a directed cycle of minimum mean length
353
    /// in the digraph using the data computed by findMinMean().
354
    ///
355
    /// \return \c true if a directed cycle exists in the digraph.
356
    ///
357
    /// \pre \ref findMinMean() must be called before using this function.
358
    bool findCycle() {
359
      if (!_best_found) return false;
360
      _cycle_path->addBack(_policy[_best_node]);
361
      for ( Node v = _best_node;
362
            (v = _gr.target(_policy[v])) != _best_node; ) {
363
        _cycle_path->addBack(_policy[v]);
364
      }
365
      return true;
366
    }
367

	
368
    /// @}
369

	
370
    /// \name Query Functions
371
    /// The results of the algorithm can be obtained using these
372
    /// functions.\n
373
    /// The algorithm should be executed before using them.
374

	
375
    /// @{
376

	
377
    /// \brief Return the total length of the found cycle.
378
    ///
379
    /// This function returns the total length of the found cycle.
380
    ///
381
    /// \pre \ref run() or \ref findMinMean() must be called before
382
    /// using this function.
383
    LargeValue cycleLength() const {
384
      return _best_length;
385
    }
386

	
387
    /// \brief Return the number of arcs on the found cycle.
388
    ///
389
    /// This function returns the number of arcs on the found cycle.
390
    ///
391
    /// \pre \ref run() or \ref findMinMean() must be called before
392
    /// using this function.
393
    int cycleArcNum() const {
394
      return _best_size;
395
    }
396

	
397
    /// \brief Return the mean length of the found cycle.
398
    ///
399
    /// This function returns the mean length of the found cycle.
400
    ///
401
    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
402
    /// following code.
403
    /// \code
404
    ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
405
    /// \endcode
406
    ///
407
    /// \pre \ref run() or \ref findMinMean() must be called before
408
    /// using this function.
409
    double cycleMean() const {
410
      return static_cast<double>(_best_length) / _best_size;
411
    }
412

	
413
    /// \brief Return the found cycle.
414
    ///
415
    /// This function returns a const reference to the path structure
416
    /// storing the found cycle.
417
    ///
418
    /// \pre \ref run() or \ref findCycle() must be called before using
419
    /// this function.
420
    const Path& cycle() const {
421
      return *_cycle_path;
422
    }
423

	
424
    ///@}
425

	
426
  private:
427

	
428
    // Initialize
429
    void init() {
430
      if (!_cycle_path) {
431
        _local_path = true;
432
        _cycle_path = new Path;
433
      }
434
      _queue.resize(countNodes(_gr));
435
      _best_found = false;
436
      _best_length = 0;
437
      _best_size = 1;
438
      _cycle_path->clear();
439
    }
440
    
441
    // Find strongly connected components and initialize _comp_nodes
442
    // and _in_arcs
443
    void findComponents() {
444
      _comp_num = stronglyConnectedComponents(_gr, _comp);
445
      _comp_nodes.resize(_comp_num);
446
      if (_comp_num == 1) {
447
        _comp_nodes[0].clear();
448
        for (NodeIt n(_gr); n != INVALID; ++n) {
449
          _comp_nodes[0].push_back(n);
450
          _in_arcs[n].clear();
451
          for (InArcIt a(_gr, n); a != INVALID; ++a) {
452
            _in_arcs[n].push_back(a);
453
          }
454
        }
455
      } else {
456
        for (int i = 0; i < _comp_num; ++i)
457
          _comp_nodes[i].clear();
458
        for (NodeIt n(_gr); n != INVALID; ++n) {
459
          int k = _comp[n];
460
          _comp_nodes[k].push_back(n);
461
          _in_arcs[n].clear();
462
          for (InArcIt a(_gr, n); a != INVALID; ++a) {
463
            if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a);
464
          }
465
        }
466
      }
467
    }
468

	
469
    // Build the policy graph in the given strongly connected component
470
    // (the out-degree of every node is 1)
471
    bool buildPolicyGraph(int comp) {
472
      _nodes = &(_comp_nodes[comp]);
473
      if (_nodes->size() < 1 ||
474
          (_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) {
475
        return false;
476
      }
477
      for (int i = 0; i < int(_nodes->size()); ++i) {
478
        _dist[(*_nodes)[i]] = INF;
479
      }
480
      Node u, v;
481
      Arc e;
482
      for (int i = 0; i < int(_nodes->size()); ++i) {
483
        v = (*_nodes)[i];
484
        for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
485
          e = _in_arcs[v][j];
486
          u = _gr.source(e);
487
          if (_length[e] < _dist[u]) {
488
            _dist[u] = _length[e];
489
            _policy[u] = e;
490
          }
491
        }
492
      }
493
      return true;
494
    }
495

	
496
    // Find the minimum mean cycle in the policy graph
497
    void findPolicyCycle() {
498
      for (int i = 0; i < int(_nodes->size()); ++i) {
499
        _level[(*_nodes)[i]] = -1;
500
      }
501
      LargeValue clength;
502
      int csize;
503
      Node u, v;
504
      _curr_found = false;
505
      for (int i = 0; i < int(_nodes->size()); ++i) {
506
        u = (*_nodes)[i];
507
        if (_level[u] >= 0) continue;
508
        for (; _level[u] < 0; u = _gr.target(_policy[u])) {
509
          _level[u] = i;
510
        }
511
        if (_level[u] == i) {
512
          // A cycle is found
513
          clength = _length[_policy[u]];
514
          csize = 1;
515
          for (v = u; (v = _gr.target(_policy[v])) != u; ) {
516
            clength += _length[_policy[v]];
517
            ++csize;
518
          }
519
          if ( !_curr_found ||
520
               (clength * _curr_size < _curr_length * csize) ) {
521
            _curr_found = true;
522
            _curr_length = clength;
523
            _curr_size = csize;
524
            _curr_node = u;
525
          }
526
        }
527
      }
528
    }
529

	
530
    // Contract the policy graph and compute node distances
531
    bool computeNodeDistances() {
532
      // Find the component of the main cycle and compute node distances
533
      // using reverse BFS
534
      for (int i = 0; i < int(_nodes->size()); ++i) {
535
        _reached[(*_nodes)[i]] = false;
536
      }
537
      _qfront = _qback = 0;
538
      _queue[0] = _curr_node;
539
      _reached[_curr_node] = true;
540
      _dist[_curr_node] = 0;
541
      Node u, v;
542
      Arc e;
543
      while (_qfront <= _qback) {
544
        v = _queue[_qfront++];
545
        for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
546
          e = _in_arcs[v][j];
547
          u = _gr.source(e);
548
          if (_policy[u] == e && !_reached[u]) {
549
            _reached[u] = true;
550
            _dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length;
551
            _queue[++_qback] = u;
552
          }
553
        }
554
      }
555

	
556
      // Connect all other nodes to this component and compute node
557
      // distances using reverse BFS
558
      _qfront = 0;
559
      while (_qback < int(_nodes->size())-1) {
560
        v = _queue[_qfront++];
561
        for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
562
          e = _in_arcs[v][j];
563
          u = _gr.source(e);
564
          if (!_reached[u]) {
565
            _reached[u] = true;
566
            _policy[u] = e;
567
            _dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length;
568
            _queue[++_qback] = u;
569
          }
570
        }
571
      }
572

	
573
      // Improve node distances
574
      bool improved = false;
575
      for (int i = 0; i < int(_nodes->size()); ++i) {
576
        v = (*_nodes)[i];
577
        for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
578
          e = _in_arcs[v][j];
579
          u = _gr.source(e);
580
          LargeValue delta = _dist[v] + _length[e] * _curr_size - _curr_length;
581
          if (_tolerance.less(delta, _dist[u])) {
582
            _dist[u] = delta;
583
            _policy[u] = e;
584
            improved = true;
585
          }
586
        }
587
      }
588
      return improved;
589
    }
590

	
591
  }; //class Howard
592

	
593
  ///@}
594

	
595
} //namespace lemon
596

	
597
#endif //LEMON_HOWARD_H
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_KARP_H
20
#define LEMON_KARP_H
21

	
22
/// \ingroup min_mean_cycle
23
///
24
/// \file
25
/// \brief Karp's algorithm for finding a minimum mean cycle.
26

	
27
#include <vector>
28
#include <limits>
29
#include <lemon/core.h>
30
#include <lemon/path.h>
31
#include <lemon/tolerance.h>
32
#include <lemon/connectivity.h>
33

	
34
namespace lemon {
35

	
36
  /// \brief Default traits class of Karp algorithm.
37
  ///
38
  /// Default traits class of Karp algorithm.
39
  /// \tparam GR The type of the digraph.
40
  /// \tparam LEN The type of the length map.
41
  /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
42
#ifdef DOXYGEN
43
  template <typename GR, typename LEN>
44
#else
45
  template <typename GR, typename LEN,
46
    bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
47
#endif
48
  struct KarpDefaultTraits
49
  {
50
    /// The type of the digraph
51
    typedef GR Digraph;
52
    /// The type of the length map
53
    typedef LEN LengthMap;
54
    /// The type of the arc lengths
55
    typedef typename LengthMap::Value Value;
56

	
57
    /// \brief The large value type used for internal computations
58
    ///
59
    /// The large value type used for internal computations.
60
    /// It is \c long \c long if the \c Value type is integer,
61
    /// otherwise it is \c double.
62
    /// \c Value must be convertible to \c LargeValue.
63
    typedef double LargeValue;
64

	
65
    /// The tolerance type used for internal computations
66
    typedef lemon::Tolerance<LargeValue> Tolerance;
67

	
68
    /// \brief The path type of the found cycles
69
    ///
70
    /// The path type of the found cycles.
71
    /// It must conform to the \ref lemon::concepts::Path "Path" concept
72
    /// and it must have an \c addFront() function.
73
    typedef lemon::Path<Digraph> Path;
74
  };
75

	
76
  // Default traits class for integer value types
77
  template <typename GR, typename LEN>
78
  struct KarpDefaultTraits<GR, LEN, true>
79
  {
80
    typedef GR Digraph;
81
    typedef LEN LengthMap;
82
    typedef typename LengthMap::Value Value;
83
#ifdef LEMON_HAVE_LONG_LONG
84
    typedef long long LargeValue;
85
#else
86
    typedef long LargeValue;
87
#endif
88
    typedef lemon::Tolerance<LargeValue> Tolerance;
89
    typedef lemon::Path<Digraph> Path;
90
  };
91

	
92

	
93
  /// \addtogroup min_mean_cycle
94
  /// @{
95

	
96
  /// \brief Implementation of Karp's algorithm for finding a minimum
97
  /// mean cycle.
98
  ///
99
  /// This class implements Karp's algorithm for finding a directed
100
  /// cycle of minimum mean length (cost) in a digraph
101
  /// \ref amo93networkflows, \ref dasdan98minmeancycle.
102
  /// It runs in time O(ne) and uses space O(n<sup>2</sup>+e).
103
  ///
104
  /// \tparam GR The type of the digraph the algorithm runs on.
105
  /// \tparam LEN The type of the length map. The default
106
  /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
107
#ifdef DOXYGEN
108
  template <typename GR, typename LEN, typename TR>
109
#else
110
  template < typename GR,
111
             typename LEN = typename GR::template ArcMap<int>,
112
             typename TR = KarpDefaultTraits<GR, LEN> >
113
#endif
114
  class Karp
115
  {
116
  public:
117

	
118
    /// The type of the digraph
119
    typedef typename TR::Digraph Digraph;
120
    /// The type of the length map
121
    typedef typename TR::LengthMap LengthMap;
122
    /// The type of the arc lengths
123
    typedef typename TR::Value Value;
124

	
125
    /// \brief The large value type
126
    ///
127
    /// The large value type used for internal computations.
128
    /// Using the \ref KarpDefaultTraits "default traits class",
129
    /// it is \c long \c long if the \c Value type is integer,
130
    /// otherwise it is \c double.
131
    typedef typename TR::LargeValue LargeValue;
132

	
133
    /// The tolerance type
134
    typedef typename TR::Tolerance Tolerance;
135

	
136
    /// \brief The path type of the found cycles
137
    ///
138
    /// The path type of the found cycles.
139
    /// Using the \ref KarpDefaultTraits "default traits class",
140
    /// it is \ref lemon::Path "Path<Digraph>".
141
    typedef typename TR::Path Path;
142

	
143
    /// The \ref KarpDefaultTraits "traits class" of the algorithm
144
    typedef TR Traits;
145

	
146
  private:
147

	
148
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
149

	
150
    // Data sturcture for path data
151
    struct PathData
152
    {
153
      LargeValue dist;
154
      Arc pred;
155
      PathData(LargeValue d, Arc p = INVALID) :
156
        dist(d), pred(p) {}
157
    };
158

	
159
    typedef typename Digraph::template NodeMap<std::vector<PathData> >
160
      PathDataNodeMap;
161

	
162
  private:
163

	
164
    // The digraph the algorithm runs on
165
    const Digraph &_gr;
166
    // The length of the arcs
167
    const LengthMap &_length;
168

	
169
    // Data for storing the strongly connected components
170
    int _comp_num;
171
    typename Digraph::template NodeMap<int> _comp;
172
    std::vector<std::vector<Node> > _comp_nodes;
173
    std::vector<Node>* _nodes;
174
    typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs;
175

	
176
    // Data for the found cycle
177
    LargeValue _cycle_length;
178
    int _cycle_size;
179
    Node _cycle_node;
180

	
181
    Path *_cycle_path;
182
    bool _local_path;
183

	
184
    // Node map for storing path data
185
    PathDataNodeMap _data;
186
    // The processed nodes in the last round
187
    std::vector<Node> _process;
188

	
189
    Tolerance _tolerance;
190
    
191
    // Infinite constant
192
    const LargeValue INF;
193

	
194
  public:
195

	
196
    /// \name Named Template Parameters
197
    /// @{
198

	
199
    template <typename T>
200
    struct SetLargeValueTraits : public Traits {
201
      typedef T LargeValue;
202
      typedef lemon::Tolerance<T> Tolerance;
203
    };
204

	
205
    /// \brief \ref named-templ-param "Named parameter" for setting
206
    /// \c LargeValue type.
207
    ///
208
    /// \ref named-templ-param "Named parameter" for setting \c LargeValue
209
    /// type. It is used for internal computations in the algorithm.
210
    template <typename T>
211
    struct SetLargeValue
212
      : public Karp<GR, LEN, SetLargeValueTraits<T> > {
213
      typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create;
214
    };
215

	
216
    template <typename T>
217
    struct SetPathTraits : public Traits {
218
      typedef T Path;
219
    };
220

	
221
    /// \brief \ref named-templ-param "Named parameter" for setting
222
    /// \c %Path type.
223
    ///
224
    /// \ref named-templ-param "Named parameter" for setting the \c %Path
225
    /// type of the found cycles.
226
    /// It must conform to the \ref lemon::concepts::Path "Path" concept
227
    /// and it must have an \c addFront() function.
228
    template <typename T>
229
    struct SetPath
230
      : public Karp<GR, LEN, SetPathTraits<T> > {
231
      typedef Karp<GR, LEN, SetPathTraits<T> > Create;
232
    };
233

	
234
    /// @}
235

	
236
  public:
237

	
238
    /// \brief Constructor.
239
    ///
240
    /// The constructor of the class.
241
    ///
242
    /// \param digraph The digraph the algorithm runs on.
243
    /// \param length The lengths (costs) of the arcs.
244
    Karp( const Digraph &digraph,
245
          const LengthMap &length ) :
246
      _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
247
      _cycle_length(0), _cycle_size(1), _cycle_node(INVALID),
248
      _cycle_path(NULL), _local_path(false), _data(digraph),
249
      INF(std::numeric_limits<LargeValue>::has_infinity ?
250
          std::numeric_limits<LargeValue>::infinity() :
251
          std::numeric_limits<LargeValue>::max())
252
    {}
253

	
254
    /// Destructor.
255
    ~Karp() {
256
      if (_local_path) delete _cycle_path;
257
    }
258

	
259
    /// \brief Set the path structure for storing the found cycle.
260
    ///
261
    /// This function sets an external path structure for storing the
262
    /// found cycle.
263
    ///
264
    /// If you don't call this function before calling \ref run() or
265
    /// \ref findMinMean(), it will allocate a local \ref Path "path"
266
    /// structure. The destuctor deallocates this automatically
267
    /// allocated object, of course.
268
    ///
269
    /// \note The algorithm calls only the \ref lemon::Path::addFront()
270
    /// "addFront()" function of the given path structure.
271
    ///
272
    /// \return <tt>(*this)</tt>
273
    Karp& cycle(Path &path) {
274
      if (_local_path) {
275
        delete _cycle_path;
276
        _local_path = false;
277
      }
278
      _cycle_path = &path;
279
      return *this;
280
    }
281

	
282
    /// \brief Set the tolerance used by the algorithm.
283
    ///
284
    /// This function sets the tolerance object used by the algorithm.
285
    ///
286
    /// \return <tt>(*this)</tt>
287
    Karp& tolerance(const Tolerance& tolerance) {
288
      _tolerance = tolerance;
289
      return *this;
290
    }
291

	
292
    /// \brief Return a const reference to the tolerance.
293
    ///
294
    /// This function returns a const reference to the tolerance object
295
    /// used by the algorithm.
296
    const Tolerance& tolerance() const {
297
      return _tolerance;
298
    }
299

	
300
    /// \name Execution control
301
    /// The simplest way to execute the algorithm is to call the \ref run()
302
    /// function.\n
303
    /// If you only need the minimum mean length, you may call
304
    /// \ref findMinMean().
305

	
306
    /// @{
307

	
308
    /// \brief Run the algorithm.
309
    ///
310
    /// This function runs the algorithm.
311
    /// It can be called more than once (e.g. if the underlying digraph
312
    /// and/or the arc lengths have been modified).
313
    ///
314
    /// \return \c true if a directed cycle exists in the digraph.
315
    ///
316
    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
317
    /// \code
318
    ///   return mmc.findMinMean() && mmc.findCycle();
319
    /// \endcode
320
    bool run() {
321
      return findMinMean() && findCycle();
322
    }
323

	
324
    /// \brief Find the minimum cycle mean.
325
    ///
326
    /// This function finds the minimum mean length of the directed
327
    /// cycles in the digraph.
328
    ///
329
    /// \return \c true if a directed cycle exists in the digraph.
330
    bool findMinMean() {
331
      // Initialization and find strongly connected components
332
      init();
333
      findComponents();
334
      
335
      // Find the minimum cycle mean in the components
336
      for (int comp = 0; comp < _comp_num; ++comp) {
337
        if (!initComponent(comp)) continue;
338
        processRounds();
339
        updateMinMean();
340
      }
341
      return (_cycle_node != INVALID);
342
    }
343

	
344
    /// \brief Find a minimum mean directed cycle.
345
    ///
346
    /// This function finds a directed cycle of minimum mean length
347
    /// in the digraph using the data computed by findMinMean().
348
    ///
349
    /// \return \c true if a directed cycle exists in the digraph.
350
    ///
351
    /// \pre \ref findMinMean() must be called before using this function.
352
    bool findCycle() {
353
      if (_cycle_node == INVALID) return false;
354
      IntNodeMap reached(_gr, -1);
355
      int r = _data[_cycle_node].size();
356
      Node u = _cycle_node;
357
      while (reached[u] < 0) {
358
        reached[u] = --r;
359
        u = _gr.source(_data[u][r].pred);
360
      }
361
      r = reached[u];
362
      Arc e = _data[u][r].pred;
363
      _cycle_path->addFront(e);
364
      _cycle_length = _length[e];
365
      _cycle_size = 1;
366
      Node v;
367
      while ((v = _gr.source(e)) != u) {
368
        e = _data[v][--r].pred;
369
        _cycle_path->addFront(e);
370
        _cycle_length += _length[e];
371
        ++_cycle_size;
372
      }
373
      return true;
374
    }
375

	
376
    /// @}
377

	
378
    /// \name Query Functions
379
    /// The results of the algorithm can be obtained using these
380
    /// functions.\n
381
    /// The algorithm should be executed before using them.
382

	
383
    /// @{
384

	
385
    /// \brief Return the total length of the found cycle.
386
    ///
387
    /// This function returns the total length of the found cycle.
388
    ///
389
    /// \pre \ref run() or \ref findMinMean() must be called before
390
    /// using this function.
391
    LargeValue cycleLength() const {
392
      return _cycle_length;
393
    }
394

	
395
    /// \brief Return the number of arcs on the found cycle.
396
    ///
397
    /// This function returns the number of arcs on the found cycle.
398
    ///
399
    /// \pre \ref run() or \ref findMinMean() must be called before
400
    /// using this function.
401
    int cycleArcNum() const {
402
      return _cycle_size;
403
    }
404

	
405
    /// \brief Return the mean length of the found cycle.
406
    ///
407
    /// This function returns the mean length of the found cycle.
408
    ///
409
    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
410
    /// following code.
411
    /// \code
412
    ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
413
    /// \endcode
414
    ///
415
    /// \pre \ref run() or \ref findMinMean() must be called before
416
    /// using this function.
417
    double cycleMean() const {
418
      return static_cast<double>(_cycle_length) / _cycle_size;
419
    }
420

	
421
    /// \brief Return the found cycle.
422
    ///
423
    /// This function returns a const reference to the path structure
424
    /// storing the found cycle.
425
    ///
426
    /// \pre \ref run() or \ref findCycle() must be called before using
427
    /// this function.
428
    const Path& cycle() const {
429
      return *_cycle_path;
430
    }
431

	
432
    ///@}
433

	
434
  private:
435

	
436
    // Initialization
437
    void init() {
438
      if (!_cycle_path) {
439
        _local_path = true;
440
        _cycle_path = new Path;
441
      }
442
      _cycle_path->clear();
443
      _cycle_length = 0;
444
      _cycle_size = 1;
445
      _cycle_node = INVALID;
446
      for (NodeIt u(_gr); u != INVALID; ++u)
447
        _data[u].clear();
448
    }
449

	
450
    // Find strongly connected components and initialize _comp_nodes
451
    // and _out_arcs
452
    void findComponents() {
453
      _comp_num = stronglyConnectedComponents(_gr, _comp);
454
      _comp_nodes.resize(_comp_num);
455
      if (_comp_num == 1) {
456
        _comp_nodes[0].clear();
457
        for (NodeIt n(_gr); n != INVALID; ++n) {
458
          _comp_nodes[0].push_back(n);
459
          _out_arcs[n].clear();
460
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
461
            _out_arcs[n].push_back(a);
462
          }
463
        }
464
      } else {
465
        for (int i = 0; i < _comp_num; ++i)
466
          _comp_nodes[i].clear();
467
        for (NodeIt n(_gr); n != INVALID; ++n) {
468
          int k = _comp[n];
469
          _comp_nodes[k].push_back(n);
470
          _out_arcs[n].clear();
471
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
472
            if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
473
          }
474
        }
475
      }
476
    }
477

	
478
    // Initialize path data for the current component
479
    bool initComponent(int comp) {
480
      _nodes = &(_comp_nodes[comp]);
481
      int n = _nodes->size();
482
      if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
483
        return false;
484
      }      
485
      for (int i = 0; i < n; ++i) {
486
        _data[(*_nodes)[i]].resize(n + 1, PathData(INF));
487
      }
488
      return true;
489
    }
490

	
491
    // Process all rounds of computing path data for the current component.
492
    // _data[v][k] is the length of a shortest directed walk from the root
493
    // node to node v containing exactly k arcs.
494
    void processRounds() {
495
      Node start = (*_nodes)[0];
496
      _data[start][0] = PathData(0);
497
      _process.clear();
498
      _process.push_back(start);
499

	
500
      int k, n = _nodes->size();
501
      for (k = 1; k <= n && int(_process.size()) < n; ++k) {
502
        processNextBuildRound(k);
503
      }
504
      for ( ; k <= n; ++k) {
505
        processNextFullRound(k);
506
      }
507
    }
508

	
509
    // Process one round and rebuild _process
510
    void processNextBuildRound(int k) {
511
      std::vector<Node> next;
512
      Node u, v;
513
      Arc e;
514
      LargeValue d;
515
      for (int i = 0; i < int(_process.size()); ++i) {
516
        u = _process[i];
517
        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
518
          e = _out_arcs[u][j];
519
          v = _gr.target(e);
520
          d = _data[u][k-1].dist + _length[e];
521
          if (_tolerance.less(d, _data[v][k].dist)) {
522
            if (_data[v][k].dist == INF) next.push_back(v);
523
            _data[v][k] = PathData(d, e);
524
          }
525
        }
526
      }
527
      _process.swap(next);
528
    }
529

	
530
    // Process one round using _nodes instead of _process
531
    void processNextFullRound(int k) {
532
      Node u, v;
533
      Arc e;
534
      LargeValue d;
535
      for (int i = 0; i < int(_nodes->size()); ++i) {
536
        u = (*_nodes)[i];
537
        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
538
          e = _out_arcs[u][j];
539
          v = _gr.target(e);
540
          d = _data[u][k-1].dist + _length[e];
541
          if (_tolerance.less(d, _data[v][k].dist)) {
542
            _data[v][k] = PathData(d, e);
543
          }
544
        }
545
      }
546
    }
547

	
548
    // Update the minimum cycle mean
549
    void updateMinMean() {
550
      int n = _nodes->size();
551
      for (int i = 0; i < n; ++i) {
552
        Node u = (*_nodes)[i];
553
        if (_data[u][n].dist == INF) continue;
554
        LargeValue length, max_length = 0;
555
        int size, max_size = 1;
556
        bool found_curr = false;
557
        for (int k = 0; k < n; ++k) {
558
          if (_data[u][k].dist == INF) continue;
559
          length = _data[u][n].dist - _data[u][k].dist;
560
          size = n - k;
561
          if (!found_curr || length * max_size > max_length * size) {
562
            found_curr = true;
563
            max_length = length;
564
            max_size = size;
565
          }
566
        }
567
        if ( found_curr && (_cycle_node == INVALID ||
568
             max_length * _cycle_size < _cycle_length * max_size) ) {
569
          _cycle_length = max_length;
570
          _cycle_size = max_size;
571
          _cycle_node = u;
572
        }
573
      }
574
    }
575

	
576
  }; //class Karp
577

	
578
  ///@}
579

	
580
} //namespace lemon
581

	
582
#endif //LEMON_KARP_H
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_KARY_HEAP_H
20
#define LEMON_KARY_HEAP_H
21

	
22
///\ingroup heaps
23
///\file
24
///\brief Fourary heap implementation.
25

	
26
#include <vector>
27
#include <utility>
28
#include <functional>
29

	
30
namespace lemon {
31

	
32
  /// \ingroup heaps
33
  ///
34
  ///\brief K-ary heap data structure.
35
  ///
36
  /// This class implements the \e K-ary \e heap data structure.
37
  /// It fully conforms to the \ref concepts::Heap "heap concept".
38
  ///
39
  /// The \ref KaryHeap "K-ary heap" is a generalization of the
40
  /// \ref BinHeap "binary heap" structure, its nodes have at most
41
  /// \c K children, instead of two.
42
  /// \ref BinHeap and \ref FouraryHeap are specialized implementations
43
  /// of this structure for <tt>K=2</tt> and <tt>K=4</tt>, respectively.
44
  ///
45
  /// \tparam PR Type of the priorities of the items.
46
  /// \tparam IM A read-writable item map with \c int values, used
47
  /// internally to handle the cross references.
48
  /// \tparam K The degree of the heap, each node have at most \e K
49
  /// children. The default is 16. Powers of two are suggested to use
50
  /// so that the multiplications and divisions needed to traverse the
51
  /// nodes of the heap could be performed faster.
52
  /// \tparam CMP A functor class for comparing the priorities.
53
  /// The default is \c std::less<PR>.
54
  ///
55
  ///\sa BinHeap
56
  ///\sa FouraryHeap
57
#ifdef DOXYGEN
58
  template <typename PR, typename IM, int K, typename CMP>
59
#else
60
  template <typename PR, typename IM, int K = 16,
61
            typename CMP = std::less<PR> >
62
#endif
63
  class KaryHeap {
64
  public:
65
    /// Type of the item-int map.
66
    typedef IM ItemIntMap;
67
    /// Type of the priorities.
68
    typedef PR Prio;
69
    /// Type of the items stored in the heap.
70
    typedef typename ItemIntMap::Key Item;
71
    /// Type of the item-priority pairs.
72
    typedef std::pair<Item,Prio> Pair;
73
    /// Functor type for comparing the priorities.
74
    typedef CMP Compare;
75

	
76
    /// \brief Type to represent the states of the items.
77
    ///
78
    /// Each item has a state associated to it. It can be "in heap",
79
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
80
    /// heap's point of view, but may be useful to the user.
81
    ///
82
    /// The item-int map must be initialized in such way that it assigns
83
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
84
    enum State {
85
      IN_HEAP = 0,    ///< = 0.
86
      PRE_HEAP = -1,  ///< = -1.
87
      POST_HEAP = -2  ///< = -2.
88
    };
89

	
90
  private:
91
    std::vector<Pair> _data;
92
    Compare _comp;
93
    ItemIntMap &_iim;
94

	
95
  public:
96
    /// \brief Constructor.
97
    ///
98
    /// Constructor.
99
    /// \param map A map that assigns \c int values to the items.
100
    /// It is used internally to handle the cross references.
101
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
102
    explicit KaryHeap(ItemIntMap &map) : _iim(map) {}
103

	
104
    /// \brief Constructor.
105
    ///
106
    /// Constructor.
107
    /// \param map A map that assigns \c int values to the items.
108
    /// It is used internally to handle the cross references.
109
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
110
    /// \param comp The function object used for comparing the priorities.
111
    KaryHeap(ItemIntMap &map, const Compare &comp)
112
      : _iim(map), _comp(comp) {}
113

	
114
    /// \brief The number of items stored in the heap.
115
    ///
116
    /// This function returns the number of items stored in the heap.
117
    int size() const { return _data.size(); }
118

	
119
    /// \brief Check if the heap is empty.
120
    ///
121
    /// This function returns \c true if the heap is empty.
122
    bool empty() const { return _data.empty(); }
123

	
124
    /// \brief Make the heap empty.
125
    ///
126
    /// This functon makes the heap empty.
127
    /// It does not change the cross reference map. If you want to reuse
128
    /// a heap that is not surely empty, you should first clear it and
129
    /// then you should set the cross reference map to \c PRE_HEAP
130
    /// for each item.
131
    void clear() { _data.clear(); }
132

	
133
  private:
134
    int parent(int i) { return (i-1)/K; }
135
    int firstChild(int i) { return K*i+1; }
136

	
137
    bool less(const Pair &p1, const Pair &p2) const {
138
      return _comp(p1.second, p2.second);
139
    }
140

	
141
    void bubbleUp(int hole, Pair p) {
142
      int par = parent(hole);
143
      while( hole>0 && less(p,_data[par]) ) {
144
        move(_data[par],hole);
145
        hole = par;
146
        par = parent(hole);
147
      }
148
      move(p, hole);
149
    }
150

	
151
    void bubbleDown(int hole, Pair p, int length) {
152
      if( length>1 ) {
153
        int child = firstChild(hole);
154
        while( child+K<=length ) {
155
          int min=child;
156
          for (int i=1; i<K; ++i) {
157
            if( less(_data[child+i], _data[min]) )
158
              min=child+i;
159
          }
160
          if( !less(_data[min], p) )
161
            goto ok;
162
          move(_data[min], hole);
163
          hole = min;
164
          child = firstChild(hole);
165
        }
166
        if ( child<length ) {
167
          int min = child;
168
          while (++child < length) {
169
            if( less(_data[child], _data[min]) )
170
              min=child;
171
          }
172
          if( less(_data[min], p) ) {
173
            move(_data[min], hole);
174
            hole = min;
175
          }
176
        }
177
      }
178
    ok:
179
      move(p, hole);
180
    }
181

	
182
    void move(const Pair &p, int i) {
183
      _data[i] = p;
184
      _iim.set(p.first, i);
185
    }
186

	
187
  public:
188
    /// \brief Insert a pair of item and priority into the heap.
189
    ///
190
    /// This function inserts \c p.first to the heap with priority
191
    /// \c p.second.
192
    /// \param p The pair to insert.
193
    /// \pre \c p.first must not be stored in the heap.
194
    void push(const Pair &p) {
195
      int n = _data.size();
196
      _data.resize(n+1);
197
      bubbleUp(n, p);
198
    }
199

	
200
    /// \brief Insert an item into the heap with the given priority.
201
    ///
202
    /// This function inserts the given item into the heap with the
203
    /// given priority.
204
    /// \param i The item to insert.
205
    /// \param p The priority of the item.
206
    /// \pre \e i must not be stored in the heap.
207
    void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
208

	
209
    /// \brief Return the item having minimum priority.
210
    ///
211
    /// This function returns the item having minimum priority.
212
    /// \pre The heap must be non-empty.
213
    Item top() const { return _data[0].first; }
214

	
215
    /// \brief The minimum priority.
216
    ///
217
    /// This function returns the minimum priority.
218
    /// \pre The heap must be non-empty.
219
    Prio prio() const { return _data[0].second; }
220

	
221
    /// \brief Remove the item having minimum priority.
222
    ///
223
    /// This function removes the item having minimum priority.
224
    /// \pre The heap must be non-empty.
225
    void pop() {
226
      int n = _data.size()-1;
227
      _iim.set(_data[0].first, POST_HEAP);
228
      if (n>0) bubbleDown(0, _data[n], n);
229
      _data.pop_back();
230
    }
231

	
232
    /// \brief Remove the given item from the heap.
233
    ///
234
    /// This function removes the given item from the heap if it is
235
    /// already stored.
236
    /// \param i The item to delete.
237
    /// \pre \e i must be in the heap.
238
    void erase(const Item &i) {
239
      int h = _iim[i];
240
      int n = _data.size()-1;
241
      _iim.set(_data[h].first, POST_HEAP);
242
      if( h<n ) {
243
        if( less(_data[parent(h)], _data[n]) )
244
          bubbleDown(h, _data[n], n);
245
        else
246
          bubbleUp(h, _data[n]);
247
      }
248
      _data.pop_back();
249
    }
250

	
251
    /// \brief The priority of the given item.
252
    ///
253
    /// This function returns the priority of the given item.
254
    /// \param i The item.
255
    /// \pre \e i must be in the heap.
256
    Prio operator[](const Item &i) const {
257
      int idx = _iim[i];
258
      return _data[idx].second;
259
    }
260

	
261
    /// \brief Set the priority of an item or insert it, if it is
262
    /// not stored in the heap.
263
    ///
264
    /// This method sets the priority of the given item if it is
265
    /// already stored in the heap. Otherwise it inserts the given
266
    /// item into the heap with the given priority.
267
    /// \param i The item.
268
    /// \param p The priority.
269
    void set(const Item &i, const Prio &p) {
270
      int idx = _iim[i];
271
      if( idx<0 )
272
        push(i,p);
273
      else if( _comp(p, _data[idx].second) )
274
        bubbleUp(idx, Pair(i,p));
275
      else
276
        bubbleDown(idx, Pair(i,p), _data.size());
277
    }
278

	
279
    /// \brief Decrease the priority of an item to the given value.
280
    ///
281
    /// This function decreases the priority of an item to the given value.
282
    /// \param i The item.
283
    /// \param p The priority.
284
    /// \pre \e i must be stored in the heap with priority at least \e p.
285
    void decrease(const Item &i, const Prio &p) {
286
      int idx = _iim[i];
287
      bubbleUp(idx, Pair(i,p));
288
    }
289

	
290
    /// \brief Increase the priority of an item to the given value.
291
    ///
292
    /// This function increases the priority of an item to the given value.
293
    /// \param i The item.
294
    /// \param p The priority.
295
    /// \pre \e i must be stored in the heap with priority at most \e p.
296
    void increase(const Item &i, const Prio &p) {
297
      int idx = _iim[i];
298
      bubbleDown(idx, Pair(i,p), _data.size());
299
    }
300

	
301
    /// \brief Return the state of an item.
302
    ///
303
    /// This method returns \c PRE_HEAP if the given item has never
304
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
305
    /// and \c POST_HEAP otherwise.
306
    /// In the latter case it is possible that the item will get back
307
    /// to the heap again.
308
    /// \param i The item.
309
    State state(const Item &i) const {
310
      int s = _iim[i];
311
      if (s>=0) s=0;
312
      return State(s);
313
    }
314

	
315
    /// \brief Set the state of an item in the heap.
316
    ///
317
    /// This function sets the state of the given item in the heap.
318
    /// It can be used to manually clear the heap when it is important
319
    /// to achive better time complexity.
320
    /// \param i The item.
321
    /// \param st The state. It should not be \c IN_HEAP.
322
    void state(const Item& i, State st) {
323
      switch (st) {
324
        case POST_HEAP:
325
        case PRE_HEAP:
326
          if (state(i) == IN_HEAP) erase(i);
327
          _iim[i] = st;
328
          break;
329
        case IN_HEAP:
330
          break;
331
      }
332
    }
333

	
334
    /// \brief Replace an item in the heap.
335
    ///
336
    /// This function replaces item \c i with item \c j.
337
    /// Item \c i must be in the heap, while \c j must be out of the heap.
338
    /// After calling this method, item \c i will be out of the
339
    /// heap and \c j will be in the heap with the same prioriority
340
    /// as item \c i had before.
341
    void replace(const Item& i, const Item& j) {
342
      int idx=_iim[i];
343
      _iim.set(i, _iim[j]);
344
      _iim.set(j, idx);
345
      _data[idx].first=j;
346
    }
347

	
348
  }; // class KaryHeap
349

	
350
} // namespace lemon
351

	
352
#endif // LEMON_KARY_HEAP_H
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_MIN_COST_ARBORESCENCE_H
20
#define LEMON_MIN_COST_ARBORESCENCE_H
21

	
22
///\ingroup spantree
23
///\file
24
///\brief Minimum Cost Arborescence algorithm.
25

	
26
#include <vector>
27

	
28
#include <lemon/list_graph.h>
29
#include <lemon/bin_heap.h>
30
#include <lemon/assert.h>
31

	
32
namespace lemon {
33

	
34

	
35
  /// \brief Default traits class for MinCostArborescence class.
36
  ///
37
  /// Default traits class for MinCostArborescence class.
38
  /// \param GR Digraph type.
39
  /// \param CM Type of the cost map.
40
  template <class GR, class CM>
41
  struct MinCostArborescenceDefaultTraits{
42

	
43
    /// \brief The digraph type the algorithm runs on.
44
    typedef GR Digraph;
45

	
46
    /// \brief The type of the map that stores the arc costs.
47
    ///
48
    /// The type of the map that stores the arc costs.
49
    /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
50
    typedef CM CostMap;
51

	
52
    /// \brief The value type of the costs.
53
    ///
54
    /// The value type of the costs.
55
    typedef typename CostMap::Value Value;
56

	
57
    /// \brief The type of the map that stores which arcs are in the
58
    /// arborescence.
59
    ///
60
    /// The type of the map that stores which arcs are in the
61
    /// arborescence.  It must conform to the \ref concepts::WriteMap
62
    /// "WriteMap" concept, and its value type must be \c bool
63
    /// (or convertible). Initially it will be set to \c false on each
64
    /// arc, then it will be set on each arborescence arc once.
65
    typedef typename Digraph::template ArcMap<bool> ArborescenceMap;
66

	
67
    /// \brief Instantiates a \c ArborescenceMap.
68
    ///
69
    /// This function instantiates a \c ArborescenceMap.
70
    /// \param digraph The digraph to which we would like to calculate
71
    /// the \c ArborescenceMap.
72
    static ArborescenceMap *createArborescenceMap(const Digraph &digraph){
73
      return new ArborescenceMap(digraph);
74
    }
75

	
76
    /// \brief The type of the \c PredMap
77
    ///
78
    /// The type of the \c PredMap. It must confrom to the
79
    /// \ref concepts::WriteMap "WriteMap" concept, and its value type
80
    /// must be the \c Arc type of the digraph.
81
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
82

	
83
    /// \brief Instantiates a \c PredMap.
84
    ///
85
    /// This function instantiates a \c PredMap.
86
    /// \param digraph The digraph to which we would like to define the
87
    /// \c PredMap.
88
    static PredMap *createPredMap(const Digraph &digraph){
89
      return new PredMap(digraph);
90
    }
91

	
92
  };
93

	
94
  /// \ingroup spantree
95
  ///
96
  /// \brief Minimum Cost Arborescence algorithm class.
97
  ///
98
  /// This class provides an efficient implementation of the
99
  /// Minimum Cost Arborescence algorithm. The arborescence is a tree
100
  /// which is directed from a given source node of the digraph. One or
101
  /// more sources should be given to the algorithm and it will calculate
102
  /// the minimum cost subgraph that is the union of arborescences with the
103
  /// given sources and spans all the nodes which are reachable from the
104
  /// sources. The time complexity of the algorithm is O(n<sup>2</sup>+e).
105
  ///
106
  /// The algorithm also provides an optimal dual solution, therefore
107
  /// the optimality of the solution can be checked.
108
  ///
109
  /// \param GR The digraph type the algorithm runs on.
110
  /// \param CM A read-only arc map storing the costs of the
111
  /// arcs. It is read once for each arc, so the map may involve in
112
  /// relatively time consuming process to compute the arc costs if
113
  /// it is necessary. The default map type is \ref
114
  /// concepts::Digraph::ArcMap "Digraph::ArcMap<int>".
115
  /// \param TR Traits class to set various data types used
116
  /// by the algorithm. The default traits class is
117
  /// \ref MinCostArborescenceDefaultTraits
118
  /// "MinCostArborescenceDefaultTraits<GR, CM>".
119
#ifndef DOXYGEN
120
  template <typename GR,
121
            typename CM = typename GR::template ArcMap<int>,
122
            typename TR =
123
              MinCostArborescenceDefaultTraits<GR, CM> >
124
#else
125
  template <typename GR, typename CM, typedef TR>
126
#endif
127
  class MinCostArborescence {
128
  public:
129

	
130
    /// \brief The \ref MinCostArborescenceDefaultTraits "traits class" 
131
    /// of the algorithm. 
132
    typedef TR Traits;
133
    /// The type of the underlying digraph.
134
    typedef typename Traits::Digraph Digraph;
135
    /// The type of the map that stores the arc costs.
136
    typedef typename Traits::CostMap CostMap;
137
    ///The type of the costs of the arcs.
138
    typedef typename Traits::Value Value;
139
    ///The type of the predecessor map.
140
    typedef typename Traits::PredMap PredMap;
141
    ///The type of the map that stores which arcs are in the arborescence.
142
    typedef typename Traits::ArborescenceMap ArborescenceMap;
143

	
144
    typedef MinCostArborescence Create;
145

	
146
  private:
147

	
148
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
149

	
150
    struct CostArc {
151

	
152
      Arc arc;
153
      Value value;
154

	
155
      CostArc() {}
156
      CostArc(Arc _arc, Value _value) : arc(_arc), value(_value) {}
157

	
158
    };
159

	
160
    const Digraph *_digraph;
161
    const CostMap *_cost;
162

	
163
    PredMap *_pred;
164
    bool local_pred;
165

	
166
    ArborescenceMap *_arborescence;
167
    bool local_arborescence;
168

	
169
    typedef typename Digraph::template ArcMap<int> ArcOrder;
170
    ArcOrder *_arc_order;
171

	
172
    typedef typename Digraph::template NodeMap<int> NodeOrder;
173
    NodeOrder *_node_order;
174

	
175
    typedef typename Digraph::template NodeMap<CostArc> CostArcMap;
176
    CostArcMap *_cost_arcs;
177

	
178
    struct StackLevel {
179

	
180
      std::vector<CostArc> arcs;
181
      int node_level;
182

	
183
    };
184

	
185
    std::vector<StackLevel> level_stack;
186
    std::vector<Node> queue;
187

	
188
    typedef std::vector<typename Digraph::Node> DualNodeList;
189

	
190
    DualNodeList _dual_node_list;
191

	
192
    struct DualVariable {
193
      int begin, end;
194
      Value value;
195

	
196
      DualVariable(int _begin, int _end, Value _value)
197
        : begin(_begin), end(_end), value(_value) {}
198

	
199
    };
200

	
201
    typedef std::vector<DualVariable> DualVariables;
202

	
203
    DualVariables _dual_variables;
204

	
205
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
206

	
207
    HeapCrossRef *_heap_cross_ref;
208

	
209
    typedef BinHeap<int, HeapCrossRef> Heap;
210

	
211
    Heap *_heap;
212

	
213
  protected:
214

	
215
    MinCostArborescence() {}
216

	
217
  private:
218

	
219
    void createStructures() {
220
      if (!_pred) {
221
        local_pred = true;
222
        _pred = Traits::createPredMap(*_digraph);
223
      }
224
      if (!_arborescence) {
225
        local_arborescence = true;
226
        _arborescence = Traits::createArborescenceMap(*_digraph);
227
      }
228
      if (!_arc_order) {
229
        _arc_order = new ArcOrder(*_digraph);
230
      }
231
      if (!_node_order) {
232
        _node_order = new NodeOrder(*_digraph);
233
      }
234
      if (!_cost_arcs) {
235
        _cost_arcs = new CostArcMap(*_digraph);
236
      }
237
      if (!_heap_cross_ref) {
238
        _heap_cross_ref = new HeapCrossRef(*_digraph, -1);
239
      }
240
      if (!_heap) {
241
        _heap = new Heap(*_heap_cross_ref);
242
      }
243
    }
244

	
245
    void destroyStructures() {
246
      if (local_arborescence) {
247
        delete _arborescence;
248
      }
249
      if (local_pred) {
250
        delete _pred;
251
      }
252
      if (_arc_order) {
253
        delete _arc_order;
254
      }
255
      if (_node_order) {
256
        delete _node_order;
257
      }
258
      if (_cost_arcs) {
259
        delete _cost_arcs;
260
      }
261
      if (_heap) {
262
        delete _heap;
263
      }
264
      if (_heap_cross_ref) {
265
        delete _heap_cross_ref;
266
      }
267
    }
268

	
269
    Arc prepare(Node node) {
270
      std::vector<Node> nodes;
271
      (*_node_order)[node] = _dual_node_list.size();
272
      StackLevel level;
273
      level.node_level = _dual_node_list.size();
274
      _dual_node_list.push_back(node);
275
      for (InArcIt it(*_digraph, node); it != INVALID; ++it) {
276
        Arc arc = it;
277
        Node source = _digraph->source(arc);
278
        Value value = (*_cost)[it];
279
        if (source == node || (*_node_order)[source] == -3) continue;
280
        if ((*_cost_arcs)[source].arc == INVALID) {
281
          (*_cost_arcs)[source].arc = arc;
282
          (*_cost_arcs)[source].value = value;
283
          nodes.push_back(source);
284
        } else {
285
          if ((*_cost_arcs)[source].value > value) {
286
            (*_cost_arcs)[source].arc = arc;
287
            (*_cost_arcs)[source].value = value;
288
          }
289
        }
290
      }
291
      CostArc minimum = (*_cost_arcs)[nodes[0]];
292
      for (int i = 1; i < int(nodes.size()); ++i) {
293
        if ((*_cost_arcs)[nodes[i]].value < minimum.value) {
294
          minimum = (*_cost_arcs)[nodes[i]];
295
        }
296
      }
297
      (*_arc_order)[minimum.arc] = _dual_variables.size();
298
      DualVariable var(_dual_node_list.size() - 1,
299
                       _dual_node_list.size(), minimum.value);
300
      _dual_variables.push_back(var);
301
      for (int i = 0; i < int(nodes.size()); ++i) {
302
        (*_cost_arcs)[nodes[i]].value -= minimum.value;
303
        level.arcs.push_back((*_cost_arcs)[nodes[i]]);
304
        (*_cost_arcs)[nodes[i]].arc = INVALID;
305
      }
306
      level_stack.push_back(level);
307
      return minimum.arc;
308
    }
309

	
310
    Arc contract(Node node) {
311
      int node_bottom = bottom(node);
312
      std::vector<Node> nodes;
313
      while (!level_stack.empty() &&
314
             level_stack.back().node_level >= node_bottom) {
315
        for (int i = 0; i < int(level_stack.back().arcs.size()); ++i) {
316
          Arc arc = level_stack.back().arcs[i].arc;
317
          Node source = _digraph->source(arc);
318
          Value value = level_stack.back().arcs[i].value;
319
          if ((*_node_order)[source] >= node_bottom) continue;
320
          if ((*_cost_arcs)[source].arc == INVALID) {
321
            (*_cost_arcs)[source].arc = arc;
322
            (*_cost_arcs)[source].value = value;
323
            nodes.push_back(source);
324
          } else {
325
            if ((*_cost_arcs)[source].value > value) {
326
              (*_cost_arcs)[source].arc = arc;
327
              (*_cost_arcs)[source].value = value;
328
            }
329
          }
330
        }
331
        level_stack.pop_back();
332
      }
333
      CostArc minimum = (*_cost_arcs)[nodes[0]];
334
      for (int i = 1; i < int(nodes.size()); ++i) {
335
        if ((*_cost_arcs)[nodes[i]].value < minimum.value) {
336
          minimum = (*_cost_arcs)[nodes[i]];
337
        }
338
      }
339
      (*_arc_order)[minimum.arc] = _dual_variables.size();
340
      DualVariable var(node_bottom, _dual_node_list.size(), minimum.value);
341
      _dual_variables.push_back(var);
342
      StackLevel level;
343
      level.node_level = node_bottom;
344
      for (int i = 0; i < int(nodes.size()); ++i) {
345
        (*_cost_arcs)[nodes[i]].value -= minimum.value;
346
        level.arcs.push_back((*_cost_arcs)[nodes[i]]);
347
        (*_cost_arcs)[nodes[i]].arc = INVALID;
348
      }
349
      level_stack.push_back(level);
350
      return minimum.arc;
351
    }
352

	
353
    int bottom(Node node) {
354
      int k = level_stack.size() - 1;
355
      while (level_stack[k].node_level > (*_node_order)[node]) {
356
        --k;
357
      }
358
      return level_stack[k].node_level;
359
    }
360

	
361
    void finalize(Arc arc) {
362
      Node node = _digraph->target(arc);
363
      _heap->push(node, (*_arc_order)[arc]);
364
      _pred->set(node, arc);
365
      while (!_heap->empty()) {
366
        Node source = _heap->top();
367
        _heap->pop();
368
        (*_node_order)[source] = -1;
369
        for (OutArcIt it(*_digraph, source); it != INVALID; ++it) {
370
          if ((*_arc_order)[it] < 0) continue;
371
          Node target = _digraph->target(it);
372
          switch(_heap->state(target)) {
373
          case Heap::PRE_HEAP:
374
            _heap->push(target, (*_arc_order)[it]);
375
            _pred->set(target, it);
376
            break;
377
          case Heap::IN_HEAP:
378
            if ((*_arc_order)[it] < (*_heap)[target]) {
379
              _heap->decrease(target, (*_arc_order)[it]);
380
              _pred->set(target, it);
381
            }
382
            break;
383
          case Heap::POST_HEAP:
384
            break;
385
          }
386
        }
387
        _arborescence->set((*_pred)[source], true);
388
      }
389
    }
390

	
391

	
392
  public:
393

	
394
    /// \name Named Template Parameters
395

	
396
    /// @{
397

	
398
    template <class T>
399
    struct SetArborescenceMapTraits : public Traits {
400
      typedef T ArborescenceMap;
401
      static ArborescenceMap *createArborescenceMap(const Digraph &)
402
      {
403
        LEMON_ASSERT(false, "ArborescenceMap is not initialized");
404
        return 0; // ignore warnings
405
      }
406
    };
407

	
408
    /// \brief \ref named-templ-param "Named parameter" for
409
    /// setting \c ArborescenceMap type
410
    ///
411
    /// \ref named-templ-param "Named parameter" for setting
412
    /// \c ArborescenceMap type.
413
    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept,
414
    /// and its value type must be \c bool (or convertible).
415
    /// Initially it will be set to \c false on each arc,
416
    /// then it will be set on each arborescence arc once.
417
    template <class T>
418
    struct SetArborescenceMap
419
      : public MinCostArborescence<Digraph, CostMap,
420
                                   SetArborescenceMapTraits<T> > {
421
    };
422

	
423
    template <class T>
424
    struct SetPredMapTraits : public Traits {
425
      typedef T PredMap;
426
      static PredMap *createPredMap(const Digraph &)
427
      {
428
        LEMON_ASSERT(false, "PredMap is not initialized");
429
        return 0; // ignore warnings
430
      }
431
    };
432

	
433
    /// \brief \ref named-templ-param "Named parameter" for
434
    /// setting \c PredMap type
435
    ///
436
    /// \ref named-templ-param "Named parameter" for setting
437
    /// \c PredMap type.
438
    /// It must meet the \ref concepts::WriteMap "WriteMap" concept, 
439
    /// and its value type must be the \c Arc type of the digraph.
440
    template <class T>
441
    struct SetPredMap
442
      : public MinCostArborescence<Digraph, CostMap, SetPredMapTraits<T> > {
443
    };
444

	
445
    /// @}
446

	
447
    /// \brief Constructor.
448
    ///
449
    /// \param digraph The digraph the algorithm will run on.
450
    /// \param cost The cost map used by the algorithm.
451
    MinCostArborescence(const Digraph& digraph, const CostMap& cost)
452
      : _digraph(&digraph), _cost(&cost), _pred(0), local_pred(false),
453
        _arborescence(0), local_arborescence(false),
454
        _arc_order(0), _node_order(0), _cost_arcs(0),
455
        _heap_cross_ref(0), _heap(0) {}
456

	
457
    /// \brief Destructor.
458
    ~MinCostArborescence() {
459
      destroyStructures();
460
    }
461

	
462
    /// \brief Sets the arborescence map.
463
    ///
464
    /// Sets the arborescence map.
465
    /// \return <tt>(*this)</tt>
466
    MinCostArborescence& arborescenceMap(ArborescenceMap& m) {
467
      if (local_arborescence) {
468
        delete _arborescence;
469
      }
470
      local_arborescence = false;
471
      _arborescence = &m;
472
      return *this;
473
    }
474

	
475
    /// \brief Sets the predecessor map.
476
    ///
477
    /// Sets the predecessor map.
478
    /// \return <tt>(*this)</tt>
479
    MinCostArborescence& predMap(PredMap& m) {
480
      if (local_pred) {
481
        delete _pred;
482
      }
483
      local_pred = false;
484
      _pred = &m;
485
      return *this;
486
    }
487

	
488
    /// \name Execution Control
489
    /// The simplest way to execute the algorithm is to use
490
    /// one of the member functions called \c run(...). \n
491
    /// If you need better control on the execution,
492
    /// you have to call \ref init() first, then you can add several
493
    /// source nodes with \ref addSource().
494
    /// Finally \ref start() will perform the arborescence
495
    /// computation.
496

	
497
    ///@{
498

	
499
    /// \brief Initializes the internal data structures.
500
    ///
501
    /// Initializes the internal data structures.
502
    ///
503
    void init() {
504
      createStructures();
505
      _heap->clear();
506
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
507
        (*_cost_arcs)[it].arc = INVALID;
508
        (*_node_order)[it] = -3;
509
        (*_heap_cross_ref)[it] = Heap::PRE_HEAP;
510
        _pred->set(it, INVALID);
511
      }
512
      for (ArcIt it(*_digraph); it != INVALID; ++it) {
513
        _arborescence->set(it, false);
514
        (*_arc_order)[it] = -1;
515
      }
516
      _dual_node_list.clear();
517
      _dual_variables.clear();
518
    }
519

	
520
    /// \brief Adds a new source node.
521
    ///
522
    /// Adds a new source node to the algorithm.
523
    void addSource(Node source) {
524
      std::vector<Node> nodes;
525
      nodes.push_back(source);
526
      while (!nodes.empty()) {
527
        Node node = nodes.back();
528
        nodes.pop_back();
529
        for (OutArcIt it(*_digraph, node); it != INVALID; ++it) {
530
          Node target = _digraph->target(it);
531
          if ((*_node_order)[target] == -3) {
532
            (*_node_order)[target] = -2;
533
            nodes.push_back(target);
534
            queue.push_back(target);
535
          }
536
        }
537
      }
538
      (*_node_order)[source] = -1;
539
    }
540

	
541
    /// \brief Processes the next node in the priority queue.
542
    ///
543
    /// Processes the next node in the priority queue.
544
    ///
545
    /// \return The processed node.
546
    ///
547
    /// \warning The queue must not be empty.
548
    Node processNextNode() {
549
      Node node = queue.back();
550
      queue.pop_back();
551
      if ((*_node_order)[node] == -2) {
552
        Arc arc = prepare(node);
553
        Node source = _digraph->source(arc);
554
        while ((*_node_order)[source] != -1) {
555
          if ((*_node_order)[source] >= 0) {
556
            arc = contract(source);
557
          } else {
558
            arc = prepare(source);
559
          }
560
          source = _digraph->source(arc);
561
        }
562
        finalize(arc);
563
        level_stack.clear();
564
      }
565
      return node;
566
    }
567

	
568
    /// \brief Returns the number of the nodes to be processed.
569
    ///
570
    /// Returns the number of the nodes to be processed in the priority
571
    /// queue.
572
    int queueSize() const {
573
      return queue.size();
574
    }
575

	
576
    /// \brief Returns \c false if there are nodes to be processed.
577
    ///
578
    /// Returns \c false if there are nodes to be processed.
579
    bool emptyQueue() const {
580
      return queue.empty();
581
    }
582

	
583
    /// \brief Executes the algorithm.
584
    ///
585
    /// Executes the algorithm.
586
    ///
587
    /// \pre init() must be called and at least one node should be added
588
    /// with addSource() before using this function.
589
    ///
590
    ///\note mca.start() is just a shortcut of the following code.
591
    ///\code
592
    ///while (!mca.emptyQueue()) {
593
    ///  mca.processNextNode();
594
    ///}
595
    ///\endcode
596
    void start() {
597
      while (!emptyQueue()) {
598
        processNextNode();
599
      }
600
    }
601

	
602
    /// \brief Runs %MinCostArborescence algorithm from node \c s.
603
    ///
604
    /// This method runs the %MinCostArborescence algorithm from
605
    /// a root node \c s.
606
    ///
607
    /// \note mca.run(s) is just a shortcut of the following code.
608
    /// \code
609
    /// mca.init();
610
    /// mca.addSource(s);
611
    /// mca.start();
612
    /// \endcode
613
    void run(Node s) {
614
      init();
615
      addSource(s);
616
      start();
617
    }
618

	
619
    ///@}
620

	
621
    /// \name Query Functions
622
    /// The result of the %MinCostArborescence algorithm can be obtained
623
    /// using these functions.\n
624
    /// Either run() or start() must be called before using them.
625

	
626
    /// @{
627

	
628
    /// \brief Returns the cost of the arborescence.
629
    ///
630
    /// Returns the cost of the arborescence.
631
    Value arborescenceCost() const {
632
      Value sum = 0;
633
      for (ArcIt it(*_digraph); it != INVALID; ++it) {
634
        if (arborescence(it)) {
635
          sum += (*_cost)[it];
636
        }
637
      }
638
      return sum;
639
    }
640

	
641
    /// \brief Returns \c true if the arc is in the arborescence.
642
    ///
643
    /// Returns \c true if the given arc is in the arborescence.
644
    /// \param arc An arc of the digraph.
645
    /// \pre \ref run() must be called before using this function.
646
    bool arborescence(Arc arc) const {
647
      return (*_pred)[_digraph->target(arc)] == arc;
648
    }
649

	
650
    /// \brief Returns a const reference to the arborescence map.
651
    ///
652
    /// Returns a const reference to the arborescence map.
653
    /// \pre \ref run() must be called before using this function.
654
    const ArborescenceMap& arborescenceMap() const {
655
      return *_arborescence;
656
    }
657

	
658
    /// \brief Returns the predecessor arc of the given node.
659
    ///
660
    /// Returns the predecessor arc of the given node.
661
    /// \pre \ref run() must be called before using this function.
662
    Arc pred(Node node) const {
663
      return (*_pred)[node];
664
    }
665

	
666
    /// \brief Returns a const reference to the pred map.
667
    ///
668
    /// Returns a const reference to the pred map.
669
    /// \pre \ref run() must be called before using this function.
670
    const PredMap& predMap() const {
671
      return *_pred;
672
    }
673

	
674
    /// \brief Indicates that a node is reachable from the sources.
675
    ///
676
    /// Indicates that a node is reachable from the sources.
677
    bool reached(Node node) const {
678
      return (*_node_order)[node] != -3;
679
    }
680

	
681
    /// \brief Indicates that a node is processed.
682
    ///
683
    /// Indicates that a node is processed. The arborescence path exists
684
    /// from the source to the given node.
685
    bool processed(Node node) const {
686
      return (*_node_order)[node] == -1;
687
    }
688

	
689
    /// \brief Returns the number of the dual variables in basis.
690
    ///
691
    /// Returns the number of the dual variables in basis.
692
    int dualNum() const {
693
      return _dual_variables.size();
694
    }
695

	
696
    /// \brief Returns the value of the dual solution.
697
    ///
698
    /// Returns the value of the dual solution. It should be
699
    /// equal to the arborescence value.
700
    Value dualValue() const {
701
      Value sum = 0;
702
      for (int i = 0; i < int(_dual_variables.size()); ++i) {
703
        sum += _dual_variables[i].value;
704
      }
705
      return sum;
706
    }
707

	
708
    /// \brief Returns the number of the nodes in the dual variable.
709
    ///
710
    /// Returns the number of the nodes in the dual variable.
711
    int dualSize(int k) const {
712
      return _dual_variables[k].end - _dual_variables[k].begin;
713
    }
714

	
715
    /// \brief Returns the value of the dual variable.
716
    ///
717
    /// Returns the the value of the dual variable.
718
    Value dualValue(int k) const {
719
      return _dual_variables[k].value;
720
    }
721

	
722
    /// \brief LEMON iterator for getting a dual variable.
723
    ///
724
    /// This class provides a common style LEMON iterator for getting a
725
    /// dual variable of \ref MinCostArborescence algorithm.
726
    /// It iterates over a subset of the nodes.
727
    class DualIt {
728
    public:
729

	
730
      /// \brief Constructor.
731
      ///
732
      /// Constructor for getting the nodeset of the dual variable
733
      /// of \ref MinCostArborescence algorithm.
734
      DualIt(const MinCostArborescence& algorithm, int variable)
735
        : _algorithm(&algorithm)
736
      {
737
        _index = _algorithm->_dual_variables[variable].begin;
738
        _last = _algorithm->_dual_variables[variable].end;
739
      }
740

	
741
      /// \brief Conversion to \c Node.
742
      ///
743
      /// Conversion to \c Node.
744
      operator Node() const {
745
        return _algorithm->_dual_node_list[_index];
746
      }
747

	
748
      /// \brief Increment operator.
749
      ///
750
      /// Increment operator.
751
      DualIt& operator++() {
752
        ++_index;
753
        return *this;
754
      }
755

	
756
      /// \brief Validity checking
757
      ///
758
      /// Checks whether the iterator is invalid.
759
      bool operator==(Invalid) const {
760
        return _index == _last;
761
      }
762

	
763
      /// \brief Validity checking
764
      ///
765
      /// Checks whether the iterator is valid.
766
      bool operator!=(Invalid) const {
767
        return _index != _last;
768
      }
769

	
770
    private:
771
      const MinCostArborescence* _algorithm;
772
      int _index, _last;
773
    };
774

	
775
    /// @}
776

	
777
  };
778

	
779
  /// \ingroup spantree
780
  ///
781
  /// \brief Function type interface for MinCostArborescence algorithm.
782
  ///
783
  /// Function type interface for MinCostArborescence algorithm.
784
  /// \param digraph The digraph the algorithm runs on.
785
  /// \param cost An arc map storing the costs.
786
  /// \param source The source node of the arborescence.
787
  /// \retval arborescence An arc map with \c bool (or convertible) value
788
  /// type that stores the arborescence.
789
  /// \return The total cost of the arborescence.
790
  ///
791
  /// \sa MinCostArborescence
792
  template <typename Digraph, typename CostMap, typename ArborescenceMap>
793
  typename CostMap::Value minCostArborescence(const Digraph& digraph,
794
                                              const CostMap& cost,
795
                                              typename Digraph::Node source,
796
                                              ArborescenceMap& arborescence) {
797
    typename MinCostArborescence<Digraph, CostMap>
798
      ::template SetArborescenceMap<ArborescenceMap>
799
      ::Create mca(digraph, cost);
800
    mca.arborescenceMap(arborescence);
801
    mca.run(source);
802
    return mca.arborescenceCost();
803
  }
804

	
805
}
806

	
807
#endif
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_NETWORK_SIMPLEX_H
20
#define LEMON_NETWORK_SIMPLEX_H
21

	
22
/// \ingroup min_cost_flow_algs
23
///
24
/// \file
25
/// \brief Network Simplex algorithm for finding a minimum cost flow.
26

	
27
#include <vector>
28
#include <limits>
29
#include <algorithm>
30

	
31
#include <lemon/core.h>
32
#include <lemon/math.h>
33

	
34
namespace lemon {
35

	
36
  /// \addtogroup min_cost_flow_algs
37
  /// @{
38

	
39
  /// \brief Implementation of the primal Network Simplex algorithm
40
  /// for finding a \ref min_cost_flow "minimum cost flow".
41
  ///
42
  /// \ref NetworkSimplex implements the primal Network Simplex algorithm
43
  /// for finding a \ref min_cost_flow "minimum cost flow"
44
  /// \ref amo93networkflows, \ref dantzig63linearprog,
45
  /// \ref kellyoneill91netsimplex.
46
  /// This algorithm is a specialized version of the linear programming
47
  /// simplex method directly for the minimum cost flow problem.
48
  /// It is one of the most efficient solution methods.
49
  ///
50
  /// In general this class is the fastest implementation available
51
  /// in LEMON for the minimum cost flow problem.
52
  /// Moreover it supports both directions of the supply/demand inequality
53
  /// constraints. For more information see \ref SupplyType.
54
  ///
55
  /// Most of the parameters of the problem (except for the digraph)
56
  /// can be given using separate functions, and the algorithm can be
57
  /// executed using the \ref run() function. If some parameters are not
58
  /// specified, then default values will be used.
59
  ///
60
  /// \tparam GR The digraph type the algorithm runs on.
61
  /// \tparam V The value type used for flow amounts, capacity bounds
62
  /// and supply values in the algorithm. By default it is \c int.
63
  /// \tparam C The value type used for costs and potentials in the
64
  /// algorithm. By default it is the same as \c V.
65
  ///
66
  /// \warning Both value types must be signed and all input data must
67
  /// be integer.
68
  ///
69
  /// \note %NetworkSimplex provides five different pivot rule
70
  /// implementations, from which the most efficient one is used
71
  /// by default. For more information see \ref PivotRule.
72
  template <typename GR, typename V = int, typename C = V>
73
  class NetworkSimplex
74
  {
75
  public:
76

	
77
    /// The type of the flow amounts, capacity bounds and supply values
78
    typedef V Value;
79
    /// The type of the arc costs
80
    typedef C Cost;
81

	
82
  public:
83

	
84
    /// \brief Problem type constants for the \c run() function.
85
    ///
86
    /// Enum type containing the problem type constants that can be
87
    /// returned by the \ref run() function of the algorithm.
88
    enum ProblemType {
89
      /// The problem has no feasible solution (flow).
90
      INFEASIBLE,
91
      /// The problem has optimal solution (i.e. it is feasible and
92
      /// bounded), and the algorithm has found optimal flow and node
93
      /// potentials (primal and dual solutions).
94
      OPTIMAL,
95
      /// The objective function of the problem is unbounded, i.e.
96
      /// there is a directed cycle having negative total cost and
97
      /// infinite upper bound.
98
      UNBOUNDED
99
    };
100
    
101
    /// \brief Constants for selecting the type of the supply constraints.
102
    ///
103
    /// Enum type containing constants for selecting the supply type,
104
    /// i.e. the direction of the inequalities in the supply/demand
105
    /// constraints of the \ref min_cost_flow "minimum cost flow problem".
106
    ///
107
    /// The default supply type is \c GEQ, the \c LEQ type can be
108
    /// selected using \ref supplyType().
109
    /// The equality form is a special case of both supply types.
110
    enum SupplyType {
111
      /// This option means that there are <em>"greater or equal"</em>
112
      /// supply/demand constraints in the definition of the problem.
113
      GEQ,
114
      /// This option means that there are <em>"less or equal"</em>
115
      /// supply/demand constraints in the definition of the problem.
116
      LEQ
117
    };
118
    
119
    /// \brief Constants for selecting the pivot rule.
120
    ///
121
    /// Enum type containing constants for selecting the pivot rule for
122
    /// the \ref run() function.
123
    ///
124
    /// \ref NetworkSimplex provides five different pivot rule
125
    /// implementations that significantly affect the running time
126
    /// of the algorithm.
127
    /// By default \ref BLOCK_SEARCH "Block Search" is used, which
128
    /// proved to be the most efficient and the most robust on various
129
    /// test inputs according to our benchmark tests.
130
    /// However another pivot rule can be selected using the \ref run()
131
    /// function with the proper parameter.
132
    enum PivotRule {
133

	
134
      /// The First Eligible pivot rule.
135
      /// The next eligible arc is selected in a wraparound fashion
136
      /// in every iteration.
137
      FIRST_ELIGIBLE,
138

	
139
      /// The Best Eligible pivot rule.
140
      /// The best eligible arc is selected in every iteration.
141
      BEST_ELIGIBLE,
142

	
143
      /// The Block Search pivot rule.
144
      /// A specified number of arcs are examined in every iteration
145
      /// in a wraparound fashion and the best eligible arc is selected
146
      /// from this block.
147
      BLOCK_SEARCH,
148

	
149
      /// The Candidate List pivot rule.
150
      /// In a major iteration a candidate list is built from eligible arcs
151
      /// in a wraparound fashion and in the following minor iterations
152
      /// the best eligible arc is selected from this list.
153
      CANDIDATE_LIST,
154

	
155
      /// The Altering Candidate List pivot rule.
156
      /// It is a modified version of the Candidate List method.
157
      /// It keeps only the several best eligible arcs from the former
158
      /// candidate list and extends this list in every iteration.
159
      ALTERING_LIST
160
    };
161
    
162
  private:
163

	
164
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
165

	
166
    typedef std::vector<int> IntVector;
167
    typedef std::vector<bool> BoolVector;
168
    typedef std::vector<Value> ValueVector;
169
    typedef std::vector<Cost> CostVector;
170

	
171
    // State constants for arcs
172
    enum ArcStateEnum {
173
      STATE_UPPER = -1,
174
      STATE_TREE  =  0,
175
      STATE_LOWER =  1
176
    };
177

	
178
  private:
179

	
180
    // Data related to the underlying digraph
181
    const GR &_graph;
182
    int _node_num;
183
    int _arc_num;
184
    int _all_arc_num;
185
    int _search_arc_num;
186

	
187
    // Parameters of the problem
188
    bool _have_lower;
189
    SupplyType _stype;
190
    Value _sum_supply;
191

	
192
    // Data structures for storing the digraph
193
    IntNodeMap _node_id;
194
    IntArcMap _arc_id;
195
    IntVector _source;
196
    IntVector _target;
197

	
198
    // Node and arc data
199
    ValueVector _lower;
200
    ValueVector _upper;
201
    ValueVector _cap;
202
    CostVector _cost;
203
    ValueVector _supply;
204
    ValueVector _flow;
205
    CostVector _pi;
206

	
207
    // Data for storing the spanning tree structure
208
    IntVector _parent;
209
    IntVector _pred;
210
    IntVector _thread;
211
    IntVector _rev_thread;
212
    IntVector _succ_num;
213
    IntVector _last_succ;
214
    IntVector _dirty_revs;
215
    BoolVector _forward;
216
    IntVector _state;
217
    int _root;
218

	
219
    // Temporary data used in the current pivot iteration
220
    int in_arc, join, u_in, v_in, u_out, v_out;
221
    int first, second, right, last;
222
    int stem, par_stem, new_stem;
223
    Value delta;
224

	
225
  public:
226
  
227
    /// \brief Constant for infinite upper bounds (capacities).
228
    ///
229
    /// Constant for infinite upper bounds (capacities).
230
    /// It is \c std::numeric_limits<Value>::infinity() if available,
231
    /// \c std::numeric_limits<Value>::max() otherwise.
232
    const Value INF;
233

	
234
  private:
235

	
236
    // Implementation of the First Eligible pivot rule
237
    class FirstEligiblePivotRule
238
    {
239
    private:
240

	
241
      // References to the NetworkSimplex class
242
      const IntVector  &_source;
243
      const IntVector  &_target;
244
      const CostVector &_cost;
245
      const IntVector  &_state;
246
      const CostVector &_pi;
247
      int &_in_arc;
248
      int _search_arc_num;
249

	
250
      // Pivot rule data
251
      int _next_arc;
252

	
253
    public:
254

	
255
      // Constructor
256
      FirstEligiblePivotRule(NetworkSimplex &ns) :
257
        _source(ns._source), _target(ns._target),
258
        _cost(ns._cost), _state(ns._state), _pi(ns._pi),
259
        _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num),
260
        _next_arc(0)
261
      {}
262

	
263
      // Find next entering arc
264
      bool findEnteringArc() {
265
        Cost c;
266
        for (int e = _next_arc; e < _search_arc_num; ++e) {
267
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
268
          if (c < 0) {
269
            _in_arc = e;
270
            _next_arc = e + 1;
271
            return true;
272
          }
273
        }
274
        for (int e = 0; e < _next_arc; ++e) {
275
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
276
          if (c < 0) {
277
            _in_arc = e;
278
            _next_arc = e + 1;
279
            return true;
280
          }
281
        }
282
        return false;
283
      }
284

	
285
    }; //class FirstEligiblePivotRule
286

	
287

	
288
    // Implementation of the Best Eligible pivot rule
289
    class BestEligiblePivotRule
290
    {
291
    private:
292

	
293
      // References to the NetworkSimplex class
294
      const IntVector  &_source;
295
      const IntVector  &_target;
296
      const CostVector &_cost;
297
      const IntVector  &_state;
298
      const CostVector &_pi;
299
      int &_in_arc;
300
      int _search_arc_num;
301

	
302
    public:
303

	
304
      // Constructor
305
      BestEligiblePivotRule(NetworkSimplex &ns) :
306
        _source(ns._source), _target(ns._target),
307
        _cost(ns._cost), _state(ns._state), _pi(ns._pi),
308
        _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num)
309
      {}
310

	
311
      // Find next entering arc
312
      bool findEnteringArc() {
313
        Cost c, min = 0;
314
        for (int e = 0; e < _search_arc_num; ++e) {
315
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
316
          if (c < min) {
317
            min = c;
318
            _in_arc = e;
319
          }
320
        }
321
        return min < 0;
322
      }
323

	
324
    }; //class BestEligiblePivotRule
325

	
326

	
327
    // Implementation of the Block Search pivot rule
328
    class BlockSearchPivotRule
329
    {
330
    private:
331

	
332
      // References to the NetworkSimplex class
333
      const IntVector  &_source;
334
      const IntVector  &_target;
335
      const CostVector &_cost;
336
      const IntVector  &_state;
337
      const CostVector &_pi;
338
      int &_in_arc;
339
      int _search_arc_num;
340

	
341
      // Pivot rule data
342
      int _block_size;
343
      int _next_arc;
344

	
345
    public:
346

	
347
      // Constructor
348
      BlockSearchPivotRule(NetworkSimplex &ns) :
349
        _source(ns._source), _target(ns._target),
350
        _cost(ns._cost), _state(ns._state), _pi(ns._pi),
351
        _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num),
352
        _next_arc(0)
353
      {
354
        // The main parameters of the pivot rule
355
        const double BLOCK_SIZE_FACTOR = 0.5;
356
        const int MIN_BLOCK_SIZE = 10;
357

	
358
        _block_size = std::max( int(BLOCK_SIZE_FACTOR *
359
                                    std::sqrt(double(_search_arc_num))),
360
                                MIN_BLOCK_SIZE );
361
      }
362

	
363
      // Find next entering arc
364
      bool findEnteringArc() {
365
        Cost c, min = 0;
366
        int cnt = _block_size;
367
        int e;
368
        for (e = _next_arc; e < _search_arc_num; ++e) {
369
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
370
          if (c < min) {
371
            min = c;
372
            _in_arc = e;
373
          }
374
          if (--cnt == 0) {
375
            if (min < 0) goto search_end;
376
            cnt = _block_size;
377
          }
378
        }
379
        for (e = 0; e < _next_arc; ++e) {
380
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
381
          if (c < min) {
382
            min = c;
383
            _in_arc = e;
384
          }
385
          if (--cnt == 0) {
386
            if (min < 0) goto search_end;
387
            cnt = _block_size;
388
          }
389
        }
390
        if (min >= 0) return false;
391

	
392
      search_end:
393
        _next_arc = e;
394
        return true;
395
      }
396

	
397
    }; //class BlockSearchPivotRule
398

	
399

	
400
    // Implementation of the Candidate List pivot rule
401
    class CandidateListPivotRule
402
    {
403
    private:
404

	
405
      // References to the NetworkSimplex class
406
      const IntVector  &_source;
407
      const IntVector  &_target;
408
      const CostVector &_cost;
409
      const IntVector  &_state;
410
      const CostVector &_pi;
411
      int &_in_arc;
412
      int _search_arc_num;
413

	
414
      // Pivot rule data
415
      IntVector _candidates;
416
      int _list_length, _minor_limit;
417
      int _curr_length, _minor_count;
418
      int _next_arc;
419

	
420
    public:
421

	
422
      /// Constructor
423
      CandidateListPivotRule(NetworkSimplex &ns) :
424
        _source(ns._source), _target(ns._target),
425
        _cost(ns._cost), _state(ns._state), _pi(ns._pi),
426
        _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num),
427
        _next_arc(0)
428
      {
429
        // The main parameters of the pivot rule
430
        const double LIST_LENGTH_FACTOR = 0.25;
431
        const int MIN_LIST_LENGTH = 10;
432
        const double MINOR_LIMIT_FACTOR = 0.1;
433
        const int MIN_MINOR_LIMIT = 3;
434

	
435
        _list_length = std::max( int(LIST_LENGTH_FACTOR *
436
                                     std::sqrt(double(_search_arc_num))),
437
                                 MIN_LIST_LENGTH );
438
        _minor_limit = std::max( int(MINOR_LIMIT_FACTOR * _list_length),
439
                                 MIN_MINOR_LIMIT );
440
        _curr_length = _minor_count = 0;
441
        _candidates.resize(_list_length);
442
      }
443

	
444
      /// Find next entering arc
445
      bool findEnteringArc() {
446
        Cost min, c;
447
        int e;
448
        if (_curr_length > 0 && _minor_count < _minor_limit) {
449
          // Minor iteration: select the best eligible arc from the
450
          // current candidate list
451
          ++_minor_count;
452
          min = 0;
453
          for (int i = 0; i < _curr_length; ++i) {
454
            e = _candidates[i];
455
            c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
456
            if (c < min) {
457
              min = c;
458
              _in_arc = e;
459
            }
460
            else if (c >= 0) {
461
              _candidates[i--] = _candidates[--_curr_length];
462
            }
463
          }
464
          if (min < 0) return true;
465
        }
466

	
467
        // Major iteration: build a new candidate list
468
        min = 0;
469
        _curr_length = 0;
470
        for (e = _next_arc; e < _search_arc_num; ++e) {
471
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
472
          if (c < 0) {
473
            _candidates[_curr_length++] = e;
474
            if (c < min) {
475
              min = c;
476
              _in_arc = e;
477
            }
478
            if (_curr_length == _list_length) goto search_end;
479
          }
480
        }
481
        for (e = 0; e < _next_arc; ++e) {
482
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
483
          if (c < 0) {
484
            _candidates[_curr_length++] = e;
485
            if (c < min) {
486
              min = c;
487
              _in_arc = e;
488
            }
489
            if (_curr_length == _list_length) goto search_end;
490
          }
491
        }
492
        if (_curr_length == 0) return false;
493
      
494
      search_end:        
495
        _minor_count = 1;
496
        _next_arc = e;
497
        return true;
498
      }
499

	
500
    }; //class CandidateListPivotRule
501

	
502

	
503
    // Implementation of the Altering Candidate List pivot rule
504
    class AlteringListPivotRule
505
    {
506
    private:
507

	
508
      // References to the NetworkSimplex class
509
      const IntVector  &_source;
510
      const IntVector  &_target;
511
      const CostVector &_cost;
512
      const IntVector  &_state;
513
      const CostVector &_pi;
514
      int &_in_arc;
515
      int _search_arc_num;
516

	
517
      // Pivot rule data
518
      int _block_size, _head_length, _curr_length;
519
      int _next_arc;
520
      IntVector _candidates;
521
      CostVector _cand_cost;
522

	
523
      // Functor class to compare arcs during sort of the candidate list
524
      class SortFunc
525
      {
526
      private:
527
        const CostVector &_map;
528
      public:
529
        SortFunc(const CostVector &map) : _map(map) {}
530
        bool operator()(int left, int right) {
531
          return _map[left] > _map[right];
532
        }
533
      };
534

	
535
      SortFunc _sort_func;
536

	
537
    public:
538

	
539
      // Constructor
540
      AlteringListPivotRule(NetworkSimplex &ns) :
541
        _source(ns._source), _target(ns._target),
542
        _cost(ns._cost), _state(ns._state), _pi(ns._pi),
543
        _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num),
544
        _next_arc(0), _cand_cost(ns._search_arc_num), _sort_func(_cand_cost)
545
      {
546
        // The main parameters of the pivot rule
547
        const double BLOCK_SIZE_FACTOR = 1.0;
548
        const int MIN_BLOCK_SIZE = 10;
549
        const double HEAD_LENGTH_FACTOR = 0.1;
550
        const int MIN_HEAD_LENGTH = 3;
551

	
552
        _block_size = std::max( int(BLOCK_SIZE_FACTOR *
553
                                    std::sqrt(double(_search_arc_num))),
554
                                MIN_BLOCK_SIZE );
555
        _head_length = std::max( int(HEAD_LENGTH_FACTOR * _block_size),
556
                                 MIN_HEAD_LENGTH );
557
        _candidates.resize(_head_length + _block_size);
558
        _curr_length = 0;
559
      }
560

	
561
      // Find next entering arc
562
      bool findEnteringArc() {
563
        // Check the current candidate list
564
        int e;
565
        for (int i = 0; i < _curr_length; ++i) {
566
          e = _candidates[i];
567
          _cand_cost[e] = _state[e] *
568
            (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
569
          if (_cand_cost[e] >= 0) {
570
            _candidates[i--] = _candidates[--_curr_length];
571
          }
572
        }
573

	
574
        // Extend the list
575
        int cnt = _block_size;
576
        int limit = _head_length;
577

	
578
        for (e = _next_arc; e < _search_arc_num; ++e) {
579
          _cand_cost[e] = _state[e] *
580
            (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
581
          if (_cand_cost[e] < 0) {
582
            _candidates[_curr_length++] = e;
583
          }
584
          if (--cnt == 0) {
585
            if (_curr_length > limit) goto search_end;
586
            limit = 0;
587
            cnt = _block_size;
588
          }
589
        }
590
        for (e = 0; e < _next_arc; ++e) {
591
          _cand_cost[e] = _state[e] *
592
            (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
593
          if (_cand_cost[e] < 0) {
594
            _candidates[_curr_length++] = e;
595
          }
596
          if (--cnt == 0) {
597
            if (_curr_length > limit) goto search_end;
598
            limit = 0;
599
            cnt = _block_size;
600
          }
601
        }
602
        if (_curr_length == 0) return false;
603
        
604
      search_end:
605

	
606
        // Make heap of the candidate list (approximating a partial sort)
607
        make_heap( _candidates.begin(), _candidates.begin() + _curr_length,
608
                   _sort_func );
609

	
610
        // Pop the first element of the heap
611
        _in_arc = _candidates[0];
612
        _next_arc = e;
613
        pop_heap( _candidates.begin(), _candidates.begin() + _curr_length,
614
                  _sort_func );
615
        _curr_length = std::min(_head_length, _curr_length - 1);
616
        return true;
617
      }
618

	
619
    }; //class AlteringListPivotRule
620

	
621
  public:
622

	
623
    /// \brief Constructor.
624
    ///
625
    /// The constructor of the class.
626
    ///
627
    /// \param graph The digraph the algorithm runs on.
628
    /// \param arc_mixing Indicate if the arcs have to be stored in a
629
    /// mixed order in the internal data structure. 
630
    /// In special cases, it could lead to better overall performance,
631
    /// but it is usually slower. Therefore it is disabled by default.
632
    NetworkSimplex(const GR& graph, bool arc_mixing = false) :
633
      _graph(graph), _node_id(graph), _arc_id(graph),
634
      INF(std::numeric_limits<Value>::has_infinity ?
635
          std::numeric_limits<Value>::infinity() :
636
          std::numeric_limits<Value>::max())
637
    {
638
      // Check the value types
639
      LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
640
        "The flow type of NetworkSimplex must be signed");
641
      LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
642
        "The cost type of NetworkSimplex must be signed");
643
        
644
      // Resize vectors
645
      _node_num = countNodes(_graph);
646
      _arc_num = countArcs(_graph);
647
      int all_node_num = _node_num + 1;
648
      int max_arc_num = _arc_num + 2 * _node_num;
649

	
650
      _source.resize(max_arc_num);
651
      _target.resize(max_arc_num);
652

	
653
      _lower.resize(_arc_num);
654
      _upper.resize(_arc_num);
655
      _cap.resize(max_arc_num);
656
      _cost.resize(max_arc_num);
657
      _supply.resize(all_node_num);
658
      _flow.resize(max_arc_num);
659
      _pi.resize(all_node_num);
660

	
661
      _parent.resize(all_node_num);
662
      _pred.resize(all_node_num);
663
      _forward.resize(all_node_num);
664
      _thread.resize(all_node_num);
665
      _rev_thread.resize(all_node_num);
666
      _succ_num.resize(all_node_num);
667
      _last_succ.resize(all_node_num);
668
      _state.resize(max_arc_num);
669

	
670
      // Copy the graph
671
      int i = 0;
672
      for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
673
        _node_id[n] = i;
674
      }
675
      if (arc_mixing) {
676
        // Store the arcs in a mixed order
677
        int k = std::max(int(std::sqrt(double(_arc_num))), 10);
678
        int i = 0, j = 0;
679
        for (ArcIt a(_graph); a != INVALID; ++a) {
680
          _arc_id[a] = i;
681
          _source[i] = _node_id[_graph.source(a)];
682
          _target[i] = _node_id[_graph.target(a)];
683
          if ((i += k) >= _arc_num) i = ++j;
684
        }
685
      } else {
686
        // Store the arcs in the original order
687
        int i = 0;
688
        for (ArcIt a(_graph); a != INVALID; ++a, ++i) {
689
          _arc_id[a] = i;
690
          _source[i] = _node_id[_graph.source(a)];
691
          _target[i] = _node_id[_graph.target(a)];
692
        }
693
      }
694
      
695
      // Reset parameters
696
      reset();
697
    }
698

	
699
    /// \name Parameters
700
    /// The parameters of the algorithm can be specified using these
701
    /// functions.
702

	
703
    /// @{
704

	
705
    /// \brief Set the lower bounds on the arcs.
706
    ///
707
    /// This function sets the lower bounds on the arcs.
708
    /// If it is not used before calling \ref run(), the lower bounds
709
    /// will be set to zero on all arcs.
710
    ///
711
    /// \param map An arc map storing the lower bounds.
712
    /// Its \c Value type must be convertible to the \c Value type
713
    /// of the algorithm.
714
    ///
715
    /// \return <tt>(*this)</tt>
716
    template <typename LowerMap>
717
    NetworkSimplex& lowerMap(const LowerMap& map) {
718
      _have_lower = true;
719
      for (ArcIt a(_graph); a != INVALID; ++a) {
720
        _lower[_arc_id[a]] = map[a];
721
      }
722
      return *this;
723
    }
724

	
725
    /// \brief Set the upper bounds (capacities) on the arcs.
726
    ///
727
    /// This function sets the upper bounds (capacities) on the arcs.
728
    /// If it is not used before calling \ref run(), the upper bounds
729
    /// will be set to \ref INF on all arcs (i.e. the flow value will be
730
    /// unbounded from above on each arc).
731
    ///
732
    /// \param map An arc map storing the upper bounds.
733
    /// Its \c Value type must be convertible to the \c Value type
734
    /// of the algorithm.
735
    ///
736
    /// \return <tt>(*this)</tt>
737
    template<typename UpperMap>
738
    NetworkSimplex& upperMap(const UpperMap& map) {
739
      for (ArcIt a(_graph); a != INVALID; ++a) {
740
        _upper[_arc_id[a]] = map[a];
741
      }
742
      return *this;
743
    }
744

	
745
    /// \brief Set the costs of the arcs.
746
    ///
747
    /// This function sets the costs of the arcs.
748
    /// If it is not used before calling \ref run(), the costs
749
    /// will be set to \c 1 on all arcs.
750
    ///
751
    /// \param map An arc map storing the costs.
752
    /// Its \c Value type must be convertible to the \c Cost type
753
    /// of the algorithm.
754
    ///
755
    /// \return <tt>(*this)</tt>
756
    template<typename CostMap>
757
    NetworkSimplex& costMap(const CostMap& map) {
758
      for (ArcIt a(_graph); a != INVALID; ++a) {
759
        _cost[_arc_id[a]] = map[a];
760
      }
761
      return *this;
762
    }
763

	
764
    /// \brief Set the supply values of the nodes.
765
    ///
766
    /// This function sets the supply values of the nodes.
767
    /// If neither this function nor \ref stSupply() is used before
768
    /// calling \ref run(), the supply of each node will be set to zero.
769
    ///
770
    /// \param map A node map storing the supply values.
771
    /// Its \c Value type must be convertible to the \c Value type
772
    /// of the algorithm.
773
    ///
774
    /// \return <tt>(*this)</tt>
775
    template<typename SupplyMap>
776
    NetworkSimplex& supplyMap(const SupplyMap& map) {
777
      for (NodeIt n(_graph); n != INVALID; ++n) {
778
        _supply[_node_id[n]] = map[n];
779
      }
780
      return *this;
781
    }
782

	
783
    /// \brief Set single source and target nodes and a supply value.
784
    ///
785
    /// This function sets a single source node and a single target node
786
    /// and the required flow value.
787
    /// If neither this function nor \ref supplyMap() is used before
788
    /// calling \ref run(), the supply of each node will be set to zero.
789
    ///
790
    /// Using this function has the same effect as using \ref supplyMap()
791
    /// with such a map in which \c k is assigned to \c s, \c -k is
792
    /// assigned to \c t and all other nodes have zero supply value.
793
    ///
794
    /// \param s The source node.
795
    /// \param t The target node.
796
    /// \param k The required amount of flow from node \c s to node \c t
797
    /// (i.e. the supply of \c s and the demand of \c t).
798
    ///
799
    /// \return <tt>(*this)</tt>
800
    NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) {
801
      for (int i = 0; i != _node_num; ++i) {
802
        _supply[i] = 0;
803
      }
804
      _supply[_node_id[s]] =  k;
805
      _supply[_node_id[t]] = -k;
806
      return *this;
807
    }
808
    
809
    /// \brief Set the type of the supply constraints.
810
    ///
811
    /// This function sets the type of the supply/demand constraints.
812
    /// If it is not used before calling \ref run(), the \ref GEQ supply
813
    /// type will be used.
814
    ///
815
    /// For more information see \ref SupplyType.
816
    ///
817
    /// \return <tt>(*this)</tt>
818
    NetworkSimplex& supplyType(SupplyType supply_type) {
819
      _stype = supply_type;
820
      return *this;
821
    }
822

	
823
    /// @}
824

	
825
    /// \name Execution Control
826
    /// The algorithm can be executed using \ref run().
827

	
828
    /// @{
829

	
830
    /// \brief Run the algorithm.
831
    ///
832
    /// This function runs the algorithm.
833
    /// The paramters can be specified using functions \ref lowerMap(),
834
    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), 
835
    /// \ref supplyType().
836
    /// For example,
837
    /// \code
838
    ///   NetworkSimplex<ListDigraph> ns(graph);
839
    ///   ns.lowerMap(lower).upperMap(upper).costMap(cost)
840
    ///     .supplyMap(sup).run();
841
    /// \endcode
842
    ///
843
    /// This function can be called more than once. All the parameters
844
    /// that have been given are kept for the next call, unless
845
    /// \ref reset() is called, thus only the modified parameters
846
    /// have to be set again. See \ref reset() for examples.
847
    /// However the underlying digraph must not be modified after this
848
    /// class have been constructed, since it copies and extends the graph.
849
    ///
850
    /// \param pivot_rule The pivot rule that will be used during the
851
    /// algorithm. For more information see \ref PivotRule.
852
    ///
853
    /// \return \c INFEASIBLE if no feasible flow exists,
854
    /// \n \c OPTIMAL if the problem has optimal solution
855
    /// (i.e. it is feasible and bounded), and the algorithm has found
856
    /// optimal flow and node potentials (primal and dual solutions),
857
    /// \n \c UNBOUNDED if the objective function of the problem is
858
    /// unbounded, i.e. there is a directed cycle having negative total
859
    /// cost and infinite upper bound.
860
    ///
861
    /// \see ProblemType, PivotRule
862
    ProblemType run(PivotRule pivot_rule = BLOCK_SEARCH) {
863
      if (!init()) return INFEASIBLE;
864
      return start(pivot_rule);
865
    }
866

	
867
    /// \brief Reset all the parameters that have been given before.
868
    ///
869
    /// This function resets all the paramaters that have been given
870
    /// before using functions \ref lowerMap(), \ref upperMap(),
871
    /// \ref costMap(), \ref supplyMap(), \ref stSupply(), \ref supplyType().
872
    ///
873
    /// It is useful for multiple run() calls. If this function is not
874
    /// used, all the parameters given before are kept for the next
875
    /// \ref run() call.
876
    /// However the underlying digraph must not be modified after this
877
    /// class have been constructed, since it copies and extends the graph.
878
    ///
879
    /// For example,
880
    /// \code
881
    ///   NetworkSimplex<ListDigraph> ns(graph);
882
    ///
883
    ///   // First run
884
    ///   ns.lowerMap(lower).upperMap(upper).costMap(cost)
885
    ///     .supplyMap(sup).run();
886
    ///
887
    ///   // Run again with modified cost map (reset() is not called,
888
    ///   // so only the cost map have to be set again)
889
    ///   cost[e] += 100;
890
    ///   ns.costMap(cost).run();
891
    ///
892
    ///   // Run again from scratch using reset()
893
    ///   // (the lower bounds will be set to zero on all arcs)
894
    ///   ns.reset();
895
    ///   ns.upperMap(capacity).costMap(cost)
896
    ///     .supplyMap(sup).run();
897
    /// \endcode
898
    ///
899
    /// \return <tt>(*this)</tt>
900
    NetworkSimplex& reset() {
901
      for (int i = 0; i != _node_num; ++i) {
902
        _supply[i] = 0;
903
      }
904
      for (int i = 0; i != _arc_num; ++i) {
905
        _lower[i] = 0;
906
        _upper[i] = INF;
907
        _cost[i] = 1;
908
      }
909
      _have_lower = false;
910
      _stype = GEQ;
911
      return *this;
912
    }
913

	
914
    /// @}
915

	
916
    /// \name Query Functions
917
    /// The results of the algorithm can be obtained using these
918
    /// functions.\n
919
    /// The \ref run() function must be called before using them.
920

	
921
    /// @{
922

	
923
    /// \brief Return the total cost of the found flow.
924
    ///
925
    /// This function returns the total cost of the found flow.
926
    /// Its complexity is O(e).
927
    ///
928
    /// \note The return type of the function can be specified as a
929
    /// template parameter. For example,
930
    /// \code
931
    ///   ns.totalCost<double>();
932
    /// \endcode
933
    /// It is useful if the total cost cannot be stored in the \c Cost
934
    /// type of the algorithm, which is the default return type of the
935
    /// function.
936
    ///
937
    /// \pre \ref run() must be called before using this function.
938
    template <typename Number>
939
    Number totalCost() const {
940
      Number c = 0;
941
      for (ArcIt a(_graph); a != INVALID; ++a) {
942
        int i = _arc_id[a];
943
        c += Number(_flow[i]) * Number(_cost[i]);
944
      }
945
      return c;
946
    }
947

	
948
#ifndef DOXYGEN
949
    Cost totalCost() const {
950
      return totalCost<Cost>();
951
    }
952
#endif
953

	
954
    /// \brief Return the flow on the given arc.
955
    ///
956
    /// This function returns the flow on the given arc.
957
    ///
958
    /// \pre \ref run() must be called before using this function.
959
    Value flow(const Arc& a) const {
960
      return _flow[_arc_id[a]];
961
    }
962

	
963
    /// \brief Return the flow map (the primal solution).
964
    ///
965
    /// This function copies the flow value on each arc into the given
966
    /// map. The \c Value type of the algorithm must be convertible to
967
    /// the \c Value type of the map.
968
    ///
969
    /// \pre \ref run() must be called before using this function.
970
    template <typename FlowMap>
971
    void flowMap(FlowMap &map) const {
972
      for (ArcIt a(_graph); a != INVALID; ++a) {
973
        map.set(a, _flow[_arc_id[a]]);
974
      }
975
    }
976

	
977
    /// \brief Return the potential (dual value) of the given node.
978
    ///
979
    /// This function returns the potential (dual value) of the
980
    /// given node.
981
    ///
982
    /// \pre \ref run() must be called before using this function.
983
    Cost potential(const Node& n) const {
984
      return _pi[_node_id[n]];
985
    }
986

	
987
    /// \brief Return the potential map (the dual solution).
988
    ///
989
    /// This function copies the potential (dual value) of each node
990
    /// into the given map.
991
    /// The \c Cost type of the algorithm must be convertible to the
992
    /// \c Value type of the map.
993
    ///
994
    /// \pre \ref run() must be called before using this function.
995
    template <typename PotentialMap>
996
    void potentialMap(PotentialMap &map) const {
997
      for (NodeIt n(_graph); n != INVALID; ++n) {
998
        map.set(n, _pi[_node_id[n]]);
999
      }
1000
    }
1001

	
1002
    /// @}
1003

	
1004
  private:
1005

	
1006
    // Initialize internal data structures
1007
    bool init() {
1008
      if (_node_num == 0) return false;
1009

	
1010
      // Check the sum of supply values
1011
      _sum_supply = 0;
1012
      for (int i = 0; i != _node_num; ++i) {
1013
        _sum_supply += _supply[i];
1014
      }
1015
      if ( !((_stype == GEQ && _sum_supply <= 0) ||
1016
             (_stype == LEQ && _sum_supply >= 0)) ) return false;
1017

	
1018
      // Remove non-zero lower bounds
1019
      if (_have_lower) {
1020
        for (int i = 0; i != _arc_num; ++i) {
1021
          Value c = _lower[i];
1022
          if (c >= 0) {
1023
            _cap[i] = _upper[i] < INF ? _upper[i] - c : INF;
1024
          } else {
1025
            _cap[i] = _upper[i] < INF + c ? _upper[i] - c : INF;
1026
          }
1027
          _supply[_source[i]] -= c;
1028
          _supply[_target[i]] += c;
1029
        }
1030
      } else {
1031
        for (int i = 0; i != _arc_num; ++i) {
1032
          _cap[i] = _upper[i];
1033
        }
1034
      }
1035

	
1036
      // Initialize artifical cost
1037
      Cost ART_COST;
1038
      if (std::numeric_limits<Cost>::is_exact) {
1039
        ART_COST = std::numeric_limits<Cost>::max() / 2 + 1;
1040
      } else {
1041
        ART_COST = std::numeric_limits<Cost>::min();
1042
        for (int i = 0; i != _arc_num; ++i) {
1043
          if (_cost[i] > ART_COST) ART_COST = _cost[i];
1044
        }
1045
        ART_COST = (ART_COST + 1) * _node_num;
1046
      }
1047

	
1048
      // Initialize arc maps
1049
      for (int i = 0; i != _arc_num; ++i) {
1050
        _flow[i] = 0;
1051
        _state[i] = STATE_LOWER;
1052
      }
1053
      
1054
      // Set data for the artificial root node
1055
      _root = _node_num;
1056
      _parent[_root] = -1;
1057
      _pred[_root] = -1;
1058
      _thread[_root] = 0;
1059
      _rev_thread[0] = _root;
1060
      _succ_num[_root] = _node_num + 1;
1061
      _last_succ[_root] = _root - 1;
1062
      _supply[_root] = -_sum_supply;
1063
      _pi[_root] = 0;
1064

	
1065
      // Add artificial arcs and initialize the spanning tree data structure
1066
      if (_sum_supply == 0) {
1067
        // EQ supply constraints
1068
        _search_arc_num = _arc_num;
1069
        _all_arc_num = _arc_num + _node_num;
1070
        for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
1071
          _parent[u] = _root;
1072
          _pred[u] = e;
1073
          _thread[u] = u + 1;
1074
          _rev_thread[u + 1] = u;
1075
          _succ_num[u] = 1;
1076
          _last_succ[u] = u;
1077
          _cap[e] = INF;
1078
          _state[e] = STATE_TREE;
1079
          if (_supply[u] >= 0) {
1080
            _forward[u] = true;
1081
            _pi[u] = 0;
1082
            _source[e] = u;
1083
            _target[e] = _root;
1084
            _flow[e] = _supply[u];
1085
            _cost[e] = 0;
1086
          } else {
1087
            _forward[u] = false;
1088
            _pi[u] = ART_COST;
1089
            _source[e] = _root;
1090
            _target[e] = u;
1091
            _flow[e] = -_supply[u];
1092
            _cost[e] = ART_COST;
1093
          }
1094
        }
1095
      }
1096
      else if (_sum_supply > 0) {
1097
        // LEQ supply constraints
1098
        _search_arc_num = _arc_num + _node_num;
1099
        int f = _arc_num + _node_num;
1100
        for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
1101
          _parent[u] = _root;
1102
          _thread[u] = u + 1;
1103
          _rev_thread[u + 1] = u;
1104
          _succ_num[u] = 1;
1105
          _last_succ[u] = u;
1106
          if (_supply[u] >= 0) {
1107
            _forward[u] = true;
1108
            _pi[u] = 0;
1109
            _pred[u] = e;
1110
            _source[e] = u;
1111
            _target[e] = _root;
1112
            _cap[e] = INF;
1113
            _flow[e] = _supply[u];
1114
            _cost[e] = 0;
1115
            _state[e] = STATE_TREE;
1116
          } else {
1117
            _forward[u] = false;
1118
            _pi[u] = ART_COST;
1119
            _pred[u] = f;
1120
            _source[f] = _root;
1121
            _target[f] = u;
1122
            _cap[f] = INF;
1123
            _flow[f] = -_supply[u];
1124
            _cost[f] = ART_COST;
1125
            _state[f] = STATE_TREE;
1126
            _source[e] = u;
1127
            _target[e] = _root;
1128
            _cap[e] = INF;
1129
            _flow[e] = 0;
1130
            _cost[e] = 0;
1131
            _state[e] = STATE_LOWER;
1132
            ++f;
1133
          }
1134
        }
1135
        _all_arc_num = f;
1136
      }
1137
      else {
1138
        // GEQ supply constraints
1139
        _search_arc_num = _arc_num + _node_num;
1140
        int f = _arc_num + _node_num;
1141
        for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
1142
          _parent[u] = _root;
1143
          _thread[u] = u + 1;
1144
          _rev_thread[u + 1] = u;
1145
          _succ_num[u] = 1;
1146
          _last_succ[u] = u;
1147
          if (_supply[u] <= 0) {
1148
            _forward[u] = false;
1149
            _pi[u] = 0;
1150
            _pred[u] = e;
1151
            _source[e] = _root;
1152
            _target[e] = u;
1153
            _cap[e] = INF;
1154
            _flow[e] = -_supply[u];
1155
            _cost[e] = 0;
1156
            _state[e] = STATE_TREE;
1157
          } else {
1158
            _forward[u] = true;
1159
            _pi[u] = -ART_COST;
1160
            _pred[u] = f;
1161
            _source[f] = u;
1162
            _target[f] = _root;
1163
            _cap[f] = INF;
1164
            _flow[f] = _supply[u];
1165
            _state[f] = STATE_TREE;
1166
            _cost[f] = ART_COST;
1167
            _source[e] = _root;
1168
            _target[e] = u;
1169
            _cap[e] = INF;
1170
            _flow[e] = 0;
1171
            _cost[e] = 0;
1172
            _state[e] = STATE_LOWER;
1173
            ++f;
1174
          }
1175
        }
1176
        _all_arc_num = f;
1177
      }
1178

	
1179
      return true;
1180
    }
1181

	
1182
    // Find the join node
1183
    void findJoinNode() {
1184
      int u = _source[in_arc];
1185
      int v = _target[in_arc];
1186
      while (u != v) {
1187
        if (_succ_num[u] < _succ_num[v]) {
1188
          u = _parent[u];
1189
        } else {
1190
          v = _parent[v];
1191
        }
1192
      }
1193
      join = u;
1194
    }
1195

	
1196
    // Find the leaving arc of the cycle and returns true if the
1197
    // leaving arc is not the same as the entering arc
1198
    bool findLeavingArc() {
1199
      // Initialize first and second nodes according to the direction
1200
      // of the cycle
1201
      if (_state[in_arc] == STATE_LOWER) {
1202
        first  = _source[in_arc];
1203
        second = _target[in_arc];
1204
      } else {
1205
        first  = _target[in_arc];
1206
        second = _source[in_arc];
1207
      }
1208
      delta = _cap[in_arc];
1209
      int result = 0;
1210
      Value d;
1211
      int e;
1212

	
1213
      // Search the cycle along the path form the first node to the root
1214
      for (int u = first; u != join; u = _parent[u]) {
1215
        e = _pred[u];
1216
        d = _forward[u] ?
1217
          _flow[e] : (_cap[e] == INF ? INF : _cap[e] - _flow[e]);
1218
        if (d < delta) {
1219
          delta = d;
1220
          u_out = u;
1221
          result = 1;
1222
        }
1223
      }
1224
      // Search the cycle along the path form the second node to the root
1225
      for (int u = second; u != join; u = _parent[u]) {
1226
        e = _pred[u];
1227
        d = _forward[u] ? 
1228
          (_cap[e] == INF ? INF : _cap[e] - _flow[e]) : _flow[e];
1229
        if (d <= delta) {
1230
          delta = d;
1231
          u_out = u;
1232
          result = 2;
1233
        }
1234
      }
1235

	
1236
      if (result == 1) {
1237
        u_in = first;
1238
        v_in = second;
1239
      } else {
1240
        u_in = second;
1241
        v_in = first;
1242
      }
1243
      return result != 0;
1244
    }
1245

	
1246
    // Change _flow and _state vectors
1247
    void changeFlow(bool change) {
1248
      // Augment along the cycle
1249
      if (delta > 0) {
1250
        Value val = _state[in_arc] * delta;
1251
        _flow[in_arc] += val;
1252
        for (int u = _source[in_arc]; u != join; u = _parent[u]) {
1253
          _flow[_pred[u]] += _forward[u] ? -val : val;
1254
        }
1255
        for (int u = _target[in_arc]; u != join; u = _parent[u]) {
1256
          _flow[_pred[u]] += _forward[u] ? val : -val;
1257
        }
1258
      }
1259
      // Update the state of the entering and leaving arcs
1260
      if (change) {
1261
        _state[in_arc] = STATE_TREE;
1262
        _state[_pred[u_out]] =
1263
          (_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER;
1264
      } else {
1265
        _state[in_arc] = -_state[in_arc];
1266
      }
1267
    }
1268

	
1269
    // Update the tree structure
1270
    void updateTreeStructure() {
1271
      int u, w;
1272
      int old_rev_thread = _rev_thread[u_out];
1273
      int old_succ_num = _succ_num[u_out];
1274
      int old_last_succ = _last_succ[u_out];
1275
      v_out = _parent[u_out];
1276

	
1277
      u = _last_succ[u_in];  // the last successor of u_in
1278
      right = _thread[u];    // the node after it
1279

	
1280
      // Handle the case when old_rev_thread equals to v_in
1281
      // (it also means that join and v_out coincide)
1282
      if (old_rev_thread == v_in) {
1283
        last = _thread[_last_succ[u_out]];
1284
      } else {
1285
        last = _thread[v_in];
1286
      }
1287

	
1288
      // Update _thread and _parent along the stem nodes (i.e. the nodes
1289
      // between u_in and u_out, whose parent have to be changed)
1290
      _thread[v_in] = stem = u_in;
1291
      _dirty_revs.clear();
1292
      _dirty_revs.push_back(v_in);
1293
      par_stem = v_in;
1294
      while (stem != u_out) {
1295
        // Insert the next stem node into the thread list
1296
        new_stem = _parent[stem];
1297
        _thread[u] = new_stem;
1298
        _dirty_revs.push_back(u);
1299

	
1300
        // Remove the subtree of stem from the thread list
1301
        w = _rev_thread[stem];
1302
        _thread[w] = right;
1303
        _rev_thread[right] = w;
1304

	
1305
        // Change the parent node and shift stem nodes
1306
        _parent[stem] = par_stem;
1307
        par_stem = stem;
1308
        stem = new_stem;
1309

	
1310
        // Update u and right
1311
        u = _last_succ[stem] == _last_succ[par_stem] ?
1312
          _rev_thread[par_stem] : _last_succ[stem];
1313
        right = _thread[u];
1314
      }
1315
      _parent[u_out] = par_stem;
1316
      _thread[u] = last;
1317
      _rev_thread[last] = u;
1318
      _last_succ[u_out] = u;
1319

	
1320
      // Remove the subtree of u_out from the thread list except for
1321
      // the case when old_rev_thread equals to v_in
1322
      // (it also means that join and v_out coincide)
1323
      if (old_rev_thread != v_in) {
1324
        _thread[old_rev_thread] = right;
1325
        _rev_thread[right] = old_rev_thread;
1326
      }
1327

	
1328
      // Update _rev_thread using the new _thread values
1329
      for (int i = 0; i < int(_dirty_revs.size()); ++i) {
1330
        u = _dirty_revs[i];
1331
        _rev_thread[_thread[u]] = u;
1332
      }
1333

	
1334
      // Update _pred, _forward, _last_succ and _succ_num for the
1335
      // stem nodes from u_out to u_in
1336
      int tmp_sc = 0, tmp_ls = _last_succ[u_out];
1337
      u = u_out;
1338
      while (u != u_in) {
1339
        w = _parent[u];
1340
        _pred[u] = _pred[w];
1341
        _forward[u] = !_forward[w];
1342
        tmp_sc += _succ_num[u] - _succ_num[w];
1343
        _succ_num[u] = tmp_sc;
1344
        _last_succ[w] = tmp_ls;
1345
        u = w;
1346
      }
1347
      _pred[u_in] = in_arc;
1348
      _forward[u_in] = (u_in == _source[in_arc]);
1349
      _succ_num[u_in] = old_succ_num;
1350

	
1351
      // Set limits for updating _last_succ form v_in and v_out
1352
      // towards the root
1353
      int up_limit_in = -1;
1354
      int up_limit_out = -1;
1355
      if (_last_succ[join] == v_in) {
1356
        up_limit_out = join;
1357
      } else {
1358
        up_limit_in = join;
1359
      }
1360

	
1361
      // Update _last_succ from v_in towards the root
1362
      for (u = v_in; u != up_limit_in && _last_succ[u] == v_in;
1363
           u = _parent[u]) {
1364
        _last_succ[u] = _last_succ[u_out];
1365
      }
1366
      // Update _last_succ from v_out towards the root
1367
      if (join != old_rev_thread && v_in != old_rev_thread) {
1368
        for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ;
1369
             u = _parent[u]) {
1370
          _last_succ[u] = old_rev_thread;
1371
        }
1372
      } else {
1373
        for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ;
1374
             u = _parent[u]) {
1375
          _last_succ[u] = _last_succ[u_out];
1376
        }
1377
      }
1378

	
1379
      // Update _succ_num from v_in to join
1380
      for (u = v_in; u != join; u = _parent[u]) {
1381
        _succ_num[u] += old_succ_num;
1382
      }
1383
      // Update _succ_num from v_out to join
1384
      for (u = v_out; u != join; u = _parent[u]) {
1385
        _succ_num[u] -= old_succ_num;
1386
      }
1387
    }
1388

	
1389
    // Update potentials
1390
    void updatePotential() {
1391
      Cost sigma = _forward[u_in] ?
1392
        _pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] :
1393
        _pi[v_in] - _pi[u_in] + _cost[_pred[u_in]];
1394
      // Update potentials in the subtree, which has been moved
1395
      int end = _thread[_last_succ[u_in]];
1396
      for (int u = u_in; u != end; u = _thread[u]) {
1397
        _pi[u] += sigma;
1398
      }
1399
    }
1400

	
1401
    // Execute the algorithm
1402
    ProblemType start(PivotRule pivot_rule) {
1403
      // Select the pivot rule implementation
1404
      switch (pivot_rule) {
1405
        case FIRST_ELIGIBLE:
1406
          return start<FirstEligiblePivotRule>();
1407
        case BEST_ELIGIBLE:
1408
          return start<BestEligiblePivotRule>();
1409
        case BLOCK_SEARCH:
1410
          return start<BlockSearchPivotRule>();
1411
        case CANDIDATE_LIST:
1412
          return start<CandidateListPivotRule>();
1413
        case ALTERING_LIST:
1414
          return start<AlteringListPivotRule>();
1415
      }
1416
      return INFEASIBLE; // avoid warning
1417
    }
1418

	
1419
    template <typename PivotRuleImpl>
1420
    ProblemType start() {
1421
      PivotRuleImpl pivot(*this);
1422

	
1423
      // Execute the Network Simplex algorithm
1424
      while (pivot.findEnteringArc()) {
1425
        findJoinNode();
1426
        bool change = findLeavingArc();
1427
        if (delta >= INF) return UNBOUNDED;
1428
        changeFlow(change);
1429
        if (change) {
1430
          updateTreeStructure();
1431
          updatePotential();
1432
        }
1433
      }
1434
      
1435
      // Check feasibility
1436
      for (int e = _search_arc_num; e != _all_arc_num; ++e) {
1437
        if (_flow[e] != 0) return INFEASIBLE;
1438
      }
1439

	
1440
      // Transform the solution and the supply map to the original form
1441
      if (_have_lower) {
1442
        for (int i = 0; i != _arc_num; ++i) {
1443
          Value c = _lower[i];
1444
          if (c != 0) {
1445
            _flow[i] += c;
1446
            _supply[_source[i]] += c;
1447
            _supply[_target[i]] -= c;
1448
          }
1449
        }
1450
      }
1451
      
1452
      // Shift potentials to meet the requirements of the GEQ/LEQ type
1453
      // optimality conditions
1454
      if (_sum_supply == 0) {
1455
        if (_stype == GEQ) {
1456
          Cost max_pot = std::numeric_limits<Cost>::min();
1457
          for (int i = 0; i != _node_num; ++i) {
1458
            if (_pi[i] > max_pot) max_pot = _pi[i];
1459
          }
1460
          if (max_pot > 0) {
1461
            for (int i = 0; i != _node_num; ++i)
1462
              _pi[i] -= max_pot;
1463
          }
1464
        } else {
1465
          Cost min_pot = std::numeric_limits<Cost>::max();
1466
          for (int i = 0; i != _node_num; ++i) {
1467
            if (_pi[i] < min_pot) min_pot = _pi[i];
1468
          }
1469
          if (min_pot < 0) {
1470
            for (int i = 0; i != _node_num; ++i)
1471
              _pi[i] -= min_pot;
1472
          }
1473
        }
1474
      }
1475

	
1476
      return OPTIMAL;
1477
    }
1478

	
1479
  }; //class NetworkSimplex
1480

	
1481
  ///@}
1482

	
1483
} //namespace lemon
1484

	
1485
#endif //LEMON_NETWORK_SIMPLEX_H
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_PAIRING_HEAP_H
20
#define LEMON_PAIRING_HEAP_H
21

	
22
///\file
23
///\ingroup heaps
24
///\brief Pairing heap implementation.
25

	
26
#include <vector>
27
#include <utility>
28
#include <functional>
29
#include <lemon/math.h>
30

	
31
namespace lemon {
32

	
33
  /// \ingroup heaps
34
  ///
35
  ///\brief Pairing Heap.
36
  ///
37
  /// This class implements the \e pairing \e heap data structure.
38
  /// It fully conforms to the \ref concepts::Heap "heap concept".
39
  ///
40
  /// The methods \ref increase() and \ref erase() are not efficient
41
  /// in a pairing heap. In case of many calls of these operations,
42
  /// it is better to use other heap structure, e.g. \ref BinHeap
43
  /// "binary heap".
44
  ///
45
  /// \tparam PR Type of the priorities of the items.
46
  /// \tparam IM A read-writable item map with \c int values, used
47
  /// internally to handle the cross references.
48
  /// \tparam CMP A functor class for comparing the priorities.
49
  /// The default is \c std::less<PR>.
50
#ifdef DOXYGEN
51
  template <typename PR, typename IM, typename CMP>
52
#else
53
  template <typename PR, typename IM, typename CMP = std::less<PR> >
54
#endif
55
  class PairingHeap {
56
  public:
57
    /// Type of the item-int map.
58
    typedef IM ItemIntMap;
59
    /// Type of the priorities.
60
    typedef PR Prio;
61
    /// Type of the items stored in the heap.
62
    typedef typename ItemIntMap::Key Item;
63
    /// Functor type for comparing the priorities.
64
    typedef CMP Compare;
65

	
66
    /// \brief Type to represent the states of the items.
67
    ///
68
    /// Each item has a state associated to it. It can be "in heap",
69
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
70
    /// heap's point of view, but may be useful to the user.
71
    ///
72
    /// The item-int map must be initialized in such way that it assigns
73
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
74
    enum State {
75
      IN_HEAP = 0,    ///< = 0.
76
      PRE_HEAP = -1,  ///< = -1.
77
      POST_HEAP = -2  ///< = -2.
78
    };
79

	
80
  private:
81
    class store;
82

	
83
    std::vector<store> _data;
84
    int _min;
85
    ItemIntMap &_iim;
86
    Compare _comp;
87
    int _num_items;
88

	
89
  public:
90
    /// \brief Constructor.
91
    ///
92
    /// Constructor.
93
    /// \param map A map that assigns \c int values to the items.
94
    /// It is used internally to handle the cross references.
95
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
96
    explicit PairingHeap(ItemIntMap &map)
97
      : _min(0), _iim(map), _num_items(0) {}
98

	
99
    /// \brief Constructor.
100
    ///
101
    /// Constructor.
102
    /// \param map A map that assigns \c int values to the items.
103
    /// It is used internally to handle the cross references.
104
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
105
    /// \param comp The function object used for comparing the priorities.
106
    PairingHeap(ItemIntMap &map, const Compare &comp)
107
      : _min(0), _iim(map), _comp(comp), _num_items(0) {}
108

	
109
    /// \brief The number of items stored in the heap.
110
    ///
111
    /// This function returns the number of items stored in the heap.
112
    int size() const { return _num_items; }
113

	
114
    /// \brief Check if the heap is empty.
115
    ///
116
    /// This function returns \c true if the heap is empty.
117
    bool empty() const { return _num_items==0; }
118

	
119
    /// \brief Make the heap empty.
120
    ///
121
    /// This functon makes the heap empty.
122
    /// It does not change the cross reference map. If you want to reuse
123
    /// a heap that is not surely empty, you should first clear it and
124
    /// then you should set the cross reference map to \c PRE_HEAP
125
    /// for each item.
126
    void clear() {
127
      _data.clear();
128
      _min = 0;
129
      _num_items = 0;
130
    }
131

	
132
    /// \brief Set the priority of an item or insert it, if it is
133
    /// not stored in the heap.
134
    ///
135
    /// This method sets the priority of the given item if it is
136
    /// already stored in the heap. Otherwise it inserts the given
137
    /// item into the heap with the given priority.
138
    /// \param item The item.
139
    /// \param value The priority.
140
    void set (const Item& item, const Prio& value) {
141
      int i=_iim[item];
142
      if ( i>=0 && _data[i].in ) {
143
        if ( _comp(value, _data[i].prio) ) decrease(item, value);
144
        if ( _comp(_data[i].prio, value) ) increase(item, value);
145
      } else push(item, value);
146
    }
147

	
148
    /// \brief Insert an item into the heap with the given priority.
149
    ///
150
    /// This function inserts the given item into the heap with the
151
    /// given priority.
152
    /// \param item The item to insert.
153
    /// \param value The priority of the item.
154
    /// \pre \e item must not be stored in the heap.
155
    void push (const Item& item, const Prio& value) {
156
      int i=_iim[item];
157
      if( i<0 ) {
158
        int s=_data.size();
159
        _iim.set(item, s);
160
        store st;
161
        st.name=item;
162
        _data.push_back(st);
163
        i=s;
164
      } else {
165
        _data[i].parent=_data[i].child=-1;
166
        _data[i].left_child=false;
167
        _data[i].degree=0;
168
        _data[i].in=true;
169
      }
170

	
171
      _data[i].prio=value;
172

	
173
      if ( _num_items!=0 ) {
174
        if ( _comp( value, _data[_min].prio) ) {
175
          fuse(i,_min);
176
          _min=i;
177
        }
178
        else fuse(_min,i);
179
      }
180
      else _min=i;
181

	
182
      ++_num_items;
183
    }
184

	
185
    /// \brief Return the item having minimum priority.
186
    ///
187
    /// This function returns the item having minimum priority.
188
    /// \pre The heap must be non-empty.
189
    Item top() const { return _data[_min].name; }
190

	
191
    /// \brief The minimum priority.
192
    ///
193
    /// This function returns the minimum priority.
194
    /// \pre The heap must be non-empty.
195
    const Prio& prio() const { return _data[_min].prio; }
196

	
197
    /// \brief The priority of the given item.
198
    ///
199
    /// This function returns the priority of the given item.
200
    /// \param item The item.
201
    /// \pre \e item must be in the heap.
202
    const Prio& operator[](const Item& item) const {
203
      return _data[_iim[item]].prio;
204
    }
205

	
206
    /// \brief Remove the item having minimum priority.
207
    ///
208
    /// This function removes the item having minimum priority.
209
    /// \pre The heap must be non-empty.
210
    void pop() {
211
      std::vector<int> trees;
212
      int i=0, child_right = 0;
213
      _data[_min].in=false;
214

	
215
      if( -1!=_data[_min].child ) {
216
        i=_data[_min].child;
217
        trees.push_back(i);
218
        _data[i].parent = -1;
219
        _data[_min].child = -1;
220

	
221
        int ch=-1;
222
        while( _data[i].child!=-1 ) {
223
          ch=_data[i].child;
224
          if( _data[ch].left_child && i==_data[ch].parent ) {
225
            break;
226
          } else {
227
            if( _data[ch].left_child ) {
228
              child_right=_data[ch].parent;
229
              _data[ch].parent = i;
230
              --_data[i].degree;
231
            }
232
            else {
233
              child_right=ch;
234
              _data[i].child=-1;
235
              _data[i].degree=0;
236
            }
237
            _data[child_right].parent = -1;
238
            trees.push_back(child_right);
239
            i = child_right;
240
          }
241
        }
242

	
243
        int num_child = trees.size();
244
        int other;
245
        for( i=0; i<num_child-1; i+=2 ) {
246
          if ( !_comp(_data[trees[i]].prio, _data[trees[i+1]].prio) ) {
247
            other=trees[i];
248
            trees[i]=trees[i+1];
249
            trees[i+1]=other;
250
          }
251
          fuse( trees[i], trees[i+1] );
252
        }
253

	
254
        i = (0==(num_child % 2)) ? num_child-2 : num_child-1;
255
        while(i>=2) {
256
          if ( _comp(_data[trees[i]].prio, _data[trees[i-2]].prio) ) {
257
            other=trees[i];
258
            trees[i]=trees[i-2];
259
            trees[i-2]=other;
260
          }
261
          fuse( trees[i-2], trees[i] );
262
          i-=2;
263
        }
264
        _min = trees[0];
265
      }
266
      else {
267
        _min = _data[_min].child;
268
      }
269

	
270
      if (_min >= 0) _data[_min].left_child = false;
271
      --_num_items;
272
    }
273

	
274
    /// \brief Remove the given item from the heap.
275
    ///
276
    /// This function removes the given item from the heap if it is
277
    /// already stored.
278
    /// \param item The item to delete.
279
    /// \pre \e item must be in the heap.
280
    void erase (const Item& item) {
281
      int i=_iim[item];
282
      if ( i>=0 && _data[i].in ) {
283
        decrease( item, _data[_min].prio-1 );
284
        pop();
285
      }
286
    }
287

	
288
    /// \brief Decrease the priority of an item to the given value.
289
    ///
290
    /// This function decreases the priority of an item to the given value.
291
    /// \param item The item.
292
    /// \param value The priority.
293
    /// \pre \e item must be stored in the heap with priority at least \e value.
294
    void decrease (Item item, const Prio& value) {
295
      int i=_iim[item];
296
      _data[i].prio=value;
297
      int p=_data[i].parent;
298

	
299
      if( _data[i].left_child && i!=_data[p].child ) {
300
        p=_data[p].parent;
301
      }
302

	
303
      if ( p!=-1 && _comp(value,_data[p].prio) ) {
304
        cut(i,p);
305
        if ( _comp(_data[_min].prio,value) ) {
306
          fuse(_min,i);
307
        } else {
308
          fuse(i,_min);
309
          _min=i;
310
        }
311
      }
312
    }
313

	
314
    /// \brief Increase the priority of an item to the given value.
315
    ///
316
    /// This function increases the priority of an item to the given value.
317
    /// \param item The item.
318
    /// \param value The priority.
319
    /// \pre \e item must be stored in the heap with priority at most \e value.
320
    void increase (Item item, const Prio& value) {
321
      erase(item);
322
      push(item,value);
323
    }
324

	
325
    /// \brief Return the state of an item.
326
    ///
327
    /// This method returns \c PRE_HEAP if the given item has never
328
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
329
    /// and \c POST_HEAP otherwise.
330
    /// In the latter case it is possible that the item will get back
331
    /// to the heap again.
332
    /// \param item The item.
333
    State state(const Item &item) const {
334
      int i=_iim[item];
335
      if( i>=0 ) {
336
        if( _data[i].in ) i=0;
337
        else i=-2;
338
      }
339
      return State(i);
340
    }
341

	
342
    /// \brief Set the state of an item in the heap.
343
    ///
344
    /// This function sets the state of the given item in the heap.
345
    /// It can be used to manually clear the heap when it is important
346
    /// to achive better time complexity.
347
    /// \param i The item.
348
    /// \param st The state. It should not be \c IN_HEAP.
349
    void state(const Item& i, State st) {
350
      switch (st) {
351
      case POST_HEAP:
352
      case PRE_HEAP:
353
        if (state(i) == IN_HEAP) erase(i);
354
        _iim[i]=st;
355
        break;
356
      case IN_HEAP:
357
        break;
358
      }
359
    }
360

	
361
  private:
362

	
363
    void cut(int a, int b) {
364
      int child_a;
365
      switch (_data[a].degree) {
366
        case 2:
367
          child_a = _data[_data[a].child].parent;
368
          if( _data[a].left_child ) {
369
            _data[child_a].left_child=true;
370
            _data[b].child=child_a;
371
            _data[child_a].parent=_data[a].parent;
372
          }
373
          else {
374
            _data[child_a].left_child=false;
375
            _data[child_a].parent=b;
376
            if( a!=_data[b].child )
377
              _data[_data[b].child].parent=child_a;
378
            else
379
              _data[b].child=child_a;
380
          }
381
          --_data[a].degree;
382
          _data[_data[a].child].parent=a;
383
          break;
384

	
385
        case 1:
386
          child_a = _data[a].child;
387
          if( !_data[child_a].left_child ) {
388
            --_data[a].degree;
389
            if( _data[a].left_child ) {
390
              _data[child_a].left_child=true;
391
              _data[child_a].parent=_data[a].parent;
392
              _data[b].child=child_a;
393
            }
394
            else {
395
              _data[child_a].left_child=false;
396
              _data[child_a].parent=b;
397
              if( a!=_data[b].child )
398
                _data[_data[b].child].parent=child_a;
399
              else
400
                _data[b].child=child_a;
401
            }
402
            _data[a].child=-1;
403
          }
404
          else {
405
            --_data[b].degree;
406
            if( _data[a].left_child ) {
407
              _data[b].child =
408
                (1==_data[b].degree) ? _data[a].parent : -1;
409
            } else {
410
              if (1==_data[b].degree)
411
                _data[_data[b].child].parent=b;
412
              else
413
                _data[b].child=-1;
414
            }
415
          }
416
          break;
417

	
418
        case 0:
419
          --_data[b].degree;
420
          if( _data[a].left_child ) {
421
            _data[b].child =
422
              (0!=_data[b].degree) ? _data[a].parent : -1;
423
          } else {
424
            if( 0!=_data[b].degree )
425
              _data[_data[b].child].parent=b;
426
            else
427
              _data[b].child=-1;
428
          }
429
          break;
430
      }
431
      _data[a].parent=-1;
432
      _data[a].left_child=false;
433
    }
434

	
435
    void fuse(int a, int b) {
436
      int child_a = _data[a].child;
437
      int child_b = _data[b].child;
438
      _data[a].child=b;
439
      _data[b].parent=a;
440
      _data[b].left_child=true;
441

	
442
      if( -1!=child_a ) {
443
        _data[b].child=child_a;
444
        _data[child_a].parent=b;
445
        _data[child_a].left_child=false;
446
        ++_data[b].degree;
447

	
448
        if( -1!=child_b ) {
449
           _data[b].child=child_b;
450
           _data[child_b].parent=child_a;
451
        }
452
      }
453
      else { ++_data[a].degree; }
454
    }
455

	
456
    class store {
457
      friend class PairingHeap;
458

	
459
      Item name;
460
      int parent;
461
      int child;
462
      bool left_child;
463
      int degree;
464
      bool in;
465
      Prio prio;
466

	
467
      store() : parent(-1), child(-1), left_child(false), degree(0), in(true) {}
468
    };
469
  };
470

	
471
} //namespace lemon
472

	
473
#endif //LEMON_PAIRING_HEAP_H
474

	
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_RADIX_HEAP_H
20
#define LEMON_RADIX_HEAP_H
21

	
22
///\ingroup heaps
23
///\file
24
///\brief Radix heap implementation.
25

	
26
#include <vector>
27
#include <lemon/error.h>
28

	
29
namespace lemon {
30

	
31

	
32
  /// \ingroup heaps
33
  ///
34
  /// \brief Radix heap data structure.
35
  ///
36
  /// This class implements the \e radix \e heap data structure.
37
  /// It practically conforms to the \ref concepts::Heap "heap concept",
38
  /// but it has some limitations due its special implementation.
39
  /// The type of the priorities must be \c int and the priority of an
40
  /// item cannot be decreased under the priority of the last removed item.
41
  ///
42
  /// \tparam IM A read-writable item map with \c int values, used
43
  /// internally to handle the cross references.
44
  template <typename IM>
45
  class RadixHeap {
46

	
47
  public:
48

	
49
    /// Type of the item-int map.
50
    typedef IM ItemIntMap;
51
    /// Type of the priorities.
52
    typedef int Prio;
53
    /// Type of the items stored in the heap.
54
    typedef typename ItemIntMap::Key Item;
55

	
56
    /// \brief Exception thrown by RadixHeap.
57
    ///
58
    /// This exception is thrown when an item is inserted into a
59
    /// RadixHeap with a priority smaller than the last erased one.
60
    /// \see RadixHeap
61
    class PriorityUnderflowError : public Exception {
62
    public:
63
      virtual const char* what() const throw() {
64
        return "lemon::RadixHeap::PriorityUnderflowError";
65
      }
66
    };
67

	
68
    /// \brief Type to represent the states of the items.
69
    ///
70
    /// Each item has a state associated to it. It can be "in heap",
71
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
72
    /// heap's point of view, but may be useful to the user.
73
    ///
74
    /// The item-int map must be initialized in such way that it assigns
75
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
76
    enum State {
77
      IN_HEAP = 0,    ///< = 0.
78
      PRE_HEAP = -1,  ///< = -1.
79
      POST_HEAP = -2  ///< = -2.
80
    };
81

	
82
  private:
83

	
84
    struct RadixItem {
85
      int prev, next, box;
86
      Item item;
87
      int prio;
88
      RadixItem(Item _item, int _prio) : item(_item), prio(_prio) {}
89
    };
90

	
91
    struct RadixBox {
92
      int first;
93
      int min, size;
94
      RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {}
95
    };
96

	
97
    std::vector<RadixItem> _data;
98
    std::vector<RadixBox> _boxes;
99

	
100
    ItemIntMap &_iim;
101

	
102
  public:
103

	
104
    /// \brief Constructor.
105
    ///
106
    /// Constructor.
107
    /// \param map A map that assigns \c int values to the items.
108
    /// It is used internally to handle the cross references.
109
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
110
    /// \param minimum The initial minimum value of the heap.
111
    /// \param capacity The initial capacity of the heap.
112
    RadixHeap(ItemIntMap &map, int minimum = 0, int capacity = 0)
113
      : _iim(map)
114
    {
115
      _boxes.push_back(RadixBox(minimum, 1));
116
      _boxes.push_back(RadixBox(minimum + 1, 1));
117
      while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
118
        extend();
119
      }
120
    }
121

	
122
    /// \brief The number of items stored in the heap.
123
    ///
124
    /// This function returns the number of items stored in the heap.
125
    int size() const { return _data.size(); }
126

	
127
    /// \brief Check if the heap is empty.
128
    ///
129
    /// This function returns \c true if the heap is empty.
130
    bool empty() const { return _data.empty(); }
131

	
132
    /// \brief Make the heap empty.
133
    ///
134
    /// This functon makes the heap empty.
135
    /// It does not change the cross reference map. If you want to reuse
136
    /// a heap that is not surely empty, you should first clear it and
137
    /// then you should set the cross reference map to \c PRE_HEAP
138
    /// for each item.
139
    /// \param minimum The minimum value of the heap.
140
    /// \param capacity The capacity of the heap.
141
    void clear(int minimum = 0, int capacity = 0) {
142
      _data.clear(); _boxes.clear();
143
      _boxes.push_back(RadixBox(minimum, 1));
144
      _boxes.push_back(RadixBox(minimum + 1, 1));
145
      while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
146
        extend();
147
      }
148
    }
149

	
150
  private:
151

	
152
    bool upper(int box, Prio pr) {
153
      return pr < _boxes[box].min;
154
    }
155

	
156
    bool lower(int box, Prio pr) {
157
      return pr >= _boxes[box].min + _boxes[box].size;
158
    }
159

	
160
    // Remove item from the box list
161
    void remove(int index) {
162
      if (_data[index].prev >= 0) {
163
        _data[_data[index].prev].next = _data[index].next;
164
      } else {
165
        _boxes[_data[index].box].first = _data[index].next;
166
      }
167
      if (_data[index].next >= 0) {
168
        _data[_data[index].next].prev = _data[index].prev;
169
      }
170
    }
171

	
172
    // Insert item into the box list
173
    void insert(int box, int index) {
174
      if (_boxes[box].first == -1) {
175
        _boxes[box].first = index;
176
        _data[index].next = _data[index].prev = -1;
177
      } else {
178
        _data[index].next = _boxes[box].first;
179
        _data[_boxes[box].first].prev = index;
180
        _data[index].prev = -1;
181
        _boxes[box].first = index;
182
      }
183
      _data[index].box = box;
184
    }
185

	
186
    // Add a new box to the box list
187
    void extend() {
188
      int min = _boxes.back().min + _boxes.back().size;
189
      int bs = 2 * _boxes.back().size;
190
      _boxes.push_back(RadixBox(min, bs));
191
    }
192

	
193
    // Move an item up into the proper box.
194
    void bubbleUp(int index) {
195
      if (!lower(_data[index].box, _data[index].prio)) return;
196
      remove(index);
197
      int box = findUp(_data[index].box, _data[index].prio);
198
      insert(box, index);
199
    }
200

	
201
    // Find up the proper box for the item with the given priority
202
    int findUp(int start, int pr) {
203
      while (lower(start, pr)) {
204
        if (++start == int(_boxes.size())) {
205
          extend();
206
        }
207
      }
208
      return start;
209
    }
210

	
211
    // Move an item down into the proper box
212
    void bubbleDown(int index) {
213
      if (!upper(_data[index].box, _data[index].prio)) return;
214
      remove(index);
215
      int box = findDown(_data[index].box, _data[index].prio);
216
      insert(box, index);
217
    }
218

	
219
    // Find down the proper box for the item with the given priority
220
    int findDown(int start, int pr) {
221
      while (upper(start, pr)) {
222
        if (--start < 0) throw PriorityUnderflowError();
223
      }
224
      return start;
225
    }
226

	
227
    // Find the first non-empty box
228
    int findFirst() {
229
      int first = 0;
230
      while (_boxes[first].first == -1) ++first;
231
      return first;
232
    }
233

	
234
    // Gives back the minimum priority of the given box
235
    int minValue(int box) {
236
      int min = _data[_boxes[box].first].prio;
237
      for (int k = _boxes[box].first; k != -1; k = _data[k].next) {
238
        if (_data[k].prio < min) min = _data[k].prio;
239
      }
240
      return min;
241
    }
242

	
243
    // Rearrange the items of the heap and make the first box non-empty
244
    void moveDown() {
245
      int box = findFirst();
246
      if (box == 0) return;
247
      int min = minValue(box);
248
      for (int i = 0; i <= box; ++i) {
249
        _boxes[i].min = min;
250
        min += _boxes[i].size;
251
      }
252
      int curr = _boxes[box].first, next;
253
      while (curr != -1) {
254
        next = _data[curr].next;
255
        bubbleDown(curr);
256
        curr = next;
257
      }
258
    }
259

	
260
    void relocateLast(int index) {
261
      if (index != int(_data.size()) - 1) {
262
        _data[index] = _data.back();
263
        if (_data[index].prev != -1) {
264
          _data[_data[index].prev].next = index;
265
        } else {
266
          _boxes[_data[index].box].first = index;
267
        }
268
        if (_data[index].next != -1) {
269
          _data[_data[index].next].prev = index;
270
        }
271
        _iim[_data[index].item] = index;
272
      }
273
      _data.pop_back();
274
    }
275

	
276
  public:
277

	
278
    /// \brief Insert an item into the heap with the given priority.
279
    ///
280
    /// This function inserts the given item into the heap with the
281
    /// given priority.
282
    /// \param i The item to insert.
283
    /// \param p The priority of the item.
284
    /// \pre \e i must not be stored in the heap.
285
    /// \warning This method may throw an \c UnderFlowPriorityException.
286
    void push(const Item &i, const Prio &p) {
287
      int n = _data.size();
288
      _iim.set(i, n);
289
      _data.push_back(RadixItem(i, p));
290
      while (lower(_boxes.size() - 1, p)) {
291
        extend();
292
      }
293
      int box = findDown(_boxes.size() - 1, p);
294
      insert(box, n);
295
    }
296

	
297
    /// \brief Return the item having minimum priority.
298
    ///
299
    /// This function returns the item having minimum priority.
300
    /// \pre The heap must be non-empty.
301
    Item top() const {
302
      const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
303
      return _data[_boxes[0].first].item;
304
    }
305

	
306
    /// \brief The minimum priority.
307
    ///
308
    /// This function returns the minimum priority.
309
    /// \pre The heap must be non-empty.
310
    Prio prio() const {
311
      const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
312
      return _data[_boxes[0].first].prio;
313
     }
314

	
315
    /// \brief Remove the item having minimum priority.
316
    ///
317
    /// This function removes the item having minimum priority.
318
    /// \pre The heap must be non-empty.
319
    void pop() {
320
      moveDown();
321
      int index = _boxes[0].first;
322
      _iim[_data[index].item] = POST_HEAP;
323
      remove(index);
324
      relocateLast(index);
325
    }
326

	
327
    /// \brief Remove the given item from the heap.
328
    ///
329
    /// This function removes the given item from the heap if it is
330
    /// already stored.
331
    /// \param i The item to delete.
332
    /// \pre \e i must be in the heap.
333
    void erase(const Item &i) {
334
      int index = _iim[i];
335
      _iim[i] = POST_HEAP;
336
      remove(index);
337
      relocateLast(index);
338
   }
339

	
340
    /// \brief The priority of the given item.
341
    ///
342
    /// This function returns the priority of the given item.
343
    /// \param i The item.
344
    /// \pre \e i must be in the heap.
345
    Prio operator[](const Item &i) const {
346
      int idx = _iim[i];
347
      return _data[idx].prio;
348
    }
349

	
350
    /// \brief Set the priority of an item or insert it, if it is
351
    /// not stored in the heap.
352
    ///
353
    /// This method sets the priority of the given item if it is
354
    /// already stored in the heap. Otherwise it inserts the given
355
    /// item into the heap with the given priority.
356
    /// \param i The item.
357
    /// \param p The priority.
358
    /// \pre \e i must be in the heap.
359
    /// \warning This method may throw an \c UnderFlowPriorityException.
360
    void set(const Item &i, const Prio &p) {
361
      int idx = _iim[i];
362
      if( idx < 0 ) {
363
        push(i, p);
364
      }
365
      else if( p >= _data[idx].prio ) {
366
        _data[idx].prio = p;
367
        bubbleUp(idx);
368
      } else {
369
        _data[idx].prio = p;
370
        bubbleDown(idx);
371
      }
372
    }
373

	
374
    /// \brief Decrease the priority of an item to the given value.
375
    ///
376
    /// This function decreases the priority of an item to the given value.
377
    /// \param i The item.
378
    /// \param p The priority.
379
    /// \pre \e i must be stored in the heap with priority at least \e p.
380
    /// \warning This method may throw an \c UnderFlowPriorityException.
381
    void decrease(const Item &i, const Prio &p) {
382
      int idx = _iim[i];
383
      _data[idx].prio = p;
384
      bubbleDown(idx);
385
    }
386

	
387
    /// \brief Increase the priority of an item to the given value.
388
    ///
389
    /// This function increases the priority of an item to the given value.
390
    /// \param i The item.
391
    /// \param p The priority.
392
    /// \pre \e i must be stored in the heap with priority at most \e p.
393
    void increase(const Item &i, const Prio &p) {
394
      int idx = _iim[i];
395
      _data[idx].prio = p;
396
      bubbleUp(idx);
397
    }
398

	
399
    /// \brief Return the state of an item.
400
    ///
401
    /// This method returns \c PRE_HEAP if the given item has never
402
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
403
    /// and \c POST_HEAP otherwise.
404
    /// In the latter case it is possible that the item will get back
405
    /// to the heap again.
406
    /// \param i The item.
407
    State state(const Item &i) const {
408
      int s = _iim[i];
409
      if( s >= 0 ) s = 0;
410
      return State(s);
411
    }
412

	
413
    /// \brief Set the state of an item in the heap.
414
    ///
415
    /// This function sets the state of the given item in the heap.
416
    /// It can be used to manually clear the heap when it is important
417
    /// to achive better time complexity.
418
    /// \param i The item.
419
    /// \param st The state. It should not be \c IN_HEAP.
420
    void state(const Item& i, State st) {
421
      switch (st) {
422
      case POST_HEAP:
423
      case PRE_HEAP:
424
        if (state(i) == IN_HEAP) {
425
          erase(i);
426
        }
427
        _iim[i] = st;
428
        break;
429
      case IN_HEAP:
430
        break;
431
      }
432
    }
433

	
434
  }; // class RadixHeap
435

	
436
} // namespace lemon
437

	
438
#endif // LEMON_RADIX_HEAP_H
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_STATIC_GRAPH_H
20
#define LEMON_STATIC_GRAPH_H
21

	
22
///\ingroup graphs
23
///\file
24
///\brief StaticDigraph class.
25

	
26
#include <lemon/core.h>
27
#include <lemon/bits/graph_extender.h>
28

	
29
namespace lemon {
30

	
31
  class StaticDigraphBase {
32
  public:
33

	
34
    StaticDigraphBase() 
35
      : built(false), node_num(0), arc_num(0), 
36
        node_first_out(NULL), node_first_in(NULL),
37
        arc_source(NULL), arc_target(NULL), 
38
        arc_next_in(NULL), arc_next_out(NULL) {}
39
    
40
    ~StaticDigraphBase() {
41
      if (built) {
42
        delete[] node_first_out;
43
        delete[] node_first_in;
44
        delete[] arc_source;
45
        delete[] arc_target;
46
        delete[] arc_next_out;
47
        delete[] arc_next_in;
48
      }
49
    }
50

	
51
    class Node {
52
      friend class StaticDigraphBase;
53
    protected:
54
      int id;
55
      Node(int _id) : id(_id) {}
56
    public:
57
      Node() {}
58
      Node (Invalid) : id(-1) {}
59
      bool operator==(const Node& node) const { return id == node.id; }
60
      bool operator!=(const Node& node) const { return id != node.id; }
61
      bool operator<(const Node& node) const { return id < node.id; }
62
    };
63

	
64
    class Arc {
65
      friend class StaticDigraphBase;      
66
    protected:
67
      int id;
68
      Arc(int _id) : id(_id) {}
69
    public:
70
      Arc() { }
71
      Arc (Invalid) : id(-1) {}
72
      bool operator==(const Arc& arc) const { return id == arc.id; }
73
      bool operator!=(const Arc& arc) const { return id != arc.id; }
74
      bool operator<(const Arc& arc) const { return id < arc.id; }
75
    };
76

	
77
    Node source(const Arc& e) const { return Node(arc_source[e.id]); }
78
    Node target(const Arc& e) const { return Node(arc_target[e.id]); }
79

	
80
    void first(Node& n) const { n.id = node_num - 1; }
81
    static void next(Node& n) { --n.id; }
82

	
83
    void first(Arc& e) const { e.id = arc_num - 1; }
84
    static void next(Arc& e) { --e.id; }
85

	
86
    void firstOut(Arc& e, const Node& n) const { 
87
      e.id = node_first_out[n.id] != node_first_out[n.id + 1] ? 
88
        node_first_out[n.id] : -1;
89
    }
90
    void nextOut(Arc& e) const { e.id = arc_next_out[e.id]; }
91

	
92
    void firstIn(Arc& e, const Node& n) const { e.id = node_first_in[n.id]; }
93
    void nextIn(Arc& e) const { e.id = arc_next_in[e.id]; }
94

	
95
    static int id(const Node& n) { return n.id; }
96
    static Node nodeFromId(int id) { return Node(id); }
97
    int maxNodeId() const { return node_num - 1; }
98

	
99
    static int id(const Arc& e) { return e.id; }
100
    static Arc arcFromId(int id) { return Arc(id); }
101
    int maxArcId() const { return arc_num - 1; }
102

	
103
    typedef True NodeNumTag;
104
    typedef True ArcNumTag;
105

	
106
    int nodeNum() const { return node_num; }
107
    int arcNum() const { return arc_num; }
108

	
109
  private:
110

	
111
    template <typename Digraph, typename NodeRefMap>
112
    class ArcLess {
113
    public:
114
      typedef typename Digraph::Arc Arc;
115

	
116
      ArcLess(const Digraph &_graph, const NodeRefMap& _nodeRef) 
117
        : digraph(_graph), nodeRef(_nodeRef) {}
118
      
119
      bool operator()(const Arc& left, const Arc& right) const {
120
	return nodeRef[digraph.target(left)] < nodeRef[digraph.target(right)];
121
      }
122
    private:
123
      const Digraph& digraph;
124
      const NodeRefMap& nodeRef;
125
    };
126
    
127
  public:
128

	
129
    typedef True BuildTag;
130
    
131
    void clear() {
132
      if (built) {
133
        delete[] node_first_out;
134
        delete[] node_first_in;
135
        delete[] arc_source;
136
        delete[] arc_target;
137
        delete[] arc_next_out;
138
        delete[] arc_next_in;
139
      }
140
      built = false;
141
      node_num = 0;
142
      arc_num = 0;
143
    }
144
    
145
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
146
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
147
      typedef typename Digraph::Node GNode;
148
      typedef typename Digraph::Arc GArc;
149

	
150
      built = true;
151

	
152
      node_num = countNodes(digraph);
153
      arc_num = countArcs(digraph);
154

	
155
      node_first_out = new int[node_num + 1];
156
      node_first_in = new int[node_num];
157

	
158
      arc_source = new int[arc_num];
159
      arc_target = new int[arc_num];
160
      arc_next_out = new int[arc_num];
161
      arc_next_in = new int[arc_num];
162

	
163
      int node_index = 0;
164
      for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
165
        nodeRef[n] = Node(node_index);
166
        node_first_in[node_index] = -1;
167
        ++node_index;
168
      }
169

	
170
      ArcLess<Digraph, NodeRefMap> arcLess(digraph, nodeRef);
171

	
172
      int arc_index = 0;
173
      for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
174
        int source = nodeRef[n].id;
175
        std::vector<GArc> arcs;
176
        for (typename Digraph::OutArcIt e(digraph, n); e != INVALID; ++e) {
177
          arcs.push_back(e);
178
        }
179
        if (!arcs.empty()) {
180
          node_first_out[source] = arc_index;
181
          std::sort(arcs.begin(), arcs.end(), arcLess);
182
          for (typename std::vector<GArc>::iterator it = arcs.begin();
183
               it != arcs.end(); ++it) {
184
            int target = nodeRef[digraph.target(*it)].id;
185
            arcRef[*it] = Arc(arc_index);
186
            arc_source[arc_index] = source; 
187
            arc_target[arc_index] = target;
188
            arc_next_in[arc_index] = node_first_in[target];
189
            node_first_in[target] = arc_index;
190
            arc_next_out[arc_index] = arc_index + 1;
191
            ++arc_index;
192
          }
193
          arc_next_out[arc_index - 1] = -1;
194
        } else {
195
          node_first_out[source] = arc_index;
196
        }
197
      }
198
      node_first_out[node_num] = arc_num;
199
    }
200
    
201
    template <typename ArcListIterator>
202
    void build(int n, ArcListIterator first, ArcListIterator last) {
203
      built = true;
204

	
205
      node_num = n;
206
      arc_num = std::distance(first, last);
207

	
208
      node_first_out = new int[node_num + 1];
209
      node_first_in = new int[node_num];
210

	
211
      arc_source = new int[arc_num];
212
      arc_target = new int[arc_num];
213
      arc_next_out = new int[arc_num];
214
      arc_next_in = new int[arc_num];
215
      
216
      for (int i = 0; i != node_num; ++i) {
217
        node_first_in[i] = -1;
218
      }      
219
      
220
      int arc_index = 0;
221
      for (int i = 0; i != node_num; ++i) {
222
        node_first_out[i] = arc_index;
223
        for ( ; first != last && (*first).first == i; ++first) {
224
          int j = (*first).second;
225
          LEMON_ASSERT(j >= 0 && j < node_num,
226
            "Wrong arc list for StaticDigraph::build()");
227
          arc_source[arc_index] = i;
228
          arc_target[arc_index] = j;
229
          arc_next_in[arc_index] = node_first_in[j];
230
          node_first_in[j] = arc_index;
231
          arc_next_out[arc_index] = arc_index + 1;
232
          ++arc_index;
233
        }
234
        if (arc_index > node_first_out[i])
235
          arc_next_out[arc_index - 1] = -1;
236
      }
237
      LEMON_ASSERT(first == last,
238
        "Wrong arc list for StaticDigraph::build()");
239
      node_first_out[node_num] = arc_num;
240
    }
241

	
242
  protected:
243

	
244
    void fastFirstOut(Arc& e, const Node& n) const {
245
      e.id = node_first_out[n.id];
246
    }
247

	
248
    static void fastNextOut(Arc& e) {
249
      ++e.id;
250
    }
251
    void fastLastOut(Arc& e, const Node& n) const {
252
      e.id = node_first_out[n.id + 1];
253
    }
254

	
255
  protected:
256
    bool built;
257
    int node_num;
258
    int arc_num;
259
    int *node_first_out;
260
    int *node_first_in;
261
    int *arc_source;
262
    int *arc_target;
263
    int *arc_next_in;
264
    int *arc_next_out;
265
  };
266

	
267
  typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase;
268

	
269

	
270
  /// \ingroup graphs
271
  ///
272
  /// \brief A static directed graph class.
273
  ///
274
  /// \ref StaticDigraph is a highly efficient digraph implementation,
275
  /// but it is fully static.
276
  /// It stores only two \c int values for each node and only four \c int
277
  /// values for each arc. Moreover it provides faster item iteration than
278
  /// \ref ListDigraph and \ref SmartDigraph, especially using \c OutArcIt
279
  /// iterators, since its arcs are stored in an appropriate order.
280
  /// However it only provides build() and clear() functions and does not
281
  /// support any other modification of the digraph.
282
  ///
283
  /// Since this digraph structure is completely static, its nodes and arcs
284
  /// can be indexed with integers from the ranges <tt>[0..nodeNum()-1]</tt>
285
  /// and <tt>[0..arcNum()-1]</tt>, respectively. 
286
  /// The index of an item is the same as its ID, it can be obtained
287
  /// using the corresponding \ref index() or \ref concepts::Digraph::id()
288
  /// "id()" function. A node or arc with a certain index can be obtained
289
  /// using node() or arc().
290
  ///
291
  /// This type fully conforms to the \ref concepts::Digraph "Digraph concept".
292
  /// Most of its member functions and nested classes are documented
293
  /// only in the concept class.
294
  ///
295
  /// \sa concepts::Digraph
296
  class StaticDigraph : public ExtendedStaticDigraphBase {
297
  public:
298

	
299
    typedef ExtendedStaticDigraphBase Parent;
300
  
301
  public:
302
  
303
    /// \brief Constructor
304
    ///
305
    /// Default constructor.
306
    StaticDigraph() : Parent() {}
307

	
308
    /// \brief The node with the given index.
309
    ///
310
    /// This function returns the node with the given index.
311
    /// \sa index()
312
    static Node node(int ix) { return Parent::nodeFromId(ix); }
313

	
314
    /// \brief The arc with the given index.
315
    ///
316
    /// This function returns the arc with the given index.
317
    /// \sa index()
318
    static Arc arc(int ix) { return Parent::arcFromId(ix); }
319

	
320
    /// \brief The index of the given node.
321
    ///
322
    /// This function returns the index of the the given node.
323
    /// \sa node()
324
    static int index(Node node) { return Parent::id(node); }
325

	
326
    /// \brief The index of the given arc.
327
    ///
328
    /// This function returns the index of the the given arc.
329
    /// \sa arc()
330
    static int index(Arc arc) { return Parent::id(arc); }
331

	
332
    /// \brief Number of nodes.
333
    ///
334
    /// This function returns the number of nodes.
335
    int nodeNum() const { return node_num; }
336

	
337
    /// \brief Number of arcs.
338
    ///
339
    /// This function returns the number of arcs.
340
    int arcNum() const { return arc_num; }
341

	
342
    /// \brief Build the digraph copying another digraph.
343
    ///
344
    /// This function builds the digraph copying another digraph of any
345
    /// kind. It can be called more than once, but in such case, the whole
346
    /// structure and all maps will be cleared and rebuilt.
347
    ///
348
    /// This method also makes possible to copy a digraph to a StaticDigraph
349
    /// structure using \ref DigraphCopy.
350
    /// 
351
    /// \param digraph An existing digraph to be copied.
352
    /// \param nodeRef The node references will be copied into this map.
353
    /// Its key type must be \c Digraph::Node and its value type must be
354
    /// \c StaticDigraph::Node.
355
    /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap"
356
    /// concept.
357
    /// \param arcRef The arc references will be copied into this map.
358
    /// Its key type must be \c Digraph::Arc and its value type must be
359
    /// \c StaticDigraph::Arc.
360
    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
361
    ///
362
    /// \note If you do not need the arc references, then you could use
363
    /// \ref NullMap for the last parameter. However the node references
364
    /// are required by the function itself, thus they must be readable
365
    /// from the map.
366
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
367
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
368
      if (built) Parent::clear();
369
      Parent::build(digraph, nodeRef, arcRef);
370
    }
371
  
372
    /// \brief Build the digraph from an arc list.
373
    ///
374
    /// This function builds the digraph from the given arc list.
375
    /// It can be called more than once, but in such case, the whole
376
    /// structure and all maps will be cleared and rebuilt.
377
    ///
378
    /// The list of the arcs must be given in the range <tt>[begin, end)</tt>
379
    /// specified by STL compatible itartors whose \c value_type must be
380
    /// <tt>std::pair<int,int></tt>.
381
    /// Each arc must be specified by a pair of integer indices
382
    /// from the range <tt>[0..n-1]</tt>. <i>The pairs must be in a
383
    /// non-decreasing order with respect to their first values.</i>
384
    /// If the k-th pair in the list is <tt>(i,j)</tt>, then
385
    /// <tt>arc(k-1)</tt> will connect <tt>node(i)</tt> to <tt>node(j)</tt>.
386
    ///
387
    /// \param n The number of nodes.
388
    /// \param begin An iterator pointing to the beginning of the arc list.
389
    /// \param end An iterator pointing to the end of the arc list.
390
    ///
391
    /// For example, a simple digraph can be constructed like this.
392
    /// \code
393
    ///   std::vector<std::pair<int,int> > arcs;
394
    ///   arcs.push_back(std::make_pair(0,1));
395
    ///   arcs.push_back(std::make_pair(0,2));
396
    ///   arcs.push_back(std::make_pair(1,3));
397
    ///   arcs.push_back(std::make_pair(1,2));
398
    ///   arcs.push_back(std::make_pair(3,0));
399
    ///   StaticDigraph gr;
400
    ///   gr.build(4, arcs.begin(), arcs.end());
401
    /// \endcode
402
    template <typename ArcListIterator>
403
    void build(int n, ArcListIterator begin, ArcListIterator end) {
404
      if (built) Parent::clear();
405
      StaticDigraphBase::build(n, begin, end);
406
      notifier(Node()).build();
407
      notifier(Arc()).build();
408
    }
409

	
410
    /// \brief Clear the digraph.
411
    ///
412
    /// This function erases all nodes and arcs from the digraph.
413
    void clear() {
414
      Parent::clear();
415
    }
416

	
417
  protected:
418

	
419
    using Parent::fastFirstOut;
420
    using Parent::fastNextOut;
421
    using Parent::fastLastOut;
422
    
423
  public:
424

	
425
    class OutArcIt : public Arc {
426
    public:
427

	
428
      OutArcIt() { }
429

	
430
      OutArcIt(Invalid i) : Arc(i) { }
431

	
432
      OutArcIt(const StaticDigraph& digraph, const Node& node) {
433
	digraph.fastFirstOut(*this, node);
434
	digraph.fastLastOut(last, node);
435
        if (last == *this) *this = INVALID;
436
      }
437

	
438
      OutArcIt(const StaticDigraph& digraph, const Arc& arc) : Arc(arc) {
439
        if (arc != INVALID) {
440
          digraph.fastLastOut(last, digraph.source(arc));
441
        }
442
      }
443

	
444
      OutArcIt& operator++() { 
445
        StaticDigraph::fastNextOut(*this);
446
        if (last == *this) *this = INVALID;
447
        return *this; 
448
      }
449

	
450
    private:
451
      Arc last;
452
    };
453

	
454
    Node baseNode(const OutArcIt &arc) const {
455
      return Parent::source(static_cast<const Arc&>(arc));
456
    }
457

	
458
    Node runningNode(const OutArcIt &arc) const {
459
      return Parent::target(static_cast<const Arc&>(arc));
460
    }
461

	
462
    Node baseNode(const InArcIt &arc) const {
463
      return Parent::target(static_cast<const Arc&>(arc));
464
    }
465

	
466
    Node runningNode(const InArcIt &arc) const {
467
      return Parent::source(static_cast<const Arc&>(arc));
468
    }
469

	
470
  };
471

	
472
}
473

	
474
#endif
Ignore white space 6 line context
1
AC_DEFUN([LX_CHECK_COIN],
2
[
3
  AC_ARG_WITH([coin],
4
AS_HELP_STRING([--with-coin@<:@=PREFIX@:>@], [search for CLP under PREFIX or under the default search paths if PREFIX is not given @<:@default@:>@])
5
AS_HELP_STRING([--without-coin], [disable checking for CLP]),
6
              [], [with_coin=yes])
7

	
8
  AC_ARG_WITH([coin-includedir],
9
AS_HELP_STRING([--with-coin-includedir=DIR], [search for CLP headers in DIR]),
10
              [], [with_coin_includedir=no])
11

	
12
  AC_ARG_WITH([coin-libdir],
13
AS_HELP_STRING([--with-coin-libdir=DIR], [search for CLP libraries in DIR]),
14
              [], [with_coin_libdir=no])
15

	
16
  lx_clp_found=no
17
  if test x"$with_coin" != x"no"; then
18
    AC_MSG_CHECKING([for CLP])
19

	
20
    if test x"$with_coin_includedir" != x"no"; then
21
      CLP_CXXFLAGS="-I$with_coin_includedir"
22
    elif test x"$with_coin" != x"yes"; then
23
      CLP_CXXFLAGS="-I$with_coin/include"
24
    fi
25

	
26
    if test x"$with_coin_libdir" != x"no"; then
27
      CLP_LDFLAGS="-L$with_coin_libdir"
28
    elif test x"$with_coin" != x"yes"; then
29
      CLP_LDFLAGS="-L$with_coin/lib"
30
    fi
31
    CLP_LIBS="-lClp -lCoinUtils -lm"
32

	
33
    lx_save_cxxflags="$CXXFLAGS"
34
    lx_save_ldflags="$LDFLAGS"
35
    lx_save_libs="$LIBS"
36
    CXXFLAGS="$CLP_CXXFLAGS"
37
    LDFLAGS="$CLP_LDFLAGS"
38
    LIBS="$CLP_LIBS"
39

	
40
    lx_clp_test_prog='
41
      #include <coin/ClpModel.hpp>
42

	
43
      int main(int argc, char** argv)
44
      {
45
        ClpModel clp;
46
        return 0;
47
      }'
48

	
49
    AC_LANG_PUSH(C++)
50
    AC_LINK_IFELSE([$lx_clp_test_prog], [lx_clp_found=yes], [lx_clp_found=no])
51
    AC_LANG_POP(C++)
52

	
53
    CXXFLAGS="$lx_save_cxxflags"
54
    LDFLAGS="$lx_save_ldflags"
55
    LIBS="$lx_save_libs"
56

	
57
    if test x"$lx_clp_found" = x"yes"; then
58
      AC_DEFINE([LEMON_HAVE_CLP], [1], [Define to 1 if you have CLP.])
59
      lx_lp_found=yes
60
      AC_DEFINE([LEMON_HAVE_LP], [1], [Define to 1 if you have any LP solver.])
61
      AC_MSG_RESULT([yes])
62
    else
63
      CLP_CXXFLAGS=""
64
      CLP_LDFLAGS=""
65
      CLP_LIBS=""
66
      AC_MSG_RESULT([no])
67
    fi
68
  fi
69
  CLP_LIBS="$CLP_LDFLAGS $CLP_LIBS"
70
  AC_SUBST(CLP_CXXFLAGS)
71
  AC_SUBST(CLP_LIBS)
72
  AM_CONDITIONAL([HAVE_CLP], [test x"$lx_clp_found" = x"yes"])
73

	
74

	
75
  lx_cbc_found=no
76
  if test x"$lx_clp_found" = x"yes"; then
77
    if test x"$with_coin" != x"no"; then
78
      AC_MSG_CHECKING([for CBC])
79

	
80
      if test x"$with_coin_includedir" != x"no"; then
81
        CBC_CXXFLAGS="-I$with_coin_includedir"
82
      elif test x"$with_coin" != x"yes"; then
83
        CBC_CXXFLAGS="-I$with_coin/include"
84
      fi
85

	
86
      if test x"$with_coin_libdir" != x"no"; then
87
        CBC_LDFLAGS="-L$with_coin_libdir"
88
      elif test x"$with_coin" != x"yes"; then
89
        CBC_LDFLAGS="-L$with_coin/lib"
90
      fi
91
      CBC_LIBS="-lOsi -lCbc -lCbcSolver -lClp -lOsiClp -lCoinUtils -lVol -lOsiVol -lCgl -lm -llapack -lblas"
92

	
93
      lx_save_cxxflags="$CXXFLAGS"
94
      lx_save_ldflags="$LDFLAGS"
95
      lx_save_libs="$LIBS"
96
      CXXFLAGS="$CBC_CXXFLAGS"
97
      LDFLAGS="$CBC_LDFLAGS"
98
      LIBS="$CBC_LIBS"
99

	
100
      lx_cbc_test_prog='
101
        #include <coin/CbcModel.hpp>
102

	
103
        int main(int argc, char** argv)
104
        {
105
          CbcModel cbc;
106
          return 0;
107
        }'
108

	
109
      AC_LANG_PUSH(C++)
110
      AC_LINK_IFELSE([$lx_cbc_test_prog], [lx_cbc_found=yes], [lx_cbc_found=no])
111
      AC_LANG_POP(C++)
112

	
113
      CXXFLAGS="$lx_save_cxxflags"
114
      LDFLAGS="$lx_save_ldflags"
115
      LIBS="$lx_save_libs"
116

	
117
      if test x"$lx_cbc_found" = x"yes"; then
118
        AC_DEFINE([LEMON_HAVE_CBC], [1], [Define to 1 if you have CBC.])
119
        lx_lp_found=yes
120
        AC_DEFINE([LEMON_HAVE_LP], [1], [Define to 1 if you have any LP solver.])
121
        lx_mip_found=yes
122
        AC_DEFINE([LEMON_HAVE_MIP], [1], [Define to 1 if you have any MIP solver.])
123
        AC_MSG_RESULT([yes])
124
      else
125
        CBC_CXXFLAGS=""
126
        CBC_LDFLAGS=""
127
        CBC_LIBS=""
128
        AC_MSG_RESULT([no])
129
      fi
130
    fi
131
  fi
132
  CBC_LIBS="$CBC_LDFLAGS $CBC_LIBS"
133
  AC_SUBST(CBC_CXXFLAGS)
134
  AC_SUBST(CBC_LIBS)
135
  AM_CONDITIONAL([HAVE_CBC], [test x"$lx_cbc_found" = x"yes"])
136
])
Ignore white space 6 line context
1
#!/usr/bin/env /usr/local/Python/bin/python2.1
2
"""
3
  BibTeX to Doxygen converter
4
  Usage: python bib2dox.py bibfile.bib > bibfile.dox
5

	
6
  This code is the modification of the BibTeX to XML converter
7
  by Vidar Bronken Gundersen et al. See the original copyright notices below. 
8

	
9
  **********************************************************************
10

	
11
  Decoder for bibliographic data, BibTeX
12
  Usage: python bibtex2xml.py bibfile.bib > bibfile.xml
13

	
14
  v.8
15
  (c)2002-06-23 Vidar Bronken Gundersen
16
  http://bibtexml.sf.net/
17
  Reuse approved as long as this notification is kept.
18
  Licence: GPL.
19

	
20
  Contributions/thanks to:
21
  Egon Willighagen, http://sf.net/projects/jreferences/
22
  Richard Mahoney (for providing a test case)
23

	
24
  Editted by Sara Sprenkle to be more robust and handle more bibtex features.
25
  (c) 2003-01-15
26

	
27
  1.  Changed bibtex: tags to bibxml: tags.
28
  2.  Use xmlns:bibxml="http://bibtexml.sf.net/"
29
  3.  Allow spaces between @type and first {
30
  4.  "author" fields with multiple authors split by " and "
31
      are put in separate xml "bibxml:author" tags.
32
  5.  Option for Titles: words are capitalized
33
      only if first letter in title or capitalized inside braces
34
  6.  Removes braces from within field values
35
  7.  Ignores comments in bibtex file (including @comment{ or % )
36
  8.  Replaces some special latex tags, e.g., replaces ~ with '&#160;'
37
  9.  Handles bibtex @string abbreviations
38
        --> includes bibtex's default abbreviations for months
39
        --> does concatenation of abbr # " more " and " more " # abbr
40
  10. Handles @type( ... ) or @type{ ... }
41
  11. The keywords field is split on , or ; and put into separate xml
42
      "bibxml:keywords" tags
43
  12. Ignores @preamble
44

	
45
  Known Limitations
46
  1.  Does not transform Latex encoding like math mode and special
47
      latex symbols.
48
  2.  Does not parse author fields into first and last names.
49
      E.g., It does not do anything special to an author whose name is
50
      in the form LAST_NAME, FIRST_NAME
51
      In "author" tag, will show up as
52
      <bibxml:author>LAST_NAME, FIRST_NAME</bibxml:author>
53
  3.  Does not handle "crossref" fields other than to print
54
      <bibxml:crossref>...</bibxml:crossref>
55
  4.  Does not inform user of the input's format errors.  You just won't
56
      be able to transform the file later with XSL
57

	
58
  You will have to manually edit the XML output if you need to handle
59
  these (and unknown) limitations.
60

	
61
"""
62

	
63
import string, re
64

	
65
# set of valid name characters
66
valid_name_chars = '[\w\-:]'
67

	
68
#
69
# define global regular expression variables
70
#
71
author_rex = re.compile('\s+and\s+')
72
rembraces_rex = re.compile('[{}]')
73
capitalize_rex = re.compile('({[^}]*})')
74

	
75
# used by bibtexkeywords(data)
76
keywords_rex = re.compile('[,;]')
77

	
78
# used by concat_line(line)
79
concatsplit_rex = re.compile('\s*#\s*')
80

	
81
# split on {, }, or " in verify_out_of_braces
82
delimiter_rex = re.compile('([{}"])',re.I)
83

	
84
field_rex = re.compile('\s*(\w*)\s*=\s*(.*)')
85
data_rex = re.compile('\s*(\w*)\s*=\s*([^,]*),?')
86

	
87
url_rex = re.compile('\\\url\{([^}]*)\}')
88

	
89
#
90
# styles for html formatting
91
#
92
divstyle = 'margin-top: -4ex; margin-left: 8em;'
93

	
94
#
95
# return the string parameter without braces
96
#
97
def transformurls(str):
98
    return url_rex.sub(r'<a href="\1">\1</a>', str)
99

	
100
#
101
# return the string parameter without braces
102
#
103
def removebraces(str):
104
    return rembraces_rex.sub('', str)
105

	
106
#
107
# latex-specific replacements
108
# (do this after braces were removed)
109
#
110
def latexreplacements(line):
111
    line = string.replace(line, '~', '&nbsp;')
112
    line = string.replace(line, '\\\'a', '&aacute;')
113
    line = string.replace(line, '\\"a', '&auml;')
114
    line = string.replace(line, '\\\'e', '&eacute;')
115
    line = string.replace(line, '\\"e', '&euml;')
116
    line = string.replace(line, '\\\'i', '&iacute;')
117
    line = string.replace(line, '\\"i', '&iuml;')
118
    line = string.replace(line, '\\\'o', '&oacute;')
119
    line = string.replace(line, '\\"o', '&ouml;')
120
    line = string.replace(line, '\\\'u', '&uacute;')
121
    line = string.replace(line, '\\"u', '&uuml;')
122
    line = string.replace(line, '\\H o', '&otilde;')
123
    line = string.replace(line, '\\H u', '&uuml;')   # &utilde; does not exist
124
    line = string.replace(line, '\\\'A', '&Aacute;')
125
    line = string.replace(line, '\\"A', '&Auml;')
126
    line = string.replace(line, '\\\'E', '&Eacute;')
127
    line = string.replace(line, '\\"E', '&Euml;')
128
    line = string.replace(line, '\\\'I', '&Iacute;')
129
    line = string.replace(line, '\\"I', '&Iuml;')
130
    line = string.replace(line, '\\\'O', '&Oacute;')
131
    line = string.replace(line, '\\"O', '&Ouml;')
132
    line = string.replace(line, '\\\'U', '&Uacute;')
133
    line = string.replace(line, '\\"U', '&Uuml;')
134
    line = string.replace(line, '\\H O', '&Otilde;')
135
    line = string.replace(line, '\\H U', '&Uuml;')   # &Utilde; does not exist
136

	
137
    return line
138

	
139
#
140
# copy characters form a string decoding html expressions (&xyz;)
141
#
142
def copychars(str, ifrom, count):
143
    result = ''
144
    i = ifrom
145
    c = 0
146
    html_spec = False
147
    while (i < len(str)) and (c < count):
148
        if str[i] == '&':
149
            html_spec = True;
150
            if i+1 < len(str):
151
                result += str[i+1]
152
            c += 1
153
            i += 2
154
        else:
155
            if not html_spec:
156
                if ((str[i] >= 'A') and (str[i] <= 'Z')) or \
157
                   ((str[i] >= 'a') and (str[i] <= 'z')):
158
                    result += str[i]
159
                    c += 1
160
            elif str[i] == ';':
161
                html_spec = False;
162
            i += 1
163
    
164
    return result
165

	
166

	
167
# 
168
# Handle a list of authors (separated by 'and').
169
# It gives back an array of the follwing values:
170
#  - num: the number of authors,
171
#  - list: the list of the author names,
172
#  - text: the bibtex text (separated by commas and/or 'and')
173
#  - abbrev: abbreviation that can be used for indicate the
174
#    bibliography entries
175
#
176
def bibtexauthor(data):
177
    result = {}
178
    bibtex = ''
179
    result['list'] = author_rex.split(data)
180
    result['num'] = len(result['list'])
181
    for i, author in enumerate(result['list']):
182
        # general transformations
183
        author = latexreplacements(removebraces(author.strip()))
184
        # transform "Xyz, A. B." to "A. B. Xyz"
185
        pos = author.find(',')
186
        if pos != -1:
187
            author = author[pos+1:].strip() + ' ' + author[:pos].strip()
188
        result['list'][i] = author
189
        bibtex += author + '#'
190
    bibtex = bibtex[:-1]
191
    if result['num'] > 1:
192
        ix = bibtex.rfind('#')
193
        if result['num'] == 2:
194
            bibtex = bibtex[:ix] + ' and ' + bibtex[ix+1:]
195
        else:
196
            bibtex = bibtex[:ix] + ', and ' + bibtex[ix+1:]
197
    bibtex = bibtex.replace('#', ', ')
198
    result['text'] = bibtex
199
    
200
    result['abbrev'] = ''
201
    for author in result['list']:
202
        pos = author.rfind(' ') + 1
203
        count = 1
204
        if result['num'] == 1:
205
            count = 3
206
        result['abbrev'] += copychars(author, pos, count)
207

	
208
    return result
209

	
210

	
211
#
212
# data = title string
213
# @return the capitalized title (first letter is capitalized), rest are capitalized
214
# only if capitalized inside braces
215
#
216
def capitalizetitle(data):
217
    title_list = capitalize_rex.split(data)
218
    title = ''
219
    count = 0
220
    for phrase in title_list:
221
         check = string.lstrip(phrase)
222

	
223
         # keep phrase's capitalization the same
224
         if check.find('{') == 0:
225
              title += removebraces(phrase)
226
         else:
227
         # first word --> capitalize first letter (after spaces)
228
              if count == 0:
229
                  title += check.capitalize()
230
              else:
231
                  title += phrase.lower()
232
         count = count + 1
233

	
234
    return title
235

	
236

	
237
#
238
# @return the bibtex for the title
239
# @param data --> title string
240
# braces are removed from title
241
#
242
def bibtextitle(data, entrytype):
243
    if entrytype in ('book', 'inbook'):
244
        title = removebraces(data.strip())
245
    else:
246
        title = removebraces(capitalizetitle(data.strip()))
247
    bibtex = title
248
    return bibtex
249

	
250

	
251
#
252
# function to compare entry lists
253
#
254
def entry_cmp(x, y):
255
    return cmp(x[0], y[0])
256

	
257

	
258
#
259
# print the XML for the transformed "filecont_source"
260
#
261
def bibtexdecoder(filecont_source):
262
    filecont = []
263
    file = []
264
    
265
    # want @<alphanumeric chars><spaces>{<spaces><any chars>,
266
    pubtype_rex = re.compile('@(\w*)\s*{\s*(.*),')
267
    endtype_rex = re.compile('}\s*$')
268
    endtag_rex = re.compile('^\s*}\s*$')
269

	
270
    bracefield_rex = re.compile('\s*(\w*)\s*=\s*(.*)')
271
    bracedata_rex = re.compile('\s*(\w*)\s*=\s*{(.*)},?')
272

	
273
    quotefield_rex = re.compile('\s*(\w*)\s*=\s*(.*)')
274
    quotedata_rex = re.compile('\s*(\w*)\s*=\s*"(.*)",?')
275

	
276
    for line in filecont_source:
277
        line = line[:-1]
278

	
279
        # encode character entities
280
        line = string.replace(line, '&', '&amp;')
281
        line = string.replace(line, '<', '&lt;')
282
        line = string.replace(line, '>', '&gt;')
283

	
284
        # start entry: publication type (store for later use)
285
        if pubtype_rex.match(line):
286
        # want @<alphanumeric chars><spaces>{<spaces><any chars>,
287
            entrycont = {}
288
            entry = []
289
            entrytype = pubtype_rex.sub('\g<1>',line)
290
            entrytype = string.lower(entrytype)
291
            entryid   = pubtype_rex.sub('\g<2>', line)
292

	
293
        # end entry if just a }
294
        elif endtype_rex.match(line):
295
            # generate doxygen code for the entry
296

	
297
            # enty type related formattings
298
            if entrytype in ('book', 'inbook'):
299
                entrycont['title'] = '<em>' + entrycont['title'] + '</em>'
300
                if not entrycont.has_key('author'):
301
                    entrycont['author'] = entrycont['editor']
302
                    entrycont['author']['text'] += ', editors'
303
            elif entrytype == 'article':
304
                entrycont['journal'] = '<em>' + entrycont['journal'] + '</em>'
305
            elif entrytype in ('inproceedings', 'incollection', 'conference'):
306
                entrycont['booktitle'] = '<em>' + entrycont['booktitle'] + '</em>'
307
            elif entrytype == 'techreport':
308
                if not entrycont.has_key('type'):
309
                    entrycont['type'] = 'Technical report'
310
            elif entrytype == 'mastersthesis':
311
                entrycont['type'] = 'Master\'s thesis'
312
            elif entrytype == 'phdthesis':
313
                entrycont['type'] = 'PhD thesis'
314

	
315
            for eline in entrycont:
316
                if eline != '':
317
                    eline = latexreplacements(eline)
318

	
319
            if entrycont.has_key('pages') and (entrycont['pages'] != ''):
320
                entrycont['pages'] = string.replace(entrycont['pages'], '--', '-')
321

	
322
            if entrycont.has_key('author') and (entrycont['author'] != ''):
323
                entry.append(entrycont['author']['text'] + '.')
324
            if entrycont.has_key('title') and (entrycont['title'] != ''):
325
                entry.append(entrycont['title'] + '.')
326
            if entrycont.has_key('journal') and (entrycont['journal'] != ''):
327
                entry.append(entrycont['journal'] + ',')
328
            if entrycont.has_key('booktitle') and (entrycont['booktitle'] != ''):
329
                entry.append('In ' + entrycont['booktitle'] + ',')
330
            if entrycont.has_key('type') and (entrycont['type'] != ''):
331
                eline = entrycont['type']
332
                if entrycont.has_key('number') and (entrycont['number'] != ''):
333
                    eline += ' ' + entrycont['number']
334
                eline += ','
335
                entry.append(eline)
336
            if entrycont.has_key('institution') and (entrycont['institution'] != ''):
337
                entry.append(entrycont['institution'] + ',')
338
            if entrycont.has_key('publisher') and (entrycont['publisher'] != ''):
339
                entry.append(entrycont['publisher'] + ',')
340
            if entrycont.has_key('school') and (entrycont['school'] != ''):
341
                entry.append(entrycont['school'] + ',')
342
            if entrycont.has_key('address') and (entrycont['address'] != ''):
343
                entry.append(entrycont['address'] + ',')
344
            if entrycont.has_key('edition') and (entrycont['edition'] != ''):
345
                entry.append(entrycont['edition'] + ' edition,')
346
            if entrycont.has_key('howpublished') and (entrycont['howpublished'] != ''):
347
                entry.append(entrycont['howpublished'] + ',')
348
            if entrycont.has_key('volume') and (entrycont['volume'] != ''):
349
                eline = entrycont['volume'];
350
                if entrycont.has_key('number') and (entrycont['number'] != ''):
351
                    eline += '(' + entrycont['number'] + ')'
352
                if entrycont.has_key('pages') and (entrycont['pages'] != ''):
353
                    eline += ':' + entrycont['pages']
354
                eline += ','
355
                entry.append(eline)
356
            else:
357
                if entrycont.has_key('pages') and (entrycont['pages'] != ''):
358
                    entry.append('pages ' + entrycont['pages'] + ',')
359
            if entrycont.has_key('year') and (entrycont['year'] != ''):
360
                if entrycont.has_key('month') and (entrycont['month'] != ''):
361
                    entry.append(entrycont['month'] + ' ' + entrycont['year'] + '.')
362
                else:
363
                    entry.append(entrycont['year'] + '.')
364
            if entrycont.has_key('note') and (entrycont['note'] != ''):
365
                entry.append(entrycont['note'] + '.')
366
            if entrycont.has_key('url') and (entrycont['url'] != ''):
367
                entry.append(entrycont['url'] + '.')
368

	
369
            # generate keys for sorting and for the output
370
            sortkey = ''
371
            bibkey = ''
372
            if entrycont.has_key('author'):
373
                for author in entrycont['author']['list']:
374
                    sortkey += copychars(author, author.rfind(' ')+1, len(author))
375
                bibkey = entrycont['author']['abbrev']
376
            else:
377
                bibkey = 'x'
378
            if entrycont.has_key('year'):
379
                sortkey += entrycont['year']
380
                bibkey += entrycont['year'][-2:]
381
            if entrycont.has_key('title'):
382
                sortkey += entrycont['title']
383
            if entrycont.has_key('key'):
384
                sortkey = entrycont['key'] + sortkey
385
                bibkey = entrycont['key']
386
            entry.insert(0, sortkey)
387
            entry.insert(1, bibkey)
388
            entry.insert(2, entryid)
389
           
390
            # add the entry to the file contents
391
            filecont.append(entry)
392

	
393
        else:
394
            # field, publication info
395
            field = ''
396
            data = ''
397
            
398
            # field = {data} entries
399
            if bracedata_rex.match(line):
400
                field = bracefield_rex.sub('\g<1>', line)
401
                field = string.lower(field)
402
                data =  bracedata_rex.sub('\g<2>', line)
403

	
404
            # field = "data" entries
405
            elif quotedata_rex.match(line):
406
                field = quotefield_rex.sub('\g<1>', line)
407
                field = string.lower(field)
408
                data =  quotedata_rex.sub('\g<2>', line)
409

	
410
            # field = data entries
411
            elif data_rex.match(line):
412
                field = field_rex.sub('\g<1>', line)
413
                field = string.lower(field)
414
                data =  data_rex.sub('\g<2>', line)
415

	
416
            if field == 'url':
417
                data = '\\url{' + data.strip() + '}'
418
            
419
            if field in ('author', 'editor'):
420
                entrycont[field] = bibtexauthor(data)
421
                line = ''
422
            elif field == 'title':
423
                line = bibtextitle(data, entrytype)
424
            elif field != '':
425
                line = removebraces(transformurls(data.strip()))
426

	
427
            if line != '':
428
                line = latexreplacements(line)
429
                entrycont[field] = line
430

	
431

	
432
    # sort entries
433
    filecont.sort(entry_cmp)
434
    
435
    # count the bibtex keys
436
    keytable = {}
437
    counttable = {}
438
    for entry in filecont:
439
        bibkey = entry[1]
440
        if not keytable.has_key(bibkey):
441
            keytable[bibkey] = 1
442
        else:
443
            keytable[bibkey] += 1
444

	
445
    for bibkey in keytable.keys():
446
        counttable[bibkey] = 0
447
    
448
    # generate output
449
    for entry in filecont:
450
        # generate output key form the bibtex key
451
        bibkey = entry[1]
452
        entryid = entry[2]
453
        if keytable[bibkey] == 1:
454
            outkey = bibkey
455
        else:
456
            outkey = bibkey + chr(97 + counttable[bibkey])
457
        counttable[bibkey] += 1
458
        
459
        # append the entry code to the output
460
        file.append('\\section ' + entryid + ' [' + outkey + ']')
461
        file.append('<div style="' + divstyle + '">')
462
        for line in entry[3:]:
463
            file.append(line)
464
        file.append('</div>')
465
        file.append('')
466

	
467
    return file
468

	
469

	
470
#
471
# return 1 iff abbr is in line but not inside braces or quotes
472
# assumes that abbr appears only once on the line (out of braces and quotes)
473
#
474
def verify_out_of_braces(line, abbr):
475

	
476
    phrase_split = delimiter_rex.split(line)
477

	
478
    abbr_rex = re.compile( '\\b' + abbr + '\\b', re.I)
479

	
480
    open_brace = 0
481
    open_quote = 0
482

	
483
    for phrase in phrase_split:
484
        if phrase == "{":
485
            open_brace = open_brace + 1
486
        elif phrase == "}":
487
            open_brace = open_brace - 1
488
        elif phrase == '"':
489
            if open_quote == 1:
490
                open_quote = 0
491
            else:
492
                open_quote = 1
493
        elif abbr_rex.search(phrase):
494
            if open_brace == 0 and open_quote == 0:
495
                return 1
496

	
497
    return 0
498

	
499

	
500
#
501
# a line in the form phrase1 # phrase2 # ... # phrasen
502
# is returned as phrase1 phrase2 ... phrasen
503
# with the correct punctuation
504
# Bug: Doesn't always work with multiple abbreviations plugged in
505
#
506
def concat_line(line):
507
    # only look at part after equals
508
    field = field_rex.sub('\g<1>',line)
509
    rest = field_rex.sub('\g<2>',line)
510

	
511
    concat_line = field + ' ='
512

	
513
    pound_split = concatsplit_rex.split(rest)
514

	
515
    phrase_count = 0
516
    length = len(pound_split)
517

	
518
    for phrase in pound_split:
519
        phrase = phrase.strip()
520
        if phrase_count != 0:
521
            if phrase.startswith('"') or phrase.startswith('{'):
522
                phrase = phrase[1:]
523
        elif phrase.startswith('"'):
524
            phrase = phrase.replace('"','{',1)
525

	
526
        if phrase_count != length-1:
527
            if phrase.endswith('"') or phrase.endswith('}'):
528
                phrase = phrase[:-1]
529
        else:
530
            if phrase.endswith('"'):
531
                phrase = phrase[:-1]
532
                phrase = phrase + "}"
533
            elif phrase.endswith('",'):
534
                phrase = phrase[:-2]
535
                phrase = phrase + "},"
536

	
537
        # if phrase did have \#, add the \# back
538
        if phrase.endswith('\\'):
539
            phrase = phrase + "#"
540
        concat_line = concat_line + ' ' + phrase
541

	
542
        phrase_count = phrase_count + 1
543

	
544
    return concat_line
545

	
546

	
547
#
548
# substitute abbreviations into filecont
549
# @param filecont_source - string of data from file
550
#
551
def bibtex_replace_abbreviations(filecont_source):
552
    filecont = filecont_source.splitlines()
553

	
554
    #  These are defined in bibtex, so we'll define them too
555
    abbr_list = ['jan','feb','mar','apr','may','jun',
556
                 'jul','aug','sep','oct','nov','dec']
557
    value_list = ['January','February','March','April',
558
                  'May','June','July','August','September',
559
                  'October','November','December']
560

	
561
    abbr_rex = []
562
    total_abbr_count = 0
563

	
564
    front = '\\b'
565
    back = '(,?)\\b'
566

	
567
    for x in abbr_list:
568
        abbr_rex.append( re.compile( front + abbr_list[total_abbr_count] + back, re.I ) )
569
        total_abbr_count = total_abbr_count + 1
570

	
571

	
572
    abbrdef_rex = re.compile('\s*@string\s*{\s*('+ valid_name_chars +'*)\s*=(.*)',
573
                             re.I)
574

	
575
    comment_rex = re.compile('@comment\s*{',re.I)
576
    preamble_rex = re.compile('@preamble\s*{',re.I)
577

	
578
    waiting_for_end_string = 0
579
    i = 0
580
    filecont2 = ''
581

	
582
    for line in filecont:
583
        if line == ' ' or line == '':
584
            continue
585

	
586
        if waiting_for_end_string:
587
            if re.search('}',line):
588
                waiting_for_end_string = 0
589
                continue
590

	
591
        if abbrdef_rex.search(line):
592
            abbr = abbrdef_rex.sub('\g<1>', line)
593

	
594
            if abbr_list.count(abbr) == 0:
595
                val = abbrdef_rex.sub('\g<2>', line)
596
                abbr_list.append(abbr)
597
                value_list.append(string.strip(val))
598
                abbr_rex.append( re.compile( front + abbr_list[total_abbr_count] + back, re.I ) )
599
                total_abbr_count = total_abbr_count + 1
600
            waiting_for_end_string = 1
601
            continue
602

	
603
        if comment_rex.search(line):
604
            waiting_for_end_string = 1
605
            continue
606

	
607
        if preamble_rex.search(line):
608
            waiting_for_end_string = 1
609
            continue
610

	
611

	
612
        # replace subsequent abbreviations with the value
613
        abbr_count = 0
614

	
615
        for x in abbr_list:
616

	
617
            if abbr_rex[abbr_count].search(line):
618
                if verify_out_of_braces(line,abbr_list[abbr_count]) == 1:
619
                    line = abbr_rex[abbr_count].sub( value_list[abbr_count] + '\g<1>', line)
620
                # Check for # concatenations
621
                if concatsplit_rex.search(line):
622
                    line = concat_line(line)
623
            abbr_count = abbr_count + 1
624

	
625

	
626
        filecont2 = filecont2 + line + '\n'
627
        i = i+1
628

	
629

	
630
    # Do one final pass over file
631

	
632
    # make sure that didn't end up with {" or }" after the substitution
633
    filecont2 = filecont2.replace('{"','{{')
634
    filecont2 = filecont2.replace('"}','}}')
635

	
636
    afterquotevalue_rex = re.compile('"\s*,\s*')
637
    afterbrace_rex = re.compile('"\s*}')
638
    afterbracevalue_rex = re.compile('(=\s*{[^=]*)},\s*')
639

	
640
    # add new lines to data that changed because of abbreviation substitutions
641
    filecont2 = afterquotevalue_rex.sub('",\n', filecont2)
642
    filecont2 = afterbrace_rex.sub('"\n}', filecont2)
643
    filecont2 = afterbracevalue_rex.sub('\g<1>},\n', filecont2)
644

	
645
    return filecont2
646

	
647
#
648
# convert @type( ... ) to @type{ ... }
649
#
650
def no_outer_parens(filecont):
651

	
652
    # do checking for open parens
653
    # will convert to braces
654
    paren_split = re.split('([(){}])',filecont)
655

	
656
    open_paren_count = 0
657
    open_type = 0
658
    look_next = 0
659

	
660
    # rebuild filecont
661
    filecont = ''
662

	
663
    at_rex = re.compile('@\w*')
664

	
665
    for phrase in paren_split:
666
        if look_next == 1:
667
            if phrase == '(':
668
                phrase = '{'
669
                open_paren_count = open_paren_count + 1
670
            else:
671
                open_type = 0
672
            look_next = 0
673

	
674
        if phrase == '(':
675
            open_paren_count = open_paren_count + 1
676

	
677
        elif phrase == ')':
678
            open_paren_count = open_paren_count - 1
679
            if open_type == 1 and open_paren_count == 0:
680
                phrase = '}'
681
                open_type = 0
682

	
683
        elif at_rex.search( phrase ):
684
            open_type = 1
685
            look_next = 1
686

	
687
        filecont = filecont + phrase
688

	
689
    return filecont
690

	
691

	
692
#
693
# make all whitespace into just one space
694
# format the bibtex file into a usable form.
695
#
696
def bibtexwasher(filecont_source):
697

	
698
    space_rex = re.compile('\s+')
699
    comment_rex = re.compile('\s*%')
700

	
701
    filecont = []
702

	
703
    # remove trailing and excessive whitespace
704
    # ignore comments
705
    for line in filecont_source:
706
        line = string.strip(line)
707
        line = space_rex.sub(' ', line)
708
        # ignore comments
709
        if not comment_rex.match(line) and line != '':
710
            filecont.append(' '+ line)
711

	
712
    filecont = string.join(filecont, '')
713

	
714
    # the file is in one long string
715

	
716
    filecont = no_outer_parens(filecont)
717

	
718
    #
719
    # split lines according to preferred syntax scheme
720
    #
721
    filecont = re.sub('(=\s*{[^=]*)},', '\g<1>},\n', filecont)
722

	
723
    # add new lines after commas that are after values
724
    filecont = re.sub('"\s*,', '",\n', filecont)
725
    filecont = re.sub('=\s*([\w\d]+)\s*,', '= \g<1>,\n', filecont)
726
    filecont = re.sub('(@\w*)\s*({(\s*)[^,\s]*)\s*,',
727
                          '\n\n\g<1>\g<2>,\n', filecont)
728

	
729
    # add new lines after }
730
    filecont = re.sub('"\s*}','"\n}\n', filecont)
731
    filecont = re.sub('}\s*,','},\n', filecont)
732

	
733

	
734
    filecont = re.sub('@(\w*)', '\n@\g<1>', filecont)
735

	
736
    # character encoding, reserved latex characters
737
    filecont = re.sub('{\\\&}', '&', filecont)
738
    filecont = re.sub('\\\&', '&', filecont)
739

	
740
    # do checking for open braces to get format correct
741
    open_brace_count = 0
742
    brace_split = re.split('([{}])',filecont)
743

	
744
    # rebuild filecont
745
    filecont = ''
746

	
747
    for phrase in brace_split:
748
        if phrase == '{':
749
            open_brace_count = open_brace_count + 1
750
        elif phrase == '}':
751
            open_brace_count = open_brace_count - 1
752
            if open_brace_count == 0:
753
                filecont = filecont + '\n'
754

	
755
        filecont = filecont + phrase
756

	
757
    filecont2 = bibtex_replace_abbreviations(filecont)
758

	
759
    # gather
760
    filecont = filecont2.splitlines()
761
    i=0
762
    j=0         # count the number of blank lines
763
    for line in filecont:
764
        # ignore blank lines
765
        if line == '' or line == ' ':
766
            j = j+1
767
            continue
768
        filecont[i] = line + '\n'
769
        i = i+1
770

	
771
    # get rid of the extra stuff at the end of the array
772
    # (The extra stuff are duplicates that are in the array because
773
    # blank lines were removed.)
774
    length = len( filecont)
775
    filecont[length-j:length] = []
776

	
777
    return filecont
778

	
779

	
780
def filehandler(filepath):
781
    try:
782
        fd = open(filepath, 'r')
783
        filecont_source = fd.readlines()
784
        fd.close()
785
    except:
786
        print 'Could not open file:', filepath
787
    washeddata = bibtexwasher(filecont_source)
788
    outdata = bibtexdecoder(washeddata)
789
    print '/**'
790
    print '\page references References'
791
    print
792
    for line in outdata:
793
        print line
794
    print '*/'
795

	
796

	
797
# main program
798

	
799
def main():
800
    import sys
801
    if sys.argv[1:]:
802
        filepath = sys.argv[1]
803
    else:
804
        print "No input file"
805
        sys.exit()
806
    filehandler(filepath)
807

	
808
if __name__ == "__main__": main()
809

	
810

	
811
# end python script
Ignore white space 6 line context
1
#!/bin/bash
2
#
3
# This file is a part of LEMON, a generic C++ optimization library.
4
#
5
# Copyright (C) 2003-2009
6
# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
# (Egervary Research Group on Combinatorial Optimization, EGRES).
8
#
9
# Permission to use, modify and distribute this software is granted
10
# provided that this copyright notice appears in all copies. For
11
# precise terms see the accompanying LICENSE file.
12
#
13
# This software is provided "AS IS" with no warranty of any kind,
14
# express or implied, and with no claim as to its suitability for any
15
# purpose.
16

	
17

	
18
if [ ! -f ~/.lemon-bootstrap ]; then
19
    echo 'Create ~/.lemon-bootstrap'.
20
    cat >~/.lemon-bootstrap <<EOF
21
#
22
# Default settings for bootstraping the LEMON source code repository
23
#
24
EOF
25
fi
26

	
27
source ~/.lemon-bootstrap
28
if [ -f ../../../.lemon-bootstrap ]; then source ../../../.lemon-bootstrap; fi
29
if [ -f ../../.lemon-bootstrap ]; then source ../../.lemon-bootstrap; fi
30
if [ -f ../.lemon-bootstrap ]; then source ../.lemon-bootstrap; fi
31
if [ -f ./.lemon-bootstrap ]; then source ./.lemon-bootstrap; fi
32

	
33

	
34
function augment_config() { 
35
    if [ "x${!1}" == "x" ]; then
36
        eval $1=$2
37
        echo Add "'$1'" to '~/.lemon-bootstrap'.
38
        echo >>~/.lemon-bootstrap
39
        echo $3 >>~/.lemon-bootstrap
40
        echo $1=$2 >>~/.lemon-bootstrap
41
    fi
42
}
43

	
44
augment_config LEMON_INSTALL_PREFIX /usr/local \
45
    "# LEMON installation prefix"
46

	
47
augment_config COIN_OR_PREFIX /usr/local/coin-or \
48
    "# COIN-OR installation root prefix (used for CLP/CBC)"
49

	
50
augment_config SOPLEX_PREFIX /usr/local/soplex \
51
    "# Soplex build prefix"
52

	
53

	
54
function ask() {
55
echo -n "$1 [$2]? "
56
read _an
57
if [ "x$_an" == "x" ]; then
58
    ret="$2"
59
else
60
    ret=$_an
61
fi
62
}
63

	
64
function yesorno() {
65
    ret='rossz'
66
    while [ "$ret" != "y" -a "$ret" != "n" -a "$ret" != "yes" -a "$ret" != "no" ]; do
67
        ask "$1" "$2"
68
    done
69
    if [ "$ret" != "y" -a "$ret" != "yes" ]; then
70
        return 1
71
    else
72
        return 0
73
    fi
74
}
75

	
76
if yesorno "External build" "n"
77
then
78
    CONFIGURE_PATH=".."
79
else
80
    CONFIGURE_PATH="."
81
    if yesorno "Autoreconf" "y"
82
    then
83
        AUTORE=yes
84
    else
85
        AUTORE=no
86
    fi
87
fi
88

	
89
if yesorno "Optimize" "n" 
90
then
91
    opt_flags=' -O2'
92
else
93
    opt_flags=''
94
fi
95

	
96
if yesorno "Stop on warning" "y" 
97
then
98
    werror_flags=' -Werror'
99
else
100
    werror_flags=''
101
fi
102

	
103
cxx_flags="CXXFLAGS=-ggdb$opt_flags$werror_flags"
104

	
105
if [ -f ${COIN_OR_PREFIX}/include/coin/config_coinutils.h ]; then
106
    if yesorno "Use COIN-OR (CBC/CLP)" "n"
107
    then
108
        coin_flag="--with-coin=$COIN_OR_PREFIX"
109
    else
110
        coin_flag=""
111
    fi
112
else
113
    coin_flag=""        
114
fi
115

	
116
if [ -f ${SOPLEX_PREFIX}/src/soplex.h ]; then
117
    if yesorno "Use Soplex" "n"
118
    then
119
        soplex_flag="--with-soplex=$SOPLEX_PREFIX"
120
    else
121
        soplex_flag=""
122
    fi
123
else
124
    soplex_flag=""
125
fi
126

	
127
if [ "x$AUTORE" == "xyes" ]; then
128
    autoreconf -vif;
129
fi
130
${CONFIGURE_PATH}/configure --prefix=$LEMON_INSTALL_PREFIX \
131
"$cxx_flags" \
132
$coin_flag \
133
$soplex_flag \
134
$*
Ignore white space 6 line context
1
#!/bin/bash
2
#
3
# This file is a part of LEMON, a generic C++ optimization library.
4
#
5
# Copyright (C) 2003-2009
6
# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
# (Egervary Research Group on Combinatorial Optimization, EGRES).
8
#
9
# Permission to use, modify and distribute this software is granted
10
# provided that this copyright notice appears in all copies. For
11
# precise terms see the accompanying LICENSE file.
12
#
13
# This software is provided "AS IS" with no warranty of any kind,
14
# express or implied, and with no claim as to its suitability for any
15
# purpose.
16

	
17
set -e
18

	
19
if [ $# = 0 ]; then
20
    echo "Usage: $0 release-id"
21
    exit 1
22
else
23
    export LEMON_VERSION=$1
24
fi
25

	
26
echo '*****************************************************************'
27
echo ' Start making release tarballs for version '${LEMON_VERSION}
28
echo '*****************************************************************'
29

	
30
autoreconf -vif
31
./configure
32

	
33
make
34
make html
35
make distcheck
36
tar xf lemon-${LEMON_VERSION}.tar.gz
37
zip -r lemon-${LEMON_VERSION}.zip lemon-${LEMON_VERSION}
38
mv lemon-${LEMON_VERSION}/doc/html lemon-doc-${LEMON_VERSION}
39
tar czf lemon-doc-${LEMON_VERSION}.tar.gz lemon-doc-${LEMON_VERSION}
40
zip -r lemon-doc-${LEMON_VERSION}.zip lemon-doc-${LEMON_VERSION}
41
tar czf lemon-nodoc-${LEMON_VERSION}.tar.gz lemon-${LEMON_VERSION}
42
zip -r lemon-nodoc-${LEMON_VERSION}.zip lemon-${LEMON_VERSION}
43
hg tag -m 'LEMON '${LEMON_VERSION}' released ('$(hg par --template="{node|short}")' tagged as r'${LEMON_VERSION}')' r${LEMON_VERSION}
44

	
45
rm -rf lemon-${LEMON_VERSION} lemon-doc-${LEMON_VERSION}
46

	
47
echo '*****************************************************************'
48
echo '  Release '${LEMON_VERSION}' has been created' 
49
echo '*****************************************************************'
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#include <lemon/concepts/digraph.h>
20
#include <lemon/smart_graph.h>
21
#include <lemon/list_graph.h>
22
#include <lemon/lgf_reader.h>
23
#include <lemon/bellman_ford.h>
24
#include <lemon/path.h>
25

	
26
#include "graph_test.h"
27
#include "test_tools.h"
28

	
29
using namespace lemon;
30

	
31
char test_lgf[] =
32
  "@nodes\n"
33
  "label\n"
34
  "0\n"
35
  "1\n"
36
  "2\n"
37
  "3\n"
38
  "4\n"
39
  "@arcs\n"
40
  "    length\n"
41
  "0 1 3\n"
42
  "1 2 -3\n"
43
  "1 2 -5\n"
44
  "1 3 -2\n"
45
  "0 2 -1\n"
46
  "1 2 -4\n"
47
  "0 3 2\n"
48
  "4 2 -5\n"
49
  "2 3 1\n"
50
  "@attributes\n"
51
  "source 0\n"
52
  "target 3\n";
53

	
54

	
55
void checkBellmanFordCompile()
56
{
57
  typedef int Value;
58
  typedef concepts::Digraph Digraph;
59
  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
60
  typedef BellmanFord<Digraph, LengthMap> BF;
61
  typedef Digraph::Node Node;
62
  typedef Digraph::Arc Arc;
63

	
64
  Digraph gr;
65
  Node s, t, n;
66
  Arc e;
67
  Value l;
68
  int k;
69
  bool b;
70
  BF::DistMap d(gr);
71
  BF::PredMap p(gr);
72
  LengthMap length;
73
  concepts::Path<Digraph> pp;
74

	
75
  {
76
    BF bf_test(gr,length);
77
    const BF& const_bf_test = bf_test;
78

	
79
    bf_test.run(s);
80
    bf_test.run(s,k);
81

	
82
    bf_test.init();
83
    bf_test.addSource(s);
84
    bf_test.addSource(s, 1);
85
    b = bf_test.processNextRound();
86
    b = bf_test.processNextWeakRound();
87

	
88
    bf_test.start();
89
    bf_test.checkedStart();
90
    bf_test.limitedStart(k);
91

	
92
    l  = const_bf_test.dist(t);
93
    e  = const_bf_test.predArc(t);
94
    s  = const_bf_test.predNode(t);
95
    b  = const_bf_test.reached(t);
96
    d  = const_bf_test.distMap();
97
    p  = const_bf_test.predMap();
98
    pp = const_bf_test.path(t);
99
    pp = const_bf_test.negativeCycle();
100
    
101
    for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
102
  }
103
  {
104
    BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
105
      ::SetDistMap<concepts::ReadWriteMap<Node,Value> >
106
      ::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> >
107
      ::Create bf_test(gr,length);
108

	
109
    LengthMap length_map;
110
    concepts::ReadWriteMap<Node,Arc> pred_map;
111
    concepts::ReadWriteMap<Node,Value> dist_map;
112
    
113
    bf_test
114
      .lengthMap(length_map)
115
      .predMap(pred_map)
116
      .distMap(dist_map);
117

	
118
    bf_test.run(s);
119
    bf_test.run(s,k);
120

	
121
    bf_test.init();
122
    bf_test.addSource(s);
123
    bf_test.addSource(s, 1);
124
    b = bf_test.processNextRound();
125
    b = bf_test.processNextWeakRound();
126

	
127
    bf_test.start();
128
    bf_test.checkedStart();
129
    bf_test.limitedStart(k);
130

	
131
    l  = bf_test.dist(t);
132
    e  = bf_test.predArc(t);
133
    s  = bf_test.predNode(t);
134
    b  = bf_test.reached(t);
135
    pp = bf_test.path(t);
136
    pp = bf_test.negativeCycle();
137
  }
138
}
139

	
140
void checkBellmanFordFunctionCompile()
141
{
142
  typedef int Value;
143
  typedef concepts::Digraph Digraph;
144
  typedef Digraph::Arc Arc;
145
  typedef Digraph::Node Node;
146
  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
147

	
148
  Digraph g;
149
  bool b;
150
  bellmanFord(g,LengthMap()).run(Node());
151
  b = bellmanFord(g,LengthMap()).run(Node(),Node());
152
  bellmanFord(g,LengthMap())
153
    .predMap(concepts::ReadWriteMap<Node,Arc>())
154
    .distMap(concepts::ReadWriteMap<Node,Value>())
155
    .run(Node());
156
  b=bellmanFord(g,LengthMap())
157
    .predMap(concepts::ReadWriteMap<Node,Arc>())
158
    .distMap(concepts::ReadWriteMap<Node,Value>())
159
    .path(concepts::Path<Digraph>())
160
    .dist(Value())
161
    .run(Node(),Node());
162
}
163

	
164

	
165
template <typename Digraph, typename Value>
166
void checkBellmanFord() {
167
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
168
  typedef typename Digraph::template ArcMap<Value> LengthMap;
169

	
170
  Digraph gr;
171
  Node s, t;
172
  LengthMap length(gr);
173

	
174
  std::istringstream input(test_lgf);
175
  digraphReader(gr, input).
176
    arcMap("length", length).
177
    node("source", s).
178
    node("target", t).
179
    run();
180

	
181
  BellmanFord<Digraph, LengthMap>
182
    bf(gr, length);
183
  bf.run(s);
184
  Path<Digraph> p = bf.path(t);
185

	
186
  check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path.");
187
  check(p.length() == 3, "path() found a wrong path.");
188
  check(checkPath(gr, p), "path() found a wrong path.");
189
  check(pathSource(gr, p) == s, "path() found a wrong path.");
190
  check(pathTarget(gr, p) == t, "path() found a wrong path.");
191
  
192
  ListPath<Digraph> path;
193
  Value dist;
194
  bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t);
195

	
196
  check(reached && dist == -1, "Bellman-Ford found a wrong path.");
197
  check(path.length() == 3, "path() found a wrong path.");
198
  check(checkPath(gr, path), "path() found a wrong path.");
199
  check(pathSource(gr, path) == s, "path() found a wrong path.");
200
  check(pathTarget(gr, path) == t, "path() found a wrong path.");
201

	
202
  for(ArcIt e(gr); e!=INVALID; ++e) {
203
    Node u=gr.source(e);
204
    Node v=gr.target(e);
205
    check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]),
206
          "Wrong output. dist(target)-dist(source)-arc_length=" <<
207
          bf.dist(v) - bf.dist(u) - length[e]);
208
  }
209

	
210
  for(NodeIt v(gr); v!=INVALID; ++v) {
211
    if (bf.reached(v)) {
212
      check(v==s || bf.predArc(v)!=INVALID, "Wrong tree.");
213
      if (bf.predArc(v)!=INVALID ) {
214
        Arc e=bf.predArc(v);
215
        Node u=gr.source(e);
216
        check(u==bf.predNode(v),"Wrong tree.");
217
        check(bf.dist(v) - bf.dist(u) == length[e],
218
              "Wrong distance! Difference: " <<
219
              bf.dist(v) - bf.dist(u) - length[e]);
220
      }
221
    }
222
  }
223
}
224

	
225
void checkBellmanFordNegativeCycle() {
226
  DIGRAPH_TYPEDEFS(SmartDigraph);
227

	
228
  SmartDigraph gr;
229
  IntArcMap length(gr);
230
  
231
  Node n1 = gr.addNode();
232
  Node n2 = gr.addNode();
233
  Node n3 = gr.addNode();
234
  Node n4 = gr.addNode();
235
  
236
  Arc a1 = gr.addArc(n1, n2);
237
  Arc a2 = gr.addArc(n2, n2);
238
  
239
  length[a1] = 2;
240
  length[a2] = -1;
241
  
242
  {
243
    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
244
    bf.run(n1);
245
    StaticPath<SmartDigraph> p = bf.negativeCycle();
246
    check(p.length() == 1 && p.front() == p.back() && p.front() == a2,
247
          "Wrong negative cycle.");
248
  }
249
 
250
  length[a2] = 0;
251
  
252
  {
253
    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
254
    bf.run(n1);
255
    check(bf.negativeCycle().empty(),
256
          "Negative cycle should not be found.");
257
  }
258
  
259
  length[gr.addArc(n1, n3)] = 5;
260
  length[gr.addArc(n4, n3)] = 1;
261
  length[gr.addArc(n2, n4)] = 2;
262
  length[gr.addArc(n3, n2)] = -4;
263
  
264
  {
265
    BellmanFord<SmartDigraph, IntArcMap> bf(gr, length);
266
    bf.init();
267
    bf.addSource(n1);
268
    for (int i = 0; i < 4; ++i) {
269
      check(bf.negativeCycle().empty(),
270
            "Negative cycle should not be found.");
271
      bf.processNextRound();
272
    }
273
    StaticPath<SmartDigraph> p = bf.negativeCycle();
274
    check(p.length() == 3, "Wrong negative cycle.");
275
    check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1,
276
          "Wrong negative cycle.");
277
  }
278
}
279

	
280
int main() {
281
  checkBellmanFord<ListDigraph, int>();
282
  checkBellmanFord<SmartDigraph, double>();
283
  checkBellmanFordNegativeCycle();
284
  return 0;
285
}
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#include <lemon/connectivity.h>
20
#include <lemon/list_graph.h>
21
#include <lemon/adaptors.h>
22

	
23
#include "test_tools.h"
24

	
25
using namespace lemon;
26

	
27

	
28
int main()
29
{
30
  typedef ListDigraph Digraph;
31
  typedef Undirector<Digraph> Graph;
32
  
33
  {
34
    Digraph d;
35
    Digraph::NodeMap<int> order(d);
36
    Graph g(d);
37
    
38
    check(stronglyConnected(d), "The empty digraph is strongly connected");
39
    check(countStronglyConnectedComponents(d) == 0,
40
          "The empty digraph has 0 strongly connected component");
41
    check(connected(g), "The empty graph is connected");
42
    check(countConnectedComponents(g) == 0,
43
          "The empty graph has 0 connected component");
44

	
45
    check(biNodeConnected(g), "The empty graph is bi-node-connected");
46
    check(countBiNodeConnectedComponents(g) == 0,
47
          "The empty graph has 0 bi-node-connected component");
48
    check(biEdgeConnected(g), "The empty graph is bi-edge-connected");
49
    check(countBiEdgeConnectedComponents(g) == 0,
50
          "The empty graph has 0 bi-edge-connected component");
51
          
52
    check(dag(d), "The empty digraph is DAG.");
53
    check(checkedTopologicalSort(d, order), "The empty digraph is DAG.");
54
    check(loopFree(d), "The empty digraph is loop-free.");
55
    check(parallelFree(d), "The empty digraph is parallel-free.");
56
    check(simpleGraph(d), "The empty digraph is simple.");
57

	
58
    check(acyclic(g), "The empty graph is acyclic.");
59
    check(tree(g), "The empty graph is tree.");
60
    check(bipartite(g), "The empty graph is bipartite.");
61
    check(loopFree(g), "The empty graph is loop-free.");
62
    check(parallelFree(g), "The empty graph is parallel-free.");
63
    check(simpleGraph(g), "The empty graph is simple.");
64
  }
65

	
66
  {
67
    Digraph d;
68
    Digraph::NodeMap<int> order(d);
69
    Graph g(d);
70
    Digraph::Node n = d.addNode();
71

	
72
    check(stronglyConnected(d), "This digraph is strongly connected");
73
    check(countStronglyConnectedComponents(d) == 1,
74
          "This digraph has 1 strongly connected component");
75
    check(connected(g), "This graph is connected");
76
    check(countConnectedComponents(g) == 1,
77
          "This graph has 1 connected component");
78

	
79
    check(biNodeConnected(g), "This graph is bi-node-connected");
80
    check(countBiNodeConnectedComponents(g) == 0,
81
          "This graph has 0 bi-node-connected component");
82
    check(biEdgeConnected(g), "This graph is bi-edge-connected");
83
    check(countBiEdgeConnectedComponents(g) == 1,
84
          "This graph has 1 bi-edge-connected component");
85
          
86
    check(dag(d), "This digraph is DAG.");
87
    check(checkedTopologicalSort(d, order), "This digraph is DAG.");
88
    check(loopFree(d), "This digraph is loop-free.");
89
    check(parallelFree(d), "This digraph is parallel-free.");
90
    check(simpleGraph(d), "This digraph is simple.");
91

	
92
    check(acyclic(g), "This graph is acyclic.");
93
    check(tree(g), "This graph is tree.");
94
    check(bipartite(g), "This graph is bipartite.");
95
    check(loopFree(g), "This graph is loop-free.");
96
    check(parallelFree(g), "This graph is parallel-free.");
97
    check(simpleGraph(g), "This graph is simple.");
98
  }
99

	
100
  {
101
    Digraph d;
102
    Digraph::NodeMap<int> order(d);
103
    Graph g(d);
104
    
105
    Digraph::Node n1 = d.addNode();
106
    Digraph::Node n2 = d.addNode();
107
    Digraph::Node n3 = d.addNode();
108
    Digraph::Node n4 = d.addNode();
109
    Digraph::Node n5 = d.addNode();
110
    Digraph::Node n6 = d.addNode();
111
    
112
    d.addArc(n1, n3);
113
    d.addArc(n3, n2);
114
    d.addArc(n2, n1);
115
    d.addArc(n4, n2);
116
    d.addArc(n4, n3);
117
    d.addArc(n5, n6);
118
    d.addArc(n6, n5);
119

	
120
    check(!stronglyConnected(d), "This digraph is not strongly connected");
121
    check(countStronglyConnectedComponents(d) == 3,
122
          "This digraph has 3 strongly connected components");
123
    check(!connected(g), "This graph is not connected");
124
    check(countConnectedComponents(g) == 2,
125
          "This graph has 2 connected components");
126

	
127
    check(!dag(d), "This digraph is not DAG.");
128
    check(!checkedTopologicalSort(d, order), "This digraph is not DAG.");
129
    check(loopFree(d), "This digraph is loop-free.");
130
    check(parallelFree(d), "This digraph is parallel-free.");
131
    check(simpleGraph(d), "This digraph is simple.");
132

	
133
    check(!acyclic(g), "This graph is not acyclic.");
134
    check(!tree(g), "This graph is not tree.");
135
    check(!bipartite(g), "This graph is not bipartite.");
136
    check(loopFree(g), "This graph is loop-free.");
137
    check(!parallelFree(g), "This graph is not parallel-free.");
138
    check(!simpleGraph(g), "This graph is not simple.");
139
    
140
    d.addArc(n3, n3);
141
    
142
    check(!loopFree(d), "This digraph is not loop-free.");
143
    check(!loopFree(g), "This graph is not loop-free.");
144
    check(!simpleGraph(d), "This digraph is not simple.");
145
    
146
    d.addArc(n3, n2);
147
    
148
    check(!parallelFree(d), "This digraph is not parallel-free.");
149
  }
150
  
151
  {
152
    Digraph d;
153
    Digraph::ArcMap<bool> cutarcs(d, false);
154
    Graph g(d);
155
    
156
    Digraph::Node n1 = d.addNode();
157
    Digraph::Node n2 = d.addNode();
158
    Digraph::Node n3 = d.addNode();
159
    Digraph::Node n4 = d.addNode();
160
    Digraph::Node n5 = d.addNode();
161
    Digraph::Node n6 = d.addNode();
162
    Digraph::Node n7 = d.addNode();
163
    Digraph::Node n8 = d.addNode();
164

	
165
    d.addArc(n1, n2);
166
    d.addArc(n5, n1);
167
    d.addArc(n2, n8);
168
    d.addArc(n8, n5);
169
    d.addArc(n6, n4);
170
    d.addArc(n4, n6);
171
    d.addArc(n2, n5);
172
    d.addArc(n1, n8);
173
    d.addArc(n6, n7);
174
    d.addArc(n7, n6);
175
   
176
    check(!stronglyConnected(d), "This digraph is not strongly connected");
177
    check(countStronglyConnectedComponents(d) == 3,
178
          "This digraph has 3 strongly connected components");
179
    Digraph::NodeMap<int> scomp1(d);
180
    check(stronglyConnectedComponents(d, scomp1) == 3,
181
          "This digraph has 3 strongly connected components");
182
    check(scomp1[n1] != scomp1[n3] && scomp1[n1] != scomp1[n4] &&
183
          scomp1[n3] != scomp1[n4], "Wrong stronglyConnectedComponents()");
184
    check(scomp1[n1] == scomp1[n2] && scomp1[n1] == scomp1[n5] &&
185
          scomp1[n1] == scomp1[n8], "Wrong stronglyConnectedComponents()");
186
    check(scomp1[n4] == scomp1[n6] && scomp1[n4] == scomp1[n7],
187
          "Wrong stronglyConnectedComponents()");
188
    Digraph::ArcMap<bool> scut1(d, false);
189
    check(stronglyConnectedCutArcs(d, scut1) == 0,
190
          "This digraph has 0 strongly connected cut arc.");
191
    for (Digraph::ArcIt a(d); a != INVALID; ++a) {
192
      check(!scut1[a], "Wrong stronglyConnectedCutArcs()");
193
    }
194

	
195
    check(!connected(g), "This graph is not connected");
196
    check(countConnectedComponents(g) == 3,
197
          "This graph has 3 connected components");
198
    Graph::NodeMap<int> comp(g);
199
    check(connectedComponents(g, comp) == 3,
200
          "This graph has 3 connected components");
201
    check(comp[n1] != comp[n3] && comp[n1] != comp[n4] &&
202
          comp[n3] != comp[n4], "Wrong connectedComponents()");
203
    check(comp[n1] == comp[n2] && comp[n1] == comp[n5] &&
204
          comp[n1] == comp[n8], "Wrong connectedComponents()");
205
    check(comp[n4] == comp[n6] && comp[n4] == comp[n7],
206
          "Wrong connectedComponents()");
207

	
208
    cutarcs[d.addArc(n3, n1)] = true;
209
    cutarcs[d.addArc(n3, n5)] = true;
210
    cutarcs[d.addArc(n3, n8)] = true;
211
    cutarcs[d.addArc(n8, n6)] = true;
212
    cutarcs[d.addArc(n8, n7)] = true;
213

	
214
    check(!stronglyConnected(d), "This digraph is not strongly connected");
215
    check(countStronglyConnectedComponents(d) == 3,
216
          "This digraph has 3 strongly connected components");
217
    Digraph::NodeMap<int> scomp2(d);
218
    check(stronglyConnectedComponents(d, scomp2) == 3,
219
          "This digraph has 3 strongly connected components");
220
    check(scomp2[n3] == 0, "Wrong stronglyConnectedComponents()");
221
    check(scomp2[n1] == 1 && scomp2[n2] == 1 && scomp2[n5] == 1 &&
222
          scomp2[n8] == 1, "Wrong stronglyConnectedComponents()");
223
    check(scomp2[n4] == 2 && scomp2[n6] == 2 && scomp2[n7] == 2,
224
          "Wrong stronglyConnectedComponents()");
225
    Digraph::ArcMap<bool> scut2(d, false);
226
    check(stronglyConnectedCutArcs(d, scut2) == 5,
227
          "This digraph has 5 strongly connected cut arcs.");
228
    for (Digraph::ArcIt a(d); a != INVALID; ++a) {
229
      check(scut2[a] == cutarcs[a], "Wrong stronglyConnectedCutArcs()");
230
    }
231
  }
232

	
233
  {
234
    // DAG example for topological sort from the book New Algorithms
235
    // (T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein)
236
    Digraph d;
237
    Digraph::NodeMap<int> order(d);
238
    
239
    Digraph::Node belt = d.addNode();
240
    Digraph::Node trousers = d.addNode();
241
    Digraph::Node necktie = d.addNode();
242
    Digraph::Node coat = d.addNode();
243
    Digraph::Node socks = d.addNode();
244
    Digraph::Node shirt = d.addNode();
245
    Digraph::Node shoe = d.addNode();
246
    Digraph::Node watch = d.addNode();
247
    Digraph::Node pants = d.addNode();
248

	
249
    d.addArc(socks, shoe);
250
    d.addArc(pants, shoe);
251
    d.addArc(pants, trousers);
252
    d.addArc(trousers, shoe);
253
    d.addArc(trousers, belt);
254
    d.addArc(belt, coat);
255
    d.addArc(shirt, belt);
256
    d.addArc(shirt, necktie);
257
    d.addArc(necktie, coat);
258
    
259
    check(dag(d), "This digraph is DAG.");
260
    topologicalSort(d, order);
261
    for (Digraph::ArcIt a(d); a != INVALID; ++a) {
262
      check(order[d.source(a)] < order[d.target(a)],
263
            "Wrong topologicalSort()");
264
    }
265
  }
266

	
267
  {
268
    ListGraph g;
269
    ListGraph::NodeMap<bool> map(g);
270
    
271
    ListGraph::Node n1 = g.addNode();
272
    ListGraph::Node n2 = g.addNode();
273
    ListGraph::Node n3 = g.addNode();
274
    ListGraph::Node n4 = g.addNode();
275
    ListGraph::Node n5 = g.addNode();
276
    ListGraph::Node n6 = g.addNode();
277
    ListGraph::Node n7 = g.addNode();
278

	
279
    g.addEdge(n1, n3);
280
    g.addEdge(n1, n4);
281
    g.addEdge(n2, n5);
282
    g.addEdge(n3, n6);
283
    g.addEdge(n4, n6);
284
    g.addEdge(n4, n7);
285
    g.addEdge(n5, n7);
286
   
287
    check(bipartite(g), "This graph is bipartite");
288
    check(bipartitePartitions(g, map), "This graph is bipartite");
289
    
290
    check(map[n1] == map[n2] && map[n1] == map[n6] && map[n1] == map[n7],
291
          "Wrong bipartitePartitions()");
292
    check(map[n3] == map[n4] && map[n3] == map[n5],
293
          "Wrong bipartitePartitions()");
294
  }
295

	
296
  return 0;
297
}
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#include <lemon/euler.h>
20
#include <lemon/list_graph.h>
21
#include <lemon/adaptors.h>
22
#include "test_tools.h"
23

	
24
using namespace lemon;
25

	
26
template <typename Digraph>
27
void checkDiEulerIt(const Digraph& g,
28
                    const typename Digraph::Node& start = INVALID)
29
{
30
  typename Digraph::template ArcMap<int> visitationNumber(g, 0);
31

	
32
  DiEulerIt<Digraph> e(g, start);
33
  if (e == INVALID) return;
34
  typename Digraph::Node firstNode = g.source(e);
35
  typename Digraph::Node lastNode = g.target(e);
36
  if (start != INVALID) {
37
    check(firstNode == start, "checkDiEulerIt: Wrong first node");
38
  }
39

	
40
  for (; e != INVALID; ++e) {
41
    if (e != INVALID) lastNode = g.target(e);
42
    ++visitationNumber[e];
43
  }
44

	
45
  check(firstNode == lastNode,
46
      "checkDiEulerIt: First and last nodes are not the same");
47

	
48
  for (typename Digraph::ArcIt a(g); a != INVALID; ++a)
49
  {
50
    check(visitationNumber[a] == 1,
51
        "checkDiEulerIt: Not visited or multiple times visited arc found");
52
  }
53
}
54

	
55
template <typename Graph>
56
void checkEulerIt(const Graph& g,
57
                  const typename Graph::Node& start = INVALID)
58
{
59
  typename Graph::template EdgeMap<int> visitationNumber(g, 0);
60

	
61
  EulerIt<Graph> e(g, start);
62
  if (e == INVALID) return;
63
  typename Graph::Node firstNode = g.source(typename Graph::Arc(e));
64
  typename Graph::Node lastNode = g.target(typename Graph::Arc(e));
65
  if (start != INVALID) {
66
    check(firstNode == start, "checkEulerIt: Wrong first node");
67
  }
68

	
69
  for (; e != INVALID; ++e) {
70
    if (e != INVALID) lastNode = g.target(typename Graph::Arc(e));
71
    ++visitationNumber[e];
72
  }
73

	
74
  check(firstNode == lastNode,
75
      "checkEulerIt: First and last nodes are not the same");
76

	
77
  for (typename Graph::EdgeIt e(g); e != INVALID; ++e)
78
  {
79
    check(visitationNumber[e] == 1,
80
        "checkEulerIt: Not visited or multiple times visited edge found");
81
  }
82
}
83

	
84
int main()
85
{
86
  typedef ListDigraph Digraph;
87
  typedef Undirector<Digraph> Graph;
88
  
89
  {
90
    Digraph d;
91
    Graph g(d);
92
    
93
    checkDiEulerIt(d);
94
    checkDiEulerIt(g);
95
    checkEulerIt(g);
96

	
97
    check(eulerian(d), "This graph is Eulerian");
98
    check(eulerian(g), "This graph is Eulerian");
99
  }
100
  {
101
    Digraph d;
102
    Graph g(d);
103
    Digraph::Node n = d.addNode();
104

	
105
    checkDiEulerIt(d);
106
    checkDiEulerIt(g);
107
    checkEulerIt(g);
108

	
109
    check(eulerian(d), "This graph is Eulerian");
110
    check(eulerian(g), "This graph is Eulerian");
111
  }
112
  {
113
    Digraph d;
114
    Graph g(d);
115
    Digraph::Node n = d.addNode();
116
    d.addArc(n, n);
117

	
118
    checkDiEulerIt(d);
119
    checkDiEulerIt(g);
120
    checkEulerIt(g);
121

	
122
    check(eulerian(d), "This graph is Eulerian");
123
    check(eulerian(g), "This graph is Eulerian");
124
  }
125
  {
126
    Digraph d;
127
    Graph g(d);
128
    Digraph::Node n1 = d.addNode();
129
    Digraph::Node n2 = d.addNode();
130
    Digraph::Node n3 = d.addNode();
131
    
132
    d.addArc(n1, n2);
133
    d.addArc(n2, n1);
134
    d.addArc(n2, n3);
135
    d.addArc(n3, n2);
136

	
137
    checkDiEulerIt(d);
138
    checkDiEulerIt(d, n2);
139
    checkDiEulerIt(g);
140
    checkDiEulerIt(g, n2);
141
    checkEulerIt(g);
142
    checkEulerIt(g, n2);
143

	
144
    check(eulerian(d), "This graph is Eulerian");
145
    check(eulerian(g), "This graph is Eulerian");
146
  }
147
  {
148
    Digraph d;
149
    Graph g(d);
150
    Digraph::Node n1 = d.addNode();
151
    Digraph::Node n2 = d.addNode();
152
    Digraph::Node n3 = d.addNode();
153
    Digraph::Node n4 = d.addNode();
154
    Digraph::Node n5 = d.addNode();
155
    Digraph::Node n6 = d.addNode();
156
    
157
    d.addArc(n1, n2);
158
    d.addArc(n2, n4);
159
    d.addArc(n1, n3);
160
    d.addArc(n3, n4);
161
    d.addArc(n4, n1);
162
    d.addArc(n3, n5);
163
    d.addArc(n5, n2);
164
    d.addArc(n4, n6);
165
    d.addArc(n2, n6);
166
    d.addArc(n6, n1);
167
    d.addArc(n6, n3);
168

	
169
    checkDiEulerIt(d);
170
    checkDiEulerIt(d, n1);
171
    checkDiEulerIt(d, n5);
172

	
173
    checkDiEulerIt(g);
174
    checkDiEulerIt(g, n1);
175
    checkDiEulerIt(g, n5);
176
    checkEulerIt(g);
177
    checkEulerIt(g, n1);
178
    checkEulerIt(g, n5);
179

	
180
    check(eulerian(d), "This graph is Eulerian");
181
    check(eulerian(g), "This graph is Eulerian");
182
  }
183
  {
184
    Digraph d;
185
    Graph g(d);
186
    Digraph::Node n0 = d.addNode();
187
    Digraph::Node n1 = d.addNode();
188
    Digraph::Node n2 = d.addNode();
189
    Digraph::Node n3 = d.addNode();
190
    Digraph::Node n4 = d.addNode();
191
    Digraph::Node n5 = d.addNode();
192
    
193
    d.addArc(n1, n2);
194
    d.addArc(n2, n3);
195
    d.addArc(n3, n1);
196

	
197
    checkDiEulerIt(d);
198
    checkDiEulerIt(d, n2);
199

	
200
    checkDiEulerIt(g);
201
    checkDiEulerIt(g, n2);
202
    checkEulerIt(g);
203
    checkEulerIt(g, n2);
204

	
205
    check(!eulerian(d), "This graph is not Eulerian");
206
    check(!eulerian(g), "This graph is not Eulerian");
207
  }
208
  {
209
    Digraph d;
210
    Graph g(d);
211
    Digraph::Node n1 = d.addNode();
212
    Digraph::Node n2 = d.addNode();
213
    Digraph::Node n3 = d.addNode();
214
    
215
    d.addArc(n1, n2);
216
    d.addArc(n2, n3);
217

	
218
    check(!eulerian(d), "This graph is not Eulerian");
219
    check(!eulerian(g), "This graph is not Eulerian");
220
  }
221

	
222
  return 0;
223
}
Ignore white space 6 line context
1
#include <iostream>
2

	
3
#include "test_tools.h"
4
#include <lemon/smart_graph.h>
5
#include <lemon/concepts/graph.h>
6
#include <lemon/concepts/maps.h>
7
#include <lemon/lgf_reader.h>
8
#include <lemon/gomory_hu.h>
9
#include <cstdlib>
10

	
11
using namespace std;
12
using namespace lemon;
13

	
14
typedef SmartGraph Graph;
15

	
16
char test_lgf[] =
17
  "@nodes\n"
18
  "label\n"
19
  "0\n"
20
  "1\n"
21
  "2\n"
22
  "3\n"
23
  "4\n"
24
  "@arcs\n"
25
  "     label capacity\n"
26
  "0 1  0     1\n"
27
  "1 2  1     1\n"
28
  "2 3  2     1\n"
29
  "0 3  4     5\n"
30
  "0 3  5     10\n"
31
  "0 3  6     7\n"
32
  "4 2  7     1\n"
33
  "@attributes\n"
34
  "source 0\n"
35
  "target 3\n";
36
  
37
void checkGomoryHuCompile()
38
{
39
  typedef int Value;
40
  typedef concepts::Graph Graph;
41

	
42
  typedef Graph::Node Node;
43
  typedef Graph::Edge Edge;
44
  typedef concepts::ReadMap<Edge, Value> CapMap;
45
  typedef concepts::ReadWriteMap<Node, bool> CutMap;
46

	
47
  Graph g;
48
  Node n;
49
  CapMap cap;
50
  CutMap cut;
51
  Value v;
52
  int d;
53

	
54
  GomoryHu<Graph, CapMap> gh_test(g, cap);
55
  const GomoryHu<Graph, CapMap>&
56
    const_gh_test = gh_test;
57

	
58
  gh_test.run();
59

	
60
  n = const_gh_test.predNode(n);
61
  v = const_gh_test.predValue(n);
62
  d = const_gh_test.rootDist(n);
63
  v = const_gh_test.minCutValue(n, n);
64
  v = const_gh_test.minCutMap(n, n, cut);
65
}
66

	
67
GRAPH_TYPEDEFS(Graph);
68
typedef Graph::EdgeMap<int> IntEdgeMap;
69
typedef Graph::NodeMap<bool> BoolNodeMap;
70

	
71
int cutValue(const Graph& graph, const BoolNodeMap& cut,
72
	     const IntEdgeMap& capacity) {
73

	
74
  int sum = 0;
75
  for (EdgeIt e(graph); e != INVALID; ++e) {
76
    Node s = graph.u(e);
77
    Node t = graph.v(e);
78

	
79
    if (cut[s] != cut[t]) {
80
      sum += capacity[e];
81
    }
82
  }
83
  return sum;
84
}
85

	
86

	
87
int main() {
88
  Graph graph;
89
  IntEdgeMap capacity(graph);
90

	
91
  std::istringstream input(test_lgf);
92
  GraphReader<Graph>(graph, input).
93
    edgeMap("capacity", capacity).run();
94

	
95
  GomoryHu<Graph> ght(graph, capacity);
96
  ght.run();
97

	
98
  for (NodeIt u(graph); u != INVALID; ++u) {
99
    for (NodeIt v(graph); v != u; ++v) {
100
      Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v);
101
      pf.runMinCut();
102
      BoolNodeMap cm(graph);
103
      ght.minCutMap(u, v, cm);
104
      check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1");
105
      check(cm[u] != cm[v], "Wrong cut 2");
106
      check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 3");
107

	
108
      int sum=0;
109
      for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a)
110
        sum+=capacity[a]; 
111
      check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt");
112

	
113
      sum=0;
114
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n)
115
        sum++;
116
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n)
117
        sum++;
118
      check(sum == countNodes(graph), "Problem with MinCutNodeIt");
119
    }
120
  }
121
  
122
  return 0;
123
}
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#include <iostream>
20
#include <set>
21
#include <vector>
22
#include <iterator>
23

	
24
#include <lemon/smart_graph.h>
25
#include <lemon/min_cost_arborescence.h>
26
#include <lemon/lgf_reader.h>
27
#include <lemon/concepts/digraph.h>
28

	
29
#include "test_tools.h"
30

	
31
using namespace lemon;
32
using namespace std;
33

	
34
const char test_lgf[] =
35
  "@nodes\n"
36
  "label\n"
37
  "0\n"
38
  "1\n"
39
  "2\n"
40
  "3\n"
41
  "4\n"
42
  "5\n"
43
  "6\n"
44
  "7\n"
45
  "8\n"
46
  "9\n"
47
  "@arcs\n"
48
  "     label  cost\n"
49
  "1 8  0      107\n"
50
  "0 3  1      70\n"
51
  "2 1  2      46\n"
52
  "4 1  3      28\n"
53
  "4 4  4      91\n"
54
  "3 9  5      76\n"
55
  "9 8  6      61\n"
56
  "8 1  7      39\n"
57
  "9 8  8      74\n"
58
  "8 0  9      39\n"
59
  "4 3  10     45\n"
60
  "2 2  11     34\n"
61
  "0 1  12     100\n"
62
  "6 3  13     95\n"
63
  "4 1  14     22\n"
64
  "1 1  15     31\n"
65
  "7 2  16     51\n"
66
  "2 6  17     29\n"
67
  "8 3  18     115\n"
68
  "6 9  19     32\n"
69
  "1 1  20     60\n"
70
  "0 3  21     40\n"
71
  "@attributes\n"
72
  "source 0\n";
73

	
74

	
75
void checkMinCostArborescenceCompile()
76
{
77
  typedef double VType;
78
  typedef concepts::Digraph Digraph;
79
  typedef concepts::ReadMap<Digraph::Arc, VType> CostMap;
80
  typedef Digraph::Node Node;
81
  typedef Digraph::Arc Arc;
82
  typedef concepts::WriteMap<Digraph::Arc, bool> ArbMap;
83
  typedef concepts::ReadWriteMap<Digraph::Node, Digraph::Arc> PredMap;
84

	
85
  typedef MinCostArborescence<Digraph, CostMap>::
86
            SetArborescenceMap<ArbMap>::
87
            SetPredMap<PredMap>::Create MinCostArbType;
88

	
89
  Digraph g;
90
  Node s, n;
91
  Arc e;
92
  VType c;
93
  bool b;
94
  int i;
95
  CostMap cost;
96
  ArbMap arb;
97
  PredMap pred;
98

	
99
  MinCostArbType mcarb_test(g, cost);
100
  const MinCostArbType& const_mcarb_test = mcarb_test;
101

	
102
  mcarb_test
103
    .arborescenceMap(arb)
104
    .predMap(pred)
105
    .run(s);
106

	
107
  mcarb_test.init();
108
  mcarb_test.addSource(s);
109
  mcarb_test.start();
110
  n = mcarb_test.processNextNode();
111
  b = const_mcarb_test.emptyQueue();
112
  i = const_mcarb_test.queueSize();
113
  
114
  c = const_mcarb_test.arborescenceCost();
115
  b = const_mcarb_test.arborescence(e);
116
  e = const_mcarb_test.pred(n);
117
  const MinCostArbType::ArborescenceMap &am =
118
    const_mcarb_test.arborescenceMap();
119
  const MinCostArbType::PredMap &pm =
120
    const_mcarb_test.predMap();
121
  b = const_mcarb_test.reached(n);
122
  b = const_mcarb_test.processed(n);
123
  
124
  i = const_mcarb_test.dualNum();
125
  c = const_mcarb_test.dualValue();
126
  i = const_mcarb_test.dualSize(i);
127
  c = const_mcarb_test.dualValue(i);
128
  
129
  ignore_unused_variable_warning(am);
130
  ignore_unused_variable_warning(pm);
131
}
132

	
133
int main() {
134
  typedef SmartDigraph Digraph;
135
  DIGRAPH_TYPEDEFS(Digraph);
136

	
137
  typedef Digraph::ArcMap<double> CostMap;
138

	
139
  Digraph digraph;
140
  CostMap cost(digraph);
141
  Node source;
142

	
143
  std::istringstream is(test_lgf);
144
  digraphReader(digraph, is).
145
    arcMap("cost", cost).
146
    node("source", source).run();
147

	
148
  MinCostArborescence<Digraph, CostMap> mca(digraph, cost);
149
  mca.run(source);
150

	
151
  vector<pair<double, set<Node> > > dualSolution(mca.dualNum());
152

	
153
  for (int i = 0; i < mca.dualNum(); ++i) {
154
    dualSolution[i].first = mca.dualValue(i);
155
    for (MinCostArborescence<Digraph, CostMap>::DualIt it(mca, i);
156
         it != INVALID; ++it) {
157
      dualSolution[i].second.insert(it);
158
    }
159
  }
160

	
161
  for (ArcIt it(digraph); it != INVALID; ++it) {
162
    if (mca.reached(digraph.source(it))) {
163
      double sum = 0.0;
164
      for (int i = 0; i < int(dualSolution.size()); ++i) {
165
        if (dualSolution[i].second.find(digraph.target(it))
166
            != dualSolution[i].second.end() &&
167
            dualSolution[i].second.find(digraph.source(it))
168
            == dualSolution[i].second.end()) {
169
          sum += dualSolution[i].first;
170
        }
171
      }
172
      if (mca.arborescence(it)) {
173
        check(sum == cost[it], "Invalid dual solution");
174
      }
175
      check(sum <= cost[it], "Invalid dual solution");
176
    }
177
  }
178

	
179

	
180
  check(mca.dualValue() == mca.arborescenceCost(), "Invalid dual solution");
181

	
182
  check(mca.reached(source), "Invalid arborescence");
183
  for (ArcIt a(digraph); a != INVALID; ++a) {
184
    check(!mca.reached(digraph.source(a)) ||
185
          mca.reached(digraph.target(a)), "Invalid arborescence");
186
  }
187

	
188
  for (NodeIt n(digraph); n != INVALID; ++n) {
189
    if (!mca.reached(n)) continue;
190
    int cnt = 0;
191
    for (InArcIt a(digraph, n); a != INVALID; ++a) {
192
      if (mca.arborescence(a)) {
193
        check(mca.pred(n) == a, "Invalid arborescence");
194
        ++cnt;
195
      }
196
    }
197
    check((n == source ? cnt == 0 : cnt == 1), "Invalid arborescence");
198
  }
199

	
200
  Digraph::ArcMap<bool> arborescence(digraph);
201
  check(mca.arborescenceCost() ==
202
        minCostArborescence(digraph, cost, source, arborescence),
203
        "Wrong result of the function interface");
204

	
205
  return 0;
206
}
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#include <iostream>
20
#include <fstream>
21
#include <limits>
22

	
23
#include <lemon/list_graph.h>
24
#include <lemon/lgf_reader.h>
25

	
26
#include <lemon/network_simplex.h>
27

	
28
#include <lemon/concepts/digraph.h>
29
#include <lemon/concept_check.h>
30

	
31
#include "test_tools.h"
32

	
33
using namespace lemon;
34

	
35
char test_lgf[] =
36
  "@nodes\n"
37
  "label  sup1 sup2 sup3 sup4 sup5 sup6\n"
38
  "    1    20   27    0   30   20   30\n"
39
  "    2    -4    0    0    0   -8   -3\n"
40
  "    3     0    0    0    0    0    0\n"
41
  "    4     0    0    0    0    0    0\n"
42
  "    5     9    0    0    0    6   11\n"
43
  "    6    -6    0    0    0   -5   -6\n"
44
  "    7     0    0    0    0    0    0\n"
45
  "    8     0    0    0    0    0    3\n"
46
  "    9     3    0    0    0    0    0\n"
47
  "   10    -2    0    0    0   -7   -2\n"
48
  "   11     0    0    0    0  -10    0\n"
49
  "   12   -20  -27    0  -30  -30  -20\n"
50
  "\n"                
51
  "@arcs\n"
52
  "       cost  cap low1 low2 low3\n"
53
  " 1  2    70   11    0    8    8\n"
54
  " 1  3   150    3    0    1    0\n"
55
  " 1  4    80   15    0    2    2\n"
56
  " 2  8    80   12    0    0    0\n"
57
  " 3  5   140    5    0    3    1\n"
58
  " 4  6    60   10    0    1    0\n"
59
  " 4  7    80    2    0    0    0\n"
60
  " 4  8   110    3    0    0    0\n"
61
  " 5  7    60   14    0    0    0\n"
62
  " 5 11   120   12    0    0    0\n"
63
  " 6  3     0    3    0    0    0\n"
64
  " 6  9   140    4    0    0    0\n"
65
  " 6 10    90    8    0    0    0\n"
66
  " 7  1    30    5    0    0   -5\n"
67
  " 8 12    60   16    0    4    3\n"
68
  " 9 12    50    6    0    0    0\n"
69
  "10 12    70   13    0    5    2\n"
70
  "10  2   100    7    0    0    0\n"
71
  "10  7    60   10    0    0   -3\n"
72
  "11 10    20   14    0    6  -20\n"
73
  "12 11    30   10    0    0  -10\n"
74
  "\n"
75
  "@attributes\n"
76
  "source 1\n"
77
  "target 12\n";
78

	
79

	
80
enum SupplyType {
81
  EQ,
82
  GEQ,
83
  LEQ
84
};
85

	
86
// Check the interface of an MCF algorithm
87
template <typename GR, typename Value, typename Cost>
88
class McfClassConcept
89
{
90
public:
91

	
92
  template <typename MCF>
93
  struct Constraints {
94
    void constraints() {
95
      checkConcept<concepts::Digraph, GR>();
96
      
97
      const Constraints& me = *this;
98

	
99
      MCF mcf(me.g);
100
      const MCF& const_mcf = mcf;
101

	
102
      b = mcf.reset()
103
             .lowerMap(me.lower)
104
             .upperMap(me.upper)
105
             .costMap(me.cost)
106
             .supplyMap(me.sup)
107
             .stSupply(me.n, me.n, me.k)
108
             .run();
109

	
110
      c = const_mcf.totalCost();
111
      x = const_mcf.template totalCost<double>();
112
      v = const_mcf.flow(me.a);
113
      c = const_mcf.potential(me.n);
114
      const_mcf.flowMap(fm);
115
      const_mcf.potentialMap(pm);
116
    }
117

	
118
    typedef typename GR::Node Node;
119
    typedef typename GR::Arc Arc;
120
    typedef concepts::ReadMap<Node, Value> NM;
121
    typedef concepts::ReadMap<Arc, Value> VAM;
122
    typedef concepts::ReadMap<Arc, Cost> CAM;
123
    typedef concepts::WriteMap<Arc, Value> FlowMap;
124
    typedef concepts::WriteMap<Node, Cost> PotMap;
125
  
126
    GR g;
127
    VAM lower;
128
    VAM upper;
129
    CAM cost;
130
    NM sup;
131
    Node n;
132
    Arc a;
133
    Value k;
134

	
135
    FlowMap fm;
136
    PotMap pm;
137
    bool b;
138
    double x;
139
    typename MCF::Value v;
140
    typename MCF::Cost c;
141
  };
142

	
143
};
144

	
145

	
146
// Check the feasibility of the given flow (primal soluiton)
147
template < typename GR, typename LM, typename UM,
148
           typename SM, typename FM >
149
bool checkFlow( const GR& gr, const LM& lower, const UM& upper,
150
                const SM& supply, const FM& flow,
151
                SupplyType type = EQ )
152
{
153
  TEMPLATE_DIGRAPH_TYPEDEFS(GR);
154

	
155
  for (ArcIt e(gr); e != INVALID; ++e) {
156
    if (flow[e] < lower[e] || flow[e] > upper[e]) return false;
157
  }
158

	
159
  for (NodeIt n(gr); n != INVALID; ++n) {
160
    typename SM::Value sum = 0;
161
    for (OutArcIt e(gr, n); e != INVALID; ++e)
162
      sum += flow[e];
163
    for (InArcIt e(gr, n); e != INVALID; ++e)
164
      sum -= flow[e];
165
    bool b = (type ==  EQ && sum == supply[n]) ||
166
             (type == GEQ && sum >= supply[n]) ||
167
             (type == LEQ && sum <= supply[n]);
168
    if (!b) return false;
169
  }
170

	
171
  return true;
172
}
173

	
174
// Check the feasibility of the given potentials (dual soluiton)
175
// using the "Complementary Slackness" optimality condition
176
template < typename GR, typename LM, typename UM,
177
           typename CM, typename SM, typename FM, typename PM >
178
bool checkPotential( const GR& gr, const LM& lower, const UM& upper,
179
                     const CM& cost, const SM& supply, const FM& flow, 
180
                     const PM& pi, SupplyType type )
181
{
182
  TEMPLATE_DIGRAPH_TYPEDEFS(GR);
183

	
184
  bool opt = true;
185
  for (ArcIt e(gr); opt && e != INVALID; ++e) {
186
    typename CM::Value red_cost =
187
      cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
188
    opt = red_cost == 0 ||
189
          (red_cost > 0 && flow[e] == lower[e]) ||
190
          (red_cost < 0 && flow[e] == upper[e]);
191
  }
192
  
193
  for (NodeIt n(gr); opt && n != INVALID; ++n) {
194
    typename SM::Value sum = 0;
195
    for (OutArcIt e(gr, n); e != INVALID; ++e)
196
      sum += flow[e];
197
    for (InArcIt e(gr, n); e != INVALID; ++e)
198
      sum -= flow[e];
199
    if (type != LEQ) {
200
      opt = (pi[n] <= 0) && (sum == supply[n] || pi[n] == 0);
201
    } else {
202
      opt = (pi[n] >= 0) && (sum == supply[n] || pi[n] == 0);
203
    }
204
  }
205
  
206
  return opt;
207
}
208

	
209
// Check whether the dual cost is equal to the primal cost
210
template < typename GR, typename LM, typename UM,
211
           typename CM, typename SM, typename PM >
212
bool checkDualCost( const GR& gr, const LM& lower, const UM& upper,
213
                    const CM& cost, const SM& supply, const PM& pi,
214
                    typename CM::Value total )
215
{
216
  TEMPLATE_DIGRAPH_TYPEDEFS(GR);
217

	
218
  typename CM::Value dual_cost = 0;
219
  SM red_supply(gr);
220
  for (NodeIt n(gr); n != INVALID; ++n) {
221
    red_supply[n] = supply[n];
222
  }
223
  for (ArcIt a(gr); a != INVALID; ++a) {
224
    if (lower[a] != 0) {
225
      dual_cost += lower[a] * cost[a];
226
      red_supply[gr.source(a)] -= lower[a];
227
      red_supply[gr.target(a)] += lower[a];
228
    }
229
  }
230
  
231
  for (NodeIt n(gr); n != INVALID; ++n) {
232
    dual_cost -= red_supply[n] * pi[n];
233
  }
234
  for (ArcIt a(gr); a != INVALID; ++a) {
235
    typename CM::Value red_cost =
236
      cost[a] + pi[gr.source(a)] - pi[gr.target(a)];
237
    dual_cost -= (upper[a] - lower[a]) * std::max(-red_cost, 0);
238
  }
239
  
240
  return dual_cost == total;
241
}
242

	
243
// Run a minimum cost flow algorithm and check the results
244
template < typename MCF, typename GR,
245
           typename LM, typename UM,
246
           typename CM, typename SM,
247
           typename PT >
248
void checkMcf( const MCF& mcf, PT mcf_result,
249
               const GR& gr, const LM& lower, const UM& upper,
250
               const CM& cost, const SM& supply,
251
               PT result, bool optimal, typename CM::Value total,
252
               const std::string &test_id = "",
253
               SupplyType type = EQ )
254
{
255
  check(mcf_result == result, "Wrong result " + test_id);
256
  if (optimal) {
257
    typename GR::template ArcMap<typename SM::Value> flow(gr);
258
    typename GR::template NodeMap<typename CM::Value> pi(gr);
259
    mcf.flowMap(flow);
260
    mcf.potentialMap(pi);
261
    check(checkFlow(gr, lower, upper, supply, flow, type),
262
          "The flow is not feasible " + test_id);
263
    check(mcf.totalCost() == total, "The flow is not optimal " + test_id);
264
    check(checkPotential(gr, lower, upper, cost, supply, flow, pi, type),
265
          "Wrong potentials " + test_id);
266
    check(checkDualCost(gr, lower, upper, cost, supply, pi, total),
267
          "Wrong dual cost " + test_id);
268
  }
269
}
270

	
271
int main()
272
{
273
  // Check the interfaces
274
  {
275
    typedef concepts::Digraph GR;
276
    checkConcept< McfClassConcept<GR, int, int>,
277
                  NetworkSimplex<GR> >();
278
    checkConcept< McfClassConcept<GR, double, double>,
279
                  NetworkSimplex<GR, double> >();
280
    checkConcept< McfClassConcept<GR, int, double>,
281
                  NetworkSimplex<GR, int, double> >();
282
  }
283

	
284
  // Run various MCF tests
285
  typedef ListDigraph Digraph;
286
  DIGRAPH_TYPEDEFS(ListDigraph);
287

	
288
  // Read the test digraph
289
  Digraph gr;
290
  Digraph::ArcMap<int> c(gr), l1(gr), l2(gr), l3(gr), u(gr);
291
  Digraph::NodeMap<int> s1(gr), s2(gr), s3(gr), s4(gr), s5(gr), s6(gr);
292
  ConstMap<Arc, int> cc(1), cu(std::numeric_limits<int>::max());
293
  Node v, w;
294

	
295
  std::istringstream input(test_lgf);
296
  DigraphReader<Digraph>(gr, input)
297
    .arcMap("cost", c)
298
    .arcMap("cap", u)
299
    .arcMap("low1", l1)
300
    .arcMap("low2", l2)
301
    .arcMap("low3", l3)
302
    .nodeMap("sup1", s1)
303
    .nodeMap("sup2", s2)
304
    .nodeMap("sup3", s3)
305
    .nodeMap("sup4", s4)
306
    .nodeMap("sup5", s5)
307
    .nodeMap("sup6", s6)
308
    .node("source", v)
309
    .node("target", w)
310
    .run();
311
  
312
  // Build test digraphs with negative costs
313
  Digraph neg_gr;
314
  Node n1 = neg_gr.addNode();
315
  Node n2 = neg_gr.addNode();
316
  Node n3 = neg_gr.addNode();
317
  Node n4 = neg_gr.addNode();
318
  Node n5 = neg_gr.addNode();
319
  Node n6 = neg_gr.addNode();
320
  Node n7 = neg_gr.addNode();
321
  
322
  Arc a1 = neg_gr.addArc(n1, n2);
323
  Arc a2 = neg_gr.addArc(n1, n3);
324
  Arc a3 = neg_gr.addArc(n2, n4);
325
  Arc a4 = neg_gr.addArc(n3, n4);
326
  Arc a5 = neg_gr.addArc(n3, n2);
327
  Arc a6 = neg_gr.addArc(n5, n3);
328
  Arc a7 = neg_gr.addArc(n5, n6);
329
  Arc a8 = neg_gr.addArc(n6, n7);
330
  Arc a9 = neg_gr.addArc(n7, n5);
331
  
332
  Digraph::ArcMap<int> neg_c(neg_gr), neg_l1(neg_gr, 0), neg_l2(neg_gr, 0);
333
  ConstMap<Arc, int> neg_u1(std::numeric_limits<int>::max()), neg_u2(5000);
334
  Digraph::NodeMap<int> neg_s(neg_gr, 0);
335
  
336
  neg_l2[a7] =  1000;
337
  neg_l2[a8] = -1000;
338
  
339
  neg_s[n1] =  100;
340
  neg_s[n4] = -100;
341
  
342
  neg_c[a1] =  100;
343
  neg_c[a2] =   30;
344
  neg_c[a3] =   20;
345
  neg_c[a4] =   80;
346
  neg_c[a5] =   50;
347
  neg_c[a6] =   10;
348
  neg_c[a7] =   80;
349
  neg_c[a8] =   30;
350
  neg_c[a9] = -120;
351

	
352
  Digraph negs_gr;
353
  Digraph::NodeMap<int> negs_s(negs_gr);
354
  Digraph::ArcMap<int> negs_c(negs_gr);
355
  ConstMap<Arc, int> negs_l(0), negs_u(1000);
356
  n1 = negs_gr.addNode();
357
  n2 = negs_gr.addNode();
358
  negs_s[n1] = 100;
359
  negs_s[n2] = -300;
360
  negs_c[negs_gr.addArc(n1, n2)] = -1;
361

	
362

	
363
  // A. Test NetworkSimplex with the default pivot rule
364
  {
365
    NetworkSimplex<Digraph> mcf(gr);
366

	
367
    // Check the equality form
368
    mcf.upperMap(u).costMap(c);
369
    checkMcf(mcf, mcf.supplyMap(s1).run(),
370
             gr, l1, u, c, s1, mcf.OPTIMAL, true,   5240, "#A1");
371
    checkMcf(mcf, mcf.stSupply(v, w, 27).run(),
372
             gr, l1, u, c, s2, mcf.OPTIMAL, true,   7620, "#A2");
373
    mcf.lowerMap(l2);
374
    checkMcf(mcf, mcf.supplyMap(s1).run(),
375
             gr, l2, u, c, s1, mcf.OPTIMAL, true,   5970, "#A3");
376
    checkMcf(mcf, mcf.stSupply(v, w, 27).run(),
377
             gr, l2, u, c, s2, mcf.OPTIMAL, true,   8010, "#A4");
378
    mcf.reset();
379
    checkMcf(mcf, mcf.supplyMap(s1).run(),
380
             gr, l1, cu, cc, s1, mcf.OPTIMAL, true,   74, "#A5");
381
    checkMcf(mcf, mcf.lowerMap(l2).stSupply(v, w, 27).run(),
382
             gr, l2, cu, cc, s2, mcf.OPTIMAL, true,   94, "#A6");
383
    mcf.reset();
384
    checkMcf(mcf, mcf.run(),
385
             gr, l1, cu, cc, s3, mcf.OPTIMAL, true,    0, "#A7");
386
    checkMcf(mcf, mcf.lowerMap(l2).upperMap(u).run(),
387
             gr, l2, u, cc, s3, mcf.INFEASIBLE, false, 0, "#A8");
388
    mcf.reset().lowerMap(l3).upperMap(u).costMap(c).supplyMap(s4);
389
    checkMcf(mcf, mcf.run(),
390
             gr, l3, u, c, s4, mcf.OPTIMAL, true,   6360, "#A9");
391

	
392
    // Check the GEQ form
393
    mcf.reset().upperMap(u).costMap(c).supplyMap(s5);
394
    checkMcf(mcf, mcf.run(),
395
             gr, l1, u, c, s5, mcf.OPTIMAL, true,   3530, "#A10", GEQ);
396
    mcf.supplyType(mcf.GEQ);
397
    checkMcf(mcf, mcf.lowerMap(l2).run(),
398
             gr, l2, u, c, s5, mcf.OPTIMAL, true,   4540, "#A11", GEQ);
399
    mcf.supplyMap(s6);
400
    checkMcf(mcf, mcf.run(),
401
             gr, l2, u, c, s6, mcf.INFEASIBLE, false,  0, "#A12", GEQ);
402

	
403
    // Check the LEQ form
404
    mcf.reset().supplyType(mcf.LEQ);
405
    mcf.upperMap(u).costMap(c).supplyMap(s6);
406
    checkMcf(mcf, mcf.run(),
407
             gr, l1, u, c, s6, mcf.OPTIMAL, true,   5080, "#A13", LEQ);
408
    checkMcf(mcf, mcf.lowerMap(l2).run(),
409
             gr, l2, u, c, s6, mcf.OPTIMAL, true,   5930, "#A14", LEQ);
410
    mcf.supplyMap(s5);
411
    checkMcf(mcf, mcf.run(),
412
             gr, l2, u, c, s5, mcf.INFEASIBLE, false,  0, "#A15", LEQ);
413

	
414
    // Check negative costs
415
    NetworkSimplex<Digraph> neg_mcf(neg_gr);
416
    neg_mcf.lowerMap(neg_l1).costMap(neg_c).supplyMap(neg_s);
417
    checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l1, neg_u1,
418
      neg_c, neg_s, neg_mcf.UNBOUNDED, false,    0, "#A16");
419
    neg_mcf.upperMap(neg_u2);
420
    checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l1, neg_u2,
421
      neg_c, neg_s, neg_mcf.OPTIMAL, true,  -40000, "#A17");
422
    neg_mcf.reset().lowerMap(neg_l2).costMap(neg_c).supplyMap(neg_s);
423
    checkMcf(neg_mcf, neg_mcf.run(), neg_gr, neg_l2, neg_u1,
424
      neg_c, neg_s, neg_mcf.UNBOUNDED, false,    0, "#A18");
425
      
426
    NetworkSimplex<Digraph> negs_mcf(negs_gr);
427
    negs_mcf.costMap(negs_c).supplyMap(negs_s);
428
    checkMcf(negs_mcf, negs_mcf.run(), negs_gr, negs_l, negs_u,
429
      negs_c, negs_s, negs_mcf.OPTIMAL, true, -300, "#A19", GEQ);
430
  }
431

	
432
  // B. Test NetworkSimplex with each pivot rule
433
  {
434
    NetworkSimplex<Digraph> mcf(gr);
435
    mcf.supplyMap(s1).costMap(c).upperMap(u).lowerMap(l2);
436

	
437
    checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::FIRST_ELIGIBLE),
438
             gr, l2, u, c, s1, mcf.OPTIMAL, true,   5970, "#B1");
439
    checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::BEST_ELIGIBLE),
440
             gr, l2, u, c, s1, mcf.OPTIMAL, true,   5970, "#B2");
441
    checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::BLOCK_SEARCH),
442
             gr, l2, u, c, s1, mcf.OPTIMAL, true,   5970, "#B3");
443
    checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::CANDIDATE_LIST),
444
             gr, l2, u, c, s1, mcf.OPTIMAL, true,   5970, "#B4");
445
    checkMcf(mcf, mcf.run(NetworkSimplex<Digraph>::ALTERING_LIST),
446
             gr, l2, u, c, s1, mcf.OPTIMAL, true,   5970, "#B5");
447
  }
448

	
449
  return 0;
450
}
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#include <iostream>
20
#include <sstream>
21

	
22
#include <lemon/smart_graph.h>
23
#include <lemon/lgf_reader.h>
24
#include <lemon/path.h>
25
#include <lemon/concepts/digraph.h>
26
#include <lemon/concept_check.h>
27

	
28
#include <lemon/karp.h>
29
#include <lemon/hartmann_orlin.h>
30
#include <lemon/howard.h>
31

	
32
#include "test_tools.h"
33

	
34
using namespace lemon;
35

	
36
char test_lgf[] =
37
  "@nodes\n"
38
  "label\n"
39
  "1\n"
40
  "2\n"
41
  "3\n"
42
  "4\n"
43
  "5\n"
44
  "6\n"
45
  "7\n"
46
  "@arcs\n"
47
  "    len1 len2 len3 len4  c1 c2 c3 c4\n"
48
  "1 2    1    1    1    1   0  0  0  0\n"
49
  "2 4    5    5    5    5   1  0  0  0\n"
50
  "2 3    8    8    8    8   0  0  0  0\n"
51
  "3 2   -2    0    0    0   1  0  0  0\n"
52
  "3 4    4    4    4    4   0  0  0  0\n"
53
  "3 7   -4   -4   -4   -4   0  0  0  0\n"
54
  "4 1    2    2    2    2   0  0  0  0\n"
55
  "4 3    3    3    3    3   1  0  0  0\n"
56
  "4 4    3    3    0    0   0  0  1  0\n"
57
  "5 2    4    4    4    4   0  0  0  0\n"
58
  "5 6    3    3    3    3   0  1  0  0\n"
59
  "6 5    2    2    2    2   0  1  0  0\n"
60
  "6 4   -1   -1   -1   -1   0  0  0  0\n"
61
  "6 7    1    1    1    1   0  0  0  0\n"
62
  "7 7    4    4    4   -1   0  0  0  1\n";
63

	
64
                        
65
// Check the interface of an MMC algorithm
66
template <typename GR, typename Value>
67
struct MmcClassConcept
68
{
69
  template <typename MMC>
70
  struct Constraints {
71
    void constraints() {
72
      const Constraints& me = *this;
73

	
74
      typedef typename MMC
75
        ::template SetPath<ListPath<GR> >
76
        ::template SetLargeValue<Value>
77
        ::Create MmcAlg;
78
      MmcAlg mmc(me.g, me.length);
79
      const MmcAlg& const_mmc = mmc;
80
      
81
      typename MmcAlg::Tolerance tol = const_mmc.tolerance();
82
      mmc.tolerance(tol);
83
      
84
      b = mmc.cycle(p).run();
85
      b = mmc.findMinMean();
86
      b = mmc.findCycle();
87

	
88
      v = const_mmc.cycleLength();
89
      i = const_mmc.cycleArcNum();
90
      d = const_mmc.cycleMean();
91
      p = const_mmc.cycle();
92
    }
93

	
94
    typedef concepts::ReadMap<typename GR::Arc, Value> LM;
95
  
96
    GR g;
97
    LM length;
98
    ListPath<GR> p;
99
    Value v;
100
    int i;
101
    double d;
102
    bool b;
103
  };
104
};
105

	
106
// Perform a test with the given parameters
107
template <typename MMC>
108
void checkMmcAlg(const SmartDigraph& gr,
109
                 const SmartDigraph::ArcMap<int>& lm,
110
                 const SmartDigraph::ArcMap<int>& cm,
111
                 int length, int size) {
112
  MMC alg(gr, lm);
113
  alg.findMinMean();
114
  check(alg.cycleMean() == static_cast<double>(length) / size,
115
        "Wrong cycle mean");
116
  alg.findCycle();
117
  check(alg.cycleLength() == length && alg.cycleArcNum() == size,
118
        "Wrong path");
119
  SmartDigraph::ArcMap<int> cycle(gr, 0);
120
  for (typename MMC::Path::ArcIt a(alg.cycle()); a != INVALID; ++a) {
121
    ++cycle[a];
122
  }
123
  for (SmartDigraph::ArcIt a(gr); a != INVALID; ++a) {
124
    check(cm[a] == cycle[a], "Wrong path");
125
  }
126
}
127

	
128
// Class for comparing types
129
template <typename T1, typename T2>
130
struct IsSameType {
131
  static const int result = 0;
132
};
133

	
134
template <typename T>
135
struct IsSameType<T,T> {
136
  static const int result = 1;
137
};
138

	
139

	
140
int main() {
141
  #ifdef LEMON_HAVE_LONG_LONG
142
    typedef long long long_int;
143
  #else
144
    typedef long long_int;
145
  #endif
146

	
147
  // Check the interface
148
  {
149
    typedef concepts::Digraph GR;
150

	
151
    // Karp
152
    checkConcept< MmcClassConcept<GR, int>,
153
                  Karp<GR, concepts::ReadMap<GR::Arc, int> > >();
154
    checkConcept< MmcClassConcept<GR, float>,
155
                  Karp<GR, concepts::ReadMap<GR::Arc, float> > >();
156
    
157
    // HartmannOrlin
158
    checkConcept< MmcClassConcept<GR, int>,
159
                  HartmannOrlin<GR, concepts::ReadMap<GR::Arc, int> > >();
160
    checkConcept< MmcClassConcept<GR, float>,
161
                  HartmannOrlin<GR, concepts::ReadMap<GR::Arc, float> > >();
162
    
163
    // Howard
164
    checkConcept< MmcClassConcept<GR, int>,
165
                  Howard<GR, concepts::ReadMap<GR::Arc, int> > >();
166
    checkConcept< MmcClassConcept<GR, float>,
167
                  Howard<GR, concepts::ReadMap<GR::Arc, float> > >();
168

	
169
    if (IsSameType<Howard<GR, concepts::ReadMap<GR::Arc, int> >::LargeValue,
170
          long_int>::result == 0) check(false, "Wrong LargeValue type");
171
    if (IsSameType<Howard<GR, concepts::ReadMap<GR::Arc, float> >::LargeValue,
172
          double>::result == 0) check(false, "Wrong LargeValue type");
173
  }
174

	
175
  // Run various tests
176
  {
177
    typedef SmartDigraph GR;
178
    DIGRAPH_TYPEDEFS(GR);
179
    
180
    GR gr;
181
    IntArcMap l1(gr), l2(gr), l3(gr), l4(gr);
182
    IntArcMap c1(gr), c2(gr), c3(gr), c4(gr);
183
    
184
    std::istringstream input(test_lgf);
185
    digraphReader(gr, input).
186
      arcMap("len1", l1).
187
      arcMap("len2", l2).
188
      arcMap("len3", l3).
189
      arcMap("len4", l4).
190
      arcMap("c1", c1).
191
      arcMap("c2", c2).
192
      arcMap("c3", c3).
193
      arcMap("c4", c4).
194
      run();
195

	
196
    // Karp
197
    checkMmcAlg<Karp<GR, IntArcMap> >(gr, l1, c1,  6, 3);
198
    checkMmcAlg<Karp<GR, IntArcMap> >(gr, l2, c2,  5, 2);
199
    checkMmcAlg<Karp<GR, IntArcMap> >(gr, l3, c3,  0, 1);
200
    checkMmcAlg<Karp<GR, IntArcMap> >(gr, l4, c4, -1, 1);
201

	
202
    // HartmannOrlin
203
    checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l1, c1,  6, 3);
204
    checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l2, c2,  5, 2);
205
    checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l3, c3,  0, 1);
206
    checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l4, c4, -1, 1);
207

	
208
    // Howard
209
    checkMmcAlg<Howard<GR, IntArcMap> >(gr, l1, c1,  6, 3);
210
    checkMmcAlg<Howard<GR, IntArcMap> >(gr, l2, c2,  5, 2);
211
    checkMmcAlg<Howard<GR, IntArcMap> >(gr, l3, c3,  0, 1);
212
    checkMmcAlg<Howard<GR, IntArcMap> >(gr, l4, c4, -1, 1);
213
  }
214

	
215
  return 0;
216
}
Ignore white space 6 line context
1
INCLUDE_DIRECTORIES(
2
  ${PROJECT_SOURCE_DIR}
3
  ${PROJECT_BINARY_DIR}
4
)
5

	
6
LINK_DIRECTORIES(
7
  ${PROJECT_BINARY_DIR}/lemon
8
)
9

	
10
ADD_EXECUTABLE(lgf-gen lgf-gen.cc)
11
TARGET_LINK_LIBRARIES(lgf-gen lemon)
12

	
13
ADD_EXECUTABLE(dimacs-to-lgf dimacs-to-lgf.cc)
14
TARGET_LINK_LIBRARIES(dimacs-to-lgf lemon)
15

	
16
ADD_EXECUTABLE(dimacs-solver dimacs-solver.cc)
17
TARGET_LINK_LIBRARIES(dimacs-solver lemon)
18

	
19
INSTALL(
20
  TARGETS lgf-gen dimacs-to-lgf dimacs-solver
21
  RUNTIME DESTINATION bin
22
  COMPONENT bin
23
)
24

	
25
IF(NOT WIN32)
26
  INSTALL(
27
    PROGRAMS ${CMAKE_CURRENT_SOURCE_DIR}/lemon-0.x-to-1.x.sh
28
    DESTINATION bin
29
    COMPONENT bin
30
  )
31
ENDIF()
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
/// \ingroup tools
20
/// \file
21
/// \brief Special plane graph generator.
22
///
23
/// Graph generator application for various types of plane graphs.
24
///
25
/// See
26
/// \code
27
///   lgf-gen --help
28
/// \endcode
29
/// for more information on the usage.
30

	
31
#include <algorithm>
32
#include <set>
33
#include <ctime>
34
#include <lemon/list_graph.h>
35
#include <lemon/random.h>
36
#include <lemon/dim2.h>
37
#include <lemon/bfs.h>
38
#include <lemon/counter.h>
39
#include <lemon/suurballe.h>
40
#include <lemon/graph_to_eps.h>
41
#include <lemon/lgf_writer.h>
42
#include <lemon/arg_parser.h>
43
#include <lemon/euler.h>
44
#include <lemon/math.h>
45
#include <lemon/kruskal.h>
46
#include <lemon/time_measure.h>
47

	
48
using namespace lemon;
49

	
50
typedef dim2::Point<double> Point;
51

	
52
GRAPH_TYPEDEFS(ListGraph);
53

	
54
bool progress=true;
55

	
56
int N;
57
// int girth;
58

	
59
ListGraph g;
60

	
61
std::vector<Node> nodes;
62
ListGraph::NodeMap<Point> coords(g);
63

	
64

	
65
double totalLen(){
66
  double tlen=0;
67
  for(EdgeIt e(g);e!=INVALID;++e)
68
    tlen+=std::sqrt((coords[g.v(e)]-coords[g.u(e)]).normSquare());
69
  return tlen;
70
}
71

	
72
int tsp_impr_num=0;
73

	
74
const double EPSILON=1e-8;
75
bool tsp_improve(Node u, Node v)
76
{
77
  double luv=std::sqrt((coords[v]-coords[u]).normSquare());
78
  Node u2=u;
79
  Node v2=v;
80
  do {
81
    Node n;
82
    for(IncEdgeIt e(g,v2);(n=g.runningNode(e))==u2;++e) { }
83
    u2=v2;
84
    v2=n;
85
    if(luv+std::sqrt((coords[v2]-coords[u2]).normSquare())-EPSILON>
86
       std::sqrt((coords[u]-coords[u2]).normSquare())+
87
       std::sqrt((coords[v]-coords[v2]).normSquare()))
88
      {
89
         g.erase(findEdge(g,u,v));
90
         g.erase(findEdge(g,u2,v2));
91
        g.addEdge(u2,u);
92
        g.addEdge(v,v2);
93
        tsp_impr_num++;
94
        return true;
95
      }
96
  } while(v2!=u);
97
  return false;
98
}
99

	
100
bool tsp_improve(Node u)
101
{
102
  for(IncEdgeIt e(g,u);e!=INVALID;++e)
103
    if(tsp_improve(u,g.runningNode(e))) return true;
104
  return false;
105
}
106

	
107
void tsp_improve()
108
{
109
  bool b;
110
  do {
111
    b=false;
112
    for(NodeIt n(g);n!=INVALID;++n)
113
      if(tsp_improve(n)) b=true;
114
  } while(b);
115
}
116

	
117
void tsp()
118
{
119
  for(int i=0;i<N;i++) g.addEdge(nodes[i],nodes[(i+1)%N]);
120
  tsp_improve();
121
}
122

	
123
class Line
124
{
125
public:
126
  Point a;
127
  Point b;
128
  Line(Point _a,Point _b) :a(_a),b(_b) {}
129
  Line(Node _a,Node _b) : a(coords[_a]),b(coords[_b]) {}
130
  Line(const Arc &e) : a(coords[g.source(e)]),b(coords[g.target(e)]) {}
131
  Line(const Edge &e) : a(coords[g.u(e)]),b(coords[g.v(e)]) {}
132
};
133

	
134
inline std::ostream& operator<<(std::ostream &os, const Line &l)
135
{
136
  os << l.a << "->" << l.b;
137
  return os;
138
}
139

	
140
bool cross(Line a, Line b)
141
{
142
  Point ao=rot90(a.b-a.a);
143
  Point bo=rot90(b.b-b.a);
144
  return (ao*(b.a-a.a))*(ao*(b.b-a.a))<0 &&
145
    (bo*(a.a-b.a))*(bo*(a.b-b.a))<0;
146
}
147

	
148
struct Parc
149
{
150
  Node a;
151
  Node b;
152
  double len;
153
};
154

	
155
bool pedgeLess(Parc a,Parc b)
156
{
157
  return a.len<b.len;
158
}
159

	
160
std::vector<Edge> arcs;
161

	
162
namespace _delaunay_bits {
163

	
164
  struct Part {
165
    int prev, curr, next;
166

	
167
    Part(int p, int c, int n) : prev(p), curr(c), next(n) {}
168
  };
169

	
170
  inline std::ostream& operator<<(std::ostream& os, const Part& part) {
171
    os << '(' << part.prev << ',' << part.curr << ',' << part.next << ')';
172
    return os;
173
  }
174

	
175
  inline double circle_point(const Point& p, const Point& q, const Point& r) {
176
    double a = p.x * (q.y - r.y) + q.x * (r.y - p.y) + r.x * (p.y - q.y);
177
    if (a == 0) return std::numeric_limits<double>::quiet_NaN();
178

	
179
    double d = (p.x * p.x + p.y * p.y) * (q.y - r.y) +
180
      (q.x * q.x + q.y * q.y) * (r.y - p.y) +
181
      (r.x * r.x + r.y * r.y) * (p.y - q.y);
182

	
183
    double e = (p.x * p.x + p.y * p.y) * (q.x - r.x) +
184
      (q.x * q.x + q.y * q.y) * (r.x - p.x) +
185
      (r.x * r.x + r.y * r.y) * (p.x - q.x);
186

	
187
    double f = (p.x * p.x + p.y * p.y) * (q.x * r.y - r.x * q.y) +
188
      (q.x * q.x + q.y * q.y) * (r.x * p.y - p.x * r.y) +
189
      (r.x * r.x + r.y * r.y) * (p.x * q.y - q.x * p.y);
190

	
191
    return d / (2 * a) + std::sqrt((d * d + e * e) / (4 * a * a) + f / a);
192
  }
193

	
194
  inline bool circle_form(const Point& p, const Point& q, const Point& r) {
195
    return rot90(q - p) * (r - q) < 0.0;
196
  }
197

	
198
  inline double intersection(const Point& p, const Point& q, double sx) {
199
    const double epsilon = 1e-8;
200

	
201
    if (p.x == q.x) return (p.y + q.y) / 2.0;
202

	
203
    if (sx < p.x + epsilon) return p.y;
204
    if (sx < q.x + epsilon) return q.y;
205

	
206
    double a = q.x - p.x;
207
    double b = (q.x - sx) * p.y - (p.x - sx) * q.y;
208
    double d = (q.x - sx) * (p.x - sx) * (p - q).normSquare();
209
    return (b - std::sqrt(d)) / a;
210
  }
211

	
212
  struct YLess {
213

	
214

	
215
    YLess(const std::vector<Point>& points, double& sweep)
216
      : _points(points), _sweep(sweep) {}
217

	
218
    bool operator()(const Part& l, const Part& r) const {
219
      const double epsilon = 1e-8;
220

	
221
      //      std::cerr << l << " vs " << r << std::endl;
222
      double lbx = l.prev != -1 ?
223
        intersection(_points[l.prev], _points[l.curr], _sweep) :
224
        - std::numeric_limits<double>::infinity();
225
      double rbx = r.prev != -1 ?
226
        intersection(_points[r.prev], _points[r.curr], _sweep) :
227
        - std::numeric_limits<double>::infinity();
228
      double lex = l.next != -1 ?
229
        intersection(_points[l.curr], _points[l.next], _sweep) :
230
        std::numeric_limits<double>::infinity();
231
      double rex = r.next != -1 ?
232
        intersection(_points[r.curr], _points[r.next], _sweep) :
233
        std::numeric_limits<double>::infinity();
234

	
235
      if (lbx > lex) std::swap(lbx, lex);
236
      if (rbx > rex) std::swap(rbx, rex);
237

	
238
      if (lex < epsilon + rex && lbx + epsilon < rex) return true;
239
      if (rex < epsilon + lex && rbx + epsilon < lex) return false;
240
      return lex < rex;
241
    }
242

	
243
    const std::vector<Point>& _points;
244
    double& _sweep;
245
  };
246

	
247
  struct BeachIt;
248

	
249
  typedef std::multimap<double, BeachIt> SpikeHeap;
250

	
251
  typedef std::multimap<Part, SpikeHeap::iterator, YLess> Beach;
252

	
253
  struct BeachIt {
254
    Beach::iterator it;
255

	
256
    BeachIt(Beach::iterator iter) : it(iter) {}
257
  };
258

	
259
}
260

	
261
inline void delaunay() {
262
  Counter cnt("Number of arcs added: ");
263

	
264
  using namespace _delaunay_bits;
265

	
266
  typedef _delaunay_bits::Part Part;
267
  typedef std::vector<std::pair<double, int> > SiteHeap;
268

	
269

	
270
  std::vector<Point> points;
271
  std::vector<Node> nodes;
272

	
273
  for (NodeIt it(g); it != INVALID; ++it) {
274
    nodes.push_back(it);
275
    points.push_back(coords[it]);
276
  }
277

	
278
  SiteHeap siteheap(points.size());
279

	
280
  double sweep;
281

	
282

	
283
  for (int i = 0; i < int(siteheap.size()); ++i) {
284
    siteheap[i] = std::make_pair(points[i].x, i);
285
  }
286

	
287
  std::sort(siteheap.begin(), siteheap.end());
288
  sweep = siteheap.front().first;
289

	
290
  YLess yless(points, sweep);
291
  Beach beach(yless);
292

	
293
  SpikeHeap spikeheap;
294

	
295
  std::set<std::pair<int, int> > arcs;
296

	
297
  int siteindex = 0;
298
  {
299
    SiteHeap front;
300

	
301
    while (siteindex < int(siteheap.size()) &&
302
           siteheap[0].first == siteheap[siteindex].first) {
303
      front.push_back(std::make_pair(points[siteheap[siteindex].second].y,
304
                                     siteheap[siteindex].second));
305
      ++siteindex;
306
    }
307

	
308
    std::sort(front.begin(), front.end());
309

	
310
    for (int i = 0; i < int(front.size()); ++i) {
311
      int prev = (i == 0 ? -1 : front[i - 1].second);
312
      int curr = front[i].second;
313
      int next = (i + 1 == int(front.size()) ? -1 : front[i + 1].second);
314

	
315
      beach.insert(std::make_pair(Part(prev, curr, next),
316
                                  spikeheap.end()));
317
    }
318
  }
319

	
320
  while (siteindex < int(points.size()) || !spikeheap.empty()) {
321

	
322
    SpikeHeap::iterator spit = spikeheap.begin();
323

	
324
    if (siteindex < int(points.size()) &&
325
        (spit == spikeheap.end() || siteheap[siteindex].first < spit->first)) {
326
      int site = siteheap[siteindex].second;
327
      sweep = siteheap[siteindex].first;
328

	
329
      Beach::iterator bit = beach.upper_bound(Part(site, site, site));
330

	
331
      if (bit->second != spikeheap.end()) {
332
        spikeheap.erase(bit->second);
333
      }
334

	
335
      int prev = bit->first.prev;
336
      int curr = bit->first.curr;
337
      int next = bit->first.next;
338

	
339
      beach.erase(bit);
340

	
341
      SpikeHeap::iterator pit = spikeheap.end();
342
      if (prev != -1 &&
343
          circle_form(points[prev], points[curr], points[site])) {
344
        double x = circle_point(points[prev], points[curr], points[site]);
345
        pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
346
        pit->second.it =
347
          beach.insert(std::make_pair(Part(prev, curr, site), pit));
348
      } else {
349
        beach.insert(std::make_pair(Part(prev, curr, site), pit));
350
      }
351

	
352
      beach.insert(std::make_pair(Part(curr, site, curr), spikeheap.end()));
353

	
354
      SpikeHeap::iterator nit = spikeheap.end();
355
      if (next != -1 &&
356
          circle_form(points[site], points[curr],points[next])) {
357
        double x = circle_point(points[site], points[curr], points[next]);
358
        nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
359
        nit->second.it =
360
          beach.insert(std::make_pair(Part(site, curr, next), nit));
361
      } else {
362
        beach.insert(std::make_pair(Part(site, curr, next), nit));
363
      }
364

	
365
      ++siteindex;
366
    } else {
367
      sweep = spit->first;
368

	
369
      Beach::iterator bit = spit->second.it;
370

	
371
      int prev = bit->first.prev;
372
      int curr = bit->first.curr;
373
      int next = bit->first.next;
374

	
375
      {
376
        std::pair<int, int> arc;
377

	
378
        arc = prev < curr ?
379
          std::make_pair(prev, curr) : std::make_pair(curr, prev);
380

	
381
        if (arcs.find(arc) == arcs.end()) {
382
          arcs.insert(arc);
383
          g.addEdge(nodes[prev], nodes[curr]);
384
          ++cnt;
385
        }
386

	
387
        arc = curr < next ?
388
          std::make_pair(curr, next) : std::make_pair(next, curr);
389

	
390
        if (arcs.find(arc) == arcs.end()) {
391
          arcs.insert(arc);
392
          g.addEdge(nodes[curr], nodes[next]);
393
          ++cnt;
394
        }
395
      }
396

	
397
      Beach::iterator pbit = bit; --pbit;
398
      int ppv = pbit->first.prev;
399
      Beach::iterator nbit = bit; ++nbit;
400
      int nnt = nbit->first.next;
401

	
402
      if (bit->second != spikeheap.end()) spikeheap.erase(bit->second);
403
      if (pbit->second != spikeheap.end()) spikeheap.erase(pbit->second);
404
      if (nbit->second != spikeheap.end()) spikeheap.erase(nbit->second);
405

	
406
      beach.erase(nbit);
407
      beach.erase(bit);
408
      beach.erase(pbit);
409

	
410
      SpikeHeap::iterator pit = spikeheap.end();
411
      if (ppv != -1 && ppv != next &&
412
          circle_form(points[ppv], points[prev], points[next])) {
413
        double x = circle_point(points[ppv], points[prev], points[next]);
414
        if (x < sweep) x = sweep;
415
        pit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
416
        pit->second.it =
417
          beach.insert(std::make_pair(Part(ppv, prev, next), pit));
418
      } else {
419
        beach.insert(std::make_pair(Part(ppv, prev, next), pit));
420
      }
421

	
422
      SpikeHeap::iterator nit = spikeheap.end();
423
      if (nnt != -1 && prev != nnt &&
424
          circle_form(points[prev], points[next], points[nnt])) {
425
        double x = circle_point(points[prev], points[next], points[nnt]);
426
        if (x < sweep) x = sweep;
427
        nit = spikeheap.insert(std::make_pair(x, BeachIt(beach.end())));
428
        nit->second.it =
429
          beach.insert(std::make_pair(Part(prev, next, nnt), nit));
430
      } else {
431
        beach.insert(std::make_pair(Part(prev, next, nnt), nit));
432
      }
433

	
434
    }
435
  }
436

	
437
  for (Beach::iterator it = beach.begin(); it != beach.end(); ++it) {
438
    int curr = it->first.curr;
439
    int next = it->first.next;
440

	
441
    if (next == -1) continue;
442

	
443
    std::pair<int, int> arc;
444

	
445
    arc = curr < next ?
446
      std::make_pair(curr, next) : std::make_pair(next, curr);
447

	
448
    if (arcs.find(arc) == arcs.end()) {
449
      arcs.insert(arc);
450
      g.addEdge(nodes[curr], nodes[next]);
451
      ++cnt;
452
    }
453
  }
454
}
455

	
456
void sparse(int d)
457
{
458
  Counter cnt("Number of arcs removed: ");
459
  Bfs<ListGraph> bfs(g);
460
  for(std::vector<Edge>::reverse_iterator ei=arcs.rbegin();
461
      ei!=arcs.rend();++ei)
462
    {
463
      Node a=g.u(*ei);
464
      Node b=g.v(*ei);
465
      g.erase(*ei);
466
      bfs.run(a,b);
467
      if(bfs.predArc(b)==INVALID || bfs.dist(b)>d)
468
        g.addEdge(a,b);
469
      else cnt++;
470
    }
471
}
472

	
473
void sparse2(int d)
474
{
475
  Counter cnt("Number of arcs removed: ");
476
  for(std::vector<Edge>::reverse_iterator ei=arcs.rbegin();
477
      ei!=arcs.rend();++ei)
478
    {
479
      Node a=g.u(*ei);
480
      Node b=g.v(*ei);
481
      g.erase(*ei);
482
      ConstMap<Arc,int> cegy(1);
483
      Suurballe<ListGraph,ConstMap<Arc,int> > sur(g,cegy);
484
      int k=sur.run(a,b,2);
485
      if(k<2 || sur.totalLength()>d)
486
        g.addEdge(a,b);
487
      else cnt++;
488
//       else std::cout << "Remove arc " << g.id(a) << "-" << g.id(b) << '\n';
489
    }
490
}
491

	
492
void sparseTriangle(int d)
493
{
494
  Counter cnt("Number of arcs added: ");
495
  std::vector<Parc> pedges;
496
  for(NodeIt n(g);n!=INVALID;++n)
497
    for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
498
      {
499
        Parc p;
500
        p.a=n;
501
        p.b=m;
502
        p.len=(coords[m]-coords[n]).normSquare();
503
        pedges.push_back(p);
504
      }
505
  std::sort(pedges.begin(),pedges.end(),pedgeLess);
506
  for(std::vector<Parc>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
507
    {
508
      Line li(pi->a,pi->b);
509
      EdgeIt e(g);
510
      for(;e!=INVALID && !cross(e,li);++e) ;
511
      Edge ne;
512
      if(e==INVALID) {
513
        ConstMap<Arc,int> cegy(1);
514
        Suurballe<ListGraph,ConstMap<Arc,int> > sur(g,cegy);
515
        int k=sur.run(pi->a,pi->b,2);
516
        if(k<2 || sur.totalLength()>d)
517
          {
518
            ne=g.addEdge(pi->a,pi->b);
519
            arcs.push_back(ne);
520
            cnt++;
521
          }
522
      }
523
    }
524
}
525

	
526
template <typename Graph, typename CoordMap>
527
class LengthSquareMap {
528
public:
529
  typedef typename Graph::Edge Key;
530
  typedef typename CoordMap::Value::Value Value;
531

	
532
  LengthSquareMap(const Graph& graph, const CoordMap& coords)
533
    : _graph(graph), _coords(coords) {}
534

	
535
  Value operator[](const Key& key) const {
536
    return (_coords[_graph.v(key)] -
537
            _coords[_graph.u(key)]).normSquare();
538
  }
539

	
540
private:
541

	
542
  const Graph& _graph;
543
  const CoordMap& _coords;
544
};
545

	
546
void minTree() {
547
  std::vector<Parc> pedges;
548
  Timer T;
549
  std::cout << T.realTime() << "s: Creating delaunay triangulation...\n";
550
  delaunay();
551
  std::cout << T.realTime() << "s: Calculating spanning tree...\n";
552
  LengthSquareMap<ListGraph, ListGraph::NodeMap<Point> > ls(g, coords);
553
  ListGraph::EdgeMap<bool> tree(g);
554
  kruskal(g, ls, tree);
555
  std::cout << T.realTime() << "s: Removing non tree arcs...\n";
556
  std::vector<Edge> remove;
557
  for (EdgeIt e(g); e != INVALID; ++e) {
558
    if (!tree[e]) remove.push_back(e);
559
  }
560
  for(int i = 0; i < int(remove.size()); ++i) {
561
    g.erase(remove[i]);
562
  }
563
  std::cout << T.realTime() << "s: Done\n";
564
}
565

	
566
void tsp2()
567
{
568
  std::cout << "Find a tree..." << std::endl;
569

	
570
  minTree();
571

	
572
  std::cout << "Total arc length (tree) : " << totalLen() << std::endl;
573

	
574
  std::cout << "Make it Euler..." << std::endl;
575

	
576
  {
577
    std::vector<Node> leafs;
578
    for(NodeIt n(g);n!=INVALID;++n)
579
      if(countIncEdges(g,n)%2==1) leafs.push_back(n);
580

	
581
//    for(unsigned int i=0;i<leafs.size();i+=2)
582
//       g.addArc(leafs[i],leafs[i+1]);
583

	
584
    std::vector<Parc> pedges;
585
    for(unsigned int i=0;i<leafs.size()-1;i++)
586
      for(unsigned int j=i+1;j<leafs.size();j++)
587
        {
588
          Node n=leafs[i];
589
          Node m=leafs[j];
590
          Parc p;
591
          p.a=n;
592
          p.b=m;
593
          p.len=(coords[m]-coords[n]).normSquare();
594
          pedges.push_back(p);
595
        }
596
    std::sort(pedges.begin(),pedges.end(),pedgeLess);
597
    for(unsigned int i=0;i<pedges.size();i++)
598
      if(countIncEdges(g,pedges[i].a)%2 &&
599
         countIncEdges(g,pedges[i].b)%2)
600
        g.addEdge(pedges[i].a,pedges[i].b);
601
  }
602

	
603
  for(NodeIt n(g);n!=INVALID;++n)
604
    if(countIncEdges(g,n)%2 || countIncEdges(g,n)==0 )
605
      std::cout << "GEBASZ!!!" << std::endl;
606

	
607
  for(EdgeIt e(g);e!=INVALID;++e)
608
    if(g.u(e)==g.v(e))
609
      std::cout << "LOOP GEBASZ!!!" << std::endl;
610

	
611
  std::cout << "Number of arcs : " << countEdges(g) << std::endl;
612

	
613
  std::cout << "Total arc length (euler) : " << totalLen() << std::endl;
614

	
615
  ListGraph::EdgeMap<Arc> enext(g);
616
  {
617
    EulerIt<ListGraph> e(g);
618
    Arc eo=e;
619
    Arc ef=e;
620
//     std::cout << "Tour arc: " << g.id(Edge(e)) << std::endl;
621
    for(++e;e!=INVALID;++e)
622
      {
623
//         std::cout << "Tour arc: " << g.id(Edge(e)) << std::endl;
624
        enext[eo]=e;
625
        eo=e;
626
      }
627
    enext[eo]=ef;
628
  }
629

	
630
  std::cout << "Creating a tour from that..." << std::endl;
631

	
632
  int nnum = countNodes(g);
633
  int ednum = countEdges(g);
634

	
635
  for(Arc p=enext[EdgeIt(g)];ednum>nnum;p=enext[p])
636
    {
637
//       std::cout << "Checking arc " << g.id(p) << std::endl;
638
      Arc e=enext[p];
639
      Arc f=enext[e];
640
      Node n2=g.source(f);
641
      Node n1=g.oppositeNode(n2,e);
642
      Node n3=g.oppositeNode(n2,f);
643
      if(countIncEdges(g,n2)>2)
644
        {
645
//           std::cout << "Remove an Arc" << std::endl;
646
          Arc ff=enext[f];
647
          g.erase(e);
648
          g.erase(f);
649
          if(n1!=n3)
650
            {
651
              Arc ne=g.direct(g.addEdge(n1,n3),n1);
652
              enext[p]=ne;
653
              enext[ne]=ff;
654
              ednum--;
655
            }
656
          else {
657
            enext[p]=ff;
658
            ednum-=2;
659
          }
660
        }
661
    }
662

	
663
  std::cout << "Total arc length (tour) : " << totalLen() << std::endl;
664

	
665
  std::cout << "2-opt the tour..." << std::endl;
666

	
667
  tsp_improve();
668

	
669
  std::cout << "Total arc length (2-opt tour) : " << totalLen() << std::endl;
670
}
671

	
672

	
673
int main(int argc,const char **argv)
674
{
675
  ArgParser ap(argc,argv);
676

	
677
//   bool eps;
678
  bool disc_d, square_d, gauss_d;
679
//   bool tsp_a,two_a,tree_a;
680
  int num_of_cities=1;
681
  double area=1;
682
  N=100;
683
//   girth=10;
684
  std::string ndist("disc");
685
  ap.refOption("n", "Number of nodes (default is 100)", N)
686
    .intOption("g", "Girth parameter (default is 10)", 10)
687
    .refOption("cities", "Number of cities (default is 1)", num_of_cities)
688
    .refOption("area", "Full relative area of the cities (default is 1)", area)
689
    .refOption("disc", "Nodes are evenly distributed on a unit disc (default)",
690
               disc_d)
691
    .optionGroup("dist", "disc")
692
    .refOption("square", "Nodes are evenly distributed on a unit square",
693
               square_d)
694
    .optionGroup("dist", "square")
695
    .refOption("gauss", "Nodes are located according to a two-dim Gauss "
696
               "distribution", gauss_d)
697
    .optionGroup("dist", "gauss")
698
    .onlyOneGroup("dist")
699
    .boolOption("eps", "Also generate .eps output (<prefix>.eps)")
700
    .boolOption("nonodes", "Draw only the edges in the generated .eps output")
701
    .boolOption("dir", "Directed graph is generated (each edge is replaced by "
702
                "two directed arcs)")
703
    .boolOption("2con", "Create a two connected planar graph")
704
    .optionGroup("alg","2con")
705
    .boolOption("tree", "Create a min. cost spanning tree")
706
    .optionGroup("alg","tree")
707
    .boolOption("tsp", "Create a TSP tour")
708
    .optionGroup("alg","tsp")
709
    .boolOption("tsp2", "Create a TSP tour (tree based)")
710
    .optionGroup("alg","tsp2")
711
    .boolOption("dela", "Delaunay triangulation graph")
712
    .optionGroup("alg","dela")
713
    .onlyOneGroup("alg")
714
    .boolOption("rand", "Use time seed for random number generator")
715
    .optionGroup("rand", "rand")
716
    .intOption("seed", "Random seed", -1)
717
    .optionGroup("rand", "seed")
718
    .onlyOneGroup("rand")
719
    .other("[prefix]","Prefix of the output files. Default is 'lgf-gen-out'")
720
    .run();
721

	
722
  if (ap["rand"]) {
723
    int seed = int(time(0));
724
    std::cout << "Random number seed: " << seed << std::endl;
725
    rnd = Random(seed);
726
  }
727
  if (ap.given("seed")) {
728
    int seed = ap["seed"];
729
    std::cout << "Random number seed: " << seed << std::endl;
730
    rnd = Random(seed);
731
  }
732

	
733
  std::string prefix;
734
  switch(ap.files().size())
735
    {
736
    case 0:
737
      prefix="lgf-gen-out";
738
      break;
739
    case 1:
740
      prefix=ap.files()[0];
741
      break;
742
    default:
743
      std::cerr << "\nAt most one prefix can be given\n\n";
744
      exit(1);
745
    }
746

	
747
  double sum_sizes=0;
748
  std::vector<double> sizes;
749
  std::vector<double> cum_sizes;
750
  for(int s=0;s<num_of_cities;s++)
751
    {
752
      //         sum_sizes+=rnd.exponential();
753
      double d=rnd();
754
      sum_sizes+=d;
755
      sizes.push_back(d);
756
      cum_sizes.push_back(sum_sizes);
757
    }
758
  int i=0;
759
  for(int s=0;s<num_of_cities;s++)
760
    {
761
      Point center=(num_of_cities==1?Point(0,0):rnd.disc());
762
      if(gauss_d)
763
        for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
764
          Node n=g.addNode();
765
          nodes.push_back(n);
766
          coords[n]=center+rnd.gauss2()*area*
767
            std::sqrt(sizes[s]/sum_sizes);
768
        }
769
      else if(square_d)
770
        for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
771
          Node n=g.addNode();
772
          nodes.push_back(n);
773
          coords[n]=center+Point(rnd()*2-1,rnd()*2-1)*area*
774
            std::sqrt(sizes[s]/sum_sizes);
775
        }
776
      else if(disc_d || true)
777
        for(;i<N*(cum_sizes[s]/sum_sizes);i++) {
778
          Node n=g.addNode();
779
          nodes.push_back(n);
780
          coords[n]=center+rnd.disc()*area*
781
            std::sqrt(sizes[s]/sum_sizes);
782
        }
783
    }
784

	
785
//   for (ListGraph::NodeIt n(g); n != INVALID; ++n) {
786
//     std::cerr << coords[n] << std::endl;
787
//   }
788

	
789
  if(ap["tsp"]) {
790
    tsp();
791
    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
792
  }
793
  if(ap["tsp2"]) {
794
    tsp2();
795
    std::cout << "#2-opt improvements: " << tsp_impr_num << std::endl;
796
  }
797
  else if(ap["2con"]) {
798
    std::cout << "Make triangles\n";
799
    //   triangle();
800
    sparseTriangle(ap["g"]);
801
    std::cout << "Make it sparser\n";
802
    sparse2(ap["g"]);
803
  }
804
  else if(ap["tree"]) {
805
    minTree();
806
  }
807
  else if(ap["dela"]) {
808
    delaunay();
809
  }
810

	
811

	
812
  std::cout << "Number of nodes    : " << countNodes(g) << std::endl;
813
  std::cout << "Number of arcs    : " << countEdges(g) << std::endl;
814
  double tlen=0;
815
  for(EdgeIt e(g);e!=INVALID;++e)
816
    tlen+=std::sqrt((coords[g.v(e)]-coords[g.u(e)]).normSquare());
817
  std::cout << "Total arc length  : " << tlen << std::endl;
818

	
819
  if(ap["eps"])
820
    graphToEps(g,prefix+".eps").scaleToA4().
821
      scale(600).nodeScale(.005).arcWidthScale(.001).preScale(false).
822
      coords(coords).hideNodes(ap.given("nonodes")).run();
823

	
824
  if(ap["dir"])
825
    DigraphWriter<ListGraph>(g,prefix+".lgf").
826
      nodeMap("coordinates_x",scaleMap(xMap(coords),600)).
827
      nodeMap("coordinates_y",scaleMap(yMap(coords),600)).
828
      run();
829
  else GraphWriter<ListGraph>(g,prefix+".lgf").
830
         nodeMap("coordinates_x",scaleMap(xMap(coords),600)).
831
         nodeMap("coordinates_y",scaleMap(yMap(coords),600)).
832
         run();
833
}
834

	
Ignore white space 6 line context
1 1
syntax: glob
2 2
*.obj
3 3
*.orig
4 4
*.rej
5 5
*~
6 6
*.o
7 7
*.log
8 8
*.lo
9 9
*.tar.*
10 10
*.bak
11 11
Makefile.in
12 12
aclocal.m4
13 13
config.h.in
14 14
configure
15 15
Makefile
16 16
config.h
17 17
config.log
18 18
config.status
19 19
libtool
20 20
stamp-h1
21 21
lemon/lemon.pc
22 22
lemon/libemon.la
23 23
lemon/stamp-h2
24 24
doc/Doxyfile
25
cmake/version.cmake
25 26
.dirstamp
26 27
.libs/*
27 28
.deps/*
28 29
demo/*.eps
29 30
m4/libtool.m4
30 31
m4/ltoptions.m4
31 32
m4/ltsugar.m4
32 33
m4/ltversion.m4
33 34
m4/lt~obsolete.m4
34 35

	
35 36
syntax: regexp
36 37
(.*/)?\#[^/]*\#$
37 38
(.*/)?\.\#[^/]*$
38 39
^doc/html/.*
39 40
^doc/.*\.tag
40 41
^autom4te.cache/.*
41 42
^build-aux/.*
42
^objs.*/.*
43
^.*objs.*/.*
43 44
^test/[a-z_]*$
44 45
^tools/[a-z-_]*$
45 46
^demo/.*_demo$
46
^build/.*
47
^.*build.*/.*
47 48
^doc/gen-images/.*
48 49
CMakeFiles
49 50
DartTestfile.txt
50 51
cmake_install.cmake
51 52
CMakeCache.txt
Ignore white space 6 line context
1 1
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
2 2

	
3 3
SET(PROJECT_NAME "LEMON")
4
SET(PROJECT_VERSION "hg-tip" CACHE STRING "The version string.")
5

	
6 4
PROJECT(${PROJECT_NAME})
7 5

	
8
SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
6
IF(EXISTS ${PROJECT_SOURCE_DIR}/cmake/version.cmake)
7
  INCLUDE(${PROJECT_SOURCE_DIR}/cmake/version.cmake)
8
ELSEIF(DEFINED ENV{LEMON_VERSION})
9
  SET(LEMON_VERSION $ENV{LEMON_VERSION} CACHE STRING "LEMON version string.")
10
ELSE()
11
  EXECUTE_PROCESS(
12
    COMMAND hg id -i
13
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
14
    OUTPUT_VARIABLE HG_REVISION
15
    ERROR_QUIET
16
    OUTPUT_STRIP_TRAILING_WHITESPACE
17
  )
18
  IF(HG_REVISION STREQUAL "")
19
    SET(HG_REVISION "hg-tip")
20
  ENDIF()
21
  SET(LEMON_VERSION ${HG_REVISION} CACHE STRING "LEMON version string.")
22
ENDIF()
9 23

	
10
INCLUDE(FindDoxygen)
11
INCLUDE(FindGhostscript)
24
SET(PROJECT_VERSION ${LEMON_VERSION})
25

	
26
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
27

	
28
FIND_PACKAGE(Doxygen)
29
FIND_PACKAGE(Ghostscript)
30
FIND_PACKAGE(GLPK 4.33)
31
FIND_PACKAGE(CPLEX)
32
FIND_PACKAGE(COIN)
33

	
34
INCLUDE(CheckTypeSize)
35
CHECK_TYPE_SIZE("long long" LONG_LONG)
36
SET(LEMON_HAVE_LONG_LONG ${HAVE_LONG_LONG})
37

	
38
INCLUDE(FindPythonInterp)
12 39

	
13 40
ENABLE_TESTING()
14 41

	
15 42
ADD_SUBDIRECTORY(lemon)
16
ADD_SUBDIRECTORY(demo)
17
ADD_SUBDIRECTORY(doc)
18
ADD_SUBDIRECTORY(test)
43
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
44
  ADD_SUBDIRECTORY(demo)
45
  ADD_SUBDIRECTORY(tools)
46
  ADD_SUBDIRECTORY(doc)
47
  ADD_SUBDIRECTORY(test)
48
ENDIF()
19 49

	
20
IF(WIN32)
21
  INSTALL(FILES ${CMAKE_SOURCE_DIR}/cmake/nsis/lemon.ico
22
    DESTINATION bin)
23
ENDIF(WIN32)
50
CONFIGURE_FILE(
51
  ${PROJECT_SOURCE_DIR}/cmake/LEMONConfig.cmake.in
52
  ${PROJECT_BINARY_DIR}/cmake/LEMONConfig.cmake
53
  @ONLY
54
)
55
IF(UNIX)
56
  INSTALL(
57
    FILES ${PROJECT_BINARY_DIR}/cmake/LEMONConfig.cmake
58
    DESTINATION share/lemon/cmake
59
  )
60
ELSEIF(WIN32)
61
  INSTALL(
62
    FILES ${PROJECT_BINARY_DIR}/cmake/LEMONConfig.cmake
63
    DESTINATION cmake
64
  )
65
ENDIF()
24 66

	
25
IF(WIN32)
67
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR} AND WIN32)
26 68
  SET(CPACK_PACKAGE_NAME ${PROJECT_NAME})
27
  SET(CPACK_PACKAGE_VENDOR
28
    "EGRES - Egervary Research Group on Combinatorial Optimization")
69
  SET(CPACK_PACKAGE_VENDOR "EGRES")
29 70
  SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY
30
    "LEMON - Library of Efficient Models and Optimization in Networks")
31
  SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")
71
    "LEMON - Library for Efficient Modeling and Optimization in Networks")
72
  SET(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
32 73

	
33 74
  SET(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
34 75

	
35 76
  SET(CPACK_PACKAGE_INSTALL_DIRECTORY
36 77
    "${PROJECT_NAME} ${PROJECT_VERSION}")
37 78
  SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY
38 79
    "${PROJECT_NAME} ${PROJECT_VERSION}")
39 80

	
40
  # Variables to generate a component-based installer.
41
  #SET(CPACK_COMPONENTS_ALL headers library html_documentation)
81
  SET(CPACK_COMPONENTS_ALL headers library html_documentation bin)
42 82

	
43
  #SET(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C++ headers")
44
  #SET(CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "Static library")
45
  #SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DISPLAY_NAME "HTML documentation")
83
  SET(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C++ headers")
84
  SET(CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "Dynamic-link library")
85
  SET(CPACK_COMPONENT_BIN_DISPLAY_NAME "Command line utilities")
86
  SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DISPLAY_NAME "HTML documentation")
46 87

	
47
  #SET(CPACK_COMPONENT_HEADERS_DESCRIPTION
48
  #  "C++ header files for use with the LEMON library")
49
  #SET(CPACK_COMPONENT_LIBRARY_DESCRIPTION
50
  #  "Static library used to build programs with LEMON")
51
  #SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DESCRIPTION
52
  #  "Doxygen generated documentation")
88
  SET(CPACK_COMPONENT_HEADERS_DESCRIPTION
89
    "C++ header files")
90
  SET(CPACK_COMPONENT_LIBRARY_DESCRIPTION
91
    "DLL and import library")
92
  SET(CPACK_COMPONENT_BIN_DESCRIPTION
93
    "Command line utilities")
94
  SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DESCRIPTION
95
    "Doxygen generated documentation")
53 96

	
54
  #SET(CPACK_COMPONENT_HEADERS_DEPENDS library)
97
  SET(CPACK_COMPONENT_HEADERS_DEPENDS library)
55 98

	
56
  #SET(CPACK_COMPONENT_HEADERS_GROUP "Development")
57
  #SET(CPACK_COMPONENT_LIBRARY_GROUP "Development")
58
  #SET(CPACK_COMPONENT_HTML_DOCUMENTATION_GROUP "Documentation")
99
  SET(CPACK_COMPONENT_HEADERS_GROUP "Development")
100
  SET(CPACK_COMPONENT_LIBRARY_GROUP "Development")
101
  SET(CPACK_COMPONENT_HTML_DOCUMENTATION_GROUP "Documentation")
59 102

	
60
  #SET(CPACK_COMPONENT_GROUP_DEVELOPMENT_DESCRIPTION
61
  #  "Components needed to develop software using LEMON")
62
  #SET(CPACK_COMPONENT_GROUP_DOCUMENTATION_DESCRIPTION
63
  #  "Documentation of LEMON")
103
  SET(CPACK_COMPONENT_GROUP_DEVELOPMENT_DESCRIPTION
104
    "Components needed to develop software using LEMON")
105
  SET(CPACK_COMPONENT_GROUP_DOCUMENTATION_DESCRIPTION
106
    "Documentation of LEMON")
64 107

	
65
  #SET(CPACK_ALL_INSTALL_TYPES Full Developer)
108
  SET(CPACK_ALL_INSTALL_TYPES Full Developer)
66 109

	
67
  #SET(CPACK_COMPONENT_HEADERS_INSTALL_TYPES Developer Full)
68
  #SET(CPACK_COMPONENT_LIBRARY_INSTALL_TYPES Developer Full)
69
  #SET(CPACK_COMPONENT_HTML_DOCUMENTATION_INSTALL_TYPES Full)
110
  SET(CPACK_COMPONENT_HEADERS_INSTALL_TYPES Developer Full)
111
  SET(CPACK_COMPONENT_LIBRARY_INSTALL_TYPES Developer Full)
112
  SET(CPACK_COMPONENT_HTML_DOCUMENTATION_INSTALL_TYPES Full)
70 113

	
71 114
  SET(CPACK_GENERATOR "NSIS")
72
  SET(CPACK_NSIS_MUI_ICON "${CMAKE_SOURCE_DIR}/cmake/nsis/lemon.ico")
73
  SET(CPACK_NSIS_MUI_UNIICON "${CMAKE_SOURCE_DIR}/cmake/nsis/uninstall.ico")
74
  #SET(CPACK_PACKAGE_ICON "${CMAKE_SOURCE_DIR}/cmake/nsis\\\\installer.bmp")
115
  SET(CPACK_NSIS_MUI_ICON "${PROJECT_SOURCE_DIR}/cmake/nsis/lemon.ico")
116
  SET(CPACK_NSIS_MUI_UNIICON "${PROJECT_SOURCE_DIR}/cmake/nsis/uninstall.ico")
117
  #SET(CPACK_PACKAGE_ICON "${PROJECT_SOURCE_DIR}/cmake/nsis\\\\installer.bmp")
75 118
  SET(CPACK_NSIS_INSTALLED_ICON_NAME "bin\\\\lemon.ico")
76 119
  SET(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY} ${PROJECT_NAME}")
77 120
  SET(CPACK_NSIS_HELP_LINK "http:\\\\\\\\lemon.cs.elte.hu")
78 121
  SET(CPACK_NSIS_URL_INFO_ABOUT "http:\\\\\\\\lemon.cs.elte.hu")
79 122
  SET(CPACK_NSIS_CONTACT "lemon-user@lemon.cs.elte.hu")
80 123
  SET(CPACK_NSIS_CREATE_ICONS_EXTRA "
81
    CreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Documentation.lnk\\\" \\\"$INSTDIR\\\\doc\\\\index.html\\\"
124
    CreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Documentation.lnk\\\" \\\"$INSTDIR\\\\share\\\\doc\\\\index.html\\\"
82 125
    ")
83 126
  SET(CPACK_NSIS_DELETE_ICONS_EXTRA "
84 127
    !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
85 128
    Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\Documentation.lnk\\\"
86 129
    ")
87 130

	
88 131
  INCLUDE(CPack)
89
ENDIF(WIN32)
132
ENDIF()
Ignore white space 6 line context
1 1
Installation Instructions
2 2
=========================
3 3

	
4 4
Since you are reading this I assume you already obtained one of the release
5 5
tarballs and successfully extracted it. The latest version of LEMON is
6 6
available at our web page (http://lemon.cs.elte.hu/).
7 7

	
8
LEMON provides two different build environments, one is based on "autotool",
9
while the other is based on "cmake". This file contains instructions only for
10
the former one, which is the recommended build environment on Linux, Mac OSX
11
and other unices or if you use Cygwin on Windows. For cmake installation
12
instructions visit http://lemon.cs.elte.hu.
13

	
8 14
In order to install LEMON from the extracted source tarball you have to
9 15
issue the following commands:
10 16

	
11 17
   1. `cd lemon-x.y.z'
12 18

	
13 19
      This command changes to the directory which was created when you
14 20
      extracted the sources. The x.y.z part is a version number.
15 21

	
16 22
   2. `./configure'
17 23

	
18 24
      This command runs the configure shell script, which does some checks and
19 25
      creates the makefiles.
20 26

	
21 27
   3. `make'
22 28

	
23 29
      This command compiles the non-template part of LEMON into libemon.a
24
      file. It also compiles the programs in the tools and demo subdirectories
25
      when enabled.
30
      file. It also compiles the programs in the tools subdirectory by
31
      default.
26 32

	
27 33
   4. `make check'
28 34

	
29 35
      This step is optional, but recommended. It runs the test programs that
30 36
      we developed for LEMON to check whether the library works properly on
31 37
      your platform.
32 38

	
33 39
   5. `make install'
34 40

	
35 41
      This command installs LEMON under /usr/local (you will need root
36 42
      privileges to be able to do that). If you want to install it to some
37 43
      other location, then pass the --prefix=DIRECTORY flag to configure in
38 44
      step 2. For example: `./configure --prefix=/home/username/lemon'.
39 45

	
40 46
   6. `make install-html'
41 47

	
42 48
      This command installs the documentation under share/doc/lemon/docs. The
43 49
      generated documentation is included in the tarball. If you want to
44 50
      generate it yourself, then run `make html'. Note that for this you need
45 51
      to have the following programs installed: Doxygen, Graphviz, Ghostscript,
46 52
      Latex.
47 53

	
48 54

	
49 55
Configure Options and Variables
50 56
===============================
51 57

	
52 58
In step 2 you can customize the actions of configure by setting variables
53 59
and passing options to it. This can be done like this:
54 60
`./configure [OPTION]... [VARIABLE=VALUE]...'
55 61

	
56 62
Below you will find some useful variables and options (see `./configure --help'
57 63
for more):
58 64

	
59 65
CXX='comp'
60 66

	
61 67
  Change the C++ compiler to 'comp'.
62 68

	
63 69
CXXFLAGS='flags'
64 70

	
65 71
  Pass the 'flags' to the compiler. For example CXXFLAGS='-O3 -march=pentium-m'
66 72
  turns on generation of aggressively optimized Pentium-M specific code.
67 73

	
68 74
--prefix=PREFIX
69 75

	
70 76
  Set the installation prefix to PREFIX. By default it is /usr/local.
71 77

	
72
--enable-demo
73

	
74
   Build the examples in the demo subdirectory.
75

	
76
--disable-demo
77

	
78
   Do not build the examples in the demo subdirectory (default).
79

	
80 78
--enable-tools
81 79

	
82 80
   Build the programs in the tools subdirectory (default).
83 81

	
84 82
--disable-tools
85 83

	
86 84
   Do not build the programs in the tools subdirectory.
87 85

	
88 86
--with-glpk[=PREFIX]
89 87

	
90 88
   Enable GLPK support (default). You should specify the prefix too if
91 89
   you installed GLPK to some non-standard location (e.g. your home
92 90
   directory). If it is not found, GLPK support will be disabled.
93 91

	
94 92
--with-glpk-includedir=DIR
95 93

	
96 94
   The directory where the GLPK header files are located. This is only
97 95
   useful when the GLPK headers and libraries are not under the same
98 96
   prefix (which is unlikely).
99 97

	
100 98
--with-glpk-libdir=DIR
101 99

	
102 100
   The directory where the GLPK libraries are located. This is only
103 101
   useful when the GLPK headers and libraries are not under the same
104 102
   prefix (which is unlikely).
105 103

	
106 104
--without-glpk
107 105

	
108 106
   Disable GLPK support.
109 107

	
110 108
--with-cplex[=PREFIX]
111 109

	
112 110
   Enable CPLEX support (default). You should specify the prefix too
113 111
   if you installed CPLEX to some non-standard location
114 112
   (e.g. /opt/ilog/cplex75). If it is not found, CPLEX support will be
115 113
   disabled.
116 114

	
117 115
--with-cplex-includedir=DIR
118 116

	
119 117
   The directory where the CPLEX header files are located. This is
120 118
   only useful when the CPLEX headers and libraries are not under the
121 119
   same prefix (e.g.  /usr/local/cplex/cplex75/include).
122 120

	
123 121
--with-cplex-libdir=DIR
124 122

	
125 123
   The directory where the CPLEX libraries are located. This is only
126 124
   useful when the CPLEX headers and libraries are not under the same
127 125
   prefix (e.g.
128 126
   /usr/local/cplex/cplex75/lib/i86_linux2_glibc2.2_gcc3.0/static_pic_mt).
129 127

	
130 128
--without-cplex
131 129

	
132 130
   Disable CPLEX support.
133 131

	
134 132
--with-soplex[=PREFIX]
135 133

	
136 134
   Enable SoPlex support (default). You should specify the prefix too if
137 135
   you installed SoPlex to some non-standard location (e.g. your home
138 136
   directory). If it is not found, SoPlex support will be disabled.
139 137

	
140 138
--with-soplex-includedir=DIR
141 139

	
142 140
   The directory where the SoPlex header files are located. This is only
143 141
   useful when the SoPlex headers and libraries are not under the same
144 142
   prefix (which is unlikely).
145 143

	
146 144
--with-soplex-libdir=DIR
147 145

	
148 146
   The directory where the SoPlex libraries are located. This is only
149 147
   useful when the SoPlex headers and libraries are not under the same
150 148
   prefix (which is unlikely).
151 149

	
152 150
--without-soplex
153 151

	
154 152
   Disable SoPlex support.
153

	
154
--with-coin[=PREFIX]
155

	
156
   Enable support for COIN-OR solvers (CLP and CBC). You should
157
   specify the prefix too. (by default, COIN-OR tools install
158
   themselves to the source code directory). This command enables the
159
   solvers that are actually found.
160

	
161
--with-coin-includedir=DIR
162

	
163
   The directory where the COIN-OR header files are located. This is
164
   only useful when the COIN-OR headers and libraries are not under
165
   the same prefix (which is unlikely).
166

	
167
--with-coin-libdir=DIR
168

	
169
   The directory where the COIN-OR libraries are located. This is only
170
   useful when the COIN-OR headers and libraries are not under the
171
   same prefix (which is unlikely).
172

	
173
--without-coin
174

	
175
   Disable COIN-OR support.
Ignore white space 6 line context
1
LEMON code without an explicit copyright is covered by the following
1
LEMON code without an explicit copyright notice is covered by the following
2 2
copyright/license.
3 3

	
4 4
Copyright (C) 2003-2009 Egervary Jeno Kombinatorikus Optimalizalasi
5 5
Kutatocsoport (Egervary Combinatorial Optimization Research Group,
6 6
EGRES).
7 7

	
8
===========================================================================
9
Boost Software License, Version 1.0
10
===========================================================================
11

	
8 12
Permission is hereby granted, free of charge, to any person or organization
9 13
obtaining a copy of the software and accompanying documentation covered by
10 14
this license (the "Software") to use, reproduce, display, distribute,
11 15
execute, and transmit the Software, and to prepare derivative works of the
12 16
Software, and to permit third-parties to whom the Software is furnished to
13 17
do so, all subject to the following:
14 18

	
15 19
The copyright notices in the Software and this entire statement, including
16 20
the above license grant, this restriction and the following disclaimer,
17 21
must be included in all copies of the Software, in whole or in part, and
18 22
all derivative works of the Software, unless such copies or derivative
19 23
works are solely in the form of machine-executable object code generated by
20 24
a source language processor.
21 25

	
22 26
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 27
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 28
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 29
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 30
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 31
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 32
DEALINGS IN THE SOFTWARE.
29

	
30
===========================================================================
31
This license is a verbatim copy of the Boost Software License, Version 1.0.
32

	
33

	
Ignore white space 6 line context
1 1
ACLOCAL_AMFLAGS = -I m4
2 2

	
3 3
AM_CXXFLAGS = $(WARNINGCXXFLAGS)
4 4

	
5 5
AM_CPPFLAGS = -I$(top_srcdir) -I$(top_builddir)
6 6
LDADD = $(top_builddir)/lemon/libemon.la
7 7

	
8 8
EXTRA_DIST = \
9 9
	AUTHORS \
10 10
	LICENSE \
11 11
	m4/lx_check_cplex.m4 \
12 12
	m4/lx_check_glpk.m4 \
13 13
	m4/lx_check_soplex.m4 \
14
	m4/lx_check_coin.m4 \
14 15
	CMakeLists.txt \
15
	cmake
16
	cmake/FindGhostscript.cmake \
17
	cmake/FindCPLEX.cmake \
18
	cmake/FindGLPK.cmake \
19
	cmake/FindCOIN.cmake \
20
	cmake/LEMONConfig.cmake.in \
21
	cmake/version.cmake.in \
22
	cmake/version.cmake \
23
	cmake/nsis/lemon.ico \
24
	cmake/nsis/uninstall.ico
16 25

	
17 26
pkgconfigdir = $(libdir)/pkgconfig
18 27
lemondir = $(pkgincludedir)
19 28
bitsdir = $(lemondir)/bits
20 29
conceptdir = $(lemondir)/concepts
21 30
pkgconfig_DATA =
22 31
lib_LTLIBRARIES =
23 32
lemon_HEADERS =
24 33
bits_HEADERS =
25 34
concept_HEADERS =
26 35
noinst_HEADERS =
27 36
noinst_PROGRAMS =
28 37
bin_PROGRAMS =
29 38
check_PROGRAMS =
30 39
dist_bin_SCRIPTS =
31 40
TESTS =
32 41
XFAIL_TESTS =
33 42

	
34 43
include lemon/Makefile.am
35 44
include test/Makefile.am
36 45
include doc/Makefile.am
37
include demo/Makefile.am
38 46
include tools/Makefile.am
39 47

	
48
DIST_SUBDIRS = demo
49

	
50
demo:
51
	$(MAKE) $(AM_MAKEFLAGS) -C demo
52

	
40 53
MRPROPERFILES = \
41 54
	aclocal.m4 \
42 55
	config.h.in \
43 56
	config.h.in~ \
44 57
	configure \
45 58
	Makefile.in \
46 59
	build-aux/config.guess \
47 60
	build-aux/config.sub \
48 61
	build-aux/depcomp \
49 62
	build-aux/install-sh \
50 63
	build-aux/ltmain.sh \
51 64
	build-aux/missing \
52 65
	doc/doxygen.log
53 66

	
54 67
mrproper:
55 68
	$(MAKE) $(AM_MAKEFLAGS) maintainer-clean
56 69
	-rm -f $(MRPROPERFILES)
57 70

	
58 71
dist-bz2: dist
59 72
	zcat $(PACKAGE)-$(VERSION).tar.gz | \
60 73
	bzip2 --best -c > $(PACKAGE)-$(VERSION).tar.bz2
61 74

	
62 75
distcheck-bz2: distcheck
63 76
	zcat $(PACKAGE)-$(VERSION).tar.gz | \
64 77
	bzip2 --best -c > $(PACKAGE)-$(VERSION).tar.bz2
65 78

	
66
.PHONY: mrproper dist-bz2 distcheck-bz2
79
.PHONY: demo mrproper dist-bz2 distcheck-bz2
Ignore white space 6 line context
1
2009-05-13 Version 1.1 released
2

	
3
        This is the second stable release of the 1.x series. It
4
        features a better coverage of the tools available in the 0.x
5
        series, a thoroughly reworked LP/MIP interface plus various
6
        improvements in the existing tools.
7

	
8
        * Much improved M$ Windows support
9
          * Various improvements in the CMAKE build system
10
          * Compilation warnings are fixed/suppressed
11
        * Support IBM xlC compiler
12
        * New algorithms
13
          * Connectivity related algorithms (#61)
14
          * Euler walks (#65)
15
          * Preflow push-relabel max. flow algorithm (#176)
16
          * Circulation algorithm (push-relabel based) (#175)
17
          * Suurballe algorithm (#47)
18
          * Gomory-Hu algorithm (#66)
19
          * Hao-Orlin algorithm (#58)
20
          * Edmond's maximum cardinality and weighted matching algorithms
21
            in general graphs (#48,#265)
22
          * Minimum cost arborescence/branching (#60)
23
          * Network Simplex min. cost flow algorithm (#234)
24
        * New data structures
25
          * Full graph structure (#57)
26
          * Grid graph structure (#57)
27
          * Hypercube graph structure (#57)
28
          * Graph adaptors (#67)
29
          * ArcSet and EdgeSet classes (#67)
30
          * Elevator class (#174)
31
        * Other new tools
32
          * LP/MIP interface (#44)
33
            * Support for GLPK, CPLEX, Soplex, COIN-OR CLP and CBC
34
          * Reader for the Nauty file format (#55)
35
          * DIMACS readers (#167)
36
          * Radix sort algorithms (#72)
37
          * RangeIdMap and CrossRefMap (#160)
38
        * New command line tools
39
          * DIMACS to LGF converter (#182)
40
          * lgf-gen - a graph generator (#45)
41
          * DIMACS solver utility (#226)
42
        * Other code improvements
43
          * Lognormal distribution added to Random (#102)
44
          * Better (i.e. O(1) time) item counting in SmartGraph (#3)
45
          * The standard maps of graphs are guaranteed to be
46
            reference maps (#190)
47
        * Miscellaneous
48
          * Various doc improvements
49
          * Improved 0.x -> 1.x converter script
50

	
51
        * Several bugfixes (compared to release 1.0):
52
          #170: Bugfix SmartDigraph::split()
53
          #171: Bugfix in SmartGraph::restoreSnapshot()
54
          #172: Extended test cases for graphs and digraphs
55
          #173: Bugfix in Random
56
                * operator()s always return a double now
57
                * the faulty real<Num>(Num) and real<Num>(Num,Num)
58
                  have been removed
59
          #187: Remove DijkstraWidestPathOperationTraits
60
          #61:  Bugfix in DfsVisit
61
          #193: Bugfix in GraphReader::skipSection()
62
          #195: Bugfix in ConEdgeIt()
63
          #197: Bugfix in heap unionfind
64
                * This bug affects Edmond's general matching algorithms
65
          #207: Fix 'make install' without 'make html' using CMAKE
66
          #208: Suppress or fix VS2008 compilation warnings
67
          ----: Update the LEMON icon
68
          ----: Enable the component-based installer
69
                (in installers made by CPACK)
70
          ----: Set the proper version for CMAKE in the tarballs
71
                (made by autotools)
72
          ----: Minor clarification in the LICENSE file
73
          ----: Add missing unistd.h include to time_measure.h
74
          #204: Compilation bug fixed in graph_to_eps.h with VS2005
75
          #214,#215: windows.h should never be included by lemon headers
76
          #230: Build systems check the availability of 'long long' type
77
          #229: Default implementation of Tolerance<> is used for integer types
78
          #211,#212: Various fixes for compiling on AIX
79
          ----: Improvements in CMAKE config
80
                - docs is installed in share/doc/
81
                - detects newer versions of Ghostscript
82
          #239: Fix missing 'inline' specifier in time_measure.h
83
          #274,#280: Install lemon/config.h
84
          #275: Prefix macro names with LEMON_ in lemon/config.h
85
          ----: Small script for making the release tarballs added
86
          ----: Minor improvement in unify-sources.sh (a76f55d7d397)
87

	
88
2009-03-27 LEMON joins to the COIN-OR initiative
89

	
90
        COIN-OR (Computational Infrastructure for Operations Research,
91
        http://www.coin-or.org) project is an initiative to spur the
92
        development of open-source software for the operations research
93
        community.
94

	
1 95
2008-10-13 Version 1.0 released
2 96

	
3 97
	This is the first stable release of LEMON. Compared to the 0.x
4 98
	release series, it features a considerably smaller but more
5 99
	matured set of tools. The API has also completely revised and
6 100
	changed in several places.
7 101

	
8 102
	* The major name changes compared to the 0.x series (see the
9 103
          Migration Guide in the doc for more details)
10 104
          * Graph -> Digraph, UGraph -> Graph
11 105
          * Edge -> Arc, UEdge -> Edge
12 106
	  * source(UEdge)/target(UEdge) -> u(Edge)/v(Edge)
13 107
	* Other improvements
14 108
	  * Better documentation
15 109
	  * Reviewed and cleaned up codebase
16 110
	  * CMake based build system (along with the autotools based one)
17 111
	* Contents of the library (ported from 0.x)
18 112
	  * Algorithms
19 113
       	    * breadth-first search (bfs.h)
20 114
       	    * depth-first search (dfs.h)
21 115
       	    * Dijkstra's algorithm (dijkstra.h)
22 116
       	    * Kruskal's algorithm (kruskal.h)
23 117
    	  * Data structures
24 118
       	    * graph data structures (list_graph.h, smart_graph.h)
25 119
       	    * path data structures (path.h)
26 120
       	    * binary heap data structure (bin_heap.h)
27 121
       	    * union-find data structures (unionfind.h)
28 122
       	    * miscellaneous property maps (maps.h)
29 123
       	    * two dimensional vector and bounding box (dim2.h)
30 124
          * Concepts
31 125
       	    * graph structure concepts (concepts/digraph.h, concepts/graph.h,
32 126
              concepts/graph_components.h)
33 127
       	    * concepts for other structures (concepts/heap.h, concepts/maps.h,
34 128
	      concepts/path.h)
35 129
    	  * Tools
36 130
       	    * Mersenne twister random number generator (random.h)
37 131
       	    * tools for measuring cpu and wall clock time (time_measure.h)
38 132
       	    * tools for counting steps and events (counter.h)
39 133
       	    * tool for parsing command line arguments (arg_parser.h)
40 134
       	    * tool for visualizing graphs (graph_to_eps.h)
41 135
       	    * tools for reading and writing data in LEMON Graph Format
42 136
              (lgf_reader.h, lgf_writer.h)
43 137
            * tools to handle the anomalies of calculations with
44 138
	      floating point numbers (tolerance.h)
45 139
            * tools to manage RGB colors (color.h)
46 140
    	  * Infrastructure
47 141
       	    * extended assertion handling (assert.h)
48 142
       	    * exception classes and error handling (error.h)
49 143
      	    * concept checking (concept_check.h)
50 144
       	    * commonly used mathematical constants (math.h)
Ignore white space 6 line context
1
==================================================================
2
LEMON - a Library of Efficient Models and Optimization in Networks
3
==================================================================
1
=====================================================================
2
LEMON - a Library for Efficient Modeling and Optimization in Networks
3
=====================================================================
4 4

	
5 5
LEMON is an open source library written in C++. It provides
6 6
easy-to-use implementations of common data structures and algorithms
7 7
in the area of optimization and helps implementing new ones. The main
8 8
focus is on graphs and graph algorithms, thus it is especially
9 9
suitable for solving design and optimization problems of
10 10
telecommunication networks. To achieve wide usability its data
11 11
structures and algorithms provide generic interfaces.
12 12

	
13 13
Contents
14 14
========
15 15

	
16 16
LICENSE
17 17

	
18 18
   Copying, distribution and modification conditions and terms.
19 19

	
20 20
INSTALL
21 21

	
22 22
   General building and installation instructions.
23 23

	
24 24
lemon/
25 25

	
26 26
   Source code of LEMON library.
27 27

	
28 28
doc/
29 29

	
30 30
   Documentation of LEMON. The starting page is doc/html/index.html.
31 31

	
32 32
demo/
33 33

	
34 34
   Some example programs to make you easier to get familiar with LEMON.
35 35

	
36 36
test/
37 37

	
38 38
   Programs to check the integrity and correctness of LEMON.
39 39

	
40 40
tools/
41 41

	
42 42
   Various utilities related to LEMON.
Ignore white space 6 line context
1 1
INCLUDE(FindPackageHandleStandardArgs)
2 2

	
3 3
FIND_PROGRAM(GHOSTSCRIPT_EXECUTABLE
4 4
  NAMES gs gswin32c
5 5
  PATHS "$ENV{ProgramFiles}/gs"
6
  PATH_SUFFIXES gs8.61/bin gs8.62/bin
6
  PATH_SUFFIXES gs8.61/bin gs8.62/bin gs8.63/bin gs8.64/bin gs8.65/bin
7 7
  DOC "Ghostscript: PostScript and PDF language interpreter and previewer."
8 8
)
9 9

	
10 10
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Ghostscript DEFAULT_MSG GHOSTSCRIPT_EXECUTABLE)
Ignore white space 6 line context
modified binary file
Ignore white space 6 line context
1 1
dnl Process this file with autoconf to produce a configure script.
2 2

	
3 3
dnl Version information.
4 4
m4_define([lemon_version_number],
5
	[m4_normalize(esyscmd([echo ${LEMON_VERSION}]))])
5
          [m4_normalize(esyscmd([echo ${LEMON_VERSION}]))])
6 6
dnl m4_define([lemon_version_number], [])
7 7
m4_define([lemon_hg_path], [m4_normalize(esyscmd([./scripts/chg-len.py]))])
8
m4_define([lemon_hg_revision], [m4_normalize(esyscmd([hg id -i]))])
8
m4_define([lemon_hg_revision], [m4_normalize(esyscmd([hg id -i 2> /dev/null]))])
9 9
m4_define([lemon_version], [ifelse(lemon_version_number(),
10
			   [],
11
			   [lemon_hg_path().lemon_hg_revision()],
12
			   [lemon_version_number()])])
10
                           [],
11
                           [ifelse(lemon_hg_revision(),
12
                           [],
13
                           [hg-tip],
14
                           [lemon_hg_path().lemon_hg_revision()])],
15
                           [lemon_version_number()])])
13 16

	
14 17
AC_PREREQ([2.59])
15 18
AC_INIT([LEMON], [lemon_version()], [lemon-user@lemon.cs.elte.hu], [lemon])
16 19
AC_CONFIG_AUX_DIR([build-aux])
17 20
AC_CONFIG_MACRO_DIR([m4])
18 21
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects nostdinc])
19 22
AC_CONFIG_SRCDIR([lemon/list_graph.h])
20 23
AC_CONFIG_HEADERS([config.h lemon/config.h])
21 24

	
25
AC_DEFINE([LEMON_VERSION], [lemon_version()], [The version string])
26

	
22 27
dnl Do compilation tests using the C++ compiler.
23 28
AC_LANG([C++])
24 29

	
30
dnl Check the existence of long long type.
31
AC_CHECK_TYPE(long long, [long_long_found=yes], [long_long_found=no])
32
if test x"$long_long_found" = x"yes"; then
33
  AC_DEFINE([LEMON_HAVE_LONG_LONG], [1], [Define to 1 if you have long long.])
34
fi
35

	
25 36
dnl Checks for programs.
26 37
AC_PROG_CXX
27 38
AC_PROG_CXXCPP
28 39
AC_PROG_INSTALL
29 40
AC_DISABLE_SHARED
30 41
AC_PROG_LIBTOOL
31 42

	
32 43
AC_CHECK_PROG([doxygen_found],[doxygen],[yes],[no])
44
AC_CHECK_PROG([python_found],[python],[yes],[no])
33 45
AC_CHECK_PROG([gs_found],[gs],[yes],[no])
34 46

	
35 47
dnl Detect Intel compiler.
36 48
AC_MSG_CHECKING([whether we are using the Intel C++ compiler])
37 49
AC_COMPILE_IFELSE([#ifndef __INTEL_COMPILER
38 50
choke me
39 51
#endif], [ICC=[yes]], [ICC=[no]])
40 52
if test x"$ICC" = x"yes"; then
41 53
  AC_MSG_RESULT([yes])
42 54
else
43 55
  AC_MSG_RESULT([no])
44 56
fi
45 57

	
46 58
dnl Set custom compiler flags when using g++.
47 59
if test "$GXX" = yes -a "$ICC" = no; then
48 60
  WARNINGCXXFLAGS="-Wall -W -Wall -W -Wunused -Wformat=2 -Wctor-dtor-privacy -Wnon-virtual-dtor -Wno-char-subscripts -Wwrite-strings -Wno-char-subscripts -Wreturn-type -Wcast-qual -Wcast-align -Wsign-promo -Woverloaded-virtual -ansi -fno-strict-aliasing -Wold-style-cast -Wno-unknown-pragmas"
49 61
fi
50 62
AC_SUBST([WARNINGCXXFLAGS])
51 63

	
52 64
dnl Checks for libraries.
53 65
LX_CHECK_GLPK
54 66
LX_CHECK_CPLEX
55 67
LX_CHECK_SOPLEX
56
LX_CHECK_CLP
68
LX_CHECK_COIN
57 69

	
58 70
AM_CONDITIONAL([HAVE_LP], [test x"$lx_lp_found" = x"yes"])
59 71
AM_CONDITIONAL([HAVE_MIP], [test x"$lx_mip_found" = x"yes"])
60 72

	
61
dnl Disable/enable building the demo programs.
62
AC_ARG_ENABLE([demo],
63
AS_HELP_STRING([--enable-demo], [build the demo programs])
64
AS_HELP_STRING([--disable-demo], [do not build the demo programs @<:@default@:>@]),
65
              [], [enable_demo=no])
66
AC_MSG_CHECKING([whether to build the demo programs])
67
if test x"$enable_demo" != x"no"; then
68
  AC_MSG_RESULT([yes])
69
else
70
  AC_MSG_RESULT([no])
71
fi
72
AM_CONDITIONAL([WANT_DEMO], [test x"$enable_demo" != x"no"])
73

	
74 73
dnl Disable/enable building the binary tools.
75 74
AC_ARG_ENABLE([tools],
76 75
AS_HELP_STRING([--enable-tools], [build additional tools @<:@default@:>@])
77 76
AS_HELP_STRING([--disable-tools], [do not build additional tools]),
78 77
              [], [enable_tools=yes])
79 78
AC_MSG_CHECKING([whether to build the additional tools])
80 79
if test x"$enable_tools" != x"no"; then
81 80
  AC_MSG_RESULT([yes])
82 81
else
83 82
  AC_MSG_RESULT([no])
84 83
fi
85 84
AM_CONDITIONAL([WANT_TOOLS], [test x"$enable_tools" != x"no"])
86 85

	
87 86
dnl Checks for header files.
88 87
AC_CHECK_HEADERS(limits.h sys/time.h sys/times.h unistd.h)
89 88

	
90 89
dnl Checks for typedefs, structures, and compiler characteristics.
91 90
AC_C_CONST
92 91
AC_C_INLINE
93 92
AC_TYPE_SIZE_T
94 93
AC_HEADER_TIME
95 94
AC_STRUCT_TM
96 95

	
97 96
dnl Checks for library functions.
98 97
AC_HEADER_STDC
99 98
AC_CHECK_FUNCS(gettimeofday times ctime_r)
100 99

	
101 100
dnl Add dependencies on files generated by configure.
102 101
AC_SUBST([CONFIG_STATUS_DEPENDENCIES],
103
  ['$(top_srcdir)/doc/Doxyfile.in $(top_srcdir)/lemon/lemon.pc.in'])
102
  ['$(top_srcdir)/doc/Doxyfile.in $(top_srcdir)/lemon/lemon.pc.in $(top_srcdir)/cmake/version.cmake.in'])
104 103

	
105 104
AC_CONFIG_FILES([
106 105
Makefile
106
demo/Makefile
107
cmake/version.cmake
107 108
doc/Doxyfile
108 109
lemon/lemon.pc
109 110
])
110 111

	
111 112
AC_OUTPUT
112 113

	
113 114
echo
114 115
echo '****************************** SUMMARY ******************************'
115 116
echo
116 117
echo Package version............... : $PACKAGE-$VERSION
117 118
echo
118 119
echo C++ compiler.................. : $CXX
119 120
echo C++ compiles flags............ : $WARNINGCXXFLAGS $CXXFLAGS
120 121
echo
122
echo Compiler supports long long... : $long_long_found
123
echo
121 124
echo GLPK support.................. : $lx_glpk_found
122 125
echo CPLEX support................. : $lx_cplex_found
123 126
echo SOPLEX support................ : $lx_soplex_found
124 127
echo CLP support................... : $lx_clp_found
128
echo CBC support................... : $lx_cbc_found
125 129
echo
126
echo Build demo programs........... : $enable_demo
127 130
echo Build additional tools........ : $enable_tools
128 131
echo
129 132
echo The packace will be installed in
130 133
echo -n '  '
131 134
echo $prefix.
132 135
echo
133 136
echo '*********************************************************************'
134 137

	
135 138
echo
136 139
echo Configure complete, now type \'make\' and then \'make install\'.
137 140
echo
Ignore white space 6 line context
1
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
1
INCLUDE_DIRECTORIES(
2
  ${PROJECT_SOURCE_DIR}
3
  ${PROJECT_BINARY_DIR}
4
)
2 5

	
3
LINK_DIRECTORIES(${CMAKE_BINARY_DIR}/lemon)
6
LINK_DIRECTORIES(
7
  ${PROJECT_BINARY_DIR}/lemon
8
)
4 9

	
5 10
SET(DEMOS
6 11
  arg_parser_demo
7 12
  graph_to_eps_demo
8
  lgf_demo)
13
  lgf_demo
14
)
9 15

	
10 16
FOREACH(DEMO_NAME ${DEMOS})
11 17
  ADD_EXECUTABLE(${DEMO_NAME} ${DEMO_NAME}.cc)
12 18
  TARGET_LINK_LIBRARIES(${DEMO_NAME} lemon)
13
ENDFOREACH(DEMO_NAME)
19
ENDFOREACH()
Ignore white space 6 line context
1
EXTRA_DIST += \
2
	demo/CMakeLists.txt \
3
	demo/digraph.lgf
1
AM_CXXFLAGS = $(WARNINGCXXFLAGS)
4 2

	
5
if WANT_DEMO
3
AM_CPPFLAGS = -I$(top_srcdir) -I$(top_builddir)
4
LDADD = $(top_builddir)/lemon/libemon.la
6 5

	
7
noinst_PROGRAMS += \
8
	demo/arg_parser_demo \
9
	demo/graph_to_eps_demo \
10
	demo/lgf_demo
6
EXTRA_DIST = \
7
	CMakeLists.txt \
8
	digraph.lgf
11 9

	
12
endif WANT_DEMO
10
noinst_PROGRAMS = \
11
	arg_parser_demo \
12
	graph_to_eps_demo \
13
	lgf_demo
13 14

	
14
demo_arg_parser_demo_SOURCES = demo/arg_parser_demo.cc
15
demo_graph_to_eps_demo_SOURCES = demo/graph_to_eps_demo.cc
16
demo_lgf_demo_SOURCES = demo/lgf_demo.cc
15
arg_parser_demo_SOURCES = arg_parser_demo.cc
16
graph_to_eps_demo_SOURCES = graph_to_eps_demo.cc
17
lgf_demo_SOURCES = lgf_demo.cc
Ignore white space 6 line context
... ...
@@ -89,118 +89,118 @@
89 89
    run();
90 90

	
91 91
  cout << "Create 'graph_to_eps_demo_out_2.eps'" << endl;
92 92
  graphToEps(g,"graph_to_eps_demo_out_2.eps").
93 93
    coords(coords).
94 94
    title("Sample .eps figure").
95 95
    copyright("(C) 2003-2009 LEMON Project").
96 96
    absoluteNodeSizes().absoluteArcWidths().
97 97
    nodeScale(2).nodeSizes(sizes).
98 98
    nodeShapes(shapes).
99 99
    nodeColors(composeMap(palette,colors)).
100 100
    arcColors(composeMap(palette,acolors)).
101 101
    arcWidthScale(.4).arcWidths(widths).
102 102
    nodeTexts(id).nodeTextSize(3).
103 103
    run();
104 104

	
105 105
  cout << "Create 'graph_to_eps_demo_out_3_arr.eps'" << endl;
106 106
  graphToEps(g,"graph_to_eps_demo_out_3_arr.eps").
107 107
    title("Sample .eps figure (with arrowheads)").
108 108
    copyright("(C) 2003-2009 LEMON Project").
109 109
    absoluteNodeSizes().absoluteArcWidths().
110 110
    nodeColors(composeMap(palette,colors)).
111 111
    coords(coords).
112 112
    nodeScale(2).nodeSizes(sizes).
113 113
    nodeShapes(shapes).
114 114
    arcColors(composeMap(palette,acolors)).
115 115
    arcWidthScale(.4).arcWidths(widths).
116 116
    nodeTexts(id).nodeTextSize(3).
117 117
    drawArrows().arrowWidth(2).arrowLength(2).
118 118
    run();
119 119

	
120 120
  // Add more arcs to the digraph
121 121
  a=g.addArc(n1,n4); acolors[a]=2; widths[a]=1;
122 122
  a=g.addArc(n4,n1); acolors[a]=1; widths[a]=2;
123 123

	
124 124
  a=g.addArc(n1,n2); acolors[a]=1; widths[a]=1;
125 125
  a=g.addArc(n1,n2); acolors[a]=2; widths[a]=1;
126 126
  a=g.addArc(n1,n2); acolors[a]=3; widths[a]=1;
127 127
  a=g.addArc(n1,n2); acolors[a]=4; widths[a]=1;
128 128
  a=g.addArc(n1,n2); acolors[a]=5; widths[a]=1;
129 129
  a=g.addArc(n1,n2); acolors[a]=6; widths[a]=1;
130 130
  a=g.addArc(n1,n2); acolors[a]=7; widths[a]=1;
131 131

	
132 132
  cout << "Create 'graph_to_eps_demo_out_4_par.eps'" << endl;
133 133
  graphToEps(g,"graph_to_eps_demo_out_4_par.eps").
134 134
    title("Sample .eps figure (parallel arcs)").
135 135
    copyright("(C) 2003-2009 LEMON Project").
136 136
    absoluteNodeSizes().absoluteArcWidths().
137 137
    nodeShapes(shapes).
138 138
    coords(coords).
139 139
    nodeScale(2).nodeSizes(sizes).
140 140
    nodeColors(composeMap(palette,colors)).
141 141
    arcColors(composeMap(palette,acolors)).
142 142
    arcWidthScale(.4).arcWidths(widths).
143 143
    nodeTexts(id).nodeTextSize(3).
144 144
    enableParallel().parArcDist(1.5).
145 145
    run();
146 146

	
147 147
  cout << "Create 'graph_to_eps_demo_out_5_par_arr.eps'" << endl;
148 148
  graphToEps(g,"graph_to_eps_demo_out_5_par_arr.eps").
149 149
    title("Sample .eps figure (parallel arcs and arrowheads)").
150 150
    copyright("(C) 2003-2009 LEMON Project").
151 151
    absoluteNodeSizes().absoluteArcWidths().
152 152
    nodeScale(2).nodeSizes(sizes).
153 153
    coords(coords).
154 154
    nodeShapes(shapes).
155 155
    nodeColors(composeMap(palette,colors)).
156 156
    arcColors(composeMap(palette,acolors)).
157 157
    arcWidthScale(.3).arcWidths(widths).
158 158
    nodeTexts(id).nodeTextSize(3).
159 159
    enableParallel().parArcDist(1).
160 160
    drawArrows().arrowWidth(1).arrowLength(1).
161 161
    run();
162 162

	
163 163
  cout << "Create 'graph_to_eps_demo_out_6_par_arr_a4.eps'" << endl;
164 164
  graphToEps(g,"graph_to_eps_demo_out_6_par_arr_a4.eps").
165 165
    title("Sample .eps figure (fits to A4)").
166 166
    copyright("(C) 2003-2009 LEMON Project").
167 167
    scaleToA4().
168 168
    absoluteNodeSizes().absoluteArcWidths().
169 169
    nodeScale(2).nodeSizes(sizes).
170 170
    coords(coords).
171 171
    nodeShapes(shapes).
172 172
    nodeColors(composeMap(palette,colors)).
173 173
    arcColors(composeMap(palette,acolors)).
174 174
    arcWidthScale(.3).arcWidths(widths).
175 175
    nodeTexts(id).nodeTextSize(3).
176 176
    enableParallel().parArcDist(1).
177 177
    drawArrows().arrowWidth(1).arrowLength(1).
178 178
    run();
179 179

	
180 180
  // Create an .eps file showing the colors of a default Palette
181 181
  ListDigraph h;
182 182
  ListDigraph::NodeMap<int> hcolors(h);
183 183
  ListDigraph::NodeMap<Point> hcoords(h);
184 184

	
185
  int cols=int(sqrt(double(palette.size())));
185
  int cols=int(std::sqrt(double(palette.size())));
186 186
  for(int i=0;i<int(paletteW.size());i++) {
187 187
    Node n=h.addNode();
188 188
    hcoords[n]=Point(1+i%cols,1+i/cols);
189 189
    hcolors[n]=i;
190 190
  }
191 191

	
192 192
  cout << "Create 'graph_to_eps_demo_out_7_colors.eps'" << endl;
193 193
  graphToEps(h,"graph_to_eps_demo_out_7_colors.eps").
194 194
    scale(60).
195 195
    title("Sample .eps figure (Palette demo)").
196 196
    copyright("(C) 2003-2009 LEMON Project").
197 197
    coords(hcoords).
198 198
    absoluteNodeSizes().absoluteArcWidths().
199 199
    nodeScale(.45).
200 200
    distantColorNodeTexts().
201 201
    nodeTexts(hcolors).nodeTextSize(.6).
202 202
    nodeColors(composeMap(paletteW,hcolors)).
203 203
    run();
204 204

	
205 205
  return 0;
206 206
}
Ignore white space 6 line context
1 1
SET(PACKAGE_NAME ${PROJECT_NAME})
2 2
SET(PACKAGE_VERSION ${PROJECT_VERSION})
3
SET(abs_top_srcdir ${CMAKE_SOURCE_DIR})
4
SET(abs_top_builddir ${CMAKE_BINARY_DIR})
3
SET(abs_top_srcdir ${PROJECT_SOURCE_DIR})
4
SET(abs_top_builddir ${PROJECT_BINARY_DIR})
5 5

	
6 6
CONFIGURE_FILE(
7
  ${CMAKE_SOURCE_DIR}/doc/Doxyfile.in
8
  ${CMAKE_BINARY_DIR}/doc/Doxyfile
9
  @ONLY)
7
  ${PROJECT_SOURCE_DIR}/doc/Doxyfile.in
8
  ${PROJECT_BINARY_DIR}/doc/Doxyfile
9
  @ONLY
10
)
10 11

	
11
IF(DOXYGEN_EXECUTABLE AND GHOSTSCRIPT_EXECUTABLE)
12
IF(DOXYGEN_EXECUTABLE AND PYTHONINTERP_FOUND AND GHOSTSCRIPT_EXECUTABLE)
13
  FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/)
14
  SET(GHOSTSCRIPT_OPTIONS -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha)
15
  ADD_CUSTOM_TARGET(html
16
    COMMAND ${CMAKE_COMMAND} -E remove_directory gen-images
17
    COMMAND ${CMAKE_COMMAND} -E make_directory gen-images
18
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/bipartite_matching.png ${CMAKE_CURRENT_SOURCE_DIR}/images/bipartite_matching.eps
19
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/bipartite_partitions.png ${CMAKE_CURRENT_SOURCE_DIR}/images/bipartite_partitions.eps
20
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/connected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/connected_components.eps
21
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/edge_biconnected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/edge_biconnected_components.eps
22
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/grid_graph.png ${CMAKE_CURRENT_SOURCE_DIR}/images/grid_graph.eps
23
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/node_biconnected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/node_biconnected_components.eps
24
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_0.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_0.eps
25
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_1.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_1.eps
26
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_2.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_2.eps
27
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_3.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_3.eps
28
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_4.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_4.eps
29
    COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/strongly_connected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/strongly_connected_components.eps
30
    COMMAND ${CMAKE_COMMAND} -E remove_directory html
31
    COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/scripts/bib2dox.py ${CMAKE_CURRENT_SOURCE_DIR}/references.bib >references.dox
32
    COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile
33
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
34
  )
35

	
36
  SET_TARGET_PROPERTIES(html PROPERTIES PROJECT_LABEL BUILD_DOC)
37

	
12 38
  IF(UNIX)
13
    ADD_CUSTOM_TARGET(html
14
      COMMAND rm -rf gen-images
15
      COMMAND mkdir gen-images
16
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/grid_graph.png ${CMAKE_CURRENT_SOURCE_DIR}/images/grid_graph.eps
17
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_0.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_0.eps
18
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_1.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_1.eps
19
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_2.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_2.eps
20
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_3.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_3.eps
21
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_4.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_4.eps
22
      COMMAND rm -rf html
23
      COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile
24
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
39
    INSTALL(
40
      DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/
41
      DESTINATION share/doc/lemon/html
42
      COMPONENT html_documentation
43
    )
25 44
  ELSEIF(WIN32)
26
    ADD_CUSTOM_TARGET(html
27
      COMMAND if exist gen-images rmdir /s /q gen-images
28
      COMMAND mkdir gen-images
29
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_0.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_0.eps
30
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_1.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_1.eps
31
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_2.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_2.eps
32
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_3.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_3.eps
33
      COMMAND ${GHOSTSCRIPT_EXECUTABLE} -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha -r18 -sOutputFile=gen-images/nodeshape_4.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_4.eps
34
      COMMAND if exist html rmdir /s /q html
35
      COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile
36
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
37
  ENDIF(UNIX)
38
ENDIF(DOXYGEN_EXECUTABLE AND GHOSTSCRIPT_EXECUTABLE)
45
    INSTALL(
46
      DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/
47
      DESTINATION doc
48
      COMPONENT html_documentation
49
    )
50
  ENDIF()
39 51

	
40
INSTALL(
41
  DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/
42
  DESTINATION doc
43
  COMPONENT html_documentation)
52
ENDIF()
Ignore white space 6 line context
1
# Doxyfile 1.5.7.1
1
# Doxyfile 1.5.9
2 2

	
3 3
#---------------------------------------------------------------------------
4 4
# Project related configuration options
5 5
#---------------------------------------------------------------------------
6 6
DOXYFILE_ENCODING      = UTF-8
7 7
PROJECT_NAME           = @PACKAGE_NAME@
8 8
PROJECT_NUMBER         = @PACKAGE_VERSION@
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
DETAILS_AT_TOP         = YES
25 24
INHERIT_DOCS           = NO
26 25
SEPARATE_MEMBER_PAGES  = NO
27 26
TAB_SIZE               = 8
28 27
ALIASES                = 
29 28
OPTIMIZE_OUTPUT_FOR_C  = NO
30 29
OPTIMIZE_OUTPUT_JAVA   = NO
31 30
OPTIMIZE_FOR_FORTRAN   = NO
32 31
OPTIMIZE_OUTPUT_VHDL   = NO
33 32
BUILTIN_STL_SUPPORT    = YES
34 33
CPP_CLI_SUPPORT        = NO
35 34
SIP_SUPPORT            = NO
36 35
IDL_PROPERTY_SUPPORT   = YES
37 36
DISTRIBUTE_GROUP_DOC   = NO
38 37
SUBGROUPING            = YES
39 38
TYPEDEF_HIDES_STRUCT   = NO
40 39
SYMBOL_CACHE_SIZE      = 0
41 40
#---------------------------------------------------------------------------
42 41
# Build related configuration options
43 42
#---------------------------------------------------------------------------
44 43
EXTRACT_ALL            = NO
45 44
EXTRACT_PRIVATE        = YES
46 45
EXTRACT_STATIC         = YES
47 46
EXTRACT_LOCAL_CLASSES  = NO
48 47
EXTRACT_LOCAL_METHODS  = NO
49 48
EXTRACT_ANON_NSPACES   = NO
50 49
HIDE_UNDOC_MEMBERS     = YES
51 50
HIDE_UNDOC_CLASSES     = YES
52 51
HIDE_FRIEND_COMPOUNDS  = NO
53 52
HIDE_IN_BODY_DOCS      = NO
54 53
INTERNAL_DOCS          = NO
55 54
CASE_SENSE_NAMES       = YES
56 55
HIDE_SCOPE_NAMES       = YES
57 56
SHOW_INCLUDE_FILES     = YES
58 57
INLINE_INFO            = YES
59 58
SORT_MEMBER_DOCS       = NO
60 59
SORT_BRIEF_DOCS        = NO
61 60
SORT_GROUP_NAMES       = NO
62 61
SORT_BY_SCOPE_NAME     = NO
63 62
GENERATE_TODOLIST      = YES
64 63
GENERATE_TESTLIST      = YES
65 64
GENERATE_BUGLIST       = YES
66 65
GENERATE_DEPRECATEDLIST= YES
67 66
ENABLED_SECTIONS       = 
68 67
MAX_INITIALIZER_LINES  = 5
69 68
SHOW_USED_FILES        = NO
70 69
SHOW_DIRECTORIES       = YES
71 70
SHOW_FILES             = YES
72 71
SHOW_NAMESPACES        = YES
73 72
FILE_VERSION_FILTER    = 
74 73
LAYOUT_FILE            = DoxygenLayout.xml
75 74
#---------------------------------------------------------------------------
76 75
# configuration options related to warning and progress messages
77 76
#---------------------------------------------------------------------------
78 77
QUIET                  = NO
79 78
WARNINGS               = YES
80 79
WARN_IF_UNDOCUMENTED   = YES
81 80
WARN_IF_DOC_ERROR      = YES
82 81
WARN_NO_PARAMDOC       = NO
83 82
WARN_FORMAT            = "$file:$line: $text"
84 83
WARN_LOGFILE           = doxygen.log
85 84
#---------------------------------------------------------------------------
86 85
# configuration options related to the input files
87 86
#---------------------------------------------------------------------------
88 87
INPUT                  = "@abs_top_srcdir@/doc" \
89 88
                         "@abs_top_srcdir@/lemon" \
90 89
                         "@abs_top_srcdir@/lemon/bits" \
91 90
                         "@abs_top_srcdir@/lemon/concepts" \
92 91
                         "@abs_top_srcdir@/demo" \
93 92
                         "@abs_top_srcdir@/tools" \
94
                         "@abs_top_srcdir@/test/test_tools.h"
93
                         "@abs_top_srcdir@/test/test_tools.h" \
94
                         "@abs_top_builddir@/doc/references.dox"
95 95
INPUT_ENCODING         = UTF-8
96 96
FILE_PATTERNS          = *.h \
97 97
                         *.cc \
98 98
                         *.dox
99 99
RECURSIVE              = NO
100 100
EXCLUDE                = 
101 101
EXCLUDE_SYMLINKS       = NO
102 102
EXCLUDE_PATTERNS       = 
103 103
EXCLUDE_SYMBOLS        = 
104 104
EXAMPLE_PATH           = "@abs_top_srcdir@/demo" \
105 105
                         "@abs_top_srcdir@/LICENSE" \
106 106
                         "@abs_top_srcdir@/doc"
107 107
EXAMPLE_PATTERNS       = 
108 108
EXAMPLE_RECURSIVE      = NO
109 109
IMAGE_PATH             = "@abs_top_srcdir@/doc/images" \
110 110
                         "@abs_top_builddir@/doc/gen-images"
111 111
INPUT_FILTER           = 
112 112
FILTER_PATTERNS        = 
113 113
FILTER_SOURCE_FILES    = NO
114 114
#---------------------------------------------------------------------------
115 115
# configuration options related to source browsing
116 116
#---------------------------------------------------------------------------
117 117
SOURCE_BROWSER         = NO
118 118
INLINE_SOURCES         = NO
119 119
STRIP_CODE_COMMENTS    = YES
120 120
REFERENCED_BY_RELATION = NO
121 121
REFERENCES_RELATION    = NO
122 122
REFERENCES_LINK_SOURCE = YES
123 123
USE_HTAGS              = NO
124 124
VERBATIM_HEADERS       = NO
125 125
#---------------------------------------------------------------------------
126 126
# configuration options related to the alphabetical class index
127 127
#---------------------------------------------------------------------------
128 128
ALPHABETICAL_INDEX     = YES
129 129
COLS_IN_ALPHA_INDEX    = 2
130 130
IGNORE_PREFIX          = 
131 131
#---------------------------------------------------------------------------
132 132
# configuration options related to the HTML output
133 133
#---------------------------------------------------------------------------
134 134
GENERATE_HTML          = YES
135 135
HTML_OUTPUT            = html
136 136
HTML_FILE_EXTENSION    = .html
137 137
HTML_HEADER            = 
138 138
HTML_FOOTER            = 
139 139
HTML_STYLESHEET        = 
140 140
HTML_ALIGN_MEMBERS     = YES
141 141
HTML_DYNAMIC_SECTIONS  = NO
142 142
GENERATE_DOCSET        = NO
143 143
DOCSET_FEEDNAME        = "Doxygen generated docs"
144 144
DOCSET_BUNDLE_ID       = org.doxygen.Project
145 145
GENERATE_HTMLHELP      = NO
146 146
CHM_FILE               = 
147 147
HHC_LOCATION           = 
148 148
GENERATE_CHI           = NO
149 149
CHM_INDEX_ENCODING     = 
150 150
BINARY_TOC             = NO
151 151
TOC_EXPAND             = NO
152 152
GENERATE_QHP           = NO
153 153
QCH_FILE               = 
154 154
QHP_NAMESPACE          = org.doxygen.Project
155 155
QHP_VIRTUAL_FOLDER     = doc
156 156
QHG_LOCATION           = 
157 157
DISABLE_INDEX          = NO
158 158
ENUM_VALUES_PER_LINE   = 4
159 159
GENERATE_TREEVIEW      = NO
160 160
TREEVIEW_WIDTH         = 250
161 161
FORMULA_FONTSIZE       = 10
162 162
#---------------------------------------------------------------------------
163 163
# configuration options related to the LaTeX output
164 164
#---------------------------------------------------------------------------
165 165
GENERATE_LATEX         = NO
166 166
LATEX_OUTPUT           = latex
167 167
LATEX_CMD_NAME         = latex
168 168
MAKEINDEX_CMD_NAME     = makeindex
169 169
COMPACT_LATEX          = YES
170 170
PAPER_TYPE             = a4wide
171 171
EXTRA_PACKAGES         = amsmath \
172 172
                         amssymb
173 173
LATEX_HEADER           = 
174 174
PDF_HYPERLINKS         = YES
175 175
USE_PDFLATEX           = YES
176 176
LATEX_BATCHMODE        = NO
177 177
LATEX_HIDE_INDICES     = NO
178 178
#---------------------------------------------------------------------------
179 179
# configuration options related to the RTF output
180 180
#---------------------------------------------------------------------------
181 181
GENERATE_RTF           = NO
182 182
RTF_OUTPUT             = rtf
183 183
COMPACT_RTF            = NO
184 184
RTF_HYPERLINKS         = NO
185 185
RTF_STYLESHEET_FILE    = 
186 186
RTF_EXTENSIONS_FILE    = 
187 187
#---------------------------------------------------------------------------
188 188
# configuration options related to the man page output
189 189
#---------------------------------------------------------------------------
190 190
GENERATE_MAN           = NO
191 191
MAN_OUTPUT             = man
192 192
MAN_EXTENSION          = .3
193 193
MAN_LINKS              = NO
194 194
#---------------------------------------------------------------------------
195 195
# configuration options related to the XML output
196 196
#---------------------------------------------------------------------------
197 197
GENERATE_XML           = NO
198 198
XML_OUTPUT             = xml
199 199
XML_SCHEMA             = 
200 200
XML_DTD                = 
201 201
XML_PROGRAMLISTING     = YES
202 202
#---------------------------------------------------------------------------
203 203
# configuration options for the AutoGen Definitions output
204 204
#---------------------------------------------------------------------------
205 205
GENERATE_AUTOGEN_DEF   = NO
206 206
#---------------------------------------------------------------------------
207 207
# configuration options related to the Perl module output
208 208
#---------------------------------------------------------------------------
209 209
GENERATE_PERLMOD       = NO
210 210
PERLMOD_LATEX          = NO
211 211
PERLMOD_PRETTY         = YES
212 212
PERLMOD_MAKEVAR_PREFIX = 
213 213
#---------------------------------------------------------------------------
214 214
# Configuration options related to the preprocessor   
215 215
#---------------------------------------------------------------------------
216 216
ENABLE_PREPROCESSING   = YES
217 217
MACRO_EXPANSION        = NO
218 218
EXPAND_ONLY_PREDEF     = NO
219 219
SEARCH_INCLUDES        = YES
220 220
INCLUDE_PATH           = 
221 221
INCLUDE_FILE_PATTERNS  = 
222 222
PREDEFINED             = DOXYGEN
223 223
EXPAND_AS_DEFINED      = 
224 224
SKIP_FUNCTION_MACROS   = YES
225 225
#---------------------------------------------------------------------------
226
# Configuration::additions related to external references   
226
# Options related to the search engine   
227 227
#---------------------------------------------------------------------------
228 228
TAGFILES               = "@abs_top_srcdir@/doc/libstdc++.tag = http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/  "
229 229
GENERATE_TAGFILE       = html/lemon.tag
230 230
ALLEXTERNALS           = NO
231 231
EXTERNAL_GROUPS        = NO
232 232
PERL_PATH              = /usr/bin/perl
233 233
#---------------------------------------------------------------------------
234 234
# Configuration options related to the dot tool   
235 235
#---------------------------------------------------------------------------
236 236
CLASS_DIAGRAMS         = YES
237 237
MSCGEN_PATH            = 
238 238
HIDE_UNDOC_RELATIONS   = YES
239 239
HAVE_DOT               = YES
240 240
DOT_FONTNAME           = FreeSans
241 241
DOT_FONTSIZE           = 10
242 242
DOT_FONTPATH           = 
243 243
CLASS_GRAPH            = YES
244 244
COLLABORATION_GRAPH    = NO
245 245
GROUP_GRAPHS           = NO
246 246
UML_LOOK               = NO
247 247
TEMPLATE_RELATIONS     = NO
248 248
INCLUDE_GRAPH          = NO
249 249
INCLUDED_BY_GRAPH      = NO
250 250
CALL_GRAPH             = NO
251 251
CALLER_GRAPH           = NO
252 252
GRAPHICAL_HIERARCHY    = NO
253 253
DIRECTORY_GRAPH        = NO
254 254
DOT_IMAGE_FORMAT       = png
255 255
DOT_PATH               = 
256 256
DOTFILE_DIRS           = 
257 257
DOT_GRAPH_MAX_NODES    = 50
258 258
MAX_DOT_GRAPH_DEPTH    = 0
259 259
DOT_TRANSPARENT        = NO
260 260
DOT_MULTI_TARGETS      = NO
261 261
GENERATE_LEGEND        = YES
262 262
DOT_CLEANUP            = YES
263 263
#---------------------------------------------------------------------------
264 264
# Configuration::additions related to the search engine   
265 265
#---------------------------------------------------------------------------
266 266
SEARCHENGINE           = NO
Ignore white space 6 line context
1 1
EXTRA_DIST += \
2 2
	doc/Doxyfile.in \
3 3
	doc/DoxygenLayout.xml \
4 4
	doc/coding_style.dox \
5 5
	doc/dirs.dox \
6 6
	doc/groups.dox \
7 7
	doc/lgf.dox \
8 8
	doc/license.dox \
9 9
	doc/mainpage.dox \
10 10
	doc/migration.dox \
11
	doc/min_cost_flow.dox \
11 12
	doc/named-param.dox \
12 13
	doc/namespaces.dox \
13 14
	doc/html \
14 15
	doc/CMakeLists.txt
15 16

	
16 17
DOC_EPS_IMAGES18 = \
17 18
	grid_graph.eps \
18 19
	nodeshape_0.eps \
19 20
	nodeshape_1.eps \
20 21
	nodeshape_2.eps \
21 22
	nodeshape_3.eps \
22 23
	nodeshape_4.eps
23 24

	
25
DOC_EPS_IMAGES27 = \
26
	bipartite_matching.eps \
27
	bipartite_partitions.eps \
28
	connected_components.eps \
29
	edge_biconnected_components.eps \
30
	node_biconnected_components.eps \
31
	strongly_connected_components.eps
32

	
24 33
DOC_EPS_IMAGES = \
25
	$(DOC_EPS_IMAGES18)
34
	$(DOC_EPS_IMAGES18) \
35
	$(DOC_EPS_IMAGES27)
26 36

	
27 37
DOC_PNG_IMAGES = \
28 38
	$(DOC_EPS_IMAGES:%.eps=doc/gen-images/%.png)
29 39

	
30 40
EXTRA_DIST += $(DOC_EPS_IMAGES:%=doc/images/%)
31 41

	
32 42
doc/html:
33 43
	$(MAKE) $(AM_MAKEFLAGS) html
34 44

	
35 45
GS_COMMAND=gs -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4
36 46

	
37 47
$(DOC_EPS_IMAGES18:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps
38 48
	-mkdir doc/gen-images
39 49
	if test ${gs_found} = yes; then \
40 50
	  $(GS_COMMAND) -sDEVICE=pngalpha -r18 -sOutputFile=$@ $<; \
41 51
	else \
42 52
	  echo; \
43 53
	  echo "Ghostscript not found."; \
44 54
	  echo; \
45 55
	  exit 1; \
46 56
	fi
47 57

	
48
html-local: $(DOC_PNG_IMAGES)
58
$(DOC_EPS_IMAGES27:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps
59
	-mkdir doc/gen-images
60
	if test ${gs_found} = yes; then \
61
	  $(GS_COMMAND) -sDEVICE=pngalpha -r27 -sOutputFile=$@ $<; \
62
	else \
63
	  echo; \
64
	  echo "Ghostscript not found."; \
65
	  echo; \
66
	  exit 1; \
67
	fi
68

	
69
references.dox: doc/references.bib
70
	if test ${python_found} = yes; then \
71
	  cd doc; \
72
	  python @abs_top_srcdir@/scripts/bib2dox.py @abs_top_builddir@/$< >$@; \
73
	  cd ..; \
74
	else \
75
	  echo; \
76
	  echo "Python not found."; \
77
	  echo; \
78
	  exit 1; \
79
	fi
80

	
81
html-local: $(DOC_PNG_IMAGES) references.dox
49 82
	if test ${doxygen_found} = yes; then \
50 83
	  cd doc; \
51 84
	  doxygen Doxyfile; \
52 85
	  cd ..; \
53 86
	else \
54 87
	  echo; \
55 88
	  echo "Doxygen not found."; \
56 89
	  echo; \
57 90
	  exit 1; \
58 91
	fi
59 92

	
60 93
clean-local:
61 94
	-rm -rf doc/html
62 95
	-rm -f doc/doxygen.log
63 96
	-rm -f $(DOC_PNG_IMAGES)
64 97
	-rm -rf doc/gen-images
65 98

	
66 99
update-external-tags:
67 100
	wget -O doc/libstdc++.tag.tmp http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/libstdc++.tag && \
68 101
	mv doc/libstdc++.tag.tmp doc/libstdc++.tag || \
69 102
	rm doc/libstdc++.tag.tmp
70 103

	
71 104
install-html-local: doc/html
72 105
	@$(NORMAL_INSTALL)
73
	$(mkinstalldirs) $(DESTDIR)$(htmldir)/docs
106
	$(mkinstalldirs) $(DESTDIR)$(htmldir)/html
74 107
	for p in doc/html/*.{html,css,png,map,gif,tag} ; do \
75 108
	  f="`echo $$p | sed -e 's|^.*/||'`"; \
76
	  echo " $(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/docs/$$f"; \
77
	  $(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/docs/$$f; \
109
	  echo " $(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/html/$$f"; \
110
	  $(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/html/$$f; \
78 111
	done
79 112

	
80 113
uninstall-local:
81 114
	@$(NORMAL_UNINSTALL)
82 115
	for p in doc/html/*.{html,css,png,map,gif,tag} ; do \
83 116
	  f="`echo $$p | sed -e 's|^.*/||'`"; \
84
	  echo " rm -f $(DESTDIR)$(htmldir)/docs/$$f"; \
85
	  rm -f $(DESTDIR)$(htmldir)/docs/$$f; \
117
	  echo " rm -f $(DESTDIR)$(htmldir)/html/$$f"; \
118
	  rm -f $(DESTDIR)$(htmldir)/html/$$f; \
86 119
	done
87 120

	
88 121
.PHONY: update-external-tags
Ignore white space 6 line context
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-2009
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
@defgroup datas Data Structures
23
This group describes the several data structures implemented in LEMON.
23
This group contains the several data structures implemented in LEMON.
24 24
*/
25 25

	
26 26
/**
27 27
@defgroup graphs Graph Structures
28 28
@ingroup datas
29 29
\brief Graph structures implemented in LEMON.
30 30

	
31 31
The implementation of combinatorial algorithms heavily relies on
32 32
efficient graph implementations. LEMON offers data structures which are
33 33
planned to be easily used in an experimental phase of implementation studies,
34 34
and thereafter the program code can be made efficient by small modifications.
35 35

	
36 36
The most efficient implementation of diverse applications require the
37 37
usage of different physical graph implementations. These differences
38 38
appear in the size of graph we require to handle, memory or time usage
39 39
limitations or in the set of operations through which the graph can be
40 40
accessed.  LEMON provides several physical graph structures to meet
41 41
the diverging requirements of the possible users.  In order to save on
42 42
running time or on memory usage, some structures may fail to provide
43 43
some graph features like arc/edge or node deletion.
44 44

	
45 45
Alteration of standard containers need a very limited number of
46 46
operations, these together satisfy the everyday requirements.
47 47
In the case of graph structures, different operations are needed which do
48 48
not alter the physical graph, but gives another view. If some nodes or
49 49
arcs have to be hidden or the reverse oriented graph have to be used, then
50 50
this is the case. It also may happen that in a flow implementation
51 51
the residual graph can be accessed by another algorithm, or a node-set
52 52
is to be shrunk for another algorithm.
53 53
LEMON also provides a variety of graphs for these requirements called
54 54
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only
55 55
in conjunction with other graph representations.
56 56

	
57 57
You are free to use the graph structure that fit your requirements
58 58
the best, most graph algorithms and auxiliary data structures can be used
59 59
with any graph structure.
60 60

	
61 61
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts".
62 62
*/
63 63

	
64 64
/**
65 65
@defgroup graph_adaptors Adaptor Classes for Graphs
66 66
@ingroup graphs
67 67
\brief Adaptor classes for digraphs and graphs
68 68

	
69 69
This group contains several useful adaptor classes for digraphs and graphs.
70 70

	
71 71
The main parts of LEMON are the different graph structures, generic
72 72
graph algorithms, graph concepts, which couple them, and graph
73 73
adaptors. While the previous notions are more or less clear, the
74 74
latter one needs further explanation. Graph adaptors are graph classes
75 75
which serve for considering graph structures in different ways.
76 76

	
77 77
A short example makes this much clearer.  Suppose that we have an
78 78
instance \c g of a directed graph type, say ListDigraph and an algorithm
79 79
\code
80 80
template <typename Digraph>
81 81
int algorithm(const Digraph&);
82 82
\endcode
83 83
is needed to run on the reverse oriented graph.  It may be expensive
84 84
(in time or in memory usage) to copy \c g with the reversed
85 85
arcs.  In this case, an adaptor class is used, which (according
86 86
to LEMON \ref concepts::Digraph "digraph concepts") works as a digraph.
87 87
The adaptor uses the original digraph structure and digraph operations when
88 88
methods of the reversed oriented graph are called.  This means that the adaptor
89 89
have minor memory usage, and do not perform sophisticated algorithmic
90 90
actions.  The purpose of it is to give a tool for the cases when a
91 91
graph have to be used in a specific alteration.  If this alteration is
92 92
obtained by a usual construction like filtering the node or the arc set or
93 93
considering a new orientation, then an adaptor is worthwhile to use.
94 94
To come back to the reverse oriented graph, in this situation
95 95
\code
96 96
template<typename Digraph> class ReverseDigraph;
97 97
\endcode
98 98
template class can be used. The code looks as follows
99 99
\code
100 100
ListDigraph g;
101 101
ReverseDigraph<ListDigraph> rg(g);
102 102
int result = algorithm(rg);
103 103
\endcode
104 104
During running the algorithm, the original digraph \c g is untouched.
105 105
This techniques give rise to an elegant code, and based on stable
106 106
graph adaptors, complex algorithms can be implemented easily.
107 107

	
108 108
In flow, circulation and matching problems, the residual
109 109
graph is of particular importance. Combining an adaptor implementing
110 110
this with shortest path algorithms or minimum mean cycle algorithms,
111 111
a range of weighted and cardinality optimization algorithms can be
112 112
obtained. For other examples, the interested user is referred to the
113 113
detailed documentation of particular adaptors.
114 114

	
115 115
The behavior of graph adaptors can be very different. Some of them keep
116 116
capabilities of the original graph while in other cases this would be
117 117
meaningless. This means that the concepts that they meet depend
118 118
on the graph adaptor, and the wrapped graph.
119 119
For example, if an arc of a reversed digraph is deleted, this is carried
120 120
out by deleting the corresponding arc of the original digraph, thus the
121 121
adaptor modifies the original digraph.
122 122
However in case of a residual digraph, this operation has no sense.
123 123

	
124 124
Let us stand one more example here to simplify your work.
125 125
ReverseDigraph has constructor
126 126
\code
127 127
ReverseDigraph(Digraph& digraph);
128 128
\endcode
129 129
This means that in a situation, when a <tt>const %ListDigraph&</tt>
130 130
reference to a graph is given, then it have to be instantiated with
131 131
<tt>Digraph=const %ListDigraph</tt>.
132 132
\code
133 133
int algorithm1(const ListDigraph& g) {
134 134
  ReverseDigraph<const ListDigraph> rg(g);
135 135
  return algorithm2(rg);
136 136
}
137 137
\endcode
138 138
*/
139 139

	
140 140
/**
141
@defgroup semi_adaptors Semi-Adaptor Classes for Graphs
142
@ingroup graphs
143
\brief Graph types between real graphs and graph adaptors.
144

	
145
This group describes some graph types between real graphs and graph adaptors.
146
These classes wrap graphs to give new functionality as the adaptors do it.
147
On the other hand they are not light-weight structures as the adaptors.
148
*/
149

	
150
/**
151 141
@defgroup maps Maps
152 142
@ingroup datas
153 143
\brief Map structures implemented in LEMON.
154 144

	
155
This group describes the map structures implemented in LEMON.
145
This group contains the map structures implemented in LEMON.
156 146

	
157 147
LEMON provides several special purpose maps and map adaptors that e.g. combine
158 148
new maps from existing ones.
159 149

	
160 150
<b>See also:</b> \ref map_concepts "Map Concepts".
161 151
*/
162 152

	
163 153
/**
164 154
@defgroup graph_maps Graph Maps
165 155
@ingroup maps
166 156
\brief Special graph-related maps.
167 157

	
168
This group describes maps that are specifically designed to assign
158
This group contains maps that are specifically designed to assign
169 159
values to the nodes and arcs/edges of graphs.
170 160

	
171 161
If you are looking for the standard graph maps (\c NodeMap, \c ArcMap,
172 162
\c EdgeMap), see the \ref graph_concepts "Graph Structure Concepts".
173 163
*/
174 164

	
175 165
/**
176 166
\defgroup map_adaptors Map Adaptors
177 167
\ingroup maps
178 168
\brief Tools to create new maps from existing ones
179 169

	
180
This group describes map adaptors that are used to create "implicit"
170
This group contains map adaptors that are used to create "implicit"
181 171
maps from other maps.
182 172

	
183 173
Most of them are \ref concepts::ReadMap "read-only maps".
184 174
They can make arithmetic and logical operations between one or two maps
185 175
(negation, shifting, addition, multiplication, logical 'and', 'or',
186 176
'not' etc.) or e.g. convert a map to another one of different Value type.
187 177

	
188 178
The typical usage of this classes is passing implicit maps to
189 179
algorithms.  If a function type algorithm is called then the function
190 180
type map adaptors can be used comfortable. For example let's see the
191 181
usage of map adaptors with the \c graphToEps() function.
192 182
\code
193 183
  Color nodeColor(int deg) {
194 184
    if (deg >= 2) {
195 185
      return Color(0.5, 0.0, 0.5);
196 186
    } else if (deg == 1) {
197 187
      return Color(1.0, 0.5, 1.0);
198 188
    } else {
199 189
      return Color(0.0, 0.0, 0.0);
200 190
    }
201 191
  }
202 192

	
203 193
  Digraph::NodeMap<int> degree_map(graph);
204 194

	
205 195
  graphToEps(graph, "graph.eps")
206 196
    .coords(coords).scaleToA4().undirected()
207 197
    .nodeColors(composeMap(functorToMap(nodeColor), degree_map))
208 198
    .run();
209 199
\endcode
210 200
The \c functorToMap() function makes an \c int to \c Color map from the
211 201
\c nodeColor() function. The \c composeMap() compose the \c degree_map
212 202
and the previously created map. The composed map is a proper function to
213 203
get the color of each node.
214 204

	
215 205
The usage with class type algorithms is little bit harder. In this
216 206
case the function type map adaptors can not be used, because the
217 207
function map adaptors give back temporary objects.
218 208
\code
219 209
  Digraph graph;
220 210

	
221 211
  typedef Digraph::ArcMap<double> DoubleArcMap;
222 212
  DoubleArcMap length(graph);
223 213
  DoubleArcMap speed(graph);
224 214

	
225 215
  typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap;
226 216
  TimeMap time(length, speed);
227 217

	
228 218
  Dijkstra<Digraph, TimeMap> dijkstra(graph, time);
229 219
  dijkstra.run(source, target);
230 220
\endcode
231 221
We have a length map and a maximum speed map on the arcs of a digraph.
232 222
The minimum time to pass the arc can be calculated as the division of
233 223
the two maps which can be done implicitly with the \c DivMap template
234 224
class. We use the implicit minimum time map as the length map of the
235 225
\c Dijkstra algorithm.
236 226
*/
237 227

	
238 228
/**
239
@defgroup matrices Matrices
240
@ingroup datas
241
\brief Two dimensional data storages implemented in LEMON.
242

	
243
This group describes two dimensional data storages implemented in LEMON.
244
*/
245

	
246
/**
247 229
@defgroup paths Path Structures
248 230
@ingroup datas
249 231
\brief %Path structures implemented in LEMON.
250 232

	
251
This group describes the path structures implemented in LEMON.
233
This group contains the path structures implemented in LEMON.
252 234

	
253 235
LEMON provides flexible data structures to work with paths.
254 236
All of them have similar interfaces and they can be copied easily with
255 237
assignment operators and copy constructors. This makes it easy and
256 238
efficient to have e.g. the Dijkstra algorithm to store its result in
257 239
any kind of path structure.
258 240

	
259
\sa lemon::concepts::Path
241
\sa \ref concepts::Path "Path concept"
242
*/
243

	
244
/**
245
@defgroup heaps Heap Structures
246
@ingroup datas
247
\brief %Heap structures implemented in LEMON.
248

	
249
This group contains the heap structures implemented in LEMON.
250

	
251
LEMON provides several heap classes. They are efficient implementations
252
of the abstract data type \e priority \e queue. They store items with
253
specified values called \e priorities in such a way that finding and
254
removing the item with minimum priority are efficient.
255
The basic operations are adding and erasing items, changing the priority
256
of an item, etc.
257

	
258
Heaps are crucial in several algorithms, such as Dijkstra and Prim.
259
The heap implementations have the same interface, thus any of them can be
260
used easily in such algorithms.
261

	
262
\sa \ref concepts::Heap "Heap concept"
263
*/
264

	
265
/**
266
@defgroup matrices Matrices
267
@ingroup datas
268
\brief Two dimensional data storages implemented in LEMON.
269

	
270
This group contains two dimensional data storages implemented in LEMON.
260 271
*/
261 272

	
262 273
/**
263 274
@defgroup auxdat Auxiliary Data Structures
264 275
@ingroup datas
265 276
\brief Auxiliary data structures implemented in LEMON.
266 277

	
267
This group describes some data structures implemented in LEMON in
278
This group contains some data structures implemented in LEMON in
268 279
order to make it easier to implement combinatorial algorithms.
269 280
*/
270 281

	
271 282
/**
283
@defgroup geomdat Geometric Data Structures
284
@ingroup auxdat
285
\brief Geometric data structures implemented in LEMON.
286

	
287
This group contains geometric data structures implemented in LEMON.
288

	
289
 - \ref lemon::dim2::Point "dim2::Point" implements a two dimensional
290
   vector with the usual operations.
291
 - \ref lemon::dim2::Box "dim2::Box" can be used to determine the
292
   rectangular bounding box of a set of \ref lemon::dim2::Point
293
   "dim2::Point"'s.
294
*/
295

	
296
/**
297
@defgroup matrices Matrices
298
@ingroup auxdat
299
\brief Two dimensional data storages implemented in LEMON.
300

	
301
This group contains two dimensional data storages implemented in LEMON.
302
*/
303

	
304
/**
272 305
@defgroup algs Algorithms
273
\brief This group describes the several algorithms
306
\brief This group contains the several algorithms
274 307
implemented in LEMON.
275 308

	
276
This group describes the several algorithms
309
This group contains the several algorithms
277 310
implemented in LEMON.
278 311
*/
279 312

	
280 313
/**
281 314
@defgroup search Graph Search
282 315
@ingroup algs
283 316
\brief Common graph search algorithms.
284 317

	
285
This group describes the common graph search algorithms, namely
286
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS).
318
This group contains the common graph search algorithms, namely
319
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS)
320
\ref clrs01algorithms.
287 321
*/
288 322

	
289 323
/**
290 324
@defgroup shortest_path Shortest Path Algorithms
291 325
@ingroup algs
292 326
\brief Algorithms for finding shortest paths.
293 327

	
294
This group describes the algorithms for finding shortest paths in digraphs.
328
This group contains the algorithms for finding shortest paths in digraphs
329
\ref clrs01algorithms.
295 330

	
296 331
 - \ref Dijkstra algorithm for finding shortest paths from a source node
297 332
   when all arc lengths are non-negative.
298 333
 - \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths
299 334
   from a source node when arc lenghts can be either positive or negative,
300 335
   but the digraph should not contain directed cycles with negative total
301 336
   length.
302 337
 - \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms
303 338
   for solving the \e all-pairs \e shortest \e paths \e problem when arc
304 339
   lenghts can be either positive or negative, but the digraph should
305 340
   not contain directed cycles with negative total length.
306 341
 - \ref Suurballe A successive shortest path algorithm for finding
307 342
   arc-disjoint paths between two nodes having minimum total length.
308 343
*/
309 344

	
310 345
/**
346
@defgroup spantree Minimum Spanning Tree Algorithms
347
@ingroup algs
348
\brief Algorithms for finding minimum cost spanning trees and arborescences.
349

	
350
This group contains the algorithms for finding minimum cost spanning
351
trees and arborescences \ref clrs01algorithms.
352
*/
353

	
354
/**
311 355
@defgroup max_flow Maximum Flow Algorithms
312 356
@ingroup algs
313 357
\brief Algorithms for finding maximum flows.
314 358

	
315
This group describes the algorithms for finding maximum flows and
316
feasible circulations.
359
This group contains the algorithms for finding maximum flows and
360
feasible circulations \ref clrs01algorithms, \ref amo93networkflows.
317 361

	
318 362
The \e maximum \e flow \e problem is to find a flow of maximum value between
319 363
a single source and a single target. Formally, there is a \f$G=(V,A)\f$
320
digraph, a \f$cap:A\rightarrow\mathbf{R}^+_0\f$ capacity function and
364
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and
321 365
\f$s, t \in V\f$ source and target nodes.
322
A maximum flow is an \f$f:A\rightarrow\mathbf{R}^+_0\f$ solution of the
366
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the
323 367
following optimization problem.
324 368

	
325
\f[ \max\sum_{a\in\delta_{out}(s)}f(a) - \sum_{a\in\delta_{in}(s)}f(a) \f]
326
\f[ \sum_{a\in\delta_{out}(v)} f(a) = \sum_{a\in\delta_{in}(v)} f(a)
327
    \qquad \forall v\in V\setminus\{s,t\} \f]
328
\f[ 0 \leq f(a) \leq cap(a) \qquad \forall a\in A \f]
369
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f]
370
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu)
371
    \quad \forall u\in V\setminus\{s,t\} \f]
372
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f]
329 373

	
330 374
LEMON contains several algorithms for solving maximum flow problems:
331
- \ref EdmondsKarp Edmonds-Karp algorithm.
332
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm.
333
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees.
334
- \ref GoldbergTarjan Preflow push-relabel algorithm with dynamic trees.
375
- \ref EdmondsKarp Edmonds-Karp algorithm
376
  \ref edmondskarp72theoretical.
377
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm
378
  \ref goldberg88newapproach.
379
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees
380
  \ref dinic70algorithm, \ref sleator83dynamic.
381
- \ref GoldbergTarjan !Preflow push-relabel algorithm with dynamic trees
382
  \ref goldberg88newapproach, \ref sleator83dynamic.
335 383

	
336
In most cases the \ref Preflow "Preflow" algorithm provides the
384
In most cases the \ref Preflow algorithm provides the
337 385
fastest method for computing a maximum flow. All implementations
338
provides functions to also query the minimum cut, which is the dual
339
problem of the maximum flow.
386
also provide functions to query the minimum cut, which is the dual
387
problem of maximum flow.
388

	
389
\ref Circulation is a preflow push-relabel algorithm implemented directly 
390
for finding feasible circulations, which is a somewhat different problem,
391
but it is strongly related to maximum flow.
392
For more information, see \ref Circulation.
340 393
*/
341 394

	
342 395
/**
343
@defgroup min_cost_flow Minimum Cost Flow Algorithms
396
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms
344 397
@ingroup algs
345 398

	
346 399
\brief Algorithms for finding minimum cost flows and circulations.
347 400

	
348
This group describes the algorithms for finding minimum cost flows and
349
circulations.
401
This group contains the algorithms for finding minimum cost flows and
402
circulations \ref amo93networkflows. For more information about this
403
problem and its dual solution, see \ref min_cost_flow
404
"Minimum Cost Flow Problem".
350 405

	
351
The \e minimum \e cost \e flow \e problem is to find a feasible flow of
352
minimum total cost from a set of supply nodes to a set of demand nodes
353
in a network with capacity constraints and arc costs.
354
Formally, let \f$G=(V,A)\f$ be a digraph,
355
\f$lower, upper: A\rightarrow\mathbf{Z}^+_0\f$ denote the lower and
356
upper bounds for the flow values on the arcs,
357
\f$cost: A\rightarrow\mathbf{Z}^+_0\f$ denotes the cost per unit flow
358
on the arcs, and
359
\f$supply: V\rightarrow\mathbf{Z}\f$ denotes the supply/demand values
360
of the nodes.
361
A minimum cost flow is an \f$f:A\rightarrow\mathbf{R}^+_0\f$ solution of
362
the following optimization problem.
406
LEMON contains several algorithms for this problem.
407
 - \ref NetworkSimplex Primal Network Simplex algorithm with various
408
   pivot strategies \ref dantzig63linearprog, \ref kellyoneill91netsimplex.
409
 - \ref CostScaling Push-Relabel and Augment-Relabel algorithms based on
410
   cost scaling \ref goldberg90approximation, \ref goldberg97efficient,
411
   \ref bunnagel98efficient.
412
 - \ref CapacityScaling Successive Shortest %Path algorithm with optional
413
   capacity scaling \ref edmondskarp72theoretical.
414
 - \ref CancelAndTighten The Cancel and Tighten algorithm
415
   \ref goldberg89cyclecanceling.
416
 - \ref CycleCanceling Cycle-Canceling algorithms
417
   \ref klein67primal, \ref goldberg89cyclecanceling.
363 418

	
364
\f[ \min\sum_{a\in A} f(a) cost(a) \f]
365
\f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a) =
366
    supply(v) \qquad \forall v\in V \f]
367
\f[ lower(a) \leq f(a) \leq upper(a) \qquad \forall a\in A \f]
368

	
369
LEMON contains several algorithms for solving minimum cost flow problems:
370
 - \ref CycleCanceling Cycle-canceling algorithms.
371
 - \ref CapacityScaling Successive shortest path algorithm with optional
372
   capacity scaling.
373
 - \ref CostScaling Push-relabel and augment-relabel algorithms based on
374
   cost scaling.
375
 - \ref NetworkSimplex Primal network simplex algorithm with various
376
   pivot strategies.
419
In general NetworkSimplex is the most efficient implementation,
420
but in special cases other algorithms could be faster.
421
For example, if the total supply and/or capacities are rather small,
422
CapacityScaling is usually the fastest algorithm (without effective scaling).
377 423
*/
378 424

	
379 425
/**
380 426
@defgroup min_cut Minimum Cut Algorithms
381 427
@ingroup algs
382 428

	
383 429
\brief Algorithms for finding minimum cut in graphs.
384 430

	
385
This group describes the algorithms for finding minimum cut in graphs.
431
This group contains the algorithms for finding minimum cut in graphs.
386 432

	
387 433
The \e minimum \e cut \e problem is to find a non-empty and non-complete
388 434
\f$X\f$ subset of the nodes with minimum overall capacity on
389 435
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a
390 436
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum
391 437
cut is the \f$X\f$ solution of the next optimization problem:
392 438

	
393 439
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
394
    \sum_{uv\in A, u\in X, v\not\in X}cap(uv) \f]
440
    \sum_{uv\in A: u\in X, v\not\in X}cap(uv) \f]
395 441

	
396 442
LEMON contains several algorithms related to minimum cut problems:
397 443

	
398 444
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut
399 445
  in directed graphs.
400 446
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for
401 447
  calculating minimum cut in undirected graphs.
402
- \ref GomoryHuTree "Gomory-Hu tree computation" for calculating
448
- \ref GomoryHu "Gomory-Hu tree computation" for calculating
403 449
  all-pairs minimum cut in undirected graphs.
404 450

	
405 451
If you want to find minimum cut just between two distinict nodes,
406 452
see the \ref max_flow "maximum flow problem".
407 453
*/
408 454

	
409 455
/**
410
@defgroup graph_prop Connectivity and Other Graph Properties
456
@defgroup min_mean_cycle Minimum Mean Cycle Algorithms
411 457
@ingroup algs
412
\brief Algorithms for discovering the graph properties
458
\brief Algorithms for finding minimum mean cycles.
413 459

	
414
This group describes the algorithms for discovering the graph properties
415
like connectivity, bipartiteness, euler property, simplicity etc.
460
This group contains the algorithms for finding minimum mean cycles
461
\ref clrs01algorithms, \ref amo93networkflows.
416 462

	
417
\image html edge_biconnected_components.png
418
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth
419
*/
463
The \e minimum \e mean \e cycle \e problem is to find a directed cycle
464
of minimum mean length (cost) in a digraph.
465
The mean length of a cycle is the average length of its arcs, i.e. the
466
ratio between the total length of the cycle and the number of arcs on it.
420 467

	
421
/**
422
@defgroup planar Planarity Embedding and Drawing
423
@ingroup algs
424
\brief Algorithms for planarity checking, embedding and drawing
468
This problem has an important connection to \e conservative \e length
469
\e functions, too. A length function on the arcs of a digraph is called
470
conservative if and only if there is no directed cycle of negative total
471
length. For an arbitrary length function, the negative of the minimum
472
cycle mean is the smallest \f$\epsilon\f$ value so that increasing the
473
arc lengths uniformly by \f$\epsilon\f$ results in a conservative length
474
function.
425 475

	
426
This group describes the algorithms for planarity checking,
427
embedding and drawing.
476
LEMON contains three algorithms for solving the minimum mean cycle problem:
477
- \ref Karp "Karp"'s original algorithm \ref amo93networkflows,
478
  \ref dasdan98minmeancycle.
479
- \ref HartmannOrlin "Hartmann-Orlin"'s algorithm, which is an improved
480
  version of Karp's algorithm \ref dasdan98minmeancycle.
481
- \ref Howard "Howard"'s policy iteration algorithm
482
  \ref dasdan98minmeancycle.
428 483

	
429
\image html planar.png
430
\image latex planar.eps "Plane graph" width=\textwidth
484
In practice, the Howard algorithm proved to be by far the most efficient
485
one, though the best known theoretical bound on its running time is
486
exponential.
487
Both Karp and HartmannOrlin algorithms run in time O(ne) and use space
488
O(n<sup>2</sup>+e), but the latter one is typically faster due to the
489
applied early termination scheme.
431 490
*/
432 491

	
433 492
/**
434 493
@defgroup matching Matching Algorithms
435 494
@ingroup algs
436 495
\brief Algorithms for finding matchings in graphs and bipartite graphs.
437 496

	
438
This group contains algorithm objects and functions to calculate
497
This group contains the algorithms for calculating
439 498
matchings in graphs and bipartite graphs. The general matching problem is
440
finding a subset of the arcs which does not shares common endpoints.
499
finding a subset of the edges for which each node has at most one incident
500
edge.
441 501

	
442 502
There are several different algorithms for calculate matchings in
443 503
graphs.  The matching problems in bipartite graphs are generally
444 504
easier than in general graphs. The goal of the matching optimization
445 505
can be finding maximum cardinality, maximum weight or minimum cost
446 506
matching. The search can be constrained to find perfect or
447 507
maximum cardinality matching.
448 508

	
449 509
The matching algorithms implemented in LEMON:
450 510
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm
451 511
  for calculating maximum cardinality matching in bipartite graphs.
452 512
- \ref PrBipartiteMatching Push-relabel algorithm
453 513
  for calculating maximum cardinality matching in bipartite graphs.
454 514
- \ref MaxWeightedBipartiteMatching
455 515
  Successive shortest path algorithm for calculating maximum weighted
456 516
  matching and maximum weighted bipartite matching in bipartite graphs.
457 517
- \ref MinCostMaxBipartiteMatching
458 518
  Successive shortest path algorithm for calculating minimum cost maximum
459 519
  matching in bipartite graphs.
460 520
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating
461 521
  maximum cardinality matching in general graphs.
462 522
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating
463 523
  maximum weighted matching in general graphs.
464 524
- \ref MaxWeightedPerfectMatching
465 525
  Edmond's blossom shrinking algorithm for calculating maximum weighted
466 526
  perfect matching in general graphs.
467 527

	
468 528
\image html bipartite_matching.png
469 529
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth
470 530
*/
471 531

	
472 532
/**
473
@defgroup spantree Minimum Spanning Tree Algorithms
533
@defgroup graph_properties Connectivity and Other Graph Properties
474 534
@ingroup algs
475
\brief Algorithms for finding a minimum cost spanning tree in a graph.
535
\brief Algorithms for discovering the graph properties
476 536

	
477
This group describes the algorithms for finding a minimum cost spanning
478
tree in a graph.
537
This group contains the algorithms for discovering the graph properties
538
like connectivity, bipartiteness, euler property, simplicity etc.
539

	
540
\image html connected_components.png
541
\image latex connected_components.eps "Connected components" width=\textwidth
542
*/
543

	
544
/**
545
@defgroup planar Planarity Embedding and Drawing
546
@ingroup algs
547
\brief Algorithms for planarity checking, embedding and drawing
548

	
549
This group contains the algorithms for planarity checking,
550
embedding and drawing.
551

	
552
\image html planar.png
553
\image latex planar.eps "Plane graph" width=\textwidth
554
*/
555

	
556
/**
557
@defgroup approx Approximation Algorithms
558
@ingroup algs
559
\brief Approximation algorithms.
560

	
561
This group contains the approximation and heuristic algorithms
562
implemented in LEMON.
479 563
*/
480 564

	
481 565
/**
482 566
@defgroup auxalg Auxiliary Algorithms
483 567
@ingroup algs
484 568
\brief Auxiliary algorithms implemented in LEMON.
485 569

	
486
This group describes some algorithms implemented in LEMON
570
This group contains some algorithms implemented in LEMON
487 571
in order to make it easier to implement complex algorithms.
488 572
*/
489 573

	
490 574
/**
491
@defgroup approx Approximation Algorithms
492
@ingroup algs
493
\brief Approximation algorithms.
575
@defgroup gen_opt_group General Optimization Tools
576
\brief This group contains some general optimization frameworks
577
implemented in LEMON.
494 578

	
495
This group describes the approximation and heuristic algorithms
579
This group contains some general optimization frameworks
496 580
implemented in LEMON.
497 581
*/
498 582

	
499 583
/**
500
@defgroup gen_opt_group General Optimization Tools
501
\brief This group describes some general optimization frameworks
502
implemented in LEMON.
584
@defgroup lp_group LP and MIP Solvers
585
@ingroup gen_opt_group
586
\brief LP and MIP solver interfaces for LEMON.
503 587

	
504
This group describes some general optimization frameworks
505
implemented in LEMON.
506
*/
588
This group contains LP and MIP solver interfaces for LEMON.
589
Various LP solvers could be used in the same manner with this
590
high-level interface.
507 591

	
508
/**
509
@defgroup lp_group Lp and Mip Solvers
510
@ingroup gen_opt_group
511
\brief Lp and Mip solver interfaces for LEMON.
512

	
513
This group describes Lp and Mip solver interfaces for LEMON. The
514
various LP solvers could be used in the same manner with this
515
interface.
592
The currently supported solvers are \ref glpk, \ref clp, \ref cbc,
593
\ref cplex, \ref soplex.
516 594
*/
517 595

	
518 596
/**
519 597
@defgroup lp_utils Tools for Lp and Mip Solvers
520 598
@ingroup lp_group
521 599
\brief Helper tools to the Lp and Mip solvers.
522 600

	
523 601
This group adds some helper tools to general optimization framework
524 602
implemented in LEMON.
525 603
*/
526 604

	
527 605
/**
528 606
@defgroup metah Metaheuristics
529 607
@ingroup gen_opt_group
530 608
\brief Metaheuristics for LEMON library.
531 609

	
532
This group describes some metaheuristic optimization tools.
610
This group contains some metaheuristic optimization tools.
533 611
*/
534 612

	
535 613
/**
536 614
@defgroup utils Tools and Utilities
537 615
\brief Tools and utilities for programming in LEMON
538 616

	
539 617
Tools and utilities for programming in LEMON.
540 618
*/
541 619

	
542 620
/**
543 621
@defgroup gutils Basic Graph Utilities
544 622
@ingroup utils
545 623
\brief Simple basic graph utilities.
546 624

	
547
This group describes some simple basic graph utilities.
625
This group contains some simple basic graph utilities.
548 626
*/
549 627

	
550 628
/**
551 629
@defgroup misc Miscellaneous Tools
552 630
@ingroup utils
553 631
\brief Tools for development, debugging and testing.
554 632

	
555
This group describes several useful tools for development,
633
This group contains several useful tools for development,
556 634
debugging and testing.
557 635
*/
558 636

	
559 637
/**
560 638
@defgroup timecount Time Measuring and Counting
561 639
@ingroup misc
562 640
\brief Simple tools for measuring the performance of algorithms.
563 641

	
564
This group describes simple tools for measuring the performance
642
This group contains simple tools for measuring the performance
565 643
of algorithms.
566 644
*/
567 645

	
568 646
/**
569 647
@defgroup exceptions Exceptions
570 648
@ingroup utils
571 649
\brief Exceptions defined in LEMON.
572 650

	
573
This group describes the exceptions defined in LEMON.
651
This group contains the exceptions defined in LEMON.
574 652
*/
575 653

	
576 654
/**
577 655
@defgroup io_group Input-Output
578 656
\brief Graph Input-Output methods
579 657

	
580
This group describes the tools for importing and exporting graphs
658
This group contains the tools for importing and exporting graphs
581 659
and graph related data. Now it supports the \ref lgf-format
582 660
"LEMON Graph Format", the \c DIMACS format and the encapsulated
583 661
postscript (EPS) format.
584 662
*/
585 663

	
586 664
/**
587 665
@defgroup lemon_io LEMON Graph Format
588 666
@ingroup io_group
589 667
\brief Reading and writing LEMON Graph Format.
590 668

	
591
This group describes methods for reading and writing
669
This group contains methods for reading and writing
592 670
\ref lgf-format "LEMON Graph Format".
593 671
*/
594 672

	
595 673
/**
596 674
@defgroup eps_io Postscript Exporting
597 675
@ingroup io_group
598 676
\brief General \c EPS drawer and graph exporter
599 677

	
600
This group describes general \c EPS drawing methods and special
678
This group contains general \c EPS drawing methods and special
601 679
graph exporting tools.
602 680
*/
603 681

	
604 682
/**
605
@defgroup dimacs_group DIMACS format
683
@defgroup dimacs_group DIMACS Format
606 684
@ingroup io_group
607 685
\brief Read and write files in DIMACS format
608 686

	
609 687
Tools to read a digraph from or write it to a file in DIMACS format data.
610 688
*/
611 689

	
612 690
/**
613 691
@defgroup nauty_group NAUTY Format
614 692
@ingroup io_group
615 693
\brief Read \e Nauty format
616 694

	
617 695
Tool to read graphs from \e Nauty format data.
618 696
*/
619 697

	
620 698
/**
621 699
@defgroup concept Concepts
622 700
\brief Skeleton classes and concept checking classes
623 701

	
624
This group describes the data/algorithm skeletons and concept checking
702
This group contains the data/algorithm skeletons and concept checking
625 703
classes implemented in LEMON.
626 704

	
627 705
The purpose of the classes in this group is fourfold.
628 706

	
629 707
- These classes contain the documentations of the %concepts. In order
630 708
  to avoid document multiplications, an implementation of a concept
631 709
  simply refers to the corresponding concept class.
632 710

	
633 711
- These classes declare every functions, <tt>typedef</tt>s etc. an
634 712
  implementation of the %concepts should provide, however completely
635 713
  without implementations and real data structures behind the
636 714
  interface. On the other hand they should provide nothing else. All
637 715
  the algorithms working on a data structure meeting a certain concept
638 716
  should compile with these classes. (Though it will not run properly,
639 717
  of course.) In this way it is easily to check if an algorithm
640 718
  doesn't use any extra feature of a certain implementation.
641 719

	
642 720
- The concept descriptor classes also provide a <em>checker class</em>
643 721
  that makes it possible to check whether a certain implementation of a
644 722
  concept indeed provides all the required features.
645 723

	
646 724
- Finally, They can serve as a skeleton of a new implementation of a concept.
647 725
*/
648 726

	
649 727
/**
650 728
@defgroup graph_concepts Graph Structure Concepts
651 729
@ingroup concept
652 730
\brief Skeleton and concept checking classes for graph structures
653 731

	
654
This group describes the skeletons and concept checking classes of LEMON's
655
graph structures and helper classes used to implement these.
732
This group contains the skeletons and concept checking classes of
733
graph structures.
656 734
*/
657 735

	
658 736
/**
659 737
@defgroup map_concepts Map Concepts
660 738
@ingroup concept
661 739
\brief Skeleton and concept checking classes for maps
662 740

	
663
This group describes the skeletons and concept checking classes of maps.
664
*/
665

	
666
/**
667
\anchor demoprograms
668

	
669
@defgroup demos Demo Programs
670

	
671
Some demo programs are listed here. Their full source codes can be found in
672
the \c demo subdirectory of the source tree.
673

	
674
It order to compile them, use <tt>--enable-demo</tt> configure option when
675
build the library.
741
This group contains the skeletons and concept checking classes of maps.
676 742
*/
677 743

	
678 744
/**
679 745
@defgroup tools Standalone Utility Applications
680 746

	
681 747
Some utility applications are listed here.
682 748

	
683 749
The standard compilation procedure (<tt>./configure;make</tt>) will compile
684 750
them, as well.
685 751
*/
686 752

	
753
/**
754
\anchor demoprograms
755

	
756
@defgroup demos Demo Programs
757

	
758
Some demo programs are listed here. Their full source codes can be found in
759
the \c demo subdirectory of the source tree.
760

	
761
In order to compile them, use the <tt>make demo</tt> or the
762
<tt>make check</tt> commands.
763
*/
764

	
687 765
}
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
/**
20 20
\mainpage LEMON Documentation
21 21

	
22 22
\section intro Introduction
23 23

	
24
\subsection whatis What is LEMON
25

	
26
LEMON stands for
27
<b>L</b>ibrary of <b>E</b>fficient <b>M</b>odels
28
and <b>O</b>ptimization in <b>N</b>etworks.
29
It is a C++ template
30
library aimed at combinatorial optimization tasks which
31
often involve in working
32
with graphs.
24
<b>LEMON</b> stands for <i><b>L</b>ibrary for <b>E</b>fficient <b>M</b>odeling
25
and <b>O</b>ptimization in <b>N</b>etworks</i>.
26
It is a C++ template library providing efficient implementation of common
27
data structures and algorithms with focus on combinatorial optimization
28
problems in graphs and networks.
33 29

	
34 30
<b>
35 31
LEMON is an <a class="el" href="http://opensource.org/">open&nbsp;source</a>
36 32
project.
37 33
You are free to use it in your commercial or
38 34
non-commercial applications under very permissive
39 35
\ref license "license terms".
40 36
</b>
41 37

	
42
\subsection howtoread How to read the documentation
38
The project is maintained by the 
39
<a href="http://www.cs.elte.hu/egres/">Egerv&aacute;ry Research Group on
40
Combinatorial Optimization</a> \ref egres
41
at the Operations Research Department of the
42
<a href="http://www.elte.hu/">E&ouml;tv&ouml;s Lor&aacute;nd University,
43
Budapest</a>, Hungary.
44
LEMON is also a member of the <a href="http://www.coin-or.org/">COIN-OR</a>
45
initiative \ref coinor.
43 46

	
44
If you want to get a quick start and see the most important features then
45
take a look at our \ref quicktour
46
"Quick Tour to LEMON" which will guide you along.
47
\section howtoread How to Read the Documentation
47 48

	
48
If you already feel like using our library, see the page that tells you
49
\ref getstart "How to start using LEMON".
49
If you would like to get to know the library, see
50
<a class="el" href="http://lemon.cs.elte.hu/pub/tutorial/">LEMON Tutorial</a>.
50 51

	
51
If you
52
want to see how LEMON works, see
53
some \ref demoprograms "demo programs".
54

	
55
If you know what you are looking for then try to find it under the
56
<a class="el" href="modules.html">Modules</a>
57
section.
52
If you know what you are looking for, then try to find it under the
53
<a class="el" href="modules.html">Modules</a> section.
58 54

	
59 55
If you are a user of the old (0.x) series of LEMON, please check out the
60 56
\ref migration "Migration Guide" for the backward incompatibilities.
61 57
*/
Ignore white space 6 line context
1
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
1
INCLUDE_DIRECTORIES(
2
  ${PROJECT_SOURCE_DIR}
3
  ${PROJECT_BINARY_DIR}
4
)
2 5

	
3
ADD_LIBRARY(lemon
6
CONFIGURE_FILE(
7
  ${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake
8
  ${CMAKE_CURRENT_BINARY_DIR}/config.h
9
)
10

	
11
SET(LEMON_SOURCES
4 12
  arg_parser.cc
5 13
  base.cc
6 14
  color.cc
7
  random.cc)
15
  lp_base.cc
16
  lp_skeleton.cc
17
  random.cc
18
  bits/windows.cc
19
)
20

	
21
IF(LEMON_HAVE_GLPK)
22
  SET(LEMON_SOURCES ${LEMON_SOURCES} glpk.cc)
23
  INCLUDE_DIRECTORIES(${GLPK_INCLUDE_DIRS})
24
  IF(WIN32)
25
    INSTALL(FILES ${GLPK_BIN_DIR}/glpk.dll DESTINATION bin)
26
    INSTALL(FILES ${GLPK_BIN_DIR}/libltdl3.dll DESTINATION bin)
27
    INSTALL(FILES ${GLPK_BIN_DIR}/zlib1.dll DESTINATION bin)
28
  ENDIF()
29
ENDIF()
30

	
31
IF(LEMON_HAVE_CPLEX)
32
  SET(LEMON_SOURCES ${LEMON_SOURCES} cplex.cc)
33
  INCLUDE_DIRECTORIES(${CPLEX_INCLUDE_DIRS})
34
ENDIF()
35

	
36
IF(LEMON_HAVE_CLP)
37
  SET(LEMON_SOURCES ${LEMON_SOURCES} clp.cc)
38
  INCLUDE_DIRECTORIES(${COIN_INCLUDE_DIRS})
39
ENDIF()
40

	
41
IF(LEMON_HAVE_CBC)
42
  SET(LEMON_SOURCES ${LEMON_SOURCES} cbc.cc)
43
  INCLUDE_DIRECTORIES(${COIN_INCLUDE_DIRS})
44
ENDIF()
45

	
46
ADD_LIBRARY(lemon ${LEMON_SOURCES})
47
IF(UNIX)
48
  SET_TARGET_PROPERTIES(lemon PROPERTIES OUTPUT_NAME emon)
49
ENDIF()
8 50

	
9 51
INSTALL(
10 52
  TARGETS lemon
11 53
  ARCHIVE DESTINATION lib
12
  COMPONENT library)
54
  COMPONENT library
55
)
13 56

	
14 57
INSTALL(
15 58
  DIRECTORY . bits concepts
16 59
  DESTINATION include/lemon
17 60
  COMPONENT headers
18
  FILES_MATCHING PATTERN "*.h")
61
  FILES_MATCHING PATTERN "*.h"
62
)
63

	
64
INSTALL(
65
  FILES ${CMAKE_CURRENT_BINARY_DIR}/config.h
66
  DESTINATION include/lemon
67
  COMPONENT headers
68
)
Ignore white space 6 line context
1 1
EXTRA_DIST += \
2 2
	lemon/lemon.pc.in \
3
	lemon/CMakeLists.txt
3
	lemon/CMakeLists.txt \
4
	lemon/config.h.cmake
4 5

	
5 6
pkgconfig_DATA += lemon/lemon.pc
6 7

	
7 8
lib_LTLIBRARIES += lemon/libemon.la
8 9

	
9 10
lemon_libemon_la_SOURCES = \
10 11
	lemon/arg_parser.cc \
11 12
	lemon/base.cc \
12 13
	lemon/color.cc \
13 14
	lemon/lp_base.cc \
14 15
	lemon/lp_skeleton.cc \
15
	lemon/random.cc
16
	lemon/random.cc \
17
	lemon/bits/windows.cc
16 18

	
19
nodist_lemon_HEADERS = lemon/config.h	
17 20

	
18 21
lemon_libemon_la_CXXFLAGS = \
22
	$(AM_CXXFLAGS) \
19 23
	$(GLPK_CFLAGS) \
20 24
	$(CPLEX_CFLAGS) \
21 25
	$(SOPLEX_CXXFLAGS) \
22
	$(CLP_CXXFLAGS)
26
	$(CLP_CXXFLAGS) \
27
	$(CBC_CXXFLAGS)
23 28

	
24 29
lemon_libemon_la_LDFLAGS = \
25 30
	$(GLPK_LIBS) \
26 31
	$(CPLEX_LIBS) \
27 32
	$(SOPLEX_LIBS) \
28
	$(CLP_LIBS)
33
	$(CLP_LIBS) \
34
	$(CBC_LIBS)
29 35

	
30 36
if HAVE_GLPK
31 37
lemon_libemon_la_SOURCES += lemon/glpk.cc
32 38
endif
33 39

	
34 40
if HAVE_CPLEX
35 41
lemon_libemon_la_SOURCES += lemon/cplex.cc
36 42
endif
37 43

	
38 44
if HAVE_SOPLEX
39 45
lemon_libemon_la_SOURCES += lemon/soplex.cc
40 46
endif
41 47

	
42 48
if HAVE_CLP
43 49
lemon_libemon_la_SOURCES += lemon/clp.cc
44 50
endif
45 51

	
52
if HAVE_CBC
53
lemon_libemon_la_SOURCES += lemon/cbc.cc
54
endif
55

	
46 56
lemon_HEADERS += \
47 57
	lemon/adaptors.h \
48 58
	lemon/arg_parser.h \
49 59
	lemon/assert.h \
60
	lemon/bellman_ford.h \
50 61
	lemon/bfs.h \
51 62
	lemon/bin_heap.h \
63
	lemon/binom_heap.h \
64
	lemon/bucket_heap.h \
65
	lemon/cbc.h \
52 66
	lemon/circulation.h \
53 67
	lemon/clp.h \
54 68
	lemon/color.h \
55 69
	lemon/concept_check.h \
70
	lemon/connectivity.h \
56 71
	lemon/counter.h \
57 72
	lemon/core.h \
58 73
	lemon/cplex.h \
59 74
	lemon/dfs.h \
60 75
	lemon/dijkstra.h \
61 76
	lemon/dim2.h \
62 77
	lemon/dimacs.h \
63 78
	lemon/edge_set.h \
64 79
	lemon/elevator.h \
65 80
	lemon/error.h \
81
	lemon/euler.h \
82
	lemon/fib_heap.h \
83
	lemon/fourary_heap.h \
66 84
	lemon/full_graph.h \
67 85
	lemon/glpk.h \
86
	lemon/gomory_hu.h \
68 87
	lemon/graph_to_eps.h \
69 88
	lemon/grid_graph.h \
89
	lemon/hartmann_orlin.h \
90
	lemon/howard.h \
70 91
	lemon/hypercube_graph.h \
92
	lemon/karp.h \
93
	lemon/kary_heap.h \
71 94
	lemon/kruskal.h \
72 95
	lemon/hao_orlin.h \
73 96
	lemon/lgf_reader.h \
74 97
	lemon/lgf_writer.h \
75 98
	lemon/list_graph.h \
76 99
	lemon/lp.h \
77 100
	lemon/lp_base.h \
78 101
	lemon/lp_skeleton.h \
79
	lemon/list_graph.h \
80 102
	lemon/maps.h \
103
	lemon/matching.h \
81 104
	lemon/math.h \
82
	lemon/max_matching.h \
105
	lemon/min_cost_arborescence.h \
83 106
	lemon/nauty_reader.h \
107
	lemon/network_simplex.h \
108
	lemon/pairing_heap.h \
84 109
	lemon/path.h \
85 110
	lemon/preflow.h \
111
	lemon/radix_heap.h \
86 112
	lemon/radix_sort.h \
87 113
	lemon/random.h \
88 114
	lemon/smart_graph.h \
89 115
	lemon/soplex.h \
116
	lemon/static_graph.h \
90 117
	lemon/suurballe.h \
91 118
	lemon/time_measure.h \
92 119
	lemon/tolerance.h \
93
	lemon/unionfind.h
120
	lemon/unionfind.h \
121
	lemon/bits/windows.h
94 122

	
95 123
bits_HEADERS += \
96 124
	lemon/bits/alteration_notifier.h \
97 125
	lemon/bits/array_map.h \
98
	lemon/bits/base_extender.h \
99 126
	lemon/bits/bezier.h \
100 127
	lemon/bits/default_map.h \
101 128
	lemon/bits/edge_set_extender.h \
102 129
	lemon/bits/enable_if.h \
103 130
	lemon/bits/graph_adaptor_extender.h \
104 131
	lemon/bits/graph_extender.h \
105 132
	lemon/bits/map_extender.h \
106 133
	lemon/bits/path_dump.h \
107 134
	lemon/bits/solver_bits.h \
108 135
	lemon/bits/traits.h \
109 136
	lemon/bits/variant.h \
110 137
	lemon/bits/vector_map.h
111 138

	
112 139
concept_HEADERS += \
113 140
	lemon/concepts/digraph.h \
114 141
	lemon/concepts/graph.h \
115 142
	lemon/concepts/graph_components.h \
116 143
	lemon/concepts/heap.h \
117 144
	lemon/concepts/maps.h \
118 145
	lemon/concepts/path.h
Ignore white space 6 line context
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-2009
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_ADAPTORS_H
20 20
#define LEMON_ADAPTORS_H
21 21

	
22 22
/// \ingroup graph_adaptors
23 23
/// \file
24 24
/// \brief Adaptor classes for digraphs and graphs
25 25
///
26 26
/// This file contains several useful adaptors for digraphs and graphs.
27 27

	
28 28
#include <lemon/core.h>
29 29
#include <lemon/maps.h>
30 30
#include <lemon/bits/variant.h>
31 31

	
32 32
#include <lemon/bits/graph_adaptor_extender.h>
33
#include <lemon/bits/map_extender.h>
33 34
#include <lemon/tolerance.h>
34 35

	
35 36
#include <algorithm>
36 37

	
37 38
namespace lemon {
38 39

	
39
  template<typename _Digraph>
40
#ifdef _MSC_VER
41
#define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED
42
#else
43
#define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED
44
#endif
45

	
46
  template<typename DGR>
40 47
  class DigraphAdaptorBase {
41 48
  public:
42
    typedef _Digraph Digraph;
49
    typedef DGR Digraph;
43 50
    typedef DigraphAdaptorBase Adaptor;
44
    typedef Digraph ParentDigraph;
45 51

	
46 52
  protected:
47
    Digraph* _digraph;
53
    DGR* _digraph;
48 54
    DigraphAdaptorBase() : _digraph(0) { }
49
    void setDigraph(Digraph& digraph) { _digraph = &digraph; }
55
    void initialize(DGR& digraph) { _digraph = &digraph; }
50 56

	
51 57
  public:
52
    DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { }
53

	
54
    typedef typename Digraph::Node Node;
55
    typedef typename Digraph::Arc Arc;
58
    DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { }
59

	
60
    typedef typename DGR::Node Node;
61
    typedef typename DGR::Arc Arc;
56 62

	
57 63
    void first(Node& i) const { _digraph->first(i); }
58 64
    void first(Arc& i) const { _digraph->first(i); }
59 65
    void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); }
60 66
    void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); }
61 67

	
62 68
    void next(Node& i) const { _digraph->next(i); }
63 69
    void next(Arc& i) const { _digraph->next(i); }
64 70
    void nextIn(Arc& i) const { _digraph->nextIn(i); }
65 71
    void nextOut(Arc& i) const { _digraph->nextOut(i); }
66 72

	
67 73
    Node source(const Arc& a) const { return _digraph->source(a); }
68 74
    Node target(const Arc& a) const { return _digraph->target(a); }
69 75

	
70
    typedef NodeNumTagIndicator<Digraph> NodeNumTag;
76
    typedef NodeNumTagIndicator<DGR> NodeNumTag;
71 77
    int nodeNum() const { return _digraph->nodeNum(); }
72 78

	
73
    typedef ArcNumTagIndicator<Digraph> ArcNumTag;
79
    typedef ArcNumTagIndicator<DGR> ArcNumTag;
74 80
    int arcNum() const { return _digraph->arcNum(); }
75 81

	
76
    typedef FindArcTagIndicator<Digraph> FindArcTag;
82
    typedef FindArcTagIndicator<DGR> FindArcTag;
77 83
    Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const {
78 84
      return _digraph->findArc(u, v, prev);
79 85
    }
80 86

	
81 87
    Node addNode() { return _digraph->addNode(); }
82 88
    Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
83 89

	
84 90
    void erase(const Node& n) { _digraph->erase(n); }
85 91
    void erase(const Arc& a) { _digraph->erase(a); }
86 92

	
87 93
    void clear() { _digraph->clear(); }
88 94

	
89 95
    int id(const Node& n) const { return _digraph->id(n); }
90 96
    int id(const Arc& a) const { return _digraph->id(a); }
91 97

	
92 98
    Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
93 99
    Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); }
94 100

	
95 101
    int maxNodeId() const { return _digraph->maxNodeId(); }
96 102
    int maxArcId() const { return _digraph->maxArcId(); }
97 103

	
98
    typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier;
104
    typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier;
99 105
    NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
100 106

	
101
    typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier;
107
    typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier;
102 108
    ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
103 109

	
104
    template <typename _Value>
105
    class NodeMap : public Digraph::template NodeMap<_Value> {
110
    template <typename V>
111
    class NodeMap : public DGR::template NodeMap<V> {
112
      typedef typename DGR::template NodeMap<V> Parent;
113

	
106 114
    public:
107

	
108
      typedef typename Digraph::template NodeMap<_Value> Parent;
109

	
110 115
      explicit NodeMap(const Adaptor& adaptor)
111 116
        : Parent(*adaptor._digraph) {}
112

	
113
      NodeMap(const Adaptor& adaptor, const _Value& value)
117
      NodeMap(const Adaptor& adaptor, const V& value)
114 118
        : Parent(*adaptor._digraph, value) { }
115 119

	
116 120
    private:
117 121
      NodeMap& operator=(const NodeMap& cmap) {
118 122
        return operator=<NodeMap>(cmap);
119 123
      }
120 124

	
121 125
      template <typename CMap>
122 126
      NodeMap& operator=(const CMap& cmap) {
123 127
        Parent::operator=(cmap);
124 128
        return *this;
125 129
      }
126 130

	
127 131
    };
128 132

	
129
    template <typename _Value>
130
    class ArcMap : public Digraph::template ArcMap<_Value> {
133
    template <typename V>
134
    class ArcMap : public DGR::template ArcMap<V> {
135
      typedef typename DGR::template ArcMap<V> Parent;
136

	
131 137
    public:
132

	
133
      typedef typename Digraph::template ArcMap<_Value> Parent;
134

	
135
      explicit ArcMap(const Adaptor& adaptor)
138
      explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor)
136 139
        : Parent(*adaptor._digraph) {}
137

	
138
      ArcMap(const Adaptor& adaptor, const _Value& value)
140
      ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value)
139 141
        : Parent(*adaptor._digraph, value) {}
140 142

	
141 143
    private:
142 144
      ArcMap& operator=(const ArcMap& cmap) {
143 145
        return operator=<ArcMap>(cmap);
144 146
      }
145 147

	
146 148
      template <typename CMap>
147 149
      ArcMap& operator=(const CMap& cmap) {
148 150
        Parent::operator=(cmap);
149 151
        return *this;
150 152
      }
151 153

	
152 154
    };
153 155

	
154 156
  };
155 157

	
156
  template<typename _Graph>
158
  template<typename GR>
157 159
  class GraphAdaptorBase {
158 160
  public:
159
    typedef _Graph Graph;
160
    typedef Graph ParentGraph;
161
    typedef GR Graph;
161 162

	
162 163
  protected:
163
    Graph* _graph;
164
    GR* _graph;
164 165

	
165 166
    GraphAdaptorBase() : _graph(0) {}
166 167

	
167
    void setGraph(Graph& graph) { _graph = &graph; }
168
    void initialize(GR& graph) { _graph = &graph; }
168 169

	
169 170
  public:
170
    GraphAdaptorBase(Graph& graph) : _graph(&graph) {}
171

	
172
    typedef typename Graph::Node Node;
173
    typedef typename Graph::Arc Arc;
174
    typedef typename Graph::Edge Edge;
171
    GraphAdaptorBase(GR& graph) : _graph(&graph) {}
172

	
173
    typedef typename GR::Node Node;
174
    typedef typename GR::Arc Arc;
175
    typedef typename GR::Edge Edge;
175 176

	
176 177
    void first(Node& i) const { _graph->first(i); }
177 178
    void first(Arc& i) const { _graph->first(i); }
178 179
    void first(Edge& i) const { _graph->first(i); }
179 180
    void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
180 181
    void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
181 182
    void firstInc(Edge &i, bool &d, const Node &n) const {
182 183
      _graph->firstInc(i, d, n);
183 184
    }
184 185

	
185 186
    void next(Node& i) const { _graph->next(i); }
186 187
    void next(Arc& i) const { _graph->next(i); }
187 188
    void next(Edge& i) const { _graph->next(i); }
188 189
    void nextIn(Arc& i) const { _graph->nextIn(i); }
189 190
    void nextOut(Arc& i) const { _graph->nextOut(i); }
190 191
    void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
191 192

	
192 193
    Node u(const Edge& e) const { return _graph->u(e); }
193 194
    Node v(const Edge& e) const { return _graph->v(e); }
194 195

	
195 196
    Node source(const Arc& a) const { return _graph->source(a); }
196 197
    Node target(const Arc& a) const { return _graph->target(a); }
197 198

	
198 199
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
199 200
    int nodeNum() const { return _graph->nodeNum(); }
200 201

	
201 202
    typedef ArcNumTagIndicator<Graph> ArcNumTag;
202 203
    int arcNum() const { return _graph->arcNum(); }
203 204

	
204 205
    typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
205 206
    int edgeNum() const { return _graph->edgeNum(); }
206 207

	
207 208
    typedef FindArcTagIndicator<Graph> FindArcTag;
208 209
    Arc findArc(const Node& u, const Node& v,
209 210
                const Arc& prev = INVALID) const {
210 211
      return _graph->findArc(u, v, prev);
211 212
    }
212 213

	
213 214
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
214 215
    Edge findEdge(const Node& u, const Node& v,
215 216
                  const Edge& prev = INVALID) const {
216 217
      return _graph->findEdge(u, v, prev);
217 218
    }
218 219

	
219 220
    Node addNode() { return _graph->addNode(); }
220 221
    Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); }
221 222

	
222 223
    void erase(const Node& i) { _graph->erase(i); }
223 224
    void erase(const Edge& i) { _graph->erase(i); }
224 225

	
225 226
    void clear() { _graph->clear(); }
226 227

	
227 228
    bool direction(const Arc& a) const { return _graph->direction(a); }
228 229
    Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); }
229 230

	
230 231
    int id(const Node& v) const { return _graph->id(v); }
231 232
    int id(const Arc& a) const { return _graph->id(a); }
232 233
    int id(const Edge& e) const { return _graph->id(e); }
233 234

	
234 235
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
235 236
    Arc arcFromId(int ix) const { return _graph->arcFromId(ix); }
236 237
    Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); }
237 238

	
238 239
    int maxNodeId() const { return _graph->maxNodeId(); }
239 240
    int maxArcId() const { return _graph->maxArcId(); }
240 241
    int maxEdgeId() const { return _graph->maxEdgeId(); }
241 242

	
242
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
243
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
243 244
    NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
244 245

	
245
    typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier;
246
    typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier;
246 247
    ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
247 248

	
248
    typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
249
    typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier;
249 250
    EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
250 251

	
251
    template <typename _Value>
252
    class NodeMap : public Graph::template NodeMap<_Value> {
252
    template <typename V>
253
    class NodeMap : public GR::template NodeMap<V> {
254
      typedef typename GR::template NodeMap<V> Parent;
255

	
253 256
    public:
254
      typedef typename Graph::template NodeMap<_Value> Parent;
255
      explicit NodeMap(const GraphAdaptorBase<Graph>& adapter)
257
      explicit NodeMap(const GraphAdaptorBase<GR>& adapter)
256 258
        : Parent(*adapter._graph) {}
257
      NodeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
259
      NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value)
258 260
        : Parent(*adapter._graph, value) {}
259 261

	
260 262
    private:
261 263
      NodeMap& operator=(const NodeMap& cmap) {
262 264
        return operator=<NodeMap>(cmap);
263 265
      }
264 266

	
265 267
      template <typename CMap>
266 268
      NodeMap& operator=(const CMap& cmap) {
267 269
        Parent::operator=(cmap);
268 270
        return *this;
269 271
      }
270 272

	
271 273
    };
272 274

	
273
    template <typename _Value>
274
    class ArcMap : public Graph::template ArcMap<_Value> {
275
    template <typename V>
276
    class ArcMap : public GR::template ArcMap<V> {
277
      typedef typename GR::template ArcMap<V> Parent;
278

	
275 279
    public:
276
      typedef typename Graph::template ArcMap<_Value> Parent;
277
      explicit ArcMap(const GraphAdaptorBase<Graph>& adapter)
280
      explicit ArcMap(const GraphAdaptorBase<GR>& adapter)
278 281
        : Parent(*adapter._graph) {}
279
      ArcMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
282
      ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value)
280 283
        : Parent(*adapter._graph, value) {}
281 284

	
282 285
    private:
283 286
      ArcMap& operator=(const ArcMap& cmap) {
284 287
        return operator=<ArcMap>(cmap);
285 288
      }
286 289

	
287 290
      template <typename CMap>
288 291
      ArcMap& operator=(const CMap& cmap) {
289 292
        Parent::operator=(cmap);
290 293
        return *this;
291 294
      }
292 295
    };
293 296

	
294
    template <typename _Value>
295
    class EdgeMap : public Graph::template EdgeMap<_Value> {
297
    template <typename V>
298
    class EdgeMap : public GR::template EdgeMap<V> {
299
      typedef typename GR::template EdgeMap<V> Parent;
300

	
296 301
    public:
297
      typedef typename Graph::template EdgeMap<_Value> Parent;
298
      explicit EdgeMap(const GraphAdaptorBase<Graph>& adapter)
302
      explicit EdgeMap(const GraphAdaptorBase<GR>& adapter)
299 303
        : Parent(*adapter._graph) {}
300
      EdgeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value)
304
      EdgeMap(const GraphAdaptorBase<GR>& adapter, const V& value)
301 305
        : Parent(*adapter._graph, value) {}
302 306

	
303 307
    private:
304 308
      EdgeMap& operator=(const EdgeMap& cmap) {
305 309
        return operator=<EdgeMap>(cmap);
306 310
      }
307 311

	
308 312
      template <typename CMap>
309 313
      EdgeMap& operator=(const CMap& cmap) {
310 314
        Parent::operator=(cmap);
311 315
        return *this;
312 316
      }
313 317
    };
314 318

	
315 319
  };
316 320

	
317
  template <typename _Digraph>
318
  class ReverseDigraphBase : public DigraphAdaptorBase<_Digraph> {
321
  template <typename DGR>
322
  class ReverseDigraphBase : public DigraphAdaptorBase<DGR> {
323
    typedef DigraphAdaptorBase<DGR> Parent;
319 324
  public:
320
    typedef _Digraph Digraph;
321
    typedef DigraphAdaptorBase<_Digraph> Parent;
325
    typedef DGR Digraph;
322 326
  protected:
323 327
    ReverseDigraphBase() : Parent() { }
324 328
  public:
325 329
    typedef typename Parent::Node Node;
326 330
    typedef typename Parent::Arc Arc;
327 331

	
328 332
    void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); }
329 333
    void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); }
330 334

	
331 335
    void nextIn(Arc& a) const { Parent::nextOut(a); }
332 336
    void nextOut(Arc& a) const { Parent::nextIn(a); }
333 337

	
334 338
    Node source(const Arc& a) const { return Parent::target(a); }
335 339
    Node target(const Arc& a) const { return Parent::source(a); }
336 340

	
337 341
    Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
338 342

	
339
    typedef FindArcTagIndicator<Digraph> FindArcTag;
343
    typedef FindArcTagIndicator<DGR> FindArcTag;
340 344
    Arc findArc(const Node& u, const Node& v,
341 345
                const Arc& prev = INVALID) const {
342 346
      return Parent::findArc(v, u, prev);
343 347
    }
344 348

	
345 349
  };
346 350

	
347 351
  /// \ingroup graph_adaptors
348 352
  ///
349 353
  /// \brief Adaptor class for reversing the orientation of the arcs in
350 354
  /// a digraph.
351 355
  ///
352 356
  /// ReverseDigraph can be used for reversing the arcs in a digraph.
353 357
  /// It conforms to the \ref concepts::Digraph "Digraph" concept.
354 358
  ///
355 359
  /// The adapted digraph can also be modified through this adaptor
356 360
  /// by adding or removing nodes or arcs, unless the \c GR template
357 361
  /// parameter is set to be \c const.
358 362
  ///
359
  /// \tparam GR The type of the adapted digraph.
363
  /// \tparam DGR The type of the adapted digraph.
360 364
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
361 365
  /// It can also be specified to be \c const.
362 366
  ///
363 367
  /// \note The \c Node and \c Arc types of this adaptor and the adapted
364 368
  /// digraph are convertible to each other.
365
  template<typename GR>
369
  template<typename DGR>
366 370
#ifdef DOXYGEN
367 371
  class ReverseDigraph {
368 372
#else
369 373
  class ReverseDigraph :
370
    public DigraphAdaptorExtender<ReverseDigraphBase<GR> > {
374
    public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > {
371 375
#endif
376
    typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent;
372 377
  public:
373 378
    /// The type of the adapted digraph.
374
    typedef GR Digraph;
375
    typedef DigraphAdaptorExtender<ReverseDigraphBase<GR> > Parent;
379
    typedef DGR Digraph;
376 380
  protected:
377 381
    ReverseDigraph() { }
378 382
  public:
379 383

	
380 384
    /// \brief Constructor
381 385
    ///
382 386
    /// Creates a reverse digraph adaptor for the given digraph.
383
    explicit ReverseDigraph(Digraph& digraph) {
384
      Parent::setDigraph(digraph);
387
    explicit ReverseDigraph(DGR& digraph) {
388
      Parent::initialize(digraph);
385 389
    }
386 390
  };
387 391

	
388 392
  /// \brief Returns a read-only ReverseDigraph adaptor
389 393
  ///
390 394
  /// This function just returns a read-only \ref ReverseDigraph adaptor.
391 395
  /// \ingroup graph_adaptors
392 396
  /// \relates ReverseDigraph
393
  template<typename GR>
394
  ReverseDigraph<const GR> reverseDigraph(const GR& digraph) {
395
    return ReverseDigraph<const GR>(digraph);
397
  template<typename DGR>
398
  ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) {
399
    return ReverseDigraph<const DGR>(digraph);
396 400
  }
397 401

	
398 402

	
399
  template <typename _Digraph, typename _NodeFilterMap,
400
            typename _ArcFilterMap, bool _checked = true>
401
  class SubDigraphBase : public DigraphAdaptorBase<_Digraph> {
403
  template <typename DGR, typename NF, typename AF, bool ch = true>
404
  class SubDigraphBase : public DigraphAdaptorBase<DGR> {
405
    typedef DigraphAdaptorBase<DGR> Parent;
402 406
  public:
403
    typedef _Digraph Digraph;
404
    typedef _NodeFilterMap NodeFilterMap;
405
    typedef _ArcFilterMap ArcFilterMap;
407
    typedef DGR Digraph;
408
    typedef NF NodeFilterMap;
409
    typedef AF ArcFilterMap;
406 410

	
407 411
    typedef SubDigraphBase Adaptor;
408
    typedef DigraphAdaptorBase<_Digraph> Parent;
409 412
  protected:
410
    NodeFilterMap* _node_filter;
411
    ArcFilterMap* _arc_filter;
413
    NF* _node_filter;
414
    AF* _arc_filter;
412 415
    SubDigraphBase()
413 416
      : Parent(), _node_filter(0), _arc_filter(0) { }
414 417

	
415
    void setNodeFilterMap(NodeFilterMap& node_filter) {
418
    void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
419
      Parent::initialize(digraph);
416 420
      _node_filter = &node_filter;
417
    }
418
    void setArcFilterMap(ArcFilterMap& arc_filter) {
419
      _arc_filter = &arc_filter;
421
      _arc_filter = &arc_filter;      
420 422
    }
421 423

	
422 424
  public:
423 425

	
424 426
    typedef typename Parent::Node Node;
425 427
    typedef typename Parent::Arc Arc;
426 428

	
427 429
    void first(Node& i) const {
428 430
      Parent::first(i);
429 431
      while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
430 432
    }
431 433

	
432 434
    void first(Arc& i) const {
433 435
      Parent::first(i);
434 436
      while (i != INVALID && (!(*_arc_filter)[i]
435 437
                              || !(*_node_filter)[Parent::source(i)]
436 438
                              || !(*_node_filter)[Parent::target(i)]))
437 439
        Parent::next(i);
438 440
    }
439 441

	
440 442
    void firstIn(Arc& i, const Node& n) const {
441 443
      Parent::firstIn(i, n);
442 444
      while (i != INVALID && (!(*_arc_filter)[i]
443 445
                              || !(*_node_filter)[Parent::source(i)]))
444 446
        Parent::nextIn(i);
445 447
    }
446 448

	
447 449
    void firstOut(Arc& i, const Node& n) const {
448 450
      Parent::firstOut(i, n);
449 451
      while (i != INVALID && (!(*_arc_filter)[i]
450 452
                              || !(*_node_filter)[Parent::target(i)]))
451 453
        Parent::nextOut(i);
452 454
    }
453 455

	
454 456
    void next(Node& i) const {
455 457
      Parent::next(i);
456 458
      while (i != INVALID && !(*_node_filter)[i]) Parent::next(i);
457 459
    }
458 460

	
459 461
    void next(Arc& i) const {
460 462
      Parent::next(i);
461 463
      while (i != INVALID && (!(*_arc_filter)[i]
462 464
                              || !(*_node_filter)[Parent::source(i)]
463 465
                              || !(*_node_filter)[Parent::target(i)]))
464 466
        Parent::next(i);
465 467
    }
466 468

	
467 469
    void nextIn(Arc& i) const {
468 470
      Parent::nextIn(i);
469 471
      while (i != INVALID && (!(*_arc_filter)[i]
470 472
                              || !(*_node_filter)[Parent::source(i)]))
471 473
        Parent::nextIn(i);
472 474
    }
473 475

	
474 476
    void nextOut(Arc& i) const {
475 477
      Parent::nextOut(i);
476 478
      while (i != INVALID && (!(*_arc_filter)[i]
477 479
                              || !(*_node_filter)[Parent::target(i)]))
478 480
        Parent::nextOut(i);
479 481
    }
480 482

	
481 483
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
482 484
    void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
483 485

	
484 486
    bool status(const Node& n) const { return (*_node_filter)[n]; }
485 487
    bool status(const Arc& a) const { return (*_arc_filter)[a]; }
486 488

	
487 489
    typedef False NodeNumTag;
488 490
    typedef False ArcNumTag;
489 491

	
490
    typedef FindArcTagIndicator<Digraph> FindArcTag;
492
    typedef FindArcTagIndicator<DGR> FindArcTag;
491 493
    Arc findArc(const Node& source, const Node& target,
492 494
                const Arc& prev = INVALID) const {
493 495
      if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
494 496
        return INVALID;
495 497
      }
496 498
      Arc arc = Parent::findArc(source, target, prev);
497 499
      while (arc != INVALID && !(*_arc_filter)[arc]) {
498 500
        arc = Parent::findArc(source, target, arc);
499 501
      }
500 502
      return arc;
501 503
    }
502 504

	
503
    template <typename _Value>
504
    class NodeMap : public SubMapExtender<Adaptor,
505
      typename Parent::template NodeMap<_Value> > {
505
  public:
506

	
507
    template <typename V>
508
    class NodeMap 
509
      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, 
510
	      LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
511
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
512
	LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent;
513

	
506 514
    public:
507
      typedef _Value Value;
508
      typedef SubMapExtender<Adaptor, typename Parent::
509
                             template NodeMap<Value> > MapParent;
510

	
511
      NodeMap(const Adaptor& adaptor)
512
        : MapParent(adaptor) {}
513
      NodeMap(const Adaptor& adaptor, const Value& value)
514
        : MapParent(adaptor, value) {}
515
      typedef V Value;
516

	
517
      NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor)
518
        : Parent(adaptor) {}
519
      NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value)
520
        : Parent(adaptor, value) {}
515 521

	
516 522
    private:
517 523
      NodeMap& operator=(const NodeMap& cmap) {
518 524
        return operator=<NodeMap>(cmap);
519 525
      }
520 526

	
521 527
      template <typename CMap>
522 528
      NodeMap& operator=(const CMap& cmap) {
523
        MapParent::operator=(cmap);
529
        Parent::operator=(cmap);
524 530
        return *this;
525 531
      }
526 532
    };
527 533

	
528
    template <typename _Value>
529
    class ArcMap : public SubMapExtender<Adaptor,
530
      typename Parent::template ArcMap<_Value> > {
534
    template <typename V>
535
    class ArcMap 
536
      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
537
	      LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
538
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>,
539
        LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent;
540

	
531 541
    public:
532
      typedef _Value Value;
533
      typedef SubMapExtender<Adaptor, typename Parent::
534
                             template ArcMap<Value> > MapParent;
535

	
536
      ArcMap(const Adaptor& adaptor)
537
        : MapParent(adaptor) {}
538
      ArcMap(const Adaptor& adaptor, const Value& value)
539
        : MapParent(adaptor, value) {}
542
      typedef V Value;
543

	
544
      ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor)
545
        : Parent(adaptor) {}
546
      ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value)
547
        : Parent(adaptor, value) {}
540 548

	
541 549
    private:
542 550
      ArcMap& operator=(const ArcMap& cmap) {
543 551
        return operator=<ArcMap>(cmap);
544 552
      }
545 553

	
546 554
      template <typename CMap>
547 555
      ArcMap& operator=(const CMap& cmap) {
548
        MapParent::operator=(cmap);
556
        Parent::operator=(cmap);
549 557
        return *this;
550 558
      }
551 559
    };
552 560

	
553 561
  };
554 562

	
555
  template <typename _Digraph, typename _NodeFilterMap, typename _ArcFilterMap>
556
  class SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false>
557
    : public DigraphAdaptorBase<_Digraph> {
563
  template <typename DGR, typename NF, typename AF>
564
  class SubDigraphBase<DGR, NF, AF, false>
565
    : public DigraphAdaptorBase<DGR> {
566
    typedef DigraphAdaptorBase<DGR> Parent;
558 567
  public:
559
    typedef _Digraph Digraph;
560
    typedef _NodeFilterMap NodeFilterMap;
561
    typedef _ArcFilterMap ArcFilterMap;
568
    typedef DGR Digraph;
569
    typedef NF NodeFilterMap;
570
    typedef AF ArcFilterMap;
562 571

	
563 572
    typedef SubDigraphBase Adaptor;
564
    typedef DigraphAdaptorBase<Digraph> Parent;
565 573
  protected:
566
    NodeFilterMap* _node_filter;
567
    ArcFilterMap* _arc_filter;
574
    NF* _node_filter;
575
    AF* _arc_filter;
568 576
    SubDigraphBase()
569 577
      : Parent(), _node_filter(0), _arc_filter(0) { }
570 578

	
571
    void setNodeFilterMap(NodeFilterMap& node_filter) {
579
    void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
580
      Parent::initialize(digraph);
572 581
      _node_filter = &node_filter;
573
    }
574
    void setArcFilterMap(ArcFilterMap& arc_filter) {
575
      _arc_filter = &arc_filter;
582
      _arc_filter = &arc_filter;      
576 583
    }
577 584

	
578 585
  public:
579 586

	
580 587
    typedef typename Parent::Node Node;
581 588
    typedef typename Parent::Arc Arc;
582 589

	
583 590
    void first(Node& i) const {
584 591
      Parent::first(i);
585 592
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
586 593
    }
587 594

	
588 595
    void first(Arc& i) const {
589 596
      Parent::first(i);
590 597
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
591 598
    }
592 599

	
593 600
    void firstIn(Arc& i, const Node& n) const {
594 601
      Parent::firstIn(i, n);
595 602
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
596 603
    }
597 604

	
598 605
    void firstOut(Arc& i, const Node& n) const {
599 606
      Parent::firstOut(i, n);
600 607
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
601 608
    }
602 609

	
603 610
    void next(Node& i) const {
604 611
      Parent::next(i);
605 612
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
606 613
    }
607 614
    void next(Arc& i) const {
608 615
      Parent::next(i);
609 616
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i);
610 617
    }
611 618
    void nextIn(Arc& i) const {
612 619
      Parent::nextIn(i);
613 620
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i);
614 621
    }
615 622

	
616 623
    void nextOut(Arc& i) const {
617 624
      Parent::nextOut(i);
618 625
      while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i);
619 626
    }
620 627

	
621 628
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
622 629
    void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
623 630

	
624 631
    bool status(const Node& n) const { return (*_node_filter)[n]; }
625 632
    bool status(const Arc& a) const { return (*_arc_filter)[a]; }
626 633

	
627 634
    typedef False NodeNumTag;
628 635
    typedef False ArcNumTag;
629 636

	
630
    typedef FindArcTagIndicator<Digraph> FindArcTag;
637
    typedef FindArcTagIndicator<DGR> FindArcTag;
631 638
    Arc findArc(const Node& source, const Node& target,
632 639
                const Arc& prev = INVALID) const {
633 640
      if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
634 641
        return INVALID;
635 642
      }
636 643
      Arc arc = Parent::findArc(source, target, prev);
637 644
      while (arc != INVALID && !(*_arc_filter)[arc]) {
638 645
        arc = Parent::findArc(source, target, arc);
639 646
      }
640 647
      return arc;
641 648
    }
642 649

	
643
    template <typename _Value>
644
    class NodeMap : public SubMapExtender<Adaptor,
645
      typename Parent::template NodeMap<_Value> > {
650
    template <typename V>
651
    class NodeMap 
652
      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
653
          LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
654
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, 
655
        LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent;
656

	
646 657
    public:
647
      typedef _Value Value;
648
      typedef SubMapExtender<Adaptor, typename Parent::
649
                             template NodeMap<Value> > MapParent;
650

	
651
      NodeMap(const Adaptor& adaptor)
652
        : MapParent(adaptor) {}
653
      NodeMap(const Adaptor& adaptor, const Value& value)
654
        : MapParent(adaptor, value) {}
658
      typedef V Value;
659

	
660
      NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor)
661
        : Parent(adaptor) {}
662
      NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value)
663
        : Parent(adaptor, value) {}
655 664

	
656 665
    private:
657 666
      NodeMap& operator=(const NodeMap& cmap) {
658 667
        return operator=<NodeMap>(cmap);
659 668
      }
660 669

	
661 670
      template <typename CMap>
662 671
      NodeMap& operator=(const CMap& cmap) {
663
        MapParent::operator=(cmap);
672
        Parent::operator=(cmap);
664 673
        return *this;
665 674
      }
666 675
    };
667 676

	
668
    template <typename _Value>
669
    class ArcMap : public SubMapExtender<Adaptor,
670
      typename Parent::template ArcMap<_Value> > {
677
    template <typename V>
678
    class ArcMap 
679
      : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
680
          LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
681
      typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>,
682
        LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent;
683

	
671 684
    public:
672
      typedef _Value Value;
673
      typedef SubMapExtender<Adaptor, typename Parent::
674
                             template ArcMap<Value> > MapParent;
675

	
676
      ArcMap(const Adaptor& adaptor)
677
        : MapParent(adaptor) {}
678
      ArcMap(const Adaptor& adaptor, const Value& value)
679
        : MapParent(adaptor, value) {}
685
      typedef V Value;
686

	
687
      ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor)
688
        : Parent(adaptor) {}
689
      ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value)
690
        : Parent(adaptor, value) {}
680 691

	
681 692
    private:
682 693
      ArcMap& operator=(const ArcMap& cmap) {
683 694
        return operator=<ArcMap>(cmap);
684 695
      }
685 696

	
686 697
      template <typename CMap>
687 698
      ArcMap& operator=(const CMap& cmap) {
688
        MapParent::operator=(cmap);
699
        Parent::operator=(cmap);
689 700
        return *this;
690 701
      }
691 702
    };
692 703

	
693 704
  };
694 705

	
695 706
  /// \ingroup graph_adaptors
696 707
  ///
697 708
  /// \brief Adaptor class for hiding nodes and arcs in a digraph
698 709
  ///
699 710
  /// SubDigraph can be used for hiding nodes and arcs in a digraph.
700 711
  /// A \c bool node map and a \c bool arc map must be specified, which
701 712
  /// define the filters for nodes and arcs.
702 713
  /// Only the nodes and arcs with \c true filter value are
703 714
  /// shown in the subdigraph. The arcs that are incident to hidden
704 715
  /// nodes are also filtered out.
705 716
  /// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept.
706 717
  ///
707 718
  /// The adapted digraph can also be modified through this adaptor
708 719
  /// by adding or removing nodes or arcs, unless the \c GR template
709 720
  /// parameter is set to be \c const.
710 721
  ///
711
  /// \tparam GR The type of the adapted digraph.
722
  /// \tparam DGR The type of the adapted digraph.
712 723
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
713 724
  /// It can also be specified to be \c const.
714 725
  /// \tparam NF The type of the node filter map.
715 726
  /// It must be a \c bool (or convertible) node map of the
716 727
  /// adapted digraph. The default type is
717
  /// \ref concepts::Digraph::NodeMap "GR::NodeMap<bool>".
728
  /// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>".
718 729
  /// \tparam AF The type of the arc filter map.
719 730
  /// It must be \c bool (or convertible) arc map of the
720 731
  /// adapted digraph. The default type is
721
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<bool>".
732
  /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>".
722 733
  ///
723 734
  /// \note The \c Node and \c Arc types of this adaptor and the adapted
724 735
  /// digraph are convertible to each other.
725 736
  ///
726 737
  /// \see FilterNodes
727 738
  /// \see FilterArcs
728 739
#ifdef DOXYGEN
729
  template<typename GR, typename NF, typename AF>
740
  template<typename DGR, typename NF, typename AF>
730 741
  class SubDigraph {
731 742
#else
732
  template<typename GR,
733
           typename NF = typename GR::template NodeMap<bool>,
734
           typename AF = typename GR::template ArcMap<bool> >
743
  template<typename DGR,
744
           typename NF = typename DGR::template NodeMap<bool>,
745
           typename AF = typename DGR::template ArcMap<bool> >
735 746
  class SubDigraph :
736
    public DigraphAdaptorExtender<SubDigraphBase<GR, NF, AF, true> > {
747
    public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > {
737 748
#endif
738 749
  public:
739 750
    /// The type of the adapted digraph.
740
    typedef GR Digraph;
751
    typedef DGR Digraph;
741 752
    /// The type of the node filter map.
742 753
    typedef NF NodeFilterMap;
743 754
    /// The type of the arc filter map.
744 755
    typedef AF ArcFilterMap;
745 756

	
746
    typedef DigraphAdaptorExtender<SubDigraphBase<GR, NF, AF, true> >
757
    typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> >
747 758
      Parent;
748 759

	
749 760
    typedef typename Parent::Node Node;
750 761
    typedef typename Parent::Arc Arc;
751 762

	
752 763
  protected:
753 764
    SubDigraph() { }
754 765
  public:
755 766

	
756 767
    /// \brief Constructor
757 768
    ///
758 769
    /// Creates a subdigraph for the given digraph with the
759 770
    /// given node and arc filter maps.
760
    SubDigraph(Digraph& digraph, NodeFilterMap& node_filter,
761
               ArcFilterMap& arc_filter) {
762
      setDigraph(digraph);
763
      setNodeFilterMap(node_filter);
764
      setArcFilterMap(arc_filter);
771
    SubDigraph(DGR& digraph, NF& node_filter, AF& arc_filter) {
772
      Parent::initialize(digraph, node_filter, arc_filter);
765 773
    }
766 774

	
767 775
    /// \brief Sets the status of the given node
768 776
    ///
769 777
    /// This function sets the status of the given node.
770 778
    /// It is done by simply setting the assigned value of \c n
771 779
    /// to \c v in the node filter map.
772 780
    void status(const Node& n, bool v) const { Parent::status(n, v); }
773 781

	
774 782
    /// \brief Sets the status of the given arc
775 783
    ///
776 784
    /// This function sets the status of the given arc.
777 785
    /// It is done by simply setting the assigned value of \c a
778 786
    /// to \c v in the arc filter map.
779 787
    void status(const Arc& a, bool v) const { Parent::status(a, v); }
780 788

	
781 789
    /// \brief Returns the status of the given node
782 790
    ///
783 791
    /// This function returns the status of the given node.
784 792
    /// It is \c true if the given node is enabled (i.e. not hidden).
785 793
    bool status(const Node& n) const { return Parent::status(n); }
786 794

	
787 795
    /// \brief Returns the status of the given arc
788 796
    ///
789 797
    /// This function returns the status of the given arc.
790 798
    /// It is \c true if the given arc is enabled (i.e. not hidden).
791 799
    bool status(const Arc& a) const { return Parent::status(a); }
792 800

	
793 801
    /// \brief Disables the given node
794 802
    ///
795 803
    /// This function disables the given node in the subdigraph,
796 804
    /// so the iteration jumps over it.
797 805
    /// It is the same as \ref status() "status(n, false)".
798 806
    void disable(const Node& n) const { Parent::status(n, false); }
799 807

	
800 808
    /// \brief Disables the given arc
801 809
    ///
802 810
    /// This function disables the given arc in the subdigraph,
803 811
    /// so the iteration jumps over it.
804 812
    /// It is the same as \ref status() "status(a, false)".
805 813
    void disable(const Arc& a) const { Parent::status(a, false); }
806 814

	
807 815
    /// \brief Enables the given node
808 816
    ///
809 817
    /// This function enables the given node in the subdigraph.
810 818
    /// It is the same as \ref status() "status(n, true)".
811 819
    void enable(const Node& n) const { Parent::status(n, true); }
812 820

	
813 821
    /// \brief Enables the given arc
814 822
    ///
815 823
    /// This function enables the given arc in the subdigraph.
816 824
    /// It is the same as \ref status() "status(a, true)".
817 825
    void enable(const Arc& a) const { Parent::status(a, true); }
818 826

	
819 827
  };
820 828

	
821 829
  /// \brief Returns a read-only SubDigraph adaptor
822 830
  ///
823 831
  /// This function just returns a read-only \ref SubDigraph adaptor.
824 832
  /// \ingroup graph_adaptors
825 833
  /// \relates SubDigraph
826
  template<typename GR, typename NF, typename AF>
827
  SubDigraph<const GR, NF, AF>
828
  subDigraph(const GR& digraph,
829
             NF& node_filter_map, AF& arc_filter_map) {
830
    return SubDigraph<const GR, NF, AF>
831
      (digraph, node_filter_map, arc_filter_map);
834
  template<typename DGR, typename NF, typename AF>
835
  SubDigraph<const DGR, NF, AF>
836
  subDigraph(const DGR& digraph,
837
             NF& node_filter, AF& arc_filter) {
838
    return SubDigraph<const DGR, NF, AF>
839
      (digraph, node_filter, arc_filter);
832 840
  }
833 841

	
834
  template<typename GR, typename NF, typename AF>
835
  SubDigraph<const GR, const NF, AF>
836
  subDigraph(const GR& digraph,
837
             const NF& node_filter_map, AF& arc_filter_map) {
838
    return SubDigraph<const GR, const NF, AF>
839
      (digraph, node_filter_map, arc_filter_map);
842
  template<typename DGR, typename NF, typename AF>
843
  SubDigraph<const DGR, const NF, AF>
844
  subDigraph(const DGR& digraph,
845
             const NF& node_filter, AF& arc_filter) {
846
    return SubDigraph<const DGR, const NF, AF>
847
      (digraph, node_filter, arc_filter);
840 848
  }
841 849

	
842
  template<typename GR, typename NF, typename AF>
843
  SubDigraph<const GR, NF, const AF>
844
  subDigraph(const GR& digraph,
845
             NF& node_filter_map, const AF& arc_filter_map) {
846
    return SubDigraph<const GR, NF, const AF>
847
      (digraph, node_filter_map, arc_filter_map);
850
  template<typename DGR, typename NF, typename AF>
851
  SubDigraph<const DGR, NF, const AF>
852
  subDigraph(const DGR& digraph,
853
             NF& node_filter, const AF& arc_filter) {
854
    return SubDigraph<const DGR, NF, const AF>
855
      (digraph, node_filter, arc_filter);
848 856
  }
849 857

	
850
  template<typename GR, typename NF, typename AF>
851
  SubDigraph<const GR, const NF, const AF>
852
  subDigraph(const GR& digraph,
853
             const NF& node_filter_map, const AF& arc_filter_map) {
854
    return SubDigraph<const GR, const NF, const AF>
855
      (digraph, node_filter_map, arc_filter_map);
858
  template<typename DGR, typename NF, typename AF>
859
  SubDigraph<const DGR, const NF, const AF>
860
  subDigraph(const DGR& digraph,
861
             const NF& node_filter, const AF& arc_filter) {
862
    return SubDigraph<const DGR, const NF, const AF>
863
      (digraph, node_filter, arc_filter);
856 864
  }
857 865

	
858 866

	
859
  template <typename _Graph, typename _NodeFilterMap,
860
            typename _EdgeFilterMap, bool _checked = true>
861
  class SubGraphBase : public GraphAdaptorBase<_Graph> {
867
  template <typename GR, typename NF, typename EF, bool ch = true>
868
  class SubGraphBase : public GraphAdaptorBase<GR> {
869
    typedef GraphAdaptorBase<GR> Parent;
862 870
  public:
863
    typedef _Graph Graph;
864
    typedef _NodeFilterMap NodeFilterMap;
865
    typedef _EdgeFilterMap EdgeFilterMap;
871
    typedef GR Graph;
872
    typedef NF NodeFilterMap;
873
    typedef EF EdgeFilterMap;
866 874

	
867 875
    typedef SubGraphBase Adaptor;
868
    typedef GraphAdaptorBase<_Graph> Parent;
869 876
  protected:
870 877

	
871
    NodeFilterMap* _node_filter_map;
872
    EdgeFilterMap* _edge_filter_map;
878
    NF* _node_filter;
879
    EF* _edge_filter;
873 880

	
874 881
    SubGraphBase()
875
      : Parent(), _node_filter_map(0), _edge_filter_map(0) { }
876

	
877
    void setNodeFilterMap(NodeFilterMap& node_filter_map) {
878
      _node_filter_map=&node_filter_map;
879
    }
880
    void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
881
      _edge_filter_map=&edge_filter_map;
882
      : Parent(), _node_filter(0), _edge_filter(0) { }
883

	
884
    void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
885
      Parent::initialize(graph);
886
      _node_filter = &node_filter;
887
      _edge_filter = &edge_filter;
882 888
    }
883 889

	
884 890
  public:
885 891

	
886 892
    typedef typename Parent::Node Node;
887 893
    typedef typename Parent::Arc Arc;
888 894
    typedef typename Parent::Edge Edge;
889 895

	
890 896
    void first(Node& i) const {
891 897
      Parent::first(i);
892
      while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
898
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
893 899
    }
894 900

	
895 901
    void first(Arc& i) const {
896 902
      Parent::first(i);
897
      while (i!=INVALID && (!(*_edge_filter_map)[i]
898
                            || !(*_node_filter_map)[Parent::source(i)]
899
                            || !(*_node_filter_map)[Parent::target(i)]))
903
      while (i!=INVALID && (!(*_edge_filter)[i]
904
                            || !(*_node_filter)[Parent::source(i)]
905
                            || !(*_node_filter)[Parent::target(i)]))
900 906
        Parent::next(i);
901 907
    }
902 908

	
903 909
    void first(Edge& i) const {
904 910
      Parent::first(i);
905
      while (i!=INVALID && (!(*_edge_filter_map)[i]
906
                            || !(*_node_filter_map)[Parent::u(i)]
907
                            || !(*_node_filter_map)[Parent::v(i)]))
911
      while (i!=INVALID && (!(*_edge_filter)[i]
912
                            || !(*_node_filter)[Parent::u(i)]
913
                            || !(*_node_filter)[Parent::v(i)]))
908 914
        Parent::next(i);
909 915
    }
910 916

	
911 917
    void firstIn(Arc& i, const Node& n) const {
912 918
      Parent::firstIn(i, n);
913
      while (i!=INVALID && (!(*_edge_filter_map)[i]
914
                            || !(*_node_filter_map)[Parent::source(i)]))
919
      while (i!=INVALID && (!(*_edge_filter)[i]
920
                            || !(*_node_filter)[Parent::source(i)]))
915 921
        Parent::nextIn(i);
916 922
    }
917 923

	
918 924
    void firstOut(Arc& i, const Node& n) const {
919 925
      Parent::firstOut(i, n);
920
      while (i!=INVALID && (!(*_edge_filter_map)[i]
921
                            || !(*_node_filter_map)[Parent::target(i)]))
926
      while (i!=INVALID && (!(*_edge_filter)[i]
927
                            || !(*_node_filter)[Parent::target(i)]))
922 928
        Parent::nextOut(i);
923 929
    }
924 930

	
925 931
    void firstInc(Edge& i, bool& d, const Node& n) const {
926 932
      Parent::firstInc(i, d, n);
927
      while (i!=INVALID && (!(*_edge_filter_map)[i]
928
                            || !(*_node_filter_map)[Parent::u(i)]
929
                            || !(*_node_filter_map)[Parent::v(i)]))
933
      while (i!=INVALID && (!(*_edge_filter)[i]
934
                            || !(*_node_filter)[Parent::u(i)]
935
                            || !(*_node_filter)[Parent::v(i)]))
930 936
        Parent::nextInc(i, d);
931 937
    }
932 938

	
933 939
    void next(Node& i) const {
934 940
      Parent::next(i);
935
      while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
941
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
936 942
    }
937 943

	
938 944
    void next(Arc& i) const {
939 945
      Parent::next(i);
940
      while (i!=INVALID && (!(*_edge_filter_map)[i]
941
                            || !(*_node_filter_map)[Parent::source(i)]
942
                            || !(*_node_filter_map)[Parent::target(i)]))
946
      while (i!=INVALID && (!(*_edge_filter)[i]
947
                            || !(*_node_filter)[Parent::source(i)]
948
                            || !(*_node_filter)[Parent::target(i)]))
943 949
        Parent::next(i);
944 950
    }
945 951

	
946 952
    void next(Edge& i) const {
947 953
      Parent::next(i);
948
      while (i!=INVALID && (!(*_edge_filter_map)[i]
949
                            || !(*_node_filter_map)[Parent::u(i)]
950
                            || !(*_node_filter_map)[Parent::v(i)]))
954
      while (i!=INVALID && (!(*_edge_filter)[i]
955
                            || !(*_node_filter)[Parent::u(i)]
956
                            || !(*_node_filter)[Parent::v(i)]))
951 957
        Parent::next(i);
952 958
    }
953 959

	
954 960
    void nextIn(Arc& i) const {
955 961
      Parent::nextIn(i);
956
      while (i!=INVALID && (!(*_edge_filter_map)[i]
957
                            || !(*_node_filter_map)[Parent::source(i)]))
962
      while (i!=INVALID && (!(*_edge_filter)[i]
963
                            || !(*_node_filter)[Parent::source(i)]))
958 964
        Parent::nextIn(i);
959 965
    }
960 966

	
961 967
    void nextOut(Arc& i) const {
962 968
      Parent::nextOut(i);
963
      while (i!=INVALID && (!(*_edge_filter_map)[i]
964
                            || !(*_node_filter_map)[Parent::target(i)]))
969
      while (i!=INVALID && (!(*_edge_filter)[i]
970
                            || !(*_node_filter)[Parent::target(i)]))
965 971
        Parent::nextOut(i);
966 972
    }
967 973

	
968 974
    void nextInc(Edge& i, bool& d) const {
969 975
      Parent::nextInc(i, d);
970
      while (i!=INVALID && (!(*_edge_filter_map)[i]
971
                            || !(*_node_filter_map)[Parent::u(i)]
972
                            || !(*_node_filter_map)[Parent::v(i)]))
976
      while (i!=INVALID && (!(*_edge_filter)[i]
977
                            || !(*_node_filter)[Parent::u(i)]
978
                            || !(*_node_filter)[Parent::v(i)]))
973 979
        Parent::nextInc(i, d);
974 980
    }
975 981

	
976
    void status(const Node& n, bool v) const { _node_filter_map->set(n, v); }
977
    void status(const Edge& e, bool v) const { _edge_filter_map->set(e, v); }
978

	
979
    bool status(const Node& n) const { return (*_node_filter_map)[n]; }
980
    bool status(const Edge& e) const { return (*_edge_filter_map)[e]; }
982
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
983
    void status(const Edge& e, bool v) const { _edge_filter->set(e, v); }
984

	
985
    bool status(const Node& n) const { return (*_node_filter)[n]; }
986
    bool status(const Edge& e) const { return (*_edge_filter)[e]; }
981 987

	
982 988
    typedef False NodeNumTag;
983 989
    typedef False ArcNumTag;
984 990
    typedef False EdgeNumTag;
985 991

	
986 992
    typedef FindArcTagIndicator<Graph> FindArcTag;
987 993
    Arc findArc(const Node& u, const Node& v,
988 994
                const Arc& prev = INVALID) const {
989
      if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
995
      if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
990 996
        return INVALID;
991 997
      }
992 998
      Arc arc = Parent::findArc(u, v, prev);
993
      while (arc != INVALID && !(*_edge_filter_map)[arc]) {
999
      while (arc != INVALID && !(*_edge_filter)[arc]) {
994 1000
        arc = Parent::findArc(u, v, arc);
995 1001
      }
996 1002
      return arc;
997 1003
    }
998 1004

	
999 1005
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1000 1006
    Edge findEdge(const Node& u, const Node& v,
1001 1007
                  const Edge& prev = INVALID) const {
1002
      if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) {
1008
      if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
1003 1009
        return INVALID;
1004 1010
      }
1005 1011
      Edge edge = Parent::findEdge(u, v, prev);
1006
      while (edge != INVALID && !(*_edge_filter_map)[edge]) {
1012
      while (edge != INVALID && !(*_edge_filter)[edge]) {
1007 1013
        edge = Parent::findEdge(u, v, edge);
1008 1014
      }
1009 1015
      return edge;
1010 1016
    }
1011 1017

	
1012
    template <typename _Value>
1013
    class NodeMap : public SubMapExtender<Adaptor,
1014
      typename Parent::template NodeMap<_Value> > {
1018
    template <typename V>
1019
    class NodeMap 
1020
      : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1021
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
1022
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, 
1023
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent;
1024

	
1015 1025
    public:
1016
      typedef _Value Value;
1017
      typedef SubMapExtender<Adaptor, typename Parent::
1018
                             template NodeMap<Value> > MapParent;
1019

	
1020
      NodeMap(const Adaptor& adaptor)
1021
        : MapParent(adaptor) {}
1022
      NodeMap(const Adaptor& adaptor, const Value& value)
1023
        : MapParent(adaptor, value) {}
1026
      typedef V Value;
1027

	
1028
      NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor)
1029
        : Parent(adaptor) {}
1030
      NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value)
1031
        : Parent(adaptor, value) {}
1024 1032

	
1025 1033
    private:
1026 1034
      NodeMap& operator=(const NodeMap& cmap) {
1027 1035
        return operator=<NodeMap>(cmap);
1028 1036
      }
1029 1037

	
1030 1038
      template <typename CMap>
1031 1039
      NodeMap& operator=(const CMap& cmap) {
1032
        MapParent::operator=(cmap);
1040
        Parent::operator=(cmap);
1033 1041
        return *this;
1034 1042
      }
1035 1043
    };
1036 1044

	
1037
    template <typename _Value>
1038
    class ArcMap : public SubMapExtender<Adaptor,
1039
      typename Parent::template ArcMap<_Value> > {
1045
    template <typename V>
1046
    class ArcMap 
1047
      : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1048
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
1049
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, 
1050
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent;
1051

	
1040 1052
    public:
1041
      typedef _Value Value;
1042
      typedef SubMapExtender<Adaptor, typename Parent::
1043
                             template ArcMap<Value> > MapParent;
1044

	
1045
      ArcMap(const Adaptor& adaptor)
1046
        : MapParent(adaptor) {}
1047
      ArcMap(const Adaptor& adaptor, const Value& value)
1048
        : MapParent(adaptor, value) {}
1053
      typedef V Value;
1054

	
1055
      ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor)
1056
        : Parent(adaptor) {}
1057
      ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value)
1058
        : Parent(adaptor, value) {}
1049 1059

	
1050 1060
    private:
1051 1061
      ArcMap& operator=(const ArcMap& cmap) {
1052 1062
        return operator=<ArcMap>(cmap);
1053 1063
      }
1054 1064

	
1055 1065
      template <typename CMap>
1056 1066
      ArcMap& operator=(const CMap& cmap) {
1057
        MapParent::operator=(cmap);
1067
        Parent::operator=(cmap);
1058 1068
        return *this;
1059 1069
      }
1060 1070
    };
1061 1071

	
1062
    template <typename _Value>
1063
    class EdgeMap : public SubMapExtender<Adaptor,
1064
      typename Parent::template EdgeMap<_Value> > {
1072
    template <typename V>
1073
    class EdgeMap 
1074
      : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>,
1075
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
1076
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, 
1077
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent;
1078

	
1065 1079
    public:
1066
      typedef _Value Value;
1067
      typedef SubMapExtender<Adaptor, typename Parent::
1068
                             template EdgeMap<Value> > MapParent;
1069

	
1070
      EdgeMap(const Adaptor& adaptor)
1071
        : MapParent(adaptor) {}
1072

	
1073
      EdgeMap(const Adaptor& adaptor, const Value& value)
1074
        : MapParent(adaptor, value) {}
1080
      typedef V Value;
1081

	
1082
      EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor)
1083
        : Parent(adaptor) {}
1084

	
1085
      EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value)
1086
        : Parent(adaptor, value) {}
1075 1087

	
1076 1088
    private:
1077 1089
      EdgeMap& operator=(const EdgeMap& cmap) {
1078 1090
        return operator=<EdgeMap>(cmap);
1079 1091
      }
1080 1092

	
1081 1093
      template <typename CMap>
1082 1094
      EdgeMap& operator=(const CMap& cmap) {
1083
        MapParent::operator=(cmap);
1095
        Parent::operator=(cmap);
1084 1096
        return *this;
1085 1097
      }
1086 1098
    };
1087 1099

	
1088 1100
  };
1089 1101

	
1090
  template <typename _Graph, typename _NodeFilterMap, typename _EdgeFilterMap>
1091
  class SubGraphBase<_Graph, _NodeFilterMap, _EdgeFilterMap, false>
1092
    : public GraphAdaptorBase<_Graph> {
1102
  template <typename GR, typename NF, typename EF>
1103
  class SubGraphBase<GR, NF, EF, false>
1104
    : public GraphAdaptorBase<GR> {
1105
    typedef GraphAdaptorBase<GR> Parent;
1093 1106
  public:
1094
    typedef _Graph Graph;
1095
    typedef _NodeFilterMap NodeFilterMap;
1096
    typedef _EdgeFilterMap EdgeFilterMap;
1107
    typedef GR Graph;
1108
    typedef NF NodeFilterMap;
1109
    typedef EF EdgeFilterMap;
1097 1110

	
1098 1111
    typedef SubGraphBase Adaptor;
1099
    typedef GraphAdaptorBase<_Graph> Parent;
1100 1112
  protected:
1101
    NodeFilterMap* _node_filter_map;
1102
    EdgeFilterMap* _edge_filter_map;
1103
    SubGraphBase() : Parent(),
1104
                     _node_filter_map(0), _edge_filter_map(0) { }
1105

	
1106
    void setNodeFilterMap(NodeFilterMap& node_filter_map) {
1107
      _node_filter_map=&node_filter_map;
1108
    }
1109
    void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) {
1110
      _edge_filter_map=&edge_filter_map;
1113
    NF* _node_filter;
1114
    EF* _edge_filter;
1115
    SubGraphBase() 
1116
	  : Parent(), _node_filter(0), _edge_filter(0) { }
1117

	
1118
    void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
1119
      Parent::initialize(graph);
1120
      _node_filter = &node_filter;
1121
      _edge_filter = &edge_filter;
1111 1122
    }
1112 1123

	
1113 1124
  public:
1114 1125

	
1115 1126
    typedef typename Parent::Node Node;
1116 1127
    typedef typename Parent::Arc Arc;
1117 1128
    typedef typename Parent::Edge Edge;
1118 1129

	
1119 1130
    void first(Node& i) const {
1120 1131
      Parent::first(i);
1121
      while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
1132
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
1122 1133
    }
1123 1134

	
1124 1135
    void first(Arc& i) const {
1125 1136
      Parent::first(i);
1126
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1137
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1127 1138
    }
1128 1139

	
1129 1140
    void first(Edge& i) const {
1130 1141
      Parent::first(i);
1131
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1142
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1132 1143
    }
1133 1144

	
1134 1145
    void firstIn(Arc& i, const Node& n) const {
1135 1146
      Parent::firstIn(i, n);
1136
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
1147
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i);
1137 1148
    }
1138 1149

	
1139 1150
    void firstOut(Arc& i, const Node& n) const {
1140 1151
      Parent::firstOut(i, n);
1141
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
1152
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i);
1142 1153
    }
1143 1154

	
1144 1155
    void firstInc(Edge& i, bool& d, const Node& n) const {
1145 1156
      Parent::firstInc(i, d, n);
1146
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
1157
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d);
1147 1158
    }
1148 1159

	
1149 1160
    void next(Node& i) const {
1150 1161
      Parent::next(i);
1151
      while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i);
1162
      while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i);
1152 1163
    }
1153 1164
    void next(Arc& i) const {
1154 1165
      Parent::next(i);
1155
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1166
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1156 1167
    }
1157 1168
    void next(Edge& i) const {
1158 1169
      Parent::next(i);
1159
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i);
1170
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i);
1160 1171
    }
1161 1172
    void nextIn(Arc& i) const {
1162 1173
      Parent::nextIn(i);
1163
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i);
1174
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i);
1164 1175
    }
1165 1176

	
1166 1177
    void nextOut(Arc& i) const {
1167 1178
      Parent::nextOut(i);
1168
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i);
1179
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i);
1169 1180
    }
1170 1181
    void nextInc(Edge& i, bool& d) const {
1171 1182
      Parent::nextInc(i, d);
1172
      while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d);
1183
      while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d);
1173 1184
    }
1174 1185

	
1175
    void status(const Node& n, bool v) const { _node_filter_map->set(n, v); }
1176
    void status(const Edge& e, bool v) const { _edge_filter_map->set(e, v); }
1177

	
1178
    bool status(const Node& n) const { return (*_node_filter_map)[n]; }
1179
    bool status(const Edge& e) const { return (*_edge_filter_map)[e]; }
1186
    void status(const Node& n, bool v) const { _node_filter->set(n, v); }
1187
    void status(const Edge& e, bool v) const { _edge_filter->set(e, v); }
1188

	
1189
    bool status(const Node& n) const { return (*_node_filter)[n]; }
1190
    bool status(const Edge& e) const { return (*_edge_filter)[e]; }
1180 1191

	
1181 1192
    typedef False NodeNumTag;
1182 1193
    typedef False ArcNumTag;
1183 1194
    typedef False EdgeNumTag;
1184 1195

	
1185 1196
    typedef FindArcTagIndicator<Graph> FindArcTag;
1186 1197
    Arc findArc(const Node& u, const Node& v,
1187 1198
                const Arc& prev = INVALID) const {
1188 1199
      Arc arc = Parent::findArc(u, v, prev);
1189
      while (arc != INVALID && !(*_edge_filter_map)[arc]) {
1200
      while (arc != INVALID && !(*_edge_filter)[arc]) {
1190 1201
        arc = Parent::findArc(u, v, arc);
1191 1202
      }
1192 1203
      return arc;
1193 1204
    }
1194 1205

	
1195 1206
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
1196 1207
    Edge findEdge(const Node& u, const Node& v,
1197 1208
                  const Edge& prev = INVALID) const {
1198 1209
      Edge edge = Parent::findEdge(u, v, prev);
1199
      while (edge != INVALID && !(*_edge_filter_map)[edge]) {
1210
      while (edge != INVALID && !(*_edge_filter)[edge]) {
1200 1211
        edge = Parent::findEdge(u, v, edge);
1201 1212
      }
1202 1213
      return edge;
1203 1214
    }
1204 1215

	
1205
    template <typename _Value>
1206
    class NodeMap : public SubMapExtender<Adaptor,
1207
      typename Parent::template NodeMap<_Value> > {
1216
    template <typename V>
1217
    class NodeMap 
1218
      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1219
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
1220
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1221
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent;
1222

	
1208 1223
    public:
1209
      typedef _Value Value;
1210
      typedef SubMapExtender<Adaptor, typename Parent::
1211
                             template NodeMap<Value> > MapParent;
1212

	
1213
      NodeMap(const Adaptor& adaptor)
1214
        : MapParent(adaptor) {}
1215
      NodeMap(const Adaptor& adaptor, const Value& value)
1216
        : MapParent(adaptor, value) {}
1224
      typedef V Value;
1225

	
1226
      NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor)
1227
        : Parent(adaptor) {}
1228
      NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value)
1229
        : Parent(adaptor, value) {}
1217 1230

	
1218 1231
    private:
1219 1232
      NodeMap& operator=(const NodeMap& cmap) {
1220 1233
        return operator=<NodeMap>(cmap);
1221 1234
      }
1222 1235

	
1223 1236
      template <typename CMap>
1224 1237
      NodeMap& operator=(const CMap& cmap) {
1225
        MapParent::operator=(cmap);
1238
        Parent::operator=(cmap);
1226 1239
        return *this;
1227 1240
      }
1228 1241
    };
1229 1242

	
1230
    template <typename _Value>
1231
    class ArcMap : public SubMapExtender<Adaptor,
1232
      typename Parent::template ArcMap<_Value> > {
1243
    template <typename V>
1244
    class ArcMap 
1245
      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1246
          LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
1247
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1248
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent;
1249

	
1233 1250
    public:
1234
      typedef _Value Value;
1235
      typedef SubMapExtender<Adaptor, typename Parent::
1236
                             template ArcMap<Value> > MapParent;
1237

	
1238
      ArcMap(const Adaptor& adaptor)
1239
        : MapParent(adaptor) {}
1240
      ArcMap(const Adaptor& adaptor, const Value& value)
1241
        : MapParent(adaptor, value) {}
1251
      typedef V Value;
1252

	
1253
      ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor)
1254
        : Parent(adaptor) {}
1255
      ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value)
1256
        : Parent(adaptor, value) {}
1242 1257

	
1243 1258
    private:
1244 1259
      ArcMap& operator=(const ArcMap& cmap) {
1245 1260
        return operator=<ArcMap>(cmap);
1246 1261
      }
1247 1262

	
1248 1263
      template <typename CMap>
1249 1264
      ArcMap& operator=(const CMap& cmap) {
1250
        MapParent::operator=(cmap);
1265
        Parent::operator=(cmap);
1251 1266
        return *this;
1252 1267
      }
1253 1268
    };
1254 1269

	
1255
    template <typename _Value>
1256
    class EdgeMap : public SubMapExtender<Adaptor,
1257
      typename Parent::template EdgeMap<_Value> > {
1270
    template <typename V>
1271
    class EdgeMap 
1272
      : public SubMapExtender<SubGraphBase<GR, NF, EF, false>,
1273
        LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
1274
      typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, 
1275
	LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent;
1276

	
1258 1277
    public:
1259
      typedef _Value Value;
1260
      typedef SubMapExtender<Adaptor, typename Parent::
1261
                             template EdgeMap<Value> > MapParent;
1262

	
1263
      EdgeMap(const Adaptor& adaptor)
1264
        : MapParent(adaptor) {}
1265

	
1266
      EdgeMap(const Adaptor& adaptor, const _Value& value)
1267
        : MapParent(adaptor, value) {}
1278
      typedef V Value;
1279

	
1280
      EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor)
1281
        : Parent(adaptor) {}
1282

	
1283
      EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value)
1284
        : Parent(adaptor, value) {}
1268 1285

	
1269 1286
    private:
1270 1287
      EdgeMap& operator=(const EdgeMap& cmap) {
1271 1288
        return operator=<EdgeMap>(cmap);
1272 1289
      }
1273 1290

	
1274 1291
      template <typename CMap>
1275 1292
      EdgeMap& operator=(const CMap& cmap) {
1276
        MapParent::operator=(cmap);
1293
        Parent::operator=(cmap);
1277 1294
        return *this;
1278 1295
      }
1279 1296
    };
1280 1297

	
1281 1298
  };
1282 1299

	
1283 1300
  /// \ingroup graph_adaptors
1284 1301
  ///
1285 1302
  /// \brief Adaptor class for hiding nodes and edges in an undirected
1286 1303
  /// graph.
1287 1304
  ///
1288 1305
  /// SubGraph can be used for hiding nodes and edges in a graph.
1289 1306
  /// A \c bool node map and a \c bool edge map must be specified, which
1290 1307
  /// define the filters for nodes and edges.
1291 1308
  /// Only the nodes and edges with \c true filter value are
1292 1309
  /// shown in the subgraph. The edges that are incident to hidden
1293 1310
  /// nodes are also filtered out.
1294 1311
  /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
1295 1312
  ///
1296 1313
  /// The adapted graph can also be modified through this adaptor
1297 1314
  /// by adding or removing nodes or edges, unless the \c GR template
1298 1315
  /// parameter is set to be \c const.
1299 1316
  ///
1300 1317
  /// \tparam GR The type of the adapted graph.
1301 1318
  /// It must conform to the \ref concepts::Graph "Graph" concept.
1302 1319
  /// It can also be specified to be \c const.
1303 1320
  /// \tparam NF The type of the node filter map.
1304 1321
  /// It must be a \c bool (or convertible) node map of the
1305 1322
  /// adapted graph. The default type is
1306 1323
  /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>".
1307 1324
  /// \tparam EF The type of the edge filter map.
1308 1325
  /// It must be a \c bool (or convertible) edge map of the
1309 1326
  /// adapted graph. The default type is
1310 1327
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
1311 1328
  ///
1312 1329
  /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
1313 1330
  /// adapted graph are convertible to each other.
1314 1331
  ///
1315 1332
  /// \see FilterNodes
1316 1333
  /// \see FilterEdges
1317 1334
#ifdef DOXYGEN
1318 1335
  template<typename GR, typename NF, typename EF>
1319 1336
  class SubGraph {
1320 1337
#else
1321 1338
  template<typename GR,
1322 1339
           typename NF = typename GR::template NodeMap<bool>,
1323 1340
           typename EF = typename GR::template EdgeMap<bool> >
1324 1341
  class SubGraph :
1325 1342
    public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > {
1326 1343
#endif
1327 1344
  public:
1328 1345
    /// The type of the adapted graph.
1329 1346
    typedef GR Graph;
1330 1347
    /// The type of the node filter map.
1331 1348
    typedef NF NodeFilterMap;
1332 1349
    /// The type of the edge filter map.
1333 1350
    typedef EF EdgeFilterMap;
1334 1351

	
1335
    typedef GraphAdaptorExtender< SubGraphBase<GR, NF, EF, true> >
1352
    typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> >
1336 1353
      Parent;
1337 1354

	
1338 1355
    typedef typename Parent::Node Node;
1339 1356
    typedef typename Parent::Edge Edge;
1340 1357

	
1341 1358
  protected:
1342 1359
    SubGraph() { }
1343 1360
  public:
1344 1361

	
1345 1362
    /// \brief Constructor
1346 1363
    ///
1347 1364
    /// Creates a subgraph for the given graph with the given node
1348 1365
    /// and edge filter maps.
1349
    SubGraph(Graph& graph, NodeFilterMap& node_filter_map,
1350
             EdgeFilterMap& edge_filter_map) {
1351
      setGraph(graph);
1352
      setNodeFilterMap(node_filter_map);
1353
      setEdgeFilterMap(edge_filter_map);
1366
    SubGraph(GR& graph, NF& node_filter, EF& edge_filter) {
1367
      initialize(graph, node_filter, edge_filter);
1354 1368
    }
1355 1369

	
1356 1370
    /// \brief Sets the status of the given node
1357 1371
    ///
1358 1372
    /// This function sets the status of the given node.
1359 1373
    /// It is done by simply setting the assigned value of \c n
1360 1374
    /// to \c v in the node filter map.
1361 1375
    void status(const Node& n, bool v) const { Parent::status(n, v); }
1362 1376

	
1363 1377
    /// \brief Sets the status of the given edge
1364 1378
    ///
1365 1379
    /// This function sets the status of the given edge.
1366 1380
    /// It is done by simply setting the assigned value of \c e
1367 1381
    /// to \c v in the edge filter map.
1368 1382
    void status(const Edge& e, bool v) const { Parent::status(e, v); }
1369 1383

	
1370 1384
    /// \brief Returns the status of the given node
1371 1385
    ///
1372 1386
    /// This function returns the status of the given node.
1373 1387
    /// It is \c true if the given node is enabled (i.e. not hidden).
1374 1388
    bool status(const Node& n) const { return Parent::status(n); }
1375 1389

	
1376 1390
    /// \brief Returns the status of the given edge
1377 1391
    ///
1378 1392
    /// This function returns the status of the given edge.
1379 1393
    /// It is \c true if the given edge is enabled (i.e. not hidden).
1380 1394
    bool status(const Edge& e) const { return Parent::status(e); }
1381 1395

	
1382 1396
    /// \brief Disables the given node
1383 1397
    ///
1384 1398
    /// This function disables the given node in the subdigraph,
1385 1399
    /// so the iteration jumps over it.
1386 1400
    /// It is the same as \ref status() "status(n, false)".
1387 1401
    void disable(const Node& n) const { Parent::status(n, false); }
1388 1402

	
1389 1403
    /// \brief Disables the given edge
1390 1404
    ///
1391 1405
    /// This function disables the given edge in the subgraph,
1392 1406
    /// so the iteration jumps over it.
1393 1407
    /// It is the same as \ref status() "status(e, false)".
1394 1408
    void disable(const Edge& e) const { Parent::status(e, false); }
1395 1409

	
1396 1410
    /// \brief Enables the given node
1397 1411
    ///
1398 1412
    /// This function enables the given node in the subdigraph.
1399 1413
    /// It is the same as \ref status() "status(n, true)".
1400 1414
    void enable(const Node& n) const { Parent::status(n, true); }
1401 1415

	
1402 1416
    /// \brief Enables the given edge
1403 1417
    ///
1404 1418
    /// This function enables the given edge in the subgraph.
1405 1419
    /// It is the same as \ref status() "status(e, true)".
1406 1420
    void enable(const Edge& e) const { Parent::status(e, true); }
1407 1421

	
1408 1422
  };
1409 1423

	
1410 1424
  /// \brief Returns a read-only SubGraph adaptor
1411 1425
  ///
1412 1426
  /// This function just returns a read-only \ref SubGraph adaptor.
1413 1427
  /// \ingroup graph_adaptors
1414 1428
  /// \relates SubGraph
1415 1429
  template<typename GR, typename NF, typename EF>
1416 1430
  SubGraph<const GR, NF, EF>
1417
  subGraph(const GR& graph,
1418
           NF& node_filter_map, EF& edge_filter_map) {
1431
  subGraph(const GR& graph, NF& node_filter, EF& edge_filter) {
1419 1432
    return SubGraph<const GR, NF, EF>
1420
      (graph, node_filter_map, edge_filter_map);
1433
      (graph, node_filter, edge_filter);
1421 1434
  }
1422 1435

	
1423 1436
  template<typename GR, typename NF, typename EF>
1424 1437
  SubGraph<const GR, const NF, EF>
1425
  subGraph(const GR& graph,
1426
           const NF& node_filter_map, EF& edge_filter_map) {
1438
  subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) {
1427 1439
    return SubGraph<const GR, const NF, EF>
1428
      (graph, node_filter_map, edge_filter_map);
1440
      (graph, node_filter, edge_filter);
1429 1441
  }
1430 1442

	
1431 1443
  template<typename GR, typename NF, typename EF>
1432 1444
  SubGraph<const GR, NF, const EF>
1433
  subGraph(const GR& graph,
1434
           NF& node_filter_map, const EF& edge_filter_map) {
1445
  subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) {
1435 1446
    return SubGraph<const GR, NF, const EF>
1436
      (graph, node_filter_map, edge_filter_map);
1447
      (graph, node_filter, edge_filter);
1437 1448
  }
1438 1449

	
1439 1450
  template<typename GR, typename NF, typename EF>
1440 1451
  SubGraph<const GR, const NF, const EF>
1441
  subGraph(const GR& graph,
1442
           const NF& node_filter_map, const EF& edge_filter_map) {
1452
  subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) {
1443 1453
    return SubGraph<const GR, const NF, const EF>
1444
      (graph, node_filter_map, edge_filter_map);
1454
      (graph, node_filter, edge_filter);
1445 1455
  }
1446 1456

	
1447 1457

	
1448 1458
  /// \ingroup graph_adaptors
1449 1459
  ///
1450 1460
  /// \brief Adaptor class for hiding nodes in a digraph or a graph.
1451 1461
  ///
1452 1462
  /// FilterNodes adaptor can be used for hiding nodes in a digraph or a
1453 1463
  /// graph. A \c bool node map must be specified, which defines the filter
1454 1464
  /// for the nodes. Only the nodes with \c true filter value and the
1455 1465
  /// arcs/edges incident to nodes both with \c true filter value are shown
1456 1466
  /// in the subgraph. This adaptor conforms to the \ref concepts::Digraph
1457 1467
  /// "Digraph" concept or the \ref concepts::Graph "Graph" concept
1458 1468
  /// depending on the \c GR template parameter.
1459 1469
  ///
1460 1470
  /// The adapted (di)graph can also be modified through this adaptor
1461 1471
  /// by adding or removing nodes or arcs/edges, unless the \c GR template
1462 1472
  /// parameter is set to be \c const.
1463 1473
  ///
1464 1474
  /// \tparam GR The type of the adapted digraph or graph.
1465 1475
  /// It must conform to the \ref concepts::Digraph "Digraph" concept
1466 1476
  /// or the \ref concepts::Graph "Graph" concept.
1467 1477
  /// It can also be specified to be \c const.
1468 1478
  /// \tparam NF The type of the node filter map.
1469 1479
  /// It must be a \c bool (or convertible) node map of the
1470 1480
  /// adapted (di)graph. The default type is
1471 1481
  /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>".
1472 1482
  ///
1473 1483
  /// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the
1474 1484
  /// adapted (di)graph are convertible to each other.
1475 1485
#ifdef DOXYGEN
1476 1486
  template<typename GR, typename NF>
1477 1487
  class FilterNodes {
1478 1488
#else
1479 1489
  template<typename GR,
1480 1490
           typename NF = typename GR::template NodeMap<bool>,
1481 1491
           typename Enable = void>
1482 1492
  class FilterNodes :
1483 1493
    public DigraphAdaptorExtender<
1484
      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, bool>, true> > {
1494
      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >,
1495
                     true> > {
1485 1496
#endif
1497
    typedef DigraphAdaptorExtender<
1498
      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, 
1499
                     true> > Parent;
1500

	
1486 1501
  public:
1487 1502

	
1488 1503
    typedef GR Digraph;
1489 1504
    typedef NF NodeFilterMap;
1490 1505

	
1491
    typedef DigraphAdaptorExtender<
1492
      SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, bool>, true> >
1493
      Parent;
1494

	
1495 1506
    typedef typename Parent::Node Node;
1496 1507

	
1497 1508
  protected:
1498
    ConstMap<typename Digraph::Arc, bool> const_true_map;
1499

	
1500
    FilterNodes() : const_true_map(true) {
1501
      Parent::setArcFilterMap(const_true_map);
1502
    }
1509
    ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map;
1510

	
1511
    FilterNodes() : const_true_map() {}
1503 1512

	
1504 1513
  public:
1505 1514

	
1506 1515
    /// \brief Constructor
1507 1516
    ///
1508 1517
    /// Creates a subgraph for the given digraph or graph with the
1509 1518
    /// given node filter map.
1510
    FilterNodes(GR& graph, NodeFilterMap& node_filter) :
1511
      Parent(), const_true_map(true)
1519
    FilterNodes(GR& graph, NF& node_filter) 
1520
      : Parent(), const_true_map()
1512 1521
    {
1513
      Parent::setDigraph(graph);
1514
      Parent::setNodeFilterMap(node_filter);
1515
      Parent::setArcFilterMap(const_true_map);
1522
      Parent::initialize(graph, node_filter, const_true_map);
1516 1523
    }
1517 1524

	
1518 1525
    /// \brief Sets the status of the given node
1519 1526
    ///
1520 1527
    /// This function sets the status of the given node.
1521 1528
    /// It is done by simply setting the assigned value of \c n
1522 1529
    /// to \c v in the node filter map.
1523 1530
    void status(const Node& n, bool v) const { Parent::status(n, v); }
1524 1531

	
1525 1532
    /// \brief Returns the status of the given node
1526 1533
    ///
1527 1534
    /// This function returns the status of the given node.
1528 1535
    /// It is \c true if the given node is enabled (i.e. not hidden).
1529 1536
    bool status(const Node& n) const { return Parent::status(n); }
1530 1537

	
1531 1538
    /// \brief Disables the given node
1532 1539
    ///
1533 1540
    /// This function disables the given node, so the iteration
1534 1541
    /// jumps over it.
1535 1542
    /// It is the same as \ref status() "status(n, false)".
1536 1543
    void disable(const Node& n) const { Parent::status(n, false); }
1537 1544

	
1538 1545
    /// \brief Enables the given node
1539 1546
    ///
1540 1547
    /// This function enables the given node.
1541 1548
    /// It is the same as \ref status() "status(n, true)".
1542 1549
    void enable(const Node& n) const { Parent::status(n, true); }
1543 1550

	
1544 1551
  };
1545 1552

	
1546 1553
  template<typename GR, typename NF>
1547 1554
  class FilterNodes<GR, NF,
1548 1555
                    typename enable_if<UndirectedTagIndicator<GR> >::type> :
1549 1556
    public GraphAdaptorExtender<
1550
      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, bool>, true> > {
1557
      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, 
1558
                   true> > {
1559

	
1560
    typedef GraphAdaptorExtender<
1561
      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, 
1562
                   true> > Parent;
1551 1563

	
1552 1564
  public:
1565

	
1553 1566
    typedef GR Graph;
1554 1567
    typedef NF NodeFilterMap;
1555
    typedef GraphAdaptorExtender<
1556
      SubGraphBase<GR, NF, ConstMap<typename GR::Edge, bool>, true> >
1557
      Parent;
1558 1568

	
1559 1569
    typedef typename Parent::Node Node;
1570

	
1560 1571
  protected:
1561
    ConstMap<typename Graph::Edge, bool> const_true_map;
1562

	
1563
    FilterNodes() : const_true_map(true) {
1564
      Parent::setEdgeFilterMap(const_true_map);
1565
    }
1572
    ConstMap<typename GR::Edge, Const<bool, true> > const_true_map;
1573

	
1574
    FilterNodes() : const_true_map() {}
1566 1575

	
1567 1576
  public:
1568 1577

	
1569
    FilterNodes(Graph& _graph, NodeFilterMap& node_filter_map) :
1570
      Parent(), const_true_map(true) {
1571
      Parent::setGraph(_graph);
1572
      Parent::setNodeFilterMap(node_filter_map);
1573
      Parent::setEdgeFilterMap(const_true_map);
1578
    FilterNodes(GR& graph, NodeFilterMap& node_filter) :
1579
      Parent(), const_true_map() {
1580
      Parent::initialize(graph, node_filter, const_true_map);
1574 1581
    }
1575 1582

	
1576 1583
    void status(const Node& n, bool v) const { Parent::status(n, v); }
1577 1584
    bool status(const Node& n) const { return Parent::status(n); }
1578 1585
    void disable(const Node& n) const { Parent::status(n, false); }
1579 1586
    void enable(const Node& n) const { Parent::status(n, true); }
1580 1587

	
1581 1588
  };
1582 1589

	
1583 1590

	
1584 1591
  /// \brief Returns a read-only FilterNodes adaptor
1585 1592
  ///
1586 1593
  /// This function just returns a read-only \ref FilterNodes adaptor.
1587 1594
  /// \ingroup graph_adaptors
1588 1595
  /// \relates FilterNodes
1589 1596
  template<typename GR, typename NF>
1590 1597
  FilterNodes<const GR, NF>
1591
  filterNodes(const GR& graph, NF& node_filter_map) {
1592
    return FilterNodes<const GR, NF>(graph, node_filter_map);
1598
  filterNodes(const GR& graph, NF& node_filter) {
1599
    return FilterNodes<const GR, NF>(graph, node_filter);
1593 1600
  }
1594 1601

	
1595 1602
  template<typename GR, typename NF>
1596 1603
  FilterNodes<const GR, const NF>
1597
  filterNodes(const GR& graph, const NF& node_filter_map) {
1598
    return FilterNodes<const GR, const NF>(graph, node_filter_map);
1604
  filterNodes(const GR& graph, const NF& node_filter) {
1605
    return FilterNodes<const GR, const NF>(graph, node_filter);
1599 1606
  }
1600 1607

	
1601 1608
  /// \ingroup graph_adaptors
1602 1609
  ///
1603 1610
  /// \brief Adaptor class for hiding arcs in a digraph.
1604 1611
  ///
1605 1612
  /// FilterArcs adaptor can be used for hiding arcs in a digraph.
1606 1613
  /// A \c bool arc map must be specified, which defines the filter for
1607 1614
  /// the arcs. Only the arcs with \c true filter value are shown in the
1608 1615
  /// subdigraph. This adaptor conforms to the \ref concepts::Digraph
1609 1616
  /// "Digraph" concept.
1610 1617
  ///
1611 1618
  /// The adapted digraph can also be modified through this adaptor
1612 1619
  /// by adding or removing nodes or arcs, unless the \c GR template
1613 1620
  /// parameter is set to be \c const.
1614 1621
  ///
1615
  /// \tparam GR The type of the adapted digraph.
1622
  /// \tparam DGR The type of the adapted digraph.
1616 1623
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
1617 1624
  /// It can also be specified to be \c const.
1618 1625
  /// \tparam AF The type of the arc filter map.
1619 1626
  /// It must be a \c bool (or convertible) arc map of the
1620 1627
  /// adapted digraph. The default type is
1621
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<bool>".
1628
  /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>".
1622 1629
  ///
1623 1630
  /// \note The \c Node and \c Arc types of this adaptor and the adapted
1624 1631
  /// digraph are convertible to each other.
1625 1632
#ifdef DOXYGEN
1626
  template<typename GR,
1633
  template<typename DGR,
1627 1634
           typename AF>
1628 1635
  class FilterArcs {
1629 1636
#else
1630
  template<typename GR,
1631
           typename AF = typename GR::template ArcMap<bool> >
1637
  template<typename DGR,
1638
           typename AF = typename DGR::template ArcMap<bool> >
1632 1639
  class FilterArcs :
1633 1640
    public DigraphAdaptorExtender<
1634
      SubDigraphBase<GR, ConstMap<typename GR::Node, bool>, AF, false> > {
1641
      SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >,
1642
                     AF, false> > {
1635 1643
#endif
1644
    typedef DigraphAdaptorExtender<
1645
      SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, 
1646
                     AF, false> > Parent;
1647

	
1636 1648
  public:
1649

	
1637 1650
    /// The type of the adapted digraph.
1638
    typedef GR Digraph;
1651
    typedef DGR Digraph;
1639 1652
    /// The type of the arc filter map.
1640 1653
    typedef AF ArcFilterMap;
1641 1654

	
1642
    typedef DigraphAdaptorExtender<
1643
      SubDigraphBase<GR, ConstMap<typename GR::Node, bool>, AF, false> >
1644
      Parent;
1645

	
1646 1655
    typedef typename Parent::Arc Arc;
1647 1656

	
1648 1657
  protected:
1649
    ConstMap<typename Digraph::Node, bool> const_true_map;
1650

	
1651
    FilterArcs() : const_true_map(true) {
1652
      Parent::setNodeFilterMap(const_true_map);
1653
    }
1658
    ConstMap<typename DGR::Node, Const<bool, true> > const_true_map;
1659

	
1660
    FilterArcs() : const_true_map() {}
1654 1661

	
1655 1662
  public:
1656 1663

	
1657 1664
    /// \brief Constructor
1658 1665
    ///
1659 1666
    /// Creates a subdigraph for the given digraph with the given arc
1660 1667
    /// filter map.
1661
    FilterArcs(Digraph& digraph, ArcFilterMap& arc_filter)
1662
      : Parent(), const_true_map(true) {
1663
      Parent::setDigraph(digraph);
1664
      Parent::setNodeFilterMap(const_true_map);
1665
      Parent::setArcFilterMap(arc_filter);
1668
    FilterArcs(DGR& digraph, ArcFilterMap& arc_filter)
1669
      : Parent(), const_true_map() {
1670
      Parent::initialize(digraph, const_true_map, arc_filter);
1666 1671
    }
1667 1672

	
1668 1673
    /// \brief Sets the status of the given arc
1669 1674
    ///
1670 1675
    /// This function sets the status of the given arc.
1671 1676
    /// It is done by simply setting the assigned value of \c a
1672 1677
    /// to \c v in the arc filter map.
1673 1678
    void status(const Arc& a, bool v) const { Parent::status(a, v); }
1674 1679

	
1675 1680
    /// \brief Returns the status of the given arc
1676 1681
    ///
1677 1682
    /// This function returns the status of the given arc.
1678 1683
    /// It is \c true if the given arc is enabled (i.e. not hidden).
1679 1684
    bool status(const Arc& a) const { return Parent::status(a); }
1680 1685

	
1681 1686
    /// \brief Disables the given arc
1682 1687
    ///
1683 1688
    /// This function disables the given arc in the subdigraph,
1684 1689
    /// so the iteration jumps over it.
1685 1690
    /// It is the same as \ref status() "status(a, false)".
1686 1691
    void disable(const Arc& a) const { Parent::status(a, false); }
1687 1692

	
1688 1693
    /// \brief Enables the given arc
1689 1694
    ///
1690 1695
    /// This function enables the given arc in the subdigraph.
1691 1696
    /// It is the same as \ref status() "status(a, true)".
1692 1697
    void enable(const Arc& a) const { Parent::status(a, true); }
1693 1698

	
1694 1699
  };
1695 1700

	
1696 1701
  /// \brief Returns a read-only FilterArcs adaptor
1697 1702
  ///
1698 1703
  /// This function just returns a read-only \ref FilterArcs adaptor.
1699 1704
  /// \ingroup graph_adaptors
1700 1705
  /// \relates FilterArcs
1701
  template<typename GR, typename AF>
1702
  FilterArcs<const GR, AF>
1703
  filterArcs(const GR& digraph, AF& arc_filter_map) {
1704
    return FilterArcs<const GR, AF>(digraph, arc_filter_map);
1706
  template<typename DGR, typename AF>
1707
  FilterArcs<const DGR, AF>
1708
  filterArcs(const DGR& digraph, AF& arc_filter) {
1709
    return FilterArcs<const DGR, AF>(digraph, arc_filter);
1705 1710
  }
1706 1711

	
1707
  template<typename GR, typename AF>
1708
  FilterArcs<const GR, const AF>
1709
  filterArcs(const GR& digraph, const AF& arc_filter_map) {
1710
    return FilterArcs<const GR, const AF>(digraph, arc_filter_map);
1712
  template<typename DGR, typename AF>
1713
  FilterArcs<const DGR, const AF>
1714
  filterArcs(const DGR& digraph, const AF& arc_filter) {
1715
    return FilterArcs<const DGR, const AF>(digraph, arc_filter);
1711 1716
  }
1712 1717

	
1713 1718
  /// \ingroup graph_adaptors
1714 1719
  ///
1715 1720
  /// \brief Adaptor class for hiding edges in a graph.
1716 1721
  ///
1717 1722
  /// FilterEdges adaptor can be used for hiding edges in a graph.
1718 1723
  /// A \c bool edge map must be specified, which defines the filter for
1719 1724
  /// the edges. Only the edges with \c true filter value are shown in the
1720 1725
  /// subgraph. This adaptor conforms to the \ref concepts::Graph
1721 1726
  /// "Graph" concept.
1722 1727
  ///
1723 1728
  /// The adapted graph can also be modified through this adaptor
1724 1729
  /// by adding or removing nodes or edges, unless the \c GR template
1725 1730
  /// parameter is set to be \c const.
1726 1731
  ///
1727 1732
  /// \tparam GR The type of the adapted graph.
1728 1733
  /// It must conform to the \ref concepts::Graph "Graph" concept.
1729 1734
  /// It can also be specified to be \c const.
1730 1735
  /// \tparam EF The type of the edge filter map.
1731 1736
  /// It must be a \c bool (or convertible) edge map of the
1732 1737
  /// adapted graph. The default type is
1733 1738
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
1734 1739
  ///
1735 1740
  /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the
1736 1741
  /// adapted graph are convertible to each other.
1737 1742
#ifdef DOXYGEN
1738 1743
  template<typename GR,
1739 1744
           typename EF>
1740 1745
  class FilterEdges {
1741 1746
#else
1742 1747
  template<typename GR,
1743 1748
           typename EF = typename GR::template EdgeMap<bool> >
1744 1749
  class FilterEdges :
1745 1750
    public GraphAdaptorExtender<
1746
      SubGraphBase<GR, ConstMap<typename GR::Node,bool>, EF, false> > {
1751
      SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, 
1752
                   EF, false> > {
1747 1753
#endif
1754
    typedef GraphAdaptorExtender<
1755
      SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, 
1756
                   EF, false> > Parent;
1757

	
1748 1758
  public:
1759

	
1749 1760
    /// The type of the adapted graph.
1750 1761
    typedef GR Graph;
1751 1762
    /// The type of the edge filter map.
1752 1763
    typedef EF EdgeFilterMap;
1753 1764

	
1754
    typedef GraphAdaptorExtender<
1755
      SubGraphBase<GR, ConstMap<typename GR::Node,bool>, EF, false> >
1756
      Parent;
1757

	
1758 1765
    typedef typename Parent::Edge Edge;
1759 1766

	
1760 1767
  protected:
1761
    ConstMap<typename Graph::Node, bool> const_true_map;
1768
    ConstMap<typename GR::Node, Const<bool, true> > const_true_map;
1762 1769

	
1763 1770
    FilterEdges() : const_true_map(true) {
1764 1771
      Parent::setNodeFilterMap(const_true_map);
1765 1772
    }
1766 1773

	
1767 1774
  public:
1768 1775

	
1769 1776
    /// \brief Constructor
1770 1777
    ///
1771 1778
    /// Creates a subgraph for the given graph with the given edge
1772 1779
    /// filter map.
1773
    FilterEdges(Graph& graph, EdgeFilterMap& edge_filter_map) :
1774
      Parent(), const_true_map(true) {
1775
      Parent::setGraph(graph);
1776
      Parent::setNodeFilterMap(const_true_map);
1777
      Parent::setEdgeFilterMap(edge_filter_map);
1780
    FilterEdges(GR& graph, EF& edge_filter) 
1781
      : Parent(), const_true_map() {
1782
      Parent::initialize(graph, const_true_map, edge_filter);
1778 1783
    }
1779 1784

	
1780 1785
    /// \brief Sets the status of the given edge
1781 1786
    ///
1782 1787
    /// This function sets the status of the given edge.
1783 1788
    /// It is done by simply setting the assigned value of \c e
1784 1789
    /// to \c v in the edge filter map.
1785 1790
    void status(const Edge& e, bool v) const { Parent::status(e, v); }
1786 1791

	
1787 1792
    /// \brief Returns the status of the given edge
1788 1793
    ///
1789 1794
    /// This function returns the status of the given edge.
1790 1795
    /// It is \c true if the given edge is enabled (i.e. not hidden).
1791 1796
    bool status(const Edge& e) const { return Parent::status(e); }
1792 1797

	
1793 1798
    /// \brief Disables the given edge
1794 1799
    ///
1795 1800
    /// This function disables the given edge in the subgraph,
1796 1801
    /// so the iteration jumps over it.
1797 1802
    /// It is the same as \ref status() "status(e, false)".
1798 1803
    void disable(const Edge& e) const { Parent::status(e, false); }
1799 1804

	
1800 1805
    /// \brief Enables the given edge
1801 1806
    ///
1802 1807
    /// This function enables the given edge in the subgraph.
1803 1808
    /// It is the same as \ref status() "status(e, true)".
1804 1809
    void enable(const Edge& e) const { Parent::status(e, true); }
1805 1810

	
1806 1811
  };
1807 1812

	
1808 1813
  /// \brief Returns a read-only FilterEdges adaptor
1809 1814
  ///
1810 1815
  /// This function just returns a read-only \ref FilterEdges adaptor.
1811 1816
  /// \ingroup graph_adaptors
1812 1817
  /// \relates FilterEdges
1813 1818
  template<typename GR, typename EF>
1814 1819
  FilterEdges<const GR, EF>
1815
  filterEdges(const GR& graph, EF& edge_filter_map) {
1816
    return FilterEdges<const GR, EF>(graph, edge_filter_map);
1820
  filterEdges(const GR& graph, EF& edge_filter) {
1821
    return FilterEdges<const GR, EF>(graph, edge_filter);
1817 1822
  }
1818 1823

	
1819 1824
  template<typename GR, typename EF>
1820 1825
  FilterEdges<const GR, const EF>
1821
  filterEdges(const GR& graph, const EF& edge_filter_map) {
1822
    return FilterEdges<const GR, const EF>(graph, edge_filter_map);
1826
  filterEdges(const GR& graph, const EF& edge_filter) {
1827
    return FilterEdges<const GR, const EF>(graph, edge_filter);
1823 1828
  }
1824 1829

	
1825 1830

	
1826
  template <typename _Digraph>
1831
  template <typename DGR>
1827 1832
  class UndirectorBase {
1828 1833
  public:
1829
    typedef _Digraph Digraph;
1834
    typedef DGR Digraph;
1830 1835
    typedef UndirectorBase Adaptor;
1831 1836

	
1832 1837
    typedef True UndirectedTag;
1833 1838

	
1834 1839
    typedef typename Digraph::Arc Edge;
1835 1840
    typedef typename Digraph::Node Node;
1836 1841

	
1837
    class Arc : public Edge {
1842
    class Arc {
1838 1843
      friend class UndirectorBase;
1839 1844
    protected:
1845
      Edge _edge;
1840 1846
      bool _forward;
1841 1847

	
1842
      Arc(const Edge& edge, bool forward) :
1843
        Edge(edge), _forward(forward) {}
1848
      Arc(const Edge& edge, bool forward) 
1849
        : _edge(edge), _forward(forward) {}
1844 1850

	
1845 1851
    public:
1846 1852
      Arc() {}
1847 1853

	
1848
      Arc(Invalid) : Edge(INVALID), _forward(true) {}
1854
      Arc(Invalid) : _edge(INVALID), _forward(true) {}
1855

	
1856
      operator const Edge&() const { return _edge; }
1849 1857

	
1850 1858
      bool operator==(const Arc &other) const {
1851
        return _forward == other._forward &&
1852
          static_cast<const Edge&>(*this) == static_cast<const Edge&>(other);
1859
        return _forward == other._forward && _edge == other._edge;
1853 1860
      }
1854 1861
      bool operator!=(const Arc &other) const {
1855
        return _forward != other._forward ||
1856
          static_cast<const Edge&>(*this) != static_cast<const Edge&>(other);
1862
        return _forward != other._forward || _edge != other._edge;
1857 1863
      }
1858 1864
      bool operator<(const Arc &other) const {
1859 1865
        return _forward < other._forward ||
1860
          (_forward == other._forward &&
1861
           static_cast<const Edge&>(*this) < static_cast<const Edge&>(other));
1866
          (_forward == other._forward && _edge < other._edge);
1862 1867
      }
1863 1868
    };
1864 1869

	
1865 1870
    void first(Node& n) const {
1866 1871
      _digraph->first(n);
1867 1872
    }
1868 1873

	
1869 1874
    void next(Node& n) const {
1870 1875
      _digraph->next(n);
1871 1876
    }
1872 1877

	
1873 1878
    void first(Arc& a) const {
1874
      _digraph->first(a);
1879
      _digraph->first(a._edge);
1875 1880
      a._forward = true;
1876 1881
    }
1877 1882

	
1878 1883
    void next(Arc& a) const {
1879 1884
      if (a._forward) {
1880 1885
        a._forward = false;
1881 1886
      } else {
1882
        _digraph->next(a);
1887
        _digraph->next(a._edge);
1883 1888
        a._forward = true;
1884 1889
      }
1885 1890
    }
1886 1891

	
1887 1892
    void first(Edge& e) const {
1888 1893
      _digraph->first(e);
1889 1894
    }
1890 1895

	
1891 1896
    void next(Edge& e) const {
1892 1897
      _digraph->next(e);
1893 1898
    }
1894 1899

	
1895 1900
    void firstOut(Arc& a, const Node& n) const {
1896
      _digraph->firstIn(a, n);
1897
      if( static_cast<const Edge&>(a) != INVALID ) {
1901
      _digraph->firstIn(a._edge, n);
1902
      if (a._edge != INVALID ) {
1898 1903
        a._forward = false;
1899 1904
      } else {
1900
        _digraph->firstOut(a, n);
1905
        _digraph->firstOut(a._edge, n);
1901 1906
        a._forward = true;
1902 1907
      }
1903 1908
    }
1904 1909
    void nextOut(Arc &a) const {
1905 1910
      if (!a._forward) {
1906
        Node n = _digraph->target(a);
1907
        _digraph->nextIn(a);
1908
        if (static_cast<const Edge&>(a) == INVALID ) {
1909
          _digraph->firstOut(a, n);
1911
        Node n = _digraph->target(a._edge);
1912
        _digraph->nextIn(a._edge);
1913
        if (a._edge == INVALID) {
1914
          _digraph->firstOut(a._edge, n);
1910 1915
          a._forward = true;
1911 1916
        }
1912 1917
      }
1913 1918
      else {
1914
        _digraph->nextOut(a);
1919
        _digraph->nextOut(a._edge);
1915 1920
      }
1916 1921
    }
1917 1922

	
1918 1923
    void firstIn(Arc &a, const Node &n) const {
1919
      _digraph->firstOut(a, n);
1920
      if (static_cast<const Edge&>(a) != INVALID ) {
1924
      _digraph->firstOut(a._edge, n);
1925
      if (a._edge != INVALID ) {
1921 1926
        a._forward = false;
1922 1927
      } else {
1923
        _digraph->firstIn(a, n);
1928
        _digraph->firstIn(a._edge, n);
1924 1929
        a._forward = true;
1925 1930
      }
1926 1931
    }
1927 1932
    void nextIn(Arc &a) const {
1928 1933
      if (!a._forward) {
1929
        Node n = _digraph->source(a);
1930
        _digraph->nextOut(a);
1931
        if( static_cast<const Edge&>(a) == INVALID ) {
1932
          _digraph->firstIn(a, n);
1934
        Node n = _digraph->source(a._edge);
1935
        _digraph->nextOut(a._edge);
1936
        if (a._edge == INVALID ) {
1937
          _digraph->firstIn(a._edge, n);
1933 1938
          a._forward = true;
1934 1939
        }
1935 1940
      }
1936 1941
      else {
1937
        _digraph->nextIn(a);
1942
        _digraph->nextIn(a._edge);
1938 1943
      }
1939 1944
    }
1940 1945

	
1941 1946
    void firstInc(Edge &e, bool &d, const Node &n) const {
1942 1947
      d = true;
1943 1948
      _digraph->firstOut(e, n);
1944 1949
      if (e != INVALID) return;
1945 1950
      d = false;
1946 1951
      _digraph->firstIn(e, n);
1947 1952
    }
1948 1953

	
1949 1954
    void nextInc(Edge &e, bool &d) const {
1950 1955
      if (d) {
1951 1956
        Node s = _digraph->source(e);
1952 1957
        _digraph->nextOut(e);
1953 1958
        if (e != INVALID) return;
1954 1959
        d = false;
1955 1960
        _digraph->firstIn(e, s);
1956 1961
      } else {
1957 1962
        _digraph->nextIn(e);
1958 1963
      }
1959 1964
    }
1960 1965

	
1961 1966
    Node u(const Edge& e) const {
1962 1967
      return _digraph->source(e);
1963 1968
    }
1964 1969

	
1965 1970
    Node v(const Edge& e) const {
1966 1971
      return _digraph->target(e);
1967 1972
    }
1968 1973

	
1969 1974
    Node source(const Arc &a) const {
1970
      return a._forward ? _digraph->source(a) : _digraph->target(a);
1975
      return a._forward ? _digraph->source(a._edge) : _digraph->target(a._edge);
1971 1976
    }
1972 1977

	
1973 1978
    Node target(const Arc &a) const {
1974
      return a._forward ? _digraph->target(a) : _digraph->source(a);
1979
      return a._forward ? _digraph->target(a._edge) : _digraph->source(a._edge);
1975 1980
    }
1976 1981

	
1977 1982
    static Arc direct(const Edge &e, bool d) {
1978 1983
      return Arc(e, d);
1979 1984
    }
1980
    Arc direct(const Edge &e, const Node& n) const {
1981
      return Arc(e, _digraph->source(e) == n);
1982
    }
1983 1985

	
1984 1986
    static bool direction(const Arc &a) { return a._forward; }
1985 1987

	
1986 1988
    Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
1987 1989
    Arc arcFromId(int ix) const {
1988 1990
      return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1));
1989 1991
    }
1990 1992
    Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); }
1991 1993

	
1992 1994
    int id(const Node &n) const { return _digraph->id(n); }
1993 1995
    int id(const Arc &a) const {
1994 1996
      return  (_digraph->id(a) << 1) | (a._forward ? 1 : 0);
1995 1997
    }
1996 1998
    int id(const Edge &e) const { return _digraph->id(e); }
1997 1999

	
1998 2000
    int maxNodeId() const { return _digraph->maxNodeId(); }
1999 2001
    int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; }
2000 2002
    int maxEdgeId() const { return _digraph->maxArcId(); }
2001 2003

	
2002 2004
    Node addNode() { return _digraph->addNode(); }
2003 2005
    Edge addEdge(const Node& u, const Node& v) {
2004 2006
      return _digraph->addArc(u, v);
2005 2007
    }
2006 2008

	
2007 2009
    void erase(const Node& i) { _digraph->erase(i); }
2008 2010
    void erase(const Edge& i) { _digraph->erase(i); }
2009 2011

	
2010 2012
    void clear() { _digraph->clear(); }
2011 2013

	
2012 2014
    typedef NodeNumTagIndicator<Digraph> NodeNumTag;
2013 2015
    int nodeNum() const { return _digraph->nodeNum(); }
2014 2016

	
2015 2017
    typedef ArcNumTagIndicator<Digraph> ArcNumTag;
2016 2018
    int arcNum() const { return 2 * _digraph->arcNum(); }
2017 2019

	
2018 2020
    typedef ArcNumTag EdgeNumTag;
2019 2021
    int edgeNum() const { return _digraph->arcNum(); }
2020 2022

	
2021 2023
    typedef FindArcTagIndicator<Digraph> FindArcTag;
2022 2024
    Arc findArc(Node s, Node t, Arc p = INVALID) const {
2023 2025
      if (p == INVALID) {
2024 2026
        Edge arc = _digraph->findArc(s, t);
2025 2027
        if (arc != INVALID) return direct(arc, true);
2026 2028
        arc = _digraph->findArc(t, s);
2027 2029
        if (arc != INVALID) return direct(arc, false);
2028 2030
      } else if (direction(p)) {
2029 2031
        Edge arc = _digraph->findArc(s, t, p);
2030 2032
        if (arc != INVALID) return direct(arc, true);
2031 2033
        arc = _digraph->findArc(t, s);
2032 2034
        if (arc != INVALID) return direct(arc, false);
2033 2035
      } else {
2034 2036
        Edge arc = _digraph->findArc(t, s, p);
2035 2037
        if (arc != INVALID) return direct(arc, false);
2036 2038
      }
2037 2039
      return INVALID;
2038 2040
    }
2039 2041

	
2040 2042
    typedef FindArcTag FindEdgeTag;
2041 2043
    Edge findEdge(Node s, Node t, Edge p = INVALID) const {
2042 2044
      if (s != t) {
2043 2045
        if (p == INVALID) {
2044 2046
          Edge arc = _digraph->findArc(s, t);
2045 2047
          if (arc != INVALID) return arc;
2046 2048
          arc = _digraph->findArc(t, s);
2047 2049
          if (arc != INVALID) return arc;
2048 2050
        } else if (_digraph->source(p) == s) {
2049 2051
          Edge arc = _digraph->findArc(s, t, p);
2050 2052
          if (arc != INVALID) return arc;
2051 2053
          arc = _digraph->findArc(t, s);
2052 2054
          if (arc != INVALID) return arc;
2053 2055
        } else {
2054 2056
          Edge arc = _digraph->findArc(t, s, p);
2055 2057
          if (arc != INVALID) return arc;
2056 2058
        }
2057 2059
      } else {
2058 2060
        return _digraph->findArc(s, t, p);
2059 2061
      }
2060 2062
      return INVALID;
2061 2063
    }
2062 2064

	
2063 2065
  private:
2064 2066

	
2065
    template <typename _Value>
2067
    template <typename V>
2066 2068
    class ArcMapBase {
2067 2069
    private:
2068 2070

	
2069
      typedef typename Digraph::template ArcMap<_Value> MapImpl;
2071
      typedef typename DGR::template ArcMap<V> MapImpl;
2070 2072

	
2071 2073
    public:
2072 2074

	
2073 2075
      typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
2074 2076

	
2075
      typedef _Value Value;
2077
      typedef V Value;
2076 2078
      typedef Arc Key;
2077 2079
      typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue;
2078 2080
      typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue;
2079 2081
      typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference;
2080 2082
      typedef typename MapTraits<MapImpl>::ReturnValue Reference;
2081 2083

	
2082
      ArcMapBase(const Adaptor& adaptor) :
2084
      ArcMapBase(const UndirectorBase<DGR>& adaptor) :
2083 2085
        _forward(*adaptor._digraph), _backward(*adaptor._digraph) {}
2084 2086

	
2085
      ArcMapBase(const Adaptor& adaptor, const Value& v)
2086
        : _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {}
2087

	
2088
      void set(const Arc& a, const Value& v) {
2087
      ArcMapBase(const UndirectorBase<DGR>& adaptor, const V& value)
2088
        : _forward(*adaptor._digraph, value), 
2089
          _backward(*adaptor._digraph, value) {}
2090

	
2091
      void set(const Arc& a, const V& value) {
2089 2092
        if (direction(a)) {
2090
          _forward.set(a, v);
2093
          _forward.set(a, value);
2091 2094
        } else {
2092
          _backward.set(a, v);
2095
          _backward.set(a, value);
2093 2096
        }
2094 2097
      }
2095 2098

	
2096 2099
      ConstReturnValue operator[](const Arc& a) const {
2097 2100
        if (direction(a)) {
2098 2101
          return _forward[a];
2099 2102
        } else {
2100 2103
          return _backward[a];
2101 2104
        }
2102 2105
      }
2103 2106

	
2104 2107
      ReturnValue operator[](const Arc& a) {
2105 2108
        if (direction(a)) {
2106 2109
          return _forward[a];
2107 2110
        } else {
2108 2111
          return _backward[a];
2109 2112
        }
2110 2113
      }
2111 2114

	
2112 2115
    protected:
2113 2116

	
2114 2117
      MapImpl _forward, _backward;
2115 2118

	
2116 2119
    };
2117 2120

	
2118 2121
  public:
2119 2122

	
2120
    template <typename _Value>
2121
    class NodeMap : public Digraph::template NodeMap<_Value> {
2123
    template <typename V>
2124
    class NodeMap : public DGR::template NodeMap<V> {
2125
      typedef typename DGR::template NodeMap<V> Parent;
2126

	
2122 2127
    public:
2123

	
2124
      typedef _Value Value;
2125
      typedef typename Digraph::template NodeMap<Value> Parent;
2126

	
2127
      explicit NodeMap(const Adaptor& adaptor)
2128
      typedef V Value;
2129

	
2130
      explicit NodeMap(const UndirectorBase<DGR>& adaptor)
2128 2131
        : Parent(*adaptor._digraph) {}
2129 2132

	
2130
      NodeMap(const Adaptor& adaptor, const _Value& value)
2133
      NodeMap(const UndirectorBase<DGR>& adaptor, const V& value)
2131 2134
        : Parent(*adaptor._digraph, value) { }
2132 2135

	
2133 2136
    private:
2134 2137
      NodeMap& operator=(const NodeMap& cmap) {
2135 2138
        return operator=<NodeMap>(cmap);
2136 2139
      }
2137 2140

	
2138 2141
      template <typename CMap>
2139 2142
      NodeMap& operator=(const CMap& cmap) {
2140 2143
        Parent::operator=(cmap);
2141 2144
        return *this;
2142 2145
      }
2143 2146

	
2144 2147
    };
2145 2148

	
2146
    template <typename _Value>
2149
    template <typename V>
2147 2150
    class ArcMap
2148
      : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
2149
    {
2151
      : public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > {
2152
      typedef SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > Parent;
2153

	
2150 2154
    public:
2151
      typedef _Value Value;
2152
      typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
2153

	
2154
      explicit ArcMap(const Adaptor& adaptor)
2155
      typedef V Value;
2156

	
2157
      explicit ArcMap(const UndirectorBase<DGR>& adaptor)
2155 2158
        : Parent(adaptor) {}
2156 2159

	
2157
      ArcMap(const Adaptor& adaptor, const Value& value)
2160
      ArcMap(const UndirectorBase<DGR>& adaptor, const V& value)
2158 2161
        : Parent(adaptor, value) {}
2159 2162

	
2160 2163
    private:
2161 2164
      ArcMap& operator=(const ArcMap& cmap) {
2162 2165
        return operator=<ArcMap>(cmap);
2163 2166
      }
2164 2167

	
2165 2168
      template <typename CMap>
2166 2169
      ArcMap& operator=(const CMap& cmap) {
2167 2170
        Parent::operator=(cmap);
2168 2171
        return *this;
2169 2172
      }
2170 2173
    };
2171 2174

	
2172
    template <typename _Value>
2173
    class EdgeMap : public Digraph::template ArcMap<_Value> {
2175
    template <typename V>
2176
    class EdgeMap : public Digraph::template ArcMap<V> {
2177
      typedef typename Digraph::template ArcMap<V> Parent;
2178

	
2174 2179
    public:
2175

	
2176
      typedef _Value Value;
2177
      typedef typename Digraph::template ArcMap<Value> Parent;
2178

	
2179
      explicit EdgeMap(const Adaptor& adaptor)
2180
      typedef V Value;
2181

	
2182
      explicit EdgeMap(const UndirectorBase<DGR>& adaptor)
2180 2183
        : Parent(*adaptor._digraph) {}
2181 2184

	
2182
      EdgeMap(const Adaptor& adaptor, const Value& value)
2185
      EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value)
2183 2186
        : Parent(*adaptor._digraph, value) {}
2184 2187

	
2185 2188
    private:
2186 2189
      EdgeMap& operator=(const EdgeMap& cmap) {
2187 2190
        return operator=<EdgeMap>(cmap);
2188 2191
      }
2189 2192

	
2190 2193
      template <typename CMap>
2191 2194
      EdgeMap& operator=(const CMap& cmap) {
2192 2195
        Parent::operator=(cmap);
2193 2196
        return *this;
2194 2197
      }
2195 2198

	
2196 2199
    };
2197 2200

	
2198
    typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier;
2201
    typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier;
2199 2202
    NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
2200 2203

	
2201
    typedef typename ItemSetTraits<Digraph, Edge>::ItemNotifier EdgeNotifier;
2204
    typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier;
2202 2205
    EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); }
2206
    
2207
    typedef EdgeNotifier ArcNotifier;
2208
    ArcNotifier& notifier(Arc) const { return _digraph->notifier(Edge()); }
2203 2209

	
2204 2210
  protected:
2205 2211

	
2206 2212
    UndirectorBase() : _digraph(0) {}
2207 2213

	
2208
    Digraph* _digraph;
2209

	
2210
    void setDigraph(Digraph& digraph) {
2214
    DGR* _digraph;
2215

	
2216
    void initialize(DGR& digraph) {
2211 2217
      _digraph = &digraph;
2212 2218
    }
2213 2219

	
2214 2220
  };
2215 2221

	
2216 2222
  /// \ingroup graph_adaptors
2217 2223
  ///
2218 2224
  /// \brief Adaptor class for viewing a digraph as an undirected graph.
2219 2225
  ///
2220 2226
  /// Undirector adaptor can be used for viewing a digraph as an undirected
2221 2227
  /// graph. All arcs of the underlying digraph are showed in the
2222 2228
  /// adaptor as an edge (and also as a pair of arcs, of course).
2223 2229
  /// This adaptor conforms to the \ref concepts::Graph "Graph" concept.
2224 2230
  ///
2225 2231
  /// The adapted digraph can also be modified through this adaptor
2226 2232
  /// by adding or removing nodes or edges, unless the \c GR template
2227 2233
  /// parameter is set to be \c const.
2228 2234
  ///
2229
  /// \tparam GR The type of the adapted digraph.
2235
  /// \tparam DGR The type of the adapted digraph.
2230 2236
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
2231 2237
  /// It can also be specified to be \c const.
2232 2238
  ///
2233 2239
  /// \note The \c Node type of this adaptor and the adapted digraph are
2234 2240
  /// convertible to each other, moreover the \c Edge type of the adaptor
2235 2241
  /// and the \c Arc type of the adapted digraph are also convertible to
2236 2242
  /// each other.
2237 2243
  /// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type
2238 2244
  /// of the adapted digraph.)
2239
  template<typename GR>
2245
  template<typename DGR>
2240 2246
#ifdef DOXYGEN
2241 2247
  class Undirector {
2242 2248
#else
2243 2249
  class Undirector :
2244
    public GraphAdaptorExtender<UndirectorBase<GR> > {
2250
    public GraphAdaptorExtender<UndirectorBase<DGR> > {
2245 2251
#endif
2252
    typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent;
2246 2253
  public:
2247 2254
    /// The type of the adapted digraph.
2248
    typedef GR Digraph;
2249
    typedef GraphAdaptorExtender<UndirectorBase<GR> > Parent;
2255
    typedef DGR Digraph;
2250 2256
  protected:
2251 2257
    Undirector() { }
2252 2258
  public:
2253 2259

	
2254 2260
    /// \brief Constructor
2255 2261
    ///
2256 2262
    /// Creates an undirected graph from the given digraph.
2257
    Undirector(Digraph& digraph) {
2258
      setDigraph(digraph);
2263
    Undirector(DGR& digraph) {
2264
      initialize(digraph);
2259 2265
    }
2260 2266

	
2261 2267
    /// \brief Arc map combined from two original arc maps
2262 2268
    ///
2263 2269
    /// This map adaptor class adapts two arc maps of the underlying
2264 2270
    /// digraph to get an arc map of the undirected graph.
2265
    /// Its value type is inherited from the first arc map type
2266
    /// (\c %ForwardMap).
2267
    template <typename ForwardMap, typename BackwardMap>
2271
    /// Its value type is inherited from the first arc map type (\c FW).
2272
    /// \tparam FW The type of the "foward" arc map.
2273
    /// \tparam BK The type of the "backward" arc map.
2274
    template <typename FW, typename BK>
2268 2275
    class CombinedArcMap {
2269 2276
    public:
2270 2277

	
2271 2278
      /// The key type of the map
2272 2279
      typedef typename Parent::Arc Key;
2273 2280
      /// The value type of the map
2274
      typedef typename ForwardMap::Value Value;
2275

	
2276
      typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
2277

	
2278
      typedef typename MapTraits<ForwardMap>::ReturnValue ReturnValue;
2279
      typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReturnValue;
2280
      typedef typename MapTraits<ForwardMap>::ReturnValue Reference;
2281
      typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReference;
2281
      typedef typename FW::Value Value;
2282

	
2283
      typedef typename MapTraits<FW>::ReferenceMapTag ReferenceMapTag;
2284

	
2285
      typedef typename MapTraits<FW>::ReturnValue ReturnValue;
2286
      typedef typename MapTraits<FW>::ConstReturnValue ConstReturnValue;
2287
      typedef typename MapTraits<FW>::ReturnValue Reference;
2288
      typedef typename MapTraits<FW>::ConstReturnValue ConstReference;
2282 2289

	
2283 2290
      /// Constructor
2284
      CombinedArcMap(ForwardMap& forward, BackwardMap& backward)
2291
      CombinedArcMap(FW& forward, BK& backward)
2285 2292
        : _forward(&forward), _backward(&backward) {}
2286 2293

	
2287 2294
      /// Sets the value associated with the given key.
2288 2295
      void set(const Key& e, const Value& a) {
2289 2296
        if (Parent::direction(e)) {
2290 2297
          _forward->set(e, a);
2291 2298
        } else {
2292 2299
          _backward->set(e, a);
2293 2300
        }
2294 2301
      }
2295 2302

	
2296 2303
      /// Returns the value associated with the given key.
2297 2304
      ConstReturnValue operator[](const Key& e) const {
2298 2305
        if (Parent::direction(e)) {
2299 2306
          return (*_forward)[e];
2300 2307
        } else {
2301 2308
          return (*_backward)[e];
2302 2309
        }
2303 2310
      }
2304 2311

	
2305 2312
      /// Returns a reference to the value associated with the given key.
2306 2313
      ReturnValue operator[](const Key& e) {
2307 2314
        if (Parent::direction(e)) {
2308 2315
          return (*_forward)[e];
2309 2316
        } else {
2310 2317
          return (*_backward)[e];
2311 2318
        }
2312 2319
      }
2313 2320

	
2314 2321
    protected:
2315 2322

	
2316
      ForwardMap* _forward;
2317
      BackwardMap* _backward;
2323
      FW* _forward;
2324
      BK* _backward;
2318 2325

	
2319 2326
    };
2320 2327

	
2321 2328
    /// \brief Returns a combined arc map
2322 2329
    ///
2323 2330
    /// This function just returns a combined arc map.
2324
    template <typename ForwardMap, typename BackwardMap>
2325
    static CombinedArcMap<ForwardMap, BackwardMap>
2326
    combinedArcMap(ForwardMap& forward, BackwardMap& backward) {
2327
      return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward);
2331
    template <typename FW, typename BK>
2332
    static CombinedArcMap<FW, BK>
2333
    combinedArcMap(FW& forward, BK& backward) {
2334
      return CombinedArcMap<FW, BK>(forward, backward);
2328 2335
    }
2329 2336

	
2330
    template <typename ForwardMap, typename BackwardMap>
2331
    static CombinedArcMap<const ForwardMap, BackwardMap>
2332
    combinedArcMap(const ForwardMap& forward, BackwardMap& backward) {
2333
      return CombinedArcMap<const ForwardMap,
2334
        BackwardMap>(forward, backward);
2337
    template <typename FW, typename BK>
2338
    static CombinedArcMap<const FW, BK>
2339
    combinedArcMap(const FW& forward, BK& backward) {
2340
      return CombinedArcMap<const FW, BK>(forward, backward);
2335 2341
    }
2336 2342

	
2337
    template <typename ForwardMap, typename BackwardMap>
2338
    static CombinedArcMap<ForwardMap, const BackwardMap>
2339
    combinedArcMap(ForwardMap& forward, const BackwardMap& backward) {
2340
      return CombinedArcMap<ForwardMap,
2341
        const BackwardMap>(forward, backward);
2343
    template <typename FW, typename BK>
2344
    static CombinedArcMap<FW, const BK>
2345
    combinedArcMap(FW& forward, const BK& backward) {
2346
      return CombinedArcMap<FW, const BK>(forward, backward);
2342 2347
    }
2343 2348

	
2344
    template <typename ForwardMap, typename BackwardMap>
2345
    static CombinedArcMap<const ForwardMap, const BackwardMap>
2346
    combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) {
2347
      return CombinedArcMap<const ForwardMap,
2348
        const BackwardMap>(forward, backward);
2349
    template <typename FW, typename BK>
2350
    static CombinedArcMap<const FW, const BK>
2351
    combinedArcMap(const FW& forward, const BK& backward) {
2352
      return CombinedArcMap<const FW, const BK>(forward, backward);
2349 2353
    }
2350 2354

	
2351 2355
  };
2352 2356

	
2353 2357
  /// \brief Returns a read-only Undirector adaptor
2354 2358
  ///
2355 2359
  /// This function just returns a read-only \ref Undirector adaptor.
2356 2360
  /// \ingroup graph_adaptors
2357 2361
  /// \relates Undirector
2358
  template<typename GR>
2359
  Undirector<const GR> undirector(const GR& digraph) {
2360
    return Undirector<const GR>(digraph);
2362
  template<typename DGR>
2363
  Undirector<const DGR> undirector(const DGR& digraph) {
2364
    return Undirector<const DGR>(digraph);
2361 2365
  }
2362 2366

	
2363 2367

	
2364
  template <typename _Graph, typename _DirectionMap>
2368
  template <typename GR, typename DM>
2365 2369
  class OrienterBase {
2366 2370
  public:
2367 2371

	
2368
    typedef _Graph Graph;
2369
    typedef _DirectionMap DirectionMap;
2370

	
2371
    typedef typename Graph::Node Node;
2372
    typedef typename Graph::Edge Arc;
2372
    typedef GR Graph;
2373
    typedef DM DirectionMap;
2374

	
2375
    typedef typename GR::Node Node;
2376
    typedef typename GR::Edge Arc;
2373 2377

	
2374 2378
    void reverseArc(const Arc& arc) {
2375 2379
      _direction->set(arc, !(*_direction)[arc]);
2376 2380
    }
2377 2381

	
2378 2382
    void first(Node& i) const { _graph->first(i); }
2379 2383
    void first(Arc& i) const { _graph->first(i); }
2380 2384
    void firstIn(Arc& i, const Node& n) const {
2381 2385
      bool d = true;
2382 2386
      _graph->firstInc(i, d, n);
2383 2387
      while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2384 2388
    }
2385 2389
    void firstOut(Arc& i, const Node& n ) const {
2386 2390
      bool d = true;
2387 2391
      _graph->firstInc(i, d, n);
2388 2392
      while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2389 2393
    }
2390 2394

	
2391 2395
    void next(Node& i) const { _graph->next(i); }
2392 2396
    void next(Arc& i) const { _graph->next(i); }
2393 2397
    void nextIn(Arc& i) const {
2394 2398
      bool d = !(*_direction)[i];
2395 2399
      _graph->nextInc(i, d);
2396 2400
      while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d);
2397 2401
    }
2398 2402
    void nextOut(Arc& i) const {
2399 2403
      bool d = (*_direction)[i];
2400 2404
      _graph->nextInc(i, d);
2401 2405
      while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d);
2402 2406
    }
2403 2407

	
2404 2408
    Node source(const Arc& e) const {
2405 2409
      return (*_direction)[e] ? _graph->u(e) : _graph->v(e);
2406 2410
    }
2407 2411
    Node target(const Arc& e) const {
2408 2412
      return (*_direction)[e] ? _graph->v(e) : _graph->u(e);
2409 2413
    }
2410 2414

	
2411 2415
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
2412 2416
    int nodeNum() const { return _graph->nodeNum(); }
2413 2417

	
2414 2418
    typedef EdgeNumTagIndicator<Graph> ArcNumTag;
2415 2419
    int arcNum() const { return _graph->edgeNum(); }
2416 2420

	
2417 2421
    typedef FindEdgeTagIndicator<Graph> FindArcTag;
2418 2422
    Arc findArc(const Node& u, const Node& v,
2419 2423
                const Arc& prev = INVALID) const {
2420 2424
      Arc arc = _graph->findEdge(u, v, prev);
2421 2425
      while (arc != INVALID && source(arc) != u) {
2422 2426
        arc = _graph->findEdge(u, v, arc);
2423 2427
      }
2424 2428
      return arc;
2425 2429
    }
2426 2430

	
2427 2431
    Node addNode() {
2428 2432
      return Node(_graph->addNode());
2429 2433
    }
2430 2434

	
2431 2435
    Arc addArc(const Node& u, const Node& v) {
2432 2436
      Arc arc = _graph->addEdge(u, v);
2433 2437
      _direction->set(arc, _graph->u(arc) == u);
2434 2438
      return arc;
2435 2439
    }
2436 2440

	
2437 2441
    void erase(const Node& i) { _graph->erase(i); }
2438 2442
    void erase(const Arc& i) { _graph->erase(i); }
2439 2443

	
2440 2444
    void clear() { _graph->clear(); }
2441 2445

	
2442 2446
    int id(const Node& v) const { return _graph->id(v); }
2443 2447
    int id(const Arc& e) const { return _graph->id(e); }
2444 2448

	
2445 2449
    Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); }
2446 2450
    Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); }
2447 2451

	
2448 2452
    int maxNodeId() const { return _graph->maxNodeId(); }
2449 2453
    int maxArcId() const { return _graph->maxEdgeId(); }
2450 2454

	
2451
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
2455
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
2452 2456
    NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
2453 2457

	
2454
    typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier;
2458
    typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier;
2455 2459
    ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
2456 2460

	
2457
    template <typename _Value>
2458
    class NodeMap : public _Graph::template NodeMap<_Value> {
2461
    template <typename V>
2462
    class NodeMap : public GR::template NodeMap<V> {
2463
      typedef typename GR::template NodeMap<V> Parent;
2464

	
2459 2465
    public:
2460 2466

	
2461
      typedef typename _Graph::template NodeMap<_Value> Parent;
2462

	
2463
      explicit NodeMap(const OrienterBase& adapter)
2467
      explicit NodeMap(const OrienterBase<GR, DM>& adapter)
2464 2468
        : Parent(*adapter._graph) {}
2465 2469

	
2466
      NodeMap(const OrienterBase& adapter, const _Value& value)
2470
      NodeMap(const OrienterBase<GR, DM>& adapter, const V& value)
2467 2471
        : Parent(*adapter._graph, value) {}
2468 2472

	
2469 2473
    private:
2470 2474
      NodeMap& operator=(const NodeMap& cmap) {
2471 2475
        return operator=<NodeMap>(cmap);
2472 2476
      }
2473 2477

	
2474 2478
      template <typename CMap>
2475 2479
      NodeMap& operator=(const CMap& cmap) {
2476 2480
        Parent::operator=(cmap);
2477 2481
        return *this;
2478 2482
      }
2479 2483

	
2480 2484
    };
2481 2485

	
2482
    template <typename _Value>
2483
    class ArcMap : public _Graph::template EdgeMap<_Value> {
2486
    template <typename V>
2487
    class ArcMap : public GR::template EdgeMap<V> {
2488
      typedef typename Graph::template EdgeMap<V> Parent;
2489

	
2484 2490
    public:
2485 2491

	
2486
      typedef typename Graph::template EdgeMap<_Value> Parent;
2487

	
2488
      explicit ArcMap(const OrienterBase& adapter)
2492
      explicit ArcMap(const OrienterBase<GR, DM>& adapter)
2489 2493
        : Parent(*adapter._graph) { }
2490 2494

	
2491
      ArcMap(const OrienterBase& adapter, const _Value& value)
2495
      ArcMap(const OrienterBase<GR, DM>& adapter, const V& value)
2492 2496
        : Parent(*adapter._graph, value) { }
2493 2497

	
2494 2498
    private:
2495 2499
      ArcMap& operator=(const ArcMap& cmap) {
2496 2500
        return operator=<ArcMap>(cmap);
2497 2501
      }
2498 2502

	
2499 2503
      template <typename CMap>
2500 2504
      ArcMap& operator=(const CMap& cmap) {
2501 2505
        Parent::operator=(cmap);
2502 2506
        return *this;
2503 2507
      }
2504 2508
    };
2505 2509

	
2506 2510

	
2507 2511

	
2508 2512
  protected:
2509 2513
    Graph* _graph;
2510
    DirectionMap* _direction;
2511

	
2512
    void setDirectionMap(DirectionMap& direction) {
2514
    DM* _direction;
2515

	
2516
    void initialize(GR& graph, DM& direction) {
2517
      _graph = &graph;
2513 2518
      _direction = &direction;
2514 2519
    }
2515 2520

	
2516
    void setGraph(Graph& graph) {
2517
      _graph = &graph;
2518
    }
2519

	
2520 2521
  };
2521 2522

	
2522 2523
  /// \ingroup graph_adaptors
2523 2524
  ///
2524 2525
  /// \brief Adaptor class for orienting the edges of a graph to get a digraph
2525 2526
  ///
2526 2527
  /// Orienter adaptor can be used for orienting the edges of a graph to
2527 2528
  /// get a digraph. A \c bool edge map of the underlying graph must be
2528 2529
  /// specified, which define the direction of the arcs in the adaptor.
2529 2530
  /// The arcs can be easily reversed by the \c reverseArc() member function
2530 2531
  /// of the adaptor.
2531 2532
  /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
2532 2533
  ///
2533 2534
  /// The adapted graph can also be modified through this adaptor
2534 2535
  /// by adding or removing nodes or arcs, unless the \c GR template
2535 2536
  /// parameter is set to be \c const.
2536 2537
  ///
2537 2538
  /// \tparam GR The type of the adapted graph.
2538 2539
  /// It must conform to the \ref concepts::Graph "Graph" concept.
2539 2540
  /// It can also be specified to be \c const.
2540 2541
  /// \tparam DM The type of the direction map.
2541 2542
  /// It must be a \c bool (or convertible) edge map of the
2542 2543
  /// adapted graph. The default type is
2543 2544
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>".
2544 2545
  ///
2545 2546
  /// \note The \c Node type of this adaptor and the adapted graph are
2546 2547
  /// convertible to each other, moreover the \c Arc type of the adaptor
2547 2548
  /// and the \c Edge type of the adapted graph are also convertible to
2548 2549
  /// each other.
2549 2550
#ifdef DOXYGEN
2550 2551
  template<typename GR,
2551 2552
           typename DM>
2552 2553
  class Orienter {
2553 2554
#else
2554 2555
  template<typename GR,
2555 2556
           typename DM = typename GR::template EdgeMap<bool> >
2556 2557
  class Orienter :
2557 2558
    public DigraphAdaptorExtender<OrienterBase<GR, DM> > {
2558 2559
#endif
2560
    typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent;
2559 2561
  public:
2560 2562

	
2561 2563
    /// The type of the adapted graph.
2562 2564
    typedef GR Graph;
2563 2565
    /// The type of the direction edge map.
2564 2566
    typedef DM DirectionMap;
2565 2567

	
2566
    typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent;
2567 2568
    typedef typename Parent::Arc Arc;
2569

	
2568 2570
  protected:
2569 2571
    Orienter() { }
2572

	
2570 2573
  public:
2571 2574

	
2572 2575
    /// \brief Constructor
2573 2576
    ///
2574 2577
    /// Constructor of the adaptor.
2575
    Orienter(Graph& graph, DirectionMap& direction) {
2576
      setGraph(graph);
2577
      setDirectionMap(direction);
2578
    Orienter(GR& graph, DM& direction) {
2579
      Parent::initialize(graph, direction);
2578 2580
    }
2579 2581

	
2580 2582
    /// \brief Reverses the given arc
2581 2583
    ///
2582 2584
    /// This function reverses the given arc.
2583 2585
    /// It is done by simply negate the assigned value of \c a
2584 2586
    /// in the direction map.
2585 2587
    void reverseArc(const Arc& a) {
2586 2588
      Parent::reverseArc(a);
2587 2589
    }
2588 2590
  };
2589 2591

	
2590 2592
  /// \brief Returns a read-only Orienter adaptor
2591 2593
  ///
2592 2594
  /// This function just returns a read-only \ref Orienter adaptor.
2593 2595
  /// \ingroup graph_adaptors
2594 2596
  /// \relates Orienter
2595 2597
  template<typename GR, typename DM>
2596 2598
  Orienter<const GR, DM>
2597
  orienter(const GR& graph, DM& direction_map) {
2598
    return Orienter<const GR, DM>(graph, direction_map);
2599
  orienter(const GR& graph, DM& direction) {
2600
    return Orienter<const GR, DM>(graph, direction);
2599 2601
  }
2600 2602

	
2601 2603
  template<typename GR, typename DM>
2602 2604
  Orienter<const GR, const DM>
2603
  orienter(const GR& graph, const DM& direction_map) {
2604
    return Orienter<const GR, const DM>(graph, direction_map);
2605
  orienter(const GR& graph, const DM& direction) {
2606
    return Orienter<const GR, const DM>(graph, direction);
2605 2607
  }
2606 2608

	
2607 2609
  namespace _adaptor_bits {
2608 2610

	
2609
    template<typename Digraph,
2610
             typename CapacityMap,
2611
             typename FlowMap,
2612
             typename Tolerance>
2611
    template <typename DGR, typename CM, typename FM, typename TL>
2613 2612
    class ResForwardFilter {
2614 2613
    public:
2615 2614

	
2616
      typedef typename Digraph::Arc Key;
2615
      typedef typename DGR::Arc Key;
2617 2616
      typedef bool Value;
2618 2617

	
2619 2618
    private:
2620 2619

	
2621
      const CapacityMap* _capacity;
2622
      const FlowMap* _flow;
2623
      Tolerance _tolerance;
2620
      const CM* _capacity;
2621
      const FM* _flow;
2622
      TL _tolerance;
2623

	
2624 2624
    public:
2625 2625

	
2626
      ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow,
2627
                       const Tolerance& tolerance = Tolerance())
2626
      ResForwardFilter(const CM& capacity, const FM& flow,
2627
                       const TL& tolerance = TL())
2628 2628
        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2629 2629

	
2630
      bool operator[](const typename Digraph::Arc& a) const {
2630
      bool operator[](const typename DGR::Arc& a) const {
2631 2631
        return _tolerance.positive((*_capacity)[a] - (*_flow)[a]);
2632 2632
      }
2633 2633
    };
2634 2634

	
2635
    template<typename Digraph,
2636
             typename CapacityMap,
2637
             typename FlowMap,
2638
             typename Tolerance>
2635
    template<typename DGR,typename CM, typename FM, typename TL>
2639 2636
    class ResBackwardFilter {
2640 2637
    public:
2641 2638

	
2642
      typedef typename Digraph::Arc Key;
2639
      typedef typename DGR::Arc Key;
2643 2640
      typedef bool Value;
2644 2641

	
2645 2642
    private:
2646 2643

	
2647
      const CapacityMap* _capacity;
2648
      const FlowMap* _flow;
2649
      Tolerance _tolerance;
2644
      const CM* _capacity;
2645
      const FM* _flow;
2646
      TL _tolerance;
2650 2647

	
2651 2648
    public:
2652 2649

	
2653
      ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow,
2654
                        const Tolerance& tolerance = Tolerance())
2650
      ResBackwardFilter(const CM& capacity, const FM& flow,
2651
                        const TL& tolerance = TL())
2655 2652
        : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { }
2656 2653

	
2657
      bool operator[](const typename Digraph::Arc& a) const {
2654
      bool operator[](const typename DGR::Arc& a) const {
2658 2655
        return _tolerance.positive((*_flow)[a]);
2659 2656
      }
2660 2657
    };
2661 2658

	
2662 2659
  }
2663 2660

	
2664 2661
  /// \ingroup graph_adaptors
2665 2662
  ///
2666 2663
  /// \brief Adaptor class for composing the residual digraph for directed
2667 2664
  /// flow and circulation problems.
2668 2665
  ///
2669 2666
  /// ResidualDigraph can be used for composing the \e residual digraph
2670 2667
  /// for directed flow and circulation problems. Let \f$ G=(V, A) \f$
2671 2668
  /// be a directed graph and let \f$ F \f$ be a number type.
2672 2669
  /// Let \f$ flow, cap: A\to F \f$ be functions on the arcs.
2673 2670
  /// This adaptor implements a digraph structure with node set \f$ V \f$
2674 2671
  /// and arc set \f$ A_{forward}\cup A_{backward} \f$,
2675 2672
  /// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and
2676 2673
  /// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so
2677 2674
  /// called residual digraph.
2678 2675
  /// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken,
2679 2676
  /// multiplicities are counted, i.e. the adaptor has exactly
2680 2677
  /// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel
2681 2678
  /// arcs).
2682 2679
  /// This class conforms to the \ref concepts::Digraph "Digraph" concept.
2683 2680
  ///
2684
  /// \tparam GR The type of the adapted digraph.
2681
  /// \tparam DGR The type of the adapted digraph.
2685 2682
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
2686 2683
  /// It is implicitly \c const.
2687 2684
  /// \tparam CM The type of the capacity map.
2688 2685
  /// It must be an arc map of some numerical type, which defines
2689 2686
  /// the capacities in the flow problem. It is implicitly \c const.
2690 2687
  /// The default type is
2691 2688
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
2692 2689
  /// \tparam FM The type of the flow map.
2693 2690
  /// It must be an arc map of some numerical type, which defines
2694 2691
  /// the flow values in the flow problem. The default type is \c CM.
2695 2692
  /// \tparam TL The tolerance type for handling inexact computation.
2696 2693
  /// The default tolerance type depends on the value type of the
2697 2694
  /// capacity map.
2698 2695
  ///
2699 2696
  /// \note This adaptor is implemented using Undirector and FilterArcs
2700 2697
  /// adaptors.
2701 2698
  ///
2702 2699
  /// \note The \c Node type of this adaptor and the adapted digraph are
2703 2700
  /// convertible to each other, moreover the \c Arc type of the adaptor
2704 2701
  /// is convertible to the \c Arc type of the adapted digraph.
2705 2702
#ifdef DOXYGEN
2706
  template<typename GR, typename CM, typename FM, typename TL>
2703
  template<typename DGR, typename CM, typename FM, typename TL>
2707 2704
  class ResidualDigraph
2708 2705
#else
2709
  template<typename GR,
2710
           typename CM = typename GR::template ArcMap<int>,
2706
  template<typename DGR,
2707
           typename CM = typename DGR::template ArcMap<int>,
2711 2708
           typename FM = CM,
2712 2709
           typename TL = Tolerance<typename CM::Value> >
2713
  class ResidualDigraph :
2714
    public FilterArcs<
2715
      Undirector<const GR>,
2716
      typename Undirector<const GR>::template CombinedArcMap<
2717
        _adaptor_bits::ResForwardFilter<const GR, CM, FM, TL>,
2718
        _adaptor_bits::ResBackwardFilter<const GR, CM, FM, TL> > >
2710
  class ResidualDigraph 
2711
    : public SubDigraph<
2712
        Undirector<const DGR>,
2713
        ConstMap<typename DGR::Node, Const<bool, true> >,
2714
        typename Undirector<const DGR>::template CombinedArcMap<
2715
          _adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>,
2716
          _adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > >
2719 2717
#endif
2720 2718
  {
2721 2719
  public:
2722 2720

	
2723 2721
    /// The type of the underlying digraph.
2724
    typedef GR Digraph;
2722
    typedef DGR Digraph;
2725 2723
    /// The type of the capacity map.
2726 2724
    typedef CM CapacityMap;
2727 2725
    /// The type of the flow map.
2728 2726
    typedef FM FlowMap;
2729 2727
    /// The tolerance type.
2730 2728
    typedef TL Tolerance;
2731 2729

	
2732 2730
    typedef typename CapacityMap::Value Value;
2733 2731
    typedef ResidualDigraph Adaptor;
2734 2732

	
2735 2733
  protected:
2736 2734

	
2737 2735
    typedef Undirector<const Digraph> Undirected;
2738 2736

	
2739
    typedef _adaptor_bits::ResForwardFilter<const Digraph, CapacityMap,
2740
                                            FlowMap, Tolerance> ForwardFilter;
2741

	
2742
    typedef _adaptor_bits::ResBackwardFilter<const Digraph, CapacityMap,
2743
                                             FlowMap, Tolerance> BackwardFilter;
2737
    typedef ConstMap<typename DGR::Node, Const<bool, true> > NodeFilter;
2738

	
2739
    typedef _adaptor_bits::ResForwardFilter<const DGR, CM,
2740
                                            FM, TL> ForwardFilter;
2741

	
2742
    typedef _adaptor_bits::ResBackwardFilter<const DGR, CM,
2743
                                             FM, TL> BackwardFilter;
2744 2744

	
2745 2745
    typedef typename Undirected::
2746 2746
      template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter;
2747 2747

	
2748
    typedef FilterArcs<Undirected, ArcFilter> Parent;
2748
    typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent;
2749 2749

	
2750 2750
    const CapacityMap* _capacity;
2751 2751
    FlowMap* _flow;
2752 2752

	
2753 2753
    Undirected _graph;
2754
    NodeFilter _node_filter;
2754 2755
    ForwardFilter _forward_filter;
2755 2756
    BackwardFilter _backward_filter;
2756 2757
    ArcFilter _arc_filter;
2757 2758

	
2758 2759
  public:
2759 2760

	
2760 2761
    /// \brief Constructor
2761 2762
    ///
2762 2763
    /// Constructor of the residual digraph adaptor. The parameters are the
2763 2764
    /// digraph, the capacity map, the flow map, and a tolerance object.
2764
    ResidualDigraph(const Digraph& digraph, const CapacityMap& capacity,
2765
                    FlowMap& flow, const Tolerance& tolerance = Tolerance())
2766
      : Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph),
2765
    ResidualDigraph(const DGR& digraph, const CM& capacity,
2766
                    FM& flow, const TL& tolerance = Tolerance())
2767
      : Parent(), _capacity(&capacity), _flow(&flow), 
2768
        _graph(digraph), _node_filter(),
2767 2769
        _forward_filter(capacity, flow, tolerance),
2768 2770
        _backward_filter(capacity, flow, tolerance),
2769 2771
        _arc_filter(_forward_filter, _backward_filter)
2770 2772
    {
2771
      Parent::setDigraph(_graph);
2772
      Parent::setArcFilterMap(_arc_filter);
2773
      Parent::initialize(_graph, _node_filter, _arc_filter);
2773 2774
    }
2774 2775

	
2775 2776
    typedef typename Parent::Arc Arc;
2776 2777

	
2777 2778
    /// \brief Returns the residual capacity of the given arc.
2778 2779
    ///
2779 2780
    /// Returns the residual capacity of the given arc.
2780 2781
    Value residualCapacity(const Arc& a) const {
2781 2782
      if (Undirected::direction(a)) {
2782 2783
        return (*_capacity)[a] - (*_flow)[a];
2783 2784
      } else {
2784 2785
        return (*_flow)[a];
2785 2786
      }
2786 2787
    }
2787 2788

	
2788 2789
    /// \brief Augments on the given arc in the residual digraph.
2789 2790
    ///
2790 2791
    /// Augments on the given arc in the residual digraph. It increases
2791 2792
    /// or decreases the flow value on the original arc according to the
2792 2793
    /// direction of the residual arc.
2793 2794
    void augment(const Arc& a, const Value& v) const {
2794 2795
      if (Undirected::direction(a)) {
2795 2796
        _flow->set(a, (*_flow)[a] + v);
2796 2797
      } else {
2797 2798
        _flow->set(a, (*_flow)[a] - v);
2798 2799
      }
2799 2800
    }
2800 2801

	
2801 2802
    /// \brief Returns \c true if the given residual arc is a forward arc.
2802 2803
    ///
2803 2804
    /// Returns \c true if the given residual arc has the same orientation
2804 2805
    /// as the original arc, i.e. it is a so called forward arc.
2805 2806
    static bool forward(const Arc& a) {
2806 2807
      return Undirected::direction(a);
2807 2808
    }
2808 2809

	
2809 2810
    /// \brief Returns \c true if the given residual arc is a backward arc.
2810 2811
    ///
2811 2812
    /// Returns \c true if the given residual arc has the opposite orientation
2812 2813
    /// than the original arc, i.e. it is a so called backward arc.
2813 2814
    static bool backward(const Arc& a) {
2814 2815
      return !Undirected::direction(a);
2815 2816
    }
2816 2817

	
2817 2818
    /// \brief Returns the forward oriented residual arc.
2818 2819
    ///
2819 2820
    /// Returns the forward oriented residual arc related to the given
2820 2821
    /// arc of the underlying digraph.
2821 2822
    static Arc forward(const typename Digraph::Arc& a) {
2822 2823
      return Undirected::direct(a, true);
2823 2824
    }
2824 2825

	
2825 2826
    /// \brief Returns the backward oriented residual arc.
2826 2827
    ///
2827 2828
    /// Returns the backward oriented residual arc related to the given
2828 2829
    /// arc of the underlying digraph.
2829 2830
    static Arc backward(const typename Digraph::Arc& a) {
2830 2831
      return Undirected::direct(a, false);
2831 2832
    }
2832 2833

	
2833 2834
    /// \brief Residual capacity map.
2834 2835
    ///
2835 2836
    /// This map adaptor class can be used for obtaining the residual
2836 2837
    /// capacities as an arc map of the residual digraph.
2837 2838
    /// Its value type is inherited from the capacity map.
2838 2839
    class ResidualCapacity {
2839 2840
    protected:
2840 2841
      const Adaptor* _adaptor;
2841 2842
    public:
2842 2843
      /// The key type of the map
2843 2844
      typedef Arc Key;
2844 2845
      /// The value type of the map
2845 2846
      typedef typename CapacityMap::Value Value;
2846 2847

	
2847 2848
      /// Constructor
2848
      ResidualCapacity(const Adaptor& adaptor) : _adaptor(&adaptor) {}
2849
      ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) 
2850
        : _adaptor(&adaptor) {}
2849 2851

	
2850 2852
      /// Returns the value associated with the given residual arc
2851 2853
      Value operator[](const Arc& a) const {
2852 2854
        return _adaptor->residualCapacity(a);
2853 2855
      }
2854 2856

	
2855 2857
    };
2856 2858

	
2857 2859
    /// \brief Returns a residual capacity map
2858 2860
    ///
2859 2861
    /// This function just returns a residual capacity map.
2860 2862
    ResidualCapacity residualCapacity() const {
2861 2863
      return ResidualCapacity(*this);
2862 2864
    }
2863 2865

	
2864 2866
  };
2865 2867

	
2866 2868
  /// \brief Returns a (read-only) Residual adaptor
2867 2869
  ///
2868
  /// This function just returns a (read-only) \ref Residual adaptor.
2870
  /// This function just returns a (read-only) \ref ResidualDigraph adaptor.
2869 2871
  /// \ingroup graph_adaptors
2870
  /// \relates Residual
2871
  template<typename GR, typename CM, typename FM>
2872
  ResidualDigraph<GR, CM, FM>
2873
  residualDigraph(const GR& digraph, const CM& capacity_map, FM& flow_map) {
2874
    return ResidualDigraph<GR, CM, FM> (digraph, capacity_map, flow_map);
2872
  /// \relates ResidualDigraph
2873
    template<typename DGR, typename CM, typename FM>
2874
  ResidualDigraph<DGR, CM, FM>
2875
  residualDigraph(const DGR& digraph, const CM& capacity_map, FM& flow_map) {
2876
    return ResidualDigraph<DGR, CM, FM> (digraph, capacity_map, flow_map);
2875 2877
  }
2876 2878

	
2877 2879

	
2878
  template <typename _Digraph>
2880
  template <typename DGR>
2879 2881
  class SplitNodesBase {
2882
    typedef DigraphAdaptorBase<const DGR> Parent;
2883

	
2880 2884
  public:
2881 2885

	
2882
    typedef _Digraph Digraph;
2883
    typedef DigraphAdaptorBase<const _Digraph> Parent;
2886
    typedef DGR Digraph;
2884 2887
    typedef SplitNodesBase Adaptor;
2885 2888

	
2886
    typedef typename Digraph::Node DigraphNode;
2887
    typedef typename Digraph::Arc DigraphArc;
2889
    typedef typename DGR::Node DigraphNode;
2890
    typedef typename DGR::Arc DigraphArc;
2888 2891

	
2889 2892
    class Node;
2890 2893
    class Arc;
2891 2894

	
2892 2895
  private:
2893 2896

	
2894 2897
    template <typename T> class NodeMapBase;
2895 2898
    template <typename T> class ArcMapBase;
2896 2899

	
2897 2900
  public:
2898 2901

	
2899 2902
    class Node : public DigraphNode {
2900 2903
      friend class SplitNodesBase;
2901 2904
      template <typename T> friend class NodeMapBase;
2902 2905
    private:
2903 2906

	
2904 2907
      bool _in;
2905 2908
      Node(DigraphNode node, bool in)
2906 2909
        : DigraphNode(node), _in(in) {}
2907 2910

	
2908 2911
    public:
2909 2912

	
2910 2913
      Node() {}
2911 2914
      Node(Invalid) : DigraphNode(INVALID), _in(true) {}
2912 2915

	
2913 2916
      bool operator==(const Node& node) const {
2914 2917
        return DigraphNode::operator==(node) && _in == node._in;
2915 2918
      }
2916 2919

	
2917 2920
      bool operator!=(const Node& node) const {
2918 2921
        return !(*this == node);
2919 2922
      }
2920 2923

	
2921 2924
      bool operator<(const Node& node) const {
2922 2925
        return DigraphNode::operator<(node) ||
2923 2926
          (DigraphNode::operator==(node) && _in < node._in);
2924 2927
      }
2925 2928
    };
2926 2929

	
2927 2930
    class Arc {
2928 2931
      friend class SplitNodesBase;
2929 2932
      template <typename T> friend class ArcMapBase;
2930 2933
    private:
2931 2934
      typedef BiVariant<DigraphArc, DigraphNode> ArcImpl;
2932 2935

	
2933 2936
      explicit Arc(const DigraphArc& arc) : _item(arc) {}
2934 2937
      explicit Arc(const DigraphNode& node) : _item(node) {}
2935 2938

	
2936 2939
      ArcImpl _item;
2937 2940

	
2938 2941
    public:
2939 2942
      Arc() {}
2940 2943
      Arc(Invalid) : _item(DigraphArc(INVALID)) {}
2941 2944

	
2942 2945
      bool operator==(const Arc& arc) const {
2943 2946
        if (_item.firstState()) {
2944 2947
          if (arc._item.firstState()) {
2945 2948
            return _item.first() == arc._item.first();
2946 2949
          }
2947 2950
        } else {
2948 2951
          if (arc._item.secondState()) {
2949 2952
            return _item.second() == arc._item.second();
2950 2953
          }
2951 2954
        }
2952 2955
        return false;
2953 2956
      }
2954 2957

	
2955 2958
      bool operator!=(const Arc& arc) const {
2956 2959
        return !(*this == arc);
2957 2960
      }
2958 2961

	
2959 2962
      bool operator<(const Arc& arc) const {
2960 2963
        if (_item.firstState()) {
2961 2964
          if (arc._item.firstState()) {
2962 2965
            return _item.first() < arc._item.first();
2963 2966
          }
2964 2967
          return false;
2965 2968
        } else {
2966 2969
          if (arc._item.secondState()) {
2967 2970
            return _item.second() < arc._item.second();
2968 2971
          }
2969 2972
          return true;
2970 2973
        }
2971 2974
      }
2972 2975

	
2973 2976
      operator DigraphArc() const { return _item.first(); }
2974 2977
      operator DigraphNode() const { return _item.second(); }
2975 2978

	
2976 2979
    };
2977 2980

	
2978 2981
    void first(Node& n) const {
2979 2982
      _digraph->first(n);
2980 2983
      n._in = true;
2981 2984
    }
2982 2985

	
2983 2986
    void next(Node& n) const {
... ...
@@ -3055,551 +3058,557 @@
3055 3058
    Node target(const Arc& e) const {
3056 3059
      if (e._item.firstState()) {
3057 3060
        return Node(_digraph->target(e._item.first()), true);
3058 3061
      } else {
3059 3062
        return Node(e._item.second(), false);
3060 3063
      }
3061 3064
    }
3062 3065

	
3063 3066
    int id(const Node& n) const {
3064 3067
      return (_digraph->id(n) << 1) | (n._in ? 0 : 1);
3065 3068
    }
3066 3069
    Node nodeFromId(int ix) const {
3067 3070
      return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0);
3068 3071
    }
3069 3072
    int maxNodeId() const {
3070 3073
      return 2 * _digraph->maxNodeId() + 1;
3071 3074
    }
3072 3075

	
3073 3076
    int id(const Arc& e) const {
3074 3077
      if (e._item.firstState()) {
3075 3078
        return _digraph->id(e._item.first()) << 1;
3076 3079
      } else {
3077 3080
        return (_digraph->id(e._item.second()) << 1) | 1;
3078 3081
      }
3079 3082
    }
3080 3083
    Arc arcFromId(int ix) const {
3081 3084
      if ((ix & 1) == 0) {
3082 3085
        return Arc(_digraph->arcFromId(ix >> 1));
3083 3086
      } else {
3084 3087
        return Arc(_digraph->nodeFromId(ix >> 1));
3085 3088
      }
3086 3089
    }
3087 3090
    int maxArcId() const {
3088 3091
      return std::max(_digraph->maxNodeId() << 1,
3089 3092
                      (_digraph->maxArcId() << 1) | 1);
3090 3093
    }
3091 3094

	
3092 3095
    static bool inNode(const Node& n) {
3093 3096
      return n._in;
3094 3097
    }
3095 3098

	
3096 3099
    static bool outNode(const Node& n) {
3097 3100
      return !n._in;
3098 3101
    }
3099 3102

	
3100 3103
    static bool origArc(const Arc& e) {
3101 3104
      return e._item.firstState();
3102 3105
    }
3103 3106

	
3104 3107
    static bool bindArc(const Arc& e) {
3105 3108
      return e._item.secondState();
3106 3109
    }
3107 3110

	
3108 3111
    static Node inNode(const DigraphNode& n) {
3109 3112
      return Node(n, true);
3110 3113
    }
3111 3114

	
3112 3115
    static Node outNode(const DigraphNode& n) {
3113 3116
      return Node(n, false);
3114 3117
    }
3115 3118

	
3116 3119
    static Arc arc(const DigraphNode& n) {
3117 3120
      return Arc(n);
3118 3121
    }
3119 3122

	
3120 3123
    static Arc arc(const DigraphArc& e) {
3121 3124
      return Arc(e);
3122 3125
    }
3123 3126

	
3124 3127
    typedef True NodeNumTag;
3125 3128
    int nodeNum() const {
3126 3129
      return  2 * countNodes(*_digraph);
3127 3130
    }
3128 3131

	
3129 3132
    typedef True ArcNumTag;
3130 3133
    int arcNum() const {
3131 3134
      return countArcs(*_digraph) + countNodes(*_digraph);
3132 3135
    }
3133 3136

	
3134 3137
    typedef True FindArcTag;
3135 3138
    Arc findArc(const Node& u, const Node& v,
3136 3139
                const Arc& prev = INVALID) const {
3137 3140
      if (inNode(u) && outNode(v)) {
3138 3141
        if (static_cast<const DigraphNode&>(u) ==
3139 3142
            static_cast<const DigraphNode&>(v) && prev == INVALID) {
3140 3143
          return Arc(u);
3141 3144
        }
3142 3145
      }
3143 3146
      else if (outNode(u) && inNode(v)) {
3144 3147
        return Arc(::lemon::findArc(*_digraph, u, v, prev));
3145 3148
      }
3146 3149
      return INVALID;
3147 3150
    }
3148 3151

	
3149 3152
  private:
3150 3153

	
3151
    template <typename _Value>
3154
    template <typename V>
3152 3155
    class NodeMapBase
3153
      : public MapTraits<typename Parent::template NodeMap<_Value> > {
3154
      typedef typename Parent::template NodeMap<_Value> NodeImpl;
3156
      : public MapTraits<typename Parent::template NodeMap<V> > {
3157
      typedef typename Parent::template NodeMap<V> NodeImpl;
3155 3158
    public:
3156 3159
      typedef Node Key;
3157
      typedef _Value Value;
3160
      typedef V Value;
3158 3161
      typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag;
3159 3162
      typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue;
3160 3163
      typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue;
3161 3164
      typedef typename MapTraits<NodeImpl>::ReturnValue Reference;
3162 3165
      typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference;
3163 3166

	
3164
      NodeMapBase(const Adaptor& adaptor)
3167
      NodeMapBase(const SplitNodesBase<DGR>& adaptor)
3165 3168
        : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {}
3166
      NodeMapBase(const Adaptor& adaptor, const Value& value)
3169
      NodeMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
3167 3170
        : _in_map(*adaptor._digraph, value),
3168 3171
          _out_map(*adaptor._digraph, value) {}
3169 3172

	
3170
      void set(const Node& key, const Value& val) {
3171
        if (Adaptor::inNode(key)) { _in_map.set(key, val); }
3173
      void set(const Node& key, const V& val) {
3174
        if (SplitNodesBase<DGR>::inNode(key)) { _in_map.set(key, val); }
3172 3175
        else {_out_map.set(key, val); }
3173 3176
      }
3174 3177

	
3175 3178
      ReturnValue operator[](const Node& key) {
3176
        if (Adaptor::inNode(key)) { return _in_map[key]; }
3179
        if (SplitNodesBase<DGR>::inNode(key)) { return _in_map[key]; }
3177 3180
        else { return _out_map[key]; }
3178 3181
      }
3179 3182

	
3180 3183
      ConstReturnValue operator[](const Node& key) const {
3181 3184
        if (Adaptor::inNode(key)) { return _in_map[key]; }
3182 3185
        else { return _out_map[key]; }
3183 3186
      }
3184 3187

	
3185 3188
    private:
3186 3189
      NodeImpl _in_map, _out_map;
3187 3190
    };
3188 3191

	
3189
    template <typename _Value>
3192
    template <typename V>
3190 3193
    class ArcMapBase
3191
      : public MapTraits<typename Parent::template ArcMap<_Value> > {
3192
      typedef typename Parent::template ArcMap<_Value> ArcImpl;
3193
      typedef typename Parent::template NodeMap<_Value> NodeImpl;
3194
      : public MapTraits<typename Parent::template ArcMap<V> > {
3195
      typedef typename Parent::template ArcMap<V> ArcImpl;
3196
      typedef typename Parent::template NodeMap<V> NodeImpl;
3194 3197
    public:
3195 3198
      typedef Arc Key;
3196
      typedef _Value Value;
3199
      typedef V Value;
3197 3200
      typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag;
3198 3201
      typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue;
3199 3202
      typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue;
3200 3203
      typedef typename MapTraits<ArcImpl>::ReturnValue Reference;
3201 3204
      typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference;
3202 3205

	
3203
      ArcMapBase(const Adaptor& adaptor)
3206
      ArcMapBase(const SplitNodesBase<DGR>& adaptor)
3204 3207
        : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {}
3205
      ArcMapBase(const Adaptor& adaptor, const Value& value)
3208
      ArcMapBase(const SplitNodesBase<DGR>& adaptor, const V& value)
3206 3209
        : _arc_map(*adaptor._digraph, value),
3207 3210
          _node_map(*adaptor._digraph, value) {}
3208 3211

	
3209
      void set(const Arc& key, const Value& val) {
3210
        if (Adaptor::origArc(key)) {
3211
          _arc_map.set(key._item.first(), val);
3212
      void set(const Arc& key, const V& val) {
3213
        if (SplitNodesBase<DGR>::origArc(key)) {
3214
          _arc_map.set(static_cast<const DigraphArc&>(key), val);
3212 3215
        } else {
3213
          _node_map.set(key._item.second(), val);
3216
          _node_map.set(static_cast<const DigraphNode&>(key), val);
3214 3217
        }
3215 3218
      }
3216 3219

	
3217 3220
      ReturnValue operator[](const Arc& key) {
3218
        if (Adaptor::origArc(key)) {
3219
          return _arc_map[key._item.first()];
3221
        if (SplitNodesBase<DGR>::origArc(key)) {
3222
          return _arc_map[static_cast<const DigraphArc&>(key)];
3220 3223
        } else {
3221
          return _node_map[key._item.second()];
3224
          return _node_map[static_cast<const DigraphNode&>(key)];
3222 3225
        }
3223 3226
      }
3224 3227

	
3225 3228
      ConstReturnValue operator[](const Arc& key) const {
3226
        if (Adaptor::origArc(key)) {
3227
          return _arc_map[key._item.first()];
3229
        if (SplitNodesBase<DGR>::origArc(key)) {
3230
          return _arc_map[static_cast<const DigraphArc&>(key)];
3228 3231
        } else {
3229
          return _node_map[key._item.second()];
3232
          return _node_map[static_cast<const DigraphNode&>(key)];
3230 3233
        }
3231 3234
      }
3232 3235

	
3233 3236
    private:
3234 3237
      ArcImpl _arc_map;
3235 3238
      NodeImpl _node_map;
3236 3239
    };
3237 3240

	
3238 3241
  public:
3239 3242

	
3240
    template <typename _Value>
3243
    template <typename V>
3241 3244
    class NodeMap
3242
      : public SubMapExtender<Adaptor, NodeMapBase<_Value> >
3243
    {
3245
      : public SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> > {
3246
      typedef SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> > Parent;
3247

	
3244 3248
    public:
3245
      typedef _Value Value;
3246
      typedef SubMapExtender<Adaptor, NodeMapBase<Value> > Parent;
3247

	
3248
      NodeMap(const Adaptor& adaptor)
3249
      typedef V Value;
3250

	
3251
      NodeMap(const SplitNodesBase<DGR>& adaptor)
3249 3252
        : Parent(adaptor) {}
3250 3253

	
3251
      NodeMap(const Adaptor& adaptor, const Value& value)
3254
      NodeMap(const SplitNodesBase<DGR>& adaptor, const V& value)
3252 3255
        : Parent(adaptor, value) {}
3253 3256

	
3254 3257
    private:
3255 3258
      NodeMap& operator=(const NodeMap& cmap) {
3256 3259
        return operator=<NodeMap>(cmap);
3257 3260
      }
3258 3261

	
3259 3262
      template <typename CMap>
3260 3263
      NodeMap& operator=(const CMap& cmap) {
3261 3264
        Parent::operator=(cmap);
3262 3265
        return *this;
3263 3266
      }
3264 3267
    };
3265 3268

	
3266
    template <typename _Value>
3269
    template <typename V>
3267 3270
    class ArcMap
3268
      : public SubMapExtender<Adaptor, ArcMapBase<_Value> >
3269
    {
3271
      : public SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> > {
3272
      typedef SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> > Parent;
3273

	
3270 3274
    public:
3271
      typedef _Value Value;
3272
      typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent;
3273

	
3274
      ArcMap(const Adaptor& adaptor)
3275
      typedef V Value;
3276

	
3277
      ArcMap(const SplitNodesBase<DGR>& adaptor)
3275 3278
        : Parent(adaptor) {}
3276 3279

	
3277
      ArcMap(const Adaptor& adaptor, const Value& value)
3280
      ArcMap(const SplitNodesBase<DGR>& adaptor, const V& value)
3278 3281
        : Parent(adaptor, value) {}
3279 3282

	
3280 3283
    private:
3281 3284
      ArcMap& operator=(const ArcMap& cmap) {
3282 3285
        return operator=<ArcMap>(cmap);
3283 3286
      }
3284 3287

	
3285 3288
      template <typename CMap>
3286 3289
      ArcMap& operator=(const CMap& cmap) {
3287 3290
        Parent::operator=(cmap);
3288 3291
        return *this;
3289 3292
      }
3290 3293
    };
3291 3294

	
3292 3295
  protected:
3293 3296

	
3294 3297
    SplitNodesBase() : _digraph(0) {}
3295 3298

	
3296
    Digraph* _digraph;
3297

	
3298
    void setDigraph(Digraph& digraph) {
3299
    DGR* _digraph;
3300

	
3301
    void initialize(Digraph& digraph) {
3299 3302
      _digraph = &digraph;
3300 3303
    }
3301 3304

	
3302 3305
  };
3303 3306

	
3304 3307
  /// \ingroup graph_adaptors
3305 3308
  ///
3306 3309
  /// \brief Adaptor class for splitting the nodes of a digraph.
3307 3310
  ///
3308 3311
  /// SplitNodes adaptor can be used for splitting each node into an
3309 3312
  /// \e in-node and an \e out-node in a digraph. Formaly, the adaptor
3310 3313
  /// replaces each node \f$ u \f$ in the digraph with two nodes,
3311 3314
  /// namely node \f$ u_{in} \f$ and node \f$ u_{out} \f$.
3312 3315
  /// If there is a \f$ (v, u) \f$ arc in the original digraph, then the
3313 3316
  /// new target of the arc will be \f$ u_{in} \f$ and similarly the
3314 3317
  /// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$.
3315 3318
  /// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$
3316 3319
  /// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph.
3317 3320
  ///
3318 3321
  /// The aim of this class is running an algorithm with respect to node
3319 3322
  /// costs or capacities if the algorithm considers only arc costs or
3320 3323
  /// capacities directly.
3321 3324
  /// In this case you can use \c SplitNodes adaptor, and set the node
3322 3325
  /// costs/capacities of the original digraph to the \e bind \e arcs
3323 3326
  /// in the adaptor.
3324 3327
  ///
3325
  /// \tparam GR The type of the adapted digraph.
3328
  /// \tparam DGR The type of the adapted digraph.
3326 3329
  /// It must conform to the \ref concepts::Digraph "Digraph" concept.
3327 3330
  /// It is implicitly \c const.
3328 3331
  ///
3329 3332
  /// \note The \c Node type of this adaptor is converible to the \c Node
3330 3333
  /// type of the adapted digraph.
3331
  template <typename GR>
3334
  template <typename DGR>
3332 3335
#ifdef DOXYGEN
3333 3336
  class SplitNodes {
3334 3337
#else
3335 3338
  class SplitNodes
3336
    : public DigraphAdaptorExtender<SplitNodesBase<const GR> > {
3339
    : public DigraphAdaptorExtender<SplitNodesBase<const DGR> > {
3337 3340
#endif
3341
    typedef DigraphAdaptorExtender<SplitNodesBase<const DGR> > Parent;
3342

	
3338 3343
  public:
3339
    typedef GR Digraph;
3340
    typedef DigraphAdaptorExtender<SplitNodesBase<const GR> > Parent;
3341

	
3342
    typedef typename Digraph::Node DigraphNode;
3343
    typedef typename Digraph::Arc DigraphArc;
3344
    typedef DGR Digraph;
3345

	
3346
    typedef typename DGR::Node DigraphNode;
3347
    typedef typename DGR::Arc DigraphArc;
3344 3348

	
3345 3349
    typedef typename Parent::Node Node;
3346 3350
    typedef typename Parent::Arc Arc;
3347 3351

	
3348 3352
    /// \brief Constructor
3349 3353
    ///
3350 3354
    /// Constructor of the adaptor.
3351
    SplitNodes(const Digraph& g) {
3352
      Parent::setDigraph(g);
3355
    SplitNodes(const DGR& g) {
3356
      Parent::initialize(g);
3353 3357
    }
3354 3358

	
3355 3359
    /// \brief Returns \c true if the given node is an in-node.
3356 3360
    ///
3357 3361
    /// Returns \c true if the given node is an in-node.
3358 3362
    static bool inNode(const Node& n) {
3359 3363
      return Parent::inNode(n);
3360 3364
    }
3361 3365

	
3362 3366
    /// \brief Returns \c true if the given node is an out-node.
3363 3367
    ///
3364 3368
    /// Returns \c true if the given node is an out-node.
3365 3369
    static bool outNode(const Node& n) {
3366 3370
      return Parent::outNode(n);
3367 3371
    }
3368 3372

	
3369 3373
    /// \brief Returns \c true if the given arc is an original arc.
3370 3374
    ///
3371 3375
    /// Returns \c true if the given arc is one of the arcs in the
3372 3376
    /// original digraph.
3373 3377
    static bool origArc(const Arc& a) {
3374 3378
      return Parent::origArc(a);
3375 3379
    }
3376 3380

	
3377 3381
    /// \brief Returns \c true if the given arc is a bind arc.
3378 3382
    ///
3379 3383
    /// Returns \c true if the given arc is a bind arc, i.e. it connects
3380 3384
    /// an in-node and an out-node.
3381 3385
    static bool bindArc(const Arc& a) {
3382 3386
      return Parent::bindArc(a);
3383 3387
    }
3384 3388

	
3385 3389
    /// \brief Returns the in-node created from the given original node.
3386 3390
    ///
3387 3391
    /// Returns the in-node created from the given original node.
3388 3392
    static Node inNode(const DigraphNode& n) {
3389 3393
      return Parent::inNode(n);
3390 3394
    }
3391 3395

	
3392 3396
    /// \brief Returns the out-node created from the given original node.
3393 3397
    ///
3394 3398
    /// Returns the out-node created from the given original node.
3395 3399
    static Node outNode(const DigraphNode& n) {
3396 3400
      return Parent::outNode(n);
3397 3401
    }
3398 3402

	
3399 3403
    /// \brief Returns the bind arc that corresponds to the given
3400 3404
    /// original node.
3401 3405
    ///
3402 3406
    /// Returns the bind arc in the adaptor that corresponds to the given
3403 3407
    /// original node, i.e. the arc connecting the in-node and out-node
3404 3408
    /// of \c n.
3405 3409
    static Arc arc(const DigraphNode& n) {
3406 3410
      return Parent::arc(n);
3407 3411
    }
3408 3412

	
3409 3413
    /// \brief Returns the arc that corresponds to the given original arc.
3410 3414
    ///
3411 3415
    /// Returns the arc in the adaptor that corresponds to the given
3412 3416
    /// original arc.
3413 3417
    static Arc arc(const DigraphArc& a) {
3414 3418
      return Parent::arc(a);
3415 3419
    }
3416 3420

	
3417 3421
    /// \brief Node map combined from two original node maps
3418 3422
    ///
3419 3423
    /// This map adaptor class adapts two node maps of the original digraph
3420 3424
    /// to get a node map of the split digraph.
3421
    /// Its value type is inherited from the first node map type
3422
    /// (\c InNodeMap).
3423
    template <typename InNodeMap, typename OutNodeMap>
3425
    /// Its value type is inherited from the first node map type (\c IN).
3426
    /// \tparam IN The type of the node map for the in-nodes. 
3427
    /// \tparam OUT The type of the node map for the out-nodes.
3428
    template <typename IN, typename OUT>
3424 3429
    class CombinedNodeMap {
3425 3430
    public:
3426 3431

	
3427 3432
      /// The key type of the map
3428 3433
      typedef Node Key;
3429 3434
      /// The value type of the map
3430
      typedef typename InNodeMap::Value Value;
3431

	
3432
      typedef typename MapTraits<InNodeMap>::ReferenceMapTag ReferenceMapTag;
3433
      typedef typename MapTraits<InNodeMap>::ReturnValue ReturnValue;
3434
      typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReturnValue;
3435
      typedef typename MapTraits<InNodeMap>::ReturnValue Reference;
3436
      typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReference;
3435
      typedef typename IN::Value Value;
3436

	
3437
      typedef typename MapTraits<IN>::ReferenceMapTag ReferenceMapTag;
3438
      typedef typename MapTraits<IN>::ReturnValue ReturnValue;
3439
      typedef typename MapTraits<IN>::ConstReturnValue ConstReturnValue;
3440
      typedef typename MapTraits<IN>::ReturnValue Reference;
3441
      typedef typename MapTraits<IN>::ConstReturnValue ConstReference;
3437 3442

	
3438 3443
      /// Constructor
3439
      CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map)
3444
      CombinedNodeMap(IN& in_map, OUT& out_map)
3440 3445
        : _in_map(in_map), _out_map(out_map) {}
3441 3446

	
3442 3447
      /// Returns the value associated with the given key.
3443 3448
      Value operator[](const Key& key) const {
3444
        if (Parent::inNode(key)) {
3449
        if (SplitNodesBase<const DGR>::inNode(key)) {
3445 3450
          return _in_map[key];
3446 3451
        } else {
3447 3452
          return _out_map[key];
3448 3453
        }
3449 3454
      }
3450 3455

	
3451 3456
      /// Returns a reference to the value associated with the given key.
3452 3457
      Value& operator[](const Key& key) {
3453
        if (Parent::inNode(key)) {
3458
        if (SplitNodesBase<const DGR>::inNode(key)) {
3454 3459
          return _in_map[key];
3455 3460
        } else {
3456 3461
          return _out_map[key];
3457 3462
        }
3458 3463
      }
3459 3464

	
3460 3465
      /// Sets the value associated with the given key.
3461 3466
      void set(const Key& key, const Value& value) {
3462
        if (Parent::inNode(key)) {
3467
        if (SplitNodesBase<const DGR>::inNode(key)) {
3463 3468
          _in_map.set(key, value);
3464 3469
        } else {
3465 3470
          _out_map.set(key, value);
3466 3471
        }
3467 3472
      }
3468 3473

	
3469 3474
    private:
3470 3475

	
3471
      InNodeMap& _in_map;
3472
      OutNodeMap& _out_map;
3476
      IN& _in_map;
3477
      OUT& _out_map;
3473 3478

	
3474 3479
    };
3475 3480

	
3476 3481

	
3477 3482
    /// \brief Returns a combined node map
3478 3483
    ///
3479 3484
    /// This function just returns a combined node map.
3480
    template <typename InNodeMap, typename OutNodeMap>
3481
    static CombinedNodeMap<InNodeMap, OutNodeMap>
3482
    combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) {
3483
      return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map);
3485
    template <typename IN, typename OUT>
3486
    static CombinedNodeMap<IN, OUT>
3487
    combinedNodeMap(IN& in_map, OUT& out_map) {
3488
      return CombinedNodeMap<IN, OUT>(in_map, out_map);
3484 3489
    }
3485 3490

	
3486
    template <typename InNodeMap, typename OutNodeMap>
3487
    static CombinedNodeMap<const InNodeMap, OutNodeMap>
3488
    combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) {
3489
      return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map);
3491
    template <typename IN, typename OUT>
3492
    static CombinedNodeMap<const IN, OUT>
3493
    combinedNodeMap(const IN& in_map, OUT& out_map) {
3494
      return CombinedNodeMap<const IN, OUT>(in_map, out_map);
3490 3495
    }
3491 3496

	
3492
    template <typename InNodeMap, typename OutNodeMap>
3493
    static CombinedNodeMap<InNodeMap, const OutNodeMap>
3494
    combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) {
3495
      return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map);
3497
    template <typename IN, typename OUT>
3498
    static CombinedNodeMap<IN, const OUT>
3499
    combinedNodeMap(IN& in_map, const OUT& out_map) {
3500
      return CombinedNodeMap<IN, const OUT>(in_map, out_map);
3496 3501
    }
3497 3502

	
3498
    template <typename InNodeMap, typename OutNodeMap>
3499
    static CombinedNodeMap<const InNodeMap, const OutNodeMap>
3500
    combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) {
3501
      return CombinedNodeMap<const InNodeMap,
3502
        const OutNodeMap>(in_map, out_map);
3503
    template <typename IN, typename OUT>
3504
    static CombinedNodeMap<const IN, const OUT>
3505
    combinedNodeMap(const IN& in_map, const OUT& out_map) {
3506
      return CombinedNodeMap<const IN, const OUT>(in_map, out_map);
3503 3507
    }
3504 3508

	
3505 3509
    /// \brief Arc map combined from an arc map and a node map of the
3506 3510
    /// original digraph.
3507 3511
    ///
3508 3512
    /// This map adaptor class adapts an arc map and a node map of the
3509 3513
    /// original digraph to get an arc map of the split digraph.
3510
    /// Its value type is inherited from the original arc map type
3511
    /// (\c ArcMap).
3512
    template <typename ArcMap, typename NodeMap>
3514
    /// Its value type is inherited from the original arc map type (\c AM).
3515
    /// \tparam AM The type of the arc map.
3516
    /// \tparam NM the type of the node map.
3517
    template <typename AM, typename NM>
3513 3518
    class CombinedArcMap {
3514 3519
    public:
3515 3520

	
3516 3521
      /// The key type of the map
3517 3522
      typedef Arc Key;
3518 3523
      /// The value type of the map
3519
      typedef typename ArcMap::Value Value;
3520

	
3521
      typedef typename MapTraits<ArcMap>::ReferenceMapTag ReferenceMapTag;
3522
      typedef typename MapTraits<ArcMap>::ReturnValue ReturnValue;
3523
      typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReturnValue;
3524
      typedef typename MapTraits<ArcMap>::ReturnValue Reference;
3525
      typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReference;
3524
      typedef typename AM::Value Value;
3525

	
3526
      typedef typename MapTraits<AM>::ReferenceMapTag ReferenceMapTag;
3527
      typedef typename MapTraits<AM>::ReturnValue ReturnValue;
3528
      typedef typename MapTraits<AM>::ConstReturnValue ConstReturnValue;
3529
      typedef typename MapTraits<AM>::ReturnValue Reference;
3530
      typedef typename MapTraits<AM>::ConstReturnValue ConstReference;
3526 3531

	
3527 3532
      /// Constructor
3528
      CombinedArcMap(ArcMap& arc_map, NodeMap& node_map)
3533
      CombinedArcMap(AM& arc_map, NM& node_map)
3529 3534
        : _arc_map(arc_map), _node_map(node_map) {}
3530 3535

	
3531 3536
      /// Returns the value associated with the given key.
3532 3537
      Value operator[](const Key& arc) const {
3533
        if (Parent::origArc(arc)) {
3538
        if (SplitNodesBase<const DGR>::origArc(arc)) {
3534 3539
          return _arc_map[arc];
3535 3540
        } else {
3536 3541
          return _node_map[arc];
3537 3542
        }
3538 3543
      }
3539 3544

	
3540 3545
      /// Returns a reference to the value associated with the given key.
3541 3546
      Value& operator[](const Key& arc) {
3542
        if (Parent::origArc(arc)) {
3547
        if (SplitNodesBase<const DGR>::origArc(arc)) {
3543 3548
          return _arc_map[arc];
3544 3549
        } else {
3545 3550
          return _node_map[arc];
3546 3551
        }
3547 3552
      }
3548 3553

	
3549 3554
      /// Sets the value associated with the given key.
3550 3555
      void set(const Arc& arc, const Value& val) {
3551
        if (Parent::origArc(arc)) {
3556
        if (SplitNodesBase<const DGR>::origArc(arc)) {
3552 3557
          _arc_map.set(arc, val);
3553 3558
        } else {
3554 3559
          _node_map.set(arc, val);
3555 3560
        }
3556 3561
      }
3557 3562

	
3558 3563
    private:
3559
      ArcMap& _arc_map;
3560
      NodeMap& _node_map;
3564

	
3565
      AM& _arc_map;
3566
      NM& _node_map;
3567

	
3561 3568
    };
3562 3569

	
3563 3570
    /// \brief Returns a combined arc map
3564 3571
    ///
3565 3572
    /// This function just returns a combined arc map.
3566 3573
    template <typename ArcMap, typename NodeMap>
3567 3574
    static CombinedArcMap<ArcMap, NodeMap>
3568 3575
    combinedArcMap(ArcMap& arc_map, NodeMap& node_map) {
3569 3576
      return CombinedArcMap<ArcMap, NodeMap>(arc_map, node_map);
3570 3577
    }
3571 3578

	
3572 3579
    template <typename ArcMap, typename NodeMap>
3573 3580
    static CombinedArcMap<const ArcMap, NodeMap>
3574 3581
    combinedArcMap(const ArcMap& arc_map, NodeMap& node_map) {
3575 3582
      return CombinedArcMap<const ArcMap, NodeMap>(arc_map, node_map);
3576 3583
    }
3577 3584

	
3578 3585
    template <typename ArcMap, typename NodeMap>
3579 3586
    static CombinedArcMap<ArcMap, const NodeMap>
3580 3587
    combinedArcMap(ArcMap& arc_map, const NodeMap& node_map) {
3581 3588
      return CombinedArcMap<ArcMap, const NodeMap>(arc_map, node_map);
3582 3589
    }
3583 3590

	
3584 3591
    template <typename ArcMap, typename NodeMap>
3585 3592
    static CombinedArcMap<const ArcMap, const NodeMap>
3586 3593
    combinedArcMap(const ArcMap& arc_map, const NodeMap& node_map) {
3587 3594
      return CombinedArcMap<const ArcMap, const NodeMap>(arc_map, node_map);
3588 3595
    }
3589 3596

	
3590 3597
  };
3591 3598

	
3592 3599
  /// \brief Returns a (read-only) SplitNodes adaptor
3593 3600
  ///
3594 3601
  /// This function just returns a (read-only) \ref SplitNodes adaptor.
3595 3602
  /// \ingroup graph_adaptors
3596 3603
  /// \relates SplitNodes
3597
  template<typename GR>
3598
  SplitNodes<GR>
3599
  splitNodes(const GR& digraph) {
3600
    return SplitNodes<GR>(digraph);
3604
  template<typename DGR>
3605
  SplitNodes<DGR>
3606
  splitNodes(const DGR& digraph) {
3607
    return SplitNodes<DGR>(digraph);
3601 3608
  }
3602 3609

	
3610
#undef LEMON_SCOPE_FIX
3611

	
3603 3612
} //namespace lemon
3604 3613

	
3605 3614
#endif //LEMON_ADAPTORS_H
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\file
20 20
///\brief Some basic non-inline functions and static global data.
21 21

	
22 22
#include<lemon/tolerance.h>
23 23
#include<lemon/core.h>
24 24
namespace lemon {
25 25

	
26
  float Tolerance<float>::def_epsilon = 1e-4;
26
  float Tolerance<float>::def_epsilon = static_cast<float>(1e-4);
27 27
  double Tolerance<double>::def_epsilon = 1e-10;
28 28
  long double Tolerance<long double>::def_epsilon = 1e-14;
29 29

	
30 30
#ifndef LEMON_ONLY_TEMPLATES
31 31
  const Invalid INVALID = Invalid();
32 32
#endif
33 33

	
34 34
} //namespace lemon
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BFS_H
20 20
#define LEMON_BFS_H
21 21

	
22 22
///\ingroup search
23 23
///\file
24 24
///\brief BFS algorithm.
25 25

	
26 26
#include <lemon/list_graph.h>
27 27
#include <lemon/bits/path_dump.h>
28 28
#include <lemon/core.h>
29 29
#include <lemon/error.h>
30 30
#include <lemon/maps.h>
31 31
#include <lemon/path.h>
32 32

	
33 33
namespace lemon {
34 34

	
35 35
  ///Default traits class of Bfs class.
36 36

	
37 37
  ///Default traits class of Bfs class.
38 38
  ///\tparam GR Digraph type.
39 39
  template<class GR>
40 40
  struct BfsDefaultTraits
41 41
  {
42 42
    ///The type of the digraph the algorithm runs on.
43 43
    typedef GR Digraph;
44 44

	
45 45
    ///\brief The type of the map that stores the predecessor
46 46
    ///arcs of the shortest paths.
47 47
    ///
48 48
    ///The type of the map that stores the predecessor
49 49
    ///arcs of the shortest paths.
50
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
50
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
51 51
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
52
    ///Instantiates a PredMap.
52
    ///Instantiates a \c PredMap.
53 53

	
54
    ///This function instantiates a PredMap.
54
    ///This function instantiates a \ref PredMap.
55 55
    ///\param g is the digraph, to which we would like to define the
56
    ///PredMap.
56
    ///\ref PredMap.
57 57
    static PredMap *createPredMap(const Digraph &g)
58 58
    {
59 59
      return new PredMap(g);
60 60
    }
61 61

	
62 62
    ///The type of the map that indicates which nodes are processed.
63 63

	
64 64
    ///The type of the map that indicates which nodes are processed.
65
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
65
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
66
    ///By default it is a NullMap.
66 67
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
67
    ///Instantiates a ProcessedMap.
68
    ///Instantiates a \c ProcessedMap.
68 69

	
69
    ///This function instantiates a ProcessedMap.
70
    ///This function instantiates a \ref ProcessedMap.
70 71
    ///\param g is the digraph, to which
71
    ///we would like to define the ProcessedMap
72
    ///we would like to define the \ref ProcessedMap
72 73
#ifdef DOXYGEN
73 74
    static ProcessedMap *createProcessedMap(const Digraph &g)
74 75
#else
75 76
    static ProcessedMap *createProcessedMap(const Digraph &)
76 77
#endif
77 78
    {
78 79
      return new ProcessedMap();
79 80
    }
80 81

	
81 82
    ///The type of the map that indicates which nodes are reached.
82 83

	
83 84
    ///The type of the map that indicates which nodes are reached.
84
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
85
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
85 86
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
86
    ///Instantiates a ReachedMap.
87
    ///Instantiates a \c ReachedMap.
87 88

	
88
    ///This function instantiates a ReachedMap.
89
    ///This function instantiates a \ref ReachedMap.
89 90
    ///\param g is the digraph, to which
90
    ///we would like to define the ReachedMap.
91
    ///we would like to define the \ref ReachedMap.
91 92
    static ReachedMap *createReachedMap(const Digraph &g)
92 93
    {
93 94
      return new ReachedMap(g);
94 95
    }
95 96

	
96 97
    ///The type of the map that stores the distances of the nodes.
97 98

	
98 99
    ///The type of the map that stores the distances of the nodes.
99
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
100
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
100 101
    typedef typename Digraph::template NodeMap<int> DistMap;
101
    ///Instantiates a DistMap.
102
    ///Instantiates a \c DistMap.
102 103

	
103
    ///This function instantiates a DistMap.
104
    ///This function instantiates a \ref DistMap.
104 105
    ///\param g is the digraph, to which we would like to define the
105
    ///DistMap.
106
    ///\ref DistMap.
106 107
    static DistMap *createDistMap(const Digraph &g)
107 108
    {
108 109
      return new DistMap(g);
109 110
    }
110 111
  };
111 112

	
112 113
  ///%BFS algorithm class.
113 114

	
114 115
  ///\ingroup search
115 116
  ///This class provides an efficient implementation of the %BFS algorithm.
116 117
  ///
117 118
  ///There is also a \ref bfs() "function-type interface" for the BFS
118 119
  ///algorithm, which is convenient in the simplier cases and it can be
119 120
  ///used easier.
120 121
  ///
121 122
  ///\tparam GR The type of the digraph the algorithm runs on.
122 123
  ///The default type is \ref ListDigraph.
123 124
#ifdef DOXYGEN
124 125
  template <typename GR,
125 126
            typename TR>
126 127
#else
127 128
  template <typename GR=ListDigraph,
128 129
            typename TR=BfsDefaultTraits<GR> >
129 130
#endif
130 131
  class Bfs {
131 132
  public:
132 133

	
133 134
    ///The type of the digraph the algorithm runs on.
134 135
    typedef typename TR::Digraph Digraph;
135 136

	
136 137
    ///\brief The type of the map that stores the predecessor arcs of the
137 138
    ///shortest paths.
138 139
    typedef typename TR::PredMap PredMap;
139 140
    ///The type of the map that stores the distances of the nodes.
140 141
    typedef typename TR::DistMap DistMap;
141 142
    ///The type of the map that indicates which nodes are reached.
142 143
    typedef typename TR::ReachedMap ReachedMap;
143 144
    ///The type of the map that indicates which nodes are processed.
144 145
    typedef typename TR::ProcessedMap ProcessedMap;
145 146
    ///The type of the paths.
146 147
    typedef PredMapPath<Digraph, PredMap> Path;
147 148

	
148 149
    ///The \ref BfsDefaultTraits "traits class" of the algorithm.
149 150
    typedef TR Traits;
150 151

	
151 152
  private:
152 153

	
153 154
    typedef typename Digraph::Node Node;
154 155
    typedef typename Digraph::NodeIt NodeIt;
155 156
    typedef typename Digraph::Arc Arc;
156 157
    typedef typename Digraph::OutArcIt OutArcIt;
157 158

	
158 159
    //Pointer to the underlying digraph.
159 160
    const Digraph *G;
160 161
    //Pointer to the map of predecessor arcs.
161 162
    PredMap *_pred;
162 163
    //Indicates if _pred is locally allocated (true) or not.
163 164
    bool local_pred;
164 165
    //Pointer to the map of distances.
165 166
    DistMap *_dist;
166 167
    //Indicates if _dist is locally allocated (true) or not.
167 168
    bool local_dist;
168 169
    //Pointer to the map of reached status of the nodes.
169 170
    ReachedMap *_reached;
170 171
    //Indicates if _reached is locally allocated (true) or not.
171 172
    bool local_reached;
172 173
    //Pointer to the map of processed status of the nodes.
173 174
    ProcessedMap *_processed;
174 175
    //Indicates if _processed is locally allocated (true) or not.
175 176
    bool local_processed;
176 177

	
177 178
    std::vector<typename Digraph::Node> _queue;
178 179
    int _queue_head,_queue_tail,_queue_next_dist;
179 180
    int _curr_dist;
180 181

	
181 182
    //Creates the maps if necessary.
182 183
    void create_maps()
183 184
    {
184 185
      if(!_pred) {
185 186
        local_pred = true;
186 187
        _pred = Traits::createPredMap(*G);
187 188
      }
188 189
      if(!_dist) {
189 190
        local_dist = true;
190 191
        _dist = Traits::createDistMap(*G);
191 192
      }
192 193
      if(!_reached) {
193 194
        local_reached = true;
194 195
        _reached = Traits::createReachedMap(*G);
195 196
      }
196 197
      if(!_processed) {
197 198
        local_processed = true;
198 199
        _processed = Traits::createProcessedMap(*G);
199 200
      }
200 201
    }
201 202

	
202 203
  protected:
203 204

	
204 205
    Bfs() {}
205 206

	
206 207
  public:
207 208

	
208 209
    typedef Bfs Create;
209 210

	
210 211
    ///\name Named Template Parameters
211 212

	
212 213
    ///@{
213 214

	
214 215
    template <class T>
215 216
    struct SetPredMapTraits : public Traits {
216 217
      typedef T PredMap;
217 218
      static PredMap *createPredMap(const Digraph &)
218 219
      {
219 220
        LEMON_ASSERT(false, "PredMap is not initialized");
220 221
        return 0; // ignore warnings
221 222
      }
222 223
    };
223 224
    ///\brief \ref named-templ-param "Named parameter" for setting
224
    ///PredMap type.
225
    ///\c PredMap type.
225 226
    ///
226 227
    ///\ref named-templ-param "Named parameter" for setting
227
    ///PredMap type.
228
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
228
    ///\c PredMap type.
229
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
229 230
    template <class T>
230 231
    struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
231 232
      typedef Bfs< Digraph, SetPredMapTraits<T> > Create;
232 233
    };
233 234

	
234 235
    template <class T>
235 236
    struct SetDistMapTraits : public Traits {
236 237
      typedef T DistMap;
237 238
      static DistMap *createDistMap(const Digraph &)
238 239
      {
239 240
        LEMON_ASSERT(false, "DistMap is not initialized");
240 241
        return 0; // ignore warnings
241 242
      }
242 243
    };
243 244
    ///\brief \ref named-templ-param "Named parameter" for setting
244
    ///DistMap type.
245
    ///\c DistMap type.
245 246
    ///
246 247
    ///\ref named-templ-param "Named parameter" for setting
247
    ///DistMap type.
248
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
248
    ///\c DistMap type.
249
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
249 250
    template <class T>
250 251
    struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
251 252
      typedef Bfs< Digraph, SetDistMapTraits<T> > Create;
252 253
    };
253 254

	
254 255
    template <class T>
255 256
    struct SetReachedMapTraits : public Traits {
256 257
      typedef T ReachedMap;
257 258
      static ReachedMap *createReachedMap(const Digraph &)
258 259
      {
259 260
        LEMON_ASSERT(false, "ReachedMap is not initialized");
260 261
        return 0; // ignore warnings
261 262
      }
262 263
    };
263 264
    ///\brief \ref named-templ-param "Named parameter" for setting
264
    ///ReachedMap type.
265
    ///\c ReachedMap type.
265 266
    ///
266 267
    ///\ref named-templ-param "Named parameter" for setting
267
    ///ReachedMap type.
268
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
268
    ///\c ReachedMap type.
269
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
269 270
    template <class T>
270 271
    struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
271 272
      typedef Bfs< Digraph, SetReachedMapTraits<T> > Create;
272 273
    };
273 274

	
274 275
    template <class T>
275 276
    struct SetProcessedMapTraits : public Traits {
276 277
      typedef T ProcessedMap;
277 278
      static ProcessedMap *createProcessedMap(const Digraph &)
278 279
      {
279 280
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
280 281
        return 0; // ignore warnings
281 282
      }
282 283
    };
283 284
    ///\brief \ref named-templ-param "Named parameter" for setting
284
    ///ProcessedMap type.
285
    ///\c ProcessedMap type.
285 286
    ///
286 287
    ///\ref named-templ-param "Named parameter" for setting
287
    ///ProcessedMap type.
288
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
288
    ///\c ProcessedMap type.
289
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
289 290
    template <class T>
290 291
    struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
291 292
      typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create;
292 293
    };
293 294

	
294 295
    struct SetStandardProcessedMapTraits : public Traits {
295 296
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
296 297
      static ProcessedMap *createProcessedMap(const Digraph &g)
297 298
      {
298 299
        return new ProcessedMap(g);
299 300
        return 0; // ignore warnings
300 301
      }
301 302
    };
302 303
    ///\brief \ref named-templ-param "Named parameter" for setting
303
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
304
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
304 305
    ///
305 306
    ///\ref named-templ-param "Named parameter" for setting
306
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
307
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
307 308
    ///If you don't set it explicitly, it will be automatically allocated.
308 309
    struct SetStandardProcessedMap :
309 310
      public Bfs< Digraph, SetStandardProcessedMapTraits > {
310 311
      typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create;
311 312
    };
312 313

	
313 314
    ///@}
314 315

	
315 316
  public:
316 317

	
317 318
    ///Constructor.
318 319

	
319 320
    ///Constructor.
320 321
    ///\param g The digraph the algorithm runs on.
321 322
    Bfs(const Digraph &g) :
322 323
      G(&g),
323 324
      _pred(NULL), local_pred(false),
324 325
      _dist(NULL), local_dist(false),
325 326
      _reached(NULL), local_reached(false),
326 327
      _processed(NULL), local_processed(false)
327 328
    { }
328 329

	
329 330
    ///Destructor.
330 331
    ~Bfs()
331 332
    {
332 333
      if(local_pred) delete _pred;
333 334
      if(local_dist) delete _dist;
334 335
      if(local_reached) delete _reached;
335 336
      if(local_processed) delete _processed;
336 337
    }
337 338

	
338 339
    ///Sets the map that stores the predecessor arcs.
339 340

	
340 341
    ///Sets the map that stores the predecessor arcs.
341 342
    ///If you don't use this function before calling \ref run(Node) "run()"
342 343
    ///or \ref init(), an instance will be allocated automatically.
343 344
    ///The destructor deallocates this automatically allocated map,
344 345
    ///of course.
345 346
    ///\return <tt> (*this) </tt>
346 347
    Bfs &predMap(PredMap &m)
347 348
    {
348 349
      if(local_pred) {
349 350
        delete _pred;
350 351
        local_pred=false;
351 352
      }
352 353
      _pred = &m;
353 354
      return *this;
354 355
    }
355 356

	
356 357
    ///Sets the map that indicates which nodes are reached.
357 358

	
358 359
    ///Sets the map that indicates which nodes are reached.
359 360
    ///If you don't use this function before calling \ref run(Node) "run()"
360 361
    ///or \ref init(), an instance will be allocated automatically.
361 362
    ///The destructor deallocates this automatically allocated map,
362 363
    ///of course.
363 364
    ///\return <tt> (*this) </tt>
364 365
    Bfs &reachedMap(ReachedMap &m)
365 366
    {
366 367
      if(local_reached) {
367 368
        delete _reached;
368 369
        local_reached=false;
369 370
      }
370 371
      _reached = &m;
371 372
      return *this;
372 373
    }
373 374

	
374 375
    ///Sets the map that indicates which nodes are processed.
375 376

	
376 377
    ///Sets the map that indicates which nodes are processed.
377 378
    ///If you don't use this function before calling \ref run(Node) "run()"
378 379
    ///or \ref init(), an instance will be allocated automatically.
379 380
    ///The destructor deallocates this automatically allocated map,
380 381
    ///of course.
381 382
    ///\return <tt> (*this) </tt>
382 383
    Bfs &processedMap(ProcessedMap &m)
383 384
    {
384 385
      if(local_processed) {
385 386
        delete _processed;
386 387
        local_processed=false;
387 388
      }
388 389
      _processed = &m;
389 390
      return *this;
390 391
    }
391 392

	
392 393
    ///Sets the map that stores the distances of the nodes.
393 394

	
394 395
    ///Sets the map that stores the distances of the nodes calculated by
395 396
    ///the algorithm.
396 397
    ///If you don't use this function before calling \ref run(Node) "run()"
397 398
    ///or \ref init(), an instance will be allocated automatically.
398 399
    ///The destructor deallocates this automatically allocated map,
399 400
    ///of course.
400 401
    ///\return <tt> (*this) </tt>
401 402
    Bfs &distMap(DistMap &m)
402 403
    {
403 404
      if(local_dist) {
404 405
        delete _dist;
405 406
        local_dist=false;
406 407
      }
407 408
      _dist = &m;
408 409
      return *this;
409 410
    }
410 411

	
411 412
  public:
412 413

	
413 414
    ///\name Execution Control
414 415
    ///The simplest way to execute the BFS algorithm is to use one of the
415 416
    ///member functions called \ref run(Node) "run()".\n
416
    ///If you need more control on the execution, first you have to call
417
    ///\ref init(), then you can add several source nodes with
417
    ///If you need better control on the execution, you have to call
418
    ///\ref init() first, then you can add several source nodes with
418 419
    ///\ref addSource(). Finally the actual path computation can be
419 420
    ///performed with one of the \ref start() functions.
420 421

	
421 422
    ///@{
422 423

	
423 424
    ///\brief Initializes the internal data structures.
424 425
    ///
425 426
    ///Initializes the internal data structures.
426 427
    void init()
427 428
    {
428 429
      create_maps();
429 430
      _queue.resize(countNodes(*G));
430 431
      _queue_head=_queue_tail=0;
431 432
      _curr_dist=1;
432 433
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
433 434
        _pred->set(u,INVALID);
434 435
        _reached->set(u,false);
435 436
        _processed->set(u,false);
436 437
      }
437 438
    }
438 439

	
439 440
    ///Adds a new source node.
440 441

	
441 442
    ///Adds a new source node to the set of nodes to be processed.
442 443
    ///
443 444
    void addSource(Node s)
444 445
    {
445 446
      if(!(*_reached)[s])
446 447
        {
447 448
          _reached->set(s,true);
448 449
          _pred->set(s,INVALID);
449 450
          _dist->set(s,0);
450 451
          _queue[_queue_head++]=s;
451 452
          _queue_next_dist=_queue_head;
452 453
        }
453 454
    }
454 455

	
455 456
    ///Processes the next node.
456 457

	
457 458
    ///Processes the next node.
458 459
    ///
459 460
    ///\return The processed node.
460 461
    ///
461 462
    ///\pre The queue must not be empty.
462 463
    Node processNextNode()
463 464
    {
464 465
      if(_queue_tail==_queue_next_dist) {
465 466
        _curr_dist++;
466 467
        _queue_next_dist=_queue_head;
467 468
      }
468 469
      Node n=_queue[_queue_tail++];
469 470
      _processed->set(n,true);
470 471
      Node m;
471 472
      for(OutArcIt e(*G,n);e!=INVALID;++e)
472 473
        if(!(*_reached)[m=G->target(e)]) {
473 474
          _queue[_queue_head++]=m;
474 475
          _reached->set(m,true);
475 476
          _pred->set(m,e);
476 477
          _dist->set(m,_curr_dist);
477 478
        }
478 479
      return n;
479 480
    }
480 481

	
481 482
    ///Processes the next node.
482 483

	
483 484
    ///Processes the next node and checks if the given target node
484 485
    ///is reached. If the target node is reachable from the processed
485 486
    ///node, then the \c reach parameter will be set to \c true.
486 487
    ///
487 488
    ///\param target The target node.
488 489
    ///\retval reach Indicates if the target node is reached.
489 490
    ///It should be initially \c false.
490 491
    ///
491 492
    ///\return The processed node.
492 493
    ///
493 494
    ///\pre The queue must not be empty.
494 495
    Node processNextNode(Node target, bool& reach)
495 496
    {
496 497
      if(_queue_tail==_queue_next_dist) {
497 498
        _curr_dist++;
498 499
        _queue_next_dist=_queue_head;
499 500
      }
500 501
      Node n=_queue[_queue_tail++];
501 502
      _processed->set(n,true);
502 503
      Node m;
503 504
      for(OutArcIt e(*G,n);e!=INVALID;++e)
504 505
        if(!(*_reached)[m=G->target(e)]) {
505 506
          _queue[_queue_head++]=m;
506 507
          _reached->set(m,true);
507 508
          _pred->set(m,e);
508 509
          _dist->set(m,_curr_dist);
509 510
          reach = reach || (target == m);
510 511
        }
511 512
      return n;
512 513
    }
513 514

	
... ...
@@ -644,882 +645,878 @@
644 645
    ///    b.processNextNode(nm, rnode);
645 646
    ///  }
646 647
    ///  return rnode;
647 648
    ///\endcode
648 649
    template<class NodeBoolMap>
649 650
    Node start(const NodeBoolMap &nm)
650 651
    {
651 652
      Node rnode = INVALID;
652 653
      while ( !emptyQueue() && rnode == INVALID ) {
653 654
        processNextNode(nm, rnode);
654 655
      }
655 656
      return rnode;
656 657
    }
657 658

	
658 659
    ///Runs the algorithm from the given source node.
659 660

	
660 661
    ///This method runs the %BFS algorithm from node \c s
661 662
    ///in order to compute the shortest path to each node.
662 663
    ///
663 664
    ///The algorithm computes
664 665
    ///- the shortest path tree,
665 666
    ///- the distance of each node from the root.
666 667
    ///
667 668
    ///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
668 669
    ///\code
669 670
    ///  b.init();
670 671
    ///  b.addSource(s);
671 672
    ///  b.start();
672 673
    ///\endcode
673 674
    void run(Node s) {
674 675
      init();
675 676
      addSource(s);
676 677
      start();
677 678
    }
678 679

	
679 680
    ///Finds the shortest path between \c s and \c t.
680 681

	
681 682
    ///This method runs the %BFS algorithm from node \c s
682 683
    ///in order to compute the shortest path to node \c t
683 684
    ///(it stops searching when \c t is processed).
684 685
    ///
685 686
    ///\return \c true if \c t is reachable form \c s.
686 687
    ///
687 688
    ///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a
688 689
    ///shortcut of the following code.
689 690
    ///\code
690 691
    ///  b.init();
691 692
    ///  b.addSource(s);
692 693
    ///  b.start(t);
693 694
    ///\endcode
694 695
    bool run(Node s,Node t) {
695 696
      init();
696 697
      addSource(s);
697 698
      start(t);
698 699
      return reached(t);
699 700
    }
700 701

	
701 702
    ///Runs the algorithm to visit all nodes in the digraph.
702 703

	
703 704
    ///This method runs the %BFS algorithm in order to
704 705
    ///compute the shortest path to each node.
705 706
    ///
706 707
    ///The algorithm computes
707 708
    ///- the shortest path tree (forest),
708 709
    ///- the distance of each node from the root(s).
709 710
    ///
710 711
    ///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
711 712
    ///\code
712 713
    ///  b.init();
713 714
    ///  for (NodeIt n(gr); n != INVALID; ++n) {
714 715
    ///    if (!b.reached(n)) {
715 716
    ///      b.addSource(n);
716 717
    ///      b.start();
717 718
    ///    }
718 719
    ///  }
719 720
    ///\endcode
720 721
    void run() {
721 722
      init();
722 723
      for (NodeIt n(*G); n != INVALID; ++n) {
723 724
        if (!reached(n)) {
724 725
          addSource(n);
725 726
          start();
726 727
        }
727 728
      }
728 729
    }
729 730

	
730 731
    ///@}
731 732

	
732 733
    ///\name Query Functions
733 734
    ///The results of the BFS algorithm can be obtained using these
734 735
    ///functions.\n
735 736
    ///Either \ref run(Node) "run()" or \ref start() should be called
736 737
    ///before using them.
737 738

	
738 739
    ///@{
739 740

	
740
    ///The shortest path to a node.
741
    ///The shortest path to the given node.
741 742

	
742
    ///Returns the shortest path to a node.
743
    ///Returns the shortest path to the given node from the root(s).
743 744
    ///
744 745
    ///\warning \c t should be reached from the root(s).
745 746
    ///
746 747
    ///\pre Either \ref run(Node) "run()" or \ref init()
747 748
    ///must be called before using this function.
748 749
    Path path(Node t) const { return Path(*G, *_pred, t); }
749 750

	
750
    ///The distance of a node from the root(s).
751
    ///The distance of the given node from the root(s).
751 752

	
752
    ///Returns the distance of a node from the root(s).
753
    ///Returns the distance of the given node from the root(s).
753 754
    ///
754 755
    ///\warning If node \c v is not reached from the root(s), then
755 756
    ///the return value of this function is undefined.
756 757
    ///
757 758
    ///\pre Either \ref run(Node) "run()" or \ref init()
758 759
    ///must be called before using this function.
759 760
    int dist(Node v) const { return (*_dist)[v]; }
760 761

	
761
    ///Returns the 'previous arc' of the shortest path tree for a node.
762

	
762
    ///\brief Returns the 'previous arc' of the shortest path tree for
763
    ///the given node.
764
    ///
763 765
    ///This function returns the 'previous arc' of the shortest path
764 766
    ///tree for the node \c v, i.e. it returns the last arc of a
765 767
    ///shortest path from a root to \c v. It is \c INVALID if \c v
766 768
    ///is not reached from the root(s) or if \c v is a root.
767 769
    ///
768 770
    ///The shortest path tree used here is equal to the shortest path
769
    ///tree used in \ref predNode().
771
    ///tree used in \ref predNode() and \ref predMap().
770 772
    ///
771 773
    ///\pre Either \ref run(Node) "run()" or \ref init()
772 774
    ///must be called before using this function.
773 775
    Arc predArc(Node v) const { return (*_pred)[v];}
774 776

	
775
    ///Returns the 'previous node' of the shortest path tree for a node.
776

	
777
    ///\brief Returns the 'previous node' of the shortest path tree for
778
    ///the given node.
779
    ///
777 780
    ///This function returns the 'previous node' of the shortest path
778 781
    ///tree for the node \c v, i.e. it returns the last but one node
779
    ///from a shortest path from a root to \c v. It is \c INVALID
782
    ///of a shortest path from a root to \c v. It is \c INVALID
780 783
    ///if \c v is not reached from the root(s) or if \c v is a root.
781 784
    ///
782 785
    ///The shortest path tree used here is equal to the shortest path
783
    ///tree used in \ref predArc().
786
    ///tree used in \ref predArc() and \ref predMap().
784 787
    ///
785 788
    ///\pre Either \ref run(Node) "run()" or \ref init()
786 789
    ///must be called before using this function.
787 790
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
788 791
                                  G->source((*_pred)[v]); }
789 792

	
790 793
    ///\brief Returns a const reference to the node map that stores the
791 794
    /// distances of the nodes.
792 795
    ///
793 796
    ///Returns a const reference to the node map that stores the distances
794 797
    ///of the nodes calculated by the algorithm.
795 798
    ///
796 799
    ///\pre Either \ref run(Node) "run()" or \ref init()
797 800
    ///must be called before using this function.
798 801
    const DistMap &distMap() const { return *_dist;}
799 802

	
800 803
    ///\brief Returns a const reference to the node map that stores the
801 804
    ///predecessor arcs.
802 805
    ///
803 806
    ///Returns a const reference to the node map that stores the predecessor
804
    ///arcs, which form the shortest path tree.
807
    ///arcs, which form the shortest path tree (forest).
805 808
    ///
806 809
    ///\pre Either \ref run(Node) "run()" or \ref init()
807 810
    ///must be called before using this function.
808 811
    const PredMap &predMap() const { return *_pred;}
809 812

	
810
    ///Checks if a node is reached from the root(s).
813
    ///Checks if the given node is reached from the root(s).
811 814

	
812 815
    ///Returns \c true if \c v is reached from the root(s).
813 816
    ///
814 817
    ///\pre Either \ref run(Node) "run()" or \ref init()
815 818
    ///must be called before using this function.
816 819
    bool reached(Node v) const { return (*_reached)[v]; }
817 820

	
818 821
    ///@}
819 822
  };
820 823

	
821 824
  ///Default traits class of bfs() function.
822 825

	
823 826
  ///Default traits class of bfs() function.
824 827
  ///\tparam GR Digraph type.
825 828
  template<class GR>
826 829
  struct BfsWizardDefaultTraits
827 830
  {
828 831
    ///The type of the digraph the algorithm runs on.
829 832
    typedef GR Digraph;
830 833

	
831 834
    ///\brief The type of the map that stores the predecessor
832 835
    ///arcs of the shortest paths.
833 836
    ///
834 837
    ///The type of the map that stores the predecessor
835 838
    ///arcs of the shortest paths.
836
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
839
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
837 840
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
838 841
    ///Instantiates a PredMap.
839 842

	
840 843
    ///This function instantiates a PredMap.
841 844
    ///\param g is the digraph, to which we would like to define the
842 845
    ///PredMap.
843 846
    static PredMap *createPredMap(const Digraph &g)
844 847
    {
845 848
      return new PredMap(g);
846 849
    }
847 850

	
848 851
    ///The type of the map that indicates which nodes are processed.
849 852

	
850 853
    ///The type of the map that indicates which nodes are processed.
851
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
854
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
852 855
    ///By default it is a NullMap.
853 856
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
854 857
    ///Instantiates a ProcessedMap.
855 858

	
856 859
    ///This function instantiates a ProcessedMap.
857 860
    ///\param g is the digraph, to which
858 861
    ///we would like to define the ProcessedMap.
859 862
#ifdef DOXYGEN
860 863
    static ProcessedMap *createProcessedMap(const Digraph &g)
861 864
#else
862 865
    static ProcessedMap *createProcessedMap(const Digraph &)
863 866
#endif
864 867
    {
865 868
      return new ProcessedMap();
866 869
    }
867 870

	
868 871
    ///The type of the map that indicates which nodes are reached.
869 872

	
870 873
    ///The type of the map that indicates which nodes are reached.
871
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
874
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
872 875
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
873 876
    ///Instantiates a ReachedMap.
874 877

	
875 878
    ///This function instantiates a ReachedMap.
876 879
    ///\param g is the digraph, to which
877 880
    ///we would like to define the ReachedMap.
878 881
    static ReachedMap *createReachedMap(const Digraph &g)
879 882
    {
880 883
      return new ReachedMap(g);
881 884
    }
882 885

	
883 886
    ///The type of the map that stores the distances of the nodes.
884 887

	
885 888
    ///The type of the map that stores the distances of the nodes.
886
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
889
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
887 890
    typedef typename Digraph::template NodeMap<int> DistMap;
888 891
    ///Instantiates a DistMap.
889 892

	
890 893
    ///This function instantiates a DistMap.
891 894
    ///\param g is the digraph, to which we would like to define
892 895
    ///the DistMap
893 896
    static DistMap *createDistMap(const Digraph &g)
894 897
    {
895 898
      return new DistMap(g);
896 899
    }
897 900

	
898 901
    ///The type of the shortest paths.
899 902

	
900 903
    ///The type of the shortest paths.
901
    ///It must meet the \ref concepts::Path "Path" concept.
904
    ///It must conform to the \ref concepts::Path "Path" concept.
902 905
    typedef lemon::Path<Digraph> Path;
903 906
  };
904 907

	
905 908
  /// Default traits class used by BfsWizard
906 909

	
907
  /// To make it easier to use Bfs algorithm
908
  /// we have created a wizard class.
909
  /// This \ref BfsWizard class needs default traits,
910
  /// as well as the \ref Bfs class.
911
  /// The \ref BfsWizardBase is a class to be the default traits of the
912
  /// \ref BfsWizard class.
910
  /// Default traits class used by BfsWizard.
911
  /// \tparam GR The type of the digraph.
913 912
  template<class GR>
914 913
  class BfsWizardBase : public BfsWizardDefaultTraits<GR>
915 914
  {
916 915

	
917 916
    typedef BfsWizardDefaultTraits<GR> Base;
918 917
  protected:
919 918
    //The type of the nodes in the digraph.
920 919
    typedef typename Base::Digraph::Node Node;
921 920

	
922 921
    //Pointer to the digraph the algorithm runs on.
923 922
    void *_g;
924 923
    //Pointer to the map of reached nodes.
925 924
    void *_reached;
926 925
    //Pointer to the map of processed nodes.
927 926
    void *_processed;
928 927
    //Pointer to the map of predecessors arcs.
929 928
    void *_pred;
930 929
    //Pointer to the map of distances.
931 930
    void *_dist;
932 931
    //Pointer to the shortest path to the target node.
933 932
    void *_path;
934 933
    //Pointer to the distance of the target node.
935 934
    int *_di;
936 935

	
937 936
    public:
938 937
    /// Constructor.
939 938

	
940
    /// This constructor does not require parameters, therefore it initiates
939
    /// This constructor does not require parameters, it initiates
941 940
    /// all of the attributes to \c 0.
942 941
    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
943 942
                      _dist(0), _path(0), _di(0) {}
944 943

	
945 944
    /// Constructor.
946 945

	
947 946
    /// This constructor requires one parameter,
948 947
    /// others are initiated to \c 0.
949 948
    /// \param g The digraph the algorithm runs on.
950 949
    BfsWizardBase(const GR &g) :
951 950
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
952 951
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
953 952

	
954 953
  };
955 954

	
956 955
  /// Auxiliary class for the function-type interface of BFS algorithm.
957 956

	
958 957
  /// This auxiliary class is created to implement the
959 958
  /// \ref bfs() "function-type interface" of \ref Bfs algorithm.
960 959
  /// It does not have own \ref run(Node) "run()" method, it uses the
961 960
  /// functions and features of the plain \ref Bfs.
962 961
  ///
963 962
  /// This class should only be used through the \ref bfs() function,
964 963
  /// which makes it easier to use the algorithm.
965 964
  template<class TR>
966 965
  class BfsWizard : public TR
967 966
  {
968 967
    typedef TR Base;
969 968

	
970
    ///The type of the digraph the algorithm runs on.
971 969
    typedef typename TR::Digraph Digraph;
972 970

	
973 971
    typedef typename Digraph::Node Node;
974 972
    typedef typename Digraph::NodeIt NodeIt;
975 973
    typedef typename Digraph::Arc Arc;
976 974
    typedef typename Digraph::OutArcIt OutArcIt;
977 975

	
978
    ///\brief The type of the map that stores the predecessor
979
    ///arcs of the shortest paths.
980 976
    typedef typename TR::PredMap PredMap;
981
    ///\brief The type of the map that stores the distances of the nodes.
982 977
    typedef typename TR::DistMap DistMap;
983
    ///\brief The type of the map that indicates which nodes are reached.
984 978
    typedef typename TR::ReachedMap ReachedMap;
985
    ///\brief The type of the map that indicates which nodes are processed.
986 979
    typedef typename TR::ProcessedMap ProcessedMap;
987
    ///The type of the shortest paths
988 980
    typedef typename TR::Path Path;
989 981

	
990 982
  public:
991 983

	
992 984
    /// Constructor.
993 985
    BfsWizard() : TR() {}
994 986

	
995 987
    /// Constructor that requires parameters.
996 988

	
997 989
    /// Constructor that requires parameters.
998 990
    /// These parameters will be the default values for the traits class.
999 991
    /// \param g The digraph the algorithm runs on.
1000 992
    BfsWizard(const Digraph &g) :
1001 993
      TR(g) {}
1002 994

	
1003 995
    ///Copy constructor
1004 996
    BfsWizard(const TR &b) : TR(b) {}
1005 997

	
1006 998
    ~BfsWizard() {}
1007 999

	
1008 1000
    ///Runs BFS algorithm from the given source node.
1009 1001

	
1010 1002
    ///This method runs BFS algorithm from node \c s
1011 1003
    ///in order to compute the shortest path to each node.
1012 1004
    void run(Node s)
1013 1005
    {
1014 1006
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
1015 1007
      if (Base::_pred)
1016 1008
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1017 1009
      if (Base::_dist)
1018 1010
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1019 1011
      if (Base::_reached)
1020 1012
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
1021 1013
      if (Base::_processed)
1022 1014
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1023 1015
      if (s!=INVALID)
1024 1016
        alg.run(s);
1025 1017
      else
1026 1018
        alg.run();
1027 1019
    }
1028 1020

	
1029 1021
    ///Finds the shortest path between \c s and \c t.
1030 1022

	
1031 1023
    ///This method runs BFS algorithm from node \c s
1032 1024
    ///in order to compute the shortest path to node \c t
1033 1025
    ///(it stops searching when \c t is processed).
1034 1026
    ///
1035 1027
    ///\return \c true if \c t is reachable form \c s.
1036 1028
    bool run(Node s, Node t)
1037 1029
    {
1038 1030
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
1039 1031
      if (Base::_pred)
1040 1032
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1041 1033
      if (Base::_dist)
1042 1034
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1043 1035
      if (Base::_reached)
1044 1036
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
1045 1037
      if (Base::_processed)
1046 1038
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1047 1039
      alg.run(s,t);
1048 1040
      if (Base::_path)
1049 1041
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
1050 1042
      if (Base::_di)
1051 1043
        *Base::_di = alg.dist(t);
1052 1044
      return alg.reached(t);
1053 1045
    }
1054 1046

	
1055 1047
    ///Runs BFS algorithm to visit all nodes in the digraph.
1056 1048

	
1057 1049
    ///This method runs BFS algorithm in order to compute
1058 1050
    ///the shortest path to each node.
1059 1051
    void run()
1060 1052
    {
1061 1053
      run(INVALID);
1062 1054
    }
1063 1055

	
1064 1056
    template<class T>
1065 1057
    struct SetPredMapBase : public Base {
1066 1058
      typedef T PredMap;
1067 1059
      static PredMap *createPredMap(const Digraph &) { return 0; };
1068 1060
      SetPredMapBase(const TR &b) : TR(b) {}
1069 1061
    };
1070
    ///\brief \ref named-func-param "Named parameter"
1071
    ///for setting PredMap object.
1062

	
1063
    ///\brief \ref named-templ-param "Named parameter" for setting
1064
    ///the predecessor map.
1072 1065
    ///
1073
    ///\ref named-func-param "Named parameter"
1074
    ///for setting PredMap object.
1066
    ///\ref named-templ-param "Named parameter" function for setting
1067
    ///the map that stores the predecessor arcs of the nodes.
1075 1068
    template<class T>
1076 1069
    BfsWizard<SetPredMapBase<T> > predMap(const T &t)
1077 1070
    {
1078 1071
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1079 1072
      return BfsWizard<SetPredMapBase<T> >(*this);
1080 1073
    }
1081 1074

	
1082 1075
    template<class T>
1083 1076
    struct SetReachedMapBase : public Base {
1084 1077
      typedef T ReachedMap;
1085 1078
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1086 1079
      SetReachedMapBase(const TR &b) : TR(b) {}
1087 1080
    };
1088
    ///\brief \ref named-func-param "Named parameter"
1089
    ///for setting ReachedMap object.
1081

	
1082
    ///\brief \ref named-templ-param "Named parameter" for setting
1083
    ///the reached map.
1090 1084
    ///
1091
    /// \ref named-func-param "Named parameter"
1092
    ///for setting ReachedMap object.
1085
    ///\ref named-templ-param "Named parameter" function for setting
1086
    ///the map that indicates which nodes are reached.
1093 1087
    template<class T>
1094 1088
    BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1095 1089
    {
1096 1090
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1097 1091
      return BfsWizard<SetReachedMapBase<T> >(*this);
1098 1092
    }
1099 1093

	
1100 1094
    template<class T>
1101 1095
    struct SetDistMapBase : public Base {
1102 1096
      typedef T DistMap;
1103 1097
      static DistMap *createDistMap(const Digraph &) { return 0; };
1104 1098
      SetDistMapBase(const TR &b) : TR(b) {}
1105 1099
    };
1106
    ///\brief \ref named-func-param "Named parameter"
1107
    ///for setting DistMap object.
1100

	
1101
    ///\brief \ref named-templ-param "Named parameter" for setting
1102
    ///the distance map.
1108 1103
    ///
1109
    /// \ref named-func-param "Named parameter"
1110
    ///for setting DistMap object.
1104
    ///\ref named-templ-param "Named parameter" function for setting
1105
    ///the map that stores the distances of the nodes calculated
1106
    ///by the algorithm.
1111 1107
    template<class T>
1112 1108
    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
1113 1109
    {
1114 1110
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1115 1111
      return BfsWizard<SetDistMapBase<T> >(*this);
1116 1112
    }
1117 1113

	
1118 1114
    template<class T>
1119 1115
    struct SetProcessedMapBase : public Base {
1120 1116
      typedef T ProcessedMap;
1121 1117
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1122 1118
      SetProcessedMapBase(const TR &b) : TR(b) {}
1123 1119
    };
1124
    ///\brief \ref named-func-param "Named parameter"
1125
    ///for setting ProcessedMap object.
1120

	
1121
    ///\brief \ref named-func-param "Named parameter" for setting
1122
    ///the processed map.
1126 1123
    ///
1127
    /// \ref named-func-param "Named parameter"
1128
    ///for setting ProcessedMap object.
1124
    ///\ref named-templ-param "Named parameter" function for setting
1125
    ///the map that indicates which nodes are processed.
1129 1126
    template<class T>
1130 1127
    BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1131 1128
    {
1132 1129
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1133 1130
      return BfsWizard<SetProcessedMapBase<T> >(*this);
1134 1131
    }
1135 1132

	
1136 1133
    template<class T>
1137 1134
    struct SetPathBase : public Base {
1138 1135
      typedef T Path;
1139 1136
      SetPathBase(const TR &b) : TR(b) {}
1140 1137
    };
1141 1138
    ///\brief \ref named-func-param "Named parameter"
1142 1139
    ///for getting the shortest path to the target node.
1143 1140
    ///
1144 1141
    ///\ref named-func-param "Named parameter"
1145 1142
    ///for getting the shortest path to the target node.
1146 1143
    template<class T>
1147 1144
    BfsWizard<SetPathBase<T> > path(const T &t)
1148 1145
    {
1149 1146
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1150 1147
      return BfsWizard<SetPathBase<T> >(*this);
1151 1148
    }
1152 1149

	
1153 1150
    ///\brief \ref named-func-param "Named parameter"
1154 1151
    ///for getting the distance of the target node.
1155 1152
    ///
1156 1153
    ///\ref named-func-param "Named parameter"
1157 1154
    ///for getting the distance of the target node.
1158 1155
    BfsWizard dist(const int &d)
1159 1156
    {
1160 1157
      Base::_di=const_cast<int*>(&d);
1161 1158
      return *this;
1162 1159
    }
1163 1160

	
1164 1161
  };
1165 1162

	
1166 1163
  ///Function-type interface for BFS algorithm.
1167 1164

	
1168 1165
  /// \ingroup search
1169 1166
  ///Function-type interface for BFS algorithm.
1170 1167
  ///
1171 1168
  ///This function also has several \ref named-func-param "named parameters",
1172 1169
  ///they are declared as the members of class \ref BfsWizard.
1173 1170
  ///The following examples show how to use these parameters.
1174 1171
  ///\code
1175 1172
  ///  // Compute shortest path from node s to each node
1176 1173
  ///  bfs(g).predMap(preds).distMap(dists).run(s);
1177 1174
  ///
1178 1175
  ///  // Compute shortest path from s to t
1179 1176
  ///  bool reached = bfs(g).path(p).dist(d).run(s,t);
1180 1177
  ///\endcode
1181 1178
  ///\warning Don't forget to put the \ref BfsWizard::run(Node) "run()"
1182 1179
  ///to the end of the parameter list.
1183 1180
  ///\sa BfsWizard
1184 1181
  ///\sa Bfs
1185 1182
  template<class GR>
1186 1183
  BfsWizard<BfsWizardBase<GR> >
1187 1184
  bfs(const GR &digraph)
1188 1185
  {
1189 1186
    return BfsWizard<BfsWizardBase<GR> >(digraph);
1190 1187
  }
1191 1188

	
1192 1189
#ifdef DOXYGEN
1193 1190
  /// \brief Visitor class for BFS.
1194 1191
  ///
1195 1192
  /// This class defines the interface of the BfsVisit events, and
1196 1193
  /// it could be the base of a real visitor class.
1197
  template <typename _Digraph>
1194
  template <typename GR>
1198 1195
  struct BfsVisitor {
1199
    typedef _Digraph Digraph;
1196
    typedef GR Digraph;
1200 1197
    typedef typename Digraph::Arc Arc;
1201 1198
    typedef typename Digraph::Node Node;
1202 1199
    /// \brief Called for the source node(s) of the BFS.
1203 1200
    ///
1204 1201
    /// This function is called for the source node(s) of the BFS.
1205 1202
    void start(const Node& node) {}
1206 1203
    /// \brief Called when a node is reached first time.
1207 1204
    ///
1208 1205
    /// This function is called when a node is reached first time.
1209 1206
    void reach(const Node& node) {}
1210 1207
    /// \brief Called when a node is processed.
1211 1208
    ///
1212 1209
    /// This function is called when a node is processed.
1213 1210
    void process(const Node& node) {}
1214 1211
    /// \brief Called when an arc reaches a new node.
1215 1212
    ///
1216 1213
    /// This function is called when the BFS finds an arc whose target node
1217 1214
    /// is not reached yet.
1218 1215
    void discover(const Arc& arc) {}
1219 1216
    /// \brief Called when an arc is examined but its target node is
1220 1217
    /// already discovered.
1221 1218
    ///
1222 1219
    /// This function is called when an arc is examined but its target node is
1223 1220
    /// already discovered.
1224 1221
    void examine(const Arc& arc) {}
1225 1222
  };
1226 1223
#else
1227
  template <typename _Digraph>
1224
  template <typename GR>
1228 1225
  struct BfsVisitor {
1229
    typedef _Digraph Digraph;
1226
    typedef GR Digraph;
1230 1227
    typedef typename Digraph::Arc Arc;
1231 1228
    typedef typename Digraph::Node Node;
1232 1229
    void start(const Node&) {}
1233 1230
    void reach(const Node&) {}
1234 1231
    void process(const Node&) {}
1235 1232
    void discover(const Arc&) {}
1236 1233
    void examine(const Arc&) {}
1237 1234

	
1238 1235
    template <typename _Visitor>
1239 1236
    struct Constraints {
1240 1237
      void constraints() {
1241 1238
        Arc arc;
1242 1239
        Node node;
1243 1240
        visitor.start(node);
1244 1241
        visitor.reach(node);
1245 1242
        visitor.process(node);
1246 1243
        visitor.discover(arc);
1247 1244
        visitor.examine(arc);
1248 1245
      }
1249 1246
      _Visitor& visitor;
1250 1247
    };
1251 1248
  };
1252 1249
#endif
1253 1250

	
1254 1251
  /// \brief Default traits class of BfsVisit class.
1255 1252
  ///
1256 1253
  /// Default traits class of BfsVisit class.
1257
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1258
  template<class _Digraph>
1254
  /// \tparam GR The type of the digraph the algorithm runs on.
1255
  template<class GR>
1259 1256
  struct BfsVisitDefaultTraits {
1260 1257

	
1261 1258
    /// \brief The type of the digraph the algorithm runs on.
1262
    typedef _Digraph Digraph;
1259
    typedef GR Digraph;
1263 1260

	
1264 1261
    /// \brief The type of the map that indicates which nodes are reached.
1265 1262
    ///
1266 1263
    /// The type of the map that indicates which nodes are reached.
1267
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1264
    /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1268 1265
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1269 1266

	
1270 1267
    /// \brief Instantiates a ReachedMap.
1271 1268
    ///
1272 1269
    /// This function instantiates a ReachedMap.
1273 1270
    /// \param digraph is the digraph, to which
1274 1271
    /// we would like to define the ReachedMap.
1275 1272
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1276 1273
      return new ReachedMap(digraph);
1277 1274
    }
1278 1275

	
1279 1276
  };
1280 1277

	
1281 1278
  /// \ingroup search
1282 1279
  ///
1283
  /// \brief %BFS algorithm class with visitor interface.
1280
  /// \brief BFS algorithm class with visitor interface.
1284 1281
  ///
1285
  /// This class provides an efficient implementation of the %BFS algorithm
1282
  /// This class provides an efficient implementation of the BFS algorithm
1286 1283
  /// with visitor interface.
1287 1284
  ///
1288
  /// The %BfsVisit class provides an alternative interface to the Bfs
1285
  /// The BfsVisit class provides an alternative interface to the Bfs
1289 1286
  /// class. It works with callback mechanism, the BfsVisit object calls
1290 1287
  /// the member functions of the \c Visitor class on every BFS event.
1291 1288
  ///
1292 1289
  /// This interface of the BFS algorithm should be used in special cases
1293 1290
  /// when extra actions have to be performed in connection with certain
1294 1291
  /// events of the BFS algorithm. Otherwise consider to use Bfs or bfs()
1295 1292
  /// instead.
1296 1293
  ///
1297
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1298
  /// The default value is
1299
  /// \ref ListDigraph. The value of _Digraph is not used directly by
1300
  /// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits.
1301
  /// \tparam _Visitor The Visitor type that is used by the algorithm.
1302
  /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which
1294
  /// \tparam GR The type of the digraph the algorithm runs on.
1295
  /// The default type is \ref ListDigraph.
1296
  /// The value of GR is not used directly by \ref BfsVisit,
1297
  /// it is only passed to \ref BfsVisitDefaultTraits.
1298
  /// \tparam VS The Visitor type that is used by the algorithm.
1299
  /// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which
1303 1300
  /// does not observe the BFS events. If you want to observe the BFS
1304 1301
  /// events, you should implement your own visitor class.
1305
  /// \tparam _Traits Traits class to set various data types used by the
1302
  /// \tparam TR Traits class to set various data types used by the
1306 1303
  /// algorithm. The default traits class is
1307
  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
1304
  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>".
1308 1305
  /// See \ref BfsVisitDefaultTraits for the documentation of
1309 1306
  /// a BFS visit traits class.
1310 1307
#ifdef DOXYGEN
1311
  template <typename _Digraph, typename _Visitor, typename _Traits>
1308
  template <typename GR, typename VS, typename TR>
1312 1309
#else
1313
  template <typename _Digraph = ListDigraph,
1314
            typename _Visitor = BfsVisitor<_Digraph>,
1315
            typename _Traits = BfsVisitDefaultTraits<_Digraph> >
1310
  template <typename GR = ListDigraph,
1311
            typename VS = BfsVisitor<GR>,
1312
            typename TR = BfsVisitDefaultTraits<GR> >
1316 1313
#endif
1317 1314
  class BfsVisit {
1318 1315
  public:
1319 1316

	
1320 1317
    ///The traits class.
1321
    typedef _Traits Traits;
1318
    typedef TR Traits;
1322 1319

	
1323 1320
    ///The type of the digraph the algorithm runs on.
1324 1321
    typedef typename Traits::Digraph Digraph;
1325 1322

	
1326 1323
    ///The visitor type used by the algorithm.
1327
    typedef _Visitor Visitor;
1324
    typedef VS Visitor;
1328 1325

	
1329 1326
    ///The type of the map that indicates which nodes are reached.
1330 1327
    typedef typename Traits::ReachedMap ReachedMap;
1331 1328

	
1332 1329
  private:
1333 1330

	
1334 1331
    typedef typename Digraph::Node Node;
1335 1332
    typedef typename Digraph::NodeIt NodeIt;
1336 1333
    typedef typename Digraph::Arc Arc;
1337 1334
    typedef typename Digraph::OutArcIt OutArcIt;
1338 1335

	
1339 1336
    //Pointer to the underlying digraph.
1340 1337
    const Digraph *_digraph;
1341 1338
    //Pointer to the visitor object.
1342 1339
    Visitor *_visitor;
1343 1340
    //Pointer to the map of reached status of the nodes.
1344 1341
    ReachedMap *_reached;
1345 1342
    //Indicates if _reached is locally allocated (true) or not.
1346 1343
    bool local_reached;
1347 1344

	
1348 1345
    std::vector<typename Digraph::Node> _list;
1349 1346
    int _list_front, _list_back;
1350 1347

	
1351 1348
    //Creates the maps if necessary.
1352 1349
    void create_maps() {
1353 1350
      if(!_reached) {
1354 1351
        local_reached = true;
1355 1352
        _reached = Traits::createReachedMap(*_digraph);
1356 1353
      }
1357 1354
    }
1358 1355

	
1359 1356
  protected:
1360 1357

	
1361 1358
    BfsVisit() {}
1362 1359

	
1363 1360
  public:
1364 1361

	
1365 1362
    typedef BfsVisit Create;
1366 1363

	
1367 1364
    /// \name Named Template Parameters
1368 1365

	
1369 1366
    ///@{
1370 1367
    template <class T>
1371 1368
    struct SetReachedMapTraits : public Traits {
1372 1369
      typedef T ReachedMap;
1373 1370
      static ReachedMap *createReachedMap(const Digraph &digraph) {
1374 1371
        LEMON_ASSERT(false, "ReachedMap is not initialized");
1375 1372
        return 0; // ignore warnings
1376 1373
      }
1377 1374
    };
1378 1375
    /// \brief \ref named-templ-param "Named parameter" for setting
1379 1376
    /// ReachedMap type.
1380 1377
    ///
1381 1378
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1382 1379
    template <class T>
1383 1380
    struct SetReachedMap : public BfsVisit< Digraph, Visitor,
1384 1381
                                            SetReachedMapTraits<T> > {
1385 1382
      typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1386 1383
    };
1387 1384
    ///@}
1388 1385

	
1389 1386
  public:
1390 1387

	
1391 1388
    /// \brief Constructor.
1392 1389
    ///
1393 1390
    /// Constructor.
1394 1391
    ///
1395 1392
    /// \param digraph The digraph the algorithm runs on.
1396 1393
    /// \param visitor The visitor object of the algorithm.
1397 1394
    BfsVisit(const Digraph& digraph, Visitor& visitor)
1398 1395
      : _digraph(&digraph), _visitor(&visitor),
1399 1396
        _reached(0), local_reached(false) {}
1400 1397

	
1401 1398
    /// \brief Destructor.
1402 1399
    ~BfsVisit() {
1403 1400
      if(local_reached) delete _reached;
1404 1401
    }
1405 1402

	
1406 1403
    /// \brief Sets the map that indicates which nodes are reached.
1407 1404
    ///
1408 1405
    /// Sets the map that indicates which nodes are reached.
1409 1406
    /// If you don't use this function before calling \ref run(Node) "run()"
1410 1407
    /// or \ref init(), an instance will be allocated automatically.
1411 1408
    /// The destructor deallocates this automatically allocated map,
1412 1409
    /// of course.
1413 1410
    /// \return <tt> (*this) </tt>
1414 1411
    BfsVisit &reachedMap(ReachedMap &m) {
1415 1412
      if(local_reached) {
1416 1413
        delete _reached;
1417 1414
        local_reached = false;
1418 1415
      }
1419 1416
      _reached = &m;
1420 1417
      return *this;
1421 1418
    }
1422 1419

	
1423 1420
  public:
1424 1421

	
1425 1422
    /// \name Execution Control
1426 1423
    /// The simplest way to execute the BFS algorithm is to use one of the
1427 1424
    /// member functions called \ref run(Node) "run()".\n
1428
    /// If you need more control on the execution, first you have to call
1429
    /// \ref init(), then you can add several source nodes with
1425
    /// If you need better control on the execution, you have to call
1426
    /// \ref init() first, then you can add several source nodes with
1430 1427
    /// \ref addSource(). Finally the actual path computation can be
1431 1428
    /// performed with one of the \ref start() functions.
1432 1429

	
1433 1430
    /// @{
1434 1431

	
1435 1432
    /// \brief Initializes the internal data structures.
1436 1433
    ///
1437 1434
    /// Initializes the internal data structures.
1438 1435
    void init() {
1439 1436
      create_maps();
1440 1437
      _list.resize(countNodes(*_digraph));
1441 1438
      _list_front = _list_back = -1;
1442 1439
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1443 1440
        _reached->set(u, false);
1444 1441
      }
1445 1442
    }
1446 1443

	
1447 1444
    /// \brief Adds a new source node.
1448 1445
    ///
1449 1446
    /// Adds a new source node to the set of nodes to be processed.
1450 1447
    void addSource(Node s) {
1451 1448
      if(!(*_reached)[s]) {
1452 1449
          _reached->set(s,true);
1453 1450
          _visitor->start(s);
1454 1451
          _visitor->reach(s);
1455 1452
          _list[++_list_back] = s;
1456 1453
        }
1457 1454
    }
1458 1455

	
1459 1456
    /// \brief Processes the next node.
1460 1457
    ///
1461 1458
    /// Processes the next node.
1462 1459
    ///
1463 1460
    /// \return The processed node.
1464 1461
    ///
1465 1462
    /// \pre The queue must not be empty.
1466 1463
    Node processNextNode() {
1467 1464
      Node n = _list[++_list_front];
1468 1465
      _visitor->process(n);
1469 1466
      Arc e;
1470 1467
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1471 1468
        Node m = _digraph->target(e);
1472 1469
        if (!(*_reached)[m]) {
1473 1470
          _visitor->discover(e);
1474 1471
          _visitor->reach(m);
1475 1472
          _reached->set(m, true);
1476 1473
          _list[++_list_back] = m;
1477 1474
        } else {
1478 1475
          _visitor->examine(e);
1479 1476
        }
1480 1477
      }
1481 1478
      return n;
1482 1479
    }
1483 1480

	
1484 1481
    /// \brief Processes the next node.
1485 1482
    ///
1486 1483
    /// Processes the next node and checks if the given target node
1487 1484
    /// is reached. If the target node is reachable from the processed
1488 1485
    /// node, then the \c reach parameter will be set to \c true.
1489 1486
    ///
1490 1487
    /// \param target The target node.
1491 1488
    /// \retval reach Indicates if the target node is reached.
1492 1489
    /// It should be initially \c false.
1493 1490
    ///
1494 1491
    /// \return The processed node.
1495 1492
    ///
1496 1493
    /// \pre The queue must not be empty.
1497 1494
    Node processNextNode(Node target, bool& reach) {
1498 1495
      Node n = _list[++_list_front];
1499 1496
      _visitor->process(n);
1500 1497
      Arc e;
1501 1498
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1502 1499
        Node m = _digraph->target(e);
1503 1500
        if (!(*_reached)[m]) {
1504 1501
          _visitor->discover(e);
1505 1502
          _visitor->reach(m);
1506 1503
          _reached->set(m, true);
1507 1504
          _list[++_list_back] = m;
1508 1505
          reach = reach || (target == m);
1509 1506
        } else {
1510 1507
          _visitor->examine(e);
1511 1508
        }
1512 1509
      }
1513 1510
      return n;
1514 1511
    }
1515 1512

	
1516 1513
    /// \brief Processes the next node.
1517 1514
    ///
1518 1515
    /// Processes the next node and checks if at least one of reached
1519 1516
    /// nodes has \c true value in the \c nm node map. If one node
1520 1517
    /// with \c true value is reachable from the processed node, then the
1521 1518
    /// \c rnode parameter will be set to the first of such nodes.
1522 1519
    ///
1523 1520
    /// \param nm A \c bool (or convertible) node map that indicates the
1524 1521
    /// possible targets.
1525 1522
    /// \retval rnode The reached target node.
... ...
@@ -1642,111 +1639,111 @@
1642 1639
    ///   while ( !b.emptyQueue() && rnode == INVALID ) {
1643 1640
    ///     b.processNextNode(nm, rnode);
1644 1641
    ///   }
1645 1642
    ///   return rnode;
1646 1643
    /// \endcode
1647 1644
    template <typename NM>
1648 1645
    Node start(const NM &nm) {
1649 1646
      Node rnode = INVALID;
1650 1647
      while ( !emptyQueue() && rnode == INVALID ) {
1651 1648
        processNextNode(nm, rnode);
1652 1649
      }
1653 1650
      return rnode;
1654 1651
    }
1655 1652

	
1656 1653
    /// \brief Runs the algorithm from the given source node.
1657 1654
    ///
1658 1655
    /// This method runs the %BFS algorithm from node \c s
1659 1656
    /// in order to compute the shortest path to each node.
1660 1657
    ///
1661 1658
    /// The algorithm computes
1662 1659
    /// - the shortest path tree,
1663 1660
    /// - the distance of each node from the root.
1664 1661
    ///
1665 1662
    /// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
1666 1663
    ///\code
1667 1664
    ///   b.init();
1668 1665
    ///   b.addSource(s);
1669 1666
    ///   b.start();
1670 1667
    ///\endcode
1671 1668
    void run(Node s) {
1672 1669
      init();
1673 1670
      addSource(s);
1674 1671
      start();
1675 1672
    }
1676 1673

	
1677 1674
    /// \brief Finds the shortest path between \c s and \c t.
1678 1675
    ///
1679 1676
    /// This method runs the %BFS algorithm from node \c s
1680 1677
    /// in order to compute the shortest path to node \c t
1681 1678
    /// (it stops searching when \c t is processed).
1682 1679
    ///
1683 1680
    /// \return \c true if \c t is reachable form \c s.
1684 1681
    ///
1685 1682
    /// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a
1686 1683
    /// shortcut of the following code.
1687 1684
    ///\code
1688 1685
    ///   b.init();
1689 1686
    ///   b.addSource(s);
1690 1687
    ///   b.start(t);
1691 1688
    ///\endcode
1692 1689
    bool run(Node s,Node t) {
1693 1690
      init();
1694 1691
      addSource(s);
1695 1692
      start(t);
1696 1693
      return reached(t);
1697 1694
    }
1698 1695

	
1699 1696
    /// \brief Runs the algorithm to visit all nodes in the digraph.
1700 1697
    ///
1701 1698
    /// This method runs the %BFS algorithm in order to
1702 1699
    /// compute the shortest path to each node.
1703 1700
    ///
1704 1701
    /// The algorithm computes
1705 1702
    /// - the shortest path tree (forest),
1706 1703
    /// - the distance of each node from the root(s).
1707 1704
    ///
1708 1705
    /// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
1709 1706
    ///\code
1710 1707
    ///  b.init();
1711 1708
    ///  for (NodeIt n(gr); n != INVALID; ++n) {
1712 1709
    ///    if (!b.reached(n)) {
1713 1710
    ///      b.addSource(n);
1714 1711
    ///      b.start();
1715 1712
    ///    }
1716 1713
    ///  }
1717 1714
    ///\endcode
1718 1715
    void run() {
1719 1716
      init();
1720 1717
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
1721 1718
        if (!reached(it)) {
1722 1719
          addSource(it);
1723 1720
          start();
1724 1721
        }
1725 1722
      }
1726 1723
    }
1727 1724

	
1728 1725
    ///@}
1729 1726

	
1730 1727
    /// \name Query Functions
1731 1728
    /// The results of the BFS algorithm can be obtained using these
1732 1729
    /// functions.\n
1733 1730
    /// Either \ref run(Node) "run()" or \ref start() should be called
1734 1731
    /// before using them.
1735 1732

	
1736 1733
    ///@{
1737 1734

	
1738
    /// \brief Checks if a node is reached from the root(s).
1735
    /// \brief Checks if the given node is reached from the root(s).
1739 1736
    ///
1740 1737
    /// Returns \c true if \c v is reached from the root(s).
1741 1738
    ///
1742 1739
    /// \pre Either \ref run(Node) "run()" or \ref init()
1743 1740
    /// must be called before using this function.
1744 1741
    bool reached(Node v) const { return (*_reached)[v]; }
1745 1742

	
1746 1743
    ///@}
1747 1744

	
1748 1745
  };
1749 1746

	
1750 1747
} //END OF NAMESPACE LEMON
1751 1748

	
1752 1749
#endif
Ignore white space 6 line context
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-2009
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_BIN_HEAP_H
20 20
#define LEMON_BIN_HEAP_H
21 21

	
22
///\ingroup auxdat
22
///\ingroup heaps
23 23
///\file
24
///\brief Binary Heap implementation.
24
///\brief Binary heap implementation.
25 25

	
26 26
#include <vector>
27 27
#include <utility>
28 28
#include <functional>
29 29

	
30 30
namespace lemon {
31 31

	
32
  ///\ingroup auxdat
32
  /// \ingroup heaps
33 33
  ///
34
  ///\brief A Binary Heap implementation.
34
  /// \brief Binary heap data structure.
35 35
  ///
36
  ///This class implements the \e binary \e heap data structure. A \e heap
37
  ///is a data structure for storing items with specified values called \e
38
  ///priorities in such a way that finding the item with minimum priority is
39
  ///efficient. \c Compare specifies the ordering of the priorities. In a heap
40
  ///one can change the priority of an item, add or erase an item, etc.
36
  /// This class implements the \e binary \e heap data structure.
37
  /// It fully conforms to the \ref concepts::Heap "heap concept".
41 38
  ///
42
  ///\tparam _Prio Type of the priority of the items.
43
  ///\tparam _ItemIntMap A read and writable Item int map, used internally
44
  ///to handle the cross references.
45
  ///\tparam _Compare A class for the ordering of the priorities. The
46
  ///default is \c std::less<_Prio>.
47
  ///
48
  ///\sa FibHeap
49
  ///\sa Dijkstra
50
  template <typename _Prio, typename _ItemIntMap,
51
            typename _Compare = std::less<_Prio> >
39
  /// \tparam PR Type of the priorities of the items.
40
  /// \tparam IM A read-writable item map with \c int values, used
41
  /// internally to handle the cross references.
42
  /// \tparam CMP A functor class for comparing the priorities.
43
  /// The default is \c std::less<PR>.
44
#ifdef DOXYGEN
45
  template <typename PR, typename IM, typename CMP>
46
#else
47
  template <typename PR, typename IM, typename CMP = std::less<PR> >
48
#endif
52 49
  class BinHeap {
50
  public:
53 51

	
54
  public:
55
    ///\e
56
    typedef _ItemIntMap ItemIntMap;
57
    ///\e
58
    typedef _Prio Prio;
59
    ///\e
52
    /// Type of the item-int map.
53
    typedef IM ItemIntMap;
54
    /// Type of the priorities.
55
    typedef PR Prio;
56
    /// Type of the items stored in the heap.
60 57
    typedef typename ItemIntMap::Key Item;
61
    ///\e
58
    /// Type of the item-priority pairs.
62 59
    typedef std::pair<Item,Prio> Pair;
63
    ///\e
64
    typedef _Compare Compare;
60
    /// Functor type for comparing the priorities.
61
    typedef CMP Compare;
65 62

	
66
    /// \brief Type to represent the items states.
63
    /// \brief Type to represent the states of the items.
67 64
    ///
68
    /// Each Item element have a state associated to it. It may be "in heap",
69
    /// "pre heap" or "post heap". The latter two are indifferent from the
65
    /// Each item has a state associated to it. It can be "in heap",
66
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
70 67
    /// heap's point of view, but may be useful to the user.
71 68
    ///
72
    /// The ItemIntMap \e should be initialized in such way that it maps
73
    /// PRE_HEAP (-1) to any element to be put in the heap...
69
    /// The item-int map must be initialized in such way that it assigns
70
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
74 71
    enum State {
75
      IN_HEAP = 0,
76
      PRE_HEAP = -1,
77
      POST_HEAP = -2
72
      IN_HEAP = 0,    ///< = 0.
73
      PRE_HEAP = -1,  ///< = -1.
74
      POST_HEAP = -2  ///< = -2.
78 75
    };
79 76

	
80 77
  private:
81
    std::vector<Pair> data;
82
    Compare comp;
83
    ItemIntMap &iim;
78
    std::vector<Pair> _data;
79
    Compare _comp;
80
    ItemIntMap &_iim;
84 81

	
85 82
  public:
86
    /// \brief The constructor.
83

	
84
    /// \brief Constructor.
87 85
    ///
88
    /// The constructor.
89
    /// \param _iim should be given to the constructor, since it is used
90
    /// internally to handle the cross references. The value of the map
91
    /// should be PRE_HEAP (-1) for each element.
92
    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
86
    /// Constructor.
87
    /// \param map A map that assigns \c int values to the items.
88
    /// It is used internally to handle the cross references.
89
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
90
    explicit BinHeap(ItemIntMap &map) : _iim(map) {}
93 91

	
94
    /// \brief The constructor.
92
    /// \brief Constructor.
95 93
    ///
96
    /// The constructor.
97
    /// \param _iim should be given to the constructor, since it is used
98
    /// internally to handle the cross references. The value of the map
99
    /// should be PRE_HEAP (-1) for each element.
94
    /// Constructor.
95
    /// \param map A map that assigns \c int values to the items.
96
    /// It is used internally to handle the cross references.
97
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
98
    /// \param comp The function object used for comparing the priorities.
99
    BinHeap(ItemIntMap &map, const Compare &comp)
100
      : _iim(map), _comp(comp) {}
101

	
102

	
103
    /// \brief The number of items stored in the heap.
100 104
    ///
101
    /// \param _comp The comparator function object.
102
    BinHeap(ItemIntMap &_iim, const Compare &_comp)
103
      : iim(_iim), comp(_comp) {}
105
    /// This function returns the number of items stored in the heap.
106
    int size() const { return _data.size(); }
104 107

	
108
    /// \brief Check if the heap is empty.
109
    ///
110
    /// This function returns \c true if the heap is empty.
111
    bool empty() const { return _data.empty(); }
105 112

	
106
    /// The number of items stored in the heap.
113
    /// \brief Make the heap empty.
107 114
    ///
108
    /// \brief Returns the number of items stored in the heap.
109
    int size() const { return data.size(); }
110

	
111
    /// \brief Checks if the heap stores no items.
112
    ///
113
    /// Returns \c true if and only if the heap stores no items.
114
    bool empty() const { return data.empty(); }
115

	
116
    /// \brief Make empty this heap.
117
    ///
118
    /// Make empty this heap. It does not change the cross reference map.
119
    /// If you want to reuse what is not surely empty you should first clear
120
    /// the heap and after that you should set the cross reference map for
121
    /// each item to \c PRE_HEAP.
115
    /// This functon makes the heap empty.
116
    /// It does not change the cross reference map. If you want to reuse
117
    /// a heap that is not surely empty, you should first clear it and
118
    /// then you should set the cross reference map to \c PRE_HEAP
119
    /// for each item.
122 120
    void clear() {
123
      data.clear();
121
      _data.clear();
124 122
    }
125 123

	
126 124
  private:
127 125
    static int parent(int i) { return (i-1)/2; }
128 126

	
129
    static int second_child(int i) { return 2*i+2; }
127
    static int secondChild(int i) { return 2*i+2; }
130 128
    bool less(const Pair &p1, const Pair &p2) const {
131
      return comp(p1.second, p2.second);
129
      return _comp(p1.second, p2.second);
132 130
    }
133 131

	
134
    int bubble_up(int hole, Pair p) {
132
    int bubbleUp(int hole, Pair p) {
135 133
      int par = parent(hole);
136
      while( hole>0 && less(p,data[par]) ) {
137
        move(data[par],hole);
134
      while( hole>0 && less(p,_data[par]) ) {
135
        move(_data[par],hole);
138 136
        hole = par;
139 137
        par = parent(hole);
140 138
      }
141 139
      move(p, hole);
142 140
      return hole;
143 141
    }
144 142

	
145
    int bubble_down(int hole, Pair p, int length) {
146
      int child = second_child(hole);
143
    int bubbleDown(int hole, Pair p, int length) {
144
      int child = secondChild(hole);
147 145
      while(child < length) {
148
        if( less(data[child-1], data[child]) ) {
146
        if( less(_data[child-1], _data[child]) ) {
149 147
          --child;
150 148
        }
151
        if( !less(data[child], p) )
149
        if( !less(_data[child], p) )
152 150
          goto ok;
153
        move(data[child], hole);
151
        move(_data[child], hole);
154 152
        hole = child;
155
        child = second_child(hole);
153
        child = secondChild(hole);
156 154
      }
157 155
      child--;
158
      if( child<length && less(data[child], p) ) {
159
        move(data[child], hole);
156
      if( child<length && less(_data[child], p) ) {
157
        move(_data[child], hole);
160 158
        hole=child;
161 159
      }
162 160
    ok:
163 161
      move(p, hole);
164 162
      return hole;
165 163
    }
166 164

	
167 165
    void move(const Pair &p, int i) {
168
      data[i] = p;
169
      iim.set(p.first, i);
166
      _data[i] = p;
167
      _iim.set(p.first, i);
170 168
    }
171 169

	
172 170
  public:
171

	
173 172
    /// \brief Insert a pair of item and priority into the heap.
174 173
    ///
175
    /// Adds \c p.first to the heap with priority \c p.second.
174
    /// This function inserts \c p.first to the heap with priority
175
    /// \c p.second.
176 176
    /// \param p The pair to insert.
177
    /// \pre \c p.first must not be stored in the heap.
177 178
    void push(const Pair &p) {
178
      int n = data.size();
179
      data.resize(n+1);
180
      bubble_up(n, p);
179
      int n = _data.size();
180
      _data.resize(n+1);
181
      bubbleUp(n, p);
181 182
    }
182 183

	
183
    /// \brief Insert an item into the heap with the given heap.
184
    /// \brief Insert an item into the heap with the given priority.
184 185
    ///
185
    /// Adds \c i to the heap with priority \c p.
186
    /// This function inserts the given item into the heap with the
187
    /// given priority.
186 188
    /// \param i The item to insert.
187 189
    /// \param p The priority of the item.
190
    /// \pre \e i must not be stored in the heap.
188 191
    void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
189 192

	
190
    /// \brief Returns the item with minimum priority relative to \c Compare.
193
    /// \brief Return the item having minimum priority.
191 194
    ///
192
    /// This method returns the item with minimum priority relative to \c
193
    /// Compare.
194
    /// \pre The heap must be nonempty.
195
    /// This function returns the item having minimum priority.
196
    /// \pre The heap must be non-empty.
195 197
    Item top() const {
196
      return data[0].first;
198
      return _data[0].first;
197 199
    }
198 200

	
199
    /// \brief Returns the minimum priority relative to \c Compare.
201
    /// \brief The minimum priority.
200 202
    ///
201
    /// It returns the minimum priority relative to \c Compare.
202
    /// \pre The heap must be nonempty.
203
    /// This function returns the minimum priority.
204
    /// \pre The heap must be non-empty.
203 205
    Prio prio() const {
204
      return data[0].second;
206
      return _data[0].second;
205 207
    }
206 208

	
207
    /// \brief Deletes the item with minimum priority relative to \c Compare.
209
    /// \brief Remove the item having minimum priority.
208 210
    ///
209
    /// This method deletes the item with minimum priority relative to \c
210
    /// Compare from the heap.
211
    /// This function removes the item having minimum priority.
211 212
    /// \pre The heap must be non-empty.
212 213
    void pop() {
213
      int n = data.size()-1;
214
      iim.set(data[0].first, POST_HEAP);
214
      int n = _data.size()-1;
215
      _iim.set(_data[0].first, POST_HEAP);
215 216
      if (n > 0) {
216
        bubble_down(0, data[n], n);
217
        bubbleDown(0, _data[n], n);
217 218
      }
218
      data.pop_back();
219
      _data.pop_back();
219 220
    }
220 221

	
221
    /// \brief Deletes \c i from the heap.
222
    /// \brief Remove the given item from the heap.
222 223
    ///
223
    /// This method deletes item \c i from the heap.
224
    /// \param i The item to erase.
225
    /// \pre The item should be in the heap.
224
    /// This function removes the given item from the heap if it is
225
    /// already stored.
226
    /// \param i The item to delete.
227
    /// \pre \e i must be in the heap.
226 228
    void erase(const Item &i) {
227
      int h = iim[i];
228
      int n = data.size()-1;
229
      iim.set(data[h].first, POST_HEAP);
229
      int h = _iim[i];
230
      int n = _data.size()-1;
231
      _iim.set(_data[h].first, POST_HEAP);
230 232
      if( h < n ) {
231
        if ( bubble_up(h, data[n]) == h) {
232
          bubble_down(h, data[n], n);
233
        if ( bubbleUp(h, _data[n]) == h) {
234
          bubbleDown(h, _data[n], n);
233 235
        }
234 236
      }
235
      data.pop_back();
237
      _data.pop_back();
236 238
    }
237 239

	
238

	
239
    /// \brief Returns the priority of \c i.
240
    /// \brief The priority of the given item.
240 241
    ///
241
    /// This function returns the priority of item \c i.
242
    /// \pre \c i must be in the heap.
242
    /// This function returns the priority of the given item.
243 243
    /// \param i The item.
244
    /// \pre \e i must be in the heap.
244 245
    Prio operator[](const Item &i) const {
245
      int idx = iim[i];
246
      return data[idx].second;
246
      int idx = _iim[i];
247
      return _data[idx].second;
247 248
    }
248 249

	
249
    /// \brief \c i gets to the heap with priority \c p independently
250
    /// if \c i was already there.
250
    /// \brief Set the priority of an item or insert it, if it is
251
    /// not stored in the heap.
251 252
    ///
252
    /// This method calls \ref push(\c i, \c p) if \c i is not stored
253
    /// in the heap and sets the priority of \c i to \c p otherwise.
253
    /// This method sets the priority of the given item if it is
254
    /// already stored in the heap. Otherwise it inserts the given
255
    /// item into the heap with the given priority.
254 256
    /// \param i The item.
255 257
    /// \param p The priority.
256 258
    void set(const Item &i, const Prio &p) {
257
      int idx = iim[i];
259
      int idx = _iim[i];
258 260
      if( idx < 0 ) {
259 261
        push(i,p);
260 262
      }
261
      else if( comp(p, data[idx].second) ) {
262
        bubble_up(idx, Pair(i,p));
263
      else if( _comp(p, _data[idx].second) ) {
264
        bubbleUp(idx, Pair(i,p));
263 265
      }
264 266
      else {
265
        bubble_down(idx, Pair(i,p), data.size());
267
        bubbleDown(idx, Pair(i,p), _data.size());
266 268
      }
267 269
    }
268 270

	
269
    /// \brief Decreases the priority of \c i to \c p.
271
    /// \brief Decrease the priority of an item to the given value.
270 272
    ///
271
    /// This method decreases the priority of item \c i to \c p.
272
    /// \pre \c i must be stored in the heap with priority at least \c
273
    /// p relative to \c Compare.
273
    /// This function decreases the priority of an item to the given value.
274 274
    /// \param i The item.
275 275
    /// \param p The priority.
276
    /// \pre \e i must be stored in the heap with priority at least \e p.
276 277
    void decrease(const Item &i, const Prio &p) {
277
      int idx = iim[i];
278
      bubble_up(idx, Pair(i,p));
278
      int idx = _iim[i];
279
      bubbleUp(idx, Pair(i,p));
279 280
    }
280 281

	
281
    /// \brief Increases the priority of \c i to \c p.
282
    /// \brief Increase the priority of an item to the given value.
282 283
    ///
283
    /// This method sets the priority of item \c i to \c p.
284
    /// \pre \c i must be stored in the heap with priority at most \c
285
    /// p relative to \c Compare.
284
    /// This function increases the priority of an item to the given value.
286 285
    /// \param i The item.
287 286
    /// \param p The priority.
287
    /// \pre \e i must be stored in the heap with priority at most \e p.
288 288
    void increase(const Item &i, const Prio &p) {
289
      int idx = iim[i];
290
      bubble_down(idx, Pair(i,p), data.size());
289
      int idx = _iim[i];
290
      bubbleDown(idx, Pair(i,p), _data.size());
291 291
    }
292 292

	
293
    /// \brief Returns if \c item is in, has already been in, or has
294
    /// never been in the heap.
293
    /// \brief Return the state of an item.
295 294
    ///
296
    /// This method returns PRE_HEAP if \c item has never been in the
297
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
298
    /// otherwise. In the latter case it is possible that \c item will
299
    /// get back to the heap again.
295
    /// This method returns \c PRE_HEAP if the given item has never
296
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
297
    /// and \c POST_HEAP otherwise.
298
    /// In the latter case it is possible that the item will get back
299
    /// to the heap again.
300 300
    /// \param i The item.
301 301
    State state(const Item &i) const {
302
      int s = iim[i];
302
      int s = _iim[i];
303 303
      if( s>=0 )
304 304
        s=0;
305 305
      return State(s);
306 306
    }
307 307

	
308
    /// \brief Sets the state of the \c item in the heap.
308
    /// \brief Set the state of an item in the heap.
309 309
    ///
310
    /// Sets the state of the \c item in the heap. It can be used to
311
    /// manually clear the heap when it is important to achive the
312
    /// better time complexity.
310
    /// This function sets the state of the given item in the heap.
311
    /// It can be used to manually clear the heap when it is important
312
    /// to achive better time complexity.
313 313
    /// \param i The item.
314 314
    /// \param st The state. It should not be \c IN_HEAP.
315 315
    void state(const Item& i, State st) {
316 316
      switch (st) {
317 317
      case POST_HEAP:
318 318
      case PRE_HEAP:
319 319
        if (state(i) == IN_HEAP) {
320 320
          erase(i);
321 321
        }
322
        iim[i] = st;
322
        _iim[i] = st;
323 323
        break;
324 324
      case IN_HEAP:
325 325
        break;
326 326
      }
327 327
    }
328 328

	
329
    /// \brief Replaces an item in the heap.
329
    /// \brief Replace an item in the heap.
330 330
    ///
331
    /// The \c i item is replaced with \c j item. The \c i item should
332
    /// be in the heap, while the \c j should be out of the heap. The
333
    /// \c i item will out of the heap and \c j will be in the heap
334
    /// with the same prioriority as prevoiusly the \c i item.
331
    /// This function replaces item \c i with item \c j.
332
    /// Item \c i must be in the heap, while \c j must be out of the heap.
333
    /// After calling this method, item \c i will be out of the
334
    /// heap and \c j will be in the heap with the same prioriority
335
    /// as item \c i had before.
335 336
    void replace(const Item& i, const Item& j) {
336
      int idx = iim[i];
337
      iim.set(i, iim[j]);
338
      iim.set(j, idx);
339
      data[idx].first = j;
337
      int idx = _iim[i];
338
      _iim.set(i, _iim[j]);
339
      _iim.set(j, idx);
340
      _data[idx].first = j;
340 341
    }
341 342

	
342 343
  }; // class BinHeap
343 344

	
344 345
} // namespace lemon
345 346

	
346 347
#endif // LEMON_BIN_HEAP_H
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_ARRAY_MAP_H
20 20
#define LEMON_BITS_ARRAY_MAP_H
21 21

	
22 22
#include <memory>
23 23

	
24 24
#include <lemon/bits/traits.h>
25 25
#include <lemon/bits/alteration_notifier.h>
26 26
#include <lemon/concept_check.h>
27 27
#include <lemon/concepts/maps.h>
28 28

	
29 29
// \ingroup graphbits
30 30
// \file
31 31
// \brief Graph map based on the array storage.
32 32

	
33 33
namespace lemon {
34 34

	
35 35
  // \ingroup graphbits
36 36
  //
37 37
  // \brief Graph map based on the array storage.
38 38
  //
39 39
  // The ArrayMap template class is graph map structure that automatically
40 40
  // updates the map when a key is added to or erased from the graph.
41 41
  // This map uses the allocators to implement the container functionality.
42 42
  //
43 43
  // The template parameters are the Graph, the current Item type and
44 44
  // the Value type of the map.
45 45
  template <typename _Graph, typename _Item, typename _Value>
46 46
  class ArrayMap
47 47
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
48 48
  public:
49 49
    // The graph type.
50
    typedef _Graph Graph;
50
    typedef _Graph GraphType;
51 51
    // The item type.
52 52
    typedef _Item Item;
53 53
    // The reference map tag.
54 54
    typedef True ReferenceMapTag;
55 55

	
56 56
    // The key type of the map.
57 57
    typedef _Item Key;
58 58
    // The value type of the map.
59 59
    typedef _Value Value;
60 60

	
61 61
    // The const reference type of the map.
62 62
    typedef const _Value& ConstReference;
63 63
    // The reference type of the map.
64 64
    typedef _Value& Reference;
65 65

	
66
    // The map type.
67
    typedef ArrayMap Map;
68

	
66 69
    // The notifier type.
67 70
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
68 71

	
72
  private:
73
  
69 74
    // The MapBase of the Map which imlements the core regisitry function.
70 75
    typedef typename Notifier::ObserverBase Parent;
71 76

	
72
  private:
73 77
    typedef std::allocator<Value> Allocator;
74 78

	
75 79
  public:
76 80

	
77 81
    // \brief Graph initialized map constructor.
78 82
    //
79 83
    // Graph initialized map constructor.
80
    explicit ArrayMap(const Graph& graph) {
84
    explicit ArrayMap(const GraphType& graph) {
81 85
      Parent::attach(graph.notifier(Item()));
82 86
      allocate_memory();
83 87
      Notifier* nf = Parent::notifier();
84 88
      Item it;
85 89
      for (nf->first(it); it != INVALID; nf->next(it)) {
86 90
        int id = nf->id(it);;
87 91
        allocator.construct(&(values[id]), Value());
88 92
      }
89 93
    }
90 94

	
91 95
    // \brief Constructor to use default value to initialize the map.
92 96
    //
93 97
    // It constructs a map and initialize all of the the map.
94
    ArrayMap(const Graph& graph, const Value& value) {
98
    ArrayMap(const GraphType& graph, const Value& value) {
95 99
      Parent::attach(graph.notifier(Item()));
96 100
      allocate_memory();
97 101
      Notifier* nf = Parent::notifier();
98 102
      Item it;
99 103
      for (nf->first(it); it != INVALID; nf->next(it)) {
100 104
        int id = nf->id(it);;
101 105
        allocator.construct(&(values[id]), value);
102 106
      }
103 107
    }
104 108

	
105 109
  private:
106 110
    // \brief Constructor to copy a map of the same map type.
107 111
    //
108 112
    // Constructor to copy a map of the same map type.
109 113
    ArrayMap(const ArrayMap& copy) : Parent() {
110 114
      if (copy.attached()) {
111 115
        attach(*copy.notifier());
112 116
      }
113 117
      capacity = copy.capacity;
114 118
      if (capacity == 0) return;
115 119
      values = allocator.allocate(capacity);
116 120
      Notifier* nf = Parent::notifier();
117 121
      Item it;
118 122
      for (nf->first(it); it != INVALID; nf->next(it)) {
119 123
        int id = nf->id(it);;
120 124
        allocator.construct(&(values[id]), copy.values[id]);
121 125
      }
122 126
    }
123 127

	
124 128
    // \brief Assign operator.
125 129
    //
126 130
    // This operator assigns for each item in the map the
127 131
    // value mapped to the same item in the copied map.
128 132
    // The parameter map should be indiced with the same
129 133
    // itemset because this assign operator does not change
130 134
    // the container of the map.
131 135
    ArrayMap& operator=(const ArrayMap& cmap) {
132 136
      return operator=<ArrayMap>(cmap);
133 137
    }
134 138

	
135 139

	
136 140
    // \brief Template assign operator.
137 141
    //
138
    // The given parameter should be conform to the ReadMap
142
    // The given parameter should conform to the ReadMap
139 143
    // concecpt and could be indiced by the current item set of
140 144
    // the NodeMap. In this case the value for each item
141 145
    // is assigned by the value of the given ReadMap.
142 146
    template <typename CMap>
143 147
    ArrayMap& operator=(const CMap& cmap) {
144 148
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
145 149
      const typename Parent::Notifier* nf = Parent::notifier();
146 150
      Item it;
147 151
      for (nf->first(it); it != INVALID; nf->next(it)) {
148 152
        set(it, cmap[it]);
149 153
      }
150 154
      return *this;
151 155
    }
152 156

	
153 157
  public:
154 158
    // \brief The destructor of the map.
155 159
    //
156 160
    // The destructor of the map.
157 161
    virtual ~ArrayMap() {
158 162
      if (attached()) {
159 163
        clear();
160 164
        detach();
161 165
      }
162 166
    }
163 167

	
164 168
  protected:
165 169

	
166 170
    using Parent::attach;
167 171
    using Parent::detach;
168 172
    using Parent::attached;
169 173

	
170 174
  public:
171 175

	
172 176
    // \brief The subscript operator.
173 177
    //
174 178
    // The subscript operator. The map can be subscripted by the
175 179
    // actual keys of the graph.
176 180
    Value& operator[](const Key& key) {
177 181
      int id = Parent::notifier()->id(key);
178 182
      return values[id];
179 183
    }
180 184

	
181 185
    // \brief The const subscript operator.
182 186
    //
183 187
    // The const subscript operator. The map can be subscripted by the
184 188
    // actual keys of the graph.
185 189
    const Value& operator[](const Key& key) const {
186 190
      int id = Parent::notifier()->id(key);
187 191
      return values[id];
188 192
    }
189 193

	
190 194
    // \brief Setter function of the map.
191 195
    //
192 196
    // Setter function of the map. Equivalent with map[key] = val.
193 197
    // This is a compatibility feature with the not dereferable maps.
194 198
    void set(const Key& key, const Value& val) {
195 199
      (*this)[key] = val;
196 200
    }
197 201

	
198 202
  protected:
199 203

	
200 204
    // \brief Adds a new key to the map.
201 205
    //
202 206
    // It adds a new key to the map. It is called by the observer notifier
203 207
    // and it overrides the add() member function of the observer base.
204 208
    virtual void add(const Key& key) {
205 209
      Notifier* nf = Parent::notifier();
206 210
      int id = nf->id(key);
207 211
      if (id >= capacity) {
208 212
        int new_capacity = (capacity == 0 ? 1 : capacity);
209 213
        while (new_capacity <= id) {
210 214
          new_capacity <<= 1;
211 215
        }
212 216
        Value* new_values = allocator.allocate(new_capacity);
213 217
        Item it;
214 218
        for (nf->first(it); it != INVALID; nf->next(it)) {
215 219
          int jd = nf->id(it);;
216 220
          if (id != jd) {
217 221
            allocator.construct(&(new_values[jd]), values[jd]);
218 222
            allocator.destroy(&(values[jd]));
219 223
          }
220 224
        }
221 225
        if (capacity != 0) allocator.deallocate(values, capacity);
222 226
        values = new_values;
223 227
        capacity = new_capacity;
224 228
      }
225 229
      allocator.construct(&(values[id]), Value());
226 230
    }
227 231

	
228 232
    // \brief Adds more new keys to the map.
229 233
    //
230 234
    // It adds more new keys to the map. It is called by the observer notifier
231 235
    // and it overrides the add() member function of the observer base.
232 236
    virtual void add(const std::vector<Key>& keys) {
233 237
      Notifier* nf = Parent::notifier();
234 238
      int max_id = -1;
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_DEFAULT_MAP_H
20 20
#define LEMON_BITS_DEFAULT_MAP_H
21 21

	
22
#include <lemon/config.h>
22 23
#include <lemon/bits/array_map.h>
23 24
#include <lemon/bits/vector_map.h>
24 25
//#include <lemon/bits/debug_map.h>
25 26

	
26 27
//\ingroup graphbits
27 28
//\file
28 29
//\brief Graph maps that construct and destruct their elements dynamically.
29 30

	
30 31
namespace lemon {
31 32

	
32 33

	
33 34
  //#ifndef LEMON_USE_DEBUG_MAP
34 35

	
35 36
  template <typename _Graph, typename _Item, typename _Value>
36 37
  struct DefaultMapSelector {
37 38
    typedef ArrayMap<_Graph, _Item, _Value> Map;
38 39
  };
39 40

	
40 41
  // bool
41 42
  template <typename _Graph, typename _Item>
42 43
  struct DefaultMapSelector<_Graph, _Item, bool> {
43 44
    typedef VectorMap<_Graph, _Item, bool> Map;
44 45
  };
45 46

	
46 47
  // char
47 48
  template <typename _Graph, typename _Item>
48 49
  struct DefaultMapSelector<_Graph, _Item, char> {
49 50
    typedef VectorMap<_Graph, _Item, char> Map;
50 51
  };
51 52

	
52 53
  template <typename _Graph, typename _Item>
53 54
  struct DefaultMapSelector<_Graph, _Item, signed char> {
54 55
    typedef VectorMap<_Graph, _Item, signed char> Map;
55 56
  };
56 57

	
57 58
  template <typename _Graph, typename _Item>
58 59
  struct DefaultMapSelector<_Graph, _Item, unsigned char> {
59 60
    typedef VectorMap<_Graph, _Item, unsigned char> Map;
60 61
  };
61 62

	
62 63

	
63 64
  // int
64 65
  template <typename _Graph, typename _Item>
65 66
  struct DefaultMapSelector<_Graph, _Item, signed int> {
66 67
    typedef VectorMap<_Graph, _Item, signed int> Map;
67 68
  };
68 69

	
69 70
  template <typename _Graph, typename _Item>
70 71
  struct DefaultMapSelector<_Graph, _Item, unsigned int> {
71 72
    typedef VectorMap<_Graph, _Item, unsigned int> Map;
72 73
  };
73 74

	
74 75

	
75 76
  // short
76 77
  template <typename _Graph, typename _Item>
77 78
  struct DefaultMapSelector<_Graph, _Item, signed short> {
78 79
    typedef VectorMap<_Graph, _Item, signed short> Map;
79 80
  };
80 81

	
81 82
  template <typename _Graph, typename _Item>
82 83
  struct DefaultMapSelector<_Graph, _Item, unsigned short> {
83 84
    typedef VectorMap<_Graph, _Item, unsigned short> Map;
84 85
  };
85 86

	
86 87

	
87 88
  // long
88 89
  template <typename _Graph, typename _Item>
89 90
  struct DefaultMapSelector<_Graph, _Item, signed long> {
90 91
    typedef VectorMap<_Graph, _Item, signed long> Map;
91 92
  };
92 93

	
93 94
  template <typename _Graph, typename _Item>
94 95
  struct DefaultMapSelector<_Graph, _Item, unsigned long> {
95 96
    typedef VectorMap<_Graph, _Item, unsigned long> Map;
96 97
  };
97 98

	
98 99

	
99
#if defined __GNUC__ && !defined __STRICT_ANSI__
100
#if defined LEMON_HAVE_LONG_LONG
100 101

	
101 102
  // long long
102 103
  template <typename _Graph, typename _Item>
103 104
  struct DefaultMapSelector<_Graph, _Item, signed long long> {
104 105
    typedef VectorMap<_Graph, _Item, signed long long> Map;
105 106
  };
106 107

	
107 108
  template <typename _Graph, typename _Item>
108 109
  struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
109 110
    typedef VectorMap<_Graph, _Item, unsigned long long> Map;
110 111
  };
111 112

	
112 113
#endif
113 114

	
114 115

	
115 116
  // float
116 117
  template <typename _Graph, typename _Item>
117 118
  struct DefaultMapSelector<_Graph, _Item, float> {
118 119
    typedef VectorMap<_Graph, _Item, float> Map;
119 120
  };
120 121

	
121 122

	
122 123
  // double
123 124
  template <typename _Graph, typename _Item>
124 125
  struct DefaultMapSelector<_Graph, _Item, double> {
125 126
    typedef VectorMap<_Graph, _Item,  double> Map;
126 127
  };
127 128

	
128 129

	
129 130
  // long double
130 131
  template <typename _Graph, typename _Item>
131 132
  struct DefaultMapSelector<_Graph, _Item, long double> {
132 133
    typedef VectorMap<_Graph, _Item, long double> Map;
133 134
  };
134 135

	
135 136

	
136 137
  // pointer
137 138
  template <typename _Graph, typename _Item, typename _Ptr>
138 139
  struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
139 140
    typedef VectorMap<_Graph, _Item, _Ptr*> Map;
140 141
  };
141 142

	
142 143
// #else
143 144

	
144 145
//   template <typename _Graph, typename _Item, typename _Value>
145 146
//   struct DefaultMapSelector {
146 147
//     typedef DebugMap<_Graph, _Item, _Value> Map;
147 148
//   };
148 149

	
149 150
// #endif
150 151

	
151 152
  // DefaultMap class
152 153
  template <typename _Graph, typename _Item, typename _Value>
153 154
  class DefaultMap
154 155
    : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
156
    typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
157

	
155 158
  public:
156
    typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
157 159
    typedef DefaultMap<_Graph, _Item, _Value> Map;
158

	
159
    typedef typename Parent::Graph Graph;
160
    
161
    typedef typename Parent::GraphType GraphType;
160 162
    typedef typename Parent::Value Value;
161 163

	
162
    explicit DefaultMap(const Graph& graph) : Parent(graph) {}
163
    DefaultMap(const Graph& graph, const Value& value)
164
    explicit DefaultMap(const GraphType& graph) : Parent(graph) {}
165
    DefaultMap(const GraphType& graph, const Value& value)
164 166
      : Parent(graph, value) {}
165 167

	
166 168
    DefaultMap& operator=(const DefaultMap& cmap) {
167 169
      return operator=<DefaultMap>(cmap);
168 170
    }
169 171

	
170 172
    template <typename CMap>
171 173
    DefaultMap& operator=(const CMap& cmap) {
172 174
      Parent::operator=(cmap);
173 175
      return *this;
174 176
    }
175 177

	
176 178
  };
177 179

	
178 180
}
179 181

	
180 182
#endif
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_EDGE_SET_EXTENDER_H
20 20
#define LEMON_BITS_EDGE_SET_EXTENDER_H
21 21

	
22
#include <lemon/core.h>
22 23
#include <lemon/error.h>
23 24
#include <lemon/bits/default_map.h>
25
#include <lemon/bits/map_extender.h>
24 26

	
25
///\ingroup digraphbits
26
///\file
27
///\brief Extenders for the arc set types
27
//\ingroup digraphbits
28
//\file
29
//\brief Extenders for the arc set types
28 30
namespace lemon {
29 31

	
30
  /// \ingroup digraphbits
31
  ///
32
  /// \brief Extender for the ArcSets
32
  // \ingroup digraphbits
33
  //
34
  // \brief Extender for the ArcSets
33 35
  template <typename Base>
34 36
  class ArcSetExtender : public Base {
37
    typedef Base Parent;
38

	
35 39
  public:
36 40

	
37
    typedef Base Parent;
38 41
    typedef ArcSetExtender Digraph;
39 42

	
40 43
    // Base extensions
41 44

	
42 45
    typedef typename Parent::Node Node;
43 46
    typedef typename Parent::Arc Arc;
44 47

	
45 48
    int maxId(Node) const {
46 49
      return Parent::maxNodeId();
47 50
    }
48 51

	
49 52
    int maxId(Arc) const {
50 53
      return Parent::maxArcId();
51 54
    }
52 55

	
53 56
    Node fromId(int id, Node) const {
54 57
      return Parent::nodeFromId(id);
55 58
    }
56 59

	
57 60
    Arc fromId(int id, Arc) const {
58 61
      return Parent::arcFromId(id);
59 62
    }
60 63

	
61 64
    Node oppositeNode(const Node &n, const Arc &e) const {
62 65
      if (n == Parent::source(e))
63 66
	return Parent::target(e);
64 67
      else if(n==Parent::target(e))
65 68
	return Parent::source(e);
66 69
      else
67 70
	return INVALID;
68 71
    }
69 72

	
70 73

	
71 74
    // Alteration notifier extensions
72 75

	
73
    /// The arc observer registry.
76
    // The arc observer registry.
74 77
    typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier;
75 78

	
76 79
  protected:
77 80

	
78 81
    mutable ArcNotifier arc_notifier;
79 82

	
80 83
  public:
81 84

	
82 85
    using Parent::notifier;
83 86

	
84
    /// \brief Gives back the arc alteration notifier.
85
    ///
86
    /// Gives back the arc alteration notifier.
87
    // Gives back the arc alteration notifier.
87 88
    ArcNotifier& notifier(Arc) const {
88 89
      return arc_notifier;
89 90
    }
90 91

	
91 92
    // Iterable extensions
92 93

	
93 94
    class NodeIt : public Node { 
94 95
      const Digraph* digraph;
95 96
    public:
96 97

	
97 98
      NodeIt() {}
98 99

	
99 100
      NodeIt(Invalid i) : Node(i) { }
100 101

	
101 102
      explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
102 103
	_graph.first(static_cast<Node&>(*this));
103 104
      }
104 105

	
105 106
      NodeIt(const Digraph& _graph, const Node& node) 
106 107
	: Node(node), digraph(&_graph) {}
107 108

	
108 109
      NodeIt& operator++() { 
109 110
	digraph->next(*this);
110 111
	return *this; 
111 112
      }
112 113

	
113 114
    };
114 115

	
115 116

	
116 117
    class ArcIt : public Arc { 
117 118
      const Digraph* digraph;
118 119
    public:
119 120

	
120 121
      ArcIt() { }
121 122

	
122 123
      ArcIt(Invalid i) : Arc(i) { }
123 124

	
124 125
      explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
125 126
	_graph.first(static_cast<Arc&>(*this));
126 127
      }
127 128

	
128 129
      ArcIt(const Digraph& _graph, const Arc& e) : 
129 130
	Arc(e), digraph(&_graph) { }
130 131

	
131 132
      ArcIt& operator++() { 
132 133
	digraph->next(*this);
133 134
	return *this; 
134 135
      }
135 136

	
136 137
    };
137 138

	
138 139

	
139 140
    class OutArcIt : public Arc { 
140 141
      const Digraph* digraph;
141 142
    public:
142 143

	
143 144
      OutArcIt() { }
144 145

	
145 146
      OutArcIt(Invalid i) : Arc(i) { }
146 147

	
147 148
      OutArcIt(const Digraph& _graph, const Node& node) 
148 149
	: digraph(&_graph) {
149 150
	_graph.firstOut(*this, node);
150 151
      }
151 152

	
152 153
      OutArcIt(const Digraph& _graph, const Arc& arc) 
153 154
	: Arc(arc), digraph(&_graph) {}
154 155

	
155 156
      OutArcIt& operator++() { 
156 157
	digraph->nextOut(*this);
157 158
	return *this; 
158 159
      }
159 160

	
160 161
    };
161 162

	
162 163

	
163 164
    class InArcIt : public Arc { 
164 165
      const Digraph* digraph;
165 166
    public:
166 167

	
167 168
      InArcIt() { }
168 169

	
169 170
      InArcIt(Invalid i) : Arc(i) { }
170 171

	
171 172
      InArcIt(const Digraph& _graph, const Node& node) 
172 173
	: digraph(&_graph) {
173 174
	_graph.firstIn(*this, node);
174 175
      }
175 176

	
176 177
      InArcIt(const Digraph& _graph, const Arc& arc) : 
177 178
	Arc(arc), digraph(&_graph) {}
178 179

	
179 180
      InArcIt& operator++() { 
180 181
	digraph->nextIn(*this);
181 182
	return *this; 
182 183
      }
183 184

	
184 185
    };
185 186

	
186
    /// \brief Base node of the iterator
187
    ///
188
    /// Returns the base node (ie. the source in this case) of the iterator
187
    // \brief Base node of the iterator
188
    //
189
    // Returns the base node (ie. the source in this case) of the iterator
189 190
    Node baseNode(const OutArcIt &e) const {
190 191
      return Parent::source(static_cast<const Arc&>(e));
191 192
    }
192
    /// \brief Running node of the iterator
193
    ///
194
    /// Returns the running node (ie. the target in this case) of the
195
    /// iterator
193
    // \brief Running node of the iterator
194
    //
195
    // Returns the running node (ie. the target in this case) of the
196
    // iterator
196 197
    Node runningNode(const OutArcIt &e) const {
197 198
      return Parent::target(static_cast<const Arc&>(e));
198 199
    }
199 200

	
200
    /// \brief Base node of the iterator
201
    ///
202
    /// Returns the base node (ie. the target in this case) of the iterator
201
    // \brief Base node of the iterator
202
    //
203
    // Returns the base node (ie. the target in this case) of the iterator
203 204
    Node baseNode(const InArcIt &e) const {
204 205
      return Parent::target(static_cast<const Arc&>(e));
205 206
    }
206
    /// \brief Running node of the iterator
207
    ///
208
    /// Returns the running node (ie. the source in this case) of the
209
    /// iterator
207
    // \brief Running node of the iterator
208
    //
209
    // Returns the running node (ie. the source in this case) of the
210
    // iterator
210 211
    Node runningNode(const InArcIt &e) const {
211 212
      return Parent::source(static_cast<const Arc&>(e));
212 213
    }
213 214

	
214 215
    using Parent::first;
215 216

	
216 217
    // Mappable extension
217 218
    
218 219
    template <typename _Value>
219 220
    class ArcMap 
220 221
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
221
    public:
222
      typedef ArcSetExtender Digraph;
223 222
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
224 223

	
224
    public:
225 225
      explicit ArcMap(const Digraph& _g) 
226 226
	: Parent(_g) {}
227 227
      ArcMap(const Digraph& _g, const _Value& _v) 
228 228
	: Parent(_g, _v) {}
229 229

	
230 230
      ArcMap& operator=(const ArcMap& cmap) {
231 231
	return operator=<ArcMap>(cmap);
232 232
      }
233 233

	
234 234
      template <typename CMap>
235 235
      ArcMap& operator=(const CMap& cmap) {
236 236
        Parent::operator=(cmap);
237 237
	return *this;
238 238
      }
239 239

	
240 240
    };
241 241

	
242 242

	
243 243
    // Alteration extension
244 244

	
245 245
    Arc addArc(const Node& from, const Node& to) {
246 246
      Arc arc = Parent::addArc(from, to);
247 247
      notifier(Arc()).add(arc);
248 248
      return arc;
249 249
    }
250 250
    
251 251
    void clear() {
252 252
      notifier(Arc()).clear();
253 253
      Parent::clear();
254 254
    }
255 255

	
256 256
    void erase(const Arc& arc) {
257 257
      notifier(Arc()).erase(arc);
258 258
      Parent::erase(arc);
259 259
    }
260 260

	
261 261
    ArcSetExtender() {
262 262
      arc_notifier.setContainer(*this);
263 263
    }
264 264

	
265 265
    ~ArcSetExtender() {
266 266
      arc_notifier.clear();
267 267
    }
268 268

	
269 269
  };
270 270

	
271 271

	
272
  /// \ingroup digraphbits
273
  ///
274
  /// \brief Extender for the EdgeSets
272
  // \ingroup digraphbits
273
  //
274
  // \brief Extender for the EdgeSets
275 275
  template <typename Base>
276 276
  class EdgeSetExtender : public Base {
277
    typedef Base Parent;
277 278

	
278 279
  public:
279 280

	
280
    typedef Base Parent;
281
    typedef EdgeSetExtender Digraph;
281
    typedef EdgeSetExtender Graph;
282 282

	
283 283
    typedef typename Parent::Node Node;
284 284
    typedef typename Parent::Arc Arc;
285 285
    typedef typename Parent::Edge Edge;
286 286

	
287

	
288 287
    int maxId(Node) const {
289 288
      return Parent::maxNodeId();
290 289
    }
291 290

	
292 291
    int maxId(Arc) const {
293 292
      return Parent::maxArcId();
294 293
    }
295 294

	
296 295
    int maxId(Edge) const {
297 296
      return Parent::maxEdgeId();
298 297
    }
299 298

	
300 299
    Node fromId(int id, Node) const {
301 300
      return Parent::nodeFromId(id);
302 301
    }
303 302

	
304 303
    Arc fromId(int id, Arc) const {
305 304
      return Parent::arcFromId(id);
306 305
    }
307 306

	
308 307
    Edge fromId(int id, Edge) const {
309 308
      return Parent::edgeFromId(id);
310 309
    }
311 310

	
312 311
    Node oppositeNode(const Node &n, const Edge &e) const {
313 312
      if( n == Parent::u(e))
314 313
	return Parent::v(e);
315 314
      else if( n == Parent::v(e))
316 315
	return Parent::u(e);
317 316
      else
318 317
	return INVALID;
319 318
    }
320 319

	
321 320
    Arc oppositeArc(const Arc &e) const {
322 321
      return Parent::direct(e, !Parent::direction(e));
323 322
    }
324 323

	
325 324
    using Parent::direct;
326 325
    Arc direct(const Edge &e, const Node &s) const {
327 326
      return Parent::direct(e, Parent::u(e) == s);
328 327
    }
329 328

	
330 329
    typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier;
331 330
    typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier;
332 331

	
333 332

	
334 333
  protected:
335 334

	
336 335
    mutable ArcNotifier arc_notifier;
337 336
    mutable EdgeNotifier edge_notifier;
338 337

	
339 338
  public:
340 339

	
341 340
    using Parent::notifier;
342 341
    
343 342
    ArcNotifier& notifier(Arc) const {
344 343
      return arc_notifier;
345 344
    }
346 345

	
347 346
    EdgeNotifier& notifier(Edge) const {
348 347
      return edge_notifier;
349 348
    }
350 349

	
351 350

	
352 351
    class NodeIt : public Node { 
353
      const Digraph* digraph;
352
      const Graph* graph;
354 353
    public:
355 354

	
356 355
      NodeIt() {}
357 356

	
358 357
      NodeIt(Invalid i) : Node(i) { }
359 358

	
360
      explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
359
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
361 360
	_graph.first(static_cast<Node&>(*this));
362 361
      }
363 362

	
364
      NodeIt(const Digraph& _graph, const Node& node) 
365
	: Node(node), digraph(&_graph) {}
363
      NodeIt(const Graph& _graph, const Node& node) 
364
	: Node(node), graph(&_graph) {}
366 365

	
367 366
      NodeIt& operator++() { 
368
	digraph->next(*this);
367
	graph->next(*this);
369 368
	return *this; 
370 369
      }
371 370

	
372 371
    };
373 372

	
374 373

	
375 374
    class ArcIt : public Arc { 
376
      const Digraph* digraph;
375
      const Graph* graph;
377 376
    public:
378 377

	
379 378
      ArcIt() { }
380 379

	
381 380
      ArcIt(Invalid i) : Arc(i) { }
382 381

	
383
      explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
382
      explicit ArcIt(const Graph& _graph) : graph(&_graph) {
384 383
	_graph.first(static_cast<Arc&>(*this));
385 384
      }
386 385

	
387
      ArcIt(const Digraph& _graph, const Arc& e) : 
388
	Arc(e), digraph(&_graph) { }
386
      ArcIt(const Graph& _graph, const Arc& e) : 
387
	Arc(e), graph(&_graph) { }
389 388

	
390 389
      ArcIt& operator++() { 
391
	digraph->next(*this);
390
	graph->next(*this);
392 391
	return *this; 
393 392
      }
394 393

	
395 394
    };
396 395

	
397 396

	
398 397
    class OutArcIt : public Arc { 
399
      const Digraph* digraph;
398
      const Graph* graph;
400 399
    public:
401 400

	
402 401
      OutArcIt() { }
403 402

	
404 403
      OutArcIt(Invalid i) : Arc(i) { }
405 404

	
406
      OutArcIt(const Digraph& _graph, const Node& node) 
407
	: digraph(&_graph) {
405
      OutArcIt(const Graph& _graph, const Node& node) 
406
	: graph(&_graph) {
408 407
	_graph.firstOut(*this, node);
409 408
      }
410 409

	
411
      OutArcIt(const Digraph& _graph, const Arc& arc) 
412
	: Arc(arc), digraph(&_graph) {}
410
      OutArcIt(const Graph& _graph, const Arc& arc) 
411
	: Arc(arc), graph(&_graph) {}
413 412

	
414 413
      OutArcIt& operator++() { 
415
	digraph->nextOut(*this);
414
	graph->nextOut(*this);
416 415
	return *this; 
417 416
      }
418 417

	
419 418
    };
420 419

	
421 420

	
422 421
    class InArcIt : public Arc { 
423
      const Digraph* digraph;
422
      const Graph* graph;
424 423
    public:
425 424

	
426 425
      InArcIt() { }
427 426

	
428 427
      InArcIt(Invalid i) : Arc(i) { }
429 428

	
430
      InArcIt(const Digraph& _graph, const Node& node) 
431
	: digraph(&_graph) {
429
      InArcIt(const Graph& _graph, const Node& node) 
430
	: graph(&_graph) {
432 431
	_graph.firstIn(*this, node);
433 432
      }
434 433

	
435
      InArcIt(const Digraph& _graph, const Arc& arc) : 
436
	Arc(arc), digraph(&_graph) {}
434
      InArcIt(const Graph& _graph, const Arc& arc) : 
435
	Arc(arc), graph(&_graph) {}
437 436

	
438 437
      InArcIt& operator++() { 
439
	digraph->nextIn(*this);
438
	graph->nextIn(*this);
440 439
	return *this; 
441 440
      }
442 441

	
443 442
    };
444 443

	
445 444

	
446 445
    class EdgeIt : public Parent::Edge { 
447
      const Digraph* digraph;
446
      const Graph* graph;
448 447
    public:
449 448

	
450 449
      EdgeIt() { }
451 450

	
452 451
      EdgeIt(Invalid i) : Edge(i) { }
453 452

	
454
      explicit EdgeIt(const Digraph& _graph) : digraph(&_graph) {
453
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
455 454
	_graph.first(static_cast<Edge&>(*this));
456 455
      }
457 456

	
458
      EdgeIt(const Digraph& _graph, const Edge& e) : 
459
	Edge(e), digraph(&_graph) { }
457
      EdgeIt(const Graph& _graph, const Edge& e) : 
458
	Edge(e), graph(&_graph) { }
460 459

	
461 460
      EdgeIt& operator++() { 
462
	digraph->next(*this);
461
	graph->next(*this);
463 462
	return *this; 
464 463
      }
465 464

	
466 465
    };
467 466

	
468 467
    class IncEdgeIt : public Parent::Edge {
469 468
      friend class EdgeSetExtender;
470
      const Digraph* digraph;
469
      const Graph* graph;
471 470
      bool direction;
472 471
    public:
473 472

	
474 473
      IncEdgeIt() { }
475 474

	
476 475
      IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
477 476

	
478
      IncEdgeIt(const Digraph& _graph, const Node &n) : digraph(&_graph) {
477
      IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
479 478
	_graph.firstInc(*this, direction, n);
480 479
      }
481 480

	
482
      IncEdgeIt(const Digraph& _graph, const Edge &ue, const Node &n)
483
	: digraph(&_graph), Edge(ue) {
481
      IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n)
482
	: graph(&_graph), Edge(ue) {
484 483
	direction = (_graph.source(ue) == n);
485 484
      }
486 485

	
487 486
      IncEdgeIt& operator++() {
488
	digraph->nextInc(*this, direction);
487
	graph->nextInc(*this, direction);
489 488
	return *this; 
490 489
      }
491 490
    };
492 491

	
493
    /// \brief Base node of the iterator
494
    ///
495
    /// Returns the base node (ie. the source in this case) of the iterator
492
    // \brief Base node of the iterator
493
    //
494
    // Returns the base node (ie. the source in this case) of the iterator
496 495
    Node baseNode(const OutArcIt &e) const {
497 496
      return Parent::source(static_cast<const Arc&>(e));
498 497
    }
499
    /// \brief Running node of the iterator
500
    ///
501
    /// Returns the running node (ie. the target in this case) of the
502
    /// iterator
498
    // \brief Running node of the iterator
499
    //
500
    // Returns the running node (ie. the target in this case) of the
501
    // iterator
503 502
    Node runningNode(const OutArcIt &e) const {
504 503
      return Parent::target(static_cast<const Arc&>(e));
505 504
    }
506 505

	
507
    /// \brief Base node of the iterator
508
    ///
509
    /// Returns the base node (ie. the target in this case) of the iterator
506
    // \brief Base node of the iterator
507
    //
508
    // Returns the base node (ie. the target in this case) of the iterator
510 509
    Node baseNode(const InArcIt &e) const {
511 510
      return Parent::target(static_cast<const Arc&>(e));
512 511
    }
513
    /// \brief Running node of the iterator
514
    ///
515
    /// Returns the running node (ie. the source in this case) of the
516
    /// iterator
512
    // \brief Running node of the iterator
513
    //
514
    // Returns the running node (ie. the source in this case) of the
515
    // iterator
517 516
    Node runningNode(const InArcIt &e) const {
518 517
      return Parent::source(static_cast<const Arc&>(e));
519 518
    }
520 519

	
521
    /// Base node of the iterator
522
    ///
523
    /// Returns the base node of the iterator
520
    // Base node of the iterator
521
    //
522
    // Returns the base node of the iterator
524 523
    Node baseNode(const IncEdgeIt &e) const {
525 524
      return e.direction ? u(e) : v(e);
526 525
    }
527
    /// Running node of the iterator
528
    ///
529
    /// Returns the running node of the iterator
526
    // Running node of the iterator
527
    //
528
    // Returns the running node of the iterator
530 529
    Node runningNode(const IncEdgeIt &e) const {
531 530
      return e.direction ? v(e) : u(e);
532 531
    }
533 532

	
534 533

	
535 534
    template <typename _Value>
536 535
    class ArcMap 
537
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
536
      : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
537
      typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
538

	
538 539
    public:
539
      typedef EdgeSetExtender Digraph;
540
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
541

	
542
      ArcMap(const Digraph& _g) 
540
      explicit ArcMap(const Graph& _g) 
543 541
	: Parent(_g) {}
544
      ArcMap(const Digraph& _g, const _Value& _v) 
542
      ArcMap(const Graph& _g, const _Value& _v) 
545 543
	: Parent(_g, _v) {}
546 544

	
547 545
      ArcMap& operator=(const ArcMap& cmap) {
548 546
	return operator=<ArcMap>(cmap);
549 547
      }
550 548

	
551 549
      template <typename CMap>
552 550
      ArcMap& operator=(const CMap& cmap) {
553 551
        Parent::operator=(cmap);
554 552
	return *this;
555 553
      }
556 554

	
557 555
    };
558 556

	
559 557

	
560 558
    template <typename _Value>
561 559
    class EdgeMap 
562
      : public MapExtender<DefaultMap<Digraph, Edge, _Value> > {
560
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
561
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
562

	
563 563
    public:
564
      typedef EdgeSetExtender Digraph;
565
      typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent;
566

	
567
      EdgeMap(const Digraph& _g) 
564
      explicit EdgeMap(const Graph& _g) 
568 565
	: Parent(_g) {}
569 566

	
570
      EdgeMap(const Digraph& _g, const _Value& _v) 
567
      EdgeMap(const Graph& _g, const _Value& _v) 
571 568
	: Parent(_g, _v) {}
572 569

	
573 570
      EdgeMap& operator=(const EdgeMap& cmap) {
574 571
	return operator=<EdgeMap>(cmap);
575 572
      }
576 573

	
577 574
      template <typename CMap>
578 575
      EdgeMap& operator=(const CMap& cmap) {
579 576
        Parent::operator=(cmap);
580 577
	return *this;
581 578
      }
582 579

	
583 580
    };
584 581

	
585 582

	
586 583
    // Alteration extension
587 584

	
588 585
    Edge addEdge(const Node& from, const Node& to) {
589 586
      Edge edge = Parent::addEdge(from, to);
590 587
      notifier(Edge()).add(edge);
591 588
      std::vector<Arc> arcs;
592 589
      arcs.push_back(Parent::direct(edge, true));
593 590
      arcs.push_back(Parent::direct(edge, false));
594 591
      notifier(Arc()).add(arcs);
595 592
      return edge;
596 593
    }
597 594
    
598 595
    void clear() {
599 596
      notifier(Arc()).clear();
600 597
      notifier(Edge()).clear();
601 598
      Parent::clear();
602 599
    }
603 600

	
604 601
    void erase(const Edge& edge) {
605 602
      std::vector<Arc> arcs;
606 603
      arcs.push_back(Parent::direct(edge, true));
607 604
      arcs.push_back(Parent::direct(edge, false));
608 605
      notifier(Arc()).erase(arcs);
609 606
      notifier(Edge()).erase(edge);
610 607
      Parent::erase(edge);
611 608
    }
612 609

	
613 610

	
614 611
    EdgeSetExtender() {
615 612
      arc_notifier.setContainer(*this);
616 613
      edge_notifier.setContainer(*this);
617 614
    }
618 615

	
619 616
    ~EdgeSetExtender() {
620 617
      edge_notifier.clear();
621 618
      arc_notifier.clear();
622 619
    }
623 620
    
624 621
  };
625 622

	
626 623
}
627 624

	
628 625
#endif
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
20 20
#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
21 21

	
22 22
#include <lemon/core.h>
23 23
#include <lemon/error.h>
24 24

	
25
#include <lemon/bits/default_map.h>
26

	
27 25
namespace lemon {
28 26

	
29 27
  template <typename _Digraph>
30 28
  class DigraphAdaptorExtender : public _Digraph {
29
    typedef _Digraph Parent;
30

	
31 31
  public:
32 32

	
33
    typedef _Digraph Parent;
34 33
    typedef _Digraph Digraph;
35 34
    typedef DigraphAdaptorExtender Adaptor;
36 35

	
37 36
    // Base extensions
38 37

	
39 38
    typedef typename Parent::Node Node;
40 39
    typedef typename Parent::Arc Arc;
41 40

	
42 41
    int maxId(Node) const {
43 42
      return Parent::maxNodeId();
44 43
    }
45 44

	
46 45
    int maxId(Arc) const {
47 46
      return Parent::maxArcId();
48 47
    }
49 48

	
50 49
    Node fromId(int id, Node) const {
51 50
      return Parent::nodeFromId(id);
52 51
    }
53 52

	
54 53
    Arc fromId(int id, Arc) const {
55 54
      return Parent::arcFromId(id);
56 55
    }
57 56

	
58 57
    Node oppositeNode(const Node &n, const Arc &e) const {
59 58
      if (n == Parent::source(e))
60 59
        return Parent::target(e);
61 60
      else if(n==Parent::target(e))
62 61
        return Parent::source(e);
63 62
      else
64 63
        return INVALID;
65 64
    }
66 65

	
67 66
    class NodeIt : public Node {
68 67
      const Adaptor* _adaptor;
69 68
    public:
70 69

	
71 70
      NodeIt() {}
72 71

	
73 72
      NodeIt(Invalid i) : Node(i) { }
74 73

	
75 74
      explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
76 75
        _adaptor->first(static_cast<Node&>(*this));
77 76
      }
78 77

	
79 78
      NodeIt(const Adaptor& adaptor, const Node& node)
80 79
        : Node(node), _adaptor(&adaptor) {}
81 80

	
82 81
      NodeIt& operator++() {
83 82
        _adaptor->next(*this);
84 83
        return *this;
85 84
      }
86 85

	
87 86
    };
88 87

	
89 88

	
90 89
    class ArcIt : public Arc {
91 90
      const Adaptor* _adaptor;
92 91
    public:
93 92

	
94 93
      ArcIt() { }
95 94

	
96 95
      ArcIt(Invalid i) : Arc(i) { }
97 96

	
98 97
      explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
99 98
        _adaptor->first(static_cast<Arc&>(*this));
100 99
      }
101 100

	
102 101
      ArcIt(const Adaptor& adaptor, const Arc& e) :
103 102
        Arc(e), _adaptor(&adaptor) { }
104 103

	
105 104
      ArcIt& operator++() {
106 105
        _adaptor->next(*this);
107 106
        return *this;
108 107
      }
109 108

	
110 109
    };
111 110

	
112 111

	
113 112
    class OutArcIt : public Arc {
114 113
      const Adaptor* _adaptor;
115 114
    public:
116 115

	
117 116
      OutArcIt() { }
118 117

	
119 118
      OutArcIt(Invalid i) : Arc(i) { }
120 119

	
121 120
      OutArcIt(const Adaptor& adaptor, const Node& node)
122 121
        : _adaptor(&adaptor) {
123 122
        _adaptor->firstOut(*this, node);
124 123
      }
125 124

	
126 125
      OutArcIt(const Adaptor& adaptor, const Arc& arc)
127 126
        : Arc(arc), _adaptor(&adaptor) {}
128 127

	
129 128
      OutArcIt& operator++() {
130 129
        _adaptor->nextOut(*this);
131 130
        return *this;
132 131
      }
133 132

	
134 133
    };
135 134

	
136 135

	
137 136
    class InArcIt : public Arc {
138 137
      const Adaptor* _adaptor;
139 138
    public:
140 139

	
141 140
      InArcIt() { }
142 141

	
143 142
      InArcIt(Invalid i) : Arc(i) { }
144 143

	
145 144
      InArcIt(const Adaptor& adaptor, const Node& node)
146 145
        : _adaptor(&adaptor) {
147 146
        _adaptor->firstIn(*this, node);
148 147
      }
149 148

	
150 149
      InArcIt(const Adaptor& adaptor, const Arc& arc) :
151 150
        Arc(arc), _adaptor(&adaptor) {}
152 151

	
153 152
      InArcIt& operator++() {
154 153
        _adaptor->nextIn(*this);
155 154
        return *this;
156 155
      }
157 156

	
158 157
    };
159 158

	
160 159
    Node baseNode(const OutArcIt &e) const {
161 160
      return Parent::source(e);
162 161
    }
163 162
    Node runningNode(const OutArcIt &e) const {
164 163
      return Parent::target(e);
165 164
    }
166 165

	
167 166
    Node baseNode(const InArcIt &e) const {
168 167
      return Parent::target(e);
169 168
    }
170 169
    Node runningNode(const InArcIt &e) const {
171 170
      return Parent::source(e);
172 171
    }
173 172

	
174 173
  };
175 174

	
176 175
  template <typename _Graph>
177 176
  class GraphAdaptorExtender : public _Graph {
177
    typedef _Graph Parent;
178

	
178 179
  public:
179 180

	
180
    typedef _Graph Parent;
181 181
    typedef _Graph Graph;
182 182
    typedef GraphAdaptorExtender Adaptor;
183 183

	
184 184
    typedef typename Parent::Node Node;
185 185
    typedef typename Parent::Arc Arc;
186 186
    typedef typename Parent::Edge Edge;
187 187

	
188 188
    // Graph extension
189 189

	
190 190
    int maxId(Node) const {
191 191
      return Parent::maxNodeId();
192 192
    }
193 193

	
194 194
    int maxId(Arc) const {
195 195
      return Parent::maxArcId();
196 196
    }
197 197

	
198 198
    int maxId(Edge) const {
199 199
      return Parent::maxEdgeId();
200 200
    }
201 201

	
202 202
    Node fromId(int id, Node) const {
203 203
      return Parent::nodeFromId(id);
204 204
    }
205 205

	
206 206
    Arc fromId(int id, Arc) const {
207 207
      return Parent::arcFromId(id);
208 208
    }
209 209

	
210 210
    Edge fromId(int id, Edge) const {
211 211
      return Parent::edgeFromId(id);
212 212
    }
213 213

	
214 214
    Node oppositeNode(const Node &n, const Edge &e) const {
215 215
      if( n == Parent::u(e))
216 216
        return Parent::v(e);
217 217
      else if( n == Parent::v(e))
218 218
        return Parent::u(e);
219 219
      else
220 220
        return INVALID;
221 221
    }
222 222

	
223 223
    Arc oppositeArc(const Arc &a) const {
224 224
      return Parent::direct(a, !Parent::direction(a));
225 225
    }
226 226

	
227 227
    using Parent::direct;
228 228
    Arc direct(const Edge &e, const Node &s) const {
229 229
      return Parent::direct(e, Parent::u(e) == s);
230 230
    }
231 231

	
232 232

	
233 233
    class NodeIt : public Node {
234 234
      const Adaptor* _adaptor;
235 235
    public:
236 236

	
237 237
      NodeIt() {}
238 238

	
239 239
      NodeIt(Invalid i) : Node(i) { }
240 240

	
241 241
      explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
242 242
        _adaptor->first(static_cast<Node&>(*this));
243 243
      }
244 244

	
245 245
      NodeIt(const Adaptor& adaptor, const Node& node)
246 246
        : Node(node), _adaptor(&adaptor) {}
247 247

	
248 248
      NodeIt& operator++() {
249 249
        _adaptor->next(*this);
250 250
        return *this;
251 251
      }
252 252

	
253 253
    };
254 254

	
255 255

	
256 256
    class ArcIt : public Arc {
257 257
      const Adaptor* _adaptor;
258 258
    public:
259 259

	
260 260
      ArcIt() { }
261 261

	
262 262
      ArcIt(Invalid i) : Arc(i) { }
263 263

	
264 264
      explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
265 265
        _adaptor->first(static_cast<Arc&>(*this));
266 266
      }
267 267

	
268 268
      ArcIt(const Adaptor& adaptor, const Arc& e) :
269 269
        Arc(e), _adaptor(&adaptor) { }
270 270

	
271 271
      ArcIt& operator++() {
272 272
        _adaptor->next(*this);
273 273
        return *this;
274 274
      }
275 275

	
276 276
    };
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_GRAPH_EXTENDER_H
20 20
#define LEMON_BITS_GRAPH_EXTENDER_H
21 21

	
22 22
#include <lemon/core.h>
23 23

	
24 24
#include <lemon/bits/map_extender.h>
25 25
#include <lemon/bits/default_map.h>
26 26

	
27 27
#include <lemon/concept_check.h>
28 28
#include <lemon/concepts/maps.h>
29 29

	
30 30
//\ingroup graphbits
31 31
//\file
32 32
//\brief Extenders for the graph types
33 33
namespace lemon {
34 34

	
35 35
  // \ingroup graphbits
36 36
  //
37 37
  // \brief Extender for the digraph implementations
38 38
  template <typename Base>
39 39
  class DigraphExtender : public Base {
40
    typedef Base Parent;
41

	
40 42
  public:
41 43

	
42
    typedef Base Parent;
43 44
    typedef DigraphExtender Digraph;
44 45

	
45 46
    // Base extensions
46 47

	
47 48
    typedef typename Parent::Node Node;
48 49
    typedef typename Parent::Arc Arc;
49 50

	
50 51
    int maxId(Node) const {
51 52
      return Parent::maxNodeId();
52 53
    }
53 54

	
54 55
    int maxId(Arc) const {
55 56
      return Parent::maxArcId();
56 57
    }
57 58

	
58
    Node fromId(int id, Node) const {
59
    static Node fromId(int id, Node) {
59 60
      return Parent::nodeFromId(id);
60 61
    }
61 62

	
62
    Arc fromId(int id, Arc) const {
63
    static Arc fromId(int id, Arc) {
63 64
      return Parent::arcFromId(id);
64 65
    }
65 66

	
66 67
    Node oppositeNode(const Node &node, const Arc &arc) const {
67 68
      if (node == Parent::source(arc))
68 69
        return Parent::target(arc);
69 70
      else if(node == Parent::target(arc))
70 71
        return Parent::source(arc);
71 72
      else
72 73
        return INVALID;
73 74
    }
74 75

	
75 76
    // Alterable extension
76 77

	
77 78
    typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
78 79
    typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
79 80

	
80 81

	
81 82
  protected:
82 83

	
83 84
    mutable NodeNotifier node_notifier;
84 85
    mutable ArcNotifier arc_notifier;
85 86

	
86 87
  public:
87 88

	
88 89
    NodeNotifier& notifier(Node) const {
89 90
      return node_notifier;
90 91
    }
91 92

	
92 93
    ArcNotifier& notifier(Arc) const {
93 94
      return arc_notifier;
94 95
    }
95 96

	
96 97
    class NodeIt : public Node {
97 98
      const Digraph* _digraph;
98 99
    public:
99 100

	
100 101
      NodeIt() {}
101 102

	
102 103
      NodeIt(Invalid i) : Node(i) { }
103 104

	
104 105
      explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
105 106
        _digraph->first(static_cast<Node&>(*this));
106 107
      }
107 108

	
108 109
      NodeIt(const Digraph& digraph, const Node& node)
109 110
        : Node(node), _digraph(&digraph) {}
110 111

	
111 112
      NodeIt& operator++() {
112 113
        _digraph->next(*this);
113 114
        return *this;
114 115
      }
115 116

	
116 117
    };
117 118

	
118 119

	
119 120
    class ArcIt : public Arc {
120 121
      const Digraph* _digraph;
121 122
    public:
122 123

	
123 124
      ArcIt() { }
124 125

	
125 126
      ArcIt(Invalid i) : Arc(i) { }
126 127

	
127 128
      explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
128 129
        _digraph->first(static_cast<Arc&>(*this));
129 130
      }
130 131

	
131 132
      ArcIt(const Digraph& digraph, const Arc& arc) :
132 133
        Arc(arc), _digraph(&digraph) { }
133 134

	
134 135
      ArcIt& operator++() {
135 136
        _digraph->next(*this);
136 137
        return *this;
137 138
      }
138 139

	
139 140
    };
140 141

	
141 142

	
142 143
    class OutArcIt : public Arc {
143 144
      const Digraph* _digraph;
144 145
    public:
145 146

	
146 147
      OutArcIt() { }
147 148

	
148 149
      OutArcIt(Invalid i) : Arc(i) { }
149 150

	
150 151
      OutArcIt(const Digraph& digraph, const Node& node)
151 152
        : _digraph(&digraph) {
152 153
        _digraph->firstOut(*this, node);
153 154
      }
154 155

	
155 156
      OutArcIt(const Digraph& digraph, const Arc& arc)
156 157
        : Arc(arc), _digraph(&digraph) {}
157 158

	
158 159
      OutArcIt& operator++() {
159 160
        _digraph->nextOut(*this);
160 161
        return *this;
161 162
      }
162 163

	
163 164
    };
164 165

	
165 166

	
166 167
    class InArcIt : public Arc {
167 168
      const Digraph* _digraph;
168 169
    public:
169 170

	
170 171
      InArcIt() { }
171 172

	
172 173
      InArcIt(Invalid i) : Arc(i) { }
173 174

	
174 175
      InArcIt(const Digraph& digraph, const Node& node)
175 176
        : _digraph(&digraph) {
176 177
        _digraph->firstIn(*this, node);
177 178
      }
178 179

	
179 180
      InArcIt(const Digraph& digraph, const Arc& arc) :
180 181
        Arc(arc), _digraph(&digraph) {}
181 182

	
182 183
      InArcIt& operator++() {
183 184
        _digraph->nextIn(*this);
184 185
        return *this;
185 186
      }
186 187

	
187 188
    };
188 189

	
189 190
    // \brief Base node of the iterator
190 191
    //
191 192
    // Returns the base node (i.e. the source in this case) of the iterator
192 193
    Node baseNode(const OutArcIt &arc) const {
193 194
      return Parent::source(arc);
194 195
    }
195 196
    // \brief Running node of the iterator
196 197
    //
197 198
    // Returns the running node (i.e. the target in this case) of the
198 199
    // iterator
199 200
    Node runningNode(const OutArcIt &arc) const {
200 201
      return Parent::target(arc);
201 202
    }
202 203

	
203 204
    // \brief Base node of the iterator
204 205
    //
205 206
    // Returns the base node (i.e. the target in this case) of the iterator
206 207
    Node baseNode(const InArcIt &arc) const {
207 208
      return Parent::target(arc);
208 209
    }
209 210
    // \brief Running node of the iterator
210 211
    //
211 212
    // Returns the running node (i.e. the source in this case) of the
212 213
    // iterator
213 214
    Node runningNode(const InArcIt &arc) const {
214 215
      return Parent::source(arc);
215 216
    }
216 217

	
217 218

	
218 219
    template <typename _Value>
219 220
    class NodeMap
220 221
      : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
221
    public:
222
      typedef DigraphExtender Digraph;
223 222
      typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
224 223

	
224
    public:
225 225
      explicit NodeMap(const Digraph& digraph)
226 226
        : Parent(digraph) {}
227 227
      NodeMap(const Digraph& digraph, const _Value& value)
228 228
        : Parent(digraph, value) {}
229 229

	
230 230
    private:
231 231
      NodeMap& operator=(const NodeMap& cmap) {
232 232
        return operator=<NodeMap>(cmap);
233 233
      }
234 234

	
235 235
      template <typename CMap>
236 236
      NodeMap& operator=(const CMap& cmap) {
237 237
        Parent::operator=(cmap);
238 238
        return *this;
239 239
      }
240 240

	
241 241
    };
242 242

	
243 243
    template <typename _Value>
244 244
    class ArcMap
245 245
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
246
    public:
247
      typedef DigraphExtender Digraph;
248 246
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
249 247

	
248
    public:
250 249
      explicit ArcMap(const Digraph& digraph)
251 250
        : Parent(digraph) {}
252 251
      ArcMap(const Digraph& digraph, const _Value& value)
253 252
        : Parent(digraph, value) {}
254 253

	
255 254
    private:
256 255
      ArcMap& operator=(const ArcMap& cmap) {
257 256
        return operator=<ArcMap>(cmap);
258 257
      }
259 258

	
260 259
      template <typename CMap>
261 260
      ArcMap& operator=(const CMap& cmap) {
262 261
        Parent::operator=(cmap);
263 262
        return *this;
264 263
      }
265 264
    };
266 265

	
267 266

	
268 267
    Node addNode() {
269 268
      Node node = Parent::addNode();
270 269
      notifier(Node()).add(node);
271 270
      return node;
272 271
    }
273 272

	
274 273
    Arc addArc(const Node& from, const Node& to) {
275 274
      Arc arc = Parent::addArc(from, to);
276 275
      notifier(Arc()).add(arc);
277 276
      return arc;
278 277
    }
279 278

	
280 279
    void clear() {
281 280
      notifier(Arc()).clear();
282 281
      notifier(Node()).clear();
283 282
      Parent::clear();
284 283
    }
285 284

	
286 285
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
287 286
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
288 287
      Parent::build(digraph, nodeRef, arcRef);
289 288
      notifier(Node()).build();
290 289
      notifier(Arc()).build();
291 290
    }
292 291

	
293 292
    void erase(const Node& node) {
294 293
      Arc arc;
295 294
      Parent::firstOut(arc, node);
296 295
      while (arc != INVALID ) {
297 296
        erase(arc);
298 297
        Parent::firstOut(arc, node);
299 298
      }
300 299

	
301 300
      Parent::firstIn(arc, node);
302 301
      while (arc != INVALID ) {
303 302
        erase(arc);
304 303
        Parent::firstIn(arc, node);
305 304
      }
306 305

	
307 306
      notifier(Node()).erase(node);
308 307
      Parent::erase(node);
309 308
    }
310 309

	
311 310
    void erase(const Arc& arc) {
312 311
      notifier(Arc()).erase(arc);
313 312
      Parent::erase(arc);
314 313
    }
315 314

	
316 315
    DigraphExtender() {
317 316
      node_notifier.setContainer(*this);
318 317
      arc_notifier.setContainer(*this);
319 318
    }
320 319

	
321 320

	
322 321
    ~DigraphExtender() {
323 322
      arc_notifier.clear();
324 323
      node_notifier.clear();
325 324
    }
326 325
  };
327 326

	
328 327
  // \ingroup _graphbits
329 328
  //
330 329
  // \brief Extender for the Graphs
331 330
  template <typename Base>
332 331
  class GraphExtender : public Base {
332
    typedef Base Parent;
333

	
333 334
  public:
334 335

	
335
    typedef Base Parent;
336 336
    typedef GraphExtender Graph;
337 337

	
338 338
    typedef True UndirectedTag;
339 339

	
340 340
    typedef typename Parent::Node Node;
341 341
    typedef typename Parent::Arc Arc;
342 342
    typedef typename Parent::Edge Edge;
343 343

	
344 344
    // Graph extension
345 345

	
346 346
    int maxId(Node) const {
347 347
      return Parent::maxNodeId();
348 348
    }
349 349

	
350 350
    int maxId(Arc) const {
351 351
      return Parent::maxArcId();
352 352
    }
353 353

	
354 354
    int maxId(Edge) const {
355 355
      return Parent::maxEdgeId();
356 356
    }
357 357

	
358
    Node fromId(int id, Node) const {
358
    static Node fromId(int id, Node) {
359 359
      return Parent::nodeFromId(id);
360 360
    }
361 361

	
362
    Arc fromId(int id, Arc) const {
362
    static Arc fromId(int id, Arc) {
363 363
      return Parent::arcFromId(id);
364 364
    }
365 365

	
366
    Edge fromId(int id, Edge) const {
366
    static Edge fromId(int id, Edge) {
367 367
      return Parent::edgeFromId(id);
368 368
    }
369 369

	
370 370
    Node oppositeNode(const Node &n, const Edge &e) const {
371 371
      if( n == Parent::u(e))
372 372
        return Parent::v(e);
373 373
      else if( n == Parent::v(e))
374 374
        return Parent::u(e);
375 375
      else
376 376
        return INVALID;
377 377
    }
378 378

	
379 379
    Arc oppositeArc(const Arc &arc) const {
380 380
      return Parent::direct(arc, !Parent::direction(arc));
381 381
    }
382 382

	
383 383
    using Parent::direct;
384 384
    Arc direct(const Edge &edge, const Node &node) const {
385 385
      return Parent::direct(edge, Parent::u(edge) == node);
386 386
    }
387 387

	
388 388
    // Alterable extension
389 389

	
390 390
    typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
391 391
    typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
392 392
    typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
393 393

	
394 394

	
395 395
  protected:
396 396

	
397 397
    mutable NodeNotifier node_notifier;
398 398
    mutable ArcNotifier arc_notifier;
399 399
    mutable EdgeNotifier edge_notifier;
400 400

	
401 401
  public:
402 402

	
403 403
    NodeNotifier& notifier(Node) const {
404 404
      return node_notifier;
405 405
    }
406 406

	
407 407
    ArcNotifier& notifier(Arc) const {
408 408
      return arc_notifier;
409 409
    }
410 410

	
411 411
    EdgeNotifier& notifier(Edge) const {
412 412
      return edge_notifier;
413 413
    }
414 414

	
415 415

	
416 416

	
417 417
    class NodeIt : public Node {
418 418
      const Graph* _graph;
419 419
    public:
420 420

	
421 421
      NodeIt() {}
422 422

	
423 423
      NodeIt(Invalid i) : Node(i) { }
424 424

	
425 425
      explicit NodeIt(const Graph& graph) : _graph(&graph) {
426 426
        _graph->first(static_cast<Node&>(*this));
427 427
      }
428 428

	
429 429
      NodeIt(const Graph& graph, const Node& node)
430 430
        : Node(node), _graph(&graph) {}
431 431

	
432 432
      NodeIt& operator++() {
433 433
        _graph->next(*this);
434 434
        return *this;
435 435
      }
436 436

	
437 437
    };
438 438

	
439 439

	
440 440
    class ArcIt : public Arc {
441 441
      const Graph* _graph;
442 442
    public:
443 443

	
444 444
      ArcIt() { }
445 445

	
446 446
      ArcIt(Invalid i) : Arc(i) { }
447 447

	
448 448
      explicit ArcIt(const Graph& graph) : _graph(&graph) {
449 449
        _graph->first(static_cast<Arc&>(*this));
450 450
      }
451 451

	
452 452
      ArcIt(const Graph& graph, const Arc& arc) :
453 453
        Arc(arc), _graph(&graph) { }
454 454

	
455 455
      ArcIt& operator++() {
456 456
        _graph->next(*this);
457 457
        return *this;
458 458
      }
459 459

	
460 460
    };
461 461

	
462 462

	
... ...
@@ -508,247 +508,244 @@
508 508
    };
509 509

	
510 510

	
511 511
    class EdgeIt : public Parent::Edge {
512 512
      const Graph* _graph;
513 513
    public:
514 514

	
515 515
      EdgeIt() { }
516 516

	
517 517
      EdgeIt(Invalid i) : Edge(i) { }
518 518

	
519 519
      explicit EdgeIt(const Graph& graph) : _graph(&graph) {
520 520
        _graph->first(static_cast<Edge&>(*this));
521 521
      }
522 522

	
523 523
      EdgeIt(const Graph& graph, const Edge& edge) :
524 524
        Edge(edge), _graph(&graph) { }
525 525

	
526 526
      EdgeIt& operator++() {
527 527
        _graph->next(*this);
528 528
        return *this;
529 529
      }
530 530

	
531 531
    };
532 532

	
533 533
    class IncEdgeIt : public Parent::Edge {
534 534
      friend class GraphExtender;
535 535
      const Graph* _graph;
536 536
      bool _direction;
537 537
    public:
538 538

	
539 539
      IncEdgeIt() { }
540 540

	
541 541
      IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
542 542

	
543 543
      IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
544 544
        _graph->firstInc(*this, _direction, node);
545 545
      }
546 546

	
547 547
      IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node)
548 548
        : _graph(&graph), Edge(edge) {
549 549
        _direction = (_graph->source(edge) == node);
550 550
      }
551 551

	
552 552
      IncEdgeIt& operator++() {
553 553
        _graph->nextInc(*this, _direction);
554 554
        return *this;
555 555
      }
556 556
    };
557 557

	
558 558
    // \brief Base node of the iterator
559 559
    //
560 560
    // Returns the base node (ie. the source in this case) of the iterator
561 561
    Node baseNode(const OutArcIt &arc) const {
562 562
      return Parent::source(static_cast<const Arc&>(arc));
563 563
    }
564 564
    // \brief Running node of the iterator
565 565
    //
566 566
    // Returns the running node (ie. the target in this case) of the
567 567
    // iterator
568 568
    Node runningNode(const OutArcIt &arc) const {
569 569
      return Parent::target(static_cast<const Arc&>(arc));
570 570
    }
571 571

	
572 572
    // \brief Base node of the iterator
573 573
    //
574 574
    // Returns the base node (ie. the target in this case) of the iterator
575 575
    Node baseNode(const InArcIt &arc) const {
576 576
      return Parent::target(static_cast<const Arc&>(arc));
577 577
    }
578 578
    // \brief Running node of the iterator
579 579
    //
580 580
    // Returns the running node (ie. the source in this case) of the
581 581
    // iterator
582 582
    Node runningNode(const InArcIt &arc) const {
583 583
      return Parent::source(static_cast<const Arc&>(arc));
584 584
    }
585 585

	
586 586
    // Base node of the iterator
587 587
    //
588 588
    // Returns the base node of the iterator
589 589
    Node baseNode(const IncEdgeIt &edge) const {
590 590
      return edge._direction ? u(edge) : v(edge);
591 591
    }
592 592
    // Running node of the iterator
593 593
    //
594 594
    // Returns the running node of the iterator
595 595
    Node runningNode(const IncEdgeIt &edge) const {
596 596
      return edge._direction ? v(edge) : u(edge);
597 597
    }
598 598

	
599 599
    // Mappable extension
600 600

	
601 601
    template <typename _Value>
602 602
    class NodeMap
603 603
      : public MapExtender<DefaultMap<Graph, Node, _Value> > {
604
    public:
605
      typedef GraphExtender Graph;
606 604
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
607 605

	
608
      NodeMap(const Graph& graph)
606
    public:
607
      explicit NodeMap(const Graph& graph)
609 608
        : Parent(graph) {}
610 609
      NodeMap(const Graph& graph, const _Value& value)
611 610
        : Parent(graph, value) {}
612 611

	
613 612
    private:
614 613
      NodeMap& operator=(const NodeMap& cmap) {
615 614
        return operator=<NodeMap>(cmap);
616 615
      }
617 616

	
618 617
      template <typename CMap>
619 618
      NodeMap& operator=(const CMap& cmap) {
620 619
        Parent::operator=(cmap);
621 620
        return *this;
622 621
      }
623 622

	
624 623
    };
625 624

	
626 625
    template <typename _Value>
627 626
    class ArcMap
628 627
      : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
629
    public:
630
      typedef GraphExtender Graph;
631 628
      typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
632 629

	
633
      ArcMap(const Graph& graph)
630
    public:
631
      explicit ArcMap(const Graph& graph)
634 632
        : Parent(graph) {}
635 633
      ArcMap(const Graph& graph, const _Value& value)
636 634
        : Parent(graph, value) {}
637 635

	
638 636
    private:
639 637
      ArcMap& operator=(const ArcMap& cmap) {
640 638
        return operator=<ArcMap>(cmap);
641 639
      }
642 640

	
643 641
      template <typename CMap>
644 642
      ArcMap& operator=(const CMap& cmap) {
645 643
        Parent::operator=(cmap);
646 644
        return *this;
647 645
      }
648 646
    };
649 647

	
650 648

	
651 649
    template <typename _Value>
652 650
    class EdgeMap
653 651
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
654
    public:
655
      typedef GraphExtender Graph;
656 652
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
657 653

	
658
      EdgeMap(const Graph& graph)
654
    public:
655
      explicit EdgeMap(const Graph& graph)
659 656
        : Parent(graph) {}
660 657

	
661 658
      EdgeMap(const Graph& graph, const _Value& value)
662 659
        : Parent(graph, value) {}
663 660

	
664 661
    private:
665 662
      EdgeMap& operator=(const EdgeMap& cmap) {
666 663
        return operator=<EdgeMap>(cmap);
667 664
      }
668 665

	
669 666
      template <typename CMap>
670 667
      EdgeMap& operator=(const CMap& cmap) {
671 668
        Parent::operator=(cmap);
672 669
        return *this;
673 670
      }
674 671

	
675 672
    };
676 673

	
677 674
    // Alteration extension
678 675

	
679 676
    Node addNode() {
680 677
      Node node = Parent::addNode();
681 678
      notifier(Node()).add(node);
682 679
      return node;
683 680
    }
684 681

	
685 682
    Edge addEdge(const Node& from, const Node& to) {
686 683
      Edge edge = Parent::addEdge(from, to);
687 684
      notifier(Edge()).add(edge);
688 685
      std::vector<Arc> ev;
689 686
      ev.push_back(Parent::direct(edge, true));
690 687
      ev.push_back(Parent::direct(edge, false));
691 688
      notifier(Arc()).add(ev);
692 689
      return edge;
693 690
    }
694 691

	
695 692
    void clear() {
696 693
      notifier(Arc()).clear();
697 694
      notifier(Edge()).clear();
698 695
      notifier(Node()).clear();
699 696
      Parent::clear();
700 697
    }
701 698

	
702 699
    template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
703 700
    void build(const Graph& graph, NodeRefMap& nodeRef,
704 701
               EdgeRefMap& edgeRef) {
705 702
      Parent::build(graph, nodeRef, edgeRef);
706 703
      notifier(Node()).build();
707 704
      notifier(Edge()).build();
708 705
      notifier(Arc()).build();
709 706
    }
710 707

	
711 708
    void erase(const Node& node) {
712 709
      Arc arc;
713 710
      Parent::firstOut(arc, node);
714 711
      while (arc != INVALID ) {
715 712
        erase(arc);
716 713
        Parent::firstOut(arc, node);
717 714
      }
718 715

	
719 716
      Parent::firstIn(arc, node);
720 717
      while (arc != INVALID ) {
721 718
        erase(arc);
722 719
        Parent::firstIn(arc, node);
723 720
      }
724 721

	
725 722
      notifier(Node()).erase(node);
726 723
      Parent::erase(node);
727 724
    }
728 725

	
729 726
    void erase(const Edge& edge) {
730 727
      std::vector<Arc> av;
731 728
      av.push_back(Parent::direct(edge, true));
732 729
      av.push_back(Parent::direct(edge, false));
733 730
      notifier(Arc()).erase(av);
734 731
      notifier(Edge()).erase(edge);
735 732
      Parent::erase(edge);
736 733
    }
737 734

	
738 735
    GraphExtender() {
739 736
      node_notifier.setContainer(*this);
740 737
      arc_notifier.setContainer(*this);
741 738
      edge_notifier.setContainer(*this);
742 739
    }
743 740

	
744 741
    ~GraphExtender() {
745 742
      edge_notifier.clear();
746 743
      arc_notifier.clear();
747 744
      node_notifier.clear();
748 745
    }
749 746

	
750 747
  };
751 748

	
752 749
}
753 750

	
754 751
#endif
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_MAP_EXTENDER_H
20 20
#define LEMON_BITS_MAP_EXTENDER_H
21 21

	
22 22
#include <iterator>
23 23

	
24 24
#include <lemon/bits/traits.h>
25 25

	
26 26
#include <lemon/concept_check.h>
27 27
#include <lemon/concepts/maps.h>
28 28

	
29 29
//\file
30 30
//\brief Extenders for iterable maps.
31 31

	
32 32
namespace lemon {
33 33

	
34 34
  // \ingroup graphbits
35 35
  //
36 36
  // \brief Extender for maps
37 37
  template <typename _Map>
38 38
  class MapExtender : public _Map {
39
    typedef _Map Parent;
40
    typedef typename Parent::GraphType GraphType;
41

	
39 42
  public:
40 43

	
41
    typedef _Map Parent;
42 44
    typedef MapExtender Map;
43

	
44

	
45
    typedef typename Parent::Graph Graph;
46 45
    typedef typename Parent::Key Item;
47 46

	
48 47
    typedef typename Parent::Key Key;
49 48
    typedef typename Parent::Value Value;
49
    typedef typename Parent::Reference Reference;
50
    typedef typename Parent::ConstReference ConstReference;
51

	
52
    typedef typename Parent::ReferenceMapTag ReferenceMapTag;
50 53

	
51 54
    class MapIt;
52 55
    class ConstMapIt;
53 56

	
54 57
    friend class MapIt;
55 58
    friend class ConstMapIt;
56 59

	
57 60
  public:
58 61

	
59
    MapExtender(const Graph& graph)
62
    MapExtender(const GraphType& graph)
60 63
      : Parent(graph) {}
61 64

	
62
    MapExtender(const Graph& graph, const Value& value)
65
    MapExtender(const GraphType& graph, const Value& value)
63 66
      : Parent(graph, value) {}
64 67

	
65 68
  private:
66 69
    MapExtender& operator=(const MapExtender& cmap) {
67 70
      return operator=<MapExtender>(cmap);
68 71
    }
69 72

	
70 73
    template <typename CMap>
71 74
    MapExtender& operator=(const CMap& cmap) {
72 75
      Parent::operator=(cmap);
73 76
      return *this;
74 77
    }
75 78

	
76 79
  public:
77 80
    class MapIt : public Item {
81
      typedef Item Parent;
82

	
78 83
    public:
79 84

	
80
      typedef Item Parent;
81 85
      typedef typename Map::Value Value;
82 86

	
83 87
      MapIt() {}
84 88

	
85 89
      MapIt(Invalid i) : Parent(i) { }
86 90

	
87 91
      explicit MapIt(Map& _map) : map(_map) {
88 92
        map.notifier()->first(*this);
89 93
      }
90 94

	
91 95
      MapIt(const Map& _map, const Item& item)
92 96
        : Parent(item), map(_map) {}
93 97

	
94 98
      MapIt& operator++() {
95 99
        map.notifier()->next(*this);
96 100
        return *this;
97 101
      }
98 102

	
99 103
      typename MapTraits<Map>::ConstReturnValue operator*() const {
100 104
        return map[*this];
101 105
      }
102 106

	
103 107
      typename MapTraits<Map>::ReturnValue operator*() {
104 108
        return map[*this];
105 109
      }
106 110

	
107 111
      void set(const Value& value) {
108 112
        map.set(*this, value);
109 113
      }
110 114

	
111 115
    protected:
112 116
      Map& map;
113 117

	
114 118
    };
115 119

	
116 120
    class ConstMapIt : public Item {
121
      typedef Item Parent;
122

	
117 123
    public:
118 124

	
119
      typedef Item Parent;
120

	
121 125
      typedef typename Map::Value Value;
122 126

	
123 127
      ConstMapIt() {}
124 128

	
125 129
      ConstMapIt(Invalid i) : Parent(i) { }
126 130

	
127 131
      explicit ConstMapIt(Map& _map) : map(_map) {
128 132
        map.notifier()->first(*this);
129 133
      }
130 134

	
131 135
      ConstMapIt(const Map& _map, const Item& item)
132 136
        : Parent(item), map(_map) {}
133 137

	
134 138
      ConstMapIt& operator++() {
135 139
        map.notifier()->next(*this);
136 140
        return *this;
137 141
      }
138 142

	
139 143
      typename MapTraits<Map>::ConstReturnValue operator*() const {
140 144
        return map[*this];
141 145
      }
142 146

	
143 147
    protected:
144 148
      const Map& map;
145 149
    };
146 150

	
147 151
    class ItemIt : public Item {
152
      typedef Item Parent;
153

	
148 154
    public:
149 155

	
150
      typedef Item Parent;
151

	
152 156
      ItemIt() {}
153 157

	
154 158
      ItemIt(Invalid i) : Parent(i) { }
155 159

	
156 160
      explicit ItemIt(Map& _map) : map(_map) {
157 161
        map.notifier()->first(*this);
158 162
      }
159 163

	
160 164
      ItemIt(const Map& _map, const Item& item)
161 165
        : Parent(item), map(_map) {}
162 166

	
163 167
      ItemIt& operator++() {
164 168
        map.notifier()->next(*this);
165 169
        return *this;
166 170
      }
167 171

	
168 172
    protected:
169 173
      const Map& map;
170 174

	
171 175
    };
172 176
  };
173 177

	
174 178
  // \ingroup graphbits
175 179
  //
176 180
  // \brief Extender for maps which use a subset of the items.
177 181
  template <typename _Graph, typename _Map>
178 182
  class SubMapExtender : public _Map {
183
    typedef _Map Parent;
184
    typedef _Graph GraphType;
185

	
179 186
  public:
180 187

	
181
    typedef _Map Parent;
182 188
    typedef SubMapExtender Map;
183

	
184
    typedef _Graph Graph;
185

	
186 189
    typedef typename Parent::Key Item;
187 190

	
188 191
    typedef typename Parent::Key Key;
189 192
    typedef typename Parent::Value Value;
193
    typedef typename Parent::Reference Reference;
194
    typedef typename Parent::ConstReference ConstReference;
195

	
196
    typedef typename Parent::ReferenceMapTag ReferenceMapTag;
190 197

	
191 198
    class MapIt;
192 199
    class ConstMapIt;
193 200

	
194 201
    friend class MapIt;
195 202
    friend class ConstMapIt;
196 203

	
197 204
  public:
198 205

	
199
    SubMapExtender(const Graph& _graph)
206
    SubMapExtender(const GraphType& _graph)
200 207
      : Parent(_graph), graph(_graph) {}
201 208

	
202
    SubMapExtender(const Graph& _graph, const Value& _value)
209
    SubMapExtender(const GraphType& _graph, const Value& _value)
203 210
      : Parent(_graph, _value), graph(_graph) {}
204 211

	
205 212
  private:
206 213
    SubMapExtender& operator=(const SubMapExtender& cmap) {
207 214
      return operator=<MapExtender>(cmap);
208 215
    }
209 216

	
210 217
    template <typename CMap>
211 218
    SubMapExtender& operator=(const CMap& cmap) {
212 219
      checkConcept<concepts::ReadMap<Key, Value>, CMap>();
213 220
      Item it;
214 221
      for (graph.first(it); it != INVALID; graph.next(it)) {
215 222
        Parent::set(it, cmap[it]);
216 223
      }
217 224
      return *this;
218 225
    }
219 226

	
220 227
  public:
221 228
    class MapIt : public Item {
229
      typedef Item Parent;
230

	
222 231
    public:
223

	
224
      typedef Item Parent;
225 232
      typedef typename Map::Value Value;
226 233

	
227 234
      MapIt() {}
228 235

	
229 236
      MapIt(Invalid i) : Parent(i) { }
230 237

	
231 238
      explicit MapIt(Map& _map) : map(_map) {
232 239
        map.graph.first(*this);
233 240
      }
234 241

	
235 242
      MapIt(const Map& _map, const Item& item)
236 243
        : Parent(item), map(_map) {}
237 244

	
238 245
      MapIt& operator++() {
239 246
        map.graph.next(*this);
240 247
        return *this;
241 248
      }
242 249

	
243 250
      typename MapTraits<Map>::ConstReturnValue operator*() const {
244 251
        return map[*this];
245 252
      }
246 253

	
247 254
      typename MapTraits<Map>::ReturnValue operator*() {
248 255
        return map[*this];
249 256
      }
250 257

	
251 258
      void set(const Value& value) {
252 259
        map.set(*this, value);
253 260
      }
254 261

	
255 262
    protected:
256 263
      Map& map;
257 264

	
258 265
    };
259 266

	
260 267
    class ConstMapIt : public Item {
268
      typedef Item Parent;
269

	
261 270
    public:
262 271

	
263
      typedef Item Parent;
264

	
265 272
      typedef typename Map::Value Value;
266 273

	
267 274
      ConstMapIt() {}
268 275

	
269 276
      ConstMapIt(Invalid i) : Parent(i) { }
270 277

	
271 278
      explicit ConstMapIt(Map& _map) : map(_map) {
272 279
        map.graph.first(*this);
273 280
      }
274 281

	
275 282
      ConstMapIt(const Map& _map, const Item& item)
276 283
        : Parent(item), map(_map) {}
277 284

	
278 285
      ConstMapIt& operator++() {
279 286
        map.graph.next(*this);
280 287
        return *this;
281 288
      }
282 289

	
283 290
      typename MapTraits<Map>::ConstReturnValue operator*() const {
284 291
        return map[*this];
285 292
      }
286 293

	
287 294
    protected:
288 295
      const Map& map;
289 296
    };
290 297

	
291 298
    class ItemIt : public Item {
299
      typedef Item Parent;
300

	
292 301
    public:
293 302

	
294
      typedef Item Parent;
295

	
296 303
      ItemIt() {}
297 304

	
298 305
      ItemIt(Invalid i) : Parent(i) { }
299 306

	
300 307
      explicit ItemIt(Map& _map) : map(_map) {
301 308
        map.graph.first(*this);
302 309
      }
303 310

	
304 311
      ItemIt(const Map& _map, const Item& item)
305 312
        : Parent(item), map(_map) {}
306 313

	
307 314
      ItemIt& operator++() {
308 315
        map.graph.next(*this);
309 316
        return *this;
310 317
      }
311 318

	
312 319
    protected:
313 320
      const Map& map;
314 321

	
315 322
    };
316 323

	
317 324
  private:
318 325

	
319
    const Graph& graph;
326
    const GraphType& graph;
320 327

	
321 328
  };
322 329

	
323 330
}
324 331

	
325 332
#endif
Ignore white space 6 line context
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-2009
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
#ifndef LEMON_BITS_PRED_MAP_PATH_H
20
#define LEMON_BITS_PRED_MAP_PATH_H
19
#ifndef LEMON_BITS_PATH_DUMP_H
20
#define LEMON_BITS_PATH_DUMP_H
21

	
22
#include <lemon/core.h>
23
#include <lemon/concept_check.h>
21 24

	
22 25
namespace lemon {
23 26

	
24 27
  template <typename _Digraph, typename _PredMap>
25 28
  class PredMapPath {
26 29
  public:
27 30
    typedef True RevPathTag;
28 31

	
29 32
    typedef _Digraph Digraph;
30 33
    typedef typename Digraph::Arc Arc;
31 34
    typedef _PredMap PredMap;
32 35

	
33 36
    PredMapPath(const Digraph& _digraph, const PredMap& _predMap,
34 37
                typename Digraph::Node _target)
35 38
      : digraph(_digraph), predMap(_predMap), target(_target) {}
36 39

	
37 40
    int length() const {
38 41
      int len = 0;
39 42
      typename Digraph::Node node = target;
40 43
      typename Digraph::Arc arc;
41 44
      while ((arc = predMap[node]) != INVALID) {
42 45
        node = digraph.source(arc);
43 46
        ++len;
44 47
      }
45 48
      return len;
46 49
    }
47 50

	
48 51
    bool empty() const {
49 52
      return predMap[target] != INVALID;
50 53
    }
51 54

	
52 55
    class RevArcIt {
53 56
    public:
54 57
      RevArcIt() {}
55 58
      RevArcIt(Invalid) : path(0), current(INVALID) {}
56 59
      RevArcIt(const PredMapPath& _path)
57 60
        : path(&_path), current(_path.target) {
58 61
        if (path->predMap[current] == INVALID) current = INVALID;
59 62
      }
60 63

	
61 64
      operator const typename Digraph::Arc() const {
62 65
        return path->predMap[current];
63 66
      }
64 67

	
65 68
      RevArcIt& operator++() {
66 69
        current = path->digraph.source(path->predMap[current]);
67 70
        if (path->predMap[current] == INVALID) current = INVALID;
68 71
        return *this;
69 72
      }
70 73

	
71 74
      bool operator==(const RevArcIt& e) const {
72 75
        return current == e.current;
73 76
      }
74 77

	
75 78
      bool operator!=(const RevArcIt& e) const {
76 79
        return current != e.current;
77 80
      }
78 81

	
79 82
      bool operator<(const RevArcIt& e) const {
80 83
        return current < e.current;
81 84
      }
82 85

	
83 86
    private:
84 87
      const PredMapPath* path;
85 88
      typename Digraph::Node current;
86 89
    };
87 90

	
88 91
  private:
89 92
    const Digraph& digraph;
90 93
    const PredMap& predMap;
91 94
    typename Digraph::Node target;
92 95
  };
93 96

	
94 97

	
95 98
  template <typename _Digraph, typename _PredMatrixMap>
96 99
  class PredMatrixMapPath {
97 100
  public:
98 101
    typedef True RevPathTag;
99 102

	
100 103
    typedef _Digraph Digraph;
101 104
    typedef typename Digraph::Arc Arc;
102 105
    typedef _PredMatrixMap PredMatrixMap;
103 106

	
104 107
    PredMatrixMapPath(const Digraph& _digraph,
105 108
                      const PredMatrixMap& _predMatrixMap,
106 109
                      typename Digraph::Node _source,
107 110
                      typename Digraph::Node _target)
108 111
      : digraph(_digraph), predMatrixMap(_predMatrixMap),
109 112
        source(_source), target(_target) {}
110 113

	
111 114
    int length() const {
112 115
      int len = 0;
113 116
      typename Digraph::Node node = target;
114 117
      typename Digraph::Arc arc;
115 118
      while ((arc = predMatrixMap(source, node)) != INVALID) {
116 119
        node = digraph.source(arc);
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_SOLVER_BITS_H
20 20
#define LEMON_BITS_SOLVER_BITS_H
21 21

	
22
#include <vector>
23

	
22 24
namespace lemon {
23 25

	
24 26
  namespace _solver_bits {
25 27

	
26 28
    class VarIndex {
27 29
    private:
28 30
      struct ItemT {
29 31
        int prev, next;
30 32
        int index;
31 33
      };
32 34
      std::vector<ItemT> items;
33 35
      int first_item, last_item, first_free_item;
34 36

	
35 37
      std::vector<int> cross;
36 38

	
37 39
    public:
38 40

	
39 41
      VarIndex()
40 42
        : first_item(-1), last_item(-1), first_free_item(-1) {
41 43
      }
42 44

	
43 45
      void clear() {
44 46
        first_item = -1;
45 47
        first_free_item = -1;
46 48
        items.clear();
47 49
        cross.clear();
48 50
      }
49 51

	
50 52
      int addIndex(int idx) {
51 53
        int n;
52 54
        if (first_free_item == -1) {
53 55
          n = items.size();
54 56
          items.push_back(ItemT());
55 57
        } else {
56 58
          n = first_free_item;
57 59
          first_free_item = items[n].next;
58 60
          if (first_free_item != -1) {
59 61
            items[first_free_item].prev = -1;
60 62
          }
61 63
        }
62 64
        items[n].index = idx;
63 65
        if (static_cast<int>(cross.size()) <= idx) {
64 66
          cross.resize(idx + 1, -1);
65 67
        }
66 68
        cross[idx] = n;
67 69

	
68 70
        items[n].prev = last_item;
69 71
        items[n].next = -1;
70 72
        if (last_item != -1) {
71 73
          items[last_item].next = n;
72 74
        } else {
73 75
          first_item = n;
74 76
        }
75 77
        last_item = n;
76 78

	
77 79
        return n;
78 80
      }
79 81

	
80 82
      int addIndex(int idx, int n) {
81 83
        while (n >= static_cast<int>(items.size())) {
82 84
          items.push_back(ItemT());
83 85
          items.back().prev = -1;
84 86
          items.back().next = first_free_item;
85 87
          if (first_free_item != -1) {
86 88
            items[first_free_item].prev = items.size() - 1;
87 89
          }
88 90
          first_free_item = items.size() - 1;
89 91
        }
90 92
        if (items[n].next != -1) {
91 93
          items[items[n].next].prev = items[n].prev;
92 94
        }
93 95
        if (items[n].prev != -1) {
94 96
          items[items[n].prev].next = items[n].next;
95 97
        } else {
96 98
          first_free_item = items[n].next;
97 99
        }
98 100

	
99 101
        items[n].index = idx;
100 102
        if (static_cast<int>(cross.size()) <= idx) {
101 103
          cross.resize(idx + 1, -1);
102 104
        }
103 105
        cross[idx] = n;
104 106

	
105 107
        items[n].prev = last_item;
106 108
        items[n].next = -1;
107 109
        if (last_item != -1) {
108 110
          items[last_item].next = n;
109 111
        } else {
110 112
          first_item = n;
111 113
        }
112 114
        last_item = n;
113 115

	
114 116
        return n;
115 117
      }
116 118

	
117 119
      void eraseIndex(int idx) {
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_TRAITS_H
20 20
#define LEMON_BITS_TRAITS_H
21 21

	
22 22
//\file
23 23
//\brief Traits for graphs and maps
24 24
//
25 25

	
26 26
#include <lemon/bits/enable_if.h>
27 27

	
28 28
namespace lemon {
29 29

	
30 30
  struct InvalidType {};
31 31

	
32
  template <typename _Graph, typename _Item>
32
  template <typename GR, typename _Item>
33 33
  class ItemSetTraits {};
34 34

	
35 35

	
36
  template <typename Graph, typename Enable = void>
36
  template <typename GR, typename Enable = void>
37 37
  struct NodeNotifierIndicator {
38 38
    typedef InvalidType Type;
39 39
  };
40
  template <typename Graph>
40
  template <typename GR>
41 41
  struct NodeNotifierIndicator<
42
    Graph,
43
    typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type
42
    GR,
43
    typename enable_if<typename GR::NodeNotifier::Notifier, void>::type
44 44
  > {
45
    typedef typename Graph::NodeNotifier Type;
45
    typedef typename GR::NodeNotifier Type;
46 46
  };
47 47

	
48
  template <typename _Graph>
49
  class ItemSetTraits<_Graph, typename _Graph::Node> {
48
  template <typename GR>
49
  class ItemSetTraits<GR, typename GR::Node> {
50 50
  public:
51 51

	
52
    typedef _Graph Graph;
52
    typedef GR Graph;
53
    typedef GR Digraph;
53 54

	
54
    typedef typename Graph::Node Item;
55
    typedef typename Graph::NodeIt ItemIt;
55
    typedef typename GR::Node Item;
56
    typedef typename GR::NodeIt ItemIt;
56 57

	
57
    typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier;
58
    typedef typename NodeNotifierIndicator<GR>::Type ItemNotifier;
58 59

	
59
    template <typename _Value>
60
    class Map : public Graph::template NodeMap<_Value> {
60
    template <typename V>
61
    class Map : public GR::template NodeMap<V> {
62
      typedef typename GR::template NodeMap<V> Parent;
63

	
61 64
    public:
62
      typedef typename Graph::template NodeMap<_Value> Parent;
63
      typedef typename Graph::template NodeMap<_Value> Type;
65
      typedef typename GR::template NodeMap<V> Type;
64 66
      typedef typename Parent::Value Value;
65 67

	
66
      Map(const Graph& _digraph) : Parent(_digraph) {}
67
      Map(const Graph& _digraph, const Value& _value)
68
      Map(const GR& _digraph) : Parent(_digraph) {}
69
      Map(const GR& _digraph, const Value& _value)
68 70
        : Parent(_digraph, _value) {}
69 71

	
70 72
     };
71 73

	
72 74
  };
73 75

	
74
  template <typename Graph, typename Enable = void>
76
  template <typename GR, typename Enable = void>
75 77
  struct ArcNotifierIndicator {
76 78
    typedef InvalidType Type;
77 79
  };
78
  template <typename Graph>
80
  template <typename GR>
79 81
  struct ArcNotifierIndicator<
80
    Graph,
81
    typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type
82
    GR,
83
    typename enable_if<typename GR::ArcNotifier::Notifier, void>::type
82 84
  > {
83
    typedef typename Graph::ArcNotifier Type;
85
    typedef typename GR::ArcNotifier Type;
84 86
  };
85 87

	
86
  template <typename _Graph>
87
  class ItemSetTraits<_Graph, typename _Graph::Arc> {
88
  template <typename GR>
89
  class ItemSetTraits<GR, typename GR::Arc> {
88 90
  public:
89 91

	
90
    typedef _Graph Graph;
92
    typedef GR Graph;
93
    typedef GR Digraph;
91 94

	
92
    typedef typename Graph::Arc Item;
93
    typedef typename Graph::ArcIt ItemIt;
95
    typedef typename GR::Arc Item;
96
    typedef typename GR::ArcIt ItemIt;
94 97

	
95
    typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier;
98
    typedef typename ArcNotifierIndicator<GR>::Type ItemNotifier;
96 99

	
97
    template <typename _Value>
98
    class Map : public Graph::template ArcMap<_Value> {
100
    template <typename V>
101
    class Map : public GR::template ArcMap<V> {
102
      typedef typename GR::template ArcMap<V> Parent;
103

	
99 104
    public:
100
      typedef typename Graph::template ArcMap<_Value> Parent;
101
      typedef typename Graph::template ArcMap<_Value> Type;
105
      typedef typename GR::template ArcMap<V> Type;
102 106
      typedef typename Parent::Value Value;
103 107

	
104
      Map(const Graph& _digraph) : Parent(_digraph) {}
105
      Map(const Graph& _digraph, const Value& _value)
108
      Map(const GR& _digraph) : Parent(_digraph) {}
109
      Map(const GR& _digraph, const Value& _value)
106 110
        : Parent(_digraph, _value) {}
107 111
    };
108 112

	
109 113
  };
110 114

	
111
  template <typename Graph, typename Enable = void>
115
  template <typename GR, typename Enable = void>
112 116
  struct EdgeNotifierIndicator {
113 117
    typedef InvalidType Type;
114 118
  };
115
  template <typename Graph>
119
  template <typename GR>
116 120
  struct EdgeNotifierIndicator<
117
    Graph,
118
    typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type
121
    GR,
122
    typename enable_if<typename GR::EdgeNotifier::Notifier, void>::type
119 123
  > {
120
    typedef typename Graph::EdgeNotifier Type;
124
    typedef typename GR::EdgeNotifier Type;
121 125
  };
122 126

	
123
  template <typename _Graph>
124
  class ItemSetTraits<_Graph, typename _Graph::Edge> {
127
  template <typename GR>
128
  class ItemSetTraits<GR, typename GR::Edge> {
125 129
  public:
126 130

	
127
    typedef _Graph Graph;
131
    typedef GR Graph;
132
    typedef GR Digraph;
128 133

	
129
    typedef typename Graph::Edge Item;
130
    typedef typename Graph::EdgeIt ItemIt;
134
    typedef typename GR::Edge Item;
135
    typedef typename GR::EdgeIt ItemIt;
131 136

	
132
    typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier;
137
    typedef typename EdgeNotifierIndicator<GR>::Type ItemNotifier;
133 138

	
134
    template <typename _Value>
135
    class Map : public Graph::template EdgeMap<_Value> {
139
    template <typename V>
140
    class Map : public GR::template EdgeMap<V> {
141
      typedef typename GR::template EdgeMap<V> Parent;
142

	
136 143
    public:
137
      typedef typename Graph::template EdgeMap<_Value> Parent;
138
      typedef typename Graph::template EdgeMap<_Value> Type;
144
      typedef typename GR::template EdgeMap<V> Type;
139 145
      typedef typename Parent::Value Value;
140 146

	
141
      Map(const Graph& _digraph) : Parent(_digraph) {}
142
      Map(const Graph& _digraph, const Value& _value)
147
      Map(const GR& _digraph) : Parent(_digraph) {}
148
      Map(const GR& _digraph, const Value& _value)
143 149
        : Parent(_digraph, _value) {}
144 150
    };
145 151

	
146 152
  };
147 153

	
148 154
  template <typename Map, typename Enable = void>
149 155
  struct MapTraits {
150 156
    typedef False ReferenceMapTag;
151 157

	
152 158
    typedef typename Map::Key Key;
153 159
    typedef typename Map::Value Value;
154 160

	
155 161
    typedef Value ConstReturnValue;
156 162
    typedef Value ReturnValue;
157 163
  };
158 164

	
159 165
  template <typename Map>
160 166
  struct MapTraits<
161 167
    Map, typename enable_if<typename Map::ReferenceMapTag, void>::type >
162 168
  {
163 169
    typedef True ReferenceMapTag;
164 170

	
165 171
    typedef typename Map::Key Key;
166 172
    typedef typename Map::Value Value;
167 173

	
168 174
    typedef typename Map::ConstReference ConstReturnValue;
169 175
    typedef typename Map::Reference ReturnValue;
170 176

	
171 177
    typedef typename Map::ConstReference ConstReference;
172 178
    typedef typename Map::Reference Reference;
173 179
 };
174 180

	
175 181
  template <typename MatrixMap, typename Enable = void>
176 182
  struct MatrixMapTraits {
177 183
    typedef False ReferenceMapTag;
178 184

	
179 185
    typedef typename MatrixMap::FirstKey FirstKey;
180 186
    typedef typename MatrixMap::SecondKey SecondKey;
181 187
    typedef typename MatrixMap::Value Value;
182 188

	
183 189
    typedef Value ConstReturnValue;
184 190
    typedef Value ReturnValue;
185 191
  };
186 192

	
187 193
  template <typename MatrixMap>
188 194
  struct MatrixMapTraits<
189 195
    MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag,
190 196
                                  void>::type >
191 197
  {
192 198
    typedef True ReferenceMapTag;
193 199

	
194 200
    typedef typename MatrixMap::FirstKey FirstKey;
195 201
    typedef typename MatrixMap::SecondKey SecondKey;
196 202
    typedef typename MatrixMap::Value Value;
197 203

	
198 204
    typedef typename MatrixMap::ConstReference ConstReturnValue;
199 205
    typedef typename MatrixMap::Reference ReturnValue;
200 206

	
201 207
    typedef typename MatrixMap::ConstReference ConstReference;
202 208
    typedef typename MatrixMap::Reference Reference;
203 209
 };
204 210

	
205 211
  // Indicators for the tags
206 212

	
207
  template <typename Graph, typename Enable = void>
213
  template <typename GR, typename Enable = void>
208 214
  struct NodeNumTagIndicator {
209 215
    static const bool value = false;
210 216
  };
211 217

	
212
  template <typename Graph>
218
  template <typename GR>
213 219
  struct NodeNumTagIndicator<
214
    Graph,
215
    typename enable_if<typename Graph::NodeNumTag, void>::type
220
    GR,
221
    typename enable_if<typename GR::NodeNumTag, void>::type
216 222
  > {
217 223
    static const bool value = true;
218 224
  };
219 225

	
220
  template <typename Graph, typename Enable = void>
226
  template <typename GR, typename Enable = void>
221 227
  struct ArcNumTagIndicator {
222 228
    static const bool value = false;
223 229
  };
224 230

	
225
  template <typename Graph>
231
  template <typename GR>
226 232
  struct ArcNumTagIndicator<
227
    Graph,
228
    typename enable_if<typename Graph::ArcNumTag, void>::type
233
    GR,
234
    typename enable_if<typename GR::ArcNumTag, void>::type
229 235
  > {
230 236
    static const bool value = true;
231 237
  };
232 238

	
233
  template <typename Graph, typename Enable = void>
239
  template <typename GR, typename Enable = void>
234 240
  struct EdgeNumTagIndicator {
235 241
    static const bool value = false;
236 242
  };
237 243

	
238
  template <typename Graph>
244
  template <typename GR>
239 245
  struct EdgeNumTagIndicator<
240
    Graph,
241
    typename enable_if<typename Graph::EdgeNumTag, void>::type
246
    GR,
247
    typename enable_if<typename GR::EdgeNumTag, void>::type
242 248
  > {
243 249
    static const bool value = true;
244 250
  };
245 251

	
246
  template <typename Graph, typename Enable = void>
252
  template <typename GR, typename Enable = void>
247 253
  struct FindArcTagIndicator {
248 254
    static const bool value = false;
249 255
  };
250 256

	
251
  template <typename Graph>
257
  template <typename GR>
252 258
  struct FindArcTagIndicator<
253
    Graph,
254
    typename enable_if<typename Graph::FindArcTag, void>::type
259
    GR,
260
    typename enable_if<typename GR::FindArcTag, void>::type
255 261
  > {
256 262
    static const bool value = true;
257 263
  };
258 264

	
259
  template <typename Graph, typename Enable = void>
265
  template <typename GR, typename Enable = void>
260 266
  struct FindEdgeTagIndicator {
261 267
    static const bool value = false;
262 268
  };
263 269

	
264
  template <typename Graph>
270
  template <typename GR>
265 271
  struct FindEdgeTagIndicator<
266
    Graph,
267
    typename enable_if<typename Graph::FindEdgeTag, void>::type
272
    GR,
273
    typename enable_if<typename GR::FindEdgeTag, void>::type
268 274
  > {
269 275
    static const bool value = true;
270 276
  };
271 277

	
272
  template <typename Graph, typename Enable = void>
278
  template <typename GR, typename Enable = void>
273 279
  struct UndirectedTagIndicator {
274 280
    static const bool value = false;
275 281
  };
276 282

	
277
  template <typename Graph>
283
  template <typename GR>
278 284
  struct UndirectedTagIndicator<
279
    Graph,
280
    typename enable_if<typename Graph::UndirectedTag, void>::type
285
    GR,
286
    typename enable_if<typename GR::UndirectedTag, void>::type
281 287
  > {
282 288
    static const bool value = true;
283 289
  };
284 290

	
285
  template <typename Graph, typename Enable = void>
291
  template <typename GR, typename Enable = void>
286 292
  struct BuildTagIndicator {
287 293
    static const bool value = false;
288 294
  };
289 295

	
290
  template <typename Graph>
296
  template <typename GR>
291 297
  struct BuildTagIndicator<
292
    Graph,
293
    typename enable_if<typename Graph::BuildTag, void>::type
298
    GR,
299
    typename enable_if<typename GR::BuildTag, void>::type
294 300
  > {
295 301
    static const bool value = true;
296 302
  };
297 303

	
298 304
}
299 305

	
300 306
#endif
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BITS_VECTOR_MAP_H
20 20
#define LEMON_BITS_VECTOR_MAP_H
21 21

	
22 22
#include <vector>
23 23
#include <algorithm>
24 24

	
25 25
#include <lemon/core.h>
26 26
#include <lemon/bits/alteration_notifier.h>
27 27

	
28 28
#include <lemon/concept_check.h>
29 29
#include <lemon/concepts/maps.h>
30 30

	
31 31
//\ingroup graphbits
32 32
//
33 33
//\file
34 34
//\brief Vector based graph maps.
35 35
namespace lemon {
36 36

	
37 37
  // \ingroup graphbits
38 38
  //
39 39
  // \brief Graph map based on the std::vector storage.
40 40
  //
41 41
  // The VectorMap template class is graph map structure that automatically
42 42
  // updates the map when a key is added to or erased from the graph.
43 43
  // This map type uses std::vector to store the values.
44 44
  //
45 45
  // \tparam _Graph The graph this map is attached to.
46 46
  // \tparam _Item The item type of the graph items.
47 47
  // \tparam _Value The value type of the map.
48 48
  template <typename _Graph, typename _Item, typename _Value>
49 49
  class VectorMap
50 50
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
51 51
  private:
52 52

	
53 53
    // The container type of the map.
54 54
    typedef std::vector<_Value> Container;
55 55

	
56 56
  public:
57 57

	
58 58
    // The graph type of the map.
59
    typedef _Graph Graph;
59
    typedef _Graph GraphType;
60 60
    // The item type of the map.
61 61
    typedef _Item Item;
62 62
    // The reference map tag.
63 63
    typedef True ReferenceMapTag;
64 64

	
65 65
    // The key type of the map.
66 66
    typedef _Item Key;
67 67
    // The value type of the map.
68 68
    typedef _Value Value;
69 69

	
70 70
    // The notifier type.
71 71
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
72 72

	
73 73
    // The map type.
74 74
    typedef VectorMap Map;
75
    // The base class of the map.
76
    typedef typename Notifier::ObserverBase Parent;
77 75

	
78 76
    // The reference type of the map;
79 77
    typedef typename Container::reference Reference;
80 78
    // The const reference type of the map;
81 79
    typedef typename Container::const_reference ConstReference;
82 80

	
81
  private:
82

	
83
    // The base class of the map.
84
    typedef typename Notifier::ObserverBase Parent;
85

	
86
  public:
83 87

	
84 88
    // \brief Constructor to attach the new map into the notifier.
85 89
    //
86 90
    // It constructs a map and attachs it into the notifier.
87 91
    // It adds all the items of the graph to the map.
88
    VectorMap(const Graph& graph) {
92
    VectorMap(const GraphType& graph) {
89 93
      Parent::attach(graph.notifier(Item()));
90 94
      container.resize(Parent::notifier()->maxId() + 1);
91 95
    }
92 96

	
93 97
    // \brief Constructor uses given value to initialize the map.
94 98
    //
95 99
    // It constructs a map uses a given value to initialize the map.
96 100
    // It adds all the items of the graph to the map.
97
    VectorMap(const Graph& graph, const Value& value) {
101
    VectorMap(const GraphType& graph, const Value& value) {
98 102
      Parent::attach(graph.notifier(Item()));
99 103
      container.resize(Parent::notifier()->maxId() + 1, value);
100 104
    }
101 105

	
102 106
  private:
103 107
    // \brief Copy constructor
104 108
    //
105 109
    // Copy constructor.
106 110
    VectorMap(const VectorMap& _copy) : Parent() {
107 111
      if (_copy.attached()) {
108 112
        Parent::attach(*_copy.notifier());
109 113
        container = _copy.container;
110 114
      }
111 115
    }
112 116

	
113 117
    // \brief Assign operator.
114 118
    //
115 119
    // This operator assigns for each item in the map the
116 120
    // value mapped to the same item in the copied map.
117 121
    // The parameter map should be indiced with the same
118 122
    // itemset because this assign operator does not change
119 123
    // the container of the map.
120 124
    VectorMap& operator=(const VectorMap& cmap) {
121 125
      return operator=<VectorMap>(cmap);
122 126
    }
123 127

	
124 128

	
125 129
    // \brief Template assign operator.
126 130
    //
127
    // The given parameter should be conform to the ReadMap
131
    // The given parameter should conform to the ReadMap
128 132
    // concecpt and could be indiced by the current item set of
129 133
    // the NodeMap. In this case the value for each item
130 134
    // is assigned by the value of the given ReadMap.
131 135
    template <typename CMap>
132 136
    VectorMap& operator=(const CMap& cmap) {
133 137
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
134 138
      const typename Parent::Notifier* nf = Parent::notifier();
135 139
      Item it;
136 140
      for (nf->first(it); it != INVALID; nf->next(it)) {
137 141
        set(it, cmap[it]);
138 142
      }
139 143
      return *this;
140 144
    }
141 145

	
142 146
  public:
143 147

	
144 148
    // \brief The subcript operator.
145 149
    //
146 150
    // The subscript operator. The map can be subscripted by the
147 151
    // actual items of the graph.
148 152
    Reference operator[](const Key& key) {
149 153
      return container[Parent::notifier()->id(key)];
150 154
    }
151 155

	
152 156
    // \brief The const subcript operator.
153 157
    //
154 158
    // The const subscript operator. The map can be subscripted by the
155 159
    // actual items of the graph.
156 160
    ConstReference operator[](const Key& key) const {
157 161
      return container[Parent::notifier()->id(key)];
158 162
    }
159 163

	
160 164

	
161 165
    // \brief The setter function of the map.
162 166
    //
163 167
    // It the same as operator[](key) = value expression.
164 168
    void set(const Key& key, const Value& value) {
165 169
      (*this)[key] = value;
166 170
    }
167 171

	
168 172
  protected:
169 173

	
170 174
    // \brief Adds a new key to the map.
171 175
    //
172 176
    // It adds a new key to the map. It is called by the observer notifier
173 177
    // and it overrides the add() member function of the observer base.
174 178
    virtual void add(const Key& key) {
175 179
      int id = Parent::notifier()->id(key);
176 180
      if (id >= int(container.size())) {
177 181
        container.resize(id + 1);
178 182
      }
179 183
    }
180 184

	
181 185
    // \brief Adds more new keys to the map.
182 186
    //
183 187
    // It adds more new keys to the map. It is called by the observer notifier
184 188
    // and it overrides the add() member function of the observer base.
185 189
    virtual void add(const std::vector<Key>& keys) {
186 190
      int max = container.size() - 1;
187 191
      for (int i = 0; i < int(keys.size()); ++i) {
188 192
        int id = Parent::notifier()->id(keys[i]);
189 193
        if (id >= max) {
190 194
          max = id;
191 195
        }
192 196
      }
193 197
      container.resize(max + 1);
194 198
    }
195 199

	
196 200
    // \brief Erase a key from the map.
197 201
    //
198 202
    // Erase a key from the map. It is called by the observer notifier
199 203
    // and it overrides the erase() member function of the observer base.
200 204
    virtual void erase(const Key& key) {
201 205
      container[Parent::notifier()->id(key)] = Value();
202 206
    }
203 207

	
204 208
    // \brief Erase more keys from the map.
205 209
    //
206 210
    // It erases more keys from the map. It is called by the observer notifier
207 211
    // and it overrides the erase() member function of the observer base.
208 212
    virtual void erase(const std::vector<Key>& keys) {
209 213
      for (int i = 0; i < int(keys.size()); ++i) {
210 214
        container[Parent::notifier()->id(keys[i])] = Value();
211 215
      }
212 216
    }
213 217

	
214 218
    // \brief Build the map.
215 219
    //
216 220
    // It builds the map. It is called by the observer notifier
217 221
    // and it overrides the build() member function of the observer base.
218 222
    virtual void build() {
219 223
      int size = Parent::notifier()->maxId() + 1;
220 224
      container.reserve(size);
221 225
      container.resize(size);
222 226
    }
223 227

	
Ignore white space 6 line context
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-2009
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_CIRCULATION_H
20 20
#define LEMON_CIRCULATION_H
21 21

	
22 22
#include <lemon/tolerance.h>
23 23
#include <lemon/elevator.h>
24
#include <limits>
24 25

	
25 26
///\ingroup max_flow
26 27
///\file
27 28
///\brief Push-relabel algorithm for finding a feasible circulation.
28 29
///
29 30
namespace lemon {
30 31

	
31 32
  /// \brief Default traits class of Circulation class.
32 33
  ///
33 34
  /// Default traits class of Circulation class.
34
  /// \tparam _Diraph Digraph type.
35
  /// \tparam _LCapMap Lower bound capacity map type.
36
  /// \tparam _UCapMap Upper bound capacity map type.
37
  /// \tparam _DeltaMap Delta map type.
38
  template <typename _Diraph, typename _LCapMap,
39
            typename _UCapMap, typename _DeltaMap>
35
  ///
36
  /// \tparam GR Type of the digraph the algorithm runs on.
37
  /// \tparam LM The type of the lower bound map.
38
  /// \tparam UM The type of the upper bound (capacity) map.
39
  /// \tparam SM The type of the supply map.
40
  template <typename GR, typename LM,
41
            typename UM, typename SM>
40 42
  struct CirculationDefaultTraits {
41 43

	
42 44
    /// \brief The type of the digraph the algorithm runs on.
43
    typedef _Diraph Digraph;
45
    typedef GR Digraph;
44 46

	
45
    /// \brief The type of the map that stores the circulation lower
46
    /// bound.
47
    /// \brief The type of the lower bound map.
47 48
    ///
48
    /// The type of the map that stores the circulation lower bound.
49
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
50
    typedef _LCapMap LCapMap;
49
    /// The type of the map that stores the lower bounds on the arcs.
50
    /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
51
    typedef LM LowerMap;
51 52

	
52
    /// \brief The type of the map that stores the circulation upper
53
    /// bound.
53
    /// \brief The type of the upper bound (capacity) map.
54 54
    ///
55
    /// The type of the map that stores the circulation upper bound.
56
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
57
    typedef _UCapMap UCapMap;
55
    /// The type of the map that stores the upper bounds (capacities)
56
    /// on the arcs.
57
    /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
58
    typedef UM UpperMap;
58 59

	
59
    /// \brief The type of the map that stores the lower bound for
60
    /// the supply of the nodes.
60
    /// \brief The type of supply map.
61 61
    ///
62
    /// The type of the map that stores the lower bound for the supply
63
    /// of the nodes. It must meet the \ref concepts::ReadMap "ReadMap"
64
    /// concept.
65
    typedef _DeltaMap DeltaMap;
62
    /// The type of the map that stores the signed supply values of the 
63
    /// nodes. 
64
    /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
65
    typedef SM SupplyMap;
66 66

	
67
    /// \brief The type of the flow values.
68
    typedef typename DeltaMap::Value Value;
67
    /// \brief The type of the flow and supply values.
68
    typedef typename SupplyMap::Value Value;
69 69

	
70 70
    /// \brief The type of the map that stores the flow values.
71 71
    ///
72 72
    /// The type of the map that stores the flow values.
73
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
73
    /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap"
74
    /// concept.
75
#ifdef DOXYGEN
76
    typedef GR::ArcMap<Value> FlowMap;
77
#else
74 78
    typedef typename Digraph::template ArcMap<Value> FlowMap;
79
#endif
75 80

	
76 81
    /// \brief Instantiates a FlowMap.
77 82
    ///
78 83
    /// This function instantiates a \ref FlowMap.
79
    /// \param digraph The digraph, to which we would like to define
84
    /// \param digraph The digraph for which we would like to define
80 85
    /// the flow map.
81 86
    static FlowMap* createFlowMap(const Digraph& digraph) {
82 87
      return new FlowMap(digraph);
83 88
    }
84 89

	
85 90
    /// \brief The elevator type used by the algorithm.
86 91
    ///
87 92
    /// The elevator type used by the algorithm.
88 93
    ///
89
    /// \sa Elevator
90
    /// \sa LinkedElevator
94
    /// \sa Elevator, LinkedElevator
95
#ifdef DOXYGEN
96
    typedef lemon::Elevator<GR, GR::Node> Elevator;
97
#else
91 98
    typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator;
99
#endif
92 100

	
93 101
    /// \brief Instantiates an Elevator.
94 102
    ///
95 103
    /// This function instantiates an \ref Elevator.
96
    /// \param digraph The digraph, to which we would like to define
104
    /// \param digraph The digraph for which we would like to define
97 105
    /// the elevator.
98 106
    /// \param max_level The maximum level of the elevator.
99 107
    static Elevator* createElevator(const Digraph& digraph, int max_level) {
100 108
      return new Elevator(digraph, max_level);
101 109
    }
102 110

	
103 111
    /// \brief The tolerance used by the algorithm
104 112
    ///
105 113
    /// The tolerance used by the algorithm to handle inexact computation.
106 114
    typedef lemon::Tolerance<Value> Tolerance;
107 115

	
108 116
  };
109 117

	
110 118
  /**
111 119
     \brief Push-relabel algorithm for the network circulation problem.
112 120

	
113 121
     \ingroup max_flow
114
     This class implements a push-relabel algorithm for the network
115
     circulation problem.
122
     This class implements a push-relabel algorithm for the \e network
123
     \e circulation problem.
116 124
     It is to find a feasible circulation when lower and upper bounds
117
     are given for the flow values on the arcs and lower bounds
118
     are given for the supply values of the nodes.
125
     are given for the flow values on the arcs and lower bounds are
126
     given for the difference between the outgoing and incoming flow
127
     at the nodes.
119 128

	
120 129
     The exact formulation of this problem is the following.
121
     Let \f$G=(V,A)\f$ be a digraph,
122
     \f$lower, upper: A\rightarrow\mathbf{R}^+_0\f$,
123
     \f$delta: V\rightarrow\mathbf{R}\f$. Find a feasible circulation
124
     \f$f: A\rightarrow\mathbf{R}^+_0\f$ so that
125
     \f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a)
126
     \geq delta(v) \quad \forall v\in V, \f]
127
     \f[ lower(a)\leq f(a) \leq upper(a) \quad \forall a\in A. \f]
128
     \note \f$delta(v)\f$ specifies a lower bound for the supply of node
129
     \f$v\f$. It can be either positive or negative, however note that
130
     \f$\sum_{v\in V}delta(v)\f$ should be zero or negative in order to
131
     have a feasible solution.
130
     Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$
131
     \f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and
132
     upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$
133
     holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$
134
     denotes the signed supply values of the nodes.
135
     If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$
136
     supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with
137
     \f$-sup(u)\f$ demand.
138
     A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$
139
     solution of the following problem.
132 140

	
133
     \note A special case of this problem is when
134
     \f$\sum_{v\in V}delta(v) = 0\f$. Then the supply of each node \f$v\f$
135
     will be \e equal \e to \f$delta(v)\f$, if a circulation can be found.
136
     Thus a feasible solution for the
137
     \ref min_cost_flow "minimum cost flow" problem can be calculated
138
     in this way.
141
     \f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu)
142
     \geq sup(u) \quad \forall u\in V, \f]
143
     \f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f]
144
     
145
     The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be
146
     zero or negative in order to have a feasible solution (since the sum
147
     of the expressions on the left-hand side of the inequalities is zero).
148
     It means that the total demand must be greater or equal to the total
149
     supply and all the supplies have to be carried out from the supply nodes,
150
     but there could be demands that are not satisfied.
151
     If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand
152
     constraints have to be satisfied with equality, i.e. all demands
153
     have to be satisfied and all supplies have to be used.
154
     
155
     If you need the opposite inequalities in the supply/demand constraints
156
     (i.e. the total demand is less than the total supply and all the demands
157
     have to be satisfied while there could be supplies that are not used),
158
     then you could easily transform the problem to the above form by reversing
159
     the direction of the arcs and taking the negative of the supply values
160
     (e.g. using \ref ReverseDigraph and \ref NegMap adaptors).
139 161

	
140
     \tparam _Digraph The type of the digraph the algorithm runs on.
141
     \tparam _LCapMap The type of the lower bound capacity map. The default
142
     map type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>".
143
     \tparam _UCapMap The type of the upper bound capacity map. The default
144
     map type is \c _LCapMap.
145
     \tparam _DeltaMap The type of the map that stores the lower bound
146
     for the supply of the nodes. The default map type is
147
     \c _Digraph::ArcMap<_UCapMap::Value>.
162
     This algorithm either calculates a feasible circulation, or provides
163
     a \ref barrier() "barrier", which prooves that a feasible soultion
164
     cannot exist.
165

	
166
     Note that this algorithm also provides a feasible solution for the
167
     \ref min_cost_flow "minimum cost flow problem".
168

	
169
     \tparam GR The type of the digraph the algorithm runs on.
170
     \tparam LM The type of the lower bound map. The default
171
     map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
172
     \tparam UM The type of the upper bound (capacity) map.
173
     The default map type is \c LM.
174
     \tparam SM The type of the supply map. The default map type is
175
     \ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>".
148 176
  */
149 177
#ifdef DOXYGEN
150
template< typename _Digraph,
151
          typename _LCapMap,
152
          typename _UCapMap,
153
          typename _DeltaMap,
154
          typename _Traits >
178
template< typename GR,
179
          typename LM,
180
          typename UM,
181
          typename SM,
182
          typename TR >
155 183
#else
156
template< typename _Digraph,
157
          typename _LCapMap = typename _Digraph::template ArcMap<int>,
158
          typename _UCapMap = _LCapMap,
159
          typename _DeltaMap = typename _Digraph::
160
                               template NodeMap<typename _UCapMap::Value>,
161
          typename _Traits=CirculationDefaultTraits<_Digraph, _LCapMap,
162
                                                    _UCapMap, _DeltaMap> >
184
template< typename GR,
185
          typename LM = typename GR::template ArcMap<int>,
186
          typename UM = LM,
187
          typename SM = typename GR::template NodeMap<typename UM::Value>,
188
          typename TR = CirculationDefaultTraits<GR, LM, UM, SM> >
163 189
#endif
164 190
  class Circulation {
165 191
  public:
166 192

	
167 193
    ///The \ref CirculationDefaultTraits "traits class" of the algorithm.
168
    typedef _Traits Traits;
194
    typedef TR Traits;
169 195
    ///The type of the digraph the algorithm runs on.
170 196
    typedef typename Traits::Digraph Digraph;
171
    ///The type of the flow values.
197
    ///The type of the flow and supply values.
172 198
    typedef typename Traits::Value Value;
173 199

	
174
    /// The type of the lower bound capacity map.
175
    typedef typename Traits::LCapMap LCapMap;
176
    /// The type of the upper bound capacity map.
177
    typedef typename Traits::UCapMap UCapMap;
178
    /// \brief The type of the map that stores the lower bound for
179
    /// the supply of the nodes.
180
    typedef typename Traits::DeltaMap DeltaMap;
200
    ///The type of the lower bound map.
201
    typedef typename Traits::LowerMap LowerMap;
202
    ///The type of the upper bound (capacity) map.
203
    typedef typename Traits::UpperMap UpperMap;
204
    ///The type of the supply map.
205
    typedef typename Traits::SupplyMap SupplyMap;
181 206
    ///The type of the flow map.
182 207
    typedef typename Traits::FlowMap FlowMap;
183 208

	
184 209
    ///The type of the elevator.
185 210
    typedef typename Traits::Elevator Elevator;
186 211
    ///The type of the tolerance.
187 212
    typedef typename Traits::Tolerance Tolerance;
188 213

	
189 214
  private:
190 215

	
191 216
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
192 217

	
193 218
    const Digraph &_g;
194 219
    int _node_num;
195 220

	
196
    const LCapMap *_lo;
197
    const UCapMap *_up;
198
    const DeltaMap *_delta;
221
    const LowerMap *_lo;
222
    const UpperMap *_up;
223
    const SupplyMap *_supply;
199 224

	
200 225
    FlowMap *_flow;
201 226
    bool _local_flow;
202 227

	
203 228
    Elevator* _level;
204 229
    bool _local_level;
205 230

	
206 231
    typedef typename Digraph::template NodeMap<Value> ExcessMap;
207 232
    ExcessMap* _excess;
208 233

	
209 234
    Tolerance _tol;
210 235
    int _el;
211 236

	
212 237
  public:
213 238

	
214 239
    typedef Circulation Create;
215 240

	
216 241
    ///\name Named Template Parameters
217 242

	
218 243
    ///@{
219 244

	
220
    template <typename _FlowMap>
245
    template <typename T>
221 246
    struct SetFlowMapTraits : public Traits {
222
      typedef _FlowMap FlowMap;
247
      typedef T FlowMap;
223 248
      static FlowMap *createFlowMap(const Digraph&) {
224 249
        LEMON_ASSERT(false, "FlowMap is not initialized");
225 250
        return 0; // ignore warnings
226 251
      }
227 252
    };
228 253

	
229 254
    /// \brief \ref named-templ-param "Named parameter" for setting
230 255
    /// FlowMap type
231 256
    ///
232 257
    /// \ref named-templ-param "Named parameter" for setting FlowMap
233 258
    /// type.
234
    template <typename _FlowMap>
259
    template <typename T>
235 260
    struct SetFlowMap
236
      : public Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
237
                           SetFlowMapTraits<_FlowMap> > {
238
      typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
239
                          SetFlowMapTraits<_FlowMap> > Create;
261
      : public Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
262
                           SetFlowMapTraits<T> > {
263
      typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
264
                          SetFlowMapTraits<T> > Create;
240 265
    };
241 266

	
242
    template <typename _Elevator>
267
    template <typename T>
243 268
    struct SetElevatorTraits : public Traits {
244
      typedef _Elevator Elevator;
269
      typedef T Elevator;
245 270
      static Elevator *createElevator(const Digraph&, int) {
246 271
        LEMON_ASSERT(false, "Elevator is not initialized");
247 272
        return 0; // ignore warnings
248 273
      }
249 274
    };
250 275

	
251 276
    /// \brief \ref named-templ-param "Named parameter" for setting
252 277
    /// Elevator type
253 278
    ///
254 279
    /// \ref named-templ-param "Named parameter" for setting Elevator
255 280
    /// type. If this named parameter is used, then an external
256 281
    /// elevator object must be passed to the algorithm using the
257 282
    /// \ref elevator(Elevator&) "elevator()" function before calling
258 283
    /// \ref run() or \ref init().
259 284
    /// \sa SetStandardElevator
260
    template <typename _Elevator>
285
    template <typename T>
261 286
    struct SetElevator
262
      : public Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
263
                           SetElevatorTraits<_Elevator> > {
264
      typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
265
                          SetElevatorTraits<_Elevator> > Create;
287
      : public Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
288
                           SetElevatorTraits<T> > {
289
      typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
290
                          SetElevatorTraits<T> > Create;
266 291
    };
267 292

	
268
    template <typename _Elevator>
293
    template <typename T>
269 294
    struct SetStandardElevatorTraits : public Traits {
270
      typedef _Elevator Elevator;
295
      typedef T Elevator;
271 296
      static Elevator *createElevator(const Digraph& digraph, int max_level) {
272 297
        return new Elevator(digraph, max_level);
273 298
      }
274 299
    };
275 300

	
276 301
    /// \brief \ref named-templ-param "Named parameter" for setting
277 302
    /// Elevator type with automatic allocation
278 303
    ///
279 304
    /// \ref named-templ-param "Named parameter" for setting Elevator
280 305
    /// type with automatic allocation.
281 306
    /// The Elevator should have standard constructor interface to be
282 307
    /// able to automatically created by the algorithm (i.e. the
283 308
    /// digraph and the maximum level should be passed to it).
284 309
    /// However an external elevator object could also be passed to the
285 310
    /// algorithm with the \ref elevator(Elevator&) "elevator()" function
286 311
    /// before calling \ref run() or \ref init().
287 312
    /// \sa SetElevator
288
    template <typename _Elevator>
313
    template <typename T>
289 314
    struct SetStandardElevator
290
      : public Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
291
                       SetStandardElevatorTraits<_Elevator> > {
292
      typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap,
293
                      SetStandardElevatorTraits<_Elevator> > Create;
315
      : public Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
316
                       SetStandardElevatorTraits<T> > {
317
      typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
318
                      SetStandardElevatorTraits<T> > Create;
294 319
    };
295 320

	
296 321
    /// @}
297 322

	
298 323
  protected:
299 324

	
300 325
    Circulation() {}
301 326

	
302 327
  public:
303 328

	
304
    /// The constructor of the class.
329
    /// Constructor.
305 330

	
306 331
    /// The constructor of the class.
307
    /// \param g The digraph the algorithm runs on.
308
    /// \param lo The lower bound capacity of the arcs.
309
    /// \param up The upper bound capacity of the arcs.
310
    /// \param delta The lower bound for the supply of the nodes.
311
    Circulation(const Digraph &g,const LCapMap &lo,
312
                const UCapMap &up,const DeltaMap &delta)
313
      : _g(g), _node_num(),
314
        _lo(&lo),_up(&up),_delta(&delta),_flow(0),_local_flow(false),
315
        _level(0), _local_level(false), _excess(0), _el() {}
332
    ///
333
    /// \param graph The digraph the algorithm runs on.
334
    /// \param lower The lower bounds for the flow values on the arcs.
335
    /// \param upper The upper bounds (capacities) for the flow values 
336
    /// on the arcs.
337
    /// \param supply The signed supply values of the nodes.
338
    Circulation(const Digraph &graph, const LowerMap &lower,
339
                const UpperMap &upper, const SupplyMap &supply)
340
      : _g(graph), _lo(&lower), _up(&upper), _supply(&supply),
341
        _flow(NULL), _local_flow(false), _level(NULL), _local_level(false),
342
        _excess(NULL) {}
316 343

	
317 344
    /// Destructor.
318 345
    ~Circulation() {
319 346
      destroyStructures();
320 347
    }
321 348

	
322 349

	
323 350
  private:
324 351

	
352
    bool checkBoundMaps() {
353
      for (ArcIt e(_g);e!=INVALID;++e) {
354
        if (_tol.less((*_up)[e], (*_lo)[e])) return false;
355
      }
356
      return true;
357
    }
358

	
325 359
    void createStructures() {
326 360
      _node_num = _el = countNodes(_g);
327 361

	
328 362
      if (!_flow) {
329 363
        _flow = Traits::createFlowMap(_g);
330 364
        _local_flow = true;
331 365
      }
332 366
      if (!_level) {
333 367
        _level = Traits::createElevator(_g, _node_num);
334 368
        _local_level = true;
335 369
      }
336 370
      if (!_excess) {
337 371
        _excess = new ExcessMap(_g);
338 372
      }
339 373
    }
340 374

	
341 375
    void destroyStructures() {
342 376
      if (_local_flow) {
343 377
        delete _flow;
344 378
      }
345 379
      if (_local_level) {
346 380
        delete _level;
347 381
      }
348 382
      if (_excess) {
349 383
        delete _excess;
350 384
      }
351 385
    }
352 386

	
353 387
  public:
354 388

	
355
    /// Sets the lower bound capacity map.
389
    /// Sets the lower bound map.
356 390

	
357
    /// Sets the lower bound capacity map.
391
    /// Sets the lower bound map.
358 392
    /// \return <tt>(*this)</tt>
359
    Circulation& lowerCapMap(const LCapMap& map) {
393
    Circulation& lowerMap(const LowerMap& map) {
360 394
      _lo = &map;
361 395
      return *this;
362 396
    }
363 397

	
364
    /// Sets the upper bound capacity map.
398
    /// Sets the upper bound (capacity) map.
365 399

	
366
    /// Sets the upper bound capacity map.
400
    /// Sets the upper bound (capacity) map.
367 401
    /// \return <tt>(*this)</tt>
368
    Circulation& upperCapMap(const LCapMap& map) {
402
    Circulation& upperMap(const UpperMap& map) {
369 403
      _up = &map;
370 404
      return *this;
371 405
    }
372 406

	
373
    /// Sets the lower bound map for the supply of the nodes.
407
    /// Sets the supply map.
374 408

	
375
    /// Sets the lower bound map for the supply of the nodes.
409
    /// Sets the supply map.
376 410
    /// \return <tt>(*this)</tt>
377
    Circulation& deltaMap(const DeltaMap& map) {
378
      _delta = &map;
411
    Circulation& supplyMap(const SupplyMap& map) {
412
      _supply = &map;
379 413
      return *this;
380 414
    }
381 415

	
382 416
    /// \brief Sets the flow map.
383 417
    ///
384 418
    /// Sets the flow map.
385 419
    /// If you don't use this function before calling \ref run() or
386 420
    /// \ref init(), an instance will be allocated automatically.
387 421
    /// The destructor deallocates this automatically allocated map,
388 422
    /// of course.
389 423
    /// \return <tt>(*this)</tt>
390 424
    Circulation& flowMap(FlowMap& map) {
391 425
      if (_local_flow) {
392 426
        delete _flow;
393 427
        _local_flow = false;
394 428
      }
395 429
      _flow = &map;
396 430
      return *this;
397 431
    }
398 432

	
399 433
    /// \brief Sets the elevator used by algorithm.
400 434
    ///
401 435
    /// Sets the elevator used by algorithm.
402 436
    /// If you don't use this function before calling \ref run() or
403 437
    /// \ref init(), an instance will be allocated automatically.
404 438
    /// The destructor deallocates this automatically allocated elevator,
405 439
    /// of course.
406 440
    /// \return <tt>(*this)</tt>
407 441
    Circulation& elevator(Elevator& elevator) {
408 442
      if (_local_level) {
409 443
        delete _level;
410 444
        _local_level = false;
411 445
      }
412 446
      _level = &elevator;
413 447
      return *this;
414 448
    }
415 449

	
416 450
    /// \brief Returns a const reference to the elevator.
417 451
    ///
418 452
    /// Returns a const reference to the elevator.
419 453
    ///
420 454
    /// \pre Either \ref run() or \ref init() must be called before
421 455
    /// using this function.
422 456
    const Elevator& elevator() const {
423 457
      return *_level;
424 458
    }
425 459

	
426
    /// \brief Sets the tolerance used by algorithm.
460
    /// \brief Sets the tolerance used by the algorithm.
427 461
    ///
428
    /// Sets the tolerance used by algorithm.
429
    Circulation& tolerance(const Tolerance& tolerance) const {
462
    /// Sets the tolerance object used by the algorithm.
463
    /// \return <tt>(*this)</tt>
464
    Circulation& tolerance(const Tolerance& tolerance) {
430 465
      _tol = tolerance;
431 466
      return *this;
432 467
    }
433 468

	
434 469
    /// \brief Returns a const reference to the tolerance.
435 470
    ///
436
    /// Returns a const reference to the tolerance.
471
    /// Returns a const reference to the tolerance object used by
472
    /// the algorithm.
437 473
    const Tolerance& tolerance() const {
438
      return tolerance;
474
      return _tol;
439 475
    }
440 476

	
441 477
    /// \name Execution Control
442 478
    /// The simplest way to execute the algorithm is to call \ref run().\n
443
    /// If you need more control on the initial solution or the execution,
444
    /// first you have to call one of the \ref init() functions, then
479
    /// If you need better control on the initial solution or the execution,
480
    /// you have to call one of the \ref init() functions first, then
445 481
    /// the \ref start() function.
446 482

	
447 483
    ///@{
448 484

	
449 485
    /// Initializes the internal data structures.
450 486

	
451 487
    /// Initializes the internal data structures and sets all flow values
452 488
    /// to the lower bound.
453 489
    void init()
454 490
    {
491
      LEMON_DEBUG(checkBoundMaps(),
492
        "Upper bounds must be greater or equal to the lower bounds");
493

	
455 494
      createStructures();
456 495

	
457 496
      for(NodeIt n(_g);n!=INVALID;++n) {
458
        _excess->set(n, (*_delta)[n]);
497
        (*_excess)[n] = (*_supply)[n];
459 498
      }
460 499

	
461 500
      for (ArcIt e(_g);e!=INVALID;++e) {
462 501
        _flow->set(e, (*_lo)[e]);
463
        _excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_flow)[e]);
464
        _excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_flow)[e]);
502
        (*_excess)[_g.target(e)] += (*_flow)[e];
503
        (*_excess)[_g.source(e)] -= (*_flow)[e];
465 504
      }
466 505

	
467 506
      // global relabeling tested, but in general case it provides
468 507
      // worse performance for random digraphs
469 508
      _level->initStart();
470 509
      for(NodeIt n(_g);n!=INVALID;++n)
471 510
        _level->initAddItem(n);
472 511
      _level->initFinish();
473 512
      for(NodeIt n(_g);n!=INVALID;++n)
474 513
        if(_tol.positive((*_excess)[n]))
475 514
          _level->activate(n);
476 515
    }
477 516

	
478 517
    /// Initializes the internal data structures using a greedy approach.
479 518

	
480 519
    /// Initializes the internal data structures using a greedy approach
481 520
    /// to construct the initial solution.
482 521
    void greedyInit()
483 522
    {
523
      LEMON_DEBUG(checkBoundMaps(),
524
        "Upper bounds must be greater or equal to the lower bounds");
525

	
484 526
      createStructures();
485 527

	
486 528
      for(NodeIt n(_g);n!=INVALID;++n) {
487
        _excess->set(n, (*_delta)[n]);
529
        (*_excess)[n] = (*_supply)[n];
488 530
      }
489 531

	
490 532
      for (ArcIt e(_g);e!=INVALID;++e) {
491
        if (!_tol.positive((*_excess)[_g.target(e)] + (*_up)[e])) {
533
        if (!_tol.less(-(*_excess)[_g.target(e)], (*_up)[e])) {
492 534
          _flow->set(e, (*_up)[e]);
493
          _excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_up)[e]);
494
          _excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_up)[e]);
495
        } else if (_tol.positive((*_excess)[_g.target(e)] + (*_lo)[e])) {
535
          (*_excess)[_g.target(e)] += (*_up)[e];
536
          (*_excess)[_g.source(e)] -= (*_up)[e];
537
        } else if (_tol.less(-(*_excess)[_g.target(e)], (*_lo)[e])) {
496 538
          _flow->set(e, (*_lo)[e]);
497
          _excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_lo)[e]);
498
          _excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_lo)[e]);
539
          (*_excess)[_g.target(e)] += (*_lo)[e];
540
          (*_excess)[_g.source(e)] -= (*_lo)[e];
499 541
        } else {
500 542
          Value fc = -(*_excess)[_g.target(e)];
501 543
          _flow->set(e, fc);
502
          _excess->set(_g.target(e), 0);
503
          _excess->set(_g.source(e), (*_excess)[_g.source(e)] - fc);
544
          (*_excess)[_g.target(e)] = 0;
545
          (*_excess)[_g.source(e)] -= fc;
504 546
        }
505 547
      }
506 548

	
507 549
      _level->initStart();
508 550
      for(NodeIt n(_g);n!=INVALID;++n)
509 551
        _level->initAddItem(n);
510 552
      _level->initFinish();
511 553
      for(NodeIt n(_g);n!=INVALID;++n)
512 554
        if(_tol.positive((*_excess)[n]))
513 555
          _level->activate(n);
514 556
    }
515 557

	
516 558
    ///Executes the algorithm
517 559

	
518 560
    ///This function executes the algorithm.
519 561
    ///
520 562
    ///\return \c true if a feasible circulation is found.
521 563
    ///
522 564
    ///\sa barrier()
523 565
    ///\sa barrierMap()
524 566
    bool start()
525 567
    {
526 568

	
527 569
      Node act;
528 570
      Node bact=INVALID;
529 571
      Node last_activated=INVALID;
530 572
      while((act=_level->highestActive())!=INVALID) {
531 573
        int actlevel=(*_level)[act];
532 574
        int mlevel=_node_num;
533 575
        Value exc=(*_excess)[act];
534 576

	
535 577
        for(OutArcIt e(_g,act);e!=INVALID; ++e) {
536 578
          Node v = _g.target(e);
537 579
          Value fc=(*_up)[e]-(*_flow)[e];
538 580
          if(!_tol.positive(fc)) continue;
539 581
          if((*_level)[v]<actlevel) {
540 582
            if(!_tol.less(fc, exc)) {
541 583
              _flow->set(e, (*_flow)[e] + exc);
542
              _excess->set(v, (*_excess)[v] + exc);
584
              (*_excess)[v] += exc;
543 585
              if(!_level->active(v) && _tol.positive((*_excess)[v]))
544 586
                _level->activate(v);
545
              _excess->set(act,0);
587
              (*_excess)[act] = 0;
546 588
              _level->deactivate(act);
547 589
              goto next_l;
548 590
            }
549 591
            else {
550 592
              _flow->set(e, (*_up)[e]);
551
              _excess->set(v, (*_excess)[v] + fc);
593
              (*_excess)[v] += fc;
552 594
              if(!_level->active(v) && _tol.positive((*_excess)[v]))
553 595
                _level->activate(v);
554 596
              exc-=fc;
555 597
            }
556 598
          }
557 599
          else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
558 600
        }
559 601
        for(InArcIt e(_g,act);e!=INVALID; ++e) {
560 602
          Node v = _g.source(e);
561 603
          Value fc=(*_flow)[e]-(*_lo)[e];
562 604
          if(!_tol.positive(fc)) continue;
563 605
          if((*_level)[v]<actlevel) {
564 606
            if(!_tol.less(fc, exc)) {
565 607
              _flow->set(e, (*_flow)[e] - exc);
566
              _excess->set(v, (*_excess)[v] + exc);
608
              (*_excess)[v] += exc;
567 609
              if(!_level->active(v) && _tol.positive((*_excess)[v]))
568 610
                _level->activate(v);
569
              _excess->set(act,0);
611
              (*_excess)[act] = 0;
570 612
              _level->deactivate(act);
571 613
              goto next_l;
572 614
            }
573 615
            else {
574 616
              _flow->set(e, (*_lo)[e]);
575
              _excess->set(v, (*_excess)[v] + fc);
617
              (*_excess)[v] += fc;
576 618
              if(!_level->active(v) && _tol.positive((*_excess)[v]))
577 619
                _level->activate(v);
578 620
              exc-=fc;
579 621
            }
580 622
          }
581 623
          else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
582 624
        }
583 625

	
584
        _excess->set(act, exc);
626
        (*_excess)[act] = exc;
585 627
        if(!_tol.positive(exc)) _level->deactivate(act);
586 628
        else if(mlevel==_node_num) {
587 629
          _level->liftHighestActiveToTop();
588 630
          _el = _node_num;
589 631
          return false;
590 632
        }
591 633
        else {
592 634
          _level->liftHighestActive(mlevel+1);
593 635
          if(_level->onLevel(actlevel)==0) {
594 636
            _el = actlevel;
595 637
            return false;
596 638
          }
597 639
        }
598 640
      next_l:
599 641
        ;
600 642
      }
601 643
      return true;
602 644
    }
603 645

	
604 646
    /// Runs the algorithm.
605 647

	
606 648
    /// This function runs the algorithm.
607 649
    ///
608 650
    /// \return \c true if a feasible circulation is found.
609 651
    ///
610 652
    /// \note Apart from the return value, c.run() is just a shortcut of
611 653
    /// the following code.
612 654
    /// \code
613 655
    ///   c.greedyInit();
614 656
    ///   c.start();
615 657
    /// \endcode
616 658
    bool run() {
617 659
      greedyInit();
618 660
      return start();
619 661
    }
620 662

	
621 663
    /// @}
622 664

	
623 665
    /// \name Query Functions
624 666
    /// The results of the circulation algorithm can be obtained using
625 667
    /// these functions.\n
626 668
    /// Either \ref run() or \ref start() should be called before
627 669
    /// using them.
628 670

	
629 671
    ///@{
630 672

	
631
    /// \brief Returns the flow on the given arc.
673
    /// \brief Returns the flow value on the given arc.
632 674
    ///
633
    /// Returns the flow on the given arc.
675
    /// Returns the flow value on the given arc.
634 676
    ///
635 677
    /// \pre Either \ref run() or \ref init() must be called before
636 678
    /// using this function.
637 679
    Value flow(const Arc& arc) const {
638 680
      return (*_flow)[arc];
639 681
    }
640 682

	
641 683
    /// \brief Returns a const reference to the flow map.
642 684
    ///
643 685
    /// Returns a const reference to the arc map storing the found flow.
644 686
    ///
645 687
    /// \pre Either \ref run() or \ref init() must be called before
646 688
    /// using this function.
647 689
    const FlowMap& flowMap() const {
648 690
      return *_flow;
649 691
    }
650 692

	
651 693
    /**
652 694
       \brief Returns \c true if the given node is in a barrier.
653 695

	
654 696
       Barrier is a set \e B of nodes for which
655 697

	
656
       \f[ \sum_{a\in\delta_{out}(B)} upper(a) -
657
           \sum_{a\in\delta_{in}(B)} lower(a) < \sum_{v\in B}delta(v) \f]
698
       \f[ \sum_{uv\in A: u\in B} upper(uv) -
699
           \sum_{uv\in A: v\in B} lower(uv) < \sum_{v\in B} sup(v) \f]
658 700

	
659 701
       holds. The existence of a set with this property prooves that a
660 702
       feasible circualtion cannot exist.
661 703

	
662 704
       This function returns \c true if the given node is in the found
663 705
       barrier. If a feasible circulation is found, the function
664 706
       gives back \c false for every node.
665 707

	
666 708
       \pre Either \ref run() or \ref init() must be called before
667 709
       using this function.
668 710

	
669 711
       \sa barrierMap()
670 712
       \sa checkBarrier()
671 713
    */
672 714
    bool barrier(const Node& node) const
673 715
    {
674 716
      return (*_level)[node] >= _el;
675 717
    }
676 718

	
677 719
    /// \brief Gives back a barrier.
678 720
    ///
679 721
    /// This function sets \c bar to the characteristic vector of the
680 722
    /// found barrier. \c bar should be a \ref concepts::WriteMap "writable"
681 723
    /// node map with \c bool (or convertible) value type.
682 724
    ///
683 725
    /// If a feasible circulation is found, the function gives back an
684 726
    /// empty set, so \c bar[v] will be \c false for all nodes \c v.
685 727
    ///
686 728
    /// \note This function calls \ref barrier() for each node,
687
    /// so it runs in \f$O(n)\f$ time.
729
    /// so it runs in O(n) time.
688 730
    ///
689 731
    /// \pre Either \ref run() or \ref init() must be called before
690 732
    /// using this function.
691 733
    ///
692 734
    /// \sa barrier()
693 735
    /// \sa checkBarrier()
694 736
    template<class BarrierMap>
695 737
    void barrierMap(BarrierMap &bar) const
696 738
    {
697 739
      for(NodeIt n(_g);n!=INVALID;++n)
698 740
        bar.set(n, (*_level)[n] >= _el);
699 741
    }
700 742

	
701 743
    /// @}
702 744

	
703 745
    /// \name Checker Functions
704 746
    /// The feasibility of the results can be checked using
705 747
    /// these functions.\n
706 748
    /// Either \ref run() or \ref start() should be called before
707 749
    /// using them.
708 750

	
709 751
    ///@{
710 752

	
711 753
    ///Check if the found flow is a feasible circulation
712 754

	
713 755
    ///Check if the found flow is a feasible circulation,
714 756
    ///
715 757
    bool checkFlow() const {
716 758
      for(ArcIt e(_g);e!=INVALID;++e)
717 759
        if((*_flow)[e]<(*_lo)[e]||(*_flow)[e]>(*_up)[e]) return false;
718 760
      for(NodeIt n(_g);n!=INVALID;++n)
719 761
        {
720
          Value dif=-(*_delta)[n];
762
          Value dif=-(*_supply)[n];
721 763
          for(InArcIt e(_g,n);e!=INVALID;++e) dif-=(*_flow)[e];
722 764
          for(OutArcIt e(_g,n);e!=INVALID;++e) dif+=(*_flow)[e];
723 765
          if(_tol.negative(dif)) return false;
724 766
        }
725 767
      return true;
726 768
    }
727 769

	
728 770
    ///Check whether or not the last execution provides a barrier
729 771

	
730 772
    ///Check whether or not the last execution provides a barrier.
731 773
    ///\sa barrier()
732 774
    ///\sa barrierMap()
733 775
    bool checkBarrier() const
734 776
    {
735 777
      Value delta=0;
778
      Value inf_cap = std::numeric_limits<Value>::has_infinity ?
779
        std::numeric_limits<Value>::infinity() :
780
        std::numeric_limits<Value>::max();
736 781
      for(NodeIt n(_g);n!=INVALID;++n)
737 782
        if(barrier(n))
738
          delta-=(*_delta)[n];
783
          delta-=(*_supply)[n];
739 784
      for(ArcIt e(_g);e!=INVALID;++e)
740 785
        {
741 786
          Node s=_g.source(e);
742 787
          Node t=_g.target(e);
743
          if(barrier(s)&&!barrier(t)) delta+=(*_up)[e];
788
          if(barrier(s)&&!barrier(t)) {
789
            if (_tol.less(inf_cap - (*_up)[e], delta)) return false;
790
            delta+=(*_up)[e];
791
          }
744 792
          else if(barrier(t)&&!barrier(s)) delta-=(*_lo)[e];
745 793
        }
746 794
      return _tol.negative(delta);
747 795
    }
748 796

	
749 797
    /// @}
750 798

	
751 799
  };
752 800

	
753 801
}
754 802

	
755 803
#endif
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/clp.h>
20 20
#include <coin/ClpSimplex.hpp>
21 21

	
22 22
namespace lemon {
23 23

	
24 24
  ClpLp::ClpLp() {
25 25
    _prob = new ClpSimplex();
26 26
    _init_temporals();
27
    messageLevel(MESSAGE_NO_OUTPUT);
27
    messageLevel(MESSAGE_NOTHING);
28 28
  }
29 29

	
30 30
  ClpLp::ClpLp(const ClpLp& other) {
31 31
    _prob = new ClpSimplex(*other._prob);
32 32
    rows = other.rows;
33 33
    cols = other.cols;
34 34
    _init_temporals();
35
    messageLevel(MESSAGE_NO_OUTPUT);
35
    messageLevel(MESSAGE_NOTHING);
36 36
  }
37 37

	
38 38
  ClpLp::~ClpLp() {
39 39
    delete _prob;
40 40
    _clear_temporals();
41 41
  }
42 42

	
43 43
  void ClpLp::_init_temporals() {
44 44
    _primal_ray = 0;
45 45
    _dual_ray = 0;
46 46
  }
47 47

	
48 48
  void ClpLp::_clear_temporals() {
49 49
    if (_primal_ray) {
50 50
      delete[] _primal_ray;
51 51
      _primal_ray = 0;
52 52
    }
53 53
    if (_dual_ray) {
54 54
      delete[] _dual_ray;
55 55
      _dual_ray = 0;
56 56
    }
57 57
  }
58 58

	
59
  ClpLp* ClpLp::_newSolver() const {
59
  ClpLp* ClpLp::newSolver() const {
60 60
    ClpLp* newlp = new ClpLp;
61 61
    return newlp;
62 62
  }
63 63

	
64
  ClpLp* ClpLp::_cloneSolver() const {
64
  ClpLp* ClpLp::cloneSolver() const {
65 65
    ClpLp* copylp = new ClpLp(*this);
66 66
    return copylp;
67 67
  }
68 68

	
69 69
  const char* ClpLp::_solverName() const { return "ClpLp"; }
70 70

	
71 71
  int ClpLp::_addCol() {
72 72
    _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0);
73 73
    return _prob->numberColumns() - 1;
74 74
  }
75 75

	
76 76
  int ClpLp::_addRow() {
77 77
    _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
78 78
    return _prob->numberRows() - 1;
79 79
  }
80 80

	
81
  int ClpLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
82
    std::vector<int> indexes;
83
    std::vector<Value> values;
84

	
85
    for(ExprIterator it = b; it != e; ++it) {
86
      indexes.push_back(it->first);
87
      values.push_back(it->second);
88
    }
89

	
90
    _prob->addRow(values.size(), &indexes.front(), &values.front(), l, u);
91
    return _prob->numberRows() - 1;
92
  }
93

	
81 94

	
82 95
  void ClpLp::_eraseCol(int c) {
83 96
    _col_names_ref.erase(_prob->getColumnName(c));
84 97
    _prob->deleteColumns(1, &c);
85 98
  }
86 99

	
87 100
  void ClpLp::_eraseRow(int r) {
88 101
    _row_names_ref.erase(_prob->getRowName(r));
89 102
    _prob->deleteRows(1, &r);
90 103
  }
91 104

	
92 105
  void ClpLp::_eraseColId(int i) {
93 106
    cols.eraseIndex(i);
94 107
    cols.shiftIndices(i);
95 108
  }
96 109

	
97 110
  void ClpLp::_eraseRowId(int i) {
98 111
    rows.eraseIndex(i);
99 112
    rows.shiftIndices(i);
100 113
  }
101 114

	
102 115
  void ClpLp::_getColName(int c, std::string& name) const {
103 116
    name = _prob->getColumnName(c);
104 117
  }
105 118

	
106 119
  void ClpLp::_setColName(int c, const std::string& name) {
107 120
    _prob->setColumnName(c, const_cast<std::string&>(name));
108 121
    _col_names_ref[name] = c;
109 122
  }
110 123

	
111 124
  int ClpLp::_colByName(const std::string& name) const {
112 125
    std::map<std::string, int>::const_iterator it = _col_names_ref.find(name);
113 126
    return it != _col_names_ref.end() ? it->second : -1;
114 127
  }
115 128

	
116 129
  void ClpLp::_getRowName(int r, std::string& name) const {
117 130
    name = _prob->getRowName(r);
118 131
  }
119 132

	
120 133
  void ClpLp::_setRowName(int r, const std::string& name) {
121 134
    _prob->setRowName(r, const_cast<std::string&>(name));
122 135
    _row_names_ref[name] = r;
123 136
  }
124 137

	
125 138
  int ClpLp::_rowByName(const std::string& name) const {
126 139
    std::map<std::string, int>::const_iterator it = _row_names_ref.find(name);
127 140
    return it != _row_names_ref.end() ? it->second : -1;
128 141
  }
129 142

	
130 143

	
131 144
  void ClpLp::_setRowCoeffs(int ix, ExprIterator b, ExprIterator e) {
132 145
    std::map<int, Value> coeffs;
133 146

	
134 147
    int n = _prob->clpMatrix()->getNumCols();
135 148

	
136 149
    const int* indices = _prob->clpMatrix()->getIndices();
137 150
    const double* elements = _prob->clpMatrix()->getElements();
138 151

	
139 152
    for (int i = 0; i < n; ++i) {
140 153
      CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
141 154
      CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
142 155

	
143 156
      const int* it = std::lower_bound(indices + begin, indices + end, ix);
144 157
      if (it != indices + end && *it == ix && elements[it - indices] != 0.0) {
145 158
        coeffs[i] = 0.0;
146 159
      }
147 160
    }
148 161

	
149 162
    for (ExprIterator it = b; it != e; ++it) {
150 163
      coeffs[it->first] = it->second;
151 164
    }
152 165

	
153 166
    for (std::map<int, Value>::iterator it = coeffs.begin();
154 167
         it != coeffs.end(); ++it) {
155 168
      _prob->modifyCoefficient(ix, it->first, it->second);
156 169
    }
157 170
  }
158 171

	
159 172
  void ClpLp::_getRowCoeffs(int ix, InsertIterator b) const {
160 173
    int n = _prob->clpMatrix()->getNumCols();
161 174

	
162 175
    const int* indices = _prob->clpMatrix()->getIndices();
163 176
    const double* elements = _prob->clpMatrix()->getElements();
164 177

	
165 178
    for (int i = 0; i < n; ++i) {
166 179
      CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
167 180
      CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
168 181

	
169 182
      const int* it = std::lower_bound(indices + begin, indices + end, ix);
170 183
      if (it != indices + end && *it == ix) {
171 184
        *b = std::make_pair(i, elements[it - indices]);
172 185
      }
173 186
    }
174 187
  }
175 188

	
176 189
  void ClpLp::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
... ...
@@ -337,101 +350,117 @@
337 350
  }
338 351

	
339 352
  ClpLp::VarStatus ClpLp::_getColStatus(int i) const {
340 353
    switch (_prob->getColumnStatus(i)) {
341 354
    case ClpSimplex::basic:
342 355
      return BASIC;
343 356
    case ClpSimplex::isFree:
344 357
      return FREE;
345 358
    case ClpSimplex::atUpperBound:
346 359
      return UPPER;
347 360
    case ClpSimplex::atLowerBound:
348 361
      return LOWER;
349 362
    case ClpSimplex::isFixed:
350 363
      return FIXED;
351 364
    case ClpSimplex::superBasic:
352 365
      return FREE;
353 366
    default:
354 367
      LEMON_ASSERT(false, "Wrong column status");
355 368
      return VarStatus();
356 369
    }
357 370
  }
358 371

	
359 372
  ClpLp::VarStatus ClpLp::_getRowStatus(int i) const {
360 373
    switch (_prob->getColumnStatus(i)) {
361 374
    case ClpSimplex::basic:
362 375
      return BASIC;
363 376
    case ClpSimplex::isFree:
364 377
      return FREE;
365 378
    case ClpSimplex::atUpperBound:
366 379
      return UPPER;
367 380
    case ClpSimplex::atLowerBound:
368 381
      return LOWER;
369 382
    case ClpSimplex::isFixed:
370 383
      return FIXED;
371 384
    case ClpSimplex::superBasic:
372 385
      return FREE;
373 386
    default:
374 387
      LEMON_ASSERT(false, "Wrong row status");
375 388
      return VarStatus();
376 389
    }
377 390
  }
378 391

	
379 392

	
380 393
  ClpLp::ProblemType ClpLp::_getPrimalType() const {
381 394
    if (_prob->isProvenOptimal()) {
382 395
      return OPTIMAL;
383 396
    } else if (_prob->isProvenPrimalInfeasible()) {
384 397
      return INFEASIBLE;
385 398
    } else if (_prob->isProvenDualInfeasible()) {
386 399
      return UNBOUNDED;
387 400
    } else {
388 401
      return UNDEFINED;
389 402
    }
390 403
  }
391 404

	
392 405
  ClpLp::ProblemType ClpLp::_getDualType() const {
393 406
    if (_prob->isProvenOptimal()) {
394 407
      return OPTIMAL;
395 408
    } else if (_prob->isProvenDualInfeasible()) {
396 409
      return INFEASIBLE;
397 410
    } else if (_prob->isProvenPrimalInfeasible()) {
398 411
      return INFEASIBLE;
399 412
    } else {
400 413
      return UNDEFINED;
401 414
    }
402 415
  }
403 416

	
404 417
  void ClpLp::_setSense(ClpLp::Sense sense) {
405 418
    switch (sense) {
406 419
    case MIN:
407 420
      _prob->setOptimizationDirection(1);
408 421
      break;
409 422
    case MAX:
410 423
      _prob->setOptimizationDirection(-1);
411 424
      break;
412 425
    }
413 426
  }
414 427

	
415 428
  ClpLp::Sense ClpLp::_getSense() const {
416 429
    double dir = _prob->optimizationDirection();
417 430
    if (dir > 0.0) {
418 431
      return MIN;
419 432
    } else {
420 433
      return MAX;
421 434
    }
422 435
  }
423 436

	
424 437
  void ClpLp::_clear() {
425 438
    delete _prob;
426 439
    _prob = new ClpSimplex();
427 440
    rows.clear();
428 441
    cols.clear();
429 442
    _col_names_ref.clear();
430 443
    _clear_temporals();
431 444
  }
432 445

	
433
  void ClpLp::messageLevel(MessageLevel m) {
434
    _prob->setLogLevel(static_cast<int>(m));
446
  void ClpLp::_messageLevel(MessageLevel level) {
447
    switch (level) {
448
    case MESSAGE_NOTHING:
449
      _prob->setLogLevel(0);
450
      break;
451
    case MESSAGE_ERROR:
452
      _prob->setLogLevel(1);
453
      break;
454
    case MESSAGE_WARNING:
455
      _prob->setLogLevel(2);
456
      break;
457
    case MESSAGE_NORMAL:
458
      _prob->setLogLevel(3);
459
      break;
460
    case MESSAGE_VERBOSE:
461
      _prob->setLogLevel(4);
462
      break;
463
    }
435 464
  }
436 465

	
437 466
} //END OF NAMESPACE LEMON
Ignore white space 6 line context
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_CLP_H
20 20
#define LEMON_CLP_H
21 21

	
22 22
///\file
23 23
///\brief Header of the LEMON-CLP lp solver interface.
24 24

	
25 25
#include <vector>
26 26
#include <string>
27 27

	
28 28
#include <lemon/lp_base.h>
29 29

	
30 30
class ClpSimplex;
31 31

	
32 32
namespace lemon {
33 33

	
34 34
  /// \ingroup lp_group
35 35
  ///
36 36
  /// \brief Interface for the CLP solver
37 37
  ///
38 38
  /// This class implements an interface for the Clp LP solver.  The
39 39
  /// Clp library is an object oriented lp solver library developed at
40 40
  /// the IBM. The CLP is part of the COIN-OR package and it can be
41 41
  /// used with Common Public License.
42 42
  class ClpLp : public LpSolver {
43 43
  protected:
44 44

	
45 45
    ClpSimplex* _prob;
46 46

	
47 47
    std::map<std::string, int> _col_names_ref;
48 48
    std::map<std::string, int> _row_names_ref;
49 49

	
50 50
  public:
51 51

	
52 52
    /// \e
53 53
    ClpLp();
54 54
    /// \e
55 55
    ClpLp(const ClpLp&);
56 56
    /// \e
57 57
    ~ClpLp();
58 58

	
59
    /// \e
60
    virtual ClpLp* newSolver() const;
61
    /// \e
62
    virtual ClpLp* cloneSolver() const;
63

	
59 64
  protected:
60 65

	
61 66
    mutable double* _primal_ray;
62 67
    mutable double* _dual_ray;
63 68

	
64 69
    void _init_temporals();
65 70
    void _clear_temporals();
66 71

	
67 72
  protected:
68 73

	
69
    virtual ClpLp* _newSolver() const;
70
    virtual ClpLp* _cloneSolver() const;
71

	
72 74
    virtual const char* _solverName() const;
73 75

	
74 76
    virtual int _addCol();
75 77
    virtual int _addRow();
78
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
76 79

	
77 80
    virtual void _eraseCol(int i);
78 81
    virtual void _eraseRow(int i);
79 82

	
80 83
    virtual void _eraseColId(int i);
81 84
    virtual void _eraseRowId(int i);
82 85

	
83 86
    virtual void _getColName(int col, std::string& name) const;
84 87
    virtual void _setColName(int col, const std::string& name);
85 88
    virtual int _colByName(const std::string& name) const;
86 89

	
87 90
    virtual void _getRowName(int row, std::string& name) const;
88 91
    virtual void _setRowName(int row, const std::string& name);
89 92
    virtual int _rowByName(const std::string& name) const;
90 93

	
91 94
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
92 95
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
93 96

	
94 97
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
95 98
    virtual void _getColCoeffs(int i, InsertIterator b) const;
96 99

	
97 100
    virtual void _setCoeff(int row, int col, Value value);
98 101
    virtual Value _getCoeff(int row, int col) const;
99 102

	
100 103
    virtual void _setColLowerBound(int i, Value value);
101 104
    virtual Value _getColLowerBound(int i) const;
102 105
    virtual void _setColUpperBound(int i, Value value);
103 106
    virtual Value _getColUpperBound(int i) const;
104 107

	
105 108
    virtual void _setRowLowerBound(int i, Value value);
106 109
    virtual Value _getRowLowerBound(int i) const;
107 110
    virtual void _setRowUpperBound(int i, Value value);
108 111
    virtual Value _getRowUpperBound(int i) const;
109 112

	
110 113
    virtual void _setObjCoeffs(ExprIterator, ExprIterator);
111 114
    virtual void _getObjCoeffs(InsertIterator) const;
112 115

	
113 116
    virtual void _setObjCoeff(int i, Value obj_coef);
114 117
    virtual Value _getObjCoeff(int i) const;
115 118

	
116 119
    virtual void _setSense(Sense sense);
117 120
    virtual Sense _getSense() const;
118 121

	
119 122
    virtual SolveExitStatus _solve();
120 123

	
121 124
    virtual Value _getPrimal(int i) const;
122 125
    virtual Value _getDual(int i) const;
123 126

	
124 127
    virtual Value _getPrimalValue() const;
125 128

	
126 129
    virtual Value _getPrimalRay(int i) const;
127 130
    virtual Value _getDualRay(int i) const;
128 131

	
129 132
    virtual VarStatus _getColStatus(int i) const;
130 133
    virtual VarStatus _getRowStatus(int i) const;
131 134

	
132 135
    virtual ProblemType _getPrimalType() const;
133 136
    virtual ProblemType _getDualType() const;
134 137

	
135 138
    virtual void _clear();
136 139

	
140
    virtual void _messageLevel(MessageLevel);
141
    
137 142
  public:
138 143

	
139 144
    ///Solves LP with primal simplex method.
140 145
    SolveExitStatus solvePrimal();
141 146

	
142 147
    ///Solves LP with dual simplex method.
143 148
    SolveExitStatus solveDual();
144 149

	
145 150
    ///Solves LP with barrier method.
146 151
    SolveExitStatus solveBarrier();
147 152

	
148 153
    ///Returns the constraint identifier understood by CLP.
149 154
    int clpRow(Row r) const { return rows(id(r)); }
150 155

	
151 156
    ///Returns the variable identifier understood by CLP.
152 157
    int clpCol(Col c) const { return cols(id(c)); }
153 158

	
154
    ///Enum for \c messageLevel() parameter
155
    enum MessageLevel {
156
      /// no output (default value)
157
      MESSAGE_NO_OUTPUT = 0,
158
      /// print final solution
159
      MESSAGE_FINAL_SOLUTION = 1,
160
      /// print factorization
161
      MESSAGE_FACTORIZATION = 2,
162
      /// normal output
163
      MESSAGE_NORMAL_OUTPUT = 3,
164
      /// verbose output
165
      MESSAGE_VERBOSE_OUTPUT = 4
166
    };
167
    ///Set the verbosity of the messages
168

	
169
    ///Set the verbosity of the messages
170
    ///
171
    ///\param m is the level of the messages output by the solver routines.
172
    void messageLevel(MessageLevel m);
173

	
174 159
  };
175 160

	
176 161
} //END OF NAMESPACE LEMON
177 162

	
178 163
#endif //LEMON_CLP_H
179 164

	
Ignore white space 6 line context
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-2009
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
#ifndef LEMON_CONCEPT_DIGRAPH_H
20
#define LEMON_CONCEPT_DIGRAPH_H
19
#ifndef LEMON_CONCEPTS_DIGRAPH_H
20
#define LEMON_CONCEPTS_DIGRAPH_H
21 21

	
22 22
///\ingroup graph_concepts
23 23
///\file
24 24
///\brief The concept of directed graphs.
25 25

	
26 26
#include <lemon/core.h>
27 27
#include <lemon/concepts/maps.h>
28 28
#include <lemon/concept_check.h>
29 29
#include <lemon/concepts/graph_components.h>
30 30

	
31 31
namespace lemon {
32 32
  namespace concepts {
33 33

	
34 34
    /// \ingroup graph_concepts
35 35
    ///
36 36
    /// \brief Class describing the concept of directed graphs.
37 37
    ///
38
    /// This class describes the \ref concept "concept" of the
39
    /// immutable directed digraphs.
38
    /// This class describes the common interface of all directed
39
    /// graphs (digraphs).
40 40
    ///
41
    /// Note that actual digraph implementation like @ref ListDigraph or
42
    /// @ref SmartDigraph may have several additional functionality.
41
    /// Like all concept classes, it only provides an interface
42
    /// without any sensible implementation. So any general algorithm for
43
    /// directed graphs should compile with this class, but it will not
44
    /// run properly, of course.
45
    /// An actual digraph implementation like \ref ListDigraph or
46
    /// \ref SmartDigraph may have additional functionality.
43 47
    ///
44
    /// \sa concept
48
    /// \sa Graph
45 49
    class Digraph {
46 50
    private:
47
      ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
51
      /// Diraphs are \e not copy constructible. Use DigraphCopy instead.
52
      Digraph(const Digraph &) {}
53
      /// \brief Assignment of a digraph to another one is \e not allowed.
54
      /// Use DigraphCopy instead.
55
      void operator=(const Digraph &) {}
48 56

	
49
      ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
50
      ///
51
      Digraph(const Digraph &) {};
52
      ///\brief Assignment of \ref Digraph "Digraph"s to another ones are
53
      ///\e not allowed. Use DigraphCopy() instead.
57
    public:
58
      /// Default constructor.
59
      Digraph() { }
54 60

	
55
      ///Assignment of \ref Digraph "Digraph"s to another ones are
56
      ///\e not allowed.  Use DigraphCopy() instead.
57

	
58
      void operator=(const Digraph &) {}
59
    public:
60
      ///\e
61

	
62
      /// Defalult constructor.
63

	
64
      /// Defalult constructor.
65
      ///
66
      Digraph() { }
67
      /// Class for identifying a node of the digraph
61
      /// The node type of the digraph
68 62

	
69 63
      /// This class identifies a node of the digraph. It also serves
70 64
      /// as a base class of the node iterators,
71
      /// thus they will convert to this type.
65
      /// thus they convert to this type.
72 66
      class Node {
73 67
      public:
74 68
        /// Default constructor
75 69

	
76
        /// @warning The default constructor sets the iterator
77
        /// to an undefined value.
70
        /// Default constructor.
71
        /// \warning It sets the object to an undefined value.
78 72
        Node() { }
79 73
        /// Copy constructor.
80 74

	
81 75
        /// Copy constructor.
82 76
        ///
83 77
        Node(const Node&) { }
84 78

	
85
        /// Invalid constructor \& conversion.
79
        /// %Invalid constructor \& conversion.
86 80

	
87
        /// This constructor initializes the iterator to be invalid.
81
        /// Initializes the object to be invalid.
88 82
        /// \sa Invalid for more details.
89 83
        Node(Invalid) { }
90 84
        /// Equality operator
91 85

	
86
        /// Equality operator.
87
        ///
92 88
        /// Two iterators are equal if and only if they point to the
93
        /// same object or both are invalid.
89
        /// same object or both are \c INVALID.
94 90
        bool operator==(Node) const { return true; }
95 91

	
96 92
        /// Inequality operator
97 93

	
98
        /// \sa operator==(Node n)
99
        ///
94
        /// Inequality operator.
100 95
        bool operator!=(Node) const { return true; }
101 96

	
102 97
        /// Artificial ordering operator.
103 98

	
104
        /// To allow the use of digraph descriptors as key type in std::map or
105
        /// similar associative container we require this.
99
        /// Artificial ordering operator.
106 100
        ///
107
        /// \note This operator only have to define some strict ordering of
108
        /// the items; this order has nothing to do with the iteration
109
        /// ordering of the items.
101
        /// \note This operator only has to define some strict ordering of
102
        /// the nodes; this order has nothing to do with the iteration
103
        /// ordering of the nodes.
110 104
        bool operator<(Node) const { return false; }
111

	
112 105
      };
113 106

	
114
      /// This iterator goes through each node.
107
      /// Iterator class for the nodes.
115 108

	
116
      /// This iterator goes through each node.
109
      /// This iterator goes through each node of the digraph.
117 110
      /// Its usage is quite simple, for example you can count the number
118
      /// of nodes in digraph \c g of type \c Digraph like this:
111
      /// of nodes in a digraph \c g of type \c %Digraph like this:
119 112
      ///\code
120 113
      /// int count=0;
121 114
      /// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count;
122 115
      ///\endcode
123 116
      class NodeIt : public Node {
124 117
      public:
125 118
        /// Default constructor
126 119

	
127
        /// @warning The default constructor sets the iterator
128
        /// to an undefined value.
120
        /// Default constructor.
121
        /// \warning It sets the iterator to an undefined value.
129 122
        NodeIt() { }
130 123
        /// Copy constructor.
131 124

	
132 125
        /// Copy constructor.
133 126
        ///
134 127
        NodeIt(const NodeIt& n) : Node(n) { }
135
        /// Invalid constructor \& conversion.
128
        /// %Invalid constructor \& conversion.
136 129

	
137
        /// Initialize the iterator to be invalid.
130
        /// Initializes the iterator to be invalid.
138 131
        /// \sa Invalid for more details.
139 132
        NodeIt(Invalid) { }
140 133
        /// Sets the iterator to the first node.
141 134

	
142
        /// Sets the iterator to the first node of \c g.
135
        /// Sets the iterator to the first node of the given digraph.
143 136
        ///
144
        NodeIt(const Digraph&) { }
145
        /// Node -> NodeIt conversion.
137
        explicit NodeIt(const Digraph&) { }
138
        /// Sets the iterator to the given node.
146 139

	
147
        /// Sets the iterator to the node of \c the digraph pointed by
148
        /// the trivial iterator.
149
        /// This feature necessitates that each time we
150
        /// iterate the arc-set, the iteration order is the same.
140
        /// Sets the iterator to the given node of the given digraph.
141
        ///
151 142
        NodeIt(const Digraph&, const Node&) { }
152 143
        /// Next node.
153 144

	
154 145
        /// Assign the iterator to the next node.
155 146
        ///
156 147
        NodeIt& operator++() { return *this; }
157 148
      };
158 149

	
159 150

	
160
      /// Class for identifying an arc of the digraph
151
      /// The arc type of the digraph
161 152

	
162 153
      /// This class identifies an arc of the digraph. It also serves
163 154
      /// as a base class of the arc iterators,
164 155
      /// thus they will convert to this type.
165 156
      class Arc {
166 157
      public:
167 158
        /// Default constructor
168 159

	
169
        /// @warning The default constructor sets the iterator
170
        /// to an undefined value.
160
        /// Default constructor.
161
        /// \warning It sets the object to an undefined value.
171 162
        Arc() { }
172 163
        /// Copy constructor.
173 164

	
174 165
        /// Copy constructor.
175 166
        ///
176 167
        Arc(const Arc&) { }
177
        /// Initialize the iterator to be invalid.
168
        /// %Invalid constructor \& conversion.
178 169

	
179
        /// Initialize the iterator to be invalid.
180
        ///
170
        /// Initializes the object to be invalid.
171
        /// \sa Invalid for more details.
181 172
        Arc(Invalid) { }
182 173
        /// Equality operator
183 174

	
175
        /// Equality operator.
176
        ///
184 177
        /// Two iterators are equal if and only if they point to the
185
        /// same object or both are invalid.
178
        /// same object or both are \c INVALID.
186 179
        bool operator==(Arc) const { return true; }
187 180
        /// Inequality operator
188 181

	
189
        /// \sa operator==(Arc n)
190
        ///
182
        /// Inequality operator.
191 183
        bool operator!=(Arc) const { return true; }
192 184

	
193 185
        /// Artificial ordering operator.
194 186

	
195
        /// To allow the use of digraph descriptors as key type in std::map or
196
        /// similar associative container we require this.
187
        /// Artificial ordering operator.
197 188
        ///
198
        /// \note This operator only have to define some strict ordering of
199
        /// the items; this order has nothing to do with the iteration
200
        /// ordering of the items.
189
        /// \note This operator only has to define some strict ordering of
190
        /// the arcs; this order has nothing to do with the iteration
191
        /// ordering of the arcs.
201 192
        bool operator<(Arc) const { return false; }
202 193
      };
203 194

	
204
      /// This iterator goes trough the outgoing arcs of a node.
195
      /// Iterator class for the outgoing arcs of a node.
205 196

	
206 197
      /// This iterator goes trough the \e outgoing arcs of a certain node
207 198
      /// of a digraph.
208 199
      /// Its usage is quite simple, for example you can count the number
209 200
      /// of outgoing arcs of a node \c n
210
      /// in digraph \c g of type \c Digraph as follows.
201
      /// in a digraph \c g of type \c %Digraph as follows.
211 202
      ///\code
212 203
      /// int count=0;
213
      /// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
204
      /// for (Digraph::OutArcIt a(g, n); a!=INVALID; ++a) ++count;
214 205
      ///\endcode
215

	
216 206
      class OutArcIt : public Arc {
217 207
      public:
218 208
        /// Default constructor
219 209

	
220
        /// @warning The default constructor sets the iterator
221
        /// to an undefined value.
210
        /// Default constructor.
211
        /// \warning It sets the iterator to an undefined value.
222 212
        OutArcIt() { }
223 213
        /// Copy constructor.
224 214

	
225 215
        /// Copy constructor.
226 216
        ///
227 217
        OutArcIt(const OutArcIt& e) : Arc(e) { }
228
        /// Initialize the iterator to be invalid.
218
        /// %Invalid constructor \& conversion.
229 219

	
230
        /// Initialize the iterator to be invalid.
220
        /// Initializes the iterator to be invalid.
221
        /// \sa Invalid for more details.
222
        OutArcIt(Invalid) { }
223
        /// Sets the iterator to the first outgoing arc.
224

	
225
        /// Sets the iterator to the first outgoing arc of the given node.
231 226
        ///
232
        OutArcIt(Invalid) { }
233
        /// This constructor sets the iterator to the first outgoing arc.
227
        OutArcIt(const Digraph&, const Node&) { }
228
        /// Sets the iterator to the given arc.
234 229

	
235
        /// This constructor sets the iterator to the first outgoing arc of
236
        /// the node.
237
        OutArcIt(const Digraph&, const Node&) { }
238
        /// Arc -> OutArcIt conversion
239

	
240
        /// Sets the iterator to the value of the trivial iterator.
241
        /// This feature necessitates that each time we
242
        /// iterate the arc-set, the iteration order is the same.
230
        /// Sets the iterator to the given arc of the given digraph.
231
        ///
243 232
        OutArcIt(const Digraph&, const Arc&) { }
244
        ///Next outgoing arc
233
        /// Next outgoing arc
245 234

	
246 235
        /// Assign the iterator to the next
247 236
        /// outgoing arc of the corresponding node.
248 237
        OutArcIt& operator++() { return *this; }
249 238
      };
250 239

	
251
      /// This iterator goes trough the incoming arcs of a node.
240
      /// Iterator class for the incoming arcs of a node.
252 241

	
253 242
      /// This iterator goes trough the \e incoming arcs of a certain node
254 243
      /// of a digraph.
255 244
      /// Its usage is quite simple, for example you can count the number
256
      /// of outgoing arcs of a node \c n
257
      /// in digraph \c g of type \c Digraph as follows.
245
      /// of incoming arcs of a node \c n
246
      /// in a digraph \c g of type \c %Digraph as follows.
258 247
      ///\code
259 248
      /// int count=0;
260
      /// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
249
      /// for(Digraph::InArcIt a(g, n); a!=INVALID; ++a) ++count;
261 250
      ///\endcode
262

	
263 251
      class InArcIt : public Arc {
264 252
      public:
265 253
        /// Default constructor
266 254

	
267
        /// @warning The default constructor sets the iterator
268
        /// to an undefined value.
255
        /// Default constructor.
256
        /// \warning It sets the iterator to an undefined value.
269 257
        InArcIt() { }
270 258
        /// Copy constructor.
271 259

	
272 260
        /// Copy constructor.
273 261
        ///
274 262
        InArcIt(const InArcIt& e) : Arc(e) { }
275
        /// Initialize the iterator to be invalid.
263
        /// %Invalid constructor \& conversion.
276 264

	
277
        /// Initialize the iterator to be invalid.
265
        /// Initializes the iterator to be invalid.
266
        /// \sa Invalid for more details.
267
        InArcIt(Invalid) { }
268
        /// Sets the iterator to the first incoming arc.
269

	
270
        /// Sets the iterator to the first incoming arc of the given node.
278 271
        ///
279
        InArcIt(Invalid) { }
280
        /// This constructor sets the iterator to first incoming arc.
272
        InArcIt(const Digraph&, const Node&) { }
273
        /// Sets the iterator to the given arc.
281 274

	
282
        /// This constructor set the iterator to the first incoming arc of
283
        /// the node.
284
        InArcIt(const Digraph&, const Node&) { }
285
        /// Arc -> InArcIt conversion
286

	
287
        /// Sets the iterator to the value of the trivial iterator \c e.
288
        /// This feature necessitates that each time we
289
        /// iterate the arc-set, the iteration order is the same.
275
        /// Sets the iterator to the given arc of the given digraph.
276
        ///
290 277
        InArcIt(const Digraph&, const Arc&) { }
291 278
        /// Next incoming arc
292 279

	
293
        /// Assign the iterator to the next inarc of the corresponding node.
294
        ///
280
        /// Assign the iterator to the next
281
        /// incoming arc of the corresponding node.
295 282
        InArcIt& operator++() { return *this; }
296 283
      };
297
      /// This iterator goes through each arc.
298 284

	
299
      /// This iterator goes through each arc of a digraph.
285
      /// Iterator class for the arcs.
286

	
287
      /// This iterator goes through each arc of the digraph.
300 288
      /// Its usage is quite simple, for example you can count the number
301
      /// of arcs in a digraph \c g of type \c Digraph as follows:
289
      /// of arcs in a digraph \c g of type \c %Digraph as follows:
302 290
      ///\code
303 291
      /// int count=0;
304
      /// for(Digraph::ArcIt e(g); e!=INVALID; ++e) ++count;
292
      /// for(Digraph::ArcIt a(g); a!=INVALID; ++a) ++count;
305 293
      ///\endcode
306 294
      class ArcIt : public Arc {
307 295
      public:
308 296
        /// Default constructor
309 297

	
310
        /// @warning The default constructor sets the iterator
311
        /// to an undefined value.
298
        /// Default constructor.
299
        /// \warning It sets the iterator to an undefined value.
312 300
        ArcIt() { }
313 301
        /// Copy constructor.
314 302

	
315 303
        /// Copy constructor.
316 304
        ///
317 305
        ArcIt(const ArcIt& e) : Arc(e) { }
318
        /// Initialize the iterator to be invalid.
306
        /// %Invalid constructor \& conversion.
319 307

	
320
        /// Initialize the iterator to be invalid.
308
        /// Initializes the iterator to be invalid.
309
        /// \sa Invalid for more details.
310
        ArcIt(Invalid) { }
311
        /// Sets the iterator to the first arc.
312

	
313
        /// Sets the iterator to the first arc of the given digraph.
321 314
        ///
322
        ArcIt(Invalid) { }
323
        /// This constructor sets the iterator to the first arc.
315
        explicit ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); }
316
        /// Sets the iterator to the given arc.
324 317

	
325
        /// This constructor sets the iterator to the first arc of \c g.
326
        ///@param g the digraph
327
        ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); }
328
        /// Arc -> ArcIt conversion
329

	
330
        /// Sets the iterator to the value of the trivial iterator \c e.
331
        /// This feature necessitates that each time we
332
        /// iterate the arc-set, the iteration order is the same.
318
        /// Sets the iterator to the given arc of the given digraph.
319
        ///
333 320
        ArcIt(const Digraph&, const Arc&) { }
334
        ///Next arc
321
        /// Next arc
335 322

	
336 323
        /// Assign the iterator to the next arc.
324
        ///
337 325
        ArcIt& operator++() { return *this; }
338 326
      };
339
      ///Gives back the target node of an arc.
340 327

	
341
      ///Gives back the target node of an arc.
328
      /// \brief The source node of the arc.
342 329
      ///
343
      Node target(Arc) const { return INVALID; }
344
      ///Gives back the source node of an arc.
345

	
346
      ///Gives back the source node of an arc.
347
      ///
330
      /// Returns the source node of the given arc.
348 331
      Node source(Arc) const { return INVALID; }
349 332

	
350
      /// \brief Returns the ID of the node.
333
      /// \brief The target node of the arc.
334
      ///
335
      /// Returns the target node of the given arc.
336
      Node target(Arc) const { return INVALID; }
337

	
338
      /// \brief The ID of the node.
339
      ///
340
      /// Returns the ID of the given node.
351 341
      int id(Node) const { return -1; }
352 342

	
353
      /// \brief Returns the ID of the arc.
343
      /// \brief The ID of the arc.
344
      ///
345
      /// Returns the ID of the given arc.
354 346
      int id(Arc) const { return -1; }
355 347

	
356
      /// \brief Returns the node with the given ID.
348
      /// \brief The node with the given ID.
357 349
      ///
358
      /// \pre The argument should be a valid node ID in the graph.
350
      /// Returns the node with the given ID.
351
      /// \pre The argument should be a valid node ID in the digraph.
359 352
      Node nodeFromId(int) const { return INVALID; }
360 353

	
361
      /// \brief Returns the arc with the given ID.
354
      /// \brief The arc with the given ID.
362 355
      ///
363
      /// \pre The argument should be a valid arc ID in the graph.
356
      /// Returns the arc with the given ID.
357
      /// \pre The argument should be a valid arc ID in the digraph.
364 358
      Arc arcFromId(int) const { return INVALID; }
365 359

	
366
      /// \brief Returns an upper bound on the node IDs.
360
      /// \brief An upper bound on the node IDs.
361
      ///
362
      /// Returns an upper bound on the node IDs.
367 363
      int maxNodeId() const { return -1; }
368 364

	
369
      /// \brief Returns an upper bound on the arc IDs.
365
      /// \brief An upper bound on the arc IDs.
366
      ///
367
      /// Returns an upper bound on the arc IDs.
370 368
      int maxArcId() const { return -1; }
371 369

	
372 370
      void first(Node&) const {}
373 371
      void next(Node&) const {}
374 372

	
375 373
      void first(Arc&) const {}
376 374
      void next(Arc&) const {}
377 375

	
378 376

	
379 377
      void firstIn(Arc&, const Node&) const {}
380 378
      void nextIn(Arc&) const {}
381 379

	
382 380
      void firstOut(Arc&, const Node&) const {}
383 381
      void nextOut(Arc&) const {}
384 382

	
385 383
      // The second parameter is dummy.
386 384
      Node fromId(int, Node) const { return INVALID; }
387 385
      // The second parameter is dummy.
388 386
      Arc fromId(int, Arc) const { return INVALID; }
389 387

	
390 388
      // Dummy parameter.
391 389
      int maxId(Node) const { return -1; }
392 390
      // Dummy parameter.
393 391
      int maxId(Arc) const { return -1; }
394 392

	
393
      /// \brief The opposite node on the arc.
394
      ///
395
      /// Returns the opposite node on the given arc.
396
      Node oppositeNode(Node, Arc) const { return INVALID; }
397

	
395 398
      /// \brief The base node of the iterator.
396 399
      ///
397
      /// Gives back the base node of the iterator.
398
      /// It is always the target of the pointed arc.
399
      Node baseNode(const InArcIt&) const { return INVALID; }
400
      /// Returns the base node of the given outgoing arc iterator
401
      /// (i.e. the source node of the corresponding arc).
402
      Node baseNode(OutArcIt) const { return INVALID; }
400 403

	
401 404
      /// \brief The running node of the iterator.
402 405
      ///
403
      /// Gives back the running node of the iterator.
404
      /// It is always the source of the pointed arc.
405
      Node runningNode(const InArcIt&) const { return INVALID; }
406
      /// Returns the running node of the given outgoing arc iterator
407
      /// (i.e. the target node of the corresponding arc).
408
      Node runningNode(OutArcIt) const { return INVALID; }
406 409

	
407 410
      /// \brief The base node of the iterator.
408 411
      ///
409
      /// Gives back the base node of the iterator.
410
      /// It is always the source of the pointed arc.
411
      Node baseNode(const OutArcIt&) const { return INVALID; }
412
      /// Returns the base node of the given incomming arc iterator
413
      /// (i.e. the target node of the corresponding arc).
414
      Node baseNode(InArcIt) const { return INVALID; }
412 415

	
413 416
      /// \brief The running node of the iterator.
414 417
      ///
415
      /// Gives back the running node of the iterator.
416
      /// It is always the target of the pointed arc.
417
      Node runningNode(const OutArcIt&) const { return INVALID; }
418
      /// Returns the running node of the given incomming arc iterator
419
      /// (i.e. the source node of the corresponding arc).
420
      Node runningNode(InArcIt) const { return INVALID; }
418 421

	
419
      /// \brief The opposite node on the given arc.
422
      /// \brief Standard graph map type for the nodes.
420 423
      ///
421
      /// Gives back the opposite node on the given arc.
422
      Node oppositeNode(const Node&, const Arc&) const { return INVALID; }
423

	
424
      /// \brief Read write map of the nodes to type \c T.
425
      ///
426
      /// ReadWrite map of the nodes to type \c T.
427
      /// \sa Reference
424
      /// Standard graph map type for the nodes.
425
      /// It conforms to the ReferenceMap concept.
428 426
      template<class T>
429
      class NodeMap : public ReadWriteMap< Node, T > {
427
      class NodeMap : public ReferenceMap<Node, T, T&, const T&> {
430 428
      public:
431 429

	
432
        ///\e
433
        NodeMap(const Digraph&) { }
434
        ///\e
430
        /// Constructor
431
        explicit NodeMap(const Digraph&) { }
432
        /// Constructor with given initial value
435 433
        NodeMap(const Digraph&, T) { }
436 434

	
437 435
      private:
438 436
        ///Copy constructor
439
        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
437
        NodeMap(const NodeMap& nm) : 
438
          ReferenceMap<Node, T, T&, const T&>(nm) { }
440 439
        ///Assignment operator
441 440
        template <typename CMap>
442 441
        NodeMap& operator=(const CMap&) {
443 442
          checkConcept<ReadMap<Node, T>, CMap>();
444 443
          return *this;
445 444
        }
446 445
      };
447 446

	
448
      /// \brief Read write map of the arcs to type \c T.
447
      /// \brief Standard graph map type for the arcs.
449 448
      ///
450
      /// Reference map of the arcs to type \c T.
451
      /// \sa Reference
449
      /// Standard graph map type for the arcs.
450
      /// It conforms to the ReferenceMap concept.
452 451
      template<class T>
453
      class ArcMap : public ReadWriteMap<Arc,T> {
452
      class ArcMap : public ReferenceMap<Arc, T, T&, const T&> {
454 453
      public:
455 454

	
456
        ///\e
457
        ArcMap(const Digraph&) { }
458
        ///\e
455
        /// Constructor
456
        explicit ArcMap(const Digraph&) { }
457
        /// Constructor with given initial value
459 458
        ArcMap(const Digraph&, T) { }
459

	
460 460
      private:
461 461
        ///Copy constructor
462
        ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
462
        ArcMap(const ArcMap& em) :
463
          ReferenceMap<Arc, T, T&, const T&>(em) { }
463 464
        ///Assignment operator
464 465
        template <typename CMap>
465 466
        ArcMap& operator=(const CMap&) {
466 467
          checkConcept<ReadMap<Arc, T>, CMap>();
467 468
          return *this;
468 469
        }
469 470
      };
470 471

	
471 472
      template <typename _Digraph>
472 473
      struct Constraints {
473 474
        void constraints() {
475
          checkConcept<BaseDigraphComponent, _Digraph>();
474 476
          checkConcept<IterableDigraphComponent<>, _Digraph>();
475 477
          checkConcept<IDableDigraphComponent<>, _Digraph>();
476 478
          checkConcept<MappableDigraphComponent<>, _Digraph>();
477 479
        }
478 480
      };
479 481

	
480 482
    };
481 483

	
482 484
  } //namespace concepts
483 485
} //namespace lemon
484 486

	
485 487

	
486 488

	
487
#endif // LEMON_CONCEPT_DIGRAPH_H
489
#endif
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup graph_concepts
20 20
///\file
21
///\brief The concept of Undirected Graphs.
21
///\brief The concept of undirected graphs.
22 22

	
23
#ifndef LEMON_CONCEPT_GRAPH_H
24
#define LEMON_CONCEPT_GRAPH_H
23
#ifndef LEMON_CONCEPTS_GRAPH_H
24
#define LEMON_CONCEPTS_GRAPH_H
25 25

	
26 26
#include <lemon/concepts/graph_components.h>
27
#include <lemon/concepts/graph.h>
27
#include <lemon/concepts/maps.h>
28
#include <lemon/concept_check.h>
28 29
#include <lemon/core.h>
29 30

	
30 31
namespace lemon {
31 32
  namespace concepts {
32 33

	
33 34
    /// \ingroup graph_concepts
34 35
    ///
35
    /// \brief Class describing the concept of Undirected Graphs.
36
    /// \brief Class describing the concept of undirected graphs.
36 37
    ///
37
    /// This class describes the common interface of all Undirected
38
    /// Graphs.
38
    /// This class describes the common interface of all undirected
39
    /// graphs.
39 40
    ///
40
    /// As all concept describing classes it provides only interface
41
    /// without any sensible implementation. So any algorithm for
42
    /// undirected graph should compile with this class, but it will not
41
    /// Like all concept classes, it only provides an interface
42
    /// without any sensible implementation. So any general algorithm for
43
    /// undirected graphs should compile with this class, but it will not
43 44
    /// run properly, of course.
45
    /// An actual graph implementation like \ref ListGraph or
46
    /// \ref SmartGraph may have additional functionality.    
44 47
    ///
45
    /// The LEMON undirected graphs also fulfill the concept of
46
    /// directed graphs (\ref lemon::concepts::Digraph "Digraph
47
    /// Concept"). Each edges can be seen as two opposite
48
    /// directed arc and consequently the undirected graph can be
49
    /// seen as the direceted graph of these directed arcs. The
50
    /// Graph has the Edge inner class for the edges and
51
    /// the Arc type for the directed arcs. The Arc type is
52
    /// convertible to Edge or inherited from it so from a directed
53
    /// arc we can get the represented edge.
48
    /// The undirected graphs also fulfill the concept of \ref Digraph
49
    /// "directed graphs", since each edge can also be regarded as two
50
    /// oppositely directed arcs.
51
    /// Undirected graphs provide an Edge type for the undirected edges and
52
    /// an Arc type for the directed arcs. The Arc type is convertible to
53
    /// Edge or inherited from it, i.e. the corresponding edge can be
54
    /// obtained from an arc.
55
    /// EdgeIt and EdgeMap classes can be used for the edges, while ArcIt
56
    /// and ArcMap classes can be used for the arcs (just like in digraphs).
57
    /// Both InArcIt and OutArcIt iterates on the same edges but with
58
    /// opposite direction. IncEdgeIt also iterates on the same edges
59
    /// as OutArcIt and InArcIt, but it is not convertible to Arc,
60
    /// only to Edge.
54 61
    ///
55
    /// In the sense of the LEMON each edge has a default
56
    /// direction (it should be in every computer implementation,
57
    /// because the order of edge's nodes defines an
58
    /// orientation). With the default orientation we can define that
59
    /// the directed arc is forward or backward directed. With the \c
60
    /// direction() and \c direct() function we can get the direction
61
    /// of the directed arc and we can direct an edge.
62
    /// In LEMON, each undirected edge has an inherent orientation.
63
    /// Thus it can defined if an arc is forward or backward oriented in
64
    /// an undirected graph with respect to this default oriantation of
65
    /// the represented edge.
66
    /// With the direction() and direct() functions the direction
67
    /// of an arc can be obtained and set, respectively.
62 68
    ///
63
    /// The EdgeIt is an iterator for the edges. We can use
64
    /// the EdgeMap to map values for the edges. The InArcIt and
65
    /// OutArcIt iterates on the same edges but with opposite
66
    /// direction. The IncEdgeIt iterates also on the same edges
67
    /// as the OutArcIt and InArcIt but it is not convertible to Arc just
68
    /// to Edge.
69
    /// Only nodes and edges can be added to or removed from an undirected
70
    /// graph and the corresponding arcs are added or removed automatically.
71
    ///
72
    /// \sa Digraph
69 73
    class Graph {
74
    private:
75
      /// Graphs are \e not copy constructible. Use DigraphCopy instead.
76
      Graph(const Graph&) {}
77
      /// \brief Assignment of a graph to another one is \e not allowed.
78
      /// Use DigraphCopy instead.
79
      void operator=(const Graph&) {}
80

	
70 81
    public:
71
      /// \brief The undirected graph should be tagged by the
72
      /// UndirectedTag.
82
      /// Default constructor.
83
      Graph() {}
84

	
85
      /// \brief Undirected graphs should be tagged with \c UndirectedTag.
73 86
      ///
74
      /// The undirected graph should be tagged by the UndirectedTag. This
75
      /// tag helps the enable_if technics to make compile time
87
      /// Undirected graphs should be tagged with \c UndirectedTag.
88
      /// 
89
      /// This tag helps the \c enable_if technics to make compile time
76 90
      /// specializations for undirected graphs.
77 91
      typedef True UndirectedTag;
78 92

	
79
      /// \brief The base type of node iterators,
80
      /// or in other words, the trivial node iterator.
81
      ///
82
      /// This is the base type of each node iterator,
83
      /// thus each kind of node iterator converts to this.
84
      /// More precisely each kind of node iterator should be inherited
85
      /// from the trivial node iterator.
93
      /// The node type of the graph
94

	
95
      /// This class identifies a node of the graph. It also serves
96
      /// as a base class of the node iterators,
97
      /// thus they convert to this type.
86 98
      class Node {
87 99
      public:
88 100
        /// Default constructor
89 101

	
90
        /// @warning The default constructor sets the iterator
91
        /// to an undefined value.
102
        /// Default constructor.
103
        /// \warning It sets the object to an undefined value.
92 104
        Node() { }
93 105
        /// Copy constructor.
94 106

	
95 107
        /// Copy constructor.
96 108
        ///
97 109
        Node(const Node&) { }
98 110

	
99
        /// Invalid constructor \& conversion.
111
        /// %Invalid constructor \& conversion.
100 112

	
101
        /// This constructor initializes the iterator to be invalid.
113
        /// Initializes the object to be invalid.
102 114
        /// \sa Invalid for more details.
103 115
        Node(Invalid) { }
104 116
        /// Equality operator
105 117

	
118
        /// Equality operator.
119
        ///
106 120
        /// Two iterators are equal if and only if they point to the
107
        /// same object or both are invalid.
121
        /// same object or both are \c INVALID.
108 122
        bool operator==(Node) const { return true; }
109 123

	
110 124
        /// Inequality operator
111 125

	
112
        /// \sa operator==(Node n)
113
        ///
126
        /// Inequality operator.
114 127
        bool operator!=(Node) const { return true; }
115 128

	
116 129
        /// Artificial ordering operator.
117 130

	
118
        /// To allow the use of graph descriptors as key type in std::map or
119
        /// similar associative container we require this.
131
        /// Artificial ordering operator.
120 132
        ///
121
        /// \note This operator only have to define some strict ordering of
133
        /// \note This operator only has to define some strict ordering of
122 134
        /// the items; this order has nothing to do with the iteration
123 135
        /// ordering of the items.
124 136
        bool operator<(Node) const { return false; }
125 137

	
126 138
      };
127 139

	
128
      /// This iterator goes through each node.
140
      /// Iterator class for the nodes.
129 141

	
130
      /// This iterator goes through each node.
142
      /// This iterator goes through each node of the graph.
131 143
      /// Its usage is quite simple, for example you can count the number
132
      /// of nodes in graph \c g of type \c Graph like this:
144
      /// of nodes in a graph \c g of type \c %Graph like this:
133 145
      ///\code
134 146
      /// int count=0;
135 147
      /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
136 148
      ///\endcode
137 149
      class NodeIt : public Node {
138 150
      public:
139 151
        /// Default constructor
140 152

	
141
        /// @warning The default constructor sets the iterator
142
        /// to an undefined value.
153
        /// Default constructor.
154
        /// \warning It sets the iterator to an undefined value.
143 155
        NodeIt() { }
144 156
        /// Copy constructor.
145 157

	
146 158
        /// Copy constructor.
147 159
        ///
148 160
        NodeIt(const NodeIt& n) : Node(n) { }
149
        /// Invalid constructor \& conversion.
161
        /// %Invalid constructor \& conversion.
150 162

	
151
        /// Initialize the iterator to be invalid.
163
        /// Initializes the iterator to be invalid.
152 164
        /// \sa Invalid for more details.
153 165
        NodeIt(Invalid) { }
154 166
        /// Sets the iterator to the first node.
155 167

	
156
        /// Sets the iterator to the first node of \c g.
168
        /// Sets the iterator to the first node of the given digraph.
157 169
        ///
158
        NodeIt(const Graph&) { }
159
        /// Node -> NodeIt conversion.
170
        explicit NodeIt(const Graph&) { }
171
        /// Sets the iterator to the given node.
160 172

	
161
        /// Sets the iterator to the node of \c the graph pointed by
162
        /// the trivial iterator.
163
        /// This feature necessitates that each time we
164
        /// iterate the arc-set, the iteration order is the same.
173
        /// Sets the iterator to the given node of the given digraph.
174
        ///
165 175
        NodeIt(const Graph&, const Node&) { }
166 176
        /// Next node.
167 177

	
168 178
        /// Assign the iterator to the next node.
169 179
        ///
170 180
        NodeIt& operator++() { return *this; }
171 181
      };
172 182

	
173 183

	
174
      /// The base type of the edge iterators.
184
      /// The edge type of the graph
175 185

	
176
      /// The base type of the edge iterators.
177
      ///
186
      /// This class identifies an edge of the graph. It also serves
187
      /// as a base class of the edge iterators,
188
      /// thus they will convert to this type.
178 189
      class Edge {
179 190
      public:
180 191
        /// Default constructor
181 192

	
182
        /// @warning The default constructor sets the iterator
183
        /// to an undefined value.
193
        /// Default constructor.
194
        /// \warning It sets the object to an undefined value.
184 195
        Edge() { }
185 196
        /// Copy constructor.
186 197

	
187 198
        /// Copy constructor.
188 199
        ///
189 200
        Edge(const Edge&) { }
190
        /// Initialize the iterator to be invalid.
201
        /// %Invalid constructor \& conversion.
191 202

	
192
        /// Initialize the iterator to be invalid.
193
        ///
203
        /// Initializes the object to be invalid.
204
        /// \sa Invalid for more details.
194 205
        Edge(Invalid) { }
195 206
        /// Equality operator
196 207

	
208
        /// Equality operator.
209
        ///
197 210
        /// Two iterators are equal if and only if they point to the
198
        /// same object or both are invalid.
211
        /// same object or both are \c INVALID.
199 212
        bool operator==(Edge) const { return true; }
200 213
        /// Inequality operator
201 214

	
202
        /// \sa operator==(Edge n)
203
        ///
215
        /// Inequality operator.
204 216
        bool operator!=(Edge) const { return true; }
205 217

	
206 218
        /// Artificial ordering operator.
207 219

	
208
        /// To allow the use of graph descriptors as key type in std::map or
209
        /// similar associative container we require this.
220
        /// Artificial ordering operator.
210 221
        ///
211
        /// \note This operator only have to define some strict ordering of
212
        /// the items; this order has nothing to do with the iteration
213
        /// ordering of the items.
222
        /// \note This operator only has to define some strict ordering of
223
        /// the edges; this order has nothing to do with the iteration
224
        /// ordering of the edges.
214 225
        bool operator<(Edge) const { return false; }
215 226
      };
216 227

	
217
      /// This iterator goes through each edge.
228
      /// Iterator class for the edges.
218 229

	
219
      /// This iterator goes through each edge of a graph.
230
      /// This iterator goes through each edge of the graph.
220 231
      /// Its usage is quite simple, for example you can count the number
221
      /// of edges in a graph \c g of type \c Graph as follows:
232
      /// of edges in a graph \c g of type \c %Graph as follows:
222 233
      ///\code
223 234
      /// int count=0;
224 235
      /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
225 236
      ///\endcode
226 237
      class EdgeIt : public Edge {
227 238
      public:
228 239
        /// Default constructor
229 240

	
230
        /// @warning The default constructor sets the iterator
231
        /// to an undefined value.
241
        /// Default constructor.
242
        /// \warning It sets the iterator to an undefined value.
232 243
        EdgeIt() { }
233 244
        /// Copy constructor.
234 245

	
235 246
        /// Copy constructor.
236 247
        ///
237 248
        EdgeIt(const EdgeIt& e) : Edge(e) { }
238
        /// Initialize the iterator to be invalid.
249
        /// %Invalid constructor \& conversion.
239 250

	
240
        /// Initialize the iterator to be invalid.
251
        /// Initializes the iterator to be invalid.
252
        /// \sa Invalid for more details.
253
        EdgeIt(Invalid) { }
254
        /// Sets the iterator to the first edge.
255

	
256
        /// Sets the iterator to the first edge of the given graph.
241 257
        ///
242
        EdgeIt(Invalid) { }
243
        /// This constructor sets the iterator to the first edge.
258
        explicit EdgeIt(const Graph&) { }
259
        /// Sets the iterator to the given edge.
244 260

	
245
        /// This constructor sets the iterator to the first edge.
246
        EdgeIt(const Graph&) { }
247
        /// Edge -> EdgeIt conversion
248

	
249
        /// Sets the iterator to the value of the trivial iterator.
250
        /// This feature necessitates that each time we
251
        /// iterate the edge-set, the iteration order is the
252
        /// same.
261
        /// Sets the iterator to the given edge of the given graph.
262
        ///
253 263
        EdgeIt(const Graph&, const Edge&) { }
254 264
        /// Next edge
255 265

	
256 266
        /// Assign the iterator to the next edge.
267
        ///
257 268
        EdgeIt& operator++() { return *this; }
258 269
      };
259 270

	
260
      /// \brief This iterator goes trough the incident undirected
261
      /// arcs of a node.
262
      ///
263
      /// This iterator goes trough the incident edges
264
      /// of a certain node of a graph. You should assume that the
265
      /// loop arcs will be iterated twice.
266
      ///
271
      /// Iterator class for the incident edges of a node.
272

	
273
      /// This iterator goes trough the incident undirected edges
274
      /// of a certain node of a graph.
267 275
      /// Its usage is quite simple, for example you can compute the
268
      /// degree (i.e. count the number of incident arcs of a node \c n
269
      /// in graph \c g of type \c Graph as follows.
276
      /// degree (i.e. the number of incident edges) of a node \c n
277
      /// in a graph \c g of type \c %Graph as follows.
270 278
      ///
271 279
      ///\code
272 280
      /// int count=0;
273 281
      /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
274 282
      ///\endcode
283
      ///
284
      /// \warning Loop edges will be iterated twice.
275 285
      class IncEdgeIt : public Edge {
276 286
      public:
277 287
        /// Default constructor
278 288

	
279
        /// @warning The default constructor sets the iterator
280
        /// to an undefined value.
289
        /// Default constructor.
290
        /// \warning It sets the iterator to an undefined value.
281 291
        IncEdgeIt() { }
282 292
        /// Copy constructor.
283 293

	
284 294
        /// Copy constructor.
285 295
        ///
286 296
        IncEdgeIt(const IncEdgeIt& e) : Edge(e) { }
287
        /// Initialize the iterator to be invalid.
297
        /// %Invalid constructor \& conversion.
288 298

	
289
        /// Initialize the iterator to be invalid.
299
        /// Initializes the iterator to be invalid.
300
        /// \sa Invalid for more details.
301
        IncEdgeIt(Invalid) { }
302
        /// Sets the iterator to the first incident edge.
303

	
304
        /// Sets the iterator to the first incident edge of the given node.
290 305
        ///
291
        IncEdgeIt(Invalid) { }
292
        /// This constructor sets the iterator to first incident arc.
306
        IncEdgeIt(const Graph&, const Node&) { }
307
        /// Sets the iterator to the given edge.
293 308

	
294
        /// This constructor set the iterator to the first incident arc of
295
        /// the node.
296
        IncEdgeIt(const Graph&, const Node&) { }
297
        /// Edge -> IncEdgeIt conversion
309
        /// Sets the iterator to the given edge of the given graph.
310
        ///
311
        IncEdgeIt(const Graph&, const Edge&) { }
312
        /// Next incident edge
298 313

	
299
        /// Sets the iterator to the value of the trivial iterator \c e.
300
        /// This feature necessitates that each time we
301
        /// iterate the arc-set, the iteration order is the same.
302
        IncEdgeIt(const Graph&, const Edge&) { }
303
        /// Next incident arc
304

	
305
        /// Assign the iterator to the next incident arc
314
        /// Assign the iterator to the next incident edge
306 315
        /// of the corresponding node.
307 316
        IncEdgeIt& operator++() { return *this; }
308 317
      };
309 318

	
310
      /// The directed arc type.
319
      /// The arc type of the graph
311 320

	
312
      /// The directed arc type. It can be converted to the
313
      /// edge or it should be inherited from the undirected
314
      /// arc.
315
      class Arc : public Edge {
321
      /// This class identifies a directed arc of the graph. It also serves
322
      /// as a base class of the arc iterators,
323
      /// thus they will convert to this type.
324
      class Arc {
316 325
      public:
317 326
        /// Default constructor
318 327

	
319
        /// @warning The default constructor sets the iterator
320
        /// to an undefined value.
328
        /// Default constructor.
329
        /// \warning It sets the object to an undefined value.
321 330
        Arc() { }
322 331
        /// Copy constructor.
323 332

	
324 333
        /// Copy constructor.
325 334
        ///
326
        Arc(const Arc& e) : Edge(e) { }
327
        /// Initialize the iterator to be invalid.
335
        Arc(const Arc&) { }
336
        /// %Invalid constructor \& conversion.
328 337

	
329
        /// Initialize the iterator to be invalid.
330
        ///
338
        /// Initializes the object to be invalid.
339
        /// \sa Invalid for more details.
331 340
        Arc(Invalid) { }
332 341
        /// Equality operator
333 342

	
343
        /// Equality operator.
344
        ///
334 345
        /// Two iterators are equal if and only if they point to the
335
        /// same object or both are invalid.
346
        /// same object or both are \c INVALID.
336 347
        bool operator==(Arc) const { return true; }
337 348
        /// Inequality operator
338 349

	
339
        /// \sa operator==(Arc n)
340
        ///
350
        /// Inequality operator.
341 351
        bool operator!=(Arc) const { return true; }
342 352

	
343 353
        /// Artificial ordering operator.
344 354

	
345
        /// To allow the use of graph descriptors as key type in std::map or
346
        /// similar associative container we require this.
355
        /// Artificial ordering operator.
347 356
        ///
348
        /// \note This operator only have to define some strict ordering of
349
        /// the items; this order has nothing to do with the iteration
350
        /// ordering of the items.
357
        /// \note This operator only has to define some strict ordering of
358
        /// the arcs; this order has nothing to do with the iteration
359
        /// ordering of the arcs.
351 360
        bool operator<(Arc) const { return false; }
352 361

	
362
        /// Converison to \c Edge
363
        
364
        /// Converison to \c Edge.
365
        ///
366
        operator Edge() const { return Edge(); }
353 367
      };
354
      /// This iterator goes through each directed arc.
355 368

	
356
      /// This iterator goes through each arc of a graph.
369
      /// Iterator class for the arcs.
370

	
371
      /// This iterator goes through each directed arc of the graph.
357 372
      /// Its usage is quite simple, for example you can count the number
358
      /// of arcs in a graph \c g of type \c Graph as follows:
373
      /// of arcs in a graph \c g of type \c %Graph as follows:
359 374
      ///\code
360 375
      /// int count=0;
361
      /// for(Graph::ArcIt e(g); e!=INVALID; ++e) ++count;
376
      /// for(Graph::ArcIt a(g); a!=INVALID; ++a) ++count;
362 377
      ///\endcode
363 378
      class ArcIt : public Arc {
364 379
      public:
365 380
        /// Default constructor
366 381

	
367
        /// @warning The default constructor sets the iterator
368
        /// to an undefined value.
382
        /// Default constructor.
383
        /// \warning It sets the iterator to an undefined value.
369 384
        ArcIt() { }
370 385
        /// Copy constructor.
371 386

	
372 387
        /// Copy constructor.
373 388
        ///
374 389
        ArcIt(const ArcIt& e) : Arc(e) { }
375
        /// Initialize the iterator to be invalid.
390
        /// %Invalid constructor \& conversion.
376 391

	
377
        /// Initialize the iterator to be invalid.
392
        /// Initializes the iterator to be invalid.
393
        /// \sa Invalid for more details.
394
        ArcIt(Invalid) { }
395
        /// Sets the iterator to the first arc.
396

	
397
        /// Sets the iterator to the first arc of the given graph.
378 398
        ///
379
        ArcIt(Invalid) { }
380
        /// This constructor sets the iterator to the first arc.
399
        explicit ArcIt(const Graph &g) { ignore_unused_variable_warning(g); }
400
        /// Sets the iterator to the given arc.
381 401

	
382
        /// This constructor sets the iterator to the first arc of \c g.
383
        ///@param g the graph
384
        ArcIt(const Graph &g) { ignore_unused_variable_warning(g); }
385
        /// Arc -> ArcIt conversion
386

	
387
        /// Sets the iterator to the value of the trivial iterator \c e.
388
        /// This feature necessitates that each time we
389
        /// iterate the arc-set, the iteration order is the same.
402
        /// Sets the iterator to the given arc of the given graph.
403
        ///
390 404
        ArcIt(const Graph&, const Arc&) { }
391
        ///Next arc
405
        /// Next arc
392 406

	
393 407
        /// Assign the iterator to the next arc.
408
        ///
394 409
        ArcIt& operator++() { return *this; }
395 410
      };
396 411

	
397
      /// This iterator goes trough the outgoing directed arcs of a node.
412
      /// Iterator class for the outgoing arcs of a node.
398 413

	
399
      /// This iterator goes trough the \e outgoing arcs of a certain node
400
      /// of a graph.
414
      /// This iterator goes trough the \e outgoing directed arcs of a
415
      /// certain node of a graph.
401 416
      /// Its usage is quite simple, for example you can count the number
402 417
      /// of outgoing arcs of a node \c n
403
      /// in graph \c g of type \c Graph as follows.
418
      /// in a graph \c g of type \c %Graph as follows.
404 419
      ///\code
405 420
      /// int count=0;
406
      /// for (Graph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
421
      /// for (Digraph::OutArcIt a(g, n); a!=INVALID; ++a) ++count;
407 422
      ///\endcode
408

	
409 423
      class OutArcIt : public Arc {
410 424
      public:
411 425
        /// Default constructor
412 426

	
413
        /// @warning The default constructor sets the iterator
414
        /// to an undefined value.
427
        /// Default constructor.
428
        /// \warning It sets the iterator to an undefined value.
415 429
        OutArcIt() { }
416 430
        /// Copy constructor.
417 431

	
418 432
        /// Copy constructor.
419 433
        ///
420 434
        OutArcIt(const OutArcIt& e) : Arc(e) { }
421
        /// Initialize the iterator to be invalid.
435
        /// %Invalid constructor \& conversion.
422 436

	
423
        /// Initialize the iterator to be invalid.
437
        /// Initializes the iterator to be invalid.
438
        /// \sa Invalid for more details.
439
        OutArcIt(Invalid) { }
440
        /// Sets the iterator to the first outgoing arc.
441

	
442
        /// Sets the iterator to the first outgoing arc of the given node.
424 443
        ///
425
        OutArcIt(Invalid) { }
426
        /// This constructor sets the iterator to the first outgoing arc.
427

	
428
        /// This constructor sets the iterator to the first outgoing arc of
429
        /// the node.
430
        ///@param n the node
431
        ///@param g the graph
432 444
        OutArcIt(const Graph& n, const Node& g) {
433 445
          ignore_unused_variable_warning(n);
434 446
          ignore_unused_variable_warning(g);
435 447
        }
436
        /// Arc -> OutArcIt conversion
448
        /// Sets the iterator to the given arc.
437 449

	
438
        /// Sets the iterator to the value of the trivial iterator.
439
        /// This feature necessitates that each time we
440
        /// iterate the arc-set, the iteration order is the same.
450
        /// Sets the iterator to the given arc of the given graph.
451
        ///
441 452
        OutArcIt(const Graph&, const Arc&) { }
442
        ///Next outgoing arc
453
        /// Next outgoing arc
443 454

	
444 455
        /// Assign the iterator to the next
445 456
        /// outgoing arc of the corresponding node.
446 457
        OutArcIt& operator++() { return *this; }
447 458
      };
448 459

	
449
      /// This iterator goes trough the incoming directed arcs of a node.
460
      /// Iterator class for the incoming arcs of a node.
450 461

	
451
      /// This iterator goes trough the \e incoming arcs of a certain node
452
      /// of a graph.
462
      /// This iterator goes trough the \e incoming directed arcs of a
463
      /// certain node of a graph.
453 464
      /// Its usage is quite simple, for example you can count the number
454
      /// of outgoing arcs of a node \c n
455
      /// in graph \c g of type \c Graph as follows.
465
      /// of incoming arcs of a node \c n
466
      /// in a graph \c g of type \c %Graph as follows.
456 467
      ///\code
457 468
      /// int count=0;
458
      /// for(Graph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
469
      /// for (Digraph::InArcIt a(g, n); a!=INVALID; ++a) ++count;
459 470
      ///\endcode
460

	
461 471
      class InArcIt : public Arc {
462 472
      public:
463 473
        /// Default constructor
464 474

	
465
        /// @warning The default constructor sets the iterator
466
        /// to an undefined value.
475
        /// Default constructor.
476
        /// \warning It sets the iterator to an undefined value.
467 477
        InArcIt() { }
468 478
        /// Copy constructor.
469 479

	
470 480
        /// Copy constructor.
471 481
        ///
472 482
        InArcIt(const InArcIt& e) : Arc(e) { }
473
        /// Initialize the iterator to be invalid.
483
        /// %Invalid constructor \& conversion.
474 484

	
475
        /// Initialize the iterator to be invalid.
485
        /// Initializes the iterator to be invalid.
486
        /// \sa Invalid for more details.
487
        InArcIt(Invalid) { }
488
        /// Sets the iterator to the first incoming arc.
489

	
490
        /// Sets the iterator to the first incoming arc of the given node.
476 491
        ///
477
        InArcIt(Invalid) { }
478
        /// This constructor sets the iterator to first incoming arc.
479

	
480
        /// This constructor set the iterator to the first incoming arc of
481
        /// the node.
482
        ///@param n the node
483
        ///@param g the graph
484 492
        InArcIt(const Graph& g, const Node& n) {
485 493
          ignore_unused_variable_warning(n);
486 494
          ignore_unused_variable_warning(g);
487 495
        }
488
        /// Arc -> InArcIt conversion
496
        /// Sets the iterator to the given arc.
489 497

	
490
        /// Sets the iterator to the value of the trivial iterator \c e.
491
        /// This feature necessitates that each time we
492
        /// iterate the arc-set, the iteration order is the same.
498
        /// Sets the iterator to the given arc of the given graph.
499
        ///
493 500
        InArcIt(const Graph&, const Arc&) { }
494 501
        /// Next incoming arc
495 502

	
496
        /// Assign the iterator to the next inarc of the corresponding node.
497
        ///
503
        /// Assign the iterator to the next
504
        /// incoming arc of the corresponding node.
498 505
        InArcIt& operator++() { return *this; }
499 506
      };
500 507

	
501
      /// \brief Read write map of the nodes to type \c T.
508
      /// \brief Standard graph map type for the nodes.
502 509
      ///
503
      /// ReadWrite map of the nodes to type \c T.
504
      /// \sa Reference
510
      /// Standard graph map type for the nodes.
511
      /// It conforms to the ReferenceMap concept.
505 512
      template<class T>
506
      class NodeMap : public ReadWriteMap< Node, T >
513
      class NodeMap : public ReferenceMap<Node, T, T&, const T&>
507 514
      {
508 515
      public:
509 516

	
510
        ///\e
511
        NodeMap(const Graph&) { }
512
        ///\e
517
        /// Constructor
518
        explicit NodeMap(const Graph&) { }
519
        /// Constructor with given initial value
513 520
        NodeMap(const Graph&, T) { }
514 521

	
515 522
      private:
516 523
        ///Copy constructor
517
        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
524
        NodeMap(const NodeMap& nm) :
525
          ReferenceMap<Node, T, T&, const T&>(nm) { }
518 526
        ///Assignment operator
519 527
        template <typename CMap>
520 528
        NodeMap& operator=(const CMap&) {
521 529
          checkConcept<ReadMap<Node, T>, CMap>();
522 530
          return *this;
523 531
        }
524 532
      };
525 533

	
526
      /// \brief Read write map of the directed arcs to type \c T.
534
      /// \brief Standard graph map type for the arcs.
527 535
      ///
528
      /// Reference map of the directed arcs to type \c T.
529
      /// \sa Reference
536
      /// Standard graph map type for the arcs.
537
      /// It conforms to the ReferenceMap concept.
530 538
      template<class T>
531
      class ArcMap : public ReadWriteMap<Arc,T>
539
      class ArcMap : public ReferenceMap<Arc, T, T&, const T&>
532 540
      {
533 541
      public:
534 542

	
535
        ///\e
536
        ArcMap(const Graph&) { }
537
        ///\e
543
        /// Constructor
544
        explicit ArcMap(const Graph&) { }
545
        /// Constructor with given initial value
538 546
        ArcMap(const Graph&, T) { }
547

	
539 548
      private:
540 549
        ///Copy constructor
541
        ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
550
        ArcMap(const ArcMap& em) :
551
          ReferenceMap<Arc, T, T&, const T&>(em) { }
542 552
        ///Assignment operator
543 553
        template <typename CMap>
544 554
        ArcMap& operator=(const CMap&) {
545 555
          checkConcept<ReadMap<Arc, T>, CMap>();
546 556
          return *this;
547 557
        }
548 558
      };
549 559

	
550
      /// Read write map of the edges to type \c T.
551

	
552
      /// Reference map of the arcs to type \c T.
553
      /// \sa Reference
560
      /// \brief Standard graph map type for the edges.
561
      ///
562
      /// Standard graph map type for the edges.
563
      /// It conforms to the ReferenceMap concept.
554 564
      template<class T>
555
      class EdgeMap : public ReadWriteMap<Edge,T>
565
      class EdgeMap : public ReferenceMap<Edge, T, T&, const T&>
556 566
      {
557 567
      public:
558 568

	
559
        ///\e
560
        EdgeMap(const Graph&) { }
561
        ///\e
569
        /// Constructor
570
        explicit EdgeMap(const Graph&) { }
571
        /// Constructor with given initial value
562 572
        EdgeMap(const Graph&, T) { }
573

	
563 574
      private:
564 575
        ///Copy constructor
565
        EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) {}
576
        EdgeMap(const EdgeMap& em) :
577
          ReferenceMap<Edge, T, T&, const T&>(em) {}
566 578
        ///Assignment operator
567 579
        template <typename CMap>
568 580
        EdgeMap& operator=(const CMap&) {
569 581
          checkConcept<ReadMap<Edge, T>, CMap>();
570 582
          return *this;
571 583
        }
572 584
      };
573 585

	
574
      /// \brief Direct the given edge.
586
      /// \brief The first node of the edge.
575 587
      ///
576
      /// Direct the given edge. The returned arc source
577
      /// will be the given node.
578
      Arc direct(const Edge&, const Node&) const {
588
      /// Returns the first node of the given edge.
589
      ///
590
      /// Edges don't have source and target nodes, however methods
591
      /// u() and v() are used to query the two end-nodes of an edge.
592
      /// The orientation of an edge that arises this way is called
593
      /// the inherent direction, it is used to define the default
594
      /// direction for the corresponding arcs.
595
      /// \sa v()
596
      /// \sa direction()
597
      Node u(Edge) const { return INVALID; }
598

	
599
      /// \brief The second node of the edge.
600
      ///
601
      /// Returns the second node of the given edge.
602
      ///
603
      /// Edges don't have source and target nodes, however methods
604
      /// u() and v() are used to query the two end-nodes of an edge.
605
      /// The orientation of an edge that arises this way is called
606
      /// the inherent direction, it is used to define the default
607
      /// direction for the corresponding arcs.
608
      /// \sa u()
609
      /// \sa direction()
610
      Node v(Edge) const { return INVALID; }
611

	
612
      /// \brief The source node of the arc.
613
      ///
614
      /// Returns the source node of the given arc.
615
      Node source(Arc) const { return INVALID; }
616

	
617
      /// \brief The target node of the arc.
618
      ///
619
      /// Returns the target node of the given arc.
620
      Node target(Arc) const { return INVALID; }
621

	
622
      /// \brief The ID of the node.
623
      ///
624
      /// Returns the ID of the given node.
625
      int id(Node) const { return -1; }
626

	
627
      /// \brief The ID of the edge.
628
      ///
629
      /// Returns the ID of the given edge.
630
      int id(Edge) const { return -1; }
631

	
632
      /// \brief The ID of the arc.
633
      ///
634
      /// Returns the ID of the given arc.
635
      int id(Arc) const { return -1; }
636

	
637
      /// \brief The node with the given ID.
638
      ///
639
      /// Returns the node with the given ID.
640
      /// \pre The argument should be a valid node ID in the graph.
641
      Node nodeFromId(int) const { return INVALID; }
642

	
643
      /// \brief The edge with the given ID.
644
      ///
645
      /// Returns the edge with the given ID.
646
      /// \pre The argument should be a valid edge ID in the graph.
647
      Edge edgeFromId(int) const { return INVALID; }
648

	
649
      /// \brief The arc with the given ID.
650
      ///
651
      /// Returns the arc with the given ID.
652
      /// \pre The argument should be a valid arc ID in the graph.
653
      Arc arcFromId(int) const { return INVALID; }
654

	
655
      /// \brief An upper bound on the node IDs.
656
      ///
657
      /// Returns an upper bound on the node IDs.
658
      int maxNodeId() const { return -1; }
659

	
660
      /// \brief An upper bound on the edge IDs.
661
      ///
662
      /// Returns an upper bound on the edge IDs.
663
      int maxEdgeId() const { return -1; }
664

	
665
      /// \brief An upper bound on the arc IDs.
666
      ///
667
      /// Returns an upper bound on the arc IDs.
668
      int maxArcId() const { return -1; }
669

	
670
      /// \brief The direction of the arc.
671
      ///
672
      /// Returns \c true if the direction of the given arc is the same as
673
      /// the inherent orientation of the represented edge.
674
      bool direction(Arc) const { return true; }
675

	
676
      /// \brief Direct the edge.
677
      ///
678
      /// Direct the given edge. The returned arc
679
      /// represents the given edge and its direction comes
680
      /// from the bool parameter. If it is \c true, then the direction
681
      /// of the arc is the same as the inherent orientation of the edge.
682
      Arc direct(Edge, bool) const {
579 683
        return INVALID;
580 684
      }
581 685

	
582
      /// \brief Direct the given edge.
686
      /// \brief Direct the edge.
583 687
      ///
584
      /// Direct the given edge. The returned arc
585
      /// represents the given edge and the direction comes
586
      /// from the bool parameter. The source of the edge and
587
      /// the directed arc is the same when the given bool is true.
588
      Arc direct(const Edge&, bool) const {
688
      /// Direct the given edge. The returned arc represents the given
689
      /// edge and its source node is the given node.
690
      Arc direct(Edge, Node) const {
589 691
        return INVALID;
590 692
      }
591 693

	
592
      /// \brief Returns true if the arc has default orientation.
694
      /// \brief The oppositely directed arc.
593 695
      ///
594
      /// Returns whether the given directed arc is same orientation as
595
      /// the corresponding edge's default orientation.
596
      bool direction(Arc) const { return true; }
597

	
598
      /// \brief Returns the opposite directed arc.
599
      ///
600
      /// Returns the opposite directed arc.
696
      /// Returns the oppositely directed arc representing the same edge.
601 697
      Arc oppositeArc(Arc) const { return INVALID; }
602 698

	
603
      /// \brief Opposite node on an arc
699
      /// \brief The opposite node on the edge.
604 700
      ///
605
      /// \return the opposite of the given Node on the given Edge
701
      /// Returns the opposite node on the given edge.
606 702
      Node oppositeNode(Node, Edge) const { return INVALID; }
607 703

	
608
      /// \brief First node of the edge.
609
      ///
610
      /// \return the first node of the given Edge.
611
      ///
612
      /// Naturally edges don't have direction and thus
613
      /// don't have source and target node. But we use these two methods
614
      /// to query the two nodes of the arc. The direction of the arc
615
      /// which arises this way is called the inherent direction of the
616
      /// edge, and is used to define the "default" direction
617
      /// of the directed versions of the arcs.
618
      /// \sa direction
619
      Node u(Edge) const { return INVALID; }
620

	
621
      /// \brief Second node of the edge.
622
      Node v(Edge) const { return INVALID; }
623

	
624
      /// \brief Source node of the directed arc.
625
      Node source(Arc) const { return INVALID; }
626

	
627
      /// \brief Target node of the directed arc.
628
      Node target(Arc) const { return INVALID; }
629

	
630
      /// \brief Returns the id of the node.
631
      int id(Node) const { return -1; }
632

	
633
      /// \brief Returns the id of the edge.
634
      int id(Edge) const { return -1; }
635

	
636
      /// \brief Returns the id of the arc.
637
      int id(Arc) const { return -1; }
638

	
639
      /// \brief Returns the node with the given id.
640
      ///
641
      /// \pre The argument should be a valid node id in the graph.
642
      Node nodeFromId(int) const { return INVALID; }
643

	
644
      /// \brief Returns the edge with the given id.
645
      ///
646
      /// \pre The argument should be a valid edge id in the graph.
647
      Edge edgeFromId(int) const { return INVALID; }
648

	
649
      /// \brief Returns the arc with the given id.
650
      ///
651
      /// \pre The argument should be a valid arc id in the graph.
652
      Arc arcFromId(int) const { return INVALID; }
653

	
654
      /// \brief Returns an upper bound on the node IDs.
655
      int maxNodeId() const { return -1; }
656

	
657
      /// \brief Returns an upper bound on the edge IDs.
658
      int maxEdgeId() const { return -1; }
659

	
660
      /// \brief Returns an upper bound on the arc IDs.
661
      int maxArcId() const { return -1; }
662

	
663 704
      void first(Node&) const {}
664 705
      void next(Node&) const {}
665 706

	
666 707
      void first(Edge&) const {}
667 708
      void next(Edge&) const {}
668 709

	
669 710
      void first(Arc&) const {}
670 711
      void next(Arc&) const {}
671 712

	
672 713
      void firstOut(Arc&, Node) const {}
673 714
      void nextOut(Arc&) const {}
674 715

	
675 716
      void firstIn(Arc&, Node) const {}
676 717
      void nextIn(Arc&) const {}
677 718

	
678 719
      void firstInc(Edge &, bool &, const Node &) const {}
679 720
      void nextInc(Edge &, bool &) const {}
680 721

	
681 722
      // The second parameter is dummy.
682 723
      Node fromId(int, Node) const { return INVALID; }
683 724
      // The second parameter is dummy.
684 725
      Edge fromId(int, Edge) const { return INVALID; }
685 726
      // The second parameter is dummy.
686 727
      Arc fromId(int, Arc) const { return INVALID; }
687 728

	
688 729
      // Dummy parameter.
689 730
      int maxId(Node) const { return -1; }
690 731
      // Dummy parameter.
691 732
      int maxId(Edge) const { return -1; }
692 733
      // Dummy parameter.
693 734
      int maxId(Arc) const { return -1; }
694 735

	
695
      /// \brief Base node of the iterator
736
      /// \brief The base node of the iterator.
696 737
      ///
697
      /// Returns the base node (the source in this case) of the iterator
698
      Node baseNode(OutArcIt e) const {
699
        return source(e);
700
      }
701
      /// \brief Running node of the iterator
738
      /// Returns the base node of the given incident edge iterator.
739
      Node baseNode(IncEdgeIt) const { return INVALID; }
740

	
741
      /// \brief The running node of the iterator.
702 742
      ///
703
      /// Returns the running node (the target in this case) of the
704
      /// iterator
705
      Node runningNode(OutArcIt e) const {
706
        return target(e);
707
      }
743
      /// Returns the running node of the given incident edge iterator.
744
      Node runningNode(IncEdgeIt) const { return INVALID; }
708 745

	
709
      /// \brief Base node of the iterator
746
      /// \brief The base node of the iterator.
710 747
      ///
711
      /// Returns the base node (the target in this case) of the iterator
712
      Node baseNode(InArcIt e) const {
713
        return target(e);
714
      }
715
      /// \brief Running node of the iterator
748
      /// Returns the base node of the given outgoing arc iterator
749
      /// (i.e. the source node of the corresponding arc).
750
      Node baseNode(OutArcIt) const { return INVALID; }
751

	
752
      /// \brief The running node of the iterator.
716 753
      ///
717
      /// Returns the running node (the source in this case) of the
718
      /// iterator
719
      Node runningNode(InArcIt e) const {
720
        return source(e);
721
      }
754
      /// Returns the running node of the given outgoing arc iterator
755
      /// (i.e. the target node of the corresponding arc).
756
      Node runningNode(OutArcIt) const { return INVALID; }
722 757

	
723
      /// \brief Base node of the iterator
758
      /// \brief The base node of the iterator.
724 759
      ///
725
      /// Returns the base node of the iterator
726
      Node baseNode(IncEdgeIt) const {
727
        return INVALID;
728
      }
760
      /// Returns the base node of the given incomming arc iterator
761
      /// (i.e. the target node of the corresponding arc).
762
      Node baseNode(InArcIt) const { return INVALID; }
729 763

	
730
      /// \brief Running node of the iterator
764
      /// \brief The running node of the iterator.
731 765
      ///
732
      /// Returns the running node of the iterator
733
      Node runningNode(IncEdgeIt) const {
734
        return INVALID;
735
      }
766
      /// Returns the running node of the given incomming arc iterator
767
      /// (i.e. the source node of the corresponding arc).
768
      Node runningNode(InArcIt) const { return INVALID; }
736 769

	
737 770
      template <typename _Graph>
738 771
      struct Constraints {
739 772
        void constraints() {
773
          checkConcept<BaseGraphComponent, _Graph>();
740 774
          checkConcept<IterableGraphComponent<>, _Graph>();
741 775
          checkConcept<IDableGraphComponent<>, _Graph>();
742 776
          checkConcept<MappableGraphComponent<>, _Graph>();
743 777
        }
744 778
      };
745 779

	
746 780
    };
747 781

	
748 782
  }
749 783

	
750 784
}
751 785

	
752 786
#endif
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup graph_concepts
20 20
///\file
21 21
///\brief The concept of graph components.
22 22

	
23

	
24
#ifndef LEMON_CONCEPT_GRAPH_COMPONENTS_H
25
#define LEMON_CONCEPT_GRAPH_COMPONENTS_H
23
#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H
24
#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H
26 25

	
27 26
#include <lemon/core.h>
28 27
#include <lemon/concepts/maps.h>
29 28

	
30 29
#include <lemon/bits/alteration_notifier.h>
31 30

	
32 31
namespace lemon {
33 32
  namespace concepts {
34 33

	
35
    /// \brief Skeleton class for graph Node and Arc types
34
    /// \brief Concept class for \c Node, \c Arc and \c Edge types.
36 35
    ///
37
    /// This class describes the interface of Node and Arc (and Edge
38
    /// in undirected graphs) subtypes of graph types.
36
    /// This class describes the concept of \c Node, \c Arc and \c Edge
37
    /// subtypes of digraph and graph types.
39 38
    ///
40 39
    /// \note This class is a template class so that we can use it to
41
    /// create graph skeleton classes. The reason for this is than Node
42
    /// and Arc types should \em not derive from the same base class.
43
    /// For Node you should instantiate it with character 'n' and for Arc
44
    /// with 'a'.
45

	
40
    /// create graph skeleton classes. The reason for this is that \c Node
41
    /// and \c Arc (or \c Edge) types should \e not derive from the same 
42
    /// base class. For \c Node you should instantiate it with character
43
    /// \c 'n', for \c Arc with \c 'a' and for \c Edge with \c 'e'.
46 44
#ifndef DOXYGEN
47
    template <char _selector = '0'>
45
    template <char sel = '0'>
48 46
#endif
49 47
    class GraphItem {
50 48
    public:
51 49
      /// \brief Default constructor.
52 50
      ///
51
      /// Default constructor.
53 52
      /// \warning The default constructor is not required to set
54 53
      /// the item to some well-defined value. So you should consider it
55 54
      /// as uninitialized.
56 55
      GraphItem() {}
56

	
57 57
      /// \brief Copy constructor.
58 58
      ///
59 59
      /// Copy constructor.
60
      GraphItem(const GraphItem &) {}
61

	
62
      /// \brief Constructor for conversion from \c INVALID.
60 63
      ///
61
      GraphItem(const GraphItem &) {}
62
      /// \brief Invalid constructor \& conversion.
63
      ///
64
      /// This constructor initializes the item to be invalid.
64
      /// Constructor for conversion from \c INVALID.
65
      /// It initializes the item to be invalid.
65 66
      /// \sa Invalid for more details.
66 67
      GraphItem(Invalid) {}
67
      /// \brief Assign operator for nodes.
68

	
69
      /// \brief Assignment operator.
68 70
      ///
69
      /// The nodes are assignable.
71
      /// Assignment operator for the item.
72
      GraphItem& operator=(const GraphItem&) { return *this; }
73

	
74
      /// \brief Assignment operator for INVALID.
70 75
      ///
71
      GraphItem& operator=(GraphItem const&) { return *this; }
76
      /// This operator makes the item invalid.
77
      GraphItem& operator=(Invalid) { return *this; }
78

	
72 79
      /// \brief Equality operator.
73 80
      ///
74
      /// Two iterators are equal if and only if they represents the
75
      /// same node in the graph or both are invalid.
76
      bool operator==(GraphItem) const { return false; }
81
      /// Equality operator.
82
      bool operator==(const GraphItem&) const { return false; }
83

	
77 84
      /// \brief Inequality operator.
78 85
      ///
79
      /// \sa operator==(const Node& n)
86
      /// Inequality operator.
87
      bool operator!=(const GraphItem&) const { return false; }
88

	
89
      /// \brief Ordering operator.
80 90
      ///
81
      bool operator!=(GraphItem) const { return false; }
82

	
83
      /// \brief Artificial ordering operator.
91
      /// This operator defines an ordering of the items.
92
      /// It makes possible to use graph item types as key types in 
93
      /// associative containers (e.g. \c std::map).
84 94
      ///
85
      /// To allow the use of graph descriptors as key type in std::map or
86
      /// similar associative container we require this.
87
      ///
88
      /// \note This operator only have to define some strict ordering of
95
      /// \note This operator only has to define some strict ordering of
89 96
      /// the items; this order has nothing to do with the iteration
90 97
      /// ordering of the items.
91
      bool operator<(GraphItem) const { return false; }
98
      bool operator<(const GraphItem&) const { return false; }
92 99

	
93 100
      template<typename _GraphItem>
94 101
      struct Constraints {
95 102
        void constraints() {
96 103
          _GraphItem i1;
104
          i1=INVALID;
97 105
          _GraphItem i2 = i1;
98 106
          _GraphItem i3 = INVALID;
99 107

	
100 108
          i1 = i2 = i3;
101 109

	
102 110
          bool b;
103
          //          b = (ia == ib) && (ia != ib) && (ia < ib);
104 111
          b = (ia == ib) && (ia != ib);
105 112
          b = (ia == INVALID) && (ib != INVALID);
106 113
          b = (ia < ib);
107 114
        }
108 115

	
109 116
        const _GraphItem &ia;
110 117
        const _GraphItem &ib;
111 118
      };
112 119
    };
113 120

	
114
    /// \brief An empty base directed graph class.
121
    /// \brief Base skeleton class for directed graphs.
115 122
    ///
116
    /// This class provides the minimal set of features needed for a
117
    /// directed graph structure. All digraph concepts have to be
118
    /// conform to this base directed graph. It just provides types
119
    /// for nodes and arcs and functions to get the source and the
120
    /// target of the arcs.
123
    /// This class describes the base interface of directed graph types.
124
    /// All digraph %concepts have to conform to this class.
125
    /// It just provides types for nodes and arcs and functions 
126
    /// to get the source and the target nodes of arcs.
121 127
    class BaseDigraphComponent {
122 128
    public:
123 129

	
124 130
      typedef BaseDigraphComponent Digraph;
125 131

	
126 132
      /// \brief Node class of the digraph.
127 133
      ///
128
      /// This class represents the Nodes of the digraph.
129
      ///
134
      /// This class represents the nodes of the digraph.
130 135
      typedef GraphItem<'n'> Node;
131 136

	
132 137
      /// \brief Arc class of the digraph.
133 138
      ///
134
      /// This class represents the Arcs of the digraph.
139
      /// This class represents the arcs of the digraph.
140
      typedef GraphItem<'a'> Arc;
141

	
142
      /// \brief Return the source node of an arc.
135 143
      ///
136
      typedef GraphItem<'e'> Arc;
144
      /// This function returns the source node of an arc.
145
      Node source(const Arc&) const { return INVALID; }
137 146

	
138
      /// \brief Gives back the target node of an arc.
147
      /// \brief Return the target node of an arc.
139 148
      ///
140
      /// Gives back the target node of an arc.
149
      /// This function returns the target node of an arc.
150
      Node target(const Arc&) const { return INVALID; }
151

	
152
      /// \brief Return the opposite node on the given arc.
141 153
      ///
142
      Node target(const Arc&) const { return INVALID;}
143

	
144
      /// \brief Gives back the source node of an arc.
145
      ///
146
      /// Gives back the source node of an arc.
147
      ///
148
      Node source(const Arc&) const { return INVALID;}
149

	
150
      /// \brief Gives back the opposite node on the given arc.
151
      ///
152
      /// Gives back the opposite node on the given arc.
154
      /// This function returns the opposite node on the given arc.
153 155
      Node oppositeNode(const Node&, const Arc&) const {
154 156
        return INVALID;
155 157
      }
156 158

	
157 159
      template <typename _Digraph>
158 160
      struct Constraints {
159 161
        typedef typename _Digraph::Node Node;
160 162
        typedef typename _Digraph::Arc Arc;
161 163

	
162 164
        void constraints() {
163 165
          checkConcept<GraphItem<'n'>, Node>();
164 166
          checkConcept<GraphItem<'a'>, Arc>();
165 167
          {
166 168
            Node n;
167 169
            Arc e(INVALID);
168 170
            n = digraph.source(e);
169 171
            n = digraph.target(e);
170 172
            n = digraph.oppositeNode(n, e);
171 173
          }
172 174
        }
173 175

	
174 176
        const _Digraph& digraph;
175 177
      };
176 178
    };
177 179

	
178
    /// \brief An empty base undirected graph class.
180
    /// \brief Base skeleton class for undirected graphs.
179 181
    ///
180
    /// This class provides the minimal set of features needed for an
181
    /// undirected graph structure. All undirected graph concepts have
182
    /// to be conform to this base graph. It just provides types for
183
    /// nodes, arcs and edges and functions to get the
184
    /// source and the target of the arcs and edges,
185
    /// conversion from arcs to edges and function to get
186
    /// both direction of the edges.
182
    /// This class describes the base interface of undirected graph types.
183
    /// All graph %concepts have to conform to this class.
184
    /// It extends the interface of \ref BaseDigraphComponent with an
185
    /// \c Edge type and functions to get the end nodes of edges,
186
    /// to convert from arcs to edges and to get both direction of edges.
187 187
    class BaseGraphComponent : public BaseDigraphComponent {
188 188
    public:
189

	
190
      typedef BaseGraphComponent Graph;
191

	
189 192
      typedef BaseDigraphComponent::Node Node;
190 193
      typedef BaseDigraphComponent::Arc Arc;
191
      /// \brief Undirected arc class of the graph.
194

	
195
      /// \brief Undirected edge class of the graph.
192 196
      ///
193
      /// This class represents the edges of the graph.
194
      /// The undirected graphs can be used as a directed graph which
195
      /// for each arc contains the opposite arc too so the graph is
196
      /// bidirected. The edge represents two opposite
197
      /// directed arcs.
198
      class Edge : public GraphItem<'u'> {
197
      /// This class represents the undirected edges of the graph.
198
      /// Undirected graphs can be used as directed graphs, each edge is
199
      /// represented by two opposite directed arcs.
200
      class Edge : public GraphItem<'e'> {
201
        typedef GraphItem<'e'> Parent;
202

	
199 203
      public:
200
        typedef GraphItem<'u'> Parent;
201 204
        /// \brief Default constructor.
202 205
        ///
206
        /// Default constructor.
203 207
        /// \warning The default constructor is not required to set
204 208
        /// the item to some well-defined value. So you should consider it
205 209
        /// as uninitialized.
206 210
        Edge() {}
211

	
207 212
        /// \brief Copy constructor.
208 213
        ///
209 214
        /// Copy constructor.
215
        Edge(const Edge &) : Parent() {}
216

	
217
        /// \brief Constructor for conversion from \c INVALID.
210 218
        ///
211
        Edge(const Edge &) : Parent() {}
212
        /// \brief Invalid constructor \& conversion.
213
        ///
214
        /// This constructor initializes the item to be invalid.
219
        /// Constructor for conversion from \c INVALID.
220
        /// It initializes the item to be invalid.
215 221
        /// \sa Invalid for more details.
216 222
        Edge(Invalid) {}
217
        /// \brief Converter from arc to edge.
223

	
224
        /// \brief Constructor for conversion from an arc.
218 225
        ///
226
        /// Constructor for conversion from an arc.
219 227
        /// Besides the core graph item functionality each arc should
220 228
        /// be convertible to the represented edge.
221 229
        Edge(const Arc&) {}
222
        /// \brief Assign arc to edge.
223
        ///
224
        /// Besides the core graph item functionality each arc should
225
        /// be convertible to the represented edge.
226
        Edge& operator=(const Arc&) { return *this; }
227
      };
230
     };
228 231

	
229
      /// \brief Returns the direction of the arc.
232
      /// \brief Return one end node of an edge.
233
      ///
234
      /// This function returns one end node of an edge.
235
      Node u(const Edge&) const { return INVALID; }
236

	
237
      /// \brief Return the other end node of an edge.
238
      ///
239
      /// This function returns the other end node of an edge.
240
      Node v(const Edge&) const { return INVALID; }
241

	
242
      /// \brief Return a directed arc related to an edge.
243
      ///
244
      /// This function returns a directed arc from its direction and the
245
      /// represented edge.
246
      Arc direct(const Edge&, bool) const { return INVALID; }
247

	
248
      /// \brief Return a directed arc related to an edge.
249
      ///
250
      /// This function returns a directed arc from its source node and the
251
      /// represented edge.
252
      Arc direct(const Edge&, const Node&) const { return INVALID; }
253

	
254
      /// \brief Return the direction of the arc.
230 255
      ///
231 256
      /// Returns the direction of the arc. Each arc represents an
232 257
      /// edge with a direction. It gives back the
233 258
      /// direction.
234 259
      bool direction(const Arc&) const { return true; }
235 260

	
236
      /// \brief Returns the directed arc.
261
      /// \brief Return the opposite arc.
237 262
      ///
238
      /// Returns the directed arc from its direction and the
239
      /// represented edge.
240
      Arc direct(const Edge&, bool) const { return INVALID;}
241

	
242
      /// \brief Returns the directed arc.
243
      ///
244
      /// Returns the directed arc from its source and the
245
      /// represented edge.
246
      Arc direct(const Edge&, const Node&) const { return INVALID;}
247

	
248
      /// \brief Returns the opposite arc.
249
      ///
250
      /// Returns the opposite arc. It is the arc representing the
251
      /// same edge and has opposite direction.
252
      Arc oppositeArc(const Arc&) const { return INVALID;}
253

	
254
      /// \brief Gives back one ending of an edge.
255
      ///
256
      /// Gives back one ending of an edge.
257
      Node u(const Edge&) const { return INVALID;}
258

	
259
      /// \brief Gives back the other ending of an edge.
260
      ///
261
      /// Gives back the other ending of an edge.
262
      Node v(const Edge&) const { return INVALID;}
263
      /// This function returns the opposite arc, i.e. the arc representing
264
      /// the same edge and has opposite direction.
265
      Arc oppositeArc(const Arc&) const { return INVALID; }
263 266

	
264 267
      template <typename _Graph>
265 268
      struct Constraints {
266 269
        typedef typename _Graph::Node Node;
267 270
        typedef typename _Graph::Arc Arc;
268 271
        typedef typename _Graph::Edge Edge;
269 272

	
270 273
        void constraints() {
271 274
          checkConcept<BaseDigraphComponent, _Graph>();
272
          checkConcept<GraphItem<'u'>, Edge>();
275
          checkConcept<GraphItem<'e'>, Edge>();
273 276
          {
274 277
            Node n;
275 278
            Edge ue(INVALID);
276 279
            Arc e;
277 280
            n = graph.u(ue);
278 281
            n = graph.v(ue);
279 282
            e = graph.direct(ue, true);
283
            e = graph.direct(ue, false);
280 284
            e = graph.direct(ue, n);
281 285
            e = graph.oppositeArc(e);
282 286
            ue = e;
283 287
            bool d = graph.direction(e);
284 288
            ignore_unused_variable_warning(d);
285 289
          }
286 290
        }
287 291

	
288 292
        const _Graph& graph;
289 293
      };
290 294

	
291 295
    };
292 296

	
293
    /// \brief An empty idable base digraph class.
297
    /// \brief Skeleton class for \e idable directed graphs.
294 298
    ///
295
    /// This class provides beside the core digraph features
296
    /// core id functions for the digraph structure.
297
    /// The most of the base digraphs should be conform to this concept.
298
    /// The id's are unique and immutable.
299
    template <typename _Base = BaseDigraphComponent>
300
    class IDableDigraphComponent : public _Base {
299
    /// This class describes the interface of \e idable directed graphs.
300
    /// It extends \ref BaseDigraphComponent with the core ID functions.
301
    /// The ids of the items must be unique and immutable.
302
    /// This concept is part of the Digraph concept.
303
    template <typename BAS = BaseDigraphComponent>
304
    class IDableDigraphComponent : public BAS {
301 305
    public:
302 306

	
303
      typedef _Base Base;
307
      typedef BAS Base;
304 308
      typedef typename Base::Node Node;
305 309
      typedef typename Base::Arc Arc;
306 310

	
307
      /// \brief Gives back an unique integer id for the Node.
311
      /// \brief Return a unique integer id for the given node.
308 312
      ///
309
      /// Gives back an unique integer id for the Node.
313
      /// This function returns a unique integer id for the given node.
314
      int id(const Node&) const { return -1; }
315

	
316
      /// \brief Return the node by its unique id.
310 317
      ///
311
      int id(const Node&) const { return -1;}
318
      /// This function returns the node by its unique id.
319
      /// If the digraph does not contain a node with the given id,
320
      /// then the result of the function is undefined.
321
      Node nodeFromId(int) const { return INVALID; }
312 322

	
313
      /// \brief Gives back the node by the unique id.
323
      /// \brief Return a unique integer id for the given arc.
314 324
      ///
315
      /// Gives back the node by the unique id.
316
      /// If the digraph does not contain node with the given id
317
      /// then the result of the function is undetermined.
318
      Node nodeFromId(int) const { return INVALID;}
325
      /// This function returns a unique integer id for the given arc.
326
      int id(const Arc&) const { return -1; }
319 327

	
320
      /// \brief Gives back an unique integer id for the Arc.
328
      /// \brief Return the arc by its unique id.
321 329
      ///
322
      /// Gives back an unique integer id for the Arc.
330
      /// This function returns the arc by its unique id.
331
      /// If the digraph does not contain an arc with the given id,
332
      /// then the result of the function is undefined.
333
      Arc arcFromId(int) const { return INVALID; }
334

	
335
      /// \brief Return an integer greater or equal to the maximum
336
      /// node id.
323 337
      ///
324
      int id(const Arc&) const { return -1;}
338
      /// This function returns an integer greater or equal to the
339
      /// maximum node id.
340
      int maxNodeId() const { return -1; }
325 341

	
326
      /// \brief Gives back the arc by the unique id.
342
      /// \brief Return an integer greater or equal to the maximum
343
      /// arc id.
327 344
      ///
328
      /// Gives back the arc by the unique id.
329
      /// If the digraph does not contain arc with the given id
330
      /// then the result of the function is undetermined.
331
      Arc arcFromId(int) const { return INVALID;}
332

	
333
      /// \brief Gives back an integer greater or equal to the maximum
334
      /// Node id.
335
      ///
336
      /// Gives back an integer greater or equal to the maximum Node
337
      /// id.
338
      int maxNodeId() const { return -1;}
339

	
340
      /// \brief Gives back an integer greater or equal to the maximum
341
      /// Arc id.
342
      ///
343
      /// Gives back an integer greater or equal to the maximum Arc
344
      /// id.
345
      int maxArcId() const { return -1;}
345
      /// This function returns an integer greater or equal to the
346
      /// maximum arc id.
347
      int maxArcId() const { return -1; }
346 348

	
347 349
      template <typename _Digraph>
348 350
      struct Constraints {
349 351

	
350 352
        void constraints() {
351 353
          checkConcept<Base, _Digraph >();
352 354
          typename _Digraph::Node node;
355
          node=INVALID;
353 356
          int nid = digraph.id(node);
354 357
          nid = digraph.id(node);
355 358
          node = digraph.nodeFromId(nid);
356 359
          typename _Digraph::Arc arc;
360
          arc=INVALID;
357 361
          int eid = digraph.id(arc);
358 362
          eid = digraph.id(arc);
359 363
          arc = digraph.arcFromId(eid);
360 364

	
361 365
          nid = digraph.maxNodeId();
362 366
          ignore_unused_variable_warning(nid);
363 367
          eid = digraph.maxArcId();
364 368
          ignore_unused_variable_warning(eid);
365 369
        }
366 370

	
367 371
        const _Digraph& digraph;
368 372
      };
369 373
    };
370 374

	
371
    /// \brief An empty idable base undirected graph class.
375
    /// \brief Skeleton class for \e idable undirected graphs.
372 376
    ///
373
    /// This class provides beside the core undirected graph features
374
    /// core id functions for the undirected graph structure.  The
375
    /// most of the base undirected graphs should be conform to this
376
    /// concept.  The id's are unique and immutable.
377
    template <typename _Base = BaseGraphComponent>
378
    class IDableGraphComponent : public IDableDigraphComponent<_Base> {
377
    /// This class describes the interface of \e idable undirected
378
    /// graphs. It extends \ref IDableDigraphComponent with the core ID
379
    /// functions of undirected graphs.
380
    /// The ids of the items must be unique and immutable.
381
    /// This concept is part of the Graph concept.
382
    template <typename BAS = BaseGraphComponent>
383
    class IDableGraphComponent : public IDableDigraphComponent<BAS> {
379 384
    public:
380 385

	
381
      typedef _Base Base;
386
      typedef BAS Base;
382 387
      typedef typename Base::Edge Edge;
383 388

	
384
      using IDableDigraphComponent<_Base>::id;
389
      using IDableDigraphComponent<Base>::id;
385 390

	
386
      /// \brief Gives back an unique integer id for the Edge.
391
      /// \brief Return a unique integer id for the given edge.
387 392
      ///
388
      /// Gives back an unique integer id for the Edge.
393
      /// This function returns a unique integer id for the given edge.
394
      int id(const Edge&) const { return -1; }
395

	
396
      /// \brief Return the edge by its unique id.
389 397
      ///
390
      int id(const Edge&) const { return -1;}
398
      /// This function returns the edge by its unique id.
399
      /// If the graph does not contain an edge with the given id,
400
      /// then the result of the function is undefined.
401
      Edge edgeFromId(int) const { return INVALID; }
391 402

	
392
      /// \brief Gives back the edge by the unique id.
403
      /// \brief Return an integer greater or equal to the maximum
404
      /// edge id.
393 405
      ///
394
      /// Gives back the edge by the unique id.  If the
395
      /// graph does not contain arc with the given id then the
396
      /// result of the function is undetermined.
397
      Edge edgeFromId(int) const { return INVALID;}
398

	
399
      /// \brief Gives back an integer greater or equal to the maximum
400
      /// Edge id.
401
      ///
402
      /// Gives back an integer greater or equal to the maximum Edge
403
      /// id.
404
      int maxEdgeId() const { return -1;}
406
      /// This function returns an integer greater or equal to the
407
      /// maximum edge id.
408
      int maxEdgeId() const { return -1; }
405 409

	
406 410
      template <typename _Graph>
407 411
      struct Constraints {
408 412

	
409 413
        void constraints() {
410
          checkConcept<Base, _Graph >();
411 414
          checkConcept<IDableDigraphComponent<Base>, _Graph >();
412 415
          typename _Graph::Edge edge;
413 416
          int ueid = graph.id(edge);
414 417
          ueid = graph.id(edge);
415 418
          edge = graph.edgeFromId(ueid);
416 419
          ueid = graph.maxEdgeId();
417 420
          ignore_unused_variable_warning(ueid);
418 421
        }
419 422

	
420 423
        const _Graph& graph;
421 424
      };
422 425
    };
423 426

	
424
    /// \brief Skeleton class for graph NodeIt and ArcIt
427
    /// \brief Concept class for \c NodeIt, \c ArcIt and \c EdgeIt types.
425 428
    ///
426
    /// Skeleton class for graph NodeIt and ArcIt.
427
    ///
428
    template <typename _Graph, typename _Item>
429
    class GraphItemIt : public _Item {
429
    /// This class describes the concept of \c NodeIt, \c ArcIt and 
430
    /// \c EdgeIt subtypes of digraph and graph types.
431
    template <typename GR, typename Item>
432
    class GraphItemIt : public Item {
430 433
    public:
431 434
      /// \brief Default constructor.
432 435
      ///
433
      /// @warning The default constructor sets the iterator
434
      /// to an undefined value.
436
      /// Default constructor.
437
      /// \warning The default constructor is not required to set
438
      /// the iterator to some well-defined value. So you should consider it
439
      /// as uninitialized.
435 440
      GraphItemIt() {}
441

	
436 442
      /// \brief Copy constructor.
437 443
      ///
438 444
      /// Copy constructor.
445
      GraphItemIt(const GraphItemIt& it) : Item(it) {}
446

	
447
      /// \brief Constructor that sets the iterator to the first item.
439 448
      ///
440
      GraphItemIt(const GraphItemIt& ) {}
441
      /// \brief Sets the iterator to the first item.
449
      /// Constructor that sets the iterator to the first item.
450
      explicit GraphItemIt(const GR&) {}
451

	
452
      /// \brief Constructor for conversion from \c INVALID.
442 453
      ///
443
      /// Sets the iterator to the first item of \c the graph.
444
      ///
445
      explicit GraphItemIt(const _Graph&) {}
446
      /// \brief Invalid constructor \& conversion.
447
      ///
448
      /// This constructor initializes the item to be invalid.
454
      /// Constructor for conversion from \c INVALID.
455
      /// It initializes the iterator to be invalid.
449 456
      /// \sa Invalid for more details.
450 457
      GraphItemIt(Invalid) {}
451
      /// \brief Assign operator for items.
458

	
459
      /// \brief Assignment operator.
452 460
      ///
453
      /// The items are assignable.
461
      /// Assignment operator for the iterator.
462
      GraphItemIt& operator=(const GraphItemIt&) { return *this; }
463

	
464
      /// \brief Increment the iterator.
454 465
      ///
455
      GraphItemIt& operator=(const GraphItemIt&) { return *this; }
456
      /// \brief Next item.
457
      ///
458
      /// Assign the iterator to the next item.
459
      ///
466
      /// This operator increments the iterator, i.e. assigns it to the
467
      /// next item.
460 468
      GraphItemIt& operator++() { return *this; }
469
 
461 470
      /// \brief Equality operator
462 471
      ///
472
      /// Equality operator.
463 473
      /// Two iterators are equal if and only if they point to the
464 474
      /// same object or both are invalid.
465 475
      bool operator==(const GraphItemIt&) const { return true;}
476

	
466 477
      /// \brief Inequality operator
467 478
      ///
468
      /// \sa operator==(Node n)
469
      ///
479
      /// Inequality operator.
480
      /// Two iterators are equal if and only if they point to the
481
      /// same object or both are invalid.
470 482
      bool operator!=(const GraphItemIt&) const { return true;}
471 483

	
472 484
      template<typename _GraphItemIt>
473 485
      struct Constraints {
474 486
        void constraints() {
487
          checkConcept<GraphItem<>, _GraphItemIt>();
475 488
          _GraphItemIt it1(g);
476 489
          _GraphItemIt it2;
490
          _GraphItemIt it3 = it1;
491
          _GraphItemIt it4 = INVALID;
477 492

	
478 493
          it2 = ++it1;
479 494
          ++it2 = it1;
480 495
          ++(++it1);
481 496

	
482
          _Item bi = it1;
497
          Item bi = it1;
483 498
          bi = it2;
484 499
        }
485
        _Graph& g;
500
        const GR& g;
486 501
      };
487 502
    };
488 503

	
489
    /// \brief Skeleton class for graph InArcIt and OutArcIt
504
    /// \brief Concept class for \c InArcIt, \c OutArcIt and 
505
    /// \c IncEdgeIt types.
490 506
    ///
491
    /// \note Because InArcIt and OutArcIt may not inherit from the same
492
    /// base class, the _selector is a additional template parameter. For
493
    /// InArcIt you should instantiate it with character 'i' and for
494
    /// OutArcIt with 'o'.
495
    template <typename _Graph,
496
              typename _Item = typename _Graph::Arc,
497
              typename _Base = typename _Graph::Node,
498
              char _selector = '0'>
499
    class GraphIncIt : public _Item {
507
    /// This class describes the concept of \c InArcIt, \c OutArcIt 
508
    /// and \c IncEdgeIt subtypes of digraph and graph types.
509
    ///
510
    /// \note Since these iterator classes do not inherit from the same
511
    /// base class, there is an additional template parameter (selector)
512
    /// \c sel. For \c InArcIt you should instantiate it with character 
513
    /// \c 'i', for \c OutArcIt with \c 'o' and for \c IncEdgeIt with \c 'e'.
514
    template <typename GR,
515
              typename Item = typename GR::Arc,
516
              typename Base = typename GR::Node,
517
              char sel = '0'>
518
    class GraphIncIt : public Item {
500 519
    public:
501 520
      /// \brief Default constructor.
502 521
      ///
503
      /// @warning The default constructor sets the iterator
504
      /// to an undefined value.
522
      /// Default constructor.
523
      /// \warning The default constructor is not required to set
524
      /// the iterator to some well-defined value. So you should consider it
525
      /// as uninitialized.
505 526
      GraphIncIt() {}
527

	
506 528
      /// \brief Copy constructor.
507 529
      ///
508 530
      /// Copy constructor.
531
      GraphIncIt(const GraphIncIt& it) : Item(it) {}
532

	
533
      /// \brief Constructor that sets the iterator to the first 
534
      /// incoming or outgoing arc.
509 535
      ///
510
      GraphIncIt(GraphIncIt const& gi) : _Item(gi) {}
511
      /// \brief Sets the iterator to the first arc incoming into or outgoing
512
      /// from the node.
536
      /// Constructor that sets the iterator to the first arc 
537
      /// incoming to or outgoing from the given node.
538
      explicit GraphIncIt(const GR&, const Base&) {}
539

	
540
      /// \brief Constructor for conversion from \c INVALID.
513 541
      ///
514
      /// Sets the iterator to the first arc incoming into or outgoing
515
      /// from the node.
516
      ///
517
      explicit GraphIncIt(const _Graph&, const _Base&) {}
518
      /// \brief Invalid constructor \& conversion.
519
      ///
520
      /// This constructor initializes the item to be invalid.
542
      /// Constructor for conversion from \c INVALID.
543
      /// It initializes the iterator to be invalid.
521 544
      /// \sa Invalid for more details.
522 545
      GraphIncIt(Invalid) {}
523
      /// \brief Assign operator for iterators.
546

	
547
      /// \brief Assignment operator.
524 548
      ///
525
      /// The iterators are assignable.
549
      /// Assignment operator for the iterator.
550
      GraphIncIt& operator=(const GraphIncIt&) { return *this; }
551

	
552
      /// \brief Increment the iterator.
526 553
      ///
527
      GraphIncIt& operator=(GraphIncIt const&) { return *this; }
528
      /// \brief Next item.
529
      ///
530
      /// Assign the iterator to the next item.
531
      ///
554
      /// This operator increments the iterator, i.e. assigns it to the
555
      /// next arc incoming to or outgoing from the given node.
532 556
      GraphIncIt& operator++() { return *this; }
533 557

	
534 558
      /// \brief Equality operator
535 559
      ///
560
      /// Equality operator.
536 561
      /// Two iterators are equal if and only if they point to the
537 562
      /// same object or both are invalid.
538 563
      bool operator==(const GraphIncIt&) const { return true;}
539 564

	
540 565
      /// \brief Inequality operator
541 566
      ///
542
      /// \sa operator==(Node n)
543
      ///
567
      /// Inequality operator.
568
      /// Two iterators are equal if and only if they point to the
569
      /// same object or both are invalid.
544 570
      bool operator!=(const GraphIncIt&) const { return true;}
545 571

	
546 572
      template <typename _GraphIncIt>
547 573
      struct Constraints {
548 574
        void constraints() {
549
          checkConcept<GraphItem<_selector>, _GraphIncIt>();
575
          checkConcept<GraphItem<sel>, _GraphIncIt>();
550 576
          _GraphIncIt it1(graph, node);
551 577
          _GraphIncIt it2;
578
          _GraphIncIt it3 = it1;
579
          _GraphIncIt it4 = INVALID;
552 580

	
553 581
          it2 = ++it1;
554 582
          ++it2 = it1;
555 583
          ++(++it1);
556
          _Item e = it1;
584
          Item e = it1;
557 585
          e = it2;
558

	
559 586
        }
560

	
561
        _Item arc;
562
        _Base node;
563
        _Graph graph;
564
        _GraphIncIt it;
587
        const Base& node;
588
        const GR& graph;
565 589
      };
566 590
    };
567 591

	
568

	
569
    /// \brief An empty iterable digraph class.
592
    /// \brief Skeleton class for iterable directed graphs.
570 593
    ///
571
    /// This class provides beside the core digraph features
572
    /// iterator based iterable interface for the digraph structure.
594
    /// This class describes the interface of iterable directed
595
    /// graphs. It extends \ref BaseDigraphComponent with the core
596
    /// iterable interface.
573 597
    /// This concept is part of the Digraph concept.
574
    template <typename _Base = BaseDigraphComponent>
575
    class IterableDigraphComponent : public _Base {
598
    template <typename BAS = BaseDigraphComponent>
599
    class IterableDigraphComponent : public BAS {
576 600

	
577 601
    public:
578 602

	
579
      typedef _Base Base;
603
      typedef BAS Base;
580 604
      typedef typename Base::Node Node;
581 605
      typedef typename Base::Arc Arc;
582 606

	
583 607
      typedef IterableDigraphComponent Digraph;
584 608

	
585
      /// \name Base iteration
609
      /// \name Base Iteration
586 610
      ///
587
      /// This interface provides functions for iteration on digraph items
611
      /// This interface provides functions for iteration on digraph items.
588 612
      ///
589 613
      /// @{
590 614

	
591
      /// \brief Gives back the first node in the iterating order.
615
      /// \brief Return the first node.
592 616
      ///
593
      /// Gives back the first node in the iterating order.
594
      ///
617
      /// This function gives back the first node in the iteration order.
595 618
      void first(Node&) const {}
596 619

	
597
      /// \brief Gives back the next node in the iterating order.
620
      /// \brief Return the next node.
598 621
      ///
599
      /// Gives back the next node in the iterating order.
600
      ///
622
      /// This function gives back the next node in the iteration order.
601 623
      void next(Node&) const {}
602 624

	
603
      /// \brief Gives back the first arc in the iterating order.
625
      /// \brief Return the first arc.
604 626
      ///
605
      /// Gives back the first arc in the iterating order.
606
      ///
627
      /// This function gives back the first arc in the iteration order.
607 628
      void first(Arc&) const {}
608 629

	
609
      /// \brief Gives back the next arc in the iterating order.
630
      /// \brief Return the next arc.
610 631
      ///
611
      /// Gives back the next arc in the iterating order.
612
      ///
632
      /// This function gives back the next arc in the iteration order.
613 633
      void next(Arc&) const {}
614 634

	
615

	
616
      /// \brief Gives back the first of the arcs point to the given
617
      /// node.
635
      /// \brief Return the first arc incomming to the given node.
618 636
      ///
619
      /// Gives back the first of the arcs point to the given node.
620
      ///
637
      /// This function gives back the first arc incomming to the
638
      /// given node.
621 639
      void firstIn(Arc&, const Node&) const {}
622 640

	
623
      /// \brief Gives back the next of the arcs points to the given
624
      /// node.
641
      /// \brief Return the next arc incomming to the given node.
625 642
      ///
626
      /// Gives back the next of the arcs points to the given node.
627
      ///
643
      /// This function gives back the next arc incomming to the
644
      /// given node.
628 645
      void nextIn(Arc&) const {}
629 646

	
630
      /// \brief Gives back the first of the arcs start from the
647
      /// \brief Return the first arc outgoing form the given node.
648
      ///
649
      /// This function gives back the first arc outgoing form the
631 650
      /// given node.
632
      ///
633
      /// Gives back the first of the arcs start from the given node.
634
      ///
635 651
      void firstOut(Arc&, const Node&) const {}
636 652

	
637
      /// \brief Gives back the next of the arcs start from the given
638
      /// node.
653
      /// \brief Return the next arc outgoing form the given node.
639 654
      ///
640
      /// Gives back the next of the arcs start from the given node.
641
      ///
655
      /// This function gives back the next arc outgoing form the
656
      /// given node.
642 657
      void nextOut(Arc&) const {}
643 658

	
644 659
      /// @}
645 660

	
646
      /// \name Class based iteration
661
      /// \name Class Based Iteration
647 662
      ///
648
      /// This interface provides functions for iteration on digraph items
663
      /// This interface provides iterator classes for digraph items.
649 664
      ///
650 665
      /// @{
651 666

	
652 667
      /// \brief This iterator goes through each node.
653 668
      ///
654 669
      /// This iterator goes through each node.
655 670
      ///
656 671
      typedef GraphItemIt<Digraph, Node> NodeIt;
657 672

	
658
      /// \brief This iterator goes through each node.
673
      /// \brief This iterator goes through each arc.
659 674
      ///
660
      /// This iterator goes through each node.
675
      /// This iterator goes through each arc.
661 676
      ///
662 677
      typedef GraphItemIt<Digraph, Arc> ArcIt;
663 678

	
664 679
      /// \brief This iterator goes trough the incoming arcs of a node.
665 680
      ///
666
      /// This iterator goes trough the \e inccoming arcs of a certain node
681
      /// This iterator goes trough the \e incoming arcs of a certain node
667 682
      /// of a digraph.
668 683
      typedef GraphIncIt<Digraph, Arc, Node, 'i'> InArcIt;
669 684

	
670 685
      /// \brief This iterator goes trough the outgoing arcs of a node.
671 686
      ///
672 687
      /// This iterator goes trough the \e outgoing arcs of a certain node
673 688
      /// of a digraph.
674 689
      typedef GraphIncIt<Digraph, Arc, Node, 'o'> OutArcIt;
675 690

	
676 691
      /// \brief The base node of the iterator.
677 692
      ///
678
      /// Gives back the base node of the iterator.
679
      /// It is always the target of the pointed arc.
693
      /// This function gives back the base node of the iterator.
694
      /// It is always the target node of the pointed arc.
680 695
      Node baseNode(const InArcIt&) const { return INVALID; }
681 696

	
682 697
      /// \brief The running node of the iterator.
683 698
      ///
684
      /// Gives back the running node of the iterator.
685
      /// It is always the source of the pointed arc.
699
      /// This function gives back the running node of the iterator.
700
      /// It is always the source node of the pointed arc.
686 701
      Node runningNode(const InArcIt&) const { return INVALID; }
687 702

	
688 703
      /// \brief The base node of the iterator.
689 704
      ///
690
      /// Gives back the base node of the iterator.
691
      /// It is always the source of the pointed arc.
705
      /// This function gives back the base node of the iterator.
706
      /// It is always the source node of the pointed arc.
692 707
      Node baseNode(const OutArcIt&) const { return INVALID; }
693 708

	
694 709
      /// \brief The running node of the iterator.
695 710
      ///
696
      /// Gives back the running node of the iterator.
697
      /// It is always the target of the pointed arc.
711
      /// This function gives back the running node of the iterator.
712
      /// It is always the target node of the pointed arc.
698 713
      Node runningNode(const OutArcIt&) const { return INVALID; }
699 714

	
700 715
      /// @}
701 716

	
702 717
      template <typename _Digraph>
703 718
      struct Constraints {
704 719
        void constraints() {
705 720
          checkConcept<Base, _Digraph>();
706 721

	
707 722
          {
708 723
            typename _Digraph::Node node(INVALID);
709 724
            typename _Digraph::Arc arc(INVALID);
710 725
            {
711 726
              digraph.first(node);
712 727
              digraph.next(node);
713 728
            }
714 729
            {
715 730
              digraph.first(arc);
716 731
              digraph.next(arc);
717 732
            }
718 733
            {
719 734
              digraph.firstIn(arc, node);
720 735
              digraph.nextIn(arc);
721 736
            }
722 737
            {
723 738
              digraph.firstOut(arc, node);
724 739
              digraph.nextOut(arc);
725 740
            }
726 741
          }
727 742

	
728 743
          {
729 744
            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>,
730 745
              typename _Digraph::ArcIt >();
731 746
            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>,
732 747
              typename _Digraph::NodeIt >();
733 748
            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
734 749
              typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>();
735 750
            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
736 751
              typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>();
737 752

	
738 753
            typename _Digraph::Node n;
739
            typename _Digraph::InArcIt ieit(INVALID);
740
            typename _Digraph::OutArcIt oeit(INVALID);
741
            n = digraph.baseNode(ieit);
742
            n = digraph.runningNode(ieit);
743
            n = digraph.baseNode(oeit);
744
            n = digraph.runningNode(oeit);
754
            const typename _Digraph::InArcIt iait(INVALID);
755
            const typename _Digraph::OutArcIt oait(INVALID);
756
            n = digraph.baseNode(iait);
757
            n = digraph.runningNode(iait);
758
            n = digraph.baseNode(oait);
759
            n = digraph.runningNode(oait);
745 760
            ignore_unused_variable_warning(n);
746 761
          }
747 762
        }
748 763

	
749 764
        const _Digraph& digraph;
750

	
751 765
      };
752 766
    };
753 767

	
754
    /// \brief An empty iterable undirected graph class.
768
    /// \brief Skeleton class for iterable undirected graphs.
755 769
    ///
756
    /// This class provides beside the core graph features iterator
757
    /// based iterable interface for the undirected graph structure.
770
    /// This class describes the interface of iterable undirected
771
    /// graphs. It extends \ref IterableDigraphComponent with the core
772
    /// iterable interface of undirected graphs.
758 773
    /// This concept is part of the Graph concept.
759
    template <typename _Base = BaseGraphComponent>
760
    class IterableGraphComponent : public IterableDigraphComponent<_Base> {
774
    template <typename BAS = BaseGraphComponent>
775
    class IterableGraphComponent : public IterableDigraphComponent<BAS> {
761 776
    public:
762 777

	
763
      typedef _Base Base;
778
      typedef BAS Base;
764 779
      typedef typename Base::Node Node;
765 780
      typedef typename Base::Arc Arc;
766 781
      typedef typename Base::Edge Edge;
767 782

	
768 783

	
769 784
      typedef IterableGraphComponent Graph;
770 785

	
771
      /// \name Base iteration
786
      /// \name Base Iteration
772 787
      ///
773
      /// This interface provides functions for iteration on graph items
788
      /// This interface provides functions for iteration on edges.
789
      ///
774 790
      /// @{
775 791

	
776
      using IterableDigraphComponent<_Base>::first;
777
      using IterableDigraphComponent<_Base>::next;
792
      using IterableDigraphComponent<Base>::first;
793
      using IterableDigraphComponent<Base>::next;
778 794

	
779
      /// \brief Gives back the first edge in the iterating
780
      /// order.
795
      /// \brief Return the first edge.
781 796
      ///
782
      /// Gives back the first edge in the iterating order.
783
      ///
797
      /// This function gives back the first edge in the iteration order.
784 798
      void first(Edge&) const {}
785 799

	
786
      /// \brief Gives back the next edge in the iterating
787
      /// order.
800
      /// \brief Return the next edge.
788 801
      ///
789
      /// Gives back the next edge in the iterating order.
790
      ///
802
      /// This function gives back the next edge in the iteration order.
791 803
      void next(Edge&) const {}
792 804

	
793

	
794
      /// \brief Gives back the first of the edges from the
805
      /// \brief Return the first edge incident to the given node.
806
      ///
807
      /// This function gives back the first edge incident to the given 
808
      /// node. The bool parameter gives back the direction for which the
809
      /// source node of the directed arc representing the edge is the 
795 810
      /// given node.
796
      ///
797
      /// Gives back the first of the edges from the given
798
      /// node. The bool parameter gives back that direction which
799
      /// gives a good direction of the edge so the source of the
800
      /// directed arc is the given node.
801 811
      void firstInc(Edge&, bool&, const Node&) const {}
802 812

	
803 813
      /// \brief Gives back the next of the edges from the
804 814
      /// given node.
805 815
      ///
806
      /// Gives back the next of the edges from the given
807
      /// node. The bool parameter should be used as the \c firstInc()
808
      /// use it.
816
      /// This function gives back the next edge incident to the given 
817
      /// node. The bool parameter should be used as \c firstInc() use it.
809 818
      void nextInc(Edge&, bool&) const {}
810 819

	
811
      using IterableDigraphComponent<_Base>::baseNode;
812
      using IterableDigraphComponent<_Base>::runningNode;
820
      using IterableDigraphComponent<Base>::baseNode;
821
      using IterableDigraphComponent<Base>::runningNode;
813 822

	
814 823
      /// @}
815 824

	
816
      /// \name Class based iteration
825
      /// \name Class Based Iteration
817 826
      ///
818
      /// This interface provides functions for iteration on graph items
827
      /// This interface provides iterator classes for edges.
819 828
      ///
820 829
      /// @{
821 830

	
822
      /// \brief This iterator goes through each node.
831
      /// \brief This iterator goes through each edge.
823 832
      ///
824
      /// This iterator goes through each node.
833
      /// This iterator goes through each edge.
825 834
      typedef GraphItemIt<Graph, Edge> EdgeIt;
826
      /// \brief This iterator goes trough the incident arcs of a
835

	
836
      /// \brief This iterator goes trough the incident edges of a
827 837
      /// node.
828 838
      ///
829
      /// This iterator goes trough the incident arcs of a certain
839
      /// This iterator goes trough the incident edges of a certain
830 840
      /// node of a graph.
831
      typedef GraphIncIt<Graph, Edge, Node, 'u'> IncEdgeIt;
841
      typedef GraphIncIt<Graph, Edge, Node, 'e'> IncEdgeIt;
842

	
832 843
      /// \brief The base node of the iterator.
833 844
      ///
834
      /// Gives back the base node of the iterator.
845
      /// This function gives back the base node of the iterator.
835 846
      Node baseNode(const IncEdgeIt&) const { return INVALID; }
836 847

	
837 848
      /// \brief The running node of the iterator.
838 849
      ///
839
      /// Gives back the running node of the iterator.
850
      /// This function gives back the running node of the iterator.
840 851
      Node runningNode(const IncEdgeIt&) const { return INVALID; }
841 852

	
842 853
      /// @}
843 854

	
844 855
      template <typename _Graph>
845 856
      struct Constraints {
846 857
        void constraints() {
847 858
          checkConcept<IterableDigraphComponent<Base>, _Graph>();
848 859

	
849 860
          {
850 861
            typename _Graph::Node node(INVALID);
851 862
            typename _Graph::Edge edge(INVALID);
852 863
            bool dir;
853 864
            {
854 865
              graph.first(edge);
855 866
              graph.next(edge);
856 867
            }
857 868
            {
858 869
              graph.firstInc(edge, dir, node);
859 870
              graph.nextInc(edge, dir);
860 871
            }
861 872

	
862 873
          }
863 874

	
864 875
          {
865 876
            checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>,
866 877
              typename _Graph::EdgeIt >();
867 878
            checkConcept<GraphIncIt<_Graph, typename _Graph::Edge,
868
              typename _Graph::Node, 'u'>, typename _Graph::IncEdgeIt>();
879
              typename _Graph::Node, 'e'>, typename _Graph::IncEdgeIt>();
869 880

	
870 881
            typename _Graph::Node n;
871
            typename _Graph::IncEdgeIt ueit(INVALID);
872
            n = graph.baseNode(ueit);
873
            n = graph.runningNode(ueit);
882
            const typename _Graph::IncEdgeIt ieit(INVALID);
883
            n = graph.baseNode(ieit);
884
            n = graph.runningNode(ieit);
874 885
          }
875 886
        }
876 887

	
877 888
        const _Graph& graph;
878

	
879 889
      };
880 890
    };
881 891

	
882
    /// \brief An empty alteration notifier digraph class.
892
    /// \brief Skeleton class for alterable directed graphs.
883 893
    ///
884
    /// This class provides beside the core digraph features alteration
885
    /// notifier interface for the digraph structure.  This implements
894
    /// This class describes the interface of alterable directed
895
    /// graphs. It extends \ref BaseDigraphComponent with the alteration
896
    /// notifier interface. It implements
886 897
    /// an observer-notifier pattern for each digraph item. More
887 898
    /// obsevers can be registered into the notifier and whenever an
888
    /// alteration occured in the digraph all the observers will
899
    /// alteration occured in the digraph all the observers will be
889 900
    /// notified about it.
890
    template <typename _Base = BaseDigraphComponent>
891
    class AlterableDigraphComponent : public _Base {
901
    template <typename BAS = BaseDigraphComponent>
902
    class AlterableDigraphComponent : public BAS {
892 903
    public:
893 904

	
894
      typedef _Base Base;
905
      typedef BAS Base;
895 906
      typedef typename Base::Node Node;
896 907
      typedef typename Base::Arc Arc;
897 908

	
898 909

	
899
      /// The node observer registry.
910
      /// Node alteration notifier class.
900 911
      typedef AlterationNotifier<AlterableDigraphComponent, Node>
901 912
      NodeNotifier;
902
      /// The arc observer registry.
913
      /// Arc alteration notifier class.
903 914
      typedef AlterationNotifier<AlterableDigraphComponent, Arc>
904 915
      ArcNotifier;
905 916

	
906
      /// \brief Gives back the node alteration notifier.
917
      /// \brief Return the node alteration notifier.
907 918
      ///
908
      /// Gives back the node alteration notifier.
919
      /// This function gives back the node alteration notifier.
909 920
      NodeNotifier& notifier(Node) const {
910
        return NodeNotifier();
921
         return NodeNotifier();
911 922
      }
912 923

	
913
      /// \brief Gives back the arc alteration notifier.
924
      /// \brief Return the arc alteration notifier.
914 925
      ///
915
      /// Gives back the arc alteration notifier.
926
      /// This function gives back the arc alteration notifier.
916 927
      ArcNotifier& notifier(Arc) const {
917 928
        return ArcNotifier();
918 929
      }
919 930

	
920 931
      template <typename _Digraph>
921 932
      struct Constraints {
922 933
        void constraints() {
923 934
          checkConcept<Base, _Digraph>();
924 935
          typename _Digraph::NodeNotifier& nn
925 936
            = digraph.notifier(typename _Digraph::Node());
926 937

	
927 938
          typename _Digraph::ArcNotifier& en
928 939
            = digraph.notifier(typename _Digraph::Arc());
929 940

	
930 941
          ignore_unused_variable_warning(nn);
931 942
          ignore_unused_variable_warning(en);
932 943
        }
933 944

	
934 945
        const _Digraph& digraph;
935

	
936 946
      };
937

	
938 947
    };
939 948

	
940
    /// \brief An empty alteration notifier undirected graph class.
949
    /// \brief Skeleton class for alterable undirected graphs.
941 950
    ///
942
    /// This class provides beside the core graph features alteration
943
    /// notifier interface for the graph structure.  This implements
944
    /// an observer-notifier pattern for each graph item. More
951
    /// This class describes the interface of alterable undirected
952
    /// graphs. It extends \ref AlterableDigraphComponent with the alteration
953
    /// notifier interface of undirected graphs. It implements
954
    /// an observer-notifier pattern for the edges. More
945 955
    /// obsevers can be registered into the notifier and whenever an
946
    /// alteration occured in the graph all the observers will
956
    /// alteration occured in the graph all the observers will be
947 957
    /// notified about it.
948
    template <typename _Base = BaseGraphComponent>
949
    class AlterableGraphComponent : public AlterableDigraphComponent<_Base> {
958
    template <typename BAS = BaseGraphComponent>
959
    class AlterableGraphComponent : public AlterableDigraphComponent<BAS> {
950 960
    public:
951 961

	
952
      typedef _Base Base;
962
      typedef BAS Base;
953 963
      typedef typename Base::Edge Edge;
954 964

	
955 965

	
956
      /// The arc observer registry.
966
      /// Edge alteration notifier class.
957 967
      typedef AlterationNotifier<AlterableGraphComponent, Edge>
958 968
      EdgeNotifier;
959 969

	
960
      /// \brief Gives back the arc alteration notifier.
970
      /// \brief Return the edge alteration notifier.
961 971
      ///
962
      /// Gives back the arc alteration notifier.
972
      /// This function gives back the edge alteration notifier.
963 973
      EdgeNotifier& notifier(Edge) const {
964 974
        return EdgeNotifier();
965 975
      }
966 976

	
967 977
      template <typename _Graph>
968 978
      struct Constraints {
969 979
        void constraints() {
970
          checkConcept<AlterableGraphComponent<Base>, _Graph>();
980
          checkConcept<AlterableDigraphComponent<Base>, _Graph>();
971 981
          typename _Graph::EdgeNotifier& uen
972 982
            = graph.notifier(typename _Graph::Edge());
973 983
          ignore_unused_variable_warning(uen);
974 984
        }
975 985

	
976 986
        const _Graph& graph;
977

	
978 987
      };
979

	
980 988
    };
981 989

	
982
    /// \brief Class describing the concept of graph maps
990
    /// \brief Concept class for standard graph maps.
983 991
    ///
984
    /// This class describes the common interface of the graph maps
985
    /// (NodeMap, ArcMap), that is maps that can be used to
986
    /// associate data to graph descriptors (nodes or arcs).
987
    template <typename _Graph, typename _Item, typename _Value>
988
    class GraphMap : public ReadWriteMap<_Item, _Value> {
992
    /// This class describes the concept of standard graph maps, i.e.
993
    /// the \c NodeMap, \c ArcMap and \c EdgeMap subtypes of digraph and 
994
    /// graph types, which can be used for associating data to graph items.
995
    /// The standard graph maps must conform to the ReferenceMap concept.
996
    template <typename GR, typename K, typename V>
997
    class GraphMap : public ReferenceMap<K, V, V&, const V&> {
998
      typedef ReferenceMap<K, V, V&, const V&> Parent;
999

	
989 1000
    public:
990 1001

	
991
      typedef ReadWriteMap<_Item, _Value> Parent;
1002
      /// The key type of the map.
1003
      typedef K Key;
1004
      /// The value type of the map.
1005
      typedef V Value;
1006
      /// The reference type of the map.
1007
      typedef Value& Reference;
1008
      /// The const reference type of the map.
1009
      typedef const Value& ConstReference;
992 1010

	
993
      /// The graph type of the map.
994
      typedef _Graph Graph;
995
      /// The key type of the map.
996
      typedef _Item Key;
997
      /// The value type of the map.
998
      typedef _Value Value;
1011
      // The reference map tag.
1012
      typedef True ReferenceMapTag;
999 1013

	
1000 1014
      /// \brief Construct a new map.
1001 1015
      ///
1002 1016
      /// Construct a new map for the graph.
1003
      explicit GraphMap(const Graph&) {}
1017
      explicit GraphMap(const GR&) {}
1004 1018
      /// \brief Construct a new map with default value.
1005 1019
      ///
1006
      /// Construct a new map for the graph and initalise the values.
1007
      GraphMap(const Graph&, const Value&) {}
1020
      /// Construct a new map for the graph and initalize the values.
1021
      GraphMap(const GR&, const Value&) {}
1008 1022

	
1009 1023
    private:
1010 1024
      /// \brief Copy constructor.
1011 1025
      ///
1012 1026
      /// Copy Constructor.
1013 1027
      GraphMap(const GraphMap&) : Parent() {}
1014 1028

	
1015
      /// \brief Assign operator.
1029
      /// \brief Assignment operator.
1016 1030
      ///
1017
      /// Assign operator. It does not mofify the underlying graph,
1031
      /// Assignment operator. It does not mofify the underlying graph,
1018 1032
      /// it just iterates on the current item set and set the  map
1019 1033
      /// with the value returned by the assigned map.
1020 1034
      template <typename CMap>
1021 1035
      GraphMap& operator=(const CMap&) {
1022 1036
        checkConcept<ReadMap<Key, Value>, CMap>();
1023 1037
        return *this;
1024 1038
      }
1025 1039

	
1026 1040
    public:
1027 1041
      template<typename _Map>
1028 1042
      struct Constraints {
1029 1043
        void constraints() {
1030
          checkConcept<ReadWriteMap<Key, Value>, _Map >();
1031
          // Construction with a graph parameter
1032
          _Map a(g);
1033
          // Constructor with a graph and a default value parameter
1034
          _Map a2(g,t);
1035
          // Copy constructor.
1036
          // _Map b(c);
1044
          checkConcept
1045
            <ReferenceMap<Key, Value, Value&, const Value&>, _Map>();
1046
          _Map m1(g);
1047
          _Map m2(g,t);
1048
          
1049
          // Copy constructor
1050
          // _Map m3(m);
1037 1051

	
1052
          // Assignment operator
1038 1053
          // ReadMap<Key, Value> cmap;
1039
          // b = cmap;
1054
          // m3 = cmap;
1040 1055

	
1041
          ignore_unused_variable_warning(a);
1042
          ignore_unused_variable_warning(a2);
1043
          // ignore_unused_variable_warning(b);
1056
          ignore_unused_variable_warning(m1);
1057
          ignore_unused_variable_warning(m2);
1058
          // ignore_unused_variable_warning(m3);
1044 1059
        }
1045 1060

	
1046
        const _Map &c;
1047
        const Graph &g;
1061
        const _Map &m;
1062
        const GR &g;
1048 1063
        const typename GraphMap::Value &t;
1049 1064
      };
1050 1065

	
1051 1066
    };
1052 1067

	
1053
    /// \brief An empty mappable digraph class.
1068
    /// \brief Skeleton class for mappable directed graphs.
1054 1069
    ///
1055
    /// This class provides beside the core digraph features
1056
    /// map interface for the digraph structure.
1070
    /// This class describes the interface of mappable directed graphs.
1071
    /// It extends \ref BaseDigraphComponent with the standard digraph 
1072
    /// map classes, namely \c NodeMap and \c ArcMap.
1057 1073
    /// This concept is part of the Digraph concept.
1058
    template <typename _Base = BaseDigraphComponent>
1059
    class MappableDigraphComponent : public _Base  {
1074
    template <typename BAS = BaseDigraphComponent>
1075
    class MappableDigraphComponent : public BAS  {
1060 1076
    public:
1061 1077

	
1062
      typedef _Base Base;
1078
      typedef BAS Base;
1063 1079
      typedef typename Base::Node Node;
1064 1080
      typedef typename Base::Arc Arc;
1065 1081

	
1066 1082
      typedef MappableDigraphComponent Digraph;
1067 1083

	
1068
      /// \brief ReadWrite map of the nodes.
1084
      /// \brief Standard graph map for the nodes.
1069 1085
      ///
1070
      /// ReadWrite map of the nodes.
1071
      ///
1072
      template <typename _Value>
1073
      class NodeMap : public GraphMap<Digraph, Node, _Value> {
1086
      /// Standard graph map for the nodes.
1087
      /// It conforms to the ReferenceMap concept.
1088
      template <typename V>
1089
      class NodeMap : public GraphMap<MappableDigraphComponent, Node, V> {
1090
        typedef GraphMap<MappableDigraphComponent, Node, V> Parent;
1091

	
1074 1092
      public:
1075
        typedef GraphMap<MappableDigraphComponent, Node, _Value> Parent;
1076

	
1077 1093
        /// \brief Construct a new map.
1078 1094
        ///
1079 1095
        /// Construct a new map for the digraph.
1080 1096
        explicit NodeMap(const MappableDigraphComponent& digraph)
1081 1097
          : Parent(digraph) {}
1082 1098

	
1083 1099
        /// \brief Construct a new map with default value.
1084 1100
        ///
1085
        /// Construct a new map for the digraph and initalise the values.
1086
        NodeMap(const MappableDigraphComponent& digraph, const _Value& value)
1101
        /// Construct a new map for the digraph and initalize the values.
1102
        NodeMap(const MappableDigraphComponent& digraph, const V& value)
1087 1103
          : Parent(digraph, value) {}
1088 1104

	
1089 1105
      private:
1090 1106
        /// \brief Copy constructor.
1091 1107
        ///
1092 1108
        /// Copy Constructor.
1093 1109
        NodeMap(const NodeMap& nm) : Parent(nm) {}
1094 1110

	
1095
        /// \brief Assign operator.
1111
        /// \brief Assignment operator.
1096 1112
        ///
1097
        /// Assign operator.
1113
        /// Assignment operator.
1098 1114
        template <typename CMap>
1099 1115
        NodeMap& operator=(const CMap&) {
1100
          checkConcept<ReadMap<Node, _Value>, CMap>();
1116
          checkConcept<ReadMap<Node, V>, CMap>();
1101 1117
          return *this;
1102 1118
        }
1103 1119

	
1104 1120
      };
1105 1121

	
1106
      /// \brief ReadWrite map of the arcs.
1122
      /// \brief Standard graph map for the arcs.
1107 1123
      ///
1108
      /// ReadWrite map of the arcs.
1109
      ///
1110
      template <typename _Value>
1111
      class ArcMap : public GraphMap<Digraph, Arc, _Value> {
1124
      /// Standard graph map for the arcs.
1125
      /// It conforms to the ReferenceMap concept.
1126
      template <typename V>
1127
      class ArcMap : public GraphMap<MappableDigraphComponent, Arc, V> {
1128
        typedef GraphMap<MappableDigraphComponent, Arc, V> Parent;
1129

	
1112 1130
      public:
1113
        typedef GraphMap<MappableDigraphComponent, Arc, _Value> Parent;
1114

	
1115 1131
        /// \brief Construct a new map.
1116 1132
        ///
1117 1133
        /// Construct a new map for the digraph.
1118 1134
        explicit ArcMap(const MappableDigraphComponent& digraph)
1119 1135
          : Parent(digraph) {}
1120 1136

	
1121 1137
        /// \brief Construct a new map with default value.
1122 1138
        ///
1123
        /// Construct a new map for the digraph and initalise the values.
1124
        ArcMap(const MappableDigraphComponent& digraph, const _Value& value)
1139
        /// Construct a new map for the digraph and initalize the values.
1140
        ArcMap(const MappableDigraphComponent& digraph, const V& value)
1125 1141
          : Parent(digraph, value) {}
1126 1142

	
1127 1143
      private:
1128 1144
        /// \brief Copy constructor.
1129 1145
        ///
1130 1146
        /// Copy Constructor.
1131 1147
        ArcMap(const ArcMap& nm) : Parent(nm) {}
1132 1148

	
1133
        /// \brief Assign operator.
1149
        /// \brief Assignment operator.
1134 1150
        ///
1135
        /// Assign operator.
1151
        /// Assignment operator.
1136 1152
        template <typename CMap>
1137 1153
        ArcMap& operator=(const CMap&) {
1138
          checkConcept<ReadMap<Arc, _Value>, CMap>();
1154
          checkConcept<ReadMap<Arc, V>, CMap>();
1139 1155
          return *this;
1140 1156
        }
1141 1157

	
1142 1158
      };
1143 1159

	
1144 1160

	
1145 1161
      template <typename _Digraph>
1146 1162
      struct Constraints {
1147 1163

	
1148 1164
        struct Dummy {
1149 1165
          int value;
1150 1166
          Dummy() : value(0) {}
1151 1167
          Dummy(int _v) : value(_v) {}
1152 1168
        };
1153 1169

	
1154 1170
        void constraints() {
1155 1171
          checkConcept<Base, _Digraph>();
1156 1172
          { // int map test
1157 1173
            typedef typename _Digraph::template NodeMap<int> IntNodeMap;
1158 1174
            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>,
1159 1175
              IntNodeMap >();
1160 1176
          } { // bool map test
1161 1177
            typedef typename _Digraph::template NodeMap<bool> BoolNodeMap;
1162 1178
            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>,
1163 1179
              BoolNodeMap >();
1164 1180
          } { // Dummy map test
1165 1181
            typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap;
1166 1182
            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>,
1167 1183
              DummyNodeMap >();
1168 1184
          }
1169 1185

	
1170 1186
          { // int map test
1171 1187
            typedef typename _Digraph::template ArcMap<int> IntArcMap;
1172 1188
            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>,
1173 1189
              IntArcMap >();
1174 1190
          } { // bool map test
1175 1191
            typedef typename _Digraph::template ArcMap<bool> BoolArcMap;
1176 1192
            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>,
1177 1193
              BoolArcMap >();
1178 1194
          } { // Dummy map test
1179 1195
            typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap;
1180 1196
            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>,
1181 1197
              DummyArcMap >();
1182 1198
          }
1183 1199
        }
1184 1200

	
1185
        _Digraph& digraph;
1201
        const _Digraph& digraph;
1186 1202
      };
1187 1203
    };
1188 1204

	
1189
    /// \brief An empty mappable base bipartite graph class.
1205
    /// \brief Skeleton class for mappable undirected graphs.
1190 1206
    ///
1191
    /// This class provides beside the core graph features
1192
    /// map interface for the graph structure.
1207
    /// This class describes the interface of mappable undirected graphs.
1208
    /// It extends \ref MappableDigraphComponent with the standard graph 
1209
    /// map class for edges (\c EdgeMap).
1193 1210
    /// This concept is part of the Graph concept.
1194
    template <typename _Base = BaseGraphComponent>
1195
    class MappableGraphComponent : public MappableDigraphComponent<_Base>  {
1211
    template <typename BAS = BaseGraphComponent>
1212
    class MappableGraphComponent : public MappableDigraphComponent<BAS>  {
1196 1213
    public:
1197 1214

	
1198
      typedef _Base Base;
1215
      typedef BAS Base;
1199 1216
      typedef typename Base::Edge Edge;
1200 1217

	
1201 1218
      typedef MappableGraphComponent Graph;
1202 1219

	
1203
      /// \brief ReadWrite map of the edges.
1220
      /// \brief Standard graph map for the edges.
1204 1221
      ///
1205
      /// ReadWrite map of the edges.
1206
      ///
1207
      template <typename _Value>
1208
      class EdgeMap : public GraphMap<Graph, Edge, _Value> {
1222
      /// Standard graph map for the edges.
1223
      /// It conforms to the ReferenceMap concept.
1224
      template <typename V>
1225
      class EdgeMap : public GraphMap<MappableGraphComponent, Edge, V> {
1226
        typedef GraphMap<MappableGraphComponent, Edge, V> Parent;
1227

	
1209 1228
      public:
1210
        typedef GraphMap<MappableGraphComponent, Edge, _Value> Parent;
1211

	
1212 1229
        /// \brief Construct a new map.
1213 1230
        ///
1214 1231
        /// Construct a new map for the graph.
1215 1232
        explicit EdgeMap(const MappableGraphComponent& graph)
1216 1233
          : Parent(graph) {}
1217 1234

	
1218 1235
        /// \brief Construct a new map with default value.
1219 1236
        ///
1220
        /// Construct a new map for the graph and initalise the values.
1221
        EdgeMap(const MappableGraphComponent& graph, const _Value& value)
1237
        /// Construct a new map for the graph and initalize the values.
1238
        EdgeMap(const MappableGraphComponent& graph, const V& value)
1222 1239
          : Parent(graph, value) {}
1223 1240

	
1224 1241
      private:
1225 1242
        /// \brief Copy constructor.
1226 1243
        ///
1227 1244
        /// Copy Constructor.
1228 1245
        EdgeMap(const EdgeMap& nm) : Parent(nm) {}
1229 1246

	
1230
        /// \brief Assign operator.
1247
        /// \brief Assignment operator.
1231 1248
        ///
1232
        /// Assign operator.
1249
        /// Assignment operator.
1233 1250
        template <typename CMap>
1234 1251
        EdgeMap& operator=(const CMap&) {
1235
          checkConcept<ReadMap<Edge, _Value>, CMap>();
1252
          checkConcept<ReadMap<Edge, V>, CMap>();
1236 1253
          return *this;
1237 1254
        }
1238 1255

	
1239 1256
      };
1240 1257

	
1241 1258

	
1242 1259
      template <typename _Graph>
1243 1260
      struct Constraints {
1244 1261

	
1245 1262
        struct Dummy {
1246 1263
          int value;
1247 1264
          Dummy() : value(0) {}
1248 1265
          Dummy(int _v) : value(_v) {}
1249 1266
        };
1250 1267

	
1251 1268
        void constraints() {
1252
          checkConcept<MappableGraphComponent<Base>, _Graph>();
1269
          checkConcept<MappableDigraphComponent<Base>, _Graph>();
1253 1270

	
1254 1271
          { // int map test
1255 1272
            typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
1256 1273
            checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
1257 1274
              IntEdgeMap >();
1258 1275
          } { // bool map test
1259 1276
            typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
1260 1277
            checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
1261 1278
              BoolEdgeMap >();
1262 1279
          } { // Dummy map test
1263 1280
            typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap;
1264 1281
            checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>,
1265 1282
              DummyEdgeMap >();
1266 1283
          }
1267 1284
        }
1268 1285

	
1269
        _Graph& graph;
1286
        const _Graph& graph;
1270 1287
      };
1271 1288
    };
1272 1289

	
1273
    /// \brief An empty extendable digraph class.
1290
    /// \brief Skeleton class for extendable directed graphs.
1274 1291
    ///
1275
    /// This class provides beside the core digraph features digraph
1276
    /// extendable interface for the digraph structure.  The main
1277
    /// difference between the base and this interface is that the
1278
    /// digraph alterations should handled already on this level.
1279
    template <typename _Base = BaseDigraphComponent>
1280
    class ExtendableDigraphComponent : public _Base {
1292
    /// This class describes the interface of extendable directed graphs.
1293
    /// It extends \ref BaseDigraphComponent with functions for adding 
1294
    /// nodes and arcs to the digraph.
1295
    /// This concept requires \ref AlterableDigraphComponent.
1296
    template <typename BAS = BaseDigraphComponent>
1297
    class ExtendableDigraphComponent : public BAS {
1281 1298
    public:
1282
      typedef _Base Base;
1299
      typedef BAS Base;
1283 1300

	
1284
      typedef typename _Base::Node Node;
1285
      typedef typename _Base::Arc Arc;
1301
      typedef typename Base::Node Node;
1302
      typedef typename Base::Arc Arc;
1286 1303

	
1287
      /// \brief Adds a new node to the digraph.
1304
      /// \brief Add a new node to the digraph.
1288 1305
      ///
1289
      /// Adds a new node to the digraph.
1290
      ///
1306
      /// This function adds a new node to the digraph.
1291 1307
      Node addNode() {
1292 1308
        return INVALID;
1293 1309
      }
1294 1310

	
1295
      /// \brief Adds a new arc connects the given two nodes.
1311
      /// \brief Add a new arc connecting the given two nodes.
1296 1312
      ///
1297
      /// Adds a new arc connects the the given two nodes.
1313
      /// This function adds a new arc connecting the given two nodes
1314
      /// of the digraph.
1298 1315
      Arc addArc(const Node&, const Node&) {
1299 1316
        return INVALID;
1300 1317
      }
1301 1318

	
1302 1319
      template <typename _Digraph>
1303 1320
      struct Constraints {
1304 1321
        void constraints() {
1305 1322
          checkConcept<Base, _Digraph>();
1306 1323
          typename _Digraph::Node node_a, node_b;
1307 1324
          node_a = digraph.addNode();
1308 1325
          node_b = digraph.addNode();
1309 1326
          typename _Digraph::Arc arc;
1310 1327
          arc = digraph.addArc(node_a, node_b);
1311 1328
        }
1312 1329

	
1313 1330
        _Digraph& digraph;
1314 1331
      };
1315 1332
    };
1316 1333

	
1317
    /// \brief An empty extendable base undirected graph class.
1334
    /// \brief Skeleton class for extendable undirected graphs.
1318 1335
    ///
1319
    /// This class provides beside the core undirected graph features
1320
    /// core undircted graph extend interface for the graph structure.
1321
    /// The main difference between the base and this interface is
1322
    /// that the graph alterations should handled already on this
1323
    /// level.
1324
    template <typename _Base = BaseGraphComponent>
1325
    class ExtendableGraphComponent : public _Base {
1336
    /// This class describes the interface of extendable undirected graphs.
1337
    /// It extends \ref BaseGraphComponent with functions for adding 
1338
    /// nodes and edges to the graph.
1339
    /// This concept requires \ref AlterableGraphComponent.
1340
    template <typename BAS = BaseGraphComponent>
1341
    class ExtendableGraphComponent : public BAS {
1326 1342
    public:
1327 1343

	
1328
      typedef _Base Base;
1329
      typedef typename _Base::Node Node;
1330
      typedef typename _Base::Edge Edge;
1344
      typedef BAS Base;
1345
      typedef typename Base::Node Node;
1346
      typedef typename Base::Edge Edge;
1331 1347

	
1332
      /// \brief Adds a new node to the graph.
1348
      /// \brief Add a new node to the digraph.
1333 1349
      ///
1334
      /// Adds a new node to the graph.
1335
      ///
1350
      /// This function adds a new node to the digraph.
1336 1351
      Node addNode() {
1337 1352
        return INVALID;
1338 1353
      }
1339 1354

	
1340
      /// \brief Adds a new arc connects the given two nodes.
1355
      /// \brief Add a new edge connecting the given two nodes.
1341 1356
      ///
1342
      /// Adds a new arc connects the the given two nodes.
1343
      Edge addArc(const Node&, const Node&) {
1357
      /// This function adds a new edge connecting the given two nodes
1358
      /// of the graph.
1359
      Edge addEdge(const Node&, const Node&) {
1344 1360
        return INVALID;
1345 1361
      }
1346 1362

	
1347 1363
      template <typename _Graph>
1348 1364
      struct Constraints {
1349 1365
        void constraints() {
1350 1366
          checkConcept<Base, _Graph>();
1351 1367
          typename _Graph::Node node_a, node_b;
1352 1368
          node_a = graph.addNode();
1353 1369
          node_b = graph.addNode();
1354 1370
          typename _Graph::Edge edge;
1355 1371
          edge = graph.addEdge(node_a, node_b);
1356 1372
        }
1357 1373

	
1358 1374
        _Graph& graph;
1359 1375
      };
1360 1376
    };
1361 1377

	
1362
    /// \brief An empty erasable digraph class.
1378
    /// \brief Skeleton class for erasable directed graphs.
1363 1379
    ///
1364
    /// This class provides beside the core digraph features core erase
1365
    /// functions for the digraph structure. The main difference between
1366
    /// the base and this interface is that the digraph alterations
1367
    /// should handled already on this level.
1368
    template <typename _Base = BaseDigraphComponent>
1369
    class ErasableDigraphComponent : public _Base {
1380
    /// This class describes the interface of erasable directed graphs.
1381
    /// It extends \ref BaseDigraphComponent with functions for removing 
1382
    /// nodes and arcs from the digraph.
1383
    /// This concept requires \ref AlterableDigraphComponent.
1384
    template <typename BAS = BaseDigraphComponent>
1385
    class ErasableDigraphComponent : public BAS {
1370 1386
    public:
1371 1387

	
1372
      typedef _Base Base;
1388
      typedef BAS Base;
1373 1389
      typedef typename Base::Node Node;
1374 1390
      typedef typename Base::Arc Arc;
1375 1391

	
1376 1392
      /// \brief Erase a node from the digraph.
1377 1393
      ///
1378
      /// Erase a node from the digraph. This function should
1379
      /// erase all arcs connecting to the node.
1394
      /// This function erases the given node from the digraph and all arcs 
1395
      /// connected to the node.
1380 1396
      void erase(const Node&) {}
1381 1397

	
1382 1398
      /// \brief Erase an arc from the digraph.
1383 1399
      ///
1384
      /// Erase an arc from the digraph.
1385
      ///
1400
      /// This function erases the given arc from the digraph.
1386 1401
      void erase(const Arc&) {}
1387 1402

	
1388 1403
      template <typename _Digraph>
1389 1404
      struct Constraints {
1390 1405
        void constraints() {
1391 1406
          checkConcept<Base, _Digraph>();
1392
          typename _Digraph::Node node;
1407
          const typename _Digraph::Node node(INVALID);
1393 1408
          digraph.erase(node);
1394
          typename _Digraph::Arc arc;
1409
          const typename _Digraph::Arc arc(INVALID);
1395 1410
          digraph.erase(arc);
1396 1411
        }
1397 1412

	
1398 1413
        _Digraph& digraph;
1399 1414
      };
1400 1415
    };
1401 1416

	
1402
    /// \brief An empty erasable base undirected graph class.
1417
    /// \brief Skeleton class for erasable undirected graphs.
1403 1418
    ///
1404
    /// This class provides beside the core undirected graph features
1405
    /// core erase functions for the undirceted graph structure. The
1406
    /// main difference between the base and this interface is that
1407
    /// the graph alterations should handled already on this level.
1408
    template <typename _Base = BaseGraphComponent>
1409
    class ErasableGraphComponent : public _Base {
1419
    /// This class describes the interface of erasable undirected graphs.
1420
    /// It extends \ref BaseGraphComponent with functions for removing 
1421
    /// nodes and edges from the graph.
1422
    /// This concept requires \ref AlterableGraphComponent.
1423
    template <typename BAS = BaseGraphComponent>
1424
    class ErasableGraphComponent : public BAS {
1410 1425
    public:
1411 1426

	
1412
      typedef _Base Base;
1427
      typedef BAS Base;
1413 1428
      typedef typename Base::Node Node;
1414 1429
      typedef typename Base::Edge Edge;
1415 1430

	
1416 1431
      /// \brief Erase a node from the graph.
1417 1432
      ///
1418
      /// Erase a node from the graph. This function should erase
1419
      /// arcs connecting to the node.
1433
      /// This function erases the given node from the graph and all edges
1434
      /// connected to the node.
1420 1435
      void erase(const Node&) {}
1421 1436

	
1422
      /// \brief Erase an arc from the graph.
1437
      /// \brief Erase an edge from the digraph.
1423 1438
      ///
1424
      /// Erase an arc from the graph.
1425
      ///
1439
      /// This function erases the given edge from the digraph.
1426 1440
      void erase(const Edge&) {}
1427 1441

	
1428 1442
      template <typename _Graph>
1429 1443
      struct Constraints {
1430 1444
        void constraints() {
1431 1445
          checkConcept<Base, _Graph>();
1432
          typename _Graph::Node node;
1446
          const typename _Graph::Node node(INVALID);
1433 1447
          graph.erase(node);
1434
          typename _Graph::Edge edge;
1448
          const typename _Graph::Edge edge(INVALID);
1435 1449
          graph.erase(edge);
1436 1450
        }
1437 1451

	
1438 1452
        _Graph& graph;
1439 1453
      };
1440 1454
    };
1441 1455

	
1442
    /// \brief An empty clearable base digraph class.
1456
    /// \brief Skeleton class for clearable directed graphs.
1443 1457
    ///
1444
    /// This class provides beside the core digraph features core clear
1445
    /// functions for the digraph structure. The main difference between
1446
    /// the base and this interface is that the digraph alterations
1447
    /// should handled already on this level.
1448
    template <typename _Base = BaseDigraphComponent>
1449
    class ClearableDigraphComponent : public _Base {
1458
    /// This class describes the interface of clearable directed graphs.
1459
    /// It extends \ref BaseDigraphComponent with a function for clearing
1460
    /// the digraph.
1461
    /// This concept requires \ref AlterableDigraphComponent.
1462
    template <typename BAS = BaseDigraphComponent>
1463
    class ClearableDigraphComponent : public BAS {
1450 1464
    public:
1451 1465

	
1452
      typedef _Base Base;
1466
      typedef BAS Base;
1453 1467

	
1454 1468
      /// \brief Erase all nodes and arcs from the digraph.
1455 1469
      ///
1456
      /// Erase all nodes and arcs from the digraph.
1457
      ///
1470
      /// This function erases all nodes and arcs from the digraph.
1458 1471
      void clear() {}
1459 1472

	
1460 1473
      template <typename _Digraph>
1461 1474
      struct Constraints {
1462 1475
        void constraints() {
1463 1476
          checkConcept<Base, _Digraph>();
1464 1477
          digraph.clear();
1465 1478
        }
1466 1479

	
1467
        _Digraph digraph;
1480
        _Digraph& digraph;
1468 1481
      };
1469 1482
    };
1470 1483

	
1471
    /// \brief An empty clearable base undirected graph class.
1484
    /// \brief Skeleton class for clearable undirected graphs.
1472 1485
    ///
1473
    /// This class provides beside the core undirected graph features
1474
    /// core clear functions for the undirected graph structure. The
1475
    /// main difference between the base and this interface is that
1476
    /// the graph alterations should handled already on this level.
1477
    template <typename _Base = BaseGraphComponent>
1478
    class ClearableGraphComponent : public ClearableDigraphComponent<_Base> {
1486
    /// This class describes the interface of clearable undirected graphs.
1487
    /// It extends \ref BaseGraphComponent with a function for clearing
1488
    /// the graph.
1489
    /// This concept requires \ref AlterableGraphComponent.
1490
    template <typename BAS = BaseGraphComponent>
1491
    class ClearableGraphComponent : public ClearableDigraphComponent<BAS> {
1479 1492
    public:
1480 1493

	
1481
      typedef _Base Base;
1494
      typedef BAS Base;
1495

	
1496
      /// \brief Erase all nodes and edges from the graph.
1497
      ///
1498
      /// This function erases all nodes and edges from the graph.
1499
      void clear() {}
1482 1500

	
1483 1501
      template <typename _Graph>
1484 1502
      struct Constraints {
1485 1503
        void constraints() {
1486
          checkConcept<ClearableGraphComponent<Base>, _Graph>();
1504
          checkConcept<Base, _Graph>();
1505
          graph.clear();
1487 1506
        }
1488 1507

	
1489
        _Graph graph;
1508
        _Graph& graph;
1490 1509
      };
1491 1510
    };
1492 1511

	
1493 1512
  }
1494 1513

	
1495 1514
}
1496 1515

	
1497 1516
#endif
Ignore white space 6 line context
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-2009
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
#ifndef LEMON_CONCEPTS_HEAP_H
20
#define LEMON_CONCEPTS_HEAP_H
21

	
19 22
///\ingroup concept
20 23
///\file
21 24
///\brief The concept of heaps.
22 25

	
23
#ifndef LEMON_CONCEPT_HEAP_H
24
#define LEMON_CONCEPT_HEAP_H
25

	
26 26
#include <lemon/core.h>
27
#include <lemon/concept_check.h>
27 28

	
28 29
namespace lemon {
29 30

	
30 31
  namespace concepts {
31 32

	
32 33
    /// \addtogroup concept
33 34
    /// @{
34 35

	
35 36
    /// \brief The heap concept.
36 37
    ///
37
    /// Concept class describing the main interface of heaps.
38
    template <typename Priority, typename ItemIntMap>
38
    /// This concept class describes the main interface of heaps.
39
    /// The various \ref heaps "heap structures" are efficient
40
    /// implementations of the abstract data type \e priority \e queue.
41
    /// They store items with specified values called \e priorities
42
    /// in such a way that finding and removing the item with minimum
43
    /// priority are efficient. The basic operations are adding and
44
    /// erasing items, changing the priority of an item, etc.
45
    ///
46
    /// Heaps are crucial in several algorithms, such as Dijkstra and Prim.
47
    /// Any class that conforms to this concept can be used easily in such
48
    /// algorithms.
49
    ///
50
    /// \tparam PR Type of the priorities of the items.
51
    /// \tparam IM A read-writable item map with \c int values, used
52
    /// internally to handle the cross references.
53
    /// \tparam CMP A functor class for comparing the priorities.
54
    /// The default is \c std::less<PR>.
55
#ifdef DOXYGEN
56
    template <typename PR, typename IM, typename CMP>
57
#else
58
    template <typename PR, typename IM, typename CMP = std::less<PR> >
59
#endif
39 60
    class Heap {
40 61
    public:
41 62

	
63
      /// Type of the item-int map.
64
      typedef IM ItemIntMap;
65
      /// Type of the priorities.
66
      typedef PR Prio;
42 67
      /// Type of the items stored in the heap.
43 68
      typedef typename ItemIntMap::Key Item;
44 69

	
45
      /// Type of the priorities.
46
      typedef Priority Prio;
47

	
48 70
      /// \brief Type to represent the states of the items.
49 71
      ///
50 72
      /// Each item has a state associated to it. It can be "in heap",
51
      /// "pre heap" or "post heap". The later two are indifferent
52
      /// from the point of view of the heap, but may be useful for
53
      /// the user.
73
      /// "pre-heap" or "post-heap". The latter two are indifferent from the
74
      /// heap's point of view, but may be useful to the user.
54 75
      ///
55
      /// The \c ItemIntMap must be initialized in such a way, that it
56
      /// assigns \c PRE_HEAP (<tt>-1</tt>) to every item.
76
      /// The item-int map must be initialized in such way that it assigns
77
      /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
57 78
      enum State {
58
        IN_HEAP = 0,
59
        PRE_HEAP = -1,
60
        POST_HEAP = -2
79
        IN_HEAP = 0,    ///< = 0. The "in heap" state constant.
80
        PRE_HEAP = -1,  ///< = -1. The "pre-heap" state constant.
81
        POST_HEAP = -2  ///< = -2. The "post-heap" state constant.
61 82
      };
62 83

	
63
      /// \brief The constructor.
84
      /// \brief Constructor.
64 85
      ///
65
      /// The constructor.
86
      /// Constructor.
66 87
      /// \param map A map that assigns \c int values to keys of type
67 88
      /// \c Item. It is used internally by the heap implementations to
68 89
      /// handle the cross references. The assigned value must be
69
      /// \c PRE_HEAP (<tt>-1</tt>) for every item.
90
      /// \c PRE_HEAP (<tt>-1</tt>) for each item.
70 91
      explicit Heap(ItemIntMap &map) {}
71 92

	
93
      /// \brief Constructor.
94
      ///
95
      /// Constructor.
96
      /// \param map A map that assigns \c int values to keys of type
97
      /// \c Item. It is used internally by the heap implementations to
98
      /// handle the cross references. The assigned value must be
99
      /// \c PRE_HEAP (<tt>-1</tt>) for each item.
100
      /// \param comp The function object used for comparing the priorities.
101
      explicit Heap(ItemIntMap &map, const CMP &comp) {}
102

	
72 103
      /// \brief The number of items stored in the heap.
73 104
      ///
74
      /// Returns the number of items stored in the heap.
105
      /// This function returns the number of items stored in the heap.
75 106
      int size() const { return 0; }
76 107

	
77
      /// \brief Checks if the heap is empty.
108
      /// \brief Check if the heap is empty.
78 109
      ///
79
      /// Returns \c true if the heap is empty.
110
      /// This function returns \c true if the heap is empty.
80 111
      bool empty() const { return false; }
81 112

	
82
      /// \brief Makes the heap empty.
113
      /// \brief Make the heap empty.
83 114
      ///
84
      /// Makes the heap empty.
85
      void clear();
115
      /// This functon makes the heap empty.
116
      /// It does not change the cross reference map. If you want to reuse
117
      /// a heap that is not surely empty, you should first clear it and
118
      /// then you should set the cross reference map to \c PRE_HEAP
119
      /// for each item.
120
      void clear() {}
86 121

	
87
      /// \brief Inserts an item into the heap with the given priority.
122
      /// \brief Insert an item into the heap with the given priority.
88 123
      ///
89
      /// Inserts the given item into the heap with the given priority.
124
      /// This function inserts the given item into the heap with the
125
      /// given priority.
90 126
      /// \param i The item to insert.
91 127
      /// \param p The priority of the item.
128
      /// \pre \e i must not be stored in the heap.
92 129
      void push(const Item &i, const Prio &p) {}
93 130

	
94
      /// \brief Returns the item having minimum priority.
131
      /// \brief Return the item having minimum priority.
95 132
      ///
96
      /// Returns the item having minimum priority.
133
      /// This function returns the item having minimum priority.
97 134
      /// \pre The heap must be non-empty.
98 135
      Item top() const {}
99 136

	
100 137
      /// \brief The minimum priority.
101 138
      ///
102
      /// Returns the minimum priority.
139
      /// This function returns the minimum priority.
103 140
      /// \pre The heap must be non-empty.
104 141
      Prio prio() const {}
105 142

	
106
      /// \brief Removes the item having minimum priority.
143
      /// \brief Remove the item having minimum priority.
107 144
      ///
108
      /// Removes the item having minimum priority.
145
      /// This function removes the item having minimum priority.
109 146
      /// \pre The heap must be non-empty.
110 147
      void pop() {}
111 148

	
112
      /// \brief Removes an item from the heap.
149
      /// \brief Remove the given item from the heap.
113 150
      ///
114
      /// Removes the given item from the heap if it is already stored.
151
      /// This function removes the given item from the heap if it is
152
      /// already stored.
115 153
      /// \param i The item to delete.
154
      /// \pre \e i must be in the heap.
116 155
      void erase(const Item &i) {}
117 156

	
118
      /// \brief The priority of an item.
157
      /// \brief The priority of the given item.
119 158
      ///
120
      /// Returns the priority of the given item.
121
      /// \pre \c i must be in the heap.
159
      /// This function returns the priority of the given item.
122 160
      /// \param i The item.
161
      /// \pre \e i must be in the heap.
123 162
      Prio operator[](const Item &i) const {}
124 163

	
125
      /// \brief Sets the priority of an item or inserts it, if it is
164
      /// \brief Set the priority of an item or insert it, if it is
126 165
      /// not stored in the heap.
127 166
      ///
128 167
      /// This method sets the priority of the given item if it is
129
      /// already stored in the heap.
130
      /// Otherwise it inserts the given item with the given priority.
168
      /// already stored in the heap. Otherwise it inserts the given
169
      /// item into the heap with the given priority.
131 170
      ///
132 171
      /// \param i The item.
133 172
      /// \param p The priority.
134 173
      void set(const Item &i, const Prio &p) {}
135 174

	
136
      /// \brief Decreases the priority of an item to the given value.
175
      /// \brief Decrease the priority of an item to the given value.
137 176
      ///
138
      /// Decreases the priority of an item to the given value.
139
      /// \pre \c i must be stored in the heap with priority at least \c p.
177
      /// This function decreases the priority of an item to the given value.
140 178
      /// \param i The item.
141 179
      /// \param p The priority.
180
      /// \pre \e i must be stored in the heap with priority at least \e p.
142 181
      void decrease(const Item &i, const Prio &p) {}
143 182

	
144
      /// \brief Increases the priority of an item to the given value.
183
      /// \brief Increase the priority of an item to the given value.
145 184
      ///
146
      /// Increases the priority of an item to the given value.
147
      /// \pre \c i must be stored in the heap with priority at most \c p.
185
      /// This function increases the priority of an item to the given value.
148 186
      /// \param i The item.
149 187
      /// \param p The priority.
188
      /// \pre \e i must be stored in the heap with priority at most \e p.
150 189
      void increase(const Item &i, const Prio &p) {}
151 190

	
152
      /// \brief Returns if an item is in, has already been in, or has
153
      /// never been in the heap.
191
      /// \brief Return the state of an item.
154 192
      ///
155 193
      /// This method returns \c PRE_HEAP if the given item has never
156 194
      /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
157 195
      /// and \c POST_HEAP otherwise.
158 196
      /// In the latter case it is possible that the item will get back
159 197
      /// to the heap again.
160 198
      /// \param i The item.
161 199
      State state(const Item &i) const {}
162 200

	
163
      /// \brief Sets the state of an item in the heap.
201
      /// \brief Set the state of an item in the heap.
164 202
      ///
165
      /// Sets the state of the given item in the heap. It can be used
166
      /// to manually clear the heap when it is important to achive the
167
      /// better time complexity.
203
      /// This function sets the state of the given item in the heap.
204
      /// It can be used to manually clear the heap when it is important
205
      /// to achive better time complexity.
168 206
      /// \param i The item.
169 207
      /// \param st The state. It should not be \c IN_HEAP.
170 208
      void state(const Item& i, State st) {}
171 209

	
172 210

	
173 211
      template <typename _Heap>
174 212
      struct Constraints {
175 213
      public:
176 214
        void constraints() {
177 215
          typedef typename _Heap::Item OwnItem;
178 216
          typedef typename _Heap::Prio OwnPrio;
179 217
          typedef typename _Heap::State OwnState;
180 218

	
181 219
          Item item;
182 220
          Prio prio;
183 221
          item=Item();
184 222
          prio=Prio();
185 223
          ignore_unused_variable_warning(item);
186 224
          ignore_unused_variable_warning(prio);
187 225

	
188 226
          OwnItem own_item;
189 227
          OwnPrio own_prio;
190 228
          OwnState own_state;
191 229
          own_item=Item();
192 230
          own_prio=Prio();
193 231
          ignore_unused_variable_warning(own_item);
194 232
          ignore_unused_variable_warning(own_prio);
195 233
          ignore_unused_variable_warning(own_state);
196 234

	
197 235
          _Heap heap1(map);
198 236
          _Heap heap2 = heap1;
199 237
          ignore_unused_variable_warning(heap1);
200 238
          ignore_unused_variable_warning(heap2);
201 239

	
202 240
          int s = heap.size();
203 241
          ignore_unused_variable_warning(s);
204 242
          bool e = heap.empty();
205 243
          ignore_unused_variable_warning(e);
206 244

	
207 245
          prio = heap.prio();
208 246
          item = heap.top();
209 247
          prio = heap[item];
210 248
          own_prio = heap.prio();
211 249
          own_item = heap.top();
212 250
          own_prio = heap[own_item];
213 251

	
214 252
          heap.push(item, prio);
215 253
          heap.push(own_item, own_prio);
216 254
          heap.pop();
217 255

	
218 256
          heap.set(item, prio);
219 257
          heap.decrease(item, prio);
220 258
          heap.increase(item, prio);
221 259
          heap.set(own_item, own_prio);
222 260
          heap.decrease(own_item, own_prio);
223 261
          heap.increase(own_item, own_prio);
224 262

	
225 263
          heap.erase(item);
226 264
          heap.erase(own_item);
227 265
          heap.clear();
228 266

	
229 267
          own_state = heap.state(own_item);
230 268
          heap.state(own_item, own_state);
231 269

	
232 270
          own_state = _Heap::PRE_HEAP;
233 271
          own_state = _Heap::IN_HEAP;
234 272
          own_state = _Heap::POST_HEAP;
235 273
        }
236 274

	
237 275
        _Heap& heap;
238 276
        ItemIntMap& map;
239 277
      };
240 278
    };
241 279

	
242 280
    /// @}
243 281
  } // namespace lemon
244 282
}
245
#endif // LEMON_CONCEPT_PATH_H
283
#endif
Ignore white space 6 line context
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-2009
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
#ifndef LEMON_CONCEPT_MAPS_H
20
#define LEMON_CONCEPT_MAPS_H
19
#ifndef LEMON_CONCEPTS_MAPS_H
20
#define LEMON_CONCEPTS_MAPS_H
21 21

	
22 22
#include <lemon/core.h>
23 23
#include <lemon/concept_check.h>
24 24

	
25 25
///\ingroup map_concepts
26 26
///\file
27 27
///\brief The concept of maps.
28 28

	
29 29
namespace lemon {
30 30

	
31 31
  namespace concepts {
32 32

	
33 33
    /// \addtogroup map_concepts
34 34
    /// @{
35 35

	
36 36
    /// Readable map concept
37 37

	
38 38
    /// Readable map concept.
39 39
    ///
40 40
    template<typename K, typename T>
41 41
    class ReadMap
42 42
    {
43 43
    public:
44 44
      /// The key type of the map.
45 45
      typedef K Key;
46 46
      /// \brief The value type of the map.
47 47
      /// (The type of objects associated with the keys).
48 48
      typedef T Value;
49 49

	
50 50
      /// Returns the value associated with the given key.
51 51
      Value operator[](const Key &) const {
52 52
        return *static_cast<Value *>(0);
53 53
      }
54 54

	
55 55
      template<typename _ReadMap>
56 56
      struct Constraints {
57 57
        void constraints() {
58 58
          Value val = m[key];
59 59
          val = m[key];
60 60
          typename _ReadMap::Value own_val = m[own_key];
61 61
          own_val = m[own_key];
62 62

	
63 63
          ignore_unused_variable_warning(key);
64 64
          ignore_unused_variable_warning(val);
65 65
          ignore_unused_variable_warning(own_key);
66 66
          ignore_unused_variable_warning(own_val);
67 67
        }
68 68
        const Key& key;
69 69
        const typename _ReadMap::Key& own_key;
70 70
        const _ReadMap& m;
71 71
      };
72 72

	
73 73
    };
74 74

	
75 75

	
76 76
    /// Writable map concept
77 77

	
78 78
    /// Writable map concept.
79 79
    ///
80 80
    template<typename K, typename T>
81 81
    class WriteMap
82 82
    {
83 83
    public:
84 84
      /// The key type of the map.
85 85
      typedef K Key;
86 86
      /// \brief The value type of the map.
87 87
      /// (The type of objects associated with the keys).
88 88
      typedef T Value;
89 89

	
90 90
      /// Sets the value associated with the given key.
91 91
      void set(const Key &, const Value &) {}
92 92

	
93 93
      /// Default constructor.
94 94
      WriteMap() {}
95 95

	
96 96
      template <typename _WriteMap>
97 97
      struct Constraints {
98 98
        void constraints() {
99 99
          m.set(key, val);
100 100
          m.set(own_key, own_val);
101 101

	
102 102
          ignore_unused_variable_warning(key);
103 103
          ignore_unused_variable_warning(val);
104 104
          ignore_unused_variable_warning(own_key);
105 105
          ignore_unused_variable_warning(own_val);
106 106
        }
107 107
        const Key& key;
108 108
        const Value& val;
109 109
        const typename _WriteMap::Key& own_key;
110 110
        const typename _WriteMap::Value& own_val;
111 111
        _WriteMap& m;
112 112
      };
113 113
    };
114 114

	
115 115
    /// Read/writable map concept
116 116

	
117 117
    /// Read/writable map concept.
118 118
    ///
119 119
    template<typename K, typename T>
120 120
    class ReadWriteMap : public ReadMap<K,T>,
121 121
                         public WriteMap<K,T>
122 122
    {
123 123
    public:
124 124
      /// The key type of the map.
125 125
      typedef K Key;
126 126
      /// \brief The value type of the map.
127 127
      /// (The type of objects associated with the keys).
128 128
      typedef T Value;
129 129

	
130 130
      /// Returns the value associated with the given key.
131 131
      Value operator[](const Key &) const {
132 132
        return *static_cast<Value *>(0);
133 133
      }
134 134

	
135 135
      /// Sets the value associated with the given key.
136 136
      void set(const Key &, const Value &) {}
137 137

	
138 138
      template<typename _ReadWriteMap>
139 139
      struct Constraints {
140 140
        void constraints() {
141 141
          checkConcept<ReadMap<K, T>, _ReadWriteMap >();
142 142
          checkConcept<WriteMap<K, T>, _ReadWriteMap >();
143 143
        }
144 144
      };
145 145
    };
146 146

	
147 147

	
148 148
    /// Dereferable map concept
149 149

	
150 150
    /// Dereferable map concept.
151 151
    ///
152 152
    template<typename K, typename T, typename R, typename CR>
153 153
    class ReferenceMap : public ReadWriteMap<K,T>
154 154
    {
155 155
    public:
156 156
      /// Tag for reference maps.
157 157
      typedef True ReferenceMapTag;
158 158
      /// The key type of the map.
159 159
      typedef K Key;
160 160
      /// \brief The value type of the map.
161 161
      /// (The type of objects associated with the keys).
162 162
      typedef T Value;
163 163
      /// The reference type of the map.
164 164
      typedef R Reference;
165 165
      /// The const reference type of the map.
166 166
      typedef CR ConstReference;
167 167

	
168 168
    public:
169 169

	
170 170
      /// Returns a reference to the value associated with the given key.
171 171
      Reference operator[](const Key &) {
172 172
        return *static_cast<Value *>(0);
173 173
      }
174 174

	
175 175
      /// Returns a const reference to the value associated with the given key.
176 176
      ConstReference operator[](const Key &) const {
177 177
        return *static_cast<Value *>(0);
178 178
      }
179 179

	
180 180
      /// Sets the value associated with the given key.
181 181
      void set(const Key &k,const Value &t) { operator[](k)=t; }
182 182

	
183 183
      template<typename _ReferenceMap>
184 184
      struct Constraints {
185
        void constraints() {
185
        typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type
186
        constraints() {
186 187
          checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
187 188
          ref = m[key];
188 189
          m[key] = val;
189 190
          m[key] = ref;
190 191
          m[key] = cref;
191 192
          own_ref = m[own_key];
192 193
          m[own_key] = own_val;
193 194
          m[own_key] = own_ref;
194 195
          m[own_key] = own_cref;
195 196
          m[key] = m[own_key];
196 197
          m[own_key] = m[key];
197 198
        }
198 199
        const Key& key;
199 200
        Value& val;
200 201
        Reference ref;
201 202
        ConstReference cref;
202 203
        const typename _ReferenceMap::Key& own_key;
203 204
        typename _ReferenceMap::Value& own_val;
204 205
        typename _ReferenceMap::Reference own_ref;
205 206
        typename _ReferenceMap::ConstReference own_cref;
206 207
        _ReferenceMap& m;
207 208
      };
208 209
    };
209 210

	
210 211
    // @}
211 212

	
212 213
  } //namespace concepts
213 214

	
214 215
} //namespace lemon
215 216

	
216
#endif // LEMON_CONCEPT_MAPS_H
217
#endif
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup concept
20 20
///\file
21 21
///\brief Classes for representing paths in digraphs.
22 22
///
23 23

	
24
#ifndef LEMON_CONCEPT_PATH_H
25
#define LEMON_CONCEPT_PATH_H
24
#ifndef LEMON_CONCEPTS_PATH_H
25
#define LEMON_CONCEPTS_PATH_H
26 26

	
27 27
#include <lemon/core.h>
28 28
#include <lemon/concept_check.h>
29 29

	
30 30
namespace lemon {
31 31
  namespace concepts {
32 32

	
33 33
    /// \addtogroup concept
34 34
    /// @{
35 35

	
36 36
    /// \brief A skeleton structure for representing directed paths in
37 37
    /// a digraph.
38 38
    ///
39 39
    /// A skeleton structure for representing directed paths in a
40 40
    /// digraph.
41
    /// \tparam _Digraph The digraph type in which the path is.
41
    /// \tparam GR The digraph type in which the path is.
42 42
    ///
43 43
    /// In a sense, the path can be treated as a list of arcs. The
44 44
    /// lemon path type stores just this list. As a consequence it
45 45
    /// cannot enumerate the nodes in the path and the zero length
46 46
    /// paths cannot store the source.
47 47
    ///
48
    template <typename _Digraph>
48
    template <typename GR>
49 49
    class Path {
50 50
    public:
51 51

	
52 52
      /// Type of the underlying digraph.
53
      typedef _Digraph Digraph;
53
      typedef GR Digraph;
54 54
      /// Arc type of the underlying digraph.
55 55
      typedef typename Digraph::Arc Arc;
56 56

	
57 57
      class ArcIt;
58 58

	
59 59
      /// \brief Default constructor
60 60
      Path() {}
61 61

	
62 62
      /// \brief Template constructor
63 63
      template <typename CPath>
64 64
      Path(const CPath& cpath) {}
65 65

	
66 66
      /// \brief Template assigment
67 67
      template <typename CPath>
68 68
      Path& operator=(const CPath& cpath) {
69 69
        ignore_unused_variable_warning(cpath);
70 70
        return *this;
71 71
      }
72 72

	
73 73
      /// Length of the path ie. the number of arcs in the path.
74 74
      int length() const { return 0;}
75 75

	
76 76
      /// Returns whether the path is empty.
77 77
      bool empty() const { return true;}
78 78

	
79 79
      /// Resets the path to an empty path.
80 80
      void clear() {}
81 81

	
82 82
      /// \brief LEMON style iterator for path arcs
83 83
      ///
84 84
      /// This class is used to iterate on the arcs of the paths.
85 85
      class ArcIt {
86 86
      public:
87 87
        /// Default constructor
88 88
        ArcIt() {}
89 89
        /// Invalid constructor
90 90
        ArcIt(Invalid) {}
91 91
        /// Constructor for first arc
92 92
        ArcIt(const Path &) {}
93 93

	
94 94
        /// Conversion to Arc
95 95
        operator Arc() const { return INVALID; }
96 96

	
97 97
        /// Next arc
98 98
        ArcIt& operator++() {return *this;}
99 99

	
100 100
        /// Comparison operator
101 101
        bool operator==(const ArcIt&) const {return true;}
102 102
        /// Comparison operator
103 103
        bool operator!=(const ArcIt&) const {return true;}
104 104
        /// Comparison operator
105 105
        bool operator<(const ArcIt&) const {return false;}
106 106

	
107 107
      };
108 108

	
109 109
      template <typename _Path>
110 110
      struct Constraints {
111 111
        void constraints() {
112 112
          Path<Digraph> pc;
113 113
          _Path p, pp(pc);
114 114
          int l = p.length();
115 115
          int e = p.empty();
116 116
          p.clear();
117 117

	
118 118
          p = pc;
119 119

	
120 120
          typename _Path::ArcIt id, ii(INVALID), i(p);
121 121

	
122 122
          ++i;
123 123
          typename Digraph::Arc ed = i;
124 124

	
125 125
          e = (i == ii);
126 126
          e = (i != ii);
127 127
          e = (i < ii);
128 128

	
129 129
          ignore_unused_variable_warning(l);
130 130
          ignore_unused_variable_warning(pp);
131 131
          ignore_unused_variable_warning(e);
132 132
          ignore_unused_variable_warning(id);
133 133
          ignore_unused_variable_warning(ii);
134 134
          ignore_unused_variable_warning(ed);
135 135
        }
136 136
      };
137 137

	
138 138
    };
139 139

	
140 140
    namespace _path_bits {
141 141

	
142 142
      template <typename _Digraph, typename _Path, typename RevPathTag = void>
143 143
      struct PathDumperConstraints {
144 144
        void constraints() {
145 145
          int l = p.length();
146 146
          int e = p.empty();
147 147

	
148 148
          typename _Path::ArcIt id, i(p);
149 149

	
150 150
          ++i;
151 151
          typename _Digraph::Arc ed = i;
152 152

	
153 153
          e = (i == INVALID);
154 154
          e = (i != INVALID);
155 155

	
156 156
          ignore_unused_variable_warning(l);
157 157
          ignore_unused_variable_warning(e);
158 158
          ignore_unused_variable_warning(id);
159 159
          ignore_unused_variable_warning(ed);
160 160
        }
161 161
        _Path& p;
162 162
      };
163 163

	
164 164
      template <typename _Digraph, typename _Path>
165 165
      struct PathDumperConstraints<
166 166
        _Digraph, _Path,
167 167
        typename enable_if<typename _Path::RevPathTag, void>::type
168 168
      > {
169 169
        void constraints() {
170 170
          int l = p.length();
171 171
          int e = p.empty();
172 172

	
173 173
          typename _Path::RevArcIt id, i(p);
174 174

	
175 175
          ++i;
176 176
          typename _Digraph::Arc ed = i;
177 177

	
178 178
          e = (i == INVALID);
179 179
          e = (i != INVALID);
180 180

	
181 181
          ignore_unused_variable_warning(l);
182 182
          ignore_unused_variable_warning(e);
183 183
          ignore_unused_variable_warning(id);
184 184
          ignore_unused_variable_warning(ed);
185 185
        }
186 186
        _Path& p;
187 187
      };
188 188

	
189 189
    }
190 190

	
191 191

	
192 192
    /// \brief A skeleton structure for path dumpers.
193 193
    ///
194 194
    /// A skeleton structure for path dumpers. The path dumpers are
195 195
    /// the generalization of the paths. The path dumpers can
196 196
    /// enumerate the arcs of the path wheter in forward or in
197 197
    /// backward order.  In most time these classes are not used
198 198
    /// directly rather it used to assign a dumped class to a real
199 199
    /// path type.
200 200
    ///
201 201
    /// The main purpose of this concept is that the shortest path
202 202
    /// algorithms can enumerate easily the arcs in reverse order.
203 203
    /// If we would like to give back a real path from these
204 204
    /// algorithms then we should create a temporarly path object. In
205 205
    /// LEMON such algorithms gives back a path dumper what can
206 206
    /// assigned to a real path and the dumpers can be implemented as
207 207
    /// an adaptor class to the predecessor map.
208

	
209
    /// \tparam _Digraph  The digraph type in which the path is.
208
    ///
209
    /// \tparam GR The digraph type in which the path is.
210 210
    ///
211 211
    /// The paths can be constructed from any path type by a
212 212
    /// template constructor or a template assignment operator.
213
    ///
214
    template <typename _Digraph>
213
    template <typename GR>
215 214
    class PathDumper {
216 215
    public:
217 216

	
218 217
      /// Type of the underlying digraph.
219
      typedef _Digraph Digraph;
218
      typedef GR Digraph;
220 219
      /// Arc type of the underlying digraph.
221 220
      typedef typename Digraph::Arc Arc;
222 221

	
223 222
      /// Length of the path ie. the number of arcs in the path.
224 223
      int length() const { return 0;}
225 224

	
226 225
      /// Returns whether the path is empty.
227 226
      bool empty() const { return true;}
228 227

	
229 228
      /// \brief Forward or reverse dumping
230 229
      ///
231 230
      /// If the RevPathTag is defined and true then reverse dumping
232 231
      /// is provided in the path dumper. In this case instead of the
233 232
      /// ArcIt the RevArcIt iterator should be implemented in the
234 233
      /// dumper.
235 234
      typedef False RevPathTag;
236 235

	
237 236
      /// \brief LEMON style iterator for path arcs
238 237
      ///
239 238
      /// This class is used to iterate on the arcs of the paths.
240 239
      class ArcIt {
241 240
      public:
242 241
        /// Default constructor
243 242
        ArcIt() {}
244 243
        /// Invalid constructor
245 244
        ArcIt(Invalid) {}
246 245
        /// Constructor for first arc
247 246
        ArcIt(const PathDumper&) {}
248 247

	
249 248
        /// Conversion to Arc
250 249
        operator Arc() const { return INVALID; }
251 250

	
252 251
        /// Next arc
253 252
        ArcIt& operator++() {return *this;}
254 253

	
255 254
        /// Comparison operator
256 255
        bool operator==(const ArcIt&) const {return true;}
257 256
        /// Comparison operator
258 257
        bool operator!=(const ArcIt&) const {return true;}
259 258
        /// Comparison operator
260 259
        bool operator<(const ArcIt&) const {return false;}
261 260

	
262 261
      };
263 262

	
264 263
      /// \brief LEMON style iterator for path arcs
265 264
      ///
266 265
      /// This class is used to iterate on the arcs of the paths in
267 266
      /// reverse direction.
268 267
      class RevArcIt {
269 268
      public:
270 269
        /// Default constructor
271 270
        RevArcIt() {}
272 271
        /// Invalid constructor
273 272
        RevArcIt(Invalid) {}
274 273
        /// Constructor for first arc
275 274
        RevArcIt(const PathDumper &) {}
276 275

	
277 276
        /// Conversion to Arc
278 277
        operator Arc() const { return INVALID; }
279 278

	
280 279
        /// Next arc
281 280
        RevArcIt& operator++() {return *this;}
282 281

	
283 282
        /// Comparison operator
284 283
        bool operator==(const RevArcIt&) const {return true;}
285 284
        /// Comparison operator
286 285
        bool operator!=(const RevArcIt&) const {return true;}
287 286
        /// Comparison operator
288 287
        bool operator<(const RevArcIt&) const {return false;}
289 288

	
290 289
      };
291 290

	
292 291
      template <typename _Path>
293 292
      struct Constraints {
294 293
        void constraints() {
295 294
          function_requires<_path_bits::
296 295
            PathDumperConstraints<Digraph, _Path> >();
297 296
        }
298 297
      };
299 298

	
300 299
    };
301 300

	
302 301

	
303 302
    ///@}
304 303
  }
305 304

	
306 305
} // namespace lemon
307 306

	
308
#endif // LEMON_CONCEPT_PATH_H
307
#endif
Ignore white space 6 line context
1
/* The version string */
2
#undef LEMON_VERSION
3

	
4
/* Define to 1 if you have long long */
5
#undef LEMON_HAVE_LONG_LONG
6

	
1 7
/* Define to 1 if you have any LP solver. */
2
#undef HAVE_LP
8
#undef LEMON_HAVE_LP
3 9

	
4 10
/* Define to 1 if you have any MIP solver. */
5
#undef HAVE_MIP
11
#undef LEMON_HAVE_MIP
6 12

	
7 13
/* Define to 1 if you have CPLEX. */
8
#undef HAVE_CPLEX
14
#undef LEMON_HAVE_CPLEX
9 15

	
10 16
/* Define to 1 if you have GLPK. */
11
#undef HAVE_GLPK
17
#undef LEMON_HAVE_GLPK
12 18

	
13 19
/* Define to 1 if you have SOPLEX */
14
#undef HAVE_SOPLEX
20
#undef LEMON_HAVE_SOPLEX
15 21

	
16 22
/* Define to 1 if you have CLP */
17
#undef HAVE_CLP
23
#undef LEMON_HAVE_CLP
24

	
25
/* Define to 1 if you have CBC */
26
#undef LEMON_HAVE_CBC
Ignore white space 6 line context
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-2009
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_CONNECTIVITY_H
20 20
#define LEMON_CONNECTIVITY_H
21 21

	
22 22
#include <lemon/dfs.h>
23 23
#include <lemon/bfs.h>
24 24
#include <lemon/core.h>
25 25
#include <lemon/maps.h>
26 26
#include <lemon/adaptors.h>
27 27

	
28 28
#include <lemon/concepts/digraph.h>
29 29
#include <lemon/concepts/graph.h>
30 30
#include <lemon/concept_check.h>
31 31

	
32 32
#include <stack>
33 33
#include <functional>
34 34

	
35
/// \ingroup connectivity
35
/// \ingroup graph_properties
36 36
/// \file
37 37
/// \brief Connectivity algorithms
38 38
///
39 39
/// Connectivity algorithms
40 40

	
41 41
namespace lemon {
42 42

	
43
  /// \ingroup connectivity
43
  /// \ingroup graph_properties
44 44
  ///
45
  /// \brief Check whether the given undirected graph is connected.
45
  /// \brief Check whether an undirected graph is connected.
46 46
  ///
47
  /// Check whether the given undirected graph is connected.
48
  /// \param graph The undirected graph.
49
  /// \return %True when there is path between any two nodes in the graph.
47
  /// This function checks whether the given undirected graph is connected,
48
  /// i.e. there is a path between any two nodes in the graph.
49
  ///
50
  /// \return \c true if the graph is connected.
50 51
  /// \note By definition, the empty graph is connected.
52
  ///
53
  /// \see countConnectedComponents(), connectedComponents()
54
  /// \see stronglyConnected()
51 55
  template <typename Graph>
52 56
  bool connected(const Graph& graph) {
53 57
    checkConcept<concepts::Graph, Graph>();
54 58
    typedef typename Graph::NodeIt NodeIt;
55 59
    if (NodeIt(graph) == INVALID) return true;
56 60
    Dfs<Graph> dfs(graph);
57 61
    dfs.run(NodeIt(graph));
58 62
    for (NodeIt it(graph); it != INVALID; ++it) {
59 63
      if (!dfs.reached(it)) {
60 64
        return false;
61 65
      }
62 66
    }
63 67
    return true;
64 68
  }
65 69

	
66
  /// \ingroup connectivity
70
  /// \ingroup graph_properties
67 71
  ///
68 72
  /// \brief Count the number of connected components of an undirected graph
69 73
  ///
70
  /// Count the number of connected components of an undirected graph
74
  /// This function counts the number of connected components of the given
75
  /// undirected graph.
71 76
  ///
72
  /// \param graph The graph. It must be undirected.
73
  /// \return The number of components
77
  /// The connected components are the classes of an equivalence relation
78
  /// on the nodes of an undirected graph. Two nodes are in the same class
79
  /// if they are connected with a path.
80
  ///
81
  /// \return The number of connected components.
74 82
  /// \note By definition, the empty graph consists
75 83
  /// of zero connected components.
84
  ///
85
  /// \see connected(), connectedComponents()
76 86
  template <typename Graph>
77 87
  int countConnectedComponents(const Graph &graph) {
78 88
    checkConcept<concepts::Graph, Graph>();
79 89
    typedef typename Graph::Node Node;
80 90
    typedef typename Graph::Arc Arc;
81 91

	
82 92
    typedef NullMap<Node, Arc> PredMap;
83 93
    typedef NullMap<Node, int> DistMap;
84 94

	
85 95
    int compNum = 0;
86 96
    typename Bfs<Graph>::
87 97
      template SetPredMap<PredMap>::
88 98
      template SetDistMap<DistMap>::
89 99
      Create bfs(graph);
90 100

	
91 101
    PredMap predMap;
92 102
    bfs.predMap(predMap);
93 103

	
94 104
    DistMap distMap;
95 105
    bfs.distMap(distMap);
96 106

	
97 107
    bfs.init();
98 108
    for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
99 109
      if (!bfs.reached(n)) {
100 110
        bfs.addSource(n);
101 111
        bfs.start();
102 112
        ++compNum;
103 113
      }
104 114
    }
105 115
    return compNum;
106 116
  }
107 117

	
108
  /// \ingroup connectivity
118
  /// \ingroup graph_properties
109 119
  ///
110 120
  /// \brief Find the connected components of an undirected graph
111 121
  ///
112
  /// Find the connected components of an undirected graph.
122
  /// This function finds the connected components of the given undirected
123
  /// graph.
113 124
  ///
114
  /// \param graph The graph. It must be undirected.
125
  /// The connected components are the classes of an equivalence relation
126
  /// on the nodes of an undirected graph. Two nodes are in the same class
127
  /// if they are connected with a path.
128
  ///
129
  /// \image html connected_components.png
130
  /// \image latex connected_components.eps "Connected components" width=\textwidth
131
  ///
132
  /// \param graph The undirected graph.
115 133
  /// \retval compMap A writable node map. The values will be set from 0 to
116
  /// the number of the connected components minus one. Each values of the map
117
  /// will be set exactly once, the values of a certain component will be
134
  /// the number of the connected components minus one. Each value of the map
135
  /// will be set exactly once, and the values of a certain component will be
118 136
  /// set continuously.
119
  /// \return The number of components
137
  /// \return The number of connected components.
138
  /// \note By definition, the empty graph consists
139
  /// of zero connected components.
120 140
  ///
141
  /// \see connected(), countConnectedComponents()
121 142
  template <class Graph, class NodeMap>
122 143
  int connectedComponents(const Graph &graph, NodeMap &compMap) {
123 144
    checkConcept<concepts::Graph, Graph>();
124 145
    typedef typename Graph::Node Node;
125 146
    typedef typename Graph::Arc Arc;
126 147
    checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
127 148

	
128 149
    typedef NullMap<Node, Arc> PredMap;
129 150
    typedef NullMap<Node, int> DistMap;
130 151

	
131 152
    int compNum = 0;
132 153
    typename Bfs<Graph>::
133 154
      template SetPredMap<PredMap>::
134 155
      template SetDistMap<DistMap>::
135 156
      Create bfs(graph);
136 157

	
137 158
    PredMap predMap;
138 159
    bfs.predMap(predMap);
139 160

	
140 161
    DistMap distMap;
141 162
    bfs.distMap(distMap);
142 163

	
143 164
    bfs.init();
144 165
    for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
145 166
      if(!bfs.reached(n)) {
146 167
        bfs.addSource(n);
147 168
        while (!bfs.emptyQueue()) {
148 169
          compMap.set(bfs.nextNode(), compNum);
149 170
          bfs.processNextNode();
150 171
        }
151 172
        ++compNum;
152 173
      }
153 174
    }
154 175
    return compNum;
155 176
  }
156 177

	
157 178
  namespace _connectivity_bits {
158 179

	
159 180
    template <typename Digraph, typename Iterator >
160 181
    struct LeaveOrderVisitor : public DfsVisitor<Digraph> {
161 182
    public:
162 183
      typedef typename Digraph::Node Node;
163 184
      LeaveOrderVisitor(Iterator it) : _it(it) {}
164 185

	
165 186
      void leave(const Node& node) {
166 187
        *(_it++) = node;
167 188
      }
168 189

	
169 190
    private:
170 191
      Iterator _it;
171 192
    };
172 193

	
173 194
    template <typename Digraph, typename Map>
174 195
    struct FillMapVisitor : public DfsVisitor<Digraph> {
175 196
    public:
176 197
      typedef typename Digraph::Node Node;
177 198
      typedef typename Map::Value Value;
178 199

	
179 200
      FillMapVisitor(Map& map, Value& value)
180 201
        : _map(map), _value(value) {}
181 202

	
182 203
      void reach(const Node& node) {
183 204
        _map.set(node, _value);
184 205
      }
185 206
    private:
186 207
      Map& _map;
187 208
      Value& _value;
188 209
    };
189 210

	
190 211
    template <typename Digraph, typename ArcMap>
191 212
    struct StronglyConnectedCutArcsVisitor : public DfsVisitor<Digraph> {
192 213
    public:
193 214
      typedef typename Digraph::Node Node;
194 215
      typedef typename Digraph::Arc Arc;
195 216

	
196 217
      StronglyConnectedCutArcsVisitor(const Digraph& digraph,
197 218
                                      ArcMap& cutMap,
198 219
                                      int& cutNum)
199 220
        : _digraph(digraph), _cutMap(cutMap), _cutNum(cutNum),
200 221
          _compMap(digraph, -1), _num(-1) {
201 222
      }
202 223

	
203 224
      void start(const Node&) {
204 225
        ++_num;
205 226
      }
206 227

	
207 228
      void reach(const Node& node) {
208 229
        _compMap.set(node, _num);
209 230
      }
210 231

	
211 232
      void examine(const Arc& arc) {
212 233
         if (_compMap[_digraph.source(arc)] !=
213 234
             _compMap[_digraph.target(arc)]) {
214 235
           _cutMap.set(arc, true);
215 236
           ++_cutNum;
216 237
         }
217 238
      }
218 239
    private:
219 240
      const Digraph& _digraph;
220 241
      ArcMap& _cutMap;
221 242
      int& _cutNum;
222 243

	
223 244
      typename Digraph::template NodeMap<int> _compMap;
224 245
      int _num;
225 246
    };
226 247

	
227 248
  }
228 249

	
229 250

	
230
  /// \ingroup connectivity
251
  /// \ingroup graph_properties
231 252
  ///
232
  /// \brief Check whether the given directed graph is strongly connected.
253
  /// \brief Check whether a directed graph is strongly connected.
233 254
  ///
234
  /// Check whether the given directed graph is strongly connected. The
235
  /// graph is strongly connected when any two nodes of the graph are
255
  /// This function checks whether the given directed graph is strongly
256
  /// connected, i.e. any two nodes of the digraph are
236 257
  /// connected with directed paths in both direction.
237
  /// \return %False when the graph is not strongly connected.
238
  /// \see connected
239 258
  ///
240
  /// \note By definition, the empty graph is strongly connected.
259
  /// \return \c true if the digraph is strongly connected.
260
  /// \note By definition, the empty digraph is strongly connected.
261
  /// 
262
  /// \see countStronglyConnectedComponents(), stronglyConnectedComponents()
263
  /// \see connected()
241 264
  template <typename Digraph>
242 265
  bool stronglyConnected(const Digraph& digraph) {
243 266
    checkConcept<concepts::Digraph, Digraph>();
244 267

	
245 268
    typedef typename Digraph::Node Node;
246 269
    typedef typename Digraph::NodeIt NodeIt;
247 270

	
248 271
    typename Digraph::Node source = NodeIt(digraph);
249 272
    if (source == INVALID) return true;
250 273

	
251 274
    using namespace _connectivity_bits;
252 275

	
253 276
    typedef DfsVisitor<Digraph> Visitor;
254 277
    Visitor visitor;
255 278

	
256 279
    DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
257 280
    dfs.init();
258 281
    dfs.addSource(source);
259 282
    dfs.start();
260 283

	
261 284
    for (NodeIt it(digraph); it != INVALID; ++it) {
262 285
      if (!dfs.reached(it)) {
263 286
        return false;
264 287
      }
265 288
    }
266 289

	
267 290
    typedef ReverseDigraph<const Digraph> RDigraph;
268 291
    typedef typename RDigraph::NodeIt RNodeIt;
269 292
    RDigraph rdigraph(digraph);
270 293

	
271
    typedef DfsVisitor<Digraph> RVisitor;
294
    typedef DfsVisitor<RDigraph> RVisitor;
272 295
    RVisitor rvisitor;
273 296

	
274 297
    DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
275 298
    rdfs.init();
276 299
    rdfs.addSource(source);
277 300
    rdfs.start();
278 301

	
279 302
    for (RNodeIt it(rdigraph); it != INVALID; ++it) {
280 303
      if (!rdfs.reached(it)) {
281 304
        return false;
282 305
      }
283 306
    }
284 307

	
285 308
    return true;
286 309
  }
287 310

	
288
  /// \ingroup connectivity
311
  /// \ingroup graph_properties
289 312
  ///
290
  /// \brief Count the strongly connected components of a directed graph
313
  /// \brief Count the number of strongly connected components of a 
314
  /// directed graph
291 315
  ///
292
  /// Count the strongly connected components of a directed graph.
316
  /// This function counts the number of strongly connected components of
317
  /// the given directed graph.
318
  ///
293 319
  /// The strongly connected components are the classes of an
294
  /// equivalence relation on the nodes of the graph. Two nodes are in
320
  /// equivalence relation on the nodes of a digraph. Two nodes are in
295 321
  /// the same class if they are connected with directed paths in both
296 322
  /// direction.
297 323
  ///
298
  /// \param digraph The graph.
299
  /// \return The number of components
300
  /// \note By definition, the empty graph has zero
324
  /// \return The number of strongly connected components.
325
  /// \note By definition, the empty digraph has zero
301 326
  /// strongly connected components.
327
  ///
328
  /// \see stronglyConnected(), stronglyConnectedComponents()
302 329
  template <typename Digraph>
303 330
  int countStronglyConnectedComponents(const Digraph& digraph) {
304 331
    checkConcept<concepts::Digraph, Digraph>();
305 332

	
306 333
    using namespace _connectivity_bits;
307 334

	
308 335
    typedef typename Digraph::Node Node;
309 336
    typedef typename Digraph::Arc Arc;
310 337
    typedef typename Digraph::NodeIt NodeIt;
311 338
    typedef typename Digraph::ArcIt ArcIt;
312 339

	
313 340
    typedef std::vector<Node> Container;
314 341
    typedef typename Container::iterator Iterator;
315 342

	
316 343
    Container nodes(countNodes(digraph));
317 344
    typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
318 345
    Visitor visitor(nodes.begin());
319 346

	
320 347
    DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
321 348
    dfs.init();
322 349
    for (NodeIt it(digraph); it != INVALID; ++it) {
323 350
      if (!dfs.reached(it)) {
324 351
        dfs.addSource(it);
325 352
        dfs.start();
326 353
      }
327 354
    }
328 355

	
329 356
    typedef typename Container::reverse_iterator RIterator;
330 357
    typedef ReverseDigraph<const Digraph> RDigraph;
331 358

	
332 359
    RDigraph rdigraph(digraph);
333 360

	
334 361
    typedef DfsVisitor<Digraph> RVisitor;
335 362
    RVisitor rvisitor;
336 363

	
337 364
    DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
338 365

	
339 366
    int compNum = 0;
340 367

	
341 368
    rdfs.init();
342 369
    for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
343 370
      if (!rdfs.reached(*it)) {
344 371
        rdfs.addSource(*it);
345 372
        rdfs.start();
346 373
        ++compNum;
347 374
      }
348 375
    }
349 376
    return compNum;
350 377
  }
351 378

	
352
  /// \ingroup connectivity
379
  /// \ingroup graph_properties
353 380
  ///
354 381
  /// \brief Find the strongly connected components of a directed graph
355 382
  ///
356
  /// Find the strongly connected components of a directed graph.  The
357
  /// strongly connected components are the classes of an equivalence
358
  /// relation on the nodes of the graph. Two nodes are in
359
  /// relationship when there are directed paths between them in both
360
  /// direction. In addition, the numbering of components will satisfy
361
  /// that there is no arc going from a higher numbered component to
362
  /// a lower.
383
  /// This function finds the strongly connected components of the given
384
  /// directed graph. In addition, the numbering of the components will
385
  /// satisfy that there is no arc going from a higher numbered component
386
  /// to a lower one (i.e. it provides a topological order of the components).
387
  ///
388
  /// The strongly connected components are the classes of an
389
  /// equivalence relation on the nodes of a digraph. Two nodes are in
390
  /// the same class if they are connected with directed paths in both
391
  /// direction.
392
  ///
393
  /// \image html strongly_connected_components.png
394
  /// \image latex strongly_connected_components.eps "Strongly connected components" width=\textwidth
363 395
  ///
364 396
  /// \param digraph The digraph.
365 397
  /// \retval compMap A writable node map. The values will be set from 0 to
366 398
  /// the number of the strongly connected components minus one. Each value
367
  /// of the map will be set exactly once, the values of a certain component
368
  /// will be set continuously.
369
  /// \return The number of components
399
  /// of the map will be set exactly once, and the values of a certain
400
  /// component will be set continuously.
401
  /// \return The number of strongly connected components.
402
  /// \note By definition, the empty digraph has zero
403
  /// strongly connected components.
370 404
  ///
405
  /// \see stronglyConnected(), countStronglyConnectedComponents()
371 406
  template <typename Digraph, typename NodeMap>
372 407
  int stronglyConnectedComponents(const Digraph& digraph, NodeMap& compMap) {
373 408
    checkConcept<concepts::Digraph, Digraph>();
374 409
    typedef typename Digraph::Node Node;
375 410
    typedef typename Digraph::NodeIt NodeIt;
376 411
    checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
377 412

	
378 413
    using namespace _connectivity_bits;
379 414

	
380 415
    typedef std::vector<Node> Container;
381 416
    typedef typename Container::iterator Iterator;
382 417

	
383 418
    Container nodes(countNodes(digraph));
384 419
    typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
385 420
    Visitor visitor(nodes.begin());
386 421

	
387 422
    DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
388 423
    dfs.init();
389 424
    for (NodeIt it(digraph); it != INVALID; ++it) {
390 425
      if (!dfs.reached(it)) {
391 426
        dfs.addSource(it);
392 427
        dfs.start();
393 428
      }
394 429
    }
395 430

	
396 431
    typedef typename Container::reverse_iterator RIterator;
397 432
    typedef ReverseDigraph<const Digraph> RDigraph;
398 433

	
399 434
    RDigraph rdigraph(digraph);
400 435

	
401 436
    int compNum = 0;
402 437

	
403 438
    typedef FillMapVisitor<RDigraph, NodeMap> RVisitor;
404 439
    RVisitor rvisitor(compMap, compNum);
405 440

	
406 441
    DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
407 442

	
408 443
    rdfs.init();
409 444
    for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
410 445
      if (!rdfs.reached(*it)) {
411 446
        rdfs.addSource(*it);
412 447
        rdfs.start();
413 448
        ++compNum;
414 449
      }
415 450
    }
416 451
    return compNum;
417 452
  }
418 453

	
419
  /// \ingroup connectivity
454
  /// \ingroup graph_properties
420 455
  ///
421 456
  /// \brief Find the cut arcs of the strongly connected components.
422 457
  ///
423
  /// Find the cut arcs of the strongly connected components.
424
  /// The strongly connected components are the classes of an equivalence
425
  /// relation on the nodes of the graph. Two nodes are in relationship
426
  /// when there are directed paths between them in both direction.
458
  /// This function finds the cut arcs of the strongly connected components
459
  /// of the given digraph.
460
  ///
461
  /// The strongly connected components are the classes of an
462
  /// equivalence relation on the nodes of a digraph. Two nodes are in
463
  /// the same class if they are connected with directed paths in both
464
  /// direction.
427 465
  /// The strongly connected components are separated by the cut arcs.
428 466
  ///
429
  /// \param graph The graph.
430
  /// \retval cutMap A writable node map. The values will be set true when the
431
  /// arc is a cut arc.
467
  /// \param digraph The digraph.
468
  /// \retval cutMap A writable arc map. The values will be set to \c true
469
  /// for the cut arcs (exactly once for each cut arc), and will not be
470
  /// changed for other arcs.
471
  /// \return The number of cut arcs.
432 472
  ///
433
  /// \return The number of cut arcs
473
  /// \see stronglyConnected(), stronglyConnectedComponents()
434 474
  template <typename Digraph, typename ArcMap>
435
  int stronglyConnectedCutArcs(const Digraph& graph, ArcMap& cutMap) {
475
  int stronglyConnectedCutArcs(const Digraph& digraph, ArcMap& cutMap) {
436 476
    checkConcept<concepts::Digraph, Digraph>();
437 477
    typedef typename Digraph::Node Node;
438 478
    typedef typename Digraph::Arc Arc;
439 479
    typedef typename Digraph::NodeIt NodeIt;
440 480
    checkConcept<concepts::WriteMap<Arc, bool>, ArcMap>();
441 481

	
442 482
    using namespace _connectivity_bits;
443 483

	
444 484
    typedef std::vector<Node> Container;
445 485
    typedef typename Container::iterator Iterator;
446 486

	
447
    Container nodes(countNodes(graph));
487
    Container nodes(countNodes(digraph));
448 488
    typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
449 489
    Visitor visitor(nodes.begin());
450 490

	
451
    DfsVisit<Digraph, Visitor> dfs(graph, visitor);
491
    DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
452 492
    dfs.init();
453
    for (NodeIt it(graph); it != INVALID; ++it) {
493
    for (NodeIt it(digraph); it != INVALID; ++it) {
454 494
      if (!dfs.reached(it)) {
455 495
        dfs.addSource(it);
456 496
        dfs.start();
457 497
      }
458 498
    }
459 499

	
460 500
    typedef typename Container::reverse_iterator RIterator;
461 501
    typedef ReverseDigraph<const Digraph> RDigraph;
462 502

	
463
    RDigraph rgraph(graph);
503
    RDigraph rdigraph(digraph);
464 504

	
465 505
    int cutNum = 0;
466 506

	
467 507
    typedef StronglyConnectedCutArcsVisitor<RDigraph, ArcMap> RVisitor;
468
    RVisitor rvisitor(rgraph, cutMap, cutNum);
508
    RVisitor rvisitor(rdigraph, cutMap, cutNum);
469 509

	
470
    DfsVisit<RDigraph, RVisitor> rdfs(rgraph, rvisitor);
510
    DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
471 511

	
472 512
    rdfs.init();
473 513
    for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
474 514
      if (!rdfs.reached(*it)) {
475 515
        rdfs.addSource(*it);
476 516
        rdfs.start();
477 517
      }
478 518
    }
479 519
    return cutNum;
480 520
  }
481 521

	
482 522
  namespace _connectivity_bits {
483 523

	
484 524
    template <typename Digraph>
485 525
    class CountBiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
486 526
    public:
487 527
      typedef typename Digraph::Node Node;
488 528
      typedef typename Digraph::Arc Arc;
489 529
      typedef typename Digraph::Edge Edge;
490 530

	
491 531
      CountBiNodeConnectedComponentsVisitor(const Digraph& graph, int &compNum)
492 532
        : _graph(graph), _compNum(compNum),
493 533
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
494 534

	
495 535
      void start(const Node& node) {
496 536
        _predMap.set(node, INVALID);
497 537
      }
498 538

	
499 539
      void reach(const Node& node) {
500 540
        _numMap.set(node, _num);
501 541
        _retMap.set(node, _num);
502 542
        ++_num;
503 543
      }
504 544

	
505 545
      void discover(const Arc& edge) {
506 546
        _predMap.set(_graph.target(edge), _graph.source(edge));
507 547
      }
508 548

	
509 549
      void examine(const Arc& edge) {
510 550
        if (_graph.source(edge) == _graph.target(edge) &&
511 551
            _graph.direction(edge)) {
512 552
          ++_compNum;
513 553
          return;
514 554
        }
515 555
        if (_predMap[_graph.source(edge)] == _graph.target(edge)) {
516 556
          return;
517 557
        }
518 558
        if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) {
519 559
          _retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]);
520 560
        }
521 561
      }
522 562

	
523 563
      void backtrack(const Arc& edge) {
524 564
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
525 565
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
526 566
        }
527 567
        if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) {
528 568
          ++_compNum;
529 569
        }
530 570
      }
531 571

	
532 572
    private:
533 573
      const Digraph& _graph;
534 574
      int& _compNum;
535 575

	
536 576
      typename Digraph::template NodeMap<int> _numMap;
537 577
      typename Digraph::template NodeMap<int> _retMap;
538 578
      typename Digraph::template NodeMap<Node> _predMap;
539 579
      int _num;
540 580
    };
541 581

	
542 582
    template <typename Digraph, typename ArcMap>
543 583
    class BiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
544 584
    public:
545 585
      typedef typename Digraph::Node Node;
546 586
      typedef typename Digraph::Arc Arc;
547 587
      typedef typename Digraph::Edge Edge;
548 588

	
549 589
      BiNodeConnectedComponentsVisitor(const Digraph& graph,
550 590
                                       ArcMap& compMap, int &compNum)
551 591
        : _graph(graph), _compMap(compMap), _compNum(compNum),
552 592
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
553 593

	
554 594
      void start(const Node& node) {
555 595
        _predMap.set(node, INVALID);
556 596
      }
557 597

	
558 598
      void reach(const Node& node) {
559 599
        _numMap.set(node, _num);
560 600
        _retMap.set(node, _num);
561 601
        ++_num;
562 602
      }
563 603

	
564 604
      void discover(const Arc& edge) {
565 605
        Node target = _graph.target(edge);
566 606
        _predMap.set(target, edge);
... ...
@@ -607,299 +647,317 @@
607 647
      }
608 648

	
609 649
    private:
610 650
      const Digraph& _graph;
611 651
      ArcMap& _compMap;
612 652
      int& _compNum;
613 653

	
614 654
      typename Digraph::template NodeMap<int> _numMap;
615 655
      typename Digraph::template NodeMap<int> _retMap;
616 656
      typename Digraph::template NodeMap<Arc> _predMap;
617 657
      std::stack<Edge> _edgeStack;
618 658
      int _num;
619 659
    };
620 660

	
621 661

	
622 662
    template <typename Digraph, typename NodeMap>
623 663
    class BiNodeConnectedCutNodesVisitor : public DfsVisitor<Digraph> {
624 664
    public:
625 665
      typedef typename Digraph::Node Node;
626 666
      typedef typename Digraph::Arc Arc;
627 667
      typedef typename Digraph::Edge Edge;
628 668

	
629 669
      BiNodeConnectedCutNodesVisitor(const Digraph& graph, NodeMap& cutMap,
630 670
                                     int& cutNum)
631 671
        : _graph(graph), _cutMap(cutMap), _cutNum(cutNum),
632 672
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
633 673

	
634 674
      void start(const Node& node) {
635 675
        _predMap.set(node, INVALID);
636 676
        rootCut = false;
637 677
      }
638 678

	
639 679
      void reach(const Node& node) {
640 680
        _numMap.set(node, _num);
641 681
        _retMap.set(node, _num);
642 682
        ++_num;
643 683
      }
644 684

	
645 685
      void discover(const Arc& edge) {
646 686
        _predMap.set(_graph.target(edge), _graph.source(edge));
647 687
      }
648 688

	
649 689
      void examine(const Arc& edge) {
650 690
        if (_graph.source(edge) == _graph.target(edge) &&
651 691
            _graph.direction(edge)) {
652 692
          if (!_cutMap[_graph.source(edge)]) {
653 693
            _cutMap.set(_graph.source(edge), true);
654 694
            ++_cutNum;
655 695
          }
656 696
          return;
657 697
        }
658 698
        if (_predMap[_graph.source(edge)] == _graph.target(edge)) return;
659 699
        if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) {
660 700
          _retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]);
661 701
        }
662 702
      }
663 703

	
664 704
      void backtrack(const Arc& edge) {
665 705
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
666 706
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
667 707
        }
668 708
        if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) {
669 709
          if (_predMap[_graph.source(edge)] != INVALID) {
670 710
            if (!_cutMap[_graph.source(edge)]) {
671 711
              _cutMap.set(_graph.source(edge), true);
672 712
              ++_cutNum;
673 713
            }
674 714
          } else if (rootCut) {
675 715
            if (!_cutMap[_graph.source(edge)]) {
676 716
              _cutMap.set(_graph.source(edge), true);
677 717
              ++_cutNum;
678 718
            }
679 719
          } else {
680 720
            rootCut = true;
681 721
          }
682 722
        }
683 723
      }
684 724

	
685 725
    private:
686 726
      const Digraph& _graph;
687 727
      NodeMap& _cutMap;
688 728
      int& _cutNum;
689 729

	
690 730
      typename Digraph::template NodeMap<int> _numMap;
691 731
      typename Digraph::template NodeMap<int> _retMap;
692 732
      typename Digraph::template NodeMap<Node> _predMap;
693 733
      std::stack<Edge> _edgeStack;
694 734
      int _num;
695 735
      bool rootCut;
696 736
    };
697 737

	
698 738
  }
699 739

	
700 740
  template <typename Graph>
701 741
  int countBiNodeConnectedComponents(const Graph& graph);
702 742

	
703
  /// \ingroup connectivity
743
  /// \ingroup graph_properties
704 744
  ///
705
  /// \brief Checks the graph is bi-node-connected.
745
  /// \brief Check whether an undirected graph is bi-node-connected.
706 746
  ///
707
  /// This function checks that the undirected graph is bi-node-connected
708
  /// graph. The graph is bi-node-connected if any two undirected edge is
709
  /// on same circle.
747
  /// This function checks whether the given undirected graph is 
748
  /// bi-node-connected, i.e. any two edges are on same circle.
710 749
  ///
711
  /// \param graph The graph.
712
  /// \return %True when the graph bi-node-connected.
750
  /// \return \c true if the graph bi-node-connected.
751
  /// \note By definition, the empty graph is bi-node-connected.
752
  ///
753
  /// \see countBiNodeConnectedComponents(), biNodeConnectedComponents()
713 754
  template <typename Graph>
714 755
  bool biNodeConnected(const Graph& graph) {
715 756
    return countBiNodeConnectedComponents(graph) <= 1;
716 757
  }
717 758

	
718
  /// \ingroup connectivity
759
  /// \ingroup graph_properties
719 760
  ///
720
  /// \brief Count the biconnected components.
761
  /// \brief Count the number of bi-node-connected components of an 
762
  /// undirected graph.
721 763
  ///
722
  /// This function finds the bi-node-connected components in an undirected
723
  /// graph. The biconnected components are the classes of an equivalence
724
  /// relation on the undirected edges. Two undirected edge is in relationship
725
  /// when they are on same circle.
764
  /// This function counts the number of bi-node-connected components of
765
  /// the given undirected graph.
726 766
  ///
727
  /// \param graph The graph.
728
  /// \return The number of components.
767
  /// The bi-node-connected components are the classes of an equivalence
768
  /// relation on the edges of a undirected graph. Two edges are in the
769
  /// same class if they are on same circle.
770
  ///
771
  /// \return The number of bi-node-connected components.
772
  ///
773
  /// \see biNodeConnected(), biNodeConnectedComponents()
729 774
  template <typename Graph>
730 775
  int countBiNodeConnectedComponents(const Graph& graph) {
731 776
    checkConcept<concepts::Graph, Graph>();
732 777
    typedef typename Graph::NodeIt NodeIt;
733 778

	
734 779
    using namespace _connectivity_bits;
735 780

	
736 781
    typedef CountBiNodeConnectedComponentsVisitor<Graph> Visitor;
737 782

	
738 783
    int compNum = 0;
739 784
    Visitor visitor(graph, compNum);
740 785

	
741 786
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
742 787
    dfs.init();
743 788

	
744 789
    for (NodeIt it(graph); it != INVALID; ++it) {
745 790
      if (!dfs.reached(it)) {
746 791
        dfs.addSource(it);
747 792
        dfs.start();
748 793
      }
749 794
    }
750 795
    return compNum;
751 796
  }
752 797

	
753
  /// \ingroup connectivity
798
  /// \ingroup graph_properties
754 799
  ///
755
  /// \brief Find the bi-node-connected components.
800
  /// \brief Find the bi-node-connected components of an undirected graph.
756 801
  ///
757
  /// This function finds the bi-node-connected components in an undirected
758
  /// graph. The bi-node-connected components are the classes of an equivalence
759
  /// relation on the undirected edges. Two undirected edge are in relationship
760
  /// when they are on same circle.
802
  /// This function finds the bi-node-connected components of the given
803
  /// undirected graph.
761 804
  ///
762
  /// \param graph The graph.
763
  /// \retval compMap A writable uedge map. The values will be set from 0
764
  /// to the number of the biconnected components minus one. Each values
765
  /// of the map will be set exactly once, the values of a certain component
766
  /// will be set continuously.
767
  /// \return The number of components.
805
  /// The bi-node-connected components are the classes of an equivalence
806
  /// relation on the edges of a undirected graph. Two edges are in the
807
  /// same class if they are on same circle.
768 808
  ///
809
  /// \image html node_biconnected_components.png
810
  /// \image latex node_biconnected_components.eps "bi-node-connected components" width=\textwidth
811
  ///
812
  /// \param graph The undirected graph.
813
  /// \retval compMap A writable edge map. The values will be set from 0
814
  /// to the number of the bi-node-connected components minus one. Each
815
  /// value of the map will be set exactly once, and the values of a 
816
  /// certain component will be set continuously.
817
  /// \return The number of bi-node-connected components.
818
  ///
819
  /// \see biNodeConnected(), countBiNodeConnectedComponents()
769 820
  template <typename Graph, typename EdgeMap>
770 821
  int biNodeConnectedComponents(const Graph& graph,
771 822
                                EdgeMap& compMap) {
772 823
    checkConcept<concepts::Graph, Graph>();
773 824
    typedef typename Graph::NodeIt NodeIt;
774 825
    typedef typename Graph::Edge Edge;
775 826
    checkConcept<concepts::WriteMap<Edge, int>, EdgeMap>();
776 827

	
777 828
    using namespace _connectivity_bits;
778 829

	
779 830
    typedef BiNodeConnectedComponentsVisitor<Graph, EdgeMap> Visitor;
780 831

	
781 832
    int compNum = 0;
782 833
    Visitor visitor(graph, compMap, compNum);
783 834

	
784 835
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
785 836
    dfs.init();
786 837

	
787 838
    for (NodeIt it(graph); it != INVALID; ++it) {
788 839
      if (!dfs.reached(it)) {
789 840
        dfs.addSource(it);
790 841
        dfs.start();
791 842
      }
792 843
    }
793 844
    return compNum;
794 845
  }
795 846

	
796
  /// \ingroup connectivity
847
  /// \ingroup graph_properties
797 848
  ///
798
  /// \brief Find the bi-node-connected cut nodes.
849
  /// \brief Find the bi-node-connected cut nodes in an undirected graph.
799 850
  ///
800
  /// This function finds the bi-node-connected cut nodes in an undirected
801
  /// graph. The bi-node-connected components are the classes of an equivalence
802
  /// relation on the undirected edges. Two undirected edges are in
803
  /// relationship when they are on same circle. The biconnected components
804
  /// are separted by nodes which are the cut nodes of the components.
851
  /// This function finds the bi-node-connected cut nodes in the given
852
  /// undirected graph.
805 853
  ///
806
  /// \param graph The graph.
807
  /// \retval cutMap A writable edge map. The values will be set true when
808
  /// the node separate two or more components.
854
  /// The bi-node-connected components are the classes of an equivalence
855
  /// relation on the edges of a undirected graph. Two edges are in the
856
  /// same class if they are on same circle.
857
  /// The bi-node-connected components are separted by the cut nodes of
858
  /// the components.
859
  ///
860
  /// \param graph The undirected graph.
861
  /// \retval cutMap A writable node map. The values will be set to 
862
  /// \c true for the nodes that separate two or more components
863
  /// (exactly once for each cut node), and will not be changed for
864
  /// other nodes.
809 865
  /// \return The number of the cut nodes.
866
  ///
867
  /// \see biNodeConnected(), biNodeConnectedComponents()
810 868
  template <typename Graph, typename NodeMap>
811 869
  int biNodeConnectedCutNodes(const Graph& graph, NodeMap& cutMap) {
812 870
    checkConcept<concepts::Graph, Graph>();
813 871
    typedef typename Graph::Node Node;
814 872
    typedef typename Graph::NodeIt NodeIt;
815 873
    checkConcept<concepts::WriteMap<Node, bool>, NodeMap>();
816 874

	
817 875
    using namespace _connectivity_bits;
818 876

	
819 877
    typedef BiNodeConnectedCutNodesVisitor<Graph, NodeMap> Visitor;
820 878

	
821 879
    int cutNum = 0;
822 880
    Visitor visitor(graph, cutMap, cutNum);
823 881

	
824 882
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
825 883
    dfs.init();
826 884

	
827 885
    for (NodeIt it(graph); it != INVALID; ++it) {
828 886
      if (!dfs.reached(it)) {
829 887
        dfs.addSource(it);
830 888
        dfs.start();
831 889
      }
832 890
    }
833 891
    return cutNum;
834 892
  }
835 893

	
836 894
  namespace _connectivity_bits {
837 895

	
838 896
    template <typename Digraph>
839 897
    class CountBiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
840 898
    public:
841 899
      typedef typename Digraph::Node Node;
842 900
      typedef typename Digraph::Arc Arc;
843 901
      typedef typename Digraph::Edge Edge;
844 902

	
845 903
      CountBiEdgeConnectedComponentsVisitor(const Digraph& graph, int &compNum)
846 904
        : _graph(graph), _compNum(compNum),
847 905
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
848 906

	
849 907
      void start(const Node& node) {
850 908
        _predMap.set(node, INVALID);
851 909
      }
852 910

	
853 911
      void reach(const Node& node) {
854 912
        _numMap.set(node, _num);
855 913
        _retMap.set(node, _num);
856 914
        ++_num;
857 915
      }
858 916

	
859 917
      void leave(const Node& node) {
860 918
        if (_numMap[node] <= _retMap[node]) {
861 919
          ++_compNum;
862 920
        }
863 921
      }
864 922

	
865 923
      void discover(const Arc& edge) {
866 924
        _predMap.set(_graph.target(edge), edge);
867 925
      }
868 926

	
869 927
      void examine(const Arc& edge) {
870 928
        if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) {
871 929
          return;
872 930
        }
873 931
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
874 932
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
875 933
        }
876 934
      }
877 935

	
878 936
      void backtrack(const Arc& edge) {
879 937
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
880 938
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
881 939
        }
882 940
      }
883 941

	
884 942
    private:
885 943
      const Digraph& _graph;
886 944
      int& _compNum;
887 945

	
888 946
      typename Digraph::template NodeMap<int> _numMap;
889 947
      typename Digraph::template NodeMap<int> _retMap;
890 948
      typename Digraph::template NodeMap<Arc> _predMap;
891 949
      int _num;
892 950
    };
893 951

	
894 952
    template <typename Digraph, typename NodeMap>
895 953
    class BiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
896 954
    public:
897 955
      typedef typename Digraph::Node Node;
898 956
      typedef typename Digraph::Arc Arc;
899 957
      typedef typename Digraph::Edge Edge;
900 958

	
901 959
      BiEdgeConnectedComponentsVisitor(const Digraph& graph,
902 960
                                       NodeMap& compMap, int &compNum)
903 961
        : _graph(graph), _compMap(compMap), _compNum(compNum),
904 962
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
905 963

	
... ...
@@ -930,646 +988,678 @@
930 988
        _predMap.set(_graph.target(edge), edge);
931 989
      }
932 990

	
933 991
      void examine(const Arc& edge) {
934 992
        if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) {
935 993
          return;
936 994
        }
937 995
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
938 996
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
939 997
        }
940 998
      }
941 999

	
942 1000
      void backtrack(const Arc& edge) {
943 1001
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
944 1002
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
945 1003
        }
946 1004
      }
947 1005

	
948 1006
    private:
949 1007
      const Digraph& _graph;
950 1008
      NodeMap& _compMap;
951 1009
      int& _compNum;
952 1010

	
953 1011
      typename Digraph::template NodeMap<int> _numMap;
954 1012
      typename Digraph::template NodeMap<int> _retMap;
955 1013
      typename Digraph::template NodeMap<Arc> _predMap;
956 1014
      std::stack<Node> _nodeStack;
957 1015
      int _num;
958 1016
    };
959 1017

	
960 1018

	
961 1019
    template <typename Digraph, typename ArcMap>
962 1020
    class BiEdgeConnectedCutEdgesVisitor : public DfsVisitor<Digraph> {
963 1021
    public:
964 1022
      typedef typename Digraph::Node Node;
965 1023
      typedef typename Digraph::Arc Arc;
966 1024
      typedef typename Digraph::Edge Edge;
967 1025

	
968 1026
      BiEdgeConnectedCutEdgesVisitor(const Digraph& graph,
969 1027
                                     ArcMap& cutMap, int &cutNum)
970 1028
        : _graph(graph), _cutMap(cutMap), _cutNum(cutNum),
971 1029
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
972 1030

	
973 1031
      void start(const Node& node) {
974 1032
        _predMap[node] = INVALID;
975 1033
      }
976 1034

	
977 1035
      void reach(const Node& node) {
978 1036
        _numMap.set(node, _num);
979 1037
        _retMap.set(node, _num);
980 1038
        ++_num;
981 1039
      }
982 1040

	
983 1041
      void leave(const Node& node) {
984 1042
        if (_numMap[node] <= _retMap[node]) {
985 1043
          if (_predMap[node] != INVALID) {
986 1044
            _cutMap.set(_predMap[node], true);
987 1045
            ++_cutNum;
988 1046
          }
989 1047
        }
990 1048
      }
991 1049

	
992 1050
      void discover(const Arc& edge) {
993 1051
        _predMap.set(_graph.target(edge), edge);
994 1052
      }
995 1053

	
996 1054
      void examine(const Arc& edge) {
997 1055
        if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) {
998 1056
          return;
999 1057
        }
1000 1058
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
1001 1059
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
1002 1060
        }
1003 1061
      }
1004 1062

	
1005 1063
      void backtrack(const Arc& edge) {
1006 1064
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
1007 1065
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
1008 1066
        }
1009 1067
      }
1010 1068

	
1011 1069
    private:
1012 1070
      const Digraph& _graph;
1013 1071
      ArcMap& _cutMap;
1014 1072
      int& _cutNum;
1015 1073

	
1016 1074
      typename Digraph::template NodeMap<int> _numMap;
1017 1075
      typename Digraph::template NodeMap<int> _retMap;
1018 1076
      typename Digraph::template NodeMap<Arc> _predMap;
1019 1077
      int _num;
1020 1078
    };
1021 1079
  }
1022 1080

	
1023 1081
  template <typename Graph>
1024 1082
  int countBiEdgeConnectedComponents(const Graph& graph);
1025 1083

	
1026
  /// \ingroup connectivity
1084
  /// \ingroup graph_properties
1027 1085
  ///
1028
  /// \brief Checks that the graph is bi-edge-connected.
1086
  /// \brief Check whether an undirected graph is bi-edge-connected.
1029 1087
  ///
1030
  /// This function checks that the graph is bi-edge-connected. The undirected
1031
  /// graph is bi-edge-connected when any two nodes are connected with two
1032
  /// edge-disjoint paths.
1088
  /// This function checks whether the given undirected graph is 
1089
  /// bi-edge-connected, i.e. any two nodes are connected with at least
1090
  /// two edge-disjoint paths.
1033 1091
  ///
1034
  /// \param graph The undirected graph.
1035
  /// \return The number of components.
1092
  /// \return \c true if the graph is bi-edge-connected.
1093
  /// \note By definition, the empty graph is bi-edge-connected.
1094
  ///
1095
  /// \see countBiEdgeConnectedComponents(), biEdgeConnectedComponents()
1036 1096
  template <typename Graph>
1037 1097
  bool biEdgeConnected(const Graph& graph) {
1038 1098
    return countBiEdgeConnectedComponents(graph) <= 1;
1039 1099
  }
1040 1100

	
1041
  /// \ingroup connectivity
1101
  /// \ingroup graph_properties
1042 1102
  ///
1043
  /// \brief Count the bi-edge-connected components.
1103
  /// \brief Count the number of bi-edge-connected components of an
1104
  /// undirected graph.
1044 1105
  ///
1045
  /// This function count the bi-edge-connected components in an undirected
1046
  /// graph. The bi-edge-connected components are the classes of an equivalence
1047
  /// relation on the nodes. Two nodes are in relationship when they are
1048
  /// connected with at least two edge-disjoint paths.
1106
  /// This function counts the number of bi-edge-connected components of
1107
  /// the given undirected graph.
1049 1108
  ///
1050
  /// \param graph The undirected graph.
1051
  /// \return The number of components.
1109
  /// The bi-edge-connected components are the classes of an equivalence
1110
  /// relation on the nodes of an undirected graph. Two nodes are in the
1111
  /// same class if they are connected with at least two edge-disjoint
1112
  /// paths.
1113
  ///
1114
  /// \return The number of bi-edge-connected components.
1115
  ///
1116
  /// \see biEdgeConnected(), biEdgeConnectedComponents()
1052 1117
  template <typename Graph>
1053 1118
  int countBiEdgeConnectedComponents(const Graph& graph) {
1054 1119
    checkConcept<concepts::Graph, Graph>();
1055 1120
    typedef typename Graph::NodeIt NodeIt;
1056 1121

	
1057 1122
    using namespace _connectivity_bits;
1058 1123

	
1059 1124
    typedef CountBiEdgeConnectedComponentsVisitor<Graph> Visitor;
1060 1125

	
1061 1126
    int compNum = 0;
1062 1127
    Visitor visitor(graph, compNum);
1063 1128

	
1064 1129
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
1065 1130
    dfs.init();
1066 1131

	
1067 1132
    for (NodeIt it(graph); it != INVALID; ++it) {
1068 1133
      if (!dfs.reached(it)) {
1069 1134
        dfs.addSource(it);
1070 1135
        dfs.start();
1071 1136
      }
1072 1137
    }
1073 1138
    return compNum;
1074 1139
  }
1075 1140

	
1076
  /// \ingroup connectivity
1141
  /// \ingroup graph_properties
1077 1142
  ///
1078
  /// \brief Find the bi-edge-connected components.
1143
  /// \brief Find the bi-edge-connected components of an undirected graph.
1079 1144
  ///
1080
  /// This function finds the bi-edge-connected components in an undirected
1081
  /// graph. The bi-edge-connected components are the classes of an equivalence
1082
  /// relation on the nodes. Two nodes are in relationship when they are
1083
  /// connected at least two edge-disjoint paths.
1145
  /// This function finds the bi-edge-connected components of the given
1146
  /// undirected graph.
1084 1147
  ///
1085
  /// \param graph The graph.
1148
  /// The bi-edge-connected components are the classes of an equivalence
1149
  /// relation on the nodes of an undirected graph. Two nodes are in the
1150
  /// same class if they are connected with at least two edge-disjoint
1151
  /// paths.
1152
  ///
1153
  /// \image html edge_biconnected_components.png
1154
  /// \image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth
1155
  ///
1156
  /// \param graph The undirected graph.
1086 1157
  /// \retval compMap A writable node map. The values will be set from 0 to
1087
  /// the number of the biconnected components minus one. Each values
1088
  /// of the map will be set exactly once, the values of a certain component
1089
  /// will be set continuously.
1090
  /// \return The number of components.
1158
  /// the number of the bi-edge-connected components minus one. Each value
1159
  /// of the map will be set exactly once, and the values of a certain
1160
  /// component will be set continuously.
1161
  /// \return The number of bi-edge-connected components.
1091 1162
  ///
1163
  /// \see biEdgeConnected(), countBiEdgeConnectedComponents()
1092 1164
  template <typename Graph, typename NodeMap>
1093 1165
  int biEdgeConnectedComponents(const Graph& graph, NodeMap& compMap) {
1094 1166
    checkConcept<concepts::Graph, Graph>();
1095 1167
    typedef typename Graph::NodeIt NodeIt;
1096 1168
    typedef typename Graph::Node Node;
1097 1169
    checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
1098 1170

	
1099 1171
    using namespace _connectivity_bits;
1100 1172

	
1101 1173
    typedef BiEdgeConnectedComponentsVisitor<Graph, NodeMap> Visitor;
1102 1174

	
1103 1175
    int compNum = 0;
1104 1176
    Visitor visitor(graph, compMap, compNum);
1105 1177

	
1106 1178
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
1107 1179
    dfs.init();
1108 1180

	
1109 1181
    for (NodeIt it(graph); it != INVALID; ++it) {
1110 1182
      if (!dfs.reached(it)) {
1111 1183
        dfs.addSource(it);
1112 1184
        dfs.start();
1113 1185
      }
1114 1186
    }
1115 1187
    return compNum;
1116 1188
  }
1117 1189

	
1118
  /// \ingroup connectivity
1190
  /// \ingroup graph_properties
1119 1191
  ///
1120
  /// \brief Find the bi-edge-connected cut edges.
1192
  /// \brief Find the bi-edge-connected cut edges in an undirected graph.
1121 1193
  ///
1122
  /// This function finds the bi-edge-connected components in an undirected
1123
  /// graph. The bi-edge-connected components are the classes of an equivalence
1124
  /// relation on the nodes. Two nodes are in relationship when they are
1125
  /// connected with at least two edge-disjoint paths. The bi-edge-connected
1126
  /// components are separted by edges which are the cut edges of the
1127
  /// components.
1194
  /// This function finds the bi-edge-connected cut edges in the given
1195
  /// undirected graph. 
1128 1196
  ///
1129
  /// \param graph The graph.
1130
  /// \retval cutMap A writable node map. The values will be set true when the
1131
  /// edge is a cut edge.
1197
  /// The bi-edge-connected components are the classes of an equivalence
1198
  /// relation on the nodes of an undirected graph. Two nodes are in the
1199
  /// same class if they are connected with at least two edge-disjoint
1200
  /// paths.
1201
  /// The bi-edge-connected components are separted by the cut edges of
1202
  /// the components.
1203
  ///
1204
  /// \param graph The undirected graph.
1205
  /// \retval cutMap A writable edge map. The values will be set to \c true
1206
  /// for the cut edges (exactly once for each cut edge), and will not be
1207
  /// changed for other edges.
1132 1208
  /// \return The number of cut edges.
1209
  ///
1210
  /// \see biEdgeConnected(), biEdgeConnectedComponents()
1133 1211
  template <typename Graph, typename EdgeMap>
1134 1212
  int biEdgeConnectedCutEdges(const Graph& graph, EdgeMap& cutMap) {
1135 1213
    checkConcept<concepts::Graph, Graph>();
1136 1214
    typedef typename Graph::NodeIt NodeIt;
1137 1215
    typedef typename Graph::Edge Edge;
1138 1216
    checkConcept<concepts::WriteMap<Edge, bool>, EdgeMap>();
1139 1217

	
1140 1218
    using namespace _connectivity_bits;
1141 1219

	
1142 1220
    typedef BiEdgeConnectedCutEdgesVisitor<Graph, EdgeMap> Visitor;
1143 1221

	
1144 1222
    int cutNum = 0;
1145 1223
    Visitor visitor(graph, cutMap, cutNum);
1146 1224

	
1147 1225
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
1148 1226
    dfs.init();
1149 1227

	
1150 1228
    for (NodeIt it(graph); it != INVALID; ++it) {
1151 1229
      if (!dfs.reached(it)) {
1152 1230
        dfs.addSource(it);
1153 1231
        dfs.start();
1154 1232
      }
1155 1233
    }
1156 1234
    return cutNum;
1157 1235
  }
1158 1236

	
1159 1237

	
1160 1238
  namespace _connectivity_bits {
1161 1239

	
1162 1240
    template <typename Digraph, typename IntNodeMap>
1163 1241
    class TopologicalSortVisitor : public DfsVisitor<Digraph> {
1164 1242
    public:
1165 1243
      typedef typename Digraph::Node Node;
1166 1244
      typedef typename Digraph::Arc edge;
1167 1245

	
1168 1246
      TopologicalSortVisitor(IntNodeMap& order, int num)
1169 1247
        : _order(order), _num(num) {}
1170 1248

	
1171 1249
      void leave(const Node& node) {
1172 1250
        _order.set(node, --_num);
1173 1251
      }
1174 1252

	
1175 1253
    private:
1176 1254
      IntNodeMap& _order;
1177 1255
      int _num;
1178 1256
    };
1179 1257

	
1180 1258
  }
1181 1259

	
1182
  /// \ingroup connectivity
1260
  /// \ingroup graph_properties
1261
  ///
1262
  /// \brief Check whether a digraph is DAG.
1263
  ///
1264
  /// This function checks whether the given digraph is DAG, i.e.
1265
  /// \e Directed \e Acyclic \e Graph.
1266
  /// \return \c true if there is no directed cycle in the digraph.
1267
  /// \see acyclic()
1268
  template <typename Digraph>
1269
  bool dag(const Digraph& digraph) {
1270

	
1271
    checkConcept<concepts::Digraph, Digraph>();
1272

	
1273
    typedef typename Digraph::Node Node;
1274
    typedef typename Digraph::NodeIt NodeIt;
1275
    typedef typename Digraph::Arc Arc;
1276

	
1277
    typedef typename Digraph::template NodeMap<bool> ProcessedMap;
1278

	
1279
    typename Dfs<Digraph>::template SetProcessedMap<ProcessedMap>::
1280
      Create dfs(digraph);
1281

	
1282
    ProcessedMap processed(digraph);
1283
    dfs.processedMap(processed);
1284

	
1285
    dfs.init();
1286
    for (NodeIt it(digraph); it != INVALID; ++it) {
1287
      if (!dfs.reached(it)) {
1288
        dfs.addSource(it);
1289
        while (!dfs.emptyQueue()) {
1290
          Arc arc = dfs.nextArc();
1291
          Node target = digraph.target(arc);
1292
          if (dfs.reached(target) && !processed[target]) {
1293
            return false;
1294
          }
1295
          dfs.processNextArc();
1296
        }
1297
      }
1298
    }
1299
    return true;
1300
  }
1301

	
1302
  /// \ingroup graph_properties
1183 1303
  ///
1184 1304
  /// \brief Sort the nodes of a DAG into topolgical order.
1185 1305
  ///
1186
  /// Sort the nodes of a DAG into topolgical order.
1306
  /// This function sorts the nodes of the given acyclic digraph (DAG)
1307
  /// into topolgical order.
1187 1308
  ///
1188
  /// \param graph The graph. It must be directed and acyclic.
1309
  /// \param digraph The digraph, which must be DAG.
1189 1310
  /// \retval order A writable node map. The values will be set from 0 to
1190
  /// the number of the nodes in the graph minus one. Each values of the map
1191
  /// will be set exactly once, the values  will be set descending order.
1311
  /// the number of the nodes in the digraph minus one. Each value of the
1312
  /// map will be set exactly once, and the values will be set descending
1313
  /// order.
1192 1314
  ///
1193
  /// \see checkedTopologicalSort
1194
  /// \see dag
1315
  /// \see dag(), checkedTopologicalSort()
1195 1316
  template <typename Digraph, typename NodeMap>
1196
  void topologicalSort(const Digraph& graph, NodeMap& order) {
1317
  void topologicalSort(const Digraph& digraph, NodeMap& order) {
1197 1318
    using namespace _connectivity_bits;
1198 1319

	
1199 1320
    checkConcept<concepts::Digraph, Digraph>();
1200 1321
    checkConcept<concepts::WriteMap<typename Digraph::Node, int>, NodeMap>();
1201 1322

	
1202 1323
    typedef typename Digraph::Node Node;
1203 1324
    typedef typename Digraph::NodeIt NodeIt;
1204 1325
    typedef typename Digraph::Arc Arc;
1205 1326

	
1206 1327
    TopologicalSortVisitor<Digraph, NodeMap>
1207
      visitor(order, countNodes(graph));
1328
      visitor(order, countNodes(digraph));
1208 1329

	
1209 1330
    DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> >
1210
      dfs(graph, visitor);
1331
      dfs(digraph, visitor);
1211 1332

	
1212 1333
    dfs.init();
1213
    for (NodeIt it(graph); it != INVALID; ++it) {
1334
    for (NodeIt it(digraph); it != INVALID; ++it) {
1214 1335
      if (!dfs.reached(it)) {
1215 1336
        dfs.addSource(it);
1216 1337
        dfs.start();
1217 1338
      }
1218 1339
    }
1219 1340
  }
1220 1341

	
1221
  /// \ingroup connectivity
1342
  /// \ingroup graph_properties
1222 1343
  ///
1223 1344
  /// \brief Sort the nodes of a DAG into topolgical order.
1224 1345
  ///
1225
  /// Sort the nodes of a DAG into topolgical order. It also checks
1226
  /// that the given graph is DAG.
1346
  /// This function sorts the nodes of the given acyclic digraph (DAG)
1347
  /// into topolgical order and also checks whether the given digraph
1348
  /// is DAG.
1227 1349
  ///
1228
  /// \param digraph The graph. It must be directed and acyclic.
1229
  /// \retval order A readable - writable node map. The values will be set
1230
  /// from 0 to the number of the nodes in the graph minus one. Each values
1231
  /// of the map will be set exactly once, the values will be set descending
1232
  /// order.
1233
  /// \return %False when the graph is not DAG.
1350
  /// \param digraph The digraph.
1351
  /// \retval order A readable and writable node map. The values will be
1352
  /// set from 0 to the number of the nodes in the digraph minus one. 
1353
  /// Each value of the map will be set exactly once, and the values will
1354
  /// be set descending order.
1355
  /// \return \c false if the digraph is not DAG.
1234 1356
  ///
1235
  /// \see topologicalSort
1236
  /// \see dag
1357
  /// \see dag(), topologicalSort()
1237 1358
  template <typename Digraph, typename NodeMap>
1238 1359
  bool checkedTopologicalSort(const Digraph& digraph, NodeMap& order) {
1239 1360
    using namespace _connectivity_bits;
1240 1361

	
1241 1362
    checkConcept<concepts::Digraph, Digraph>();
1242 1363
    checkConcept<concepts::ReadWriteMap<typename Digraph::Node, int>,
1243 1364
      NodeMap>();
1244 1365

	
1245 1366
    typedef typename Digraph::Node Node;
1246 1367
    typedef typename Digraph::NodeIt NodeIt;
1247 1368
    typedef typename Digraph::Arc Arc;
1248 1369

	
1249 1370
    for (NodeIt it(digraph); it != INVALID; ++it) {
1250 1371
      order.set(it, -1);
1251 1372
    }
1252 1373

	
1253 1374
    TopologicalSortVisitor<Digraph, NodeMap>
1254 1375
      visitor(order, countNodes(digraph));
1255 1376

	
1256 1377
    DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> >
1257 1378
      dfs(digraph, visitor);
1258 1379

	
1259 1380
    dfs.init();
1260 1381
    for (NodeIt it(digraph); it != INVALID; ++it) {
1261 1382
      if (!dfs.reached(it)) {
1262 1383
        dfs.addSource(it);
1263 1384
        while (!dfs.emptyQueue()) {
1264 1385
           Arc arc = dfs.nextArc();
1265 1386
           Node target = digraph.target(arc);
1266 1387
           if (dfs.reached(target) && order[target] == -1) {
1267 1388
             return false;
1268 1389
           }
1269 1390
           dfs.processNextArc();
1270 1391
         }
1271 1392
      }
1272 1393
    }
1273 1394
    return true;
1274 1395
  }
1275 1396

	
1276
  /// \ingroup connectivity
1397
  /// \ingroup graph_properties
1277 1398
  ///
1278
  /// \brief Check that the given directed graph is a DAG.
1399
  /// \brief Check whether an undirected graph is acyclic.
1279 1400
  ///
1280
  /// Check that the given directed graph is a DAG. The DAG is
1281
  /// an Directed Acyclic Digraph.
1282
  /// \return %False when the graph is not DAG.
1283
  /// \see acyclic
1284
  template <typename Digraph>
1285
  bool dag(const Digraph& digraph) {
1286

	
1287
    checkConcept<concepts::Digraph, Digraph>();
1288

	
1289
    typedef typename Digraph::Node Node;
1290
    typedef typename Digraph::NodeIt NodeIt;
1291
    typedef typename Digraph::Arc Arc;
1292

	
1293
    typedef typename Digraph::template NodeMap<bool> ProcessedMap;
1294

	
1295
    typename Dfs<Digraph>::template SetProcessedMap<ProcessedMap>::
1296
      Create dfs(digraph);
1297

	
1298
    ProcessedMap processed(digraph);
1299
    dfs.processedMap(processed);
1300

	
1301
    dfs.init();
1302
    for (NodeIt it(digraph); it != INVALID; ++it) {
1303
      if (!dfs.reached(it)) {
1304
        dfs.addSource(it);
1305
        while (!dfs.emptyQueue()) {
1306
          Arc edge = dfs.nextArc();
1307
          Node target = digraph.target(edge);
1308
          if (dfs.reached(target) && !processed[target]) {
1309
            return false;
1310
          }
1311
          dfs.processNextArc();
1312
        }
1313
      }
1314
    }
1315
    return true;
1316
  }
1317

	
1318
  /// \ingroup connectivity
1319
  ///
1320
  /// \brief Check that the given undirected graph is acyclic.
1321
  ///
1322
  /// Check that the given undirected graph acyclic.
1323
  /// \param graph The undirected graph.
1324
  /// \return %True when there is no circle in the graph.
1325
  /// \see dag
1401
  /// This function checks whether the given undirected graph is acyclic.
1402
  /// \return \c true if there is no cycle in the graph.
1403
  /// \see dag()
1326 1404
  template <typename Graph>
1327 1405
  bool acyclic(const Graph& graph) {
1328 1406
    checkConcept<concepts::Graph, Graph>();
1329 1407
    typedef typename Graph::Node Node;
1330 1408
    typedef typename Graph::NodeIt NodeIt;
1331 1409
    typedef typename Graph::Arc Arc;
1332 1410
    Dfs<Graph> dfs(graph);
1333 1411
    dfs.init();
1334 1412
    for (NodeIt it(graph); it != INVALID; ++it) {
1335 1413
      if (!dfs.reached(it)) {
1336 1414
        dfs.addSource(it);
1337 1415
        while (!dfs.emptyQueue()) {
1338
          Arc edge = dfs.nextArc();
1339
          Node source = graph.source(edge);
1340
          Node target = graph.target(edge);
1416
          Arc arc = dfs.nextArc();
1417
          Node source = graph.source(arc);
1418
          Node target = graph.target(arc);
1341 1419
          if (dfs.reached(target) &&
1342
              dfs.predArc(source) != graph.oppositeArc(edge)) {
1420
              dfs.predArc(source) != graph.oppositeArc(arc)) {
1343 1421
            return false;
1344 1422
          }
1345 1423
          dfs.processNextArc();
1346 1424
        }
1347 1425
      }
1348 1426
    }
1349 1427
    return true;
1350 1428
  }
1351 1429

	
1352
  /// \ingroup connectivity
1430
  /// \ingroup graph_properties
1353 1431
  ///
1354
  /// \brief Check that the given undirected graph is tree.
1432
  /// \brief Check whether an undirected graph is tree.
1355 1433
  ///
1356
  /// Check that the given undirected graph is tree.
1357
  /// \param graph The undirected graph.
1358
  /// \return %True when the graph is acyclic and connected.
1434
  /// This function checks whether the given undirected graph is tree.
1435
  /// \return \c true if the graph is acyclic and connected.
1436
  /// \see acyclic(), connected()
1359 1437
  template <typename Graph>
1360 1438
  bool tree(const Graph& graph) {
1361 1439
    checkConcept<concepts::Graph, Graph>();
1362 1440
    typedef typename Graph::Node Node;
1363 1441
    typedef typename Graph::NodeIt NodeIt;
1364 1442
    typedef typename Graph::Arc Arc;
1443
    if (NodeIt(graph) == INVALID) return true;
1365 1444
    Dfs<Graph> dfs(graph);
1366 1445
    dfs.init();
1367 1446
    dfs.addSource(NodeIt(graph));
1368 1447
    while (!dfs.emptyQueue()) {
1369
      Arc edge = dfs.nextArc();
1370
      Node source = graph.source(edge);
1371
      Node target = graph.target(edge);
1448
      Arc arc = dfs.nextArc();
1449
      Node source = graph.source(arc);
1450
      Node target = graph.target(arc);
1372 1451
      if (dfs.reached(target) &&
1373
          dfs.predArc(source) != graph.oppositeArc(edge)) {
1452
          dfs.predArc(source) != graph.oppositeArc(arc)) {
1374 1453
        return false;
1375 1454
      }
1376 1455
      dfs.processNextArc();
1377 1456
    }
1378 1457
    for (NodeIt it(graph); it != INVALID; ++it) {
1379 1458
      if (!dfs.reached(it)) {
1380 1459
        return false;
1381 1460
      }
1382 1461
    }
1383 1462
    return true;
1384 1463
  }
1385 1464

	
1386 1465
  namespace _connectivity_bits {
1387 1466

	
1388 1467
    template <typename Digraph>
1389 1468
    class BipartiteVisitor : public BfsVisitor<Digraph> {
1390 1469
    public:
1391 1470
      typedef typename Digraph::Arc Arc;
1392 1471
      typedef typename Digraph::Node Node;
1393 1472

	
1394 1473
      BipartiteVisitor(const Digraph& graph, bool& bipartite)
1395 1474
        : _graph(graph), _part(graph), _bipartite(bipartite) {}
1396 1475

	
1397 1476
      void start(const Node& node) {
1398 1477
        _part[node] = true;
1399 1478
      }
1400 1479
      void discover(const Arc& edge) {
1401 1480
        _part.set(_graph.target(edge), !_part[_graph.source(edge)]);
1402 1481
      }
1403 1482
      void examine(const Arc& edge) {
1404 1483
        _bipartite = _bipartite &&
1405 1484
          _part[_graph.target(edge)] != _part[_graph.source(edge)];
1406 1485
      }
1407 1486

	
1408 1487
    private:
1409 1488

	
1410 1489
      const Digraph& _graph;
1411 1490
      typename Digraph::template NodeMap<bool> _part;
1412 1491
      bool& _bipartite;
1413 1492
    };
1414 1493

	
1415 1494
    template <typename Digraph, typename PartMap>
1416 1495
    class BipartitePartitionsVisitor : public BfsVisitor<Digraph> {
1417 1496
    public:
1418 1497
      typedef typename Digraph::Arc Arc;
1419 1498
      typedef typename Digraph::Node Node;
1420 1499

	
1421 1500
      BipartitePartitionsVisitor(const Digraph& graph,
1422 1501
                                 PartMap& part, bool& bipartite)
1423 1502
        : _graph(graph), _part(part), _bipartite(bipartite) {}
1424 1503

	
1425 1504
      void start(const Node& node) {
1426 1505
        _part.set(node, true);
1427 1506
      }
1428 1507
      void discover(const Arc& edge) {
1429 1508
        _part.set(_graph.target(edge), !_part[_graph.source(edge)]);
1430 1509
      }
1431 1510
      void examine(const Arc& edge) {
1432 1511
        _bipartite = _bipartite &&
1433 1512
          _part[_graph.target(edge)] != _part[_graph.source(edge)];
1434 1513
      }
1435 1514

	
1436 1515
    private:
1437 1516

	
1438 1517
      const Digraph& _graph;
1439 1518
      PartMap& _part;
1440 1519
      bool& _bipartite;
1441 1520
    };
1442 1521
  }
1443 1522

	
1444
  /// \ingroup connectivity
1523
  /// \ingroup graph_properties
1445 1524
  ///
1446
  /// \brief Check if the given undirected graph is bipartite or not
1525
  /// \brief Check whether an undirected graph is bipartite.
1447 1526
  ///
1448
  /// The function checks if the given undirected \c graph graph is bipartite
1449
  /// or not. The \ref Bfs algorithm is used to calculate the result.
1450
  /// \param graph The undirected graph.
1451
  /// \return %True if \c graph is bipartite, %false otherwise.
1452
  /// \sa bipartitePartitions
1527
  /// The function checks whether the given undirected graph is bipartite.
1528
  /// \return \c true if the graph is bipartite.
1529
  ///
1530
  /// \see bipartitePartitions()
1453 1531
  template<typename Graph>
1454
  inline bool bipartite(const Graph &graph){
1532
  bool bipartite(const Graph &graph){
1455 1533
    using namespace _connectivity_bits;
1456 1534

	
1457 1535
    checkConcept<concepts::Graph, Graph>();
1458 1536

	
1459 1537
    typedef typename Graph::NodeIt NodeIt;
1460 1538
    typedef typename Graph::ArcIt ArcIt;
1461 1539

	
1462 1540
    bool bipartite = true;
1463 1541

	
1464 1542
    BipartiteVisitor<Graph>
1465 1543
      visitor(graph, bipartite);
1466 1544
    BfsVisit<Graph, BipartiteVisitor<Graph> >
1467 1545
      bfs(graph, visitor);
1468 1546
    bfs.init();
1469 1547
    for(NodeIt it(graph); it != INVALID; ++it) {
1470 1548
      if(!bfs.reached(it)){
1471 1549
        bfs.addSource(it);
1472 1550
        while (!bfs.emptyQueue()) {
1473 1551
          bfs.processNextNode();
1474 1552
          if (!bipartite) return false;
1475 1553
        }
1476 1554
      }
1477 1555
    }
1478 1556
    return true;
1479 1557
  }
1480 1558

	
1481
  /// \ingroup connectivity
1559
  /// \ingroup graph_properties
1482 1560
  ///
1483
  /// \brief Check if the given undirected graph is bipartite or not
1561
  /// \brief Find the bipartite partitions of an undirected graph.
1484 1562
  ///
1485
  /// The function checks if the given undirected graph is bipartite
1486
  /// or not. The  \ref  Bfs  algorithm  is   used  to  calculate the result.
1487
  /// During the execution, the \c partMap will be set as the two
1488
  /// partitions of the graph.
1563
  /// This function checks whether the given undirected graph is bipartite
1564
  /// and gives back the bipartite partitions.
1565
  ///
1566
  /// \image html bipartite_partitions.png
1567
  /// \image latex bipartite_partitions.eps "Bipartite partititions" width=\textwidth
1568
  ///
1489 1569
  /// \param graph The undirected graph.
1490
  /// \retval partMap A writable bool map of nodes. It will be set as the
1491
  /// two partitions of the graph.
1492
  /// \return %True if \c graph is bipartite, %false otherwise.
1570
  /// \retval partMap A writable node map of \c bool (or convertible) value
1571
  /// type. The values will be set to \c true for one component and
1572
  /// \c false for the other one.
1573
  /// \return \c true if the graph is bipartite, \c false otherwise.
1574
  ///
1575
  /// \see bipartite()
1493 1576
  template<typename Graph, typename NodeMap>
1494
  inline bool bipartitePartitions(const Graph &graph, NodeMap &partMap){
1577
  bool bipartitePartitions(const Graph &graph, NodeMap &partMap){
1495 1578
    using namespace _connectivity_bits;
1496 1579

	
1497 1580
    checkConcept<concepts::Graph, Graph>();
1581
    checkConcept<concepts::WriteMap<typename Graph::Node, bool>, NodeMap>();
1498 1582

	
1499 1583
    typedef typename Graph::Node Node;
1500 1584
    typedef typename Graph::NodeIt NodeIt;
1501 1585
    typedef typename Graph::ArcIt ArcIt;
1502 1586

	
1503 1587
    bool bipartite = true;
1504 1588

	
1505 1589
    BipartitePartitionsVisitor<Graph, NodeMap>
1506 1590
      visitor(graph, partMap, bipartite);
1507 1591
    BfsVisit<Graph, BipartitePartitionsVisitor<Graph, NodeMap> >
1508 1592
      bfs(graph, visitor);
1509 1593
    bfs.init();
1510 1594
    for(NodeIt it(graph); it != INVALID; ++it) {
1511 1595
      if(!bfs.reached(it)){
1512 1596
        bfs.addSource(it);
1513 1597
        while (!bfs.emptyQueue()) {
1514 1598
          bfs.processNextNode();
1515 1599
          if (!bipartite) return false;
1516 1600
        }
1517 1601
      }
1518 1602
    }
1519 1603
    return true;
1520 1604
  }
1521 1605

	
1522
  /// \brief Returns true when there are not loop edges in the graph.
1606
  /// \ingroup graph_properties
1523 1607
  ///
1524
  /// Returns true when there are not loop edges in the graph.
1525
  template <typename Digraph>
1526
  bool loopFree(const Digraph& digraph) {
1527
    for (typename Digraph::ArcIt it(digraph); it != INVALID; ++it) {
1528
      if (digraph.source(it) == digraph.target(it)) return false;
1608
  /// \brief Check whether the given graph contains no loop arcs/edges.
1609
  ///
1610
  /// This function returns \c true if there are no loop arcs/edges in
1611
  /// the given graph. It works for both directed and undirected graphs.
1612
  template <typename Graph>
1613
  bool loopFree(const Graph& graph) {
1614
    for (typename Graph::ArcIt it(graph); it != INVALID; ++it) {
1615
      if (graph.source(it) == graph.target(it)) return false;
1529 1616
    }
1530 1617
    return true;
1531 1618
  }
1532 1619

	
1533
  /// \brief Returns true when there are not parallel edges in the graph.
1620
  /// \ingroup graph_properties
1534 1621
  ///
1535
  /// Returns true when there are not parallel edges in the graph.
1536
  template <typename Digraph>
1537
  bool parallelFree(const Digraph& digraph) {
1538
    typename Digraph::template NodeMap<bool> reached(digraph, false);
1539
    for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
1540
      for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
1541
        if (reached[digraph.target(a)]) return false;
1542
        reached.set(digraph.target(a), true);
1622
  /// \brief Check whether the given graph contains no parallel arcs/edges.
1623
  ///
1624
  /// This function returns \c true if there are no parallel arcs/edges in
1625
  /// the given graph. It works for both directed and undirected graphs.
1626
  template <typename Graph>
1627
  bool parallelFree(const Graph& graph) {
1628
    typename Graph::template NodeMap<int> reached(graph, 0);
1629
    int cnt = 1;
1630
    for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
1631
      for (typename Graph::OutArcIt a(graph, n); a != INVALID; ++a) {
1632
        if (reached[graph.target(a)] == cnt) return false;
1633
        reached[graph.target(a)] = cnt;
1543 1634
      }
1544
      for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
1545
        reached.set(digraph.target(a), false);
1546
      }
1635
      ++cnt;
1547 1636
    }
1548 1637
    return true;
1549 1638
  }
1550 1639

	
1551
  /// \brief Returns true when there are not loop edges and parallel
1552
  /// edges in the graph.
1640
  /// \ingroup graph_properties
1553 1641
  ///
1554
  /// Returns true when there are not loop edges and parallel edges in
1555
  /// the graph.
1556
  template <typename Digraph>
1557
  bool simpleDigraph(const Digraph& digraph) {
1558
    typename Digraph::template NodeMap<bool> reached(digraph, false);
1559
    for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
1560
      reached.set(n, true);
1561
      for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
1562
        if (reached[digraph.target(a)]) return false;
1563
        reached.set(digraph.target(a), true);
1642
  /// \brief Check whether the given graph is simple.
1643
  ///
1644
  /// This function returns \c true if the given graph is simple, i.e.
1645
  /// it contains no loop arcs/edges and no parallel arcs/edges.
1646
  /// The function works for both directed and undirected graphs.
1647
  /// \see loopFree(), parallelFree()
1648
  template <typename Graph>
1649
  bool simpleGraph(const Graph& graph) {
1650
    typename Graph::template NodeMap<int> reached(graph, 0);
1651
    int cnt = 1;
1652
    for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
1653
      reached[n] = cnt;
1654
      for (typename Graph::OutArcIt a(graph, n); a != INVALID; ++a) {
1655
        if (reached[graph.target(a)] == cnt) return false;
1656
        reached[graph.target(a)] = cnt;
1564 1657
      }
1565
      for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
1566
        reached.set(digraph.target(a), false);
1567
      }
1568
      reached.set(n, false);
1658
      ++cnt;
1569 1659
    }
1570 1660
    return true;
1571 1661
  }
1572 1662

	
1573 1663
} //namespace lemon
1574 1664

	
1575 1665
#endif //LEMON_CONNECTIVITY_H
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_CORE_H
20 20
#define LEMON_CORE_H
21 21

	
22 22
#include <vector>
23 23
#include <algorithm>
24 24

	
25
#include <lemon/config.h>
25 26
#include <lemon/bits/enable_if.h>
26 27
#include <lemon/bits/traits.h>
27 28
#include <lemon/assert.h>
28 29

	
30
// Disable the following warnings when compiling with MSVC:
31
// C4250: 'class1' : inherits 'class2::member' via dominance
32
// C4355: 'this' : used in base member initializer list
33
// C4503: 'function' : decorated name length exceeded, name was truncated
34
// C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
35
// C4996: 'function': was declared deprecated
36
#ifdef _MSC_VER
37
#pragma warning( disable : 4250 4355 4503 4800 4996 )
38
#endif
39

	
29 40
///\file
30 41
///\brief LEMON core utilities.
31 42
///
32 43
///This header file contains core utilities for LEMON.
33 44
///It is automatically included by all graph types, therefore it usually
34 45
///do not have to be included directly.
35 46

	
36 47
namespace lemon {
37 48

	
38 49
  /// \brief Dummy type to make it easier to create invalid iterators.
39 50
  ///
40 51
  /// Dummy type to make it easier to create invalid iterators.
41 52
  /// See \ref INVALID for the usage.
42 53
  struct Invalid {
43 54
  public:
44 55
    bool operator==(Invalid) { return true;  }
45 56
    bool operator!=(Invalid) { return false; }
46 57
    bool operator< (Invalid) { return false; }
47 58
  };
48 59

	
49 60
  /// \brief Invalid iterators.
50 61
  ///
51 62
  /// \ref Invalid is a global type that converts to each iterator
52 63
  /// in such a way that the value of the target iterator will be invalid.
53 64
#ifdef LEMON_ONLY_TEMPLATES
54 65
  const Invalid INVALID = Invalid();
55 66
#else
56 67
  extern const Invalid INVALID;
57 68
#endif
58 69

	
59 70
  /// \addtogroup gutils
60 71
  /// @{
61 72

	
62 73
  ///Create convenience typedefs for the digraph types and iterators
63 74

	
64 75
  ///This \c \#define creates convenient type definitions for the following
65 76
  ///types of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
66 77
  ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
67 78
  ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
68 79
  ///
69 80
  ///\note If the graph type is a dependent type, ie. the graph type depend
70 81
  ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
71 82
  ///macro.
72 83
#define DIGRAPH_TYPEDEFS(Digraph)                                       \
73 84
  typedef Digraph::Node Node;                                           \
74 85
  typedef Digraph::NodeIt NodeIt;                                       \
75 86
  typedef Digraph::Arc Arc;                                             \
76 87
  typedef Digraph::ArcIt ArcIt;                                         \
77 88
  typedef Digraph::InArcIt InArcIt;                                     \
78 89
  typedef Digraph::OutArcIt OutArcIt;                                   \
79 90
  typedef Digraph::NodeMap<bool> BoolNodeMap;                           \
80 91
  typedef Digraph::NodeMap<int> IntNodeMap;                             \
81 92
  typedef Digraph::NodeMap<double> DoubleNodeMap;                       \
82 93
  typedef Digraph::ArcMap<bool> BoolArcMap;                             \
83 94
  typedef Digraph::ArcMap<int> IntArcMap;                               \
84 95
  typedef Digraph::ArcMap<double> DoubleArcMap
85 96

	
86 97
  ///Create convenience typedefs for the digraph types and iterators
87 98

	
88 99
  ///\see DIGRAPH_TYPEDEFS
89 100
  ///
90 101
  ///\note Use this macro, if the graph type is a dependent type,
91 102
  ///ie. the graph type depend on a template parameter.
92 103
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph)                              \
93 104
  typedef typename Digraph::Node Node;                                  \
94 105
  typedef typename Digraph::NodeIt NodeIt;                              \
95 106
  typedef typename Digraph::Arc Arc;                                    \
96 107
  typedef typename Digraph::ArcIt ArcIt;                                \
97 108
  typedef typename Digraph::InArcIt InArcIt;                            \
98 109
  typedef typename Digraph::OutArcIt OutArcIt;                          \
99 110
  typedef typename Digraph::template NodeMap<bool> BoolNodeMap;         \
100 111
  typedef typename Digraph::template NodeMap<int> IntNodeMap;           \
101 112
  typedef typename Digraph::template NodeMap<double> DoubleNodeMap;     \
102 113
  typedef typename Digraph::template ArcMap<bool> BoolArcMap;           \
103 114
  typedef typename Digraph::template ArcMap<int> IntArcMap;             \
104 115
  typedef typename Digraph::template ArcMap<double> DoubleArcMap
105 116

	
106 117
  ///Create convenience typedefs for the graph types and iterators
107 118

	
108 119
  ///This \c \#define creates the same convenient type definitions as defined
109 120
  ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
110 121
  ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
111 122
  ///\c DoubleEdgeMap.
112 123
  ///
113 124
  ///\note If the graph type is a dependent type, ie. the graph type depend
114 125
  ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
115 126
  ///macro.
116 127
#define GRAPH_TYPEDEFS(Graph)                                           \
117 128
  DIGRAPH_TYPEDEFS(Graph);                                              \
118 129
  typedef Graph::Edge Edge;                                             \
119 130
  typedef Graph::EdgeIt EdgeIt;                                         \
120 131
  typedef Graph::IncEdgeIt IncEdgeIt;                                   \
121 132
  typedef Graph::EdgeMap<bool> BoolEdgeMap;                             \
122 133
  typedef Graph::EdgeMap<int> IntEdgeMap;                               \
123 134
  typedef Graph::EdgeMap<double> DoubleEdgeMap
124 135

	
... ...
@@ -940,906 +951,915 @@
940 951

	
941 952
  /// \brief Copy a graph to another graph.
942 953
  ///
943 954
  /// This function copies a graph to another graph.
944 955
  /// The complete usage of it is detailed in the GraphCopy class,
945 956
  /// but a short example shows a basic work:
946 957
  ///\code
947 958
  /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
948 959
  ///\endcode
949 960
  ///
950 961
  /// After the copy the \c nr map will contain the mapping from the
951 962
  /// nodes of the \c from graph to the nodes of the \c to graph and
952 963
  /// \c ecr will contain the mapping from the edges of the \c to graph
953 964
  /// to the edges of the \c from graph.
954 965
  ///
955 966
  /// \see GraphCopy
956 967
  template <typename From, typename To>
957 968
  GraphCopy<From, To>
958 969
  graphCopy(const From& from, To& to) {
959 970
    return GraphCopy<From, To>(from, to);
960 971
  }
961 972

	
962 973
  namespace _core_bits {
963 974

	
964 975
    template <typename Graph, typename Enable = void>
965 976
    struct FindArcSelector {
966 977
      typedef typename Graph::Node Node;
967 978
      typedef typename Graph::Arc Arc;
968 979
      static Arc find(const Graph &g, Node u, Node v, Arc e) {
969 980
        if (e == INVALID) {
970 981
          g.firstOut(e, u);
971 982
        } else {
972 983
          g.nextOut(e);
973 984
        }
974 985
        while (e != INVALID && g.target(e) != v) {
975 986
          g.nextOut(e);
976 987
        }
977 988
        return e;
978 989
      }
979 990
    };
980 991

	
981 992
    template <typename Graph>
982 993
    struct FindArcSelector<
983 994
      Graph,
984 995
      typename enable_if<typename Graph::FindArcTag, void>::type>
985 996
    {
986 997
      typedef typename Graph::Node Node;
987 998
      typedef typename Graph::Arc Arc;
988 999
      static Arc find(const Graph &g, Node u, Node v, Arc prev) {
989 1000
        return g.findArc(u, v, prev);
990 1001
      }
991 1002
    };
992 1003
  }
993 1004

	
994 1005
  /// \brief Find an arc between two nodes of a digraph.
995 1006
  ///
996 1007
  /// This function finds an arc from node \c u to node \c v in the
997 1008
  /// digraph \c g.
998 1009
  ///
999 1010
  /// If \c prev is \ref INVALID (this is the default value), then
1000 1011
  /// it finds the first arc from \c u to \c v. Otherwise it looks for
1001 1012
  /// the next arc from \c u to \c v after \c prev.
1002 1013
  /// \return The found arc or \ref INVALID if there is no such an arc.
1003 1014
  ///
1004 1015
  /// Thus you can iterate through each arc from \c u to \c v as it follows.
1005 1016
  ///\code
1006 1017
  /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1007 1018
  ///   ...
1008 1019
  /// }
1009 1020
  ///\endcode
1010 1021
  ///
1011 1022
  /// \note \ref ConArcIt provides iterator interface for the same
1012 1023
  /// functionality.
1013 1024
  ///
1014 1025
  ///\sa ConArcIt
1015 1026
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1016 1027
  template <typename Graph>
1017 1028
  inline typename Graph::Arc
1018 1029
  findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1019 1030
          typename Graph::Arc prev = INVALID) {
1020 1031
    return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1021 1032
  }
1022 1033

	
1023 1034
  /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1024 1035
  ///
1025 1036
  /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1026 1037
  /// a higher level interface for the \ref findArc() function. You can
1027 1038
  /// use it the following way:
1028 1039
  ///\code
1029 1040
  /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1030 1041
  ///   ...
1031 1042
  /// }
1032 1043
  ///\endcode
1033 1044
  ///
1034 1045
  ///\sa findArc()
1035 1046
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1036
  template <typename _Graph>
1037
  class ConArcIt : public _Graph::Arc {
1047
  template <typename GR>
1048
  class ConArcIt : public GR::Arc {
1049
    typedef typename GR::Arc Parent;
1050

	
1038 1051
  public:
1039 1052

	
1040
    typedef _Graph Graph;
1041
    typedef typename Graph::Arc Parent;
1042

	
1043
    typedef typename Graph::Arc Arc;
1044
    typedef typename Graph::Node Node;
1053
    typedef typename GR::Arc Arc;
1054
    typedef typename GR::Node Node;
1045 1055

	
1046 1056
    /// \brief Constructor.
1047 1057
    ///
1048 1058
    /// Construct a new ConArcIt iterating on the arcs that
1049 1059
    /// connects nodes \c u and \c v.
1050
    ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
1060
    ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
1051 1061
      Parent::operator=(findArc(_graph, u, v));
1052 1062
    }
1053 1063

	
1054 1064
    /// \brief Constructor.
1055 1065
    ///
1056 1066
    /// Construct a new ConArcIt that continues the iterating from arc \c a.
1057
    ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
1067
    ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
1058 1068

	
1059 1069
    /// \brief Increment operator.
1060 1070
    ///
1061 1071
    /// It increments the iterator and gives back the next arc.
1062 1072
    ConArcIt& operator++() {
1063 1073
      Parent::operator=(findArc(_graph, _graph.source(*this),
1064 1074
                                _graph.target(*this), *this));
1065 1075
      return *this;
1066 1076
    }
1067 1077
  private:
1068
    const Graph& _graph;
1078
    const GR& _graph;
1069 1079
  };
1070 1080

	
1071 1081
  namespace _core_bits {
1072 1082

	
1073 1083
    template <typename Graph, typename Enable = void>
1074 1084
    struct FindEdgeSelector {
1075 1085
      typedef typename Graph::Node Node;
1076 1086
      typedef typename Graph::Edge Edge;
1077 1087
      static Edge find(const Graph &g, Node u, Node v, Edge e) {
1078 1088
        bool b;
1079 1089
        if (u != v) {
1080 1090
          if (e == INVALID) {
1081 1091
            g.firstInc(e, b, u);
1082 1092
          } else {
1083 1093
            b = g.u(e) == u;
1084 1094
            g.nextInc(e, b);
1085 1095
          }
1086 1096
          while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1087 1097
            g.nextInc(e, b);
1088 1098
          }
1089 1099
        } else {
1090 1100
          if (e == INVALID) {
1091 1101
            g.firstInc(e, b, u);
1092 1102
          } else {
1093 1103
            b = true;
1094 1104
            g.nextInc(e, b);
1095 1105
          }
1096 1106
          while (e != INVALID && (!b || g.v(e) != v)) {
1097 1107
            g.nextInc(e, b);
1098 1108
          }
1099 1109
        }
1100 1110
        return e;
1101 1111
      }
1102 1112
    };
1103 1113

	
1104 1114
    template <typename Graph>
1105 1115
    struct FindEdgeSelector<
1106 1116
      Graph,
1107 1117
      typename enable_if<typename Graph::FindEdgeTag, void>::type>
1108 1118
    {
1109 1119
      typedef typename Graph::Node Node;
1110 1120
      typedef typename Graph::Edge Edge;
1111 1121
      static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1112 1122
        return g.findEdge(u, v, prev);
1113 1123
      }
1114 1124
    };
1115 1125
  }
1116 1126

	
1117 1127
  /// \brief Find an edge between two nodes of a graph.
1118 1128
  ///
1119 1129
  /// This function finds an edge from node \c u to node \c v in graph \c g.
1120 1130
  /// If node \c u and node \c v is equal then each loop edge
1121 1131
  /// will be enumerated once.
1122 1132
  ///
1123 1133
  /// If \c prev is \ref INVALID (this is the default value), then
1124 1134
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
1125 1135
  /// the next edge from \c u to \c v after \c prev.
1126 1136
  /// \return The found edge or \ref INVALID if there is no such an edge.
1127 1137
  ///
1128 1138
  /// Thus you can iterate through each edge between \c u and \c v
1129 1139
  /// as it follows.
1130 1140
  ///\code
1131 1141
  /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1132 1142
  ///   ...
1133 1143
  /// }
1134 1144
  ///\endcode
1135 1145
  ///
1136 1146
  /// \note \ref ConEdgeIt provides iterator interface for the same
1137 1147
  /// functionality.
1138 1148
  ///
1139 1149
  ///\sa ConEdgeIt
1140 1150
  template <typename Graph>
1141 1151
  inline typename Graph::Edge
1142 1152
  findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1143 1153
            typename Graph::Edge p = INVALID) {
1144 1154
    return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1145 1155
  }
1146 1156

	
1147 1157
  /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1148 1158
  ///
1149 1159
  /// Iterator for iterating on parallel edges connecting the same nodes.
1150 1160
  /// It is a higher level interface for the findEdge() function. You can
1151 1161
  /// use it the following way:
1152 1162
  ///\code
1153 1163
  /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1154 1164
  ///   ...
1155 1165
  /// }
1156 1166
  ///\endcode
1157 1167
  ///
1158 1168
  ///\sa findEdge()
1159
  template <typename _Graph>
1160
  class ConEdgeIt : public _Graph::Edge {
1169
  template <typename GR>
1170
  class ConEdgeIt : public GR::Edge {
1171
    typedef typename GR::Edge Parent;
1172

	
1161 1173
  public:
1162 1174

	
1163
    typedef _Graph Graph;
1164
    typedef typename Graph::Edge Parent;
1165

	
1166
    typedef typename Graph::Edge Edge;
1167
    typedef typename Graph::Node Node;
1175
    typedef typename GR::Edge Edge;
1176
    typedef typename GR::Node Node;
1168 1177

	
1169 1178
    /// \brief Constructor.
1170 1179
    ///
1171 1180
    /// Construct a new ConEdgeIt iterating on the edges that
1172 1181
    /// connects nodes \c u and \c v.
1173
    ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1182
    ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1174 1183
      Parent::operator=(findEdge(_graph, _u, _v));
1175 1184
    }
1176 1185

	
1177 1186
    /// \brief Constructor.
1178 1187
    ///
1179 1188
    /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1180
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
1189
    ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
1181 1190

	
1182 1191
    /// \brief Increment operator.
1183 1192
    ///
1184 1193
    /// It increments the iterator and gives back the next edge.
1185 1194
    ConEdgeIt& operator++() {
1186 1195
      Parent::operator=(findEdge(_graph, _u, _v, *this));
1187 1196
      return *this;
1188 1197
    }
1189 1198
  private:
1190
    const Graph& _graph;
1199
    const GR& _graph;
1191 1200
    Node _u, _v;
1192 1201
  };
1193 1202

	
1194 1203

	
1195 1204
  ///Dynamic arc look-up between given endpoints.
1196 1205

	
1197 1206
  ///Using this class, you can find an arc in a digraph from a given
1198 1207
  ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1199 1208
  ///where <em>d</em> is the out-degree of the source node.
1200 1209
  ///
1201 1210
  ///It is possible to find \e all parallel arcs between two nodes with
1202 1211
  ///the \c operator() member.
1203 1212
  ///
1204 1213
  ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1205 1214
  ///\ref AllArcLookUp if your digraph is not changed so frequently.
1206 1215
  ///
1207 1216
  ///This class uses a self-adjusting binary search tree, the Splay tree
1208 1217
  ///of Sleator and Tarjan to guarantee the logarithmic amortized
1209 1218
  ///time bound for arc look-ups. This class also guarantees the
1210 1219
  ///optimal time bound in a constant factor for any distribution of
1211 1220
  ///queries.
1212 1221
  ///
1213
  ///\tparam G The type of the underlying digraph.
1222
  ///\tparam GR The type of the underlying digraph.
1214 1223
  ///
1215 1224
  ///\sa ArcLookUp
1216 1225
  ///\sa AllArcLookUp
1217
  template<class G>
1226
  template <typename GR>
1218 1227
  class DynArcLookUp
1219
    : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
1228
    : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
1220 1229
  {
1221
  public:
1222
    typedef typename ItemSetTraits<G, typename G::Arc>
1230
    typedef typename ItemSetTraits<GR, typename GR::Arc>
1223 1231
    ::ItemNotifier::ObserverBase Parent;
1224 1232

	
1225
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1226
    typedef G Digraph;
1233
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1234

	
1235
  public:
1236

	
1237
    /// The Digraph type
1238
    typedef GR Digraph;
1227 1239

	
1228 1240
  protected:
1229 1241

	
1230
    class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type {
1242
    class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type {
1243
      typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
1244

	
1231 1245
    public:
1232 1246

	
1233
      typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent;
1234

	
1235
      AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
1247
      AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
1236 1248

	
1237 1249
      virtual void add(const Node& node) {
1238 1250
        Parent::add(node);
1239 1251
        Parent::set(node, INVALID);
1240 1252
      }
1241 1253

	
1242 1254
      virtual void add(const std::vector<Node>& nodes) {
1243 1255
        Parent::add(nodes);
1244 1256
        for (int i = 0; i < int(nodes.size()); ++i) {
1245 1257
          Parent::set(nodes[i], INVALID);
1246 1258
        }
1247 1259
      }
1248 1260

	
1249 1261
      virtual void build() {
1250 1262
        Parent::build();
1251 1263
        Node it;
1252 1264
        typename Parent::Notifier* nf = Parent::notifier();
1253 1265
        for (nf->first(it); it != INVALID; nf->next(it)) {
1254 1266
          Parent::set(it, INVALID);
1255 1267
        }
1256 1268
      }
1257 1269
    };
1258 1270

	
1259
    const Digraph &_g;
1260
    AutoNodeMap _head;
1261
    typename Digraph::template ArcMap<Arc> _parent;
1262
    typename Digraph::template ArcMap<Arc> _left;
1263
    typename Digraph::template ArcMap<Arc> _right;
1264

	
1265 1271
    class ArcLess {
1266 1272
      const Digraph &g;
1267 1273
    public:
1268 1274
      ArcLess(const Digraph &_g) : g(_g) {}
1269 1275
      bool operator()(Arc a,Arc b) const
1270 1276
      {
1271 1277
        return g.target(a)<g.target(b);
1272 1278
      }
1273 1279
    };
1274 1280

	
1281
  protected: 
1282

	
1283
    const Digraph &_g;
1284
    AutoNodeMap _head;
1285
    typename Digraph::template ArcMap<Arc> _parent;
1286
    typename Digraph::template ArcMap<Arc> _left;
1287
    typename Digraph::template ArcMap<Arc> _right;
1288

	
1275 1289
  public:
1276 1290

	
1277 1291
    ///Constructor
1278 1292

	
1279 1293
    ///Constructor.
1280 1294
    ///
1281 1295
    ///It builds up the search database.
1282 1296
    DynArcLookUp(const Digraph &g)
1283 1297
      : _g(g),_head(g),_parent(g),_left(g),_right(g)
1284 1298
    {
1285 1299
      Parent::attach(_g.notifier(typename Digraph::Arc()));
1286 1300
      refresh();
1287 1301
    }
1288 1302

	
1289 1303
  protected:
1290 1304

	
1291 1305
    virtual void add(const Arc& arc) {
1292 1306
      insert(arc);
1293 1307
    }
1294 1308

	
1295 1309
    virtual void add(const std::vector<Arc>& arcs) {
1296 1310
      for (int i = 0; i < int(arcs.size()); ++i) {
1297 1311
        insert(arcs[i]);
1298 1312
      }
1299 1313
    }
1300 1314

	
1301 1315
    virtual void erase(const Arc& arc) {
1302 1316
      remove(arc);
1303 1317
    }
1304 1318

	
1305 1319
    virtual void erase(const std::vector<Arc>& arcs) {
1306 1320
      for (int i = 0; i < int(arcs.size()); ++i) {
1307 1321
        remove(arcs[i]);
1308 1322
      }
1309 1323
    }
1310 1324

	
1311 1325
    virtual void build() {
1312 1326
      refresh();
1313 1327
    }
1314 1328

	
1315 1329
    virtual void clear() {
1316 1330
      for(NodeIt n(_g);n!=INVALID;++n) {
1317
        _head.set(n, INVALID);
1331
        _head[n] = INVALID;
1318 1332
      }
1319 1333
    }
1320 1334

	
1321 1335
    void insert(Arc arc) {
1322 1336
      Node s = _g.source(arc);
1323 1337
      Node t = _g.target(arc);
1324
      _left.set(arc, INVALID);
1325
      _right.set(arc, INVALID);
1338
      _left[arc] = INVALID;
1339
      _right[arc] = INVALID;
1326 1340

	
1327 1341
      Arc e = _head[s];
1328 1342
      if (e == INVALID) {
1329
        _head.set(s, arc);
1330
        _parent.set(arc, INVALID);
1343
        _head[s] = arc;
1344
        _parent[arc] = INVALID;
1331 1345
        return;
1332 1346
      }
1333 1347
      while (true) {
1334 1348
        if (t < _g.target(e)) {
1335 1349
          if (_left[e] == INVALID) {
1336
            _left.set(e, arc);
1337
            _parent.set(arc, e);
1350
            _left[e] = arc;
1351
            _parent[arc] = e;
1338 1352
            splay(arc);
1339 1353
            return;
1340 1354
          } else {
1341 1355
            e = _left[e];
1342 1356
          }
1343 1357
        } else {
1344 1358
          if (_right[e] == INVALID) {
1345
            _right.set(e, arc);
1346
            _parent.set(arc, e);
1359
            _right[e] = arc;
1360
            _parent[arc] = e;
1347 1361
            splay(arc);
1348 1362
            return;
1349 1363
          } else {
1350 1364
            e = _right[e];
1351 1365
          }
1352 1366
        }
1353 1367
      }
1354 1368
    }
1355 1369

	
1356 1370
    void remove(Arc arc) {
1357 1371
      if (_left[arc] == INVALID) {
1358 1372
        if (_right[arc] != INVALID) {
1359
          _parent.set(_right[arc], _parent[arc]);
1373
          _parent[_right[arc]] = _parent[arc];
1360 1374
        }
1361 1375
        if (_parent[arc] != INVALID) {
1362 1376
          if (_left[_parent[arc]] == arc) {
1363
            _left.set(_parent[arc], _right[arc]);
1377
            _left[_parent[arc]] = _right[arc];
1364 1378
          } else {
1365
            _right.set(_parent[arc], _right[arc]);
1379
            _right[_parent[arc]] = _right[arc];
1366 1380
          }
1367 1381
        } else {
1368
          _head.set(_g.source(arc), _right[arc]);
1382
          _head[_g.source(arc)] = _right[arc];
1369 1383
        }
1370 1384
      } else if (_right[arc] == INVALID) {
1371
        _parent.set(_left[arc], _parent[arc]);
1385
        _parent[_left[arc]] = _parent[arc];
1372 1386
        if (_parent[arc] != INVALID) {
1373 1387
          if (_left[_parent[arc]] == arc) {
1374
            _left.set(_parent[arc], _left[arc]);
1388
            _left[_parent[arc]] = _left[arc];
1375 1389
          } else {
1376
            _right.set(_parent[arc], _left[arc]);
1390
            _right[_parent[arc]] = _left[arc];
1377 1391
          }
1378 1392
        } else {
1379
          _head.set(_g.source(arc), _left[arc]);
1393
          _head[_g.source(arc)] = _left[arc];
1380 1394
        }
1381 1395
      } else {
1382 1396
        Arc e = _left[arc];
1383 1397
        if (_right[e] != INVALID) {
1384 1398
          e = _right[e];
1385 1399
          while (_right[e] != INVALID) {
1386 1400
            e = _right[e];
1387 1401
          }
1388 1402
          Arc s = _parent[e];
1389
          _right.set(_parent[e], _left[e]);
1403
          _right[_parent[e]] = _left[e];
1390 1404
          if (_left[e] != INVALID) {
1391
            _parent.set(_left[e], _parent[e]);
1405
            _parent[_left[e]] = _parent[e];
1392 1406
          }
1393 1407

	
1394
          _left.set(e, _left[arc]);
1395
          _parent.set(_left[arc], e);
1396
          _right.set(e, _right[arc]);
1397
          _parent.set(_right[arc], e);
1408
          _left[e] = _left[arc];
1409
          _parent[_left[arc]] = e;
1410
          _right[e] = _right[arc];
1411
          _parent[_right[arc]] = e;
1398 1412

	
1399
          _parent.set(e, _parent[arc]);
1413
          _parent[e] = _parent[arc];
1400 1414
          if (_parent[arc] != INVALID) {
1401 1415
            if (_left[_parent[arc]] == arc) {
1402
              _left.set(_parent[arc], e);
1416
              _left[_parent[arc]] = e;
1403 1417
            } else {
1404
              _right.set(_parent[arc], e);
1418
              _right[_parent[arc]] = e;
1405 1419
            }
1406 1420
          }
1407 1421
          splay(s);
1408 1422
        } else {
1409
          _right.set(e, _right[arc]);
1410
          _parent.set(_right[arc], e);
1411
          _parent.set(e, _parent[arc]);
1423
          _right[e] = _right[arc];
1424
          _parent[_right[arc]] = e;
1425
          _parent[e] = _parent[arc];
1412 1426

	
1413 1427
          if (_parent[arc] != INVALID) {
1414 1428
            if (_left[_parent[arc]] == arc) {
1415
              _left.set(_parent[arc], e);
1429
              _left[_parent[arc]] = e;
1416 1430
            } else {
1417
              _right.set(_parent[arc], e);
1431
              _right[_parent[arc]] = e;
1418 1432
            }
1419 1433
          } else {
1420
            _head.set(_g.source(arc), e);
1434
            _head[_g.source(arc)] = e;
1421 1435
          }
1422 1436
        }
1423 1437
      }
1424 1438
    }
1425 1439

	
1426 1440
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1427 1441
    {
1428 1442
      int m=(a+b)/2;
1429 1443
      Arc me=v[m];
1430 1444
      if (a < m) {
1431 1445
        Arc left = refreshRec(v,a,m-1);
1432
        _left.set(me, left);
1433
        _parent.set(left, me);
1446
        _left[me] = left;
1447
        _parent[left] = me;
1434 1448
      } else {
1435
        _left.set(me, INVALID);
1449
        _left[me] = INVALID;
1436 1450
      }
1437 1451
      if (m < b) {
1438 1452
        Arc right = refreshRec(v,m+1,b);
1439
        _right.set(me, right);
1440
        _parent.set(right, me);
1453
        _right[me] = right;
1454
        _parent[right] = me;
1441 1455
      } else {
1442
        _right.set(me, INVALID);
1456
        _right[me] = INVALID;
1443 1457
      }
1444 1458
      return me;
1445 1459
    }
1446 1460

	
1447 1461
    void refresh() {
1448 1462
      for(NodeIt n(_g);n!=INVALID;++n) {
1449 1463
        std::vector<Arc> v;
1450 1464
        for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
1451 1465
        if (!v.empty()) {
1452 1466
          std::sort(v.begin(),v.end(),ArcLess(_g));
1453 1467
          Arc head = refreshRec(v,0,v.size()-1);
1454
          _head.set(n, head);
1455
          _parent.set(head, INVALID);
1468
          _head[n] = head;
1469
          _parent[head] = INVALID;
1456 1470
        }
1457
        else _head.set(n, INVALID);
1471
        else _head[n] = INVALID;
1458 1472
      }
1459 1473
    }
1460 1474

	
1461 1475
    void zig(Arc v) {
1462 1476
      Arc w = _parent[v];
1463
      _parent.set(v, _parent[w]);
1464
      _parent.set(w, v);
1465
      _left.set(w, _right[v]);
1466
      _right.set(v, w);
1477
      _parent[v] = _parent[w];
1478
      _parent[w] = v;
1479
      _left[w] = _right[v];
1480
      _right[v] = w;
1467 1481
      if (_parent[v] != INVALID) {
1468 1482
        if (_right[_parent[v]] == w) {
1469
          _right.set(_parent[v], v);
1483
          _right[_parent[v]] = v;
1470 1484
        } else {
1471
          _left.set(_parent[v], v);
1485
          _left[_parent[v]] = v;
1472 1486
        }
1473 1487
      }
1474 1488
      if (_left[w] != INVALID){
1475
        _parent.set(_left[w], w);
1489
        _parent[_left[w]] = w;
1476 1490
      }
1477 1491
    }
1478 1492

	
1479 1493
    void zag(Arc v) {
1480 1494
      Arc w = _parent[v];
1481
      _parent.set(v, _parent[w]);
1482
      _parent.set(w, v);
1483
      _right.set(w, _left[v]);
1484
      _left.set(v, w);
1495
      _parent[v] = _parent[w];
1496
      _parent[w] = v;
1497
      _right[w] = _left[v];
1498
      _left[v] = w;
1485 1499
      if (_parent[v] != INVALID){
1486 1500
        if (_left[_parent[v]] == w) {
1487
          _left.set(_parent[v], v);
1501
          _left[_parent[v]] = v;
1488 1502
        } else {
1489
          _right.set(_parent[v], v);
1503
          _right[_parent[v]] = v;
1490 1504
        }
1491 1505
      }
1492 1506
      if (_right[w] != INVALID){
1493
        _parent.set(_right[w], w);
1507
        _parent[_right[w]] = w;
1494 1508
      }
1495 1509
    }
1496 1510

	
1497 1511
    void splay(Arc v) {
1498 1512
      while (_parent[v] != INVALID) {
1499 1513
        if (v == _left[_parent[v]]) {
1500 1514
          if (_parent[_parent[v]] == INVALID) {
1501 1515
            zig(v);
1502 1516
          } else {
1503 1517
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1504 1518
              zig(_parent[v]);
1505 1519
              zig(v);
1506 1520
            } else {
1507 1521
              zig(v);
1508 1522
              zag(v);
1509 1523
            }
1510 1524
          }
1511 1525
        } else {
1512 1526
          if (_parent[_parent[v]] == INVALID) {
1513 1527
            zag(v);
1514 1528
          } else {
1515 1529
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1516 1530
              zag(v);
1517 1531
              zig(v);
1518 1532
            } else {
1519 1533
              zag(_parent[v]);
1520 1534
              zag(v);
1521 1535
            }
1522 1536
          }
1523 1537
        }
1524 1538
      }
1525 1539
      _head[_g.source(v)] = v;
1526 1540
    }
1527 1541

	
1528 1542

	
1529 1543
  public:
1530 1544

	
1531 1545
    ///Find an arc between two nodes.
1532 1546

	
1533 1547
    ///Find an arc between two nodes.
1534 1548
    ///\param s The source node.
1535 1549
    ///\param t The target node.
1536 1550
    ///\param p The previous arc between \c s and \c t. It it is INVALID or
1537 1551
    ///not given, the operator finds the first appropriate arc.
1538 1552
    ///\return An arc from \c s to \c t after \c p or
1539 1553
    ///\ref INVALID if there is no more.
1540 1554
    ///
1541 1555
    ///For example, you can count the number of arcs from \c u to \c v in the
1542 1556
    ///following way.
1543 1557
    ///\code
1544 1558
    ///DynArcLookUp<ListDigraph> ae(g);
1545 1559
    ///...
1546 1560
    ///int n = 0;
1547 1561
    ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
1548 1562
    ///\endcode
1549 1563
    ///
1550 1564
    ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
1551 1565
    ///amortized time, specifically, the time complexity of the lookups
1552 1566
    ///is equal to the optimal search tree implementation for the
1553 1567
    ///current query distribution in a constant factor.
1554 1568
    ///
1555 1569
    ///\note This is a dynamic data structure, therefore the data
1556 1570
    ///structure is updated after each graph alteration. Thus although
1557 1571
    ///this data structure is theoretically faster than \ref ArcLookUp
1558 1572
    ///and \ref AllArcLookUp, it often provides worse performance than
1559 1573
    ///them.
1560 1574
    Arc operator()(Node s, Node t, Arc p = INVALID) const  {
1561 1575
      if (p == INVALID) {
1562 1576
        Arc a = _head[s];
1563 1577
        if (a == INVALID) return INVALID;
1564 1578
        Arc r = INVALID;
1565 1579
        while (true) {
1566 1580
          if (_g.target(a) < t) {
1567 1581
            if (_right[a] == INVALID) {
1568 1582
              const_cast<DynArcLookUp&>(*this).splay(a);
1569 1583
              return r;
1570 1584
            } else {
1571 1585
              a = _right[a];
1572 1586
            }
1573 1587
          } else {
1574 1588
            if (_g.target(a) == t) {
1575 1589
              r = a;
1576 1590
            }
1577 1591
            if (_left[a] == INVALID) {
1578 1592
              const_cast<DynArcLookUp&>(*this).splay(a);
1579 1593
              return r;
1580 1594
            } else {
1581 1595
              a = _left[a];
1582 1596
            }
1583 1597
          }
1584 1598
        }
1585 1599
      } else {
1586 1600
        Arc a = p;
1587 1601
        if (_right[a] != INVALID) {
1588 1602
          a = _right[a];
1589 1603
          while (_left[a] != INVALID) {
1590 1604
            a = _left[a];
1591 1605
          }
1592 1606
          const_cast<DynArcLookUp&>(*this).splay(a);
1593 1607
        } else {
1594 1608
          while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
1595 1609
            a = _parent[a];
1596 1610
          }
1597 1611
          if (_parent[a] == INVALID) {
1598 1612
            return INVALID;
1599 1613
          } else {
1600 1614
            a = _parent[a];
1601 1615
            const_cast<DynArcLookUp&>(*this).splay(a);
1602 1616
          }
1603 1617
        }
1604 1618
        if (_g.target(a) == t) return a;
1605 1619
        else return INVALID;
1606 1620
      }
1607 1621
    }
1608 1622

	
1609 1623
  };
1610 1624

	
1611 1625
  ///Fast arc look-up between given endpoints.
1612 1626

	
1613 1627
  ///Using this class, you can find an arc in a digraph from a given
1614 1628
  ///source to a given target in time <em>O</em>(log<em>d</em>),
1615 1629
  ///where <em>d</em> is the out-degree of the source node.
1616 1630
  ///
1617 1631
  ///It is not possible to find \e all parallel arcs between two nodes.
1618 1632
  ///Use \ref AllArcLookUp for this purpose.
1619 1633
  ///
1620 1634
  ///\warning This class is static, so you should call refresh() (or at
1621 1635
  ///least refresh(Node)) to refresh this data structure whenever the
1622 1636
  ///digraph changes. This is a time consuming (superlinearly proportional
1623 1637
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1624 1638
  ///
1625
  ///\tparam G The type of the underlying digraph.
1639
  ///\tparam GR The type of the underlying digraph.
1626 1640
  ///
1627 1641
  ///\sa DynArcLookUp
1628 1642
  ///\sa AllArcLookUp
1629
  template<class G>
1643
  template<class GR>
1630 1644
  class ArcLookUp
1631 1645
  {
1646
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1647

	
1632 1648
  public:
1633
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1634
    typedef G Digraph;
1649

	
1650
    /// The Digraph type
1651
    typedef GR Digraph;
1635 1652

	
1636 1653
  protected:
1637 1654
    const Digraph &_g;
1638 1655
    typename Digraph::template NodeMap<Arc> _head;
1639 1656
    typename Digraph::template ArcMap<Arc> _left;
1640 1657
    typename Digraph::template ArcMap<Arc> _right;
1641 1658

	
1642 1659
    class ArcLess {
1643 1660
      const Digraph &g;
1644 1661
    public:
1645 1662
      ArcLess(const Digraph &_g) : g(_g) {}
1646 1663
      bool operator()(Arc a,Arc b) const
1647 1664
      {
1648 1665
        return g.target(a)<g.target(b);
1649 1666
      }
1650 1667
    };
1651 1668

	
1652 1669
  public:
1653 1670

	
1654 1671
    ///Constructor
1655 1672

	
1656 1673
    ///Constructor.
1657 1674
    ///
1658 1675
    ///It builds up the search database, which remains valid until the digraph
1659 1676
    ///changes.
1660 1677
    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1661 1678

	
1662 1679
  private:
1663 1680
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1664 1681
    {
1665 1682
      int m=(a+b)/2;
1666 1683
      Arc me=v[m];
1667 1684
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1668 1685
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1669 1686
      return me;
1670 1687
    }
1671 1688
  public:
1672 1689
    ///Refresh the search data structure at a node.
1673 1690

	
1674 1691
    ///Build up the search database of node \c n.
1675 1692
    ///
1676 1693
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
1677 1694
    ///is the number of the outgoing arcs of \c n.
1678 1695
    void refresh(Node n)
1679 1696
    {
1680 1697
      std::vector<Arc> v;
1681 1698
      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1682 1699
      if(v.size()) {
1683 1700
        std::sort(v.begin(),v.end(),ArcLess(_g));
1684 1701
        _head[n]=refreshRec(v,0,v.size()-1);
1685 1702
      }
1686 1703
      else _head[n]=INVALID;
1687 1704
    }
1688 1705
    ///Refresh the full data structure.
1689 1706

	
1690 1707
    ///Build up the full search database. In fact, it simply calls
1691 1708
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1692 1709
    ///
1693 1710
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1694 1711
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
1695 1712
    ///out-degree of the digraph.
1696 1713
    void refresh()
1697 1714
    {
1698 1715
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1699 1716
    }
1700 1717

	
1701 1718
    ///Find an arc between two nodes.
1702 1719

	
1703 1720
    ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
1704 1721
    ///where <em>d</em> is the number of outgoing arcs of \c s.
1705 1722
    ///\param s The source node.
1706 1723
    ///\param t The target node.
1707 1724
    ///\return An arc from \c s to \c t if there exists,
1708 1725
    ///\ref INVALID otherwise.
1709 1726
    ///
1710 1727
    ///\warning If you change the digraph, refresh() must be called before using
1711 1728
    ///this operator. If you change the outgoing arcs of
1712 1729
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1713 1730
    Arc operator()(Node s, Node t) const
1714 1731
    {
1715 1732
      Arc e;
1716 1733
      for(e=_head[s];
1717 1734
          e!=INVALID&&_g.target(e)!=t;
1718 1735
          e = t < _g.target(e)?_left[e]:_right[e]) ;
1719 1736
      return e;
1720 1737
    }
1721 1738

	
1722 1739
  };
1723 1740

	
1724 1741
  ///Fast look-up of all arcs between given endpoints.
1725 1742

	
1726 1743
  ///This class is the same as \ref ArcLookUp, with the addition
1727 1744
  ///that it makes it possible to find all parallel arcs between given
1728 1745
  ///endpoints.
1729 1746
  ///
1730 1747
  ///\warning This class is static, so you should call refresh() (or at
1731 1748
  ///least refresh(Node)) to refresh this data structure whenever the
1732 1749
  ///digraph changes. This is a time consuming (superlinearly proportional
1733 1750
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1734 1751
  ///
1735
  ///\tparam G The type of the underlying digraph.
1752
  ///\tparam GR The type of the underlying digraph.
1736 1753
  ///
1737 1754
  ///\sa DynArcLookUp
1738 1755
  ///\sa ArcLookUp
1739
  template<class G>
1740
  class AllArcLookUp : public ArcLookUp<G>
1756
  template<class GR>
1757
  class AllArcLookUp : public ArcLookUp<GR>
1741 1758
  {
1742
    using ArcLookUp<G>::_g;
1743
    using ArcLookUp<G>::_right;
1744
    using ArcLookUp<G>::_left;
1745
    using ArcLookUp<G>::_head;
1759
    using ArcLookUp<GR>::_g;
1760
    using ArcLookUp<GR>::_right;
1761
    using ArcLookUp<GR>::_left;
1762
    using ArcLookUp<GR>::_head;
1746 1763

	
1747
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1748
    typedef G Digraph;
1764
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1749 1765

	
1750
    typename Digraph::template ArcMap<Arc> _next;
1766
    typename GR::template ArcMap<Arc> _next;
1751 1767

	
1752 1768
    Arc refreshNext(Arc head,Arc next=INVALID)
1753 1769
    {
1754 1770
      if(head==INVALID) return next;
1755 1771
      else {
1756 1772
        next=refreshNext(_right[head],next);
1757 1773
        _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1758 1774
          ? next : INVALID;
1759 1775
        return refreshNext(_left[head],head);
1760 1776
      }
1761 1777
    }
1762 1778

	
1763 1779
    void refreshNext()
1764 1780
    {
1765 1781
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1766 1782
    }
1767 1783

	
1768 1784
  public:
1785

	
1786
    /// The Digraph type
1787
    typedef GR Digraph;
1788

	
1769 1789
    ///Constructor
1770 1790

	
1771 1791
    ///Constructor.
1772 1792
    ///
1773 1793
    ///It builds up the search database, which remains valid until the digraph
1774 1794
    ///changes.
1775
    AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
1795
    AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
1776 1796

	
1777 1797
    ///Refresh the data structure at a node.
1778 1798

	
1779 1799
    ///Build up the search database of node \c n.
1780 1800
    ///
1781 1801
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
1782 1802
    ///the number of the outgoing arcs of \c n.
1783 1803
    void refresh(Node n)
1784 1804
    {
1785
      ArcLookUp<G>::refresh(n);
1805
      ArcLookUp<GR>::refresh(n);
1786 1806
      refreshNext(_head[n]);
1787 1807
    }
1788 1808

	
1789 1809
    ///Refresh the full data structure.
1790 1810

	
1791 1811
    ///Build up the full search database. In fact, it simply calls
1792 1812
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1793 1813
    ///
1794 1814
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1795 1815
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
1796 1816
    ///out-degree of the digraph.
1797 1817
    void refresh()
1798 1818
    {
1799 1819
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
1800 1820
    }
1801 1821

	
1802 1822
    ///Find an arc between two nodes.
1803 1823

	
1804 1824
    ///Find an arc between two nodes.
1805 1825
    ///\param s The source node.
1806 1826
    ///\param t The target node.
1807 1827
    ///\param prev The previous arc between \c s and \c t. It it is INVALID or
1808 1828
    ///not given, the operator finds the first appropriate arc.
1809 1829
    ///\return An arc from \c s to \c t after \c prev or
1810 1830
    ///\ref INVALID if there is no more.
1811 1831
    ///
1812 1832
    ///For example, you can count the number of arcs from \c u to \c v in the
1813 1833
    ///following way.
1814 1834
    ///\code
1815 1835
    ///AllArcLookUp<ListDigraph> ae(g);
1816 1836
    ///...
1817 1837
    ///int n = 0;
1818 1838
    ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
1819 1839
    ///\endcode
1820 1840
    ///
1821 1841
    ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
1822 1842
    ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
1823 1843
    ///consecutive arcs are found in constant time.
1824 1844
    ///
1825 1845
    ///\warning If you change the digraph, refresh() must be called before using
1826 1846
    ///this operator. If you change the outgoing arcs of
1827 1847
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1828 1848
    ///
1829 1849
#ifdef DOXYGEN
1830 1850
    Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
1831 1851
#else
1832
    using ArcLookUp<G>::operator() ;
1852
    using ArcLookUp<GR>::operator() ;
1833 1853
    Arc operator()(Node s, Node t, Arc prev) const
1834 1854
    {
1835 1855
      return prev==INVALID?(*this)(s,t):_next[prev];
1836 1856
    }
1837 1857
#endif
1838 1858

	
1839 1859
  };
1840 1860

	
1841 1861
  /// @}
1842 1862

	
1843 1863
} //namespace lemon
1844 1864

	
1845 1865
#endif
Ignore white space 6 line context
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
 * Copyright (C) 2003-2008
5
 * Copyright (C) 2003-2009
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 <vector>
21 21
#include <cstring>
22 22

	
23 23
#include <lemon/cplex.h>
24 24

	
25 25
extern "C" {
26 26
#include <ilcplex/cplex.h>
27 27
}
28 28

	
29 29

	
30 30
///\file
31 31
///\brief Implementation of the LEMON-CPLEX lp solver interface.
32 32
namespace lemon {
33 33

	
34 34
  CplexEnv::LicenseError::LicenseError(int status) {
35 35
    if (!CPXgeterrorstring(0, status, _message)) {
36 36
      std::strcpy(_message, "Cplex unknown error");
37 37
    }
38 38
  }
39 39

	
40 40
  CplexEnv::CplexEnv() {
41 41
    int status;
42 42
    _cnt = new int;
43 43
    _env = CPXopenCPLEX(&status);
44 44
    if (_env == 0) {
45 45
      delete _cnt;
46 46
      _cnt = 0;
47 47
      throw LicenseError(status);
48 48
    }
49 49
  }
50 50

	
51 51
  CplexEnv::CplexEnv(const CplexEnv& other) {
52 52
    _env = other._env;
53 53
    _cnt = other._cnt;
54 54
    ++(*_cnt);
55 55
  }
56 56

	
57 57
  CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
58 58
    _env = other._env;
59 59
    _cnt = other._cnt;
60 60
    ++(*_cnt);
61 61
    return *this;
62 62
  }
63 63

	
64 64
  CplexEnv::~CplexEnv() {
65 65
    --(*_cnt);
66 66
    if (*_cnt == 0) {
67 67
      delete _cnt;
68 68
      CPXcloseCPLEX(&_env);
69 69
    }
70 70
  }
71 71

	
72 72
  CplexBase::CplexBase() : LpBase() {
73 73
    int status;
74 74
    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
75
    messageLevel(MESSAGE_NOTHING);
75 76
  }
76 77

	
77 78
  CplexBase::CplexBase(const CplexEnv& env)
78 79
    : LpBase(), _env(env) {
79 80
    int status;
80 81
    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
82
    messageLevel(MESSAGE_NOTHING);
81 83
  }
82 84

	
83 85
  CplexBase::CplexBase(const CplexBase& cplex)
84 86
    : LpBase() {
85 87
    int status;
86 88
    _prob = CPXcloneprob(cplexEnv(), cplex._prob, &status);
87 89
    rows = cplex.rows;
88 90
    cols = cplex.cols;
91
    messageLevel(MESSAGE_NOTHING);
89 92
  }
90 93

	
91 94
  CplexBase::~CplexBase() {
92 95
    CPXfreeprob(cplexEnv(),&_prob);
93 96
  }
94 97

	
95 98
  int CplexBase::_addCol() {
96 99
    int i = CPXgetnumcols(cplexEnv(), _prob);
97 100
    double lb = -INF, ub = INF;
98 101
    CPXnewcols(cplexEnv(), _prob, 1, 0, &lb, &ub, 0, 0);
99 102
    return i;
100 103
  }
101 104

	
102 105

	
103 106
  int CplexBase::_addRow() {
104 107
    int i = CPXgetnumrows(cplexEnv(), _prob);
105 108
    const double ub = INF;
106 109
    const char s = 'L';
107 110
    CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
108 111
    return i;
109 112
  }
110 113

	
114
  int CplexBase::_addRow(Value lb, ExprIterator b, 
115
                         ExprIterator e, Value ub) {
116
    int i = CPXgetnumrows(cplexEnv(), _prob);
117
    if (lb == -INF) {
118
      const char s = 'L';
119
      CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
120
    } else if (ub == INF) {
121
      const char s = 'G';
122
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
123
    } else if (lb == ub){
124
      const char s = 'E';
125
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
126
    } else {
127
      const char s = 'R';
128
      double len = ub - lb;
129
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, &len, 0);
130
    }
131

	
132
    std::vector<int> indices;
133
    std::vector<int> rowlist;
134
    std::vector<Value> values;
135

	
136
    for(ExprIterator it=b; it!=e; ++it) {
137
      indices.push_back(it->first);
138
      values.push_back(it->second);
139
      rowlist.push_back(i);
140
    }
141

	
142
    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
143
                   &rowlist.front(), &indices.front(), &values.front());
144

	
145
    return i;
146
  }
111 147

	
112 148
  void CplexBase::_eraseCol(int i) {
113 149
    CPXdelcols(cplexEnv(), _prob, i, i);
114 150
  }
115 151

	
116 152
  void CplexBase::_eraseRow(int i) {
117 153
    CPXdelrows(cplexEnv(), _prob, i, i);
118 154
  }
119 155

	
120 156
  void CplexBase::_eraseColId(int i) {
121 157
    cols.eraseIndex(i);
122 158
    cols.shiftIndices(i);
123 159
  }
124 160
  void CplexBase::_eraseRowId(int i) {
125 161
    rows.eraseIndex(i);
126 162
    rows.shiftIndices(i);
127 163
  }
128 164

	
129 165
  void CplexBase::_getColName(int col, std::string &name) const {
130 166
    int size;
131 167
    CPXgetcolname(cplexEnv(), _prob, 0, 0, 0, &size, col, col);
132 168
    if (size == 0) {
133 169
      name.clear();
134 170
      return;
135 171
    }
136 172

	
137 173
    size *= -1;
138 174
    std::vector<char> buf(size);
139 175
    char *cname;
140 176
    int tmp;
141 177
    CPXgetcolname(cplexEnv(), _prob, &cname, &buf.front(), size,
142 178
                  &tmp, col, col);
143 179
    name = cname;
144 180
  }
145 181

	
146 182
  void CplexBase::_setColName(int col, const std::string &name) {
147 183
    char *cname;
148 184
    cname = const_cast<char*>(name.c_str());
149 185
    CPXchgcolname(cplexEnv(), _prob, 1, &col, &cname);
150 186
  }
151 187

	
152 188
  int CplexBase::_colByName(const std::string& name) const {
153 189
    int index;
154 190
    if (CPXgetcolindex(cplexEnv(), _prob,
155 191
                       const_cast<char*>(name.c_str()), &index) == 0) {
156 192
      return index;
157 193
    }
158 194
    return -1;
159 195
  }
160 196

	
161 197
  void CplexBase::_getRowName(int row, std::string &name) const {
162 198
    int size;
163 199
    CPXgetrowname(cplexEnv(), _prob, 0, 0, 0, &size, row, row);
164 200
    if (size == 0) {
165 201
      name.clear();
166 202
      return;
167 203
    }
168 204

	
169 205
    size *= -1;
170 206
    std::vector<char> buf(size);
171 207
    char *cname;
172 208
    int tmp;
173 209
    CPXgetrowname(cplexEnv(), _prob, &cname, &buf.front(), size,
174 210
                  &tmp, row, row);
175 211
    name = cname;
176 212
  }
177 213

	
178 214
  void CplexBase::_setRowName(int row, const std::string &name) {
179 215
    char *cname;
180 216
    cname = const_cast<char*>(name.c_str());
181 217
    CPXchgrowname(cplexEnv(), _prob, 1, &row, &cname);
182 218
  }
183 219

	
184 220
  int CplexBase::_rowByName(const std::string& name) const {
185 221
    int index;
186 222
    if (CPXgetrowindex(cplexEnv(), _prob,
187 223
                       const_cast<char*>(name.c_str()), &index) == 0) {
188 224
      return index;
189 225
    }
190 226
    return -1;
191 227
  }
192 228

	
193 229
  void CplexBase::_setRowCoeffs(int i, ExprIterator b,
194 230
                                      ExprIterator e)
195 231
  {
196 232
    std::vector<int> indices;
197 233
    std::vector<int> rowlist;
198 234
    std::vector<Value> values;
199 235

	
200 236
    for(ExprIterator it=b; it!=e; ++it) {
201 237
      indices.push_back(it->first);
202 238
      values.push_back(it->second);
203 239
      rowlist.push_back(i);
204 240
    }
205 241

	
206 242
    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
... ...
@@ -345,581 +381,604 @@
345 381
    } else if (lb == ub){
346 382
      const char s = 'E';
347 383
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
348 384
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
349 385
    } else {
350 386
      const char s = 'R';
351 387
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
352 388
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
353 389
      double len = ub - lb;
354 390
      CPXchgrngval(cplexEnv(), _prob, 1, &i, &len);
355 391
    }
356 392
  }
357 393

	
358 394
  void CplexBase::_setRowLowerBound(int i, Value lb)
359 395
  {
360 396
    LEMON_ASSERT(lb != INF, "Invalid bound");
361 397
    _set_row_bounds(i, lb, CplexBase::_getRowUpperBound(i));
362 398
  }
363 399

	
364 400
  void CplexBase::_setRowUpperBound(int i, Value ub)
365 401
  {
366 402

	
367 403
    LEMON_ASSERT(ub != -INF, "Invalid bound");
368 404
    _set_row_bounds(i, CplexBase::_getRowLowerBound(i), ub);
369 405
  }
370 406

	
371 407
  void CplexBase::_setObjCoeffs(ExprIterator b, ExprIterator e)
372 408
  {
373 409
    std::vector<int> indices;
374 410
    std::vector<Value> values;
375 411
    for(ExprIterator it=b; it!=e; ++it) {
376 412
      indices.push_back(it->first);
377 413
      values.push_back(it->second);
378 414
    }
379 415
    CPXchgobj(cplexEnv(), _prob, values.size(),
380 416
              &indices.front(), &values.front());
381 417

	
382 418
  }
383 419

	
384 420
  void CplexBase::_getObjCoeffs(InsertIterator b) const
385 421
  {
386 422
    int num = CPXgetnumcols(cplexEnv(), _prob);
387 423
    std::vector<Value> x(num);
388 424

	
389 425
    CPXgetobj(cplexEnv(), _prob, &x.front(), 0, num - 1);
390 426
    for (int i = 0; i < num; ++i) {
391 427
      if (x[i] != 0.0) {
392 428
        *b = std::make_pair(i, x[i]);
393 429
        ++b;
394 430
      }
395 431
    }
396 432
  }
397 433

	
398 434
  void CplexBase::_setObjCoeff(int i, Value obj_coef)
399 435
  {
400 436
    CPXchgobj(cplexEnv(), _prob, 1, &i, &obj_coef);
401 437
  }
402 438

	
403 439
  CplexBase::Value CplexBase::_getObjCoeff(int i) const
404 440
  {
405 441
    Value x;
406 442
    CPXgetobj(cplexEnv(), _prob, &x, i, i);
407 443
    return x;
408 444
  }
409 445

	
410 446
  void CplexBase::_setSense(CplexBase::Sense sense) {
411 447
    switch (sense) {
412 448
    case MIN:
413 449
      CPXchgobjsen(cplexEnv(), _prob, CPX_MIN);
414 450
      break;
415 451
    case MAX:
416 452
      CPXchgobjsen(cplexEnv(), _prob, CPX_MAX);
417 453
      break;
418 454
    }
419 455
  }
420 456

	
421 457
  CplexBase::Sense CplexBase::_getSense() const {
422 458
    switch (CPXgetobjsen(cplexEnv(), _prob)) {
423 459
    case CPX_MIN:
424 460
      return MIN;
425 461
    case CPX_MAX:
426 462
      return MAX;
427 463
    default:
428 464
      LEMON_ASSERT(false, "Invalid sense");
429 465
      return CplexBase::Sense();
430 466
    }
431 467
  }
432 468

	
433 469
  void CplexBase::_clear() {
434 470
    CPXfreeprob(cplexEnv(),&_prob);
435 471
    int status;
436 472
    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
437 473
    rows.clear();
438 474
    cols.clear();
439 475
  }
440 476

	
477
  void CplexBase::_messageLevel(MessageLevel level) {
478
    switch (level) {
479
    case MESSAGE_NOTHING:
480
      _message_enabled = false;
481
      break;
482
    case MESSAGE_ERROR:
483
    case MESSAGE_WARNING:
484
    case MESSAGE_NORMAL:
485
    case MESSAGE_VERBOSE:
486
      _message_enabled = true;
487
      break;
488
    }
489
  }
490

	
491
  void CplexBase::_applyMessageLevel() {
492
    CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND, 
493
                   _message_enabled ? CPX_ON : CPX_OFF);
494
  }
495

	
441 496
  // CplexLp members
442 497

	
443 498
  CplexLp::CplexLp()
444
    : LpBase(), CplexBase(), LpSolver() {}
499
    : LpBase(), LpSolver(), CplexBase() {}
445 500

	
446 501
  CplexLp::CplexLp(const CplexEnv& env)
447
    : LpBase(), CplexBase(env), LpSolver() {}
502
    : LpBase(), LpSolver(), CplexBase(env) {}
448 503

	
449 504
  CplexLp::CplexLp(const CplexLp& other)
450
    : LpBase(), CplexBase(other), LpSolver() {}
505
    : LpBase(), LpSolver(), CplexBase(other) {}
451 506

	
452 507
  CplexLp::~CplexLp() {}
453 508

	
454
  CplexLp* CplexLp::_newSolver() const { return new CplexLp; }
455
  CplexLp* CplexLp::_cloneSolver() const {return new CplexLp(*this); }
509
  CplexLp* CplexLp::newSolver() const { return new CplexLp; }
510
  CplexLp* CplexLp::cloneSolver() const {return new CplexLp(*this); }
456 511

	
457 512
  const char* CplexLp::_solverName() const { return "CplexLp"; }
458 513

	
459 514
  void CplexLp::_clear_temporals() {
460 515
    _col_status.clear();
461 516
    _row_status.clear();
462 517
    _primal_ray.clear();
463 518
    _dual_ray.clear();
464 519
  }
465 520

	
466 521
  // The routine returns zero unless an error occurred during the
467 522
  // optimization. Examples of errors include exhausting available
468 523
  // memory (CPXERR_NO_MEMORY) or encountering invalid data in the
469 524
  // CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a
470 525
  // user-specified CPLEX limit, or proving the model infeasible or
471 526
  // unbounded, are not considered errors. Note that a zero return
472 527
  // value does not necessarily mean that a solution exists. Use query
473 528
  // routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain
474 529
  // further information about the status of the optimization.
475 530
  CplexLp::SolveExitStatus CplexLp::convertStatus(int status) {
476 531
#if CPX_VERSION >= 800
477 532
    if (status == 0) {
478 533
      switch (CPXgetstat(cplexEnv(), _prob)) {
479 534
      case CPX_STAT_OPTIMAL:
480 535
      case CPX_STAT_INFEASIBLE:
481 536
      case CPX_STAT_UNBOUNDED:
482 537
        return SOLVED;
483 538
      default:
484 539
        return UNSOLVED;
485 540
      }
486 541
    } else {
487 542
      return UNSOLVED;
488 543
    }
489 544
#else
490 545
    if (status == 0) {
491 546
      //We want to exclude some cases
492 547
      switch (CPXgetstat(cplexEnv(), _prob)) {
493 548
      case CPX_OBJ_LIM:
494 549
      case CPX_IT_LIM_FEAS:
495 550
      case CPX_IT_LIM_INFEAS:
496 551
      case CPX_TIME_LIM_FEAS:
497 552
      case CPX_TIME_LIM_INFEAS:
498 553
        return UNSOLVED;
499 554
      default:
500 555
        return SOLVED;
501 556
      }
502 557
    } else {
503 558
      return UNSOLVED;
504 559
    }
505 560
#endif
506 561
  }
507 562

	
508 563
  CplexLp::SolveExitStatus CplexLp::_solve() {
509 564
    _clear_temporals();
565
    _applyMessageLevel();
510 566
    return convertStatus(CPXlpopt(cplexEnv(), _prob));
511 567
  }
512 568

	
513 569
  CplexLp::SolveExitStatus CplexLp::solvePrimal() {
514 570
    _clear_temporals();
571
    _applyMessageLevel();
515 572
    return convertStatus(CPXprimopt(cplexEnv(), _prob));
516 573
  }
517 574

	
518 575
  CplexLp::SolveExitStatus CplexLp::solveDual() {
519 576
    _clear_temporals();
577
    _applyMessageLevel();
520 578
    return convertStatus(CPXdualopt(cplexEnv(), _prob));
521 579
  }
522 580

	
523 581
  CplexLp::SolveExitStatus CplexLp::solveBarrier() {
524 582
    _clear_temporals();
583
    _applyMessageLevel();
525 584
    return convertStatus(CPXbaropt(cplexEnv(), _prob));
526 585
  }
527 586

	
528 587
  CplexLp::Value CplexLp::_getPrimal(int i) const {
529 588
    Value x;
530 589
    CPXgetx(cplexEnv(), _prob, &x, i, i);
531 590
    return x;
532 591
  }
533 592

	
534 593
  CplexLp::Value CplexLp::_getDual(int i) const {
535 594
    Value y;
536 595
    CPXgetpi(cplexEnv(), _prob, &y, i, i);
537 596
    return y;
538 597
  }
539 598

	
540 599
  CplexLp::Value CplexLp::_getPrimalValue() const {
541 600
    Value objval;
542 601
    CPXgetobjval(cplexEnv(), _prob, &objval);
543 602
    return objval;
544 603
  }
545 604

	
546 605
  CplexLp::VarStatus CplexLp::_getColStatus(int i) const {
547 606
    if (_col_status.empty()) {
548 607
      _col_status.resize(CPXgetnumcols(cplexEnv(), _prob));
549 608
      CPXgetbase(cplexEnv(), _prob, &_col_status.front(), 0);
550 609
    }
551 610
    switch (_col_status[i]) {
552 611
    case CPX_BASIC:
553 612
      return BASIC;
554 613
    case CPX_FREE_SUPER:
555 614
      return FREE;
556 615
    case CPX_AT_LOWER:
557 616
      return LOWER;
558 617
    case CPX_AT_UPPER:
559 618
      return UPPER;
560 619
    default:
561 620
      LEMON_ASSERT(false, "Wrong column status");
562 621
      return CplexLp::VarStatus();
563 622
    }
564 623
  }
565 624

	
566 625
  CplexLp::VarStatus CplexLp::_getRowStatus(int i) const {
567 626
    if (_row_status.empty()) {
568 627
      _row_status.resize(CPXgetnumrows(cplexEnv(), _prob));
569 628
      CPXgetbase(cplexEnv(), _prob, 0, &_row_status.front());
570 629
    }
571 630
    switch (_row_status[i]) {
572 631
    case CPX_BASIC:
573 632
      return BASIC;
574 633
    case CPX_AT_LOWER:
575 634
      {
576 635
        char s;
577 636
        CPXgetsense(cplexEnv(), _prob, &s, i, i);
578 637
        return s != 'L' ? LOWER : UPPER;
579 638
      }
580 639
    case CPX_AT_UPPER:
581 640
      return UPPER;
582 641
    default:
583 642
      LEMON_ASSERT(false, "Wrong row status");
584 643
      return CplexLp::VarStatus();
585 644
    }
586 645
  }
587 646

	
588 647
  CplexLp::Value CplexLp::_getPrimalRay(int i) const {
589 648
    if (_primal_ray.empty()) {
590 649
      _primal_ray.resize(CPXgetnumcols(cplexEnv(), _prob));
591 650
      CPXgetray(cplexEnv(), _prob, &_primal_ray.front());
592 651
    }
593 652
    return _primal_ray[i];
594 653
  }
595 654

	
596 655
  CplexLp::Value CplexLp::_getDualRay(int i) const {
597 656
    if (_dual_ray.empty()) {
598 657

	
599 658
    }
600 659
    return _dual_ray[i];
601 660
  }
602 661

	
603
  //7.5-os cplex statusai (Vigyazat: a 9.0-asei masok!)
662
  // Cplex 7.0 status values
604 663
  // This table lists the statuses, returned by the CPXgetstat()
605 664
  // routine, for solutions to LP problems or mixed integer problems. If
606 665
  // no solution exists, the return value is zero.
607 666

	
608 667
  // For Simplex, Barrier
609 668
  // 1          CPX_OPTIMAL
610 669
  //          Optimal solution found
611 670
  // 2          CPX_INFEASIBLE
612 671
  //          Problem infeasible
613 672
  // 3    CPX_UNBOUNDED
614 673
  //          Problem unbounded
615 674
  // 4          CPX_OBJ_LIM
616 675
  //          Objective limit exceeded in Phase II
617 676
  // 5          CPX_IT_LIM_FEAS
618 677
  //          Iteration limit exceeded in Phase II
619 678
  // 6          CPX_IT_LIM_INFEAS
620 679
  //          Iteration limit exceeded in Phase I
621 680
  // 7          CPX_TIME_LIM_FEAS
622 681
  //          Time limit exceeded in Phase II
623 682
  // 8          CPX_TIME_LIM_INFEAS
624 683
  //          Time limit exceeded in Phase I
625 684
  // 9          CPX_NUM_BEST_FEAS
626 685
  //          Problem non-optimal, singularities in Phase II
627 686
  // 10         CPX_NUM_BEST_INFEAS
628 687
  //          Problem non-optimal, singularities in Phase I
629 688
  // 11         CPX_OPTIMAL_INFEAS
630 689
  //          Optimal solution found, unscaled infeasibilities
631 690
  // 12         CPX_ABORT_FEAS
632 691
  //          Aborted in Phase II
633 692
  // 13         CPX_ABORT_INFEAS
634 693
  //          Aborted in Phase I
635 694
  // 14          CPX_ABORT_DUAL_INFEAS
636 695
  //          Aborted in barrier, dual infeasible
637 696
  // 15          CPX_ABORT_PRIM_INFEAS
638 697
  //          Aborted in barrier, primal infeasible
639 698
  // 16          CPX_ABORT_PRIM_DUAL_INFEAS
640 699
  //          Aborted in barrier, primal and dual infeasible
641 700
  // 17          CPX_ABORT_PRIM_DUAL_FEAS
642 701
  //          Aborted in barrier, primal and dual feasible
643 702
  // 18          CPX_ABORT_CROSSOVER
644 703
  //          Aborted in crossover
645 704
  // 19          CPX_INForUNBD
646 705
  //          Infeasible or unbounded
647 706
  // 20   CPX_PIVOT
648 707
  //       User pivot used
649 708
  //
650
  //     Ezeket hova tegyem:
709
  // Pending return values
651 710
  // ??case CPX_ABORT_DUAL_INFEAS
652 711
  // ??case CPX_ABORT_CROSSOVER
653 712
  // ??case CPX_INForUNBD
654 713
  // ??case CPX_PIVOT
655 714

	
656 715
  //Some more interesting stuff:
657 716

	
658 717
  // CPX_PARAM_PROBMETHOD  1062  int  LPMETHOD
659 718
  // 0 Automatic
660 719
  // 1 Primal Simplex
661 720
  // 2 Dual Simplex
662 721
  // 3 Network Simplex
663 722
  // 4 Standard Barrier
664 723
  // Default: 0
665 724
  // Description: Method for linear optimization.
666 725
  // Determines which algorithm is used when CPXlpopt() (or "optimize"
667 726
  // in the Interactive Optimizer) is called. Currently the behavior of
668 727
  // the "Automatic" setting is that CPLEX simply invokes the dual
669 728
  // simplex method, but this capability may be expanded in the future
670 729
  // so that CPLEX chooses the method based on problem characteristics
671 730
#if CPX_VERSION < 900
672 731
  void statusSwitch(CPXENVptr cplexEnv(),int& stat){
673 732
    int lpmethod;
674 733
    CPXgetintparam (cplexEnv(),CPX_PARAM_PROBMETHOD,&lpmethod);
675 734
    if (lpmethod==2){
676 735
      if (stat==CPX_UNBOUNDED){
677 736
        stat=CPX_INFEASIBLE;
678 737
      }
679 738
      else{
680 739
        if (stat==CPX_INFEASIBLE)
681 740
          stat=CPX_UNBOUNDED;
682 741
      }
683 742
    }
684 743
  }
685 744
#else
686 745
  void statusSwitch(CPXENVptr,int&){}
687 746
#endif
688 747

	
689 748
  CplexLp::ProblemType CplexLp::_getPrimalType() const {
690 749
    // Unboundedness not treated well: the following is from cplex 9.0 doc
691 750
    // About Unboundedness
692 751

	
693 752
    // The treatment of models that are unbounded involves a few
694 753
    // subtleties. Specifically, a declaration of unboundedness means that
695 754
    // ILOG CPLEX has determined that the model has an unbounded
696 755
    // ray. Given any feasible solution x with objective z, a multiple of
697 756
    // the unbounded ray can be added to x to give a feasible solution
698 757
    // with objective z-1 (or z+1 for maximization models). Thus, if a
699 758
    // feasible solution exists, then the optimal objective is
700 759
    // unbounded. Note that ILOG CPLEX has not necessarily concluded that
701 760
    // a feasible solution exists. Users can call the routine CPXsolninfo
702 761
    // to determine whether ILOG CPLEX has also concluded that the model
703 762
    // has a feasible solution.
704 763

	
705 764
    int stat = CPXgetstat(cplexEnv(), _prob);
706 765
#if CPX_VERSION >= 800
707 766
    switch (stat)
708 767
      {
709 768
      case CPX_STAT_OPTIMAL:
710 769
        return OPTIMAL;
711 770
      case CPX_STAT_UNBOUNDED:
712 771
        return UNBOUNDED;
713 772
      case CPX_STAT_INFEASIBLE:
714 773
        return INFEASIBLE;
715 774
      default:
716 775
        return UNDEFINED;
717 776
      }
718 777
#else
719 778
    statusSwitch(cplexEnv(),stat);
720 779
    //CPXgetstat(cplexEnv(), _prob);
721
    //printf("A primal status: %d, CPX_OPTIMAL=%d \n",stat,CPX_OPTIMAL);
722 780
    switch (stat) {
723 781
    case 0:
724 782
      return UNDEFINED; //Undefined
725 783
    case CPX_OPTIMAL://Optimal
726 784
      return OPTIMAL;
727 785
    case CPX_UNBOUNDED://Unbounded
728 786
      return INFEASIBLE;//In case of dual simplex
729 787
      //return UNBOUNDED;
730 788
    case CPX_INFEASIBLE://Infeasible
731 789
      //    case CPX_IT_LIM_INFEAS:
732 790
      //     case CPX_TIME_LIM_INFEAS:
733 791
      //     case CPX_NUM_BEST_INFEAS:
734 792
      //     case CPX_OPTIMAL_INFEAS:
735 793
      //     case CPX_ABORT_INFEAS:
736 794
      //     case CPX_ABORT_PRIM_INFEAS:
737 795
      //     case CPX_ABORT_PRIM_DUAL_INFEAS:
738 796
      return UNBOUNDED;//In case of dual simplex
739 797
      //return INFEASIBLE;
740 798
      //     case CPX_OBJ_LIM:
741 799
      //     case CPX_IT_LIM_FEAS:
742 800
      //     case CPX_TIME_LIM_FEAS:
743 801
      //     case CPX_NUM_BEST_FEAS:
744 802
      //     case CPX_ABORT_FEAS:
745 803
      //     case CPX_ABORT_PRIM_DUAL_FEAS:
746 804
      //       return FEASIBLE;
747 805
    default:
748 806
      return UNDEFINED; //Everything else comes here
749 807
      //FIXME error
750 808
    }
751 809
#endif
752 810
  }
753 811

	
754
  //9.0-as cplex verzio statusai
812
  // Cplex 9.0 status values
755 813
  // CPX_STAT_ABORT_DUAL_OBJ_LIM
756 814
  // CPX_STAT_ABORT_IT_LIM
757 815
  // CPX_STAT_ABORT_OBJ_LIM
758 816
  // CPX_STAT_ABORT_PRIM_OBJ_LIM
759 817
  // CPX_STAT_ABORT_TIME_LIM
760 818
  // CPX_STAT_ABORT_USER
761 819
  // CPX_STAT_FEASIBLE_RELAXED
762 820
  // CPX_STAT_INFEASIBLE
763 821
  // CPX_STAT_INForUNBD
764 822
  // CPX_STAT_NUM_BEST
765 823
  // CPX_STAT_OPTIMAL
766 824
  // CPX_STAT_OPTIMAL_FACE_UNBOUNDED
767 825
  // CPX_STAT_OPTIMAL_INFEAS
768 826
  // CPX_STAT_OPTIMAL_RELAXED
769 827
  // CPX_STAT_UNBOUNDED
770 828

	
771 829
  CplexLp::ProblemType CplexLp::_getDualType() const {
772 830
    int stat = CPXgetstat(cplexEnv(), _prob);
773 831
#if CPX_VERSION >= 800
774 832
    switch (stat) {
775 833
    case CPX_STAT_OPTIMAL:
776 834
      return OPTIMAL;
777 835
    case CPX_STAT_UNBOUNDED:
778 836
      return INFEASIBLE;
779 837
    default:
780 838
      return UNDEFINED;
781 839
    }
782 840
#else
783 841
    statusSwitch(cplexEnv(),stat);
784 842
    switch (stat) {
785 843
    case 0:
786 844
      return UNDEFINED; //Undefined
787 845
    case CPX_OPTIMAL://Optimal
788 846
      return OPTIMAL;
789 847
    case CPX_UNBOUNDED:
790 848
      return INFEASIBLE;
791 849
    default:
792 850
      return UNDEFINED; //Everything else comes here
793 851
      //FIXME error
794 852
    }
795 853
#endif
796 854
  }
797 855

	
798 856
  // CplexMip members
799 857

	
800 858
  CplexMip::CplexMip()
801
    : LpBase(), CplexBase(), MipSolver() {
859
    : LpBase(), MipSolver(), CplexBase() {
802 860

	
803 861
#if CPX_VERSION < 800
804 862
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MIP);
805 863
#else
806 864
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MILP);
807 865
#endif
808 866
  }
809 867

	
810 868
  CplexMip::CplexMip(const CplexEnv& env)
811
    : LpBase(), CplexBase(env), MipSolver() {
869
    : LpBase(), MipSolver(), CplexBase(env) {
812 870

	
813 871
#if CPX_VERSION < 800
814 872
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MIP);
815 873
#else
816 874
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MILP);
817 875
#endif
818 876

	
819 877
  }
820 878

	
821 879
  CplexMip::CplexMip(const CplexMip& other)
822
    : LpBase(), CplexBase(other), MipSolver() {}
880
    : LpBase(), MipSolver(), CplexBase(other) {}
823 881

	
824 882
  CplexMip::~CplexMip() {}
825 883

	
826
  CplexMip* CplexMip::_newSolver() const { return new CplexMip; }
827
  CplexMip* CplexMip::_cloneSolver() const {return new CplexMip(*this); }
884
  CplexMip* CplexMip::newSolver() const { return new CplexMip; }
885
  CplexMip* CplexMip::cloneSolver() const {return new CplexMip(*this); }
828 886

	
829 887
  const char* CplexMip::_solverName() const { return "CplexMip"; }
830 888

	
831 889
  void CplexMip::_setColType(int i, CplexMip::ColTypes col_type) {
832 890

	
833 891
    // Note If a variable is to be changed to binary, a call to CPXchgbds
834 892
    // should also be made to change the bounds to 0 and 1.
835 893

	
836 894
    switch (col_type){
837 895
    case INTEGER: {
838 896
      const char t = 'I';
839 897
      CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
840 898
    } break;
841 899
    case REAL: {
842 900
      const char t = 'C';
843 901
      CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
844 902
    } break;
845 903
    default:
846 904
      break;
847 905
    }
848 906
  }
849 907

	
850 908
  CplexMip::ColTypes CplexMip::_getColType(int i) const {
851 909
    char t;
852 910
    CPXgetctype (cplexEnv(), _prob, &t, i, i);
853 911
    switch (t) {
854 912
    case 'I':
855 913
      return INTEGER;
856 914
    case 'C':
857 915
      return REAL;
858 916
    default:
859 917
      LEMON_ASSERT(false, "Invalid column type");
860 918
      return ColTypes();
861 919
    }
862 920

	
863 921
  }
864 922

	
865 923
  CplexMip::SolveExitStatus CplexMip::_solve() {
866 924
    int status;
925
    _applyMessageLevel();
867 926
    status = CPXmipopt (cplexEnv(), _prob);
868 927
    if (status==0)
869 928
      return SOLVED;
870 929
    else
871 930
      return UNSOLVED;
872 931

	
873 932
  }
874 933

	
875 934

	
876 935
  CplexMip::ProblemType CplexMip::_getType() const {
877 936

	
878 937
    int stat = CPXgetstat(cplexEnv(), _prob);
879 938

	
880 939
    //Fortunately, MIP statuses did not change for cplex 8.0
881 940
    switch (stat) {
882 941
    case CPXMIP_OPTIMAL:
883 942
      // Optimal integer solution has been found.
884 943
    case CPXMIP_OPTIMAL_TOL:
885 944
      // Optimal soluton with the tolerance defined by epgap or epagap has
886 945
      // been found.
887 946
      return OPTIMAL;
888 947
      //This also exists in later issues
889 948
      //    case CPXMIP_UNBOUNDED:
890 949
      //return UNBOUNDED;
891 950
      case CPXMIP_INFEASIBLE:
892 951
        return INFEASIBLE;
893 952
    default:
894 953
      return UNDEFINED;
895 954
    }
896 955
    //Unboundedness not treated well: the following is from cplex 9.0 doc
897 956
    // About Unboundedness
898 957

	
899 958
    // The treatment of models that are unbounded involves a few
900 959
    // subtleties. Specifically, a declaration of unboundedness means that
901 960
    // ILOG CPLEX has determined that the model has an unbounded
902 961
    // ray. Given any feasible solution x with objective z, a multiple of
903 962
    // the unbounded ray can be added to x to give a feasible solution
904 963
    // with objective z-1 (or z+1 for maximization models). Thus, if a
905 964
    // feasible solution exists, then the optimal objective is
906 965
    // unbounded. Note that ILOG CPLEX has not necessarily concluded that
907 966
    // a feasible solution exists. Users can call the routine CPXsolninfo
908 967
    // to determine whether ILOG CPLEX has also concluded that the model
909 968
    // has a feasible solution.
910 969
  }
911 970

	
912 971
  CplexMip::Value CplexMip::_getSol(int i) const {
913 972
    Value x;
914 973
    CPXgetmipx(cplexEnv(), _prob, &x, i, i);
915 974
    return x;
916 975
  }
917 976

	
918 977
  CplexMip::Value CplexMip::_getSolValue() const {
919 978
    Value objval;
920 979
    CPXgetmipobjval(cplexEnv(), _prob, &objval);
921 980
    return objval;
922 981
  }
923 982

	
924 983
} //namespace lemon
925 984

	
Ignore white space 6 line context
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
 * Copyright (C) 2003-2008
5
 * Copyright (C) 2003-2009
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_CPLEX_H
20 20
#define LEMON_CPLEX_H
21 21

	
22 22
///\file
23 23
///\brief Header of the LEMON-CPLEX lp solver interface.
24 24

	
25 25
#include <lemon/lp_base.h>
26 26

	
27 27
struct cpxenv;
28 28
struct cpxlp;
29 29

	
30 30
namespace lemon {
31 31

	
32 32
  /// \brief Reference counted wrapper around cpxenv pointer
33 33
  ///
34 34
  /// The cplex uses environment object which is responsible for
35 35
  /// checking the proper license usage. This class provides a simple
36 36
  /// interface for share the environment object between different
37 37
  /// problems.
38 38
  class CplexEnv {
39 39
    friend class CplexBase;
40 40
  private:
41 41
    cpxenv* _env;
42 42
    mutable int* _cnt;
43 43

	
44 44
  public:
45 45

	
46 46
    /// \brief This exception is thrown when the license check is not
47 47
    /// sufficient
48 48
    class LicenseError : public Exception {
49 49
      friend class CplexEnv;
50 50
    private:
51 51

	
52 52
      LicenseError(int status);
53 53
      char _message[510];
54 54

	
55 55
    public:
56 56

	
57 57
      /// The short error message
58 58
      virtual const char* what() const throw() {
59 59
        return _message;
60 60
      }
61 61
    };
62 62

	
63 63
    /// Constructor
64 64
    CplexEnv();
65 65
    /// Shallow copy constructor
66 66
    CplexEnv(const CplexEnv&);
67 67
    /// Shallow assignement
68 68
    CplexEnv& operator=(const CplexEnv&);
69 69
    /// Destructor
70 70
    virtual ~CplexEnv();
71 71

	
72 72
  protected:
73 73

	
74 74
    cpxenv* cplexEnv() { return _env; }
75 75
    const cpxenv* cplexEnv() const { return _env; }
76 76
  };
77 77

	
78 78
  /// \brief Base interface for the CPLEX LP and MIP solver
79 79
  ///
80 80
  /// This class implements the common interface of the CPLEX LP and
81
  /// MIP solvers.  
81
  /// MIP solvers.
82 82
  /// \ingroup lp_group
83 83
  class CplexBase : virtual public LpBase {
84 84
  protected:
85 85

	
86 86
    CplexEnv _env;
87 87
    cpxlp* _prob;
88 88

	
89 89
    CplexBase();
90 90
    CplexBase(const CplexEnv&);
91 91
    CplexBase(const CplexBase &);
92 92
    virtual ~CplexBase();
93 93

	
94 94
    virtual int _addCol();
95 95
    virtual int _addRow();
96
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
96 97

	
97 98
    virtual void _eraseCol(int i);
98 99
    virtual void _eraseRow(int i);
99 100

	
100 101
    virtual void _eraseColId(int i);
101 102
    virtual void _eraseRowId(int i);
102 103

	
103 104
    virtual void _getColName(int col, std::string& name) const;
104 105
    virtual void _setColName(int col, const std::string& name);
105 106
    virtual int _colByName(const std::string& name) const;
106 107

	
107 108
    virtual void _getRowName(int row, std::string& name) const;
108 109
    virtual void _setRowName(int row, const std::string& name);
109 110
    virtual int _rowByName(const std::string& name) const;
110 111

	
111 112
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
112 113
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
113 114

	
114 115
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
115 116
    virtual void _getColCoeffs(int i, InsertIterator b) const;
116 117

	
117 118
    virtual void _setCoeff(int row, int col, Value value);
118 119
    virtual Value _getCoeff(int row, int col) const;
119 120

	
120 121
    virtual void _setColLowerBound(int i, Value value);
121 122
    virtual Value _getColLowerBound(int i) const;
122 123

	
123 124
    virtual void _setColUpperBound(int i, Value value);
124 125
    virtual Value _getColUpperBound(int i) const;
125 126

	
126 127
  private:
127 128
    void _set_row_bounds(int i, Value lb, Value ub);
128 129
  protected:
129 130

	
130 131
    virtual void _setRowLowerBound(int i, Value value);
131 132
    virtual Value _getRowLowerBound(int i) const;
132 133

	
133 134
    virtual void _setRowUpperBound(int i, Value value);
134 135
    virtual Value _getRowUpperBound(int i) const;
135 136

	
136 137
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
137 138
    virtual void _getObjCoeffs(InsertIterator b) const;
138 139

	
139 140
    virtual void _setObjCoeff(int i, Value obj_coef);
140 141
    virtual Value _getObjCoeff(int i) const;
141 142

	
142 143
    virtual void _setSense(Sense sense);
143 144
    virtual Sense _getSense() const;
144 145

	
145 146
    virtual void _clear();
146 147

	
148
    virtual void _messageLevel(MessageLevel level);
149
    void _applyMessageLevel();
150

	
151
    bool _message_enabled;
152

	
147 153
  public:
148 154

	
149 155
    /// Returns the used \c CplexEnv instance
150 156
    const CplexEnv& env() const { return _env; }
157

	
158
    /// \brief Returns the const cpxenv pointer
151 159
    ///
160
    /// \note The cpxenv might be destructed with the solver.
152 161
    const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
153 162

	
163
    /// \brief Returns the const cpxenv pointer
164
    ///
165
    /// \note The cpxenv might be destructed with the solver.
166
    cpxenv* cplexEnv() { return _env.cplexEnv(); }
167

	
168
    /// Returns the cplex problem object
154 169
    cpxlp* cplexLp() { return _prob; }
170
    /// Returns the cplex problem object
155 171
    const cpxlp* cplexLp() const { return _prob; }
156 172

	
157 173
  };
158 174

	
159 175
  /// \brief Interface for the CPLEX LP solver
160 176
  ///
161 177
  /// This class implements an interface for the CPLEX LP solver.
162 178
  ///\ingroup lp_group
163
  class CplexLp : public CplexBase, public LpSolver {
179
  class CplexLp : public LpSolver, public CplexBase {
164 180
  public:
165 181
    /// \e
166 182
    CplexLp();
167 183
    /// \e
168 184
    CplexLp(const CplexEnv&);
169 185
    /// \e
170 186
    CplexLp(const CplexLp&);
171 187
    /// \e
172 188
    virtual ~CplexLp();
173 189

	
190
    /// \e
191
    virtual CplexLp* cloneSolver() const;
192
    /// \e
193
    virtual CplexLp* newSolver() const;
194

	
174 195
  private:
175 196

	
176 197
    // these values cannot retrieved element by element
177 198
    mutable std::vector<int> _col_status;
178 199
    mutable std::vector<int> _row_status;
179 200

	
180 201
    mutable std::vector<Value> _primal_ray;
181 202
    mutable std::vector<Value> _dual_ray;
182 203

	
183 204
    void _clear_temporals();
184 205

	
185 206
    SolveExitStatus convertStatus(int status);
186 207

	
187 208
  protected:
188 209

	
189
    virtual CplexLp* _cloneSolver() const;
190
    virtual CplexLp* _newSolver() const;
191

	
192 210
    virtual const char* _solverName() const;
193 211

	
194 212
    virtual SolveExitStatus _solve();
195 213
    virtual Value _getPrimal(int i) const;
196 214
    virtual Value _getDual(int i) const;
197 215
    virtual Value _getPrimalValue() const;
198 216

	
199 217
    virtual VarStatus _getColStatus(int i) const;
200 218
    virtual VarStatus _getRowStatus(int i) const;
201 219

	
202 220
    virtual Value _getPrimalRay(int i) const;
203 221
    virtual Value _getDualRay(int i) const;
204 222

	
205 223
    virtual ProblemType _getPrimalType() const;
206 224
    virtual ProblemType _getDualType() const;
207 225

	
208 226
  public:
209 227

	
210 228
    /// Solve with primal simplex method
211 229
    SolveExitStatus solvePrimal();
212 230

	
213 231
    /// Solve with dual simplex method
214 232
    SolveExitStatus solveDual();
215 233

	
216 234
    /// Solve with barrier method
217 235
    SolveExitStatus solveBarrier();
218 236

	
219 237
  };
220 238

	
221 239
  /// \brief Interface for the CPLEX MIP solver
222 240
  ///
223 241
  /// This class implements an interface for the CPLEX MIP solver.
224 242
  ///\ingroup lp_group
225
  class CplexMip : public CplexBase, public MipSolver {
243
  class CplexMip : public MipSolver, public CplexBase {
226 244
  public:
227 245
    /// \e
228 246
    CplexMip();
229 247
    /// \e
230 248
    CplexMip(const CplexEnv&);
231 249
    /// \e
232 250
    CplexMip(const CplexMip&);
233 251
    /// \e
234 252
    virtual ~CplexMip();
235 253

	
254
    /// \e
255
    virtual CplexMip* cloneSolver() const;
256
    /// \e
257
    virtual CplexMip* newSolver() const;
258

	
236 259
  protected:
237 260

	
238
    virtual CplexMip* _cloneSolver() const;
239
    virtual CplexMip* _newSolver() const;
240 261

	
241 262
    virtual const char* _solverName() const;
242 263

	
243 264
    virtual ColTypes _getColType(int col) const;
244 265
    virtual void _setColType(int col, ColTypes col_type);
245 266

	
246 267
    virtual SolveExitStatus _solve();
247 268
    virtual ProblemType _getType() const;
248 269
    virtual Value _getSol(int i) const;
249 270
    virtual Value _getSolValue() const;
250 271

	
251 272
  };
252 273

	
253 274
} //END OF NAMESPACE LEMON
254 275

	
255 276
#endif //LEMON_CPLEX_H
256 277

	
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_DFS_H
20 20
#define LEMON_DFS_H
21 21

	
22 22
///\ingroup search
23 23
///\file
24 24
///\brief DFS algorithm.
25 25

	
26 26
#include <lemon/list_graph.h>
27 27
#include <lemon/bits/path_dump.h>
28 28
#include <lemon/core.h>
29 29
#include <lemon/error.h>
30 30
#include <lemon/maps.h>
31 31
#include <lemon/path.h>
32 32

	
33 33
namespace lemon {
34 34

	
35 35
  ///Default traits class of Dfs class.
36 36

	
37 37
  ///Default traits class of Dfs class.
38 38
  ///\tparam GR Digraph type.
39 39
  template<class GR>
40 40
  struct DfsDefaultTraits
41 41
  {
42 42
    ///The type of the digraph the algorithm runs on.
43 43
    typedef GR Digraph;
44 44

	
45 45
    ///\brief The type of the map that stores the predecessor
46 46
    ///arcs of the %DFS paths.
47 47
    ///
48 48
    ///The type of the map that stores the predecessor
49 49
    ///arcs of the %DFS paths.
50
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
50
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
51 51
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
52
    ///Instantiates a PredMap.
52
    ///Instantiates a \c PredMap.
53 53

	
54
    ///This function instantiates a PredMap.
54
    ///This function instantiates a \ref PredMap.
55 55
    ///\param g is the digraph, to which we would like to define the
56
    ///PredMap.
56
    ///\ref PredMap.
57 57
    static PredMap *createPredMap(const Digraph &g)
58 58
    {
59 59
      return new PredMap(g);
60 60
    }
61 61

	
62 62
    ///The type of the map that indicates which nodes are processed.
63 63

	
64 64
    ///The type of the map that indicates which nodes are processed.
65
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
65
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
66
    ///By default it is a NullMap.
66 67
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
67
    ///Instantiates a ProcessedMap.
68
    ///Instantiates a \c ProcessedMap.
68 69

	
69
    ///This function instantiates a ProcessedMap.
70
    ///This function instantiates a \ref ProcessedMap.
70 71
    ///\param g is the digraph, to which
71
    ///we would like to define the ProcessedMap
72
    ///we would like to define the \ref ProcessedMap.
72 73
#ifdef DOXYGEN
73 74
    static ProcessedMap *createProcessedMap(const Digraph &g)
74 75
#else
75 76
    static ProcessedMap *createProcessedMap(const Digraph &)
76 77
#endif
77 78
    {
78 79
      return new ProcessedMap();
79 80
    }
80 81

	
81 82
    ///The type of the map that indicates which nodes are reached.
82 83

	
83 84
    ///The type of the map that indicates which nodes are reached.
84
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
85
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
85 86
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
86
    ///Instantiates a ReachedMap.
87
    ///Instantiates a \c ReachedMap.
87 88

	
88
    ///This function instantiates a ReachedMap.
89
    ///This function instantiates a \ref ReachedMap.
89 90
    ///\param g is the digraph, to which
90
    ///we would like to define the ReachedMap.
91
    ///we would like to define the \ref ReachedMap.
91 92
    static ReachedMap *createReachedMap(const Digraph &g)
92 93
    {
93 94
      return new ReachedMap(g);
94 95
    }
95 96

	
96 97
    ///The type of the map that stores the distances of the nodes.
97 98

	
98 99
    ///The type of the map that stores the distances of the nodes.
99
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
100
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
100 101
    typedef typename Digraph::template NodeMap<int> DistMap;
101
    ///Instantiates a DistMap.
102
    ///Instantiates a \c DistMap.
102 103

	
103
    ///This function instantiates a DistMap.
104
    ///This function instantiates a \ref DistMap.
104 105
    ///\param g is the digraph, to which we would like to define the
105
    ///DistMap.
106
    ///\ref DistMap.
106 107
    static DistMap *createDistMap(const Digraph &g)
107 108
    {
108 109
      return new DistMap(g);
109 110
    }
110 111
  };
111 112

	
112 113
  ///%DFS algorithm class.
113 114

	
114 115
  ///\ingroup search
115 116
  ///This class provides an efficient implementation of the %DFS algorithm.
116 117
  ///
117 118
  ///There is also a \ref dfs() "function-type interface" for the DFS
118 119
  ///algorithm, which is convenient in the simplier cases and it can be
119 120
  ///used easier.
120 121
  ///
121 122
  ///\tparam GR The type of the digraph the algorithm runs on.
122 123
  ///The default type is \ref ListDigraph.
123 124
#ifdef DOXYGEN
124 125
  template <typename GR,
125 126
            typename TR>
126 127
#else
127 128
  template <typename GR=ListDigraph,
128 129
            typename TR=DfsDefaultTraits<GR> >
129 130
#endif
130 131
  class Dfs {
131 132
  public:
132 133

	
133 134
    ///The type of the digraph the algorithm runs on.
134 135
    typedef typename TR::Digraph Digraph;
135 136

	
136 137
    ///\brief The type of the map that stores the predecessor arcs of the
137 138
    ///DFS paths.
138 139
    typedef typename TR::PredMap PredMap;
139 140
    ///The type of the map that stores the distances of the nodes.
140 141
    typedef typename TR::DistMap DistMap;
141 142
    ///The type of the map that indicates which nodes are reached.
142 143
    typedef typename TR::ReachedMap ReachedMap;
143 144
    ///The type of the map that indicates which nodes are processed.
144 145
    typedef typename TR::ProcessedMap ProcessedMap;
145 146
    ///The type of the paths.
146 147
    typedef PredMapPath<Digraph, PredMap> Path;
147 148

	
148 149
    ///The \ref DfsDefaultTraits "traits class" of the algorithm.
149 150
    typedef TR Traits;
150 151

	
151 152
  private:
152 153

	
153 154
    typedef typename Digraph::Node Node;
154 155
    typedef typename Digraph::NodeIt NodeIt;
155 156
    typedef typename Digraph::Arc Arc;
156 157
    typedef typename Digraph::OutArcIt OutArcIt;
157 158

	
158 159
    //Pointer to the underlying digraph.
159 160
    const Digraph *G;
160 161
    //Pointer to the map of predecessor arcs.
161 162
    PredMap *_pred;
162 163
    //Indicates if _pred is locally allocated (true) or not.
163 164
    bool local_pred;
164 165
    //Pointer to the map of distances.
165 166
    DistMap *_dist;
166 167
    //Indicates if _dist is locally allocated (true) or not.
167 168
    bool local_dist;
168 169
    //Pointer to the map of reached status of the nodes.
169 170
    ReachedMap *_reached;
170 171
    //Indicates if _reached is locally allocated (true) or not.
171 172
    bool local_reached;
172 173
    //Pointer to the map of processed status of the nodes.
173 174
    ProcessedMap *_processed;
174 175
    //Indicates if _processed is locally allocated (true) or not.
175 176
    bool local_processed;
176 177

	
177 178
    std::vector<typename Digraph::OutArcIt> _stack;
178 179
    int _stack_head;
179 180

	
180 181
    //Creates the maps if necessary.
181 182
    void create_maps()
182 183
    {
183 184
      if(!_pred) {
184 185
        local_pred = true;
185 186
        _pred = Traits::createPredMap(*G);
186 187
      }
187 188
      if(!_dist) {
188 189
        local_dist = true;
189 190
        _dist = Traits::createDistMap(*G);
190 191
      }
191 192
      if(!_reached) {
192 193
        local_reached = true;
193 194
        _reached = Traits::createReachedMap(*G);
194 195
      }
195 196
      if(!_processed) {
196 197
        local_processed = true;
197 198
        _processed = Traits::createProcessedMap(*G);
198 199
      }
199 200
    }
200 201

	
201 202
  protected:
202 203

	
203 204
    Dfs() {}
204 205

	
205 206
  public:
206 207

	
207 208
    typedef Dfs Create;
208 209

	
209
    ///\name Named template parameters
210
    ///\name Named Template Parameters
210 211

	
211 212
    ///@{
212 213

	
213 214
    template <class T>
214 215
    struct SetPredMapTraits : public Traits {
215 216
      typedef T PredMap;
216 217
      static PredMap *createPredMap(const Digraph &)
217 218
      {
218 219
        LEMON_ASSERT(false, "PredMap is not initialized");
219 220
        return 0; // ignore warnings
220 221
      }
221 222
    };
222 223
    ///\brief \ref named-templ-param "Named parameter" for setting
223
    ///PredMap type.
224
    ///\c PredMap type.
224 225
    ///
225 226
    ///\ref named-templ-param "Named parameter" for setting
226
    ///PredMap type.
227
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
227
    ///\c PredMap type.
228
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
228 229
    template <class T>
229 230
    struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
230 231
      typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
231 232
    };
232 233

	
233 234
    template <class T>
234 235
    struct SetDistMapTraits : public Traits {
235 236
      typedef T DistMap;
236 237
      static DistMap *createDistMap(const Digraph &)
237 238
      {
238 239
        LEMON_ASSERT(false, "DistMap is not initialized");
239 240
        return 0; // ignore warnings
240 241
      }
241 242
    };
242 243
    ///\brief \ref named-templ-param "Named parameter" for setting
243
    ///DistMap type.
244
    ///\c DistMap type.
244 245
    ///
245 246
    ///\ref named-templ-param "Named parameter" for setting
246
    ///DistMap type.
247
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
247
    ///\c DistMap type.
248
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
248 249
    template <class T>
249 250
    struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
250 251
      typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
251 252
    };
252 253

	
253 254
    template <class T>
254 255
    struct SetReachedMapTraits : public Traits {
255 256
      typedef T ReachedMap;
256 257
      static ReachedMap *createReachedMap(const Digraph &)
257 258
      {
258 259
        LEMON_ASSERT(false, "ReachedMap is not initialized");
259 260
        return 0; // ignore warnings
260 261
      }
261 262
    };
262 263
    ///\brief \ref named-templ-param "Named parameter" for setting
263
    ///ReachedMap type.
264
    ///\c ReachedMap type.
264 265
    ///
265 266
    ///\ref named-templ-param "Named parameter" for setting
266
    ///ReachedMap type.
267
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
267
    ///\c ReachedMap type.
268
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
268 269
    template <class T>
269 270
    struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
270 271
      typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
271 272
    };
272 273

	
273 274
    template <class T>
274 275
    struct SetProcessedMapTraits : public Traits {
275 276
      typedef T ProcessedMap;
276 277
      static ProcessedMap *createProcessedMap(const Digraph &)
277 278
      {
278 279
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
279 280
        return 0; // ignore warnings
280 281
      }
281 282
    };
282 283
    ///\brief \ref named-templ-param "Named parameter" for setting
283
    ///ProcessedMap type.
284
    ///\c ProcessedMap type.
284 285
    ///
285 286
    ///\ref named-templ-param "Named parameter" for setting
286
    ///ProcessedMap type.
287
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
287
    ///\c ProcessedMap type.
288
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
288 289
    template <class T>
289 290
    struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
290 291
      typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
291 292
    };
292 293

	
293 294
    struct SetStandardProcessedMapTraits : public Traits {
294 295
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
295 296
      static ProcessedMap *createProcessedMap(const Digraph &g)
296 297
      {
297 298
        return new ProcessedMap(g);
298 299
      }
299 300
    };
300 301
    ///\brief \ref named-templ-param "Named parameter" for setting
301
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
302
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
302 303
    ///
303 304
    ///\ref named-templ-param "Named parameter" for setting
304
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
305
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
305 306
    ///If you don't set it explicitly, it will be automatically allocated.
306 307
    struct SetStandardProcessedMap :
307 308
      public Dfs< Digraph, SetStandardProcessedMapTraits > {
308 309
      typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create;
309 310
    };
310 311

	
311 312
    ///@}
312 313

	
313 314
  public:
314 315

	
315 316
    ///Constructor.
316 317

	
317 318
    ///Constructor.
318 319
    ///\param g The digraph the algorithm runs on.
319 320
    Dfs(const Digraph &g) :
320 321
      G(&g),
321 322
      _pred(NULL), local_pred(false),
322 323
      _dist(NULL), local_dist(false),
323 324
      _reached(NULL), local_reached(false),
324 325
      _processed(NULL), local_processed(false)
325 326
    { }
326 327

	
327 328
    ///Destructor.
328 329
    ~Dfs()
329 330
    {
330 331
      if(local_pred) delete _pred;
331 332
      if(local_dist) delete _dist;
332 333
      if(local_reached) delete _reached;
333 334
      if(local_processed) delete _processed;
334 335
    }
335 336

	
336 337
    ///Sets the map that stores the predecessor arcs.
337 338

	
338 339
    ///Sets the map that stores the predecessor arcs.
339 340
    ///If you don't use this function before calling \ref run(Node) "run()"
340 341
    ///or \ref init(), an instance will be allocated automatically.
341 342
    ///The destructor deallocates this automatically allocated map,
342 343
    ///of course.
343 344
    ///\return <tt> (*this) </tt>
344 345
    Dfs &predMap(PredMap &m)
345 346
    {
346 347
      if(local_pred) {
347 348
        delete _pred;
348 349
        local_pred=false;
349 350
      }
350 351
      _pred = &m;
351 352
      return *this;
352 353
    }
353 354

	
354 355
    ///Sets the map that indicates which nodes are reached.
355 356

	
356 357
    ///Sets the map that indicates which nodes are reached.
357 358
    ///If you don't use this function before calling \ref run(Node) "run()"
358 359
    ///or \ref init(), an instance will be allocated automatically.
359 360
    ///The destructor deallocates this automatically allocated map,
360 361
    ///of course.
361 362
    ///\return <tt> (*this) </tt>
362 363
    Dfs &reachedMap(ReachedMap &m)
363 364
    {
364 365
      if(local_reached) {
365 366
        delete _reached;
366 367
        local_reached=false;
367 368
      }
368 369
      _reached = &m;
369 370
      return *this;
370 371
    }
371 372

	
372 373
    ///Sets the map that indicates which nodes are processed.
373 374

	
374 375
    ///Sets the map that indicates which nodes are processed.
375 376
    ///If you don't use this function before calling \ref run(Node) "run()"
376 377
    ///or \ref init(), an instance will be allocated automatically.
377 378
    ///The destructor deallocates this automatically allocated map,
378 379
    ///of course.
379 380
    ///\return <tt> (*this) </tt>
380 381
    Dfs &processedMap(ProcessedMap &m)
381 382
    {
382 383
      if(local_processed) {
383 384
        delete _processed;
384 385
        local_processed=false;
385 386
      }
386 387
      _processed = &m;
387 388
      return *this;
388 389
    }
389 390

	
390 391
    ///Sets the map that stores the distances of the nodes.
391 392

	
392 393
    ///Sets the map that stores the distances of the nodes calculated by
393 394
    ///the algorithm.
394 395
    ///If you don't use this function before calling \ref run(Node) "run()"
395 396
    ///or \ref init(), an instance will be allocated automatically.
396 397
    ///The destructor deallocates this automatically allocated map,
397 398
    ///of course.
398 399
    ///\return <tt> (*this) </tt>
399 400
    Dfs &distMap(DistMap &m)
400 401
    {
401 402
      if(local_dist) {
402 403
        delete _dist;
403 404
        local_dist=false;
404 405
      }
405 406
      _dist = &m;
406 407
      return *this;
407 408
    }
408 409

	
409 410
  public:
410 411

	
411 412
    ///\name Execution Control
412 413
    ///The simplest way to execute the DFS algorithm is to use one of the
413 414
    ///member functions called \ref run(Node) "run()".\n
414
    ///If you need more control on the execution, first you have to call
415
    ///\ref init(), then you can add a source node with \ref addSource()
415
    ///If you need better control on the execution, you have to call
416
    ///\ref init() first, then you can add a source node with \ref addSource()
416 417
    ///and perform the actual computation with \ref start().
417 418
    ///This procedure can be repeated if there are nodes that have not
418 419
    ///been reached.
419 420

	
420 421
    ///@{
421 422

	
422 423
    ///\brief Initializes the internal data structures.
423 424
    ///
424 425
    ///Initializes the internal data structures.
425 426
    void init()
426 427
    {
427 428
      create_maps();
428 429
      _stack.resize(countNodes(*G));
429 430
      _stack_head=-1;
430 431
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
431 432
        _pred->set(u,INVALID);
432 433
        _reached->set(u,false);
433 434
        _processed->set(u,false);
434 435
      }
435 436
    }
436 437

	
437 438
    ///Adds a new source node.
438 439

	
439 440
    ///Adds a new source node to the set of nodes to be processed.
440 441
    ///
441 442
    ///\pre The stack must be empty. Otherwise the algorithm gives
442 443
    ///wrong results. (One of the outgoing arcs of all the source nodes
443 444
    ///except for the last one will not be visited and distances will
444 445
    ///also be wrong.)
445 446
    void addSource(Node s)
446 447
    {
447 448
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
448 449
      if(!(*_reached)[s])
449 450
        {
450 451
          _reached->set(s,true);
451 452
          _pred->set(s,INVALID);
452 453
          OutArcIt e(*G,s);
453 454
          if(e!=INVALID) {
454 455
            _stack[++_stack_head]=e;
455 456
            _dist->set(s,_stack_head);
456 457
          }
457 458
          else {
458 459
            _processed->set(s,true);
459 460
            _dist->set(s,0);
460 461
          }
461 462
        }
462 463
    }
463 464

	
464 465
    ///Processes the next arc.
465 466

	
466 467
    ///Processes the next arc.
467 468
    ///
468 469
    ///\return The processed arc.
469 470
    ///
470 471
    ///\pre The stack must not be empty.
471 472
    Arc processNextArc()
472 473
    {
473 474
      Node m;
474 475
      Arc e=_stack[_stack_head];
475 476
      if(!(*_reached)[m=G->target(e)]) {
476 477
        _pred->set(m,e);
477 478
        _reached->set(m,true);
478 479
        ++_stack_head;
479 480
        _stack[_stack_head] = OutArcIt(*G, m);
480 481
        _dist->set(m,_stack_head);
481 482
      }
482 483
      else {
483 484
        m=G->source(e);
484 485
        ++_stack[_stack_head];
485 486
      }
486 487
      while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
487 488
        _processed->set(m,true);
488 489
        --_stack_head;
489 490
        if(_stack_head>=0) {
490 491
          m=G->source(_stack[_stack_head]);
491 492
          ++_stack[_stack_head];
492 493
        }
493 494
      }
494 495
      return e;
495 496
    }
496 497

	
497 498
    ///Next arc to be processed.
498 499

	
499 500
    ///Next arc to be processed.
500 501
    ///
501 502
    ///\return The next arc to be processed or \c INVALID if the stack
502 503
    ///is empty.
503 504
    OutArcIt nextArc() const
504 505
    {
505 506
      return _stack_head>=0?_stack[_stack_head]:INVALID;
506 507
    }
507 508

	
508 509
    ///Returns \c false if there are nodes to be processed.
509 510

	
510 511
    ///Returns \c false if there are nodes to be processed
511 512
    ///in the queue (stack).
... ...
@@ -576,894 +577,888 @@
576 577
    ///
577 578
    ///\pre init() must be called and a root node should be
578 579
    ///added with addSource() before using this function.
579 580
    ///
580 581
    ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
581 582
    ///not a node map.
582 583
    template<class ArcBoolMap>
583 584
    Arc start(const ArcBoolMap &am)
584 585
    {
585 586
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
586 587
        processNextArc();
587 588
      return emptyQueue() ? INVALID : _stack[_stack_head];
588 589
    }
589 590

	
590 591
    ///Runs the algorithm from the given source node.
591 592

	
592 593
    ///This method runs the %DFS algorithm from node \c s
593 594
    ///in order to compute the DFS path to each node.
594 595
    ///
595 596
    ///The algorithm computes
596 597
    ///- the %DFS tree,
597 598
    ///- the distance of each node from the root in the %DFS tree.
598 599
    ///
599 600
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
600 601
    ///\code
601 602
    ///  d.init();
602 603
    ///  d.addSource(s);
603 604
    ///  d.start();
604 605
    ///\endcode
605 606
    void run(Node s) {
606 607
      init();
607 608
      addSource(s);
608 609
      start();
609 610
    }
610 611

	
611 612
    ///Finds the %DFS path between \c s and \c t.
612 613

	
613 614
    ///This method runs the %DFS algorithm from node \c s
614 615
    ///in order to compute the DFS path to node \c t
615 616
    ///(it stops searching when \c t is processed)
616 617
    ///
617 618
    ///\return \c true if \c t is reachable form \c s.
618 619
    ///
619 620
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
620 621
    ///just a shortcut of the following code.
621 622
    ///\code
622 623
    ///  d.init();
623 624
    ///  d.addSource(s);
624 625
    ///  d.start(t);
625 626
    ///\endcode
626 627
    bool run(Node s,Node t) {
627 628
      init();
628 629
      addSource(s);
629 630
      start(t);
630 631
      return reached(t);
631 632
    }
632 633

	
633 634
    ///Runs the algorithm to visit all nodes in the digraph.
634 635

	
635 636
    ///This method runs the %DFS algorithm in order to compute the
636 637
    ///%DFS path to each node.
637 638
    ///
638 639
    ///The algorithm computes
639 640
    ///- the %DFS tree (forest),
640 641
    ///- the distance of each node from the root(s) in the %DFS tree.
641 642
    ///
642 643
    ///\note <tt>d.run()</tt> is just a shortcut of the following code.
643 644
    ///\code
644 645
    ///  d.init();
645 646
    ///  for (NodeIt n(digraph); n != INVALID; ++n) {
646 647
    ///    if (!d.reached(n)) {
647 648
    ///      d.addSource(n);
648 649
    ///      d.start();
649 650
    ///    }
650 651
    ///  }
651 652
    ///\endcode
652 653
    void run() {
653 654
      init();
654 655
      for (NodeIt it(*G); it != INVALID; ++it) {
655 656
        if (!reached(it)) {
656 657
          addSource(it);
657 658
          start();
658 659
        }
659 660
      }
660 661
    }
661 662

	
662 663
    ///@}
663 664

	
664 665
    ///\name Query Functions
665 666
    ///The results of the DFS algorithm can be obtained using these
666 667
    ///functions.\n
667 668
    ///Either \ref run(Node) "run()" or \ref start() should be called
668 669
    ///before using them.
669 670

	
670 671
    ///@{
671 672

	
672
    ///The DFS path to a node.
673
    ///The DFS path to the given node.
673 674

	
674
    ///Returns the DFS path to a node.
675
    ///Returns the DFS path to the given node from the root(s).
675 676
    ///
676 677
    ///\warning \c t should be reached from the root(s).
677 678
    ///
678 679
    ///\pre Either \ref run(Node) "run()" or \ref init()
679 680
    ///must be called before using this function.
680 681
    Path path(Node t) const { return Path(*G, *_pred, t); }
681 682

	
682
    ///The distance of a node from the root(s).
683
    ///The distance of the given node from the root(s).
683 684

	
684
    ///Returns the distance of a node from the root(s).
685
    ///Returns the distance of the given node from the root(s).
685 686
    ///
686 687
    ///\warning If node \c v is not reached from the root(s), then
687 688
    ///the return value of this function is undefined.
688 689
    ///
689 690
    ///\pre Either \ref run(Node) "run()" or \ref init()
690 691
    ///must be called before using this function.
691 692
    int dist(Node v) const { return (*_dist)[v]; }
692 693

	
693
    ///Returns the 'previous arc' of the %DFS tree for a node.
694
    ///Returns the 'previous arc' of the %DFS tree for the given node.
694 695

	
695 696
    ///This function returns the 'previous arc' of the %DFS tree for the
696 697
    ///node \c v, i.e. it returns the last arc of a %DFS path from a
697 698
    ///root to \c v. It is \c INVALID if \c v is not reached from the
698 699
    ///root(s) or if \c v is a root.
699 700
    ///
700 701
    ///The %DFS tree used here is equal to the %DFS tree used in
701
    ///\ref predNode().
702
    ///\ref predNode() and \ref predMap().
702 703
    ///
703 704
    ///\pre Either \ref run(Node) "run()" or \ref init()
704 705
    ///must be called before using this function.
705 706
    Arc predArc(Node v) const { return (*_pred)[v];}
706 707

	
707
    ///Returns the 'previous node' of the %DFS tree.
708
    ///Returns the 'previous node' of the %DFS tree for the given node.
708 709

	
709 710
    ///This function returns the 'previous node' of the %DFS
710 711
    ///tree for the node \c v, i.e. it returns the last but one node
711
    ///from a %DFS path from a root to \c v. It is \c INVALID
712
    ///of a %DFS path from a root to \c v. It is \c INVALID
712 713
    ///if \c v is not reached from the root(s) or if \c v is a root.
713 714
    ///
714 715
    ///The %DFS tree used here is equal to the %DFS tree used in
715
    ///\ref predArc().
716
    ///\ref predArc() and \ref predMap().
716 717
    ///
717 718
    ///\pre Either \ref run(Node) "run()" or \ref init()
718 719
    ///must be called before using this function.
719 720
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
720 721
                                  G->source((*_pred)[v]); }
721 722

	
722 723
    ///\brief Returns a const reference to the node map that stores the
723 724
    ///distances of the nodes.
724 725
    ///
725 726
    ///Returns a const reference to the node map that stores the
726 727
    ///distances of the nodes calculated by the algorithm.
727 728
    ///
728 729
    ///\pre Either \ref run(Node) "run()" or \ref init()
729 730
    ///must be called before using this function.
730 731
    const DistMap &distMap() const { return *_dist;}
731 732

	
732 733
    ///\brief Returns a const reference to the node map that stores the
733 734
    ///predecessor arcs.
734 735
    ///
735 736
    ///Returns a const reference to the node map that stores the predecessor
736
    ///arcs, which form the DFS tree.
737
    ///arcs, which form the DFS tree (forest).
737 738
    ///
738 739
    ///\pre Either \ref run(Node) "run()" or \ref init()
739 740
    ///must be called before using this function.
740 741
    const PredMap &predMap() const { return *_pred;}
741 742

	
742
    ///Checks if a node is reached from the root(s).
743
    ///Checks if the given node. node is reached from the root(s).
743 744

	
744 745
    ///Returns \c true if \c v is reached from the root(s).
745 746
    ///
746 747
    ///\pre Either \ref run(Node) "run()" or \ref init()
747 748
    ///must be called before using this function.
748 749
    bool reached(Node v) const { return (*_reached)[v]; }
749 750

	
750 751
    ///@}
751 752
  };
752 753

	
753 754
  ///Default traits class of dfs() function.
754 755

	
755 756
  ///Default traits class of dfs() function.
756 757
  ///\tparam GR Digraph type.
757 758
  template<class GR>
758 759
  struct DfsWizardDefaultTraits
759 760
  {
760 761
    ///The type of the digraph the algorithm runs on.
761 762
    typedef GR Digraph;
762 763

	
763 764
    ///\brief The type of the map that stores the predecessor
764 765
    ///arcs of the %DFS paths.
765 766
    ///
766 767
    ///The type of the map that stores the predecessor
767 768
    ///arcs of the %DFS paths.
768
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
769
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
769 770
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
770 771
    ///Instantiates a PredMap.
771 772

	
772 773
    ///This function instantiates a PredMap.
773 774
    ///\param g is the digraph, to which we would like to define the
774 775
    ///PredMap.
775 776
    static PredMap *createPredMap(const Digraph &g)
776 777
    {
777 778
      return new PredMap(g);
778 779
    }
779 780

	
780 781
    ///The type of the map that indicates which nodes are processed.
781 782

	
782 783
    ///The type of the map that indicates which nodes are processed.
783
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
784
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
784 785
    ///By default it is a NullMap.
785 786
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
786 787
    ///Instantiates a ProcessedMap.
787 788

	
788 789
    ///This function instantiates a ProcessedMap.
789 790
    ///\param g is the digraph, to which
790 791
    ///we would like to define the ProcessedMap.
791 792
#ifdef DOXYGEN
792 793
    static ProcessedMap *createProcessedMap(const Digraph &g)
793 794
#else
794 795
    static ProcessedMap *createProcessedMap(const Digraph &)
795 796
#endif
796 797
    {
797 798
      return new ProcessedMap();
798 799
    }
799 800

	
800 801
    ///The type of the map that indicates which nodes are reached.
801 802

	
802 803
    ///The type of the map that indicates which nodes are reached.
803
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
804
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
804 805
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
805 806
    ///Instantiates a ReachedMap.
806 807

	
807 808
    ///This function instantiates a ReachedMap.
808 809
    ///\param g is the digraph, to which
809 810
    ///we would like to define the ReachedMap.
810 811
    static ReachedMap *createReachedMap(const Digraph &g)
811 812
    {
812 813
      return new ReachedMap(g);
813 814
    }
814 815

	
815 816
    ///The type of the map that stores the distances of the nodes.
816 817

	
817 818
    ///The type of the map that stores the distances of the nodes.
818
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
819
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
819 820
    typedef typename Digraph::template NodeMap<int> DistMap;
820 821
    ///Instantiates a DistMap.
821 822

	
822 823
    ///This function instantiates a DistMap.
823 824
    ///\param g is the digraph, to which we would like to define
824 825
    ///the DistMap
825 826
    static DistMap *createDistMap(const Digraph &g)
826 827
    {
827 828
      return new DistMap(g);
828 829
    }
829 830

	
830 831
    ///The type of the DFS paths.
831 832

	
832 833
    ///The type of the DFS paths.
833
    ///It must meet the \ref concepts::Path "Path" concept.
834
    ///It must conform to the \ref concepts::Path "Path" concept.
834 835
    typedef lemon::Path<Digraph> Path;
835 836
  };
836 837

	
837 838
  /// Default traits class used by DfsWizard
838 839

	
839
  /// To make it easier to use Dfs algorithm
840
  /// we have created a wizard class.
841
  /// This \ref DfsWizard class needs default traits,
842
  /// as well as the \ref Dfs class.
843
  /// The \ref DfsWizardBase is a class to be the default traits of the
844
  /// \ref DfsWizard class.
840
  /// Default traits class used by DfsWizard.
841
  /// \tparam GR The type of the digraph.
845 842
  template<class GR>
846 843
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
847 844
  {
848 845

	
849 846
    typedef DfsWizardDefaultTraits<GR> Base;
850 847
  protected:
851 848
    //The type of the nodes in the digraph.
852 849
    typedef typename Base::Digraph::Node Node;
853 850

	
854 851
    //Pointer to the digraph the algorithm runs on.
855 852
    void *_g;
856 853
    //Pointer to the map of reached nodes.
857 854
    void *_reached;
858 855
    //Pointer to the map of processed nodes.
859 856
    void *_processed;
860 857
    //Pointer to the map of predecessors arcs.
861 858
    void *_pred;
862 859
    //Pointer to the map of distances.
863 860
    void *_dist;
864 861
    //Pointer to the DFS path to the target node.
865 862
    void *_path;
866 863
    //Pointer to the distance of the target node.
867 864
    int *_di;
868 865

	
869 866
    public:
870 867
    /// Constructor.
871 868

	
872
    /// This constructor does not require parameters, therefore it initiates
869
    /// This constructor does not require parameters, it initiates
873 870
    /// all of the attributes to \c 0.
874 871
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
875 872
                      _dist(0), _path(0), _di(0) {}
876 873

	
877 874
    /// Constructor.
878 875

	
879 876
    /// This constructor requires one parameter,
880 877
    /// others are initiated to \c 0.
881 878
    /// \param g The digraph the algorithm runs on.
882 879
    DfsWizardBase(const GR &g) :
883 880
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
884 881
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
885 882

	
886 883
  };
887 884

	
888 885
  /// Auxiliary class for the function-type interface of DFS algorithm.
889 886

	
890 887
  /// This auxiliary class is created to implement the
891 888
  /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
892 889
  /// It does not have own \ref run(Node) "run()" method, it uses the
893 890
  /// functions and features of the plain \ref Dfs.
894 891
  ///
895 892
  /// This class should only be used through the \ref dfs() function,
896 893
  /// which makes it easier to use the algorithm.
897 894
  template<class TR>
898 895
  class DfsWizard : public TR
899 896
  {
900 897
    typedef TR Base;
901 898

	
902
    ///The type of the digraph the algorithm runs on.
903 899
    typedef typename TR::Digraph Digraph;
904 900

	
905 901
    typedef typename Digraph::Node Node;
906 902
    typedef typename Digraph::NodeIt NodeIt;
907 903
    typedef typename Digraph::Arc Arc;
908 904
    typedef typename Digraph::OutArcIt OutArcIt;
909 905

	
910
    ///\brief The type of the map that stores the predecessor
911
    ///arcs of the DFS paths.
912 906
    typedef typename TR::PredMap PredMap;
913
    ///\brief The type of the map that stores the distances of the nodes.
914 907
    typedef typename TR::DistMap DistMap;
915
    ///\brief The type of the map that indicates which nodes are reached.
916 908
    typedef typename TR::ReachedMap ReachedMap;
917
    ///\brief The type of the map that indicates which nodes are processed.
918 909
    typedef typename TR::ProcessedMap ProcessedMap;
919
    ///The type of the DFS paths
920 910
    typedef typename TR::Path Path;
921 911

	
922 912
  public:
923 913

	
924 914
    /// Constructor.
925 915
    DfsWizard() : TR() {}
926 916

	
927 917
    /// Constructor that requires parameters.
928 918

	
929 919
    /// Constructor that requires parameters.
930 920
    /// These parameters will be the default values for the traits class.
931 921
    /// \param g The digraph the algorithm runs on.
932 922
    DfsWizard(const Digraph &g) :
933 923
      TR(g) {}
934 924

	
935 925
    ///Copy constructor
936 926
    DfsWizard(const TR &b) : TR(b) {}
937 927

	
938 928
    ~DfsWizard() {}
939 929

	
940 930
    ///Runs DFS algorithm from the given source node.
941 931

	
942 932
    ///This method runs DFS algorithm from node \c s
943 933
    ///in order to compute the DFS path to each node.
944 934
    void run(Node s)
945 935
    {
946 936
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
947 937
      if (Base::_pred)
948 938
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
949 939
      if (Base::_dist)
950 940
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
951 941
      if (Base::_reached)
952 942
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
953 943
      if (Base::_processed)
954 944
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
955 945
      if (s!=INVALID)
956 946
        alg.run(s);
957 947
      else
958 948
        alg.run();
959 949
    }
960 950

	
961 951
    ///Finds the DFS path between \c s and \c t.
962 952

	
963 953
    ///This method runs DFS algorithm from node \c s
964 954
    ///in order to compute the DFS path to node \c t
965 955
    ///(it stops searching when \c t is processed).
966 956
    ///
967 957
    ///\return \c true if \c t is reachable form \c s.
968 958
    bool run(Node s, Node t)
969 959
    {
970 960
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
971 961
      if (Base::_pred)
972 962
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
973 963
      if (Base::_dist)
974 964
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
975 965
      if (Base::_reached)
976 966
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
977 967
      if (Base::_processed)
978 968
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
979 969
      alg.run(s,t);
980 970
      if (Base::_path)
981 971
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
982 972
      if (Base::_di)
983 973
        *Base::_di = alg.dist(t);
984 974
      return alg.reached(t);
985 975
      }
986 976

	
987 977
    ///Runs DFS algorithm to visit all nodes in the digraph.
988 978

	
989 979
    ///This method runs DFS algorithm in order to compute
990 980
    ///the DFS path to each node.
991 981
    void run()
992 982
    {
993 983
      run(INVALID);
994 984
    }
995 985

	
996 986
    template<class T>
997 987
    struct SetPredMapBase : public Base {
998 988
      typedef T PredMap;
999 989
      static PredMap *createPredMap(const Digraph &) { return 0; };
1000 990
      SetPredMapBase(const TR &b) : TR(b) {}
1001 991
    };
1002
    ///\brief \ref named-func-param "Named parameter"
1003
    ///for setting PredMap object.
992

	
993
    ///\brief \ref named-templ-param "Named parameter" for setting
994
    ///the predecessor map.
1004 995
    ///
1005
    ///\ref named-func-param "Named parameter"
1006
    ///for setting PredMap object.
996
    ///\ref named-templ-param "Named parameter" function for setting
997
    ///the map that stores the predecessor arcs of the nodes.
1007 998
    template<class T>
1008 999
    DfsWizard<SetPredMapBase<T> > predMap(const T &t)
1009 1000
    {
1010 1001
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1011 1002
      return DfsWizard<SetPredMapBase<T> >(*this);
1012 1003
    }
1013 1004

	
1014 1005
    template<class T>
1015 1006
    struct SetReachedMapBase : public Base {
1016 1007
      typedef T ReachedMap;
1017 1008
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1018 1009
      SetReachedMapBase(const TR &b) : TR(b) {}
1019 1010
    };
1020
    ///\brief \ref named-func-param "Named parameter"
1021
    ///for setting ReachedMap object.
1011

	
1012
    ///\brief \ref named-templ-param "Named parameter" for setting
1013
    ///the reached map.
1022 1014
    ///
1023
    /// \ref named-func-param "Named parameter"
1024
    ///for setting ReachedMap object.
1015
    ///\ref named-templ-param "Named parameter" function for setting
1016
    ///the map that indicates which nodes are reached.
1025 1017
    template<class T>
1026 1018
    DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1027 1019
    {
1028 1020
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1029 1021
      return DfsWizard<SetReachedMapBase<T> >(*this);
1030 1022
    }
1031 1023

	
1032 1024
    template<class T>
1033 1025
    struct SetDistMapBase : public Base {
1034 1026
      typedef T DistMap;
1035 1027
      static DistMap *createDistMap(const Digraph &) { return 0; };
1036 1028
      SetDistMapBase(const TR &b) : TR(b) {}
1037 1029
    };
1038
    ///\brief \ref named-func-param "Named parameter"
1039
    ///for setting DistMap object.
1030

	
1031
    ///\brief \ref named-templ-param "Named parameter" for setting
1032
    ///the distance map.
1040 1033
    ///
1041
    /// \ref named-func-param "Named parameter"
1042
    ///for setting DistMap object.
1034
    ///\ref named-templ-param "Named parameter" function for setting
1035
    ///the map that stores the distances of the nodes calculated
1036
    ///by the algorithm.
1043 1037
    template<class T>
1044 1038
    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
1045 1039
    {
1046 1040
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1047 1041
      return DfsWizard<SetDistMapBase<T> >(*this);
1048 1042
    }
1049 1043

	
1050 1044
    template<class T>
1051 1045
    struct SetProcessedMapBase : public Base {
1052 1046
      typedef T ProcessedMap;
1053 1047
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1054 1048
      SetProcessedMapBase(const TR &b) : TR(b) {}
1055 1049
    };
1056
    ///\brief \ref named-func-param "Named parameter"
1057
    ///for setting ProcessedMap object.
1050

	
1051
    ///\brief \ref named-func-param "Named parameter" for setting
1052
    ///the processed map.
1058 1053
    ///
1059
    /// \ref named-func-param "Named parameter"
1060
    ///for setting ProcessedMap object.
1054
    ///\ref named-templ-param "Named parameter" function for setting
1055
    ///the map that indicates which nodes are processed.
1061 1056
    template<class T>
1062 1057
    DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1063 1058
    {
1064 1059
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1065 1060
      return DfsWizard<SetProcessedMapBase<T> >(*this);
1066 1061
    }
1067 1062

	
1068 1063
    template<class T>
1069 1064
    struct SetPathBase : public Base {
1070 1065
      typedef T Path;
1071 1066
      SetPathBase(const TR &b) : TR(b) {}
1072 1067
    };
1073 1068
    ///\brief \ref named-func-param "Named parameter"
1074 1069
    ///for getting the DFS path to the target node.
1075 1070
    ///
1076 1071
    ///\ref named-func-param "Named parameter"
1077 1072
    ///for getting the DFS path to the target node.
1078 1073
    template<class T>
1079 1074
    DfsWizard<SetPathBase<T> > path(const T &t)
1080 1075
    {
1081 1076
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1082 1077
      return DfsWizard<SetPathBase<T> >(*this);
1083 1078
    }
1084 1079

	
1085 1080
    ///\brief \ref named-func-param "Named parameter"
1086 1081
    ///for getting the distance of the target node.
1087 1082
    ///
1088 1083
    ///\ref named-func-param "Named parameter"
1089 1084
    ///for getting the distance of the target node.
1090 1085
    DfsWizard dist(const int &d)
1091 1086
    {
1092 1087
      Base::_di=const_cast<int*>(&d);
1093 1088
      return *this;
1094 1089
    }
1095 1090

	
1096 1091
  };
1097 1092

	
1098 1093
  ///Function-type interface for DFS algorithm.
1099 1094

	
1100 1095
  ///\ingroup search
1101 1096
  ///Function-type interface for DFS algorithm.
1102 1097
  ///
1103 1098
  ///This function also has several \ref named-func-param "named parameters",
1104 1099
  ///they are declared as the members of class \ref DfsWizard.
1105 1100
  ///The following examples show how to use these parameters.
1106 1101
  ///\code
1107 1102
  ///  // Compute the DFS tree
1108 1103
  ///  dfs(g).predMap(preds).distMap(dists).run(s);
1109 1104
  ///
1110 1105
  ///  // Compute the DFS path from s to t
1111 1106
  ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
1112 1107
  ///\endcode
1113 1108
  ///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()"
1114 1109
  ///to the end of the parameter list.
1115 1110
  ///\sa DfsWizard
1116 1111
  ///\sa Dfs
1117 1112
  template<class GR>
1118 1113
  DfsWizard<DfsWizardBase<GR> >
1119 1114
  dfs(const GR &digraph)
1120 1115
  {
1121 1116
    return DfsWizard<DfsWizardBase<GR> >(digraph);
1122 1117
  }
1123 1118

	
1124 1119
#ifdef DOXYGEN
1125 1120
  /// \brief Visitor class for DFS.
1126 1121
  ///
1127 1122
  /// This class defines the interface of the DfsVisit events, and
1128 1123
  /// it could be the base of a real visitor class.
1129
  template <typename _Digraph>
1124
  template <typename GR>
1130 1125
  struct DfsVisitor {
1131
    typedef _Digraph Digraph;
1126
    typedef GR Digraph;
1132 1127
    typedef typename Digraph::Arc Arc;
1133 1128
    typedef typename Digraph::Node Node;
1134 1129
    /// \brief Called for the source node of the DFS.
1135 1130
    ///
1136 1131
    /// This function is called for the source node of the DFS.
1137 1132
    void start(const Node& node) {}
1138 1133
    /// \brief Called when the source node is leaved.
1139 1134
    ///
1140 1135
    /// This function is called when the source node is leaved.
1141 1136
    void stop(const Node& node) {}
1142 1137
    /// \brief Called when a node is reached first time.
1143 1138
    ///
1144 1139
    /// This function is called when a node is reached first time.
1145 1140
    void reach(const Node& node) {}
1146 1141
    /// \brief Called when an arc reaches a new node.
1147 1142
    ///
1148 1143
    /// This function is called when the DFS finds an arc whose target node
1149 1144
    /// is not reached yet.
1150 1145
    void discover(const Arc& arc) {}
1151 1146
    /// \brief Called when an arc is examined but its target node is
1152 1147
    /// already discovered.
1153 1148
    ///
1154 1149
    /// This function is called when an arc is examined but its target node is
1155 1150
    /// already discovered.
1156 1151
    void examine(const Arc& arc) {}
1157 1152
    /// \brief Called when the DFS steps back from a node.
1158 1153
    ///
1159 1154
    /// This function is called when the DFS steps back from a node.
1160 1155
    void leave(const Node& node) {}
1161 1156
    /// \brief Called when the DFS steps back on an arc.
1162 1157
    ///
1163 1158
    /// This function is called when the DFS steps back on an arc.
1164 1159
    void backtrack(const Arc& arc) {}
1165 1160
  };
1166 1161
#else
1167
  template <typename _Digraph>
1162
  template <typename GR>
1168 1163
  struct DfsVisitor {
1169
    typedef _Digraph Digraph;
1164
    typedef GR Digraph;
1170 1165
    typedef typename Digraph::Arc Arc;
1171 1166
    typedef typename Digraph::Node Node;
1172 1167
    void start(const Node&) {}
1173 1168
    void stop(const Node&) {}
1174 1169
    void reach(const Node&) {}
1175 1170
    void discover(const Arc&) {}
1176 1171
    void examine(const Arc&) {}
1177 1172
    void leave(const Node&) {}
1178 1173
    void backtrack(const Arc&) {}
1179 1174

	
1180 1175
    template <typename _Visitor>
1181 1176
    struct Constraints {
1182 1177
      void constraints() {
1183 1178
        Arc arc;
1184 1179
        Node node;
1185 1180
        visitor.start(node);
1186 1181
        visitor.stop(arc);
1187 1182
        visitor.reach(node);
1188 1183
        visitor.discover(arc);
1189 1184
        visitor.examine(arc);
1190 1185
        visitor.leave(node);
1191 1186
        visitor.backtrack(arc);
1192 1187
      }
1193 1188
      _Visitor& visitor;
1194 1189
    };
1195 1190
  };
1196 1191
#endif
1197 1192

	
1198 1193
  /// \brief Default traits class of DfsVisit class.
1199 1194
  ///
1200 1195
  /// Default traits class of DfsVisit class.
1201 1196
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1202
  template<class _Digraph>
1197
  template<class GR>
1203 1198
  struct DfsVisitDefaultTraits {
1204 1199

	
1205 1200
    /// \brief The type of the digraph the algorithm runs on.
1206
    typedef _Digraph Digraph;
1201
    typedef GR Digraph;
1207 1202

	
1208 1203
    /// \brief The type of the map that indicates which nodes are reached.
1209 1204
    ///
1210 1205
    /// The type of the map that indicates which nodes are reached.
1211
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1206
    /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1212 1207
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1213 1208

	
1214 1209
    /// \brief Instantiates a ReachedMap.
1215 1210
    ///
1216 1211
    /// This function instantiates a ReachedMap.
1217 1212
    /// \param digraph is the digraph, to which
1218 1213
    /// we would like to define the ReachedMap.
1219 1214
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1220 1215
      return new ReachedMap(digraph);
1221 1216
    }
1222 1217

	
1223 1218
  };
1224 1219

	
1225 1220
  /// \ingroup search
1226 1221
  ///
1227
  /// \brief %DFS algorithm class with visitor interface.
1222
  /// \brief DFS algorithm class with visitor interface.
1228 1223
  ///
1229
  /// This class provides an efficient implementation of the %DFS algorithm
1224
  /// This class provides an efficient implementation of the DFS algorithm
1230 1225
  /// with visitor interface.
1231 1226
  ///
1232
  /// The %DfsVisit class provides an alternative interface to the Dfs
1227
  /// The DfsVisit class provides an alternative interface to the Dfs
1233 1228
  /// class. It works with callback mechanism, the DfsVisit object calls
1234 1229
  /// the member functions of the \c Visitor class on every DFS event.
1235 1230
  ///
1236 1231
  /// This interface of the DFS algorithm should be used in special cases
1237 1232
  /// when extra actions have to be performed in connection with certain
1238 1233
  /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
1239 1234
  /// instead.
1240 1235
  ///
1241
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1242
  /// The default value is
1243
  /// \ref ListDigraph. The value of _Digraph is not used directly by
1244
  /// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits.
1245
  /// \tparam _Visitor The Visitor type that is used by the algorithm.
1246
  /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which
1236
  /// \tparam GR The type of the digraph the algorithm runs on.
1237
  /// The default type is \ref ListDigraph.
1238
  /// The value of GR is not used directly by \ref DfsVisit,
1239
  /// it is only passed to \ref DfsVisitDefaultTraits.
1240
  /// \tparam VS The Visitor type that is used by the algorithm.
1241
  /// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which
1247 1242
  /// does not observe the DFS events. If you want to observe the DFS
1248 1243
  /// events, you should implement your own visitor class.
1249
  /// \tparam _Traits Traits class to set various data types used by the
1244
  /// \tparam TR Traits class to set various data types used by the
1250 1245
  /// algorithm. The default traits class is
1251
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>".
1246
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>".
1252 1247
  /// See \ref DfsVisitDefaultTraits for the documentation of
1253 1248
  /// a DFS visit traits class.
1254 1249
#ifdef DOXYGEN
1255
  template <typename _Digraph, typename _Visitor, typename _Traits>
1250
  template <typename GR, typename VS, typename TR>
1256 1251
#else
1257
  template <typename _Digraph = ListDigraph,
1258
            typename _Visitor = DfsVisitor<_Digraph>,
1259
            typename _Traits = DfsVisitDefaultTraits<_Digraph> >
1252
  template <typename GR = ListDigraph,
1253
            typename VS = DfsVisitor<GR>,
1254
            typename TR = DfsVisitDefaultTraits<GR> >
1260 1255
#endif
1261 1256
  class DfsVisit {
1262 1257
  public:
1263 1258

	
1264 1259
    ///The traits class.
1265
    typedef _Traits Traits;
1260
    typedef TR Traits;
1266 1261

	
1267 1262
    ///The type of the digraph the algorithm runs on.
1268 1263
    typedef typename Traits::Digraph Digraph;
1269 1264

	
1270 1265
    ///The visitor type used by the algorithm.
1271
    typedef _Visitor Visitor;
1266
    typedef VS Visitor;
1272 1267

	
1273 1268
    ///The type of the map that indicates which nodes are reached.
1274 1269
    typedef typename Traits::ReachedMap ReachedMap;
1275 1270

	
1276 1271
  private:
1277 1272

	
1278 1273
    typedef typename Digraph::Node Node;
1279 1274
    typedef typename Digraph::NodeIt NodeIt;
1280 1275
    typedef typename Digraph::Arc Arc;
1281 1276
    typedef typename Digraph::OutArcIt OutArcIt;
1282 1277

	
1283 1278
    //Pointer to the underlying digraph.
1284 1279
    const Digraph *_digraph;
1285 1280
    //Pointer to the visitor object.
1286 1281
    Visitor *_visitor;
1287 1282
    //Pointer to the map of reached status of the nodes.
1288 1283
    ReachedMap *_reached;
1289 1284
    //Indicates if _reached is locally allocated (true) or not.
1290 1285
    bool local_reached;
1291 1286

	
1292 1287
    std::vector<typename Digraph::Arc> _stack;
1293 1288
    int _stack_head;
1294 1289

	
1295 1290
    //Creates the maps if necessary.
1296 1291
    void create_maps() {
1297 1292
      if(!_reached) {
1298 1293
        local_reached = true;
1299 1294
        _reached = Traits::createReachedMap(*_digraph);
1300 1295
      }
1301 1296
    }
1302 1297

	
1303 1298
  protected:
1304 1299

	
1305 1300
    DfsVisit() {}
1306 1301

	
1307 1302
  public:
1308 1303

	
1309 1304
    typedef DfsVisit Create;
1310 1305

	
1311 1306
    /// \name Named Template Parameters
1312 1307

	
1313 1308
    ///@{
1314 1309
    template <class T>
1315 1310
    struct SetReachedMapTraits : public Traits {
1316 1311
      typedef T ReachedMap;
1317 1312
      static ReachedMap *createReachedMap(const Digraph &digraph) {
1318 1313
        LEMON_ASSERT(false, "ReachedMap is not initialized");
1319 1314
        return 0; // ignore warnings
1320 1315
      }
1321 1316
    };
1322 1317
    /// \brief \ref named-templ-param "Named parameter" for setting
1323 1318
    /// ReachedMap type.
1324 1319
    ///
1325 1320
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1326 1321
    template <class T>
1327 1322
    struct SetReachedMap : public DfsVisit< Digraph, Visitor,
1328 1323
                                            SetReachedMapTraits<T> > {
1329 1324
      typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1330 1325
    };
1331 1326
    ///@}
1332 1327

	
1333 1328
  public:
1334 1329

	
1335 1330
    /// \brief Constructor.
1336 1331
    ///
1337 1332
    /// Constructor.
1338 1333
    ///
1339 1334
    /// \param digraph The digraph the algorithm runs on.
1340 1335
    /// \param visitor The visitor object of the algorithm.
1341 1336
    DfsVisit(const Digraph& digraph, Visitor& visitor)
1342 1337
      : _digraph(&digraph), _visitor(&visitor),
1343 1338
        _reached(0), local_reached(false) {}
1344 1339

	
1345 1340
    /// \brief Destructor.
1346 1341
    ~DfsVisit() {
1347 1342
      if(local_reached) delete _reached;
1348 1343
    }
1349 1344

	
1350 1345
    /// \brief Sets the map that indicates which nodes are reached.
1351 1346
    ///
1352 1347
    /// Sets the map that indicates which nodes are reached.
1353 1348
    /// If you don't use this function before calling \ref run(Node) "run()"
1354 1349
    /// or \ref init(), an instance will be allocated automatically.
1355 1350
    /// The destructor deallocates this automatically allocated map,
1356 1351
    /// of course.
1357 1352
    /// \return <tt> (*this) </tt>
1358 1353
    DfsVisit &reachedMap(ReachedMap &m) {
1359 1354
      if(local_reached) {
1360 1355
        delete _reached;
1361 1356
        local_reached=false;
1362 1357
      }
1363 1358
      _reached = &m;
1364 1359
      return *this;
1365 1360
    }
1366 1361

	
1367 1362
  public:
1368 1363

	
1369 1364
    /// \name Execution Control
1370 1365
    /// The simplest way to execute the DFS algorithm is to use one of the
1371 1366
    /// member functions called \ref run(Node) "run()".\n
1372
    /// If you need more control on the execution, first you have to call
1373
    /// \ref init(), then you can add a source node with \ref addSource()
1367
    /// If you need better control on the execution, you have to call
1368
    /// \ref init() first, then you can add a source node with \ref addSource()
1374 1369
    /// and perform the actual computation with \ref start().
1375 1370
    /// This procedure can be repeated if there are nodes that have not
1376 1371
    /// been reached.
1377 1372

	
1378 1373
    /// @{
1379 1374

	
1380 1375
    /// \brief Initializes the internal data structures.
1381 1376
    ///
1382 1377
    /// Initializes the internal data structures.
1383 1378
    void init() {
1384 1379
      create_maps();
1385 1380
      _stack.resize(countNodes(*_digraph));
1386 1381
      _stack_head = -1;
1387 1382
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1388 1383
        _reached->set(u, false);
1389 1384
      }
1390 1385
    }
1391 1386

	
1392 1387
    /// \brief Adds a new source node.
1393 1388
    ///
1394 1389
    /// Adds a new source node to the set of nodes to be processed.
1395 1390
    ///
1396 1391
    /// \pre The stack must be empty. Otherwise the algorithm gives
1397 1392
    /// wrong results. (One of the outgoing arcs of all the source nodes
1398 1393
    /// except for the last one will not be visited and distances will
1399 1394
    /// also be wrong.)
1400 1395
    void addSource(Node s)
1401 1396
    {
1402 1397
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
1403 1398
      if(!(*_reached)[s]) {
1404 1399
          _reached->set(s,true);
1405 1400
          _visitor->start(s);
1406 1401
          _visitor->reach(s);
1407 1402
          Arc e;
1408 1403
          _digraph->firstOut(e, s);
1409 1404
          if (e != INVALID) {
1410 1405
            _stack[++_stack_head] = e;
1411 1406
          } else {
1412 1407
            _visitor->leave(s);
1413 1408
            _visitor->stop(s);
1414 1409
          }
1415 1410
        }
1416 1411
    }
1417 1412

	
1418 1413
    /// \brief Processes the next arc.
1419 1414
    ///
1420 1415
    /// Processes the next arc.
1421 1416
    ///
1422 1417
    /// \return The processed arc.
1423 1418
    ///
1424 1419
    /// \pre The stack must not be empty.
1425 1420
    Arc processNextArc() {
1426 1421
      Arc e = _stack[_stack_head];
1427 1422
      Node m = _digraph->target(e);
1428 1423
      if(!(*_reached)[m]) {
1429 1424
        _visitor->discover(e);
1430 1425
        _visitor->reach(m);
1431 1426
        _reached->set(m, true);
1432 1427
        _digraph->firstOut(_stack[++_stack_head], m);
1433 1428
      } else {
1434 1429
        _visitor->examine(e);
1435 1430
        m = _digraph->source(e);
1436 1431
        _digraph->nextOut(_stack[_stack_head]);
1437 1432
      }
1438 1433
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
1439 1434
        _visitor->leave(m);
1440 1435
        --_stack_head;
1441 1436
        if (_stack_head >= 0) {
1442 1437
          _visitor->backtrack(_stack[_stack_head]);
1443 1438
          m = _digraph->source(_stack[_stack_head]);
1444 1439
          _digraph->nextOut(_stack[_stack_head]);
1445 1440
        } else {
1446 1441
          _visitor->stop(m);
1447 1442
        }
1448 1443
      }
1449 1444
      return e;
1450 1445
    }
1451 1446

	
1452 1447
    /// \brief Next arc to be processed.
1453 1448
    ///
1454 1449
    /// Next arc to be processed.
1455 1450
    ///
1456 1451
    /// \return The next arc to be processed or INVALID if the stack is
1457 1452
    /// empty.
1458 1453
    Arc nextArc() const {
1459 1454
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
1460 1455
    }
1461 1456

	
1462 1457
    /// \brief Returns \c false if there are nodes
1463 1458
    /// to be processed.
1464 1459
    ///
1465 1460
    /// Returns \c false if there are nodes
1466 1461
    /// to be processed in the queue (stack).
1467 1462
    bool emptyQueue() const { return _stack_head < 0; }
1468 1463

	
1469 1464
    /// \brief Returns the number of the nodes to be processed.
... ...
@@ -1527,111 +1522,111 @@
1527 1522
    /// \c INVALID if no such arc was found.
1528 1523
    ///
1529 1524
    /// \pre init() must be called and a root node should be added
1530 1525
    /// with addSource() before using this function.
1531 1526
    ///
1532 1527
    /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
1533 1528
    /// not a node map.
1534 1529
    template <typename AM>
1535 1530
    Arc start(const AM &am) {
1536 1531
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
1537 1532
        processNextArc();
1538 1533
      return emptyQueue() ? INVALID : _stack[_stack_head];
1539 1534
    }
1540 1535

	
1541 1536
    /// \brief Runs the algorithm from the given source node.
1542 1537
    ///
1543 1538
    /// This method runs the %DFS algorithm from node \c s.
1544 1539
    /// in order to compute the DFS path to each node.
1545 1540
    ///
1546 1541
    /// The algorithm computes
1547 1542
    /// - the %DFS tree,
1548 1543
    /// - the distance of each node from the root in the %DFS tree.
1549 1544
    ///
1550 1545
    /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
1551 1546
    ///\code
1552 1547
    ///   d.init();
1553 1548
    ///   d.addSource(s);
1554 1549
    ///   d.start();
1555 1550
    ///\endcode
1556 1551
    void run(Node s) {
1557 1552
      init();
1558 1553
      addSource(s);
1559 1554
      start();
1560 1555
    }
1561 1556

	
1562 1557
    /// \brief Finds the %DFS path between \c s and \c t.
1563 1558

	
1564 1559
    /// This method runs the %DFS algorithm from node \c s
1565 1560
    /// in order to compute the DFS path to node \c t
1566 1561
    /// (it stops searching when \c t is processed).
1567 1562
    ///
1568 1563
    /// \return \c true if \c t is reachable form \c s.
1569 1564
    ///
1570 1565
    /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
1571 1566
    /// just a shortcut of the following code.
1572 1567
    ///\code
1573 1568
    ///   d.init();
1574 1569
    ///   d.addSource(s);
1575 1570
    ///   d.start(t);
1576 1571
    ///\endcode
1577 1572
    bool run(Node s,Node t) {
1578 1573
      init();
1579 1574
      addSource(s);
1580 1575
      start(t);
1581 1576
      return reached(t);
1582 1577
    }
1583 1578

	
1584 1579
    /// \brief Runs the algorithm to visit all nodes in the digraph.
1585 1580

	
1586 1581
    /// This method runs the %DFS algorithm in order to
1587 1582
    /// compute the %DFS path to each node.
1588 1583
    ///
1589 1584
    /// The algorithm computes
1590 1585
    /// - the %DFS tree (forest),
1591 1586
    /// - the distance of each node from the root(s) in the %DFS tree.
1592 1587
    ///
1593 1588
    /// \note <tt>d.run()</tt> is just a shortcut of the following code.
1594 1589
    ///\code
1595 1590
    ///   d.init();
1596 1591
    ///   for (NodeIt n(digraph); n != INVALID; ++n) {
1597 1592
    ///     if (!d.reached(n)) {
1598 1593
    ///       d.addSource(n);
1599 1594
    ///       d.start();
1600 1595
    ///     }
1601 1596
    ///   }
1602 1597
    ///\endcode
1603 1598
    void run() {
1604 1599
      init();
1605 1600
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
1606 1601
        if (!reached(it)) {
1607 1602
          addSource(it);
1608 1603
          start();
1609 1604
        }
1610 1605
      }
1611 1606
    }
1612 1607

	
1613 1608
    ///@}
1614 1609

	
1615 1610
    /// \name Query Functions
1616 1611
    /// The results of the DFS algorithm can be obtained using these
1617 1612
    /// functions.\n
1618 1613
    /// Either \ref run(Node) "run()" or \ref start() should be called
1619 1614
    /// before using them.
1620 1615

	
1621 1616
    ///@{
1622 1617

	
1623
    /// \brief Checks if a node is reached from the root(s).
1618
    /// \brief Checks if the given node is reached from the root(s).
1624 1619
    ///
1625 1620
    /// Returns \c true if \c v is reached from the root(s).
1626 1621
    ///
1627 1622
    /// \pre Either \ref run(Node) "run()" or \ref init()
1628 1623
    /// must be called before using this function.
1629 1624
    bool reached(Node v) const { return (*_reached)[v]; }
1630 1625

	
1631 1626
    ///@}
1632 1627

	
1633 1628
  };
1634 1629

	
1635 1630
} //END OF NAMESPACE LEMON
1636 1631

	
1637 1632
#endif
Ignore white space 6 line context
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-2009
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_DIJKSTRA_H
20 20
#define LEMON_DIJKSTRA_H
21 21

	
22 22
///\ingroup shortest_path
23 23
///\file
24 24
///\brief Dijkstra algorithm.
25 25

	
26 26
#include <limits>
27 27
#include <lemon/list_graph.h>
28 28
#include <lemon/bin_heap.h>
29 29
#include <lemon/bits/path_dump.h>
30 30
#include <lemon/core.h>
31 31
#include <lemon/error.h>
32 32
#include <lemon/maps.h>
33 33
#include <lemon/path.h>
34 34

	
35 35
namespace lemon {
36 36

	
37 37
  /// \brief Default operation traits for the Dijkstra algorithm class.
38 38
  ///
39 39
  /// This operation traits class defines all computational operations and
40 40
  /// constants which are used in the Dijkstra algorithm.
41
  template <typename Value>
41
  template <typename V>
42 42
  struct DijkstraDefaultOperationTraits {
43
    /// \e
44
    typedef V Value;
43 45
    /// \brief Gives back the zero value of the type.
44 46
    static Value zero() {
45 47
      return static_cast<Value>(0);
46 48
    }
47 49
    /// \brief Gives back the sum of the given two elements.
48 50
    static Value plus(const Value& left, const Value& right) {
49 51
      return left + right;
50 52
    }
51 53
    /// \brief Gives back true only if the first value is less than the second.
52 54
    static bool less(const Value& left, const Value& right) {
53 55
      return left < right;
54 56
    }
55 57
  };
56 58

	
57 59
  ///Default traits class of Dijkstra class.
58 60

	
59 61
  ///Default traits class of Dijkstra class.
60 62
  ///\tparam GR The type of the digraph.
61
  ///\tparam LM The type of the length map.
62
  template<class GR, class LM>
63
  ///\tparam LEN The type of the length map.
64
  template<typename GR, typename LEN>
63 65
  struct DijkstraDefaultTraits
64 66
  {
65 67
    ///The type of the digraph the algorithm runs on.
66 68
    typedef GR Digraph;
67 69

	
68 70
    ///The type of the map that stores the arc lengths.
69 71

	
70 72
    ///The type of the map that stores the arc lengths.
71
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
72
    typedef LM LengthMap;
73
    ///The type of the length of the arcs.
74
    typedef typename LM::Value Value;
73
    ///It must conform to the \ref concepts::ReadMap "ReadMap" concept.
74
    typedef LEN LengthMap;
75
    ///The type of the arc lengths.
76
    typedef typename LEN::Value Value;
75 77

	
76
    /// Operation traits for Dijkstra algorithm.
78
    /// Operation traits for %Dijkstra algorithm.
77 79

	
78 80
    /// This class defines the operations that are used in the algorithm.
79 81
    /// \see DijkstraDefaultOperationTraits
80 82
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
81 83

	
82 84
    /// The cross reference type used by the heap.
83 85

	
84 86
    /// The cross reference type used by the heap.
85 87
    /// Usually it is \c Digraph::NodeMap<int>.
86 88
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
87
    ///Instantiates a \ref HeapCrossRef.
89
    ///Instantiates a \c HeapCrossRef.
88 90

	
89 91
    ///This function instantiates a \ref HeapCrossRef.
90 92
    /// \param g is the digraph, to which we would like to define the
91 93
    /// \ref HeapCrossRef.
92 94
    static HeapCrossRef *createHeapCrossRef(const Digraph &g)
93 95
    {
94 96
      return new HeapCrossRef(g);
95 97
    }
96 98

	
97
    ///The heap type used by the Dijkstra algorithm.
99
    ///The heap type used by the %Dijkstra algorithm.
98 100

	
99 101
    ///The heap type used by the Dijkstra algorithm.
100 102
    ///
101 103
    ///\sa BinHeap
102 104
    ///\sa Dijkstra
103
    typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap;
104
    ///Instantiates a \ref Heap.
105
    typedef BinHeap<typename LEN::Value, HeapCrossRef, std::less<Value> > Heap;
106
    ///Instantiates a \c Heap.
105 107

	
106 108
    ///This function instantiates a \ref Heap.
107 109
    static Heap *createHeap(HeapCrossRef& r)
108 110
    {
109 111
      return new Heap(r);
110 112
    }
111 113

	
112 114
    ///\brief The type of the map that stores the predecessor
113 115
    ///arcs of the shortest paths.
114 116
    ///
115 117
    ///The type of the map that stores the predecessor
116 118
    ///arcs of the shortest paths.
117
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
119
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
118 120
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
119
    ///Instantiates a PredMap.
121
    ///Instantiates a \c PredMap.
120 122

	
121
    ///This function instantiates a PredMap.
123
    ///This function instantiates a \ref PredMap.
122 124
    ///\param g is the digraph, to which we would like to define the
123
    ///PredMap.
125
    ///\ref PredMap.
124 126
    static PredMap *createPredMap(const Digraph &g)
125 127
    {
126 128
      return new PredMap(g);
127 129
    }
128 130

	
129 131
    ///The type of the map that indicates which nodes are processed.
130 132

	
131 133
    ///The type of the map that indicates which nodes are processed.
132
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
134
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
133 135
    ///By default it is a NullMap.
134 136
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
135
    ///Instantiates a ProcessedMap.
137
    ///Instantiates a \c ProcessedMap.
136 138

	
137
    ///This function instantiates a ProcessedMap.
139
    ///This function instantiates a \ref ProcessedMap.
138 140
    ///\param g is the digraph, to which
139
    ///we would like to define the ProcessedMap
141
    ///we would like to define the \ref ProcessedMap.
140 142
#ifdef DOXYGEN
141 143
    static ProcessedMap *createProcessedMap(const Digraph &g)
142 144
#else
143 145
    static ProcessedMap *createProcessedMap(const Digraph &)
144 146
#endif
145 147
    {
146 148
      return new ProcessedMap();
147 149
    }
148 150

	
149 151
    ///The type of the map that stores the distances of the nodes.
150 152

	
151 153
    ///The type of the map that stores the distances of the nodes.
152
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
153
    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
154
    ///Instantiates a DistMap.
154
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
155
    typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap;
156
    ///Instantiates a \c DistMap.
155 157

	
156
    ///This function instantiates a DistMap.
158
    ///This function instantiates a \ref DistMap.
157 159
    ///\param g is the digraph, to which we would like to define
158
    ///the DistMap
160
    ///the \ref DistMap.
159 161
    static DistMap *createDistMap(const Digraph &g)
160 162
    {
161 163
      return new DistMap(g);
162 164
    }
163 165
  };
164 166

	
165 167
  ///%Dijkstra algorithm class.
166 168

	
167 169
  /// \ingroup shortest_path
168 170
  ///This class provides an efficient implementation of the %Dijkstra algorithm.
169 171
  ///
172
  ///The %Dijkstra algorithm solves the single-source shortest path problem
173
  ///when all arc lengths are non-negative. If there are negative lengths,
174
  ///the BellmanFord algorithm should be used instead.
175
  ///
170 176
  ///The arc lengths are passed to the algorithm using a
171 177
  ///\ref concepts::ReadMap "ReadMap",
172 178
  ///so it is easy to change it to any kind of length.
173 179
  ///The type of the length is determined by the
174 180
  ///\ref concepts::ReadMap::Value "Value" of the length map.
175 181
  ///It is also possible to change the underlying priority heap.
176 182
  ///
177 183
  ///There is also a \ref dijkstra() "function-type interface" for the
178 184
  ///%Dijkstra algorithm, which is convenient in the simplier cases and
179 185
  ///it can be used easier.
180 186
  ///
181 187
  ///\tparam GR The type of the digraph the algorithm runs on.
182 188
  ///The default type is \ref ListDigraph.
183
  ///\tparam LM A \ref concepts::ReadMap "readable" arc map that specifies
189
  ///\tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies
184 190
  ///the lengths of the arcs.
185 191
  ///It is read once for each arc, so the map may involve in
186 192
  ///relatively time consuming process to compute the arc lengths if
187 193
  ///it is necessary. The default map type is \ref
188 194
  ///concepts::Digraph::ArcMap "GR::ArcMap<int>".
189 195
#ifdef DOXYGEN
190
  template <typename GR, typename LM, typename TR>
196
  template <typename GR, typename LEN, typename TR>
191 197
#else
192 198
  template <typename GR=ListDigraph,
193
            typename LM=typename GR::template ArcMap<int>,
194
            typename TR=DijkstraDefaultTraits<GR,LM> >
199
            typename LEN=typename GR::template ArcMap<int>,
200
            typename TR=DijkstraDefaultTraits<GR,LEN> >
195 201
#endif
196 202
  class Dijkstra {
197 203
  public:
198 204

	
199 205
    ///The type of the digraph the algorithm runs on.
200 206
    typedef typename TR::Digraph Digraph;
201 207

	
202
    ///The type of the length of the arcs.
208
    ///The type of the arc lengths.
203 209
    typedef typename TR::LengthMap::Value Value;
204 210
    ///The type of the map that stores the arc lengths.
205 211
    typedef typename TR::LengthMap LengthMap;
206 212
    ///\brief The type of the map that stores the predecessor arcs of the
207 213
    ///shortest paths.
208 214
    typedef typename TR::PredMap PredMap;
209 215
    ///The type of the map that stores the distances of the nodes.
210 216
    typedef typename TR::DistMap DistMap;
211 217
    ///The type of the map that indicates which nodes are processed.
212 218
    typedef typename TR::ProcessedMap ProcessedMap;
213 219
    ///The type of the paths.
214 220
    typedef PredMapPath<Digraph, PredMap> Path;
215 221
    ///The cross reference type used for the current heap.
216 222
    typedef typename TR::HeapCrossRef HeapCrossRef;
217 223
    ///The heap type used by the algorithm.
218 224
    typedef typename TR::Heap Heap;
219
    ///The operation traits class.
225
    ///\brief The \ref DijkstraDefaultOperationTraits "operation traits class"
226
    ///of the algorithm.
220 227
    typedef typename TR::OperationTraits OperationTraits;
221 228

	
222 229
    ///The \ref DijkstraDefaultTraits "traits class" of the algorithm.
223 230
    typedef TR Traits;
224 231

	
225 232
  private:
226 233

	
227 234
    typedef typename Digraph::Node Node;
228 235
    typedef typename Digraph::NodeIt NodeIt;
229 236
    typedef typename Digraph::Arc Arc;
230 237
    typedef typename Digraph::OutArcIt OutArcIt;
231 238

	
232 239
    //Pointer to the underlying digraph.
233 240
    const Digraph *G;
234 241
    //Pointer to the length map.
235
    const LengthMap *length;
242
    const LengthMap *_length;
236 243
    //Pointer to the map of predecessors arcs.
237 244
    PredMap *_pred;
238 245
    //Indicates if _pred is locally allocated (true) or not.
239 246
    bool local_pred;
240 247
    //Pointer to the map of distances.
241 248
    DistMap *_dist;
242 249
    //Indicates if _dist is locally allocated (true) or not.
243 250
    bool local_dist;
244 251
    //Pointer to the map of processed status of the nodes.
245 252
    ProcessedMap *_processed;
246 253
    //Indicates if _processed is locally allocated (true) or not.
247 254
    bool local_processed;
248 255
    //Pointer to the heap cross references.
249 256
    HeapCrossRef *_heap_cross_ref;
250 257
    //Indicates if _heap_cross_ref is locally allocated (true) or not.
251 258
    bool local_heap_cross_ref;
252 259
    //Pointer to the heap.
253 260
    Heap *_heap;
254 261
    //Indicates if _heap is locally allocated (true) or not.
255 262
    bool local_heap;
256 263

	
257 264
    //Creates the maps if necessary.
258 265
    void create_maps()
259 266
    {
260 267
      if(!_pred) {
261 268
        local_pred = true;
262 269
        _pred = Traits::createPredMap(*G);
263 270
      }
264 271
      if(!_dist) {
265 272
        local_dist = true;
266 273
        _dist = Traits::createDistMap(*G);
267 274
      }
268 275
      if(!_processed) {
269 276
        local_processed = true;
270 277
        _processed = Traits::createProcessedMap(*G);
271 278
      }
272 279
      if (!_heap_cross_ref) {
273 280
        local_heap_cross_ref = true;
274 281
        _heap_cross_ref = Traits::createHeapCrossRef(*G);
275 282
      }
276 283
      if (!_heap) {
277 284
        local_heap = true;
278 285
        _heap = Traits::createHeap(*_heap_cross_ref);
279 286
      }
280 287
    }
281 288

	
282 289
  public:
283 290

	
284 291
    typedef Dijkstra Create;
285 292

	
286
    ///\name Named template parameters
293
    ///\name Named Template Parameters
287 294

	
288 295
    ///@{
289 296

	
290 297
    template <class T>
291 298
    struct SetPredMapTraits : public Traits {
292 299
      typedef T PredMap;
293 300
      static PredMap *createPredMap(const Digraph &)
294 301
      {
295 302
        LEMON_ASSERT(false, "PredMap is not initialized");
296 303
        return 0; // ignore warnings
297 304
      }
298 305
    };
299 306
    ///\brief \ref named-templ-param "Named parameter" for setting
300
    ///PredMap type.
307
    ///\c PredMap type.
301 308
    ///
302 309
    ///\ref named-templ-param "Named parameter" for setting
303
    ///PredMap type.
304
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
310
    ///\c PredMap type.
311
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
305 312
    template <class T>
306 313
    struct SetPredMap
307 314
      : public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > {
308 315
      typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create;
309 316
    };
310 317

	
311 318
    template <class T>
312 319
    struct SetDistMapTraits : public Traits {
313 320
      typedef T DistMap;
314 321
      static DistMap *createDistMap(const Digraph &)
315 322
      {
316 323
        LEMON_ASSERT(false, "DistMap is not initialized");
317 324
        return 0; // ignore warnings
318 325
      }
319 326
    };
320 327
    ///\brief \ref named-templ-param "Named parameter" for setting
321
    ///DistMap type.
328
    ///\c DistMap type.
322 329
    ///
323 330
    ///\ref named-templ-param "Named parameter" for setting
324
    ///DistMap type.
325
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
331
    ///\c DistMap type.
332
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
326 333
    template <class T>
327 334
    struct SetDistMap
328 335
      : public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > {
329 336
      typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create;
330 337
    };
331 338

	
332 339
    template <class T>
333 340
    struct SetProcessedMapTraits : public Traits {
334 341
      typedef T ProcessedMap;
335 342
      static ProcessedMap *createProcessedMap(const Digraph &)
336 343
      {
337 344
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
338 345
        return 0; // ignore warnings
339 346
      }
340 347
    };
341 348
    ///\brief \ref named-templ-param "Named parameter" for setting
342
    ///ProcessedMap type.
349
    ///\c ProcessedMap type.
343 350
    ///
344 351
    ///\ref named-templ-param "Named parameter" for setting
345
    ///ProcessedMap type.
346
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
352
    ///\c ProcessedMap type.
353
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
347 354
    template <class T>
348 355
    struct SetProcessedMap
349 356
      : public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > {
350 357
      typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create;
351 358
    };
352 359

	
353 360
    struct SetStandardProcessedMapTraits : public Traits {
354 361
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
355 362
      static ProcessedMap *createProcessedMap(const Digraph &g)
356 363
      {
357 364
        return new ProcessedMap(g);
358 365
      }
359 366
    };
360 367
    ///\brief \ref named-templ-param "Named parameter" for setting
361
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
368
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
362 369
    ///
363 370
    ///\ref named-templ-param "Named parameter" for setting
364
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
371
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
365 372
    ///If you don't set it explicitly, it will be automatically allocated.
366 373
    struct SetStandardProcessedMap
367 374
      : public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > {
368 375
      typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits >
369 376
      Create;
370 377
    };
371 378

	
372 379
    template <class H, class CR>
373 380
    struct SetHeapTraits : public Traits {
374 381
      typedef CR HeapCrossRef;
375 382
      typedef H Heap;
376 383
      static HeapCrossRef *createHeapCrossRef(const Digraph &) {
377 384
        LEMON_ASSERT(false, "HeapCrossRef is not initialized");
378 385
        return 0; // ignore warnings
379 386
      }
380 387
      static Heap *createHeap(HeapCrossRef &)
381 388
      {
382 389
        LEMON_ASSERT(false, "Heap is not initialized");
383 390
        return 0; // ignore warnings
384 391
      }
385 392
    };
386 393
    ///\brief \ref named-templ-param "Named parameter" for setting
387 394
    ///heap and cross reference types
388 395
    ///
389 396
    ///\ref named-templ-param "Named parameter" for setting heap and cross
390 397
    ///reference types. If this named parameter is used, then external
391 398
    ///heap and cross reference objects must be passed to the algorithm
392 399
    ///using the \ref heap() function before calling \ref run(Node) "run()"
393 400
    ///or \ref init().
394 401
    ///\sa SetStandardHeap
395 402
    template <class H, class CR = typename Digraph::template NodeMap<int> >
396 403
    struct SetHeap
397 404
      : public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > {
398 405
      typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create;
399 406
    };
400 407

	
401 408
    template <class H, class CR>
402 409
    struct SetStandardHeapTraits : public Traits {
403 410
      typedef CR HeapCrossRef;
404 411
      typedef H Heap;
405 412
      static HeapCrossRef *createHeapCrossRef(const Digraph &G) {
406 413
        return new HeapCrossRef(G);
407 414
      }
408 415
      static Heap *createHeap(HeapCrossRef &R)
409 416
      {
410 417
        return new Heap(R);
411 418
      }
412 419
    };
413 420
    ///\brief \ref named-templ-param "Named parameter" for setting
414 421
    ///heap and cross reference types with automatic allocation
415 422
    ///
416 423
    ///\ref named-templ-param "Named parameter" for setting heap and cross
417 424
    ///reference types with automatic allocation.
418 425
    ///They should have standard constructor interfaces to be able to
419 426
    ///automatically created by the algorithm (i.e. the digraph should be
420 427
    ///passed to the constructor of the cross reference and the cross
421 428
    ///reference should be passed to the constructor of the heap).
422 429
    ///However external heap and cross reference objects could also be
423 430
    ///passed to the algorithm using the \ref heap() function before
424 431
    ///calling \ref run(Node) "run()" or \ref init().
425 432
    ///\sa SetHeap
426 433
    template <class H, class CR = typename Digraph::template NodeMap<int> >
427 434
    struct SetStandardHeap
428 435
      : public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > {
429 436
      typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> >
430 437
      Create;
431 438
    };
432 439

	
433 440
    template <class T>
434 441
    struct SetOperationTraitsTraits : public Traits {
435 442
      typedef T OperationTraits;
436 443
    };
437 444

	
438 445
    /// \brief \ref named-templ-param "Named parameter" for setting
439 446
    ///\c OperationTraits type
440 447
    ///
441 448
    ///\ref named-templ-param "Named parameter" for setting
442
    ///\ref OperationTraits type.
449
    ///\c OperationTraits type.
450
    /// For more information see \ref DijkstraDefaultOperationTraits.
443 451
    template <class T>
444 452
    struct SetOperationTraits
445 453
      : public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > {
446 454
      typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> >
447 455
      Create;
448 456
    };
449 457

	
450 458
    ///@}
451 459

	
452 460
  protected:
453 461

	
454 462
    Dijkstra() {}
455 463

	
456 464
  public:
457 465

	
458 466
    ///Constructor.
459 467

	
460 468
    ///Constructor.
461
    ///\param _g The digraph the algorithm runs on.
462
    ///\param _length The length map used by the algorithm.
463
    Dijkstra(const Digraph& _g, const LengthMap& _length) :
464
      G(&_g), length(&_length),
469
    ///\param g The digraph the algorithm runs on.
470
    ///\param length The length map used by the algorithm.
471
    Dijkstra(const Digraph& g, const LengthMap& length) :
472
      G(&g), _length(&length),
465 473
      _pred(NULL), local_pred(false),
466 474
      _dist(NULL), local_dist(false),
467 475
      _processed(NULL), local_processed(false),
468 476
      _heap_cross_ref(NULL), local_heap_cross_ref(false),
469 477
      _heap(NULL), local_heap(false)
470 478
    { }
471 479

	
472 480
    ///Destructor.
473 481
    ~Dijkstra()
474 482
    {
475 483
      if(local_pred) delete _pred;
476 484
      if(local_dist) delete _dist;
477 485
      if(local_processed) delete _processed;
478 486
      if(local_heap_cross_ref) delete _heap_cross_ref;
479 487
      if(local_heap) delete _heap;
480 488
    }
481 489

	
482 490
    ///Sets the length map.
483 491

	
484 492
    ///Sets the length map.
485 493
    ///\return <tt> (*this) </tt>
486 494
    Dijkstra &lengthMap(const LengthMap &m)
487 495
    {
488
      length = &m;
496
      _length = &m;
489 497
      return *this;
490 498
    }
491 499

	
492 500
    ///Sets the map that stores the predecessor arcs.
493 501

	
494 502
    ///Sets the map that stores the predecessor arcs.
495 503
    ///If you don't use this function before calling \ref run(Node) "run()"
496 504
    ///or \ref init(), an instance will be allocated automatically.
497 505
    ///The destructor deallocates this automatically allocated map,
498 506
    ///of course.
499 507
    ///\return <tt> (*this) </tt>
500 508
    Dijkstra &predMap(PredMap &m)
501 509
    {
502 510
      if(local_pred) {
503 511
        delete _pred;
504 512
        local_pred=false;
505 513
      }
506 514
      _pred = &m;
507 515
      return *this;
508 516
    }
509 517

	
510 518
    ///Sets the map that indicates which nodes are processed.
511 519

	
512 520
    ///Sets the map that indicates which nodes are processed.
513 521
    ///If you don't use this function before calling \ref run(Node) "run()"
514 522
    ///or \ref init(), an instance will be allocated automatically.
515 523
    ///The destructor deallocates this automatically allocated map,
516 524
    ///of course.
517 525
    ///\return <tt> (*this) </tt>
518 526
    Dijkstra &processedMap(ProcessedMap &m)
519 527
    {
520 528
      if(local_processed) {
521 529
        delete _processed;
522 530
        local_processed=false;
523 531
      }
524 532
      _processed = &m;
525 533
      return *this;
526 534
    }
527 535

	
528 536
    ///Sets the map that stores the distances of the nodes.
529 537

	
530 538
    ///Sets the map that stores the distances of the nodes calculated by the
531 539
    ///algorithm.
532 540
    ///If you don't use this function before calling \ref run(Node) "run()"
533 541
    ///or \ref init(), an instance will be allocated automatically.
534 542
    ///The destructor deallocates this automatically allocated map,
535 543
    ///of course.
536 544
    ///\return <tt> (*this) </tt>
537 545
    Dijkstra &distMap(DistMap &m)
538 546
    {
539 547
      if(local_dist) {
540 548
        delete _dist;
541 549
        local_dist=false;
542 550
      }
543 551
      _dist = &m;
544 552
      return *this;
545 553
    }
546 554

	
547 555
    ///Sets the heap and the cross reference used by algorithm.
548 556

	
549 557
    ///Sets the heap and the cross reference used by algorithm.
550 558
    ///If you don't use this function before calling \ref run(Node) "run()"
551 559
    ///or \ref init(), heap and cross reference instances will be
552 560
    ///allocated automatically.
553 561
    ///The destructor deallocates these automatically allocated objects,
554 562
    ///of course.
555 563
    ///\return <tt> (*this) </tt>
556 564
    Dijkstra &heap(Heap& hp, HeapCrossRef &cr)
557 565
    {
558 566
      if(local_heap_cross_ref) {
559 567
        delete _heap_cross_ref;
560 568
        local_heap_cross_ref=false;
561 569
      }
562 570
      _heap_cross_ref = &cr;
563 571
      if(local_heap) {
564 572
        delete _heap;
565 573
        local_heap=false;
566 574
      }
567 575
      _heap = &hp;
568 576
      return *this;
569 577
    }
570 578

	
571 579
  private:
572 580

	
573 581
    void finalizeNodeData(Node v,Value dst)
574 582
    {
575 583
      _processed->set(v,true);
576 584
      _dist->set(v, dst);
577 585
    }
578 586

	
579 587
  public:
580 588

	
581 589
    ///\name Execution Control
582 590
    ///The simplest way to execute the %Dijkstra algorithm is to use
583 591
    ///one of the member functions called \ref run(Node) "run()".\n
584
    ///If you need more control on the execution, first you have to call
585
    ///\ref init(), then you can add several source nodes with
592
    ///If you need better control on the execution, you have to call
593
    ///\ref init() first, then you can add several source nodes with
586 594
    ///\ref addSource(). Finally the actual path computation can be
587 595
    ///performed with one of the \ref start() functions.
588 596

	
589 597
    ///@{
590 598

	
591 599
    ///\brief Initializes the internal data structures.
592 600
    ///
593 601
    ///Initializes the internal data structures.
594 602
    void init()
595 603
    {
596 604
      create_maps();
597 605
      _heap->clear();
598 606
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
599 607
        _pred->set(u,INVALID);
600 608
        _processed->set(u,false);
601 609
        _heap_cross_ref->set(u,Heap::PRE_HEAP);
602 610
      }
603 611
    }
604 612

	
605 613
    ///Adds a new source node.
606 614

	
607 615
    ///Adds a new source node to the priority heap.
608 616
    ///The optional second parameter is the initial distance of the node.
609 617
    ///
610 618
    ///The function checks if the node has already been added to the heap and
611 619
    ///it is pushed to the heap only if either it was not in the heap
612 620
    ///or the shortest path found till then is shorter than \c dst.
613 621
    void addSource(Node s,Value dst=OperationTraits::zero())
614 622
    {
615 623
      if(_heap->state(s) != Heap::IN_HEAP) {
616 624
        _heap->push(s,dst);
617 625
      } else if(OperationTraits::less((*_heap)[s], dst)) {
618 626
        _heap->set(s,dst);
619 627
        _pred->set(s,INVALID);
620 628
      }
621 629
    }
622 630

	
623 631
    ///Processes the next node in the priority heap
624 632

	
625 633
    ///Processes the next node in the priority heap.
626 634
    ///
627 635
    ///\return The processed node.
628 636
    ///
629 637
    ///\warning The priority heap must not be empty.
630 638
    Node processNextNode()
631 639
    {
632 640
      Node v=_heap->top();
633 641
      Value oldvalue=_heap->prio();
634 642
      _heap->pop();
635 643
      finalizeNodeData(v,oldvalue);
636 644

	
637 645
      for(OutArcIt e(*G,v); e!=INVALID; ++e) {
638 646
        Node w=G->target(e);
639 647
        switch(_heap->state(w)) {
640 648
        case Heap::PRE_HEAP:
641
          _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e]));
649
          _heap->push(w,OperationTraits::plus(oldvalue, (*_length)[e]));
642 650
          _pred->set(w,e);
643 651
          break;
644 652
        case Heap::IN_HEAP:
645 653
          {
646
            Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]);
654
            Value newvalue = OperationTraits::plus(oldvalue, (*_length)[e]);
647 655
            if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
648 656
              _heap->decrease(w, newvalue);
649 657
              _pred->set(w,e);
650 658
            }
651 659
          }
652 660
          break;
653 661
        case Heap::POST_HEAP:
654 662
          break;
655 663
        }
656 664
      }
657 665
      return v;
658 666
    }
659 667

	
660 668
    ///The next node to be processed.
661 669

	
662 670
    ///Returns the next node to be processed or \c INVALID if the
663 671
    ///priority heap is empty.
664 672
    Node nextNode() const
665 673
    {
666 674
      return !_heap->empty()?_heap->top():INVALID;
667 675
    }
668 676

	
669 677
    ///Returns \c false if there are nodes to be processed.
670 678

	
671 679
    ///Returns \c false if there are nodes to be processed
672 680
    ///in the priority heap.
673 681
    bool emptyQueue() const { return _heap->empty(); }
674 682

	
675 683
    ///Returns the number of the nodes to be processed.
676 684

	
677 685
    ///Returns the number of the nodes to be processed
678 686
    ///in the priority heap.
679 687
    int queueSize() const { return _heap->size(); }
680 688

	
681 689
    ///Executes the algorithm.
682 690

	
683 691
    ///Executes the algorithm.
684 692
    ///
685 693
    ///This method runs the %Dijkstra algorithm from the root node(s)
686 694
    ///in order to compute the shortest path to each node.
687 695
    ///
688 696
    ///The algorithm computes
689 697
    ///- the shortest path tree (forest),
690 698
    ///- the distance of each node from the root(s).
691 699
    ///
692 700
    ///\pre init() must be called and at least one root node should be
693 701
    ///added with addSource() before using this function.
694 702
    ///
695 703
    ///\note <tt>d.start()</tt> is just a shortcut of the following code.
696 704
    ///\code
697 705
    ///  while ( !d.emptyQueue() ) {
698 706
    ///    d.processNextNode();
699 707
    ///  }
700 708
    ///\endcode
701 709
    void start()
702 710
    {
703 711
      while ( !emptyQueue() ) processNextNode();
704 712
    }
705 713

	
706 714
    ///Executes the algorithm until the given target node is processed.
707 715

	
708 716
    ///Executes the algorithm until the given target node is processed.
709 717
    ///
710 718
    ///This method runs the %Dijkstra algorithm from the root node(s)
711 719
    ///in order to compute the shortest path to \c t.
712 720
    ///
713 721
    ///The algorithm computes
714 722
    ///- the shortest path to \c t,
715 723
    ///- the distance of \c t from the root(s).
716 724
    ///
717 725
    ///\pre init() must be called and at least one root node should be
718 726
    ///added with addSource() before using this function.
719 727
    void start(Node t)
720 728
    {
721 729
      while ( !_heap->empty() && _heap->top()!=t ) processNextNode();
722 730
      if ( !_heap->empty() ) {
723 731
        finalizeNodeData(_heap->top(),_heap->prio());
724 732
        _heap->pop();
725 733
      }
726 734
    }
727 735

	
728 736
    ///Executes the algorithm until a condition is met.
729 737

	
730 738
    ///Executes the algorithm until a condition is met.
731 739
    ///
732 740
    ///This method runs the %Dijkstra algorithm from the root node(s) in
733 741
    ///order to compute the shortest path to a node \c v with
734 742
    /// <tt>nm[v]</tt> true, if such a node can be found.
735 743
    ///
736 744
    ///\param nm A \c bool (or convertible) node map. The algorithm
737 745
    ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
738 746
    ///
739 747
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
740 748
    ///\c INVALID if no such node was found.
741 749
    ///
742 750
    ///\pre init() must be called and at least one root node should be
743 751
    ///added with addSource() before using this function.
744 752
    template<class NodeBoolMap>
745 753
    Node start(const NodeBoolMap &nm)
746 754
    {
747 755
      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
748 756
      if ( _heap->empty() ) return INVALID;
749 757
      finalizeNodeData(_heap->top(),_heap->prio());
750 758
      return _heap->top();
751 759
    }
752 760

	
753 761
    ///Runs the algorithm from the given source node.
754 762

	
755 763
    ///This method runs the %Dijkstra algorithm from node \c s
756 764
    ///in order to compute the shortest path to each node.
757 765
    ///
758 766
    ///The algorithm computes
759 767
    ///- the shortest path tree,
760 768
    ///- the distance of each node from the root.
761 769
    ///
762 770
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
763 771
    ///\code
764 772
    ///  d.init();
765 773
    ///  d.addSource(s);
766 774
    ///  d.start();
767 775
    ///\endcode
768 776
    void run(Node s) {
769 777
      init();
770 778
      addSource(s);
771 779
      start();
772 780
    }
773 781

	
774 782
    ///Finds the shortest path between \c s and \c t.
775 783

	
776 784
    ///This method runs the %Dijkstra algorithm from node \c s
777 785
    ///in order to compute the shortest path to node \c t
778 786
    ///(it stops searching when \c t is processed).
779 787
    ///
780 788
    ///\return \c true if \c t is reachable form \c s.
781 789
    ///
782 790
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a
783 791
    ///shortcut of the following code.
784 792
    ///\code
785 793
    ///  d.init();
786 794
    ///  d.addSource(s);
787 795
    ///  d.start(t);
788 796
    ///\endcode
789 797
    bool run(Node s,Node t) {
790 798
      init();
791 799
      addSource(s);
792 800
      start(t);
793 801
      return (*_heap_cross_ref)[t] == Heap::POST_HEAP;
794 802
    }
795 803

	
796 804
    ///@}
797 805

	
798 806
    ///\name Query Functions
799 807
    ///The results of the %Dijkstra algorithm can be obtained using these
800 808
    ///functions.\n
801
    ///Either \ref run(Node) "run()" or \ref start() should be called
809
    ///Either \ref run(Node) "run()" or \ref init() should be called
802 810
    ///before using them.
803 811

	
804 812
    ///@{
805 813

	
806
    ///The shortest path to a node.
814
    ///The shortest path to the given node.
807 815

	
808
    ///Returns the shortest path to a node.
816
    ///Returns the shortest path to the given node from the root(s).
809 817
    ///
810 818
    ///\warning \c t should be reached from the root(s).
811 819
    ///
812 820
    ///\pre Either \ref run(Node) "run()" or \ref init()
813 821
    ///must be called before using this function.
814 822
    Path path(Node t) const { return Path(*G, *_pred, t); }
815 823

	
816
    ///The distance of a node from the root(s).
824
    ///The distance of the given node from the root(s).
817 825

	
818
    ///Returns the distance of a node from the root(s).
826
    ///Returns the distance of the given node from the root(s).
819 827
    ///
820 828
    ///\warning If node \c v is not reached from the root(s), then
821 829
    ///the return value of this function is undefined.
822 830
    ///
823 831
    ///\pre Either \ref run(Node) "run()" or \ref init()
824 832
    ///must be called before using this function.
825 833
    Value dist(Node v) const { return (*_dist)[v]; }
826 834

	
827
    ///Returns the 'previous arc' of the shortest path tree for a node.
828

	
835
    ///\brief Returns the 'previous arc' of the shortest path tree for
836
    ///the given node.
837
    ///
829 838
    ///This function returns the 'previous arc' of the shortest path
830 839
    ///tree for the node \c v, i.e. it returns the last arc of a
831 840
    ///shortest path from a root to \c v. It is \c INVALID if \c v
832 841
    ///is not reached from the root(s) or if \c v is a root.
833 842
    ///
834 843
    ///The shortest path tree used here is equal to the shortest path
835
    ///tree used in \ref predNode().
844
    ///tree used in \ref predNode() and \ref predMap().
836 845
    ///
837 846
    ///\pre Either \ref run(Node) "run()" or \ref init()
838 847
    ///must be called before using this function.
839 848
    Arc predArc(Node v) const { return (*_pred)[v]; }
840 849

	
841
    ///Returns the 'previous node' of the shortest path tree for a node.
842

	
850
    ///\brief Returns the 'previous node' of the shortest path tree for
851
    ///the given node.
852
    ///
843 853
    ///This function returns the 'previous node' of the shortest path
844 854
    ///tree for the node \c v, i.e. it returns the last but one node
845
    ///from a shortest path from a root to \c v. It is \c INVALID
855
    ///of a shortest path from a root to \c v. It is \c INVALID
846 856
    ///if \c v is not reached from the root(s) or if \c v is a root.
847 857
    ///
848 858
    ///The shortest path tree used here is equal to the shortest path
849
    ///tree used in \ref predArc().
859
    ///tree used in \ref predArc() and \ref predMap().
850 860
    ///
851 861
    ///\pre Either \ref run(Node) "run()" or \ref init()
852 862
    ///must be called before using this function.
853 863
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
854 864
                                  G->source((*_pred)[v]); }
855 865

	
856 866
    ///\brief Returns a const reference to the node map that stores the
857 867
    ///distances of the nodes.
858 868
    ///
859 869
    ///Returns a const reference to the node map that stores the distances
860 870
    ///of the nodes calculated by the algorithm.
861 871
    ///
862 872
    ///\pre Either \ref run(Node) "run()" or \ref init()
863 873
    ///must be called before using this function.
864 874
    const DistMap &distMap() const { return *_dist;}
865 875

	
866 876
    ///\brief Returns a const reference to the node map that stores the
867 877
    ///predecessor arcs.
868 878
    ///
869 879
    ///Returns a const reference to the node map that stores the predecessor
870
    ///arcs, which form the shortest path tree.
880
    ///arcs, which form the shortest path tree (forest).
871 881
    ///
872 882
    ///\pre Either \ref run(Node) "run()" or \ref init()
873 883
    ///must be called before using this function.
874 884
    const PredMap &predMap() const { return *_pred;}
875 885

	
876
    ///Checks if a node is reached from the root(s).
886
    ///Checks if the given node is reached from the root(s).
877 887

	
878 888
    ///Returns \c true if \c v is reached from the root(s).
879 889
    ///
880 890
    ///\pre Either \ref run(Node) "run()" or \ref init()
881 891
    ///must be called before using this function.
882 892
    bool reached(Node v) const { return (*_heap_cross_ref)[v] !=
883 893
                                        Heap::PRE_HEAP; }
884 894

	
885 895
    ///Checks if a node is processed.
886 896

	
887 897
    ///Returns \c true if \c v is processed, i.e. the shortest
888 898
    ///path to \c v has already found.
889 899
    ///
890 900
    ///\pre Either \ref run(Node) "run()" or \ref init()
891 901
    ///must be called before using this function.
892 902
    bool processed(Node v) const { return (*_heap_cross_ref)[v] ==
893 903
                                          Heap::POST_HEAP; }
894 904

	
895
    ///The current distance of a node from the root(s).
905
    ///The current distance of the given node from the root(s).
896 906

	
897
    ///Returns the current distance of a node from the root(s).
907
    ///Returns the current distance of the given node from the root(s).
898 908
    ///It may be decreased in the following processes.
899 909
    ///
900 910
    ///\pre Either \ref run(Node) "run()" or \ref init()
901 911
    ///must be called before using this function and
902 912
    ///node \c v must be reached but not necessarily processed.
903 913
    Value currentDist(Node v) const {
904 914
      return processed(v) ? (*_dist)[v] : (*_heap)[v];
905 915
    }
906 916

	
907 917
    ///@}
908 918
  };
909 919

	
910 920

	
911 921
  ///Default traits class of dijkstra() function.
912 922

	
913 923
  ///Default traits class of dijkstra() function.
914 924
  ///\tparam GR The type of the digraph.
915
  ///\tparam LM The type of the length map.
916
  template<class GR, class LM>
925
  ///\tparam LEN The type of the length map.
926
  template<class GR, class LEN>
917 927
  struct DijkstraWizardDefaultTraits
918 928
  {
919 929
    ///The type of the digraph the algorithm runs on.
920 930
    typedef GR Digraph;
921 931
    ///The type of the map that stores the arc lengths.
922 932

	
923 933
    ///The type of the map that stores the arc lengths.
924
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
925
    typedef LM LengthMap;
926
    ///The type of the length of the arcs.
927
    typedef typename LM::Value Value;
934
    ///It must conform to the \ref concepts::ReadMap "ReadMap" concept.
935
    typedef LEN LengthMap;
936
    ///The type of the arc lengths.
937
    typedef typename LEN::Value Value;
928 938

	
929 939
    /// Operation traits for Dijkstra algorithm.
930 940

	
931 941
    /// This class defines the operations that are used in the algorithm.
932 942
    /// \see DijkstraDefaultOperationTraits
933 943
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
934 944

	
935 945
    /// The cross reference type used by the heap.
936 946

	
937 947
    /// The cross reference type used by the heap.
938 948
    /// Usually it is \c Digraph::NodeMap<int>.
939 949
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
940 950
    ///Instantiates a \ref HeapCrossRef.
941 951

	
942 952
    ///This function instantiates a \ref HeapCrossRef.
943 953
    /// \param g is the digraph, to which we would like to define the
944 954
    /// HeapCrossRef.
945 955
    static HeapCrossRef *createHeapCrossRef(const Digraph &g)
946 956
    {
947 957
      return new HeapCrossRef(g);
948 958
    }
949 959

	
950 960
    ///The heap type used by the Dijkstra algorithm.
951 961

	
952 962
    ///The heap type used by the Dijkstra algorithm.
953 963
    ///
954 964
    ///\sa BinHeap
955 965
    ///\sa Dijkstra
956 966
    typedef BinHeap<Value, typename Digraph::template NodeMap<int>,
957 967
                    std::less<Value> > Heap;
958 968

	
959 969
    ///Instantiates a \ref Heap.
960 970

	
961 971
    ///This function instantiates a \ref Heap.
962 972
    /// \param r is the HeapCrossRef which is used.
963 973
    static Heap *createHeap(HeapCrossRef& r)
964 974
    {
965 975
      return new Heap(r);
966 976
    }
967 977

	
968 978
    ///\brief The type of the map that stores the predecessor
969 979
    ///arcs of the shortest paths.
970 980
    ///
971 981
    ///The type of the map that stores the predecessor
972 982
    ///arcs of the shortest paths.
973
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
983
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
974 984
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
975 985
    ///Instantiates a PredMap.
976 986

	
977 987
    ///This function instantiates a PredMap.
978 988
    ///\param g is the digraph, to which we would like to define the
979 989
    ///PredMap.
980 990
    static PredMap *createPredMap(const Digraph &g)
981 991
    {
982 992
      return new PredMap(g);
983 993
    }
984 994

	
985 995
    ///The type of the map that indicates which nodes are processed.
986 996

	
987 997
    ///The type of the map that indicates which nodes are processed.
988
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
998
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
989 999
    ///By default it is a NullMap.
990 1000
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
991 1001
    ///Instantiates a ProcessedMap.
992 1002

	
993 1003
    ///This function instantiates a ProcessedMap.
994 1004
    ///\param g is the digraph, to which
995 1005
    ///we would like to define the ProcessedMap.
996 1006
#ifdef DOXYGEN
997 1007
    static ProcessedMap *createProcessedMap(const Digraph &g)
998 1008
#else
999 1009
    static ProcessedMap *createProcessedMap(const Digraph &)
1000 1010
#endif
1001 1011
    {
1002 1012
      return new ProcessedMap();
1003 1013
    }
1004 1014

	
1005 1015
    ///The type of the map that stores the distances of the nodes.
1006 1016

	
1007 1017
    ///The type of the map that stores the distances of the nodes.
1008
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
1009
    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
1018
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
1019
    typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap;
1010 1020
    ///Instantiates a DistMap.
1011 1021

	
1012 1022
    ///This function instantiates a DistMap.
1013 1023
    ///\param g is the digraph, to which we would like to define
1014 1024
    ///the DistMap
1015 1025
    static DistMap *createDistMap(const Digraph &g)
1016 1026
    {
1017 1027
      return new DistMap(g);
1018 1028
    }
1019 1029

	
1020 1030
    ///The type of the shortest paths.
1021 1031

	
1022 1032
    ///The type of the shortest paths.
1023
    ///It must meet the \ref concepts::Path "Path" concept.
1033
    ///It must conform to the \ref concepts::Path "Path" concept.
1024 1034
    typedef lemon::Path<Digraph> Path;
1025 1035
  };
1026 1036

	
1027 1037
  /// Default traits class used by DijkstraWizard
1028 1038

	
1029
  /// To make it easier to use Dijkstra algorithm
1030
  /// we have created a wizard class.
1031
  /// This \ref DijkstraWizard class needs default traits,
1032
  /// as well as the \ref Dijkstra class.
1033
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
1034
  /// \ref DijkstraWizard class.
1035
  template<class GR,class LM>
1036
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
1039
  /// Default traits class used by DijkstraWizard.
1040
  /// \tparam GR The type of the digraph.
1041
  /// \tparam LEN The type of the length map.
1042
  template<typename GR, typename LEN>
1043
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LEN>
1037 1044
  {
1038
    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
1045
    typedef DijkstraWizardDefaultTraits<GR,LEN> Base;
1039 1046
  protected:
1040 1047
    //The type of the nodes in the digraph.
1041 1048
    typedef typename Base::Digraph::Node Node;
1042 1049

	
1043 1050
    //Pointer to the digraph the algorithm runs on.
1044 1051
    void *_g;
1045 1052
    //Pointer to the length map.
1046 1053
    void *_length;
1047 1054
    //Pointer to the map of processed nodes.
1048 1055
    void *_processed;
1049 1056
    //Pointer to the map of predecessors arcs.
1050 1057
    void *_pred;
1051 1058
    //Pointer to the map of distances.
1052 1059
    void *_dist;
1053 1060
    //Pointer to the shortest path to the target node.
1054 1061
    void *_path;
1055 1062
    //Pointer to the distance of the target node.
1056 1063
    void *_di;
1057 1064

	
1058 1065
  public:
1059 1066
    /// Constructor.
1060 1067

	
1061 1068
    /// This constructor does not require parameters, therefore it initiates
1062 1069
    /// all of the attributes to \c 0.
1063 1070
    DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
1064 1071
                           _dist(0), _path(0), _di(0) {}
1065 1072

	
1066 1073
    /// Constructor.
1067 1074

	
1068 1075
    /// This constructor requires two parameters,
1069 1076
    /// others are initiated to \c 0.
1070 1077
    /// \param g The digraph the algorithm runs on.
1071 1078
    /// \param l The length map.
1072
    DijkstraWizardBase(const GR &g,const LM &l) :
1079
    DijkstraWizardBase(const GR &g,const LEN &l) :
1073 1080
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
1074
      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
1081
      _length(reinterpret_cast<void*>(const_cast<LEN*>(&l))),
1075 1082
      _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
1076 1083

	
1077 1084
  };
1078 1085

	
1079 1086
  /// Auxiliary class for the function-type interface of Dijkstra algorithm.
1080 1087

	
1081 1088
  /// This auxiliary class is created to implement the
1082 1089
  /// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm.
1083 1090
  /// It does not have own \ref run(Node) "run()" method, it uses the
1084 1091
  /// functions and features of the plain \ref Dijkstra.
1085 1092
  ///
1086 1093
  /// This class should only be used through the \ref dijkstra() function,
1087 1094
  /// which makes it easier to use the algorithm.
1088 1095
  template<class TR>
1089 1096
  class DijkstraWizard : public TR
1090 1097
  {
1091 1098
    typedef TR Base;
1092 1099

	
1093
    ///The type of the digraph the algorithm runs on.
1094 1100
    typedef typename TR::Digraph Digraph;
1095 1101

	
1096 1102
    typedef typename Digraph::Node Node;
1097 1103
    typedef typename Digraph::NodeIt NodeIt;
1098 1104
    typedef typename Digraph::Arc Arc;
1099 1105
    typedef typename Digraph::OutArcIt OutArcIt;
1100 1106

	
1101
    ///The type of the map that stores the arc lengths.
1102 1107
    typedef typename TR::LengthMap LengthMap;
1103
    ///The type of the length of the arcs.
1104 1108
    typedef typename LengthMap::Value Value;
1105
    ///\brief The type of the map that stores the predecessor
1106
    ///arcs of the shortest paths.
1107 1109
    typedef typename TR::PredMap PredMap;
1108
    ///The type of the map that stores the distances of the nodes.
1109 1110
    typedef typename TR::DistMap DistMap;
1110
    ///The type of the map that indicates which nodes are processed.
1111 1111
    typedef typename TR::ProcessedMap ProcessedMap;
1112
    ///The type of the shortest paths
1113 1112
    typedef typename TR::Path Path;
1114
    ///The heap type used by the dijkstra algorithm.
1115 1113
    typedef typename TR::Heap Heap;
1116 1114

	
1117 1115
  public:
1118 1116

	
1119 1117
    /// Constructor.
1120 1118
    DijkstraWizard() : TR() {}
1121 1119

	
1122 1120
    /// Constructor that requires parameters.
1123 1121

	
1124 1122
    /// Constructor that requires parameters.
1125 1123
    /// These parameters will be the default values for the traits class.
1126 1124
    /// \param g The digraph the algorithm runs on.
1127 1125
    /// \param l The length map.
1128 1126
    DijkstraWizard(const Digraph &g, const LengthMap &l) :
1129 1127
      TR(g,l) {}
1130 1128

	
1131 1129
    ///Copy constructor
1132 1130
    DijkstraWizard(const TR &b) : TR(b) {}
1133 1131

	
1134 1132
    ~DijkstraWizard() {}
1135 1133

	
1136 1134
    ///Runs Dijkstra algorithm from the given source node.
1137 1135

	
1138 1136
    ///This method runs %Dijkstra algorithm from the given source node
1139 1137
    ///in order to compute the shortest path to each node.
1140 1138
    void run(Node s)
1141 1139
    {
1142 1140
      Dijkstra<Digraph,LengthMap,TR>
1143 1141
        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
1144 1142
             *reinterpret_cast<const LengthMap*>(Base::_length));
1145 1143
      if (Base::_pred)
1146 1144
        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1147 1145
      if (Base::_dist)
1148 1146
        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1149 1147
      if (Base::_processed)
1150 1148
        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1151 1149
      dijk.run(s);
1152 1150
    }
1153 1151

	
1154 1152
    ///Finds the shortest path between \c s and \c t.
1155 1153

	
1156 1154
    ///This method runs the %Dijkstra algorithm from node \c s
1157 1155
    ///in order to compute the shortest path to node \c t
1158 1156
    ///(it stops searching when \c t is processed).
1159 1157
    ///
1160 1158
    ///\return \c true if \c t is reachable form \c s.
1161 1159
    bool run(Node s, Node t)
1162 1160
    {
1163 1161
      Dijkstra<Digraph,LengthMap,TR>
1164 1162
        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
1165 1163
             *reinterpret_cast<const LengthMap*>(Base::_length));
1166 1164
      if (Base::_pred)
1167 1165
        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1168 1166
      if (Base::_dist)
1169 1167
        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1170 1168
      if (Base::_processed)
1171 1169
        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1172 1170
      dijk.run(s,t);
1173 1171
      if (Base::_path)
1174 1172
        *reinterpret_cast<Path*>(Base::_path) = dijk.path(t);
1175 1173
      if (Base::_di)
1176 1174
        *reinterpret_cast<Value*>(Base::_di) = dijk.dist(t);
1177 1175
      return dijk.reached(t);
1178 1176
    }
1179 1177

	
1180 1178
    template<class T>
1181 1179
    struct SetPredMapBase : public Base {
1182 1180
      typedef T PredMap;
1183 1181
      static PredMap *createPredMap(const Digraph &) { return 0; };
1184 1182
      SetPredMapBase(const TR &b) : TR(b) {}
1185 1183
    };
1186
    ///\brief \ref named-func-param "Named parameter"
1187
    ///for setting PredMap object.
1184

	
1185
    ///\brief \ref named-templ-param "Named parameter" for setting
1186
    ///the predecessor map.
1188 1187
    ///
1189
    ///\ref named-func-param "Named parameter"
1190
    ///for setting PredMap object.
1188
    ///\ref named-templ-param "Named parameter" function for setting
1189
    ///the map that stores the predecessor arcs of the nodes.
1191 1190
    template<class T>
1192 1191
    DijkstraWizard<SetPredMapBase<T> > predMap(const T &t)
1193 1192
    {
1194 1193
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1195 1194
      return DijkstraWizard<SetPredMapBase<T> >(*this);
1196 1195
    }
1197 1196

	
1198 1197
    template<class T>
1199 1198
    struct SetDistMapBase : public Base {
1200 1199
      typedef T DistMap;
1201 1200
      static DistMap *createDistMap(const Digraph &) { return 0; };
1202 1201
      SetDistMapBase(const TR &b) : TR(b) {}
1203 1202
    };
1204
    ///\brief \ref named-func-param "Named parameter"
1205
    ///for setting DistMap object.
1203

	
1204
    ///\brief \ref named-templ-param "Named parameter" for setting
1205
    ///the distance map.
1206 1206
    ///
1207
    ///\ref named-func-param "Named parameter"
1208
    ///for setting DistMap object.
1207
    ///\ref named-templ-param "Named parameter" function for setting
1208
    ///the map that stores the distances of the nodes calculated
1209
    ///by the algorithm.
1209 1210
    template<class T>
1210 1211
    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
1211 1212
    {
1212 1213
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1213 1214
      return DijkstraWizard<SetDistMapBase<T> >(*this);
1214 1215
    }
1215 1216

	
1216 1217
    template<class T>
1217 1218
    struct SetProcessedMapBase : public Base {
1218 1219
      typedef T ProcessedMap;
1219 1220
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1220 1221
      SetProcessedMapBase(const TR &b) : TR(b) {}
1221 1222
    };
1222
    ///\brief \ref named-func-param "Named parameter"
1223
    ///for setting ProcessedMap object.
1223

	
1224
    ///\brief \ref named-func-param "Named parameter" for setting
1225
    ///the processed map.
1224 1226
    ///
1225
    /// \ref named-func-param "Named parameter"
1226
    ///for setting ProcessedMap object.
1227
    ///\ref named-templ-param "Named parameter" function for setting
1228
    ///the map that indicates which nodes are processed.
1227 1229
    template<class T>
1228 1230
    DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1229 1231
    {
1230 1232
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1231 1233
      return DijkstraWizard<SetProcessedMapBase<T> >(*this);
1232 1234
    }
1233 1235

	
1234 1236
    template<class T>
1235 1237
    struct SetPathBase : public Base {
1236 1238
      typedef T Path;
1237 1239
      SetPathBase(const TR &b) : TR(b) {}
1238 1240
    };
1241

	
1239 1242
    ///\brief \ref named-func-param "Named parameter"
1240 1243
    ///for getting the shortest path to the target node.
1241 1244
    ///
1242 1245
    ///\ref named-func-param "Named parameter"
1243 1246
    ///for getting the shortest path to the target node.
1244 1247
    template<class T>
1245 1248
    DijkstraWizard<SetPathBase<T> > path(const T &t)
1246 1249
    {
1247 1250
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1248 1251
      return DijkstraWizard<SetPathBase<T> >(*this);
1249 1252
    }
1250 1253

	
1251 1254
    ///\brief \ref named-func-param "Named parameter"
1252 1255
    ///for getting the distance of the target node.
1253 1256
    ///
1254 1257
    ///\ref named-func-param "Named parameter"
1255 1258
    ///for getting the distance of the target node.
1256 1259
    DijkstraWizard dist(const Value &d)
1257 1260
    {
1258 1261
      Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
1259 1262
      return *this;
1260 1263
    }
1261 1264

	
1262 1265
  };
1263 1266

	
1264 1267
  ///Function-type interface for Dijkstra algorithm.
1265 1268

	
1266 1269
  /// \ingroup shortest_path
1267 1270
  ///Function-type interface for Dijkstra algorithm.
1268 1271
  ///
1269 1272
  ///This function also has several \ref named-func-param "named parameters",
1270 1273
  ///they are declared as the members of class \ref DijkstraWizard.
1271 1274
  ///The following examples show how to use these parameters.
1272 1275
  ///\code
1273 1276
  ///  // Compute shortest path from node s to each node
1274 1277
  ///  dijkstra(g,length).predMap(preds).distMap(dists).run(s);
1275 1278
  ///
1276 1279
  ///  // Compute shortest path from s to t
1277 1280
  ///  bool reached = dijkstra(g,length).path(p).dist(d).run(s,t);
1278 1281
  ///\endcode
1279 1282
  ///\warning Don't forget to put the \ref DijkstraWizard::run(Node) "run()"
1280 1283
  ///to the end of the parameter list.
1281 1284
  ///\sa DijkstraWizard
1282 1285
  ///\sa Dijkstra
1283
  template<class GR, class LM>
1284
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
1285
  dijkstra(const GR &digraph, const LM &length)
1286
  template<typename GR, typename LEN>
1287
  DijkstraWizard<DijkstraWizardBase<GR,LEN> >
1288
  dijkstra(const GR &digraph, const LEN &length)
1286 1289
  {
1287
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length);
1290
    return DijkstraWizard<DijkstraWizardBase<GR,LEN> >(digraph,length);
1288 1291
  }
1289 1292

	
1290 1293
} //END OF NAMESPACE LEMON
1291 1294

	
1292 1295
#endif
Ignore white space 6 line context
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-2009
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_DIM2_H
20 20
#define LEMON_DIM2_H
21 21

	
22 22
#include <iostream>
23 23

	
24
///\ingroup misc
24
///\ingroup geomdat
25 25
///\file
26 26
///\brief A simple two dimensional vector and a bounding box implementation
27
///
28
/// The class \ref lemon::dim2::Point "dim2::Point" implements
29
/// a two dimensional vector with the usual operations.
30
///
31
/// The class \ref lemon::dim2::Box "dim2::Box" can be used to determine
32
/// the rectangular bounding box of a set of
33
/// \ref lemon::dim2::Point "dim2::Point"'s.
34 27

	
35 28
namespace lemon {
36 29

	
37 30
  ///Tools for handling two dimensional coordinates
38 31

	
39 32
  ///This namespace is a storage of several
40 33
  ///tools for handling two dimensional coordinates
41 34
  namespace dim2 {
42 35

	
43
  /// \addtogroup misc
36
  /// \addtogroup geomdat
44 37
  /// @{
45 38

	
46 39
  /// Two dimensional vector (plain vector)
47 40

	
48 41
  /// A simple two dimensional vector (plain vector) implementation
49 42
  /// with the usual vector operations.
50 43
  template<typename T>
51 44
    class Point {
52 45

	
53 46
    public:
54 47

	
55 48
      typedef T Value;
56 49

	
57 50
      ///First coordinate
58 51
      T x;
59 52
      ///Second coordinate
60 53
      T y;
61 54

	
62 55
      ///Default constructor
63 56
      Point() {}
64 57

	
65 58
      ///Construct an instance from coordinates
66 59
      Point(T a, T b) : x(a), y(b) { }
67 60

	
68 61
      ///Returns the dimension of the vector (i.e. returns 2).
69 62

	
70 63
      ///The dimension of the vector.
71 64
      ///This function always returns 2.
72 65
      int size() const { return 2; }
73 66

	
74 67
      ///Subscripting operator
75 68

	
76 69
      ///\c p[0] is \c p.x and \c p[1] is \c p.y
77 70
      ///
78 71
      T& operator[](int idx) { return idx == 0 ? x : y; }
79 72

	
80 73
      ///Const subscripting operator
81 74

	
82 75
      ///\c p[0] is \c p.x and \c p[1] is \c p.y
83 76
      ///
84 77
      const T& operator[](int idx) const { return idx == 0 ? x : y; }
85 78

	
86 79
      ///Conversion constructor
87 80
      template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
88 81

	
89 82
      ///Give back the square of the norm of the vector
90 83
      T normSquare() const {
91 84
        return x*x+y*y;
92 85
      }
93 86

	
94 87
      ///Increment the left hand side by \c u
95 88
      Point<T>& operator +=(const Point<T>& u) {
96 89
        x += u.x;
97 90
        y += u.y;
98 91
        return *this;
99 92
      }
100 93

	
101 94
      ///Decrement the left hand side by \c u
102 95
      Point<T>& operator -=(const Point<T>& u) {
103 96
        x -= u.x;
104 97
        y -= u.y;
105 98
        return *this;
106 99
      }
107 100

	
108 101
      ///Multiply the left hand side with a scalar
109 102
      Point<T>& operator *=(const T &u) {
110 103
        x *= u;
111 104
        y *= u;
112 105
        return *this;
113 106
      }
114 107

	
115 108
      ///Divide the left hand side by a scalar
116 109
      Point<T>& operator /=(const T &u) {
117 110
        x /= u;
118 111
        y /= u;
119 112
        return *this;
120 113
      }
121 114

	
122 115
      ///Return the scalar product of two vectors
123 116
      T operator *(const Point<T>& u) const {
124 117
        return x*u.x+y*u.y;
125 118
      }
126 119

	
127 120
      ///Return the sum of two vectors
128 121
      Point<T> operator+(const Point<T> &u) const {
129 122
        Point<T> b=*this;
130 123
        return b+=u;
131 124
      }
132 125

	
133 126
      ///Return the negative of the vector
134 127
      Point<T> operator-() const {
135 128
        Point<T> b=*this;
136 129
        b.x=-b.x; b.y=-b.y;
137 130
        return b;
138 131
      }
139 132

	
Ignore white space 6 line context
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-2009
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_DIMACS_H
20 20
#define LEMON_DIMACS_H
21 21

	
22 22
#include <iostream>
23 23
#include <string>
24 24
#include <vector>
25
#include <limits>
25 26
#include <lemon/maps.h>
26 27
#include <lemon/error.h>
27

	
28 28
/// \ingroup dimacs_group
29 29
/// \file
30 30
/// \brief DIMACS file format reader.
31 31

	
32 32
namespace lemon {
33 33

	
34 34
  /// \addtogroup dimacs_group
35 35
  /// @{
36 36

	
37 37
  /// DIMACS file type descriptor.
38 38
  struct DimacsDescriptor
39 39
  {
40
    ///File type enum
41
    enum Type
42
      {
43
        NONE, MIN, MAX, SP, MAT
44
      };
40
    ///\brief DIMACS file type enum
41
    ///
42
    ///DIMACS file type enum.
43
    enum Type {
44
      NONE,  ///< Undefined type.
45
      MIN,   ///< DIMACS file type for minimum cost flow problems.
46
      MAX,   ///< DIMACS file type for maximum flow problems.
47
      SP,    ///< DIMACS file type for shostest path problems.
48
      MAT    ///< DIMACS file type for plain graphs and matching problems.
49
    };
45 50
    ///The file type
46 51
    Type type;
47 52
    ///The number of nodes in the graph
48 53
    int nodeNum;
49 54
    ///The number of edges in the graph
50 55
    int edgeNum;
51 56
    int lineShift;
52
    /// Constructor. Sets the type to NONE.
57
    ///Constructor. It sets the type to \c NONE.
53 58
    DimacsDescriptor() : type(NONE) {}
54 59
  };
55 60

	
56 61
  ///Discover the type of a DIMACS file
57 62

	
58
  ///It starts seeking the begining of the file for the problem type
59
  ///and size info. The found data is returned in a special struct
60
  ///that can be evaluated and passed to the appropriate reader
61
  ///function.
63
  ///This function starts seeking the beginning of the given file for the
64
  ///problem type and size info. 
65
  ///The found data is returned in a special struct that can be evaluated
66
  ///and passed to the appropriate reader function.
62 67
  DimacsDescriptor dimacsType(std::istream& is)
63 68
  {
64 69
    DimacsDescriptor r;
65 70
    std::string problem,str;
66 71
    char c;
67 72
    r.lineShift=0;
68 73
    while (is >> c)
69 74
      switch(c)
70 75
        {
71 76
        case 'p':
72 77
          if(is >> problem >> r.nodeNum >> r.edgeNum)
73 78
            {
74 79
              getline(is, str);
75 80
              r.lineShift++;
76 81
              if(problem=="min") r.type=DimacsDescriptor::MIN;
77 82
              else if(problem=="max") r.type=DimacsDescriptor::MAX;
78 83
              else if(problem=="sp") r.type=DimacsDescriptor::SP;
79 84
              else if(problem=="mat") r.type=DimacsDescriptor::MAT;
80 85
              else throw FormatError("Unknown problem type");
81 86
              return r;
82 87
            }
83 88
          else
84 89
            {
85 90
              throw FormatError("Missing or wrong problem type declaration.");
86 91
            }
87 92
          break;
88 93
        case 'c':
89 94
          getline(is, str);
90 95
          r.lineShift++;
91 96
          break;
92 97
        default:
93 98
          throw FormatError("Unknown DIMACS declaration.");
94 99
        }
95 100
    throw FormatError("Missing problem type declaration.");
96 101
  }
97 102

	
98 103

	
99

	
100
  /// DIMACS minimum cost flow reader function.
104
  /// \brief DIMACS minimum cost flow reader function.
101 105
  ///
102 106
  /// This function reads a minimum cost flow instance from DIMACS format,
103 107
  /// i.e. from a DIMACS file having a line starting with
104 108
  /// \code
105 109
  ///   p min
106 110
  /// \endcode
107 111
  /// At the beginning, \c g is cleared by \c g.clear(). The supply
108
  /// amount of the nodes are written to \c supply (signed). The
109
  /// lower bounds, capacities and costs of the arcs are written to
110
  /// \c lower, \c capacity and \c cost.
112
  /// amount of the nodes are written to the \c supply node map
113
  /// (they are signed values). The lower bounds, capacities and costs
114
  /// of the arcs are written to the \c lower, \c capacity and \c cost
115
  /// arc maps.
116
  ///
117
  /// If the capacity of an arc is less than the lower bound, it will
118
  /// be set to "infinite" instead. The actual value of "infinite" is
119
  /// contolled by the \c infty parameter. If it is 0 (the default value),
120
  /// \c std::numeric_limits<Capacity>::infinity() will be used if available,
121
  /// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to
122
  /// a non-zero value, that value will be used as "infinite".
111 123
  ///
112 124
  /// If the file type was previously evaluated by dimacsType(), then
113 125
  /// the descriptor struct should be given by the \c dest parameter.
114 126
  template <typename Digraph, typename LowerMap,
115 127
            typename CapacityMap, typename CostMap,
116 128
            typename SupplyMap>
117 129
  void readDimacsMin(std::istream& is,
118 130
                     Digraph &g,
119 131
                     LowerMap& lower,
120 132
                     CapacityMap& capacity,
121 133
                     CostMap& cost,
122 134
                     SupplyMap& supply,
135
                     typename CapacityMap::Value infty = 0,
123 136
                     DimacsDescriptor desc=DimacsDescriptor())
124 137
  {
125 138
    g.clear();
126 139
    std::vector<typename Digraph::Node> nodes;
127 140
    typename Digraph::Arc e;
128 141
    std::string problem, str;
129 142
    char c;
130 143
    int i, j;
131 144
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
132 145
    if(desc.type!=DimacsDescriptor::MIN)
133 146
      throw FormatError("Problem type mismatch");
134 147

	
135 148
    nodes.resize(desc.nodeNum + 1);
136 149
    for (int k = 1; k <= desc.nodeNum; ++k) {
137 150
      nodes[k] = g.addNode();
138 151
      supply.set(nodes[k], 0);
139 152
    }
140 153

	
141 154
    typename SupplyMap::Value sup;
142 155
    typename CapacityMap::Value low;
143 156
    typename CapacityMap::Value cap;
144 157
    typename CostMap::Value co;
158
    typedef typename CapacityMap::Value Capacity;
159
    if(infty==0)
160
      infty = std::numeric_limits<Capacity>::has_infinity ?
161
        std::numeric_limits<Capacity>::infinity() :
162
        std::numeric_limits<Capacity>::max();
163

	
145 164
    while (is >> c) {
146 165
      switch (c) {
147 166
      case 'c': // comment line
148 167
        getline(is, str);
149 168
        break;
150 169
      case 'n': // node definition line
151 170
        is >> i >> sup;
152 171
        getline(is, str);
153 172
        supply.set(nodes[i], sup);
154 173
        break;
155
      case 'a': // arc (arc) definition line
174
      case 'a': // arc definition line
156 175
        is >> i >> j >> low >> cap >> co;
157 176
        getline(is, str);
158 177
        e = g.addArc(nodes[i], nodes[j]);
159 178
        lower.set(e, low);
160
        if (cap >= 0)
179
        if (cap >= low)
161 180
          capacity.set(e, cap);
162 181
        else
163
          capacity.set(e, -1);
182
          capacity.set(e, infty);
164 183
        cost.set(e, co);
165 184
        break;
166 185
      }
167 186
    }
168 187
  }
169 188

	
170 189
  template<typename Digraph, typename CapacityMap>
171 190
  void _readDimacs(std::istream& is,
172 191
                   Digraph &g,
173 192
                   CapacityMap& capacity,
174 193
                   typename Digraph::Node &s,
175 194
                   typename Digraph::Node &t,
195
                   typename CapacityMap::Value infty = 0,
176 196
                   DimacsDescriptor desc=DimacsDescriptor()) {
177 197
    g.clear();
178 198
    s=t=INVALID;
179 199
    std::vector<typename Digraph::Node> nodes;
180 200
    typename Digraph::Arc e;
181 201
    char c, d;
182 202
    int i, j;
183 203
    typename CapacityMap::Value _cap;
184 204
    std::string str;
185 205
    nodes.resize(desc.nodeNum + 1);
186 206
    for (int k = 1; k <= desc.nodeNum; ++k) {
187 207
      nodes[k] = g.addNode();
188 208
    }
209
    typedef typename CapacityMap::Value Capacity;
189 210

	
211
    if(infty==0)
212
      infty = std::numeric_limits<Capacity>::has_infinity ?
213
        std::numeric_limits<Capacity>::infinity() :
214
        std::numeric_limits<Capacity>::max();
215
 
190 216
    while (is >> c) {
191 217
      switch (c) {
192 218
      case 'c': // comment line
193 219
        getline(is, str);
194 220
        break;
195 221
      case 'n': // node definition line
196 222
        if (desc.type==DimacsDescriptor::SP) { // shortest path problem
197 223
          is >> i;
198 224
          getline(is, str);
199 225
          s = nodes[i];
200 226
        }
201 227
        if (desc.type==DimacsDescriptor::MAX) { // max flow problem
202 228
          is >> i >> d;
203 229
          getline(is, str);
204 230
          if (d == 's') s = nodes[i];
205 231
          if (d == 't') t = nodes[i];
206 232
        }
207 233
        break;
208
      case 'a': // arc (arc) definition line
209
        if (desc.type==DimacsDescriptor::SP ||
210
            desc.type==DimacsDescriptor::MAX) {
234
      case 'a': // arc definition line
235
        if (desc.type==DimacsDescriptor::SP) {
211 236
          is >> i >> j >> _cap;
212 237
          getline(is, str);
213 238
          e = g.addArc(nodes[i], nodes[j]);
214 239
          capacity.set(e, _cap);
215
        } else {
240
        } 
241
        else if (desc.type==DimacsDescriptor::MAX) {
242
          is >> i >> j >> _cap;
243
          getline(is, str);
244
          e = g.addArc(nodes[i], nodes[j]);
245
          if (_cap >= 0)
246
            capacity.set(e, _cap);
247
          else
248
            capacity.set(e, infty);
249
        }
250
        else {
216 251
          is >> i >> j;
217 252
          getline(is, str);
218 253
          g.addArc(nodes[i], nodes[j]);
219 254
        }
220 255
        break;
221 256
      }
222 257
    }
223 258
  }
224 259

	
225
  /// DIMACS maximum flow reader function.
260
  /// \brief DIMACS maximum flow reader function.
226 261
  ///
227 262
  /// This function reads a maximum flow instance from DIMACS format,
228 263
  /// i.e. from a DIMACS file having a line starting with
229 264
  /// \code
230 265
  ///   p max
231 266
  /// \endcode
232 267
  /// At the beginning, \c g is cleared by \c g.clear(). The arc
233
  /// capacities are written to \c capacity and \c s and \c t are
234
  /// set to the source and the target nodes.
268
  /// capacities are written to the \c capacity arc map and \c s and
269
  /// \c t are set to the source and the target nodes.
270
  ///
271
  /// If the capacity of an arc is negative, it will
272
  /// be set to "infinite" instead. The actual value of "infinite" is
273
  /// contolled by the \c infty parameter. If it is 0 (the default value),
274
  /// \c std::numeric_limits<Capacity>::infinity() will be used if available,
275
  /// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to
276
  /// a non-zero value, that value will be used as "infinite".
235 277
  ///
236 278
  /// If the file type was previously evaluated by dimacsType(), then
237 279
  /// the descriptor struct should be given by the \c dest parameter.
238 280
  template<typename Digraph, typename CapacityMap>
239 281
  void readDimacsMax(std::istream& is,
240 282
                     Digraph &g,
241 283
                     CapacityMap& capacity,
242 284
                     typename Digraph::Node &s,
243 285
                     typename Digraph::Node &t,
286
                     typename CapacityMap::Value infty = 0,
244 287
                     DimacsDescriptor desc=DimacsDescriptor()) {
245 288
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
246 289
    if(desc.type!=DimacsDescriptor::MAX)
247 290
      throw FormatError("Problem type mismatch");
248
    _readDimacs(is,g,capacity,s,t,desc);
291
    _readDimacs(is,g,capacity,s,t,infty,desc);
249 292
  }
250 293

	
251
  /// DIMACS shortest path reader function.
294
  /// \brief DIMACS shortest path reader function.
252 295
  ///
253 296
  /// This function reads a shortest path instance from DIMACS format,
254 297
  /// i.e. from a DIMACS file having a line starting with
255 298
  /// \code
256 299
  ///   p sp
257 300
  /// \endcode
258 301
  /// At the beginning, \c g is cleared by \c g.clear(). The arc
259
  /// lengths are written to \c length and \c s is set to the
302
  /// lengths are written to the \c length arc map and \c s is set to the
260 303
  /// source node.
261 304
  ///
262 305
  /// If the file type was previously evaluated by dimacsType(), then
263 306
  /// the descriptor struct should be given by the \c dest parameter.
264 307
  template<typename Digraph, typename LengthMap>
265 308
  void readDimacsSp(std::istream& is,
266 309
                    Digraph &g,
267 310
                    LengthMap& length,
268 311
                    typename Digraph::Node &s,
269 312
                    DimacsDescriptor desc=DimacsDescriptor()) {
270 313
    typename Digraph::Node t;
271 314
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
272 315
    if(desc.type!=DimacsDescriptor::SP)
273 316
      throw FormatError("Problem type mismatch");
274
    _readDimacs(is, g, length, s, t,desc);
317
    _readDimacs(is, g, length, s, t, 0, desc);
275 318
  }
276 319

	
277
  /// DIMACS capacitated digraph reader function.
320
  /// \brief DIMACS capacitated digraph reader function.
278 321
  ///
279 322
  /// This function reads an arc capacitated digraph instance from
280
  /// DIMACS 'mat' or 'sp' format.
323
  /// DIMACS 'max' or 'sp' format.
281 324
  /// At the beginning, \c g is cleared by \c g.clear()
282
  /// and the arc capacities/lengths are written to \c capacity.
325
  /// and the arc capacities/lengths are written to the \c capacity
326
  /// arc map.
327
  ///
328
  /// In case of the 'max' format, if the capacity of an arc is negative,
329
  /// it will
330
  /// be set to "infinite" instead. The actual value of "infinite" is
331
  /// contolled by the \c infty parameter. If it is 0 (the default value),
332
  /// \c std::numeric_limits<Capacity>::infinity() will be used if available,
333
  /// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to
334
  /// a non-zero value, that value will be used as "infinite".
283 335
  ///
284 336
  /// If the file type was previously evaluated by dimacsType(), then
285 337
  /// the descriptor struct should be given by the \c dest parameter.
286 338
  template<typename Digraph, typename CapacityMap>
287 339
  void readDimacsCap(std::istream& is,
288 340
                     Digraph &g,
289 341
                     CapacityMap& capacity,
342
                     typename CapacityMap::Value infty = 0,
290 343
                     DimacsDescriptor desc=DimacsDescriptor()) {
291 344
    typename Digraph::Node u,v;
292 345
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
293 346
    if(desc.type!=DimacsDescriptor::MAX || desc.type!=DimacsDescriptor::SP)
294 347
      throw FormatError("Problem type mismatch");
295
    _readDimacs(is, g, capacity, u, v, desc);
348
    _readDimacs(is, g, capacity, u, v, infty, desc);
296 349
  }
297 350

	
298
  /// DIMACS plain digraph reader function.
351
  template<typename Graph>
352
  typename enable_if<lemon::UndirectedTagIndicator<Graph>,void>::type
353
  _addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t,
354
              dummy<0> = 0)
355
  {
356
    g.addEdge(s,t);
357
  }
358
  template<typename Graph>
359
  typename disable_if<lemon::UndirectedTagIndicator<Graph>,void>::type
360
  _addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t,
361
              dummy<1> = 1)
362
  {
363
    g.addArc(s,t);
364
  }
365
  
366
  /// \brief DIMACS plain (di)graph reader function.
299 367
  ///
300
  /// This function reads a digraph without any designated nodes and
301
  /// maps from DIMACS format, i.e. from DIMACS files having a line
302
  /// starting with
368
  /// This function reads a plain (di)graph without any designated nodes
369
  /// and maps (e.g. a matching instance) from DIMACS format, i.e. from 
370
  /// DIMACS files having a line starting with
303 371
  /// \code
304 372
  ///   p mat
305 373
  /// \endcode
306 374
  /// At the beginning, \c g is cleared by \c g.clear().
307 375
  ///
308 376
  /// If the file type was previously evaluated by dimacsType(), then
309 377
  /// the descriptor struct should be given by the \c dest parameter.
310
  template<typename Digraph>
311
  void readDimacsMat(std::istream& is, Digraph &g,
312
                     DimacsDescriptor desc=DimacsDescriptor()) {
313
    typename Digraph::Node u,v;
314
    NullMap<typename Digraph::Arc, int> n;
378
  template<typename Graph>
379
  void readDimacsMat(std::istream& is, Graph &g,
380
                     DimacsDescriptor desc=DimacsDescriptor())
381
  {
315 382
    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
316 383
    if(desc.type!=DimacsDescriptor::MAT)
317 384
      throw FormatError("Problem type mismatch");
318
    _readDimacs(is, g, n, u, v, desc);
385

	
386
    g.clear();
387
    std::vector<typename Graph::Node> nodes;
388
    char c;
389
    int i, j;
390
    std::string str;
391
    nodes.resize(desc.nodeNum + 1);
392
    for (int k = 1; k <= desc.nodeNum; ++k) {
393
      nodes[k] = g.addNode();
394
    }
395
    
396
    while (is >> c) {
397
      switch (c) {
398
      case 'c': // comment line
399
        getline(is, str);
400
        break;
401
      case 'n': // node definition line
402
        break;
403
      case 'a': // arc definition line
404
        is >> i >> j;
405
        getline(is, str);
406
        _addArcEdge(g,nodes[i], nodes[j]);
407
        break;
408
      }
409
    }
319 410
  }
320 411

	
321 412
  /// DIMACS plain digraph writer function.
322 413
  ///
323 414
  /// This function writes a digraph without any designated nodes and
324 415
  /// maps into DIMACS format, i.e. into DIMACS file having a line
325 416
  /// starting with
326 417
  /// \code
327 418
  ///   p mat
328 419
  /// \endcode
329 420
  /// If \c comment is not empty, then it will be printed in the first line
330 421
  /// prefixed by 'c'.
331 422
  template<typename Digraph>
332 423
  void writeDimacsMat(std::ostream& os, const Digraph &g,
333 424
                      std::string comment="") {
334 425
    typedef typename Digraph::NodeIt NodeIt;
335 426
    typedef typename Digraph::ArcIt ArcIt;
336 427

	
337 428
    if(!comment.empty())
338 429
      os << "c " << comment << std::endl;
339 430
    os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl;
340 431

	
341 432
    typename Digraph::template NodeMap<int> nodes(g);
342 433
    int i = 1;
343 434
    for(NodeIt v(g); v != INVALID; ++v) {
344 435
      nodes.set(v, i);
345 436
      ++i;
346 437
    }
347 438
    for(ArcIt e(g); e != INVALID; ++e) {
348 439
      os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)]
349 440
         << std::endl;
350 441
    }
351 442
  }
352 443

	
353 444
  /// @}
354 445

	
355 446
} //namespace lemon
356 447

	
357 448
#endif //LEMON_DIMACS_H
Ignore white space 6 line context
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_EDGE_SET_H
20 20
#define LEMON_EDGE_SET_H
21 21

	
22 22
#include <lemon/core.h>
23 23
#include <lemon/bits/edge_set_extender.h>
24 24

	
25
/// \ingroup semi_adaptors
25
/// \ingroup graphs
26 26
/// \file
27 27
/// \brief ArcSet and EdgeSet classes.
28 28
///
29 29
/// Graphs which use another graph's node-set as own.
30 30
namespace lemon {
31 31

	
32
  template <typename _Graph>
32
  template <typename GR>
33 33
  class ListArcSetBase {
34 34
  public:
35 35

	
36
    typedef _Graph Graph;
37
    typedef typename Graph::Node Node;
38
    typedef typename Graph::NodeIt NodeIt;
36
    typedef typename GR::Node Node;
37
    typedef typename GR::NodeIt NodeIt;
39 38

	
40 39
  protected:
41 40

	
42 41
    struct NodeT {
43 42
      int first_out, first_in;
44 43
      NodeT() : first_out(-1), first_in(-1) {}
45 44
    };
46 45

	
47
    typedef typename ItemSetTraits<Graph, Node>::
46
    typedef typename ItemSetTraits<GR, Node>::
48 47
    template Map<NodeT>::Type NodesImplBase;
49 48

	
50
    NodesImplBase* nodes;
49
    NodesImplBase* _nodes;
51 50

	
52 51
    struct ArcT {
53 52
      Node source, target;
54 53
      int next_out, next_in;
55 54
      int prev_out, prev_in;
56 55
      ArcT() : prev_out(-1), prev_in(-1) {}
57 56
    };
58 57

	
59 58
    std::vector<ArcT> arcs;
60 59

	
61 60
    int first_arc;
62 61
    int first_free_arc;
63 62

	
64
    const Graph* graph;
63
    const GR* _graph;
65 64

	
66
    void initalize(const Graph& _graph, NodesImplBase& _nodes) {
67
      graph = &_graph;
68
      nodes = &_nodes;
65
    void initalize(const GR& graph, NodesImplBase& nodes) {
66
      _graph = &graph;
67
      _nodes = &nodes;
69 68
    }
70 69

	
71 70
  public:
72 71

	
73 72
    class Arc {
74
      friend class ListArcSetBase<Graph>;
73
      friend class ListArcSetBase<GR>;
75 74
    protected:
76 75
      Arc(int _id) : id(_id) {}
77 76
      int id;
78 77
    public:
79 78
      Arc() {}
80 79
      Arc(Invalid) : id(-1) {}
81 80
      bool operator==(const Arc& arc) const { return id == arc.id; }
82 81
      bool operator!=(const Arc& arc) const { return id != arc.id; }
83 82
      bool operator<(const Arc& arc) const { return id < arc.id; }
84 83
    };
85 84

	
86 85
    ListArcSetBase() : first_arc(-1), first_free_arc(-1) {}
87 86

	
87
    Node addNode() {
88
      LEMON_ASSERT(false,
89
        "This graph structure does not support node insertion");
90
      return INVALID; // avoid warning
91
    }
92

	
88 93
    Arc addArc(const Node& u, const Node& v) {
89 94
      int n;
90 95
      if (first_free_arc == -1) {
91 96
        n = arcs.size();
92 97
        arcs.push_back(ArcT());
93 98
      } else {
94 99
        n = first_free_arc;
95 100
        first_free_arc = arcs[first_free_arc].next_in;
96 101
      }
97
      arcs[n].next_in = (*nodes)[v].first_in;
98
      if ((*nodes)[v].first_in != -1) {
99
        arcs[(*nodes)[v].first_in].prev_in = n;
102
      arcs[n].next_in = (*_nodes)[v].first_in;
103
      if ((*_nodes)[v].first_in != -1) {
104
        arcs[(*_nodes)[v].first_in].prev_in = n;
100 105
      }
101
      (*nodes)[v].first_in = n;
102
      arcs[n].next_out = (*nodes)[u].first_out;
103
      if ((*nodes)[u].first_out != -1) {
104
        arcs[(*nodes)[u].first_out].prev_out = n;
106
      (*_nodes)[v].first_in = n;
107
      arcs[n].next_out = (*_nodes)[u].first_out;
108
      if ((*_nodes)[u].first_out != -1) {
109
        arcs[(*_nodes)[u].first_out].prev_out = n;
105 110
      }
106
      (*nodes)[u].first_out = n;
111
      (*_nodes)[u].first_out = n;
107 112
      arcs[n].source = u;
108 113
      arcs[n].target = v;
109 114
      return Arc(n);
110 115
    }
111 116

	
112 117
    void erase(const Arc& arc) {
113 118
      int n = arc.id;
114 119
      if (arcs[n].prev_in != -1) {
115 120
        arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
116 121
      } else {
117
        (*nodes)[arcs[n].target].first_in = arcs[n].next_in;
122
        (*_nodes)[arcs[n].target].first_in = arcs[n].next_in;
118 123
      }
119 124
      if (arcs[n].next_in != -1) {
120 125
        arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
121 126
      }
122 127

	
123 128
      if (arcs[n].prev_out != -1) {
124 129
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
125 130
      } else {
126
        (*nodes)[arcs[n].source].first_out = arcs[n].next_out;
131
        (*_nodes)[arcs[n].source].first_out = arcs[n].next_out;
127 132
      }
128 133
      if (arcs[n].next_out != -1) {
129 134
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
130 135
      }
131 136

	
132 137
    }
133 138

	
134 139
    void clear() {
135 140
      Node node;
136 141
      for (first(node); node != INVALID; next(node)) {
137
        (*nodes)[node].first_in = -1;
138
        (*nodes)[node].first_out = -1;
142
        (*_nodes)[node].first_in = -1;
143
        (*_nodes)[node].first_out = -1;
139 144
      }
140 145
      arcs.clear();
141 146
      first_arc = -1;
142 147
      first_free_arc = -1;
143 148
    }
144 149

	
145 150
    void first(Node& node) const {
146
      graph->first(node);
151
      _graph->first(node);
147 152
    }
148 153

	
149 154
    void next(Node& node) const {
150
      graph->next(node);
155
      _graph->next(node);
151 156
    }
152 157

	
153 158
    void first(Arc& arc) const {
154 159
      Node node;
155 160
      first(node);
156
      while (node != INVALID && (*nodes)[node].first_in == -1) {
161
      while (node != INVALID && (*_nodes)[node].first_in == -1) {
157 162
        next(node);
158 163
      }
159
      arc.id = (node == INVALID) ? -1 : (*nodes)[node].first_in;
164
      arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
160 165
    }
161 166

	
162 167
    void next(Arc& arc) const {
163 168
      if (arcs[arc.id].next_in != -1) {
164 169
        arc.id = arcs[arc.id].next_in;
165 170
      } else {
166 171
        Node node = arcs[arc.id].target;
167 172
        next(node);
168
        while (node != INVALID && (*nodes)[node].first_in == -1) {
173
        while (node != INVALID && (*_nodes)[node].first_in == -1) {
169 174
          next(node);
170 175
        }
171
        arc.id = (node == INVALID) ? -1 : (*nodes)[node].first_in;
176
        arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
172 177
      }
173 178
    }
174 179

	
175 180
    void firstOut(Arc& arc, const Node& node) const {
176
      arc.id = (*nodes)[node].first_out;
181
      arc.id = (*_nodes)[node].first_out;
177 182
    }
178 183

	
179 184
    void nextOut(Arc& arc) const {
180 185
      arc.id = arcs[arc.id].next_out;
181 186
    }
182 187

	
183 188
    void firstIn(Arc& arc, const Node& node) const {
184
      arc.id = (*nodes)[node].first_in;
189
      arc.id = (*_nodes)[node].first_in;
185 190
    }
186 191

	
187 192
    void nextIn(Arc& arc) const {
188 193
      arc.id = arcs[arc.id].next_in;
189 194
    }
190 195

	
191
    int id(const Node& node) const { return graph->id(node); }
196
    int id(const Node& node) const { return _graph->id(node); }
192 197
    int id(const Arc& arc) const { return arc.id; }
193 198

	
194
    Node nodeFromId(int ix) const { return graph->nodeFromId(ix); }
199
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
195 200
    Arc arcFromId(int ix) const { return Arc(ix); }
196 201

	
197
    int maxNodeId() const { return graph->maxNodeId(); };
202
    int maxNodeId() const { return _graph->maxNodeId(); };
198 203
    int maxArcId() const { return arcs.size() - 1; }
199 204

	
200 205
    Node source(const Arc& arc) const { return arcs[arc.id].source;}
201 206
    Node target(const Arc& arc) const { return arcs[arc.id].target;}
202 207

	
203
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
208
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
204 209

	
205 210
    NodeNotifier& notifier(Node) const {
206
      return graph->notifier(Node());
211
      return _graph->notifier(Node());
207 212
    }
208 213

	
209
    template <typename _Value>
210
    class NodeMap : public Graph::template NodeMap<_Value> {
214
    template <typename V>
215
    class NodeMap : public GR::template NodeMap<V> {
216
      typedef typename GR::template NodeMap<V> Parent;
217

	
211 218
    public:
212 219

	
213
      typedef typename _Graph::template NodeMap<_Value> Parent;
220
      explicit NodeMap(const ListArcSetBase<GR>& arcset)
221
        : Parent(*arcset._graph) {}
214 222

	
215
      explicit NodeMap(const ListArcSetBase<Graph>& arcset)
216
        : Parent(*arcset.graph) {}
217

	
218
      NodeMap(const ListArcSetBase<Graph>& arcset, const _Value& value)
219
        : Parent(*arcset.graph, value) {}
223
      NodeMap(const ListArcSetBase<GR>& arcset, const V& value)
224
        : Parent(*arcset._graph, value) {}
220 225

	
221 226
      NodeMap& operator=(const NodeMap& cmap) {
222 227
        return operator=<NodeMap>(cmap);
223 228
      }
224 229

	
225 230
      template <typename CMap>
226 231
      NodeMap& operator=(const CMap& cmap) {
227 232
        Parent::operator=(cmap);
228 233
        return *this;
229 234
      }
230 235
    };
231 236

	
232 237
  };
233 238

	
234
  /// \ingroup semi_adaptors
239
  /// \ingroup graphs
235 240
  ///
236 241
  /// \brief Digraph using a node set of another digraph or graph and
237 242
  /// an own arc set.
238 243
  ///
239 244
  /// This structure can be used to establish another directed graph
240 245
  /// over a node set of an existing one. This class uses the same
241 246
  /// Node type as the underlying graph, and each valid node of the
242 247
  /// original graph is valid in this arc set, therefore the node
243 248
  /// objects of the original graph can be used directly with this
244 249
  /// class. The node handling functions (id handling, observing, and
245 250
  /// iterators) works equivalently as in the original graph.
246 251
  ///
247 252
  /// This implementation is based on doubly-linked lists, from each
248 253
  /// node the outgoing and the incoming arcs make up lists, therefore
249 254
  /// one arc can be erased in constant time. It also makes possible,
250 255
  /// that node can be removed from the underlying graph, in this case
251 256
  /// all arcs incident to the given node is erased from the arc set.
252 257
  ///
253
  /// \param _Graph The type of the graph which shares its node set with
258
  /// \param GR The type of the graph which shares its node set with
254 259
  /// this class. Its interface must conform to the
255 260
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
256 261
  /// concept.
257 262
  ///
258
  /// This class is fully conform to the \ref concepts::Digraph
263
  /// This class fully conforms to the \ref concepts::Digraph
259 264
  /// "Digraph" concept.
260
  template <typename _Graph>
261
  class ListArcSet : public ArcSetExtender<ListArcSetBase<_Graph> > {
265
  template <typename GR>
266
  class ListArcSet : public ArcSetExtender<ListArcSetBase<GR> > {
267
    typedef ArcSetExtender<ListArcSetBase<GR> > Parent;
262 268

	
263 269
  public:
264 270

	
265
    typedef ArcSetExtender<ListArcSetBase<_Graph> > Parent;
266

	
267 271
    typedef typename Parent::Node Node;
268 272
    typedef typename Parent::Arc Arc;
269 273

	
270
    typedef _Graph Graph;
271

	
272

	
273 274
    typedef typename Parent::NodesImplBase NodesImplBase;
274 275

	
275 276
    void eraseNode(const Node& node) {
276 277
      Arc arc;
277 278
      Parent::firstOut(arc, node);
278 279
      while (arc != INVALID ) {
279 280
        erase(arc);
280 281
        Parent::firstOut(arc, node);
281 282
      }
282 283

	
283 284
      Parent::firstIn(arc, node);
284 285
      while (arc != INVALID ) {
285 286
        erase(arc);
286 287
        Parent::firstIn(arc, node);
287 288
      }
288 289
    }
289 290

	
290 291
    void clearNodes() {
291 292
      Parent::clear();
292 293
    }
293 294

	
294 295
    class NodesImpl : public NodesImplBase {
295
    public:
296 296
      typedef NodesImplBase Parent;
297 297

	
298
      NodesImpl(const Graph& graph, ListArcSet& arcset)
298
    public:
299
      NodesImpl(const GR& graph, ListArcSet& arcset)
299 300
        : Parent(graph), _arcset(arcset) {}
300 301

	
301 302
      virtual ~NodesImpl() {}
302 303

	
303 304
    protected:
304 305

	
305 306
      virtual void erase(const Node& node) {
306 307
        _arcset.eraseNode(node);
307 308
        Parent::erase(node);
308 309
      }
309 310
      virtual void erase(const std::vector<Node>& nodes) {
310 311
        for (int i = 0; i < int(nodes.size()); ++i) {
311 312
          _arcset.eraseNode(nodes[i]);
312 313
        }
313 314
        Parent::erase(nodes);
314 315
      }
315 316
      virtual void clear() {
316 317
        _arcset.clearNodes();
317 318
        Parent::clear();
318 319
      }
319 320

	
320 321
    private:
321 322
      ListArcSet& _arcset;
322 323
    };
323 324

	
324
    NodesImpl nodes;
325
    NodesImpl _nodes;
325 326

	
326 327
  public:
327 328

	
328 329
    /// \brief Constructor of the ArcSet.
329 330
    ///
330 331
    /// Constructor of the ArcSet.
331
    ListArcSet(const Graph& graph) : nodes(graph, *this) {
332
      Parent::initalize(graph, nodes);
332
    ListArcSet(const GR& graph) : _nodes(graph, *this) {
333
      Parent::initalize(graph, _nodes);
333 334
    }
334 335

	
335 336
    /// \brief Add a new arc to the digraph.
336 337
    ///
337 338
    /// Add a new arc to the digraph with source node \c s
338 339
    /// and target node \c t.
339
    /// \return the new arc.
340
    /// \return The new arc.
340 341
    Arc addArc(const Node& s, const Node& t) {
341 342
      return Parent::addArc(s, t);
342 343
    }
343 344

	
344 345
    /// \brief Erase an arc from the digraph.
345 346
    ///
346 347
    /// Erase an arc \c a from the digraph.
347 348
    void erase(const Arc& a) {
348 349
      return Parent::erase(a);
349 350
    }
350 351

	
351 352
  };
352 353

	
353
  template <typename _Graph>
354
  template <typename GR>
354 355
  class ListEdgeSetBase {
355 356
  public:
356 357

	
357
    typedef _Graph Graph;
358
    typedef typename Graph::Node Node;
359
    typedef typename Graph::NodeIt NodeIt;
358
    typedef typename GR::Node Node;
359
    typedef typename GR::NodeIt NodeIt;
360 360

	
361 361
  protected:
362 362

	
363 363
    struct NodeT {
364 364
      int first_out;
365 365
      NodeT() : first_out(-1) {}
366 366
    };
367 367

	
368
    typedef typename ItemSetTraits<Graph, Node>::
368
    typedef typename ItemSetTraits<GR, Node>::
369 369
    template Map<NodeT>::Type NodesImplBase;
370 370

	
371
    NodesImplBase* nodes;
371
    NodesImplBase* _nodes;
372 372

	
373 373
    struct ArcT {
374 374
      Node target;
375 375
      int prev_out, next_out;
376 376
      ArcT() : prev_out(-1), next_out(-1) {}
377 377
    };
378 378

	
379 379
    std::vector<ArcT> arcs;
380 380

	
381 381
    int first_arc;
382 382
    int first_free_arc;
383 383

	
384
    const Graph* graph;
384
    const GR* _graph;
385 385

	
386
    void initalize(const Graph& _graph, NodesImplBase& _nodes) {
387
      graph = &_graph;
388
      nodes = &_nodes;
386
    void initalize(const GR& graph, NodesImplBase& nodes) {
387
      _graph = &graph;
388
      _nodes = &nodes;
389 389
    }
390 390

	
391 391
  public:
392 392

	
393 393
    class Edge {
394 394
      friend class ListEdgeSetBase;
395 395
    protected:
396 396

	
397 397
      int id;
398 398
      explicit Edge(int _id) { id = _id;}
399 399

	
400 400
    public:
401 401
      Edge() {}
402 402
      Edge (Invalid) { id = -1; }
403 403
      bool operator==(const Edge& arc) const {return id == arc.id;}
404 404
      bool operator!=(const Edge& arc) const {return id != arc.id;}
405 405
      bool operator<(const Edge& arc) const {return id < arc.id;}
406 406
    };
407 407

	
408 408
    class Arc {
409 409
      friend class ListEdgeSetBase;
410 410
    protected:
411 411
      Arc(int _id) : id(_id) {}
412 412
      int id;
413 413
    public:
414 414
      operator Edge() const { return edgeFromId(id / 2); }
415 415

	
416 416
      Arc() {}
417 417
      Arc(Invalid) : id(-1) {}
418 418
      bool operator==(const Arc& arc) const { return id == arc.id; }
419 419
      bool operator!=(const Arc& arc) const { return id != arc.id; }
420 420
      bool operator<(const Arc& arc) const { return id < arc.id; }
421 421
    };
422 422

	
423 423
    ListEdgeSetBase() : first_arc(-1), first_free_arc(-1) {}
424 424

	
425
    Node addNode() {
426
      LEMON_ASSERT(false,
427
        "This graph structure does not support node insertion");
428
      return INVALID; // avoid warning
429
    }
430

	
425 431
    Edge addEdge(const Node& u, const Node& v) {
426 432
      int n;
427 433

	
428 434
      if (first_free_arc == -1) {
429 435
        n = arcs.size();
430 436
        arcs.push_back(ArcT());
431 437
        arcs.push_back(ArcT());
432 438
      } else {
433 439
        n = first_free_arc;
434 440
        first_free_arc = arcs[n].next_out;
435 441
      }
436 442

	
437 443
      arcs[n].target = u;
438 444
      arcs[n | 1].target = v;
439 445

	
440
      arcs[n].next_out = (*nodes)[v].first_out;
441
      if ((*nodes)[v].first_out != -1) {
442
        arcs[(*nodes)[v].first_out].prev_out = n;
446
      arcs[n].next_out = (*_nodes)[v].first_out;
447
      if ((*_nodes)[v].first_out != -1) {
448
        arcs[(*_nodes)[v].first_out].prev_out = n;
443 449
      }
444
      (*nodes)[v].first_out = n;
450
      (*_nodes)[v].first_out = n;
445 451
      arcs[n].prev_out = -1;
446 452

	
447
      if ((*nodes)[u].first_out != -1) {
448
        arcs[(*nodes)[u].first_out].prev_out = (n | 1);
453
      if ((*_nodes)[u].first_out != -1) {
454
        arcs[(*_nodes)[u].first_out].prev_out = (n | 1);
449 455
      }
450
      arcs[n | 1].next_out = (*nodes)[u].first_out;
451
      (*nodes)[u].first_out = (n | 1);
456
      arcs[n | 1].next_out = (*_nodes)[u].first_out;
457
      (*_nodes)[u].first_out = (n | 1);
452 458
      arcs[n | 1].prev_out = -1;
453 459

	
454 460
      return Edge(n / 2);
455 461
    }
456 462

	
457 463
    void erase(const Edge& arc) {
458 464
      int n = arc.id * 2;
459 465

	
460 466
      if (arcs[n].next_out != -1) {
461 467
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
462 468
      }
463 469

	
464 470
      if (arcs[n].prev_out != -1) {
465 471
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
466 472
      } else {
467
        (*nodes)[arcs[n | 1].target].first_out = arcs[n].next_out;
473
        (*_nodes)[arcs[n | 1].target].first_out = arcs[n].next_out;
468 474
      }
469 475

	
470 476
      if (arcs[n | 1].next_out != -1) {
471 477
        arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
472 478
      }
473 479

	
474 480
      if (arcs[n | 1].prev_out != -1) {
475 481
        arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
476 482
      } else {
477
        (*nodes)[arcs[n].target].first_out = arcs[n | 1].next_out;
483
        (*_nodes)[arcs[n].target].first_out = arcs[n | 1].next_out;
478 484
      }
479 485

	
480 486
      arcs[n].next_out = first_free_arc;
481 487
      first_free_arc = n;
482 488

	
483 489
    }
484 490

	
485 491
    void clear() {
486 492
      Node node;
487 493
      for (first(node); node != INVALID; next(node)) {
488
        (*nodes)[node].first_out = -1;
494
        (*_nodes)[node].first_out = -1;
489 495
      }
490 496
      arcs.clear();
491 497
      first_arc = -1;
492 498
      first_free_arc = -1;
493 499
    }
494 500

	
495 501
    void first(Node& node) const {
496
      graph->first(node);
502
      _graph->first(node);
497 503
    }
498 504

	
499 505
    void next(Node& node) const {
500
      graph->next(node);
506
      _graph->next(node);
501 507
    }
502 508

	
503 509
    void first(Arc& arc) const {
504 510
      Node node;
505 511
      first(node);
506
      while (node != INVALID && (*nodes)[node].first_out == -1) {
512
      while (node != INVALID && (*_nodes)[node].first_out == -1) {
507 513
        next(node);
508 514
      }
509
      arc.id = (node == INVALID) ? -1 : (*nodes)[node].first_out;
515
      arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
510 516
    }
511 517

	
512 518
    void next(Arc& arc) const {
513 519
      if (arcs[arc.id].next_out != -1) {
514 520
        arc.id = arcs[arc.id].next_out;
515 521
      } else {
516 522
        Node node = arcs[arc.id ^ 1].target;
517 523
        next(node);
518
        while(node != INVALID && (*nodes)[node].first_out == -1) {
524
        while(node != INVALID && (*_nodes)[node].first_out == -1) {
519 525
          next(node);
520 526
        }
521
        arc.id = (node == INVALID) ? -1 : (*nodes)[node].first_out;
527
        arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
522 528
      }
523 529
    }
524 530

	
525 531
    void first(Edge& edge) const {
526 532
      Node node;
527 533
      first(node);
528 534
      while (node != INVALID) {
529
        edge.id = (*nodes)[node].first_out;
535
        edge.id = (*_nodes)[node].first_out;
530 536
        while ((edge.id & 1) != 1) {
531 537
          edge.id = arcs[edge.id].next_out;
532 538
        }
533 539
        if (edge.id != -1) {
534 540
          edge.id /= 2;
535 541
          return;
536 542
        }
537 543
        next(node);
538 544
      }
539 545
      edge.id = -1;
540 546
    }
541 547

	
542 548
    void next(Edge& edge) const {
543 549
      Node node = arcs[edge.id * 2].target;
544 550
      edge.id = arcs[(edge.id * 2) | 1].next_out;
545 551
      while ((edge.id & 1) != 1) {
546 552
        edge.id = arcs[edge.id].next_out;
547 553
      }
548 554
      if (edge.id != -1) {
549 555
        edge.id /= 2;
550 556
        return;
551 557
      }
552 558
      next(node);
553 559
      while (node != INVALID) {
554
        edge.id = (*nodes)[node].first_out;
560
        edge.id = (*_nodes)[node].first_out;
555 561
        while ((edge.id & 1) != 1) {
556 562
          edge.id = arcs[edge.id].next_out;
557 563
        }
558 564
        if (edge.id != -1) {
559 565
          edge.id /= 2;
560 566
          return;
561 567
        }
562 568
        next(node);
563 569
      }
564 570
      edge.id = -1;
565 571
    }
566 572

	
567 573
    void firstOut(Arc& arc, const Node& node) const {
568
      arc.id = (*nodes)[node].first_out;
574
      arc.id = (*_nodes)[node].first_out;
569 575
    }
570 576

	
571 577
    void nextOut(Arc& arc) const {
572 578
      arc.id = arcs[arc.id].next_out;
573 579
    }
574 580

	
575 581
    void firstIn(Arc& arc, const Node& node) const {
576
      arc.id = (((*nodes)[node].first_out) ^ 1);
582
      arc.id = (((*_nodes)[node].first_out) ^ 1);
577 583
      if (arc.id == -2) arc.id = -1;
578 584
    }
579 585

	
580 586
    void nextIn(Arc& arc) const {
581 587
      arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
582 588
      if (arc.id == -2) arc.id = -1;
583 589
    }
584 590

	
585 591
    void firstInc(Edge &arc, bool& dir, const Node& node) const {
586
      int de = (*nodes)[node].first_out;
592
      int de = (*_nodes)[node].first_out;
587 593
      if (de != -1 ) {
588 594
        arc.id = de / 2;
589 595
        dir = ((de & 1) == 1);
590 596
      } else {
591 597
        arc.id = -1;
592 598
        dir = true;
593 599
      }
594 600
    }
595 601
    void nextInc(Edge &arc, bool& dir) const {
596 602
      int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
597 603
      if (de != -1 ) {
598 604
        arc.id = de / 2;
599 605
        dir = ((de & 1) == 1);
600 606
      } else {
601 607
        arc.id = -1;
602 608
        dir = true;
603 609
      }
604 610
    }
605 611

	
606 612
    static bool direction(Arc arc) {
607 613
      return (arc.id & 1) == 1;
608 614
    }
609 615

	
610 616
    static Arc direct(Edge edge, bool dir) {
611 617
      return Arc(edge.id * 2 + (dir ? 1 : 0));
612 618
    }
613 619

	
614
    int id(const Node& node) const { return graph->id(node); }
620
    int id(const Node& node) const { return _graph->id(node); }
615 621
    static int id(Arc e) { return e.id; }
616 622
    static int id(Edge e) { return e.id; }
617 623

	
618
    Node nodeFromId(int id) const { return graph->nodeFromId(id); }
624
    Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
619 625
    static Arc arcFromId(int id) { return Arc(id);}
620 626
    static Edge edgeFromId(int id) { return Edge(id);}
621 627

	
622
    int maxNodeId() const { return graph->maxNodeId(); };
628
    int maxNodeId() const { return _graph->maxNodeId(); };
623 629
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
624 630
    int maxArcId() const { return arcs.size()-1; }
625 631

	
626 632
    Node source(Arc e) const { return arcs[e.id ^ 1].target; }
627 633
    Node target(Arc e) const { return arcs[e.id].target; }
628 634

	
629 635
    Node u(Edge e) const { return arcs[2 * e.id].target; }
630 636
    Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
631 637

	
632
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
638
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
633 639

	
634 640
    NodeNotifier& notifier(Node) const {
635
      return graph->notifier(Node());
641
      return _graph->notifier(Node());
636 642
    }
637 643

	
638
    template <typename _Value>
639
    class NodeMap : public Graph::template NodeMap<_Value> {
644
    template <typename V>
645
    class NodeMap : public GR::template NodeMap<V> {
646
      typedef typename GR::template NodeMap<V> Parent;
647

	
640 648
    public:
641 649

	
642
      typedef typename _Graph::template NodeMap<_Value> Parent;
650
      explicit NodeMap(const ListEdgeSetBase<GR>& arcset)
651
        : Parent(*arcset._graph) {}
643 652

	
644
      explicit NodeMap(const ListEdgeSetBase<Graph>& arcset)
645
        : Parent(*arcset.graph) {}
646

	
647
      NodeMap(const ListEdgeSetBase<Graph>& arcset, const _Value& value)
648
        : Parent(*arcset.graph, value) {}
653
      NodeMap(const ListEdgeSetBase<GR>& arcset, const V& value)
654
        : Parent(*arcset._graph, value) {}
649 655

	
650 656
      NodeMap& operator=(const NodeMap& cmap) {
651 657
        return operator=<NodeMap>(cmap);
652 658
      }
653 659

	
654 660
      template <typename CMap>
655 661
      NodeMap& operator=(const CMap& cmap) {
656 662
        Parent::operator=(cmap);
657 663
        return *this;
658 664
      }
659 665
    };
660 666

	
661 667
  };
662 668

	
663
  /// \ingroup semi_adaptors
669
  /// \ingroup graphs
664 670
  ///
665 671
  /// \brief Graph using a node set of another digraph or graph and an
666 672
  /// own edge set.
667 673
  ///
668 674
  /// This structure can be used to establish another graph over a
669 675
  /// node set of an existing one. This class uses the same Node type
670 676
  /// as the underlying graph, and each valid node of the original
671 677
  /// graph is valid in this arc set, therefore the node objects of
672 678
  /// the original graph can be used directly with this class. The
673 679
  /// node handling functions (id handling, observing, and iterators)
674 680
  /// works equivalently as in the original graph.
675 681
  ///
676 682
  /// This implementation is based on doubly-linked lists, from each
677 683
  /// node the incident edges make up lists, therefore one edge can be
678 684
  /// erased in constant time. It also makes possible, that node can
679 685
  /// be removed from the underlying graph, in this case all edges
680 686
  /// incident to the given node is erased from the arc set.
681 687
  ///
682
  /// \param _Graph The type of the graph which shares its node set
688
  /// \param GR The type of the graph which shares its node set
683 689
  /// with this class. Its interface must conform to the
684 690
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
685 691
  /// concept.
686 692
  ///
687
  /// This class is fully conform to the \ref concepts::Graph "Graph"
693
  /// This class fully conforms to the \ref concepts::Graph "Graph"
688 694
  /// concept.
689
  template <typename _Graph>
690
  class ListEdgeSet : public EdgeSetExtender<ListEdgeSetBase<_Graph> > {
695
  template <typename GR>
696
  class ListEdgeSet : public EdgeSetExtender<ListEdgeSetBase<GR> > {
697
    typedef EdgeSetExtender<ListEdgeSetBase<GR> > Parent;
691 698

	
692 699
  public:
693 700

	
694
    typedef EdgeSetExtender<ListEdgeSetBase<_Graph> > Parent;
695

	
696 701
    typedef typename Parent::Node Node;
697 702
    typedef typename Parent::Arc Arc;
698 703
    typedef typename Parent::Edge Edge;
699 704

	
700
    typedef _Graph Graph;
701

	
702

	
703 705
    typedef typename Parent::NodesImplBase NodesImplBase;
704 706

	
705 707
    void eraseNode(const Node& node) {
706 708
      Arc arc;
707 709
      Parent::firstOut(arc, node);
708 710
      while (arc != INVALID ) {
709 711
        erase(arc);
710 712
        Parent::firstOut(arc, node);
711 713
      }
712 714

	
713 715
    }
714 716

	
715 717
    void clearNodes() {
716 718
      Parent::clear();
717 719
    }
718 720

	
719 721
    class NodesImpl : public NodesImplBase {
720
    public:
721 722
      typedef NodesImplBase Parent;
722 723

	
723
      NodesImpl(const Graph& graph, ListEdgeSet& arcset)
724
    public:
725
      NodesImpl(const GR& graph, ListEdgeSet& arcset)
724 726
        : Parent(graph), _arcset(arcset) {}
725 727

	
726 728
      virtual ~NodesImpl() {}
727 729

	
728 730
    protected:
729 731

	
730 732
      virtual void erase(const Node& node) {
731 733
        _arcset.eraseNode(node);
732 734
        Parent::erase(node);
733 735
      }
734 736
      virtual void erase(const std::vector<Node>& nodes) {
735 737
        for (int i = 0; i < int(nodes.size()); ++i) {
736 738
          _arcset.eraseNode(nodes[i]);
737 739
        }
738 740
        Parent::erase(nodes);
739 741
      }
740 742
      virtual void clear() {
741 743
        _arcset.clearNodes();
742 744
        Parent::clear();
743 745
      }
744 746

	
745 747
    private:
746 748
      ListEdgeSet& _arcset;
747 749
    };
748 750

	
749
    NodesImpl nodes;
751
    NodesImpl _nodes;
750 752

	
751 753
  public:
752 754

	
753 755
    /// \brief Constructor of the EdgeSet.
754 756
    ///
755 757
    /// Constructor of the EdgeSet.
756
    ListEdgeSet(const Graph& graph) : nodes(graph, *this) {
757
      Parent::initalize(graph, nodes);
758
    ListEdgeSet(const GR& graph) : _nodes(graph, *this) {
759
      Parent::initalize(graph, _nodes);
758 760
    }
759 761

	
760 762
    /// \brief Add a new edge to the graph.
761 763
    ///
762 764
    /// Add a new edge to the graph with node \c u
763 765
    /// and node \c v endpoints.
764
    /// \return the new edge.
766
    /// \return The new edge.
765 767
    Edge addEdge(const Node& u, const Node& v) {
766 768
      return Parent::addEdge(u, v);
767 769
    }
768 770

	
769 771
    /// \brief Erase an edge from the graph.
770 772
    ///
771 773
    /// Erase the edge \c e from the graph.
772 774
    void erase(const Edge& e) {
773 775
      return Parent::erase(e);
774 776
    }
775 777

	
776 778
  };
777 779

	
778
  template <typename _Graph>
780
  template <typename GR>
779 781
  class SmartArcSetBase {
780 782
  public:
781 783

	
782
    typedef _Graph Graph;
783
    typedef typename Graph::Node Node;
784
    typedef typename Graph::NodeIt NodeIt;
784
    typedef typename GR::Node Node;
785
    typedef typename GR::NodeIt NodeIt;
785 786

	
786 787
  protected:
787 788

	
788 789
    struct NodeT {
789 790
      int first_out, first_in;
790 791
      NodeT() : first_out(-1), first_in(-1) {}
791 792
    };
792 793

	
793
    typedef typename ItemSetTraits<Graph, Node>::
794
    typedef typename ItemSetTraits<GR, Node>::
794 795
    template Map<NodeT>::Type NodesImplBase;
795 796

	
796
    NodesImplBase* nodes;
797
    NodesImplBase* _nodes;
797 798

	
798 799
    struct ArcT {
799 800
      Node source, target;
800 801
      int next_out, next_in;
801 802
      ArcT() {}
802 803
    };
803 804

	
804 805
    std::vector<ArcT> arcs;
805 806

	
806
    const Graph* graph;
807
    const GR* _graph;
807 808

	
808
    void initalize(const Graph& _graph, NodesImplBase& _nodes) {
809
      graph = &_graph;
810
      nodes = &_nodes;
809
    void initalize(const GR& graph, NodesImplBase& nodes) {
810
      _graph = &graph;
811
      _nodes = &nodes;
811 812
    }
812 813

	
813 814
  public:
814 815

	
815 816
    class Arc {
816
      friend class SmartArcSetBase<Graph>;
817
      friend class SmartArcSetBase<GR>;
817 818
    protected:
818 819
      Arc(int _id) : id(_id) {}
819 820
      int id;
820 821
    public:
821 822
      Arc() {}
822 823
      Arc(Invalid) : id(-1) {}
823 824
      bool operator==(const Arc& arc) const { return id == arc.id; }
824 825
      bool operator!=(const Arc& arc) const { return id != arc.id; }
825 826
      bool operator<(const Arc& arc) const { return id < arc.id; }
826 827
    };
827 828

	
828 829
    SmartArcSetBase() {}
829 830

	
831
    Node addNode() {
832
      LEMON_ASSERT(false,
833
        "This graph structure does not support node insertion");
834
      return INVALID; // avoid warning
835
    }
836

	
830 837
    Arc addArc(const Node& u, const Node& v) {
831 838
      int n = arcs.size();
832 839
      arcs.push_back(ArcT());
833
      arcs[n].next_in = (*nodes)[v].first_in;
834
      (*nodes)[v].first_in = n;
835
      arcs[n].next_out = (*nodes)[u].first_out;
836
      (*nodes)[u].first_out = n;
840
      arcs[n].next_in = (*_nodes)[v].first_in;
841
      (*_nodes)[v].first_in = n;
842
      arcs[n].next_out = (*_nodes)[u].first_out;
843
      (*_nodes)[u].first_out = n;
837 844
      arcs[n].source = u;
838 845
      arcs[n].target = v;
839 846
      return Arc(n);
840 847
    }
841 848

	
842 849
    void clear() {
843 850
      Node node;
844 851
      for (first(node); node != INVALID; next(node)) {
845
        (*nodes)[node].first_in = -1;
846
        (*nodes)[node].first_out = -1;
852
        (*_nodes)[node].first_in = -1;
853
        (*_nodes)[node].first_out = -1;
847 854
      }
848 855
      arcs.clear();
849 856
    }
850 857

	
851 858
    void first(Node& node) const {
852
      graph->first(node);
859
      _graph->first(node);
853 860
    }
854 861

	
855 862
    void next(Node& node) const {
856
      graph->next(node);
863
      _graph->next(node);
857 864
    }
858 865

	
859 866
    void first(Arc& arc) const {
860 867
      arc.id = arcs.size() - 1;
861 868
    }
862 869

	
863
    void next(Arc& arc) const {
870
    static void next(Arc& arc) {
864 871
      --arc.id;
865 872
    }
866 873

	
867 874
    void firstOut(Arc& arc, const Node& node) const {
868
      arc.id = (*nodes)[node].first_out;
875
      arc.id = (*_nodes)[node].first_out;
869 876
    }
870 877

	
871 878
    void nextOut(Arc& arc) const {
872 879
      arc.id = arcs[arc.id].next_out;
873 880
    }
874 881

	
875 882
    void firstIn(Arc& arc, const Node& node) const {
876
      arc.id = (*nodes)[node].first_in;
883
      arc.id = (*_nodes)[node].first_in;
877 884
    }
878 885

	
879 886
    void nextIn(Arc& arc) const {
880 887
      arc.id = arcs[arc.id].next_in;
881 888
    }
882 889

	
883
    int id(const Node& node) const { return graph->id(node); }
890
    int id(const Node& node) const { return _graph->id(node); }
884 891
    int id(const Arc& arc) const { return arc.id; }
885 892

	
886
    Node nodeFromId(int ix) const { return graph->nodeFromId(ix); }
893
    Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
887 894
    Arc arcFromId(int ix) const { return Arc(ix); }
888 895

	
889
    int maxNodeId() const { return graph->maxNodeId(); };
896
    int maxNodeId() const { return _graph->maxNodeId(); };
890 897
    int maxArcId() const { return arcs.size() - 1; }
891 898

	
892 899
    Node source(const Arc& arc) const { return arcs[arc.id].source;}
893 900
    Node target(const Arc& arc) const { return arcs[arc.id].target;}
894 901

	
895
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
902
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
896 903

	
897 904
    NodeNotifier& notifier(Node) const {
898
      return graph->notifier(Node());
905
      return _graph->notifier(Node());
899 906
    }
900 907

	
901
    template <typename _Value>
902
    class NodeMap : public Graph::template NodeMap<_Value> {
908
    template <typename V>
909
    class NodeMap : public GR::template NodeMap<V> {
910
      typedef typename GR::template NodeMap<V> Parent;
911

	
903 912
    public:
904 913

	
905
      typedef typename _Graph::template NodeMap<_Value> Parent;
914
      explicit NodeMap(const SmartArcSetBase<GR>& arcset)
915
        : Parent(*arcset._graph) { }
906 916

	
907
      explicit NodeMap(const SmartArcSetBase<Graph>& arcset)
908
        : Parent(*arcset.graph) { }
909

	
910
      NodeMap(const SmartArcSetBase<Graph>& arcset, const _Value& value)
911
        : Parent(*arcset.graph, value) { }
917
      NodeMap(const SmartArcSetBase<GR>& arcset, const V& value)
918
        : Parent(*arcset._graph, value) { }
912 919

	
913 920
      NodeMap& operator=(const NodeMap& cmap) {
914 921
        return operator=<NodeMap>(cmap);
915 922
      }
916 923

	
917 924
      template <typename CMap>
918 925
      NodeMap& operator=(const CMap& cmap) {
919 926
        Parent::operator=(cmap);
920 927
        return *this;
921 928
      }
922 929
    };
923 930

	
924 931
  };
925 932

	
926 933

	
927
  /// \ingroup semi_adaptors
934
  /// \ingroup graphs
928 935
  ///
929 936
  /// \brief Digraph using a node set of another digraph or graph and
930 937
  /// an own arc set.
931 938
  ///
932 939
  /// This structure can be used to establish another directed graph
933 940
  /// over a node set of an existing one. This class uses the same
934 941
  /// Node type as the underlying graph, and each valid node of the
935 942
  /// original graph is valid in this arc set, therefore the node
936 943
  /// objects of the original graph can be used directly with this
937 944
  /// class. The node handling functions (id handling, observing, and
938 945
  /// iterators) works equivalently as in the original graph.
939 946
  ///
940
  /// \param _Graph The type of the graph which shares its node set with
947
  /// \param GR The type of the graph which shares its node set with
941 948
  /// this class. Its interface must conform to the
942 949
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
943 950
  /// concept.
944 951
  ///
945 952
  /// This implementation is slightly faster than the \c ListArcSet,
946 953
  /// because it uses continuous storage for arcs and it uses just
947 954
  /// single-linked lists for enumerate outgoing and incoming
948 955
  /// arcs. Therefore the arcs cannot be erased from the arc sets.
949 956
  ///
950 957
  /// \warning If a node is erased from the underlying graph and this
951 958
  /// node is the source or target of one arc in the arc set, then
952 959
  /// the arc set is invalidated, and it cannot be used anymore. The
953 960
  /// validity can be checked with the \c valid() member function.
954 961
  ///
955
  /// This class is fully conform to the \ref concepts::Digraph
962
  /// This class fully conforms to the \ref concepts::Digraph
956 963
  /// "Digraph" concept.
957
  template <typename _Graph>
958
  class SmartArcSet : public ArcSetExtender<SmartArcSetBase<_Graph> > {
964
  template <typename GR>
965
  class SmartArcSet : public ArcSetExtender<SmartArcSetBase<GR> > {
966
    typedef ArcSetExtender<SmartArcSetBase<GR> > Parent;
959 967

	
960 968
  public:
961 969

	
962
    typedef ArcSetExtender<SmartArcSetBase<_Graph> > Parent;
963

	
964 970
    typedef typename Parent::Node Node;
965 971
    typedef typename Parent::Arc Arc;
966 972

	
967
    typedef _Graph Graph;
968

	
969 973
  protected:
970 974

	
971 975
    typedef typename Parent::NodesImplBase NodesImplBase;
972 976

	
973 977
    void eraseNode(const Node& node) {
974 978
      if (typename Parent::InArcIt(*this, node) == INVALID &&
975 979
          typename Parent::OutArcIt(*this, node) == INVALID) {
976 980
        return;
977 981
      }
978 982
      throw typename NodesImplBase::Notifier::ImmediateDetach();
979 983
    }
980 984

	
981 985
    void clearNodes() {
982 986
      Parent::clear();
983 987
    }
984 988

	
985 989
    class NodesImpl : public NodesImplBase {
986
    public:
987 990
      typedef NodesImplBase Parent;
988 991

	
989
      NodesImpl(const Graph& graph, SmartArcSet& arcset)
992
    public:
993
      NodesImpl(const GR& graph, SmartArcSet& arcset)
990 994
        : Parent(graph), _arcset(arcset) {}
991 995

	
992 996
      virtual ~NodesImpl() {}
993 997

	
994 998
      bool attached() const {
995 999
        return Parent::attached();
996 1000
      }
997 1001

	
998 1002
    protected:
999 1003

	
1000 1004
      virtual void erase(const Node& node) {
1001 1005
        try {
1002 1006
          _arcset.eraseNode(node);
1003 1007
          Parent::erase(node);
1004 1008
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1005 1009
          Parent::clear();
1006 1010
          throw;
1007 1011
        }
1008 1012
      }
1009 1013
      virtual void erase(const std::vector<Node>& nodes) {
1010 1014
        try {
1011 1015
          for (int i = 0; i < int(nodes.size()); ++i) {
1012 1016
            _arcset.eraseNode(nodes[i]);
1013 1017
          }
1014 1018
          Parent::erase(nodes);
1015 1019
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1016 1020
          Parent::clear();
1017 1021
          throw;
1018 1022
        }
1019 1023
      }
1020 1024
      virtual void clear() {
1021 1025
        _arcset.clearNodes();
1022 1026
        Parent::clear();
1023 1027
      }
1024 1028

	
1025 1029
    private:
1026 1030
      SmartArcSet& _arcset;
1027 1031
    };
1028 1032

	
1029
    NodesImpl nodes;
1033
    NodesImpl _nodes;
1030 1034

	
1031 1035
  public:
1032 1036

	
1033 1037
    /// \brief Constructor of the ArcSet.
1034 1038
    ///
1035 1039
    /// Constructor of the ArcSet.
1036
    SmartArcSet(const Graph& graph) : nodes(graph, *this) {
1037
      Parent::initalize(graph, nodes);
1040
    SmartArcSet(const GR& graph) : _nodes(graph, *this) {
1041
      Parent::initalize(graph, _nodes);
1038 1042
    }
1039 1043

	
1040 1044
    /// \brief Add a new arc to the digraph.
1041 1045
    ///
1042 1046
    /// Add a new arc to the digraph with source node \c s
1043 1047
    /// and target node \c t.
1044
    /// \return the new arc.
1048
    /// \return The new arc.
1045 1049
    Arc addArc(const Node& s, const Node& t) {
1046 1050
      return Parent::addArc(s, t);
1047 1051
    }
1048 1052

	
1049 1053
    /// \brief Validity check
1050 1054
    ///
1051 1055
    /// This functions gives back false if the ArcSet is
1052 1056
    /// invalidated. It occurs when a node in the underlying graph is
1053 1057
    /// erased and it is not isolated in the ArcSet.
1054 1058
    bool valid() const {
1055
      return nodes.attached();
1059
      return _nodes.attached();
1056 1060
    }
1057 1061

	
1058 1062
  };
1059 1063

	
1060 1064

	
1061
  template <typename _Graph>
1065
  template <typename GR>
1062 1066
  class SmartEdgeSetBase {
1063 1067
  public:
1064 1068

	
1065
    typedef _Graph Graph;
1066
    typedef typename Graph::Node Node;
1067
    typedef typename Graph::NodeIt NodeIt;
1069
    typedef typename GR::Node Node;
1070
    typedef typename GR::NodeIt NodeIt;
1068 1071

	
1069 1072
  protected:
1070 1073

	
1071 1074
    struct NodeT {
1072 1075
      int first_out;
1073 1076
      NodeT() : first_out(-1) {}
1074 1077
    };
1075 1078

	
1076
    typedef typename ItemSetTraits<Graph, Node>::
1079
    typedef typename ItemSetTraits<GR, Node>::
1077 1080
    template Map<NodeT>::Type NodesImplBase;
1078 1081

	
1079
    NodesImplBase* nodes;
1082
    NodesImplBase* _nodes;
1080 1083

	
1081 1084
    struct ArcT {
1082 1085
      Node target;
1083 1086
      int next_out;
1084 1087
      ArcT() {}
1085 1088
    };
1086 1089

	
1087 1090
    std::vector<ArcT> arcs;
1088 1091

	
1089
    const Graph* graph;
1092
    const GR* _graph;
1090 1093

	
1091
    void initalize(const Graph& _graph, NodesImplBase& _nodes) {
1092
      graph = &_graph;
1093
      nodes = &_nodes;
1094
    void initalize(const GR& graph, NodesImplBase& nodes) {
1095
      _graph = &graph;
1096
      _nodes = &nodes;
1094 1097
    }
1095 1098

	
1096 1099
  public:
1097 1100

	
1098 1101
    class Edge {
1099 1102
      friend class SmartEdgeSetBase;
1100 1103
    protected:
1101 1104

	
1102 1105
      int id;
1103 1106
      explicit Edge(int _id) { id = _id;}
1104 1107

	
1105 1108
    public:
1106 1109
      Edge() {}
1107 1110
      Edge (Invalid) { id = -1; }
1108 1111
      bool operator==(const Edge& arc) const {return id == arc.id;}
1109 1112
      bool operator!=(const Edge& arc) const {return id != arc.id;}
1110 1113
      bool operator<(const Edge& arc) const {return id < arc.id;}
1111 1114
    };
1112 1115

	
1113 1116
    class Arc {
1114 1117
      friend class SmartEdgeSetBase;
1115 1118
    protected:
1116 1119
      Arc(int _id) : id(_id) {}
1117 1120
      int id;
1118 1121
    public:
1119 1122
      operator Edge() const { return edgeFromId(id / 2); }
1120 1123

	
1121 1124
      Arc() {}
1122 1125
      Arc(Invalid) : id(-1) {}
1123 1126
      bool operator==(const Arc& arc) const { return id == arc.id; }
1124 1127
      bool operator!=(const Arc& arc) const { return id != arc.id; }
1125 1128
      bool operator<(const Arc& arc) const { return id < arc.id; }
1126 1129
    };
1127 1130

	
1128 1131
    SmartEdgeSetBase() {}
1129 1132

	
1133
    Node addNode() {
1134
      LEMON_ASSERT(false,
1135
        "This graph structure does not support node insertion");
1136
      return INVALID; // avoid warning
1137
    }
1138

	
1130 1139
    Edge addEdge(const Node& u, const Node& v) {
1131 1140
      int n = arcs.size();
1132 1141
      arcs.push_back(ArcT());
1133 1142
      arcs.push_back(ArcT());
1134 1143

	
1135 1144
      arcs[n].target = u;
1136 1145
      arcs[n | 1].target = v;
1137 1146

	
1138
      arcs[n].next_out = (*nodes)[v].first_out;
1139
      (*nodes)[v].first_out = n;
1147
      arcs[n].next_out = (*_nodes)[v].first_out;
1148
      (*_nodes)[v].first_out = n;
1140 1149

	
1141
      arcs[n | 1].next_out = (*nodes)[u].first_out;
1142
      (*nodes)[u].first_out = (n | 1);
1150
      arcs[n | 1].next_out = (*_nodes)[u].first_out;
1151
      (*_nodes)[u].first_out = (n | 1);
1143 1152

	
1144 1153
      return Edge(n / 2);
1145 1154
    }
1146 1155

	
1147 1156
    void clear() {
1148 1157
      Node node;
1149 1158
      for (first(node); node != INVALID; next(node)) {
1150
        (*nodes)[node].first_out = -1;
1159
        (*_nodes)[node].first_out = -1;
1151 1160
      }
1152 1161
      arcs.clear();
1153 1162
    }
1154 1163

	
1155 1164
    void first(Node& node) const {
1156
      graph->first(node);
1165
      _graph->first(node);
1157 1166
    }
1158 1167

	
1159 1168
    void next(Node& node) const {
1160
      graph->next(node);
1169
      _graph->next(node);
1161 1170
    }
1162 1171

	
1163 1172
    void first(Arc& arc) const {
1164 1173
      arc.id = arcs.size() - 1;
1165 1174
    }
1166 1175

	
1167
    void next(Arc& arc) const {
1176
    static void next(Arc& arc) {
1168 1177
      --arc.id;
1169 1178
    }
1170 1179

	
1171 1180
    void first(Edge& arc) const {
1172 1181
      arc.id = arcs.size() / 2 - 1;
1173 1182
    }
1174 1183

	
1175
    void next(Edge& arc) const {
1184
    static void next(Edge& arc) {
1176 1185
      --arc.id;
1177 1186
    }
1178 1187

	
1179 1188
    void firstOut(Arc& arc, const Node& node) const {
1180
      arc.id = (*nodes)[node].first_out;
1189
      arc.id = (*_nodes)[node].first_out;
1181 1190
    }
1182 1191

	
1183 1192
    void nextOut(Arc& arc) const {
1184 1193
      arc.id = arcs[arc.id].next_out;
1185 1194
    }
1186 1195

	
1187 1196
    void firstIn(Arc& arc, const Node& node) const {
1188
      arc.id = (((*nodes)[node].first_out) ^ 1);
1197
      arc.id = (((*_nodes)[node].first_out) ^ 1);
1189 1198
      if (arc.id == -2) arc.id = -1;
1190 1199
    }
1191 1200

	
1192 1201
    void nextIn(Arc& arc) const {
1193 1202
      arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
1194 1203
      if (arc.id == -2) arc.id = -1;
1195 1204
    }
1196 1205

	
1197 1206
    void firstInc(Edge &arc, bool& dir, const Node& node) const {
1198
      int de = (*nodes)[node].first_out;
1207
      int de = (*_nodes)[node].first_out;
1199 1208
      if (de != -1 ) {
1200 1209
        arc.id = de / 2;
1201 1210
        dir = ((de & 1) == 1);
1202 1211
      } else {
1203 1212
        arc.id = -1;
1204 1213
        dir = true;
1205 1214
      }
1206 1215
    }
1207 1216
    void nextInc(Edge &arc, bool& dir) const {
1208 1217
      int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
1209 1218
      if (de != -1 ) {
1210 1219
        arc.id = de / 2;
1211 1220
        dir = ((de & 1) == 1);
1212 1221
      } else {
1213 1222
        arc.id = -1;
1214 1223
        dir = true;
1215 1224
      }
1216 1225
    }
1217 1226

	
1218 1227
    static bool direction(Arc arc) {
1219 1228
      return (arc.id & 1) == 1;
1220 1229
    }
1221 1230

	
1222 1231
    static Arc direct(Edge edge, bool dir) {
1223 1232
      return Arc(edge.id * 2 + (dir ? 1 : 0));
1224 1233
    }
1225 1234

	
1226
    int id(Node node) const { return graph->id(node); }
1235
    int id(Node node) const { return _graph->id(node); }
1227 1236
    static int id(Arc arc) { return arc.id; }
1228 1237
    static int id(Edge arc) { return arc.id; }
1229 1238

	
1230
    Node nodeFromId(int id) const { return graph->nodeFromId(id); }
1239
    Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
1231 1240
    static Arc arcFromId(int id) { return Arc(id); }
1232 1241
    static Edge edgeFromId(int id) { return Edge(id);}
1233 1242

	
1234
    int maxNodeId() const { return graph->maxNodeId(); };
1243
    int maxNodeId() const { return _graph->maxNodeId(); };
1235 1244
    int maxArcId() const { return arcs.size() - 1; }
1236 1245
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
1237 1246

	
1238 1247
    Node source(Arc e) const { return arcs[e.id ^ 1].target; }
1239 1248
    Node target(Arc e) const { return arcs[e.id].target; }
1240 1249

	
1241 1250
    Node u(Edge e) const { return arcs[2 * e.id].target; }
1242 1251
    Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
1243 1252

	
1244
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
1253
    typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
1245 1254

	
1246 1255
    NodeNotifier& notifier(Node) const {
1247
      return graph->notifier(Node());
1256
      return _graph->notifier(Node());
1248 1257
    }
1249 1258

	
1250
    template <typename _Value>
1251
    class NodeMap : public Graph::template NodeMap<_Value> {
1259
    template <typename V>
1260
    class NodeMap : public GR::template NodeMap<V> {
1261
      typedef typename GR::template NodeMap<V> Parent;
1262

	
1252 1263
    public:
1253 1264

	
1254
      typedef typename _Graph::template NodeMap<_Value> Parent;
1265
      explicit NodeMap(const SmartEdgeSetBase<GR>& arcset)
1266
        : Parent(*arcset._graph) { }
1255 1267

	
1256
      explicit NodeMap(const SmartEdgeSetBase<Graph>& arcset)
1257
        : Parent(*arcset.graph) { }
1258

	
1259
      NodeMap(const SmartEdgeSetBase<Graph>& arcset, const _Value& value)
1260
        : Parent(*arcset.graph, value) { }
1268
      NodeMap(const SmartEdgeSetBase<GR>& arcset, const V& value)
1269
        : Parent(*arcset._graph, value) { }
1261 1270

	
1262 1271
      NodeMap& operator=(const NodeMap& cmap) {
1263 1272
        return operator=<NodeMap>(cmap);
1264 1273
      }
1265 1274

	
1266 1275
      template <typename CMap>
1267 1276
      NodeMap& operator=(const CMap& cmap) {
1268 1277
        Parent::operator=(cmap);
1269 1278
        return *this;
1270 1279
      }
1271 1280
    };
1272 1281

	
1273 1282
  };
1274 1283

	
1275
  /// \ingroup semi_adaptors
1284
  /// \ingroup graphs
1276 1285
  ///
1277 1286
  /// \brief Graph using a node set of another digraph or graph and an
1278 1287
  /// own edge set.
1279 1288
  ///
1280 1289
  /// This structure can be used to establish another graph over a
1281 1290
  /// node set of an existing one. This class uses the same Node type
1282 1291
  /// as the underlying graph, and each valid node of the original
1283 1292
  /// graph is valid in this arc set, therefore the node objects of
1284 1293
  /// the original graph can be used directly with this class. The
1285 1294
  /// node handling functions (id handling, observing, and iterators)
1286 1295
  /// works equivalently as in the original graph.
1287 1296
  ///
1288
  /// \param _Graph The type of the graph which shares its node set
1297
  /// \param GR The type of the graph which shares its node set
1289 1298
  /// with this class. Its interface must conform to the
1290 1299
  /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
1291 1300
  ///  concept.
1292 1301
  ///
1293 1302
  /// This implementation is slightly faster than the \c ListEdgeSet,
1294 1303
  /// because it uses continuous storage for edges and it uses just
1295 1304
  /// single-linked lists for enumerate incident edges. Therefore the
1296 1305
  /// edges cannot be erased from the edge sets.
1297 1306
  ///
1298 1307
  /// \warning If a node is erased from the underlying graph and this
1299 1308
  /// node is incident to one edge in the edge set, then the edge set
1300 1309
  /// is invalidated, and it cannot be used anymore. The validity can
1301 1310
  /// be checked with the \c valid() member function.
1302 1311
  ///
1303
  /// This class is fully conform to the \ref concepts::Graph
1312
  /// This class fully conforms to the \ref concepts::Graph
1304 1313
  /// "Graph" concept.
1305
  template <typename _Graph>
1306
  class SmartEdgeSet : public EdgeSetExtender<SmartEdgeSetBase<_Graph> > {
1314
  template <typename GR>
1315
  class SmartEdgeSet : public EdgeSetExtender<SmartEdgeSetBase<GR> > {
1316
    typedef EdgeSetExtender<SmartEdgeSetBase<GR> > Parent;
1307 1317

	
1308 1318
  public:
1309 1319

	
1310
    typedef EdgeSetExtender<SmartEdgeSetBase<_Graph> > Parent;
1311

	
1312 1320
    typedef typename Parent::Node Node;
1313 1321
    typedef typename Parent::Arc Arc;
1314 1322
    typedef typename Parent::Edge Edge;
1315 1323

	
1316
    typedef _Graph Graph;
1317

	
1318 1324
  protected:
1319 1325

	
1320 1326
    typedef typename Parent::NodesImplBase NodesImplBase;
1321 1327

	
1322 1328
    void eraseNode(const Node& node) {
1323 1329
      if (typename Parent::IncEdgeIt(*this, node) == INVALID) {
1324 1330
        return;
1325 1331
      }
1326 1332
      throw typename NodesImplBase::Notifier::ImmediateDetach();
1327 1333
    }
1328 1334

	
1329 1335
    void clearNodes() {
1330 1336
      Parent::clear();
1331 1337
    }
1332 1338

	
1333 1339
    class NodesImpl : public NodesImplBase {
1334
    public:
1335 1340
      typedef NodesImplBase Parent;
1336 1341

	
1337
      NodesImpl(const Graph& graph, SmartEdgeSet& arcset)
1342
    public:
1343
      NodesImpl(const GR& graph, SmartEdgeSet& arcset)
1338 1344
        : Parent(graph), _arcset(arcset) {}
1339 1345

	
1340 1346
      virtual ~NodesImpl() {}
1341 1347

	
1342 1348
      bool attached() const {
1343 1349
        return Parent::attached();
1344 1350
      }
1345 1351

	
1346 1352
    protected:
1347 1353

	
1348 1354
      virtual void erase(const Node& node) {
1349 1355
        try {
1350 1356
          _arcset.eraseNode(node);
1351 1357
          Parent::erase(node);
1352 1358
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1353 1359
          Parent::clear();
1354 1360
          throw;
1355 1361
        }
1356 1362
      }
1357 1363
      virtual void erase(const std::vector<Node>& nodes) {
1358 1364
        try {
1359 1365
          for (int i = 0; i < int(nodes.size()); ++i) {
1360 1366
            _arcset.eraseNode(nodes[i]);
1361 1367
          }
1362 1368
          Parent::erase(nodes);
1363 1369
        } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1364 1370
          Parent::clear();
1365 1371
          throw;
1366 1372
        }
1367 1373
      }
1368 1374
      virtual void clear() {
1369 1375
        _arcset.clearNodes();
1370 1376
        Parent::clear();
1371 1377
      }
1372 1378

	
1373 1379
    private:
1374 1380
      SmartEdgeSet& _arcset;
1375 1381
    };
1376 1382

	
1377
    NodesImpl nodes;
1383
    NodesImpl _nodes;
1378 1384

	
1379 1385
  public:
1380 1386

	
1381 1387
    /// \brief Constructor of the EdgeSet.
1382 1388
    ///
1383 1389
    /// Constructor of the EdgeSet.
1384
    SmartEdgeSet(const Graph& graph) : nodes(graph, *this) {
1385
      Parent::initalize(graph, nodes);
1390
    SmartEdgeSet(const GR& graph) : _nodes(graph, *this) {
1391
      Parent::initalize(graph, _nodes);
1386 1392
    }
1387 1393

	
1388 1394
    /// \brief Add a new edge to the graph.
1389 1395
    ///
1390 1396
    /// Add a new edge to the graph with node \c u
1391 1397
    /// and node \c v endpoints.
1392
    /// \return the new edge.
1398
    /// \return The new edge.
1393 1399
    Edge addEdge(const Node& u, const Node& v) {
1394 1400
      return Parent::addEdge(u, v);
1395 1401
    }
1396 1402

	
1397 1403
    /// \brief Validity check
1398 1404
    ///
1399 1405
    /// This functions gives back false if the EdgeSet is
1400 1406
    /// invalidated. It occurs when a node in the underlying graph is
1401 1407
    /// erased and it is not isolated in the EdgeSet.
1402 1408
    bool valid() const {
1403
      return nodes.attached();
1409
      return _nodes.attached();
1404 1410
    }
1405 1411

	
1406 1412
  };
1407 1413

	
1408 1414
}
1409 1415

	
1410 1416
#endif
Ignore white space 6 line context
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-2009
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_ELEVATOR_H
20 20
#define LEMON_ELEVATOR_H
21 21

	
22 22
///\ingroup auxdat
23 23
///\file
24 24
///\brief Elevator class
25 25
///
26 26
///Elevator class implements an efficient data structure
27 27
///for labeling items in push-relabel type algorithms.
28 28
///
29 29

	
30
#include <lemon/core.h>
30 31
#include <lemon/bits/traits.h>
31 32

	
32 33
namespace lemon {
33 34

	
34 35
  ///Class for handling "labels" in push-relabel type algorithms.
35 36

	
36 37
  ///A class for handling "labels" in push-relabel type algorithms.
37 38
  ///
38 39
  ///\ingroup auxdat
39 40
  ///Using this class you can assign "labels" (nonnegative integer numbers)
40 41
  ///to the edges or nodes of a graph, manipulate and query them through
41 42
  ///operations typically arising in "push-relabel" type algorithms.
42 43
  ///
43 44
  ///Each item is either \em active or not, and you can also choose a
44 45
  ///highest level active item.
45 46
  ///
46 47
  ///\sa LinkedElevator
47 48
  ///
48
  ///\param Graph Type of the underlying graph.
49
  ///\param Item Type of the items the data is assigned to (Graph::Node,
50
  ///Graph::Arc, Graph::Edge).
51
  template<class Graph, class Item>
49
  ///\param GR Type of the underlying graph.
50
  ///\param Item Type of the items the data is assigned to (\c GR::Node,
51
  ///\c GR::Arc or \c GR::Edge).
52
  template<class GR, class Item>
52 53
  class Elevator
53 54
  {
54 55
  public:
55 56

	
56 57
    typedef Item Key;
57 58
    typedef int Value;
58 59

	
59 60
  private:
60 61

	
61 62
    typedef Item *Vit;
62
    typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap;
63
    typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap;
63
    typedef typename ItemSetTraits<GR,Item>::template Map<Vit>::Type VitMap;
64
    typedef typename ItemSetTraits<GR,Item>::template Map<int>::Type IntMap;
64 65

	
65
    const Graph &_g;
66
    const GR &_g;
66 67
    int _max_level;
67 68
    int _item_num;
68 69
    VitMap _where;
69 70
    IntMap _level;
70 71
    std::vector<Item> _items;
71 72
    std::vector<Vit> _first;
72 73
    std::vector<Vit> _last_active;
73 74

	
74 75
    int _highest_active;
75 76

	
76 77
    void copy(Item i, Vit p)
77 78
    {
78
      _where.set(*p=i,p);
79
      _where[*p=i] = p;
79 80
    }
80 81
    void copy(Vit s, Vit p)
81 82
    {
82 83
      if(s!=p)
83 84
        {
84 85
          Item i=*s;
85 86
          *p=i;
86
          _where.set(i,p);
87
          _where[i] = p;
87 88
        }
88 89
    }
89 90
    void swap(Vit i, Vit j)
90 91
    {
91 92
      Item ti=*i;
92 93
      Vit ct = _where[ti];
93
      _where.set(ti,_where[*i=*j]);
94
      _where.set(*j,ct);
94
      _where[ti] = _where[*i=*j];
95
      _where[*j] = ct;
95 96
      *j=ti;
96 97
    }
97 98

	
98 99
  public:
99 100

	
100 101
    ///Constructor with given maximum level.
101 102

	
102 103
    ///Constructor with given maximum level.
103 104
    ///
104 105
    ///\param graph The underlying graph.
105 106
    ///\param max_level The maximum allowed level.
106 107
    ///Set the range of the possible labels to <tt>[0..max_level]</tt>.
107
    Elevator(const Graph &graph,int max_level) :
108
    Elevator(const GR &graph,int max_level) :
108 109
      _g(graph),
109 110
      _max_level(max_level),
110 111
      _item_num(_max_level),
111 112
      _where(graph),
112 113
      _level(graph,0),
113 114
      _items(_max_level),
114 115
      _first(_max_level+2),
115 116
      _last_active(_max_level+2),
116 117
      _highest_active(-1) {}
117 118
    ///Constructor.
118 119

	
119 120
    ///Constructor.
120 121
    ///
121 122
    ///\param graph The underlying graph.
122 123
    ///Set the range of the possible labels to <tt>[0..max_level]</tt>,
123 124
    ///where \c max_level is equal to the number of labeled items in the graph.
124
    Elevator(const Graph &graph) :
125
    Elevator(const GR &graph) :
125 126
      _g(graph),
126
      _max_level(countItems<Graph, Item>(graph)),
127
      _max_level(countItems<GR, Item>(graph)),
127 128
      _item_num(_max_level),
128 129
      _where(graph),
129 130
      _level(graph,0),
130 131
      _items(_max_level),
131 132
      _first(_max_level+2),
132 133
      _last_active(_max_level+2),
133 134
      _highest_active(-1)
134 135
    {
135 136
    }
136 137

	
137 138
    ///Activate item \c i.
138 139

	
139 140
    ///Activate item \c i.
140 141
    ///\pre Item \c i shouldn't be active before.
141 142
    void activate(Item i)
142 143
    {
143 144
      const int l=_level[i];
144 145
      swap(_where[i],++_last_active[l]);
145 146
      if(l>_highest_active) _highest_active=l;
146 147
    }
147 148

	
148 149
    ///Deactivate item \c i.
149 150

	
150 151
    ///Deactivate item \c i.
151 152
    ///\pre Item \c i must be active before.
152 153
    void deactivate(Item i)
153 154
    {
154 155
      swap(_where[i],_last_active[_level[i]]--);
155 156
      while(_highest_active>=0 &&
156 157
            _last_active[_highest_active]<_first[_highest_active])
157 158
        _highest_active--;
158 159
    }
159 160

	
160 161
    ///Query whether item \c i is active
161 162
    bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; }
162 163

	
163 164
    ///Return the level of item \c i.
164 165
    int operator[](Item i) const { return _level[i]; }
165 166

	
166 167
    ///Return the number of items on level \c l.
167 168
    int onLevel(int l) const
168 169
    {
169 170
      return _first[l+1]-_first[l];
170 171
    }
171 172
    ///Return true if level \c l is empty.
172 173
    bool emptyLevel(int l) const
173 174
    {
174 175
      return _first[l+1]-_first[l]==0;
175 176
    }
176 177
    ///Return the number of items above level \c l.
177 178
    int aboveLevel(int l) const
178 179
    {
179 180
      return _first[_max_level+1]-_first[l+1];
180 181
    }
181 182
    ///Return the number of active items on level \c l.
182 183
    int activesOnLevel(int l) const
183 184
    {
184 185
      return _last_active[l]-_first[l]+1;
185 186
    }
186 187
    ///Return true if there is no active item on level \c l.
187 188
    bool activeFree(int l) const
188 189
    {
189 190
      return _last_active[l]<_first[l];
190 191
    }
191 192
    ///Return the maximum allowed level.
192 193
    int maxLevel() const
193 194
    {
194 195
      return _max_level;
195 196
    }
196 197

	
197 198
    ///\name Highest Active Item
198 199
    ///Functions for working with the highest level
199 200
    ///active item.
200 201

	
201 202
    ///@{
202 203

	
203 204
    ///Return a highest level active item.
204 205

	
205 206
    ///Return a highest level active item or INVALID if there is no active
206 207
    ///item.
207 208
    Item highestActive() const
208 209
    {
209 210
      return _highest_active>=0?*_last_active[_highest_active]:INVALID;
210 211
    }
211 212

	
212 213
    ///Return the highest active level.
213 214

	
214 215
    ///Return the level of the highest active item or -1 if there is no active
215 216
    ///item.
216 217
    int highestActiveLevel() const
217 218
    {
218 219
      return _highest_active;
219 220
    }
220 221

	
221 222
    ///Lift the highest active item by one.
222 223

	
223 224
    ///Lift the item returned by highestActive() by one.
224 225
    ///
225 226
    void liftHighestActive()
226 227
    {
227 228
      Item it = *_last_active[_highest_active];
228
      _level.set(it,_level[it]+1);
229
      ++_level[it];
229 230
      swap(_last_active[_highest_active]--,_last_active[_highest_active+1]);
230 231
      --_first[++_highest_active];
231 232
    }
232 233

	
233 234
    ///Lift the highest active item to the given level.
234 235

	
235 236
    ///Lift the item returned by highestActive() to level \c new_level.
236 237
    ///
237 238
    ///\warning \c new_level must be strictly higher
238 239
    ///than the current level.
239 240
    ///
240 241
    void liftHighestActive(int new_level)
241 242
    {
242 243
      const Item li = *_last_active[_highest_active];
243 244

	
244 245
      copy(--_first[_highest_active+1],_last_active[_highest_active]--);
245 246
      for(int l=_highest_active+1;l<new_level;l++)
246 247
        {
247 248
          copy(--_first[l+1],_first[l]);
248 249
          --_last_active[l];
249 250
        }
250 251
      copy(li,_first[new_level]);
251
      _level.set(li,new_level);
252
      _level[li] = new_level;
252 253
      _highest_active=new_level;
253 254
    }
254 255

	
255 256
    ///Lift the highest active item to the top level.
256 257

	
257 258
    ///Lift the item returned by highestActive() to the top level and
258 259
    ///deactivate it.
259 260
    void liftHighestActiveToTop()
260 261
    {
261 262
      const Item li = *_last_active[_highest_active];
262 263

	
263 264
      copy(--_first[_highest_active+1],_last_active[_highest_active]--);
264 265
      for(int l=_highest_active+1;l<_max_level;l++)
265 266
        {
266 267
          copy(--_first[l+1],_first[l]);
267 268
          --_last_active[l];
268 269
        }
269 270
      copy(li,_first[_max_level]);
270 271
      --_last_active[_max_level];
271
      _level.set(li,_max_level);
272
      _level[li] = _max_level;
272 273

	
273 274
      while(_highest_active>=0 &&
274 275
            _last_active[_highest_active]<_first[_highest_active])
275 276
        _highest_active--;
276 277
    }
277 278

	
278 279
    ///@}
279 280

	
280 281
    ///\name Active Item on Certain Level
281 282
    ///Functions for working with the active items.
282 283

	
283 284
    ///@{
284 285

	
285 286
    ///Return an active item on level \c l.
286 287

	
287 288
    ///Return an active item on level \c l or \ref INVALID if there is no such
288 289
    ///an item. (\c l must be from the range [0...\c max_level].
289 290
    Item activeOn(int l) const
290 291
    {
291 292
      return _last_active[l]>=_first[l]?*_last_active[l]:INVALID;
292 293
    }
293 294

	
294 295
    ///Lift the active item returned by \c activeOn(level) by one.
295 296

	
296 297
    ///Lift the active item returned by \ref activeOn() "activeOn(level)"
297 298
    ///by one.
298 299
    Item liftActiveOn(int level)
299 300
    {
300 301
      Item it =*_last_active[level];
301
      _level.set(it,_level[it]+1);
302
      ++_level[it];
302 303
      swap(_last_active[level]--, --_first[level+1]);
303 304
      if (level+1>_highest_active) ++_highest_active;
304 305
    }
305 306

	
306 307
    ///Lift the active item returned by \c activeOn(level) to the given level.
307 308

	
308 309
    ///Lift the active item returned by \ref activeOn() "activeOn(level)"
309 310
    ///to the given level.
310 311
    void liftActiveOn(int level, int new_level)
311 312
    {
312 313
      const Item ai = *_last_active[level];
313 314

	
314 315
      copy(--_first[level+1], _last_active[level]--);
315 316
      for(int l=level+1;l<new_level;l++)
316 317
        {
317 318
          copy(_last_active[l],_first[l]);
318 319
          copy(--_first[l+1], _last_active[l]--);
319 320
        }
320 321
      copy(ai,_first[new_level]);
321
      _level.set(ai,new_level);
322
      _level[ai] = new_level;
322 323
      if (new_level>_highest_active) _highest_active=new_level;
323 324
    }
324 325

	
325 326
    ///Lift the active item returned by \c activeOn(level) to the top level.
326 327

	
327 328
    ///Lift the active item returned by \ref activeOn() "activeOn(level)"
328 329
    ///to the top level and deactivate it.
329 330
    void liftActiveToTop(int level)
330 331
    {
331 332
      const Item ai = *_last_active[level];
332 333

	
333 334
      copy(--_first[level+1],_last_active[level]--);
334 335
      for(int l=level+1;l<_max_level;l++)
335 336
        {
336 337
          copy(_last_active[l],_first[l]);
337 338
          copy(--_first[l+1], _last_active[l]--);
338 339
        }
339 340
      copy(ai,_first[_max_level]);
340 341
      --_last_active[_max_level];
341
      _level.set(ai,_max_level);
342
      _level[ai] = _max_level;
342 343

	
343 344
      if (_highest_active==level) {
344 345
        while(_highest_active>=0 &&
345 346
              _last_active[_highest_active]<_first[_highest_active])
346 347
          _highest_active--;
347 348
      }
348 349
    }
349 350

	
350 351
    ///@}
351 352

	
352 353
    ///Lift an active item to a higher level.
353 354

	
354 355
    ///Lift an active item to a higher level.
355 356
    ///\param i The item to be lifted. It must be active.
356 357
    ///\param new_level The new level of \c i. It must be strictly higher
357 358
    ///than the current level.
358 359
    ///
359 360
    void lift(Item i, int new_level)
360 361
    {
361 362
      const int lo = _level[i];
362 363
      const Vit w = _where[i];
363 364

	
364 365
      copy(_last_active[lo],w);
365 366
      copy(--_first[lo+1],_last_active[lo]--);
366 367
      for(int l=lo+1;l<new_level;l++)
367 368
        {
368 369
          copy(_last_active[l],_first[l]);
369 370
          copy(--_first[l+1],_last_active[l]--);
370 371
        }
371 372
      copy(i,_first[new_level]);
372
      _level.set(i,new_level);
373
      _level[i] = new_level;
373 374
      if(new_level>_highest_active) _highest_active=new_level;
374 375
    }
375 376

	
376 377
    ///Move an inactive item to the top but one level (in a dirty way).
377 378

	
378 379
    ///This function moves an inactive item from the top level to the top
379 380
    ///but one level (in a dirty way).
380 381
    ///\warning It makes the underlying datastructure corrupt, so use it
381 382
    ///only if you really know what it is for.
382 383
    ///\pre The item is on the top level.
383 384
    void dirtyTopButOne(Item i) {
384
      _level.set(i,_max_level - 1);
385
      _level[i] = _max_level - 1;
385 386
    }
386 387

	
387 388
    ///Lift all items on and above the given level to the top level.
388 389

	
389 390
    ///This function lifts all items on and above level \c l to the top
390 391
    ///level and deactivates them.
391 392
    void liftToTop(int l)
392 393
    {
393 394
      const Vit f=_first[l];
394 395
      const Vit tl=_first[_max_level];
395 396
      for(Vit i=f;i!=tl;++i)
396
        _level.set(*i,_max_level);
397
        _level[*i] = _max_level;
397 398
      for(int i=l;i<=_max_level;i++)
398 399
        {
399 400
          _first[i]=f;
400 401
          _last_active[i]=f-1;
401 402
        }
402 403
      for(_highest_active=l-1;
403 404
          _highest_active>=0 &&
404 405
            _last_active[_highest_active]<_first[_highest_active];
405 406
          _highest_active--) ;
406 407
    }
407 408

	
408 409
  private:
409 410
    int _init_lev;
410 411
    Vit _init_num;
411 412

	
412 413
  public:
413 414

	
414 415
    ///\name Initialization
415 416
    ///Using these functions you can initialize the levels of the items.
416 417
    ///\n
417 418
    ///The initialization must be started with calling \c initStart().
418 419
    ///Then the items should be listed level by level starting with the
419 420
    ///lowest one (level 0) using \c initAddItem() and \c initNewLevel().
420 421
    ///Finally \c initFinish() must be called.
421 422
    ///The items not listed are put on the highest level.
422 423
    ///@{
423 424

	
424 425
    ///Start the initialization process.
425 426
    void initStart()
426 427
    {
427 428
      _init_lev=0;
428 429
      _init_num=&_items[0];
429 430
      _first[0]=&_items[0];
430 431
      _last_active[0]=&_items[0]-1;
431 432
      Vit n=&_items[0];
432
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i)
433
      for(typename ItemSetTraits<GR,Item>::ItemIt i(_g);i!=INVALID;++i)
433 434
        {
434 435
          *n=i;
435
          _where.set(i,n);
436
          _level.set(i,_max_level);
436
          _where[i] = n;
437
          _level[i] = _max_level;
437 438
          ++n;
438 439
        }
439 440
    }
440 441

	
441 442
    ///Add an item to the current level.
442 443
    void initAddItem(Item i)
443 444
    {
444 445
      swap(_where[i],_init_num);
445
      _level.set(i,_init_lev);
446
      _level[i] = _init_lev;
446 447
      ++_init_num;
447 448
    }
448 449

	
449 450
    ///Start a new level.
450 451

	
451 452
    ///Start a new level.
452 453
    ///It shouldn't be used before the items on level 0 are listed.
453 454
    void initNewLevel()
454 455
    {
455 456
      _init_lev++;
456 457
      _first[_init_lev]=_init_num;
457 458
      _last_active[_init_lev]=_init_num-1;
458 459
    }
459 460

	
460 461
    ///Finalize the initialization process.
461 462
    void initFinish()
462 463
    {
463 464
      for(_init_lev++;_init_lev<=_max_level;_init_lev++)
464 465
        {
465 466
          _first[_init_lev]=_init_num;
466 467
          _last_active[_init_lev]=_init_num-1;
467 468
        }
468 469
      _first[_max_level+1]=&_items[0]+_item_num;
469 470
      _last_active[_max_level+1]=&_items[0]+_item_num-1;
470 471
      _highest_active = -1;
471 472
    }
472 473

	
473 474
    ///@}
474 475

	
475 476
  };
476 477

	
477 478
  ///Class for handling "labels" in push-relabel type algorithms.
478 479

	
479 480
  ///A class for handling "labels" in push-relabel type algorithms.
480 481
  ///
481 482
  ///\ingroup auxdat
482 483
  ///Using this class you can assign "labels" (nonnegative integer numbers)
483 484
  ///to the edges or nodes of a graph, manipulate and query them through
484 485
  ///operations typically arising in "push-relabel" type algorithms.
485 486
  ///
486 487
  ///Each item is either \em active or not, and you can also choose a
487 488
  ///highest level active item.
488 489
  ///
489 490
  ///\sa Elevator
490 491
  ///
491
  ///\param Graph Type of the underlying graph.
492
  ///\param Item Type of the items the data is assigned to (Graph::Node,
493
  ///Graph::Arc, Graph::Edge).
494
  template <class Graph, class Item>
492
  ///\param GR Type of the underlying graph.
493
  ///\param Item Type of the items the data is assigned to (\c GR::Node,
494
  ///\c GR::Arc or \c GR::Edge).
495
  template <class GR, class Item>
495 496
  class LinkedElevator {
496 497
  public:
497 498

	
498 499
    typedef Item Key;
499 500
    typedef int Value;
500 501

	
501 502
  private:
502 503

	
503
    typedef typename ItemSetTraits<Graph,Item>::
504
    typedef typename ItemSetTraits<GR,Item>::
504 505
    template Map<Item>::Type ItemMap;
505
    typedef typename ItemSetTraits<Graph,Item>::
506
    typedef typename ItemSetTraits<GR,Item>::
506 507
    template Map<int>::Type IntMap;
507
    typedef typename ItemSetTraits<Graph,Item>::
508
    typedef typename ItemSetTraits<GR,Item>::
508 509
    template Map<bool>::Type BoolMap;
509 510

	
510
    const Graph &_graph;
511
    const GR &_graph;
511 512
    int _max_level;
512 513
    int _item_num;
513 514
    std::vector<Item> _first, _last;
514 515
    ItemMap _prev, _next;
515 516
    int _highest_active;
516 517
    IntMap _level;
517 518
    BoolMap _active;
518 519

	
519 520
  public:
520 521
    ///Constructor with given maximum level.
521 522

	
522 523
    ///Constructor with given maximum level.
523 524
    ///
524 525
    ///\param graph The underlying graph.
525 526
    ///\param max_level The maximum allowed level.
526 527
    ///Set the range of the possible labels to <tt>[0..max_level]</tt>.
527
    LinkedElevator(const Graph& graph, int max_level)
528
    LinkedElevator(const GR& graph, int max_level)
528 529
      : _graph(graph), _max_level(max_level), _item_num(_max_level),
529 530
        _first(_max_level + 1), _last(_max_level + 1),
530 531
        _prev(graph), _next(graph),
531 532
        _highest_active(-1), _level(graph), _active(graph) {}
532 533

	
533 534
    ///Constructor.
534 535

	
535 536
    ///Constructor.
536 537
    ///
537 538
    ///\param graph The underlying graph.
538 539
    ///Set the range of the possible labels to <tt>[0..max_level]</tt>,
539 540
    ///where \c max_level is equal to the number of labeled items in the graph.
540
    LinkedElevator(const Graph& graph)
541
      : _graph(graph), _max_level(countItems<Graph, Item>(graph)),
541
    LinkedElevator(const GR& graph)
542
      : _graph(graph), _max_level(countItems<GR, Item>(graph)),
542 543
        _item_num(_max_level),
543 544
        _first(_max_level + 1), _last(_max_level + 1),
544 545
        _prev(graph, INVALID), _next(graph, INVALID),
545 546
        _highest_active(-1), _level(graph), _active(graph) {}
546 547

	
547 548

	
548 549
    ///Activate item \c i.
549 550

	
550 551
    ///Activate item \c i.
551 552
    ///\pre Item \c i shouldn't be active before.
552 553
    void activate(Item i) {
553
      _active.set(i, true);
554
      _active[i] = true;
554 555

	
555 556
      int level = _level[i];
556 557
      if (level > _highest_active) {
557 558
        _highest_active = level;
558 559
      }
559 560

	
560 561
      if (_prev[i] == INVALID || _active[_prev[i]]) return;
561 562
      //unlace
562
      _next.set(_prev[i], _next[i]);
563
      _next[_prev[i]] = _next[i];
563 564
      if (_next[i] != INVALID) {
564
        _prev.set(_next[i], _prev[i]);
565
        _prev[_next[i]] = _prev[i];
565 566
      } else {
566 567
        _last[level] = _prev[i];
567 568
      }
568 569
      //lace
569
      _next.set(i, _first[level]);
570
      _prev.set(_first[level], i);
571
      _prev.set(i, INVALID);
570
      _next[i] = _first[level];
571
      _prev[_first[level]] = i;
572
      _prev[i] = INVALID;
572 573
      _first[level] = i;
573 574

	
574 575
    }
575 576

	
576 577
    ///Deactivate item \c i.
577 578

	
578 579
    ///Deactivate item \c i.
579 580
    ///\pre Item \c i must be active before.
580 581
    void deactivate(Item i) {
581
      _active.set(i, false);
582
      _active[i] = false;
582 583
      int level = _level[i];
583 584

	
584 585
      if (_next[i] == INVALID || !_active[_next[i]])
585 586
        goto find_highest_level;
586 587

	
587 588
      //unlace
588
      _prev.set(_next[i], _prev[i]);
589
      _prev[_next[i]] = _prev[i];
589 590
      if (_prev[i] != INVALID) {
590
        _next.set(_prev[i], _next[i]);
591
        _next[_prev[i]] = _next[i];
591 592
      } else {
592 593
        _first[_level[i]] = _next[i];
593 594
      }
594 595
      //lace
595
      _prev.set(i, _last[level]);
596
      _next.set(_last[level], i);
597
      _next.set(i, INVALID);
596
      _prev[i] = _last[level];
597
      _next[_last[level]] = i;
598
      _next[i] = INVALID;
598 599
      _last[level] = i;
599 600

	
600 601
    find_highest_level:
601 602
      if (level == _highest_active) {
602 603
        while (_highest_active >= 0 && activeFree(_highest_active))
603 604
          --_highest_active;
604 605
      }
605 606
    }
606 607

	
607 608
    ///Query whether item \c i is active
608 609
    bool active(Item i) const { return _active[i]; }
609 610

	
610 611
    ///Return the level of item \c i.
611 612
    int operator[](Item i) const { return _level[i]; }
612 613

	
613 614
    ///Return the number of items on level \c l.
614 615
    int onLevel(int l) const {
615 616
      int num = 0;
616 617
      Item n = _first[l];
617 618
      while (n != INVALID) {
618 619
        ++num;
619 620
        n = _next[n];
620 621
      }
621 622
      return num;
622 623
    }
623 624

	
624 625
    ///Return true if the level is empty.
625 626
    bool emptyLevel(int l) const {
626 627
      return _first[l] == INVALID;
627 628
    }
628 629

	
629 630
    ///Return the number of items above level \c l.
630 631
    int aboveLevel(int l) const {
631 632
      int num = 0;
632 633
      for (int level = l + 1; level < _max_level; ++level)
633 634
        num += onLevel(level);
634 635
      return num;
635 636
    }
636 637

	
637 638
    ///Return the number of active items on level \c l.
638 639
    int activesOnLevel(int l) const {
639 640
      int num = 0;
640 641
      Item n = _first[l];
641 642
      while (n != INVALID && _active[n]) {
642 643
        ++num;
643 644
        n = _next[n];
644 645
      }
645 646
      return num;
646 647
    }
647 648

	
648 649
    ///Return true if there is no active item on level \c l.
649 650
    bool activeFree(int l) const {
650 651
      return _first[l] == INVALID || !_active[_first[l]];
651 652
    }
652 653

	
653 654
    ///Return the maximum allowed level.
654 655
    int maxLevel() const {
655 656
      return _max_level;
656 657
    }
657 658

	
658 659
    ///\name Highest Active Item
659 660
    ///Functions for working with the highest level
660 661
    ///active item.
661 662

	
662 663
    ///@{
663 664

	
664 665
    ///Return a highest level active item.
665 666

	
666 667
    ///Return a highest level active item or INVALID if there is no active
667 668
    ///item.
668 669
    Item highestActive() const {
669 670
      return _highest_active >= 0 ? _first[_highest_active] : INVALID;
670 671
    }
671 672

	
672 673
    ///Return the highest active level.
673 674

	
674 675
    ///Return the level of the highest active item or -1 if there is no active
675 676
    ///item.
676 677
    int highestActiveLevel() const {
677 678
      return _highest_active;
678 679
    }
679 680

	
680 681
    ///Lift the highest active item by one.
681 682

	
682 683
    ///Lift the item returned by highestActive() by one.
683 684
    ///
684 685
    void liftHighestActive() {
685 686
      Item i = _first[_highest_active];
686 687
      if (_next[i] != INVALID) {
687
        _prev.set(_next[i], INVALID);
688
        _prev[_next[i]] = INVALID;
688 689
        _first[_highest_active] = _next[i];
689 690
      } else {
690 691
        _first[_highest_active] = INVALID;
691 692
        _last[_highest_active] = INVALID;
692 693
      }
693
      _level.set(i, ++_highest_active);
694
      _level[i] = ++_highest_active;
694 695
      if (_first[_highest_active] == INVALID) {
695 696
        _first[_highest_active] = i;
696 697
        _last[_highest_active] = i;
697
        _prev.set(i, INVALID);
698
        _next.set(i, INVALID);
698
        _prev[i] = INVALID;
699
        _next[i] = INVALID;
699 700
      } else {
700
        _prev.set(_first[_highest_active], i);
701
        _next.set(i, _first[_highest_active]);
701
        _prev[_first[_highest_active]] = i;
702
        _next[i] = _first[_highest_active];
702 703
        _first[_highest_active] = i;
703 704
      }
704 705
    }
705 706

	
706 707
    ///Lift the highest active item to the given level.
707 708

	
708 709
    ///Lift the item returned by highestActive() to level \c new_level.
709 710
    ///
710 711
    ///\warning \c new_level must be strictly higher
711 712
    ///than the current level.
712 713
    ///
713 714
    void liftHighestActive(int new_level) {
714 715
      Item i = _first[_highest_active];
715 716
      if (_next[i] != INVALID) {
716
        _prev.set(_next[i], INVALID);
717
        _prev[_next[i]] = INVALID;
717 718
        _first[_highest_active] = _next[i];
718 719
      } else {
719 720
        _first[_highest_active] = INVALID;
720 721
        _last[_highest_active] = INVALID;
721 722
      }
722
      _level.set(i, _highest_active = new_level);
723
      _level[i] = _highest_active = new_level;
723 724
      if (_first[_highest_active] == INVALID) {
724 725
        _first[_highest_active] = _last[_highest_active] = i;
725
        _prev.set(i, INVALID);
726
        _next.set(i, INVALID);
726
        _prev[i] = INVALID;
727
        _next[i] = INVALID;
727 728
      } else {
728
        _prev.set(_first[_highest_active], i);
729
        _next.set(i, _first[_highest_active]);
729
        _prev[_first[_highest_active]] = i;
730
        _next[i] = _first[_highest_active];
730 731
        _first[_highest_active] = i;
731 732
      }
732 733
    }
733 734

	
734 735
    ///Lift the highest active item to the top level.
735 736

	
736 737
    ///Lift the item returned by highestActive() to the top level and
737 738
    ///deactivate it.
738 739
    void liftHighestActiveToTop() {
739 740
      Item i = _first[_highest_active];
740
      _level.set(i, _max_level);
741
      _level[i] = _max_level;
741 742
      if (_next[i] != INVALID) {
742
        _prev.set(_next[i], INVALID);
743
        _prev[_next[i]] = INVALID;
743 744
        _first[_highest_active] = _next[i];
744 745
      } else {
745 746
        _first[_highest_active] = INVALID;
746 747
        _last[_highest_active] = INVALID;
747 748
      }
748 749
      while (_highest_active >= 0 && activeFree(_highest_active))
749 750
        --_highest_active;
750 751
    }
751 752

	
752 753
    ///@}
753 754

	
754 755
    ///\name Active Item on Certain Level
755 756
    ///Functions for working with the active items.
756 757

	
757 758
    ///@{
758 759

	
759 760
    ///Return an active item on level \c l.
760 761

	
761 762
    ///Return an active item on level \c l or \ref INVALID if there is no such
762 763
    ///an item. (\c l must be from the range [0...\c max_level].
763 764
    Item activeOn(int l) const
764 765
    {
765 766
      return _active[_first[l]] ? _first[l] : INVALID;
766 767
    }
767 768

	
768 769
    ///Lift the active item returned by \c activeOn(l) by one.
769 770

	
770 771
    ///Lift the active item returned by \ref activeOn() "activeOn(l)"
771 772
    ///by one.
772 773
    Item liftActiveOn(int l)
773 774
    {
774 775
      Item i = _first[l];
775 776
      if (_next[i] != INVALID) {
776
        _prev.set(_next[i], INVALID);
777
        _prev[_next[i]] = INVALID;
777 778
        _first[l] = _next[i];
778 779
      } else {
779 780
        _first[l] = INVALID;
780 781
        _last[l] = INVALID;
781 782
      }
782
      _level.set(i, ++l);
783
      _level[i] = ++l;
783 784
      if (_first[l] == INVALID) {
784 785
        _first[l] = _last[l] = i;
785
        _prev.set(i, INVALID);
786
        _next.set(i, INVALID);
786
        _prev[i] = INVALID;
787
        _next[i] = INVALID;
787 788
      } else {
788
        _prev.set(_first[l], i);
789
        _next.set(i, _first[l]);
789
        _prev[_first[l]] = i;
790
        _next[i] = _first[l];
790 791
        _first[l] = i;
791 792
      }
792 793
      if (_highest_active < l) {
793 794
        _highest_active = l;
794 795
      }
795 796
    }
796 797

	
797 798
    ///Lift the active item returned by \c activeOn(l) to the given level.
798 799

	
799 800
    ///Lift the active item returned by \ref activeOn() "activeOn(l)"
800 801
    ///to the given level.
801 802
    void liftActiveOn(int l, int new_level)
802 803
    {
803 804
      Item i = _first[l];
804 805
      if (_next[i] != INVALID) {
805
        _prev.set(_next[i], INVALID);
806
        _prev[_next[i]] = INVALID;
806 807
        _first[l] = _next[i];
807 808
      } else {
808 809
        _first[l] = INVALID;
809 810
        _last[l] = INVALID;
810 811
      }
811
      _level.set(i, l = new_level);
812
      _level[i] = l = new_level;
812 813
      if (_first[l] == INVALID) {
813 814
        _first[l] = _last[l] = i;
814
        _prev.set(i, INVALID);
815
        _next.set(i, INVALID);
815
        _prev[i] = INVALID;
816
        _next[i] = INVALID;
816 817
      } else {
817
        _prev.set(_first[l], i);
818
        _next.set(i, _first[l]);
818
        _prev[_first[l]] = i;
819
        _next[i] = _first[l];
819 820
        _first[l] = i;
820 821
      }
821 822
      if (_highest_active < l) {
822 823
        _highest_active = l;
823 824
      }
824 825
    }
825 826

	
826 827
    ///Lift the active item returned by \c activeOn(l) to the top level.
827 828

	
828 829
    ///Lift the active item returned by \ref activeOn() "activeOn(l)"
829 830
    ///to the top level and deactivate it.
830 831
    void liftActiveToTop(int l)
831 832
    {
832 833
      Item i = _first[l];
833 834
      if (_next[i] != INVALID) {
834
        _prev.set(_next[i], INVALID);
835
        _prev[_next[i]] = INVALID;
835 836
        _first[l] = _next[i];
836 837
      } else {
837 838
        _first[l] = INVALID;
838 839
        _last[l] = INVALID;
839 840
      }
840
      _level.set(i, _max_level);
841
      _level[i] = _max_level;
841 842
      if (l == _highest_active) {
842 843
        while (_highest_active >= 0 && activeFree(_highest_active))
843 844
          --_highest_active;
844 845
      }
845 846
    }
846 847

	
847 848
    ///@}
848 849

	
849 850
    /// \brief Lift an active item to a higher level.
850 851
    ///
851 852
    /// Lift an active item to a higher level.
852 853
    /// \param i The item to be lifted. It must be active.
853 854
    /// \param new_level The new level of \c i. It must be strictly higher
854 855
    /// than the current level.
855 856
    ///
856 857
    void lift(Item i, int new_level) {
857 858
      if (_next[i] != INVALID) {
858
        _prev.set(_next[i], _prev[i]);
859
        _prev[_next[i]] = _prev[i];
859 860
      } else {
860 861
        _last[new_level] = _prev[i];
861 862
      }
862 863
      if (_prev[i] != INVALID) {
863
        _next.set(_prev[i], _next[i]);
864
        _next[_prev[i]] = _next[i];
864 865
      } else {
865 866
        _first[new_level] = _next[i];
866 867
      }
867
      _level.set(i, new_level);
868
      _level[i] = new_level;
868 869
      if (_first[new_level] == INVALID) {
869 870
        _first[new_level] = _last[new_level] = i;
870
        _prev.set(i, INVALID);
871
        _next.set(i, INVALID);
871
        _prev[i] = INVALID;
872
        _next[i] = INVALID;
872 873
      } else {
873
        _prev.set(_first[new_level], i);
874
        _next.set(i, _first[new_level]);
874
        _prev[_first[new_level]] = i;
875
        _next[i] = _first[new_level];
875 876
        _first[new_level] = i;
876 877
      }
877 878
      if (_highest_active < new_level) {
878 879
        _highest_active = new_level;
879 880
      }
880 881
    }
881 882

	
882 883
    ///Move an inactive item to the top but one level (in a dirty way).
883 884

	
884 885
    ///This function moves an inactive item from the top level to the top
885 886
    ///but one level (in a dirty way).
886 887
    ///\warning It makes the underlying datastructure corrupt, so use it
887 888
    ///only if you really know what it is for.
888 889
    ///\pre The item is on the top level.
889 890
    void dirtyTopButOne(Item i) {
890
      _level.set(i, _max_level - 1);
891
      _level[i] = _max_level - 1;
891 892
    }
892 893

	
893 894
    ///Lift all items on and above the given level to the top level.
894 895

	
895 896
    ///This function lifts all items on and above level \c l to the top
896 897
    ///level and deactivates them.
897 898
    void liftToTop(int l)  {
898 899
      for (int i = l + 1; _first[i] != INVALID; ++i) {
899 900
        Item n = _first[i];
900 901
        while (n != INVALID) {
901
          _level.set(n, _max_level);
902
          _level[n] = _max_level;
902 903
          n = _next[n];
903 904
        }
904 905
        _first[i] = INVALID;
905 906
        _last[i] = INVALID;
906 907
      }
907 908
      if (_highest_active > l - 1) {
908 909
        _highest_active = l - 1;
909 910
        while (_highest_active >= 0 && activeFree(_highest_active))
910 911
          --_highest_active;
911 912
      }
912 913
    }
913 914

	
914 915
  private:
915 916

	
916 917
    int _init_level;
917 918

	
918 919
  public:
919 920

	
920 921
    ///\name Initialization
921 922
    ///Using these functions you can initialize the levels of the items.
922 923
    ///\n
923 924
    ///The initialization must be started with calling \c initStart().
924 925
    ///Then the items should be listed level by level starting with the
925 926
    ///lowest one (level 0) using \c initAddItem() and \c initNewLevel().
926 927
    ///Finally \c initFinish() must be called.
927 928
    ///The items not listed are put on the highest level.
928 929
    ///@{
929 930

	
930 931
    ///Start the initialization process.
931 932
    void initStart() {
932 933

	
933 934
      for (int i = 0; i <= _max_level; ++i) {
934 935
        _first[i] = _last[i] = INVALID;
935 936
      }
936 937
      _init_level = 0;
937
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_graph);
938
      for(typename ItemSetTraits<GR,Item>::ItemIt i(_graph);
938 939
          i != INVALID; ++i) {
939
        _level.set(i, _max_level);
940
        _active.set(i, false);
940
        _level[i] = _max_level;
941
        _active[i] = false;
941 942
      }
942 943
    }
943 944

	
944 945
    ///Add an item to the current level.
945 946
    void initAddItem(Item i) {
946
      _level.set(i, _init_level);
947
      _level[i] = _init_level;
947 948
      if (_last[_init_level] == INVALID) {
948 949
        _first[_init_level] = i;
949 950
        _last[_init_level] = i;
950
        _prev.set(i, INVALID);
951
        _next.set(i, INVALID);
951
        _prev[i] = INVALID;
952
        _next[i] = INVALID;
952 953
      } else {
953
        _prev.set(i, _last[_init_level]);
954
        _next.set(i, INVALID);
955
        _next.set(_last[_init_level], i);
954
        _prev[i] = _last[_init_level];
955
        _next[i] = INVALID;
956
        _next[_last[_init_level]] = i;
956 957
        _last[_init_level] = i;
957 958
      }
958 959
    }
959 960

	
960 961
    ///Start a new level.
961 962

	
962 963
    ///Start a new level.
963 964
    ///It shouldn't be used before the items on level 0 are listed.
964 965
    void initNewLevel() {
965 966
      ++_init_level;
966 967
    }
967 968

	
968 969
    ///Finalize the initialization process.
969 970
    void initFinish() {
970 971
      _highest_active = -1;
971 972
    }
972 973

	
973 974
    ///@}
974 975

	
975 976
  };
976 977

	
977 978

	
978 979
} //END OF NAMESPACE LEMON
979 980

	
980 981
#endif
981 982

	
Ignore white space 6 line context
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-2009
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_FULL_GRAPH_H
20 20
#define LEMON_FULL_GRAPH_H
21 21

	
22 22
#include <lemon/core.h>
23 23
#include <lemon/bits/graph_extender.h>
24 24

	
25 25
///\ingroup graphs
26 26
///\file
27
///\brief FullGraph and FullDigraph classes.
27
///\brief FullDigraph and FullGraph classes.
28 28

	
29 29
namespace lemon {
30 30

	
31 31
  class FullDigraphBase {
32 32
  public:
33 33

	
34
    typedef FullDigraphBase Graph;
34
    typedef FullDigraphBase Digraph;
35 35

	
36 36
    class Node;
37 37
    class Arc;
38 38

	
39 39
  protected:
40 40

	
41 41
    int _node_num;
42 42
    int _arc_num;
43 43

	
44 44
    FullDigraphBase() {}
45 45

	
46 46
    void construct(int n) { _node_num = n; _arc_num = n * n; }
47 47

	
48 48
  public:
49 49

	
50 50
    typedef True NodeNumTag;
51 51
    typedef True ArcNumTag;
52 52

	
53 53
    Node operator()(int ix) const { return Node(ix); }
54
    int index(const Node& node) const { return node._id; }
54
    static int index(const Node& node) { return node._id; }
55 55

	
56 56
    Arc arc(const Node& s, const Node& t) const {
57 57
      return Arc(s._id * _node_num + t._id);
58 58
    }
59 59

	
60 60
    int nodeNum() const { return _node_num; }
61 61
    int arcNum() const { return _arc_num; }
62 62

	
63 63
    int maxNodeId() const { return _node_num - 1; }
64 64
    int maxArcId() const { return _arc_num - 1; }
65 65

	
66 66
    Node source(Arc arc) const { return arc._id / _node_num; }
67 67
    Node target(Arc arc) const { return arc._id % _node_num; }
68 68

	
69 69
    static int id(Node node) { return node._id; }
70 70
    static int id(Arc arc) { return arc._id; }
71 71

	
72 72
    static Node nodeFromId(int id) { return Node(id);}
73 73
    static Arc arcFromId(int id) { return Arc(id);}
74 74

	
75 75
    typedef True FindArcTag;
76 76

	
77 77
    Arc findArc(Node s, Node t, Arc prev = INVALID) const {
78 78
      return prev == INVALID ? arc(s, t) : INVALID;
79 79
    }
80 80

	
81 81
    class Node {
82 82
      friend class FullDigraphBase;
83 83

	
84 84
    protected:
85 85
      int _id;
86 86
      Node(int id) : _id(id) {}
87 87
    public:
88 88
      Node() {}
89 89
      Node (Invalid) : _id(-1) {}
90 90
      bool operator==(const Node node) const {return _id == node._id;}
91 91
      bool operator!=(const Node node) const {return _id != node._id;}
92 92
      bool operator<(const Node node) const {return _id < node._id;}
93 93
    };
94 94

	
95 95
    class Arc {
96 96
      friend class FullDigraphBase;
97 97

	
98 98
    protected:
99 99
      int _id;  // _node_num * source + target;
100 100

	
101 101
      Arc(int id) : _id(id) {}
102 102

	
103 103
    public:
104 104
      Arc() { }
105 105
      Arc (Invalid) { _id = -1; }
106 106
      bool operator==(const Arc arc) const {return _id == arc._id;}
107 107
      bool operator!=(const Arc arc) const {return _id != arc._id;}
108 108
      bool operator<(const Arc arc) const {return _id < arc._id;}
109 109
    };
110 110

	
111 111
    void first(Node& node) const {
112 112
      node._id = _node_num - 1;
113 113
    }
114 114

	
115 115
    static void next(Node& node) {
116 116
      --node._id;
117 117
    }
118 118

	
119 119
    void first(Arc& arc) const {
120 120
      arc._id = _arc_num - 1;
121 121
    }
122 122

	
123 123
    static void next(Arc& arc) {
124 124
      --arc._id;
125 125
    }
126 126

	
127 127
    void firstOut(Arc& arc, const Node& node) const {
128 128
      arc._id = (node._id + 1) * _node_num - 1;
129 129
    }
130 130

	
131 131
    void nextOut(Arc& arc) const {
132 132
      if (arc._id % _node_num == 0) arc._id = 0;
133 133
      --arc._id;
134 134
    }
135 135

	
136 136
    void firstIn(Arc& arc, const Node& node) const {
137 137
      arc._id = _arc_num + node._id - _node_num;
138 138
    }
139 139

	
140 140
    void nextIn(Arc& arc) const {
141 141
      arc._id -= _node_num;
142 142
      if (arc._id < 0) arc._id = -1;
143 143
    }
144 144

	
145 145
  };
146 146

	
147 147
  typedef DigraphExtender<FullDigraphBase> ExtendedFullDigraphBase;
148 148

	
149 149
  /// \ingroup graphs
150 150
  ///
151
  /// \brief A full digraph class.
151
  /// \brief A directed full graph class.
152 152
  ///
153
  /// This is a simple and fast directed full graph implementation.
154
  /// From each node go arcs to each node (including the source node),
155
  /// therefore the number of the arcs in the digraph is the square of
156
  /// the node number. This digraph type is completely static, so you
157
  /// can neither add nor delete either arcs or nodes, and it needs
158
  /// constant space in memory.
153
  /// FullDigraph is a simple and fast implmenetation of directed full
154
  /// (complete) graphs. It contains an arc from each node to each node
155
  /// (including a loop for each node), therefore the number of arcs
156
  /// is the square of the number of nodes.
157
  /// This class is completely static and it needs constant memory space.
158
  /// Thus you can neither add nor delete nodes or arcs, however
159
  /// the structure can be resized using resize().
159 160
  ///
160
  /// This class conforms to the \ref concepts::Digraph "Digraph" concept
161
  /// and it also has an important extra feature that its maps are
162
  /// real \ref concepts::ReferenceMap "reference map"s.
161
  /// This type fully conforms to the \ref concepts::Digraph "Digraph concept".
162
  /// Most of its member functions and nested classes are documented
163
  /// only in the concept class.
163 164
  ///
164
  /// The \c FullDigraph and \c FullGraph classes are very similar,
165
  /// \note FullDigraph and FullGraph classes are very similar,
165 166
  /// but there are two differences. While this class conforms only
166
  /// to the \ref concepts::Digraph "Digraph" concept, the \c FullGraph
167
  /// class conforms to the \ref concepts::Graph "Graph" concept,
168
  /// moreover \c FullGraph does not contain a loop arc for each
169
  /// node as \c FullDigraph does.
167
  /// to the \ref concepts::Digraph "Digraph" concept, FullGraph
168
  /// conforms to the \ref concepts::Graph "Graph" concept,
169
  /// moreover FullGraph does not contain a loop for each
170
  /// node as this class does.
170 171
  ///
171 172
  /// \sa FullGraph
172 173
  class FullDigraph : public ExtendedFullDigraphBase {
174
    typedef ExtendedFullDigraphBase Parent;
175

	
173 176
  public:
174 177

	
175
    typedef ExtendedFullDigraphBase Parent;
176

	
177
    /// \brief Constructor
178
    /// \brief Default constructor.
179
    ///
180
    /// Default constructor. The number of nodes and arcs will be zero.
178 181
    FullDigraph() { construct(0); }
179 182

	
180 183
    /// \brief Constructor
181 184
    ///
182 185
    /// Constructor.
183 186
    /// \param n The number of the nodes.
184 187
    FullDigraph(int n) { construct(n); }
185 188

	
186 189
    /// \brief Resizes the digraph
187 190
    ///
188
    /// Resizes the digraph. The function will fully destroy and
189
    /// rebuild the digraph. This cause that the maps of the digraph will
191
    /// This function resizes the digraph. It fully destroys and
192
    /// rebuilds the structure, therefore the maps of the digraph will be
190 193
    /// reallocated automatically and the previous values will be lost.
191 194
    void resize(int n) {
192 195
      Parent::notifier(Arc()).clear();
193 196
      Parent::notifier(Node()).clear();
194 197
      construct(n);
195 198
      Parent::notifier(Node()).build();
196 199
      Parent::notifier(Arc()).build();
197 200
    }
198 201

	
199 202
    /// \brief Returns the node with the given index.
200 203
    ///
201
    /// Returns the node with the given index. Since it is a static
202
    /// digraph its nodes can be indexed with integers from the range
203
    /// <tt>[0..nodeNum()-1]</tt>.
204
    /// Returns the node with the given index. Since this structure is 
205
    /// completely static, the nodes can be indexed with integers from
206
    /// the range <tt>[0..nodeNum()-1]</tt>.
204 207
    /// \sa index()
205 208
    Node operator()(int ix) const { return Parent::operator()(ix); }
206 209

	
207 210
    /// \brief Returns the index of the given node.
208 211
    ///
209
    /// Returns the index of the given node. Since it is a static
210
    /// digraph its nodes can be indexed with integers from the range
211
    /// <tt>[0..nodeNum()-1]</tt>.
212
    /// \sa operator()
213
    int index(const Node& node) const { return Parent::index(node); }
212
    /// Returns the index of the given node. Since this structure is 
213
    /// completely static, the nodes can be indexed with integers from
214
    /// the range <tt>[0..nodeNum()-1]</tt>.
215
    /// \sa operator()()
216
    static int index(const Node& node) { return Parent::index(node); }
214 217

	
215 218
    /// \brief Returns the arc connecting the given nodes.
216 219
    ///
217 220
    /// Returns the arc connecting the given nodes.
218
    Arc arc(const Node& u, const Node& v) const {
221
    Arc arc(Node u, Node v) const {
219 222
      return Parent::arc(u, v);
220 223
    }
221 224

	
222 225
    /// \brief Number of nodes.
223 226
    int nodeNum() const { return Parent::nodeNum(); }
224 227
    /// \brief Number of arcs.
225 228
    int arcNum() const { return Parent::arcNum(); }
226 229
  };
227 230

	
228 231

	
229 232
  class FullGraphBase {
230
    int _node_num;
231
    int _edge_num;
232 233
  public:
233 234

	
234 235
    typedef FullGraphBase Graph;
235 236

	
236 237
    class Node;
237 238
    class Arc;
238 239
    class Edge;
239 240

	
240 241
  protected:
241 242

	
243
    int _node_num;
244
    int _edge_num;
245

	
242 246
    FullGraphBase() {}
243 247

	
244 248
    void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; }
245 249

	
246 250
    int _uid(int e) const {
247 251
      int u = e / _node_num;
248 252
      int v = e % _node_num;
249 253
      return u < v ? u : _node_num - 2 - u;
250 254
    }
251 255

	
252 256
    int _vid(int e) const {
253 257
      int u = e / _node_num;
254 258
      int v = e % _node_num;
255 259
      return u < v ? v : _node_num - 1 - v;
256 260
    }
257 261

	
258 262
    void _uvid(int e, int& u, int& v) const {
259 263
      u = e / _node_num;
260 264
      v = e % _node_num;
261 265
      if  (u >= v) {
262 266
        u = _node_num - 2 - u;
263 267
        v = _node_num - 1 - v;
264 268
      }
265 269
    }
266 270

	
267 271
    void _stid(int a, int& s, int& t) const {
268 272
      if ((a & 1) == 1) {
269 273
        _uvid(a >> 1, s, t);
270 274
      } else {
271 275
        _uvid(a >> 1, t, s);
272 276
      }
273 277
    }
274 278

	
275 279
    int _eid(int u, int v) const {
276 280
      if (u < (_node_num - 1) / 2) {
277 281
        return u * _node_num + v;
278 282
      } else {
279 283
        return (_node_num - 1 - u) * _node_num - v - 1;
280 284
      }
281 285
    }
282 286

	
283 287
  public:
284 288

	
285 289
    Node operator()(int ix) const { return Node(ix); }
286
    int index(const Node& node) const { return node._id; }
290
    static int index(const Node& node) { return node._id; }
287 291

	
288 292
    Edge edge(const Node& u, const Node& v) const {
289 293
      if (u._id < v._id) {
290 294
        return Edge(_eid(u._id, v._id));
291 295
      } else if (u._id != v._id) {
292 296
        return Edge(_eid(v._id, u._id));
293 297
      } else {
294 298
        return INVALID;
295 299
      }
296 300
    }
297 301

	
298 302
    Arc arc(const Node& s, const Node& t) const {
299 303
      if (s._id < t._id) {
300 304
        return Arc((_eid(s._id, t._id) << 1) | 1);
301 305
      } else if (s._id != t._id) {
302 306
        return Arc(_eid(t._id, s._id) << 1);
303 307
      } else {
304 308
        return INVALID;
305 309
      }
306 310
    }
307 311

	
308 312
    typedef True NodeNumTag;
309 313
    typedef True ArcNumTag;
310 314
    typedef True EdgeNumTag;
311 315

	
312 316
    int nodeNum() const { return _node_num; }
313 317
    int arcNum() const { return 2 * _edge_num; }
314 318
    int edgeNum() const { return _edge_num; }
315 319

	
316 320
    static int id(Node node) { return node._id; }
317 321
    static int id(Arc arc) { return arc._id; }
318 322
    static int id(Edge edge) { return edge._id; }
319 323

	
320 324
    int maxNodeId() const { return _node_num-1; }
321 325
    int maxArcId() const { return 2 * _edge_num-1; }
322 326
    int maxEdgeId() const { return _edge_num-1; }
323 327

	
324 328
    static Node nodeFromId(int id) { return Node(id);}
325 329
    static Arc arcFromId(int id) { return Arc(id);}
326 330
    static Edge edgeFromId(int id) { return Edge(id);}
327 331

	
328 332
    Node u(Edge edge) const {
329 333
      return Node(_uid(edge._id));
330 334
    }
331 335

	
332 336
    Node v(Edge edge) const {
333 337
      return Node(_vid(edge._id));
334 338
    }
335 339

	
336 340
    Node source(Arc arc) const {
337 341
      return Node((arc._id & 1) == 1 ?
338 342
                  _uid(arc._id >> 1) : _vid(arc._id >> 1));
339 343
    }
340 344

	
341 345
    Node target(Arc arc) const {
342 346
      return Node((arc._id & 1) == 1 ?
343 347
                  _vid(arc._id >> 1) : _uid(arc._id >> 1));
344 348
    }
345 349

	
346 350
    typedef True FindEdgeTag;
347 351
    typedef True FindArcTag;
348 352

	
349 353
    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
350 354
      return prev != INVALID ? INVALID : edge(u, v);
351 355
    }
352 356

	
353 357
    Arc findArc(Node s, Node t, Arc prev = INVALID) const {
354 358
      return prev != INVALID ? INVALID : arc(s, t);
355 359
    }
356 360

	
357 361
    class Node {
358 362
      friend class FullGraphBase;
359 363

	
360 364
    protected:
361 365
      int _id;
362 366
      Node(int id) : _id(id) {}
363 367
    public:
364 368
      Node() {}
365 369
      Node (Invalid) { _id = -1; }
366 370
      bool operator==(const Node node) const {return _id == node._id;}
367 371
      bool operator!=(const Node node) const {return _id != node._id;}
368 372
      bool operator<(const Node node) const {return _id < node._id;}
369 373
    };
370 374

	
371 375
    class Edge {
372 376
      friend class FullGraphBase;
373 377
      friend class Arc;
374 378

	
375 379
    protected:
376 380
      int _id;
377 381

	
378 382
      Edge(int id) : _id(id) {}
379 383

	
380 384
    public:
381 385
      Edge() { }
382 386
      Edge (Invalid) { _id = -1; }
... ...
@@ -427,188 +431,190 @@
427 431

	
428 432
    static void next(Arc& arc) {
429 433
      --arc._id;
430 434
    }
431 435

	
432 436
    void first(Edge& edge) const {
433 437
      edge._id = _edge_num - 1;
434 438
    }
435 439

	
436 440
    static void next(Edge& edge) {
437 441
      --edge._id;
438 442
    }
439 443

	
440 444
    void firstOut(Arc& arc, const Node& node) const {
441 445
      int s = node._id, t = _node_num - 1;
442 446
      if (s < t) {
443 447
        arc._id = (_eid(s, t) << 1) | 1;
444 448
      } else {
445 449
        --t;
446 450
        arc._id = (t != -1 ? (_eid(t, s) << 1) : -1);
447 451
      }
448 452
    }
449 453

	
450 454
    void nextOut(Arc& arc) const {
451 455
      int s, t;
452 456
      _stid(arc._id, s, t);
453 457
      --t;
454 458
      if (s < t) {
455 459
        arc._id = (_eid(s, t) << 1) | 1;
456 460
      } else {
457 461
        if (s == t) --t;
458 462
        arc._id = (t != -1 ? (_eid(t, s) << 1) : -1);
459 463
      }
460 464
    }
461 465

	
462 466
    void firstIn(Arc& arc, const Node& node) const {
463 467
      int s = _node_num - 1, t = node._id;
464 468
      if (s > t) {
465 469
        arc._id = (_eid(t, s) << 1);
466 470
      } else {
467 471
        --s;
468 472
        arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1);
469 473
      }
470 474
    }
471 475

	
472 476
    void nextIn(Arc& arc) const {
473 477
      int s, t;
474 478
      _stid(arc._id, s, t);
475 479
      --s;
476 480
      if (s > t) {
477 481
        arc._id = (_eid(t, s) << 1);
478 482
      } else {
479 483
        if (s == t) --s;
480 484
        arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1);
481 485
      }
482 486
    }
483 487

	
484 488
    void firstInc(Edge& edge, bool& dir, const Node& node) const {
485 489
      int u = node._id, v = _node_num - 1;
486 490
      if (u < v) {
487 491
        edge._id = _eid(u, v);
488 492
        dir = true;
489 493
      } else {
490 494
        --v;
491 495
        edge._id = (v != -1 ? _eid(v, u) : -1);
492 496
        dir = false;
493 497
      }
494 498
    }
495 499

	
496 500
    void nextInc(Edge& edge, bool& dir) const {
497 501
      int u, v;
498 502
      if (dir) {
499 503
        _uvid(edge._id, u, v);
500 504
        --v;
501 505
        if (u < v) {
502 506
          edge._id = _eid(u, v);
503 507
        } else {
504 508
          --v;
505 509
          edge._id = (v != -1 ? _eid(v, u) : -1);
506 510
          dir = false;
507 511
        }
508 512
      } else {
509 513
        _uvid(edge._id, v, u);
510 514
        --v;
511 515
        edge._id = (v != -1 ? _eid(v, u) : -1);
512 516
      }
513 517
    }
514 518

	
515 519
  };
516 520

	
517 521
  typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
518 522

	
519 523
  /// \ingroup graphs
520 524
  ///
521 525
  /// \brief An undirected full graph class.
522 526
  ///
523
  /// This is a simple and fast undirected full graph
524
  /// implementation. From each node go edge to each other node,
525
  /// therefore the number of edges in the graph is \f$n(n-1)/2\f$.
526
  /// This graph type is completely static, so you can neither
527
  /// add nor delete either edges or nodes, and it needs constant
528
  /// space in memory.
527
  /// FullGraph is a simple and fast implmenetation of undirected full
528
  /// (complete) graphs. It contains an edge between every distinct pair
529
  /// of nodes, therefore the number of edges is <tt>n(n-1)/2</tt>.
530
  /// This class is completely static and it needs constant memory space.
531
  /// Thus you can neither add nor delete nodes or edges, however
532
  /// the structure can be resized using resize().
529 533
  ///
530
  /// This class conforms to the \ref concepts::Graph "Graph" concept
531
  /// and it also has an important extra feature that its maps are
532
  /// real \ref concepts::ReferenceMap "reference map"s.
534
  /// This type fully conforms to the \ref concepts::Graph "Graph concept".
535
  /// Most of its member functions and nested classes are documented
536
  /// only in the concept class.
533 537
  ///
534
  /// The \c FullGraph and \c FullDigraph classes are very similar,
535
  /// but there are two differences. While the \c FullDigraph class
538
  /// \note FullDigraph and FullGraph classes are very similar,
539
  /// but there are two differences. While FullDigraph
536 540
  /// conforms only to the \ref concepts::Digraph "Digraph" concept,
537 541
  /// this class conforms to the \ref concepts::Graph "Graph" concept,
538
  /// moreover \c FullGraph does not contain a loop arc for each
539
  /// node as \c FullDigraph does.
542
  /// moreover this class does not contain a loop for each
543
  /// node as FullDigraph does.
540 544
  ///
541 545
  /// \sa FullDigraph
542 546
  class FullGraph : public ExtendedFullGraphBase {
547
    typedef ExtendedFullGraphBase Parent;
548

	
543 549
  public:
544 550

	
545
    typedef ExtendedFullGraphBase Parent;
546

	
547
    /// \brief Constructor
551
    /// \brief Default constructor.
552
    ///
553
    /// Default constructor. The number of nodes and edges will be zero.
548 554
    FullGraph() { construct(0); }
549 555

	
550 556
    /// \brief Constructor
551 557
    ///
552 558
    /// Constructor.
553 559
    /// \param n The number of the nodes.
554 560
    FullGraph(int n) { construct(n); }
555 561

	
556 562
    /// \brief Resizes the graph
557 563
    ///
558
    /// Resizes the graph. The function will fully destroy and
559
    /// rebuild the graph. This cause that the maps of the graph will
564
    /// This function resizes the graph. It fully destroys and
565
    /// rebuilds the structure, therefore the maps of the graph will be
560 566
    /// reallocated automatically and the previous values will be lost.
561 567
    void resize(int n) {
562 568
      Parent::notifier(Arc()).clear();
563 569
      Parent::notifier(Edge()).clear();
564 570
      Parent::notifier(Node()).clear();
565 571
      construct(n);
566 572
      Parent::notifier(Node()).build();
567 573
      Parent::notifier(Edge()).build();
568 574
      Parent::notifier(Arc()).build();
569 575
    }
570 576

	
571 577
    /// \brief Returns the node with the given index.
572 578
    ///
573
    /// Returns the node with the given index. Since it is a static
574
    /// graph its nodes can be indexed with integers from the range
575
    /// <tt>[0..nodeNum()-1]</tt>.
579
    /// Returns the node with the given index. Since this structure is 
580
    /// completely static, the nodes can be indexed with integers from
581
    /// the range <tt>[0..nodeNum()-1]</tt>.
576 582
    /// \sa index()
577 583
    Node operator()(int ix) const { return Parent::operator()(ix); }
578 584

	
579 585
    /// \brief Returns the index of the given node.
580 586
    ///
581
    /// Returns the index of the given node. Since it is a static
582
    /// graph its nodes can be indexed with integers from the range
583
    /// <tt>[0..nodeNum()-1]</tt>.
584
    /// \sa operator()
585
    int index(const Node& node) const { return Parent::index(node); }
587
    /// Returns the index of the given node. Since this structure is 
588
    /// completely static, the nodes can be indexed with integers from
589
    /// the range <tt>[0..nodeNum()-1]</tt>.
590
    /// \sa operator()()
591
    static int index(const Node& node) { return Parent::index(node); }
586 592

	
587 593
    /// \brief Returns the arc connecting the given nodes.
588 594
    ///
589 595
    /// Returns the arc connecting the given nodes.
590
    Arc arc(const Node& s, const Node& t) const {
596
    Arc arc(Node s, Node t) const {
591 597
      return Parent::arc(s, t);
592 598
    }
593 599

	
594
    /// \brief Returns the edge connects the given nodes.
600
    /// \brief Returns the edge connecting the given nodes.
595 601
    ///
596
    /// Returns the edge connects the given nodes.
597
    Edge edge(const Node& u, const Node& v) const {
602
    /// Returns the edge connecting the given nodes.
603
    Edge edge(Node u, Node v) const {
598 604
      return Parent::edge(u, v);
599 605
    }
600 606

	
601 607
    /// \brief Number of nodes.
602 608
    int nodeNum() const { return Parent::nodeNum(); }
603 609
    /// \brief Number of arcs.
604 610
    int arcNum() const { return Parent::arcNum(); }
605 611
    /// \brief Number of edges.
606 612
    int edgeNum() const { return Parent::edgeNum(); }
607 613

	
608 614
  };
609 615

	
610 616

	
611 617
} //namespace lemon
612 618

	
613 619

	
614 620
#endif //LEMON_FULL_GRAPH_H
Ignore white space 6 line context
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
 * Copyright (C) 2003-2008
5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\file
20 20
///\brief Implementation of the LEMON GLPK LP and MIP solver interface.
21 21

	
22 22
#include <lemon/glpk.h>
23 23
#include <glpk.h>
24 24

	
25 25
#include <lemon/assert.h>
26 26

	
27 27
namespace lemon {
28 28

	
29 29
  // GlpkBase members
30 30

	
31 31
  GlpkBase::GlpkBase() : LpBase() {
32 32
    lp = glp_create_prob();
33 33
    glp_create_index(lp);
34
    messageLevel(MESSAGE_NOTHING);
34 35
  }
35 36

	
36 37
  GlpkBase::GlpkBase(const GlpkBase &other) : LpBase() {
37 38
    lp = glp_create_prob();
38 39
    glp_copy_prob(lp, other.lp, GLP_ON);
39 40
    glp_create_index(lp);
40 41
    rows = other.rows;
41 42
    cols = other.cols;
43
    messageLevel(MESSAGE_NOTHING);
42 44
  }
43 45

	
44 46
  GlpkBase::~GlpkBase() {
45 47
    glp_delete_prob(lp);
46 48
  }
47 49

	
48 50
  int GlpkBase::_addCol() {
49 51
    int i = glp_add_cols(lp, 1);
50 52
    glp_set_col_bnds(lp, i, GLP_FR, 0.0, 0.0);
51 53
    return i;
52 54
  }
53 55

	
54 56
  int GlpkBase::_addRow() {
55 57
    int i = glp_add_rows(lp, 1);
56 58
    glp_set_row_bnds(lp, i, GLP_FR, 0.0, 0.0);
57 59
    return i;
58 60
  }
59 61

	
62
  int GlpkBase::_addRow(Value lo, ExprIterator b, 
63
                        ExprIterator e, Value up) {
64
    int i = glp_add_rows(lp, 1);
65

	
66
    if (lo == -INF) {
67
      if (up == INF) {
68
        glp_set_row_bnds(lp, i, GLP_FR, lo, up);
69
      } else {
70
        glp_set_row_bnds(lp, i, GLP_UP, lo, up);
71
      }    
72
    } else {
73
      if (up == INF) {
74
        glp_set_row_bnds(lp, i, GLP_LO, lo, up);
75
      } else if (lo != up) {        
76
        glp_set_row_bnds(lp, i, GLP_DB, lo, up);
77
      } else {
78
        glp_set_row_bnds(lp, i, GLP_FX, lo, up);
79
      }
80
    }
81

	
82
    std::vector<int> indexes;
83
    std::vector<Value> values;
84

	
85
    indexes.push_back(0);
86
    values.push_back(0);
87

	
88
    for(ExprIterator it = b; it != e; ++it) {
89
      indexes.push_back(it->first);
90
      values.push_back(it->second);
91
    }
92

	
93
    glp_set_mat_row(lp, i, values.size() - 1,
94
                    &indexes.front(), &values.front());
95
    return i;
96
  }
97

	
60 98
  void GlpkBase::_eraseCol(int i) {
61 99
    int ca[2];
62 100
    ca[1] = i;
63 101
    glp_del_cols(lp, 1, ca);
64 102
  }
65 103

	
66 104
  void GlpkBase::_eraseRow(int i) {
67 105
    int ra[2];
68 106
    ra[1] = i;
69 107
    glp_del_rows(lp, 1, ra);
70 108
  }
71 109

	
72 110
  void GlpkBase::_eraseColId(int i) {
73 111
    cols.eraseIndex(i);
74 112
    cols.shiftIndices(i);
75 113
  }
76 114

	
77 115
  void GlpkBase::_eraseRowId(int i) {
78 116
    rows.eraseIndex(i);
79 117
    rows.shiftIndices(i);
80 118
  }
81 119

	
82 120
  void GlpkBase::_getColName(int c, std::string& name) const {
83 121
    const char *str = glp_get_col_name(lp, c);
84 122
    if (str) name = str;
85 123
    else name.clear();
86 124
  }
87 125

	
88 126
  void GlpkBase::_setColName(int c, const std::string & name) {
89 127
    glp_set_col_name(lp, c, const_cast<char*>(name.c_str()));
90 128

	
91 129
  }
92 130

	
93 131
  int GlpkBase::_colByName(const std::string& name) const {
94 132
    int k = glp_find_col(lp, const_cast<char*>(name.c_str()));
95 133
    return k > 0 ? k : -1;
96 134
  }
97 135

	
98 136
  void GlpkBase::_getRowName(int r, std::string& name) const {
99 137
    const char *str = glp_get_row_name(lp, r);
100 138
    if (str) name = str;
101 139
    else name.clear();
102 140
  }
103 141

	
104 142
  void GlpkBase::_setRowName(int r, const std::string & name) {
105 143
    glp_set_row_name(lp, r, const_cast<char*>(name.c_str()));
106 144

	
107 145
  }
108 146

	
109 147
  int GlpkBase::_rowByName(const std::string& name) const {
110 148
    int k = glp_find_row(lp, const_cast<char*>(name.c_str()));
111 149
    return k > 0 ? k : -1;
112 150
  }
113 151

	
114 152
  void GlpkBase::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
115 153
    std::vector<int> indexes;
116 154
    std::vector<Value> values;
117 155

	
118 156
    indexes.push_back(0);
119 157
    values.push_back(0);
120 158

	
121 159
    for(ExprIterator it = b; it != e; ++it) {
122 160
      indexes.push_back(it->first);
123 161
      values.push_back(it->second);
124 162
    }
125 163

	
126 164
    glp_set_mat_row(lp, i, values.size() - 1,
127 165
                    &indexes.front(), &values.front());
128 166
  }
129 167

	
130 168
  void GlpkBase::_getRowCoeffs(int ix, InsertIterator b) const {
131 169
    int length = glp_get_mat_row(lp, ix, 0, 0);
132 170

	
133 171
    std::vector<int> indexes(length + 1);
134 172
    std::vector<Value> values(length + 1);
135 173

	
136 174
    glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
137 175

	
138 176
    for (int i = 1; i <= length; ++i) {
139 177
      *b = std::make_pair(indexes[i], values[i]);
140 178
      ++b;
141 179
    }
142 180
  }
143 181

	
144 182
  void GlpkBase::_setColCoeffs(int ix, ExprIterator b,
145 183
                                     ExprIterator e) {
146 184

	
147 185
    std::vector<int> indexes;
148 186
    std::vector<Value> values;
149 187

	
150 188
    indexes.push_back(0);
151 189
    values.push_back(0);
152 190

	
153 191
    for(ExprIterator it = b; it != e; ++it) {
154 192
      indexes.push_back(it->first);
155 193
      values.push_back(it->second);
... ...
@@ -429,266 +467,299 @@
429 467
        break;
430 468
      default:
431 469
        break;
432 470
      }
433 471
    } else {
434 472
      switch (b) {
435 473
      case GLP_FR:
436 474
        glp_set_row_bnds(lp, i, GLP_UP, lo, up);
437 475
        break;
438 476
      case GLP_UP:
439 477
        glp_set_row_bnds(lp, i, GLP_UP, lo, up);
440 478
        break;
441 479
      case GLP_LO:
442 480
      case GLP_DB:
443 481
      case GLP_FX:
444 482
        if (lo == up)
445 483
          glp_set_row_bnds(lp, i, GLP_FX, lo, up);
446 484
        else
447 485
          glp_set_row_bnds(lp, i, GLP_DB, lo, up);
448 486
        break;
449 487
      default:
450 488
        break;
451 489
      }
452 490
    }
453 491
  }
454 492

	
455 493
  GlpkBase::Value GlpkBase::_getRowUpperBound(int i) const {
456 494
    int b = glp_get_row_type(lp, i);
457 495
    switch (b) {
458 496
    case GLP_UP:
459 497
    case GLP_DB:
460 498
    case GLP_FX:
461 499
      return glp_get_row_ub(lp, i);
462 500
    default:
463 501
      return INF;
464 502
    }
465 503
  }
466 504

	
467 505
  void GlpkBase::_setObjCoeffs(ExprIterator b, ExprIterator e) {
468 506
    for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
469 507
      glp_set_obj_coef(lp, i, 0.0);
470 508
    }
471 509
    for (ExprIterator it = b; it != e; ++it) {
472 510
      glp_set_obj_coef(lp, it->first, it->second);
473 511
    }
474 512
  }
475 513

	
476 514
  void GlpkBase::_getObjCoeffs(InsertIterator b) const {
477 515
    for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
478 516
      Value val = glp_get_obj_coef(lp, i);
479 517
      if (val != 0.0) {
480 518
        *b = std::make_pair(i, val);
481 519
        ++b;
482 520
      }
483 521
    }
484 522
  }
485 523

	
486 524
  void GlpkBase::_setObjCoeff(int i, Value obj_coef) {
487 525
    //i = 0 means the constant term (shift)
488 526
    glp_set_obj_coef(lp, i, obj_coef);
489 527
  }
490 528

	
491 529
  GlpkBase::Value GlpkBase::_getObjCoeff(int i) const {
492 530
    //i = 0 means the constant term (shift)
493 531
    return glp_get_obj_coef(lp, i);
494 532
  }
495 533

	
496 534
  void GlpkBase::_setSense(GlpkBase::Sense sense) {
497 535
    switch (sense) {
498 536
    case MIN:
499 537
      glp_set_obj_dir(lp, GLP_MIN);
500 538
      break;
501 539
    case MAX:
502 540
      glp_set_obj_dir(lp, GLP_MAX);
503 541
      break;
504 542
    }
505 543
  }
506 544

	
507 545
  GlpkBase::Sense GlpkBase::_getSense() const {
508 546
    switch(glp_get_obj_dir(lp)) {
509 547
    case GLP_MIN:
510 548
      return MIN;
511 549
    case GLP_MAX:
512 550
      return MAX;
513 551
    default:
514 552
      LEMON_ASSERT(false, "Wrong sense");
515 553
      return GlpkBase::Sense();
516 554
    }
517 555
  }
518 556

	
519 557
  void GlpkBase::_clear() {
520 558
    glp_erase_prob(lp);
521 559
    rows.clear();
522 560
    cols.clear();
523 561
  }
524 562

	
563
  void GlpkBase::freeEnv() {
564
    glp_free_env();
565
  }
566

	
567
  void GlpkBase::_messageLevel(MessageLevel level) {
568
    switch (level) {
569
    case MESSAGE_NOTHING:
570
      _message_level = GLP_MSG_OFF;
571
      break;
572
    case MESSAGE_ERROR:
573
      _message_level = GLP_MSG_ERR;
574
      break;
575
    case MESSAGE_WARNING:
576
      _message_level = GLP_MSG_ERR;
577
      break;
578
    case MESSAGE_NORMAL:
579
      _message_level = GLP_MSG_ON;
580
      break;
581
    case MESSAGE_VERBOSE:
582
      _message_level = GLP_MSG_ALL;
583
      break;
584
    }
585
  }
586

	
587
  GlpkBase::FreeEnvHelper GlpkBase::freeEnvHelper;
588

	
525 589
  // GlpkLp members
526 590

	
527 591
  GlpkLp::GlpkLp()
528
    : LpBase(), GlpkBase(), LpSolver() {
529
    messageLevel(MESSAGE_NO_OUTPUT);
592
    : LpBase(), LpSolver(), GlpkBase() {
593
    presolver(false);
530 594
  }
531 595

	
532 596
  GlpkLp::GlpkLp(const GlpkLp& other)
533
    : LpBase(other), GlpkBase(other), LpSolver(other) {
534
    messageLevel(MESSAGE_NO_OUTPUT);
597
    : LpBase(other), LpSolver(other), GlpkBase(other) {
598
    presolver(false);
535 599
  }
536 600

	
537
  GlpkLp* GlpkLp::_newSolver() const { return new GlpkLp; }
538
  GlpkLp* GlpkLp::_cloneSolver() const { return new GlpkLp(*this); }
601
  GlpkLp* GlpkLp::newSolver() const { return new GlpkLp; }
602
  GlpkLp* GlpkLp::cloneSolver() const { return new GlpkLp(*this); }
539 603

	
540 604
  const char* GlpkLp::_solverName() const { return "GlpkLp"; }
541 605

	
542 606
  void GlpkLp::_clear_temporals() {
543 607
    _primal_ray.clear();
544 608
    _dual_ray.clear();
545 609
  }
546 610

	
547 611
  GlpkLp::SolveExitStatus GlpkLp::_solve() {
548 612
    return solvePrimal();
549 613
  }
550 614

	
551 615
  GlpkLp::SolveExitStatus GlpkLp::solvePrimal() {
552 616
    _clear_temporals();
553 617

	
554 618
    glp_smcp smcp;
555 619
    glp_init_smcp(&smcp);
556 620

	
557
    switch (_message_level) {
558
    case MESSAGE_NO_OUTPUT:
559
      smcp.msg_lev = GLP_MSG_OFF;
621
    smcp.msg_lev = _message_level;
622
    smcp.presolve = _presolve;
623

	
624
    // If the basis is not valid we get an error return value.
625
    // In this case we can try to create a new basis.
626
    switch (glp_simplex(lp, &smcp)) {
627
    case 0:
560 628
      break;
561
    case MESSAGE_ERROR_MESSAGE:
562
      smcp.msg_lev = GLP_MSG_ERR;
629
    case GLP_EBADB:
630
    case GLP_ESING:
631
    case GLP_ECOND:
632
      glp_term_out(false);
633
      glp_adv_basis(lp, 0);
634
      glp_term_out(true);
635
      if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
563 636
      break;
564
    case MESSAGE_NORMAL_OUTPUT:
565
      smcp.msg_lev = GLP_MSG_ON;
566
      break;
567
    case MESSAGE_FULL_OUTPUT:
568
      smcp.msg_lev = GLP_MSG_ALL;
569
      break;
637
    default:
638
      return UNSOLVED;
570 639
    }
571 640

	
572
    if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
573 641
    return SOLVED;
574 642
  }
575 643

	
576 644
  GlpkLp::SolveExitStatus GlpkLp::solveDual() {
577 645
    _clear_temporals();
578 646

	
579 647
    glp_smcp smcp;
580 648
    glp_init_smcp(&smcp);
581 649

	
582
    switch (_message_level) {
583
    case MESSAGE_NO_OUTPUT:
584
      smcp.msg_lev = GLP_MSG_OFF;
650
    smcp.msg_lev = _message_level;
651
    smcp.meth = GLP_DUAL;
652
    smcp.presolve = _presolve;
653

	
654
    // If the basis is not valid we get an error return value.
655
    // In this case we can try to create a new basis.
656
    switch (glp_simplex(lp, &smcp)) {
657
    case 0:
585 658
      break;
586
    case MESSAGE_ERROR_MESSAGE:
587
      smcp.msg_lev = GLP_MSG_ERR;
659
    case GLP_EBADB:
660
    case GLP_ESING:
661
    case GLP_ECOND:
662
      glp_term_out(false);
663
      glp_adv_basis(lp, 0);
664
      glp_term_out(true);
665
      if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
588 666
      break;
589
    case MESSAGE_NORMAL_OUTPUT:
590
      smcp.msg_lev = GLP_MSG_ON;
591
      break;
592
    case MESSAGE_FULL_OUTPUT:
593
      smcp.msg_lev = GLP_MSG_ALL;
594
      break;
667
    default:
668
      return UNSOLVED;
595 669
    }
596
    smcp.meth = GLP_DUAL;
597

	
598
    if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
599 670
    return SOLVED;
600 671
  }
601 672

	
602 673
  GlpkLp::Value GlpkLp::_getPrimal(int i) const {
603 674
    return glp_get_col_prim(lp, i);
604 675
  }
605 676

	
606 677
  GlpkLp::Value GlpkLp::_getDual(int i) const {
607 678
    return glp_get_row_dual(lp, i);
608 679
  }
609 680

	
610 681
  GlpkLp::Value GlpkLp::_getPrimalValue() const {
611 682
    return glp_get_obj_val(lp);
612 683
  }
613 684

	
614 685
  GlpkLp::VarStatus GlpkLp::_getColStatus(int i) const {
615 686
    switch (glp_get_col_stat(lp, i)) {
616 687
    case GLP_BS:
617 688
      return BASIC;
618 689
    case GLP_UP:
619 690
      return UPPER;
620 691
    case GLP_LO:
621 692
      return LOWER;
622 693
    case GLP_NF:
623 694
      return FREE;
624 695
    case GLP_NS:
625 696
      return FIXED;
626 697
    default:
627 698
      LEMON_ASSERT(false, "Wrong column status");
628 699
      return GlpkLp::VarStatus();
629 700
    }
630 701
  }
631 702

	
632 703
  GlpkLp::VarStatus GlpkLp::_getRowStatus(int i) const {
633 704
    switch (glp_get_row_stat(lp, i)) {
634 705
    case GLP_BS:
635 706
      return BASIC;
636 707
    case GLP_UP:
637 708
      return UPPER;
638 709
    case GLP_LO:
639 710
      return LOWER;
640 711
    case GLP_NF:
641 712
      return FREE;
642 713
    case GLP_NS:
643 714
      return FIXED;
644 715
    default:
645 716
      LEMON_ASSERT(false, "Wrong row status");
646 717
      return GlpkLp::VarStatus();
647 718
    }
648 719
  }
649 720

	
650 721
  GlpkLp::Value GlpkLp::_getPrimalRay(int i) const {
651 722
    if (_primal_ray.empty()) {
652 723
      int row_num = glp_get_num_rows(lp);
653 724
      int col_num = glp_get_num_cols(lp);
654 725

	
655 726
      _primal_ray.resize(col_num + 1, 0.0);
656 727

	
657 728
      int index = glp_get_unbnd_ray(lp);
658 729
      if (index != 0) {
659 730
        // The primal ray is found in primal simplex second phase
660 731
        LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) :
661 732
                      glp_get_col_stat(lp, index - row_num)) != GLP_BS,
662 733
                     "Wrong primal ray");
663 734

	
664 735
        bool negate = glp_get_obj_dir(lp) == GLP_MAX;
665 736

	
666 737
        if (index > row_num) {
667 738
          _primal_ray[index - row_num] = 1.0;
668 739
          if (glp_get_col_dual(lp, index - row_num) > 0) {
669 740
            negate = !negate;
670 741
          }
671 742
        } else {
672 743
          if (glp_get_row_dual(lp, index) > 0) {
673 744
            negate = !negate;
674 745
          }
675 746
        }
676 747

	
677 748
        std::vector<int> ray_indexes(row_num + 1);
678 749
        std::vector<Value> ray_values(row_num + 1);
679 750
        int ray_length = glp_eval_tab_col(lp, index, &ray_indexes.front(),
680 751
                                          &ray_values.front());
681 752

	
682 753
        for (int i = 1; i <= ray_length; ++i) {
683 754
          if (ray_indexes[i] > row_num) {
684 755
            _primal_ray[ray_indexes[i] - row_num] = ray_values[i];
685 756
          }
686 757
        }
687 758

	
688 759
        if (negate) {
689 760
          for (int i = 1; i <= col_num; ++i) {
690 761
            _primal_ray[i] = - _primal_ray[i];
691 762
          }
692 763
        }
693 764
      } else {
694 765
        for (int i = 1; i <= col_num; ++i) {
... ...
@@ -720,233 +791,213 @@
720 791
          idx = glp_get_col_bind(lp, index - row_num);
721 792
          if (glp_get_col_prim(lp, index - row_num) >
722 793
              glp_get_col_ub(lp, index - row_num)) {
723 794
            negate = true;
724 795
          }
725 796
        } else {
726 797
          idx = glp_get_row_bind(lp, index);
727 798
          if (glp_get_row_prim(lp, index) > glp_get_row_ub(lp, index)) {
728 799
            negate = true;
729 800
          }
730 801
        }
731 802

	
732 803
        _dual_ray[idx] = negate ?  - 1.0 : 1.0;
733 804

	
734 805
        glp_btran(lp, &_dual_ray.front());
735 806
      } else {
736 807
        double eps = 1e-7;
737 808
        // The dual ray is found in primal simplex first phase
738 809
        // We assume that the glpk minimizes the slack to get feasible solution
739 810
        for (int i = 1; i <= row_num; ++i) {
740 811
          int index = glp_get_bhead(lp, i);
741 812
          if (index <= row_num) {
742 813
            double res = glp_get_row_prim(lp, index);
743 814
            if (res > glp_get_row_ub(lp, index) + eps) {
744 815
              _dual_ray[i] = -1;
745 816
            } else if (res < glp_get_row_lb(lp, index) - eps) {
746 817
              _dual_ray[i] = 1;
747 818
            } else {
748 819
              _dual_ray[i] = 0;
749 820
            }
750 821
            _dual_ray[i] *= glp_get_rii(lp, index);
751 822
          } else {
752 823
            double res = glp_get_col_prim(lp, index - row_num);
753 824
            if (res > glp_get_col_ub(lp, index - row_num) + eps) {
754 825
              _dual_ray[i] = -1;
755 826
            } else if (res < glp_get_col_lb(lp, index - row_num) - eps) {
756 827
              _dual_ray[i] = 1;
757 828
            } else {
758 829
              _dual_ray[i] = 0;
759 830
            }
760 831
            _dual_ray[i] /= glp_get_sjj(lp, index - row_num);
761 832
          }
762 833
        }
763 834

	
764 835
        glp_btran(lp, &_dual_ray.front());
765 836

	
766 837
        for (int i = 1; i <= row_num; ++i) {
767 838
          _dual_ray[i] /= glp_get_rii(lp, i);
768 839
        }
769 840
      }
770 841
    }
771 842
    return _dual_ray[i];
772 843
  }
773 844

	
774 845
  GlpkLp::ProblemType GlpkLp::_getPrimalType() const {
775 846
    if (glp_get_status(lp) == GLP_OPT)
776 847
      return OPTIMAL;
777 848
    switch (glp_get_prim_stat(lp)) {
778 849
    case GLP_UNDEF:
779 850
      return UNDEFINED;
780 851
    case GLP_FEAS:
781 852
    case GLP_INFEAS:
782 853
      if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
783 854
        return UNBOUNDED;
784 855
      } else {
785 856
        return UNDEFINED;
786 857
      }
787 858
    case GLP_NOFEAS:
788 859
      return INFEASIBLE;
789 860
    default:
790 861
      LEMON_ASSERT(false, "Wrong primal type");
791 862
      return  GlpkLp::ProblemType();
792 863
    }
793 864
  }
794 865

	
795 866
  GlpkLp::ProblemType GlpkLp::_getDualType() const {
796 867
    if (glp_get_status(lp) == GLP_OPT)
797 868
      return OPTIMAL;
798 869
    switch (glp_get_dual_stat(lp)) {
799 870
    case GLP_UNDEF:
800 871
      return UNDEFINED;
801 872
    case GLP_FEAS:
802 873
    case GLP_INFEAS:
803 874
      if (glp_get_prim_stat(lp) == GLP_NOFEAS) {
804 875
        return UNBOUNDED;
805 876
      } else {
806 877
        return UNDEFINED;
807 878
      }
808 879
    case GLP_NOFEAS:
809 880
      return INFEASIBLE;
810 881
    default:
811 882
      LEMON_ASSERT(false, "Wrong primal type");
812 883
      return  GlpkLp::ProblemType();
813 884
    }
814 885
  }
815 886

	
816
  void GlpkLp::presolver(bool b) {
817
    lpx_set_int_parm(lp, LPX_K_PRESOL, b ? 1 : 0);
818
  }
819

	
820
  void GlpkLp::messageLevel(MessageLevel m) {
821
    _message_level = m;
887
  void GlpkLp::presolver(bool presolve) {
888
    _presolve = presolve;
822 889
  }
823 890

	
824 891
  // GlpkMip members
825 892

	
826 893
  GlpkMip::GlpkMip()
827
    : LpBase(), GlpkBase(), MipSolver() {
828
    messageLevel(MESSAGE_NO_OUTPUT);
894
    : LpBase(), MipSolver(), GlpkBase() {
829 895
  }
830 896

	
831 897
  GlpkMip::GlpkMip(const GlpkMip& other)
832
    : LpBase(), GlpkBase(other), MipSolver() {
833
    messageLevel(MESSAGE_NO_OUTPUT);
898
    : LpBase(), MipSolver(), GlpkBase(other) {
834 899
  }
835 900

	
836 901
  void GlpkMip::_setColType(int i, GlpkMip::ColTypes col_type) {
837 902
    switch (col_type) {
838 903
    case INTEGER:
839 904
      glp_set_col_kind(lp, i, GLP_IV);
840 905
      break;
841 906
    case REAL:
842 907
      glp_set_col_kind(lp, i, GLP_CV);
843 908
      break;
844 909
    }
845 910
  }
846 911

	
847 912
  GlpkMip::ColTypes GlpkMip::_getColType(int i) const {
848 913
    switch (glp_get_col_kind(lp, i)) {
849 914
    case GLP_IV:
850 915
    case GLP_BV:
851 916
      return INTEGER;
852 917
    default:
853 918
      return REAL;
854 919
    }
855 920

	
856 921
  }
857 922

	
858 923
  GlpkMip::SolveExitStatus GlpkMip::_solve() {
859 924
    glp_smcp smcp;
860 925
    glp_init_smcp(&smcp);
861 926

	
862
    switch (_message_level) {
863
    case MESSAGE_NO_OUTPUT:
864
      smcp.msg_lev = GLP_MSG_OFF;
865
      break;
866
    case MESSAGE_ERROR_MESSAGE:
867
      smcp.msg_lev = GLP_MSG_ERR;
868
      break;
869
    case MESSAGE_NORMAL_OUTPUT:
870
      smcp.msg_lev = GLP_MSG_ON;
871
      break;
872
    case MESSAGE_FULL_OUTPUT:
873
      smcp.msg_lev = GLP_MSG_ALL;
874
      break;
875
    }
927
    smcp.msg_lev = _message_level;
876 928
    smcp.meth = GLP_DUAL;
877 929

	
878
    if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
930
    // If the basis is not valid we get an error return value.
931
    // In this case we can try to create a new basis.
932
    switch (glp_simplex(lp, &smcp)) {
933
    case 0:
934
      break;
935
    case GLP_EBADB:
936
    case GLP_ESING:
937
    case GLP_ECOND:
938
      glp_term_out(false);
939
      glp_adv_basis(lp, 0);
940
      glp_term_out(true);
941
      if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
942
      break;
943
    default:
944
      return UNSOLVED;
945
    }
946

	
879 947
    if (glp_get_status(lp) != GLP_OPT) return SOLVED;
880 948

	
881 949
    glp_iocp iocp;
882 950
    glp_init_iocp(&iocp);
883 951

	
884
    switch (_message_level) {
885
    case MESSAGE_NO_OUTPUT:
886
      iocp.msg_lev = GLP_MSG_OFF;
887
      break;
888
    case MESSAGE_ERROR_MESSAGE:
889
      iocp.msg_lev = GLP_MSG_ERR;
890
      break;
891
    case MESSAGE_NORMAL_OUTPUT:
892
      iocp.msg_lev = GLP_MSG_ON;
893
      break;
894
    case MESSAGE_FULL_OUTPUT:
895
      iocp.msg_lev = GLP_MSG_ALL;
896
      break;
897
    }
952
    iocp.msg_lev = _message_level;
898 953

	
899 954
    if (glp_intopt(lp, &iocp) != 0) return UNSOLVED;
900 955
    return SOLVED;
901 956
  }
902 957

	
903 958

	
904 959
  GlpkMip::ProblemType GlpkMip::_getType() const {
905 960
    switch (glp_get_status(lp)) {
906 961
    case GLP_OPT:
907 962
      switch (glp_mip_status(lp)) {
908 963
      case GLP_UNDEF:
909 964
        return UNDEFINED;
910 965
      case GLP_NOFEAS:
911 966
        return INFEASIBLE;
912 967
      case GLP_FEAS:
913 968
        return FEASIBLE;
914 969
      case GLP_OPT:
915 970
        return OPTIMAL;
916 971
      default:
917 972
        LEMON_ASSERT(false, "Wrong problem type.");
918 973
        return GlpkMip::ProblemType();
919 974
      }
920 975
    case GLP_NOFEAS:
921 976
      return INFEASIBLE;
922 977
    case GLP_INFEAS:
923 978
    case GLP_FEAS:
924 979
      if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
925 980
        return UNBOUNDED;
926 981
      } else {
927 982
        return UNDEFINED;
928 983
      }
929 984
    default:
930 985
      LEMON_ASSERT(false, "Wrong problem type.");
931 986
      return GlpkMip::ProblemType();
932 987
    }
933 988
  }
934 989

	
935 990
  GlpkMip::Value GlpkMip::_getSol(int i) const {
936 991
    return glp_mip_col_val(lp, i);
937 992
  }
938 993

	
939 994
  GlpkMip::Value GlpkMip::_getSolValue() const {
940 995
    return glp_mip_obj_val(lp);
941 996
  }
942 997

	
943
  GlpkMip* GlpkMip::_newSolver() const { return new GlpkMip; }
944
  GlpkMip* GlpkMip::_cloneSolver() const {return new GlpkMip(*this); }
998
  GlpkMip* GlpkMip::newSolver() const { return new GlpkMip; }
999
  GlpkMip* GlpkMip::cloneSolver() const {return new GlpkMip(*this); }
945 1000

	
946 1001
  const char* GlpkMip::_solverName() const { return "GlpkMip"; }
947 1002

	
948
  void GlpkMip::messageLevel(MessageLevel m) {
949
    _message_level = m;
950
  }
951

	
952 1003
} //END OF NAMESPACE LEMON
Ignore white space 6 line context
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_GLPK_H
20 20
#define LEMON_GLPK_H
21 21

	
22 22
///\file
23 23
///\brief Header of the LEMON-GLPK lp solver interface.
24 24
///\ingroup lp_group
25 25

	
26 26
#include <lemon/lp_base.h>
27 27

	
28 28
// forward declaration
29
#ifndef _GLP_PROB
29
#if !defined _GLP_PROB && !defined GLP_PROB
30 30
#define _GLP_PROB
31
typedef struct { double _prob; } glp_prob;
31
#define GLP_PROB
32
typedef struct { double _opaque_prob; } glp_prob;
32 33
/* LP/MIP problem object */
33 34
#endif
34 35

	
35 36
namespace lemon {
36 37

	
37 38

	
38 39
  /// \brief Base interface for the GLPK LP and MIP solver
39 40
  ///
40 41
  /// This class implements the common interface of the GLPK LP and MIP solver.
41 42
  /// \ingroup lp_group
42 43
  class GlpkBase : virtual public LpBase {
43 44
  protected:
44 45

	
45 46
    typedef glp_prob LPX;
46 47
    glp_prob* lp;
47 48

	
48 49
    GlpkBase();
49 50
    GlpkBase(const GlpkBase&);
50 51
    virtual ~GlpkBase();
51 52

	
52 53
  protected:
53 54

	
54 55
    virtual int _addCol();
55 56
    virtual int _addRow();
57
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
56 58

	
57 59
    virtual void _eraseCol(int i);
58 60
    virtual void _eraseRow(int i);
59 61

	
60 62
    virtual void _eraseColId(int i);
61 63
    virtual void _eraseRowId(int i);
62 64

	
63 65
    virtual void _getColName(int col, std::string& name) const;
64 66
    virtual void _setColName(int col, const std::string& name);
65 67
    virtual int _colByName(const std::string& name) const;
66 68

	
67 69
    virtual void _getRowName(int row, std::string& name) const;
68 70
    virtual void _setRowName(int row, const std::string& name);
69 71
    virtual int _rowByName(const std::string& name) const;
70 72

	
71 73
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
72 74
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
73 75

	
74 76
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
75 77
    virtual void _getColCoeffs(int i, InsertIterator b) const;
76 78

	
77 79
    virtual void _setCoeff(int row, int col, Value value);
78 80
    virtual Value _getCoeff(int row, int col) const;
79 81

	
80 82
    virtual void _setColLowerBound(int i, Value value);
81 83
    virtual Value _getColLowerBound(int i) const;
82 84

	
83 85
    virtual void _setColUpperBound(int i, Value value);
84 86
    virtual Value _getColUpperBound(int i) const;
85 87

	
86 88
    virtual void _setRowLowerBound(int i, Value value);
87 89
    virtual Value _getRowLowerBound(int i) const;
88 90

	
89 91
    virtual void _setRowUpperBound(int i, Value value);
90 92
    virtual Value _getRowUpperBound(int i) const;
91 93

	
92 94
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
93 95
    virtual void _getObjCoeffs(InsertIterator b) const;
94 96

	
95 97
    virtual void _setObjCoeff(int i, Value obj_coef);
96 98
    virtual Value _getObjCoeff(int i) const;
97 99

	
98 100
    virtual void _setSense(Sense);
99 101
    virtual Sense _getSense() const;
100 102

	
101 103
    virtual void _clear();
102 104

	
105
    virtual void _messageLevel(MessageLevel level);
106

	
107
  private:
108

	
109
    static void freeEnv();
110

	
111
    struct FreeEnvHelper {
112
      ~FreeEnvHelper() {
113
        freeEnv();
114
      }
115
    };
116
    
117
    static FreeEnvHelper freeEnvHelper;
118

	
119
  protected:
120
    
121
    int _message_level;
122
    
103 123
  public:
104 124

	
105 125
    ///Pointer to the underlying GLPK data structure.
106 126
    LPX *lpx() {return lp;}
107 127
    ///Const pointer to the underlying GLPK data structure.
108 128
    const LPX *lpx() const {return lp;}
109 129

	
110 130
    ///Returns the constraint identifier understood by GLPK.
111 131
    int lpxRow(Row r) const { return rows(id(r)); }
112 132

	
113 133
    ///Returns the variable identifier understood by GLPK.
114 134
    int lpxCol(Col c) const { return cols(id(c)); }
115 135

	
116 136
  };
117 137

	
118 138
  /// \brief Interface for the GLPK LP solver
119 139
  ///
120 140
  /// This class implements an interface for the GLPK LP solver.
121 141
  ///\ingroup lp_group
122
  class GlpkLp : public GlpkBase, public LpSolver {
142
  class GlpkLp : public LpSolver, public GlpkBase {
123 143
  public:
124 144

	
125 145
    ///\e
126 146
    GlpkLp();
127 147
    ///\e
128 148
    GlpkLp(const GlpkLp&);
129 149

	
150
    ///\e
151
    virtual GlpkLp* cloneSolver() const;
152
    ///\e
153
    virtual GlpkLp* newSolver() const;
154

	
130 155
  private:
131 156

	
132 157
    mutable std::vector<double> _primal_ray;
133 158
    mutable std::vector<double> _dual_ray;
134 159

	
135 160
    void _clear_temporals();
136 161

	
137 162
  protected:
138 163

	
139
    virtual GlpkLp* _cloneSolver() const;
140
    virtual GlpkLp* _newSolver() const;
141

	
142 164
    virtual const char* _solverName() const;
143 165

	
144 166
    virtual SolveExitStatus _solve();
145 167
    virtual Value _getPrimal(int i) const;
146 168
    virtual Value _getDual(int i) const;
147 169

	
148 170
    virtual Value _getPrimalValue() const;
149 171

	
150 172
    virtual VarStatus _getColStatus(int i) const;
151 173
    virtual VarStatus _getRowStatus(int i) const;
152 174

	
153 175
    virtual Value _getPrimalRay(int i) const;
154 176
    virtual Value _getDualRay(int i) const;
155 177

	
156
    ///\todo It should be clarified
157
    ///
158 178
    virtual ProblemType _getPrimalType() const;
159 179
    virtual ProblemType _getDualType() const;
160 180

	
161 181
  public:
162 182

	
163 183
    ///Solve with primal simplex
164 184
    SolveExitStatus solvePrimal();
165 185

	
166 186
    ///Solve with dual simplex
167 187
    SolveExitStatus solveDual();
168 188

	
189
  private:
190

	
191
    bool _presolve;
192

	
193
  public:
194

	
169 195
    ///Turns on or off the presolver
170 196

	
171 197
    ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
172 198
    ///
173 199
    ///The presolver is off by default.
174
    void presolver(bool b);
200
    void presolver(bool presolve);
175 201

	
176
    ///Enum for \c messageLevel() parameter
177
    enum MessageLevel {
178
      /// no output (default value)
179
      MESSAGE_NO_OUTPUT = 0,
180
      /// error messages only
181
      MESSAGE_ERROR_MESSAGE = 1,
182
      /// normal output
183
      MESSAGE_NORMAL_OUTPUT = 2,
184
      /// full output (includes informational messages)
185
      MESSAGE_FULL_OUTPUT = 3
186
    };
187

	
188
  private:
189

	
190
    MessageLevel _message_level;
191

	
192
  public:
193

	
194
    ///Set the verbosity of the messages
195

	
196
    ///Set the verbosity of the messages
197
    ///
198
    ///\param m is the level of the messages output by the solver routines.
199
    void messageLevel(MessageLevel m);
200 202
  };
201 203

	
202 204
  /// \brief Interface for the GLPK MIP solver
203 205
  ///
204 206
  /// This class implements an interface for the GLPK MIP solver.
205 207
  ///\ingroup lp_group
206
  class GlpkMip : public GlpkBase, public MipSolver {
208
  class GlpkMip : public MipSolver, public GlpkBase {
207 209
  public:
208 210

	
209 211
    ///\e
210 212
    GlpkMip();
211 213
    ///\e
212 214
    GlpkMip(const GlpkMip&);
213 215

	
216
    virtual GlpkMip* cloneSolver() const;
217
    virtual GlpkMip* newSolver() const;
218

	
214 219
  protected:
215 220

	
216
    virtual GlpkMip* _cloneSolver() const;
217
    virtual GlpkMip* _newSolver() const;
218

	
219 221
    virtual const char* _solverName() const;
220 222

	
221 223
    virtual ColTypes _getColType(int col) const;
222 224
    virtual void _setColType(int col, ColTypes col_type);
223 225

	
224 226
    virtual SolveExitStatus _solve();
225 227
    virtual ProblemType _getType() const;
226 228
    virtual Value _getSol(int i) const;
227 229
    virtual Value _getSolValue() const;
228 230

	
229
    ///Enum for \c messageLevel() parameter
230
    enum MessageLevel {
231
      /// no output (default value)
232
      MESSAGE_NO_OUTPUT = 0,
233
      /// error messages only
234
      MESSAGE_ERROR_MESSAGE = 1,
235
      /// normal output
236
      MESSAGE_NORMAL_OUTPUT = 2,
237
      /// full output (includes informational messages)
238
      MESSAGE_FULL_OUTPUT = 3
239
    };
240

	
241
  private:
242

	
243
    MessageLevel _message_level;
244

	
245
  public:
246

	
247
    ///Set the verbosity of the messages
248

	
249
    ///Set the verbosity of the messages
250
    ///
251
    ///\param m is the level of the messages output by the solver routines.
252
    void messageLevel(MessageLevel m);
253 231
  };
254 232

	
255 233

	
256 234
} //END OF NAMESPACE LEMON
257 235

	
258 236
#endif //LEMON_GLPK_H
259 237

	
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_GRAPH_TO_EPS_H
20 20
#define LEMON_GRAPH_TO_EPS_H
21 21

	
22 22
#include<iostream>
23 23
#include<fstream>
24 24
#include<sstream>
25 25
#include<algorithm>
26 26
#include<vector>
27 27

	
28 28
#ifndef WIN32
29 29
#include<sys/time.h>
30 30
#include<ctime>
31 31
#else
32
#define WIN32_LEAN_AND_MEAN
33
#define NOMINMAX
34
#include<windows.h>
32
#include<lemon/bits/windows.h>
35 33
#endif
36 34

	
37 35
#include<lemon/math.h>
38 36
#include<lemon/core.h>
39 37
#include<lemon/dim2.h>
40 38
#include<lemon/maps.h>
41 39
#include<lemon/color.h>
42 40
#include<lemon/bits/bezier.h>
43 41
#include<lemon/error.h>
44 42

	
45 43

	
46 44
///\ingroup eps_io
47 45
///\file
48 46
///\brief A well configurable tool for visualizing graphs
49 47

	
50 48
namespace lemon {
51 49

	
52 50
  namespace _graph_to_eps_bits {
53 51
    template<class MT>
54 52
    class _NegY {
55 53
    public:
56 54
      typedef typename MT::Key Key;
57 55
      typedef typename MT::Value Value;
58 56
      const MT &map;
59 57
      int yscale;
60 58
      _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
61 59
      Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
62 60
    };
63 61
  }
64 62

	
65 63
///Default traits class of GraphToEps
66 64

	
67 65
///Default traits class of \ref GraphToEps.
68 66
///
69
///\c G is the type of the underlying graph.
70
template<class G>
67
///\param GR is the type of the underlying graph.
68
template<class GR>
71 69
struct DefaultGraphToEpsTraits
72 70
{
73
  typedef G Graph;
71
  typedef GR Graph;
72
  typedef GR Digraph;
74 73
  typedef typename Graph::Node Node;
75 74
  typedef typename Graph::NodeIt NodeIt;
76 75
  typedef typename Graph::Arc Arc;
77 76
  typedef typename Graph::ArcIt ArcIt;
78 77
  typedef typename Graph::InArcIt InArcIt;
79 78
  typedef typename Graph::OutArcIt OutArcIt;
80 79

	
81 80

	
82 81
  const Graph &g;
83 82

	
84 83
  std::ostream& os;
85 84

	
86 85
  typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType;
87 86
  CoordsMapType _coords;
88 87
  ConstMap<typename Graph::Node,double > _nodeSizes;
89 88
  ConstMap<typename Graph::Node,int > _nodeShapes;
90 89

	
91 90
  ConstMap<typename Graph::Node,Color > _nodeColors;
92 91
  ConstMap<typename Graph::Arc,Color > _arcColors;
93 92

	
94 93
  ConstMap<typename Graph::Arc,double > _arcWidths;
95 94

	
96 95
  double _arcWidthScale;
97 96

	
98 97
  double _nodeScale;
99 98
  double _xBorder, _yBorder;
100 99
  double _scale;
101 100
  double _nodeBorderQuotient;
102 101

	
103 102
  bool _drawArrows;
104 103
  double _arrowLength, _arrowWidth;
105 104

	
106 105
  bool _showNodes, _showArcs;
107 106

	
108 107
  bool _enableParallel;
109 108
  double _parArcDist;
110 109

	
111 110
  bool _showNodeText;
112 111
  ConstMap<typename Graph::Node,bool > _nodeTexts;
113 112
  double _nodeTextSize;
114 113

	
115 114
  bool _showNodePsText;
116 115
  ConstMap<typename Graph::Node,bool > _nodePsTexts;
117 116
  char *_nodePsTextsPreamble;
118 117

	
119 118
  bool _undirected;
120 119

	
121 120
  bool _pleaseRemoveOsStream;
122 121

	
123 122
  bool _scaleToA4;
124 123

	
125 124
  std::string _title;
126 125
  std::string _copyright;
127 126

	
128 127
  enum NodeTextColorType
129 128
    { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
130 129
  ConstMap<typename Graph::Node,Color > _nodeTextColors;
131 130

	
132 131
  bool _autoNodeScale;
133 132
  bool _autoArcWidthScale;
134 133

	
135 134
  bool _absoluteNodeSizes;
136 135
  bool _absoluteArcWidths;
137 136

	
138 137
  bool _negY;
139 138

	
140 139
  bool _preScale;
141 140
  ///Constructor
142 141

	
143 142
  ///Constructor
144
  ///\param _g  Reference to the graph to be printed.
145
  ///\param _os Reference to the output stream.
146
  ///\param _os Reference to the output stream.
143
  ///\param gr  Reference to the graph to be printed.
144
  ///\param ost Reference to the output stream.
147 145
  ///By default it is <tt>std::cout</tt>.
148
  ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
146
  ///\param pros If it is \c true, then the \c ostream referenced by \c os
149 147
  ///will be explicitly deallocated by the destructor.
150
  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
151
                          bool _pros=false) :
152
    g(_g), os(_os),
148
  DefaultGraphToEpsTraits(const GR &gr, std::ostream& ost = std::cout,
149
                          bool pros = false) :
150
    g(gr), os(ost),
153 151
    _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0),
154 152
    _nodeColors(WHITE), _arcColors(BLACK),
155 153
    _arcWidths(1.0), _arcWidthScale(0.003),
156 154
    _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0),
157 155
    _nodeBorderQuotient(.1),
158 156
    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
159 157
    _showNodes(true), _showArcs(true),
160 158
    _enableParallel(false), _parArcDist(1),
161 159
    _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
162 160
    _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
163
    _undirected(lemon::UndirectedTagIndicator<G>::value),
164
    _pleaseRemoveOsStream(_pros), _scaleToA4(false),
161
    _undirected(lemon::UndirectedTagIndicator<GR>::value),
162
    _pleaseRemoveOsStream(pros), _scaleToA4(false),
165 163
    _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK),
166 164
    _autoNodeScale(false),
167 165
    _autoArcWidthScale(false),
168 166
    _absoluteNodeSizes(false),
169 167
    _absoluteArcWidths(false),
170 168
    _negY(false),
171 169
    _preScale(true)
172 170
  {}
173 171
};
174 172

	
175 173
///Auxiliary class to implement the named parameters of \ref graphToEps()
176 174

	
177 175
///Auxiliary class to implement the named parameters of \ref graphToEps().
178 176
///
179 177
///For detailed examples see the \ref graph_to_eps_demo.cc demo file.
180 178
template<class T> class GraphToEps : public T
181 179
{
182 180
  // Can't believe it is required by the C++ standard
183 181
  using T::g;
184 182
  using T::os;
185 183

	
186 184
  using T::_coords;
187 185
  using T::_nodeSizes;
188 186
  using T::_nodeShapes;
189 187
  using T::_nodeColors;
190 188
  using T::_arcColors;
191 189
  using T::_arcWidths;
192 190

	
193 191
  using T::_arcWidthScale;
194 192
  using T::_nodeScale;
195 193
  using T::_xBorder;
196 194
  using T::_yBorder;
197 195
  using T::_scale;
198 196
  using T::_nodeBorderQuotient;
199 197

	
200 198
  using T::_drawArrows;
201 199
  using T::_arrowLength;
202 200
  using T::_arrowWidth;
203 201

	
204 202
  using T::_showNodes;
205 203
  using T::_showArcs;
206 204

	
207 205
  using T::_enableParallel;
208 206
  using T::_parArcDist;
209 207

	
210 208
  using T::_showNodeText;
211 209
  using T::_nodeTexts;
212 210
  using T::_nodeTextSize;
213 211

	
214 212
  using T::_showNodePsText;
215 213
  using T::_nodePsTexts;
216 214
  using T::_nodePsTextsPreamble;
217 215

	
218 216
  using T::_undirected;
219 217

	
220 218
  using T::_pleaseRemoveOsStream;
221 219

	
222 220
  using T::_scaleToA4;
223 221

	
224 222
  using T::_title;
225 223
  using T::_copyright;
226 224

	
227 225
  using T::NodeTextColorType;
228 226
  using T::CUST_COL;
229 227
  using T::DIST_COL;
230 228
  using T::DIST_BW;
231 229
  using T::_nodeTextColorType;
232 230
  using T::_nodeTextColors;
233 231

	
234 232
  using T::_autoNodeScale;
235 233
  using T::_autoArcWidthScale;
236 234

	
237 235
  using T::_absoluteNodeSizes;
238 236
  using T::_absoluteArcWidths;
239 237

	
240 238

	
241 239
  using T::_negY;
242 240
  using T::_preScale;
243 241

	
244 242
  // dradnats ++C eht yb deriuqer si ti eveileb t'naC
245 243

	
246 244
  typedef typename T::Graph Graph;
245
  typedef typename T::Digraph Digraph;
247 246
  typedef typename Graph::Node Node;
248 247
  typedef typename Graph::NodeIt NodeIt;
249 248
  typedef typename Graph::Arc Arc;
250 249
  typedef typename Graph::ArcIt ArcIt;
251 250
  typedef typename Graph::InArcIt InArcIt;
252 251
  typedef typename Graph::OutArcIt OutArcIt;
253 252

	
254 253
  static const int INTERPOL_PREC;
255 254
  static const double A4HEIGHT;
256 255
  static const double A4WIDTH;
257 256
  static const double A4BORDER;
258 257

	
259 258
  bool dontPrint;
260 259

	
261 260
public:
262 261
  ///Node shapes
263 262

	
264 263
  ///Node shapes.
265 264
  ///
266 265
  enum NodeShapes {
267 266
    /// = 0
268 267
    ///\image html nodeshape_0.png
269 268
    ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
270 269
    CIRCLE=0,
271 270
    /// = 1
272 271
    ///\image html nodeshape_1.png
273 272
    ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
274
    ///
275 273
    SQUARE=1,
276 274
    /// = 2
277 275
    ///\image html nodeshape_2.png
278 276
    ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
279
    ///
280 277
    DIAMOND=2,
281 278
    /// = 3
282 279
    ///\image html nodeshape_3.png
283
    ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
284
    ///
280
    ///\image latex nodeshape_3.eps "MALE shape (3)" width=2cm
285 281
    MALE=3,
286 282
    /// = 4
287 283
    ///\image html nodeshape_4.png
288
    ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
289
    ///
284
    ///\image latex nodeshape_4.eps "FEMALE shape (4)" width=2cm
290 285
    FEMALE=4
291 286
  };
292 287

	
293 288
private:
294 289
  class arcLess {
295 290
    const Graph &g;
296 291
  public:
297 292
    arcLess(const Graph &_g) : g(_g) {}
298 293
    bool operator()(Arc a,Arc b) const
299 294
    {
300 295
      Node ai=std::min(g.source(a),g.target(a));
301 296
      Node aa=std::max(g.source(a),g.target(a));
302 297
      Node bi=std::min(g.source(b),g.target(b));
303 298
      Node ba=std::max(g.source(b),g.target(b));
304 299
      return ai<bi ||
305 300
        (ai==bi && (aa < ba ||
306 301
                    (aa==ba && ai==g.source(a) && bi==g.target(b))));
307 302
    }
308 303
  };
309 304
  bool isParallel(Arc e,Arc f) const
310 305
  {
311 306
    return (g.source(e)==g.source(f)&&
312 307
            g.target(e)==g.target(f)) ||
313 308
      (g.source(e)==g.target(f)&&
314 309
       g.target(e)==g.source(f));
315 310
  }
316 311
  template<class TT>
317 312
  static std::string psOut(const dim2::Point<TT> &p)
318 313
    {
319 314
      std::ostringstream os;
320 315
      os << p.x << ' ' << p.y;
321 316
      return os.str();
322 317
    }
323 318
  static std::string psOut(const Color &c)
324 319
    {
325 320
      std::ostringstream os;
326 321
      os << c.red() << ' ' << c.green() << ' ' << c.blue();
327 322
      return os.str();
328 323
    }
329 324

	
330 325
public:
331 326
  GraphToEps(const T &t) : T(t), dontPrint(false) {};
332 327

	
333 328
  template<class X> struct CoordsTraits : public T {
334 329
  typedef X CoordsMapType;
335 330
    const X &_coords;
336 331
    CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
337 332
  };
338 333
  ///Sets the map of the node coordinates
339 334

	
340 335
  ///Sets the map of the node coordinates.
341 336
  ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
342 337
  ///\ref dim2::Point "dim2::Point<int>" values.
343 338
  template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
344 339
    dontPrint=true;
345 340
    return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
346 341
  }
347 342
  template<class X> struct NodeSizesTraits : public T {
348 343
    const X &_nodeSizes;
349 344
    NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
350 345
  };
351 346
  ///Sets the map of the node sizes
352 347

	
353 348
  ///Sets the map of the node sizes.
354 349
  ///\param x must be a node map with \c double (or convertible) values.
355 350
  template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
356 351
  {
357 352
    dontPrint=true;
358 353
    return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
359 354
  }
360 355
  template<class X> struct NodeShapesTraits : public T {
361 356
    const X &_nodeShapes;
362 357
    NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
363 358
  };
364 359
  ///Sets the map of the node shapes
365 360

	
366 361
  ///Sets the map of the node shapes.
367 362
  ///The available shape values
368 363
  ///can be found in \ref NodeShapes "enum NodeShapes".
369 364
  ///\param x must be a node map with \c int (or convertible) values.
370 365
  ///\sa NodeShapes
371 366
  template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
372 367
  {
373 368
    dontPrint=true;
374 369
    return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
375 370
  }
376 371
  template<class X> struct NodeTextsTraits : public T {
377 372
    const X &_nodeTexts;
378 373
    NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
379 374
  };
380 375
  ///Sets the text printed on the nodes
381 376

	
382 377
  ///Sets the text printed on the nodes.
383 378
  ///\param x must be a node map with type that can be pushed to a standard
384 379
  ///\c ostream.
385 380
  template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
... ...
@@ -586,215 +581,205 @@
586 581
  GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
587 582

	
588 583
  ///Sets the size of the node texts
589 584
  GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
590 585

	
591 586
  ///Sets the color of the node texts to be different from the node color
592 587

	
593 588
  ///Sets the color of the node texts to be as different from the node color
594 589
  ///as it is possible.
595 590
  GraphToEps<T> &distantColorNodeTexts()
596 591
  {_nodeTextColorType=DIST_COL;return *this;}
597 592
  ///Sets the color of the node texts to be black or white and always visible.
598 593

	
599 594
  ///Sets the color of the node texts to be black or white according to
600 595
  ///which is more different from the node color.
601 596
  GraphToEps<T> &distantBWNodeTexts()
602 597
  {_nodeTextColorType=DIST_BW;return *this;}
603 598

	
604 599
  ///Gives a preamble block for node Postscript block.
605 600

	
606 601
  ///Gives a preamble block for node Postscript block.
607 602
  ///
608 603
  ///\sa nodePsTexts()
609 604
  GraphToEps<T> & nodePsTextsPreamble(const char *str) {
610 605
    _nodePsTextsPreamble=str ;return *this;
611 606
  }
612 607
  ///Sets whether the graph is undirected
613 608

	
614 609
  ///Sets whether the graph is undirected.
615 610
  ///
616 611
  ///This setting is the default for undirected graphs.
617 612
  ///
618 613
  ///\sa directed()
619 614
   GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
620 615

	
621 616
  ///Sets whether the graph is directed
622 617

	
623 618
  ///Sets whether the graph is directed.
624 619
  ///Use it to show the edges as a pair of directed ones.
625 620
  ///
626 621
  ///This setting is the default for digraphs.
627 622
  ///
628 623
  ///\sa undirected()
629 624
  GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
630 625

	
631 626
  ///Sets the title.
632 627

	
633 628
  ///Sets the title of the generated image,
634 629
  ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
635 630
  ///the EPS file.
636 631
  GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
637 632
  ///Sets the copyright statement.
638 633

	
639 634
  ///Sets the copyright statement of the generated image,
640 635
  ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
641 636
  ///the EPS file.
642 637
  GraphToEps<T> &copyright(const std::string &t) {_copyright=t;return *this;}
643 638

	
644 639
protected:
645 640
  bool isInsideNode(dim2::Point<double> p, double r,int t)
646 641
  {
647 642
    switch(t) {
648 643
    case CIRCLE:
649 644
    case MALE:
650 645
    case FEMALE:
651 646
      return p.normSquare()<=r*r;
652 647
    case SQUARE:
653 648
      return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
654 649
    case DIAMOND:
655 650
      return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
656 651
    }
657 652
    return false;
658 653
  }
659 654

	
660 655
public:
661 656
  ~GraphToEps() { }
662 657

	
663 658
  ///Draws the graph.
664 659

	
665 660
  ///Like other functions using
666 661
  ///\ref named-templ-func-param "named template parameters",
667 662
  ///this function calls the algorithm itself, i.e. in this case
668 663
  ///it draws the graph.
669 664
  void run() {
670 665
    const double EPSILON=1e-9;
671 666
    if(dontPrint) return;
672 667

	
673 668
    _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
674 669
      mycoords(_coords,_negY);
675 670

	
676 671
    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
677 672
    if(_title.size()>0) os << "%%Title: " << _title << '\n';
678 673
     if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
679 674
    os << "%%Creator: LEMON, graphToEps()\n";
680 675

	
681 676
    {
677
      os << "%%CreationDate: ";
682 678
#ifndef WIN32
683 679
      timeval tv;
684 680
      gettimeofday(&tv, 0);
685 681

	
686 682
      char cbuf[26];
687 683
      ctime_r(&tv.tv_sec,cbuf);
688
      os << "%%CreationDate: " << cbuf;
684
      os << cbuf;
689 685
#else
690
      SYSTEMTIME time;
691
      char buf1[11], buf2[9], buf3[5];
692

	
693
      GetSystemTime(&time);
694
      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
695
                        "ddd MMM dd", buf1, 11) &&
696
          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
697
                        "HH':'mm':'ss", buf2, 9) &&
698
          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
699
                                "yyyy", buf3, 5)) {
700
        os << "%%CreationDate: " << buf1 << ' '
701
           << buf2 << ' ' << buf3 << std::endl;
702
      }
686
      os << bits::getWinFormattedDate();
703 687
#endif
704 688
    }
689
    os << std::endl;
705 690

	
706 691
    if (_autoArcWidthScale) {
707 692
      double max_w=0;
708 693
      for(ArcIt e(g);e!=INVALID;++e)
709 694
        max_w=std::max(double(_arcWidths[e]),max_w);
710 695
      if(max_w>EPSILON) {
711 696
        _arcWidthScale/=max_w;
712 697
      }
713 698
    }
714 699

	
715 700
    if (_autoNodeScale) {
716 701
      double max_s=0;
717 702
      for(NodeIt n(g);n!=INVALID;++n)
718 703
        max_s=std::max(double(_nodeSizes[n]),max_s);
719 704
      if(max_s>EPSILON) {
720 705
        _nodeScale/=max_s;
721 706
      }
722 707
    }
723 708

	
724 709
    double diag_len = 1;
725 710
    if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
726 711
      dim2::Box<double> bb;
727 712
      for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
728 713
      if (bb.empty()) {
729 714
        bb = dim2::Box<double>(dim2::Point<double>(0,0));
730 715
      }
731 716
      diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
732 717
      if(diag_len<EPSILON) diag_len = 1;
733 718
      if(!_absoluteNodeSizes) _nodeScale*=diag_len;
734 719
      if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
735 720
    }
736 721

	
737 722
    dim2::Box<double> bb;
738 723
    for(NodeIt n(g);n!=INVALID;++n) {
739 724
      double ns=_nodeSizes[n]*_nodeScale;
740 725
      dim2::Point<double> p(ns,ns);
741 726
      switch(_nodeShapes[n]) {
742 727
      case CIRCLE:
743 728
      case SQUARE:
744 729
      case DIAMOND:
745 730
        bb.add(p+mycoords[n]);
746 731
        bb.add(-p+mycoords[n]);
747 732
        break;
748 733
      case MALE:
749 734
        bb.add(-p+mycoords[n]);
750 735
        bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
751 736
        break;
752 737
      case FEMALE:
753 738
        bb.add(p+mycoords[n]);
754 739
        bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
755 740
        break;
756 741
      }
757 742
    }
758 743
    if (bb.empty()) {
759 744
      bb = dim2::Box<double>(dim2::Point<double>(0,0));
760 745
    }
761 746

	
762 747
    if(_scaleToA4)
763 748
      os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
764 749
    else {
765 750
      if(_preScale) {
766 751
        //Rescale so that BoundingBox won't be neither to big nor too small.
767 752
        while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
768 753
        while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
769 754
      }
770 755

	
771 756
      os << "%%BoundingBox: "
772 757
         << int(floor(bb.left()   * _scale - _xBorder)) << ' '
773 758
         << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
774 759
         << int(ceil(bb.right()  * _scale + _xBorder)) << ' '
775 760
         << int(ceil(bb.top()    * _scale + _yBorder)) << '\n';
776 761
    }
777 762

	
778 763
    os << "%%EndComments\n";
779 764

	
780 765
    //x1 y1 x2 y2 x3 y3 cr cg cb w
781 766
    os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
782 767
       << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
783 768
    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }"
784 769
       << " bind def\n";
785 770
    //x y r
786 771
    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }"
787 772
       << " bind def\n";
788 773
    //x y r
789 774
    os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
790 775
       << "      2 index 1 index sub 2 index 2 index add lineto\n"
791 776
       << "      2 index 1 index sub 2 index 2 index sub lineto\n"
792 777
       << "      2 index 1 index add 2 index 2 index sub lineto\n"
793 778
       << "      closepath pop pop pop} bind def\n";
794 779
    //x y r
795 780
    os << "/di { newpath 2 index 1 index add 2 index moveto\n"
796 781
       << "      2 index             2 index 2 index add lineto\n"
797 782
       << "      2 index 1 index sub 2 index             lineto\n"
798 783
       << "      2 index             2 index 2 index sub lineto\n"
799 784
       << "      closepath pop pop pop} bind def\n";
800 785
    // x y r cr cg cb
... ...
@@ -1053,150 +1038,150 @@
1053 1038
          os << "0 0 0 setrgbcolor\n";
1054 1039
        }
1055 1040
        os << mycoords[n].x << ' ' << mycoords[n].y
1056 1041
           << " (" << _nodeTexts[n] << ") cshow\n";
1057 1042
      }
1058 1043
      os << "grestore\n";
1059 1044
    }
1060 1045
    if(_showNodePsText) {
1061 1046
      os << "%Node PS blocks:\ngsave\n";
1062 1047
      for(NodeIt n(g);n!=INVALID;++n)
1063 1048
        os << mycoords[n].x << ' ' << mycoords[n].y
1064 1049
           << " moveto\n" << _nodePsTexts[n] << "\n";
1065 1050
      os << "grestore\n";
1066 1051
    }
1067 1052

	
1068 1053
    os << "grestore\nshowpage\n";
1069 1054

	
1070 1055
    //CleanUp:
1071 1056
    if(_pleaseRemoveOsStream) {delete &os;}
1072 1057
  }
1073 1058

	
1074 1059
  ///\name Aliases
1075 1060
  ///These are just some aliases to other parameter setting functions.
1076 1061

	
1077 1062
  ///@{
1078 1063

	
1079 1064
  ///An alias for arcWidths()
1080 1065
  template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x)
1081 1066
  {
1082 1067
    return arcWidths(x);
1083 1068
  }
1084 1069

	
1085 1070
  ///An alias for arcColors()
1086 1071
  template<class X> GraphToEps<ArcColorsTraits<X> >
1087 1072
  edgeColors(const X &x)
1088 1073
  {
1089 1074
    return arcColors(x);
1090 1075
  }
1091 1076

	
1092 1077
  ///An alias for arcWidthScale()
1093 1078
  GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);}
1094 1079

	
1095 1080
  ///An alias for autoArcWidthScale()
1096 1081
  GraphToEps<T> &autoEdgeWidthScale(bool b=true)
1097 1082
  {
1098 1083
    return autoArcWidthScale(b);
1099 1084
  }
1100 1085

	
1101 1086
  ///An alias for absoluteArcWidths()
1102 1087
  GraphToEps<T> &absoluteEdgeWidths(bool b=true)
1103 1088
  {
1104 1089
    return absoluteArcWidths(b);
1105 1090
  }
1106 1091

	
1107 1092
  ///An alias for parArcDist()
1108 1093
  GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
1109 1094

	
1110 1095
  ///An alias for hideArcs()
1111 1096
  GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
1112 1097

	
1113 1098
  ///@}
1114 1099
};
1115 1100

	
1116 1101
template<class T>
1117 1102
const int GraphToEps<T>::INTERPOL_PREC = 20;
1118 1103
template<class T>
1119 1104
const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1120 1105
template<class T>
1121 1106
const double GraphToEps<T>::A4WIDTH  = 595.275590551181;
1122 1107
template<class T>
1123 1108
const double GraphToEps<T>::A4BORDER = 15;
1124 1109

	
1125 1110

	
1126 1111
///Generates an EPS file from a graph
1127 1112

	
1128 1113
///\ingroup eps_io
1129 1114
///Generates an EPS file from a graph.
1130 1115
///\param g Reference to the graph to be printed.
1131 1116
///\param os Reference to the output stream.
1132 1117
///By default it is <tt>std::cout</tt>.
1133 1118
///
1134 1119
///This function also has a lot of
1135 1120
///\ref named-templ-func-param "named parameters",
1136 1121
///they are declared as the members of class \ref GraphToEps. The following
1137 1122
///example shows how to use these parameters.
1138 1123
///\code
1139 1124
/// graphToEps(g,os).scale(10).coords(coords)
1140 1125
///              .nodeScale(2).nodeSizes(sizes)
1141 1126
///              .arcWidthScale(.4).run();
1142 1127
///\endcode
1143 1128
///
1144 1129
///For more detailed examples see the \ref graph_to_eps_demo.cc demo file.
1145 1130
///
1146 1131
///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1147 1132
///to the end of the parameter list.
1148 1133
///\sa GraphToEps
1149
///\sa graphToEps(G &g, const char *file_name)
1150
template<class G>
1151
GraphToEps<DefaultGraphToEpsTraits<G> >
1152
graphToEps(G &g, std::ostream& os=std::cout)
1134
///\sa graphToEps(GR &g, const char *file_name)
1135
template<class GR>
1136
GraphToEps<DefaultGraphToEpsTraits<GR> >
1137
graphToEps(GR &g, std::ostream& os=std::cout)
1153 1138
{
1154 1139
  return
1155
    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1140
    GraphToEps<DefaultGraphToEpsTraits<GR> >(DefaultGraphToEpsTraits<GR>(g,os));
1156 1141
}
1157 1142

	
1158 1143
///Generates an EPS file from a graph
1159 1144

	
1160 1145
///\ingroup eps_io
1161 1146
///This function does the same as
1162
///\ref graphToEps(G &g,std::ostream& os)
1147
///\ref graphToEps(GR &g,std::ostream& os)
1163 1148
///but it writes its output into the file \c file_name
1164 1149
///instead of a stream.
1165
///\sa graphToEps(G &g, std::ostream& os)
1166
template<class G>
1167
GraphToEps<DefaultGraphToEpsTraits<G> >
1168
graphToEps(G &g,const char *file_name)
1150
///\sa graphToEps(GR &g, std::ostream& os)
1151
template<class GR>
1152
GraphToEps<DefaultGraphToEpsTraits<GR> >
1153
graphToEps(GR &g,const char *file_name)
1169 1154
{
1170 1155
  std::ostream* os = new std::ofstream(file_name);
1171 1156
  if (!(*os)) {
1172 1157
    delete os;
1173 1158
    throw IoError("Cannot write file", file_name);
1174 1159
  }
1175
  return GraphToEps<DefaultGraphToEpsTraits<G> >
1176
    (DefaultGraphToEpsTraits<G>(g,*os,true));
1160
  return GraphToEps<DefaultGraphToEpsTraits<GR> >
1161
    (DefaultGraphToEpsTraits<GR>(g,*os,true));
1177 1162
}
1178 1163

	
1179 1164
///Generates an EPS file from a graph
1180 1165

	
1181 1166
///\ingroup eps_io
1182 1167
///This function does the same as
1183
///\ref graphToEps(G &g,std::ostream& os)
1168
///\ref graphToEps(GR &g,std::ostream& os)
1184 1169
///but it writes its output into the file \c file_name
1185 1170
///instead of a stream.
1186
///\sa graphToEps(G &g, std::ostream& os)
1187
template<class G>
1188
GraphToEps<DefaultGraphToEpsTraits<G> >
1189
graphToEps(G &g,const std::string& file_name)
1171
///\sa graphToEps(GR &g, std::ostream& os)
1172
template<class GR>
1173
GraphToEps<DefaultGraphToEpsTraits<GR> >
1174
graphToEps(GR &g,const std::string& file_name)
1190 1175
{
1191 1176
  std::ostream* os = new std::ofstream(file_name.c_str());
1192 1177
  if (!(*os)) {
1193 1178
    delete os;
1194 1179
    throw IoError("Cannot write file", file_name);
1195 1180
  }
1196
  return GraphToEps<DefaultGraphToEpsTraits<G> >
1197
    (DefaultGraphToEpsTraits<G>(g,*os,true));
1181
  return GraphToEps<DefaultGraphToEpsTraits<GR> >
1182
    (DefaultGraphToEpsTraits<GR>(g,*os,true));
1198 1183
}
1199 1184

	
1200 1185
} //END OF NAMESPACE LEMON
1201 1186

	
1202 1187
#endif // LEMON_GRAPH_TO_EPS_H
Ignore white space 6 line context
... ...
@@ -377,329 +377,321 @@
377 377
          (node._id / _width) * (_width - 1) - 1;
378 378
        dir = false;
379 379
        return;
380 380
      }
381 381
      if (node._id >= _width) {
382 382
        edge._id = node._id - _width;
383 383
        dir = false;
384 384
        return;
385 385
      }
386 386
      edge._id = -1;
387 387
      dir = true;
388 388
    }
389 389

	
390 390
    void nextInc(Edge& edge, bool& dir) const {
391 391
      int nid = edge._id;
392 392
      if (dir) {
393 393
        if (nid >= _edge_limit) {
394 394
          nid = (nid - _edge_limit) % (_width - 1) +
395 395
            (nid - _edge_limit) / (_width - 1) * _width;
396 396
          if (nid < _node_num - _width) {
397 397
            edge._id = nid;
398 398
            return;
399 399
          }
400 400
        }
401 401
        if (nid % _width > 0) {
402 402
          edge._id = _edge_limit + nid % _width +
403 403
            (nid / _width) * (_width - 1) - 1;
404 404
          dir = false;
405 405
          return;
406 406
        }
407 407
        if (nid >= _width) {
408 408
          edge._id = nid - _width;
409 409
          dir = false;
410 410
          return;
411 411
        }
412 412
      } else {
413 413
        if (nid >= _edge_limit) {
414 414
          nid = (nid - _edge_limit) % (_width - 1) +
415 415
            (nid - _edge_limit) / (_width - 1) * _width + 1;
416 416
          if (nid >= _width) {
417 417
            edge._id = nid - _width;
418 418
            return;
419 419
          }
420 420
        }
421 421
      }
422 422
      edge._id = -1;
423 423
      dir = true;
424 424
    }
425 425

	
426 426
    Arc right(Node n) const {
427 427
      if (n._id % _width < _width - 1) {
428 428
        return Arc(((_edge_limit + n._id % _width +
429 429
                    (n._id / _width) * (_width - 1)) << 1) | 1);
430 430
      } else {
431 431
        return INVALID;
432 432
      }
433 433
    }
434 434

	
435 435
    Arc left(Node n) const {
436 436
      if (n._id % _width > 0) {
437 437
        return Arc((_edge_limit + n._id % _width +
438 438
                     (n._id / _width) * (_width - 1) - 1) << 1);
439 439
      } else {
440 440
        return INVALID;
441 441
      }
442 442
    }
443 443

	
444 444
    Arc up(Node n) const {
445 445
      if (n._id < _edge_limit) {
446 446
        return Arc((n._id << 1) | 1);
447 447
      } else {
448 448
        return INVALID;
449 449
      }
450 450
    }
451 451

	
452 452
    Arc down(Node n) const {
453 453
      if (n._id >= _width) {
454 454
        return Arc((n._id - _width) << 1);
455 455
      } else {
456 456
        return INVALID;
457 457
      }
458 458
    }
459 459

	
460 460
  private:
461 461
    int _width, _height;
462 462
    int _node_num, _edge_num;
463 463
    int _edge_limit;
464 464
  };
465 465

	
466 466

	
467 467
  typedef GraphExtender<GridGraphBase> ExtendedGridGraphBase;
468 468

	
469 469
  /// \ingroup graphs
470 470
  ///
471 471
  /// \brief Grid graph class
472 472
  ///
473
  /// This class implements a special graph type. The nodes of the
474
  /// graph can be indexed by two integer \c (i,j) value where \c i is
475
  /// in the \c [0..width()-1] range and j is in the \c
476
  /// [0..height()-1] range.  Two nodes are connected in the graph if
477
  /// the indexes differ exactly on one position and exactly one is
478
  /// the difference. The nodes of the graph can be indexed by position
479
  /// with the \c operator()() function. The positions of the nodes can be
480
  /// get with \c pos(), \c col() and \c row() members. The outgoing
473
  /// GridGraph implements a special graph type. The nodes of the
474
  /// graph can be indexed by two integer values \c (i,j) where \c i is
475
  /// in the range <tt>[0..width()-1]</tt> and j is in the range
476
  /// <tt>[0..height()-1]</tt>. Two nodes are connected in the graph if
477
  /// the indices differ exactly on one position and the difference is
478
  /// also exactly one. The nodes of the graph can be obtained by position
479
  /// using the \c operator()() function and the indices of the nodes can
480
  /// be obtained using \c pos(), \c col() and \c row() members. The outgoing
481 481
  /// arcs can be retrieved with the \c right(), \c up(), \c left()
482 482
  /// and \c down() functions, where the bottom-left corner is the
483 483
  /// origin.
484 484
  ///
485
  /// This class is completely static and it needs constant memory space.
486
  /// Thus you can neither add nor delete nodes or edges, however
487
  /// the structure can be resized using resize().
488
  ///
485 489
  /// \image html grid_graph.png
486 490
  /// \image latex grid_graph.eps "Grid graph" width=\textwidth
487 491
  ///
488 492
  /// A short example about the basic usage:
489 493
  ///\code
490 494
  /// GridGraph graph(rows, cols);
491 495
  /// GridGraph::NodeMap<int> val(graph);
492 496
  /// for (int i = 0; i < graph.width(); ++i) {
493 497
  ///   for (int j = 0; j < graph.height(); ++j) {
494 498
  ///     val[graph(i, j)] = i + j;
495 499
  ///   }
496 500
  /// }
497 501
  ///\endcode
498 502
  ///
499
  /// This graph type is fully conform to the \ref concepts::Graph
500
  /// "Graph" concept, and it also has an important extra feature
501
  /// that its maps are real \ref concepts::ReferenceMap
502
  /// "reference map"s.
503
  /// This type fully conforms to the \ref concepts::Graph "Graph concept".
504
  /// Most of its member functions and nested classes are documented
505
  /// only in the concept class.
503 506
  class GridGraph : public ExtendedGridGraphBase {
507
    typedef ExtendedGridGraphBase Parent;
508

	
504 509
  public:
505 510

	
506
    typedef ExtendedGridGraphBase Parent;
507

	
508
    /// \brief Map to get the indices of the nodes as dim2::Point<int>.
511
    /// \brief Map to get the indices of the nodes as \ref dim2::Point
512
    /// "dim2::Point<int>".
509 513
    ///
510
    /// Map to get the indices of the nodes as dim2::Point<int>.
514
    /// Map to get the indices of the nodes as \ref dim2::Point
515
    /// "dim2::Point<int>".
511 516
    class IndexMap {
512 517
    public:
513 518
      /// \brief The key type of the map
514 519
      typedef GridGraph::Node Key;
515 520
      /// \brief The value type of the map
516 521
      typedef dim2::Point<int> Value;
517 522

	
518 523
      /// \brief Constructor
519
      ///
520
      /// Constructor
521 524
      IndexMap(const GridGraph& graph) : _graph(graph) {}
522 525

	
523 526
      /// \brief The subscript operator
524
      ///
525
      /// The subscript operator.
526 527
      Value operator[](Key key) const {
527 528
        return _graph.pos(key);
528 529
      }
529 530

	
530 531
    private:
531 532
      const GridGraph& _graph;
532 533
    };
533 534

	
534 535
    /// \brief Map to get the column of the nodes.
535 536
    ///
536 537
    /// Map to get the column of the nodes.
537 538
    class ColMap {
538 539
    public:
539 540
      /// \brief The key type of the map
540 541
      typedef GridGraph::Node Key;
541 542
      /// \brief The value type of the map
542 543
      typedef int Value;
543 544

	
544 545
      /// \brief Constructor
545
      ///
546
      /// Constructor
547 546
      ColMap(const GridGraph& graph) : _graph(graph) {}
548 547

	
549 548
      /// \brief The subscript operator
550
      ///
551
      /// The subscript operator.
552 549
      Value operator[](Key key) const {
553 550
        return _graph.col(key);
554 551
      }
555 552

	
556 553
    private:
557 554
      const GridGraph& _graph;
558 555
    };
559 556

	
560 557
    /// \brief Map to get the row of the nodes.
561 558
    ///
562 559
    /// Map to get the row of the nodes.
563 560
    class RowMap {
564 561
    public:
565 562
      /// \brief The key type of the map
566 563
      typedef GridGraph::Node Key;
567 564
      /// \brief The value type of the map
568 565
      typedef int Value;
569 566

	
570 567
      /// \brief Constructor
571
      ///
572
      /// Constructor
573 568
      RowMap(const GridGraph& graph) : _graph(graph) {}
574 569

	
575 570
      /// \brief The subscript operator
576
      ///
577
      /// The subscript operator.
578 571
      Value operator[](Key key) const {
579 572
        return _graph.row(key);
580 573
      }
581 574

	
582 575
    private:
583 576
      const GridGraph& _graph;
584 577
    };
585 578

	
586 579
    /// \brief Constructor
587 580
    ///
588
    /// Construct a grid graph with given size.
581
    /// Construct a grid graph with the given size.
589 582
    GridGraph(int width, int height) { construct(width, height); }
590 583

	
591
    /// \brief Resize the graph
584
    /// \brief Resizes the graph
592 585
    ///
593
    /// Resize the graph. The function will fully destroy and rebuild
594
    /// the graph.  This cause that the maps of the graph will
595
    /// reallocated automatically and the previous values will be
596
    /// lost.
586
    /// This function resizes the graph. It fully destroys and
587
    /// rebuilds the structure, therefore the maps of the graph will be
588
    /// reallocated automatically and the previous values will be lost.
597 589
    void resize(int width, int height) {
598 590
      Parent::notifier(Arc()).clear();
599 591
      Parent::notifier(Edge()).clear();
600 592
      Parent::notifier(Node()).clear();
601 593
      construct(width, height);
602 594
      Parent::notifier(Node()).build();
603 595
      Parent::notifier(Edge()).build();
604 596
      Parent::notifier(Arc()).build();
605 597
    }
606 598

	
607 599
    /// \brief The node on the given position.
608 600
    ///
609 601
    /// Gives back the node on the given position.
610 602
    Node operator()(int i, int j) const {
611 603
      return Parent::operator()(i, j);
612 604
    }
613 605

	
614
    /// \brief Gives back the column index of the node.
606
    /// \brief The column index of the node.
615 607
    ///
616 608
    /// Gives back the column index of the node.
617 609
    int col(Node n) const {
618 610
      return Parent::col(n);
619 611
    }
620 612

	
621
    /// \brief Gives back the row index of the node.
613
    /// \brief The row index of the node.
622 614
    ///
623 615
    /// Gives back the row index of the node.
624 616
    int row(Node n) const {
625 617
      return Parent::row(n);
626 618
    }
627 619

	
628
    /// \brief Gives back the position of the node.
620
    /// \brief The position of the node.
629 621
    ///
630 622
    /// Gives back the position of the node, ie. the <tt>(col,row)</tt> pair.
631 623
    dim2::Point<int> pos(Node n) const {
632 624
      return Parent::pos(n);
633 625
    }
634 626

	
635
    /// \brief Gives back the number of the columns.
627
    /// \brief The number of the columns.
636 628
    ///
637 629
    /// Gives back the number of the columns.
638 630
    int width() const {
639 631
      return Parent::width();
640 632
    }
641 633

	
642
    /// \brief Gives back the number of the rows.
634
    /// \brief The number of the rows.
643 635
    ///
644 636
    /// Gives back the number of the rows.
645 637
    int height() const {
646 638
      return Parent::height();
647 639
    }
648 640

	
649
    /// \brief Gives back the arc goes right from the node.
641
    /// \brief The arc goes right from the node.
650 642
    ///
651 643
    /// Gives back the arc goes right from the node. If there is not
652 644
    /// outgoing arc then it gives back INVALID.
653 645
    Arc right(Node n) const {
654 646
      return Parent::right(n);
655 647
    }
656 648

	
657
    /// \brief Gives back the arc goes left from the node.
649
    /// \brief The arc goes left from the node.
658 650
    ///
659 651
    /// Gives back the arc goes left from the node. If there is not
660 652
    /// outgoing arc then it gives back INVALID.
661 653
    Arc left(Node n) const {
662 654
      return Parent::left(n);
663 655
    }
664 656

	
665
    /// \brief Gives back the arc goes up from the node.
657
    /// \brief The arc goes up from the node.
666 658
    ///
667 659
    /// Gives back the arc goes up from the node. If there is not
668 660
    /// outgoing arc then it gives back INVALID.
669 661
    Arc up(Node n) const {
670 662
      return Parent::up(n);
671 663
    }
672 664

	
673
    /// \brief Gives back the arc goes down from the node.
665
    /// \brief The arc goes down from the node.
674 666
    ///
675 667
    /// Gives back the arc goes down from the node. If there is not
676 668
    /// outgoing arc then it gives back INVALID.
677 669
    Arc down(Node n) const {
678 670
      return Parent::down(n);
679 671
    }
680 672

	
681 673
    /// \brief Index map of the grid graph
682 674
    ///
683 675
    /// Just returns an IndexMap for the grid graph.
684 676
    IndexMap indexMap() const {
685 677
      return IndexMap(*this);
686 678
    }
687 679

	
688 680
    /// \brief Row map of the grid graph
689 681
    ///
690 682
    /// Just returns a RowMap for the grid graph.
691 683
    RowMap rowMap() const {
692 684
      return RowMap(*this);
693 685
    }
694 686

	
695 687
    /// \brief Column map of the grid graph
696 688
    ///
697 689
    /// Just returns a ColMap for the grid graph.
698 690
    ColMap colMap() const {
699 691
      return ColMap(*this);
700 692
    }
701 693

	
702 694
  };
703 695

	
704 696
}
705 697
#endif
Ignore white space 6 line context
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-2009
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_HAO_ORLIN_H
20 20
#define LEMON_HAO_ORLIN_H
21 21

	
22 22
#include <vector>
23 23
#include <list>
24 24
#include <limits>
25 25

	
26 26
#include <lemon/maps.h>
27 27
#include <lemon/core.h>
28 28
#include <lemon/tolerance.h>
29 29

	
30 30
/// \file
31 31
/// \ingroup min_cut
32 32
/// \brief Implementation of the Hao-Orlin algorithm.
33 33
///
34
/// Implementation of the Hao-Orlin algorithm class for testing network
35
/// reliability.
34
/// Implementation of the Hao-Orlin algorithm for finding a minimum cut 
35
/// in a digraph.
36 36

	
37 37
namespace lemon {
38 38

	
39 39
  /// \ingroup min_cut
40 40
  ///
41
  /// \brief %Hao-Orlin algorithm to find a minimum cut in directed graphs.
41
  /// \brief Hao-Orlin algorithm for finding a minimum cut in a digraph.
42 42
  ///
43
  /// Hao-Orlin calculates a minimum cut in a directed graph
44
  /// \f$D=(V,A)\f$. It takes a fixed node \f$ source \in V \f$ and
43
  /// This class implements the Hao-Orlin algorithm for finding a minimum
44
  /// value cut in a directed graph \f$D=(V,A)\f$. 
45
  /// It takes a fixed node \f$ source \in V \f$ and
45 46
  /// consists of two phases: in the first phase it determines a
46 47
  /// minimum cut with \f$ source \f$ on the source-side (i.e. a set
47
  /// \f$ X\subsetneq V \f$ with \f$ source \in X \f$ and minimal
48
  /// out-degree) and in the second phase it determines a minimum cut
48
  /// \f$ X\subsetneq V \f$ with \f$ source \in X \f$ and minimal outgoing
49
  /// capacity) and in the second phase it determines a minimum cut
49 50
  /// with \f$ source \f$ on the sink-side (i.e. a set
50
  /// \f$ X\subsetneq V \f$ with \f$ source \notin X \f$ and minimal
51
  /// out-degree). Obviously, the smaller of these two cuts will be a
51
  /// \f$ X\subsetneq V \f$ with \f$ source \notin X \f$ and minimal outgoing
52
  /// capacity). Obviously, the smaller of these two cuts will be a
52 53
  /// minimum cut of \f$ D \f$. The algorithm is a modified
53
  /// push-relabel preflow algorithm and our implementation calculates
54
  /// preflow push-relabel algorithm. Our implementation calculates
54 55
  /// the minimum cut in \f$ O(n^2\sqrt{m}) \f$ time (we use the
55 56
  /// highest-label rule), or in \f$O(nm)\f$ for unit capacities. The
56
  /// purpose of such algorithm is testing network reliability. For an
57
  /// undirected graph you can run just the first phase of the
58
  /// algorithm or you can use the algorithm of Nagamochi and Ibaraki
59
  /// which solves the undirected problem in
60
  /// \f$ O(nm + n^2 \log(n)) \f$ time: it is implemented in the
61
  /// NagamochiIbaraki algorithm class.
57
  /// purpose of such algorithm is e.g. testing network reliability.
62 58
  ///
63
  /// \param _Digraph is the graph type of the algorithm.
64
  /// \param _CapacityMap is an edge map of capacities which should
65
  /// be any numreric type. The default type is _Digraph::ArcMap<int>.
66
  /// \param _Tolerance is the handler of the inexact computation. The
67
  /// default type for this is Tolerance<CapacityMap::Value>.
59
  /// For an undirected graph you can run just the first phase of the
60
  /// algorithm or you can use the algorithm of Nagamochi and Ibaraki,
61
  /// which solves the undirected problem in \f$ O(nm + n^2 \log n) \f$ 
62
  /// time. It is implemented in the NagamochiIbaraki algorithm class.
63
  ///
64
  /// \tparam GR The type of the digraph the algorithm runs on.
65
  /// \tparam CAP The type of the arc map containing the capacities,
66
  /// which can be any numreric type. The default map type is
67
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
68
  /// \tparam TOL Tolerance class for handling inexact computations. The
69
  /// default tolerance type is \ref Tolerance "Tolerance<CAP::Value>".
68 70
#ifdef DOXYGEN
69
  template <typename _Digraph, typename _CapacityMap, typename _Tolerance>
71
  template <typename GR, typename CAP, typename TOL>
70 72
#else
71
  template <typename _Digraph,
72
            typename _CapacityMap = typename _Digraph::template ArcMap<int>,
73
            typename _Tolerance = Tolerance<typename _CapacityMap::Value> >
73
  template <typename GR,
74
            typename CAP = typename GR::template ArcMap<int>,
75
            typename TOL = Tolerance<typename CAP::Value> >
74 76
#endif
75 77
  class HaoOrlin {
78
  public:
79
   
80
    /// The digraph type of the algorithm
81
    typedef GR Digraph;
82
    /// The capacity map type of the algorithm
83
    typedef CAP CapacityMap;
84
    /// The tolerance type of the algorithm
85
    typedef TOL Tolerance;
86

	
76 87
  private:
77 88

	
78
    typedef _Digraph Digraph;
79
    typedef _CapacityMap CapacityMap;
80
    typedef _Tolerance Tolerance;
81

	
82 89
    typedef typename CapacityMap::Value Value;
83 90

	
84
    TEMPLATE_GRAPH_TYPEDEFS(Digraph);
91
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
85 92

	
86 93
    const Digraph& _graph;
87 94
    const CapacityMap* _capacity;
88 95

	
89 96
    typedef typename Digraph::template ArcMap<Value> FlowMap;
90 97
    FlowMap* _flow;
91 98

	
92 99
    Node _source;
93 100

	
94 101
    int _node_num;
95 102

	
96 103
    // Bucketing structure
97 104
    std::vector<Node> _first, _last;
98 105
    typename Digraph::template NodeMap<Node>* _next;
99 106
    typename Digraph::template NodeMap<Node>* _prev;
100 107
    typename Digraph::template NodeMap<bool>* _active;
101 108
    typename Digraph::template NodeMap<int>* _bucket;
102 109

	
103 110
    std::vector<bool> _dormant;
104 111

	
105 112
    std::list<std::list<int> > _sets;
106 113
    std::list<int>::iterator _highest;
107 114

	
108 115
    typedef typename Digraph::template NodeMap<Value> ExcessMap;
109 116
    ExcessMap* _excess;
110 117

	
111 118
    typedef typename Digraph::template NodeMap<bool> SourceSetMap;
112 119
    SourceSetMap* _source_set;
113 120

	
114 121
    Value _min_cut;
115 122

	
116 123
    typedef typename Digraph::template NodeMap<bool> MinCutMap;
117 124
    MinCutMap* _min_cut_map;
118 125

	
119 126
    Tolerance _tolerance;
120 127

	
121 128
  public:
122 129

	
123 130
    /// \brief Constructor
124 131
    ///
125 132
    /// Constructor of the algorithm class.
126 133
    HaoOrlin(const Digraph& graph, const CapacityMap& capacity,
127 134
             const Tolerance& tolerance = Tolerance()) :
128 135
      _graph(graph), _capacity(&capacity), _flow(0), _source(),
129 136
      _node_num(), _first(), _last(), _next(0), _prev(0),
130 137
      _active(0), _bucket(0), _dormant(), _sets(), _highest(),
131 138
      _excess(0), _source_set(0), _min_cut(), _min_cut_map(0),
132 139
      _tolerance(tolerance) {}
133 140

	
134 141
    ~HaoOrlin() {
135 142
      if (_min_cut_map) {
136 143
        delete _min_cut_map;
137 144
      }
138 145
      if (_source_set) {
139 146
        delete _source_set;
140 147
      }
141 148
      if (_excess) {
142 149
        delete _excess;
143 150
      }
144 151
      if (_next) {
145 152
        delete _next;
146 153
      }
147 154
      if (_prev) {
148 155
        delete _prev;
149 156
      }
150 157
      if (_active) {
151 158
        delete _active;
152 159
      }
153 160
      if (_bucket) {
154 161
        delete _bucket;
155 162
      }
156 163
      if (_flow) {
157 164
        delete _flow;
158 165
      }
159 166
    }
160 167

	
161 168
  private:
162 169

	
163 170
    void activate(const Node& i) {
164
      _active->set(i, true);
171
      (*_active)[i] = true;
165 172

	
166 173
      int bucket = (*_bucket)[i];
167 174

	
168 175
      if ((*_prev)[i] == INVALID || (*_active)[(*_prev)[i]]) return;
169 176
      //unlace
170
      _next->set((*_prev)[i], (*_next)[i]);
177
      (*_next)[(*_prev)[i]] = (*_next)[i];
171 178
      if ((*_next)[i] != INVALID) {
172
        _prev->set((*_next)[i], (*_prev)[i]);
179
        (*_prev)[(*_next)[i]] = (*_prev)[i];
173 180
      } else {
174 181
        _last[bucket] = (*_prev)[i];
175 182
      }
176 183
      //lace
177
      _next->set(i, _first[bucket]);
178
      _prev->set(_first[bucket], i);
179
      _prev->set(i, INVALID);
184
      (*_next)[i] = _first[bucket];
185
      (*_prev)[_first[bucket]] = i;
186
      (*_prev)[i] = INVALID;
180 187
      _first[bucket] = i;
181 188
    }
182 189

	
183 190
    void deactivate(const Node& i) {
184
      _active->set(i, false);
191
      (*_active)[i] = false;
185 192
      int bucket = (*_bucket)[i];
186 193

	
187 194
      if ((*_next)[i] == INVALID || !(*_active)[(*_next)[i]]) return;
188 195

	
189 196
      //unlace
190
      _prev->set((*_next)[i], (*_prev)[i]);
197
      (*_prev)[(*_next)[i]] = (*_prev)[i];
191 198
      if ((*_prev)[i] != INVALID) {
192
        _next->set((*_prev)[i], (*_next)[i]);
199
        (*_next)[(*_prev)[i]] = (*_next)[i];
193 200
      } else {
194 201
        _first[bucket] = (*_next)[i];
195 202
      }
196 203
      //lace
197
      _prev->set(i, _last[bucket]);
198
      _next->set(_last[bucket], i);
199
      _next->set(i, INVALID);
204
      (*_prev)[i] = _last[bucket];
205
      (*_next)[_last[bucket]] = i;
206
      (*_next)[i] = INVALID;
200 207
      _last[bucket] = i;
201 208
    }
202 209

	
203 210
    void addItem(const Node& i, int bucket) {
204 211
      (*_bucket)[i] = bucket;
205 212
      if (_last[bucket] != INVALID) {
206
        _prev->set(i, _last[bucket]);
207
        _next->set(_last[bucket], i);
208
        _next->set(i, INVALID);
213
        (*_prev)[i] = _last[bucket];
214
        (*_next)[_last[bucket]] = i;
215
        (*_next)[i] = INVALID;
209 216
        _last[bucket] = i;
210 217
      } else {
211
        _prev->set(i, INVALID);
218
        (*_prev)[i] = INVALID;
212 219
        _first[bucket] = i;
213
        _next->set(i, INVALID);
220
        (*_next)[i] = INVALID;
214 221
        _last[bucket] = i;
215 222
      }
216 223
    }
217 224

	
218 225
    void findMinCutOut() {
219 226

	
220 227
      for (NodeIt n(_graph); n != INVALID; ++n) {
221
        _excess->set(n, 0);
228
        (*_excess)[n] = 0;
229
        (*_source_set)[n] = false;
222 230
      }
223 231

	
224 232
      for (ArcIt a(_graph); a != INVALID; ++a) {
225
        _flow->set(a, 0);
233
        (*_flow)[a] = 0;
226 234
      }
227 235

	
228 236
      int bucket_num = 0;
229 237
      std::vector<Node> queue(_node_num);
230 238
      int qfirst = 0, qlast = 0, qsep = 0;
231 239

	
232 240
      {
233 241
        typename Digraph::template NodeMap<bool> reached(_graph, false);
234 242

	
235
        reached.set(_source, true);
243
        reached[_source] = true;
236 244
        bool first_set = true;
237 245

	
238 246
        for (NodeIt t(_graph); t != INVALID; ++t) {
239 247
          if (reached[t]) continue;
240 248
          _sets.push_front(std::list<int>());
241 249

	
242 250
          queue[qlast++] = t;
243
          reached.set(t, true);
251
          reached[t] = true;
244 252

	
245 253
          while (qfirst != qlast) {
246 254
            if (qsep == qfirst) {
247 255
              ++bucket_num;
248 256
              _sets.front().push_front(bucket_num);
249 257
              _dormant[bucket_num] = !first_set;
250 258
              _first[bucket_num] = _last[bucket_num] = INVALID;
251 259
              qsep = qlast;
252 260
            }
253 261

	
254 262
            Node n = queue[qfirst++];
255 263
            addItem(n, bucket_num);
256 264

	
257 265
            for (InArcIt a(_graph, n); a != INVALID; ++a) {
258 266
              Node u = _graph.source(a);
259 267
              if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
260
                reached.set(u, true);
268
                reached[u] = true;
261 269
                queue[qlast++] = u;
262 270
              }
263 271
            }
264 272
          }
265 273
          first_set = false;
266 274
        }
267 275

	
268 276
        ++bucket_num;
269
        _bucket->set(_source, 0);
277
        (*_bucket)[_source] = 0;
270 278
        _dormant[0] = true;
271 279
      }
272
      _source_set->set(_source, true);
280
      (*_source_set)[_source] = true;
273 281

	
274 282
      Node target = _last[_sets.back().back()];
275 283
      {
276 284
        for (OutArcIt a(_graph, _source); a != INVALID; ++a) {
277 285
          if (_tolerance.positive((*_capacity)[a])) {
278 286
            Node u = _graph.target(a);
279
            _flow->set(a, (*_capacity)[a]);
280
            _excess->set(u, (*_excess)[u] + (*_capacity)[a]);
287
            (*_flow)[a] = (*_capacity)[a];
288
            (*_excess)[u] += (*_capacity)[a];
281 289
            if (!(*_active)[u] && u != _source) {
282 290
              activate(u);
283 291
            }
284 292
          }
285 293
        }
286 294

	
287 295
        if ((*_active)[target]) {
288 296
          deactivate(target);
289 297
        }
290 298

	
291 299
        _highest = _sets.back().begin();
292 300
        while (_highest != _sets.back().end() &&
293 301
               !(*_active)[_first[*_highest]]) {
294 302
          ++_highest;
295 303
        }
296 304
      }
297 305

	
298 306
      while (true) {
299 307
        while (_highest != _sets.back().end()) {
300 308
          Node n = _first[*_highest];
301 309
          Value excess = (*_excess)[n];
302 310
          int next_bucket = _node_num;
303 311

	
304 312
          int under_bucket;
305 313
          if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
306 314
            under_bucket = -1;
307 315
          } else {
308 316
            under_bucket = *(++std::list<int>::iterator(_highest));
309 317
          }
310 318

	
311 319
          for (OutArcIt a(_graph, n); a != INVALID; ++a) {
312 320
            Node v = _graph.target(a);
313 321
            if (_dormant[(*_bucket)[v]]) continue;
314 322
            Value rem = (*_capacity)[a] - (*_flow)[a];
315 323
            if (!_tolerance.positive(rem)) continue;
316 324
            if ((*_bucket)[v] == under_bucket) {
317 325
              if (!(*_active)[v] && v != target) {
318 326
                activate(v);
319 327
              }
320 328
              if (!_tolerance.less(rem, excess)) {
321
                _flow->set(a, (*_flow)[a] + excess);
322
                _excess->set(v, (*_excess)[v] + excess);
329
                (*_flow)[a] += excess;
330
                (*_excess)[v] += excess;
323 331
                excess = 0;
324 332
                goto no_more_push;
325 333
              } else {
326 334
                excess -= rem;
327
                _excess->set(v, (*_excess)[v] + rem);
328
                _flow->set(a, (*_capacity)[a]);
335
                (*_excess)[v] += rem;
336
                (*_flow)[a] = (*_capacity)[a];
329 337
              }
330 338
            } else if (next_bucket > (*_bucket)[v]) {
331 339
              next_bucket = (*_bucket)[v];
332 340
            }
333 341
          }
334 342

	
335 343
          for (InArcIt a(_graph, n); a != INVALID; ++a) {
336 344
            Node v = _graph.source(a);
337 345
            if (_dormant[(*_bucket)[v]]) continue;
338 346
            Value rem = (*_flow)[a];
339 347
            if (!_tolerance.positive(rem)) continue;
340 348
            if ((*_bucket)[v] == under_bucket) {
341 349
              if (!(*_active)[v] && v != target) {
342 350
                activate(v);
343 351
              }
344 352
              if (!_tolerance.less(rem, excess)) {
345
                _flow->set(a, (*_flow)[a] - excess);
346
                _excess->set(v, (*_excess)[v] + excess);
353
                (*_flow)[a] -= excess;
354
                (*_excess)[v] += excess;
347 355
                excess = 0;
348 356
                goto no_more_push;
349 357
              } else {
350 358
                excess -= rem;
351
                _excess->set(v, (*_excess)[v] + rem);
352
                _flow->set(a, 0);
359
                (*_excess)[v] += rem;
360
                (*_flow)[a] = 0;
353 361
              }
354 362
            } else if (next_bucket > (*_bucket)[v]) {
355 363
              next_bucket = (*_bucket)[v];
356 364
            }
357 365
          }
358 366

	
359 367
        no_more_push:
360 368

	
361
          _excess->set(n, excess);
369
          (*_excess)[n] = excess;
362 370

	
363 371
          if (excess != 0) {
364 372
            if ((*_next)[n] == INVALID) {
365 373
              typename std::list<std::list<int> >::iterator new_set =
366 374
                _sets.insert(--_sets.end(), std::list<int>());
367 375
              new_set->splice(new_set->end(), _sets.back(),
368 376
                              _sets.back().begin(), ++_highest);
369 377
              for (std::list<int>::iterator it = new_set->begin();
370 378
                   it != new_set->end(); ++it) {
371 379
                _dormant[*it] = true;
372 380
              }
373 381
              while (_highest != _sets.back().end() &&
374 382
                     !(*_active)[_first[*_highest]]) {
375 383
                ++_highest;
376 384
              }
377 385
            } else if (next_bucket == _node_num) {
378 386
              _first[(*_bucket)[n]] = (*_next)[n];
379
              _prev->set((*_next)[n], INVALID);
387
              (*_prev)[(*_next)[n]] = INVALID;
380 388

	
381 389
              std::list<std::list<int> >::iterator new_set =
382 390
                _sets.insert(--_sets.end(), std::list<int>());
383 391

	
384 392
              new_set->push_front(bucket_num);
385
              _bucket->set(n, bucket_num);
393
              (*_bucket)[n] = bucket_num;
386 394
              _first[bucket_num] = _last[bucket_num] = n;
387
              _next->set(n, INVALID);
388
              _prev->set(n, INVALID);
395
              (*_next)[n] = INVALID;
396
              (*_prev)[n] = INVALID;
389 397
              _dormant[bucket_num] = true;
390 398
              ++bucket_num;
391 399

	
392 400
              while (_highest != _sets.back().end() &&
393 401
                     !(*_active)[_first[*_highest]]) {
394 402
                ++_highest;
395 403
              }
396 404
            } else {
397 405
              _first[*_highest] = (*_next)[n];
398
              _prev->set((*_next)[n], INVALID);
406
              (*_prev)[(*_next)[n]] = INVALID;
399 407

	
400 408
              while (next_bucket != *_highest) {
401 409
                --_highest;
402 410
              }
403 411

	
404 412
              if (_highest == _sets.back().begin()) {
405 413
                _sets.back().push_front(bucket_num);
406 414
                _dormant[bucket_num] = false;
407 415
                _first[bucket_num] = _last[bucket_num] = INVALID;
408 416
                ++bucket_num;
409 417
              }
410 418
              --_highest;
411 419

	
412
              _bucket->set(n, *_highest);
413
              _next->set(n, _first[*_highest]);
420
              (*_bucket)[n] = *_highest;
421
              (*_next)[n] = _first[*_highest];
414 422
              if (_first[*_highest] != INVALID) {
415
                _prev->set(_first[*_highest], n);
423
                (*_prev)[_first[*_highest]] = n;
416 424
              } else {
417 425
                _last[*_highest] = n;
418 426
              }
419 427
              _first[*_highest] = n;
420 428
            }
421 429
          } else {
422 430

	
423 431
            deactivate(n);
424 432
            if (!(*_active)[_first[*_highest]]) {
425 433
              ++_highest;
426 434
              if (_highest != _sets.back().end() &&
427 435
                  !(*_active)[_first[*_highest]]) {
428 436
                _highest = _sets.back().end();
429 437
              }
430 438
            }
431 439
          }
432 440
        }
433 441

	
434 442
        if ((*_excess)[target] < _min_cut) {
435 443
          _min_cut = (*_excess)[target];
436 444
          for (NodeIt i(_graph); i != INVALID; ++i) {
437
            _min_cut_map->set(i, true);
445
            (*_min_cut_map)[i] = true;
438 446
          }
439 447
          for (std::list<int>::iterator it = _sets.back().begin();
440 448
               it != _sets.back().end(); ++it) {
441 449
            Node n = _first[*it];
442 450
            while (n != INVALID) {
443
              _min_cut_map->set(n, false);
451
              (*_min_cut_map)[n] = false;
444 452
              n = (*_next)[n];
445 453
            }
446 454
          }
447 455
        }
448 456

	
449 457
        {
450 458
          Node new_target;
451 459
          if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) {
452 460
            if ((*_next)[target] == INVALID) {
453 461
              _last[(*_bucket)[target]] = (*_prev)[target];
454 462
              new_target = (*_prev)[target];
455 463
            } else {
456
              _prev->set((*_next)[target], (*_prev)[target]);
464
              (*_prev)[(*_next)[target]] = (*_prev)[target];
457 465
              new_target = (*_next)[target];
458 466
            }
459 467
            if ((*_prev)[target] == INVALID) {
460 468
              _first[(*_bucket)[target]] = (*_next)[target];
461 469
            } else {
462
              _next->set((*_prev)[target], (*_next)[target]);
470
              (*_next)[(*_prev)[target]] = (*_next)[target];
463 471
            }
464 472
          } else {
465 473
            _sets.back().pop_back();
466 474
            if (_sets.back().empty()) {
467 475
              _sets.pop_back();
468 476
              if (_sets.empty())
469 477
                break;
470 478
              for (std::list<int>::iterator it = _sets.back().begin();
471 479
                   it != _sets.back().end(); ++it) {
472 480
                _dormant[*it] = false;
473 481
              }
474 482
            }
475 483
            new_target = _last[_sets.back().back()];
476 484
          }
477 485

	
478
          _bucket->set(target, 0);
486
          (*_bucket)[target] = 0;
479 487

	
480
          _source_set->set(target, true);
488
          (*_source_set)[target] = true;
481 489
          for (OutArcIt a(_graph, target); a != INVALID; ++a) {
482 490
            Value rem = (*_capacity)[a] - (*_flow)[a];
483 491
            if (!_tolerance.positive(rem)) continue;
484 492
            Node v = _graph.target(a);
485 493
            if (!(*_active)[v] && !(*_source_set)[v]) {
486 494
              activate(v);
487 495
            }
488
            _excess->set(v, (*_excess)[v] + rem);
489
            _flow->set(a, (*_capacity)[a]);
496
            (*_excess)[v] += rem;
497
            (*_flow)[a] = (*_capacity)[a];
490 498
          }
491 499

	
492 500
          for (InArcIt a(_graph, target); a != INVALID; ++a) {
493 501
            Value rem = (*_flow)[a];
494 502
            if (!_tolerance.positive(rem)) continue;
495 503
            Node v = _graph.source(a);
496 504
            if (!(*_active)[v] && !(*_source_set)[v]) {
497 505
              activate(v);
498 506
            }
499
            _excess->set(v, (*_excess)[v] + rem);
500
            _flow->set(a, 0);
507
            (*_excess)[v] += rem;
508
            (*_flow)[a] = 0;
501 509
          }
502 510

	
503 511
          target = new_target;
504 512
          if ((*_active)[target]) {
505 513
            deactivate(target);
506 514
          }
507 515

	
508 516
          _highest = _sets.back().begin();
509 517
          while (_highest != _sets.back().end() &&
510 518
                 !(*_active)[_first[*_highest]]) {
511 519
            ++_highest;
512 520
          }
513 521
        }
514 522
      }
515 523
    }
516 524

	
517 525
    void findMinCutIn() {
518 526

	
519 527
      for (NodeIt n(_graph); n != INVALID; ++n) {
520
        _excess->set(n, 0);
528
        (*_excess)[n] = 0;
529
        (*_source_set)[n] = false;
521 530
      }
522 531

	
523 532
      for (ArcIt a(_graph); a != INVALID; ++a) {
524
        _flow->set(a, 0);
533
        (*_flow)[a] = 0;
525 534
      }
526 535

	
527 536
      int bucket_num = 0;
528 537
      std::vector<Node> queue(_node_num);
529 538
      int qfirst = 0, qlast = 0, qsep = 0;
530 539

	
531 540
      {
532 541
        typename Digraph::template NodeMap<bool> reached(_graph, false);
533 542

	
534
        reached.set(_source, true);
543
        reached[_source] = true;
535 544

	
536 545
        bool first_set = true;
537 546

	
538 547
        for (NodeIt t(_graph); t != INVALID; ++t) {
539 548
          if (reached[t]) continue;
540 549
          _sets.push_front(std::list<int>());
541 550

	
542 551
          queue[qlast++] = t;
543
          reached.set(t, true);
552
          reached[t] = true;
544 553

	
545 554
          while (qfirst != qlast) {
546 555
            if (qsep == qfirst) {
547 556
              ++bucket_num;
548 557
              _sets.front().push_front(bucket_num);
549 558
              _dormant[bucket_num] = !first_set;
550 559
              _first[bucket_num] = _last[bucket_num] = INVALID;
551 560
              qsep = qlast;
552 561
            }
553 562

	
554 563
            Node n = queue[qfirst++];
555 564
            addItem(n, bucket_num);
556 565

	
557 566
            for (OutArcIt a(_graph, n); a != INVALID; ++a) {
558 567
              Node u = _graph.target(a);
559 568
              if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
560
                reached.set(u, true);
569
                reached[u] = true;
561 570
                queue[qlast++] = u;
562 571
              }
563 572
            }
564 573
          }
565 574
          first_set = false;
566 575
        }
567 576

	
568 577
        ++bucket_num;
569
        _bucket->set(_source, 0);
578
        (*_bucket)[_source] = 0;
570 579
        _dormant[0] = true;
571 580
      }
572
      _source_set->set(_source, true);
581
      (*_source_set)[_source] = true;
573 582

	
574 583
      Node target = _last[_sets.back().back()];
575 584
      {
576 585
        for (InArcIt a(_graph, _source); a != INVALID; ++a) {
577 586
          if (_tolerance.positive((*_capacity)[a])) {
578 587
            Node u = _graph.source(a);
579
            _flow->set(a, (*_capacity)[a]);
580
            _excess->set(u, (*_excess)[u] + (*_capacity)[a]);
588
            (*_flow)[a] = (*_capacity)[a];
589
            (*_excess)[u] += (*_capacity)[a];
581 590
            if (!(*_active)[u] && u != _source) {
582 591
              activate(u);
583 592
            }
584 593
          }
585 594
        }
586 595
        if ((*_active)[target]) {
587 596
          deactivate(target);
588 597
        }
589 598

	
590 599
        _highest = _sets.back().begin();
591 600
        while (_highest != _sets.back().end() &&
592 601
               !(*_active)[_first[*_highest]]) {
593 602
          ++_highest;
594 603
        }
595 604
      }
596 605

	
597 606

	
598 607
      while (true) {
599 608
        while (_highest != _sets.back().end()) {
600 609
          Node n = _first[*_highest];
601 610
          Value excess = (*_excess)[n];
602 611
          int next_bucket = _node_num;
603 612

	
604 613
          int under_bucket;
605 614
          if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
606 615
            under_bucket = -1;
607 616
          } else {
608 617
            under_bucket = *(++std::list<int>::iterator(_highest));
609 618
          }
610 619

	
611 620
          for (InArcIt a(_graph, n); a != INVALID; ++a) {
612 621
            Node v = _graph.source(a);
613 622
            if (_dormant[(*_bucket)[v]]) continue;
614 623
            Value rem = (*_capacity)[a] - (*_flow)[a];
615 624
            if (!_tolerance.positive(rem)) continue;
616 625
            if ((*_bucket)[v] == under_bucket) {
617 626
              if (!(*_active)[v] && v != target) {
618 627
                activate(v);
619 628
              }
620 629
              if (!_tolerance.less(rem, excess)) {
621
                _flow->set(a, (*_flow)[a] + excess);
622
                _excess->set(v, (*_excess)[v] + excess);
630
                (*_flow)[a] += excess;
631
                (*_excess)[v] += excess;
623 632
                excess = 0;
624 633
                goto no_more_push;
625 634
              } else {
626 635
                excess -= rem;
627
                _excess->set(v, (*_excess)[v] + rem);
628
                _flow->set(a, (*_capacity)[a]);
636
                (*_excess)[v] += rem;
637
                (*_flow)[a] = (*_capacity)[a];
629 638
              }
630 639
            } else if (next_bucket > (*_bucket)[v]) {
631 640
              next_bucket = (*_bucket)[v];
632 641
            }
633 642
          }
634 643

	
635 644
          for (OutArcIt a(_graph, n); a != INVALID; ++a) {
636 645
            Node v = _graph.target(a);
637 646
            if (_dormant[(*_bucket)[v]]) continue;
638 647
            Value rem = (*_flow)[a];
639 648
            if (!_tolerance.positive(rem)) continue;
640 649
            if ((*_bucket)[v] == under_bucket) {
641 650
              if (!(*_active)[v] && v != target) {
642 651
                activate(v);
643 652
              }
644 653
              if (!_tolerance.less(rem, excess)) {
645
                _flow->set(a, (*_flow)[a] - excess);
646
                _excess->set(v, (*_excess)[v] + excess);
654
                (*_flow)[a] -= excess;
655
                (*_excess)[v] += excess;
647 656
                excess = 0;
648 657
                goto no_more_push;
649 658
              } else {
650 659
                excess -= rem;
651
                _excess->set(v, (*_excess)[v] + rem);
652
                _flow->set(a, 0);
660
                (*_excess)[v] += rem;
661
                (*_flow)[a] = 0;
653 662
              }
654 663
            } else if (next_bucket > (*_bucket)[v]) {
655 664
              next_bucket = (*_bucket)[v];
656 665
            }
657 666
          }
658 667

	
659 668
        no_more_push:
660 669

	
661
          _excess->set(n, excess);
670
          (*_excess)[n] = excess;
662 671

	
663 672
          if (excess != 0) {
664 673
            if ((*_next)[n] == INVALID) {
665 674
              typename std::list<std::list<int> >::iterator new_set =
666 675
                _sets.insert(--_sets.end(), std::list<int>());
667 676
              new_set->splice(new_set->end(), _sets.back(),
668 677
                              _sets.back().begin(), ++_highest);
669 678
              for (std::list<int>::iterator it = new_set->begin();
670 679
                   it != new_set->end(); ++it) {
671 680
                _dormant[*it] = true;
672 681
              }
673 682
              while (_highest != _sets.back().end() &&
674 683
                     !(*_active)[_first[*_highest]]) {
675 684
                ++_highest;
676 685
              }
677 686
            } else if (next_bucket == _node_num) {
678 687
              _first[(*_bucket)[n]] = (*_next)[n];
679
              _prev->set((*_next)[n], INVALID);
688
              (*_prev)[(*_next)[n]] = INVALID;
680 689

	
681 690
              std::list<std::list<int> >::iterator new_set =
682 691
                _sets.insert(--_sets.end(), std::list<int>());
683 692

	
684 693
              new_set->push_front(bucket_num);
685
              _bucket->set(n, bucket_num);
694
              (*_bucket)[n] = bucket_num;
686 695
              _first[bucket_num] = _last[bucket_num] = n;
687
              _next->set(n, INVALID);
688
              _prev->set(n, INVALID);
696
              (*_next)[n] = INVALID;
697
              (*_prev)[n] = INVALID;
689 698
              _dormant[bucket_num] = true;
690 699
              ++bucket_num;
691 700

	
692 701
              while (_highest != _sets.back().end() &&
693 702
                     !(*_active)[_first[*_highest]]) {
694 703
                ++_highest;
695 704
              }
696 705
            } else {
697 706
              _first[*_highest] = (*_next)[n];
698
              _prev->set((*_next)[n], INVALID);
707
              (*_prev)[(*_next)[n]] = INVALID;
699 708

	
700 709
              while (next_bucket != *_highest) {
701 710
                --_highest;
702 711
              }
703 712
              if (_highest == _sets.back().begin()) {
704 713
                _sets.back().push_front(bucket_num);
705 714
                _dormant[bucket_num] = false;
706 715
                _first[bucket_num] = _last[bucket_num] = INVALID;
707 716
                ++bucket_num;
708 717
              }
709 718
              --_highest;
710 719

	
711
              _bucket->set(n, *_highest);
712
              _next->set(n, _first[*_highest]);
720
              (*_bucket)[n] = *_highest;
721
              (*_next)[n] = _first[*_highest];
713 722
              if (_first[*_highest] != INVALID) {
714
                _prev->set(_first[*_highest], n);
723
                (*_prev)[_first[*_highest]] = n;
715 724
              } else {
716 725
                _last[*_highest] = n;
717 726
              }
718 727
              _first[*_highest] = n;
719 728
            }
720 729
          } else {
721 730

	
722 731
            deactivate(n);
723 732
            if (!(*_active)[_first[*_highest]]) {
724 733
              ++_highest;
725 734
              if (_highest != _sets.back().end() &&
726 735
                  !(*_active)[_first[*_highest]]) {
727 736
                _highest = _sets.back().end();
728 737
              }
729 738
            }
730 739
          }
731 740
        }
732 741

	
733 742
        if ((*_excess)[target] < _min_cut) {
734 743
          _min_cut = (*_excess)[target];
735 744
          for (NodeIt i(_graph); i != INVALID; ++i) {
736
            _min_cut_map->set(i, false);
745
            (*_min_cut_map)[i] = false;
737 746
          }
738 747
          for (std::list<int>::iterator it = _sets.back().begin();
739 748
               it != _sets.back().end(); ++it) {
740 749
            Node n = _first[*it];
741 750
            while (n != INVALID) {
742
              _min_cut_map->set(n, true);
751
              (*_min_cut_map)[n] = true;
743 752
              n = (*_next)[n];
744 753
            }
745 754
          }
746 755
        }
747 756

	
748 757
        {
749 758
          Node new_target;
750 759
          if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) {
751 760
            if ((*_next)[target] == INVALID) {
752 761
              _last[(*_bucket)[target]] = (*_prev)[target];
753 762
              new_target = (*_prev)[target];
754 763
            } else {
755
              _prev->set((*_next)[target], (*_prev)[target]);
764
              (*_prev)[(*_next)[target]] = (*_prev)[target];
756 765
              new_target = (*_next)[target];
757 766
            }
758 767
            if ((*_prev)[target] == INVALID) {
759 768
              _first[(*_bucket)[target]] = (*_next)[target];
760 769
            } else {
761
              _next->set((*_prev)[target], (*_next)[target]);
770
              (*_next)[(*_prev)[target]] = (*_next)[target];
762 771
            }
763 772
          } else {
764 773
            _sets.back().pop_back();
765 774
            if (_sets.back().empty()) {
766 775
              _sets.pop_back();
767 776
              if (_sets.empty())
768 777
                break;
769 778
              for (std::list<int>::iterator it = _sets.back().begin();
770 779
                   it != _sets.back().end(); ++it) {
771 780
                _dormant[*it] = false;
772 781
              }
773 782
            }
774 783
            new_target = _last[_sets.back().back()];
775 784
          }
776 785

	
777
          _bucket->set(target, 0);
786
          (*_bucket)[target] = 0;
778 787

	
779
          _source_set->set(target, true);
788
          (*_source_set)[target] = true;
780 789
          for (InArcIt a(_graph, target); a != INVALID; ++a) {
781 790
            Value rem = (*_capacity)[a] - (*_flow)[a];
782 791
            if (!_tolerance.positive(rem)) continue;
783 792
            Node v = _graph.source(a);
784 793
            if (!(*_active)[v] && !(*_source_set)[v]) {
785 794
              activate(v);
786 795
            }
787
            _excess->set(v, (*_excess)[v] + rem);
788
            _flow->set(a, (*_capacity)[a]);
796
            (*_excess)[v] += rem;
797
            (*_flow)[a] = (*_capacity)[a];
789 798
          }
790 799

	
791 800
          for (OutArcIt a(_graph, target); a != INVALID; ++a) {
792 801
            Value rem = (*_flow)[a];
793 802
            if (!_tolerance.positive(rem)) continue;
794 803
            Node v = _graph.target(a);
795 804
            if (!(*_active)[v] && !(*_source_set)[v]) {
796 805
              activate(v);
797 806
            }
798
            _excess->set(v, (*_excess)[v] + rem);
799
            _flow->set(a, 0);
807
            (*_excess)[v] += rem;
808
            (*_flow)[a] = 0;
800 809
          }
801 810

	
802 811
          target = new_target;
803 812
          if ((*_active)[target]) {
804 813
            deactivate(target);
805 814
          }
806 815

	
807 816
          _highest = _sets.back().begin();
808 817
          while (_highest != _sets.back().end() &&
809 818
                 !(*_active)[_first[*_highest]]) {
810 819
            ++_highest;
811 820
          }
812 821
        }
813 822
      }
814 823
    }
815 824

	
816 825
  public:
817 826

	
818
    /// \name Execution control
827
    /// \name Execution Control
819 828
    /// The simplest way to execute the algorithm is to use
820
    /// one of the member functions called \c run(...).
829
    /// one of the member functions called \ref run().
821 830
    /// \n
822
    /// If you need more control on the execution,
823
    /// first you must call \ref init(), then the \ref calculateIn() or
824
    /// \ref calculateOut() functions.
831
    /// If you need better control on the execution,
832
    /// you have to call one of the \ref init() functions first, then
833
    /// \ref calculateOut() and/or \ref calculateIn().
825 834

	
826 835
    /// @{
827 836

	
828
    /// \brief Initializes the internal data structures.
837
    /// \brief Initialize the internal data structures.
829 838
    ///
830
    /// Initializes the internal data structures. It creates
831
    /// the maps, residual graph adaptors and some bucket structures
832
    /// for the algorithm.
839
    /// This function initializes the internal data structures. It creates
840
    /// the maps and some bucket structures for the algorithm.
841
    /// The first node is used as the source node for the push-relabel
842
    /// algorithm.
833 843
    void init() {
834 844
      init(NodeIt(_graph));
835 845
    }
836 846

	
837
    /// \brief Initializes the internal data structures.
847
    /// \brief Initialize the internal data structures.
838 848
    ///
839
    /// Initializes the internal data structures. It creates
840
    /// the maps, residual graph adaptor and some bucket structures
841
    /// for the algorithm. Node \c source  is used as the push-relabel
842
    /// algorithm's source.
849
    /// This function initializes the internal data structures. It creates
850
    /// the maps and some bucket structures for the algorithm. 
851
    /// The given node is used as the source node for the push-relabel
852
    /// algorithm.
843 853
    void init(const Node& source) {
844 854
      _source = source;
845 855

	
846 856
      _node_num = countNodes(_graph);
847 857

	
848 858
      _first.resize(_node_num);
849 859
      _last.resize(_node_num);
850 860

	
851 861
      _dormant.resize(_node_num);
852 862

	
853 863
      if (!_flow) {
854 864
        _flow = new FlowMap(_graph);
855 865
      }
856 866
      if (!_next) {
857 867
        _next = new typename Digraph::template NodeMap<Node>(_graph);
858 868
      }
859 869
      if (!_prev) {
860 870
        _prev = new typename Digraph::template NodeMap<Node>(_graph);
861 871
      }
862 872
      if (!_active) {
863 873
        _active = new typename Digraph::template NodeMap<bool>(_graph);
864 874
      }
865 875
      if (!_bucket) {
866 876
        _bucket = new typename Digraph::template NodeMap<int>(_graph);
867 877
      }
868 878
      if (!_excess) {
869 879
        _excess = new ExcessMap(_graph);
870 880
      }
871 881
      if (!_source_set) {
872 882
        _source_set = new SourceSetMap(_graph);
873 883
      }
874 884
      if (!_min_cut_map) {
875 885
        _min_cut_map = new MinCutMap(_graph);
876 886
      }
877 887

	
878 888
      _min_cut = std::numeric_limits<Value>::max();
879 889
    }
880 890

	
881 891

	
882
    /// \brief Calculates a minimum cut with \f$ source \f$ on the
892
    /// \brief Calculate a minimum cut with \f$ source \f$ on the
883 893
    /// source-side.
884 894
    ///
885
    /// Calculates a minimum cut with \f$ source \f$ on the
895
    /// This function calculates a minimum cut with \f$ source \f$ on the
886 896
    /// source-side (i.e. a set \f$ X\subsetneq V \f$ with
887
    /// \f$ source \in X \f$ and minimal out-degree).
897
    /// \f$ source \in X \f$ and minimal outgoing capacity).
898
    ///
899
    /// \pre \ref init() must be called before using this function.
888 900
    void calculateOut() {
889 901
      findMinCutOut();
890 902
    }
891 903

	
892
    /// \brief Calculates a minimum cut with \f$ source \f$ on the
893
    /// target-side.
904
    /// \brief Calculate a minimum cut with \f$ source \f$ on the
905
    /// sink-side.
894 906
    ///
895
    /// Calculates a minimum cut with \f$ source \f$ on the
896
    /// target-side (i.e. a set \f$ X\subsetneq V \f$ with
897
    /// \f$ source \in X \f$ and minimal out-degree).
907
    /// This function calculates a minimum cut with \f$ source \f$ on the
908
    /// sink-side (i.e. a set \f$ X\subsetneq V \f$ with
909
    /// \f$ source \notin X \f$ and minimal outgoing capacity).
910
    ///
911
    /// \pre \ref init() must be called before using this function.
898 912
    void calculateIn() {
899 913
      findMinCutIn();
900 914
    }
901 915

	
902 916

	
903
    /// \brief Runs the algorithm.
917
    /// \brief Run the algorithm.
904 918
    ///
905
    /// Runs the algorithm. It finds nodes \c source and \c target
906
    /// arbitrarily and then calls \ref init(), \ref calculateOut()
919
    /// This function runs the algorithm. It finds nodes \c source and
920
    /// \c target arbitrarily and then calls \ref init(), \ref calculateOut()
907 921
    /// and \ref calculateIn().
908 922
    void run() {
909 923
      init();
910 924
      calculateOut();
911 925
      calculateIn();
912 926
    }
913 927

	
914
    /// \brief Runs the algorithm.
928
    /// \brief Run the algorithm.
915 929
    ///
916
    /// Runs the algorithm. It uses the given \c source node, finds a
917
    /// proper \c target and then calls the \ref init(), \ref
918
    /// calculateOut() and \ref calculateIn().
930
    /// This function runs the algorithm. It uses the given \c source node, 
931
    /// finds a proper \c target node and then calls the \ref init(),
932
    /// \ref calculateOut() and \ref calculateIn().
919 933
    void run(const Node& s) {
920 934
      init(s);
921 935
      calculateOut();
922 936
      calculateIn();
923 937
    }
924 938

	
925 939
    /// @}
926 940

	
927 941
    /// \name Query Functions
928 942
    /// The result of the %HaoOrlin algorithm
929
    /// can be obtained using these functions.
930
    /// \n
931
    /// Before using these functions, either \ref run(), \ref
932
    /// calculateOut() or \ref calculateIn() must be called.
943
    /// can be obtained using these functions.\n
944
    /// \ref run(), \ref calculateOut() or \ref calculateIn() 
945
    /// should be called before using them.
933 946

	
934 947
    /// @{
935 948

	
936
    /// \brief Returns the value of the minimum value cut.
949
    /// \brief Return the value of the minimum cut.
937 950
    ///
938
    /// Returns the value of the minimum value cut.
951
    /// This function returns the value of the minimum cut.
952
    ///
953
    /// \pre \ref run(), \ref calculateOut() or \ref calculateIn() 
954
    /// must be called before using this function.
939 955
    Value minCutValue() const {
940 956
      return _min_cut;
941 957
    }
942 958

	
943 959

	
944
    /// \brief Returns a minimum cut.
960
    /// \brief Return a minimum cut.
945 961
    ///
946
    /// Sets \c nodeMap to the characteristic vector of a minimum
947
    /// value cut: it will give a nonempty set \f$ X\subsetneq V \f$
948
    /// with minimal out-degree (i.e. \c nodeMap will be true exactly
949
    /// for the nodes of \f$ X \f$).  \pre nodeMap should be a
950
    /// bool-valued node-map.
951
    template <typename NodeMap>
952
    Value minCutMap(NodeMap& nodeMap) const {
962
    /// This function sets \c cutMap to the characteristic vector of a
963
    /// minimum value cut: it will give a non-empty set \f$ X\subsetneq V \f$
964
    /// with minimal outgoing capacity (i.e. \c cutMap will be \c true exactly
965
    /// for the nodes of \f$ X \f$).
966
    ///
967
    /// \param cutMap A \ref concepts::WriteMap "writable" node map with
968
    /// \c bool (or convertible) value type.
969
    ///
970
    /// \return The value of the minimum cut.
971
    ///
972
    /// \pre \ref run(), \ref calculateOut() or \ref calculateIn() 
973
    /// must be called before using this function.
974
    template <typename CutMap>
975
    Value minCutMap(CutMap& cutMap) const {
953 976
      for (NodeIt it(_graph); it != INVALID; ++it) {
954
        nodeMap.set(it, (*_min_cut_map)[it]);
977
        cutMap.set(it, (*_min_cut_map)[it]);
955 978
      }
956 979
      return _min_cut;
957 980
    }
958 981

	
959 982
    /// @}
960 983

	
961 984
  }; //class HaoOrlin
962 985

	
963

	
964 986
} //namespace lemon
965 987

	
966 988
#endif //LEMON_HAO_ORLIN_H
Ignore white space 6 line context
... ...
@@ -169,270 +169,287 @@
169 169
    }
170 170

	
171 171
    static void next(Node& node) {
172 172
      --node._id;
173 173
    }
174 174

	
175 175
    void first(Edge& edge) const {
176 176
      edge._id = _edge_num - 1;
177 177
    }
178 178

	
179 179
    static void next(Edge& edge) {
180 180
      --edge._id;
181 181
    }
182 182

	
183 183
    void first(Arc& arc) const {
184 184
      arc._id = 2 * _edge_num - 1;
185 185
    }
186 186

	
187 187
    static void next(Arc& arc) {
188 188
      --arc._id;
189 189
    }
190 190

	
191 191
    void firstInc(Edge& edge, bool& dir, const Node& node) const {
192 192
      edge._id = node._id >> 1;
193 193
      dir = (node._id & 1) == 0;
194 194
    }
195 195

	
196 196
    void nextInc(Edge& edge, bool& dir) const {
197 197
      Node n = dir ? u(edge) : v(edge);
198 198
      int k = (edge._id >> (_dim-1)) + 1;
199 199
      if (k < _dim) {
200 200
        edge._id = (k << (_dim-1)) |
201 201
          ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
202 202
        dir = ((n._id >> k) & 1) == 0;
203 203
      } else {
204 204
        edge._id = -1;
205 205
        dir = true;
206 206
      }
207 207
    }
208 208

	
209 209
    void firstOut(Arc& arc, const Node& node) const {
210 210
      arc._id = ((node._id >> 1) << 1) | (~node._id & 1);
211 211
    }
212 212

	
213 213
    void nextOut(Arc& arc) const {
214 214
      Node n = (arc._id & 1) == 1 ? u(arc) : v(arc);
215 215
      int k = (arc._id >> _dim) + 1;
216 216
      if (k < _dim) {
217 217
        arc._id = (k << (_dim-1)) |
218 218
          ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
219 219
        arc._id = (arc._id << 1) | (~(n._id >> k) & 1);
220 220
      } else {
221 221
        arc._id = -1;
222 222
      }
223 223
    }
224 224

	
225 225
    void firstIn(Arc& arc, const Node& node) const {
226 226
      arc._id = ((node._id >> 1) << 1) | (node._id & 1);
227 227
    }
228 228

	
229 229
    void nextIn(Arc& arc) const {
230 230
      Node n = (arc._id & 1) == 1 ? v(arc) : u(arc);
231 231
      int k = (arc._id >> _dim) + 1;
232 232
      if (k < _dim) {
233 233
        arc._id = (k << (_dim-1)) |
234 234
          ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
235 235
        arc._id = (arc._id << 1) | ((n._id >> k) & 1);
236 236
      } else {
237 237
        arc._id = -1;
238 238
      }
239 239
    }
240 240

	
241 241
    static bool direction(Arc arc) {
242 242
      return (arc._id & 1) == 1;
243 243
    }
244 244

	
245 245
    static Arc direct(Edge edge, bool dir) {
246 246
      return Arc((edge._id << 1) | (dir ? 1 : 0));
247 247
    }
248 248

	
249 249
    int dimension() const {
250 250
      return _dim;
251 251
    }
252 252

	
253 253
    bool projection(Node node, int n) const {
254 254
      return static_cast<bool>(node._id & (1 << n));
255 255
    }
256 256

	
257 257
    int dimension(Edge edge) const {
258 258
      return edge._id >> (_dim-1);
259 259
    }
260 260

	
261 261
    int dimension(Arc arc) const {
262 262
      return arc._id >> _dim;
263 263
    }
264 264

	
265
    int index(Node node) const {
265
    static int index(Node node) {
266 266
      return node._id;
267 267
    }
268 268

	
269 269
    Node operator()(int ix) const {
270 270
      return Node(ix);
271 271
    }
272 272

	
273 273
  private:
274 274
    int _dim;
275 275
    int _node_num, _edge_num;
276 276
  };
277 277

	
278 278

	
279 279
  typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase;
280 280

	
281 281
  /// \ingroup graphs
282 282
  ///
283 283
  /// \brief Hypercube graph class
284 284
  ///
285
  /// This class implements a special graph type. The nodes of the graph
286
  /// are indiced with integers with at most \c dim binary digits.
285
  /// HypercubeGraph implements a special graph type. The nodes of the
286
  /// graph are indexed with integers having at most \c dim binary digits.
287 287
  /// Two nodes are connected in the graph if and only if their indices
288 288
  /// differ only on one position in the binary form.
289
  /// This class is completely static and it needs constant memory space.
290
  /// Thus you can neither add nor delete nodes or edges, however 
291
  /// the structure can be resized using resize().
292
  ///
293
  /// This type fully conforms to the \ref concepts::Graph "Graph concept".
294
  /// Most of its member functions and nested classes are documented
295
  /// only in the concept class.
289 296
  ///
290 297
  /// \note The type of the indices is chosen to \c int for efficiency
291 298
  /// reasons. Thus the maximum dimension of this implementation is 26
292 299
  /// (assuming that the size of \c int is 32 bit).
293
  ///
294
  /// This graph type is fully conform to the \ref concepts::Graph
295
  /// "Graph" concept, and it also has an important extra feature
296
  /// that its maps are real \ref concepts::ReferenceMap
297
  /// "reference map"s.
298 300
  class HypercubeGraph : public ExtendedHypercubeGraphBase {
301
    typedef ExtendedHypercubeGraphBase Parent;
302

	
299 303
  public:
300 304

	
301
    typedef ExtendedHypercubeGraphBase Parent;
302

	
303 305
    /// \brief Constructs a hypercube graph with \c dim dimensions.
304 306
    ///
305 307
    /// Constructs a hypercube graph with \c dim dimensions.
306 308
    HypercubeGraph(int dim) { construct(dim); }
307 309

	
310
    /// \brief Resizes the graph
311
    ///
312
    /// This function resizes the graph. It fully destroys and
313
    /// rebuilds the structure, therefore the maps of the graph will be
314
    /// reallocated automatically and the previous values will be lost.
315
    void resize(int dim) {
316
      Parent::notifier(Arc()).clear();
317
      Parent::notifier(Edge()).clear();
318
      Parent::notifier(Node()).clear();
319
      construct(dim);
320
      Parent::notifier(Node()).build();
321
      Parent::notifier(Edge()).build();
322
      Parent::notifier(Arc()).build();
323
    }
324

	
308 325
    /// \brief The number of dimensions.
309 326
    ///
310 327
    /// Gives back the number of dimensions.
311 328
    int dimension() const {
312 329
      return Parent::dimension();
313 330
    }
314 331

	
315 332
    /// \brief Returns \c true if the n'th bit of the node is one.
316 333
    ///
317 334
    /// Returns \c true if the n'th bit of the node is one.
318 335
    bool projection(Node node, int n) const {
319 336
      return Parent::projection(node, n);
320 337
    }
321 338

	
322 339
    /// \brief The dimension id of an edge.
323 340
    ///
324 341
    /// Gives back the dimension id of the given edge.
325
    /// It is in the [0..dim-1] range.
342
    /// It is in the range <tt>[0..dim-1]</tt>.
326 343
    int dimension(Edge edge) const {
327 344
      return Parent::dimension(edge);
328 345
    }
329 346

	
330 347
    /// \brief The dimension id of an arc.
331 348
    ///
332 349
    /// Gives back the dimension id of the given arc.
333
    /// It is in the [0..dim-1] range.
350
    /// It is in the range <tt>[0..dim-1]</tt>.
334 351
    int dimension(Arc arc) const {
335 352
      return Parent::dimension(arc);
336 353
    }
337 354

	
338 355
    /// \brief The index of a node.
339 356
    ///
340 357
    /// Gives back the index of the given node.
341 358
    /// The lower bits of the integer describes the node.
342
    int index(Node node) const {
359
    static int index(Node node) {
343 360
      return Parent::index(node);
344 361
    }
345 362

	
346 363
    /// \brief Gives back a node by its index.
347 364
    ///
348 365
    /// Gives back a node by its index.
349 366
    Node operator()(int ix) const {
350 367
      return Parent::operator()(ix);
351 368
    }
352 369

	
353 370
    /// \brief Number of nodes.
354 371
    int nodeNum() const { return Parent::nodeNum(); }
355 372
    /// \brief Number of edges.
356 373
    int edgeNum() const { return Parent::edgeNum(); }
357 374
    /// \brief Number of arcs.
358 375
    int arcNum() const { return Parent::arcNum(); }
359 376

	
360 377
    /// \brief Linear combination map.
361 378
    ///
362 379
    /// This map makes possible to give back a linear combination
363 380
    /// for each node. It works like the \c std::accumulate function,
364 381
    /// so it accumulates the \c bf binary function with the \c fv first
365 382
    /// value. The map accumulates only on that positions (dimensions)
366 383
    /// where the index of the node is one. The values that have to be
367 384
    /// accumulated should be given by the \c begin and \c end iterators
368 385
    /// and the length of this range should be equal to the dimension
369 386
    /// number of the graph.
370 387
    ///
371 388
    ///\code
372 389
    /// const int DIM = 3;
373 390
    /// HypercubeGraph graph(DIM);
374 391
    /// dim2::Point<double> base[DIM];
375 392
    /// for (int k = 0; k < DIM; ++k) {
376 393
    ///   base[k].x = rnd();
377 394
    ///   base[k].y = rnd();
378 395
    /// }
379 396
    /// HypercubeGraph::HyperMap<dim2::Point<double> >
380 397
    ///   pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0));
381 398
    ///\endcode
382 399
    ///
383 400
    /// \see HypercubeGraph
384 401
    template <typename T, typename BF = std::plus<T> >
385 402
    class HyperMap {
386 403
    public:
387 404

	
388 405
      /// \brief The key type of the map
389 406
      typedef Node Key;
390 407
      /// \brief The value type of the map
391 408
      typedef T Value;
392 409

	
393 410
      /// \brief Constructor for HyperMap.
394 411
      ///
395 412
      /// Construct a HyperMap for the given graph. The values that have
396 413
      /// to be accumulated should be given by the \c begin and \c end
397 414
      /// iterators and the length of this range should be equal to the
398 415
      /// dimension number of the graph.
399 416
      ///
400 417
      /// This map accumulates the \c bf binary function with the \c fv
401 418
      /// first value on that positions (dimensions) where the index of
402 419
      /// the node is one.
403 420
      template <typename It>
404 421
      HyperMap(const Graph& graph, It begin, It end,
405 422
               T fv = 0, const BF& bf = BF())
406 423
        : _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf)
407 424
      {
408 425
        LEMON_ASSERT(_values.size() == graph.dimension(),
409 426
                     "Wrong size of range");
410 427
      }
411 428

	
412 429
      /// \brief The partial accumulated value.
413 430
      ///
414 431
      /// Gives back the partial accumulated value.
415 432
      Value operator[](const Key& k) const {
416 433
        Value val = _first_value;
417 434
        int id = _graph.index(k);
418 435
        int n = 0;
419 436
        while (id != 0) {
420 437
          if (id & 1) {
421 438
            val = _bin_func(val, _values[n]);
422 439
          }
423 440
          id >>= 1;
424 441
          ++n;
425 442
        }
426 443
        return val;
427 444
      }
428 445

	
429 446
    private:
430 447
      const Graph& _graph;
431 448
      std::vector<T> _values;
432 449
      T _first_value;
433 450
      BF _bin_func;
434 451
    };
435 452

	
436 453
  };
437 454

	
438 455
}
Ignore white space 6 line context
... ...
@@ -155,175 +155,173 @@
155 155
    template <typename In>
156 156
    struct KruskalValueSelector<In,
157 157
      typename enable_if<SequenceInputIndicator<In>, void>::type>
158 158
    {
159 159
      typedef typename In::value_type::second_type Value;
160 160
    };
161 161

	
162 162
    template <typename In>
163 163
    struct KruskalValueSelector<In,
164 164
      typename enable_if<MapInputIndicator<In>, void>::type>
165 165
    {
166 166
      typedef typename In::Value Value;
167 167
    };
168 168

	
169 169
    template <typename Graph, typename In, typename Out,
170 170
              typename InEnable = void>
171 171
    struct KruskalInputSelector {};
172 172

	
173 173
    template <typename Graph, typename In, typename Out,
174 174
              typename InEnable = void>
175 175
    struct KruskalOutputSelector {};
176 176

	
177 177
    template <typename Graph, typename In, typename Out>
178 178
    struct KruskalInputSelector<Graph, In, Out,
179 179
      typename enable_if<SequenceInputIndicator<In>, void>::type >
180 180
    {
181 181
      typedef typename In::value_type::second_type Value;
182 182

	
183 183
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
184 184
        return KruskalOutputSelector<Graph, In, Out>::
185 185
          kruskal(graph, in, out);
186 186
      }
187 187

	
188 188
    };
189 189

	
190 190
    template <typename Graph, typename In, typename Out>
191 191
    struct KruskalInputSelector<Graph, In, Out,
192 192
      typename enable_if<MapInputIndicator<In>, void>::type >
193 193
    {
194 194
      typedef typename In::Value Value;
195 195
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
196 196
        typedef typename In::Key MapArc;
197 197
        typedef typename In::Value Value;
198 198
        typedef typename ItemSetTraits<Graph, MapArc>::ItemIt MapArcIt;
199 199
        typedef std::vector<std::pair<MapArc, Value> > Sequence;
200 200
        Sequence seq;
201 201

	
202 202
        for (MapArcIt it(graph); it != INVALID; ++it) {
203 203
          seq.push_back(std::make_pair(it, in[it]));
204 204
        }
205 205

	
206 206
        std::sort(seq.begin(), seq.end(), PairComp<Sequence>());
207 207
        return KruskalOutputSelector<Graph, Sequence, Out>::
208 208
          kruskal(graph, seq, out);
209 209
      }
210 210
    };
211 211

	
212 212
    template <typename T>
213 213
    struct RemoveConst {
214 214
      typedef T type;
215 215
    };
216 216

	
217 217
    template <typename T>
218 218
    struct RemoveConst<const T> {
219 219
      typedef T type;
220 220
    };
221 221

	
222 222
    template <typename Graph, typename In, typename Out>
223 223
    struct KruskalOutputSelector<Graph, In, Out,
224 224
      typename enable_if<SequenceOutputIndicator<Out>, void>::type >
225 225
    {
226 226
      typedef typename In::value_type::second_type Value;
227 227

	
228 228
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
229 229
        typedef LoggerBoolMap<typename RemoveConst<Out>::type> Map;
230 230
        Map map(out);
231 231
        return _kruskal_bits::kruskal(graph, in, map);
232 232
      }
233 233

	
234 234
    };
235 235

	
236 236
    template <typename Graph, typename In, typename Out>
237 237
    struct KruskalOutputSelector<Graph, In, Out,
238 238
      typename enable_if<MapOutputIndicator<Out>, void>::type >
239 239
    {
240 240
      typedef typename In::value_type::second_type Value;
241 241

	
242 242
      static Value kruskal(const Graph& graph, const In& in, Out& out) {
243 243
        return _kruskal_bits::kruskal(graph, in, out);
244 244
      }
245 245
    };
246 246

	
247 247
  }
248 248

	
249 249
  /// \ingroup spantree
250 250
  ///
251
  /// \brief Kruskal algorithm to find a minimum cost spanning tree of
251
  /// \brief Kruskal's algorithm for finding a minimum cost spanning tree of
252 252
  /// a graph.
253 253
  ///
254 254
  /// This function runs Kruskal's algorithm to find a minimum cost
255
  /// spanning tree.
255
  /// spanning tree of a graph.
256 256
  /// Due to some C++ hacking, it accepts various input and output types.
257 257
  ///
258 258
  /// \param g The graph the algorithm runs on.
259 259
  /// It can be either \ref concepts::Digraph "directed" or
260 260
  /// \ref concepts::Graph "undirected".
261 261
  /// If the graph is directed, the algorithm consider it to be
262 262
  /// undirected by disregarding the direction of the arcs.
263 263
  ///
264 264
  /// \param in This object is used to describe the arc/edge costs.
265 265
  /// It can be one of the following choices.
266 266
  /// - An STL compatible 'Forward Container' with
267
  /// <tt>std::pair<GR::Arc,X></tt> or
268
  /// <tt>std::pair<GR::Edge,X></tt> as its <tt>value_type</tt>, where
269
  /// \c X is the type of the costs. The pairs indicates the arcs/edges
267
  /// <tt>std::pair<GR::Arc,C></tt> or
268
  /// <tt>std::pair<GR::Edge,C></tt> as its <tt>value_type</tt>, where
269
  /// \c C is the type of the costs. The pairs indicates the arcs/edges
270 270
  /// along with the assigned cost. <em>They must be in a
271 271
  /// cost-ascending order.</em>
272 272
  /// - Any readable arc/edge map. The values of the map indicate the
273 273
  /// arc/edge costs.
274 274
  ///
275 275
  /// \retval out Here we also have a choice.
276
  /// - It can be a writable \c bool arc/edge map. After running the
277
  /// algorithm it will contain the found minimum cost spanning
276
  /// - It can be a writable arc/edge map with \c bool value type. After
277
  /// running the algorithm it will contain the found minimum cost spanning
278 278
  /// tree: the value of an arc/edge will be set to \c true if it belongs
279 279
  /// to the tree, otherwise it will be set to \c false. The value of
280 280
  /// each arc/edge will be set exactly once.
281 281
  /// - It can also be an iteraror of an STL Container with
282 282
  /// <tt>GR::Arc</tt> or <tt>GR::Edge</tt> as its
283 283
  /// <tt>value_type</tt>.  The algorithm copies the elements of the
284 284
  /// found tree into this sequence.  For example, if we know that the
285 285
  /// spanning tree of the graph \c g has say 53 arcs, then we can
286 286
  /// put its arcs into an STL vector \c tree with a code like this.
287 287
  ///\code
288 288
  /// std::vector<Arc> tree(53);
289 289
  /// kruskal(g,cost,tree.begin());
290 290
  ///\endcode
291 291
  /// Or if we don't know in advance the size of the tree, we can
292 292
  /// write this.
293 293
  ///\code
294 294
  /// std::vector<Arc> tree;
295 295
  /// kruskal(g,cost,std::back_inserter(tree));
296 296
  ///\endcode
297 297
  ///
298 298
  /// \return The total cost of the found spanning tree.
299 299
  ///
300 300
  /// \note If the input graph is not (weakly) connected, a spanning
301 301
  /// forest is calculated instead of a spanning tree.
302 302

	
303 303
#ifdef DOXYGEN
304
  template <class Graph, class In, class Out>
305
  Value kruskal(GR const& g, const In& in, Out& out)
304
  template <typename Graph, typename In, typename Out>
305
  Value kruskal(const Graph& g, const In& in, Out& out)
306 306
#else
307 307
  template <class Graph, class In, class Out>
308 308
  inline typename _kruskal_bits::KruskalValueSelector<In>::Value
309 309
  kruskal(const Graph& graph, const In& in, Out& out)
310 310
#endif
311 311
  {
312 312
    return _kruskal_bits::KruskalInputSelector<Graph, In, Out>::
313 313
      kruskal(graph, in, out);
314 314
  }
315 315

	
316 316

	
317

	
318

	
319 317
  template <class Graph, class In, class Out>
320 318
  inline typename _kruskal_bits::KruskalValueSelector<In>::Value
321 319
  kruskal(const Graph& graph, const In& in, const Out& out)
322 320
  {
323 321
    return _kruskal_bits::KruskalInputSelector<Graph, In, const Out>::
324 322
      kruskal(graph, in, out);
325 323
  }
326 324

	
327 325
} //namespace lemon
328 326

	
329 327
#endif //LEMON_KRUSKAL_H
Ignore white space 6 line context
1 1
prefix=@prefix@
2 2
exec_prefix=@exec_prefix@
3 3
libdir=@libdir@
4 4
includedir=@includedir@
5 5

	
6 6
Name: @PACKAGE_NAME@
7
Description: Library of Efficient Models and Optimization in Networks
7
Description: Library for Efficient Modeling and Optimization in Networks
8 8
Version: @PACKAGE_VERSION@
9
Libs: -L${libdir} -lemon
9
Libs: -L${libdir} -lemon @GLPK_LIBS@ @CPLEX_LIBS@ @SOPLEX_LIBS@ @CLP_LIBS@ @CBC_LIBS@
10 10
Cflags: -I${includedir}
Ignore white space 6 line context
... ...
@@ -8,279 +8,279 @@
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 lemon_io
20 20
///\file
21 21
///\brief \ref lgf-format "LEMON Graph Format" reader.
22 22

	
23 23

	
24 24
#ifndef LEMON_LGF_READER_H
25 25
#define LEMON_LGF_READER_H
26 26

	
27 27
#include <iostream>
28 28
#include <fstream>
29 29
#include <sstream>
30 30

	
31 31
#include <set>
32 32
#include <map>
33 33

	
34 34
#include <lemon/core.h>
35 35

	
36 36
#include <lemon/lgf_writer.h>
37 37

	
38 38
#include <lemon/concept_check.h>
39 39
#include <lemon/concepts/maps.h>
40 40

	
41 41
namespace lemon {
42 42

	
43 43
  namespace _reader_bits {
44 44

	
45 45
    template <typename Value>
46 46
    struct DefaultConverter {
47 47
      Value operator()(const std::string& str) {
48 48
        std::istringstream is(str);
49 49
        Value value;
50 50
        if (!(is >> value)) {
51 51
          throw FormatError("Cannot read token");
52 52
        }
53 53

	
54 54
        char c;
55 55
        if (is >> std::ws >> c) {
56 56
          throw FormatError("Remaining characters in token");
57 57
        }
58 58
        return value;
59 59
      }
60 60
    };
61 61

	
62 62
    template <>
63 63
    struct DefaultConverter<std::string> {
64 64
      std::string operator()(const std::string& str) {
65 65
        return str;
66 66
      }
67 67
    };
68 68

	
69 69
    template <typename _Item>
70 70
    class MapStorageBase {
71 71
    public:
72 72
      typedef _Item Item;
73 73

	
74 74
    public:
75 75
      MapStorageBase() {}
76 76
      virtual ~MapStorageBase() {}
77 77

	
78 78
      virtual void set(const Item& item, const std::string& value) = 0;
79 79

	
80 80
    };
81 81

	
82 82
    template <typename _Item, typename _Map,
83 83
              typename _Converter = DefaultConverter<typename _Map::Value> >
84 84
    class MapStorage : public MapStorageBase<_Item> {
85 85
    public:
86 86
      typedef _Map Map;
87 87
      typedef _Converter Converter;
88 88
      typedef _Item Item;
89 89

	
90 90
    private:
91 91
      Map& _map;
92 92
      Converter _converter;
93 93

	
94 94
    public:
95 95
      MapStorage(Map& map, const Converter& converter = Converter())
96 96
        : _map(map), _converter(converter) {}
97 97
      virtual ~MapStorage() {}
98 98

	
99 99
      virtual void set(const Item& item ,const std::string& value) {
100 100
        _map.set(item, _converter(value));
101 101
      }
102 102
    };
103 103

	
104
    template <typename _Graph, bool _dir, typename _Map,
104
    template <typename _GR, bool _dir, typename _Map,
105 105
              typename _Converter = DefaultConverter<typename _Map::Value> >
106
    class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
106
    class GraphArcMapStorage : public MapStorageBase<typename _GR::Edge> {
107 107
    public:
108 108
      typedef _Map Map;
109 109
      typedef _Converter Converter;
110
      typedef _Graph Graph;
111
      typedef typename Graph::Edge Item;
110
      typedef _GR GR;
111
      typedef typename GR::Edge Item;
112 112
      static const bool dir = _dir;
113 113

	
114 114
    private:
115
      const Graph& _graph;
115
      const GR& _graph;
116 116
      Map& _map;
117 117
      Converter _converter;
118 118

	
119 119
    public:
120
      GraphArcMapStorage(const Graph& graph, Map& map,
120
      GraphArcMapStorage(const GR& graph, Map& map,
121 121
                         const Converter& converter = Converter())
122 122
        : _graph(graph), _map(map), _converter(converter) {}
123 123
      virtual ~GraphArcMapStorage() {}
124 124

	
125 125
      virtual void set(const Item& item ,const std::string& value) {
126 126
        _map.set(_graph.direct(item, dir), _converter(value));
127 127
      }
128 128
    };
129 129

	
130 130
    class ValueStorageBase {
131 131
    public:
132 132
      ValueStorageBase() {}
133 133
      virtual ~ValueStorageBase() {}
134 134

	
135 135
      virtual void set(const std::string&) = 0;
136 136
    };
137 137

	
138 138
    template <typename _Value, typename _Converter = DefaultConverter<_Value> >
139 139
    class ValueStorage : public ValueStorageBase {
140 140
    public:
141 141
      typedef _Value Value;
142 142
      typedef _Converter Converter;
143 143

	
144 144
    private:
145 145
      Value& _value;
146 146
      Converter _converter;
147 147

	
148 148
    public:
149 149
      ValueStorage(Value& value, const Converter& converter = Converter())
150 150
        : _value(value), _converter(converter) {}
151 151

	
152 152
      virtual void set(const std::string& value) {
153 153
        _value = _converter(value);
154 154
      }
155 155
    };
156 156

	
157 157
    template <typename Value>
158 158
    struct MapLookUpConverter {
159 159
      const std::map<std::string, Value>& _map;
160 160

	
161 161
      MapLookUpConverter(const std::map<std::string, Value>& map)
162 162
        : _map(map) {}
163 163

	
164 164
      Value operator()(const std::string& str) {
165 165
        typename std::map<std::string, Value>::const_iterator it =
166 166
          _map.find(str);
167 167
        if (it == _map.end()) {
168 168
          std::ostringstream msg;
169 169
          msg << "Item not found: " << str;
170 170
          throw FormatError(msg.str());
171 171
        }
172 172
        return it->second;
173 173
      }
174 174
    };
175 175

	
176
    template <typename Graph>
176
    template <typename GR>
177 177
    struct GraphArcLookUpConverter {
178
      const Graph& _graph;
179
      const std::map<std::string, typename Graph::Edge>& _map;
180

	
181
      GraphArcLookUpConverter(const Graph& graph,
178
      const GR& _graph;
179
      const std::map<std::string, typename GR::Edge>& _map;
180

	
181
      GraphArcLookUpConverter(const GR& graph,
182 182
                              const std::map<std::string,
183
                                             typename Graph::Edge>& map)
183
                                             typename GR::Edge>& map)
184 184
        : _graph(graph), _map(map) {}
185 185

	
186
      typename Graph::Arc operator()(const std::string& str) {
186
      typename GR::Arc operator()(const std::string& str) {
187 187
        if (str.empty() || (str[0] != '+' && str[0] != '-')) {
188 188
          throw FormatError("Item must start with '+' or '-'");
189 189
        }
190
        typename std::map<std::string, typename Graph::Edge>
190
        typename std::map<std::string, typename GR::Edge>
191 191
          ::const_iterator it = _map.find(str.substr(1));
192 192
        if (it == _map.end()) {
193 193
          throw FormatError("Item not found");
194 194
        }
195 195
        return _graph.direct(it->second, str[0] == '+');
196 196
      }
197 197
    };
198 198

	
199 199
    inline bool isWhiteSpace(char c) {
200 200
      return c == ' ' || c == '\t' || c == '\v' ||
201 201
        c == '\n' || c == '\r' || c == '\f';
202 202
    }
203 203

	
204 204
    inline bool isOct(char c) {
205 205
      return '0' <= c && c <='7';
206 206
    }
207 207

	
208 208
    inline int valueOct(char c) {
209 209
      LEMON_ASSERT(isOct(c), "The character is not octal.");
210 210
      return c - '0';
211 211
    }
212 212

	
213 213
    inline bool isHex(char c) {
214 214
      return ('0' <= c && c <= '9') ||
215 215
        ('a' <= c && c <= 'z') ||
216 216
        ('A' <= c && c <= 'Z');
217 217
    }
218 218

	
219 219
    inline int valueHex(char c) {
220 220
      LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
221 221
      if ('0' <= c && c <= '9') return c - '0';
222 222
      if ('a' <= c && c <= 'z') return c - 'a' + 10;
223 223
      return c - 'A' + 10;
224 224
    }
225 225

	
226 226
    inline bool isIdentifierFirstChar(char c) {
227 227
      return ('a' <= c && c <= 'z') ||
228 228
        ('A' <= c && c <= 'Z') || c == '_';
229 229
    }
230 230

	
231 231
    inline bool isIdentifierChar(char c) {
232 232
      return isIdentifierFirstChar(c) ||
233 233
        ('0' <= c && c <= '9');
234 234
    }
235 235

	
236 236
    inline char readEscape(std::istream& is) {
237 237
      char c;
238 238
      if (!is.get(c))
239 239
        throw FormatError("Escape format error");
240 240

	
241 241
      switch (c) {
242 242
      case '\\':
243 243
        return '\\';
244 244
      case '\"':
245 245
        return '\"';
246 246
      case '\'':
247 247
        return '\'';
248 248
      case '\?':
249 249
        return '\?';
250 250
      case 'a':
251 251
        return '\a';
252 252
      case 'b':
253 253
        return '\b';
254 254
      case 'f':
255 255
        return '\f';
256 256
      case 'n':
257 257
        return '\n';
258 258
      case 'r':
259 259
        return '\r';
260 260
      case 't':
261 261
        return '\t';
262 262
      case 'v':
263 263
        return '\v';
264 264
      case 'x':
265 265
        {
266 266
          int code;
267 267
          if (!is.get(c) || !isHex(c))
268 268
            throw FormatError("Escape format error");
269 269
          else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
270 270
          else code = code * 16 + valueHex(c);
271 271
          return code;
272 272
        }
273 273
      default:
274 274
        {
275 275
          int code;
276 276
          if (!isOct(c))
277 277
            throw FormatError("Escape format error");
278 278
          else if (code = valueOct(c), !is.get(c) || !isOct(c))
279 279
            is.putback(c);
280 280
          else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
281 281
            is.putback(c);
282 282
          else code = code * 8 + valueOct(c);
283 283
          return code;
284 284
        }
285 285
      }
286 286
    }
... ...
@@ -294,556 +294,532 @@
294 294
      if (!is.get(c))
295 295
        return is;
296 296

	
297 297
      if (c == '\"') {
298 298
        while (is.get(c) && c != '\"') {
299 299
          if (c == '\\')
300 300
            c = readEscape(is);
301 301
          os << c;
302 302
        }
303 303
        if (!is)
304 304
          throw FormatError("Quoted format error");
305 305
      } else {
306 306
        is.putback(c);
307 307
        while (is.get(c) && !isWhiteSpace(c)) {
308 308
          if (c == '\\')
309 309
            c = readEscape(is);
310 310
          os << c;
311 311
        }
312 312
        if (!is) {
313 313
          is.clear();
314 314
        } else {
315 315
          is.putback(c);
316 316
        }
317 317
      }
318 318
      str = os.str();
319 319
      return is;
320 320
    }
321 321

	
322 322
    class Section {
323 323
    public:
324 324
      virtual ~Section() {}
325 325
      virtual void process(std::istream& is, int& line_num) = 0;
326 326
    };
327 327

	
328 328
    template <typename Functor>
329 329
    class LineSection : public Section {
330 330
    private:
331 331

	
332 332
      Functor _functor;
333 333

	
334 334
    public:
335 335

	
336 336
      LineSection(const Functor& functor) : _functor(functor) {}
337 337
      virtual ~LineSection() {}
338 338

	
339 339
      virtual void process(std::istream& is, int& line_num) {
340 340
        char c;
341 341
        std::string line;
342 342
        while (is.get(c) && c != '@') {
343 343
          if (c == '\n') {
344 344
            ++line_num;
345 345
          } else if (c == '#') {
346 346
            getline(is, line);
347 347
            ++line_num;
348 348
          } else if (!isWhiteSpace(c)) {
349 349
            is.putback(c);
350 350
            getline(is, line);
351 351
            _functor(line);
352 352
            ++line_num;
353 353
          }
354 354
        }
355 355
        if (is) is.putback(c);
356 356
        else if (is.eof()) is.clear();
357 357
      }
358 358
    };
359 359

	
360 360
    template <typename Functor>
361 361
    class StreamSection : public Section {
362 362
    private:
363 363

	
364 364
      Functor _functor;
365 365

	
366 366
    public:
367 367

	
368 368
      StreamSection(const Functor& functor) : _functor(functor) {}
369 369
      virtual ~StreamSection() {}
370 370

	
371 371
      virtual void process(std::istream& is, int& line_num) {
372 372
        _functor(is, line_num);
373 373
        char c;
374 374
        std::string line;
375 375
        while (is.get(c) && c != '@') {
376 376
          if (c == '\n') {
377 377
            ++line_num;
378 378
          } else if (!isWhiteSpace(c)) {
379 379
            getline(is, line);
380 380
            ++line_num;
381 381
          }
382 382
        }
383 383
        if (is) is.putback(c);
384 384
        else if (is.eof()) is.clear();
385 385
      }
386 386
    };
387 387

	
388 388
  }
389 389

	
390
  template <typename Digraph>
390
  template <typename DGR>
391 391
  class DigraphReader;
392 392

	
393
  /// \brief Return a \ref DigraphReader class
394
  ///
395
  /// This function just returns a \ref DigraphReader class.
396
  /// \relates DigraphReader
397
  template <typename Digraph>
398
  DigraphReader<Digraph> digraphReader(Digraph& digraph,
399
                                       std::istream& is = std::cin) {
400
    DigraphReader<Digraph> tmp(digraph, is);
401
    return tmp;
402
  }
403

	
404
  /// \brief Return a \ref DigraphReader class
405
  ///
406
  /// This function just returns a \ref DigraphReader class.
407
  /// \relates DigraphReader
408
  template <typename Digraph>
409
  DigraphReader<Digraph> digraphReader(Digraph& digraph,
410
                                       const std::string& fn) {
411
    DigraphReader<Digraph> tmp(digraph, fn);
412
    return tmp;
413
  }
414

	
415
  /// \brief Return a \ref DigraphReader class
416
  ///
417
  /// This function just returns a \ref DigraphReader class.
418
  /// \relates DigraphReader
419
  template <typename Digraph>
420
  DigraphReader<Digraph> digraphReader(Digraph& digraph, const char* fn) {
421
    DigraphReader<Digraph> tmp(digraph, fn);
422
    return tmp;
423
  }
393
  template <typename TDGR>
394
  DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is = std::cin);
395
  template <typename TDGR>
396
  DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn);
397
  template <typename TDGR>
398
  DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn);
424 399

	
425 400
  /// \ingroup lemon_io
426 401
  ///
427 402
  /// \brief \ref lgf-format "LGF" reader for directed graphs
428 403
  ///
429 404
  /// This utility reads an \ref lgf-format "LGF" file.
430 405
  ///
431 406
  /// The reading method does a batch processing. The user creates a
432 407
  /// reader object, then various reading rules can be added to the
433 408
  /// reader, and eventually the reading is executed with the \c run()
434 409
  /// member function. A map reading rule can be added to the reader
435 410
  /// with the \c nodeMap() or \c arcMap() members. An optional
436 411
  /// converter parameter can also be added as a standard functor
437 412
  /// converting from \c std::string to the value type of the map. If it
438 413
  /// is set, it will determine how the tokens in the file should be
439 414
  /// converted to the value type of the map. If the functor is not set,
440 415
  /// then a default conversion will be used. One map can be read into
441 416
  /// multiple map objects at the same time. The \c attribute(), \c
442 417
  /// node() and \c arc() functions are used to add attribute reading
443 418
  /// rules.
444 419
  ///
445 420
  ///\code
446
  /// DigraphReader<Digraph>(digraph, std::cin).
421
  /// DigraphReader<DGR>(digraph, std::cin).
447 422
  ///   nodeMap("coordinates", coord_map).
448 423
  ///   arcMap("capacity", cap_map).
449 424
  ///   node("source", src).
450 425
  ///   node("target", trg).
451 426
  ///   attribute("caption", caption).
452 427
  ///   run();
453 428
  ///\endcode
454 429
  ///
455 430
  /// By default the reader uses the first section in the file of the
456 431
  /// proper type. If a section has an optional name, then it can be
457 432
  /// selected for reading by giving an optional name parameter to the
458 433
  /// \c nodes(), \c arcs() or \c attributes() functions.
459 434
  ///
460 435
  /// The \c useNodes() and \c useArcs() functions are used to tell the reader
461 436
  /// that the nodes or arcs should not be constructed (added to the
462 437
  /// graph) during the reading, but instead the label map of the items
463 438
  /// are given as a parameter of these functions. An
464 439
  /// application of these functions is multipass reading, which is
465 440
  /// important if two \c \@arcs sections must be read from the
466 441
  /// file. In this case the first phase would read the node set and one
467 442
  /// of the arc sets, while the second phase would read the second arc
468 443
  /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
469 444
  /// The previously read label node map should be passed to the \c
470 445
  /// useNodes() functions. Another application of multipass reading when
471 446
  /// paths are given as a node map or an arc map.
472 447
  /// It is impossible to read this in
473 448
  /// a single pass, because the arcs are not constructed when the node
474 449
  /// maps are read.
475
  template <typename _Digraph>
450
  template <typename DGR>
476 451
  class DigraphReader {
477 452
  public:
478 453

	
479
    typedef _Digraph Digraph;
480
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
454
    typedef DGR Digraph;
481 455

	
482 456
  private:
483 457

	
458
    TEMPLATE_DIGRAPH_TYPEDEFS(DGR);
484 459

	
485 460
    std::istream* _is;
486 461
    bool local_is;
487 462
    std::string _filename;
488 463

	
489
    Digraph& _digraph;
464
    DGR& _digraph;
490 465

	
491 466
    std::string _nodes_caption;
492 467
    std::string _arcs_caption;
493 468
    std::string _attributes_caption;
494 469

	
495 470
    typedef std::map<std::string, Node> NodeIndex;
496 471
    NodeIndex _node_index;
497 472
    typedef std::map<std::string, Arc> ArcIndex;
498 473
    ArcIndex _arc_index;
499 474

	
500 475
    typedef std::vector<std::pair<std::string,
501 476
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;
502 477
    NodeMaps _node_maps;
503 478

	
504 479
    typedef std::vector<std::pair<std::string,
505 480
      _reader_bits::MapStorageBase<Arc>*> >ArcMaps;
506 481
    ArcMaps _arc_maps;
507 482

	
508 483
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
509 484
      Attributes;
510 485
    Attributes _attributes;
511 486

	
512 487
    bool _use_nodes;
513 488
    bool _use_arcs;
514 489

	
515 490
    bool _skip_nodes;
516 491
    bool _skip_arcs;
517 492

	
518 493
    int line_num;
519 494
    std::istringstream line;
520 495

	
521 496
  public:
522 497

	
523 498
    /// \brief Constructor
524 499
    ///
525 500
    /// Construct a directed graph reader, which reads from the given
526 501
    /// input stream.
527
    DigraphReader(Digraph& digraph, std::istream& is = std::cin)
502
    DigraphReader(DGR& digraph, std::istream& is = std::cin)
528 503
      : _is(&is), local_is(false), _digraph(digraph),
529 504
        _use_nodes(false), _use_arcs(false),
530 505
        _skip_nodes(false), _skip_arcs(false) {}
531 506

	
532 507
    /// \brief Constructor
533 508
    ///
534 509
    /// Construct a directed graph reader, which reads from the given
535 510
    /// file.
536
    DigraphReader(Digraph& digraph, const std::string& fn)
511
    DigraphReader(DGR& digraph, const std::string& fn)
537 512
      : _is(new std::ifstream(fn.c_str())), local_is(true),
538 513
        _filename(fn), _digraph(digraph),
539 514
        _use_nodes(false), _use_arcs(false),
540 515
        _skip_nodes(false), _skip_arcs(false) {
541 516
      if (!(*_is)) {
542 517
        delete _is;
543 518
        throw IoError("Cannot open file", fn);
544 519
      }
545 520
    }
546 521

	
547 522
    /// \brief Constructor
548 523
    ///
549 524
    /// Construct a directed graph reader, which reads from the given
550 525
    /// file.
551
    DigraphReader(Digraph& digraph, const char* fn)
526
    DigraphReader(DGR& digraph, const char* fn)
552 527
      : _is(new std::ifstream(fn)), local_is(true),
553 528
        _filename(fn), _digraph(digraph),
554 529
        _use_nodes(false), _use_arcs(false),
555 530
        _skip_nodes(false), _skip_arcs(false) {
556 531
      if (!(*_is)) {
557 532
        delete _is;
558 533
        throw IoError("Cannot open file", fn);
559 534
      }
560 535
    }
561 536

	
562 537
    /// \brief Destructor
563 538
    ~DigraphReader() {
564 539
      for (typename NodeMaps::iterator it = _node_maps.begin();
565 540
           it != _node_maps.end(); ++it) {
566 541
        delete it->second;
567 542
      }
568 543

	
569 544
      for (typename ArcMaps::iterator it = _arc_maps.begin();
570 545
           it != _arc_maps.end(); ++it) {
571 546
        delete it->second;
572 547
      }
573 548

	
574 549
      for (typename Attributes::iterator it = _attributes.begin();
575 550
           it != _attributes.end(); ++it) {
576 551
        delete it->second;
577 552
      }
578 553

	
579 554
      if (local_is) {
580 555
        delete _is;
581 556
      }
582 557

	
583 558
    }
584 559

	
585 560
  private:
586 561

	
587
    friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph,
588
                                                  std::istream& is);
589
    friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph,
590
                                                  const std::string& fn);
591
    friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph,
592
                                                  const char *fn);
562
    template <typename TDGR>
563
    friend DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is);
564
    template <typename TDGR>
565
    friend DigraphReader<TDGR> digraphReader(TDGR& digraph, 
566
                                             const std::string& fn);
567
    template <typename TDGR>
568
    friend DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn);
593 569

	
594 570
    DigraphReader(DigraphReader& other)
595 571
      : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
596 572
        _use_nodes(other._use_nodes), _use_arcs(other._use_arcs),
597 573
        _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
598 574

	
599 575
      other._is = 0;
600 576
      other.local_is = false;
601 577

	
602 578
      _node_index.swap(other._node_index);
603 579
      _arc_index.swap(other._arc_index);
604 580

	
605 581
      _node_maps.swap(other._node_maps);
606 582
      _arc_maps.swap(other._arc_maps);
607 583
      _attributes.swap(other._attributes);
608 584

	
609 585
      _nodes_caption = other._nodes_caption;
610 586
      _arcs_caption = other._arcs_caption;
611 587
      _attributes_caption = other._attributes_caption;
612 588

	
613 589
    }
614 590

	
615 591
    DigraphReader& operator=(const DigraphReader&);
616 592

	
617 593
  public:
618 594

	
619
    /// \name Reading rules
595
    /// \name Reading Rules
620 596
    /// @{
621 597

	
622 598
    /// \brief Node map reading rule
623 599
    ///
624 600
    /// Add a node map reading rule to the reader.
625 601
    template <typename Map>
626 602
    DigraphReader& nodeMap(const std::string& caption, Map& map) {
627 603
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
628 604
      _reader_bits::MapStorageBase<Node>* storage =
629 605
        new _reader_bits::MapStorage<Node, Map>(map);
630 606
      _node_maps.push_back(std::make_pair(caption, storage));
631 607
      return *this;
632 608
    }
633 609

	
634 610
    /// \brief Node map reading rule
635 611
    ///
636 612
    /// Add a node map reading rule with specialized converter to the
637 613
    /// reader.
638 614
    template <typename Map, typename Converter>
639 615
    DigraphReader& nodeMap(const std::string& caption, Map& map,
640 616
                           const Converter& converter = Converter()) {
641 617
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
642 618
      _reader_bits::MapStorageBase<Node>* storage =
643 619
        new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
644 620
      _node_maps.push_back(std::make_pair(caption, storage));
645 621
      return *this;
646 622
    }
647 623

	
648 624
    /// \brief Arc map reading rule
649 625
    ///
650 626
    /// Add an arc map reading rule to the reader.
651 627
    template <typename Map>
652 628
    DigraphReader& arcMap(const std::string& caption, Map& map) {
653 629
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
654 630
      _reader_bits::MapStorageBase<Arc>* storage =
655 631
        new _reader_bits::MapStorage<Arc, Map>(map);
656 632
      _arc_maps.push_back(std::make_pair(caption, storage));
657 633
      return *this;
658 634
    }
659 635

	
660 636
    /// \brief Arc map reading rule
661 637
    ///
662 638
    /// Add an arc map reading rule with specialized converter to the
663 639
    /// reader.
664 640
    template <typename Map, typename Converter>
665 641
    DigraphReader& arcMap(const std::string& caption, Map& map,
666 642
                          const Converter& converter = Converter()) {
667 643
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
668 644
      _reader_bits::MapStorageBase<Arc>* storage =
669 645
        new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
670 646
      _arc_maps.push_back(std::make_pair(caption, storage));
671 647
      return *this;
672 648
    }
673 649

	
674 650
    /// \brief Attribute reading rule
675 651
    ///
676 652
    /// Add an attribute reading rule to the reader.
677 653
    template <typename Value>
678 654
    DigraphReader& attribute(const std::string& caption, Value& value) {
679 655
      _reader_bits::ValueStorageBase* storage =
680 656
        new _reader_bits::ValueStorage<Value>(value);
681 657
      _attributes.insert(std::make_pair(caption, storage));
682 658
      return *this;
683 659
    }
684 660

	
685 661
    /// \brief Attribute reading rule
686 662
    ///
687 663
    /// Add an attribute reading rule with specialized converter to the
688 664
    /// reader.
689 665
    template <typename Value, typename Converter>
690 666
    DigraphReader& attribute(const std::string& caption, Value& value,
691 667
                             const Converter& converter = Converter()) {
692 668
      _reader_bits::ValueStorageBase* storage =
693 669
        new _reader_bits::ValueStorage<Value, Converter>(value, converter);
694 670
      _attributes.insert(std::make_pair(caption, storage));
695 671
      return *this;
696 672
    }
697 673

	
698 674
    /// \brief Node reading rule
699 675
    ///
700 676
    /// Add a node reading rule to reader.
701 677
    DigraphReader& node(const std::string& caption, Node& node) {
702 678
      typedef _reader_bits::MapLookUpConverter<Node> Converter;
703 679
      Converter converter(_node_index);
704 680
      _reader_bits::ValueStorageBase* storage =
705 681
        new _reader_bits::ValueStorage<Node, Converter>(node, converter);
706 682
      _attributes.insert(std::make_pair(caption, storage));
707 683
      return *this;
708 684
    }
709 685

	
710 686
    /// \brief Arc reading rule
711 687
    ///
712 688
    /// Add an arc reading rule to reader.
713 689
    DigraphReader& arc(const std::string& caption, Arc& arc) {
714 690
      typedef _reader_bits::MapLookUpConverter<Arc> Converter;
715 691
      Converter converter(_arc_index);
716 692
      _reader_bits::ValueStorageBase* storage =
717 693
        new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
718 694
      _attributes.insert(std::make_pair(caption, storage));
719 695
      return *this;
720 696
    }
721 697

	
722 698
    /// @}
723 699

	
724
    /// \name Select section by name
700
    /// \name Select Section by Name
725 701
    /// @{
726 702

	
727 703
    /// \brief Set \c \@nodes section to be read
728 704
    ///
729 705
    /// Set \c \@nodes section to be read
730 706
    DigraphReader& nodes(const std::string& caption) {
731 707
      _nodes_caption = caption;
732 708
      return *this;
733 709
    }
734 710

	
735 711
    /// \brief Set \c \@arcs section to be read
736 712
    ///
737 713
    /// Set \c \@arcs section to be read
738 714
    DigraphReader& arcs(const std::string& caption) {
739 715
      _arcs_caption = caption;
740 716
      return *this;
741 717
    }
742 718

	
743 719
    /// \brief Set \c \@attributes section to be read
744 720
    ///
745 721
    /// Set \c \@attributes section to be read
746 722
    DigraphReader& attributes(const std::string& caption) {
747 723
      _attributes_caption = caption;
748 724
      return *this;
749 725
    }
750 726

	
751 727
    /// @}
752 728

	
753
    /// \name Using previously constructed node or arc set
729
    /// \name Using Previously Constructed Node or Arc Set
754 730
    /// @{
755 731

	
756 732
    /// \brief Use previously constructed node set
757 733
    ///
758 734
    /// Use previously constructed node set, and specify the node
759 735
    /// label map.
760 736
    template <typename Map>
761 737
    DigraphReader& useNodes(const Map& map) {
762 738
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
763 739
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
764 740
      _use_nodes = true;
765 741
      _writer_bits::DefaultConverter<typename Map::Value> converter;
766 742
      for (NodeIt n(_digraph); n != INVALID; ++n) {
767 743
        _node_index.insert(std::make_pair(converter(map[n]), n));
768 744
      }
769 745
      return *this;
770 746
    }
771 747

	
772 748
    /// \brief Use previously constructed node set
773 749
    ///
774 750
    /// Use previously constructed node set, and specify the node
775 751
    /// label map and a functor which converts the label map values to
776 752
    /// \c std::string.
777 753
    template <typename Map, typename Converter>
778 754
    DigraphReader& useNodes(const Map& map,
779 755
                            const Converter& converter = Converter()) {
780 756
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
781 757
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
782 758
      _use_nodes = true;
783 759
      for (NodeIt n(_digraph); n != INVALID; ++n) {
784 760
        _node_index.insert(std::make_pair(converter(map[n]), n));
785 761
      }
786 762
      return *this;
787 763
    }
788 764

	
789 765
    /// \brief Use previously constructed arc set
790 766
    ///
791 767
    /// Use previously constructed arc set, and specify the arc
792 768
    /// label map.
793 769
    template <typename Map>
794 770
    DigraphReader& useArcs(const Map& map) {
795 771
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
796 772
      LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
797 773
      _use_arcs = true;
798 774
      _writer_bits::DefaultConverter<typename Map::Value> converter;
799 775
      for (ArcIt a(_digraph); a != INVALID; ++a) {
800 776
        _arc_index.insert(std::make_pair(converter(map[a]), a));
801 777
      }
802 778
      return *this;
803 779
    }
804 780

	
805 781
    /// \brief Use previously constructed arc set
806 782
    ///
807 783
    /// Use previously constructed arc set, and specify the arc
808 784
    /// label map and a functor which converts the label map values to
809 785
    /// \c std::string.
810 786
    template <typename Map, typename Converter>
811 787
    DigraphReader& useArcs(const Map& map,
812 788
                           const Converter& converter = Converter()) {
813 789
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
814 790
      LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
815 791
      _use_arcs = true;
816 792
      for (ArcIt a(_digraph); a != INVALID; ++a) {
817 793
        _arc_index.insert(std::make_pair(converter(map[a]), a));
818 794
      }
819 795
      return *this;
820 796
    }
821 797

	
822 798
    /// \brief Skips the reading of node section
823 799
    ///
824 800
    /// Omit the reading of the node section. This implies that each node
825 801
    /// map reading rule will be abandoned, and the nodes of the graph
826 802
    /// will not be constructed, which usually cause that the arc set
827 803
    /// could not be read due to lack of node name resolving.
828 804
    /// Therefore \c skipArcs() function should also be used, or
829 805
    /// \c useNodes() should be used to specify the label of the nodes.
830 806
    DigraphReader& skipNodes() {
831 807
      LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
832 808
      _skip_nodes = true;
833 809
      return *this;
834 810
    }
835 811

	
836 812
    /// \brief Skips the reading of arc section
837 813
    ///
838 814
    /// Omit the reading of the arc section. This implies that each arc
839 815
    /// map reading rule will be abandoned, and the arcs of the graph
840 816
    /// will not be constructed.
841 817
    DigraphReader& skipArcs() {
842 818
      LEMON_ASSERT(!_skip_arcs, "Skip arcs already set");
843 819
      _skip_arcs = true;
844 820
      return *this;
845 821
    }
846 822

	
847 823
    /// @}
848 824

	
849 825
  private:
... ...
@@ -1046,634 +1022,673 @@
1046 1022
        if (!_use_arcs) {
1047 1023

	
1048 1024
          typename NodeIndex::iterator it;
1049 1025

	
1050 1026
          it = _node_index.find(source_token);
1051 1027
          if (it == _node_index.end()) {
1052 1028
            std::ostringstream msg;
1053 1029
            msg << "Item not found: " << source_token;
1054 1030
            throw FormatError(msg.str());
1055 1031
          }
1056 1032
          Node source = it->second;
1057 1033

	
1058 1034
          it = _node_index.find(target_token);
1059 1035
          if (it == _node_index.end()) {
1060 1036
            std::ostringstream msg;
1061 1037
            msg << "Item not found: " << target_token;
1062 1038
            throw FormatError(msg.str());
1063 1039
          }
1064 1040
          Node target = it->second;
1065 1041

	
1066 1042
          a = _digraph.addArc(source, target);
1067 1043
          if (label_index != -1)
1068 1044
            _arc_index.insert(std::make_pair(tokens[label_index], a));
1069 1045
        } else {
1070 1046
          if (label_index == -1)
1071 1047
            throw FormatError("Label map not found");
1072 1048
          typename std::map<std::string, Arc>::iterator it =
1073 1049
            _arc_index.find(tokens[label_index]);
1074 1050
          if (it == _arc_index.end()) {
1075 1051
            std::ostringstream msg;
1076 1052
            msg << "Arc with label not found: " << tokens[label_index];
1077 1053
            throw FormatError(msg.str());
1078 1054
          }
1079 1055
          a = it->second;
1080 1056
        }
1081 1057

	
1082 1058
        for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
1083 1059
          _arc_maps[i].second->set(a, tokens[map_index[i]]);
1084 1060
        }
1085 1061

	
1086 1062
      }
1087 1063
      if (readSuccess()) {
1088 1064
        line.putback(c);
1089 1065
      }
1090 1066
    }
1091 1067

	
1092 1068
    void readAttributes() {
1093 1069

	
1094 1070
      std::set<std::string> read_attr;
1095 1071

	
1096 1072
      char c;
1097 1073
      while (readLine() && line >> c && c != '@') {
1098 1074
        line.putback(c);
1099 1075

	
1100 1076
        std::string attr, token;
1101 1077
        if (!_reader_bits::readToken(line, attr))
1102 1078
          throw FormatError("Attribute name not found");
1103 1079
        if (!_reader_bits::readToken(line, token))
1104 1080
          throw FormatError("Attribute value not found");
1105 1081
        if (line >> c)
1106 1082
          throw FormatError("Extra character at the end of line");
1107 1083

	
1108 1084
        {
1109 1085
          std::set<std::string>::iterator it = read_attr.find(attr);
1110 1086
          if (it != read_attr.end()) {
1111 1087
            std::ostringstream msg;
1112 1088
            msg << "Multiple occurence of attribute: " << attr;
1113 1089
            throw FormatError(msg.str());
1114 1090
          }
1115 1091
          read_attr.insert(attr);
1116 1092
        }
1117 1093

	
1118 1094
        {
1119 1095
          typename Attributes::iterator it = _attributes.lower_bound(attr);
1120 1096
          while (it != _attributes.end() && it->first == attr) {
1121 1097
            it->second->set(token);
1122 1098
            ++it;
1123 1099
          }
1124 1100
        }
1125 1101

	
1126 1102
      }
1127 1103
      if (readSuccess()) {
1128 1104
        line.putback(c);
1129 1105
      }
1130 1106
      for (typename Attributes::iterator it = _attributes.begin();
1131 1107
           it != _attributes.end(); ++it) {
1132 1108
        if (read_attr.find(it->first) == read_attr.end()) {
1133 1109
          std::ostringstream msg;
1134 1110
          msg << "Attribute not found: " << it->first;
1135 1111
          throw FormatError(msg.str());
1136 1112
        }
1137 1113
      }
1138 1114
    }
1139 1115

	
1140 1116
  public:
1141 1117

	
1142
    /// \name Execution of the reader
1118
    /// \name Execution of the Reader
1143 1119
    /// @{
1144 1120

	
1145 1121
    /// \brief Start the batch processing
1146 1122
    ///
1147 1123
    /// This function starts the batch processing
1148 1124
    void run() {
1149 1125
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
1150 1126

	
1151 1127
      bool nodes_done = _skip_nodes;
1152 1128
      bool arcs_done = _skip_arcs;
1153 1129
      bool attributes_done = false;
1154 1130

	
1155 1131
      line_num = 0;
1156 1132
      readLine();
1157 1133
      skipSection();
1158 1134

	
1159 1135
      while (readSuccess()) {
1160 1136
        try {
1161 1137
          char c;
1162 1138
          std::string section, caption;
1163 1139
          line >> c;
1164 1140
          _reader_bits::readToken(line, section);
1165 1141
          _reader_bits::readToken(line, caption);
1166 1142

	
1167 1143
          if (line >> c)
1168 1144
            throw FormatError("Extra character at the end of line");
1169 1145

	
1170 1146
          if (section == "nodes" && !nodes_done) {
1171 1147
            if (_nodes_caption.empty() || _nodes_caption == caption) {
1172 1148
              readNodes();
1173 1149
              nodes_done = true;
1174 1150
            }
1175 1151
          } else if ((section == "arcs" || section == "edges") &&
1176 1152
                     !arcs_done) {
1177 1153
            if (_arcs_caption.empty() || _arcs_caption == caption) {
1178 1154
              readArcs();
1179 1155
              arcs_done = true;
1180 1156
            }
1181 1157
          } else if (section == "attributes" && !attributes_done) {
1182 1158
            if (_attributes_caption.empty() || _attributes_caption == caption) {
1183 1159
              readAttributes();
1184 1160
              attributes_done = true;
1185 1161
            }
1186 1162
          } else {
1187 1163
            readLine();
1188 1164
            skipSection();
1189 1165
          }
1190 1166
        } catch (FormatError& error) {
1191 1167
          error.line(line_num);
1192 1168
          error.file(_filename);
1193 1169
          throw;
1194 1170
        }
1195 1171
      }
1196 1172

	
1197 1173
      if (!nodes_done) {
1198 1174
        throw FormatError("Section @nodes not found");
1199 1175
      }
1200 1176

	
1201 1177
      if (!arcs_done) {
1202 1178
        throw FormatError("Section @arcs not found");
1203 1179
      }
1204 1180

	
1205 1181
      if (!attributes_done && !_attributes.empty()) {
1206 1182
        throw FormatError("Section @attributes not found");
1207 1183
      }
1208 1184

	
1209 1185
    }
1210 1186

	
1211 1187
    /// @}
1212 1188

	
1213 1189
  };
1214

	
1215
  template <typename Graph>
1216
  class GraphReader;
1217

	
1218
  /// \brief Return a \ref GraphReader class
1190
  
1191
  /// \ingroup lemon_io
1219 1192
  ///
1220
  /// This function just returns a \ref GraphReader class.
1221
  /// \relates GraphReader
1222
  template <typename Graph>
1223
  GraphReader<Graph> graphReader(Graph& graph, std::istream& is = std::cin) {
1224
    GraphReader<Graph> tmp(graph, is);
1193
  /// \brief Return a \ref DigraphReader class
1194
  ///
1195
  /// This function just returns a \ref DigraphReader class.
1196
  ///
1197
  /// With this function a digraph can be read from an 
1198
  /// \ref lgf-format "LGF" file or input stream with several maps and
1199
  /// attributes. For example, there is network flow problem on a
1200
  /// digraph, i.e. a digraph with a \e capacity map on the arcs and
1201
  /// \e source and \e target nodes. This digraph can be read with the
1202
  /// following code:
1203
  ///
1204
  ///\code
1205
  ///ListDigraph digraph;
1206
  ///ListDigraph::ArcMap<int> cm(digraph);
1207
  ///ListDigraph::Node src, trg;
1208
  ///digraphReader(digraph, std::cin).
1209
  ///  arcMap("capacity", cap).
1210
  ///  node("source", src).
1211
  ///  node("target", trg).
1212
  ///  run();
1213
  ///\endcode
1214
  ///
1215
  /// For a complete documentation, please see the \ref DigraphReader
1216
  /// class documentation.
1217
  /// \warning Don't forget to put the \ref DigraphReader::run() "run()"
1218
  /// to the end of the parameter list.
1219
  /// \relates DigraphReader
1220
  /// \sa digraphReader(TDGR& digraph, const std::string& fn)
1221
  /// \sa digraphReader(TDGR& digraph, const char* fn)
1222
  template <typename TDGR>
1223
  DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is) {
1224
    DigraphReader<TDGR> tmp(digraph, is);
1225 1225
    return tmp;
1226 1226
  }
1227 1227

	
1228
  /// \brief Return a \ref GraphReader class
1228
  /// \brief Return a \ref DigraphReader class
1229 1229
  ///
1230
  /// This function just returns a \ref GraphReader class.
1231
  /// \relates GraphReader
1232
  template <typename Graph>
1233
  GraphReader<Graph> graphReader(Graph& graph, const std::string& fn) {
1234
    GraphReader<Graph> tmp(graph, fn);
1230
  /// This function just returns a \ref DigraphReader class.
1231
  /// \relates DigraphReader
1232
  /// \sa digraphReader(TDGR& digraph, std::istream& is)
1233
  template <typename TDGR>
1234
  DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn) {
1235
    DigraphReader<TDGR> tmp(digraph, fn);
1235 1236
    return tmp;
1236 1237
  }
1237 1238

	
1238
  /// \brief Return a \ref GraphReader class
1239
  /// \brief Return a \ref DigraphReader class
1239 1240
  ///
1240
  /// This function just returns a \ref GraphReader class.
1241
  /// \relates GraphReader
1242
  template <typename Graph>
1243
  GraphReader<Graph> graphReader(Graph& graph, const char* fn) {
1244
    GraphReader<Graph> tmp(graph, fn);
1241
  /// This function just returns a \ref DigraphReader class.
1242
  /// \relates DigraphReader
1243
  /// \sa digraphReader(TDGR& digraph, std::istream& is)
1244
  template <typename TDGR>
1245
  DigraphReader<TDGR> digraphReader(TDGR& digraph, const char* fn) {
1246
    DigraphReader<TDGR> tmp(digraph, fn);
1245 1247
    return tmp;
1246 1248
  }
1247 1249

	
1250
  template <typename GR>
1251
  class GraphReader;
1252
 
1253
  template <typename TGR>
1254
  GraphReader<TGR> graphReader(TGR& graph, std::istream& is = std::cin);
1255
  template <typename TGR>
1256
  GraphReader<TGR> graphReader(TGR& graph, const std::string& fn);
1257
  template <typename TGR>
1258
  GraphReader<TGR> graphReader(TGR& graph, const char *fn);
1259

	
1248 1260
  /// \ingroup lemon_io
1249 1261
  ///
1250 1262
  /// \brief \ref lgf-format "LGF" reader for undirected graphs
1251 1263
  ///
1252 1264
  /// This utility reads an \ref lgf-format "LGF" file.
1253 1265
  ///
1254 1266
  /// It can be used almost the same way as \c DigraphReader.
1255 1267
  /// The only difference is that this class can handle edges and
1256 1268
  /// edge maps as well as arcs and arc maps.
1257 1269
  ///
1258 1270
  /// The columns in the \c \@edges (or \c \@arcs) section are the
1259 1271
  /// edge maps. However, if there are two maps with the same name
1260 1272
  /// prefixed with \c '+' and \c '-', then these can be read into an
1261 1273
  /// arc map.  Similarly, an attribute can be read into an arc, if
1262 1274
  /// it's value is an edge label prefixed with \c '+' or \c '-'.
1263
  template <typename _Graph>
1275
  template <typename GR>
1264 1276
  class GraphReader {
1265 1277
  public:
1266 1278

	
1267
    typedef _Graph Graph;
1268
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
1279
    typedef GR Graph;
1269 1280

	
1270 1281
  private:
1271 1282

	
1283
    TEMPLATE_GRAPH_TYPEDEFS(GR);
1284

	
1272 1285
    std::istream* _is;
1273 1286
    bool local_is;
1274 1287
    std::string _filename;
1275 1288

	
1276
    Graph& _graph;
1289
    GR& _graph;
1277 1290

	
1278 1291
    std::string _nodes_caption;
1279 1292
    std::string _edges_caption;
1280 1293
    std::string _attributes_caption;
1281 1294

	
1282 1295
    typedef std::map<std::string, Node> NodeIndex;
1283 1296
    NodeIndex _node_index;
1284 1297
    typedef std::map<std::string, Edge> EdgeIndex;
1285 1298
    EdgeIndex _edge_index;
1286 1299

	
1287 1300
    typedef std::vector<std::pair<std::string,
1288 1301
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;
1289 1302
    NodeMaps _node_maps;
1290 1303

	
1291 1304
    typedef std::vector<std::pair<std::string,
1292 1305
      _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
1293 1306
    EdgeMaps _edge_maps;
1294 1307

	
1295 1308
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
1296 1309
      Attributes;
1297 1310
    Attributes _attributes;
1298 1311

	
1299 1312
    bool _use_nodes;
1300 1313
    bool _use_edges;
1301 1314

	
1302 1315
    bool _skip_nodes;
1303 1316
    bool _skip_edges;
1304 1317

	
1305 1318
    int line_num;
1306 1319
    std::istringstream line;
1307 1320

	
1308 1321
  public:
1309 1322

	
1310 1323
    /// \brief Constructor
1311 1324
    ///
1312 1325
    /// Construct an undirected graph reader, which reads from the given
1313 1326
    /// input stream.
1314
    GraphReader(Graph& graph, std::istream& is = std::cin)
1327
    GraphReader(GR& graph, std::istream& is = std::cin)
1315 1328
      : _is(&is), local_is(false), _graph(graph),
1316 1329
        _use_nodes(false), _use_edges(false),
1317 1330
        _skip_nodes(false), _skip_edges(false) {}
1318 1331

	
1319 1332
    /// \brief Constructor
1320 1333
    ///
1321 1334
    /// Construct an undirected graph reader, which reads from the given
1322 1335
    /// file.
1323
    GraphReader(Graph& graph, const std::string& fn)
1336
    GraphReader(GR& graph, const std::string& fn)
1324 1337
      : _is(new std::ifstream(fn.c_str())), local_is(true),
1325 1338
        _filename(fn), _graph(graph),
1326 1339
        _use_nodes(false), _use_edges(false),
1327 1340
        _skip_nodes(false), _skip_edges(false) {
1328 1341
      if (!(*_is)) {
1329 1342
        delete _is;
1330 1343
        throw IoError("Cannot open file", fn);
1331 1344
      }
1332 1345
    }
1333 1346

	
1334 1347
    /// \brief Constructor
1335 1348
    ///
1336 1349
    /// Construct an undirected graph reader, which reads from the given
1337 1350
    /// file.
1338
    GraphReader(Graph& graph, const char* fn)
1351
    GraphReader(GR& graph, const char* fn)
1339 1352
      : _is(new std::ifstream(fn)), local_is(true),
1340 1353
        _filename(fn), _graph(graph),
1341 1354
        _use_nodes(false), _use_edges(false),
1342 1355
        _skip_nodes(false), _skip_edges(false) {
1343 1356
      if (!(*_is)) {
1344 1357
        delete _is;
1345 1358
        throw IoError("Cannot open file", fn);
1346 1359
      }
1347 1360
    }
1348 1361

	
1349 1362
    /// \brief Destructor
1350 1363
    ~GraphReader() {
1351 1364
      for (typename NodeMaps::iterator it = _node_maps.begin();
1352 1365
           it != _node_maps.end(); ++it) {
1353 1366
        delete it->second;
1354 1367
      }
1355 1368

	
1356 1369
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
1357 1370
           it != _edge_maps.end(); ++it) {
1358 1371
        delete it->second;
1359 1372
      }
1360 1373

	
1361 1374
      for (typename Attributes::iterator it = _attributes.begin();
1362 1375
           it != _attributes.end(); ++it) {
1363 1376
        delete it->second;
1364 1377
      }
1365 1378

	
1366 1379
      if (local_is) {
1367 1380
        delete _is;
1368 1381
      }
1369 1382

	
1370 1383
    }
1371 1384

	
1372 1385
  private:
1373
    friend GraphReader<Graph> graphReader<>(Graph& graph, std::istream& is);
1374
    friend GraphReader<Graph> graphReader<>(Graph& graph,
1375
                                            const std::string& fn);
1376
    friend GraphReader<Graph> graphReader<>(Graph& graph, const char *fn);
1386
    template <typename TGR>
1387
    friend GraphReader<TGR> graphReader(TGR& graph, std::istream& is);
1388
    template <typename TGR>
1389
    friend GraphReader<TGR> graphReader(TGR& graph, const std::string& fn); 
1390
    template <typename TGR>
1391
    friend GraphReader<TGR> graphReader(TGR& graph, const char *fn);
1377 1392

	
1378 1393
    GraphReader(GraphReader& other)
1379 1394
      : _is(other._is), local_is(other.local_is), _graph(other._graph),
1380 1395
        _use_nodes(other._use_nodes), _use_edges(other._use_edges),
1381 1396
        _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
1382 1397

	
1383 1398
      other._is = 0;
1384 1399
      other.local_is = false;
1385 1400

	
1386 1401
      _node_index.swap(other._node_index);
1387 1402
      _edge_index.swap(other._edge_index);
1388 1403

	
1389 1404
      _node_maps.swap(other._node_maps);
1390 1405
      _edge_maps.swap(other._edge_maps);
1391 1406
      _attributes.swap(other._attributes);
1392 1407

	
1393 1408
      _nodes_caption = other._nodes_caption;
1394 1409
      _edges_caption = other._edges_caption;
1395 1410
      _attributes_caption = other._attributes_caption;
1396 1411

	
1397 1412
    }
1398 1413

	
1399 1414
    GraphReader& operator=(const GraphReader&);
1400 1415

	
1401 1416
  public:
1402 1417

	
1403
    /// \name Reading rules
1418
    /// \name Reading Rules
1404 1419
    /// @{
1405 1420

	
1406 1421
    /// \brief Node map reading rule
1407 1422
    ///
1408 1423
    /// Add a node map reading rule to the reader.
1409 1424
    template <typename Map>
1410 1425
    GraphReader& nodeMap(const std::string& caption, Map& map) {
1411 1426
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
1412 1427
      _reader_bits::MapStorageBase<Node>* storage =
1413 1428
        new _reader_bits::MapStorage<Node, Map>(map);
1414 1429
      _node_maps.push_back(std::make_pair(caption, storage));
1415 1430
      return *this;
1416 1431
    }
1417 1432

	
1418 1433
    /// \brief Node map reading rule
1419 1434
    ///
1420 1435
    /// Add a node map reading rule with specialized converter to the
1421 1436
    /// reader.
1422 1437
    template <typename Map, typename Converter>
1423 1438
    GraphReader& nodeMap(const std::string& caption, Map& map,
1424 1439
                           const Converter& converter = Converter()) {
1425 1440
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
1426 1441
      _reader_bits::MapStorageBase<Node>* storage =
1427 1442
        new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
1428 1443
      _node_maps.push_back(std::make_pair(caption, storage));
1429 1444
      return *this;
1430 1445
    }
1431 1446

	
1432 1447
    /// \brief Edge map reading rule
1433 1448
    ///
1434 1449
    /// Add an edge map reading rule to the reader.
1435 1450
    template <typename Map>
1436 1451
    GraphReader& edgeMap(const std::string& caption, Map& map) {
1437 1452
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
1438 1453
      _reader_bits::MapStorageBase<Edge>* storage =
1439 1454
        new _reader_bits::MapStorage<Edge, Map>(map);
1440 1455
      _edge_maps.push_back(std::make_pair(caption, storage));
1441 1456
      return *this;
1442 1457
    }
1443 1458

	
1444 1459
    /// \brief Edge map reading rule
1445 1460
    ///
1446 1461
    /// Add an edge map reading rule with specialized converter to the
1447 1462
    /// reader.
1448 1463
    template <typename Map, typename Converter>
1449 1464
    GraphReader& edgeMap(const std::string& caption, Map& map,
1450 1465
                          const Converter& converter = Converter()) {
1451 1466
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
1452 1467
      _reader_bits::MapStorageBase<Edge>* storage =
1453 1468
        new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter);
1454 1469
      _edge_maps.push_back(std::make_pair(caption, storage));
1455 1470
      return *this;
1456 1471
    }
1457 1472

	
1458 1473
    /// \brief Arc map reading rule
1459 1474
    ///
1460 1475
    /// Add an arc map reading rule to the reader.
1461 1476
    template <typename Map>
1462 1477
    GraphReader& arcMap(const std::string& caption, Map& map) {
1463 1478
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
1464 1479
      _reader_bits::MapStorageBase<Edge>* forward_storage =
1465 1480
        new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
1466 1481
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1467 1482
      _reader_bits::MapStorageBase<Edge>* backward_storage =
1468
        new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
1483
        new _reader_bits::GraphArcMapStorage<GR, false, Map>(_graph, map);
1469 1484
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1470 1485
      return *this;
1471 1486
    }
1472 1487

	
1473 1488
    /// \brief Arc map reading rule
1474 1489
    ///
1475 1490
    /// Add an arc map reading rule with specialized converter to the
1476 1491
    /// reader.
1477 1492
    template <typename Map, typename Converter>
1478 1493
    GraphReader& arcMap(const std::string& caption, Map& map,
1479 1494
                          const Converter& converter = Converter()) {
1480 1495
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
1481 1496
      _reader_bits::MapStorageBase<Edge>* forward_storage =
1482
        new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter>
1497
        new _reader_bits::GraphArcMapStorage<GR, true, Map, Converter>
1483 1498
        (_graph, map, converter);
1484 1499
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1485 1500
      _reader_bits::MapStorageBase<Edge>* backward_storage =
1486
        new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter>
1501
        new _reader_bits::GraphArcMapStorage<GR, false, Map, Converter>
1487 1502
        (_graph, map, converter);
1488 1503
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1489 1504
      return *this;
1490 1505
    }
1491 1506

	
1492 1507
    /// \brief Attribute reading rule
1493 1508
    ///
1494 1509
    /// Add an attribute reading rule to the reader.
1495 1510
    template <typename Value>
1496 1511
    GraphReader& attribute(const std::string& caption, Value& value) {
1497 1512
      _reader_bits::ValueStorageBase* storage =
1498 1513
        new _reader_bits::ValueStorage<Value>(value);
1499 1514
      _attributes.insert(std::make_pair(caption, storage));
1500 1515
      return *this;
1501 1516
    }
1502 1517

	
1503 1518
    /// \brief Attribute reading rule
1504 1519
    ///
1505 1520
    /// Add an attribute reading rule with specialized converter to the
1506 1521
    /// reader.
1507 1522
    template <typename Value, typename Converter>
1508 1523
    GraphReader& attribute(const std::string& caption, Value& value,
1509 1524
                             const Converter& converter = Converter()) {
1510 1525
      _reader_bits::ValueStorageBase* storage =
1511 1526
        new _reader_bits::ValueStorage<Value, Converter>(value, converter);
1512 1527
      _attributes.insert(std::make_pair(caption, storage));
1513 1528
      return *this;
1514 1529
    }
1515 1530

	
1516 1531
    /// \brief Node reading rule
1517 1532
    ///
1518 1533
    /// Add a node reading rule to reader.
1519 1534
    GraphReader& node(const std::string& caption, Node& node) {
1520 1535
      typedef _reader_bits::MapLookUpConverter<Node> Converter;
1521 1536
      Converter converter(_node_index);
1522 1537
      _reader_bits::ValueStorageBase* storage =
1523 1538
        new _reader_bits::ValueStorage<Node, Converter>(node, converter);
1524 1539
      _attributes.insert(std::make_pair(caption, storage));
1525 1540
      return *this;
1526 1541
    }
1527 1542

	
1528 1543
    /// \brief Edge reading rule
1529 1544
    ///
1530 1545
    /// Add an edge reading rule to reader.
1531 1546
    GraphReader& edge(const std::string& caption, Edge& edge) {
1532 1547
      typedef _reader_bits::MapLookUpConverter<Edge> Converter;
1533 1548
      Converter converter(_edge_index);
1534 1549
      _reader_bits::ValueStorageBase* storage =
1535 1550
        new _reader_bits::ValueStorage<Edge, Converter>(edge, converter);
1536 1551
      _attributes.insert(std::make_pair(caption, storage));
1537 1552
      return *this;
1538 1553
    }
1539 1554

	
1540 1555
    /// \brief Arc reading rule
1541 1556
    ///
1542 1557
    /// Add an arc reading rule to reader.
1543 1558
    GraphReader& arc(const std::string& caption, Arc& arc) {
1544
      typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter;
1559
      typedef _reader_bits::GraphArcLookUpConverter<GR> Converter;
1545 1560
      Converter converter(_graph, _edge_index);
1546 1561
      _reader_bits::ValueStorageBase* storage =
1547 1562
        new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
1548 1563
      _attributes.insert(std::make_pair(caption, storage));
1549 1564
      return *this;
1550 1565
    }
1551 1566

	
1552 1567
    /// @}
1553 1568

	
1554
    /// \name Select section by name
1569
    /// \name Select Section by Name
1555 1570
    /// @{
1556 1571

	
1557 1572
    /// \brief Set \c \@nodes section to be read
1558 1573
    ///
1559 1574
    /// Set \c \@nodes section to be read.
1560 1575
    GraphReader& nodes(const std::string& caption) {
1561 1576
      _nodes_caption = caption;
1562 1577
      return *this;
1563 1578
    }
1564 1579

	
1565 1580
    /// \brief Set \c \@edges section to be read
1566 1581
    ///
1567 1582
    /// Set \c \@edges section to be read.
1568 1583
    GraphReader& edges(const std::string& caption) {
1569 1584
      _edges_caption = caption;
1570 1585
      return *this;
1571 1586
    }
1572 1587

	
1573 1588
    /// \brief Set \c \@attributes section to be read
1574 1589
    ///
1575 1590
    /// Set \c \@attributes section to be read.
1576 1591
    GraphReader& attributes(const std::string& caption) {
1577 1592
      _attributes_caption = caption;
1578 1593
      return *this;
1579 1594
    }
1580 1595

	
1581 1596
    /// @}
1582 1597

	
1583
    /// \name Using previously constructed node or edge set
1598
    /// \name Using Previously Constructed Node or Edge Set
1584 1599
    /// @{
1585 1600

	
1586 1601
    /// \brief Use previously constructed node set
1587 1602
    ///
1588 1603
    /// Use previously constructed node set, and specify the node
1589 1604
    /// label map.
1590 1605
    template <typename Map>
1591 1606
    GraphReader& useNodes(const Map& map) {
1592 1607
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
1593 1608
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
1594 1609
      _use_nodes = true;
1595 1610
      _writer_bits::DefaultConverter<typename Map::Value> converter;
1596 1611
      for (NodeIt n(_graph); n != INVALID; ++n) {
1597 1612
        _node_index.insert(std::make_pair(converter(map[n]), n));
1598 1613
      }
1599 1614
      return *this;
1600 1615
    }
1601 1616

	
1602 1617
    /// \brief Use previously constructed node set
1603 1618
    ///
1604 1619
    /// Use previously constructed node set, and specify the node
1605 1620
    /// label map and a functor which converts the label map values to
1606 1621
    /// \c std::string.
1607 1622
    template <typename Map, typename Converter>
1608 1623
    GraphReader& useNodes(const Map& map,
1609 1624
                            const Converter& converter = Converter()) {
1610 1625
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
1611 1626
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
1612 1627
      _use_nodes = true;
1613 1628
      for (NodeIt n(_graph); n != INVALID; ++n) {
1614 1629
        _node_index.insert(std::make_pair(converter(map[n]), n));
1615 1630
      }
1616 1631
      return *this;
1617 1632
    }
1618 1633

	
1619 1634
    /// \brief Use previously constructed edge set
1620 1635
    ///
1621 1636
    /// Use previously constructed edge set, and specify the edge
1622 1637
    /// label map.
1623 1638
    template <typename Map>
1624 1639
    GraphReader& useEdges(const Map& map) {
1625 1640
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
1626 1641
      LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
1627 1642
      _use_edges = true;
1628 1643
      _writer_bits::DefaultConverter<typename Map::Value> converter;
1629 1644
      for (EdgeIt a(_graph); a != INVALID; ++a) {
1630 1645
        _edge_index.insert(std::make_pair(converter(map[a]), a));
1631 1646
      }
1632 1647
      return *this;
1633 1648
    }
1634 1649

	
1635 1650
    /// \brief Use previously constructed edge set
1636 1651
    ///
1637 1652
    /// Use previously constructed edge set, and specify the edge
1638 1653
    /// label map and a functor which converts the label map values to
1639 1654
    /// \c std::string.
1640 1655
    template <typename Map, typename Converter>
1641 1656
    GraphReader& useEdges(const Map& map,
1642 1657
                            const Converter& converter = Converter()) {
1643 1658
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
1644 1659
      LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
1645 1660
      _use_edges = true;
1646 1661
      for (EdgeIt a(_graph); a != INVALID; ++a) {
1647 1662
        _edge_index.insert(std::make_pair(converter(map[a]), a));
1648 1663
      }
1649 1664
      return *this;
1650 1665
    }
1651 1666

	
1652 1667
    /// \brief Skip the reading of node section
1653 1668
    ///
1654 1669
    /// Omit the reading of the node section. This implies that each node
1655 1670
    /// map reading rule will be abandoned, and the nodes of the graph
1656 1671
    /// will not be constructed, which usually cause that the edge set
1657 1672
    /// could not be read due to lack of node name
1658 1673
    /// could not be read due to lack of node name resolving.
1659 1674
    /// Therefore \c skipEdges() function should also be used, or
1660 1675
    /// \c useNodes() should be used to specify the label of the nodes.
1661 1676
    GraphReader& skipNodes() {
1662 1677
      LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
1663 1678
      _skip_nodes = true;
1664 1679
      return *this;
1665 1680
    }
1666 1681

	
1667 1682
    /// \brief Skip the reading of edge section
1668 1683
    ///
1669 1684
    /// Omit the reading of the edge section. This implies that each edge
1670 1685
    /// map reading rule will be abandoned, and the edges of the graph
1671 1686
    /// will not be constructed.
1672 1687
    GraphReader& skipEdges() {
1673 1688
      LEMON_ASSERT(!_skip_edges, "Skip edges already set");
1674 1689
      _skip_edges = true;
1675 1690
      return *this;
1676 1691
    }
1677 1692

	
1678 1693
    /// @}
1679 1694

	
... ...
@@ -1877,780 +1892,845 @@
1877 1892
        if (!_use_edges) {
1878 1893

	
1879 1894
          typename NodeIndex::iterator it;
1880 1895

	
1881 1896
          it = _node_index.find(source_token);
1882 1897
          if (it == _node_index.end()) {
1883 1898
            std::ostringstream msg;
1884 1899
            msg << "Item not found: " << source_token;
1885 1900
            throw FormatError(msg.str());
1886 1901
          }
1887 1902
          Node source = it->second;
1888 1903

	
1889 1904
          it = _node_index.find(target_token);
1890 1905
          if (it == _node_index.end()) {
1891 1906
            std::ostringstream msg;
1892 1907
            msg << "Item not found: " << target_token;
1893 1908
            throw FormatError(msg.str());
1894 1909
          }
1895 1910
          Node target = it->second;
1896 1911

	
1897 1912
          e = _graph.addEdge(source, target);
1898 1913
          if (label_index != -1)
1899 1914
            _edge_index.insert(std::make_pair(tokens[label_index], e));
1900 1915
        } else {
1901 1916
          if (label_index == -1)
1902 1917
            throw FormatError("Label map not found");
1903 1918
          typename std::map<std::string, Edge>::iterator it =
1904 1919
            _edge_index.find(tokens[label_index]);
1905 1920
          if (it == _edge_index.end()) {
1906 1921
            std::ostringstream msg;
1907 1922
            msg << "Edge with label not found: " << tokens[label_index];
1908 1923
            throw FormatError(msg.str());
1909 1924
          }
1910 1925
          e = it->second;
1911 1926
        }
1912 1927

	
1913 1928
        for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
1914 1929
          _edge_maps[i].second->set(e, tokens[map_index[i]]);
1915 1930
        }
1916 1931

	
1917 1932
      }
1918 1933
      if (readSuccess()) {
1919 1934
        line.putback(c);
1920 1935
      }
1921 1936
    }
1922 1937

	
1923 1938
    void readAttributes() {
1924 1939

	
1925 1940
      std::set<std::string> read_attr;
1926 1941

	
1927 1942
      char c;
1928 1943
      while (readLine() && line >> c && c != '@') {
1929 1944
        line.putback(c);
1930 1945

	
1931 1946
        std::string attr, token;
1932 1947
        if (!_reader_bits::readToken(line, attr))
1933 1948
          throw FormatError("Attribute name not found");
1934 1949
        if (!_reader_bits::readToken(line, token))
1935 1950
          throw FormatError("Attribute value not found");
1936 1951
        if (line >> c)
1937 1952
          throw FormatError("Extra character at the end of line");
1938 1953

	
1939 1954
        {
1940 1955
          std::set<std::string>::iterator it = read_attr.find(attr);
1941 1956
          if (it != read_attr.end()) {
1942 1957
            std::ostringstream msg;
1943 1958
            msg << "Multiple occurence of attribute: " << attr;
1944 1959
            throw FormatError(msg.str());
1945 1960
          }
1946 1961
          read_attr.insert(attr);
1947 1962
        }
1948 1963

	
1949 1964
        {
1950 1965
          typename Attributes::iterator it = _attributes.lower_bound(attr);
1951 1966
          while (it != _attributes.end() && it->first == attr) {
1952 1967
            it->second->set(token);
1953 1968
            ++it;
1954 1969
          }
1955 1970
        }
1956 1971

	
1957 1972
      }
1958 1973
      if (readSuccess()) {
1959 1974
        line.putback(c);
1960 1975
      }
1961 1976
      for (typename Attributes::iterator it = _attributes.begin();
1962 1977
           it != _attributes.end(); ++it) {
1963 1978
        if (read_attr.find(it->first) == read_attr.end()) {
1964 1979
          std::ostringstream msg;
1965 1980
          msg << "Attribute not found: " << it->first;
1966 1981
          throw FormatError(msg.str());
1967 1982
        }
1968 1983
      }
1969 1984
    }
1970 1985

	
1971 1986
  public:
1972 1987

	
1973
    /// \name Execution of the reader
1988
    /// \name Execution of the Reader
1974 1989
    /// @{
1975 1990

	
1976 1991
    /// \brief Start the batch processing
1977 1992
    ///
1978 1993
    /// This function starts the batch processing
1979 1994
    void run() {
1980 1995

	
1981 1996
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
1982 1997

	
1983 1998
      bool nodes_done = _skip_nodes;
1984 1999
      bool edges_done = _skip_edges;
1985 2000
      bool attributes_done = false;
1986 2001

	
1987 2002
      line_num = 0;
1988 2003
      readLine();
1989 2004
      skipSection();
1990 2005

	
1991 2006
      while (readSuccess()) {
1992 2007
        try {
1993 2008
          char c;
1994 2009
          std::string section, caption;
1995 2010
          line >> c;
1996 2011
          _reader_bits::readToken(line, section);
1997 2012
          _reader_bits::readToken(line, caption);
1998 2013

	
1999 2014
          if (line >> c)
2000 2015
            throw FormatError("Extra character at the end of line");
2001 2016

	
2002 2017
          if (section == "nodes" && !nodes_done) {
2003 2018
            if (_nodes_caption.empty() || _nodes_caption == caption) {
2004 2019
              readNodes();
2005 2020
              nodes_done = true;
2006 2021
            }
2007 2022
          } else if ((section == "edges" || section == "arcs") &&
2008 2023
                     !edges_done) {
2009 2024
            if (_edges_caption.empty() || _edges_caption == caption) {
2010 2025
              readEdges();
2011 2026
              edges_done = true;
2012 2027
            }
2013 2028
          } else if (section == "attributes" && !attributes_done) {
2014 2029
            if (_attributes_caption.empty() || _attributes_caption == caption) {
2015 2030
              readAttributes();
2016 2031
              attributes_done = true;
2017 2032
            }
2018 2033
          } else {
2019 2034
            readLine();
2020 2035
            skipSection();
2021 2036
          }
2022 2037
        } catch (FormatError& error) {
2023 2038
          error.line(line_num);
2024 2039
          error.file(_filename);
2025 2040
          throw;
2026 2041
        }
2027 2042
      }
2028 2043

	
2029 2044
      if (!nodes_done) {
2030 2045
        throw FormatError("Section @nodes not found");
2031 2046
      }
2032 2047

	
2033 2048
      if (!edges_done) {
2034 2049
        throw FormatError("Section @edges not found");
2035 2050
      }
2036 2051

	
2037 2052
      if (!attributes_done && !_attributes.empty()) {
2038 2053
        throw FormatError("Section @attributes not found");
2039 2054
      }
2040 2055

	
2041 2056
    }
2042 2057

	
2043 2058
    /// @}
2044 2059

	
2045 2060
  };
2046 2061

	
2062
  /// \ingroup lemon_io
2063
  ///
2064
  /// \brief Return a \ref GraphReader class
2065
  ///
2066
  /// This function just returns a \ref GraphReader class. 
2067
  ///
2068
  /// With this function a graph can be read from an 
2069
  /// \ref lgf-format "LGF" file or input stream with several maps and
2070
  /// attributes. For example, there is weighted matching problem on a
2071
  /// graph, i.e. a graph with a \e weight map on the edges. This
2072
  /// graph can be read with the following code:
2073
  ///
2074
  ///\code
2075
  ///ListGraph graph;
2076
  ///ListGraph::EdgeMap<int> weight(graph);
2077
  ///graphReader(graph, std::cin).
2078
  ///  edgeMap("weight", weight).
2079
  ///  run();
2080
  ///\endcode
2081
  ///
2082
  /// For a complete documentation, please see the \ref GraphReader
2083
  /// class documentation.
2084
  /// \warning Don't forget to put the \ref GraphReader::run() "run()"
2085
  /// to the end of the parameter list.
2086
  /// \relates GraphReader
2087
  /// \sa graphReader(TGR& graph, const std::string& fn)
2088
  /// \sa graphReader(TGR& graph, const char* fn)
2089
  template <typename TGR>
2090
  GraphReader<TGR> graphReader(TGR& graph, std::istream& is) {
2091
    GraphReader<TGR> tmp(graph, is);
2092
    return tmp;
2093
  }
2094

	
2095
  /// \brief Return a \ref GraphReader class
2096
  ///
2097
  /// This function just returns a \ref GraphReader class.
2098
  /// \relates GraphReader
2099
  /// \sa graphReader(TGR& graph, std::istream& is)
2100
  template <typename TGR>
2101
  GraphReader<TGR> graphReader(TGR& graph, const std::string& fn) {
2102
    GraphReader<TGR> tmp(graph, fn);
2103
    return tmp;
2104
  }
2105

	
2106
  /// \brief Return a \ref GraphReader class
2107
  ///
2108
  /// This function just returns a \ref GraphReader class.
2109
  /// \relates GraphReader
2110
  /// \sa graphReader(TGR& graph, std::istream& is)
2111
  template <typename TGR>
2112
  GraphReader<TGR> graphReader(TGR& graph, const char* fn) {
2113
    GraphReader<TGR> tmp(graph, fn);
2114
    return tmp;
2115
  }
2116

	
2047 2117
  class SectionReader;
2048 2118

	
2049 2119
  SectionReader sectionReader(std::istream& is);
2050 2120
  SectionReader sectionReader(const std::string& fn);
2051 2121
  SectionReader sectionReader(const char* fn);
2052 2122

	
2053 2123
  /// \ingroup lemon_io
2054 2124
  ///
2055 2125
  /// \brief Section reader class
2056 2126
  ///
2057 2127
  /// In the \ref lgf-format "LGF" file extra sections can be placed,
2058 2128
  /// which contain any data in arbitrary format. Such sections can be
2059 2129
  /// read with this class. A reading rule can be added to the class
2060 2130
  /// with two different functions. With the \c sectionLines() function a
2061 2131
  /// functor can process the section line-by-line, while with the \c
2062 2132
  /// sectionStream() member the section can be read from an input
2063 2133
  /// stream.
2064 2134
  class SectionReader {
2065 2135
  private:
2066 2136

	
2067 2137
    std::istream* _is;
2068 2138
    bool local_is;
2069 2139
    std::string _filename;
2070 2140

	
2071 2141
    typedef std::map<std::string, _reader_bits::Section*> Sections;
2072 2142
    Sections _sections;
2073 2143

	
2074 2144
    int line_num;
2075 2145
    std::istringstream line;
2076 2146

	
2077 2147
  public:
2078 2148

	
2079 2149
    /// \brief Constructor
2080 2150
    ///
2081 2151
    /// Construct a section reader, which reads from the given input
2082 2152
    /// stream.
2083 2153
    SectionReader(std::istream& is)
2084 2154
      : _is(&is), local_is(false) {}
2085 2155

	
2086 2156
    /// \brief Constructor
2087 2157
    ///
2088 2158
    /// Construct a section reader, which reads from the given file.
2089 2159
    SectionReader(const std::string& fn)
2090 2160
      : _is(new std::ifstream(fn.c_str())), local_is(true),
2091 2161
        _filename(fn) {
2092 2162
      if (!(*_is)) {
2093 2163
        delete _is;
2094 2164
        throw IoError("Cannot open file", fn);
2095 2165
      }
2096 2166
    }
2097 2167

	
2098 2168
    /// \brief Constructor
2099 2169
    ///
2100 2170
    /// Construct a section reader, which reads from the given file.
2101 2171
    SectionReader(const char* fn)
2102 2172
      : _is(new std::ifstream(fn)), local_is(true),
2103 2173
        _filename(fn) {
2104 2174
      if (!(*_is)) {
2105 2175
        delete _is;
2106 2176
        throw IoError("Cannot open file", fn);
2107 2177
      }
2108 2178
    }
2109 2179

	
2110 2180
    /// \brief Destructor
2111 2181
    ~SectionReader() {
2112 2182
      for (Sections::iterator it = _sections.begin();
2113 2183
           it != _sections.end(); ++it) {
2114 2184
        delete it->second;
2115 2185
      }
2116 2186

	
2117 2187
      if (local_is) {
2118 2188
        delete _is;
2119 2189
      }
2120 2190

	
2121 2191
    }
2122 2192

	
2123 2193
  private:
2124 2194

	
2125 2195
    friend SectionReader sectionReader(std::istream& is);
2126 2196
    friend SectionReader sectionReader(const std::string& fn);
2127 2197
    friend SectionReader sectionReader(const char* fn);
2128 2198

	
2129 2199
    SectionReader(SectionReader& other)
2130 2200
      : _is(other._is), local_is(other.local_is) {
2131 2201

	
2132 2202
      other._is = 0;
2133 2203
      other.local_is = false;
2134 2204

	
2135 2205
      _sections.swap(other._sections);
2136 2206
    }
2137 2207

	
2138 2208
    SectionReader& operator=(const SectionReader&);
2139 2209

	
2140 2210
  public:
2141 2211

	
2142
    /// \name Section readers
2212
    /// \name Section Readers
2143 2213
    /// @{
2144 2214

	
2145 2215
    /// \brief Add a section processor with line oriented reading
2146 2216
    ///
2147 2217
    /// The first parameter is the type descriptor of the section, the
2148 2218
    /// second is a functor, which takes just one \c std::string
2149 2219
    /// parameter. At the reading process, each line of the section
2150 2220
    /// will be given to the functor object. However, the empty lines
2151 2221
    /// and the comment lines are filtered out, and the leading
2152 2222
    /// whitespaces are trimmed from each processed string.
2153 2223
    ///
2154 2224
    /// For example let's see a section, which contain several
2155 2225
    /// integers, which should be inserted into a vector.
2156 2226
    ///\code
2157 2227
    ///  @numbers
2158 2228
    ///  12 45 23
2159 2229
    ///  4
2160 2230
    ///  23 6
2161 2231
    ///\endcode
2162 2232
    ///
2163 2233
    /// The functor is implemented as a struct:
2164 2234
    ///\code
2165 2235
    ///  struct NumberSection {
2166 2236
    ///    std::vector<int>& _data;
2167 2237
    ///    NumberSection(std::vector<int>& data) : _data(data) {}
2168 2238
    ///    void operator()(const std::string& line) {
2169 2239
    ///      std::istringstream ls(line);
2170 2240
    ///      int value;
2171 2241
    ///      while (ls >> value) _data.push_back(value);
2172 2242
    ///    }
2173 2243
    ///  };
2174 2244
    ///
2175 2245
    ///  // ...
2176 2246
    ///
2177 2247
    ///  reader.sectionLines("numbers", NumberSection(vec));
2178 2248
    ///\endcode
2179 2249
    template <typename Functor>
2180 2250
    SectionReader& sectionLines(const std::string& type, Functor functor) {
2181 2251
      LEMON_ASSERT(!type.empty(), "Type is empty.");
2182 2252
      LEMON_ASSERT(_sections.find(type) == _sections.end(),
2183 2253
                   "Multiple reading of section.");
2184 2254
      _sections.insert(std::make_pair(type,
2185 2255
        new _reader_bits::LineSection<Functor>(functor)));
2186 2256
      return *this;
2187 2257
    }
2188 2258

	
2189 2259

	
2190 2260
    /// \brief Add a section processor with stream oriented reading
2191 2261
    ///
2192 2262
    /// The first parameter is the type of the section, the second is
2193 2263
    /// a functor, which takes an \c std::istream& and an \c int&
2194 2264
    /// parameter, the latter regard to the line number of stream. The
2195 2265
    /// functor can read the input while the section go on, and the
2196 2266
    /// line number should be modified accordingly.
2197 2267
    template <typename Functor>
2198 2268
    SectionReader& sectionStream(const std::string& type, Functor functor) {
2199 2269
      LEMON_ASSERT(!type.empty(), "Type is empty.");
2200 2270
      LEMON_ASSERT(_sections.find(type) == _sections.end(),
2201 2271
                   "Multiple reading of section.");
2202 2272
      _sections.insert(std::make_pair(type,
2203 2273
         new _reader_bits::StreamSection<Functor>(functor)));
2204 2274
      return *this;
2205 2275
    }
2206 2276

	
2207 2277
    /// @}
2208 2278

	
2209 2279
  private:
2210 2280

	
2211 2281
    bool readLine() {
2212 2282
      std::string str;
2213 2283
      while(++line_num, std::getline(*_is, str)) {
2214 2284
        line.clear(); line.str(str);
2215 2285
        char c;
2216 2286
        if (line >> std::ws >> c && c != '#') {
2217 2287
          line.putback(c);
2218 2288
          return true;
2219 2289
        }
2220 2290
      }
2221 2291
      return false;
2222 2292
    }
2223 2293

	
2224 2294
    bool readSuccess() {
2225 2295
      return static_cast<bool>(*_is);
2226 2296
    }
2227 2297

	
2228 2298
    void skipSection() {
2229 2299
      char c;
2230 2300
      while (readSuccess() && line >> c && c != '@') {
2231 2301
        readLine();
2232 2302
      }
2233 2303
      if (readSuccess()) {
2234 2304
        line.putback(c);
2235 2305
      }
2236 2306
    }
2237 2307

	
2238 2308
  public:
2239 2309

	
2240 2310

	
2241
    /// \name Execution of the reader
2311
    /// \name Execution of the Reader
2242 2312
    /// @{
2243 2313

	
2244 2314
    /// \brief Start the batch processing
2245 2315
    ///
2246 2316
    /// This function starts the batch processing.
2247 2317
    void run() {
2248 2318

	
2249 2319
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
2250 2320

	
2251 2321
      std::set<std::string> extra_sections;
2252 2322

	
2253 2323
      line_num = 0;
2254 2324
      readLine();
2255 2325
      skipSection();
2256 2326

	
2257 2327
      while (readSuccess()) {
2258 2328
        try {
2259 2329
          char c;
2260 2330
          std::string section, caption;
2261 2331
          line >> c;
2262 2332
          _reader_bits::readToken(line, section);
2263 2333
          _reader_bits::readToken(line, caption);
2264 2334

	
2265 2335
          if (line >> c)
2266 2336
            throw FormatError("Extra character at the end of line");
2267 2337

	
2268 2338
          if (extra_sections.find(section) != extra_sections.end()) {
2269 2339
            std::ostringstream msg;
2270 2340
            msg << "Multiple occurence of section: " << section;
2271 2341
            throw FormatError(msg.str());
2272 2342
          }
2273 2343
          Sections::iterator it = _sections.find(section);
2274 2344
          if (it != _sections.end()) {
2275 2345
            extra_sections.insert(section);
2276 2346
            it->second->process(*_is, line_num);
2277 2347
          }
2278 2348
          readLine();
2279 2349
          skipSection();
2280 2350
        } catch (FormatError& error) {
2281 2351
          error.line(line_num);
2282 2352
          error.file(_filename);
2283 2353
          throw;
2284 2354
        }
2285 2355
      }
2286 2356
      for (Sections::iterator it = _sections.begin();
2287 2357
           it != _sections.end(); ++it) {
2288 2358
        if (extra_sections.find(it->first) == extra_sections.end()) {
2289 2359
          std::ostringstream os;
2290 2360
          os << "Cannot find section: " << it->first;
2291 2361
          throw FormatError(os.str());
2292 2362
        }
2293 2363
      }
2294 2364
    }
2295 2365

	
2296 2366
    /// @}
2297 2367

	
2298 2368
  };
2299 2369

	
2370
  /// \ingroup lemon_io
2371
  ///
2372
  /// \brief Return a \ref SectionReader class
2373
  ///
2374
  /// This function just returns a \ref SectionReader class.
2375
  ///
2376
  /// Please see SectionReader documentation about the custom section
2377
  /// input.
2378
  ///
2379
  /// \relates SectionReader
2380
  /// \sa sectionReader(const std::string& fn)
2381
  /// \sa sectionReader(const char *fn)
2382
  inline SectionReader sectionReader(std::istream& is) {
2383
    SectionReader tmp(is);
2384
    return tmp;
2385
  }
2386

	
2300 2387
  /// \brief Return a \ref SectionReader class
2301 2388
  ///
2302 2389
  /// This function just returns a \ref SectionReader class.
2303 2390
  /// \relates SectionReader
2304
  inline SectionReader sectionReader(std::istream& is) {
2305
    SectionReader tmp(is);
2391
  /// \sa sectionReader(std::istream& is)
2392
  inline SectionReader sectionReader(const std::string& fn) {
2393
    SectionReader tmp(fn);
2306 2394
    return tmp;
2307 2395
  }
2308 2396

	
2309 2397
  /// \brief Return a \ref SectionReader class
2310 2398
  ///
2311 2399
  /// This function just returns a \ref SectionReader class.
2312 2400
  /// \relates SectionReader
2313
  inline SectionReader sectionReader(const std::string& fn) {
2314
    SectionReader tmp(fn);
2315
    return tmp;
2316
  }
2317

	
2318
  /// \brief Return a \ref SectionReader class
2319
  ///
2320
  /// This function just returns a \ref SectionReader class.
2321
  /// \relates SectionReader
2401
  /// \sa sectionReader(std::istream& is)
2322 2402
  inline SectionReader sectionReader(const char* fn) {
2323 2403
    SectionReader tmp(fn);
2324 2404
    return tmp;
2325 2405
  }
2326 2406

	
2327 2407
  /// \ingroup lemon_io
2328 2408
  ///
2329 2409
  /// \brief Reader for the contents of the \ref lgf-format "LGF" file
2330 2410
  ///
2331 2411
  /// This class can be used to read the sections, the map names and
2332 2412
  /// the attributes from a file. Usually, the LEMON programs know
2333 2413
  /// that, which type of graph, which maps and which attributes
2334 2414
  /// should be read from a file, but in general tools (like glemon)
2335 2415
  /// the contents of an LGF file should be guessed somehow. This class
2336 2416
  /// reads the graph and stores the appropriate information for
2337 2417
  /// reading the graph.
2338 2418
  ///
2339 2419
  ///\code
2340 2420
  /// LgfContents contents("graph.lgf");
2341 2421
  /// contents.run();
2342 2422
  ///
2343 2423
  /// // Does it contain any node section and arc section?
2344 2424
  /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
2345 2425
  ///   std::cerr << "Failure, cannot find graph." << std::endl;
2346 2426
  ///   return -1;
2347 2427
  /// }
2348 2428
  /// std::cout << "The name of the default node section: "
2349 2429
  ///           << contents.nodeSection(0) << std::endl;
2350 2430
  /// std::cout << "The number of the arc maps: "
2351 2431
  ///           << contents.arcMaps(0).size() << std::endl;
2352 2432
  /// std::cout << "The name of second arc map: "
2353 2433
  ///           << contents.arcMaps(0)[1] << std::endl;
2354 2434
  ///\endcode
2355 2435
  class LgfContents {
2356 2436
  private:
2357 2437

	
2358 2438
    std::istream* _is;
2359 2439
    bool local_is;
2360 2440

	
2361 2441
    std::vector<std::string> _node_sections;
2362 2442
    std::vector<std::string> _edge_sections;
2363 2443
    std::vector<std::string> _attribute_sections;
2364 2444
    std::vector<std::string> _extra_sections;
2365 2445

	
2366 2446
    std::vector<bool> _arc_sections;
2367 2447

	
2368 2448
    std::vector<std::vector<std::string> > _node_maps;
2369 2449
    std::vector<std::vector<std::string> > _edge_maps;
2370 2450

	
2371 2451
    std::vector<std::vector<std::string> > _attributes;
2372 2452

	
2373 2453

	
2374 2454
    int line_num;
2375 2455
    std::istringstream line;
2376 2456

	
2377 2457
  public:
2378 2458

	
2379 2459
    /// \brief Constructor
2380 2460
    ///
2381 2461
    /// Construct an \e LGF contents reader, which reads from the given
2382 2462
    /// input stream.
2383 2463
    LgfContents(std::istream& is)
2384 2464
      : _is(&is), local_is(false) {}
2385 2465

	
2386 2466
    /// \brief Constructor
2387 2467
    ///
2388 2468
    /// Construct an \e LGF contents reader, which reads from the given
2389 2469
    /// file.
2390 2470
    LgfContents(const std::string& fn)
2391 2471
      : _is(new std::ifstream(fn.c_str())), local_is(true) {
2392 2472
      if (!(*_is)) {
2393 2473
        delete _is;
2394 2474
        throw IoError("Cannot open file", fn);
2395 2475
      }
2396 2476
    }
2397 2477

	
2398 2478
    /// \brief Constructor
2399 2479
    ///
2400 2480
    /// Construct an \e LGF contents reader, which reads from the given
2401 2481
    /// file.
2402 2482
    LgfContents(const char* fn)
2403 2483
      : _is(new std::ifstream(fn)), local_is(true) {
2404 2484
      if (!(*_is)) {
2405 2485
        delete _is;
2406 2486
        throw IoError("Cannot open file", fn);
2407 2487
      }
2408 2488
    }
2409 2489

	
2410 2490
    /// \brief Destructor
2411 2491
    ~LgfContents() {
2412 2492
      if (local_is) delete _is;
2413 2493
    }
2414 2494

	
2415 2495
  private:
2416 2496

	
2417 2497
    LgfContents(const LgfContents&);
2418 2498
    LgfContents& operator=(const LgfContents&);
2419 2499

	
2420 2500
  public:
2421 2501

	
2422 2502

	
2423
    /// \name Node sections
2503
    /// \name Node Sections
2424 2504
    /// @{
2425 2505

	
2426 2506
    /// \brief Gives back the number of node sections in the file.
2427 2507
    ///
2428 2508
    /// Gives back the number of node sections in the file.
2429 2509
    int nodeSectionNum() const {
2430 2510
      return _node_sections.size();
2431 2511
    }
2432 2512

	
2433 2513
    /// \brief Returns the node section name at the given position.
2434 2514
    ///
2435 2515
    /// Returns the node section name at the given position.
2436 2516
    const std::string& nodeSection(int i) const {
2437 2517
      return _node_sections[i];
2438 2518
    }
2439 2519

	
2440 2520
    /// \brief Gives back the node maps for the given section.
2441 2521
    ///
2442 2522
    /// Gives back the node maps for the given section.
2443 2523
    const std::vector<std::string>& nodeMapNames(int i) const {
2444 2524
      return _node_maps[i];
2445 2525
    }
2446 2526

	
2447 2527
    /// @}
2448 2528

	
2449
    /// \name Arc/Edge sections
2529
    /// \name Arc/Edge Sections
2450 2530
    /// @{
2451 2531

	
2452 2532
    /// \brief Gives back the number of arc/edge sections in the file.
2453 2533
    ///
2454 2534
    /// Gives back the number of arc/edge sections in the file.
2455 2535
    /// \note It is synonym of \c edgeSectionNum().
2456 2536
    int arcSectionNum() const {
2457 2537
      return _edge_sections.size();
2458 2538
    }
2459 2539

	
2460 2540
    /// \brief Returns the arc/edge section name at the given position.
2461 2541
    ///
2462 2542
    /// Returns the arc/edge section name at the given position.
2463 2543
    /// \note It is synonym of \c edgeSection().
2464 2544
    const std::string& arcSection(int i) const {
2465 2545
      return _edge_sections[i];
2466 2546
    }
2467 2547

	
2468 2548
    /// \brief Gives back the arc/edge maps for the given section.
2469 2549
    ///
2470 2550
    /// Gives back the arc/edge maps for the given section.
2471 2551
    /// \note It is synonym of \c edgeMapNames().
2472 2552
    const std::vector<std::string>& arcMapNames(int i) const {
2473 2553
      return _edge_maps[i];
2474 2554
    }
2475 2555

	
2476 2556
    /// @}
2477 2557

	
2478 2558
    /// \name Synonyms
2479 2559
    /// @{
2480 2560

	
2481 2561
    /// \brief Gives back the number of arc/edge sections in the file.
2482 2562
    ///
2483 2563
    /// Gives back the number of arc/edge sections in the file.
2484 2564
    /// \note It is synonym of \c arcSectionNum().
2485 2565
    int edgeSectionNum() const {
2486 2566
      return _edge_sections.size();
2487 2567
    }
2488 2568

	
2489 2569
    /// \brief Returns the section name at the given position.
2490 2570
    ///
2491 2571
    /// Returns the section name at the given position.
2492 2572
    /// \note It is synonym of \c arcSection().
2493 2573
    const std::string& edgeSection(int i) const {
2494 2574
      return _edge_sections[i];
2495 2575
    }
2496 2576

	
2497 2577
    /// \brief Gives back the edge maps for the given section.
2498 2578
    ///
2499 2579
    /// Gives back the edge maps for the given section.
2500 2580
    /// \note It is synonym of \c arcMapNames().
2501 2581
    const std::vector<std::string>& edgeMapNames(int i) const {
2502 2582
      return _edge_maps[i];
2503 2583
    }
2504 2584

	
2505 2585
    /// @}
2506 2586

	
2507
    /// \name Attribute sections
2587
    /// \name Attribute Sections
2508 2588
    /// @{
2509 2589

	
2510 2590
    /// \brief Gives back the number of attribute sections in the file.
2511 2591
    ///
2512 2592
    /// Gives back the number of attribute sections in the file.
2513 2593
    int attributeSectionNum() const {
2514 2594
      return _attribute_sections.size();
2515 2595
    }
2516 2596

	
2517 2597
    /// \brief Returns the attribute section name at the given position.
2518 2598
    ///
2519 2599
    /// Returns the attribute section name at the given position.
2520 2600
    const std::string& attributeSectionNames(int i) const {
2521 2601
      return _attribute_sections[i];
2522 2602
    }
2523 2603

	
2524 2604
    /// \brief Gives back the attributes for the given section.
2525 2605
    ///
2526 2606
    /// Gives back the attributes for the given section.
2527 2607
    const std::vector<std::string>& attributes(int i) const {
2528 2608
      return _attributes[i];
2529 2609
    }
2530 2610

	
2531 2611
    /// @}
2532 2612

	
2533
    /// \name Extra sections
2613
    /// \name Extra Sections
2534 2614
    /// @{
2535 2615

	
2536 2616
    /// \brief Gives back the number of extra sections in the file.
2537 2617
    ///
2538 2618
    /// Gives back the number of extra sections in the file.
2539 2619
    int extraSectionNum() const {
2540 2620
      return _extra_sections.size();
2541 2621
    }
2542 2622

	
2543 2623
    /// \brief Returns the extra section type at the given position.
2544 2624
    ///
2545 2625
    /// Returns the section type at the given position.
2546 2626
    const std::string& extraSection(int i) const {
2547 2627
      return _extra_sections[i];
2548 2628
    }
2549 2629

	
2550 2630
    /// @}
2551 2631

	
2552 2632
  private:
2553 2633

	
2554 2634
    bool readLine() {
2555 2635
      std::string str;
2556 2636
      while(++line_num, std::getline(*_is, str)) {
2557 2637
        line.clear(); line.str(str);
2558 2638
        char c;
2559 2639
        if (line >> std::ws >> c && c != '#') {
2560 2640
          line.putback(c);
2561 2641
          return true;
2562 2642
        }
2563 2643
      }
2564 2644
      return false;
2565 2645
    }
2566 2646

	
2567 2647
    bool readSuccess() {
2568 2648
      return static_cast<bool>(*_is);
2569 2649
    }
2570 2650

	
2571 2651
    void skipSection() {
2572 2652
      char c;
2573 2653
      while (readSuccess() && line >> c && c != '@') {
2574 2654
        readLine();
2575 2655
      }
2576 2656
      if (readSuccess()) {
2577 2657
        line.putback(c);
2578 2658
      }
2579 2659
    }
2580 2660

	
2581 2661
    void readMaps(std::vector<std::string>& maps) {
2582 2662
      char c;
2583 2663
      if (!readLine() || !(line >> c) || c == '@') {
2584 2664
        if (readSuccess() && line) line.putback(c);
2585 2665
        return;
2586 2666
      }
2587 2667
      line.putback(c);
2588 2668
      std::string map;
2589 2669
      while (_reader_bits::readToken(line, map)) {
2590 2670
        maps.push_back(map);
2591 2671
      }
2592 2672
    }
2593 2673

	
2594 2674
    void readAttributes(std::vector<std::string>& attrs) {
2595 2675
      readLine();
2596 2676
      char c;
2597 2677
      while (readSuccess() && line >> c && c != '@') {
2598 2678
        line.putback(c);
2599 2679
        std::string attr;
2600 2680
        _reader_bits::readToken(line, attr);
2601 2681
        attrs.push_back(attr);
2602 2682
        readLine();
2603 2683
      }
2604 2684
      line.putback(c);
2605 2685
    }
2606 2686

	
2607 2687
  public:
2608 2688

	
2609
    /// \name Execution of the contents reader
2689
    /// \name Execution of the Contents Reader
2610 2690
    /// @{
2611 2691

	
2612 2692
    /// \brief Starts the reading
2613 2693
    ///
2614 2694
    /// This function starts the reading.
2615 2695
    void run() {
2616 2696

	
2617 2697
      readLine();
2618 2698
      skipSection();
2619 2699

	
2620 2700
      while (readSuccess()) {
2621 2701

	
2622 2702
        char c;
2623 2703
        line >> c;
2624 2704

	
2625 2705
        std::string section, caption;
2626 2706
        _reader_bits::readToken(line, section);
2627 2707
        _reader_bits::readToken(line, caption);
2628 2708

	
2629 2709
        if (section == "nodes") {
2630 2710
          _node_sections.push_back(caption);
2631 2711
          _node_maps.push_back(std::vector<std::string>());
2632 2712
          readMaps(_node_maps.back());
2633 2713
          readLine(); skipSection();
2634 2714
        } else if (section == "arcs" || section == "edges") {
2635 2715
          _edge_sections.push_back(caption);
2636 2716
          _arc_sections.push_back(section == "arcs");
2637 2717
          _edge_maps.push_back(std::vector<std::string>());
2638 2718
          readMaps(_edge_maps.back());
2639 2719
          readLine(); skipSection();
2640 2720
        } else if (section == "attributes") {
2641 2721
          _attribute_sections.push_back(caption);
2642 2722
          _attributes.push_back(std::vector<std::string>());
2643 2723
          readAttributes(_attributes.back());
2644 2724
        } else {
2645 2725
          _extra_sections.push_back(section);
2646 2726
          readLine(); skipSection();
2647 2727
        }
2648 2728
      }
2649 2729
    }
2650 2730

	
2651 2731
    /// @}
2652 2732

	
2653 2733
  };
2654 2734
}
2655 2735

	
2656 2736
#endif
Ignore white space 6 line context
... ...
@@ -254,1498 +254,1582 @@
254 254
        return;
255 255
      case '\f':
256 256
        os << "\\f";
257 257
        return;
258 258
      case '\r':
259 259
        os << "\\r";
260 260
        return;
261 261
      case '\n':
262 262
        os << "\\n";
263 263
        return;
264 264
      case '\t':
265 265
        os << "\\t";
266 266
        return;
267 267
      case '\v':
268 268
        os << "\\v";
269 269
        return;
270 270
      default:
271 271
        if (c < 0x20) {
272 272
          std::ios::fmtflags flags = os.flags();
273 273
          os << '\\' << std::oct << static_cast<int>(c);
274 274
          os.flags(flags);
275 275
        } else {
276 276
          os << c;
277 277
        }
278 278
        return;
279 279
      }
280 280
    }
281 281

	
282 282
    inline bool requireEscape(const std::string& str) {
283 283
      if (str.empty() || str[0] == '@') return true;
284 284
      std::istringstream is(str);
285 285
      char c;
286 286
      while (is.get(c)) {
287 287
        if (isWhiteSpace(c) || isEscaped(c)) {
288 288
          return true;
289 289
        }
290 290
      }
291 291
      return false;
292 292
    }
293 293

	
294 294
    inline std::ostream& writeToken(std::ostream& os, const std::string& str) {
295 295

	
296 296
      if (requireEscape(str)) {
297 297
        os << '\"';
298 298
        for (std::string::const_iterator it = str.begin();
299 299
             it != str.end(); ++it) {
300 300
          writeEscape(os, *it);
301 301
        }
302 302
        os << '\"';
303 303
      } else {
304 304
        os << str;
305 305
      }
306 306
      return os;
307 307
    }
308 308

	
309 309
    class Section {
310 310
    public:
311 311
      virtual ~Section() {}
312 312
      virtual void process(std::ostream& os) = 0;
313 313
    };
314 314

	
315 315
    template <typename Functor>
316 316
    class LineSection : public Section {
317 317
    private:
318 318

	
319 319
      Functor _functor;
320 320

	
321 321
    public:
322 322

	
323 323
      LineSection(const Functor& functor) : _functor(functor) {}
324 324
      virtual ~LineSection() {}
325 325

	
326 326
      virtual void process(std::ostream& os) {
327 327
        std::string line;
328 328
        while (!(line = _functor()).empty()) os << line << std::endl;
329 329
      }
330 330
    };
331 331

	
332 332
    template <typename Functor>
333 333
    class StreamSection : public Section {
334 334
    private:
335 335

	
336 336
      Functor _functor;
337 337

	
338 338
    public:
339 339

	
340 340
      StreamSection(const Functor& functor) : _functor(functor) {}
341 341
      virtual ~StreamSection() {}
342 342

	
343 343
      virtual void process(std::ostream& os) {
344 344
        _functor(os);
345 345
      }
346 346
    };
347 347

	
348 348
  }
349 349

	
350
  template <typename Digraph>
350
  template <typename DGR>
351 351
  class DigraphWriter;
352 352

	
353
  /// \brief Return a \ref DigraphWriter class
354
  ///
355
  /// This function just returns a \ref DigraphWriter class.
356
  /// \relates DigraphWriter
357
  template <typename Digraph>
358
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
359
                                       std::ostream& os = std::cout) {
360
    DigraphWriter<Digraph> tmp(digraph, os);
361
    return tmp;
362
  }
353
  template <typename TDGR>
354
  DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, 
355
                                   std::ostream& os = std::cout);
356
  template <typename TDGR>
357
  DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, const std::string& fn);
363 358

	
364
  /// \brief Return a \ref DigraphWriter class
365
  ///
366
  /// This function just returns a \ref DigraphWriter class.
367
  /// \relates DigraphWriter
368
  template <typename Digraph>
369
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
370
                                       const std::string& fn) {
371
    DigraphWriter<Digraph> tmp(digraph, fn);
372
    return tmp;
373
  }
359
  template <typename TDGR>
360
  DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, const char* fn);
374 361

	
375
  /// \brief Return a \ref DigraphWriter class
376
  ///
377
  /// This function just returns a \ref DigraphWriter class.
378
  /// \relates DigraphWriter
379
  template <typename Digraph>
380
  DigraphWriter<Digraph> digraphWriter(const Digraph& digraph,
381
                                       const char* fn) {
382
    DigraphWriter<Digraph> tmp(digraph, fn);
383
    return tmp;
384
  }
385 362

	
386 363
  /// \ingroup lemon_io
387 364
  ///
388 365
  /// \brief \ref lgf-format "LGF" writer for directed graphs
389 366
  ///
390 367
  /// This utility writes an \ref lgf-format "LGF" file.
391 368
  ///
392 369
  /// The writing method does a batch processing. The user creates a
393 370
  /// writer object, then various writing rules can be added to the
394 371
  /// writer, and eventually the writing is executed with the \c run()
395 372
  /// member function. A map writing rule can be added to the writer
396 373
  /// with the \c nodeMap() or \c arcMap() members. An optional
397 374
  /// converter parameter can also be added as a standard functor
398 375
  /// converting from the value type of the map to \c std::string. If it
399 376
  /// is set, it will determine how the value type of the map is written to
400 377
  /// the output stream. If the functor is not set, then a default
401 378
  /// conversion will be used. The \c attribute(), \c node() and \c
402 379
  /// arc() functions are used to add attribute writing rules.
403 380
  ///
404 381
  ///\code
405
  /// DigraphWriter<Digraph>(digraph, std::cout).
382
  /// DigraphWriter<DGR>(digraph, std::cout).
406 383
  ///   nodeMap("coordinates", coord_map).
407 384
  ///   nodeMap("size", size).
408 385
  ///   nodeMap("title", title).
409 386
  ///   arcMap("capacity", cap_map).
410 387
  ///   node("source", src).
411 388
  ///   node("target", trg).
412 389
  ///   attribute("caption", caption).
413 390
  ///   run();
414 391
  ///\endcode
415 392
  ///
416 393
  ///
417 394
  /// By default, the writer does not write additional captions to the
418 395
  /// sections, but they can be give as an optional parameter of
419 396
  /// the \c nodes(), \c arcs() or \c
420 397
  /// attributes() functions.
421 398
  ///
422 399
  /// The \c skipNodes() and \c skipArcs() functions forbid the
423 400
  /// writing of the sections. If two arc sections should be written
424 401
  /// to the output, it can be done in two passes, the first pass
425 402
  /// writes the node section and the first arc section, then the
426 403
  /// second pass skips the node section and writes just the arc
427 404
  /// section to the stream. The output stream can be retrieved with
428 405
  /// the \c ostream() function, hence the second pass can append its
429 406
  /// output to the output of the first pass.
430
  template <typename _Digraph>
407
  template <typename DGR>
431 408
  class DigraphWriter {
432 409
  public:
433 410

	
434
    typedef _Digraph Digraph;
435
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
411
    typedef DGR Digraph;
412
    TEMPLATE_DIGRAPH_TYPEDEFS(DGR);
436 413

	
437 414
  private:
438 415

	
439 416

	
440 417
    std::ostream* _os;
441 418
    bool local_os;
442 419

	
443
    const Digraph& _digraph;
420
    const DGR& _digraph;
444 421

	
445 422
    std::string _nodes_caption;
446 423
    std::string _arcs_caption;
447 424
    std::string _attributes_caption;
448 425

	
449 426
    typedef std::map<Node, std::string> NodeIndex;
450 427
    NodeIndex _node_index;
451 428
    typedef std::map<Arc, std::string> ArcIndex;
452 429
    ArcIndex _arc_index;
453 430

	
454 431
    typedef std::vector<std::pair<std::string,
455 432
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;
456 433
    NodeMaps _node_maps;
457 434

	
458 435
    typedef std::vector<std::pair<std::string,
459 436
      _writer_bits::MapStorageBase<Arc>* > >ArcMaps;
460 437
    ArcMaps _arc_maps;
461 438

	
462 439
    typedef std::vector<std::pair<std::string,
463 440
      _writer_bits::ValueStorageBase*> > Attributes;
464 441
    Attributes _attributes;
465 442

	
466 443
    bool _skip_nodes;
467 444
    bool _skip_arcs;
468 445

	
469 446
  public:
470 447

	
471 448
    /// \brief Constructor
472 449
    ///
473 450
    /// Construct a directed graph writer, which writes to the given
474 451
    /// output stream.
475
    DigraphWriter(const Digraph& digraph, std::ostream& os = std::cout)
452
    DigraphWriter(const DGR& digraph, std::ostream& os = std::cout)
476 453
      : _os(&os), local_os(false), _digraph(digraph),
477 454
        _skip_nodes(false), _skip_arcs(false) {}
478 455

	
479 456
    /// \brief Constructor
480 457
    ///
481 458
    /// Construct a directed graph writer, which writes to the given
482 459
    /// output file.
483
    DigraphWriter(const Digraph& digraph, const std::string& fn)
460
    DigraphWriter(const DGR& digraph, const std::string& fn)
484 461
      : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
485 462
        _skip_nodes(false), _skip_arcs(false) {
486 463
      if (!(*_os)) {
487 464
        delete _os;
488 465
        throw IoError("Cannot write file", fn);
489 466
      }
490 467
    }
491 468

	
492 469
    /// \brief Constructor
493 470
    ///
494 471
    /// Construct a directed graph writer, which writes to the given
495 472
    /// output file.
496
    DigraphWriter(const Digraph& digraph, const char* fn)
473
    DigraphWriter(const DGR& digraph, const char* fn)
497 474
      : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
498 475
        _skip_nodes(false), _skip_arcs(false) {
499 476
      if (!(*_os)) {
500 477
        delete _os;
501 478
        throw IoError("Cannot write file", fn);
502 479
      }
503 480
    }
504 481

	
505 482
    /// \brief Destructor
506 483
    ~DigraphWriter() {
507 484
      for (typename NodeMaps::iterator it = _node_maps.begin();
508 485
           it != _node_maps.end(); ++it) {
509 486
        delete it->second;
510 487
      }
511 488

	
512 489
      for (typename ArcMaps::iterator it = _arc_maps.begin();
513 490
           it != _arc_maps.end(); ++it) {
514 491
        delete it->second;
515 492
      }
516 493

	
517 494
      for (typename Attributes::iterator it = _attributes.begin();
518 495
           it != _attributes.end(); ++it) {
519 496
        delete it->second;
520 497
      }
521 498

	
522 499
      if (local_os) {
523 500
        delete _os;
524 501
      }
525 502
    }
526 503

	
527 504
  private:
528 505

	
529
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
530
                                                  std::ostream& os);
531
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
532
                                                  const std::string& fn);
533
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
534
                                                  const char *fn);
506
    template <typename TDGR>
507
    friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, 
508
                                             std::ostream& os);
509
    template <typename TDGR>
510
    friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph,
511
                                             const std::string& fn);
512
    template <typename TDGR>
513
    friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph,
514
                                             const char *fn);
535 515

	
536 516
    DigraphWriter(DigraphWriter& other)
537 517
      : _os(other._os), local_os(other.local_os), _digraph(other._digraph),
538 518
        _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
539 519

	
540 520
      other._os = 0;
541 521
      other.local_os = false;
542 522

	
543 523
      _node_index.swap(other._node_index);
544 524
      _arc_index.swap(other._arc_index);
545 525

	
546 526
      _node_maps.swap(other._node_maps);
547 527
      _arc_maps.swap(other._arc_maps);
548 528
      _attributes.swap(other._attributes);
549 529

	
550 530
      _nodes_caption = other._nodes_caption;
551 531
      _arcs_caption = other._arcs_caption;
552 532
      _attributes_caption = other._attributes_caption;
553 533
    }
554 534

	
555 535
    DigraphWriter& operator=(const DigraphWriter&);
556 536

	
557 537
  public:
558 538

	
559
    /// \name Writing rules
539
    /// \name Writing Rules
560 540
    /// @{
561 541

	
562 542
    /// \brief Node map writing rule
563 543
    ///
564 544
    /// Add a node map writing rule to the writer.
565 545
    template <typename Map>
566 546
    DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
567 547
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
568 548
      _writer_bits::MapStorageBase<Node>* storage =
569 549
        new _writer_bits::MapStorage<Node, Map>(map);
570 550
      _node_maps.push_back(std::make_pair(caption, storage));
571 551
      return *this;
572 552
    }
573 553

	
574 554
    /// \brief Node map writing rule
575 555
    ///
576 556
    /// Add a node map writing rule with specialized converter to the
577 557
    /// writer.
578 558
    template <typename Map, typename Converter>
579 559
    DigraphWriter& nodeMap(const std::string& caption, const Map& map,
580 560
                           const Converter& converter = Converter()) {
581 561
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
582 562
      _writer_bits::MapStorageBase<Node>* storage =
583 563
        new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
584 564
      _node_maps.push_back(std::make_pair(caption, storage));
585 565
      return *this;
586 566
    }
587 567

	
588 568
    /// \brief Arc map writing rule
589 569
    ///
590 570
    /// Add an arc map writing rule to the writer.
591 571
    template <typename Map>
592 572
    DigraphWriter& arcMap(const std::string& caption, const Map& map) {
593 573
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
594 574
      _writer_bits::MapStorageBase<Arc>* storage =
595 575
        new _writer_bits::MapStorage<Arc, Map>(map);
596 576
      _arc_maps.push_back(std::make_pair(caption, storage));
597 577
      return *this;
598 578
    }
599 579

	
600 580
    /// \brief Arc map writing rule
601 581
    ///
602 582
    /// Add an arc map writing rule with specialized converter to the
603 583
    /// writer.
604 584
    template <typename Map, typename Converter>
605 585
    DigraphWriter& arcMap(const std::string& caption, const Map& map,
606 586
                          const Converter& converter = Converter()) {
607 587
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
608 588
      _writer_bits::MapStorageBase<Arc>* storage =
609 589
        new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter);
610 590
      _arc_maps.push_back(std::make_pair(caption, storage));
611 591
      return *this;
612 592
    }
613 593

	
614 594
    /// \brief Attribute writing rule
615 595
    ///
616 596
    /// Add an attribute writing rule to the writer.
617 597
    template <typename Value>
618 598
    DigraphWriter& attribute(const std::string& caption, const Value& value) {
619 599
      _writer_bits::ValueStorageBase* storage =
620 600
        new _writer_bits::ValueStorage<Value>(value);
621 601
      _attributes.push_back(std::make_pair(caption, storage));
622 602
      return *this;
623 603
    }
624 604

	
625 605
    /// \brief Attribute writing rule
626 606
    ///
627 607
    /// Add an attribute writing rule with specialized converter to the
628 608
    /// writer.
629 609
    template <typename Value, typename Converter>
630 610
    DigraphWriter& attribute(const std::string& caption, const Value& value,
631 611
                             const Converter& converter = Converter()) {
632 612
      _writer_bits::ValueStorageBase* storage =
633 613
        new _writer_bits::ValueStorage<Value, Converter>(value, converter);
634 614
      _attributes.push_back(std::make_pair(caption, storage));
635 615
      return *this;
636 616
    }
637 617

	
638 618
    /// \brief Node writing rule
639 619
    ///
640 620
    /// Add a node writing rule to the writer.
641 621
    DigraphWriter& node(const std::string& caption, const Node& node) {
642 622
      typedef _writer_bits::MapLookUpConverter<Node> Converter;
643 623
      Converter converter(_node_index);
644 624
      _writer_bits::ValueStorageBase* storage =
645 625
        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
646 626
      _attributes.push_back(std::make_pair(caption, storage));
647 627
      return *this;
648 628
    }
649 629

	
650 630
    /// \brief Arc writing rule
651 631
    ///
652 632
    /// Add an arc writing rule to writer.
653 633
    DigraphWriter& arc(const std::string& caption, const Arc& arc) {
654 634
      typedef _writer_bits::MapLookUpConverter<Arc> Converter;
655 635
      Converter converter(_arc_index);
656 636
      _writer_bits::ValueStorageBase* storage =
657 637
        new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
658 638
      _attributes.push_back(std::make_pair(caption, storage));
659 639
      return *this;
660 640
    }
661 641

	
662
    /// \name Section captions
642
    /// \name Section Captions
663 643
    /// @{
664 644

	
665 645
    /// \brief Add an additional caption to the \c \@nodes section
666 646
    ///
667 647
    /// Add an additional caption to the \c \@nodes section.
668 648
    DigraphWriter& nodes(const std::string& caption) {
669 649
      _nodes_caption = caption;
670 650
      return *this;
671 651
    }
672 652

	
673 653
    /// \brief Add an additional caption to the \c \@arcs section
674 654
    ///
675 655
    /// Add an additional caption to the \c \@arcs section.
676 656
    DigraphWriter& arcs(const std::string& caption) {
677 657
      _arcs_caption = caption;
678 658
      return *this;
679 659
    }
680 660

	
681 661
    /// \brief Add an additional caption to the \c \@attributes section
682 662
    ///
683 663
    /// Add an additional caption to the \c \@attributes section.
684 664
    DigraphWriter& attributes(const std::string& caption) {
685 665
      _attributes_caption = caption;
686 666
      return *this;
687 667
    }
688 668

	
689
    /// \name Skipping section
669
    /// \name Skipping Section
690 670
    /// @{
691 671

	
692 672
    /// \brief Skip writing the node set
693 673
    ///
694 674
    /// The \c \@nodes section will not be written to the stream.
695 675
    DigraphWriter& skipNodes() {
696 676
      LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
697 677
      _skip_nodes = true;
698 678
      return *this;
699 679
    }
700 680

	
701 681
    /// \brief Skip writing arc set
702 682
    ///
703 683
    /// The \c \@arcs section will not be written to the stream.
704 684
    DigraphWriter& skipArcs() {
705 685
      LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
706 686
      _skip_arcs = true;
707 687
      return *this;
708 688
    }
709 689

	
710 690
    /// @}
711 691

	
712 692
  private:
713 693

	
714 694
    void writeNodes() {
715 695
      _writer_bits::MapStorageBase<Node>* label = 0;
716 696
      for (typename NodeMaps::iterator it = _node_maps.begin();
717 697
           it != _node_maps.end(); ++it) {
718 698
        if (it->first == "label") {
719 699
          label = it->second;
720 700
          break;
721 701
        }
722 702
      }
723 703

	
724 704
      *_os << "@nodes";
725 705
      if (!_nodes_caption.empty()) {
726 706
        _writer_bits::writeToken(*_os << ' ', _nodes_caption);
727 707
      }
728 708
      *_os << std::endl;
729 709

	
730 710
      if (label == 0) {
731 711
        *_os << "label" << '\t';
732 712
      }
733 713
      for (typename NodeMaps::iterator it = _node_maps.begin();
734 714
           it != _node_maps.end(); ++it) {
735 715
        _writer_bits::writeToken(*_os, it->first) << '\t';
736 716
      }
737 717
      *_os << std::endl;
738 718

	
739 719
      std::vector<Node> nodes;
740 720
      for (NodeIt n(_digraph); n != INVALID; ++n) {
741 721
        nodes.push_back(n);
742 722
      }
743 723

	
744 724
      if (label == 0) {
745
        IdMap<Digraph, Node> id_map(_digraph);
746
        _writer_bits::MapLess<IdMap<Digraph, Node> > id_less(id_map);
725
        IdMap<DGR, Node> id_map(_digraph);
726
        _writer_bits::MapLess<IdMap<DGR, Node> > id_less(id_map);
747 727
        std::sort(nodes.begin(), nodes.end(), id_less);
748 728
      } else {
749 729
        label->sort(nodes);
750 730
      }
751 731

	
752 732
      for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
753 733
        Node n = nodes[i];
754 734
        if (label == 0) {
755 735
          std::ostringstream os;
756 736
          os << _digraph.id(n);
757 737
          _writer_bits::writeToken(*_os, os.str());
758 738
          *_os << '\t';
759 739
          _node_index.insert(std::make_pair(n, os.str()));
760 740
        }
761 741
        for (typename NodeMaps::iterator it = _node_maps.begin();
762 742
             it != _node_maps.end(); ++it) {
763 743
          std::string value = it->second->get(n);
764 744
          _writer_bits::writeToken(*_os, value);
765 745
          if (it->first == "label") {
766 746
            _node_index.insert(std::make_pair(n, value));
767 747
          }
768 748
          *_os << '\t';
769 749
        }
770 750
        *_os << std::endl;
771 751
      }
772 752
    }
773 753

	
774 754
    void createNodeIndex() {
775 755
      _writer_bits::MapStorageBase<Node>* label = 0;
776 756
      for (typename NodeMaps::iterator it = _node_maps.begin();
777 757
           it != _node_maps.end(); ++it) {
778 758
        if (it->first == "label") {
779 759
          label = it->second;
780 760
          break;
781 761
        }
782 762
      }
783 763

	
784 764
      if (label == 0) {
785 765
        for (NodeIt n(_digraph); n != INVALID; ++n) {
786 766
          std::ostringstream os;
787 767
          os << _digraph.id(n);
788 768
          _node_index.insert(std::make_pair(n, os.str()));
789 769
        }
790 770
      } else {
791 771
        for (NodeIt n(_digraph); n != INVALID; ++n) {
792 772
          std::string value = label->get(n);
793 773
          _node_index.insert(std::make_pair(n, value));
794 774
        }
795 775
      }
796 776
    }
797 777

	
798 778
    void writeArcs() {
799 779
      _writer_bits::MapStorageBase<Arc>* label = 0;
800 780
      for (typename ArcMaps::iterator it = _arc_maps.begin();
801 781
           it != _arc_maps.end(); ++it) {
802 782
        if (it->first == "label") {
803 783
          label = it->second;
804 784
          break;
805 785
        }
806 786
      }
807 787

	
808 788
      *_os << "@arcs";
809 789
      if (!_arcs_caption.empty()) {
810 790
        _writer_bits::writeToken(*_os << ' ', _arcs_caption);
811 791
      }
812 792
      *_os << std::endl;
813 793

	
814 794
      *_os << '\t' << '\t';
815 795
      if (label == 0) {
816 796
        *_os << "label" << '\t';
817 797
      }
818 798
      for (typename ArcMaps::iterator it = _arc_maps.begin();
819 799
           it != _arc_maps.end(); ++it) {
820 800
        _writer_bits::writeToken(*_os, it->first) << '\t';
821 801
      }
822 802
      *_os << std::endl;
823 803

	
824 804
      std::vector<Arc> arcs;
825 805
      for (ArcIt n(_digraph); n != INVALID; ++n) {
826 806
        arcs.push_back(n);
827 807
      }
828 808

	
829 809
      if (label == 0) {
830
        IdMap<Digraph, Arc> id_map(_digraph);
831
        _writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map);
810
        IdMap<DGR, Arc> id_map(_digraph);
811
        _writer_bits::MapLess<IdMap<DGR, Arc> > id_less(id_map);
832 812
        std::sort(arcs.begin(), arcs.end(), id_less);
833 813
      } else {
834 814
        label->sort(arcs);
835 815
      }
836 816

	
837 817
      for (int i = 0; i < static_cast<int>(arcs.size()); ++i) {
838 818
        Arc a = arcs[i];
839 819
        _writer_bits::writeToken(*_os, _node_index.
840 820
                                 find(_digraph.source(a))->second);
841 821
        *_os << '\t';
842 822
        _writer_bits::writeToken(*_os, _node_index.
843 823
                                 find(_digraph.target(a))->second);
844 824
        *_os << '\t';
845 825
        if (label == 0) {
846 826
          std::ostringstream os;
847 827
          os << _digraph.id(a);
848 828
          _writer_bits::writeToken(*_os, os.str());
849 829
          *_os << '\t';
850 830
          _arc_index.insert(std::make_pair(a, os.str()));
851 831
        }
852 832
        for (typename ArcMaps::iterator it = _arc_maps.begin();
853 833
             it != _arc_maps.end(); ++it) {
854 834
          std::string value = it->second->get(a);
855 835
          _writer_bits::writeToken(*_os, value);
856 836
          if (it->first == "label") {
857 837
            _arc_index.insert(std::make_pair(a, value));
858 838
          }
859 839
          *_os << '\t';
860 840
        }
861 841
        *_os << std::endl;
862 842
      }
863 843
    }
864 844

	
865 845
    void createArcIndex() {
866 846
      _writer_bits::MapStorageBase<Arc>* label = 0;
867 847
      for (typename ArcMaps::iterator it = _arc_maps.begin();
868 848
           it != _arc_maps.end(); ++it) {
869 849
        if (it->first == "label") {
870 850
          label = it->second;
871 851
          break;
872 852
        }
873 853
      }
874 854

	
875 855
      if (label == 0) {
876 856
        for (ArcIt a(_digraph); a != INVALID; ++a) {
877 857
          std::ostringstream os;
878 858
          os << _digraph.id(a);
879 859
          _arc_index.insert(std::make_pair(a, os.str()));
880 860
        }
881 861
      } else {
882 862
        for (ArcIt a(_digraph); a != INVALID; ++a) {
883 863
          std::string value = label->get(a);
884 864
          _arc_index.insert(std::make_pair(a, value));
885 865
        }
886 866
      }
887 867
    }
888 868

	
889 869
    void writeAttributes() {
890 870
      if (_attributes.empty()) return;
891 871
      *_os << "@attributes";
892 872
      if (!_attributes_caption.empty()) {
893 873
        _writer_bits::writeToken(*_os << ' ', _attributes_caption);
894 874
      }
895 875
      *_os << std::endl;
896 876
      for (typename Attributes::iterator it = _attributes.begin();
897 877
           it != _attributes.end(); ++it) {
898 878
        _writer_bits::writeToken(*_os, it->first) << ' ';
899 879
        _writer_bits::writeToken(*_os, it->second->get());
900 880
        *_os << std::endl;
901 881
      }
902 882
    }
903 883

	
904 884
  public:
905 885

	
906
    /// \name Execution of the writer
886
    /// \name Execution of the Writer
907 887
    /// @{
908 888

	
909 889
    /// \brief Start the batch processing
910 890
    ///
911 891
    /// This function starts the batch processing.
912 892
    void run() {
913 893
      if (!_skip_nodes) {
914 894
        writeNodes();
915 895
      } else {
916 896
        createNodeIndex();
917 897
      }
918 898
      if (!_skip_arcs) {
919 899
        writeArcs();
920 900
      } else {
921 901
        createArcIndex();
922 902
      }
923 903
      writeAttributes();
924 904
    }
925 905

	
926 906
    /// \brief Give back the stream of the writer
927 907
    ///
928 908
    /// Give back the stream of the writer.
929 909
    std::ostream& ostream() {
930 910
      return *_os;
931 911
    }
932 912

	
933 913
    /// @}
934 914
  };
935 915

	
936
  template <typename Graph>
937
  class GraphWriter;
938

	
939
  /// \brief Return a \ref GraphWriter class
916
  /// \ingroup lemon_io
940 917
  ///
941
  /// This function just returns a \ref GraphWriter class.
942
  /// \relates GraphWriter
943
  template <typename Graph>
944
  GraphWriter<Graph> graphWriter(const Graph& graph,
945
                                 std::ostream& os = std::cout) {
946
    GraphWriter<Graph> tmp(graph, os);
918
  /// \brief Return a \ref DigraphWriter class
919
  ///
920
  /// This function just returns a \ref DigraphWriter class. 
921
  ///
922
  /// With this function a digraph can be write to a file or output
923
  /// stream in \ref lgf-format "LGF" format with several maps and
924
  /// attributes. For example, with the following code a network flow
925
  /// problem can be written to the standard output, i.e. a digraph
926
  /// with a \e capacity map on the arcs and \e source and \e target
927
  /// nodes:
928
  ///
929
  ///\code
930
  ///ListDigraph digraph;
931
  ///ListDigraph::ArcMap<int> cap(digraph);
932
  ///ListDigraph::Node src, trg;
933
  ///  // Setting the capacity map and source and target nodes
934
  ///digraphWriter(digraph, std::cout).
935
  ///  arcMap("capacity", cap).
936
  ///  node("source", src).
937
  ///  node("target", trg).
938
  ///  run();
939
  ///\endcode
940
  ///
941
  /// For a complete documentation, please see the \ref DigraphWriter
942
  /// class documentation.
943
  /// \warning Don't forget to put the \ref DigraphWriter::run() "run()"
944
  /// to the end of the parameter list.
945
  /// \relates DigraphWriter
946
  /// \sa digraphWriter(const TDGR& digraph, const std::string& fn)
947
  /// \sa digraphWriter(const TDGR& digraph, const char* fn)
948
  template <typename TDGR>
949
  DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, std::ostream& os) {
950
    DigraphWriter<TDGR> tmp(digraph, os);
947 951
    return tmp;
948 952
  }
949 953

	
950
  /// \brief Return a \ref GraphWriter class
954
  /// \brief Return a \ref DigraphWriter class
951 955
  ///
952
  /// This function just returns a \ref GraphWriter class.
953
  /// \relates GraphWriter
954
  template <typename Graph>
955
  GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn) {
956
    GraphWriter<Graph> tmp(graph, fn);
956
  /// This function just returns a \ref DigraphWriter class.
957
  /// \relates DigraphWriter
958
  /// \sa digraphWriter(const TDGR& digraph, std::ostream& os)
959
  template <typename TDGR>
960
  DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, 
961
                                    const std::string& fn) {
962
    DigraphWriter<TDGR> tmp(digraph, fn);
957 963
    return tmp;
958 964
  }
959 965

	
960
  /// \brief Return a \ref GraphWriter class
966
  /// \brief Return a \ref DigraphWriter class
961 967
  ///
962
  /// This function just returns a \ref GraphWriter class.
963
  /// \relates GraphWriter
964
  template <typename Graph>
965
  GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) {
966
    GraphWriter<Graph> tmp(graph, fn);
968
  /// This function just returns a \ref DigraphWriter class.
969
  /// \relates DigraphWriter
970
  /// \sa digraphWriter(const TDGR& digraph, std::ostream& os)
971
  template <typename TDGR>
972
  DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, const char* fn) {
973
    DigraphWriter<TDGR> tmp(digraph, fn);
967 974
    return tmp;
968 975
  }
969 976

	
977
  template <typename GR>
978
  class GraphWriter;
979

	
980
  template <typename TGR>
981
  GraphWriter<TGR> graphWriter(const TGR& graph, std::ostream& os = std::cout);
982
  template <typename TGR>
983
  GraphWriter<TGR> graphWriter(const TGR& graph, const std::string& fn);
984
  template <typename TGR>
985
  GraphWriter<TGR> graphWriter(const TGR& graph, const char* fn);
986

	
970 987
  /// \ingroup lemon_io
971 988
  ///
972 989
  /// \brief \ref lgf-format "LGF" writer for directed graphs
973 990
  ///
974 991
  /// This utility writes an \ref lgf-format "LGF" file.
975 992
  ///
976 993
  /// It can be used almost the same way as \c DigraphWriter.
977 994
  /// The only difference is that this class can handle edges and
978 995
  /// edge maps as well as arcs and arc maps.
979 996
  ///
980 997
  /// The arc maps are written into the file as two columns, the
981 998
  /// caption of the columns are the name of the map prefixed with \c
982 999
  /// '+' and \c '-'. The arcs are written into the \c \@attributes
983 1000
  /// section as a \c '+' or a \c '-' prefix (depends on the direction
984 1001
  /// of the arc) and the label of corresponding edge.
985
  template <typename _Graph>
1002
  template <typename GR>
986 1003
  class GraphWriter {
987 1004
  public:
988 1005

	
989
    typedef _Graph Graph;
990
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
1006
    typedef GR Graph;
1007
    TEMPLATE_GRAPH_TYPEDEFS(GR);
991 1008

	
992 1009
  private:
993 1010

	
994 1011

	
995 1012
    std::ostream* _os;
996 1013
    bool local_os;
997 1014

	
998
    const Graph& _graph;
1015
    const GR& _graph;
999 1016

	
1000 1017
    std::string _nodes_caption;
1001 1018
    std::string _edges_caption;
1002 1019
    std::string _attributes_caption;
1003 1020

	
1004 1021
    typedef std::map<Node, std::string> NodeIndex;
1005 1022
    NodeIndex _node_index;
1006 1023
    typedef std::map<Edge, std::string> EdgeIndex;
1007 1024
    EdgeIndex _edge_index;
1008 1025

	
1009 1026
    typedef std::vector<std::pair<std::string,
1010 1027
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;
1011 1028
    NodeMaps _node_maps;
1012 1029

	
1013 1030
    typedef std::vector<std::pair<std::string,
1014 1031
      _writer_bits::MapStorageBase<Edge>* > >EdgeMaps;
1015 1032
    EdgeMaps _edge_maps;
1016 1033

	
1017 1034
    typedef std::vector<std::pair<std::string,
1018 1035
      _writer_bits::ValueStorageBase*> > Attributes;
1019 1036
    Attributes _attributes;
1020 1037

	
1021 1038
    bool _skip_nodes;
1022 1039
    bool _skip_edges;
1023 1040

	
1024 1041
  public:
1025 1042

	
1026 1043
    /// \brief Constructor
1027 1044
    ///
1028 1045
    /// Construct a directed graph writer, which writes to the given
1029 1046
    /// output stream.
1030
    GraphWriter(const Graph& graph, std::ostream& os = std::cout)
1047
    GraphWriter(const GR& graph, std::ostream& os = std::cout)
1031 1048
      : _os(&os), local_os(false), _graph(graph),
1032 1049
        _skip_nodes(false), _skip_edges(false) {}
1033 1050

	
1034 1051
    /// \brief Constructor
1035 1052
    ///
1036 1053
    /// Construct a directed graph writer, which writes to the given
1037 1054
    /// output file.
1038
    GraphWriter(const Graph& graph, const std::string& fn)
1055
    GraphWriter(const GR& graph, const std::string& fn)
1039 1056
      : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
1040 1057
        _skip_nodes(false), _skip_edges(false) {
1041 1058
      if (!(*_os)) {
1042 1059
        delete _os;
1043 1060
        throw IoError("Cannot write file", fn);
1044 1061
      }
1045 1062
    }
1046 1063

	
1047 1064
    /// \brief Constructor
1048 1065
    ///
1049 1066
    /// Construct a directed graph writer, which writes to the given
1050 1067
    /// output file.
1051
    GraphWriter(const Graph& graph, const char* fn)
1068
    GraphWriter(const GR& graph, const char* fn)
1052 1069
      : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
1053 1070
        _skip_nodes(false), _skip_edges(false) {
1054 1071
      if (!(*_os)) {
1055 1072
        delete _os;
1056 1073
        throw IoError("Cannot write file", fn);
1057 1074
      }
1058 1075
    }
1059 1076

	
1060 1077
    /// \brief Destructor
1061 1078
    ~GraphWriter() {
1062 1079
      for (typename NodeMaps::iterator it = _node_maps.begin();
1063 1080
           it != _node_maps.end(); ++it) {
1064 1081
        delete it->second;
1065 1082
      }
1066 1083

	
1067 1084
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
1068 1085
           it != _edge_maps.end(); ++it) {
1069 1086
        delete it->second;
1070 1087
      }
1071 1088

	
1072 1089
      for (typename Attributes::iterator it = _attributes.begin();
1073 1090
           it != _attributes.end(); ++it) {
1074 1091
        delete it->second;
1075 1092
      }
1076 1093

	
1077 1094
      if (local_os) {
1078 1095
        delete _os;
1079 1096
      }
1080 1097
    }
1081 1098

	
1082 1099
  private:
1083 1100

	
1084
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
1085
                                            std::ostream& os);
1086
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
1087
                                            const std::string& fn);
1088
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
1089
                                            const char *fn);
1090

	
1101
    template <typename TGR>
1102
    friend GraphWriter<TGR> graphWriter(const TGR& graph, std::ostream& os);
1103
    template <typename TGR>
1104
    friend GraphWriter<TGR> graphWriter(const TGR& graph, 
1105
                                        const std::string& fn);
1106
    template <typename TGR>
1107
    friend GraphWriter<TGR> graphWriter(const TGR& graph, const char *fn);
1108
    
1091 1109
    GraphWriter(GraphWriter& other)
1092 1110
      : _os(other._os), local_os(other.local_os), _graph(other._graph),
1093 1111
        _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
1094 1112

	
1095 1113
      other._os = 0;
1096 1114
      other.local_os = false;
1097 1115

	
1098 1116
      _node_index.swap(other._node_index);
1099 1117
      _edge_index.swap(other._edge_index);
1100 1118

	
1101 1119
      _node_maps.swap(other._node_maps);
1102 1120
      _edge_maps.swap(other._edge_maps);
1103 1121
      _attributes.swap(other._attributes);
1104 1122

	
1105 1123
      _nodes_caption = other._nodes_caption;
1106 1124
      _edges_caption = other._edges_caption;
1107 1125
      _attributes_caption = other._attributes_caption;
1108 1126
    }
1109 1127

	
1110 1128
    GraphWriter& operator=(const GraphWriter&);
1111 1129

	
1112 1130
  public:
1113 1131

	
1114
    /// \name Writing rules
1132
    /// \name Writing Rules
1115 1133
    /// @{
1116 1134

	
1117 1135
    /// \brief Node map writing rule
1118 1136
    ///
1119 1137
    /// Add a node map writing rule to the writer.
1120 1138
    template <typename Map>
1121 1139
    GraphWriter& nodeMap(const std::string& caption, const Map& map) {
1122 1140
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
1123 1141
      _writer_bits::MapStorageBase<Node>* storage =
1124 1142
        new _writer_bits::MapStorage<Node, Map>(map);
1125 1143
      _node_maps.push_back(std::make_pair(caption, storage));
1126 1144
      return *this;
1127 1145
    }
1128 1146

	
1129 1147
    /// \brief Node map writing rule
1130 1148
    ///
1131 1149
    /// Add a node map writing rule with specialized converter to the
1132 1150
    /// writer.
1133 1151
    template <typename Map, typename Converter>
1134 1152
    GraphWriter& nodeMap(const std::string& caption, const Map& map,
1135 1153
                           const Converter& converter = Converter()) {
1136 1154
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
1137 1155
      _writer_bits::MapStorageBase<Node>* storage =
1138 1156
        new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
1139 1157
      _node_maps.push_back(std::make_pair(caption, storage));
1140 1158
      return *this;
1141 1159
    }
1142 1160

	
1143 1161
    /// \brief Edge map writing rule
1144 1162
    ///
1145 1163
    /// Add an edge map writing rule to the writer.
1146 1164
    template <typename Map>
1147 1165
    GraphWriter& edgeMap(const std::string& caption, const Map& map) {
1148 1166
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
1149 1167
      _writer_bits::MapStorageBase<Edge>* storage =
1150 1168
        new _writer_bits::MapStorage<Edge, Map>(map);
1151 1169
      _edge_maps.push_back(std::make_pair(caption, storage));
1152 1170
      return *this;
1153 1171
    }
1154 1172

	
1155 1173
    /// \brief Edge map writing rule
1156 1174
    ///
1157 1175
    /// Add an edge map writing rule with specialized converter to the
1158 1176
    /// writer.
1159 1177
    template <typename Map, typename Converter>
1160 1178
    GraphWriter& edgeMap(const std::string& caption, const Map& map,
1161 1179
                          const Converter& converter = Converter()) {
1162 1180
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
1163 1181
      _writer_bits::MapStorageBase<Edge>* storage =
1164 1182
        new _writer_bits::MapStorage<Edge, Map, Converter>(map, converter);
1165 1183
      _edge_maps.push_back(std::make_pair(caption, storage));
1166 1184
      return *this;
1167 1185
    }
1168 1186

	
1169 1187
    /// \brief Arc map writing rule
1170 1188
    ///
1171 1189
    /// Add an arc map writing rule to the writer.
1172 1190
    template <typename Map>
1173 1191
    GraphWriter& arcMap(const std::string& caption, const Map& map) {
1174 1192
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
1175 1193
      _writer_bits::MapStorageBase<Edge>* forward_storage =
1176
        new _writer_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
1194
        new _writer_bits::GraphArcMapStorage<GR, true, Map>(_graph, map);
1177 1195
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1178 1196
      _writer_bits::MapStorageBase<Edge>* backward_storage =
1179
        new _writer_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
1197
        new _writer_bits::GraphArcMapStorage<GR, false, Map>(_graph, map);
1180 1198
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1181 1199
      return *this;
1182 1200
    }
1183 1201

	
1184 1202
    /// \brief Arc map writing rule
1185 1203
    ///
1186 1204
    /// Add an arc map writing rule with specialized converter to the
1187 1205
    /// writer.
1188 1206
    template <typename Map, typename Converter>
1189 1207
    GraphWriter& arcMap(const std::string& caption, const Map& map,
1190 1208
                          const Converter& converter = Converter()) {
1191 1209
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
1192 1210
      _writer_bits::MapStorageBase<Edge>* forward_storage =
1193
        new _writer_bits::GraphArcMapStorage<Graph, true, Map, Converter>
1211
        new _writer_bits::GraphArcMapStorage<GR, true, Map, Converter>
1194 1212
        (_graph, map, converter);
1195 1213
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1196 1214
      _writer_bits::MapStorageBase<Edge>* backward_storage =
1197
        new _writer_bits::GraphArcMapStorage<Graph, false, Map, Converter>
1215
        new _writer_bits::GraphArcMapStorage<GR, false, Map, Converter>
1198 1216
        (_graph, map, converter);
1199 1217
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1200 1218
      return *this;
1201 1219
    }
1202 1220

	
1203 1221
    /// \brief Attribute writing rule
1204 1222
    ///
1205 1223
    /// Add an attribute writing rule to the writer.
1206 1224
    template <typename Value>
1207 1225
    GraphWriter& attribute(const std::string& caption, const Value& value) {
1208 1226
      _writer_bits::ValueStorageBase* storage =
1209 1227
        new _writer_bits::ValueStorage<Value>(value);
1210 1228
      _attributes.push_back(std::make_pair(caption, storage));
1211 1229
      return *this;
1212 1230
    }
1213 1231

	
1214 1232
    /// \brief Attribute writing rule
1215 1233
    ///
1216 1234
    /// Add an attribute writing rule with specialized converter to the
1217 1235
    /// writer.
1218 1236
    template <typename Value, typename Converter>
1219 1237
    GraphWriter& attribute(const std::string& caption, const Value& value,
1220 1238
                             const Converter& converter = Converter()) {
1221 1239
      _writer_bits::ValueStorageBase* storage =
1222 1240
        new _writer_bits::ValueStorage<Value, Converter>(value, converter);
1223 1241
      _attributes.push_back(std::make_pair(caption, storage));
1224 1242
      return *this;
1225 1243
    }
1226 1244

	
1227 1245
    /// \brief Node writing rule
1228 1246
    ///
1229 1247
    /// Add a node writing rule to the writer.
1230 1248
    GraphWriter& node(const std::string& caption, const Node& node) {
1231 1249
      typedef _writer_bits::MapLookUpConverter<Node> Converter;
1232 1250
      Converter converter(_node_index);
1233 1251
      _writer_bits::ValueStorageBase* storage =
1234 1252
        new _writer_bits::ValueStorage<Node, Converter>(node, converter);
1235 1253
      _attributes.push_back(std::make_pair(caption, storage));
1236 1254
      return *this;
1237 1255
    }
1238 1256

	
1239 1257
    /// \brief Edge writing rule
1240 1258
    ///
1241 1259
    /// Add an edge writing rule to writer.
1242 1260
    GraphWriter& edge(const std::string& caption, const Edge& edge) {
1243 1261
      typedef _writer_bits::MapLookUpConverter<Edge> Converter;
1244 1262
      Converter converter(_edge_index);
1245 1263
      _writer_bits::ValueStorageBase* storage =
1246 1264
        new _writer_bits::ValueStorage<Edge, Converter>(edge, converter);
1247 1265
      _attributes.push_back(std::make_pair(caption, storage));
1248 1266
      return *this;
1249 1267
    }
1250 1268

	
1251 1269
    /// \brief Arc writing rule
1252 1270
    ///
1253 1271
    /// Add an arc writing rule to writer.
1254 1272
    GraphWriter& arc(const std::string& caption, const Arc& arc) {
1255
      typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter;
1273
      typedef _writer_bits::GraphArcLookUpConverter<GR> Converter;
1256 1274
      Converter converter(_graph, _edge_index);
1257 1275
      _writer_bits::ValueStorageBase* storage =
1258 1276
        new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
1259 1277
      _attributes.push_back(std::make_pair(caption, storage));
1260 1278
      return *this;
1261 1279
    }
1262 1280

	
1263
    /// \name Section captions
1281
    /// \name Section Captions
1264 1282
    /// @{
1265 1283

	
1266 1284
    /// \brief Add an additional caption to the \c \@nodes section
1267 1285
    ///
1268 1286
    /// Add an additional caption to the \c \@nodes section.
1269 1287
    GraphWriter& nodes(const std::string& caption) {
1270 1288
      _nodes_caption = caption;
1271 1289
      return *this;
1272 1290
    }
1273 1291

	
1274 1292
    /// \brief Add an additional caption to the \c \@arcs section
1275 1293
    ///
1276 1294
    /// Add an additional caption to the \c \@arcs section.
1277 1295
    GraphWriter& edges(const std::string& caption) {
1278 1296
      _edges_caption = caption;
1279 1297
      return *this;
1280 1298
    }
1281 1299

	
1282 1300
    /// \brief Add an additional caption to the \c \@attributes section
1283 1301
    ///
1284 1302
    /// Add an additional caption to the \c \@attributes section.
1285 1303
    GraphWriter& attributes(const std::string& caption) {
1286 1304
      _attributes_caption = caption;
1287 1305
      return *this;
1288 1306
    }
1289 1307

	
1290
    /// \name Skipping section
1308
    /// \name Skipping Section
1291 1309
    /// @{
1292 1310

	
1293 1311
    /// \brief Skip writing the node set
1294 1312
    ///
1295 1313
    /// The \c \@nodes section will not be written to the stream.
1296 1314
    GraphWriter& skipNodes() {
1297 1315
      LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
1298 1316
      _skip_nodes = true;
1299 1317
      return *this;
1300 1318
    }
1301 1319

	
1302 1320
    /// \brief Skip writing edge set
1303 1321
    ///
1304 1322
    /// The \c \@edges section will not be written to the stream.
1305 1323
    GraphWriter& skipEdges() {
1306 1324
      LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
1307 1325
      _skip_edges = true;
1308 1326
      return *this;
1309 1327
    }
1310 1328

	
1311 1329
    /// @}
1312 1330

	
1313 1331
  private:
1314 1332

	
1315 1333
    void writeNodes() {
1316 1334
      _writer_bits::MapStorageBase<Node>* label = 0;
1317 1335
      for (typename NodeMaps::iterator it = _node_maps.begin();
1318 1336
           it != _node_maps.end(); ++it) {
1319 1337
        if (it->first == "label") {
1320 1338
          label = it->second;
1321 1339
          break;
1322 1340
        }
1323 1341
      }
1324 1342

	
1325 1343
      *_os << "@nodes";
1326 1344
      if (!_nodes_caption.empty()) {
1327 1345
        _writer_bits::writeToken(*_os << ' ', _nodes_caption);
1328 1346
      }
1329 1347
      *_os << std::endl;
1330 1348

	
1331 1349
      if (label == 0) {
1332 1350
        *_os << "label" << '\t';
1333 1351
      }
1334 1352
      for (typename NodeMaps::iterator it = _node_maps.begin();
1335 1353
           it != _node_maps.end(); ++it) {
1336 1354
        _writer_bits::writeToken(*_os, it->first) << '\t';
1337 1355
      }
1338 1356
      *_os << std::endl;
1339 1357

	
1340 1358
      std::vector<Node> nodes;
1341 1359
      for (NodeIt n(_graph); n != INVALID; ++n) {
1342 1360
        nodes.push_back(n);
1343 1361
      }
1344 1362

	
1345 1363
      if (label == 0) {
1346
        IdMap<Graph, Node> id_map(_graph);
1347
        _writer_bits::MapLess<IdMap<Graph, Node> > id_less(id_map);
1364
        IdMap<GR, Node> id_map(_graph);
1365
        _writer_bits::MapLess<IdMap<GR, Node> > id_less(id_map);
1348 1366
        std::sort(nodes.begin(), nodes.end(), id_less);
1349 1367
      } else {
1350 1368
        label->sort(nodes);
1351 1369
      }
1352 1370

	
1353 1371
      for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
1354 1372
        Node n = nodes[i];
1355 1373
        if (label == 0) {
1356 1374
          std::ostringstream os;
1357 1375
          os << _graph.id(n);
1358 1376
          _writer_bits::writeToken(*_os, os.str());
1359 1377
          *_os << '\t';
1360 1378
          _node_index.insert(std::make_pair(n, os.str()));
1361 1379
        }
1362 1380
        for (typename NodeMaps::iterator it = _node_maps.begin();
1363 1381
             it != _node_maps.end(); ++it) {
1364 1382
          std::string value = it->second->get(n);
1365 1383
          _writer_bits::writeToken(*_os, value);
1366 1384
          if (it->first == "label") {
1367 1385
            _node_index.insert(std::make_pair(n, value));
1368 1386
          }
1369 1387
          *_os << '\t';
1370 1388
        }
1371 1389
        *_os << std::endl;
1372 1390
      }
1373 1391
    }
1374 1392

	
1375 1393
    void createNodeIndex() {
1376 1394
      _writer_bits::MapStorageBase<Node>* label = 0;
1377 1395
      for (typename NodeMaps::iterator it = _node_maps.begin();
1378 1396
           it != _node_maps.end(); ++it) {
1379 1397
        if (it->first == "label") {
1380 1398
          label = it->second;
1381 1399
          break;
1382 1400
        }
1383 1401
      }
1384 1402

	
1385 1403
      if (label == 0) {
1386 1404
        for (NodeIt n(_graph); n != INVALID; ++n) {
1387 1405
          std::ostringstream os;
1388 1406
          os << _graph.id(n);
1389 1407
          _node_index.insert(std::make_pair(n, os.str()));
1390 1408
        }
1391 1409
      } else {
1392 1410
        for (NodeIt n(_graph); n != INVALID; ++n) {
1393 1411
          std::string value = label->get(n);
1394 1412
          _node_index.insert(std::make_pair(n, value));
1395 1413
        }
1396 1414
      }
1397 1415
    }
1398 1416

	
1399 1417
    void writeEdges() {
1400 1418
      _writer_bits::MapStorageBase<Edge>* label = 0;
1401 1419
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
1402 1420
           it != _edge_maps.end(); ++it) {
1403 1421
        if (it->first == "label") {
1404 1422
          label = it->second;
1405 1423
          break;
1406 1424
        }
1407 1425
      }
1408 1426

	
1409 1427
      *_os << "@edges";
1410 1428
      if (!_edges_caption.empty()) {
1411 1429
        _writer_bits::writeToken(*_os << ' ', _edges_caption);
1412 1430
      }
1413 1431
      *_os << std::endl;
1414 1432

	
1415 1433
      *_os << '\t' << '\t';
1416 1434
      if (label == 0) {
1417 1435
        *_os << "label" << '\t';
1418 1436
      }
1419 1437
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
1420 1438
           it != _edge_maps.end(); ++it) {
1421 1439
        _writer_bits::writeToken(*_os, it->first) << '\t';
1422 1440
      }
1423 1441
      *_os << std::endl;
1424 1442

	
1425 1443
      std::vector<Edge> edges;
1426 1444
      for (EdgeIt n(_graph); n != INVALID; ++n) {
1427 1445
        edges.push_back(n);
1428 1446
      }
1429 1447

	
1430 1448
      if (label == 0) {
1431
        IdMap<Graph, Edge> id_map(_graph);
1432
        _writer_bits::MapLess<IdMap<Graph, Edge> > id_less(id_map);
1449
        IdMap<GR, Edge> id_map(_graph);
1450
        _writer_bits::MapLess<IdMap<GR, Edge> > id_less(id_map);
1433 1451
        std::sort(edges.begin(), edges.end(), id_less);
1434 1452
      } else {
1435 1453
        label->sort(edges);
1436 1454
      }
1437 1455

	
1438 1456
      for (int i = 0; i < static_cast<int>(edges.size()); ++i) {
1439 1457
        Edge e = edges[i];
1440 1458
        _writer_bits::writeToken(*_os, _node_index.
1441 1459
                                 find(_graph.u(e))->second);
1442 1460
        *_os << '\t';
1443 1461
        _writer_bits::writeToken(*_os, _node_index.
1444 1462
                                 find(_graph.v(e))->second);
1445 1463
        *_os << '\t';
1446 1464
        if (label == 0) {
1447 1465
          std::ostringstream os;
1448 1466
          os << _graph.id(e);
1449 1467
          _writer_bits::writeToken(*_os, os.str());
1450 1468
          *_os << '\t';
1451 1469
          _edge_index.insert(std::make_pair(e, os.str()));
1452 1470
        }
1453 1471
        for (typename EdgeMaps::iterator it = _edge_maps.begin();
1454 1472
             it != _edge_maps.end(); ++it) {
1455 1473
          std::string value = it->second->get(e);
1456 1474
          _writer_bits::writeToken(*_os, value);
1457 1475
          if (it->first == "label") {
1458 1476
            _edge_index.insert(std::make_pair(e, value));
1459 1477
          }
1460 1478
          *_os << '\t';
1461 1479
        }
1462 1480
        *_os << std::endl;
1463 1481
      }
1464 1482
    }
1465 1483

	
1466 1484
    void createEdgeIndex() {
1467 1485
      _writer_bits::MapStorageBase<Edge>* label = 0;
1468 1486
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
1469 1487
           it != _edge_maps.end(); ++it) {
1470 1488
        if (it->first == "label") {
1471 1489
          label = it->second;
1472 1490
          break;
1473 1491
        }
1474 1492
      }
1475 1493

	
1476 1494
      if (label == 0) {
1477 1495
        for (EdgeIt e(_graph); e != INVALID; ++e) {
1478 1496
          std::ostringstream os;
1479 1497
          os << _graph.id(e);
1480 1498
          _edge_index.insert(std::make_pair(e, os.str()));
1481 1499
        }
1482 1500
      } else {
1483 1501
        for (EdgeIt e(_graph); e != INVALID; ++e) {
1484 1502
          std::string value = label->get(e);
1485 1503
          _edge_index.insert(std::make_pair(e, value));
1486 1504
        }
1487 1505
      }
1488 1506
    }
1489 1507

	
1490 1508
    void writeAttributes() {
1491 1509
      if (_attributes.empty()) return;
1492 1510
      *_os << "@attributes";
1493 1511
      if (!_attributes_caption.empty()) {
1494 1512
        _writer_bits::writeToken(*_os << ' ', _attributes_caption);
1495 1513
      }
1496 1514
      *_os << std::endl;
1497 1515
      for (typename Attributes::iterator it = _attributes.begin();
1498 1516
           it != _attributes.end(); ++it) {
1499 1517
        _writer_bits::writeToken(*_os, it->first) << ' ';
1500 1518
        _writer_bits::writeToken(*_os, it->second->get());
1501 1519
        *_os << std::endl;
1502 1520
      }
1503 1521
    }
1504 1522

	
1505 1523
  public:
1506 1524

	
1507
    /// \name Execution of the writer
1525
    /// \name Execution of the Writer
1508 1526
    /// @{
1509 1527

	
1510 1528
    /// \brief Start the batch processing
1511 1529
    ///
1512 1530
    /// This function starts the batch processing.
1513 1531
    void run() {
1514 1532
      if (!_skip_nodes) {
1515 1533
        writeNodes();
1516 1534
      } else {
1517 1535
        createNodeIndex();
1518 1536
      }
1519 1537
      if (!_skip_edges) {
1520 1538
        writeEdges();
1521 1539
      } else {
1522 1540
        createEdgeIndex();
1523 1541
      }
1524 1542
      writeAttributes();
1525 1543
    }
1526 1544

	
1527 1545
    /// \brief Give back the stream of the writer
1528 1546
    ///
1529 1547
    /// Give back the stream of the writer
1530 1548
    std::ostream& ostream() {
1531 1549
      return *_os;
1532 1550
    }
1533 1551

	
1534 1552
    /// @}
1535 1553
  };
1536 1554

	
1555
  /// \ingroup lemon_io
1556
  ///
1557
  /// \brief Return a \ref GraphWriter class
1558
  ///
1559
  /// This function just returns a \ref GraphWriter class. 
1560
  ///
1561
  /// With this function a graph can be write to a file or output
1562
  /// stream in \ref lgf-format "LGF" format with several maps and
1563
  /// attributes. For example, with the following code a weighted
1564
  /// matching problem can be written to the standard output, i.e. a
1565
  /// graph with a \e weight map on the edges:
1566
  ///
1567
  ///\code
1568
  ///ListGraph graph;
1569
  ///ListGraph::EdgeMap<int> weight(graph);
1570
  ///  // Setting the weight map
1571
  ///graphWriter(graph, std::cout).
1572
  ///  edgeMap("weight", weight).
1573
  ///  run();
1574
  ///\endcode
1575
  ///
1576
  /// For a complete documentation, please see the \ref GraphWriter
1577
  /// class documentation.
1578
  /// \warning Don't forget to put the \ref GraphWriter::run() "run()"
1579
  /// to the end of the parameter list.
1580
  /// \relates GraphWriter
1581
  /// \sa graphWriter(const TGR& graph, const std::string& fn)
1582
  /// \sa graphWriter(const TGR& graph, const char* fn)
1583
  template <typename TGR>
1584
  GraphWriter<TGR> graphWriter(const TGR& graph, std::ostream& os) {
1585
    GraphWriter<TGR> tmp(graph, os);
1586
    return tmp;
1587
  }
1588

	
1589
  /// \brief Return a \ref GraphWriter class
1590
  ///
1591
  /// This function just returns a \ref GraphWriter class.
1592
  /// \relates GraphWriter
1593
  /// \sa graphWriter(const TGR& graph, std::ostream& os)
1594
  template <typename TGR>
1595
  GraphWriter<TGR> graphWriter(const TGR& graph, const std::string& fn) {
1596
    GraphWriter<TGR> tmp(graph, fn);
1597
    return tmp;
1598
  }
1599

	
1600
  /// \brief Return a \ref GraphWriter class
1601
  ///
1602
  /// This function just returns a \ref GraphWriter class.
1603
  /// \relates GraphWriter
1604
  /// \sa graphWriter(const TGR& graph, std::ostream& os)
1605
  template <typename TGR>
1606
  GraphWriter<TGR> graphWriter(const TGR& graph, const char* fn) {
1607
    GraphWriter<TGR> tmp(graph, fn);
1608
    return tmp;
1609
  }
1610

	
1537 1611
  class SectionWriter;
1538 1612

	
1539 1613
  SectionWriter sectionWriter(std::istream& is);
1540 1614
  SectionWriter sectionWriter(const std::string& fn);
1541 1615
  SectionWriter sectionWriter(const char* fn);
1542 1616

	
1543 1617
  /// \ingroup lemon_io
1544 1618
  ///
1545 1619
  /// \brief Section writer class
1546 1620
  ///
1547 1621
  /// In the \ref lgf-format "LGF" file extra sections can be placed,
1548 1622
  /// which contain any data in arbitrary format. Such sections can be
1549 1623
  /// written with this class. A writing rule can be added to the
1550 1624
  /// class with two different functions. With the \c sectionLines()
1551 1625
  /// function a generator can write the section line-by-line, while
1552 1626
  /// with the \c sectionStream() member the section can be written to
1553 1627
  /// an output stream.
1554 1628
  class SectionWriter {
1555 1629
  private:
1556 1630

	
1557 1631
    std::ostream* _os;
1558 1632
    bool local_os;
1559 1633

	
1560 1634
    typedef std::vector<std::pair<std::string, _writer_bits::Section*> >
1561 1635
    Sections;
1562 1636

	
1563 1637
    Sections _sections;
1564 1638

	
1565 1639
  public:
1566 1640

	
1567 1641
    /// \brief Constructor
1568 1642
    ///
1569 1643
    /// Construct a section writer, which writes to the given output
1570 1644
    /// stream.
1571 1645
    SectionWriter(std::ostream& os)
1572 1646
      : _os(&os), local_os(false) {}
1573 1647

	
1574 1648
    /// \brief Constructor
1575 1649
    ///
1576 1650
    /// Construct a section writer, which writes into the given file.
1577 1651
    SectionWriter(const std::string& fn)
1578 1652
      : _os(new std::ofstream(fn.c_str())), local_os(true) {
1579 1653
      if (!(*_os)) {
1580 1654
        delete _os;
1581 1655
        throw IoError("Cannot write file", fn);
1582 1656
      }
1583 1657
    }
1584 1658

	
1585 1659
    /// \brief Constructor
1586 1660
    ///
1587 1661
    /// Construct a section writer, which writes into the given file.
1588 1662
    SectionWriter(const char* fn)
1589 1663
      : _os(new std::ofstream(fn)), local_os(true) {
1590 1664
      if (!(*_os)) {
1591 1665
        delete _os;
1592 1666
        throw IoError("Cannot write file", fn);
1593 1667
      }
1594 1668
    }
1595 1669

	
1596 1670
    /// \brief Destructor
1597 1671
    ~SectionWriter() {
1598 1672
      for (Sections::iterator it = _sections.begin();
1599 1673
           it != _sections.end(); ++it) {
1600 1674
        delete it->second;
1601 1675
      }
1602 1676

	
1603 1677
      if (local_os) {
1604 1678
        delete _os;
1605 1679
      }
1606 1680

	
1607 1681
    }
1608 1682

	
1609 1683
  private:
1610 1684

	
1611 1685
    friend SectionWriter sectionWriter(std::ostream& os);
1612 1686
    friend SectionWriter sectionWriter(const std::string& fn);
1613 1687
    friend SectionWriter sectionWriter(const char* fn);
1614 1688

	
1615 1689
    SectionWriter(SectionWriter& other)
1616 1690
      : _os(other._os), local_os(other.local_os) {
1617 1691

	
1618 1692
      other._os = 0;
1619 1693
      other.local_os = false;
1620 1694

	
1621 1695
      _sections.swap(other._sections);
1622 1696
    }
1623 1697

	
1624 1698
    SectionWriter& operator=(const SectionWriter&);
1625 1699

	
1626 1700
  public:
1627 1701

	
1628
    /// \name Section writers
1702
    /// \name Section Writers
1629 1703
    /// @{
1630 1704

	
1631 1705
    /// \brief Add a section writer with line oriented writing
1632 1706
    ///
1633 1707
    /// The first parameter is the type descriptor of the section, the
1634 1708
    /// second is a generator with std::string values. At the writing
1635 1709
    /// process, the returned \c std::string will be written into the
1636 1710
    /// output file until it is an empty string.
1637 1711
    ///
1638 1712
    /// For example, an integer vector is written into a section.
1639 1713
    ///\code
1640 1714
    ///  @numbers
1641 1715
    ///  12 45 23 78
1642 1716
    ///  4 28 38 28
1643 1717
    ///  23 6 16
1644 1718
    ///\endcode
1645 1719
    ///
1646 1720
    /// The generator is implemented as a struct.
1647 1721
    ///\code
1648 1722
    ///  struct NumberSection {
1649 1723
    ///    std::vector<int>::const_iterator _it, _end;
1650 1724
    ///    NumberSection(const std::vector<int>& data)
1651 1725
    ///      : _it(data.begin()), _end(data.end()) {}
1652 1726
    ///    std::string operator()() {
1653 1727
    ///      int rem_in_line = 4;
1654 1728
    ///      std::ostringstream ls;
1655 1729
    ///      while (rem_in_line > 0 && _it != _end) {
1656 1730
    ///        ls << *(_it++) << ' ';
1657 1731
    ///        --rem_in_line;
1658 1732
    ///      }
1659 1733
    ///      return ls.str();
1660 1734
    ///    }
1661 1735
    ///  };
1662 1736
    ///
1663 1737
    ///  // ...
1664 1738
    ///
1665 1739
    ///  writer.sectionLines("numbers", NumberSection(vec));
1666 1740
    ///\endcode
1667 1741
    template <typename Functor>
1668 1742
    SectionWriter& sectionLines(const std::string& type, Functor functor) {
1669 1743
      LEMON_ASSERT(!type.empty(), "Type is empty.");
1670 1744
      _sections.push_back(std::make_pair(type,
1671 1745
        new _writer_bits::LineSection<Functor>(functor)));
1672 1746
      return *this;
1673 1747
    }
1674 1748

	
1675 1749

	
1676 1750
    /// \brief Add a section writer with stream oriented writing
1677 1751
    ///
1678 1752
    /// The first parameter is the type of the section, the second is
1679 1753
    /// a functor, which takes a \c std::ostream& parameter. The
1680 1754
    /// functor writes the section to the output stream.
1681 1755
    /// \warning The last line must be closed with end-line character.
1682 1756
    template <typename Functor>
1683 1757
    SectionWriter& sectionStream(const std::string& type, Functor functor) {
1684 1758
      LEMON_ASSERT(!type.empty(), "Type is empty.");
1685 1759
      _sections.push_back(std::make_pair(type,
1686 1760
         new _writer_bits::StreamSection<Functor>(functor)));
1687 1761
      return *this;
1688 1762
    }
1689 1763

	
1690 1764
    /// @}
1691 1765

	
1692 1766
  public:
1693 1767

	
1694 1768

	
1695
    /// \name Execution of the writer
1769
    /// \name Execution of the Writer
1696 1770
    /// @{
1697 1771

	
1698 1772
    /// \brief Start the batch processing
1699 1773
    ///
1700 1774
    /// This function starts the batch processing.
1701 1775
    void run() {
1702 1776

	
1703 1777
      LEMON_ASSERT(_os != 0, "This writer is assigned to an other writer");
1704 1778

	
1705 1779
      for (Sections::iterator it = _sections.begin();
1706 1780
           it != _sections.end(); ++it) {
1707 1781
        (*_os) << '@' << it->first << std::endl;
1708 1782
        it->second->process(*_os);
1709 1783
      }
1710 1784
    }
1711 1785

	
1712 1786
    /// \brief Give back the stream of the writer
1713 1787
    ///
1714 1788
    /// Returns the stream of the writer
1715 1789
    std::ostream& ostream() {
1716 1790
      return *_os;
1717 1791
    }
1718 1792

	
1719 1793
    /// @}
1720 1794

	
1721 1795
  };
1722 1796

	
1797
  /// \ingroup lemon_io
1798
  ///
1723 1799
  /// \brief Return a \ref SectionWriter class
1724 1800
  ///
1725 1801
  /// This function just returns a \ref SectionWriter class.
1802
  ///
1803
  /// Please see SectionWriter documentation about the custom section
1804
  /// output.
1805
  ///
1726 1806
  /// \relates SectionWriter
1807
  /// \sa sectionWriter(const std::string& fn)
1808
  /// \sa sectionWriter(const char *fn)
1727 1809
  inline SectionWriter sectionWriter(std::ostream& os) {
1728 1810
    SectionWriter tmp(os);
1729 1811
    return tmp;
1730 1812
  }
1731 1813

	
1732 1814
  /// \brief Return a \ref SectionWriter class
1733 1815
  ///
1734 1816
  /// This function just returns a \ref SectionWriter class.
1735 1817
  /// \relates SectionWriter
1818
  /// \sa sectionWriter(std::ostream& os)
1736 1819
  inline SectionWriter sectionWriter(const std::string& fn) {
1737 1820
    SectionWriter tmp(fn);
1738 1821
    return tmp;
1739 1822
  }
1740 1823

	
1741 1824
  /// \brief Return a \ref SectionWriter class
1742 1825
  ///
1743 1826
  /// This function just returns a \ref SectionWriter class.
1744 1827
  /// \relates SectionWriter
1828
  /// \sa sectionWriter(std::ostream& os)
1745 1829
  inline SectionWriter sectionWriter(const char* fn) {
1746 1830
    SectionWriter tmp(fn);
1747 1831
    return tmp;
1748 1832
  }
1749 1833
}
1750 1834

	
1751 1835
#endif
Ignore white space 6 line context
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-2009
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_LIST_GRAPH_H
20 20
#define LEMON_LIST_GRAPH_H
21 21

	
22 22
///\ingroup graphs
23 23
///\file
24
///\brief ListDigraph, ListGraph classes.
24
///\brief ListDigraph and ListGraph classes.
25 25

	
26 26
#include <lemon/core.h>
27 27
#include <lemon/error.h>
28 28
#include <lemon/bits/graph_extender.h>
29 29

	
30 30
#include <vector>
31 31
#include <list>
32 32

	
33 33
namespace lemon {
34 34

	
35
  class ListDigraph;
36

	
35 37
  class ListDigraphBase {
36 38

	
37 39
  protected:
38 40
    struct NodeT {
39 41
      int first_in, first_out;
40 42
      int prev, next;
41 43
    };
42 44

	
43 45
    struct ArcT {
44 46
      int target, source;
45 47
      int prev_in, prev_out;
46 48
      int next_in, next_out;
47 49
    };
48 50

	
49 51
    std::vector<NodeT> nodes;
50 52

	
51 53
    int first_node;
52 54

	
53 55
    int first_free_node;
54 56

	
55 57
    std::vector<ArcT> arcs;
56 58

	
57 59
    int first_free_arc;
58 60

	
59 61
  public:
60 62

	
61 63
    typedef ListDigraphBase Digraph;
62 64

	
63 65
    class Node {
64 66
      friend class ListDigraphBase;
67
      friend class ListDigraph;
65 68
    protected:
66 69

	
67 70
      int id;
68 71
      explicit Node(int pid) { id = pid;}
69 72

	
70 73
    public:
71 74
      Node() {}
72 75
      Node (Invalid) { id = -1; }
73 76
      bool operator==(const Node& node) const {return id == node.id;}
74 77
      bool operator!=(const Node& node) const {return id != node.id;}
75 78
      bool operator<(const Node& node) const {return id < node.id;}
76 79
    };
77 80

	
78 81
    class Arc {
79 82
      friend class ListDigraphBase;
83
      friend class ListDigraph;
80 84
    protected:
81 85

	
82 86
      int id;
83 87
      explicit Arc(int pid) { id = pid;}
84 88

	
85 89
    public:
86 90
      Arc() {}
87 91
      Arc (Invalid) { id = -1; }
88 92
      bool operator==(const Arc& arc) const {return id == arc.id;}
89 93
      bool operator!=(const Arc& arc) const {return id != arc.id;}
90 94
      bool operator<(const Arc& arc) const {return id < arc.id;}
91 95
    };
92 96

	
93 97

	
94 98

	
95 99
    ListDigraphBase()
96 100
      : nodes(), first_node(-1),
97 101
        first_free_node(-1), arcs(), first_free_arc(-1) {}
98 102

	
99 103

	
100 104
    int maxNodeId() const { return nodes.size()-1; }
101 105
    int maxArcId() const { return arcs.size()-1; }
102 106

	
103 107
    Node source(Arc e) const { return Node(arcs[e.id].source); }
104 108
    Node target(Arc e) const { return Node(arcs[e.id].target); }
105 109

	
106 110

	
107 111
    void first(Node& node) const {
108 112
      node.id = first_node;
109 113
    }
110 114

	
111 115
    void next(Node& node) const {
112 116
      node.id = nodes[node.id].next;
113 117
    }
114 118

	
115 119

	
116 120
    void first(Arc& arc) const {
117 121
      int n;
118 122
      for(n = first_node;
119
          n!=-1 && nodes[n].first_in == -1;
123
          n != -1 && nodes[n].first_out == -1;
120 124
          n = nodes[n].next) {}
121
      arc.id = (n == -1) ? -1 : nodes[n].first_in;
125
      arc.id = (n == -1) ? -1 : nodes[n].first_out;
122 126
    }
123 127

	
124 128
    void next(Arc& arc) const {
125
      if (arcs[arc.id].next_in != -1) {
126
        arc.id = arcs[arc.id].next_in;
129
      if (arcs[arc.id].next_out != -1) {
130
        arc.id = arcs[arc.id].next_out;
127 131
      } else {
128 132
        int n;
129
        for(n = nodes[arcs[arc.id].target].next;
130
            n!=-1 && nodes[n].first_in == -1;
133
        for(n = nodes[arcs[arc.id].source].next;
134
            n != -1 && nodes[n].first_out == -1;
131 135
            n = nodes[n].next) {}
132
        arc.id = (n == -1) ? -1 : nodes[n].first_in;
136
        arc.id = (n == -1) ? -1 : nodes[n].first_out;
133 137
      }
134 138
    }
135 139

	
136 140
    void firstOut(Arc &e, const Node& v) const {
137 141
      e.id = nodes[v.id].first_out;
138 142
    }
139 143
    void nextOut(Arc &e) const {
140 144
      e.id=arcs[e.id].next_out;
141 145
    }
142 146

	
143 147
    void firstIn(Arc &e, const Node& v) const {
144 148
      e.id = nodes[v.id].first_in;
145 149
    }
146 150
    void nextIn(Arc &e) const {
147 151
      e.id=arcs[e.id].next_in;
148 152
    }
149 153

	
150 154

	
151 155
    static int id(Node v) { return v.id; }
152 156
    static int id(Arc e) { return e.id; }
153 157

	
154 158
    static Node nodeFromId(int id) { return Node(id);}
155 159
    static Arc arcFromId(int id) { return Arc(id);}
156 160

	
157 161
    bool valid(Node n) const {
158 162
      return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
159 163
        nodes[n.id].prev != -2;
160 164
    }
161 165

	
162 166
    bool valid(Arc a) const {
163 167
      return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
164 168
        arcs[a.id].prev_in != -2;
165 169
    }
166 170

	
167 171
    Node addNode() {
168 172
      int n;
169 173

	
170 174
      if(first_free_node==-1) {
171 175
        n = nodes.size();
172 176
        nodes.push_back(NodeT());
173 177
      } else {
174 178
        n = first_free_node;
175 179
        first_free_node = nodes[n].next;
176 180
      }
177 181

	
178 182
      nodes[n].next = first_node;
179 183
      if(first_node != -1) nodes[first_node].prev = n;
180 184
      first_node = n;
181 185
      nodes[n].prev = -1;
182 186

	
183 187
      nodes[n].first_in = nodes[n].first_out = -1;
184 188

	
185 189
      return Node(n);
186 190
    }
187 191

	
188 192
    Arc addArc(Node u, Node v) {
189 193
      int n;
190 194

	
191 195
      if (first_free_arc == -1) {
192 196
        n = arcs.size();
193 197
        arcs.push_back(ArcT());
194 198
      } else {
195 199
        n = first_free_arc;
196 200
        first_free_arc = arcs[n].next_in;
197 201
      }
198 202

	
199 203
      arcs[n].source = u.id;
200 204
      arcs[n].target = v.id;
201 205

	
202 206
      arcs[n].next_out = nodes[u.id].first_out;
203 207
      if(nodes[u.id].first_out != -1) {
204 208
        arcs[nodes[u.id].first_out].prev_out = n;
205 209
      }
206 210

	
207 211
      arcs[n].next_in = nodes[v.id].first_in;
208 212
      if(nodes[v.id].first_in != -1) {
209 213
        arcs[nodes[v.id].first_in].prev_in = n;
210 214
      }
211 215

	
212 216
      arcs[n].prev_in = arcs[n].prev_out = -1;
213 217

	
214 218
      nodes[u.id].first_out = nodes[v.id].first_in = n;
215 219

	
216 220
      return Arc(n);
217 221
    }
218 222

	
219 223
    void erase(const Node& node) {
220 224
      int n = node.id;
221 225

	
222 226
      if(nodes[n].next != -1) {
223 227
        nodes[nodes[n].next].prev = nodes[n].prev;
224 228
      }
225 229

	
226 230
      if(nodes[n].prev != -1) {
227 231
        nodes[nodes[n].prev].next = nodes[n].next;
228 232
      } else {
229 233
        first_node = nodes[n].next;
230 234
      }
231 235

	
232 236
      nodes[n].next = first_free_node;
233 237
      first_free_node = n;
234 238
      nodes[n].prev = -2;
235 239

	
236 240
    }
237 241

	
238 242
    void erase(const Arc& arc) {
239 243
      int n = arc.id;
240 244

	
241 245
      if(arcs[n].next_in!=-1) {
242 246
        arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
243 247
      }
244 248

	
245 249
      if(arcs[n].prev_in!=-1) {
246 250
        arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
247 251
      } else {
248 252
        nodes[arcs[n].target].first_in = arcs[n].next_in;
249 253
      }
250 254

	
251 255

	
252 256
      if(arcs[n].next_out!=-1) {
253 257
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
254 258
      }
255 259

	
256 260
      if(arcs[n].prev_out!=-1) {
257 261
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
258 262
      } else {
259 263
        nodes[arcs[n].source].first_out = arcs[n].next_out;
260 264
      }
261 265

	
262 266
      arcs[n].next_in = first_free_arc;
263 267
      first_free_arc = n;
264 268
      arcs[n].prev_in = -2;
265 269
    }
266 270

	
267 271
    void clear() {
268 272
      arcs.clear();
269 273
      nodes.clear();
270 274
      first_node = first_free_node = first_free_arc = -1;
271 275
    }
272 276

	
273 277
  protected:
274 278
    void changeTarget(Arc e, Node n)
275 279
    {
276 280
      if(arcs[e.id].next_in != -1)
277 281
        arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in;
278 282
      if(arcs[e.id].prev_in != -1)
279 283
        arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in;
280 284
      else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in;
281 285
      if (nodes[n.id].first_in != -1) {
282 286
        arcs[nodes[n.id].first_in].prev_in = e.id;
283 287
      }
284 288
      arcs[e.id].target = n.id;
285 289
      arcs[e.id].prev_in = -1;
286 290
      arcs[e.id].next_in = nodes[n.id].first_in;
287 291
      nodes[n.id].first_in = e.id;
288 292
    }
289 293
    void changeSource(Arc e, Node n)
290 294
    {
291 295
      if(arcs[e.id].next_out != -1)
292 296
        arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out;
293 297
      if(arcs[e.id].prev_out != -1)
294 298
        arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out;
295 299
      else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out;
296 300
      if (nodes[n.id].first_out != -1) {
297 301
        arcs[nodes[n.id].first_out].prev_out = e.id;
298 302
      }
299 303
      arcs[e.id].source = n.id;
300 304
      arcs[e.id].prev_out = -1;
301 305
      arcs[e.id].next_out = nodes[n.id].first_out;
302 306
      nodes[n.id].first_out = e.id;
303 307
    }
304 308

	
305 309
  };
306 310

	
307 311
  typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase;
308 312

	
309 313
  /// \addtogroup graphs
310 314
  /// @{
311 315

	
312 316
  ///A general directed graph structure.
313 317

	
314
  ///\ref ListDigraph is a simple and fast <em>directed graph</em>
315
  ///implementation based on static linked lists that are stored in
318
  ///\ref ListDigraph is a versatile and fast directed graph
319
  ///implementation based on linked lists that are stored in
316 320
  ///\c std::vector structures.
317 321
  ///
318
  ///It conforms to the \ref concepts::Digraph "Digraph concept" and it
319
  ///also provides several useful additional functionalities.
320
  ///Most of the member functions and nested classes are documented
322
  ///This type fully conforms to the \ref concepts::Digraph "Digraph concept"
323
  ///and it also provides several useful additional functionalities.
324
  ///Most of its member functions and nested classes are documented
321 325
  ///only in the concept class.
322 326
  ///
323
  ///An important extra feature of this digraph implementation is that
324
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
325
  ///
326 327
  ///\sa concepts::Digraph
328
  ///\sa ListGraph
329
  class ListDigraph : public ExtendedListDigraphBase {
330
    typedef ExtendedListDigraphBase Parent;
327 331

	
328
  class ListDigraph : public ExtendedListDigraphBase {
329 332
  private:
330
    ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
331

	
332
    ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
333
    ///
333
    /// Digraphs are \e not copy constructible. Use DigraphCopy instead.
334 334
    ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
335
    ///\brief Assignment of ListDigraph to another one is \e not allowed.
336
    ///Use copyDigraph() instead.
337

	
338
    ///Assignment of ListDigraph to another one is \e not allowed.
339
    ///Use copyDigraph() instead.
335
    /// \brief Assignment of a digraph to another one is \e not allowed.
336
    /// Use DigraphCopy instead.
340 337
    void operator=(const ListDigraph &) {}
341 338
  public:
342 339

	
343
    typedef ExtendedListDigraphBase Parent;
344

	
345 340
    /// Constructor
346 341

	
347 342
    /// Constructor.
348 343
    ///
349 344
    ListDigraph() {}
350 345

	
351 346
    ///Add a new node to the digraph.
352 347

	
353
    ///Add a new node to the digraph.
354
    ///\return the new node.
348
    ///This function adds a new node to the digraph.
349
    ///\return The new node.
355 350
    Node addNode() { return Parent::addNode(); }
356 351

	
357 352
    ///Add a new arc to the digraph.
358 353

	
359
    ///Add a new arc to the digraph with source node \c s
354
    ///This function adds a new arc to the digraph with source node \c s
360 355
    ///and target node \c t.
361
    ///\return the new arc.
362
    Arc addArc(const Node& s, const Node& t) {
356
    ///\return The new arc.
357
    Arc addArc(Node s, Node t) {
363 358
      return Parent::addArc(s, t);
364 359
    }
365 360

	
366 361
    ///\brief Erase a node from the digraph.
367 362
    ///
368
    ///Erase a node from the digraph.
369
    ///
370
    void erase(const Node& n) { Parent::erase(n); }
363
    ///This function erases the given node from the digraph.
364
    void erase(Node n) { Parent::erase(n); }
371 365

	
372 366
    ///\brief Erase an arc from the digraph.
373 367
    ///
374
    ///Erase an arc from the digraph.
375
    ///
376
    void erase(const Arc& a) { Parent::erase(a); }
368
    ///This function erases the given arc from the digraph.
369
    void erase(Arc a) { Parent::erase(a); }
377 370

	
378 371
    /// Node validity check
379 372

	
380
    /// This function gives back true if the given node is valid,
381
    /// ie. it is a real node of the graph.
373
    /// This function gives back \c true if the given node is valid,
374
    /// i.e. it is a real node of the digraph.
382 375
    ///
383
    /// \warning A Node pointing to a removed item
384
    /// could become valid again later if new nodes are
385
    /// added to the graph.
376
    /// \warning A removed node could become valid again if new nodes are
377
    /// added to the digraph.
386 378
    bool valid(Node n) const { return Parent::valid(n); }
387 379

	
388 380
    /// Arc validity check
389 381

	
390
    /// This function gives back true if the given arc is valid,
391
    /// ie. it is a real arc of the graph.
382
    /// This function gives back \c true if the given arc is valid,
383
    /// i.e. it is a real arc of the digraph.
392 384
    ///
393
    /// \warning An Arc pointing to a removed item
394
    /// could become valid again later if new nodes are
395
    /// added to the graph.
385
    /// \warning A removed arc could become valid again if new arcs are
386
    /// added to the digraph.
396 387
    bool valid(Arc a) const { return Parent::valid(a); }
397 388

	
398
    /// Change the target of \c a to \c n
389
    /// Change the target node of an arc
399 390

	
400
    /// Change the target of \c a to \c n
391
    /// This function changes the target node of the given arc \c a to \c n.
401 392
    ///
402
    ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing
403
    ///the changed arc remain valid. However <tt>InArcIt</tt>s are
404
    ///invalidated.
393
    ///\note \c ArcIt and \c OutArcIt iterators referencing the changed
394
    ///arc remain valid, however \c InArcIt iterators are invalidated.
405 395
    ///
406 396
    ///\warning This functionality cannot be used together with the Snapshot
407 397
    ///feature.
408 398
    void changeTarget(Arc a, Node n) {
409 399
      Parent::changeTarget(a,n);
410 400
    }
411
    /// Change the source of \c a to \c n
401
    /// Change the source node of an arc
412 402

	
413
    /// Change the source of \c a to \c n
403
    /// This function changes the source node of the given arc \c a to \c n.
414 404
    ///
415
    ///\note The <tt>InArcIt</tt>s referencing the changed arc remain
416
    ///valid. However the <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s are
417
    ///invalidated.
405
    ///\note \c InArcIt iterators referencing the changed arc remain
406
    ///valid, however \c ArcIt and \c OutArcIt iterators are invalidated.
418 407
    ///
419 408
    ///\warning This functionality cannot be used together with the Snapshot
420 409
    ///feature.
421 410
    void changeSource(Arc a, Node n) {
422 411
      Parent::changeSource(a,n);
423 412
    }
424 413

	
425
    /// Invert the direction of an arc.
414
    /// Reverse the direction of an arc.
426 415

	
427
    ///\note The <tt>ArcIt</tt>s referencing the changed arc remain
428
    ///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are
429
    ///invalidated.
416
    /// This function reverses the direction of the given arc.
417
    ///\note \c ArcIt, \c OutArcIt and \c InArcIt iterators referencing
418
    ///the changed arc are invalidated.
430 419
    ///
431 420
    ///\warning This functionality cannot be used together with the Snapshot
432 421
    ///feature.
433
    void reverseArc(Arc e) {
434
      Node t=target(e);
435
      changeTarget(e,source(e));
436
      changeSource(e,t);
422
    void reverseArc(Arc a) {
423
      Node t=target(a);
424
      changeTarget(a,source(a));
425
      changeSource(a,t);
437 426
    }
438 427

	
439
    /// Reserve memory for nodes.
440

	
441
    /// Using this function it is possible to avoid the superfluous memory
442
    /// allocation: if you know that the digraph you want to build will
443
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
444
    /// then it is worth reserving space for this amount before starting
445
    /// to build the digraph.
446
    /// \sa reserveArc
447
    void reserveNode(int n) { nodes.reserve(n); };
448

	
449
    /// Reserve memory for arcs.
450

	
451
    /// Using this function it is possible to avoid the superfluous memory
452
    /// allocation: if you know that the digraph you want to build will
453
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
454
    /// then it is worth reserving space for this amount before starting
455
    /// to build the digraph.
456
    /// \sa reserveNode
457
    void reserveArc(int m) { arcs.reserve(m); };
458

	
459 428
    ///Contract two nodes.
460 429

	
461
    ///This function contracts two nodes.
462
    ///Node \p b will be removed but instead of deleting
463
    ///incident arcs, they will be joined to \p a.
464
    ///The last parameter \p r controls whether to remove loops. \c true
465
    ///means that loops will be removed.
430
    ///This function contracts the given two nodes.
431
    ///Node \c v is removed, but instead of deleting its
432
    ///incident arcs, they are joined to node \c u.
433
    ///If the last parameter \c r is \c true (this is the default value),
434
    ///then the newly created loops are removed.
466 435
    ///
467
    ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
468
    ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s
469
    ///may be invalidated.
436
    ///\note The moved arcs are joined to node \c u using changeSource()
437
    ///or changeTarget(), thus \c ArcIt and \c OutArcIt iterators are
438
    ///invalidated for the outgoing arcs of node \c v and \c InArcIt
439
    ///iterators are invalidated for the incomming arcs of \c v.
440
    ///Moreover all iterators referencing node \c v or the removed 
441
    ///loops are also invalidated. Other iterators remain valid.
470 442
    ///
471 443
    ///\warning This functionality cannot be used together with the Snapshot
472 444
    ///feature.
473
    void contract(Node a, Node b, bool r = true)
445
    void contract(Node u, Node v, bool r = true)
474 446
    {
475
      for(OutArcIt e(*this,b);e!=INVALID;) {
447
      for(OutArcIt e(*this,v);e!=INVALID;) {
476 448
        OutArcIt f=e;
477 449
        ++f;
478
        if(r && target(e)==a) erase(e);
479
        else changeSource(e,a);
450
        if(r && target(e)==u) erase(e);
451
        else changeSource(e,u);
480 452
        e=f;
481 453
      }
482
      for(InArcIt e(*this,b);e!=INVALID;) {
454
      for(InArcIt e(*this,v);e!=INVALID;) {
483 455
        InArcIt f=e;
484 456
        ++f;
485
        if(r && source(e)==a) erase(e);
486
        else changeTarget(e,a);
457
        if(r && source(e)==u) erase(e);
458
        else changeTarget(e,u);
487 459
        e=f;
488 460
      }
489
      erase(b);
461
      erase(v);
490 462
    }
491 463

	
492 464
    ///Split a node.
493 465

	
494
    ///This function splits a node. First a new node is added to the digraph,
495
    ///then the source of each outgoing arc of \c n is moved to this new node.
496
    ///If \c connect is \c true (this is the default value), then a new arc
497
    ///from \c n to the newly created node is also added.
466
    ///This function splits the given node. First, a new node is added
467
    ///to the digraph, then the source of each outgoing arc of node \c n
468
    ///is moved to this new node.
469
    ///If the second parameter \c connect is \c true (this is the default
470
    ///value), then a new arc from node \c n to the newly created node
471
    ///is also added.
498 472
    ///\return The newly created node.
499 473
    ///
500
    ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
501
    ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may
502
    ///be invalidated.
474
    ///\note All iterators remain valid.
503 475
    ///
504
    ///\warning This functionality cannot be used in conjunction with the
476
    ///\warning This functionality cannot be used together with the
505 477
    ///Snapshot feature.
506 478
    Node split(Node n, bool connect = true) {
507 479
      Node b = addNode();
508
      for(OutArcIt e(*this,n);e!=INVALID;) {
509
        OutArcIt f=e;
510
        ++f;
511
        changeSource(e,b);
512
        e=f;
480
      nodes[b.id].first_out=nodes[n.id].first_out;
481
      nodes[n.id].first_out=-1;
482
      for(int i=nodes[b.id].first_out; i!=-1; i=arcs[i].next_out) {
483
        arcs[i].source=b.id;
513 484
      }
514 485
      if (connect) addArc(n,b);
515 486
      return b;
516 487
    }
517 488

	
518 489
    ///Split an arc.
519 490

	
520
    ///This function splits an arc. First a new node \c b is added to
521
    ///the digraph, then the original arc is re-targeted to \c
522
    ///b. Finally an arc from \c b to the original target is added.
491
    ///This function splits the given arc. First, a new node \c v is
492
    ///added to the digraph, then the target node of the original arc
493
    ///is set to \c v. Finally, an arc from \c v to the original target
494
    ///is added.
495
    ///\return The newly created node.
523 496
    ///
524
    ///\return The newly created node.
497
    ///\note \c InArcIt iterators referencing the original arc are
498
    ///invalidated. Other iterators remain valid.
525 499
    ///
526 500
    ///\warning This functionality cannot be used together with the
527 501
    ///Snapshot feature.
528
    Node split(Arc e) {
529
      Node b = addNode();
530
      addArc(b,target(e));
531
      changeTarget(e,b);
532
      return b;
502
    Node split(Arc a) {
503
      Node v = addNode();
504
      addArc(v,target(a));
505
      changeTarget(a,v);
506
      return v;
533 507
    }
534 508

	
509
    ///Clear the digraph.
510

	
511
    ///This function erases all nodes and arcs from the digraph.
512
    ///
513
    void clear() {
514
      Parent::clear();
515
    }
516

	
517
    /// Reserve memory for nodes.
518

	
519
    /// Using this function, it is possible to avoid superfluous memory
520
    /// allocation: if you know that the digraph you want to build will
521
    /// be large (e.g. it will contain millions of nodes and/or arcs),
522
    /// then it is worth reserving space for this amount before starting
523
    /// to build the digraph.
524
    /// \sa reserveArc()
525
    void reserveNode(int n) { nodes.reserve(n); };
526

	
527
    /// Reserve memory for arcs.
528

	
529
    /// Using this function, it is possible to avoid superfluous memory
530
    /// allocation: if you know that the digraph you want to build will
531
    /// be large (e.g. it will contain millions of nodes and/or arcs),
532
    /// then it is worth reserving space for this amount before starting
533
    /// to build the digraph.
534
    /// \sa reserveNode()
535
    void reserveArc(int m) { arcs.reserve(m); };
536

	
535 537
    /// \brief Class to make a snapshot of the digraph and restore
536 538
    /// it later.
537 539
    ///
538 540
    /// Class to make a snapshot of the digraph and restore it later.
539 541
    ///
540 542
    /// The newly added nodes and arcs can be removed using the
541 543
    /// restore() function.
542 544
    ///
543
    /// \warning Arc and node deletions and other modifications (e.g.
544
    /// contracting, splitting, reversing arcs or nodes) cannot be
545
    /// \note After a state is restored, you cannot restore a later state, 
546
    /// i.e. you cannot add the removed nodes and arcs again using
547
    /// another Snapshot instance.
548
    ///
549
    /// \warning Node and arc deletions and other modifications (e.g.
550
    /// reversing, contracting, splitting arcs or nodes) cannot be
545 551
    /// restored. These events invalidate the snapshot.
552
    /// However the arcs and nodes that were added to the digraph after
553
    /// making the current snapshot can be removed without invalidating it.
546 554
    class Snapshot {
547 555
    protected:
548 556

	
549 557
      typedef Parent::NodeNotifier NodeNotifier;
550 558

	
551 559
      class NodeObserverProxy : public NodeNotifier::ObserverBase {
552 560
      public:
553 561

	
554 562
        NodeObserverProxy(Snapshot& _snapshot)
555 563
          : snapshot(_snapshot) {}
556 564

	
557 565
        using NodeNotifier::ObserverBase::attach;
558 566
        using NodeNotifier::ObserverBase::detach;
559 567
        using NodeNotifier::ObserverBase::attached;
560 568

	
561 569
      protected:
562 570

	
563 571
        virtual void add(const Node& node) {
564 572
          snapshot.addNode(node);
565 573
        }
566 574
        virtual void add(const std::vector<Node>& nodes) {
567 575
          for (int i = nodes.size() - 1; i >= 0; ++i) {
568 576
            snapshot.addNode(nodes[i]);
569 577
          }
570 578
        }
571 579
        virtual void erase(const Node& node) {
572 580
          snapshot.eraseNode(node);
573 581
        }
574 582
        virtual void erase(const std::vector<Node>& nodes) {
575 583
          for (int i = 0; i < int(nodes.size()); ++i) {
576 584
            snapshot.eraseNode(nodes[i]);
577 585
          }
578 586
        }
579 587
        virtual void build() {
580 588
          Node node;
581 589
          std::vector<Node> nodes;
582 590
          for (notifier()->first(node); node != INVALID;
583 591
               notifier()->next(node)) {
584 592
            nodes.push_back(node);
585 593
          }
586 594
          for (int i = nodes.size() - 1; i >= 0; --i) {
587 595
            snapshot.addNode(nodes[i]);
588 596
          }
589 597
        }
590 598
        virtual void clear() {
591 599
          Node node;
592 600
          for (notifier()->first(node); node != INVALID;
593 601
               notifier()->next(node)) {
594 602
            snapshot.eraseNode(node);
595 603
          }
596 604
        }
597 605

	
598 606
        Snapshot& snapshot;
599 607
      };
600 608

	
601 609
      class ArcObserverProxy : public ArcNotifier::ObserverBase {
602 610
      public:
603 611

	
604 612
        ArcObserverProxy(Snapshot& _snapshot)
605 613
          : snapshot(_snapshot) {}
606 614

	
607 615
        using ArcNotifier::ObserverBase::attach;
608 616
        using ArcNotifier::ObserverBase::detach;
609 617
        using ArcNotifier::ObserverBase::attached;
610 618

	
611 619
      protected:
612 620

	
613 621
        virtual void add(const Arc& arc) {
614 622
          snapshot.addArc(arc);
615 623
        }
616 624
        virtual void add(const std::vector<Arc>& arcs) {
617 625
          for (int i = arcs.size() - 1; i >= 0; ++i) {
618 626
            snapshot.addArc(arcs[i]);
619 627
          }
620 628
        }
621 629
        virtual void erase(const Arc& arc) {
622 630
          snapshot.eraseArc(arc);
623 631
        }
624 632
        virtual void erase(const std::vector<Arc>& arcs) {
625 633
          for (int i = 0; i < int(arcs.size()); ++i) {
626 634
            snapshot.eraseArc(arcs[i]);
627 635
          }
628 636
        }
629 637
        virtual void build() {
630 638
          Arc arc;
631 639
          std::vector<Arc> arcs;
632 640
          for (notifier()->first(arc); arc != INVALID;
633 641
               notifier()->next(arc)) {
634 642
            arcs.push_back(arc);
635 643
          }
636 644
          for (int i = arcs.size() - 1; i >= 0; --i) {
637 645
            snapshot.addArc(arcs[i]);
638 646
          }
639 647
        }
640 648
        virtual void clear() {
641 649
          Arc arc;
642 650
          for (notifier()->first(arc); arc != INVALID;
643 651
               notifier()->next(arc)) {
644 652
            snapshot.eraseArc(arc);
645 653
          }
646 654
        }
647 655

	
648 656
        Snapshot& snapshot;
649 657
      };
650 658

	
651 659
      ListDigraph *digraph;
652 660

	
653 661
      NodeObserverProxy node_observer_proxy;
654 662
      ArcObserverProxy arc_observer_proxy;
655 663

	
656 664
      std::list<Node> added_nodes;
657 665
      std::list<Arc> added_arcs;
658 666

	
659 667

	
660 668
      void addNode(const Node& node) {
661 669
        added_nodes.push_front(node);
662 670
      }
663 671
      void eraseNode(const Node& node) {
664 672
        std::list<Node>::iterator it =
665 673
          std::find(added_nodes.begin(), added_nodes.end(), node);
666 674
        if (it == added_nodes.end()) {
667 675
          clear();
668 676
          arc_observer_proxy.detach();
669 677
          throw NodeNotifier::ImmediateDetach();
670 678
        } else {
671 679
          added_nodes.erase(it);
672 680
        }
673 681
      }
674 682

	
675 683
      void addArc(const Arc& arc) {
676 684
        added_arcs.push_front(arc);
677 685
      }
678 686
      void eraseArc(const Arc& arc) {
679 687
        std::list<Arc>::iterator it =
680 688
          std::find(added_arcs.begin(), added_arcs.end(), arc);
681 689
        if (it == added_arcs.end()) {
682 690
          clear();
683 691
          node_observer_proxy.detach();
684 692
          throw ArcNotifier::ImmediateDetach();
685 693
        } else {
686 694
          added_arcs.erase(it);
687 695
        }
688 696
      }
689 697

	
690 698
      void attach(ListDigraph &_digraph) {
691 699
        digraph = &_digraph;
692 700
        node_observer_proxy.attach(digraph->notifier(Node()));
693 701
        arc_observer_proxy.attach(digraph->notifier(Arc()));
694 702
      }
695 703

	
696 704
      void detach() {
697 705
        node_observer_proxy.detach();
698 706
        arc_observer_proxy.detach();
699 707
      }
700 708

	
701 709
      bool attached() const {
702 710
        return node_observer_proxy.attached();
703 711
      }
704 712

	
705 713
      void clear() {
706 714
        added_nodes.clear();
707 715
        added_arcs.clear();
708 716
      }
709 717

	
710 718
    public:
711 719

	
712 720
      /// \brief Default constructor.
713 721
      ///
714 722
      /// Default constructor.
715
      /// To actually make a snapshot you must call save().
723
      /// You have to call save() to actually make a snapshot.
716 724
      Snapshot()
717 725
        : digraph(0), node_observer_proxy(*this),
718 726
          arc_observer_proxy(*this) {}
719 727

	
720 728
      /// \brief Constructor that immediately makes a snapshot.
721 729
      ///
722
      /// This constructor immediately makes a snapshot of the digraph.
723
      /// \param _digraph The digraph we make a snapshot of.
724
      Snapshot(ListDigraph &_digraph)
730
      /// This constructor immediately makes a snapshot of the given digraph.
731
      Snapshot(ListDigraph &gr)
725 732
        : node_observer_proxy(*this),
726 733
          arc_observer_proxy(*this) {
727
        attach(_digraph);
734
        attach(gr);
728 735
      }
729 736

	
730 737
      /// \brief Make a snapshot.
731 738
      ///
732
      /// Make a snapshot of the digraph.
733
      ///
734
      /// This function can be called more than once. In case of a repeated
739
      /// This function makes a snapshot of the given digraph.
740
      /// It can be called more than once. In case of a repeated
735 741
      /// call, the previous snapshot gets lost.
736
      /// \param _digraph The digraph we make the snapshot of.
737
      void save(ListDigraph &_digraph) {
742
      void save(ListDigraph &gr) {
738 743
        if (attached()) {
739 744
          detach();
740 745
          clear();
741 746
        }
742
        attach(_digraph);
747
        attach(gr);
743 748
      }
744 749

	
745 750
      /// \brief Undo the changes until the last snapshot.
746
      //
747
      /// Undo the changes until the last snapshot created by save().
751
      ///
752
      /// This function undos the changes until the last snapshot
753
      /// created by save() or Snapshot(ListDigraph&).
754
      ///
755
      /// \warning This method invalidates the snapshot, i.e. repeated
756
      /// restoring is not supported unless you call save() again.
748 757
      void restore() {
749 758
        detach();
750 759
        for(std::list<Arc>::iterator it = added_arcs.begin();
751 760
            it != added_arcs.end(); ++it) {
752 761
          digraph->erase(*it);
753 762
        }
754 763
        for(std::list<Node>::iterator it = added_nodes.begin();
755 764
            it != added_nodes.end(); ++it) {
756 765
          digraph->erase(*it);
757 766
        }
758 767
        clear();
759 768
      }
760 769

	
761
      /// \brief Gives back true when the snapshot is valid.
770
      /// \brief Returns \c true if the snapshot is valid.
762 771
      ///
763
      /// Gives back true when the snapshot is valid.
772
      /// This function returns \c true if the snapshot is valid.
764 773
      bool valid() const {
765 774
        return attached();
766 775
      }
767 776
    };
768 777

	
769 778
  };
770 779

	
771 780
  ///@}
772 781

	
773 782
  class ListGraphBase {
774 783

	
775 784
  protected:
776 785

	
777 786
    struct NodeT {
778 787
      int first_out;
779 788
      int prev, next;
780 789
    };
781 790

	
782 791
    struct ArcT {
783 792
      int target;
784 793
      int prev_out, next_out;
785 794
    };
786 795

	
787 796
    std::vector<NodeT> nodes;
788 797

	
789 798
    int first_node;
790 799

	
791 800
    int first_free_node;
792 801

	
793 802
    std::vector<ArcT> arcs;
794 803

	
795 804
    int first_free_arc;
796 805

	
797 806
  public:
798 807

	
799
    typedef ListGraphBase Digraph;
800

	
801
    class Node;
802
    class Arc;
803
    class Edge;
808
    typedef ListGraphBase Graph;
804 809

	
805 810
    class Node {
806 811
      friend class ListGraphBase;
807 812
    protected:
808 813

	
809 814
      int id;
810 815
      explicit Node(int pid) { id = pid;}
811 816

	
812 817
    public:
813 818
      Node() {}
814 819
      Node (Invalid) { id = -1; }
815 820
      bool operator==(const Node& node) const {return id == node.id;}
816 821
      bool operator!=(const Node& node) const {return id != node.id;}
817 822
      bool operator<(const Node& node) const {return id < node.id;}
818 823
    };
819 824

	
820 825
    class Edge {
821 826
      friend class ListGraphBase;
822 827
    protected:
823 828

	
824 829
      int id;
825 830
      explicit Edge(int pid) { id = pid;}
826 831

	
827 832
    public:
828 833
      Edge() {}
829 834
      Edge (Invalid) { id = -1; }
830 835
      bool operator==(const Edge& edge) const {return id == edge.id;}
831 836
      bool operator!=(const Edge& edge) const {return id != edge.id;}
832 837
      bool operator<(const Edge& edge) const {return id < edge.id;}
833 838
    };
834 839

	
835 840
    class Arc {
836 841
      friend class ListGraphBase;
837 842
    protected:
838 843

	
839 844
      int id;
840 845
      explicit Arc(int pid) { id = pid;}
841 846

	
842 847
    public:
843 848
      operator Edge() const {
844 849
        return id != -1 ? edgeFromId(id / 2) : INVALID;
845 850
      }
846 851

	
847 852
      Arc() {}
848 853
      Arc (Invalid) { id = -1; }
849 854
      bool operator==(const Arc& arc) const {return id == arc.id;}
850 855
      bool operator!=(const Arc& arc) const {return id != arc.id;}
851 856
      bool operator<(const Arc& arc) const {return id < arc.id;}
852 857
    };
853 858

	
854

	
855

	
856 859
    ListGraphBase()
857 860
      : nodes(), first_node(-1),
858 861
        first_free_node(-1), arcs(), first_free_arc(-1) {}
859 862

	
860 863

	
861 864
    int maxNodeId() const { return nodes.size()-1; }
862 865
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
863 866
    int maxArcId() const { return arcs.size()-1; }
864 867

	
865 868
    Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); }
866 869
    Node target(Arc e) const { return Node(arcs[e.id].target); }
867 870

	
868 871
    Node u(Edge e) const { return Node(arcs[2 * e.id].target); }
869 872
    Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); }
870 873

	
871 874
    static bool direction(Arc e) {
872 875
      return (e.id & 1) == 1;
873 876
    }
874 877

	
875 878
    static Arc direct(Edge e, bool d) {
876 879
      return Arc(e.id * 2 + (d ? 1 : 0));
877 880
    }
878 881

	
879 882
    void first(Node& node) const {
880 883
      node.id = first_node;
881 884
    }
882 885

	
883 886
    void next(Node& node) const {
884 887
      node.id = nodes[node.id].next;
885 888
    }
886 889

	
887 890
    void first(Arc& e) const {
888 891
      int n = first_node;
889 892
      while (n != -1 && nodes[n].first_out == -1) {
890 893
        n = nodes[n].next;
891 894
      }
892 895
      e.id = (n == -1) ? -1 : nodes[n].first_out;
893 896
    }
894 897

	
895 898
    void next(Arc& e) const {
896 899
      if (arcs[e.id].next_out != -1) {
897 900
        e.id = arcs[e.id].next_out;
898 901
      } else {
899 902
        int n = nodes[arcs[e.id ^ 1].target].next;
900 903
        while(n != -1 && nodes[n].first_out == -1) {
901 904
          n = nodes[n].next;
902 905
        }
903 906
        e.id = (n == -1) ? -1 : nodes[n].first_out;
904 907
      }
905 908
    }
906 909

	
907 910
    void first(Edge& e) const {
908 911
      int n = first_node;
909 912
      while (n != -1) {
910 913
        e.id = nodes[n].first_out;
911 914
        while ((e.id & 1) != 1) {
912 915
          e.id = arcs[e.id].next_out;
913 916
        }
914 917
        if (e.id != -1) {
915 918
          e.id /= 2;
916 919
          return;
917 920
        }
918 921
        n = nodes[n].next;
919 922
      }
920 923
      e.id = -1;
921 924
    }
922 925

	
923 926
    void next(Edge& e) const {
924 927
      int n = arcs[e.id * 2].target;
925 928
      e.id = arcs[(e.id * 2) | 1].next_out;
926 929
      while ((e.id & 1) != 1) {
927 930
        e.id = arcs[e.id].next_out;
928 931
      }
929 932
      if (e.id != -1) {
930 933
        e.id /= 2;
931 934
        return;
932 935
      }
933 936
      n = nodes[n].next;
934 937
      while (n != -1) {
935 938
        e.id = nodes[n].first_out;
936 939
        while ((e.id & 1) != 1) {
937 940
          e.id = arcs[e.id].next_out;
938 941
        }
939 942
        if (e.id != -1) {
940 943
          e.id /= 2;
941 944
          return;
942 945
        }
943 946
        n = nodes[n].next;
944 947
      }
945 948
      e.id = -1;
946 949
    }
947 950

	
948 951
    void firstOut(Arc &e, const Node& v) const {
949 952
      e.id = nodes[v.id].first_out;
950 953
    }
951 954
    void nextOut(Arc &e) const {
... ...
@@ -1074,483 +1077,509 @@
1074 1077
      nodes[n].next = first_free_node;
1075 1078
      first_free_node = n;
1076 1079
      nodes[n].prev = -2;
1077 1080
    }
1078 1081

	
1079 1082
    void erase(const Edge& edge) {
1080 1083
      int n = edge.id * 2;
1081 1084

	
1082 1085
      if (arcs[n].next_out != -1) {
1083 1086
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
1084 1087
      }
1085 1088

	
1086 1089
      if (arcs[n].prev_out != -1) {
1087 1090
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
1088 1091
      } else {
1089 1092
        nodes[arcs[n | 1].target].first_out = arcs[n].next_out;
1090 1093
      }
1091 1094

	
1092 1095
      if (arcs[n | 1].next_out != -1) {
1093 1096
        arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
1094 1097
      }
1095 1098

	
1096 1099
      if (arcs[n | 1].prev_out != -1) {
1097 1100
        arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
1098 1101
      } else {
1099 1102
        nodes[arcs[n].target].first_out = arcs[n | 1].next_out;
1100 1103
      }
1101 1104

	
1102 1105
      arcs[n].next_out = first_free_arc;
1103 1106
      first_free_arc = n;
1104 1107
      arcs[n].prev_out = -2;
1105 1108
      arcs[n | 1].prev_out = -2;
1106 1109

	
1107 1110
    }
1108 1111

	
1109 1112
    void clear() {
1110 1113
      arcs.clear();
1111 1114
      nodes.clear();
1112 1115
      first_node = first_free_node = first_free_arc = -1;
1113 1116
    }
1114 1117

	
1115 1118
  protected:
1116 1119

	
1117 1120
    void changeV(Edge e, Node n) {
1118 1121
      if(arcs[2 * e.id].next_out != -1) {
1119 1122
        arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out;
1120 1123
      }
1121 1124
      if(arcs[2 * e.id].prev_out != -1) {
1122 1125
        arcs[arcs[2 * e.id].prev_out].next_out =
1123 1126
          arcs[2 * e.id].next_out;
1124 1127
      } else {
1125 1128
        nodes[arcs[(2 * e.id) | 1].target].first_out =
1126 1129
          arcs[2 * e.id].next_out;
1127 1130
      }
1128 1131

	
1129 1132
      if (nodes[n.id].first_out != -1) {
1130 1133
        arcs[nodes[n.id].first_out].prev_out = 2 * e.id;
1131 1134
      }
1132 1135
      arcs[(2 * e.id) | 1].target = n.id;
1133 1136
      arcs[2 * e.id].prev_out = -1;
1134 1137
      arcs[2 * e.id].next_out = nodes[n.id].first_out;
1135 1138
      nodes[n.id].first_out = 2 * e.id;
1136 1139
    }
1137 1140

	
1138 1141
    void changeU(Edge e, Node n) {
1139 1142
      if(arcs[(2 * e.id) | 1].next_out != -1) {
1140 1143
        arcs[arcs[(2 * e.id) | 1].next_out].prev_out =
1141 1144
          arcs[(2 * e.id) | 1].prev_out;
1142 1145
      }
1143 1146
      if(arcs[(2 * e.id) | 1].prev_out != -1) {
1144 1147
        arcs[arcs[(2 * e.id) | 1].prev_out].next_out =
1145 1148
          arcs[(2 * e.id) | 1].next_out;
1146 1149
      } else {
1147 1150
        nodes[arcs[2 * e.id].target].first_out =
1148 1151
          arcs[(2 * e.id) | 1].next_out;
1149 1152
      }
1150 1153

	
1151 1154
      if (nodes[n.id].first_out != -1) {
1152 1155
        arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1);
1153 1156
      }
1154 1157
      arcs[2 * e.id].target = n.id;
1155 1158
      arcs[(2 * e.id) | 1].prev_out = -1;
1156 1159
      arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out;
1157 1160
      nodes[n.id].first_out = ((2 * e.id) | 1);
1158 1161
    }
1159 1162

	
1160 1163
  };
1161 1164

	
1162 1165
  typedef GraphExtender<ListGraphBase> ExtendedListGraphBase;
1163 1166

	
1164 1167

	
1165 1168
  /// \addtogroup graphs
1166 1169
  /// @{
1167 1170

	
1168 1171
  ///A general undirected graph structure.
1169 1172

	
1170
  ///\ref ListGraph is a simple and fast <em>undirected graph</em>
1171
  ///implementation based on static linked lists that are stored in
1173
  ///\ref ListGraph is a versatile and fast undirected graph
1174
  ///implementation based on linked lists that are stored in
1172 1175
  ///\c std::vector structures.
1173 1176
  ///
1174
  ///It conforms to the \ref concepts::Graph "Graph concept" and it
1175
  ///also provides several useful additional functionalities.
1176
  ///Most of the member functions and nested classes are documented
1177
  ///This type fully conforms to the \ref concepts::Graph "Graph concept"
1178
  ///and it also provides several useful additional functionalities.
1179
  ///Most of its member functions and nested classes are documented
1177 1180
  ///only in the concept class.
1178 1181
  ///
1179
  ///An important extra feature of this graph implementation is that
1180
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
1181
  ///
1182 1182
  ///\sa concepts::Graph
1183
  ///\sa ListDigraph
1184
  class ListGraph : public ExtendedListGraphBase {
1185
    typedef ExtendedListGraphBase Parent;
1183 1186

	
1184
  class ListGraph : public ExtendedListGraphBase {
1185 1187
  private:
1186
    ///ListGraph is \e not copy constructible. Use copyGraph() instead.
1187

	
1188
    ///ListGraph is \e not copy constructible. Use copyGraph() instead.
1189
    ///
1188
    /// Graphs are \e not copy constructible. Use GraphCopy instead.
1190 1189
    ListGraph(const ListGraph &) :ExtendedListGraphBase()  {};
1191
    ///\brief Assignment of ListGraph to another one is \e not allowed.
1192
    ///Use copyGraph() instead.
1193

	
1194
    ///Assignment of ListGraph to another one is \e not allowed.
1195
    ///Use copyGraph() instead.
1190
    /// \brief Assignment of a graph to another one is \e not allowed.
1191
    /// Use GraphCopy instead.
1196 1192
    void operator=(const ListGraph &) {}
1197 1193
  public:
1198 1194
    /// Constructor
1199 1195

	
1200 1196
    /// Constructor.
1201 1197
    ///
1202 1198
    ListGraph() {}
1203 1199

	
1204
    typedef ExtendedListGraphBase Parent;
1205

	
1206 1200
    typedef Parent::OutArcIt IncEdgeIt;
1207 1201

	
1208 1202
    /// \brief Add a new node to the graph.
1209 1203
    ///
1210
    /// Add a new node to the graph.
1211
    /// \return the new node.
1204
    /// This function adds a new node to the graph.
1205
    /// \return The new node.
1212 1206
    Node addNode() { return Parent::addNode(); }
1213 1207

	
1214 1208
    /// \brief Add a new edge to the graph.
1215 1209
    ///
1216
    /// Add a new edge to the graph with source node \c s
1217
    /// and target node \c t.
1218
    /// \return the new edge.
1219
    Edge addEdge(const Node& s, const Node& t) {
1220
      return Parent::addEdge(s, t);
1210
    /// This function adds a new edge to the graph between nodes
1211
    /// \c u and \c v with inherent orientation from node \c u to
1212
    /// node \c v.
1213
    /// \return The new edge.
1214
    Edge addEdge(Node u, Node v) {
1215
      return Parent::addEdge(u, v);
1221 1216
    }
1222 1217

	
1223
    /// \brief Erase a node from the graph.
1218
    ///\brief Erase a node from the graph.
1224 1219
    ///
1225
    /// Erase a node from the graph.
1220
    /// This function erases the given node from the graph.
1221
    void erase(Node n) { Parent::erase(n); }
1222

	
1223
    ///\brief Erase an edge from the graph.
1226 1224
    ///
1227
    void erase(const Node& n) { Parent::erase(n); }
1228

	
1229
    /// \brief Erase an edge from the graph.
1230
    ///
1231
    /// Erase an edge from the graph.
1232
    ///
1233
    void erase(const Edge& e) { Parent::erase(e); }
1225
    /// This function erases the given edge from the graph.
1226
    void erase(Edge e) { Parent::erase(e); }
1234 1227
    /// Node validity check
1235 1228

	
1236
    /// This function gives back true if the given node is valid,
1237
    /// ie. it is a real node of the graph.
1229
    /// This function gives back \c true if the given node is valid,
1230
    /// i.e. it is a real node of the graph.
1238 1231
    ///
1239
    /// \warning A Node pointing to a removed item
1240
    /// could become valid again later if new nodes are
1232
    /// \warning A removed node could become valid again if new nodes are
1241 1233
    /// added to the graph.
1242 1234
    bool valid(Node n) const { return Parent::valid(n); }
1235
    /// Edge validity check
1236

	
1237
    /// This function gives back \c true if the given edge is valid,
1238
    /// i.e. it is a real edge of the graph.
1239
    ///
1240
    /// \warning A removed edge could become valid again if new edges are
1241
    /// added to the graph.
1242
    bool valid(Edge e) const { return Parent::valid(e); }
1243 1243
    /// Arc validity check
1244 1244

	
1245
    /// This function gives back true if the given arc is valid,
1246
    /// ie. it is a real arc of the graph.
1245
    /// This function gives back \c true if the given arc is valid,
1246
    /// i.e. it is a real arc of the graph.
1247 1247
    ///
1248
    /// \warning An Arc pointing to a removed item
1249
    /// could become valid again later if new edges are
1248
    /// \warning A removed arc could become valid again if new edges are
1250 1249
    /// added to the graph.
1251 1250
    bool valid(Arc a) const { return Parent::valid(a); }
1252
    /// Edge validity check
1253 1251

	
1254
    /// This function gives back true if the given edge is valid,
1255
    /// ie. it is a real arc of the graph.
1252
    /// \brief Change the first node of an edge.
1256 1253
    ///
1257
    /// \warning A Edge pointing to a removed item
1258
    /// could become valid again later if new edges are
1259
    /// added to the graph.
1260
    bool valid(Edge e) const { return Parent::valid(e); }
1261
    /// \brief Change the end \c u of \c e to \c n
1254
    /// This function changes the first node of the given edge \c e to \c n.
1262 1255
    ///
1263
    /// This function changes the end \c u of \c e to node \c n.
1264
    ///
1265
    ///\note The <tt>EdgeIt</tt>s and <tt>ArcIt</tt>s referencing the
1266
    ///changed edge are invalidated and if the changed node is the
1267
    ///base node of an iterator then this iterator is also
1268
    ///invalidated.
1256
    ///\note \c EdgeIt and \c ArcIt iterators referencing the
1257
    ///changed edge are invalidated and all other iterators whose
1258
    ///base node is the changed node are also invalidated.
1269 1259
    ///
1270 1260
    ///\warning This functionality cannot be used together with the
1271 1261
    ///Snapshot feature.
1272 1262
    void changeU(Edge e, Node n) {
1273 1263
      Parent::changeU(e,n);
1274 1264
    }
1275
    /// \brief Change the end \c v of \c e to \c n
1265
    /// \brief Change the second node of an edge.
1276 1266
    ///
1277
    /// This function changes the end \c v of \c e to \c n.
1267
    /// This function changes the second node of the given edge \c e to \c n.
1278 1268
    ///
1279
    ///\note The <tt>EdgeIt</tt>s referencing the changed edge remain
1280
    ///valid, however <tt>ArcIt</tt>s and if the changed node is the
1281
    ///base node of an iterator then this iterator is invalidated.
1269
    ///\note \c EdgeIt iterators referencing the changed edge remain
1270
    ///valid, however \c ArcIt iterators referencing the changed edge and
1271
    ///all other iterators whose base node is the changed node are also
1272
    ///invalidated.
1282 1273
    ///
1283 1274
    ///\warning This functionality cannot be used together with the
1284 1275
    ///Snapshot feature.
1285 1276
    void changeV(Edge e, Node n) {
1286 1277
      Parent::changeV(e,n);
1287 1278
    }
1279

	
1288 1280
    /// \brief Contract two nodes.
1289 1281
    ///
1290
    /// This function contracts two nodes.
1291
    /// Node \p b will be removed but instead of deleting
1292
    /// its neighboring arcs, they will be joined to \p a.
1293
    /// The last parameter \p r controls whether to remove loops. \c true
1294
    /// means that loops will be removed.
1282
    /// This function contracts the given two nodes.
1283
    /// Node \c b is removed, but instead of deleting
1284
    /// its incident edges, they are joined to node \c a.
1285
    /// If the last parameter \c r is \c true (this is the default value),
1286
    /// then the newly created loops are removed.
1295 1287
    ///
1296
    /// \note The <tt>ArcIt</tt>s referencing a moved arc remain
1297
    /// valid.
1288
    /// \note The moved edges are joined to node \c a using changeU()
1289
    /// or changeV(), thus all edge and arc iterators whose base node is
1290
    /// \c b are invalidated.
1291
    /// Moreover all iterators referencing node \c b or the removed 
1292
    /// loops are also invalidated. Other iterators remain valid.
1298 1293
    ///
1299 1294
    ///\warning This functionality cannot be used together with the
1300 1295
    ///Snapshot feature.
1301 1296
    void contract(Node a, Node b, bool r = true) {
1302 1297
      for(IncEdgeIt e(*this, b); e!=INVALID;) {
1303 1298
        IncEdgeIt f = e; ++f;
1304 1299
        if (r && runningNode(e) == a) {
1305 1300
          erase(e);
1306 1301
        } else if (u(e) == b) {
1307 1302
          changeU(e, a);
1308 1303
        } else {
1309 1304
          changeV(e, a);
1310 1305
        }
1311 1306
        e = f;
1312 1307
      }
1313 1308
      erase(b);
1314 1309
    }
1315 1310

	
1311
    ///Clear the graph.
1312

	
1313
    ///This function erases all nodes and arcs from the graph.
1314
    ///
1315
    void clear() {
1316
      Parent::clear();
1317
    }
1318

	
1319
    /// Reserve memory for nodes.
1320

	
1321
    /// Using this function, it is possible to avoid superfluous memory
1322
    /// allocation: if you know that the graph you want to build will
1323
    /// be large (e.g. it will contain millions of nodes and/or edges),
1324
    /// then it is worth reserving space for this amount before starting
1325
    /// to build the graph.
1326
    /// \sa reserveEdge()
1327
    void reserveNode(int n) { nodes.reserve(n); };
1328

	
1329
    /// Reserve memory for edges.
1330

	
1331
    /// Using this function, it is possible to avoid superfluous memory
1332
    /// allocation: if you know that the graph you want to build will
1333
    /// be large (e.g. it will contain millions of nodes and/or edges),
1334
    /// then it is worth reserving space for this amount before starting
1335
    /// to build the graph.
1336
    /// \sa reserveNode()
1337
    void reserveEdge(int m) { arcs.reserve(2 * m); };
1316 1338

	
1317 1339
    /// \brief Class to make a snapshot of the graph and restore
1318 1340
    /// it later.
1319 1341
    ///
1320 1342
    /// Class to make a snapshot of the graph and restore it later.
1321 1343
    ///
1322 1344
    /// The newly added nodes and edges can be removed
1323 1345
    /// using the restore() function.
1324 1346
    ///
1325
    /// \warning Edge and node deletions and other modifications
1326
    /// (e.g. changing nodes of edges, contracting nodes) cannot be
1327
    /// restored. These events invalidate the snapshot.
1347
    /// \note After a state is restored, you cannot restore a later state, 
1348
    /// i.e. you cannot add the removed nodes and edges again using
1349
    /// another Snapshot instance.
1350
    ///
1351
    /// \warning Node and edge deletions and other modifications
1352
    /// (e.g. changing the end-nodes of edges or contracting nodes)
1353
    /// cannot be restored. These events invalidate the snapshot.
1354
    /// However the edges and nodes that were added to the graph after
1355
    /// making the current snapshot can be removed without invalidating it.
1328 1356
    class Snapshot {
1329 1357
    protected:
1330 1358

	
1331 1359
      typedef Parent::NodeNotifier NodeNotifier;
1332 1360

	
1333 1361
      class NodeObserverProxy : public NodeNotifier::ObserverBase {
1334 1362
      public:
1335 1363

	
1336 1364
        NodeObserverProxy(Snapshot& _snapshot)
1337 1365
          : snapshot(_snapshot) {}
1338 1366

	
1339 1367
        using NodeNotifier::ObserverBase::attach;
1340 1368
        using NodeNotifier::ObserverBase::detach;
1341 1369
        using NodeNotifier::ObserverBase::attached;
1342 1370

	
1343 1371
      protected:
1344 1372

	
1345 1373
        virtual void add(const Node& node) {
1346 1374
          snapshot.addNode(node);
1347 1375
        }
1348 1376
        virtual void add(const std::vector<Node>& nodes) {
1349 1377
          for (int i = nodes.size() - 1; i >= 0; ++i) {
1350 1378
            snapshot.addNode(nodes[i]);
1351 1379
          }
1352 1380
        }
1353 1381
        virtual void erase(const Node& node) {
1354 1382
          snapshot.eraseNode(node);
1355 1383
        }
1356 1384
        virtual void erase(const std::vector<Node>& nodes) {
1357 1385
          for (int i = 0; i < int(nodes.size()); ++i) {
1358 1386
            snapshot.eraseNode(nodes[i]);
1359 1387
          }
1360 1388
        }
1361 1389
        virtual void build() {
1362 1390
          Node node;
1363 1391
          std::vector<Node> nodes;
1364 1392
          for (notifier()->first(node); node != INVALID;
1365 1393
               notifier()->next(node)) {
1366 1394
            nodes.push_back(node);
1367 1395
          }
1368 1396
          for (int i = nodes.size() - 1; i >= 0; --i) {
1369 1397
            snapshot.addNode(nodes[i]);
1370 1398
          }
1371 1399
        }
1372 1400
        virtual void clear() {
1373 1401
          Node node;
1374 1402
          for (notifier()->first(node); node != INVALID;
1375 1403
               notifier()->next(node)) {
1376 1404
            snapshot.eraseNode(node);
1377 1405
          }
1378 1406
        }
1379 1407

	
1380 1408
        Snapshot& snapshot;
1381 1409
      };
1382 1410

	
1383 1411
      class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
1384 1412
      public:
1385 1413

	
1386 1414
        EdgeObserverProxy(Snapshot& _snapshot)
1387 1415
          : snapshot(_snapshot) {}
1388 1416

	
1389 1417
        using EdgeNotifier::ObserverBase::attach;
1390 1418
        using EdgeNotifier::ObserverBase::detach;
1391 1419
        using EdgeNotifier::ObserverBase::attached;
1392 1420

	
1393 1421
      protected:
1394 1422

	
1395 1423
        virtual void add(const Edge& edge) {
1396 1424
          snapshot.addEdge(edge);
1397 1425
        }
1398 1426
        virtual void add(const std::vector<Edge>& edges) {
1399 1427
          for (int i = edges.size() - 1; i >= 0; ++i) {
1400 1428
            snapshot.addEdge(edges[i]);
1401 1429
          }
1402 1430
        }
1403 1431
        virtual void erase(const Edge& edge) {
1404 1432
          snapshot.eraseEdge(edge);
1405 1433
        }
1406 1434
        virtual void erase(const std::vector<Edge>& edges) {
1407 1435
          for (int i = 0; i < int(edges.size()); ++i) {
1408 1436
            snapshot.eraseEdge(edges[i]);
1409 1437
          }
1410 1438
        }
1411 1439
        virtual void build() {
1412 1440
          Edge edge;
1413 1441
          std::vector<Edge> edges;
1414 1442
          for (notifier()->first(edge); edge != INVALID;
1415 1443
               notifier()->next(edge)) {
1416 1444
            edges.push_back(edge);
1417 1445
          }
1418 1446
          for (int i = edges.size() - 1; i >= 0; --i) {
1419 1447
            snapshot.addEdge(edges[i]);
1420 1448
          }
1421 1449
        }
1422 1450
        virtual void clear() {
1423 1451
          Edge edge;
1424 1452
          for (notifier()->first(edge); edge != INVALID;
1425 1453
               notifier()->next(edge)) {
1426 1454
            snapshot.eraseEdge(edge);
1427 1455
          }
1428 1456
        }
1429 1457

	
1430 1458
        Snapshot& snapshot;
1431 1459
      };
1432 1460

	
1433 1461
      ListGraph *graph;
1434 1462

	
1435 1463
      NodeObserverProxy node_observer_proxy;
1436 1464
      EdgeObserverProxy edge_observer_proxy;
1437 1465

	
1438 1466
      std::list<Node> added_nodes;
1439 1467
      std::list<Edge> added_edges;
1440 1468

	
1441 1469

	
1442 1470
      void addNode(const Node& node) {
1443 1471
        added_nodes.push_front(node);
1444 1472
      }
1445 1473
      void eraseNode(const Node& node) {
1446 1474
        std::list<Node>::iterator it =
1447 1475
          std::find(added_nodes.begin(), added_nodes.end(), node);
1448 1476
        if (it == added_nodes.end()) {
1449 1477
          clear();
1450 1478
          edge_observer_proxy.detach();
1451 1479
          throw NodeNotifier::ImmediateDetach();
1452 1480
        } else {
1453 1481
          added_nodes.erase(it);
1454 1482
        }
1455 1483
      }
1456 1484

	
1457 1485
      void addEdge(const Edge& edge) {
1458 1486
        added_edges.push_front(edge);
1459 1487
      }
1460 1488
      void eraseEdge(const Edge& edge) {
1461 1489
        std::list<Edge>::iterator it =
1462 1490
          std::find(added_edges.begin(), added_edges.end(), edge);
1463 1491
        if (it == added_edges.end()) {
1464 1492
          clear();
1465 1493
          node_observer_proxy.detach();
1466 1494
          throw EdgeNotifier::ImmediateDetach();
1467 1495
        } else {
1468 1496
          added_edges.erase(it);
1469 1497
        }
1470 1498
      }
1471 1499

	
1472 1500
      void attach(ListGraph &_graph) {
1473 1501
        graph = &_graph;
1474 1502
        node_observer_proxy.attach(graph->notifier(Node()));
1475 1503
        edge_observer_proxy.attach(graph->notifier(Edge()));
1476 1504
      }
1477 1505

	
1478 1506
      void detach() {
1479 1507
        node_observer_proxy.detach();
1480 1508
        edge_observer_proxy.detach();
1481 1509
      }
1482 1510

	
1483 1511
      bool attached() const {
1484 1512
        return node_observer_proxy.attached();
1485 1513
      }
1486 1514

	
1487 1515
      void clear() {
1488 1516
        added_nodes.clear();
1489 1517
        added_edges.clear();
1490 1518
      }
1491 1519

	
1492 1520
    public:
1493 1521

	
1494 1522
      /// \brief Default constructor.
1495 1523
      ///
1496 1524
      /// Default constructor.
1497
      /// To actually make a snapshot you must call save().
1525
      /// You have to call save() to actually make a snapshot.
1498 1526
      Snapshot()
1499 1527
        : graph(0), node_observer_proxy(*this),
1500 1528
          edge_observer_proxy(*this) {}
1501 1529

	
1502 1530
      /// \brief Constructor that immediately makes a snapshot.
1503 1531
      ///
1504
      /// This constructor immediately makes a snapshot of the graph.
1505
      /// \param _graph The graph we make a snapshot of.
1506
      Snapshot(ListGraph &_graph)
1532
      /// This constructor immediately makes a snapshot of the given graph.
1533
      Snapshot(ListGraph &gr)
1507 1534
        : node_observer_proxy(*this),
1508 1535
          edge_observer_proxy(*this) {
1509
        attach(_graph);
1536
        attach(gr);
1510 1537
      }
1511 1538

	
1512 1539
      /// \brief Make a snapshot.
1513 1540
      ///
1514
      /// Make a snapshot of the graph.
1515
      ///
1516
      /// This function can be called more than once. In case of a repeated
1541
      /// This function makes a snapshot of the given graph.
1542
      /// It can be called more than once. In case of a repeated
1517 1543
      /// call, the previous snapshot gets lost.
1518
      /// \param _graph The graph we make the snapshot of.
1519
      void save(ListGraph &_graph) {
1544
      void save(ListGraph &gr) {
1520 1545
        if (attached()) {
1521 1546
          detach();
1522 1547
          clear();
1523 1548
        }
1524
        attach(_graph);
1549
        attach(gr);
1525 1550
      }
1526 1551

	
1527 1552
      /// \brief Undo the changes until the last snapshot.
1528
      //
1529
      /// Undo the changes until the last snapshot created by save().
1553
      ///
1554
      /// This function undos the changes until the last snapshot
1555
      /// created by save() or Snapshot(ListGraph&).
1556
      ///
1557
      /// \warning This method invalidates the snapshot, i.e. repeated
1558
      /// restoring is not supported unless you call save() again.
1530 1559
      void restore() {
1531 1560
        detach();
1532 1561
        for(std::list<Edge>::iterator it = added_edges.begin();
1533 1562
            it != added_edges.end(); ++it) {
1534 1563
          graph->erase(*it);
1535 1564
        }
1536 1565
        for(std::list<Node>::iterator it = added_nodes.begin();
1537 1566
            it != added_nodes.end(); ++it) {
1538 1567
          graph->erase(*it);
1539 1568
        }
1540 1569
        clear();
1541 1570
      }
1542 1571

	
1543
      /// \brief Gives back true when the snapshot is valid.
1572
      /// \brief Returns \c true if the snapshot is valid.
1544 1573
      ///
1545
      /// Gives back true when the snapshot is valid.
1574
      /// This function returns \c true if the snapshot is valid.
1546 1575
      bool valid() const {
1547 1576
        return attached();
1548 1577
      }
1549 1578
    };
1550 1579
  };
1551 1580

	
1552 1581
  /// @}
1553 1582
} //namespace lemon
1554 1583

	
1555 1584

	
1556 1585
#endif
Ignore white space 6 line context
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_LP_H
20 20
#define LEMON_LP_H
21 21

	
22 22
#include<lemon/config.h>
23 23

	
24 24

	
25
#ifdef HAVE_GLPK
25
#ifdef LEMON_HAVE_GLPK
26 26
#include <lemon/glpk.h>
27
#elif HAVE_CPLEX
27
#elif LEMON_HAVE_CPLEX
28 28
#include <lemon/cplex.h>
29
#elif HAVE_SOPLEX
29
#elif LEMON_HAVE_SOPLEX
30 30
#include <lemon/soplex.h>
31
#elif HAVE_CLP
31
#elif LEMON_HAVE_CLP
32 32
#include <lemon/clp.h>
33 33
#endif
34 34

	
35 35
///\file
36 36
///\brief Defines a default LP solver
37 37
///\ingroup lp_group
38 38
namespace lemon {
39 39

	
40 40
#ifdef DOXYGEN
41 41
  ///The default LP solver identifier
42 42

	
43 43
  ///The default LP solver identifier.
44 44
  ///\ingroup lp_group
45 45
  ///
46 46
  ///Currently, the possible values are \c GLPK, \c CPLEX,
47 47
  ///\c SOPLEX or \c CLP
48 48
#define LEMON_DEFAULT_LP SOLVER
49 49
  ///The default LP solver
50 50

	
51 51
  ///The default LP solver.
52 52
  ///\ingroup lp_group
53 53
  ///
54 54
  ///Currently, it is either \c GlpkLp, \c CplexLp, \c SoplexLp or \c ClpLp
55 55
  typedef GlpkLp Lp;
56 56

	
57 57
  ///The default MIP solver identifier
58 58

	
59 59
  ///The default MIP solver identifier.
60 60
  ///\ingroup lp_group
61 61
  ///
62 62
  ///Currently, the possible values are \c GLPK or \c CPLEX
63 63
#define LEMON_DEFAULT_MIP SOLVER
64 64
  ///The default MIP solver.
65 65

	
66 66
  ///The default MIP solver.
67 67
  ///\ingroup lp_group
68 68
  ///
69 69
  ///Currently, it is either \c GlpkMip or \c CplexMip
70 70
  typedef GlpkMip Mip;
71 71
#else
72
#ifdef HAVE_GLPK
72
#ifdef LEMON_HAVE_GLPK
73 73
# define LEMON_DEFAULT_LP GLPK
74 74
  typedef GlpkLp Lp;
75 75
# define LEMON_DEFAULT_MIP GLPK
76 76
  typedef GlpkMip Mip;
77
#elif HAVE_CPLEX
77
#elif LEMON_HAVE_CPLEX
78 78
# define LEMON_DEFAULT_LP CPLEX
79 79
  typedef CplexLp Lp;
80 80
# define LEMON_DEFAULT_MIP CPLEX
81 81
  typedef CplexMip Mip;
82
#elif HAVE_SOPLEX
82
#elif LEMON_HAVE_SOPLEX
83 83
# define DEFAULT_LP SOPLEX
84 84
  typedef SoplexLp Lp;
85
#elif HAVE_CLP
85
#elif LEMON_HAVE_CLP
86 86
# define DEFAULT_LP CLP
87 87
  typedef ClpLp Lp;  
88 88
#endif
89 89
#endif
90 90

	
91 91
} //namespace lemon
92 92

	
93 93
#endif //LEMON_LP_H
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\file
20 20
///\brief The implementation of the LP solver interface.
21 21

	
22 22
#include <lemon/lp_base.h>
23 23
namespace lemon {
24 24

	
25
  const LpBase::Value LpBase::INF = std::numeric_limits<Value>::infinity();
26
  const LpBase::Value LpBase::NaN = std::numeric_limits<Value>::quiet_NaN();
25
  const LpBase::Value LpBase::INF =
26
    std::numeric_limits<LpBase::Value>::infinity();
27
  const LpBase::Value LpBase::NaN =
28
    std::numeric_limits<LpBase::Value>::quiet_NaN();
27 29

	
28 30
} //namespace lemon
Ignore white space 6 line context
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_LP_BASE_H
20 20
#define LEMON_LP_BASE_H
21 21

	
22 22
#include<iostream>
23 23
#include<vector>
24 24
#include<map>
25 25
#include<limits>
26 26
#include<lemon/math.h>
27 27

	
28 28
#include<lemon/error.h>
29 29
#include<lemon/assert.h>
30 30

	
31 31
#include<lemon/core.h>
32 32
#include<lemon/bits/solver_bits.h>
33 33

	
34 34
///\file
35 35
///\brief The interface of the LP solver interface.
36 36
///\ingroup lp_group
37 37
namespace lemon {
38 38

	
39 39
  ///Common base class for LP and MIP solvers
40 40

	
41 41
  ///Usually this class is not used directly, please use one of the concrete
42 42
  ///implementations of the solver interface.
43 43
  ///\ingroup lp_group
44 44
  class LpBase {
45 45

	
46 46
  protected:
47 47

	
48 48
    _solver_bits::VarIndex rows;
49 49
    _solver_bits::VarIndex cols;
50 50

	
51 51
  public:
52 52

	
53 53
    ///Possible outcomes of an LP solving procedure
54 54
    enum SolveExitStatus {
55
      ///This means that the problem has been successfully solved: either
55
      /// = 0. It means that the problem has been successfully solved: either
56 56
      ///an optimal solution has been found or infeasibility/unboundedness
57 57
      ///has been proved.
58 58
      SOLVED = 0,
59
      ///Any other case (including the case when some user specified
60
      ///limit has been exceeded)
59
      /// = 1. Any other case (including the case when some user specified
60
      ///limit has been exceeded).
61 61
      UNSOLVED = 1
62 62
    };
63 63

	
64 64
    ///Direction of the optimization
65 65
    enum Sense {
66 66
      /// Minimization
67 67
      MIN,
68 68
      /// Maximization
69 69
      MAX
70 70
    };
71 71

	
72
    ///Enum for \c messageLevel() parameter
73
    enum MessageLevel {
74
      /// No output (default value).
75
      MESSAGE_NOTHING,
76
      /// Error messages only.
77
      MESSAGE_ERROR,
78
      /// Warnings.
79
      MESSAGE_WARNING,
80
      /// Normal output.
81
      MESSAGE_NORMAL,
82
      /// Verbose output.
83
      MESSAGE_VERBOSE
84
    };
85
    
86

	
72 87
    ///The floating point type used by the solver
73 88
    typedef double Value;
74 89
    ///The infinity constant
75 90
    static const Value INF;
76 91
    ///The not a number constant
77 92
    static const Value NaN;
78 93

	
79 94
    friend class Col;
80 95
    friend class ColIt;
81 96
    friend class Row;
82 97
    friend class RowIt;
83 98

	
84 99
    ///Refer to a column of the LP.
85 100

	
86 101
    ///This type is used to refer to a column of the LP.
87 102
    ///
88 103
    ///Its value remains valid and correct even after the addition or erase of
89 104
    ///other columns.
90 105
    ///
91 106
    ///\note This class is similar to other Item types in LEMON, like
92 107
    ///Node and Arc types in digraph.
93 108
    class Col {
94 109
      friend class LpBase;
95 110
    protected:
96 111
      int _id;
97 112
      explicit Col(int id) : _id(id) {}
98 113
    public:
99 114
      typedef Value ExprValue;
100 115
      typedef True LpCol;
101 116
      /// Default constructor
102 117
      
103 118
      /// \warning The default constructor sets the Col to an
104 119
      /// undefined value.
105 120
      Col() {}
106 121
      /// Invalid constructor \& conversion.
107 122
      
108 123
      /// This constructor initializes the Col to be invalid.
109 124
      /// \sa Invalid for more details.      
110 125
      Col(const Invalid&) : _id(-1) {}
111 126
      /// Equality operator
112 127

	
113 128
      /// Two \ref Col "Col"s are equal if and only if they point to
114 129
      /// the same LP column or both are invalid.
115 130
      bool operator==(Col c) const  {return _id == c._id;}
116 131
      /// Inequality operator
117 132

	
118 133
      /// \sa operator==(Col c)
119 134
      ///
120 135
      bool operator!=(Col c) const  {return _id != c._id;}
121 136
      /// Artificial ordering operator.
122 137

	
123 138
      /// To allow the use of this object in std::map or similar
124 139
      /// associative container we require this.
125 140
      ///
126 141
      /// \note This operator only have to define some strict ordering of
127 142
      /// the items; this order has nothing to do with the iteration
128 143
      /// ordering of the items.
129 144
      bool operator<(Col c) const  {return _id < c._id;}
130 145
    };
131 146

	
132 147
    ///Iterator for iterate over the columns of an LP problem
133 148

	
134 149
    /// Its usage is quite simple, for example you can count the number
135 150
    /// of columns in an LP \c lp:
136 151
    ///\code
137 152
    /// int count=0;
138 153
    /// for (LpBase::ColIt c(lp); c!=INVALID; ++c) ++count;
139 154
    ///\endcode
140 155
    class ColIt : public Col {
141 156
      const LpBase *_solver;
142 157
    public:
143 158
      /// Default constructor
144 159
      
145 160
      /// \warning The default constructor sets the iterator
146 161
      /// to an undefined value.
147 162
      ColIt() {}
148 163
      /// Sets the iterator to the first Col
149 164
      
150 165
      /// Sets the iterator to the first Col.
151 166
      ///
152 167
      ColIt(const LpBase &solver) : _solver(&solver)
153 168
      {
154 169
        _solver->cols.firstItem(_id);
155 170
      }
156 171
      /// Invalid constructor \& conversion
157 172
      
158 173
      /// Initialize the iterator to be invalid.
159 174
      /// \sa Invalid for more details.
160 175
      ColIt(const Invalid&) : Col(INVALID) {}
161 176
      /// Next column
162 177
      
163 178
      /// Assign the iterator to the next column.
164 179
      ///
165 180
      ColIt &operator++()
166 181
      {
167 182
        _solver->cols.nextItem(_id);
... ...
@@ -504,197 +519,197 @@
504 519
        operator Col() const {
505 520
          return colFromId(_it->first);
506 521
        }
507 522

	
508 523
        /// Returns the coefficient of the term
509 524
        const Value& operator*() const { return _it->second; }
510 525

	
511 526
        /// Next term
512 527
        
513 528
        /// Assign the iterator to the next term.
514 529
        ///
515 530
        ConstCoeffIt& operator++() { ++_it; return *this; }
516 531

	
517 532
        /// Equality operator
518 533
        bool operator==(Invalid) const { return _it == _end; }
519 534
        /// Inequality operator
520 535
        bool operator!=(Invalid) const { return _it != _end; }
521 536
      };
522 537

	
523 538
    };
524 539

	
525 540
    ///Linear constraint
526 541

	
527 542
    ///This data stucture represents a linear constraint in the LP.
528 543
    ///Basically it is a linear expression with a lower or an upper bound
529 544
    ///(or both). These parts of the constraint can be obtained by the member
530 545
    ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
531 546
    ///respectively.
532 547
    ///There are two ways to construct a constraint.
533 548
    ///- You can set the linear expression and the bounds directly
534 549
    ///  by the functions above.
535 550
    ///- The operators <tt>\<=</tt>, <tt>==</tt> and  <tt>\>=</tt>
536 551
    ///  are defined between expressions, or even between constraints whenever
537 552
    ///  it makes sense. Therefore if \c e and \c f are linear expressions and
538 553
    ///  \c s and \c t are numbers, then the followings are valid expressions
539 554
    ///  and thus they can be used directly e.g. in \ref addRow() whenever
540 555
    ///  it makes sense.
541 556
    ///\code
542 557
    ///  e<=s
543 558
    ///  e<=f
544 559
    ///  e==f
545 560
    ///  s<=e<=t
546 561
    ///  e>=t
547 562
    ///\endcode
548 563
    ///\warning The validity of a constraint is checked only at run
549 564
    ///time, so e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will
550 565
    ///compile, but will fail an assertion.
551 566
    class Constr
552 567
    {
553 568
    public:
554 569
      typedef LpBase::Expr Expr;
555 570
      typedef Expr::Key Key;
556 571
      typedef Expr::Value Value;
557 572

	
558 573
    protected:
559 574
      Expr _expr;
560 575
      Value _lb,_ub;
561 576
    public:
562 577
      ///\e
563 578
      Constr() : _expr(), _lb(NaN), _ub(NaN) {}
564 579
      ///\e
565 580
      Constr(Value lb, const Expr &e, Value ub) :
566 581
        _expr(e), _lb(lb), _ub(ub) {}
567 582
      Constr(const Expr &e) :
568 583
        _expr(e), _lb(NaN), _ub(NaN) {}
569 584
      ///\e
570 585
      void clear()
571 586
      {
572 587
        _expr.clear();
573 588
        _lb=_ub=NaN;
574 589
      }
575 590

	
576 591
      ///Reference to the linear expression
577 592
      Expr &expr() { return _expr; }
578 593
      ///Cont reference to the linear expression
579 594
      const Expr &expr() const { return _expr; }
580 595
      ///Reference to the lower bound.
581 596

	
582 597
      ///\return
583 598
      ///- \ref INF "INF": the constraint is lower unbounded.
584 599
      ///- \ref NaN "NaN": lower bound has not been set.
585 600
      ///- finite number: the lower bound
586 601
      Value &lowerBound() { return _lb; }
587 602
      ///The const version of \ref lowerBound()
588 603
      const Value &lowerBound() const { return _lb; }
589 604
      ///Reference to the upper bound.
590 605

	
591 606
      ///\return
592 607
      ///- \ref INF "INF": the constraint is upper unbounded.
593 608
      ///- \ref NaN "NaN": upper bound has not been set.
594 609
      ///- finite number: the upper bound
595 610
      Value &upperBound() { return _ub; }
596 611
      ///The const version of \ref upperBound()
597 612
      const Value &upperBound() const { return _ub; }
598 613
      ///Is the constraint lower bounded?
599 614
      bool lowerBounded() const {
600
        return _lb != -INF && !std::isnan(_lb);
615
        return _lb != -INF && !isNaN(_lb);
601 616
      }
602 617
      ///Is the constraint upper bounded?
603 618
      bool upperBounded() const {
604
        return _ub != INF && !std::isnan(_ub);
619
        return _ub != INF && !isNaN(_ub);
605 620
      }
606 621

	
607 622
    };
608 623

	
609 624
    ///Linear expression of rows
610 625

	
611 626
    ///This data structure represents a column of the matrix,
612 627
    ///thas is it strores a linear expression of the dual variables
613 628
    ///(\ref Row "Row"s).
614 629
    ///
615 630
    ///There are several ways to access and modify the contents of this
616 631
    ///container.
617 632
    ///\code
618 633
    ///e[v]=5;
619 634
    ///e[v]+=12;
620 635
    ///e.erase(v);
621 636
    ///\endcode
622 637
    ///or you can also iterate through its elements.
623 638
    ///\code
624 639
    ///double s=0;
625 640
    ///for(LpBase::DualExpr::ConstCoeffIt i(e);i!=INVALID;++i)
626 641
    ///  s+=*i;
627 642
    ///\endcode
628 643
    ///(This code computes the sum of all coefficients).
629 644
    ///- Numbers (<tt>double</tt>'s)
630 645
    ///and variables (\ref Row "Row"s) directly convert to an
631 646
    ///\ref DualExpr and the usual linear operations are defined, so
632 647
    ///\code
633 648
    ///v+w
634 649
    ///2*v-3.12*(v-w/2)
635 650
    ///v*2.1+(3*v+(v*12+w)*3)/2
636 651
    ///\endcode
637 652
    ///are valid \ref DualExpr dual expressions.
638 653
    ///The usual assignment operations are also defined.
639 654
    ///\code
640 655
    ///e=v+w;
641 656
    ///e+=2*v-3.12*(v-w/2);
642 657
    ///e*=3.4;
643 658
    ///e/=5;
644 659
    ///\endcode
645 660
    ///
646 661
    ///\sa Expr
647 662
    class DualExpr {
648 663
      friend class LpBase;
649 664
    public:
650 665
      /// The key type of the expression
651 666
      typedef LpBase::Row Key;
652 667
      /// The value type of the expression
653 668
      typedef LpBase::Value Value;
654 669

	
655 670
    protected:
656 671
      std::map<int, Value> comps;
657 672

	
658 673
    public:
659 674
      typedef True SolverExpr;
660 675
      /// Default constructor
661 676
      
662 677
      /// Construct an empty expression, the coefficients are
663 678
      /// initialized to zero.
664 679
      DualExpr() {}
665 680
      /// Construct an expression from a row
666 681

	
667 682
      /// Construct an expression, which has a term with \c r dual
668 683
      /// variable and 1.0 coefficient.
669 684
      DualExpr(const Row &r) {
670 685
        typedef std::map<int, Value>::value_type pair_type;
671 686
        comps.insert(pair_type(id(r), 1));
672 687
      }
673 688
      /// Returns the coefficient of the row
674 689
      Value operator[](const Row& r) const {
675 690
        std::map<int, Value>::const_iterator it = comps.find(id(r));
676 691
        if (it != comps.end()) {
677 692
          return it->second;
678 693
        } else {
679 694
          return 0;
680 695
        }
681 696
      }
682 697
      /// Returns the coefficient of the row
683 698
      Value& operator[](const Row& r) {
684 699
        return comps[id(r)];
685 700
      }
686 701
      /// Sets the coefficient of the row
687 702
      void set(const Row &r, const Value &v) {
688 703
        if (v != 0.0) {
689 704
          typedef std::map<int, Value>::value_type pair_type;
690 705
          comps.insert(pair_type(id(r), v));
691 706
        } else {
692 707
          comps.erase(id(r));
693 708
        }
694 709
      }
695 710
      /// \brief Removes the coefficients which's absolute value does
696 711
      /// not exceed \c epsilon. 
697 712
      void simplify(Value epsilon = 0.0) {
698 713
        std::map<int, Value>::iterator it=comps.begin();
699 714
        while (it != comps.end()) {
700 715
          std::map<int, Value>::iterator jt=it;
... ...
@@ -825,1256 +840,1263 @@
825 840
        ConstCoeffIt& operator++() { ++_it; return *this; }
826 841

	
827 842
        /// Equality operator
828 843
        bool operator==(Invalid) const { return _it == _end; }
829 844
        /// Inequality operator
830 845
        bool operator!=(Invalid) const { return _it != _end; }
831 846
      };
832 847
    };
833 848

	
834 849

	
835 850
  protected:
836 851

	
837 852
    class InsertIterator {
838 853
    private:
839 854

	
840 855
      std::map<int, Value>& _host;
841 856
      const _solver_bits::VarIndex& _index;
842 857

	
843 858
    public:
844 859

	
845 860
      typedef std::output_iterator_tag iterator_category;
846 861
      typedef void difference_type;
847 862
      typedef void value_type;
848 863
      typedef void reference;
849 864
      typedef void pointer;
850 865

	
851 866
      InsertIterator(std::map<int, Value>& host,
852 867
                   const _solver_bits::VarIndex& index)
853 868
        : _host(host), _index(index) {}
854 869

	
855 870
      InsertIterator& operator=(const std::pair<int, Value>& value) {
856 871
        typedef std::map<int, Value>::value_type pair_type;
857 872
        _host.insert(pair_type(_index[value.first], value.second));
858 873
        return *this;
859 874
      }
860 875

	
861 876
      InsertIterator& operator*() { return *this; }
862 877
      InsertIterator& operator++() { return *this; }
863 878
      InsertIterator operator++(int) { return *this; }
864 879

	
865 880
    };
866 881

	
867 882
    class ExprIterator {
868 883
    private:
869 884
      std::map<int, Value>::const_iterator _host_it;
870 885
      const _solver_bits::VarIndex& _index;
871 886
    public:
872 887

	
873 888
      typedef std::bidirectional_iterator_tag iterator_category;
874 889
      typedef std::ptrdiff_t difference_type;
875 890
      typedef const std::pair<int, Value> value_type;
876 891
      typedef value_type reference;
877 892

	
878 893
      class pointer {
879 894
      public:
880 895
        pointer(value_type& _value) : value(_value) {}
881 896
        value_type* operator->() { return &value; }
882 897
      private:
883 898
        value_type value;
884 899
      };
885 900

	
886 901
      ExprIterator(const std::map<int, Value>::const_iterator& host_it,
887 902
                   const _solver_bits::VarIndex& index)
888 903
        : _host_it(host_it), _index(index) {}
889 904

	
890 905
      reference operator*() {
891 906
        return std::make_pair(_index(_host_it->first), _host_it->second);
892 907
      }
893 908

	
894 909
      pointer operator->() {
895 910
        return pointer(operator*());
896 911
      }
897 912

	
898 913
      ExprIterator& operator++() { ++_host_it; return *this; }
899 914
      ExprIterator operator++(int) {
900 915
        ExprIterator tmp(*this); ++_host_it; return tmp;
901 916
      }
902 917

	
903 918
      ExprIterator& operator--() { --_host_it; return *this; }
904 919
      ExprIterator operator--(int) {
905 920
        ExprIterator tmp(*this); --_host_it; return tmp;
906 921
      }
907 922

	
908 923
      bool operator==(const ExprIterator& it) const {
909 924
        return _host_it == it._host_it;
910 925
      }
911 926

	
912 927
      bool operator!=(const ExprIterator& it) const {
913 928
        return _host_it != it._host_it;
914 929
      }
915 930

	
916 931
    };
917 932

	
918 933
  protected:
919 934

	
920 935
    //Abstract virtual functions
921
    virtual LpBase* _newSolver() const = 0;
922
    virtual LpBase* _cloneSolver() const = 0;
923 936

	
924 937
    virtual int _addColId(int col) { return cols.addIndex(col); }
925 938
    virtual int _addRowId(int row) { return rows.addIndex(row); }
926 939

	
927 940
    virtual void _eraseColId(int col) { cols.eraseIndex(col); }
928 941
    virtual void _eraseRowId(int row) { rows.eraseIndex(row); }
929 942

	
930 943
    virtual int _addCol() = 0;
931 944
    virtual int _addRow() = 0;
932 945

	
946
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
947
      int row = _addRow();
948
      _setRowCoeffs(row, b, e);
949
      _setRowLowerBound(row, l);
950
      _setRowUpperBound(row, u);
951
      return row;
952
    }
953

	
933 954
    virtual void _eraseCol(int col) = 0;
934 955
    virtual void _eraseRow(int row) = 0;
935 956

	
936 957
    virtual void _getColName(int col, std::string& name) const = 0;
937 958
    virtual void _setColName(int col, const std::string& name) = 0;
938 959
    virtual int _colByName(const std::string& name) const = 0;
939 960

	
940 961
    virtual void _getRowName(int row, std::string& name) const = 0;
941 962
    virtual void _setRowName(int row, const std::string& name) = 0;
942 963
    virtual int _rowByName(const std::string& name) const = 0;
943 964

	
944 965
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e) = 0;
945 966
    virtual void _getRowCoeffs(int i, InsertIterator b) const = 0;
946 967

	
947 968
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e) = 0;
948 969
    virtual void _getColCoeffs(int i, InsertIterator b) const = 0;
949 970

	
950 971
    virtual void _setCoeff(int row, int col, Value value) = 0;
951 972
    virtual Value _getCoeff(int row, int col) const = 0;
952 973

	
953 974
    virtual void _setColLowerBound(int i, Value value) = 0;
954 975
    virtual Value _getColLowerBound(int i) const = 0;
955 976

	
956 977
    virtual void _setColUpperBound(int i, Value value) = 0;
957 978
    virtual Value _getColUpperBound(int i) const = 0;
958 979

	
959 980
    virtual void _setRowLowerBound(int i, Value value) = 0;
960 981
    virtual Value _getRowLowerBound(int i) const = 0;
961 982

	
962 983
    virtual void _setRowUpperBound(int i, Value value) = 0;
963 984
    virtual Value _getRowUpperBound(int i) const = 0;
964 985

	
965 986
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e) = 0;
966 987
    virtual void _getObjCoeffs(InsertIterator b) const = 0;
967 988

	
968 989
    virtual void _setObjCoeff(int i, Value obj_coef) = 0;
969 990
    virtual Value _getObjCoeff(int i) const = 0;
970 991

	
971 992
    virtual void _setSense(Sense) = 0;
972 993
    virtual Sense _getSense() const = 0;
973 994

	
974 995
    virtual void _clear() = 0;
975 996

	
976 997
    virtual const char* _solverName() const = 0;
977 998

	
999
    virtual void _messageLevel(MessageLevel level) = 0;
1000

	
978 1001
    //Own protected stuff
979 1002

	
980 1003
    //Constant component of the objective function
981 1004
    Value obj_const_comp;
982 1005

	
983 1006
    LpBase() : rows(), cols(), obj_const_comp(0) {}
984 1007

	
985 1008
  public:
986 1009

	
987 1010
    /// Virtual destructor
988 1011
    virtual ~LpBase() {}
989 1012

	
990
    ///Creates a new LP problem
991
    LpBase* newSolver() {return _newSolver();}
992
    ///Makes a copy of the LP problem
993
    LpBase* cloneSolver() {return _cloneSolver();}
994

	
995 1013
    ///Gives back the name of the solver.
996 1014
    const char* solverName() const {return _solverName();}
997 1015

	
998
    ///\name Build up and modify the LP
1016
    ///\name Build Up and Modify the LP
999 1017

	
1000 1018
    ///@{
1001 1019

	
1002 1020
    ///Add a new empty column (i.e a new variable) to the LP
1003 1021
    Col addCol() { Col c; c._id = _addColId(_addCol()); return c;}
1004 1022

	
1005 1023
    ///\brief Adds several new columns (i.e variables) at once
1006 1024
    ///
1007 1025
    ///This magic function takes a container as its argument and fills
1008 1026
    ///its elements with new columns (i.e. variables)
1009 1027
    ///\param t can be
1010 1028
    ///- a standard STL compatible iterable container with
1011 1029
    ///\ref Col as its \c values_type like
1012 1030
    ///\code
1013 1031
    ///std::vector<LpBase::Col>
1014 1032
    ///std::list<LpBase::Col>
1015 1033
    ///\endcode
1016 1034
    ///- a standard STL compatible iterable container with
1017 1035
    ///\ref Col as its \c mapped_type like
1018 1036
    ///\code
1019 1037
    ///std::map<AnyType,LpBase::Col>
1020 1038
    ///\endcode
1021 1039
    ///- an iterable lemon \ref concepts::WriteMap "write map" like
1022 1040
    ///\code
1023 1041
    ///ListGraph::NodeMap<LpBase::Col>
1024 1042
    ///ListGraph::ArcMap<LpBase::Col>
1025 1043
    ///\endcode
1026 1044
    ///\return The number of the created column.
1027 1045
#ifdef DOXYGEN
1028 1046
    template<class T>
1029 1047
    int addColSet(T &t) { return 0;}
1030 1048
#else
1031 1049
    template<class T>
1032 1050
    typename enable_if<typename T::value_type::LpCol,int>::type
1033 1051
    addColSet(T &t,dummy<0> = 0) {
1034 1052
      int s=0;
1035 1053
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
1036 1054
      return s;
1037 1055
    }
1038 1056
    template<class T>
1039 1057
    typename enable_if<typename T::value_type::second_type::LpCol,
1040 1058
                       int>::type
1041 1059
    addColSet(T &t,dummy<1> = 1) {
1042 1060
      int s=0;
1043 1061
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
1044 1062
        i->second=addCol();
1045 1063
        s++;
1046 1064
      }
1047 1065
      return s;
1048 1066
    }
1049 1067
    template<class T>
1050 1068
    typename enable_if<typename T::MapIt::Value::LpCol,
1051 1069
                       int>::type
1052 1070
    addColSet(T &t,dummy<2> = 2) {
1053 1071
      int s=0;
1054 1072
      for(typename T::MapIt i(t); i!=INVALID; ++i)
1055 1073
        {
1056 1074
          i.set(addCol());
1057 1075
          s++;
1058 1076
        }
1059 1077
      return s;
1060 1078
    }
1061 1079
#endif
1062 1080

	
1063 1081
    ///Set a column (i.e a dual constraint) of the LP
1064 1082

	
1065 1083
    ///\param c is the column to be modified
1066 1084
    ///\param e is a dual linear expression (see \ref DualExpr)
1067 1085
    ///a better one.
1068 1086
    void col(Col c, const DualExpr &e) {
1069 1087
      e.simplify();
1070
      _setColCoeffs(cols(id(c)), ExprIterator(e.comps.begin(), cols),
1071
                    ExprIterator(e.comps.end(), cols));
1088
      _setColCoeffs(cols(id(c)), ExprIterator(e.comps.begin(), rows),
1089
                    ExprIterator(e.comps.end(), rows));
1072 1090
    }
1073 1091

	
1074 1092
    ///Get a column (i.e a dual constraint) of the LP
1075 1093

	
1076 1094
    ///\param c is the column to get
1077 1095
    ///\return the dual expression associated to the column
1078 1096
    DualExpr col(Col c) const {
1079 1097
      DualExpr e;
1080 1098
      _getColCoeffs(cols(id(c)), InsertIterator(e.comps, rows));
1081 1099
      return e;
1082 1100
    }
1083 1101

	
1084 1102
    ///Add a new column to the LP
1085 1103

	
1086 1104
    ///\param e is a dual linear expression (see \ref DualExpr)
1087 1105
    ///\param o is the corresponding component of the objective
1088 1106
    ///function. It is 0 by default.
1089 1107
    ///\return The created column.
1090 1108
    Col addCol(const DualExpr &e, Value o = 0) {
1091 1109
      Col c=addCol();
1092 1110
      col(c,e);
1093 1111
      objCoeff(c,o);
1094 1112
      return c;
1095 1113
    }
1096 1114

	
1097 1115
    ///Add a new empty row (i.e a new constraint) to the LP
1098 1116

	
1099 1117
    ///This function adds a new empty row (i.e a new constraint) to the LP.
1100 1118
    ///\return The created row
1101 1119
    Row addRow() { Row r; r._id = _addRowId(_addRow()); return r;}
1102 1120

	
1103 1121
    ///\brief Add several new rows (i.e constraints) at once
1104 1122
    ///
1105 1123
    ///This magic function takes a container as its argument and fills
1106 1124
    ///its elements with new row (i.e. variables)
1107 1125
    ///\param t can be
1108 1126
    ///- a standard STL compatible iterable container with
1109 1127
    ///\ref Row as its \c values_type like
1110 1128
    ///\code
1111 1129
    ///std::vector<LpBase::Row>
1112 1130
    ///std::list<LpBase::Row>
1113 1131
    ///\endcode
1114 1132
    ///- a standard STL compatible iterable container with
1115 1133
    ///\ref Row as its \c mapped_type like
1116 1134
    ///\code
1117 1135
    ///std::map<AnyType,LpBase::Row>
1118 1136
    ///\endcode
1119 1137
    ///- an iterable lemon \ref concepts::WriteMap "write map" like
1120 1138
    ///\code
1121 1139
    ///ListGraph::NodeMap<LpBase::Row>
1122 1140
    ///ListGraph::ArcMap<LpBase::Row>
1123 1141
    ///\endcode
1124 1142
    ///\return The number of rows created.
1125 1143
#ifdef DOXYGEN
1126 1144
    template<class T>
1127 1145
    int addRowSet(T &t) { return 0;}
1128 1146
#else
1129 1147
    template<class T>
1130 1148
    typename enable_if<typename T::value_type::LpRow,int>::type
1131 1149
    addRowSet(T &t, dummy<0> = 0) {
1132 1150
      int s=0;
1133 1151
      for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
1134 1152
      return s;
1135 1153
    }
1136 1154
    template<class T>
1137 1155
    typename enable_if<typename T::value_type::second_type::LpRow, int>::type
1138 1156
    addRowSet(T &t, dummy<1> = 1) {
1139 1157
      int s=0;
1140 1158
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
1141 1159
        i->second=addRow();
1142 1160
        s++;
1143 1161
      }
1144 1162
      return s;
1145 1163
    }
1146 1164
    template<class T>
1147 1165
    typename enable_if<typename T::MapIt::Value::LpRow, int>::type
1148 1166
    addRowSet(T &t, dummy<2> = 2) {
1149 1167
      int s=0;
1150 1168
      for(typename T::MapIt i(t); i!=INVALID; ++i)
1151 1169
        {
1152 1170
          i.set(addRow());
1153 1171
          s++;
1154 1172
        }
1155 1173
      return s;
1156 1174
    }
1157 1175
#endif
1158 1176

	
1159 1177
    ///Set a row (i.e a constraint) of the LP
1160 1178

	
1161 1179
    ///\param r is the row to be modified
1162 1180
    ///\param l is lower bound (-\ref INF means no bound)
1163 1181
    ///\param e is a linear expression (see \ref Expr)
1164 1182
    ///\param u is the upper bound (\ref INF means no bound)
1165 1183
    void row(Row r, Value l, const Expr &e, Value u) {
1166 1184
      e.simplify();
1167 1185
      _setRowCoeffs(rows(id(r)), ExprIterator(e.comps.begin(), cols),
1168 1186
                    ExprIterator(e.comps.end(), cols));
1169 1187
      _setRowLowerBound(rows(id(r)),l - *e);
1170 1188
      _setRowUpperBound(rows(id(r)),u - *e);
1171 1189
    }
1172 1190

	
1173 1191
    ///Set a row (i.e a constraint) of the LP
1174 1192

	
1175 1193
    ///\param r is the row to be modified
1176 1194
    ///\param c is a linear expression (see \ref Constr)
1177 1195
    void row(Row r, const Constr &c) {
1178 1196
      row(r, c.lowerBounded()?c.lowerBound():-INF,
1179 1197
          c.expr(), c.upperBounded()?c.upperBound():INF);
1180 1198
    }
1181 1199

	
1182 1200

	
1183 1201
    ///Get a row (i.e a constraint) of the LP
1184 1202

	
1185 1203
    ///\param r is the row to get
1186 1204
    ///\return the expression associated to the row
1187 1205
    Expr row(Row r) const {
1188 1206
      Expr e;
1189 1207
      _getRowCoeffs(rows(id(r)), InsertIterator(e.comps, cols));
1190 1208
      return e;
1191 1209
    }
1192 1210

	
1193 1211
    ///Add a new row (i.e a new constraint) to the LP
1194 1212

	
1195 1213
    ///\param l is the lower bound (-\ref INF means no bound)
1196 1214
    ///\param e is a linear expression (see \ref Expr)
1197 1215
    ///\param u is the upper bound (\ref INF means no bound)
1198 1216
    ///\return The created row.
1199 1217
    Row addRow(Value l,const Expr &e, Value u) {
1200
      Row r=addRow();
1201
      row(r,l,e,u);
1218
      Row r;
1219
      e.simplify();
1220
      r._id = _addRowId(_addRow(l - *e, ExprIterator(e.comps.begin(), cols),
1221
                                ExprIterator(e.comps.end(), cols), u - *e));
1202 1222
      return r;
1203 1223
    }
1204 1224

	
1205 1225
    ///Add a new row (i.e a new constraint) to the LP
1206 1226

	
1207 1227
    ///\param c is a linear expression (see \ref Constr)
1208 1228
    ///\return The created row.
1209 1229
    Row addRow(const Constr &c) {
1210
      Row r=addRow();
1211
      row(r,c);
1230
      Row r;
1231
      c.expr().simplify();
1232
      r._id = _addRowId(_addRow(c.lowerBounded()?c.lowerBound():-INF, 
1233
                                ExprIterator(c.expr().comps.begin(), cols),
1234
                                ExprIterator(c.expr().comps.end(), cols),
1235
                                c.upperBounded()?c.upperBound():INF));
1212 1236
      return r;
1213 1237
    }
1214 1238
    ///Erase a column (i.e a variable) from the LP
1215 1239

	
1216 1240
    ///\param c is the column to be deleted
1217 1241
    void erase(Col c) {
1218 1242
      _eraseCol(cols(id(c)));
1219 1243
      _eraseColId(cols(id(c)));
1220 1244
    }
1221 1245
    ///Erase a row (i.e a constraint) from the LP
1222 1246

	
1223 1247
    ///\param r is the row to be deleted
1224 1248
    void erase(Row r) {
1225 1249
      _eraseRow(rows(id(r)));
1226 1250
      _eraseRowId(rows(id(r)));
1227 1251
    }
1228 1252

	
1229 1253
    /// Get the name of a column
1230 1254

	
1231 1255
    ///\param c is the coresponding column
1232 1256
    ///\return The name of the colunm
1233 1257
    std::string colName(Col c) const {
1234 1258
      std::string name;
1235 1259
      _getColName(cols(id(c)), name);
1236 1260
      return name;
1237 1261
    }
1238 1262

	
1239 1263
    /// Set the name of a column
1240 1264

	
1241 1265
    ///\param c is the coresponding column
1242 1266
    ///\param name The name to be given
1243 1267
    void colName(Col c, const std::string& name) {
1244 1268
      _setColName(cols(id(c)), name);
1245 1269
    }
1246 1270

	
1247 1271
    /// Get the column by its name
1248 1272

	
1249 1273
    ///\param name The name of the column
1250 1274
    ///\return the proper column or \c INVALID
1251 1275
    Col colByName(const std::string& name) const {
1252 1276
      int k = _colByName(name);
1253 1277
      return k != -1 ? Col(cols[k]) : Col(INVALID);
1254 1278
    }
1255 1279

	
1256 1280
    /// Get the name of a row
1257 1281

	
1258 1282
    ///\param r is the coresponding row
1259 1283
    ///\return The name of the row
1260 1284
    std::string rowName(Row r) const {
1261 1285
      std::string name;
1262 1286
      _getRowName(rows(id(r)), name);
1263 1287
      return name;
1264 1288
    }
1265 1289

	
1266 1290
    /// Set the name of a row
1267 1291

	
1268 1292
    ///\param r is the coresponding row
1269 1293
    ///\param name The name to be given
1270 1294
    void rowName(Row r, const std::string& name) {
1271 1295
      _setRowName(rows(id(r)), name);
1272 1296
    }
1273 1297

	
1274 1298
    /// Get the row by its name
1275 1299

	
1276 1300
    ///\param name The name of the row
1277 1301
    ///\return the proper row or \c INVALID
1278 1302
    Row rowByName(const std::string& name) const {
1279 1303
      int k = _rowByName(name);
1280 1304
      return k != -1 ? Row(rows[k]) : Row(INVALID);
1281 1305
    }
1282 1306

	
1283 1307
    /// Set an element of the coefficient matrix of the LP
1284 1308

	
1285 1309
    ///\param r is the row of the element to be modified
1286 1310
    ///\param c is the column of the element to be modified
1287 1311
    ///\param val is the new value of the coefficient
1288 1312
    void coeff(Row r, Col c, Value val) {
1289 1313
      _setCoeff(rows(id(r)),cols(id(c)), val);
1290 1314
    }
1291 1315

	
1292 1316
    /// Get an element of the coefficient matrix of the LP
1293 1317

	
1294 1318
    ///\param r is the row of the element
1295 1319
    ///\param c is the column of the element
1296 1320
    ///\return the corresponding coefficient
1297 1321
    Value coeff(Row r, Col c) const {
1298 1322
      return _getCoeff(rows(id(r)),cols(id(c)));
1299 1323
    }
1300 1324

	
1301 1325
    /// Set the lower bound of a column (i.e a variable)
1302 1326

	
1303 1327
    /// The lower bound of a variable (column) has to be given by an
1304 1328
    /// extended number of type Value, i.e. a finite number of type
1305 1329
    /// Value or -\ref INF.
1306 1330
    void colLowerBound(Col c, Value value) {
1307 1331
      _setColLowerBound(cols(id(c)),value);
1308 1332
    }
1309 1333

	
1310 1334
    /// Get the lower bound of a column (i.e a variable)
1311 1335

	
1312 1336
    /// This function returns the lower bound for column (variable) \c c
1313 1337
    /// (this might be -\ref INF as well).
1314 1338
    ///\return The lower bound for column \c c
1315 1339
    Value colLowerBound(Col c) const {
1316 1340
      return _getColLowerBound(cols(id(c)));
1317 1341
    }
1318 1342

	
1319 1343
    ///\brief Set the lower bound of  several columns
1320 1344
    ///(i.e variables) at once
1321 1345
    ///
1322 1346
    ///This magic function takes a container as its argument
1323 1347
    ///and applies the function on all of its elements.
1324 1348
    ///The lower bound of a variable (column) has to be given by an
1325 1349
    ///extended number of type Value, i.e. a finite number of type
1326 1350
    ///Value or -\ref INF.
1327 1351
#ifdef DOXYGEN
1328 1352
    template<class T>
1329 1353
    void colLowerBound(T &t, Value value) { return 0;}
1330 1354
#else
1331 1355
    template<class T>
1332 1356
    typename enable_if<typename T::value_type::LpCol,void>::type
1333 1357
    colLowerBound(T &t, Value value,dummy<0> = 0) {
1334 1358
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
1335 1359
        colLowerBound(*i, value);
1336 1360
      }
1337 1361
    }
1338 1362
    template<class T>
1339 1363
    typename enable_if<typename T::value_type::second_type::LpCol,
1340 1364
                       void>::type
1341 1365
    colLowerBound(T &t, Value value,dummy<1> = 1) {
1342 1366
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
1343 1367
        colLowerBound(i->second, value);
1344 1368
      }
1345 1369
    }
1346 1370
    template<class T>
1347 1371
    typename enable_if<typename T::MapIt::Value::LpCol,
1348 1372
                       void>::type
1349 1373
    colLowerBound(T &t, Value value,dummy<2> = 2) {
1350 1374
      for(typename T::MapIt i(t); i!=INVALID; ++i){
1351 1375
        colLowerBound(*i, value);
1352 1376
      }
1353 1377
    }
1354 1378
#endif
1355 1379

	
1356 1380
    /// Set the upper bound of a column (i.e a variable)
1357 1381

	
1358 1382
    /// The upper bound of a variable (column) has to be given by an
1359 1383
    /// extended number of type Value, i.e. a finite number of type
1360 1384
    /// Value or \ref INF.
1361 1385
    void colUpperBound(Col c, Value value) {
1362 1386
      _setColUpperBound(cols(id(c)),value);
1363 1387
    };
1364 1388

	
1365 1389
    /// Get the upper bound of a column (i.e a variable)
1366 1390

	
1367 1391
    /// This function returns the upper bound for column (variable) \c c
1368 1392
    /// (this might be \ref INF as well).
1369 1393
    /// \return The upper bound for column \c c
1370 1394
    Value colUpperBound(Col c) const {
1371 1395
      return _getColUpperBound(cols(id(c)));
1372 1396
    }
1373 1397

	
1374 1398
    ///\brief Set the upper bound of  several columns
1375 1399
    ///(i.e variables) at once
1376 1400
    ///
1377 1401
    ///This magic function takes a container as its argument
1378 1402
    ///and applies the function on all of its elements.
1379 1403
    ///The upper bound of a variable (column) has to be given by an
1380 1404
    ///extended number of type Value, i.e. a finite number of type
1381 1405
    ///Value or \ref INF.
1382 1406
#ifdef DOXYGEN
1383 1407
    template<class T>
1384 1408
    void colUpperBound(T &t, Value value) { return 0;}
1385 1409
#else
1386
    template<class T>
1387
    typename enable_if<typename T::value_type::LpCol,void>::type
1388
    colUpperBound(T &t, Value value,dummy<0> = 0) {
1389
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
1410
    template<class T1>
1411
    typename enable_if<typename T1::value_type::LpCol,void>::type
1412
    colUpperBound(T1 &t, Value value,dummy<0> = 0) {
1413
      for(typename T1::iterator i=t.begin();i!=t.end();++i) {
1390 1414
        colUpperBound(*i, value);
1391 1415
      }
1392 1416
    }
1393
    template<class T>
1394
    typename enable_if<typename T::value_type::second_type::LpCol,
1417
    template<class T1>
1418
    typename enable_if<typename T1::value_type::second_type::LpCol,
1395 1419
                       void>::type
1396
    colUpperBound(T &t, Value value,dummy<1> = 1) {
1397
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
1420
    colUpperBound(T1 &t, Value value,dummy<1> = 1) {
1421
      for(typename T1::iterator i=t.begin();i!=t.end();++i) {
1398 1422
        colUpperBound(i->second, value);
1399 1423
      }
1400 1424
    }
1401
    template<class T>
1402
    typename enable_if<typename T::MapIt::Value::LpCol,
1425
    template<class T1>
1426
    typename enable_if<typename T1::MapIt::Value::LpCol,
1403 1427
                       void>::type
1404
    colUpperBound(T &t, Value value,dummy<2> = 2) {
1405
      for(typename T::MapIt i(t); i!=INVALID; ++i){
1428
    colUpperBound(T1 &t, Value value,dummy<2> = 2) {
1429
      for(typename T1::MapIt i(t); i!=INVALID; ++i){
1406 1430
        colUpperBound(*i, value);
1407 1431
      }
1408 1432
    }
1409 1433
#endif
1410 1434

	
1411 1435
    /// Set the lower and the upper bounds of a column (i.e a variable)
1412 1436

	
1413 1437
    /// The lower and the upper bounds of
1414 1438
    /// a variable (column) have to be given by an
1415 1439
    /// extended number of type Value, i.e. a finite number of type
1416 1440
    /// Value, -\ref INF or \ref INF.
1417 1441
    void colBounds(Col c, Value lower, Value upper) {
1418 1442
      _setColLowerBound(cols(id(c)),lower);
1419 1443
      _setColUpperBound(cols(id(c)),upper);
1420 1444
    }
1421 1445

	
1422 1446
    ///\brief Set the lower and the upper bound of several columns
1423 1447
    ///(i.e variables) at once
1424 1448
    ///
1425 1449
    ///This magic function takes a container as its argument
1426 1450
    ///and applies the function on all of its elements.
1427 1451
    /// The lower and the upper bounds of
1428 1452
    /// a variable (column) have to be given by an
1429 1453
    /// extended number of type Value, i.e. a finite number of type
1430 1454
    /// Value, -\ref INF or \ref INF.
1431 1455
#ifdef DOXYGEN
1432 1456
    template<class T>
1433 1457
    void colBounds(T &t, Value lower, Value upper) { return 0;}
1434 1458
#else
1435
    template<class T>
1436
    typename enable_if<typename T::value_type::LpCol,void>::type
1437
    colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1438
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
1459
    template<class T2>
1460
    typename enable_if<typename T2::value_type::LpCol,void>::type
1461
    colBounds(T2 &t, Value lower, Value upper,dummy<0> = 0) {
1462
      for(typename T2::iterator i=t.begin();i!=t.end();++i) {
1439 1463
        colBounds(*i, lower, upper);
1440 1464
      }
1441 1465
    }
1442
    template<class T>
1443
    typename enable_if<typename T::value_type::second_type::LpCol, void>::type
1444
    colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1445
      for(typename T::iterator i=t.begin();i!=t.end();++i) {
1466
    template<class T2>
1467
    typename enable_if<typename T2::value_type::second_type::LpCol, void>::type
1468
    colBounds(T2 &t, Value lower, Value upper,dummy<1> = 1) {
1469
      for(typename T2::iterator i=t.begin();i!=t.end();++i) {
1446 1470
        colBounds(i->second, lower, upper);
1447 1471
      }
1448 1472
    }
1449
    template<class T>
1450
    typename enable_if<typename T::MapIt::Value::LpCol, void>::type
1451
    colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1452
      for(typename T::MapIt i(t); i!=INVALID; ++i){
1473
    template<class T2>
1474
    typename enable_if<typename T2::MapIt::Value::LpCol, void>::type
1475
    colBounds(T2 &t, Value lower, Value upper,dummy<2> = 2) {
1476
      for(typename T2::MapIt i(t); i!=INVALID; ++i){
1453 1477
        colBounds(*i, lower, upper);
1454 1478
      }
1455 1479
    }
1456 1480
#endif
1457 1481

	
1458 1482
    /// Set the lower bound of a row (i.e a constraint)
1459 1483

	
1460 1484
    /// The lower bound of a constraint (row) has to be given by an
1461 1485
    /// extended number of type Value, i.e. a finite number of type
1462 1486
    /// Value or -\ref INF.
1463 1487
    void rowLowerBound(Row r, Value value) {
1464 1488
      _setRowLowerBound(rows(id(r)),value);
1465 1489
    }
1466 1490

	
1467 1491
    /// Get the lower bound of a row (i.e a constraint)
1468 1492

	
1469 1493
    /// This function returns the lower bound for row (constraint) \c c
1470 1494
    /// (this might be -\ref INF as well).
1471 1495
    ///\return The lower bound for row \c r
1472 1496
    Value rowLowerBound(Row r) const {
1473 1497
      return _getRowLowerBound(rows(id(r)));
1474 1498
    }
1475 1499

	
1476 1500
    /// Set the upper bound of a row (i.e a constraint)
1477 1501

	
1478 1502
    /// The upper bound of a constraint (row) has to be given by an
1479 1503
    /// extended number of type Value, i.e. a finite number of type
1480 1504
    /// Value or -\ref INF.
1481 1505
    void rowUpperBound(Row r, Value value) {
1482 1506
      _setRowUpperBound(rows(id(r)),value);
1483 1507
    }
1484 1508

	
1485 1509
    /// Get the upper bound of a row (i.e a constraint)
1486 1510

	
1487 1511
    /// This function returns the upper bound for row (constraint) \c c
1488 1512
    /// (this might be -\ref INF as well).
1489 1513
    ///\return The upper bound for row \c r
1490 1514
    Value rowUpperBound(Row r) const {
1491 1515
      return _getRowUpperBound(rows(id(r)));
1492 1516
    }
1493 1517

	
1494 1518
    ///Set an element of the objective function
1495 1519
    void objCoeff(Col c, Value v) {_setObjCoeff(cols(id(c)),v); };
1496 1520

	
1497 1521
    ///Get an element of the objective function
1498 1522
    Value objCoeff(Col c) const { return _getObjCoeff(cols(id(c))); };
1499 1523

	
1500 1524
    ///Set the objective function
1501 1525

	
1502 1526
    ///\param e is a linear expression of type \ref Expr.
1503 1527
    ///
1504 1528
    void obj(const Expr& e) {
1505 1529
      _setObjCoeffs(ExprIterator(e.comps.begin(), cols),
1506 1530
                    ExprIterator(e.comps.end(), cols));
1507 1531
      obj_const_comp = *e;
1508 1532
    }
1509 1533

	
1510 1534
    ///Get the objective function
1511 1535

	
1512 1536
    ///\return the objective function as a linear expression of type
1513 1537
    ///Expr.
1514 1538
    Expr obj() const {
1515 1539
      Expr e;
1516 1540
      _getObjCoeffs(InsertIterator(e.comps, cols));
1517 1541
      *e = obj_const_comp;
1518 1542
      return e;
1519 1543
    }
1520 1544

	
1521 1545

	
1522 1546
    ///Set the direction of optimization
1523 1547
    void sense(Sense sense) { _setSense(sense); }
1524 1548

	
1525 1549
    ///Query the direction of the optimization
1526 1550
    Sense sense() const {return _getSense(); }
1527 1551

	
1528 1552
    ///Set the sense to maximization
1529 1553
    void max() { _setSense(MAX); }
1530 1554

	
1531 1555
    ///Set the sense to maximization
1532 1556
    void min() { _setSense(MIN); }
1533 1557

	
1534 1558
    ///Clears the problem
1535 1559
    void clear() { _clear(); }
1536 1560

	
1561
    /// Sets the message level of the solver
1562
    void messageLevel(MessageLevel level) { _messageLevel(level); }
1563

	
1537 1564
    ///@}
1538 1565

	
1539 1566
  };
1540 1567

	
1541 1568
  /// Addition
1542 1569

	
1543 1570
  ///\relates LpBase::Expr
1544 1571
  ///
1545 1572
  inline LpBase::Expr operator+(const LpBase::Expr &a, const LpBase::Expr &b) {
1546 1573
    LpBase::Expr tmp(a);
1547 1574
    tmp+=b;
1548 1575
    return tmp;
1549 1576
  }
1550 1577
  ///Substraction
1551 1578

	
1552 1579
  ///\relates LpBase::Expr
1553 1580
  ///
1554 1581
  inline LpBase::Expr operator-(const LpBase::Expr &a, const LpBase::Expr &b) {
1555 1582
    LpBase::Expr tmp(a);
1556 1583
    tmp-=b;
1557 1584
    return tmp;
1558 1585
  }
1559 1586
  ///Multiply with constant
1560 1587

	
1561 1588
  ///\relates LpBase::Expr
1562 1589
  ///
1563 1590
  inline LpBase::Expr operator*(const LpBase::Expr &a, const LpBase::Value &b) {
1564 1591
    LpBase::Expr tmp(a);
1565 1592
    tmp*=b;
1566 1593
    return tmp;
1567 1594
  }
1568 1595

	
1569 1596
  ///Multiply with constant
1570 1597

	
1571 1598
  ///\relates LpBase::Expr
1572 1599
  ///
1573 1600
  inline LpBase::Expr operator*(const LpBase::Value &a, const LpBase::Expr &b) {
1574 1601
    LpBase::Expr tmp(b);
1575 1602
    tmp*=a;
1576 1603
    return tmp;
1577 1604
  }
1578 1605
  ///Divide with constant
1579 1606

	
1580 1607
  ///\relates LpBase::Expr
1581 1608
  ///
1582 1609
  inline LpBase::Expr operator/(const LpBase::Expr &a, const LpBase::Value &b) {
1583 1610
    LpBase::Expr tmp(a);
1584 1611
    tmp/=b;
1585 1612
    return tmp;
1586 1613
  }
1587 1614

	
1588 1615
  ///Create constraint
1589 1616

	
1590 1617
  ///\relates LpBase::Constr
1591 1618
  ///
1592 1619
  inline LpBase::Constr operator<=(const LpBase::Expr &e,
1593 1620
                                   const LpBase::Expr &f) {
1594 1621
    return LpBase::Constr(0, f - e, LpBase::INF);
1595 1622
  }
1596 1623

	
1597 1624
  ///Create constraint
1598 1625

	
1599 1626
  ///\relates LpBase::Constr
1600 1627
  ///
1601 1628
  inline LpBase::Constr operator<=(const LpBase::Value &e,
1602 1629
                                   const LpBase::Expr &f) {
1603 1630
    return LpBase::Constr(e, f, LpBase::NaN);
1604 1631
  }
1605 1632

	
1606 1633
  ///Create constraint
1607 1634

	
1608 1635
  ///\relates LpBase::Constr
1609 1636
  ///
1610 1637
  inline LpBase::Constr operator<=(const LpBase::Expr &e,
1611 1638
                                   const LpBase::Value &f) {
1612 1639
    return LpBase::Constr(- LpBase::INF, e, f);
1613 1640
  }
1614 1641

	
1615 1642
  ///Create constraint
1616 1643

	
1617 1644
  ///\relates LpBase::Constr
1618 1645
  ///
1619 1646
  inline LpBase::Constr operator>=(const LpBase::Expr &e,
1620 1647
                                   const LpBase::Expr &f) {
1621 1648
    return LpBase::Constr(0, e - f, LpBase::INF);
1622 1649
  }
1623 1650

	
1624 1651

	
1625 1652
  ///Create constraint
1626 1653

	
1627 1654
  ///\relates LpBase::Constr
1628 1655
  ///
1629 1656
  inline LpBase::Constr operator>=(const LpBase::Value &e,
1630 1657
                                   const LpBase::Expr &f) {
1631 1658
    return LpBase::Constr(LpBase::NaN, f, e);
1632 1659
  }
1633 1660

	
1634 1661

	
1635 1662
  ///Create constraint
1636 1663

	
1637 1664
  ///\relates LpBase::Constr
1638 1665
  ///
1639 1666
  inline LpBase::Constr operator>=(const LpBase::Expr &e,
1640 1667
                                   const LpBase::Value &f) {
1641 1668
    return LpBase::Constr(f, e, LpBase::INF);
1642 1669
  }
1643 1670

	
1644 1671
  ///Create constraint
1645 1672

	
1646 1673
  ///\relates LpBase::Constr
1647 1674
  ///
1648 1675
  inline LpBase::Constr operator==(const LpBase::Expr &e,
1649 1676
                                   const LpBase::Value &f) {
1650 1677
    return LpBase::Constr(f, e, f);
1651 1678
  }
1652 1679

	
1653 1680
  ///Create constraint
1654 1681

	
1655 1682
  ///\relates LpBase::Constr
1656 1683
  ///
1657 1684
  inline LpBase::Constr operator==(const LpBase::Expr &e,
1658 1685
                                   const LpBase::Expr &f) {
1659 1686
    return LpBase::Constr(0, f - e, 0);
1660 1687
  }
1661 1688

	
1662 1689
  ///Create constraint
1663 1690

	
1664 1691
  ///\relates LpBase::Constr
1665 1692
  ///
1666 1693
  inline LpBase::Constr operator<=(const LpBase::Value &n,
1667 1694
                                   const LpBase::Constr &c) {
1668 1695
    LpBase::Constr tmp(c);
1669
    LEMON_ASSERT(std::isnan(tmp.lowerBound()), "Wrong LP constraint");
1696
    LEMON_ASSERT(isNaN(tmp.lowerBound()), "Wrong LP constraint");
1670 1697
    tmp.lowerBound()=n;
1671 1698
    return tmp;
1672 1699
  }
1673 1700
  ///Create constraint
1674 1701

	
1675 1702
  ///\relates LpBase::Constr
1676 1703
  ///
1677 1704
  inline LpBase::Constr operator<=(const LpBase::Constr &c,
1678 1705
                                   const LpBase::Value &n)
1679 1706
  {
1680 1707
    LpBase::Constr tmp(c);
1681
    LEMON_ASSERT(std::isnan(tmp.upperBound()), "Wrong LP constraint");
1708
    LEMON_ASSERT(isNaN(tmp.upperBound()), "Wrong LP constraint");
1682 1709
    tmp.upperBound()=n;
1683 1710
    return tmp;
1684 1711
  }
1685 1712

	
1686 1713
  ///Create constraint
1687 1714

	
1688 1715
  ///\relates LpBase::Constr
1689 1716
  ///
1690 1717
  inline LpBase::Constr operator>=(const LpBase::Value &n,
1691 1718
                                   const LpBase::Constr &c) {
1692 1719
    LpBase::Constr tmp(c);
1693
    LEMON_ASSERT(std::isnan(tmp.upperBound()), "Wrong LP constraint");
1720
    LEMON_ASSERT(isNaN(tmp.upperBound()), "Wrong LP constraint");
1694 1721
    tmp.upperBound()=n;
1695 1722
    return tmp;
1696 1723
  }
1697 1724
  ///Create constraint
1698 1725

	
1699 1726
  ///\relates LpBase::Constr
1700 1727
  ///
1701 1728
  inline LpBase::Constr operator>=(const LpBase::Constr &c,
1702 1729
                                   const LpBase::Value &n)
1703 1730
  {
1704 1731
    LpBase::Constr tmp(c);
1705
    LEMON_ASSERT(std::isnan(tmp.lowerBound()), "Wrong LP constraint");
1732
    LEMON_ASSERT(isNaN(tmp.lowerBound()), "Wrong LP constraint");
1706 1733
    tmp.lowerBound()=n;
1707 1734
    return tmp;
1708 1735
  }
1709 1736

	
1710 1737
  ///Addition
1711 1738

	
1712 1739
  ///\relates LpBase::DualExpr
1713 1740
  ///
1714 1741
  inline LpBase::DualExpr operator+(const LpBase::DualExpr &a,
1715 1742
                                    const LpBase::DualExpr &b) {
1716 1743
    LpBase::DualExpr tmp(a);
1717 1744
    tmp+=b;
1718 1745
    return tmp;
1719 1746
  }
1720 1747
  ///Substraction
1721 1748

	
1722 1749
  ///\relates LpBase::DualExpr
1723 1750
  ///
1724 1751
  inline LpBase::DualExpr operator-(const LpBase::DualExpr &a,
1725 1752
                                    const LpBase::DualExpr &b) {
1726 1753
    LpBase::DualExpr tmp(a);
1727 1754
    tmp-=b;
1728 1755
    return tmp;
1729 1756
  }
1730 1757
  ///Multiply with constant
1731 1758

	
1732 1759
  ///\relates LpBase::DualExpr
1733 1760
  ///
1734 1761
  inline LpBase::DualExpr operator*(const LpBase::DualExpr &a,
1735 1762
                                    const LpBase::Value &b) {
1736 1763
    LpBase::DualExpr tmp(a);
1737 1764
    tmp*=b;
1738 1765
    return tmp;
1739 1766
  }
1740 1767

	
1741 1768
  ///Multiply with constant
1742 1769

	
1743 1770
  ///\relates LpBase::DualExpr
1744 1771
  ///
1745 1772
  inline LpBase::DualExpr operator*(const LpBase::Value &a,
1746 1773
                                    const LpBase::DualExpr &b) {
1747 1774
    LpBase::DualExpr tmp(b);
1748 1775
    tmp*=a;
1749 1776
    return tmp;
1750 1777
  }
1751 1778
  ///Divide with constant
1752 1779

	
1753 1780
  ///\relates LpBase::DualExpr
1754 1781
  ///
1755 1782
  inline LpBase::DualExpr operator/(const LpBase::DualExpr &a,
1756 1783
                                    const LpBase::Value &b) {
1757 1784
    LpBase::DualExpr tmp(a);
1758 1785
    tmp/=b;
1759 1786
    return tmp;
1760 1787
  }
1761 1788

	
1762 1789
  /// \ingroup lp_group
1763 1790
  ///
1764 1791
  /// \brief Common base class for LP solvers
1765 1792
  ///
1766 1793
  /// This class is an abstract base class for LP solvers. This class
1767 1794
  /// provides a full interface for set and modify an LP problem,
1768 1795
  /// solve it and retrieve the solution. You can use one of the
1769 1796
  /// descendants as a concrete implementation, or the \c Lp
1770 1797
  /// default LP solver. However, if you would like to handle LP
1771 1798
  /// solvers as reference or pointer in a generic way, you can use
1772 1799
  /// this class directly.
1773 1800
  class LpSolver : virtual public LpBase {
1774 1801
  public:
1775 1802

	
1776 1803
    /// The problem types for primal and dual problems
1777 1804
    enum ProblemType {
1778
      ///Feasible solution hasn't been found (but may exist).
1805
      /// = 0. Feasible solution hasn't been found (but may exist).
1779 1806
      UNDEFINED = 0,
1780
      ///The problem has no feasible solution
1807
      /// = 1. The problem has no feasible solution.
1781 1808
      INFEASIBLE = 1,
1782
      ///Feasible solution found
1809
      /// = 2. Feasible solution found.
1783 1810
      FEASIBLE = 2,
1784
      ///Optimal solution exists and found
1811
      /// = 3. Optimal solution exists and found.
1785 1812
      OPTIMAL = 3,
1786
      ///The cost function is unbounded
1813
      /// = 4. The cost function is unbounded.
1787 1814
      UNBOUNDED = 4
1788 1815
    };
1789 1816

	
1790 1817
    ///The basis status of variables
1791 1818
    enum VarStatus {
1792 1819
      /// The variable is in the basis
1793 1820
      BASIC, 
1794 1821
      /// The variable is free, but not basic
1795 1822
      FREE,
1796 1823
      /// The variable has active lower bound 
1797 1824
      LOWER,
1798 1825
      /// The variable has active upper bound
1799 1826
      UPPER,
1800 1827
      /// The variable is non-basic and fixed
1801 1828
      FIXED
1802 1829
    };
1803 1830

	
1804 1831
  protected:
1805 1832

	
1806 1833
    virtual SolveExitStatus _solve() = 0;
1807 1834

	
1808 1835
    virtual Value _getPrimal(int i) const = 0;
1809 1836
    virtual Value _getDual(int i) const = 0;
1810 1837

	
1811 1838
    virtual Value _getPrimalRay(int i) const = 0;
1812 1839
    virtual Value _getDualRay(int i) const = 0;
1813 1840

	
1814 1841
    virtual Value _getPrimalValue() const = 0;
1815 1842

	
1816 1843
    virtual VarStatus _getColStatus(int i) const = 0;
1817 1844
    virtual VarStatus _getRowStatus(int i) const = 0;
1818 1845

	
1819 1846
    virtual ProblemType _getPrimalType() const = 0;
1820 1847
    virtual ProblemType _getDualType() const = 0;
1821 1848

	
1822 1849
  public:
1823 1850

	
1851
    ///Allocate a new LP problem instance
1852
    virtual LpSolver* newSolver() const = 0;
1853
    ///Make a copy of the LP problem
1854
    virtual LpSolver* cloneSolver() const = 0;
1855

	
1824 1856
    ///\name Solve the LP
1825 1857

	
1826 1858
    ///@{
1827 1859

	
1828 1860
    ///\e Solve the LP problem at hand
1829 1861
    ///
1830 1862
    ///\return The result of the optimization procedure. Possible
1831 1863
    ///values and their meanings can be found in the documentation of
1832 1864
    ///\ref SolveExitStatus.
1833 1865
    SolveExitStatus solve() { return _solve(); }
1834 1866

	
1835 1867
    ///@}
1836 1868

	
1837
    ///\name Obtain the solution
1869
    ///\name Obtain the Solution
1838 1870

	
1839 1871
    ///@{
1840 1872

	
1841 1873
    /// The type of the primal problem
1842 1874
    ProblemType primalType() const {
1843 1875
      return _getPrimalType();
1844 1876
    }
1845 1877

	
1846 1878
    /// The type of the dual problem
1847 1879
    ProblemType dualType() const {
1848 1880
      return _getDualType();
1849 1881
    }
1850 1882

	
1851 1883
    /// Return the primal value of the column
1852 1884

	
1853 1885
    /// Return the primal value of the column.
1854 1886
    /// \pre The problem is solved.
1855 1887
    Value primal(Col c) const { return _getPrimal(cols(id(c))); }
1856 1888

	
1857 1889
    /// Return the primal value of the expression
1858 1890

	
1859 1891
    /// Return the primal value of the expression, i.e. the dot
1860 1892
    /// product of the primal solution and the expression.
1861 1893
    /// \pre The problem is solved.
1862 1894
    Value primal(const Expr& e) const {
1863 1895
      double res = *e;
1864 1896
      for (Expr::ConstCoeffIt c(e); c != INVALID; ++c) {
1865 1897
        res += *c * primal(c);
1866 1898
      }
1867 1899
      return res;
1868 1900
    }
1869 1901
    /// Returns a component of the primal ray
1870 1902
    
1871 1903
    /// The primal ray is solution of the modified primal problem,
1872 1904
    /// where we change each finite bound to 0, and we looking for a
1873 1905
    /// negative objective value in case of minimization, and positive
1874 1906
    /// objective value for maximization. If there is such solution,
1875 1907
    /// that proofs the unsolvability of the dual problem, and if a
1876 1908
    /// feasible primal solution exists, then the unboundness of
1877 1909
    /// primal problem.
1878 1910
    ///
1879 1911
    /// \pre The problem is solved and the dual problem is infeasible.
1880 1912
    /// \note Some solvers does not provide primal ray calculation
1881 1913
    /// functions.
1882 1914
    Value primalRay(Col c) const { return _getPrimalRay(cols(id(c))); }
1883 1915

	
1884 1916
    /// Return the dual value of the row
1885 1917

	
1886 1918
    /// Return the dual value of the row.
1887 1919
    /// \pre The problem is solved.
1888 1920
    Value dual(Row r) const { return _getDual(rows(id(r))); }
1889 1921

	
1890 1922
    /// Return the dual value of the dual expression
1891 1923

	
1892 1924
    /// Return the dual value of the dual expression, i.e. the dot
1893 1925
    /// product of the dual solution and the dual expression.
1894 1926
    /// \pre The problem is solved.
1895 1927
    Value dual(const DualExpr& e) const {
1896 1928
      double res = 0.0;
1897 1929
      for (DualExpr::ConstCoeffIt r(e); r != INVALID; ++r) {
1898 1930
        res += *r * dual(r);
1899 1931
      }
1900 1932
      return res;
1901 1933
    }
1902 1934

	
1903 1935
    /// Returns a component of the dual ray
1904 1936
    
1905 1937
    /// The dual ray is solution of the modified primal problem, where
1906 1938
    /// we change each finite bound to 0 (i.e. the objective function
1907 1939
    /// coefficients in the primal problem), and we looking for a
1908 1940
    /// ositive objective value. If there is such solution, that
1909 1941
    /// proofs the unsolvability of the primal problem, and if a
1910 1942
    /// feasible dual solution exists, then the unboundness of
1911 1943
    /// dual problem.
1912 1944
    ///
1913 1945
    /// \pre The problem is solved and the primal problem is infeasible.
1914 1946
    /// \note Some solvers does not provide dual ray calculation
1915 1947
    /// functions.
1916 1948
    Value dualRay(Row r) const { return _getDualRay(rows(id(r))); }
1917 1949

	
1918 1950
    /// Return the basis status of the column
1919 1951

	
1920 1952
    /// \see VarStatus
1921 1953
    VarStatus colStatus(Col c) const { return _getColStatus(cols(id(c))); }
1922 1954

	
1923 1955
    /// Return the basis status of the row
1924 1956

	
1925 1957
    /// \see VarStatus
1926 1958
    VarStatus rowStatus(Row r) const { return _getRowStatus(rows(id(r))); }
1927 1959

	
1928 1960
    ///The value of the objective function
1929 1961

	
1930 1962
    ///\return
1931 1963
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1932 1964
    /// of the primal problem, depending on whether we minimize or maximize.
1933 1965
    ///- \ref NaN if no primal solution is found.
1934 1966
    ///- The (finite) objective value if an optimal solution is found.
1935 1967
    Value primal() const { return _getPrimalValue()+obj_const_comp;}
1936 1968
    ///@}
1937 1969

	
1938
    LpSolver* newSolver() {return _newSolver();}
1939
    LpSolver* cloneSolver() {return _cloneSolver();}
1940

	
1941 1970
  protected:
1942 1971

	
1943
    virtual LpSolver* _newSolver() const = 0;
1944
    virtual LpSolver* _cloneSolver() const = 0;
1945 1972
  };
1946 1973

	
1947 1974

	
1948 1975
  /// \ingroup lp_group
1949 1976
  ///
1950 1977
  /// \brief Common base class for MIP solvers
1951 1978
  ///
1952 1979
  /// This class is an abstract base class for MIP solvers. This class
1953 1980
  /// provides a full interface for set and modify an MIP problem,
1954 1981
  /// solve it and retrieve the solution. You can use one of the
1955 1982
  /// descendants as a concrete implementation, or the \c Lp
1956 1983
  /// default MIP solver. However, if you would like to handle MIP
1957 1984
  /// solvers as reference or pointer in a generic way, you can use
1958 1985
  /// this class directly.
1959 1986
  class MipSolver : virtual public LpBase {
1960 1987
  public:
1961 1988

	
1962 1989
    /// The problem types for MIP problems
1963 1990
    enum ProblemType {
1964
      ///Feasible solution hasn't been found (but may exist).
1991
      /// = 0. Feasible solution hasn't been found (but may exist).
1965 1992
      UNDEFINED = 0,
1966
      ///The problem has no feasible solution
1993
      /// = 1. The problem has no feasible solution.
1967 1994
      INFEASIBLE = 1,
1968
      ///Feasible solution found
1995
      /// = 2. Feasible solution found.
1969 1996
      FEASIBLE = 2,
1970
      ///Optimal solution exists and found
1997
      /// = 3. Optimal solution exists and found.
1971 1998
      OPTIMAL = 3,
1972
      ///The cost function is unbounded
1973
      ///
1974
      ///The Mip or at least the relaxed problem is unbounded
1999
      /// = 4. The cost function is unbounded.
2000
      ///The Mip or at least the relaxed problem is unbounded.
1975 2001
      UNBOUNDED = 4
1976 2002
    };
1977 2003

	
2004
    ///Allocate a new MIP problem instance
2005
    virtual MipSolver* newSolver() const = 0;
2006
    ///Make a copy of the MIP problem
2007
    virtual MipSolver* cloneSolver() const = 0;
2008

	
1978 2009
    ///\name Solve the MIP
1979 2010

	
1980 2011
    ///@{
1981 2012

	
1982 2013
    /// Solve the MIP problem at hand
1983 2014
    ///
1984 2015
    ///\return The result of the optimization procedure. Possible
1985 2016
    ///values and their meanings can be found in the documentation of
1986 2017
    ///\ref SolveExitStatus.
1987 2018
    SolveExitStatus solve() { return _solve(); }
1988 2019

	
1989 2020
    ///@}
1990 2021

	
1991
    ///\name Setting column type
2022
    ///\name Set Column Type
1992 2023
    ///@{
1993 2024

	
1994 2025
    ///Possible variable (column) types (e.g. real, integer, binary etc.)
1995 2026
    enum ColTypes {
1996
      ///Continuous variable (default)
2027
      /// = 0. Continuous variable (default).
1997 2028
      REAL = 0,
1998
      ///Integer variable
2029
      /// = 1. Integer variable.
1999 2030
      INTEGER = 1
2000 2031
    };
2001 2032

	
2002 2033
    ///Sets the type of the given column to the given type
2003 2034

	
2004 2035
    ///Sets the type of the given column to the given type.
2005 2036
    ///
2006 2037
    void colType(Col c, ColTypes col_type) {
2007 2038
      _setColType(cols(id(c)),col_type);
2008 2039
    }
2009 2040

	
2010 2041
    ///Gives back the type of the column.
2011 2042

	
2012 2043
    ///Gives back the type of the column.
2013 2044
    ///
2014 2045
    ColTypes colType(Col c) const {
2015 2046
      return _getColType(cols(id(c)));
2016 2047
    }
2017 2048
    ///@}
2018 2049

	
2019
    ///\name Obtain the solution
2050
    ///\name Obtain the Solution
2020 2051

	
2021 2052
    ///@{
2022 2053

	
2023 2054
    /// The type of the MIP problem
2024 2055
    ProblemType type() const {
2025 2056
      return _getType();
2026 2057
    }
2027 2058

	
2028 2059
    /// Return the value of the row in the solution
2029 2060

	
2030 2061
    ///  Return the value of the row in the solution.
2031 2062
    /// \pre The problem is solved.
2032 2063
    Value sol(Col c) const { return _getSol(cols(id(c))); }
2033 2064

	
2034 2065
    /// Return the value of the expression in the solution
2035 2066

	
2036 2067
    /// Return the value of the expression in the solution, i.e. the
2037 2068
    /// dot product of the solution and the expression.
2038 2069
    /// \pre The problem is solved.
2039 2070
    Value sol(const Expr& e) const {
2040 2071
      double res = *e;
2041 2072
      for (Expr::ConstCoeffIt c(e); c != INVALID; ++c) {
2042 2073
        res += *c * sol(c);
2043 2074
      }
2044 2075
      return res;
2045 2076
    }
2046 2077
    ///The value of the objective function
2047 2078
    
2048 2079
    ///\return
2049 2080
    ///- \ref INF or -\ref INF means either infeasibility or unboundedness
2050 2081
    /// of the problem, depending on whether we minimize or maximize.
2051 2082
    ///- \ref NaN if no primal solution is found.
2052 2083
    ///- The (finite) objective value if an optimal solution is found.
2053 2084
    Value solValue() const { return _getSolValue()+obj_const_comp;}
2054 2085
    ///@}
2055 2086

	
2056 2087
  protected:
2057 2088

	
2058 2089
    virtual SolveExitStatus _solve() = 0;
2059 2090
    virtual ColTypes _getColType(int col) const = 0;
2060 2091
    virtual void _setColType(int col, ColTypes col_type) = 0;
2061 2092
    virtual ProblemType _getType() const = 0;
2062 2093
    virtual Value _getSol(int i) const = 0;
2063 2094
    virtual Value _getSolValue() const = 0;
2064 2095

	
2065
  public:
2066

	
2067
    MipSolver* newSolver() {return _newSolver();}
2068
    MipSolver* cloneSolver() {return _cloneSolver();}
2069

	
2070
  protected:
2071

	
2072
    virtual MipSolver* _newSolver() const = 0;
2073
    virtual MipSolver* _cloneSolver() const = 0;
2074 2096
  };
2075 2097

	
2076 2098

	
2077 2099

	
2078 2100
} //namespace lemon
2079 2101

	
2080 2102
#endif //LEMON_LP_BASE_H
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/lp_skeleton.h>
20 20

	
21 21
///\file
22 22
///\brief A skeleton file to implement LP solver interfaces
23 23
namespace lemon {
24 24

	
25 25
  int SkeletonSolverBase::_addCol()
26 26
  {
27 27
    return ++col_num;
28 28
  }
29 29

	
30 30
  int SkeletonSolverBase::_addRow()
31 31
  {
32 32
    return ++row_num;
33 33
  }
34 34

	
35
  int SkeletonSolverBase::_addRow(Value, ExprIterator, ExprIterator, Value)
36
  {
37
    return ++row_num;
38
  }
39

	
35 40
  void SkeletonSolverBase::_eraseCol(int) {}
36 41
  void SkeletonSolverBase::_eraseRow(int) {}
37 42

	
38 43
  void SkeletonSolverBase::_getColName(int, std::string &) const {}
39 44
  void SkeletonSolverBase::_setColName(int, const std::string &) {}
40 45
  int SkeletonSolverBase::_colByName(const std::string&) const { return -1; }
41 46

	
42 47
  void SkeletonSolverBase::_getRowName(int, std::string &) const {}
43 48
  void SkeletonSolverBase::_setRowName(int, const std::string &) {}
44 49
  int SkeletonSolverBase::_rowByName(const std::string&) const { return -1; }
45 50

	
46 51
  void SkeletonSolverBase::_setRowCoeffs(int, ExprIterator, ExprIterator) {}
47 52
  void SkeletonSolverBase::_getRowCoeffs(int, InsertIterator) const {}
48 53

	
49 54
  void SkeletonSolverBase::_setColCoeffs(int, ExprIterator, ExprIterator) {}
50 55
  void SkeletonSolverBase::_getColCoeffs(int, InsertIterator) const {}
51 56

	
52 57
  void SkeletonSolverBase::_setCoeff(int, int, Value) {}
53 58
  SkeletonSolverBase::Value SkeletonSolverBase::_getCoeff(int, int) const
54 59
  { return 0; }
55 60

	
56 61
  void SkeletonSolverBase::_setColLowerBound(int, Value) {}
57 62
  SkeletonSolverBase::Value SkeletonSolverBase::_getColLowerBound(int) const
58 63
  {  return 0; }
59 64

	
60 65
  void SkeletonSolverBase::_setColUpperBound(int, Value) {}
61 66
  SkeletonSolverBase::Value SkeletonSolverBase::_getColUpperBound(int) const
62 67
  {  return 0; }
63 68

	
64 69
  void SkeletonSolverBase::_setRowLowerBound(int, Value) {}
65 70
  SkeletonSolverBase::Value SkeletonSolverBase::_getRowLowerBound(int) const
66 71
  {  return 0; }
67 72

	
68 73
  void SkeletonSolverBase::_setRowUpperBound(int, Value) {}
69 74
  SkeletonSolverBase::Value SkeletonSolverBase::_getRowUpperBound(int) const
70 75
  {  return 0; }
71 76

	
72 77
  void SkeletonSolverBase::_setObjCoeffs(ExprIterator, ExprIterator) {}
73 78
  void SkeletonSolverBase::_getObjCoeffs(InsertIterator) const {};
74 79

	
75 80
  void SkeletonSolverBase::_setObjCoeff(int, Value) {}
76 81
  SkeletonSolverBase::Value SkeletonSolverBase::_getObjCoeff(int) const
77 82
  {  return 0; }
78 83

	
79 84
  void SkeletonSolverBase::_setSense(Sense) {}
80 85
  SkeletonSolverBase::Sense SkeletonSolverBase::_getSense() const
81 86
  { return MIN; }
82 87

	
83 88
  void SkeletonSolverBase::_clear() {
84 89
    row_num = col_num = 0;
85 90
  }
86 91

	
92
  void SkeletonSolverBase::_messageLevel(MessageLevel) {}
93

	
87 94
  LpSkeleton::SolveExitStatus LpSkeleton::_solve() { return SOLVED; }
88 95

	
89 96
  LpSkeleton::Value LpSkeleton::_getPrimal(int) const { return 0; }
90 97
  LpSkeleton::Value LpSkeleton::_getDual(int) const { return 0; }
91 98
  LpSkeleton::Value LpSkeleton::_getPrimalValue() const { return 0; }
92 99

	
93 100
  LpSkeleton::Value LpSkeleton::_getPrimalRay(int) const { return 0; }
94 101
  LpSkeleton::Value LpSkeleton::_getDualRay(int) const { return 0; }
95 102

	
96 103
  LpSkeleton::ProblemType LpSkeleton::_getPrimalType() const
97 104
  { return UNDEFINED; }
98 105

	
99 106
  LpSkeleton::ProblemType LpSkeleton::_getDualType() const
100 107
  { return UNDEFINED; }
101 108

	
102 109
  LpSkeleton::VarStatus LpSkeleton::_getColStatus(int) const
103 110
  { return BASIC; }
104 111

	
105 112
  LpSkeleton::VarStatus LpSkeleton::_getRowStatus(int) const
106 113
  { return BASIC; }
107 114

	
108
  LpSkeleton* LpSkeleton::_newSolver() const
115
  LpSkeleton* LpSkeleton::newSolver() const
109 116
  { return static_cast<LpSkeleton*>(0); }
110 117

	
111
  LpSkeleton* LpSkeleton::_cloneSolver() const
118
  LpSkeleton* LpSkeleton::cloneSolver() const
112 119
  { return static_cast<LpSkeleton*>(0); }
113 120

	
114 121
  const char* LpSkeleton::_solverName() const { return "LpSkeleton"; }
115 122

	
116 123
  MipSkeleton::SolveExitStatus MipSkeleton::_solve()
117 124
  { return SOLVED; }
118 125

	
119 126
  MipSkeleton::Value MipSkeleton::_getSol(int) const { return 0; }
120 127
  MipSkeleton::Value MipSkeleton::_getSolValue() const { return 0; }
121 128

	
122 129
  MipSkeleton::ProblemType MipSkeleton::_getType() const
123 130
  { return UNDEFINED; }
124 131

	
125
  MipSkeleton* MipSkeleton::_newSolver() const
132
  MipSkeleton* MipSkeleton::newSolver() const
126 133
  { return static_cast<MipSkeleton*>(0); }
127 134

	
128
  MipSkeleton* MipSkeleton::_cloneSolver() const
135
  MipSkeleton* MipSkeleton::cloneSolver() const
129 136
  { return static_cast<MipSkeleton*>(0); }
130 137

	
131 138
  const char* MipSkeleton::_solverName() const { return "MipSkeleton"; }
132 139

	
133 140
} //namespace lemon
134 141

	
Ignore white space 6 line context
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
#ifndef LEMON_LP_SKELETON
20
#define LEMON_LP_SKELETON
19
#ifndef LEMON_LP_SKELETON_H
20
#define LEMON_LP_SKELETON_H
21 21

	
22 22
#include <lemon/lp_base.h>
23 23

	
24 24
///\file
25
///\brief A skeleton file to implement LP solver interfaces
25
///\brief Skeleton file to implement LP/MIP solver interfaces
26
///  
27
///The classes in this file do nothing, but they can serve as skeletons when
28
///implementing an interface to new solvers.
26 29
namespace lemon {
27 30

	
28
  ///A skeleton class to implement LP solver interfaces
31
  ///A skeleton class to implement LP/MIP solver base interface
32
  
33
  ///This class does nothing, but it can serve as a skeleton when
34
  ///implementing an interface to new solvers.
29 35
  class SkeletonSolverBase : public virtual LpBase {
30 36
    int col_num,row_num;
31 37

	
32 38
  protected:
33 39

	
34 40
    SkeletonSolverBase()
35 41
      : col_num(-1), row_num(-1) {}
36 42

	
37 43
    /// \e
38 44
    virtual int _addCol();
39 45
    /// \e
40 46
    virtual int _addRow();
41 47
    /// \e
48
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
49
    /// \e
42 50
    virtual void _eraseCol(int i);
43 51
    /// \e
44 52
    virtual void _eraseRow(int i);
45 53

	
46 54
    /// \e
47 55
    virtual void _getColName(int col, std::string& name) const;
48 56
    /// \e
49 57
    virtual void _setColName(int col, const std::string& name);
50 58
    /// \e
51 59
    virtual int _colByName(const std::string& name) const;
52 60

	
53 61
    /// \e
54 62
    virtual void _getRowName(int row, std::string& name) const;
55 63
    /// \e
56 64
    virtual void _setRowName(int row, const std::string& name);
57 65
    /// \e
58 66
    virtual int _rowByName(const std::string& name) const;
59 67

	
60 68
    /// \e
61 69
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
62 70
    /// \e
63 71
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
64 72
    /// \e
65 73
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
66 74
    /// \e
67 75
    virtual void _getColCoeffs(int i, InsertIterator b) const;
68 76

	
69 77
    /// Set one element of the coefficient matrix
70 78
    virtual void _setCoeff(int row, int col, Value value);
71 79

	
72 80
    /// Get one element of the coefficient matrix
73 81
    virtual Value _getCoeff(int row, int col) const;
74 82

	
75 83
    /// The lower bound of a variable (column) have to be given by an
76 84
    /// extended number of type Value, i.e. a finite number of type
77 85
    /// Value or -\ref INF.
78 86
    virtual void _setColLowerBound(int i, Value value);
79 87
    /// \e
80 88

	
81 89
    /// The lower bound of a variable (column) is an
82 90
    /// extended number of type Value, i.e. a finite number of type
83 91
    /// Value or -\ref INF.
84 92
    virtual Value _getColLowerBound(int i) const;
85 93

	
86 94
    /// The upper bound of a variable (column) have to be given by an
87 95
    /// extended number of type Value, i.e. a finite number of type
88 96
    /// Value or \ref INF.
89 97
    virtual void _setColUpperBound(int i, Value value);
90 98
    /// \e
91 99

	
92 100
    /// The upper bound of a variable (column) is an
93 101
    /// extended number of type Value, i.e. a finite number of type
94 102
    /// Value or \ref INF.
95 103
    virtual Value _getColUpperBound(int i) const;
96 104

	
97 105
    /// The lower bound of a constraint (row) have to be given by an
98 106
    /// extended number of type Value, i.e. a finite number of type
99 107
    /// Value or -\ref INF.
100 108
    virtual void _setRowLowerBound(int i, Value value);
101 109
    /// \e
102 110

	
103 111
    /// The lower bound of a constraint (row) is an
104 112
    /// extended number of type Value, i.e. a finite number of type
105 113
    /// Value or -\ref INF.
106 114
    virtual Value _getRowLowerBound(int i) const;
107 115

	
108 116
    /// The upper bound of a constraint (row) have to be given by an
109 117
    /// extended number of type Value, i.e. a finite number of type
110 118
    /// Value or \ref INF.
111 119
    virtual void _setRowUpperBound(int i, Value value);
112 120
    /// \e
113 121

	
114 122
    /// The upper bound of a constraint (row) is an
115 123
    /// extended number of type Value, i.e. a finite number of type
116 124
    /// Value or \ref INF.
117 125
    virtual Value _getRowUpperBound(int i) const;
118 126

	
119 127
    /// \e
120 128
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
121 129
    /// \e
122 130
    virtual void _getObjCoeffs(InsertIterator b) const;
123 131

	
124 132
    /// \e
125 133
    virtual void _setObjCoeff(int i, Value obj_coef);
126 134
    /// \e
127 135
    virtual Value _getObjCoeff(int i) const;
128 136

	
129 137
    ///\e
130 138
    virtual void _setSense(Sense);
131 139
    ///\e
132 140
    virtual Sense _getSense() const;
133 141

	
134 142
    ///\e
135 143
    virtual void _clear();
136 144

	
145
    ///\e
146
    virtual void _messageLevel(MessageLevel);
137 147
  };
138 148

	
139
  /// \brief Interface for a skeleton LP solver
149
  /// \brief Skeleton class for an LP solver interface
140 150
  ///
141
  /// This class implements an interface for a skeleton LP solver.
151
  ///This class does nothing, but it can serve as a skeleton when
152
  ///implementing an interface to new solvers.
153

	
142 154
  ///\ingroup lp_group
143
  class LpSkeleton : public SkeletonSolverBase, public LpSolver {
155
  class LpSkeleton : public LpSolver, public SkeletonSolverBase {
144 156
  public:
145
    LpSkeleton() : SkeletonSolverBase(), LpSolver() {}
146

	
157
    ///\e
158
    LpSkeleton() : LpSolver(), SkeletonSolverBase() {}
159
    ///\e
160
    virtual LpSkeleton* newSolver() const;
161
    ///\e
162
    virtual LpSkeleton* cloneSolver() const;
147 163
  protected:
148 164

	
149 165
    ///\e
150 166
    virtual SolveExitStatus _solve();
151 167

	
152 168
    ///\e
153 169
    virtual Value _getPrimal(int i) const;
154 170
    ///\e
155 171
    virtual Value _getDual(int i) const;
156 172

	
157 173
    ///\e
158 174
    virtual Value _getPrimalValue() const;
159 175

	
160 176
    ///\e
161 177
    virtual Value _getPrimalRay(int i) const;
162 178
    ///\e
163 179
    virtual Value _getDualRay(int i) const;
164 180

	
165 181
    ///\e
166 182
    virtual ProblemType _getPrimalType() const;
167 183
    ///\e
168 184
    virtual ProblemType _getDualType() const;
169 185

	
170 186
    ///\e
171 187
    virtual VarStatus _getColStatus(int i) const;
172 188
    ///\e
173 189
    virtual VarStatus _getRowStatus(int i) const;
174 190

	
175 191
    ///\e
176
    virtual LpSkeleton* _newSolver() const;
177
    ///\e
178
    virtual LpSkeleton* _cloneSolver() const;
179
    ///\e
180 192
    virtual const char* _solverName() const;
181 193

	
182 194
  };
183 195

	
184
  /// \brief Interface for a skeleton MIP solver
196
  /// \brief Skeleton class for a MIP solver interface
185 197
  ///
186
  /// This class implements an interface for a skeleton MIP solver.
198
  ///This class does nothing, but it can serve as a skeleton when
199
  ///implementing an interface to new solvers.
187 200
  ///\ingroup lp_group
188
  class MipSkeleton : public SkeletonSolverBase, public MipSolver {
201
  class MipSkeleton : public MipSolver, public SkeletonSolverBase {
189 202
  public:
190
    MipSkeleton() : SkeletonSolverBase(), MipSolver() {}
203
    ///\e
204
    MipSkeleton() : MipSolver(), SkeletonSolverBase() {}
205
    ///\e
206
    virtual MipSkeleton* newSolver() const;
207
    ///\e
208
    virtual MipSkeleton* cloneSolver() const;
191 209

	
192 210
  protected:
193 211
    ///\e
194

	
195
    ///\bug Wrong interface
196
    ///
197 212
    virtual SolveExitStatus _solve();
198 213

	
199 214
    ///\e
200

	
201
    ///\bug Wrong interface
202
    ///
203 215
    virtual Value _getSol(int i) const;
204 216

	
205 217
    ///\e
206

	
207
    ///\bug Wrong interface
208
    ///
209 218
    virtual Value _getSolValue() const;
210 219

	
211 220
    ///\e
212

	
213
    ///\bug Wrong interface
214
    ///
215 221
    virtual ProblemType _getType() const;
216 222

	
217 223
    ///\e
218
    virtual MipSkeleton* _newSolver() const;
219

	
220
    ///\e
221
    virtual MipSkeleton* _cloneSolver() const;
222
    ///\e
223 224
    virtual const char* _solverName() const;
224

	
225 225
  };
226 226

	
227 227
} //namespace lemon
228 228

	
229
#endif // LEMON_LP_SKELETON
229
#endif
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_MAPS_H
20 20
#define LEMON_MAPS_H
21 21

	
22 22
#include <iterator>
23 23
#include <functional>
24 24
#include <vector>
25
#include <map>
25 26

	
26 27
#include <lemon/core.h>
27 28

	
28 29
///\file
29 30
///\ingroup maps
30 31
///\brief Miscellaneous property maps
31 32

	
32
#include <map>
33

	
34 33
namespace lemon {
35 34

	
36 35
  /// \addtogroup maps
37 36
  /// @{
38 37

	
39 38
  /// Base class of maps.
40 39

	
41 40
  /// Base class of maps. It provides the necessary type definitions
42 41
  /// required by the map %concepts.
43 42
  template<typename K, typename V>
44 43
  class MapBase {
45 44
  public:
46 45
    /// \brief The key type of the map.
47 46
    typedef K Key;
48 47
    /// \brief The value type of the map.
49 48
    /// (The type of objects associated with the keys).
50 49
    typedef V Value;
51 50
  };
52 51

	
53 52

	
54 53
  /// Null map. (a.k.a. DoNothingMap)
55 54

	
56 55
  /// This map can be used if you have to provide a map only for
57 56
  /// its type definitions, or if you have to provide a writable map,
58 57
  /// but data written to it is not required (i.e. it will be sent to
59 58
  /// <tt>/dev/null</tt>).
60
  /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
59
  /// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
61 60
  ///
62 61
  /// \sa ConstMap
63 62
  template<typename K, typename V>
64 63
  class NullMap : public MapBase<K, V> {
65 64
  public:
66
    typedef MapBase<K, V> Parent;
67
    typedef typename Parent::Key Key;
68
    typedef typename Parent::Value Value;
65
    ///\e
66
    typedef K Key;
67
    ///\e
68
    typedef V Value;
69 69

	
70 70
    /// Gives back a default constructed element.
71 71
    Value operator[](const Key&) const { return Value(); }
72 72
    /// Absorbs the value.
73 73
    void set(const Key&, const Value&) {}
74 74
  };
75 75

	
76 76
  /// Returns a \c NullMap class
77 77

	
78 78
  /// This function just returns a \c NullMap class.
79 79
  /// \relates NullMap
80 80
  template <typename K, typename V>
81 81
  NullMap<K, V> nullMap() {
82 82
    return NullMap<K, V>();
83 83
  }
84 84

	
85 85

	
86 86
  /// Constant map.
87 87

	
88 88
  /// This \ref concepts::ReadMap "readable map" assigns a specified
89 89
  /// value to each key.
90 90
  ///
91 91
  /// In other aspects it is equivalent to \c NullMap.
92
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
92
  /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
93 93
  /// concept, but it absorbs the data written to it.
94 94
  ///
95 95
  /// The simplest way of using this map is through the constMap()
96 96
  /// function.
97 97
  ///
98 98
  /// \sa NullMap
99 99
  /// \sa IdentityMap
100 100
  template<typename K, typename V>
101 101
  class ConstMap : public MapBase<K, V> {
102 102
  private:
103 103
    V _value;
104 104
  public:
105
    typedef MapBase<K, V> Parent;
106
    typedef typename Parent::Key Key;
107
    typedef typename Parent::Value Value;
105
    ///\e
106
    typedef K Key;
107
    ///\e
108
    typedef V Value;
108 109

	
109 110
    /// Default constructor
110 111

	
111 112
    /// Default constructor.
112 113
    /// The value of the map will be default constructed.
113 114
    ConstMap() {}
114 115

	
115 116
    /// Constructor with specified initial value
116 117

	
117 118
    /// Constructor with specified initial value.
118 119
    /// \param v The initial value of the map.
119 120
    ConstMap(const Value &v) : _value(v) {}
120 121

	
121 122
    /// Gives back the specified value.
122 123
    Value operator[](const Key&) const { return _value; }
123 124

	
124 125
    /// Absorbs the value.
125 126
    void set(const Key&, const Value&) {}
126 127

	
127 128
    /// Sets the value that is assigned to each key.
128 129
    void setAll(const Value &v) {
129 130
      _value = v;
130 131
    }
131 132

	
132 133
    template<typename V1>
133 134
    ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
134 135
  };
135 136

	
136 137
  /// Returns a \c ConstMap class
137 138

	
138 139
  /// This function just returns a \c ConstMap class.
139 140
  /// \relates ConstMap
140 141
  template<typename K, typename V>
141 142
  inline ConstMap<K, V> constMap(const V &v) {
142 143
    return ConstMap<K, V>(v);
143 144
  }
144 145

	
145 146
  template<typename K, typename V>
146 147
  inline ConstMap<K, V> constMap() {
147 148
    return ConstMap<K, V>();
148 149
  }
149 150

	
150 151

	
151 152
  template<typename T, T v>
152 153
  struct Const {};
153 154

	
154 155
  /// Constant map with inlined constant value.
155 156

	
156 157
  /// This \ref concepts::ReadMap "readable map" assigns a specified
157 158
  /// value to each key.
158 159
  ///
159 160
  /// In other aspects it is equivalent to \c NullMap.
160
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
161
  /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
161 162
  /// concept, but it absorbs the data written to it.
162 163
  ///
163 164
  /// The simplest way of using this map is through the constMap()
164 165
  /// function.
165 166
  ///
166 167
  /// \sa NullMap
167 168
  /// \sa IdentityMap
168 169
  template<typename K, typename V, V v>
169 170
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
170 171
  public:
171
    typedef MapBase<K, V> Parent;
172
    typedef typename Parent::Key Key;
173
    typedef typename Parent::Value Value;
172
    ///\e
173
    typedef K Key;
174
    ///\e
175
    typedef V Value;
174 176

	
175 177
    /// Constructor.
176 178
    ConstMap() {}
177 179

	
178 180
    /// Gives back the specified value.
179 181
    Value operator[](const Key&) const { return v; }
180 182

	
181 183
    /// Absorbs the value.
182 184
    void set(const Key&, const Value&) {}
183 185
  };
184 186

	
185 187
  /// Returns a \c ConstMap class with inlined constant value
186 188

	
187 189
  /// This function just returns a \c ConstMap class with inlined
188 190
  /// constant value.
189 191
  /// \relates ConstMap
190 192
  template<typename K, typename V, V v>
191 193
  inline ConstMap<K, Const<V, v> > constMap() {
192 194
    return ConstMap<K, Const<V, v> >();
193 195
  }
194 196

	
195 197

	
196 198
  /// Identity map.
197 199

	
198 200
  /// This \ref concepts::ReadMap "read-only map" gives back the given
199 201
  /// key as value without any modification.
200 202
  ///
201 203
  /// \sa ConstMap
202 204
  template <typename T>
203 205
  class IdentityMap : public MapBase<T, T> {
204 206
  public:
205
    typedef MapBase<T, T> Parent;
206
    typedef typename Parent::Key Key;
207
    typedef typename Parent::Value Value;
207
    ///\e
208
    typedef T Key;
209
    ///\e
210
    typedef T Value;
208 211

	
209 212
    /// Gives back the given value without any modification.
210 213
    Value operator[](const Key &k) const {
211 214
      return k;
212 215
    }
213 216
  };
214 217

	
215 218
  /// Returns an \c IdentityMap class
216 219

	
217 220
  /// This function just returns an \c IdentityMap class.
218 221
  /// \relates IdentityMap
219 222
  template<typename T>
220 223
  inline IdentityMap<T> identityMap() {
221 224
    return IdentityMap<T>();
222 225
  }
223 226

	
224 227

	
225 228
  /// \brief Map for storing values for integer keys from the range
226 229
  /// <tt>[0..size-1]</tt>.
227 230
  ///
228 231
  /// This map is essentially a wrapper for \c std::vector. It assigns
229 232
  /// values to integer keys from the range <tt>[0..size-1]</tt>.
230 233
  /// It can be used with some data structures, for example
231 234
  /// \c UnionFind, \c BinHeap, when the used items are small
232
  /// integers. This map conforms the \ref concepts::ReferenceMap
235
  /// integers. This map conforms to the \ref concepts::ReferenceMap
233 236
  /// "ReferenceMap" concept.
234 237
  ///
235 238
  /// The simplest way of using this map is through the rangeMap()
236 239
  /// function.
237 240
  template <typename V>
238 241
  class RangeMap : public MapBase<int, V> {
239 242
    template <typename V1>
240 243
    friend class RangeMap;
241 244
  private:
242 245

	
243 246
    typedef std::vector<V> Vector;
244 247
    Vector _vector;
245 248

	
246 249
  public:
247 250

	
248
    typedef MapBase<int, V> Parent;
249 251
    /// Key type
250
    typedef typename Parent::Key Key;
252
    typedef int Key;
251 253
    /// Value type
252
    typedef typename Parent::Value Value;
254
    typedef V Value;
253 255
    /// Reference type
254 256
    typedef typename Vector::reference Reference;
255 257
    /// Const reference type
256 258
    typedef typename Vector::const_reference ConstReference;
257 259

	
258 260
    typedef True ReferenceMapTag;
259 261

	
260 262
  public:
261 263

	
262 264
    /// Constructor with specified default value.
263 265
    RangeMap(int size = 0, const Value &value = Value())
264 266
      : _vector(size, value) {}
265 267

	
266 268
    /// Constructs the map from an appropriate \c std::vector.
267 269
    template <typename V1>
268 270
    RangeMap(const std::vector<V1>& vector)
269 271
      : _vector(vector.begin(), vector.end()) {}
270 272

	
271 273
    /// Constructs the map from another \c RangeMap.
272 274
    template <typename V1>
273 275
    RangeMap(const RangeMap<V1> &c)
274 276
      : _vector(c._vector.begin(), c._vector.end()) {}
275 277

	
276 278
    /// Returns the size of the map.
277 279
    int size() {
278 280
      return _vector.size();
279 281
    }
280 282

	
281 283
    /// Resizes the map.
282 284

	
283 285
    /// Resizes the underlying \c std::vector container, so changes the
284 286
    /// keyset of the map.
285 287
    /// \param size The new size of the map. The new keyset will be the
286 288
    /// range <tt>[0..size-1]</tt>.
287 289
    /// \param value The default value to assign to the new keys.
288 290
    void resize(int size, const Value &value = Value()) {
289 291
      _vector.resize(size, value);
290 292
    }
291 293

	
292 294
  private:
293 295

	
294 296
    RangeMap& operator=(const RangeMap&);
295 297

	
296 298
  public:
297 299

	
298 300
    ///\e
299 301
    Reference operator[](const Key &k) {
300 302
      return _vector[k];
301 303
    }
302 304

	
303 305
    ///\e
304 306
    ConstReference operator[](const Key &k) const {
305 307
      return _vector[k];
306 308
    }
307 309

	
308 310
    ///\e
309 311
    void set(const Key &k, const Value &v) {
310 312
      _vector[k] = v;
311 313
    }
312 314
  };
313 315

	
314 316
  /// Returns a \c RangeMap class
315 317

	
316 318
  /// This function just returns a \c RangeMap class.
317 319
  /// \relates RangeMap
318 320
  template<typename V>
319 321
  inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
320 322
    return RangeMap<V>(size, value);
321 323
  }
322 324

	
323 325
  /// \brief Returns a \c RangeMap class created from an appropriate
324 326
  /// \c std::vector
325 327

	
326 328
  /// This function just returns a \c RangeMap class created from an
327 329
  /// appropriate \c std::vector.
328 330
  /// \relates RangeMap
329 331
  template<typename V>
330 332
  inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
331 333
    return RangeMap<V>(vector);
332 334
  }
333 335

	
334 336

	
335 337
  /// Map type based on \c std::map
336 338

	
337 339
  /// This map is essentially a wrapper for \c std::map with addition
338 340
  /// that you can specify a default value for the keys that are not
339 341
  /// stored actually. This value can be different from the default
340 342
  /// contructed value (i.e. \c %Value()).
341
  /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
343
  /// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
342 344
  /// concept.
343 345
  ///
344 346
  /// This map is useful if a default value should be assigned to most of
345 347
  /// the keys and different values should be assigned only to a few
346 348
  /// keys (i.e. the map is "sparse").
347 349
  /// The name of this type also refers to this important usage.
348 350
  ///
349 351
  /// Apart form that this map can be used in many other cases since it
350 352
  /// is based on \c std::map, which is a general associative container.
351 353
  /// However keep in mind that it is usually not as efficient as other
352 354
  /// maps.
353 355
  ///
354 356
  /// The simplest way of using this map is through the sparseMap()
355 357
  /// function.
356
  template <typename K, typename V, typename Compare = std::less<K> >
358
  template <typename K, typename V, typename Comp = std::less<K> >
357 359
  class SparseMap : public MapBase<K, V> {
358 360
    template <typename K1, typename V1, typename C1>
359 361
    friend class SparseMap;
360 362
  public:
361 363

	
362
    typedef MapBase<K, V> Parent;
363 364
    /// Key type
364
    typedef typename Parent::Key Key;
365
    typedef K Key;
365 366
    /// Value type
366
    typedef typename Parent::Value Value;
367
    typedef V Value;
367 368
    /// Reference type
368 369
    typedef Value& Reference;
369 370
    /// Const reference type
370 371
    typedef const Value& ConstReference;
371 372

	
372 373
    typedef True ReferenceMapTag;
373 374

	
374 375
  private:
375 376

	
376
    typedef std::map<K, V, Compare> Map;
377
    typedef std::map<K, V, Comp> Map;
377 378
    Map _map;
378 379
    Value _value;
379 380

	
380 381
  public:
381 382

	
382 383
    /// \brief Constructor with specified default value.
383 384
    SparseMap(const Value &value = Value()) : _value(value) {}
384 385
    /// \brief Constructs the map from an appropriate \c std::map, and
385 386
    /// explicitly specifies a default value.
386 387
    template <typename V1, typename Comp1>
387 388
    SparseMap(const std::map<Key, V1, Comp1> &map,
388 389
              const Value &value = Value())
389 390
      : _map(map.begin(), map.end()), _value(value) {}
390 391

	
391 392
    /// \brief Constructs the map from another \c SparseMap.
392 393
    template<typename V1, typename Comp1>
393 394
    SparseMap(const SparseMap<Key, V1, Comp1> &c)
394 395
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
395 396

	
396 397
  private:
397 398

	
398 399
    SparseMap& operator=(const SparseMap&);
399 400

	
400 401
  public:
401 402

	
402 403
    ///\e
403 404
    Reference operator[](const Key &k) {
404 405
      typename Map::iterator it = _map.lower_bound(k);
405 406
      if (it != _map.end() && !_map.key_comp()(k, it->first))
406 407
        return it->second;
407 408
      else
408 409
        return _map.insert(it, std::make_pair(k, _value))->second;
409 410
    }
410 411

	
411 412
    ///\e
412 413
    ConstReference operator[](const Key &k) const {
413 414
      typename Map::const_iterator it = _map.find(k);
414 415
      if (it != _map.end())
415 416
        return it->second;
416 417
      else
417 418
        return _value;
418 419
    }
419 420

	
420 421
    ///\e
421 422
    void set(const Key &k, const Value &v) {
422 423
      typename Map::iterator it = _map.lower_bound(k);
423 424
      if (it != _map.end() && !_map.key_comp()(k, it->first))
424 425
        it->second = v;
425 426
      else
426 427
        _map.insert(it, std::make_pair(k, v));
427 428
    }
428 429

	
429 430
    ///\e
430 431
    void setAll(const Value &v) {
431 432
      _value = v;
432 433
      _map.clear();
433 434
    }
434 435
  };
435 436

	
436 437
  /// Returns a \c SparseMap class
437 438

	
438 439
  /// This function just returns a \c SparseMap class with specified
439 440
  /// default value.
440 441
  /// \relates SparseMap
441 442
  template<typename K, typename V, typename Compare>
442 443
  inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
443 444
    return SparseMap<K, V, Compare>(value);
444 445
  }
445 446

	
446 447
  template<typename K, typename V>
447 448
  inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
448 449
    return SparseMap<K, V, std::less<K> >(value);
449 450
  }
450 451

	
451 452
  /// \brief Returns a \c SparseMap class created from an appropriate
452 453
  /// \c std::map
453 454

	
454 455
  /// This function just returns a \c SparseMap class created from an
455 456
  /// appropriate \c std::map.
456 457
  /// \relates SparseMap
457 458
  template<typename K, typename V, typename Compare>
458 459
  inline SparseMap<K, V, Compare>
459 460
    sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
460 461
  {
461 462
    return SparseMap<K, V, Compare>(map, value);
462 463
  }
463 464

	
464 465
  /// @}
465 466

	
466 467
  /// \addtogroup map_adaptors
467 468
  /// @{
468 469

	
469 470
  /// Composition of two maps
470 471

	
471 472
  /// This \ref concepts::ReadMap "read-only map" returns the
472 473
  /// composition of two given maps. That is to say, if \c m1 is of
473 474
  /// type \c M1 and \c m2 is of \c M2, then for
474 475
  /// \code
475 476
  ///   ComposeMap<M1, M2> cm(m1,m2);
476 477
  /// \endcode
477 478
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
478 479
  ///
479 480
  /// The \c Key type of the map is inherited from \c M2 and the
480 481
  /// \c Value type is from \c M1.
481 482
  /// \c M2::Value must be convertible to \c M1::Key.
482 483
  ///
483 484
  /// The simplest way of using this map is through the composeMap()
484 485
  /// function.
485 486
  ///
486 487
  /// \sa CombineMap
487 488
  template <typename M1, typename M2>
488 489
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
489 490
    const M1 &_m1;
490 491
    const M2 &_m2;
491 492
  public:
492
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
493
    typedef typename Parent::Key Key;
494
    typedef typename Parent::Value Value;
493
    ///\e
494
    typedef typename M2::Key Key;
495
    ///\e
496
    typedef typename M1::Value Value;
495 497

	
496 498
    /// Constructor
497 499
    ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
498 500

	
499
    /// \e
501
    ///\e
500 502
    typename MapTraits<M1>::ConstReturnValue
501 503
    operator[](const Key &k) const { return _m1[_m2[k]]; }
502 504
  };
503 505

	
504 506
  /// Returns a \c ComposeMap class
505 507

	
506 508
  /// This function just returns a \c ComposeMap class.
507 509
  ///
508 510
  /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
509 511
  /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
510 512
  /// will be equal to <tt>m1[m2[x]]</tt>.
511 513
  ///
512 514
  /// \relates ComposeMap
513 515
  template <typename M1, typename M2>
514 516
  inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
515 517
    return ComposeMap<M1, M2>(m1, m2);
516 518
  }
517 519

	
518 520

	
519 521
  /// Combination of two maps using an STL (binary) functor.
520 522

	
521 523
  /// This \ref concepts::ReadMap "read-only map" takes two maps and a
522 524
  /// binary functor and returns the combination of the two given maps
523 525
  /// using the functor.
524 526
  /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
525 527
  /// and \c f is of \c F, then for
526 528
  /// \code
527 529
  ///   CombineMap<M1,M2,F,V> cm(m1,m2,f);
528 530
  /// \endcode
529 531
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
530 532
  ///
531 533
  /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
532 534
  /// must be convertible to \c M2::Key) and the \c Value type is \c V.
533 535
  /// \c M2::Value and \c M1::Value must be convertible to the
534 536
  /// corresponding input parameter of \c F and the return type of \c F
535 537
  /// must be convertible to \c V.
536 538
  ///
537 539
  /// The simplest way of using this map is through the combineMap()
538 540
  /// function.
539 541
  ///
540 542
  /// \sa ComposeMap
541 543
  template<typename M1, typename M2, typename F,
542 544
           typename V = typename F::result_type>
543 545
  class CombineMap : public MapBase<typename M1::Key, V> {
544 546
    const M1 &_m1;
545 547
    const M2 &_m2;
546 548
    F _f;
547 549
  public:
548
    typedef MapBase<typename M1::Key, V> Parent;
549
    typedef typename Parent::Key Key;
550
    typedef typename Parent::Value Value;
550
    ///\e
551
    typedef typename M1::Key Key;
552
    ///\e
553
    typedef V Value;
551 554

	
552 555
    /// Constructor
553 556
    CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
554 557
      : _m1(m1), _m2(m2), _f(f) {}
555
    /// \e
558
    ///\e
556 559
    Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
557 560
  };
558 561

	
559 562
  /// Returns a \c CombineMap class
560 563

	
561 564
  /// This function just returns a \c CombineMap class.
562 565
  ///
563 566
  /// For example, if \c m1 and \c m2 are both maps with \c double
564 567
  /// values, then
565 568
  /// \code
566 569
  ///   combineMap(m1,m2,std::plus<double>())
567 570
  /// \endcode
568 571
  /// is equivalent to
569 572
  /// \code
570 573
  ///   addMap(m1,m2)
571 574
  /// \endcode
572 575
  ///
573 576
  /// This function is specialized for adaptable binary function
574 577
  /// classes and C++ functions.
575 578
  ///
576 579
  /// \relates CombineMap
577 580
  template<typename M1, typename M2, typename F, typename V>
578 581
  inline CombineMap<M1, M2, F, V>
579 582
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
580 583
    return CombineMap<M1, M2, F, V>(m1,m2,f);
581 584
  }
582 585

	
583 586
  template<typename M1, typename M2, typename F>
584 587
  inline CombineMap<M1, M2, F, typename F::result_type>
585 588
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
586 589
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
587 590
  }
588 591

	
589 592
  template<typename M1, typename M2, typename K1, typename K2, typename V>
590 593
  inline CombineMap<M1, M2, V (*)(K1, K2), V>
591 594
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
592 595
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
593 596
  }
594 597

	
595 598

	
596 599
  /// Converts an STL style (unary) functor to a map
597 600

	
598 601
  /// This \ref concepts::ReadMap "read-only map" returns the value
599 602
  /// of a given functor. Actually, it just wraps the functor and
600 603
  /// provides the \c Key and \c Value typedefs.
601 604
  ///
602 605
  /// Template parameters \c K and \c V will become its \c Key and
603 606
  /// \c Value. In most cases they have to be given explicitly because
604 607
  /// a functor typically does not provide \c argument_type and
605 608
  /// \c result_type typedefs.
606 609
  /// Parameter \c F is the type of the used functor.
607 610
  ///
608 611
  /// The simplest way of using this map is through the functorToMap()
609 612
  /// function.
610 613
  ///
611 614
  /// \sa MapToFunctor
612 615
  template<typename F,
613 616
           typename K = typename F::argument_type,
614 617
           typename V = typename F::result_type>
615 618
  class FunctorToMap : public MapBase<K, V> {
616 619
    F _f;
617 620
  public:
618
    typedef MapBase<K, V> Parent;
619
    typedef typename Parent::Key Key;
620
    typedef typename Parent::Value Value;
621
    ///\e
622
    typedef K Key;
623
    ///\e
624
    typedef V Value;
621 625

	
622 626
    /// Constructor
623 627
    FunctorToMap(const F &f = F()) : _f(f) {}
624
    /// \e
628
    ///\e
625 629
    Value operator[](const Key &k) const { return _f(k); }
626 630
  };
627 631

	
628 632
  /// Returns a \c FunctorToMap class
629 633

	
630 634
  /// This function just returns a \c FunctorToMap class.
631 635
  ///
632 636
  /// This function is specialized for adaptable binary function
633 637
  /// classes and C++ functions.
634 638
  ///
635 639
  /// \relates FunctorToMap
636 640
  template<typename K, typename V, typename F>
637 641
  inline FunctorToMap<F, K, V> functorToMap(const F &f) {
638 642
    return FunctorToMap<F, K, V>(f);
639 643
  }
640 644

	
641 645
  template <typename F>
642 646
  inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
643 647
    functorToMap(const F &f)
644 648
  {
645 649
    return FunctorToMap<F, typename F::argument_type,
646 650
      typename F::result_type>(f);
647 651
  }
648 652

	
649 653
  template <typename K, typename V>
650 654
  inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
651 655
    return FunctorToMap<V (*)(K), K, V>(f);
652 656
  }
653 657

	
654 658

	
655 659
  /// Converts a map to an STL style (unary) functor
656 660

	
657 661
  /// This class converts a map to an STL style (unary) functor.
658 662
  /// That is it provides an <tt>operator()</tt> to read its values.
659 663
  ///
660 664
  /// For the sake of convenience it also works as a usual
661 665
  /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
662 666
  /// and the \c Key and \c Value typedefs also exist.
663 667
  ///
664 668
  /// The simplest way of using this map is through the mapToFunctor()
665 669
  /// function.
666 670
  ///
667 671
  ///\sa FunctorToMap
668 672
  template <typename M>
669 673
  class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
670 674
    const M &_m;
671 675
  public:
672
    typedef MapBase<typename M::Key, typename M::Value> Parent;
673
    typedef typename Parent::Key Key;
674
    typedef typename Parent::Value Value;
675

	
676
    typedef typename Parent::Key argument_type;
677
    typedef typename Parent::Value result_type;
676
    ///\e
677
    typedef typename M::Key Key;
678
    ///\e
679
    typedef typename M::Value Value;
680

	
681
    typedef typename M::Key argument_type;
682
    typedef typename M::Value result_type;
678 683

	
679 684
    /// Constructor
680 685
    MapToFunctor(const M &m) : _m(m) {}
681
    /// \e
686
    ///\e
682 687
    Value operator()(const Key &k) const { return _m[k]; }
683
    /// \e
688
    ///\e
684 689
    Value operator[](const Key &k) const { return _m[k]; }
685 690
  };
686 691

	
687 692
  /// Returns a \c MapToFunctor class
688 693

	
689 694
  /// This function just returns a \c MapToFunctor class.
690 695
  /// \relates MapToFunctor
691 696
  template<typename M>
692 697
  inline MapToFunctor<M> mapToFunctor(const M &m) {
693 698
    return MapToFunctor<M>(m);
694 699
  }
695 700

	
696 701

	
697 702
  /// \brief Map adaptor to convert the \c Value type of a map to
698 703
  /// another type using the default conversion.
699 704

	
700 705
  /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
701 706
  /// "readable map" to another type using the default conversion.
702 707
  /// The \c Key type of it is inherited from \c M and the \c Value
703 708
  /// type is \c V.
704
  /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
709
  /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
705 710
  ///
706 711
  /// The simplest way of using this map is through the convertMap()
707 712
  /// function.
708 713
  template <typename M, typename V>
709 714
  class ConvertMap : public MapBase<typename M::Key, V> {
710 715
    const M &_m;
711 716
  public:
712
    typedef MapBase<typename M::Key, V> Parent;
713
    typedef typename Parent::Key Key;
714
    typedef typename Parent::Value Value;
717
    ///\e
718
    typedef typename M::Key Key;
719
    ///\e
720
    typedef V Value;
715 721

	
716 722
    /// Constructor
717 723

	
718 724
    /// Constructor.
719 725
    /// \param m The underlying map.
720 726
    ConvertMap(const M &m) : _m(m) {}
721 727

	
722
    /// \e
728
    ///\e
723 729
    Value operator[](const Key &k) const { return _m[k]; }
724 730
  };
725 731

	
726 732
  /// Returns a \c ConvertMap class
727 733

	
728 734
  /// This function just returns a \c ConvertMap class.
729 735
  /// \relates ConvertMap
730 736
  template<typename V, typename M>
731 737
  inline ConvertMap<M, V> convertMap(const M &map) {
732 738
    return ConvertMap<M, V>(map);
733 739
  }
734 740

	
735 741

	
736 742
  /// Applies all map setting operations to two maps
737 743

	
738 744
  /// This map has two \ref concepts::WriteMap "writable map" parameters
739 745
  /// and each write request will be passed to both of them.
740 746
  /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
741 747
  /// operations will return the corresponding values of \c M1.
742 748
  ///
743 749
  /// The \c Key and \c Value types are inherited from \c M1.
744 750
  /// The \c Key and \c Value of \c M2 must be convertible from those
745 751
  /// of \c M1.
746 752
  ///
747 753
  /// The simplest way of using this map is through the forkMap()
748 754
  /// function.
749 755
  template<typename  M1, typename M2>
750 756
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
751 757
    M1 &_m1;
752 758
    M2 &_m2;
753 759
  public:
754
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
755
    typedef typename Parent::Key Key;
756
    typedef typename Parent::Value Value;
760
    ///\e
761
    typedef typename M1::Key Key;
762
    ///\e
763
    typedef typename M1::Value Value;
757 764

	
758 765
    /// Constructor
759 766
    ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
760 767
    /// Returns the value associated with the given key in the first map.
761 768
    Value operator[](const Key &k) const { return _m1[k]; }
762 769
    /// Sets the value associated with the given key in both maps.
763 770
    void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
764 771
  };
765 772

	
766 773
  /// Returns a \c ForkMap class
767 774

	
768 775
  /// This function just returns a \c ForkMap class.
769 776
  /// \relates ForkMap
770 777
  template <typename M1, typename M2>
771 778
  inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
772 779
    return ForkMap<M1,M2>(m1,m2);
773 780
  }
774 781

	
775 782

	
776 783
  /// Sum of two maps
777 784

	
778 785
  /// This \ref concepts::ReadMap "read-only map" returns the sum
779 786
  /// of the values of the two given maps.
780 787
  /// Its \c Key and \c Value types are inherited from \c M1.
781 788
  /// The \c Key and \c Value of \c M2 must be convertible to those of
782 789
  /// \c M1.
783 790
  ///
784 791
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
785 792
  /// \code
786 793
  ///   AddMap<M1,M2> am(m1,m2);
787 794
  /// \endcode
788 795
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
789 796
  ///
790 797
  /// The simplest way of using this map is through the addMap()
791 798
  /// function.
792 799
  ///
793 800
  /// \sa SubMap, MulMap, DivMap
794 801
  /// \sa ShiftMap, ShiftWriteMap
795 802
  template<typename M1, typename M2>
796 803
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
797 804
    const M1 &_m1;
798 805
    const M2 &_m2;
799 806
  public:
800
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
801
    typedef typename Parent::Key Key;
802
    typedef typename Parent::Value Value;
807
    ///\e
808
    typedef typename M1::Key Key;
809
    ///\e
810
    typedef typename M1::Value Value;
803 811

	
804 812
    /// Constructor
805 813
    AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
806
    /// \e
814
    ///\e
807 815
    Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
808 816
  };
809 817

	
810 818
  /// Returns an \c AddMap class
811 819

	
812 820
  /// This function just returns an \c AddMap class.
813 821
  ///
814 822
  /// For example, if \c m1 and \c m2 are both maps with \c double
815 823
  /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
816 824
  /// <tt>m1[x]+m2[x]</tt>.
817 825
  ///
818 826
  /// \relates AddMap
819 827
  template<typename M1, typename M2>
820 828
  inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
821 829
    return AddMap<M1, M2>(m1,m2);
822 830
  }
823 831

	
824 832

	
825 833
  /// Difference of two maps
826 834

	
827 835
  /// This \ref concepts::ReadMap "read-only map" returns the difference
828 836
  /// of the values of the two given maps.
829 837
  /// Its \c Key and \c Value types are inherited from \c M1.
830 838
  /// The \c Key and \c Value of \c M2 must be convertible to those of
831 839
  /// \c M1.
832 840
  ///
833 841
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
834 842
  /// \code
835 843
  ///   SubMap<M1,M2> sm(m1,m2);
836 844
  /// \endcode
837 845
  /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
838 846
  ///
839 847
  /// The simplest way of using this map is through the subMap()
840 848
  /// function.
841 849
  ///
842 850
  /// \sa AddMap, MulMap, DivMap
843 851
  template<typename M1, typename M2>
844 852
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
845 853
    const M1 &_m1;
846 854
    const M2 &_m2;
847 855
  public:
848
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
849
    typedef typename Parent::Key Key;
850
    typedef typename Parent::Value Value;
856
    ///\e
857
    typedef typename M1::Key Key;
858
    ///\e
859
    typedef typename M1::Value Value;
851 860

	
852 861
    /// Constructor
853 862
    SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
854
    /// \e
863
    ///\e
855 864
    Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
856 865
  };
857 866

	
858 867
  /// Returns a \c SubMap class
859 868

	
860 869
  /// This function just returns a \c SubMap class.
861 870
  ///
862 871
  /// For example, if \c m1 and \c m2 are both maps with \c double
863 872
  /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
864 873
  /// <tt>m1[x]-m2[x]</tt>.
865 874
  ///
866 875
  /// \relates SubMap
867 876
  template<typename M1, typename M2>
868 877
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
869 878
    return SubMap<M1, M2>(m1,m2);
870 879
  }
871 880

	
872 881

	
873 882
  /// Product of two maps
874 883

	
875 884
  /// This \ref concepts::ReadMap "read-only map" returns the product
876 885
  /// of the values of the two given maps.
877 886
  /// Its \c Key and \c Value types are inherited from \c M1.
878 887
  /// The \c Key and \c Value of \c M2 must be convertible to those of
879 888
  /// \c M1.
880 889
  ///
881 890
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
882 891
  /// \code
883 892
  ///   MulMap<M1,M2> mm(m1,m2);
884 893
  /// \endcode
885 894
  /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
886 895
  ///
887 896
  /// The simplest way of using this map is through the mulMap()
888 897
  /// function.
889 898
  ///
890 899
  /// \sa AddMap, SubMap, DivMap
891 900
  /// \sa ScaleMap, ScaleWriteMap
892 901
  template<typename M1, typename M2>
893 902
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
894 903
    const M1 &_m1;
895 904
    const M2 &_m2;
896 905
  public:
897
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
898
    typedef typename Parent::Key Key;
899
    typedef typename Parent::Value Value;
906
    ///\e
907
    typedef typename M1::Key Key;
908
    ///\e
909
    typedef typename M1::Value Value;
900 910

	
901 911
    /// Constructor
902 912
    MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
903
    /// \e
913
    ///\e
904 914
    Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
905 915
  };
906 916

	
907 917
  /// Returns a \c MulMap class
908 918

	
909 919
  /// This function just returns a \c MulMap class.
910 920
  ///
911 921
  /// For example, if \c m1 and \c m2 are both maps with \c double
912 922
  /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
913 923
  /// <tt>m1[x]*m2[x]</tt>.
914 924
  ///
915 925
  /// \relates MulMap
916 926
  template<typename M1, typename M2>
917 927
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
918 928
    return MulMap<M1, M2>(m1,m2);
919 929
  }
920 930

	
921 931

	
922 932
  /// Quotient of two maps
923 933

	
924 934
  /// This \ref concepts::ReadMap "read-only map" returns the quotient
925 935
  /// of the values of the two given maps.
926 936
  /// Its \c Key and \c Value types are inherited from \c M1.
927 937
  /// The \c Key and \c Value of \c M2 must be convertible to those of
928 938
  /// \c M1.
929 939
  ///
930 940
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
931 941
  /// \code
932 942
  ///   DivMap<M1,M2> dm(m1,m2);
933 943
  /// \endcode
934 944
  /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
935 945
  ///
936 946
  /// The simplest way of using this map is through the divMap()
937 947
  /// function.
938 948
  ///
939 949
  /// \sa AddMap, SubMap, MulMap
940 950
  template<typename M1, typename M2>
941 951
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
942 952
    const M1 &_m1;
943 953
    const M2 &_m2;
944 954
  public:
945
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
946
    typedef typename Parent::Key Key;
947
    typedef typename Parent::Value Value;
955
    ///\e
956
    typedef typename M1::Key Key;
957
    ///\e
958
    typedef typename M1::Value Value;
948 959

	
949 960
    /// Constructor
950 961
    DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
951
    /// \e
962
    ///\e
952 963
    Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
953 964
  };
954 965

	
955 966
  /// Returns a \c DivMap class
956 967

	
957 968
  /// This function just returns a \c DivMap class.
958 969
  ///
959 970
  /// For example, if \c m1 and \c m2 are both maps with \c double
960 971
  /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
961 972
  /// <tt>m1[x]/m2[x]</tt>.
962 973
  ///
963 974
  /// \relates DivMap
964 975
  template<typename M1, typename M2>
965 976
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
966 977
    return DivMap<M1, M2>(m1,m2);
967 978
  }
968 979

	
969 980

	
970 981
  /// Shifts a map with a constant.
971 982

	
972 983
  /// This \ref concepts::ReadMap "read-only map" returns the sum of
973 984
  /// the given map and a constant value (i.e. it shifts the map with
974 985
  /// the constant). Its \c Key and \c Value are inherited from \c M.
975 986
  ///
976 987
  /// Actually,
977 988
  /// \code
978 989
  ///   ShiftMap<M> sh(m,v);
979 990
  /// \endcode
980 991
  /// is equivalent to
981 992
  /// \code
982 993
  ///   ConstMap<M::Key, M::Value> cm(v);
983 994
  ///   AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
984 995
  /// \endcode
985 996
  ///
986 997
  /// The simplest way of using this map is through the shiftMap()
987 998
  /// function.
988 999
  ///
989 1000
  /// \sa ShiftWriteMap
990 1001
  template<typename M, typename C = typename M::Value>
991 1002
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
992 1003
    const M &_m;
993 1004
    C _v;
994 1005
  public:
995
    typedef MapBase<typename M::Key, typename M::Value> Parent;
996
    typedef typename Parent::Key Key;
997
    typedef typename Parent::Value Value;
1006
    ///\e
1007
    typedef typename M::Key Key;
1008
    ///\e
1009
    typedef typename M::Value Value;
998 1010

	
999 1011
    /// Constructor
1000 1012

	
1001 1013
    /// Constructor.
1002 1014
    /// \param m The undelying map.
1003 1015
    /// \param v The constant value.
1004 1016
    ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
1005
    /// \e
1017
    ///\e
1006 1018
    Value operator[](const Key &k) const { return _m[k]+_v; }
1007 1019
  };
1008 1020

	
1009 1021
  /// Shifts a map with a constant (read-write version).
1010 1022

	
1011 1023
  /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
1012 1024
  /// of the given map and a constant value (i.e. it shifts the map with
1013 1025
  /// the constant). Its \c Key and \c Value are inherited from \c M.
1014 1026
  /// It makes also possible to write the map.
1015 1027
  ///
1016 1028
  /// The simplest way of using this map is through the shiftWriteMap()
1017 1029
  /// function.
1018 1030
  ///
1019 1031
  /// \sa ShiftMap
1020 1032
  template<typename M, typename C = typename M::Value>
1021 1033
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
1022 1034
    M &_m;
1023 1035
    C _v;
1024 1036
  public:
1025
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1026
    typedef typename Parent::Key Key;
1027
    typedef typename Parent::Value Value;
1037
    ///\e
1038
    typedef typename M::Key Key;
1039
    ///\e
1040
    typedef typename M::Value Value;
1028 1041

	
1029 1042
    /// Constructor
1030 1043

	
1031 1044
    /// Constructor.
1032 1045
    /// \param m The undelying map.
1033 1046
    /// \param v The constant value.
1034 1047
    ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1035
    /// \e
1048
    ///\e
1036 1049
    Value operator[](const Key &k) const { return _m[k]+_v; }
1037
    /// \e
1050
    ///\e
1038 1051
    void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
1039 1052
  };
1040 1053

	
1041 1054
  /// Returns a \c ShiftMap class
1042 1055

	
1043 1056
  /// This function just returns a \c ShiftMap class.
1044 1057
  ///
1045 1058
  /// For example, if \c m is a map with \c double values and \c v is
1046 1059
  /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
1047 1060
  /// <tt>m[x]+v</tt>.
1048 1061
  ///
1049 1062
  /// \relates ShiftMap
1050 1063
  template<typename M, typename C>
1051 1064
  inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
1052 1065
    return ShiftMap<M, C>(m,v);
1053 1066
  }
1054 1067

	
1055 1068
  /// Returns a \c ShiftWriteMap class
1056 1069

	
1057 1070
  /// This function just returns a \c ShiftWriteMap class.
1058 1071
  ///
1059 1072
  /// For example, if \c m is a map with \c double values and \c v is
1060 1073
  /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
1061 1074
  /// <tt>m[x]+v</tt>.
1062 1075
  /// Moreover it makes also possible to write the map.
1063 1076
  ///
1064 1077
  /// \relates ShiftWriteMap
1065 1078
  template<typename M, typename C>
1066 1079
  inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
1067 1080
    return ShiftWriteMap<M, C>(m,v);
1068 1081
  }
1069 1082

	
1070 1083

	
1071 1084
  /// Scales a map with a constant.
1072 1085

	
1073 1086
  /// This \ref concepts::ReadMap "read-only map" returns the value of
1074 1087
  /// the given map multiplied from the left side with a constant value.
1075 1088
  /// Its \c Key and \c Value are inherited from \c M.
1076 1089
  ///
1077 1090
  /// Actually,
1078 1091
  /// \code
1079 1092
  ///   ScaleMap<M> sc(m,v);
1080 1093
  /// \endcode
1081 1094
  /// is equivalent to
1082 1095
  /// \code
1083 1096
  ///   ConstMap<M::Key, M::Value> cm(v);
1084 1097
  ///   MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
1085 1098
  /// \endcode
1086 1099
  ///
1087 1100
  /// The simplest way of using this map is through the scaleMap()
1088 1101
  /// function.
1089 1102
  ///
1090 1103
  /// \sa ScaleWriteMap
1091 1104
  template<typename M, typename C = typename M::Value>
1092 1105
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
1093 1106
    const M &_m;
1094 1107
    C _v;
1095 1108
  public:
1096
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1097
    typedef typename Parent::Key Key;
1098
    typedef typename Parent::Value Value;
1109
    ///\e
1110
    typedef typename M::Key Key;
1111
    ///\e
1112
    typedef typename M::Value Value;
1099 1113

	
1100 1114
    /// Constructor
1101 1115

	
1102 1116
    /// Constructor.
1103 1117
    /// \param m The undelying map.
1104 1118
    /// \param v The constant value.
1105 1119
    ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
1106
    /// \e
1120
    ///\e
1107 1121
    Value operator[](const Key &k) const { return _v*_m[k]; }
1108 1122
  };
1109 1123

	
1110 1124
  /// Scales a map with a constant (read-write version).
1111 1125

	
1112 1126
  /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
1113 1127
  /// the given map multiplied from the left side with a constant value.
1114 1128
  /// Its \c Key and \c Value are inherited from \c M.
1115 1129
  /// It can also be used as write map if the \c / operator is defined
1116 1130
  /// between \c Value and \c C and the given multiplier is not zero.
1117 1131
  ///
1118 1132
  /// The simplest way of using this map is through the scaleWriteMap()
1119 1133
  /// function.
1120 1134
  ///
1121 1135
  /// \sa ScaleMap
1122 1136
  template<typename M, typename C = typename M::Value>
1123 1137
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
1124 1138
    M &_m;
1125 1139
    C _v;
1126 1140
  public:
1127
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1128
    typedef typename Parent::Key Key;
1129
    typedef typename Parent::Value Value;
1141
    ///\e
1142
    typedef typename M::Key Key;
1143
    ///\e
1144
    typedef typename M::Value Value;
1130 1145

	
1131 1146
    /// Constructor
1132 1147

	
1133 1148
    /// Constructor.
1134 1149
    /// \param m The undelying map.
1135 1150
    /// \param v The constant value.
1136 1151
    ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1137
    /// \e
1152
    ///\e
1138 1153
    Value operator[](const Key &k) const { return _v*_m[k]; }
1139
    /// \e
1154
    ///\e
1140 1155
    void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
1141 1156
  };
1142 1157

	
1143 1158
  /// Returns a \c ScaleMap class
1144 1159

	
1145 1160
  /// This function just returns a \c ScaleMap class.
1146 1161
  ///
1147 1162
  /// For example, if \c m is a map with \c double values and \c v is
1148 1163
  /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
1149 1164
  /// <tt>v*m[x]</tt>.
1150 1165
  ///
1151 1166
  /// \relates ScaleMap
1152 1167
  template<typename M, typename C>
1153 1168
  inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
1154 1169
    return ScaleMap<M, C>(m,v);
1155 1170
  }
1156 1171

	
1157 1172
  /// Returns a \c ScaleWriteMap class
1158 1173

	
1159 1174
  /// This function just returns a \c ScaleWriteMap class.
1160 1175
  ///
1161 1176
  /// For example, if \c m is a map with \c double values and \c v is
1162 1177
  /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
1163 1178
  /// <tt>v*m[x]</tt>.
1164 1179
  /// Moreover it makes also possible to write the map.
1165 1180
  ///
1166 1181
  /// \relates ScaleWriteMap
1167 1182
  template<typename M, typename C>
1168 1183
  inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
1169 1184
    return ScaleWriteMap<M, C>(m,v);
1170 1185
  }
1171 1186

	
1172 1187

	
1173 1188
  /// Negative of a map
1174 1189

	
1175 1190
  /// This \ref concepts::ReadMap "read-only map" returns the negative
1176 1191
  /// of the values of the given map (using the unary \c - operator).
1177 1192
  /// Its \c Key and \c Value are inherited from \c M.
1178 1193
  ///
1179 1194
  /// If M::Value is \c int, \c double etc., then
1180 1195
  /// \code
1181 1196
  ///   NegMap<M> neg(m);
1182 1197
  /// \endcode
1183 1198
  /// is equivalent to
1184 1199
  /// \code
1185 1200
  ///   ScaleMap<M> neg(m,-1);
1186 1201
  /// \endcode
1187 1202
  ///
1188 1203
  /// The simplest way of using this map is through the negMap()
1189 1204
  /// function.
1190 1205
  ///
1191 1206
  /// \sa NegWriteMap
1192 1207
  template<typename M>
1193 1208
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
1194 1209
    const M& _m;
1195 1210
  public:
1196
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1197
    typedef typename Parent::Key Key;
1198
    typedef typename Parent::Value Value;
1211
    ///\e
1212
    typedef typename M::Key Key;
1213
    ///\e
1214
    typedef typename M::Value Value;
1199 1215

	
1200 1216
    /// Constructor
1201 1217
    NegMap(const M &m) : _m(m) {}
1202
    /// \e
1218
    ///\e
1203 1219
    Value operator[](const Key &k) const { return -_m[k]; }
1204 1220
  };
1205 1221

	
1206 1222
  /// Negative of a map (read-write version)
1207 1223

	
1208 1224
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
1209 1225
  /// negative of the values of the given map (using the unary \c -
1210 1226
  /// operator).
1211 1227
  /// Its \c Key and \c Value are inherited from \c M.
1212 1228
  /// It makes also possible to write the map.
1213 1229
  ///
1214 1230
  /// If M::Value is \c int, \c double etc., then
1215 1231
  /// \code
1216 1232
  ///   NegWriteMap<M> neg(m);
1217 1233
  /// \endcode
1218 1234
  /// is equivalent to
1219 1235
  /// \code
1220 1236
  ///   ScaleWriteMap<M> neg(m,-1);
1221 1237
  /// \endcode
1222 1238
  ///
1223 1239
  /// The simplest way of using this map is through the negWriteMap()
1224 1240
  /// function.
1225 1241
  ///
1226 1242
  /// \sa NegMap
1227 1243
  template<typename M>
1228 1244
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
1229 1245
    M &_m;
1230 1246
  public:
1231
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1232
    typedef typename Parent::Key Key;
1233
    typedef typename Parent::Value Value;
1247
    ///\e
1248
    typedef typename M::Key Key;
1249
    ///\e
1250
    typedef typename M::Value Value;
1234 1251

	
1235 1252
    /// Constructor
1236 1253
    NegWriteMap(M &m) : _m(m) {}
1237
    /// \e
1254
    ///\e
1238 1255
    Value operator[](const Key &k) const { return -_m[k]; }
1239
    /// \e
1256
    ///\e
1240 1257
    void set(const Key &k, const Value &v) { _m.set(k, -v); }
1241 1258
  };
1242 1259

	
1243 1260
  /// Returns a \c NegMap class
1244 1261

	
1245 1262
  /// This function just returns a \c NegMap class.
1246 1263
  ///
1247 1264
  /// For example, if \c m is a map with \c double values, then
1248 1265
  /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1249 1266
  ///
1250 1267
  /// \relates NegMap
1251 1268
  template <typename M>
1252 1269
  inline NegMap<M> negMap(const M &m) {
1253 1270
    return NegMap<M>(m);
1254 1271
  }
1255 1272

	
1256 1273
  /// Returns a \c NegWriteMap class
1257 1274

	
1258 1275
  /// This function just returns a \c NegWriteMap class.
1259 1276
  ///
1260 1277
  /// For example, if \c m is a map with \c double values, then
1261 1278
  /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1262 1279
  /// Moreover it makes also possible to write the map.
1263 1280
  ///
1264 1281
  /// \relates NegWriteMap
1265 1282
  template <typename M>
1266 1283
  inline NegWriteMap<M> negWriteMap(M &m) {
1267 1284
    return NegWriteMap<M>(m);
1268 1285
  }
1269 1286

	
1270 1287

	
1271 1288
  /// Absolute value of a map
1272 1289

	
1273 1290
  /// This \ref concepts::ReadMap "read-only map" returns the absolute
1274 1291
  /// value of the values of the given map.
1275 1292
  /// Its \c Key and \c Value are inherited from \c M.
1276 1293
  /// \c Value must be comparable to \c 0 and the unary \c -
1277 1294
  /// operator must be defined for it, of course.
1278 1295
  ///
1279 1296
  /// The simplest way of using this map is through the absMap()
1280 1297
  /// function.
1281 1298
  template<typename M>
1282 1299
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
1283 1300
    const M &_m;
1284 1301
  public:
1285
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1286
    typedef typename Parent::Key Key;
1287
    typedef typename Parent::Value Value;
1302
    ///\e
1303
    typedef typename M::Key Key;
1304
    ///\e
1305
    typedef typename M::Value Value;
1288 1306

	
1289 1307
    /// Constructor
1290 1308
    AbsMap(const M &m) : _m(m) {}
1291
    /// \e
1309
    ///\e
1292 1310
    Value operator[](const Key &k) const {
1293 1311
      Value tmp = _m[k];
1294 1312
      return tmp >= 0 ? tmp : -tmp;
1295 1313
    }
1296 1314

	
1297 1315
  };
1298 1316

	
1299 1317
  /// Returns an \c AbsMap class
1300 1318

	
1301 1319
  /// This function just returns an \c AbsMap class.
1302 1320
  ///
1303 1321
  /// For example, if \c m is a map with \c double values, then
1304 1322
  /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
1305 1323
  /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
1306 1324
  /// negative.
1307 1325
  ///
1308 1326
  /// \relates AbsMap
1309 1327
  template<typename M>
1310 1328
  inline AbsMap<M> absMap(const M &m) {
1311 1329
    return AbsMap<M>(m);
1312 1330
  }
1313 1331

	
1314 1332
  /// @}
1315 1333

	
1316 1334
  // Logical maps and map adaptors:
1317 1335

	
1318 1336
  /// \addtogroup maps
1319 1337
  /// @{
1320 1338

	
1321 1339
  /// Constant \c true map.
1322 1340

	
1323 1341
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1324 1342
  /// each key.
1325 1343
  ///
1326 1344
  /// Note that
1327 1345
  /// \code
1328 1346
  ///   TrueMap<K> tm;
1329 1347
  /// \endcode
1330 1348
  /// is equivalent to
1331 1349
  /// \code
1332 1350
  ///   ConstMap<K,bool> tm(true);
1333 1351
  /// \endcode
1334 1352
  ///
1335 1353
  /// \sa FalseMap
1336 1354
  /// \sa ConstMap
1337 1355
  template <typename K>
1338 1356
  class TrueMap : public MapBase<K, bool> {
1339 1357
  public:
1340
    typedef MapBase<K, bool> Parent;
1341
    typedef typename Parent::Key Key;
1342
    typedef typename Parent::Value Value;
1358
    ///\e
1359
    typedef K Key;
1360
    ///\e
1361
    typedef bool Value;
1343 1362

	
1344 1363
    /// Gives back \c true.
1345 1364
    Value operator[](const Key&) const { return true; }
1346 1365
  };
1347 1366

	
1348 1367
  /// Returns a \c TrueMap class
1349 1368

	
1350 1369
  /// This function just returns a \c TrueMap class.
1351 1370
  /// \relates TrueMap
1352 1371
  template<typename K>
1353 1372
  inline TrueMap<K> trueMap() {
1354 1373
    return TrueMap<K>();
1355 1374
  }
1356 1375

	
1357 1376

	
1358 1377
  /// Constant \c false map.
1359 1378

	
1360 1379
  /// This \ref concepts::ReadMap "read-only map" assigns \c false to
1361 1380
  /// each key.
1362 1381
  ///
1363 1382
  /// Note that
1364 1383
  /// \code
1365 1384
  ///   FalseMap<K> fm;
1366 1385
  /// \endcode
1367 1386
  /// is equivalent to
1368 1387
  /// \code
1369 1388
  ///   ConstMap<K,bool> fm(false);
1370 1389
  /// \endcode
1371 1390
  ///
1372 1391
  /// \sa TrueMap
1373 1392
  /// \sa ConstMap
1374 1393
  template <typename K>
1375 1394
  class FalseMap : public MapBase<K, bool> {
1376 1395
  public:
1377
    typedef MapBase<K, bool> Parent;
1378
    typedef typename Parent::Key Key;
1379
    typedef typename Parent::Value Value;
1396
    ///\e
1397
    typedef K Key;
1398
    ///\e
1399
    typedef bool Value;
1380 1400

	
1381 1401
    /// Gives back \c false.
1382 1402
    Value operator[](const Key&) const { return false; }
1383 1403
  };
1384 1404

	
1385 1405
  /// Returns a \c FalseMap class
1386 1406

	
1387 1407
  /// This function just returns a \c FalseMap class.
1388 1408
  /// \relates FalseMap
1389 1409
  template<typename K>
1390 1410
  inline FalseMap<K> falseMap() {
1391 1411
    return FalseMap<K>();
1392 1412
  }
1393 1413

	
1394 1414
  /// @}
1395 1415

	
1396 1416
  /// \addtogroup map_adaptors
1397 1417
  /// @{
1398 1418

	
1399 1419
  /// Logical 'and' of two maps
1400 1420

	
1401 1421
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1402 1422
  /// 'and' of the values of the two given maps.
1403 1423
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1404 1424
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1405 1425
  ///
1406 1426
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1407 1427
  /// \code
1408 1428
  ///   AndMap<M1,M2> am(m1,m2);
1409 1429
  /// \endcode
1410 1430
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
1411 1431
  ///
1412 1432
  /// The simplest way of using this map is through the andMap()
1413 1433
  /// function.
1414 1434
  ///
1415 1435
  /// \sa OrMap
1416 1436
  /// \sa NotMap, NotWriteMap
1417 1437
  template<typename M1, typename M2>
1418 1438
  class AndMap : public MapBase<typename M1::Key, bool> {
1419 1439
    const M1 &_m1;
1420 1440
    const M2 &_m2;
1421 1441
  public:
1422
    typedef MapBase<typename M1::Key, bool> Parent;
1423
    typedef typename Parent::Key Key;
1424
    typedef typename Parent::Value Value;
1442
    ///\e
1443
    typedef typename M1::Key Key;
1444
    ///\e
1445
    typedef bool Value;
1425 1446

	
1426 1447
    /// Constructor
1427 1448
    AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1428
    /// \e
1449
    ///\e
1429 1450
    Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
1430 1451
  };
1431 1452

	
1432 1453
  /// Returns an \c AndMap class
1433 1454

	
1434 1455
  /// This function just returns an \c AndMap class.
1435 1456
  ///
1436 1457
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1437 1458
  /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
1438 1459
  /// <tt>m1[x]&&m2[x]</tt>.
1439 1460
  ///
1440 1461
  /// \relates AndMap
1441 1462
  template<typename M1, typename M2>
1442 1463
  inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
1443 1464
    return AndMap<M1, M2>(m1,m2);
1444 1465
  }
1445 1466

	
1446 1467

	
1447 1468
  /// Logical 'or' of two maps
1448 1469

	
1449 1470
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1450 1471
  /// 'or' of the values of the two given maps.
1451 1472
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1452 1473
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1453 1474
  ///
1454 1475
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1455 1476
  /// \code
1456 1477
  ///   OrMap<M1,M2> om(m1,m2);
1457 1478
  /// \endcode
1458 1479
  /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
1459 1480
  ///
1460 1481
  /// The simplest way of using this map is through the orMap()
1461 1482
  /// function.
1462 1483
  ///
1463 1484
  /// \sa AndMap
1464 1485
  /// \sa NotMap, NotWriteMap
1465 1486
  template<typename M1, typename M2>
1466 1487
  class OrMap : public MapBase<typename M1::Key, bool> {
1467 1488
    const M1 &_m1;
1468 1489
    const M2 &_m2;
1469 1490
  public:
1470
    typedef MapBase<typename M1::Key, bool> Parent;
1471
    typedef typename Parent::Key Key;
1472
    typedef typename Parent::Value Value;
1491
    ///\e
1492
    typedef typename M1::Key Key;
1493
    ///\e
1494
    typedef bool Value;
1473 1495

	
1474 1496
    /// Constructor
1475 1497
    OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1476
    /// \e
1498
    ///\e
1477 1499
    Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1478 1500
  };
1479 1501

	
1480 1502
  /// Returns an \c OrMap class
1481 1503

	
1482 1504
  /// This function just returns an \c OrMap class.
1483 1505
  ///
1484 1506
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1485 1507
  /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
1486 1508
  /// <tt>m1[x]||m2[x]</tt>.
1487 1509
  ///
1488 1510
  /// \relates OrMap
1489 1511
  template<typename M1, typename M2>
1490 1512
  inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
1491 1513
    return OrMap<M1, M2>(m1,m2);
1492 1514
  }
1493 1515

	
1494 1516

	
1495 1517
  /// Logical 'not' of a map
1496 1518

	
1497 1519
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1498 1520
  /// negation of the values of the given map.
1499 1521
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1500 1522
  ///
1501 1523
  /// The simplest way of using this map is through the notMap()
1502 1524
  /// function.
1503 1525
  ///
1504 1526
  /// \sa NotWriteMap
1505 1527
  template <typename M>
1506 1528
  class NotMap : public MapBase<typename M::Key, bool> {
1507 1529
    const M &_m;
1508 1530
  public:
1509
    typedef MapBase<typename M::Key, bool> Parent;
1510
    typedef typename Parent::Key Key;
1511
    typedef typename Parent::Value Value;
1531
    ///\e
1532
    typedef typename M::Key Key;
1533
    ///\e
1534
    typedef bool Value;
1512 1535

	
1513 1536
    /// Constructor
1514 1537
    NotMap(const M &m) : _m(m) {}
1515
    /// \e
1538
    ///\e
1516 1539
    Value operator[](const Key &k) const { return !_m[k]; }
1517 1540
  };
1518 1541

	
1519 1542
  /// Logical 'not' of a map (read-write version)
1520 1543

	
1521 1544
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
1522 1545
  /// logical negation of the values of the given map.
1523 1546
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1524 1547
  /// It makes also possible to write the map. When a value is set,
1525 1548
  /// the opposite value is set to the original map.
1526 1549
  ///
1527 1550
  /// The simplest way of using this map is through the notWriteMap()
1528 1551
  /// function.
1529 1552
  ///
1530 1553
  /// \sa NotMap
1531 1554
  template <typename M>
1532 1555
  class NotWriteMap : public MapBase<typename M::Key, bool> {
1533 1556
    M &_m;
1534 1557
  public:
1535
    typedef MapBase<typename M::Key, bool> Parent;
1536
    typedef typename Parent::Key Key;
1537
    typedef typename Parent::Value Value;
1558
    ///\e
1559
    typedef typename M::Key Key;
1560
    ///\e
1561
    typedef bool Value;
1538 1562

	
1539 1563
    /// Constructor
1540 1564
    NotWriteMap(M &m) : _m(m) {}
1541
    /// \e
1565
    ///\e
1542 1566
    Value operator[](const Key &k) const { return !_m[k]; }
1543
    /// \e
1567
    ///\e
1544 1568
    void set(const Key &k, bool v) { _m.set(k, !v); }
1545 1569
  };
1546 1570

	
1547 1571
  /// Returns a \c NotMap class
1548 1572

	
1549 1573
  /// This function just returns a \c NotMap class.
1550 1574
  ///
1551 1575
  /// For example, if \c m is a map with \c bool values, then
1552 1576
  /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1553 1577
  ///
1554 1578
  /// \relates NotMap
1555 1579
  template <typename M>
1556 1580
  inline NotMap<M> notMap(const M &m) {
1557 1581
    return NotMap<M>(m);
1558 1582
  }
1559 1583

	
1560 1584
  /// Returns a \c NotWriteMap class
1561 1585

	
1562 1586
  /// This function just returns a \c NotWriteMap class.
1563 1587
  ///
1564 1588
  /// For example, if \c m is a map with \c bool values, then
1565 1589
  /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1566 1590
  /// Moreover it makes also possible to write the map.
1567 1591
  ///
1568 1592
  /// \relates NotWriteMap
1569 1593
  template <typename M>
1570 1594
  inline NotWriteMap<M> notWriteMap(M &m) {
1571 1595
    return NotWriteMap<M>(m);
1572 1596
  }
1573 1597

	
1574 1598

	
1575 1599
  /// Combination of two maps using the \c == operator
1576 1600

	
1577 1601
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1578 1602
  /// the keys for which the corresponding values of the two maps are
1579 1603
  /// equal.
1580 1604
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1581 1605
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1582 1606
  ///
1583 1607
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1584 1608
  /// \code
1585 1609
  ///   EqualMap<M1,M2> em(m1,m2);
1586 1610
  /// \endcode
1587 1611
  /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1588 1612
  ///
1589 1613
  /// The simplest way of using this map is through the equalMap()
1590 1614
  /// function.
1591 1615
  ///
1592 1616
  /// \sa LessMap
1593 1617
  template<typename M1, typename M2>
1594 1618
  class EqualMap : public MapBase<typename M1::Key, bool> {
1595 1619
    const M1 &_m1;
1596 1620
    const M2 &_m2;
1597 1621
  public:
1598
    typedef MapBase<typename M1::Key, bool> Parent;
1599
    typedef typename Parent::Key Key;
1600
    typedef typename Parent::Value Value;
1622
    ///\e
1623
    typedef typename M1::Key Key;
1624
    ///\e
1625
    typedef bool Value;
1601 1626

	
1602 1627
    /// Constructor
1603 1628
    EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1604
    /// \e
1629
    ///\e
1605 1630
    Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1606 1631
  };
1607 1632

	
1608 1633
  /// Returns an \c EqualMap class
1609 1634

	
1610 1635
  /// This function just returns an \c EqualMap class.
1611 1636
  ///
1612 1637
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1613 1638
  /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1614 1639
  /// <tt>m1[x]==m2[x]</tt>.
1615 1640
  ///
1616 1641
  /// \relates EqualMap
1617 1642
  template<typename M1, typename M2>
1618 1643
  inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1619 1644
    return EqualMap<M1, M2>(m1,m2);
1620 1645
  }
1621 1646

	
1622 1647

	
1623 1648
  /// Combination of two maps using the \c < operator
1624 1649

	
1625 1650
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1626 1651
  /// the keys for which the corresponding value of the first map is
1627 1652
  /// less then the value of the second map.
1628 1653
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1629 1654
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1630 1655
  ///
1631 1656
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1632 1657
  /// \code
1633 1658
  ///   LessMap<M1,M2> lm(m1,m2);
1634 1659
  /// \endcode
1635 1660
  /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1636 1661
  ///
1637 1662
  /// The simplest way of using this map is through the lessMap()
1638 1663
  /// function.
1639 1664
  ///
1640 1665
  /// \sa EqualMap
1641 1666
  template<typename M1, typename M2>
1642 1667
  class LessMap : public MapBase<typename M1::Key, bool> {
1643 1668
    const M1 &_m1;
1644 1669
    const M2 &_m2;
1645 1670
  public:
1646
    typedef MapBase<typename M1::Key, bool> Parent;
1647
    typedef typename Parent::Key Key;
1648
    typedef typename Parent::Value Value;
1671
    ///\e
1672
    typedef typename M1::Key Key;
1673
    ///\e
1674
    typedef bool Value;
1649 1675

	
1650 1676
    /// Constructor
1651 1677
    LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1652
    /// \e
1678
    ///\e
1653 1679
    Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1654 1680
  };
1655 1681

	
1656 1682
  /// Returns an \c LessMap class
1657 1683

	
1658 1684
  /// This function just returns an \c LessMap class.
1659 1685
  ///
1660 1686
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1661 1687
  /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1662 1688
  /// <tt>m1[x]<m2[x]</tt>.
1663 1689
  ///
1664 1690
  /// \relates LessMap
1665 1691
  template<typename M1, typename M2>
1666 1692
  inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1667 1693
    return LessMap<M1, M2>(m1,m2);
1668 1694
  }
1669 1695

	
1670 1696
  namespace _maps_bits {
1671 1697

	
1672 1698
    template <typename _Iterator, typename Enable = void>
1673 1699
    struct IteratorTraits {
1674 1700
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
1675 1701
    };
1676 1702

	
1677 1703
    template <typename _Iterator>
1678 1704
    struct IteratorTraits<_Iterator,
1679 1705
      typename exists<typename _Iterator::container_type>::type>
1680 1706
    {
1681 1707
      typedef typename _Iterator::container_type::value_type Value;
1682 1708
    };
1683 1709

	
1684 1710
  }
1685 1711

	
1686 1712
  /// @}
1687 1713

	
1688 1714
  /// \addtogroup maps
1689 1715
  /// @{
1690 1716

	
1691 1717
  /// \brief Writable bool map for logging each \c true assigned element
1692 1718
  ///
1693 1719
  /// A \ref concepts::WriteMap "writable" bool map for logging
1694 1720
  /// each \c true assigned element, i.e it copies subsequently each
1695 1721
  /// keys set to \c true to the given iterator.
1696 1722
  /// The most important usage of it is storing certain nodes or arcs
1697 1723
  /// that were marked \c true by an algorithm.
1698 1724
  ///
1699 1725
  /// There are several algorithms that provide solutions through bool
1700 1726
  /// maps and most of them assign \c true at most once for each key.
1701 1727
  /// In these cases it is a natural request to store each \c true
1702 1728
  /// assigned elements (in order of the assignment), which can be
1703 1729
  /// easily done with LoggerBoolMap.
1704 1730
  ///
1705 1731
  /// The simplest way of using this map is through the loggerBoolMap()
1706 1732
  /// function.
1707 1733
  ///
1708
  /// \tparam It The type of the iterator.
1709
  /// \tparam Ke The key type of the map. The default value set
1734
  /// \tparam IT The type of the iterator.
1735
  /// \tparam KEY The key type of the map. The default value set
1710 1736
  /// according to the iterator type should work in most cases.
1711 1737
  ///
1712 1738
  /// \note The container of the iterator must contain enough space
1713 1739
  /// for the elements or the iterator should be an inserter iterator.
1714 1740
#ifdef DOXYGEN
1715
  template <typename It, typename Ke>
1741
  template <typename IT, typename KEY>
1716 1742
#else
1717
  template <typename It,
1718
            typename Ke=typename _maps_bits::IteratorTraits<It>::Value>
1743
  template <typename IT,
1744
            typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
1719 1745
#endif
1720
  class LoggerBoolMap {
1746
  class LoggerBoolMap : public MapBase<KEY, bool> {
1721 1747
  public:
1722
    typedef It Iterator;
1723

	
1724
    typedef Ke Key;
1748

	
1749
    ///\e
1750
    typedef KEY Key;
1751
    ///\e
1725 1752
    typedef bool Value;
1753
    ///\e
1754
    typedef IT Iterator;
1726 1755

	
1727 1756
    /// Constructor
1728 1757
    LoggerBoolMap(Iterator it)
1729 1758
      : _begin(it), _end(it) {}
1730 1759

	
1731 1760
    /// Gives back the given iterator set for the first key
1732 1761
    Iterator begin() const {
1733 1762
      return _begin;
1734 1763
    }
1735 1764

	
1736 1765
    /// Gives back the the 'after the last' iterator
1737 1766
    Iterator end() const {
1738 1767
      return _end;
1739 1768
    }
1740 1769

	
1741 1770
    /// The set function of the map
1742 1771
    void set(const Key& key, Value value) {
1743 1772
      if (value) {
1744 1773
        *_end++ = key;
1745 1774
      }
1746 1775
    }
1747 1776

	
1748 1777
  private:
1749 1778
    Iterator _begin;
1750 1779
    Iterator _end;
1751 1780
  };
1752 1781

	
1753 1782
  /// Returns a \c LoggerBoolMap class
1754 1783

	
1755 1784
  /// This function just returns a \c LoggerBoolMap class.
1756 1785
  ///
1757 1786
  /// The most important usage of it is storing certain nodes or arcs
1758 1787
  /// that were marked \c true by an algorithm.
1759 1788
  /// For example it makes easier to store the nodes in the processing
1760 1789
  /// order of Dfs algorithm, as the following examples show.
1761 1790
  /// \code
1762 1791
  ///   std::vector<Node> v;
1763
  ///   dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1792
  ///   dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
1764 1793
  /// \endcode
1765 1794
  /// \code
1766 1795
  ///   std::vector<Node> v(countNodes(g));
1767
  ///   dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1796
  ///   dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
1768 1797
  /// \endcode
1769 1798
  ///
1770 1799
  /// \note The container of the iterator must contain enough space
1771 1800
  /// for the elements or the iterator should be an inserter iterator.
1772 1801
  ///
1773 1802
  /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1774 1803
  /// it cannot be used when a readable map is needed, for example as
1775 1804
  /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1776 1805
  ///
1777 1806
  /// \relates LoggerBoolMap
1778 1807
  template<typename Iterator>
1779 1808
  inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
1780 1809
    return LoggerBoolMap<Iterator>(it);
1781 1810
  }
1782 1811

	
1783 1812
  /// @}
1784 1813

	
1785 1814
  /// \addtogroup graph_maps
1786 1815
  /// @{
1787 1816

	
1788
  /// Provides an immutable and unique id for each item in the graph.
1789

	
1790
  /// The IdMap class provides a unique and immutable id for each item of the
1791
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
1792
  /// different items (nodes) get different ids <li>\b immutable: the id of an
1793
  /// item (node) does not change (even if you delete other nodes).  </ul>
1794
  /// Through this map you get access (i.e. can read) the inner id values of
1795
  /// the items stored in the graph. This map can be inverted with its member
1796
  /// class \c InverseMap or with the \c operator() member.
1817
  /// \brief Provides an immutable and unique id for each item in a graph.
1797 1818
  ///
1798
  template <typename _Graph, typename _Item>
1799
  class IdMap {
1819
  /// IdMap provides a unique and immutable id for each item of the
1820
  /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1821
  ///  - \b unique: different items get different ids,
1822
  ///  - \b immutable: the id of an item does not change (even if you
1823
  ///    delete other nodes).
1824
  ///
1825
  /// Using this map you get access (i.e. can read) the inner id values of
1826
  /// the items stored in the graph, which is returned by the \c id()
1827
  /// function of the graph. This map can be inverted with its member
1828
  /// class \c InverseMap or with the \c operator()() member.
1829
  ///
1830
  /// \tparam GR The graph type.
1831
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1832
  /// \c GR::Edge).
1833
  ///
1834
  /// \see RangeIdMap
1835
  template <typename GR, typename K>
1836
  class IdMap : public MapBase<K, int> {
1800 1837
  public:
1801
    typedef _Graph Graph;
1838
    /// The graph type of IdMap.
1839
    typedef GR Graph;
1840
    typedef GR Digraph;
1841
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1842
    typedef K Item;
1843
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1844
    typedef K Key;
1845
    /// The value type of IdMap.
1802 1846
    typedef int Value;
1803
    typedef _Item Item;
1804
    typedef _Item Key;
1805 1847

	
1806 1848
    /// \brief Constructor.
1807 1849
    ///
1808 1850
    /// Constructor of the map.
1809 1851
    explicit IdMap(const Graph& graph) : _graph(&graph) {}
1810 1852

	
1811 1853
    /// \brief Gives back the \e id of the item.
1812 1854
    ///
1813 1855
    /// Gives back the immutable and unique \e id of the item.
1814 1856
    int operator[](const Item& item) const { return _graph->id(item);}
1815 1857

	
1816
    /// \brief Gives back the item by its id.
1858
    /// \brief Gives back the \e item by its id.
1817 1859
    ///
1818
    /// Gives back the item by its id.
1860
    /// Gives back the \e item by its id.
1819 1861
    Item operator()(int id) { return _graph->fromId(id, Item()); }
1820 1862

	
1821 1863
  private:
1822 1864
    const Graph* _graph;
1823 1865

	
1824 1866
  public:
1825 1867

	
1826
    /// \brief The class represents the inverse of its owner (IdMap).
1868
    /// \brief The inverse map type of IdMap.
1827 1869
    ///
1828
    /// The class represents the inverse of its owner (IdMap).
1870
    /// The inverse map type of IdMap. The subscript operator gives back
1871
    /// an item by its id.
1872
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1829 1873
    /// \see inverse()
1830 1874
    class InverseMap {
1831 1875
    public:
1832 1876

	
1833 1877
      /// \brief Constructor.
1834 1878
      ///
1835 1879
      /// Constructor for creating an id-to-item map.
1836 1880
      explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1837 1881

	
1838 1882
      /// \brief Constructor.
1839 1883
      ///
1840 1884
      /// Constructor for creating an id-to-item map.
1841 1885
      explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1842 1886

	
1843
      /// \brief Gives back the given item from its id.
1887
      /// \brief Gives back an item by its id.
1844 1888
      ///
1845
      /// Gives back the given item from its id.
1846
      ///
1889
      /// Gives back an item by its id.
1847 1890
      Item operator[](int id) const { return _graph->fromId(id, Item());}
1848 1891

	
1849 1892
    private:
1850 1893
      const Graph* _graph;
1851 1894
    };
1852 1895

	
1853 1896
    /// \brief Gives back the inverse of the map.
1854 1897
    ///
1855 1898
    /// Gives back the inverse of the IdMap.
1856 1899
    InverseMap inverse() const { return InverseMap(*_graph);}
1857

	
1858 1900
  };
1859 1901

	
1860

	
1861
  /// \brief General invertable graph-map type.
1862

	
1863
  /// This type provides simple invertable graph-maps.
1864
  /// The InvertableMap wraps an arbitrary ReadWriteMap
1865
  /// and if a key is set to a new value then store it
1866
  /// in the inverse map.
1902
  /// \brief Returns an \c IdMap class.
1867 1903
  ///
1868
  /// The values of the map can be accessed
1869
  /// with stl compatible forward iterator.
1904
  /// This function just returns an \c IdMap class.
1905
  /// \relates IdMap
1906
  template <typename K, typename GR>
1907
  inline IdMap<GR, K> idMap(const GR& graph) {
1908
    return IdMap<GR, K>(graph);
1909
  }
1910

	
1911
  /// \brief General cross reference graph map type.
1912

	
1913
  /// This class provides simple invertable graph maps.
1914
  /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1915
  /// and if a key is set to a new value, then stores it in the inverse map.
1916
  /// The graph items can be accessed by their values either using
1917
  /// \c InverseMap or \c operator()(), and the values of the map can be
1918
  /// accessed with an STL compatible forward iterator (\c ValueIt).
1919
  /// 
1920
  /// This map is intended to be used when all associated values are
1921
  /// different (the map is actually invertable) or there are only a few
1922
  /// items with the same value.
1923
  /// Otherwise consider to use \c IterableValueMap, which is more 
1924
  /// suitable and more efficient for such cases. It provides iterators
1925
  /// to traverse the items with the same associated value, however
1926
  /// it does not have \c InverseMap.
1870 1927
  ///
1871
  /// \tparam _Graph The graph type.
1872
  /// \tparam _Item The item type of the graph.
1873
  /// \tparam _Value The value type of the map.
1928
  /// This type is not reference map, so it cannot be modified with
1929
  /// the subscript operator.
1930
  ///
1931
  /// \tparam GR The graph type.
1932
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1933
  /// \c GR::Edge).
1934
  /// \tparam V The value type of the map.
1874 1935
  ///
1875 1936
  /// \see IterableValueMap
1876
  template <typename _Graph, typename _Item, typename _Value>
1877
  class InvertableMap
1878
    : protected ItemSetTraits<_Graph, _Item>::template Map<_Value>::Type {
1937
  template <typename GR, typename K, typename V>
1938
  class CrossRefMap
1939
    : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1879 1940
  private:
1880 1941

	
1881
    typedef typename ItemSetTraits<_Graph, _Item>::
1882
    template Map<_Value>::Type Map;
1883
    typedef _Graph Graph;
1884

	
1885
    typedef std::map<_Value, _Item> Container;
1942
    typedef typename ItemSetTraits<GR, K>::
1943
      template Map<V>::Type Map;
1944

	
1945
    typedef std::multimap<V, K> Container;
1886 1946
    Container _inv_map;
1887 1947

	
1888 1948
  public:
1889 1949

	
1890
    /// The key type of InvertableMap (Node, Arc, Edge).
1891
    typedef typename Map::Key Key;
1892
    /// The value type of the InvertableMap.
1893
    typedef typename Map::Value Value;
1950
    /// The graph type of CrossRefMap.
1951
    typedef GR Graph;
1952
    typedef GR Digraph;
1953
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1954
    typedef K Item;
1955
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1956
    typedef K Key;
1957
    /// The value type of CrossRefMap.
1958
    typedef V Value;
1894 1959

	
1895 1960
    /// \brief Constructor.
1896 1961
    ///
1897
    /// Construct a new InvertableMap for the graph.
1898
    ///
1899
    explicit InvertableMap(const Graph& graph) : Map(graph) {}
1962
    /// Construct a new CrossRefMap for the given graph.
1963
    explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1900 1964

	
1901 1965
    /// \brief Forward iterator for values.
1902 1966
    ///
1903
    /// This iterator is an stl compatible forward
1967
    /// This iterator is an STL compatible forward
1904 1968
    /// iterator on the values of the map. The values can
1905
    /// be accessed in the [beginValue, endValue) range.
1906
    ///
1907
    class ValueIterator
1969
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1970
    /// They are considered with multiplicity, so each value is
1971
    /// traversed for each item it is assigned to.
1972
    class ValueIt
1908 1973
      : public std::iterator<std::forward_iterator_tag, Value> {
1909
      friend class InvertableMap;
1974
      friend class CrossRefMap;
1910 1975
    private:
1911
      ValueIterator(typename Container::const_iterator _it)
1976
      ValueIt(typename Container::const_iterator _it)
1912 1977
        : it(_it) {}
1913 1978
    public:
1914 1979

	
1915
      ValueIterator() {}
1916

	
1917
      ValueIterator& operator++() { ++it; return *this; }
1918
      ValueIterator operator++(int) {
1919
        ValueIterator tmp(*this);
1980
      /// Constructor
1981
      ValueIt() {}
1982

	
1983
      /// \e
1984
      ValueIt& operator++() { ++it; return *this; }
1985
      /// \e
1986
      ValueIt operator++(int) {
1987
        ValueIt tmp(*this);
1920 1988
        operator++();
1921 1989
        return tmp;
1922 1990
      }
1923 1991

	
1992
      /// \e
1924 1993
      const Value& operator*() const { return it->first; }
1994
      /// \e
1925 1995
      const Value* operator->() const { return &(it->first); }
1926 1996

	
1927
      bool operator==(ValueIterator jt) const { return it == jt.it; }
1928
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
1997
      /// \e
1998
      bool operator==(ValueIt jt) const { return it == jt.it; }
1999
      /// \e
2000
      bool operator!=(ValueIt jt) const { return it != jt.it; }
1929 2001

	
1930 2002
    private:
1931 2003
      typename Container::const_iterator it;
1932 2004
    };
2005
    
2006
    /// Alias for \c ValueIt
2007
    typedef ValueIt ValueIterator;
1933 2008

	
1934 2009
    /// \brief Returns an iterator to the first value.
1935 2010
    ///
1936
    /// Returns an stl compatible iterator to the
2011
    /// Returns an STL compatible iterator to the
1937 2012
    /// first value of the map. The values of the
1938
    /// map can be accessed in the [beginValue, endValue)
2013
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1939 2014
    /// range.
1940
    ValueIterator beginValue() const {
1941
      return ValueIterator(_inv_map.begin());
2015
    ValueIt beginValue() const {
2016
      return ValueIt(_inv_map.begin());
1942 2017
    }
1943 2018

	
1944 2019
    /// \brief Returns an iterator after the last value.
1945 2020
    ///
1946
    /// Returns an stl compatible iterator after the
2021
    /// Returns an STL compatible iterator after the
1947 2022
    /// last value of the map. The values of the
1948
    /// map can be accessed in the [beginValue, endValue)
2023
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1949 2024
    /// range.
1950
    ValueIterator endValue() const {
1951
      return ValueIterator(_inv_map.end());
2025
    ValueIt endValue() const {
2026
      return ValueIt(_inv_map.end());
1952 2027
    }
1953 2028

	
1954
    /// \brief The setter function of the map.
2029
    /// \brief Sets the value associated with the given key.
1955 2030
    ///
1956
    /// Sets the mapped value.
2031
    /// Sets the value associated with the given key.
1957 2032
    void set(const Key& key, const Value& val) {
1958 2033
      Value oldval = Map::operator[](key);
1959
      typename Container::iterator it = _inv_map.find(oldval);
1960
      if (it != _inv_map.end() && it->second == key) {
1961
        _inv_map.erase(it);
2034
      typename Container::iterator it;
2035
      for (it = _inv_map.equal_range(oldval).first;
2036
           it != _inv_map.equal_range(oldval).second; ++it) {
2037
        if (it->second == key) {
2038
          _inv_map.erase(it);
2039
          break;
2040
        }
1962 2041
      }
1963
      _inv_map.insert(make_pair(val, key));
2042
      _inv_map.insert(std::make_pair(val, key));
1964 2043
      Map::set(key, val);
1965 2044
    }
1966 2045

	
1967
    /// \brief The getter function of the map.
2046
    /// \brief Returns the value associated with the given key.
1968 2047
    ///
1969
    /// It gives back the value associated with the key.
2048
    /// Returns the value associated with the given key.
1970 2049
    typename MapTraits<Map>::ConstReturnValue
1971 2050
    operator[](const Key& key) const {
1972 2051
      return Map::operator[](key);
1973 2052
    }
1974 2053

	
1975
    /// \brief Gives back the item by its value.
2054
    /// \brief Gives back an item by its value.
1976 2055
    ///
1977
    /// Gives back the item by its value.
1978
    Key operator()(const Value& key) const {
1979
      typename Container::const_iterator it = _inv_map.find(key);
2056
    /// This function gives back an item that is assigned to
2057
    /// the given value or \c INVALID if no such item exists.
2058
    /// If there are more items with the same associated value,
2059
    /// only one of them is returned.
2060
    Key operator()(const Value& val) const {
2061
      typename Container::const_iterator it = _inv_map.find(val);
1980 2062
      return it != _inv_map.end() ? it->second : INVALID;
1981 2063
    }
2064
    
2065
    /// \brief Returns the number of items with the given value.
2066
    ///
2067
    /// This function returns the number of items with the given value
2068
    /// associated with it.
2069
    int count(const Value &val) const {
2070
      return _inv_map.count(val);
2071
    }
1982 2072

	
1983 2073
  protected:
1984 2074

	
1985
    /// \brief Erase the key from the map.
2075
    /// \brief Erase the key from the map and the inverse map.
1986 2076
    ///
1987
    /// Erase the key to the map. It is called by the
2077
    /// Erase the key from the map and the inverse map. It is called by the
1988 2078
    /// \c AlterationNotifier.
1989 2079
    virtual void erase(const Key& key) {
1990 2080
      Value val = Map::operator[](key);
1991
      typename Container::iterator it = _inv_map.find(val);
1992
      if (it != _inv_map.end() && it->second == key) {
1993
        _inv_map.erase(it);
2081
      typename Container::iterator it;
2082
      for (it = _inv_map.equal_range(val).first;
2083
           it != _inv_map.equal_range(val).second; ++it) {
2084
        if (it->second == key) {
2085
          _inv_map.erase(it);
2086
          break;
2087
        }
1994 2088
      }
1995 2089
      Map::erase(key);
1996 2090
    }
1997 2091

	
1998
    /// \brief Erase more keys from the map.
2092
    /// \brief Erase more keys from the map and the inverse map.
1999 2093
    ///
2000
    /// Erase more keys from the map. It is called by the
2094
    /// Erase more keys from the map and the inverse map. It is called by the
2001 2095
    /// \c AlterationNotifier.
2002 2096
    virtual void erase(const std::vector<Key>& keys) {
2003 2097
      for (int i = 0; i < int(keys.size()); ++i) {
2004 2098
        Value val = Map::operator[](keys[i]);
2005
        typename Container::iterator it = _inv_map.find(val);
2006
        if (it != _inv_map.end() && it->second == keys[i]) {
2007
          _inv_map.erase(it);
2099
        typename Container::iterator it;
2100
        for (it = _inv_map.equal_range(val).first;
2101
             it != _inv_map.equal_range(val).second; ++it) {
2102
          if (it->second == keys[i]) {
2103
            _inv_map.erase(it);
2104
            break;
2105
          }
2008 2106
        }
2009 2107
      }
2010 2108
      Map::erase(keys);
2011 2109
    }
2012 2110

	
2013
    /// \brief Clear the keys from the map and inverse map.
2111
    /// \brief Clear the keys from the map and the inverse map.
2014 2112
    ///
2015
    /// Clear the keys from the map and inverse map. It is called by the
2113
    /// Clear the keys from the map and the inverse map. It is called by the
2016 2114
    /// \c AlterationNotifier.
2017 2115
    virtual void clear() {
2018 2116
      _inv_map.clear();
2019 2117
      Map::clear();
2020 2118
    }
2021 2119

	
2022 2120
  public:
2023 2121

	
2024
    /// \brief The inverse map type.
2122
    /// \brief The inverse map type of CrossRefMap.
2025 2123
    ///
2026
    /// The inverse of this map. The subscript operator of the map
2027
    /// gives back always the item what was last assigned to the value.
2124
    /// The inverse map type of CrossRefMap. The subscript operator gives
2125
    /// back an item by its value.
2126
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2127
    /// \see inverse()
2028 2128
    class InverseMap {
2029 2129
    public:
2030
      /// \brief Constructor of the InverseMap.
2130
      /// \brief Constructor
2031 2131
      ///
2032 2132
      /// Constructor of the InverseMap.
2033
      explicit InverseMap(const InvertableMap& inverted)
2133
      explicit InverseMap(const CrossRefMap& inverted)
2034 2134
        : _inverted(inverted) {}
2035 2135

	
2036 2136
      /// The value type of the InverseMap.
2037
      typedef typename InvertableMap::Key Value;
2137
      typedef typename CrossRefMap::Key Value;
2038 2138
      /// The key type of the InverseMap.
2039
      typedef typename InvertableMap::Value Key;
2139
      typedef typename CrossRefMap::Value Key;
2040 2140

	
2041 2141
      /// \brief Subscript operator.
2042 2142
      ///
2043
      /// Subscript operator. It gives back always the item
2044
      /// what was last assigned to the value.
2143
      /// Subscript operator. It gives back an item
2144
      /// that is assigned to the given value or \c INVALID
2145
      /// if no such item exists.
2045 2146
      Value operator[](const Key& key) const {
2046 2147
        return _inverted(key);
2047 2148
      }
2048 2149

	
2049 2150
    private:
2050
      const InvertableMap& _inverted;
2151
      const CrossRefMap& _inverted;
2051 2152
    };
2052 2153

	
2053
    /// \brief It gives back the just readable inverse map.
2154
    /// \brief Gives back the inverse of the map.
2054 2155
    ///
2055
    /// It gives back the just readable inverse map.
2156
    /// Gives back the inverse of the CrossRefMap.
2056 2157
    InverseMap inverse() const {
2057 2158
      return InverseMap(*this);
2058 2159
    }
2059 2160

	
2060 2161
  };
2061 2162

	
2062
  /// \brief Provides a mutable, continuous and unique descriptor for each
2063
  /// item in the graph.
2163
  /// \brief Provides continuous and unique id for the
2164
  /// items of a graph.
2064 2165
  ///
2065
  /// The DescriptorMap class provides a unique and continuous (but mutable)
2066
  /// descriptor (id) for each item of the same type (e.g. node) in the
2067
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
2068
  /// different ids <li>\b continuous: the range of the ids is the set of
2069
  /// integers between 0 and \c n-1, where \c n is the number of the items of
2070
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
2071
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
2072
  /// with its member class \c InverseMap, or with the \c operator() member.
2166
  /// RangeIdMap provides a unique and continuous
2167
  /// id for each item of a given type (\c Node, \c Arc or
2168
  /// \c Edge) in a graph. This id is
2169
  ///  - \b unique: different items get different ids,
2170
  ///  - \b continuous: the range of the ids is the set of integers
2171
  ///    between 0 and \c n-1, where \c n is the number of the items of
2172
  ///    this type (\c Node, \c Arc or \c Edge).
2173
  ///  - So, the ids can change when deleting an item of the same type.
2073 2174
  ///
2074
  /// \tparam _Graph The graph class the \c DescriptorMap belongs to.
2075
  /// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or
2076
  /// Edge.
2077
  template <typename _Graph, typename _Item>
2078
  class DescriptorMap
2079
    : protected ItemSetTraits<_Graph, _Item>::template Map<int>::Type {
2080

	
2081
    typedef _Item Item;
2082
    typedef typename ItemSetTraits<_Graph, _Item>::template Map<int>::Type Map;
2175
  /// Thus this id is not (necessarily) the same as what can get using
2176
  /// the \c id() function of the graph or \ref IdMap.
2177
  /// This map can be inverted with its member class \c InverseMap,
2178
  /// or with the \c operator()() member.
2179
  ///
2180
  /// \tparam GR The graph type.
2181
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2182
  /// \c GR::Edge).
2183
  ///
2184
  /// \see IdMap
2185
  template <typename GR, typename K>
2186
  class RangeIdMap
2187
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2188

	
2189
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2083 2190

	
2084 2191
  public:
2085
    /// The graph class of DescriptorMap.
2086
    typedef _Graph Graph;
2087

	
2088
    /// The key type of DescriptorMap (Node, Arc, Edge).
2089
    typedef typename Map::Key Key;
2090
    /// The value type of DescriptorMap.
2091
    typedef typename Map::Value Value;
2192
    /// The graph type of RangeIdMap.
2193
    typedef GR Graph;
2194
    typedef GR Digraph;
2195
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2196
    typedef K Item;
2197
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2198
    typedef K Key;
2199
    /// The value type of RangeIdMap.
2200
    typedef int Value;
2092 2201

	
2093 2202
    /// \brief Constructor.
2094 2203
    ///
2095
    /// Constructor for descriptor map.
2096
    explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
2204
    /// Constructor.
2205
    explicit RangeIdMap(const Graph& gr) : Map(gr) {
2097 2206
      Item it;
2098 2207
      const typename Map::Notifier* nf = Map::notifier();
2099 2208
      for (nf->first(it); it != INVALID; nf->next(it)) {
2100 2209
        Map::set(it, _inv_map.size());
2101 2210
        _inv_map.push_back(it);
2102 2211
      }
2103 2212
    }
2104 2213

	
2105 2214
  protected:
2106 2215

	
2107
    /// \brief Add a new key to the map.
2216
    /// \brief Adds a new key to the map.
2108 2217
    ///
2109 2218
    /// Add a new key to the map. It is called by the
2110 2219
    /// \c AlterationNotifier.
2111 2220
    virtual void add(const Item& item) {
2112 2221
      Map::add(item);
2113 2222
      Map::set(item, _inv_map.size());
2114 2223
      _inv_map.push_back(item);
2115 2224
    }
2116 2225

	
2117 2226
    /// \brief Add more new keys to the map.
2118 2227
    ///
2119 2228
    /// Add more new keys to the map. It is called by the
2120 2229
    /// \c AlterationNotifier.
2121 2230
    virtual void add(const std::vector<Item>& items) {
2122 2231
      Map::add(items);
2123 2232
      for (int i = 0; i < int(items.size()); ++i) {
2124 2233
        Map::set(items[i], _inv_map.size());
2125 2234
        _inv_map.push_back(items[i]);
2126 2235
      }
2127 2236
    }
2128 2237

	
2129 2238
    /// \brief Erase the key from the map.
2130 2239
    ///
2131 2240
    /// Erase the key from the map. It is called by the
2132 2241
    /// \c AlterationNotifier.
2133 2242
    virtual void erase(const Item& item) {
2134 2243
      Map::set(_inv_map.back(), Map::operator[](item));
2135 2244
      _inv_map[Map::operator[](item)] = _inv_map.back();
2136 2245
      _inv_map.pop_back();
2137 2246
      Map::erase(item);
2138 2247
    }
2139 2248

	
2140 2249
    /// \brief Erase more keys from the map.
2141 2250
    ///
2142 2251
    /// Erase more keys from the map. It is called by the
2143 2252
    /// \c AlterationNotifier.
2144 2253
    virtual void erase(const std::vector<Item>& items) {
2145 2254
      for (int i = 0; i < int(items.size()); ++i) {
2146 2255
        Map::set(_inv_map.back(), Map::operator[](items[i]));
2147 2256
        _inv_map[Map::operator[](items[i])] = _inv_map.back();
2148 2257
        _inv_map.pop_back();
2149 2258
      }
2150 2259
      Map::erase(items);
2151 2260
    }
2152 2261

	
2153 2262
    /// \brief Build the unique map.
2154 2263
    ///
2155 2264
    /// Build the unique map. It is called by the
2156 2265
    /// \c AlterationNotifier.
2157 2266
    virtual void build() {
2158 2267
      Map::build();
2159 2268
      Item it;
2160 2269
      const typename Map::Notifier* nf = Map::notifier();
2161 2270
      for (nf->first(it); it != INVALID; nf->next(it)) {
2162 2271
        Map::set(it, _inv_map.size());
2163 2272
        _inv_map.push_back(it);
2164 2273
      }
2165 2274
    }
2166 2275

	
2167 2276
    /// \brief Clear the keys from the map.
2168 2277
    ///
2169 2278
    /// Clear the keys from the map. It is called by the
2170 2279
    /// \c AlterationNotifier.
2171 2280
    virtual void clear() {
2172 2281
      _inv_map.clear();
2173 2282
      Map::clear();
2174 2283
    }
2175 2284

	
2176 2285
  public:
2177 2286

	
2178 2287
    /// \brief Returns the maximal value plus one.
2179 2288
    ///
2180 2289
    /// Returns the maximal value plus one in the map.
2181 2290
    unsigned int size() const {
2182 2291
      return _inv_map.size();
2183 2292
    }
2184 2293

	
2185 2294
    /// \brief Swaps the position of the two items in the map.
2186 2295
    ///
2187 2296
    /// Swaps the position of the two items in the map.
2188 2297
    void swap(const Item& p, const Item& q) {
2189 2298
      int pi = Map::operator[](p);
2190 2299
      int qi = Map::operator[](q);
2191 2300
      Map::set(p, qi);
2192 2301
      _inv_map[qi] = p;
2193 2302
      Map::set(q, pi);
2194 2303
      _inv_map[pi] = q;
2195 2304
    }
2196 2305

	
2197
    /// \brief Gives back the \e descriptor of the item.
2306
    /// \brief Gives back the \e range \e id of the item
2198 2307
    ///
2199
    /// Gives back the mutable and unique \e descriptor of the map.
2308
    /// Gives back the \e range \e id of the item.
2200 2309
    int operator[](const Item& item) const {
2201 2310
      return Map::operator[](item);
2202 2311
    }
2203 2312

	
2204
    /// \brief Gives back the item by its descriptor.
2313
    /// \brief Gives back the item belonging to a \e range \e id
2205 2314
    ///
2206
    /// Gives back th item by its descriptor.
2315
    /// Gives back the item belonging to the given \e range \e id.
2207 2316
    Item operator()(int id) const {
2208 2317
      return _inv_map[id];
2209 2318
    }
2210 2319

	
2211 2320
  private:
2212 2321

	
2213 2322
    typedef std::vector<Item> Container;
2214 2323
    Container _inv_map;
2215 2324

	
2216 2325
  public:
2217
    /// \brief The inverse map type of DescriptorMap.
2326

	
2327
    /// \brief The inverse map type of RangeIdMap.
2218 2328
    ///
2219
    /// The inverse map type of DescriptorMap.
2329
    /// The inverse map type of RangeIdMap. The subscript operator gives
2330
    /// back an item by its \e range \e id.
2331
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2220 2332
    class InverseMap {
2221 2333
    public:
2222
      /// \brief Constructor of the InverseMap.
2334
      /// \brief Constructor
2223 2335
      ///
2224 2336
      /// Constructor of the InverseMap.
2225
      explicit InverseMap(const DescriptorMap& inverted)
2337
      explicit InverseMap(const RangeIdMap& inverted)
2226 2338
        : _inverted(inverted) {}
2227 2339

	
2228 2340

	
2229 2341
      /// The value type of the InverseMap.
2230
      typedef typename DescriptorMap::Key Value;
2342
      typedef typename RangeIdMap::Key Value;
2231 2343
      /// The key type of the InverseMap.
2232
      typedef typename DescriptorMap::Value Key;
2344
      typedef typename RangeIdMap::Value Key;
2233 2345

	
2234 2346
      /// \brief Subscript operator.
2235 2347
      ///
2236 2348
      /// Subscript operator. It gives back the item
2237
      /// that the descriptor belongs to currently.
2349
      /// that the given \e range \e id currently belongs to.
2238 2350
      Value operator[](const Key& key) const {
2239 2351
        return _inverted(key);
2240 2352
      }
2241 2353

	
2242 2354
      /// \brief Size of the map.
2243 2355
      ///
2244 2356
      /// Returns the size of the map.
2245 2357
      unsigned int size() const {
2246 2358
        return _inverted.size();
2247 2359
      }
2248 2360

	
2249 2361
    private:
2250
      const DescriptorMap& _inverted;
2362
      const RangeIdMap& _inverted;
2251 2363
    };
2252 2364

	
2253 2365
    /// \brief Gives back the inverse of the map.
2254 2366
    ///
2255
    /// Gives back the inverse of the map.
2367
    /// Gives back the inverse of the RangeIdMap.
2256 2368
    const InverseMap inverse() const {
2257 2369
      return InverseMap(*this);
2258 2370
    }
2259 2371
  };
2260 2372

	
2261
  /// \brief Returns the source of the given arc.
2373
  /// \brief Returns a \c RangeIdMap class.
2262 2374
  ///
2263
  /// The SourceMap gives back the source Node of the given arc.
2375
  /// This function just returns an \c RangeIdMap class.
2376
  /// \relates RangeIdMap
2377
  template <typename K, typename GR>
2378
  inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
2379
    return RangeIdMap<GR, K>(graph);
2380
  }
2381
  
2382
  /// \brief Dynamic iterable \c bool map.
2383
  ///
2384
  /// This class provides a special graph map type which can store a
2385
  /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
2386
  /// For both \c true and \c false values it is possible to iterate on
2387
  /// the keys mapped to the value.
2388
  ///
2389
  /// This type is a reference map, so it can be modified with the
2390
  /// subscript operator.
2391
  ///
2392
  /// \tparam GR The graph type.
2393
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2394
  /// \c GR::Edge).
2395
  ///
2396
  /// \see IterableIntMap, IterableValueMap
2397
  /// \see CrossRefMap
2398
  template <typename GR, typename K>
2399
  class IterableBoolMap
2400
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2401
  private:
2402
    typedef GR Graph;
2403

	
2404
    typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
2405
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
2406

	
2407
    std::vector<K> _array;
2408
    int _sep;
2409

	
2410
  public:
2411

	
2412
    /// Indicates that the map is reference map.
2413
    typedef True ReferenceMapTag;
2414

	
2415
    /// The key type
2416
    typedef K Key;
2417
    /// The value type
2418
    typedef bool Value;
2419
    /// The const reference type.
2420
    typedef const Value& ConstReference;
2421

	
2422
  private:
2423

	
2424
    int position(const Key& key) const {
2425
      return Parent::operator[](key);
2426
    }
2427

	
2428
  public:
2429

	
2430
    /// \brief Reference to the value of the map.
2431
    ///
2432
    /// This class is similar to the \c bool type. It can be converted to
2433
    /// \c bool and it provides the same operators.
2434
    class Reference {
2435
      friend class IterableBoolMap;
2436
    private:
2437
      Reference(IterableBoolMap& map, const Key& key)
2438
        : _key(key), _map(map) {}
2439
    public:
2440

	
2441
      Reference& operator=(const Reference& value) {
2442
        _map.set(_key, static_cast<bool>(value));
2443
         return *this;
2444
      }
2445

	
2446
      operator bool() const {
2447
        return static_cast<const IterableBoolMap&>(_map)[_key];
2448
      }
2449

	
2450
      Reference& operator=(bool value) {
2451
        _map.set(_key, value);
2452
        return *this;
2453
      }
2454
      Reference& operator&=(bool value) {
2455
        _map.set(_key, _map[_key] & value);
2456
        return *this;
2457
      }
2458
      Reference& operator|=(bool value) {
2459
        _map.set(_key, _map[_key] | value);
2460
        return *this;
2461
      }
2462
      Reference& operator^=(bool value) {
2463
        _map.set(_key, _map[_key] ^ value);
2464
        return *this;
2465
      }
2466
    private:
2467
      Key _key;
2468
      IterableBoolMap& _map;
2469
    };
2470

	
2471
    /// \brief Constructor of the map with a default value.
2472
    ///
2473
    /// Constructor of the map with a default value.
2474
    explicit IterableBoolMap(const Graph& graph, bool def = false)
2475
      : Parent(graph) {
2476
      typename Parent::Notifier* nf = Parent::notifier();
2477
      Key it;
2478
      for (nf->first(it); it != INVALID; nf->next(it)) {
2479
        Parent::set(it, _array.size());
2480
        _array.push_back(it);
2481
      }
2482
      _sep = (def ? _array.size() : 0);
2483
    }
2484

	
2485
    /// \brief Const subscript operator of the map.
2486
    ///
2487
    /// Const subscript operator of the map.
2488
    bool operator[](const Key& key) const {
2489
      return position(key) < _sep;
2490
    }
2491

	
2492
    /// \brief Subscript operator of the map.
2493
    ///
2494
    /// Subscript operator of the map.
2495
    Reference operator[](const Key& key) {
2496
      return Reference(*this, key);
2497
    }
2498

	
2499
    /// \brief Set operation of the map.
2500
    ///
2501
    /// Set operation of the map.
2502
    void set(const Key& key, bool value) {
2503
      int pos = position(key);
2504
      if (value) {
2505
        if (pos < _sep) return;
2506
        Key tmp = _array[_sep];
2507
        _array[_sep] = key;
2508
        Parent::set(key, _sep);
2509
        _array[pos] = tmp;
2510
        Parent::set(tmp, pos);
2511
        ++_sep;
2512
      } else {
2513
        if (pos >= _sep) return;
2514
        --_sep;
2515
        Key tmp = _array[_sep];
2516
        _array[_sep] = key;
2517
        Parent::set(key, _sep);
2518
        _array[pos] = tmp;
2519
        Parent::set(tmp, pos);
2520
      }
2521
    }
2522

	
2523
    /// \brief Set all items.
2524
    ///
2525
    /// Set all items in the map.
2526
    /// \note Constant time operation.
2527
    void setAll(bool value) {
2528
      _sep = (value ? _array.size() : 0);
2529
    }
2530

	
2531
    /// \brief Returns the number of the keys mapped to \c true.
2532
    ///
2533
    /// Returns the number of the keys mapped to \c true.
2534
    int trueNum() const {
2535
      return _sep;
2536
    }
2537

	
2538
    /// \brief Returns the number of the keys mapped to \c false.
2539
    ///
2540
    /// Returns the number of the keys mapped to \c false.
2541
    int falseNum() const {
2542
      return _array.size() - _sep;
2543
    }
2544

	
2545
    /// \brief Iterator for the keys mapped to \c true.
2546
    ///
2547
    /// Iterator for the keys mapped to \c true. It works
2548
    /// like a graph item iterator, it can be converted to
2549
    /// the key type of the map, incremented with \c ++ operator, and
2550
    /// if the iterator leaves the last valid key, it will be equal to
2551
    /// \c INVALID.
2552
    class TrueIt : public Key {
2553
    public:
2554
      typedef Key Parent;
2555

	
2556
      /// \brief Creates an iterator.
2557
      ///
2558
      /// Creates an iterator. It iterates on the
2559
      /// keys mapped to \c true.
2560
      /// \param map The IterableBoolMap.
2561
      explicit TrueIt(const IterableBoolMap& map)
2562
        : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
2563
          _map(&map) {}
2564

	
2565
      /// \brief Invalid constructor \& conversion.
2566
      ///
2567
      /// This constructor initializes the iterator to be invalid.
2568
      /// \sa Invalid for more details.
2569
      TrueIt(Invalid) : Parent(INVALID), _map(0) {}
2570

	
2571
      /// \brief Increment operator.
2572
      ///
2573
      /// Increment operator.
2574
      TrueIt& operator++() {
2575
        int pos = _map->position(*this);
2576
        Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
2577
        return *this;
2578
      }
2579

	
2580
    private:
2581
      const IterableBoolMap* _map;
2582
    };
2583

	
2584
    /// \brief Iterator for the keys mapped to \c false.
2585
    ///
2586
    /// Iterator for the keys mapped to \c false. It works
2587
    /// like a graph item iterator, it can be converted to
2588
    /// the key type of the map, incremented with \c ++ operator, and
2589
    /// if the iterator leaves the last valid key, it will be equal to
2590
    /// \c INVALID.
2591
    class FalseIt : public Key {
2592
    public:
2593
      typedef Key Parent;
2594

	
2595
      /// \brief Creates an iterator.
2596
      ///
2597
      /// Creates an iterator. It iterates on the
2598
      /// keys mapped to \c false.
2599
      /// \param map The IterableBoolMap.
2600
      explicit FalseIt(const IterableBoolMap& map)
2601
        : Parent(map._sep < int(map._array.size()) ?
2602
                 map._array.back() : INVALID), _map(&map) {}
2603

	
2604
      /// \brief Invalid constructor \& conversion.
2605
      ///
2606
      /// This constructor initializes the iterator to be invalid.
2607
      /// \sa Invalid for more details.
2608
      FalseIt(Invalid) : Parent(INVALID), _map(0) {}
2609

	
2610
      /// \brief Increment operator.
2611
      ///
2612
      /// Increment operator.
2613
      FalseIt& operator++() {
2614
        int pos = _map->position(*this);
2615
        Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
2616
        return *this;
2617
      }
2618

	
2619
    private:
2620
      const IterableBoolMap* _map;
2621
    };
2622

	
2623
    /// \brief Iterator for the keys mapped to a given value.
2624
    ///
2625
    /// Iterator for the keys mapped to a given value. It works
2626
    /// like a graph item iterator, it can be converted to
2627
    /// the key type of the map, incremented with \c ++ operator, and
2628
    /// if the iterator leaves the last valid key, it will be equal to
2629
    /// \c INVALID.
2630
    class ItemIt : public Key {
2631
    public:
2632
      typedef Key Parent;
2633

	
2634
      /// \brief Creates an iterator with a value.
2635
      ///
2636
      /// Creates an iterator with a value. It iterates on the
2637
      /// keys mapped to the given value.
2638
      /// \param map The IterableBoolMap.
2639
      /// \param value The value.
2640
      ItemIt(const IterableBoolMap& map, bool value)
2641
        : Parent(value ? 
2642
                 (map._sep > 0 ?
2643
                  map._array[map._sep - 1] : INVALID) :
2644
                 (map._sep < int(map._array.size()) ?
2645
                  map._array.back() : INVALID)), _map(&map) {}
2646

	
2647
      /// \brief Invalid constructor \& conversion.
2648
      ///
2649
      /// This constructor initializes the iterator to be invalid.
2650
      /// \sa Invalid for more details.
2651
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2652

	
2653
      /// \brief Increment operator.
2654
      ///
2655
      /// Increment operator.
2656
      ItemIt& operator++() {
2657
        int pos = _map->position(*this);
2658
        int _sep = pos >= _map->_sep ? _map->_sep : 0;
2659
        Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
2660
        return *this;
2661
      }
2662

	
2663
    private:
2664
      const IterableBoolMap* _map;
2665
    };
2666

	
2667
  protected:
2668

	
2669
    virtual void add(const Key& key) {
2670
      Parent::add(key);
2671
      Parent::set(key, _array.size());
2672
      _array.push_back(key);
2673
    }
2674

	
2675
    virtual void add(const std::vector<Key>& keys) {
2676
      Parent::add(keys);
2677
      for (int i = 0; i < int(keys.size()); ++i) {
2678
        Parent::set(keys[i], _array.size());
2679
        _array.push_back(keys[i]);
2680
      }
2681
    }
2682

	
2683
    virtual void erase(const Key& key) {
2684
      int pos = position(key);
2685
      if (pos < _sep) {
2686
        --_sep;
2687
        Parent::set(_array[_sep], pos);
2688
        _array[pos] = _array[_sep];
2689
        Parent::set(_array.back(), _sep);
2690
        _array[_sep] = _array.back();
2691
        _array.pop_back();
2692
      } else {
2693
        Parent::set(_array.back(), pos);
2694
        _array[pos] = _array.back();
2695
        _array.pop_back();
2696
      }
2697
      Parent::erase(key);
2698
    }
2699

	
2700
    virtual void erase(const std::vector<Key>& keys) {
2701
      for (int i = 0; i < int(keys.size()); ++i) {
2702
        int pos = position(keys[i]);
2703
        if (pos < _sep) {
2704
          --_sep;
2705
          Parent::set(_array[_sep], pos);
2706
          _array[pos] = _array[_sep];
2707
          Parent::set(_array.back(), _sep);
2708
          _array[_sep] = _array.back();
2709
          _array.pop_back();
2710
        } else {
2711
          Parent::set(_array.back(), pos);
2712
          _array[pos] = _array.back();
2713
          _array.pop_back();
2714
        }
2715
      }
2716
      Parent::erase(keys);
2717
    }
2718

	
2719
    virtual void build() {
2720
      Parent::build();
2721
      typename Parent::Notifier* nf = Parent::notifier();
2722
      Key it;
2723
      for (nf->first(it); it != INVALID; nf->next(it)) {
2724
        Parent::set(it, _array.size());
2725
        _array.push_back(it);
2726
      }
2727
      _sep = 0;
2728
    }
2729

	
2730
    virtual void clear() {
2731
      _array.clear();
2732
      _sep = 0;
2733
      Parent::clear();
2734
    }
2735

	
2736
  };
2737

	
2738

	
2739
  namespace _maps_bits {
2740
    template <typename Item>
2741
    struct IterableIntMapNode {
2742
      IterableIntMapNode() : value(-1) {}
2743
      IterableIntMapNode(int _value) : value(_value) {}
2744
      Item prev, next;
2745
      int value;
2746
    };
2747
  }
2748

	
2749
  /// \brief Dynamic iterable integer map.
2750
  ///
2751
  /// This class provides a special graph map type which can store an
2752
  /// integer value for graph items (\c Node, \c Arc or \c Edge).
2753
  /// For each non-negative value it is possible to iterate on the keys
2754
  /// mapped to the value.
2755
  ///
2756
  /// This map is intended to be used with small integer values, for which
2757
  /// it is efficient, and supports iteration only for non-negative values.
2758
  /// If you need large values and/or iteration for negative integers,
2759
  /// consider to use \ref IterableValueMap instead.
2760
  ///
2761
  /// This type is a reference map, so it can be modified with the
2762
  /// subscript operator.
2763
  ///
2764
  /// \note The size of the data structure depends on the largest
2765
  /// value in the map.
2766
  ///
2767
  /// \tparam GR The graph type.
2768
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2769
  /// \c GR::Edge).
2770
  ///
2771
  /// \see IterableBoolMap, IterableValueMap
2772
  /// \see CrossRefMap
2773
  template <typename GR, typename K>
2774
  class IterableIntMap
2775
    : protected ItemSetTraits<GR, K>::
2776
        template Map<_maps_bits::IterableIntMapNode<K> >::Type {
2777
  public:
2778
    typedef typename ItemSetTraits<GR, K>::
2779
      template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
2780

	
2781
    /// The key type
2782
    typedef K Key;
2783
    /// The value type
2784
    typedef int Value;
2785
    /// The graph type
2786
    typedef GR Graph;
2787

	
2788
    /// \brief Constructor of the map.
2789
    ///
2790
    /// Constructor of the map. It sets all values to -1.
2791
    explicit IterableIntMap(const Graph& graph)
2792
      : Parent(graph) {}
2793

	
2794
    /// \brief Constructor of the map with a given value.
2795
    ///
2796
    /// Constructor of the map with a given value.
2797
    explicit IterableIntMap(const Graph& graph, int value)
2798
      : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
2799
      if (value >= 0) {
2800
        for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
2801
          lace(it);
2802
        }
2803
      }
2804
    }
2805

	
2806
  private:
2807

	
2808
    void unlace(const Key& key) {
2809
      typename Parent::Value& node = Parent::operator[](key);
2810
      if (node.value < 0) return;
2811
      if (node.prev != INVALID) {
2812
        Parent::operator[](node.prev).next = node.next;
2813
      } else {
2814
        _first[node.value] = node.next;
2815
      }
2816
      if (node.next != INVALID) {
2817
        Parent::operator[](node.next).prev = node.prev;
2818
      }
2819
      while (!_first.empty() && _first.back() == INVALID) {
2820
        _first.pop_back();
2821
      }
2822
    }
2823

	
2824
    void lace(const Key& key) {
2825
      typename Parent::Value& node = Parent::operator[](key);
2826
      if (node.value < 0) return;
2827
      if (node.value >= int(_first.size())) {
2828
        _first.resize(node.value + 1, INVALID);
2829
      }
2830
      node.prev = INVALID;
2831
      node.next = _first[node.value];
2832
      if (node.next != INVALID) {
2833
        Parent::operator[](node.next).prev = key;
2834
      }
2835
      _first[node.value] = key;
2836
    }
2837

	
2838
  public:
2839

	
2840
    /// Indicates that the map is reference map.
2841
    typedef True ReferenceMapTag;
2842

	
2843
    /// \brief Reference to the value of the map.
2844
    ///
2845
    /// This class is similar to the \c int type. It can
2846
    /// be converted to \c int and it has the same operators.
2847
    class Reference {
2848
      friend class IterableIntMap;
2849
    private:
2850
      Reference(IterableIntMap& map, const Key& key)
2851
        : _key(key), _map(map) {}
2852
    public:
2853

	
2854
      Reference& operator=(const Reference& value) {
2855
        _map.set(_key, static_cast<const int&>(value));
2856
         return *this;
2857
      }
2858

	
2859
      operator const int&() const {
2860
        return static_cast<const IterableIntMap&>(_map)[_key];
2861
      }
2862

	
2863
      Reference& operator=(int value) {
2864
        _map.set(_key, value);
2865
        return *this;
2866
      }
2867
      Reference& operator++() {
2868
        _map.set(_key, _map[_key] + 1);
2869
        return *this;
2870
      }
2871
      int operator++(int) {
2872
        int value = _map[_key];
2873
        _map.set(_key, value + 1);
2874
        return value;
2875
      }
2876
      Reference& operator--() {
2877
        _map.set(_key, _map[_key] - 1);
2878
        return *this;
2879
      }
2880
      int operator--(int) {
2881
        int value = _map[_key];
2882
        _map.set(_key, value - 1);
2883
        return value;
2884
      }
2885
      Reference& operator+=(int value) {
2886
        _map.set(_key, _map[_key] + value);
2887
        return *this;
2888
      }
2889
      Reference& operator-=(int value) {
2890
        _map.set(_key, _map[_key] - value);
2891
        return *this;
2892
      }
2893
      Reference& operator*=(int value) {
2894
        _map.set(_key, _map[_key] * value);
2895
        return *this;
2896
      }
2897
      Reference& operator/=(int value) {
2898
        _map.set(_key, _map[_key] / value);
2899
        return *this;
2900
      }
2901
      Reference& operator%=(int value) {
2902
        _map.set(_key, _map[_key] % value);
2903
        return *this;
2904
      }
2905
      Reference& operator&=(int value) {
2906
        _map.set(_key, _map[_key] & value);
2907
        return *this;
2908
      }
2909
      Reference& operator|=(int value) {
2910
        _map.set(_key, _map[_key] | value);
2911
        return *this;
2912
      }
2913
      Reference& operator^=(int value) {
2914
        _map.set(_key, _map[_key] ^ value);
2915
        return *this;
2916
      }
2917
      Reference& operator<<=(int value) {
2918
        _map.set(_key, _map[_key] << value);
2919
        return *this;
2920
      }
2921
      Reference& operator>>=(int value) {
2922
        _map.set(_key, _map[_key] >> value);
2923
        return *this;
2924
      }
2925

	
2926
    private:
2927
      Key _key;
2928
      IterableIntMap& _map;
2929
    };
2930

	
2931
    /// The const reference type.
2932
    typedef const Value& ConstReference;
2933

	
2934
    /// \brief Gives back the maximal value plus one.
2935
    ///
2936
    /// Gives back the maximal value plus one.
2937
    int size() const {
2938
      return _first.size();
2939
    }
2940

	
2941
    /// \brief Set operation of the map.
2942
    ///
2943
    /// Set operation of the map.
2944
    void set(const Key& key, const Value& value) {
2945
      unlace(key);
2946
      Parent::operator[](key).value = value;
2947
      lace(key);
2948
    }
2949

	
2950
    /// \brief Const subscript operator of the map.
2951
    ///
2952
    /// Const subscript operator of the map.
2953
    const Value& operator[](const Key& key) const {
2954
      return Parent::operator[](key).value;
2955
    }
2956

	
2957
    /// \brief Subscript operator of the map.
2958
    ///
2959
    /// Subscript operator of the map.
2960
    Reference operator[](const Key& key) {
2961
      return Reference(*this, key);
2962
    }
2963

	
2964
    /// \brief Iterator for the keys with the same value.
2965
    ///
2966
    /// Iterator for the keys with the same value. It works
2967
    /// like a graph item iterator, it can be converted to
2968
    /// the item type of the map, incremented with \c ++ operator, and
2969
    /// if the iterator leaves the last valid item, it will be equal to
2970
    /// \c INVALID.
2971
    class ItemIt : public Key {
2972
    public:
2973
      typedef Key Parent;
2974

	
2975
      /// \brief Invalid constructor \& conversion.
2976
      ///
2977
      /// This constructor initializes the iterator to be invalid.
2978
      /// \sa Invalid for more details.
2979
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2980

	
2981
      /// \brief Creates an iterator with a value.
2982
      ///
2983
      /// Creates an iterator with a value. It iterates on the
2984
      /// keys mapped to the given value.
2985
      /// \param map The IterableIntMap.
2986
      /// \param value The value.
2987
      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
2988
        if (value < 0 || value >= int(_map->_first.size())) {
2989
          Parent::operator=(INVALID);
2990
        } else {
2991
          Parent::operator=(_map->_first[value]);
2992
        }
2993
      }
2994

	
2995
      /// \brief Increment operator.
2996
      ///
2997
      /// Increment operator.
2998
      ItemIt& operator++() {
2999
        Parent::operator=(_map->IterableIntMap::Parent::
3000
                          operator[](static_cast<Parent&>(*this)).next);
3001
        return *this;
3002
      }
3003

	
3004
    private:
3005
      const IterableIntMap* _map;
3006
    };
3007

	
3008
  protected:
3009

	
3010
    virtual void erase(const Key& key) {
3011
      unlace(key);
3012
      Parent::erase(key);
3013
    }
3014

	
3015
    virtual void erase(const std::vector<Key>& keys) {
3016
      for (int i = 0; i < int(keys.size()); ++i) {
3017
        unlace(keys[i]);
3018
      }
3019
      Parent::erase(keys);
3020
    }
3021

	
3022
    virtual void clear() {
3023
      _first.clear();
3024
      Parent::clear();
3025
    }
3026

	
3027
  private:
3028
    std::vector<Key> _first;
3029
  };
3030

	
3031
  namespace _maps_bits {
3032
    template <typename Item, typename Value>
3033
    struct IterableValueMapNode {
3034
      IterableValueMapNode(Value _value = Value()) : value(_value) {}
3035
      Item prev, next;
3036
      Value value;
3037
    };
3038
  }
3039

	
3040
  /// \brief Dynamic iterable map for comparable values.
3041
  ///
3042
  /// This class provides a special graph map type which can store a
3043
  /// comparable value for graph items (\c Node, \c Arc or \c Edge).
3044
  /// For each value it is possible to iterate on the keys mapped to
3045
  /// the value (\c ItemIt), and the values of the map can be accessed
3046
  /// with an STL compatible forward iterator (\c ValueIt).
3047
  /// The map stores a linked list for each value, which contains
3048
  /// the items mapped to the value, and the used values are stored
3049
  /// in balanced binary tree (\c std::map).
3050
  ///
3051
  /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
3052
  /// specialized for \c bool and \c int values, respectively.
3053
  ///
3054
  /// This type is not reference map, so it cannot be modified with
3055
  /// the subscript operator.
3056
  ///
3057
  /// \tparam GR The graph type.
3058
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
3059
  /// \c GR::Edge).
3060
  /// \tparam V The value type of the map. It can be any comparable
3061
  /// value type.
3062
  ///
3063
  /// \see IterableBoolMap, IterableIntMap
3064
  /// \see CrossRefMap
3065
  template <typename GR, typename K, typename V>
3066
  class IterableValueMap
3067
    : protected ItemSetTraits<GR, K>::
3068
        template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
3069
  public:
3070
    typedef typename ItemSetTraits<GR, K>::
3071
      template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
3072

	
3073
    /// The key type
3074
    typedef K Key;
3075
    /// The value type
3076
    typedef V Value;
3077
    /// The graph type
3078
    typedef GR Graph;
3079

	
3080
  public:
3081

	
3082
    /// \brief Constructor of the map with a given value.
3083
    ///
3084
    /// Constructor of the map with a given value.
3085
    explicit IterableValueMap(const Graph& graph,
3086
                              const Value& value = Value())
3087
      : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
3088
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3089
        lace(it);
3090
      }
3091
    }
3092

	
3093
  protected:
3094

	
3095
    void unlace(const Key& key) {
3096
      typename Parent::Value& node = Parent::operator[](key);
3097
      if (node.prev != INVALID) {
3098
        Parent::operator[](node.prev).next = node.next;
3099
      } else {
3100
        if (node.next != INVALID) {
3101
          _first[node.value] = node.next;
3102
        } else {
3103
          _first.erase(node.value);
3104
        }
3105
      }
3106
      if (node.next != INVALID) {
3107
        Parent::operator[](node.next).prev = node.prev;
3108
      }
3109
    }
3110

	
3111
    void lace(const Key& key) {
3112
      typename Parent::Value& node = Parent::operator[](key);
3113
      typename std::map<Value, Key>::iterator it = _first.find(node.value);
3114
      if (it == _first.end()) {
3115
        node.prev = node.next = INVALID;
3116
        _first.insert(std::make_pair(node.value, key));
3117
      } else {
3118
        node.prev = INVALID;
3119
        node.next = it->second;
3120
        if (node.next != INVALID) {
3121
          Parent::operator[](node.next).prev = key;
3122
        }
3123
        it->second = key;
3124
      }
3125
    }
3126

	
3127
  public:
3128

	
3129
    /// \brief Forward iterator for values.
3130
    ///
3131
    /// This iterator is an STL compatible forward
3132
    /// iterator on the values of the map. The values can
3133
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
3134
    class ValueIt
3135
      : public std::iterator<std::forward_iterator_tag, Value> {
3136
      friend class IterableValueMap;
3137
    private:
3138
      ValueIt(typename std::map<Value, Key>::const_iterator _it)
3139
        : it(_it) {}
3140
    public:
3141

	
3142
      /// Constructor
3143
      ValueIt() {}
3144

	
3145
      /// \e
3146
      ValueIt& operator++() { ++it; return *this; }
3147
      /// \e
3148
      ValueIt operator++(int) {
3149
        ValueIt tmp(*this);
3150
        operator++();
3151
        return tmp;
3152
      }
3153

	
3154
      /// \e
3155
      const Value& operator*() const { return it->first; }
3156
      /// \e
3157
      const Value* operator->() const { return &(it->first); }
3158

	
3159
      /// \e
3160
      bool operator==(ValueIt jt) const { return it == jt.it; }
3161
      /// \e
3162
      bool operator!=(ValueIt jt) const { return it != jt.it; }
3163

	
3164
    private:
3165
      typename std::map<Value, Key>::const_iterator it;
3166
    };
3167

	
3168
    /// \brief Returns an iterator to the first value.
3169
    ///
3170
    /// Returns an STL compatible iterator to the
3171
    /// first value of the map. The values of the
3172
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3173
    /// range.
3174
    ValueIt beginValue() const {
3175
      return ValueIt(_first.begin());
3176
    }
3177

	
3178
    /// \brief Returns an iterator after the last value.
3179
    ///
3180
    /// Returns an STL compatible iterator after the
3181
    /// last value of the map. The values of the
3182
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3183
    /// range.
3184
    ValueIt endValue() const {
3185
      return ValueIt(_first.end());
3186
    }
3187

	
3188
    /// \brief Set operation of the map.
3189
    ///
3190
    /// Set operation of the map.
3191
    void set(const Key& key, const Value& value) {
3192
      unlace(key);
3193
      Parent::operator[](key).value = value;
3194
      lace(key);
3195
    }
3196

	
3197
    /// \brief Const subscript operator of the map.
3198
    ///
3199
    /// Const subscript operator of the map.
3200
    const Value& operator[](const Key& key) const {
3201
      return Parent::operator[](key).value;
3202
    }
3203

	
3204
    /// \brief Iterator for the keys with the same value.
3205
    ///
3206
    /// Iterator for the keys with the same value. It works
3207
    /// like a graph item iterator, it can be converted to
3208
    /// the item type of the map, incremented with \c ++ operator, and
3209
    /// if the iterator leaves the last valid item, it will be equal to
3210
    /// \c INVALID.
3211
    class ItemIt : public Key {
3212
    public:
3213
      typedef Key Parent;
3214

	
3215
      /// \brief Invalid constructor \& conversion.
3216
      ///
3217
      /// This constructor initializes the iterator to be invalid.
3218
      /// \sa Invalid for more details.
3219
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
3220

	
3221
      /// \brief Creates an iterator with a value.
3222
      ///
3223
      /// Creates an iterator with a value. It iterates on the
3224
      /// keys which have the given value.
3225
      /// \param map The IterableValueMap
3226
      /// \param value The value
3227
      ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
3228
        typename std::map<Value, Key>::const_iterator it =
3229
          map._first.find(value);
3230
        if (it == map._first.end()) {
3231
          Parent::operator=(INVALID);
3232
        } else {
3233
          Parent::operator=(it->second);
3234
        }
3235
      }
3236

	
3237
      /// \brief Increment operator.
3238
      ///
3239
      /// Increment Operator.
3240
      ItemIt& operator++() {
3241
        Parent::operator=(_map->IterableValueMap::Parent::
3242
                          operator[](static_cast<Parent&>(*this)).next);
3243
        return *this;
3244
      }
3245

	
3246

	
3247
    private:
3248
      const IterableValueMap* _map;
3249
    };
3250

	
3251
  protected:
3252

	
3253
    virtual void add(const Key& key) {
3254
      Parent::add(key);
3255
      unlace(key);
3256
    }
3257

	
3258
    virtual void add(const std::vector<Key>& keys) {
3259
      Parent::add(keys);
3260
      for (int i = 0; i < int(keys.size()); ++i) {
3261
        lace(keys[i]);
3262
      }
3263
    }
3264

	
3265
    virtual void erase(const Key& key) {
3266
      unlace(key);
3267
      Parent::erase(key);
3268
    }
3269

	
3270
    virtual void erase(const std::vector<Key>& keys) {
3271
      for (int i = 0; i < int(keys.size()); ++i) {
3272
        unlace(keys[i]);
3273
      }
3274
      Parent::erase(keys);
3275
    }
3276

	
3277
    virtual void build() {
3278
      Parent::build();
3279
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3280
        lace(it);
3281
      }
3282
    }
3283

	
3284
    virtual void clear() {
3285
      _first.clear();
3286
      Parent::clear();
3287
    }
3288

	
3289
  private:
3290
    std::map<Value, Key> _first;
3291
  };
3292

	
3293
  /// \brief Map of the source nodes of arcs in a digraph.
3294
  ///
3295
  /// SourceMap provides access for the source node of each arc in a digraph,
3296
  /// which is returned by the \c source() function of the digraph.
3297
  /// \tparam GR The digraph type.
2264 3298
  /// \see TargetMap
2265
  template <typename Digraph>
3299
  template <typename GR>
2266 3300
  class SourceMap {
2267 3301
  public:
2268 3302

	
2269
    typedef typename Digraph::Node Value;
2270
    typedef typename Digraph::Arc Key;
3303
    /// The key type (the \c Arc type of the digraph).
3304
    typedef typename GR::Arc Key;
3305
    /// The value type (the \c Node type of the digraph).
3306
    typedef typename GR::Node Value;
2271 3307

	
2272 3308
    /// \brief Constructor
2273 3309
    ///
2274
    /// Constructor
3310
    /// Constructor.
2275 3311
    /// \param digraph The digraph that the map belongs to.
2276
    explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {}
2277

	
2278
    /// \brief The subscript operator.
3312
    explicit SourceMap(const GR& digraph) : _graph(digraph) {}
3313

	
3314
    /// \brief Returns the source node of the given arc.
2279 3315
    ///
2280
    /// The subscript operator.
2281
    /// \param arc The arc
2282
    /// \return The source of the arc
3316
    /// Returns the source node of the given arc.
2283 3317
    Value operator[](const Key& arc) const {
2284
      return _digraph.source(arc);
3318
      return _graph.source(arc);
2285 3319
    }
2286 3320

	
2287 3321
  private:
2288
    const Digraph& _digraph;
3322
    const GR& _graph;
2289 3323
  };
2290 3324

	
2291 3325
  /// \brief Returns a \c SourceMap class.
2292 3326
  ///
2293 3327
  /// This function just returns an \c SourceMap class.
2294 3328
  /// \relates SourceMap
2295
  template <typename Digraph>
2296
  inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
2297
    return SourceMap<Digraph>(digraph);
3329
  template <typename GR>
3330
  inline SourceMap<GR> sourceMap(const GR& graph) {
3331
    return SourceMap<GR>(graph);
2298 3332
  }
2299 3333

	
2300
  /// \brief Returns the target of the given arc.
3334
  /// \brief Map of the target nodes of arcs in a digraph.
2301 3335
  ///
2302
  /// The TargetMap gives back the target Node of the given arc.
3336
  /// TargetMap provides access for the target node of each arc in a digraph,
3337
  /// which is returned by the \c target() function of the digraph.
3338
  /// \tparam GR The digraph type.
2303 3339
  /// \see SourceMap
2304
  template <typename Digraph>
3340
  template <typename GR>
2305 3341
  class TargetMap {
2306 3342
  public:
2307 3343

	
2308
    typedef typename Digraph::Node Value;
2309
    typedef typename Digraph::Arc Key;
3344
    /// The key type (the \c Arc type of the digraph).
3345
    typedef typename GR::Arc Key;
3346
    /// The value type (the \c Node type of the digraph).
3347
    typedef typename GR::Node Value;
2310 3348

	
2311 3349
    /// \brief Constructor
2312 3350
    ///
2313
    /// Constructor
3351
    /// Constructor.
2314 3352
    /// \param digraph The digraph that the map belongs to.
2315
    explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {}
2316

	
2317
    /// \brief The subscript operator.
3353
    explicit TargetMap(const GR& digraph) : _graph(digraph) {}
3354

	
3355
    /// \brief Returns the target node of the given arc.
2318 3356
    ///
2319
    /// The subscript operator.
2320
    /// \param e The arc
2321
    /// \return The target of the arc
3357
    /// Returns the target node of the given arc.
2322 3358
    Value operator[](const Key& e) const {
2323
      return _digraph.target(e);
3359
      return _graph.target(e);
2324 3360
    }
2325 3361

	
2326 3362
  private:
2327
    const Digraph& _digraph;
3363
    const GR& _graph;
2328 3364
  };
2329 3365

	
2330 3366
  /// \brief Returns a \c TargetMap class.
2331 3367
  ///
2332 3368
  /// This function just returns a \c TargetMap class.
2333 3369
  /// \relates TargetMap
2334
  template <typename Digraph>
2335
  inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
2336
    return TargetMap<Digraph>(digraph);
3370
  template <typename GR>
3371
  inline TargetMap<GR> targetMap(const GR& graph) {
3372
    return TargetMap<GR>(graph);
2337 3373
  }
2338 3374

	
2339
  /// \brief Returns the "forward" directed arc view of an edge.
3375
  /// \brief Map of the "forward" directed arc view of edges in a graph.
2340 3376
  ///
2341
  /// Returns the "forward" directed arc view of an edge.
3377
  /// ForwardMap provides access for the "forward" directed arc view of
3378
  /// each edge in a graph, which is returned by the \c direct() function
3379
  /// of the graph with \c true parameter.
3380
  /// \tparam GR The graph type.
2342 3381
  /// \see BackwardMap
2343
  template <typename Graph>
3382
  template <typename GR>
2344 3383
  class ForwardMap {
2345 3384
  public:
2346 3385

	
2347
    typedef typename Graph::Arc Value;
2348
    typedef typename Graph::Edge Key;
3386
    /// The key type (the \c Edge type of the digraph).
3387
    typedef typename GR::Edge Key;
3388
    /// The value type (the \c Arc type of the digraph).
3389
    typedef typename GR::Arc Value;
2349 3390

	
2350 3391
    /// \brief Constructor
2351 3392
    ///
2352
    /// Constructor
3393
    /// Constructor.
2353 3394
    /// \param graph The graph that the map belongs to.
2354
    explicit ForwardMap(const Graph& graph) : _graph(graph) {}
2355

	
2356
    /// \brief The subscript operator.
3395
    explicit ForwardMap(const GR& graph) : _graph(graph) {}
3396

	
3397
    /// \brief Returns the "forward" directed arc view of the given edge.
2357 3398
    ///
2358
    /// The subscript operator.
2359
    /// \param key An edge
2360
    /// \return The "forward" directed arc view of edge
3399
    /// Returns the "forward" directed arc view of the given edge.
2361 3400
    Value operator[](const Key& key) const {
2362 3401
      return _graph.direct(key, true);
2363 3402
    }
2364 3403

	
2365 3404
  private:
2366
    const Graph& _graph;
3405
    const GR& _graph;
2367 3406
  };
2368 3407

	
2369 3408
  /// \brief Returns a \c ForwardMap class.
2370 3409
  ///
2371 3410
  /// This function just returns an \c ForwardMap class.
2372 3411
  /// \relates ForwardMap
2373
  template <typename Graph>
2374
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
2375
    return ForwardMap<Graph>(graph);
3412
  template <typename GR>
3413
  inline ForwardMap<GR> forwardMap(const GR& graph) {
3414
    return ForwardMap<GR>(graph);
2376 3415
  }
2377 3416

	
2378
  /// \brief Returns the "backward" directed arc view of an edge.
3417
  /// \brief Map of the "backward" directed arc view of edges in a graph.
2379 3418
  ///
2380
  /// Returns the "backward" directed arc view of an edge.
3419
  /// BackwardMap provides access for the "backward" directed arc view of
3420
  /// each edge in a graph, which is returned by the \c direct() function
3421
  /// of the graph with \c false parameter.
3422
  /// \tparam GR The graph type.
2381 3423
  /// \see ForwardMap
2382
  template <typename Graph>
3424
  template <typename GR>
2383 3425
  class BackwardMap {
2384 3426
  public:
2385 3427

	
2386
    typedef typename Graph::Arc Value;
2387
    typedef typename Graph::Edge Key;
3428
    /// The key type (the \c Edge type of the digraph).
3429
    typedef typename GR::Edge Key;
3430
    /// The value type (the \c Arc type of the digraph).
3431
    typedef typename GR::Arc Value;
2388 3432

	
2389 3433
    /// \brief Constructor
2390 3434
    ///
2391
    /// Constructor
3435
    /// Constructor.
2392 3436
    /// \param graph The graph that the map belongs to.
2393
    explicit BackwardMap(const Graph& graph) : _graph(graph) {}
2394

	
2395
    /// \brief The subscript operator.
3437
    explicit BackwardMap(const GR& graph) : _graph(graph) {}
3438

	
3439
    /// \brief Returns the "backward" directed arc view of the given edge.
2396 3440
    ///
2397
    /// The subscript operator.
2398
    /// \param key An edge
2399
    /// \return The "backward" directed arc view of edge
3441
    /// Returns the "backward" directed arc view of the given edge.
2400 3442
    Value operator[](const Key& key) const {
2401 3443
      return _graph.direct(key, false);
2402 3444
    }
2403 3445

	
2404 3446
  private:
2405
    const Graph& _graph;
3447
    const GR& _graph;
2406 3448
  };
2407 3449

	
2408 3450
  /// \brief Returns a \c BackwardMap class
2409 3451

	
2410 3452
  /// This function just returns a \c BackwardMap class.
2411 3453
  /// \relates BackwardMap
2412
  template <typename Graph>
2413
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
2414
    return BackwardMap<Graph>(graph);
3454
  template <typename GR>
3455
  inline BackwardMap<GR> backwardMap(const GR& graph) {
3456
    return BackwardMap<GR>(graph);
2415 3457
  }
2416 3458

	
2417
  /// \brief Potential difference map
2418
  ///
2419
  /// If there is an potential map on the nodes then we
2420
  /// can get an arc map as we get the substraction of the
2421
  /// values of the target and source.
2422
  template <typename Digraph, typename NodeMap>
2423
  class PotentialDifferenceMap {
2424
  public:
2425
    typedef typename Digraph::Arc Key;
2426
    typedef typename NodeMap::Value Value;
2427

	
2428
    /// \brief Constructor
2429
    ///
2430
    /// Contructor of the map
2431
    explicit PotentialDifferenceMap(const Digraph& digraph,
2432
                                    const NodeMap& potential)
2433
      : _digraph(digraph), _potential(potential) {}
2434

	
2435
    /// \brief Const subscription operator
2436
    ///
2437
    /// Const subscription operator
2438
    Value operator[](const Key& arc) const {
2439
      return _potential[_digraph.target(arc)] -
2440
        _potential[_digraph.source(arc)];
2441
    }
2442

	
2443
  private:
2444
    const Digraph& _digraph;
2445
    const NodeMap& _potential;
2446
  };
2447

	
2448
  /// \brief Returns a PotentialDifferenceMap.
2449
  ///
2450
  /// This function just returns a PotentialDifferenceMap.
2451
  /// \relates PotentialDifferenceMap
2452
  template <typename Digraph, typename NodeMap>
2453
  PotentialDifferenceMap<Digraph, NodeMap>
2454
  potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
2455
    return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential);
2456
  }
2457

	
2458
  /// \brief Map of the node in-degrees.
3459
  /// \brief Map of the in-degrees of nodes in a digraph.
2459 3460
  ///
2460 3461
  /// This map returns the in-degree of a node. Once it is constructed,
2461
  /// the degrees are stored in a standard NodeMap, so each query is done
3462
  /// the degrees are stored in a standard \c NodeMap, so each query is done
2462 3463
  /// in constant time. On the other hand, the values are updated automatically
2463 3464
  /// whenever the digraph changes.
2464 3465
  ///
2465
  /// \warning Besides addNode() and addArc(), a digraph structure may provide
2466
  /// alternative ways to modify the digraph. The correct behavior of InDegMap
2467
  /// is not guarantied if these additional features are used. For example
2468
  /// the functions \ref ListDigraph::changeSource() "changeSource()",
3466
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3467
  /// may provide alternative ways to modify the digraph.
3468
  /// The correct behavior of InDegMap is not guarantied if these additional
3469
  /// features are used. For example the functions
3470
  /// \ref ListDigraph::changeSource() "changeSource()",
2469 3471
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
2470 3472
  /// \ref ListDigraph::reverseArc() "reverseArc()"
2471 3473
  /// of \ref ListDigraph will \e not update the degree values correctly.
2472 3474
  ///
2473 3475
  /// \sa OutDegMap
2474

	
2475
  template <typename _Digraph>
3476
  template <typename GR>
2476 3477
  class InDegMap
2477
    : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
3478
    : protected ItemSetTraits<GR, typename GR::Arc>
2478 3479
      ::ItemNotifier::ObserverBase {
2479 3480

	
2480 3481
  public:
2481 3482

	
2482
    typedef _Digraph Digraph;
3483
    /// The graph type of InDegMap
3484
    typedef GR Graph;
3485
    typedef GR Digraph;
3486
    /// The key type
3487
    typedef typename Digraph::Node Key;
3488
    /// The value type
2483 3489
    typedef int Value;
2484
    typedef typename Digraph::Node Key;
2485 3490

	
2486 3491
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2487 3492
    ::ItemNotifier::ObserverBase Parent;
2488 3493

	
2489 3494
  private:
2490 3495

	
2491 3496
    class AutoNodeMap
2492 3497
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2493 3498
    public:
2494 3499

	
2495 3500
      typedef typename ItemSetTraits<Digraph, Key>::
2496 3501
      template Map<int>::Type Parent;
2497 3502

	
2498 3503
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2499 3504

	
2500 3505
      virtual void add(const Key& key) {
2501 3506
        Parent::add(key);
2502 3507
        Parent::set(key, 0);
2503 3508
      }
2504 3509

	
2505 3510
      virtual void add(const std::vector<Key>& keys) {
2506 3511
        Parent::add(keys);
2507 3512
        for (int i = 0; i < int(keys.size()); ++i) {
2508 3513
          Parent::set(keys[i], 0);
2509 3514
        }
2510 3515
      }
2511 3516

	
2512 3517
      virtual void build() {
2513 3518
        Parent::build();
2514 3519
        Key it;
2515 3520
        typename Parent::Notifier* nf = Parent::notifier();
2516 3521
        for (nf->first(it); it != INVALID; nf->next(it)) {
2517 3522
          Parent::set(it, 0);
2518 3523
        }
2519 3524
      }
2520 3525
    };
2521 3526

	
2522 3527
  public:
2523 3528

	
2524 3529
    /// \brief Constructor.
2525 3530
    ///
2526
    /// Constructor for creating in-degree map.
2527
    explicit InDegMap(const Digraph& digraph)
2528
      : _digraph(digraph), _deg(digraph) {
3531
    /// Constructor for creating an in-degree map.
3532
    explicit InDegMap(const Digraph& graph)
3533
      : _digraph(graph), _deg(graph) {
2529 3534
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2530 3535

	
2531 3536
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2532 3537
        _deg[it] = countInArcs(_digraph, it);
2533 3538
      }
2534 3539
    }
2535 3540

	
3541
    /// \brief Gives back the in-degree of a Node.
3542
    ///
2536 3543
    /// Gives back the in-degree of a Node.
2537 3544
    int operator[](const Key& key) const {
2538 3545
      return _deg[key];
2539 3546
    }
2540 3547

	
2541 3548
  protected:
2542 3549

	
2543 3550
    typedef typename Digraph::Arc Arc;
2544 3551

	
2545 3552
    virtual void add(const Arc& arc) {
2546 3553
      ++_deg[_digraph.target(arc)];
2547 3554
    }
2548 3555

	
2549 3556
    virtual void add(const std::vector<Arc>& arcs) {
2550 3557
      for (int i = 0; i < int(arcs.size()); ++i) {
2551 3558
        ++_deg[_digraph.target(arcs[i])];
2552 3559
      }
2553 3560
    }
2554 3561

	
2555 3562
    virtual void erase(const Arc& arc) {
2556 3563
      --_deg[_digraph.target(arc)];
2557 3564
    }
2558 3565

	
2559 3566
    virtual void erase(const std::vector<Arc>& arcs) {
2560 3567
      for (int i = 0; i < int(arcs.size()); ++i) {
2561 3568
        --_deg[_digraph.target(arcs[i])];
2562 3569
      }
2563 3570
    }
2564 3571

	
2565 3572
    virtual void build() {
2566 3573
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2567 3574
        _deg[it] = countInArcs(_digraph, it);
2568 3575
      }
2569 3576
    }
2570 3577

	
2571 3578
    virtual void clear() {
2572 3579
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2573 3580
        _deg[it] = 0;
2574 3581
      }
2575 3582
    }
2576 3583
  private:
2577 3584

	
2578 3585
    const Digraph& _digraph;
2579 3586
    AutoNodeMap _deg;
2580 3587
  };
2581 3588

	
2582
  /// \brief Map of the node out-degrees.
3589
  /// \brief Map of the out-degrees of nodes in a digraph.
2583 3590
  ///
2584 3591
  /// This map returns the out-degree of a node. Once it is constructed,
2585
  /// the degrees are stored in a standard NodeMap, so each query is done
3592
  /// the degrees are stored in a standard \c NodeMap, so each query is done
2586 3593
  /// in constant time. On the other hand, the values are updated automatically
2587 3594
  /// whenever the digraph changes.
2588 3595
  ///
2589
  /// \warning Besides addNode() and addArc(), a digraph structure may provide
2590
  /// alternative ways to modify the digraph. The correct behavior of OutDegMap
2591
  /// is not guarantied if these additional features are used. For example
2592
  /// the functions \ref ListDigraph::changeSource() "changeSource()",
3596
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
3597
  /// may provide alternative ways to modify the digraph.
3598
  /// The correct behavior of OutDegMap is not guarantied if these additional
3599
  /// features are used. For example the functions
3600
  /// \ref ListDigraph::changeSource() "changeSource()",
2593 3601
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
2594 3602
  /// \ref ListDigraph::reverseArc() "reverseArc()"
2595 3603
  /// of \ref ListDigraph will \e not update the degree values correctly.
2596 3604
  ///
2597 3605
  /// \sa InDegMap
2598

	
2599
  template <typename _Digraph>
3606
  template <typename GR>
2600 3607
  class OutDegMap
2601
    : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
3608
    : protected ItemSetTraits<GR, typename GR::Arc>
2602 3609
      ::ItemNotifier::ObserverBase {
2603 3610

	
2604 3611
  public:
2605 3612

	
2606
    typedef _Digraph Digraph;
3613
    /// The graph type of OutDegMap
3614
    typedef GR Graph;
3615
    typedef GR Digraph;
3616
    /// The key type
3617
    typedef typename Digraph::Node Key;
3618
    /// The value type
2607 3619
    typedef int Value;
2608
    typedef typename Digraph::Node Key;
2609 3620

	
2610 3621
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2611 3622
    ::ItemNotifier::ObserverBase Parent;
2612 3623

	
2613 3624
  private:
2614 3625

	
2615 3626
    class AutoNodeMap
2616 3627
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2617 3628
    public:
2618 3629

	
2619 3630
      typedef typename ItemSetTraits<Digraph, Key>::
2620 3631
      template Map<int>::Type Parent;
2621 3632

	
2622 3633
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2623 3634

	
2624 3635
      virtual void add(const Key& key) {
2625 3636
        Parent::add(key);
2626 3637
        Parent::set(key, 0);
2627 3638
      }
2628 3639
      virtual void add(const std::vector<Key>& keys) {
2629 3640
        Parent::add(keys);
2630 3641
        for (int i = 0; i < int(keys.size()); ++i) {
2631 3642
          Parent::set(keys[i], 0);
2632 3643
        }
2633 3644
      }
2634 3645
      virtual void build() {
2635 3646
        Parent::build();
2636 3647
        Key it;
2637 3648
        typename Parent::Notifier* nf = Parent::notifier();
2638 3649
        for (nf->first(it); it != INVALID; nf->next(it)) {
2639 3650
          Parent::set(it, 0);
2640 3651
        }
2641 3652
      }
2642 3653
    };
2643 3654

	
2644 3655
  public:
2645 3656

	
2646 3657
    /// \brief Constructor.
2647 3658
    ///
2648
    /// Constructor for creating out-degree map.
2649
    explicit OutDegMap(const Digraph& digraph)
2650
      : _digraph(digraph), _deg(digraph) {
3659
    /// Constructor for creating an out-degree map.
3660
    explicit OutDegMap(const Digraph& graph)
3661
      : _digraph(graph), _deg(graph) {
2651 3662
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2652 3663

	
2653 3664
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2654 3665
        _deg[it] = countOutArcs(_digraph, it);
2655 3666
      }
2656 3667
    }
2657 3668

	
3669
    /// \brief Gives back the out-degree of a Node.
3670
    ///
2658 3671
    /// Gives back the out-degree of a Node.
2659 3672
    int operator[](const Key& key) const {
2660 3673
      return _deg[key];
2661 3674
    }
2662 3675

	
2663 3676
  protected:
2664 3677

	
2665 3678
    typedef typename Digraph::Arc Arc;
2666 3679

	
2667 3680
    virtual void add(const Arc& arc) {
2668 3681
      ++_deg[_digraph.source(arc)];
2669 3682
    }
2670 3683

	
2671 3684
    virtual void add(const std::vector<Arc>& arcs) {
2672 3685
      for (int i = 0; i < int(arcs.size()); ++i) {
2673 3686
        ++_deg[_digraph.source(arcs[i])];
2674 3687
      }
2675 3688
    }
2676 3689

	
2677 3690
    virtual void erase(const Arc& arc) {
2678 3691
      --_deg[_digraph.source(arc)];
2679 3692
    }
2680 3693

	
2681 3694
    virtual void erase(const std::vector<Arc>& arcs) {
2682 3695
      for (int i = 0; i < int(arcs.size()); ++i) {
2683 3696
        --_deg[_digraph.source(arcs[i])];
2684 3697
      }
2685 3698
    }
2686 3699

	
2687 3700
    virtual void build() {
2688 3701
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2689 3702
        _deg[it] = countOutArcs(_digraph, it);
2690 3703
      }
2691 3704
    }
2692 3705

	
2693 3706
    virtual void clear() {
2694 3707
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2695 3708
        _deg[it] = 0;
2696 3709
      }
2697 3710
    }
2698 3711
  private:
2699 3712

	
2700 3713
    const Digraph& _digraph;
2701 3714
    AutoNodeMap _deg;
2702 3715
  };
2703 3716

	
3717
  /// \brief Potential difference map
3718
  ///
3719
  /// PotentialDifferenceMap returns the difference between the potentials of
3720
  /// the source and target nodes of each arc in a digraph, i.e. it returns
3721
  /// \code
3722
  ///   potential[gr.target(arc)] - potential[gr.source(arc)].
3723
  /// \endcode
3724
  /// \tparam GR The digraph type.
3725
  /// \tparam POT A node map storing the potentials.
3726
  template <typename GR, typename POT>
3727
  class PotentialDifferenceMap {
3728
  public:
3729
    /// Key type
3730
    typedef typename GR::Arc Key;
3731
    /// Value type
3732
    typedef typename POT::Value Value;
3733

	
3734
    /// \brief Constructor
3735
    ///
3736
    /// Contructor of the map.
3737
    explicit PotentialDifferenceMap(const GR& gr,
3738
                                    const POT& potential)
3739
      : _digraph(gr), _potential(potential) {}
3740

	
3741
    /// \brief Returns the potential difference for the given arc.
3742
    ///
3743
    /// Returns the potential difference for the given arc, i.e.
3744
    /// \code
3745
    ///   potential[gr.target(arc)] - potential[gr.source(arc)].
3746
    /// \endcode
3747
    Value operator[](const Key& arc) const {
3748
      return _potential[_digraph.target(arc)] -
3749
        _potential[_digraph.source(arc)];
3750
    }
3751

	
3752
  private:
3753
    const GR& _digraph;
3754
    const POT& _potential;
3755
  };
3756

	
3757
  /// \brief Returns a PotentialDifferenceMap.
3758
  ///
3759
  /// This function just returns a PotentialDifferenceMap.
3760
  /// \relates PotentialDifferenceMap
3761
  template <typename GR, typename POT>
3762
  PotentialDifferenceMap<GR, POT>
3763
  potentialDifferenceMap(const GR& gr, const POT& potential) {
3764
    return PotentialDifferenceMap<GR, POT>(gr, potential);
3765
  }
3766

	
2704 3767
  /// @}
2705 3768
}
2706 3769

	
2707 3770
#endif // LEMON_MAPS_H
Ignore white space 6 line context
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-2009
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_MAX_MATCHING_H
20 20
#define LEMON_MAX_MATCHING_H
21 21

	
22 22
#include <vector>
23 23
#include <queue>
24 24
#include <set>
25 25
#include <limits>
26 26

	
27 27
#include <lemon/core.h>
28 28
#include <lemon/unionfind.h>
29 29
#include <lemon/bin_heap.h>
30 30
#include <lemon/maps.h>
31 31

	
32 32
///\ingroup matching
33 33
///\file
34 34
///\brief Maximum matching algorithms in general graphs.
35 35

	
36 36
namespace lemon {
37 37

	
38 38
  /// \ingroup matching
39 39
  ///
40
  /// \brief Edmonds' alternating forest maximum matching algorithm.
40
  /// \brief Maximum cardinality matching in general graphs
41 41
  ///
42
  /// This class implements Edmonds' alternating forest matching
43
  /// algorithm. The algorithm can be started from an arbitrary initial
44
  /// matching (the default is the empty one)
42
  /// This class implements Edmonds' alternating forest matching algorithm
43
  /// for finding a maximum cardinality matching in a general undirected graph.
44
  /// It can be started from an arbitrary initial matching 
45
  /// (the default is the empty one).
45 46
  ///
46 47
  /// The dual solution of the problem is a map of the nodes to
47
  /// MaxMatching::Status, having values \c EVEN/D, \c ODD/A and \c
48
  /// MATCHED/C showing the Gallai-Edmonds decomposition of the
49
  /// graph. The nodes in \c EVEN/D induce a graph with
50
  /// factor-critical components, the nodes in \c ODD/A form the
51
  /// barrier, and the nodes in \c MATCHED/C induce a graph having a
52
  /// perfect matching. The number of the factor-critical components
48
  /// \ref MaxMatching::Status "Status", having values \c EVEN (or \c D),
49
  /// \c ODD (or \c A) and \c MATCHED (or \c C) defining the Gallai-Edmonds
50
  /// decomposition of the graph. The nodes in \c EVEN/D induce a subgraph
51
  /// with factor-critical components, the nodes in \c ODD/A form the
52
  /// canonical barrier, and the nodes in \c MATCHED/C induce a graph having
53
  /// a perfect matching. The number of the factor-critical components
53 54
  /// minus the number of barrier nodes is a lower bound on the
54 55
  /// unmatched nodes, and the matching is optimal if and only if this bound is
55
  /// tight. This decomposition can be attained by calling \c
56
  /// decomposition() after running the algorithm.
56
  /// tight. This decomposition can be obtained using \ref status() or
57
  /// \ref statusMap() after running the algorithm.
57 58
  ///
58
  /// \param _Graph The graph type the algorithm runs on.
59
  template <typename _Graph>
59
  /// \tparam GR The undirected graph type the algorithm runs on.
60
  template <typename GR>
60 61
  class MaxMatching {
61 62
  public:
62 63

	
63
    typedef _Graph Graph;
64
    /// The graph type of the algorithm
65
    typedef GR Graph;
66
    /// The type of the matching map
64 67
    typedef typename Graph::template NodeMap<typename Graph::Arc>
65 68
    MatchingMap;
66 69

	
67
    ///\brief Indicates the Gallai-Edmonds decomposition of the graph.
70
    ///\brief Status constants for Gallai-Edmonds decomposition.
68 71
    ///
69
    ///Indicates the Gallai-Edmonds decomposition of the graph. The
70
    ///nodes with Status \c EVEN/D induce a graph with factor-critical
71
    ///components, the nodes in \c ODD/A form the canonical barrier,
72
    ///and the nodes in \c MATCHED/C induce a graph having a perfect
73
    ///matching.
72
    ///These constants are used for indicating the Gallai-Edmonds 
73
    ///decomposition of a graph. The nodes with status \c EVEN (or \c D)
74
    ///induce a subgraph with factor-critical components, the nodes with
75
    ///status \c ODD (or \c A) form the canonical barrier, and the nodes
76
    ///with status \c MATCHED (or \c C) induce a subgraph having a 
77
    ///perfect matching.
74 78
    enum Status {
75
      EVEN = 1, D = 1, MATCHED = 0, C = 0, ODD = -1, A = -1, UNMATCHED = -2
79
      EVEN = 1,       ///< = 1. (\c D is an alias for \c EVEN.)
80
      D = 1,
81
      MATCHED = 0,    ///< = 0. (\c C is an alias for \c MATCHED.)
82
      C = 0,
83
      ODD = -1,       ///< = -1. (\c A is an alias for \c ODD.)
84
      A = -1,
85
      UNMATCHED = -2  ///< = -2.
76 86
    };
77 87

	
88
    /// The type of the status map
78 89
    typedef typename Graph::template NodeMap<Status> StatusMap;
79 90

	
80 91
  private:
81 92

	
82 93
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
83 94

	
84 95
    typedef UnionFindEnum<IntNodeMap> BlossomSet;
85 96
    typedef ExtendFindEnum<IntNodeMap> TreeSet;
86 97
    typedef RangeMap<Node> NodeIntMap;
87 98
    typedef MatchingMap EarMap;
88 99
    typedef std::vector<Node> NodeQueue;
89 100

	
90 101
    const Graph& _graph;
91 102
    MatchingMap* _matching;
92 103
    StatusMap* _status;
93 104

	
94 105
    EarMap* _ear;
95 106

	
96 107
    IntNodeMap* _blossom_set_index;
97 108
    BlossomSet* _blossom_set;
98 109
    NodeIntMap* _blossom_rep;
99 110

	
100 111
    IntNodeMap* _tree_set_index;
101 112
    TreeSet* _tree_set;
102 113

	
103 114
    NodeQueue _node_queue;
104 115
    int _process, _postpone, _last;
105 116

	
106 117
    int _node_num;
107 118

	
108 119
  private:
109 120

	
110 121
    void createStructures() {
111 122
      _node_num = countNodes(_graph);
112 123
      if (!_matching) {
113 124
        _matching = new MatchingMap(_graph);
114 125
      }
115 126
      if (!_status) {
116 127
        _status = new StatusMap(_graph);
117 128
      }
118 129
      if (!_ear) {
119 130
        _ear = new EarMap(_graph);
120 131
      }
121 132
      if (!_blossom_set) {
122 133
        _blossom_set_index = new IntNodeMap(_graph);
123 134
        _blossom_set = new BlossomSet(*_blossom_set_index);
124 135
      }
125 136
      if (!_blossom_rep) {
126 137
        _blossom_rep = new NodeIntMap(_node_num);
127 138
      }
128 139
      if (!_tree_set) {
129 140
        _tree_set_index = new IntNodeMap(_graph);
130 141
        _tree_set = new TreeSet(*_tree_set_index);
131 142
      }
132 143
      _node_queue.resize(_node_num);
133 144
    }
134 145

	
135 146
    void destroyStructures() {
136 147
      if (_matching) {
137 148
        delete _matching;
138 149
      }
139 150
      if (_status) {
140 151
        delete _status;
141 152
      }
142 153
      if (_ear) {
143 154
        delete _ear;
144 155
      }
145 156
      if (_blossom_set) {
146 157
        delete _blossom_set;
147 158
        delete _blossom_set_index;
148 159
      }
149 160
      if (_blossom_rep) {
150 161
        delete _blossom_rep;
151 162
      }
152 163
      if (_tree_set) {
153 164
        delete _tree_set_index;
154 165
        delete _tree_set;
155 166
      }
156 167
    }
157 168

	
158 169
    void processDense(const Node& n) {
159 170
      _process = _postpone = _last = 0;
160 171
      _node_queue[_last++] = n;
161 172

	
162 173
      while (_process != _last) {
163 174
        Node u = _node_queue[_process++];
164 175
        for (OutArcIt a(_graph, u); a != INVALID; ++a) {
165 176
          Node v = _graph.target(a);
166 177
          if ((*_status)[v] == MATCHED) {
167 178
            extendOnArc(a);
168 179
          } else if ((*_status)[v] == UNMATCHED) {
169 180
            augmentOnArc(a);
170 181
            return;
171 182
          }
172 183
        }
173 184
      }
... ...
@@ -189,576 +200,610 @@
189 200
            for (OutArcIt b(_graph, w); b != INVALID; ++b) {
190 201
              Node x = _graph.target(b);
191 202
              if ((*_status)[x] == MATCHED) {
192 203
                extendOnArc(b);
193 204
              } else if ((*_status)[x] == UNMATCHED) {
194 205
                augmentOnArc(b);
195 206
                return;
196 207
              }
197 208
            }
198 209
          }
199 210
        }
200 211
      }
201 212
    }
202 213

	
203 214
    void processSparse(const Node& n) {
204 215
      _process = _last = 0;
205 216
      _node_queue[_last++] = n;
206 217
      while (_process != _last) {
207 218
        Node u = _node_queue[_process++];
208 219
        for (OutArcIt a(_graph, u); a != INVALID; ++a) {
209 220
          Node v = _graph.target(a);
210 221

	
211 222
          if ((*_status)[v] == EVEN) {
212 223
            if (_blossom_set->find(u) != _blossom_set->find(v)) {
213 224
              shrinkOnEdge(a);
214 225
            }
215 226
          } else if ((*_status)[v] == MATCHED) {
216 227
            extendOnArc(a);
217 228
          } else if ((*_status)[v] == UNMATCHED) {
218 229
            augmentOnArc(a);
219 230
            return;
220 231
          }
221 232
        }
222 233
      }
223 234
    }
224 235

	
225 236
    void shrinkOnEdge(const Edge& e) {
226 237
      Node nca = INVALID;
227 238

	
228 239
      {
229 240
        std::set<Node> left_set, right_set;
230 241

	
231 242
        Node left = (*_blossom_rep)[_blossom_set->find(_graph.u(e))];
232 243
        left_set.insert(left);
233 244

	
234 245
        Node right = (*_blossom_rep)[_blossom_set->find(_graph.v(e))];
235 246
        right_set.insert(right);
236 247

	
237 248
        while (true) {
238 249
          if ((*_matching)[left] == INVALID) break;
239 250
          left = _graph.target((*_matching)[left]);
240 251
          left = (*_blossom_rep)[_blossom_set->
241 252
                                 find(_graph.target((*_ear)[left]))];
242 253
          if (right_set.find(left) != right_set.end()) {
243 254
            nca = left;
244 255
            break;
245 256
          }
246 257
          left_set.insert(left);
247 258

	
248 259
          if ((*_matching)[right] == INVALID) break;
249 260
          right = _graph.target((*_matching)[right]);
250 261
          right = (*_blossom_rep)[_blossom_set->
251 262
                                  find(_graph.target((*_ear)[right]))];
252 263
          if (left_set.find(right) != left_set.end()) {
253 264
            nca = right;
254 265
            break;
255 266
          }
256 267
          right_set.insert(right);
257 268
        }
258 269

	
259 270
        if (nca == INVALID) {
260 271
          if ((*_matching)[left] == INVALID) {
261 272
            nca = right;
262 273
            while (left_set.find(nca) == left_set.end()) {
263 274
              nca = _graph.target((*_matching)[nca]);
264 275
              nca =(*_blossom_rep)[_blossom_set->
265 276
                                   find(_graph.target((*_ear)[nca]))];
266 277
            }
267 278
          } else {
268 279
            nca = left;
269 280
            while (right_set.find(nca) == right_set.end()) {
270 281
              nca = _graph.target((*_matching)[nca]);
271 282
              nca = (*_blossom_rep)[_blossom_set->
272 283
                                   find(_graph.target((*_ear)[nca]))];
273 284
            }
274 285
          }
275 286
        }
276 287
      }
277 288

	
278 289
      {
279 290

	
280 291
        Node node = _graph.u(e);
281 292
        Arc arc = _graph.direct(e, true);
282 293
        Node base = (*_blossom_rep)[_blossom_set->find(node)];
283 294

	
284 295
        while (base != nca) {
285
          _ear->set(node, arc);
296
          (*_ear)[node] = arc;
286 297

	
287 298
          Node n = node;
288 299
          while (n != base) {
289 300
            n = _graph.target((*_matching)[n]);
290 301
            Arc a = (*_ear)[n];
291 302
            n = _graph.target(a);
292
            _ear->set(n, _graph.oppositeArc(a));
303
            (*_ear)[n] = _graph.oppositeArc(a);
293 304
          }
294 305
          node = _graph.target((*_matching)[base]);
295 306
          _tree_set->erase(base);
296 307
          _tree_set->erase(node);
297 308
          _blossom_set->insert(node, _blossom_set->find(base));
298
          _status->set(node, EVEN);
309
          (*_status)[node] = EVEN;
299 310
          _node_queue[_last++] = node;
300 311
          arc = _graph.oppositeArc((*_ear)[node]);
301 312
          node = _graph.target((*_ear)[node]);
302 313
          base = (*_blossom_rep)[_blossom_set->find(node)];
303 314
          _blossom_set->join(_graph.target(arc), base);
304 315
        }
305 316
      }
306 317

	
307
      _blossom_rep->set(_blossom_set->find(nca), nca);
318
      (*_blossom_rep)[_blossom_set->find(nca)] = nca;
308 319

	
309 320
      {
310 321

	
311 322
        Node node = _graph.v(e);
312 323
        Arc arc = _graph.direct(e, false);
313 324
        Node base = (*_blossom_rep)[_blossom_set->find(node)];
314 325

	
315 326
        while (base != nca) {
316
          _ear->set(node, arc);
327
          (*_ear)[node] = arc;
317 328

	
318 329
          Node n = node;
319 330
          while (n != base) {
320 331
            n = _graph.target((*_matching)[n]);
321 332
            Arc a = (*_ear)[n];
322 333
            n = _graph.target(a);
323
            _ear->set(n, _graph.oppositeArc(a));
334
            (*_ear)[n] = _graph.oppositeArc(a);
324 335
          }
325 336
          node = _graph.target((*_matching)[base]);
326 337
          _tree_set->erase(base);
327 338
          _tree_set->erase(node);
328 339
          _blossom_set->insert(node, _blossom_set->find(base));
329
          _status->set(node, EVEN);
340
          (*_status)[node] = EVEN;
330 341
          _node_queue[_last++] = node;
331 342
          arc = _graph.oppositeArc((*_ear)[node]);
332 343
          node = _graph.target((*_ear)[node]);
333 344
          base = (*_blossom_rep)[_blossom_set->find(node)];
334 345
          _blossom_set->join(_graph.target(arc), base);
335 346
        }
336 347
      }
337 348

	
338
      _blossom_rep->set(_blossom_set->find(nca), nca);
349
      (*_blossom_rep)[_blossom_set->find(nca)] = nca;
339 350
    }
340 351

	
341

	
342

	
343 352
    void extendOnArc(const Arc& a) {
344 353
      Node base = _graph.source(a);
345 354
      Node odd = _graph.target(a);
346 355

	
347
      _ear->set(odd, _graph.oppositeArc(a));
356
      (*_ear)[odd] = _graph.oppositeArc(a);
348 357
      Node even = _graph.target((*_matching)[odd]);
349
      _blossom_rep->set(_blossom_set->insert(even), even);
350
      _status->set(odd, ODD);
351
      _status->set(even, EVEN);
358
      (*_blossom_rep)[_blossom_set->insert(even)] = even;
359
      (*_status)[odd] = ODD;
360
      (*_status)[even] = EVEN;
352 361
      int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(base)]);
353 362
      _tree_set->insert(odd, tree);
354 363
      _tree_set->insert(even, tree);
355 364
      _node_queue[_last++] = even;
356 365

	
357 366
    }
358 367

	
359 368
    void augmentOnArc(const Arc& a) {
360 369
      Node even = _graph.source(a);
361 370
      Node odd = _graph.target(a);
362 371

	
363 372
      int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(even)]);
364 373

	
365
      _matching->set(odd, _graph.oppositeArc(a));
366
      _status->set(odd, MATCHED);
374
      (*_matching)[odd] = _graph.oppositeArc(a);
375
      (*_status)[odd] = MATCHED;
367 376

	
368 377
      Arc arc = (*_matching)[even];
369
      _matching->set(even, a);
378
      (*_matching)[even] = a;
370 379

	
371 380
      while (arc != INVALID) {
372 381
        odd = _graph.target(arc);
373 382
        arc = (*_ear)[odd];
374 383
        even = _graph.target(arc);
375
        _matching->set(odd, arc);
384
        (*_matching)[odd] = arc;
376 385
        arc = (*_matching)[even];
377
        _matching->set(even, _graph.oppositeArc((*_matching)[odd]));
386
        (*_matching)[even] = _graph.oppositeArc((*_matching)[odd]);
378 387
      }
379 388

	
380 389
      for (typename TreeSet::ItemIt it(*_tree_set, tree);
381 390
           it != INVALID; ++it) {
382 391
        if ((*_status)[it] == ODD) {
383
          _status->set(it, MATCHED);
392
          (*_status)[it] = MATCHED;
384 393
        } else {
385 394
          int blossom = _blossom_set->find(it);
386 395
          for (typename BlossomSet::ItemIt jt(*_blossom_set, blossom);
387 396
               jt != INVALID; ++jt) {
388
            _status->set(jt, MATCHED);
397
            (*_status)[jt] = MATCHED;
389 398
          }
390 399
          _blossom_set->eraseClass(blossom);
391 400
        }
392 401
      }
393 402
      _tree_set->eraseClass(tree);
394 403

	
395 404
    }
396 405

	
397 406
  public:
398 407

	
399 408
    /// \brief Constructor
400 409
    ///
401 410
    /// Constructor.
402 411
    MaxMatching(const Graph& graph)
403 412
      : _graph(graph), _matching(0), _status(0), _ear(0),
404 413
        _blossom_set_index(0), _blossom_set(0), _blossom_rep(0),
405 414
        _tree_set_index(0), _tree_set(0) {}
406 415

	
407 416
    ~MaxMatching() {
408 417
      destroyStructures();
409 418
    }
410 419

	
411
    /// \name Execution control
420
    /// \name Execution Control
412 421
    /// The simplest way to execute the algorithm is to use the
413
    /// \c run() member function.
414
    /// \n
415

	
416
    /// If you need better control on the execution, you must call
417
    /// \ref init(), \ref greedyInit() or \ref matchingInit()
418
    /// functions first, then you can start the algorithm with the \ref
419
    /// startSparse() or startDense() functions.
422
    /// \c run() member function.\n
423
    /// If you need better control on the execution, you have to call
424
    /// one of the functions \ref init(), \ref greedyInit() or
425
    /// \ref matchingInit() first, then you can start the algorithm with
426
    /// \ref startSparse() or \ref startDense().
420 427

	
421 428
    ///@{
422 429

	
423
    /// \brief Sets the actual matching to the empty matching.
430
    /// \brief Set the initial matching to the empty matching.
424 431
    ///
425
    /// Sets the actual matching to the empty matching.
426
    ///
432
    /// This function sets the initial matching to the empty matching.
427 433
    void init() {
428 434
      createStructures();
429 435
      for(NodeIt n(_graph); n != INVALID; ++n) {
430
        _matching->set(n, INVALID);
431
        _status->set(n, UNMATCHED);
436
        (*_matching)[n] = INVALID;
437
        (*_status)[n] = UNMATCHED;
432 438
      }
433 439
    }
434 440

	
435
    ///\brief Finds an initial matching in a greedy way
441
    /// \brief Find an initial matching in a greedy way.
436 442
    ///
437
    ///It finds an initial matching in a greedy way.
443
    /// This function finds an initial matching in a greedy way.
438 444
    void greedyInit() {
439 445
      createStructures();
440 446
      for (NodeIt n(_graph); n != INVALID; ++n) {
441
        _matching->set(n, INVALID);
442
        _status->set(n, UNMATCHED);
447
        (*_matching)[n] = INVALID;
448
        (*_status)[n] = UNMATCHED;
443 449
      }
444 450
      for (NodeIt n(_graph); n != INVALID; ++n) {
445 451
        if ((*_matching)[n] == INVALID) {
446 452
          for (OutArcIt a(_graph, n); a != INVALID ; ++a) {
447 453
            Node v = _graph.target(a);
448 454
            if ((*_matching)[v] == INVALID && v != n) {
449
              _matching->set(n, a);
450
              _status->set(n, MATCHED);
451
              _matching->set(v, _graph.oppositeArc(a));
452
              _status->set(v, MATCHED);
455
              (*_matching)[n] = a;
456
              (*_status)[n] = MATCHED;
457
              (*_matching)[v] = _graph.oppositeArc(a);
458
              (*_status)[v] = MATCHED;
453 459
              break;
454 460
            }
455 461
          }
456 462
        }
457 463
      }
458 464
    }
459 465

	
460 466

	
461
    /// \brief Initialize the matching from a map containing.
467
    /// \brief Initialize the matching from a map.
462 468
    ///
463
    /// Initialize the matching from a \c bool valued \c Edge map. This
464
    /// map must have the property that there are no two incident edges
465
    /// with true value, ie. it contains a matching.
466
    /// \return %True if the map contains a matching.
469
    /// This function initializes the matching from a \c bool valued edge
470
    /// map. This map should have the property that there are no two incident
471
    /// edges with \c true value, i.e. it really contains a matching.
472
    /// \return \c true if the map contains a matching.
467 473
    template <typename MatchingMap>
468 474
    bool matchingInit(const MatchingMap& matching) {
469 475
      createStructures();
470 476

	
471 477
      for (NodeIt n(_graph); n != INVALID; ++n) {
472
        _matching->set(n, INVALID);
473
        _status->set(n, UNMATCHED);
478
        (*_matching)[n] = INVALID;
479
        (*_status)[n] = UNMATCHED;
474 480
      }
475 481
      for(EdgeIt e(_graph); e!=INVALID; ++e) {
476 482
        if (matching[e]) {
477 483

	
478 484
          Node u = _graph.u(e);
479 485
          if ((*_matching)[u] != INVALID) return false;
480
          _matching->set(u, _graph.direct(e, true));
481
          _status->set(u, MATCHED);
486
          (*_matching)[u] = _graph.direct(e, true);
487
          (*_status)[u] = MATCHED;
482 488

	
483 489
          Node v = _graph.v(e);
484 490
          if ((*_matching)[v] != INVALID) return false;
485
          _matching->set(v, _graph.direct(e, false));
486
          _status->set(v, MATCHED);
491
          (*_matching)[v] = _graph.direct(e, false);
492
          (*_status)[v] = MATCHED;
487 493
        }
488 494
      }
489 495
      return true;
490 496
    }
491 497

	
492
    /// \brief Starts Edmonds' algorithm
498
    /// \brief Start Edmonds' algorithm
493 499
    ///
494
    /// If runs the original Edmonds' algorithm.
500
    /// This function runs the original Edmonds' algorithm.
501
    ///
502
    /// \pre \ref init(), \ref greedyInit() or \ref matchingInit() must be
503
    /// called before using this function.
495 504
    void startSparse() {
496 505
      for(NodeIt n(_graph); n != INVALID; ++n) {
497 506
        if ((*_status)[n] == UNMATCHED) {
498 507
          (*_blossom_rep)[_blossom_set->insert(n)] = n;
499 508
          _tree_set->insert(n);
500
          _status->set(n, EVEN);
509
          (*_status)[n] = EVEN;
501 510
          processSparse(n);
502 511
        }
503 512
      }
504 513
    }
505 514

	
506
    /// \brief Starts Edmonds' algorithm.
515
    /// \brief Start Edmonds' algorithm with a heuristic improvement 
516
    /// for dense graphs
507 517
    ///
508
    /// It runs Edmonds' algorithm with a heuristic of postponing
518
    /// This function runs Edmonds' algorithm with a heuristic of postponing
509 519
    /// shrinks, therefore resulting in a faster algorithm for dense graphs.
520
    ///
521
    /// \pre \ref init(), \ref greedyInit() or \ref matchingInit() must be
522
    /// called before using this function.
510 523
    void startDense() {
511 524
      for(NodeIt n(_graph); n != INVALID; ++n) {
512 525
        if ((*_status)[n] == UNMATCHED) {
513 526
          (*_blossom_rep)[_blossom_set->insert(n)] = n;
514 527
          _tree_set->insert(n);
515
          _status->set(n, EVEN);
528
          (*_status)[n] = EVEN;
516 529
          processDense(n);
517 530
        }
518 531
      }
519 532
    }
520 533

	
521 534

	
522
    /// \brief Runs Edmonds' algorithm
535
    /// \brief Run Edmonds' algorithm
523 536
    ///
524
    /// Runs Edmonds' algorithm for sparse graphs (<tt>m<2*n</tt>)
525
    /// or Edmonds' algorithm with a heuristic of
526
    /// postponing shrinks for dense graphs.
537
    /// This function runs Edmonds' algorithm. An additional heuristic of 
538
    /// postponing shrinks is used for relatively dense graphs 
539
    /// (for which <tt>m>=2*n</tt> holds).
527 540
    void run() {
528 541
      if (countEdges(_graph) < 2 * countNodes(_graph)) {
529 542
        greedyInit();
530 543
        startSparse();
531 544
      } else {
532 545
        init();
533 546
        startDense();
534 547
      }
535 548
    }
536 549

	
537 550
    /// @}
538 551

	
539
    /// \name Primal solution
540
    /// Functions to get the primal solution, ie. the matching.
552
    /// \name Primal Solution
553
    /// Functions to get the primal solution, i.e. the maximum matching.
541 554

	
542 555
    /// @{
543 556

	
544
    ///\brief Returns the size of the current matching.
557
    /// \brief Return the size (cardinality) of the matching.
545 558
    ///
546
    ///Returns the size of the current matching. After \ref
547
    ///run() it returns the size of the maximum matching in the graph.
559
    /// This function returns the size (cardinality) of the current matching. 
560
    /// After run() it returns the size of the maximum matching in the graph.
548 561
    int matchingSize() const {
549 562
      int size = 0;
550 563
      for (NodeIt n(_graph); n != INVALID; ++n) {
551 564
        if ((*_matching)[n] != INVALID) {
552 565
          ++size;
553 566
        }
554 567
      }
555 568
      return size / 2;
556 569
    }
557 570

	
558
    /// \brief Returns true when the edge is in the matching.
571
    /// \brief Return \c true if the given edge is in the matching.
559 572
    ///
560
    /// Returns true when the edge is in the matching.
573
    /// This function returns \c true if the given edge is in the current 
574
    /// matching.
561 575
    bool matching(const Edge& edge) const {
562 576
      return edge == (*_matching)[_graph.u(edge)];
563 577
    }
564 578

	
565
    /// \brief Returns the matching edge incident to the given node.
579
    /// \brief Return the matching arc (or edge) incident to the given node.
566 580
    ///
567
    /// Returns the matching edge of a \c node in the actual matching or
568
    /// INVALID if the \c node is not covered by the actual matching.
581
    /// This function returns the matching arc (or edge) incident to the
582
    /// given node in the current matching or \c INVALID if the node is 
583
    /// not covered by the matching.
569 584
    Arc matching(const Node& n) const {
570 585
      return (*_matching)[n];
571 586
    }
572 587

	
573
    ///\brief Returns the mate of a node in the actual matching.
588
    /// \brief Return a const reference to the matching map.
574 589
    ///
575
    ///Returns the mate of a \c node in the actual matching or
576
    ///INVALID if the \c node is not covered by the actual matching.
590
    /// This function returns a const reference to a node map that stores
591
    /// the matching arc (or edge) incident to each node.
592
    const MatchingMap& matchingMap() const {
593
      return *_matching;
594
    }
595

	
596
    /// \brief Return the mate of the given node.
597
    ///
598
    /// This function returns the mate of the given node in the current 
599
    /// matching or \c INVALID if the node is not covered by the matching.
577 600
    Node mate(const Node& n) const {
578 601
      return (*_matching)[n] != INVALID ?
579 602
        _graph.target((*_matching)[n]) : INVALID;
580 603
    }
581 604

	
582 605
    /// @}
583 606

	
584
    /// \name Dual solution
585
    /// Functions to get the dual solution, ie. the decomposition.
607
    /// \name Dual Solution
608
    /// Functions to get the dual solution, i.e. the Gallai-Edmonds 
609
    /// decomposition.
586 610

	
587 611
    /// @{
588 612

	
589
    /// \brief Returns the class of the node in the Edmonds-Gallai
613
    /// \brief Return the status of the given node in the Edmonds-Gallai
590 614
    /// decomposition.
591 615
    ///
592
    /// Returns the class of the node in the Edmonds-Gallai
593
    /// decomposition.
594
    Status decomposition(const Node& n) const {
616
    /// This function returns the \ref Status "status" of the given node
617
    /// in the Edmonds-Gallai decomposition.
618
    Status status(const Node& n) const {
595 619
      return (*_status)[n];
596 620
    }
597 621

	
598
    /// \brief Returns true when the node is in the barrier.
622
    /// \brief Return a const reference to the status map, which stores
623
    /// the Edmonds-Gallai decomposition.
599 624
    ///
600
    /// Returns true when the node is in the barrier.
625
    /// This function returns a const reference to a node map that stores the
626
    /// \ref Status "status" of each node in the Edmonds-Gallai decomposition.
627
    const StatusMap& statusMap() const {
628
      return *_status;
629
    }
630

	
631
    /// \brief Return \c true if the given node is in the barrier.
632
    ///
633
    /// This function returns \c true if the given node is in the barrier.
601 634
    bool barrier(const Node& n) const {
602 635
      return (*_status)[n] == ODD;
603 636
    }
604 637

	
605 638
    /// @}
606 639

	
607 640
  };
608 641

	
609 642
  /// \ingroup matching
610 643
  ///
611 644
  /// \brief Weighted matching in general graphs
612 645
  ///
613 646
  /// This class provides an efficient implementation of Edmond's
614 647
  /// maximum weighted matching algorithm. The implementation is based
615 648
  /// on extensive use of priority queues and provides
616
  /// \f$O(nm\log(n))\f$ time complexity.
649
  /// \f$O(nm\log n)\f$ time complexity.
617 650
  ///
618
  /// The maximum weighted matching problem is to find undirected
619
  /// edges in the graph with maximum overall weight and no two of
620
  /// them shares their ends. The problem can be formulated with the
621
  /// following linear program.
651
  /// The maximum weighted matching problem is to find a subset of the 
652
  /// edges in an undirected graph with maximum overall weight for which 
653
  /// each node has at most one incident edge.
654
  /// It can be formulated with the following linear program.
622 655
  /// \f[ \sum_{e \in \delta(u)}x_e \le 1 \quad \forall u\in V\f]
623 656
  /** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2}
624 657
      \quad \forall B\in\mathcal{O}\f] */
625 658
  /// \f[x_e \ge 0\quad \forall e\in E\f]
626 659
  /// \f[\max \sum_{e\in E}x_ew_e\f]
627 660
  /// where \f$\delta(X)\f$ is the set of edges incident to a node in
628 661
  /// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in
629 662
  /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality
630 663
  /// subsets of the nodes.
631 664
  ///
632 665
  /// The algorithm calculates an optimal matching and a proof of the
633 666
  /// optimality. The solution of the dual problem can be used to check
634 667
  /// the result of the algorithm. The dual linear problem is the
668
  /// following.
635 669
  /** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}
636 670
      z_B \ge w_{uv} \quad \forall uv\in E\f] */
637 671
  /// \f[y_u \ge 0 \quad \forall u \in V\f]
638 672
  /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f]
639 673
  /** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}
640 674
      \frac{\vert B \vert - 1}{2}z_B\f] */
641 675
  ///
642
  /// The algorithm can be executed with \c run() or the \c init() and
643
  /// then the \c start() member functions. After it the matching can
644
  /// be asked with \c matching() or mate() functions. The dual
645
  /// solution can be get with \c nodeValue(), \c blossomNum() and \c
646
  /// blossomValue() members and \ref MaxWeightedMatching::BlossomIt
647
  /// "BlossomIt" nested class, which is able to iterate on the nodes
648
  /// of a blossom. If the value type is integral then the dual
649
  /// solution is multiplied by \ref MaxWeightedMatching::dualScale "4".
650
  template <typename _Graph,
651
            typename _WeightMap = typename _Graph::template EdgeMap<int> >
676
  /// The algorithm can be executed with the run() function. 
677
  /// After it the matching (the primal solution) and the dual solution
678
  /// can be obtained using the query functions and the 
679
  /// \ref MaxWeightedMatching::BlossomIt "BlossomIt" nested class, 
680
  /// which is able to iterate on the nodes of a blossom. 
681
  /// If the value type is integer, then the dual solution is multiplied
682
  /// by \ref MaxWeightedMatching::dualScale "4".
683
  ///
684
  /// \tparam GR The undirected graph type the algorithm runs on.
685
  /// \tparam WM The type edge weight map. The default type is 
686
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
687
#ifdef DOXYGEN
688
  template <typename GR, typename WM>
689
#else
690
  template <typename GR,
691
            typename WM = typename GR::template EdgeMap<int> >
692
#endif
652 693
  class MaxWeightedMatching {
653 694
  public:
654 695

	
655
    typedef _Graph Graph;
656
    typedef _WeightMap WeightMap;
696
    /// The graph type of the algorithm
697
    typedef GR Graph;
698
    /// The type of the edge weight map
699
    typedef WM WeightMap;
700
    /// The value type of the edge weights
657 701
    typedef typename WeightMap::Value Value;
658 702

	
703
    /// The type of the matching map
704
    typedef typename Graph::template NodeMap<typename Graph::Arc>
705
    MatchingMap;
706

	
659 707
    /// \brief Scaling factor for dual solution
660 708
    ///
661
    /// Scaling factor for dual solution, it is equal to 4 or 1
709
    /// Scaling factor for dual solution. It is equal to 4 or 1
662 710
    /// according to the value type.
663 711
    static const int dualScale =
664 712
      std::numeric_limits<Value>::is_integer ? 4 : 1;
665 713

	
666
    typedef typename Graph::template NodeMap<typename Graph::Arc>
667
    MatchingMap;
668

	
669 714
  private:
670 715

	
671 716
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
672 717

	
673 718
    typedef typename Graph::template NodeMap<Value> NodePotential;
674 719
    typedef std::vector<Node> BlossomNodeList;
675 720

	
676 721
    struct BlossomVariable {
677 722
      int begin, end;
678 723
      Value value;
679 724

	
680 725
      BlossomVariable(int _begin, int _end, Value _value)
681 726
        : begin(_begin), end(_end), value(_value) {}
682 727

	
683 728
    };
684 729

	
685 730
    typedef std::vector<BlossomVariable> BlossomPotential;
686 731

	
687 732
    const Graph& _graph;
688 733
    const WeightMap& _weight;
689 734

	
690 735
    MatchingMap* _matching;
691 736

	
692 737
    NodePotential* _node_potential;
693 738

	
694 739
    BlossomPotential _blossom_potential;
695 740
    BlossomNodeList _blossom_node_list;
696 741

	
697 742
    int _node_num;
698 743
    int _blossom_num;
699 744

	
700 745
    typedef RangeMap<int> IntIntMap;
701 746

	
702 747
    enum Status {
703 748
      EVEN = -1, MATCHED = 0, ODD = 1, UNMATCHED = -2
704 749
    };
705 750

	
706 751
    typedef HeapUnionFind<Value, IntNodeMap> BlossomSet;
707 752
    struct BlossomData {
708 753
      int tree;
709 754
      Status status;
710 755
      Arc pred, next;
711 756
      Value pot, offset;
712 757
      Node base;
713 758
    };
714 759

	
715 760
    IntNodeMap *_blossom_index;
716 761
    BlossomSet *_blossom_set;
717 762
    RangeMap<BlossomData>* _blossom_data;
718 763

	
719 764
    IntNodeMap *_node_index;
720 765
    IntArcMap *_node_heap_index;
721 766

	
722 767
    struct NodeData {
723 768

	
724 769
      NodeData(IntArcMap& node_heap_index)
725 770
        : heap(node_heap_index) {}
726 771

	
727 772
      int blossom;
728 773
      Value pot;
729 774
      BinHeap<Value, IntArcMap> heap;
730 775
      std::map<int, Arc> heap_index;
731 776

	
732 777
      int tree;
733 778
    };
734 779

	
735 780
    RangeMap<NodeData>* _node_data;
736 781

	
737 782
    typedef ExtendFindEnum<IntIntMap> TreeSet;
738 783

	
739 784
    IntIntMap *_tree_set_index;
740 785
    TreeSet *_tree_set;
741 786

	
742 787
    IntNodeMap *_delta1_index;
743 788
    BinHeap<Value, IntNodeMap> *_delta1;
744 789

	
745 790
    IntIntMap *_delta2_index;
746 791
    BinHeap<Value, IntIntMap> *_delta2;
747 792

	
748 793
    IntEdgeMap *_delta3_index;
749 794
    BinHeap<Value, IntEdgeMap> *_delta3;
750 795

	
751 796
    IntIntMap *_delta4_index;
752 797
    BinHeap<Value, IntIntMap> *_delta4;
753 798

	
754 799
    Value _delta_sum;
755 800

	
756 801
    void createStructures() {
757 802
      _node_num = countNodes(_graph);
758 803
      _blossom_num = _node_num * 3 / 2;
759 804

	
760 805
      if (!_matching) {
761 806
        _matching = new MatchingMap(_graph);
762 807
      }
763 808
      if (!_node_potential) {
764 809
        _node_potential = new NodePotential(_graph);
... ...
@@ -1452,653 +1497,707 @@
1452 1497
      for (int i = 0; i < int(subblossoms.size()); ++i) {
1453 1498
        if (subblossoms[i] == b) ib = i;
1454 1499
        if (subblossoms[i] == d) id = i;
1455 1500

	
1456 1501
        (*_blossom_data)[subblossoms[i]].offset = offset;
1457 1502
        if (!_blossom_set->trivial(subblossoms[i])) {
1458 1503
          (*_blossom_data)[subblossoms[i]].pot -= 2 * offset;
1459 1504
        }
1460 1505
        if (_blossom_set->classPrio(subblossoms[i]) !=
1461 1506
            std::numeric_limits<Value>::max()) {
1462 1507
          _delta2->push(subblossoms[i],
1463 1508
                        _blossom_set->classPrio(subblossoms[i]) -
1464 1509
                        (*_blossom_data)[subblossoms[i]].offset);
1465 1510
        }
1466 1511
      }
1467 1512

	
1468 1513
      if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) {
1469 1514
        for (int i = (id + 1) % subblossoms.size();
1470 1515
             i != ib; i = (i + 2) % subblossoms.size()) {
1471 1516
          int sb = subblossoms[i];
1472 1517
          int tb = subblossoms[(i + 1) % subblossoms.size()];
1473 1518
          (*_blossom_data)[sb].next =
1474 1519
            _graph.oppositeArc((*_blossom_data)[tb].next);
1475 1520
        }
1476 1521

	
1477 1522
        for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) {
1478 1523
          int sb = subblossoms[i];
1479 1524
          int tb = subblossoms[(i + 1) % subblossoms.size()];
1480 1525
          int ub = subblossoms[(i + 2) % subblossoms.size()];
1481 1526

	
1482 1527
          (*_blossom_data)[sb].status = ODD;
1483 1528
          matchedToOdd(sb);
1484 1529
          _tree_set->insert(sb, tree);
1485 1530
          (*_blossom_data)[sb].pred = pred;
1486 1531
          (*_blossom_data)[sb].next =
1487 1532
                           _graph.oppositeArc((*_blossom_data)[tb].next);
1488 1533

	
1489 1534
          pred = (*_blossom_data)[ub].next;
1490 1535

	
1491 1536
          (*_blossom_data)[tb].status = EVEN;
1492 1537
          matchedToEven(tb, tree);
1493 1538
          _tree_set->insert(tb, tree);
1494 1539
          (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next;
1495 1540
        }
1496 1541

	
1497 1542
        (*_blossom_data)[subblossoms[id]].status = ODD;
1498 1543
        matchedToOdd(subblossoms[id]);
1499 1544
        _tree_set->insert(subblossoms[id], tree);
1500 1545
        (*_blossom_data)[subblossoms[id]].next = next;
1501 1546
        (*_blossom_data)[subblossoms[id]].pred = pred;
1502 1547

	
1503 1548
      } else {
1504 1549

	
1505 1550
        for (int i = (ib + 1) % subblossoms.size();
1506 1551
             i != id; i = (i + 2) % subblossoms.size()) {
1507 1552
          int sb = subblossoms[i];
1508 1553
          int tb = subblossoms[(i + 1) % subblossoms.size()];
1509 1554
          (*_blossom_data)[sb].next =
1510 1555
            _graph.oppositeArc((*_blossom_data)[tb].next);
1511 1556
        }
1512 1557

	
1513 1558
        for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) {
1514 1559
          int sb = subblossoms[i];
1515 1560
          int tb = subblossoms[(i + 1) % subblossoms.size()];
1516 1561
          int ub = subblossoms[(i + 2) % subblossoms.size()];
1517 1562

	
1518 1563
          (*_blossom_data)[sb].status = ODD;
1519 1564
          matchedToOdd(sb);
1520 1565
          _tree_set->insert(sb, tree);
1521 1566
          (*_blossom_data)[sb].next = next;
1522 1567
          (*_blossom_data)[sb].pred =
1523 1568
            _graph.oppositeArc((*_blossom_data)[tb].next);
1524 1569

	
1525 1570
          (*_blossom_data)[tb].status = EVEN;
1526 1571
          matchedToEven(tb, tree);
1527 1572
          _tree_set->insert(tb, tree);
1528 1573
          (*_blossom_data)[tb].pred =
1529 1574
            (*_blossom_data)[tb].next =
1530 1575
            _graph.oppositeArc((*_blossom_data)[ub].next);
1531 1576
          next = (*_blossom_data)[ub].next;
1532 1577
        }
1533 1578

	
1534 1579
        (*_blossom_data)[subblossoms[ib]].status = ODD;
1535 1580
        matchedToOdd(subblossoms[ib]);
1536 1581
        _tree_set->insert(subblossoms[ib], tree);
1537 1582
        (*_blossom_data)[subblossoms[ib]].next = next;
1538 1583
        (*_blossom_data)[subblossoms[ib]].pred = pred;
1539 1584
      }
1540 1585
      _tree_set->erase(blossom);
1541 1586
    }
1542 1587

	
1543 1588
    void extractBlossom(int blossom, const Node& base, const Arc& matching) {
1544 1589
      if (_blossom_set->trivial(blossom)) {
1545 1590
        int bi = (*_node_index)[base];
1546 1591
        Value pot = (*_node_data)[bi].pot;
1547 1592

	
1548
        _matching->set(base, matching);
1593
        (*_matching)[base] = matching;
1549 1594
        _blossom_node_list.push_back(base);
1550
        _node_potential->set(base, pot);
1595
        (*_node_potential)[base] = pot;
1551 1596
      } else {
1552 1597

	
1553 1598
        Value pot = (*_blossom_data)[blossom].pot;
1554 1599
        int bn = _blossom_node_list.size();
1555 1600

	
1556 1601
        std::vector<int> subblossoms;
1557 1602
        _blossom_set->split(blossom, std::back_inserter(subblossoms));
1558 1603
        int b = _blossom_set->find(base);
1559 1604
        int ib = -1;
1560 1605
        for (int i = 0; i < int(subblossoms.size()); ++i) {
1561 1606
          if (subblossoms[i] == b) { ib = i; break; }
1562 1607
        }
1563 1608

	
1564 1609
        for (int i = 1; i < int(subblossoms.size()); i += 2) {
1565 1610
          int sb = subblossoms[(ib + i) % subblossoms.size()];
1566 1611
          int tb = subblossoms[(ib + i + 1) % subblossoms.size()];
1567 1612

	
1568 1613
          Arc m = (*_blossom_data)[tb].next;
1569 1614
          extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m));
1570 1615
          extractBlossom(tb, _graph.source(m), m);
1571 1616
        }
1572 1617
        extractBlossom(subblossoms[ib], base, matching);
1573 1618

	
1574 1619
        int en = _blossom_node_list.size();
1575 1620

	
1576 1621
        _blossom_potential.push_back(BlossomVariable(bn, en, pot));
1577 1622
      }
1578 1623
    }
1579 1624

	
1580 1625
    void extractMatching() {
1581 1626
      std::vector<int> blossoms;
1582 1627
      for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) {
1583 1628
        blossoms.push_back(c);
1584 1629
      }
1585 1630

	
1586 1631
      for (int i = 0; i < int(blossoms.size()); ++i) {
1587 1632
        if ((*_blossom_data)[blossoms[i]].status == MATCHED) {
1588 1633

	
1589 1634
          Value offset = (*_blossom_data)[blossoms[i]].offset;
1590 1635
          (*_blossom_data)[blossoms[i]].pot += 2 * offset;
1591 1636
          for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]);
1592 1637
               n != INVALID; ++n) {
1593 1638
            (*_node_data)[(*_node_index)[n]].pot -= offset;
1594 1639
          }
1595 1640

	
1596 1641
          Arc matching = (*_blossom_data)[blossoms[i]].next;
1597 1642
          Node base = _graph.source(matching);
1598 1643
          extractBlossom(blossoms[i], base, matching);
1599 1644
        } else {
1600 1645
          Node base = (*_blossom_data)[blossoms[i]].base;
1601 1646
          extractBlossom(blossoms[i], base, INVALID);
1602 1647
        }
1603 1648
      }
1604 1649
    }
1605 1650

	
1606 1651
  public:
1607 1652

	
1608 1653
    /// \brief Constructor
1609 1654
    ///
1610 1655
    /// Constructor.
1611 1656
    MaxWeightedMatching(const Graph& graph, const WeightMap& weight)
1612 1657
      : _graph(graph), _weight(weight), _matching(0),
1613 1658
        _node_potential(0), _blossom_potential(), _blossom_node_list(),
1614 1659
        _node_num(0), _blossom_num(0),
1615 1660

	
1616 1661
        _blossom_index(0), _blossom_set(0), _blossom_data(0),
1617 1662
        _node_index(0), _node_heap_index(0), _node_data(0),
1618 1663
        _tree_set_index(0), _tree_set(0),
1619 1664

	
1620 1665
        _delta1_index(0), _delta1(0),
1621 1666
        _delta2_index(0), _delta2(0),
1622 1667
        _delta3_index(0), _delta3(0),
1623 1668
        _delta4_index(0), _delta4(0),
1624 1669

	
1625 1670
        _delta_sum() {}
1626 1671

	
1627 1672
    ~MaxWeightedMatching() {
1628 1673
      destroyStructures();
1629 1674
    }
1630 1675

	
1631
    /// \name Execution control
1676
    /// \name Execution Control
1632 1677
    /// The simplest way to execute the algorithm is to use the
1633
    /// \c run() member function.
1678
    /// \ref run() member function.
1634 1679

	
1635 1680
    ///@{
1636 1681

	
1637 1682
    /// \brief Initialize the algorithm
1638 1683
    ///
1639
    /// Initialize the algorithm
1684
    /// This function initializes the algorithm.
1640 1685
    void init() {
1641 1686
      createStructures();
1642 1687

	
1643 1688
      for (ArcIt e(_graph); e != INVALID; ++e) {
1644
        _node_heap_index->set(e, BinHeap<Value, IntArcMap>::PRE_HEAP);
1689
        (*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
1645 1690
      }
1646 1691
      for (NodeIt n(_graph); n != INVALID; ++n) {
1647
        _delta1_index->set(n, _delta1->PRE_HEAP);
1692
        (*_delta1_index)[n] = _delta1->PRE_HEAP;
1648 1693
      }
1649 1694
      for (EdgeIt e(_graph); e != INVALID; ++e) {
1650
        _delta3_index->set(e, _delta3->PRE_HEAP);
1695
        (*_delta3_index)[e] = _delta3->PRE_HEAP;
1651 1696
      }
1652 1697
      for (int i = 0; i < _blossom_num; ++i) {
1653
        _delta2_index->set(i, _delta2->PRE_HEAP);
1654
        _delta4_index->set(i, _delta4->PRE_HEAP);
1698
        (*_delta2_index)[i] = _delta2->PRE_HEAP;
1699
        (*_delta4_index)[i] = _delta4->PRE_HEAP;
1655 1700
      }
1656 1701

	
1657 1702
      int index = 0;
1658 1703
      for (NodeIt n(_graph); n != INVALID; ++n) {
1659 1704
        Value max = 0;
1660 1705
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
1661 1706
          if (_graph.target(e) == n) continue;
1662 1707
          if ((dualScale * _weight[e]) / 2 > max) {
1663 1708
            max = (dualScale * _weight[e]) / 2;
1664 1709
          }
1665 1710
        }
1666
        _node_index->set(n, index);
1711
        (*_node_index)[n] = index;
1667 1712
        (*_node_data)[index].pot = max;
1668 1713
        _delta1->push(n, max);
1669 1714
        int blossom =
1670 1715
          _blossom_set->insert(n, std::numeric_limits<Value>::max());
1671 1716

	
1672 1717
        _tree_set->insert(blossom);
1673 1718

	
1674 1719
        (*_blossom_data)[blossom].status = EVEN;
1675 1720
        (*_blossom_data)[blossom].pred = INVALID;
1676 1721
        (*_blossom_data)[blossom].next = INVALID;
1677 1722
        (*_blossom_data)[blossom].pot = 0;
1678 1723
        (*_blossom_data)[blossom].offset = 0;
1679 1724
        ++index;
1680 1725
      }
1681 1726
      for (EdgeIt e(_graph); e != INVALID; ++e) {
1682 1727
        int si = (*_node_index)[_graph.u(e)];
1683 1728
        int ti = (*_node_index)[_graph.v(e)];
1684 1729
        if (_graph.u(e) != _graph.v(e)) {
1685 1730
          _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
1686 1731
                            dualScale * _weight[e]) / 2);
1687 1732
        }
1688 1733
      }
1689 1734
    }
1690 1735

	
1691
    /// \brief Starts the algorithm
1736
    /// \brief Start the algorithm
1692 1737
    ///
1693
    /// Starts the algorithm
1738
    /// This function starts the algorithm.
1739
    ///
1740
    /// \pre \ref init() must be called before using this function.
1694 1741
    void start() {
1695 1742
      enum OpType {
1696 1743
        D1, D2, D3, D4
1697 1744
      };
1698 1745

	
1699 1746
      int unmatched = _node_num;
1700 1747
      while (unmatched > 0) {
1701 1748
        Value d1 = !_delta1->empty() ?
1702 1749
          _delta1->prio() : std::numeric_limits<Value>::max();
1703 1750

	
1704 1751
        Value d2 = !_delta2->empty() ?
1705 1752
          _delta2->prio() : std::numeric_limits<Value>::max();
1706 1753

	
1707 1754
        Value d3 = !_delta3->empty() ?
1708 1755
          _delta3->prio() : std::numeric_limits<Value>::max();
1709 1756

	
1710 1757
        Value d4 = !_delta4->empty() ?
1711 1758
          _delta4->prio() : std::numeric_limits<Value>::max();
1712 1759

	
1713 1760
        _delta_sum = d1; OpType ot = D1;
1714 1761
        if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; }
1715 1762
        if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; }
1716 1763
        if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; }
1717 1764

	
1718 1765

	
1719 1766
        switch (ot) {
1720 1767
        case D1:
1721 1768
          {
1722 1769
            Node n = _delta1->top();
1723 1770
            unmatchNode(n);
1724 1771
            --unmatched;
1725 1772
          }
1726 1773
          break;
1727 1774
        case D2:
1728 1775
          {
1729 1776
            int blossom = _delta2->top();
1730 1777
            Node n = _blossom_set->classTop(blossom);
1731 1778
            Arc e = (*_node_data)[(*_node_index)[n]].heap.top();
1732 1779
            extendOnArc(e);
1733 1780
          }
1734 1781
          break;
1735 1782
        case D3:
1736 1783
          {
1737 1784
            Edge e = _delta3->top();
1738 1785

	
1739 1786
            int left_blossom = _blossom_set->find(_graph.u(e));
1740 1787
            int right_blossom = _blossom_set->find(_graph.v(e));
1741 1788

	
1742 1789
            if (left_blossom == right_blossom) {
1743 1790
              _delta3->pop();
1744 1791
            } else {
1745 1792
              int left_tree;
1746 1793
              if ((*_blossom_data)[left_blossom].status == EVEN) {
1747 1794
                left_tree = _tree_set->find(left_blossom);
1748 1795
              } else {
1749 1796
                left_tree = -1;
1750 1797
                ++unmatched;
1751 1798
              }
1752 1799
              int right_tree;
1753 1800
              if ((*_blossom_data)[right_blossom].status == EVEN) {
1754 1801
                right_tree = _tree_set->find(right_blossom);
1755 1802
              } else {
1756 1803
                right_tree = -1;
1757 1804
                ++unmatched;
1758 1805
              }
1759 1806

	
1760 1807
              if (left_tree == right_tree) {
1761 1808
                shrinkOnEdge(e, left_tree);
1762 1809
              } else {
1763 1810
                augmentOnEdge(e);
1764 1811
                unmatched -= 2;
1765 1812
              }
1766 1813
            }
1767 1814
          } break;
1768 1815
        case D4:
1769 1816
          splitBlossom(_delta4->top());
1770 1817
          break;
1771 1818
        }
1772 1819
      }
1773 1820
      extractMatching();
1774 1821
    }
1775 1822

	
1776
    /// \brief Runs %MaxWeightedMatching algorithm.
1823
    /// \brief Run the algorithm.
1777 1824
    ///
1778
    /// This method runs the %MaxWeightedMatching algorithm.
1825
    /// This method runs the \c %MaxWeightedMatching algorithm.
1779 1826
    ///
1780 1827
    /// \note mwm.run() is just a shortcut of the following code.
1781 1828
    /// \code
1782 1829
    ///   mwm.init();
1783 1830
    ///   mwm.start();
1784 1831
    /// \endcode
1785 1832
    void run() {
1786 1833
      init();
1787 1834
      start();
1788 1835
    }
1789 1836

	
1790 1837
    /// @}
1791 1838

	
1792
    /// \name Primal solution
1793
    /// Functions to get the primal solution, ie. the matching.
1839
    /// \name Primal Solution
1840
    /// Functions to get the primal solution, i.e. the maximum weighted 
1841
    /// matching.\n
1842
    /// Either \ref run() or \ref start() function should be called before
1843
    /// using them.
1794 1844

	
1795 1845
    /// @{
1796 1846

	
1797
    /// \brief Returns the weight of the matching.
1847
    /// \brief Return the weight of the matching.
1798 1848
    ///
1799
    /// Returns the weight of the matching.
1800
    Value matchingValue() const {
1849
    /// This function returns the weight of the found matching.
1850
    ///
1851
    /// \pre Either run() or start() must be called before using this function.
1852
    Value matchingWeight() const {
1801 1853
      Value sum = 0;
1802 1854
      for (NodeIt n(_graph); n != INVALID; ++n) {
1803 1855
        if ((*_matching)[n] != INVALID) {
1804 1856
          sum += _weight[(*_matching)[n]];
1805 1857
        }
1806 1858
      }
1807 1859
      return sum /= 2;
1808 1860
    }
1809 1861

	
1810
    /// \brief Returns the cardinality of the matching.
1862
    /// \brief Return the size (cardinality) of the matching.
1811 1863
    ///
1812
    /// Returns the cardinality of the matching.
1864
    /// This function returns the size (cardinality) of the found matching.
1865
    ///
1866
    /// \pre Either run() or start() must be called before using this function.
1813 1867
    int matchingSize() const {
1814 1868
      int num = 0;
1815 1869
      for (NodeIt n(_graph); n != INVALID; ++n) {
1816 1870
        if ((*_matching)[n] != INVALID) {
1817 1871
          ++num;
1818 1872
        }
1819 1873
      }
1820 1874
      return num /= 2;
1821 1875
    }
1822 1876

	
1823
    /// \brief Returns true when the edge is in the matching.
1877
    /// \brief Return \c true if the given edge is in the matching.
1824 1878
    ///
1825
    /// Returns true when the edge is in the matching.
1879
    /// This function returns \c true if the given edge is in the found 
1880
    /// matching.
1881
    ///
1882
    /// \pre Either run() or start() must be called before using this function.
1826 1883
    bool matching(const Edge& edge) const {
1827 1884
      return edge == (*_matching)[_graph.u(edge)];
1828 1885
    }
1829 1886

	
1830
    /// \brief Returns the incident matching arc.
1887
    /// \brief Return the matching arc (or edge) incident to the given node.
1831 1888
    ///
1832
    /// Returns the incident matching arc from given node. If the
1833
    /// node is not matched then it gives back \c INVALID.
1889
    /// This function returns the matching arc (or edge) incident to the
1890
    /// given node in the found matching or \c INVALID if the node is 
1891
    /// not covered by the matching.
1892
    ///
1893
    /// \pre Either run() or start() must be called before using this function.
1834 1894
    Arc matching(const Node& node) const {
1835 1895
      return (*_matching)[node];
1836 1896
    }
1837 1897

	
1838
    /// \brief Returns the mate of the node.
1898
    /// \brief Return a const reference to the matching map.
1839 1899
    ///
1840
    /// Returns the adjancent node in a mathcing arc. If the node is
1841
    /// not matched then it gives back \c INVALID.
1900
    /// This function returns a const reference to a node map that stores
1901
    /// the matching arc (or edge) incident to each node.
1902
    const MatchingMap& matchingMap() const {
1903
      return *_matching;
1904
    }
1905

	
1906
    /// \brief Return the mate of the given node.
1907
    ///
1908
    /// This function returns the mate of the given node in the found 
1909
    /// matching or \c INVALID if the node is not covered by the matching.
1910
    ///
1911
    /// \pre Either run() or start() must be called before using this function.
1842 1912
    Node mate(const Node& node) const {
1843 1913
      return (*_matching)[node] != INVALID ?
1844 1914
        _graph.target((*_matching)[node]) : INVALID;
1845 1915
    }
1846 1916

	
1847 1917
    /// @}
1848 1918

	
1849
    /// \name Dual solution
1850
    /// Functions to get the dual solution.
1919
    /// \name Dual Solution
1920
    /// Functions to get the dual solution.\n
1921
    /// Either \ref run() or \ref start() function should be called before
1922
    /// using them.
1851 1923

	
1852 1924
    /// @{
1853 1925

	
1854
    /// \brief Returns the value of the dual solution.
1926
    /// \brief Return the value of the dual solution.
1855 1927
    ///
1856
    /// Returns the value of the dual solution. It should be equal to
1857
    /// the primal value scaled by \ref dualScale "dual scale".
1928
    /// This function returns the value of the dual solution. 
1929
    /// It should be equal to the primal value scaled by \ref dualScale 
1930
    /// "dual scale".
1931
    ///
1932
    /// \pre Either run() or start() must be called before using this function.
1858 1933
    Value dualValue() const {
1859 1934
      Value sum = 0;
1860 1935
      for (NodeIt n(_graph); n != INVALID; ++n) {
1861 1936
        sum += nodeValue(n);
1862 1937
      }
1863 1938
      for (int i = 0; i < blossomNum(); ++i) {
1864 1939
        sum += blossomValue(i) * (blossomSize(i) / 2);
1865 1940
      }
1866 1941
      return sum;
1867 1942
    }
1868 1943

	
1869
    /// \brief Returns the value of the node.
1944
    /// \brief Return the dual value (potential) of the given node.
1870 1945
    ///
1871
    /// Returns the the value of the node.
1946
    /// This function returns the dual value (potential) of the given node.
1947
    ///
1948
    /// \pre Either run() or start() must be called before using this function.
1872 1949
    Value nodeValue(const Node& n) const {
1873 1950
      return (*_node_potential)[n];
1874 1951
    }
1875 1952

	
1876
    /// \brief Returns the number of the blossoms in the basis.
1953
    /// \brief Return the number of the blossoms in the basis.
1877 1954
    ///
1878
    /// Returns the number of the blossoms in the basis.
1955
    /// This function returns the number of the blossoms in the basis.
1956
    ///
1957
    /// \pre Either run() or start() must be called before using this function.
1879 1958
    /// \see BlossomIt
1880 1959
    int blossomNum() const {
1881 1960
      return _blossom_potential.size();
1882 1961
    }
1883 1962

	
1884

	
1885
    /// \brief Returns the number of the nodes in the blossom.
1963
    /// \brief Return the number of the nodes in the given blossom.
1886 1964
    ///
1887
    /// Returns the number of the nodes in the blossom.
1965
    /// This function returns the number of the nodes in the given blossom.
1966
    ///
1967
    /// \pre Either run() or start() must be called before using this function.
1968
    /// \see BlossomIt
1888 1969
    int blossomSize(int k) const {
1889 1970
      return _blossom_potential[k].end - _blossom_potential[k].begin;
1890 1971
    }
1891 1972

	
1892
    /// \brief Returns the value of the blossom.
1973
    /// \brief Return the dual value (ptential) of the given blossom.
1893 1974
    ///
1894
    /// Returns the the value of the blossom.
1895
    /// \see BlossomIt
1975
    /// This function returns the dual value (ptential) of the given blossom.
1976
    ///
1977
    /// \pre Either run() or start() must be called before using this function.
1896 1978
    Value blossomValue(int k) const {
1897 1979
      return _blossom_potential[k].value;
1898 1980
    }
1899 1981

	
1900
    /// \brief Iterator for obtaining the nodes of the blossom.
1982
    /// \brief Iterator for obtaining the nodes of a blossom.
1901 1983
    ///
1902
    /// Iterator for obtaining the nodes of the blossom. This class
1903
    /// provides a common lemon style iterator for listing a
1904
    /// subset of the nodes.
1984
    /// This class provides an iterator for obtaining the nodes of the 
1985
    /// given blossom. It lists a subset of the nodes.
1986
    /// Before using this iterator, you must allocate a 
1987
    /// MaxWeightedMatching class and execute it.
1905 1988
    class BlossomIt {
1906 1989
    public:
1907 1990

	
1908 1991
      /// \brief Constructor.
1909 1992
      ///
1910
      /// Constructor to get the nodes of the variable.
1993
      /// Constructor to get the nodes of the given variable.
1994
      ///
1995
      /// \pre Either \ref MaxWeightedMatching::run() "algorithm.run()" or 
1996
      /// \ref MaxWeightedMatching::start() "algorithm.start()" must be 
1997
      /// called before initializing this iterator.
1911 1998
      BlossomIt(const MaxWeightedMatching& algorithm, int variable)
1912 1999
        : _algorithm(&algorithm)
1913 2000
      {
1914 2001
        _index = _algorithm->_blossom_potential[variable].begin;
1915 2002
        _last = _algorithm->_blossom_potential[variable].end;
1916 2003
      }
1917 2004

	
1918
      /// \brief Conversion to node.
2005
      /// \brief Conversion to \c Node.
1919 2006
      ///
1920
      /// Conversion to node.
2007
      /// Conversion to \c Node.
1921 2008
      operator Node() const {
1922 2009
        return _algorithm->_blossom_node_list[_index];
1923 2010
      }
1924 2011

	
1925 2012
      /// \brief Increment operator.
1926 2013
      ///
1927 2014
      /// Increment operator.
1928 2015
      BlossomIt& operator++() {
1929 2016
        ++_index;
1930 2017
        return *this;
1931 2018
      }
1932 2019

	
1933 2020
      /// \brief Validity checking
1934 2021
      ///
1935 2022
      /// Checks whether the iterator is invalid.
1936 2023
      bool operator==(Invalid) const { return _index == _last; }
1937 2024

	
1938 2025
      /// \brief Validity checking
1939 2026
      ///
1940 2027
      /// Checks whether the iterator is valid.
1941 2028
      bool operator!=(Invalid) const { return _index != _last; }
1942 2029

	
1943 2030
    private:
1944 2031
      const MaxWeightedMatching* _algorithm;
1945 2032
      int _last;
1946 2033
      int _index;
1947 2034
    };
1948 2035

	
1949 2036
    /// @}
1950 2037

	
1951 2038
  };
1952 2039

	
1953 2040
  /// \ingroup matching
1954 2041
  ///
1955 2042
  /// \brief Weighted perfect matching in general graphs
1956 2043
  ///
1957 2044
  /// This class provides an efficient implementation of Edmond's
1958 2045
  /// maximum weighted perfect matching algorithm. The implementation
1959 2046
  /// is based on extensive use of priority queues and provides
1960
  /// \f$O(nm\log(n))\f$ time complexity.
2047
  /// \f$O(nm\log n)\f$ time complexity.
1961 2048
  ///
1962
  /// The maximum weighted matching problem is to find undirected
1963
  /// edges in the graph with maximum overall weight and no two of
1964
  /// them shares their ends and covers all nodes. The problem can be
1965
  /// formulated with the following linear program.
2049
  /// The maximum weighted perfect matching problem is to find a subset of 
2050
  /// the edges in an undirected graph with maximum overall weight for which 
2051
  /// each node has exactly one incident edge.
2052
  /// It can be formulated with the following linear program.
1966 2053
  /// \f[ \sum_{e \in \delta(u)}x_e = 1 \quad \forall u\in V\f]
1967 2054
  /** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2}
1968 2055
      \quad \forall B\in\mathcal{O}\f] */
1969 2056
  /// \f[x_e \ge 0\quad \forall e\in E\f]
1970 2057
  /// \f[\max \sum_{e\in E}x_ew_e\f]
1971 2058
  /// where \f$\delta(X)\f$ is the set of edges incident to a node in
1972 2059
  /// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in
1973 2060
  /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality
1974 2061
  /// subsets of the nodes.
1975 2062
  ///
1976 2063
  /// The algorithm calculates an optimal matching and a proof of the
1977 2064
  /// optimality. The solution of the dual problem can be used to check
1978 2065
  /// the result of the algorithm. The dual linear problem is the
2066
  /// following.
1979 2067
  /** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}z_B \ge
1980 2068
      w_{uv} \quad \forall uv\in E\f] */
1981 2069
  /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f]
1982 2070
  /** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}
1983 2071
      \frac{\vert B \vert - 1}{2}z_B\f] */
1984 2072
  ///
1985
  /// The algorithm can be executed with \c run() or the \c init() and
1986
  /// then the \c start() member functions. After it the matching can
1987
  /// be asked with \c matching() or mate() functions. The dual
1988
  /// solution can be get with \c nodeValue(), \c blossomNum() and \c
1989
  /// blossomValue() members and \ref MaxWeightedMatching::BlossomIt
1990
  /// "BlossomIt" nested class which is able to iterate on the nodes
1991
  /// of a blossom. If the value type is integral then the dual
1992
  /// solution is multiplied by \ref MaxWeightedMatching::dualScale "4".
1993
  template <typename _Graph,
1994
            typename _WeightMap = typename _Graph::template EdgeMap<int> >
2073
  /// The algorithm can be executed with the run() function. 
2074
  /// After it the matching (the primal solution) and the dual solution
2075
  /// can be obtained using the query functions and the 
2076
  /// \ref MaxWeightedPerfectMatching::BlossomIt "BlossomIt" nested class, 
2077
  /// which is able to iterate on the nodes of a blossom. 
2078
  /// If the value type is integer, then the dual solution is multiplied
2079
  /// by \ref MaxWeightedMatching::dualScale "4".
2080
  ///
2081
  /// \tparam GR The undirected graph type the algorithm runs on.
2082
  /// \tparam WM The type edge weight map. The default type is 
2083
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
2084
#ifdef DOXYGEN
2085
  template <typename GR, typename WM>
2086
#else
2087
  template <typename GR,
2088
            typename WM = typename GR::template EdgeMap<int> >
2089
#endif
1995 2090
  class MaxWeightedPerfectMatching {
1996 2091
  public:
1997 2092

	
1998
    typedef _Graph Graph;
1999
    typedef _WeightMap WeightMap;
2093
    /// The graph type of the algorithm
2094
    typedef GR Graph;
2095
    /// The type of the edge weight map
2096
    typedef WM WeightMap;
2097
    /// The value type of the edge weights
2000 2098
    typedef typename WeightMap::Value Value;
2001 2099

	
2002 2100
    /// \brief Scaling factor for dual solution
2003 2101
    ///
2004 2102
    /// Scaling factor for dual solution, it is equal to 4 or 1
2005 2103
    /// according to the value type.
2006 2104
    static const int dualScale =
2007 2105
      std::numeric_limits<Value>::is_integer ? 4 : 1;
2008 2106

	
2107
    /// The type of the matching map
2009 2108
    typedef typename Graph::template NodeMap<typename Graph::Arc>
2010 2109
    MatchingMap;
2011 2110

	
2012 2111
  private:
2013 2112

	
2014 2113
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
2015 2114

	
2016 2115
    typedef typename Graph::template NodeMap<Value> NodePotential;
2017 2116
    typedef std::vector<Node> BlossomNodeList;
2018 2117

	
2019 2118
    struct BlossomVariable {
2020 2119
      int begin, end;
2021 2120
      Value value;
2022 2121

	
2023 2122
      BlossomVariable(int _begin, int _end, Value _value)
2024 2123
        : begin(_begin), end(_end), value(_value) {}
2025 2124

	
2026 2125
    };
2027 2126

	
2028 2127
    typedef std::vector<BlossomVariable> BlossomPotential;
2029 2128

	
2030 2129
    const Graph& _graph;
2031 2130
    const WeightMap& _weight;
2032 2131

	
2033 2132
    MatchingMap* _matching;
2034 2133

	
2035 2134
    NodePotential* _node_potential;
2036 2135

	
2037 2136
    BlossomPotential _blossom_potential;
2038 2137
    BlossomNodeList _blossom_node_list;
2039 2138

	
2040 2139
    int _node_num;
2041 2140
    int _blossom_num;
2042 2141

	
2043 2142
    typedef RangeMap<int> IntIntMap;
2044 2143

	
2045 2144
    enum Status {
2046 2145
      EVEN = -1, MATCHED = 0, ODD = 1
2047 2146
    };
2048 2147

	
2049 2148
    typedef HeapUnionFind<Value, IntNodeMap> BlossomSet;
2050 2149
    struct BlossomData {
2051 2150
      int tree;
2052 2151
      Status status;
2053 2152
      Arc pred, next;
2054 2153
      Value pot, offset;
2055 2154
    };
2056 2155

	
2057 2156
    IntNodeMap *_blossom_index;
2058 2157
    BlossomSet *_blossom_set;
2059 2158
    RangeMap<BlossomData>* _blossom_data;
2060 2159

	
2061 2160
    IntNodeMap *_node_index;
2062 2161
    IntArcMap *_node_heap_index;
2063 2162

	
2064 2163
    struct NodeData {
2065 2164

	
2066 2165
      NodeData(IntArcMap& node_heap_index)
2067 2166
        : heap(node_heap_index) {}
2068 2167

	
2069 2168
      int blossom;
2070 2169
      Value pot;
2071 2170
      BinHeap<Value, IntArcMap> heap;
2072 2171
      std::map<int, Arc> heap_index;
2073 2172

	
2074 2173
      int tree;
2075 2174
    };
2076 2175

	
2077 2176
    RangeMap<NodeData>* _node_data;
2078 2177

	
2079 2178
    typedef ExtendFindEnum<IntIntMap> TreeSet;
2080 2179

	
2081 2180
    IntIntMap *_tree_set_index;
2082 2181
    TreeSet *_tree_set;
2083 2182

	
2084 2183
    IntIntMap *_delta2_index;
2085 2184
    BinHeap<Value, IntIntMap> *_delta2;
2086 2185

	
2087 2186
    IntEdgeMap *_delta3_index;
2088 2187
    BinHeap<Value, IntEdgeMap> *_delta3;
2089 2188

	
2090 2189
    IntIntMap *_delta4_index;
2091 2190
    BinHeap<Value, IntIntMap> *_delta4;
2092 2191

	
2093 2192
    Value _delta_sum;
2094 2193

	
2095 2194
    void createStructures() {
2096 2195
      _node_num = countNodes(_graph);
2097 2196
      _blossom_num = _node_num * 3 / 2;
2098 2197

	
2099 2198
      if (!_matching) {
2100 2199
        _matching = new MatchingMap(_graph);
2101 2200
      }
2102 2201
      if (!_node_potential) {
2103 2202
        _node_potential = new NodePotential(_graph);
2104 2203
      }
... ...
@@ -2645,460 +2744,501 @@
2645 2744
      for (int i = 0; i < int(subblossoms.size()); ++i) {
2646 2745
        if (subblossoms[i] == b) ib = i;
2647 2746
        if (subblossoms[i] == d) id = i;
2648 2747

	
2649 2748
        (*_blossom_data)[subblossoms[i]].offset = offset;
2650 2749
        if (!_blossom_set->trivial(subblossoms[i])) {
2651 2750
          (*_blossom_data)[subblossoms[i]].pot -= 2 * offset;
2652 2751
        }
2653 2752
        if (_blossom_set->classPrio(subblossoms[i]) !=
2654 2753
            std::numeric_limits<Value>::max()) {
2655 2754
          _delta2->push(subblossoms[i],
2656 2755
                        _blossom_set->classPrio(subblossoms[i]) -
2657 2756
                        (*_blossom_data)[subblossoms[i]].offset);
2658 2757
        }
2659 2758
      }
2660 2759

	
2661 2760
      if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) {
2662 2761
        for (int i = (id + 1) % subblossoms.size();
2663 2762
             i != ib; i = (i + 2) % subblossoms.size()) {
2664 2763
          int sb = subblossoms[i];
2665 2764
          int tb = subblossoms[(i + 1) % subblossoms.size()];
2666 2765
          (*_blossom_data)[sb].next =
2667 2766
            _graph.oppositeArc((*_blossom_data)[tb].next);
2668 2767
        }
2669 2768

	
2670 2769
        for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) {
2671 2770
          int sb = subblossoms[i];
2672 2771
          int tb = subblossoms[(i + 1) % subblossoms.size()];
2673 2772
          int ub = subblossoms[(i + 2) % subblossoms.size()];
2674 2773

	
2675 2774
          (*_blossom_data)[sb].status = ODD;
2676 2775
          matchedToOdd(sb);
2677 2776
          _tree_set->insert(sb, tree);
2678 2777
          (*_blossom_data)[sb].pred = pred;
2679 2778
          (*_blossom_data)[sb].next =
2680 2779
                           _graph.oppositeArc((*_blossom_data)[tb].next);
2681 2780

	
2682 2781
          pred = (*_blossom_data)[ub].next;
2683 2782

	
2684 2783
          (*_blossom_data)[tb].status = EVEN;
2685 2784
          matchedToEven(tb, tree);
2686 2785
          _tree_set->insert(tb, tree);
2687 2786
          (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next;
2688 2787
        }
2689 2788

	
2690 2789
        (*_blossom_data)[subblossoms[id]].status = ODD;
2691 2790
        matchedToOdd(subblossoms[id]);
2692 2791
        _tree_set->insert(subblossoms[id], tree);
2693 2792
        (*_blossom_data)[subblossoms[id]].next = next;
2694 2793
        (*_blossom_data)[subblossoms[id]].pred = pred;
2695 2794

	
2696 2795
      } else {
2697 2796

	
2698 2797
        for (int i = (ib + 1) % subblossoms.size();
2699 2798
             i != id; i = (i + 2) % subblossoms.size()) {
2700 2799
          int sb = subblossoms[i];
2701 2800
          int tb = subblossoms[(i + 1) % subblossoms.size()];
2702 2801
          (*_blossom_data)[sb].next =
2703 2802
            _graph.oppositeArc((*_blossom_data)[tb].next);
2704 2803
        }
2705 2804

	
2706 2805
        for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) {
2707 2806
          int sb = subblossoms[i];
2708 2807
          int tb = subblossoms[(i + 1) % subblossoms.size()];
2709 2808
          int ub = subblossoms[(i + 2) % subblossoms.size()];
2710 2809

	
2711 2810
          (*_blossom_data)[sb].status = ODD;
2712 2811
          matchedToOdd(sb);
2713 2812
          _tree_set->insert(sb, tree);
2714 2813
          (*_blossom_data)[sb].next = next;
2715 2814
          (*_blossom_data)[sb].pred =
2716 2815
            _graph.oppositeArc((*_blossom_data)[tb].next);
2717 2816

	
2718 2817
          (*_blossom_data)[tb].status = EVEN;
2719 2818
          matchedToEven(tb, tree);
2720 2819
          _tree_set->insert(tb, tree);
2721 2820
          (*_blossom_data)[tb].pred =
2722 2821
            (*_blossom_data)[tb].next =
2723 2822
            _graph.oppositeArc((*_blossom_data)[ub].next);
2724 2823
          next = (*_blossom_data)[ub].next;
2725 2824
        }
2726 2825

	
2727 2826
        (*_blossom_data)[subblossoms[ib]].status = ODD;
2728 2827
        matchedToOdd(subblossoms[ib]);
2729 2828
        _tree_set->insert(subblossoms[ib], tree);
2730 2829
        (*_blossom_data)[subblossoms[ib]].next = next;
2731 2830
        (*_blossom_data)[subblossoms[ib]].pred = pred;
2732 2831
      }
2733 2832
      _tree_set->erase(blossom);
2734 2833
    }
2735 2834

	
2736 2835
    void extractBlossom(int blossom, const Node& base, const Arc& matching) {
2737 2836
      if (_blossom_set->trivial(blossom)) {
2738 2837
        int bi = (*_node_index)[base];
2739 2838
        Value pot = (*_node_data)[bi].pot;
2740 2839

	
2741
        _matching->set(base, matching);
2840
        (*_matching)[base] = matching;
2742 2841
        _blossom_node_list.push_back(base);
2743
        _node_potential->set(base, pot);
2842
        (*_node_potential)[base] = pot;
2744 2843
      } else {
2745 2844

	
2746 2845
        Value pot = (*_blossom_data)[blossom].pot;
2747 2846
        int bn = _blossom_node_list.size();
2748 2847

	
2749 2848
        std::vector<int> subblossoms;
2750 2849
        _blossom_set->split(blossom, std::back_inserter(subblossoms));
2751 2850
        int b = _blossom_set->find(base);
2752 2851
        int ib = -1;
2753 2852
        for (int i = 0; i < int(subblossoms.size()); ++i) {
2754 2853
          if (subblossoms[i] == b) { ib = i; break; }
2755 2854
        }
2756 2855

	
2757 2856
        for (int i = 1; i < int(subblossoms.size()); i += 2) {
2758 2857
          int sb = subblossoms[(ib + i) % subblossoms.size()];
2759 2858
          int tb = subblossoms[(ib + i + 1) % subblossoms.size()];
2760 2859

	
2761 2860
          Arc m = (*_blossom_data)[tb].next;
2762 2861
          extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m));
2763 2862
          extractBlossom(tb, _graph.source(m), m);
2764 2863
        }
2765 2864
        extractBlossom(subblossoms[ib], base, matching);
2766 2865

	
2767 2866
        int en = _blossom_node_list.size();
2768 2867

	
2769 2868
        _blossom_potential.push_back(BlossomVariable(bn, en, pot));
2770 2869
      }
2771 2870
    }
2772 2871

	
2773 2872
    void extractMatching() {
2774 2873
      std::vector<int> blossoms;
2775 2874
      for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) {
2776 2875
        blossoms.push_back(c);
2777 2876
      }
2778 2877

	
2779 2878
      for (int i = 0; i < int(blossoms.size()); ++i) {
2780 2879

	
2781 2880
        Value offset = (*_blossom_data)[blossoms[i]].offset;
2782 2881
        (*_blossom_data)[blossoms[i]].pot += 2 * offset;
2783 2882
        for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]);
2784 2883
             n != INVALID; ++n) {
2785 2884
          (*_node_data)[(*_node_index)[n]].pot -= offset;
2786 2885
        }
2787 2886

	
2788 2887
        Arc matching = (*_blossom_data)[blossoms[i]].next;
2789 2888
        Node base = _graph.source(matching);
2790 2889
        extractBlossom(blossoms[i], base, matching);
2791 2890
      }
2792 2891
    }
2793 2892

	
2794 2893
  public:
2795 2894

	
2796 2895
    /// \brief Constructor
2797 2896
    ///
2798 2897
    /// Constructor.
2799 2898
    MaxWeightedPerfectMatching(const Graph& graph, const WeightMap& weight)
2800 2899
      : _graph(graph), _weight(weight), _matching(0),
2801 2900
        _node_potential(0), _blossom_potential(), _blossom_node_list(),
2802 2901
        _node_num(0), _blossom_num(0),
2803 2902

	
2804 2903
        _blossom_index(0), _blossom_set(0), _blossom_data(0),
2805 2904
        _node_index(0), _node_heap_index(0), _node_data(0),
2806 2905
        _tree_set_index(0), _tree_set(0),
2807 2906

	
2808 2907
        _delta2_index(0), _delta2(0),
2809 2908
        _delta3_index(0), _delta3(0),
2810 2909
        _delta4_index(0), _delta4(0),
2811 2910

	
2812 2911
        _delta_sum() {}
2813 2912

	
2814 2913
    ~MaxWeightedPerfectMatching() {
2815 2914
      destroyStructures();
2816 2915
    }
2817 2916

	
2818
    /// \name Execution control
2917
    /// \name Execution Control
2819 2918
    /// The simplest way to execute the algorithm is to use the
2820
    /// \c run() member function.
2919
    /// \ref run() member function.
2821 2920

	
2822 2921
    ///@{
2823 2922

	
2824 2923
    /// \brief Initialize the algorithm
2825 2924
    ///
2826
    /// Initialize the algorithm
2925
    /// This function initializes the algorithm.
2827 2926
    void init() {
2828 2927
      createStructures();
2829 2928

	
2830 2929
      for (ArcIt e(_graph); e != INVALID; ++e) {
2831
        _node_heap_index->set(e, BinHeap<Value, IntArcMap>::PRE_HEAP);
2930
        (*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
2832 2931
      }
2833 2932
      for (EdgeIt e(_graph); e != INVALID; ++e) {
2834
        _delta3_index->set(e, _delta3->PRE_HEAP);
2933
        (*_delta3_index)[e] = _delta3->PRE_HEAP;
2835 2934
      }
2836 2935
      for (int i = 0; i < _blossom_num; ++i) {
2837
        _delta2_index->set(i, _delta2->PRE_HEAP);
2838
        _delta4_index->set(i, _delta4->PRE_HEAP);
2936
        (*_delta2_index)[i] = _delta2->PRE_HEAP;
2937
        (*_delta4_index)[i] = _delta4->PRE_HEAP;
2839 2938
      }
2840 2939

	
2841 2940
      int index = 0;
2842 2941
      for (NodeIt n(_graph); n != INVALID; ++n) {
2843 2942
        Value max = - std::numeric_limits<Value>::max();
2844 2943
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
2845 2944
          if (_graph.target(e) == n) continue;
2846 2945
          if ((dualScale * _weight[e]) / 2 > max) {
2847 2946
            max = (dualScale * _weight[e]) / 2;
2848 2947
          }
2849 2948
        }
2850
        _node_index->set(n, index);
2949
        (*_node_index)[n] = index;
2851 2950
        (*_node_data)[index].pot = max;
2852 2951
        int blossom =
2853 2952
          _blossom_set->insert(n, std::numeric_limits<Value>::max());
2854 2953

	
2855 2954
        _tree_set->insert(blossom);
2856 2955

	
2857 2956
        (*_blossom_data)[blossom].status = EVEN;
2858 2957
        (*_blossom_data)[blossom].pred = INVALID;
2859 2958
        (*_blossom_data)[blossom].next = INVALID;
2860 2959
        (*_blossom_data)[blossom].pot = 0;
2861 2960
        (*_blossom_data)[blossom].offset = 0;
2862 2961
        ++index;
2863 2962
      }
2864 2963
      for (EdgeIt e(_graph); e != INVALID; ++e) {
2865 2964
        int si = (*_node_index)[_graph.u(e)];
2866 2965
        int ti = (*_node_index)[_graph.v(e)];
2867 2966
        if (_graph.u(e) != _graph.v(e)) {
2868 2967
          _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
2869 2968
                            dualScale * _weight[e]) / 2);
2870 2969
        }
2871 2970
      }
2872 2971
    }
2873 2972

	
2874
    /// \brief Starts the algorithm
2973
    /// \brief Start the algorithm
2875 2974
    ///
2876
    /// Starts the algorithm
2975
    /// This function starts the algorithm.
2976
    ///
2977
    /// \pre \ref init() must be called before using this function.
2877 2978
    bool start() {
2878 2979
      enum OpType {
2879 2980
        D2, D3, D4
2880 2981
      };
2881 2982

	
2882 2983
      int unmatched = _node_num;
2883 2984
      while (unmatched > 0) {
2884 2985
        Value d2 = !_delta2->empty() ?
2885 2986
          _delta2->prio() : std::numeric_limits<Value>::max();
2886 2987

	
2887 2988
        Value d3 = !_delta3->empty() ?
2888 2989
          _delta3->prio() : std::numeric_limits<Value>::max();
2889 2990

	
2890 2991
        Value d4 = !_delta4->empty() ?
2891 2992
          _delta4->prio() : std::numeric_limits<Value>::max();
2892 2993

	
2893 2994
        _delta_sum = d2; OpType ot = D2;
2894 2995
        if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; }
2895 2996
        if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; }
2896 2997

	
2897 2998
        if (_delta_sum == std::numeric_limits<Value>::max()) {
2898 2999
          return false;
2899 3000
        }
2900 3001

	
2901 3002
        switch (ot) {
2902 3003
        case D2:
2903 3004
          {
2904 3005
            int blossom = _delta2->top();
2905 3006
            Node n = _blossom_set->classTop(blossom);
2906 3007
            Arc e = (*_node_data)[(*_node_index)[n]].heap.top();
2907 3008
            extendOnArc(e);
2908 3009
          }
2909 3010
          break;
2910 3011
        case D3:
2911 3012
          {
2912 3013
            Edge e = _delta3->top();
2913 3014

	
2914 3015
            int left_blossom = _blossom_set->find(_graph.u(e));
2915 3016
            int right_blossom = _blossom_set->find(_graph.v(e));
2916 3017

	
2917 3018
            if (left_blossom == right_blossom) {
2918 3019
              _delta3->pop();
2919 3020
            } else {
2920 3021
              int left_tree = _tree_set->find(left_blossom);
2921 3022
              int right_tree = _tree_set->find(right_blossom);
2922 3023

	
2923 3024
              if (left_tree == right_tree) {
2924 3025
                shrinkOnEdge(e, left_tree);
2925 3026
              } else {
2926 3027
                augmentOnEdge(e);
2927 3028
                unmatched -= 2;
2928 3029
              }
2929 3030
            }
2930 3031
          } break;
2931 3032
        case D4:
2932 3033
          splitBlossom(_delta4->top());
2933 3034
          break;
2934 3035
        }
2935 3036
      }
2936 3037
      extractMatching();
2937 3038
      return true;
2938 3039
    }
2939 3040

	
2940
    /// \brief Runs %MaxWeightedPerfectMatching algorithm.
3041
    /// \brief Run the algorithm.
2941 3042
    ///
2942
    /// This method runs the %MaxWeightedPerfectMatching algorithm.
3043
    /// This method runs the \c %MaxWeightedPerfectMatching algorithm.
2943 3044
    ///
2944
    /// \note mwm.run() is just a shortcut of the following code.
3045
    /// \note mwpm.run() is just a shortcut of the following code.
2945 3046
    /// \code
2946
    ///   mwm.init();
2947
    ///   mwm.start();
3047
    ///   mwpm.init();
3048
    ///   mwpm.start();
2948 3049
    /// \endcode
2949 3050
    bool run() {
2950 3051
      init();
2951 3052
      return start();
2952 3053
    }
2953 3054

	
2954 3055
    /// @}
2955 3056

	
2956
    /// \name Primal solution
2957
    /// Functions to get the primal solution, ie. the matching.
3057
    /// \name Primal Solution
3058
    /// Functions to get the primal solution, i.e. the maximum weighted 
3059
    /// perfect matching.\n
3060
    /// Either \ref run() or \ref start() function should be called before
3061
    /// using them.
2958 3062

	
2959 3063
    /// @{
2960 3064

	
2961
    /// \brief Returns the matching value.
3065
    /// \brief Return the weight of the matching.
2962 3066
    ///
2963
    /// Returns the matching value.
2964
    Value matchingValue() const {
3067
    /// This function returns the weight of the found matching.
3068
    ///
3069
    /// \pre Either run() or start() must be called before using this function.
3070
    Value matchingWeight() const {
2965 3071
      Value sum = 0;
2966 3072
      for (NodeIt n(_graph); n != INVALID; ++n) {
2967 3073
        if ((*_matching)[n] != INVALID) {
2968 3074
          sum += _weight[(*_matching)[n]];
2969 3075
        }
2970 3076
      }
2971 3077
      return sum /= 2;
2972 3078
    }
2973 3079

	
2974
    /// \brief Returns true when the edge is in the matching.
3080
    /// \brief Return \c true if the given edge is in the matching.
2975 3081
    ///
2976
    /// Returns true when the edge is in the matching.
3082
    /// This function returns \c true if the given edge is in the found 
3083
    /// matching.
3084
    ///
3085
    /// \pre Either run() or start() must be called before using this function.
2977 3086
    bool matching(const Edge& edge) const {
2978 3087
      return static_cast<const Edge&>((*_matching)[_graph.u(edge)]) == edge;
2979 3088
    }
2980 3089

	
2981
    /// \brief Returns the incident matching edge.
3090
    /// \brief Return the matching arc (or edge) incident to the given node.
2982 3091
    ///
2983
    /// Returns the incident matching arc from given edge.
3092
    /// This function returns the matching arc (or edge) incident to the
3093
    /// given node in the found matching or \c INVALID if the node is 
3094
    /// not covered by the matching.
3095
    ///
3096
    /// \pre Either run() or start() must be called before using this function.
2984 3097
    Arc matching(const Node& node) const {
2985 3098
      return (*_matching)[node];
2986 3099
    }
2987 3100

	
2988
    /// \brief Returns the mate of the node.
3101
    /// \brief Return a const reference to the matching map.
2989 3102
    ///
2990
    /// Returns the adjancent node in a mathcing arc.
3103
    /// This function returns a const reference to a node map that stores
3104
    /// the matching arc (or edge) incident to each node.
3105
    const MatchingMap& matchingMap() const {
3106
      return *_matching;
3107
    }
3108

	
3109
    /// \brief Return the mate of the given node.
3110
    ///
3111
    /// This function returns the mate of the given node in the found 
3112
    /// matching or \c INVALID if the node is not covered by the matching.
3113
    ///
3114
    /// \pre Either run() or start() must be called before using this function.
2991 3115
    Node mate(const Node& node) const {
2992 3116
      return _graph.target((*_matching)[node]);
2993 3117
    }
2994 3118

	
2995 3119
    /// @}
2996 3120

	
2997
    /// \name Dual solution
2998
    /// Functions to get the dual solution.
3121
    /// \name Dual Solution
3122
    /// Functions to get the dual solution.\n
3123
    /// Either \ref run() or \ref start() function should be called before
3124
    /// using them.
2999 3125

	
3000 3126
    /// @{
3001 3127

	
3002
    /// \brief Returns the value of the dual solution.
3128
    /// \brief Return the value of the dual solution.
3003 3129
    ///
3004
    /// Returns the value of the dual solution. It should be equal to
3005
    /// the primal value scaled by \ref dualScale "dual scale".
3130
    /// This function returns the value of the dual solution. 
3131
    /// It should be equal to the primal value scaled by \ref dualScale 
3132
    /// "dual scale".
3133
    ///
3134
    /// \pre Either run() or start() must be called before using this function.
3006 3135
    Value dualValue() const {
3007 3136
      Value sum = 0;
3008 3137
      for (NodeIt n(_graph); n != INVALID; ++n) {
3009 3138
        sum += nodeValue(n);
3010 3139
      }
3011 3140
      for (int i = 0; i < blossomNum(); ++i) {
3012 3141
        sum += blossomValue(i) * (blossomSize(i) / 2);
3013 3142
      }
3014 3143
      return sum;
3015 3144
    }
3016 3145

	
3017
    /// \brief Returns the value of the node.
3146
    /// \brief Return the dual value (potential) of the given node.
3018 3147
    ///
3019
    /// Returns the the value of the node.
3148
    /// This function returns the dual value (potential) of the given node.
3149
    ///
3150
    /// \pre Either run() or start() must be called before using this function.
3020 3151
    Value nodeValue(const Node& n) const {
3021 3152
      return (*_node_potential)[n];
3022 3153
    }
3023 3154

	
3024
    /// \brief Returns the number of the blossoms in the basis.
3155
    /// \brief Return the number of the blossoms in the basis.
3025 3156
    ///
3026
    /// Returns the number of the blossoms in the basis.
3157
    /// This function returns the number of the blossoms in the basis.
3158
    ///
3159
    /// \pre Either run() or start() must be called before using this function.
3027 3160
    /// \see BlossomIt
3028 3161
    int blossomNum() const {
3029 3162
      return _blossom_potential.size();
3030 3163
    }
3031 3164

	
3032

	
3033
    /// \brief Returns the number of the nodes in the blossom.
3165
    /// \brief Return the number of the nodes in the given blossom.
3034 3166
    ///
3035
    /// Returns the number of the nodes in the blossom.
3167
    /// This function returns the number of the nodes in the given blossom.
3168
    ///
3169
    /// \pre Either run() or start() must be called before using this function.
3170
    /// \see BlossomIt
3036 3171
    int blossomSize(int k) const {
3037 3172
      return _blossom_potential[k].end - _blossom_potential[k].begin;
3038 3173
    }
3039 3174

	
3040
    /// \brief Returns the value of the blossom.
3175
    /// \brief Return the dual value (ptential) of the given blossom.
3041 3176
    ///
3042
    /// Returns the the value of the blossom.
3043
    /// \see BlossomIt
3177
    /// This function returns the dual value (ptential) of the given blossom.
3178
    ///
3179
    /// \pre Either run() or start() must be called before using this function.
3044 3180
    Value blossomValue(int k) const {
3045 3181
      return _blossom_potential[k].value;
3046 3182
    }
3047 3183

	
3048
    /// \brief Iterator for obtaining the nodes of the blossom.
3184
    /// \brief Iterator for obtaining the nodes of a blossom.
3049 3185
    ///
3050
    /// Iterator for obtaining the nodes of the blossom. This class
3051
    /// provides a common lemon style iterator for listing a
3052
    /// subset of the nodes.
3186
    /// This class provides an iterator for obtaining the nodes of the 
3187
    /// given blossom. It lists a subset of the nodes.
3188
    /// Before using this iterator, you must allocate a 
3189
    /// MaxWeightedPerfectMatching class and execute it.
3053 3190
    class BlossomIt {
3054 3191
    public:
3055 3192

	
3056 3193
      /// \brief Constructor.
3057 3194
      ///
3058
      /// Constructor to get the nodes of the variable.
3195
      /// Constructor to get the nodes of the given variable.
3196
      ///
3197
      /// \pre Either \ref MaxWeightedPerfectMatching::run() "algorithm.run()" 
3198
      /// or \ref MaxWeightedPerfectMatching::start() "algorithm.start()" 
3199
      /// must be called before initializing this iterator.
3059 3200
      BlossomIt(const MaxWeightedPerfectMatching& algorithm, int variable)
3060 3201
        : _algorithm(&algorithm)
3061 3202
      {
3062 3203
        _index = _algorithm->_blossom_potential[variable].begin;
3063 3204
        _last = _algorithm->_blossom_potential[variable].end;
3064 3205
      }
3065 3206

	
3066
      /// \brief Conversion to node.
3207
      /// \brief Conversion to \c Node.
3067 3208
      ///
3068
      /// Conversion to node.
3209
      /// Conversion to \c Node.
3069 3210
      operator Node() const {
3070 3211
        return _algorithm->_blossom_node_list[_index];
3071 3212
      }
3072 3213

	
3073 3214
      /// \brief Increment operator.
3074 3215
      ///
3075 3216
      /// Increment operator.
3076 3217
      BlossomIt& operator++() {
3077 3218
        ++_index;
3078 3219
        return *this;
3079 3220
      }
3080 3221

	
3081 3222
      /// \brief Validity checking
3082 3223
      ///
3083
      /// Checks whether the iterator is invalid.
3224
      /// This function checks whether the iterator is invalid.
3084 3225
      bool operator==(Invalid) const { return _index == _last; }
3085 3226

	
3086 3227
      /// \brief Validity checking
3087 3228
      ///
3088
      /// Checks whether the iterator is valid.
3229
      /// This function checks whether the iterator is valid.
3089 3230
      bool operator!=(Invalid) const { return _index != _last; }
3090 3231

	
3091 3232
    private:
3092 3233
      const MaxWeightedPerfectMatching* _algorithm;
3093 3234
      int _last;
3094 3235
      int _index;
3095 3236
    };
3096 3237

	
3097 3238
    /// @}
3098 3239

	
3099 3240
  };
3100 3241

	
3101

	
3102 3242
} //END OF NAMESPACE LEMON
3103 3243

	
3104 3244
#endif //LEMON_MAX_MATCHING_H
Ignore white space 6 line context
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-2009
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_MATH_H
20 20
#define LEMON_MATH_H
21 21

	
22 22
///\ingroup misc
23 23
///\file
24 24
///\brief Some extensions to the standard \c cmath library.
25 25
///
26 26
///Some extensions to the standard \c cmath library.
27 27
///
28 28
///This file includes the standard math library (cmath).
29 29

	
30 30
#include<cmath>
31 31

	
32 32
namespace lemon {
33 33

	
34 34
  /// \addtogroup misc
35 35
  /// @{
36 36

	
37 37
  /// The Euler constant
38 38
  const long double E       = 2.7182818284590452353602874713526625L;
39 39
  /// log_2(e)
40 40
  const long double LOG2E   = 1.4426950408889634073599246810018921L;
41 41
  /// log_10(e)
42 42
  const long double LOG10E  = 0.4342944819032518276511289189166051L;
43 43
  /// ln(2)
44 44
  const long double LN2     = 0.6931471805599453094172321214581766L;
45 45
  /// ln(10)
46 46
  const long double LN10    = 2.3025850929940456840179914546843642L;
47 47
  /// pi
48 48
  const long double PI      = 3.1415926535897932384626433832795029L;
49 49
  /// pi/2
50 50
  const long double PI_2    = 1.5707963267948966192313216916397514L;
51 51
  /// pi/4
52 52
  const long double PI_4    = 0.7853981633974483096156608458198757L;
53 53
  /// sqrt(2)
54 54
  const long double SQRT2   = 1.4142135623730950488016887242096981L;
55 55
  /// 1/sqrt(2)
56 56
  const long double SQRT1_2 = 0.7071067811865475244008443621048490L;
57 57

	
58
  ///Check whether the parameter is NaN or not
59
  
60
  ///This function checks whether the parameter is NaN or not.
61
  ///Is should be equivalent with std::isnan(), but it is not
62
  ///provided by all compilers.
63
  inline bool isNaN(double v)
64
    {
65
      return v!=v;
66
    }
58 67

	
59 68
  /// @}
60 69

	
61 70
} //namespace lemon
62 71

	
63 72
#endif //LEMON_TOLERANCE_H
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup paths
20 20
///\file
21 21
///\brief Classes for representing paths in digraphs.
22 22
///
23 23

	
24 24
#ifndef LEMON_PATH_H
25 25
#define LEMON_PATH_H
26 26

	
27 27
#include <vector>
28 28
#include <algorithm>
29 29

	
30 30
#include <lemon/error.h>
31 31
#include <lemon/core.h>
32 32
#include <lemon/concepts/path.h>
33 33

	
34 34
namespace lemon {
35 35

	
36 36
  /// \addtogroup paths
37 37
  /// @{
38 38

	
39 39

	
40 40
  /// \brief A structure for representing directed paths in a digraph.
41 41
  ///
42 42
  /// A structure for representing directed path in a digraph.
43
  /// \tparam _Digraph The digraph type in which the path is.
43
  /// \tparam GR 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 of the path and the source node of
48 48
  /// a zero length path is undefined.
49 49
  ///
50 50
  /// This implementation is a back and front insertable and erasable
51 51
  /// path type. It can be indexed in O(1) time. The front and back
52 52
  /// insertion and erase is done in O(1) (amortized) time. The
53 53
  /// implementation uses two vectors for storing the front and back
54 54
  /// insertions.
55
  template <typename _Digraph>
55
  template <typename GR>
56 56
  class Path {
57 57
  public:
58 58

	
59
    typedef _Digraph Digraph;
59
    typedef GR Digraph;
60 60
    typedef typename Digraph::Arc Arc;
61 61

	
62 62
    /// \brief Default constructor
63 63
    ///
64 64
    /// Default constructor
65 65
    Path() {}
66 66

	
67 67
    /// \brief Template copy constructor
68 68
    ///
69 69
    /// This constuctor initializes the path from any other path type.
70 70
    /// It simply makes a copy of the given path.
71 71
    template <typename CPath>
72 72
    Path(const CPath& cpath) {
73 73
      copyPath(*this, cpath);
74 74
    }
75 75

	
76 76
    /// \brief Template copy assignment
77 77
    ///
78 78
    /// This operator makes a copy of a path of any other type.
79 79
    template <typename CPath>
80 80
    Path& operator=(const CPath& cpath) {
81 81
      copyPath(*this, cpath);
82 82
      return *this;
83 83
    }
84 84

	
85 85
    /// \brief LEMON style iterator for path arcs
86 86
    ///
87 87
    /// This class is used to iterate on the arcs of the paths.
88 88
    class ArcIt {
89 89
      friend class Path;
90 90
    public:
91 91
      /// \brief Default constructor
92 92
      ArcIt() {}
93 93
      /// \brief Invalid constructor
94 94
      ArcIt(Invalid) : path(0), idx(-1) {}
95 95
      /// \brief Initializate the iterator to the first arc of path
96 96
      ArcIt(const Path &_path)
97 97
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
98 98

	
99 99
    private:
100 100

	
101 101
      ArcIt(const Path &_path, int _idx)
102 102
        : path(&_path), idx(_idx) {}
103 103

	
104 104
    public:
105 105

	
106 106
      /// \brief Conversion to Arc
107 107
      operator const Arc&() const {
108 108
        return path->nth(idx);
109 109
      }
110 110

	
111 111
      /// \brief Next arc
112 112
      ArcIt& operator++() {
113 113
        ++idx;
114 114
        if (idx >= path->length()) idx = -1;
115 115
        return *this;
116 116
      }
117 117

	
118 118
      /// \brief Comparison operator
119 119
      bool operator==(const ArcIt& e) const { return idx==e.idx; }
120 120
      /// \brief Comparison operator
121 121
      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
122 122
      /// \brief Comparison operator
123 123
      bool operator<(const ArcIt& e) const { return idx<e.idx; }
124 124

	
125 125
    private:
126 126
      const Path *path;
127 127
      int idx;
128 128
    };
129 129

	
130 130
    /// \brief Length of the path.
131 131
    int length() const { return head.size() + tail.size(); }
132 132
    /// \brief Return whether the path is empty.
133 133
    bool empty() const { return head.empty() && tail.empty(); }
134 134

	
135 135
    /// \brief Reset the path to an empty one.
136 136
    void clear() { head.clear(); tail.clear(); }
137 137

	
138 138
    /// \brief The nth arc.
139 139
    ///
140
    /// \pre n is in the [0..length() - 1] range
140
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
141 141
    const Arc& nth(int n) const {
142 142
      return n < int(head.size()) ? *(head.rbegin() + n) :
143 143
        *(tail.begin() + (n - head.size()));
144 144
    }
145 145

	
146 146
    /// \brief Initialize arc iterator to point to the nth arc
147 147
    ///
148
    /// \pre n is in the [0..length() - 1] range
148
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
149 149
    ArcIt nthIt(int n) const {
150 150
      return ArcIt(*this, n);
151 151
    }
152 152

	
153 153
    /// \brief The first arc of the path
154 154
    const Arc& front() const {
155 155
      return head.empty() ? tail.front() : head.back();
156 156
    }
157 157

	
158 158
    /// \brief Add a new arc before the current path
159 159
    void addFront(const Arc& arc) {
160 160
      head.push_back(arc);
161 161
    }
162 162

	
163 163
    /// \brief Erase the first arc of the path
164 164
    void eraseFront() {
165 165
      if (!head.empty()) {
166 166
        head.pop_back();
167 167
      } else {
168 168
        head.clear();
169 169
        int halfsize = tail.size() / 2;
170 170
        head.resize(halfsize);
171 171
        std::copy(tail.begin() + 1, tail.begin() + halfsize + 1,
172 172
                  head.rbegin());
173 173
        std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin());
174 174
        tail.resize(tail.size() - halfsize - 1);
175 175
      }
176 176
    }
177 177

	
178 178
    /// \brief The last arc of the path
179 179
    const Arc& back() const {
180 180
      return tail.empty() ? head.front() : tail.back();
181 181
    }
182 182

	
183 183
    /// \brief Add a new arc behind the current path
184 184
    void addBack(const Arc& arc) {
185 185
      tail.push_back(arc);
186 186
    }
187 187

	
188 188
    /// \brief Erase the last arc of the path
189 189
    void eraseBack() {
190 190
      if (!tail.empty()) {
191 191
        tail.pop_back();
192 192
      } else {
193 193
        int halfsize = head.size() / 2;
194 194
        tail.resize(halfsize);
195 195
        std::copy(head.begin() + 1, head.begin() + halfsize + 1,
196 196
                  tail.rbegin());
197 197
        std::copy(head.begin() + halfsize + 1, head.end(), head.begin());
198 198
        head.resize(head.size() - halfsize - 1);
199 199
      }
200 200
    }
201 201

	
202 202
    typedef True BuildTag;
203 203

	
204 204
    template <typename CPath>
205 205
    void build(const CPath& path) {
206 206
      int len = path.length();
207 207
      tail.reserve(len);
208 208
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
209 209
        tail.push_back(it);
210 210
      }
211 211
    }
212 212

	
213 213
    template <typename CPath>
214 214
    void buildRev(const CPath& path) {
215 215
      int len = path.length();
216 216
      head.reserve(len);
217 217
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
218 218
        head.push_back(it);
219 219
      }
220 220
    }
221 221

	
222 222
  protected:
223 223
    typedef std::vector<Arc> Container;
224 224
    Container head, tail;
225 225

	
226 226
  };
227 227

	
228 228
  /// \brief A structure for representing directed paths in a digraph.
229 229
  ///
230 230
  /// A structure for representing directed path in a digraph.
231
  /// \tparam _Digraph The digraph type in which the path is.
231
  /// \tparam GR The digraph type in which the path is.
232 232
  ///
233 233
  /// In a sense, the path can be treated as a list of arcs. The
234 234
  /// lemon path type stores just this list. As a consequence it
235 235
  /// cannot enumerate the nodes in the path and the zero length paths
236 236
  /// cannot store the source.
237 237
  ///
238 238
  /// This implementation is a just back insertable and erasable path
239 239
  /// type. It can be indexed in O(1) time. The back insertion and
240 240
  /// erasure is amortized O(1) time. This implementation is faster
241 241
  /// then the \c Path type because it use just one vector for the
242 242
  /// arcs.
243
  template <typename _Digraph>
243
  template <typename GR>
244 244
  class SimplePath {
245 245
  public:
246 246

	
247
    typedef _Digraph Digraph;
247
    typedef GR Digraph;
248 248
    typedef typename Digraph::Arc Arc;
249 249

	
250 250
    /// \brief Default constructor
251 251
    ///
252 252
    /// Default constructor
253 253
    SimplePath() {}
254 254

	
255 255
    /// \brief Template copy constructor
256 256
    ///
257 257
    /// This path can be initialized with any other path type. It just
258 258
    /// makes a copy of the given path.
259 259
    template <typename CPath>
260 260
    SimplePath(const CPath& cpath) {
261 261
      copyPath(*this, cpath);
262 262
    }
263 263

	
264 264
    /// \brief Template copy assignment
265 265
    ///
266 266
    /// This path can be initialized with any other path type. It just
267 267
    /// makes a copy of the given path.
268 268
    template <typename CPath>
269 269
    SimplePath& operator=(const CPath& cpath) {
270 270
      copyPath(*this, cpath);
271 271
      return *this;
272 272
    }
273 273

	
274 274
    /// \brief Iterator class to iterate on the arcs of the paths
275 275
    ///
276 276
    /// This class is used to iterate on the arcs of the paths
277 277
    ///
278 278
    /// Of course it converts to Digraph::Arc
279 279
    class ArcIt {
280 280
      friend class SimplePath;
281 281
    public:
282 282
      /// Default constructor
283 283
      ArcIt() {}
284 284
      /// Invalid constructor
285 285
      ArcIt(Invalid) : path(0), idx(-1) {}
286 286
      /// \brief Initializate the constructor to the first arc of path
287 287
      ArcIt(const SimplePath &_path)
288 288
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
289 289

	
290 290
    private:
291 291

	
292 292
      /// Constructor with starting point
293 293
      ArcIt(const SimplePath &_path, int _idx)
294 294
        : idx(_idx), path(&_path) {}
295 295

	
296 296
    public:
297 297

	
298 298
      ///Conversion to Digraph::Arc
299 299
      operator const Arc&() const {
300 300
        return path->nth(idx);
301 301
      }
302 302

	
303 303
      /// Next arc
304 304
      ArcIt& operator++() {
305 305
        ++idx;
306 306
        if (idx >= path->length()) idx = -1;
307 307
        return *this;
308 308
      }
309 309

	
310 310
      /// Comparison operator
311 311
      bool operator==(const ArcIt& e) const { return idx==e.idx; }
312 312
      /// Comparison operator
313 313
      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
314 314
      /// Comparison operator
315 315
      bool operator<(const ArcIt& e) const { return idx<e.idx; }
316 316

	
317 317
    private:
318 318
      const SimplePath *path;
319 319
      int idx;
320 320
    };
321 321

	
322 322
    /// \brief Length of the path.
323 323
    int length() const { return data.size(); }
324 324
    /// \brief Return true if the path is empty.
325 325
    bool empty() const { return data.empty(); }
326 326

	
327 327
    /// \brief Reset the path to an empty one.
328 328
    void clear() { data.clear(); }
329 329

	
330 330
    /// \brief The nth arc.
331 331
    ///
332
    /// \pre n is in the [0..length() - 1] range
332
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
333 333
    const Arc& nth(int n) const {
334 334
      return data[n];
335 335
    }
336 336

	
337 337
    /// \brief  Initializes arc iterator to point to the nth arc.
338 338
    ArcIt nthIt(int n) const {
339 339
      return ArcIt(*this, n);
340 340
    }
341 341

	
342 342
    /// \brief The first arc of the path.
343 343
    const Arc& front() const {
344 344
      return data.front();
345 345
    }
346 346

	
347 347
    /// \brief The last arc of the path.
348 348
    const Arc& back() const {
349 349
      return data.back();
350 350
    }
351 351

	
352 352
    /// \brief Add a new arc behind the current path.
353 353
    void addBack(const Arc& arc) {
354 354
      data.push_back(arc);
355 355
    }
356 356

	
357 357
    /// \brief Erase the last arc of the path
358 358
    void eraseBack() {
359 359
      data.pop_back();
360 360
    }
361 361

	
362 362
    typedef True BuildTag;
363 363

	
364 364
    template <typename CPath>
365 365
    void build(const CPath& path) {
366 366
      int len = path.length();
367 367
      data.resize(len);
368 368
      int index = 0;
369 369
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
370 370
        data[index] = it;;
371 371
        ++index;
372 372
      }
373 373
    }
374 374

	
375 375
    template <typename CPath>
376 376
    void buildRev(const CPath& path) {
377 377
      int len = path.length();
378 378
      data.resize(len);
379 379
      int index = len;
380 380
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
381 381
        --index;
382 382
        data[index] = it;;
383 383
      }
384 384
    }
385 385

	
386 386
  protected:
387 387
    typedef std::vector<Arc> Container;
388 388
    Container data;
389 389

	
390 390
  };
391 391

	
392 392
  /// \brief A structure for representing directed paths in a digraph.
393 393
  ///
394 394
  /// A structure for representing directed path in a digraph.
395
  /// \tparam _Digraph The digraph type in which the path is.
395
  /// \tparam GR The digraph type in which the path is.
396 396
  ///
397 397
  /// In a sense, the path can be treated as a list of arcs. The
398 398
  /// lemon path type stores just this list. As a consequence it
399 399
  /// cannot enumerate the nodes in the path and the zero length paths
400 400
  /// cannot store the source.
401 401
  ///
402 402
  /// This implementation is a back and front insertable and erasable
403 403
  /// path type. It can be indexed in O(k) time, where k is the rank
404 404
  /// of the arc in the path. The length can be computed in O(n)
405 405
  /// time. The front and back insertion and erasure is O(1) time
406 406
  /// and it can be splited and spliced in O(1) time.
407
  template <typename _Digraph>
407
  template <typename GR>
408 408
  class ListPath {
409 409
  public:
410 410

	
411
    typedef _Digraph Digraph;
411
    typedef GR Digraph;
412 412
    typedef typename Digraph::Arc Arc;
413 413

	
414 414
  protected:
415 415

	
416 416
    // the std::list<> is incompatible
417 417
    // hard to create invalid iterator
418 418
    struct Node {
419 419
      Arc arc;
420 420
      Node *next, *prev;
421 421
    };
422 422

	
423 423
    Node *first, *last;
424 424

	
425 425
    std::allocator<Node> alloc;
426 426

	
427 427
  public:
428 428

	
429 429
    /// \brief Default constructor
430 430
    ///
431 431
    /// Default constructor
432 432
    ListPath() : first(0), last(0) {}
433 433

	
434 434
    /// \brief Template copy constructor
435 435
    ///
436 436
    /// This path can be initialized with any other path type. It just
437 437
    /// makes a copy of the given path.
438 438
    template <typename CPath>
439 439
    ListPath(const CPath& cpath) : first(0), last(0) {
440 440
      copyPath(*this, cpath);
441 441
    }
442 442

	
443 443
    /// \brief Destructor of the path
444 444
    ///
445 445
    /// Destructor of the path
446 446
    ~ListPath() {
447 447
      clear();
448 448
    }
449 449

	
450 450
    /// \brief Template copy assignment
451 451
    ///
452 452
    /// This path can be initialized with any other path type. It just
453 453
    /// makes a copy of the given path.
454 454
    template <typename CPath>
455 455
    ListPath& operator=(const CPath& cpath) {
456 456
      copyPath(*this, cpath);
457 457
      return *this;
458 458
    }
459 459

	
460 460
    /// \brief Iterator class to iterate on the arcs of the paths
461 461
    ///
462 462
    /// This class is used to iterate on the arcs of the paths
463 463
    ///
464 464
    /// Of course it converts to Digraph::Arc
465 465
    class ArcIt {
466 466
      friend class ListPath;
467 467
    public:
468 468
      /// Default constructor
469 469
      ArcIt() {}
470 470
      /// Invalid constructor
471 471
      ArcIt(Invalid) : path(0), node(0) {}
472 472
      /// \brief Initializate the constructor to the first arc of path
473 473
      ArcIt(const ListPath &_path)
474 474
        : path(&_path), node(_path.first) {}
475 475

	
476 476
    protected:
477 477

	
478 478
      ArcIt(const ListPath &_path, Node *_node)
479 479
        : path(&_path), node(_node) {}
480 480

	
481 481

	
482 482
    public:
483 483

	
484 484
      ///Conversion to Digraph::Arc
485 485
      operator const Arc&() const {
486 486
        return node->arc;
487 487
      }
488 488

	
489 489
      /// Next arc
490 490
      ArcIt& operator++() {
491 491
        node = node->next;
492 492
        return *this;
493 493
      }
494 494

	
495 495
      /// Comparison operator
496 496
      bool operator==(const ArcIt& e) const { return node==e.node; }
497 497
      /// Comparison operator
498 498
      bool operator!=(const ArcIt& e) const { return node!=e.node; }
499 499
      /// Comparison operator
500 500
      bool operator<(const ArcIt& e) const { return node<e.node; }
501 501

	
502 502
    private:
503 503
      const ListPath *path;
504 504
      Node *node;
505 505
    };
506 506

	
507 507
    /// \brief The nth arc.
508 508
    ///
509 509
    /// This function looks for the nth arc in O(n) time.
510
    /// \pre n is in the [0..length() - 1] range
510
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
511 511
    const Arc& nth(int n) const {
512 512
      Node *node = first;
513 513
      for (int i = 0; i < n; ++i) {
514 514
        node = node->next;
515 515
      }
516 516
      return node->arc;
517 517
    }
518 518

	
519 519
    /// \brief Initializes arc iterator to point to the nth arc.
520 520
    ArcIt nthIt(int n) const {
521 521
      Node *node = first;
522 522
      for (int i = 0; i < n; ++i) {
523 523
        node = node->next;
524 524
      }
525 525
      return ArcIt(*this, node);
526 526
    }
527 527

	
528 528
    /// \brief Length of the path.
529 529
    int length() const {
530 530
      int len = 0;
531 531
      Node *node = first;
532 532
      while (node != 0) {
533 533
        node = node->next;
534 534
        ++len;
535 535
      }
536 536
      return len;
537 537
    }
538 538

	
539 539
    /// \brief Return true if the path is empty.
540 540
    bool empty() const { return first == 0; }
541 541

	
542 542
    /// \brief Reset the path to an empty one.
543 543
    void clear() {
544 544
      while (first != 0) {
545 545
        last = first->next;
546 546
        alloc.destroy(first);
547 547
        alloc.deallocate(first, 1);
548 548
        first = last;
549 549
      }
550 550
    }
551 551

	
552 552
    /// \brief The first arc of the path
553 553
    const Arc& front() const {
554 554
      return first->arc;
555 555
    }
556 556

	
557 557
    /// \brief Add a new arc before the current path
558 558
    void addFront(const Arc& arc) {
559 559
      Node *node = alloc.allocate(1);
560 560
      alloc.construct(node, Node());
561 561
      node->prev = 0;
562 562
      node->next = first;
563 563
      node->arc = arc;
564 564
      if (first) {
565 565
        first->prev = node;
566 566
        first = node;
567 567
      } else {
568 568
        first = last = node;
569 569
      }
570 570
    }
571 571

	
572 572
    /// \brief Erase the first arc of the path
573 573
    void eraseFront() {
574 574
      Node *node = first;
575 575
      first = first->next;
576 576
      if (first) {
577 577
        first->prev = 0;
578 578
      } else {
579 579
        last = 0;
580 580
      }
581 581
      alloc.destroy(node);
582 582
      alloc.deallocate(node, 1);
583 583
    }
584 584

	
585 585
    /// \brief The last arc of the path.
586 586
    const Arc& back() const {
587 587
      return last->arc;
588 588
    }
589 589

	
590 590
    /// \brief Add a new arc behind the current path.
591 591
    void addBack(const Arc& arc) {
592 592
      Node *node = alloc.allocate(1);
593 593
      alloc.construct(node, Node());
594 594
      node->next = 0;
595 595
      node->prev = last;
596 596
      node->arc = arc;
597 597
      if (last) {
598 598
        last->next = node;
599 599
        last = node;
600 600
      } else {
601 601
        last = first = node;
602 602
      }
603 603
    }
604 604

	
605 605
    /// \brief Erase the last arc of the path
606 606
    void eraseBack() {
... ...
@@ -639,448 +639,466 @@
639 639
    /// It splices \c tpath before the current path and \c tpath
640 640
    /// becomes empty. The time complexity of this function
641 641
    /// is O(1).
642 642
    void spliceFront(ListPath& tpath) {
643 643
      if (first) {
644 644
        if (tpath.first) {
645 645
          first->prev = tpath.last;
646 646
          tpath.last->next = first;
647 647
          first = tpath.first;
648 648
        }
649 649
      } else {
650 650
        first = tpath.first;
651 651
        last = tpath.last;
652 652
      }
653 653
      tpath.first = tpath.last = 0;
654 654
    }
655 655

	
656 656
    /// \brief Splice a path into the current path.
657 657
    ///
658 658
    /// It splices the \c tpath into the current path before the
659 659
    /// position of \c it iterator and \c tpath becomes empty. The
660 660
    /// time complexity of this function is O(1). If the \c it is
661 661
    /// \c INVALID then it will splice behind the current path.
662 662
    void splice(ArcIt it, ListPath& tpath) {
663 663
      if (it.node) {
664 664
        if (tpath.first) {
665 665
          tpath.first->prev = it.node->prev;
666 666
          if (it.node->prev) {
667 667
            it.node->prev->next = tpath.first;
668 668
          } else {
669 669
            first = tpath.first;
670 670
          }
671 671
          it.node->prev = tpath.last;
672 672
          tpath.last->next = it.node;
673 673
        }
674 674
      } else {
675 675
        if (first) {
676 676
          if (tpath.first) {
677 677
            last->next = tpath.first;
678 678
            tpath.first->prev = last;
679 679
            last = tpath.last;
680 680
          }
681 681
        } else {
682 682
          first = tpath.first;
683 683
          last = tpath.last;
684 684
        }
685 685
      }
686 686
      tpath.first = tpath.last = 0;
687 687
    }
688 688

	
689 689
    /// \brief Split the current path.
690 690
    ///
691 691
    /// It splits the current path into two parts. The part before
692 692
    /// the iterator \c it will remain in the current path and the part
693 693
    /// starting with
694 694
    /// \c it will put into \c tpath. If \c tpath have arcs
695 695
    /// before the operation they are removed first.  The time
696 696
    /// complexity of this function is O(1) plus the the time of emtying
697 697
    /// \c tpath. If \c it is \c INVALID then it just clears \c tpath
698 698
    void split(ArcIt it, ListPath& tpath) {
699 699
      tpath.clear();
700 700
      if (it.node) {
701 701
        tpath.first = it.node;
702 702
        tpath.last = last;
703 703
        if (it.node->prev) {
704 704
          last = it.node->prev;
705 705
          last->next = 0;
706 706
        } else {
707 707
          first = last = 0;
708 708
        }
709 709
        it.node->prev = 0;
710 710
      }
711 711
    }
712 712

	
713 713

	
714 714
    typedef True BuildTag;
715 715

	
716 716
    template <typename CPath>
717 717
    void build(const CPath& path) {
718 718
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
719 719
        addBack(it);
720 720
      }
721 721
    }
722 722

	
723 723
    template <typename CPath>
724 724
    void buildRev(const CPath& path) {
725 725
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
726 726
        addFront(it);
727 727
      }
728 728
    }
729 729

	
730 730
  };
731 731

	
732 732
  /// \brief A structure for representing directed paths in a digraph.
733 733
  ///
734 734
  /// A structure for representing directed path in a digraph.
735
  /// \tparam _Digraph The digraph type in which the path is.
735
  /// \tparam GR The digraph type in which the path is.
736 736
  ///
737 737
  /// In a sense, the path can be treated as a list of arcs. The
738 738
  /// lemon path type stores just this list. As a consequence it
739 739
  /// cannot enumerate the nodes in the path and the source node of
740 740
  /// a zero length path is undefined.
741 741
  ///
742 742
  /// This implementation is completly static, i.e. it can be copy constucted
743 743
  /// or copy assigned from another path, but otherwise it cannot be
744 744
  /// modified.
745 745
  ///
746 746
  /// Being the the most memory efficient path type in LEMON,
747 747
  /// it is intented to be
748 748
  /// used when you want to store a large number of paths.
749
  template <typename _Digraph>
749
  template <typename GR>
750 750
  class StaticPath {
751 751
  public:
752 752

	
753
    typedef _Digraph Digraph;
753
    typedef GR Digraph;
754 754
    typedef typename Digraph::Arc Arc;
755 755

	
756 756
    /// \brief Default constructor
757 757
    ///
758 758
    /// Default constructor
759 759
    StaticPath() : len(0), arcs(0) {}
760 760

	
761 761
    /// \brief Template copy constructor
762 762
    ///
763 763
    /// This path can be initialized from any other path type.
764 764
    template <typename CPath>
765 765
    StaticPath(const CPath& cpath) : arcs(0) {
766 766
      copyPath(*this, cpath);
767 767
    }
768 768

	
769 769
    /// \brief Destructor of the path
770 770
    ///
771 771
    /// Destructor of the path
772 772
    ~StaticPath() {
773 773
      if (arcs) delete[] arcs;
774 774
    }
775 775

	
776 776
    /// \brief Template copy assignment
777 777
    ///
778 778
    /// This path can be made equal to any other path type. It simply
779 779
    /// makes a copy of the given path.
780 780
    template <typename CPath>
781 781
    StaticPath& operator=(const CPath& cpath) {
782 782
      copyPath(*this, cpath);
783 783
      return *this;
784 784
    }
785 785

	
786 786
    /// \brief Iterator class to iterate on the arcs of the paths
787 787
    ///
788 788
    /// This class is used to iterate on the arcs of the paths
789 789
    ///
790 790
    /// Of course it converts to Digraph::Arc
791 791
    class ArcIt {
792 792
      friend class StaticPath;
793 793
    public:
794 794
      /// Default constructor
795 795
      ArcIt() {}
796 796
      /// Invalid constructor
797 797
      ArcIt(Invalid) : path(0), idx(-1) {}
798 798
      /// Initializate the constructor to the first arc of path
799 799
      ArcIt(const StaticPath &_path)
800 800
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
801 801

	
802 802
    private:
803 803

	
804 804
      /// Constructor with starting point
805 805
      ArcIt(const StaticPath &_path, int _idx)
806 806
        : idx(_idx), path(&_path) {}
807 807

	
808 808
    public:
809 809

	
810 810
      ///Conversion to Digraph::Arc
811 811
      operator const Arc&() const {
812 812
        return path->nth(idx);
813 813
      }
814 814

	
815 815
      /// Next arc
816 816
      ArcIt& operator++() {
817 817
        ++idx;
818 818
        if (idx >= path->length()) idx = -1;
819 819
        return *this;
820 820
      }
821 821

	
822 822
      /// Comparison operator
823 823
      bool operator==(const ArcIt& e) const { return idx==e.idx; }
824 824
      /// Comparison operator
825 825
      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
826 826
      /// Comparison operator
827 827
      bool operator<(const ArcIt& e) const { return idx<e.idx; }
828 828

	
829 829
    private:
830 830
      const StaticPath *path;
831 831
      int idx;
832 832
    };
833 833

	
834 834
    /// \brief The nth arc.
835 835
    ///
836
    /// \pre n is in the [0..length() - 1] range
836
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
837 837
    const Arc& nth(int n) const {
838 838
      return arcs[n];
839 839
    }
840 840

	
841 841
    /// \brief The arc iterator pointing to the nth arc.
842 842
    ArcIt nthIt(int n) const {
843 843
      return ArcIt(*this, n);
844 844
    }
845 845

	
846 846
    /// \brief The length of the path.
847 847
    int length() const { return len; }
848 848

	
849 849
    /// \brief Return true when the path is empty.
850 850
    int empty() const { return len == 0; }
851 851

	
852 852
    /// \brief Erase all arcs in the digraph.
853 853
    void clear() {
854 854
      len = 0;
855 855
      if (arcs) delete[] arcs;
856 856
      arcs = 0;
857 857
    }
858 858

	
859 859
    /// \brief The first arc of the path.
860 860
    const Arc& front() const {
861 861
      return arcs[0];
862 862
    }
863 863

	
864 864
    /// \brief The last arc of the path.
865 865
    const Arc& back() const {
866 866
      return arcs[len - 1];
867 867
    }
868 868

	
869 869

	
870 870
    typedef True BuildTag;
871 871

	
872 872
    template <typename CPath>
873 873
    void build(const CPath& path) {
874 874
      len = path.length();
875 875
      arcs = new Arc[len];
876 876
      int index = 0;
877 877
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
878 878
        arcs[index] = it;
879 879
        ++index;
880 880
      }
881 881
    }
882 882

	
883 883
    template <typename CPath>
884 884
    void buildRev(const CPath& path) {
885 885
      len = path.length();
886 886
      arcs = new Arc[len];
887 887
      int index = len;
888 888
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
889 889
        --index;
890 890
        arcs[index] = it;
891 891
      }
892 892
    }
893 893

	
894 894
  private:
895 895
    int len;
896 896
    Arc* arcs;
897 897
  };
898 898

	
899 899
  ///////////////////////////////////////////////////////////////////////
900 900
  // Additional utilities
901 901
  ///////////////////////////////////////////////////////////////////////
902 902

	
903 903
  namespace _path_bits {
904 904

	
905 905
    template <typename Path, typename Enable = void>
906 906
    struct RevPathTagIndicator {
907 907
      static const bool value = false;
908 908
    };
909 909

	
910 910
    template <typename Path>
911 911
    struct RevPathTagIndicator<
912 912
      Path,
913 913
      typename enable_if<typename Path::RevPathTag, void>::type
914 914
      > {
915 915
      static const bool value = true;
916 916
    };
917 917

	
918 918
    template <typename Path, typename Enable = void>
919 919
    struct BuildTagIndicator {
920 920
      static const bool value = false;
921 921
    };
922 922

	
923 923
    template <typename Path>
924 924
    struct BuildTagIndicator<
925 925
      Path,
926 926
      typename enable_if<typename Path::BuildTag, void>::type
927 927
    > {
928 928
      static const bool value = true;
929 929
    };
930 930

	
931 931
    template <typename Target, typename Source,
932
              bool buildEnable = BuildTagIndicator<Target>::value,
933
              bool revEnable = RevPathTagIndicator<Source>::value>
934
    struct PathCopySelector {
932
              bool buildEnable = BuildTagIndicator<Target>::value>
933
    struct PathCopySelectorForward {
935 934
      static void copy(Target& target, const Source& source) {
936 935
        target.clear();
937 936
        for (typename Source::ArcIt it(source); it != INVALID; ++it) {
938 937
          target.addBack(it);
939 938
        }
940 939
      }
941 940
    };
942 941

	
943 942
    template <typename Target, typename Source>
944
    struct PathCopySelector<Target, Source, false, true> {
943
    struct PathCopySelectorForward<Target, Source, true> {
944
      static void copy(Target& target, const Source& source) {
945
        target.clear();
946
        target.build(source);
947
      }
948
    };
949

	
950
    template <typename Target, typename Source,
951
              bool buildEnable = BuildTagIndicator<Target>::value>
952
    struct PathCopySelectorBackward {
945 953
      static void copy(Target& target, const Source& source) {
946 954
        target.clear();
947 955
        for (typename Source::RevArcIt it(source); it != INVALID; ++it) {
948 956
          target.addFront(it);
949 957
        }
950 958
      }
951 959
    };
952 960

	
953 961
    template <typename Target, typename Source>
954
    struct PathCopySelector<Target, Source, true, false> {
955
      static void copy(Target& target, const Source& source) {
956
        target.clear();
957
        target.build(source);
958
      }
959
    };
960

	
961
    template <typename Target, typename Source>
962
    struct PathCopySelector<Target, Source, true, true> {
962
    struct PathCopySelectorBackward<Target, Source, true> {
963 963
      static void copy(Target& target, const Source& source) {
964 964
        target.clear();
965 965
        target.buildRev(source);
966 966
      }
967 967
    };
968 968

	
969
    
970
    template <typename Target, typename Source,
971
              bool revEnable = RevPathTagIndicator<Source>::value>
972
    struct PathCopySelector {
973
      static void copy(Target& target, const Source& source) {
974
        PathCopySelectorForward<Target, Source>::copy(target, source);
975
      }      
976
    };
977

	
978
    template <typename Target, typename Source>
979
    struct PathCopySelector<Target, Source, true> {
980
      static void copy(Target& target, const Source& source) {
981
        PathCopySelectorBackward<Target, Source>::copy(target, source);
982
      }      
983
    };
984

	
969 985
  }
970 986

	
971 987

	
972 988
  /// \brief Make a copy of a path.
973 989
  ///
974 990
  ///  This function makes a copy of a path.
975 991
  template <typename Target, typename Source>
976 992
  void copyPath(Target& target, const Source& source) {
977 993
    checkConcept<concepts::PathDumper<typename Source::Digraph>, Source>();
978 994
    _path_bits::PathCopySelector<Target, Source>::copy(target, source);
979 995
  }
980 996

	
981 997
  /// \brief Check the consistency of a path.
982 998
  ///
983 999
  /// This function checks that the target of each arc is the same
984 1000
  /// as the source of the next one.
985 1001
  ///
986 1002
  template <typename Digraph, typename Path>
987 1003
  bool checkPath(const Digraph& digraph, const Path& path) {
988 1004
    typename Path::ArcIt it(path);
989 1005
    if (it == INVALID) return true;
990 1006
    typename Digraph::Node node = digraph.target(it);
991 1007
    ++it;
992 1008
    while (it != INVALID) {
993 1009
      if (digraph.source(it) != node) return false;
994 1010
      node = digraph.target(it);
995 1011
      ++it;
996 1012
    }
997 1013
    return true;
998 1014
  }
999 1015

	
1000 1016
  /// \brief The source of a path
1001 1017
  ///
1002
  /// This function returns the source of the given path.
1018
  /// This function returns the source node of the given path.
1019
  /// If the path is empty, then it returns \c INVALID.
1003 1020
  template <typename Digraph, typename Path>
1004 1021
  typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) {
1005
    return digraph.source(path.front());
1022
    return path.empty() ? INVALID : digraph.source(path.front());
1006 1023
  }
1007 1024

	
1008 1025
  /// \brief The target of a path
1009 1026
  ///
1010
  /// This function returns the target of the given path.
1027
  /// This function returns the target node of the given path.
1028
  /// If the path is empty, then it returns \c INVALID.
1011 1029
  template <typename Digraph, typename Path>
1012 1030
  typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
1013
    return digraph.target(path.back());
1031
    return path.empty() ? INVALID : digraph.target(path.back());
1014 1032
  }
1015 1033

	
1016 1034
  /// \brief Class which helps to iterate through the nodes of a path
1017 1035
  ///
1018 1036
  /// In a sense, the path can be treated as a list of arcs. The
1019 1037
  /// lemon path type stores only this list. As a consequence, it
1020 1038
  /// cannot enumerate the nodes in the path and the zero length paths
1021 1039
  /// cannot have a source node.
1022 1040
  ///
1023 1041
  /// This class implements the node iterator of a path structure. To
1024 1042
  /// provide this feature, the underlying digraph should be passed to
1025 1043
  /// the constructor of the iterator.
1026 1044
  template <typename Path>
1027 1045
  class PathNodeIt {
1028 1046
  private:
1029 1047
    const typename Path::Digraph *_digraph;
1030 1048
    typename Path::ArcIt _it;
1031 1049
    typename Path::Digraph::Node _nd;
1032 1050

	
1033 1051
  public:
1034 1052

	
1035 1053
    typedef typename Path::Digraph Digraph;
1036 1054
    typedef typename Digraph::Node Node;
1037 1055

	
1038 1056
    /// Default constructor
1039 1057
    PathNodeIt() {}
1040 1058
    /// Invalid constructor
1041 1059
    PathNodeIt(Invalid)
1042 1060
      : _digraph(0), _it(INVALID), _nd(INVALID) {}
1043 1061
    /// Constructor
1044 1062
    PathNodeIt(const Digraph& digraph, const Path& path)
1045 1063
      : _digraph(&digraph), _it(path) {
1046 1064
      _nd = (_it != INVALID ? _digraph->source(_it) : INVALID);
1047 1065
    }
1048 1066
    /// Constructor
1049 1067
    PathNodeIt(const Digraph& digraph, const Path& path, const Node& src)
1050 1068
      : _digraph(&digraph), _it(path), _nd(src) {}
1051 1069

	
1052 1070
    ///Conversion to Digraph::Node
1053 1071
    operator Node() const {
1054 1072
      return _nd;
1055 1073
    }
1056 1074

	
1057 1075
    /// Next node
1058 1076
    PathNodeIt& operator++() {
1059 1077
      if (_it == INVALID) _nd = INVALID;
1060 1078
      else {
1061 1079
        _nd = _digraph->target(_it);
1062 1080
        ++_it;
1063 1081
      }
1064 1082
      return *this;
1065 1083
    }
1066 1084

	
1067 1085
    /// Comparison operator
1068 1086
    bool operator==(const PathNodeIt& n) const {
1069 1087
      return _it == n._it && _nd == n._nd;
1070 1088
    }
1071 1089
    /// Comparison operator
1072 1090
    bool operator!=(const PathNodeIt& n) const {
1073 1091
      return _it != n._it || _nd != n._nd;
1074 1092
    }
1075 1093
    /// Comparison operator
1076 1094
    bool operator<(const PathNodeIt& n) const {
1077 1095
      return (_it < n._it && _nd != INVALID);
1078 1096
    }
1079 1097

	
1080 1098
  };
1081 1099

	
1082 1100
  ///@}
1083 1101

	
1084 1102
} // namespace lemon
1085 1103

	
1086 1104
#endif // LEMON_PATH_H
Ignore white space 6 line context
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-2009
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_PREFLOW_H
20 20
#define LEMON_PREFLOW_H
21 21

	
22 22
#include <lemon/tolerance.h>
23 23
#include <lemon/elevator.h>
24 24

	
25 25
/// \file
26 26
/// \ingroup max_flow
27 27
/// \brief Implementation of the preflow algorithm.
28 28

	
29 29
namespace lemon {
30 30

	
31 31
  /// \brief Default traits class of Preflow class.
32 32
  ///
33 33
  /// Default traits class of Preflow class.
34
  /// \tparam _Digraph Digraph type.
35
  /// \tparam _CapacityMap Capacity map type.
36
  template <typename _Digraph, typename _CapacityMap>
34
  /// \tparam GR Digraph type.
35
  /// \tparam CAP Capacity map type.
36
  template <typename GR, typename CAP>
37 37
  struct PreflowDefaultTraits {
38 38

	
39 39
    /// \brief The type of the digraph the algorithm runs on.
40
    typedef _Digraph Digraph;
40
    typedef GR Digraph;
41 41

	
42 42
    /// \brief The type of the map that stores the arc capacities.
43 43
    ///
44 44
    /// The type of the map that stores the arc capacities.
45 45
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
46
    typedef _CapacityMap CapacityMap;
46
    typedef CAP CapacityMap;
47 47

	
48 48
    /// \brief The type of the flow values.
49 49
    typedef typename CapacityMap::Value Value;
50 50

	
51 51
    /// \brief The type of the map that stores the flow values.
52 52
    ///
53 53
    /// The type of the map that stores the flow values.
54 54
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
55
#ifdef DOXYGEN
56
    typedef GR::ArcMap<Value> FlowMap;
57
#else
55 58
    typedef typename Digraph::template ArcMap<Value> FlowMap;
59
#endif
56 60

	
57 61
    /// \brief Instantiates a FlowMap.
58 62
    ///
59 63
    /// This function instantiates a \ref FlowMap.
60
    /// \param digraph The digraph, to which we would like to define
64
    /// \param digraph The digraph for which we would like to define
61 65
    /// the flow map.
62 66
    static FlowMap* createFlowMap(const Digraph& digraph) {
63 67
      return new FlowMap(digraph);
64 68
    }
65 69

	
66 70
    /// \brief The elevator type used by Preflow algorithm.
67 71
    ///
68 72
    /// The elevator type used by Preflow algorithm.
69 73
    ///
70
    /// \sa Elevator
71
    /// \sa LinkedElevator
72
    typedef LinkedElevator<Digraph, typename Digraph::Node> Elevator;
74
    /// \sa Elevator, LinkedElevator
75
#ifdef DOXYGEN
76
    typedef lemon::Elevator<GR, GR::Node> Elevator;
77
#else
78
    typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator;
79
#endif
73 80

	
74 81
    /// \brief Instantiates an Elevator.
75 82
    ///
76 83
    /// This function instantiates an \ref Elevator.
77
    /// \param digraph The digraph, to which we would like to define
84
    /// \param digraph The digraph for which we would like to define
78 85
    /// the elevator.
79 86
    /// \param max_level The maximum level of the elevator.
80 87
    static Elevator* createElevator(const Digraph& digraph, int max_level) {
81 88
      return new Elevator(digraph, max_level);
82 89
    }
83 90

	
84 91
    /// \brief The tolerance used by the algorithm
85 92
    ///
86 93
    /// The tolerance used by the algorithm to handle inexact computation.
87 94
    typedef lemon::Tolerance<Value> Tolerance;
88 95

	
89 96
  };
90 97

	
91 98

	
92 99
  /// \ingroup max_flow
93 100
  ///
94 101
  /// \brief %Preflow algorithm class.
95 102
  ///
96 103
  /// This class provides an implementation of Goldberg-Tarjan's \e preflow
97
  /// \e push-relabel algorithm producing a flow of maximum value in a
98
  /// digraph. The preflow algorithms are the fastest known maximum
99
  /// flow algorithms. The current implementation use a mixture of the
104
  /// \e push-relabel algorithm producing a \ref max_flow
105
  /// "flow of maximum value" in a digraph \ref clrs01algorithms,
106
  /// \ref amo93networkflows, \ref goldberg88newapproach.
107
  /// The preflow algorithms are the fastest known maximum
108
  /// flow algorithms. The current implementation uses a mixture of the
100 109
  /// \e "highest label" and the \e "bound decrease" heuristics.
101 110
  /// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
102 111
  ///
103 112
  /// The algorithm consists of two phases. After the first phase
104 113
  /// the maximum flow value and the minimum cut is obtained. The
105 114
  /// second phase constructs a feasible maximum flow on each arc.
106 115
  ///
107
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
108
  /// \tparam _CapacityMap The type of the capacity map. The default map
109
  /// type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>".
116
  /// \tparam GR The type of the digraph the algorithm runs on.
117
  /// \tparam CAP The type of the capacity map. The default map
118
  /// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
110 119
#ifdef DOXYGEN
111
  template <typename _Digraph, typename _CapacityMap, typename _Traits>
120
  template <typename GR, typename CAP, typename TR>
112 121
#else
113
  template <typename _Digraph,
114
            typename _CapacityMap = typename _Digraph::template ArcMap<int>,
115
            typename _Traits = PreflowDefaultTraits<_Digraph, _CapacityMap> >
122
  template <typename GR,
123
            typename CAP = typename GR::template ArcMap<int>,
124
            typename TR = PreflowDefaultTraits<GR, CAP> >
116 125
#endif
117 126
  class Preflow {
118 127
  public:
119 128

	
120 129
    ///The \ref PreflowDefaultTraits "traits class" of the algorithm.
121
    typedef _Traits Traits;
130
    typedef TR Traits;
122 131
    ///The type of the digraph the algorithm runs on.
123 132
    typedef typename Traits::Digraph Digraph;
124 133
    ///The type of the capacity map.
125 134
    typedef typename Traits::CapacityMap CapacityMap;
126 135
    ///The type of the flow values.
127 136
    typedef typename Traits::Value Value;
128 137

	
129 138
    ///The type of the flow map.
130 139
    typedef typename Traits::FlowMap FlowMap;
131 140
    ///The type of the elevator.
132 141
    typedef typename Traits::Elevator Elevator;
133 142
    ///The type of the tolerance.
134 143
    typedef typename Traits::Tolerance Tolerance;
135 144

	
136 145
  private:
137 146

	
138 147
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
139 148

	
140 149
    const Digraph& _graph;
141 150
    const CapacityMap* _capacity;
142 151

	
143 152
    int _node_num;
144 153

	
145 154
    Node _source, _target;
146 155

	
147 156
    FlowMap* _flow;
148 157
    bool _local_flow;
149 158

	
150 159
    Elevator* _level;
151 160
    bool _local_level;
152 161

	
153 162
    typedef typename Digraph::template NodeMap<Value> ExcessMap;
154 163
    ExcessMap* _excess;
155 164

	
156 165
    Tolerance _tolerance;
157 166

	
158 167
    bool _phase;
159 168

	
160 169

	
161 170
    void createStructures() {
162 171
      _node_num = countNodes(_graph);
163 172

	
164 173
      if (!_flow) {
165 174
        _flow = Traits::createFlowMap(_graph);
166 175
        _local_flow = true;
167 176
      }
168 177
      if (!_level) {
169 178
        _level = Traits::createElevator(_graph, _node_num);
170 179
        _local_level = true;
171 180
      }
172 181
      if (!_excess) {
173 182
        _excess = new ExcessMap(_graph);
174 183
      }
175 184
    }
176 185

	
177 186
    void destroyStructures() {
178 187
      if (_local_flow) {
179 188
        delete _flow;
180 189
      }
181 190
      if (_local_level) {
182 191
        delete _level;
183 192
      }
184 193
      if (_excess) {
185 194
        delete _excess;
186 195
      }
187 196
    }
188 197

	
189 198
  public:
190 199

	
191 200
    typedef Preflow Create;
192 201

	
193 202
    ///\name Named Template Parameters
194 203

	
195 204
    ///@{
196 205

	
197
    template <typename _FlowMap>
206
    template <typename T>
198 207
    struct SetFlowMapTraits : public Traits {
199
      typedef _FlowMap FlowMap;
208
      typedef T FlowMap;
200 209
      static FlowMap *createFlowMap(const Digraph&) {
201 210
        LEMON_ASSERT(false, "FlowMap is not initialized");
202 211
        return 0; // ignore warnings
203 212
      }
204 213
    };
205 214

	
206 215
    /// \brief \ref named-templ-param "Named parameter" for setting
207 216
    /// FlowMap type
208 217
    ///
209 218
    /// \ref named-templ-param "Named parameter" for setting FlowMap
210 219
    /// type.
211
    template <typename _FlowMap>
220
    template <typename T>
212 221
    struct SetFlowMap
213
      : public Preflow<Digraph, CapacityMap, SetFlowMapTraits<_FlowMap> > {
222
      : public Preflow<Digraph, CapacityMap, SetFlowMapTraits<T> > {
214 223
      typedef Preflow<Digraph, CapacityMap,
215
                      SetFlowMapTraits<_FlowMap> > Create;
224
                      SetFlowMapTraits<T> > Create;
216 225
    };
217 226

	
218
    template <typename _Elevator>
227
    template <typename T>
219 228
    struct SetElevatorTraits : public Traits {
220
      typedef _Elevator Elevator;
229
      typedef T Elevator;
221 230
      static Elevator *createElevator(const Digraph&, int) {
222 231
        LEMON_ASSERT(false, "Elevator is not initialized");
223 232
        return 0; // ignore warnings
224 233
      }
225 234
    };
226 235

	
227 236
    /// \brief \ref named-templ-param "Named parameter" for setting
228 237
    /// Elevator type
229 238
    ///
230 239
    /// \ref named-templ-param "Named parameter" for setting Elevator
231 240
    /// type. If this named parameter is used, then an external
232 241
    /// elevator object must be passed to the algorithm using the
233 242
    /// \ref elevator(Elevator&) "elevator()" function before calling
234 243
    /// \ref run() or \ref init().
235 244
    /// \sa SetStandardElevator
236
    template <typename _Elevator>
245
    template <typename T>
237 246
    struct SetElevator
238
      : public Preflow<Digraph, CapacityMap, SetElevatorTraits<_Elevator> > {
247
      : public Preflow<Digraph, CapacityMap, SetElevatorTraits<T> > {
239 248
      typedef Preflow<Digraph, CapacityMap,
240
                      SetElevatorTraits<_Elevator> > Create;
249
                      SetElevatorTraits<T> > Create;
241 250
    };
242 251

	
243
    template <typename _Elevator>
252
    template <typename T>
244 253
    struct SetStandardElevatorTraits : public Traits {
245
      typedef _Elevator Elevator;
254
      typedef T Elevator;
246 255
      static Elevator *createElevator(const Digraph& digraph, int max_level) {
247 256
        return new Elevator(digraph, max_level);
248 257
      }
249 258
    };
250 259

	
251 260
    /// \brief \ref named-templ-param "Named parameter" for setting
252 261
    /// Elevator type with automatic allocation
253 262
    ///
254 263
    /// \ref named-templ-param "Named parameter" for setting Elevator
255 264
    /// type with automatic allocation.
256 265
    /// The Elevator should have standard constructor interface to be
257 266
    /// able to automatically created by the algorithm (i.e. the
258 267
    /// digraph and the maximum level should be passed to it).
259 268
    /// However an external elevator object could also be passed to the
260 269
    /// algorithm with the \ref elevator(Elevator&) "elevator()" function
261 270
    /// before calling \ref run() or \ref init().
262 271
    /// \sa SetElevator
263
    template <typename _Elevator>
272
    template <typename T>
264 273
    struct SetStandardElevator
265 274
      : public Preflow<Digraph, CapacityMap,
266
                       SetStandardElevatorTraits<_Elevator> > {
275
                       SetStandardElevatorTraits<T> > {
267 276
      typedef Preflow<Digraph, CapacityMap,
268
                      SetStandardElevatorTraits<_Elevator> > Create;
277
                      SetStandardElevatorTraits<T> > Create;
269 278
    };
270 279

	
271 280
    /// @}
272 281

	
273 282
  protected:
274 283

	
275 284
    Preflow() {}
276 285

	
277 286
  public:
278 287

	
279 288

	
280 289
    /// \brief The constructor of the class.
281 290
    ///
282 291
    /// The constructor of the class.
283 292
    /// \param digraph The digraph the algorithm runs on.
284 293
    /// \param capacity The capacity of the arcs.
285 294
    /// \param source The source node.
286 295
    /// \param target The target node.
287 296
    Preflow(const Digraph& digraph, const CapacityMap& capacity,
288 297
            Node source, Node target)
289 298
      : _graph(digraph), _capacity(&capacity),
290 299
        _node_num(0), _source(source), _target(target),
291 300
        _flow(0), _local_flow(false),
292 301
        _level(0), _local_level(false),
293 302
        _excess(0), _tolerance(), _phase() {}
294 303

	
295 304
    /// \brief Destructor.
296 305
    ///
297 306
    /// Destructor.
298 307
    ~Preflow() {
299 308
      destroyStructures();
300 309
    }
301 310

	
302 311
    /// \brief Sets the capacity map.
303 312
    ///
304 313
    /// Sets the capacity map.
305 314
    /// \return <tt>(*this)</tt>
306 315
    Preflow& capacityMap(const CapacityMap& map) {
307 316
      _capacity = &map;
308 317
      return *this;
309 318
    }
310 319

	
311 320
    /// \brief Sets the flow map.
312 321
    ///
313 322
    /// Sets the flow map.
314 323
    /// If you don't use this function before calling \ref run() or
315 324
    /// \ref init(), an instance will be allocated automatically.
316 325
    /// The destructor deallocates this automatically allocated map,
317 326
    /// of course.
318 327
    /// \return <tt>(*this)</tt>
319 328
    Preflow& flowMap(FlowMap& map) {
320 329
      if (_local_flow) {
321 330
        delete _flow;
322 331
        _local_flow = false;
323 332
      }
324 333
      _flow = &map;
325 334
      return *this;
326 335
    }
327 336

	
328 337
    /// \brief Sets the source node.
329 338
    ///
330 339
    /// Sets the source node.
331 340
    /// \return <tt>(*this)</tt>
332 341
    Preflow& source(const Node& node) {
333 342
      _source = node;
334 343
      return *this;
335 344
    }
336 345

	
337 346
    /// \brief Sets the target node.
338 347
    ///
339 348
    /// Sets the target node.
340 349
    /// \return <tt>(*this)</tt>
341 350
    Preflow& target(const Node& node) {
342 351
      _target = node;
343 352
      return *this;
344 353
    }
345 354

	
346 355
    /// \brief Sets the elevator used by algorithm.
347 356
    ///
348 357
    /// Sets the elevator used by algorithm.
349 358
    /// If you don't use this function before calling \ref run() or
350 359
    /// \ref init(), an instance will be allocated automatically.
351 360
    /// The destructor deallocates this automatically allocated elevator,
352 361
    /// of course.
353 362
    /// \return <tt>(*this)</tt>
354 363
    Preflow& elevator(Elevator& elevator) {
355 364
      if (_local_level) {
356 365
        delete _level;
357 366
        _local_level = false;
358 367
      }
359 368
      _level = &elevator;
360 369
      return *this;
361 370
    }
362 371

	
363 372
    /// \brief Returns a const reference to the elevator.
364 373
    ///
365 374
    /// Returns a const reference to the elevator.
366 375
    ///
367 376
    /// \pre Either \ref run() or \ref init() must be called before
368 377
    /// using this function.
369 378
    const Elevator& elevator() const {
370 379
      return *_level;
371 380
    }
372 381

	
373
    /// \brief Sets the tolerance used by algorithm.
382
    /// \brief Sets the tolerance used by the algorithm.
374 383
    ///
375
    /// Sets the tolerance used by algorithm.
376
    Preflow& tolerance(const Tolerance& tolerance) const {
384
    /// Sets the tolerance object used by the algorithm.
385
    /// \return <tt>(*this)</tt>
386
    Preflow& tolerance(const Tolerance& tolerance) {
377 387
      _tolerance = tolerance;
378 388
      return *this;
379 389
    }
380 390

	
381 391
    /// \brief Returns a const reference to the tolerance.
382 392
    ///
383
    /// Returns a const reference to the tolerance.
393
    /// Returns a const reference to the tolerance object used by
394
    /// the algorithm.
384 395
    const Tolerance& tolerance() const {
385
      return tolerance;
396
      return _tolerance;
386 397
    }
387 398

	
388 399
    /// \name Execution Control
389 400
    /// The simplest way to execute the preflow algorithm is to use
390 401
    /// \ref run() or \ref runMinCut().\n
391
    /// If you need more control on the initial solution or the execution,
392
    /// first you have to call one of the \ref init() functions, then
402
    /// If you need better control on the initial solution or the execution,
403
    /// you have to call one of the \ref init() functions first, then
393 404
    /// \ref startFirstPhase() and if you need it \ref startSecondPhase().
394 405

	
395 406
    ///@{
396 407

	
397 408
    /// \brief Initializes the internal data structures.
398 409
    ///
399 410
    /// Initializes the internal data structures and sets the initial
400 411
    /// flow to zero on each arc.
401 412
    void init() {
402 413
      createStructures();
403 414

	
404 415
      _phase = true;
405 416
      for (NodeIt n(_graph); n != INVALID; ++n) {
406
        _excess->set(n, 0);
417
        (*_excess)[n] = 0;
407 418
      }
408 419

	
409 420
      for (ArcIt e(_graph); e != INVALID; ++e) {
410 421
        _flow->set(e, 0);
411 422
      }
412 423

	
413 424
      typename Digraph::template NodeMap<bool> reached(_graph, false);
414 425

	
415 426
      _level->initStart();
416 427
      _level->initAddItem(_target);
417 428

	
418 429
      std::vector<Node> queue;
419
      reached.set(_source, true);
430
      reached[_source] = true;
420 431

	
421 432
      queue.push_back(_target);
422
      reached.set(_target, true);
433
      reached[_target] = true;
423 434
      while (!queue.empty()) {
424 435
        _level->initNewLevel();
425 436
        std::vector<Node> nqueue;
426 437
        for (int i = 0; i < int(queue.size()); ++i) {
427 438
          Node n = queue[i];
428 439
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
429 440
            Node u = _graph.source(e);
430 441
            if (!reached[u] && _tolerance.positive((*_capacity)[e])) {
431
              reached.set(u, true);
442
              reached[u] = true;
432 443
              _level->initAddItem(u);
433 444
              nqueue.push_back(u);
434 445
            }
435 446
          }
436 447
        }
437 448
        queue.swap(nqueue);
438 449
      }
439 450
      _level->initFinish();
440 451

	
441 452
      for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
442 453
        if (_tolerance.positive((*_capacity)[e])) {
443 454
          Node u = _graph.target(e);
444 455
          if ((*_level)[u] == _level->maxLevel()) continue;
445 456
          _flow->set(e, (*_capacity)[e]);
446
          _excess->set(u, (*_excess)[u] + (*_capacity)[e]);
457
          (*_excess)[u] += (*_capacity)[e];
447 458
          if (u != _target && !_level->active(u)) {
448 459
            _level->activate(u);
449 460
          }
450 461
        }
451 462
      }
452 463
    }
453 464

	
454 465
    /// \brief Initializes the internal data structures using the
455 466
    /// given flow map.
456 467
    ///
457 468
    /// Initializes the internal data structures and sets the initial
458 469
    /// flow to the given \c flowMap. The \c flowMap should contain a
459 470
    /// flow or at least a preflow, i.e. at each node excluding the
460 471
    /// source node the incoming flow should greater or equal to the
461 472
    /// outgoing flow.
462 473
    /// \return \c false if the given \c flowMap is not a preflow.
463 474
    template <typename FlowMap>
464 475
    bool init(const FlowMap& flowMap) {
465 476
      createStructures();
466 477

	
467 478
      for (ArcIt e(_graph); e != INVALID; ++e) {
468 479
        _flow->set(e, flowMap[e]);
469 480
      }
470 481

	
471 482
      for (NodeIt n(_graph); n != INVALID; ++n) {
472 483
        Value excess = 0;
473 484
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
474 485
          excess += (*_flow)[e];
475 486
        }
476 487
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
477 488
          excess -= (*_flow)[e];
478 489
        }
479 490
        if (excess < 0 && n != _source) return false;
480
        _excess->set(n, excess);
491
        (*_excess)[n] = excess;
481 492
      }
482 493

	
483 494
      typename Digraph::template NodeMap<bool> reached(_graph, false);
484 495

	
485 496
      _level->initStart();
486 497
      _level->initAddItem(_target);
487 498

	
488 499
      std::vector<Node> queue;
489
      reached.set(_source, true);
500
      reached[_source] = true;
490 501

	
491 502
      queue.push_back(_target);
492
      reached.set(_target, true);
503
      reached[_target] = true;
493 504
      while (!queue.empty()) {
494 505
        _level->initNewLevel();
495 506
        std::vector<Node> nqueue;
496 507
        for (int i = 0; i < int(queue.size()); ++i) {
497 508
          Node n = queue[i];
498 509
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
499 510
            Node u = _graph.source(e);
500 511
            if (!reached[u] &&
501 512
                _tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
502
              reached.set(u, true);
513
              reached[u] = true;
503 514
              _level->initAddItem(u);
504 515
              nqueue.push_back(u);
505 516
            }
506 517
          }
507 518
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
508 519
            Node v = _graph.target(e);
509 520
            if (!reached[v] && _tolerance.positive((*_flow)[e])) {
510
              reached.set(v, true);
521
              reached[v] = true;
511 522
              _level->initAddItem(v);
512 523
              nqueue.push_back(v);
513 524
            }
514 525
          }
515 526
        }
516 527
        queue.swap(nqueue);
517 528
      }
518 529
      _level->initFinish();
519 530

	
520 531
      for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
521 532
        Value rem = (*_capacity)[e] - (*_flow)[e];
522 533
        if (_tolerance.positive(rem)) {
523 534
          Node u = _graph.target(e);
524 535
          if ((*_level)[u] == _level->maxLevel()) continue;
525 536
          _flow->set(e, (*_capacity)[e]);
526
          _excess->set(u, (*_excess)[u] + rem);
537
          (*_excess)[u] += rem;
527 538
          if (u != _target && !_level->active(u)) {
528 539
            _level->activate(u);
529 540
          }
530 541
        }
531 542
      }
532 543
      for (InArcIt e(_graph, _source); e != INVALID; ++e) {
533 544
        Value rem = (*_flow)[e];
534 545
        if (_tolerance.positive(rem)) {
535 546
          Node v = _graph.source(e);
536 547
          if ((*_level)[v] == _level->maxLevel()) continue;
537 548
          _flow->set(e, 0);
538
          _excess->set(v, (*_excess)[v] + rem);
549
          (*_excess)[v] += rem;
539 550
          if (v != _target && !_level->active(v)) {
540 551
            _level->activate(v);
541 552
          }
542 553
        }
543 554
      }
544 555
      return true;
545 556
    }
546 557

	
547 558
    /// \brief Starts the first phase of the preflow algorithm.
548 559
    ///
549 560
    /// The preflow algorithm consists of two phases, this method runs
550 561
    /// the first phase. After the first phase the maximum flow value
551 562
    /// and a minimum value cut can already be computed, although a
552 563
    /// maximum flow is not yet obtained. So after calling this method
553 564
    /// \ref flowValue() returns the value of a maximum flow and \ref
554 565
    /// minCut() returns a minimum cut.
555 566
    /// \pre One of the \ref init() functions must be called before
556 567
    /// using this function.
557 568
    void startFirstPhase() {
558 569
      _phase = true;
559 570

	
560 571
      Node n = _level->highestActive();
561 572
      int level = _level->highestActiveLevel();
562 573
      while (n != INVALID) {
563 574
        int num = _node_num;
564 575

	
565 576
        while (num > 0 && n != INVALID) {
566 577
          Value excess = (*_excess)[n];
567 578
          int new_level = _level->maxLevel();
568 579

	
569 580
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
570 581
            Value rem = (*_capacity)[e] - (*_flow)[e];
571 582
            if (!_tolerance.positive(rem)) continue;
572 583
            Node v = _graph.target(e);
573 584
            if ((*_level)[v] < level) {
574 585
              if (!_level->active(v) && v != _target) {
575 586
                _level->activate(v);
576 587
              }
577 588
              if (!_tolerance.less(rem, excess)) {
578 589
                _flow->set(e, (*_flow)[e] + excess);
579
                _excess->set(v, (*_excess)[v] + excess);
590
                (*_excess)[v] += excess;
580 591
                excess = 0;
581 592
                goto no_more_push_1;
582 593
              } else {
583 594
                excess -= rem;
584
                _excess->set(v, (*_excess)[v] + rem);
595
                (*_excess)[v] += rem;
585 596
                _flow->set(e, (*_capacity)[e]);
586 597
              }
587 598
            } else if (new_level > (*_level)[v]) {
588 599
              new_level = (*_level)[v];
589 600
            }
590 601
          }
591 602

	
592 603
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
593 604
            Value rem = (*_flow)[e];
594 605
            if (!_tolerance.positive(rem)) continue;
595 606
            Node v = _graph.source(e);
596 607
            if ((*_level)[v] < level) {
597 608
              if (!_level->active(v) && v != _target) {
598 609
                _level->activate(v);
599 610
              }
600 611
              if (!_tolerance.less(rem, excess)) {
601 612
                _flow->set(e, (*_flow)[e] - excess);
602
                _excess->set(v, (*_excess)[v] + excess);
613
                (*_excess)[v] += excess;
603 614
                excess = 0;
604 615
                goto no_more_push_1;
605 616
              } else {
606 617
                excess -= rem;
607
                _excess->set(v, (*_excess)[v] + rem);
618
                (*_excess)[v] += rem;
608 619
                _flow->set(e, 0);
609 620
              }
610 621
            } else if (new_level > (*_level)[v]) {
611 622
              new_level = (*_level)[v];
612 623
            }
613 624
          }
614 625

	
615 626
        no_more_push_1:
616 627

	
617
          _excess->set(n, excess);
628
          (*_excess)[n] = excess;
618 629

	
619 630
          if (excess != 0) {
620 631
            if (new_level + 1 < _level->maxLevel()) {
621 632
              _level->liftHighestActive(new_level + 1);
622 633
            } else {
623 634
              _level->liftHighestActiveToTop();
624 635
            }
625 636
            if (_level->emptyLevel(level)) {
626 637
              _level->liftToTop(level);
627 638
            }
628 639
          } else {
629 640
            _level->deactivate(n);
630 641
          }
631 642

	
632 643
          n = _level->highestActive();
633 644
          level = _level->highestActiveLevel();
634 645
          --num;
635 646
        }
636 647

	
637 648
        num = _node_num * 20;
638 649
        while (num > 0 && n != INVALID) {
639 650
          Value excess = (*_excess)[n];
640 651
          int new_level = _level->maxLevel();
641 652

	
642 653
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
643 654
            Value rem = (*_capacity)[e] - (*_flow)[e];
644 655
            if (!_tolerance.positive(rem)) continue;
645 656
            Node v = _graph.target(e);
646 657
            if ((*_level)[v] < level) {
647 658
              if (!_level->active(v) && v != _target) {
648 659
                _level->activate(v);
649 660
              }
650 661
              if (!_tolerance.less(rem, excess)) {
651 662
                _flow->set(e, (*_flow)[e] + excess);
652
                _excess->set(v, (*_excess)[v] + excess);
663
                (*_excess)[v] += excess;
653 664
                excess = 0;
654 665
                goto no_more_push_2;
655 666
              } else {
656 667
                excess -= rem;
657
                _excess->set(v, (*_excess)[v] + rem);
668
                (*_excess)[v] += rem;
658 669
                _flow->set(e, (*_capacity)[e]);
659 670
              }
660 671
            } else if (new_level > (*_level)[v]) {
661 672
              new_level = (*_level)[v];
662 673
            }
663 674
          }
664 675

	
665 676
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
666 677
            Value rem = (*_flow)[e];
667 678
            if (!_tolerance.positive(rem)) continue;
668 679
            Node v = _graph.source(e);
669 680
            if ((*_level)[v] < level) {
670 681
              if (!_level->active(v) && v != _target) {
671 682
                _level->activate(v);
672 683
              }
673 684
              if (!_tolerance.less(rem, excess)) {
674 685
                _flow->set(e, (*_flow)[e] - excess);
675
                _excess->set(v, (*_excess)[v] + excess);
686
                (*_excess)[v] += excess;
676 687
                excess = 0;
677 688
                goto no_more_push_2;
678 689
              } else {
679 690
                excess -= rem;
680
                _excess->set(v, (*_excess)[v] + rem);
691
                (*_excess)[v] += rem;
681 692
                _flow->set(e, 0);
682 693
              }
683 694
            } else if (new_level > (*_level)[v]) {
684 695
              new_level = (*_level)[v];
685 696
            }
686 697
          }
687 698

	
688 699
        no_more_push_2:
689 700

	
690
          _excess->set(n, excess);
701
          (*_excess)[n] = excess;
691 702

	
692 703
          if (excess != 0) {
693 704
            if (new_level + 1 < _level->maxLevel()) {
694 705
              _level->liftActiveOn(level, new_level + 1);
695 706
            } else {
696 707
              _level->liftActiveToTop(level);
697 708
            }
698 709
            if (_level->emptyLevel(level)) {
699 710
              _level->liftToTop(level);
700 711
            }
701 712
          } else {
702 713
            _level->deactivate(n);
703 714
          }
704 715

	
705 716
          while (level >= 0 && _level->activeFree(level)) {
706 717
            --level;
707 718
          }
708 719
          if (level == -1) {
709 720
            n = _level->highestActive();
710 721
            level = _level->highestActiveLevel();
711 722
          } else {
712 723
            n = _level->activeOn(level);
713 724
          }
714 725
          --num;
715 726
        }
716 727
      }
717 728
    }
718 729

	
719 730
    /// \brief Starts the second phase of the preflow algorithm.
720 731
    ///
721 732
    /// The preflow algorithm consists of two phases, this method runs
722 733
    /// the second phase. After calling one of the \ref init() functions
723 734
    /// and \ref startFirstPhase() and then \ref startSecondPhase(),
724 735
    /// \ref flowMap() returns a maximum flow, \ref flowValue() returns the
725 736
    /// value of a maximum flow, \ref minCut() returns a minimum cut
726 737
    /// \pre One of the \ref init() functions and \ref startFirstPhase()
727 738
    /// must be called before using this function.
728 739
    void startSecondPhase() {
729 740
      _phase = false;
730 741

	
731 742
      typename Digraph::template NodeMap<bool> reached(_graph);
732 743
      for (NodeIt n(_graph); n != INVALID; ++n) {
733
        reached.set(n, (*_level)[n] < _level->maxLevel());
744
        reached[n] = (*_level)[n] < _level->maxLevel();
734 745
      }
735 746

	
736 747
      _level->initStart();
737 748
      _level->initAddItem(_source);
738 749

	
739 750
      std::vector<Node> queue;
740 751
      queue.push_back(_source);
741
      reached.set(_source, true);
752
      reached[_source] = true;
742 753

	
743 754
      while (!queue.empty()) {
744 755
        _level->initNewLevel();
745 756
        std::vector<Node> nqueue;
746 757
        for (int i = 0; i < int(queue.size()); ++i) {
747 758
          Node n = queue[i];
748 759
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
749 760
            Node v = _graph.target(e);
750 761
            if (!reached[v] && _tolerance.positive((*_flow)[e])) {
751
              reached.set(v, true);
762
              reached[v] = true;
752 763
              _level->initAddItem(v);
753 764
              nqueue.push_back(v);
754 765
            }
755 766
          }
756 767
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
757 768
            Node u = _graph.source(e);
758 769
            if (!reached[u] &&
759 770
                _tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
760
              reached.set(u, true);
771
              reached[u] = true;
761 772
              _level->initAddItem(u);
762 773
              nqueue.push_back(u);
763 774
            }
764 775
          }
765 776
        }
766 777
        queue.swap(nqueue);
767 778
      }
768 779
      _level->initFinish();
769 780

	
770 781
      for (NodeIt n(_graph); n != INVALID; ++n) {
771 782
        if (!reached[n]) {
772 783
          _level->dirtyTopButOne(n);
773 784
        } else if ((*_excess)[n] > 0 && _target != n) {
774 785
          _level->activate(n);
775 786
        }
776 787
      }
777 788

	
778 789
      Node n;
779 790
      while ((n = _level->highestActive()) != INVALID) {
780 791
        Value excess = (*_excess)[n];
781 792
        int level = _level->highestActiveLevel();
782 793
        int new_level = _level->maxLevel();
783 794

	
784 795
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
785 796
          Value rem = (*_capacity)[e] - (*_flow)[e];
786 797
          if (!_tolerance.positive(rem)) continue;
787 798
          Node v = _graph.target(e);
788 799
          if ((*_level)[v] < level) {
789 800
            if (!_level->active(v) && v != _source) {
790 801
              _level->activate(v);
791 802
            }
792 803
            if (!_tolerance.less(rem, excess)) {
793 804
              _flow->set(e, (*_flow)[e] + excess);
794
              _excess->set(v, (*_excess)[v] + excess);
805
              (*_excess)[v] += excess;
795 806
              excess = 0;
796 807
              goto no_more_push;
797 808
            } else {
798 809
              excess -= rem;
799
              _excess->set(v, (*_excess)[v] + rem);
810
              (*_excess)[v] += rem;
800 811
              _flow->set(e, (*_capacity)[e]);
801 812
            }
802 813
          } else if (new_level > (*_level)[v]) {
803 814
            new_level = (*_level)[v];
804 815
          }
805 816
        }
806 817

	
807 818
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
808 819
          Value rem = (*_flow)[e];
809 820
          if (!_tolerance.positive(rem)) continue;
810 821
          Node v = _graph.source(e);
811 822
          if ((*_level)[v] < level) {
812 823
            if (!_level->active(v) && v != _source) {
813 824
              _level->activate(v);
814 825
            }
815 826
            if (!_tolerance.less(rem, excess)) {
816 827
              _flow->set(e, (*_flow)[e] - excess);
817
              _excess->set(v, (*_excess)[v] + excess);
828
              (*_excess)[v] += excess;
818 829
              excess = 0;
819 830
              goto no_more_push;
820 831
            } else {
821 832
              excess -= rem;
822
              _excess->set(v, (*_excess)[v] + rem);
833
              (*_excess)[v] += rem;
823 834
              _flow->set(e, 0);
824 835
            }
825 836
          } else if (new_level > (*_level)[v]) {
826 837
            new_level = (*_level)[v];
827 838
          }
828 839
        }
829 840

	
830 841
      no_more_push:
831 842

	
832
        _excess->set(n, excess);
843
        (*_excess)[n] = excess;
833 844

	
834 845
        if (excess != 0) {
835 846
          if (new_level + 1 < _level->maxLevel()) {
836 847
            _level->liftHighestActive(new_level + 1);
837 848
          } else {
838 849
            // Calculation error
839 850
            _level->liftHighestActiveToTop();
840 851
          }
841 852
          if (_level->emptyLevel(level)) {
842 853
            // Calculation error
843 854
            _level->liftToTop(level);
844 855
          }
845 856
        } else {
846 857
          _level->deactivate(n);
847 858
        }
848 859

	
849 860
      }
850 861
    }
851 862

	
852 863
    /// \brief Runs the preflow algorithm.
853 864
    ///
854 865
    /// Runs the preflow algorithm.
855 866
    /// \note pf.run() is just a shortcut of the following code.
856 867
    /// \code
857 868
    ///   pf.init();
858 869
    ///   pf.startFirstPhase();
859 870
    ///   pf.startSecondPhase();
860 871
    /// \endcode
861 872
    void run() {
862 873
      init();
863 874
      startFirstPhase();
864 875
      startSecondPhase();
865 876
    }
866 877

	
867 878
    /// \brief Runs the preflow algorithm to compute the minimum cut.
868 879
    ///
869 880
    /// Runs the preflow algorithm to compute the minimum cut.
870 881
    /// \note pf.runMinCut() is just a shortcut of the following code.
871 882
    /// \code
872 883
    ///   pf.init();
873 884
    ///   pf.startFirstPhase();
874 885
    /// \endcode
875 886
    void runMinCut() {
876 887
      init();
877 888
      startFirstPhase();
878 889
    }
879 890

	
880 891
    /// @}
881 892

	
882 893
    /// \name Query Functions
883 894
    /// The results of the preflow algorithm can be obtained using these
884 895
    /// functions.\n
885 896
    /// Either one of the \ref run() "run*()" functions or one of the
886 897
    /// \ref startFirstPhase() "start*()" functions should be called
887 898
    /// before using them.
888 899

	
889 900
    ///@{
890 901

	
891 902
    /// \brief Returns the value of the maximum flow.
892 903
    ///
893 904
    /// Returns the value of the maximum flow by returning the excess
894 905
    /// of the target node. This value equals to the value of
895 906
    /// the maximum flow already after the first phase of the algorithm.
896 907
    ///
897 908
    /// \pre Either \ref run() or \ref init() must be called before
898 909
    /// using this function.
899 910
    Value flowValue() const {
900 911
      return (*_excess)[_target];
901 912
    }
902 913

	
903
    /// \brief Returns the flow on the given arc.
914
    /// \brief Returns the flow value on the given arc.
904 915
    ///
905
    /// Returns the flow on the given arc. This method can
916
    /// Returns the flow value on the given arc. This method can
906 917
    /// be called after the second phase of the algorithm.
907 918
    ///
908 919
    /// \pre Either \ref run() or \ref init() must be called before
909 920
    /// using this function.
910 921
    Value flow(const Arc& arc) const {
911 922
      return (*_flow)[arc];
912 923
    }
913 924

	
914 925
    /// \brief Returns a const reference to the flow map.
915 926
    ///
916 927
    /// Returns a const reference to the arc map storing the found flow.
917 928
    /// This method can be called after the second phase of the algorithm.
918 929
    ///
919 930
    /// \pre Either \ref run() or \ref init() must be called before
920 931
    /// using this function.
921 932
    const FlowMap& flowMap() const {
922 933
      return *_flow;
923 934
    }
924 935

	
925 936
    /// \brief Returns \c true when the node is on the source side of the
926 937
    /// minimum cut.
927 938
    ///
928 939
    /// Returns true when the node is on the source side of the found
929 940
    /// minimum cut. This method can be called both after running \ref
930 941
    /// startFirstPhase() and \ref startSecondPhase().
931 942
    ///
932 943
    /// \pre Either \ref run() or \ref init() must be called before
933 944
    /// using this function.
934 945
    bool minCut(const Node& node) const {
935 946
      return ((*_level)[node] == _level->maxLevel()) == _phase;
936 947
    }
937 948

	
938 949
    /// \brief Gives back a minimum value cut.
939 950
    ///
940 951
    /// Sets \c cutMap to the characteristic vector of a minimum value
941 952
    /// cut. \c cutMap should be a \ref concepts::WriteMap "writable"
942 953
    /// node map with \c bool (or convertible) value type.
943 954
    ///
944 955
    /// This method can be called both after running \ref startFirstPhase()
945 956
    /// and \ref startSecondPhase(). The result after the second phase
946 957
    /// could be slightly different if inexact computation is used.
947 958
    ///
948 959
    /// \note This function calls \ref minCut() for each node, so it runs in
949
    /// \f$O(n)\f$ time.
960
    /// O(n) time.
950 961
    ///
951 962
    /// \pre Either \ref run() or \ref init() must be called before
952 963
    /// using this function.
953 964
    template <typename CutMap>
954 965
    void minCutMap(CutMap& cutMap) const {
955 966
      for (NodeIt n(_graph); n != INVALID; ++n) {
956 967
        cutMap.set(n, minCut(n));
957 968
      }
958 969
    }
959 970

	
960 971
    /// @}
961 972
  };
962 973
}
963 974

	
964 975
#endif
Ignore white space 6 line context
... ...
@@ -112,197 +112,197 @@
112 112
          return first;
113 113
        }
114 114
        std::iter_swap(first, last);
115 115
        ++first;
116 116
      }
117 117
    }
118 118

	
119 119
    template <typename Value, typename Iterator, typename Functor>
120 120
    void radixIntroSort(Iterator first, Iterator last,
121 121
                        Functor functor, Value mask) {
122 122
      while (mask != 0 && last - first > 1) {
123 123
        Iterator cut = radixSortPartition(first, last, functor, mask);
124 124
        mask >>= 1;
125 125
        radixIntroSort(first, cut, functor, mask);
126 126
        first = cut;
127 127
      }
128 128
    }
129 129

	
130 130
    template <typename Value, typename Iterator, typename Functor>
131 131
    void radixSignedSort(Iterator first, Iterator last, Functor functor) {
132 132

	
133 133
      Iterator cut = radixSortSignPartition(first, last, functor);
134 134

	
135 135
      Value mask;
136 136
      int max_digit;
137 137
      Iterator it;
138 138

	
139 139
      mask = ~0; max_digit = 0;
140 140
      for (it = first; it != cut; ++it) {
141 141
        while ((mask & functor(*it)) != mask) {
142 142
          ++max_digit;
143 143
          mask <<= 1;
144 144
        }
145 145
      }
146 146
      radixIntroSort(first, cut, functor, 1 << max_digit);
147 147

	
148 148
      mask = 0; max_digit = 0;
149 149
      for (it = cut; it != last; ++it) {
150 150
        while ((mask | functor(*it)) != mask) {
151 151
          ++max_digit;
152 152
          mask <<= 1; mask |= 1;
153 153
        }
154 154
      }
155 155
      radixIntroSort(cut, last, functor, 1 << max_digit);
156 156
    }
157 157

	
158 158
    template <typename Value, typename Iterator, typename Functor>
159 159
    void radixUnsignedSort(Iterator first, Iterator last, Functor functor) {
160 160

	
161 161
      Value mask = 0;
162 162
      int max_digit = 0;
163 163

	
164 164
      Iterator it;
165 165
      for (it = first; it != last; ++it) {
166 166
        while ((mask | functor(*it)) != mask) {
167 167
          ++max_digit;
168 168
          mask <<= 1; mask |= 1;
169 169
        }
170 170
      }
171 171
      radixIntroSort(first, last, functor, 1 << max_digit);
172 172
    }
173 173

	
174 174

	
175 175
    template <typename Value,
176 176
              bool sign = std::numeric_limits<Value>::is_signed >
177 177
    struct RadixSortSelector {
178 178
      template <typename Iterator, typename Functor>
179 179
      static void sort(Iterator first, Iterator last, Functor functor) {
180 180
        radixSignedSort<Value>(first, last, functor);
181 181
      }
182 182
    };
183 183

	
184 184
    template <typename Value>
185 185
    struct RadixSortSelector<Value, false> {
186 186
      template <typename Iterator, typename Functor>
187 187
      static void sort(Iterator first, Iterator last, Functor functor) {
188 188
        radixUnsignedSort<Value>(first, last, functor);
189 189
      }
190 190
    };
191 191

	
192 192
  }
193 193

	
194 194
  /// \ingroup auxalg
195 195
  ///
196 196
  /// \brief Sorts the STL compatible range into ascending order.
197 197
  ///
198 198
  /// The \c radixSort sorts an STL compatible range into ascending
199 199
  /// order.  The radix sort algorithm can sort items which are mapped
200 200
  /// to integers with an adaptable unary function \c functor and the
201 201
  /// order will be ascending according to these mapped values.
202 202
  ///
203 203
  /// It is also possible to use a normal function instead
204 204
  /// of the functor object. If the functor is not given it will use
205 205
  /// the identity function instead.
206 206
  ///
207 207
  /// This is a special quick sort algorithm where the pivot
208
  /// values to split the items are choosen to be \f$ 2^k \f$ for each \c k.
209
  /// Therefore, the time complexity of the
210
  /// algorithm is \f$ O(\log(c)n) \f$ and it uses \f$ O(\log(c)) \f$,
211
  /// additional space, where \c c is the maximal value and \c n is the
212
  /// number of the items in the container.
208
  /// values to split the items are choosen to be 2<sup>k</sup>
209
  /// for each \c k.
210
  /// Therefore, the time complexity of the algorithm is O(log(c)*n) and
211
  /// it uses O(log(c)) additional space, where \c c is the maximal value
212
  /// and \c n is the number of the items in the container.
213 213
  ///
214 214
  /// \param first The begin of the given range.
215 215
  /// \param last The end of the given range.
216 216
  /// \param functor An adaptible unary function or a normal function
217 217
  /// which maps the items to any integer type which can be either
218 218
  /// signed or unsigned.
219 219
  ///
220 220
  /// \sa stableRadixSort()
221 221
  template <typename Iterator, typename Functor>
222 222
  void radixSort(Iterator first, Iterator last, Functor functor) {
223 223
    using namespace _radix_sort_bits;
224 224
    typedef typename Functor::result_type Value;
225 225
    RadixSortSelector<Value>::sort(first, last, functor);
226 226
  }
227 227

	
228 228
  template <typename Iterator, typename Value, typename Key>
229 229
  void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
230 230
    using namespace _radix_sort_bits;
231 231
    RadixSortSelector<Value>::sort(first, last, functor);
232 232
  }
233 233

	
234 234
  template <typename Iterator, typename Value, typename Key>
235 235
  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
236 236
    using namespace _radix_sort_bits;
237 237
    RadixSortSelector<Value>::sort(first, last, functor);
238 238
  }
239 239

	
240 240
  template <typename Iterator, typename Value, typename Key>
241 241
  void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
242 242
    using namespace _radix_sort_bits;
243 243
    RadixSortSelector<Value>::sort(first, last, functor);
244 244
  }
245 245

	
246 246
  template <typename Iterator, typename Value, typename Key>
247 247
  void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
248 248
    using namespace _radix_sort_bits;
249 249
    RadixSortSelector<Value>::sort(first, last, functor);
250 250
  }
251 251

	
252 252
  template <typename Iterator>
253 253
  void radixSort(Iterator first, Iterator last) {
254 254
    using namespace _radix_sort_bits;
255 255
    typedef typename std::iterator_traits<Iterator>::value_type Value;
256 256
    RadixSortSelector<Value>::sort(first, last, Identity<Value>());
257 257
  }
258 258

	
259 259
  namespace _radix_sort_bits {
260 260

	
261 261
    template <typename Value>
262 262
    unsigned char valueByte(Value value, int byte) {
263 263
      return value >> (std::numeric_limits<unsigned char>::digits * byte);
264 264
    }
265 265

	
266 266
    template <typename Functor, typename Key>
267 267
    void stableRadixIntroSort(Key *first, Key *last, Key *target,
268 268
                              int byte, Functor functor) {
269 269
      const int size =
270 270
        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
271 271
      std::vector<int> counter(size);
272 272
      for (int i = 0; i < size; ++i) {
273 273
        counter[i] = 0;
274 274
      }
275 275
      Key *it = first;
276 276
      while (first != last) {
277 277
        ++counter[valueByte(functor(*first), byte)];
278 278
        ++first;
279 279
      }
280 280
      int prev, num = 0;
281 281
      for (int i = 0; i < size; ++i) {
282 282
        prev = num;
283 283
        num += counter[i];
284 284
        counter[i] = prev;
285 285
      }
286 286
      while (it != last) {
287 287
        target[counter[valueByte(functor(*it), byte)]++] = *it;
288 288
        ++it;
289 289
      }
290 290
    }
291 291

	
292 292
    template <typename Functor, typename Key>
293 293
    void signedStableRadixIntroSort(Key *first, Key *last, Key *target,
294 294
                                    int byte, Functor functor) {
295 295
      const int size =
296 296
        unsigned(std::numeric_limits<unsigned char>::max()) + 1;
297 297
      std::vector<int> counter(size);
298 298
      for (int i = 0; i < size; ++i) {
299 299
        counter[i] = 0;
300 300
      }
301 301
      Key *it = first;
302 302
      while (first != last) {
303 303
        counter[valueByte(functor(*first), byte)]++;
304 304
        ++first;
305 305
      }
306 306
      int prev, num = 0;
307 307
      for (int i = size / 2; i < size; ++i) {
308 308
        prev = num;
... ...
@@ -337,151 +337,151 @@
337 337
          if (dir) {
338 338
            stableRadixIntroSort(buffer, buffer + length, buffer + length,
339 339
                                 i, functor);
340 340
          } else {
341 341
            stableRadixIntroSort(buffer + length, buffer + 2 * length, buffer,
342 342
                                 i, functor);
343 343
          }
344 344
          dir = !dir;
345 345
        }
346 346
        if (dir) {
347 347
          signedStableRadixIntroSort(buffer, buffer + length, buffer + length,
348 348
                                     sizeof(Value) - 1, functor);
349 349
          std::copy(buffer + length, buffer + 2 * length, first);
350 350
        }        else {
351 351
          signedStableRadixIntroSort(buffer + length, buffer + 2 * length,
352 352
                                     buffer, sizeof(Value) - 1, functor);
353 353
          std::copy(buffer, buffer + length, first);
354 354
        }
355 355
      } catch (...) {
356 356
        allocator.deallocate(buffer, 2 * length);
357 357
        throw;
358 358
      }
359 359
      allocator.deallocate(buffer, 2 * length);
360 360
    }
361 361

	
362 362
    template <typename Value, typename Iterator, typename Functor>
363 363
    void stableRadixUnsignedSort(Iterator first, Iterator last,
364 364
                                 Functor functor) {
365 365
      if (first == last) return;
366 366
      typedef typename std::iterator_traits<Iterator>::value_type Key;
367 367
      typedef std::allocator<Key> Allocator;
368 368
      Allocator allocator;
369 369

	
370 370
      int length = std::distance(first, last);
371 371
      Key *buffer = allocator.allocate(2 * length);
372 372
      try {
373 373
        bool dir = true;
374 374
        std::copy(first, last, buffer);
375 375
        for (int i = 0; i < int(sizeof(Value)); ++i) {
376 376
          if (dir) {
377 377
            stableRadixIntroSort(buffer, buffer + length,
378 378
                                 buffer + length, i, functor);
379 379
          } else {
380 380
            stableRadixIntroSort(buffer + length, buffer + 2 * length,
381 381
                                 buffer, i, functor);
382 382
          }
383 383
          dir = !dir;
384 384
        }
385 385
        if (dir) {
386 386
          std::copy(buffer, buffer + length, first);
387 387
        }        else {
388 388
          std::copy(buffer + length, buffer + 2 * length, first);
389 389
        }
390 390
      } catch (...) {
391 391
        allocator.deallocate(buffer, 2 * length);
392 392
        throw;
393 393
      }
394 394
      allocator.deallocate(buffer, 2 * length);
395 395
    }
396 396

	
397 397

	
398 398

	
399 399
    template <typename Value,
400 400
              bool sign = std::numeric_limits<Value>::is_signed >
401 401
    struct StableRadixSortSelector {
402 402
      template <typename Iterator, typename Functor>
403 403
      static void sort(Iterator first, Iterator last, Functor functor) {
404 404
        stableRadixSignedSort<Value>(first, last, functor);
405 405
      }
406 406
    };
407 407

	
408 408
    template <typename Value>
409 409
    struct StableRadixSortSelector<Value, false> {
410 410
      template <typename Iterator, typename Functor>
411 411
      static void sort(Iterator first, Iterator last, Functor functor) {
412 412
        stableRadixUnsignedSort<Value>(first, last, functor);
413 413
      }
414 414
    };
415 415

	
416 416
  }
417 417

	
418 418
  /// \ingroup auxalg
419 419
  ///
420 420
  /// \brief Sorts the STL compatible range into ascending order in a stable
421 421
  /// way.
422 422
  ///
423 423
  /// This function sorts an STL compatible range into ascending
424 424
  /// order according to an integer mapping in the same as radixSort() does.
425 425
  ///
426 426
  /// This sorting algorithm is stable, i.e. the order of two equal
427 427
  /// elements remains the same after the sorting.
428 428
  ///
429 429
  /// This sort algorithm  use a radix forward sort on the
430 430
  /// bytes of the integer number. The algorithm sorts the items
431 431
  /// byte-by-byte. First, it counts how many times a byte value occurs
432 432
  /// in the container, then it copies the corresponding items to
433
  /// another container in asceding order in \c O(n) time.
433
  /// another container in asceding order in O(n) time.
434 434
  ///
435
  /// The time complexity of the algorithm is \f$ O(\log(c)n) \f$ and
436
  /// it uses \f$ O(n) \f$, additional space, where \c c is the
435
  /// The time complexity of the algorithm is O(log(c)*n) and
436
  /// it uses O(n) additional space, where \c c is the
437 437
  /// maximal value and \c n is the number of the items in the
438 438
  /// container.
439 439
  ///
440 440

	
441 441
  /// \param first The begin of the given range.
442 442
  /// \param last The end of the given range.
443 443
  /// \param functor An adaptible unary function or a normal function
444 444
  /// which maps the items to any integer type which can be either
445 445
  /// signed or unsigned.
446 446
  /// \sa radixSort()
447 447
  template <typename Iterator, typename Functor>
448 448
  void stableRadixSort(Iterator first, Iterator last, Functor functor) {
449 449
    using namespace _radix_sort_bits;
450 450
    typedef typename Functor::result_type Value;
451 451
    StableRadixSortSelector<Value>::sort(first, last, functor);
452 452
  }
453 453

	
454 454
  template <typename Iterator, typename Value, typename Key>
455 455
  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
456 456
    using namespace _radix_sort_bits;
457 457
    StableRadixSortSelector<Value>::sort(first, last, functor);
458 458
  }
459 459

	
460 460
  template <typename Iterator, typename Value, typename Key>
461 461
  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
462 462
    using namespace _radix_sort_bits;
463 463
    StableRadixSortSelector<Value>::sort(first, last, functor);
464 464
  }
465 465

	
466 466
  template <typename Iterator, typename Value, typename Key>
467 467
  void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
468 468
    using namespace _radix_sort_bits;
469 469
    StableRadixSortSelector<Value>::sort(first, last, functor);
470 470
  }
471 471

	
472 472
  template <typename Iterator, typename Value, typename Key>
473 473
  void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
474 474
    using namespace _radix_sort_bits;
475 475
    StableRadixSortSelector<Value>::sort(first, last, functor);
476 476
  }
477 477

	
478 478
  template <typename Iterator>
479 479
  void stableRadixSort(Iterator first, Iterator last) {
480 480
    using namespace _radix_sort_bits;
481 481
    typedef typename std::iterator_traits<Iterator>::value_type Value;
482 482
    StableRadixSortSelector<Value>::sort(first, last, Identity<Value>());
483 483
  }
484 484

	
485 485
}
486 486

	
487 487
#endif
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
/*
20 20
 * This file contains the reimplemented version of the Mersenne Twister
21 21
 * Generator of Matsumoto and Nishimura.
22 22
 *
23 23
 * See the appropriate copyright notice below.
24 24
 *
25 25
 * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
26 26
 * All rights reserved.
27 27
 *
28 28
 * Redistribution and use in source and binary forms, with or without
29 29
 * modification, are permitted provided that the following conditions
30 30
 * are met:
31 31
 *
32 32
 * 1. Redistributions of source code must retain the above copyright
33 33
 *    notice, this list of conditions and the following disclaimer.
34 34
 *
35 35
 * 2. Redistributions in binary form must reproduce the above copyright
36 36
 *    notice, this list of conditions and the following disclaimer in the
37 37
 *    documentation and/or other materials provided with the distribution.
38 38
 *
39 39
 * 3. The names of its contributors may not be used to endorse or promote
40 40
 *    products derived from this software without specific prior written
41 41
 *    permission.
42 42
 *
43 43
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 44
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 45
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
46 46
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
47 47
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
48 48
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 49
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 50
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 51
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52 52
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 53
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54 54
 * OF THE POSSIBILITY OF SUCH DAMAGE.
55 55
 *
56 56
 *
57 57
 * Any feedback is very welcome.
58 58
 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
59 59
 * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
60 60
 */
61 61

	
62 62
#ifndef LEMON_RANDOM_H
63 63
#define LEMON_RANDOM_H
64 64

	
65 65
#include <algorithm>
66 66
#include <iterator>
67 67
#include <vector>
68 68
#include <limits>
69 69
#include <fstream>
70 70

	
71 71
#include <lemon/math.h>
72 72
#include <lemon/dim2.h>
73 73

	
74 74
#ifndef WIN32
75 75
#include <sys/time.h>
76 76
#include <ctime>
77 77
#include <sys/types.h>
78 78
#include <unistd.h>
79 79
#else
80
#include <windows.h>
80
#include <lemon/bits/windows.h>
81 81
#endif
82 82

	
83 83
///\ingroup misc
84 84
///\file
85 85
///\brief Mersenne Twister random number generator
86 86

	
87 87
namespace lemon {
88 88

	
89 89
  namespace _random_bits {
90 90

	
91 91
    template <typename _Word, int _bits = std::numeric_limits<_Word>::digits>
92 92
    struct RandomTraits {};
93 93

	
94 94
    template <typename _Word>
95 95
    struct RandomTraits<_Word, 32> {
96 96

	
97 97
      typedef _Word Word;
98 98
      static const int bits = 32;
99 99

	
100 100
      static const int length = 624;
101 101
      static const int shift = 397;
102 102

	
103 103
      static const Word mul = 0x6c078965u;
104 104
      static const Word arrayInit = 0x012BD6AAu;
105 105
      static const Word arrayMul1 = 0x0019660Du;
106 106
      static const Word arrayMul2 = 0x5D588B65u;
107 107

	
108 108
      static const Word mask = 0x9908B0DFu;
109 109
      static const Word loMask = (1u << 31) - 1;
110 110
      static const Word hiMask = ~loMask;
111 111

	
112 112

	
113 113
      static Word tempering(Word rnd) {
114 114
        rnd ^= (rnd >> 11);
115 115
        rnd ^= (rnd << 7) & 0x9D2C5680u;
116 116
        rnd ^= (rnd << 15) & 0xEFC60000u;
117 117
        rnd ^= (rnd >> 18);
118 118
        return rnd;
119 119
      }
120 120

	
121 121
    };
122 122

	
123 123
    template <typename _Word>
124 124
    struct RandomTraits<_Word, 64> {
125 125

	
126 126
      typedef _Word Word;
127 127
      static const int bits = 64;
128 128

	
129 129
      static const int length = 312;
130 130
      static const int shift = 156;
131 131

	
132 132
      static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du);
133 133
      static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu);
134 134
      static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u);
135 135
      static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu);
136 136

	
137 137
      static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u);
138 138
      static const Word loMask = (Word(1u) << 31) - 1;
139 139
      static const Word hiMask = ~loMask;
140 140

	
141 141
      static Word tempering(Word rnd) {
142 142
        rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u));
143 143
        rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u));
144 144
        rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u));
145 145
        rnd ^= (rnd >> 43);
146 146
        return rnd;
147 147
      }
148 148

	
149 149
    };
150 150

	
151 151
    template <typename _Word>
152 152
    class RandomCore {
153 153
    public:
154 154

	
155 155
      typedef _Word Word;
156 156

	
157 157
    private:
158 158

	
159 159
      static const int bits = RandomTraits<Word>::bits;
160 160

	
161 161
      static const int length = RandomTraits<Word>::length;
162 162
      static const int shift = RandomTraits<Word>::shift;
163 163

	
164 164
    public:
165 165

	
166 166
      void initState() {
167 167
        static const Word seedArray[4] = {
168 168
          0x12345u, 0x23456u, 0x34567u, 0x45678u
169 169
        };
170 170

	
171 171
        initState(seedArray, seedArray + 4);
172 172
      }
173 173

	
174 174
      void initState(Word seed) {
175 175

	
176 176
        static const Word mul = RandomTraits<Word>::mul;
... ...
@@ -251,767 +251,755 @@
251 251

	
252 252
        register Word *curr = state + length - 1;
253 253
        register long num;
254 254

	
255 255
        num = length - shift;
256 256
        while (num--) {
257 257
          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
258 258
            curr[- shift] ^ mask[curr[-1] & 1ul];
259 259
          --curr;
260 260
        }
261 261
        num = shift - 1;
262 262
        while (num--) {
263 263
          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
264 264
            curr[length - shift] ^ mask[curr[-1] & 1ul];
265 265
          --curr;
266 266
        }
267 267
        state[0] = (((state[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
268 268
          curr[length - shift] ^ mask[curr[length - 1] & 1ul];
269 269

	
270 270
      }
271 271

	
272 272

	
273 273
      Word *current;
274 274
      Word state[length];
275 275

	
276 276
    };
277 277

	
278 278

	
279 279
    template <typename Result,
280 280
              int shift = (std::numeric_limits<Result>::digits + 1) / 2>
281 281
    struct Masker {
282 282
      static Result mask(const Result& result) {
283 283
        return Masker<Result, (shift + 1) / 2>::
284 284
          mask(static_cast<Result>(result | (result >> shift)));
285 285
      }
286 286
    };
287 287

	
288 288
    template <typename Result>
289 289
    struct Masker<Result, 1> {
290 290
      static Result mask(const Result& result) {
291 291
        return static_cast<Result>(result | (result >> 1));
292 292
      }
293 293
    };
294 294

	
295 295
    template <typename Result, typename Word,
296 296
              int rest = std::numeric_limits<Result>::digits, int shift = 0,
297 297
              bool last = rest <= std::numeric_limits<Word>::digits>
298 298
    struct IntConversion {
299 299
      static const int bits = std::numeric_limits<Word>::digits;
300 300

	
301 301
      static Result convert(RandomCore<Word>& rnd) {
302 302
        return static_cast<Result>(rnd() >> (bits - rest)) << shift;
303 303
      }
304 304

	
305 305
    };
306 306

	
307 307
    template <typename Result, typename Word, int rest, int shift>
308 308
    struct IntConversion<Result, Word, rest, shift, false> {
309 309
      static const int bits = std::numeric_limits<Word>::digits;
310 310

	
311 311
      static Result convert(RandomCore<Word>& rnd) {
312 312
        return (static_cast<Result>(rnd()) << shift) |
313 313
          IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd);
314 314
      }
315 315
    };
316 316

	
317 317

	
318 318
    template <typename Result, typename Word,
319 319
              bool one_word = (std::numeric_limits<Word>::digits <
320 320
                               std::numeric_limits<Result>::digits) >
321 321
    struct Mapping {
322 322
      static Result map(RandomCore<Word>& rnd, const Result& bound) {
323 323
        Word max = Word(bound - 1);
324 324
        Result mask = Masker<Result>::mask(bound - 1);
325 325
        Result num;
326 326
        do {
327 327
          num = IntConversion<Result, Word>::convert(rnd) & mask;
328 328
        } while (num > max);
329 329
        return num;
330 330
      }
331 331
    };
332 332

	
333 333
    template <typename Result, typename Word>
334 334
    struct Mapping<Result, Word, false> {
335 335
      static Result map(RandomCore<Word>& rnd, const Result& bound) {
336 336
        Word max = Word(bound - 1);
337 337
        Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2>
338 338
          ::mask(max);
339 339
        Word num;
340 340
        do {
341 341
          num = rnd() & mask;
342 342
        } while (num > max);
343 343
        return num;
344 344
      }
345 345
    };
346 346

	
347
    template <typename Result, int exp, bool pos = (exp >= 0)>
347
    template <typename Result, int exp>
348 348
    struct ShiftMultiplier {
349 349
      static const Result multiplier() {
350 350
        Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
351 351
        res *= res;
352
        if ((exp & 1) == 1) res *= static_cast<Result>(2.0);
353
        return res;
354
      }
355
    };
356

	
357
    template <typename Result, int exp>
358
    struct ShiftMultiplier<Result, exp, false> {
359
      static const Result multiplier() {
360
        Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
361
        res *= res;
362 352
        if ((exp & 1) == 1) res *= static_cast<Result>(0.5);
363 353
        return res;
364 354
      }
365 355
    };
366 356

	
367 357
    template <typename Result>
368
    struct ShiftMultiplier<Result, 0, true> {
358
    struct ShiftMultiplier<Result, 0> {
369 359
      static const Result multiplier() {
370 360
        return static_cast<Result>(1.0);
371 361
      }
372 362
    };
373 363

	
374 364
    template <typename Result>
375
    struct ShiftMultiplier<Result, -20, true> {
365
    struct ShiftMultiplier<Result, 20> {
376 366
      static const Result multiplier() {
377 367
        return static_cast<Result>(1.0/1048576.0);
378 368
      }
379 369
    };
380 370

	
381 371
    template <typename Result>
382
    struct ShiftMultiplier<Result, -32, true> {
372
    struct ShiftMultiplier<Result, 32> {
383 373
      static const Result multiplier() {
384
        return static_cast<Result>(1.0/424967296.0);
374
        return static_cast<Result>(1.0/4294967296.0);
385 375
      }
386 376
    };
387 377

	
388 378
    template <typename Result>
389
    struct ShiftMultiplier<Result, -53, true> {
379
    struct ShiftMultiplier<Result, 53> {
390 380
      static const Result multiplier() {
391 381
        return static_cast<Result>(1.0/9007199254740992.0);
392 382
      }
393 383
    };
394 384

	
395 385
    template <typename Result>
396
    struct ShiftMultiplier<Result, -64, true> {
386
    struct ShiftMultiplier<Result, 64> {
397 387
      static const Result multiplier() {
398 388
        return static_cast<Result>(1.0/18446744073709551616.0);
399 389
      }
400 390
    };
401 391

	
402 392
    template <typename Result, int exp>
403 393
    struct Shifting {
404 394
      static Result shift(const Result& result) {
405 395
        return result * ShiftMultiplier<Result, exp>::multiplier();
406 396
      }
407 397
    };
408 398

	
409 399
    template <typename Result, typename Word,
410 400
              int rest = std::numeric_limits<Result>::digits, int shift = 0,
411 401
              bool last = rest <= std::numeric_limits<Word>::digits>
412 402
    struct RealConversion{
413 403
      static const int bits = std::numeric_limits<Word>::digits;
414 404

	
415 405
      static Result convert(RandomCore<Word>& rnd) {
416
        return Shifting<Result, - shift - rest>::
406
        return Shifting<Result, shift + rest>::
417 407
          shift(static_cast<Result>(rnd() >> (bits - rest)));
418 408
      }
419 409
    };
420 410

	
421 411
    template <typename Result, typename Word, int rest, int shift>
422 412
    struct RealConversion<Result, Word, rest, shift, false> {
423 413
      static const int bits = std::numeric_limits<Word>::digits;
424 414

	
425 415
      static Result convert(RandomCore<Word>& rnd) {
426
        return Shifting<Result, - shift - bits>::
416
        return Shifting<Result, shift + bits>::
427 417
          shift(static_cast<Result>(rnd())) +
428 418
          RealConversion<Result, Word, rest-bits, shift + bits>::
429 419
          convert(rnd);
430 420
      }
431 421
    };
432 422

	
433 423
    template <typename Result, typename Word>
434 424
    struct Initializer {
435 425

	
436 426
      template <typename Iterator>
437 427
      static void init(RandomCore<Word>& rnd, Iterator begin, Iterator end) {
438 428
        std::vector<Word> ws;
439 429
        for (Iterator it = begin; it != end; ++it) {
440 430
          ws.push_back(Word(*it));
441 431
        }
442 432
        rnd.initState(ws.begin(), ws.end());
443 433
      }
444 434

	
445 435
      static void init(RandomCore<Word>& rnd, Result seed) {
446 436
        rnd.initState(seed);
447 437
      }
448 438
    };
449 439

	
450 440
    template <typename Word>
451 441
    struct BoolConversion {
452 442
      static bool convert(RandomCore<Word>& rnd) {
453 443
        return (rnd() & 1) == 1;
454 444
      }
455 445
    };
456 446

	
457 447
    template <typename Word>
458 448
    struct BoolProducer {
459 449
      Word buffer;
460 450
      int num;
461 451

	
462 452
      BoolProducer() : num(0) {}
463 453

	
464 454
      bool convert(RandomCore<Word>& rnd) {
465 455
        if (num == 0) {
466 456
          buffer = rnd();
467 457
          num = RandomTraits<Word>::bits;
468 458
        }
469 459
        bool r = (buffer & 1);
470 460
        buffer >>= 1;
471 461
        --num;
472 462
        return r;
473 463
      }
474 464
    };
475 465

	
476 466
  }
477 467

	
478 468
  /// \ingroup misc
479 469
  ///
480 470
  /// \brief Mersenne Twister random number generator
481 471
  ///
482 472
  /// The Mersenne Twister is a twisted generalized feedback
483 473
  /// shift-register generator of Matsumoto and Nishimura. The period
484 474
  /// of this generator is \f$ 2^{19937} - 1 \f$ and it is
485 475
  /// equi-distributed in 623 dimensions for 32-bit numbers. The time
486 476
  /// performance of this generator is comparable to the commonly used
487 477
  /// generators.
488 478
  ///
489 479
  /// This implementation is specialized for both 32-bit and 64-bit
490 480
  /// architectures. The generators differ sligthly in the
491 481
  /// initialization and generation phase so they produce two
492 482
  /// completly different sequences.
493 483
  ///
494 484
  /// The generator gives back random numbers of serveral types. To
495 485
  /// get a random number from a range of a floating point type you
496 486
  /// can use one form of the \c operator() or the \c real() member
497 487
  /// function. If you want to get random number from the {0, 1, ...,
498 488
  /// n-1} integer range use the \c operator[] or the \c integer()
499 489
  /// method. And to get random number from the whole range of an
500 490
  /// integer type you can use the argumentless \c integer() or \c
501 491
  /// uinteger() functions. After all you can get random bool with
502 492
  /// equal chance of true and false or given probability of true
503 493
  /// result with the \c boolean() member functions.
504 494
  ///
505 495
  ///\code
506 496
  /// // The commented code is identical to the other
507 497
  /// double a = rnd();                     // [0.0, 1.0)
508 498
  /// // double a = rnd.real();             // [0.0, 1.0)
509 499
  /// double b = rnd(100.0);                // [0.0, 100.0)
510 500
  /// // double b = rnd.real(100.0);        // [0.0, 100.0)
511 501
  /// double c = rnd(1.0, 2.0);             // [1.0, 2.0)
512 502
  /// // double c = rnd.real(1.0, 2.0);     // [1.0, 2.0)
513 503
  /// int d = rnd[100000];                  // 0..99999
514 504
  /// // int d = rnd.integer(100000);       // 0..99999
515 505
  /// int e = rnd[6] + 1;                   // 1..6
516 506
  /// // int e = rnd.integer(1, 1 + 6);     // 1..6
517 507
  /// int b = rnd.uinteger<int>();          // 0 .. 2^31 - 1
518 508
  /// int c = rnd.integer<int>();           // - 2^31 .. 2^31 - 1
519 509
  /// bool g = rnd.boolean();               // P(g = true) = 0.5
520 510
  /// bool h = rnd.boolean(0.8);            // P(h = true) = 0.8
521 511
  ///\endcode
522 512
  ///
523 513
  /// LEMON provides a global instance of the random number
524 514
  /// generator which name is \ref lemon::rnd "rnd". Usually it is a
525 515
  /// good programming convenience to use this global generator to get
526 516
  /// random numbers.
527 517
  class Random {
528 518
  private:
529 519

	
530 520
    // Architecture word
531 521
    typedef unsigned long Word;
532 522

	
533 523
    _random_bits::RandomCore<Word> core;
534 524
    _random_bits::BoolProducer<Word> bool_producer;
535 525

	
536 526

	
537 527
  public:
538 528

	
539 529
    ///\name Initialization
540 530
    ///
541 531
    /// @{
542 532

	
543 533
    /// \brief Default constructor
544 534
    ///
545 535
    /// Constructor with constant seeding.
546 536
    Random() { core.initState(); }
547 537

	
548 538
    /// \brief Constructor with seed
549 539
    ///
550 540
    /// Constructor with seed. The current number type will be converted
551 541
    /// to the architecture word type.
552 542
    template <typename Number>
553 543
    Random(Number seed) {
554 544
      _random_bits::Initializer<Number, Word>::init(core, seed);
555 545
    }
556 546

	
557 547
    /// \brief Constructor with array seeding
558 548
    ///
559 549
    /// Constructor with array seeding. The given range should contain
560 550
    /// any number type and the numbers will be converted to the
561 551
    /// architecture word type.
562 552
    template <typename Iterator>
563 553
    Random(Iterator begin, Iterator end) {
564 554
      typedef typename std::iterator_traits<Iterator>::value_type Number;
565 555
      _random_bits::Initializer<Number, Word>::init(core, begin, end);
566 556
    }
567 557

	
568 558
    /// \brief Copy constructor
569 559
    ///
570 560
    /// Copy constructor. The generated sequence will be identical to
571 561
    /// the other sequence. It can be used to save the current state
572 562
    /// of the generator and later use it to generate the same
573 563
    /// sequence.
574 564
    Random(const Random& other) {
575 565
      core.copyState(other.core);
576 566
    }
577 567

	
578 568
    /// \brief Assign operator
579 569
    ///
580 570
    /// Assign operator. The generated sequence will be identical to
581 571
    /// the other sequence. It can be used to save the current state
582 572
    /// of the generator and later use it to generate the same
583 573
    /// sequence.
584 574
    Random& operator=(const Random& other) {
585 575
      if (&other != this) {
586 576
        core.copyState(other.core);
587 577
      }
588 578
      return *this;
589 579
    }
590 580

	
591 581
    /// \brief Seeding random sequence
592 582
    ///
593 583
    /// Seeding the random sequence. The current number type will be
594 584
    /// converted to the architecture word type.
595 585
    template <typename Number>
596 586
    void seed(Number seed) {
597 587
      _random_bits::Initializer<Number, Word>::init(core, seed);
598 588
    }
599 589

	
600 590
    /// \brief Seeding random sequence
601 591
    ///
602 592
    /// Seeding the random sequence. The given range should contain
603 593
    /// any number type and the numbers will be converted to the
604 594
    /// architecture word type.
605 595
    template <typename Iterator>
606 596
    void seed(Iterator begin, Iterator end) {
607 597
      typedef typename std::iterator_traits<Iterator>::value_type Number;
608 598
      _random_bits::Initializer<Number, Word>::init(core, begin, end);
609 599
    }
610 600

	
611 601
    /// \brief Seeding from file or from process id and time
612 602
    ///
613 603
    /// By default, this function calls the \c seedFromFile() member
614 604
    /// function with the <tt>/dev/urandom</tt> file. If it does not success,
615 605
    /// it uses the \c seedFromTime().
616
    /// \return Currently always true.
606
    /// \return Currently always \c true.
617 607
    bool seed() {
618 608
#ifndef WIN32
619 609
      if (seedFromFile("/dev/urandom", 0)) return true;
620 610
#endif
621 611
      if (seedFromTime()) return true;
622 612
      return false;
623 613
    }
624 614

	
625 615
    /// \brief Seeding from file
626 616
    ///
627 617
    /// Seeding the random sequence from file. The linux kernel has two
628 618
    /// devices, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which
629 619
    /// could give good seed values for pseudo random generators (The
630 620
    /// difference between two devices is that the <tt>random</tt> may
631 621
    /// block the reading operation while the kernel can give good
632 622
    /// source of randomness, while the <tt>urandom</tt> does not
633 623
    /// block the input, but it could give back bytes with worse
634 624
    /// entropy).
635 625
    /// \param file The source file
636 626
    /// \param offset The offset, from the file read.
637
    /// \return True when the seeding successes.
627
    /// \return \c true when the seeding successes.
638 628
#ifndef WIN32
639 629
    bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0)
640 630
#else
641 631
    bool seedFromFile(const std::string& file = "", int offset = 0)
642 632
#endif
643 633
    {
644 634
      std::ifstream rs(file.c_str());
645 635
      const int size = 4;
646 636
      Word buf[size];
647 637
      if (offset != 0 && !rs.seekg(offset)) return false;
648 638
      if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false;
649 639
      seed(buf, buf + size);
650 640
      return true;
651 641
    }
652 642

	
653 643
    /// \brief Seding from process id and time
654 644
    ///
655 645
    /// Seding from process id and time. This function uses the
656 646
    /// current process id and the current time for initialize the
657 647
    /// random sequence.
658
    /// \return Currently always true.
648
    /// \return Currently always \c true.
659 649
    bool seedFromTime() {
660 650
#ifndef WIN32
661 651
      timeval tv;
662 652
      gettimeofday(&tv, 0);
663 653
      seed(getpid() + tv.tv_sec + tv.tv_usec);
664 654
#else
665
      FILETIME time;
666
      GetSystemTimeAsFileTime(&time);
667
      seed(GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime);
655
      seed(bits::getWinRndSeed());
668 656
#endif
669 657
      return true;
670 658
    }
671 659

	
672 660
    /// @}
673 661

	
674
    ///\name Uniform distributions
662
    ///\name Uniform Distributions
675 663
    ///
676 664
    /// @{
677 665

	
678 666
    /// \brief Returns a random real number from the range [0, 1)
679 667
    ///
680 668
    /// It returns a random real number from the range [0, 1). The
681 669
    /// default Number type is \c double.
682 670
    template <typename Number>
683 671
    Number real() {
684 672
      return _random_bits::RealConversion<Number, Word>::convert(core);
685 673
    }
686 674

	
687 675
    double real() {
688 676
      return real<double>();
689 677
    }
690 678

	
691 679
    /// \brief Returns a random real number from the range [0, 1)
692 680
    ///
693 681
    /// It returns a random double from the range [0, 1).
694 682
    double operator()() {
695 683
      return real<double>();
696 684
    }
697 685

	
698 686
    /// \brief Returns a random real number from the range [0, b)
699 687
    ///
700 688
    /// It returns a random real number from the range [0, b).
701 689
    double operator()(double b) {
702 690
      return real<double>() * b;
703 691
    }
704 692

	
705 693
    /// \brief Returns a random real number from the range [a, b)
706 694
    ///
707 695
    /// It returns a random real number from the range [a, b).
708 696
    double operator()(double a, double b) {
709 697
      return real<double>() * (b - a) + a;
710 698
    }
711 699

	
712 700
    /// \brief Returns a random integer from a range
713 701
    ///
714 702
    /// It returns a random integer from the range {0, 1, ..., b - 1}.
715 703
    template <typename Number>
716 704
    Number integer(Number b) {
717 705
      return _random_bits::Mapping<Number, Word>::map(core, b);
718 706
    }
719 707

	
720 708
    /// \brief Returns a random integer from a range
721 709
    ///
722 710
    /// It returns a random integer from the range {a, a + 1, ..., b - 1}.
723 711
    template <typename Number>
724 712
    Number integer(Number a, Number b) {
725 713
      return _random_bits::Mapping<Number, Word>::map(core, b - a) + a;
726 714
    }
727 715

	
728 716
    /// \brief Returns a random integer from a range
729 717
    ///
730 718
    /// It returns a random integer from the range {0, 1, ..., b - 1}.
731 719
    template <typename Number>
732 720
    Number operator[](Number b) {
733 721
      return _random_bits::Mapping<Number, Word>::map(core, b);
734 722
    }
735 723

	
736 724
    /// \brief Returns a random non-negative integer
737 725
    ///
738 726
    /// It returns a random non-negative integer uniformly from the
739 727
    /// whole range of the current \c Number type. The default result
740 728
    /// type of this function is <tt>unsigned int</tt>.
741 729
    template <typename Number>
742 730
    Number uinteger() {
743 731
      return _random_bits::IntConversion<Number, Word>::convert(core);
744 732
    }
745 733

	
746 734
    unsigned int uinteger() {
747 735
      return uinteger<unsigned int>();
748 736
    }
749 737

	
750 738
    /// \brief Returns a random integer
751 739
    ///
752 740
    /// It returns a random integer uniformly from the whole range of
753 741
    /// the current \c Number type. The default result type of this
754 742
    /// function is \c int.
755 743
    template <typename Number>
756 744
    Number integer() {
757 745
      static const int nb = std::numeric_limits<Number>::digits +
758 746
        (std::numeric_limits<Number>::is_signed ? 1 : 0);
759 747
      return _random_bits::IntConversion<Number, Word, nb>::convert(core);
760 748
    }
761 749

	
762 750
    int integer() {
763 751
      return integer<int>();
764 752
    }
765 753

	
766 754
    /// \brief Returns a random bool
767 755
    ///
768 756
    /// It returns a random bool. The generator holds a buffer for
769 757
    /// random bits. Every time when it become empty the generator makes
770 758
    /// a new random word and fill the buffer up.
771 759
    bool boolean() {
772 760
      return bool_producer.convert(core);
773 761
    }
774 762

	
775 763
    /// @}
776 764

	
777
    ///\name Non-uniform distributions
765
    ///\name Non-uniform Distributions
778 766
    ///
779 767
    ///@{
780 768

	
781 769
    /// \brief Returns a random bool with given probability of true result.
782 770
    ///
783 771
    /// It returns a random bool with given probability of true result.
784 772
    bool boolean(double p) {
785 773
      return operator()() < p;
786 774
    }
787 775

	
788 776
    /// Standard normal (Gauss) distribution
789 777

	
790 778
    /// Standard normal (Gauss) distribution.
791 779
    /// \note The Cartesian form of the Box-Muller
792 780
    /// transformation is used to generate a random normal distribution.
793 781
    double gauss()
794 782
    {
795 783
      double V1,V2,S;
796 784
      do {
797 785
        V1=2*real<double>()-1;
798 786
        V2=2*real<double>()-1;
799 787
        S=V1*V1+V2*V2;
800 788
      } while(S>=1);
801 789
      return std::sqrt(-2*std::log(S)/S)*V1;
802 790
    }
803 791
    /// Normal (Gauss) distribution with given mean and standard deviation
804 792

	
805 793
    /// Normal (Gauss) distribution with given mean and standard deviation.
806 794
    /// \sa gauss()
807 795
    double gauss(double mean,double std_dev)
808 796
    {
809 797
      return gauss()*std_dev+mean;
810 798
    }
811 799

	
812 800
    /// Lognormal distribution
813 801

	
814 802
    /// Lognormal distribution. The parameters are the mean and the standard
815 803
    /// deviation of <tt>exp(X)</tt>.
816 804
    ///
817 805
    double lognormal(double n_mean,double n_std_dev)
818 806
    {
819 807
      return std::exp(gauss(n_mean,n_std_dev));
820 808
    }
821 809
    /// Lognormal distribution
822 810

	
823 811
    /// Lognormal distribution. The parameter is an <tt>std::pair</tt> of
824 812
    /// the mean and the standard deviation of <tt>exp(X)</tt>.
825 813
    ///
826 814
    double lognormal(const std::pair<double,double> &params)
827 815
    {
828 816
      return std::exp(gauss(params.first,params.second));
829 817
    }
830 818
    /// Compute the lognormal parameters from mean and standard deviation
831 819

	
832 820
    /// This function computes the lognormal parameters from mean and
833 821
    /// standard deviation. The return value can direcly be passed to
834 822
    /// lognormal().
835 823
    std::pair<double,double> lognormalParamsFromMD(double mean,
836 824
                                                   double std_dev)
837 825
    {
838 826
      double fr=std_dev/mean;
839 827
      fr*=fr;
840 828
      double lg=std::log(1+fr);
841 829
      return std::pair<double,double>(std::log(mean)-lg/2.0,std::sqrt(lg));
842 830
    }
843 831
    /// Lognormal distribution with given mean and standard deviation
844 832

	
845 833
    /// Lognormal distribution with given mean and standard deviation.
846 834
    ///
847 835
    double lognormalMD(double mean,double std_dev)
848 836
    {
849 837
      return lognormal(lognormalParamsFromMD(mean,std_dev));
850 838
    }
851 839

	
852 840
    /// Exponential distribution with given mean
853 841

	
854 842
    /// This function generates an exponential distribution random number
855 843
    /// with mean <tt>1/lambda</tt>.
856 844
    ///
857 845
    double exponential(double lambda=1.0)
858 846
    {
859 847
      return -std::log(1.0-real<double>())/lambda;
860 848
    }
861 849

	
862 850
    /// Gamma distribution with given integer shape
863 851

	
864 852
    /// This function generates a gamma distribution random number.
865 853
    ///
866 854
    ///\param k shape parameter (<tt>k>0</tt> integer)
867 855
    double gamma(int k)
868 856
    {
869 857
      double s = 0;
870 858
      for(int i=0;i<k;i++) s-=std::log(1.0-real<double>());
871 859
      return s;
872 860
    }
873 861

	
874 862
    /// Gamma distribution with given shape and scale parameter
875 863

	
876 864
    /// This function generates a gamma distribution random number.
877 865
    ///
878 866
    ///\param k shape parameter (<tt>k>0</tt>)
879 867
    ///\param theta scale parameter
880 868
    ///
881 869
    double gamma(double k,double theta=1.0)
882 870
    {
883 871
      double xi,nu;
884 872
      const double delta = k-std::floor(k);
885 873
      const double v0=E/(E-delta);
886 874
      do {
887 875
        double V0=1.0-real<double>();
888 876
        double V1=1.0-real<double>();
889 877
        double V2=1.0-real<double>();
890 878
        if(V2<=v0)
891 879
          {
892 880
            xi=std::pow(V1,1.0/delta);
893 881
            nu=V0*std::pow(xi,delta-1.0);
894 882
          }
895 883
        else
896 884
          {
897 885
            xi=1.0-std::log(V1);
898 886
            nu=V0*std::exp(-xi);
899 887
          }
900 888
      } while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
901 889
      return theta*(xi+gamma(int(std::floor(k))));
902 890
    }
903 891

	
904 892
    /// Weibull distribution
905 893

	
906 894
    /// This function generates a Weibull distribution random number.
907 895
    ///
908 896
    ///\param k shape parameter (<tt>k>0</tt>)
909 897
    ///\param lambda scale parameter (<tt>lambda>0</tt>)
910 898
    ///
911 899
    double weibull(double k,double lambda)
912 900
    {
913 901
      return lambda*pow(-std::log(1.0-real<double>()),1.0/k);
914 902
    }
915 903

	
916 904
    /// Pareto distribution
917 905

	
918 906
    /// This function generates a Pareto distribution random number.
919 907
    ///
920 908
    ///\param k shape parameter (<tt>k>0</tt>)
921 909
    ///\param x_min location parameter (<tt>x_min>0</tt>)
922 910
    ///
923 911
    double pareto(double k,double x_min)
924 912
    {
925 913
      return exponential(gamma(k,1.0/x_min))+x_min;
926 914
    }
927 915

	
928 916
    /// Poisson distribution
929 917

	
930 918
    /// This function generates a Poisson distribution random number with
931 919
    /// parameter \c lambda.
932 920
    ///
933 921
    /// The probability mass function of this distribusion is
934 922
    /// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f]
935 923
    /// \note The algorithm is taken from the book of Donald E. Knuth titled
936 924
    /// ''Seminumerical Algorithms'' (1969). Its running time is linear in the
937 925
    /// return value.
938 926

	
939 927
    int poisson(double lambda)
940 928
    {
941 929
      const double l = std::exp(-lambda);
942 930
      int k=0;
943 931
      double p = 1.0;
944 932
      do {
945 933
        k++;
946 934
        p*=real<double>();
947 935
      } while (p>=l);
948 936
      return k-1;
949 937
    }
950 938

	
951 939
    ///@}
952 940

	
953
    ///\name Two dimensional distributions
941
    ///\name Two Dimensional Distributions
954 942
    ///
955 943
    ///@{
956 944

	
957 945
    /// Uniform distribution on the full unit circle
958 946

	
959 947
    /// Uniform distribution on the full unit circle.
960 948
    ///
961 949
    dim2::Point<double> disc()
962 950
    {
963 951
      double V1,V2;
964 952
      do {
965 953
        V1=2*real<double>()-1;
966 954
        V2=2*real<double>()-1;
967 955

	
968 956
      } while(V1*V1+V2*V2>=1);
969 957
      return dim2::Point<double>(V1,V2);
970 958
    }
971 959
    /// A kind of two dimensional normal (Gauss) distribution
972 960

	
973 961
    /// This function provides a turning symmetric two-dimensional distribution.
974 962
    /// Both coordinates are of standard normal distribution, but they are not
975 963
    /// independent.
976 964
    ///
977 965
    /// \note The coordinates are the two random variables provided by
978 966
    /// the Box-Muller method.
979 967
    dim2::Point<double> gauss2()
980 968
    {
981 969
      double V1,V2,S;
982 970
      do {
983 971
        V1=2*real<double>()-1;
984 972
        V2=2*real<double>()-1;
985 973
        S=V1*V1+V2*V2;
986 974
      } while(S>=1);
987 975
      double W=std::sqrt(-2*std::log(S)/S);
988 976
      return dim2::Point<double>(W*V1,W*V2);
989 977
    }
990 978
    /// A kind of two dimensional exponential distribution
991 979

	
992 980
    /// This function provides a turning symmetric two-dimensional distribution.
993 981
    /// The x-coordinate is of conditionally exponential distribution
994 982
    /// with the condition that x is positive and y=0. If x is negative and
995 983
    /// y=0 then, -x is of exponential distribution. The same is true for the
996 984
    /// y-coordinate.
997 985
    dim2::Point<double> exponential2()
998 986
    {
999 987
      double V1,V2,S;
1000 988
      do {
1001 989
        V1=2*real<double>()-1;
1002 990
        V2=2*real<double>()-1;
1003 991
        S=V1*V1+V2*V2;
1004 992
      } while(S>=1);
1005 993
      double W=-std::log(S)/S;
1006 994
      return dim2::Point<double>(W*V1,W*V2);
1007 995
    }
1008 996

	
1009 997
    ///@}
1010 998
  };
1011 999

	
1012 1000

	
1013 1001
  extern Random rnd;
1014 1002

	
1015 1003
}
1016 1004

	
1017 1005
#endif
Ignore white space 6 line context
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-2009
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_SMART_GRAPH_H
20 20
#define LEMON_SMART_GRAPH_H
21 21

	
22 22
///\ingroup graphs
23 23
///\file
24 24
///\brief SmartDigraph and SmartGraph classes.
25 25

	
26 26
#include <vector>
27 27

	
28 28
#include <lemon/core.h>
29 29
#include <lemon/error.h>
30 30
#include <lemon/bits/graph_extender.h>
31 31

	
32 32
namespace lemon {
33 33

	
34 34
  class SmartDigraph;
35
  ///Base of SmartDigraph
36 35

	
37
  ///Base of SmartDigraph
38
  ///
39 36
  class SmartDigraphBase {
40 37
  protected:
41 38

	
42 39
    struct NodeT
43 40
    {
44 41
      int first_in, first_out;
45 42
      NodeT() {}
46 43
    };
47 44
    struct ArcT
48 45
    {
49 46
      int target, source, next_in, next_out;
50 47
      ArcT() {}
51 48
    };
52 49

	
53 50
    std::vector<NodeT> nodes;
54 51
    std::vector<ArcT> arcs;
55 52

	
56 53
  public:
57 54

	
58
    typedef SmartDigraphBase Graph;
55
    typedef SmartDigraphBase Digraph;
59 56

	
60 57
    class Node;
61 58
    class Arc;
62 59

	
63 60
  public:
64 61

	
65 62
    SmartDigraphBase() : nodes(), arcs() { }
66 63
    SmartDigraphBase(const SmartDigraphBase &_g)
67 64
      : nodes(_g.nodes), arcs(_g.arcs) { }
68 65

	
69 66
    typedef True NodeNumTag;
70 67
    typedef True ArcNumTag;
71 68

	
72 69
    int nodeNum() const { return nodes.size(); }
73 70
    int arcNum() const { return arcs.size(); }
74 71

	
75 72
    int maxNodeId() const { return nodes.size()-1; }
76 73
    int maxArcId() const { return arcs.size()-1; }
77 74

	
78 75
    Node addNode() {
79 76
      int n = nodes.size();
80 77
      nodes.push_back(NodeT());
81 78
      nodes[n].first_in = -1;
82 79
      nodes[n].first_out = -1;
83 80
      return Node(n);
84 81
    }
85 82

	
86 83
    Arc addArc(Node u, Node v) {
87 84
      int n = arcs.size();
88 85
      arcs.push_back(ArcT());
89 86
      arcs[n].source = u._id;
90 87
      arcs[n].target = v._id;
91 88
      arcs[n].next_out = nodes[u._id].first_out;
92 89
      arcs[n].next_in = nodes[v._id].first_in;
93 90
      nodes[u._id].first_out = nodes[v._id].first_in = n;
94 91

	
95 92
      return Arc(n);
96 93
    }
97 94

	
98 95
    void clear() {
99 96
      arcs.clear();
100 97
      nodes.clear();
101 98
    }
102 99

	
103 100
    Node source(Arc a) const { return Node(arcs[a._id].source); }
104 101
    Node target(Arc a) const { return Node(arcs[a._id].target); }
105 102

	
106 103
    static int id(Node v) { return v._id; }
107 104
    static int id(Arc a) { return a._id; }
108 105

	
109 106
    static Node nodeFromId(int id) { return Node(id);}
110 107
    static Arc arcFromId(int id) { return Arc(id);}
111 108

	
112 109
    bool valid(Node n) const {
113 110
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
114 111
    }
115 112
    bool valid(Arc a) const {
116 113
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
117 114
    }
118 115

	
119 116
    class Node {
120 117
      friend class SmartDigraphBase;
121 118
      friend class SmartDigraph;
122 119

	
123 120
    protected:
124 121
      int _id;
125 122
      explicit Node(int id) : _id(id) {}
126 123
    public:
127 124
      Node() {}
128 125
      Node (Invalid) : _id(-1) {}
129 126
      bool operator==(const Node i) const {return _id == i._id;}
130 127
      bool operator!=(const Node i) const {return _id != i._id;}
131 128
      bool operator<(const Node i) const {return _id < i._id;}
132 129
    };
133 130

	
134 131

	
135 132
    class Arc {
136 133
      friend class SmartDigraphBase;
137 134
      friend class SmartDigraph;
138 135

	
139 136
    protected:
140 137
      int _id;
141 138
      explicit Arc(int id) : _id(id) {}
142 139
    public:
143 140
      Arc() { }
144 141
      Arc (Invalid) : _id(-1) {}
145 142
      bool operator==(const Arc i) const {return _id == i._id;}
146 143
      bool operator!=(const Arc i) const {return _id != i._id;}
147 144
      bool operator<(const Arc i) const {return _id < i._id;}
148 145
    };
149 146

	
150 147
    void first(Node& node) const {
151 148
      node._id = nodes.size() - 1;
152 149
    }
153 150

	
154 151
    static void next(Node& node) {
155 152
      --node._id;
156 153
    }
157 154

	
158 155
    void first(Arc& arc) const {
159 156
      arc._id = arcs.size() - 1;
160 157
    }
161 158

	
162 159
    static void next(Arc& arc) {
163 160
      --arc._id;
164 161
    }
165 162

	
166 163
    void firstOut(Arc& arc, const Node& node) const {
167 164
      arc._id = nodes[node._id].first_out;
168 165
    }
169 166

	
170 167
    void nextOut(Arc& arc) const {
171 168
      arc._id = arcs[arc._id].next_out;
172 169
    }
173 170

	
174 171
    void firstIn(Arc& arc, const Node& node) const {
175 172
      arc._id = nodes[node._id].first_in;
176 173
    }
177 174

	
178 175
    void nextIn(Arc& arc) const {
179 176
      arc._id = arcs[arc._id].next_in;
180 177
    }
181 178

	
182 179
  };
183 180

	
184 181
  typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase;
185 182

	
186 183
  ///\ingroup graphs
187 184
  ///
188 185
  ///\brief A smart directed graph class.
189 186
  ///
190
  ///This is a simple and fast digraph implementation.
191
  ///It is also quite memory efficient, but at the price
192
  ///that <b> it does support only limited (only stack-like)
193
  ///node and arc deletions</b>.
194
  ///It conforms to the \ref concepts::Digraph "Digraph concept" with
195
  ///an important extra feature that its maps are real \ref
196
  ///concepts::ReferenceMap "reference map"s.
187
  ///\ref SmartDigraph is a simple and fast digraph implementation.
188
  ///It is also quite memory efficient but at the price
189
  ///that it does not support node and arc deletion 
190
  ///(except for the Snapshot feature).
197 191
  ///
198
  ///\sa concepts::Digraph.
192
  ///This type fully conforms to the \ref concepts::Digraph "Digraph concept"
193
  ///and it also provides some additional functionalities.
194
  ///Most of its member functions and nested classes are documented
195
  ///only in the concept class.
196
  ///
197
  ///\sa concepts::Digraph
198
  ///\sa SmartGraph
199 199
  class SmartDigraph : public ExtendedSmartDigraphBase {
200
  public:
201

	
202 200
    typedef ExtendedSmartDigraphBase Parent;
203 201

	
204 202
  private:
205

	
206
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
207

	
208
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
209
    ///
203
    /// Digraphs are \e not copy constructible. Use DigraphCopy instead.
210 204
    SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
211
    ///\brief Assignment of SmartDigraph to another one is \e not allowed.
212
    ///Use DigraphCopy() instead.
213

	
214
    ///Assignment of SmartDigraph to another one is \e not allowed.
215
    ///Use DigraphCopy() instead.
205
    /// \brief Assignment of a digraph to another one is \e not allowed.
206
    /// Use DigraphCopy instead.
216 207
    void operator=(const SmartDigraph &) {}
217 208

	
218 209
  public:
219 210

	
220 211
    /// Constructor
221 212

	
222 213
    /// Constructor.
223 214
    ///
224 215
    SmartDigraph() {};
225 216

	
226 217
    ///Add a new node to the digraph.
227 218

	
228
    /// \return the new node.
229
    ///
219
    ///This function adds a new node to the digraph.
220
    ///\return The new node.
230 221
    Node addNode() { return Parent::addNode(); }
231 222

	
232 223
    ///Add a new arc to the digraph.
233 224

	
234
    ///Add a new arc to the digraph with source node \c s
225
    ///This function adds a new arc to the digraph with source node \c s
235 226
    ///and target node \c t.
236
    ///\return the new arc.
237
    Arc addArc(const Node& s, const Node& t) {
227
    ///\return The new arc.
228
    Arc addArc(Node s, Node t) {
238 229
      return Parent::addArc(s, t);
239 230
    }
240 231

	
241
    /// \brief Using this it is possible to avoid the superfluous memory
242
    /// allocation.
243

	
244
    /// Using this it is possible to avoid the superfluous memory
245
    /// allocation: if you know that the digraph you want to build will
246
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
247
    /// then it is worth reserving space for this amount before starting
248
    /// to build the digraph.
249
    /// \sa reserveArc
250
    void reserveNode(int n) { nodes.reserve(n); };
251

	
252
    /// \brief Using this it is possible to avoid the superfluous memory
253
    /// allocation.
254

	
255
    /// Using this it is possible to avoid the superfluous memory
256
    /// allocation: if you know that the digraph you want to build will
257
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
258
    /// then it is worth reserving space for this amount before starting
259
    /// to build the digraph.
260
    /// \sa reserveNode
261
    void reserveArc(int m) { arcs.reserve(m); };
262

	
263 232
    /// \brief Node validity check
264 233
    ///
265
    /// This function gives back true if the given node is valid,
266
    /// ie. it is a real node of the graph.
234
    /// This function gives back \c true if the given node is valid,
235
    /// i.e. it is a real node of the digraph.
267 236
    ///
268 237
    /// \warning A removed node (using Snapshot) could become valid again
269
    /// when new nodes are added to the graph.
238
    /// if new nodes are added to the digraph.
270 239
    bool valid(Node n) const { return Parent::valid(n); }
271 240

	
272 241
    /// \brief Arc validity check
273 242
    ///
274
    /// This function gives back true if the given arc is valid,
275
    /// ie. it is a real arc of the graph.
243
    /// This function gives back \c true if the given arc is valid,
244
    /// i.e. it is a real arc of the digraph.
276 245
    ///
277 246
    /// \warning A removed arc (using Snapshot) could become valid again
278
    /// when new arcs are added to the graph.
247
    /// if new arcs are added to the graph.
279 248
    bool valid(Arc a) const { return Parent::valid(a); }
280 249

	
281
    ///Clear the digraph.
282

	
283
    ///Erase all the nodes and arcs from the digraph.
284
    ///
285
    void clear() {
286
      Parent::clear();
287
    }
288

	
289 250
    ///Split a node.
290 251

	
291
    ///This function splits a node. First a new node is added to the digraph,
292
    ///then the source of each outgoing arc of \c n is moved to this new node.
293
    ///If \c connect is \c true (this is the default value), then a new arc
294
    ///from \c n to the newly created node is also added.
252
    ///This function splits the given node. First, a new node is added
253
    ///to the digraph, then the source of each outgoing arc of node \c n
254
    ///is moved to this new node.
255
    ///If the second parameter \c connect is \c true (this is the default
256
    ///value), then a new arc from node \c n to the newly created node
257
    ///is also added.
295 258
    ///\return The newly created node.
296 259
    ///
297
    ///\note The <tt>Arc</tt>s
298
    ///referencing a moved arc remain
299
    ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
300
    ///may be invalidated.
260
    ///\note All iterators remain valid.
261
    ///
301 262
    ///\warning This functionality cannot be used together with the Snapshot
302 263
    ///feature.
303 264
    Node split(Node n, bool connect = true)
304 265
    {
305 266
      Node b = addNode();
306 267
      nodes[b._id].first_out=nodes[n._id].first_out;
307 268
      nodes[n._id].first_out=-1;
308 269
      for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
309 270
        arcs[i].source=b._id;
310 271
      }
311 272
      if(connect) addArc(n,b);
312 273
      return b;
313 274
    }
314 275

	
276
    ///Clear the digraph.
277

	
278
    ///This function erases all nodes and arcs from the digraph.
279
    ///
280
    void clear() {
281
      Parent::clear();
282
    }
283

	
284
    /// Reserve memory for nodes.
285

	
286
    /// Using this function, it is possible to avoid superfluous memory
287
    /// allocation: if you know that the digraph you want to build will
288
    /// be large (e.g. it will contain millions of nodes and/or arcs),
289
    /// then it is worth reserving space for this amount before starting
290
    /// to build the digraph.
291
    /// \sa reserveArc()
292
    void reserveNode(int n) { nodes.reserve(n); };
293

	
294
    /// Reserve memory for arcs.
295

	
296
    /// Using this function, it is possible to avoid superfluous memory
297
    /// allocation: if you know that the digraph you want to build will
298
    /// be large (e.g. it will contain millions of nodes and/or arcs),
299
    /// then it is worth reserving space for this amount before starting
300
    /// to build the digraph.
301
    /// \sa reserveNode()
302
    void reserveArc(int m) { arcs.reserve(m); };
303

	
315 304
  public:
316 305

	
317 306
    class Snapshot;
318 307

	
319 308
  protected:
320 309

	
321 310
    void restoreSnapshot(const Snapshot &s)
322 311
    {
323 312
      while(s.arc_num<arcs.size()) {
324 313
        Arc arc = arcFromId(arcs.size()-1);
325 314
        Parent::notifier(Arc()).erase(arc);
326 315
        nodes[arcs.back().source].first_out=arcs.back().next_out;
327 316
        nodes[arcs.back().target].first_in=arcs.back().next_in;
328 317
        arcs.pop_back();
329 318
      }
330 319
      while(s.node_num<nodes.size()) {
331 320
        Node node = nodeFromId(nodes.size()-1);
332 321
        Parent::notifier(Node()).erase(node);
333 322
        nodes.pop_back();
334 323
      }
335 324
    }
336 325

	
337 326
  public:
338 327

	
339
    ///Class to make a snapshot of the digraph and to restrore to it later.
328
    ///Class to make a snapshot of the digraph and to restore it later.
340 329

	
341
    ///Class to make a snapshot of the digraph and to restrore to it later.
330
    ///Class to make a snapshot of the digraph and to restore it later.
342 331
    ///
343 332
    ///The newly added nodes and arcs can be removed using the
344
    ///restore() function.
345
    ///\note After you restore a state, you cannot restore
346
    ///a later state, in other word you cannot add again the arcs deleted
347
    ///by restore() using another one Snapshot instance.
333
    ///restore() function. This is the only way for deleting nodes and/or
334
    ///arcs from a SmartDigraph structure.
348 335
    ///
349
    ///\warning If you do not use correctly the snapshot that can cause
350
    ///either broken program, invalid state of the digraph, valid but
351
    ///not the restored digraph or no change. Because the runtime performance
352
    ///the validity of the snapshot is not stored.
336
    ///\note After a state is restored, you cannot restore a later state, 
337
    ///i.e. you cannot add the removed nodes and arcs again using
338
    ///another Snapshot instance.
339
    ///
340
    ///\warning Node splitting cannot be restored.
341
    ///\warning The validity of the snapshot is not stored due to
342
    ///performance reasons. If you do not use the snapshot correctly,
343
    ///it can cause broken program, invalid or not restored state of
344
    ///the digraph or no change.
353 345
    class Snapshot
354 346
    {
355 347
      SmartDigraph *_graph;
356 348
    protected:
357 349
      friend class SmartDigraph;
358 350
      unsigned int node_num;
359 351
      unsigned int arc_num;
360 352
    public:
361 353
      ///Default constructor.
362 354

	
363 355
      ///Default constructor.
364
      ///To actually make a snapshot you must call save().
365
      ///
356
      ///You have to call save() to actually make a snapshot.
366 357
      Snapshot() : _graph(0) {}
367 358
      ///Constructor that immediately makes a snapshot
368 359

	
369
      ///This constructor immediately makes a snapshot of the digraph.
370
      ///\param graph The digraph we make a snapshot of.
371
      Snapshot(SmartDigraph &graph) : _graph(&graph) {
360
      ///This constructor immediately makes a snapshot of the given digraph.
361
      ///
362
      Snapshot(SmartDigraph &gr) : _graph(&gr) {
372 363
        node_num=_graph->nodes.size();
373 364
        arc_num=_graph->arcs.size();
374 365
      }
375 366

	
376 367
      ///Make a snapshot.
377 368

	
378
      ///Make a snapshot of the digraph.
379
      ///
380
      ///This function can be called more than once. In case of a repeated
369
      ///This function makes a snapshot of the given digraph.
370
      ///It can be called more than once. In case of a repeated
381 371
      ///call, the previous snapshot gets lost.
382
      ///\param graph The digraph we make the snapshot of.
383
      void save(SmartDigraph &graph)
384
      {
385
        _graph=&graph;
372
      void save(SmartDigraph &gr) {
373
        _graph=&gr;
386 374
        node_num=_graph->nodes.size();
387 375
        arc_num=_graph->arcs.size();
388 376
      }
389 377

	
390 378
      ///Undo the changes until a snapshot.
391 379

	
392
      ///Undo the changes until a snapshot created by save().
393
      ///
394
      ///\note After you restored a state, you cannot restore
395
      ///a later state, in other word you cannot add again the arcs deleted
396
      ///by restore().
380
      ///This function undos the changes until the last snapshot
381
      ///created by save() or Snapshot(SmartDigraph&).
397 382
      void restore()
398 383
      {
399 384
        _graph->restoreSnapshot(*this);
400 385
      }
401 386
    };
402 387
  };
403 388

	
404 389

	
405 390
  class SmartGraphBase {
406 391

	
407 392
  protected:
408 393

	
409 394
    struct NodeT {
410 395
      int first_out;
411 396
    };
412 397

	
413 398
    struct ArcT {
414 399
      int target;
415 400
      int next_out;
416 401
    };
417 402

	
418 403
    std::vector<NodeT> nodes;
419 404
    std::vector<ArcT> arcs;
420 405

	
421 406
    int first_free_arc;
422 407

	
423 408
  public:
424 409

	
425
    typedef SmartGraphBase Digraph;
410
    typedef SmartGraphBase Graph;
426 411

	
427 412
    class Node;
428 413
    class Arc;
429 414
    class Edge;
430 415

	
431 416
    class Node {
432 417
      friend class SmartGraphBase;
433 418
    protected:
434 419

	
435 420
      int _id;
436 421
      explicit Node(int id) { _id = id;}
437 422

	
438 423
    public:
439 424
      Node() {}
440 425
      Node (Invalid) { _id = -1; }
441 426
      bool operator==(const Node& node) const {return _id == node._id;}
442 427
      bool operator!=(const Node& node) const {return _id != node._id;}
443 428
      bool operator<(const Node& node) const {return _id < node._id;}
444 429
    };
445 430

	
446 431
    class Edge {
447 432
      friend class SmartGraphBase;
448 433
    protected:
449 434

	
450 435
      int _id;
451 436
      explicit Edge(int id) { _id = id;}
452 437

	
453 438
    public:
454 439
      Edge() {}
455 440
      Edge (Invalid) { _id = -1; }
456 441
      bool operator==(const Edge& arc) const {return _id == arc._id;}
457 442
      bool operator!=(const Edge& arc) const {return _id != arc._id;}
458 443
      bool operator<(const Edge& arc) const {return _id < arc._id;}
459 444
    };
460 445

	
461 446
    class Arc {
462 447
      friend class SmartGraphBase;
463 448
    protected:
464 449

	
465 450
      int _id;
466 451
      explicit Arc(int id) { _id = id;}
467 452

	
468 453
    public:
469 454
      operator Edge() const {
470 455
        return _id != -1 ? edgeFromId(_id / 2) : INVALID;
471 456
      }
472 457

	
473 458
      Arc() {}
474 459
      Arc (Invalid) { _id = -1; }
475 460
      bool operator==(const Arc& arc) const {return _id == arc._id;}
476 461
      bool operator!=(const Arc& arc) const {return _id != arc._id;}
477 462
      bool operator<(const Arc& arc) const {return _id < arc._id;}
478 463
    };
479 464

	
480 465

	
481 466

	
482 467
    SmartGraphBase()
483 468
      : nodes(), arcs() {}
484 469

	
485 470
    typedef True NodeNumTag;
486 471
    typedef True EdgeNumTag;
487 472
    typedef True ArcNumTag;
488 473

	
489 474
    int nodeNum() const { return nodes.size(); }
490 475
    int edgeNum() const { return arcs.size() / 2; }
491 476
    int arcNum() const { return arcs.size(); }
492 477

	
493 478
    int maxNodeId() const { return nodes.size()-1; }
494 479
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
495 480
    int maxArcId() const { return arcs.size()-1; }
496 481

	
497 482
    Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
498 483
    Node target(Arc e) const { return Node(arcs[e._id].target); }
499 484

	
500 485
    Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
501 486
    Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
502 487

	
503 488
    static bool direction(Arc e) {
504 489
      return (e._id & 1) == 1;
505 490
    }
506 491

	
507 492
    static Arc direct(Edge e, bool d) {
508 493
      return Arc(e._id * 2 + (d ? 1 : 0));
509 494
    }
510 495

	
511 496
    void first(Node& node) const {
512 497
      node._id = nodes.size() - 1;
513 498
    }
514 499

	
515
    void next(Node& node) const {
500
    static void next(Node& node) {
516 501
      --node._id;
517 502
    }
518 503

	
519 504
    void first(Arc& arc) const {
520 505
      arc._id = arcs.size() - 1;
521 506
    }
522 507

	
523
    void next(Arc& arc) const {
508
    static void next(Arc& arc) {
524 509
      --arc._id;
525 510
    }
526 511

	
527 512
    void first(Edge& arc) const {
528 513
      arc._id = arcs.size() / 2 - 1;
529 514
    }
530 515

	
531
    void next(Edge& arc) const {
516
    static void next(Edge& arc) {
532 517
      --arc._id;
533 518
    }
534 519

	
535 520
    void firstOut(Arc &arc, const Node& v) const {
536 521
      arc._id = nodes[v._id].first_out;
537 522
    }
538 523
    void nextOut(Arc &arc) const {
539 524
      arc._id = arcs[arc._id].next_out;
540 525
    }
541 526

	
542 527
    void firstIn(Arc &arc, const Node& v) const {
543 528
      arc._id = ((nodes[v._id].first_out) ^ 1);
544 529
      if (arc._id == -2) arc._id = -1;
545 530
    }
546 531
    void nextIn(Arc &arc) const {
547 532
      arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
548 533
      if (arc._id == -2) arc._id = -1;
549 534
    }
550 535

	
551 536
    void firstInc(Edge &arc, bool& d, const Node& v) const {
552 537
      int de = nodes[v._id].first_out;
553 538
      if (de != -1) {
554 539
        arc._id = de / 2;
555 540
        d = ((de & 1) == 1);
556 541
      } else {
557 542
        arc._id = -1;
558 543
        d = true;
559 544
      }
560 545
    }
561 546
    void nextInc(Edge &arc, bool& d) const {
562 547
      int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
563 548
      if (de != -1) {
564 549
        arc._id = de / 2;
565 550
        d = ((de & 1) == 1);
566 551
      } else {
567 552
        arc._id = -1;
568 553
        d = true;
569 554
      }
570 555
    }
571 556

	
572 557
    static int id(Node v) { return v._id; }
573 558
    static int id(Arc e) { return e._id; }
574 559
    static int id(Edge e) { return e._id; }
575 560

	
576 561
    static Node nodeFromId(int id) { return Node(id);}
577 562
    static Arc arcFromId(int id) { return Arc(id);}
578 563
    static Edge edgeFromId(int id) { return Edge(id);}
579 564

	
580 565
    bool valid(Node n) const {
581 566
      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
582 567
    }
583 568
    bool valid(Arc a) const {
584 569
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
585 570
    }
586 571
    bool valid(Edge e) const {
587 572
      return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
588 573
    }
589 574

	
590 575
    Node addNode() {
591 576
      int n = nodes.size();
592 577
      nodes.push_back(NodeT());
593 578
      nodes[n].first_out = -1;
594 579

	
595 580
      return Node(n);
596 581
    }
597 582

	
598 583
    Edge addEdge(Node u, Node v) {
599 584
      int n = arcs.size();
600 585
      arcs.push_back(ArcT());
601 586
      arcs.push_back(ArcT());
602 587

	
603 588
      arcs[n].target = u._id;
604 589
      arcs[n | 1].target = v._id;
605 590

	
606 591
      arcs[n].next_out = nodes[v._id].first_out;
607 592
      nodes[v._id].first_out = n;
608 593

	
609 594
      arcs[n | 1].next_out = nodes[u._id].first_out;
610 595
      nodes[u._id].first_out = (n | 1);
611 596

	
612 597
      return Edge(n / 2);
613 598
    }
614 599

	
615 600
    void clear() {
616 601
      arcs.clear();
617 602
      nodes.clear();
618 603
    }
619 604

	
620 605
  };
621 606

	
622 607
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
623 608

	
624 609
  /// \ingroup graphs
625 610
  ///
626 611
  /// \brief A smart undirected graph class.
627 612
  ///
628
  /// This is a simple and fast graph implementation.
629
  /// It is also quite memory efficient, but at the price
630
  /// that <b> it does support only limited (only stack-like)
631
  /// node and arc deletions</b>.
632
  /// Except from this it conforms to
633
  /// the \ref concepts::Graph "Graph concept".
613
  /// \ref SmartGraph is a simple and fast graph implementation.
614
  /// It is also quite memory efficient but at the price
615
  /// that it does not support node and edge deletion 
616
  /// (except for the Snapshot feature).
634 617
  ///
635
  /// It also has an
636
  /// important extra feature that
637
  /// its maps are real \ref concepts::ReferenceMap "reference map"s.
618
  /// This type fully conforms to the \ref concepts::Graph "Graph concept"
619
  /// and it also provides some additional functionalities.
620
  /// Most of its member functions and nested classes are documented
621
  /// only in the concept class.
638 622
  ///
639
  /// \sa concepts::Graph.
640
  ///
623
  /// \sa concepts::Graph
624
  /// \sa SmartDigraph
641 625
  class SmartGraph : public ExtendedSmartGraphBase {
626
    typedef ExtendedSmartGraphBase Parent;
627

	
642 628
  private:
643

	
644
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
645

	
646
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
647
    ///
629
    /// Graphs are \e not copy constructible. Use GraphCopy instead.
648 630
    SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
649

	
650
    ///\brief Assignment of SmartGraph to another one is \e not allowed.
651
    ///Use GraphCopy() instead.
652

	
653
    ///Assignment of SmartGraph to another one is \e not allowed.
654
    ///Use GraphCopy() instead.
631
    /// \brief Assignment of a graph to another one is \e not allowed.
632
    /// Use GraphCopy instead.
655 633
    void operator=(const SmartGraph &) {}
656 634

	
657 635
  public:
658 636

	
659
    typedef ExtendedSmartGraphBase Parent;
660

	
661 637
    /// Constructor
662 638

	
663 639
    /// Constructor.
664 640
    ///
665 641
    SmartGraph() {}
666 642

	
667
    ///Add a new node to the graph.
668

	
669
    /// \return the new node.
643
    /// \brief Add a new node to the graph.
670 644
    ///
645
    /// This function adds a new node to the graph.
646
    /// \return The new node.
671 647
    Node addNode() { return Parent::addNode(); }
672 648

	
673
    ///Add a new edge to the graph.
674

	
675
    ///Add a new edge to the graph with node \c s
676
    ///and \c t.
677
    ///\return the new edge.
678
    Edge addEdge(const Node& s, const Node& t) {
679
      return Parent::addEdge(s, t);
649
    /// \brief Add a new edge to the graph.
650
    ///
651
    /// This function adds a new edge to the graph between nodes
652
    /// \c u and \c v with inherent orientation from node \c u to
653
    /// node \c v.
654
    /// \return The new edge.
655
    Edge addEdge(Node u, Node v) {
656
      return Parent::addEdge(u, v);
680 657
    }
681 658

	
682 659
    /// \brief Node validity check
683 660
    ///
684
    /// This function gives back true if the given node is valid,
685
    /// ie. it is a real node of the graph.
661
    /// This function gives back \c true if the given node is valid,
662
    /// i.e. it is a real node of the graph.
686 663
    ///
687 664
    /// \warning A removed node (using Snapshot) could become valid again
688
    /// when new nodes are added to the graph.
665
    /// if new nodes are added to the graph.
689 666
    bool valid(Node n) const { return Parent::valid(n); }
690 667

	
668
    /// \brief Edge validity check
669
    ///
670
    /// This function gives back \c true if the given edge is valid,
671
    /// i.e. it is a real edge of the graph.
672
    ///
673
    /// \warning A removed edge (using Snapshot) could become valid again
674
    /// if new edges are added to the graph.
675
    bool valid(Edge e) const { return Parent::valid(e); }
676

	
691 677
    /// \brief Arc validity check
692 678
    ///
693
    /// This function gives back true if the given arc is valid,
694
    /// ie. it is a real arc of the graph.
679
    /// This function gives back \c true if the given arc is valid,
680
    /// i.e. it is a real arc of the graph.
695 681
    ///
696 682
    /// \warning A removed arc (using Snapshot) could become valid again
697
    /// when new edges are added to the graph.
683
    /// if new edges are added to the graph.
698 684
    bool valid(Arc a) const { return Parent::valid(a); }
699 685

	
700
    /// \brief Edge validity check
701
    ///
702
    /// This function gives back true if the given edge is valid,
703
    /// ie. it is a real edge of the graph.
704
    ///
705
    /// \warning A removed edge (using Snapshot) could become valid again
706
    /// when new edges are added to the graph.
707
    bool valid(Edge e) const { return Parent::valid(e); }
708

	
709 686
    ///Clear the graph.
710 687

	
711
    ///Erase all the nodes and edges from the graph.
688
    ///This function erases all nodes and arcs from the graph.
712 689
    ///
713 690
    void clear() {
714 691
      Parent::clear();
715 692
    }
716 693

	
694
    /// Reserve memory for nodes.
695

	
696
    /// Using this function, it is possible to avoid superfluous memory
697
    /// allocation: if you know that the graph you want to build will
698
    /// be large (e.g. it will contain millions of nodes and/or edges),
699
    /// then it is worth reserving space for this amount before starting
700
    /// to build the graph.
701
    /// \sa reserveEdge()
702
    void reserveNode(int n) { nodes.reserve(n); };
703

	
704
    /// Reserve memory for edges.
705

	
706
    /// Using this function, it is possible to avoid superfluous memory
707
    /// allocation: if you know that the graph you want to build will
708
    /// be large (e.g. it will contain millions of nodes and/or edges),
709
    /// then it is worth reserving space for this amount before starting
710
    /// to build the graph.
711
    /// \sa reserveNode()
712
    void reserveEdge(int m) { arcs.reserve(2 * m); };
713

	
717 714
  public:
718 715

	
719 716
    class Snapshot;
720 717

	
721 718
  protected:
722 719

	
723 720
    void saveSnapshot(Snapshot &s)
724 721
    {
725 722
      s._graph = this;
726 723
      s.node_num = nodes.size();
727 724
      s.arc_num = arcs.size();
728 725
    }
729 726

	
730 727
    void restoreSnapshot(const Snapshot &s)
731 728
    {
732 729
      while(s.arc_num<arcs.size()) {
733 730
        int n=arcs.size()-1;
734 731
        Edge arc=edgeFromId(n/2);
735 732
        Parent::notifier(Edge()).erase(arc);
736 733
        std::vector<Arc> dir;
737 734
        dir.push_back(arcFromId(n));
738 735
        dir.push_back(arcFromId(n-1));
739 736
        Parent::notifier(Arc()).erase(dir);
740 737
        nodes[arcs[n-1].target].first_out=arcs[n].next_out;
741 738
        nodes[arcs[n].target].first_out=arcs[n-1].next_out;
742 739
        arcs.pop_back();
743 740
        arcs.pop_back();
744 741
      }
745 742
      while(s.node_num<nodes.size()) {
746 743
        int n=nodes.size()-1;
747 744
        Node node = nodeFromId(n);
748 745
        Parent::notifier(Node()).erase(node);
749 746
        nodes.pop_back();
750 747
      }
751 748
    }
752 749

	
753 750
  public:
754 751

	
755
    ///Class to make a snapshot of the digraph and to restrore to it later.
752
    ///Class to make a snapshot of the graph and to restore it later.
756 753

	
757
    ///Class to make a snapshot of the digraph and to restrore to it later.
754
    ///Class to make a snapshot of the graph and to restore it later.
758 755
    ///
759
    ///The newly added nodes and arcs can be removed using the
760
    ///restore() function.
756
    ///The newly added nodes and edges can be removed using the
757
    ///restore() function. This is the only way for deleting nodes and/or
758
    ///edges from a SmartGraph structure.
761 759
    ///
762
    ///\note After you restore a state, you cannot restore
763
    ///a later state, in other word you cannot add again the arcs deleted
764
    ///by restore() using another one Snapshot instance.
760
    ///\note After a state is restored, you cannot restore a later state, 
761
    ///i.e. you cannot add the removed nodes and edges again using
762
    ///another Snapshot instance.
765 763
    ///
766
    ///\warning If you do not use correctly the snapshot that can cause
767
    ///either broken program, invalid state of the digraph, valid but
768
    ///not the restored digraph or no change. Because the runtime performance
769
    ///the validity of the snapshot is not stored.
764
    ///\warning The validity of the snapshot is not stored due to
765
    ///performance reasons. If you do not use the snapshot correctly,
766
    ///it can cause broken program, invalid or not restored state of
767
    ///the graph or no change.
770 768
    class Snapshot
771 769
    {
772 770
      SmartGraph *_graph;
773 771
    protected:
774 772
      friend class SmartGraph;
775 773
      unsigned int node_num;
776 774
      unsigned int arc_num;
777 775
    public:
778 776
      ///Default constructor.
779 777

	
780 778
      ///Default constructor.
781
      ///To actually make a snapshot you must call save().
782
      ///
779
      ///You have to call save() to actually make a snapshot.
783 780
      Snapshot() : _graph(0) {}
784 781
      ///Constructor that immediately makes a snapshot
785 782

	
786
      ///This constructor immediately makes a snapshot of the digraph.
787
      ///\param graph The digraph we make a snapshot of.
788
      Snapshot(SmartGraph &graph) {
789
        graph.saveSnapshot(*this);
783
      /// This constructor immediately makes a snapshot of the given graph.
784
      ///
785
      Snapshot(SmartGraph &gr) {
786
        gr.saveSnapshot(*this);
790 787
      }
791 788

	
792 789
      ///Make a snapshot.
793 790

	
794
      ///Make a snapshot of the graph.
795
      ///
796
      ///This function can be called more than once. In case of a repeated
791
      ///This function makes a snapshot of the given graph.
792
      ///It can be called more than once. In case of a repeated
797 793
      ///call, the previous snapshot gets lost.
798
      ///\param graph The digraph we make the snapshot of.
799
      void save(SmartGraph &graph)
794
      void save(SmartGraph &gr)
800 795
      {
801
        graph.saveSnapshot(*this);
796
        gr.saveSnapshot(*this);
802 797
      }
803 798

	
804
      ///Undo the changes until a snapshot.
799
      ///Undo the changes until the last snapshot.
805 800

	
806
      ///Undo the changes until a snapshot created by save().
807
      ///
808
      ///\note After you restored a state, you cannot restore
809
      ///a later state, in other word you cannot add again the arcs deleted
810
      ///by restore().
801
      ///This function undos the changes until the last snapshot
802
      ///created by save() or Snapshot(SmartGraph&).
811 803
      void restore()
812 804
      {
813 805
        _graph->restoreSnapshot(*this);
814 806
      }
815 807
    };
816 808
  };
817 809

	
818 810
} //namespace lemon
819 811

	
820 812

	
821 813
#endif //LEMON_SMART_GRAPH_H
Ignore white space 6 line context
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 <lemon/soplex.h>
21 21

	
22
#include <soplex/soplex.h>
22
#include <soplex.h>
23
#include <spxout.h>
23 24

	
24 25

	
25 26
///\file
26 27
///\brief Implementation of the LEMON-SOPLEX lp solver interface.
27 28
namespace lemon {
28 29

	
29 30
  SoplexLp::SoplexLp() {
30 31
    soplex = new soplex::SoPlex;
32
    messageLevel(MESSAGE_NOTHING);
31 33
  }
32 34

	
33 35
  SoplexLp::~SoplexLp() {
34 36
    delete soplex;
35 37
  }
36 38

	
37 39
  SoplexLp::SoplexLp(const SoplexLp& lp) {
38 40
    rows = lp.rows;
39 41
    cols = lp.cols;
40 42

	
41 43
    soplex = new soplex::SoPlex;
42 44
    (*static_cast<soplex::SPxLP*>(soplex)) = *(lp.soplex);
43 45

	
44 46
    _col_names = lp._col_names;
45 47
    _col_names_ref = lp._col_names_ref;
46 48

	
47 49
    _row_names = lp._row_names;
48 50
    _row_names_ref = lp._row_names_ref;
49 51

	
52
    messageLevel(MESSAGE_NOTHING);
50 53
  }
51 54

	
52 55
  void SoplexLp::_clear_temporals() {
53 56
    _primal_values.clear();
54 57
    _dual_values.clear();
55 58
  }
56 59

	
57
  SoplexLp* SoplexLp::_newSolver() const {
60
  SoplexLp* SoplexLp::newSolver() const {
58 61
    SoplexLp* newlp = new SoplexLp();
59 62
    return newlp;
60 63
  }
61 64

	
62
  SoplexLp* SoplexLp::_cloneSolver() const {
65
  SoplexLp* SoplexLp::cloneSolver() const {
63 66
    SoplexLp* newlp = new SoplexLp(*this);
64 67
    return newlp;
65 68
  }
66 69

	
67 70
  const char* SoplexLp::_solverName() const { return "SoplexLp"; }
68 71

	
69 72
  int SoplexLp::_addCol() {
70 73
    soplex::LPCol c;
71 74
    c.setLower(-soplex::infinity);
72 75
    c.setUpper(soplex::infinity);
73 76
    soplex->addCol(c);
74 77

	
75 78
    _col_names.push_back(std::string());
76 79

	
77 80
    return soplex->nCols() - 1;
78 81
  }
79 82

	
80 83
  int SoplexLp::_addRow() {
81 84
    soplex::LPRow r;
82 85
    r.setLhs(-soplex::infinity);
83 86
    r.setRhs(soplex::infinity);
84 87
    soplex->addRow(r);
85 88

	
86 89
    _row_names.push_back(std::string());
87 90

	
88 91
    return soplex->nRows() - 1;
89 92
  }
90 93

	
94
  int SoplexLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
95
    soplex::DSVector v;
96
    for (ExprIterator it = b; it != e; ++it) {
97
      v.add(it->first, it->second);
98
    }
99
    soplex::LPRow r(l, v, u);
100
    soplex->addRow(r);
101

	
102
    _row_names.push_back(std::string());
103

	
104
    return soplex->nRows() - 1;
105
  }
106

	
91 107

	
92 108
  void SoplexLp::_eraseCol(int i) {
93 109
    soplex->removeCol(i);
94 110
    _col_names_ref.erase(_col_names[i]);
95 111
    _col_names[i] = _col_names.back();
96 112
    _col_names_ref[_col_names.back()] = i;
97 113
    _col_names.pop_back();
98 114
  }
99 115

	
100 116
  void SoplexLp::_eraseRow(int i) {
101 117
    soplex->removeRow(i);
102 118
    _row_names_ref.erase(_row_names[i]);
103 119
    _row_names[i] = _row_names.back();
104 120
    _row_names_ref[_row_names.back()] = i;
105 121
    _row_names.pop_back();
106 122
  }
107 123

	
108 124
  void SoplexLp::_eraseColId(int i) {
109 125
    cols.eraseIndex(i);
110 126
    cols.relocateIndex(i, cols.maxIndex());
111 127
  }
112 128
  void SoplexLp::_eraseRowId(int i) {
113 129
    rows.eraseIndex(i);
114 130
    rows.relocateIndex(i, rows.maxIndex());
115 131
  }
116 132

	
117 133
  void SoplexLp::_getColName(int c, std::string &name) const {
118 134
    name = _col_names[c];
119 135
  }
120 136

	
121 137
  void SoplexLp::_setColName(int c, const std::string &name) {
122 138
    _col_names_ref.erase(_col_names[c]);
123 139
    _col_names[c] = name;
124 140
    if (!name.empty()) {
125 141
      _col_names_ref.insert(std::make_pair(name, c));
126 142
    }
127 143
  }
128 144

	
129 145
  int SoplexLp::_colByName(const std::string& name) const {
130 146
    std::map<std::string, int>::const_iterator it =
131 147
      _col_names_ref.find(name);
132 148
    if (it != _col_names_ref.end()) {
133 149
      return it->second;
134 150
    } else {
135 151
      return -1;
136 152
    }
137 153
  }
138 154

	
139 155
  void SoplexLp::_getRowName(int r, std::string &name) const {
140 156
    name = _row_names[r];
141 157
  }
142 158

	
143 159
  void SoplexLp::_setRowName(int r, const std::string &name) {
144 160
    _row_names_ref.erase(_row_names[r]);
145 161
    _row_names[r] = name;
146 162
    if (!name.empty()) {
147 163
      _row_names_ref.insert(std::make_pair(name, r));
148 164
    }
149 165
  }
150 166

	
151 167
  int SoplexLp::_rowByName(const std::string& name) const {
152 168
    std::map<std::string, int>::const_iterator it =
153 169
      _row_names_ref.find(name);
154 170
    if (it != _row_names_ref.end()) {
155 171
      return it->second;
156 172
    } else {
157 173
      return -1;
158 174
    }
159 175
  }
160 176

	
161 177

	
162 178
  void SoplexLp::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
163 179
    for (int j = 0; j < soplex->nCols(); ++j) {
164 180
      soplex->changeElement(i, j, 0.0);
165 181
    }
166 182
    for(ExprIterator it = b; it != e; ++it) {
167 183
      soplex->changeElement(i, it->first, it->second);
168 184
    }
169 185
  }
170 186

	
171 187
  void SoplexLp::_getRowCoeffs(int i, InsertIterator b) const {
172 188
    const soplex::SVector& vec = soplex->rowVector(i);
173 189
    for (int k = 0; k < vec.size(); ++k) {
174 190
      *b = std::make_pair(vec.index(k), vec.value(k));
175 191
      ++b;
176 192
    }
177 193
  }
178 194

	
179 195
  void SoplexLp::_setColCoeffs(int j, ExprIterator b, ExprIterator e) {
180 196
    for (int i = 0; i < soplex->nRows(); ++i) {
181 197
      soplex->changeElement(i, j, 0.0);
182 198
    }
183 199
    for(ExprIterator it = b; it != e; ++it) {
184 200
      soplex->changeElement(it->first, j, it->second);
185 201
    }
186 202
  }
187 203

	
188 204
  void SoplexLp::_getColCoeffs(int i, InsertIterator b) const {
189 205
    const soplex::SVector& vec = soplex->colVector(i);
190 206
    for (int k = 0; k < vec.size(); ++k) {
191 207
      *b = std::make_pair(vec.index(k), vec.value(k));
192 208
      ++b;
193 209
    }
194 210
  }
195 211

	
196 212
  void SoplexLp::_setCoeff(int i, int j, Value value) {
197 213
    soplex->changeElement(i, j, value);
198 214
  }
199 215

	
200 216
  SoplexLp::Value SoplexLp::_getCoeff(int i, int j) const {
201 217
    return soplex->rowVector(i)[j];
202 218
  }
203 219

	
204 220
  void SoplexLp::_setColLowerBound(int i, Value value) {
205 221
    LEMON_ASSERT(value != INF, "Invalid bound");
206 222
    soplex->changeLower(i, value != -INF ? value : -soplex::infinity);
207 223
  }
208 224

	
209 225
  SoplexLp::Value SoplexLp::_getColLowerBound(int i) const {
210 226
    double value = soplex->lower(i);
211 227
    return value != -soplex::infinity ? value : -INF;
212 228
  }
213 229

	
214 230
  void SoplexLp::_setColUpperBound(int i, Value value) {
215 231
    LEMON_ASSERT(value != -INF, "Invalid bound");
216 232
    soplex->changeUpper(i, value != INF ? value : soplex::infinity);
217 233
  }
218 234

	
219 235
  SoplexLp::Value SoplexLp::_getColUpperBound(int i) const {
220 236
    double value = soplex->upper(i);
221 237
    return value != soplex::infinity ? value : INF;
222 238
  }
223 239

	
224 240
  void SoplexLp::_setRowLowerBound(int i, Value lb) {
225 241
    LEMON_ASSERT(lb != INF, "Invalid bound");
226 242
    soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity, soplex->rhs(i));
227 243
  }
228 244

	
229 245
  SoplexLp::Value SoplexLp::_getRowLowerBound(int i) const {
230 246
    double res = soplex->lhs(i);
231 247
    return res == -soplex::infinity ? -INF : res;
232 248
  }
233 249

	
234 250
  void SoplexLp::_setRowUpperBound(int i, Value ub) {
235 251
    LEMON_ASSERT(ub != -INF, "Invalid bound");
236 252
    soplex->changeRange(i, soplex->lhs(i), ub != INF ? ub : soplex::infinity);
237 253
  }
238 254

	
239 255
  SoplexLp::Value SoplexLp::_getRowUpperBound(int i) const {
240 256
    double res = soplex->rhs(i);
241 257
    return res == soplex::infinity ? INF : res;
242 258
  }
243 259

	
244 260
  void SoplexLp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
245 261
    for (int j = 0; j < soplex->nCols(); ++j) {
246 262
      soplex->changeObj(j, 0.0);
247 263
    }
248 264
    for (ExprIterator it = b; it != e; ++it) {
249 265
      soplex->changeObj(it->first, it->second);
250 266
    }
251 267
  }
252 268

	
253 269
  void SoplexLp::_getObjCoeffs(InsertIterator b) const {
254 270
    for (int j = 0; j < soplex->nCols(); ++j) {
255 271
      Value coef = soplex->obj(j);
256 272
      if (coef != 0.0) {
257 273
        *b = std::make_pair(j, coef);
258 274
        ++b;
259 275
      }
260 276
    }
261 277
  }
262 278

	
263 279
  void SoplexLp::_setObjCoeff(int i, Value obj_coef) {
264 280
    soplex->changeObj(i, obj_coef);
265 281
  }
266 282

	
267 283
  SoplexLp::Value SoplexLp::_getObjCoeff(int i) const {
268 284
    return soplex->obj(i);
269 285
  }
270 286

	
271 287
  SoplexLp::SolveExitStatus SoplexLp::_solve() {
272 288

	
273 289
    _clear_temporals();
290
    
291
    _applyMessageLevel();
274 292

	
275 293
    soplex::SPxSolver::Status status = soplex->solve();
276 294

	
277 295
    switch (status) {
278 296
    case soplex::SPxSolver::OPTIMAL:
279 297
    case soplex::SPxSolver::INFEASIBLE:
280 298
    case soplex::SPxSolver::UNBOUNDED:
281 299
      return SOLVED;
282 300
    default:
283 301
      return UNSOLVED;
284 302
    }
285 303
  }
286 304

	
287 305
  SoplexLp::Value SoplexLp::_getPrimal(int i) const {
288 306
    if (_primal_values.empty()) {
289 307
      _primal_values.resize(soplex->nCols());
290 308
      soplex::Vector pv(_primal_values.size(), &_primal_values.front());
291 309
      soplex->getPrimal(pv);
292 310
    }
293 311
    return _primal_values[i];
294 312
  }
295 313

	
296 314
  SoplexLp::Value SoplexLp::_getDual(int i) const {
297 315
    if (_dual_values.empty()) {
298 316
      _dual_values.resize(soplex->nRows());
299 317
      soplex::Vector dv(_dual_values.size(), &_dual_values.front());
300 318
      soplex->getDual(dv);
301 319
    }
302 320
    return _dual_values[i];
303 321
  }
304 322

	
305 323
  SoplexLp::Value SoplexLp::_getPrimalValue() const {
306 324
    return soplex->objValue();
307 325
  }
308 326

	
309 327
  SoplexLp::VarStatus SoplexLp::_getColStatus(int i) const {
310 328
    switch (soplex->getBasisColStatus(i)) {
311 329
    case soplex::SPxSolver::BASIC:
312 330
      return BASIC;
313 331
    case soplex::SPxSolver::ON_UPPER:
314 332
      return UPPER;
315 333
    case soplex::SPxSolver::ON_LOWER:
316 334
      return LOWER;
317 335
    case soplex::SPxSolver::FIXED:
318 336
      return FIXED;
319 337
    case soplex::SPxSolver::ZERO:
320 338
      return FREE;
321 339
    default:
322 340
      LEMON_ASSERT(false, "Wrong column status");
323 341
      return VarStatus();
324 342
    }
325 343
  }
326 344

	
327 345
  SoplexLp::VarStatus SoplexLp::_getRowStatus(int i) const {
328 346
    switch (soplex->getBasisRowStatus(i)) {
329 347
    case soplex::SPxSolver::BASIC:
330 348
      return BASIC;
331 349
    case soplex::SPxSolver::ON_UPPER:
332 350
      return UPPER;
333 351
    case soplex::SPxSolver::ON_LOWER:
334 352
      return LOWER;
335 353
    case soplex::SPxSolver::FIXED:
336 354
      return FIXED;
337 355
    case soplex::SPxSolver::ZERO:
338 356
      return FREE;
339 357
    default:
340 358
      LEMON_ASSERT(false, "Wrong row status");
341 359
      return VarStatus();
342 360
    }
343 361
  }
344 362

	
345 363
  SoplexLp::Value SoplexLp::_getPrimalRay(int i) const {
346 364
    if (_primal_ray.empty()) {
347 365
      _primal_ray.resize(soplex->nCols());
348 366
      soplex::Vector pv(_primal_ray.size(), &_primal_ray.front());
349 367
      soplex->getDualfarkas(pv);
350 368
    }
351 369
    return _primal_ray[i];
352 370
  }
353 371

	
354 372
  SoplexLp::Value SoplexLp::_getDualRay(int i) const {
355 373
    if (_dual_ray.empty()) {
356 374
      _dual_ray.resize(soplex->nRows());
357 375
      soplex::Vector dv(_dual_ray.size(), &_dual_ray.front());
358 376
      soplex->getDualfarkas(dv);
359 377
    }
360 378
    return _dual_ray[i];
361 379
  }
362 380

	
363 381
  SoplexLp::ProblemType SoplexLp::_getPrimalType() const {
364 382
    switch (soplex->status()) {
365 383
    case soplex::SPxSolver::OPTIMAL:
366 384
      return OPTIMAL;
367 385
    case soplex::SPxSolver::UNBOUNDED:
368 386
      return UNBOUNDED;
369 387
    case soplex::SPxSolver::INFEASIBLE:
370 388
      return INFEASIBLE;
371 389
    default:
372 390
      return UNDEFINED;
373 391
    }
374 392
  }
375 393

	
376 394
  SoplexLp::ProblemType SoplexLp::_getDualType() const {
377 395
    switch (soplex->status()) {
378 396
    case soplex::SPxSolver::OPTIMAL:
379 397
      return OPTIMAL;
380 398
    case soplex::SPxSolver::UNBOUNDED:
381 399
      return UNBOUNDED;
382 400
    case soplex::SPxSolver::INFEASIBLE:
383 401
      return INFEASIBLE;
384 402
    default:
385 403
      return UNDEFINED;
386 404
    }
387 405
  }
388 406

	
389 407
  void SoplexLp::_setSense(Sense sense) {
390 408
    switch (sense) {
391 409
    case MIN:
392 410
      soplex->changeSense(soplex::SPxSolver::MINIMIZE);
393 411
      break;
394 412
    case MAX:
395 413
      soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
396 414
    }
397 415
  }
398 416

	
399 417
  SoplexLp::Sense SoplexLp::_getSense() const {
400 418
    switch (soplex->spxSense()) {
401 419
    case soplex::SPxSolver::MAXIMIZE:
402 420
      return MAX;
403 421
    case soplex::SPxSolver::MINIMIZE:
404 422
      return MIN;
405 423
    default:
406 424
      LEMON_ASSERT(false, "Wrong sense.");
407 425
      return SoplexLp::Sense();
408 426
    }
409 427
  }
410 428

	
411 429
  void SoplexLp::_clear() {
412 430
    soplex->clear();
413 431
    _col_names.clear();
414 432
    _col_names_ref.clear();
415 433
    _row_names.clear();
416 434
    _row_names_ref.clear();
417 435
    cols.clear();
418 436
    rows.clear();
419 437
    _clear_temporals();
420 438
  }
421 439

	
440
  void SoplexLp::_messageLevel(MessageLevel level) {
441
    switch (level) {
442
    case MESSAGE_NOTHING:
443
      _message_level = -1;
444
      break;
445
    case MESSAGE_ERROR:
446
      _message_level = soplex::SPxOut::ERROR;
447
      break;
448
    case MESSAGE_WARNING:
449
      _message_level = soplex::SPxOut::WARNING;
450
      break;
451
    case MESSAGE_NORMAL:
452
      _message_level = soplex::SPxOut::INFO2;
453
      break;
454
    case MESSAGE_VERBOSE:
455
      _message_level = soplex::SPxOut::DEBUG;
456
      break;
457
    }
458
  }
459

	
460
  void SoplexLp::_applyMessageLevel() {
461
    soplex::Param::setVerbose(_message_level);
462
  }
463

	
422 464
} //namespace lemon
423 465

	
Ignore white space 6 line context
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_SOPLEX_H
20 20
#define LEMON_SOPLEX_H
21 21

	
22 22
///\file
23 23
///\brief Header of the LEMON-SOPLEX lp solver interface.
24 24

	
25 25
#include <vector>
26 26
#include <string>
27 27

	
28 28
#include <lemon/lp_base.h>
29 29

	
30 30
// Forward declaration
31 31
namespace soplex {
32 32
  class SoPlex;
33 33
}
34 34

	
35 35
namespace lemon {
36 36

	
37 37
  /// \ingroup lp_group
38 38
  ///
39 39
  /// \brief Interface for the SOPLEX solver
40 40
  ///
41 41
  /// This class implements an interface for the SoPlex LP solver.
42 42
  /// The SoPlex library is an object oriented lp solver library
43 43
  /// developed at the Konrad-Zuse-Zentrum f�r Informationstechnik
44 44
  /// Berlin (ZIB). You can find detailed information about it at the
45 45
  /// <tt>http://soplex.zib.de</tt> address.
46 46
  class SoplexLp : public LpSolver {
47 47
  private:
48 48

	
49 49
    soplex::SoPlex* soplex;
50 50

	
51 51
    std::vector<std::string> _col_names;
52 52
    std::map<std::string, int> _col_names_ref;
53 53

	
54 54
    std::vector<std::string> _row_names;
55 55
    std::map<std::string, int> _row_names_ref;
56 56

	
57 57
  private:
58 58

	
59 59
    // these values cannot be retrieved element by element
60 60
    mutable std::vector<Value> _primal_values;
61 61
    mutable std::vector<Value> _dual_values;
62 62

	
63 63
    mutable std::vector<Value> _primal_ray;
64 64
    mutable std::vector<Value> _dual_ray;
65 65

	
66 66
    void _clear_temporals();
67 67

	
68 68
  public:
69 69

	
70 70
    /// \e
71 71
    SoplexLp();
72 72
    /// \e
73 73
    SoplexLp(const SoplexLp&);
74 74
    /// \e
75 75
    ~SoplexLp();
76
    /// \e
77
    virtual SoplexLp* newSolver() const;
78
    /// \e
79
    virtual SoplexLp* cloneSolver() const;
76 80

	
77 81
  protected:
78 82

	
79
    virtual SoplexLp* _newSolver() const;
80
    virtual SoplexLp* _cloneSolver() const;
81

	
82 83
    virtual const char* _solverName() const;
83 84

	
84 85
    virtual int _addCol();
85 86
    virtual int _addRow();
87
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
86 88

	
87 89
    virtual void _eraseCol(int i);
88 90
    virtual void _eraseRow(int i);
89 91

	
90 92
    virtual void _eraseColId(int i);
91 93
    virtual void _eraseRowId(int i);
92 94

	
93 95
    virtual void _getColName(int col, std::string& name) const;
94 96
    virtual void _setColName(int col, const std::string& name);
95 97
    virtual int _colByName(const std::string& name) const;
96 98

	
97 99
    virtual void _getRowName(int row, std::string& name) const;
98 100
    virtual void _setRowName(int row, const std::string& name);
99 101
    virtual int _rowByName(const std::string& name) const;
100 102

	
101 103
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
102 104
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
103 105

	
104 106
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
105 107
    virtual void _getColCoeffs(int i, InsertIterator b) const;
106 108

	
107 109
    virtual void _setCoeff(int row, int col, Value value);
108 110
    virtual Value _getCoeff(int row, int col) const;
109 111

	
110 112
    virtual void _setColLowerBound(int i, Value value);
111 113
    virtual Value _getColLowerBound(int i) const;
112 114
    virtual void _setColUpperBound(int i, Value value);
113 115
    virtual Value _getColUpperBound(int i) const;
114 116

	
115 117
    virtual void _setRowLowerBound(int i, Value value);
116 118
    virtual Value _getRowLowerBound(int i) const;
117 119
    virtual void _setRowUpperBound(int i, Value value);
118 120
    virtual Value _getRowUpperBound(int i) const;
119 121

	
120 122
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
121 123
    virtual void _getObjCoeffs(InsertIterator b) const;
122 124

	
123 125
    virtual void _setObjCoeff(int i, Value obj_coef);
124 126
    virtual Value _getObjCoeff(int i) const;
125 127

	
126 128
    virtual void _setSense(Sense sense);
127 129
    virtual Sense _getSense() const;
128 130

	
129 131
    virtual SolveExitStatus _solve();
130 132
    virtual Value _getPrimal(int i) const;
131 133
    virtual Value _getDual(int i) const;
132 134

	
133 135
    virtual Value _getPrimalValue() const;
134 136

	
135 137
    virtual Value _getPrimalRay(int i) const;
136 138
    virtual Value _getDualRay(int i) const;
137 139

	
138 140
    virtual VarStatus _getColStatus(int i) const;
139 141
    virtual VarStatus _getRowStatus(int i) const;
140 142

	
141 143
    virtual ProblemType _getPrimalType() const;
142 144
    virtual ProblemType _getDualType() const;
143 145

	
144 146
    virtual void _clear();
145 147

	
148
    void _messageLevel(MessageLevel m);
149
    void _applyMessageLevel();
150

	
151
    int _message_level;
152

	
146 153
  };
147 154

	
148 155
} //END OF NAMESPACE LEMON
149 156

	
150 157
#endif //LEMON_SOPLEX_H
151 158

	
Ignore white space 6 line context
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-2009
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_SUURBALLE_H
20 20
#define LEMON_SUURBALLE_H
21 21

	
22 22
///\ingroup shortest_path
23 23
///\file
24 24
///\brief An algorithm for finding arc-disjoint paths between two
25 25
/// nodes having minimum total length.
26 26

	
27 27
#include <vector>
28
#include <limits>
28 29
#include <lemon/bin_heap.h>
29 30
#include <lemon/path.h>
31
#include <lemon/list_graph.h>
32
#include <lemon/maps.h>
30 33

	
31 34
namespace lemon {
32 35

	
33 36
  /// \addtogroup shortest_path
34 37
  /// @{
35 38

	
36 39
  /// \brief Algorithm for finding arc-disjoint paths between two nodes
37 40
  /// having minimum total length.
38 41
  ///
39 42
  /// \ref lemon::Suurballe "Suurballe" implements an algorithm for
40 43
  /// finding arc-disjoint paths having minimum total length (cost)
41 44
  /// from a given source node to a given target node in a digraph.
42 45
  ///
43
  /// In fact, this implementation is the specialization of the
44
  /// \ref CapacityScaling "successive shortest path" algorithm.
46
  /// Note that this problem is a special case of the \ref min_cost_flow
47
  /// "minimum cost flow problem". This implementation is actually an
48
  /// efficient specialized version of the \ref CapacityScaling
49
  /// "Successive Shortest Path" algorithm directly for this problem.
50
  /// Therefore this class provides query functions for flow values and
51
  /// node potentials (the dual solution) just like the minimum cost flow
52
  /// algorithms.
45 53
  ///
46
  /// \tparam Digraph The digraph type the algorithm runs on.
47
  /// The default value is \c ListDigraph.
48
  /// \tparam LengthMap The type of the length (cost) map.
49
  /// The default value is <tt>Digraph::ArcMap<int></tt>.
54
  /// \tparam GR The digraph type the algorithm runs on.
55
  /// \tparam LEN The type of the length map.
56
  /// The default value is <tt>GR::ArcMap<int></tt>.
50 57
  ///
51 58
  /// \warning Length values should be \e non-negative \e integers.
52 59
  ///
53 60
  /// \note For finding node-disjoint paths this algorithm can be used
54
  /// with \ref SplitNodes.
61
  /// along with the \ref SplitNodes adaptor.
55 62
#ifdef DOXYGEN
56
  template <typename Digraph, typename LengthMap>
63
  template <typename GR, typename LEN>
57 64
#else
58
  template < typename Digraph = ListDigraph,
59
             typename LengthMap = typename Digraph::template ArcMap<int> >
65
  template < typename GR,
66
             typename LEN = typename GR::template ArcMap<int> >
60 67
#endif
61 68
  class Suurballe
62 69
  {
63
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
70
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
64 71

	
65
    typedef typename LengthMap::Value Length;
66 72
    typedef ConstMap<Arc, int> ConstArcMap;
67
    typedef typename Digraph::template NodeMap<Arc> PredMap;
73
    typedef typename GR::template NodeMap<Arc> PredMap;
68 74

	
69 75
  public:
70 76

	
77
    /// The type of the digraph the algorithm runs on.
78
    typedef GR Digraph;
79
    /// The type of the length map.
80
    typedef LEN LengthMap;
81
    /// The type of the lengths.
82
    typedef typename LengthMap::Value Length;
83
#ifdef DOXYGEN
84
    /// The type of the flow map.
85
    typedef GR::ArcMap<int> FlowMap;
86
    /// The type of the potential map.
87
    typedef GR::NodeMap<Length> PotentialMap;
88
#else
71 89
    /// The type of the flow map.
72 90
    typedef typename Digraph::template ArcMap<int> FlowMap;
73 91
    /// The type of the potential map.
74 92
    typedef typename Digraph::template NodeMap<Length> PotentialMap;
93
#endif
94

	
75 95
    /// The type of the path structures.
76
    typedef SimplePath<Digraph> Path;
96
    typedef SimplePath<GR> Path;
77 97

	
78 98
  private:
79 99

	
80
    /// \brief Special implementation of the Dijkstra algorithm
81
    /// for finding shortest paths in the residual network.
82
    ///
83
    /// \ref ResidualDijkstra is a special implementation of the
84
    /// \ref Dijkstra algorithm for finding shortest paths in the
85
    /// residual network of the digraph with respect to the reduced arc
86
    /// lengths and modifying the node potentials according to the
87
    /// distance of the nodes.
100
    // ResidualDijkstra is a special implementation of the
101
    // Dijkstra algorithm for finding shortest paths in the
102
    // residual network with respect to the reduced arc lengths
103
    // and modifying the node potentials according to the
104
    // distance of the nodes.
88 105
    class ResidualDijkstra
89 106
    {
90 107
      typedef typename Digraph::template NodeMap<int> HeapCrossRef;
91 108
      typedef BinHeap<Length, HeapCrossRef> Heap;
92 109

	
93 110
    private:
94 111

	
95 112
      // The digraph the algorithm runs on
96 113
      const Digraph &_graph;
97 114

	
98 115
      // The main maps
99 116
      const FlowMap &_flow;
100 117
      const LengthMap &_length;
101 118
      PotentialMap &_potential;
102 119

	
103 120
      // The distance map
104 121
      PotentialMap _dist;
105 122
      // The pred arc map
106 123
      PredMap &_pred;
107 124
      // The processed (i.e. permanently labeled) nodes
108 125
      std::vector<Node> _proc_nodes;
109 126

	
110 127
      Node _s;
111 128
      Node _t;
112 129

	
113 130
    public:
114 131

	
115 132
      /// Constructor.
116
      ResidualDijkstra( const Digraph &digraph,
133
      ResidualDijkstra( const Digraph &graph,
117 134
                        const FlowMap &flow,
118 135
                        const LengthMap &length,
119 136
                        PotentialMap &potential,
120 137
                        PredMap &pred,
121 138
                        Node s, Node t ) :
122
        _graph(digraph), _flow(flow), _length(length), _potential(potential),
123
        _dist(digraph), _pred(pred), _s(s), _t(t) {}
139
        _graph(graph), _flow(flow), _length(length), _potential(potential),
140
        _dist(graph), _pred(pred), _s(s), _t(t) {}
124 141

	
125 142
      /// \brief Run the algorithm. It returns \c true if a path is found
126 143
      /// from the source node to the target node.
127 144
      bool run() {
128 145
        HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
129 146
        Heap heap(heap_cross_ref);
130 147
        heap.push(_s, 0);
131 148
        _pred[_s] = INVALID;
132 149
        _proc_nodes.clear();
133 150

	
134 151
        // Process nodes
135 152
        while (!heap.empty() && heap.top() != _t) {
136 153
          Node u = heap.top(), v;
137 154
          Length d = heap.prio() + _potential[u], nd;
138 155
          _dist[u] = heap.prio();
139 156
          heap.pop();
140 157
          _proc_nodes.push_back(u);
141 158

	
142 159
          // Traverse outgoing arcs
143 160
          for (OutArcIt e(_graph, u); e != INVALID; ++e) {
144 161
            if (_flow[e] == 0) {
145 162
              v = _graph.target(e);
146 163
              switch(heap.state(v)) {
147 164
              case Heap::PRE_HEAP:
148 165
                heap.push(v, d + _length[e] - _potential[v]);
149 166
                _pred[v] = e;
150 167
                break;
151 168
              case Heap::IN_HEAP:
152 169
                nd = d + _length[e] - _potential[v];
153 170
                if (nd < heap[v]) {
154 171
                  heap.decrease(v, nd);
155 172
                  _pred[v] = e;
156 173
                }
157 174
                break;
158 175
              case Heap::POST_HEAP:
159 176
                break;
160 177
              }
161 178
            }
162 179
          }
163 180

	
164 181
          // Traverse incoming arcs
165 182
          for (InArcIt e(_graph, u); e != INVALID; ++e) {
166 183
            if (_flow[e] == 1) {
167 184
              v = _graph.source(e);
168 185
              switch(heap.state(v)) {
169 186
              case Heap::PRE_HEAP:
170 187
                heap.push(v, d - _length[e] - _potential[v]);
171 188
                _pred[v] = e;
172 189
                break;
173 190
              case Heap::IN_HEAP:
174 191
                nd = d - _length[e] - _potential[v];
175 192
                if (nd < heap[v]) {
176 193
                  heap.decrease(v, nd);
177 194
                  _pred[v] = e;
178 195
                }
179 196
                break;
180 197
              case Heap::POST_HEAP:
181 198
                break;
182 199
              }
183 200
            }
184 201
          }
185 202
        }
186 203
        if (heap.empty()) return false;
187 204

	
188 205
        // Update potentials of processed nodes
189 206
        Length t_dist = heap.prio();
190 207
        for (int i = 0; i < int(_proc_nodes.size()); ++i)
191 208
          _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
192 209
        return true;
193 210
      }
194 211

	
195 212
    }; //class ResidualDijkstra
196 213

	
197 214
  private:
198 215

	
199 216
    // The digraph the algorithm runs on
200 217
    const Digraph &_graph;
201 218
    // The length map
202 219
    const LengthMap &_length;
203 220

	
204 221
    // Arc map of the current flow
205 222
    FlowMap *_flow;
206 223
    bool _local_flow;
207 224
    // Node map of the current potentials
208 225
    PotentialMap *_potential;
209 226
    bool _local_potential;
210 227

	
211 228
    // The source node
212 229
    Node _source;
213 230
    // The target node
214 231
    Node _target;
215 232

	
216 233
    // Container to store the found paths
217 234
    std::vector< SimplePath<Digraph> > paths;
218 235
    int _path_num;
219 236

	
220 237
    // The pred arc map
221 238
    PredMap _pred;
222 239
    // Implementation of the Dijkstra algorithm for finding augmenting
223 240
    // shortest paths in the residual network
224 241
    ResidualDijkstra *_dijkstra;
225 242

	
226 243
  public:
227 244

	
228 245
    /// \brief Constructor.
229 246
    ///
230 247
    /// Constructor.
231 248
    ///
232
    /// \param digraph The digraph the algorithm runs on.
249
    /// \param graph The digraph the algorithm runs on.
233 250
    /// \param length The length (cost) values of the arcs.
234
    /// \param s The source node.
235
    /// \param t The target node.
236
    Suurballe( const Digraph &digraph,
237
               const LengthMap &length,
238
               Node s, Node t ) :
239
      _graph(digraph), _length(length), _flow(0), _local_flow(false),
240
      _potential(0), _local_potential(false), _source(s), _target(t),
241
      _pred(digraph) {}
251
    Suurballe( const Digraph &graph,
252
               const LengthMap &length ) :
253
      _graph(graph), _length(length), _flow(0), _local_flow(false),
254
      _potential(0), _local_potential(false), _pred(graph)
255
    {
256
      LEMON_ASSERT(std::numeric_limits<Length>::is_integer,
257
        "The length type of Suurballe must be integer");
258
    }
242 259

	
243 260
    /// Destructor.
244 261
    ~Suurballe() {
245 262
      if (_local_flow) delete _flow;
246 263
      if (_local_potential) delete _potential;
247 264
      delete _dijkstra;
248 265
    }
249 266

	
250 267
    /// \brief Set the flow map.
251 268
    ///
252 269
    /// This function sets the flow map.
270
    /// If it is not used before calling \ref run() or \ref init(),
271
    /// an instance will be allocated automatically. The destructor
272
    /// deallocates this automatically allocated map, of course.
253 273
    ///
254
    /// The found flow contains only 0 and 1 values. It is the union of
255
    /// the found arc-disjoint paths.
274
    /// The found flow contains only 0 and 1 values, since it is the
275
    /// union of the found arc-disjoint paths.
256 276
    ///
257
    /// \return \c (*this)
277
    /// \return <tt>(*this)</tt>
258 278
    Suurballe& flowMap(FlowMap &map) {
259 279
      if (_local_flow) {
260 280
        delete _flow;
261 281
        _local_flow = false;
262 282
      }
263 283
      _flow = &map;
264 284
      return *this;
265 285
    }
266 286

	
267 287
    /// \brief Set the potential map.
268 288
    ///
269 289
    /// This function sets the potential map.
290
    /// If it is not used before calling \ref run() or \ref init(),
291
    /// an instance will be allocated automatically. The destructor
292
    /// deallocates this automatically allocated map, of course.
270 293
    ///
271
    /// The potentials provide the dual solution of the underlying
272
    /// minimum cost flow problem.
294
    /// The node potentials provide the dual solution of the underlying
295
    /// \ref min_cost_flow "minimum cost flow problem".
273 296
    ///
274
    /// \return \c (*this)
297
    /// \return <tt>(*this)</tt>
275 298
    Suurballe& potentialMap(PotentialMap &map) {
276 299
      if (_local_potential) {
277 300
        delete _potential;
278 301
        _local_potential = false;
279 302
      }
280 303
      _potential = &map;
281 304
      return *this;
282 305
    }
283 306

	
284
    /// \name Execution control
307
    /// \name Execution Control
285 308
    /// The simplest way to execute the algorithm is to call the run()
286 309
    /// function.
287 310
    /// \n
288 311
    /// If you only need the flow that is the union of the found
289 312
    /// arc-disjoint paths, you may call init() and findFlow().
290 313

	
291 314
    /// @{
292 315

	
293 316
    /// \brief Run the algorithm.
294 317
    ///
295 318
    /// This function runs the algorithm.
296 319
    ///
320
    /// \param s The source node.
321
    /// \param t The target node.
297 322
    /// \param k The number of paths to be found.
298 323
    ///
299 324
    /// \return \c k if there are at least \c k arc-disjoint paths from
300 325
    /// \c s to \c t in the digraph. Otherwise it returns the number of
301 326
    /// arc-disjoint paths found.
302 327
    ///
303
    /// \note Apart from the return value, <tt>s.run(k)</tt> is just a
304
    /// shortcut of the following code.
328
    /// \note Apart from the return value, <tt>s.run(s, t, k)</tt> is
329
    /// just a shortcut of the following code.
305 330
    /// \code
306
    ///   s.init();
307
    ///   s.findFlow(k);
331
    ///   s.init(s);
332
    ///   s.findFlow(t, k);
308 333
    ///   s.findPaths();
309 334
    /// \endcode
310
    int run(int k = 2) {
311
      init();
312
      findFlow(k);
335
    int run(const Node& s, const Node& t, int k = 2) {
336
      init(s);
337
      findFlow(t, k);
313 338
      findPaths();
314 339
      return _path_num;
315 340
    }
316 341

	
317 342
    /// \brief Initialize the algorithm.
318 343
    ///
319 344
    /// This function initializes the algorithm.
320
    void init() {
345
    ///
346
    /// \param s The source node.
347
    void init(const Node& s) {
348
      _source = s;
349

	
321 350
      // Initialize maps
322 351
      if (!_flow) {
323 352
        _flow = new FlowMap(_graph);
324 353
        _local_flow = true;
325 354
      }
326 355
      if (!_potential) {
327 356
        _potential = new PotentialMap(_graph);
328 357
        _local_potential = true;
329 358
      }
330 359
      for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
331 360
      for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
332

	
333
      _dijkstra = new ResidualDijkstra( _graph, *_flow, _length,
334
                                        *_potential, _pred,
335
                                        _source, _target );
336 361
    }
337 362

	
338
    /// \brief Execute the successive shortest path algorithm to find
339
    /// an optimal flow.
363
    /// \brief Execute the algorithm to find an optimal flow.
340 364
    ///
341 365
    /// This function executes the successive shortest path algorithm to
342
    /// find a minimum cost flow, which is the union of \c k or less
366
    /// find a minimum cost flow, which is the union of \c k (or less)
343 367
    /// arc-disjoint paths.
344 368
    ///
369
    /// \param t The target node.
370
    /// \param k The number of paths to be found.
371
    ///
345 372
    /// \return \c k if there are at least \c k arc-disjoint paths from
346
    /// \c s to \c t in the digraph. Otherwise it returns the number of
347
    /// arc-disjoint paths found.
373
    /// the source node to the given node \c t in the digraph.
374
    /// Otherwise it returns the number of arc-disjoint paths found.
348 375
    ///
349 376
    /// \pre \ref init() must be called before using this function.
350
    int findFlow(int k = 2) {
377
    int findFlow(const Node& t, int k = 2) {
378
      _target = t;
379
      _dijkstra =
380
        new ResidualDijkstra( _graph, *_flow, _length, *_potential, _pred,
381
                              _source, _target );
382

	
351 383
      // Find shortest paths
352 384
      _path_num = 0;
353 385
      while (_path_num < k) {
354 386
        // Run Dijkstra
355 387
        if (!_dijkstra->run()) break;
356 388
        ++_path_num;
357 389

	
358 390
        // Set the flow along the found shortest path
359 391
        Node u = _target;
360 392
        Arc e;
361 393
        while ((e = _pred[u]) != INVALID) {
362 394
          if (u == _graph.target(e)) {
363 395
            (*_flow)[e] = 1;
364 396
            u = _graph.source(e);
365 397
          } else {
366 398
            (*_flow)[e] = 0;
367 399
            u = _graph.target(e);
368 400
          }
369 401
        }
370 402
      }
371 403
      return _path_num;
372 404
    }
373 405

	
374 406
    /// \brief Compute the paths from the flow.
375 407
    ///
376
    /// This function computes the paths from the flow.
408
    /// This function computes the paths from the found minimum cost flow,
409
    /// which is the union of some arc-disjoint paths.
377 410
    ///
378 411
    /// \pre \ref init() and \ref findFlow() must be called before using
379 412
    /// this function.
380 413
    void findPaths() {
381
      // Create the residual flow map (the union of the paths not found
382
      // so far)
383 414
      FlowMap res_flow(_graph);
384 415
      for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
385 416

	
386 417
      paths.clear();
387 418
      paths.resize(_path_num);
388 419
      for (int i = 0; i < _path_num; ++i) {
389 420
        Node n = _source;
390 421
        while (n != _target) {
391 422
          OutArcIt e(_graph, n);
392 423
          for ( ; res_flow[e] == 0; ++e) ;
393 424
          n = _graph.target(e);
394 425
          paths[i].addBack(e);
395 426
          res_flow[e] = 0;
396 427
        }
397 428
      }
398 429
    }
399 430

	
400 431
    /// @}
401 432

	
402 433
    /// \name Query Functions
403 434
    /// The results of the algorithm can be obtained using these
404 435
    /// functions.
405 436
    /// \n The algorithm should be executed before using them.
406 437

	
407 438
    /// @{
408 439

	
409
    /// \brief Return a const reference to the arc map storing the
440
    /// \brief Return the total length of the found paths.
441
    ///
442
    /// This function returns the total length of the found paths, i.e.
443
    /// the total cost of the found flow.
444
    /// The complexity of the function is O(e).
445
    ///
446
    /// \pre \ref run() or \ref findFlow() must be called before using
447
    /// this function.
448
    Length totalLength() const {
449
      Length c = 0;
450
      for (ArcIt e(_graph); e != INVALID; ++e)
451
        c += (*_flow)[e] * _length[e];
452
      return c;
453
    }
454

	
455
    /// \brief Return the flow value on the given arc.
456
    ///
457
    /// This function returns the flow value on the given arc.
458
    /// It is \c 1 if the arc is involved in one of the found arc-disjoint
459
    /// paths, otherwise it is \c 0.
460
    ///
461
    /// \pre \ref run() or \ref findFlow() must be called before using
462
    /// this function.
463
    int flow(const Arc& arc) const {
464
      return (*_flow)[arc];
465
    }
466

	
467
    /// \brief Return a const reference to an arc map storing the
410 468
    /// found flow.
411 469
    ///
412
    /// This function returns a const reference to the arc map storing
470
    /// This function returns a const reference to an arc map storing
413 471
    /// the flow that is the union of the found arc-disjoint paths.
414 472
    ///
415 473
    /// \pre \ref run() or \ref findFlow() must be called before using
416 474
    /// this function.
417 475
    const FlowMap& flowMap() const {
418 476
      return *_flow;
419 477
    }
420 478

	
421
    /// \brief Return a const reference to the node map storing the
422
    /// found potentials (the dual solution).
423
    ///
424
    /// This function returns a const reference to the node map storing
425
    /// the found potentials that provide the dual solution of the
426
    /// underlying minimum cost flow problem.
427
    ///
428
    /// \pre \ref run() or \ref findFlow() must be called before using
429
    /// this function.
430
    const PotentialMap& potentialMap() const {
431
      return *_potential;
432
    }
433

	
434
    /// \brief Return the flow on the given arc.
435
    ///
436
    /// This function returns the flow on the given arc.
437
    /// It is \c 1 if the arc is involved in one of the found paths,
438
    /// otherwise it is \c 0.
439
    ///
440
    /// \pre \ref run() or \ref findFlow() must be called before using
441
    /// this function.
442
    int flow(const Arc& arc) const {
443
      return (*_flow)[arc];
444
    }
445

	
446 479
    /// \brief Return the potential of the given node.
447 480
    ///
448 481
    /// This function returns the potential of the given node.
482
    /// The node potentials provide the dual solution of the
483
    /// underlying \ref min_cost_flow "minimum cost flow problem".
449 484
    ///
450 485
    /// \pre \ref run() or \ref findFlow() must be called before using
451 486
    /// this function.
452 487
    Length potential(const Node& node) const {
453 488
      return (*_potential)[node];
454 489
    }
455 490

	
456
    /// \brief Return the total length (cost) of the found paths (flow).
491
    /// \brief Return a const reference to a node map storing the
492
    /// found potentials (the dual solution).
457 493
    ///
458
    /// This function returns the total length (cost) of the found paths
459
    /// (flow). The complexity of the function is \f$ O(e) \f$.
494
    /// This function returns a const reference to a node map storing
495
    /// the found potentials that provide the dual solution of the
496
    /// underlying \ref min_cost_flow "minimum cost flow problem".
460 497
    ///
461 498
    /// \pre \ref run() or \ref findFlow() must be called before using
462 499
    /// this function.
463
    Length totalLength() const {
464
      Length c = 0;
465
      for (ArcIt e(_graph); e != INVALID; ++e)
466
        c += (*_flow)[e] * _length[e];
467
      return c;
500
    const PotentialMap& potentialMap() const {
501
      return *_potential;
468 502
    }
469 503

	
470 504
    /// \brief Return the number of the found paths.
471 505
    ///
472 506
    /// This function returns the number of the found paths.
473 507
    ///
474 508
    /// \pre \ref run() or \ref findFlow() must be called before using
475 509
    /// this function.
476 510
    int pathNum() const {
477 511
      return _path_num;
478 512
    }
479 513

	
480 514
    /// \brief Return a const reference to the specified path.
481 515
    ///
482 516
    /// This function returns a const reference to the specified path.
483 517
    ///
484
    /// \param i The function returns the \c i-th path.
518
    /// \param i The function returns the <tt>i</tt>-th path.
485 519
    /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
486 520
    ///
487 521
    /// \pre \ref run() or \ref findPaths() must be called before using
488 522
    /// this function.
489 523
    Path path(int i) const {
490 524
      return paths[i];
491 525
    }
492 526

	
493 527
    /// @}
494 528

	
495 529
  }; //class Suurballe
496 530

	
497 531
  ///@}
498 532

	
499 533
} //namespace lemon
500 534

	
501 535
#endif //LEMON_SUURBALLE_H
Ignore white space 6 line context
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-2009
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_TIME_MEASURE_H
20 20
#define LEMON_TIME_MEASURE_H
21 21

	
22 22
///\ingroup timecount
23 23
///\file
24 24
///\brief Tools for measuring cpu usage
25 25

	
26 26
#ifdef WIN32
27
#define WIN32_LEAN_AND_MEAN
28
#define NOMINMAX
29
#include <windows.h>
30
#include <cmath>
27
#include <lemon/bits/windows.h>
31 28
#else
29
#include <unistd.h>
32 30
#include <sys/times.h>
33 31
#include <sys/time.h>
34 32
#endif
35 33

	
36 34
#include <string>
37 35
#include <fstream>
38 36
#include <iostream>
39 37

	
40 38
namespace lemon {
41 39

	
42 40
  /// \addtogroup timecount
43 41
  /// @{
44 42

	
45 43
  /// A class to store (cpu)time instances.
46 44

	
47 45
  /// This class stores five time values.
48 46
  /// - a real time
49 47
  /// - a user cpu time
50 48
  /// - a system cpu time
51 49
  /// - a user cpu time of children
52 50
  /// - a system cpu time of children
53 51
  ///
54 52
  /// TimeStamp's can be added to or substracted from each other and
55 53
  /// they can be pushed to a stream.
56 54
  ///
57 55
  /// In most cases, perhaps the \ref Timer or the \ref TimeReport
58 56
  /// class is what you want to use instead.
59 57

	
60 58
  class TimeStamp
61 59
  {
62 60
    double utime;
63 61
    double stime;
64 62
    double cutime;
65 63
    double cstime;
66 64
    double rtime;
67 65

	
68 66
    void _reset() {
69 67
      utime = stime = cutime = cstime = rtime = 0;
70 68
    }
71 69

	
72 70
  public:
73 71

	
74 72
    ///Read the current time values of the process
75 73
    void stamp()
76 74
    {
77 75
#ifndef WIN32
78 76
      timeval tv;
79 77
      gettimeofday(&tv, 0);
80 78
      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
81 79

	
82 80
      tms ts;
83 81
      double tck=sysconf(_SC_CLK_TCK);
84 82
      times(&ts);
85 83
      utime=ts.tms_utime/tck;
86 84
      stime=ts.tms_stime/tck;
87 85
      cutime=ts.tms_cutime/tck;
88 86
      cstime=ts.tms_cstime/tck;
89 87
#else
90
      static const double ch = 4294967296.0e-7;
91
      static const double cl = 1.0e-7;
92

	
93
      FILETIME system;
94
      GetSystemTimeAsFileTime(&system);
95
      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
96

	
97
      FILETIME create, exit, kernel, user;
98
      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
99
        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
100
        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
101
        cutime = 0;
102
        cstime = 0;
103
      } else {
104
        rtime = 0;
105
        utime = 0;
106
        stime = 0;
107
        cutime = 0;
108
        cstime = 0;
109
      }
88
      bits::getWinProcTimes(rtime, utime, stime, cutime, cstime);
110 89
#endif
111 90
    }
112 91

	
113 92
    /// Constructor initializing with zero
114 93
    TimeStamp()
115 94
    { _reset(); }
116 95
    ///Constructor initializing with the current time values of the process
117 96
    TimeStamp(void *) { stamp();}
118 97

	
119 98
    ///Set every time value to zero
120 99
    TimeStamp &reset() {_reset();return *this;}
121 100

	
122 101
    ///\e
123 102
    TimeStamp &operator+=(const TimeStamp &b)
124 103
    {
125 104
      utime+=b.utime;
126 105
      stime+=b.stime;
127 106
      cutime+=b.cutime;
128 107
      cstime+=b.cstime;
129 108
      rtime+=b.rtime;
130 109
      return *this;
131 110
    }
132 111
    ///\e
133 112
    TimeStamp operator+(const TimeStamp &b) const
134 113
    {
135 114
      TimeStamp t(*this);
136 115
      return t+=b;
137 116
    }
138 117
    ///\e
139 118
    TimeStamp &operator-=(const TimeStamp &b)
140 119
    {
141 120
      utime-=b.utime;
142 121
      stime-=b.stime;
143 122
      cutime-=b.cutime;
144 123
      cstime-=b.cstime;
145 124
      rtime-=b.rtime;
146 125
      return *this;
147 126
    }
148 127
    ///\e
149 128
    TimeStamp operator-(const TimeStamp &b) const
150 129
    {
151 130
      TimeStamp t(*this);
152 131
      return t-=b;
153 132
    }
154 133
    ///\e
155 134
    TimeStamp &operator*=(double b)
156 135
    {
157 136
      utime*=b;
158 137
      stime*=b;
159 138
      cutime*=b;
160 139
      cstime*=b;
161 140
      rtime*=b;
162 141
      return *this;
163 142
    }
164 143
    ///\e
165 144
    TimeStamp operator*(double b) const
166 145
    {
167 146
      TimeStamp t(*this);
168 147
      return t*=b;
169 148
    }
170 149
    friend TimeStamp operator*(double b,const TimeStamp &t);
171 150
    ///\e
172 151
    TimeStamp &operator/=(double b)
173 152
    {
174 153
      utime/=b;
175 154
      stime/=b;
176 155
      cutime/=b;
177 156
      cstime/=b;
178 157
      rtime/=b;
179 158
      return *this;
180 159
    }
181 160
    ///\e
182 161
    TimeStamp operator/(double b) const
183 162
    {
184 163
      TimeStamp t(*this);
185 164
      return t/=b;
186 165
    }
187 166
    ///The time ellapsed since the last call of stamp()
188 167
    TimeStamp ellapsed() const
189 168
    {
190 169
      TimeStamp t(NULL);
191 170
      return t-*this;
192 171
    }
193 172

	
194 173
    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
195 174

	
196 175
    ///Gives back the user time of the process
197 176
    double userTime() const
198 177
    {
199 178
      return utime;
200 179
    }
201 180
    ///Gives back the system time of the process
202 181
    double systemTime() const
203 182
    {
204 183
      return stime;
205 184
    }
206 185
    ///Gives back the user time of the process' children
207 186

	
208 187
    ///\note On <tt>WIN32</tt> platform this value is not calculated.
209 188
    ///
210 189
    double cUserTime() const
211 190
    {
212 191
      return cutime;
213 192
    }
214 193
    ///Gives back the user time of the process' children
215 194

	
216 195
    ///\note On <tt>WIN32</tt> platform this value is not calculated.
217 196
    ///
218 197
    double cSystemTime() const
219 198
    {
220 199
      return cstime;
221 200
    }
222 201
    ///Gives back the real time
223 202
    double realTime() const {return rtime;}
224 203
  };
225 204

	
226
  TimeStamp operator*(double b,const TimeStamp &t)
205
  inline TimeStamp operator*(double b,const TimeStamp &t)
227 206
  {
228 207
    return t*b;
229 208
  }
230 209

	
231 210
  ///Prints the time counters
232 211

	
233 212
  ///Prints the time counters in the following form:
234 213
  ///
235 214
  /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
236 215
  ///
237 216
  /// where the values are the
238 217
  /// \li \c u: user cpu time,
239 218
  /// \li \c s: system cpu time,
240 219
  /// \li \c cu: user cpu time of children,
241 220
  /// \li \c cs: system cpu time of children,
242 221
  /// \li \c real: real time.
243 222
  /// \relates TimeStamp
244 223
  /// \note On <tt>WIN32</tt> platform the cummulative values are not
245 224
  /// calculated.
246 225
  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
247 226
  {
248 227
    os << "u: " << t.userTime() <<
249 228
      "s, s: " << t.systemTime() <<
250 229
      "s, cu: " << t.cUserTime() <<
251 230
      "s, cs: " << t.cSystemTime() <<
252 231
      "s, real: " << t.realTime() << "s";
253 232
    return os;
254 233
  }
255 234

	
256 235
  ///Class for measuring the cpu time and real time usage of the process
257 236

	
258 237
  ///Class for measuring the cpu time and real time usage of the process.
259 238
  ///It is quite easy-to-use, here is a short example.
260 239
  ///\code
261 240
  /// #include<lemon/time_measure.h>
262 241
  /// #include<iostream>
263 242
  ///
264 243
  /// int main()
265 244
  /// {
266 245
  ///
267 246
  ///   ...
268 247
  ///
269 248
  ///   Timer t;
270 249
  ///   doSomething();
271 250
  ///   std::cout << t << '\n';
272 251
  ///   t.restart();
273 252
  ///   doSomethingElse();
274 253
  ///   std::cout << t << '\n';
275 254
  ///
276 255
  ///   ...
277 256
  ///
278 257
  /// }
279 258
  ///\endcode
280 259
  ///
281 260
  ///The \ref Timer can also be \ref stop() "stopped" and
282 261
  ///\ref start() "started" again, so it is possible to compute collected
283 262
  ///running times.
284 263
  ///
285 264
  ///\warning Depending on the operation system and its actual configuration
286 265
  ///the time counters have a certain (10ms on a typical Linux system)
287 266
  ///granularity.
288 267
  ///Therefore this tool is not appropriate to measure very short times.
289 268
  ///Also, if you start and stop the timer very frequently, it could lead to
290 269
  ///distorted results.
291 270
  ///
292 271
  ///\note If you want to measure the running time of the execution of a certain
293 272
  ///function, consider the usage of \ref TimeReport instead.
294 273
  ///
295 274
  ///\sa TimeReport
296 275
  class Timer
297 276
  {
298 277
    int _running; //Timer is running iff _running>0; (_running>=0 always holds)
299 278
    TimeStamp start_time; //This is the relativ start-time if the timer
300 279
                          //is _running, the collected _running time otherwise.
301 280

	
302 281
    void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
303 282

	
304 283
  public:
305 284
    ///Constructor.
306 285

	
307 286
    ///\param run indicates whether or not the timer starts immediately.
308 287
    ///
309 288
    Timer(bool run=true) :_running(run) {_reset();}
310 289

	
311
    ///\name Control the state of the timer
290
    ///\name Control the State of the Timer
312 291
    ///Basically a Timer can be either running or stopped,
313 292
    ///but it provides a bit finer control on the execution.
314 293
    ///The \ref lemon::Timer "Timer" also counts the number of
315 294
    ///\ref lemon::Timer::start() "start()" executions, and it stops
316 295
    ///only after the same amount (or more) \ref lemon::Timer::stop()
317 296
    ///"stop()"s. This can be useful e.g. to compute the running time
318 297
    ///of recursive functions.
319 298

	
320 299
    ///@{
321 300

	
322 301
    ///Reset and stop the time counters
323 302

	
324 303
    ///This function resets and stops the time counters
325 304
    ///\sa restart()
326 305
    void reset()
327 306
    {
328 307
      _running=0;
329 308
      _reset();
330 309
    }
331 310

	
332 311
    ///Start the time counters
333 312

	
334 313
    ///This function starts the time counters.
335 314
    ///
336 315
    ///If the timer is started more than ones, it will remain running
337 316
    ///until the same amount of \ref stop() is called.
338 317
    ///\sa stop()
339 318
    void start()
340 319
    {
341 320
      if(_running) _running++;
342 321
      else {
343 322
        _running=1;
344 323
        TimeStamp t;
345 324
        t.stamp();
346 325
        start_time=t-start_time;
347 326
      }
348 327
    }
349 328

	
350 329

	
351 330
    ///Stop the time counters
352 331

	
353 332
    ///This function stops the time counters. If start() was executed more than
354 333
    ///once, then the same number of stop() execution is necessary the really
355 334
    ///stop the timer.
356 335
    ///
357 336
    ///\sa halt()
358 337
    ///\sa start()
359 338
    ///\sa restart()
360 339
    ///\sa reset()
361 340

	
362 341
    void stop()
363 342
    {
364 343
      if(_running && !--_running) {
365 344
        TimeStamp t;
366 345
        t.stamp();
367 346
        start_time=t-start_time;
368 347
      }
369 348
    }
370 349

	
371 350
    ///Halt (i.e stop immediately) the time counters
372 351

	
373 352
    ///This function stops immediately the time counters, i.e. <tt>t.halt()</tt>
374 353
    ///is a faster
375 354
    ///equivalent of the following.
376 355
    ///\code
377 356
    ///  while(t.running()) t.stop()
378 357
    ///\endcode
379 358
    ///
380 359
    ///
381 360
    ///\sa stop()
382 361
    ///\sa restart()
383 362
    ///\sa reset()
384 363

	
385 364
    void halt()
386 365
    {
387 366
      if(_running) {
388 367
        _running=0;
389 368
        TimeStamp t;
390 369
        t.stamp();
391 370
        start_time=t-start_time;
392 371
      }
393 372
    }
394 373

	
395 374
    ///Returns the running state of the timer
396 375

	
397 376
    ///This function returns the number of stop() exections that is
398 377
    ///necessary to really stop the timer.
399 378
    ///For example the timer
400 379
    ///is running if and only if the return value is \c true
401 380
    ///(i.e. greater than
402 381
    ///zero).
403 382
    int running()  { return _running; }
404 383

	
405 384

	
406 385
    ///Restart the time counters
407 386

	
408 387
    ///This function is a shorthand for
409 388
    ///a reset() and a start() calls.
410 389
    ///
411 390
    void restart()
412 391
    {
413 392
      reset();
414 393
      start();
415 394
    }
416 395

	
417 396
    ///@}
418 397

	
419
    ///\name Query Functions for the ellapsed time
398
    ///\name Query Functions for the Ellapsed Time
420 399

	
421 400
    ///@{
422 401

	
423 402
    ///Gives back the ellapsed user time of the process
424 403
    double userTime() const
425 404
    {
426 405
      return operator TimeStamp().userTime();
427 406
    }
428 407
    ///Gives back the ellapsed system time of the process
429 408
    double systemTime() const
430 409
    {
431 410
      return operator TimeStamp().systemTime();
432 411
    }
433 412
    ///Gives back the ellapsed user time of the process' children
434 413

	
435 414
    ///\note On <tt>WIN32</tt> platform this value is not calculated.
436 415
    ///
437 416
    double cUserTime() const
438 417
    {
439 418
      return operator TimeStamp().cUserTime();
440 419
    }
441 420
    ///Gives back the ellapsed user time of the process' children
442 421

	
443 422
    ///\note On <tt>WIN32</tt> platform this value is not calculated.
444 423
    ///
445 424
    double cSystemTime() const
446 425
    {
447 426
      return operator TimeStamp().cSystemTime();
448 427
    }
449 428
    ///Gives back the ellapsed real time
450 429
    double realTime() const
451 430
    {
452 431
      return operator TimeStamp().realTime();
453 432
    }
454 433
    ///Computes the ellapsed time
455 434

	
456 435
    ///This conversion computes the ellapsed time, therefore you can print
457 436
    ///the ellapsed time like this.
458 437
    ///\code
459 438
    ///  Timer t;
460 439
    ///  doSomething();
461 440
    ///  std::cout << t << '\n';
462 441
    ///\endcode
463 442
    operator TimeStamp () const
464 443
    {
465 444
      TimeStamp t;
466 445
      t.stamp();
467 446
      return _running?t-start_time:start_time;
468 447
    }
469 448

	
470 449

	
471 450
    ///@}
472 451
  };
473 452

	
474 453
  ///Same as Timer but prints a report on destruction.
475 454

	
476 455
  ///Same as \ref Timer but prints a report on destruction.
477 456
  ///This example shows its usage.
478 457
  ///\code
479 458
  ///  void myAlg(ListGraph &g,int n)
480 459
  ///  {
481 460
  ///    TimeReport tr("Running time of myAlg: ");
482 461
  ///    ... //Here comes the algorithm
483 462
  ///  }
484 463
  ///\endcode
485 464
  ///
486 465
  ///\sa Timer
487 466
  ///\sa NoTimeReport
488 467
  class TimeReport : public Timer
489 468
  {
490 469
    std::string _title;
491 470
    std::ostream &_os;
492 471
  public:
493 472
    ///Constructor
494 473

	
495 474
    ///Constructor.
496 475
    ///\param title This text will be printed before the ellapsed time.
497 476
    ///\param os The stream to print the report to.
498 477
    ///\param run Sets whether the timer should start immediately.
499 478
    TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true)
500 479
      : Timer(run), _title(title), _os(os){}
501 480
    ///Destructor that prints the ellapsed time
502 481
    ~TimeReport()
503 482
    {
504 483
      _os << _title << *this << std::endl;
505 484
    }
506 485
  };
507 486

	
508 487
  ///'Do nothing' version of TimeReport
509 488

	
510 489
  ///\sa TimeReport
511 490
  ///
512 491
  class NoTimeReport
513 492
  {
514 493
  public:
515 494
    ///\e
Ignore white space 6 line context
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-2009
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_TOLERANCE_H
20 20
#define LEMON_TOLERANCE_H
21 21

	
22 22
///\ingroup misc
23 23
///\file
24 24
///\brief A basic tool to handle the anomalies of calculation with
25 25
///floating point numbers.
26 26
///
27 27

	
28 28
namespace lemon {
29 29

	
30 30
  /// \addtogroup misc
31 31
  /// @{
32 32

	
33 33
  ///\brief A class to provide a basic way to
34 34
  ///handle the comparison of numbers that are obtained
35 35
  ///as a result of a probably inexact computation.
36 36
  ///
37 37
  ///\ref Tolerance is a class to provide a basic way to
38 38
  ///handle the comparison of numbers that are obtained
39 39
  ///as a result of a probably inexact computation.
40 40
  ///
41
  ///This is an abstract class, it should be specialized for all
42
  ///numerical data types. These specialized classes like
41
  ///The general implementation is suitable only if the data type is exact,
42
  ///like the integer types, otherwise a specialized version must be
43
  ///implemented. These specialized classes like
43 44
  ///Tolerance<double> may offer additional tuning parameters.
44 45
  ///
45 46
  ///\sa Tolerance<float>
46 47
  ///\sa Tolerance<double>
47 48
  ///\sa Tolerance<long double>
48
  ///\sa Tolerance<int>
49
  ///\sa Tolerance<long long int>
50
  ///\sa Tolerance<unsigned int>
51
  ///\sa Tolerance<unsigned long long int>
52 49

	
53 50
  template<class T>
54 51
  class Tolerance
55 52
  {
56 53
  public:
57 54
    typedef T Value;
58 55

	
59 56
    ///\name Comparisons
60 57
    ///The concept is that these bool functions return \c true only if
61 58
    ///the related comparisons hold even if some numerical error appeared
62 59
    ///during the computations.
63 60

	
64 61
    ///@{
65 62

	
66 63
    ///Returns \c true if \c a is \e surely strictly less than \c b
67
    static bool less(Value a,Value b) {return false;}
64
    static bool less(Value a,Value b) {return a<b;}
68 65
    ///Returns \c true if \c a is \e surely different from \c b
69
    static bool different(Value a,Value b) {return false;}
66
    static bool different(Value a,Value b) {return a!=b;}
70 67
    ///Returns \c true if \c a is \e surely positive
71
    static bool positive(Value a) {return false;}
68
    static bool positive(Value a) {return static_cast<Value>(0) < a;}
72 69
    ///Returns \c true if \c a is \e surely negative
73
    static bool negative(Value a) {return false;}
70
    static bool negative(Value a) {return a < static_cast<Value>(0);}
74 71
    ///Returns \c true if \c a is \e surely non-zero
75
    static bool nonZero(Value a) {return false;}
72
    static bool nonZero(Value a) {return a != static_cast<Value>(0);}
76 73

	
77 74
    ///@}
78 75

	
79 76
    ///Returns the zero value.
80
    static Value zero() {return T();}
77
    static Value zero() {return static_cast<Value>(0);}
81 78

	
82 79
    //   static bool finite(Value a) {}
83 80
    //   static Value big() {}
84 81
    //   static Value negativeBig() {}
85 82
  };
86 83

	
87 84

	
88 85
  ///Float specialization of Tolerance.
89 86

	
90 87
  ///Float specialization of Tolerance.
91 88
  ///\sa Tolerance
92 89
  ///\relates Tolerance
93 90
  template<>
94 91
  class Tolerance<float>
95 92
  {
96 93
    static float def_epsilon;
97 94
    float _epsilon;
98 95
  public:
99 96
    ///\e
100 97
    typedef float Value;
101 98

	
102 99
    ///Constructor setting the epsilon tolerance to the default value.
103 100
    Tolerance() : _epsilon(def_epsilon) {}
104 101
    ///Constructor setting the epsilon tolerance to the given value.
105 102
    Tolerance(float e) : _epsilon(e) {}
106 103

	
107 104
    ///Returns the epsilon value.
108 105
    Value epsilon() const {return _epsilon;}
109 106
    ///Sets the epsilon value.
110 107
    void epsilon(Value e) {_epsilon=e;}
111 108

	
112 109
    ///Returns the default epsilon value.
113 110
    static Value defaultEpsilon() {return def_epsilon;}
114 111
    ///Sets the default epsilon value.
115 112
    static void defaultEpsilon(Value e) {def_epsilon=e;}
116 113

	
117 114
    ///\name Comparisons
118 115
    ///See \ref lemon::Tolerance "Tolerance" for more details.
119 116

	
120 117
    ///@{
121 118

	
122 119
    ///Returns \c true if \c a is \e surely strictly less than \c b
123 120
    bool less(Value a,Value b) const {return a+_epsilon<b;}
124 121
    ///Returns \c true if \c a is \e surely different from \c b
125 122
    bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
126 123
    ///Returns \c true if \c a is \e surely positive
127 124
    bool positive(Value a) const { return _epsilon<a; }
128 125
    ///Returns \c true if \c a is \e surely negative
129 126
    bool negative(Value a) const { return -_epsilon>a; }
130 127
    ///Returns \c true if \c a is \e surely non-zero
131 128
    bool nonZero(Value a) const { return positive(a)||negative(a); }
132 129

	
133 130
    ///@}
134 131

	
135 132
    ///Returns zero
136 133
    static Value zero() {return 0;}
137 134
  };
138 135

	
139 136
  ///Double specialization of Tolerance.
140 137

	
141 138
  ///Double specialization of Tolerance.
142 139
  ///\sa Tolerance
143 140
  ///\relates Tolerance
144 141
  template<>
145 142
  class Tolerance<double>
146 143
  {
147 144
    static double def_epsilon;
148 145
    double _epsilon;
149 146
  public:
150 147
    ///\e
151 148
    typedef double Value;
152 149

	
153 150
    ///Constructor setting the epsilon tolerance to the default value.
154 151
    Tolerance() : _epsilon(def_epsilon) {}
155 152
    ///Constructor setting the epsilon tolerance to the given value.
156 153
    Tolerance(double e) : _epsilon(e) {}
157 154

	
158 155
    ///Returns the epsilon value.
159 156
    Value epsilon() const {return _epsilon;}
160 157
    ///Sets the epsilon value.
161 158
    void epsilon(Value e) {_epsilon=e;}
162 159

	
163 160
    ///Returns the default epsilon value.
164 161
    static Value defaultEpsilon() {return def_epsilon;}
165 162
    ///Sets the default epsilon value.
166 163
    static void defaultEpsilon(Value e) {def_epsilon=e;}
167 164

	
168 165
    ///\name Comparisons
169 166
    ///See \ref lemon::Tolerance "Tolerance" for more details.
170 167

	
171 168
    ///@{
172 169

	
173 170
    ///Returns \c true if \c a is \e surely strictly less than \c b
174 171
    bool less(Value a,Value b) const {return a+_epsilon<b;}
175 172
    ///Returns \c true if \c a is \e surely different from \c b
176 173
    bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
177 174
    ///Returns \c true if \c a is \e surely positive
178 175
    bool positive(Value a) const { return _epsilon<a; }
179 176
    ///Returns \c true if \c a is \e surely negative
180 177
    bool negative(Value a) const { return -_epsilon>a; }
181 178
    ///Returns \c true if \c a is \e surely non-zero
182 179
    bool nonZero(Value a) const { return positive(a)||negative(a); }
183 180

	
184 181
    ///@}
185 182

	
186 183
    ///Returns zero
187 184
    static Value zero() {return 0;}
188 185
  };
189 186

	
190 187
  ///Long double specialization of Tolerance.
191 188

	
192 189
  ///Long double specialization of Tolerance.
193 190
  ///\sa Tolerance
194 191
  ///\relates Tolerance
195 192
  template<>
196 193
  class Tolerance<long double>
197 194
  {
198 195
    static long double def_epsilon;
199 196
    long double _epsilon;
200 197
  public:
201 198
    ///\e
202 199
    typedef long double Value;
203 200

	
204 201
    ///Constructor setting the epsilon tolerance to the default value.
205 202
    Tolerance() : _epsilon(def_epsilon) {}
206 203
    ///Constructor setting the epsilon tolerance to the given value.
207 204
    Tolerance(long double e) : _epsilon(e) {}
208 205

	
209 206
    ///Returns the epsilon value.
210 207
    Value epsilon() const {return _epsilon;}
211 208
    ///Sets the epsilon value.
212 209
    void epsilon(Value e) {_epsilon=e;}
213 210

	
214 211
    ///Returns the default epsilon value.
215 212
    static Value defaultEpsilon() {return def_epsilon;}
216 213
    ///Sets the default epsilon value.
217 214
    static void defaultEpsilon(Value e) {def_epsilon=e;}
218 215

	
219 216
    ///\name Comparisons
220 217
    ///See \ref lemon::Tolerance "Tolerance" for more details.
221 218

	
222 219
    ///@{
223 220

	
224 221
    ///Returns \c true if \c a is \e surely strictly less than \c b
225 222
    bool less(Value a,Value b) const {return a+_epsilon<b;}
226 223
    ///Returns \c true if \c a is \e surely different from \c b
227 224
    bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
228 225
    ///Returns \c true if \c a is \e surely positive
229 226
    bool positive(Value a) const { return _epsilon<a; }
230 227
    ///Returns \c true if \c a is \e surely negative
231 228
    bool negative(Value a) const { return -_epsilon>a; }
232 229
    ///Returns \c true if \c a is \e surely non-zero
233 230
    bool nonZero(Value a) const { return positive(a)||negative(a); }
234 231

	
235 232
    ///@}
236 233

	
237 234
    ///Returns zero
238 235
    static Value zero() {return 0;}
239 236
  };
240 237

	
241
  ///Integer specialization of Tolerance.
242

	
243
  ///Integer specialization of Tolerance.
244
  ///\sa Tolerance
245
  template<>
246
  class Tolerance<int>
247
  {
248
  public:
249
    ///\e
250
    typedef int Value;
251

	
252
    ///\name Comparisons
253
    ///See \ref lemon::Tolerance "Tolerance" for more details.
254

	
255
    ///@{
256

	
257
    ///Returns \c true if \c a is \e surely strictly less than \c b
258
    static bool less(Value a,Value b) { return a<b;}
259
    ///Returns \c true if \c a is \e surely different from \c b
260
    static bool different(Value a,Value b) { return a!=b; }
261
    ///Returns \c true if \c a is \e surely positive
262
    static bool positive(Value a) { return 0<a; }
263
    ///Returns \c true if \c a is \e surely negative
264
    static bool negative(Value a) { return 0>a; }
265
    ///Returns \c true if \c a is \e surely non-zero
266
    static bool nonZero(Value a) { return a!=0; }
267

	
268
    ///@}
269

	
270
    ///Returns zero
271
    static Value zero() {return 0;}
272
  };
273

	
274
  ///Unsigned integer specialization of Tolerance.
275

	
276
  ///Unsigned integer specialization of Tolerance.
277
  ///\sa Tolerance
278
  template<>
279
  class Tolerance<unsigned int>
280
  {
281
  public:
282
    ///\e
283
    typedef unsigned int Value;
284

	
285
    ///\name Comparisons
286
    ///See \ref lemon::Tolerance "Tolerance" for more details.
287

	
288
    ///@{
289

	
290
    ///Returns \c true if \c a is \e surely strictly less than \c b
291
    static bool less(Value a,Value b) { return a<b;}
292
    ///Returns \c true if \c a is \e surely different from \c b
293
    static bool different(Value a,Value b) { return a!=b; }
294
    ///Returns \c true if \c a is \e surely positive
295
    static bool positive(Value a) { return 0<a; }
296
    ///Returns \c true if \c a is \e surely negative
297
    static bool negative(Value) { return false; }
298
    ///Returns \c true if \c a is \e surely non-zero
299
    static bool nonZero(Value a) { return a!=0; }
300

	
301
    ///@}
302

	
303
    ///Returns zero
304
    static Value zero() {return 0;}
305
  };
306

	
307

	
308
  ///Long integer specialization of Tolerance.
309

	
310
  ///Long integer specialization of Tolerance.
311
  ///\sa Tolerance
312
  template<>
313
  class Tolerance<long int>
314
  {
315
  public:
316
    ///\e
317
    typedef long int Value;
318

	
319
    ///\name Comparisons
320
    ///See \ref lemon::Tolerance "Tolerance" for more details.
321

	
322
    ///@{
323

	
324
    ///Returns \c true if \c a is \e surely strictly less than \c b
325
    static bool less(Value a,Value b) { return a<b;}
326
    ///Returns \c true if \c a is \e surely different from \c b
327
    static bool different(Value a,Value b) { return a!=b; }
328
    ///Returns \c true if \c a is \e surely positive
329
    static bool positive(Value a) { return 0<a; }
330
    ///Returns \c true if \c a is \e surely negative
331
    static bool negative(Value a) { return 0>a; }
332
    ///Returns \c true if \c a is \e surely non-zero
333
    static bool nonZero(Value a) { return a!=0;}
334

	
335
    ///@}
336

	
337
    ///Returns zero
338
    static Value zero() {return 0;}
339
  };
340

	
341
  ///Unsigned long integer specialization of Tolerance.
342

	
343
  ///Unsigned long integer specialization of Tolerance.
344
  ///\sa Tolerance
345
  template<>
346
  class Tolerance<unsigned long int>
347
  {
348
  public:
349
    ///\e
350
    typedef unsigned long int Value;
351

	
352
    ///\name Comparisons
353
    ///See \ref lemon::Tolerance "Tolerance" for more details.
354

	
355
    ///@{
356

	
357
    ///Returns \c true if \c a is \e surely strictly less than \c b
358
    static bool less(Value a,Value b) { return a<b;}
359
    ///Returns \c true if \c a is \e surely different from \c b
360
    static bool different(Value a,Value b) { return a!=b; }
361
    ///Returns \c true if \c a is \e surely positive
362
    static bool positive(Value a) { return 0<a; }
363
    ///Returns \c true if \c a is \e surely negative
364
    static bool negative(Value) { return false; }
365
    ///Returns \c true if \c a is \e surely non-zero
366
    static bool nonZero(Value a) { return a!=0;}
367

	
368
    ///@}
369

	
370
    ///Returns zero
371
    static Value zero() {return 0;}
372
  };
373

	
374
#if defined __GNUC__ && !defined __STRICT_ANSI__
375

	
376
  ///Long long integer specialization of Tolerance.
377

	
378
  ///Long long integer specialization of Tolerance.
379
  ///\warning This class (more exactly, type <tt>long long</tt>)
380
  ///is not ansi compatible.
381
  ///\sa Tolerance
382
  template<>
383
  class Tolerance<long long int>
384
  {
385
  public:
386
    ///\e
387
    typedef long long int Value;
388

	
389
    ///\name Comparisons
390
    ///See \ref lemon::Tolerance "Tolerance" for more details.
391

	
392
    ///@{
393

	
394
    ///Returns \c true if \c a is \e surely strictly less than \c b
395
    static bool less(Value a,Value b) { return a<b;}
396
    ///Returns \c true if \c a is \e surely different from \c b
397
    static bool different(Value a,Value b) { return a!=b; }
398
    ///Returns \c true if \c a is \e surely positive
399
    static bool positive(Value a) { return 0<a; }
400
    ///Returns \c true if \c a is \e surely negative
401
    static bool negative(Value a) { return 0>a; }
402
    ///Returns \c true if \c a is \e surely non-zero
403
    static bool nonZero(Value a) { return a!=0;}
404

	
405
    ///@}
406

	
407
    ///Returns zero
408
    static Value zero() {return 0;}
409
  };
410

	
411
  ///Unsigned long long integer specialization of Tolerance.
412

	
413
  ///Unsigned long long integer specialization of Tolerance.
414
  ///\warning This class (more exactly, type <tt>unsigned long long</tt>)
415
  ///is not ansi compatible.
416
  ///\sa Tolerance
417
  template<>
418
  class Tolerance<unsigned long long int>
419
  {
420
  public:
421
    ///\e
422
    typedef unsigned long long int Value;
423

	
424
    ///\name Comparisons
425
    ///See \ref lemon::Tolerance "Tolerance" for more details.
426

	
427
    ///@{
428

	
429
    ///Returns \c true if \c a is \e surely strictly less than \c b
430
    static bool less(Value a,Value b) { return a<b;}
431
    ///Returns \c true if \c a is \e surely different from \c b
432
    static bool different(Value a,Value b) { return a!=b; }
433
    ///Returns \c true if \c a is \e surely positive
434
    static bool positive(Value a) { return 0<a; }
435
    ///Returns \c true if \c a is \e surely negative
436
    static bool negative(Value) { return false; }
437
    ///Returns \c true if \c a is \e surely non-zero
438
    static bool nonZero(Value a) { return a!=0;}
439

	
440
    ///@}
441

	
442
    ///Returns zero
443
    static Value zero() {return 0;}
444
  };
445

	
446
#endif
447

	
448 238
  /// @}
449 239

	
450 240
} //namespace lemon
451 241

	
452 242
#endif //LEMON_TOLERANCE_H
Ignore white space 6 line context
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-2009
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_UNION_FIND_H
20 20
#define LEMON_UNION_FIND_H
21 21

	
22 22
//!\ingroup auxdat
23 23
//!\file
24 24
//!\brief Union-Find data structures.
25 25
//!
26 26

	
27 27
#include <vector>
28 28
#include <list>
29 29
#include <utility>
30 30
#include <algorithm>
31 31
#include <functional>
32 32

	
33 33
#include <lemon/core.h>
34 34

	
35 35
namespace lemon {
36 36

	
37 37
  /// \ingroup auxdat
38 38
  ///
39 39
  /// \brief A \e Union-Find data structure implementation
40 40
  ///
41 41
  /// The class implements the \e Union-Find data structure.
42 42
  /// The union operation uses rank heuristic, while
43 43
  /// the find operation uses path compression.
44 44
  /// This is a very simple but efficient implementation, providing
45 45
  /// only four methods: join (union), find, insert and size.
46 46
  /// For more features see the \ref UnionFindEnum class.
47 47
  ///
48 48
  /// It is primarily used in Kruskal algorithm for finding minimal
49 49
  /// cost spanning tree in a graph.
50 50
  /// \sa kruskal()
51 51
  ///
52 52
  /// \pre You need to add all the elements by the \ref insert()
53 53
  /// method.
54
  template <typename _ItemIntMap>
54
  template <typename IM>
55 55
  class UnionFind {
56 56
  public:
57 57

	
58
    typedef _ItemIntMap ItemIntMap;
58
    ///\e
59
    typedef IM ItemIntMap;
60
    ///\e
59 61
    typedef typename ItemIntMap::Key Item;
60 62

	
61 63
  private:
62 64
    // If the items vector stores negative value for an item then
63 65
    // that item is root item and it has -items[it] component size.
64 66
    // Else the items[it] contains the index of the parent.
65 67
    std::vector<int> items;
66 68
    ItemIntMap& index;
67 69

	
68 70
    bool rep(int idx) const {
69 71
      return items[idx] < 0;
70 72
    }
71 73

	
72 74
    int repIndex(int idx) const {
73 75
      int k = idx;
74 76
      while (!rep(k)) {
75 77
        k = items[k] ;
76 78
      }
77 79
      while (idx != k) {
78 80
        int next = items[idx];
79 81
        const_cast<int&>(items[idx]) = k;
80 82
        idx = next;
81 83
      }
82 84
      return k;
83 85
    }
84 86

	
85 87
  public:
86 88

	
87 89
    /// \brief Constructor
88 90
    ///
89 91
    /// Constructor of the UnionFind class. You should give an item to
90 92
    /// integer map which will be used from the data structure. If you
91 93
    /// modify directly this map that may cause segmentation fault,
92 94
    /// invalid data structure, or infinite loop when you use again
93 95
    /// the union-find.
94 96
    UnionFind(ItemIntMap& m) : index(m) {}
95 97

	
96 98
    /// \brief Returns the index of the element's component.
97 99
    ///
98 100
    /// The method returns the index of the element's component.
99 101
    /// This is an integer between zero and the number of inserted elements.
100 102
    ///
101 103
    int find(const Item& a) {
102 104
      return repIndex(index[a]);
103 105
    }
104 106

	
105 107
    /// \brief Clears the union-find data structure
106 108
    ///
107 109
    /// Erase each item from the data structure.
108 110
    void clear() {
109 111
      items.clear();
110 112
    }
111 113

	
112 114
    /// \brief Inserts a new element into the structure.
113 115
    ///
114 116
    /// This method inserts a new element into the data structure.
115 117
    ///
116 118
    /// The method returns the index of the new component.
117 119
    int insert(const Item& a) {
118 120
      int n = items.size();
119 121
      items.push_back(-1);
120 122
      index.set(a,n);
121 123
      return n;
122 124
    }
123 125

	
124 126
    /// \brief Joining the components of element \e a and element \e b.
125 127
    ///
126 128
    /// This is the \e union operation of the Union-Find structure.
127 129
    /// Joins the component of element \e a and component of
128 130
    /// element \e b. If \e a and \e b are in the same component then
129 131
    /// it returns false otherwise it returns true.
130 132
    bool join(const Item& a, const Item& b) {
131 133
      int ka = repIndex(index[a]);
132 134
      int kb = repIndex(index[b]);
133 135

	
134 136
      if ( ka == kb )
135 137
        return false;
136 138

	
137 139
      if (items[ka] < items[kb]) {
138 140
        items[ka] += items[kb];
139 141
        items[kb] = ka;
140 142
      } else {
141 143
        items[kb] += items[ka];
142 144
        items[ka] = kb;
143 145
      }
144 146
      return true;
145 147
    }
146 148

	
147 149
    /// \brief Returns the size of the component of element \e a.
148 150
    ///
149 151
    /// Returns the size of the component of element \e a.
150 152
    int size(const Item& a) {
151 153
      int k = repIndex(index[a]);
152 154
      return - items[k];
153 155
    }
154 156

	
155 157
  };
156 158

	
157 159
  /// \ingroup auxdat
158 160
  ///
159 161
  /// \brief A \e Union-Find data structure implementation which
160 162
  /// is able to enumerate the components.
161 163
  ///
162 164
  /// The class implements a \e Union-Find data structure
163 165
  /// which is able to enumerate the components and the items in
164 166
  /// a component. If you don't need this feature then perhaps it's
165 167
  /// better to use the \ref UnionFind class which is more efficient.
166 168
  ///
167 169
  /// The union operation uses rank heuristic, while
168 170
  /// the find operation uses path compression.
169 171
  ///
170 172
  /// \pre You need to add all the elements by the \ref insert()
171 173
  /// method.
172 174
  ///
173
  template <typename _ItemIntMap>
175
  template <typename IM>
174 176
  class UnionFindEnum {
175 177
  public:
176 178

	
177
    typedef _ItemIntMap ItemIntMap;
179
    ///\e
180
    typedef IM ItemIntMap;
181
    ///\e
178 182
    typedef typename ItemIntMap::Key Item;
179 183

	
180 184
  private:
181 185

	
182 186
    ItemIntMap& index;
183 187

	
184 188
    // If the parent stores negative value for an item then that item
185 189
    // is root item and it has ~(items[it].parent) component id.  Else
186 190
    // the items[it].parent contains the index of the parent.
187 191
    //
188 192
    // The \c next and \c prev provides the double-linked
189 193
    // cyclic list of one component's items.
190 194
    struct ItemT {
191 195
      int parent;
192 196
      Item item;
193 197

	
194 198
      int next, prev;
195 199
    };
196 200

	
197 201
    std::vector<ItemT> items;
198 202
    int firstFreeItem;
199 203

	
200 204
    struct ClassT {
201 205
      int size;
202 206
      int firstItem;
203 207
      int next, prev;
204 208
    };
205 209

	
206 210
    std::vector<ClassT> classes;
207 211
    int firstClass, firstFreeClass;
208 212

	
209 213
    int newClass() {
210 214
      if (firstFreeClass == -1) {
211 215
        int cdx = classes.size();
212 216
        classes.push_back(ClassT());
213 217
        return cdx;
214 218
      } else {
215 219
        int cdx = firstFreeClass;
216 220
        firstFreeClass = classes[firstFreeClass].next;
217 221
        return cdx;
218 222
      }
219 223
    }
220 224

	
221 225
    int newItem() {
222 226
      if (firstFreeItem == -1) {
223 227
        int idx = items.size();
224 228
        items.push_back(ItemT());
225 229
        return idx;
226 230
      } else {
227 231
        int idx = firstFreeItem;
228 232
        firstFreeItem = items[firstFreeItem].next;
229 233
        return idx;
230 234
      }
231 235
    }
232 236

	
233 237

	
234 238
    bool rep(int idx) const {
235 239
      return items[idx].parent < 0;
236 240
    }
237 241

	
238 242
    int repIndex(int idx) const {
239 243
      int k = idx;
240 244
      while (!rep(k)) {
241 245
        k = items[k].parent;
242 246
      }
243 247
      while (idx != k) {
244 248
        int next = items[idx].parent;
245 249
        const_cast<int&>(items[idx].parent) = k;
246 250
        idx = next;
247 251
      }
248 252
      return k;
249 253
    }
250 254

	
251 255
    int classIndex(int idx) const {
252 256
      return ~(items[repIndex(idx)].parent);
253 257
    }
254 258

	
255 259
    void singletonItem(int idx) {
256 260
      items[idx].next = idx;
257 261
      items[idx].prev = idx;
258 262
    }
259 263

	
260 264
    void laceItem(int idx, int rdx) {
261 265
      items[idx].prev = rdx;
262 266
      items[idx].next = items[rdx].next;
263 267
      items[items[rdx].next].prev = idx;
264 268
      items[rdx].next = idx;
265 269
    }
266 270

	
267 271
    void unlaceItem(int idx) {
268 272
      items[items[idx].prev].next = items[idx].next;
269 273
      items[items[idx].next].prev = items[idx].prev;
270 274

	
271 275
      items[idx].next = firstFreeItem;
272 276
      firstFreeItem = idx;
273 277
    }
... ...
@@ -534,197 +538,199 @@
534 538
      ///
535 539
      /// Equality operator
536 540
      bool operator==(const ClassIt& i) {
537 541
        return i.cdx == cdx;
538 542
      }
539 543

	
540 544
      /// \brief Inequality operator
541 545
      ///
542 546
      /// Inequality operator
543 547
      bool operator!=(const ClassIt& i) {
544 548
        return i.cdx != cdx;
545 549
      }
546 550

	
547 551
    private:
548 552
      const UnionFindEnum* unionFind;
549 553
      int cdx;
550 554
    };
551 555

	
552 556
    /// \brief LEMON style iterator for the items of a component.
553 557
    ///
554 558
    /// ClassIt is a lemon style iterator for the components. It iterates
555 559
    /// on the items of a class. By example if you want to iterate on
556 560
    /// each items of each classes then you may write the next code.
557 561
    ///\code
558 562
    /// for (ClassIt cit(ufe); cit != INVALID; ++cit) {
559 563
    ///   std::cout << "Class: ";
560 564
    ///   for (ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
561 565
    ///     std::cout << toString(iit) << ' ' << std::endl;
562 566
    ///   }
563 567
    ///   std::cout << std::endl;
564 568
    /// }
565 569
    ///\endcode
566 570
    class ItemIt {
567 571
    public:
568 572
      /// \brief Constructor of the iterator
569 573
      ///
570 574
      /// Constructor of the iterator. The iterator iterates
571 575
      /// on the class of the \c item.
572 576
      ItemIt(const UnionFindEnum& ufe, int cls) : unionFind(&ufe) {
573 577
        fdx = idx = unionFind->classes[cls].firstItem;
574 578
      }
575 579

	
576 580
      /// \brief Constructor to get invalid iterator
577 581
      ///
578 582
      /// Constructor to get invalid iterator
579 583
      ItemIt(Invalid) : unionFind(0), idx(-1) {}
580 584

	
581 585
      /// \brief Increment operator
582 586
      ///
583 587
      /// It steps to the next item in the class.
584 588
      ItemIt& operator++() {
585 589
        idx = unionFind->items[idx].next;
586 590
        if (idx == fdx) idx = -1;
587 591
        return *this;
588 592
      }
589 593

	
590 594
      /// \brief Conversion operator
591 595
      ///
592 596
      /// It converts the iterator to the current item.
593 597
      operator const Item&() const {
594 598
        return unionFind->items[idx].item;
595 599
      }
596 600

	
597 601
      /// \brief Equality operator
598 602
      ///
599 603
      /// Equality operator
600 604
      bool operator==(const ItemIt& i) {
601 605
        return i.idx == idx;
602 606
      }
603 607

	
604 608
      /// \brief Inequality operator
605 609
      ///
606 610
      /// Inequality operator
607 611
      bool operator!=(const ItemIt& i) {
608 612
        return i.idx != idx;
609 613
      }
610 614

	
611 615
    private:
612 616
      const UnionFindEnum* unionFind;
613 617
      int idx, fdx;
614 618
    };
615 619

	
616 620
  };
617 621

	
618 622
  /// \ingroup auxdat
619 623
  ///
620 624
  /// \brief A \e Extend-Find data structure implementation which
621 625
  /// is able to enumerate the components.
622 626
  ///
623 627
  /// The class implements an \e Extend-Find data structure which is
624 628
  /// able to enumerate the components and the items in a
625 629
  /// component. The data structure is a simplification of the
626 630
  /// Union-Find structure, and it does not allow to merge two components.
627 631
  ///
628 632
  /// \pre You need to add all the elements by the \ref insert()
629 633
  /// method.
630
  template <typename _ItemIntMap>
634
  template <typename IM>
631 635
  class ExtendFindEnum {
632 636
  public:
633 637

	
634
    typedef _ItemIntMap ItemIntMap;
638
    ///\e
639
    typedef IM ItemIntMap;
640
    ///\e
635 641
    typedef typename ItemIntMap::Key Item;
636 642

	
637 643
  private:
638 644

	
639 645
    ItemIntMap& index;
640 646

	
641 647
    struct ItemT {
642 648
      int cls;
643 649
      Item item;
644 650
      int next, prev;
645 651
    };
646 652

	
647 653
    std::vector<ItemT> items;
648 654
    int firstFreeItem;
649 655

	
650 656
    struct ClassT {
651 657
      int firstItem;
652 658
      int next, prev;
653 659
    };
654 660

	
655 661
    std::vector<ClassT> classes;
656 662

	
657 663
    int firstClass, firstFreeClass;
658 664

	
659 665
    int newClass() {
660 666
      if (firstFreeClass != -1) {
661 667
        int cdx = firstFreeClass;
662 668
        firstFreeClass = classes[cdx].next;
663 669
        return cdx;
664 670
      } else {
665 671
        classes.push_back(ClassT());
666 672
        return classes.size() - 1;
667 673
      }
668 674
    }
669 675

	
670 676
    int newItem() {
671 677
      if (firstFreeItem != -1) {
672 678
        int idx = firstFreeItem;
673 679
        firstFreeItem = items[idx].next;
674 680
        return idx;
675 681
      } else {
676 682
        items.push_back(ItemT());
677 683
        return items.size() - 1;
678 684
      }
679 685
    }
680 686

	
681 687
  public:
682 688

	
683 689
    /// \brief Constructor
684 690
    ExtendFindEnum(ItemIntMap& _index)
685 691
      : index(_index), items(), firstFreeItem(-1),
686 692
        classes(), firstClass(-1), firstFreeClass(-1) {}
687 693

	
688 694
    /// \brief Inserts the given element into a new component.
689 695
    ///
690 696
    /// This method creates a new component consisting only of the
691 697
    /// given element.
692 698
    int insert(const Item& item) {
693 699
      int cdx = newClass();
694 700
      classes[cdx].prev = -1;
695 701
      classes[cdx].next = firstClass;
696 702
      if (firstClass != -1) {
697 703
        classes[firstClass].prev = cdx;
698 704
      }
699 705
      firstClass = cdx;
700 706

	
701 707
      int idx = newItem();
702 708
      items[idx].item = item;
703 709
      items[idx].cls = cdx;
704 710
      items[idx].prev = idx;
705 711
      items[idx].next = idx;
706 712

	
707 713
      classes[cdx].firstItem = idx;
708 714

	
709 715
      index.set(item, idx);
710 716

	
711 717
      return cdx;
712 718
    }
713 719

	
714 720
    /// \brief Inserts the given element into the given component.
715 721
    ///
716 722
    /// This methods inserts the element \e item a into the \e cls class.
717 723
    void insert(const Item& item, int cls) {
718 724
      int idx = newItem();
719 725
      int rdx = classes[cls].firstItem;
720 726
      items[idx].item = item;
721 727
      items[idx].cls = cls;
722 728

	
723 729
      items[idx].prev = rdx;
724 730
      items[idx].next = items[rdx].next;
725 731
      items[items[rdx].next].prev = idx;
726 732
      items[rdx].next = idx;
727 733

	
728 734
      index.set(item, idx);
729 735
    }
730 736

	
... ...
@@ -855,204 +861,204 @@
855 861
      }
856 862

	
857 863
    private:
858 864
      const ExtendFindEnum* extendFind;
859 865
      int cdx;
860 866
    };
861 867

	
862 868
    /// \brief LEMON style iterator for the items of a component.
863 869
    ///
864 870
    /// ClassIt is a lemon style iterator for the components. It iterates
865 871
    /// on the items of a class. By example if you want to iterate on
866 872
    /// each items of each classes then you may write the next code.
867 873
    ///\code
868 874
    /// for (ClassIt cit(ufe); cit != INVALID; ++cit) {
869 875
    ///   std::cout << "Class: ";
870 876
    ///   for (ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
871 877
    ///     std::cout << toString(iit) << ' ' << std::endl;
872 878
    ///   }
873 879
    ///   std::cout << std::endl;
874 880
    /// }
875 881
    ///\endcode
876 882
    class ItemIt {
877 883
    public:
878 884
      /// \brief Constructor of the iterator
879 885
      ///
880 886
      /// Constructor of the iterator. The iterator iterates
881 887
      /// on the class of the \c item.
882 888
      ItemIt(const ExtendFindEnum& ufe, int cls) : extendFind(&ufe) {
883 889
        fdx = idx = extendFind->classes[cls].firstItem;
884 890
      }
885 891

	
886 892
      /// \brief Constructor to get invalid iterator
887 893
      ///
888 894
      /// Constructor to get invalid iterator
889 895
      ItemIt(Invalid) : extendFind(0), idx(-1) {}
890 896

	
891 897
      /// \brief Increment operator
892 898
      ///
893 899
      /// It steps to the next item in the class.
894 900
      ItemIt& operator++() {
895 901
        idx = extendFind->items[idx].next;
896 902
        if (fdx == idx) idx = -1;
897 903
        return *this;
898 904
      }
899 905

	
900 906
      /// \brief Conversion operator
901 907
      ///
902 908
      /// It converts the iterator to the current item.
903 909
      operator const Item&() const {
904 910
        return extendFind->items[idx].item;
905 911
      }
906 912

	
907 913
      /// \brief Equality operator
908 914
      ///
909 915
      /// Equality operator
910 916
      bool operator==(const ItemIt& i) {
911 917
        return i.idx == idx;
912 918
      }
913 919

	
914 920
      /// \brief Inequality operator
915 921
      ///
916 922
      /// Inequality operator
917 923
      bool operator!=(const ItemIt& i) {
918 924
        return i.idx != idx;
919 925
      }
920 926

	
921 927
    private:
922 928
      const ExtendFindEnum* extendFind;
923 929
      int idx, fdx;
924 930
    };
925 931

	
926 932
  };
927 933

	
928 934
  /// \ingroup auxdat
929 935
  ///
930 936
  /// \brief A \e Union-Find data structure implementation which
931 937
  /// is able to store a priority for each item and retrieve the minimum of
932 938
  /// each class.
933 939
  ///
934 940
  /// A \e Union-Find data structure implementation which is able to
935 941
  /// store a priority for each item and retrieve the minimum of each
936 942
  /// class. In addition, it supports the joining and splitting the
937 943
  /// components. If you don't need this feature then you makes
938 944
  /// better to use the \ref UnionFind class which is more efficient.
939 945
  ///
940 946
  /// The union-find data strcuture based on a (2, 16)-tree with a
941 947
  /// tournament minimum selection on the internal nodes. The insert
942 948
  /// operation takes O(1), the find, set, decrease and increase takes
943 949
  /// O(log(n)), where n is the number of nodes in the current
944 950
  /// component.  The complexity of join and split is O(log(n)*k),
945 951
  /// where n is the sum of the number of the nodes and k is the
946 952
  /// number of joined components or the number of the components
947 953
  /// after the split.
948 954
  ///
949 955
  /// \pre You need to add all the elements by the \ref insert()
950 956
  /// method.
951
  ///
952
  template <typename _Value, typename _ItemIntMap,
953
            typename _Comp = std::less<_Value> >
957
  template <typename V, typename IM, typename Comp = std::less<V> >
954 958
  class HeapUnionFind {
955 959
  public:
956 960

	
957
    typedef _Value Value;
958
    typedef typename _ItemIntMap::Key Item;
959

	
960
    typedef _ItemIntMap ItemIntMap;
961

	
962
    typedef _Comp Comp;
961
    ///\e
962
    typedef V Value;
963
    ///\e
964
    typedef typename IM::Key Item;
965
    ///\e
966
    typedef IM ItemIntMap;
967
    ///\e
968
    typedef Comp Compare;
963 969

	
964 970
  private:
965 971

	
966 972
    static const int cmax = 16;
967 973

	
968 974
    ItemIntMap& index;
969 975

	
970 976
    struct ClassNode {
971 977
      int parent;
972 978
      int depth;
973 979

	
974 980
      int left, right;
975 981
      int next, prev;
976 982
    };
977 983

	
978 984
    int first_class;
979 985
    int first_free_class;
980 986
    std::vector<ClassNode> classes;
981 987

	
982 988
    int newClass() {
983 989
      if (first_free_class < 0) {
984 990
        int id = classes.size();
985 991
        classes.push_back(ClassNode());
986 992
        return id;
987 993
      } else {
988 994
        int id = first_free_class;
989 995
        first_free_class = classes[id].next;
990 996
        return id;
991 997
      }
992 998
    }
993 999

	
994 1000
    void deleteClass(int id) {
995 1001
      classes[id].next = first_free_class;
996 1002
      first_free_class = id;
997 1003
    }
998 1004

	
999 1005
    struct ItemNode {
1000 1006
      int parent;
1001 1007
      Item item;
1002 1008
      Value prio;
1003 1009
      int next, prev;
1004 1010
      int left, right;
1005 1011
      int size;
1006 1012
    };
1007 1013

	
1008 1014
    int first_free_node;
1009 1015
    std::vector<ItemNode> nodes;
1010 1016

	
1011 1017
    int newNode() {
1012 1018
      if (first_free_node < 0) {
1013 1019
        int id = nodes.size();
1014 1020
        nodes.push_back(ItemNode());
1015 1021
        return id;
1016 1022
      } else {
1017 1023
        int id = first_free_node;
1018 1024
        first_free_node = nodes[id].next;
1019 1025
        return id;
1020 1026
      }
1021 1027
    }
1022 1028

	
1023 1029
    void deleteNode(int id) {
1024 1030
      nodes[id].next = first_free_node;
1025 1031
      first_free_node = id;
1026 1032
    }
1027 1033

	
1028 1034
    Comp comp;
1029 1035

	
1030 1036
    int findClass(int id) const {
1031 1037
      int kd = id;
1032 1038
      while (kd >= 0) {
1033 1039
        kd = nodes[kd].parent;
1034 1040
      }
1035 1041
      return ~kd;
1036 1042
    }
1037 1043

	
1038 1044
    int leftNode(int id) const {
1039 1045
      int kd = ~(classes[id].parent);
1040 1046
      for (int i = 0; i < classes[id].depth; ++i) {
1041 1047
        kd = nodes[kd].left;
1042 1048
      }
1043 1049
      return kd;
1044 1050
    }
1045 1051

	
1046 1052
    int nextNode(int id) const {
1047 1053
      int depth = 0;
1048 1054
      while (id >= 0 && nodes[id].next == -1) {
1049 1055
        id = nodes[id].parent;
1050 1056
        ++depth;
1051 1057
      }
1052 1058
      if (id < 0) {
1053 1059
        return -1;
1054 1060
      }
1055 1061
      id = nodes[id].next;
1056 1062
      while (depth--) {
1057 1063
        id = nodes[id].left;
1058 1064
      }
... ...
@@ -1508,254 +1514,254 @@
1508 1514
          }
1509 1515
          if (classes[r].next != -1) {
1510 1516
            classes[classes[r].next].prev = classes[r].prev;
1511 1517
          }
1512 1518
          classes[classes[r].prev].next = classes[r].next;
1513 1519

	
1514 1520
          classes[r].prev = classes[l].right;
1515 1521
          classes[classes[l].right].next = r;
1516 1522
          classes[l].right = r;
1517 1523
          classes[r].parent = l;
1518 1524

	
1519 1525
          classes[r].next = -1;
1520 1526
          classes[r].depth = rln;
1521 1527
        }
1522 1528
      }
1523 1529
      return class_id;
1524 1530
    }
1525 1531

	
1526 1532
    /// \brief Split the class to subclasses.
1527 1533
    ///
1528 1534
    /// The current function splits the given class. The join, which
1529 1535
    /// made the current class, stored a reference to the
1530 1536
    /// subclasses. The \c splitClass() member restores the classes
1531 1537
    /// and creates the heaps. The parameter is an STL output iterator
1532 1538
    /// which will be filled with the subclass ids. The time
1533 1539
    /// complexity is O(log(n)*k) where n is the overall number of
1534 1540
    /// nodes in the splitted classes and k is the number of the
1535 1541
    /// classes.
1536 1542
    template <typename Iterator>
1537 1543
    void split(int cls, Iterator out) {
1538 1544
      std::vector<int> cs;
1539 1545
      { // splitting union-find
1540 1546
        int id = cls;
1541 1547
        int l = classes[id].left;
1542 1548

	
1543 1549
        classes[l].parent = classes[id].parent;
1544 1550
        classes[l].depth = classes[id].depth;
1545 1551

	
1546 1552
        nodes[~(classes[l].parent)].parent = ~l;
1547 1553

	
1548 1554
        *out++ = l;
1549 1555

	
1550 1556
        while (l != -1) {
1551 1557
          cs.push_back(l);
1552 1558
          l = classes[l].next;
1553 1559
        }
1554 1560

	
1555 1561
        classes[classes[id].right].next = first_class;
1556 1562
        classes[first_class].prev = classes[id].right;
1557 1563
        first_class = classes[id].left;
1558 1564

	
1559 1565
        if (classes[id].next != -1) {
1560 1566
          classes[classes[id].next].prev = classes[id].prev;
1561 1567
        }
1562 1568
        classes[classes[id].prev].next = classes[id].next;
1563 1569

	
1564 1570
        deleteClass(id);
1565 1571
      }
1566 1572

	
1567 1573
      {
1568 1574
        for (int i = 1; i < int(cs.size()); ++i) {
1569 1575
          int l = classes[cs[i]].depth;
1570 1576
          while (nodes[nodes[l].parent].left == l) {
1571 1577
            l = nodes[l].parent;
1572 1578
          }
1573 1579
          int r = l;
1574 1580
          while (nodes[l].parent >= 0) {
1575 1581
            l = nodes[l].parent;
1576 1582
            int new_node = newNode();
1577 1583

	
1578 1584
            nodes[new_node].prev = -1;
1579 1585
            nodes[new_node].next = -1;
1580 1586

	
1581 1587
            split(r, new_node);
1582 1588
            pushAfter(l, new_node);
1583 1589
            setPrio(l);
1584 1590
            setPrio(new_node);
1585 1591
            r = new_node;
1586 1592
          }
1587 1593
          classes[cs[i]].parent = ~r;
1588 1594
          classes[cs[i]].depth = classes[~(nodes[l].parent)].depth;
1589 1595
          nodes[r].parent = ~cs[i];
1590 1596

	
1591 1597
          nodes[l].next = -1;
1592 1598
          nodes[r].prev = -1;
1593 1599

	
1594 1600
          repairRight(~(nodes[l].parent));
1595 1601
          repairLeft(cs[i]);
1596 1602

	
1597 1603
          *out++ = cs[i];
1598 1604
        }
1599 1605
      }
1600 1606
    }
1601 1607

	
1602 1608
    /// \brief Gives back the priority of the current item.
1603 1609
    ///
1604
    /// \return Gives back the priority of the current item.
1610
    /// Gives back the priority of the current item.
1605 1611
    const Value& operator[](const Item& item) const {
1606 1612
      return nodes[index[item]].prio;
1607 1613
    }
1608 1614

	
1609 1615
    /// \brief Sets the priority of the current item.
1610 1616
    ///
1611 1617
    /// Sets the priority of the current item.
1612 1618
    void set(const Item& item, const Value& prio) {
1613 1619
      if (comp(prio, nodes[index[item]].prio)) {
1614 1620
        decrease(item, prio);
1615 1621
      } else if (!comp(prio, nodes[index[item]].prio)) {
1616 1622
        increase(item, prio);
1617 1623
      }
1618 1624
    }
1619 1625

	
1620 1626
    /// \brief Increase the priority of the current item.
1621 1627
    ///
1622 1628
    /// Increase the priority of the current item.
1623 1629
    void increase(const Item& item, const Value& prio) {
1624 1630
      int id = index[item];
1625 1631
      int kd = nodes[id].parent;
1626 1632
      nodes[id].prio = prio;
1627 1633
      while (kd >= 0 && nodes[kd].item == item) {
1628 1634
        setPrio(kd);
1629 1635
        kd = nodes[kd].parent;
1630 1636
      }
1631 1637
    }
1632 1638

	
1633 1639
    /// \brief Increase the priority of the current item.
1634 1640
    ///
1635 1641
    /// Increase the priority of the current item.
1636 1642
    void decrease(const Item& item, const Value& prio) {
1637 1643
      int id = index[item];
1638 1644
      int kd = nodes[id].parent;
1639 1645
      nodes[id].prio = prio;
1640 1646
      while (kd >= 0 && less(id, kd)) {
1641 1647
        nodes[kd].prio = prio;
1642 1648
        nodes[kd].item = item;
1643 1649
        kd = nodes[kd].parent;
1644 1650
      }
1645 1651
    }
1646 1652

	
1647 1653
    /// \brief Gives back the minimum priority of the class.
1648 1654
    ///
1649
    /// \return Gives back the minimum priority of the class.
1655
    /// Gives back the minimum priority of the class.
1650 1656
    const Value& classPrio(int cls) const {
1651 1657
      return nodes[~(classes[cls].parent)].prio;
1652 1658
    }
1653 1659

	
1654 1660
    /// \brief Gives back the minimum priority item of the class.
1655 1661
    ///
1656 1662
    /// \return Gives back the minimum priority item of the class.
1657 1663
    const Item& classTop(int cls) const {
1658 1664
      return nodes[~(classes[cls].parent)].item;
1659 1665
    }
1660 1666

	
1661 1667
    /// \brief Gives back a representant item of the class.
1662 1668
    ///
1669
    /// Gives back a representant item of the class.
1663 1670
    /// The representant is indpendent from the priorities of the
1664 1671
    /// items.
1665
    /// \return Gives back a representant item of the class.
1666 1672
    const Item& classRep(int id) const {
1667 1673
      int parent = classes[id].parent;
1668 1674
      return nodes[parent >= 0 ? classes[id].depth : leftNode(id)].item;
1669 1675
    }
1670 1676

	
1671 1677
    /// \brief LEMON style iterator for the items of a class.
1672 1678
    ///
1673 1679
    /// ClassIt is a lemon style iterator for the components. It iterates
1674 1680
    /// on the items of a class. By example if you want to iterate on
1675 1681
    /// each items of each classes then you may write the next code.
1676 1682
    ///\code
1677 1683
    /// for (ClassIt cit(huf); cit != INVALID; ++cit) {
1678 1684
    ///   std::cout << "Class: ";
1679 1685
    ///   for (ItemIt iit(huf, cit); iit != INVALID; ++iit) {
1680 1686
    ///     std::cout << toString(iit) << ' ' << std::endl;
1681 1687
    ///   }
1682 1688
    ///   std::cout << std::endl;
1683 1689
    /// }
1684 1690
    ///\endcode
1685 1691
    class ItemIt {
1686 1692
    private:
1687 1693

	
1688 1694
      const HeapUnionFind* _huf;
1689 1695
      int _id, _lid;
1690 1696

	
1691 1697
    public:
1692 1698

	
1693 1699
      /// \brief Default constructor
1694 1700
      ///
1695 1701
      /// Default constructor
1696 1702
      ItemIt() {}
1697 1703

	
1698 1704
      ItemIt(const HeapUnionFind& huf, int cls) : _huf(&huf) {
1699 1705
        int id = cls;
1700 1706
        int parent = _huf->classes[id].parent;
1701 1707
        if (parent >= 0) {
1702 1708
          _id = _huf->classes[id].depth;
1703 1709
          if (_huf->classes[id].next != -1) {
1704 1710
            _lid = _huf->classes[_huf->classes[id].next].depth;
1705 1711
          } else {
1706 1712
            _lid = -1;
1707 1713
          }
1708 1714
        } else {
1709 1715
          _id = _huf->leftNode(id);
1710 1716
          _lid = -1;
1711 1717
        }
1712 1718
      }
1713 1719

	
1714 1720
      /// \brief Increment operator
1715 1721
      ///
1716 1722
      /// It steps to the next item in the class.
1717 1723
      ItemIt& operator++() {
1718 1724
        _id = _huf->nextNode(_id);
1719 1725
        return *this;
1720 1726
      }
1721 1727

	
1722 1728
      /// \brief Conversion operator
1723 1729
      ///
1724 1730
      /// It converts the iterator to the current item.
1725 1731
      operator const Item&() const {
1726 1732
        return _huf->nodes[_id].item;
1727 1733
      }
1728 1734

	
1729 1735
      /// \brief Equality operator
1730 1736
      ///
1731 1737
      /// Equality operator
1732 1738
      bool operator==(const ItemIt& i) {
1733 1739
        return i._id == _id;
1734 1740
      }
1735 1741

	
1736 1742
      /// \brief Inequality operator
1737 1743
      ///
1738 1744
      /// Inequality operator
1739 1745
      bool operator!=(const ItemIt& i) {
1740 1746
        return i._id != _id;
1741 1747
      }
1742 1748

	
1743 1749
      /// \brief Equality operator
1744 1750
      ///
1745 1751
      /// Equality operator
1746 1752
      bool operator==(Invalid) {
1747 1753
        return _id == _lid;
1748 1754
      }
1749 1755

	
1750 1756
      /// \brief Inequality operator
1751 1757
      ///
1752 1758
      /// Inequality operator
1753 1759
      bool operator!=(Invalid) {
1754 1760
        return _id != _lid;
1755 1761
      }
1756 1762

	
1757 1763
    };
1758 1764

	
1759 1765
    /// \brief Class iterator
1760 1766
    ///
1761 1767
    /// The iterator stores
Ignore white space 6 line context
1 1
AC_DEFUN([LX_CHECK_CPLEX],
2 2
[
3 3
  AC_ARG_WITH([cplex],
4 4
AS_HELP_STRING([--with-cplex@<:@=PREFIX@:>@], [search for CPLEX under PREFIX or under the default search paths if PREFIX is not given @<:@default@:>@])
5 5
AS_HELP_STRING([--without-cplex], [disable checking for CPLEX]),
6 6
              [], [with_cplex=yes])
7 7

	
8 8
  AC_ARG_WITH([cplex-includedir],
9 9
AS_HELP_STRING([--with-cplex-includedir=DIR], [search for CPLEX headers in DIR]),
10 10
              [], [with_cplex_includedir=no])
11 11

	
12 12
  AC_ARG_WITH([cplex-libdir],
13 13
AS_HELP_STRING([--with-cplex-libdir=DIR], [search for CPLEX libraries in DIR]),
14 14
              [], [with_cplex_libdir=no])
15 15

	
16 16
  lx_cplex_found=no
17 17
  if test x"$with_cplex" != x"no"; then
18 18
    AC_MSG_CHECKING([for CPLEX])
19 19

	
20 20
    if test x"$with_cplex_includedir" != x"no"; then
21 21
      CPLEX_CFLAGS="-I$with_cplex_includedir"
22 22
    elif test x"$with_cplex" != x"yes"; then
23 23
      CPLEX_CFLAGS="-I$with_cplex/include"
24 24
    elif test x"$CPLEX_INCLUDEDIR" != x; then
25 25
      CPLEX_CFLAGS="-I$CPLEX_INCLUDEDIR"
26 26
    fi
27 27

	
28 28
    if test x"$with_cplex_libdir" != x"no"; then
29 29
      CPLEX_LDFLAGS="-L$with_cplex_libdir"
30 30
    elif test x"$with_cplex" != x"yes"; then
31 31
      CPLEX_LDFLAGS="-L$with_cplex/lib"
32 32
    elif test x"$CPLEX_LIBDIR" != x; then
33 33
      CPLEX_LDFLAGS="-L$CPLEX_LIBDIR"
34 34
    fi
35 35
    CPLEX_LIBS="-lcplex -lm -lpthread"
36 36

	
37 37
    lx_save_cxxflags="$CXXFLAGS"
38 38
    lx_save_ldflags="$LDFLAGS"
39 39
    lx_save_libs="$LIBS"
40 40
    CXXFLAGS="$CPLEX_CFLAGS"
41 41
    LDFLAGS="$CPLEX_LDFLAGS"
42 42
    LIBS="$CPLEX_LIBS"
43 43

	
44 44
    lx_cplex_test_prog='
45 45
      extern "C" {
46 46
      #include <ilcplex/cplex.h>
47 47
      }
48 48

	
49 49
      int main(int argc, char** argv)
50 50
      {
51 51
        CPXENVptr env = NULL;
52 52
        return 0;
53 53
      }'
54 54

	
55 55
    AC_LANG_PUSH(C++)
56 56
    AC_LINK_IFELSE([$lx_cplex_test_prog], [lx_cplex_found=yes], [lx_cplex_found=no])
57 57
    AC_LANG_POP(C++)
58 58

	
59 59
    CXXFLAGS="$lx_save_cxxflags"
60 60
    LDFLAGS="$lx_save_ldflags"
61 61
    LIBS="$lx_save_libs"
62 62

	
63 63
    if test x"$lx_cplex_found" = x"yes"; then
64
      AC_DEFINE([HAVE_CPLEX], [1], [Define to 1 if you have CPLEX.])
64
      AC_DEFINE([LEMON_HAVE_CPLEX], [1], [Define to 1 if you have CPLEX.])
65 65
      lx_lp_found=yes
66
      AC_DEFINE([HAVE_LP], [1], [Define to 1 if you have any LP solver.])
66
      AC_DEFINE([LEMON_HAVE_LP], [1], [Define to 1 if you have any LP solver.])
67 67
      lx_mip_found=yes
68
      AC_DEFINE([HAVE_MIP], [1], [Define to 1 if you have any MIP solver.])
68
      AC_DEFINE([LEMON_HAVE_MIP], [1], [Define to 1 if you have any MIP solver.])
69 69
      AC_MSG_RESULT([yes])
70 70
    else
71 71
      CPLEX_CFLAGS=""
72 72
      CPLEX_LDFLAGS=""
73 73
      CPLEX_LIBS=""
74 74
      AC_MSG_RESULT([no])
75 75
    fi
76 76
  fi
77 77
  CPLEX_LIBS="$CPLEX_LDFLAGS $CPLEX_LIBS"
78 78
  AC_SUBST(CPLEX_CFLAGS)
79 79
  AC_SUBST(CPLEX_LIBS)
80 80
  AM_CONDITIONAL([HAVE_CPLEX], [test x"$lx_cplex_found" = x"yes"])
81 81
])
Ignore white space 6 line context
1 1
AC_DEFUN([LX_CHECK_GLPK],
2 2
[
3 3
  AC_ARG_WITH([glpk],
4 4
AS_HELP_STRING([--with-glpk@<:@=PREFIX@:>@], [search for GLPK under PREFIX or under the default search paths if PREFIX is not given @<:@default@:>@])
5 5
AS_HELP_STRING([--without-glpk], [disable checking for GLPK]),
6 6
              [], [with_glpk=yes])
7 7

	
8 8
  AC_ARG_WITH([glpk-includedir],
9 9
AS_HELP_STRING([--with-glpk-includedir=DIR], [search for GLPK headers in DIR]),
10 10
              [], [with_glpk_includedir=no])
11 11

	
12 12
  AC_ARG_WITH([glpk-libdir],
13 13
AS_HELP_STRING([--with-glpk-libdir=DIR], [search for GLPK libraries in DIR]),
14 14
              [], [with_glpk_libdir=no])
15 15

	
16 16
  lx_glpk_found=no
17 17
  if test x"$with_glpk" != x"no"; then
18 18
    AC_MSG_CHECKING([for GLPK])
19 19

	
20 20
    if test x"$with_glpk_includedir" != x"no"; then
21 21
      GLPK_CFLAGS="-I$with_glpk_includedir"
22 22
    elif test x"$with_glpk" != x"yes"; then
23 23
      GLPK_CFLAGS="-I$with_glpk/include"
24 24
    fi
25 25

	
26 26
    if test x"$with_glpk_libdir" != x"no"; then
27 27
      GLPK_LDFLAGS="-L$with_glpk_libdir"
28 28
    elif test x"$with_glpk" != x"yes"; then
29 29
      GLPK_LDFLAGS="-L$with_glpk/lib"
30 30
    fi
31 31
    GLPK_LIBS="-lglpk"
32 32

	
33 33
    lx_save_cxxflags="$CXXFLAGS"
34 34
    lx_save_ldflags="$LDFLAGS"
35 35
    lx_save_libs="$LIBS"
36 36
    CXXFLAGS="$GLPK_CFLAGS"
37 37
    LDFLAGS="$GLPK_LDFLAGS"
38 38
    LIBS="$GLPK_LIBS"
39 39

	
40 40
    lx_glpk_test_prog='
41 41
      extern "C" {
42 42
      #include <glpk.h>
43 43
      }
44 44

	
45 45
      #if (GLP_MAJOR_VERSION < 4) \
46 46
         || (GLP_MAJOR_VERSION == 4 && GLP_MINOR_VERSION < 33)
47 47
      #error Supported GLPK versions: 4.33 or above
48 48
      #endif
49 49

	
50 50
      int main(int argc, char** argv)
51 51
      {
52 52
        LPX *lp;
53 53
        lp = lpx_create_prob();
54 54
        lpx_delete_prob(lp);
55 55
        return 0;
56 56
      }'
57 57

	
58 58
    AC_LANG_PUSH(C++)
59 59
    AC_LINK_IFELSE([$lx_glpk_test_prog], [lx_glpk_found=yes], [lx_glpk_found=no])
60 60
    AC_LANG_POP(C++)
61 61

	
62 62
    CXXFLAGS="$lx_save_cxxflags"
63 63
    LDFLAGS="$lx_save_ldflags"
64 64
    LIBS="$lx_save_libs"
65 65

	
66 66
    if test x"$lx_glpk_found" = x"yes"; then
67
      AC_DEFINE([HAVE_GLPK], [1], [Define to 1 if you have GLPK.])
67
      AC_DEFINE([LEMON_HAVE_GLPK], [1], [Define to 1 if you have GLPK.])
68 68
      lx_lp_found=yes
69
      AC_DEFINE([HAVE_LP], [1], [Define to 1 if you have any LP solver.])
69
      AC_DEFINE([LEMON_HAVE_LP], [1], [Define to 1 if you have any LP solver.])
70 70
      lx_mip_found=yes
71
      AC_DEFINE([HAVE_MIP], [1], [Define to 1 if you have any MIP solver.])
71
      AC_DEFINE([LEMON_HAVE_MIP], [1], [Define to 1 if you have any MIP solver.])
72 72
      AC_MSG_RESULT([yes])
73 73
    else
74 74
      GLPK_CFLAGS=""
75 75
      GLPK_LDFLAGS=""
76 76
      GLPK_LIBS=""
77 77
      AC_MSG_RESULT([no])
78 78
    fi
79 79
  fi
80 80
  GLPK_LIBS="$GLPK_LDFLAGS $GLPK_LIBS"
81 81
  AC_SUBST(GLPK_CFLAGS)
82 82
  AC_SUBST(GLPK_LIBS)
83 83
  AM_CONDITIONAL([HAVE_GLPK], [test x"$lx_glpk_found" = x"yes"])
84 84
])
Ignore white space 6 line context
1 1
AC_DEFUN([LX_CHECK_SOPLEX],
2 2
[
3 3
  AC_ARG_WITH([soplex],
4 4
AS_HELP_STRING([--with-soplex@<:@=PREFIX@:>@], [search for SOPLEX under PREFIX or under the default search paths if PREFIX is not given @<:@default@:>@])
5 5
AS_HELP_STRING([--without-soplex], [disable checking for SOPLEX]),
6 6
              [], [with_soplex=yes])
7 7

	
8 8
  AC_ARG_WITH([soplex-includedir],
9 9
AS_HELP_STRING([--with-soplex-includedir=DIR], [search for SOPLEX headers in DIR]),
10 10
              [], [with_soplex_includedir=no])
11 11

	
12 12
  AC_ARG_WITH([soplex-libdir],
13 13
AS_HELP_STRING([--with-soplex-libdir=DIR], [search for SOPLEX libraries in DIR]),
14 14
              [], [with_soplex_libdir=no])
15 15

	
16 16
  lx_soplex_found=no
17 17
  if test x"$with_soplex" != x"no"; then
18 18
    AC_MSG_CHECKING([for SOPLEX])
19 19

	
20 20
    if test x"$with_soplex_includedir" != x"no"; then
21 21
      SOPLEX_CXXFLAGS="-I$with_soplex_includedir"
22 22
    elif test x"$with_soplex" != x"yes"; then
23
      SOPLEX_CXXFLAGS="-I$with_soplex/include"
23
      SOPLEX_CXXFLAGS="-I$with_soplex/src"
24 24
    fi
25 25

	
26 26
    if test x"$with_soplex_libdir" != x"no"; then
27 27
      SOPLEX_LDFLAGS="-L$with_soplex_libdir"
28 28
    elif test x"$with_soplex" != x"yes"; then
29 29
      SOPLEX_LDFLAGS="-L$with_soplex/lib"
30 30
    fi
31 31
    SOPLEX_LIBS="-lsoplex -lz"
32 32

	
33 33
    lx_save_cxxflags="$CXXFLAGS"
34 34
    lx_save_ldflags="$LDFLAGS"
35 35
    lx_save_libs="$LIBS"
36 36
    CXXFLAGS="$SOPLEX_CXXFLAGS"
37 37
    LDFLAGS="$SOPLEX_LDFLAGS"
38 38
    LIBS="$SOPLEX_LIBS"
39 39

	
40 40
    lx_soplex_test_prog='
41
      #include <soplex/soplex.h>
41
      #include <soplex.h>
42 42

	
43 43
      int main(int argc, char** argv)
44 44
      {
45 45
        soplex::SoPlex soplex;
46 46
        return 0;
47 47
      }'
48 48

	
49 49
    AC_LANG_PUSH(C++)
50 50
    AC_LINK_IFELSE([$lx_soplex_test_prog], [lx_soplex_found=yes], [lx_soplex_found=no])
51 51
    AC_LANG_POP(C++)
52 52

	
53 53
    CXXFLAGS="$lx_save_cxxflags"
54 54
    LDFLAGS="$lx_save_ldflags"
55 55
    LIBS="$lx_save_libs"
56 56

	
57 57
    if test x"$lx_soplex_found" = x"yes"; then
58
      AC_DEFINE([HAVE_SOPLEX], [1], [Define to 1 if you have SOPLEX.])
58
      AC_DEFINE([LEMON_HAVE_SOPLEX], [1], [Define to 1 if you have SOPLEX.])
59 59
      lx_lp_found=yes
60
      AC_DEFINE([HAVE_LP], [1], [Define to 1 if you have any LP solver.])
60
      AC_DEFINE([LEMON_HAVE_LP], [1], [Define to 1 if you have any LP solver.])
61 61
      AC_MSG_RESULT([yes])
62 62
    else
63 63
      SOPLEX_CXXFLAGS=""
64 64
      SOPLEX_LDFLAGS=""
65 65
      SOPLEX_LIBS=""
66 66
      AC_MSG_RESULT([no])
67 67
    fi
68 68
  fi
69 69
  SOPLEX_LIBS="$SOPLEX_LDFLAGS $SOPLEX_LIBS"
70 70
  AC_SUBST(SOPLEX_CXXFLAGS)
71 71
  AC_SUBST(SOPLEX_LIBS)
72 72
  AM_CONDITIONAL([HAVE_SOPLEX], [test x"$lx_soplex_found" = x"yes"])
73 73
])
Ignore white space 6 line context
1 1
#! /usr/bin/env python
2
#
3
# This file is a part of LEMON, a generic C++ optimization library.
4
#
5
# Copyright (C) 2003-2009
6
# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
# (Egervary Research Group on Combinatorial Optimization, EGRES).
8
#
9
# Permission to use, modify and distribute this software is granted
10
# provided that this copyright notice appears in all copies. For
11
# precise terms see the accompanying LICENSE file.
12
#
13
# This software is provided "AS IS" with no warranty of any kind,
14
# express or implied, and with no claim as to its suitability for any
15
# purpose.
2 16

	
3 17
import sys
4 18

	
5 19
from mercurial import ui, hg
6 20
from mercurial import util
7 21

	
8 22
util.rcpath = lambda : []
9 23

	
10 24
if len(sys.argv)>1 and sys.argv[1] in ["-h","--help"]:
11 25
    print """
12 26
This utility just prints the length of the longest path
13 27
in the revision graph from revison 0 to the current one.
14 28
"""
15 29
    exit(0)
16 30

	
17 31
u = ui.ui()
18 32
r = hg.repository(u, ".")
19 33
N = r.changectx(".").rev()
20 34
lengths=[0]*(N+1)
21 35
for i in range(N+1):
22 36
    p=r.changectx(i).parents()
23 37
    if p[0]:
24 38
        p0=lengths[p[0].rev()]
25 39
    else:
26 40
        p0=-1
27 41
    if len(p)>1 and p[1]:
28 42
        p1=lengths[p[1].rev()]
29 43
    else:
30 44
        p1=-1
31 45
    lengths[i]=max(p0,p1)+1
32 46
print lengths[N]
Ignore white space 6 line context
1 1
#!/bin/bash
2
#
3
# This file is a part of LEMON, a generic C++ optimization library.
4
#
5
# Copyright (C) 2003-2009
6
# Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
# (Egervary Research Group on Combinatorial Optimization, EGRES).
8
#
9
# Permission to use, modify and distribute this software is granted
10
# provided that this copyright notice appears in all copies. For
11
# precise terms see the accompanying LICENSE file.
12
#
13
# This software is provided "AS IS" with no warranty of any kind,
14
# express or implied, and with no claim as to its suitability for any
15
# purpose.
2 16

	
3
YEAR=`date +2003-%Y`
17
YEAR=`date +%Y`
4 18
HGROOT=`hg root`
5 19

	
20
function hg_year() {
21
    if [ -n "$(hg st $1)" ]; then
22
        echo $YEAR
23
    else
24
        hg log -l 1 --template='{date|isodate}\n' $1 |
25
        cut -d '-' -f 1
26
    fi
27
}
28

	
6 29
# file enumaration modes
7 30

	
8 31
function all_files() {
9 32
    hg status -a -m -c |
10 33
    cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' |
11 34
    while read file; do echo $HGROOT/$file; done
12 35
}
13 36

	
14 37
function modified_files() {
15 38
    hg status -a -m |
16 39
    cut -d ' ' -f 2 | grep -E  '(\.(cc|h|dox)$|Makefile\.am$)' |
17 40
    while read file; do echo $HGROOT/$file; done
18 41
}
19 42

	
20 43
function changed_files() {
21 44
    {
22 45
        if [ -n "$HG_PARENT1" ]
23 46
        then
24 47
            hg status --rev $HG_PARENT1:$HG_NODE -a -m
25 48
        fi
26 49
        if [ -n "$HG_PARENT2" ]
27 50
        then
28 51
            hg status --rev $HG_PARENT2:$HG_NODE -a -m
29 52
        fi
30 53
    } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | 
31 54
    sort | uniq |
32 55
    while read file; do echo $HGROOT/$file; done
33 56
}
34 57

	
35 58
function given_files() {
36 59
    for file in $GIVEN_FILES
37 60
    do
38 61
	echo $file
39 62
    done
40 63
}
41 64

	
42 65
# actions
43 66

	
44 67
function update_action() {
45 68
    if ! diff -q $1 $2 >/dev/null
46 69
    then
47 70
	echo -n " [$3 updated]"
48 71
	rm $2
49 72
	mv $1 $2
50 73
	CHANGED=YES
51 74
    fi
52 75
}
53 76

	
54 77
function update_warning() {
55 78
    echo -n " [$2 warning]"
56 79
    WARNED=YES
57 80
}
58 81

	
59 82
function update_init() {
60 83
    echo Update source files...
61 84
    TOTAL_FILES=0
62 85
    CHANGED_FILES=0
63 86
    WARNED_FILES=0
64 87
}
65 88

	
66 89
function update_done() {
67 90
    echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
68 91
    echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
69 92
}
70 93

	
71 94
function update_begin() {
72 95
    ((TOTAL_FILES++))
73 96
    CHANGED=NO
74 97
    WARNED=NO
75 98
}
76 99

	
77 100
function update_end() {
78 101
    if [ $CHANGED == YES ]
79 102
    then
80 103
	((++CHANGED_FILES))
81 104
    fi
82 105
    if [ $WARNED == YES ]
83 106
    then
84 107
	((++WARNED_FILES))
85 108
    fi
86 109
}
87 110

	
88 111
function check_action() {
89 112
    if [ "$3" == 'tabs' ]
90 113
    then
91
        PATTERN=$(echo -e '\t')
114
        if echo $2 | grep -q -v -E 'Makefile\.am$'
115
        then
116
            PATTERN=$(echo -e '\t')
117
        else
118
            PATTERN='        '
119
        fi
92 120
    elif [ "$3" == 'trailing spaces' ]
93 121
    then
94 122
        PATTERN='\ +$'
95 123
    else
96 124
        PATTERN='*'
97 125
    fi
98 126

	
99 127
    if ! diff -q $1 $2 >/dev/null
100 128
    then
101 129
        if [ "$PATTERN" == '*' ]
102 130
        then
103 131
            diff $1 $2 | grep '^[0-9]' | sed "s|^\(.*\)c.*$|$2:\1: check failed: $3|g" |
104 132
              sed "s/:\([0-9]*\),\([0-9]*\):\(.*\)$/:\1:\3 (until line \2)/g"
105 133
        else
106 134
            grep -n -E "$PATTERN" $2 | sed "s|^\([0-9]*\):.*$|$2:\1: check failed: $3|g"
107 135
        fi
108 136
        FAILED=YES
109 137
    fi
110 138
}
111 139

	
112 140
function check_warning() {
113 141
    if [ "$2" == 'long lines' ]
114 142
    then
115 143
        grep -n -E '.{81,}' $1 | sed "s|^\([0-9]*\):.*$|$1:\1: warning: $2|g"
116 144
    else
117 145
        echo "$1: warning: $2"
118 146
    fi
119 147
    WARNED=YES
120 148
}
121 149

	
122 150
function check_init() {
123 151
    echo Check source files...
124 152
    FAILED_FILES=0
125 153
    WARNED_FILES=0
126 154
    TOTAL_FILES=0
127 155
}
128 156

	
129 157
function check_done() {
130 158
    echo $FAILED_FILES out of $TOTAL_FILES files has been failed.
131 159
    echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
132 160

	
133 161
    if [ $WARNED_FILES -gt 0 -o $FAILED_FILES -gt 0 ]
134 162
    then
135 163
	if [ "$WARNING" == 'INTERACTIVE' ]
136 164
	then
137 165
	    echo -n "Are the files with errors/warnings acceptable? (yes/no) "
138 166
	    while read answer
139 167
	    do
140 168
		if [ "$answer" == 'yes' ]
141 169
		then
142 170
		    return 0
143 171
		elif [ "$answer" == 'no' ]
144 172
		then
145 173
		    return 1
146 174
		fi
147 175
		echo -n "Are the files with errors/warnings acceptable? (yes/no) "
148 176
	    done
149 177
	elif [ "$WARNING" == 'WERROR' ]
150 178
	then
151 179
	    return 1
152 180
	fi
153 181
    fi
154 182
}
155 183

	
156 184
function check_begin() {
157 185
    ((TOTAL_FILES++))
158 186
    FAILED=NO
159 187
    WARNED=NO
160 188
}
161 189

	
162 190
function check_end() {
163 191
    if [ $FAILED == YES ]
164 192
    then
165 193
	((++FAILED_FILES))
166 194
    fi
167 195
    if [ $WARNED == YES ]
168 196
    then
169 197
	((++WARNED_FILES))
170 198
    fi
171 199
}
172 200

	
173 201

	
174 202

	
175 203
# checks
176 204

	
177 205
function header_check() {
178 206
    if echo $1 | grep -q -E 'Makefile\.am$'
179 207
    then
180 208
	return
181 209
    fi
182 210

	
183 211
    TMP_FILE=`mktemp`
184 212

	
185 213
    (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
186 214
 *
187 215
 * This file is a part of LEMON, a generic C++ optimization library.
188 216
 *
189
 * Copyright (C) "$YEAR"
217
 * Copyright (C) 2003-"$(hg_year $1)"
190 218
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
191 219
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
192 220
 *
193 221
 * Permission to use, modify and distribute this software is granted
194 222
 * provided that this copyright notice appears in all copies. For
195 223
 * precise terms see the accompanying LICENSE file.
196 224
 *
197 225
 * This software is provided \"AS IS\" with no warranty of any kind,
198 226
 * express or implied, and with no claim as to its suitability for any
199 227
 * purpose.
200 228
 *
201 229
 */
202 230
"
203 231
    awk 'BEGIN { pm=0; }
204 232
     pm==3 { print }
205 233
     /\/\* / && pm==0 { pm=1;}
206 234
     /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
207 235
     /\*\// && pm==1 { pm=2;}
208 236
    ' $1
209 237
    ) >$TMP_FILE
210 238

	
211 239
    "$ACTION"_action "$TMP_FILE" "$1" header
212 240
}
213 241

	
214 242
function tabs_check() {
215 243
    if echo $1 | grep -q -v -E 'Makefile\.am$'
216 244
    then
217 245
        OLD_PATTERN=$(echo -e '\t')
218 246
        NEW_PATTERN='        '
219 247
    else
220 248
        OLD_PATTERN='        '
221 249
        NEW_PATTERN=$(echo -e '\t')
222 250
    fi
223 251
    TMP_FILE=`mktemp`
224 252
    cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
225 253

	
226 254
    "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
227 255
}
228 256

	
229 257
function spaces_check() {
230 258
    TMP_FILE=`mktemp`
231 259
    cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
232 260

	
233 261
    "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
234 262
}
235 263

	
236 264
function long_lines_check() {
237 265
    if cat $1 | grep -q -E '.{81,}'
238 266
    then
239 267
	"$ACTION"_warning $1 'long lines'
240 268
    fi
241 269
}
242 270

	
243 271
# process the file
244 272

	
245 273
function process_file() {
246 274
    if [ "$ACTION" == 'update' ]
247 275
    then
248 276
        echo -n "    $ACTION $1..."
249 277
    else
250 278
        echo "	  $ACTION $1..."
251 279
    fi
252 280

	
253 281
    CHECKING="header tabs spaces long_lines"
254 282

	
255 283
    "$ACTION"_begin $1
256 284
    for check in $CHECKING
257 285
    do
258 286
	"$check"_check $1
259 287
    done
260 288
    "$ACTION"_end $1
261 289
    if [ "$ACTION" == 'update' ]
262 290
    then
263 291
        echo
264 292
    fi
265 293
}
266 294

	
267 295
function process_all {
268 296
    "$ACTION"_init
269 297
    while read file
270 298
    do
271 299
	process_file $file
272 300
    done < <($FILES)
273 301
    "$ACTION"_done
274 302
}
275 303

	
276 304
while [ $# -gt 0 ]
277 305
do
278 306
    
279 307
    if [ "$1" == '--help' ] || [ "$1" == '-h' ]
280 308
    then
281 309
	echo -n \
282 310
"Usage:
283 311
  $0 [OPTIONS] [files]
284 312
Options:
285 313
  --dry-run|-n
Ignore white space 6 line context
1
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
1
INCLUDE_DIRECTORIES(
2
  ${PROJECT_SOURCE_DIR}
3
  ${PROJECT_BINARY_DIR}
4
)
2 5

	
3
LINK_DIRECTORIES(${CMAKE_BINARY_DIR}/lemon)
6
LINK_DIRECTORIES(
7
  ${PROJECT_BINARY_DIR}/lemon
8
)
4 9

	
5 10
SET(TESTS
6 11
  adaptors_test
12
  bellman_ford_test
7 13
  bfs_test
8 14
  circulation_test
15
  connectivity_test
9 16
  counter_test
10 17
  dfs_test
11 18
  digraph_test
12 19
  dijkstra_test
13 20
  dim_test
21
  edge_set_test
14 22
  error_test
15
  edge_set_test
23
  euler_test
24
  gomory_hu_test
16 25
  graph_copy_test
17 26
  graph_test
18 27
  graph_utils_test
19 28
  hao_orlin_test
20 29
  heap_test
21 30
  kruskal_test
22
  lp_test
23
  mip_test
24 31
  maps_test
25
  max_matching_test
26
  radix_sort_test
32
  matching_test
33
  min_cost_arborescence_test
34
  min_cost_flow_test
35
  min_mean_cycle_test
27 36
  path_test
28 37
  preflow_test
38
  radix_sort_test
29 39
  random_test
30 40
  suurballe_test
31 41
  time_measure_test
32
  unionfind_test)
42
  unionfind_test
43
)
44

	
45
IF(LEMON_HAVE_LP)
46
  ADD_EXECUTABLE(lp_test lp_test.cc)
47
  SET(LP_TEST_LIBS lemon)
48

	
49
  IF(LEMON_HAVE_GLPK)
50
    SET(LP_TEST_LIBS ${LP_TEST_LIBS} ${GLPK_LIBRARIES})
51
  ENDIF()
52
  IF(LEMON_HAVE_CPLEX)
53
    SET(LP_TEST_LIBS ${LP_TEST_LIBS} ${CPLEX_LIBRARIES})
54
  ENDIF()
55
  IF(LEMON_HAVE_CLP)
56
    SET(LP_TEST_LIBS ${LP_TEST_LIBS} ${COIN_CLP_LIBRARIES})
57
  ENDIF()
58

	
59
  TARGET_LINK_LIBRARIES(lp_test ${LP_TEST_LIBS})
60
  ADD_TEST(lp_test lp_test)
61

	
62
  IF(WIN32 AND LEMON_HAVE_GLPK)
63
    GET_TARGET_PROPERTY(TARGET_LOC lp_test LOCATION)
64
    GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH)
65
    ADD_CUSTOM_COMMAND(TARGET lp_test POST_BUILD
66
      COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/glpk.dll ${TARGET_PATH}
67
      COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/libltdl3.dll ${TARGET_PATH}
68
      COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/zlib1.dll ${TARGET_PATH}
69
    )
70
  ENDIF()
71

	
72
  IF(WIN32 AND LEMON_HAVE_CPLEX)
73
    GET_TARGET_PROPERTY(TARGET_LOC lp_test LOCATION)
74
    GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH)
75
    ADD_CUSTOM_COMMAND(TARGET lp_test POST_BUILD
76
      COMMAND ${CMAKE_COMMAND} -E copy ${CPLEX_BIN_DIR}/cplex91.dll ${TARGET_PATH}
77
    )
78
  ENDIF()
79
ENDIF()
80

	
81
IF(LEMON_HAVE_MIP)
82
  ADD_EXECUTABLE(mip_test mip_test.cc)
83
  SET(MIP_TEST_LIBS lemon)
84

	
85
  IF(LEMON_HAVE_GLPK)
86
    SET(MIP_TEST_LIBS ${MIP_TEST_LIBS} ${GLPK_LIBRARIES})
87
  ENDIF()
88
  IF(LEMON_HAVE_CPLEX)
89
    SET(MIP_TEST_LIBS ${MIP_TEST_LIBS} ${CPLEX_LIBRARIES})
90
  ENDIF()
91
  IF(LEMON_HAVE_CBC)
92
    SET(MIP_TEST_LIBS ${MIP_TEST_LIBS} ${COIN_CBC_LIBRARIES})
93
  ENDIF()
94

	
95
  TARGET_LINK_LIBRARIES(mip_test ${MIP_TEST_LIBS})
96
  ADD_TEST(mip_test mip_test)
97

	
98
  IF(WIN32 AND LEMON_HAVE_GLPK)
99
    GET_TARGET_PROPERTY(TARGET_LOC mip_test LOCATION)
100
    GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH)
101
    ADD_CUSTOM_COMMAND(TARGET mip_test POST_BUILD
102
      COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/glpk.dll ${TARGET_PATH}
103
      COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/libltdl3.dll ${TARGET_PATH}
104
      COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/zlib1.dll ${TARGET_PATH}
105
    )
106
  ENDIF()
107

	
108
  IF(WIN32 AND LEMON_HAVE_CPLEX)
109
    GET_TARGET_PROPERTY(TARGET_LOC mip_test LOCATION)
110
    GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH)
111
    ADD_CUSTOM_COMMAND(TARGET mip_test POST_BUILD
112
      COMMAND ${CMAKE_COMMAND} -E copy ${CPLEX_BIN_DIR}/cplex91.dll ${TARGET_PATH}
113
    )
114
  ENDIF()
115
ENDIF()
33 116

	
34 117
FOREACH(TEST_NAME ${TESTS})
35 118
  ADD_EXECUTABLE(${TEST_NAME} ${TEST_NAME}.cc)
36 119
  TARGET_LINK_LIBRARIES(${TEST_NAME} lemon)
37 120
  ADD_TEST(${TEST_NAME} ${TEST_NAME})
38
ENDFOREACH(TEST_NAME)
121
ENDFOREACH()
Ignore white space 6 line context
1 1
EXTRA_DIST += \
2 2
	test/CMakeLists.txt
3 3

	
4 4
noinst_HEADERS += \
5 5
	test/graph_test.h \
6 6
	test/test_tools.h
7 7

	
8 8
check_PROGRAMS += \
9 9
	test/adaptors_test \
10
	test/bellman_ford_test \
10 11
	test/bfs_test \
11 12
	test/circulation_test \
13
	test/connectivity_test \
12 14
	test/counter_test \
13 15
	test/dfs_test \
14 16
	test/digraph_test \
15 17
	test/dijkstra_test \
16 18
	test/dim_test \
17 19
	test/edge_set_test \
18 20
	test/error_test \
21
	test/euler_test \
22
	test/gomory_hu_test \
19 23
	test/graph_copy_test \
20 24
	test/graph_test \
21 25
	test/graph_utils_test \
22 26
	test/hao_orlin_test \
23 27
	test/heap_test \
24 28
	test/kruskal_test \
25 29
	test/maps_test \
26
	test/max_matching_test \
30
	test/matching_test \
31
	test/min_cost_arborescence_test \
32
	test/min_cost_flow_test \
33
	test/min_mean_cycle_test \
27 34
	test/path_test \
28 35
	test/preflow_test \
29 36
	test/radix_sort_test \
30 37
	test/random_test \
31 38
	test/suurballe_test \
32 39
	test/test_tools_fail \
33 40
	test/test_tools_pass \
34 41
	test/time_measure_test \
35 42
	test/unionfind_test
36 43

	
44
test_test_tools_pass_DEPENDENCIES = demo
45

	
37 46
if HAVE_LP
38 47
check_PROGRAMS += test/lp_test
39 48
endif HAVE_LP
40 49
if HAVE_MIP
41 50
check_PROGRAMS += test/mip_test
42 51
endif HAVE_MIP
43 52

	
44 53
TESTS += $(check_PROGRAMS)
45 54
XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
46 55

	
47 56
test_adaptors_test_SOURCES = test/adaptors_test.cc
57
test_bellman_ford_test_SOURCES = test/bellman_ford_test.cc
48 58
test_bfs_test_SOURCES = test/bfs_test.cc
49 59
test_circulation_test_SOURCES = test/circulation_test.cc
50 60
test_counter_test_SOURCES = test/counter_test.cc
61
test_connectivity_test_SOURCES = test/connectivity_test.cc
51 62
test_dfs_test_SOURCES = test/dfs_test.cc
52 63
test_digraph_test_SOURCES = test/digraph_test.cc
53 64
test_dijkstra_test_SOURCES = test/dijkstra_test.cc
54 65
test_dim_test_SOURCES = test/dim_test.cc
55 66
test_edge_set_test_SOURCES = test/edge_set_test.cc
56 67
test_error_test_SOURCES = test/error_test.cc
68
test_euler_test_SOURCES = test/euler_test.cc
69
test_gomory_hu_test_SOURCES = test/gomory_hu_test.cc
57 70
test_graph_copy_test_SOURCES = test/graph_copy_test.cc
58 71
test_graph_test_SOURCES = test/graph_test.cc
59 72
test_graph_utils_test_SOURCES = test/graph_utils_test.cc
60 73
test_heap_test_SOURCES = test/heap_test.cc
61 74
test_kruskal_test_SOURCES = test/kruskal_test.cc
62 75
test_hao_orlin_test_SOURCES = test/hao_orlin_test.cc
63 76
test_lp_test_SOURCES = test/lp_test.cc
64 77
test_maps_test_SOURCES = test/maps_test.cc
65 78
test_mip_test_SOURCES = test/mip_test.cc
66
test_max_matching_test_SOURCES = test/max_matching_test.cc
79
test_matching_test_SOURCES = test/matching_test.cc
80
test_min_cost_arborescence_test_SOURCES = test/min_cost_arborescence_test.cc
81
test_min_cost_flow_test_SOURCES = test/min_cost_flow_test.cc
82
test_min_mean_cycle_test_SOURCES = test/min_mean_cycle_test.cc
67 83
test_path_test_SOURCES = test/path_test.cc
68 84
test_preflow_test_SOURCES = test/preflow_test.cc
69 85
test_radix_sort_test_SOURCES = test/radix_sort_test.cc
70 86
test_suurballe_test_SOURCES = test/suurballe_test.cc
71 87
test_random_test_SOURCES = test/random_test.cc
72 88
test_test_tools_fail_SOURCES = test/test_tools_fail.cc
73 89
test_test_tools_pass_SOURCES = test/test_tools_pass.cc
74 90
test_time_measure_test_SOURCES = test/time_measure_test.cc
75 91
test_unionfind_test_SOURCES = test/unionfind_test.cc
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/concepts/digraph.h>
20 20
#include <lemon/smart_graph.h>
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23 23
#include <lemon/bfs.h>
24 24
#include <lemon/path.h>
25 25

	
26 26
#include "graph_test.h"
27 27
#include "test_tools.h"
28 28

	
29 29
using namespace lemon;
30 30

	
31 31
char test_lgf[] =
32 32
  "@nodes\n"
33 33
  "label\n"
34 34
  "0\n"
35 35
  "1\n"
36 36
  "2\n"
37 37
  "3\n"
38 38
  "4\n"
39 39
  "5\n"
40 40
  "@arcs\n"
41 41
  "     label\n"
42 42
  "0 1  0\n"
43 43
  "1 2  1\n"
44 44
  "2 3  2\n"
45 45
  "3 4  3\n"
46 46
  "0 3  4\n"
47 47
  "0 3  5\n"
48 48
  "5 2  6\n"
49 49
  "@attributes\n"
50 50
  "source 0\n"
51 51
  "target 4\n";
52 52

	
53 53
void checkBfsCompile()
54 54
{
55 55
  typedef concepts::Digraph Digraph;
56 56
  typedef Bfs<Digraph> BType;
57 57
  typedef Digraph::Node Node;
58 58
  typedef Digraph::Arc Arc;
59 59

	
60 60
  Digraph G;
61
  Node s, t;
61
  Node s, t, n;
62 62
  Arc e;
63
  int l;
63
  int l, i;
64 64
  bool b;
65 65
  BType::DistMap d(G);
66 66
  BType::PredMap p(G);
67 67
  Path<Digraph> pp;
68
  concepts::ReadMap<Node,bool> nm;
68 69

	
69 70
  {
70 71
    BType bfs_test(G);
72
    const BType& const_bfs_test = bfs_test;
71 73

	
72 74
    bfs_test.run(s);
73 75
    bfs_test.run(s,t);
74 76
    bfs_test.run();
75 77

	
76
    l  = bfs_test.dist(t);
77
    e  = bfs_test.predArc(t);
78
    s  = bfs_test.predNode(t);
79
    b  = bfs_test.reached(t);
80
    d  = bfs_test.distMap();
81
    p  = bfs_test.predMap();
82
    pp = bfs_test.path(t);
78
    bfs_test.init();
79
    bfs_test.addSource(s);
80
    n = bfs_test.processNextNode();
81
    n = bfs_test.processNextNode(t, b);
82
    n = bfs_test.processNextNode(nm, n);
83
    n = const_bfs_test.nextNode();
84
    b = const_bfs_test.emptyQueue();
85
    i = const_bfs_test.queueSize();
86
    
87
    bfs_test.start();
88
    bfs_test.start(t);
89
    bfs_test.start(nm);
90

	
91
    l  = const_bfs_test.dist(t);
92
    e  = const_bfs_test.predArc(t);
93
    s  = const_bfs_test.predNode(t);
94
    b  = const_bfs_test.reached(t);
95
    d  = const_bfs_test.distMap();
96
    p  = const_bfs_test.predMap();
97
    pp = const_bfs_test.path(t);
83 98
  }
84 99
  {
85 100
    BType
86 101
      ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
87 102
      ::SetDistMap<concepts::ReadWriteMap<Node,int> >
88 103
      ::SetReachedMap<concepts::ReadWriteMap<Node,bool> >
104
      ::SetStandardProcessedMap
89 105
      ::SetProcessedMap<concepts::WriteMap<Node,bool> >
90
      ::SetStandardProcessedMap
91 106
      ::Create bfs_test(G);
107
      
108
    concepts::ReadWriteMap<Node,Arc> pred_map;
109
    concepts::ReadWriteMap<Node,int> dist_map;
110
    concepts::ReadWriteMap<Node,bool> reached_map;
111
    concepts::WriteMap<Node,bool> processed_map;
112
    
113
    bfs_test
114
      .predMap(pred_map)
115
      .distMap(dist_map)
116
      .reachedMap(reached_map)
117
      .processedMap(processed_map);
92 118

	
93 119
    bfs_test.run(s);
94 120
    bfs_test.run(s,t);
95 121
    bfs_test.run();
122
    
123
    bfs_test.init();
124
    bfs_test.addSource(s);
125
    n = bfs_test.processNextNode();
126
    n = bfs_test.processNextNode(t, b);
127
    n = bfs_test.processNextNode(nm, n);
128
    n = bfs_test.nextNode();
129
    b = bfs_test.emptyQueue();
130
    i = bfs_test.queueSize();
131
    
132
    bfs_test.start();
133
    bfs_test.start(t);
134
    bfs_test.start(nm);
96 135

	
97 136
    l  = bfs_test.dist(t);
98 137
    e  = bfs_test.predArc(t);
99 138
    s  = bfs_test.predNode(t);
100 139
    b  = bfs_test.reached(t);
101 140
    pp = bfs_test.path(t);
102 141
  }
103 142
}
104 143

	
105 144
void checkBfsFunctionCompile()
106 145
{
107 146
  typedef int VType;
108 147
  typedef concepts::Digraph Digraph;
109 148
  typedef Digraph::Arc Arc;
110 149
  typedef Digraph::Node Node;
111 150

	
112 151
  Digraph g;
113 152
  bool b;
114 153
  bfs(g).run(Node());
115 154
  b=bfs(g).run(Node(),Node());
116 155
  bfs(g).run();
117 156
  bfs(g)
118 157
    .predMap(concepts::ReadWriteMap<Node,Arc>())
119 158
    .distMap(concepts::ReadWriteMap<Node,VType>())
120 159
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
121 160
    .processedMap(concepts::WriteMap<Node,bool>())
122 161
    .run(Node());
123 162
  b=bfs(g)
124 163
    .predMap(concepts::ReadWriteMap<Node,Arc>())
125 164
    .distMap(concepts::ReadWriteMap<Node,VType>())
126 165
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
127 166
    .processedMap(concepts::WriteMap<Node,bool>())
128 167
    .path(concepts::Path<Digraph>())
129 168
    .dist(VType())
130 169
    .run(Node(),Node());
131 170
  bfs(g)
132 171
    .predMap(concepts::ReadWriteMap<Node,Arc>())
133 172
    .distMap(concepts::ReadWriteMap<Node,VType>())
134 173
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
135 174
    .processedMap(concepts::WriteMap<Node,bool>())
136 175
    .run();
137 176
}
138 177

	
139 178
template <class Digraph>
140 179
void checkBfs() {
141 180
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
142 181

	
143 182
  Digraph G;
144 183
  Node s, t;
145 184

	
146 185
  std::istringstream input(test_lgf);
147 186
  digraphReader(G, input).
148 187
    node("source", s).
149 188
    node("target", t).
150 189
    run();
151 190

	
152 191
  Bfs<Digraph> bfs_test(G);
153 192
  bfs_test.run(s);
154 193

	
155 194
  check(bfs_test.dist(t)==2,"Bfs found a wrong path.");
156 195

	
157 196
  Path<Digraph> p = bfs_test.path(t);
158 197
  check(p.length()==2,"path() found a wrong path.");
159 198
  check(checkPath(G, p),"path() found a wrong path.");
160 199
  check(pathSource(G, p) == s,"path() found a wrong path.");
161 200
  check(pathTarget(G, p) == t,"path() found a wrong path.");
162 201

	
163 202

	
164 203
  for(ArcIt a(G); a!=INVALID; ++a) {
165 204
    Node u=G.source(a);
166 205
    Node v=G.target(a);
167 206
    check( !bfs_test.reached(u) ||
168 207
           (bfs_test.dist(v) <= bfs_test.dist(u)+1),
169 208
           "Wrong output. " << G.id(u) << "->" << G.id(v));
170 209
  }
171 210

	
172 211
  for(NodeIt v(G); v!=INVALID; ++v) {
173 212
    if (bfs_test.reached(v)) {
174 213
      check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree.");
175 214
      if (bfs_test.predArc(v)!=INVALID ) {
176 215
        Arc a=bfs_test.predArc(v);
177 216
        Node u=G.source(a);
178 217
        check(u==bfs_test.predNode(v),"Wrong tree.");
179 218
        check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
180 219
              "Wrong distance. Difference: "
181 220
              << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1));
182 221
      }
183 222
    }
184 223
  }
185 224

	
186 225
  {
187 226
    NullMap<Node,Arc> myPredMap;
188 227
    bfs(G).predMap(myPredMap).run(s);
189 228
  }
190 229
}
191 230

	
Ignore white space 6 line context
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-2009
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

	
21 21
#include "test_tools.h"
22 22
#include <lemon/list_graph.h>
23 23
#include <lemon/circulation.h>
24 24
#include <lemon/lgf_reader.h>
25 25
#include <lemon/concepts/digraph.h>
26 26
#include <lemon/concepts/maps.h>
27 27

	
28 28
using namespace lemon;
29 29

	
30 30
char test_lgf[] =
31 31
  "@nodes\n"
32 32
  "label\n"
33 33
  "0\n"
34 34
  "1\n"
35 35
  "2\n"
36 36
  "3\n"
37 37
  "4\n"
38 38
  "5\n"
39 39
  "@arcs\n"
40 40
  "     lcap  ucap\n"
41 41
  "0 1  2  10\n"
42 42
  "0 2  2  6\n"
43 43
  "1 3  4  7\n"
44 44
  "1 4  0  5\n"
45 45
  "2 4  1  3\n"
46 46
  "3 5  3  8\n"
47 47
  "4 5  3  7\n"
48 48
  "@attributes\n"
49 49
  "source 0\n"
50 50
  "sink   5\n";
51 51

	
52 52
void checkCirculationCompile()
53 53
{
54 54
  typedef int VType;
55 55
  typedef concepts::Digraph Digraph;
56 56

	
57 57
  typedef Digraph::Node Node;
58 58
  typedef Digraph::Arc Arc;
59 59
  typedef concepts::ReadMap<Arc,VType> CapMap;
60
  typedef concepts::ReadMap<Node,VType> DeltaMap;
60
  typedef concepts::ReadMap<Node,VType> SupplyMap;
61 61
  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
62 62
  typedef concepts::WriteMap<Node,bool> BarrierMap;
63 63

	
64 64
  typedef Elevator<Digraph, Digraph::Node> Elev;
65 65
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
66 66

	
67 67
  Digraph g;
68 68
  Node n;
69 69
  Arc a;
70 70
  CapMap lcap, ucap;
71
  DeltaMap delta;
71
  SupplyMap supply;
72 72
  FlowMap flow;
73 73
  BarrierMap bar;
74
  VType v;
75
  bool b;
74 76

	
75
  Circulation<Digraph, CapMap, CapMap, DeltaMap>
76
    ::SetFlowMap<FlowMap>
77
    ::SetElevator<Elev>
78
    ::SetStandardElevator<LinkedElev>
79
    ::Create circ_test(g,lcap,ucap,delta);
80

	
81
  circ_test.lowerCapMap(lcap);
82
  circ_test.upperCapMap(ucap);
83
  circ_test.deltaMap(delta);
84
  flow = circ_test.flowMap();
85
  circ_test.flowMap(flow);
77
  typedef Circulation<Digraph, CapMap, CapMap, SupplyMap>
78
            ::SetFlowMap<FlowMap>
79
            ::SetElevator<Elev>
80
            ::SetStandardElevator<LinkedElev>
81
            ::Create CirculationType;
82
  CirculationType circ_test(g, lcap, ucap, supply);
83
  const CirculationType& const_circ_test = circ_test;
84
   
85
  circ_test
86
    .lowerMap(lcap)
87
    .upperMap(ucap)
88
    .supplyMap(supply)
89
    .flowMap(flow);
90
  
91
  const CirculationType::Elevator& elev = const_circ_test.elevator();
92
  circ_test.elevator(const_cast<CirculationType::Elevator&>(elev));
93
  CirculationType::Tolerance tol = const_circ_test.tolerance();
94
  circ_test.tolerance(tol);
86 95

	
87 96
  circ_test.init();
88 97
  circ_test.greedyInit();
89 98
  circ_test.start();
90 99
  circ_test.run();
91 100

	
92
  circ_test.barrier(n);
93
  circ_test.barrierMap(bar);
94
  circ_test.flow(a);
101
  v = const_circ_test.flow(a);
102
  const FlowMap& fm = const_circ_test.flowMap();
103
  b = const_circ_test.barrier(n);
104
  const_circ_test.barrierMap(bar);
105
  
106
  ignore_unused_variable_warning(fm);
95 107
}
96 108

	
97 109
template <class G, class LM, class UM, class DM>
98 110
void checkCirculation(const G& g, const LM& lm, const UM& um,
99 111
                      const DM& dm, bool find)
100 112
{
101 113
  Circulation<G, LM, UM, DM> circ(g, lm, um, dm);
102 114
  bool ret = circ.run();
103 115
  if (find) {
104 116
    check(ret, "A feasible solution should have been found.");
105 117
    check(circ.checkFlow(), "The found flow is corrupt.");
106 118
    check(!circ.checkBarrier(), "A barrier should not have been found.");
107 119
  } else {
108 120
    check(!ret, "A feasible solution should not have been found.");
109 121
    check(circ.checkBarrier(), "The found barrier is corrupt.");
110 122
  }
111 123
}
112 124

	
113 125
int main (int, char*[])
114 126
{
115 127
  typedef ListDigraph Digraph;
116 128
  DIGRAPH_TYPEDEFS(Digraph);
117 129

	
118 130
  Digraph g;
119 131
  IntArcMap lo(g), up(g);
120 132
  IntNodeMap delta(g, 0);
121 133
  Node s, t;
122 134

	
123 135
  std::istringstream input(test_lgf);
124 136
  DigraphReader<Digraph>(g,input).
125 137
    arcMap("lcap", lo).
126 138
    arcMap("ucap", up).
127 139
    node("source",s).
128 140
    node("sink",t).
129 141
    run();
130 142

	
131 143
  delta[s] = 7; delta[t] = -7;
132 144
  checkCirculation(g, lo, up, delta, true);
133 145

	
134 146
  delta[s] = 13; delta[t] = -13;
135 147
  checkCirculation(g, lo, up, delta, true);
136 148

	
137 149
  delta[s] = 6; delta[t] = -6;
138 150
  checkCirculation(g, lo, up, delta, false);
139 151

	
140 152
  delta[s] = 14; delta[t] = -14;
141 153
  checkCirculation(g, lo, up, delta, false);
142 154

	
143 155
  delta[s] = 7; delta[t] = -13;
144 156
  checkCirculation(g, lo, up, delta, true);
145 157

	
146 158
  delta[s] = 5; delta[t] = -15;
147 159
  checkCirculation(g, lo, up, delta, true);
148 160

	
149 161
  delta[s] = 10; delta[t] = -11;
150 162
  checkCirculation(g, lo, up, delta, true);
151 163

	
152 164
  delta[s] = 11; delta[t] = -10;
153 165
  checkCirculation(g, lo, up, delta, false);
154 166

	
155 167
  return 0;
156 168
}
Ignore white space 192 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/counter.h>
20 20
#include <vector>
21
#include <sstream>
22

	
23
#include "test/test_tools.h"
21 24

	
22 25
using namespace lemon;
23 26

	
24 27
template <typename T>
25 28
void bubbleSort(std::vector<T>& v) {
26
  Counter op("Bubble Sort - Operations: ");
27
  Counter::NoSubCounter as(op, "Assignments: ");
28
  Counter::NoSubCounter co(op, "Comparisons: ");
29
  for (int i = v.size()-1; i > 0; --i) {
30
    for (int j = 0; j < i; ++j) {
31
      if (v[j] > v[j+1]) {
32
        T tmp = v[j];
33
        v[j] = v[j+1];
34
        v[j+1] = tmp;
35
        as += 3;
29
  std::stringstream s1, s2, s3;
30
  {
31
    Counter op("Bubble Sort - Operations: ", s1);
32
    Counter::SubCounter as(op, "Assignments: ", s2);
33
    Counter::SubCounter co(op, "Comparisons: ", s3);
34
    for (int i = v.size()-1; i > 0; --i) {
35
      for (int j = 0; j < i; ++j) {
36
        if (v[j] > v[j+1]) {
37
          T tmp = v[j];
38
          v[j] = v[j+1];
39
          v[j+1] = tmp;
40
          as += 3;
41
        }
42
        ++co;
36 43
      }
37
      ++co;
38 44
    }
39 45
  }
46
  check(s1.str() == "Bubble Sort - Operations: 102\n", "Wrong counter");
47
  check(s2.str() == "Assignments: 57\n", "Wrong subcounter");
48
  check(s3.str() == "Comparisons: 45\n", "Wrong subcounter");
40 49
}
41 50

	
42 51
template <typename T>
43 52
void insertionSort(std::vector<T>& v) {
44
  Counter op("Insertion Sort - Operations: ");
45
  Counter::NoSubCounter as(op, "Assignments: ");
46
  Counter::NoSubCounter co(op, "Comparisons: ");
47
  for (int i = 1; i < int(v.size()); ++i) {
48
    T value = v[i];
49
    ++as;
50
    int j = i;
51
    while (j > 0 && v[j-1] > value) {
52
      v[j] = v[j-1];
53
      --j;
54
      ++co; ++as;
53
  std::stringstream s1, s2, s3;
54
  {
55
    Counter op("Insertion Sort - Operations: ", s1);
56
    Counter::SubCounter as(op, "Assignments: ", s2);
57
    Counter::SubCounter co(op, "Comparisons: ", s3);
58
    for (int i = 1; i < int(v.size()); ++i) {
59
      T value = v[i];
60
      ++as;
61
      int j = i;
62
      while (j > 0 && v[j-1] > value) {
63
        v[j] = v[j-1];
64
        --j;
65
        ++co; ++as;
66
      }
67
      v[j] = value;
68
      ++as;
55 69
    }
56
    v[j] = value;
57
    ++as;
58 70
  }
71
  check(s1.str() == "Insertion Sort - Operations: 56\n", "Wrong counter");
72
  check(s2.str() == "Assignments: 37\n", "Wrong subcounter");
73
  check(s3.str() == "Comparisons: 19\n", "Wrong subcounter");
59 74
}
60 75

	
61 76
template <typename MyCounter>
62
void counterTest() {
63
  MyCounter c("Main Counter: ");
64
  c++;
65
  typename MyCounter::SubCounter d(c, "SubCounter: ");
66
  d++;
67
  typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ");
68
  e++;
69
  d+=3;
70
  c-=4;
71
  e-=2;
72
  c.reset(2);
73
  c.reset();
77
void counterTest(bool output) {
78
  std::stringstream s1, s2, s3;
79
  {
80
    MyCounter c("Main Counter: ", s1);
81
    c++;
82
    typename MyCounter::SubCounter d(c, "SubCounter: ", s2);
83
    d++;
84
    typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ", s3);
85
    e++;
86
    d+=3;
87
    c-=4;
88
    e-=2;
89
    c.reset(2);
90
    c.reset();
91
  }
92
  if (output) {
93
    check(s1.str() == "Main Counter: 3\n", "Wrong Counter");
94
    check(s2.str() == "SubCounter: 3\n", "Wrong SubCounter");
95
    check(s3.str() == "", "Wrong NoSubCounter");
96
  } else {
97
    check(s1.str() == "", "Wrong NoCounter");
98
    check(s2.str() == "", "Wrong SubCounter");
99
    check(s3.str() == "", "Wrong NoSubCounter");
100
  }
74 101
}
75 102

	
76 103
void init(std::vector<int>& v) {
77 104
  v[0] = 10; v[1] = 60; v[2] = 20; v[3] = 90; v[4] = 100;
78 105
  v[5] = 80; v[6] = 40; v[7] = 30; v[8] = 50; v[9] = 70;
79 106
}
80 107

	
81 108
int main()
82 109
{
83
  counterTest<Counter>();
84
  counterTest<NoCounter>();
110
  counterTest<Counter>(true);
111
  counterTest<NoCounter>(false);
85 112

	
86 113
  std::vector<int> x(10);
87 114
  init(x); bubbleSort(x);
88 115
  init(x); insertionSort(x);
89 116

	
90 117
  return 0;
91 118
}
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/concepts/digraph.h>
20 20
#include <lemon/smart_graph.h>
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23 23
#include <lemon/dfs.h>
24 24
#include <lemon/path.h>
25 25

	
26 26
#include "graph_test.h"
27 27
#include "test_tools.h"
28 28

	
29 29
using namespace lemon;
30 30

	
31 31
char test_lgf[] =
32 32
  "@nodes\n"
33 33
  "label\n"
34 34
  "0\n"
35 35
  "1\n"
36 36
  "2\n"
37 37
  "3\n"
38 38
  "4\n"
39 39
  "5\n"
40 40
  "6\n"
41 41
  "@arcs\n"
42 42
  "     label\n"
43 43
  "0 1  0\n"
44 44
  "1 2  1\n"
45 45
  "2 3  2\n"
46 46
  "1 4  3\n"
47 47
  "4 2  4\n"
48 48
  "4 5  5\n"
49 49
  "5 0  6\n"
50 50
  "6 3  7\n"
51 51
  "@attributes\n"
52 52
  "source 0\n"
53 53
  "target 5\n";
54 54

	
55 55
void checkDfsCompile()
56 56
{
57 57
  typedef concepts::Digraph Digraph;
58 58
  typedef Dfs<Digraph> DType;
59 59
  typedef Digraph::Node Node;
60 60
  typedef Digraph::Arc Arc;
61 61

	
62 62
  Digraph G;
63 63
  Node s, t;
64 64
  Arc e;
65
  int l;
65
  int l, i;
66 66
  bool b;
67 67
  DType::DistMap d(G);
68 68
  DType::PredMap p(G);
69 69
  Path<Digraph> pp;
70
  concepts::ReadMap<Arc,bool> am;
70 71

	
71 72
  {
72 73
    DType dfs_test(G);
74
    const DType& const_dfs_test = dfs_test;
73 75

	
74 76
    dfs_test.run(s);
75 77
    dfs_test.run(s,t);
76 78
    dfs_test.run();
77 79

	
78
    l  = dfs_test.dist(t);
79
    e  = dfs_test.predArc(t);
80
    s  = dfs_test.predNode(t);
81
    b  = dfs_test.reached(t);
82
    d  = dfs_test.distMap();
83
    p  = dfs_test.predMap();
84
    pp = dfs_test.path(t);
80
    dfs_test.init();
81
    dfs_test.addSource(s);
82
    e = dfs_test.processNextArc();
83
    e = const_dfs_test.nextArc();
84
    b = const_dfs_test.emptyQueue();
85
    i = const_dfs_test.queueSize();
86
    
87
    dfs_test.start();
88
    dfs_test.start(t);
89
    dfs_test.start(am);
90

	
91
    l  = const_dfs_test.dist(t);
92
    e  = const_dfs_test.predArc(t);
93
    s  = const_dfs_test.predNode(t);
94
    b  = const_dfs_test.reached(t);
95
    d  = const_dfs_test.distMap();
96
    p  = const_dfs_test.predMap();
97
    pp = const_dfs_test.path(t);
85 98
  }
86 99
  {
87 100
    DType
88 101
      ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
89 102
      ::SetDistMap<concepts::ReadWriteMap<Node,int> >
90 103
      ::SetReachedMap<concepts::ReadWriteMap<Node,bool> >
104
      ::SetStandardProcessedMap
91 105
      ::SetProcessedMap<concepts::WriteMap<Node,bool> >
92
      ::SetStandardProcessedMap
93 106
      ::Create dfs_test(G);
94 107

	
108
    concepts::ReadWriteMap<Node,Arc> pred_map;
109
    concepts::ReadWriteMap<Node,int> dist_map;
110
    concepts::ReadWriteMap<Node,bool> reached_map;
111
    concepts::WriteMap<Node,bool> processed_map;
112
    
113
    dfs_test
114
      .predMap(pred_map)
115
      .distMap(dist_map)
116
      .reachedMap(reached_map)
117
      .processedMap(processed_map);
118

	
95 119
    dfs_test.run(s);
96 120
    dfs_test.run(s,t);
97 121
    dfs_test.run();
122
    dfs_test.init();
123

	
124
    dfs_test.addSource(s);
125
    e = dfs_test.processNextArc();
126
    e = dfs_test.nextArc();
127
    b = dfs_test.emptyQueue();
128
    i = dfs_test.queueSize();
129
    
130
    dfs_test.start();
131
    dfs_test.start(t);
132
    dfs_test.start(am);
98 133

	
99 134
    l  = dfs_test.dist(t);
100 135
    e  = dfs_test.predArc(t);
101 136
    s  = dfs_test.predNode(t);
102 137
    b  = dfs_test.reached(t);
103 138
    pp = dfs_test.path(t);
104 139
  }
105 140
}
106 141

	
107 142
void checkDfsFunctionCompile()
108 143
{
109 144
  typedef int VType;
110 145
  typedef concepts::Digraph Digraph;
111 146
  typedef Digraph::Arc Arc;
112 147
  typedef Digraph::Node Node;
113 148

	
114 149
  Digraph g;
115 150
  bool b;
116 151
  dfs(g).run(Node());
117 152
  b=dfs(g).run(Node(),Node());
118 153
  dfs(g).run();
119 154
  dfs(g)
120 155
    .predMap(concepts::ReadWriteMap<Node,Arc>())
121 156
    .distMap(concepts::ReadWriteMap<Node,VType>())
122 157
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
123 158
    .processedMap(concepts::WriteMap<Node,bool>())
124 159
    .run(Node());
125 160
  b=dfs(g)
126 161
    .predMap(concepts::ReadWriteMap<Node,Arc>())
127 162
    .distMap(concepts::ReadWriteMap<Node,VType>())
128 163
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
129 164
    .processedMap(concepts::WriteMap<Node,bool>())
130 165
    .path(concepts::Path<Digraph>())
131 166
    .dist(VType())
132 167
    .run(Node(),Node());
133 168
  dfs(g)
134 169
    .predMap(concepts::ReadWriteMap<Node,Arc>())
135 170
    .distMap(concepts::ReadWriteMap<Node,VType>())
136 171
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
137 172
    .processedMap(concepts::WriteMap<Node,bool>())
138 173
    .run();
139 174
}
140 175

	
141 176
template <class Digraph>
142 177
void checkDfs() {
143 178
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
144 179

	
145 180
  Digraph G;
146 181
  Node s, t;
147 182

	
148 183
  std::istringstream input(test_lgf);
149 184
  digraphReader(G, input).
150 185
    node("source", s).
151 186
    node("target", t).
152 187
    run();
153 188

	
154 189
  Dfs<Digraph> dfs_test(G);
155 190
  dfs_test.run(s);
156 191

	
157 192
  Path<Digraph> p = dfs_test.path(t);
158 193
  check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
159 194
  check(checkPath(G, p),"path() found a wrong path.");
160 195
  check(pathSource(G, p) == s,"path() found a wrong path.");
161 196
  check(pathTarget(G, p) == t,"path() found a wrong path.");
162 197

	
163 198
  for(NodeIt v(G); v!=INVALID; ++v) {
164 199
    if (dfs_test.reached(v)) {
165 200
      check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
166 201
      if (dfs_test.predArc(v)!=INVALID ) {
167 202
        Arc e=dfs_test.predArc(v);
168 203
        Node u=G.source(e);
169 204
        check(u==dfs_test.predNode(v),"Wrong tree.");
170 205
        check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
171 206
              "Wrong distance. (" << dfs_test.dist(u) << "->"
172 207
              << dfs_test.dist(v) << ")");
173 208
      }
174 209
    }
175 210
  }
176 211

	
177 212
  {
178 213
    NullMap<Node,Arc> myPredMap;
179 214
    dfs(G).predMap(myPredMap).run(s);
180 215
  }
181 216
}
182 217

	
183 218
int main()
184 219
{
185 220
  checkDfs<ListDigraph>();
186 221
  checkDfs<SmartDigraph>();
187 222
  return 0;
188 223
}
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/concepts/digraph.h>
20 20
#include <lemon/list_graph.h>
21 21
#include <lemon/smart_graph.h>
22
#include <lemon/static_graph.h>
22 23
#include <lemon/full_graph.h>
23 24

	
24 25
#include "test_tools.h"
25 26
#include "graph_test.h"
26 27

	
27 28
using namespace lemon;
28 29
using namespace lemon::concepts;
29 30

	
30 31
template <class Digraph>
31 32
void checkDigraphBuild() {
32 33
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
33 34
  Digraph G;
34 35

	
35 36
  checkGraphNodeList(G, 0);
36 37
  checkGraphArcList(G, 0);
37 38

	
39
  G.reserveNode(3);
40
  G.reserveArc(4);
41

	
38 42
  Node
39 43
    n1 = G.addNode(),
40 44
    n2 = G.addNode(),
41 45
    n3 = G.addNode();
42 46
  checkGraphNodeList(G, 3);
43 47
  checkGraphArcList(G, 0);
44 48

	
45 49
  Arc a1 = G.addArc(n1, n2);
46 50
  check(G.source(a1) == n1 && G.target(a1) == n2, "Wrong arc");
47 51
  checkGraphNodeList(G, 3);
48 52
  checkGraphArcList(G, 1);
49 53

	
50 54
  checkGraphOutArcList(G, n1, 1);
51 55
  checkGraphOutArcList(G, n2, 0);
52 56
  checkGraphOutArcList(G, n3, 0);
53 57

	
54 58
  checkGraphInArcList(G, n1, 0);
55 59
  checkGraphInArcList(G, n2, 1);
56 60
  checkGraphInArcList(G, n3, 0);
57 61

	
58 62
  checkGraphConArcList(G, 1);
59 63

	
60 64
  Arc a2 = G.addArc(n2, n1),
61 65
      a3 = G.addArc(n2, n3),
62 66
      a4 = G.addArc(n2, n3);
63 67

	
64 68
  checkGraphNodeList(G, 3);
65 69
  checkGraphArcList(G, 4);
66 70

	
67 71
  checkGraphOutArcList(G, n1, 1);
68 72
  checkGraphOutArcList(G, n2, 3);
69 73
  checkGraphOutArcList(G, n3, 0);
70 74

	
71 75
  checkGraphInArcList(G, n1, 1);
72 76
  checkGraphInArcList(G, n2, 1);
73 77
  checkGraphInArcList(G, n3, 2);
74 78

	
75 79
  checkGraphConArcList(G, 4);
76 80

	
77 81
  checkNodeIds(G);
78 82
  checkArcIds(G);
79 83
  checkGraphNodeMap(G);
80 84
  checkGraphArcMap(G);
81 85
}
82 86

	
83 87
template <class Digraph>
84 88
void checkDigraphSplit() {
85 89
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
86 90

	
87 91
  Digraph G;
88 92
  Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
89 93
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
90 94
      a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
91 95

	
92 96
  Node n4 = G.split(n2);
93 97

	
94 98
  check(G.target(OutArcIt(G, n2)) == n4 &&
95 99
        G.source(InArcIt(G, n4)) == n2,
96 100
        "Wrong split.");
97 101

	
98 102
  checkGraphNodeList(G, 4);
99 103
  checkGraphArcList(G, 5);
100 104

	
101 105
  checkGraphOutArcList(G, n1, 1);
102 106
  checkGraphOutArcList(G, n2, 1);
103 107
  checkGraphOutArcList(G, n3, 0);
104 108
  checkGraphOutArcList(G, n4, 3);
105 109

	
106 110
  checkGraphInArcList(G, n1, 1);
107 111
  checkGraphInArcList(G, n2, 1);
108 112
  checkGraphInArcList(G, n3, 2);
109 113
  checkGraphInArcList(G, n4, 1);
110 114

	
111 115
  checkGraphConArcList(G, 5);
112 116
}
113 117

	
114 118
template <class Digraph>
115 119
void checkDigraphAlter() {
116 120
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
117 121

	
118 122
  Digraph G;
119 123
  Node n1 = G.addNode(), n2 = G.addNode(),
120 124
       n3 = G.addNode(), n4 = G.addNode();
121 125
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
122 126
      a3 = G.addArc(n4, n3), a4 = G.addArc(n4, n3),
123 127
      a5 = G.addArc(n2, n4);
124 128

	
125 129
  checkGraphNodeList(G, 4);
126 130
  checkGraphArcList(G, 5);
127 131

	
128 132
  // Check changeSource() and changeTarget()
129 133
  G.changeTarget(a4, n1);
130 134

	
131 135
  checkGraphNodeList(G, 4);
132 136
  checkGraphArcList(G, 5);
133 137

	
... ...
@@ -190,242 +194,369 @@
190 194
  checkGraphConArcList(G, 3);
191 195
}
192 196

	
193 197
template <class Digraph>
194 198
void checkDigraphErase() {
195 199
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
196 200

	
197 201
  Digraph G;
198 202
  Node n1 = G.addNode(), n2 = G.addNode(),
199 203
       n3 = G.addNode(), n4 = G.addNode();
200 204
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
201 205
      a3 = G.addArc(n4, n3), a4 = G.addArc(n3, n1),
202 206
      a5 = G.addArc(n2, n4);
203 207

	
204 208
  // Check arc deletion
205 209
  G.erase(a1);
206 210

	
207 211
  checkGraphNodeList(G, 4);
208 212
  checkGraphArcList(G, 4);
209 213

	
210 214
  checkGraphOutArcList(G, n1, 0);
211 215
  checkGraphOutArcList(G, n2, 1);
212 216
  checkGraphOutArcList(G, n3, 1);
213 217
  checkGraphOutArcList(G, n4, 2);
214 218

	
215 219
  checkGraphInArcList(G, n1, 2);
216 220
  checkGraphInArcList(G, n2, 0);
217 221
  checkGraphInArcList(G, n3, 1);
218 222
  checkGraphInArcList(G, n4, 1);
219 223

	
220 224
  checkGraphConArcList(G, 4);
221 225

	
222 226
  // Check node deletion
223 227
  G.erase(n4);
224 228

	
225 229
  checkGraphNodeList(G, 3);
226 230
  checkGraphArcList(G, 1);
227 231

	
228 232
  checkGraphOutArcList(G, n1, 0);
229 233
  checkGraphOutArcList(G, n2, 0);
230 234
  checkGraphOutArcList(G, n3, 1);
231 235
  checkGraphOutArcList(G, n4, 0);
232 236

	
233 237
  checkGraphInArcList(G, n1, 1);
234 238
  checkGraphInArcList(G, n2, 0);
235 239
  checkGraphInArcList(G, n3, 0);
236 240
  checkGraphInArcList(G, n4, 0);
237 241

	
238 242
  checkGraphConArcList(G, 1);
239 243
}
240 244

	
241 245

	
242 246
template <class Digraph>
243 247
void checkDigraphSnapshot() {
244 248
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
245 249

	
246 250
  Digraph G;
247 251
  Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
248 252
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
249 253
      a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
250 254

	
251 255
  typename Digraph::Snapshot snapshot(G);
252 256

	
253 257
  Node n = G.addNode();
254 258
  G.addArc(n3, n);
255 259
  G.addArc(n, n3);
256 260

	
257 261
  checkGraphNodeList(G, 4);
258 262
  checkGraphArcList(G, 6);
259 263

	
260 264
  snapshot.restore();
261 265

	
262 266
  checkGraphNodeList(G, 3);
263 267
  checkGraphArcList(G, 4);
264 268

	
265 269
  checkGraphOutArcList(G, n1, 1);
266 270
  checkGraphOutArcList(G, n2, 3);
267 271
  checkGraphOutArcList(G, n3, 0);
268 272

	
269 273
  checkGraphInArcList(G, n1, 1);
270 274
  checkGraphInArcList(G, n2, 1);
271 275
  checkGraphInArcList(G, n3, 2);
272 276

	
273 277
  checkGraphConArcList(G, 4);
274 278

	
275 279
  checkNodeIds(G);
276 280
  checkArcIds(G);
277 281
  checkGraphNodeMap(G);
278 282
  checkGraphArcMap(G);
279 283

	
280 284
  G.addNode();
281 285
  snapshot.save(G);
282 286

	
283 287
  G.addArc(G.addNode(), G.addNode());
284 288

	
285 289
  snapshot.restore();
290
  snapshot.save(G);
291

	
292
  checkGraphNodeList(G, 4);
293
  checkGraphArcList(G, 4);
294

	
295
  G.addArc(G.addNode(), G.addNode());
296

	
297
  snapshot.restore();
286 298

	
287 299
  checkGraphNodeList(G, 4);
288 300
  checkGraphArcList(G, 4);
289 301
}
290 302

	
291 303
void checkConcepts() {
292 304
  { // Checking digraph components
293 305
    checkConcept<BaseDigraphComponent, BaseDigraphComponent >();
294 306

	
295 307
    checkConcept<IDableDigraphComponent<>,
296 308
      IDableDigraphComponent<> >();
297 309

	
298 310
    checkConcept<IterableDigraphComponent<>,
299 311
      IterableDigraphComponent<> >();
300 312

	
301 313
    checkConcept<MappableDigraphComponent<>,
302 314
      MappableDigraphComponent<> >();
303 315
  }
304 316
  { // Checking skeleton digraph
305 317
    checkConcept<Digraph, Digraph>();
306 318
  }
307 319
  { // Checking ListDigraph
308 320
    checkConcept<Digraph, ListDigraph>();
309 321
    checkConcept<AlterableDigraphComponent<>, ListDigraph>();
310 322
    checkConcept<ExtendableDigraphComponent<>, ListDigraph>();
311 323
    checkConcept<ClearableDigraphComponent<>, ListDigraph>();
312 324
    checkConcept<ErasableDigraphComponent<>, ListDigraph>();
313 325
  }
314 326
  { // Checking SmartDigraph
315 327
    checkConcept<Digraph, SmartDigraph>();
316 328
    checkConcept<AlterableDigraphComponent<>, SmartDigraph>();
317 329
    checkConcept<ExtendableDigraphComponent<>, SmartDigraph>();
318 330
    checkConcept<ClearableDigraphComponent<>, SmartDigraph>();
319 331
  }
332
  { // Checking StaticDigraph
333
    checkConcept<Digraph, StaticDigraph>();
334
    checkConcept<ClearableDigraphComponent<>, StaticDigraph>();
335
  }
320 336
  { // Checking FullDigraph
321 337
    checkConcept<Digraph, FullDigraph>();
322 338
  }
323 339
}
324 340

	
325 341
template <typename Digraph>
326 342
void checkDigraphValidity() {
327 343
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
328 344
  Digraph g;
329 345

	
330 346
  Node
331 347
    n1 = g.addNode(),
332 348
    n2 = g.addNode(),
333 349
    n3 = g.addNode();
334 350

	
335 351
  Arc
336 352
    e1 = g.addArc(n1, n2),
337 353
    e2 = g.addArc(n2, n3);
338 354

	
339 355
  check(g.valid(n1), "Wrong validity check");
340 356
  check(g.valid(e1), "Wrong validity check");
341 357

	
342 358
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
343 359
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
344 360
}
345 361

	
346 362
template <typename Digraph>
347 363
void checkDigraphValidityErase() {
348 364
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
349 365
  Digraph g;
350 366

	
351 367
  Node
352 368
    n1 = g.addNode(),
353 369
    n2 = g.addNode(),
354 370
    n3 = g.addNode();
355 371

	
356 372
  Arc
357 373
    e1 = g.addArc(n1, n2),
358 374
    e2 = g.addArc(n2, n3);
359 375

	
360 376
  check(g.valid(n1), "Wrong validity check");
361 377
  check(g.valid(e1), "Wrong validity check");
362 378

	
363 379
  g.erase(n1);
364 380

	
365 381
  check(!g.valid(n1), "Wrong validity check");
366 382
  check(g.valid(n2), "Wrong validity check");
367 383
  check(g.valid(n3), "Wrong validity check");
368 384
  check(!g.valid(e1), "Wrong validity check");
369 385
  check(g.valid(e2), "Wrong validity check");
370 386

	
371 387
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
372 388
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
373 389
}
374 390

	
391
void checkStaticDigraph() {
392
  SmartDigraph g;
393
  SmartDigraph::NodeMap<StaticDigraph::Node> nref(g);
394
  SmartDigraph::ArcMap<StaticDigraph::Arc> aref(g);
395
  
396
  StaticDigraph G;
397
  
398
  checkGraphNodeList(G, 0);
399
  checkGraphArcList(G, 0);
400

	
401
  G.build(g, nref, aref);
402

	
403
  checkGraphNodeList(G, 0);
404
  checkGraphArcList(G, 0);
405

	
406
  SmartDigraph::Node
407
    n1 = g.addNode(),
408
    n2 = g.addNode(),
409
    n3 = g.addNode();
410

	
411
  G.build(g, nref, aref);
412

	
413
  checkGraphNodeList(G, 3);
414
  checkGraphArcList(G, 0);
415

	
416
  SmartDigraph::Arc a1 = g.addArc(n1, n2);
417

	
418
  G.build(g, nref, aref);
419

	
420
  check(G.source(aref[a1]) == nref[n1] && G.target(aref[a1]) == nref[n2],
421
        "Wrong arc or wrong references");
422
  checkGraphNodeList(G, 3);
423
  checkGraphArcList(G, 1);
424

	
425
  checkGraphOutArcList(G, nref[n1], 1);
426
  checkGraphOutArcList(G, nref[n2], 0);
427
  checkGraphOutArcList(G, nref[n3], 0);
428

	
429
  checkGraphInArcList(G, nref[n1], 0);
430
  checkGraphInArcList(G, nref[n2], 1);
431
  checkGraphInArcList(G, nref[n3], 0);
432

	
433
  checkGraphConArcList(G, 1);
434

	
435
  SmartDigraph::Arc
436
    a2 = g.addArc(n2, n1),
437
    a3 = g.addArc(n2, n3),
438
    a4 = g.addArc(n2, n3);
439

	
440
  digraphCopy(g, G).nodeRef(nref).run();
441

	
442
  checkGraphNodeList(G, 3);
443
  checkGraphArcList(G, 4);
444

	
445
  checkGraphOutArcList(G, nref[n1], 1);
446
  checkGraphOutArcList(G, nref[n2], 3);
447
  checkGraphOutArcList(G, nref[n3], 0);
448

	
449
  checkGraphInArcList(G, nref[n1], 1);
450
  checkGraphInArcList(G, nref[n2], 1);
451
  checkGraphInArcList(G, nref[n3], 2);
452

	
453
  checkGraphConArcList(G, 4);
454

	
455
  std::vector<std::pair<int,int> > arcs;
456
  arcs.push_back(std::make_pair(0,1));
457
  arcs.push_back(std::make_pair(0,2));
458
  arcs.push_back(std::make_pair(1,3));
459
  arcs.push_back(std::make_pair(1,2));
460
  arcs.push_back(std::make_pair(3,0));
461
  arcs.push_back(std::make_pair(3,3));
462
  arcs.push_back(std::make_pair(4,2));
463
  arcs.push_back(std::make_pair(4,3));
464
  arcs.push_back(std::make_pair(4,1));
465

	
466
  G.build(6, arcs.begin(), arcs.end());
467
  
468
  checkGraphNodeList(G, 6);
469
  checkGraphArcList(G, 9);
470

	
471
  checkGraphOutArcList(G, G.node(0), 2);
472
  checkGraphOutArcList(G, G.node(1), 2);
473
  checkGraphOutArcList(G, G.node(2), 0);
474
  checkGraphOutArcList(G, G.node(3), 2);
475
  checkGraphOutArcList(G, G.node(4), 3);
476
  checkGraphOutArcList(G, G.node(5), 0);
477

	
478
  checkGraphInArcList(G, G.node(0), 1);
479
  checkGraphInArcList(G, G.node(1), 2);
480
  checkGraphInArcList(G, G.node(2), 3);
481
  checkGraphInArcList(G, G.node(3), 3);
482
  checkGraphInArcList(G, G.node(4), 0);
483
  checkGraphInArcList(G, G.node(5), 0);
484

	
485
  checkGraphConArcList(G, 9);
486

	
487
  checkNodeIds(G);
488
  checkArcIds(G);
489
  checkGraphNodeMap(G);
490
  checkGraphArcMap(G);
491
  
492
  int n = G.nodeNum();
493
  int m = G.arcNum();
494
  check(G.index(G.node(n-1)) == n-1, "Wrong index.");
495
  check(G.index(G.arc(m-1)) == m-1, "Wrong index.");
496
}
497

	
375 498
void checkFullDigraph(int num) {
376 499
  typedef FullDigraph Digraph;
377 500
  DIGRAPH_TYPEDEFS(Digraph);
501

	
378 502
  Digraph G(num);
503
  check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
504

	
505
  G.resize(num);
506
  check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
379 507

	
380 508
  checkGraphNodeList(G, num);
381 509
  checkGraphArcList(G, num * num);
382 510

	
383 511
  for (NodeIt n(G); n != INVALID; ++n) {
384 512
    checkGraphOutArcList(G, n, num);
385 513
    checkGraphInArcList(G, n, num);
386 514
  }
387 515

	
388 516
  checkGraphConArcList(G, num * num);
389 517

	
390 518
  checkNodeIds(G);
391 519
  checkArcIds(G);
392 520
  checkGraphNodeMap(G);
393 521
  checkGraphArcMap(G);
394 522

	
395 523
  for (int i = 0; i < G.nodeNum(); ++i) {
396 524
    check(G.index(G(i)) == i, "Wrong index");
397 525
  }
398 526

	
399 527
  for (NodeIt s(G); s != INVALID; ++s) {
400 528
    for (NodeIt t(G); t != INVALID; ++t) {
401 529
      Arc a = G.arc(s, t);
402 530
      check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup");
403 531
    }
404 532
  }
405 533
}
406 534

	
407 535
void checkDigraphs() {
408 536
  { // Checking ListDigraph
409 537
    checkDigraphBuild<ListDigraph>();
410 538
    checkDigraphSplit<ListDigraph>();
411 539
    checkDigraphAlter<ListDigraph>();
412 540
    checkDigraphErase<ListDigraph>();
413 541
    checkDigraphSnapshot<ListDigraph>();
414 542
    checkDigraphValidityErase<ListDigraph>();
415 543
  }
416 544
  { // Checking SmartDigraph
417 545
    checkDigraphBuild<SmartDigraph>();
418 546
    checkDigraphSplit<SmartDigraph>();
419 547
    checkDigraphSnapshot<SmartDigraph>();
420 548
    checkDigraphValidity<SmartDigraph>();
421 549
  }
550
  { // Checking StaticDigraph
551
    checkStaticDigraph();
552
  }
422 553
  { // Checking FullDigraph
423 554
    checkFullDigraph(8);
424 555
  }
425 556
}
426 557

	
427 558
int main() {
428 559
  checkDigraphs();
429 560
  checkConcepts();
430 561
  return 0;
431 562
}
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/concepts/digraph.h>
20 20
#include <lemon/smart_graph.h>
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23 23
#include <lemon/dijkstra.h>
24 24
#include <lemon/path.h>
25 25
#include <lemon/bin_heap.h>
26 26

	
27 27
#include "graph_test.h"
28 28
#include "test_tools.h"
29 29

	
30 30
using namespace lemon;
31 31

	
32 32
char test_lgf[] =
33 33
  "@nodes\n"
34 34
  "label\n"
35 35
  "0\n"
36 36
  "1\n"
37 37
  "2\n"
38 38
  "3\n"
39 39
  "4\n"
40 40
  "@arcs\n"
41 41
  "     label length\n"
42 42
  "0 1  0     1\n"
43 43
  "1 2  1     1\n"
44 44
  "2 3  2     1\n"
45 45
  "0 3  4     5\n"
46 46
  "0 3  5     10\n"
47 47
  "0 3  6     7\n"
48 48
  "4 2  7     1\n"
49 49
  "@attributes\n"
50 50
  "source 0\n"
51 51
  "target 3\n";
52 52

	
53 53
void checkDijkstraCompile()
54 54
{
55 55
  typedef int VType;
56 56
  typedef concepts::Digraph Digraph;
57 57
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
58 58
  typedef Dijkstra<Digraph, LengthMap> DType;
59 59
  typedef Digraph::Node Node;
60 60
  typedef Digraph::Arc Arc;
61 61

	
62 62
  Digraph G;
63
  Node s, t;
63
  Node s, t, n;
64 64
  Arc e;
65 65
  VType l;
66
  int i;
66 67
  bool b;
67 68
  DType::DistMap d(G);
68 69
  DType::PredMap p(G);
69 70
  LengthMap length;
70 71
  Path<Digraph> pp;
72
  concepts::ReadMap<Node,bool> nm;
71 73

	
72 74
  {
73 75
    DType dijkstra_test(G,length);
76
    const DType& const_dijkstra_test = dijkstra_test;
74 77

	
75 78
    dijkstra_test.run(s);
76 79
    dijkstra_test.run(s,t);
77 80

	
81
    dijkstra_test.init();
82
    dijkstra_test.addSource(s);
83
    dijkstra_test.addSource(s, 1);
84
    n = dijkstra_test.processNextNode();
85
    n = const_dijkstra_test.nextNode();
86
    b = const_dijkstra_test.emptyQueue();
87
    i = const_dijkstra_test.queueSize();
88
    
89
    dijkstra_test.start();
90
    dijkstra_test.start(t);
91
    dijkstra_test.start(nm);
92

	
93
    l  = const_dijkstra_test.dist(t);
94
    e  = const_dijkstra_test.predArc(t);
95
    s  = const_dijkstra_test.predNode(t);
96
    b  = const_dijkstra_test.reached(t);
97
    b  = const_dijkstra_test.processed(t);
98
    d  = const_dijkstra_test.distMap();
99
    p  = const_dijkstra_test.predMap();
100
    pp = const_dijkstra_test.path(t);
101
    l  = const_dijkstra_test.currentDist(t);
102
  }
103
  {
104
    DType
105
      ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
106
      ::SetDistMap<concepts::ReadWriteMap<Node,VType> >
107
      ::SetStandardProcessedMap
108
      ::SetProcessedMap<concepts::WriteMap<Node,bool> >
109
      ::SetOperationTraits<DijkstraDefaultOperationTraits<VType> >
110
      ::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > >
111
      ::SetStandardHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > >
112
      ::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> >, 
113
                concepts::ReadWriteMap<Node,int> >
114
      ::Create dijkstra_test(G,length);
115

	
116
    LengthMap length_map;
117
    concepts::ReadWriteMap<Node,Arc> pred_map;
118
    concepts::ReadWriteMap<Node,VType> dist_map;
119
    concepts::WriteMap<Node,bool> processed_map;
120
    concepts::ReadWriteMap<Node,int> heap_cross_ref;
121
    BinHeap<VType, concepts::ReadWriteMap<Node,int> > heap(heap_cross_ref);
122
    
123
    dijkstra_test
124
      .lengthMap(length_map)
125
      .predMap(pred_map)
126
      .distMap(dist_map)
127
      .processedMap(processed_map)
128
      .heap(heap, heap_cross_ref);
129

	
130
    dijkstra_test.run(s);
131
    dijkstra_test.run(s,t);
132

	
133
    dijkstra_test.addSource(s);
134
    dijkstra_test.addSource(s, 1);
135
    n = dijkstra_test.processNextNode();
136
    n = dijkstra_test.nextNode();
137
    b = dijkstra_test.emptyQueue();
138
    i = dijkstra_test.queueSize();
139
    
140
    dijkstra_test.start();
141
    dijkstra_test.start(t);
142
    dijkstra_test.start(nm);
143

	
78 144
    l  = dijkstra_test.dist(t);
79 145
    e  = dijkstra_test.predArc(t);
80 146
    s  = dijkstra_test.predNode(t);
81 147
    b  = dijkstra_test.reached(t);
82
    d  = dijkstra_test.distMap();
83
    p  = dijkstra_test.predMap();
148
    b  = dijkstra_test.processed(t);
84 149
    pp = dijkstra_test.path(t);
85
  }
86
  {
87
    DType
88
      ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
89
      ::SetDistMap<concepts::ReadWriteMap<Node,VType> >
90
      ::SetProcessedMap<concepts::WriteMap<Node,bool> >
91
      ::SetStandardProcessedMap
92
      ::SetOperationTraits<DijkstraDefaultOperationTraits<VType> >
93
      ::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > >
94
      ::SetStandardHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > >
95
      ::Create dijkstra_test(G,length);
96

	
97
    dijkstra_test.run(s);
98
    dijkstra_test.run(s,t);
99

	
100
    l  = dijkstra_test.dist(t);
101
    e  = dijkstra_test.predArc(t);
102
    s  = dijkstra_test.predNode(t);
103
    b  = dijkstra_test.reached(t);
104
    pp = dijkstra_test.path(t);
150
    l  = dijkstra_test.currentDist(t);
105 151
  }
106 152

	
107 153
}
108 154

	
109 155
void checkDijkstraFunctionCompile()
110 156
{
111 157
  typedef int VType;
112 158
  typedef concepts::Digraph Digraph;
113 159
  typedef Digraph::Arc Arc;
114 160
  typedef Digraph::Node Node;
115 161
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
116 162

	
117 163
  Digraph g;
118 164
  bool b;
119 165
  dijkstra(g,LengthMap()).run(Node());
120 166
  b=dijkstra(g,LengthMap()).run(Node(),Node());
121 167
  dijkstra(g,LengthMap())
122 168
    .predMap(concepts::ReadWriteMap<Node,Arc>())
123 169
    .distMap(concepts::ReadWriteMap<Node,VType>())
124 170
    .processedMap(concepts::WriteMap<Node,bool>())
125 171
    .run(Node());
126 172
  b=dijkstra(g,LengthMap())
127 173
    .predMap(concepts::ReadWriteMap<Node,Arc>())
128 174
    .distMap(concepts::ReadWriteMap<Node,VType>())
129 175
    .processedMap(concepts::WriteMap<Node,bool>())
130 176
    .path(concepts::Path<Digraph>())
131 177
    .dist(VType())
132 178
    .run(Node(),Node());
133 179
}
134 180

	
135 181
template <class Digraph>
136 182
void checkDijkstra() {
137 183
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
138 184
  typedef typename Digraph::template ArcMap<int> LengthMap;
139 185

	
140 186
  Digraph G;
141 187
  Node s, t;
142 188
  LengthMap length(G);
143 189

	
144 190
  std::istringstream input(test_lgf);
145 191
  digraphReader(G, input).
146 192
    arcMap("length", length).
147 193
    node("source", s).
148 194
    node("target", t).
149 195
    run();
150 196

	
151 197
  Dijkstra<Digraph, LengthMap>
152 198
        dijkstra_test(G, length);
153 199
  dijkstra_test.run(s);
154 200

	
155 201
  check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
156 202

	
157 203
  Path<Digraph> p = dijkstra_test.path(t);
158 204
  check(p.length()==3,"path() found a wrong path.");
159 205
  check(checkPath(G, p),"path() found a wrong path.");
160 206
  check(pathSource(G, p) == s,"path() found a wrong path.");
161 207
  check(pathTarget(G, p) == t,"path() found a wrong path.");
162 208

	
163 209
  for(ArcIt e(G); e!=INVALID; ++e) {
164 210
    Node u=G.source(e);
165 211
    Node v=G.target(e);
166 212
    check( !dijkstra_test.reached(u) ||
167 213
           (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
168 214
           "Wrong output. dist(target)-dist(source)-arc_length=" <<
169 215
           dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
170 216
  }
171 217

	
172 218
  for(NodeIt v(G); v!=INVALID; ++v) {
173 219
    if (dijkstra_test.reached(v)) {
174 220
      check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree.");
175 221
      if (dijkstra_test.predArc(v)!=INVALID ) {
176 222
        Arc e=dijkstra_test.predArc(v);
177 223
        Node u=G.source(e);
178 224
        check(u==dijkstra_test.predNode(v),"Wrong tree.");
179 225
        check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
180 226
              "Wrong distance! Difference: " <<
181 227
              std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
182 228
      }
183 229
    }
184 230
  }
185 231

	
186 232
  {
187 233
    NullMap<Node,Arc> myPredMap;
188 234
    dijkstra(G,length).predMap(myPredMap).run(s);
189 235
  }
190 236
}
191 237

	
192 238
int main() {
193 239
  checkDijkstra<ListDigraph>();
194 240
  checkDijkstra<SmartDigraph>();
195 241
  return 0;
196 242
}
Ignore white space 6 line context
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 <vector>
21 21

	
22 22
#include <lemon/concepts/digraph.h>
23 23
#include <lemon/concepts/graph.h>
24 24
#include <lemon/concept_check.h>
25 25

	
26 26
#include <lemon/list_graph.h>
27 27

	
28 28
#include <lemon/edge_set.h>
29 29

	
30 30
#include "graph_test.h"
31 31
#include "test_tools.h"
32 32

	
33 33
using namespace lemon;
34 34

	
35 35
void checkSmartArcSet() {
36
  checkConcept<concepts::Digraph, SmartArcSet<concepts::Digraph> >();
36
  checkConcept<concepts::Digraph, SmartArcSet<ListDigraph> >();
37 37

	
38 38
  typedef ListDigraph Digraph;
39 39
  typedef SmartArcSet<Digraph> ArcSet;
40 40

	
41 41
  Digraph digraph;
42 42
  Digraph::Node
43 43
    n1 = digraph.addNode(),
44 44
    n2 = digraph.addNode();
45 45

	
46 46
  Digraph::Arc ga1 = digraph.addArc(n1, n2);
47 47

	
48 48
  ArcSet arc_set(digraph);
49 49

	
50 50
  Digraph::Arc ga2 = digraph.addArc(n2, n1);
51 51

	
52 52
  checkGraphNodeList(arc_set, 2);
53 53
  checkGraphArcList(arc_set, 0);
54 54

	
55 55
  Digraph::Node
56 56
    n3 = digraph.addNode();
57 57
  checkGraphNodeList(arc_set, 3);
58 58
  checkGraphArcList(arc_set, 0);
59 59

	
60 60
  ArcSet::Arc a1 = arc_set.addArc(n1, n2);
61 61
  check(arc_set.source(a1) == n1 && arc_set.target(a1) == n2, "Wrong arc");
62 62
  checkGraphNodeList(arc_set, 3);
63 63
  checkGraphArcList(arc_set, 1);
64 64

	
65 65
  checkGraphOutArcList(arc_set, n1, 1);
66 66
  checkGraphOutArcList(arc_set, n2, 0);
67 67
  checkGraphOutArcList(arc_set, n3, 0);
68 68

	
69 69
  checkGraphInArcList(arc_set, n1, 0);
70 70
  checkGraphInArcList(arc_set, n2, 1);
71 71
  checkGraphInArcList(arc_set, n3, 0);
72 72

	
73 73
  checkGraphConArcList(arc_set, 1);
74 74

	
75 75
  ArcSet::Arc a2 = arc_set.addArc(n2, n1),
76 76
    a3 = arc_set.addArc(n2, n3),
77 77
    a4 = arc_set.addArc(n2, n3);
78 78
  checkGraphNodeList(arc_set, 3);
79 79
  checkGraphArcList(arc_set, 4);
80 80

	
81 81
  checkGraphOutArcList(arc_set, n1, 1);
82 82
  checkGraphOutArcList(arc_set, n2, 3);
83 83
  checkGraphOutArcList(arc_set, n3, 0);
84 84

	
85 85
  checkGraphInArcList(arc_set, n1, 1);
86 86
  checkGraphInArcList(arc_set, n2, 1);
87 87
  checkGraphInArcList(arc_set, n3, 2);
88 88

	
89 89
  checkGraphConArcList(arc_set, 4);
90 90

	
91 91
  checkNodeIds(arc_set);
92 92
  checkArcIds(arc_set);
93 93
  checkGraphNodeMap(arc_set);
94 94
  checkGraphArcMap(arc_set);
95 95

	
96 96
  check(arc_set.valid(), "Wrong validity");
97 97
  digraph.erase(n1);
98 98
  check(!arc_set.valid(), "Wrong validity");
99 99
}
100 100

	
101 101
void checkListArcSet() {
102
  checkConcept<concepts::Digraph, SmartArcSet<concepts::Digraph> >();
102
  checkConcept<concepts::Digraph, SmartArcSet<ListDigraph> >();
103 103

	
104 104
  typedef ListDigraph Digraph;
105 105
  typedef ListArcSet<Digraph> ArcSet;
106 106

	
107 107
  Digraph digraph;
108 108
  Digraph::Node
109 109
    n1 = digraph.addNode(),
110 110
    n2 = digraph.addNode();
111 111

	
112 112
  Digraph::Arc ga1 = digraph.addArc(n1, n2);
113 113

	
114 114
  ArcSet arc_set(digraph);
115 115

	
116 116
  Digraph::Arc ga2 = digraph.addArc(n2, n1);
117 117

	
118 118
  checkGraphNodeList(arc_set, 2);
119 119
  checkGraphArcList(arc_set, 0);
120 120

	
121 121
  Digraph::Node
122 122
    n3 = digraph.addNode();
123 123
  checkGraphNodeList(arc_set, 3);
124 124
  checkGraphArcList(arc_set, 0);
125 125

	
126 126
  ArcSet::Arc a1 = arc_set.addArc(n1, n2);
127 127
  check(arc_set.source(a1) == n1 && arc_set.target(a1) == n2, "Wrong arc");
128 128
  checkGraphNodeList(arc_set, 3);
129 129
  checkGraphArcList(arc_set, 1);
130 130

	
131 131
  checkGraphOutArcList(arc_set, n1, 1);
132 132
  checkGraphOutArcList(arc_set, n2, 0);
133 133
  checkGraphOutArcList(arc_set, n3, 0);
134 134

	
135 135
  checkGraphInArcList(arc_set, n1, 0);
136 136
  checkGraphInArcList(arc_set, n2, 1);
137 137
  checkGraphInArcList(arc_set, n3, 0);
138 138

	
139 139
  checkGraphConArcList(arc_set, 1);
140 140

	
141 141
  ArcSet::Arc a2 = arc_set.addArc(n2, n1),
142 142
    a3 = arc_set.addArc(n2, n3),
143 143
    a4 = arc_set.addArc(n2, n3);
144 144
  checkGraphNodeList(arc_set, 3);
145 145
  checkGraphArcList(arc_set, 4);
146 146

	
147 147
  checkGraphOutArcList(arc_set, n1, 1);
148 148
  checkGraphOutArcList(arc_set, n2, 3);
149 149
  checkGraphOutArcList(arc_set, n3, 0);
150 150

	
151 151
  checkGraphInArcList(arc_set, n1, 1);
152 152
  checkGraphInArcList(arc_set, n2, 1);
153 153
  checkGraphInArcList(arc_set, n3, 2);
154 154

	
155 155
  checkGraphConArcList(arc_set, 4);
156 156

	
157 157
  checkNodeIds(arc_set);
158 158
  checkArcIds(arc_set);
159 159
  checkGraphNodeMap(arc_set);
160 160
  checkGraphArcMap(arc_set);
161 161

	
162 162
  digraph.erase(n1);
163 163

	
164 164
  checkGraphNodeList(arc_set, 2);
165 165
  checkGraphArcList(arc_set, 2);
166 166

	
167 167
  checkGraphOutArcList(arc_set, n2, 2);
168 168
  checkGraphOutArcList(arc_set, n3, 0);
169 169

	
170 170
  checkGraphInArcList(arc_set, n2, 0);
171 171
  checkGraphInArcList(arc_set, n3, 2);
172 172

	
173 173
  checkNodeIds(arc_set);
174 174
  checkArcIds(arc_set);
175 175
  checkGraphNodeMap(arc_set);
176 176
  checkGraphArcMap(arc_set);
177 177

	
178 178
  checkGraphConArcList(arc_set, 2);
179 179
}
180 180

	
181 181
void checkSmartEdgeSet() {
182
  checkConcept<concepts::Digraph, SmartEdgeSet<concepts::Digraph> >();
182
  checkConcept<concepts::Digraph, SmartEdgeSet<ListDigraph> >();
183 183

	
184 184
  typedef ListDigraph Digraph;
185 185
  typedef SmartEdgeSet<Digraph> EdgeSet;
186 186

	
187 187
  Digraph digraph;
188 188
  Digraph::Node
189 189
    n1 = digraph.addNode(),
190 190
    n2 = digraph.addNode();
191 191

	
192 192
  Digraph::Arc ga1 = digraph.addArc(n1, n2);
193 193

	
194 194
  EdgeSet edge_set(digraph);
195 195

	
196 196
  Digraph::Arc ga2 = digraph.addArc(n2, n1);
197 197

	
198 198
  checkGraphNodeList(edge_set, 2);
199 199
  checkGraphArcList(edge_set, 0);
200 200
  checkGraphEdgeList(edge_set, 0);
201 201

	
202 202
  Digraph::Node
203 203
    n3 = digraph.addNode();
204 204
  checkGraphNodeList(edge_set, 3);
205 205
  checkGraphArcList(edge_set, 0);
206 206
  checkGraphEdgeList(edge_set, 0);
207 207

	
208 208
  EdgeSet::Edge e1 = edge_set.addEdge(n1, n2);
209 209
  check((edge_set.u(e1) == n1 && edge_set.v(e1) == n2) ||
210 210
        (edge_set.v(e1) == n1 && edge_set.u(e1) == n2), "Wrong edge");
211 211
  checkGraphNodeList(edge_set, 3);
212 212
  checkGraphArcList(edge_set, 2);
213 213
  checkGraphEdgeList(edge_set, 1);
214 214

	
215 215
  checkGraphOutArcList(edge_set, n1, 1);
216 216
  checkGraphOutArcList(edge_set, n2, 1);
217 217
  checkGraphOutArcList(edge_set, n3, 0);
218 218

	
219 219
  checkGraphInArcList(edge_set, n1, 1);
220 220
  checkGraphInArcList(edge_set, n2, 1);
221 221
  checkGraphInArcList(edge_set, n3, 0);
222 222

	
223 223
  checkGraphIncEdgeList(edge_set, n1, 1);
224 224
  checkGraphIncEdgeList(edge_set, n2, 1);
225 225
  checkGraphIncEdgeList(edge_set, n3, 0);
226 226

	
227 227
  checkGraphConEdgeList(edge_set, 1);
228 228
  checkGraphConArcList(edge_set, 2);
229 229

	
230 230
  EdgeSet::Edge e2 = edge_set.addEdge(n2, n1),
231 231
    e3 = edge_set.addEdge(n2, n3),
232 232
    e4 = edge_set.addEdge(n2, n3);
233 233
  checkGraphNodeList(edge_set, 3);
234 234
  checkGraphEdgeList(edge_set, 4);
235 235

	
236 236
  checkGraphOutArcList(edge_set, n1, 2);
237 237
  checkGraphOutArcList(edge_set, n2, 4);
238 238
  checkGraphOutArcList(edge_set, n3, 2);
239 239

	
240 240
  checkGraphInArcList(edge_set, n1, 2);
241 241
  checkGraphInArcList(edge_set, n2, 4);
242 242
  checkGraphInArcList(edge_set, n3, 2);
243 243

	
244 244
  checkGraphIncEdgeList(edge_set, n1, 2);
245 245
  checkGraphIncEdgeList(edge_set, n2, 4);
246 246
  checkGraphIncEdgeList(edge_set, n3, 2);
247 247

	
248 248
  checkGraphConEdgeList(edge_set, 4);
249 249
  checkGraphConArcList(edge_set, 8);
250 250

	
251 251
  checkArcDirections(edge_set);
252 252

	
253 253
  checkNodeIds(edge_set);
254 254
  checkArcIds(edge_set);
255 255
  checkEdgeIds(edge_set);
256 256
  checkGraphNodeMap(edge_set);
257 257
  checkGraphArcMap(edge_set);
258 258
  checkGraphEdgeMap(edge_set);
259 259

	
260 260
  check(edge_set.valid(), "Wrong validity");
261 261
  digraph.erase(n1);
262 262
  check(!edge_set.valid(), "Wrong validity");
263 263
}
264 264

	
265 265
void checkListEdgeSet() {
266
  checkConcept<concepts::Digraph, ListEdgeSet<concepts::Digraph> >();
266
  checkConcept<concepts::Digraph, ListEdgeSet<ListDigraph> >();
267 267

	
268 268
  typedef ListDigraph Digraph;
269 269
  typedef ListEdgeSet<Digraph> EdgeSet;
270 270

	
271 271
  Digraph digraph;
272 272
  Digraph::Node
273 273
    n1 = digraph.addNode(),
274 274
    n2 = digraph.addNode();
275 275

	
276 276
  Digraph::Arc ga1 = digraph.addArc(n1, n2);
277 277

	
278 278
  EdgeSet edge_set(digraph);
279 279

	
280 280
  Digraph::Arc ga2 = digraph.addArc(n2, n1);
281 281

	
282 282
  checkGraphNodeList(edge_set, 2);
283 283
  checkGraphArcList(edge_set, 0);
284 284
  checkGraphEdgeList(edge_set, 0);
285 285

	
286 286
  Digraph::Node
287 287
    n3 = digraph.addNode();
288 288
  checkGraphNodeList(edge_set, 3);
289 289
  checkGraphArcList(edge_set, 0);
290 290
  checkGraphEdgeList(edge_set, 0);
291 291

	
292 292
  EdgeSet::Edge e1 = edge_set.addEdge(n1, n2);
293 293
  check((edge_set.u(e1) == n1 && edge_set.v(e1) == n2) ||
294 294
        (edge_set.v(e1) == n1 && edge_set.u(e1) == n2), "Wrong edge");
295 295
  checkGraphNodeList(edge_set, 3);
296 296
  checkGraphArcList(edge_set, 2);
297 297
  checkGraphEdgeList(edge_set, 1);
298 298

	
299 299
  checkGraphOutArcList(edge_set, n1, 1);
300 300
  checkGraphOutArcList(edge_set, n2, 1);
301 301
  checkGraphOutArcList(edge_set, n3, 0);
302 302

	
303 303
  checkGraphInArcList(edge_set, n1, 1);
304 304
  checkGraphInArcList(edge_set, n2, 1);
305 305
  checkGraphInArcList(edge_set, n3, 0);
306 306

	
307 307
  checkGraphIncEdgeList(edge_set, n1, 1);
308 308
  checkGraphIncEdgeList(edge_set, n2, 1);
309 309
  checkGraphIncEdgeList(edge_set, n3, 0);
310 310

	
311 311
  checkGraphConEdgeList(edge_set, 1);
312 312
  checkGraphConArcList(edge_set, 2);
313 313

	
314 314
  EdgeSet::Edge e2 = edge_set.addEdge(n2, n1),
315 315
    e3 = edge_set.addEdge(n2, n3),
316 316
    e4 = edge_set.addEdge(n2, n3);
317 317
  checkGraphNodeList(edge_set, 3);
318 318
  checkGraphEdgeList(edge_set, 4);
319 319

	
320 320
  checkGraphOutArcList(edge_set, n1, 2);
321 321
  checkGraphOutArcList(edge_set, n2, 4);
322 322
  checkGraphOutArcList(edge_set, n3, 2);
323 323

	
324 324
  checkGraphInArcList(edge_set, n1, 2);
325 325
  checkGraphInArcList(edge_set, n2, 4);
326 326
  checkGraphInArcList(edge_set, n3, 2);
327 327

	
328 328
  checkGraphIncEdgeList(edge_set, n1, 2);
329 329
  checkGraphIncEdgeList(edge_set, n2, 4);
330 330
  checkGraphIncEdgeList(edge_set, n3, 2);
331 331

	
332 332
  checkGraphConEdgeList(edge_set, 4);
333 333
  checkGraphConArcList(edge_set, 8);
334 334

	
335 335
  checkArcDirections(edge_set);
336 336

	
337 337
  checkNodeIds(edge_set);
338 338
  checkArcIds(edge_set);
339 339
  checkEdgeIds(edge_set);
340 340
  checkGraphNodeMap(edge_set);
341 341
  checkGraphArcMap(edge_set);
342 342
  checkGraphEdgeMap(edge_set);
343 343

	
344 344
  digraph.erase(n1);
345 345

	
346 346
  checkGraphNodeList(edge_set, 2);
347 347
  checkGraphArcList(edge_set, 4);
348 348
  checkGraphEdgeList(edge_set, 2);
349 349

	
350 350
  checkGraphOutArcList(edge_set, n2, 2);
351 351
  checkGraphOutArcList(edge_set, n3, 2);
352 352

	
353 353
  checkGraphInArcList(edge_set, n2, 2);
354 354
  checkGraphInArcList(edge_set, n3, 2);
355 355

	
356 356
  checkGraphIncEdgeList(edge_set, n2, 2);
357 357
  checkGraphIncEdgeList(edge_set, n3, 2);
358 358

	
359 359
  checkNodeIds(edge_set);
360 360
  checkArcIds(edge_set);
361 361
  checkEdgeIds(edge_set);
362 362
  checkGraphNodeMap(edge_set);
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/concepts/graph.h>
20 20
#include <lemon/list_graph.h>
21 21
#include <lemon/smart_graph.h>
22 22
#include <lemon/full_graph.h>
23 23
#include <lemon/grid_graph.h>
24 24
#include <lemon/hypercube_graph.h>
25 25

	
26 26
#include "test_tools.h"
27 27
#include "graph_test.h"
28 28

	
29 29
using namespace lemon;
30 30
using namespace lemon::concepts;
31 31

	
32 32
template <class Graph>
33 33
void checkGraphBuild() {
34 34
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
35 35

	
36 36
  Graph G;
37 37
  checkGraphNodeList(G, 0);
38 38
  checkGraphEdgeList(G, 0);
39 39
  checkGraphArcList(G, 0);
40 40

	
41
  G.reserveNode(3);
42
  G.reserveEdge(3);
43

	
41 44
  Node
42 45
    n1 = G.addNode(),
43 46
    n2 = G.addNode(),
44 47
    n3 = G.addNode();
45 48
  checkGraphNodeList(G, 3);
46 49
  checkGraphEdgeList(G, 0);
47 50
  checkGraphArcList(G, 0);
48 51

	
49 52
  Edge e1 = G.addEdge(n1, n2);
50 53
  check((G.u(e1) == n1 && G.v(e1) == n2) || (G.u(e1) == n2 && G.v(e1) == n1),
51 54
        "Wrong edge");
52 55

	
53 56
  checkGraphNodeList(G, 3);
54 57
  checkGraphEdgeList(G, 1);
55 58
  checkGraphArcList(G, 2);
56 59

	
57 60
  checkGraphIncEdgeArcLists(G, n1, 1);
58 61
  checkGraphIncEdgeArcLists(G, n2, 1);
59 62
  checkGraphIncEdgeArcLists(G, n3, 0);
60 63

	
61 64
  checkGraphConEdgeList(G, 1);
62 65
  checkGraphConArcList(G, 2);
63 66

	
64 67
  Edge e2 = G.addEdge(n2, n1),
65 68
       e3 = G.addEdge(n2, n3);
66 69

	
67 70
  checkGraphNodeList(G, 3);
68 71
  checkGraphEdgeList(G, 3);
69 72
  checkGraphArcList(G, 6);
70 73

	
71 74
  checkGraphIncEdgeArcLists(G, n1, 2);
72 75
  checkGraphIncEdgeArcLists(G, n2, 3);
73 76
  checkGraphIncEdgeArcLists(G, n3, 1);
74 77

	
75 78
  checkGraphConEdgeList(G, 3);
76 79
  checkGraphConArcList(G, 6);
77 80

	
78 81
  checkArcDirections(G);
79 82

	
80 83
  checkNodeIds(G);
81 84
  checkArcIds(G);
82 85
  checkEdgeIds(G);
83 86
  checkGraphNodeMap(G);
84 87
  checkGraphArcMap(G);
85 88
  checkGraphEdgeMap(G);
86 89
}
87 90

	
88 91
template <class Graph>
89 92
void checkGraphAlter() {
90 93
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
91 94

	
92 95
  Graph G;
93 96
  Node n1 = G.addNode(), n2 = G.addNode(),
94 97
       n3 = G.addNode(), n4 = G.addNode();
95 98
  Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1),
96 99
       e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4),
97 100
       e5 = G.addEdge(n4, n3);
98 101

	
99 102
  checkGraphNodeList(G, 4);
100 103
  checkGraphEdgeList(G, 5);
101 104
  checkGraphArcList(G, 10);
102 105

	
103 106
  // Check changeU() and changeV()
104 107
  if (G.u(e2) == n2) {
105 108
    G.changeU(e2, n3);
106 109
  } else {
107 110
    G.changeV(e2, n3);
108 111
  }
109 112

	
110 113
  checkGraphNodeList(G, 4);
111 114
  checkGraphEdgeList(G, 5);
112 115
  checkGraphArcList(G, 10);
113 116

	
114 117
  checkGraphIncEdgeArcLists(G, n1, 3);
115 118
  checkGraphIncEdgeArcLists(G, n2, 2);
116 119
  checkGraphIncEdgeArcLists(G, n3, 3);
117 120
  checkGraphIncEdgeArcLists(G, n4, 2);
118 121

	
119 122
  checkGraphConEdgeList(G, 5);
120 123
  checkGraphConArcList(G, 10);
121 124

	
122 125
  if (G.u(e2) == n1) {
123 126
    G.changeU(e2, n2);
124 127
  } else {
125 128
    G.changeV(e2, n2);
126 129
  }
127 130

	
128 131
  checkGraphNodeList(G, 4);
129 132
  checkGraphEdgeList(G, 5);
130 133
  checkGraphArcList(G, 10);
131 134

	
132 135
  checkGraphIncEdgeArcLists(G, n1, 2);
133 136
  checkGraphIncEdgeArcLists(G, n2, 3);
134 137
  checkGraphIncEdgeArcLists(G, n3, 3);
135 138
  checkGraphIncEdgeArcLists(G, n4, 2);
136 139

	
... ...
@@ -163,407 +166,432 @@
163 166
  checkGraphConEdgeList(G, 3);
164 167
  checkGraphConArcList(G, 6);
165 168
}
166 169

	
167 170
template <class Graph>
168 171
void checkGraphErase() {
169 172
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
170 173

	
171 174
  Graph G;
172 175
  Node n1 = G.addNode(), n2 = G.addNode(),
173 176
       n3 = G.addNode(), n4 = G.addNode();
174 177
  Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1),
175 178
       e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4),
176 179
       e5 = G.addEdge(n4, n3);
177 180

	
178 181
  // Check edge deletion
179 182
  G.erase(e2);
180 183

	
181 184
  checkGraphNodeList(G, 4);
182 185
  checkGraphEdgeList(G, 4);
183 186
  checkGraphArcList(G, 8);
184 187

	
185 188
  checkGraphIncEdgeArcLists(G, n1, 2);
186 189
  checkGraphIncEdgeArcLists(G, n2, 2);
187 190
  checkGraphIncEdgeArcLists(G, n3, 2);
188 191
  checkGraphIncEdgeArcLists(G, n4, 2);
189 192

	
190 193
  checkGraphConEdgeList(G, 4);
191 194
  checkGraphConArcList(G, 8);
192 195

	
193 196
  // Check node deletion
194 197
  G.erase(n3);
195 198

	
196 199
  checkGraphNodeList(G, 3);
197 200
  checkGraphEdgeList(G, 2);
198 201
  checkGraphArcList(G, 4);
199 202

	
200 203
  checkGraphIncEdgeArcLists(G, n1, 2);
201 204
  checkGraphIncEdgeArcLists(G, n2, 1);
202 205
  checkGraphIncEdgeArcLists(G, n4, 1);
203 206

	
204 207
  checkGraphConEdgeList(G, 2);
205 208
  checkGraphConArcList(G, 4);
206 209
}
207 210

	
208 211

	
209 212
template <class Graph>
210 213
void checkGraphSnapshot() {
211 214
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
212 215

	
213 216
  Graph G;
214 217
  Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
215 218
  Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1),
216 219
       e3 = G.addEdge(n2, n3);
217 220

	
218 221
  checkGraphNodeList(G, 3);
219 222
  checkGraphEdgeList(G, 3);
220 223
  checkGraphArcList(G, 6);
221 224

	
222 225
  typename Graph::Snapshot snapshot(G);
223 226

	
224 227
  Node n = G.addNode();
225 228
  G.addEdge(n3, n);
226 229
  G.addEdge(n, n3);
227 230
  G.addEdge(n3, n2);
228 231

	
229 232
  checkGraphNodeList(G, 4);
230 233
  checkGraphEdgeList(G, 6);
231 234
  checkGraphArcList(G, 12);
232 235

	
233 236
  snapshot.restore();
234 237

	
235 238
  checkGraphNodeList(G, 3);
236 239
  checkGraphEdgeList(G, 3);
237 240
  checkGraphArcList(G, 6);
238 241

	
239 242
  checkGraphIncEdgeArcLists(G, n1, 2);
240 243
  checkGraphIncEdgeArcLists(G, n2, 3);
241 244
  checkGraphIncEdgeArcLists(G, n3, 1);
242 245

	
243 246
  checkGraphConEdgeList(G, 3);
244 247
  checkGraphConArcList(G, 6);
245 248

	
246 249
  checkNodeIds(G);
247 250
  checkEdgeIds(G);
248 251
  checkArcIds(G);
249 252
  checkGraphNodeMap(G);
250 253
  checkGraphEdgeMap(G);
251 254
  checkGraphArcMap(G);
252 255

	
253 256
  G.addNode();
254 257
  snapshot.save(G);
255 258

	
256 259
  G.addEdge(G.addNode(), G.addNode());
257 260

	
258 261
  snapshot.restore();
262
  snapshot.save(G);
263

	
264
  checkGraphNodeList(G, 4);
265
  checkGraphEdgeList(G, 3);
266
  checkGraphArcList(G, 6);
267
  
268
  G.addEdge(G.addNode(), G.addNode());
269

	
270
  snapshot.restore();
259 271

	
260 272
  checkGraphNodeList(G, 4);
261 273
  checkGraphEdgeList(G, 3);
262 274
  checkGraphArcList(G, 6);
263 275
}
264 276

	
265 277
void checkFullGraph(int num) {
266 278
  typedef FullGraph Graph;
267 279
  GRAPH_TYPEDEFS(Graph);
268 280

	
269 281
  Graph G(num);
282
  check(G.nodeNum() == num && G.edgeNum() == num * (num - 1) / 2,
283
        "Wrong size");
284

	
285
  G.resize(num);
286
  check(G.nodeNum() == num && G.edgeNum() == num * (num - 1) / 2,
287
        "Wrong size");
288

	
270 289
  checkGraphNodeList(G, num);
271 290
  checkGraphEdgeList(G, num * (num - 1) / 2);
272 291

	
273 292
  for (NodeIt n(G); n != INVALID; ++n) {
274 293
    checkGraphOutArcList(G, n, num - 1);
275 294
    checkGraphInArcList(G, n, num - 1);
276 295
    checkGraphIncEdgeList(G, n, num - 1);
277 296
  }
278 297

	
279 298
  checkGraphConArcList(G, num * (num - 1));
280 299
  checkGraphConEdgeList(G, num * (num - 1) / 2);
281 300

	
282 301
  checkArcDirections(G);
283 302

	
284 303
  checkNodeIds(G);
285 304
  checkArcIds(G);
286 305
  checkEdgeIds(G);
287 306
  checkGraphNodeMap(G);
288 307
  checkGraphArcMap(G);
289 308
  checkGraphEdgeMap(G);
290 309

	
291 310

	
292 311
  for (int i = 0; i < G.nodeNum(); ++i) {
293 312
    check(G.index(G(i)) == i, "Wrong index");
294 313
  }
295 314

	
296 315
  for (NodeIt u(G); u != INVALID; ++u) {
297 316
    for (NodeIt v(G); v != INVALID; ++v) {
298 317
      Edge e = G.edge(u, v);
299 318
      Arc a = G.arc(u, v);
300 319
      if (u == v) {
301 320
        check(e == INVALID, "Wrong edge lookup");
302 321
        check(a == INVALID, "Wrong arc lookup");
303 322
      } else {
304 323
        check((G.u(e) == u && G.v(e) == v) ||
305 324
              (G.u(e) == v && G.v(e) == u), "Wrong edge lookup");
306 325
        check(G.source(a) == u && G.target(a) == v, "Wrong arc lookup");
307 326
      }
308 327
    }
309 328
  }
310 329
}
311 330

	
312 331
void checkConcepts() {
313 332
  { // Checking graph components
314 333
    checkConcept<BaseGraphComponent, BaseGraphComponent >();
315 334

	
316 335
    checkConcept<IDableGraphComponent<>,
317 336
      IDableGraphComponent<> >();
318 337

	
319 338
    checkConcept<IterableGraphComponent<>,
320 339
      IterableGraphComponent<> >();
321 340

	
322 341
    checkConcept<MappableGraphComponent<>,
323 342
      MappableGraphComponent<> >();
324 343
  }
325 344
  { // Checking skeleton graph
326 345
    checkConcept<Graph, Graph>();
327 346
  }
328 347
  { // Checking ListGraph
329 348
    checkConcept<Graph, ListGraph>();
330 349
    checkConcept<AlterableGraphComponent<>, ListGraph>();
331 350
    checkConcept<ExtendableGraphComponent<>, ListGraph>();
332 351
    checkConcept<ClearableGraphComponent<>, ListGraph>();
333 352
    checkConcept<ErasableGraphComponent<>, ListGraph>();
334 353
  }
335 354
  { // Checking SmartGraph
336 355
    checkConcept<Graph, SmartGraph>();
337 356
    checkConcept<AlterableGraphComponent<>, SmartGraph>();
338 357
    checkConcept<ExtendableGraphComponent<>, SmartGraph>();
339 358
    checkConcept<ClearableGraphComponent<>, SmartGraph>();
340 359
  }
341 360
  { // Checking FullGraph
342 361
    checkConcept<Graph, FullGraph>();
343 362
  }
344 363
  { // Checking GridGraph
345 364
    checkConcept<Graph, GridGraph>();
346 365
  }
347 366
  { // Checking HypercubeGraph
348 367
    checkConcept<Graph, HypercubeGraph>();
349 368
  }
350 369
}
351 370

	
352 371
template <typename Graph>
353 372
void checkGraphValidity() {
354 373
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
355 374
  Graph g;
356 375

	
357 376
  Node
358 377
    n1 = g.addNode(),
359 378
    n2 = g.addNode(),
360 379
    n3 = g.addNode();
361 380

	
362 381
  Edge
363 382
    e1 = g.addEdge(n1, n2),
364 383
    e2 = g.addEdge(n2, n3);
365 384

	
366 385
  check(g.valid(n1), "Wrong validity check");
367 386
  check(g.valid(e1), "Wrong validity check");
368 387
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
369 388

	
370 389
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
371 390
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
372 391
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
373 392
}
374 393

	
375 394
template <typename Graph>
376 395
void checkGraphValidityErase() {
377 396
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
378 397
  Graph g;
379 398

	
380 399
  Node
381 400
    n1 = g.addNode(),
382 401
    n2 = g.addNode(),
383 402
    n3 = g.addNode();
384 403

	
385 404
  Edge
386 405
    e1 = g.addEdge(n1, n2),
387 406
    e2 = g.addEdge(n2, n3);
388 407

	
389 408
  check(g.valid(n1), "Wrong validity check");
390 409
  check(g.valid(e1), "Wrong validity check");
391 410
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
392 411

	
393 412
  g.erase(n1);
394 413

	
395 414
  check(!g.valid(n1), "Wrong validity check");
396 415
  check(g.valid(n2), "Wrong validity check");
397 416
  check(g.valid(n3), "Wrong validity check");
398 417
  check(!g.valid(e1), "Wrong validity check");
399 418
  check(g.valid(e2), "Wrong validity check");
400 419

	
401 420
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
402 421
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
403 422
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
404 423
}
405 424

	
406 425
void checkGridGraph(int width, int height) {
407 426
  typedef GridGraph Graph;
408 427
  GRAPH_TYPEDEFS(Graph);
409 428
  Graph G(width, height);
410 429

	
411 430
  check(G.width() == width, "Wrong column number");
412 431
  check(G.height() == height, "Wrong row number");
413 432

	
433
  G.resize(width, height);
434
  check(G.width() == width, "Wrong column number");
435
  check(G.height() == height, "Wrong row number");
436

	
414 437
  for (int i = 0; i < width; ++i) {
415 438
    for (int j = 0; j < height; ++j) {
416 439
      check(G.col(G(i, j)) == i, "Wrong column");
417 440
      check(G.row(G(i, j)) == j, "Wrong row");
418 441
      check(G.pos(G(i, j)).x == i, "Wrong column");
419 442
      check(G.pos(G(i, j)).y == j, "Wrong row");
420 443
    }
421 444
  }
422 445

	
423 446
  for (int j = 0; j < height; ++j) {
424 447
    for (int i = 0; i < width - 1; ++i) {
425 448
      check(G.source(G.right(G(i, j))) == G(i, j), "Wrong right");
426 449
      check(G.target(G.right(G(i, j))) == G(i + 1, j), "Wrong right");
427 450
    }
428 451
    check(G.right(G(width - 1, j)) == INVALID, "Wrong right");
429 452
  }
430 453

	
431 454
  for (int j = 0; j < height; ++j) {
432 455
    for (int i = 1; i < width; ++i) {
433 456
      check(G.source(G.left(G(i, j))) == G(i, j), "Wrong left");
434 457
      check(G.target(G.left(G(i, j))) == G(i - 1, j), "Wrong left");
435 458
    }
436 459
    check(G.left(G(0, j)) == INVALID, "Wrong left");
437 460
  }
438 461

	
439 462
  for (int i = 0; i < width; ++i) {
440 463
    for (int j = 0; j < height - 1; ++j) {
441 464
      check(G.source(G.up(G(i, j))) == G(i, j), "Wrong up");
442 465
      check(G.target(G.up(G(i, j))) == G(i, j + 1), "Wrong up");
443 466
    }
444 467
    check(G.up(G(i, height - 1)) == INVALID, "Wrong up");
445 468
  }
446 469

	
447 470
  for (int i = 0; i < width; ++i) {
448 471
    for (int j = 1; j < height; ++j) {
449 472
      check(G.source(G.down(G(i, j))) == G(i, j), "Wrong down");
450 473
      check(G.target(G.down(G(i, j))) == G(i, j - 1), "Wrong down");
451 474
    }
452 475
    check(G.down(G(i, 0)) == INVALID, "Wrong down");
453 476
  }
454 477

	
455 478
  checkGraphNodeList(G, width * height);
456 479
  checkGraphEdgeList(G, width * (height - 1) + (width - 1) * height);
457 480
  checkGraphArcList(G, 2 * (width * (height - 1) + (width - 1) * height));
458 481

	
459 482
  for (NodeIt n(G); n != INVALID; ++n) {
460 483
    int nb = 4;
461 484
    if (G.col(n) == 0) --nb;
462 485
    if (G.col(n) == width - 1) --nb;
463 486
    if (G.row(n) == 0) --nb;
464 487
    if (G.row(n) == height - 1) --nb;
465 488

	
466 489
    checkGraphOutArcList(G, n, nb);
467 490
    checkGraphInArcList(G, n, nb);
468 491
    checkGraphIncEdgeList(G, n, nb);
469 492
  }
470 493

	
471 494
  checkArcDirections(G);
472 495

	
473 496
  checkGraphConArcList(G, 2 * (width * (height - 1) + (width - 1) * height));
474 497
  checkGraphConEdgeList(G, width * (height - 1) + (width - 1) * height);
475 498

	
476 499
  checkNodeIds(G);
477 500
  checkArcIds(G);
478 501
  checkEdgeIds(G);
479 502
  checkGraphNodeMap(G);
480 503
  checkGraphArcMap(G);
481 504
  checkGraphEdgeMap(G);
482 505

	
483 506
}
484 507

	
485 508
void checkHypercubeGraph(int dim) {
486 509
  GRAPH_TYPEDEFS(HypercubeGraph);
487 510

	
488 511
  HypercubeGraph G(dim);
512
  check(G.dimension() == dim, "Wrong dimension");
513

	
514
  G.resize(dim);
515
  check(G.dimension() == dim, "Wrong dimension");
516
  
489 517
  checkGraphNodeList(G, 1 << dim);
490 518
  checkGraphEdgeList(G, dim * (1 << (dim-1)));
491 519
  checkGraphArcList(G, dim * (1 << dim));
492 520

	
493 521
  Node n = G.nodeFromId(dim);
494 522

	
495 523
  for (NodeIt n(G); n != INVALID; ++n) {
496 524
    checkGraphIncEdgeList(G, n, dim);
497 525
    for (IncEdgeIt e(G, n); e != INVALID; ++e) {
498 526
      check( (G.u(e) == n &&
499 527
              G.id(G.v(e)) == (G.id(n) ^ (1 << G.dimension(e)))) ||
500 528
             (G.v(e) == n &&
501 529
              G.id(G.u(e)) == (G.id(n) ^ (1 << G.dimension(e)))),
502 530
             "Wrong edge or wrong dimension");
503 531
    }
504 532

	
505 533
    checkGraphOutArcList(G, n, dim);
506 534
    for (OutArcIt a(G, n); a != INVALID; ++a) {
507 535
      check(G.source(a) == n &&
508 536
            G.id(G.target(a)) == (G.id(n) ^ (1 << G.dimension(a))),
509 537
            "Wrong arc or wrong dimension");
510 538
    }
511 539

	
512 540
    checkGraphInArcList(G, n, dim);
513 541
    for (InArcIt a(G, n); a != INVALID; ++a) {
514 542
      check(G.target(a) == n &&
515 543
            G.id(G.source(a)) == (G.id(n) ^ (1 << G.dimension(a))),
516 544
            "Wrong arc or wrong dimension");
517 545
    }
518 546
  }
519 547

	
520 548
  checkGraphConArcList(G, (1 << dim) * dim);
521 549
  checkGraphConEdgeList(G, dim * (1 << (dim-1)));
522 550

	
523 551
  checkArcDirections(G);
524 552

	
525 553
  checkNodeIds(G);
526 554
  checkArcIds(G);
527 555
  checkEdgeIds(G);
528 556
  checkGraphNodeMap(G);
529 557
  checkGraphArcMap(G);
530 558
  checkGraphEdgeMap(G);
531 559
}
532 560

	
533 561
void checkGraphs() {
534 562
  { // Checking ListGraph
535 563
    checkGraphBuild<ListGraph>();
536 564
    checkGraphAlter<ListGraph>();
537 565
    checkGraphErase<ListGraph>();
538 566
    checkGraphSnapshot<ListGraph>();
539 567
    checkGraphValidityErase<ListGraph>();
540 568
  }
541 569
  { // Checking SmartGraph
542 570
    checkGraphBuild<SmartGraph>();
543 571
    checkGraphSnapshot<SmartGraph>();
544 572
    checkGraphValidity<SmartGraph>();
545 573
  }
546 574
  { // Checking FullGraph
547 575
    checkFullGraph(7);
548 576
    checkFullGraph(8);
549 577
  }
550 578
  { // Checking GridGraph
551 579
    checkGridGraph(5, 8);
552 580
    checkGridGraph(8, 5);
553 581
    checkGridGraph(5, 5);
554 582
    checkGridGraph(0, 0);
555 583
    checkGridGraph(1, 1);
556 584
  }
557 585
  { // Checking HypercubeGraph
558 586
    checkHypercubeGraph(1);
559 587
    checkHypercubeGraph(2);
560 588
    checkHypercubeGraph(3);
561 589
    checkHypercubeGraph(4);
562 590
  }
563 591
}
564 592

	
565 593
int main() {
566 594
  checkConcepts();
567 595
  checkGraphs();
568 596
  return 0;
569 597
}
Ignore white space 6 line context
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-2009
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 <cstdlib>
20 20
#include <ctime>
21 21

	
22 22
#include <lemon/random.h>
23 23
#include <lemon/list_graph.h>
24 24
#include <lemon/smart_graph.h>
25 25
#include <lemon/maps.h>
26 26

	
27 27
#include "graph_test.h"
28 28
#include "test_tools.h"
29 29

	
30 30
using namespace lemon;
31 31

	
32 32
template <typename Digraph>
33 33
void checkFindArcs() {
34 34
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
35 35

	
36 36
  {
37 37
    Digraph digraph;
38 38
    for (int i = 0; i < 10; ++i) {
39 39
      digraph.addNode();
40 40
    }
41
    DescriptorMap<Digraph, Node> nodes(digraph);
42
    typename DescriptorMap<Digraph, Node>::InverseMap invNodes(nodes);
41
    RangeIdMap<Digraph, Node> nodes(digraph);
42
    typename RangeIdMap<Digraph, Node>::InverseMap invNodes(nodes);
43 43
    for (int i = 0; i < 100; ++i) {
44 44
      int src = rnd[invNodes.size()];
45 45
      int trg = rnd[invNodes.size()];
46 46
      digraph.addArc(invNodes[src], invNodes[trg]);
47 47
    }
48 48
    typename Digraph::template ArcMap<bool> found(digraph, false);
49
    DescriptorMap<Digraph, Arc> arcs(digraph);
49
    RangeIdMap<Digraph, Arc> arcs(digraph);
50 50
    for (NodeIt src(digraph); src != INVALID; ++src) {
51 51
      for (NodeIt trg(digraph); trg != INVALID; ++trg) {
52 52
        for (ConArcIt<Digraph> con(digraph, src, trg); con != INVALID; ++con) {
53 53
          check(digraph.source(con) == src, "Wrong source.");
54 54
          check(digraph.target(con) == trg, "Wrong target.");
55 55
          check(found[con] == false, "The arc found already.");
56 56
          found[con] = true;
57 57
        }
58 58
      }
59 59
    }
60 60
    for (ArcIt it(digraph); it != INVALID; ++it) {
61 61
      check(found[it] == true, "The arc is not found.");
62 62
    }
63 63
  }
64 64

	
65 65
  {
66 66
    int num = 5;
67 67
    Digraph fg;
68 68
    std::vector<Node> nodes;
69 69
    for (int i = 0; i < num; ++i) {
70 70
      nodes.push_back(fg.addNode());
71 71
    }
72 72
    for (int i = 0; i < num * num; ++i) {
73 73
      fg.addArc(nodes[i / num], nodes[i % num]);
74 74
    }
75 75
    check(countNodes(fg) == num, "Wrong node number.");
76 76
    check(countArcs(fg) == num*num, "Wrong arc number.");
77 77
    for (NodeIt src(fg); src != INVALID; ++src) {
78 78
      for (NodeIt trg(fg); trg != INVALID; ++trg) {
79 79
        ConArcIt<Digraph> con(fg, src, trg);
80 80
        check(con != INVALID, "There is no connecting arc.");
81 81
        check(fg.source(con) == src, "Wrong source.");
82 82
        check(fg.target(con) == trg, "Wrong target.");
83 83
        check(++con == INVALID, "There is more connecting arc.");
84 84
      }
85 85
    }
86 86
    ArcLookUp<Digraph> al1(fg);
87 87
    DynArcLookUp<Digraph> al2(fg);
88 88
    AllArcLookUp<Digraph> al3(fg);
89 89
    for (NodeIt src(fg); src != INVALID; ++src) {
90 90
      for (NodeIt trg(fg); trg != INVALID; ++trg) {
91 91
        Arc con1 = al1(src, trg);
92 92
        Arc con2 = al2(src, trg);
93 93
        Arc con3 = al3(src, trg);
94 94
        Arc con4 = findArc(fg, src, trg);
95 95
        check(con1 == con2 && con2 == con3 && con3 == con4,
96 96
              "Different results.")
97 97
        check(con1 != INVALID, "There is no connecting arc.");
98 98
        check(fg.source(con1) == src, "Wrong source.");
99 99
        check(fg.target(con1) == trg, "Wrong target.");
100 100
        check(al3(src, trg, con3) == INVALID,
101 101
              "There is more connecting arc.");
102 102
        check(findArc(fg, src, trg, con4) == INVALID,
103 103
              "There is more connecting arc.");
104 104
      }
105 105
    }
106 106
  }
107 107
}
108 108

	
109 109
template <typename Graph>
110 110
void checkFindEdges() {
111 111
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
112 112
  Graph graph;
113 113
  for (int i = 0; i < 10; ++i) {
114 114
    graph.addNode();
115 115
  }
116
  DescriptorMap<Graph, Node> nodes(graph);
117
  typename DescriptorMap<Graph, Node>::InverseMap invNodes(nodes);
116
  RangeIdMap<Graph, Node> nodes(graph);
117
  typename RangeIdMap<Graph, Node>::InverseMap invNodes(nodes);
118 118
  for (int i = 0; i < 100; ++i) {
119 119
    int src = rnd[invNodes.size()];
120 120
    int trg = rnd[invNodes.size()];
121 121
    graph.addEdge(invNodes[src], invNodes[trg]);
122 122
  }
123 123
  typename Graph::template EdgeMap<int> found(graph, 0);
124
  DescriptorMap<Graph, Edge> edges(graph);
124
  RangeIdMap<Graph, Edge> edges(graph);
125 125
  for (NodeIt src(graph); src != INVALID; ++src) {
126 126
    for (NodeIt trg(graph); trg != INVALID; ++trg) {
127 127
      for (ConEdgeIt<Graph> con(graph, src, trg); con != INVALID; ++con) {
128 128
        check( (graph.u(con) == src && graph.v(con) == trg) ||
129 129
               (graph.v(con) == src && graph.u(con) == trg),
130 130
               "Wrong end nodes.");
131 131
        ++found[con];
132 132
        check(found[con] <= 2, "The edge found more than twice.");
133 133
      }
134 134
    }
135 135
  }
136 136
  for (EdgeIt it(graph); it != INVALID; ++it) {
137 137
    check( (graph.u(it) != graph.v(it) && found[it] == 2) ||
138 138
           (graph.u(it) == graph.v(it) && found[it] == 1),
139 139
           "The edge is not found correctly.");
140 140
  }
141 141
}
142 142

	
143 143
template <class Digraph>
144 144
void checkDeg()
145 145
{
146 146
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
147 147

	
148 148
  const int nodeNum = 10;
149 149
  const int arcNum = 100;
150 150
  Digraph digraph;
151 151
  InDegMap<Digraph> inDeg(digraph);
152 152
  OutDegMap<Digraph> outDeg(digraph);
153 153
  std::vector<Node> nodes(nodeNum);
154 154
  for (int i = 0; i < nodeNum; ++i) {
155 155
    nodes[i] = digraph.addNode();
156 156
  }
157 157
  std::vector<Arc> arcs(arcNum);
158 158
  for (int i = 0; i < arcNum; ++i) {
159 159
    arcs[i] = digraph.addArc(nodes[rnd[nodeNum]], nodes[rnd[nodeNum]]);
160 160
  }
161 161
  for (int i = 0; i < nodeNum; ++i) {
162 162
    check(inDeg[nodes[i]] == countInArcs(digraph, nodes[i]),
163 163
          "Wrong in degree map");
164 164
  }
165 165
  for (int i = 0; i < nodeNum; ++i) {
166 166
    check(outDeg[nodes[i]] == countOutArcs(digraph, nodes[i]),
167 167
          "Wrong out degree map");
168 168
  }
169 169
}
170 170

	
171 171
template <class Digraph>
172 172
void checkSnapDeg()
173 173
{
174 174
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
175 175

	
176 176
  Digraph g;
177 177
  Node n1=g.addNode();
178 178
  Node n2=g.addNode();
179 179

	
180 180
  InDegMap<Digraph> ind(g);
181 181

	
182 182
  g.addArc(n1,n2);
183 183

	
184 184
  typename Digraph::Snapshot snap(g);
185 185

	
186 186
  OutDegMap<Digraph> outd(g);
187 187

	
188 188
  check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
189 189
  check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
190 190

	
191 191
  g.addArc(n1,n2);
192 192
  g.addArc(n2,n1);
193 193

	
194 194
  check(ind[n1]==1 && ind[n2]==2, "Wrong InDegMap value.");
195 195
  check(outd[n1]==2 && outd[n2]==1, "Wrong OutDegMap value.");
196 196

	
197 197
  snap.restore();
198 198

	
199 199
  check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
200 200
  check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
201 201
}
202 202

	
203 203
int main() {
204 204
  // Checking ConArcIt, ConEdgeIt, ArcLookUp, AllArcLookUp, and DynArcLookUp
205 205
  checkFindArcs<ListDigraph>();
206 206
  checkFindArcs<SmartDigraph>();
207 207
  checkFindEdges<ListGraph>();
208 208
  checkFindEdges<SmartGraph>();
209 209

	
210 210
  // Checking In/OutDegMap (and Snapshot feature)
211 211
  checkDeg<ListDigraph>();
212 212
  checkDeg<SmartDigraph>();
213 213
  checkSnapDeg<ListDigraph>();
214 214
  checkSnapDeg<SmartDigraph>();
215 215

	
216 216
  return 0;
217 217
}
Ignore white space 6 line context
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-2009
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 <sstream>
20 20

	
21 21
#include <lemon/smart_graph.h>
22
#include <lemon/adaptors.h>
23
#include <lemon/concepts/digraph.h>
24
#include <lemon/concepts/maps.h>
25
#include <lemon/lgf_reader.h>
22 26
#include <lemon/hao_orlin.h>
23 27

	
24
#include <lemon/lgf_reader.h>
25 28
#include "test_tools.h"
26 29

	
27 30
using namespace lemon;
28 31
using namespace std;
29 32

	
30 33
const std::string lgf =
31 34
  "@nodes\n"
32 35
  "label\n"
33 36
  "0\n"
34 37
  "1\n"
35 38
  "2\n"
36 39
  "3\n"
37 40
  "4\n"
38 41
  "5\n"
39 42
  "@edges\n"
40
  "     label  capacity\n"
41
  "0 1  0      2\n"
42
  "1 2  1      2\n"
43
  "2 0  2      2\n"
44
  "3 4  3      2\n"
45
  "4 5  4      2\n"
46
  "5 3  5      2\n"
47
  "2 3  6      3\n";
43
  "     cap1 cap2 cap3\n"
44
  "0 1  1    1    1   \n"
45
  "0 2  2    2    4   \n"
46
  "1 2  4    4    4   \n"
47
  "3 4  1    1    1   \n"
48
  "3 5  2    2    4   \n"
49
  "4 5  4    4    4   \n"
50
  "5 4  4    4    4   \n"
51
  "2 3  1    6    6   \n"
52
  "4 0  1    6    6   \n";
53

	
54
void checkHaoOrlinCompile()
55
{
56
  typedef int Value;
57
  typedef concepts::Digraph Digraph;
58

	
59
  typedef Digraph::Node Node;
60
  typedef Digraph::Arc Arc;
61
  typedef concepts::ReadMap<Arc, Value> CapMap;
62
  typedef concepts::WriteMap<Node, bool> CutMap;
63

	
64
  Digraph g;
65
  Node n;
66
  CapMap cap;
67
  CutMap cut;
68
  Value v;
69

	
70
  HaoOrlin<Digraph, CapMap> ho_test(g, cap);
71
  const HaoOrlin<Digraph, CapMap>&
72
    const_ho_test = ho_test;
73

	
74
  ho_test.init();
75
  ho_test.init(n);
76
  ho_test.calculateOut();
77
  ho_test.calculateIn();
78
  ho_test.run();
79
  ho_test.run(n);
80

	
81
  v = const_ho_test.minCutValue();
82
  v = const_ho_test.minCutMap(cut);
83
}
84

	
85
template <typename Graph, typename CapMap, typename CutMap>
86
typename CapMap::Value 
87
  cutValue(const Graph& graph, const CapMap& cap, const CutMap& cut)
88
{
89
  typename CapMap::Value sum = 0;
90
  for (typename Graph::ArcIt a(graph); a != INVALID; ++a) {
91
    if (cut[graph.source(a)] && !cut[graph.target(a)])
92
      sum += cap[a];
93
  }
94
  return sum;
95
}
48 96

	
49 97
int main() {
50
  SmartGraph graph;
51
  SmartGraph::EdgeMap<int> capacity(graph);
98
  SmartDigraph graph;
99
  SmartDigraph::ArcMap<int> cap1(graph), cap2(graph), cap3(graph);
100
  SmartDigraph::NodeMap<bool> cut(graph);
52 101

	
53
  istringstream lgfs(lgf);
54
  graphReader(graph, lgfs).
55
    edgeMap("capacity", capacity).run();
102
  istringstream input(lgf);
103
  digraphReader(graph, input)
104
    .arcMap("cap1", cap1)
105
    .arcMap("cap2", cap2)
106
    .arcMap("cap3", cap3)
107
    .run();
56 108

	
57
  HaoOrlin<SmartGraph, SmartGraph::EdgeMap<int> > ho(graph, capacity);
58
  ho.run();
109
  {
110
    HaoOrlin<SmartDigraph> ho(graph, cap1);
111
    ho.run();
112
    ho.minCutMap(cut);
113
    
114
    check(ho.minCutValue() == 1, "Wrong cut value");
115
    check(ho.minCutValue() == cutValue(graph, cap1, cut), "Wrong cut value");
116
  }
117
  {
118
    HaoOrlin<SmartDigraph> ho(graph, cap2);
119
    ho.run();
120
    ho.minCutMap(cut);
59 121

	
60
  check(ho.minCutValue() == 3, "Wrong cut value");
122
    check(ho.minCutValue() == 1, "Wrong cut value");
123
    check(ho.minCutValue() == cutValue(graph, cap2, cut), "Wrong cut value");
124
  }
125
  {
126
    HaoOrlin<SmartDigraph> ho(graph, cap3);
127
    ho.run();
128
    ho.minCutMap(cut);
129
    
130
    check(ho.minCutValue() == 1, "Wrong cut value");
131
    check(ho.minCutValue() == cutValue(graph, cap3, cut), "Wrong cut value");
132
  }
133
  
134
  typedef Undirector<SmartDigraph> UGraph;
135
  UGraph ugraph(graph);
136
  
137
  {
138
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap1);
139
    ho.run();
140
    ho.minCutMap(cut);
141
    
142
    check(ho.minCutValue() == 2, "Wrong cut value");
143
    check(ho.minCutValue() == cutValue(ugraph, cap1, cut), "Wrong cut value");
144
  }
145
  {
146
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap2);
147
    ho.run();
148
    ho.minCutMap(cut);
149
    
150
    check(ho.minCutValue() == 5, "Wrong cut value");
151
    check(ho.minCutValue() == cutValue(ugraph, cap2, cut), "Wrong cut value");
152
  }
153
  {
154
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap3);
155
    ho.run();
156
    ho.minCutMap(cut);
157
    
158
    check(ho.minCutValue() == 5, "Wrong cut value");
159
    check(ho.minCutValue() == cutValue(ugraph, cap3, cut), "Wrong cut value");
160
  }
61 161

	
62 162
  return 0;
63 163
}
Ignore white space 6 line context
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-2009
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

	
29 28
#include <lemon/lgf_reader.h>
30 29
#include <lemon/dijkstra.h>
31 30
#include <lemon/maps.h>
32 31

	
33 32
#include <lemon/bin_heap.h>
33
#include <lemon/fourary_heap.h>
34
#include <lemon/kary_heap.h>
35
#include <lemon/fib_heap.h>
36
#include <lemon/pairing_heap.h>
37
#include <lemon/radix_heap.h>
38
#include <lemon/binom_heap.h>
39
#include <lemon/bucket_heap.h>
34 40

	
35 41
#include "test_tools.h"
36 42

	
37 43
using namespace lemon;
38 44
using namespace lemon::concepts;
39 45

	
40 46
typedef ListDigraph Digraph;
41 47
DIGRAPH_TYPEDEFS(Digraph);
42 48

	
43 49
char test_lgf[] =
44 50
  "@nodes\n"
45 51
  "label\n"
46 52
  "0\n"
47 53
  "1\n"
48 54
  "2\n"
49 55
  "3\n"
50 56
  "4\n"
51 57
  "5\n"
52 58
  "6\n"
53 59
  "7\n"
54 60
  "8\n"
55 61
  "9\n"
56 62
  "@arcs\n"
57 63
  "                label   capacity\n"
58 64
  "0       5       0       94\n"
59 65
  "3       9       1       11\n"
60 66
  "8       7       2       83\n"
61 67
  "1       2       3       94\n"
62 68
  "5       7       4       35\n"
63 69
  "7       4       5       84\n"
64 70
  "9       5       6       38\n"
65 71
  "0       4       7       96\n"
66 72
  "6       7       8       6\n"
67 73
  "3       1       9       27\n"
68 74
  "5       2       10      77\n"
69 75
  "5       6       11      69\n"
70 76
  "6       5       12      41\n"
71 77
  "4       6       13      70\n"
72 78
  "3       2       14      45\n"
73 79
  "7       9       15      93\n"
74 80
  "5       9       16      50\n"
75 81
  "9       0       17      94\n"
76 82
  "9       6       18      67\n"
77 83
  "0       9       19      86\n"
78 84
  "@attributes\n"
79 85
  "source 3\n";
80 86

	
81 87
int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26,  1,  9,  4, 34};
82 88
int test_inc[] = {20, 28, 34, 16,  0, 46, 44,  0, 42, 32, 14,  8,  6, 37};
83 89

	
84 90
int test_len = sizeof(test_seq) / sizeof(test_seq[0]);
85 91

	
86 92
template <typename Heap>
87 93
void heapSortTest() {
88 94
  RangeMap<int> map(test_len, -1);
89

	
90 95
  Heap heap(map);
91 96

	
92 97
  std::vector<int> v(test_len);
93

	
94 98
  for (int i = 0; i < test_len; ++i) {
95 99
    v[i] = test_seq[i];
96 100
    heap.push(i, v[i]);
97 101
  }
98 102
  std::sort(v.begin(), v.end());
99 103
  for (int i = 0; i < test_len; ++i) {
100
    check(v[i] == heap.prio() ,"Wrong order in heap sort.");
104
    check(v[i] == heap.prio(), "Wrong order in heap sort.");
101 105
    heap.pop();
102 106
  }
103 107
}
104 108

	
105 109
template <typename Heap>
106 110
void heapIncreaseTest() {
107 111
  RangeMap<int> map(test_len, -1);
108 112

	
109 113
  Heap heap(map);
110 114

	
111 115
  std::vector<int> v(test_len);
112

	
113 116
  for (int i = 0; i < test_len; ++i) {
114 117
    v[i] = test_seq[i];
115 118
    heap.push(i, v[i]);
116 119
  }
117 120
  for (int i = 0; i < test_len; ++i) {
118 121
    v[i] += test_inc[i];
119 122
    heap.increase(i, v[i]);
120 123
  }
121 124
  std::sort(v.begin(), v.end());
122 125
  for (int i = 0; i < test_len; ++i) {
123
    check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
126
    check(v[i] == heap.prio(), "Wrong order in heap increase test.");
124 127
    heap.pop();
125 128
  }
126 129
}
127 130

	
128

	
129

	
130 131
template <typename Heap>
131 132
void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length,
132 133
                      Node source) {
133 134

	
134 135
  typename Dijkstra<Digraph, IntArcMap>::template SetStandardHeap<Heap>::
135 136
    Create dijkstra(digraph, length);
136 137

	
137 138
  dijkstra.run(source);
138 139

	
139 140
  for(ArcIt a(digraph); a != INVALID; ++a) {
140 141
    Node s = digraph.source(a);
141 142
    Node t = digraph.target(a);
142 143
    if (dijkstra.reached(s)) {
143 144
      check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a],
144
             "Error in a shortest path tree!");
145
             "Error in shortest path tree.");
145 146
    }
146 147
  }
147 148

	
148 149
  for(NodeIt n(digraph); n != INVALID; ++n) {
149 150
    if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) {
150 151
      Arc a = dijkstra.predArc(n);
151 152
      Node s = digraph.source(a);
152 153
      check( dijkstra.dist(n) - dijkstra.dist(s) == length[a],
153
             "Error in a shortest path tree!");
154
             "Error in shortest path tree.");
154 155
    }
155 156
  }
156 157

	
157 158
}
158 159

	
159 160
int main() {
160 161

	
161 162
  typedef int Item;
162 163
  typedef int Prio;
163 164
  typedef RangeMap<int> ItemIntMap;
164 165

	
165 166
  Digraph digraph;
166 167
  IntArcMap length(digraph);
167 168
  Node source;
168 169

	
169 170
  std::istringstream input(test_lgf);
170 171
  digraphReader(digraph, input).
171 172
    arcMap("capacity", length).
172 173
    node("source", source).
173 174
    run();
174 175

	
176
  // BinHeap
175 177
  {
176 178
    typedef BinHeap<Prio, ItemIntMap> IntHeap;
177 179
    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
178 180
    heapSortTest<IntHeap>();
179 181
    heapIncreaseTest<IntHeap>();
180 182

	
181 183
    typedef BinHeap<Prio, IntNodeMap > NodeHeap;
182 184
    checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
183 185
    dijkstraHeapTest<NodeHeap>(digraph, length, source);
184 186
  }
185 187

	
188
  // FouraryHeap
189
  {
190
    typedef FouraryHeap<Prio, ItemIntMap> IntHeap;
191
    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
192
    heapSortTest<IntHeap>();
193
    heapIncreaseTest<IntHeap>();
194

	
195
    typedef FouraryHeap<Prio, IntNodeMap > NodeHeap;
196
    checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
197
    dijkstraHeapTest<NodeHeap>(digraph, length, source);
198
  }
199

	
200
  // KaryHeap
201
  {
202
    typedef KaryHeap<Prio, ItemIntMap> IntHeap;
203
    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
204
    heapSortTest<IntHeap>();
205
    heapIncreaseTest<IntHeap>();
206

	
207
    typedef KaryHeap<Prio, IntNodeMap > NodeHeap;
208
    checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
209
    dijkstraHeapTest<NodeHeap>(digraph, length, source);
210
  }
211

	
212
  // FibHeap
213
  {
214
    typedef FibHeap<Prio, ItemIntMap> IntHeap;
215
    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
216
    heapSortTest<IntHeap>();
217
    heapIncreaseTest<IntHeap>();
218

	
219
    typedef FibHeap<Prio, IntNodeMap > NodeHeap;
220
    checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
221
    dijkstraHeapTest<NodeHeap>(digraph, length, source);
222
  }
223

	
224
  // PairingHeap
225
  {
226
    typedef PairingHeap<Prio, ItemIntMap> IntHeap;
227
    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
228
    heapSortTest<IntHeap>();
229
    heapIncreaseTest<IntHeap>();
230

	
231
    typedef PairingHeap<Prio, IntNodeMap > NodeHeap;
232
    checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
233
    dijkstraHeapTest<NodeHeap>(digraph, length, source);
234
  }
235

	
236
  // RadixHeap
237
  {
238
    typedef RadixHeap<ItemIntMap> IntHeap;
239
    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
240
    heapSortTest<IntHeap>();
241
    heapIncreaseTest<IntHeap>();
242

	
243
    typedef RadixHeap<IntNodeMap > NodeHeap;
244
    checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
245
    dijkstraHeapTest<NodeHeap>(digraph, length, source);
246
  }
247

	
248
  // BinomHeap
249
  {
250
    typedef BinomHeap<Prio, ItemIntMap> IntHeap;
251
    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
252
    heapSortTest<IntHeap>();
253
    heapIncreaseTest<IntHeap>();
254

	
255
    typedef BinomHeap<Prio, IntNodeMap > NodeHeap;
256
    checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
257
    dijkstraHeapTest<NodeHeap>(digraph, length, source);
258
  }
259

	
260
  // BucketHeap, SimpleBucketHeap
261
  {
262
    typedef BucketHeap<ItemIntMap> IntHeap;
263
    checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
264
    heapSortTest<IntHeap>();
265
    heapIncreaseTest<IntHeap>();
266

	
267
    typedef BucketHeap<IntNodeMap > NodeHeap;
268
    checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
269
    dijkstraHeapTest<NodeHeap>(digraph, length, source);
270

	
271
    typedef SimpleBucketHeap<ItemIntMap> SimpleIntHeap;
272
    heapSortTest<SimpleIntHeap>();
273
  }
274

	
186 275
  return 0;
187 276
}
Ignore white space 6 line context
... ...
@@ -6,142 +6,142 @@
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 <vector>
21 21

	
22 22
#include "test_tools.h"
23 23
#include <lemon/maps.h>
24 24
#include <lemon/kruskal.h>
25 25
#include <lemon/list_graph.h>
26 26

	
27 27
#include <lemon/concepts/maps.h>
28 28
#include <lemon/concepts/digraph.h>
29 29
#include <lemon/concepts/graph.h>
30 30

	
31 31
using namespace std;
32 32
using namespace lemon;
33 33

	
34 34
void checkCompileKruskal()
35 35
{
36 36
  concepts::WriteMap<concepts::Digraph::Arc,bool> w;
37 37
  concepts::WriteMap<concepts::Graph::Edge,bool> uw;
38 38

	
39 39
  concepts::ReadMap<concepts::Digraph::Arc,int> r;
40 40
  concepts::ReadMap<concepts::Graph::Edge,int> ur;
41 41

	
42 42
  concepts::Digraph g;
43 43
  concepts::Graph ug;
44 44

	
45 45
  kruskal(g, r, w);
46 46
  kruskal(ug, ur, uw);
47 47

	
48 48
  std::vector<std::pair<concepts::Digraph::Arc, int> > rs;
49 49
  std::vector<std::pair<concepts::Graph::Edge, int> > urs;
50 50

	
51 51
  kruskal(g, rs, w);
52 52
  kruskal(ug, urs, uw);
53 53

	
54 54
  std::vector<concepts::Digraph::Arc> ws;
55 55
  std::vector<concepts::Graph::Edge> uws;
56 56

	
57 57
  kruskal(g, r, ws.begin());
58 58
  kruskal(ug, ur, uws.begin());
59 59
}
60 60

	
61 61
int main() {
62 62

	
63 63
  typedef ListGraph::Node Node;
64 64
  typedef ListGraph::Edge Edge;
65 65
  typedef ListGraph::NodeIt NodeIt;
66 66
  typedef ListGraph::ArcIt ArcIt;
67 67

	
68 68
  ListGraph G;
69 69

	
70 70
  Node s=G.addNode();
71 71
  Node v1=G.addNode();
72 72
  Node v2=G.addNode();
73 73
  Node v3=G.addNode();
74 74
  Node v4=G.addNode();
75 75
  Node t=G.addNode();
76 76

	
77 77
  Edge e1 = G.addEdge(s, v1);
78 78
  Edge e2 = G.addEdge(s, v2);
79 79
  Edge e3 = G.addEdge(v1, v2);
80 80
  Edge e4 = G.addEdge(v2, v1);
81 81
  Edge e5 = G.addEdge(v1, v3);
82 82
  Edge e6 = G.addEdge(v3, v2);
83 83
  Edge e7 = G.addEdge(v2, v4);
84 84
  Edge e8 = G.addEdge(v4, v3);
85 85
  Edge e9 = G.addEdge(v3, t);
86 86
  Edge e10 = G.addEdge(v4, t);
87 87

	
88 88
  typedef ListGraph::EdgeMap<int> ECostMap;
89 89
  typedef ListGraph::EdgeMap<bool> EBoolMap;
90 90

	
91 91
  ECostMap edge_cost_map(G, 2);
92 92
  EBoolMap tree_map(G);
93 93

	
94 94

	
95 95
  //Test with const map.
96 96
  check(kruskal(G, ConstMap<ListGraph::Edge,int>(2), tree_map)==10,
97 97
        "Total cost should be 10");
98 98
  //Test with an edge map (filled with uniform costs).
99 99
  check(kruskal(G, edge_cost_map, tree_map)==10,
100 100
        "Total cost should be 10");
101 101

	
102
  edge_cost_map.set(e1, -10);
103
  edge_cost_map.set(e2, -9);
104
  edge_cost_map.set(e3, -8);
105
  edge_cost_map.set(e4, -7);
106
  edge_cost_map.set(e5, -6);
107
  edge_cost_map.set(e6, -5);
108
  edge_cost_map.set(e7, -4);
109
  edge_cost_map.set(e8, -3);
110
  edge_cost_map.set(e9, -2);
111
  edge_cost_map.set(e10, -1);
102
  edge_cost_map[e1] = -10;
103
  edge_cost_map[e2] = -9;
104
  edge_cost_map[e3] = -8;
105
  edge_cost_map[e4] = -7;
106
  edge_cost_map[e5] = -6;
107
  edge_cost_map[e6] = -5;
108
  edge_cost_map[e7] = -4;
109
  edge_cost_map[e8] = -3;
110
  edge_cost_map[e9] = -2;
111
  edge_cost_map[e10] = -1;
112 112

	
113 113
  vector<Edge> tree_edge_vec(5);
114 114

	
115 115
  //Test with a edge map and inserter.
116 116
  check(kruskal(G, edge_cost_map,
117 117
                 tree_edge_vec.begin())
118 118
        ==-31,
119 119
        "Total cost should be -31.");
120 120

	
121 121
  tree_edge_vec.clear();
122 122

	
123 123
  check(kruskal(G, edge_cost_map,
124 124
                back_inserter(tree_edge_vec))
125 125
        ==-31,
126 126
        "Total cost should be -31.");
127 127

	
128 128
//   tree_edge_vec.clear();
129 129

	
130 130
//   //The above test could also be coded like this:
131 131
//   check(kruskal(G,
132 132
//                 makeKruskalMapInput(G, edge_cost_map),
133 133
//                 makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
134 134
//         ==-31,
135 135
//         "Total cost should be -31.");
136 136

	
137 137
  check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
138 138

	
139 139
  check(tree_edge_vec[0]==e1 &&
140 140
        tree_edge_vec[1]==e2 &&
141 141
        tree_edge_vec[2]==e5 &&
142 142
        tree_edge_vec[3]==e7 &&
143 143
        tree_edge_vec[4]==e9,
144 144
        "Wrong tree.");
145 145

	
146 146
  return 0;
147 147
}
Ignore white space 6 line context
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
 * Copyright (C) 2003-2008
5
 * Copyright (C) 2003-2009
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 <sstream>
20 20
#include <lemon/lp_skeleton.h>
21 21
#include "test_tools.h"
22 22
#include <lemon/tolerance.h>
23 23

	
24
#ifdef HAVE_CONFIG_H
25 24
#include <lemon/config.h>
26
#endif
27 25

	
28
#ifdef HAVE_GLPK
26
#ifdef LEMON_HAVE_GLPK
29 27
#include <lemon/glpk.h>
30 28
#endif
31 29

	
32
#ifdef HAVE_CPLEX
30
#ifdef LEMON_HAVE_CPLEX
33 31
#include <lemon/cplex.h>
34 32
#endif
35 33

	
36
#ifdef HAVE_SOPLEX
34
#ifdef LEMON_HAVE_SOPLEX
37 35
#include <lemon/soplex.h>
38 36
#endif
39 37

	
40
#ifdef HAVE_CLP
38
#ifdef LEMON_HAVE_CLP
41 39
#include <lemon/clp.h>
42 40
#endif
43 41

	
44 42
using namespace lemon;
45 43

	
46 44
void lpTest(LpSolver& lp)
47 45
{
48 46

	
49 47
  typedef LpSolver LP;
50 48

	
51 49
  std::vector<LP::Col> x(10);
52 50
  //  for(int i=0;i<10;i++) x.push_back(lp.addCol());
53 51
  lp.addColSet(x);
54 52
  lp.colLowerBound(x,1);
55 53
  lp.colUpperBound(x,1);
56 54
  lp.colBounds(x,1,2);
57 55

	
58 56
  std::vector<LP::Col> y(10);
59 57
  lp.addColSet(y);
60 58

	
61 59
  lp.colLowerBound(y,1);
62 60
  lp.colUpperBound(y,1);
63 61
  lp.colBounds(y,1,2);
64 62

	
65 63
  std::map<int,LP::Col> z;
66 64

	
67 65
  z.insert(std::make_pair(12,INVALID));
68 66
  z.insert(std::make_pair(2,INVALID));
69 67
  z.insert(std::make_pair(7,INVALID));
70 68
  z.insert(std::make_pair(5,INVALID));
71 69

	
72 70
  lp.addColSet(z);
73 71

	
74 72
  lp.colLowerBound(z,1);
75 73
  lp.colUpperBound(z,1);
76 74
  lp.colBounds(z,1,2);
77 75

	
78 76
  {
79 77
    LP::Expr e,f,g;
80 78
    LP::Col p1,p2,p3,p4,p5;
81 79
    LP::Constr c;
82 80

	
83 81
    p1=lp.addCol();
84 82
    p2=lp.addCol();
85 83
    p3=lp.addCol();
86 84
    p4=lp.addCol();
87 85
    p5=lp.addCol();
88 86

	
89 87
    e[p1]=2;
90 88
    *e=12;
91 89
    e[p1]+=2;
92 90
    *e+=12;
93 91
    e[p1]-=2;
94 92
    *e-=12;
95 93

	
96 94
    e=2;
97 95
    e=2.2;
98 96
    e=p1;
99 97
    e=f;
100 98

	
101 99
    e+=2;
102 100
    e+=2.2;
103 101
    e+=p1;
104 102
    e+=f;
105 103

	
106 104
    e-=2;
107 105
    e-=2.2;
108 106
    e-=p1;
109 107
    e-=f;
110 108

	
111 109
    e*=2;
112 110
    e*=2.2;
113 111
    e/=2;
114 112
    e/=2.2;
115 113

	
116 114
    e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
117 115
       (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
118 116
       (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
119 117
       2.2*f+f*2.2+f/2.2+
120 118
       2*f+f*2+f/2+
121 119
       2.2*p1+p1*2.2+p1/2.2+
122 120
       2*p1+p1*2+p1/2
123 121
       );
124 122

	
125 123

	
126 124
    c = (e  <= f  );
127 125
    c = (e  <= 2.2);
128 126
    c = (e  <= 2  );
129 127
    c = (e  <= p1 );
130 128
    c = (2.2<= f  );
131 129
    c = (2  <= f  );
132 130
    c = (p1 <= f  );
133 131
    c = (p1 <= p2 );
134 132
    c = (p1 <= 2.2);
135 133
    c = (p1 <= 2  );
136 134
    c = (2.2<= p2 );
137 135
    c = (2  <= p2 );
138 136

	
139 137
    c = (e  >= f  );
140 138
    c = (e  >= 2.2);
141 139
    c = (e  >= 2  );
142 140
    c = (e  >= p1 );
143 141
    c = (2.2>= f  );
144 142
    c = (2  >= f  );
145 143
    c = (p1 >= f  );
146 144
    c = (p1 >= p2 );
147 145
    c = (p1 >= 2.2);
148 146
    c = (p1 >= 2  );
149 147
    c = (2.2>= p2 );
150 148
    c = (2  >= p2 );
151 149

	
152 150
    c = (e  == f  );
153 151
    c = (e  == 2.2);
154 152
    c = (e  == 2  );
155 153
    c = (e  == p1 );
156 154
    c = (2.2== f  );
157 155
    c = (2  == f  );
158 156
    c = (p1 == f  );
159 157
    //c = (p1 == p2 );
160 158
    c = (p1 == 2.2);
161 159
    c = (p1 == 2  );
162 160
    c = (2.2== p2 );
163 161
    c = (2  == p2 );
164 162

	
165 163
    c = ((2 <= e) <= 3);
166 164
    c = ((2 <= p1) <= 3);
167 165

	
168 166
    c = ((2 >= e) >= 3);
169 167
    c = ((2 >= p1) >= 3);
170 168

	
171 169
    e[x[3]]=2;
172 170
    e[x[3]]=4;
173 171
    e[x[3]]=1;
174 172
    *e=12;
175 173

	
176 174
    lp.addRow(-LP::INF,e,23);
177 175
    lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
178 176
    lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
179 177

	
180 178
    lp.addRow(x[1]+x[3]<=x[5]-3);
181 179
    lp.addRow((-7<=x[1]+x[3]-12)<=3);
182 180
    lp.addRow(x[1]<=x[5]);
183 181

	
184 182
    std::ostringstream buf;
185 183

	
186 184

	
187 185
    e=((p1+p2)+(p1-0.99*p2));
188 186
    //e.prettyPrint(std::cout);
189 187
    //(e<=2).prettyPrint(std::cout);
190 188
    double tolerance=0.001;
191 189
    e.simplify(tolerance);
192 190
    buf << "Coeff. of p2 should be 0.01";
193 191
    check(e[p2]>0, buf.str());
194 192

	
195 193
    tolerance=0.02;
196 194
    e.simplify(tolerance);
197 195
    buf << "Coeff. of p2 should be 0";
198 196
    check(const_cast<const LpSolver::Expr&>(e)[p2]==0, buf.str());
199 197

	
198
    //Test for clone/new
199
    LP* lpnew = lp.newSolver();
200
    LP* lpclone = lp.cloneSolver();
201
    delete lpnew;
202
    delete lpclone;
200 203

	
201 204
  }
202 205

	
203 206
  {
204 207
    LP::DualExpr e,f,g;
205 208
    LP::Row p1 = INVALID, p2 = INVALID, p3 = INVALID,
206 209
      p4 = INVALID, p5 = INVALID;
207 210

	
208 211
    e[p1]=2;
209 212
    e[p1]+=2;
210 213
    e[p1]-=2;
211 214

	
212 215
    e=p1;
213 216
    e=f;
214 217

	
215 218
    e+=p1;
216 219
    e+=f;
217 220

	
218 221
    e-=p1;
219 222
    e-=f;
220 223

	
221 224
    e*=2;
222 225
    e*=2.2;
223 226
    e/=2;
224 227
    e/=2.2;
225 228

	
226 229
    e=((p1+p2)+(p1-p2)+
227 230
       (p1+f)+(f+p1)+(f+g)+
228 231
       (p1-f)+(f-p1)+(f-g)+
229 232
       2.2*f+f*2.2+f/2.2+
230 233
       2*f+f*2+f/2+
231 234
       2.2*p1+p1*2.2+p1/2.2+
232 235
       2*p1+p1*2+p1/2
233 236
       );
234 237
  }
235 238

	
236 239
}
237 240

	
238 241
void solveAndCheck(LpSolver& lp, LpSolver::ProblemType stat,
239 242
                   double exp_opt) {
240 243
  using std::string;
241 244
  lp.solve();
242 245

	
243 246
  std::ostringstream buf;
244 247
  buf << "PrimalType should be: " << int(stat) << int(lp.primalType());
245 248

	
246 249
  check(lp.primalType()==stat, buf.str());
247 250

	
248 251
  if (stat ==  LpSolver::OPTIMAL) {
249 252
    std::ostringstream sbuf;
250
    sbuf << "Wrong optimal value: the right optimum is " << exp_opt;
253
    sbuf << "Wrong optimal value (" << lp.primal() <<") with "
254
         << lp.solverName() <<"\n     the right optimum is " << exp_opt;
251 255
    check(std::abs(lp.primal()-exp_opt) < 1e-3, sbuf.str());
252 256
  }
253 257
}
254 258

	
255 259
void aTest(LpSolver & lp)
256 260
{
257 261
  typedef LpSolver LP;
258 262

	
259 263
 //The following example is very simple
260 264

	
261 265
  typedef LpSolver::Row Row;
262 266
  typedef LpSolver::Col Col;
263 267

	
264 268

	
265 269
  Col x1 = lp.addCol();
266 270
  Col x2 = lp.addCol();
267 271

	
268 272

	
269 273
  //Constraints
270 274
  Row upright=lp.addRow(x1+2*x2 <=1);
271 275
  lp.addRow(x1+x2 >=-1);
272 276
  lp.addRow(x1-x2 <=1);
273 277
  lp.addRow(x1-x2 >=-1);
274 278
  //Nonnegativity of the variables
275 279
  lp.colLowerBound(x1, 0);
276 280
  lp.colLowerBound(x2, 0);
277 281
  //Objective function
278 282
  lp.obj(x1+x2);
279 283

	
280 284
  lp.sense(lp.MAX);
281 285

	
282 286
  //Testing the problem retrieving routines
283 287
  check(lp.objCoeff(x1)==1,"First term should be 1 in the obj function!");
284 288
  check(lp.sense() == lp.MAX,"This is a maximization!");
285 289
  check(lp.coeff(upright,x1)==1,"The coefficient in question is 1!");
286 290
  check(lp.colLowerBound(x1)==0,
287 291
        "The lower bound for variable x1 should be 0.");
288 292
  check(lp.colUpperBound(x1)==LpSolver::INF,
289 293
        "The upper bound for variable x1 should be infty.");
290 294
  check(lp.rowLowerBound(upright) == -LpSolver::INF,
291 295
        "The lower bound for the first row should be -infty.");
292 296
  check(lp.rowUpperBound(upright)==1,
293 297
        "The upper bound for the first row should be 1.");
294 298
  LpSolver::Expr e = lp.row(upright);
295 299
  check(e[x1] == 1, "The first coefficient should 1.");
296 300
  check(e[x2] == 2, "The second coefficient should 1.");
297 301

	
298 302
  lp.row(upright, x1+x2 <=1);
299 303
  e = lp.row(upright);
300 304
  check(e[x1] == 1, "The first coefficient should 1.");
301 305
  check(e[x2] == 1, "The second coefficient should 1.");
302 306

	
303 307
  LpSolver::DualExpr de = lp.col(x1);
304 308
  check(  de[upright] == 1, "The first coefficient should 1.");
305 309

	
306 310
  LpSolver* clp = lp.cloneSolver();
307 311

	
308 312
  //Testing the problem retrieving routines
309 313
  check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!");
310 314
  check(clp->sense() == clp->MAX,"This is a maximization!");
311 315
  check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!");
312 316
  //  std::cout<<lp.colLowerBound(x1)<<std::endl;
313 317
  check(clp->colLowerBound(x1)==0,
314 318
        "The lower bound for variable x1 should be 0.");
315 319
  check(clp->colUpperBound(x1)==LpSolver::INF,
316 320
        "The upper bound for variable x1 should be infty.");
317 321

	
318 322
  check(lp.rowLowerBound(upright)==-LpSolver::INF,
319 323
        "The lower bound for the first row should be -infty.");
320 324
  check(lp.rowUpperBound(upright)==1,
321 325
        "The upper bound for the first row should be 1.");
322 326
  e = clp->row(upright);
323 327
  check(e[x1] == 1, "The first coefficient should 1.");
324 328
  check(e[x2] == 1, "The second coefficient should 1.");
325 329

	
326 330
  de = clp->col(x1);
327 331
  check(de[upright] == 1, "The first coefficient should 1.");
328 332

	
329 333
  delete clp;
330 334

	
331 335
  //Maximization of x1+x2
332 336
  //over the triangle with vertices (0,0) (0,1) (1,0)
333 337
  double expected_opt=1;
334 338
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
335 339

	
336 340
  //Minimization
337 341
  lp.sense(lp.MIN);
338 342
  expected_opt=0;
339 343
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
340 344

	
341 345
  //Vertex (-1,0) instead of (0,0)
342 346
  lp.colLowerBound(x1, -LpSolver::INF);
343 347
  expected_opt=-1;
344 348
  solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt);
345 349

	
346 350
  //Erase one constraint and return to maximization
347 351
  lp.erase(upright);
348 352
  lp.sense(lp.MAX);
349 353
  expected_opt=LpSolver::INF;
350 354
  solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt);
351 355

	
352 356
  //Infeasibilty
353 357
  lp.addRow(x1+x2 <=-2);
354 358
  solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt);
355 359

	
356 360
}
357 361

	
362
template<class LP>
363
void cloneTest()
364
{
365
  //Test for clone/new
366

	
367
  LP* lp = new LP();
368
  LP* lpnew = lp->newSolver();
369
  LP* lpclone = lp->cloneSolver();
370
  delete lp;
371
  delete lpnew;
372
  delete lpclone;
373
}
374

	
358 375
int main()
359 376
{
360 377
  LpSkeleton lp_skel;
361 378
  lpTest(lp_skel);
362 379

	
363
#ifdef HAVE_GLPK
380
#ifdef LEMON_HAVE_GLPK
364 381
  {
365 382
    GlpkLp lp_glpk1,lp_glpk2;
366 383
    lpTest(lp_glpk1);
367 384
    aTest(lp_glpk2);
385
    cloneTest<GlpkLp>();
368 386
  }
369 387
#endif
370 388

	
371
#ifdef HAVE_CPLEX
389
#ifdef LEMON_HAVE_CPLEX
372 390
  try {
373 391
    CplexLp lp_cplex1,lp_cplex2;
374 392
    lpTest(lp_cplex1);
375 393
    aTest(lp_cplex2);
394
    cloneTest<CplexLp>();
376 395
  } catch (CplexEnv::LicenseError& error) {
377
#ifdef LEMON_FORCE_CPLEX_CHECK
378 396
    check(false, error.what());
379
#else
380
    std::cerr << error.what() << std::endl;
381
    std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
382
#endif
383 397
  }
384 398
#endif
385 399

	
386
#ifdef HAVE_SOPLEX
400
#ifdef LEMON_HAVE_SOPLEX
387 401
  {
388 402
    SoplexLp lp_soplex1,lp_soplex2;
389 403
    lpTest(lp_soplex1);
390 404
    aTest(lp_soplex2);
405
    cloneTest<SoplexLp>();
391 406
  }
392 407
#endif
393 408

	
394
#ifdef HAVE_CLP
409
#ifdef LEMON_HAVE_CLP
395 410
  {
396 411
    ClpLp lp_clp1,lp_clp2;
397 412
    lpTest(lp_clp1);
398 413
    aTest(lp_clp2);
414
    cloneTest<ClpLp>();
399 415
  }
400 416
#endif
401 417

	
402 418
  return 0;
403 419
}
Ignore white space 6 line context
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-2009
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 <deque>
20 20
#include <set>
21 21

	
22 22
#include <lemon/concept_check.h>
23 23
#include <lemon/concepts/maps.h>
24 24
#include <lemon/maps.h>
25
#include <lemon/list_graph.h>
26
#include <lemon/smart_graph.h>
27
#include <lemon/adaptors.h>
28
#include <lemon/dfs.h>
25 29

	
26 30
#include "test_tools.h"
27 31

	
28 32
using namespace lemon;
29 33
using namespace lemon::concepts;
30 34

	
31 35
struct A {};
32 36
inline bool operator<(A, A) { return true; }
33 37
struct B {};
34 38

	
35 39
class C {
36 40
  int x;
37 41
public:
38 42
  C(int _x) : x(_x) {}
39 43
};
40 44

	
41 45
class F {
42 46
public:
43 47
  typedef A argument_type;
44 48
  typedef B result_type;
45 49

	
46 50
  B operator()(const A&) const { return B(); }
47 51
private:
48 52
  F& operator=(const F&);
49 53
};
50 54

	
51 55
int func(A) { return 3; }
52 56

	
53 57
int binc(int a, B) { return a+1; }
54 58

	
55 59
typedef ReadMap<A, double> DoubleMap;
56 60
typedef ReadWriteMap<A, double> DoubleWriteMap;
57 61
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
58 62

	
59 63
typedef ReadMap<A, bool> BoolMap;
60 64
typedef ReadWriteMap<A, bool> BoolWriteMap;
61 65
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
62 66

	
67
template<typename Map1, typename Map2, typename ItemIt>
68
void compareMap(const Map1& map1, const Map2& map2, ItemIt it) {
69
  for (; it != INVALID; ++it)
70
    check(map1[it] == map2[it], "The maps are not equal");
71
}
72

	
63 73
int main()
64 74
{
65 75
  // Map concepts
66 76
  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
67 77
  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
68 78
  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
69 79
  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
70 80
  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
71 81
  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
72 82
  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
73 83
  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
74 84

	
75 85
  // NullMap
76 86
  {
77 87
    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
78 88
    NullMap<A,B> map1;
79 89
    NullMap<A,B> map2 = map1;
80 90
    map1 = nullMap<A,B>();
81 91
  }
82 92

	
83 93
  // ConstMap
84 94
  {
85 95
    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
86 96
    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
87 97
    ConstMap<A,B> map1;
88 98
    ConstMap<A,B> map2 = B();
89 99
    ConstMap<A,B> map3 = map1;
90 100
    map1 = constMap<A>(B());
91 101
    map1 = constMap<A,B>();
92 102
    map1.setAll(B());
93 103
    ConstMap<A,C> map4(C(1));
94 104
    ConstMap<A,C> map5 = map4;
95 105
    map4 = constMap<A>(C(2));
96 106
    map4.setAll(C(3));
97 107

	
98 108
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
99 109
    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
100 110

	
101 111
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
102 112
    ConstMap<A,Const<int,10> > map6;
103 113
    ConstMap<A,Const<int,10> > map7 = map6;
104 114
    map6 = constMap<A,int,10>();
105 115
    map7 = constMap<A,Const<int,10> >();
106 116
    check(map6[A()] == 10 && map7[A()] == 10,
107 117
          "Something is wrong with ConstMap");
108 118
  }
109 119

	
110 120
  // IdentityMap
111 121
  {
112 122
    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
113 123
    IdentityMap<A> map1;
114 124
    IdentityMap<A> map2 = map1;
115 125
    map1 = identityMap<A>();
116 126

	
117 127
    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
118 128
    check(identityMap<double>()[1.0] == 1.0 &&
119 129
          identityMap<double>()[3.14] == 3.14,
120 130
          "Something is wrong with IdentityMap");
121 131
  }
122 132

	
123 133
  // RangeMap
124 134
  {
125 135
    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
126 136
    RangeMap<B> map1;
127 137
    RangeMap<B> map2(10);
128 138
    RangeMap<B> map3(10,B());
129 139
    RangeMap<B> map4 = map1;
130 140
    RangeMap<B> map5 = rangeMap<B>();
131 141
    RangeMap<B> map6 = rangeMap<B>(10);
132 142
    RangeMap<B> map7 = rangeMap(10,B());
133 143

	
134 144
    checkConcept< ReferenceMap<int, double, double&, const double&>,
135 145
                  RangeMap<double> >();
136 146
    std::vector<double> v(10, 0);
137 147
    v[5] = 100;
138 148
    RangeMap<double> map8(v);
139 149
    RangeMap<double> map9 = rangeMap(v);
140 150
    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
141 151
          "Something is wrong with RangeMap");
142 152
  }
143 153

	
144 154
  // SparseMap
145 155
  {
146 156
    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
147 157
    SparseMap<A,B> map1;
148 158
    SparseMap<A,B> map2 = B();
149 159
    SparseMap<A,B> map3 = sparseMap<A,B>();
150 160
    SparseMap<A,B> map4 = sparseMap<A>(B());
151 161

	
152 162
    checkConcept< ReferenceMap<double, int, int&, const int&>,
153 163
                  SparseMap<double, int> >();
154 164
    std::map<double, int> m;
155 165
    SparseMap<double, int> map5(m);
156 166
    SparseMap<double, int> map6(m,10);
157 167
    SparseMap<double, int> map7 = sparseMap(m);
158 168
    SparseMap<double, int> map8 = sparseMap(m,10);
159 169

	
160 170
    check(map5[1.0] == 0 && map5[3.14] == 0 &&
161 171
          map6[1.0] == 10 && map6[3.14] == 10,
162 172
          "Something is wrong with SparseMap");
163 173
    map5[1.0] = map6[3.14] = 100;
164 174
    check(map5[1.0] == 100 && map5[3.14] == 0 &&
165 175
          map6[1.0] == 10 && map6[3.14] == 100,
166 176
          "Something is wrong with SparseMap");
167 177
  }
168 178

	
169 179
  // ComposeMap
170 180
  {
171 181
    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
172 182
    checkConcept<ReadMap<B,double>, CompMap>();
173
    CompMap map1(DoubleMap(),ReadMap<B,A>());
183
    CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
174 184
    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
175 185

	
176 186
    SparseMap<double, bool> m1(false); m1[3.14] = true;
177 187
    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
178 188
    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
179 189
          "Something is wrong with ComposeMap")
180 190
  }
181 191

	
182 192
  // CombineMap
183 193
  {
184 194
    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
185 195
    checkConcept<ReadMap<A,double>, CombMap>();
186
    CombMap map1(DoubleMap(), DoubleMap());
196
    CombMap map1 = CombMap(DoubleMap(), DoubleMap());
187 197
    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
188 198

	
189 199
    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
190 200
          "Something is wrong with CombineMap");
191 201
  }
192 202

	
193 203
  // FunctorToMap, MapToFunctor
194 204
  {
195 205
    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
196 206
    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
197 207
    FunctorToMap<F> map1;
198
    FunctorToMap<F> map2(F());
208
    FunctorToMap<F> map2 = FunctorToMap<F>(F());
199 209
    B b = functorToMap(F())[A()];
200 210

	
201 211
    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
202
    MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
212
    MapToFunctor<ReadMap<A,B> > map = MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
203 213

	
204 214
    check(functorToMap(&func)[A()] == 3,
205 215
          "Something is wrong with FunctorToMap");
206 216
    check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
207 217
          "Something is wrong with MapToFunctor");
208 218
    check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
209 219
          mapToFunctor(functorToMap(&func))[A()] == 3,
210 220
          "Something is wrong with FunctorToMap or MapToFunctor");
211 221
    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
212 222
          "Something is wrong with FunctorToMap or MapToFunctor");
213 223
  }
214 224

	
215 225
  // ConvertMap
216 226
  {
217 227
    checkConcept<ReadMap<double,double>,
218 228
      ConvertMap<ReadMap<double, int>, double> >();
219 229
    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
220 230
    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
221 231
  }
222 232

	
223 233
  // ForkMap
224 234
  {
225 235
    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
226 236

	
227 237
    typedef RangeMap<double> RM;
228 238
    typedef SparseMap<int, double> SM;
229 239
    RM m1(10, -1);
230 240
    SM m2(-1);
231 241
    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
232 242
    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
233 243
    ForkMap<RM, SM> map1(m1,m2);
234 244
    ForkMap<SM, RM> map2 = forkMap(m2,m1);
235 245
    map2.set(5, 10);
236 246
    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
237 247
          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
238 248
          "Something is wrong with ForkMap");
239 249
  }
240 250

	
241 251
  // Arithmetic maps:
242 252
  // - AddMap, SubMap, MulMap, DivMap
243 253
  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
244 254
  // - NegMap, NegWriteMap, AbsMap
245 255
  {
246 256
    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
247 257
    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
248 258
    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
249 259
    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
250 260

	
251 261
    ConstMap<int, double> c1(1.0), c2(3.14);
252 262
    IdentityMap<int> im;
253 263
    ConvertMap<IdentityMap<int>, double> id(im);
254 264
    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
255 265
          "Something is wrong with AddMap");
256 266
    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
257 267
          "Something is wrong with SubMap");
258 268
    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
259 269
          "Something is wrong with MulMap");
260 270
    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
261 271
          "Something is wrong with DivMap");
262 272

	
263 273
    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
264 274
    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
265 275
    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
266 276
    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
267 277
    checkConcept<DoubleMap, NegMap<DoubleMap> >();
268 278
    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
269 279
    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
270 280

	
271 281
    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
272 282
          "Something is wrong with ShiftMap");
273 283
    check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
274 284
          shiftWriteMap(id, 2.0)[10] == 12.0,
275 285
          "Something is wrong with ShiftWriteMap");
276 286
    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
277 287
          "Something is wrong with ScaleMap");
278 288
    check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
279 289
          scaleWriteMap(id, 2.0)[10] == 20.0,
280 290
          "Something is wrong with ScaleWriteMap");
281 291
    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
282 292
          "Something is wrong with NegMap");
283 293
    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
284 294
          "Something is wrong with NegWriteMap");
285 295
    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
286 296
          "Something is wrong with AbsMap");
287 297
  }
288 298

	
289 299
  // Logical maps:
290 300
  // - TrueMap, FalseMap
291 301
  // - AndMap, OrMap
292 302
  // - NotMap, NotWriteMap
293 303
  // - EqualMap, LessMap
294 304
  {
295 305
    checkConcept<BoolMap, TrueMap<A> >();
296 306
    checkConcept<BoolMap, FalseMap<A> >();
297 307
    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
298 308
    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
299 309
    checkConcept<BoolMap, NotMap<BoolMap> >();
300 310
    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
301 311
    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
302 312
    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
303 313

	
304 314
    TrueMap<int> tm;
305 315
    FalseMap<int> fm;
306 316
    RangeMap<bool> rm(2);
307 317
    rm[0] = true; rm[1] = false;
308 318
    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
309 319
          !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
310 320
          "Something is wrong with AndMap");
311 321
    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
312 322
          orMap(fm,rm)[0] && !orMap(fm,rm)[1],
313 323
          "Something is wrong with OrMap");
314 324
    check(!notMap(rm)[0] && notMap(rm)[1],
315 325
          "Something is wrong with NotMap");
316 326
    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
317 327
          "Something is wrong with NotWriteMap");
318 328

	
319 329
    ConstMap<int, double> cm(2.0);
320 330
    IdentityMap<int> im;
321 331
    ConvertMap<IdentityMap<int>, double> id(im);
322 332
    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
323 333
          "Something is wrong with LessMap");
324 334
    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
325 335
          "Something is wrong with EqualMap");
326 336
  }
327 337

	
328 338
  // LoggerBoolMap
329 339
  {
330 340
    typedef std::vector<int> vec;
341
    checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
342
    checkConcept<WriteMap<int, bool>,
343
                 LoggerBoolMap<std::back_insert_iterator<vec> > >();
344

	
331 345
    vec v1;
332 346
    vec v2(10);
333 347
    LoggerBoolMap<std::back_insert_iterator<vec> >
334 348
      map1(std::back_inserter(v1));
335 349
    LoggerBoolMap<vec::iterator> map2(v2.begin());
336 350
    map1.set(10, false);
337 351
    map1.set(20, true);   map2.set(20, true);
338 352
    map1.set(30, false);  map2.set(40, false);
339 353
    map1.set(50, true);   map2.set(50, true);
340 354
    map1.set(60, true);   map2.set(60, true);
341 355
    check(v1.size() == 3 && v2.size() == 10 &&
342 356
          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
343 357
          v2[0]==20 && v2[1]==50 && v2[2]==60,
344 358
          "Something is wrong with LoggerBoolMap");
345 359

	
346 360
    int i = 0;
347 361
    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
348 362
          it != map2.end(); ++it )
349 363
      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
364
    
365
    typedef ListDigraph Graph;
366
    DIGRAPH_TYPEDEFS(Graph);
367
    Graph gr;
368

	
369
    Node n0 = gr.addNode();
370
    Node n1 = gr.addNode();
371
    Node n2 = gr.addNode();
372
    Node n3 = gr.addNode();
373
    
374
    gr.addArc(n3, n0);
375
    gr.addArc(n3, n2);
376
    gr.addArc(n0, n2);
377
    gr.addArc(n2, n1);
378
    gr.addArc(n0, n1);
379
    
380
    {
381
      std::vector<Node> v;
382
      dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run();
383

	
384
      check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
385
            "Something is wrong with LoggerBoolMap");
386
    }
387
    {
388
      std::vector<Node> v(countNodes(gr));
389
      dfs(gr).processedMap(loggerBoolMap(v.begin())).run();
390
      
391
      check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
392
            "Something is wrong with LoggerBoolMap");
393
    }
394
  }
395
  
396
  // IdMap, RangeIdMap
397
  {
398
    typedef ListDigraph Graph;
399
    DIGRAPH_TYPEDEFS(Graph);
400

	
401
    checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
402
    checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
403
    checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
404
    checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
405
    
406
    Graph gr;
407
    IdMap<Graph, Node> nmap(gr);
408
    IdMap<Graph, Arc> amap(gr);
409
    RangeIdMap<Graph, Node> nrmap(gr);
410
    RangeIdMap<Graph, Arc> armap(gr);
411
    
412
    Node n0 = gr.addNode();
413
    Node n1 = gr.addNode();
414
    Node n2 = gr.addNode();
415
    
416
    Arc a0 = gr.addArc(n0, n1);
417
    Arc a1 = gr.addArc(n0, n2);
418
    Arc a2 = gr.addArc(n2, n1);
419
    Arc a3 = gr.addArc(n2, n0);
420
    
421
    check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
422
    check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
423
    check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
424

	
425
    check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
426
    check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
427
    check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
428
    check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
429

	
430
    check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
431
    check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
432
    
433
    check(nrmap.size() == 3 && armap.size() == 4,
434
          "Wrong RangeIdMap::size()");
435

	
436
    check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
437
    check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
438
    check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
439
    
440
    check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
441
    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
442
    check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
443
    check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
444

	
445
    check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
446
    check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
447
    
448
    gr.erase(n1);
449
    
450
    if (nrmap[n0] == 1) nrmap.swap(n0, n2);
451
    nrmap.swap(n2, n0);
452
    if (armap[a1] == 1) armap.swap(a1, a3);
453
    armap.swap(a3, a1);
454
    
455
    check(nrmap.size() == 2 && armap.size() == 2,
456
          "Wrong RangeIdMap::size()");
457

	
458
    check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
459
    check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
460
    
461
    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
462
    check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
463

	
464
    check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
465
    check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
466
  }
467
  
468
  // SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap
469
  {
470
    typedef ListGraph Graph;
471
    GRAPH_TYPEDEFS(Graph);
472
    
473
    checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >();
474
    checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >();
475
    checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >();
476
    checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >();
477
    checkConcept<ReadMap<Node, int>, InDegMap<Graph> >();
478
    checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >();
479

	
480
    Graph gr;
481
    Node n0 = gr.addNode();
482
    Node n1 = gr.addNode();
483
    Node n2 = gr.addNode();
484
    
485
    gr.addEdge(n0,n1);
486
    gr.addEdge(n1,n2);
487
    gr.addEdge(n0,n2);
488
    gr.addEdge(n2,n1);
489
    gr.addEdge(n1,n2);
490
    gr.addEdge(n0,n1);
491
    
492
    for (EdgeIt e(gr); e != INVALID; ++e) {
493
      check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap");
494
      check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap");
495
    }
496
    
497
    compareMap(sourceMap(orienter(gr, constMap<Edge, bool>(true))),
498
               targetMap(orienter(gr, constMap<Edge, bool>(false))),
499
               EdgeIt(gr));
500

	
501
    typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph;
502
    Digraph dgr(gr, constMap<Edge, bool>(true));
503
    OutDegMap<Digraph> odm(dgr);
504
    InDegMap<Digraph> idm(dgr);
505
    
506
    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap");
507
    check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
508
   
509
    gr.addEdge(n2, n0);
510

	
511
    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap");
512
    check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
513
  }
514
  
515
  // CrossRefMap
516
  {
517
    typedef ListDigraph Graph;
518
    DIGRAPH_TYPEDEFS(Graph);
519

	
520
    checkConcept<ReadWriteMap<Node, int>,
521
                 CrossRefMap<Graph, Node, int> >();
522
    checkConcept<ReadWriteMap<Node, bool>,
523
                 CrossRefMap<Graph, Node, bool> >();
524
    checkConcept<ReadWriteMap<Node, double>,
525
                 CrossRefMap<Graph, Node, double> >();
526
    
527
    Graph gr;
528
    typedef CrossRefMap<Graph, Node, char> CRMap;
529
    CRMap map(gr);
530
    
531
    Node n0 = gr.addNode();
532
    Node n1 = gr.addNode();
533
    Node n2 = gr.addNode();
534
    
535
    map.set(n0, 'A');
536
    map.set(n1, 'B');
537
    map.set(n2, 'C');
538
    
539
    check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
540
          "Wrong CrossRefMap");
541
    check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
542
          "Wrong CrossRefMap");
543
    check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
544
          "Wrong CrossRefMap");
545
    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
546
          "Wrong CrossRefMap::count()");
547
    
548
    CRMap::ValueIt it = map.beginValue();
549
    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
550
          it == map.endValue(), "Wrong value iterator");
551
    
552
    map.set(n2, 'A');
553

	
554
    check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
555
          "Wrong CrossRefMap");
556
    check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
557
    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
558
    check(map('C') == INVALID && map.inverse()['C'] == INVALID,
559
          "Wrong CrossRefMap");
560
    check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
561
          "Wrong CrossRefMap::count()");
562

	
563
    it = map.beginValue();
564
    check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
565
          it == map.endValue(), "Wrong value iterator");
566

	
567
    map.set(n0, 'C');
568

	
569
    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
570
          "Wrong CrossRefMap");
571
    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
572
    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
573
    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
574
    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
575
          "Wrong CrossRefMap::count()");
576

	
577
    it = map.beginValue();
578
    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
579
          it == map.endValue(), "Wrong value iterator");
350 580
  }
351 581

	
582
  // CrossRefMap
583
  {
584
    typedef SmartDigraph Graph;
585
    DIGRAPH_TYPEDEFS(Graph);
586

	
587
    checkConcept<ReadWriteMap<Node, int>,
588
                 CrossRefMap<Graph, Node, int> >();
589
    
590
    Graph gr;
591
    typedef CrossRefMap<Graph, Node, char> CRMap;
592
    typedef CRMap::ValueIterator ValueIt;
593
    CRMap map(gr);
594
    
595
    Node n0 = gr.addNode();
596
    Node n1 = gr.addNode();
597
    Node n2 = gr.addNode();
598
    
599
    map.set(n0, 'A');
600
    map.set(n1, 'B');
601
    map.set(n2, 'C');
602
    map.set(n2, 'A');
603
    map.set(n0, 'C');
604

	
605
    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
606
          "Wrong CrossRefMap");
607
    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
608
    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
609
    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
610

	
611
    ValueIt it = map.beginValue();
612
    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
613
          it == map.endValue(), "Wrong value iterator");
614
  }
615
  
616
  // Iterable bool map
617
  {
618
    typedef SmartGraph Graph;
619
    typedef SmartGraph::Node Item;
620

	
621
    typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
622
    checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
623

	
624
    const int num = 10;
625
    Graph g;
626
    std::vector<Item> items;
627
    for (int i = 0; i < num; ++i) {
628
      items.push_back(g.addNode());
629
    }
630

	
631
    Ibm map1(g, true);
632
    int n = 0;
633
    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
634
      check(map1[static_cast<Item>(it)], "Wrong TrueIt");
635
      ++n;
636
    }
637
    check(n == num, "Wrong number");
638

	
639
    n = 0;
640
    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
641
        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
642
        ++n;
643
    }
644
    check(n == num, "Wrong number");
645
    check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
646
    check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
647

	
648
    map1[items[5]] = true;
649

	
650
    n = 0;
651
    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
652
        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
653
        ++n;
654
    }
655
    check(n == num, "Wrong number");
656

	
657
    map1[items[num / 2]] = false;
658
    check(map1[items[num / 2]] == false, "Wrong map value");
659

	
660
    n = 0;
661
    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
662
        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
663
        ++n;
664
    }
665
    check(n == num - 1, "Wrong number");
666

	
667
    n = 0;
668
    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
669
        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
670
        ++n;
671
    }
672
    check(n == 1, "Wrong number");
673

	
674
    map1[items[0]] = false;
675
    check(map1[items[0]] == false, "Wrong map value");
676

	
677
    map1[items[num - 1]] = false;
678
    check(map1[items[num - 1]] == false, "Wrong map value");
679

	
680
    n = 0;
681
    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
682
        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
683
        ++n;
684
    }
685
    check(n == num - 3, "Wrong number");
686
    check(map1.trueNum() == num - 3, "Wrong number");
687

	
688
    n = 0;
689
    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
690
        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
691
        ++n;
692
    }
693
    check(n == 3, "Wrong number");
694
    check(map1.falseNum() == 3, "Wrong number");
695
  }
696

	
697
  // Iterable int map
698
  {
699
    typedef SmartGraph Graph;
700
    typedef SmartGraph::Node Item;
701
    typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
702

	
703
    checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
704

	
705
    const int num = 10;
706
    Graph g;
707
    std::vector<Item> items;
708
    for (int i = 0; i < num; ++i) {
709
      items.push_back(g.addNode());
710
    }
711

	
712
    Iim map1(g);
713
    check(map1.size() == 0, "Wrong size");
714

	
715
    for (int i = 0; i < num; ++i) {
716
      map1[items[i]] = i;
717
    }
718
    check(map1.size() == num, "Wrong size");
719

	
720
    for (int i = 0; i < num; ++i) {
721
      Iim::ItemIt it(map1, i);
722
      check(static_cast<Item>(it) == items[i], "Wrong value");
723
      ++it;
724
      check(static_cast<Item>(it) == INVALID, "Wrong value");
725
    }
726

	
727
    for (int i = 0; i < num; ++i) {
728
      map1[items[i]] = i % 2;
729
    }
730
    check(map1.size() == 2, "Wrong size");
731

	
732
    int n = 0;
733
    for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
734
      check(map1[static_cast<Item>(it)] == 0, "Wrong value");
735
      ++n;
736
    }
737
    check(n == (num + 1) / 2, "Wrong number");
738

	
739
    for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
740
      check(map1[static_cast<Item>(it)] == 1, "Wrong value");
741
      ++n;
742
    }
743
    check(n == num, "Wrong number");
744

	
745
  }
746

	
747
  // Iterable value map
748
  {
749
    typedef SmartGraph Graph;
750
    typedef SmartGraph::Node Item;
751
    typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
752

	
753
    checkConcept<ReadWriteMap<Item, double>, Ivm>();
754

	
755
    const int num = 10;
756
    Graph g;
757
    std::vector<Item> items;
758
    for (int i = 0; i < num; ++i) {
759
      items.push_back(g.addNode());
760
    }
761

	
762
    Ivm map1(g, 0.0);
763
    check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
764
    check(*map1.beginValue() == 0.0, "Wrong value");
765

	
766
    for (int i = 0; i < num; ++i) {
767
      map1.set(items[i], static_cast<double>(i));
768
    }
769
    check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
770

	
771
    for (int i = 0; i < num; ++i) {
772
      Ivm::ItemIt it(map1, static_cast<double>(i));
773
      check(static_cast<Item>(it) == items[i], "Wrong value");
774
      ++it;
775
      check(static_cast<Item>(it) == INVALID, "Wrong value");
776
    }
777

	
778
    for (Ivm::ValueIt vit = map1.beginValue();
779
         vit != map1.endValue(); ++vit) {
780
      check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
781
            "Wrong ValueIt");
782
    }
783

	
784
    for (int i = 0; i < num; ++i) {
785
      map1.set(items[i], static_cast<double>(i % 2));
786
    }
787
    check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
788

	
789
    int n = 0;
790
    for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
791
      check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
792
      ++n;
793
    }
794
    check(n == (num + 1) / 2, "Wrong number");
795

	
796
    for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
797
      check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
798
      ++n;
799
    }
800
    check(n == num, "Wrong number");
801

	
802
  }
352 803
  return 0;
353 804
}
Ignore white space 6 line context
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-2009
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 <sstream>
21 21
#include <vector>
22 22
#include <queue>
23
#include <lemon/math.h>
24 23
#include <cstdlib>
25 24

	
26
#include <lemon/max_matching.h>
25
#include <lemon/matching.h>
27 26
#include <lemon/smart_graph.h>
27
#include <lemon/concepts/graph.h>
28
#include <lemon/concepts/maps.h>
28 29
#include <lemon/lgf_reader.h>
30
#include <lemon/math.h>
29 31

	
30 32
#include "test_tools.h"
31 33

	
32 34
using namespace std;
33 35
using namespace lemon;
34 36

	
35 37
GRAPH_TYPEDEFS(SmartGraph);
36 38

	
37 39

	
38 40
const int lgfn = 3;
39 41
const std::string lgf[lgfn] = {
40 42
  "@nodes\n"
41 43
  "label\n"
42 44
  "0\n"
43 45
  "1\n"
44 46
  "2\n"
45 47
  "3\n"
46 48
  "4\n"
47 49
  "5\n"
48 50
  "6\n"
49 51
  "7\n"
50 52
  "@edges\n"
51 53
  "     label  weight\n"
52 54
  "7 4  0      984\n"
53 55
  "0 7  1      73\n"
54 56
  "7 1  2      204\n"
55 57
  "2 3  3      583\n"
56 58
  "2 7  4      565\n"
57 59
  "2 1  5      582\n"
58 60
  "0 4  6      551\n"
59 61
  "2 5  7      385\n"
60 62
  "1 5  8      561\n"
61 63
  "5 3  9      484\n"
62 64
  "7 5  10     904\n"
63 65
  "3 6  11     47\n"
64 66
  "7 6  12     888\n"
65 67
  "3 0  13     747\n"
66 68
  "6 1  14     310\n",
67 69

	
68 70
  "@nodes\n"
69 71
  "label\n"
70 72
  "0\n"
71 73
  "1\n"
72 74
  "2\n"
73 75
  "3\n"
74 76
  "4\n"
75 77
  "5\n"
76 78
  "6\n"
77 79
  "7\n"
78 80
  "@edges\n"
79 81
  "     label  weight\n"
80 82
  "2 5  0      710\n"
81 83
  "0 5  1      241\n"
82 84
  "2 4  2      856\n"
83 85
  "2 6  3      762\n"
84 86
  "4 1  4      747\n"
85 87
  "6 1  5      962\n"
86 88
  "4 7  6      723\n"
87 89
  "1 7  7      661\n"
88 90
  "2 3  8      376\n"
89 91
  "1 0  9      416\n"
90 92
  "6 7  10     391\n",
91 93

	
92 94
  "@nodes\n"
93 95
  "label\n"
94 96
  "0\n"
95 97
  "1\n"
96 98
  "2\n"
97 99
  "3\n"
98 100
  "4\n"
99 101
  "5\n"
100 102
  "6\n"
101 103
  "7\n"
102 104
  "@edges\n"
103 105
  "     label  weight\n"
104 106
  "6 2  0      553\n"
105 107
  "0 7  1      653\n"
106 108
  "6 3  2      22\n"
107 109
  "4 7  3      846\n"
108 110
  "7 2  4      981\n"
109 111
  "7 6  5      250\n"
110 112
  "5 2  6      539\n",
111 113
};
112 114

	
115
void checkMaxMatchingCompile()
116
{
117
  typedef concepts::Graph Graph;
118
  typedef Graph::Node Node;
119
  typedef Graph::Edge Edge;
120
  typedef Graph::EdgeMap<bool> MatMap;
121

	
122
  Graph g;
123
  Node n;
124
  Edge e;
125
  MatMap mat(g);
126

	
127
  MaxMatching<Graph> mat_test(g);
128
  const MaxMatching<Graph>&
129
    const_mat_test = mat_test;
130

	
131
  mat_test.init();
132
  mat_test.greedyInit();
133
  mat_test.matchingInit(mat);
134
  mat_test.startSparse();
135
  mat_test.startDense();
136
  mat_test.run();
137
  
138
  const_mat_test.matchingSize();
139
  const_mat_test.matching(e);
140
  const_mat_test.matching(n);
141
  const MaxMatching<Graph>::MatchingMap& mmap =
142
    const_mat_test.matchingMap();
143
  e = mmap[n];
144
  const_mat_test.mate(n);
145

	
146
  MaxMatching<Graph>::Status stat = 
147
    const_mat_test.status(n);
148
  const MaxMatching<Graph>::StatusMap& smap =
149
    const_mat_test.statusMap();
150
  stat = smap[n];
151
  const_mat_test.barrier(n);
152
}
153

	
154
void checkMaxWeightedMatchingCompile()
155
{
156
  typedef concepts::Graph Graph;
157
  typedef Graph::Node Node;
158
  typedef Graph::Edge Edge;
159
  typedef Graph::EdgeMap<int> WeightMap;
160

	
161
  Graph g;
162
  Node n;
163
  Edge e;
164
  WeightMap w(g);
165

	
166
  MaxWeightedMatching<Graph> mat_test(g, w);
167
  const MaxWeightedMatching<Graph>&
168
    const_mat_test = mat_test;
169

	
170
  mat_test.init();
171
  mat_test.start();
172
  mat_test.run();
173
  
174
  const_mat_test.matchingWeight();
175
  const_mat_test.matchingSize();
176
  const_mat_test.matching(e);
177
  const_mat_test.matching(n);
178
  const MaxWeightedMatching<Graph>::MatchingMap& mmap =
179
    const_mat_test.matchingMap();
180
  e = mmap[n];
181
  const_mat_test.mate(n);
182
  
183
  int k = 0;
184
  const_mat_test.dualValue();
185
  const_mat_test.nodeValue(n);
186
  const_mat_test.blossomNum();
187
  const_mat_test.blossomSize(k);
188
  const_mat_test.blossomValue(k);
189
}
190

	
191
void checkMaxWeightedPerfectMatchingCompile()
192
{
193
  typedef concepts::Graph Graph;
194
  typedef Graph::Node Node;
195
  typedef Graph::Edge Edge;
196
  typedef Graph::EdgeMap<int> WeightMap;
197

	
198
  Graph g;
199
  Node n;
200
  Edge e;
201
  WeightMap w(g);
202

	
203
  MaxWeightedPerfectMatching<Graph> mat_test(g, w);
204
  const MaxWeightedPerfectMatching<Graph>&
205
    const_mat_test = mat_test;
206

	
207
  mat_test.init();
208
  mat_test.start();
209
  mat_test.run();
210
  
211
  const_mat_test.matchingWeight();
212
  const_mat_test.matching(e);
213
  const_mat_test.matching(n);
214
  const MaxWeightedPerfectMatching<Graph>::MatchingMap& mmap =
215
    const_mat_test.matchingMap();
216
  e = mmap[n];
217
  const_mat_test.mate(n);
218
  
219
  int k = 0;
220
  const_mat_test.dualValue();
221
  const_mat_test.nodeValue(n);
222
  const_mat_test.blossomNum();
223
  const_mat_test.blossomSize(k);
224
  const_mat_test.blossomValue(k);
225
}
226

	
113 227
void checkMatching(const SmartGraph& graph,
114 228
                   const MaxMatching<SmartGraph>& mm) {
115 229
  int num = 0;
116 230

	
117 231
  IntNodeMap comp_index(graph);
118 232
  UnionFind<IntNodeMap> comp(comp_index);
119 233

	
120 234
  int barrier_num = 0;
121 235

	
122 236
  for (NodeIt n(graph); n != INVALID; ++n) {
123
    check(mm.decomposition(n) == MaxMatching<SmartGraph>::EVEN ||
237
    check(mm.status(n) == MaxMatching<SmartGraph>::EVEN ||
124 238
          mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition");
125
    if (mm.decomposition(n) == MaxMatching<SmartGraph>::ODD) {
239
    if (mm.status(n) == MaxMatching<SmartGraph>::ODD) {
126 240
      ++barrier_num;
127 241
    } else {
128 242
      comp.insert(n);
129 243
    }
130 244
  }
131 245

	
132 246
  for (EdgeIt e(graph); e != INVALID; ++e) {
133 247
    if (mm.matching(e)) {
134 248
      check(e == mm.matching(graph.u(e)), "Wrong matching");
135 249
      check(e == mm.matching(graph.v(e)), "Wrong matching");
136 250
      ++num;
137 251
    }
138
    check(mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
139
          mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
252
    check(mm.status(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
253
          mm.status(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
140 254
          "Wrong Gallai-Edmonds decomposition");
141 255

	
142
    check(mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
143
          mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
256
    check(mm.status(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
257
          mm.status(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
144 258
          "Wrong Gallai-Edmonds decomposition");
145 259

	
146
    if (mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
147
        mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
260
    if (mm.status(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
261
        mm.status(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
148 262
      comp.join(graph.u(e), graph.v(e));
149 263
    }
150 264
  }
151 265

	
152 266
  std::set<int> comp_root;
153 267
  int odd_comp_num = 0;
154 268
  for (NodeIt n(graph); n != INVALID; ++n) {
155
    if (mm.decomposition(n) != MaxMatching<SmartGraph>::ODD) {
269
    if (mm.status(n) != MaxMatching<SmartGraph>::ODD) {
156 270
      int root = comp.find(n);
157 271
      if (comp_root.find(root) == comp_root.end()) {
158 272
        comp_root.insert(root);
159 273
        if (comp.size(n) % 2 == 1) {
160 274
          ++odd_comp_num;
161 275
        }
162 276
      }
163 277
    }
164 278
  }
165 279

	
166 280
  check(mm.matchingSize() == num, "Wrong matching");
167 281
  check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num),
168 282
         "Wrong matching");
169 283
  return;
170 284
}
171 285

	
172 286
void checkWeightedMatching(const SmartGraph& graph,
173 287
                   const SmartGraph::EdgeMap<int>& weight,
174 288
                   const MaxWeightedMatching<SmartGraph>& mwm) {
175 289
  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
176 290
    if (graph.u(e) == graph.v(e)) continue;
177 291
    int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
178 292

	
179 293
    for (int i = 0; i < mwm.blossomNum(); ++i) {
180 294
      bool s = false, t = false;
181 295
      for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
182 296
           n != INVALID; ++n) {
183 297
        if (graph.u(e) == n) s = true;
184 298
        if (graph.v(e) == n) t = true;
185 299
      }
186 300
      if (s == true && t == true) {
187 301
        rw += mwm.blossomValue(i);
188 302
      }
189 303
    }
190 304
    rw -= weight[e] * mwm.dualScale;
191 305

	
192 306
    check(rw >= 0, "Negative reduced weight");
193 307
    check(rw == 0 || !mwm.matching(e),
194 308
          "Non-zero reduced weight on matching edge");
195 309
  }
196 310

	
197 311
  int pv = 0;
198 312
  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
199 313
    if (mwm.matching(n) != INVALID) {
200 314
      check(mwm.nodeValue(n) >= 0, "Invalid node value");
201 315
      pv += weight[mwm.matching(n)];
202 316
      SmartGraph::Node o = graph.target(mwm.matching(n));
203 317
      check(mwm.mate(n) == o, "Invalid matching");
204 318
      check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
205 319
            "Invalid matching");
206 320
    } else {
207 321
      check(mwm.mate(n) == INVALID, "Invalid matching");
208 322
      check(mwm.nodeValue(n) == 0, "Invalid matching");
209 323
    }
210 324
  }
211 325

	
212 326
  int dv = 0;
213 327
  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
214 328
    dv += mwm.nodeValue(n);
215 329
  }
216 330

	
217 331
  for (int i = 0; i < mwm.blossomNum(); ++i) {
218 332
    check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
219 333
    check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
220 334
    dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
221 335
  }
222 336

	
223 337
  check(pv * mwm.dualScale == dv * 2, "Wrong duality");
224 338

	
225 339
  return;
226 340
}
227 341

	
228 342
void checkWeightedPerfectMatching(const SmartGraph& graph,
229 343
                          const SmartGraph::EdgeMap<int>& weight,
230 344
                          const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
231 345
  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
232 346
    if (graph.u(e) == graph.v(e)) continue;
233 347
    int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
234 348

	
235 349
    for (int i = 0; i < mwpm.blossomNum(); ++i) {
236 350
      bool s = false, t = false;
237 351
      for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
238 352
           n != INVALID; ++n) {
239 353
        if (graph.u(e) == n) s = true;
240 354
        if (graph.v(e) == n) t = true;
241 355
      }
242 356
      if (s == true && t == true) {
243 357
        rw += mwpm.blossomValue(i);
244 358
      }
245 359
    }
246 360
    rw -= weight[e] * mwpm.dualScale;
247 361

	
248 362
    check(rw >= 0, "Negative reduced weight");
249 363
    check(rw == 0 || !mwpm.matching(e),
250 364
          "Non-zero reduced weight on matching edge");
251 365
  }
Ignore white space 6 line context
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
 * Copyright (C) 2003-2008
5
 * Copyright (C) 2003-2009
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 "test_tools.h"
20 20

	
21
#include <lemon/config.h>
21 22

	
22
#ifdef HAVE_CONFIG_H
23
#include <lemon/config.h>
24
#endif
25

	
26
#ifdef HAVE_CPLEX
23
#ifdef LEMON_HAVE_CPLEX
27 24
#include <lemon/cplex.h>
28 25
#endif
29 26

	
30
#ifdef HAVE_GLPK
27
#ifdef LEMON_HAVE_GLPK
31 28
#include <lemon/glpk.h>
32 29
#endif
33 30

	
31
#ifdef LEMON_HAVE_CBC
32
#include <lemon/cbc.h>
33
#endif
34

	
34 35

	
35 36
using namespace lemon;
36 37

	
37 38
void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat,
38 39
                   double exp_opt) {
39 40
  using std::string;
40 41

	
41 42
  mip.solve();
42 43
  //int decimal,sign;
43 44
  std::ostringstream buf;
44 45
  buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type());
45 46

	
46 47

	
47 48
  //  itoa(stat,buf1, 10);
48 49
  check(mip.type()==stat, buf.str());
49 50

	
50 51
  if (stat ==  MipSolver::OPTIMAL) {
51 52
    std::ostringstream sbuf;
52
    buf << "Wrong optimal value: the right optimum is " << exp_opt;
53
    sbuf << "Wrong optimal value ("<< mip.solValue()
54
         <<" instead of " << exp_opt << ")";
53 55
    check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str());
54 56
    //+ecvt(exp_opt,2)
55 57
  }
56 58
}
57 59

	
58 60
void aTest(MipSolver& mip)
59 61
{
60
 //The following example is very simple
62
  //The following example is very simple
61 63

	
62 64

	
63 65
  typedef MipSolver::Row Row;
64 66
  typedef MipSolver::Col Col;
65 67

	
66 68

	
67

	
68 69
  Col x1 = mip.addCol();
69 70
  Col x2 = mip.addCol();
70 71

	
71 72

	
72 73
  //Objective function
73 74
  mip.obj(x1);
74 75

	
75 76
  mip.max();
76 77

	
77

	
78 78
  //Unconstrained optimization
79 79
  mip.solve();
80 80
  //Check it out!
81 81

	
82 82
  //Constraints
83
  mip.addRow(2*x1+x2 <=2);
84
  mip.addRow(x1-2*x2 <=0);
83
  mip.addRow(2 * x1 + x2 <= 2);
84
  Row y2 = mip.addRow(x1 - 2 * x2 <= 0);
85 85

	
86 86
  //Nonnegativity of the variable x1
87 87
  mip.colLowerBound(x1, 0);
88 88

	
89

	
89 90
  //Maximization of x1
90 91
  //over the triangle with vertices (0,0),(4/5,2/5),(0,2)
91 92
  double expected_opt=4.0/5.0;
92 93
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
93 94

	
95

	
94 96
  //Restrict x2 to integer
95 97
  mip.colType(x2,MipSolver::INTEGER);
96 98
  expected_opt=1.0/2.0;
97 99
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
98 100

	
99 101

	
100 102
  //Restrict both to integer
101 103
  mip.colType(x1,MipSolver::INTEGER);
102 104
  expected_opt=0;
103 105
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
104 106

	
105

	
107
  //Erase a variable
108
  mip.erase(x2);
109
  mip.rowUpperBound(y2, 8);
110
  expected_opt=1;
111
  solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt);
106 112

	
107 113
}
108 114

	
109 115

	
116
template<class MIP>
117
void cloneTest()
118
{
119

	
120
  MIP* mip = new MIP();
121
  MIP* mipnew = mip->newSolver();
122
  MIP* mipclone = mip->cloneSolver();
123
  delete mip;
124
  delete mipnew;
125
  delete mipclone;
126
}
127

	
110 128
int main()
111 129
{
112 130

	
113
#ifdef HAVE_GLPK
131
#ifdef LEMON_HAVE_GLPK
114 132
  {
115 133
    GlpkMip mip1;
116 134
    aTest(mip1);
135
    cloneTest<GlpkMip>();
117 136
  }
118 137
#endif
119 138

	
120
#ifdef HAVE_CPLEX
139
#ifdef LEMON_HAVE_CPLEX
121 140
  try {
122 141
    CplexMip mip2;
123 142
    aTest(mip2);
143
    cloneTest<CplexMip>();
124 144
  } catch (CplexEnv::LicenseError& error) {
125
#ifdef LEMON_FORCE_CPLEX_CHECK
126 145
    check(false, error.what());
127
#else
128
    std::cerr << error.what() << std::endl;
129
    std::cerr << "Cplex license check failed, lp check skipped" << std::endl;
146
  }
130 147
#endif
148

	
149
#ifdef LEMON_HAVE_CBC
150
  {
151
    CbcMip mip1;
152
    aTest(mip1);
153
    cloneTest<CbcMip>();
131 154
  }
132 155
#endif
133 156

	
134 157
  return 0;
135 158

	
136 159
}
Ignore white space 6 line context
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-2009
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

	
21 21
#include "test_tools.h"
22 22
#include <lemon/smart_graph.h>
23 23
#include <lemon/preflow.h>
24 24
#include <lemon/concepts/digraph.h>
25 25
#include <lemon/concepts/maps.h>
26 26
#include <lemon/lgf_reader.h>
27 27
#include <lemon/elevator.h>
28 28

	
29 29
using namespace lemon;
30 30

	
31 31
char test_lgf[] =
32 32
  "@nodes\n"
33 33
  "label\n"
34 34
  "0\n"
35 35
  "1\n"
36 36
  "2\n"
37 37
  "3\n"
38 38
  "4\n"
39 39
  "5\n"
40 40
  "6\n"
41 41
  "7\n"
42 42
  "8\n"
43 43
  "9\n"
44 44
  "@arcs\n"
45 45
  "    label capacity\n"
46 46
  "0 1 0     20\n"
47 47
  "0 2 1     0\n"
48 48
  "1 1 2     3\n"
49 49
  "1 2 3     8\n"
50 50
  "1 3 4     8\n"
51 51
  "2 5 5     5\n"
52 52
  "3 2 6     5\n"
53 53
  "3 5 7     5\n"
54 54
  "3 6 8     5\n"
55 55
  "4 3 9     3\n"
56 56
  "5 7 10    3\n"
57 57
  "5 6 11    10\n"
58 58
  "5 8 12    10\n"
59 59
  "6 8 13    8\n"
60 60
  "8 9 14    20\n"
61 61
  "8 1 15    5\n"
62 62
  "9 5 16    5\n"
63 63
  "@attributes\n"
64 64
  "source 1\n"
65 65
  "target 8\n";
66 66

	
67 67
void checkPreflowCompile()
68 68
{
69 69
  typedef int VType;
70 70
  typedef concepts::Digraph Digraph;
71 71

	
72 72
  typedef Digraph::Node Node;
73 73
  typedef Digraph::Arc Arc;
74 74
  typedef concepts::ReadMap<Arc,VType> CapMap;
75 75
  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
76 76
  typedef concepts::WriteMap<Node,bool> CutMap;
77 77

	
78 78
  typedef Elevator<Digraph, Digraph::Node> Elev;
79 79
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
80 80

	
81 81
  Digraph g;
82 82
  Node n;
83 83
  Arc e;
84 84
  CapMap cap;
85 85
  FlowMap flow;
86 86
  CutMap cut;
87
  VType v;
88
  bool b;
87 89

	
88
  Preflow<Digraph, CapMap>
89
    ::SetFlowMap<FlowMap>
90
    ::SetElevator<Elev>
91
    ::SetStandardElevator<LinkedElev>
92
    ::Create preflow_test(g,cap,n,n);
90
  typedef Preflow<Digraph, CapMap>
91
            ::SetFlowMap<FlowMap>
92
            ::SetElevator<Elev>
93
            ::SetStandardElevator<LinkedElev>
94
            ::Create PreflowType;
95
  PreflowType preflow_test(g, cap, n, n);
96
  const PreflowType& const_preflow_test = preflow_test;
97
  
98
  const PreflowType::Elevator& elev = const_preflow_test.elevator();
99
  preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
100
  PreflowType::Tolerance tol = const_preflow_test.tolerance();
101
  preflow_test.tolerance(tol);
93 102

	
94
  preflow_test.capacityMap(cap);
95
  flow = preflow_test.flowMap();
96
  preflow_test.flowMap(flow);
97
  preflow_test.source(n);
98
  preflow_test.target(n);
103
  preflow_test
104
    .capacityMap(cap)
105
    .flowMap(flow)
106
    .source(n)
107
    .target(n);
99 108

	
100 109
  preflow_test.init();
101 110
  preflow_test.init(cap);
102 111
  preflow_test.startFirstPhase();
103 112
  preflow_test.startSecondPhase();
104 113
  preflow_test.run();
105 114
  preflow_test.runMinCut();
106 115

	
107
  preflow_test.flowValue();
108
  preflow_test.minCut(n);
109
  preflow_test.minCutMap(cut);
110
  preflow_test.flow(e);
111

	
116
  v = const_preflow_test.flowValue();
117
  v = const_preflow_test.flow(e);
118
  const FlowMap& fm = const_preflow_test.flowMap();
119
  b = const_preflow_test.minCut(n);
120
  const_preflow_test.minCutMap(cut);
121
  
122
  ignore_unused_variable_warning(fm);
112 123
}
113 124

	
114 125
int cutValue (const SmartDigraph& g,
115 126
              const SmartDigraph::NodeMap<bool>& cut,
116 127
              const SmartDigraph::ArcMap<int>& cap) {
117 128

	
118 129
  int c=0;
119 130
  for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
120 131
    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
121 132
  }
122 133
  return c;
123 134
}
124 135

	
125 136
bool checkFlow(const SmartDigraph& g,
126 137
               const SmartDigraph::ArcMap<int>& flow,
127 138
               const SmartDigraph::ArcMap<int>& cap,
128 139
               SmartDigraph::Node s, SmartDigraph::Node t) {
129 140

	
130 141
  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
131 142
    if (flow[e] < 0 || flow[e] > cap[e]) return false;
132 143
  }
133 144

	
134 145
  for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
135 146
    if (n == s || n == t) continue;
136 147
    int sum = 0;
137 148
    for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
138 149
      sum += flow[e];
139 150
    }
140 151
    for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
141 152
      sum -= flow[e];
142 153
    }
143 154
    if (sum != 0) return false;
144 155
  }
145 156
  return true;
146 157
}
147 158

	
148 159
int main() {
149 160

	
150 161
  typedef SmartDigraph Digraph;
151 162

	
152 163
  typedef Digraph::Node Node;
153 164
  typedef Digraph::NodeIt NodeIt;
154 165
  typedef Digraph::ArcIt ArcIt;
155 166
  typedef Digraph::ArcMap<int> CapMap;
156 167
  typedef Digraph::ArcMap<int> FlowMap;
157 168
  typedef Digraph::NodeMap<bool> CutMap;
158 169

	
159 170
  typedef Preflow<Digraph, CapMap> PType;
160 171

	
161 172
  Digraph g;
162 173
  Node s, t;
163 174
  CapMap cap(g);
164 175
  std::istringstream input(test_lgf);
165 176
  DigraphReader<Digraph>(g,input).
166 177
    arcMap("capacity", cap).
167 178
    node("source",s).
168 179
    node("target",t).
169 180
    run();
170 181

	
171 182
  PType preflow_test(g, cap, s, t);
172 183
  preflow_test.run();
173 184

	
174 185
  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
175 186
        "The flow is not feasible.");
176 187

	
177 188
  CutMap min_cut(g);
178 189
  preflow_test.minCutMap(min_cut);
179 190
  int min_cut_value=cutValue(g,min_cut,cap);
180 191

	
181 192
  check(preflow_test.flowValue() == min_cut_value,
182 193
        "The max flow value is not equal to the three min cut values.");
183 194

	
184 195
  FlowMap flow(g);
185 196
  for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
186 197

	
187 198
  int flow_value=preflow_test.flowValue();
188 199

	
189 200
  for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
190 201
  preflow_test.init(flow);
191 202
  preflow_test.startFirstPhase();
192 203

	
193 204
  CutMap min_cut1(g);
194 205
  preflow_test.minCutMap(min_cut1);
195 206
  min_cut_value=cutValue(g,min_cut1,cap);
196 207

	
197 208
  check(preflow_test.flowValue() == min_cut_value &&
198 209
        min_cut_value == 2*flow_value,
199 210
        "The max flow value or the min cut value is wrong.");
200 211

	
201 212
  preflow_test.startSecondPhase();
202 213

	
203 214
  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
204 215
        "The flow is not feasible.");
205 216

	
206 217
  CutMap min_cut2(g);
207 218
  preflow_test.minCutMap(min_cut2);
Ignore white space 6 line context
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-2009
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

	
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23 23
#include <lemon/path.h>
24 24
#include <lemon/suurballe.h>
25
#include <lemon/concepts/digraph.h>
25 26

	
26 27
#include "test_tools.h"
27 28

	
28 29
using namespace lemon;
29 30

	
30 31
char test_lgf[] =
31 32
  "@nodes\n"
32
  "label supply1 supply2 supply3\n"
33
  "1     0        20      27\n"
34
  "2     0       -4        0\n"
35
  "3     0        0        0\n"
36
  "4     0        0        0\n"
37
  "5     0        9        0\n"
38
  "6     0       -6        0\n"
39
  "7     0        0        0\n"
40
  "8     0        0        0\n"
41
  "9     0        3        0\n"
42
  "10    0       -2        0\n"
43
  "11    0        0        0\n"
44
  "12    0       -20     -27\n"
33
  "label\n"
34
  "1\n"
35
  "2\n"
36
  "3\n"
37
  "4\n"
38
  "5\n"
39
  "6\n"
40
  "7\n"
41
  "8\n"
42
  "9\n"
43
  "10\n"
44
  "11\n"
45
  "12\n"
45 46
  "@arcs\n"
46
  "      cost capacity lower1 lower2\n"
47
  " 1  2  70  11       0      8\n"
48
  " 1  3 150   3       0      1\n"
49
  " 1  4  80  15       0      2\n"
50
  " 2  8  80  12       0      0\n"
51
  " 3  5 140   5       0      3\n"
52
  " 4  6  60  10       0      1\n"
53
  " 4  7  80   2       0      0\n"
54
  " 4  8 110   3       0      0\n"
55
  " 5  7  60  14       0      0\n"
56
  " 5 11 120  12       0      0\n"
57
  " 6  3   0   3       0      0\n"
58
  " 6  9 140   4       0      0\n"
59
  " 6 10  90   8       0      0\n"
60
  " 7  1  30   5       0      0\n"
61
  " 8 12  60  16       0      4\n"
62
  " 9 12  50   6       0      0\n"
63
  "10 12  70  13       0      5\n"
64
  "10  2 100   7       0      0\n"
65
  "10  7  60  10       0      0\n"
66
  "11 10  20  14       0      6\n"
67
  "12 11  30  10       0      0\n"
47
  "      length\n"
48
  " 1  2  70\n"
49
  " 1  3 150\n"
50
  " 1  4  80\n"
51
  " 2  8  80\n"
52
  " 3  5 140\n"
53
  " 4  6  60\n"
54
  " 4  7  80\n"
55
  " 4  8 110\n"
56
  " 5  7  60\n"
57
  " 5 11 120\n"
58
  " 6  3   0\n"
59
  " 6  9 140\n"
60
  " 6 10  90\n"
61
  " 7  1  30\n"
62
  " 8 12  60\n"
63
  " 9 12  50\n"
64
  "10 12  70\n"
65
  "10  2 100\n"
66
  "10  7  60\n"
67
  "11 10  20\n"
68
  "12 11  30\n"
68 69
  "@attributes\n"
69 70
  "source  1\n"
70 71
  "target 12\n"
71 72
  "@end\n";
72 73

	
74
// Check the interface of Suurballe
75
void checkSuurballeCompile()
76
{
77
  typedef int VType;
78
  typedef concepts::Digraph Digraph;
79

	
80
  typedef Digraph::Node Node;
81
  typedef Digraph::Arc Arc;
82
  typedef concepts::ReadMap<Arc, VType> LengthMap;
83
  
84
  typedef Suurballe<Digraph, LengthMap> SuurballeType;
85

	
86
  Digraph g;
87
  Node n;
88
  Arc e;
89
  LengthMap len;
90
  SuurballeType::FlowMap flow(g);
91
  SuurballeType::PotentialMap pi(g);
92

	
93
  SuurballeType suurb_test(g, len);
94
  const SuurballeType& const_suurb_test = suurb_test;
95

	
96
  suurb_test
97
    .flowMap(flow)
98
    .potentialMap(pi);
99

	
100
  int k;
101
  k = suurb_test.run(n, n);
102
  k = suurb_test.run(n, n, k);
103
  suurb_test.init(n);
104
  k = suurb_test.findFlow(n);
105
  k = suurb_test.findFlow(n, k);
106
  suurb_test.findPaths();
107
  
108
  int f;
109
  VType c;
110
  c = const_suurb_test.totalLength();
111
  f = const_suurb_test.flow(e);
112
  const SuurballeType::FlowMap& fm =
113
    const_suurb_test.flowMap();
114
  c = const_suurb_test.potential(n);
115
  const SuurballeType::PotentialMap& pm =
116
    const_suurb_test.potentialMap();
117
  k = const_suurb_test.pathNum();
118
  Path<Digraph> p = const_suurb_test.path(k);
119
  
120
  ignore_unused_variable_warning(fm);
121
  ignore_unused_variable_warning(pm);
122
}
123

	
73 124
// Check the feasibility of the flow
74 125
template <typename Digraph, typename FlowMap>
75 126
bool checkFlow( const Digraph& gr, const FlowMap& flow,
76 127
                typename Digraph::Node s, typename Digraph::Node t,
77 128
                int value )
78 129
{
79 130
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
80 131
  for (ArcIt e(gr); e != INVALID; ++e)
81 132
    if (!(flow[e] == 0 || flow[e] == 1)) return false;
82 133

	
83 134
  for (NodeIt n(gr); n != INVALID; ++n) {
84 135
    int sum = 0;
85 136
    for (OutArcIt e(gr, n); e != INVALID; ++e)
86 137
      sum += flow[e];
87 138
    for (InArcIt e(gr, n); e != INVALID; ++e)
88 139
      sum -= flow[e];
89 140
    if (n == s && sum != value) return false;
90 141
    if (n == t && sum != -value) return false;
91 142
    if (n != s && n != t && sum != 0) return false;
92 143
  }
93 144

	
94 145
  return true;
95 146
}
96 147

	
97 148
// Check the optimalitiy of the flow
98 149
template < typename Digraph, typename CostMap,
99 150
           typename FlowMap, typename PotentialMap >
100 151
bool checkOptimality( const Digraph& gr, const CostMap& cost,
101 152
                      const FlowMap& flow, const PotentialMap& pi )
102 153
{
103 154
  // Check the "Complementary Slackness" optimality condition
104 155
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
105 156
  bool opt = true;
106 157
  for (ArcIt e(gr); e != INVALID; ++e) {
107 158
    typename CostMap::Value red_cost =
108 159
      cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
109 160
    opt = (flow[e] == 0 && red_cost >= 0) ||
110 161
          (flow[e] == 1 && red_cost <= 0);
111 162
    if (!opt) break;
112 163
  }
113 164
  return opt;
114 165
}
115 166

	
116 167
// Check a path
117 168
template <typename Digraph, typename Path>
118 169
bool checkPath( const Digraph& gr, const Path& path,
119 170
                typename Digraph::Node s, typename Digraph::Node t)
120 171
{
121
  // Check the "Complementary Slackness" optimality condition
122 172
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
123 173
  Node n = s;
124 174
  for (int i = 0; i < path.length(); ++i) {
125 175
    if (gr.source(path.nth(i)) != n) return false;
126 176
    n = gr.target(path.nth(i));
127 177
  }
128 178
  return n == t;
129 179
}
130 180

	
131 181

	
132 182
int main()
133 183
{
134 184
  DIGRAPH_TYPEDEFS(ListDigraph);
135 185

	
136 186
  // Read the test digraph
137 187
  ListDigraph digraph;
138 188
  ListDigraph::ArcMap<int> length(digraph);
139
  Node source, target;
189
  Node s, t;
140 190

	
141 191
  std::istringstream input(test_lgf);
142 192
  DigraphReader<ListDigraph>(digraph, input).
143
    arcMap("cost", length).
144
    node("source", source).
145
    node("target", target).
193
    arcMap("length", length).
194
    node("source", s).
195
    node("target", t).
146 196
    run();
147 197

	
148 198
  // Find 2 paths
149 199
  {
150
    Suurballe<ListDigraph> suurballe(digraph, length, source, target);
151
    check(suurballe.run(2) == 2, "Wrong number of paths");
152
    check(checkFlow(digraph, suurballe.flowMap(), source, target, 2),
200
    Suurballe<ListDigraph> suurballe(digraph, length);
201
    check(suurballe.run(s, t) == 2, "Wrong number of paths");
202
    check(checkFlow(digraph, suurballe.flowMap(), s, t, 2),
153 203
          "The flow is not feasible");
154 204
    check(suurballe.totalLength() == 510, "The flow is not optimal");
155 205
    check(checkOptimality(digraph, length, suurballe.flowMap(),
156 206
                          suurballe.potentialMap()),
157 207
          "Wrong potentials");
158 208
    for (int i = 0; i < suurballe.pathNum(); ++i)
159
      check(checkPath(digraph, suurballe.path(i), source, target),
160
            "Wrong path");
209
      check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
161 210
  }
162 211

	
163 212
  // Find 3 paths
164 213
  {
165
    Suurballe<ListDigraph> suurballe(digraph, length, source, target);
166
    check(suurballe.run(3) == 3, "Wrong number of paths");
167
    check(checkFlow(digraph, suurballe.flowMap(), source, target, 3),
214
    Suurballe<ListDigraph> suurballe(digraph, length);
215
    check(suurballe.run(s, t, 3) == 3, "Wrong number of paths");
216
    check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
168 217
          "The flow is not feasible");
169 218
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
170 219
    check(checkOptimality(digraph, length, suurballe.flowMap(),
171 220
                          suurballe.potentialMap()),
172 221
          "Wrong potentials");
173 222
    for (int i = 0; i < suurballe.pathNum(); ++i)
174
      check(checkPath(digraph, suurballe.path(i), source, target),
175
            "Wrong path");
223
      check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
176 224
  }
177 225

	
178 226
  // Find 5 paths (only 3 can be found)
179 227
  {
180
    Suurballe<ListDigraph> suurballe(digraph, length, source, target);
181
    check(suurballe.run(5) == 3, "Wrong number of paths");
182
    check(checkFlow(digraph, suurballe.flowMap(), source, target, 3),
228
    Suurballe<ListDigraph> suurballe(digraph, length);
229
    check(suurballe.run(s, t, 5) == 3, "Wrong number of paths");
230
    check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
183 231
          "The flow is not feasible");
184 232
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
185 233
    check(checkOptimality(digraph, length, suurballe.flowMap(),
186 234
                          suurballe.potentialMap()),
187 235
          "Wrong potentials");
188 236
    for (int i = 0; i < suurballe.pathNum(); ++i)
189
      check(checkPath(digraph, suurballe.path(i), source, target),
190
            "Wrong path");
237
      check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
191 238
  }
192 239

	
193 240
  return 0;
194 241
}
Ignore white space 6 line context
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-2009
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_TEST_TEST_TOOLS_H
20 20
#define LEMON_TEST_TEST_TOOLS_H
21 21

	
22 22
///\ingroup misc
23 23
///\file
24 24
///\brief Some utilities to write test programs.
25 25

	
26 26
#include <iostream>
27 27
#include <stdlib.h>
28 28

	
29 29
///If \c rc is fail, writes an error message and exits.
30 30

	
31 31
///If \c rc is fail, writes an error message and exits.
32 32
///The error message contains the file name and the line number of the
33 33
///source code in a standard from, which makes it possible to go there
34 34
///using good source browsers like e.g. \c emacs.
35 35
///
36 36
///For example
37 37
///\code check(0==1,"This is obviously false.");\endcode will
38 38
///print something like this (and then exits).
39 39
///\verbatim file_name.cc:123: error: This is obviously false. \endverbatim
40
#define check(rc, msg) \
41
  if(!(rc)) { \
42
    std::cerr << __FILE__ ":" << __LINE__ << ": error: " << msg << std::endl; \
43
    abort(); \
44
  } else { } \
40
#define check(rc, msg)                                                  \
41
  {                                                                     \
42
    if(!(rc)) {                                                         \
43
      std::cerr << __FILE__ ":" << __LINE__ << ": error: "              \
44
                << msg << std::endl;                                    \
45
      abort();                                                          \
46
    } else { }                                                          \
47
  }                                                                     \
48
    
45 49

	
46 50
#endif
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/time_measure.h>
20 20

	
21 21
using namespace lemon;
22 22

	
23 23
void f()
24 24
{
25 25
  double d=0;
26 26
  for(int i=0;i<1000;i++)
27 27
    d+=0.1;
28 28
}
29 29

	
30 30
void g()
31 31
{
32 32
  static Timer T;
33 33

	
34 34
  for(int i=0;i<1000;i++)
35 35
    TimeStamp x(T);
36 36
}
37 37

	
38 38
int main()
39 39
{
40 40
  Timer T;
41 41
  unsigned int n;
42
  for(n=0;T.realTime()<1.0;n++) ;
42
  for(n=0;T.realTime()<0.1;n++) ;
43 43
  std::cout << T << " (" << n << " time queries)\n";
44
  T.restart();
45
  while(T.realTime()<2.0) ;
46
  std::cout << T << '\n';
44

	
47 45
  TimeStamp full;
48 46
  TimeStamp t;
49
  t=runningTimeTest(f,1,&n,&full);
47
  t=runningTimeTest(f,0.1,&n,&full);
50 48
  std::cout << t << " (" << n << " tests)\n";
51 49
  std::cout << "Total: " << full << "\n";
52 50

	
53
  t=runningTimeTest(g,1,&n,&full);
51
  t=runningTimeTest(g,0.1,&n,&full);
54 52
  std::cout << t << " (" << n << " tests)\n";
55 53
  std::cout << "Total: " << full << "\n";
56 54

	
57 55
  return 0;
58 56
}
Ignore white space 6 line context
1
EXTRA_DIST += \
2
	tools/CMakeLists.txt
3

	
1 4
if WANT_TOOLS
2 5

	
3 6
bin_PROGRAMS += \
4
	tools/dimacs-to-lgf
7
	tools/dimacs-solver \
8
	tools/dimacs-to-lgf \
9
	tools/lgf-gen
5 10

	
6 11
dist_bin_SCRIPTS += tools/lemon-0.x-to-1.x.sh
7 12

	
8 13
endif WANT_TOOLS
9 14

	
15
tools_dimacs_solver_SOURCES = tools/dimacs-solver.cc
10 16
tools_dimacs_to_lgf_SOURCES = tools/dimacs-to-lgf.cc
17
tools_lgf_gen_SOURCES = tools/lgf-gen.cc
Ignore white space 6 line context
modified binary file
Ignore white space 6 line context
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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup tools
20 20
///\file
21 21
///\brief DIMACS to LGF converter.
22 22
///
23 23
/// This program converts various DIMACS formats to the LEMON Digraph Format
24 24
/// (LGF).
25 25
///
26 26
/// See
27
/// \verbatim
28
///  dimacs-to-lgf --help
29
/// \endverbatim
30
/// for more info on usage.
31
///
27
/// \code
28
///   dimacs-to-lgf --help
29
/// \endcode
30
/// for more info on the usage.
32 31

	
33 32
#include <iostream>
34 33
#include <fstream>
35 34
#include <cstring>
36 35

	
37 36
#include <lemon/smart_graph.h>
38 37
#include <lemon/dimacs.h>
39 38
#include <lemon/lgf_writer.h>
40 39

	
41 40
#include <lemon/arg_parser.h>
42 41
#include <lemon/error.h>
43 42

	
44 43
using namespace std;
45 44
using namespace lemon;
46 45

	
47 46

	
48 47
int main(int argc, const char *argv[]) {
49 48
  typedef SmartDigraph Digraph;
50 49

	
51 50
  typedef Digraph::Arc Arc;
52 51
  typedef Digraph::Node Node;
53 52
  typedef Digraph::ArcIt ArcIt;
54 53
  typedef Digraph::NodeIt NodeIt;
55 54
  typedef Digraph::ArcMap<double> DoubleArcMap;
56 55
  typedef Digraph::NodeMap<double> DoubleNodeMap;
57 56

	
58 57
  std::string inputName;
59 58
  std::string outputName;
60 59

	
61 60
  ArgParser ap(argc, argv);
62 61
  ap.other("[INFILE [OUTFILE]]",
63 62
           "If either the INFILE or OUTFILE file is missing the standard\n"
64 63
           "     input/output will be used instead.")
65 64
    .run();
66 65

	
67 66
  ifstream input;
68 67
  ofstream output;
69 68

	
70 69
  switch(ap.files().size())
71 70
    {
72 71
    case 2:
73 72
      output.open(ap.files()[1].c_str());
74 73
      if (!output) {
75 74
        throw IoError("Cannot open the file for writing", ap.files()[1]);
76 75
      }
77 76
    case 1:
78 77
      input.open(ap.files()[0].c_str());
79 78
      if (!input) {
80 79
        throw IoError("File cannot be found", ap.files()[0]);
81 80
      }
82 81
    case 0:
83 82
      break;
84 83
    default:
85 84
      cerr << ap.commandName() << ": too many arguments\n";
86 85
      return 1;
87 86
  }
88 87
  istream& is = (ap.files().size()<1 ? cin : input);
89 88
  ostream& os = (ap.files().size()<2 ? cout : output);
90 89

	
91 90
  DimacsDescriptor desc = dimacsType(is);
92 91
  switch(desc.type)
93 92
    {
94 93
    case DimacsDescriptor::MIN:
95 94
      {
96 95
        Digraph digraph;
97 96
        DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
98 97
        DoubleNodeMap supply(digraph);
99
        readDimacsMin(is, digraph, lower, capacity, cost, supply, desc);
98
        readDimacsMin(is, digraph, lower, capacity, cost, supply, 0, desc);
100 99
        DigraphWriter<Digraph>(digraph, os).
101 100
          nodeMap("supply", supply).
102 101
          arcMap("lower", lower).
103 102
          arcMap("capacity", capacity).
104 103
          arcMap("cost", cost).
105 104
          attribute("problem","min").
106 105
          run();
107 106
      }
108 107
      break;
109 108
    case DimacsDescriptor::MAX:
110 109
      {
111 110
        Digraph digraph;
112 111
        Node s, t;
113 112
        DoubleArcMap capacity(digraph);
114
        readDimacsMax(is, digraph, capacity, s, t, desc);
113
        readDimacsMax(is, digraph, capacity, s, t, 0, desc);
115 114
        DigraphWriter<Digraph>(digraph, os).
116 115
          arcMap("capacity", capacity).
117 116
          node("source", s).
118 117
          node("target", t).
119 118
          attribute("problem","max").
120 119
          run();
121 120
      }
122 121
      break;
123 122
    case DimacsDescriptor::SP:
124 123
      {
125 124
        Digraph digraph;
126 125
        Node s;
127 126
        DoubleArcMap capacity(digraph);
128 127
        readDimacsSp(is, digraph, capacity, s, desc);
129 128
        DigraphWriter<Digraph>(digraph, os).
130 129
          arcMap("capacity", capacity).
131 130
          node("source", s).
132 131
          attribute("problem","sp").
133 132
          run();
134 133
      }
135 134
      break;
136 135
    case DimacsDescriptor::MAT:
137 136
      {
138 137
        Digraph digraph;
139 138
        readDimacsMat(is, digraph,desc);
140 139
        DigraphWriter<Digraph>(digraph, os).
141 140
          attribute("problem","mat").
142 141
          run();
143 142
      }
144 143
      break;
145 144
    default:
146 145
      break;
147 146
    }
148 147
  return 0;
149 148
}
Ignore white space 6 line context
1 1
#!/bin/bash
2 2

	
3 3
set -e
4 4

	
5 5
if [ $# -eq 0 -o x$1 = "x-h" -o x$1 = "x-help" -o x$1 = "x--help" ]; then
6 6
    echo "Usage:"
7 7
    echo "  $0 source-file(s)"
8 8
    exit
9 9
fi
10 10

	
11 11
for i in $@
12 12
do
13 13
    echo Update $i...
14 14
    TMP=`mktemp`
15 15
    sed -e "s/\<undirected graph\>/_gr_aph_label_/g"\
16 16
        -e "s/\<undirected graphs\>/_gr_aph_label_s/g"\
17 17
        -e "s/\<undirected edge\>/_ed_ge_label_/g"\
18 18
        -e "s/\<undirected edges\>/_ed_ge_label_s/g"\
19 19
        -e "s/\<directed graph\>/_digr_aph_label_/g"\
20 20
        -e "s/\<directed graphs\>/_digr_aph_label_s/g"\
21 21
        -e "s/\<directed edge\>/_ar_c_label_/g"\
22 22
        -e "s/\<directed edges\>/_ar_c_label_s/g"\
23 23
        -e "s/UGraph/_Gr_aph_label_/g"\
24 24
        -e "s/u[Gg]raph/_gr_aph_label_/g"\
25
        -e "s/\<Graph\>/_Digr_aph_label_/g"\
25
        -e "s/Graph\>/_Digr_aph_label_/g"\
26 26
        -e "s/\<graph\>/_digr_aph_label_/g"\
27
        -e "s/\<Graphs\>/_Digr_aph_label_s/g"\
27
        -e "s/Graphs\>/_Digr_aph_label_s/g"\
28 28
        -e "s/\<graphs\>/_digr_aph_label_s/g"\
29
        -e "s/_Graph/__Gr_aph_label_/g"\
30
        -e "s/\([Gg]\)raph\([a-z_]\)/_\1r_aph_label_\2/g"\
29
        -e "s/\([Gg]\)raph\([a-z]\)/_\1r_aph_label_\2/g"\
31 30
        -e "s/\([a-z_]\)graph/\1_gr_aph_label_/g"\
32 31
        -e "s/Graph/_Digr_aph_label_/g"\
33 32
        -e "s/graph/_digr_aph_label_/g"\
34 33
        -e "s/UEdge/_Ed_ge_label_/g"\
35 34
        -e "s/u[Ee]dge/_ed_ge_label_/g"\
36 35
        -e "s/IncEdgeIt/_In_cEd_geIt_label_/g"\
37
        -e "s/\<Edge\>/_Ar_c_label_/g"\
36
        -e "s/Edge\>/_Ar_c_label_/g"\
38 37
        -e "s/\<edge\>/_ar_c_label_/g"\
39
        -e "s/\<Edges\>/_Ar_c_label_s/g"\
38
        -e "s/_edge\>/__ar_c_label_/g"\
39
        -e "s/Edges\>/_Ar_c_label_s/g"\
40 40
        -e "s/\<edges\>/_ar_c_label_s/g"\
41
        -e "s/_Edge/__Ed_ge_label_/g"\
42
        -e "s/Edge\([a-z_]\)/_Ed_ge_label_\1/g"\
43
        -e "s/edge\([a-z_]\)/_ed_ge_label_\1/g"\
44
        -e "s/\([a-z_]\)edge/\1_ed_ge_label_/g"\
41
        -e "s/_edges\>/__ar_c_label_s/g"\
42
        -e "s/\([Ee]\)dge\([a-z]\)/_\1d_ge_label_\2/g"\
43
        -e "s/\([a-z]\)edge/\1_ed_ge_label_/g"\
45 44
        -e "s/Edge/_Ar_c_label_/g"\
46 45
        -e "s/edge/_ar_c_label_/g"\
47 46
        -e "s/A[Nn]ode/_Re_d_label_/g"\
48 47
        -e "s/B[Nn]ode/_Blu_e_label_/g"\
49 48
        -e "s/A-[Nn]ode/_Re_d_label_/g"\
50 49
        -e "s/B-[Nn]ode/_Blu_e_label_/g"\
51 50
        -e "s/a[Nn]ode/_re_d_label_/g"\
52 51
        -e "s/b[Nn]ode/_blu_e_label_/g"\
53 52
        -e "s/\<UGRAPH_TYPEDEFS\([ \t]*([ \t]*\)typename[ \t]/TEMPLATE__GR_APH_TY_PEDE_FS_label_\1/g"\
54 53
        -e "s/\<GRAPH_TYPEDEFS\([ \t]*([ \t]*\)typename[ \t]/TEMPLATE__DIGR_APH_TY_PEDE_FS_label_\1/g"\
55 54
        -e "s/\<UGRAPH_TYPEDEFS\>/_GR_APH_TY_PEDE_FS_label_/g"\
56 55
        -e "s/\<GRAPH_TYPEDEFS\>/_DIGR_APH_TY_PEDE_FS_label_/g"\
57 56
        -e "s/_Digr_aph_label_/Digraph/g"\
58 57
        -e "s/_digr_aph_label_/digraph/g"\
59 58
        -e "s/_Gr_aph_label_/Graph/g"\
60 59
        -e "s/_gr_aph_label_/graph/g"\
61 60
        -e "s/_Ar_c_label_/Arc/g"\
62 61
        -e "s/_ar_c_label_/arc/g"\
63 62
        -e "s/_Ed_ge_label_/Edge/g"\
64 63
        -e "s/_ed_ge_label_/edge/g"\
65 64
        -e "s/_In_cEd_geIt_label_/IncEdgeIt/g"\
66 65
        -e "s/_Re_d_label_/Red/g"\
67 66
        -e "s/_Blu_e_label_/Blue/g"\
68 67
        -e "s/_re_d_label_/red/g"\
69 68
        -e "s/_blu_e_label_/blue/g"\
70 69
        -e "s/_GR_APH_TY_PEDE_FS_label_/GRAPH_TYPEDEFS/g"\
71 70
        -e "s/_DIGR_APH_TY_PEDE_FS_label_/DIGRAPH_TYPEDEFS/g"\
71
        -e "s/\<digraph_adaptor\.h\>/adaptors.h/g"\
72
        -e "s/\<digraph_utils\.h\>/core.h/g"\
73
        -e "s/\<digraph_reader\.h\>/lgf_reader.h/g"\
74
        -e "s/\<digraph_writer\.h\>/lgf_writer.h/g"\
75
        -e "s/\<topology\.h\>/connectivity.h/g"\
72 76
        -e "s/DigraphToEps/GraphToEps/g"\
73 77
        -e "s/digraphToEps/graphToEps/g"\
74 78
        -e "s/\<DefPredMap\>/SetPredMap/g"\
75 79
        -e "s/\<DefDistMap\>/SetDistMap/g"\
76 80
        -e "s/\<DefReachedMap\>/SetReachedMap/g"\
77 81
        -e "s/\<DefProcessedMap\>/SetProcessedMap/g"\
78 82
        -e "s/\<DefHeap\>/SetHeap/g"\
79 83
        -e "s/\<DefStandardHeap\>/SetStandradHeap/g"\
80 84
        -e "s/\<DefOperationTraits\>/SetOperationTraits/g"\
81 85
        -e "s/\<DefProcessedMapToBeDefaultMap\>/SetStandardProcessedMap/g"\
82 86
        -e "s/\<copyGraph\>/graphCopy/g"\
83 87
        -e "s/\<copyDigraph\>/digraphCopy/g"\
84 88
        -e "s/\<HyperCubeDigraph\>/HypercubeGraph/g"\
85 89
        -e "s/\<IntegerMap\>/RangeMap/g"\
86 90
        -e "s/\<integerMap\>/rangeMap/g"\
87 91
        -e "s/\<\([sS]\)tdMap\>/\1parseMap/g"\
88 92
        -e "s/\<\([Ff]\)unctorMap\>/\1unctorToMap/g"\
89 93
        -e "s/\<\([Mm]\)apFunctor\>/\1apToFunctor/g"\
90 94
        -e "s/\<\([Ff]\)orkWriteMap\>/\1orkMap/g"\
91 95
        -e "s/\<StoreBoolMap\>/LoggerBoolMap/g"\
92 96
        -e "s/\<storeBoolMap\>/loggerBoolMap/g"\
97
        -e "s/\<InvertableMap\>/CrossRefMap/g"\
98
        -e "s/\<invertableMap\>/crossRefMap/g"\
99
        -e "s/\<DescriptorMap\>/RangeIdMap/g"\
100
        -e "s/\<descriptorMap\>/rangeIdMap/g"\
93 101
        -e "s/\<BoundingBox\>/Box/g"\
94 102
        -e "s/\<readNauty\>/readNautyGraph/g"\
95 103
        -e "s/\<RevDigraphAdaptor\>/ReverseDigraph/g"\
96 104
        -e "s/\<revDigraphAdaptor\>/reverseDigraph/g"\
97 105
        -e "s/\<SubDigraphAdaptor\>/SubDigraph/g"\
98 106
        -e "s/\<subDigraphAdaptor\>/subDigraph/g"\
99 107
        -e "s/\<SubGraphAdaptor\>/SubGraph/g"\
100 108
        -e "s/\<subGraphAdaptor\>/subGraph/g"\
101 109
        -e "s/\<NodeSubDigraphAdaptor\>/FilterNodes/g"\
102 110
        -e "s/\<nodeSubDigraphAdaptor\>/filterNodes/g"\
103 111
        -e "s/\<ArcSubDigraphAdaptor\>/FilterArcs/g"\
104 112
        -e "s/\<arcSubDigraphAdaptor\>/filterArcs/g"\
105 113
        -e "s/\<UndirDigraphAdaptor\>/Undirector/g"\
106 114
        -e "s/\<undirDigraphAdaptor\>/undirector/g"\
107 115
        -e "s/\<ResDigraphAdaptor\>/ResidualDigraph/g"\
108 116
        -e "s/\<resDigraphAdaptor\>/residualDigraph/g"\
109 117
        -e "s/\<SplitDigraphAdaptor\>/SplitNodes/g"\
110 118
        -e "s/\<splitDigraphAdaptor\>/splitNodes/g"\
111 119
        -e "s/\<SubGraphAdaptor\>/SubGraph/g"\
112 120
        -e "s/\<subGraphAdaptor\>/subGraph/g"\
113 121
        -e "s/\<NodeSubGraphAdaptor\>/FilterNodes/g"\
114 122
        -e "s/\<nodeSubGraphAdaptor\>/filterNodes/g"\
115 123
        -e "s/\<ArcSubGraphAdaptor\>/FilterEdges/g"\
116 124
        -e "s/\<arcSubGraphAdaptor\>/filterEdges/g"\
117 125
        -e "s/\<DirGraphAdaptor\>/Orienter/g"\
118 126
        -e "s/\<dirGraphAdaptor\>/orienter/g"\
127
        -e "s/\<LpCplex\>/CplexLp/g"\
128
        -e "s/\<MipCplex\>/CplexMip/g"\
129
        -e "s/\<LpGlpk\>/GlpkLp/g"\
130
        -e "s/\<MipGlpk\>/GlpkMip/g"\
131
        -e "s/\<LpSoplex\>/SoplexLp/g"\
119 132
    <$i > $TMP
120 133
    mv $TMP $i
121 134
done
Ignore white space 6 line context
1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library.
4
 *
5
 * Copyright (C) 2003-2009
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_BASE_EXTENDER_H
20
#define LEMON_BITS_BASE_EXTENDER_H
21

	
22
#include <lemon/core.h>
23
#include <lemon/error.h>
24

	
25
#include <lemon/bits/map_extender.h>
26
#include <lemon/bits/default_map.h>
27

	
28
#include <lemon/concept_check.h>
29
#include <lemon/concepts/maps.h>
30

	
31
//\ingroup digraphbits
32
//\file
33
//\brief Extenders for the graph types
34
namespace lemon {
35

	
36
  // \ingroup digraphbits
37
  //
38
  // \brief BaseDigraph to BaseGraph extender
39
  template <typename Base>
40
  class UndirDigraphExtender : public Base {
41

	
42
  public:
43

	
44
    typedef Base Parent;
45
    typedef typename Parent::Arc Edge;
46
    typedef typename Parent::Node Node;
47

	
48
    typedef True UndirectedTag;
49

	
50
    class Arc : public Edge {
51
      friend class UndirDigraphExtender;
52

	
53
    protected:
54
      bool forward;
55

	
56
      Arc(const Edge &ue, bool _forward) :
57
        Edge(ue), forward(_forward) {}
58

	
59
    public:
60
      Arc() {}
61

	
62
      // Invalid arc constructor
63
      Arc(Invalid i) : Edge(i), forward(true) {}
64

	
65
      bool operator==(const Arc &that) const {
66
        return forward==that.forward && Edge(*this)==Edge(that);
67
      }
68
      bool operator!=(const Arc &that) const {
69
        return forward!=that.forward || Edge(*this)!=Edge(that);
70
      }
71
      bool operator<(const Arc &that) const {
72
        return forward<that.forward ||
73
          (!(that.forward<forward) && Edge(*this)<Edge(that));
74
      }
75
    };
76

	
77
    // First node of the edge
78
    Node u(const Edge &e) const {
79
      return Parent::source(e);
80
    }
81

	
82
    // Source of the given arc
83
    Node source(const Arc &e) const {
84
      return e.forward ? Parent::source(e) : Parent::target(e);
85
    }
86

	
87
    // Second node of the edge
88
    Node v(const Edge &e) const {
89
      return Parent::target(e);
90
    }
91

	
92
    // Target of the given arc
93
    Node target(const Arc &e) const {
94
      return e.forward ? Parent::target(e) : Parent::source(e);
95
    }
96

	
97
    // \brief Directed arc from an edge.
98
    //
99
    // Returns a directed arc corresponding to the specified edge.
100
    // If the given bool is true, the first node of the given edge and
101
    // the source node of the returned arc are the same.
102
    static Arc direct(const Edge &e, bool d) {
103
      return Arc(e, d);
104
    }
105

	
106
    // Returns whether the given directed arc has the same orientation
107
    // as the corresponding edge.
108
    static bool direction(const Arc &a) { return a.forward; }
109

	
110
    using Parent::first;
111
    using Parent::next;
112

	
113
    void first(Arc &e) const {
114
      Parent::first(e);
115
      e.forward=true;
116
    }
117

	
118
    void next(Arc &e) const {
119
      if( e.forward ) {
120
        e.forward = false;
121
      }
122
      else {
123
        Parent::next(e);
124
        e.forward = true;
125
      }
126
    }
127

	
128
    void firstOut(Arc &e, const Node &n) const {
129
      Parent::firstIn(e,n);
130
      if( Edge(e) != INVALID ) {
131
        e.forward = false;
132
      }
133
      else {
134
        Parent::firstOut(e,n);
135
        e.forward = true;
136
      }
137
    }
138
    void nextOut(Arc &e) const {
139
      if( ! e.forward ) {
140
        Node n = Parent::target(e);
141
        Parent::nextIn(e);
142
        if( Edge(e) == INVALID ) {
143
          Parent::firstOut(e, n);
144
          e.forward = true;
145
        }
146
      }
147
      else {
148
        Parent::nextOut(e);
149
      }
150
    }
151

	
152
    void firstIn(Arc &e, const Node &n) const {
153
      Parent::firstOut(e,n);
154
      if( Edge(e) != INVALID ) {
155
        e.forward = false;
156
      }
157
      else {
158
        Parent::firstIn(e,n);
159
        e.forward = true;
160
      }
161
    }
162
    void nextIn(Arc &e) const {
163
      if( ! e.forward ) {
164
        Node n = Parent::source(e);
165
        Parent::nextOut(e);
166
        if( Edge(e) == INVALID ) {
167
          Parent::firstIn(e, n);
168
          e.forward = true;
169
        }
170
      }
171
      else {
172
        Parent::nextIn(e);
173
      }
174
    }
175

	
176
    void firstInc(Edge &e, bool &d, const Node &n) const {
177
      d = true;
178
      Parent::firstOut(e, n);
179
      if (e != INVALID) return;
180
      d = false;
181
      Parent::firstIn(e, n);
182
    }
183

	
184
    void nextInc(Edge &e, bool &d) const {
185
      if (d) {
186
        Node s = Parent::source(e);
187
        Parent::nextOut(e);
188
        if (e != INVALID) return;
189
        d = false;
190
        Parent::firstIn(e, s);
191
      } else {
192
        Parent::nextIn(e);
193
      }
194
    }
195

	
196
    Node nodeFromId(int ix) const {
197
      return Parent::nodeFromId(ix);
198
    }
199

	
200
    Arc arcFromId(int ix) const {
201
      return direct(Parent::arcFromId(ix >> 1), bool(ix & 1));
202
    }
203

	
204
    Edge edgeFromId(int ix) const {
205
      return Parent::arcFromId(ix);
206
    }
207

	
208
    int id(const Node &n) const {
209
      return Parent::id(n);
210
    }
211

	
212
    int id(const Edge &e) const {
213
      return Parent::id(e);
214
    }
215

	
216
    int id(const Arc &e) const {
217
      return 2 * Parent::id(e) + int(e.forward);
218
    }
219

	
220
    int maxNodeId() const {
221
      return Parent::maxNodeId();
222
    }
223

	
224
    int maxArcId() const {
225
      return 2 * Parent::maxArcId() + 1;
226
    }
227

	
228
    int maxEdgeId() const {
229
      return Parent::maxArcId();
230
    }
231

	
232
    int arcNum() const {
233
      return 2 * Parent::arcNum();
234
    }
235

	
236
    int edgeNum() const {
237
      return Parent::arcNum();
238
    }
239

	
240
    Arc findArc(Node s, Node t, Arc p = INVALID) const {
241
      if (p == INVALID) {
242
        Edge arc = Parent::findArc(s, t);
243
        if (arc != INVALID) return direct(arc, true);
244
        arc = Parent::findArc(t, s);
245
        if (arc != INVALID) return direct(arc, false);
246
      } else if (direction(p)) {
247
        Edge arc = Parent::findArc(s, t, p);
248
        if (arc != INVALID) return direct(arc, true);
249
        arc = Parent::findArc(t, s);
250
        if (arc != INVALID) return direct(arc, false);
251
      } else {
252
        Edge arc = Parent::findArc(t, s, p);
253
        if (arc != INVALID) return direct(arc, false);
254
      }
255
      return INVALID;
256
    }
257

	
258
    Edge findEdge(Node s, Node t, Edge p = INVALID) const {
259
      if (s != t) {
260
        if (p == INVALID) {
261
          Edge arc = Parent::findArc(s, t);
262
          if (arc != INVALID) return arc;
263
          arc = Parent::findArc(t, s);
264
          if (arc != INVALID) return arc;
265
        } else if (Parent::s(p) == s) {
266
          Edge arc = Parent::findArc(s, t, p);
267
          if (arc != INVALID) return arc;
268
          arc = Parent::findArc(t, s);
269
          if (arc != INVALID) return arc;
270
        } else {
271
          Edge arc = Parent::findArc(t, s, p);
272
          if (arc != INVALID) return arc;
273
        }
274
      } else {
275
        return Parent::findArc(s, t, p);
276
      }
277
      return INVALID;
278
    }
279
  };
280

	
281
  template <typename Base>
282
  class BidirBpGraphExtender : public Base {
283
  public:
284
    typedef Base Parent;
285
    typedef BidirBpGraphExtender Digraph;
286

	
287
    typedef typename Parent::Node Node;
288
    typedef typename Parent::Edge Edge;
289

	
290

	
291
    using Parent::first;
292
    using Parent::next;
293

	
294
    using Parent::id;
295

	
296
    class Red : public Node {
297
      friend class BidirBpGraphExtender;
298
    public:
299
      Red() {}
300
      Red(const Node& node) : Node(node) {
301
        LEMON_DEBUG(Parent::red(node) || node == INVALID,
302
                    typename Parent::NodeSetError());
303
      }
304
      Red& operator=(const Node& node) {
305
        LEMON_DEBUG(Parent::red(node) || node == INVALID,
306
                    typename Parent::NodeSetError());
307
        Node::operator=(node);
308
        return *this;
309
      }
310
      Red(Invalid) : Node(INVALID) {}
311
      Red& operator=(Invalid) {
312
        Node::operator=(INVALID);
313
        return *this;
314
      }
315
    };
316

	
317
    void first(Red& node) const {
318
      Parent::firstRed(static_cast<Node&>(node));
319
    }
320
    void next(Red& node) const {
321
      Parent::nextRed(static_cast<Node&>(node));
322
    }
323

	
324
    int id(const Red& node) const {
325
      return Parent::redId(node);
326
    }
327

	
328
    class Blue : public Node {
329
      friend class BidirBpGraphExtender;
330
    public:
331
      Blue() {}
332
      Blue(const Node& node) : Node(node) {
333
        LEMON_DEBUG(Parent::blue(node) || node == INVALID,
334
                    typename Parent::NodeSetError());
335
      }
336
      Blue& operator=(const Node& node) {
337
        LEMON_DEBUG(Parent::blue(node) || node == INVALID,
338
                    typename Parent::NodeSetError());
339
        Node::operator=(node);
340
        return *this;
341
      }
342
      Blue(Invalid) : Node(INVALID) {}
343
      Blue& operator=(Invalid) {
344
        Node::operator=(INVALID);
345
        return *this;
346
      }
347
    };
348

	
349
    void first(Blue& node) const {
350
      Parent::firstBlue(static_cast<Node&>(node));
351
    }
352
    void next(Blue& node) const {
353
      Parent::nextBlue(static_cast<Node&>(node));
354
    }
355

	
356
    int id(const Blue& node) const {
357
      return Parent::redId(node);
358
    }
359

	
360
    Node source(const Edge& arc) const {
361
      return red(arc);
362
    }
363
    Node target(const Edge& arc) const {
364
      return blue(arc);
365
    }
366

	
367
    void firstInc(Edge& arc, bool& dir, const Node& node) const {
368
      if (Parent::red(node)) {
369
        Parent::firstFromRed(arc, node);
370
        dir = true;
371
      } else {
372
        Parent::firstFromBlue(arc, node);
373
        dir = static_cast<Edge&>(arc) == INVALID;
374
      }
375
    }
376
    void nextInc(Edge& arc, bool& dir) const {
377
      if (dir) {
378
        Parent::nextFromRed(arc);
379
      } else {
380
        Parent::nextFromBlue(arc);
381
        if (arc == INVALID) dir = true;
382
      }
383
    }
384

	
385
    class Arc : public Edge {
386
      friend class BidirBpGraphExtender;
387
    protected:
388
      bool forward;
389

	
390
      Arc(const Edge& arc, bool _forward)
391
        : Edge(arc), forward(_forward) {}
392

	
393
    public:
394
      Arc() {}
395
      Arc (Invalid) : Edge(INVALID), forward(true) {}
396
      bool operator==(const Arc& i) const {
397
        return Edge::operator==(i) && forward == i.forward;
398
      }
399
      bool operator!=(const Arc& i) const {
400
        return Edge::operator!=(i) || forward != i.forward;
401
      }
402
      bool operator<(const Arc& i) const {
403
        return Edge::operator<(i) ||
404
          (!(i.forward<forward) && Edge(*this)<Edge(i));
405
      }
406
    };
407

	
408
    void first(Arc& arc) const {
409
      Parent::first(static_cast<Edge&>(arc));
410
      arc.forward = true;
411
    }
412

	
413
    void next(Arc& arc) const {
414
      if (!arc.forward) {
415
        Parent::next(static_cast<Edge&>(arc));
416
      }
417
      arc.forward = !arc.forward;
418
    }
419

	
420
    void firstOut(Arc& arc, const Node& node) const {
421
      if (Parent::red(node)) {
422
        Parent::firstFromRed(arc, node);
423
        arc.forward = true;
424
      } else {
425
        Parent::firstFromBlue(arc, node);
426
        arc.forward = static_cast<Edge&>(arc) == INVALID;
427
      }
428
    }
429
    void nextOut(Arc& arc) const {
430
      if (arc.forward) {
431
        Parent::nextFromRed(arc);
432
      } else {
433
        Parent::nextFromBlue(arc);
434
        arc.forward = static_cast<Edge&>(arc) == INVALID;
435
      }
436
    }
437

	
438
    void firstIn(Arc& arc, const Node& node) const {
439
      if (Parent::blue(node)) {
440
        Parent::firstFromBlue(arc, node);
441
        arc.forward = true;
442
      } else {
443
        Parent::firstFromRed(arc, node);
444
        arc.forward = static_cast<Edge&>(arc) == INVALID;
445
      }
446
    }
447
    void nextIn(Arc& arc) const {
448
      if (arc.forward) {
449
        Parent::nextFromBlue(arc);
450
      } else {
451
        Parent::nextFromRed(arc);
452
        arc.forward = static_cast<Edge&>(arc) == INVALID;
453
      }
454
    }
455

	
456
    Node source(const Arc& arc) const {
457
      return arc.forward ? Parent::red(arc) : Parent::blue(arc);
458
    }
459
    Node target(const Arc& arc) const {
460
      return arc.forward ? Parent::blue(arc) : Parent::red(arc);
461
    }
462

	
463
    int id(const Arc& arc) const {
464
      return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
465
        (arc.forward ? 0 : 1);
466
    }
467
    Arc arcFromId(int ix) const {
468
      return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
469
    }
470
    int maxArcId() const {
471
      return (Parent::maxEdgeId() << 1) + 1;
472
    }
473

	
474
    bool direction(const Arc& arc) const {
475
      return arc.forward;
476
    }
477

	
478
    Arc direct(const Edge& arc, bool dir) const {
479
      return Arc(arc, dir);
480
    }
481

	
482
    int arcNum() const {
483
      return 2 * Parent::edgeNum();
484
    }
485

	
486
    int edgeNum() const {
487
      return Parent::edgeNum();
488
    }
489

	
490

	
491
  };
492
}
493

	
494
#endif
Ignore white space 6 line context
1
AC_DEFUN([LX_CHECK_CLP],
2
[
3
  AC_ARG_WITH([clp],
4
AS_HELP_STRING([--with-clp@<:@=PREFIX@:>@], [search for CLP under PREFIX or under the default search paths if PREFIX is not given @<:@default@:>@])
5
AS_HELP_STRING([--without-clp], [disable checking for CLP]),
6
              [], [with_clp=yes])
7

	
8
  AC_ARG_WITH([clp-includedir],
9
AS_HELP_STRING([--with-clp-includedir=DIR], [search for CLP headers in DIR]),
10
              [], [with_clp_includedir=no])
11

	
12
  AC_ARG_WITH([clp-libdir],
13
AS_HELP_STRING([--with-clp-libdir=DIR], [search for CLP libraries in DIR]),
14
              [], [with_clp_libdir=no])
15

	
16
  lx_clp_found=no
17
  if test x"$with_clp" != x"no"; then
18
    AC_MSG_CHECKING([for CLP])
19

	
20
    if test x"$with_clp_includedir" != x"no"; then
21
      CLP_CXXFLAGS="-I$with_clp_includedir"
22
    elif test x"$with_clp" != x"yes"; then
23
      CLP_CXXFLAGS="-I$with_clp/include"
24
    fi
25

	
26
    if test x"$with_clp_libdir" != x"no"; then
27
      CLP_LDFLAGS="-L$with_clp_libdir"
28
    elif test x"$with_clp" != x"yes"; then
29
      CLP_LDFLAGS="-L$with_clp/lib"
30
    fi
31
    CLP_LIBS="-lClp -lCoinUtils -lm"
32

	
33
    lx_save_cxxflags="$CXXFLAGS"
34
    lx_save_ldflags="$LDFLAGS"
35
    lx_save_libs="$LIBS"
36
    CXXFLAGS="$CLP_CXXFLAGS"
37
    LDFLAGS="$CLP_LDFLAGS"
38
    LIBS="$CLP_LIBS"
39

	
40
    lx_clp_test_prog='
41
      #include <coin/ClpModel.hpp>
42

	
43
      int main(int argc, char** argv)
44
      {
45
        ClpModel clp;
46
        return 0;
47
      }'
48

	
49
    AC_LANG_PUSH(C++)
50
    AC_LINK_IFELSE([$lx_clp_test_prog], [lx_clp_found=yes], [lx_clp_found=no])
51
    AC_LANG_POP(C++)
52

	
53
    CXXFLAGS="$lx_save_cxxflags"
54
    LDFLAGS="$lx_save_ldflags"
55
    LIBS="$lx_save_libs"
56

	
57
    if test x"$lx_clp_found" = x"yes"; then
58
      AC_DEFINE([HAVE_CLP], [1], [Define to 1 if you have CLP.])
59
      lx_lp_found=yes
60
      AC_DEFINE([HAVE_LP], [1], [Define to 1 if you have any LP solver.])
61
      AC_MSG_RESULT([yes])
62
    else
63
      CLP_CXXFLAGS=""
64
      CLP_LDFLAGS=""
65
      CLP_LIBS=""
66
      AC_MSG_RESULT([no])
67
    fi
68
  fi
69
  CLP_LIBS="$CLP_LDFLAGS $CLP_LIBS"
70
  AC_SUBST(CLP_CXXFLAGS)
71
  AC_SUBST(CLP_LIBS)
72
  AM_CONDITIONAL([HAVE_CLP], [test x"$lx_clp_found" = x"yes"])
73
])
0 comments (0 inline)