src/lemon/bin_heap.h
author marci
Thu, 17 Feb 2005 15:14:13 +0000
changeset 1153 4b0468de3a31
parent 921 818510fa3d99
child 1164 80bb73097736
permissions -rw-r--r--
if you have a nuclear power plant and wanna compute small magic squares, then let's do it
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library
klao@39
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
klao@39
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
klao@39
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
klao@39
    14
 *
klao@39
    15
 */
klao@39
    16
alpar@921
    17
#ifndef LEMON_BIN_HEAP_H
alpar@921
    18
#define LEMON_BIN_HEAP_H
klao@37
    19
klao@491
    20
///\ingroup auxdat
klao@274
    21
///\file
klao@274
    22
///\brief Binary Heap implementation.
alpar@906
    23
///\todo It should be documented.
klao@274
    24
klao@37
    25
#include <vector>
klao@37
    26
#include <utility>
klao@37
    27
#include <functional>
klao@37
    28
alpar@921
    29
namespace lemon {
klao@37
    30
alpar@430
    31
  /// \addtogroup auxdat
alpar@430
    32
  /// @{
alpar@430
    33
alpar@430
    34
   /// A Binary Heap implementation.
alpar@967
    35
  
alpar@967
    36
  ///\todo Please document...
alpar@967
    37
  ///
alpar@967
    38
  ///\sa FibHeap
alpar@967
    39
  ///\sa Dijkstra
klao@172
    40
  template <typename Item, typename Prio, typename ItemIntMap,
klao@172
    41
	    typename Compare = std::less<Prio> >
klao@37
    42
  class BinHeap {
klao@37
    43
klao@37
    44
  public:
klao@172
    45
    typedef Item                             ItemType;
klao@37
    46
    // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
klao@172
    47
    typedef Prio                             PrioType;
klao@172
    48
    typedef std::pair<ItemType,PrioType>     PairType;
klao@172
    49
    typedef ItemIntMap                       ItemIntMapType;
klao@172
    50
    typedef Compare                          PrioCompare;
klao@37
    51
klao@37
    52
    /**
klao@172
    53
     * Each Item element have a state associated to it. It may be "in heap",
klao@37
    54
     * "pre heap" or "post heap". The later two are indifferent from the
klao@37
    55
     * heap's point of view, but may be useful to the user.
klao@37
    56
     *
klao@172
    57
     * The ItemIntMap _should_ be initialized in such way, that it maps
klao@37
    58
     * PRE_HEAP (-1) to any element to be put in the heap...
klao@37
    59
     */
klao@274
    60
    ///\todo it is used nowhere
klao@274
    61
    ///
klao@39
    62
    enum state_enum {
klao@37
    63
      IN_HEAP = 0,
klao@37
    64
      PRE_HEAP = -1,
klao@37
    65
      POST_HEAP = -2
klao@37
    66
    };
klao@37
    67
klao@37
    68
  private:
klao@37
    69
    std::vector<PairType> data;
klao@37
    70
    Compare comp;
klao@37
    71
    // FIXME: jo ez igy???
klao@172
    72
    ItemIntMap &iim;
klao@37
    73
klao@37
    74
  public:
alpar@967
    75
    ///\e
klao@172
    76
    BinHeap(ItemIntMap &_iim) : iim(_iim) {}
alpar@967
    77
    ///\e
klao@172
    78
    BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {}
klao@37
    79
klao@37
    80
alpar@967
    81
    ///\e
klao@37
    82
    int size() const { return data.size(); }
alpar@967
    83
    ///\e
klao@41
    84
    bool empty() const { return data.empty(); }
klao@37
    85
klao@37
    86
  private:
klao@37
    87
    static int parent(int i) { return (i-1)/2; }
klao@37
    88
    static int second_child(int i) { return 2*i+2; }
klao@214
    89
    bool less(const PairType &p1, const PairType &p2) const {
klao@37
    90
      return comp(p1.second, p2.second);
klao@37
    91
    }
klao@37
    92
klao@37
    93
    int bubble_up(int hole, PairType p);
klao@37
    94
    int bubble_down(int hole, PairType p, int length);
klao@37
    95
klao@37
    96
    void move(const PairType &p, int i) {
klao@37
    97
      data[i] = p;
klao@172
    98
      iim.set(p.first, i);
klao@37
    99
    }
klao@37
   100
klao@41
   101
    void rmidx(int h) {
klao@41
   102
      int n = data.size()-1;
klao@41
   103
      if( h>=0 && h<=n ) {
klao@172
   104
	iim.set(data[h].first, POST_HEAP);
klao@41
   105
	if ( h<n ) {
klao@41
   106
	  bubble_down(h, data[n], n);
klao@41
   107
	}
klao@41
   108
	data.pop_back();
klao@41
   109
      }
klao@41
   110
    }
klao@41
   111
klao@37
   112
  public:
alpar@967
   113
    ///\e
klao@37
   114
    void push(const PairType &p) {
klao@37
   115
      int n = data.size();
klao@37
   116
      data.resize(n+1);
klao@37
   117
      bubble_up(n, p);
klao@37
   118
    }
alpar@967
   119
    ///\e
klao@172
   120
    void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
klao@37
   121
alpar@967
   122
    ///\e
klao@172
   123
    Item top() const {
klao@37
   124
      return data[0].first;
klao@37
   125
    }
klao@274
   126
    /// Returns the prio of the top element of the heap.
klao@274
   127
    Prio prio() const {
klao@37
   128
      return data[0].second;
klao@37
   129
    }
klao@37
   130
alpar@967
   131
    ///\e
klao@37
   132
    void pop() {
klao@41
   133
      rmidx(0);
klao@41
   134
    }
klao@41
   135
alpar@967
   136
    ///\e
klao@172
   137
    void erase(const Item &i) {
jacint@221
   138
      rmidx(iim[i]);
klao@37
   139
    }
klao@37
   140
alpar@967
   141
    ///\e
klao@274
   142
    Prio operator[](const Item &i) const {
jacint@221
   143
      int idx = iim[i];
klao@37
   144
      return data[idx].second;
klao@37
   145
    }
klao@274
   146
alpar@967
   147
    ///\e
klao@172
   148
    void set(const Item &i, const Prio &p) {
jacint@221
   149
      int idx = iim[i];
klao@37
   150
      if( idx < 0 ) {
klao@172
   151
	push(i,p);
klao@37
   152
      }
klao@172
   153
      else if( comp(p, data[idx].second) ) {
klao@172
   154
	bubble_up(idx, PairType(i,p));
klao@37
   155
      }
klao@37
   156
      else {
klao@172
   157
	bubble_down(idx, PairType(i,p), data.size());
klao@37
   158
      }
klao@37
   159
    }
klao@37
   160
alpar@967
   161
    ///\e
klao@172
   162
    void decrease(const Item &i, const Prio &p) {
jacint@221
   163
      int idx = iim[i];
klao@172
   164
      bubble_up(idx, PairType(i,p));
klao@37
   165
    }
alpar@967
   166
    ///\e
klao@172
   167
    void increase(const Item &i, const Prio &p) {
jacint@221
   168
      int idx = iim[i];
klao@172
   169
      bubble_down(idx, PairType(i,p), data.size());
klao@37
   170
    }
klao@37
   171
alpar@967
   172
    ///\e
klao@172
   173
    state_enum state(const Item &i) const {
jacint@221
   174
      int s = iim[i];
klao@39
   175
      if( s>=0 )
klao@39
   176
	s=0;
klao@39
   177
      return state_enum(s);
klao@39
   178
    }
klao@39
   179
klao@37
   180
  }; // class BinHeap
klao@37
   181
klao@37
   182
  
klao@37
   183
  template <typename K, typename V, typename M, typename C>
klao@37
   184
  int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
klao@37
   185
    int par = parent(hole);
klao@37
   186
    while( hole>0 && less(p,data[par]) ) {
klao@37
   187
      move(data[par],hole);
klao@37
   188
      hole = par;
klao@37
   189
      par = parent(hole);
klao@37
   190
    }
klao@37
   191
    move(p, hole);
klao@37
   192
    return hole;
klao@37
   193
  }
klao@37
   194
klao@37
   195
  template <typename K, typename V, typename M, typename C>
klao@37
   196
  int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
klao@37
   197
    int child = second_child(hole);
klao@37
   198
    while(child < length) {
klao@37
   199
      if( less(data[child-1], data[child]) ) {
klao@37
   200
	--child;
klao@37
   201
      }
klao@37
   202
      if( !less(data[child], p) )
klao@37
   203
	goto ok;
klao@37
   204
      move(data[child], hole);
klao@37
   205
      hole = child;
klao@37
   206
      child = second_child(hole);
klao@37
   207
    }
klao@37
   208
    child--;
klao@37
   209
    if( child<length && less(data[child], p) ) {
klao@37
   210
      move(data[child], hole);
klao@37
   211
      hole=child;
klao@37
   212
    }
klao@37
   213
  ok:
klao@37
   214
    move(p, hole);
klao@37
   215
    return hole;
klao@37
   216
  }
klao@37
   217
alpar@430
   218
  ///@}
alpar@430
   219
alpar@921
   220
} // namespace lemon
klao@37
   221
alpar@921
   222
#endif // LEMON_BIN_HEAP_H