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