gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Rename markToBottom() to dirtyTopButOne() + better doc (#174)
0 1 0
default
1 file changed with 17 insertions and 19 deletions:
↑ Collapse diff ↑
Ignore white space 192 line context
... ...
@@ -287,206 +287,205 @@
287 287
    ///Functions for working with the active items.
288 288

	
289 289
    ///@{
290 290

	
291 291
    ///Returns an active item on level \c l.
292 292

	
293 293
    ///Returns an active item on level \c l.
294 294
    ///
295 295
    ///Returns an active item on level \c l or \ref INVALID if there is no such
296 296
    ///an item. (\c l must be from the range [0...\c max_level].
297 297
    Item activeOn(int l) const
298 298
    {
299 299
      return _last_active[l]>=_first[l]?*_last_active[l]:INVALID;
300 300
    }
301 301

	
302 302
    ///Lifts the active item returned by \c activeOn() member function.
303 303

	
304 304
    ///Lifts the active item returned by \c activeOn() member function
305 305
    ///by one.
306 306
    Item liftActiveOn(int level)
307 307
    {
308 308
      ++_level[*_last_active[level]];
309 309
      swap(_last_active[level]--, --_first[level+1]);
310 310
      if (level+1>_highest_active) ++_highest_active;
311 311
    }
312 312

	
313 313
    ///Lifts the active item returned by \c activeOn() member function.
314 314

	
315 315
    ///Lifts the active item returned by \c activeOn() member function
316 316
    ///to the given level.
317 317
    void liftActiveOn(int level, int new_level)
318 318
    {
319 319
      const Item ai = *_last_active[level];
320 320

	
321 321
      copy(--_first[level+1], _last_active[level]--);
322 322
      for(int l=level+1;l<new_level;l++)
323 323
        {
324 324
          copy(_last_active[l],_first[l]);
325 325
          copy(--_first[l+1], _last_active[l]--);
326 326
        }
327 327
      copy(ai,_first[new_level]);
328 328
      _level[ai]=new_level;
329 329
      if (new_level>_highest_active) _highest_active=new_level;
330 330
    }
331 331

	
332 332
    ///Lifts the active item returned by \c activeOn() member function.
333 333

	
334 334
    ///Lifts the active item returned by \c activeOn() member function
335 335
    ///to the top level.
336 336
    void liftActiveToTop(int level)
337 337
    {
338 338
      const Item ai = *_last_active[level];
339 339

	
340 340
      copy(--_first[level+1],_last_active[level]--);
341 341
      for(int l=level+1;l<_max_level;l++)
342 342
        {
343 343
          copy(_last_active[l],_first[l]);
344 344
          copy(--_first[l+1], _last_active[l]--);
345 345
        }
346 346
      copy(ai,_first[_max_level]);
347 347
      --_last_active[_max_level];
348 348
      _level[ai]=_max_level;
349 349

	
350 350
      if (_highest_active==level) {
351 351
        while(_highest_active>=0 &&
352 352
              _last_active[_highest_active]<_first[_highest_active])
353 353
          _highest_active--;
354 354
      }
355 355
    }
356 356

	
357 357
    ///@}
358 358

	
359 359
    ///Lift an active item to a higher level.
360 360

	
361 361
    ///Lift an active item to a higher level.
362 362
    ///\param i The item to be lifted. It must be active.
363 363
    ///\param new_level The new level of \c i. It must be strictly higher
364 364
    ///than the current level.
365 365
    ///
366 366
    void lift(Item i, int new_level)
367 367
    {
368 368
      const int lo = _level[i];
369 369
      const Vit w = _where[i];
370 370

	
371 371
      copy(_last_active[lo],w);
372 372
      copy(--_first[lo+1],_last_active[lo]--);
373 373
      for(int l=lo+1;l<new_level;l++)
374 374
        {
375 375
          copy(_last_active[l],_first[l]);
376 376
          copy(--_first[l+1],_last_active[l]--);
377 377
        }
378 378
      copy(i,_first[new_level]);
379 379
      _level[i]=new_level;
380 380
      if(new_level>_highest_active) _highest_active=new_level;
381 381
    }
382 382

	
383
    ///Mark the node as it did not reach the max level
383
    ///Move an inactive item to the top but one level (in a dirty way).
384 384

	
385
    ///Mark the node as it did not reach the max level. It sets the
386
    ///level to the under the max level value. The node will be never
387
    ///more activated because the push operation from the maximum
388
    ///level is forbidden in the push-relabel algorithms. The node
389
    ///should be lifted previously to the top level.
390
    void markToBottom(Item i) {
385
    ///This function moves an inactive item to the top but one level.
386
    ///It makes the underlying datastructure corrupt, so use is only if
387
    ///you really know what it is for.
388
    ///\pre The item is on the top level.
389
    void dirtyTopButOne(Item i) {
391 390
      _level[i] = _max_level - 1;
392 391
    }
393 392

	
394
    ///Lift all nodes on and above a level to the top (and deactivate them).
393
    ///Lift all items on and above a level to the top (and deactivate them).
395 394

	
396
    ///This function lifts all nodes on and above level \c l to \c
395
    ///This function lifts all items on and above level \c l to \c
397 396
    ///maxLevel(), and also deactivates them.
398 397
    void liftToTop(int l)
399 398
    {
400 399
      const Vit f=_first[l];
401 400
      const Vit tl=_first[_max_level];
402 401
      for(Vit i=f;i!=tl;++i)
403 402
        _level[*i]=_max_level;
404 403
      for(int i=l;i<=_max_level;i++)
405 404
        {
406 405
          _first[i]=f;
407 406
          _last_active[i]=f-1;
408 407
        }
409 408
      for(_highest_active=l-1;
410 409
          _highest_active>=0 &&
411 410
            _last_active[_highest_active]<_first[_highest_active];
412 411
          _highest_active--) ;
413 412
    }
414 413

	
415 414
  private:
416 415
    int _init_lev;
417 416
    Vit _init_num;
418 417

	
419 418
  public:
420 419

	
421 420
    ///\name Initialization
422 421
    ///Using this function you can initialize the levels of the item.
423 422
    ///\n
424 423
    ///This initializatios is started with calling \c initStart().
425 424
    ///Then the
426 425
    ///items should be listed levels by levels statring with the lowest one
427 426
    ///(with level 0). This is done by using \c initAddItem()
428 427
    ///and \c initNewLevel(). Finally \c initFinish() must be called.
429 428
    ///The items not listed will be put on the highest level.
430 429
    ///@{
431 430

	
432 431
    ///Start the initialization process.
433 432

	
434 433
    void initStart()
435 434
    {
436 435
      _init_lev=0;
437 436
      _init_num=_items.begin();
438 437
      _first[0]=_items.begin();
439 438
      _last_active[0]=_items.begin()-1;
440 439
      Vit n=_items.begin();
441 440
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i)
442 441
        {
443 442
          *n=i;
444 443
          _where[i]=n;
445 444
          _level[i]=_max_level;
446 445
          ++n;
447 446
        }
448 447
    }
449 448

	
450 449
    ///Add an item to the current level.
451 450

	
452 451
    void initAddItem(Item i)
453 452
    {
454 453
     swap(_where[i],_init_num);
455 454
      _level[i]=_init_lev;
456 455
      ++_init_num;
457 456
    }
458 457

	
459 458
    ///Start a new level.
460 459

	
461 460
    ///Start a new level.
462 461
    ///It shouldn't be used before the items on level 0 are listed.
463 462
    void initNewLevel()
464 463
    {
465 464
      _init_lev++;
466 465
      _first[_init_lev]=_init_num;
467 466
      _last_active[_init_lev]=_init_num-1;
468 467
    }
469 468

	
470 469
    ///Finalize the initialization process.
471 470

	
472 471
    void initFinish()
473 472
    {
474 473
      for(_init_lev++;_init_lev<=_max_level;_init_lev++)
475 474
        {
476 475
          _first[_init_lev]=_init_num;
477 476
          _last_active[_init_lev]=_init_num-1;
478 477
        }
479 478
      _first[_max_level+1]=_items.begin()+_item_num;
480 479
      _last_active[_max_level+1]=_items.begin()+_item_num-1;
481 480
      _highest_active = -1;
482 481
    }
483 482

	
484 483
    ///@}
485 484

	
486 485
  };
