src/lemon/bin_heap.h
author zsuzska
Fri, 25 Mar 2005 22:05:54 +0000
changeset 1265 47c60ae9c26a
parent 1185 22bb02339808
child 1270 806451fd084b
permissions -rw-r--r--
zsuzska's own work directory
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library
klao@39
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 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
deba@1185
    76
    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
alpar@967
    77
    ///\e
deba@1191
    78
    BinHeap(ItemIntMap &_iim, const Compare &_comp) 
deba@1185
    79
      : iim(_iim), comp(_comp) {}
klao@37
    80
klao@37
    81
alpar@967
    82
    ///\e
klao@37
    83
    int size() const { return data.size(); }
alpar@967
    84
    ///\e
klao@41
    85
    bool empty() const { return data.empty(); }
klao@37
    86
klao@37
    87
  private:
klao@37
    88
    static int parent(int i) { return (i-1)/2; }
klao@37
    89
    static int second_child(int i) { return 2*i+2; }
klao@214
    90
    bool less(const PairType &p1, const PairType &p2) const {
klao@37
    91
      return comp(p1.second, p2.second);
klao@37
    92
    }
klao@37
    93
klao@37
    94
    int bubble_up(int hole, PairType p);
klao@37
    95
    int bubble_down(int hole, PairType p, int length);
klao@37
    96
klao@37
    97
    void move(const PairType &p, int i) {
klao@37
    98
      data[i] = p;
klao@172
    99
      iim.set(p.first, i);
klao@37
   100
    }
klao@37
   101
klao@41
   102
    void rmidx(int h) {
klao@41
   103
      int n = data.size()-1;
klao@41
   104
      if( h>=0 && h<=n ) {
klao@172
   105
	iim.set(data[h].first, POST_HEAP);
klao@41
   106
	if ( h<n ) {
klao@41
   107
	  bubble_down(h, data[n], n);
klao@41
   108
	}
klao@41
   109
	data.pop_back();
klao@41
   110
      }
klao@41
   111
    }
klao@41
   112
klao@37
   113
  public:
alpar@967
   114
    ///\e
klao@37
   115
    void push(const PairType &p) {
klao@37
   116
      int n = data.size();
klao@37
   117
      data.resize(n+1);
klao@37
   118
      bubble_up(n, p);
klao@37
   119
    }
alpar@967
   120
    ///\e
klao@172
   121
    void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
klao@37
   122
alpar@967
   123
    ///\e
klao@172
   124
    Item top() const {
klao@37
   125
      return data[0].first;
klao@37
   126
    }
klao@274
   127
    /// Returns the prio of the top element of the heap.
klao@274
   128
    Prio prio() const {
klao@37
   129
      return data[0].second;
klao@37
   130
    }
klao@37
   131
alpar@967
   132
    ///\e
klao@37
   133
    void pop() {
klao@41
   134
      rmidx(0);
klao@41
   135
    }
klao@41
   136
alpar@967
   137
    ///\e
klao@172
   138
    void erase(const Item &i) {
jacint@221
   139
      rmidx(iim[i]);
klao@37
   140
    }
klao@37
   141
alpar@967
   142
    ///\e
klao@274
   143
    Prio operator[](const Item &i) const {
jacint@221
   144
      int idx = iim[i];
klao@37
   145
      return data[idx].second;
klao@37
   146
    }
klao@274
   147
alpar@967
   148
    ///\e
klao@172
   149
    void set(const Item &i, const Prio &p) {
jacint@221
   150
      int idx = iim[i];
klao@37
   151
      if( idx < 0 ) {
klao@172
   152
	push(i,p);
klao@37
   153
      }
klao@172
   154
      else if( comp(p, data[idx].second) ) {
klao@172
   155
	bubble_up(idx, PairType(i,p));
klao@37
   156
      }
klao@37
   157
      else {
klao@172
   158
	bubble_down(idx, PairType(i,p), data.size());
klao@37
   159
      }
klao@37
   160
    }
klao@37
   161
alpar@967
   162
    ///\e
klao@172
   163
    void decrease(const Item &i, const Prio &p) {
jacint@221
   164
      int idx = iim[i];
klao@172
   165
      bubble_up(idx, PairType(i,p));
klao@37
   166
    }
alpar@967
   167
    ///\e
klao@172
   168
    void increase(const Item &i, const Prio &p) {
jacint@221
   169
      int idx = iim[i];
klao@172
   170
      bubble_down(idx, PairType(i,p), data.size());
klao@37
   171
    }
klao@37
   172
alpar@967
   173
    ///\e
klao@172
   174
    state_enum state(const Item &i) const {
jacint@221
   175
      int s = iim[i];
klao@39
   176
      if( s>=0 )
klao@39
   177
	s=0;
klao@39
   178
      return state_enum(s);
klao@39
   179
    }
klao@39
   180
klao@37
   181
  }; // class BinHeap
klao@37
   182
klao@37
   183
  
klao@37
   184
  template <typename K, typename V, typename M, typename C>
klao@37
   185
  int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
klao@37
   186
    int par = parent(hole);
klao@37
   187
    while( hole>0 && less(p,data[par]) ) {
klao@37
   188
      move(data[par],hole);
klao@37
   189
      hole = par;
klao@37
   190
      par = parent(hole);
klao@37
   191
    }
klao@37
   192
    move(p, hole);
klao@37
   193
    return hole;
klao@37
   194
  }
klao@37
   195
klao@37
   196
  template <typename K, typename V, typename M, typename C>
klao@37
   197
  int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
klao@37
   198
    int child = second_child(hole);
klao@37
   199
    while(child < length) {
klao@37
   200
      if( less(data[child-1], data[child]) ) {
klao@37
   201
	--child;
klao@37
   202
      }
klao@37
   203
      if( !less(data[child], p) )
klao@37
   204
	goto ok;
klao@37
   205
      move(data[child], hole);
klao@37
   206
      hole = child;
klao@37
   207
      child = second_child(hole);
klao@37
   208
    }
klao@37
   209
    child--;
klao@37
   210
    if( child<length && less(data[child], p) ) {
klao@37
   211
      move(data[child], hole);
klao@37
   212
      hole=child;
klao@37
   213
    }
klao@37
   214
  ok:
klao@37
   215
    move(p, hole);
klao@37
   216
    return hole;
klao@37
   217
  }
klao@37
   218
alpar@430
   219
  ///@}
alpar@430
   220
alpar@921
   221
} // namespace lemon
klao@37
   222
alpar@921
   223
#endif // LEMON_BIN_HEAP_H