lemon/bucket_heap.h
author Balazs Dezso <deba@inf.elte.hu>
Thu, 11 Jun 2009 22:11:29 +0200
changeset 681 532697c9fa53
child 682 bb8c4cd57900
permissions -rw-r--r--
Port remaining heaps from SVN -r 3509 (#50)

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