487 486

	
488 487
  ///Class for handling "labels" in push-relabel type algorithms.
489 488

	
490 489
  ///A class for handling "labels" in push-relabel type algorithms.
491 490
  ///
492 491
  ///\ingroup auxdat
... ...
@@ -656,348 +655,347 @@
656 655
      return num;
657 656
    }
658 657

	
659 658
    ///Return true if there is not active item on level \c l.
660 659
    bool activeFree(int l) const {
661 660
      return _first[l] == INVALID || !_active[_first[l]];
662 661
    }
663 662

	
664 663
    ///Return the maximum allowed level.
665 664
    int maxLevel() const {
666 665
      return _max_level;
667 666
    }
668 667

	
669 668
    ///\name Highest Active Item
670 669
    ///Functions for working with the highest level
671 670
    ///active item.
672 671

	
673 672
    ///@{
674 673

	
675 674
    ///Return a highest level active item.
676 675

	
677 676
    ///Return a highest level active item.
678 677
    ///
679 678
    ///\return the highest level active item or INVALID if there is no
680 679
    ///active item.
681 680
    Item highestActive() const {
682 681
      return _highest_active >= 0 ? _first[_highest_active] : INVALID;
683 682
    }
684 683

	
685 684
    ///Return a highest active level.
686 685

	
687 686
    ///Return a highest active level.
