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