Magic triangle is READY.
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 explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
78 BinHeap(ItemIntMap &_iim, const Compare &_comp)
79 : iim(_iim), comp(_comp) {}
83 int size() const { return data.size(); }
85 bool empty() const { return data.empty(); }
88 static int parent(int i) { return (i-1)/2; }
89 static int second_child(int i) { return 2*i+2; }
90 bool less(const PairType &p1, const PairType &p2) const {
91 return comp(p1.second, p2.second);
94 int bubble_up(int hole, PairType p);
95 int bubble_down(int hole, PairType p, int length);
97 void move(const PairType &p, int i) {
103 int n = data.size()-1;
105 iim.set(data[h].first, POST_HEAP);
107 bubble_down(h, data[n], n);
115 void push(const PairType &p) {
121 void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
125 return data[0].first;
127 /// Returns the prio of the top element of the heap.
129 return data[0].second;
138 void erase(const Item &i) {
143 Prio operator[](const Item &i) const {
145 return data[idx].second;
149 void set(const Item &i, const Prio &p) {
154 else if( comp(p, data[idx].second) ) {
155 bubble_up(idx, PairType(i,p));
158 bubble_down(idx, PairType(i,p), data.size());
163 void decrease(const Item &i, const Prio &p) {
165 bubble_up(idx, PairType(i,p));
168 void increase(const Item &i, const Prio &p) {
170 bubble_down(idx, PairType(i,p), data.size());
174 state_enum state(const Item &i) const {
178 return state_enum(s);
184 template <typename K, typename V, typename M, typename C>
185 int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
186 int par = parent(hole);
187 while( hole>0 && less(p,data[par]) ) {
188 move(data[par],hole);
196 template <typename K, typename V, typename M, typename C>
197 int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
198 int child = second_child(hole);
199 while(child < length) {
200 if( less(data[child-1], data[child]) ) {
203 if( !less(data[child], p) )
205 move(data[child], hole);
207 child = second_child(hole);
210 if( child<length && less(data[child], p) ) {
211 move(data[child], hole);
223 #endif // LEMON_BIN_HEAP_H