gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fix processedMap() named parameter for dijkstra() (ticket #140)
0 1 0
default
1 file changed with 10 insertions and 4 deletions:
↑ Collapse diff ↑
Ignore white space 1536 line context
... ...
@@ -303,981 +303,987 @@
303 303
      if(!_pred) {
304 304
        local_pred = true;
305 305
        _pred = Traits::createPredMap(*G);
306 306
      }
307 307
      if(!_dist) {
308 308
        local_dist = true;
309 309
        _dist = Traits::createDistMap(*G);
310 310
      }
311 311
      if(!_processed) {
312 312
        local_processed = true;
313 313
        _processed = Traits::createProcessedMap(*G);
314 314
      }
315 315
      if (!_heap_cross_ref) {
316 316
        local_heap_cross_ref = true;
317 317
        _heap_cross_ref = Traits::createHeapCrossRef(*G);
318 318
      }
319 319
      if (!_heap) {
320 320
        local_heap = true;
321 321
        _heap = Traits::createHeap(*_heap_cross_ref);
322 322
      }
323 323
    }
324 324

	
325 325
  public:
326 326

	
327 327
    typedef Dijkstra Create;
328 328

	
329 329
    ///\name Named template parameters
330 330

	
331 331
    ///@{
332 332

	
333 333
    template <class T>
334 334
    struct DefPredMapTraits : public Traits {
335 335
      typedef T PredMap;
336 336
      static PredMap *createPredMap(const Digraph &)
337 337
      {
338 338
        throw UninitializedParameter();
339 339
      }
340 340
    };
341 341
    ///\brief \ref named-templ-param "Named parameter" for setting
342 342
    ///\ref PredMap type.
343 343
    ///
344 344
    ///\ref named-templ-param "Named parameter" for setting
345 345
    ///\ref PredMap type.
346 346
    template <class T>
347 347
    struct DefPredMap
348 348
      : public Dijkstra< Digraph, LengthMap, DefPredMapTraits<T> > {
349 349
      typedef Dijkstra< Digraph, LengthMap, DefPredMapTraits<T> > Create;
350 350
    };
351 351

	
352 352
    template <class T>
353 353
    struct DefDistMapTraits : public Traits {
354 354
      typedef T DistMap;
355 355
      static DistMap *createDistMap(const Digraph &)
356 356
      {
357 357
        throw UninitializedParameter();
358 358
      }
359 359
    };
360 360
    ///\brief \ref named-templ-param "Named parameter" for setting
361 361
    ///\ref DistMap type.
362 362
    ///
363 363
    ///\ref named-templ-param "Named parameter" for setting
364 364
    ///\ref DistMap type.
365 365
    template <class T>
366 366
    struct DefDistMap
367 367
      : public Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > {
368 368
      typedef Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > Create;
369 369
    };
370 370

	
371 371
    template <class T>
372 372
    struct DefProcessedMapTraits : public Traits {
373 373
      typedef T ProcessedMap;
374 374
      static ProcessedMap *createProcessedMap(const Digraph &)
375 375
      {
376 376
        throw UninitializedParameter();
377 377
      }
378 378
    };
379 379
    ///\brief \ref named-templ-param "Named parameter" for setting
380 380
    ///\ref ProcessedMap type.
381 381
    ///
382 382
    ///\ref named-templ-param "Named parameter" for setting
383 383
    ///\ref ProcessedMap type.
384 384
    template <class T>
385 385
    struct DefProcessedMap
386 386
      : public Dijkstra< Digraph, LengthMap, DefProcessedMapTraits<T> > {
387 387
      typedef Dijkstra< Digraph, LengthMap, DefProcessedMapTraits<T> > Create;
388 388
    };
389 389

	
390 390
    struct DefDigraphProcessedMapTraits : public Traits {
391 391
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
392 392
      static ProcessedMap *createProcessedMap(const Digraph &g)
393 393
      {
394 394
        return new ProcessedMap(g);
395 395
      }
396 396
    };
397 397
    ///\brief \ref named-templ-param "Named parameter" for setting
