gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fix several doxygen warings
0 4 0
default
4 files changed with 238 insertions and 238 deletions:
↑ Collapse diff ↑
Ignore white space 3072 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_BFS_H
20 20
#define LEMON_BFS_H
21 21

	
22 22
///\ingroup search
23 23
///\file
24 24
///\brief BFS algorithm.
25 25

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

	
33 33
namespace lemon {
34 34

	
35 35
  ///Default traits class of Bfs class.
36 36

	
37 37
  ///Default traits class of Bfs class.
38 38
  ///\tparam GR Digraph type.
39 39
  template<class GR>
40 40
  struct BfsDefaultTraits
41 41
  {
42 42
    ///The type of the digraph the algorithm runs on.
43 43
    typedef GR Digraph;
44 44

	
45 45
    ///\brief The type of the map that stores the predecessor
46 46
    ///arcs of the shortest paths.
47 47
    ///
48 48
    ///The type of the map that stores the predecessor
49 49
    ///arcs of the shortest paths.
50 50
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
51 51
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
52
    ///Instantiates a \ref PredMap.
52
    ///Instantiates a PredMap.
53 53

	
54
    ///This function instantiates a \ref PredMap.
54
    ///This function instantiates a PredMap.
55 55
    ///\param g is the digraph, to which we would like to define the
56
    ///\ref PredMap.
56
    ///PredMap.
57 57
    static PredMap *createPredMap(const Digraph &g)
58 58
    {
59 59
      return new PredMap(g);
60 60
    }
61 61

	
62 62
    ///The type of the map that indicates which nodes are processed.
63 63

	
64 64
    ///The type of the map that indicates which nodes are processed.
65 65
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
66 66
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
67
    ///Instantiates a \ref ProcessedMap.
67
    ///Instantiates a ProcessedMap.
68 68

	
69
    ///This function instantiates a \ref ProcessedMap.
69
    ///This function instantiates a ProcessedMap.
70 70
    ///\param g is the digraph, to which
71
    ///we would like to define the \ref ProcessedMap
71
    ///we would like to define the ProcessedMap
72 72
#ifdef DOXYGEN
73 73
    static ProcessedMap *createProcessedMap(const Digraph &g)
74 74
#else
75 75
    static ProcessedMap *createProcessedMap(const Digraph &)
76 76
#endif
77 77
    {
78 78
      return new ProcessedMap();
79 79
    }
80 80

	
81 81
    ///The type of the map that indicates which nodes are reached.
82 82

	
83 83
    ///The type of the map that indicates which nodes are reached.
84 84
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
85 85
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
86
    ///Instantiates a \ref ReachedMap.
86
    ///Instantiates a ReachedMap.
87 87

	
88
    ///This function instantiates a \ref ReachedMap.
88
    ///This function instantiates a ReachedMap.
89 89
    ///\param g is the digraph, to which
90
    ///we would like to define the \ref ReachedMap.
90
    ///we would like to define the ReachedMap.
91 91
    static ReachedMap *createReachedMap(const Digraph &g)
92 92
    {
93 93
      return new ReachedMap(g);
94 94
    }
95 95

	
96 96
    ///The type of the map that stores the distances of the nodes.
97 97

	
98 98
    ///The type of the map that stores the distances of the nodes.
99 99
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
100 100
    typedef typename Digraph::template NodeMap<int> DistMap;
101
    ///Instantiates a \ref DistMap.
101
    ///Instantiates a DistMap.
102 102

	
103
    ///This function instantiates a \ref DistMap.
103
    ///This function instantiates a DistMap.
104 104
    ///\param g is the digraph, to which we would like to define the
105
    ///\ref DistMap.
105
    ///DistMap.
106 106
    static DistMap *createDistMap(const Digraph &g)
107 107
    {
108 108
      return new DistMap(g);
109 109
    }
110 110
  };
111 111

	
112 112
  ///%BFS algorithm class.
113 113

	
114 114
  ///\ingroup search
115 115
  ///This class provides an efficient implementation of the %BFS algorithm.
116 116
  ///
117 117
  ///There is also a \ref bfs() "function-type interface" for the BFS
118 118
  ///algorithm, which is convenient in the simplier cases and it can be
119 119
  ///used easier.
120 120
  ///
121 121
  ///\tparam GR The type of the digraph the algorithm runs on.
122 122
  ///The default value is \ref ListDigraph. The value of GR is not used
123 123
  ///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits.
124 124
  ///\tparam TR Traits class to set various data types used by the algorithm.
125 125
  ///The default traits class is
126 126
  ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
127 127
  ///See \ref BfsDefaultTraits for the documentation of
128 128
  ///a Bfs traits class.
129 129
#ifdef DOXYGEN
130 130
  template <typename GR,
131 131
            typename TR>
132 132
#else
133 133
  template <typename GR=ListDigraph,
134 134
            typename TR=BfsDefaultTraits<GR> >
135 135
#endif
136 136
  class Bfs {
137 137
  public:
138 138

	
139 139
    ///The type of the digraph the algorithm runs on.
140 140
    typedef typename TR::Digraph Digraph;
141 141

	
142 142
    ///\brief The type of the map that stores the predecessor arcs of the
143 143
    ///shortest paths.
144 144
    typedef typename TR::PredMap PredMap;
145 145
    ///The type of the map that stores the distances of the nodes.
146 146
    typedef typename TR::DistMap DistMap;
147 147
    ///The type of the map that indicates which nodes are reached.
148 148
    typedef typename TR::ReachedMap ReachedMap;
149 149
    ///The type of the map that indicates which nodes are processed.
150 150
    typedef typename TR::ProcessedMap ProcessedMap;
151 151
    ///The type of the paths.
152 152
    typedef PredMapPath<Digraph, PredMap> Path;
153 153

	
154 154
    ///The traits class.
155 155
    typedef TR Traits;
156 156

	
157 157
  private:
158 158

	
159 159
    typedef typename Digraph::Node Node;
160 160
    typedef typename Digraph::NodeIt NodeIt;
161 161
    typedef typename Digraph::Arc Arc;
162 162
    typedef typename Digraph::OutArcIt OutArcIt;
163 163

	
164 164
    //Pointer to the underlying digraph.
165 165
    const Digraph *G;
166 166
    //Pointer to the map of predecessor arcs.
167 167
    PredMap *_pred;
168 168
    //Indicates if _pred is locally allocated (true) or not.
169 169
    bool local_pred;
170 170
    //Pointer to the map of distances.
171 171
    DistMap *_dist;
172 172
    //Indicates if _dist is locally allocated (true) or not.
173 173
    bool local_dist;
174 174
    //Pointer to the map of reached status of the nodes.
175 175
    ReachedMap *_reached;
176 176
    //Indicates if _reached is locally allocated (true) or not.
177 177
    bool local_reached;
178 178
    //Pointer to the map of processed status of the nodes.
179 179
    ProcessedMap *_processed;
180 180
    //Indicates if _processed is locally allocated (true) or not.
181 181
    bool local_processed;
182 182

	
183 183
    std::vector<typename Digraph::Node> _queue;
184 184
    int _queue_head,_queue_tail,_queue_next_dist;
185 185
    int _curr_dist;
186 186

	
187 187
    //Creates the maps if necessary.
188 188
    void create_maps()
189 189
    {
190 190
      if(!_pred) {
191 191
        local_pred = true;
192 192
        _pred = Traits::createPredMap(*G);
193 193
      }
194 194
      if(!_dist) {
195 195
        local_dist = true;
196 196
        _dist = Traits::createDistMap(*G);
197 197
      }
198 198
      if(!_reached) {
199 199
        local_reached = true;
200 200
        _reached = Traits::createReachedMap(*G);
201 201
      }
202 202
      if(!_processed) {
203 203
        local_processed = true;
204 204
        _processed = Traits::createProcessedMap(*G);
205 205
      }
206 206
    }
207 207

	
208 208
  protected:
209 209

	
210 210
    Bfs() {}
211 211

	
212 212
  public:
213 213

	
214 214
    typedef Bfs Create;
215 215

	
216 216
    ///\name Named template parameters
217 217

	
218 218
    ///@{
219 219

	
220 220
    template <class T>
221 221
    struct SetPredMapTraits : public Traits {
222 222
      typedef T PredMap;
223 223
      static PredMap *createPredMap(const Digraph &)
224 224
      {
225 225
        LEMON_ASSERT(false, "PredMap is not initialized");
226 226
        return 0; // ignore warnings
227 227
      }
228 228
    };
229 229
    ///\brief \ref named-templ-param "Named parameter" for setting
230
    ///\ref PredMap type.
230
    ///PredMap type.
231 231
    ///
232 232
    ///\ref named-templ-param "Named parameter" for setting
233
    ///\ref PredMap type.
233
    ///PredMap type.
234 234
    template <class T>
235 235
    struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
236 236
      typedef Bfs< Digraph, SetPredMapTraits<T> > Create;
237 237
    };
238 238

	
239 239
    template <class T>
240 240
    struct SetDistMapTraits : public Traits {
241 241
      typedef T DistMap;
242 242
      static DistMap *createDistMap(const Digraph &)
243 243
      {
244 244
        LEMON_ASSERT(false, "DistMap is not initialized");
245 245
        return 0; // ignore warnings
246 246
      }
247 247
    };
248 248
    ///\brief \ref named-templ-param "Named parameter" for setting
249
    ///\ref DistMap type.
249
    ///DistMap type.
250 250
    ///
251 251
    ///\ref named-templ-param "Named parameter" for setting
252
    ///\ref DistMap type.
252
    ///DistMap type.
253 253
    template <class T>
254 254
    struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
255 255
      typedef Bfs< Digraph, SetDistMapTraits<T> > Create;
256 256
    };
257 257

	
258 258
    template <class T>
259 259
    struct SetReachedMapTraits : public Traits {
260 260
      typedef T ReachedMap;
261 261
      static ReachedMap *createReachedMap(const Digraph &)
262 262
      {
263 263
        LEMON_ASSERT(false, "ReachedMap is not initialized");
264 264
        return 0; // ignore warnings
265 265
      }
266 266
    };
267 267
    ///\brief \ref named-templ-param "Named parameter" for setting
268
    ///\ref ReachedMap type.
268
    ///ReachedMap type.
269 269
    ///
270 270
    ///\ref named-templ-param "Named parameter" for setting
271
    ///\ref ReachedMap type.
271
    ///ReachedMap type.
272 272
    template <class T>
273 273
    struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
274 274
      typedef Bfs< Digraph, SetReachedMapTraits<T> > Create;
275 275
    };
276 276

	
277 277
    template <class T>
278 278
    struct SetProcessedMapTraits : public Traits {
279 279
      typedef T ProcessedMap;
280 280
      static ProcessedMap *createProcessedMap(const Digraph &)
281 281
      {
282 282
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
283 283
        return 0; // ignore warnings
284 284
      }
285 285
    };
286 286
    ///\brief \ref named-templ-param "Named parameter" for setting
287
    ///\ref ProcessedMap type.
287
    ///ProcessedMap type.
288 288
    ///
289 289
    ///\ref named-templ-param "Named parameter" for setting
290
    ///\ref ProcessedMap type.
290
    ///ProcessedMap type.
291 291
    template <class T>
292 292
    struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
293 293
      typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create;
294 294
    };
295 295

	
296 296
    struct SetStandardProcessedMapTraits : public Traits {
297 297
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
298 298
      static ProcessedMap *createProcessedMap(const Digraph &g)
299 299
      {
300 300
        return new ProcessedMap(g);
301 301
        return 0; // ignore warnings
302 302
      }
303 303
    };
304 304
    ///\brief \ref named-templ-param "Named parameter" for setting
305
    ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
305
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
306 306
    ///
307 307
    ///\ref named-templ-param "Named parameter" for setting
308
    ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
308
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
309 309
    ///If you don't set it explicitly, it will be automatically allocated.
310 310
    struct SetStandardProcessedMap :
311 311
      public Bfs< Digraph, SetStandardProcessedMapTraits > {
312 312
      typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create;
313 313
    };
314 314

	
315 315
    ///@}
316 316

	
317 317
  public:
318 318

	
319 319
    ///Constructor.
320 320

	
321 321
    ///Constructor.
322 322
    ///\param g The digraph the algorithm runs on.
323 323
    Bfs(const Digraph &g) :
324 324
      G(&g),
325 325
      _pred(NULL), local_pred(false),
326 326
      _dist(NULL), local_dist(false),
327 327
      _reached(NULL), local_reached(false),
328 328
      _processed(NULL), local_processed(false)
329 329
    { }
330 330

	
331 331
    ///Destructor.
332 332
    ~Bfs()
333 333
    {
334 334
      if(local_pred) delete _pred;
335 335
      if(local_dist) delete _dist;
336 336
      if(local_reached) delete _reached;
337 337
      if(local_processed) delete _processed;
338 338
    }
339 339

	
340 340
    ///Sets the map that stores the predecessor arcs.
341 341

	
342 342
    ///Sets the map that stores the predecessor arcs.
343 343
    ///If you don't use this function before calling \ref run(),
344 344
    ///it will allocate one. The destructor deallocates this
345 345
    ///automatically allocated map, of course.
346 346
    ///\return <tt> (*this) </tt>
347 347
    Bfs &predMap(PredMap &m)
348 348
    {
349 349
      if(local_pred) {
350 350
        delete _pred;
351 351
        local_pred=false;
352 352
      }
353 353
      _pred = &m;
354 354
      return *this;
355 355
    }
356 356

	
357 357
    ///Sets the map that indicates which nodes are reached.
358 358

	
359 359
    ///Sets the map that indicates which nodes are reached.
360 360
    ///If you don't use this function before calling \ref run(),
361 361
    ///it will allocate one. The destructor deallocates this
362 362
    ///automatically allocated map, of course.
363 363
    ///\return <tt> (*this) </tt>
364 364
    Bfs &reachedMap(ReachedMap &m)
365 365
    {
366 366
      if(local_reached) {
367 367
        delete _reached;
368 368
        local_reached=false;
369 369
      }
370 370
      _reached = &m;
371 371
      return *this;
372 372
    }
373 373

	
374 374
    ///Sets the map that indicates which nodes are processed.
375 375

	
376 376
    ///Sets the map that indicates which nodes are processed.
377 377
    ///If you don't use this function before calling \ref run(),
378 378
    ///it will allocate one. The destructor deallocates this
379 379
    ///automatically allocated map, of course.
380 380
    ///\return <tt> (*this) </tt>
381 381
    Bfs &processedMap(ProcessedMap &m)
382 382
    {
383 383
      if(local_processed) {
384 384
        delete _processed;
385 385
        local_processed=false;
386 386
      }
387 387
      _processed = &m;
388 388
      return *this;
389 389
    }
390 390

	
391 391
    ///Sets the map that stores the distances of the nodes.
392 392

	
393 393
    ///Sets the map that stores the distances of the nodes calculated by
394 394
    ///the algorithm.
395 395
    ///If you don't use this function before calling \ref run(),
396 396
    ///it will allocate one. The destructor deallocates this
397 397
    ///automatically allocated map, of course.
398 398
    ///\return <tt> (*this) </tt>
399 399
    Bfs &distMap(DistMap &m)
400 400
    {
401 401
      if(local_dist) {
402 402
        delete _dist;
403 403
        local_dist=false;
404 404
      }
405 405
      _dist = &m;
406 406
      return *this;
407 407
    }
408 408

	
409 409
  public:
410 410

	
411 411
    ///\name Execution control
412 412
    ///The simplest way to execute the algorithm is to use
413 413
    ///one of the member functions called \ref lemon::Bfs::run() "run()".
414 414
    ///\n
415 415
    ///If you need more control on the execution, first you must call
416 416
    ///\ref lemon::Bfs::init() "init()", then you can add several source
417 417
    ///nodes with \ref lemon::Bfs::addSource() "addSource()".
418 418
    ///Finally \ref lemon::Bfs::start() "start()" will perform the
419 419
    ///actual path computation.
420 420

	
421 421
    ///@{
422 422

	
423 423
    ///Initializes the internal data structures.
424 424

	
425 425
    ///Initializes the internal data structures.
426 426
    ///
427 427
    void init()
428 428
    {
429 429
      create_maps();
430 430
      _queue.resize(countNodes(*G));
431 431
      _queue_head=_queue_tail=0;
432 432
      _curr_dist=1;
433 433
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
434 434
        _pred->set(u,INVALID);
435 435
        _reached->set(u,false);
436 436
        _processed->set(u,false);
437 437
      }
438 438
    }
439 439

	
440 440
    ///Adds a new source node.
441 441

	
442 442
    ///Adds a new source node to the set of nodes to be processed.
443 443
    ///
444 444
    void addSource(Node s)
445 445
    {
446 446
      if(!(*_reached)[s])
447 447
        {
448 448
          _reached->set(s,true);
449 449
          _pred->set(s,INVALID);
450 450
          _dist->set(s,0);
451 451
          _queue[_queue_head++]=s;
452 452
          _queue_next_dist=_queue_head;
453 453
        }
454 454
    }
455 455

	
456 456
    ///Processes the next node.
457 457

	
458 458
    ///Processes the next node.
459 459
    ///
460 460
    ///\return The processed node.
461 461
    ///
462 462
    ///\pre The queue must not be empty.
463 463
    Node processNextNode()
464 464
    {
465 465
      if(_queue_tail==_queue_next_dist) {
466 466
        _curr_dist++;
467 467
        _queue_next_dist=_queue_head;
468 468
      }
469 469
      Node n=_queue[_queue_tail++];
470 470
      _processed->set(n,true);
471 471
      Node m;
472 472
      for(OutArcIt e(*G,n);e!=INVALID;++e)
473 473
        if(!(*_reached)[m=G->target(e)]) {
474 474
          _queue[_queue_head++]=m;
475 475
          _reached->set(m,true);
476 476
          _pred->set(m,e);
477 477
          _dist->set(m,_curr_dist);
478 478
        }
479 479
      return n;
480 480
    }
481 481

	
482 482
    ///Processes the next node.
483 483

	
484 484
    ///Processes the next node and checks if the given target node
485 485
    ///is reached. If the target node is reachable from the processed
486 486
    ///node, then the \c reach parameter will be set to \c true.
487 487
    ///
488 488
    ///\param target The target node.
489 489
    ///\retval reach Indicates if the target node is reached.
490 490
    ///It should be initially \c false.
491 491
    ///
492 492
    ///\return The processed node.
493 493
    ///
494 494
    ///\pre The queue must not be empty.
495 495
    Node processNextNode(Node target, bool& reach)
496 496
    {
497 497
      if(_queue_tail==_queue_next_dist) {
498 498
        _curr_dist++;
499 499
        _queue_next_dist=_queue_head;
500 500
      }
501 501
      Node n=_queue[_queue_tail++];
502 502
      _processed->set(n,true);
503 503
      Node m;
504 504
      for(OutArcIt e(*G,n);e!=INVALID;++e)
505 505
        if(!(*_reached)[m=G->target(e)]) {
506 506
          _queue[_queue_head++]=m;
507 507
          _reached->set(m,true);
508 508
          _pred->set(m,e);
509 509
          _dist->set(m,_curr_dist);
510 510
          reach = reach || (target == m);
511 511
        }
512 512
      return n;
513 513
    }
514 514

	
515 515
    ///Processes the next node.
516 516

	
517 517
    ///Processes the next node and checks if at least one of reached
518 518
    ///nodes has \c true value in the \c nm node map. If one node
519 519
    ///with \c true value is reachable from the processed node, then the
520 520
    ///\c rnode parameter will be set to the first of such nodes.
521 521
    ///
522 522
    ///\param nm A \c bool (or convertible) node map that indicates the
523 523
    ///possible targets.
524 524
    ///\retval rnode The reached target node.
525 525
    ///It should be initially \c INVALID.
526 526
    ///
527 527
    ///\return The processed node.
528 528
    ///
529 529
    ///\pre The queue must not be empty.
530 530
    template<class NM>
531 531
    Node processNextNode(const NM& nm, Node& rnode)
532 532
    {
533 533
      if(_queue_tail==_queue_next_dist) {
534 534
        _curr_dist++;
535 535
        _queue_next_dist=_queue_head;
536 536
      }
537 537
      Node n=_queue[_queue_tail++];
538 538
      _processed->set(n,true);
539 539
      Node m;
540 540
      for(OutArcIt e(*G,n);e!=INVALID;++e)
541 541
        if(!(*_reached)[m=G->target(e)]) {
542 542
          _queue[_queue_head++]=m;
543 543
          _reached->set(m,true);
544 544
          _pred->set(m,e);
545 545
          _dist->set(m,_curr_dist);
546 546
          if (nm[m] && rnode == INVALID) rnode = m;
547 547
        }
548 548
      return n;
549 549
    }
550 550

	
551 551
    ///The next node to be processed.
552 552

	
553 553
    ///Returns the next node to be processed or \c INVALID if the queue
554 554
    ///is empty.
555 555
    Node nextNode() const
556 556
    {
557 557
      return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
558 558
    }
559 559

	
560 560
    ///\brief Returns \c false if there are nodes
561 561
    ///to be processed.
562 562
    ///
563 563
    ///Returns \c false if there are nodes
564 564
    ///to be processed in the queue.
565 565
    bool emptyQueue() const { return _queue_tail==_queue_head; }
566 566

	
567 567
    ///Returns the number of the nodes to be processed.
568 568

	
569 569
    ///Returns the number of the nodes to be processed in the queue.
570 570
    int queueSize() const { return _queue_head-_queue_tail; }
571 571

	
572 572
    ///Executes the algorithm.
573 573

	
574 574
    ///Executes the algorithm.
575 575
    ///
576 576
    ///This method runs the %BFS algorithm from the root node(s)
577 577
    ///in order to compute the shortest path to each node.
578 578
    ///
579 579
    ///The algorithm computes
580 580
    ///- the shortest path tree (forest),
581 581
    ///- the distance of each node from the root(s).
582 582
    ///
583 583
    ///\pre init() must be called and at least one root node should be
584 584
    ///added with addSource() before using this function.
585 585
    ///
586 586
    ///\note <tt>b.start()</tt> is just a shortcut of the following code.
587 587
    ///\code
588 588
    ///  while ( !b.emptyQueue() ) {
589 589
    ///    b.processNextNode();
590 590
    ///  }
591 591
    ///\endcode
592 592
    void start()
593 593
    {
594 594
      while ( !emptyQueue() ) processNextNode();
595 595
    }
596 596

	
597 597
    ///Executes the algorithm until the given target node is reached.
598 598

	
599 599
    ///Executes the algorithm until the given target node is reached.
600 600
    ///
601 601
    ///This method runs the %BFS algorithm from the root node(s)
602 602
    ///in order to compute the shortest path to \c t.
603 603
    ///
604 604
    ///The algorithm computes
605 605
    ///- the shortest path to \c t,
606 606
    ///- the distance of \c t from the root(s).
607 607
    ///
608 608
    ///\pre init() must be called and at least one root node should be
609 609
    ///added with addSource() before using this function.
610 610
    ///
611 611
    ///\note <tt>b.start(t)</tt> is just a shortcut of the following code.
612 612
    ///\code
613 613
    ///  bool reach = false;
614 614
    ///  while ( !b.emptyQueue() && !reach ) {
615 615
    ///    b.processNextNode(t, reach);
616 616
    ///  }
617 617
    ///\endcode
618 618
    void start(Node t)
619 619
    {
620 620
      bool reach = false;
621 621
      while ( !emptyQueue() && !reach ) processNextNode(t, reach);
622 622
    }
623 623

	
624 624
    ///Executes the algorithm until a condition is met.
625 625

	
626 626
    ///Executes the algorithm until a condition is met.
627 627
    ///
628 628
    ///This method runs the %BFS algorithm from the root node(s) in
629 629
    ///order to compute the shortest path to a node \c v with
630 630
    /// <tt>nm[v]</tt> true, if such a node can be found.
631 631
    ///
632 632
    ///\param nm A \c bool (or convertible) node map. The algorithm
633 633
    ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
634 634
    ///
635 635
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
636 636
    ///\c INVALID if no such node was found.
637 637
    ///
638 638
    ///\pre init() must be called and at least one root node should be
639 639
    ///added with addSource() before using this function.
640 640
    ///
641 641
    ///\note <tt>b.start(nm)</tt> is just a shortcut of the following code.
642 642
    ///\code
643 643
    ///  Node rnode = INVALID;
644 644
    ///  while ( !b.emptyQueue() && rnode == INVALID ) {
645 645
    ///    b.processNextNode(nm, rnode);
646 646
    ///  }
647 647
    ///  return rnode;
648 648
    ///\endcode
649 649
    template<class NodeBoolMap>
650 650
    Node start(const NodeBoolMap &nm)
651 651
    {
652 652
      Node rnode = INVALID;
653 653
      while ( !emptyQueue() && rnode == INVALID ) {
654 654
        processNextNode(nm, rnode);
655 655
      }
656 656
      return rnode;
657 657
    }
658 658

	
659 659
    ///Runs the algorithm from the given source node.
660 660

	
661 661
    ///This method runs the %BFS algorithm from node \c s
662 662
    ///in order to compute the shortest path to each node.
663 663
    ///
664 664
    ///The algorithm computes
665 665
    ///- the shortest path tree,
666 666
    ///- the distance of each node from the root.
667 667
    ///
668 668
    ///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
669 669
    ///\code
670 670
    ///  b.init();
671 671
    ///  b.addSource(s);
672 672
    ///  b.start();
673 673
    ///\endcode
674 674
    void run(Node s) {
675 675
      init();
676 676
      addSource(s);
677 677
      start();
678 678
    }
679 679

	
680 680
    ///Finds the shortest path between \c s and \c t.
681 681

	
682 682
    ///This method runs the %BFS algorithm from node \c s
683 683
    ///in order to compute the shortest path to node \c t
684 684
    ///(it stops searching when \c t is processed).
685 685
    ///
686 686
    ///\return \c true if \c t is reachable form \c s.
687 687
    ///
688 688
    ///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a
689 689
    ///shortcut of the following code.
690 690
    ///\code
691 691
    ///  b.init();
692 692
    ///  b.addSource(s);
693 693
    ///  b.start(t);
694 694
    ///\endcode
695 695
    bool run(Node s,Node t) {
696 696
      init();
697 697
      addSource(s);
698 698
      start(t);
699 699
      return reached(t);
700 700
    }
701 701

	
702 702
    ///Runs the algorithm to visit all nodes in the digraph.
703 703

	
704 704
    ///This method runs the %BFS algorithm in order to
705 705
    ///compute the shortest path to each node.
706 706
    ///
707 707
    ///The algorithm computes
708 708
    ///- the shortest path tree (forest),
709 709
    ///- the distance of each node from the root(s).
710 710
    ///
711 711
    ///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
712 712
    ///\code
713 713
    ///  b.init();
714 714
    ///  for (NodeIt n(gr); n != INVALID; ++n) {
715 715
    ///    if (!b.reached(n)) {
716 716
    ///      b.addSource(n);
717 717
    ///      b.start();
718 718
    ///    }
719 719
    ///  }
720 720
    ///\endcode
721 721
    void run() {
722 722
      init();
723 723
      for (NodeIt n(*G); n != INVALID; ++n) {
724 724
        if (!reached(n)) {
725 725
          addSource(n);
726 726
          start();
727 727
        }
728 728
      }
729 729
    }
730 730

	
731 731
    ///@}
732 732

	
733 733
    ///\name Query Functions
734 734
    ///The result of the %BFS algorithm can be obtained using these
735 735
    ///functions.\n
736 736
    ///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start()
737 737
    ///"start()" must be called before using them.
738 738

	
739 739
    ///@{
740 740

	
741 741
    ///The shortest path to a node.
742 742

	
743 743
    ///Returns the shortest path to a node.
744 744
    ///
745 745
    ///\warning \c t should be reachable from the root(s).
746 746
    ///
747 747
    ///\pre Either \ref run() or \ref start() must be called before
748 748
    ///using this function.
749 749
    Path path(Node t) const { return Path(*G, *_pred, t); }
750 750

	
751 751
    ///The distance of a node from the root(s).
752 752

	
753 753
    ///Returns the distance of a node from the root(s).
754 754
    ///
755 755
    ///\warning If node \c v is not reachable from the root(s), then
756 756
    ///the return value of this function is undefined.
757 757
    ///
758 758
    ///\pre Either \ref run() or \ref start() must be called before
759 759
    ///using this function.
760 760
    int dist(Node v) const { return (*_dist)[v]; }
761 761

	
762 762
    ///Returns the 'previous arc' of the shortest path tree for a node.
763 763

	
764 764
    ///This function returns the 'previous arc' of the shortest path
765 765
    ///tree for the node \c v, i.e. it returns the last arc of a
766 766
    ///shortest path from the root(s) to \c v. It is \c INVALID if \c v
767 767
    ///is not reachable from the root(s) or if \c v is a root.
768 768
    ///
769 769
    ///The shortest path tree used here is equal to the shortest path
770 770
    ///tree used in \ref predNode().
771 771
    ///
772 772
    ///\pre Either \ref run() or \ref start() must be called before
773 773
    ///using this function.
774 774
    Arc predArc(Node v) const { return (*_pred)[v];}
775 775

	
776 776
    ///Returns the 'previous node' of the shortest path tree for a node.
777 777

	
778 778
    ///This function returns the 'previous node' of the shortest path
779 779
    ///tree for the node \c v, i.e. it returns the last but one node
780 780
    ///from a shortest path from the root(s) to \c v. It is \c INVALID
781 781
    ///if \c v is not reachable from the root(s) or if \c v is a root.
782 782
    ///
783 783
    ///The shortest path tree used here is equal to the shortest path
784 784
    ///tree used in \ref predArc().
785 785
    ///
786 786
    ///\pre Either \ref run() or \ref start() must be called before
787 787
    ///using this function.
788 788
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
789 789
                                  G->source((*_pred)[v]); }
790 790

	
791 791
    ///\brief Returns a const reference to the node map that stores the
792 792
    /// distances of the nodes.
793 793
    ///
794 794
    ///Returns a const reference to the node map that stores the distances
795 795
    ///of the nodes calculated by the algorithm.
796 796
    ///
797 797
    ///\pre Either \ref run() or \ref init()
798 798
    ///must be called before using this function.
799 799
    const DistMap &distMap() const { return *_dist;}
800 800

	
801 801
    ///\brief Returns a const reference to the node map that stores the
802 802
    ///predecessor arcs.
803 803
    ///
804 804
    ///Returns a const reference to the node map that stores the predecessor
805 805
    ///arcs, which form the shortest path tree.
806 806
    ///
807 807
    ///\pre Either \ref run() or \ref init()
808 808
    ///must be called before using this function.
809 809
    const PredMap &predMap() const { return *_pred;}
810 810

	
811 811
    ///Checks if a node is reachable from the root(s).
812 812

	
813 813
    ///Returns \c true if \c v is reachable from the root(s).
814 814
    ///\pre Either \ref run() or \ref start()
815 815
    ///must be called before using this function.
816 816
    bool reached(Node v) const { return (*_reached)[v]; }
817 817

	
818 818
    ///@}
819 819
  };
820 820

	
821 821
  ///Default traits class of bfs() function.
822 822

	
823 823
  ///Default traits class of bfs() function.
824 824
  ///\tparam GR Digraph type.
825 825
  template<class GR>
826 826
  struct BfsWizardDefaultTraits
827 827
  {
828 828
    ///The type of the digraph the algorithm runs on.
829 829
    typedef GR Digraph;
830 830

	
831 831
    ///\brief The type of the map that stores the predecessor
832 832
    ///arcs of the shortest paths.
833 833
    ///
834 834
    ///The type of the map that stores the predecessor
835 835
    ///arcs of the shortest paths.
836 836
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
837 837
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
838
    ///Instantiates a \ref PredMap.
838
    ///Instantiates a PredMap.
839 839

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

	
848 848
    ///The type of the map that indicates which nodes are processed.
849 849

	
850 850
    ///The type of the map that indicates which nodes are processed.
851 851
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
852 852
    ///By default it is a NullMap.
853 853
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
854
    ///Instantiates a \ref ProcessedMap.
854
    ///Instantiates a ProcessedMap.
855 855

	
856
    ///This function instantiates a \ref ProcessedMap.
856
    ///This function instantiates a ProcessedMap.
857 857
    ///\param g is the digraph, to which
858
    ///we would like to define the \ref ProcessedMap.
858
    ///we would like to define the ProcessedMap.
859 859
#ifdef DOXYGEN
860 860
    static ProcessedMap *createProcessedMap(const Digraph &g)
861 861
#else
862 862
    static ProcessedMap *createProcessedMap(const Digraph &)
863 863
#endif
864 864
    {
865 865
      return new ProcessedMap();
866 866
    }
867 867

	
868 868
    ///The type of the map that indicates which nodes are reached.
869 869

	
870 870
    ///The type of the map that indicates which nodes are reached.
871 871
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
872 872
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
873
    ///Instantiates a \ref ReachedMap.
873
    ///Instantiates a ReachedMap.
874 874

	
875
    ///This function instantiates a \ref ReachedMap.
875
    ///This function instantiates a ReachedMap.
876 876
    ///\param g is the digraph, to which
877
    ///we would like to define the \ref ReachedMap.
877
    ///we would like to define the ReachedMap.
878 878
    static ReachedMap *createReachedMap(const Digraph &g)
879 879
    {
880 880
      return new ReachedMap(g);
881 881
    }
882 882

	
883 883
    ///The type of the map that stores the distances of the nodes.
884 884

	
885 885
    ///The type of the map that stores the distances of the nodes.
886 886
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
887 887
    typedef typename Digraph::template NodeMap<int> DistMap;
888
    ///Instantiates a \ref DistMap.
888
    ///Instantiates a DistMap.
889 889

	
890
    ///This function instantiates a \ref DistMap.
890
    ///This function instantiates a DistMap.
891 891
    ///\param g is the digraph, to which we would like to define
892
    ///the \ref DistMap
892
    ///the DistMap
893 893
    static DistMap *createDistMap(const Digraph &g)
894 894
    {
895 895
      return new DistMap(g);
896 896
    }
897 897

	
898 898
    ///The type of the shortest paths.
899 899

	
900 900
    ///The type of the shortest paths.
901 901
    ///It must meet the \ref concepts::Path "Path" concept.
902 902
    typedef lemon::Path<Digraph> Path;
903 903
  };
904 904

	
905
  /// Default traits class used by \ref BfsWizard
905
  /// Default traits class used by BfsWizard
906 906

	
907 907
  /// To make it easier to use Bfs algorithm
908 908
  /// we have created a wizard class.
909 909
  /// This \ref BfsWizard class needs default traits,
910 910
  /// as well as the \ref Bfs class.
911 911
  /// The \ref BfsWizardBase is a class to be the default traits of the
912 912
  /// \ref BfsWizard class.
913 913
  template<class GR>
914 914
  class BfsWizardBase : public BfsWizardDefaultTraits<GR>
915 915
  {
916 916

	
917 917
    typedef BfsWizardDefaultTraits<GR> Base;
918 918
  protected:
919 919
    //The type of the nodes in the digraph.
920 920
    typedef typename Base::Digraph::Node Node;
921 921

	
922 922
    //Pointer to the digraph the algorithm runs on.
923 923
    void *_g;
924 924
    //Pointer to the map of reached nodes.
925 925
    void *_reached;
926 926
    //Pointer to the map of processed nodes.
927 927
    void *_processed;
928 928
    //Pointer to the map of predecessors arcs.
929 929
    void *_pred;
930 930
    //Pointer to the map of distances.
931 931
    void *_dist;
932 932
    //Pointer to the shortest path to the target node.
933 933
    void *_path;
934 934
    //Pointer to the distance of the target node.
935 935
    int *_di;
936 936

	
937 937
    public:
938 938
    /// Constructor.
939 939

	
940 940
    /// This constructor does not require parameters, therefore it initiates
941 941
    /// all of the attributes to \c 0.
942 942
    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
943 943
                      _dist(0), _path(0), _di(0) {}
944 944

	
945 945
    /// Constructor.
946 946

	
947 947
    /// This constructor requires one parameter,
948 948
    /// others are initiated to \c 0.
949 949
    /// \param g The digraph the algorithm runs on.
950 950
    BfsWizardBase(const GR &g) :
951 951
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
952 952
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
953 953

	
954 954
  };
955 955

	
956 956
  /// Auxiliary class for the function-type interface of BFS algorithm.
957 957

	
958 958
  /// This auxiliary class is created to implement the
959 959
  /// \ref bfs() "function-type interface" of \ref Bfs algorithm.
960 960
  /// It does not have own \ref run() method, it uses the functions
961 961
  /// and features of the plain \ref Bfs.
962 962
  ///
963 963
  /// This class should only be used through the \ref bfs() function,
964 964
  /// which makes it easier to use the algorithm.
965 965
  template<class TR>
966 966
  class BfsWizard : public TR