688 687
    ///
689 688
    ///\return the level of the highest active item or -1 if there is
690 689
    ///no active item.
691 690
    int highestActiveLevel() const {
692 691
      return _highest_active;
693 692
    }
694 693

	
695 694
    ///Lift the highest active item by one.
696 695

	
697 696
    ///Lift the item returned by highestActive() by one.
698 697
    ///
699 698
    void liftHighestActive() {
700 699
      Item i = _first[_highest_active];
701 700
      if (_next[i] != INVALID) {
702 701
        _prev.set(_next[i], INVALID);
703 702
        _first[_highest_active] = _next[i];
704 703
      } else {
705 704
        _first[_highest_active] = INVALID;
706 705
        _last[_highest_active] = INVALID;
707 706
      }
708 707
      _level.set(i, ++_highest_active);
709 708
      if (_first[_highest_active] == INVALID) {
710 709
        _first[_highest_active] = i;
711 710
        _last[_highest_active] = i;
712 711
        _prev.set(i, INVALID);
713 712
        _next.set(i, INVALID);
714 713
      } else {
715 714
        _prev.set(_first[_highest_active], i);
716 715
        _next.set(i, _first[_highest_active]);
717 716
        _first[_highest_active] = i;
718 717
      }
719 718
    }
720 719

	
721 720
    ///Lift the highest active item.
722 721

	
723 722
    ///Lift the item returned by highestActive() to level \c new_level.
724 723
    ///
725 724
    ///\warning \c new_level must be strictly higher
726 725
    ///than the current level.
727 726
    ///
728 727
    void liftHighestActive(int new_level) {
729 728
      Item i = _first[_highest_active];
730 729
      if (_next[i] != INVALID) {
731 730
        _prev.set(_next[i], INVALID);
732 731
        _first[_highest_active] = _next[i];
733 732
      } else {
734 733
        _first[_highest_active] = INVALID;
735 734
        _last[_highest_active] = INVALID;
736 735
      }
737 736
      _level.set(i, _highest_active = new_level);
738 737
      if (_first[_highest_active] == INVALID) {
739 738
        _first[_highest_active] = _last[_highest_active] = i;
740 739
        _prev.set(i, INVALID);
741 740
        _next.set(i, INVALID);
742 741
      } else {
743 742
        _prev.set(_first[_highest_active], i);
744 743
        _next.set(i, _first[_highest_active]);
745 744
        _first[_highest_active] = i;
746 745
      }
747 746
    }
748 747

	
749 748
    ///Lift the highest active to top.
750 749

	
751 750
    ///Lift the item returned by highestActive() to the top level and
752
    ///deactivates the node.
751
    ///deactivates the item.
753 752
    ///
754 753
    void liftHighestActiveToTop() {
755 754
      Item i = _first[_highest_active];
756 755
      _level.set(i, _max_level);
757 756
      if (_next[i] != INVALID) {
758 757
        _prev.set(_next[i], INVALID);
759 758
        _first[_highest_active] = _next[i];
760 759
      } else {
761 760
        _first[_highest_active] = INVALID;
762 761
        _last[_highest_active] = INVALID;
763 762
      }
764 763
      while (_highest_active >= 0 && activeFree(_highest_active))
765 764
        --_highest_active;
766 765
    }
767 766

	
768 767
    ///@}
769 768

	
770 769
    ///\name Active Item on Certain Level
771 770
    ///Functions for working with the active items.
772 771

	
773 772
    ///@{
774 773

	
775 774
    ///Returns an active item on level \c l.
776 775

	
777 776
    ///Returns an active item on level \c l.
778 777
    ///