398 398
    ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
399 399
    ///
400 400
    ///\ref named-templ-param "Named parameter" for setting
401 401
    ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
402 402
    ///If you don't set it explicitly, it will be automatically allocated.
403 403
    template <class T>
404 404
    struct DefProcessedMapToBeDefaultMap
405 405
      : public Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> {
406 406
      typedef Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits>
407 407
      Create;
408 408
    };
409 409

	
410 410
    template <class H, class CR>
411 411
    struct DefHeapTraits : public Traits {
412 412
      typedef CR HeapCrossRef;
413 413
      typedef H Heap;
414 414
      static HeapCrossRef *createHeapCrossRef(const Digraph &) {
415 415
        throw UninitializedParameter();
416 416
      }
417 417
      static Heap *createHeap(HeapCrossRef &)
418 418
      {
419 419
        throw UninitializedParameter();
420 420
      }
421 421
    };
422 422
    ///\brief \ref named-templ-param "Named parameter" for setting
423 423
    ///heap and cross reference type
424 424
    ///
425 425
    ///\ref named-templ-param "Named parameter" for setting heap and cross
426 426
    ///reference type.
427 427
    template <class H, class CR = typename Digraph::template NodeMap<int> >
428 428
    struct DefHeap
429 429
      : public Dijkstra< Digraph, LengthMap, DefHeapTraits<H, CR> > {
430 430
      typedef Dijkstra< Digraph, LengthMap, DefHeapTraits<H, CR> > Create;
431 431
    };
432 432

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

	
459 459
    template <class T>
460 460
    struct DefOperationTraitsTraits : public Traits {
461 461
      typedef T OperationTraits;
462 462
    };
463 463

	
464 464
    /// \brief \ref named-templ-param "Named parameter" for setting
465 465
    ///\ref OperationTraits type
466 466
    ///
467 467
    ///\ref named-templ-param "Named parameter" for setting
468 468
    ///\ref OperationTraits type.
469 469
    template <class T>
470 470
    struct DefOperationTraits
471 471
      : public Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> > {
472 472
      typedef Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> >
473 473
      Create;
474 474
    };
475 475

	
476 476
    ///@}
477 477

	
478 478
  protected:
479 479

	
480 480
    Dijkstra() {}
481 481

	
482 482
  public:
483 483

	
484 484
    ///Constructor.
485 485

	
486 486
    ///Constructor.
487 487
    ///\param _g The digraph the algorithm runs on.
488 488
    ///\param _length The length map used by the algorithm.
489 489
    Dijkstra(const Digraph& _g, const LengthMap& _length) :
490 490
      G(&_g), length(&_length),
491 491
      _pred(NULL), local_pred(false),
492 492
      _dist(NULL), local_dist(false),
493 493
      _processed(NULL), local_processed(false),
494 494
      _heap_cross_ref(NULL), local_heap_cross_ref(false),
495 495
      _heap(NULL), local_heap(false)
496 496
    { }
497 497

	
498 498
    ///Destructor.
499 499
    ~Dijkstra()
500 500
    {
501 501
      if(local_pred) delete _pred;
502 502
      if(local_dist) delete _dist;
503 503
      if(local_processed) delete _processed;
504 504
      if(local_heap_cross_ref) delete _heap_cross_ref;
505 505
      if(local_heap) delete _heap;
506 506
    }
507 507

	
508 508
    ///Sets the length map.
509 509

	
510 510
    ///Sets the length map.
511 511
    ///\return <tt> (*this) </tt>
512 512
    Dijkstra &lengthMap(const LengthMap &m)
513 513
    {
514 514
      length = &m;
515 515
      return *this;
516 516
    }
517 517

	
518 518
    ///Sets the map that stores the predecessor arcs.
519 519

	
520 520
    ///Sets the map that stores the predecessor arcs.
521 521
    ///If you don't use this function before calling \ref run(),
522 522
    ///it will allocate one. The destructor deallocates this
523 523
    ///automatically allocated map, of course.
524 524
    ///\return <tt> (*this) </tt>
