diff -r 2d6c8075d9d0 -r 818510fa3d99 src/lemon/bin_heap.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lemon/bin_heap.h Wed Sep 29 15:30:04 2004 +0000 @@ -0,0 +1,203 @@ +/* -*- C++ -*- + * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Combinatorial Optimization Research Group, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_BIN_HEAP_H +#define LEMON_BIN_HEAP_H + +///\ingroup auxdat +///\file +///\brief Binary Heap implementation. +///\todo It should be documented. + +#include +#include +#include + +namespace lemon { + + /// \addtogroup auxdat + /// @{ + + /// A Binary Heap implementation. + template > + class BinHeap { + + public: + typedef Item ItemType; + // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot... + typedef Prio PrioType; + typedef std::pair PairType; + typedef ItemIntMap ItemIntMapType; + typedef Compare PrioCompare; + + /** + * Each Item element have a state associated to it. It may be "in heap", + * "pre heap" or "post heap". The later two are indifferent from the + * heap's point of view, but may be useful to the user. + * + * The ItemIntMap _should_ be initialized in such way, that it maps + * PRE_HEAP (-1) to any element to be put in the heap... + */ + ///\todo it is used nowhere + /// + enum state_enum { + IN_HEAP = 0, + PRE_HEAP = -1, + POST_HEAP = -2 + }; + + private: + std::vector data; + Compare comp; + // FIXME: jo ez igy??? + ItemIntMap &iim; + + public: + BinHeap(ItemIntMap &_iim) : iim(_iim) {} + BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {} + + + int size() const { return data.size(); } + bool empty() const { return data.empty(); } + + private: + static int parent(int i) { return (i-1)/2; } + static int second_child(int i) { return 2*i+2; } + bool less(const PairType &p1, const PairType &p2) const { + return comp(p1.second, p2.second); + } + + int bubble_up(int hole, PairType p); + int bubble_down(int hole, PairType p, int length); + + void move(const PairType &p, int i) { + data[i] = p; + iim.set(p.first, i); + } + + void rmidx(int h) { + int n = data.size()-1; + if( h>=0 && h<=n ) { + iim.set(data[h].first, POST_HEAP); + if ( h=0 ) + s=0; + return state_enum(s); + } + + }; // class BinHeap + + + template + int BinHeap::bubble_up(int hole, PairType p) { + int par = parent(hole); + while( hole>0 && less(p,data[par]) ) { + move(data[par],hole); + hole = par; + par = parent(hole); + } + move(p, hole); + return hole; + } + + template + int BinHeap::bubble_down(int hole, PairType p, int length) { + int child = second_child(hole); + while(child < length) { + if( less(data[child-1], data[child]) ) { + --child; + } + if( !less(data[child], p) ) + goto ok; + move(data[child], hole); + hole = child; + child = second_child(hole); + } + child--; + if( child