| ... | ... |
@@ -12,97 +12,97 @@ |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ELEVATOR_H |
| 20 | 20 |
#define LEMON_ELEVATOR_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup auxdat |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Elevator class |
| 25 | 25 |
/// |
| 26 | 26 |
///Elevator class implements an efficient data structure |
| 27 | 27 |
///for labeling items in push-relabel type algorithms. |
| 28 | 28 |
/// |
| 29 | 29 |
|
| 30 | 30 |
#include <test/test_tools.h> |
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
///Class for handling "labels" in push-relabel type algorithms. |
| 34 | 34 |
|
| 35 | 35 |
///A class for handling "labels" in push-relabel type algorithms. |
| 36 | 36 |
/// |
| 37 | 37 |
///\ingroup auxdat |
| 38 | 38 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
| 39 | 39 |
///to the edges or nodes of a graph, manipulate and query them through |
| 40 | 40 |
///operations typically arising in "push-relabel" type algorithms. |
| 41 | 41 |
/// |
| 42 | 42 |
///Each item is either \em active or not, and you can also choose a |
| 43 | 43 |
///highest level active item. |
| 44 | 44 |
/// |
| 45 | 45 |
///\sa LinkedElevator |
| 46 | 46 |
/// |
| 47 | 47 |
///\param Graph the underlying graph type |
| 48 | 48 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
| 49 | 49 |
///Graph::Edge, Graph::UEdge) |
| 50 | 50 |
template<class Graph, class Item> |
| 51 | 51 |
class Elevator |
| 52 | 52 |
{
|
| 53 | 53 |
public: |
| 54 | 54 |
|
| 55 | 55 |
typedef Item Key; |
| 56 | 56 |
typedef int Value; |
| 57 | 57 |
|
| 58 | 58 |
private: |
| 59 | 59 |
|
| 60 |
typedef |
|
| 60 |
typedef Item *Vit; |
|
| 61 | 61 |
typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap; |
| 62 | 62 |
typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap; |
| 63 | 63 |
|
| 64 | 64 |
const Graph &_g; |
| 65 | 65 |
int _max_level; |
| 66 | 66 |
int _item_num; |
| 67 | 67 |
VitMap _where; |
| 68 | 68 |
IntMap _level; |
| 69 | 69 |
std::vector<Item> _items; |
| 70 | 70 |
std::vector<Vit> _first; |
| 71 | 71 |
std::vector<Vit> _last_active; |
| 72 | 72 |
|
| 73 | 73 |
int _highest_active; |
| 74 | 74 |
|
| 75 | 75 |
void copy(Item i, Vit p) |
| 76 | 76 |
{
|
| 77 | 77 |
_where[*p=i]=p; |
| 78 | 78 |
} |
| 79 | 79 |
void copy(Vit s, Vit p) |
| 80 | 80 |
{
|
| 81 | 81 |
if(s!=p) |
| 82 | 82 |
{
|
| 83 | 83 |
Item i=*s; |
| 84 | 84 |
*p=i; |
| 85 | 85 |
_where[i]=p; |
| 86 | 86 |
} |
| 87 | 87 |
} |
| 88 | 88 |
void swap(Vit i, Vit j) |
| 89 | 89 |
{
|
| 90 | 90 |
Item ti=*i; |
| 91 | 91 |
Vit ct = _where[ti]; |
| 92 | 92 |
_where[ti]=_where[*i=*j]; |
| 93 | 93 |
_where[*j]=ct; |
| 94 | 94 |
*j=ti; |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
public: |
| 98 | 98 |
|
| 99 | 99 |
///Constructor with given maximum level. |
| 100 | 100 |
|
| 101 | 101 |
///Constructor with given maximum level. |
| 102 | 102 |
/// |
| 103 | 103 |
///\param g The underlying graph |
| 104 | 104 |
///\param max_level Set the range of the possible labels to |
| 105 | 105 |
///[0...\c max_level] |
| 106 | 106 |
Elevator(const Graph &g,int max_level) : |
| 107 | 107 |
_g(g), |
| 108 | 108 |
_max_level(max_level), |
| ... | ... |
@@ -388,140 +388,140 @@ |
| 388 | 388 |
///\pre The item is on the top level. |
| 389 | 389 |
void dirtyTopButOne(Item i) {
|
| 390 | 390 |
_level[i] = _max_level - 1; |
| 391 | 391 |
} |
| 392 | 392 |
|
| 393 | 393 |
///Lift all items on and above a level to the top (and deactivate them). |
| 394 | 394 |
|
| 395 | 395 |
///This function lifts all items on and above level \c l to \c |
| 396 | 396 |
///maxLevel(), and also deactivates them. |
| 397 | 397 |
void liftToTop(int l) |
| 398 | 398 |
{
|
| 399 | 399 |
const Vit f=_first[l]; |
| 400 | 400 |
const Vit tl=_first[_max_level]; |
| 401 | 401 |
for(Vit i=f;i!=tl;++i) |
| 402 | 402 |
_level[*i]=_max_level; |
| 403 | 403 |
for(int i=l;i<=_max_level;i++) |
| 404 | 404 |
{
|
| 405 | 405 |
_first[i]=f; |
| 406 | 406 |
_last_active[i]=f-1; |
| 407 | 407 |
} |
| 408 | 408 |
for(_highest_active=l-1; |
| 409 | 409 |
_highest_active>=0 && |
| 410 | 410 |
_last_active[_highest_active]<_first[_highest_active]; |
| 411 | 411 |
_highest_active--) ; |
| 412 | 412 |
} |
| 413 | 413 |
|
| 414 | 414 |
private: |
| 415 | 415 |
int _init_lev; |
| 416 | 416 |
Vit _init_num; |
| 417 | 417 |
|
| 418 | 418 |
public: |
| 419 | 419 |
|
| 420 | 420 |
///\name Initialization |
| 421 | 421 |
///Using this function you can initialize the levels of the item. |
| 422 | 422 |
///\n |
| 423 | 423 |
///This initializatios is started with calling \c initStart(). |
| 424 | 424 |
///Then the |
| 425 | 425 |
///items should be listed levels by levels statring with the lowest one |
| 426 | 426 |
///(with level 0). This is done by using \c initAddItem() |
| 427 | 427 |
///and \c initNewLevel(). Finally \c initFinish() must be called. |
| 428 | 428 |
///The items not listed will be put on the highest level. |
| 429 | 429 |
///@{
|
| 430 | 430 |
|
| 431 | 431 |
///Start the initialization process. |
| 432 | 432 |
|
| 433 | 433 |
void initStart() |
| 434 | 434 |
{
|
| 435 | 435 |
_init_lev=0; |
| 436 |
_init_num=_items.begin(); |
|
| 437 |
_first[0]=_items.begin(); |
|
| 438 |
_last_active[0]=_items.begin()-1; |
|
| 439 |
Vit n=_items.begin(); |
|
| 436 |
_init_num=&_items[0]; |
|
| 437 |
_first[0]=&_items[0]; |
|
| 438 |
_last_active[0]=&_items[0]-1; |
|
| 439 |
Vit n=&_items[0]; |
|
| 440 | 440 |
for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i) |
| 441 | 441 |
{
|
| 442 | 442 |
*n=i; |
| 443 | 443 |
_where[i]=n; |
| 444 | 444 |
_level[i]=_max_level; |
| 445 | 445 |
++n; |
| 446 | 446 |
} |
| 447 | 447 |
} |
| 448 | 448 |
|
| 449 | 449 |
///Add an item to the current level. |
| 450 | 450 |
|
| 451 | 451 |
void initAddItem(Item i) |
| 452 | 452 |
{
|
| 453 | 453 |
swap(_where[i],_init_num); |
| 454 | 454 |
_level[i]=_init_lev; |
| 455 | 455 |
++_init_num; |
| 456 | 456 |
} |
| 457 | 457 |
|
| 458 | 458 |
///Start a new level. |
| 459 | 459 |
|
| 460 | 460 |
///Start a new level. |
| 461 | 461 |
///It shouldn't be used before the items on level 0 are listed. |
| 462 | 462 |
void initNewLevel() |
| 463 | 463 |
{
|
| 464 | 464 |
_init_lev++; |
| 465 | 465 |
_first[_init_lev]=_init_num; |
| 466 | 466 |
_last_active[_init_lev]=_init_num-1; |
| 467 | 467 |
} |
| 468 | 468 |
|
| 469 | 469 |
///Finalize the initialization process. |
| 470 | 470 |
|
| 471 | 471 |
void initFinish() |
| 472 | 472 |
{
|
| 473 | 473 |
for(_init_lev++;_init_lev<=_max_level;_init_lev++) |
| 474 | 474 |
{
|
| 475 | 475 |
_first[_init_lev]=_init_num; |
| 476 | 476 |
_last_active[_init_lev]=_init_num-1; |
| 477 | 477 |
} |
| 478 |
_first[_max_level+1]=_items.begin()+_item_num; |
|
| 479 |
_last_active[_max_level+1]=_items.begin()+_item_num-1; |
|
| 478 |
_first[_max_level+1]=&_items[0]+_item_num; |
|
| 479 |
_last_active[_max_level+1]=&_items[0]+_item_num-1; |
|
| 480 | 480 |
_highest_active = -1; |
| 481 | 481 |
} |
| 482 | 482 |
|
| 483 | 483 |
///@} |
| 484 | 484 |
|
| 485 | 485 |
}; |
| 486 | 486 |
|
| 487 | 487 |
///Class for handling "labels" in push-relabel type algorithms. |
| 488 | 488 |
|
| 489 | 489 |
///A class for handling "labels" in push-relabel type algorithms. |
| 490 | 490 |
/// |
| 491 | 491 |
///\ingroup auxdat |
| 492 | 492 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
| 493 | 493 |
///to the edges or nodes of a graph, manipulate and query them through |
| 494 | 494 |
///operations typically arising in "push-relabel" type algorithms. |
| 495 | 495 |
/// |
| 496 | 496 |
///Each item is either \em active or not, and you can also choose a |
| 497 | 497 |
///highest level active item. |
| 498 | 498 |
/// |
| 499 | 499 |
///\sa Elevator |
| 500 | 500 |
/// |
| 501 | 501 |
///\param Graph the underlying graph type |
| 502 | 502 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
| 503 | 503 |
///Graph::Edge, Graph::UEdge) |
| 504 | 504 |
template <class Graph, class Item> |
| 505 | 505 |
class LinkedElevator {
|
| 506 | 506 |
public: |
| 507 | 507 |
|
| 508 | 508 |
typedef Item Key; |
| 509 | 509 |
typedef int Value; |
| 510 | 510 |
|
| 511 | 511 |
private: |
| 512 | 512 |
|
| 513 | 513 |
typedef typename ItemSetTraits<Graph,Item>:: |
| 514 | 514 |
template Map<Item>::Type ItemMap; |
| 515 | 515 |
typedef typename ItemSetTraits<Graph,Item>:: |
| 516 | 516 |
template Map<int>::Type IntMap; |
| 517 | 517 |
typedef typename ItemSetTraits<Graph,Item>:: |
| 518 | 518 |
template Map<bool>::Type BoolMap; |
| 519 | 519 |
|
| 520 | 520 |
const Graph &_graph; |
| 521 | 521 |
int _max_level; |
| 522 | 522 |
int _item_num; |
| 523 | 523 |
std::vector<Item> _first, _last; |
| 524 | 524 |
ItemMap _prev, _next; |
| 525 | 525 |
int _highest_active; |
| 526 | 526 |
IntMap _level; |
| 527 | 527 |
BoolMap _active; |
0 comments (0 inline)