525 525
    Dijkstra &predMap(PredMap &m)
526 526
    {
527 527
      if(local_pred) {
528 528
        delete _pred;
529 529
        local_pred=false;
530 530
      }
531 531
      _pred = &m;
532 532
      return *this;
533 533
    }
534 534

	
535 535
    ///Sets the map that indicates which nodes are processed.
536 536

	
537 537
    ///Sets the map that indicates which nodes are processed.
538 538
    ///If you don't use this function before calling \ref run(),
539 539
    ///it will allocate one. The destructor deallocates this
540 540
    ///automatically allocated map, of course.
541 541
    ///\return <tt> (*this) </tt>
542 542
    Dijkstra &processedMap(ProcessedMap &m)
543 543
    {
544 544
      if(local_processed) {
545 545
        delete _processed;
546 546
        local_processed=false;
547 547
      }
548 548
      _processed = &m;
549 549
      return *this;
550 550
    }
551 551

	
552 552
    ///Sets the map that stores the distances of the nodes.
553 553

	
554 554
    ///Sets the map that stores the distances of the nodes calculated by the
555 555
    ///algorithm.
556 556
    ///If you don't use this function before calling \ref run(),
557 557
    ///it will allocate one. The destructor deallocates this
558 558
    ///automatically allocated map, of course.
559 559
    ///\return <tt> (*this) </tt>
560 560
    Dijkstra &distMap(DistMap &m)
561 561
    {
562 562
      if(local_dist) {
563 563
        delete _dist;
564 564
        local_dist=false;
565 565
      }
566 566
      _dist = &m;
567 567
      return *this;
568 568
    }
569 569

	
570 570
    ///Sets the heap and the cross reference used by algorithm.
571 571

	
572 572
    ///Sets the heap and the cross reference used by algorithm.
573 573
    ///If you don't use this function before calling \ref run(),
574 574
    ///it will allocate one. The destructor deallocates this
575 575
    ///automatically allocated heap and cross reference, of course.
576 576
    ///\return <tt> (*this) </tt>
577 577
    Dijkstra &heap(Heap& hp, HeapCrossRef &cr)
578 578
    {
579 579
      if(local_heap_cross_ref) {
580 580
        delete _heap_cross_ref;
581 581
        local_heap_cross_ref=false;
582 582
      }
583 583
      _heap_cross_ref = &cr;
584 584
      if(local_heap) {
585 585
        delete _heap;
586 586
        local_heap=false;
587 587
      }
588 588
      _heap = &hp;
589 589
      return *this;
590 590
    }
591 591

	
592 592
  private:
593 593

	
594 594
    void finalizeNodeData(Node v,Value dst)
595 595
    {
596 596
      _processed->set(v,true);
597 597
      _dist->set(v, dst);
598 598
    }
599 599

	
600 600
  public:
601 601

	
602 602
    ///\name Execution control
603 603
    ///The simplest way to execute the algorithm is to use one of the
604 604
    ///member functions called \ref lemon::Dijkstra::run() "run()".
605 605
    ///\n
606 606
    ///If you need more control on the execution, first you must call
607 607
    ///\ref lemon::Dijkstra::init() "init()", then you can add several
608 608
    ///source nodes with \ref lemon::Dijkstra::addSource() "addSource()".
609 609
    ///Finally \ref lemon::Dijkstra::start() "start()" will perform the
610 610
    ///actual path computation.
611 611

	
612 612
    ///@{
613 613

	
614 614
    ///Initializes the internal data structures.
615 615

	
616 616
    ///Initializes the internal data structures.
617 617
    ///
618 618
    void init()
619 619
    {
620 620
      create_maps();
621 621
      _heap->clear();
622 622
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
623 623
        _pred->set(u,INVALID);
624 624
        _processed->set(u,false);
625 625
        _heap_cross_ref->set(u,Heap::PRE_HEAP);
626 626
      }
627 627
    }
628 628

	
629 629
    ///Adds a new source node.
630 630

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

	
647 647
    ///Processes the next node in the priority heap
648 648

	
649 649
    ///Processes the next node in the priority heap.
