lemon/elevator.h
author deba
Wed, 14 Nov 2007 17:44:42 +0000
changeset 2512 371cf309fc3c
parent 2391 14a343be7a5a
child 2518 4c0a23bd70b5
permissions -rw-r--r--
Elevator: slight changes in elevator interface
LinkedElevator: based on linked lists
alpar@2346
     1
/* -*- C++ -*-
alpar@2346
     2
 *
alpar@2346
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2346
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@2346
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2346
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2346
     8
 *
alpar@2346
     9
 * Permission to use, modify and distribute this software is granted
alpar@2346
    10
 * provided that this copyright notice appears in all copies. For
alpar@2346
    11
 * precise terms see the accompanying LICENSE file.
alpar@2346
    12
 *
alpar@2346
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2346
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2346
    15
 * purpose.
alpar@2346
    16
 *
alpar@2346
    17
 */
alpar@2346
    18
alpar@2346
    19
#ifndef LEMON_ELEVATOR_H
alpar@2346
    20
#define LEMON_ELEVATOR_H
alpar@2346
    21
alpar@2346
    22
///\ingroup auxdat
alpar@2346
    23
///\file
alpar@2346
    24
///\brief Elevator class
alpar@2346
    25
///
alpar@2346
    26
///Elevator class implements an efficient data structure
alpar@2346
    27
///for labeling items in push-relabel type algorithms.
alpar@2346
    28
///
alpar@2346
    29
alpar@2346
    30
#include <test/test_tools.h>
alpar@2346
    31
