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