650 650
    ///
651 651
    ///\return The processed node.
652 652
    ///
653 653
    ///\warning The priority heap must not be empty.
654 654
    Node processNextNode()
655 655
    {
656 656
      Node v=_heap->top();
657 657
      Value oldvalue=_heap->prio();
658 658
      _heap->pop();
659 659
      finalizeNodeData(v,oldvalue);
660 660

	
661 661
      for(OutArcIt e(*G,v); e!=INVALID; ++e) {
662 662
        Node w=G->target(e);
663 663
        switch(_heap->state(w)) {
664 664
        case Heap::PRE_HEAP:
665 665
          _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e]));
666 666
          _pred->set(w,e);
667 667
          break;
668 668
        case Heap::IN_HEAP:
669 669
          {
670 670
            Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]);
671 671
            if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
672 672
              _heap->decrease(w, newvalue);
673 673
              _pred->set(w,e);
674 674
            }
675 675
          }
676 676
          break;
677 677
        case Heap::POST_HEAP:
678 678
          break;
679 679
        }
680 680
      }
681 681
      return v;
682 682
    }
683 683

	
684 684
    ///The next node to be processed.
685 685

	
686 686
    ///Returns the next node to be processed or \c INVALID if the
687 687
    ///priority heap is empty.
688 688
    Node nextNode() const
689 689
    {
690 690
      return !_heap->empty()?_heap->top():INVALID;
691 691
    }
692 692

	
693 693
    ///\brief Returns \c false if there are nodes
694 694
    ///to be processed.
695 695
    ///
696 696
    ///Returns \c false if there are nodes
697 697
    ///to be processed in the priority heap.
698 698
    bool emptyQueue() const { return _heap->empty(); }
699 699

	
700 700
    ///Returns the number of the nodes to be processed in the priority heap
701 701

	
702 702
    ///Returns the number of the nodes to be processed in the priority heap.
703 703
    ///
704 704
    int queueSize() const { return _heap->size(); }
705 705

	
706 706
    ///Executes the algorithm.
707 707

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

	
731 731
    ///Executes the algorithm until the given target node is reached.
732 732

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

	
750 750
    ///Executes the algorithm until a condition is met.
751 751

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

	
775 775
    ///Runs the algorithm from the given node.
776 776

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

	
796 796
    ///Finds the shortest path between \c s and \c t.
797 797

	
798 798
    ///This method runs the %Dijkstra algorithm from node \c s
799 799
    ///in order to compute the shortest path to \c t.
800 800
    ///
801 801
    ///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path,
802 802
    ///if \c t is reachable form \c s, \c 0 otherwise.
803 803
    ///
804 804
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a
805 805
    ///shortcut of the following code.
806 806
    ///\code
807 807
    ///  d.init();
808 808
    ///  d.addSource(s);
809 809
    ///  d.start(t);
810 810
    ///\endcode
811 811
    Value run(Node s,Node t) {
812 812
      init();
813 813
      addSource(s);
814 814
      start(t);
815 815
      return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t];
816 816
    }
817 817

	
818 818
    ///@}
819 819

	
820 820
    ///\name Query Functions
821 821
    ///The result of the %Dijkstra algorithm can be obtained using these
822 822
    ///functions.\n
823 823
    ///Either \ref lemon::Dijkstra::run() "run()" or
824 824
    ///\ref lemon::Dijkstra::start() "start()" must be called before
825 825
    ///using them.
826 826

	
827 827
    ///@{
828 828

	
829 829
    ///The shortest path to a node.
830 830

	
831 831
    ///Returns the shortest path to a node.
832 832
    ///
833 833
    ///\warning \c t should be reachable from the root(s).
834 834
    ///
835 835
    ///\pre Either \ref run() or \ref start() must be called before
836 836
    ///using this function.
837 837
    Path path(Node t) const { return Path(*G, *_pred, t); }
838 838

	
839 839
    ///The distance of a node from the root(s).
840 840

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

	
850 850
    ///Returns the 'previous arc' of the shortest path tree for a node.