namespace lemon {
alpar@2346
    32
alpar@2347
    33
  ///Class for handling "labels" in push-relabel type algorithms.
alpar@2346
    34
  
alpar@2347
    35
  ///A class for handling "labels" in push-relabel type algorithms.
alpar@2346
    36
  ///
alpar@2346
    37
  ///\ingroup auxdat
alpar@2352
    38
  ///Using this class you can assign "labels" (nonnegative integer numbers)
alpar@2346
    39
  ///to the edges or nodes of a graph, manipulate and query them through
alpar@2346
    40
  ///operations typically arising in "push-relabel" type algorithms.
alpar@2346
    41
  ///
alpar@2346
    42
  ///Each item is either \em active or not, and you can also choose a
alpar@2346
    43
  ///highest level active item.
alpar@2346
    44
  ///
deba@2512
    45
  ///\sa LinkedElevator
deba@2512
    46
  ///
alpar@2346
    47
  ///\param Graph the underlying graph type
alpar@2346
    48
  ///\param Item Type of the items the data is assigned to (Graph::Node,
alpar@2346
    49
  ///Graph::Edge, Graph::UEdge)
alpar@2346
    50
  template<class Graph, class Item>
alpar@2346
    51
  class Elevator 
alpar@2346
    52
  {
deba@2512
    53
  private:
deba@2512
    54
deba@2512
    55
    typedef Item Key;
deba@2512
    56
    typedef int Value;
deba@2512
    57
alpar@2346
    58
    typedef typename std::vector<Item>::iterator Vit;
deba@2512
    59
    typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap;
deba@2512
    60
    typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap;
alpar@2346
    61
alpar@2346
    62
    const Graph &_g;
alpar@2346
    63
    int _max_level;
alpar@2346
    64
    int _item_num;
alpar@2346
    65
    VitMap _where;
alpar@2346
    66
    IntMap _level;
alpar@2346
    67
    std::vector<Item> _items;
alpar@2346
    68
    std::vector<Vit> _first;
alpar@2346
    69
    std::vector<Vit> _last_active;
alpar@2346
    70
alpar@2346
    71
    int _highest_active;
alpar@2346
    72
alpar@2346
    73
    void copy(Item i, Vit p)
alpar@2346
    74
    {
alpar@2346
    75
      _where[*p=i]=p;
alpar@2346
    76
    }
alpar@2346
    77
    void copy(Vit s, Vit p)
alpar@2346
    78
    {
alpar@2346
    79
      if(s!=p) 
alpar@2346
    80
	{
alpar@2346
    81
	  Item i=*s;
alpar@2346
    82
	  *p=i;
alpar@2346
    83
	  _where[i]=p;
alpar@2346
    84
	}
alpar@2346
    85
    }
alpar@2346
    86
    void swap(Vit i, Vit j)
alpar@2346
    87
    {
alpar@2346
    88
      Item ti=*i;
alpar@2346
    89
      Vit ct = _where[ti];
alpar@2346
    90
      _where[ti]=_where[*i=*j];
alpar@2346
    91
      _where[*j]=ct;
alpar@2346
    92
      *j=ti;
alpar@2346
    93
    }
alpar@2346
    94
    
alpar@2346
    95
  public:
deba@2512
    96
        
alpar@2346
    97
    ///Constructor with given maximum level.
alpar@2346
    98
alpar@2346
    99
    ///Constructor with given maximum level.
alpar@2346
   100
    ///
alpar@2346
   101
    ///\param g The underlying graph
alpar@2346
   102
    ///\param max_level Set the range of the possible labels to
alpar@2346
   103
    ///[0...\c max_level]
alpar@2346
   104
    Elevator(const Graph &g,int max_level) :
alpar@2346
   105
      _g(g),
alpar@2346
   106
      _max_level(max_level),
alpar@2346
   107
      _item_num(_max_level),
alpar@2346
   108
      _where(g),
alpar@2346
   109
      _level(g,0),
alpar@2346
   110
      _items(_max_level),
alpar@2346
   111
      _first(_max_level+2),
alpar@2346
   112
      _last_active(_max_level+2),
alpar@2346
   113
      _highest_active(-1) {}
alpar@2346
   114
    ///Constructor.
alpar@2346
   115
alpar@2346
   116
    ///Constructor.
alpar@2346
   117
    ///
alpar@2346
   118
    ///\param g The underlying graph
alpar@2346
   119
    ///The range of the possible labels is [0...\c max_level],
alpar@2346
   120
    ///where \c max_level is equal to the number of labeled items in the graph.
alpar@2346
   121
    Elevator(const Graph &g) :
alpar@2346
   122
      _g(g),
alpar@2346
   123
      _max_level(countItems<Graph, Item>(g)),
alpar@2346
   124
      _item_num(_max_level),
alpar@2346
   125
      _where(g),
alpar@2346
   126
      _level(g,0),
alpar@2346
   127
      _items(_max_level),
alpar@2346
   128
      _first(_max_level+2),
alpar@2346
   129
      _last_active(_max_level+2),
alpar@2346
   130
      _highest_active(-1)
alpar@2346
   131
    {
alpar@2346
   132
    }
deba@2512
   133
    
alpar@2346
   134
    ///Activate item \c i.
alpar@2373
   135
alpar@2373
   136
    ///Activate item \c i.
alpar@2373
   137
    ///\pre Item \c i shouldn't be active before.
alpar@2346
   138
    void activate(Item i)
alpar@2346
   139
    {
alpar@2346
   140
      const int l=_level[i];
alpar@2346
   141
      swap(_where[i],++_last_active[l]);
alpar@2346
   142
      if(l>_highest_active) _highest_active=l;
alpar@2346
   143
    }
alpar@2346
   144
  
alpar@2346
   145
    ///Deactivate item \c i.
alpar@2373
   146
alpar@2373
   147
    ///Deactivate item \c i.
alpar@2373
   148
    ///\pre Item \c i must be active before.
alpar@2346
   149
    void deactivate(Item i)  
alpar@2346
   150
    {
alpar@2346
   151
      swap(_where[i],_last_active[_level[i]]--);
alpar@2346
   152
      while(_highest_active>=0 &&
alpar@2346
   153
	    _last_active[_highest_active]<_first[_highest_active])
alpar@2346
   154
	_highest_active--;
alpar@2346
   155
    }
alpar@2346
   156
alpar@2346
   157
    ///Query whether item \c i is active
alpar@2346
   158
    bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; }
alpar@2346
   159
    
alpar@2346
   160
    ///Return the level of item \c i.
alpar@2346
   161
    int operator[](Item i) const { return _level[i]; }
alpar@2346
   162
alpar@2346
   163
    ///Return the number of items on level \c l.
alpar@2348
   164
    int onLevel(int l) const
alpar@2346
   165
    {
alpar@2346
   166
      return _first[l+1]-_first[l];
alpar@2346
   167
    }
deba@2512
   168
    ///Return true if the level is empty.
deba@2512
   169
    bool emptyLevel(int l) const
deba@2512
   170
    {
deba@2512
   171
      return _first[l+1]-_first[l]==0;
deba@2512
   172
    }
alpar@2348
   173
    ///Return the number of items above level \c l.
alpar@2348
   174
    int aboveLevel(int l) const
alpar@2348
   175
    {
alpar@2348
   176
      return _first[_max_level+1]-_first[l+1];
alpar@2348
   177
    }
alpar@2346
   178
    ///Return the number of active items on level \c l.
alpar@2348
   179
    int activesOnLevel(int l) const
alpar@2346
   180
    {
alpar@2346
   181
      return _last_active[l]-_first[l]+1;
alpar@2346
   182
    }
deba@2512
   183
    ///Return true if there is not active item on level \c l.
deba@2512
   184
    bool activeFree(int l) const
deba@2512
   185
    {
deba@2512
   186
      return _last_active[l]<_first[l];
deba@2512
   187
    }
alpar@2346
   188
    ///Return the maximum allowed level.
alpar@2346
   189
    int maxLevel() const 
alpar@2346
   190
    {
alpar@2346
   191
      return _max_level;
alpar@2346
   192
    }    
alpar@2346
   193
alpar@2346
   194
    ///\name Highest Active Item
alpar@2346
   195
    ///Functions for working with the highest level
alpar@2346
   196
    ///active item.
alpar@2346
   197
alpar@2346
   198
    ///@{
alpar@2346
   199
alpar@2346
   200
    ///Return a highest level active item.
alpar@2346
   201
  
alpar@2346
   202
    ///Return a highest level active item.
alpar@2346
   203
    ///
alpar@2346
   204
    ///\return the highest level active item or INVALID if there is no active
alpar@2346
   205
    ///item.
alpar@2346
   206
    Item highestActive() const 
alpar@2346
   207
    {
alpar@2346
   208
      return _highest_active>=0?*_last_active[_highest_active]:INVALID;
alpar@2346
   209
    }
alpar@2346
   210
alpar@2346
   211
    ///Return a highest active level.
alpar@2346
   212
alpar@2346
   213
    ///Return a highest active level.
alpar@2346
   214
    ///
alpar@2346
   215
    ///\return the level of the highest active item or -1 if there is no active
alpar@2346
   216
    ///item.
athos@2349
   217
    int highestActiveLevel() const 
alpar@2346
   218
    {
alpar@2346
   219
      return _highest_active;
alpar@2346
   220
    }
alpar@2346
   221
alpar@2346
   222
    ///Lift the highest active item by one.
alpar@2346
   223
alpar@2346
   224
    ///Lift the item returned by highestActive() by one.
alpar@2346
   225
    ///
alpar@2346
   226
    void liftHighestActive() 
alpar@2346
   227
    {
alpar@2346
   228
      ++_level[*_last_active[_highest_active]];
alpar@2346
   229
      swap(_last_active[_highest_active]--,_last_active[_highest_active+1]);
alpar@2346
   230
      --_first[++_highest_active];
alpar@2346
   231
    }
alpar@2346
   232
alpar@2346
   233
    ///Lift the highest active item.
alpar@2346
   234
alpar@2346
   235
    ///Lift the item returned by highestActive() to level \c new_level.
alpar@2346
   236
    ///
alpar@2348
   237
    ///\warning \c new_level must be strictly higher
alpar@2348
   238
    ///than the current level.
alpar@2346
   239
    ///
deba@2512
   240
    void liftHighestActive(int new_level) 
alpar@2346
   241
    {
alpar@2346
   242
      const Item li = *_last_active[_highest_active];
alpar@2346
   243
      
alpar@2346
   244
      copy(--_first[_highest_active+1],_last_active[_highest_active]--);
alpar@2346
   245
      for(int l=_highest_active+1;l<new_level;l++)
alpar@2346
   246
	{
alpar@2346
   247
	  copy(--_first[l+1],_first[l]);
alpar@2346
   248
	  --_last_active[l];
alpar@2346
   249
	}
alpar@2346
   250
      copy(li,_first[new_level]);
alpar@2346
   251
      _level[li]=new_level;
alpar@2348
   252
      _highest_active=new_level;
alpar@2346
   253
    }
deba@2512
   254
deba@2512
   255
    ///Lift the highest active item.
deba@2512
   256
deba@2512
   257
    ///Lift the item returned by highestActive() to the top level and
deba@2512
   258
    ///deactivates it.
deba@2512
   259
    ///
deba@2512
   260
    ///\warning \c new_level must be strictly higher
deba@2512
   261
    ///than the current level.
deba@2512
   262
    ///
deba@2512
   263
    void liftHighestActiveToTop() 
deba@2512
   264
    {
deba@2512
   265
      const Item li = *_last_active[_highest_active];
deba@2512
   266
      
deba@2512
   267
      copy(--_first[_highest_active+1],_last_active[_highest_active]--);
deba@2512
   268
      for(int l=_highest_active+1;l<_max_level;l++)
deba@2512
   269
	{
deba@2512
   270
	  copy(--_first[l+1],_first[l]);
deba@2512
   271
	  --_last_active[l];
deba@2512
   272
	}
deba@2512
   273
      copy(li,_first[_max_level]);
deba@2512
   274
      --_last_active[_max_level];
deba@2512
   275
      _level[li]=_max_level;
deba@2512
   276
deba@2512
   277
      while(_highest_active>=0 &&
deba@2512
   278
	    _last_active[_highest_active]<_first[_highest_active])
deba@2512
   279
	_highest_active--;
deba@2512
   280
    }
alpar@2346
   281
    
alpar@2346
   282
    ///@}
alpar@2346
   283
    
deba@2512
   284
    ///\name Active Item on Certain Level 
deba@2512
   285
    ///Functions for working with the active items.
deba@2512
   286
deba@2512
   287
    ///@{
deba@2512
   288
deba@2512
   289
    ///Returns an active item on level \c l.
deba@2512
   290
    
deba@2512
   291
    ///Returns an active item on level \c l.
deba@2512
   292
    ///
deba@2512
   293
    ///Returns an active item on level \c l or \ref INVALID if there is no such
deba@2512
   294
    ///an item. (\c l must be from the range [0...\c max_level].
deba@2512
   295
    Item activeOn(int l) const
deba@2512
   296
    { 
deba@2512
   297
      return _last_active[l]>=_first[l]?*_last_active[l]:INVALID;
deba@2512
   298
    }
deba@2512
   299
deba@2512
   300
    ///Lifts the active item returned by \c activeOn() member function.
deba@2512
   301
    
deba@2512
   302
    ///Lifts the active item returned by \c activeOn() member function
deba@2512
   303
    ///by one.
deba@2512
   304
    Item liftActiveOn(int level)
deba@2512
   305
    { 
deba@2512
   306
      ++_level[*_last_active[level]];
deba@2512
   307
      swap(_last_active[level]--, --_first[level+1]);
deba@2512
   308
      if (level+1>_highest_active) ++_highest_active;
deba@2512
   309
    }
deba@2512
   310
deba@2512
   311
    ///Lifts the active item returned by \c activeOn() member function.
deba@2512
   312
    
deba@2512
   313
    ///Lifts the active item returned by \c activeOn() member function
deba@2512
   314
    ///to the given level.
deba@2512
   315
    void liftActiveOn(int level, int new_level)
deba@2512
   316
    { 
deba@2512
   317
      const Item ai = *_last_active[level];
deba@2512
   318
      
deba@2512
   319
      copy(--_first[level+1], _last_active[level]--);
deba@2512
   320
      for(int l=level+1;l<new_level;l++)
deba@2512
   321
	{
deba@2512
   322
	  copy(_last_active[l],_first[l]);
deba@2512
   323
	  copy(--_first[l+1], _last_active[l]--);
deba@2512
   324
	}
deba@2512
   325
      copy(ai,_first[new_level]);
deba@2512
   326
      _level[ai]=new_level;
deba@2512
   327
      if (new_level>_highest_active) _highest_active=new_level;
deba@2512
   328
    }
deba@2512
   329
deba@2512
   330
    ///Lifts the active item returned by \c activeOn() member function.
deba@2512
   331
    
deba@2512
   332
    ///Lifts the active item returned by \c activeOn() member function
deba@2512
   333
    ///to the top level.
deba@2512
   334
    void liftActiveToTop(int level)
deba@2512
   335
    {
deba@2512
   336
      const Item ai = *_last_active[level];
deba@2512
   337
      
deba@2512
   338
      copy(--_first[level+1],_last_active[level]--);
deba@2512
   339
      for(int l=level+1;l<_max_level;l++)
deba@2512
   340
	{
deba@2512
   341
	  copy(_last_active[l],_first[l]);
deba@2512
   342
	  copy(--_first[l+1], _last_active[l]--);
deba@2512
   343
	}
deba@2512
   344
      copy(ai,_first[_max_level]);
deba@2512
   345
      --_last_active[_max_level];
deba@2512
   346
      _level[ai]=_max_level;
deba@2512
   347
deba@2512
   348
      if (_highest_active==level) {
deba@2512
   349
	while(_highest_active>=0 && 
deba@2512
   350
	      _last_active[_highest_active]<_first[_highest_active]) 
deba@2512
   351
	  _highest_active--;
deba@2512
   352
      }
deba@2512
   353
    }
deba@2512
   354
deba@2512
   355
    ///@}
deba@2512
   356
    
alpar@2348
   357
    ///Lift an active item to a higher level.
alpar@2348
   358
alpar@2348
   359
    ///Lift an active item to a higher level.
alpar@2350
   360
    ///\param i The item to be lifted. It must be active.
alpar@2350
   361
    ///\param new_level The new level of \c i. It must be strictly higher
alpar@2348
   362
    ///than the current level.
alpar@2348
   363
    ///
deba@2512
   364
    void lift(Item i, int new_level) 
alpar@2348
   365
    {
alpar@2348
   366
      const int lo = _level[i];
alpar@2348
   367
      const Vit w = _where[i];
alpar@2348
   368
alpar@2348
   369
      copy(_last_active[lo],w);
alpar@2348
   370
      copy(--_first[lo+1],_last_active[lo]--);
alpar@2348
   371
      for(int l=lo+1;l<new_level;l++)
alpar@2348
   372
	{
alpar@2348
   373
	  copy(_last_active[l],_first[l]);
alpar@2348
   374
	  copy(--_first[l+1],_last_active[l]--);
alpar@2348
   375
	}
alpar@2348
   376
      copy(i,_first[new_level]);
alpar@2348
   377
      _level[i]=new_level;
alpar@2348
   378
      if(new_level>_highest_active) _highest_active=new_level;
alpar@2348
   379
    }
alpar@2348
   380
    
alpar@2348
   381
    ///Lift all nodes on and above a level to the top (and deactivate them).
alpar@2348
   382
deba@2512
   383
    ///This function lifts all nodes on and above level \c l to \c
deba@2512
   384
    ///maxLevel(), and also deactivates them.
alpar@2348
   385
    void liftToTop(int l) 
alpar@2348
   386
    {
alpar@2348
   387
      const Vit f=_first[l];
alpar@2348
   388
      const Vit tl=_first[_max_level];
alpar@2348
   389
      for(Vit i=f;i!=tl;++i)
alpar@2348
   390
	_level[*i]=_max_level;
alpar@2348
   391
      for(int i=l;i<=_max_level;i++)
alpar@2348
   392
	{
alpar@2348
   393
	  _first[i]=f;
alpar@2348
   394
	  _last_active[i]=f-1;
alpar@2348
   395
	}
alpar@2348
   396
      for(_highest_active=l-1;
alpar@2348
   397
	  _highest_active>=0 &&
alpar@2348
   398
	    _last_active[_highest_active]<_first[_highest_active];
alpar@2348
   399
	  _highest_active--) ;
alpar@2348
   400
    }
alpar@2348
   401
    
alpar@2346
   402
  private:
alpar@2346
   403
    int _init_lev;
alpar@2346
   404
    Vit _init_num;
alpar@2346
   405
alpar@2346
   406
  public:
alpar@2346
   407
alpar@2346
   408
    ///\name Initialization
alpar@2346
   409
    ///Using this function you can initialize the levels of the item.
alpar@2346
   410
    ///\n
alpar@2346
   411
    ///This initializatios is started with calling \c initStart().
alpar@2346
   412
    ///Then the
alpar@2346
   413
    ///items should be listed levels by levels statring with the lowest one
alpar@2346
   414
    ///(with level 0). This is done by using \c initAddItem()
alpar@2346
   415
    ///and \c initNewLevel(). Finally \c initFinish() must be called.
alpar@2348
   416
    ///The items not listed will be put on the highest level.
alpar@2346
   417
    ///@{
alpar@2346
   418
alpar@2348
   419
    ///Start the initialization process.
alpar@2346
   420
alpar@2346
   421
    void initStart() 
alpar@2346
   422
    {
alpar@2346
   423
      _init_lev=0;
alpar@2346
   424
      _init_num=_items.begin();
alpar@2346
   425
      _first[0]=_items.begin();
alpar@2346
   426
      _last_active[0]=_items.begin()-1;
alpar@2346
   427
      Vit n=_items.begin();
alpar@2346
   428
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i)
alpar@2346
   429
	{
alpar@2346
   430
	  *n=i;
alpar@2346
   431
	  _where[i]=n;
alpar@2346
   432
	  _level[i]=_max_level;
alpar@2346
   433
	  ++n;
alpar@2346
   434
	}
alpar@2346
   435
    }
