diff -r d8475431bbbb -r 8e85e6bbefdf src/lemon/bin_heap.h --- a/src/lemon/bin_heap.h Sat May 21 21:04:57 2005 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,303 +0,0 @@ -/* -*- C++ -*- - * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library - * - * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Research Group on Combinatorial Optimization, 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. - -#include -#include -#include - -namespace lemon { - - /// \addtogroup auxdat - /// @{ - - /// A Binary Heap implementation. - - ///This class implements the \e binary \e heap data structure. A \e heap - ///is a data structure for storing items with specified values called \e - ///priorities in such a way that finding the item with minimum priority is - ///efficient. \c Compare specifies the ordering of the priorities. In a heap - ///one can change the priority of an item, add or erase an item, etc. - /// - ///\param Item Type of the items to be stored. - ///\param Prio Type of the priority of the items. - ///\param ItemIntMap A read and writable Item int map, used internally - ///to handle the cross references. - ///\param Compare A class for the ordering of the priorities. The - ///default is \c std::less. - /// - ///\sa FibHeap - ///\sa Dijkstra - 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; - - /// \brief Type to represent the items states. - /// - /// Each Item element have a state associated to it. It may be "in heap", - /// "pre heap" or "post heap". The latter two are indifferent from the - /// heap's point of view, but may be useful to the user. - /// - /// The ItemIntMap \e should be initialized in such way that it maps - /// PRE_HEAP (-1) to any element to be put in the heap... - enum state_enum { - IN_HEAP = 0, - PRE_HEAP = -1, - POST_HEAP = -2 - }; - - private: - std::vector data; - Compare comp; - ItemIntMap &iim; - - public: - /// \brief The constructor. - /// - /// The constructor. - /// \param _iim should be given to the constructor, since it is used - /// internally to handle the cross references. The value of the map - /// should be PRE_HEAP (-1) for each element. - explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {} - - /// \brief The constructor. - /// - /// The constructor. - /// \param _iim should be given to the constructor, since it is used - /// internally to handle the cross references. The value of the map - /// should be PRE_HEAP (-1) for each element. - /// - /// \param _comp The comparator function object. - BinHeap(ItemIntMap &_iim, const Compare &_comp) - : iim(_iim), comp(_comp) {} - - - /// The number of items stored in the heap. - /// - /// \brief Returns the number of items stored in the heap. - int size() const { return data.size(); } - - /// \brief Checks if the heap stores no items. - /// - /// Returns \c true if and only if the heap stores no items. - 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