851 851

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

	
864 864
    ///Returns the 'previous node' of the shortest path tree for a node.
865 865

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

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

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

	
899 899
    ///Checks if a node is reachable from the root(s).
900 900

	
901 901
    ///Returns \c true if \c v is reachable from the root(s).
902 902
    ///\pre Either \ref run() or \ref start()
903 903
    ///must be called before using this function.
904 904
    bool reached(Node v) const { return (*_heap_cross_ref)[v] !=
905 905
                                        Heap::PRE_HEAP; }
906 906

	
907 907
    ///Checks if a node is processed.
908 908

	
909 909
    ///Returns \c true if \c v is processed, i.e. the shortest
910 910
    ///path to \c v has already found.
911 911
    ///\pre Either \ref run() or \ref start()
912 912
    ///must be called before using this function.
913 913
    bool processed(Node v) const { return (*_heap_cross_ref)[v] ==
914 914
                                          Heap::POST_HEAP; }
915 915

	
916 916
    ///The current distance of a node from the root(s).
917 917

	
918 918
    ///Returns the current distance of a node from the root(s).
919 919
    ///It may be decreased in the following processes.
920 920
    ///\pre \c v should be reached but not processed.
921 921
    Value currentDist(Node v) const { return (*_heap)[v]; }
922 922

	
923 923
    ///@}
924 924
  };
925 925

	
926 926

	
927 927
  ///Default traits class of dijkstra() function.
928 928

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

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

	
945 945
    /// Operation traits for Dijkstra algorithm.
946 946

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

	
951 951
    /// The cross reference type used by the heap.
952 952

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

	
958 958
    ///This function instantiates a \ref HeapCrossRef.
959 959
    /// \param g is the digraph, to which we would like to define the
960 960
    /// HeapCrossRef.
961 961
    /// \todo The digraph alone may be insufficient for the initialization
962 962
    static HeapCrossRef *createHeapCrossRef(const Digraph &g)
963 963
    {
964 964
      return new HeapCrossRef(g);
965 965
    }
966 966

	
967 967
    ///The heap type used by the Dijkstra algorithm.
968 968

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

	
976 976
    ///Instantiates a \ref Heap.
977 977

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

	
985 985
    ///\brief The type of the map that stores the predecessor
986 986
    ///arcs of the shortest paths.
987 987
    ///
988 988
    ///The type of the map that stores the predecessor
989 989
    ///arcs of the shortest paths.
990 990
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
991 991
    typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap;
992 992
    ///Instantiates a \ref PredMap.
993 993

	
994 994
    ///This function instantiates a \ref PredMap.
995 995
    ///\param g is the digraph, to which we would like to define the
996 996
    ///\ref PredMap.
997 997
    ///\todo The digraph alone may be insufficient to initialize
998 998
#ifdef DOXYGEN
999 999
    static PredMap *createPredMap(const Digraph &g)
1000 1000
#else
1001 1001
    static PredMap *createPredMap(const Digraph &)
1002 1002
#endif
1003 1003
    {
1004 1004
      return new PredMap();
1005 1005
    }
1006 1006

	
1007 1007
    ///The type of the map that indicates which nodes are processed.
1008 1008

	
1009 1009
    ///The type of the map that indicates which nodes are processed.
1010 1010
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
1011 1011
    ///By default it is a NullMap.
1012 1012
    ///\todo If it is set to a real map,
1013 1013
    ///Dijkstra::processed() should read this.
1014 1014
    ///\todo named parameter to set this type, function to read and write.
1015 1015
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
1016 1016
    ///Instantiates a \ref ProcessedMap.
1017 1017

	
1018 1018
    ///This function instantiates a \ref ProcessedMap.
1019 1019
    ///\param g is the digraph, to which
1020 1020
    ///we would like to define the \ref ProcessedMap.
1021 1021
#ifdef DOXYGEN
1022 1022
    static ProcessedMap *createProcessedMap(const Digraph &g)
1023 1023
#else
1024 1024
    static ProcessedMap *createProcessedMap(const Digraph &)