alpar@2346
   436
alpar@2346
   437
    ///Add an item to the current level.
alpar@2346
   438
alpar@2346
   439
    void initAddItem(Item i)
alpar@2346
   440
    { 
alpar@2346
   441
     swap(_where[i],_init_num);
alpar@2346
   442
      _level[i]=_init_lev;
alpar@2346
   443
      ++_init_num;
alpar@2346
   444
    }
alpar@2346
   445
alpar@2346
   446
    ///Start a new level.
alpar@2346
   447
alpar@2346
   448
    ///Start a new level.
alpar@2346
   449
    ///It shouldn't be used before the items on level 0 are listed.
alpar@2346
   450
    void initNewLevel() 
alpar@2346
   451
    {
alpar@2346
   452
      _init_lev++;
alpar@2346
   453
      _first[_init_lev]=_init_num;
alpar@2346
   454
      _last_active[_init_lev]=_init_num-1;
alpar@2346
   455
    }
alpar@2346
   456
alpar@2348
   457
    ///Finalize the initialization process.
alpar@2346
   458
alpar@2346
   459
    void initFinish()
alpar@2346
   460
    {
alpar@2346
   461
      for(_init_lev++;_init_lev<=_max_level;_init_lev++)
alpar@2346
   462
	{
alpar@2346
   463
	  _first[_init_lev]=_init_num;
alpar@2346
   464
	  _last_active[_init_lev]=_init_num-1;
alpar@2346
   465
	}
alpar@2346
   466
      _first[_max_level+1]=_items.begin()+_item_num;
alpar@2346
   467
      _last_active[_max_level+1]=_items.begin()+_item_num-1;
deba@2512
   468
      _highest_active = -1;
alpar@2346
   469
    }