779 778
    ///Returns an active item on level \c l or \ref INVALID if there is no such
780 779
    ///an item. (\c l must be from the range [0...\c max_level].
781 780
    Item activeOn(int l) const
782 781
    {
783 782
      return _active[_first[l]] ? _first[l] : INVALID;
784 783
    }
785 784

	
786 785
    ///Lifts the active item returned by \c activeOn() member function.
787 786

	
788 787
    ///Lifts the active item returned by \c activeOn() member function
789 788
    ///by one.
790 789
    Item liftActiveOn(int l)
791 790
    {
792 791
      Item i = _first[l];
793 792
      if (_next[i] != INVALID) {
794 793
        _prev.set(_next[i], INVALID);
795 794
        _first[l] = _next[i];
796 795
      } else {
797 796
        _first[l] = INVALID;
798 797
        _last[l] = INVALID;
799 798
      }
800 799
      _level.set(i, ++l);
801 800
      if (_first[l] == INVALID) {
802 801
        _first[l] = _last[l] = i;
803 802
        _prev.set(i, INVALID);
804 803
        _next.set(i, INVALID);
805 804
      } else {
806 805
        _prev.set(_first[l], i);
807 806
        _next.set(i, _first[l]);
808 807
        _first[l] = i;
809 808
      }
810 809
      if (_highest_active < l) {
811 810
        _highest_active = l;
812 811
      }
813 812
    }
814 813

	
815 814
    /// \brief Lifts the active item returned by \c activeOn() member function.
816 815
    ///
817 816
    /// Lifts the active item returned by \c activeOn() member function
818 817
    /// to the given level.
819 818
    void liftActiveOn(int l, int new_level)
820 819
    {
821 820
      Item i = _first[l];
822 821
      if (_next[i] != INVALID) {
823 822
        _prev.set(_next[i], INVALID);
824 823
        _first[l] = _next[i];
825 824
      } else {
826 825
        _first[l] = INVALID;
827 826
        _last[l] = INVALID;
828 827
      }
829 828
      _level.set(i, l = new_level);
830 829
      if (_first[l] == INVALID) {
831 830
        _first[l] = _last[l] = i;
832 831
        _prev.set(i, INVALID);
833 832
        _next.set(i, INVALID);
834 833
      } else {
835 834
        _prev.set(_first[l], i);
836 835
        _next.set(i, _first[l]);
837 836
        _first[l] = i;
838 837
      }
839 838
      if (_highest_active < l) {
840 839
        _highest_active = l;
841 840
      }
842 841
    }
843 842

	
844 843
    ///Lifts the active item returned by \c activeOn() member function.
845 844

	
846 845
    ///Lifts the active item returned by \c activeOn() member function
847 846
    ///to the top level.
848 847
    void liftActiveToTop(int l)
849 848
    {
850 849
      Item i = _first[l];
851 850
      if (_next[i] != INVALID) {
852 851
        _prev.set(_next[i], INVALID);
853 852
        _first[l] = _next[i];
854 853
      } else {
855 854
        _first[l] = INVALID;
856 855
        _last[l] = INVALID;
857 856
      }
858 857
      _level.set(i, _max_level);
859 858
      if (l == _highest_active) {
860 859
        while (_highest_active >= 0 && activeFree(_highest_active))
861 860
          --_highest_active;
862 861
      }
863 862
    }
864 863

	
865 864
    ///@}
866 865

	
867 866
    /// \brief Lift an active item to a higher level.
868 867
    ///
869 868
    /// Lift an active item to a higher level.
870 869
    /// \param i The item to be lifted. It must be active.
871 870
    /// \param new_level The new level of \c i. It must be strictly higher
872 871
    /// than the current level.
873 872
    ///
874 873
    void lift(Item i, int new_level) {
875 874
      if (_next[i] != INVALID) {
876 875
        _prev.set(_next[i], _prev[i]);
877 876
      } else {
878 877
        _last[new_level] = _prev[i];
879 878
      }
880 879
      if (_prev[i] != INVALID) {
881 880
        _next.set(_prev[i], _next[i]);
882 881
      } else {
883 882
        _first[new_level] = _next[i];
884 883
      }
885 884
      _level.set(i, new_level);
886 885
      if (_first[new_level] == INVALID) {
887 886
        _first[new_level] = _last[new_level] = i;
888 887
        _prev.set(i, INVALID);
889 888
        _next.set(i, INVALID);
890 889
      } else {
891 890
        _prev.set(_first[new_level], i);
892 891
        _next.set(i, _first[new_level]);
893 892
        _first[new_level] = i;
894 893
      }
895 894
      if (_highest_active < new_level) {
896 895
        _highest_active = new_level;
897 896
      }
898 897
    }