1025 1025
#endif
1026 1026
    {
1027 1027
      return new ProcessedMap();
1028 1028
    }
1029 1029

	
1030 1030
    ///The type of the map that stores the distances of the nodes.
1031 1031

	
1032 1032
    ///The type of the map that stores the distances of the nodes.
1033 1033
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
1034 1034
    typedef NullMap<typename Digraph::Node,Value> DistMap;
1035 1035
    ///Instantiates a \ref DistMap.
1036 1036

	
1037 1037
    ///This function instantiates a \ref DistMap.
1038 1038
    ///\param g is the digraph, to which we would like to define
1039 1039
    ///the \ref DistMap
1040 1040
#ifdef DOXYGEN
1041 1041
    static DistMap *createDistMap(const Digraph &g)
1042 1042
#else
1043 1043
    static DistMap *createDistMap(const Digraph &)
1044 1044
#endif
1045 1045
    {
1046 1046
      return new DistMap();
1047 1047
    }
1048 1048
  };
1049 1049

	
1050 1050
  /// Default traits class used by \ref DijkstraWizard
1051 1051

	
1052 1052
  /// To make it easier to use Dijkstra algorithm
1053 1053
  /// we have created a wizard class.
1054 1054
  /// This \ref DijkstraWizard class needs default traits,
1055 1055
  /// as well as the \ref Dijkstra class.
1056 1056
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
1057 1057
  /// \ref DijkstraWizard class.
1058 1058
  /// \todo More named parameters are required...
1059 1059
  template<class GR,class LM>
1060 1060
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
1061 1061
  {
1062 1062
    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
1063 1063
  protected:
1064 1064
    //The type of the nodes in the digraph.
1065 1065
    typedef typename Base::Digraph::Node Node;
1066 1066

	
1067 1067
    //Pointer to the digraph the algorithm runs on.
1068 1068
    void *_g;
1069 1069
    //Pointer to the length map
1070 1070
    void *_length;
1071
    //Pointer to the map of processed nodes.
1072
    void *_processed;
1071 1073
    //Pointer to the map of predecessors arcs.
1072 1074
    void *_pred;
1073 1075
    //Pointer to the map of distances.
1074 1076
    void *_dist;
1075 1077
    //Pointer to the source node.
1076 1078
    Node _source;
1077 1079

	
1078 1080
  public:
1079 1081
    /// Constructor.
1080 1082

	
1081 1083
    /// This constructor does not require parameters, therefore it initiates
1082 1084
    /// all of the attributes to default values (0, INVALID).
1083
    DijkstraWizardBase() : _g(0), _length(0), _pred(0),
1085
    DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
1084 1086
                           _dist(0), _source(INVALID) {}
1085 1087

	
1086 1088
    /// Constructor.
1087 1089

	
1088 1090
    /// This constructor requires some parameters,
1089 1091
    /// listed in the parameters list.
1090 1092
    /// Others are initiated to 0.
1091 1093
    /// \param g The digraph the algorithm runs on.
1092 1094
    /// \param l The length map.
1093 1095
    /// \param s The source node.
1094 1096
    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
1095 1097
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
1096 1098
      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
1097
      _pred(0), _dist(0), _source(s) {}
1099
      _processed(0), _pred(0), _dist(0), _source(s) {}
1098 1100

	
1099 1101
  };
1100 1102

	
1101 1103
  /// Auxiliary class for the function type interface of Dijkstra algorithm.
1102 1104

	
1103 1105
  /// This auxiliary class is created to implement the function type
1104 1106
  /// interface of \ref Dijkstra algorithm. It uses the functions and features
1105 1107
  /// of the plain \ref Dijkstra, but it is much simpler to use it.
1106 1108
  /// It should only be used through the \ref dijkstra() function, which makes
1107 1109
  /// it easier to use the algorithm.
1108 1110
  ///
1109 1111
  /// Simplicity means that the way to change the types defined
1110 1112
  /// in the traits class is based on functions that returns the new class
1111 1113
  /// and not on templatable built-in classes.