967 967
  {
968 968
    typedef TR Base;
969 969

	
970 970
    ///The type of the digraph the algorithm runs on.
971 971
    typedef typename TR::Digraph Digraph;
972 972

	
973 973
    typedef typename Digraph::Node Node;
974 974
    typedef typename Digraph::NodeIt NodeIt;
975 975
    typedef typename Digraph::Arc Arc;
976 976
    typedef typename Digraph::OutArcIt OutArcIt;
977 977

	
978 978
    ///\brief The type of the map that stores the predecessor
979 979
    ///arcs of the shortest paths.
980 980
    typedef typename TR::PredMap PredMap;
981 981
    ///\brief The type of the map that stores the distances of the nodes.
982 982
    typedef typename TR::DistMap DistMap;
983 983
    ///\brief The type of the map that indicates which nodes are reached.
984 984
    typedef typename TR::ReachedMap ReachedMap;
985 985
    ///\brief The type of the map that indicates which nodes are processed.
986 986
    typedef typename TR::ProcessedMap ProcessedMap;
987 987
    ///The type of the shortest paths
988 988
    typedef typename TR::Path Path;
989 989

	
990 990
  public:
991 991

	
992 992
    /// Constructor.
993 993
    BfsWizard() : TR() {}
994 994

	
995 995
    /// Constructor that requires parameters.
996 996

	
997 997
    /// Constructor that requires parameters.
998 998
    /// These parameters will be the default values for the traits class.
999 999
    /// \param g The digraph the algorithm runs on.
1000 1000
    BfsWizard(const Digraph &g) :
1001 1001
      TR(g) {}
1002 1002

	
1003 1003
    ///Copy constructor
1004 1004
    BfsWizard(const TR &b) : TR(b) {}
1005 1005

	
1006 1006
    ~BfsWizard() {}
1007 1007

	
1008 1008
    ///Runs BFS algorithm from the given source node.
1009 1009

	
1010 1010
    ///This method runs BFS algorithm from node \c s
1011 1011
    ///in order to compute the shortest path to each node.
1012 1012
    void run(Node s)
1013 1013
    {
1014 1014
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
1015 1015
      if (Base::_pred)
1016 1016
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1017 1017
      if (Base::_dist)
1018 1018
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1019 1019
      if (Base::_reached)
1020 1020
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
1021 1021
      if (Base::_processed)
1022 1022
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1023 1023
      if (s!=INVALID)
1024 1024
        alg.run(s);
1025 1025
      else
1026 1026
        alg.run();
1027 1027
    }
1028 1028

	
1029 1029
    ///Finds the shortest path between \c s and \c t.
1030 1030

	
1031 1031
    ///This method runs BFS algorithm from node \c s
1032 1032
    ///in order to compute the shortest path to node \c t
1033 1033
    ///(it stops searching when \c t is processed).
1034 1034
    ///
1035 1035
    ///\return \c true if \c t is reachable form \c s.
1036 1036
    bool run(Node s, Node t)
1037 1037
    {
1038 1038
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
1039 1039
      if (Base::_pred)
1040 1040
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1041 1041
      if (Base::_dist)
1042 1042
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1043 1043
      if (Base::_reached)
1044 1044
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
1045 1045
      if (Base::_processed)
1046 1046
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1047 1047
      alg.run(s,t);
1048 1048
      if (Base::_path)
1049 1049
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
1050 1050
      if (Base::_di)
1051 1051
        *Base::_di = alg.dist(t);
1052 1052
      return alg.reached(t);
1053 1053
    }
1054 1054

	
1055 1055
    ///Runs BFS algorithm to visit all nodes in the digraph.
1056 1056

	
1057 1057
    ///This method runs BFS algorithm in order to compute
1058 1058
    ///the shortest path to each node.
1059 1059
    void run()
1060 1060
    {
1061 1061
      run(INVALID);
1062 1062
    }
1063 1063

	
1064 1064
    template<class T>
1065 1065
    struct SetPredMapBase : public Base {
1066 1066
      typedef T PredMap;
1067 1067
      static PredMap *createPredMap(const Digraph &) { return 0; };
1068 1068
      SetPredMapBase(const TR &b) : TR(b) {}
1069 1069
    };
1070 1070
    ///\brief \ref named-func-param "Named parameter"
1071
    ///for setting \ref PredMap object.
1071
    ///for setting PredMap object.
1072 1072
    ///
1073 1073
    ///\ref named-func-param "Named parameter"
1074
    ///for setting \ref PredMap object.
1074
    ///for setting PredMap object.
1075 1075
    template<class T>
1076 1076
    BfsWizard<SetPredMapBase<T> > predMap(const T &t)
1077 1077
    {
1078 1078
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1079 1079
      return BfsWizard<SetPredMapBase<T> >(*this);
1080 1080
    }
1081 1081

	
1082 1082
    template<class T>
1083 1083
    struct SetReachedMapBase : public Base {
1084 1084
      typedef T ReachedMap;
1085 1085
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1086 1086
      SetReachedMapBase(const TR &b) : TR(b) {}
1087 1087
    };
1088 1088
    ///\brief \ref named-func-param "Named parameter"
1089
    ///for setting \ref ReachedMap object.
1089
    ///for setting ReachedMap object.
1090 1090
    ///
1091 1091
    /// \ref named-func-param "Named parameter"
1092
    ///for setting \ref ReachedMap object.
1092
    ///for setting ReachedMap object.
1093 1093
    template<class T>
1094 1094
    BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1095 1095
    {
1096 1096
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1097 1097
      return BfsWizard<SetReachedMapBase<T> >(*this);
1098 1098
    }
1099 1099

	
1100 1100
    template<class T>
1101 1101
    struct SetDistMapBase : public Base {
1102 1102
      typedef T DistMap;
1103 1103
      static DistMap *createDistMap(const Digraph &) { return 0; };
1104 1104
      SetDistMapBase(const TR &b) : TR(b) {}
1105 1105
    };
1106 1106
    ///\brief \ref named-func-param "Named parameter"
1107
    ///for setting \ref DistMap object.
1107
    ///for setting DistMap object.
1108 1108
    ///
1109 1109
    /// \ref named-func-param "Named parameter"
1110
    ///for setting \ref DistMap object.
1110
    ///for setting DistMap object.
1111 1111
    template<class T>
1112 1112
    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
1113 1113
    {
1114 1114
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1115 1115
      return BfsWizard<SetDistMapBase<T> >(*this);
1116 1116
    }
1117 1117

	
1118 1118
    template<class T>
1119 1119
    struct SetProcessedMapBase : public Base {
1120 1120
      typedef T ProcessedMap;
1121 1121
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1122 1122
      SetProcessedMapBase(const TR &b) : TR(b) {}
1123 1123
    };
1124 1124
    ///\brief \ref named-func-param "Named parameter"
1125
    ///for setting \ref ProcessedMap object.
1125
    ///for setting ProcessedMap object.
1126 1126
    ///
1127 1127
    /// \ref named-func-param "Named parameter"
1128
    ///for setting \ref ProcessedMap object.
1128
    ///for setting ProcessedMap object.
1129 1129
    template<class T>
1130 1130
    BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1131 1131
    {
1132 1132
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1133 1133
      return BfsWizard<SetProcessedMapBase<T> >(*this);
1134 1134
    }
1135 1135

	
1136 1136
    template<class T>
1137 1137
    struct SetPathBase : public Base {
1138 1138
      typedef T Path;
1139 1139
      SetPathBase(const TR &b) : TR(b) {}
1140 1140
    };
1141 1141
    ///\brief \ref named-func-param "Named parameter"
1142 1142
    ///for getting the shortest path to the target node.
1143 1143
    ///
1144 1144
    ///\ref named-func-param "Named parameter"
1145 1145
    ///for getting the shortest path to the target node.
1146 1146
    template<class T>
1147 1147
    BfsWizard<SetPathBase<T> > path(const T &t)
1148 1148
    {
1149 1149
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1150 1150
      return BfsWizard<SetPathBase<T> >(*this);
1151 1151
    }
1152 1152

	
1153 1153
    ///\brief \ref named-func-param "Named parameter"
1154 1154
    ///for getting the distance of the target node.
1155 1155
    ///
1156 1156
    ///\ref named-func-param "Named parameter"
1157 1157
    ///for getting the distance of the target node.
1158 1158
    BfsWizard dist(const int &d)
1159 1159
    {
1160 1160
      Base::_di=const_cast<int*>(&d);
1161 1161
      return *this;
1162 1162
    }
1163 1163

	
1164 1164
  };
1165 1165

	
1166 1166
  ///Function-type interface for BFS algorithm.
1167 1167

	
1168 1168
  /// \ingroup search
1169 1169
  ///Function-type interface for BFS algorithm.
1170 1170
  ///
1171 1171
  ///This function also has several \ref named-func-param "named parameters",
1172 1172
  ///they are declared as the members of class \ref BfsWizard.
1173 1173
  ///The following examples show how to use these parameters.
1174 1174
  ///\code
1175 1175
  ///  // Compute shortest path from node s to each node
1176 1176
  ///  bfs(g).predMap(preds).distMap(dists).run(s);
1177 1177
  ///
1178 1178
  ///  // Compute shortest path from s to t
1179 1179
  ///  bool reached = bfs(g).path(p).dist(d).run(s,t);
1180 1180
  ///\endcode
1181 1181
  ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
1182 1182
  ///to the end of the parameter list.
1183 1183
  ///\sa BfsWizard
1184 1184
  ///\sa Bfs
1185 1185
  template<class GR>
1186 1186
  BfsWizard<BfsWizardBase<GR> >
1187 1187
  bfs(const GR &digraph)
1188 1188
  {
1189 1189
    return BfsWizard<BfsWizardBase<GR> >(digraph);
1190 1190
  }
1191 1191

	
1192 1192
#ifdef DOXYGEN
1193 1193
  /// \brief Visitor class for BFS.
1194 1194
  ///
1195 1195
  /// This class defines the interface of the BfsVisit events, and
1196 1196
  /// it could be the base of a real visitor class.
1197 1197
  template <typename _Digraph>
1198 1198
  struct BfsVisitor {
1199 1199
    typedef _Digraph Digraph;
1200 1200
    typedef typename Digraph::Arc Arc;
1201 1201
    typedef typename Digraph::Node Node;
1202 1202
    /// \brief Called for the source node(s) of the BFS.
1203 1203
    ///
1204 1204
    /// This function is called for the source node(s) of the BFS.
1205 1205
    void start(const Node& node) {}
1206 1206
    /// \brief Called when a node is reached first time.
1207 1207
    ///
1208 1208
    /// This function is called when a node is reached first time.
1209 1209
    void reach(const Node& node) {}
1210 1210
    /// \brief Called when a node is processed.
1211 1211
    ///
1212 1212
    /// This function is called when a node is processed.
1213 1213
    void process(const Node& node) {}
1214 1214
    /// \brief Called when an arc reaches a new node.
1215 1215
    ///
1216 1216
    /// This function is called when the BFS finds an arc whose target node
1217 1217
    /// is not reached yet.
1218 1218
    void discover(const Arc& arc) {}
1219 1219
    /// \brief Called when an arc is examined but its target node is
1220 1220
    /// already discovered.
1221 1221
    ///
1222 1222
    /// This function is called when an arc is examined but its target node is
1223 1223
    /// already discovered.
1224 1224
    void examine(const Arc& arc) {}
1225 1225
  };
1226 1226
#else
1227 1227
  template <typename _Digraph>
1228 1228
  struct BfsVisitor {
1229 1229
    typedef _Digraph Digraph;
1230 1230
    typedef typename Digraph::Arc Arc;
1231 1231
    typedef typename Digraph::Node Node;
1232 1232
    void start(const Node&) {}
1233 1233
    void reach(const Node&) {}
1234 1234
    void process(const Node&) {}
1235 1235
    void discover(const Arc&) {}
1236 1236
    void examine(const Arc&) {}
1237 1237

	
1238 1238
    template <typename _Visitor>
1239 1239
    struct Constraints {
1240 1240
      void constraints() {
1241 1241
        Arc arc;
1242 1242
        Node node;
1243 1243
        visitor.start(node);
1244 1244
        visitor.reach(node);
1245 1245
        visitor.process(node);
1246 1246
        visitor.discover(arc);
1247 1247
        visitor.examine(arc);
1248 1248
      }
1249 1249
      _Visitor& visitor;
1250 1250
    };
1251 1251
  };
1252 1252
#endif
1253 1253

	
1254 1254
  /// \brief Default traits class of BfsVisit class.
1255 1255
  ///
1256 1256
  /// Default traits class of BfsVisit class.
1257 1257
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1258 1258
  template<class _Digraph>
1259 1259
  struct BfsVisitDefaultTraits {
1260 1260

	
1261 1261
    /// \brief The type of the digraph the algorithm runs on.
1262 1262
    typedef _Digraph Digraph;
1263 1263

	
1264 1264
    /// \brief The type of the map that indicates which nodes are reached.
1265 1265
    ///
1266 1266
    /// The type of the map that indicates which nodes are reached.
1267 1267
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1268 1268
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1269 1269

	
1270
    /// \brief Instantiates a \ref ReachedMap.
1270
    /// \brief Instantiates a ReachedMap.
1271 1271
    ///
1272
    /// This function instantiates a \ref ReachedMap.
1272
    /// This function instantiates a ReachedMap.
1273 1273
    /// \param digraph is the digraph, to which
1274
    /// we would like to define the \ref ReachedMap.
1274
    /// we would like to define the ReachedMap.
1275 1275
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1276 1276
      return new ReachedMap(digraph);
1277 1277
    }
1278 1278

	
1279 1279
  };
1280 1280

	
1281 1281
  /// \ingroup search
1282 1282
  ///
1283 1283
  /// \brief %BFS algorithm class with visitor interface.
1284 1284
  ///
1285 1285
  /// This class provides an efficient implementation of the %BFS algorithm
1286 1286
  /// with visitor interface.
1287 1287
  ///
1288 1288
  /// The %BfsVisit class provides an alternative interface to the Bfs
1289 1289
  /// class. It works with callback mechanism, the BfsVisit object calls
1290 1290
  /// the member functions of the \c Visitor class on every BFS event.
1291 1291
  ///
1292 1292
  /// This interface of the BFS algorithm should be used in special cases
1293 1293
  /// when extra actions have to be performed in connection with certain
1294 1294
  /// events of the BFS algorithm. Otherwise consider to use Bfs or bfs()
1295 1295
  /// instead.
1296 1296
  ///
1297 1297
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1298 1298
  /// The default value is
1299 1299
  /// \ref ListDigraph. The value of _Digraph is not used directly by
1300 1300
  /// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits.
1301 1301
  /// \tparam _Visitor The Visitor type that is used by the algorithm.
1302 1302
  /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which
1303 1303
  /// does not observe the BFS events. If you want to observe the BFS
1304 1304
  /// events, you should implement your own visitor class.
1305 1305
  /// \tparam _Traits Traits class to set various data types used by the
1306 1306
  /// algorithm. The default traits class is
1307 1307
  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
1308 1308
  /// See \ref BfsVisitDefaultTraits for the documentation of
1309 1309
  /// a BFS visit traits class.
1310 1310
#ifdef DOXYGEN
1311 1311
  template <typename _Digraph, typename _Visitor, typename _Traits>
1312 1312
#else
1313 1313
  template <typename _Digraph = ListDigraph,
1314 1314
            typename _Visitor = BfsVisitor<_Digraph>,
1315 1315
            typename _Traits = BfsVisitDefaultTraits<_Digraph> >
1316 1316
#endif
1317 1317
  class BfsVisit {
1318 1318
  public:
1319 1319

	
1320 1320
    ///The traits class.
1321 1321
    typedef _Traits Traits;
1322 1322

	
1323 1323
    ///The type of the digraph the algorithm runs on.
1324 1324
    typedef typename Traits::Digraph Digraph;
1325 1325

	
1326 1326
    ///The visitor type used by the algorithm.
1327 1327
    typedef _Visitor Visitor;
1328 1328

	
1329 1329
    ///The type of the map that indicates which nodes are reached.
1330 1330
    typedef typename Traits::ReachedMap ReachedMap;
1331 1331

	
1332 1332
  private:
1333 1333

	
1334 1334
    typedef typename Digraph::Node Node;
1335 1335
    typedef typename Digraph::NodeIt NodeIt;
1336 1336
    typedef typename Digraph::Arc Arc;
1337 1337
    typedef typename Digraph::OutArcIt OutArcIt;
1338 1338

	
1339 1339
    //Pointer to the underlying digraph.
1340 1340
    const Digraph *_digraph;
1341 1341
    //Pointer to the visitor object.
1342 1342
    Visitor *_visitor;
1343 1343
    //Pointer to the map of reached status of the nodes.
1344 1344
    ReachedMap *_reached;
1345 1345
    //Indicates if _reached is locally allocated (true) or not.
1346 1346
    bool local_reached;
1347 1347

	
1348 1348
    std::vector<typename Digraph::Node> _list;
1349 1349
    int _list_front, _list_back;
1350 1350

	
1351 1351
    //Creates the maps if necessary.
1352 1352
    void create_maps() {
1353 1353
      if(!_reached) {
1354 1354
        local_reached = true;
1355 1355
        _reached = Traits::createReachedMap(*_digraph);
1356 1356
      }
1357 1357
    }
1358 1358

	
1359 1359
  protected:
1360 1360

	
1361 1361
    BfsVisit() {}
1362 1362

	
1363 1363
  public:
1364 1364

	
1365 1365
    typedef BfsVisit Create;
1366 1366

	
1367 1367
    /// \name Named template parameters
1368 1368

	
1369 1369
    ///@{
1370 1370
    template <class T>
1371 1371
    struct SetReachedMapTraits : public Traits {
1372 1372
      typedef T ReachedMap;
1373 1373
      static ReachedMap *createReachedMap(const Digraph &digraph) {
1374 1374
        LEMON_ASSERT(false, "ReachedMap is not initialized");
1375 1375
        return 0; // ignore warnings
1376 1376
      }
1377 1377
    };
1378 1378
    /// \brief \ref named-templ-param "Named parameter" for setting
1379 1379
    /// ReachedMap type.
1380 1380
    ///
1381 1381
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1382 1382
    template <class T>
1383 1383
    struct SetReachedMap : public BfsVisit< Digraph, Visitor,
1384 1384
                                            SetReachedMapTraits<T> > {
1385 1385
      typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1386 1386
    };
1387 1387
    ///@}
1388 1388

	
1389 1389
  public:
1390 1390

	
1391 1391
    /// \brief Constructor.
1392 1392
    ///
1393 1393
    /// Constructor.
1394 1394
    ///
1395 1395
    /// \param digraph The digraph the algorithm runs on.
1396 1396
    /// \param visitor The visitor object of the algorithm.
1397 1397
    BfsVisit(const Digraph& digraph, Visitor& visitor)
1398 1398
      : _digraph(&digraph), _visitor(&visitor),
1399 1399
        _reached(0), local_reached(false) {}
1400 1400

	
1401 1401
    /// \brief Destructor.
1402 1402
    ~BfsVisit() {
1403 1403
      if(local_reached) delete _reached;
1404 1404
    }
1405 1405

	
1406 1406
    /// \brief Sets the map that indicates which nodes are reached.
1407 1407
    ///
1408 1408
    /// Sets the map that indicates which nodes are reached.
1409 1409
    /// If you don't use this function before calling \ref run(),
1410 1410
    /// it will allocate one. The destructor deallocates this
1411 1411
    /// automatically allocated map, of course.
1412 1412
    /// \return <tt> (*this) </tt>
1413 1413
    BfsVisit &reachedMap(ReachedMap &m) {
1414 1414
      if(local_reached) {
1415 1415
        delete _reached;
1416 1416
        local_reached = false;
1417 1417
      }
1418 1418
      _reached = &m;
1419 1419
      return *this;
1420 1420
    }
1421 1421

	
1422 1422
  public:
1423 1423

	
1424 1424
    /// \name Execution control
1425 1425
    /// The simplest way to execute the algorithm is to use
1426 1426
    /// one of the member functions called \ref lemon::BfsVisit::run()
1427 1427
    /// "run()".
1428 1428
    /// \n
1429 1429
    /// If you need more control on the execution, first you must call
1430 1430
    /// \ref lemon::BfsVisit::init() "init()", then you can add several
1431 1431
    /// source nodes with \ref lemon::BfsVisit::addSource() "addSource()".
1432 1432
    /// Finally \ref lemon::BfsVisit::start() "start()" will perform the
1433 1433
    /// actual path computation.
1434 1434

	
1435 1435
    /// @{
1436 1436

	
1437 1437
    /// \brief Initializes the internal data structures.
1438 1438
    ///
1439 1439
    /// Initializes the internal data structures.
1440 1440
    void init() {
1441 1441
      create_maps();
1442 1442
      _list.resize(countNodes(*_digraph));
1443 1443
      _list_front = _list_back = -1;
1444 1444
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1445 1445
        _reached->set(u, false);
1446 1446
      }
1447 1447
    }
1448 1448

	
1449 1449
    /// \brief Adds a new source node.
1450 1450
    ///
1451 1451
    /// Adds a new source node to the set of nodes to be processed.
1452 1452
    void addSource(Node s) {
1453 1453
      if(!(*_reached)[s]) {
1454 1454
          _reached->set(s,true);
1455 1455
          _visitor->start(s);
1456 1456
          _visitor->reach(s);
1457 1457
          _list[++_list_back] = s;
1458 1458
        }
1459 1459
    }
1460 1460

	
1461 1461
    /// \brief Processes the next node.
1462 1462
    ///
1463 1463
    /// Processes the next node.
1464 1464
    ///
1465 1465
    /// \return The processed node.
1466 1466
    ///
1467 1467
    /// \pre The queue must not be empty.
1468 1468
    Node processNextNode() {
1469 1469
      Node n = _list[++_list_front];
1470 1470
      _visitor->process(n);
1471 1471
      Arc e;
1472 1472
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1473 1473
        Node m = _digraph->target(e);
1474 1474
        if (!(*_reached)[m]) {
1475 1475
          _visitor->discover(e);
1476 1476
          _visitor->reach(m);
1477 1477
          _reached->set(m, true);
1478 1478
          _list[++_list_back] = m;
1479 1479
        } else {
1480 1480
          _visitor->examine(e);
1481 1481
        }
1482 1482
      }
1483 1483
      return n;
1484 1484
    }
1485 1485

	
1486 1486
    /// \brief Processes the next node.
1487 1487
    ///
1488 1488
    /// Processes the next node and checks if the given target node
1489 1489
    /// is reached. If the target node is reachable from the processed
1490 1490
    /// node, then the \c reach parameter will be set to \c true.
1491 1491
    ///
1492 1492
    /// \param target The target node.
1493 1493
    /// \retval reach Indicates if the target node is reached.
1494 1494
    /// It should be initially \c false.
1495 1495
    ///
1496 1496
    /// \return The processed node.
1497 1497
    ///
1498 1498
    /// \pre The queue must not be empty.
1499 1499
    Node processNextNode(Node target, bool& reach) {
1500 1500
      Node n = _list[++_list_front];
1501 1501
      _visitor->process(n);
1502 1502
      Arc e;
1503 1503
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1504 1504
        Node m = _digraph->target(e);
1505 1505
        if (!(*_reached)[m]) {
1506 1506
          _visitor->discover(e);
1507 1507
          _visitor->reach(m);
1508 1508
          _reached->set(m, true);
1509 1509
          _list[++_list_back] = m;
1510 1510
          reach = reach || (target == m);
1511 1511
        } else {
1512 1512
          _visitor->examine(e);
1513 1513
        }
1514 1514
      }
1515 1515
      return n;
1516 1516
    }
1517 1517

	
1518 1518
    /// \brief Processes the next node.
1519 1519
    ///
1520 1520
    /// Processes the next node and checks if at least one of reached
1521 1521
    /// nodes has \c true value in the \c nm node map. If one node
1522 1522
    /// with \c true value is reachable from the processed node, then the
1523 1523
    /// \c rnode parameter will be set to the first of such nodes.
1524 1524
    ///
1525 1525
    /// \param nm A \c bool (or convertible) node map that indicates the
1526 1526
    /// possible targets.
1527 1527
    /// \retval rnode The reached target node.
1528 1528
    /// It should be initially \c INVALID.
1529 1529
    ///
1530 1530
    /// \return The processed node.
1531 1531
    ///
1532 1532
    /// \pre The queue must not be empty.
1533 1533
    template <typename NM>
1534 1534
    Node processNextNode(const NM& nm, Node& rnode) {
1535 1535
      Node n = _list[++_list_front];
1536 1536
      _visitor->process(n);
1537 1537
      Arc e;
1538 1538
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1539 1539
        Node m = _digraph->target(e);
1540 1540
        if (!(*_reached)[m]) {
1541 1541
          _visitor->discover(e);
1542 1542
          _visitor->reach(m);
1543 1543
          _reached->set(m, true);
1544 1544
          _list[++_list_back] = m;
1545 1545
          if (nm[m] && rnode == INVALID) rnode = m;
1546 1546
        } else {
1547 1547
          _visitor->examine(e);
1548 1548
        }
1549 1549
      }
1550 1550
      return n;
1551 1551
    }
1552 1552

	
1553 1553
    /// \brief The next node to be processed.
1554 1554
    ///
1555 1555
    /// Returns the next node to be processed or \c INVALID if the queue
1556 1556
    /// is empty.
1557 1557
    Node nextNode() const {
1558 1558
      return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
1559 1559
    }
1560 1560

	
1561 1561
    /// \brief Returns \c false if there are nodes
1562 1562
    /// to be processed.
1563 1563
    ///
1564 1564
    /// Returns \c false if there are nodes
1565 1565
    /// to be processed in the queue.
1566 1566
    bool emptyQueue() const { return _list_front == _list_back; }
1567 1567

	
1568 1568
    /// \brief Returns the number of the nodes to be processed.
1569 1569
    ///
1570 1570
    /// Returns the number of the nodes to be processed in the queue.
1571 1571
    int queueSize() const { return _list_back - _list_front; }
1572 1572

	
1573 1573
    /// \brief Executes the algorithm.
1574 1574
    ///
1575 1575
    /// Executes the algorithm.
1576 1576
    ///
1577 1577
    /// This method runs the %BFS algorithm from the root node(s)
1578 1578
    /// in order to compute the shortest path to each node.
1579 1579
    ///
1580 1580
    /// The algorithm computes
1581 1581
    /// - the shortest path tree (forest),
1582 1582
    /// - the distance of each node from the root(s).
1583 1583
    ///
1584 1584
    /// \pre init() must be called and at least one root node should be added
1585 1585
    /// with addSource() before using this function.
1586 1586
    ///
1587 1587
    /// \note <tt>b.start()</tt> is just a shortcut of the following code.
1588 1588
    /// \code
1589 1589
    ///   while ( !b.emptyQueue() ) {
1590 1590
    ///     b.processNextNode();
1591 1591
    ///   }
1592 1592
    /// \endcode
1593 1593
    void start() {
1594 1594
      while ( !emptyQueue() ) processNextNode();
1595 1595
    }
1596 1596

	
1597 1597
    /// \brief Executes the algorithm until the given target node is reached.
1598 1598
    ///
1599 1599
    /// Executes the algorithm until the given target node is reached.
1600 1600
    ///
1601 1601
    /// This method runs the %BFS algorithm from the root node(s)
1602 1602
    /// in order to compute the shortest path to \c t.
1603 1603
    ///
1604 1604
    /// The algorithm computes
1605 1605
    /// - the shortest path to \c t,
1606 1606
    /// - the distance of \c t from the root(s).
1607 1607
    ///
1608 1608
    /// \pre init() must be called and at least one root node should be
1609 1609
    /// added with addSource() before using this function.
1610 1610
    ///
1611 1611
    /// \note <tt>b.start(t)</tt> is just a shortcut of the following code.
1612 1612
    /// \code
1613 1613
    ///   bool reach = false;
1614 1614
    ///   while ( !b.emptyQueue() && !reach ) {
1615 1615
    ///     b.processNextNode(t, reach);
1616 1616
    ///   }
1617 1617
    /// \endcode
1618 1618
    void start(Node t) {
1619 1619
      bool reach = false;
1620 1620
      while ( !emptyQueue() && !reach ) processNextNode(t, reach);
1621 1621
    }
1622 1622

	
1623 1623
    /// \brief Executes the algorithm until a condition is met.
1624 1624
    ///
1625 1625
    /// Executes the algorithm until a condition is met.
1626 1626
    ///
1627 1627
    /// This method runs the %BFS algorithm from the root node(s) in
1628 1628
    /// order to compute the shortest path to a node \c v with
1629 1629
    /// <tt>nm[v]</tt> true, if such a node can be found.
1630 1630
    ///
1631 1631
    /// \param nm must be a bool (or convertible) node map. The
1632 1632
    /// algorithm will stop when it reaches a node \c v with
1633 1633
    /// <tt>nm[v]</tt> true.
1634 1634
    ///
1635 1635
    /// \return The reached node \c v with <tt>nm[v]</tt> true or
1636 1636
    /// \c INVALID if no such node was found.
1637 1637
    ///
1638 1638
    /// \pre init() must be called and at least one root node should be
1639 1639
    /// added with addSource() before using this function.
1640 1640
    ///
1641 1641
    /// \note <tt>b.start(nm)</tt> is just a shortcut of the following code.
1642 1642
    /// \code
1643 1643
    ///   Node rnode = INVALID;
1644 1644
    ///   while ( !b.emptyQueue() && rnode == INVALID ) {
1645 1645
    ///     b.processNextNode(nm, rnode);
1646 1646
    ///   }
1647 1647
    ///   return rnode;
1648 1648
    /// \endcode
1649 1649
    template <typename NM>
1650 1650
    Node start(const NM &nm) {
1651 1651
      Node rnode = INVALID;
1652 1652
      while ( !emptyQueue() && rnode == INVALID ) {
1653 1653
        processNextNode(nm, rnode);
1654 1654
      }
1655 1655
      return rnode;
1656 1656
    }
1657 1657

	
1658 1658
    /// \brief Runs the algorithm from the given source node.
1659 1659
    ///
1660 1660
    /// This method runs the %BFS algorithm from node \c s
1661 1661
    /// in order to compute the shortest path to each node.
1662 1662
    ///
1663 1663
    /// The algorithm computes
1664 1664
    /// - the shortest path tree,
1665 1665
    /// - the distance of each node from the root.
1666 1666
    ///
1667 1667
    /// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
1668 1668
    ///\code
1669 1669
    ///   b.init();
1670 1670
    ///   b.addSource(s);
1671 1671
    ///   b.start();
1672 1672
    ///\endcode
1673 1673
    void run(Node s) {
1674 1674
      init();
1675 1675
      addSource(s);
1676 1676
      start();
1677 1677
    }
1678 1678

	
1679 1679
    /// \brief Finds the shortest path between \c s and \c t.
1680 1680
    ///
1681 1681
    /// This method runs the %BFS algorithm from node \c s
1682 1682
    /// in order to compute the shortest path to node \c t
1683 1683
    /// (it stops searching when \c t is processed).
1684 1684
    ///
1685 1685
    /// \return \c true if \c t is reachable form \c s.
1686 1686
    ///
1687 1687
    /// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a
1688 1688
    /// shortcut of the following code.
1689 1689
    ///\code
1690 1690
    ///   b.init();
1691 1691
    ///   b.addSource(s);
1692 1692
    ///   b.start(t);
1693 1693
    ///\endcode
1694 1694
    bool run(Node s,Node t) {
1695 1695
      init();
1696 1696
      addSource(s);
1697 1697
      start(t);
1698 1698
      return reached(t);
1699 1699
    }
1700 1700

	
1701 1701
    /// \brief Runs the algorithm to visit all nodes in the digraph.
1702 1702
    ///
1703 1703
    /// This method runs the %BFS algorithm in order to
1704 1704
    /// compute the shortest path to each node.
1705 1705
    ///
1706 1706
    /// The algorithm computes
1707 1707
    /// - the shortest path tree (forest),
1708 1708
    /// - the distance of each node from the root(s).
1709 1709
    ///
1710 1710
    /// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
1711 1711
    ///\code
1712 1712
    ///  b.init();
1713 1713
    ///  for (NodeIt n(gr); n != INVALID; ++n) {
1714 1714
    ///    if (!b.reached(n)) {
1715 1715
    ///      b.addSource(n);
1716 1716
    ///      b.start();
1717 1717
    ///    }
1718 1718
    ///  }
1719 1719
    ///\endcode
1720 1720
    void run() {
1721 1721
      init();
1722 1722
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
1723 1723
        if (!reached(it)) {
1724 1724
          addSource(it);
1725 1725
          start();
1726 1726
        }
1727 1727
      }
1728 1728
    }
1729 1729

	
1730 1730
    ///@}
1731 1731

	
1732 1732
    /// \name Query Functions
1733 1733
    /// The result of the %BFS algorithm can be obtained using these
1734 1734
    /// functions.\n
1735 1735
    /// Either \ref lemon::BfsVisit::run() "run()" or
1736 1736
    /// \ref lemon::BfsVisit::start() "start()" must be called before
1737 1737
    /// using them.
1738 1738
    ///@{
1739 1739

	
1740 1740
    /// \brief Checks if a node is reachable from the root(s).
1741 1741
    ///
1742 1742
    /// Returns \c true if \c v is reachable from the root(s).
1743 1743
    /// \pre Either \ref run() or \ref start()
1744 1744
    /// must be called before using this function.
1745 1745
    bool reached(Node v) { return (*_reached)[v]; }
1746 1746

	
1747 1747
    ///@}
1748 1748

	
1749 1749
  };
