lemon/bucket_heap.h
author Alpar Juttner <alpar@cs.elte.hu>
Sat, 24 Dec 2011 01:15:59 +0100
changeset 780 1818cc848005
parent 704 bb8c4cd57900
permissions -rw-r--r--
Update CPLEX lookup

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