1112 1114
  /// When using the plain \ref Dijkstra
1113 1115
  /// the new class with the modified type comes from
1114 1116
  /// the original class by using the ::
1115 1117
  /// operator. In the case of \ref DijkstraWizard only
1116 1118
  /// a function have to be called, and it will
1117 1119
  /// return the needed class.
1118 1120
  ///
1119 1121
  /// It does not have own \ref run() method. When its \ref run() method
1120 1122
  /// is called, it initiates a plain \ref Dijkstra object, and calls the
1121 1123
  /// \ref Dijkstra::run() method of it.
1122 1124
  template<class TR>
1123 1125
  class DijkstraWizard : public TR
1124 1126
  {
1125 1127
    typedef TR Base;
1126 1128

	
1127 1129
    ///The type of the digraph the algorithm runs on.
1128 1130
    typedef typename TR::Digraph Digraph;
1129 1131

	
1130 1132
    typedef typename Digraph::Node Node;
1131 1133
    typedef typename Digraph::NodeIt NodeIt;
1132 1134
    typedef typename Digraph::Arc Arc;
1133 1135
    typedef typename Digraph::OutArcIt OutArcIt;
1134 1136

	
1135 1137
    ///The type of the map that stores the arc lengths.
1136 1138
    typedef typename TR::LengthMap LengthMap;
1137 1139
    ///The type of the length of the arcs.
1138 1140
    typedef typename LengthMap::Value Value;
1139 1141
    ///\brief The type of the map that stores the predecessor
1140 1142
    ///arcs of the shortest paths.
1141 1143
    typedef typename TR::PredMap PredMap;
1142 1144
    ///The type of the map that stores the distances of the nodes.
1143 1145
    typedef typename TR::DistMap DistMap;
1144 1146
    ///The type of the map that indicates which nodes are processed.
1145 1147
    typedef typename TR::ProcessedMap ProcessedMap;
1146 1148
    ///The heap type used by the dijkstra algorithm.
1147 1149
    typedef typename TR::Heap Heap;
1148 1150

	
1149 1151
  public:
1150 1152

	
1151 1153
    /// Constructor.
1152 1154
    DijkstraWizard() : TR() {}
1153 1155

	
1154 1156
    /// Constructor that requires parameters.
1155 1157

	
1156 1158
    /// Constructor that requires parameters.
1157 1159
    /// These parameters will be the default values for the traits class.
1158 1160
    DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) :
1159 1161
      TR(g,l,s) {}
1160 1162

	
1161 1163
    ///Copy constructor
1162 1164
    DijkstraWizard(const TR &b) : TR(b) {}
1163 1165

	
1164 1166
    ~DijkstraWizard() {}
1165 1167

	
1166 1168
    ///Runs Dijkstra algorithm from a source node.
1167 1169

	
1168 1170
    ///Runs Dijkstra algorithm from a source node.
1169 1171
    ///The node can be given with the \ref source() function.
1170 1172
    void run()
1171 1173
    {
1172 1174
      if(Base::_source==INVALID) throw UninitializedParameter();
1173 1175
      Dijkstra<Digraph,LengthMap,TR>
1174 1176
        dij(*reinterpret_cast<const Digraph*>(Base::_g),
1175 1177
            *reinterpret_cast<const LengthMap*>(Base::_length));
1176
      if(Base::_pred) dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1177
      if(Base::_dist) dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1178
      if(Base::_processed)
1179
        dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1180
      if(Base::_pred)
1181
        dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1182
      if(Base::_dist)
1183
        dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1178 1184
      dij.run(Base::_source);
1179 1185
    }
1180 1186

	
1181 1187
    ///Runs Dijkstra algorithm from the given node.
1182 1188

	
1183 1189
    ///Runs Dijkstra algorithm from the given node.
1184 1190
    ///\param s is the given source.
1185 1191
    void run(Node s)
1186 1192
    {
1187 1193
      Base::_source=s;
1188 1194
      run();
1189 1195
    }
1190 1196

	
1191 1197
    /// Sets the source node, from which the Dijkstra algorithm runs.
