| ... | ... |
@@ -65,41 +65,41 @@ |
| 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 |
_where |
|
| 77 |
_where.set(*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 |
_where |
|
| 85 |
_where.set(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 |
_where[ti]=_where[*i=*j]; |
|
| 93 |
_where[*j]=ct; |
|
| 92 |
_where.set(ti,_where[*i=*j]); |
|
| 93 |
_where.set(*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] |
| ... | ... |
@@ -218,72 +218,73 @@ |
| 218 | 218 |
///item. |
| 219 | 219 |
int highestActiveLevel() const |
| 220 | 220 |
{
|
| 221 | 221 |
return _highest_active; |
| 222 | 222 |
} |
| 223 | 223 |
|
| 224 | 224 |
///Lift the highest active item by one. |
| 225 | 225 |
|
| 226 | 226 |
///Lift the item returned by highestActive() by one. |
| 227 | 227 |
/// |
| 228 | 228 |
void liftHighestActive() |
| 229 | 229 |
{
|
| 230 |
|
|
| 230 |
Item it = *_last_active[_highest_active]; |
|
| 231 |
_level.set(it,_level[it]+1); |
|
| 231 | 232 |
swap(_last_active[_highest_active]--,_last_active[_highest_active+1]); |
| 232 | 233 |
--_first[++_highest_active]; |
| 233 | 234 |
} |
| 234 | 235 |
|
| 235 | 236 |
///Lift the highest active item. |
| 236 | 237 |
|
| 237 | 238 |
///Lift the item returned by highestActive() to level \c new_level. |
| 238 | 239 |
/// |
| 239 | 240 |
///\warning \c new_level must be strictly higher |
| 240 | 241 |
///than the current level. |
| 241 | 242 |
/// |
| 242 | 243 |
void liftHighestActive(int new_level) |
| 243 | 244 |
{
|
| 244 | 245 |
const Item li = *_last_active[_highest_active]; |
| 245 | 246 |
|
| 246 | 247 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
| 247 | 248 |
for(int l=_highest_active+1;l<new_level;l++) |
| 248 | 249 |
{
|
| 249 | 250 |
copy(--_first[l+1],_first[l]); |
| 250 | 251 |
--_last_active[l]; |
| 251 | 252 |
} |
| 252 | 253 |
copy(li,_first[new_level]); |
| 253 |
_level |
|
| 254 |
_level.set(li,new_level); |
|
| 254 | 255 |
_highest_active=new_level; |
| 255 | 256 |
} |
| 256 | 257 |
|
| 257 | 258 |
///Lift the highest active item. |
| 258 | 259 |
|
| 259 | 260 |
///Lift the item returned by highestActive() to the top level and |
| 260 | 261 |
///deactivates it. |
| 261 | 262 |
/// |
| 262 | 263 |
///\warning \c new_level must be strictly higher |
| 263 | 264 |
///than the current level. |
| 264 | 265 |
/// |
| 265 | 266 |
void liftHighestActiveToTop() |
| 266 | 267 |
{
|
| 267 | 268 |
const Item li = *_last_active[_highest_active]; |
| 268 | 269 |
|
| 269 | 270 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
| 270 | 271 |
for(int l=_highest_active+1;l<_max_level;l++) |
| 271 | 272 |
{
|
| 272 | 273 |
copy(--_first[l+1],_first[l]); |
| 273 | 274 |
--_last_active[l]; |
| 274 | 275 |
} |
| 275 | 276 |
copy(li,_first[_max_level]); |
| 276 | 277 |
--_last_active[_max_level]; |
| 277 |
_level |
|
| 278 |
_level.set(li,_max_level); |
|
| 278 | 279 |
|
| 279 | 280 |
while(_highest_active>=0 && |
| 280 | 281 |
_last_active[_highest_active]<_first[_highest_active]) |
| 281 | 282 |
_highest_active--; |
| 282 | 283 |
} |
| 283 | 284 |
|
| 284 | 285 |
///@} |
| 285 | 286 |
|
| 286 | 287 |
///\name Active Item on Certain Level |
| 287 | 288 |
///Functions for working with the active items. |
| 288 | 289 |
|
| 289 | 290 |
///@{
|
| ... | ... |
@@ -296,65 +297,66 @@ |
| 296 | 297 |
///an item. (\c l must be from the range [0...\c max_level]. |
| 297 | 298 |
Item activeOn(int l) const |
| 298 | 299 |
{
|
| 299 | 300 |
return _last_active[l]>=_first[l]?*_last_active[l]:INVALID; |
| 300 | 301 |
} |
| 301 | 302 |
|
| 302 | 303 |
///Lifts the active item returned by \c activeOn() member function. |
| 303 | 304 |
|
| 304 | 305 |
///Lifts the active item returned by \c activeOn() member function |
| 305 | 306 |
///by one. |
| 306 | 307 |
Item liftActiveOn(int level) |
| 307 | 308 |
{
|
| 308 |
|
|
| 309 |
Item it =*_last_active[level]; |
|
| 310 |
_level.set(it,_level[it]+1); |
|
| 309 | 311 |
swap(_last_active[level]--, --_first[level+1]); |
| 310 | 312 |
if (level+1>_highest_active) ++_highest_active; |
| 311 | 313 |
} |
| 312 | 314 |
|
| 313 | 315 |
///Lifts the active item returned by \c activeOn() member function. |
| 314 | 316 |
|
| 315 | 317 |
///Lifts the active item returned by \c activeOn() member function |
| 316 | 318 |
///to the given level. |
| 317 | 319 |
void liftActiveOn(int level, int new_level) |
| 318 | 320 |
{
|
| 319 | 321 |
const Item ai = *_last_active[level]; |
| 320 | 322 |
|
| 321 | 323 |
copy(--_first[level+1], _last_active[level]--); |
| 322 | 324 |
for(int l=level+1;l<new_level;l++) |
| 323 | 325 |
{
|
| 324 | 326 |
copy(_last_active[l],_first[l]); |
| 325 | 327 |
copy(--_first[l+1], _last_active[l]--); |
| 326 | 328 |
} |
| 327 | 329 |
copy(ai,_first[new_level]); |
| 328 |
_level |
|
| 330 |
_level.set(ai,new_level); |
|
| 329 | 331 |
if (new_level>_highest_active) _highest_active=new_level; |
| 330 | 332 |
} |
| 331 | 333 |
|
| 332 | 334 |
///Lifts the active item returned by \c activeOn() member function. |
| 333 | 335 |
|
| 334 | 336 |
///Lifts the active item returned by \c activeOn() member function |
| 335 | 337 |
///to the top level. |
| 336 | 338 |
void liftActiveToTop(int level) |
| 337 | 339 |
{
|
| 338 | 340 |
const Item ai = *_last_active[level]; |
| 339 | 341 |
|
| 340 | 342 |
copy(--_first[level+1],_last_active[level]--); |
| 341 | 343 |
for(int l=level+1;l<_max_level;l++) |
| 342 | 344 |
{
|
| 343 | 345 |
copy(_last_active[l],_first[l]); |
| 344 | 346 |
copy(--_first[l+1], _last_active[l]--); |
| 345 | 347 |
} |
| 346 | 348 |
copy(ai,_first[_max_level]); |
| 347 | 349 |
--_last_active[_max_level]; |
| 348 |
_level |
|
| 350 |
_level.set(ai,_max_level); |
|
| 349 | 351 |
|
| 350 | 352 |
if (_highest_active==level) {
|
| 351 | 353 |
while(_highest_active>=0 && |
| 352 | 354 |
_last_active[_highest_active]<_first[_highest_active]) |
| 353 | 355 |
_highest_active--; |
| 354 | 356 |
} |
| 355 | 357 |
} |
| 356 | 358 |
|
| 357 | 359 |
///@} |
| 358 | 360 |
|
| 359 | 361 |
///Lift an active item to a higher level. |
| 360 | 362 |
|
| ... | ... |
@@ -367,48 +369,48 @@ |
| 367 | 369 |
{
|
| 368 | 370 |
const int lo = _level[i]; |
| 369 | 371 |
const Vit w = _where[i]; |
| 370 | 372 |
|
| 371 | 373 |
copy(_last_active[lo],w); |
| 372 | 374 |
copy(--_first[lo+1],_last_active[lo]--); |
| 373 | 375 |
for(int l=lo+1;l<new_level;l++) |
| 374 | 376 |
{
|
| 375 | 377 |
copy(_last_active[l],_first[l]); |
| 376 | 378 |
copy(--_first[l+1],_last_active[l]--); |
| 377 | 379 |
} |
| 378 | 380 |
copy(i,_first[new_level]); |
| 379 |
_level |
|
| 381 |
_level.set(i,new_level); |
|
| 380 | 382 |
if(new_level>_highest_active) _highest_active=new_level; |
| 381 | 383 |
} |
| 382 | 384 |
|
| 383 | 385 |
///Move an inactive item to the top but one level (in a dirty way). |
| 384 | 386 |
|
| 385 | 387 |
///This function moves an inactive item to the top but one level. |
| 386 | 388 |
///It makes the underlying datastructure corrupt, so use is only if |
| 387 | 389 |
///you really know what it is for. |
| 388 | 390 |
///\pre The item is on the top level. |
| 389 | 391 |
void dirtyTopButOne(Item i) {
|
| 390 |
_level |
|
| 392 |
_level.set(i,_max_level - 1); |
|
| 391 | 393 |
} |
| 392 | 394 |
|
| 393 | 395 |
///Lift all items on and above a level to the top (and deactivate them). |
| 394 | 396 |
|
| 395 | 397 |
///This function lifts all items on and above level \c l to \c |
| 396 | 398 |
///maxLevel(), and also deactivates them. |
| 397 | 399 |
void liftToTop(int l) |
| 398 | 400 |
{
|
| 399 | 401 |
const Vit f=_first[l]; |
| 400 | 402 |
const Vit tl=_first[_max_level]; |
| 401 | 403 |
for(Vit i=f;i!=tl;++i) |
| 402 |
_level |
|
| 404 |
_level.set(*i,_max_level); |
|
| 403 | 405 |
for(int i=l;i<=_max_level;i++) |
| 404 | 406 |
{
|
| 405 | 407 |
_first[i]=f; |
| 406 | 408 |
_last_active[i]=f-1; |
| 407 | 409 |
} |
| 408 | 410 |
for(_highest_active=l-1; |
| 409 | 411 |
_highest_active>=0 && |
| 410 | 412 |
_last_active[_highest_active]<_first[_highest_active]; |
| 411 | 413 |
_highest_active--) ; |
| 412 | 414 |
} |
| 413 | 415 |
|
| 414 | 416 |
private: |
| ... | ... |
@@ -431,36 +433,36 @@ |
| 431 | 433 |
///Start the initialization process. |
| 432 | 434 |
|
| 433 | 435 |
void initStart() |
| 434 | 436 |
{
|
| 435 | 437 |
_init_lev=0; |
| 436 | 438 |
_init_num=&_items[0]; |
| 437 | 439 |
_first[0]=&_items[0]; |
| 438 | 440 |
_last_active[0]=&_items[0]-1; |
| 439 | 441 |
Vit n=&_items[0]; |
| 440 | 442 |
for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i) |
| 441 | 443 |
{
|
| 442 | 444 |
*n=i; |
| 443 |
_where[i]=n; |
|
| 444 |
_level[i]=_max_level; |
|
| 445 |
_where.set(i,n); |
|
| 446 |
_level.set(i,_max_level); |
|
| 445 | 447 |
++n; |
| 446 | 448 |
} |
| 447 | 449 |
} |
| 448 | 450 |
|
| 449 | 451 |
///Add an item to the current level. |
| 450 | 452 |
|
| 451 | 453 |
void initAddItem(Item i) |
| 452 | 454 |
{
|
| 453 |
swap(_where[i],_init_num); |
|
| 454 |
_level[i]=_init_lev; |
|
| 455 |
swap(_where[i],_init_num); |
|
| 456 |
_level.set(i,_init_lev); |
|
| 455 | 457 |
++_init_num; |
| 456 | 458 |
} |
| 457 | 459 |
|
| 458 | 460 |
///Start a new level. |
| 459 | 461 |
|
| 460 | 462 |
///Start a new level. |
| 461 | 463 |
///It shouldn't be used before the items on level 0 are listed. |
| 462 | 464 |
void initNewLevel() |
| 463 | 465 |
{
|
| 464 | 466 |
_init_lev++; |
| 465 | 467 |
_first[_init_lev]=_init_num; |
| 466 | 468 |
_last_active[_init_lev]=_init_num-1; |
0 comments (0 inline)