1750 1750

	
1751 1751
} //END OF NAMESPACE LEMON
1752 1752

	
1753 1753
#endif
Ignore white space 3072 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_DFS_H
20 20
#define LEMON_DFS_H
21 21

	
22 22
///\ingroup search
23 23
///\file
24 24
///\brief DFS algorithm.
25 25

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

	
34 34
namespace lemon {
35 35

	
36 36
  ///Default traits class of Dfs class.
37 37

	
38 38
  ///Default traits class of Dfs class.
39 39
  ///\tparam GR Digraph type.
40 40
  template<class GR>
41 41
  struct DfsDefaultTraits
42 42
  {
43 43
    ///The type of the digraph the algorithm runs on.
44 44
    typedef GR Digraph;
45 45

	
46 46
    ///\brief The type of the map that stores the predecessor
47 47
    ///arcs of the %DFS paths.
48 48
    ///
49 49
    ///The type of the map that stores the predecessor
50 50
    ///arcs of the %DFS paths.
51 51
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
52 52
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
53
    ///Instantiates a \ref PredMap.
53
    ///Instantiates a PredMap.
54 54

	
55
    ///This function instantiates a \ref PredMap.
55
    ///This function instantiates a PredMap.
56 56
    ///\param g is the digraph, to which we would like to define the
57
    ///\ref PredMap.
57
    ///PredMap.
58 58
    static PredMap *createPredMap(const Digraph &g)
59 59
    {
60 60
      return new PredMap(g);
61 61
    }
62 62

	
63 63
    ///The type of the map that indicates which nodes are processed.
64 64

	
65 65
    ///The type of the map that indicates which nodes are processed.
66 66
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
67 67
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
68
    ///Instantiates a \ref ProcessedMap.
68
    ///Instantiates a ProcessedMap.
69 69

	
70
    ///This function instantiates a \ref ProcessedMap.
70
    ///This function instantiates a ProcessedMap.
71 71
    ///\param g is the digraph, to which
72
    ///we would like to define the \ref ProcessedMap
72
    ///we would like to define the ProcessedMap
73 73
#ifdef DOXYGEN
74 74
    static ProcessedMap *createProcessedMap(const Digraph &g)
75 75
#else
76 76
    static ProcessedMap *createProcessedMap(const Digraph &)
77 77
#endif
78 78
    {
79 79
      return new ProcessedMap();
80 80
    }
81 81

	
82 82
    ///The type of the map that indicates which nodes are reached.
83 83

	
84 84
    ///The type of the map that indicates which nodes are reached.
85 85
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
86 86
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
87
    ///Instantiates a \ref ReachedMap.
87
    ///Instantiates a ReachedMap.
88 88

	
89
    ///This function instantiates a \ref ReachedMap.
89
    ///This function instantiates a ReachedMap.
90 90
    ///\param g is the digraph, to which
91
    ///we would like to define the \ref ReachedMap.
91
    ///we would like to define the ReachedMap.
92 92
    static ReachedMap *createReachedMap(const Digraph &g)
93 93
    {
94 94
      return new ReachedMap(g);
95 95
    }
96 96

	
97 97
    ///The type of the map that stores the distances of the nodes.
98 98

	
99 99
    ///The type of the map that stores the distances of the nodes.
100 100
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
101 101
    typedef typename Digraph::template NodeMap<int> DistMap;
102
    ///Instantiates a \ref DistMap.
102
    ///Instantiates a DistMap.
103 103

	
104
    ///This function instantiates a \ref DistMap.
104
    ///This function instantiates a DistMap.
105 105
    ///\param g is the digraph, to which we would like to define the
106
    ///\ref DistMap.
106
    ///DistMap.
107 107
    static DistMap *createDistMap(const Digraph &g)
108 108
    {
109 109
      return new DistMap(g);
110 110
    }
111 111
  };
112 112

	
113 113
  ///%DFS algorithm class.
114 114

	
115 115
  ///\ingroup search
116 116
  ///This class provides an efficient implementation of the %DFS algorithm.
117 117
  ///
118 118
  ///There is also a \ref dfs() "function-type interface" for the DFS
119 119
  ///algorithm, which is convenient in the simplier cases and it can be
120 120
  ///used easier.
121 121
  ///
122 122
  ///\tparam GR The type of the digraph the algorithm runs on.
123 123
  ///The default value is \ref ListDigraph. The value of GR is not used
124 124
  ///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits.
125 125
  ///\tparam TR Traits class to set various data types used by the algorithm.
126 126
  ///The default traits class is
127 127
  ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
128 128
  ///See \ref DfsDefaultTraits for the documentation of
129 129
  ///a Dfs traits class.
130 130
#ifdef DOXYGEN
131 131
  template <typename GR,
132 132
            typename TR>
133 133
#else
134 134
  template <typename GR=ListDigraph,
135 135
            typename TR=DfsDefaultTraits<GR> >
136 136
#endif
137 137
  class Dfs {
138 138
  public:
139 139

	
140 140
    ///The type of the digraph the algorithm runs on.
141 141
    typedef typename TR::Digraph Digraph;
142 142

	
143 143
    ///\brief The type of the map that stores the predecessor arcs of the
144 144
    ///DFS paths.
145 145
    typedef typename TR::PredMap PredMap;
146 146
    ///The type of the map that stores the distances of the nodes.
147 147
    typedef typename TR::DistMap DistMap;
148 148
    ///The type of the map that indicates which nodes are reached.
149 149
    typedef typename TR::ReachedMap ReachedMap;
150 150
    ///The type of the map that indicates which nodes are processed.
151 151
    typedef typename TR::ProcessedMap ProcessedMap;
152 152
    ///The type of the paths.
153 153
    typedef PredMapPath<Digraph, PredMap> Path;
154 154

	
155 155
    ///The traits class.
156 156
    typedef TR Traits;
157 157

	
158 158
  private:
159 159

	
160 160
    typedef typename Digraph::Node Node;
161 161
    typedef typename Digraph::NodeIt NodeIt;
162 162
    typedef typename Digraph::Arc Arc;
163 163
    typedef typename Digraph::OutArcIt OutArcIt;
164 164

	
165 165
    //Pointer to the underlying digraph.
166 166
    const Digraph *G;
167 167
    //Pointer to the map of predecessor arcs.
168 168
    PredMap *_pred;
169 169
    //Indicates if _pred is locally allocated (true) or not.
170 170
    bool local_pred;
171 171
    //Pointer to the map of distances.
172 172
    DistMap *_dist;
173 173
    //Indicates if _dist is locally allocated (true) or not.
174 174
    bool local_dist;
175 175
    //Pointer to the map of reached status of the nodes.
176 176
    ReachedMap *_reached;
177 177
    //Indicates if _reached is locally allocated (true) or not.
178 178
    bool local_reached;
179 179
    //Pointer to the map of processed status of the nodes.
180 180
    ProcessedMap *_processed;
181 181
    //Indicates if _processed is locally allocated (true) or not.
182 182
    bool local_processed;
183 183

	
184 184
    std::vector<typename Digraph::OutArcIt> _stack;
185 185
    int _stack_head;
186 186

	
187 187
    //Creates the maps if necessary.
188 188
    void create_maps()
189 189
    {
190 190
      if(!_pred) {
191 191
        local_pred = true;
192 192
        _pred = Traits::createPredMap(*G);
193 193
      }
194 194
      if(!_dist) {
195 195
        local_dist = true;
196 196
        _dist = Traits::createDistMap(*G);
197 197
      }
198 198
      if(!_reached) {
199 199
        local_reached = true;
200 200
        _reached = Traits::createReachedMap(*G);
201 201
      }
202 202
      if(!_processed) {
203 203
        local_processed = true;
204 204
        _processed = Traits::createProcessedMap(*G);
205 205
      }
206 206
    }
207 207

	
208 208
  protected:
209 209

	
210 210
    Dfs() {}
211 211

	
212 212
  public:
213 213

	
214 214
    typedef Dfs Create;
215 215

	
216 216
    ///\name Named template parameters
217 217

	
218 218
    ///@{
219 219

	
220 220
    template <class T>
221 221
    struct SetPredMapTraits : public Traits {
222 222
      typedef T PredMap;
223 223
      static PredMap *createPredMap(const Digraph &)
224 224
      {
225 225
        LEMON_ASSERT(false, "PredMap is not initialized");
226 226
        return 0; // ignore warnings
227 227
      }
228 228
    };
229 229
    ///\brief \ref named-templ-param "Named parameter" for setting
230
    ///\ref PredMap type.
230
    ///PredMap type.
231 231
    ///
232 232
    ///\ref named-templ-param "Named parameter" for setting
233
    ///\ref PredMap type.
233
    ///PredMap type.
234 234
    template <class T>
235 235
    struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
236 236
      typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
237 237
    };
238 238

	
239 239
    template <class T>
240 240
    struct SetDistMapTraits : public Traits {
241 241
      typedef T DistMap;
242 242
      static DistMap *createDistMap(const Digraph &)
243 243
      {
244 244
        LEMON_ASSERT(false, "DistMap is not initialized");
245 245
        return 0; // ignore warnings
246 246
      }
247 247
    };
248 248
    ///\brief \ref named-templ-param "Named parameter" for setting
249
    ///\ref DistMap type.
249
    ///DistMap type.
250 250
    ///
251 251
    ///\ref named-templ-param "Named parameter" for setting
252
    ///\ref DistMap type.
252
    ///DistMap type.
253 253
    template <class T>
254 254
    struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
255 255
      typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
256 256
    };
257 257

	
258 258
    template <class T>
259 259
    struct SetReachedMapTraits : public Traits {
260 260
      typedef T ReachedMap;
261 261
      static ReachedMap *createReachedMap(const Digraph &)
262 262
      {
263 263
        LEMON_ASSERT(false, "ReachedMap is not initialized");
264 264
        return 0; // ignore warnings
265 265
      }
266 266
    };
267 267
    ///\brief \ref named-templ-param "Named parameter" for setting
268
    ///\ref ReachedMap type.
268
    ///ReachedMap type.
269 269
    ///
270 270
    ///\ref named-templ-param "Named parameter" for setting
271
    ///\ref ReachedMap type.
271
    ///ReachedMap type.
272 272
    template <class T>
273 273
    struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
274 274
      typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
275 275
    };
276 276

	
277 277
    template <class T>
278 278
    struct SetProcessedMapTraits : public Traits {
279 279
      typedef T ProcessedMap;
280 280
      static ProcessedMap *createProcessedMap(const Digraph &)
281 281
      {
282 282
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
283 283
        return 0; // ignore warnings
284 284
      }
285 285
    };
286 286
    ///\brief \ref named-templ-param "Named parameter" for setting
287
    ///\ref ProcessedMap type.
287
    ///ProcessedMap type.
288 288
    ///
289 289
    ///\ref named-templ-param "Named parameter" for setting
290
    ///\ref ProcessedMap type.
290
    ///ProcessedMap type.
291 291
    template <class T>
292 292
    struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
293 293
      typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
294 294
    };
295 295

	
296 296
    struct SetStandardProcessedMapTraits : public Traits {
297 297
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
298 298
      static ProcessedMap *createProcessedMap(const Digraph &g)
299 299
      {
300 300
        return new ProcessedMap(g);
301 301
      }
302 302
    };
303 303
    ///\brief \ref named-templ-param "Named parameter" for setting
304
    ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
304
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
305 305
    ///
306 306
    ///\ref named-templ-param "Named parameter" for setting
307
    ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
307
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
308 308
    ///If you don't set it explicitly, it will be automatically allocated.
309 309
    struct SetStandardProcessedMap :
310 310
      public Dfs< Digraph, SetStandardProcessedMapTraits > {
311 311
      typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create;
312 312
    };
313 313

	
314 314
    ///@}
315 315

	
316 316
  public:
317 317

	
318 318
    ///Constructor.
319 319

	
320 320
    ///Constructor.
321 321
    ///\param g The digraph the algorithm runs on.
322 322
    Dfs(const Digraph &g) :
323 323
      G(&g),
324 324
      _pred(NULL), local_pred(false),
325 325
      _dist(NULL), local_dist(false),
326 326
      _reached(NULL), local_reached(false),
327 327
      _processed(NULL), local_processed(false)
328 328
    { }
329 329

	
330 330
    ///Destructor.
331 331
    ~Dfs()
332 332
    {
333 333
      if(local_pred) delete _pred;
334 334
      if(local_dist) delete _dist;
335 335
      if(local_reached) delete _reached;
336 336
      if(local_processed) delete _processed;
337 337
    }
338 338

	
339 339
    ///Sets the map that stores the predecessor arcs.
340 340

	
341 341
    ///Sets the map that stores the predecessor arcs.
342 342
    ///If you don't use this function before calling \ref run(),
343 343
    ///it will allocate one. The destructor deallocates this
344 344
    ///automatically allocated map, of course.
345 345
    ///\return <tt> (*this) </tt>
346 346
    Dfs &predMap(PredMap &m)
347 347
    {
348 348
      if(local_pred) {
349 349
        delete _pred;
350 350
        local_pred=false;
351 351
      }
352 352
      _pred = &m;
353 353
      return *this;
354 354
    }
355 355

	
356 356
    ///Sets the map that indicates which nodes are reached.
357 357

	
358 358
    ///Sets the map that indicates which nodes are reached.
359 359
    ///If you don't use this function before calling \ref run(),
360 360
    ///it will allocate one. The destructor deallocates this
361 361
    ///automatically allocated map, of course.
362 362
    ///\return <tt> (*this) </tt>
363 363
    Dfs &reachedMap(ReachedMap &m)
364 364
    {
365 365
      if(local_reached) {
366 366
        delete _reached;
367 367
        local_reached=false;
368 368
      }
369 369
      _reached = &m;
370 370
      return *this;
371 371
    }
372 372

	
373 373
    ///Sets the map that indicates which nodes are processed.
374 374

	
375 375
    ///Sets the map that indicates which nodes are processed.
376 376
    ///If you don't use this function before calling \ref run(),
377 377
    ///it will allocate one. The destructor deallocates this
378 378
    ///automatically allocated map, of course.
379 379
    ///\return <tt> (*this) </tt>
380 380
    Dfs &processedMap(ProcessedMap &m)
381 381
    {
382 382
      if(local_processed) {
383 383
        delete _processed;
384 384
        local_processed=false;
385 385
      }
386 386
      _processed = &m;
387 387
      return *this;
388 388
    }
389 389

	
390 390
    ///Sets the map that stores the distances of the nodes.
391 391

	
392 392
    ///Sets the map that stores the distances of the nodes calculated by
393 393
    ///the algorithm.
394 394
    ///If you don't use this function before calling \ref run(),
395 395
    ///it will allocate one. The destructor deallocates this
396 396
    ///automatically allocated map, of course.
397 397
    ///\return <tt> (*this) </tt>
398 398
    Dfs &distMap(DistMap &m)
399 399
    {
400 400
      if(local_dist) {
401 401
        delete _dist;
402 402
        local_dist=false;
403 403
      }
404 404
      _dist = &m;
405 405
      return *this;
406 406
    }
407 407

	
408 408
  public:
409 409

	
410 410
    ///\name Execution control
411 411
    ///The simplest way to execute the algorithm is to use
412 412
    ///one of the member functions called \ref lemon::Dfs::run() "run()".
413 413
    ///\n
414 414
    ///If you need more control on the execution, first you must call
415 415
    ///\ref lemon::Dfs::init() "init()", then you can add a source node
416 416
    ///with \ref lemon::Dfs::addSource() "addSource()".
417 417
    ///Finally \ref lemon::Dfs::start() "start()" will perform the
418 418
    ///actual path computation.
419 419

	
420 420
    ///@{
421 421

	
422 422
    ///Initializes the internal data structures.
423 423

	
424 424
    ///Initializes the internal data structures.
425 425
    ///
426 426
    void init()
427 427
    {
428 428
      create_maps();
429 429
      _stack.resize(countNodes(*G));
430 430
      _stack_head=-1;
431 431
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
432 432
        _pred->set(u,INVALID);
433 433
        _reached->set(u,false);
434 434
        _processed->set(u,false);
435 435
      }
436 436
    }
437 437

	
438 438
    ///Adds a new source node.
439 439

	
440 440
    ///Adds a new source node to the set of nodes to be processed.
441 441
    ///
442 442
    ///\pre The stack must be empty. (Otherwise the algorithm gives
443 443
    ///false results.)
444 444
    ///
445 445
    ///\warning Distances will be wrong (or at least strange) in case of
446 446
    ///multiple sources.
447 447
    void addSource(Node s)
448 448
    {
449 449
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
450 450
      if(!(*_reached)[s])
451 451
        {
452 452
          _reached->set(s,true);
453 453
          _pred->set(s,INVALID);
454 454
          OutArcIt e(*G,s);
455 455
          if(e!=INVALID) {
456 456
            _stack[++_stack_head]=e;
457 457
            _dist->set(s,_stack_head);
458 458
          }
459 459
          else {
460 460
            _processed->set(s,true);
461 461
            _dist->set(s,0);
462 462
          }
463 463
        }
464 464
    }
465 465

	
466 466
    ///Processes the next arc.
467 467

	
468 468
    ///Processes the next arc.
469 469
    ///
470 470
    ///\return The processed arc.
471 471
    ///
472 472
    ///\pre The stack must not be empty.
473 473
    Arc processNextArc()
474 474
    {
475 475
      Node m;
476 476
      Arc e=_stack[_stack_head];
477 477
      if(!(*_reached)[m=G->target(e)]) {
478 478
        _pred->set(m,e);
479 479
        _reached->set(m,true);
480 480
        ++_stack_head;
481 481
        _stack[_stack_head] = OutArcIt(*G, m);
482 482
        _dist->set(m,_stack_head);
483 483
      }
484 484
      else {
485 485
        m=G->source(e);
486 486
        ++_stack[_stack_head];
487 487
      }
488 488
      while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
489 489
        _processed->set(m,true);
490 490
        --_stack_head;
491 491
        if(_stack_head>=0) {
492 492
          m=G->source(_stack[_stack_head]);
493 493
          ++_stack[_stack_head];
494 494
        }
495 495
      }
496 496
      return e;
497 497
    }
498 498

	
499 499
    ///Next arc to be processed.
500 500

	
501 501
    ///Next arc to be processed.
502 502
    ///
503 503
    ///\return The next arc to be processed or \c INVALID if the stack
504 504
    ///is empty.
505 505
    OutArcIt nextArc() const
506 506
    {
507 507
      return _stack_head>=0?_stack[_stack_head]:INVALID;
508 508
    }
509 509

	
510 510
    ///\brief Returns \c false if there are nodes
511 511
    ///to be processed.
512 512
    ///
513 513
    ///Returns \c false if there are nodes
514 514
    ///to be processed in the queue (stack).
515 515
    bool emptyQueue() const { return _stack_head<0; }
516 516

	
517 517
    ///Returns the number of the nodes to be processed.
518 518

	
519 519
    ///Returns the number of the nodes to be processed in the queue (stack).
520 520
    int queueSize() const { return _stack_head+1; }
521 521

	
522 522
    ///Executes the algorithm.
523 523

	
524 524
    ///Executes the algorithm.
525 525
    ///
526 526
    ///This method runs the %DFS algorithm from the root node
527 527
    ///in order to compute the DFS path to each node.
528 528
    ///
529 529
    /// The algorithm computes
530 530
    ///- the %DFS tree,
531 531
    ///- the distance of each node from the root in the %DFS tree.
532 532
    ///
533 533
    ///\pre init() must be called and a root node should be
534 534
    ///added with addSource() before using this function.
535 535
    ///
536 536
    ///\note <tt>d.start()</tt> is just a shortcut of the following code.
537 537
    ///\code
538 538
    ///  while ( !d.emptyQueue() ) {
539 539
    ///    d.processNextArc();
540 540
    ///  }
541 541
    ///\endcode
542 542
    void start()
543 543
    {
544 544
      while ( !emptyQueue() ) processNextArc();
545 545
    }
546 546

	
547 547
    ///Executes the algorithm until the given target node is reached.
548 548

	
549 549
    ///Executes the algorithm until the given target node is reached.
550 550
    ///
551 551
    ///This method runs the %DFS algorithm from the root node
552 552
    ///in order to compute the DFS path to \c t.
553 553
    ///
554 554
    ///The algorithm computes
555 555
    ///- the %DFS path to \c t,
556 556
    ///- the distance of \c t from the root in the %DFS tree.
557 557
    ///
558 558
    ///\pre init() must be called and a root node should be
559 559
    ///added with addSource() before using this function.
560 560
    void start(Node t)
561 561
    {
562 562
      while ( !emptyQueue() && G->target(_stack[_stack_head])!=t )
563 563
        processNextArc();
564 564
    }
565 565

	
566 566
    ///Executes the algorithm until a condition is met.
567 567

	
568 568
    ///Executes the algorithm until a condition is met.
569 569
    ///
570 570
    ///This method runs the %DFS algorithm from the root node
571 571
    ///until an arc \c a with <tt>am[a]</tt> true is found.
572 572
    ///
573 573
    ///\param am A \c bool (or convertible) arc map. The algorithm
574 574
    ///will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
575 575
    ///
576 576
    ///\return The reached arc \c a with <tt>am[a]</tt> true or
577 577
    ///\c INVALID if no such arc was found.
578 578
    ///
579 579
    ///\pre init() must be called and a root node should be
580 580
    ///added with addSource() before using this function.
581 581
    ///
582 582
    ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
583 583
    ///not a node map.
584 584
    template<class ArcBoolMap>
585 585
    Arc start(const ArcBoolMap &am)
586 586
    {
587 587
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
588 588
        processNextArc();
589 589
      return emptyQueue() ? INVALID : _stack[_stack_head];
590 590
    }
591 591

	
592 592
    ///Runs the algorithm from the given source node.
593 593

	
594 594
    ///This method runs the %DFS algorithm from node \c s
595 595
    ///in order to compute the DFS path to each node.
596 596
    ///
597 597
    ///The algorithm computes
598 598
    ///- the %DFS tree,
599 599
    ///- the distance of each node from the root in the %DFS tree.
600 600
    ///
601 601
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
602 602
    ///\code
603 603
    ///  d.init();
604 604
    ///  d.addSource(s);
605 605
    ///  d.start();
606 606
    ///\endcode
607 607
    void run(Node s) {
608 608
      init();
609 609
      addSource(s);
610 610
      start();
611 611
    }
612 612

	
613 613
    ///Finds the %DFS path between \c s and \c t.
614 614

	
615 615
    ///This method runs the %DFS algorithm from node \c s
616 616
    ///in order to compute the DFS path to node \c t
617 617
    ///(it stops searching when \c t is processed)
618 618
    ///
619 619
    ///\return \c true if \c t is reachable form \c s.
620 620
    ///
621 621
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
622 622
    ///just a shortcut of the following code.
623 623
    ///\code
624 624
    ///  d.init();
625 625
    ///  d.addSource(s);
626 626
    ///  d.start(t);
627 627
    ///\endcode
628 628
    bool run(Node s,Node t) {
629 629
      init();
630 630
      addSource(s);
631 631
      start(t);
632 632
      return reached(t);
633 633
    }
634 634

	
635 635
    ///Runs the algorithm to visit all nodes in the digraph.
636 636

	
637 637
    ///This method runs the %DFS algorithm in order to compute the
638 638
    ///%DFS path to each node.
639 639
    ///
640 640
    ///The algorithm computes
641 641
    ///- the %DFS tree,
642 642
    ///- the distance of each node from the root in the %DFS tree.
643 643
    ///
644 644
    ///\note <tt>d.run()</tt> is just a shortcut of the following code.
645 645
    ///\code
646 646
    ///  d.init();
647 647
    ///  for (NodeIt n(digraph); n != INVALID; ++n) {
648 648
    ///    if (!d.reached(n)) {
649 649
    ///      d.addSource(n);
650 650
    ///      d.start();
651 651
    ///    }
652 652
    ///  }
653 653
    ///\endcode
654 654
    void run() {
655 655
      init();
656 656
      for (NodeIt it(*G); it != INVALID; ++it) {
657 657
        if (!reached(it)) {
658 658
          addSource(it);
659 659
          start();
660 660
        }
661 661
      }
662 662
    }
663 663

	
664 664
    ///@}
665 665

	
666 666
    ///\name Query Functions
667 667
    ///The result of the %DFS algorithm can be obtained using these
668 668
    ///functions.\n
669 669
    ///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start()
670 670
    ///"start()" must be called before using them.
671 671

	
672 672
    ///@{
673 673

	
674 674
    ///The DFS path to a node.
675 675

	
676 676
    ///Returns the DFS path to a node.
677 677
    ///
678 678
    ///\warning \c t should be reachable from the root.
679 679
    ///
680 680
    ///\pre Either \ref run() or \ref start() must be called before
681 681
    ///using this function.
682 682
    Path path(Node t) const { return Path(*G, *_pred, t); }
683 683

	
684 684
    ///The distance of a node from the root.
685 685

	
686 686
    ///Returns the distance of a node from the root.
687 687
    ///
688 688
    ///\warning If node \c v is not reachable from the root, then
689 689
    ///the return value of this function is undefined.
690 690
    ///
691 691
    ///\pre Either \ref run() or \ref start() must be called before
692 692
    ///using this function.
693 693
    int dist(Node v) const { return (*_dist)[v]; }
694 694

	
695 695
    ///Returns the 'previous arc' of the %DFS tree for a node.
696 696

	
697 697
    ///This function returns the 'previous arc' of the %DFS tree for the
698 698
    ///node \c v, i.e. it returns the last arc of a %DFS path from the
699 699
    ///root to \c v. It is \c INVALID
700 700
    ///if \c v is not reachable from the root(s) or if \c v is a root.
701 701
    ///
702 702
    ///The %DFS tree used here is equal to the %DFS tree used in
703 703
    ///\ref predNode().
704 704
    ///
705 705
    ///\pre Either \ref run() or \ref start() must be called before using
706 706
    ///this function.
707 707
    Arc predArc(Node v) const { return (*_pred)[v];}
708 708

	
709 709
    ///Returns the 'previous node' of the %DFS tree.
710 710

	
711 711
    ///This function returns the 'previous node' of the %DFS
712 712
    ///tree for the node \c v, i.e. it returns the last but one node
713 713
    ///from a %DFS path from the root to \c v. It is \c INVALID
714 714
    ///if \c v is not reachable from the root(s) or if \c v is a root.
715 715
    ///
716 716
    ///The %DFS tree used here is equal to the %DFS tree used in
717 717
    ///\ref predArc().
718 718
    ///
719 719
    ///\pre Either \ref run() or \ref start() must be called before
720 720
    ///using this function.
721 721
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
722 722
                                  G->source((*_pred)[v]); }
723 723

	
724 724
    ///\brief Returns a const reference to the node map that stores the
725 725
    ///distances of the nodes.
726 726
    ///
727 727
    ///Returns a const reference to the node map that stores the
728 728
    ///distances of the nodes calculated by the algorithm.
729 729
    ///
730 730
    ///\pre Either \ref run() or \ref init()
731 731
    ///must be called before using this function.
732 732
    const DistMap &distMap() const { return *_dist;}
733 733

	
734 734
    ///\brief Returns a const reference to the node map that stores the
735 735
    ///predecessor arcs.
736 736
    ///
737 737
    ///Returns a const reference to the node map that stores the predecessor
738 738
    ///arcs, which form the DFS tree.
739 739
    ///
740 740
    ///\pre Either \ref run() or \ref init()
741 741
    ///must be called before using this function.
742 742
    const PredMap &predMap() const { return *_pred;}
743 743

	
744 744
    ///Checks if a node is reachable from the root(s).
745 745

	
746 746
    ///Returns \c true if \c v is reachable from the root(s).
747 747
    ///\pre Either \ref run() or \ref start()
748 748
    ///must be called before using this function.
749 749
    bool reached(Node v) const { return (*_reached)[v]; }
750 750

	
751 751
    ///@}
752 752
  };
753 753

	
754 754
  ///Default traits class of dfs() function.
755 755

	
756 756
  ///Default traits class of dfs() function.
757 757
  ///\tparam GR Digraph type.
758 758
  template<class GR>
759 759
  struct DfsWizardDefaultTraits
760 760
  {
761 761
    ///The type of the digraph the algorithm runs on.
762 762
    typedef GR Digraph;
763 763

	
764 764
    ///\brief The type of the map that stores the predecessor
765 765
    ///arcs of the %DFS paths.
766 766
    ///
767 767
    ///The type of the map that stores the predecessor
768 768
    ///arcs of the %DFS paths.
769 769
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
770 770
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
771
    ///Instantiates a \ref PredMap.
771
    ///Instantiates a PredMap.
772 772

	
773
    ///This function instantiates a \ref PredMap.
773
    ///This function instantiates a PredMap.
774 774
    ///\param g is the digraph, to which we would like to define the
775
    ///\ref PredMap.
775
    ///PredMap.
776 776
    static PredMap *createPredMap(const Digraph &g)
777 777
    {
778 778
      return new PredMap(g);
779 779
    }
780 780

	
781 781
    ///The type of the map that indicates which nodes are processed.
782 782

	
783 783
    ///The type of the map that indicates which nodes are processed.
784 784
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
785 785
    ///By default it is a NullMap.
786 786
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
787
    ///Instantiates a \ref ProcessedMap.
787
    ///Instantiates a ProcessedMap.
788 788

	
789
    ///This function instantiates a \ref ProcessedMap.
789
    ///This function instantiates a ProcessedMap.
790 790
    ///\param g is the digraph, to which
791
    ///we would like to define the \ref ProcessedMap.
791
    ///we would like to define the ProcessedMap.
792 792
#ifdef DOXYGEN
793 793
    static ProcessedMap *createProcessedMap(const Digraph &g)
794 794
#else
795 795
    static ProcessedMap *createProcessedMap(const Digraph &)
796 796
#endif
797 797
    {
798 798
      return new ProcessedMap();
799 799
    }
800 800

	
801 801
    ///The type of the map that indicates which nodes are reached.
802 802

	
803 803
    ///The type of the map that indicates which nodes are reached.
804 804
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
805 805
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
806
    ///Instantiates a \ref ReachedMap.
806
    ///Instantiates a ReachedMap.
807 807

	
808
    ///This function instantiates a \ref ReachedMap.
808
    ///This function instantiates a ReachedMap.
809 809
    ///\param g is the digraph, to which
810
    ///we would like to define the \ref ReachedMap.
810
    ///we would like to define the ReachedMap.
811 811
    static ReachedMap *createReachedMap(const Digraph &g)
812 812
    {
813 813
      return new ReachedMap(g);
814 814
    }
815 815

	
816 816
    ///The type of the map that stores the distances of the nodes.
817 817

	
818 818
    ///The type of the map that stores the distances of the nodes.
819 819
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
820 820
    typedef typename Digraph::template NodeMap<int> DistMap;
821
    ///Instantiates a \ref DistMap.
821
    ///Instantiates a DistMap.
822 822

	
823
    ///This function instantiates a \ref DistMap.
823
    ///This function instantiates a DistMap.
824 824
    ///\param g is the digraph, to which we would like to define
825
    ///the \ref DistMap
825
    ///the DistMap
826 826
    static DistMap *createDistMap(const Digraph &g)
827 827
    {
828 828
      return new DistMap(g);
829 829
    }
830 830

	
831 831
    ///The type of the DFS paths.
832 832

	
833 833
    ///The type of the DFS paths.
834 834
    ///It must meet the \ref concepts::Path "Path" concept.
835 835
    typedef lemon::Path<Digraph> Path;
836 836
  };
837 837

	
838 838
  /// Default traits class used by \ref DfsWizard
839 839

	
840 840
  /// To make it easier to use Dfs algorithm
841 841
  /// we have created a wizard class.
842 842
  /// This \ref DfsWizard class needs default traits,
843 843
  /// as well as the \ref Dfs class.
844 844
  /// The \ref DfsWizardBase is a class to be the default traits of the
845 845
  /// \ref DfsWizard class.
846 846
  template<class GR>
847 847
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
848 848
  {
849 849

	
850 850
    typedef DfsWizardDefaultTraits<GR> Base;
851 851
  protected:
852 852
    //The type of the nodes in the digraph.
853 853
    typedef typename Base::Digraph::Node Node;
854 854

	
855 855
    //Pointer to the digraph the algorithm runs on.
856 856
    void *_g;
857 857
    //Pointer to the map of reached nodes.
858 858
    void *_reached;
859 859
    //Pointer to the map of processed nodes.
860 860
    void *_processed;
861 861
    //Pointer to the map of predecessors arcs.
862 862
    void *_pred;
863 863
    //Pointer to the map of distances.
864 864
    void *_dist;
865 865
    //Pointer to the DFS path to the target node.
866 866
    void *_path;
867 867
    //Pointer to the distance of the target node.
868 868
    int *_di;
869 869

	
870 870
    public:
871 871
    /// Constructor.
872 872

	
873 873
    /// This constructor does not require parameters, therefore it initiates
874 874
    /// all of the attributes to \c 0.
875 875
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
876 876
                      _dist(0), _path(0), _di(0) {}
877 877

	
878 878
    /// Constructor.
879 879

	
880 880
    /// This constructor requires one parameter,
881 881
    /// others are initiated to \c 0.
882 882
    /// \param g The digraph the algorithm runs on.
883 883
    DfsWizardBase(const GR &g) :
884 884
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
885 885
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
886 886

	
887 887
  };
888 888

	
889 889
  /// Auxiliary class for the function-type interface of DFS algorithm.
890 890

	
891 891
  /// This auxiliary class is created to implement the
892 892
  /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
893 893
  /// It does not have own \ref run() method, it uses the functions
894 894
  /// and features of the plain \ref Dfs.
895 895
  ///
896 896
  /// This class should only be used through the \ref dfs() function,
897 897
  /// which makes it easier to use the algorithm.
898 898
  template<class TR>
899 899
  class DfsWizard : public TR
900 900
  {
901 901
    typedef TR Base;
902 902

	
903 903
    ///The type of the digraph the algorithm runs on.
904 904
    typedef typename TR::Digraph Digraph;
905 905

	
906 906
    typedef typename Digraph::Node Node;
907 907
    typedef typename Digraph::NodeIt NodeIt;
908 908
    typedef typename Digraph::Arc Arc;
909 909
    typedef typename Digraph::OutArcIt OutArcIt;
910 910

	
911 911
    ///\brief The type of the map that stores the predecessor
912 912
    ///arcs of the DFS paths.
913 913
    typedef typename TR::PredMap PredMap;
914 914
    ///\brief The type of the map that stores the distances of the nodes.
915 915
    typedef typename TR::DistMap DistMap;
916 916
    ///\brief The type of the map that indicates which nodes are reached.
917 917
    typedef typename TR::ReachedMap ReachedMap;
918 918
    ///\brief The type of the map that indicates which nodes are processed.
919 919
    typedef typename TR::ProcessedMap ProcessedMap;
920 920
    ///The type of the DFS paths
921 921
    typedef typename TR::Path Path;
922 922

	
923 923
  public:
924 924

	
925 925
    /// Constructor.
926 926
    DfsWizard() : TR() {}
927 927

	
928 928
    /// Constructor that requires parameters.
929 929

	
930 930
    /// Constructor that requires parameters.
931 931
    /// These parameters will be the default values for the traits class.
932 932
    /// \param g The digraph the algorithm runs on.
933 933
    DfsWizard(const Digraph &g) :
934 934
      TR(g) {}
935 935

	
936 936
    ///Copy constructor
937 937
    DfsWizard(const TR &b) : TR(b) {}
938 938

	
939 939
    ~DfsWizard() {}
940 940

	
941 941
    ///Runs DFS algorithm from the given source node.
942 942

	
943 943
    ///This method runs DFS algorithm from node \c s
944 944
    ///in order to compute the DFS path to each node.
945 945
    void run(Node s)
946 946
    {
947 947
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
948 948
      if (Base::_pred)
949 949
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
950 950
      if (Base::_dist)
951 951
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
952 952
      if (Base::_reached)
953 953
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
954 954
      if (Base::_processed)
955 955
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
956 956
      if (s!=INVALID)
957 957
        alg.run(s);
958 958
      else
959 959
        alg.run();
960 960
    }
961 961

	
962 962
    ///Finds the DFS path between \c s and \c t.
963 963

	
964 964
    ///This method runs DFS algorithm from node \c s
965 965
    ///in order to compute the DFS path to node \c t
966 966
    ///(it stops searching when \c t is processed).
967 967
    ///
968 968
    ///\return \c true if \c t is reachable form \c s.
969 969
    bool run(Node s, Node t)
970 970
    {
971 971
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
972 972
      if (Base::_pred)
973 973
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
974 974
      if (Base::_dist)
975 975
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
976 976
      if (Base::_reached)
977 977
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
978 978
      if (Base::_processed)
979 979
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
980 980
      alg.run(s,t);
981 981
      if (Base::_path)
982 982
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
983 983
      if (Base::_di)
984 984
        *Base::_di = alg.dist(t);
985 985
      return alg.reached(t);
986 986
      }
987 987

	
988 988
    ///Runs DFS algorithm to visit all nodes in the digraph.
989 989

	
990 990
    ///This method runs DFS algorithm in order to compute
991 991
    ///the DFS path to each node.
992 992
    void run()
993 993
    {
994 994
      run(INVALID);
995 995
    }
996 996

	
997 997
    template<class T>
998 998
    struct SetPredMapBase : public Base {
999 999
      typedef T PredMap;
1000 1000
      static PredMap *createPredMap(const Digraph &) { return 0; };
1001 1001
      SetPredMapBase(const TR &b) : TR(b) {}
1002 1002
    };
1003 1003
    ///\brief \ref named-func-param "Named parameter"
1004
    ///for setting \ref PredMap object.
1004
    ///for setting PredMap object.
1005 1005
    ///
1006 1006
    ///\ref named-func-param "Named parameter"
1007
    ///for setting \ref PredMap object.
1007
    ///for setting PredMap object.
1008 1008
    template<class T>
1009 1009
    DfsWizard<SetPredMapBase<T> > predMap(const T &t)
1010 1010
    {
1011 1011
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1012 1012
      return DfsWizard<SetPredMapBase<T> >(*this);
1013 1013
    }
1014 1014

	
1015 1015
    template<class T>
1016 1016
    struct SetReachedMapBase : public Base {
1017 1017
      typedef T ReachedMap;
1018 1018
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1019 1019
      SetReachedMapBase(const TR &b) : TR(b) {}
1020 1020
    };
1021 1021
    ///\brief \ref named-func-param "Named parameter"
1022
    ///for setting \ref ReachedMap object.
1022
    ///for setting ReachedMap object.
1023 1023
    ///
1024 1024
    /// \ref named-func-param "Named parameter"
1025
    ///for setting \ref ReachedMap object.
1025
    ///for setting ReachedMap object.
1026 1026
    template<class T>
1027 1027
    DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1028 1028
    {
1029 1029
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1030 1030
      return DfsWizard<SetReachedMapBase<T> >(*this);
1031 1031
    }
1032 1032

	
1033 1033
    template<class T>
1034 1034
    struct SetDistMapBase : public Base {
1035 1035
      typedef T DistMap;
1036 1036
      static DistMap *createDistMap(const Digraph &) { return 0; };
1037 1037
      SetDistMapBase(const TR &b) : TR(b) {}
1038 1038
    };
1039 1039
    ///\brief \ref named-func-param "Named parameter"
1040
    ///for setting \ref DistMap object.
1040
    ///for setting DistMap object.
1041 1041
    ///
1042 1042
    /// \ref named-func-param "Named parameter"
1043
    ///for setting \ref DistMap object.
1043
    ///for setting DistMap object.
1044 1044
    template<class T>
1045 1045
    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
1046 1046
    {
1047 1047
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1048 1048
      return DfsWizard<SetDistMapBase<T> >(*this);
1049 1049
    }
1050 1050

	
1051 1051
    template<class T>
1052 1052
    struct SetProcessedMapBase : public Base {
1053 1053
      typedef T ProcessedMap;
1054 1054
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1055 1055
      SetProcessedMapBase(const TR &b) : TR(b) {}
1056 1056
    };
1057 1057
    ///\brief \ref named-func-param "Named parameter"
1058
    ///for setting \ref ProcessedMap object.
1058
    ///for setting ProcessedMap object.
1059 1059
    ///
1060 1060
    /// \ref named-func-param "Named parameter"
1061
    ///for setting \ref ProcessedMap object.
1061
    ///for setting ProcessedMap object.
1062 1062
    template<class T>
1063 1063
    DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1064 1064
    {
1065 1065
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1066 1066
      return DfsWizard<SetProcessedMapBase<T> >(*this);
1067 1067
    }
1068 1068

	
1069 1069
    template<class T>
1070 1070
    struct SetPathBase : public Base {
1071 1071
      typedef T Path;
1072 1072
      SetPathBase(const TR &b) : TR(b) {}
1073 1073
    };
1074 1074
    ///\brief \ref named-func-param "Named parameter"
1075 1075
    ///for getting the DFS path to the target node.
1076 1076
    ///
1077 1077
    ///\ref named-func-param "Named parameter"
1078 1078
    ///for getting the DFS path to the target node.
1079 1079
    template<class T>
1080 1080
    DfsWizard<SetPathBase<T> > path(const T &t)
1081 1081
    {
1082 1082
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1083 1083
      return DfsWizard<SetPathBase<T> >(*this);
1084 1084
    }
1085 1085

	
1086 1086
    ///\brief \ref named-func-param "Named parameter"
1087 1087
    ///for getting the distance of the target node.
1088 1088
    ///
1089 1089
    ///\ref named-func-param "Named parameter"
1090 1090
    ///for getting the distance of the target node.
1091 1091
    DfsWizard dist(const int &d)
1092 1092
    {
1093 1093
      Base::_di=const_cast<int*>(&d);
1094 1094
      return *this;
1095 1095
    }
1096 1096

	
1097 1097
  };