1192 1198

	
1193 1199
    /// Sets the source node, from which the Dijkstra algorithm runs.
1194 1200
    /// \param s is the source node.
1195 1201
    DijkstraWizard<TR> &source(Node s)
1196 1202
    {
1197 1203
      Base::_source=s;
1198 1204
      return *this;
1199 1205
    }
1200 1206

	
1201 1207
    template<class T>
1202 1208
    struct DefPredMapBase : public Base {
1203 1209
      typedef T PredMap;
1204 1210
      static PredMap *createPredMap(const Digraph &) { return 0; };
1205 1211
      DefPredMapBase(const TR &b) : TR(b) {}
1206 1212
    };
1207 1213
    ///\brief \ref named-templ-param "Named parameter"
1208 1214
    ///for setting \ref PredMap object.
1209 1215
    ///
1210 1216
    ///\ref named-templ-param "Named parameter"
1211 1217
    ///for setting \ref PredMap object.
1212 1218
    template<class T>
1213 1219
    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
1214 1220
    {
1215 1221
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1216 1222
      return DijkstraWizard<DefPredMapBase<T> >(*this);
1217 1223
    }
1218 1224

	
1219 1225
    template<class T>
1220 1226
    struct DefProcessedMapBase : public Base {
1221 1227
      typedef T ProcessedMap;
1222 1228
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1223 1229
      DefProcessedMapBase(const TR &b) : TR(b) {}
1224 1230
    };
1225 1231
    ///\brief \ref named-templ-param "Named parameter"
1226 1232
    ///for setting \ref ProcessedMap object.
1227 1233
    ///
1228 1234
    /// \ref named-templ-param "Named parameter"
1229 1235
    ///for setting \ref ProcessedMap object.
1230 1236
    template<class T>
1231 1237
    DijkstraWizard<DefProcessedMapBase<T> > processedMap(const T &t)
1232 1238
    {
1233 1239
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1234 1240
      return DijkstraWizard<DefProcessedMapBase<T> >(*this);
1235 1241
    }
1236 1242

	
1237 1243
    template<class T>
1238 1244
    struct DefDistMapBase : public Base {
1239 1245
      typedef T DistMap;
1240 1246
      static DistMap *createDistMap(const Digraph &) { return 0; };
1241 1247
      DefDistMapBase(const TR &b) : TR(b) {}
1242 1248
    };
1243 1249
    ///\brief \ref named-templ-param "Named parameter"
1244 1250
    ///for setting \ref DistMap object.
1245 1251
    ///
1246 1252
    ///\ref named-templ-param "Named parameter"
1247 1253
    ///for setting \ref DistMap object.
1248 1254
    template<class T>
1249 1255
    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
1250 1256
    {
1251 1257
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1252 1258
      return DijkstraWizard<DefDistMapBase<T> >(*this);
1253 1259
    }
1254 1260

	
1255 1261
  };
1256 1262

	
1257 1263
  ///Function type interface for Dijkstra algorithm.
1258 1264

	
1259 1265
  /// \ingroup shortest_path
1260 1266
  ///Function type interface for Dijkstra algorithm.
1261 1267
  ///
1262 1268
  ///This function also has several
1263 1269
  ///\ref named-templ-func-param "named parameters",
1264 1270
  ///they are declared as the members of class \ref DijkstraWizard.
1265 1271
  ///The following
1266 1272
  ///example shows how to use these parameters.
1267 1273
  ///\code
1268 1274
  ///  dijkstra(g,length,source).predMap(preds).run();
1269 1275
  ///\endcode
1270 1276
  ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1271 1277
  ///to the end of the parameter list.
1272 1278
  ///\sa DijkstraWizard
1273 1279
  ///\sa Dijkstra
1274 1280
  template<class GR, class LM>
1275 1281
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
1276 1282
  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1277 1283
  {
1278 1284
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1279 1285
  }
1280 1286

	
1281 1287
} //END OF NAMESPACE LEMON
1282 1288

	
1283 1289
#endif
0 comments (0 inline)