alpar@2346
   470
alpar@2346
   471
    ///@}
alpar@2346
   472
alpar@2346
   473
  };
alpar@2346
   474
deba@2512
   475
  ///Class for handling "labels" in push-relabel type algorithms.
deba@2512
   476
  
deba@2512
   477
  ///A class for handling "labels" in push-relabel type algorithms.
deba@2512
   478
  ///
deba@2512
   479
  ///\ingroup auxdat
deba@2512
   480
  ///Using this class you can assign "labels" (nonnegative integer numbers)
deba@2512
   481
  ///to the edges or nodes of a graph, manipulate and query them through
deba@2512
   482
  ///operations typically arising in "push-relabel" type algorithms.
deba@2512
   483
  ///
deba@2512
   484
  ///Each item is either \em active or not, and you can also choose a
deba@2512
   485
  ///highest level active item.
deba@2512
   486
  ///
deba@2512
   487
  ///\sa Elevator
deba@2512
   488
  ///
deba@2512
   489
  ///\param Graph the underlying graph type
deba@2512
   490
  ///\param Item Type of the items the data is assigned to (Graph::Node,
deba@2512
   491
  ///Graph::Edge, Graph::UEdge)
deba@2512
   492
  template <class Graph, class Item>
deba@2512
   493
  class LinkedElevator {
deba@2512
   494
  private:
deba@2512
   495
deba@2512
   496
    typedef Item Key;
deba@2512
   497
    typedef int Value;
deba@2512
   498
deba@2512
   499
    typedef typename ItemSetTraits<Graph,Item>::
deba@2512
   500
    template Map<Item>::Type ItemMap;
deba@2512
   501
    typedef typename ItemSetTraits<Graph,Item>::
deba@2512
   502
    template Map<int>::Type IntMap;
deba@2512
   503
    typedef typename ItemSetTraits<Graph,Item>::
deba@2512
   504
    template Map<bool>::Type BoolMap;
deba@2512
   505
deba@2512
   506
    const Graph &_graph;
deba@2512
   507
    int _max_level;
deba@2512
   508
    int _item_num;
deba@2512
   509
    std::vector<Item> _first, _last;
deba@2512
   510
    ItemMap _prev, _next;    
deba@2512
   511
    int _highest_active;
deba@2512
   512
    IntMap _level;
deba@2512
   513
    BoolMap _active;
deba@2512
   514
    
deba@2512
   515
  public:
deba@2512
   516
    ///Constructor with given maximum level.
deba@2512
   517
deba@2512
   518
    ///Constructor with given maximum level.
deba@2512
   519
    ///
deba@2512
   520
    ///\param g The underlying graph
deba@2512
   521
    ///\param max_level Set the range of the possible labels to
deba@2512
   522
    ///[0...\c max_level]
deba@2512
   523
    LinkedElevator(const Graph& graph, int max_level) 
deba@2512
   524
      : _graph(graph), _max_level(max_level), _item_num(_max_level), 
deba@2512
   525
	_first(_max_level + 1), _last(_max_level + 1),
deba@2512
   526
	_prev(graph), _next(graph),      
deba@2512
   527
	_highest_active(-1), _level(graph), _active(graph) {}
deba@2512
   528
deba@2512
   529
    ///Constructor.
deba@2512
   530
deba@2512
   531
    ///Constructor.
deba@2512
   532
    ///
deba@2512
   533
    ///\param g The underlying graph
deba@2512
   534
    ///The range of the possible labels is [0...\c max_level],
deba@2512
   535
    ///where \c max_level is equal to the number of labeled items in the graph.
deba@2512
   536
    LinkedElevator(const Graph& graph) 
deba@2512
   537
      : _graph(graph), _max_level(countItems<Graph, Item>(graph)), 
deba@2512
   538
	_item_num(_max_level), 
deba@2512
   539
	_first(_max_level + 1), _last(_max_level + 1),
deba@2512
   540
	_prev(graph, INVALID), _next(graph, INVALID),      
deba@2512
   541
	_highest_active(-1), _level(graph), _active(graph) {}
deba@2512
   542
  
deba@2512
   543
deba@2512
   544
    ///Activate item \c i.
deba@2512
   545
deba@2512
   546
    ///Activate item \c i.
deba@2512
   547
    ///\pre Item \c i shouldn't be active before.
deba@2512
   548
    void activate(Item i) {
deba@2512
   549
      _active.set(i, true);
deba@2512
   550
deba@2512
   551
      int level = _level[i];
deba@2512
   552
      if (level > _highest_active) {
deba@2512
   553
	_highest_active = level;
deba@2512
   554
      }
deba@2512
   555
deba@2512
   556
      if (_prev[i] == INVALID || _active[_prev[i]]) return;	    
deba@2512
   557
      //unlace
deba@2512
   558
      _next.set(_prev[i], _next[i]);
deba@2512
   559
      if (_next[i] != INVALID) {
deba@2512
   560
	_prev.set(_next[i], _prev[i]);
deba@2512
   561
      } else {
deba@2512
   562
	_last[level] = _prev[i];
deba@2512
   563
      }
deba@2512
   564
      //lace
deba@2512
   565
      _next.set(i, _first[level]);
deba@2512
   566
      _prev.set(_first[level], i);
deba@2512
   567
      _prev.set(i, INVALID);
deba@2512
   568
      _first[level] = i;
deba@2512
   569
deba@2512
   570
    }
deba@2512
   571
  
deba@2512
   572
    ///Deactivate item \c i.
deba@2512
   573
deba@2512
   574
    ///Deactivate item \c i.
deba@2512
   575
    ///\pre Item \c i must be active before.
deba@2512
   576
    void deactivate(Item i) {
deba@2512
   577
      _active.set(i, false);
deba@2512
   578
      int level = _level[i];
deba@2512
   579
deba@2512
   580
      if (_next[i] == INVALID || !_active[_next[i]])
deba@2512
   581
	goto find_highest_level;
deba@2512
   582
deba@2512
   583
      //unlace
deba@2512
   584
      _prev.set(_next[i], _prev[i]);
deba@2512
   585
      if (_prev[i] != INVALID) {
deba@2512
   586
	_next.set(_prev[i], _next[i]);
deba@2512
   587
      } else {
deba@2512
   588
	_first[_level[i]] = _next[i];
deba@2512
   589
      }
deba@2512
   590
      //lace
deba@2512
   591
      _prev.set(i, _last[level]);
deba@2512
   592
      _next.set(_last[level], i);
deba@2512
   593
      _next.set(i, INVALID);
deba@2512
   594
      _last[level] = i;
deba@2512
   595
      
deba@2512
   596
    find_highest_level:
deba@2512
   597
      if (level == _highest_active) {
deba@2512
   598
	while (_highest_active >= 0 && activeFree(_highest_active)) 
deba@2512
   599
	  --_highest_active;
deba@2512
   600
      }
deba@2512
   601
    }
deba@2512
   602
deba@2512
   603
    ///Query whether item \c i is active
deba@2512
   604
    bool active(Item i) const { return _active[i]; }
deba@2512
   605
    
deba@2512
   606
    ///Return the level of item \c i.
deba@2512
   607
    int operator[](Item i) const { return _level[i]; }
deba@2512
   608
    
deba@2512
   609
    ///Return the number of items on level \c l.
deba@2512
   610
    int onLevel(int l) const {
deba@2512
   611
      int num = 0;
deba@2512
   612
      Item n = _first[l];
deba@2512
   613
      while (n != INVALID) {
deba@2512
   614
	++num;
deba@2512
   615
	n = _next[n];
deba@2512
   616
      }
deba@2512
   617
      return num;
deba@2512
   618
    }
deba@2512
   619
deba@2512
   620
    ///Return true if the level is empty.
deba@2512
   621
    bool emptyLevel(int l) const {
deba@2512
   622
      return _first[l] == INVALID;
deba@2512
   623
    }
deba@2512
   624
deba@2512
   625
    ///Return the number of items above level \c l.
deba@2512
   626
    int aboveLevel(int l) const {
deba@2512
   627
      int num = 0;
deba@2512
   628
      for (int level = l + 1; level < _max_level; ++level)
deba@2512
   629
	num += onLevel(level);
deba@2512
   630
      return num;
deba@2512
   631
    }
deba@2512
   632
deba@2512
   633
    ///Return the number of active items on level \c l.
deba@2512
   634
    int activesOnLevel(int l) const {
deba@2512
   635
      int num = 0;
deba@2512
   636
      Item n = _first[l];
deba@2512
   637
      while (n != INVALID && _active[n]) {
deba@2512
   638
	++num;
deba@2512
   639
	n = _next[n];
deba@2512
   640
      }
deba@2512
   641
      return num;
deba@2512
   642
    }
deba@2512
   643
deba@2512
   644
    ///Return true if there is not active item on level \c l.
deba@2512
   645
    bool activeFree(int l) const {
deba@2512
   646
      return _first[l] == INVALID || !_active[_first[l]];
deba@2512
   647
    }
deba@2512
   648
deba@2512
   649
    ///Return the maximum allowed level.
deba@2512
   650
    int maxLevel() const {
deba@2512
   651
      return _max_level;
deba@2512
   652
    }    
deba@2512
   653
deba@2512
   654
    ///\name Highest Active Item
deba@2512
   655
    ///Functions for working with the highest level
deba@2512
   656
    ///active item.
deba@2512
   657
deba@2512
   658
    ///@{
deba@2512
   659
deba@2512
   660
    ///Return a highest level active item.
deba@2512
   661
  
deba@2512
   662
    ///Return a highest level active item.
deba@2512
   663
    ///
deba@2512
   664
    ///\return the highest level active item or INVALID if there is no
deba@2512
   665
    ///active item.
deba@2512
   666
    Item highestActive() const {
deba@2512
   667
      return _highest_active >= 0 ? _first[_highest_active] : INVALID;
deba@2512
   668
    }
deba@2512
   669
deba@2512
   670
    ///Return a highest active level.
deba@2512
   671
deba@2512
   672
    ///Return a highest active level.
deba@2512
   673
    ///
deba@2512
   674
    ///\return the level of the highest active item or -1 if there is
deba@2512
   675
    ///no active item.
deba@2512
   676
    int highestActiveLevel() const {
deba@2512
   677
      return _highest_active;
deba@2512
   678
    }
deba@2512
   679
deba@2512
   680
    ///Lift the highest active item by one.
deba@2512
   681
deba@2512
   682
    ///Lift the item returned by highestActive() by one.
deba@2512
   683
    ///
deba@2512
   684
    void liftHighestActive() {
deba@2512
   685
      Item i = _first[_highest_active];
deba@2512
   686
      if (_next[i] != INVALID) {
deba@2512
   687
	_prev.set(_next[i], INVALID);
deba@2512
   688
	_first[_highest_active] = _next[i]; 
deba@2512
   689
      } else {
deba@2512
   690
	_first[_highest_active] = INVALID;
deba@2512
   691
	_last[_highest_active] = INVALID;
deba@2512
   692
      }
deba@2512
   693
      _level.set(i, ++_highest_active);
deba@2512
   694
      if (_first[_highest_active] == INVALID) {
deba@2512
   695
	_first[_highest_active] = i;
deba@2512
   696
	_last[_highest_active] = i;
deba@2512
   697
	_prev.set(i, INVALID);
deba@2512
   698
	_next.set(i, INVALID);
deba@2512
   699
      } else {
deba@2512
   700
	_prev.set(_first[_highest_active], i);
deba@2512
   701
	_next.set(i, _first[_highest_active]);
deba@2512
   702
	_first[_highest_active] = i;
deba@2512
   703
      }
deba@2512
   704
    }
deba@2512
   705
deba@2512
   706
    ///Lift the highest active item.
deba@2512
   707
deba@2512
   708
    ///Lift the item returned by highestActive() to level \c new_level.
deba@2512
   709
    ///
deba@2512
   710
    ///\warning \c new_level must be strictly higher
deba@2512
   711
    ///than the current level.
deba@2512
   712
    ///
deba@2512
   713
    void liftHighestActive(int new_level) {
deba@2512
   714
      Item i = _first[_highest_active];
deba@2512
   715
      if (_next[i] != INVALID) {
deba@2512
   716
	_prev.set(_next[i], INVALID);
deba@2512
   717
	_first[_highest_active] = _next[i]; 
deba@2512
   718
      } else {
deba@2512
   719
	_first[_highest_active] = INVALID;
deba@2512
   720
	_last[_highest_active] = INVALID;
deba@2512
   721
      }
deba@2512
   722
      _level.set(i, _highest_active = new_level);
deba@2512
   723
      if (_first[_highest_active] == INVALID) {
deba@2512
   724
	_first[_highest_active] = _last[_highest_active] = i;
deba@2512
   725
	_prev.set(i, INVALID);
deba@2512
   726
	_next.set(i, INVALID);
deba@2512
   727
      } else {
deba@2512
   728
	_prev.set(_first[_highest_active], i);
deba@2512
   729
	_next.set(i, _first[_highest_active]);
deba@2512
   730
	_first[_highest_active] = i;
deba@2512
   731
      }
deba@2512
   732
    }
deba@2512
   733
deba@2512
   734
    ///Lift the highest active to top.
deba@2512
   735
deba@2512
   736
    ///Lift the item returned by highestActive() to the top level and
deba@2512
   737
    ///deactivates the node.
deba@2512
   738
    ///
deba@2512
   739
    void liftHighestActiveToTop() {
deba@2512
   740
      Item i = _first[_highest_active];
deba@2512
   741
      _level.set(i, _max_level);
deba@2512
   742
      if (_next[i] != INVALID) {
deba@2512
   743
	_prev.set(_next[i], INVALID);
deba@2512
   744
	_first[_highest_active] = _next[i]; 
deba@2512
   745
      } else {
deba@2512
   746
	_first[_highest_active] = INVALID;
deba@2512
   747
	_last[_highest_active] = INVALID;
deba@2512
   748
      }
deba@2512
   749
      while (_highest_active >= 0 && activeFree(_highest_active)) 
deba@2512
   750
	--_highest_active;
deba@2512
   751
    }
deba@2512
   752
    
deba@2512
   753
    ///@}
deba@2512
   754
deba@2512
   755
    ///\name Active Item on Certain Level 
deba@2512
   756
    ///Functions for working with the active items.
deba@2512
   757
deba@2512
   758
    ///@{
deba@2512
   759
deba@2512
   760
    ///Returns an active item on level \c l.
deba@2512
   761
    
deba@2512
   762
    ///Returns an active item on level \c l.
deba@2512
   763
    ///
deba@2512
   764
    ///Returns an active item on level \c l or \ref INVALID if there is no such
deba@2512
   765
    ///an item. (\c l must be from the range [0...\c max_level].
deba@2512
   766
    Item activeOn(int l) const
deba@2512
   767
    { 
deba@2512
   768
      return _active[_first[l]] ? _first[l] : INVALID;
deba@2512
   769
    }
deba@2512
   770
deba@2512
   771
    ///Lifts the active item returned by \c activeOn() member function.
deba@2512
   772
    
deba@2512
   773
    ///Lifts the active item returned by \c activeOn() member function
deba@2512
   774
    ///by one.
deba@2512
   775
    Item liftActiveOn(int l)
deba@2512
   776
    { 
deba@2512
   777
      Item i = _first[l];
deba@2512
   778
      if (_next[i] != INVALID) {
deba@2512
   779
	_prev.set(_next[i], INVALID);
deba@2512
   780
	_first[l] = _next[i]; 
deba@2512
   781
      } else {
deba@2512
   782
	_first[l] = INVALID;
deba@2512
   783
	_last[l] = INVALID;
deba@2512
   784
      }
deba@2512
   785
      _level.set(i, ++l);
deba@2512
   786
      if (_first[l] == INVALID) {
deba@2512
   787
	_first[l] = _last[l] = i;
deba@2512
   788
	_prev.set(i, INVALID);
deba@2512
   789
	_next.set(i, INVALID);
deba@2512
   790
      } else {
deba@2512
   791
	_prev.set(_first[l], i);
deba@2512
   792
	_next.set(i, _first[l]);
deba@2512
   793
	_first[l] = i;
deba@2512
   794
      }
deba@2512
   795
      if (_highest_active < l) {
deba@2512
   796
	_highest_active = l;
deba@2512
   797
      }
deba@2512
   798
    }
deba@2512
   799
deba@2512
   800
    /// \brief Lifts the active item returned by \c activeOn() member function.
deba@2512
   801
    ///    
deba@2512
   802
    /// Lifts the active item returned by \c activeOn() member function
deba@2512
   803
    /// to the given level.
deba@2512
   804
    void liftActiveOn(int l, int new_level)
deba@2512
   805
    { 
deba@2512
   806
      Item i = _first[l];
deba@2512
   807
      if (_next[i] != INVALID) {
deba@2512
   808
	_prev.set(_next[i], INVALID);
deba@2512
   809
	_first[l] = _next[i]; 
deba@2512
   810
      } else {
deba@2512
   811
	_first[l] = INVALID;
deba@2512
   812
	_last[l] = INVALID;
deba@2512
   813
      }
deba@2512
   814
      _level.set(i, l = new_level);
deba@2512
   815
      if (_first[l] == INVALID) {
deba@2512
   816
	_first[l] = _last[l] = i;
deba@2512
   817
	_prev.set(i, INVALID);
deba@2512
   818
	_next.set(i, INVALID);
deba@2512
   819
      } else {
deba@2512
   820
	_prev.set(_first[l], i);
deba@2512
   821
	_next.set(i, _first[l]);
deba@2512
   822
	_first[l] = i;
deba@2512
   823
      }
deba@2512
   824
      if (_highest_active < l) {
deba@2512
   825
	_highest_active = l;
deba@2512
   826
      }
deba@2512
   827
    }
deba@2512
   828
deba@2512
   829
    ///Lifts the active item returned by \c activeOn() member function.
deba@2512
   830
    
deba@2512
   831
    ///Lifts the active item returned by \c activeOn() member function
deba@2512
   832
    ///to the top level.
deba@2512
   833
    void liftActiveToTop(int l)
deba@2512
   834
    { 
deba@2512
   835
      Item i = _first[l];
deba@2512
   836
      if (_next[i] != INVALID) {
deba@2512
   837
	_prev.set(_next[i], INVALID);
deba@2512
   838
	_first[l] = _next[i]; 
deba@2512
   839
      } else {
deba@2512
   840
	_first[l] = INVALID;
deba@2512
   841
	_last[l] = INVALID;
deba@2512
   842
      }
deba@2512
   843
      _level.set(i, _max_level);
deba@2512
   844
      if (l == _highest_active) {
deba@2512
   845
	while (_highest_active >= 0 && activeFree(_highest_active)) 
deba@2512
   846
	  --_highest_active;
deba@2512
   847
      }
deba@2512
   848
    }
deba@2512
   849
deba@2512
   850
    ///@}
deba@2512
   851
    
deba@2512
   852
    /// \brief Lift an active item to a higher level.
deba@2512
   853
    ///
deba@2512
   854
    /// Lift an active item to a higher level.
deba@2512
   855
    /// \param i The item to be lifted. It must be active.
deba@2512
   856
    /// \param new_level The new level of \c i. It must be strictly higher
deba@2512
   857
    /// than the current level.
deba@2512
   858
    ///
deba@2512
   859
    void lift(Item i, int new_level) {
deba@2512
   860
      if (_next[i] != INVALID) {
deba@2512
   861
	_prev.set(_next[i], _prev[i]);
deba@2512
   862
      } else {
deba@2512
   863
	_last[new_level] = _prev[i];
deba@2512
   864
      }
deba@2512
   865
      if (_prev[i] != INVALID) {
deba@2512
   866
	_next.set(_prev[i], _next[i]);
deba@2512
   867
      } else {
deba@2512
   868
	_first[new_level] = _next[i];
deba@2512
   869
      }
deba@2512
   870
      _level.set(i, new_level);
deba@2512
   871
      if (_first[new_level] == INVALID) {
deba@2512
   872
	_first[new_level] = _last[new_level] = i;
deba@2512
   873
	_prev.set(i, INVALID);
deba@2512
   874
	_next.set(i, INVALID);
deba@2512
   875
      } else {
deba@2512
   876
	_prev.set(_first[new_level], i);
deba@2512
   877
	_next.set(i, _first[new_level]);
deba@2512
   878
	_first[new_level] = i;
deba@2512
   879
      }
deba@2512
   880
      if (_highest_active < new_level) {
deba@2512
   881
	_highest_active = new_level;
deba@2512
   882
      }
deba@2512
   883
    }
deba@2512
   884
    
deba@2512
   885
    ///Lift all nodes on and above a level to the top (and deactivate them).
deba@2512
   886
deba@2512
   887
    ///This function lifts all nodes on and above level \c l to \c
deba@2512
   888
    ///maxLevel(), and also deactivates them. 
deba@2512
   889
    void liftToTop(int l)  {
deba@2512
   890
      for (int i = l + 1; _first[i] != INVALID; ++i) {
deba@2512
   891
	Item n = _first[i];
deba@2512
   892
	while (n != INVALID) {
deba@2512
   893
	  _level.set(n, _max_level);
deba@2512
   894
	  n = _next[n];
deba@2512
   895
	}
deba@2512
   896
	_first[i] = INVALID;
deba@2512
   897
	_last[i] = INVALID;
deba@2512
   898
      }
deba@2512
   899
      if (_highest_active > l - 1) {
deba@2512
   900
	_highest_active = l - 1;
deba@2512
   901
	while (_highest_active >= 0 && activeFree(_highest_active)) 
deba@2512
   902
	  --_highest_active;
deba@2512
   903
      }
deba@2512
   904
    }
deba@2512
   905
    
deba@2512
   906
  private:
deba@2512
   907
deba@2512
   908
    int _init_level;
deba@2512
   909
deba@2512
   910
  public:
deba@2512
   911
deba@2512
   912
    ///\name Initialization
deba@2512
   913
    ///Using this function you can initialize the levels of the item.
deba@2512
   914
    ///\n
deba@2512
   915
    ///This initializatios is started with calling \c initStart().
deba@2512
   916
    ///Then the
deba@2512
   917
    ///items should be listed levels by levels statring with the lowest one
deba@2512
   918
    ///(with level 0). This is done by using \c initAddItem()
deba@2512
   919
    ///and \c initNewLevel(). Finally \c initFinish() must be called.
deba@2512
   920
    ///The items not listed will be put on the highest level.
deba@2512
   921
    ///@{
deba@2512
   922
deba@2512
   923
    ///Start the initialization process.
deba@2512
   924
deba@2512
   925
    void initStart() {
deba@2512
   926
      
deba@2512
   927
      for (int i = 0; i <= _max_level; ++i) {
deba@2512
   928
	_first[i] = _last[i] = INVALID;
deba@2512
   929
      }
deba@2512
   930
      _init_level = 0;
deba@2512
   931
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_graph); 
deba@2512
   932
	  i != INVALID; ++i) {
deba@2512
   933
	_level.set(i, _max_level);
deba@2512
   934
      }
