2 * src/hugo/bin_heap.h - Part of HUGOlib, a generic C++ optimization library
4 * Copyright (C) 2004 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 HUGO_BIN_HEAP_H
18 #define HUGO_BIN_HEAP_H
22 ///\brief Binary Heap implementation.
23 ///\todo It should be documented.
31 /// \addtogroup auxdat
34 /// A Binary Heap implementation.
35 template <typename Item, typename Prio, typename ItemIntMap,
36 typename Compare = std::less<Prio> >
40 typedef Item ItemType;
41 // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
42 typedef Prio PrioType;
43 typedef std::pair<ItemType,PrioType> PairType;
44 typedef ItemIntMap ItemIntMapType;
45 typedef Compare PrioCompare;
48 * Each Item element have a state associated to it. It may be "in heap",
49 * "pre heap" or "post heap". The later two are indifferent from the
50 * heap's point of view, but may be useful to the user.
52 * The ItemIntMap _should_ be initialized in such way, that it maps
53 * PRE_HEAP (-1) to any element to be put in the heap...
55 ///\todo it is used nowhere
64 std::vector<PairType> data;
66 // FIXME: jo ez igy???
70 BinHeap(ItemIntMap &_iim) : iim(_iim) {}
71 BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {}
74 int size() const { return data.size(); }
75 bool empty() const { return data.empty(); }
78 static int parent(int i) { return (i-1)/2; }
79 static int second_child(int i) { return 2*i+2; }
80 bool less(const PairType &p1, const PairType &p2) const {
81 return comp(p1.second, p2.second);
84 int bubble_up(int hole, PairType p);
85 int bubble_down(int hole, PairType p, int length);
87 void move(const PairType &p, int i) {
93 int n = data.size()-1;
95 iim.set(data[h].first, POST_HEAP);
97 bubble_down(h, data[n], n);
104 void push(const PairType &p) {
109 void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
112 return data[0].first;
114 /// Returns the prio of the top element of the heap.
116 return data[0].second;
123 void erase(const Item &i) {
127 Prio operator[](const Item &i) const {
129 return data[idx].second;
132 void set(const Item &i, const Prio &p) {
137 else if( comp(p, data[idx].second) ) {
138 bubble_up(idx, PairType(i,p));
141 bubble_down(idx, PairType(i,p), data.size());
145 void decrease(const Item &i, const Prio &p) {
147 bubble_up(idx, PairType(i,p));
149 void increase(const Item &i, const Prio &p) {
151 bubble_down(idx, PairType(i,p), data.size());
154 state_enum state(const Item &i) const {
158 return state_enum(s);
164 template <typename K, typename V, typename M, typename C>
165 int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
166 int par = parent(hole);
167 while( hole>0 && less(p,data[par]) ) {
168 move(data[par],hole);
176 template <typename K, typename V, typename M, typename C>
177 int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
178 int child = second_child(hole);
179 while(child < length) {
180 if( less(data[child-1], data[child]) ) {
183 if( !less(data[child], p) )
185 move(data[child], hole);
187 child = second_child(hole);
190 if( child<length && less(data[child], p) ) {
191 move(data[child], hole);
203 #endif // HUGO_BIN_HEAP_H