1098 1098

	
1099 1099
  ///Function-type interface for DFS algorithm.
1100 1100

	
1101 1101
  ///\ingroup search
1102 1102
  ///Function-type interface for DFS algorithm.
1103 1103
  ///
1104 1104
  ///This function also has several \ref named-func-param "named parameters",
1105 1105
  ///they are declared as the members of class \ref DfsWizard.
1106 1106
  ///The following examples show how to use these parameters.
1107 1107
  ///\code
1108 1108
  ///  // Compute the DFS tree
1109 1109
  ///  dfs(g).predMap(preds).distMap(dists).run(s);
1110 1110
  ///
1111 1111
  ///  // Compute the DFS path from s to t
1112 1112
  ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
1113 1113
  ///\endcode
1114 1114

	
1115 1115
  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
1116 1116
  ///to the end of the parameter list.
1117 1117
  ///\sa DfsWizard
1118 1118
  ///\sa Dfs
1119 1119
  template<class GR>
1120 1120
  DfsWizard<DfsWizardBase<GR> >
1121 1121
  dfs(const GR &digraph)
1122 1122
  {
1123 1123
    return DfsWizard<DfsWizardBase<GR> >(digraph);
1124 1124
  }
1125 1125

	
1126 1126
#ifdef DOXYGEN
1127 1127
  /// \brief Visitor class for DFS.
1128 1128
  ///
1129 1129
  /// This class defines the interface of the DfsVisit events, and
1130 1130
  /// it could be the base of a real visitor class.
1131 1131
  template <typename _Digraph>
1132 1132
  struct DfsVisitor {
1133 1133
    typedef _Digraph Digraph;
1134 1134
    typedef typename Digraph::Arc Arc;
1135 1135
    typedef typename Digraph::Node Node;
1136 1136
    /// \brief Called for the source node of the DFS.
1137 1137
    ///
1138 1138
    /// This function is called for the source node of the DFS.
1139 1139
    void start(const Node& node) {}
1140 1140
    /// \brief Called when the source node is leaved.
1141 1141
    ///
1142 1142
    /// This function is called when the source node is leaved.
1143 1143
    void stop(const Node& node) {}
1144 1144
    /// \brief Called when a node is reached first time.
1145 1145
    ///
1146 1146
    /// This function is called when a node is reached first time.
1147 1147
    void reach(const Node& node) {}
1148 1148
    /// \brief Called when an arc reaches a new node.
1149 1149
    ///
1150 1150
    /// This function is called when the DFS finds an arc whose target node
1151 1151
    /// is not reached yet.
1152 1152
    void discover(const Arc& arc) {}
1153 1153
    /// \brief Called when an arc is examined but its target node is
1154 1154
    /// already discovered.
1155 1155
    ///
1156 1156
    /// This function is called when an arc is examined but its target node is
1157 1157
    /// already discovered.
1158 1158
    void examine(const Arc& arc) {}
1159 1159
    /// \brief Called when the DFS steps back from a node.
1160 1160
    ///
1161 1161
    /// This function is called when the DFS steps back from a node.
1162 1162
    void leave(const Node& node) {}
1163 1163
    /// \brief Called when the DFS steps back on an arc.
1164 1164
    ///
1165 1165
    /// This function is called when the DFS steps back on an arc.
1166 1166
    void backtrack(const Arc& arc) {}
1167 1167
  };
1168 1168
#else
1169 1169
  template <typename _Digraph>
1170 1170
  struct DfsVisitor {
1171 1171
    typedef _Digraph Digraph;
1172 1172
    typedef typename Digraph::Arc Arc;
1173 1173
    typedef typename Digraph::Node Node;
1174 1174
    void start(const Node&) {}
1175 1175
    void stop(const Node&) {}
1176 1176
    void reach(const Node&) {}
1177 1177
    void discover(const Arc&) {}
1178 1178
    void examine(const Arc&) {}
1179 1179
    void leave(const Node&) {}
1180 1180
    void backtrack(const Arc&) {}
1181 1181

	
1182 1182
    template <typename _Visitor>
1183 1183
    struct Constraints {
1184 1184
      void constraints() {
1185 1185
        Arc arc;
1186 1186
        Node node;
1187 1187
        visitor.start(node);
1188 1188
        visitor.stop(arc);
1189 1189
        visitor.reach(node);
1190 1190
        visitor.discover(arc);
1191 1191
        visitor.examine(arc);
1192 1192
        visitor.leave(node);
1193 1193
        visitor.backtrack(arc);
1194 1194
      }
1195 1195
      _Visitor& visitor;
1196 1196
    };
1197 1197
  };
1198 1198
#endif
1199 1199

	
1200 1200
  /// \brief Default traits class of DfsVisit class.
1201 1201
  ///
1202 1202
  /// Default traits class of DfsVisit class.
1203 1203
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1204 1204
  template<class _Digraph>
1205 1205
  struct DfsVisitDefaultTraits {
1206 1206

	
1207 1207
    /// \brief The type of the digraph the algorithm runs on.
1208 1208
    typedef _Digraph Digraph;
1209 1209

	
1210 1210
    /// \brief The type of the map that indicates which nodes are reached.
1211 1211
    ///
1212 1212
    /// The type of the map that indicates which nodes are reached.
1213 1213
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1214 1214
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1215 1215

	
1216
    /// \brief Instantiates a \ref ReachedMap.
1216
    /// \brief Instantiates a ReachedMap.
1217 1217
    ///
1218
    /// This function instantiates a \ref ReachedMap.
1218
    /// This function instantiates a ReachedMap.
1219 1219
    /// \param digraph is the digraph, to which
1220
    /// we would like to define the \ref ReachedMap.
1220
    /// we would like to define the ReachedMap.
1221 1221
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1222 1222
      return new ReachedMap(digraph);
1223 1223
    }
1224 1224

	
1225 1225
  };
1226 1226

	
1227 1227
  /// \ingroup search
1228 1228
  ///
1229 1229
  /// \brief %DFS algorithm class with visitor interface.
1230 1230
  ///
1231 1231
  /// This class provides an efficient implementation of the %DFS algorithm
1232 1232
  /// with visitor interface.
1233 1233
  ///
1234 1234
  /// The %DfsVisit class provides an alternative interface to the Dfs
1235 1235
  /// class. It works with callback mechanism, the DfsVisit object calls
1236 1236
  /// the member functions of the \c Visitor class on every DFS event.
1237 1237
  ///
1238 1238
  /// This interface of the DFS algorithm should be used in special cases
1239 1239
  /// when extra actions have to be performed in connection with certain
1240 1240
  /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
1241 1241
  /// instead.
1242 1242
  ///
1243 1243
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1244 1244
  /// The default value is
1245 1245
  /// \ref ListDigraph. The value of _Digraph is not used directly by
1246 1246
  /// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits.
1247 1247
  /// \tparam _Visitor The Visitor type that is used by the algorithm.
1248 1248
  /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which
1249 1249
  /// does not observe the DFS events. If you want to observe the DFS
1250 1250
  /// events, you should implement your own visitor class.
1251 1251
  /// \tparam _Traits Traits class to set various data types used by the
1252 1252
  /// algorithm. The default traits class is
1253 1253
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>".
1254 1254
  /// See \ref DfsVisitDefaultTraits for the documentation of
1255 1255
  /// a DFS visit traits class.
1256 1256
#ifdef DOXYGEN
1257 1257
  template <typename _Digraph, typename _Visitor, typename _Traits>
1258 1258
#else
1259 1259
  template <typename _Digraph = ListDigraph,
1260 1260
            typename _Visitor = DfsVisitor<_Digraph>,
1261 1261
            typename _Traits = DfsVisitDefaultTraits<_Digraph> >
1262 1262
#endif
1263 1263
  class DfsVisit {
1264 1264
  public:
1265 1265

	
1266 1266
    ///The traits class.
1267 1267
    typedef _Traits Traits;
1268 1268

	
1269 1269
    ///The type of the digraph the algorithm runs on.
1270 1270
    typedef typename Traits::Digraph Digraph;
1271 1271

	
1272 1272
    ///The visitor type used by the algorithm.
1273 1273
    typedef _Visitor Visitor;
1274 1274

	
1275 1275
    ///The type of the map that indicates which nodes are reached.
1276 1276
    typedef typename Traits::ReachedMap ReachedMap;
1277 1277

	
1278 1278
  private:
1279 1279

	
1280 1280
    typedef typename Digraph::Node Node;
1281 1281
    typedef typename Digraph::NodeIt NodeIt;
1282 1282
    typedef typename Digraph::Arc Arc;
1283 1283
    typedef typename Digraph::OutArcIt OutArcIt;
1284 1284

	
1285 1285
    //Pointer to the underlying digraph.
1286 1286
    const Digraph *_digraph;
1287 1287
    //Pointer to the visitor object.
1288 1288
    Visitor *_visitor;
1289 1289
    //Pointer to the map of reached status of the nodes.
1290 1290
    ReachedMap *_reached;
1291 1291
    //Indicates if _reached is locally allocated (true) or not.
1292 1292
    bool local_reached;
1293 1293

	
1294 1294
    std::vector<typename Digraph::Arc> _stack;
1295 1295
    int _stack_head;
1296 1296

	
1297 1297
    //Creates the maps if necessary.
1298 1298
    void create_maps() {
1299 1299
      if(!_reached) {
1300 1300
        local_reached = true;
1301 1301
        _reached = Traits::createReachedMap(*_digraph);
1302 1302
      }
1303 1303
    }
1304 1304

	
1305 1305
  protected:
1306 1306

	
1307 1307
    DfsVisit() {}
1308 1308

	
1309 1309
  public:
1310 1310

	
1311 1311
    typedef DfsVisit Create;
1312 1312

	
1313 1313
    /// \name Named template parameters
1314 1314

	
1315 1315
    ///@{
1316 1316
    template <class T>
1317 1317
    struct SetReachedMapTraits : public Traits {
1318 1318
      typedef T ReachedMap;
1319 1319
      static ReachedMap *createReachedMap(const Digraph &digraph) {
1320 1320
        LEMON_ASSERT(false, "ReachedMap is not initialized");
1321 1321
        return 0; // ignore warnings
1322 1322
      }
1323 1323
    };
1324 1324
    /// \brief \ref named-templ-param "Named parameter" for setting
1325 1325
    /// ReachedMap type.
1326 1326
    ///
1327 1327
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1328 1328
    template <class T>
1329 1329
    struct SetReachedMap : public DfsVisit< Digraph, Visitor,
1330 1330
                                            SetReachedMapTraits<T> > {
1331 1331
      typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1332 1332
    };
1333 1333
    ///@}
1334 1334

	
1335 1335
  public:
1336 1336

	
1337 1337
    /// \brief Constructor.
1338 1338
    ///
1339 1339
    /// Constructor.
1340 1340
    ///
1341 1341
    /// \param digraph The digraph the algorithm runs on.
1342 1342
    /// \param visitor The visitor object of the algorithm.
1343 1343
    DfsVisit(const Digraph& digraph, Visitor& visitor)
1344 1344
      : _digraph(&digraph), _visitor(&visitor),
1345 1345
        _reached(0), local_reached(false) {}
1346 1346

	
1347 1347
    /// \brief Destructor.
1348 1348
    ~DfsVisit() {
1349 1349
      if(local_reached) delete _reached;
1350 1350
    }
1351 1351

	
1352 1352
    /// \brief Sets the map that indicates which nodes are reached.
1353 1353
    ///
1354 1354
    /// Sets the map that indicates which nodes are reached.
1355 1355
    /// If you don't use this function before calling \ref run(),
1356 1356
    /// it will allocate one. The destructor deallocates this
1357 1357
    /// automatically allocated map, of course.
1358 1358
    /// \return <tt> (*this) </tt>
1359 1359
    DfsVisit &reachedMap(ReachedMap &m) {
1360 1360
      if(local_reached) {
1361 1361
        delete _reached;
1362 1362
        local_reached=false;
1363 1363
      }
1364 1364
      _reached = &m;
1365 1365
      return *this;
1366 1366
    }
1367 1367

	
1368 1368
  public:
1369 1369

	
1370 1370
    /// \name Execution control
1371 1371
    /// The simplest way to execute the algorithm is to use
1372 1372
    /// one of the member functions called \ref lemon::DfsVisit::run()
1373 1373
    /// "run()".
1374 1374
    /// \n
1375 1375
    /// If you need more control on the execution, first you must call
1376 1376
    /// \ref lemon::DfsVisit::init() "init()", then you can add several
1377 1377
    /// source nodes with \ref lemon::DfsVisit::addSource() "addSource()".
1378 1378
    /// Finally \ref lemon::DfsVisit::start() "start()" will perform the
1379 1379
    /// actual path computation.
1380 1380

	
1381 1381
    /// @{
1382 1382

	
1383 1383
    /// \brief Initializes the internal data structures.
1384 1384
    ///
1385 1385
    /// Initializes the internal data structures.
1386 1386
    void init() {
1387 1387
      create_maps();
1388 1388
      _stack.resize(countNodes(*_digraph));
1389 1389
      _stack_head = -1;
1390 1390
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1391 1391
        _reached->set(u, false);
1392 1392
      }
1393 1393
    }
1394 1394

	
1395 1395
    ///Adds a new source node.
1396 1396

	
1397 1397
    ///Adds a new source node to the set of nodes to be processed.
1398 1398
    ///
1399 1399
    ///\pre The stack must be empty. (Otherwise the algorithm gives
1400 1400
    ///false results.)
1401 1401
    ///
1402 1402
    ///\warning Distances will be wrong (or at least strange) in case of
1403 1403
    ///multiple sources.
1404 1404
    void addSource(Node s)
1405 1405
    {
1406 1406
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
1407 1407
      if(!(*_reached)[s]) {
1408 1408
          _reached->set(s,true);
1409 1409
          _visitor->start(s);
1410 1410
          _visitor->reach(s);
1411 1411
          Arc e;
1412 1412
          _digraph->firstOut(e, s);
1413 1413
          if (e != INVALID) {
1414 1414
            _stack[++_stack_head] = e;
1415 1415
          } else {
1416 1416
            _visitor->leave(s);
1417 1417
          }
1418 1418
        }
1419 1419
    }
1420 1420

	
1421 1421
    /// \brief Processes the next arc.
1422 1422
    ///
1423 1423
    /// Processes the next arc.
1424 1424
    ///
1425 1425
    /// \return The processed arc.
1426 1426
    ///
1427 1427
    /// \pre The stack must not be empty.
1428 1428
    Arc processNextArc() {
1429 1429
      Arc e = _stack[_stack_head];
1430 1430
      Node m = _digraph->target(e);
1431 1431
      if(!(*_reached)[m]) {
1432 1432
        _visitor->discover(e);
1433 1433
        _visitor->reach(m);
1434 1434
        _reached->set(m, true);
1435 1435
        _digraph->firstOut(_stack[++_stack_head], m);
1436 1436
      } else {
1437 1437
        _visitor->examine(e);
1438 1438
        m = _digraph->source(e);
1439 1439
        _digraph->nextOut(_stack[_stack_head]);
1440 1440
      }
1441 1441
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
1442 1442
        _visitor->leave(m);
1443 1443
        --_stack_head;
1444 1444
        if (_stack_head >= 0) {
1445 1445
          _visitor->backtrack(_stack[_stack_head]);
1446 1446
          m = _digraph->source(_stack[_stack_head]);
1447 1447
          _digraph->nextOut(_stack[_stack_head]);
1448 1448
        } else {
1449 1449
          _visitor->stop(m);
1450 1450
        }
1451 1451
      }
1452 1452
      return e;
1453 1453
    }
1454 1454

	
1455 1455
    /// \brief Next arc to be processed.
1456 1456
    ///
1457 1457
    /// Next arc to be processed.
1458 1458
    ///
1459 1459
    /// \return The next arc to be processed or INVALID if the stack is
1460 1460
    /// empty.
1461 1461
    Arc nextArc() const {
1462 1462
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
1463 1463
    }
1464 1464

	
1465 1465
    /// \brief Returns \c false if there are nodes
1466 1466
    /// to be processed.
1467 1467
    ///
1468 1468
    /// Returns \c false if there are nodes
1469 1469
    /// to be processed in the queue (stack).
1470 1470
    bool emptyQueue() const { return _stack_head < 0; }
1471 1471

	
1472 1472
    /// \brief Returns the number of the nodes to be processed.
1473 1473
    ///
1474 1474
    /// Returns the number of the nodes to be processed in the queue (stack).
1475 1475
    int queueSize() const { return _stack_head + 1; }
1476 1476

	
1477 1477
    /// \brief Executes the algorithm.
1478 1478
    ///
1479 1479
    /// Executes the algorithm.
1480 1480
    ///
1481 1481
    /// This method runs the %DFS algorithm from the root node
1482 1482
    /// in order to compute the %DFS path to each node.
1483 1483
    ///
1484 1484
    /// The algorithm computes
1485 1485
    /// - the %DFS tree,
1486 1486
    /// - the distance of each node from the root in the %DFS tree.
1487 1487
    ///
1488 1488
    /// \pre init() must be called and a root node should be
1489 1489
    /// added with addSource() before using this function.
1490 1490
    ///
1491 1491
    /// \note <tt>d.start()</tt> is just a shortcut of the following code.
1492 1492
    /// \code
1493 1493
    ///   while ( !d.emptyQueue() ) {
1494 1494
    ///     d.processNextArc();
1495 1495
    ///   }
1496 1496
    /// \endcode
1497 1497
    void start() {
1498 1498
      while ( !emptyQueue() ) processNextArc();
1499 1499
    }
1500 1500

	
1501 1501
    /// \brief Executes the algorithm until the given target node is reached.
1502 1502
    ///
1503 1503
    /// Executes the algorithm until the given target node is reached.
1504 1504
    ///
1505 1505
    /// This method runs the %DFS algorithm from the root node
1506 1506
    /// in order to compute the DFS path to \c t.
1507 1507
    ///
1508 1508
    /// The algorithm computes
1509 1509
    /// - the %DFS path to \c t,
1510 1510
    /// - the distance of \c t from the root in the %DFS tree.
1511 1511
    ///
1512 1512
    /// \pre init() must be called and a root node should be added
1513 1513
    /// with addSource() before using this function.
1514 1514
    void start(Node t) {
1515 1515
      while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t )
1516 1516
        processNextArc();
1517 1517
    }
1518 1518

	
1519 1519
    /// \brief Executes the algorithm until a condition is met.
1520 1520
    ///
1521 1521
    /// Executes the algorithm until a condition is met.
1522 1522
    ///
1523 1523
    /// This method runs the %DFS algorithm from the root node
1524 1524
    /// until an arc \c a with <tt>am[a]</tt> true is found.
1525 1525
    ///
1526 1526
    /// \param am A \c bool (or convertible) arc map. The algorithm
1527 1527
    /// will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
1528 1528
    ///
1529 1529
    /// \return The reached arc \c a with <tt>am[a]</tt> true or
1530 1530
    /// \c INVALID if no such arc was found.
1531 1531
    ///
1532 1532
    /// \pre init() must be called and a root node should be added
1533 1533
    /// with addSource() before using this function.
1534 1534
    ///
1535 1535
    /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
1536 1536
    /// not a node map.
1537 1537
    template <typename AM>
1538 1538
    Arc start(const AM &am) {
1539 1539
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
1540 1540
        processNextArc();
1541 1541
      return emptyQueue() ? INVALID : _stack[_stack_head];
1542 1542
    }
1543 1543

	
1544 1544
    /// \brief Runs the algorithm from the given source node.
1545 1545
    ///
1546 1546
    /// This method runs the %DFS algorithm from node \c s.
1547 1547
    /// in order to compute the DFS path to each node.
1548 1548
    ///
1549 1549
    /// The algorithm computes
1550 1550
    /// - the %DFS tree,
1551 1551
    /// - the distance of each node from the root in the %DFS tree.
1552 1552
    ///
1553 1553
    /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
1554 1554
    ///\code
1555 1555
    ///   d.init();
1556 1556
    ///   d.addSource(s);
1557 1557
    ///   d.start();
1558 1558
    ///\endcode
1559 1559
    void run(Node s) {
1560 1560
      init();
1561 1561
      addSource(s);
1562 1562
      start();
1563 1563
    }
1564 1564

	
1565 1565
    /// \brief Finds the %DFS path between \c s and \c t.
1566 1566

	
1567 1567
    /// This method runs the %DFS algorithm from node \c s
1568 1568
    /// in order to compute the DFS path to node \c t
1569 1569
    /// (it stops searching when \c t is processed).
1570 1570
    ///
1571 1571
    /// \return \c true if \c t is reachable form \c s.
1572 1572
    ///
1573 1573
    /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
1574 1574
    /// just a shortcut of the following code.
1575 1575
    ///\code
1576 1576
    ///   d.init();
1577 1577
    ///   d.addSource(s);
1578 1578
    ///   d.start(t);
1579 1579
    ///\endcode
1580 1580
    bool run(Node s,Node t) {
1581 1581
      init();
1582 1582
      addSource(s);
1583 1583
      start(t);
1584 1584
      return reached(t);
1585 1585
    }
1586 1586

	
1587 1587
    /// \brief Runs the algorithm to visit all nodes in the digraph.
1588 1588

	
1589 1589
    /// This method runs the %DFS algorithm in order to
1590 1590
    /// compute the %DFS path to each node.
1591 1591
    ///
1592 1592
    /// The algorithm computes
1593 1593
    /// - the %DFS tree,
1594 1594
    /// - the distance of each node from the root in the %DFS tree.
1595 1595
    ///
1596 1596
    /// \note <tt>d.run()</tt> is just a shortcut of the following code.
1597 1597
    ///\code
1598 1598
    ///   d.init();
1599 1599
    ///   for (NodeIt n(digraph); n != INVALID; ++n) {
1600 1600
    ///     if (!d.reached(n)) {
1601 1601
    ///       d.addSource(n);
1602 1602
    ///       d.start();
1603 1603
    ///     }
1604 1604
    ///   }
1605 1605
    ///\endcode
1606 1606
    void run() {
1607 1607
      init();
1608 1608
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
1609 1609
        if (!reached(it)) {
1610 1610
          addSource(it);
1611 1611
          start();
1612 1612
        }
1613 1613
      }
1614 1614
    }
1615 1615

	
1616 1616
    ///@}
1617 1617

	
1618 1618
    /// \name Query Functions
1619 1619
    /// The result of the %DFS algorithm can be obtained using these
1620 1620
    /// functions.\n
1621 1621
    /// Either \ref lemon::DfsVisit::run() "run()" or
1622 1622
    /// \ref lemon::DfsVisit::start() "start()" must be called before
1623 1623
    /// using them.
1624 1624
    ///@{
1625 1625

	
1626 1626
    /// \brief Checks if a node is reachable from the root(s).
1627 1627
    ///
1628 1628
    /// Returns \c true if \c v is reachable from the root(s).
1629 1629
    /// \pre Either \ref run() or \ref start()
1630 1630
    /// must be called before using this function.
1631 1631
    bool reached(Node v) { return (*_reached)[v]; }
1632 1632

	
1633 1633
    ///@}
1634 1634

	
1635 1635
  };
1636 1636

	
1637 1637
} //END OF NAMESPACE LEMON
1638 1638

	
1639 1639
#endif
Ignore white space 3072 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_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 41
  template <typename Value>
42 42
  struct DijkstraDefaultOperationTraits {
43 43
    /// \brief Gives back the zero value of the type.
44 44
    static Value zero() {
45 45
      return static_cast<Value>(0);
46 46
    }
47 47
    /// \brief Gives back the sum of the given two elements.
48 48
    static Value plus(const Value& left, const Value& right) {
49 49
      return left + right;
50 50
    }
51 51
    /// \brief Gives back true only if the first value is less than the second.
52 52
    static bool less(const Value& left, const Value& right) {
53 53
      return left < right;
54 54
    }
55 55
  };
56 56

	
57 57
  /// \brief Widest path operation traits for the Dijkstra algorithm class.
58 58
  ///
59 59
  /// This operation traits class defines all computational operations and
60 60
  /// constants which are used in the Dijkstra algorithm for widest path
61 61
  /// computation.
62 62
  ///
63 63
  /// \see DijkstraDefaultOperationTraits
64 64
  template <typename Value>
65 65
  struct DijkstraWidestPathOperationTraits {
66 66
    /// \brief Gives back the maximum value of the type.
67 67
    static Value zero() {
68 68
      return std::numeric_limits<Value>::max();
69 69
    }
70 70
    /// \brief Gives back the minimum of the given two elements.
71 71
    static Value plus(const Value& left, const Value& right) {
72 72
      return std::min(left, right);
73 73
    }
74 74
    /// \brief Gives back true only if the first value is less than the second.
75 75
    static bool less(const Value& left, const Value& right) {
76 76
      return left < right;
77 77
    }
78 78
  };
79 79

	
80 80
  ///Default traits class of Dijkstra class.
81 81

	
82 82
  ///Default traits class of Dijkstra class.
83 83
  ///\tparam GR The type of the digraph.
84 84
  ///\tparam LM The type of the length map.
85 85
  template<class GR, class LM>
86 86
  struct DijkstraDefaultTraits
87 87
  {
88 88
    ///The type of the digraph the algorithm runs on.
89 89
    typedef GR Digraph;
90 90

	
91 91
    ///The type of the map that stores the arc lengths.
92 92

	
93 93
    ///The type of the map that stores the arc lengths.
94 94
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
95 95
    typedef LM LengthMap;
96 96
    ///The type of the length of the arcs.
97 97
    typedef typename LM::Value Value;
98 98

	
99 99
    /// Operation traits for Dijkstra algorithm.
100 100

	
101 101
    /// This class defines the operations that are used in the algorithm.
102 102
    /// \see DijkstraDefaultOperationTraits
103 103
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
104 104

	
105 105
    /// The cross reference type used by the heap.
106 106

	
107 107
    /// The cross reference type used by the heap.
108 108
    /// Usually it is \c Digraph::NodeMap<int>.
109 109
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
110 110
    ///Instantiates a \ref HeapCrossRef.
111 111

	
112 112
    ///This function instantiates a \ref HeapCrossRef.
113 113
    /// \param g is the digraph, to which we would like to define the
114 114
    /// \ref HeapCrossRef.
115 115
    static HeapCrossRef *createHeapCrossRef(const Digraph &g)
116 116
    {
117 117
      return new HeapCrossRef(g);
118 118
    }
119 119

	
120 120
    ///The heap type used by the Dijkstra algorithm.
121 121

	
122 122
    ///The heap type used by the Dijkstra algorithm.
123 123
    ///
124 124
    ///\sa BinHeap
125 125
    ///\sa Dijkstra
126 126
    typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap;
127 127
    ///Instantiates a \ref Heap.
128 128

	
129 129
    ///This function instantiates a \ref Heap.
130 130
    static Heap *createHeap(HeapCrossRef& r)
131 131
    {
132 132
      return new Heap(r);
133 133
    }
134 134

	
135 135
    ///\brief The type of the map that stores the predecessor
136 136
    ///arcs of the shortest paths.
137 137
    ///
138 138
    ///The type of the map that stores the predecessor
139 139
    ///arcs of the shortest paths.
140 140
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
141 141
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
142
    ///Instantiates a \ref PredMap.
142
    ///Instantiates a PredMap.
143 143

	
144
    ///This function instantiates a \ref PredMap.
144
    ///This function instantiates a PredMap.
145 145
    ///\param g is the digraph, to which we would like to define the
146
    ///\ref PredMap.
146
    ///PredMap.
147 147
    static PredMap *createPredMap(const Digraph &g)
148 148
    {
149 149
      return new PredMap(g);
150 150
    }
151 151

	
152 152
    ///The type of the map that indicates which nodes are processed.
153 153

	
154 154
    ///The type of the map that indicates which nodes are processed.
155 155
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
156 156
    ///By default it is a NullMap.
157 157
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
158
    ///Instantiates a \ref ProcessedMap.
158
    ///Instantiates a ProcessedMap.
159 159

	
160
    ///This function instantiates a \ref ProcessedMap.
160
    ///This function instantiates a ProcessedMap.
161 161
    ///\param g is the digraph, to which
162
    ///we would like to define the \ref ProcessedMap
162
    ///we would like to define the ProcessedMap
163 163
#ifdef DOXYGEN
164 164
    static ProcessedMap *createProcessedMap(const Digraph &g)
165 165
#else
166 166
    static ProcessedMap *createProcessedMap(const Digraph &)
167 167
#endif
168 168
    {
169 169
      return new ProcessedMap();
170 170
    }
171 171

	
172 172
    ///The type of the map that stores the distances of the nodes.
173 173

	
174 174
    ///The type of the map that stores the distances of the nodes.
175 175
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
176 176
    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
177
    ///Instantiates a \ref DistMap.
177
    ///Instantiates a DistMap.
178 178

	
179
    ///This function instantiates a \ref DistMap.
179
    ///This function instantiates a DistMap.
180 180
    ///\param g is the digraph, to which we would like to define
181
    ///the \ref DistMap
181
    ///the DistMap
182 182
    static DistMap *createDistMap(const Digraph &g)
183 183
    {
184 184
      return new DistMap(g);
185 185
    }
186 186
  };
187 187

	
188 188
  ///%Dijkstra algorithm class.
189 189

	
190 190
  /// \ingroup shortest_path
191 191
  ///This class provides an efficient implementation of the %Dijkstra algorithm.
192 192
  ///
193 193
  ///The arc lengths are passed to the algorithm using a
194 194
  ///\ref concepts::ReadMap "ReadMap",
195 195
  ///so it is easy to change it to any kind of length.
196 196
  ///The type of the length is determined by the
197 197
  ///\ref concepts::ReadMap::Value "Value" of the length map.
198 198
  ///It is also possible to change the underlying priority heap.
199 199
  ///
200 200
  ///There is also a \ref dijkstra() "function-type interface" for the
201 201
  ///%Dijkstra algorithm, which is convenient in the simplier cases and
202 202
  ///it can be used easier.
203 203
  ///
204 204
  ///\tparam GR The type of the digraph the algorithm runs on.
205 205
  ///The default value is \ref ListDigraph.
206 206
  ///The value of GR is not used directly by \ref Dijkstra, it is only
207 207
  ///passed to \ref DijkstraDefaultTraits.
208 208
  ///\tparam LM A readable arc map that determines the lengths of the
209 209
  ///arcs. It is read once for each arc, so the map may involve in
210 210
  ///relatively time consuming process to compute the arc lengths if
211 211
  ///it is necessary. The default map type is \ref
212 212
  ///concepts::Digraph::ArcMap "Digraph::ArcMap<int>".
213 213
  ///The value of LM is not used directly by \ref Dijkstra, it is only
214 214
  ///passed to \ref DijkstraDefaultTraits.
215 215
  ///\tparam TR Traits class to set various data types used by the algorithm.
216 216
  ///The default traits class is \ref DijkstraDefaultTraits
217 217
  ///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits
218 218
  ///for the documentation of a Dijkstra traits class.
219 219
#ifdef DOXYGEN
220 220
  template <typename GR, typename LM, typename TR>
221 221
#else
222 222
  template <typename GR=ListDigraph,
223 223
            typename LM=typename GR::template ArcMap<int>,
224 224
            typename TR=DijkstraDefaultTraits<GR,LM> >