deba@2512
   935
    }
deba@2512
   936
deba@2512
   937
    ///Add an item to the current level.
deba@2512
   938
deba@2512
   939
    void initAddItem(Item i) {
deba@2512
   940
      _level.set(i, _init_level);
deba@2512
   941
      if (_last[_init_level] == INVALID) {
deba@2512
   942
	_first[_init_level] = i;
deba@2512
   943
	_last[_init_level] = i;
deba@2512
   944
	_prev.set(i, INVALID);
deba@2512
   945
	_next.set(i, INVALID);
deba@2512
   946
      } else {
deba@2512
   947
	_prev.set(i, _last[_init_level]);
deba@2512
   948
	_next.set(i, INVALID);
deba@2512
   949
	_next.set(_last[_init_level], i);
deba@2512
   950
	_last[_init_level] = i;
deba@2512
   951
      }
deba@2512
   952
    }
deba@2512
   953
deba@2512
   954
    ///Start a new level.
deba@2512
   955
deba@2512
   956
    ///Start a new level.
deba@2512
   957
    ///It shouldn't be used before the items on level 0 are listed.
deba@2512
   958
    void initNewLevel() {
deba@2512
   959
      ++_init_level;
deba@2512
   960
    }
deba@2512
   961
deba@2512
   962
    ///Finalize the initialization process.
deba@2512
   963
deba@2512
   964
    void initFinish() {
deba@2512
   965
      _highest_active = -1;
deba@2512
   966
    }
deba@2512
   967
deba@2512
   968
    ///@}
deba@2512
   969
deba@2512
   970
  };
deba@2512
   971
deba@2512
   972
alpar@2346
   973
} //END OF NAMESPACE LEMON
alpar@2346
   974
alpar@2346
   975
#endif
alpar@2346
   976