1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lemon/bin_heap.h Wed Sep 29 15:30:04 2004 +0000
1.3 @@ -0,0 +1,203 @@
1.4 +/* -*- C++ -*-
1.5 + * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_BIN_HEAP_H
1.21 +#define LEMON_BIN_HEAP_H
1.22 +
1.23 +///\ingroup auxdat
1.24 +///\file
1.25 +///\brief Binary Heap implementation.
1.26 +///\todo It should be documented.
1.27 +
1.28 +#include <vector>
1.29 +#include <utility>
1.30 +#include <functional>
1.31 +
1.32 +namespace lemon {
1.33 +
1.34 + /// \addtogroup auxdat
1.35 + /// @{
1.36 +
1.37 + /// A Binary Heap implementation.
1.38 + template <typename Item, typename Prio, typename ItemIntMap,
1.39 + typename Compare = std::less<Prio> >
1.40 + class BinHeap {
1.41 +
1.42 + public:
1.43 + typedef Item ItemType;
1.44 + // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
1.45 + typedef Prio PrioType;
1.46 + typedef std::pair<ItemType,PrioType> PairType;
1.47 + typedef ItemIntMap ItemIntMapType;
1.48 + typedef Compare PrioCompare;
1.49 +
1.50 + /**
1.51 + * Each Item element have a state associated to it. It may be "in heap",
1.52 + * "pre heap" or "post heap". The later two are indifferent from the
1.53 + * heap's point of view, but may be useful to the user.
1.54 + *
1.55 + * The ItemIntMap _should_ be initialized in such way, that it maps
1.56 + * PRE_HEAP (-1) to any element to be put in the heap...
1.57 + */
1.58 + ///\todo it is used nowhere
1.59 + ///
1.60 + enum state_enum {
1.61 + IN_HEAP = 0,
1.62 + PRE_HEAP = -1,
1.63 + POST_HEAP = -2
1.64 + };
1.65 +
1.66 + private:
1.67 + std::vector<PairType> data;
1.68 + Compare comp;
1.69 + // FIXME: jo ez igy???
1.70 + ItemIntMap &iim;
1.71 +
1.72 + public:
1.73 + BinHeap(ItemIntMap &_iim) : iim(_iim) {}
1.74 + BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {}
1.75 +
1.76 +
1.77 + int size() const { return data.size(); }
1.78 + bool empty() const { return data.empty(); }
1.79 +
1.80 + private:
1.81 + static int parent(int i) { return (i-1)/2; }
1.82 + static int second_child(int i) { return 2*i+2; }
1.83 + bool less(const PairType &p1, const PairType &p2) const {
1.84 + return comp(p1.second, p2.second);
1.85 + }
1.86 +
1.87 + int bubble_up(int hole, PairType p);
1.88 + int bubble_down(int hole, PairType p, int length);
1.89 +
1.90 + void move(const PairType &p, int i) {
1.91 + data[i] = p;
1.92 + iim.set(p.first, i);
1.93 + }
1.94 +
1.95 + void rmidx(int h) {
1.96 + int n = data.size()-1;
1.97 + if( h>=0 && h<=n ) {
1.98 + iim.set(data[h].first, POST_HEAP);
1.99 + if ( h<n ) {
1.100 + bubble_down(h, data[n], n);
1.101 + }
1.102 + data.pop_back();
1.103 + }
1.104 + }
1.105 +
1.106 + public:
1.107 + void push(const PairType &p) {
1.108 + int n = data.size();
1.109 + data.resize(n+1);
1.110 + bubble_up(n, p);
1.111 + }
1.112 + void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
1.113 +
1.114 + Item top() const {
1.115 + return data[0].first;
1.116 + }
1.117 + /// Returns the prio of the top element of the heap.
1.118 + Prio prio() const {
1.119 + return data[0].second;
1.120 + }
1.121 +
1.122 + void pop() {
1.123 + rmidx(0);
1.124 + }
1.125 +
1.126 + void erase(const Item &i) {
1.127 + rmidx(iim[i]);
1.128 + }
1.129 +
1.130 + Prio operator[](const Item &i) const {
1.131 + int idx = iim[i];
1.132 + return data[idx].second;
1.133 + }
1.134 +
1.135 + void set(const Item &i, const Prio &p) {
1.136 + int idx = iim[i];
1.137 + if( idx < 0 ) {
1.138 + push(i,p);
1.139 + }
1.140 + else if( comp(p, data[idx].second) ) {
1.141 + bubble_up(idx, PairType(i,p));
1.142 + }
1.143 + else {
1.144 + bubble_down(idx, PairType(i,p), data.size());
1.145 + }
1.146 + }
1.147 +
1.148 + void decrease(const Item &i, const Prio &p) {
1.149 + int idx = iim[i];
1.150 + bubble_up(idx, PairType(i,p));
1.151 + }
1.152 + void increase(const Item &i, const Prio &p) {
1.153 + int idx = iim[i];
1.154 + bubble_down(idx, PairType(i,p), data.size());
1.155 + }
1.156 +
1.157 + state_enum state(const Item &i) const {
1.158 + int s = iim[i];
1.159 + if( s>=0 )
1.160 + s=0;
1.161 + return state_enum(s);
1.162 + }
1.163 +
1.164 + }; // class BinHeap
1.165 +
1.166 +
1.167 + template <typename K, typename V, typename M, typename C>
1.168 + int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
1.169 + int par = parent(hole);
1.170 + while( hole>0 && less(p,data[par]) ) {
1.171 + move(data[par],hole);
1.172 + hole = par;
1.173 + par = parent(hole);
1.174 + }
1.175 + move(p, hole);
1.176 + return hole;
1.177 + }
1.178 +
1.179 + template <typename K, typename V, typename M, typename C>
1.180 + int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
1.181 + int child = second_child(hole);
1.182 + while(child < length) {
1.183 + if( less(data[child-1], data[child]) ) {
1.184 + --child;
1.185 + }
1.186 + if( !less(data[child], p) )
1.187 + goto ok;
1.188 + move(data[child], hole);
1.189 + hole = child;
1.190 + child = second_child(hole);
1.191 + }
1.192 + child--;
1.193 + if( child<length && less(data[child], p) ) {
1.194 + move(data[child], hole);
1.195 + hole=child;
1.196 + }
1.197 + ok:
1.198 + move(p, hole);
1.199 + return hole;
1.200 + }
1.201 +
1.202 + ///@}
1.203 +
1.204 +} // namespace lemon
1.205 +
1.206 +#endif // LEMON_BIN_HEAP_H