sl@0: // Copyright 2004 The Trustees of Indiana University. sl@0: sl@0: // Use, modification and distribution is subject to the Boost Software sl@0: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: // Authors: Douglas Gregor sl@0: // Andrew Lumsdaine sl@0: #ifndef BOOST_RELAXED_HEAP_HEADER sl@0: #define BOOST_RELAXED_HEAP_HEADER sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef BOOST_RELAXED_HEAP_DEBUG sl@0: # include sl@0: #endif // BOOST_RELAXED_HEAP_DEBUG sl@0: sl@0: #if defined(BOOST_MSVC) sl@0: # pragma warning(push) sl@0: # pragma warning(disable:4355) // complaint about using 'this' to sl@0: #endif // initialize a member sl@0: sl@0: namespace boost { sl@0: sl@0: template, sl@0: typename ID = identity_property_map> sl@0: class relaxed_heap sl@0: { sl@0: struct group; sl@0: sl@0: typedef relaxed_heap self_type; sl@0: typedef std::size_t rank_type; sl@0: sl@0: public: sl@0: typedef IndexedType value_type; sl@0: typedef rank_type size_type; sl@0: sl@0: private: sl@0: /** sl@0: * The kind of key that a group has. The actual values are discussed sl@0: * in-depth in the documentation of the @c kind field of the @c group sl@0: * structure. Note that the order of the enumerators *IS* important sl@0: * and must not be changed. sl@0: */ sl@0: enum group_key_kind { smallest_key, stored_key, largest_key }; sl@0: sl@0: struct group { sl@0: explicit group(group_key_kind kind = largest_key) sl@0: : kind(kind), parent(this), rank(0) { } sl@0: sl@0: /** The value associated with this group. This value is only valid sl@0: * when @c kind!=largest_key (which indicates a deleted sl@0: * element). Note that the use of boost::optional increases the sl@0: * memory requirements slightly but does not result in extraneous sl@0: * memory allocations or deallocations. The optional could be sl@0: * eliminated when @c value_type is a model of sl@0: * DefaultConstructible. sl@0: */ sl@0: ::boost::optional value; sl@0: sl@0: /** sl@0: * The kind of key stored at this group. This may be @c sl@0: * smallest_key, which indicates that the key is infinitely small; sl@0: * @c largest_key, which indicates that the key is infinitely sl@0: * large; or @c stored_key, which means that the key is unknown, sl@0: * but its relationship to other keys can be determined via the sl@0: * comparison function object. sl@0: */ sl@0: group_key_kind kind; sl@0: sl@0: /// The parent of this group. Will only be NULL for the dummy root group sl@0: group* parent; sl@0: sl@0: /// The rank of this group. Equivalent to the number of children in sl@0: /// the group. sl@0: rank_type rank; sl@0: sl@0: /** The children of this group. For the dummy root group, these are sl@0: * the roots. This is an array of length log n containing pointers sl@0: * to the child groups. sl@0: */ sl@0: group** children; sl@0: }; sl@0: sl@0: size_type log_base_2(size_type n) // log2 is a macro on some platforms sl@0: { sl@0: size_type leading_zeroes = 0; sl@0: do { sl@0: size_type next = n << 1; sl@0: if (n == (next >> 1)) { sl@0: ++leading_zeroes; sl@0: n = next; sl@0: } else { sl@0: break; sl@0: } sl@0: } while (true); sl@0: return sizeof(size_type) * CHAR_BIT - leading_zeroes - 1; sl@0: } sl@0: sl@0: public: sl@0: relaxed_heap(size_type n, const Compare& compare = Compare(), sl@0: const ID& id = ID()) sl@0: : compare(compare), id(id), root(smallest_key), groups(n), sl@0: smallest_value(0) sl@0: { sl@0: if (n == 0) { sl@0: root.children = new group*[1]; sl@0: return; sl@0: } sl@0: sl@0: log_n = log_base_2(n); sl@0: if (log_n == 0) log_n = 1; sl@0: size_type g = n / log_n; sl@0: if (n % log_n > 0) ++g; sl@0: size_type log_g = log_base_2(g); sl@0: size_type r = log_g; sl@0: sl@0: // Reserve an appropriate amount of space for data structures, so sl@0: // that we do not need to expand them. sl@0: index_to_group.resize(g); sl@0: A.resize(r + 1, 0); sl@0: root.rank = r + 1; sl@0: root.children = new group*[(log_g + 1) * (g + 1)]; sl@0: for (rank_type i = 0; i < r+1; ++i) root.children[i] = 0; sl@0: sl@0: // Build initial heap sl@0: size_type idx = 0; sl@0: while (idx < g) { sl@0: root.children[r] = &index_to_group[idx]; sl@0: idx = build_tree(root, idx, r, log_g + 1); sl@0: if (idx != g) sl@0: r = static_cast(log_base_2(g-idx)); sl@0: } sl@0: } sl@0: sl@0: ~relaxed_heap() { delete [] root.children; } sl@0: sl@0: void push(const value_type& x) sl@0: { sl@0: groups[get(id, x)] = x; sl@0: update(x); sl@0: } sl@0: sl@0: void update(const value_type& x) sl@0: { sl@0: group* a = &index_to_group[get(id, x) / log_n]; sl@0: if (!a->value sl@0: || *a->value == x sl@0: || compare(x, *a->value)) { sl@0: if (a != smallest_value) smallest_value = 0; sl@0: a->kind = stored_key; sl@0: a->value = x; sl@0: promote(a); sl@0: } sl@0: } sl@0: sl@0: void remove(const value_type& x) sl@0: { sl@0: group* a = &index_to_group[get(id, x) / log_n]; sl@0: assert(groups[get(id, x)] != 0); sl@0: a->value = x; sl@0: a->kind = smallest_key; sl@0: promote(a); sl@0: smallest_value = a; sl@0: pop(); sl@0: } sl@0: sl@0: value_type& top() sl@0: { sl@0: find_smallest(); sl@0: assert(smallest_value->value != 0); sl@0: return *smallest_value->value; sl@0: } sl@0: sl@0: const value_type& top() const sl@0: { sl@0: find_smallest(); sl@0: assert(smallest_value->value != 0); sl@0: return *smallest_value->value; sl@0: } sl@0: sl@0: bool empty() const sl@0: { sl@0: find_smallest(); sl@0: return !smallest_value || (smallest_value->kind == largest_key); sl@0: } sl@0: sl@0: bool contains(const value_type& x) const { return groups[get(id, x)]; } sl@0: sl@0: void pop() sl@0: { sl@0: // Fill in smallest_value. This is the group x. sl@0: find_smallest(); sl@0: group* x = smallest_value; sl@0: smallest_value = 0; sl@0: sl@0: // Make x a leaf, giving it the smallest value within its group sl@0: rank_type r = x->rank; sl@0: group* p = x->parent; sl@0: { sl@0: assert(x->value != 0); sl@0: sl@0: // Find x's group sl@0: size_type start = get(id, *x->value) - get(id, *x->value) % log_n; sl@0: size_type end = start + log_n; sl@0: if (end > groups.size()) end = groups.size(); sl@0: sl@0: // Remove the smallest value from the group, and find the new sl@0: // smallest value. sl@0: groups[get(id, *x->value)].reset(); sl@0: x->value.reset(); sl@0: x->kind = largest_key; sl@0: for (size_type i = start; i < end; ++i) { sl@0: if (groups[i] && (!x->value || compare(*groups[i], *x->value))) { sl@0: x->kind = stored_key; sl@0: x->value = groups[i]; sl@0: } sl@0: } sl@0: } sl@0: x->rank = 0; sl@0: sl@0: // Combine prior children of x with x sl@0: group* y = x; sl@0: for (size_type c = 0; c < r; ++c) { sl@0: group* child = x->children[c]; sl@0: if (A[c] == child) A[c] = 0; sl@0: y = combine(y, child); sl@0: } sl@0: sl@0: // If we got back something other than x, let y take x's place sl@0: if (y != x) { sl@0: y->parent = p; sl@0: p->children[r] = y; sl@0: sl@0: assert(r == y->rank); sl@0: if (A[y->rank] == x) sl@0: A[y->rank] = do_compare(y, p)? y : 0; sl@0: } sl@0: } sl@0: sl@0: #ifdef BOOST_RELAXED_HEAP_DEBUG sl@0: /************************************************************************* sl@0: * Debugging support * sl@0: *************************************************************************/ sl@0: void dump_tree() { dump_tree(std::cout); } sl@0: void dump_tree(std::ostream& out) { dump_tree(out, &root); } sl@0: sl@0: void dump_tree(std::ostream& out, group* p, bool in_progress = false) sl@0: { sl@0: if (!in_progress) { sl@0: out << "digraph heap {\n" sl@0: << " edge[dir=\"back\"];\n"; sl@0: } sl@0: sl@0: size_type p_index = 0; sl@0: if (p != &root) while (&index_to_group[p_index] != p) ++p_index; sl@0: sl@0: for (size_type i = 0; i < p->rank; ++i) { sl@0: group* c = p->children[i]; sl@0: if (c) { sl@0: size_type c_index = 0; sl@0: if (c != &root) while (&index_to_group[c_index] != c) ++c_index; sl@0: sl@0: out << " "; sl@0: if (p == &root) out << 'p'; else out << p_index; sl@0: out << " -> "; sl@0: if (c == &root) out << 'p'; else out << c_index; sl@0: if (A[c->rank] == c) out << " [style=\"dotted\"]"; sl@0: out << ";\n"; sl@0: dump_tree(out, c, true); sl@0: sl@0: // Emit node information sl@0: out << " "; sl@0: if (c == &root) out << 'p'; else out << c_index; sl@0: out << " [label=\""; sl@0: if (c == &root) out << 'p'; else out << c_index; sl@0: out << ":"; sl@0: size_type start = c_index * log_n; sl@0: size_type end = start + log_n; sl@0: if (end > groups.size()) end = groups.size(); sl@0: while (start != end) { sl@0: if (groups[start]) { sl@0: out << " " << get(id, *groups[start]); sl@0: if (*groups[start] == *c->value) out << "(*)"; sl@0: } sl@0: ++start; sl@0: } sl@0: out << '"'; sl@0: sl@0: if (do_compare(c, p)) { sl@0: out << " "; sl@0: if (c == &root) out << 'p'; else out << c_index; sl@0: out << ", style=\"filled\", fillcolor=\"gray\""; sl@0: } sl@0: out << "];\n"; sl@0: } else { sl@0: assert(p->parent == p); sl@0: } sl@0: } sl@0: if (!in_progress) out << "}\n"; sl@0: } sl@0: sl@0: bool valid() sl@0: { sl@0: // Check that the ranks in the A array match the ranks of the sl@0: // groups stored there. Also, the active groups must be the last sl@0: // child of their parent. sl@0: for (size_type r = 0; r < A.size(); ++r) { sl@0: if (A[r] && A[r]->rank != r) return false; sl@0: sl@0: if (A[r] && A[r]->parent->children[A[r]->parent->rank-1] != A[r]) sl@0: return false; sl@0: } sl@0: sl@0: // The root must have no value and a key of -Infinity sl@0: if (root.kind != smallest_key) return false; sl@0: sl@0: return valid(&root); sl@0: } sl@0: sl@0: bool valid(group* p) sl@0: { sl@0: for (size_type i = 0; i < p->rank; ++i) { sl@0: group* c = p->children[i]; sl@0: if (c) { sl@0: // Check link structure sl@0: if (c->parent != p) return false; sl@0: if (c->rank != i) return false; sl@0: sl@0: // A bad group must be active sl@0: if (do_compare(c, p) && A[i] != c) return false; sl@0: sl@0: // Check recursively sl@0: if (!valid(c)) return false; sl@0: } else { sl@0: // Only the root may sl@0: if (p != &root) return false; sl@0: } sl@0: } sl@0: return true; sl@0: } sl@0: sl@0: #endif // BOOST_RELAXED_HEAP_DEBUG sl@0: sl@0: private: sl@0: size_type sl@0: build_tree(group& parent, size_type idx, size_type r, size_type max_rank) sl@0: { sl@0: group& this_group = index_to_group[idx]; sl@0: this_group.parent = &parent; sl@0: ++idx; sl@0: sl@0: this_group.children = root.children + (idx * max_rank); sl@0: this_group.rank = r; sl@0: for (size_type i = 0; i < r; ++i) { sl@0: this_group.children[i] = &index_to_group[idx]; sl@0: idx = build_tree(this_group, idx, i, max_rank); sl@0: } sl@0: return idx; sl@0: } sl@0: sl@0: void find_smallest() const sl@0: { sl@0: group** roots = root.children; sl@0: sl@0: if (!smallest_value) { sl@0: std::size_t i; sl@0: for (i = 0; i < root.rank; ++i) { sl@0: if (roots[i] && sl@0: (!smallest_value || do_compare(roots[i], smallest_value))) { sl@0: smallest_value = roots[i]; sl@0: } sl@0: } sl@0: for (i = 0; i < A.size(); ++i) { sl@0: if (A[i] && (!smallest_value || do_compare(A[i], smallest_value))) sl@0: smallest_value = A[i]; sl@0: } sl@0: } sl@0: } sl@0: sl@0: bool do_compare(group* x, group* y) const sl@0: { sl@0: return (x->kind < y->kind sl@0: || (x->kind == y->kind sl@0: && x->kind == stored_key sl@0: && compare(*x->value, *y->value))); sl@0: } sl@0: sl@0: void promote(group* a) sl@0: { sl@0: assert(a != 0); sl@0: rank_type r = a->rank; sl@0: group* p = a->parent; sl@0: assert(p != 0); sl@0: if (do_compare(a, p)) { sl@0: // s is the rank + 1 sibling sl@0: group* s = p->rank > r + 1? p->children[r + 1] : 0; sl@0: sl@0: // If a is the last child of p sl@0: if (r == p->rank - 1) { sl@0: if (!A[r]) A[r] = a; sl@0: else if (A[r] != a) pair_transform(a); sl@0: } else { sl@0: assert(s != 0); sl@0: if (A[r + 1] == s) active_sibling_transform(a, s); sl@0: else good_sibling_transform(a, s); sl@0: } sl@0: } sl@0: } sl@0: sl@0: group* combine(group* a1, group* a2) sl@0: { sl@0: assert(a1->rank == a2->rank); sl@0: if (do_compare(a2, a1)) do_swap(a1, a2); sl@0: a1->children[a1->rank++] = a2; sl@0: a2->parent = a1; sl@0: clean(a1); sl@0: return a1; sl@0: } sl@0: sl@0: void clean(group* q) sl@0: { sl@0: if (2 > q->rank) return; sl@0: group* qp = q->children[q->rank-1]; sl@0: rank_type s = q->rank - 2; sl@0: group* x = q->children[s]; sl@0: group* xp = qp->children[s]; sl@0: assert(s == x->rank); sl@0: sl@0: // If x is active, swap x and xp sl@0: if (A[s] == x) { sl@0: q->children[s] = xp; sl@0: xp->parent = q; sl@0: qp->children[s] = x; sl@0: x->parent = qp; sl@0: } sl@0: } sl@0: sl@0: void pair_transform(group* a) sl@0: { sl@0: #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1 sl@0: std::cerr << "- pair transform\n"; sl@0: #endif sl@0: rank_type r = a->rank; sl@0: sl@0: // p is a's parent sl@0: group* p = a->parent; sl@0: assert(p != 0); sl@0: sl@0: // g is p's parent (a's grandparent) sl@0: group* g = p->parent; sl@0: assert(g != 0); sl@0: sl@0: // a' <- A(r) sl@0: assert(A[r] != 0); sl@0: group* ap = A[r]; sl@0: assert(ap != 0); sl@0: sl@0: // A(r) <- nil sl@0: A[r] = 0; sl@0: sl@0: // let a' have parent p' sl@0: group* pp = ap->parent; sl@0: assert(pp != 0); sl@0: sl@0: // let a' have grandparent g' sl@0: group* gp = pp->parent; sl@0: assert(gp != 0); sl@0: sl@0: // Remove a and a' from their parents sl@0: assert(ap == pp->children[pp->rank-1]); // Guaranteed because ap is active sl@0: --pp->rank; sl@0: sl@0: // Guaranteed by caller sl@0: assert(a == p->children[p->rank-1]); sl@0: --p->rank; sl@0: sl@0: // Note: a, ap, p, pp all have rank r sl@0: if (do_compare(pp, p)) { sl@0: do_swap(a, ap); sl@0: do_swap(p, pp); sl@0: do_swap(g, gp); sl@0: } sl@0: sl@0: // Assuming k(p) <= k(p') sl@0: // make p' the rank r child of p sl@0: assert(r == p->rank); sl@0: p->children[p->rank++] = pp; sl@0: pp->parent = p; sl@0: sl@0: // Combine a, ap into a rank r+1 group c sl@0: group* c = combine(a, ap); sl@0: sl@0: // make c the rank r+1 child of g' sl@0: assert(gp->rank > r+1); sl@0: gp->children[r+1] = c; sl@0: c->parent = gp; sl@0: sl@0: #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1 sl@0: std::cerr << "After pair transform...\n"; sl@0: dump_tree(); sl@0: #endif sl@0: sl@0: if (A[r+1] == pp) A[r+1] = c; sl@0: else promote(c); sl@0: } sl@0: sl@0: void active_sibling_transform(group* a, group* s) sl@0: { sl@0: #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1 sl@0: std::cerr << "- active sibling transform\n"; sl@0: #endif sl@0: group* p = a->parent; sl@0: group* g = p->parent; sl@0: sl@0: // remove a, s from their parents sl@0: assert(s->parent == p); sl@0: assert(p->children[p->rank-1] == s); sl@0: --p->rank; sl@0: assert(p->children[p->rank-1] == a); sl@0: --p->rank; sl@0: sl@0: rank_type r = a->rank; sl@0: A[r+1] = 0; sl@0: a = combine(p, a); sl@0: group* c = combine(a, s); sl@0: sl@0: // make c the rank r+2 child of g sl@0: assert(g->children[r+2] == p); sl@0: g->children[r+2] = c; sl@0: c->parent = g; sl@0: if (A[r+2] == p) A[r+2] = c; sl@0: else promote(c); sl@0: } sl@0: sl@0: void good_sibling_transform(group* a, group* s) sl@0: { sl@0: #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1 sl@0: std::cerr << "- good sibling transform\n"; sl@0: #endif sl@0: rank_type r = a->rank; sl@0: group* c = s->children[s->rank-1]; sl@0: assert(c->rank == r); sl@0: if (A[r] == c) { sl@0: #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1 sl@0: std::cerr << "- good sibling pair transform\n"; sl@0: #endif sl@0: A[r] = 0; sl@0: group* p = a->parent; sl@0: sl@0: // Remove c from its parent sl@0: --s->rank; sl@0: sl@0: // Make s the rank r child of p sl@0: s->parent = p; sl@0: p->children[r] = s; sl@0: sl@0: // combine a, c and let the result by the rank r+1 child of p sl@0: assert(p->rank > r+1); sl@0: group* x = combine(a, c); sl@0: x->parent = p; sl@0: p->children[r+1] = x; sl@0: sl@0: if (A[r+1] == s) A[r+1] = x; sl@0: else promote(x); sl@0: sl@0: #if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1 sl@0: dump_tree(std::cerr); sl@0: #endif sl@0: // pair_transform(a); sl@0: } else { sl@0: // Clean operation sl@0: group* p = a->parent; sl@0: s->children[r] = a; sl@0: a->parent = s; sl@0: p->children[r] = c; sl@0: c->parent = p; sl@0: sl@0: promote(a); sl@0: } sl@0: } sl@0: sl@0: static void do_swap(group*& x, group*& y) sl@0: { sl@0: group* tmp = x; sl@0: x = y; sl@0: y = tmp; sl@0: } sl@0: sl@0: /// Function object that compares two values in the heap sl@0: Compare compare; sl@0: sl@0: /// Mapping from values to indices in the range [0, n). sl@0: ID id; sl@0: sl@0: /** The root group of the queue. This group is special because it will sl@0: * never store a value, but it acts as a parent to all of the sl@0: * roots. Thus, its list of children is the list of roots. sl@0: */ sl@0: group root; sl@0: sl@0: /** Mapping from the group index of a value to the group associated sl@0: * with that value. If a value is not in the queue, then the "value" sl@0: * field will be empty. sl@0: */ sl@0: std::vector index_to_group; sl@0: sl@0: /** Flat data structure containing the values in each of the sl@0: * groups. It will be indexed via the id of the values. The groups sl@0: * are each log_n long, with the last group potentially being sl@0: * smaller. sl@0: */ sl@0: std::vector< ::boost::optional > groups; sl@0: sl@0: /** The list of active groups, indexed by rank. When A[r] is null, sl@0: * there is no active group of rank r. Otherwise, A[r] is the active sl@0: * group of rank r. sl@0: */ sl@0: std::vector A; sl@0: sl@0: /** The group containing the smallest value in the queue, which must sl@0: * be either a root or an active group. If this group is null, then we sl@0: * will need to search for this group when it is needed. sl@0: */ sl@0: mutable group* smallest_value; sl@0: sl@0: /// Cached value log_base_2(n) sl@0: size_type log_n; sl@0: }; sl@0: sl@0: sl@0: } // end namespace boost sl@0: sl@0: #if defined(BOOST_MSVC) sl@0: # pragma warning(pop) sl@0: #endif sl@0: sl@0: #endif // BOOST_RELAXED_HEAP_HEADER