alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@906
|
2 |
* src/hugo/bin_heap.h - Part of HUGOlib, a generic C++ optimization library
|
klao@39
|
3 |
*
|
alpar@906
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
klao@39
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
klao@39
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
klao@39
|
14 |
*
|
klao@39
|
15 |
*/
|
klao@39
|
16 |
|
ladanyi@542
|
17 |
#ifndef HUGO_BIN_HEAP_H
|
ladanyi@542
|
18 |
#define HUGO_BIN_HEAP_H
|
klao@37
|
19 |
|
klao@491
|
20 |
///\ingroup auxdat
|
klao@274
|
21 |
///\file
|
klao@274
|
22 |
///\brief Binary Heap implementation.
|
alpar@906
|
23 |
///\todo It should be documented.
|
klao@274
|
24 |
|
klao@37
|
25 |
#include <vector>
|
klao@37
|
26 |
#include <utility>
|
klao@37
|
27 |
#include <functional>
|
klao@37
|
28 |
|
alpar@114
|
29 |
namespace hugo {
|
klao@37
|
30 |
|
alpar@430
|
31 |
/// \addtogroup auxdat
|
alpar@430
|
32 |
/// @{
|
alpar@430
|
33 |
|
alpar@430
|
34 |
/// A Binary Heap implementation.
|
klao@172
|
35 |
template <typename Item, typename Prio, typename ItemIntMap,
|
klao@172
|
36 |
typename Compare = std::less<Prio> >
|
klao@37
|
37 |
class BinHeap {
|
klao@37
|
38 |
|
klao@37
|
39 |
public:
|
klao@172
|
40 |
typedef Item ItemType;
|
klao@37
|
41 |
// FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
|
klao@172
|
42 |
typedef Prio PrioType;
|
klao@172
|
43 |
typedef std::pair<ItemType,PrioType> PairType;
|
klao@172
|
44 |
typedef ItemIntMap ItemIntMapType;
|
klao@172
|
45 |
typedef Compare PrioCompare;
|
klao@37
|
46 |
|
klao@37
|
47 |
/**
|
klao@172
|
48 |
* Each Item element have a state associated to it. It may be "in heap",
|
klao@37
|
49 |
* "pre heap" or "post heap". The later two are indifferent from the
|
klao@37
|
50 |
* heap's point of view, but may be useful to the user.
|
klao@37
|
51 |
*
|
klao@172
|
52 |
* The ItemIntMap _should_ be initialized in such way, that it maps
|
klao@37
|
53 |
* PRE_HEAP (-1) to any element to be put in the heap...
|
klao@37
|
54 |
*/
|
klao@274
|
55 |
///\todo it is used nowhere
|
klao@274
|
56 |
///
|
klao@39
|
57 |
enum state_enum {
|
klao@37
|
58 |
IN_HEAP = 0,
|
klao@37
|
59 |
PRE_HEAP = -1,
|
klao@37
|
60 |
POST_HEAP = -2
|
klao@37
|
61 |
};
|
klao@37
|
62 |
|
klao@37
|
63 |
private:
|
klao@37
|
64 |
std::vector<PairType> data;
|
klao@37
|
65 |
Compare comp;
|
klao@37
|
66 |
// FIXME: jo ez igy???
|
klao@172
|
67 |
ItemIntMap &iim;
|
klao@37
|
68 |
|
klao@37
|
69 |
public:
|
klao@172
|
70 |
BinHeap(ItemIntMap &_iim) : iim(_iim) {}
|
klao@172
|
71 |
BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {}
|
klao@37
|
72 |
|
klao@37
|
73 |
|
klao@37
|
74 |
int size() const { return data.size(); }
|
klao@41
|
75 |
bool empty() const { return data.empty(); }
|
klao@37
|
76 |
|
klao@37
|
77 |
private:
|
klao@37
|
78 |
static int parent(int i) { return (i-1)/2; }
|
klao@37
|
79 |
static int second_child(int i) { return 2*i+2; }
|
klao@214
|
80 |
bool less(const PairType &p1, const PairType &p2) const {
|
klao@37
|
81 |
return comp(p1.second, p2.second);
|
klao@37
|
82 |
}
|
klao@37
|
83 |
|
klao@37
|
84 |
int bubble_up(int hole, PairType p);
|
klao@37
|
85 |
int bubble_down(int hole, PairType p, int length);
|
klao@37
|
86 |
|
klao@37
|
87 |
void move(const PairType &p, int i) {
|
klao@37
|
88 |
data[i] = p;
|
klao@172
|
89 |
iim.set(p.first, i);
|
klao@37
|
90 |
}
|
klao@37
|
91 |
|
klao@41
|
92 |
void rmidx(int h) {
|
klao@41
|
93 |
int n = data.size()-1;
|
klao@41
|
94 |
if( h>=0 && h<=n ) {
|
klao@172
|
95 |
iim.set(data[h].first, POST_HEAP);
|
klao@41
|
96 |
if ( h<n ) {
|
klao@41
|
97 |
bubble_down(h, data[n], n);
|
klao@41
|
98 |
}
|
klao@41
|
99 |
data.pop_back();
|
klao@41
|
100 |
}
|
klao@41
|
101 |
}
|
klao@41
|
102 |
|
klao@37
|
103 |
public:
|
klao@37
|
104 |
void push(const PairType &p) {
|
klao@37
|
105 |
int n = data.size();
|
klao@37
|
106 |
data.resize(n+1);
|
klao@37
|
107 |
bubble_up(n, p);
|
klao@37
|
108 |
}
|
klao@172
|
109 |
void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
|
klao@37
|
110 |
|
klao@172
|
111 |
Item top() const {
|
klao@37
|
112 |
return data[0].first;
|
klao@37
|
113 |
}
|
klao@274
|
114 |
/// Returns the prio of the top element of the heap.
|
klao@274
|
115 |
Prio prio() const {
|
klao@37
|
116 |
return data[0].second;
|
klao@37
|
117 |
}
|
klao@37
|
118 |
|
klao@37
|
119 |
void pop() {
|
klao@41
|
120 |
rmidx(0);
|
klao@41
|
121 |
}
|
klao@41
|
122 |
|
klao@172
|
123 |
void erase(const Item &i) {
|
jacint@221
|
124 |
rmidx(iim[i]);
|
klao@37
|
125 |
}
|
klao@37
|
126 |
|
klao@274
|
127 |
Prio operator[](const Item &i) const {
|
jacint@221
|
128 |
int idx = iim[i];
|
klao@37
|
129 |
return data[idx].second;
|
klao@37
|
130 |
}
|
klao@274
|
131 |
|
klao@172
|
132 |
void set(const Item &i, const Prio &p) {
|
jacint@221
|
133 |
int idx = iim[i];
|
klao@37
|
134 |
if( idx < 0 ) {
|
klao@172
|
135 |
push(i,p);
|
klao@37
|
136 |
}
|
klao@172
|
137 |
else if( comp(p, data[idx].second) ) {
|
klao@172
|
138 |
bubble_up(idx, PairType(i,p));
|
klao@37
|
139 |
}
|
klao@37
|
140 |
else {
|
klao@172
|
141 |
bubble_down(idx, PairType(i,p), data.size());
|
klao@37
|
142 |
}
|
klao@37
|
143 |
}
|
klao@37
|
144 |
|
klao@172
|
145 |
void decrease(const Item &i, const Prio &p) {
|
jacint@221
|
146 |
int idx = iim[i];
|
klao@172
|
147 |
bubble_up(idx, PairType(i,p));
|
klao@37
|
148 |
}
|
klao@172
|
149 |
void increase(const Item &i, const Prio &p) {
|
jacint@221
|
150 |
int idx = iim[i];
|
klao@172
|
151 |
bubble_down(idx, PairType(i,p), data.size());
|
klao@37
|
152 |
}
|
klao@37
|
153 |
|
klao@172
|
154 |
state_enum state(const Item &i) const {
|
jacint@221
|
155 |
int s = iim[i];
|
klao@39
|
156 |
if( s>=0 )
|
klao@39
|
157 |
s=0;
|
klao@39
|
158 |
return state_enum(s);
|
klao@39
|
159 |
}
|
klao@39
|
160 |
|
klao@37
|
161 |
}; // class BinHeap
|
klao@37
|
162 |
|
klao@37
|
163 |
|
klao@37
|
164 |
template <typename K, typename V, typename M, typename C>
|
klao@37
|
165 |
int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
|
klao@37
|
166 |
int par = parent(hole);
|
klao@37
|
167 |
while( hole>0 && less(p,data[par]) ) {
|
klao@37
|
168 |
move(data[par],hole);
|
klao@37
|
169 |
hole = par;
|
klao@37
|
170 |
par = parent(hole);
|
klao@37
|
171 |
}
|
klao@37
|
172 |
move(p, hole);
|
klao@37
|
173 |
return hole;
|
klao@37
|
174 |
}
|
klao@37
|
175 |
|
klao@37
|
176 |
template <typename K, typename V, typename M, typename C>
|
klao@37
|
177 |
int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
|
klao@37
|
178 |
int child = second_child(hole);
|
klao@37
|
179 |
while(child < length) {
|
klao@37
|
180 |
if( less(data[child-1], data[child]) ) {
|
klao@37
|
181 |
--child;
|
klao@37
|
182 |
}
|
klao@37
|
183 |
if( !less(data[child], p) )
|
klao@37
|
184 |
goto ok;
|
klao@37
|
185 |
move(data[child], hole);
|
klao@37
|
186 |
hole = child;
|
klao@37
|
187 |
child = second_child(hole);
|
klao@37
|
188 |
}
|
klao@37
|
189 |
child--;
|
klao@37
|
190 |
if( child<length && less(data[child], p) ) {
|
klao@37
|
191 |
move(data[child], hole);
|
klao@37
|
192 |
hole=child;
|
klao@37
|
193 |
}
|
klao@37
|
194 |
ok:
|
klao@37
|
195 |
move(p, hole);
|
klao@37
|
196 |
return hole;
|
klao@37
|
197 |
}
|
klao@37
|
198 |
|
alpar@430
|
199 |
///@}
|
alpar@430
|
200 |
|
alpar@114
|
201 |
} // namespace hugo
|
klao@37
|
202 |
|
marci@901
|
203 |
#endif // HUGO_BIN_HEAP_H
|