lemon/bucket_heap.h
author deba
Fri, 14 Mar 2008 14:50:04 +0000
changeset 2594 97dcc3c5ea31
parent 2547 f393a8162688
permissions -rw-r--r--
Executable property removed
deba@2038
     1
/* -*- C++ -*-
deba@2038
     2
 *
deba@2038
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2038
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
deba@2038
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2038
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2038
     8
 *
deba@2038
     9
 * Permission to use, modify and distribute this software is granted
deba@2038
    10
 * provided that this copyright notice appears in all copies. For
deba@2038
    11
 * precise terms see the accompanying LICENSE file.
deba@2038
    12
 *
deba@2038
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2038
    14
 * express or implied, and with no claim as to its suitability for any
deba@2038
    15
 * purpose.
deba@2038
    16
 *
deba@2038
    17
 */
deba@2038
    18
deba@2038
    19
#ifndef LEMON_BUCKET_HEAP_H
deba@2038
    20
#define LEMON_BUCKET_HEAP_H
deba@2038
    21
deba@2038
    22
///\ingroup auxdat
deba@2038
    23
///\file
deba@2038
    24
///\brief Bucket Heap implementation.
deba@2038
    25
deba@2038
    26
#include <vector>
deba@2038
    27
#include <utility>
deba@2038
    28
#include <functional>
deba@2038
    29
deba@2038
    30
