Corrected spelling errors.
2 * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_BIN_HEAP_H
18 #define LEMON_BIN_HEAP_H
22 ///\brief Binary Heap implementation.
23 ///\todo It should be documented.
31 /// \addtogroup auxdat
34 /// A Binary Heap implementation.
36 ///\todo Please document...
40 template <typename Item, typename Prio, typename ItemIntMap,
41 typename Compare = std::less<Prio> >
45 typedef Item ItemType;
46 // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
47 typedef Prio PrioType;
48 typedef std::pair<ItemType,PrioType> PairType;
49 typedef ItemIntMap ItemIntMapType;
50 typedef Compare PrioCompare;
53 * Each Item element have a state associated to it. It may be "in heap",
54 * "pre heap" or "post heap". The later two are indifferent from the
55 * heap's point of view, but may be useful to the user.
57 * The ItemIntMap _should_ be initialized in such way, that it maps
58 * PRE_HEAP (-1) to any element to be put in the heap...
60 ///\todo it is used nowhere
69 std::vector<PairType> data;
71 // FIXME: jo ez igy???
76 BinHeap(ItemIntMap &_iim) : iim(_iim) {}
78 BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {}
82 int size() const { return data.size(); }
84 bool empty() const { return data.empty(); }
87 static int parent(int i) { return (i-1)/2; }
88 static int second_child(int i) { return 2*i+2; }
89 bool less(const PairType &p1, const PairType &p2) const {
90 return comp(p1.second, p2.second);
93 int bubble_up(int hole, PairType p);
94 int bubble_down(int hole, PairType p, int length);
96 void move(const PairType &p, int i) {
102 int n = data.size()-1;
104 iim.set(data[h].first, POST_HEAP);
106 bubble_down(h, data[n], n);
114 void push(const PairType &p) {
120 void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
124 return data[0].first;
126 /// Returns the prio of the top element of the heap.
128 return data[0].second;
137 void erase(const Item &i) {
142 Prio operator[](const Item &i) const {
144 return data[idx].second;
148 void set(const Item &i, const Prio &p) {
153 else if( comp(p, data[idx].second) ) {
154 bubble_up(idx, PairType(i,p));
157 bubble_down(idx, PairType(i,p), data.size());
162 void decrease(const Item &i, const Prio &p) {
164 bubble_up(idx, PairType(i,p));
167 void increase(const Item &i, const Prio &p) {
169 bubble_down(idx, PairType(i,p), data.size());
173 state_enum state(const Item &i) const {
177 return state_enum(s);
183 template <typename K, typename V, typename M, typename C>
184 int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
185 int par = parent(hole);
186 while( hole>0 && less(p,data[par]) ) {
187 move(data[par],hole);
195 template <typename K, typename V, typename M, typename C>
196 int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
197 int child = second_child(hole);
198 while(child < length) {
199 if( less(data[child-1], data[child]) ) {
202 if( !less(data[child], p) )
204 move(data[child], hole);
206 child = second_child(hole);
209 if( child<length && less(data[child], p) ) {
210 move(data[child], hole);
222 #endif // LEMON_BIN_HEAP_H