899 898

	
900
    ///Mark the node as it did not reach the max level
899
    ///Move an inactive item to the top but one level (in a dirty way).
901 900

	
902
    ///Mark the node as it did not reach the max level. It sets the
903
    ///level to the under the max level value. The node will be never
904
    ///more activated because the push operation from the maximum
905
    ///level is forbidden in the push-relabel algorithms. The node
906
    ///should be lifted previously to the top level.
907
    void markToBottom(Item i) {
901
    ///This function moves an inactive item to the top but one level.
902
    ///It makes the underlying datastructure corrupt, so use is only if
903
    ///you really know what it is for.
904
    ///\pre The item is on the top level.
905
    void dirtyTopButOne(Item i) {
908 906
      _level.set(i, _max_level - 1);
909 907
    }
910 908

	
911
    ///Lift all nodes on and above a level to the top (and deactivate them).
909
    ///Lift all items on and above a level to the top (and deactivate them).
912 910

	
913
    ///This function lifts all nodes on and above level \c l to \c
911
    ///This function lifts all items on and above level \c l to \c
914 912
    ///maxLevel(), and also deactivates them.
915 913
    void liftToTop(int l)  {
916 914
      for (int i = l + 1; _first[i] != INVALID; ++i) {
917 915
        Item n = _first[i];
918 916
        while (n != INVALID) {
919 917
          _level.set(n, _max_level);
920 918
          n = _next[n];
921 919
        }
922 920
        _first[i] = INVALID;
923 921
        _last[i] = INVALID;
924 922
      }
925 923
      if (_highest_active > l - 1) {
926 924
        _highest_active = l - 1;
927 925
        while (_highest_active >= 0 && activeFree(_highest_active))
928 926
          --_highest_active;
929 927
      }
930 928
    }
931 929

	
932 930
  private:
933 931

	
934 932
    int _init_level;
935 933

	
936 934
  public:
937 935

	
938 936
    ///\name Initialization
939 937
    ///Using this function you can initialize the levels of the item.
940 938
    ///\n
941 939
    ///This initializatios is started with calling \c initStart().
942 940
    ///Then the
943 941
    ///items should be listed levels by levels statring with the lowest one
944 942
    ///(with level 0). This is done by using \c initAddItem()
945 943
    ///and \c initNewLevel(). Finally \c initFinish() must be called.
946 944
    ///The items not listed will be put on the highest level.
947 945
    ///@{
948 946

	
949 947
    ///Start the initialization process.
950 948

	
951 949
    void initStart() {
952 950

	
953 951
      for (int i = 0; i <= _max_level; ++i) {
954 952
        _first[i] = _last[i] = INVALID;
955 953
      }
956 954
      _init_level = 0;
957 955
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_graph);
958 956
          i != INVALID; ++i) {
959 957
        _level.set(i, _max_level);
960 958
        _active.set(i, false);
961 959
      }
962 960
    }
963 961

	
964 962
    ///Add an item to the current level.
965 963

	
966 964
    void initAddItem(Item i) {
967 965
      _level.set(i, _init_level);
968 966
      if (_last[_init_level] == INVALID) {
969 967
        _first[_init_level] = i;
970 968
        _last[_init_level] = i;
971 969
        _prev.set(i, INVALID);
972 970
        _next.set(i, INVALID);
973 971
      } else {
974 972
        _prev.set(i, _last[_init_level]);
975 973
        _next.set(i, INVALID);
976 974
        _next.set(_last[_init_level], i);
977 975
        _last[_init_level] = i;
978 976
      }
979 977
    }
980 978

	
981 979
    ///Start a new level.
982 980

	
983 981
    ///Start a new level.
984 982
    ///It shouldn't be used before the items on level 0 are listed.
985 983
    void initNewLevel() {
986 984
      ++_init_level;
987 985
    }
988 986

	
989 987
    ///Finalize the initialization process.
990 988

	
991 989
    void initFinish() {
992 990
      _highest_active = -1;
993 991
    }
994 992

	
995 993
    ///@}
996 994

	
997 995
  };
998 996

	
999 997

	
1000 998
} //END OF NAMESPACE LEMON
1001 999

	
1002 1000
#endif
1003 1001

	
0 comments (0 inline)