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