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