lemon/bucket_heap.h
author Balazs Dezso <deba@inf.elte.hu>
Thu, 11 Jun 2009 22:16:11 +0200
changeset 704 bb8c4cd57900
parent 703 532697c9fa53
child 705 9f529abcaebf
permissions -rw-r--r--
Simplified implementation of bucket heaps (#50)
deba@703
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@703
     2
 *
deba@703
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@703
     4
 *
deba@703
     5
 * Copyright (C) 2003-2009
deba@703
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@703
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@703
     8
 *
deba@703
     9
 * Permission to use, modify and distribute this software is granted
deba@703
    10
 * provided that this copyright notice appears in all copies. For
deba@703
    11
 * precise terms see the accompanying LICENSE file.
deba@703
    12
 *
deba@703
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@703
    14
 * express or implied, and with no claim as to its suitability for any
deba@703
    15
 * purpose.
deba@703
    16
 *
deba@703
    17
 */
deba@703
    18
deba@703
    19
#ifndef LEMON_BUCKET_HEAP_H
deba@703
    20
#define LEMON_BUCKET_HEAP_H
deba@703
    21
deba@703
    22
///\ingroup auxdat
deba@703
    23
///\file
deba@703
    24
///\brief Bucket Heap implementation.
deba@703
    25
deba@703
    26
#include <vector>
deba@703
    27
#include <utility>
deba@703
    28
#include <functional>
deba@703
    29
deba@703
    30
namespace lemon {
deba@703
    31
deba@704
    32
  namespace _bucket_heap_bits {
deba@704
    33
deba@704
    34
    template <bool minimize>
deba@704
    35
    struct DirectionTraits {
deba@704
    36
      static bool less(int left, int right) {
deba@704
    37
        return left < right;
deba@704
    38
      }
deba@704
    39
      static void increase(int& value) {
deba@704
    40
        ++value;
deba@704
    41
      }
deba@704
    42
    };
deba@704
    43
deba@704
    44
    template <>
deba@704
    45
    struct DirectionTraits<false> {
deba@704
    46
      static bool less(int left, int right) {
deba@704
    47
        return left > right;
deba@704
    48
      }
deba@704
    49
      static void increase(int& value) {
deba@704
    50
        --value;
deba@704
    51
      }
deba@704
    52
    };
deba@704
    53
deba@704
    54
  }
deba@704
    55
deba@703
    56
  /// \ingroup auxdat
deba@703
    57
  ///
deba@703
    58
  /// \brief A Bucket Heap implementation.
deba@703
    59
  ///
deba@703
    60
  /// This class implements the \e bucket \e heap data structure. A \e heap
deba@703
    61
  /// is a data structure for storing items with specified values called \e
deba@703
    62
  /// priorities in such a way that finding the item with minimum priority is
deba@703
    63
  /// efficient. The bucket heap is very simple implementation, it can store
deba@703
    64
  /// only integer priorities and it stores for each priority in the
deba@703
    65
  /// \f$ [0..C) \f$ range a list of items. So it should be used only when
deba@703
    66
  /// the priorities are small. It is not intended to use as dijkstra heap.
deba@703
    67
  ///
deba@703
    68
  /// \param _ItemIntMap A read and writable Item int map, used internally
deba@703
    69
  /// to handle the cross references.
deba@703
    70
  /// \param minimize If the given parameter is true then the heap gives back
deba@703
    71
  /// the lowest priority.
deba@704
    72
  template <typename _ItemIntMap, bool minimize = true>
deba@703
    73
  class BucketHeap {
deba@703
    74
deba@703
    75
  public:
deba@703
    76
    /// \e
deba@703
    77
    typedef typename _ItemIntMap::Key Item;
deba@703
    78
    /// \e
deba@703
    79
    typedef int Prio;
deba@703
    80
    /// \e
deba@703
    81
    typedef std::pair<Item, Prio> Pair;
deba@703
    82
    /// \e
deba@703
    83
    typedef _ItemIntMap ItemIntMap;
deba@703
    84
deba@704
    85
  private:
deba@704
    86
deba@704
    87
    typedef _bucket_heap_bits::DirectionTraits<minimize> Direction;
deba@704
    88
deba@704
    89
  public:
deba@704
    90
deba@703
    91
    /// \brief Type to represent the items states.
deba@703
    92
    ///
deba@703
    93
    /// Each Item element have a state associated to it. It may be "in heap",
deba@703
    94
    /// "pre heap" or "post heap". The latter two are indifferent from the
deba@703
    95
    /// heap's point of view, but may be useful to the user.
deba@703
    96
    ///
deba@703
    97
    /// The ItemIntMap \e should be initialized in such way that it maps
deba@703
    98
    /// PRE_HEAP (-1) to any element to be put in the heap...
deba@703
    99
    enum State {
deba@703
   100
      IN_HEAP = 0,
deba@703
   101
      PRE_HEAP = -1,
deba@703
   102
      POST_HEAP = -2
deba@703
   103
    };
deba@703
   104
deba@703
   105
  public:
deba@703
   106
    /// \brief The constructor.
deba@703
   107
    ///
deba@703
   108
    /// The constructor.
deba@703
   109
    /// \param _index should be given to the constructor, since it is used
deba@703
   110
    /// internally to handle the cross references. The value of the map
deba@703
   111
    /// should be PRE_HEAP (-1) for each element.
deba@703
   112
    explicit BucketHeap(ItemIntMap &_index) : index(_index), minimal(0) {}
deba@703
   113
deba@703
   114
    /// The number of items stored in the heap.
deba@703
   115
    ///
deba@703
   116
    /// \brief Returns the number of items stored in the heap.
deba@703
   117
    int size() const { return data.size(); }
deba@703
   118
deba@703
   119
    /// \brief Checks if the heap stores no items.
deba@703
   120
    ///
deba@703
   121
    /// Returns \c true if and only if the heap stores no items.
deba@703
   122
    bool empty() const { return data.empty(); }
deba@703
   123
deba@703
   124
    /// \brief Make empty this heap.
deba@703
   125
    ///
deba@703
   126
    /// Make empty this heap. It does not change the cross reference
deba@703
   127
    /// map.  If you want to reuse a heap what is not surely empty you
deba@703
   128
    /// should first clear the heap and after that you should set the
deba@703
   129
    /// cross reference map for each item to \c PRE_HEAP.
deba@703
   130
    void clear() {
deba@703
   131
      data.clear(); first.clear(); minimal = 0;
deba@703
   132
    }
deba@703
   133
deba@703
   134
  private:
deba@703
   135
deba@703
   136
    void relocate_last(int idx) {
deba@703
   137
      if (idx + 1 < int(data.size())) {
deba@703
   138
        data[idx] = data.back();
deba@703
   139
        if (data[idx].prev != -1) {
deba@703
   140
          data[data[idx].prev].next = idx;
deba@703
   141
        } else {
deba@703
   142
          first[data[idx].value] = idx;
deba@703
   143
        }
deba@703
   144
        if (data[idx].next != -1) {
deba@703
   145
          data[data[idx].next].prev = idx;
deba@703
   146
        }
deba@703
   147
        index[data[idx].item] = idx;
deba@703
   148
      }
deba@703
   149
      data.pop_back();
deba@703
   150
    }
deba@703
   151
deba@703
   152
    void unlace(int idx) {
deba@703
   153
      if (data[idx].prev != -1) {
deba@703
   154
        data[data[idx].prev].next = data[idx].next;
deba@703
   155
      } else {
deba@703
   156
        first[data[idx].value] = data[idx].next;
deba@703
   157
      }
deba@703
   158
      if (data[idx].next != -1) {
deba@703
   159
        data[data[idx].next].prev = data[idx].prev;
deba@703
   160
      }
deba@703
   161
    }
deba@703
   162
deba@703
   163
    void lace(int idx) {
deba@703
   164
      if (int(first.size()) <= data[idx].value) {
deba@703
   165
        first.resize(data[idx].value + 1, -1);
deba@703
   166
      }
deba@703
   167
      data[idx].next = first[data[idx].value];
deba@703
   168
      if (data[idx].next != -1) {
deba@703
   169
        data[data[idx].next].prev = idx;
deba@703
   170
      }
deba@703
   171
      first[data[idx].value] = idx;
deba@703
   172
      data[idx].prev = -1;
deba@703
   173
    }
deba@703
   174
deba@703
   175
  public:
deba@703
   176
    /// \brief Insert a pair of item and priority into the heap.
deba@703
   177
    ///
deba@703
   178
    /// Adds \c p.first to the heap with priority \c p.second.
deba@703
   179
    /// \param p The pair to insert.
deba@703
   180
    void push(const Pair& p) {
deba@703
   181
      push(p.first, p.second);
deba@703
   182
    }
deba@703
   183
deba@703
   184
    /// \brief Insert an item into the heap with the given priority.
deba@703
   185
    ///
deba@703
   186
    /// Adds \c i to the heap with priority \c p.
deba@703
   187
    /// \param i The item to insert.
deba@703
   188
    /// \param p The priority of the item.
deba@703
   189
    void push(const Item &i, const Prio &p) {
deba@703
   190
      int idx = data.size();
deba@703
   191
      index[i] = idx;
deba@703
   192
      data.push_back(BucketItem(i, p));
deba@703
   193
      lace(idx);
deba@704
   194
      if (Direction::less(p, minimal)) {
deba@703
   195
        minimal = p;
deba@703
   196
      }
deba@703
   197
    }
deba@703
   198
deba@703
   199
    /// \brief Returns the item with minimum priority.
deba@703
   200
    ///
deba@703
   201
    /// This method returns the item with minimum priority.
deba@703
   202
    /// \pre The heap must be nonempty.
deba@703
   203
    Item top() const {
deba@703
   204
      while (first[minimal] == -1) {
deba@704
   205
        Direction::increase(minimal);
deba@703
   206
      }
deba@703
   207
      return data[first[minimal]].item;
deba@703
   208
    }
deba@703
   209
deba@703
   210
    /// \brief Returns the minimum priority.
deba@703
   211
    ///
deba@703
   212
    /// It returns the minimum priority.
deba@703
   213
    /// \pre The heap must be nonempty.
deba@703
   214
    Prio prio() const {
deba@703
   215
      while (first[minimal] == -1) {
deba@704
   216
        Direction::increase(minimal);
deba@703
   217
      }
deba@703
   218
      return minimal;
deba@703
   219
    }
deba@703
   220
deba@703
   221
    /// \brief Deletes the item with minimum priority.
deba@703
   222
    ///
deba@703
   223
    /// This method deletes the item with minimum priority from the heap.
deba@703
   224
    /// \pre The heap must be non-empty.
deba@703
   225
    void pop() {
deba@703
   226
      while (first[minimal] == -1) {
deba@704
   227
        Direction::increase(minimal);
deba@703
   228
      }
deba@703
   229
      int idx = first[minimal];
deba@703
   230
      index[data[idx].item] = -2;
deba@703
   231
      unlace(idx);
deba@703
   232
      relocate_last(idx);
deba@703
   233
    }
deba@703
   234
deba@703
   235
    /// \brief Deletes \c i from the heap.
deba@703
   236
    ///
deba@703
   237
    /// This method deletes item \c i from the heap, if \c i was
deba@703
   238
    /// already stored in the heap.
deba@703
   239
    /// \param i The item to erase.
deba@703
   240
    void erase(const Item &i) {
deba@703
   241
      int idx = index[i];
deba@703
   242
      index[data[idx].item] = -2;
deba@703
   243
      unlace(idx);
deba@703
   244
      relocate_last(idx);
deba@703
   245
    }
deba@703
   246
deba@703
   247
deba@703
   248
    /// \brief Returns the priority of \c i.
deba@703
   249
    ///
deba@703
   250
    /// This function returns the priority of item \c i.
deba@703
   251
    /// \pre \c i must be in the heap.
deba@703
   252
    /// \param i The item.
deba@703
   253
    Prio operator[](const Item &i) const {
deba@703
   254
      int idx = index[i];
deba@703
   255
      return data[idx].value;
deba@703
   256
    }
deba@703
   257
deba@703
   258
    /// \brief \c i gets to the heap with priority \c p independently
deba@703
   259
    /// if \c i was already there.
deba@703
   260
    ///
deba@703
   261
    /// This method calls \ref push(\c i, \c p) if \c i is not stored
deba@703
   262
    /// in the heap and sets the priority of \c i to \c p otherwise.
deba@703
   263
    /// \param i The item.
deba@703
   264
    /// \param p The priority.
deba@703
   265
    void set(const Item &i, const Prio &p) {
deba@703
   266
      int idx = index[i];
deba@703
   267
      if (idx < 0) {
deba@704
   268
        push(i, p);
deba@704
   269
      } else if (Direction::less(p, data[idx].value)) {
deba@704
   270
        decrease(i, p);
deba@704
   271
      } else {
deba@703
   272
        increase(i, p);
deba@703
   273
      }
deba@703
   274
    }
deba@703
   275
deba@703
   276
    /// \brief Decreases the priority of \c i to \c p.
deba@703
   277
    ///
deba@703
   278
    /// This method decreases the priority of item \c i to \c p.
deba@703
   279
    /// \pre \c i must be stored in the heap with priority at least \c
deba@703
   280
    /// p relative to \c Compare.
deba@703
   281
    /// \param i The item.
deba@703
   282
    /// \param p The priority.
deba@703
   283
    void decrease(const Item &i, const Prio &p) {
deba@703
   284
      int idx = index[i];
deba@703
   285
      unlace(idx);
deba@703
   286
      data[idx].value = p;
deba@704
   287
      if (Direction::less(p, minimal)) {
deba@703
   288
        minimal = p;
deba@703
   289
      }
deba@703
   290
      lace(idx);
deba@703
   291
    }
deba@703
   292
deba@703
   293
    /// \brief Increases the priority of \c i to \c p.
deba@703
   294
    ///
deba@703
   295
    /// This method sets the priority of item \c i to \c p.
deba@703
   296
    /// \pre \c i must be stored in the heap with priority at most \c
deba@703
   297
    /// p relative to \c Compare.
deba@703
   298
    /// \param i The item.
deba@703
   299
    /// \param p The priority.
deba@703
   300
    void increase(const Item &i, const Prio &p) {
deba@703
   301
      int idx = index[i];
deba@703
   302
      unlace(idx);
deba@703
   303
      data[idx].value = p;
deba@703
   304
      lace(idx);
deba@703
   305
    }
deba@703
   306
deba@703
   307
    /// \brief Returns if \c item is in, has already been in, or has
deba@703
   308
    /// never been in the heap.
deba@703
   309
    ///
deba@703
   310
    /// This method returns PRE_HEAP if \c item has never been in the
deba@703
   311
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@703
   312
    /// otherwise. In the latter case it is possible that \c item will
deba@703
   313
    /// get back to the heap again.
deba@703
   314
    /// \param i The item.
deba@703
   315
    State state(const Item &i) const {
deba@703
   316
      int idx = index[i];
deba@703
   317
      if (idx >= 0) idx = 0;
deba@703
   318
      return State(idx);
deba@703
   319
    }
deba@703
   320
deba@703
   321
    /// \brief Sets the state of the \c item in the heap.
deba@703
   322
    ///
deba@703
   323
    /// Sets the state of the \c item in the heap. It can be used to
deba@703
   324
    /// manually clear the heap when it is important to achive the
deba@703
   325
    /// better time complexity.
deba@703
   326
    /// \param i The item.
deba@703
   327
    /// \param st The state. It should not be \c IN_HEAP.
deba@703
   328
    void state(const Item& i, State st) {
deba@703
   329
      switch (st) {
deba@703
   330
      case POST_HEAP:
deba@703
   331
      case PRE_HEAP:
deba@703
   332
        if (state(i) == IN_HEAP) {
deba@703
   333
          erase(i);
deba@703
   334
        }
deba@703
   335
        index[i] = st;
deba@703
   336
        break;
deba@703
   337
      case IN_HEAP:
deba@703
   338
        break;
deba@703
   339
      }
deba@703
   340
    }
deba@703
   341
deba@703
   342
  private:
deba@703
   343
deba@703
   344
    struct BucketItem {
deba@703
   345
      BucketItem(const Item& _item, int _value)
deba@703
   346
        : item(_item), value(_value) {}
deba@703
   347
deba@703
   348
      Item item;
deba@703
   349
      int value;
deba@703
   350
deba@703
   351
      int prev, next;
deba@703
   352
    };
deba@703
   353
deba@703
   354
    ItemIntMap& index;
deba@703
   355
    std::vector<int> first;
deba@703
   356
    std::vector<BucketItem> data;
deba@703
   357
    mutable int minimal;
deba@703
   358
deba@703
   359
  }; // class BucketHeap
deba@703
   360
deba@703
   361
  /// \ingroup auxdat
deba@703
   362
  ///
deba@703
   363
  /// \brief A Simplified Bucket Heap implementation.
deba@703
   364
  ///
deba@703
   365
  /// This class implements a simplified \e bucket \e heap data
deba@703
   366
  /// structure.  It does not provide some functionality but it faster
deba@703
   367
  /// and simplier data structure than the BucketHeap. The main
deba@703
   368
  /// difference is that the BucketHeap stores for every key a double
deba@703
   369
  /// linked list while this class stores just simple lists. In the
deba@704
   370
  /// other way it does not support erasing each elements just the
deba@703
   371
  /// minimal and it does not supports key increasing, decreasing.
deba@703
   372
  ///
deba@703
   373
  /// \param _ItemIntMap A read and writable Item int map, used internally
deba@703
   374
  /// to handle the cross references.
deba@703
   375
  /// \param minimize If the given parameter is true then the heap gives back
deba@703
   376
  /// the lowest priority.
deba@703
   377
  ///
deba@703
   378
  /// \sa BucketHeap
deba@703
   379
  template <typename _ItemIntMap, bool minimize = true >
deba@703
   380
  class SimpleBucketHeap {
deba@703
   381
deba@703
   382
  public:
deba@703
   383
    typedef typename _ItemIntMap::Key Item;
deba@703
   384
    typedef int Prio;
deba@703
   385
    typedef std::pair<Item, Prio> Pair;
deba@703
   386
    typedef _ItemIntMap ItemIntMap;
deba@703
   387
deba@704
   388
  private:
deba@704
   389
deba@704
   390
    typedef _bucket_heap_bits::DirectionTraits<minimize> Direction;
deba@704
   391
deba@704
   392
  public:
deba@704
   393
deba@703
   394
    /// \brief Type to represent the items states.
deba@703
   395
    ///
deba@703
   396
    /// Each Item element have a state associated to it. It may be "in heap",
deba@703
   397
    /// "pre heap" or "post heap". The latter two are indifferent from the
deba@703
   398
    /// heap's point of view, but may be useful to the user.
deba@703
   399
    ///
deba@703
   400
    /// The ItemIntMap \e should be initialized in such way that it maps
deba@703
   401
    /// PRE_HEAP (-1) to any element to be put in the heap...
deba@703
   402
    enum State {
deba@703
   403
      IN_HEAP = 0,
deba@703
   404
      PRE_HEAP = -1,
deba@703
   405
      POST_HEAP = -2
deba@703
   406
    };
deba@703
   407
deba@703
   408
  public:
deba@703
   409
deba@703
   410
    /// \brief The constructor.
deba@703
   411
    ///
deba@703
   412
    /// The constructor.
deba@703
   413
    /// \param _index should be given to the constructor, since it is used
deba@703
   414
    /// internally to handle the cross references. The value of the map
deba@703
   415
    /// should be PRE_HEAP (-1) for each element.
deba@703
   416
    explicit SimpleBucketHeap(ItemIntMap &_index)
deba@703
   417
      : index(_index), free(-1), num(0), minimal(0) {}
deba@703
   418
deba@703
   419
    /// \brief Returns the number of items stored in the heap.
deba@703
   420
    ///
deba@703
   421
    /// The number of items stored in the heap.
deba@703
   422
    int size() const { return num; }
deba@703
   423
deba@703
   424
    /// \brief Checks if the heap stores no items.
deba@703
   425
    ///
deba@703
   426
    /// Returns \c true if and only if the heap stores no items.
deba@703
   427
    bool empty() const { return num == 0; }
deba@703
   428
deba@703
   429
    /// \brief Make empty this heap.
deba@703
   430
    ///
deba@703
   431
    /// Make empty this heap. It does not change the cross reference
deba@703
   432
    /// map.  If you want to reuse a heap what is not surely empty you
deba@703
   433
    /// should first clear the heap and after that you should set the
deba@703
   434
    /// cross reference map for each item to \c PRE_HEAP.
deba@703
   435
    void clear() {
deba@703
   436
      data.clear(); first.clear(); free = -1; num = 0; minimal = 0;
deba@703
   437
    }
deba@703
   438
deba@703
   439
    /// \brief Insert a pair of item and priority into the heap.
deba@703
   440
    ///
deba@703
   441
    /// Adds \c p.first to the heap with priority \c p.second.
deba@703
   442
    /// \param p The pair to insert.
deba@703
   443
    void push(const Pair& p) {
deba@703
   444
      push(p.first, p.second);
deba@703
   445
    }
deba@703
   446
deba@703
   447
    /// \brief Insert an item into the heap with the given priority.
deba@703
   448
    ///
deba@703
   449
    /// Adds \c i to the heap with priority \c p.
deba@703
   450
    /// \param i The item to insert.
deba@703
   451
    /// \param p The priority of the item.
deba@703
   452
    void push(const Item &i, const Prio &p) {
deba@703
   453
      int idx;
deba@703
   454
      if (free == -1) {
deba@703
   455
        idx = data.size();
deba@703
   456
        data.push_back(BucketItem(i));
deba@703
   457
      } else {
deba@703
   458
        idx = free;
deba@703
   459
        free = data[idx].next;
deba@703
   460
        data[idx].item = i;
deba@703
   461
      }
deba@703
   462
      index[i] = idx;
deba@703
   463
      if (p >= int(first.size())) first.resize(p + 1, -1);
deba@703
   464
      data[idx].next = first[p];
deba@703
   465
      first[p] = idx;
deba@704
   466
      if (Direction::less(p, minimal)) {
deba@703
   467
        minimal = p;
deba@703
   468
      }
deba@703
   469
      ++num;
deba@703
   470
    }
deba@703
   471
deba@703
   472
    /// \brief Returns the item with minimum priority.
deba@703
   473
    ///
deba@703
   474
    /// This method returns the item with minimum priority.
deba@703
   475
    /// \pre The heap must be nonempty.
deba@703
   476
    Item top() const {
deba@703
   477
      while (first[minimal] == -1) {
deba@704
   478
        Direction::increase(minimal);
deba@703
   479
      }
deba@703
   480
      return data[first[minimal]].item;
deba@703
   481
    }
deba@703
   482
deba@703
   483
    /// \brief Returns the minimum priority.
deba@703
   484
    ///
deba@703
   485
    /// It returns the minimum priority.
deba@703
   486
    /// \pre The heap must be nonempty.
deba@703
   487
    Prio prio() const {
deba@703
   488
      while (first[minimal] == -1) {
deba@704
   489
        Direction::increase(minimal);
deba@703
   490
      }
deba@703
   491
      return minimal;
deba@703
   492
    }
deba@703
   493
deba@703
   494
    /// \brief Deletes the item with minimum priority.
deba@703
   495
    ///
deba@703
   496
    /// This method deletes the item with minimum priority from the heap.
deba@703
   497
    /// \pre The heap must be non-empty.
deba@703
   498
    void pop() {
deba@703
   499
      while (first[minimal] == -1) {
deba@704
   500
        Direction::increase(minimal);
deba@703
   501
      }
deba@703
   502
      int idx = first[minimal];
deba@703
   503
      index[data[idx].item] = -2;
deba@703
   504
      first[minimal] = data[idx].next;
deba@703
   505
      data[idx].next = free;
deba@703
   506
      free = idx;
deba@703
   507
      --num;
deba@703
   508
    }
deba@703
   509
deba@703
   510
    /// \brief Returns the priority of \c i.
deba@703
   511
    ///
deba@703
   512
    /// This function returns the priority of item \c i.
deba@703
   513
    /// \warning This operator is not a constant time function
deba@703
   514
    /// because it scans the whole data structure to find the proper
deba@703
   515
    /// value.
deba@703
   516
    /// \pre \c i must be in the heap.
deba@703
   517
    /// \param i The item.
deba@703
   518
    Prio operator[](const Item &i) const {
deba@703
   519
      for (int k = 0; k < first.size(); ++k) {
deba@703
   520
        int idx = first[k];
deba@703
   521
        while (idx != -1) {
deba@703
   522
          if (data[idx].item == i) {
deba@703
   523
            return k;
deba@703
   524
          }
deba@703
   525
          idx = data[idx].next;
deba@703
   526
        }
deba@703
   527
      }
deba@703
   528
      return -1;
deba@703
   529
    }
deba@703
   530
deba@703
   531
    /// \brief Returns if \c item is in, has already been in, or has
deba@703
   532
    /// never been in the heap.
deba@703
   533
    ///
deba@703
   534
    /// This method returns PRE_HEAP if \c item has never been in the
deba@703
   535
    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
deba@703
   536
    /// otherwise. In the latter case it is possible that \c item will
deba@703
   537
    /// get back to the heap again.
deba@703
   538
    /// \param i The item.
deba@703
   539
    State state(const Item &i) const {
deba@703
   540
      int idx = index[i];
deba@703
   541
      if (idx >= 0) idx = 0;
deba@703
   542
      return State(idx);
deba@703
   543
    }
deba@703
   544
deba@703
   545
  private:
deba@703
   546
deba@703
   547
    struct BucketItem {
deba@703
   548
      BucketItem(const Item& _item)
deba@703
   549
        : item(_item) {}
deba@703
   550
deba@703
   551
      Item item;
deba@703
   552
      int next;
deba@703
   553
    };
deba@703
   554
deba@703
   555
    ItemIntMap& index;
deba@703
   556
    std::vector<int> first;
deba@703
   557
    std::vector<BucketItem> data;
deba@703
   558
    int free, num;
deba@703
   559
    mutable int minimal;
deba@703
   560
deba@703
   561
  }; // class SimpleBucketHeap
deba@703
   562
deba@703
   563
}
deba@703
   564
deba@703
   565
#endif