225 225
#endif
226 226
  class Dijkstra {
227 227
  public:
228 228

	
229 229
    ///The type of the digraph the algorithm runs on.
230 230
    typedef typename TR::Digraph Digraph;
231 231

	
232 232
    ///The type of the length of the arcs.
233 233
    typedef typename TR::LengthMap::Value Value;
234 234
    ///The type of the map that stores the arc lengths.
235 235
    typedef typename TR::LengthMap LengthMap;
236 236
    ///\brief The type of the map that stores the predecessor arcs of the
237 237
    ///shortest paths.
238 238
    typedef typename TR::PredMap PredMap;
239 239
    ///The type of the map that stores the distances of the nodes.
240 240
    typedef typename TR::DistMap DistMap;
241 241
    ///The type of the map that indicates which nodes are processed.
242 242
    typedef typename TR::ProcessedMap ProcessedMap;
243 243
    ///The type of the paths.
244 244
    typedef PredMapPath<Digraph, PredMap> Path;
245 245
    ///The cross reference type used for the current heap.
246 246
    typedef typename TR::HeapCrossRef HeapCrossRef;
247 247
    ///The heap type used by the algorithm.
248 248
    typedef typename TR::Heap Heap;
249 249
    ///The operation traits class.
250 250
    typedef typename TR::OperationTraits OperationTraits;
251 251

	
252 252
    ///The traits class.
253 253
    typedef TR Traits;
254 254

	
255 255
  private:
256 256

	
257 257
    typedef typename Digraph::Node Node;
258 258
    typedef typename Digraph::NodeIt NodeIt;
259 259
    typedef typename Digraph::Arc Arc;
260 260
    typedef typename Digraph::OutArcIt OutArcIt;
261 261

	
262 262
    //Pointer to the underlying digraph.
263 263
    const Digraph *G;
264 264
    //Pointer to the length map.
265 265
    const LengthMap *length;
266 266
    //Pointer to the map of predecessors arcs.
267 267
    PredMap *_pred;
268 268
    //Indicates if _pred is locally allocated (true) or not.
269 269
    bool local_pred;
270 270
    //Pointer to the map of distances.
271 271
    DistMap *_dist;
272 272
    //Indicates if _dist is locally allocated (true) or not.
273 273
    bool local_dist;
274 274
    //Pointer to the map of processed status of the nodes.
275 275
    ProcessedMap *_processed;
276 276
    //Indicates if _processed is locally allocated (true) or not.
277 277
    bool local_processed;
278 278
    //Pointer to the heap cross references.
279 279
    HeapCrossRef *_heap_cross_ref;
280 280
    //Indicates if _heap_cross_ref is locally allocated (true) or not.
281 281
    bool local_heap_cross_ref;
282 282
    //Pointer to the heap.
283 283
    Heap *_heap;
284 284
    //Indicates if _heap is locally allocated (true) or not.
285 285
    bool local_heap;
286 286

	
287 287
    //Creates the maps if necessary.
288 288
    void create_maps()
289 289
    {
290 290
      if(!_pred) {
291 291
        local_pred = true;
292 292
        _pred = Traits::createPredMap(*G);
293 293
      }
294 294
      if(!_dist) {
295 295
        local_dist = true;
296 296
        _dist = Traits::createDistMap(*G);
297 297
      }
298 298
      if(!_processed) {
299 299
        local_processed = true;
300 300
        _processed = Traits::createProcessedMap(*G);
301 301
      }
302 302
      if (!_heap_cross_ref) {
303 303
        local_heap_cross_ref = true;
304 304
        _heap_cross_ref = Traits::createHeapCrossRef(*G);
305 305
      }
306 306
      if (!_heap) {
307 307
        local_heap = true;
308 308
        _heap = Traits::createHeap(*_heap_cross_ref);
309 309
      }
310 310
    }
311 311

	
312 312
  public:
313 313

	
314 314
    typedef Dijkstra Create;
315 315

	
316 316
    ///\name Named template parameters
317 317

	
318 318
    ///@{
319 319

	
320 320
    template <class T>
321 321
    struct SetPredMapTraits : public Traits {
322 322
      typedef T PredMap;
323 323
      static PredMap *createPredMap(const Digraph &)
324 324
      {
325 325
        LEMON_ASSERT(false, "PredMap is not initialized");
326 326
        return 0; // ignore warnings
327 327
      }
328 328
    };
329 329
    ///\brief \ref named-templ-param "Named parameter" for setting
330
    ///\ref PredMap type.
330
    ///PredMap type.
331 331
    ///
332 332
    ///\ref named-templ-param "Named parameter" for setting
333
    ///\ref PredMap type.
333
    ///PredMap type.
334 334
    template <class T>
335 335
    struct SetPredMap
336 336
      : public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > {
337 337
      typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create;
338 338
    };
339 339

	
340 340
    template <class T>
341 341
    struct SetDistMapTraits : public Traits {
342 342
      typedef T DistMap;
343 343
      static DistMap *createDistMap(const Digraph &)
344 344
      {
345 345
        LEMON_ASSERT(false, "DistMap is not initialized");
346 346
        return 0; // ignore warnings
347 347
      }
348 348
    };
349 349
    ///\brief \ref named-templ-param "Named parameter" for setting
350
    ///\ref DistMap type.
350
    ///DistMap type.
351 351
    ///
352 352
    ///\ref named-templ-param "Named parameter" for setting
353
    ///\ref DistMap type.
353
    ///DistMap type.
354 354
    template <class T>
355 355
    struct SetDistMap
356 356
      : public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > {
357 357
      typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create;
358 358
    };
359 359

	
360 360
    template <class T>
361 361
    struct SetProcessedMapTraits : public Traits {
362 362
      typedef T ProcessedMap;
363 363
      static ProcessedMap *createProcessedMap(const Digraph &)
364 364
      {
365 365
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
366 366
        return 0; // ignore warnings
367 367
      }
368 368
    };
369 369
    ///\brief \ref named-templ-param "Named parameter" for setting
370
    ///\ref ProcessedMap type.
370
    ///ProcessedMap type.
371 371
    ///
372 372
    ///\ref named-templ-param "Named parameter" for setting
373
    ///\ref ProcessedMap type.
373
    ///ProcessedMap type.
374 374
    template <class T>
375 375
    struct SetProcessedMap
376 376
      : public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > {
377 377
      typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create;
378 378
    };
379 379

	
380 380
    struct SetStandardProcessedMapTraits : public Traits {
381 381
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
382 382
      static ProcessedMap *createProcessedMap(const Digraph &g)
383 383
      {
384 384
        return new ProcessedMap(g);
385 385
      }
386 386
    };
387 387
    ///\brief \ref named-templ-param "Named parameter" for setting
388
    ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
388
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
389 389
    ///
390 390
    ///\ref named-templ-param "Named parameter" for setting
391
    ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
391
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
392 392
    ///If you don't set it explicitly, it will be automatically allocated.
393 393
    struct SetStandardProcessedMap
394 394
      : public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > {
395 395
      typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits >
396 396
      Create;
397 397
    };
398 398

	
399 399
    template <class H, class CR>
400 400
    struct SetHeapTraits : public Traits {
401 401
      typedef CR HeapCrossRef;
402 402
      typedef H Heap;
403 403
      static HeapCrossRef *createHeapCrossRef(const Digraph &) {
404 404
        LEMON_ASSERT(false, "HeapCrossRef is not initialized");
405 405
        return 0; // ignore warnings
406 406
      }
407 407
      static Heap *createHeap(HeapCrossRef &)
408 408
      {
409 409
        LEMON_ASSERT(false, "Heap is not initialized");
410 410
        return 0; // ignore warnings
411 411
      }
412 412
    };
413 413
    ///\brief \ref named-templ-param "Named parameter" for setting
414 414
    ///heap and cross reference type
415 415
    ///
416 416
    ///\ref named-templ-param "Named parameter" for setting heap and cross
417 417
    ///reference type.
418 418
    template <class H, class CR = typename Digraph::template NodeMap<int> >
419 419
    struct SetHeap
420 420
      : public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > {
421 421
      typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create;
422 422
    };
423 423

	
424 424
    template <class H, class CR>
425 425
    struct SetStandardHeapTraits : public Traits {
426 426
      typedef CR HeapCrossRef;
427 427
      typedef H Heap;
428 428
      static HeapCrossRef *createHeapCrossRef(const Digraph &G) {
429 429
        return new HeapCrossRef(G);
430 430
      }
431 431
      static Heap *createHeap(HeapCrossRef &R)
432 432
      {
433 433
        return new Heap(R);
434 434
      }
435 435
    };
436 436
    ///\brief \ref named-templ-param "Named parameter" for setting
437 437
    ///heap and cross reference type with automatic allocation
438 438
    ///
439 439
    ///\ref named-templ-param "Named parameter" for setting heap and cross
440 440
    ///reference type. It can allocate the heap and the cross reference
441 441
    ///object if the cross reference's constructor waits for the digraph as
442 442
    ///parameter and the heap's constructor waits for the cross reference.
443 443
    template <class H, class CR = typename Digraph::template NodeMap<int> >
444 444
    struct SetStandardHeap
445 445
      : public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > {
446 446
      typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> >
447 447
      Create;
448 448
    };
449 449

	
450 450
    template <class T>
451 451
    struct SetOperationTraitsTraits : public Traits {
452 452
      typedef T OperationTraits;
453 453
    };
454 454

	
455 455
    /// \brief \ref named-templ-param "Named parameter" for setting
456 456
    ///\ref OperationTraits type
457 457
    ///
458 458
    ///\ref named-templ-param "Named parameter" for setting
459 459
    ///\ref OperationTraits type.
460 460
    template <class T>
461 461
    struct SetOperationTraits
462 462
      : public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > {
463 463
      typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> >
464 464
      Create;
465 465
    };
466 466

	
467 467
    ///@}
468 468

	
469 469
  protected:
470 470

	
471 471
    Dijkstra() {}
472 472

	
473 473
  public:
474 474

	
475 475
    ///Constructor.
476 476

	
477 477
    ///Constructor.
478 478
    ///\param _g The digraph the algorithm runs on.
479 479
    ///\param _length The length map used by the algorithm.
480 480
    Dijkstra(const Digraph& _g, const LengthMap& _length) :
481 481
      G(&_g), length(&_length),
482 482
      _pred(NULL), local_pred(false),
483 483
      _dist(NULL), local_dist(false),
484 484
      _processed(NULL), local_processed(false),
485 485
      _heap_cross_ref(NULL), local_heap_cross_ref(false),
486 486
      _heap(NULL), local_heap(false)
487 487
    { }
488 488

	
489 489
    ///Destructor.
490 490
    ~Dijkstra()
491 491
    {
492 492
      if(local_pred) delete _pred;
493 493
      if(local_dist) delete _dist;
494 494
      if(local_processed) delete _processed;
495 495
      if(local_heap_cross_ref) delete _heap_cross_ref;
496 496
      if(local_heap) delete _heap;
497 497
    }
498 498

	
499 499
    ///Sets the length map.
500 500

	
501 501
    ///Sets the length map.
502 502
    ///\return <tt> (*this) </tt>
503 503
    Dijkstra &lengthMap(const LengthMap &m)
504 504
    {
505 505
      length = &m;
506 506
      return *this;
507 507
    }
508 508

	
509 509
    ///Sets the map that stores the predecessor arcs.
510 510

	
511 511
    ///Sets the map that stores the predecessor arcs.
512 512
    ///If you don't use this function before calling \ref run(),
513 513
    ///it will allocate one. The destructor deallocates this
514 514
    ///automatically allocated map, of course.
515 515
    ///\return <tt> (*this) </tt>
516 516
    Dijkstra &predMap(PredMap &m)
517 517
    {
518 518
      if(local_pred) {
519 519
        delete _pred;
520 520
        local_pred=false;
521 521
      }
522 522
      _pred = &m;
523 523
      return *this;
524 524
    }
525 525

	
526 526
    ///Sets the map that indicates which nodes are processed.
527 527

	
528 528
    ///Sets the map that indicates which nodes are processed.
529 529
    ///If you don't use this function before calling \ref run(),
530 530
    ///it will allocate one. The destructor deallocates this
531 531
    ///automatically allocated map, of course.
532 532
    ///\return <tt> (*this) </tt>
533 533
    Dijkstra &processedMap(ProcessedMap &m)
534 534
    {
535 535
      if(local_processed) {
536 536
        delete _processed;
537 537
        local_processed=false;
538 538
      }
539 539
      _processed = &m;
540 540
      return *this;
541 541
    }
542 542

	
543 543
    ///Sets the map that stores the distances of the nodes.
544 544

	
545 545
    ///Sets the map that stores the distances of the nodes calculated by the
546 546
    ///algorithm.
547 547
    ///If you don't use this function before calling \ref run(),
548 548
    ///it will allocate one. The destructor deallocates this
549 549
    ///automatically allocated map, of course.
550 550
    ///\return <tt> (*this) </tt>
551 551
    Dijkstra &distMap(DistMap &m)
552 552
    {
553 553
      if(local_dist) {
554 554
        delete _dist;
555 555
        local_dist=false;
556 556
      }
557 557
      _dist = &m;
558 558
      return *this;
559 559
    }
560 560

	
561 561
    ///Sets the heap and the cross reference used by algorithm.
562 562

	
563 563
    ///Sets the heap and the cross reference used by algorithm.
564 564
    ///If you don't use this function before calling \ref run(),
565 565
    ///it will allocate one. The destructor deallocates this
566 566
    ///automatically allocated heap and cross reference, of course.
567 567
    ///\return <tt> (*this) </tt>
568 568
    Dijkstra &heap(Heap& hp, HeapCrossRef &cr)
569 569
    {
570 570
      if(local_heap_cross_ref) {
571 571
        delete _heap_cross_ref;
572 572
        local_heap_cross_ref=false;
573 573
      }
574 574
      _heap_cross_ref = &cr;
575 575
      if(local_heap) {
576 576
        delete _heap;
577 577
        local_heap=false;
578 578
      }
579 579
      _heap = &hp;
580 580
      return *this;
581 581
    }
582 582

	
583 583
  private:
584 584

	
585 585
    void finalizeNodeData(Node v,Value dst)
586 586
    {
587 587
      _processed->set(v,true);
588 588
      _dist->set(v, dst);
589 589
    }
590 590

	
591 591
  public:
592 592

	
593 593
    ///\name Execution control
594 594
    ///The simplest way to execute the algorithm is to use one of the
595 595
    ///member functions called \ref lemon::Dijkstra::run() "run()".
596 596
    ///\n
597 597
    ///If you need more control on the execution, first you must call
598 598
    ///\ref lemon::Dijkstra::init() "init()", then you can add several
599 599
    ///source nodes with \ref lemon::Dijkstra::addSource() "addSource()".
600 600
    ///Finally \ref lemon::Dijkstra::start() "start()" will perform the
601 601
    ///actual path computation.
602 602

	
603 603
    ///@{
604 604

	
605 605
    ///Initializes the internal data structures.
606 606

	
607 607
    ///Initializes the internal data structures.
608 608
    ///
609 609
    void init()
610 610
    {
611 611
      create_maps();
612 612
      _heap->clear();
613 613
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
614 614
        _pred->set(u,INVALID);
615 615
        _processed->set(u,false);
616 616
        _heap_cross_ref->set(u,Heap::PRE_HEAP);
617 617
      }
618 618
    }
619 619

	
620 620
    ///Adds a new source node.
621 621

	
622 622
    ///Adds a new source node to the priority heap.
623 623
    ///The optional second parameter is the initial distance of the node.
624 624
    ///
625 625
    ///The function checks if the node has already been added to the heap and
626 626
    ///it is pushed to the heap only if either it was not in the heap
627 627
    ///or the shortest path found till then is shorter than \c dst.
628 628
    void addSource(Node s,Value dst=OperationTraits::zero())
629 629
    {
630 630
      if(_heap->state(s) != Heap::IN_HEAP) {
631 631
        _heap->push(s,dst);
632 632
      } else if(OperationTraits::less((*_heap)[s], dst)) {
633 633
        _heap->set(s,dst);
634 634
        _pred->set(s,INVALID);
635 635
      }
636 636
    }
637 637

	
638 638
    ///Processes the next node in the priority heap
639 639

	
640 640
    ///Processes the next node in the priority heap.
641 641
    ///
642 642
    ///\return The processed node.
643 643
    ///
644 644
    ///\warning The priority heap must not be empty.
645 645
    Node processNextNode()
646 646
    {
647 647
      Node v=_heap->top();
648 648
      Value oldvalue=_heap->prio();
649 649
      _heap->pop();
650 650
      finalizeNodeData(v,oldvalue);
651 651

	
652 652
      for(OutArcIt e(*G,v); e!=INVALID; ++e) {
653 653
        Node w=G->target(e);
654 654
        switch(_heap->state(w)) {
655 655
        case Heap::PRE_HEAP:
656 656
          _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e]));
657 657
          _pred->set(w,e);
658 658
          break;
659 659
        case Heap::IN_HEAP:
660 660
          {
661 661
            Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]);
662 662
            if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
663 663
              _heap->decrease(w, newvalue);
664 664
              _pred->set(w,e);
665 665
            }
666 666
          }
667 667
          break;
668 668
        case Heap::POST_HEAP:
669 669
          break;
670 670
        }
671 671
      }
672 672
      return v;
673 673
    }
674 674

	
675 675
    ///The next node to be processed.
676 676

	
677 677
    ///Returns the next node to be processed or \c INVALID if the
678 678
    ///priority heap is empty.
679 679
    Node nextNode() const
680 680
    {
681 681
      return !_heap->empty()?_heap->top():INVALID;
682 682
    }
683 683

	
684 684
    ///\brief Returns \c false if there are nodes
685 685
    ///to be processed.
686 686
    ///
687 687
    ///Returns \c false if there are nodes
688 688
    ///to be processed in the priority heap.
689 689
    bool emptyQueue() const { return _heap->empty(); }
690 690

	
691 691
    ///Returns the number of the nodes to be processed in the priority heap
692 692

	
693 693
    ///Returns the number of the nodes to be processed in the priority heap.
694 694
    ///
695 695
    int queueSize() const { return _heap->size(); }
696 696

	
697 697
    ///Executes the algorithm.
698 698

	
699 699
    ///Executes the algorithm.
700 700
    ///
701 701
    ///This method runs the %Dijkstra algorithm from the root node(s)
702 702
    ///in order to compute the shortest path to each node.
703 703
    ///
704 704
    ///The algorithm computes
705 705
    ///- the shortest path tree (forest),
706 706
    ///- the distance of each node from the root(s).
707 707
    ///
708 708
    ///\pre init() must be called and at least one root node should be
709 709
    ///added with addSource() before using this function.
710 710
    ///
711 711
    ///\note <tt>d.start()</tt> is just a shortcut of the following code.
712 712
    ///\code
713 713
    ///  while ( !d.emptyQueue() ) {
714 714
    ///    d.processNextNode();
715 715
    ///  }
716 716
    ///\endcode
717 717
    void start()
718 718
    {
719 719
      while ( !emptyQueue() ) processNextNode();
720 720
    }
721 721

	
722 722
    ///Executes the algorithm until the given target node is processed.
723 723

	
724 724
    ///Executes the algorithm until the given target node is processed.
725 725
    ///
726 726
    ///This method runs the %Dijkstra algorithm from the root node(s)
727 727
    ///in order to compute the shortest path to \c t.
728 728
    ///
729 729
    ///The algorithm computes
730 730
    ///- the shortest path to \c t,
731 731
    ///- the distance of \c t from the root(s).
732 732
    ///
733 733
    ///\pre init() must be called and at least one root node should be
734 734
    ///added with addSource() before using this function.
735 735
    void start(Node t)
736 736
    {
737 737
      while ( !_heap->empty() && _heap->top()!=t ) processNextNode();
738 738
      if ( !_heap->empty() ) {
739 739
        finalizeNodeData(_heap->top(),_heap->prio());
740 740
        _heap->pop();
741 741
      }
742 742
    }
743 743

	
744 744
    ///Executes the algorithm until a condition is met.
745 745

	
746 746
    ///Executes the algorithm until a condition is met.
747 747
    ///
748 748
    ///This method runs the %Dijkstra algorithm from the root node(s) in
749 749
    ///order to compute the shortest path to a node \c v with
750 750
    /// <tt>nm[v]</tt> true, if such a node can be found.
751 751
    ///
752 752
    ///\param nm A \c bool (or convertible) node map. The algorithm
753 753
    ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
754 754
    ///
755 755
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
756 756
    ///\c INVALID if no such node was found.
757 757
    ///
758 758
    ///\pre init() must be called and at least one root node should be
759 759
    ///added with addSource() before using this function.
760 760
    template<class NodeBoolMap>
761 761
    Node start(const NodeBoolMap &nm)
762 762
    {
763 763
      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
764 764
      if ( _heap->empty() ) return INVALID;
765 765
      finalizeNodeData(_heap->top(),_heap->prio());
766 766
      return _heap->top();
767 767
    }
768 768

	
769 769
    ///Runs the algorithm from the given source node.
770 770

	
771 771
    ///This method runs the %Dijkstra algorithm from node \c s
772 772
    ///in order to compute the shortest path to each node.
773 773
    ///
774 774
    ///The algorithm computes
775 775
    ///- the shortest path tree,
776 776
    ///- the distance of each node from the root.
777 777
    ///
778 778
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
779 779
    ///\code
780 780
    ///  d.init();
781 781
    ///  d.addSource(s);
782 782
    ///  d.start();
783 783
    ///\endcode
784 784
    void run(Node s) {
785 785
      init();
786 786
      addSource(s);
787 787
      start();
788 788
    }
789 789

	
790 790
    ///Finds the shortest path between \c s and \c t.
791 791

	
792 792
    ///This method runs the %Dijkstra algorithm from node \c s
793 793
    ///in order to compute the shortest path to node \c t
794 794
    ///(it stops searching when \c t is processed).
795 795
    ///
796 796
    ///\return \c true if \c t is reachable form \c s.
797 797
    ///
798 798
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a
799 799
    ///shortcut of the following code.
800 800
    ///\code
801 801
    ///  d.init();
802 802
    ///  d.addSource(s);
803 803
    ///  d.start(t);
804 804
    ///\endcode
805 805
    bool run(Node s,Node t) {
806 806
      init();
807 807
      addSource(s);
808 808
      start(t);
809 809
      return (*_heap_cross_ref)[t] == Heap::POST_HEAP;
810 810
    }
811 811

	
812 812
    ///@}
813 813

	
814 814
    ///\name Query Functions
815 815
    ///The result of the %Dijkstra algorithm can be obtained using these
816 816
    ///functions.\n
817 817
    ///Either \ref lemon::Dijkstra::run() "run()" or
818 818
    ///\ref lemon::Dijkstra::start() "start()" must be called before
819 819
    ///using them.
820 820

	
821 821
    ///@{
822 822

	
823 823
    ///The shortest path to a node.
824 824

	
825 825
    ///Returns the shortest path to a node.
826 826
    ///
827 827
    ///\warning \c t should be reachable from the root(s).
828 828
    ///
829 829
    ///\pre Either \ref run() or \ref start() must be called before
830 830
    ///using this function.
831 831
    Path path(Node t) const { return Path(*G, *_pred, t); }
832 832

	
833 833
    ///The distance of a node from the root(s).
834 834

	
835 835
    ///Returns the distance of a node from the root(s).
836 836
    ///
837 837
    ///\warning If node \c v is not reachable from the root(s), then
838 838
    ///the return value of this function is undefined.
839 839
    ///
840 840
    ///\pre Either \ref run() or \ref start() must be called before
841 841
    ///using this function.
842 842
    Value dist(Node v) const { return (*_dist)[v]; }
843 843

	
844 844
    ///Returns the 'previous arc' of the shortest path tree for a node.
845 845

	
846 846
    ///This function returns the 'previous arc' of the shortest path
847 847
    ///tree for the node \c v, i.e. it returns the last arc of a
848 848
    ///shortest path from the root(s) to \c v. It is \c INVALID if \c v
849 849
    ///is not reachable from the root(s) or if \c v is a root.
850 850
    ///
851 851
    ///The shortest path tree used here is equal to the shortest path
852 852
    ///tree used in \ref predNode().
853 853
    ///
854 854
    ///\pre Either \ref run() or \ref start() must be called before
855 855
    ///using this function.
856 856
    Arc predArc(Node v) const { return (*_pred)[v]; }
857 857

	
858 858
    ///Returns the 'previous node' of the shortest path tree for a node.
859 859

	
860 860
    ///This function returns the 'previous node' of the shortest path
861 861
    ///tree for the node \c v, i.e. it returns the last but one node
862 862
    ///from a shortest path from the root(s) to \c v. It is \c INVALID
863 863
    ///if \c v is not reachable from the root(s) or if \c v is a root.
864 864
    ///
865 865
    ///The shortest path tree used here is equal to the shortest path
866 866
    ///tree used in \ref predArc().
867 867
    ///
868 868
    ///\pre Either \ref run() or \ref start() must be called before
869 869
    ///using this function.
870 870
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
871 871
                                  G->source((*_pred)[v]); }
872 872

	
873 873
    ///\brief Returns a const reference to the node map that stores the
874 874
    ///distances of the nodes.
875 875
    ///
876 876
    ///Returns a const reference to the node map that stores the distances
877 877
    ///of the nodes calculated by the algorithm.
878 878
    ///
879 879
    ///\pre Either \ref run() or \ref init()
880 880
    ///must be called before using this function.
881 881
    const DistMap &distMap() const { return *_dist;}
882 882

	
883 883
    ///\brief Returns a const reference to the node map that stores the
884 884
    ///predecessor arcs.
885 885
    ///
886 886
    ///Returns a const reference to the node map that stores the predecessor
887 887
    ///arcs, which form the shortest path tree.
888 888
    ///
889 889
    ///\pre Either \ref run() or \ref init()
890 890
    ///must be called before using this function.
891 891
    const PredMap &predMap() const { return *_pred;}
892 892

	
893 893
    ///Checks if a node is reachable from the root(s).
894 894

	
895 895
    ///Returns \c true if \c v is reachable from the root(s).
896 896
    ///\pre Either \ref run() or \ref start()
897 897
    ///must be called before using this function.
898 898
    bool reached(Node v) const { return (*_heap_cross_ref)[v] !=
899 899
                                        Heap::PRE_HEAP; }
900 900

	
901 901
    ///Checks if a node is processed.
902 902

	
903 903
    ///Returns \c true if \c v is processed, i.e. the shortest
904 904
    ///path to \c v has already found.
905 905
    ///\pre Either \ref run() or \ref init()
906 906
    ///must be called before using this function.
907 907
    bool processed(Node v) const { return (*_heap_cross_ref)[v] ==
908 908
                                          Heap::POST_HEAP; }
909 909

	
910 910
    ///The current distance of a node from the root(s).
911 911

	
912 912
    ///Returns the current distance of a node from the root(s).
913 913
    ///It may be decreased in the following processes.
914 914
    ///\pre Either \ref run() or \ref init()
915 915
    ///must be called before using this function and
916 916
    ///node \c v must be reached but not necessarily processed.
917 917
    Value currentDist(Node v) const {
918 918
      return processed(v) ? (*_dist)[v] : (*_heap)[v];
919 919
    }
920 920

	
921 921
    ///@}
922 922
  };
923 923

	
924 924

	
925 925
  ///Default traits class of dijkstra() function.
926 926

	
927 927
  ///Default traits class of dijkstra() function.
928 928
  ///\tparam GR The type of the digraph.
929 929
  ///\tparam LM The type of the length map.
930 930
  template<class GR, class LM>
931 931
  struct DijkstraWizardDefaultTraits
932 932
  {
933 933
    ///The type of the digraph the algorithm runs on.
934 934
    typedef GR Digraph;
935 935
    ///The type of the map that stores the arc lengths.
936 936

	
937 937
    ///The type of the map that stores the arc lengths.
938 938
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
939 939
    typedef LM LengthMap;
940 940
    ///The type of the length of the arcs.
941 941
    typedef typename LM::Value Value;
942 942

	
943 943
    /// Operation traits for Dijkstra algorithm.
944 944

	
945 945
    /// This class defines the operations that are used in the algorithm.
946 946
    /// \see DijkstraDefaultOperationTraits
947 947
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
948 948

	
949 949
    /// The cross reference type used by the heap.
950 950

	
951 951
    /// The cross reference type used by the heap.
952 952
    /// Usually it is \c Digraph::NodeMap<int>.
953 953
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
954 954
    ///Instantiates a \ref HeapCrossRef.
955 955

	
956 956
    ///This function instantiates a \ref HeapCrossRef.
957 957
    /// \param g is the digraph, to which we would like to define the
958 958
    /// HeapCrossRef.
959 959
    static HeapCrossRef *createHeapCrossRef(const Digraph &g)
960 960
    {
961 961
      return new HeapCrossRef(g);
962 962
    }
963 963

	
964 964
    ///The heap type used by the Dijkstra algorithm.
965 965

	
966 966
    ///The heap type used by the Dijkstra algorithm.
967 967
    ///
968 968
    ///\sa BinHeap
969 969
    ///\sa Dijkstra
970 970
    typedef BinHeap<Value, typename Digraph::template NodeMap<int>,
971 971
                    std::less<Value> > Heap;
972 972

	
973 973
    ///Instantiates a \ref Heap.
974 974

	
975 975
    ///This function instantiates a \ref Heap.
976 976
    /// \param r is the HeapCrossRef which is used.
977 977
    static Heap *createHeap(HeapCrossRef& r)
978 978
    {
979 979
      return new Heap(r);
980 980
    }
981 981

	
982 982
    ///\brief The type of the map that stores the predecessor
983 983
    ///arcs of the shortest paths.
984 984
    ///
985 985
    ///The type of the map that stores the predecessor
986 986
    ///arcs of the shortest paths.
987 987
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
988 988
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
989
    ///Instantiates a \ref PredMap.
989
    ///Instantiates a PredMap.
990 990

	
991
    ///This function instantiates a \ref PredMap.
991
    ///This function instantiates a PredMap.
992 992
    ///\param g is the digraph, to which we would like to define the
993
    ///\ref PredMap.
993
    ///PredMap.
994 994
    static PredMap *createPredMap(const Digraph &g)
995 995
    {
996 996
      return new PredMap(g);
997 997
    }
998 998

	
999 999
    ///The type of the map that indicates which nodes are processed.
1000 1000

	
1001 1001
    ///The type of the map that indicates which nodes are processed.
1002 1002
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
1003 1003
    ///By default it is a NullMap.
1004 1004
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
1005
    ///Instantiates a \ref ProcessedMap.
1005
    ///Instantiates a ProcessedMap.
1006 1006

	
1007
    ///This function instantiates a \ref ProcessedMap.
1007
    ///This function instantiates a ProcessedMap.
1008 1008
    ///\param g is the digraph, to which
1009
    ///we would like to define the \ref ProcessedMap.
1009
    ///we would like to define the ProcessedMap.
1010 1010
#ifdef DOXYGEN
1011 1011
    static ProcessedMap *createProcessedMap(const Digraph &g)
1012 1012
#else
1013 1013
    static ProcessedMap *createProcessedMap(const Digraph &)
1014 1014
#endif
1015 1015
    {
1016 1016
      return new ProcessedMap();
1017 1017
    }
1018 1018

	
1019 1019
    ///The type of the map that stores the distances of the nodes.
1020 1020

	
1021 1021
    ///The type of the map that stores the distances of the nodes.
1022 1022
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
1023 1023
    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
1024
    ///Instantiates a \ref DistMap.
1024
    ///Instantiates a DistMap.
1025 1025

	
1026
    ///This function instantiates a \ref DistMap.
1026
    ///This function instantiates a DistMap.
1027 1027
    ///\param g is the digraph, to which we would like to define
1028
    ///the \ref DistMap
1028
    ///the DistMap
1029 1029
    static DistMap *createDistMap(const Digraph &g)
1030 1030
    {
1031 1031
      return new DistMap(g);
1032 1032
    }
1033 1033

	
1034 1034
    ///The type of the shortest paths.
1035 1035

	
1036 1036
    ///The type of the shortest paths.
1037 1037
    ///It must meet the \ref concepts::Path "Path" concept.
1038 1038
    typedef lemon::Path<Digraph> Path;
1039 1039
  };
1040 1040

	
1041 1041
  /// Default traits class used by \ref DijkstraWizard
1042 1042

	
1043 1043
  /// To make it easier to use Dijkstra algorithm
1044 1044
  /// we have created a wizard class.
1045 1045
  /// This \ref DijkstraWizard class needs default traits,
1046 1046
  /// as well as the \ref Dijkstra class.
1047 1047
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
1048 1048
  /// \ref DijkstraWizard class.
1049 1049
  template<class GR,class LM>
1050 1050
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
1051 1051
  {
1052 1052
    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
1053 1053
  protected:
1054 1054
    //The type of the nodes in the digraph.
1055 1055
    typedef typename Base::Digraph::Node Node;
1056 1056

	
1057 1057
    //Pointer to the digraph the algorithm runs on.
1058 1058
    void *_g;
1059 1059
    //Pointer to the length map.
1060 1060
    void *_length;
1061 1061
    //Pointer to the map of processed nodes.
1062 1062
    void *_processed;
1063 1063
    //Pointer to the map of predecessors arcs.
1064 1064
    void *_pred;
1065 1065
    //Pointer to the map of distances.
1066 1066
    void *_dist;
1067 1067
    //Pointer to the shortest path to the target node.
1068 1068
    void *_path;
1069 1069
    //Pointer to the distance of the target node.
1070 1070
    void *_di;
1071 1071

	
1072 1072
  public:
1073 1073
    /// Constructor.
1074 1074

	
1075 1075
    /// This constructor does not require parameters, therefore it initiates
1076 1076
    /// all of the attributes to \c 0.
1077 1077
    DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
1078 1078
                           _dist(0), _path(0), _di(0) {}
1079 1079

	
1080 1080
    /// Constructor.
1081 1081

	
1082 1082
    /// This constructor requires two parameters,
1083 1083
    /// others are initiated to \c 0.
1084 1084
    /// \param g The digraph the algorithm runs on.
1085 1085
    /// \param l The length map.
1086 1086
    DijkstraWizardBase(const GR &g,const LM &l) :
1087 1087
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
1088 1088
      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
1089 1089
      _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
1090 1090

	
1091 1091
  };
1092 1092

	
1093 1093
  /// Auxiliary class for the function-type interface of Dijkstra algorithm.
1094 1094

	
1095 1095
  /// This auxiliary class is created to implement the
1096 1096
  /// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm.
1097 1097
  /// It does not have own \ref run() method, it uses the functions
1098 1098
  /// and features of the plain \ref Dijkstra.
1099 1099
  ///
1100 1100
  /// This class should only be used through the \ref dijkstra() function,
1101 1101
  /// which makes it easier to use the algorithm.
1102 1102
  template<class TR>
1103 1103
  class DijkstraWizard : public TR
1104 1104
  {
1105 1105
    typedef TR Base;
1106 1106

	
1107 1107
    ///The type of the digraph the algorithm runs on.
1108 1108
    typedef typename TR::Digraph Digraph;
1109 1109

	
1110 1110
    typedef typename Digraph::Node Node;
1111 1111
    typedef typename Digraph::NodeIt NodeIt;
1112 1112
    typedef typename Digraph::Arc Arc;
1113 1113
    typedef typename Digraph::OutArcIt OutArcIt;
1114 1114

	
1115 1115
    ///The type of the map that stores the arc lengths.
1116 1116
    typedef typename TR::LengthMap LengthMap;
1117 1117
    ///The type of the length of the arcs.
1118 1118
    typedef typename LengthMap::Value Value;
1119 1119
    ///\brief The type of the map that stores the predecessor
1120 1120
    ///arcs of the shortest paths.
1121 1121
    typedef typename TR::PredMap PredMap;
1122 1122
    ///The type of the map that stores the distances of the nodes.
1123 1123
    typedef typename TR::DistMap DistMap;
1124 1124
    ///The type of the map that indicates which nodes are processed.
1125 1125
    typedef typename TR::ProcessedMap ProcessedMap;
1126 1126
    ///The type of the shortest paths
1127 1127
    typedef typename TR::Path Path;
1128 1128
    ///The heap type used by the dijkstra algorithm.
1129 1129
    typedef typename TR::Heap Heap;
1130 1130

	
1131 1131
  public:
1132 1132

	
1133 1133
    /// Constructor.
1134 1134
    DijkstraWizard() : TR() {}
1135 1135

	
1136 1136
    /// Constructor that requires parameters.
1137 1137

	
1138 1138
    /// Constructor that requires parameters.
1139 1139
    /// These parameters will be the default values for the traits class.
1140 1140
    /// \param g The digraph the algorithm runs on.
1141 1141
    /// \param l The length map.
1142 1142
    DijkstraWizard(const Digraph &g, const LengthMap &l) :
1143 1143
      TR(g,l) {}
1144 1144

	
1145 1145
    ///Copy constructor
1146 1146
    DijkstraWizard(const TR &b) : TR(b) {}
1147 1147

	
1148 1148
    ~DijkstraWizard() {}
1149 1149

	
1150 1150
    ///Runs Dijkstra algorithm from the given source node.
1151 1151

	
1152 1152
    ///This method runs %Dijkstra algorithm from the given source node
1153 1153
    ///in order to compute the shortest path to each node.
1154 1154
    void run(Node s)
1155 1155
    {
1156 1156
      Dijkstra<Digraph,LengthMap,TR>
1157 1157
        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
1158 1158
             *reinterpret_cast<const LengthMap*>(Base::_length));
1159 1159
      if (Base::_pred)
1160 1160
        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1161 1161
      if (Base::_dist)
1162 1162
        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1163 1163
      if (Base::_processed)
1164 1164
        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1165 1165
      dijk.run(s);
1166 1166
    }
1167 1167

	
1168 1168
    ///Finds the shortest path between \c s and \c t.
1169 1169

	
1170 1170
    ///This method runs the %Dijkstra algorithm from node \c s
1171 1171
    ///in order to compute the shortest path to node \c t
1172 1172
    ///(it stops searching when \c t is processed).
1173 1173
    ///
1174 1174
    ///\return \c true if \c t is reachable form \c s.
1175 1175
    bool run(Node s, Node t)
1176 1176
    {
1177 1177
      Dijkstra<Digraph,LengthMap,TR>
1178 1178
        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
1179 1179
             *reinterpret_cast<const LengthMap*>(Base::_length));
1180 1180
      if (Base::_pred)
1181 1181
        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1182 1182
      if (Base::_dist)
1183 1183
        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1184 1184
      if (Base::_processed)
1185 1185
        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1186 1186
      dijk.run(s,t);
1187 1187
      if (Base::_path)
1188 1188
        *reinterpret_cast<Path*>(Base::_path) = dijk.path(t);
1189 1189
      if (Base::_di)
1190 1190
        *reinterpret_cast<Value*>(Base::_di) = dijk.dist(t);
1191 1191
      return dijk.reached(t);
1192 1192
    }
1193 1193

	
1194 1194
    template<class T>
1195 1195
    struct SetPredMapBase : public Base {
1196 1196
      typedef T PredMap;
1197 1197
      static PredMap *createPredMap(const Digraph &) { return 0; };
1198 1198
      SetPredMapBase(const TR &b) : TR(b) {}
1199 1199
    };
1200 1200
    ///\brief \ref named-func-param "Named parameter"
1201
    ///for setting \ref PredMap object.
1201
    ///for setting PredMap object.
1202 1202
    ///
1203 1203
    ///\ref named-func-param "Named parameter"
1204
    ///for setting \ref PredMap object.
1204
    ///for setting PredMap object.
1205 1205
    template<class T>
1206 1206
    DijkstraWizard<SetPredMapBase<T> > predMap(const T &t)
1207 1207
    {
1208 1208
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1209 1209
      return DijkstraWizard<SetPredMapBase<T> >(*this);
1210 1210
    }
1211 1211

	
1212 1212
    template<class T>
1213 1213
    struct SetDistMapBase : public Base {
1214 1214
      typedef T DistMap;
1215 1215
      static DistMap *createDistMap(const Digraph &) { return 0; };
1216 1216
      SetDistMapBase(const TR &b) : TR(b) {}
1217 1217
    };
1218 1218
    ///\brief \ref named-func-param "Named parameter"
1219
    ///for setting \ref DistMap object.
1219
    ///for setting DistMap object.
1220 1220
    ///
1221 1221
    ///\ref named-func-param "Named parameter"
1222
    ///for setting \ref DistMap object.
1222
    ///for setting DistMap object.
1223 1223
    template<class T>
1224 1224
    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
1225 1225
    {
1226 1226
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1227 1227
      return DijkstraWizard<SetDistMapBase<T> >(*this);
1228 1228
    }
1229 1229

	
1230 1230
    template<class T>
1231 1231
    struct SetProcessedMapBase : public Base {
1232 1232
      typedef T ProcessedMap;
1233 1233
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1234 1234
      SetProcessedMapBase(const TR &b) : TR(b) {}
1235 1235
    };
