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__)
11 # include <boost/config/no_tr1/cmath.hpp>
16 #include <boost/config.hpp>
17 #include <boost/property_map/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])) {
133 void make_child(size_type u, size_type v, size_type r) {
139 size_type t = _child[v];
140 _right[u] = _right[t];
143 _left[_right[u]] = u;
148 inline void rebuild_root_list(LinkIter new_roots, int& h)
155 u = v = new_roots[h];
158 for (h--; h >= 0; --h)
159 if (new_roots[h] != nil()) {
163 if (_compare(_key[w], d)) {
175 void update(const T& d) {
176 size_type v = get(_id, d);
177 assert(!_compare(_key[v], d));
181 if (_compare(d, _key[_root]))
183 } else if (_compare(d, _key[p]))
185 size_type r = _degree[p];
187 remove_from_family(v, p);
188 insert_into_forest(v, d);
189 size_type pp = _p[p];
194 if (_mark[p] == false) {
205 inline size_type size() const { return _n; }
206 inline bool empty() const { return _n == 0; }
208 void print(std::ostream& os) {
209 if (_root != nil()) {
215 } while (i != _root);
221 inline void remove_from_family(size_type v, size_type p) {
222 size_type u = _left[v];
223 size_type w = _right[v];
230 inline void insert_into_forest(size_type v, const T& d) {
232 size_type u = _left[_root];
235 _left[_root] = _right[u] = v;
236 if (_compare(d, _key[_root]))
240 void print_recur(size_type x, std::ostream& os) {
243 if (_degree[x] > 0) {
245 size_type i = _child[x];
247 print_recur(i, os); os << " ";
249 } while (i != _child[x]);
255 size_type nil() const { return _left.size(); }
258 LinkVec _left, _right, _p;
259 std::vector<bool> _mark;
271 #endif // BOOST_FIBONACCI_HEAP_HPP