namespace lemon {
deba@2038
    31
deba@2038
    32
  /// \ingroup auxdat
deba@2089
    33
  ///
deba@2038
    34
  /// \brief A Bucket Heap implementation.
deba@2038
    35
  ///
deba@2038
    36
  /// This class implements the \e bucket \e heap data structure. A \e heap
deba@2038
    37
  /// is a data structure for storing items with specified values called \e
deba@2038
    38
  /// priorities in such a way that finding the item with minimum priority is
deba@2038
    39
  /// efficient. The bucket heap is very simple implementation, it can store
deba@2042
    40
  /// only integer priorities and it stores for each priority in the 
deba@2042
    41
  /// \f$ [0..C) \f$ range a list of items. So it should be used only when 
deba@2042
    42
  /// the priorities are small. It is not intended to use as dijkstra heap.
deba@2038
    43
  ///
deba@2038
    44
  /// \param _ItemIntMap A read and writable Item int map, used internally
deba@2038
    45
  /// to handle the cross references.
deba@2038
    46
  /// \param minimize If the given parameter is true then the heap gives back
deba@2038
    47
  /// the lowest priority. 
mqrelly@2263
    48
  template <typename _ItemIntMap, bool minimize = true >
deba@2038
    49
  class BucketHeap {
deba@2038
    50
deba@2038
    51
  public:
deba@2547
    52
    /// \e
mqrelly@2263
    53
    typedef typename _ItemIntMap::Key Item;
deba@2547
    54
    /// \e
deba@2038
    55
    typedef int Prio;
deba@2547
    56
    /// \e
deba@2038
    57
    typedef std::pair<Item, Prio> Pair;
deba@2547
    58
    /// \e
deba@2038
    59
    typedef _ItemIntMap ItemIntMap;
deba@2038
    60
deba@2038
    61
    /// \brief Type to represent the items states.
deba@2038
    62
    ///
deba@2038
    63
    /// Each Item element have a state associated to it. It may be "in heap",
deba@2038
    64
    /// "pre heap" or "post heap". The latter two are indifferent from the
deba@2038
    65
    /// heap's point of view, but may be useful to the user.
deba@2038
    66
    ///
deba@2038
    67
    /// The ItemIntMap \e should be initialized in such way that it maps
deba@2038
    68
    /// PRE_HEAP (-1) to any element to be put in the heap...
deba@2547
    69
    enum State {
deba@2038
    70
      IN_HEAP = 0,
deba@2038
    71
      PRE_HEAP = -1,
deba@2038
    72
      POST_HEAP = -2
deba@2038
    73
    };
deba@2038
    74
deba@2038
    75
  public:
deba@2038
    76
    /// \brief The constructor.
deba@2038
    77
    ///
deba@2038
    78
    /// The constructor.
deba@2038
    79
    /// \param _index should be given to the constructor, since it is used
deba@2038
    80
    /// internally to handle the cross references. The value of the map
deba@2038
    81
    /// should be PRE_HEAP (-1) for each element.
deba@2038
    82
    explicit BucketHeap(ItemIntMap &_index) : index(_index), minimal(0) {}
deba@2038
    83
    
deba@2038
    84
    /// The number of items stored in the heap.
deba@2038
    85
    ///
deba@2038
    86
    /// \brief Returns the number of items stored in the heap.
deba@2038
    87
    int size() const { return data.size(); }
deba@2038
    88
    
deba@2038
    89
    /// \brief Checks if the heap stores no items.
deba@2038
    90
    ///
deba@2038
    91
    /// Returns \c true if and only if the heap stores no items.
deba@2038
    92
    bool empty() const { return data.empty(); }
deba@2038
    93
deba@2038
    94
    /// \brief Make empty this heap.
deba@2038
    95
    /// 
deba@2050
    96
    /// Make empty this heap. It does not change the cross reference
deba@2050
    97
    /// map.  If you want to reuse a heap what is not surely empty you
deba@2050
    98
    /// should first clear the heap and after that you should set the
deba@2050
    99
    /// cross reference map for each item to \c PRE_HEAP.
deba@2038
   100
    void clear() { 
deba@2038
   101
      data.clear(); first.clear(); minimal = 0;
deba@2038
   102
    }
deba@2038
   103
deba@2038
   104
  private:
deba@2038
   105
deba@2038
   106
    void relocate_last(int idx) {
deba@2386
   107
      if (idx + 1 < int(data.size())) {
deba@2038
   108
	data[idx] = data.back();
deba@2038
   109
	if (data[idx].prev != -1) {
deba@2038
   110
	  data[data[idx].prev].next = idx;
deba@2038
   111
	} else {
deba@2038
   112
	  first[data[idx].value] = idx;
deba@2038
   113
	}
deba@2038
   114
	if (data[idx].next != -1) {
deba@2038
   115
	  data[data[idx].next].prev = idx;
deba@2038
   116
	}
deba@2038
   117
	index[data[idx].item] = idx;
deba@2038
   118
      }
deba@2038
   119
      data.pop_back();
deba@2038
   120
    }
deba@2038
   121
deba@2038
   122
    void unlace(int idx) {
deba@2038
   123
      if (data[idx].prev != -1) {
deba@2038
   124
	data[data[idx].prev].next = data[idx].next;
deba@2038
   125
      } else {
deba@2038
   126
	first[data[idx].value] = data[idx].next;
deba@2038
   127
      }
deba@2038
   128
      if (data[idx].next != -1) {
deba@2038
   129
	data[data[idx].next].prev = data[idx].prev;
deba@2038
   130
      }
deba@2038
   131
    }
deba@2038
   132
deba@2038
   133
    void lace(int idx) {
deba@2386
   134
      if (int(first.size()) <= data[idx].value) {
deba@2038
   135
	first.resize(data[idx].value + 1, -1);
deba@2038
   136
      }
deba@2038
   137
      data[idx].next = first[data[idx].value];
deba@2038
   138
      if (data[idx].next != -1) {
deba@2038
   139
	data[data[idx].next].prev = idx;
deba@2038
   140
      }
deba@2038
   141
      first[data[idx].value] = idx;
deba@2038
   142
      data[idx].prev = -1;
deba@2038
   143
    }
deba@2038
   144
deba@2038
   145
  public:
deba@2038
   146
    /// \brief Insert a pair of item and priority into the heap.
deba@2038
   147
    ///
deba@2038
   148
    /// Adds \c p.first to the heap with priority \c p.second.
deba@2038
   149
    /// \param p The pair to insert.
deba@2038
   150
    void push(const Pair& p) {
deba@2038
   151
      push(p.first, p.second);
deba@2038
   152
    }
deba@2038
   153
deba@2038
   154
    /// \brief Insert an item into the heap with the given priority.
deba@2038
   155
    ///    
deba@2038
   156
    /// Adds \c i to the heap with priority \c p. 
deba@2038
   157
    /// \param i The item to insert.
deba@2038
   158
    /// \param p The priority of the item.
deba@2038
   159
    void push(const Item &i, const Prio &p) { 
deba@2038
   160
      int idx = data.size();
deba@2038
   161
      index[i] = idx;
deba@2038
   162
      data.push_back(BucketItem(i, p));
deba@2038
   163
      lace(idx);
deba@2038
   164
      if (p < minimal) {
deba@2038
   165
	minimal = p;
deba@2038
   166
      }
deba@2038
   167
    }
deba@2038
   168
deba@2038
   169
    /// \brief Returns the item with minimum priority.
deba@2038
   170
    ///
deba@2038
   171
    /// This method returns the item with minimum priority.
deba@2038
   172
    /// \pre The heap must be nonempty.  
deba@2038
   173
    Item top() const {
deba@2038
   174
      while (first[minimal] == -1) {
deba@2038
   175
	++minimal;
deba@2038
   176
      }
deba@2038
   177
      return data[first[minimal]].item;
deba@2038
   178
    }
deba@2038
   179
deba@2038
   180
    /// \brief Returns the minimum priority.
deba@2038
   181
    ///
deba@2038
   182
    /// It returns the minimum priority.
deba@2038
   183
    /// \pre The heap must be nonempty.
deba@2038
   184
    Prio prio() const {
deba@2038
   185
      while (first[minimal] == -1) {
deba@2038
   186
	++minimal;
deba@2038
   187
      }
deba@2038
   188
      return minimal;
deba@2038
   189
    }
deba@2038
   190
deba@2038
   191
    /// \brief Deletes the item with minimum priority.
deba@2038
   192
    ///
deba@2038
   193
    /// This method deletes the item with minimum priority from the heap.  
deba@2038
   194
    /// \pre The heap must be non-empty.  
deba@2038
   195
    void pop() {
deba@2038
   196
      while (first[minimal] == -1) {
deba@2038
   197
	++minimal;
deba@2038
   198
      }
deba@2038
   199
      int idx = first[minimal];
deba@2038
   200
      index[data[idx].item] = -2;
deba@2038
   201
      unlace(idx);
deba@2038
   202
      relocate_last(idx);
deba@2038
   203
    }
deba@2038
   204
deba@2038
   205
    /// \brief Deletes \c i from the heap.
deba@2038
   206
    ///
deba@2038
   207
    /// This method deletes item \c i from the heap, if \c i was
deba@2038
   208
    /// already stored in the heap.
deba@2038
   209
    /// \param i The item to erase. 
deba@2038
   210
    void erase(const Item &i) {
deba@2038
   211
      int idx = index[i];
deba@2038
   212
      index[data[idx].item] = -2;
deba@2038
   213
      unlace(idx);
deba@2038
   214
      relocate_last(idx);
deba@2038
   215
    }
deba@2038
   216
deba@2038
   217
    
deba@2038
   218
    /// \brief Returns the priority of \c i.
deba@2038
   219
    ///
deba@2038
   220
    /// This function returns the priority of item \c i.  
deba@2038
   221
    /// \pre \c i must be in the heap.
deba@2038
   222
    /// \param i The item.
deba@2038
   223
    Prio operator[](const Item &i) const {
deba@2038
   224
      int idx = index[i];
deba@2038
   225
      return data[idx].value;
deba@2038
   226
    }
deba@2038
   227
deba@2038
   228
    /// \brief \c i gets to the heap with priority \c p independently 
deba@2038
   229
    /// if \c i was already there.
deba@2038
   230
    ///
deba@2038
   231
    /// This method calls \ref push(\c i, \c p) if \c i is not stored
deba@2038
   232
    /// in the heap and sets the priority of \c i to \c p otherwise.
deba@2038
   233
    /// \param i The item.
deba@2038
   234
    /// \param p The priority.
deba@2038
   235
    void set(const Item &i, const Prio &p) {
deba@2038
   236
      int idx = index[i];
deba@2038
   237
      if (idx < 0) {
deba@2038
   238
	push(i,p);
deba@2038
   239
      } else if (p > data[idx].value) {
deba@2038
   240
	increase(i, p);
deba@2038
   241
      } else {
deba@2038
   242
	decrease(i, p);
deba@2038
   243
      }
deba@2038
   244
    }
deba@2038
   245
deba@2038
   246
    /// \brief Decreases the priority of \c i to \c p.
deba@2089
   247
    ///
deba@2038
   248
    /// This method decreases the priority of item \c i to \c p.
deba@2038
   249
    /// \pre \c i must be stored in the heap with priority at least \c
deba@2038
   250
    /// p relative to \c Compare.
deba@2038
   251
    /// \param i The item.
deba@2038
   252
    /// \param p The priority.
deba@2038
   253
    void decrease(const Item &i, const Prio &p) {
deba@2038
   254
      int idx = index[i];
deba@2038
   255
      unlace(idx);
deba@2038
   256
      data[idx].value = p;
deba@2038
   257
      if (p < minimal) {
deba@2038
   258
	minimal = p;
deba@2038
   259
      }
deba@2038
   260
      lace(idx);
deba@2038
   261
    }
deba@2038
   262
    
deba@2038
   263
    /// \brief Increases the priority of \c i to \c p.
deba@2038
   264
    ///
deba@2038
   265
    /// This method sets the priority of item \c i to \c p. 
deba@2038
   266
    /// \pre \c i must be stored in the heap with priority at most \c
deba@2038
   267
    /// p relative to \c Compare.
deba@2038
   268
    /// \param i The item.
deba@2038
   269
    /// \param p The priority.
deba@2038
   270
    void increase(const Item &i, const Prio &p) {
deba@2038
   271
      int idx = index[i];
deba@2038
   272
      unlace(idx);
deba@2038
   273
      data[idx].value = p;
deba@2038
   274
      lace(idx);
deba@2038
   275
    }
deba@2038
   276
deba@2038
   277
    /// \brief Returns if \c item is in, has already been in, or has 
deba@2038
   278
    /// never been in the heap.
deba@2038
   279
    ///
deba@2038
   280
    /// This method returns PRE_HEAP if \c item has never been in the
deba@2038
   281
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@2038
   282
    /// otherwise. In the latter case it is possible that \c item will
deba@2038
   283
    /// get back to the heap again.
deba@2038
   284
    /// \param i The item.
deba@2547
   285
    State state(const Item &i) const {
deba@2038
   286
      int idx = index[i];
deba@2038
   287
      if (idx >= 0) idx = 0;
deba@2547
   288
      return State(idx);
deba@2038
   289
    }
deba@2038
   290
deba@2038
   291
    /// \brief Sets the state of the \c item in the heap.
deba@2038
   292
    ///
deba@2038
   293
    /// Sets the state of the \c item in the heap. It can be used to
deba@2038
   294
    /// manually clear the heap when it is important to achive the
deba@2038
   295
    /// better time complexity.
deba@2038
   296
    /// \param i The item.
deba@2038
   297
    /// \param st The state. It should not be \c IN_HEAP. 
deba@2547
   298
    void state(const Item& i, State st) {
deba@2038
   299
      switch (st) {
deba@2038
   300
      case POST_HEAP:
deba@2038
   301
      case PRE_HEAP:
deba@2038
   302
        if (state(i) == IN_HEAP) {
deba@2038
   303
          erase(i);
deba@2038
   304
        }
deba@2038
   305
        index[i] = st;
deba@2038
   306
        break;
deba@2038
   307
      case IN_HEAP:
deba@2038
   308
        break;
deba@2038
   309
      }
deba@2038
   310
    }
deba@2038
   311
deba@2038
   312
  private:
deba@2038
   313
deba@2038
   314
    struct BucketItem {
deba@2038
   315
      BucketItem(const Item& _item, int _value) 
deba@2038
   316
	: item(_item), value(_value) {}
deba@2038
   317
deba@2038
   318
      Item item;
deba@2038
   319
      int value;
deba@2038
   320
deba@2038
   321
      int prev, next;
deba@2038
   322
    };
deba@2038
   323
deba@2038
   324
    ItemIntMap& index;
deba@2038
   325
    std::vector<int> first;
deba@2038
   326
    std::vector<BucketItem> data;
deba@2038
   327
    mutable int minimal;
deba@2038
   328
deba@2038
   329
  }; // class BucketHeap
deba@2038
   330
deba@2038
   331
mqrelly@2263
   332
  template <typename _ItemIntMap>
mqrelly@2263
   333
  class BucketHeap<_ItemIntMap, false> {
deba@2038
   334
deba@2038
   335
  public:
mqrelly@2263
   336
    typedef typename _ItemIntMap::Key Item;
deba@2038
   337
    typedef int Prio;
deba@2038
   338
    typedef std::pair<Item, Prio> Pair;
deba@2038
   339
    typedef _ItemIntMap ItemIntMap;
deba@2038
   340
deba@2547
   341
    enum State {
deba@2038
   342
      IN_HEAP = 0,
deba@2038
   343
      PRE_HEAP = -1,
deba@2038
   344
      POST_HEAP = -2
deba@2038
   345
    };
deba@2038
   346
deba@2038
   347
  public:
deba@2038
   348
deba@2038
   349
    explicit BucketHeap(ItemIntMap &_index) : index(_index), maximal(-1) {}
deba@2038
   350
deba@2038
   351
    int size() const { return data.size(); }
deba@2038
   352
    bool empty() const { return data.empty(); }
deba@2038
   353
deba@2038
   354
    void clear() { 
deba@2038
   355
      data.clear(); first.clear(); maximal = -1; 
deba@2038
   356
    }
deba@2038
   357
deba@2038
   358
  private:
deba@2038
   359
deba@2038
   360
    void relocate_last(int idx) {
deba@2386
   361
      if (idx + 1 != int(data.size())) {
deba@2038
   362
	data[idx] = data.back();
deba@2038
   363
	if (data[idx].prev != -1) {
deba@2038
   364
	  data[data[idx].prev].next = idx;
deba@2038
   365
	} else {
deba@2038
   366
	  first[data[idx].value] = idx;
deba@2038
   367
	}
deba@2038
   368
	if (data[idx].next != -1) {
deba@2038
   369
	  data[data[idx].next].prev = idx;
deba@2038
   370
	}
deba@2038
   371
	index[data[idx].item] = idx;
deba@2038
   372
      }
deba@2038
   373
      data.pop_back();
deba@2038
   374
    }
deba@2038
   375
deba@2038
   376
    void unlace(int idx) {
deba@2038
   377
      if (data[idx].prev != -1) {
deba@2038
   378
	data[data[idx].prev].next = data[idx].next;
deba@2038
   379
      } else {
deba@2038
   380
	first[data[idx].value] = data[idx].next;
deba@2038
   381
      }
deba@2038
   382
      if (data[idx].next != -1) {
deba@2038
   383
	data[data[idx].next].prev = data[idx].prev;
deba@2038
   384
      }
deba@2038
   385
    }
deba@2038
   386
deba@2038
   387
    void lace(int idx) {
deba@2386
   388
      if (int(first.size()) <= data[idx].value) {
deba@2038
   389
	first.resize(data[idx].value + 1, -1);
deba@2038
   390
      }
deba@2038
   391
      data[idx].next = first[data[idx].value];
deba@2038
   392
      if (data[idx].next != -1) {
deba@2038
   393
	data[data[idx].next].prev = idx;
deba@2038
   394
      }
deba@2038
   395
      first[data[idx].value] = idx;
deba@2038
   396
      data[idx].prev = -1;
deba@2038
   397
    }
deba@2038
   398
deba@2038
   399
  public:
deba@2038
   400
deba@2038
   401
    void push(const Pair& p) {
deba@2038
   402
      push(p.first, p.second);
deba@2038
   403
    }
deba@2038
   404
deba@2038
   405
    void push(const Item &i, const Prio &p) { 
deba@2038
   406
      int idx = data.size();
deba@2038
   407
      index[i] = idx;
deba@2038
   408
      data.push_back(BucketItem(i, p));
deba@2038
   409
      lace(idx);
deba@2038
   410
      if (data[idx].value > maximal) {
deba@2038
   411
	maximal = data[idx].value;
deba@2038
   412
      }
deba@2038
   413
    }
deba@2038
   414
deba@2038
   415
    Item top() const {
deba@2038
   416
      while (first[maximal] == -1) {
deba@2038
   417
	--maximal;
deba@2038
   418
      }
deba@2038
   419
      return data[first[maximal]].item;
deba@2038
   420
    }
deba@2038
   421
deba@2038
   422
    Prio prio() const {
deba@2038
   423
      while (first[maximal] == -1) {
deba@2038
   424
	--maximal;
deba@2038
   425
      }
deba@2038
   426
      return maximal;
deba@2038
   427
    }
deba@2038
   428
deba@2038
   429
    void pop() {
deba@2038
   430
      while (first[maximal] == -1) {
deba@2038
   431
	--maximal;
deba@2038
   432
      }
deba@2038
   433
      int idx = first[maximal];
deba@2038
   434
      index[data[idx].item] = -2;
deba@2038
   435
      unlace(idx);
deba@2038
   436
      relocate_last(idx);
deba@2038
   437
    }
deba@2038
   438
deba@2038
   439
    void erase(const Item &i) {
deba@2038
   440
      int idx = index[i];
deba@2038
   441
      index[data[idx].item] = -2;
deba@2038
   442
      unlace(idx);
deba@2038
   443
      relocate_last(idx);
deba@2038
   444
    }
deba@2038
   445
deba@2038
   446
    Prio operator[](const Item &i) const {
deba@2038
   447
      int idx = index[i];
deba@2038
   448
      return data[idx].value;
deba@2038
   449
    }
deba@2038
   450
deba@2038
   451
    void set(const Item &i, const Prio &p) {
deba@2038
   452
      int idx = index[i];
deba@2038
   453
      if (idx < 0) {
deba@2038
   454
	push(i,p);
deba@2038
   455
      } else if (p > data[idx].value) {
deba@2038
   456
	decrease(i, p);
deba@2038
   457
      } else {
deba@2038
   458
	increase(i, p);
deba@2038
   459
      }
deba@2038
   460
    }
deba@2038
   461
deba@2038
   462
    void decrease(const Item &i, const Prio &p) {
deba@2038
   463
      int idx = index[i];
deba@2038
   464
      unlace(idx);
deba@2038
   465
      data[idx].value = p;
deba@2038
   466
      if (p > maximal) {
deba@2038
   467
	maximal = p;
deba@2038
   468
      }
deba@2038
   469
      lace(idx);
deba@2038
   470
    }
deba@2038
   471
    
deba@2038
   472
    void increase(const Item &i, const Prio &p) {
deba@2038
   473
      int idx = index[i];
deba@2038
   474
      unlace(idx);
deba@2038
   475
      data[idx].value = p;
deba@2038
   476
      lace(idx);
deba@2038
   477
    }
deba@2038
   478
deba@2547
   479
    State state(const Item &i) const {
deba@2038
   480
      int idx = index[i];
deba@2038
   481
      if (idx >= 0) idx = 0;
deba@2547
   482
      return State(idx);
deba@2038
   483
    }
deba@2038
   484
deba@2547
   485
    void state(const Item& i, State st) {
deba@2038
   486
      switch (st) {
deba@2038
   487
      case POST_HEAP:
deba@2038
   488
      case PRE_HEAP:
deba@2038
   489
        if (state(i) == IN_HEAP) {
deba@2038
   490
          erase(i);
deba@2038
   491
        }
deba@2038
   492
        index[i] = st;
deba@2038
   493
        break;
deba@2038
   494
      case IN_HEAP:
deba@2038
   495
        break;
deba@2038
   496
      }
deba@2038
   497
    }
deba@2038
   498
deba@2038
   499
  private:
deba@2038
   500
deba@2038
   501
    struct BucketItem {
deba@2038
   502
      BucketItem(const Item& _item, int _value) 
deba@2038
   503
	: item(_item), value(_value) {}
deba@2038
   504
deba@2038
   505
      Item item;
deba@2038
   506
      int value;
deba@2038
   507
deba@2038
   508
      int prev, next;
deba@2038
   509
    };
deba@2038
   510
deba@2038
   511
    ItemIntMap& index;
deba@2038
   512
    std::vector<int> first;
deba@2038
   513
    std::vector<BucketItem> data;
deba@2038
   514
    mutable int maximal;
deba@2038
   515
deba@2038
   516
  }; // class BucketHeap
deba@2038
   517
deba@2089
   518
  /// \ingroup auxdat
deba@2089
   519
  ///
deba@2089
   520
  /// \brief A Simplified Bucket Heap implementation.
deba@2089
   521
  ///
deba@2089
   522
  /// This class implements a simplified \e bucket \e heap data
deba@2089
   523
  /// structure.  It does not provide some functionality but it faster
deba@2089
   524
  /// and simplier data structure than the BucketHeap. The main
deba@2089
   525
  /// difference is that the BucketHeap stores for every key a double
deba@2089
   526
  /// linked list while this class stores just simple lists. In the
deba@2089
   527
  /// other way it does not supports erasing each elements just the
deba@2089
   528
  /// minimal and it does not supports key increasing, decreasing.
deba@2089
   529
  ///
deba@2089
   530
  /// \param _ItemIntMap A read and writable Item int map, used internally
deba@2089
   531
  /// to handle the cross references.
deba@2089
   532
  /// \param minimize If the given parameter is true then the heap gives back
deba@2089
   533
  /// the lowest priority.
deba@2089
   534
  ///
deba@2089
   535
  /// \sa BucketHeap 
mqrelly@2263
   536
  template <typename _ItemIntMap, bool minimize = true >
deba@2089
   537
  class SimpleBucketHeap {
deba@2089
   538
deba@2089
   539
  public:
mqrelly@2263
   540
    typedef typename _ItemIntMap::Key Item;
deba@2089
   541
    typedef int Prio;
deba@2089
   542
    typedef std::pair<Item, Prio> Pair;
deba@2089
   543
    typedef _ItemIntMap ItemIntMap;
deba@2089
   544
deba@2089
   545
    /// \brief Type to represent the items states.
deba@2089
   546
    ///
deba@2089
   547
    /// Each Item element have a state associated to it. It may be "in heap",
deba@2089
   548
    /// "pre heap" or "post heap". The latter two are indifferent from the
deba@2089
   549
    /// heap's point of view, but may be useful to the user.
deba@2089
   550
    ///
deba@2089
   551
    /// The ItemIntMap \e should be initialized in such way that it maps
deba@2089
   552
    /// PRE_HEAP (-1) to any element to be put in the heap...
deba@2547
   553
    enum State {
deba@2089
   554
      IN_HEAP = 0,
deba@2089
   555
      PRE_HEAP = -1,
deba@2089
   556
      POST_HEAP = -2
deba@2089
   557
    };
deba@2089
   558
deba@2089
   559
  public:
deba@2089
   560
deba@2089
   561
    /// \brief The constructor.
deba@2089
   562
    ///
deba@2089
   563
    /// The constructor.
deba@2089
   564
    /// \param _index should be given to the constructor, since it is used
deba@2089
   565
    /// internally to handle the cross references. The value of the map
deba@2089
   566
    /// should be PRE_HEAP (-1) for each element.
deba@2089
   567
    explicit SimpleBucketHeap(ItemIntMap &_index) 
deba@2089
   568
      : index(_index), free(-1), num(0), minimal(0) {}
deba@2089
   569
    
deba@2089
   570
    /// \brief Returns the number of items stored in the heap.
deba@2089
   571
    ///
deba@2089
   572
    /// The number of items stored in the heap.
deba@2089
   573
    int size() const { return num; }
deba@2089
   574
    
deba@2089
   575
    /// \brief Checks if the heap stores no items.
deba@2089
   576
    ///
deba@2089
   577
    /// Returns \c true if and only if the heap stores no items.
deba@2089
   578
    bool empty() const { return num == 0; }
deba@2089
   579
deba@2089
   580
    /// \brief Make empty this heap.
deba@2089
   581
    /// 
deba@2089
   582
    /// Make empty this heap. It does not change the cross reference
deba@2089
   583
    /// map.  If you want to reuse a heap what is not surely empty you
deba@2089
   584
    /// should first clear the heap and after that you should set the
deba@2089
   585
    /// cross reference map for each item to \c PRE_HEAP.
deba@2089
   586
    void clear() { 
deba@2089
   587
      data.clear(); first.clear(); free = -1; num = 0; minimal = 0;
deba@2089
   588
    }
deba@2089
   589
deba@2089
   590
    /// \brief Insert a pair of item and priority into the heap.
deba@2089
   591
    ///
deba@2089
   592
    /// Adds \c p.first to the heap with priority \c p.second.
deba@2089
   593
    /// \param p The pair to insert.
deba@2089
   594
    void push(const Pair& p) {
deba@2089
   595
      push(p.first, p.second);
deba@2089
   596
    }
deba@2089
   597
deba@2089
   598
    /// \brief Insert an item into the heap with the given priority.
deba@2089
   599
    ///    
deba@2089
   600
    /// Adds \c i to the heap with priority \c p. 
deba@2089
   601
    /// \param i The item to insert.
deba@2089
   602
    /// \param p The priority of the item.
deba@2089
   603
    void push(const Item &i, const Prio &p) {
deba@2089
   604
      int idx;
deba@2089
   605
      if (free == -1) {
deba@2089
   606
        idx = data.size();
deba@2110
   607
        data.push_back(BucketItem(i));
deba@2089
   608
      } else {
deba@2089
   609
        idx = free;
deba@2089
   610
        free = data[idx].next;
deba@2110
   611
        data[idx].item = i;
deba@2089
   612
      }
deba@2089
   613
      index[i] = idx;
deba@2386
   614
      if (p >= int(first.size())) first.resize(p + 1, -1);
deba@2089
   615
      data[idx].next = first[p];
deba@2089
   616
      first[p] = idx;
deba@2089
   617
      if (p < minimal) {
deba@2089
   618
	minimal = p;
deba@2089
   619
      }
deba@2089
   620
      ++num;
deba@2089
   621
    }
deba@2089
   622
deba@2089
   623
    /// \brief Returns the item with minimum priority.
deba@2089
   624
    ///
deba@2089
   625
    /// This method returns the item with minimum priority.
deba@2089
   626
    /// \pre The heap must be nonempty.  
deba@2089
   627
    Item top() const {
deba@2089
   628
      while (first[minimal] == -1) {
deba@2089
   629
	++minimal;
deba@2089
   630
      }
deba@2089
   631
      return data[first[minimal]].item;
deba@2089
   632
    }
deba@2089
   633
deba@2089
   634
    /// \brief Returns the minimum priority.
deba@2089
   635
    ///
deba@2089
   636
    /// It returns the minimum priority.
deba@2089
   637
    /// \pre The heap must be nonempty.
deba@2089
   638
    Prio prio() const {
deba@2089
   639
      while (first[minimal] == -1) {
deba@2089
   640
	++minimal;
deba@2089
   641
      }
deba@2089
   642
      return minimal;
deba@2089
   643
    }
deba@2089
   644
deba@2089
   645
    /// \brief Deletes the item with minimum priority.
deba@2089
   646
    ///
deba@2089
   647
    /// This method deletes the item with minimum priority from the heap.  
deba@2089
   648
    /// \pre The heap must be non-empty.  
deba@2089
   649
    void pop() {
deba@2089
   650
      while (first[minimal] == -1) {
deba@2089
   651
	++minimal;
deba@2089
   652
      }
deba@2089
   653
      int idx = first[minimal];
deba@2089
   654
      index[data[idx].item] = -2;
deba@2089
   655
      first[minimal] = data[idx].next;
deba@2089
   656
      data[idx].next = free;
deba@2089
   657
      free = idx;
deba@2089
   658
      --num;
deba@2089
   659
    }
deba@2089
   660
    
deba@2089
   661
    /// \brief Returns the priority of \c i.
deba@2089
   662
    ///
deba@2110
   663
    /// This function returns the priority of item \c i.
deba@2110
   664
    /// \warning This operator is not a constant time function
deba@2110
   665
    /// because it scans the whole data structure to find the proper
deba@2110
   666
    /// value.  
deba@2089
   667
    /// \pre \c i must be in the heap.
deba@2089
   668
    /// \param i The item.
deba@2089
   669
    Prio operator[](const Item &i) const {
deba@2110
   670
      for (int k = 0; k < first.size(); ++k) {
deba@2110
   671
        int idx = first[k];
deba@2110
   672
        while (idx != -1) {
deba@2110
   673
          if (data[idx].item == i) {
deba@2110
   674
            return k;
deba@2110
   675
          }
deba@2110
   676
          idx = data[idx].next;
deba@2110
   677
        }
deba@2110
   678
      }
deba@2110
   679
      return -1;
deba@2089
   680
    }
deba@2089
   681
deba@2089
   682
    /// \brief Returns if \c item is in, has already been in, or has 
deba@2089
   683
    /// never been in the heap.
deba@2089
   684
    ///
deba@2089
   685
    /// This method returns PRE_HEAP if \c item has never been in the
deba@2089
   686
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@2089
   687
    /// otherwise. In the latter case it is possible that \c item will
deba@2089
   688
    /// get back to the heap again.
deba@2089
   689
    /// \param i The item.
deba@2547
   690
    State state(const Item &i) const {
deba@2089
   691
      int idx = index[i];
deba@2089
   692
      if (idx >= 0) idx = 0;
deba@2547
   693
      return State(idx);
deba@2089
   694
    }
deba@2089
   695
deba@2089
   696
  private:
deba@2089
   697
deba@2089
   698
    struct BucketItem {
deba@2110
   699
      BucketItem(const Item& _item) 
deba@2110
   700
	: item(_item) {}
deba@2089
   701
deba@2089
   702
      Item item;
deba@2089
   703
      int next;
deba@2089
   704
    };
deba@2089
   705
deba@2089
   706
    ItemIntMap& index;
deba@2089
   707
    std::vector<int> first;
deba@2089
   708
    std::vector<BucketItem> data;
deba@2089
   709
    int free, num;
deba@2089
   710
    mutable int minimal;
deba@2089
   711
deba@2089
   712
  }; // class SimpleBucketHeap
deba@2089
   713
mqrelly@2263
   714
  template <typename _ItemIntMap>
mqrelly@2263
   715
  class SimpleBucketHeap<_ItemIntMap, false> {
deba@2089
   716
deba@2089
   717
  public:
mqrelly@2263
   718
    typedef typename _ItemIntMap::Key Item;
deba@2089
   719
    typedef int Prio;
deba@2089
   720
    typedef std::pair<Item, Prio> Pair;
deba@2089
   721
    typedef _ItemIntMap ItemIntMap;
deba@2089
   722
deba@2547
   723
    enum State {
deba@2089
   724
      IN_HEAP = 0,
deba@2089
   725
      PRE_HEAP = -1,
deba@2089
   726
      POST_HEAP = -2
deba@2089
   727
    };
deba@2089
   728
deba@2089
   729
  public:
deba@2089
   730
deba@2089
   731
    explicit SimpleBucketHeap(ItemIntMap &_index) 
deba@2089
   732
      : index(_index), free(-1), num(0), maximal(0) {}
deba@2089
   733
    
deba@2089
   734
    int size() const { return num; }
deba@2089
   735
    
deba@2089
   736
    bool empty() const { return num == 0; }
deba@2089
   737
deba@2089
   738
    void clear() { 
deba@2089
   739
      data.clear(); first.clear(); free = -1; num = 0; maximal = 0;
deba@2089
   740
    }
deba@2089
   741
deba@2089
   742
    void push(const Pair& p) {
deba@2089
   743
      push(p.first, p.second);
deba@2089
   744
    }
deba@2089
   745
deba@2089
   746
    void push(const Item &i, const Prio &p) {
deba@2089
   747
      int idx;
deba@2089
   748
      if (free == -1) {
deba@2089
   749
        idx = data.size();
deba@2110
   750
        data.push_back(BucketItem(i));
deba@2089
   751
      } else {
deba@2089
   752
        idx = free;
deba@2089
   753
        free = data[idx].next;
deba@2110
   754
        data[idx].item = i;
deba@2089
   755
      }
deba@2089
   756
      index[i] = idx;
deba@2386
   757
      if (p >= int(first.size())) first.resize(p + 1, -1);
deba@2089
   758
      data[idx].next = first[p];
deba@2089
   759
      first[p] = idx;
deba@2089
   760
      if (p > maximal) {
deba@2089
   761
	maximal = p;
deba@2089
   762
      }
deba@2089
   763
      ++num;
deba@2089
   764
    }
deba@2089
   765
deba@2089
   766
    Item top() const {
deba@2089
   767
      while (first[maximal] == -1) {
deba@2089
   768
	--maximal;
deba@2089
   769
      }
deba@2089
   770
      return data[first[maximal]].item;
deba@2089
   771
    }
deba@2089
   772
deba@2089
   773
    Prio prio() const {
deba@2089
   774
      while (first[maximal] == -1) {
deba@2089
   775
	--maximal;
deba@2089
   776
      }
deba@2089
   777
      return maximal;
deba@2089
   778
    }
deba@2089
   779
deba@2089
   780
    void pop() {
deba@2089
   781
      while (first[maximal] == -1) {
deba@2089
   782
	--maximal;
deba@2089
   783
      }
deba@2089
   784
      int idx = first[maximal];
deba@2089
   785
      index[data[idx].item] = -2;
deba@2089
   786
      first[maximal] = data[idx].next;
deba@2089
   787
      data[idx].next = free;
deba@2089
   788
      free = idx;
deba@2089
   789
      --num;
deba@2089
   790
    }
deba@2089
   791
    
deba@2089
   792
    Prio operator[](const Item &i) const {
deba@2110
   793
      for (int k = 0; k < first.size(); ++k) {
deba@2110
   794
        int idx = first[k];
deba@2110
   795
        while (idx != -1) {
deba@2110
   796
          if (data[idx].item == i) {
deba@2110
   797
            return k;
deba@2110
   798
          }
deba@2110
   799
          idx = data[idx].next;
deba@2110
   800
        }
deba@2110
   801
      }
deba@2110
   802
      return -1;
deba@2089
   803
    }
deba@2089
   804
deba@2547
   805
    State state(const Item &i) const {
deba@2089
   806
      int idx = index[i];
deba@2089
   807
      if (idx >= 0) idx = 0;
deba@2547
   808
      return State(idx);
deba@2089
   809
    }
deba@2089
   810
deba@2089
   811
  private:
deba@2089
   812
deba@2089
   813
    struct BucketItem {
deba@2110
   814
      BucketItem(const Item& _item) : item(_item) {}
deba@2089
   815
deba@2089
   816
      Item item;
deba@2089
   817
deba@2089
   818
      int next;
deba@2089
   819
    };
deba@2089
   820
deba@2089
   821
    ItemIntMap& index;
deba@2089
   822
    std::vector<int> first;
deba@2089
   823
    std::vector<BucketItem> data;
deba@2089
   824
    int free, num;
deba@2089
   825
    mutable int maximal;
deba@2089
   826
deba@2089
   827
  };
deba@2089
   828
deba@2038
   829
}
deba@2038
   830
  
deba@2038
   831
#endif