1236 1236
    ///\brief \ref named-func-param "Named parameter"
1237
    ///for setting \ref ProcessedMap object.
1237
    ///for setting ProcessedMap object.
1238 1238
    ///
1239 1239
    /// \ref named-func-param "Named parameter"
1240
    ///for setting \ref ProcessedMap object.
1240
    ///for setting ProcessedMap object.
1241 1241
    template<class T>
1242 1242
    DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1243 1243
    {
1244 1244
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1245 1245
      return DijkstraWizard<SetProcessedMapBase<T> >(*this);
1246 1246
    }
1247 1247

	
1248 1248
    template<class T>
1249 1249
    struct SetPathBase : public Base {
1250 1250
      typedef T Path;
1251 1251
      SetPathBase(const TR &b) : TR(b) {}
1252 1252
    };
1253 1253
    ///\brief \ref named-func-param "Named parameter"
1254 1254
    ///for getting the shortest path to the target node.
1255 1255
    ///
1256 1256
    ///\ref named-func-param "Named parameter"
1257 1257
    ///for getting the shortest path to the target node.
1258 1258
    template<class T>
1259 1259
    DijkstraWizard<SetPathBase<T> > path(const T &t)
1260 1260
    {
1261 1261
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1262 1262
      return DijkstraWizard<SetPathBase<T> >(*this);
1263 1263
    }
1264 1264

	
1265 1265
    ///\brief \ref named-func-param "Named parameter"
1266 1266
    ///for getting the distance of the target node.
1267 1267
    ///
1268 1268
    ///\ref named-func-param "Named parameter"
1269 1269
    ///for getting the distance of the target node.
1270 1270
    DijkstraWizard dist(const Value &d)
1271 1271
    {
1272 1272
      Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
1273 1273
      return *this;
1274 1274
    }
1275 1275

	
1276 1276
  };
1277 1277

	
1278 1278
  ///Function-type interface for Dijkstra algorithm.
1279 1279

	
1280 1280
  /// \ingroup shortest_path
1281 1281
  ///Function-type interface for Dijkstra algorithm.
1282 1282
  ///
1283 1283
  ///This function also has several \ref named-func-param "named parameters",
1284 1284
  ///they are declared as the members of class \ref DijkstraWizard.
1285 1285
  ///The following examples show how to use these parameters.
1286 1286
  ///\code
1287 1287
  ///  // Compute shortest path from node s to each node
1288 1288
  ///  dijkstra(g,length).predMap(preds).distMap(dists).run(s);
1289 1289
  ///
1290 1290
  ///  // Compute shortest path from s to t
1291 1291
  ///  bool reached = dijkstra(g,length).path(p).dist(d).run(s,t);
1292 1292
  ///\endcode
1293 1293
  ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1294 1294
  ///to the end of the parameter list.
1295 1295
  ///\sa DijkstraWizard
1296 1296
  ///\sa Dijkstra
1297 1297
  template<class GR, class LM>
1298 1298
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
1299 1299
  dijkstra(const GR &digraph, const LM &length)
1300 1300
  {
1301 1301
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length);
1302 1302
  }
1303 1303

	
1304 1304
} //END OF NAMESPACE LEMON
1305 1305

	
1306 1306
#endif
Ignore white space 3072 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_MAPS_H
20 20
#define LEMON_MAPS_H
21 21

	
22 22
#include <iterator>
23 23
#include <functional>
24 24
#include <vector>
25 25

	
26 26
#include <lemon/core.h>
27 27

	
28 28
///\file
29 29
///\ingroup maps
30 30
///\brief Miscellaneous property maps
31 31

	
32 32
#include <map>
33 33

	
34 34
namespace lemon {
35 35

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

	
39 39
  /// Base class of maps.
40 40

	
41 41
  /// Base class of maps. It provides the necessary type definitions
42 42
  /// required by the map %concepts.
43 43
  template<typename K, typename V>
44 44
  class MapBase {
45 45
  public:
46 46
    /// \biref The key type of the map.
47 47
    typedef K Key;
48 48
    /// \brief The value type of the map.
49 49
    /// (The type of objects associated with the keys).
50 50
    typedef V Value;
51 51
  };
52 52

	
53 53

	
54 54
  /// Null map. (a.k.a. DoNothingMap)
55 55

	
56 56
  /// This map can be used if you have to provide a map only for
57 57
  /// its type definitions, or if you have to provide a writable map,
58 58
  /// but data written to it is not required (i.e. it will be sent to
59 59
  /// <tt>/dev/null</tt>).
60 60
  /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
61 61
  ///
62 62
  /// \sa ConstMap
63 63
  template<typename K, typename V>
64 64
  class NullMap : public MapBase<K, V> {
65 65
  public:
66 66
    typedef MapBase<K, V> Parent;
67 67
    typedef typename Parent::Key Key;
68 68
    typedef typename Parent::Value Value;
69 69

	
70 70
    /// Gives back a default constructed element.
71 71
    Value operator[](const Key&) const { return Value(); }
72 72
    /// Absorbs the value.
73 73
    void set(const Key&, const Value&) {}
74 74
  };
75 75

	
76
  /// Returns a \ref NullMap class
77

	
78
  /// This function just returns a \ref NullMap class.
76
  /// Returns a \c NullMap class
77

	
78
  /// This function just returns a \c NullMap class.
79 79
  /// \relates NullMap
80 80
  template <typename K, typename V>
81 81
  NullMap<K, V> nullMap() {
82 82
    return NullMap<K, V>();
83 83
  }
84 84

	
85 85

	
86 86
  /// Constant map.
87 87

	
88 88
  /// This \ref concepts::ReadMap "readable map" assigns a specified
89 89
  /// value to each key.
90 90
  ///
91
  /// In other aspects it is equivalent to \ref NullMap.
91
  /// In other aspects it is equivalent to \c NullMap.
92 92
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
93 93
  /// concept, but it absorbs the data written to it.
94 94
  ///
95 95
  /// The simplest way of using this map is through the constMap()
96 96
  /// function.
97 97
  ///
98 98
  /// \sa NullMap
99 99
  /// \sa IdentityMap
100 100
  template<typename K, typename V>
101 101
  class ConstMap : public MapBase<K, V> {
102 102
  private:
103 103
    V _value;
104 104
  public:
105 105
    typedef MapBase<K, V> Parent;
106 106
    typedef typename Parent::Key Key;
107 107
    typedef typename Parent::Value Value;
108 108

	
109 109
    /// Default constructor
110 110

	
111 111
    /// Default constructor.
112 112
    /// The value of the map will be default constructed.
113 113
    ConstMap() {}
114 114

	
115 115
    /// Constructor with specified initial value
116 116

	
117 117
    /// Constructor with specified initial value.
118 118
    /// \param v The initial value of the map.
119 119
    ConstMap(const Value &v) : _value(v) {}
120 120

	
121 121
    /// Gives back the specified value.
122 122
    Value operator[](const Key&) const { return _value; }
123 123

	
124 124
    /// Absorbs the value.
125 125
    void set(const Key&, const Value&) {}
126 126

	
127 127
    /// Sets the value that is assigned to each key.
128 128
    void setAll(const Value &v) {
129 129
      _value = v;
130 130
    }
131 131

	
132 132
    template<typename V1>
133 133
    ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
134 134
  };
135 135

	
136
  /// Returns a \ref ConstMap class
137

	
138
  /// This function just returns a \ref ConstMap class.
136
  /// Returns a \c ConstMap class
137

	
138
  /// This function just returns a \c ConstMap class.
139 139
  /// \relates ConstMap
140 140
  template<typename K, typename V>
141 141
  inline ConstMap<K, V> constMap(const V &v) {
142 142
    return ConstMap<K, V>(v);
143 143
  }
144 144

	
145 145
  template<typename K, typename V>
146 146
  inline ConstMap<K, V> constMap() {
147 147
    return ConstMap<K, V>();
148 148
  }
149 149

	
150 150

	
151 151
  template<typename T, T v>
152 152
  struct Const {};
153 153

	
154 154
  /// Constant map with inlined constant value.
155 155

	
156 156
  /// This \ref concepts::ReadMap "readable map" assigns a specified
157 157
  /// value to each key.
158 158
  ///
159
  /// In other aspects it is equivalent to \ref NullMap.
159
  /// In other aspects it is equivalent to \c NullMap.
160 160
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
161 161
  /// concept, but it absorbs the data written to it.
162 162
  ///
163 163
  /// The simplest way of using this map is through the constMap()
164 164
  /// function.
165 165
  ///
166 166
  /// \sa NullMap
167 167
  /// \sa IdentityMap
168 168
  template<typename K, typename V, V v>
169 169
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
170 170
  public:
171 171
    typedef MapBase<K, V> Parent;
172 172
    typedef typename Parent::Key Key;
173 173
    typedef typename Parent::Value Value;
174 174

	
175 175
    /// Constructor.
176 176
    ConstMap() {}
177 177

	
178 178
    /// Gives back the specified value.
179 179
    Value operator[](const Key&) const { return v; }
180 180

	
181 181
    /// Absorbs the value.
182 182
    void set(const Key&, const Value&) {}
183 183
  };
184 184

	
185
  /// Returns a \ref ConstMap class with inlined constant value
186

	
187
  /// This function just returns a \ref ConstMap class with inlined
185
  /// Returns a \c ConstMap class with inlined constant value
186

	
187
  /// This function just returns a \c ConstMap class with inlined
188 188
  /// constant value.
189 189
  /// \relates ConstMap
190 190
  template<typename K, typename V, V v>
191 191
  inline ConstMap<K, Const<V, v> > constMap() {
192 192
    return ConstMap<K, Const<V, v> >();
193 193
  }
194 194

	
195 195

	
196 196
  /// Identity map.
197 197

	
198 198
  /// This \ref concepts::ReadMap "read-only map" gives back the given
199 199
  /// key as value without any modification.
200 200
  ///
201 201
  /// \sa ConstMap
202 202
  template <typename T>
203 203
  class IdentityMap : public MapBase<T, T> {
204 204
  public:
205 205
    typedef MapBase<T, T> Parent;
206 206
    typedef typename Parent::Key Key;
207 207
    typedef typename Parent::Value Value;
208 208

	
209 209
    /// Gives back the given value without any modification.
210 210
    Value operator[](const Key &k) const {
211 211
      return k;
212 212
    }
213 213
  };
214 214

	
215
  /// Returns an \ref IdentityMap class
216

	
217
  /// This function just returns an \ref IdentityMap class.
215
  /// Returns an \c IdentityMap class
216

	
217
  /// This function just returns an \c IdentityMap class.
218 218
  /// \relates IdentityMap
219 219
  template<typename T>
220 220
  inline IdentityMap<T> identityMap() {
221 221
    return IdentityMap<T>();
222 222
  }
223 223

	
224 224

	
225 225
  /// \brief Map for storing values for integer keys from the range
226 226
  /// <tt>[0..size-1]</tt>.
227 227
  ///
228 228
  /// This map is essentially a wrapper for \c std::vector. It assigns
229 229
  /// values to integer keys from the range <tt>[0..size-1]</tt>.
230 230
  /// It can be used with some data structures, for example
231
  /// \ref UnionFind, \ref BinHeap, when the used items are small
231
  /// \c UnionFind, \c BinHeap, when the used items are small
232 232
  /// integers. This map conforms the \ref concepts::ReferenceMap
233 233
  /// "ReferenceMap" concept.
234 234
  ///
235 235
  /// The simplest way of using this map is through the rangeMap()
236 236
  /// function.
237 237
  template <typename V>
238 238
  class RangeMap : public MapBase<int, V> {
239 239
    template <typename V1>
240 240
    friend class RangeMap;
241 241
  private:
242 242

	
243 243
    typedef std::vector<V> Vector;
244 244
    Vector _vector;
245 245

	
246 246
  public:
247 247

	
248 248
    typedef MapBase<int, V> Parent;
249 249
    /// Key type
250 250
    typedef typename Parent::Key Key;
251 251
    /// Value type
252 252
    typedef typename Parent::Value Value;
253 253
    /// Reference type
254 254
    typedef typename Vector::reference Reference;
255 255
    /// Const reference type
256 256
    typedef typename Vector::const_reference ConstReference;
257 257

	
258 258
    typedef True ReferenceMapTag;
259 259

	
260 260
  public:
261 261

	
262 262
    /// Constructor with specified default value.
263 263
    RangeMap(int size = 0, const Value &value = Value())
264 264
      : _vector(size, value) {}
265 265

	
266 266
    /// Constructs the map from an appropriate \c std::vector.
267 267
    template <typename V1>
268 268
    RangeMap(const std::vector<V1>& vector)
269 269
      : _vector(vector.begin(), vector.end()) {}
270 270

	
271
    /// Constructs the map from another \ref RangeMap.
271
    /// Constructs the map from another \c RangeMap.
272 272
    template <typename V1>
273 273
    RangeMap(const RangeMap<V1> &c)
274 274
      : _vector(c._vector.begin(), c._vector.end()) {}
275 275

	
276 276
    /// Returns the size of the map.
277 277
    int size() {
278 278
      return _vector.size();
279 279
    }
280 280

	
281 281
    /// Resizes the map.
282 282

	
283 283
    /// Resizes the underlying \c std::vector container, so changes the
284 284
    /// keyset of the map.
285 285
    /// \param size The new size of the map. The new keyset will be the
286 286
    /// range <tt>[0..size-1]</tt>.
287 287
    /// \param value The default value to assign to the new keys.
288 288
    void resize(int size, const Value &value = Value()) {
289 289
      _vector.resize(size, value);
290 290
    }
291 291

	
292 292
  private:
293 293

	
294 294
    RangeMap& operator=(const RangeMap&);
295 295

	
296 296
  public:
297 297

	
298 298
    ///\e
299 299
    Reference operator[](const Key &k) {
300 300
      return _vector[k];
301 301
    }
302 302

	
303 303
    ///\e
304 304
    ConstReference operator[](const Key &k) const {
305 305
      return _vector[k];
306 306
    }
307 307

	
308 308
    ///\e
309 309
    void set(const Key &k, const Value &v) {
310 310
      _vector[k] = v;
311 311
    }
312 312
  };
313 313

	
314
  /// Returns a \ref RangeMap class
315

	
316
  /// This function just returns a \ref RangeMap class.
314
  /// Returns a \c RangeMap class
315

	
316
  /// This function just returns a \c RangeMap class.
317 317
  /// \relates RangeMap
318 318
  template<typename V>
319 319
  inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
320 320
    return RangeMap<V>(size, value);
321 321
  }
322 322

	
323
  /// \brief Returns a \ref RangeMap class created from an appropriate
323
  /// \brief Returns a \c RangeMap class created from an appropriate
324 324
  /// \c std::vector
325 325

	
326
  /// This function just returns a \ref RangeMap class created from an
326
  /// This function just returns a \c RangeMap class created from an
327 327
  /// appropriate \c std::vector.
328 328
  /// \relates RangeMap
329 329
  template<typename V>
330 330
  inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
331 331
    return RangeMap<V>(vector);
332 332
  }
333 333

	
334 334

	
335 335
  /// Map type based on \c std::map
336 336

	
337 337
  /// This map is essentially a wrapper for \c std::map with addition
338 338
  /// that you can specify a default value for the keys that are not
339 339
  /// stored actually. This value can be different from the default
340 340
  /// contructed value (i.e. \c %Value()).
341 341
  /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
342 342
  /// concept.
343 343
  ///
344 344
  /// This map is useful if a default value should be assigned to most of
345 345
  /// the keys and different values should be assigned only to a few
346 346
  /// keys (i.e. the map is "sparse").
347 347
  /// The name of this type also refers to this important usage.
348 348
  ///
349 349
  /// Apart form that this map can be used in many other cases since it
350 350
  /// is based on \c std::map, which is a general associative container.
351 351
  /// However keep in mind that it is usually not as efficient as other
352 352
  /// maps.
353 353
  ///
354 354
  /// The simplest way of using this map is through the sparseMap()
355 355
  /// function.
356 356
  template <typename K, typename V, typename Compare = std::less<K> >
357 357
  class SparseMap : public MapBase<K, V> {
358 358
    template <typename K1, typename V1, typename C1>
359 359
    friend class SparseMap;
360 360
  public:
361 361

	
362 362
    typedef MapBase<K, V> Parent;
363 363
    /// Key type
364 364
    typedef typename Parent::Key Key;
365 365
    /// Value type
366 366
    typedef typename Parent::Value Value;
367 367
    /// Reference type
368 368
    typedef Value& Reference;
369 369
    /// Const reference type
370 370
    typedef const Value& ConstReference;
371 371

	
372 372
    typedef True ReferenceMapTag;
373 373

	
374 374
  private:
375 375

	
376 376
    typedef std::map<K, V, Compare> Map;
377 377
    Map _map;
378 378
    Value _value;
379 379

	
380 380
  public:
381 381

	
382 382
    /// \brief Constructor with specified default value.
383 383
    SparseMap(const Value &value = Value()) : _value(value) {}
384 384
    /// \brief Constructs the map from an appropriate \c std::map, and
385 385
    /// explicitly specifies a default value.
386 386
    template <typename V1, typename Comp1>
387 387
    SparseMap(const std::map<Key, V1, Comp1> &map,
388 388
              const Value &value = Value())
389 389
      : _map(map.begin(), map.end()), _value(value) {}
390 390

	
391
    /// \brief Constructs the map from another \ref SparseMap.
391
    /// \brief Constructs the map from another \c SparseMap.
392 392
    template<typename V1, typename Comp1>
393 393
    SparseMap(const SparseMap<Key, V1, Comp1> &c)
394 394
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
395 395

	
396 396
  private:
397 397

	
398 398
    SparseMap& operator=(const SparseMap&);
399 399

	
400 400
  public:
401 401

	
402 402
    ///\e
403 403
    Reference operator[](const Key &k) {
404 404
      typename Map::iterator it = _map.lower_bound(k);
405 405
      if (it != _map.end() && !_map.key_comp()(k, it->first))
406 406
        return it->second;
407 407
      else
408 408
        return _map.insert(it, std::make_pair(k, _value))->second;
409 409
    }
410 410

	
411 411
    ///\e
412 412
    ConstReference operator[](const Key &k) const {
413 413
      typename Map::const_iterator it = _map.find(k);
414 414
      if (it != _map.end())
415 415
        return it->second;
416 416
      else
417 417
        return _value;
418 418
    }
419 419

	
420 420
    ///\e
421 421
    void set(const Key &k, const Value &v) {
422 422
      typename Map::iterator it = _map.lower_bound(k);
423 423
      if (it != _map.end() && !_map.key_comp()(k, it->first))
424 424
        it->second = v;
425 425
      else
426 426
        _map.insert(it, std::make_pair(k, v));
427 427
    }
428 428

	
429 429
    ///\e
430 430
    void setAll(const Value &v) {
431 431
      _value = v;
432 432
      _map.clear();
433 433
    }
434 434
  };
435 435

	
436
  /// Returns a \ref SparseMap class
437

	
438
  /// This function just returns a \ref SparseMap class with specified
436
  /// Returns a \c SparseMap class
437

	
438
  /// This function just returns a \c SparseMap class with specified
439 439
  /// default value.
440 440
  /// \relates SparseMap
441 441
  template<typename K, typename V, typename Compare>
442 442
  inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
443 443
    return SparseMap<K, V, Compare>(value);
444 444
  }
445 445

	
446 446
  template<typename K, typename V>
447 447
  inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
448 448
    return SparseMap<K, V, std::less<K> >(value);
449 449
  }
450 450

	
451
  /// \brief Returns a \ref SparseMap class created from an appropriate
451
  /// \brief Returns a \c SparseMap class created from an appropriate
452 452
  /// \c std::map
453 453

	
454
  /// This function just returns a \ref SparseMap class created from an
454
  /// This function just returns a \c SparseMap class created from an
455 455
  /// appropriate \c std::map.
456 456
  /// \relates SparseMap
457 457
  template<typename K, typename V, typename Compare>
458 458
  inline SparseMap<K, V, Compare>
459 459
    sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
460 460
  {
461 461
    return SparseMap<K, V, Compare>(map, value);
462 462
  }
463 463

	
464 464
  /// @}
465 465

	
466 466
  /// \addtogroup map_adaptors
467 467
  /// @{
468 468

	
469 469
  /// Composition of two maps
470 470

	
471 471
  /// This \ref concepts::ReadMap "read-only map" returns the
472 472
  /// composition of two given maps. That is to say, if \c m1 is of
473 473
  /// type \c M1 and \c m2 is of \c M2, then for
474 474
  /// \code
475 475
  ///   ComposeMap<M1, M2> cm(m1,m2);
476 476
  /// \endcode
477 477
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
478 478
  ///
479 479
  /// The \c Key type of the map is inherited from \c M2 and the
480 480
  /// \c Value type is from \c M1.
481 481
  /// \c M2::Value must be convertible to \c M1::Key.
482 482
  ///
483 483
  /// The simplest way of using this map is through the composeMap()
484 484
  /// function.
485 485
  ///
486 486
  /// \sa CombineMap
487 487
  template <typename M1, typename M2>
488 488
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
489 489
    const M1 &_m1;
490 490
    const M2 &_m2;
491 491
  public:
492 492
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
493 493
    typedef typename Parent::Key Key;
494 494
    typedef typename Parent::Value Value;
495 495

	
496 496
    /// Constructor
497 497
    ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
498 498

	
499 499
    /// \e
500 500
    typename MapTraits<M1>::ConstReturnValue
501 501
    operator[](const Key &k) const { return _m1[_m2[k]]; }
502 502
  };
503 503

	
504
  /// Returns a \ref ComposeMap class
505

	
506
  /// This function just returns a \ref ComposeMap class.
504
  /// Returns a \c ComposeMap class
505

	
506
  /// This function just returns a \c ComposeMap class.
507 507
  ///
508 508
  /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
509 509
  /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
510 510
  /// will be equal to <tt>m1[m2[x]]</tt>.
511 511
  ///
512 512
  /// \relates ComposeMap
513 513
  template <typename M1, typename M2>
514 514
  inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
515 515
    return ComposeMap<M1, M2>(m1, m2);
516 516
  }
517 517

	
518 518

	
519 519
  /// Combination of two maps using an STL (binary) functor.
520 520

	
521 521
  /// This \ref concepts::ReadMap "read-only map" takes two maps and a
522 522
  /// binary functor and returns the combination of the two given maps
523 523
  /// using the functor.
524 524
  /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
525 525
  /// and \c f is of \c F, then for
526 526
  /// \code
527 527
  ///   CombineMap<M1,M2,F,V> cm(m1,m2,f);
528 528
  /// \endcode
529 529
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
530 530
  ///
531 531
  /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
532 532
  /// must be convertible to \c M2::Key) and the \c Value type is \c V.
533 533
  /// \c M2::Value and \c M1::Value must be convertible to the
534 534
  /// corresponding input parameter of \c F and the return type of \c F
535 535
  /// must be convertible to \c V.
536 536
  ///
537 537
  /// The simplest way of using this map is through the combineMap()
538 538
  /// function.
539 539
  ///
540 540
  /// \sa ComposeMap
541 541
  template<typename M1, typename M2, typename F,
542 542
           typename V = typename F::result_type>
543 543
  class CombineMap : public MapBase<typename M1::Key, V> {
544 544
    const M1 &_m1;
545 545
    const M2 &_m2;
546 546
    F _f;
547 547
  public:
548 548
    typedef MapBase<typename M1::Key, V> Parent;
549 549
    typedef typename Parent::Key Key;
550 550
    typedef typename Parent::Value Value;
551 551

	
552 552
    /// Constructor
553 553
    CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
554 554
      : _m1(m1), _m2(m2), _f(f) {}
555 555
    /// \e
556 556
    Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
557 557
  };
558 558

	
559
  /// Returns a \ref CombineMap class
560

	
561
  /// This function just returns a \ref CombineMap class.
559
  /// Returns a \c CombineMap class
560

	
561
  /// This function just returns a \c CombineMap class.
562 562
  ///
563 563
  /// For example, if \c m1 and \c m2 are both maps with \c double
564 564
  /// values, then
565 565
  /// \code
566 566
  ///   combineMap(m1,m2,std::plus<double>())
567 567
  /// \endcode
568 568
  /// is equivalent to
569 569
  /// \code
570 570
  ///   addMap(m1,m2)
571 571
  /// \endcode
572 572
  ///
573 573
  /// This function is specialized for adaptable binary function
574 574
  /// classes and C++ functions.
575 575
  ///
576 576
  /// \relates CombineMap
577 577
  template<typename M1, typename M2, typename F, typename V>
578 578
  inline CombineMap<M1, M2, F, V>
579 579
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
580 580
    return CombineMap<M1, M2, F, V>(m1,m2,f);
581 581
  }
582 582

	
583 583
  template<typename M1, typename M2, typename F>
584 584
  inline CombineMap<M1, M2, F, typename F::result_type>
585 585
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
586 586
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
587 587
  }
588 588

	
589 589
  template<typename M1, typename M2, typename K1, typename K2, typename V>
590 590
  inline CombineMap<M1, M2, V (*)(K1, K2), V>
591 591
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
592 592
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
593 593
  }
594 594

	
595 595

	
596 596
  /// Converts an STL style (unary) functor to a map
597 597

	
598 598
  /// This \ref concepts::ReadMap "read-only map" returns the value
599 599
  /// of a given functor. Actually, it just wraps the functor and
600 600
  /// provides the \c Key and \c Value typedefs.
601 601
  ///
602 602
  /// Template parameters \c K and \c V will become its \c Key and
603 603
  /// \c Value. In most cases they have to be given explicitly because
604 604
  /// a functor typically does not provide \c argument_type and
605 605
  /// \c result_type typedefs.
606 606
  /// Parameter \c F is the type of the used functor.
607 607
  ///
608 608
  /// The simplest way of using this map is through the functorToMap()
609 609
  /// function.
610 610
  ///
611 611
  /// \sa MapToFunctor
612 612
  template<typename F,
613 613
           typename K = typename F::argument_type,
614 614
           typename V = typename F::result_type>
615 615
  class FunctorToMap : public MapBase<K, V> {
616 616
    F _f;
617 617
  public:
618 618
    typedef MapBase<K, V> Parent;
619 619
    typedef typename Parent::Key Key;
620 620
    typedef typename Parent::Value Value;
621 621

	
622 622
    /// Constructor
623 623
    FunctorToMap(const F &f = F()) : _f(f) {}
624 624
    /// \e
625 625
    Value operator[](const Key &k) const { return _f(k); }
626 626
  };
627 627

	
628
  /// Returns a \ref FunctorToMap class
629

	
630
  /// This function just returns a \ref FunctorToMap class.
628
  /// Returns a \c FunctorToMap class
629

	
630
  /// This function just returns a \c FunctorToMap class.
631 631
  ///
632 632
  /// This function is specialized for adaptable binary function
633 633
  /// classes and C++ functions.
634 634
  ///
635 635
  /// \relates FunctorToMap
636 636
  template<typename K, typename V, typename F>
637 637
  inline FunctorToMap<F, K, V> functorToMap(const F &f) {
638 638
    return FunctorToMap<F, K, V>(f);
639 639
  }
640 640

	
641 641
  template <typename F>
642 642
  inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
643 643
    functorToMap(const F &f)
644 644
  {
645 645
    return FunctorToMap<F, typename F::argument_type,
646 646
      typename F::result_type>(f);
647 647
  }
648 648

	
649 649
  template <typename K, typename V>
650 650
  inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
651 651
    return FunctorToMap<V (*)(K), K, V>(f);
652 652
  }
653 653

	
654 654

	
655 655
  /// Converts a map to an STL style (unary) functor
656 656

	
657 657
  /// This class converts a map to an STL style (unary) functor.
658 658
  /// That is it provides an <tt>operator()</tt> to read its values.
659 659
  ///
660 660
  /// For the sake of convenience it also works as a usual
661 661
  /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
662 662
  /// and the \c Key and \c Value typedefs also exist.
663 663
  ///
664 664
  /// The simplest way of using this map is through the mapToFunctor()
665 665
  /// function.
666 666
  ///
667 667
  ///\sa FunctorToMap
668 668
  template <typename M>
669 669
  class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
670 670
    const M &_m;
671 671
  public:
672 672
    typedef MapBase<typename M::Key, typename M::Value> Parent;
673 673
    typedef typename Parent::Key Key;
674 674
    typedef typename Parent::Value Value;
675 675

	
676 676
    typedef typename Parent::Key argument_type;
677 677
    typedef typename Parent::Value result_type;
678 678

	
679 679
    /// Constructor
680 680
    MapToFunctor(const M &m) : _m(m) {}
681 681
    /// \e
682 682
    Value operator()(const Key &k) const { return _m[k]; }
683 683
    /// \e
684 684
    Value operator[](const Key &k) const { return _m[k]; }
685 685
  };
686 686

	
687
  /// Returns a \ref MapToFunctor class
688

	
689
  /// This function just returns a \ref MapToFunctor class.
687
  /// Returns a \c MapToFunctor class
688

	
689
  /// This function just returns a \c MapToFunctor class.
690 690
  /// \relates MapToFunctor
691 691
  template<typename M>
692 692
  inline MapToFunctor<M> mapToFunctor(const M &m) {
693 693
    return MapToFunctor<M>(m);
694 694
  }
695 695

	
696 696

	
697 697
  /// \brief Map adaptor to convert the \c Value type of a map to
698 698
  /// another type using the default conversion.
699 699

	
700 700
  /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
701 701
  /// "readable map" to another type using the default conversion.
702 702
  /// The \c Key type of it is inherited from \c M and the \c Value
703 703
  /// type is \c V.
704 704
  /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
705 705
  ///
706 706
  /// The simplest way of using this map is through the convertMap()
707 707
  /// function.
708 708
  template <typename M, typename V>
709 709
  class ConvertMap : public MapBase<typename M::Key, V> {
710 710
    const M &_m;
711 711
  public:
712 712
    typedef MapBase<typename M::Key, V> Parent;
713 713
    typedef typename Parent::Key Key;
714 714
    typedef typename Parent::Value Value;
715 715

	
716 716
    /// Constructor
717 717

	
718 718
    /// Constructor.
719 719
    /// \param m The underlying map.
720 720
    ConvertMap(const M &m) : _m(m) {}
721 721

	
722 722
    /// \e
723 723
    Value operator[](const Key &k) const { return _m[k]; }
724 724
  };
725 725

	
726
  /// Returns a \ref ConvertMap class
727

	
728
  /// This function just returns a \ref ConvertMap class.
726
  /// Returns a \c ConvertMap class
727

	
728
  /// This function just returns a \c ConvertMap class.
729 729
  /// \relates ConvertMap
730 730
  template<typename V, typename M>
731 731
  inline ConvertMap<M, V> convertMap(const M &map) {
732 732
    return ConvertMap<M, V>(map);
733 733
  }
734 734

	
735 735

	
736 736
  /// Applies all map setting operations to two maps
737 737

	
738 738
  /// This map has two \ref concepts::WriteMap "writable map" parameters
739 739
  /// and each write request will be passed to both of them.
740 740
  /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
741 741
  /// operations will return the corresponding values of \c M1.
742 742
  ///
743 743
  /// The \c Key and \c Value types are inherited from \c M1.
744 744
  /// The \c Key and \c Value of \c M2 must be convertible from those
745 745
  /// of \c M1.
746 746
  ///
747 747
  /// The simplest way of using this map is through the forkMap()
748 748
  /// function.
749 749
  template<typename  M1, typename M2>
750 750
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
751 751
    M1 &_m1;
752 752
    M2 &_m2;
753 753
  public:
754 754
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
755 755
    typedef typename Parent::Key Key;
756 756
    typedef typename Parent::Value Value;
757 757

	
758 758
    /// Constructor
759 759
    ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
760 760
    /// Returns the value associated with the given key in the first map.
761 761
    Value operator[](const Key &k) const { return _m1[k]; }
762 762
    /// Sets the value associated with the given key in both maps.
763 763
    void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
764 764
  };
765 765

	
766
  /// Returns a \ref ForkMap class
767

	
768
  /// This function just returns a \ref ForkMap class.
766
  /// Returns a \c ForkMap class
767

	
768
  /// This function just returns a \c ForkMap class.
769 769
  /// \relates ForkMap
770 770
  template <typename M1, typename M2>
771 771
  inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
772 772
    return ForkMap<M1,M2>(m1,m2);
773 773
  }
774 774

	
775 775

	
776 776
  /// Sum of two maps
777 777

	
778 778
  /// This \ref concepts::ReadMap "read-only map" returns the sum
779 779
  /// of the values of the two given maps.
780 780
  /// Its \c Key and \c Value types are inherited from \c M1.
781 781
  /// The \c Key and \c Value of \c M2 must be convertible to those of
782 782
  /// \c M1.
783 783
  ///
784 784
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
785 785
  /// \code
786 786
  ///   AddMap<M1,M2> am(m1,m2);
787 787
  /// \endcode
788 788
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
789 789
  ///
790 790
  /// The simplest way of using this map is through the addMap()
791 791
  /// function.
792 792
  ///
793 793
  /// \sa SubMap, MulMap, DivMap
794 794
  /// \sa ShiftMap, ShiftWriteMap
795 795
  template<typename M1, typename M2>
796 796
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
797 797
    const M1 &_m1;
798 798
    const M2 &_m2;
799 799
  public:
800 800
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
801 801
    typedef typename Parent::Key Key;
802 802
    typedef typename Parent::Value Value;
803 803

	
804 804
    /// Constructor
805 805
    AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
806 806
    /// \e
807 807
    Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
808 808
  };
809 809

	
810
  /// Returns an \ref AddMap class
811

	
812
  /// This function just returns an \ref AddMap class.
810
  /// Returns an \c AddMap class
811

	
812
  /// This function just returns an \c AddMap class.
813 813
  ///
814 814
  /// For example, if \c m1 and \c m2 are both maps with \c double
815 815
  /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
816 816
  /// <tt>m1[x]+m2[x]</tt>.
817 817
  ///
818 818
  /// \relates AddMap
819 819
  template<typename M1, typename M2>
820 820
  inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
821 821
    return AddMap<M1, M2>(m1,m2);
822 822
  }
823 823

	
824 824

	
825 825
  /// Difference of two maps
826 826

	
827 827
  /// This \ref concepts::ReadMap "read-only map" returns the difference
828 828
  /// of the values of the two given maps.
829 829
  /// Its \c Key and \c Value types are inherited from \c M1.
830 830
  /// The \c Key and \c Value of \c M2 must be convertible to those of
831 831
  /// \c M1.
832 832
  ///
833 833
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
834 834
  /// \code
835 835
  ///   SubMap<M1,M2> sm(m1,m2);
836 836
  /// \endcode
837 837
  /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
838 838
  ///
839 839
  /// The simplest way of using this map is through the subMap()
840 840
  /// function.
841 841
  ///
842 842
  /// \sa AddMap, MulMap, DivMap
843 843
  template<typename M1, typename M2>
844 844
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
845 845
    const M1 &_m1;
846 846
    const M2 &_m2;
847 847
  public:
848 848
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
849 849
    typedef typename Parent::Key Key;
850 850
    typedef typename Parent::Value Value;
851 851

	
852 852
    /// Constructor
853 853
    SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
854 854
    /// \e
855 855
    Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
856 856
  };
857 857

	
858
  /// Returns a \ref SubMap class
859

	
860
  /// This function just returns a \ref SubMap class.
858
  /// Returns a \c SubMap class
859

	
860
  /// This function just returns a \c SubMap class.
861 861
  ///
862 862
  /// For example, if \c m1 and \c m2 are both maps with \c double
863 863
  /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
864 864
  /// <tt>m1[x]-m2[x]</tt>.
865 865
  ///
866 866
  /// \relates SubMap
867 867
  template<typename M1, typename M2>
868 868
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
869 869
    return SubMap<M1, M2>(m1,m2);
870 870
  }
871 871

	
872 872

	
873 873
  /// Product of two maps
874 874

	
875 875
  /// This \ref concepts::ReadMap "read-only map" returns the product
876 876
  /// of the values of the two given maps.
877 877
  /// Its \c Key and \c Value types are inherited from \c M1.
878 878
  /// The \c Key and \c Value of \c M2 must be convertible to those of
879 879
  /// \c M1.
880 880
  ///
881 881
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
882 882
  /// \code
883 883
  ///   MulMap<M1,M2> mm(m1,m2);
884 884
  /// \endcode
885 885
  /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
886 886
  ///
887 887
  /// The simplest way of using this map is through the mulMap()
888 888
  /// function.
889 889
  ///
890 890
  /// \sa AddMap, SubMap, DivMap
891 891
  /// \sa ScaleMap, ScaleWriteMap
892 892
  template<typename M1, typename M2>
893 893
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
894 894
    const M1 &_m1;
895 895
    const M2 &_m2;
896 896
  public:
897 897
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
898 898
    typedef typename Parent::Key Key;
899 899
    typedef typename Parent::Value Value;
900 900

	
901 901
    /// Constructor
902 902
    MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
