| ... | ... |
@@ -18,44 +18,45 @@ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ELEVATOR_H |
| 20 | 20 |
#define LEMON_ELEVATOR_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup auxdat |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Elevator class |
| 25 | 25 |
/// |
| 26 | 26 |
///Elevator class implements an efficient data structure |
| 27 | 27 |
///for labeling items in push-relabel type algorithms. |
| 28 | 28 |
/// |
| 29 | 29 |
|
| 30 |
#include < |
|
| 30 |
#include <lemon/bits/traits.h> |
|
| 31 |
|
|
| 31 | 32 |
namespace lemon {
|
| 32 | 33 |
|
| 33 | 34 |
///Class for handling "labels" in push-relabel type algorithms. |
| 34 | 35 |
|
| 35 | 36 |
///A class for handling "labels" in push-relabel type algorithms. |
| 36 | 37 |
/// |
| 37 | 38 |
///\ingroup auxdat |
| 38 | 39 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
| 39 | 40 |
///to the edges or nodes of a graph, manipulate and query them through |
| 40 | 41 |
///operations typically arising in "push-relabel" type algorithms. |
| 41 | 42 |
/// |
| 42 | 43 |
///Each item is either \em active or not, and you can also choose a |
| 43 | 44 |
///highest level active item. |
| 44 | 45 |
/// |
| 45 | 46 |
///\sa LinkedElevator |
| 46 | 47 |
/// |
| 47 |
///\param Graph the underlying graph |
|
| 48 |
///\param Graph Type of the underlying graph. |
|
| 48 | 49 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
| 49 |
///Graph:: |
|
| 50 |
///Graph::Arc, Graph::Edge). |
|
| 50 | 51 |
template<class Graph, class Item> |
| 51 | 52 |
class Elevator |
| 52 | 53 |
{
|
| 53 | 54 |
public: |
| 54 | 55 |
|
| 55 | 56 |
typedef Item Key; |
| 56 | 57 |
typedef int Value; |
| 57 | 58 |
|
| 58 | 59 |
private: |
| 59 | 60 |
|
| 60 | 61 |
typedef Item *Vit; |
| 61 | 62 |
typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap; |
| ... | ... |
@@ -91,50 +92,50 @@ |
| 91 | 92 |
Vit ct = _where[ti]; |
| 92 | 93 |
_where.set(ti,_where[*i=*j]); |
| 93 | 94 |
_where.set(*j,ct); |
| 94 | 95 |
*j=ti; |
| 95 | 96 |
} |
| 96 | 97 |
|
| 97 | 98 |
public: |
| 98 | 99 |
|
| 99 | 100 |
///Constructor with given maximum level. |
| 100 | 101 |
|
| 101 | 102 |
///Constructor with given maximum level. |
| 102 | 103 |
/// |
| 103 |
///\param g The underlying graph |
|
| 104 |
///\param max_level Set the range of the possible labels to |
|
| 105 |
///[0...\c max_level] |
|
| 106 |
Elevator(const Graph &g,int max_level) : |
|
| 107 |
|
|
| 104 |
///\param graph The underlying graph. |
|
| 105 |
///\param max_level The maximum allowed level. |
|
| 106 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
|
| 107 |
Elevator(const Graph &graph,int max_level) : |
|
| 108 |
_g(graph), |
|
| 108 | 109 |
_max_level(max_level), |
| 109 | 110 |
_item_num(_max_level), |
| 110 |
_where(g), |
|
| 111 |
_level(g,0), |
|
| 111 |
_where(graph), |
|
| 112 |
_level(graph,0), |
|
| 112 | 113 |
_items(_max_level), |
| 113 | 114 |
_first(_max_level+2), |
| 114 | 115 |
_last_active(_max_level+2), |
| 115 | 116 |
_highest_active(-1) {}
|
| 116 | 117 |
///Constructor. |
| 117 | 118 |
|
| 118 | 119 |
///Constructor. |
| 119 | 120 |
/// |
| 120 |
///\param g The underlying graph |
|
| 121 |
///The range of the possible labels is [0...\c max_level], |
|
| 121 |
///\param graph The underlying graph. |
|
| 122 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>, |
|
| 122 | 123 |
///where \c max_level is equal to the number of labeled items in the graph. |
| 123 |
Elevator(const Graph &g) : |
|
| 124 |
_g(g), |
|
| 125 |
|
|
| 124 |
Elevator(const Graph &graph) : |
|
| 125 |
_g(graph), |
|
| 126 |
_max_level(countItems<Graph, Item>(graph)), |
|
| 126 | 127 |
_item_num(_max_level), |
| 127 |
_where(g), |
|
| 128 |
_level(g,0), |
|
| 128 |
_where(graph), |
|
| 129 |
_level(graph,0), |
|
| 129 | 130 |
_items(_max_level), |
| 130 | 131 |
_first(_max_level+2), |
| 131 | 132 |
_last_active(_max_level+2), |
| 132 | 133 |
_highest_active(-1) |
| 133 | 134 |
{
|
| 134 | 135 |
} |
| 135 | 136 |
|
| 136 | 137 |
///Activate item \c i. |
| 137 | 138 |
|
| 138 | 139 |
///Activate item \c i. |
| 139 | 140 |
///\pre Item \c i shouldn't be active before. |
| 140 | 141 |
void activate(Item i) |
| ... | ... |
@@ -158,120 +159,112 @@ |
| 158 | 159 |
|
| 159 | 160 |
///Query whether item \c i is active |
| 160 | 161 |
bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; }
|
| 161 | 162 |
|
| 162 | 163 |
///Return the level of item \c i. |
| 163 | 164 |
int operator[](Item i) const { return _level[i]; }
|
| 164 | 165 |
|
| 165 | 166 |
///Return the number of items on level \c l. |
| 166 | 167 |
int onLevel(int l) const |
| 167 | 168 |
{
|
| 168 | 169 |
return _first[l+1]-_first[l]; |
| 169 | 170 |
} |
| 170 |
///Return true if |
|
| 171 |
///Return true if level \c l is empty. |
|
| 171 | 172 |
bool emptyLevel(int l) const |
| 172 | 173 |
{
|
| 173 | 174 |
return _first[l+1]-_first[l]==0; |
| 174 | 175 |
} |
| 175 | 176 |
///Return the number of items above level \c l. |
| 176 | 177 |
int aboveLevel(int l) const |
| 177 | 178 |
{
|
| 178 | 179 |
return _first[_max_level+1]-_first[l+1]; |
| 179 | 180 |
} |
| 180 | 181 |
///Return the number of active items on level \c l. |
| 181 | 182 |
int activesOnLevel(int l) const |
| 182 | 183 |
{
|
| 183 | 184 |
return _last_active[l]-_first[l]+1; |
| 184 | 185 |
} |
| 185 |
///Return true if there is |
|
| 186 |
///Return true if there is no active item on level \c l. |
|
| 186 | 187 |
bool activeFree(int l) const |
| 187 | 188 |
{
|
| 188 | 189 |
return _last_active[l]<_first[l]; |
| 189 | 190 |
} |
| 190 | 191 |
///Return the maximum allowed level. |
| 191 | 192 |
int maxLevel() const |
| 192 | 193 |
{
|
| 193 | 194 |
return _max_level; |
| 194 | 195 |
} |
| 195 | 196 |
|
| 196 | 197 |
///\name Highest Active Item |
| 197 | 198 |
///Functions for working with the highest level |
| 198 | 199 |
///active item. |
| 199 | 200 |
|
| 200 | 201 |
///@{
|
| 201 | 202 |
|
| 202 | 203 |
///Return a highest level active item. |
| 203 | 204 |
|
| 204 |
///Return a highest level active item. |
|
| 205 |
/// |
|
| 206 |
/// |
|
| 205 |
///Return a highest level active item or INVALID if there is no active |
|
| 207 | 206 |
///item. |
| 208 | 207 |
Item highestActive() const |
| 209 | 208 |
{
|
| 210 | 209 |
return _highest_active>=0?*_last_active[_highest_active]:INVALID; |
| 211 | 210 |
} |
| 212 | 211 |
|
| 213 |
///Return |
|
| 212 |
///Return the highest active level. |
|
| 214 | 213 |
|
| 215 |
///Return a highest active level. |
|
| 216 |
/// |
|
| 217 |
/// |
|
| 214 |
///Return the level of the highest active item or -1 if there is no active |
|
| 218 | 215 |
///item. |
| 219 | 216 |
int highestActiveLevel() const |
| 220 | 217 |
{
|
| 221 | 218 |
return _highest_active; |
| 222 | 219 |
} |
| 223 | 220 |
|
| 224 | 221 |
///Lift the highest active item by one. |
| 225 | 222 |
|
| 226 | 223 |
///Lift the item returned by highestActive() by one. |
| 227 | 224 |
/// |
| 228 | 225 |
void liftHighestActive() |
| 229 | 226 |
{
|
| 230 | 227 |
Item it = *_last_active[_highest_active]; |
| 231 | 228 |
_level.set(it,_level[it]+1); |
| 232 | 229 |
swap(_last_active[_highest_active]--,_last_active[_highest_active+1]); |
| 233 | 230 |
--_first[++_highest_active]; |
| 234 | 231 |
} |
| 235 | 232 |
|
| 236 |
///Lift the highest active item. |
|
| 233 |
///Lift the highest active item to the given level. |
|
| 237 | 234 |
|
| 238 | 235 |
///Lift the item returned by highestActive() to level \c new_level. |
| 239 | 236 |
/// |
| 240 | 237 |
///\warning \c new_level must be strictly higher |
| 241 | 238 |
///than the current level. |
| 242 | 239 |
/// |
| 243 | 240 |
void liftHighestActive(int new_level) |
| 244 | 241 |
{
|
| 245 | 242 |
const Item li = *_last_active[_highest_active]; |
| 246 | 243 |
|
| 247 | 244 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
| 248 | 245 |
for(int l=_highest_active+1;l<new_level;l++) |
| 249 | 246 |
{
|
| 250 | 247 |
copy(--_first[l+1],_first[l]); |
| 251 | 248 |
--_last_active[l]; |
| 252 | 249 |
} |
| 253 | 250 |
copy(li,_first[new_level]); |
| 254 | 251 |
_level.set(li,new_level); |
| 255 | 252 |
_highest_active=new_level; |
| 256 | 253 |
} |
| 257 | 254 |
|
| 258 |
///Lift the highest active item. |
|
| 255 |
///Lift the highest active item to the top level. |
|
| 259 | 256 |
|
| 260 | 257 |
///Lift the item returned by highestActive() to the top level and |
| 261 |
///deactivates it. |
|
| 262 |
/// |
|
| 263 |
///\warning \c new_level must be strictly higher |
|
| 264 |
///than the current level. |
|
| 265 |
/// |
|
| 258 |
///deactivate it. |
|
| 266 | 259 |
void liftHighestActiveToTop() |
| 267 | 260 |
{
|
| 268 | 261 |
const Item li = *_last_active[_highest_active]; |
| 269 | 262 |
|
| 270 | 263 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
| 271 | 264 |
for(int l=_highest_active+1;l<_max_level;l++) |
| 272 | 265 |
{
|
| 273 | 266 |
copy(--_first[l+1],_first[l]); |
| 274 | 267 |
--_last_active[l]; |
| 275 | 268 |
} |
| 276 | 269 |
copy(li,_first[_max_level]); |
| 277 | 270 |
--_last_active[_max_level]; |
| ... | ... |
@@ -280,70 +273,68 @@ |
| 280 | 273 |
while(_highest_active>=0 && |
| 281 | 274 |
_last_active[_highest_active]<_first[_highest_active]) |
| 282 | 275 |
_highest_active--; |
| 283 | 276 |
} |
| 284 | 277 |
|
| 285 | 278 |
///@} |
| 286 | 279 |
|
| 287 | 280 |
///\name Active Item on Certain Level |
| 288 | 281 |
///Functions for working with the active items. |
| 289 | 282 |
|
| 290 | 283 |
///@{
|
| 291 | 284 |
|
| 292 |
/// |
|
| 285 |
///Return an active item on level \c l. |
|
| 293 | 286 |
|
| 294 |
///Returns an active item on level \c l. |
|
| 295 |
/// |
|
| 296 |
/// |
|
| 287 |
///Return an active item on level \c l or \ref INVALID if there is no such |
|
| 297 | 288 |
///an item. (\c l must be from the range [0...\c max_level]. |
| 298 | 289 |
Item activeOn(int l) const |
| 299 | 290 |
{
|
| 300 | 291 |
return _last_active[l]>=_first[l]?*_last_active[l]:INVALID; |
| 301 | 292 |
} |
| 302 | 293 |
|
| 303 |
/// |
|
| 294 |
///Lift the active item returned by \c activeOn(level) by one. |
|
| 304 | 295 |
|
| 305 |
/// |
|
| 296 |
///Lift the active item returned by \ref activeOn() "activeOn(level)" |
|
| 306 | 297 |
///by one. |
| 307 | 298 |
Item liftActiveOn(int level) |
| 308 | 299 |
{
|
| 309 | 300 |
Item it =*_last_active[level]; |
| 310 | 301 |
_level.set(it,_level[it]+1); |
| 311 | 302 |
swap(_last_active[level]--, --_first[level+1]); |
| 312 | 303 |
if (level+1>_highest_active) ++_highest_active; |
| 313 | 304 |
} |
| 314 | 305 |
|
| 315 |
/// |
|
| 306 |
///Lift the active item returned by \c activeOn(level) to the given level. |
|
| 316 | 307 |
|
| 317 |
/// |
|
| 308 |
///Lift the active item returned by \ref activeOn() "activeOn(level)" |
|
| 318 | 309 |
///to the given level. |
| 319 | 310 |
void liftActiveOn(int level, int new_level) |
| 320 | 311 |
{
|
| 321 | 312 |
const Item ai = *_last_active[level]; |
| 322 | 313 |
|
| 323 | 314 |
copy(--_first[level+1], _last_active[level]--); |
| 324 | 315 |
for(int l=level+1;l<new_level;l++) |
| 325 | 316 |
{
|
| 326 | 317 |
copy(_last_active[l],_first[l]); |
| 327 | 318 |
copy(--_first[l+1], _last_active[l]--); |
| 328 | 319 |
} |
| 329 | 320 |
copy(ai,_first[new_level]); |
| 330 | 321 |
_level.set(ai,new_level); |
| 331 | 322 |
if (new_level>_highest_active) _highest_active=new_level; |
| 332 | 323 |
} |
| 333 | 324 |
|
| 334 |
/// |
|
| 325 |
///Lift the active item returned by \c activeOn(level) to the top level. |
|
| 335 | 326 |
|
| 336 |
///Lifts the active item returned by \c activeOn() member function |
|
| 337 |
///to the top level. |
|
| 327 |
///Lift the active item returned by \ref activeOn() "activeOn(level)" |
|
| 328 |
///to the top level and deactivate it. |
|
| 338 | 329 |
void liftActiveToTop(int level) |
| 339 | 330 |
{
|
| 340 | 331 |
const Item ai = *_last_active[level]; |
| 341 | 332 |
|
| 342 | 333 |
copy(--_first[level+1],_last_active[level]--); |
| 343 | 334 |
for(int l=level+1;l<_max_level;l++) |
| 344 | 335 |
{
|
| 345 | 336 |
copy(_last_active[l],_first[l]); |
| 346 | 337 |
copy(--_first[l+1], _last_active[l]--); |
| 347 | 338 |
} |
| 348 | 339 |
copy(ai,_first[_max_level]); |
| 349 | 340 |
--_last_active[_max_level]; |
| ... | ... |
@@ -375,110 +366,107 @@ |
| 375 | 366 |
for(int l=lo+1;l<new_level;l++) |
| 376 | 367 |
{
|
| 377 | 368 |
copy(_last_active[l],_first[l]); |
| 378 | 369 |
copy(--_first[l+1],_last_active[l]--); |
| 379 | 370 |
} |
| 380 | 371 |
copy(i,_first[new_level]); |
| 381 | 372 |
_level.set(i,new_level); |
| 382 | 373 |
if(new_level>_highest_active) _highest_active=new_level; |
| 383 | 374 |
} |
| 384 | 375 |
|
| 385 | 376 |
///Move an inactive item to the top but one level (in a dirty way). |
| 386 | 377 |
|
| 387 |
///This function moves an inactive item to the top but one level. |
|
| 388 |
///It makes the underlying datastructure corrupt, so use is only if |
|
| 389 |
/// |
|
| 378 |
///This function moves an inactive item from the top level to the top |
|
| 379 |
///but one level (in a dirty way). |
|
| 380 |
///\warning It makes the underlying datastructure corrupt, so use it |
|
| 381 |
///only if you really know what it is for. |
|
| 390 | 382 |
///\pre The item is on the top level. |
| 391 | 383 |
void dirtyTopButOne(Item i) {
|
| 392 | 384 |
_level.set(i,_max_level - 1); |
| 393 | 385 |
} |
| 394 | 386 |
|
| 395 |
///Lift all items on and above |
|
| 387 |
///Lift all items on and above the given level to the top level. |
|
| 396 | 388 |
|
| 397 |
///This function lifts all items on and above level \c l to \c |
|
| 398 |
///maxLevel(), and also deactivates them. |
|
| 389 |
///This function lifts all items on and above level \c l to the top |
|
| 390 |
///level and deactivates them. |
|
| 399 | 391 |
void liftToTop(int l) |
| 400 | 392 |
{
|
| 401 | 393 |
const Vit f=_first[l]; |
| 402 | 394 |
const Vit tl=_first[_max_level]; |
| 403 | 395 |
for(Vit i=f;i!=tl;++i) |
| 404 | 396 |
_level.set(*i,_max_level); |
| 405 | 397 |
for(int i=l;i<=_max_level;i++) |
| 406 | 398 |
{
|
| 407 | 399 |
_first[i]=f; |
| 408 | 400 |
_last_active[i]=f-1; |
| 409 | 401 |
} |
| 410 | 402 |
for(_highest_active=l-1; |
| 411 | 403 |
_highest_active>=0 && |
| 412 | 404 |
_last_active[_highest_active]<_first[_highest_active]; |
| 413 | 405 |
_highest_active--) ; |
| 414 | 406 |
} |
| 415 | 407 |
|
| 416 | 408 |
private: |
| 417 | 409 |
int _init_lev; |
| 418 | 410 |
Vit _init_num; |
| 419 | 411 |
|
| 420 | 412 |
public: |
| 421 | 413 |
|
| 422 | 414 |
///\name Initialization |
| 423 |
///Using |
|
| 415 |
///Using these functions you can initialize the levels of the items. |
|
| 424 | 416 |
///\n |
| 425 |
///This initializatios is started with calling \c initStart(). |
|
| 426 |
///Then the |
|
| 427 |
///items should be listed levels by levels statring with the lowest one |
|
| 428 |
///(with level 0). This is done by using \c initAddItem() |
|
| 429 |
///and \c initNewLevel(). Finally \c initFinish() must be called. |
|
| 430 |
///The items not listed will be put on the highest level. |
|
| 417 |
///The initialization must be started with calling \c initStart(). |
|
| 418 |
///Then the items should be listed level by level starting with the |
|
| 419 |
///lowest one (level 0) using \c initAddItem() and \c initNewLevel(). |
|
| 420 |
///Finally \c initFinish() must be called. |
|
| 421 |
///The items not listed are put on the highest level. |
|
| 431 | 422 |
///@{
|
| 432 | 423 |
|
| 433 | 424 |
///Start the initialization process. |
| 434 |
|
|
| 435 | 425 |
void initStart() |
| 436 | 426 |
{
|
| 437 | 427 |
_init_lev=0; |
| 438 | 428 |
_init_num=&_items[0]; |
| 439 | 429 |
_first[0]=&_items[0]; |
| 440 | 430 |
_last_active[0]=&_items[0]-1; |
| 441 | 431 |
Vit n=&_items[0]; |
| 442 | 432 |
for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i) |
| 443 | 433 |
{
|
| 444 | 434 |
*n=i; |
| 445 | 435 |
_where.set(i,n); |
| 446 | 436 |
_level.set(i,_max_level); |
| 447 | 437 |
++n; |
| 448 | 438 |
} |
| 449 | 439 |
} |
| 450 | 440 |
|
| 451 | 441 |
///Add an item to the current level. |
| 452 |
|
|
| 453 | 442 |
void initAddItem(Item i) |
| 454 | 443 |
{
|
| 455 | 444 |
swap(_where[i],_init_num); |
| 456 | 445 |
_level.set(i,_init_lev); |
| 457 | 446 |
++_init_num; |
| 458 | 447 |
} |
| 459 | 448 |
|
| 460 | 449 |
///Start a new level. |
| 461 | 450 |
|
| 462 | 451 |
///Start a new level. |
| 463 | 452 |
///It shouldn't be used before the items on level 0 are listed. |
| 464 | 453 |
void initNewLevel() |
| 465 | 454 |
{
|
| 466 | 455 |
_init_lev++; |
| 467 | 456 |
_first[_init_lev]=_init_num; |
| 468 | 457 |
_last_active[_init_lev]=_init_num-1; |
| 469 | 458 |
} |
| 470 | 459 |
|
| 471 | 460 |
///Finalize the initialization process. |
| 472 |
|
|
| 473 | 461 |
void initFinish() |
| 474 | 462 |
{
|
| 475 | 463 |
for(_init_lev++;_init_lev<=_max_level;_init_lev++) |
| 476 | 464 |
{
|
| 477 | 465 |
_first[_init_lev]=_init_num; |
| 478 | 466 |
_last_active[_init_lev]=_init_num-1; |
| 479 | 467 |
} |
| 480 | 468 |
_first[_max_level+1]=&_items[0]+_item_num; |
| 481 | 469 |
_last_active[_max_level+1]=&_items[0]+_item_num-1; |
| 482 | 470 |
_highest_active = -1; |
| 483 | 471 |
} |
| 484 | 472 |
|
| ... | ... |
@@ -491,27 +479,27 @@ |
| 491 | 479 |
///A class for handling "labels" in push-relabel type algorithms. |
| 492 | 480 |
/// |
| 493 | 481 |
///\ingroup auxdat |
| 494 | 482 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
| 495 | 483 |
///to the edges or nodes of a graph, manipulate and query them through |
| 496 | 484 |
///operations typically arising in "push-relabel" type algorithms. |
| 497 | 485 |
/// |
| 498 | 486 |
///Each item is either \em active or not, and you can also choose a |
| 499 | 487 |
///highest level active item. |
| 500 | 488 |
/// |
| 501 | 489 |
///\sa Elevator |
| 502 | 490 |
/// |
| 503 |
///\param Graph the underlying graph |
|
| 491 |
///\param Graph Type of the underlying graph. |
|
| 504 | 492 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
| 505 |
///Graph:: |
|
| 493 |
///Graph::Arc, Graph::Edge). |
|
| 506 | 494 |
template <class Graph, class Item> |
| 507 | 495 |
class LinkedElevator {
|
| 508 | 496 |
public: |
| 509 | 497 |
|
| 510 | 498 |
typedef Item Key; |
| 511 | 499 |
typedef int Value; |
| 512 | 500 |
|
| 513 | 501 |
private: |
| 514 | 502 |
|
| 515 | 503 |
typedef typename ItemSetTraits<Graph,Item>:: |
| 516 | 504 |
template Map<Item>::Type ItemMap; |
| 517 | 505 |
typedef typename ItemSetTraits<Graph,Item>:: |
| ... | ... |
@@ -524,39 +512,39 @@ |
| 524 | 512 |
int _item_num; |
| 525 | 513 |
std::vector<Item> _first, _last; |
| 526 | 514 |
ItemMap _prev, _next; |
| 527 | 515 |
int _highest_active; |
| 528 | 516 |
IntMap _level; |
| 529 | 517 |
BoolMap _active; |
| 530 | 518 |
|
| 531 | 519 |
public: |
| 532 | 520 |
///Constructor with given maximum level. |
| 533 | 521 |
|
| 534 | 522 |
///Constructor with given maximum level. |
| 535 | 523 |
/// |
| 536 |
///\param g The underlying graph |
|
| 537 |
///\param max_level Set the range of the possible labels to |
|
| 538 |
/// |
|
| 524 |
///\param graph The underlying graph. |
|
| 525 |
///\param max_level The maximum allowed level. |
|
| 526 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
|
| 539 | 527 |
LinkedElevator(const Graph& graph, int max_level) |
| 540 | 528 |
: _graph(graph), _max_level(max_level), _item_num(_max_level), |
| 541 | 529 |
_first(_max_level + 1), _last(_max_level + 1), |
| 542 | 530 |
_prev(graph), _next(graph), |
| 543 | 531 |
_highest_active(-1), _level(graph), _active(graph) {}
|
| 544 | 532 |
|
| 545 | 533 |
///Constructor. |
| 546 | 534 |
|
| 547 | 535 |
///Constructor. |
| 548 | 536 |
/// |
| 549 |
///\param g The underlying graph |
|
| 550 |
///The range of the possible labels is [0...\c max_level], |
|
| 537 |
///\param graph The underlying graph. |
|
| 538 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>, |
|
| 551 | 539 |
///where \c max_level is equal to the number of labeled items in the graph. |
| 552 | 540 |
LinkedElevator(const Graph& graph) |
| 553 | 541 |
: _graph(graph), _max_level(countItems<Graph, Item>(graph)), |
| 554 | 542 |
_item_num(_max_level), |
| 555 | 543 |
_first(_max_level + 1), _last(_max_level + 1), |
| 556 | 544 |
_prev(graph, INVALID), _next(graph, INVALID), |
| 557 | 545 |
_highest_active(-1), _level(graph), _active(graph) {}
|
| 558 | 546 |
|
| 559 | 547 |
|
| 560 | 548 |
///Activate item \c i. |
| 561 | 549 |
|
| 562 | 550 |
///Activate item \c i. |
| ... | ... |
@@ -648,56 +636,52 @@ |
| 648 | 636 |
|
| 649 | 637 |
///Return the number of active items on level \c l. |
| 650 | 638 |
int activesOnLevel(int l) const {
|
| 651 | 639 |
int num = 0; |
| 652 | 640 |
Item n = _first[l]; |
| 653 | 641 |
while (n != INVALID && _active[n]) {
|
| 654 | 642 |
++num; |
| 655 | 643 |
n = _next[n]; |
| 656 | 644 |
} |
| 657 | 645 |
return num; |
| 658 | 646 |
} |
| 659 | 647 |
|
| 660 |
///Return true if there is |
|
| 648 |
///Return true if there is no active item on level \c l. |
|
| 661 | 649 |
bool activeFree(int l) const {
|
| 662 | 650 |
return _first[l] == INVALID || !_active[_first[l]]; |
| 663 | 651 |
} |
| 664 | 652 |
|
| 665 | 653 |
///Return the maximum allowed level. |
| 666 | 654 |
int maxLevel() const {
|
| 667 | 655 |
return _max_level; |
| 668 | 656 |
} |
| 669 | 657 |
|
| 670 | 658 |
///\name Highest Active Item |
| 671 | 659 |
///Functions for working with the highest level |
| 672 | 660 |
///active item. |
| 673 | 661 |
|
| 674 | 662 |
///@{
|
| 675 | 663 |
|
| 676 | 664 |
///Return a highest level active item. |
| 677 | 665 |
|
| 678 |
///Return a highest level active item. |
|
| 679 |
/// |
|
| 680 |
///\return the highest level active item or INVALID if there is no |
|
| 681 |
///active item. |
|
| 666 |
///Return a highest level active item or INVALID if there is no active |
|
| 667 |
///item. |
|
| 682 | 668 |
Item highestActive() const {
|
| 683 | 669 |
return _highest_active >= 0 ? _first[_highest_active] : INVALID; |
| 684 | 670 |
} |
| 685 | 671 |
|
| 686 |
///Return |
|
| 672 |
///Return the highest active level. |
|
| 687 | 673 |
|
| 688 |
///Return a highest active level. |
|
| 689 |
/// |
|
| 690 |
///\return the level of the highest active item or -1 if there is |
|
| 691 |
///no active item. |
|
| 674 |
///Return the level of the highest active item or -1 if there is no active |
|
| 675 |
///item. |
|
| 692 | 676 |
int highestActiveLevel() const {
|
| 693 | 677 |
return _highest_active; |
| 694 | 678 |
} |
| 695 | 679 |
|
| 696 | 680 |
///Lift the highest active item by one. |
| 697 | 681 |
|
| 698 | 682 |
///Lift the item returned by highestActive() by one. |
| 699 | 683 |
/// |
| 700 | 684 |
void liftHighestActive() {
|
| 701 | 685 |
Item i = _first[_highest_active]; |
| 702 | 686 |
if (_next[i] != INVALID) {
|
| 703 | 687 |
_prev.set(_next[i], INVALID); |
| ... | ... |
@@ -710,25 +694,25 @@ |
| 710 | 694 |
if (_first[_highest_active] == INVALID) {
|
| 711 | 695 |
_first[_highest_active] = i; |
| 712 | 696 |
_last[_highest_active] = i; |
| 713 | 697 |
_prev.set(i, INVALID); |
| 714 | 698 |
_next.set(i, INVALID); |
| 715 | 699 |
} else {
|
| 716 | 700 |
_prev.set(_first[_highest_active], i); |
| 717 | 701 |
_next.set(i, _first[_highest_active]); |
| 718 | 702 |
_first[_highest_active] = i; |
| 719 | 703 |
} |
| 720 | 704 |
} |
| 721 | 705 |
|
| 722 |
///Lift the highest active item. |
|
| 706 |
///Lift the highest active item to the given level. |
|
| 723 | 707 |
|
| 724 | 708 |
///Lift the item returned by highestActive() to level \c new_level. |
| 725 | 709 |
/// |
| 726 | 710 |
///\warning \c new_level must be strictly higher |
| 727 | 711 |
///than the current level. |
| 728 | 712 |
/// |
| 729 | 713 |
void liftHighestActive(int new_level) {
|
| 730 | 714 |
Item i = _first[_highest_active]; |
| 731 | 715 |
if (_next[i] != INVALID) {
|
| 732 | 716 |
_prev.set(_next[i], INVALID); |
| 733 | 717 |
_first[_highest_active] = _next[i]; |
| 734 | 718 |
} else {
|
| ... | ... |
@@ -738,64 +722,61 @@ |
| 738 | 722 |
_level.set(i, _highest_active = new_level); |
| 739 | 723 |
if (_first[_highest_active] == INVALID) {
|
| 740 | 724 |
_first[_highest_active] = _last[_highest_active] = i; |
| 741 | 725 |
_prev.set(i, INVALID); |
| 742 | 726 |
_next.set(i, INVALID); |
| 743 | 727 |
} else {
|
| 744 | 728 |
_prev.set(_first[_highest_active], i); |
| 745 | 729 |
_next.set(i, _first[_highest_active]); |
| 746 | 730 |
_first[_highest_active] = i; |
| 747 | 731 |
} |
| 748 | 732 |
} |
| 749 | 733 |
|
| 750 |
///Lift the highest active to top. |
|
| 734 |
///Lift the highest active item to the top level. |
|
| 751 | 735 |
|
| 752 | 736 |
///Lift the item returned by highestActive() to the top level and |
| 753 |
///deactivates the item. |
|
| 754 |
/// |
|
| 737 |
///deactivate it. |
|
| 755 | 738 |
void liftHighestActiveToTop() {
|
| 756 | 739 |
Item i = _first[_highest_active]; |
| 757 | 740 |
_level.set(i, _max_level); |
| 758 | 741 |
if (_next[i] != INVALID) {
|
| 759 | 742 |
_prev.set(_next[i], INVALID); |
| 760 | 743 |
_first[_highest_active] = _next[i]; |
| 761 | 744 |
} else {
|
| 762 | 745 |
_first[_highest_active] = INVALID; |
| 763 | 746 |
_last[_highest_active] = INVALID; |
| 764 | 747 |
} |
| 765 | 748 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
| 766 | 749 |
--_highest_active; |
| 767 | 750 |
} |
| 768 | 751 |
|
| 769 | 752 |
///@} |
| 770 | 753 |
|
| 771 | 754 |
///\name Active Item on Certain Level |
| 772 | 755 |
///Functions for working with the active items. |
| 773 | 756 |
|
| 774 | 757 |
///@{
|
| 775 | 758 |
|
| 776 |
/// |
|
| 759 |
///Return an active item on level \c l. |
|
| 777 | 760 |
|
| 778 |
///Returns an active item on level \c l. |
|
| 779 |
/// |
|
| 780 |
/// |
|
| 761 |
///Return an active item on level \c l or \ref INVALID if there is no such |
|
| 781 | 762 |
///an item. (\c l must be from the range [0...\c max_level]. |
| 782 | 763 |
Item activeOn(int l) const |
| 783 | 764 |
{
|
| 784 | 765 |
return _active[_first[l]] ? _first[l] : INVALID; |
| 785 | 766 |
} |
| 786 | 767 |
|
| 787 |
/// |
|
| 768 |
///Lift the active item returned by \c activeOn(l) by one. |
|
| 788 | 769 |
|
| 789 |
/// |
|
| 770 |
///Lift the active item returned by \ref activeOn() "activeOn(l)" |
|
| 790 | 771 |
///by one. |
| 791 | 772 |
Item liftActiveOn(int l) |
| 792 | 773 |
{
|
| 793 | 774 |
Item i = _first[l]; |
| 794 | 775 |
if (_next[i] != INVALID) {
|
| 795 | 776 |
_prev.set(_next[i], INVALID); |
| 796 | 777 |
_first[l] = _next[i]; |
| 797 | 778 |
} else {
|
| 798 | 779 |
_first[l] = INVALID; |
| 799 | 780 |
_last[l] = INVALID; |
| 800 | 781 |
} |
| 801 | 782 |
_level.set(i, ++l); |
| ... | ... |
@@ -804,27 +785,27 @@ |
| 804 | 785 |
_prev.set(i, INVALID); |
| 805 | 786 |
_next.set(i, INVALID); |
| 806 | 787 |
} else {
|
| 807 | 788 |
_prev.set(_first[l], i); |
| 808 | 789 |
_next.set(i, _first[l]); |
| 809 | 790 |
_first[l] = i; |
| 810 | 791 |
} |
| 811 | 792 |
if (_highest_active < l) {
|
| 812 | 793 |
_highest_active = l; |
| 813 | 794 |
} |
| 814 | 795 |
} |
| 815 | 796 |
|
| 816 |
/// \brief Lifts the active item returned by \c activeOn() member function. |
|
| 817 |
/// |
|
| 818 |
/// |
|
| 797 |
///Lift the active item returned by \c activeOn(l) to the given level. |
|
| 798 |
|
|
| 799 |
///Lift the active item returned by \ref activeOn() "activeOn(l)" |
|
| 819 | 800 |
/// to the given level. |
| 820 | 801 |
void liftActiveOn(int l, int new_level) |
| 821 | 802 |
{
|
| 822 | 803 |
Item i = _first[l]; |
| 823 | 804 |
if (_next[i] != INVALID) {
|
| 824 | 805 |
_prev.set(_next[i], INVALID); |
| 825 | 806 |
_first[l] = _next[i]; |
| 826 | 807 |
} else {
|
| 827 | 808 |
_first[l] = INVALID; |
| 828 | 809 |
_last[l] = INVALID; |
| 829 | 810 |
} |
| 830 | 811 |
_level.set(i, l = new_level); |
| ... | ... |
@@ -833,28 +814,28 @@ |
| 833 | 814 |
_prev.set(i, INVALID); |
| 834 | 815 |
_next.set(i, INVALID); |
| 835 | 816 |
} else {
|
| 836 | 817 |
_prev.set(_first[l], i); |
| 837 | 818 |
_next.set(i, _first[l]); |
| 838 | 819 |
_first[l] = i; |
| 839 | 820 |
} |
| 840 | 821 |
if (_highest_active < l) {
|
| 841 | 822 |
_highest_active = l; |
| 842 | 823 |
} |
| 843 | 824 |
} |
| 844 | 825 |
|
| 845 |
/// |
|
| 826 |
///Lift the active item returned by \c activeOn(l) to the top level. |
|
| 846 | 827 |
|
| 847 |
///Lifts the active item returned by \c activeOn() member function |
|
| 848 |
///to the top level. |
|
| 828 |
///Lift the active item returned by \ref activeOn() "activeOn(l)" |
|
| 829 |
///to the top level and deactivate it. |
|
| 849 | 830 |
void liftActiveToTop(int l) |
| 850 | 831 |
{
|
| 851 | 832 |
Item i = _first[l]; |
| 852 | 833 |
if (_next[i] != INVALID) {
|
| 853 | 834 |
_prev.set(_next[i], INVALID); |
| 854 | 835 |
_first[l] = _next[i]; |
| 855 | 836 |
} else {
|
| 856 | 837 |
_first[l] = INVALID; |
| 857 | 838 |
_last[l] = INVALID; |
| 858 | 839 |
} |
| 859 | 840 |
_level.set(i, _max_level); |
| 860 | 841 |
if (l == _highest_active) {
|
| ... | ... |
@@ -891,112 +872,109 @@ |
| 891 | 872 |
} else {
|
| 892 | 873 |
_prev.set(_first[new_level], i); |
| 893 | 874 |
_next.set(i, _first[new_level]); |
| 894 | 875 |
_first[new_level] = i; |
| 895 | 876 |
} |
| 896 | 877 |
if (_highest_active < new_level) {
|
| 897 | 878 |
_highest_active = new_level; |
| 898 | 879 |
} |
| 899 | 880 |
} |
| 900 | 881 |
|
| 901 | 882 |
///Move an inactive item to the top but one level (in a dirty way). |
| 902 | 883 |
|
| 903 |
///This function moves an inactive item to the top but one level. |
|
| 904 |
///It makes the underlying datastructure corrupt, so use is only if |
|
| 905 |
/// |
|
| 884 |
///This function moves an inactive item from the top level to the top |
|
| 885 |
///but one level (in a dirty way). |
|
| 886 |
///\warning It makes the underlying datastructure corrupt, so use it |
|
| 887 |
///only if you really know what it is for. |
|
| 906 | 888 |
///\pre The item is on the top level. |
| 907 | 889 |
void dirtyTopButOne(Item i) {
|
| 908 | 890 |
_level.set(i, _max_level - 1); |
| 909 | 891 |
} |
| 910 | 892 |
|
| 911 |
///Lift all items on and above |
|
| 893 |
///Lift all items on and above the given level to the top level. |
|
| 912 | 894 |
|
| 913 |
///This function lifts all items on and above level \c l to \c |
|
| 914 |
///maxLevel(), and also deactivates them. |
|
| 895 |
///This function lifts all items on and above level \c l to the top |
|
| 896 |
///level and deactivates them. |
|
| 915 | 897 |
void liftToTop(int l) {
|
| 916 | 898 |
for (int i = l + 1; _first[i] != INVALID; ++i) {
|
| 917 | 899 |
Item n = _first[i]; |
| 918 | 900 |
while (n != INVALID) {
|
| 919 | 901 |
_level.set(n, _max_level); |
| 920 | 902 |
n = _next[n]; |
| 921 | 903 |
} |
| 922 | 904 |
_first[i] = INVALID; |
| 923 | 905 |
_last[i] = INVALID; |
| 924 | 906 |
} |
| 925 | 907 |
if (_highest_active > l - 1) {
|
| 926 | 908 |
_highest_active = l - 1; |
| 927 | 909 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
| 928 | 910 |
--_highest_active; |
| 929 | 911 |
} |
| 930 | 912 |
} |
| 931 | 913 |
|
| 932 | 914 |
private: |
| 933 | 915 |
|
| 934 | 916 |
int _init_level; |
| 935 | 917 |
|
| 936 | 918 |
public: |
| 937 | 919 |
|
| 938 | 920 |
///\name Initialization |
| 939 |
///Using |
|
| 921 |
///Using these functions you can initialize the levels of the items. |
|
| 940 | 922 |
///\n |
| 941 |
///This initializatios is started with calling \c initStart(). |
|
| 942 |
///Then the |
|
| 943 |
///items should be listed levels by levels statring with the lowest one |
|
| 944 |
///(with level 0). This is done by using \c initAddItem() |
|
| 945 |
///and \c initNewLevel(). Finally \c initFinish() must be called. |
|
| 946 |
///The items not listed will be put on the highest level. |
|
| 923 |
///The initialization must be started with calling \c initStart(). |
|
| 924 |
///Then the items should be listed level by level starting with the |
|
| 925 |
///lowest one (level 0) using \c initAddItem() and \c initNewLevel(). |
|
| 926 |
///Finally \c initFinish() must be called. |
|
| 927 |
///The items not listed are put on the highest level. |
|
| 947 | 928 |
///@{
|
| 948 | 929 |
|
| 949 | 930 |
///Start the initialization process. |
| 950 |
|
|
| 951 | 931 |
void initStart() {
|
| 952 | 932 |
|
| 953 | 933 |
for (int i = 0; i <= _max_level; ++i) {
|
| 954 | 934 |
_first[i] = _last[i] = INVALID; |
| 955 | 935 |
} |
| 956 | 936 |
_init_level = 0; |
| 957 | 937 |
for(typename ItemSetTraits<Graph,Item>::ItemIt i(_graph); |
| 958 | 938 |
i != INVALID; ++i) {
|
| 959 | 939 |
_level.set(i, _max_level); |
| 960 | 940 |
_active.set(i, false); |
| 961 | 941 |
} |
| 962 | 942 |
} |
| 963 | 943 |
|
| 964 | 944 |
///Add an item to the current level. |
| 965 |
|
|
| 966 | 945 |
void initAddItem(Item i) {
|
| 967 | 946 |
_level.set(i, _init_level); |
| 968 | 947 |
if (_last[_init_level] == INVALID) {
|
| 969 | 948 |
_first[_init_level] = i; |
| 970 | 949 |
_last[_init_level] = i; |
| 971 | 950 |
_prev.set(i, INVALID); |
| 972 | 951 |
_next.set(i, INVALID); |
| 973 | 952 |
} else {
|
| 974 | 953 |
_prev.set(i, _last[_init_level]); |
| 975 | 954 |
_next.set(i, INVALID); |
| 976 | 955 |
_next.set(_last[_init_level], i); |
| 977 | 956 |
_last[_init_level] = i; |
| 978 | 957 |
} |
| 979 | 958 |
} |
| 980 | 959 |
|
| 981 | 960 |
///Start a new level. |
| 982 | 961 |
|
| 983 | 962 |
///Start a new level. |
| 984 | 963 |
///It shouldn't be used before the items on level 0 are listed. |
| 985 | 964 |
void initNewLevel() {
|
| 986 | 965 |
++_init_level; |
| 987 | 966 |
} |
| 988 | 967 |
|
| 989 | 968 |
///Finalize the initialization process. |
| 990 |
|
|
| 991 | 969 |
void initFinish() {
|
| 992 | 970 |
_highest_active = -1; |
| 993 | 971 |
} |
| 994 | 972 |
|
| 995 | 973 |
///@} |
| 996 | 974 |
|
| 997 | 975 |
}; |
| 998 | 976 |
|
| 999 | 977 |
|
| 1000 | 978 |
} //END OF NAMESPACE LEMON |
| 1001 | 979 |
|
| 1002 | 980 |
#endif |
0 comments (0 inline)