1 // Copyright 2004 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Douglas Gregor
9 #warning "Use of relaxed_heap is depreciated; please use the standard heap functions."
10 #ifndef BOOST_RELAXED_HEAP_HEADER
11 #define BOOST_RELAXED_HEAP_HEADER
13 #include <boost/config/header_deprecated.hpp>
14 BOOST_HEADER_DEPRECATED("the standard heap functions")
17 #include <boost/property_map/property_map.hpp>
18 #include <boost/optional.hpp>
20 #include <climits> // for CHAR_BIT
21 #include <boost/none.hpp>
23 #ifdef BOOST_RELAXED_HEAP_DEBUG
25 #endif // BOOST_RELAXED_HEAP_DEBUG
27 #if defined(BOOST_MSVC)
28 # pragma warning(push)
29 # pragma warning(disable:4355) // complaint about using 'this' to
30 #endif // initialize a member
34 template<typename IndexedType,
35 typename Compare = std::less<IndexedType>,
36 typename ID = identity_property_map>
41 typedef relaxed_heap self_type;
42 typedef std::size_t rank_type;
45 typedef IndexedType value_type;
46 typedef rank_type size_type;
50 * The kind of key that a group has. The actual values are discussed
51 * in-depth in the documentation of the @c kind field of the @c group
52 * structure. Note that the order of the enumerators *IS* important
53 * and must not be changed.
55 enum group_key_kind { smallest_key, stored_key, largest_key };
58 explicit group(group_key_kind kind = largest_key)
59 : kind(kind), parent(this), rank(0) { }
61 /** The value associated with this group. This value is only valid
62 * when @c kind!=largest_key (which indicates a deleted
63 * element). Note that the use of boost::optional increases the
64 * memory requirements slightly but does not result in extraneous
65 * memory allocations or deallocations. The optional could be
66 * eliminated when @c value_type is a model of
67 * DefaultConstructible.
69 ::boost::optional<value_type> value;
72 * The kind of key stored at this group. This may be @c
73 * smallest_key, which indicates that the key is infinitely small;
74 * @c largest_key, which indicates that the key is infinitely
75 * large; or @c stored_key, which means that the key is unknown,
76 * but its relationship to other keys can be determined via the
77 * comparison function object.
81 /// The parent of this group. Will only be NULL for the dummy root group
84 /// The rank of this group. Equivalent to the number of children in
88 /** The children of this group. For the dummy root group, these are
89 * the roots. This is an array of length log n containing pointers
90 * to the child groups.
95 size_type log_base_2(size_type n) // log2 is a macro on some platforms
97 size_type leading_zeroes = 0;
99 size_type next = n << 1;
100 if (n == (next >> 1)) {
107 return sizeof(size_type) * CHAR_BIT - leading_zeroes - 1;
111 relaxed_heap(size_type n, const Compare& compare = Compare(),
113 : compare(compare), id(id), root(smallest_key), groups(n),
117 root.children = new group*[1];
121 log_n = log_base_2(n);
122 if (log_n == 0) log_n = 1;
123 size_type g = n / log_n;
124 if (n % log_n > 0) ++g;
125 size_type log_g = log_base_2(g);
128 // Reserve an appropriate amount of space for data structures, so
129 // that we do not need to expand them.
130 index_to_group.resize(g);
133 root.children = new group*[(log_g + 1) * (g + 1)];
134 for (rank_type i = 0; i < r+1; ++i) root.children[i] = 0;
136 // Build initial heap
139 root.children[r] = &index_to_group[idx];
140 idx = build_tree(root, idx, r, log_g + 1);
142 r = static_cast<size_type>(log_base_2(g-idx));
146 ~relaxed_heap() { delete [] root.children; }
148 void push(const value_type& x)
150 groups[get(id, x)] = x;
154 void update(const value_type& x)
156 group* a = &index_to_group[get(id, x) / log_n];
159 || compare(x, *a->value)) {
160 if (a != smallest_value) smallest_value = 0;
161 a->kind = stored_key;
167 void remove(const value_type& x)
169 group* a = &index_to_group[get(id, x) / log_n];
170 assert(groups[get(id, x)]);
172 a->kind = smallest_key;
181 assert(smallest_value->value != none);
182 return *smallest_value->value;
185 const value_type& top() const
188 assert(smallest_value->value != none);
189 return *smallest_value->value;
195 return !smallest_value || (smallest_value->kind == largest_key);
198 bool contains(const value_type& x) const {
199 return static_cast<bool>(groups[get(id, x)]);
204 // Fill in smallest_value. This is the group x.
206 group* x = smallest_value;
209 // Make x a leaf, giving it the smallest value within its group
210 rank_type r = x->rank;
211 group* p = x->parent;
213 assert(x->value != none);
216 size_type start = get(id, *x->value) - get(id, *x->value) % log_n;
217 size_type end = start + log_n;
218 if (end > groups.size()) end = groups.size();
220 // Remove the smallest value from the group, and find the new
222 groups[get(id, *x->value)].reset();
224 x->kind = largest_key;
225 for (size_type i = start; i < end; ++i) {
226 if (groups[i] && (!x->value || compare(*groups[i], *x->value))) {
227 x->kind = stored_key;
228 x->value = groups[i];
234 // Combine prior children of x with x
236 for (size_type c = 0; c < r; ++c) {
237 group* child = x->children[c];
238 if (A[c] == child) A[c] = 0;
239 y = combine(y, child);
242 // If we got back something other than x, let y take x's place
247 assert(r == y->rank);
249 A[y->rank] = do_compare(y, p)? y : 0;
253 #ifdef BOOST_RELAXED_HEAP_DEBUG
254 /*************************************************************************
255 * Debugging support *
256 *************************************************************************/
257 void dump_tree() { dump_tree(std::cout); }
258 void dump_tree(std::ostream& out) { dump_tree(out, &root); }
260 void dump_tree(std::ostream& out, group* p, bool in_progress = false)
263 out << "digraph heap {\n"
264 << " edge[dir=\"back\"];\n";
267 size_type p_index = 0;
268 if (p != &root) while (&index_to_group[p_index] != p) ++p_index;
270 for (size_type i = 0; i < p->rank; ++i) {
271 group* c = p->children[i];
273 size_type c_index = 0;
274 if (c != &root) while (&index_to_group[c_index] != c) ++c_index;
277 if (p == &root) out << 'p'; else out << p_index;
279 if (c == &root) out << 'p'; else out << c_index;
280 if (A[c->rank] == c) out << " [style=\"dotted\"]";
282 dump_tree(out, c, true);
284 // Emit node information
286 if (c == &root) out << 'p'; else out << c_index;
288 if (c == &root) out << 'p'; else out << c_index;
290 size_type start = c_index * log_n;
291 size_type end = start + log_n;
292 if (end > groups.size()) end = groups.size();
293 while (start != end) {
295 out << " " << get(id, *groups[start]);
296 if (*groups[start] == *c->value) out << "(*)";
302 if (do_compare(c, p)) {
304 if (c == &root) out << 'p'; else out << c_index;
305 out << ", style=\"filled\", fillcolor=\"gray\"";
309 assert(p->parent == p);
312 if (!in_progress) out << "}\n";
317 // Check that the ranks in the A array match the ranks of the
318 // groups stored there. Also, the active groups must be the last
319 // child of their parent.
320 for (size_type r = 0; r < A.size(); ++r) {
321 if (A[r] && A[r]->rank != r) return false;
323 if (A[r] && A[r]->parent->children[A[r]->parent->rank-1] != A[r])
327 // The root must have no value and a key of -Infinity
328 if (root.kind != smallest_key) return false;
335 for (size_type i = 0; i < p->rank; ++i) {
336 group* c = p->children[i];
338 // Check link structure
339 if (c->parent != p) return false;
340 if (c->rank != i) return false;
342 // A bad group must be active
343 if (do_compare(c, p) && A[i] != c) return false;
346 if (!valid(c)) return false;
349 if (p != &root) return false;
355 #endif // BOOST_RELAXED_HEAP_DEBUG
359 build_tree(group& parent, size_type idx, size_type r, size_type max_rank)
361 group& this_group = index_to_group[idx];
362 this_group.parent = &parent;
365 this_group.children = root.children + (idx * max_rank);
367 for (size_type i = 0; i < r; ++i) {
368 this_group.children[i] = &index_to_group[idx];
369 idx = build_tree(this_group, idx, i, max_rank);
374 void find_smallest() const
376 group** roots = root.children;
378 if (!smallest_value) {
380 for (i = 0; i < root.rank; ++i) {
382 (!smallest_value || do_compare(roots[i], smallest_value))) {
383 smallest_value = roots[i];
386 for (i = 0; i < A.size(); ++i) {
387 if (A[i] && (!smallest_value || do_compare(A[i], smallest_value)))
388 smallest_value = A[i];
393 bool do_compare(group* x, group* y) const
395 return (x->kind < y->kind
396 || (x->kind == y->kind
397 && x->kind == stored_key
398 && compare(*x->value, *y->value)));
401 void promote(group* a)
404 rank_type r = a->rank;
405 group* p = a->parent;
407 if (do_compare(a, p)) {
408 // s is the rank + 1 sibling
409 group* s = p->rank > r + 1? p->children[r + 1] : 0;
411 // If a is the last child of p
412 if (r == p->rank - 1) {
414 else if (A[r] != a) pair_transform(a);
417 if (A[r + 1] == s) active_sibling_transform(a, s);
418 else good_sibling_transform(a, s);
423 group* combine(group* a1, group* a2)
425 assert(a1->rank == a2->rank);
426 if (do_compare(a2, a1)) do_swap(a1, a2);
427 a1->children[a1->rank++] = a2;
435 if (2 > q->rank) return;
436 group* qp = q->children[q->rank-1];
437 rank_type s = q->rank - 2;
438 group* x = q->children[s];
439 group* xp = qp->children[s];
440 assert(s == x->rank);
442 // If x is active, swap x and xp
451 void pair_transform(group* a)
453 #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
454 std::cerr << "- pair transform\n";
456 rank_type r = a->rank;
459 group* p = a->parent;
462 // g is p's parent (a's grandparent)
463 group* g = p->parent;
474 // let a' have parent p'
475 group* pp = ap->parent;
478 // let a' have grandparent g'
479 group* gp = pp->parent;
482 // Remove a and a' from their parents
483 assert(ap == pp->children[pp->rank-1]); // Guaranteed because ap is active
486 // Guaranteed by caller
487 assert(a == p->children[p->rank-1]);
490 // Note: a, ap, p, pp all have rank r
491 if (do_compare(pp, p)) {
497 // Assuming k(p) <= k(p')
498 // make p' the rank r child of p
499 assert(r == p->rank);
500 p->children[p->rank++] = pp;
503 // Combine a, ap into a rank r+1 group c
504 group* c = combine(a, ap);
506 // make c the rank r+1 child of g'
507 assert(gp->rank > r+1);
508 gp->children[r+1] = c;
511 #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
512 std::cerr << "After pair transform...\n";
516 if (A[r+1] == pp) A[r+1] = c;
520 void active_sibling_transform(group* a, group* s)
522 #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
523 std::cerr << "- active sibling transform\n";
525 group* p = a->parent;
526 group* g = p->parent;
528 // remove a, s from their parents
529 assert(s->parent == p);
530 assert(p->children[p->rank-1] == s);
532 assert(p->children[p->rank-1] == a);
535 rank_type r = a->rank;
538 group* c = combine(a, s);
540 // make c the rank r+2 child of g
541 assert(g->children[r+2] == p);
542 g->children[r+2] = c;
544 if (A[r+2] == p) A[r+2] = c;
548 void good_sibling_transform(group* a, group* s)
550 #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
551 std::cerr << "- good sibling transform\n";
553 rank_type r = a->rank;
554 group* c = s->children[s->rank-1];
555 assert(c->rank == r);
557 #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
558 std::cerr << "- good sibling pair transform\n";
561 group* p = a->parent;
563 // Remove c from its parent
566 // Make s the rank r child of p
570 // combine a, c and let the result by the rank r+1 child of p
571 assert(p->rank > r+1);
572 group* x = combine(a, c);
574 p->children[r+1] = x;
576 if (A[r+1] == s) A[r+1] = x;
579 #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
580 dump_tree(std::cerr);
582 // pair_transform(a);
585 group* p = a->parent;
595 static void do_swap(group*& x, group*& y)
602 /// Function object that compares two values in the heap
605 /// Mapping from values to indices in the range [0, n).
608 /** The root group of the queue. This group is special because it will
609 * never store a value, but it acts as a parent to all of the
610 * roots. Thus, its list of children is the list of roots.
614 /** Mapping from the group index of a value to the group associated
615 * with that value. If a value is not in the queue, then the "value"
616 * field will be empty.
618 std::vector<group> index_to_group;
620 /** Flat data structure containing the values in each of the
621 * groups. It will be indexed via the id of the values. The groups
622 * are each log_n long, with the last group potentially being
625 std::vector< ::boost::optional<value_type> > groups;
627 /** The list of active groups, indexed by rank. When A[r] is null,
628 * there is no active group of rank r. Otherwise, A[r] is the active
631 std::vector<group*> A;
633 /** The group containing the smallest value in the queue, which must
634 * be either a root or an active group. If this group is null, then we
635 * will need to search for this group when it is needed.
637 mutable group* smallest_value;
639 /// Cached value log_base_2(n)
644 } // end namespace boost
646 #if defined(BOOST_MSVC)
647 # pragma warning(pop)
650 #endif // BOOST_RELAXED_HEAP_HEADER