903 903
    /// \e
904 904
    Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
905 905
  };
906 906

	
907
  /// Returns a \ref MulMap class
908

	
909
  /// This function just returns a \ref MulMap class.
907
  /// Returns a \c MulMap class
908

	
909
  /// This function just returns a \c MulMap class.
910 910
  ///
911 911
  /// For example, if \c m1 and \c m2 are both maps with \c double
912 912
  /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
913 913
  /// <tt>m1[x]*m2[x]</tt>.
914 914
  ///
915 915
  /// \relates MulMap
916 916
  template<typename M1, typename M2>
917 917
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
918 918
    return MulMap<M1, M2>(m1,m2);
919 919
  }
920 920

	
921 921

	
922 922
  /// Quotient of two maps
923 923

	
924 924
  /// This \ref concepts::ReadMap "read-only map" returns the quotient
925 925
  /// of the values of the two given maps.
926 926
  /// Its \c Key and \c Value types are inherited from \c M1.
927 927
  /// The \c Key and \c Value of \c M2 must be convertible to those of
928 928
  /// \c M1.
929 929
  ///
930 930
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
931 931
  /// \code
932 932
  ///   DivMap<M1,M2> dm(m1,m2);
933 933
  /// \endcode
934 934
  /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
935 935
  ///
936 936
  /// The simplest way of using this map is through the divMap()
937 937
  /// function.
938 938
  ///
939 939
  /// \sa AddMap, SubMap, MulMap
940 940
  template<typename M1, typename M2>
941 941
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
942 942
    const M1 &_m1;
943 943
    const M2 &_m2;
944 944
  public:
945 945
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
946 946
    typedef typename Parent::Key Key;
947 947
    typedef typename Parent::Value Value;
948 948

	
949 949
    /// Constructor
950 950
    DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
951 951
    /// \e
952 952
    Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
953 953
  };
954 954

	
955
  /// Returns a \ref DivMap class
956

	
957
  /// This function just returns a \ref DivMap class.
955
  /// Returns a \c DivMap class
956

	
957
  /// This function just returns a \c DivMap class.
958 958
  ///
959 959
  /// For example, if \c m1 and \c m2 are both maps with \c double
960 960
  /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
961 961
  /// <tt>m1[x]/m2[x]</tt>.
962 962
  ///
963 963
  /// \relates DivMap
964 964
  template<typename M1, typename M2>
965 965
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
966 966
    return DivMap<M1, M2>(m1,m2);
967 967
  }
968 968

	
969 969

	
970 970
  /// Shifts a map with a constant.
971 971

	
972 972
  /// This \ref concepts::ReadMap "read-only map" returns the sum of
973 973
  /// the given map and a constant value (i.e. it shifts the map with
974 974
  /// the constant). Its \c Key and \c Value are inherited from \c M.
975 975
  ///
976 976
  /// Actually,
977 977
  /// \code
978 978
  ///   ShiftMap<M> sh(m,v);
979 979
  /// \endcode
980 980
  /// is equivalent to
981 981
  /// \code
982 982
  ///   ConstMap<M::Key, M::Value> cm(v);
983 983
  ///   AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
984 984
  /// \endcode
985 985
  ///
986 986
  /// The simplest way of using this map is through the shiftMap()
987 987
  /// function.
988 988
  ///
989 989
  /// \sa ShiftWriteMap
990 990
  template<typename M, typename C = typename M::Value>
991 991
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
992 992
    const M &_m;
993 993
    C _v;
994 994
  public:
995 995
    typedef MapBase<typename M::Key, typename M::Value> Parent;
996 996
    typedef typename Parent::Key Key;
997 997
    typedef typename Parent::Value Value;
998 998

	
999 999
    /// Constructor
1000 1000

	
1001 1001
    /// Constructor.
1002 1002
    /// \param m The undelying map.
1003 1003
    /// \param v The constant value.
1004 1004
    ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
1005 1005
    /// \e
1006 1006
    Value operator[](const Key &k) const { return _m[k]+_v; }
1007 1007
  };
1008 1008

	
1009 1009
  /// Shifts a map with a constant (read-write version).
1010 1010

	
1011 1011
  /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
1012 1012
  /// of the given map and a constant value (i.e. it shifts the map with
1013 1013
  /// the constant). Its \c Key and \c Value are inherited from \c M.
1014 1014
  /// It makes also possible to write the map.
1015 1015
  ///
1016 1016
  /// The simplest way of using this map is through the shiftWriteMap()
1017 1017
  /// function.
1018 1018
  ///
1019 1019
  /// \sa ShiftMap
1020 1020
  template<typename M, typename C = typename M::Value>
1021 1021
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
1022 1022
    M &_m;
1023 1023
    C _v;
1024 1024
  public:
1025 1025
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1026 1026
    typedef typename Parent::Key Key;
1027 1027
    typedef typename Parent::Value Value;
1028 1028

	
1029 1029
    /// Constructor
1030 1030

	
1031 1031
    /// Constructor.
1032 1032
    /// \param m The undelying map.
1033 1033
    /// \param v The constant value.
1034 1034
    ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1035 1035
    /// \e
1036 1036
    Value operator[](const Key &k) const { return _m[k]+_v; }
1037 1037
    /// \e
1038 1038
    void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
1039 1039
  };
1040 1040

	
1041
  /// Returns a \ref ShiftMap class
1042

	
1043
  /// This function just returns a \ref ShiftMap class.
1041
  /// Returns a \c ShiftMap class
1042

	
1043
  /// This function just returns a \c ShiftMap class.
1044 1044
  ///
1045 1045
  /// For example, if \c m is a map with \c double values and \c v is
1046 1046
  /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
1047 1047
  /// <tt>m[x]+v</tt>.
1048 1048
  ///
1049 1049
  /// \relates ShiftMap
1050 1050
  template<typename M, typename C>
1051 1051
  inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
1052 1052
    return ShiftMap<M, C>(m,v);
1053 1053
  }
1054 1054

	
1055
  /// Returns a \ref ShiftWriteMap class
1056

	
1057
  /// This function just returns a \ref ShiftWriteMap class.
1055
  /// Returns a \c ShiftWriteMap class
1056

	
1057
  /// This function just returns a \c ShiftWriteMap class.
1058 1058
  ///
1059 1059
  /// For example, if \c m is a map with \c double values and \c v is
1060 1060
  /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
1061 1061
  /// <tt>m[x]+v</tt>.
1062 1062
  /// Moreover it makes also possible to write the map.
1063 1063
  ///
1064 1064
  /// \relates ShiftWriteMap
1065 1065
  template<typename M, typename C>
1066 1066
  inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
1067 1067
    return ShiftWriteMap<M, C>(m,v);
1068 1068
  }
1069 1069

	
1070 1070

	
1071 1071
  /// Scales a map with a constant.
1072 1072

	
1073 1073
  /// This \ref concepts::ReadMap "read-only map" returns the value of
1074 1074
  /// the given map multiplied from the left side with a constant value.
1075 1075
  /// Its \c Key and \c Value are inherited from \c M.
1076 1076
  ///
1077 1077
  /// Actually,
1078 1078
  /// \code
1079 1079
  ///   ScaleMap<M> sc(m,v);
1080 1080
  /// \endcode
1081 1081
  /// is equivalent to
1082 1082
  /// \code
1083 1083
  ///   ConstMap<M::Key, M::Value> cm(v);
1084 1084
  ///   MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
1085 1085
  /// \endcode
1086 1086
  ///
1087 1087
  /// The simplest way of using this map is through the scaleMap()
1088 1088
  /// function.
1089 1089
  ///
1090 1090
  /// \sa ScaleWriteMap
1091 1091
  template<typename M, typename C = typename M::Value>
1092 1092
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
1093 1093
    const M &_m;
1094 1094
    C _v;
1095 1095
  public:
1096 1096
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1097 1097
    typedef typename Parent::Key Key;
1098 1098
    typedef typename Parent::Value Value;
1099 1099

	
1100 1100
    /// Constructor
1101 1101

	
1102 1102
    /// Constructor.
1103 1103
    /// \param m The undelying map.
1104 1104
    /// \param v The constant value.
1105 1105
    ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
1106 1106
    /// \e
1107 1107
    Value operator[](const Key &k) const { return _v*_m[k]; }
1108 1108
  };
1109 1109

	
1110 1110
  /// Scales a map with a constant (read-write version).
1111 1111

	
1112 1112
  /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
1113 1113
  /// the given map multiplied from the left side with a constant value.
1114 1114
  /// Its \c Key and \c Value are inherited from \c M.
1115 1115
  /// It can also be used as write map if the \c / operator is defined
1116 1116
  /// between \c Value and \c C and the given multiplier is not zero.
1117 1117
  ///
1118 1118
  /// The simplest way of using this map is through the scaleWriteMap()
1119 1119
  /// function.
1120 1120
  ///
1121 1121
  /// \sa ScaleMap
1122 1122
  template<typename M, typename C = typename M::Value>
1123 1123
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
1124 1124
    M &_m;
1125 1125
    C _v;
1126 1126
  public:
1127 1127
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1128 1128
    typedef typename Parent::Key Key;
1129 1129
    typedef typename Parent::Value Value;
1130 1130

	
1131 1131
    /// Constructor
1132 1132

	
1133 1133
    /// Constructor.
1134 1134
    /// \param m The undelying map.
1135 1135
    /// \param v The constant value.
1136 1136
    ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1137 1137
    /// \e
1138 1138
    Value operator[](const Key &k) const { return _v*_m[k]; }
1139 1139
    /// \e
1140 1140
    void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
1141 1141
  };
1142 1142

	
1143
  /// Returns a \ref ScaleMap class
1144

	
1145
  /// This function just returns a \ref ScaleMap class.
1143
  /// Returns a \c ScaleMap class
1144

	
1145
  /// This function just returns a \c ScaleMap class.
1146 1146
  ///
1147 1147
  /// For example, if \c m is a map with \c double values and \c v is
1148 1148
  /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
1149 1149
  /// <tt>v*m[x]</tt>.
1150 1150
  ///
1151 1151
  /// \relates ScaleMap
1152 1152
  template<typename M, typename C>
1153 1153
  inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
1154 1154
    return ScaleMap<M, C>(m,v);
1155 1155
  }
1156 1156

	
1157
  /// Returns a \ref ScaleWriteMap class
1158

	
1159
  /// This function just returns a \ref ScaleWriteMap class.
1157
  /// Returns a \c ScaleWriteMap class
1158

	
1159
  /// This function just returns a \c ScaleWriteMap class.
1160 1160
  ///
1161 1161
  /// For example, if \c m is a map with \c double values and \c v is
1162 1162
  /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
1163 1163
  /// <tt>v*m[x]</tt>.
1164 1164
  /// Moreover it makes also possible to write the map.
1165 1165
  ///
1166 1166
  /// \relates ScaleWriteMap
1167 1167
  template<typename M, typename C>
1168 1168
  inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
1169 1169
    return ScaleWriteMap<M, C>(m,v);
1170 1170
  }
1171 1171

	
1172 1172

	
1173 1173
  /// Negative of a map
1174 1174

	
1175 1175
  /// This \ref concepts::ReadMap "read-only map" returns the negative
1176 1176
  /// of the values of the given map (using the unary \c - operator).
1177 1177
  /// Its \c Key and \c Value are inherited from \c M.
1178 1178
  ///
1179 1179
  /// If M::Value is \c int, \c double etc., then
1180 1180
  /// \code
1181 1181
  ///   NegMap<M> neg(m);
1182 1182
  /// \endcode
1183 1183
  /// is equivalent to
1184 1184
  /// \code
1185 1185
  ///   ScaleMap<M> neg(m,-1);
1186 1186
  /// \endcode
1187 1187
  ///
1188 1188
  /// The simplest way of using this map is through the negMap()
1189 1189
  /// function.
1190 1190
  ///
1191 1191
  /// \sa NegWriteMap
1192 1192
  template<typename M>
1193 1193
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
1194 1194
    const M& _m;
1195 1195
  public:
1196 1196
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1197 1197
    typedef typename Parent::Key Key;
1198 1198
    typedef typename Parent::Value Value;
1199 1199

	
1200 1200
    /// Constructor
1201 1201
    NegMap(const M &m) : _m(m) {}
1202 1202
    /// \e
1203 1203
    Value operator[](const Key &k) const { return -_m[k]; }
1204 1204
  };
1205 1205

	
1206 1206
  /// Negative of a map (read-write version)
1207 1207

	
1208 1208
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
1209 1209
  /// negative of the values of the given map (using the unary \c -
1210 1210
  /// operator).
1211 1211
  /// Its \c Key and \c Value are inherited from \c M.
1212 1212
  /// It makes also possible to write the map.
1213 1213
  ///
1214 1214
  /// If M::Value is \c int, \c double etc., then
1215 1215
  /// \code
1216 1216
  ///   NegWriteMap<M> neg(m);
1217 1217
  /// \endcode
1218 1218
  /// is equivalent to
1219 1219
  /// \code
1220 1220
  ///   ScaleWriteMap<M> neg(m,-1);
1221 1221
  /// \endcode
1222 1222
  ///
1223 1223
  /// The simplest way of using this map is through the negWriteMap()
1224 1224
  /// function.
1225 1225
  ///
1226 1226
  /// \sa NegMap
1227 1227
  template<typename M>
1228 1228
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
1229 1229
    M &_m;
1230 1230
  public:
1231 1231
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1232 1232
    typedef typename Parent::Key Key;
1233 1233
    typedef typename Parent::Value Value;
1234 1234

	
1235 1235
    /// Constructor
1236 1236
    NegWriteMap(M &m) : _m(m) {}
1237 1237
    /// \e
1238 1238
    Value operator[](const Key &k) const { return -_m[k]; }
1239 1239
    /// \e
1240 1240
    void set(const Key &k, const Value &v) { _m.set(k, -v); }
1241 1241
  };
1242 1242

	
1243
  /// Returns a \ref NegMap class
1244

	
1245
  /// This function just returns a \ref NegMap class.
1243
  /// Returns a \c NegMap class
1244

	
1245
  /// This function just returns a \c NegMap class.
1246 1246
  ///
1247 1247
  /// For example, if \c m is a map with \c double values, then
1248 1248
  /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1249 1249
  ///
1250 1250
  /// \relates NegMap
1251 1251
  template <typename M>
1252 1252
  inline NegMap<M> negMap(const M &m) {
1253 1253
    return NegMap<M>(m);
1254 1254
  }
1255 1255

	
1256
  /// Returns a \ref NegWriteMap class
1257

	
1258
  /// This function just returns a \ref NegWriteMap class.
1256
  /// Returns a \c NegWriteMap class
1257

	
1258
  /// This function just returns a \c NegWriteMap class.
1259 1259
  ///
1260 1260
  /// For example, if \c m is a map with \c double values, then
1261 1261
  /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1262 1262
  /// Moreover it makes also possible to write the map.
1263 1263
  ///
1264 1264
  /// \relates NegWriteMap
1265 1265
  template <typename M>
1266 1266
  inline NegWriteMap<M> negWriteMap(M &m) {
1267 1267
    return NegWriteMap<M>(m);
1268 1268
  }
1269 1269

	
1270 1270

	
1271 1271
  /// Absolute value of a map
1272 1272

	
1273 1273
  /// This \ref concepts::ReadMap "read-only map" returns the absolute
1274 1274
  /// value of the values of the given map.
1275 1275
  /// Its \c Key and \c Value are inherited from \c M.
1276 1276
  /// \c Value must be comparable to \c 0 and the unary \c -
1277 1277
  /// operator must be defined for it, of course.
1278 1278
  ///
1279 1279
  /// The simplest way of using this map is through the absMap()
1280 1280
  /// function.
1281 1281
  template<typename M>
1282 1282
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
1283 1283
    const M &_m;
1284 1284
  public:
1285 1285
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1286 1286
    typedef typename Parent::Key Key;
1287 1287
    typedef typename Parent::Value Value;
1288 1288

	
1289 1289
    /// Constructor
1290 1290
    AbsMap(const M &m) : _m(m) {}
1291 1291
    /// \e
1292 1292
    Value operator[](const Key &k) const {
1293 1293
      Value tmp = _m[k];
1294 1294
      return tmp >= 0 ? tmp : -tmp;
1295 1295
    }
1296 1296

	
1297 1297
  };
1298 1298

	
1299
  /// Returns an \ref AbsMap class
1300

	
1301
  /// This function just returns an \ref AbsMap class.
1299
  /// Returns an \c AbsMap class
1300

	
1301
  /// This function just returns an \c AbsMap class.
1302 1302
  ///
1303 1303
  /// For example, if \c m is a map with \c double values, then
1304 1304
  /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
1305 1305
  /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
1306 1306
  /// negative.
1307 1307
  ///
1308 1308
  /// \relates AbsMap
1309 1309
  template<typename M>
1310 1310
  inline AbsMap<M> absMap(const M &m) {
1311 1311
    return AbsMap<M>(m);
1312 1312
  }
1313 1313

	
1314 1314
  /// @}
1315 1315

	
1316 1316
  // Logical maps and map adaptors:
1317 1317

	
1318 1318
  /// \addtogroup maps
1319 1319
  /// @{
1320 1320

	
1321 1321
  /// Constant \c true map.
1322 1322

	
1323 1323
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1324 1324
  /// each key.
1325 1325
  ///
1326 1326
  /// Note that
1327 1327
  /// \code
1328 1328
  ///   TrueMap<K> tm;
1329 1329
  /// \endcode
1330 1330
  /// is equivalent to
1331 1331
  /// \code
1332 1332
  ///   ConstMap<K,bool> tm(true);
1333 1333
  /// \endcode
1334 1334
  ///
1335 1335
  /// \sa FalseMap
1336 1336
  /// \sa ConstMap
1337 1337
  template <typename K>
1338 1338
  class TrueMap : public MapBase<K, bool> {
1339 1339
  public:
1340 1340
    typedef MapBase<K, bool> Parent;
1341 1341
    typedef typename Parent::Key Key;
1342 1342
    typedef typename Parent::Value Value;
1343 1343

	
1344 1344
    /// Gives back \c true.
1345 1345
    Value operator[](const Key&) const { return true; }
1346 1346
  };
1347 1347

	
1348
  /// Returns a \ref TrueMap class
1349

	
1350
  /// This function just returns a \ref TrueMap class.
1348
  /// Returns a \c TrueMap class
1349

	
1350
  /// This function just returns a \c TrueMap class.
1351 1351
  /// \relates TrueMap
1352 1352
  template<typename K>
1353 1353
  inline TrueMap<K> trueMap() {
1354 1354
    return TrueMap<K>();
1355 1355
  }
1356 1356

	
1357 1357

	
1358 1358
  /// Constant \c false map.
1359 1359

	
1360 1360
  /// This \ref concepts::ReadMap "read-only map" assigns \c false to
1361 1361
  /// each key.
1362 1362
  ///
1363 1363
  /// Note that
1364 1364
  /// \code
1365 1365
  ///   FalseMap<K> fm;
1366 1366
  /// \endcode
1367 1367
  /// is equivalent to
1368 1368
  /// \code
1369 1369
  ///   ConstMap<K,bool> fm(false);
1370 1370
  /// \endcode
1371 1371
  ///
1372 1372
  /// \sa TrueMap
1373 1373
  /// \sa ConstMap
1374 1374
  template <typename K>
1375 1375
  class FalseMap : public MapBase<K, bool> {
1376 1376
  public:
1377 1377
    typedef MapBase<K, bool> Parent;
1378 1378
    typedef typename Parent::Key Key;
1379 1379
    typedef typename Parent::Value Value;
1380 1380

	
1381 1381
    /// Gives back \c false.
1382 1382
    Value operator[](const Key&) const { return false; }
1383 1383
  };
1384 1384

	
1385
  /// Returns a \ref FalseMap class
1386

	
1387
  /// This function just returns a \ref FalseMap class.
1385
  /// Returns a \c FalseMap class
1386

	
1387
  /// This function just returns a \c FalseMap class.
1388 1388
  /// \relates FalseMap
1389 1389
  template<typename K>
1390 1390
  inline FalseMap<K> falseMap() {
1391 1391
    return FalseMap<K>();
1392 1392
  }
1393 1393

	
1394 1394
  /// @}
1395 1395

	
1396 1396
  /// \addtogroup map_adaptors
1397 1397
  /// @{
1398 1398

	
1399 1399
  /// Logical 'and' of two maps
1400 1400

	
1401 1401
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1402 1402
  /// 'and' of the values of the two given maps.
1403 1403
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1404 1404
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1405 1405
  ///
1406 1406
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1407 1407
  /// \code
1408 1408
  ///   AndMap<M1,M2> am(m1,m2);
1409 1409
  /// \endcode
1410 1410
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
1411 1411
  ///
1412 1412
  /// The simplest way of using this map is through the andMap()
1413 1413
  /// function.
1414 1414
  ///
1415 1415
  /// \sa OrMap
1416 1416
  /// \sa NotMap, NotWriteMap
1417 1417
  template<typename M1, typename M2>
1418 1418
  class AndMap : public MapBase<typename M1::Key, bool> {
1419 1419
    const M1 &_m1;
1420 1420
    const M2 &_m2;
1421 1421
  public:
1422 1422
    typedef MapBase<typename M1::Key, bool> Parent;
1423 1423
    typedef typename Parent::Key Key;
1424 1424
    typedef typename Parent::Value Value;
1425 1425

	
1426 1426
    /// Constructor
1427 1427
    AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1428 1428
    /// \e
1429 1429
    Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
1430 1430
  };
1431 1431

	
1432
  /// Returns an \ref AndMap class
1433

	
1434
  /// This function just returns an \ref AndMap class.
1432
  /// Returns an \c AndMap class
1433

	
1434
  /// This function just returns an \c AndMap class.
1435 1435
  ///
1436 1436
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1437 1437
  /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
1438 1438
  /// <tt>m1[x]&&m2[x]</tt>.
1439 1439
  ///
1440 1440
  /// \relates AndMap
1441 1441
  template<typename M1, typename M2>
1442 1442
  inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
1443 1443
    return AndMap<M1, M2>(m1,m2);
1444 1444
  }
1445 1445

	
1446 1446

	
1447 1447
  /// Logical 'or' of two maps
1448 1448

	
1449 1449
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1450 1450
  /// 'or' of the values of the two given maps.
1451 1451
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1452 1452
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1453 1453
  ///
1454 1454
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1455 1455
  /// \code
1456 1456
  ///   OrMap<M1,M2> om(m1,m2);
1457 1457
  /// \endcode
1458 1458
  /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
1459 1459
  ///
1460 1460
  /// The simplest way of using this map is through the orMap()
1461 1461
  /// function.
1462 1462
  ///
1463 1463
  /// \sa AndMap
1464 1464
  /// \sa NotMap, NotWriteMap
1465 1465
  template<typename M1, typename M2>
1466 1466
  class OrMap : public MapBase<typename M1::Key, bool> {
1467 1467
    const M1 &_m1;
1468 1468
    const M2 &_m2;
1469 1469
  public:
1470 1470
    typedef MapBase<typename M1::Key, bool> Parent;
1471 1471
    typedef typename Parent::Key Key;
1472 1472
    typedef typename Parent::Value Value;
1473 1473

	
1474 1474
    /// Constructor
1475 1475
    OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1476 1476
    /// \e
1477 1477
    Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1478 1478
  };
1479 1479

	
1480
  /// Returns an \ref OrMap class
1481

	
1482
  /// This function just returns an \ref OrMap class.
1480
  /// Returns an \c OrMap class
1481

	
1482
  /// This function just returns an \c OrMap class.
1483 1483
  ///
1484 1484
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1485 1485
  /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
1486 1486
  /// <tt>m1[x]||m2[x]</tt>.
1487 1487
  ///
1488 1488
  /// \relates OrMap
1489 1489
  template<typename M1, typename M2>
1490 1490
  inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
1491 1491
    return OrMap<M1, M2>(m1,m2);
1492 1492
  }
1493 1493

	
1494 1494

	
1495 1495
  /// Logical 'not' of a map
1496 1496

	
1497 1497
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1498 1498
  /// negation of the values of the given map.
1499 1499
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1500 1500
  ///
1501 1501
  /// The simplest way of using this map is through the notMap()
1502 1502
  /// function.
1503 1503
  ///
1504 1504
  /// \sa NotWriteMap
1505 1505
  template <typename M>
1506 1506
  class NotMap : public MapBase<typename M::Key, bool> {
1507 1507
    const M &_m;
1508 1508
  public:
1509 1509
    typedef MapBase<typename M::Key, bool> Parent;
1510 1510
    typedef typename Parent::Key Key;
1511 1511
    typedef typename Parent::Value Value;
1512 1512

	
1513 1513
    /// Constructor
1514 1514
    NotMap(const M &m) : _m(m) {}
1515 1515
    /// \e
1516 1516
    Value operator[](const Key &k) const { return !_m[k]; }
1517 1517
  };
1518 1518

	
1519 1519
  /// Logical 'not' of a map (read-write version)
1520 1520

	
1521 1521
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
1522 1522
  /// logical negation of the values of the given map.
1523 1523
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1524 1524
  /// It makes also possible to write the map. When a value is set,
1525 1525
  /// the opposite value is set to the original map.
1526 1526
  ///
1527 1527
  /// The simplest way of using this map is through the notWriteMap()
1528 1528
  /// function.
1529 1529
  ///
1530 1530
  /// \sa NotMap
1531 1531
  template <typename M>
1532 1532
  class NotWriteMap : public MapBase<typename M::Key, bool> {
1533 1533
    M &_m;
1534 1534
  public:
1535 1535
    typedef MapBase<typename M::Key, bool> Parent;
1536 1536
    typedef typename Parent::Key Key;
1537 1537
    typedef typename Parent::Value Value;
1538 1538

	
1539 1539
    /// Constructor
1540 1540
    NotWriteMap(M &m) : _m(m) {}
1541 1541
    /// \e
1542 1542
    Value operator[](const Key &k) const { return !_m[k]; }
1543 1543
    /// \e
1544 1544
    void set(const Key &k, bool v) { _m.set(k, !v); }
1545 1545
  };
1546 1546

	
1547
  /// Returns a \ref NotMap class
1548

	
1549
  /// This function just returns a \ref NotMap class.
1547
  /// Returns a \c NotMap class
1548

	
1549
  /// This function just returns a \c NotMap class.
1550 1550
  ///
1551 1551
  /// For example, if \c m is a map with \c bool values, then
1552 1552
  /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1553 1553
  ///
1554 1554
  /// \relates NotMap
1555 1555
  template <typename M>
1556 1556
  inline NotMap<M> notMap(const M &m) {
1557 1557
    return NotMap<M>(m);
1558 1558
  }
1559 1559

	
1560
  /// Returns a \ref NotWriteMap class
1561

	
1562
  /// This function just returns a \ref NotWriteMap class.
1560
  /// Returns a \c NotWriteMap class
1561

	
1562
  /// This function just returns a \c NotWriteMap class.
1563 1563
  ///
1564 1564
  /// For example, if \c m is a map with \c bool values, then
1565 1565
  /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1566 1566
  /// Moreover it makes also possible to write the map.
1567 1567
  ///
1568 1568
  /// \relates NotWriteMap
1569 1569
  template <typename M>
1570 1570
  inline NotWriteMap<M> notWriteMap(M &m) {
1571 1571
    return NotWriteMap<M>(m);
1572 1572
  }
1573 1573

	
1574 1574

	
1575 1575
  /// Combination of two maps using the \c == operator
1576 1576

	
1577 1577
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1578 1578
  /// the keys for which the corresponding values of the two maps are
1579 1579
  /// equal.
1580 1580
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1581 1581
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1582 1582
  ///
1583 1583
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1584 1584
  /// \code
1585 1585
  ///   EqualMap<M1,M2> em(m1,m2);
1586 1586
  /// \endcode
1587 1587
  /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1588 1588
  ///
1589 1589
  /// The simplest way of using this map is through the equalMap()
1590 1590
  /// function.
1591 1591
  ///
1592 1592
  /// \sa LessMap
1593 1593
  template<typename M1, typename M2>
1594 1594
  class EqualMap : public MapBase<typename M1::Key, bool> {
1595 1595
    const M1 &_m1;
1596 1596
    const M2 &_m2;
1597 1597
  public:
1598 1598
    typedef MapBase<typename M1::Key, bool> Parent;
1599 1599
    typedef typename Parent::Key Key;
1600 1600
    typedef typename Parent::Value Value;
1601 1601

	
1602 1602
    /// Constructor
1603 1603
    EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1604 1604
    /// \e
1605 1605
    Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1606 1606
  };
1607 1607

	
1608
  /// Returns an \ref EqualMap class
1609

	
1610
  /// This function just returns an \ref EqualMap class.
1608
  /// Returns an \c EqualMap class
1609

	
1610
  /// This function just returns an \c EqualMap class.
1611 1611
  ///
1612 1612
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1613 1613
  /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1614 1614
  /// <tt>m1[x]==m2[x]</tt>.
1615 1615
  ///
1616 1616
  /// \relates EqualMap
1617 1617
  template<typename M1, typename M2>
1618 1618
  inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1619 1619
    return EqualMap<M1, M2>(m1,m2);
1620 1620
  }
1621 1621

	
1622 1622

	
1623 1623
  /// Combination of two maps using the \c < operator
1624 1624

	
1625 1625
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1626 1626
  /// the keys for which the corresponding value of the first map is
1627 1627
  /// less then the value of the second map.
1628 1628
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1629 1629
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1630 1630
  ///
1631 1631
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1632 1632
  /// \code
1633 1633
  ///   LessMap<M1,M2> lm(m1,m2);
1634 1634
  /// \endcode
1635 1635
  /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1636 1636
  ///
1637 1637
  /// The simplest way of using this map is through the lessMap()
1638 1638
  /// function.
1639 1639
  ///
1640 1640
  /// \sa EqualMap
1641 1641
  template<typename M1, typename M2>
1642 1642
  class LessMap : public MapBase<typename M1::Key, bool> {
1643 1643
    const M1 &_m1;
1644 1644
    const M2 &_m2;
1645 1645
  public:
1646 1646
    typedef MapBase<typename M1::Key, bool> Parent;
1647 1647
    typedef typename Parent::Key Key;
1648 1648
    typedef typename Parent::Value Value;
1649 1649

	
1650 1650
    /// Constructor
1651 1651
    LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1652 1652
    /// \e
1653 1653
    Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1654 1654
  };
1655 1655

	
1656
  /// Returns an \ref LessMap class
1657

	
1658
  /// This function just returns an \ref LessMap class.
1656
  /// Returns an \c LessMap class
1657

	
1658
  /// This function just returns an \c LessMap class.
1659 1659
  ///
1660 1660
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1661 1661
  /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1662 1662
  /// <tt>m1[x]<m2[x]</tt>.
1663 1663
  ///
1664 1664
  /// \relates LessMap
1665 1665
  template<typename M1, typename M2>
1666 1666
  inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1667 1667
    return LessMap<M1, M2>(m1,m2);
1668 1668
  }
1669 1669

	
1670 1670
  namespace _maps_bits {
1671 1671

	
1672 1672
    template <typename _Iterator, typename Enable = void>
1673 1673
    struct IteratorTraits {
1674 1674
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
1675 1675
    };
1676 1676

	
1677 1677
    template <typename _Iterator>
1678 1678
    struct IteratorTraits<_Iterator,
1679 1679
      typename exists<typename _Iterator::container_type>::type>
1680 1680
    {
1681 1681
      typedef typename _Iterator::container_type::value_type Value;
1682 1682
    };
1683 1683

	
1684 1684
  }
1685 1685

	
1686 1686
  /// \brief Writable bool map for logging each \c true assigned element
1687 1687
  ///
1688 1688
  /// A \ref concepts::WriteMap "writable" bool map for logging
1689 1689
  /// each \c true assigned element, i.e it copies subsequently each
1690 1690
  /// keys set to \c true to the given iterator.
1691 1691
  /// The most important usage of it is storing certain nodes or arcs
1692 1692
  /// that were marked \c true by an algorithm.
1693 1693
  ///
1694 1694
  /// There are several algorithms that provide solutions through bool
1695 1695
  /// maps and most of them assign \c true at most once for each key.
1696 1696
  /// In these cases it is a natural request to store each \c true
1697 1697
  /// assigned elements (in order of the assignment), which can be
1698 1698
  /// easily done with LoggerBoolMap.
1699 1699
  ///
1700 1700
  /// The simplest way of using this map is through the loggerBoolMap()
1701 1701
  /// function.
1702 1702
  ///
1703 1703
  /// \tparam It The type of the iterator.
1704 1704
  /// \tparam Ke The key type of the map. The default value set
1705 1705
  /// according to the iterator type should work in most cases.
1706 1706
  ///
1707 1707
  /// \note The container of the iterator must contain enough space
1708 1708
  /// for the elements or the iterator should be an inserter iterator.
1709 1709
#ifdef DOXYGEN
1710 1710
  template <typename It, typename Ke>
1711 1711
#else
1712 1712
  template <typename It,
1713 1713
            typename Ke=typename _maps_bits::IteratorTraits<It>::Value>
1714 1714
#endif
1715 1715
  class LoggerBoolMap {
1716 1716
  public:
1717 1717
    typedef It Iterator;
1718 1718

	
1719 1719
    typedef Ke Key;
1720 1720
    typedef bool Value;
1721 1721

	
1722 1722
    /// Constructor
1723 1723
    LoggerBoolMap(Iterator it)
1724 1724
      : _begin(it), _end(it) {}
1725 1725

	
1726 1726
    /// Gives back the given iterator set for the first key
1727 1727
    Iterator begin() const {
1728 1728
      return _begin;
1729 1729
    }
1730 1730

	
1731 1731
    /// Gives back the the 'after the last' iterator
1732 1732
    Iterator end() const {
1733 1733
      return _end;
1734 1734
    }
1735 1735

	
1736 1736
    /// The set function of the map
1737 1737
    void set(const Key& key, Value value) {
1738 1738
      if (value) {
1739 1739
        *_end++ = key;
1740 1740
      }
1741 1741
    }
1742 1742

	
1743 1743
  private:
1744 1744
    Iterator _begin;
1745 1745
    Iterator _end;
1746 1746
  };
1747 1747

	
1748
  /// Returns a \ref LoggerBoolMap class
1749

	
1750
  /// This function just returns a \ref LoggerBoolMap class.
1748
  /// Returns a \c LoggerBoolMap class
1749

	
1750
  /// This function just returns a \c LoggerBoolMap class.
1751 1751
  ///
1752 1752
  /// The most important usage of it is storing certain nodes or arcs
1753 1753
  /// that were marked \c true by an algorithm.
1754 1754
  /// For example it makes easier to store the nodes in the processing
1755 1755
  /// order of Dfs algorithm, as the following examples show.
1756 1756
  /// \code
1757 1757
  ///   std::vector<Node> v;
1758 1758
  ///   dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1759 1759
  /// \endcode
1760 1760
  /// \code
1761 1761
  ///   std::vector<Node> v(countNodes(g));
1762 1762
  ///   dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1763 1763
  /// \endcode
1764 1764
  ///
1765 1765
  /// \note The container of the iterator must contain enough space
1766 1766
  /// for the elements or the iterator should be an inserter iterator.
1767 1767
  ///
1768 1768
  /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1769 1769
  /// it cannot be used when a readable map is needed, for example as
1770
  /// \c ReachedMap for \ref Bfs, \ref Dfs and \ref Dijkstra algorithms.
1770
  /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1771 1771
  ///
1772 1772
  /// \relates LoggerBoolMap
1773 1773
  template<typename Iterator>
1774 1774
  inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
1775 1775
    return LoggerBoolMap<Iterator>(it);
1776 1776
  }
1777 1777

	
1778 1778
  /// Provides an immutable and unique id for each item in the graph.
1779 1779

	
1780 1780
  /// The IdMap class provides a unique and immutable id for each item of the
1781 1781
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
1782 1782
  /// different items (nodes) get different ids <li>\b immutable: the id of an
1783 1783
  /// item (node) does not change (even if you delete other nodes).  </ul>
1784 1784
  /// Through this map you get access (i.e. can read) the inner id values of
1785 1785
  /// the items stored in the graph. This map can be inverted with its member
1786 1786
  /// class \c InverseMap or with the \c operator() member.
1787 1787
  ///
1788 1788
  template <typename _Graph, typename _Item>
