First public contribution.
1 // (C) Copyright Jeremy Siek 2004.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef BOOST_FIBONACCI_HEAP_HPP
6 #define BOOST_FIBONACCI_HEAP_HPP
8 #if defined(__sgi) && !defined(__GNUC__)
16 #include <boost/config.hpp>
17 #include <boost/property_map.hpp>
20 // An adaptation of Knuth's Fibonacci heap implementation
21 // in "The Stanford Graph Base", pages 475-482.
28 class Compare = std::less<T>,
29 class ID = identity_property_map>
32 typedef typename boost::property_traits<ID>::value_type size_type;
35 typedef fibonacci_heap self;
36 typedef std::vector<size_type> LinkVec;
37 typedef typename LinkVec::iterator LinkIter;
40 fibonacci_heap(size_type n,
42 const ID& id = identity_property_map())
43 : _key(n), _left(n), _right(n), _p(n), _mark(n), _degree(n),
44 _n(0), _root(n), _id(id), _compare(cmp), _child(n),
45 #if defined(BOOST_MSVC) || defined(__ICL) // need a new macro?
46 new_roots(size_type(log(float(n))) + 5) { }
48 new_roots(size_type(std::log(float(n))) + 5) { }
52 void push(const T& d) {
54 size_type v = get(_id, d);
61 _root = _left[v] = _right[v] = v;
62 //std::cout << "root added" << std::endl;
64 size_type u = _left[_root];
67 _left[_root] = _right[u] = v;
68 if (_compare(d, _key[_root]))
70 //std::cout << "non-root node added" << std::endl;
73 T& top() { return _key[_root]; }
74 const T& top() const { return _key[_root]; }
82 if (_degree[_root] == 0) {
87 _right[w] = _right[_root];
88 for (w = v; w != _right[_root]; w = _right[w])
93 add_tree_to_new_roots(v, new_roots.begin(), h);
96 rebuild_root_list(new_roots.begin(), h);
100 inline void add_tree_to_new_roots(size_type v,
111 new_roots[h] = (h == r ? v : nil());
115 if (new_roots[r] == nil()) {
120 new_roots[r] = nil();
121 if (_compare(_key[u], _key[v])) {
131 void make_child(size_type u, size_type v, size_type r) {
137 size_type t = _child[v];
138 _right[u] = _right[t];
141 _left[_right[u]] = u;
146 inline void rebuild_root_list(LinkIter new_roots, int& h)
153 u = v = new_roots[h];
156 for (h--; h >= 0; --h)
157 if (new_roots[h] != nil()) {
161 if (_compare(_key[w], d)) {
173 void update(const T& d) {
174 size_type v = get(_id, d);
175 assert(!_compare(_key[v], d));
179 if (_compare(d, _key[_root]))
181 } else if (_compare(d, _key[_root]))
183 size_type r = _degree[p];
185 remove_from_family(v, p);
186 insert_into_forest(v, d);
187 size_type pp = _p[p];
192 if (_mark[p] == false) {
202 inline size_type size() const { return _n; }
203 inline bool empty() const { return _n == 0; }
205 void print(std::ostream& os) {
206 if (_root != nil()) {
212 } while (i != _root);
218 inline void remove_from_family(size_type v, size_type p) {
219 size_type u = _left[v];
220 size_type w = _right[v];
227 inline void insert_into_forest(size_type v, const T& d) {
229 size_type u = _left[_root];
232 _left[_root] = _right[u] = v;
233 if (_compare(d, _key[_root]))
237 void print_recur(size_type x, std::ostream& os) {
240 if (_child[x] != nil()) {
242 size_type i = _child[x];
244 print_recur(i, os); os << " ";
246 } while (i != _child[x]);
252 size_type nil() const { return _left.size(); }
255 LinkVec _left, _right, _p;
256 std::vector<bool> _mark;
268 #endif // BOOST_FIBONACCI_HEAP_HPP