1789 1789
  class IdMap {
1790 1790
  public:
1791 1791
    typedef _Graph Graph;
1792 1792
    typedef int Value;
1793 1793
    typedef _Item Item;
1794 1794
    typedef _Item Key;
1795 1795

	
1796 1796
    /// \brief Constructor.
1797 1797
    ///
1798 1798
    /// Constructor of the map.
1799 1799
    explicit IdMap(const Graph& graph) : _graph(&graph) {}
1800 1800

	
1801 1801
    /// \brief Gives back the \e id of the item.
1802 1802
    ///
1803 1803
    /// Gives back the immutable and unique \e id of the item.
1804 1804
    int operator[](const Item& item) const { return _graph->id(item);}
1805 1805

	
1806 1806
    /// \brief Gives back the item by its id.
1807 1807
    ///
1808 1808
    /// Gives back the item by its id.
1809 1809
    Item operator()(int id) { return _graph->fromId(id, Item()); }
1810 1810

	
1811 1811
  private:
1812 1812
    const Graph* _graph;
1813 1813

	
1814 1814
  public:
1815 1815

	
1816 1816
    /// \brief The class represents the inverse of its owner (IdMap).
1817 1817
    ///
1818 1818
    /// The class represents the inverse of its owner (IdMap).
1819 1819
    /// \see inverse()
1820 1820
    class InverseMap {
1821 1821
    public:
1822 1822

	
1823 1823
      /// \brief Constructor.
1824 1824
      ///
1825 1825
      /// Constructor for creating an id-to-item map.
1826 1826
      explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1827 1827

	
1828 1828
      /// \brief Constructor.
1829 1829
      ///
1830 1830
      /// Constructor for creating an id-to-item map.
1831 1831
      explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1832 1832

	
1833 1833
      /// \brief Gives back the given item from its id.
1834 1834
      ///
1835 1835
      /// Gives back the given item from its id.
1836 1836
      ///
1837 1837
      Item operator[](int id) const { return _graph->fromId(id, Item());}
1838 1838

	
1839 1839
    private:
1840 1840
      const Graph* _graph;
1841 1841
    };
1842 1842

	
1843 1843
    /// \brief Gives back the inverse of the map.
1844 1844
    ///
1845 1845
    /// Gives back the inverse of the IdMap.
1846 1846
    InverseMap inverse() const { return InverseMap(*_graph);}
1847 1847

	
1848 1848
  };
1849 1849

	
1850 1850

	
1851 1851
  /// \brief General invertable graph-map type.
1852 1852

	
1853 1853
  /// This type provides simple invertable graph-maps.
1854 1854
  /// The InvertableMap wraps an arbitrary ReadWriteMap
1855 1855
  /// and if a key is set to a new value then store it
1856 1856
  /// in the inverse map.
1857 1857
  ///
1858 1858
  /// The values of the map can be accessed
1859 1859
  /// with stl compatible forward iterator.
1860 1860
  ///
1861 1861
  /// \tparam _Graph The graph type.
1862 1862
  /// \tparam _Item The item type of the graph.
1863 1863
  /// \tparam _Value The value type of the map.
1864 1864
  ///
1865 1865
  /// \see IterableValueMap
1866 1866
  template <typename _Graph, typename _Item, typename _Value>
1867 1867
  class InvertableMap
1868 1868
    : protected ItemSetTraits<_Graph, _Item>::template Map<_Value>::Type {
1869 1869
  private:
1870 1870

	
1871 1871
    typedef typename ItemSetTraits<_Graph, _Item>::
1872 1872
    template Map<_Value>::Type Map;
1873 1873
    typedef _Graph Graph;
1874 1874

	
1875 1875
    typedef std::map<_Value, _Item> Container;
1876 1876
    Container _inv_map;
1877 1877

	
1878 1878
  public:
1879 1879

	
1880 1880
    /// The key type of InvertableMap (Node, Arc, Edge).
1881 1881
    typedef typename Map::Key Key;
1882 1882
    /// The value type of the InvertableMap.
1883 1883
    typedef typename Map::Value Value;
1884 1884

	
1885 1885

	
1886 1886

	
1887 1887
    /// \brief Constructor.
1888 1888
    ///
1889 1889
    /// Construct a new InvertableMap for the graph.
1890 1890
    ///
1891 1891
    explicit InvertableMap(const Graph& graph) : Map(graph) {}
1892 1892

	
1893 1893
    /// \brief Forward iterator for values.
1894 1894
    ///
1895 1895
    /// This iterator is an stl compatible forward
1896 1896
    /// iterator on the values of the map. The values can
1897 1897
    /// be accessed in the [beginValue, endValue) range.
1898 1898
    ///
1899 1899
    class ValueIterator
1900 1900
      : public std::iterator<std::forward_iterator_tag, Value> {
1901 1901
      friend class InvertableMap;
1902 1902
    private:
1903 1903
      ValueIterator(typename Container::const_iterator _it)
1904 1904
        : it(_it) {}
1905 1905
    public:
1906 1906

	
1907 1907
      ValueIterator() {}
1908 1908

	
1909 1909
      ValueIterator& operator++() { ++it; return *this; }
1910 1910
      ValueIterator operator++(int) {
1911 1911
        ValueIterator tmp(*this);
1912 1912
        operator++();
1913 1913
        return tmp;
1914 1914
      }
1915 1915

	
1916 1916
      const Value& operator*() const { return it->first; }
1917 1917
      const Value* operator->() const { return &(it->first); }
1918 1918

	
1919 1919
      bool operator==(ValueIterator jt) const { return it == jt.it; }
1920 1920
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
1921 1921

	
1922 1922
    private:
1923 1923
      typename Container::const_iterator it;
1924 1924
    };
1925 1925

	
1926 1926
    /// \brief Returns an iterator to the first value.
1927 1927
    ///
1928 1928
    /// Returns an stl compatible iterator to the
1929 1929
    /// first value of the map. The values of the
1930 1930
    /// map can be accessed in the [beginValue, endValue)
1931 1931
    /// range.
1932 1932
    ValueIterator beginValue() const {
1933 1933
      return ValueIterator(_inv_map.begin());
1934 1934
    }
1935 1935

	
1936 1936
    /// \brief Returns an iterator after the last value.
1937 1937
    ///
1938 1938
    /// Returns an stl compatible iterator after the
1939 1939
    /// last value of the map. The values of the
1940 1940
    /// map can be accessed in the [beginValue, endValue)
1941 1941
    /// range.
1942 1942
    ValueIterator endValue() const {
1943 1943
      return ValueIterator(_inv_map.end());
1944 1944
    }
1945 1945

	
1946 1946
    /// \brief The setter function of the map.
1947 1947
    ///
1948 1948
    /// Sets the mapped value.
1949 1949
    void set(const Key& key, const Value& val) {
1950 1950
      Value oldval = Map::operator[](key);
1951 1951
      typename Container::iterator it = _inv_map.find(oldval);
1952 1952
      if (it != _inv_map.end() && it->second == key) {
1953 1953
        _inv_map.erase(it);
1954 1954
      }
1955 1955
      _inv_map.insert(make_pair(val, key));
1956 1956
      Map::set(key, val);
1957 1957
    }
1958 1958

	
1959 1959
    /// \brief The getter function of the map.
1960 1960
    ///
1961 1961
    /// It gives back the value associated with the key.
1962 1962
    typename MapTraits<Map>::ConstReturnValue
1963 1963
    operator[](const Key& key) const {
1964 1964
      return Map::operator[](key);
1965 1965
    }
1966 1966

	
1967 1967
    /// \brief Gives back the item by its value.
1968 1968
    ///
1969 1969
    /// Gives back the item by its value.
1970 1970
    Key operator()(const Value& key) const {
1971 1971
      typename Container::const_iterator it = _inv_map.find(key);
1972 1972
      return it != _inv_map.end() ? it->second : INVALID;
1973 1973
    }
1974 1974

	
1975 1975
  protected:
1976 1976

	
1977 1977
    /// \brief Erase the key from the map.
1978 1978
    ///
1979 1979
    /// Erase the key to the map. It is called by the
1980 1980
    /// \c AlterationNotifier.
1981 1981
    virtual void erase(const Key& key) {
1982 1982
      Value val = Map::operator[](key);
1983 1983
      typename Container::iterator it = _inv_map.find(val);
1984 1984
      if (it != _inv_map.end() && it->second == key) {
1985 1985
        _inv_map.erase(it);
1986 1986
      }
1987 1987
      Map::erase(key);
1988 1988
    }
1989 1989

	
1990 1990
    /// \brief Erase more keys from the map.
1991 1991
    ///
1992 1992
    /// Erase more keys from the map. It is called by the
1993 1993
    /// \c AlterationNotifier.
1994 1994
    virtual void erase(const std::vector<Key>& keys) {
1995 1995
      for (int i = 0; i < int(keys.size()); ++i) {
1996 1996
        Value val = Map::operator[](keys[i]);
1997 1997
        typename Container::iterator it = _inv_map.find(val);
1998 1998
        if (it != _inv_map.end() && it->second == keys[i]) {
1999 1999
          _inv_map.erase(it);
2000 2000
        }
2001 2001
      }
2002 2002
      Map::erase(keys);
2003 2003
    }
2004 2004

	
2005 2005
    /// \brief Clear the keys from the map and inverse map.
2006 2006
    ///
2007 2007
    /// Clear the keys from the map and inverse map. It is called by the
2008 2008
    /// \c AlterationNotifier.
2009 2009
    virtual void clear() {
2010 2010
      _inv_map.clear();
2011 2011
      Map::clear();
2012 2012
    }
2013 2013

	
2014 2014
  public:
2015 2015

	
2016 2016
    /// \brief The inverse map type.
2017 2017
    ///
2018 2018
    /// The inverse of this map. The subscript operator of the map
2019 2019
    /// gives back always the item what was last assigned to the value.
2020 2020
    class InverseMap {
2021 2021
    public:
2022 2022
      /// \brief Constructor of the InverseMap.
2023 2023
      ///
2024 2024
      /// Constructor of the InverseMap.
2025 2025
      explicit InverseMap(const InvertableMap& inverted)
2026 2026
        : _inverted(inverted) {}
2027 2027

	
2028 2028
      /// The value type of the InverseMap.
2029 2029
      typedef typename InvertableMap::Key Value;
2030 2030
      /// The key type of the InverseMap.
2031 2031
      typedef typename InvertableMap::Value Key;
2032 2032

	
2033 2033
      /// \brief Subscript operator.
2034 2034
      ///
2035 2035
      /// Subscript operator. It gives back always the item
2036 2036
      /// what was last assigned to the value.
2037 2037
      Value operator[](const Key& key) const {
2038 2038
        return _inverted(key);
2039 2039
      }
2040 2040

	
2041 2041
    private:
2042 2042
      const InvertableMap& _inverted;
2043 2043
    };
2044 2044

	
2045 2045
    /// \brief It gives back the just readable inverse map.
2046 2046
    ///
2047 2047
    /// It gives back the just readable inverse map.
2048 2048
    InverseMap inverse() const {
2049 2049
      return InverseMap(*this);
2050 2050
    }
2051 2051

	
2052 2052

	
2053 2053

	
2054 2054
  };
2055 2055

	
2056 2056
  /// \brief Provides a mutable, continuous and unique descriptor for each
2057 2057
  /// item in the graph.
2058 2058
  ///
2059 2059
  /// The DescriptorMap class provides a unique and continuous (but mutable)
2060 2060
  /// descriptor (id) for each item of the same type (e.g. node) in the
2061 2061
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
2062 2062
  /// different ids <li>\b continuous: the range of the ids is the set of
2063 2063
  /// integers between 0 and \c n-1, where \c n is the number of the items of
2064 2064
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
2065 2065
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
2066 2066
  /// with its member class \c InverseMap, or with the \c operator() member.
2067 2067
  ///
2068 2068
  /// \tparam _Graph The graph class the \c DescriptorMap belongs to.
2069 2069
  /// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or
2070 2070
  /// Edge.
2071 2071
  template <typename _Graph, typename _Item>
2072 2072
  class DescriptorMap
2073 2073
    : protected ItemSetTraits<_Graph, _Item>::template Map<int>::Type {
2074 2074

	
2075 2075
    typedef _Item Item;
2076 2076
    typedef typename ItemSetTraits<_Graph, _Item>::template Map<int>::Type Map;
2077 2077

	
2078 2078
  public:
2079 2079
    /// The graph class of DescriptorMap.
2080 2080
    typedef _Graph Graph;
2081 2081

	
2082 2082
    /// The key type of DescriptorMap (Node, Arc, Edge).
2083 2083
    typedef typename Map::Key Key;
2084 2084
    /// The value type of DescriptorMap.
2085 2085
    typedef typename Map::Value Value;
2086 2086

	
2087 2087
    /// \brief Constructor.
2088 2088
    ///
2089 2089
    /// Constructor for descriptor map.
2090 2090
    explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
2091 2091
      Item it;
2092 2092
      const typename Map::Notifier* nf = Map::notifier();
2093 2093
      for (nf->first(it); it != INVALID; nf->next(it)) {
2094 2094
        Map::set(it, _inv_map.size());
2095 2095
        _inv_map.push_back(it);
2096 2096
      }
2097 2097
    }
2098 2098

	
2099 2099
  protected:
2100 2100

	
2101 2101
    /// \brief Add a new key to the map.
2102 2102
    ///
2103 2103
    /// Add a new key to the map. It is called by the
2104 2104
    /// \c AlterationNotifier.
2105 2105
    virtual void add(const Item& item) {
2106 2106
      Map::add(item);
2107 2107
      Map::set(item, _inv_map.size());
2108 2108
      _inv_map.push_back(item);
2109 2109
    }
2110 2110

	
2111 2111
    /// \brief Add more new keys to the map.
2112 2112
    ///
2113 2113
    /// Add more new keys to the map. It is called by the
2114 2114
    /// \c AlterationNotifier.
2115 2115
    virtual void add(const std::vector<Item>& items) {
2116 2116
      Map::add(items);
2117 2117
      for (int i = 0; i < int(items.size()); ++i) {
2118 2118
        Map::set(items[i], _inv_map.size());
2119 2119
        _inv_map.push_back(items[i]);
2120 2120
      }
2121 2121
    }
2122 2122

	
2123 2123
    /// \brief Erase the key from the map.
2124 2124
    ///
2125 2125
    /// Erase the key from the map. It is called by the
2126 2126
    /// \c AlterationNotifier.
2127 2127
    virtual void erase(const Item& item) {
2128 2128
      Map::set(_inv_map.back(), Map::operator[](item));
2129 2129
      _inv_map[Map::operator[](item)] = _inv_map.back();
2130 2130
      _inv_map.pop_back();
2131 2131
      Map::erase(item);
2132 2132
    }
2133 2133

	
2134 2134
    /// \brief Erase more keys from the map.
2135 2135
    ///
2136 2136
    /// Erase more keys from the map. It is called by the
2137 2137
    /// \c AlterationNotifier.
2138 2138
    virtual void erase(const std::vector<Item>& items) {
2139 2139
      for (int i = 0; i < int(items.size()); ++i) {
2140 2140
        Map::set(_inv_map.back(), Map::operator[](items[i]));
2141 2141
        _inv_map[Map::operator[](items[i])] = _inv_map.back();
2142 2142
        _inv_map.pop_back();
2143 2143
      }
2144 2144
      Map::erase(items);
2145 2145
    }
2146 2146

	
2147 2147
    /// \brief Build the unique map.
2148 2148
    ///
2149 2149
    /// Build the unique map. It is called by the
2150 2150
    /// \c AlterationNotifier.
2151 2151
    virtual void build() {
2152 2152
      Map::build();
2153 2153
      Item it;
2154 2154
      const typename Map::Notifier* nf = Map::notifier();
2155 2155
      for (nf->first(it); it != INVALID; nf->next(it)) {
2156 2156
        Map::set(it, _inv_map.size());
2157 2157
        _inv_map.push_back(it);
2158 2158
      }
2159 2159
    }
2160 2160

	
2161 2161
    /// \brief Clear the keys from the map.
2162 2162
    ///
2163 2163
    /// Clear the keys from the map. It is called by the
2164 2164
    /// \c AlterationNotifier.
2165 2165
    virtual void clear() {
2166 2166
      _inv_map.clear();
2167 2167
      Map::clear();
2168 2168
    }
2169 2169

	
2170 2170
  public:
2171 2171

	
2172 2172
    /// \brief Returns the maximal value plus one.
2173 2173
    ///
2174 2174
    /// Returns the maximal value plus one in the map.
2175 2175
    unsigned int size() const {
2176 2176
      return _inv_map.size();
2177 2177
    }
2178 2178

	
2179 2179
    /// \brief Swaps the position of the two items in the map.
2180 2180
    ///
2181 2181
    /// Swaps the position of the two items in the map.
2182 2182
    void swap(const Item& p, const Item& q) {
2183 2183
      int pi = Map::operator[](p);
2184 2184
      int qi = Map::operator[](q);
2185 2185
      Map::set(p, qi);
2186 2186
      _inv_map[qi] = p;
2187 2187
      Map::set(q, pi);
2188 2188
      _inv_map[pi] = q;
2189 2189
    }
2190 2190

	
2191 2191
    /// \brief Gives back the \e descriptor of the item.
2192 2192
    ///
2193 2193
    /// Gives back the mutable and unique \e descriptor of the map.
2194 2194
    int operator[](const Item& item) const {
2195 2195
      return Map::operator[](item);
2196 2196
    }
2197 2197

	
2198 2198
    /// \brief Gives back the item by its descriptor.
2199 2199
    ///
2200 2200
    /// Gives back th item by its descriptor.
2201 2201
    Item operator()(int id) const {
2202 2202
      return _inv_map[id];
2203 2203
    }
2204 2204

	
2205 2205
  private:
2206 2206

	
2207 2207
    typedef std::vector<Item> Container;
2208 2208
    Container _inv_map;
2209 2209

	
2210 2210
  public:
2211 2211
    /// \brief The inverse map type of DescriptorMap.
2212 2212
    ///
2213 2213
    /// The inverse map type of DescriptorMap.
2214 2214
    class InverseMap {
2215 2215
    public:
2216 2216
      /// \brief Constructor of the InverseMap.
2217 2217
      ///
2218 2218
      /// Constructor of the InverseMap.
2219 2219
      explicit InverseMap(const DescriptorMap& inverted)
2220 2220
        : _inverted(inverted) {}
2221 2221

	
2222 2222

	
2223 2223
      /// The value type of the InverseMap.
2224 2224
      typedef typename DescriptorMap::Key Value;
2225 2225
      /// The key type of the InverseMap.
2226 2226
      typedef typename DescriptorMap::Value Key;
2227 2227

	
2228 2228
      /// \brief Subscript operator.
2229 2229
      ///
2230 2230
      /// Subscript operator. It gives back the item
2231 2231
      /// that the descriptor belongs to currently.
2232 2232
      Value operator[](const Key& key) const {
2233 2233
        return _inverted(key);
2234 2234
      }
2235 2235

	
2236 2236
      /// \brief Size of the map.
2237 2237
      ///
2238 2238
      /// Returns the size of the map.
2239 2239
      unsigned int size() const {
2240 2240
        return _inverted.size();
2241 2241
      }
2242 2242

	
2243 2243
    private:
2244 2244
      const DescriptorMap& _inverted;
2245 2245
    };
2246 2246

	
2247 2247
    /// \brief Gives back the inverse of the map.
2248 2248
    ///
2249 2249
    /// Gives back the inverse of the map.
2250 2250
    const InverseMap inverse() const {
2251 2251
      return InverseMap(*this);
2252 2252
    }
2253 2253
  };
2254 2254

	
2255 2255
  /// \brief Returns the source of the given arc.
2256 2256
  ///
2257 2257
  /// The SourceMap gives back the source Node of the given arc.
2258 2258
  /// \see TargetMap
2259 2259
  template <typename Digraph>
2260 2260
  class SourceMap {
2261 2261
  public:
2262 2262

	
2263 2263
    typedef typename Digraph::Node Value;
2264 2264
    typedef typename Digraph::Arc Key;
2265 2265

	
2266 2266
    /// \brief Constructor
2267 2267
    ///
2268 2268
    /// Constructor
2269 2269
    /// \param _digraph The digraph that the map belongs to.
2270 2270
    explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {}
2271 2271

	
2272 2272
    /// \brief The subscript operator.
2273 2273
    ///
2274 2274
    /// The subscript operator.
2275 2275
    /// \param arc The arc
2276 2276
    /// \return The source of the arc
2277 2277
    Value operator[](const Key& arc) const {
2278 2278
      return _digraph.source(arc);
2279 2279
    }
2280 2280

	
2281 2281
  private:
2282 2282
    const Digraph& _digraph;
2283 2283
  };
2284 2284

	
2285
  /// \brief Returns a \ref SourceMap class.
2285
  /// \brief Returns a \c SourceMap class.
2286 2286
  ///
2287
  /// This function just returns an \ref SourceMap class.
2287
  /// This function just returns an \c SourceMap class.
2288 2288
  /// \relates SourceMap
2289 2289
  template <typename Digraph>
2290 2290
  inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
2291 2291
    return SourceMap<Digraph>(digraph);
2292 2292
  }
2293 2293

	
2294 2294
  /// \brief Returns the target of the given arc.
2295 2295
  ///
2296 2296
  /// The TargetMap gives back the target Node of the given arc.
2297 2297
  /// \see SourceMap
2298 2298
  template <typename Digraph>
2299 2299
  class TargetMap {
2300 2300
  public:
2301 2301

	
2302 2302
    typedef typename Digraph::Node Value;
2303 2303
    typedef typename Digraph::Arc Key;
2304 2304

	
2305 2305
    /// \brief Constructor
2306 2306
    ///
2307 2307
    /// Constructor
2308 2308
    /// \param _digraph The digraph that the map belongs to.
2309 2309
    explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {}
2310 2310

	
2311 2311
    /// \brief The subscript operator.
2312 2312
    ///
2313 2313
    /// The subscript operator.
2314 2314
    /// \param e The arc
2315 2315
    /// \return The target of the arc
2316 2316
    Value operator[](const Key& e) const {
2317 2317
      return _digraph.target(e);
2318 2318
    }
2319 2319

	
2320 2320
  private:
2321 2321
    const Digraph& _digraph;
2322 2322
  };
2323 2323

	
2324
  /// \brief Returns a \ref TargetMap class.
2324
  /// \brief Returns a \c TargetMap class.
2325 2325
  ///
2326
  /// This function just returns a \ref TargetMap class.
2326
  /// This function just returns a \c TargetMap class.
2327 2327
  /// \relates TargetMap
2328 2328
  template <typename Digraph>
2329 2329
  inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
2330 2330
    return TargetMap<Digraph>(digraph);
2331 2331
  }
2332 2332

	
2333 2333
  /// \brief Returns the "forward" directed arc view of an edge.
2334 2334
  ///
2335 2335
  /// Returns the "forward" directed arc view of an edge.
2336 2336
  /// \see BackwardMap
2337 2337
  template <typename Graph>
2338 2338
  class ForwardMap {
2339 2339
  public:
2340 2340

	
2341 2341
    typedef typename Graph::Arc Value;
2342 2342
    typedef typename Graph::Edge Key;
2343 2343

	
2344 2344
    /// \brief Constructor
2345 2345
    ///
2346 2346
    /// Constructor
2347 2347
    /// \param _graph The graph that the map belongs to.
2348 2348
    explicit ForwardMap(const Graph& graph) : _graph(graph) {}
2349 2349

	
2350 2350
    /// \brief The subscript operator.
2351 2351
    ///
2352 2352
    /// The subscript operator.
2353 2353
    /// \param key An edge
2354 2354
    /// \return The "forward" directed arc view of edge
2355 2355
    Value operator[](const Key& key) const {
2356 2356
      return _graph.direct(key, true);
2357 2357
    }
2358 2358

	
2359 2359
  private:
2360 2360
    const Graph& _graph;
2361 2361
  };
2362 2362

	
2363
  /// \brief Returns a \ref ForwardMap class.
2363
  /// \brief Returns a \c ForwardMap class.
2364 2364
  ///
2365
  /// This function just returns an \ref ForwardMap class.
2365
  /// This function just returns an \c ForwardMap class.
2366 2366
  /// \relates ForwardMap
2367 2367
  template <typename Graph>
2368 2368
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
2369 2369
    return ForwardMap<Graph>(graph);
2370 2370
  }
2371 2371

	
2372 2372
  /// \brief Returns the "backward" directed arc view of an edge.
2373 2373
  ///
2374 2374
  /// Returns the "backward" directed arc view of an edge.
2375 2375
  /// \see ForwardMap
2376 2376
  template <typename Graph>
2377 2377
  class BackwardMap {
2378 2378
  public:
2379 2379

	
2380 2380
    typedef typename Graph::Arc Value;
2381 2381
    typedef typename Graph::Edge Key;
2382 2382

	
2383 2383
    /// \brief Constructor
2384 2384
    ///
2385 2385
    /// Constructor
2386 2386
    /// \param _graph The graph that the map belongs to.
2387 2387
    explicit BackwardMap(const Graph& graph) : _graph(graph) {}
2388 2388

	
2389 2389
    /// \brief The subscript operator.
2390 2390
    ///
2391 2391
    /// The subscript operator.
2392 2392
    /// \param key An edge
2393 2393
    /// \return The "backward" directed arc view of edge
2394 2394
    Value operator[](const Key& key) const {
2395 2395
      return _graph.direct(key, false);
2396 2396
    }
2397 2397

	
2398 2398
  private:
2399 2399
    const Graph& _graph;
2400 2400
  };
2401 2401

	
2402
  /// \brief Returns a \ref BackwardMap class
2403

	
2404
  /// This function just returns a \ref BackwardMap class.
2402
  /// \brief Returns a \c BackwardMap class
2403

	
2404
  /// This function just returns a \c BackwardMap class.
2405 2405
  /// \relates BackwardMap
2406 2406
  template <typename Graph>
2407 2407
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
2408 2408
    return BackwardMap<Graph>(graph);
2409 2409
  }
2410 2410

	
2411 2411
  /// \brief Potential difference map
2412 2412
  ///
2413 2413
  /// If there is an potential map on the nodes then we
2414 2414
  /// can get an arc map as we get the substraction of the
2415 2415
  /// values of the target and source.
2416 2416
  template <typename Digraph, typename NodeMap>
2417 2417
  class PotentialDifferenceMap {
2418 2418
  public:
2419 2419
    typedef typename Digraph::Arc Key;
2420 2420
    typedef typename NodeMap::Value Value;
2421 2421

	
2422 2422
    /// \brief Constructor
2423 2423
    ///
2424 2424
    /// Contructor of the map
2425 2425
    explicit PotentialDifferenceMap(const Digraph& digraph,
2426 2426
                                    const NodeMap& potential)
2427 2427
      : _digraph(digraph), _potential(potential) {}
2428 2428

	
2429 2429
    /// \brief Const subscription operator
2430 2430
    ///
2431 2431
    /// Const subscription operator
2432 2432
    Value operator[](const Key& arc) const {
2433 2433
      return _potential[_digraph.target(arc)] -
2434 2434
        _potential[_digraph.source(arc)];
2435 2435
    }
2436 2436

	
2437 2437
  private:
2438 2438
    const Digraph& _digraph;
2439 2439
    const NodeMap& _potential;
2440 2440
  };
2441 2441

	
2442 2442
  /// \brief Returns a PotentialDifferenceMap.
2443 2443
  ///
2444 2444
  /// This function just returns a PotentialDifferenceMap.
2445 2445
  /// \relates PotentialDifferenceMap
2446 2446
  template <typename Digraph, typename NodeMap>
2447 2447
  PotentialDifferenceMap<Digraph, NodeMap>
2448 2448
  potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
2449 2449
    return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential);
2450 2450
  }
2451 2451

	
2452 2452
  /// \brief Map of the node in-degrees.
2453 2453
  ///
2454 2454
  /// This map returns the in-degree of a node. Once it is constructed,
2455 2455
  /// the degrees are stored in a standard NodeMap, so each query is done
2456 2456
  /// in constant time. On the other hand, the values are updated automatically
2457 2457
  /// whenever the digraph changes.
2458 2458
  ///
2459 2459
  /// \warning Besides addNode() and addArc(), a digraph structure may provide
2460 2460
  /// alternative ways to modify the digraph. The correct behavior of InDegMap
2461 2461
  /// is not guarantied if these additional features are used. For example
2462 2462
  /// the functions \ref ListDigraph::changeSource() "changeSource()",
2463 2463
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
2464 2464
  /// \ref ListDigraph::reverseArc() "reverseArc()"
2465 2465
  /// of \ref ListDigraph will \e not update the degree values correctly.
2466 2466
  ///
2467 2467
  /// \sa OutDegMap
2468 2468

	
2469 2469
  template <typename _Digraph>
2470 2470
  class InDegMap
2471 2471
    : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
2472 2472
      ::ItemNotifier::ObserverBase {
2473 2473

	
2474 2474
  public:
2475 2475

	
2476 2476
    typedef _Digraph Digraph;
2477 2477
    typedef int Value;
2478 2478
    typedef typename Digraph::Node Key;
2479 2479

	
2480 2480
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2481 2481
    ::ItemNotifier::ObserverBase Parent;
2482 2482

	
2483 2483
  private:
2484 2484

	
2485 2485
    class AutoNodeMap
2486 2486
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2487 2487
    public:
2488 2488

	
2489 2489
      typedef typename ItemSetTraits<Digraph, Key>::
2490 2490
      template Map<int>::Type Parent;
2491 2491

	
2492 2492
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2493 2493

	
2494 2494
      virtual void add(const Key& key) {
2495 2495
        Parent::add(key);
2496 2496
        Parent::set(key, 0);
2497 2497
      }
2498 2498

	
2499 2499
      virtual void add(const std::vector<Key>& keys) {
2500 2500
        Parent::add(keys);
2501 2501
        for (int i = 0; i < int(keys.size()); ++i) {
2502 2502
          Parent::set(keys[i], 0);
2503 2503
        }
2504 2504
      }
2505 2505

	
2506 2506
      virtual void build() {
2507 2507
        Parent::build();
2508 2508
        Key it;
2509 2509
        typename Parent::Notifier* nf = Parent::notifier();
2510 2510
        for (nf->first(it); it != INVALID; nf->next(it)) {
2511 2511
          Parent::set(it, 0);
2512 2512
        }
2513 2513
      }
2514 2514
    };
2515 2515

	
2516 2516
  public:
2517 2517

	
2518 2518
    /// \brief Constructor.
2519 2519
    ///
2520 2520
    /// Constructor for creating in-degree map.
2521 2521
    explicit InDegMap(const Digraph& digraph)
2522 2522
      : _digraph(digraph), _deg(digraph) {
2523 2523
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2524 2524

	
2525 2525
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2526 2526
        _deg[it] = countInArcs(_digraph, it);
2527 2527
      }
2528 2528
    }
2529 2529

	
2530 2530
    /// Gives back the in-degree of a Node.
2531 2531
    int operator[](const Key& key) const {
2532 2532
      return _deg[key];
2533 2533
    }
2534 2534

	
2535 2535
  protected:
2536 2536

	
2537 2537
    typedef typename Digraph::Arc Arc;
2538 2538

	
2539 2539
    virtual void add(const Arc& arc) {
2540 2540
      ++_deg[_digraph.target(arc)];
2541 2541
    }
2542 2542

	
2543 2543
    virtual void add(const std::vector<Arc>& arcs) {
2544 2544
      for (int i = 0; i < int(arcs.size()); ++i) {
2545 2545
        ++_deg[_digraph.target(arcs[i])];
2546 2546
      }
2547 2547
    }
2548 2548

	
2549 2549
    virtual void erase(const Arc& arc) {
2550 2550
      --_deg[_digraph.target(arc)];
2551 2551
    }
2552 2552

	
2553 2553
    virtual void erase(const std::vector<Arc>& arcs) {
2554 2554
      for (int i = 0; i < int(arcs.size()); ++i) {
2555 2555
        --_deg[_digraph.target(arcs[i])];
2556 2556
      }
2557 2557
    }
2558 2558

	
2559 2559
    virtual void build() {
2560 2560
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2561 2561
        _deg[it] = countInArcs(_digraph, it);
2562 2562
      }
2563 2563
    }
2564 2564

	
2565 2565
    virtual void clear() {
2566 2566
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2567 2567
        _deg[it] = 0;
2568 2568
      }
2569 2569
    }
2570 2570
  private:
2571 2571

	
2572 2572
    const Digraph& _digraph;
2573 2573
    AutoNodeMap _deg;
2574 2574
  };
2575 2575

	
2576 2576
  /// \brief Map of the node out-degrees.
2577 2577
  ///
2578 2578
  /// This map returns the out-degree of a node. Once it is constructed,
2579 2579
  /// the degrees are stored in a standard NodeMap, so each query is done
2580 2580
  /// in constant time. On the other hand, the values are updated automatically
2581 2581
  /// whenever the digraph changes.
2582 2582
  ///
2583 2583
  /// \warning Besides addNode() and addArc(), a digraph structure may provide
2584 2584
  /// alternative ways to modify the digraph. The correct behavior of OutDegMap
2585 2585
  /// is not guarantied if these additional features are used. For example
2586 2586
  /// the functions \ref ListDigraph::changeSource() "changeSource()",
2587 2587
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
2588 2588
  /// \ref ListDigraph::reverseArc() "reverseArc()"
2589 2589
  /// of \ref ListDigraph will \e not update the degree values correctly.
2590 2590
  ///
2591 2591
  /// \sa InDegMap
2592 2592

	
2593 2593
  template <typename _Digraph>
2594 2594
  class OutDegMap
2595 2595
    : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
2596 2596
      ::ItemNotifier::ObserverBase {
2597 2597

	
2598 2598
  public:
2599 2599

	
2600 2600
    typedef _Digraph Digraph;
2601 2601
    typedef int Value;
2602 2602
    typedef typename Digraph::Node Key;
2603 2603

	
2604 2604
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2605 2605
    ::ItemNotifier::ObserverBase Parent;
2606 2606

	
2607 2607
  private:
2608 2608

	
2609 2609
    class AutoNodeMap
2610 2610
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2611 2611
    public:
2612 2612

	
2613 2613
      typedef typename ItemSetTraits<Digraph, Key>::
2614 2614
      template Map<int>::Type Parent;
2615 2615

	
2616 2616
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2617 2617

	
2618 2618
      virtual void add(const Key& key) {
2619 2619
        Parent::add(key);
2620 2620
        Parent::set(key, 0);
2621 2621
      }
2622 2622
      virtual void add(const std::vector<Key>& keys) {
2623 2623
        Parent::add(keys);
2624 2624
        for (int i = 0; i < int(keys.size()); ++i) {
2625 2625
          Parent::set(keys[i], 0);
2626 2626
        }
2627 2627
      }
2628 2628
      virtual void build() {
2629 2629
        Parent::build();
2630 2630
        Key it;
2631 2631
        typename Parent::Notifier* nf = Parent::notifier();
2632 2632
        for (nf->first(it); it != INVALID; nf->next(it)) {
2633 2633
          Parent::set(it, 0);
2634 2634
        }
2635 2635
      }
2636 2636
    };
2637 2637

	
2638 2638
  public:
2639 2639

	
2640 2640
    /// \brief Constructor.
2641 2641
    ///
2642 2642
    /// Constructor for creating out-degree map.
2643 2643
    explicit OutDegMap(const Digraph& digraph)
2644 2644
      : _digraph(digraph), _deg(digraph) {
2645 2645
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2646 2646

	
2647 2647
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2648 2648
        _deg[it] = countOutArcs(_digraph, it);
2649 2649
      }
2650 2650
    }
2651 2651

	
2652 2652
    /// Gives back the out-degree of a Node.
2653 2653
    int operator[](const Key& key) const {
2654 2654
      return _deg[key];
2655 2655
    }
2656 2656

	
2657 2657
  protected:
2658 2658

	
2659 2659
    typedef typename Digraph::Arc Arc;
2660 2660

	
2661 2661
    virtual void add(const Arc& arc) {
2662 2662
      ++_deg[_digraph.source(arc)];
2663 2663
    }
2664 2664

	
2665 2665
    virtual void add(const std::vector<Arc>& arcs) {
2666 2666
      for (int i = 0; i < int(arcs.size()); ++i) {
2667 2667
        ++_deg[_digraph.source(arcs[i])];
2668 2668
      }
2669 2669
    }
2670 2670

	
2671 2671
    virtual void erase(const Arc& arc) {
2672 2672
      --_deg[_digraph.source(arc)];
2673 2673
    }
2674 2674

	
2675 2675
    virtual void erase(const std::vector<Arc>& arcs) {
2676 2676
      for (int i = 0; i < int(arcs.size()); ++i) {
2677 2677
        --_deg[_digraph.source(arcs[i])];
2678 2678
      }
2679 2679
    }
2680 2680

	
2681 2681
    virtual void build() {
2682 2682
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2683 2683
        _deg[it] = countOutArcs(_digraph, it);
2684 2684
      }
2685 2685
    }
2686 2686

	
2687 2687
    virtual void clear() {
2688 2688
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2689 2689
        _deg[it] = 0;
2690 2690
      }
2691 2691
    }
2692 2692
  private:
2693 2693

	
2694 2694
    const Digraph& _digraph;
2695 2695
    AutoNodeMap _deg;
2696 2696
  };
2697 2697

	
2698 2698
  /// @}
2699 2699
}
2700 2700

	
2701 2701
#endif // LEMON_MAPS_H
0 comments (0 inline)