os/ossrv/ossrv_pub/boost_apis/boost/pending/relaxed_heap.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright 2004 The Trustees of Indiana University.
sl@0
     2
sl@0
     3
// Use, modification and distribution is subject to the Boost Software
sl@0
     4
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
sl@0
     5
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     6
sl@0
     7
//  Authors: Douglas Gregor
sl@0
     8
//           Andrew Lumsdaine
sl@0
     9
#ifndef BOOST_RELAXED_HEAP_HEADER
sl@0
    10
#define BOOST_RELAXED_HEAP_HEADER
sl@0
    11
sl@0
    12
#include <functional>
sl@0
    13
#include <boost/property_map.hpp>
sl@0
    14
#include <boost/optional.hpp>
sl@0
    15
#include <vector>
sl@0
    16
sl@0
    17
#ifdef BOOST_RELAXED_HEAP_DEBUG
sl@0
    18
#  include <iostream>
sl@0
    19
#endif // BOOST_RELAXED_HEAP_DEBUG
sl@0
    20
sl@0
    21
#if defined(BOOST_MSVC)
sl@0
    22
#  pragma warning(push)
sl@0
    23
#  pragma warning(disable:4355) // complaint about using 'this' to
sl@0
    24
#endif                          // initialize a member
sl@0
    25
sl@0
    26
namespace boost {
sl@0
    27
sl@0
    28
template<typename IndexedType,
sl@0
    29
         typename Compare = std::less<IndexedType>,
sl@0
    30
         typename ID = identity_property_map>
sl@0
    31
class relaxed_heap
sl@0
    32
{
sl@0
    33
  struct group;
sl@0
    34
sl@0
    35
  typedef relaxed_heap self_type;
sl@0
    36
  typedef std::size_t  rank_type;
sl@0
    37
sl@0
    38
public:
sl@0
    39
  typedef IndexedType  value_type;
sl@0
    40
  typedef rank_type    size_type;
sl@0
    41
sl@0
    42
private:
sl@0
    43
  /**
sl@0
    44
   * The kind of key that a group has. The actual values are discussed
sl@0
    45
   * in-depth in the documentation of the @c kind field of the @c group
sl@0
    46
   * structure. Note that the order of the enumerators *IS* important
sl@0
    47
   * and must not be changed.
sl@0
    48
   */
sl@0
    49
  enum group_key_kind { smallest_key, stored_key, largest_key };
sl@0
    50
sl@0
    51
  struct group {
sl@0
    52
    explicit group(group_key_kind kind = largest_key)
sl@0
    53
      : kind(kind), parent(this), rank(0) { }
sl@0
    54
sl@0
    55
    /** The value associated with this group. This value is only valid
sl@0
    56
     *  when @c kind!=largest_key (which indicates a deleted
sl@0
    57
     *  element). Note that the use of boost::optional increases the
sl@0
    58
     *  memory requirements slightly but does not result in extraneous
sl@0
    59
     *  memory allocations or deallocations. The optional could be
sl@0
    60
     *  eliminated when @c value_type is a model of
sl@0
    61
     *  DefaultConstructible.
sl@0
    62
     */
sl@0
    63
    ::boost::optional<value_type> value;
sl@0
    64
sl@0
    65
    /**
sl@0
    66
     * The kind of key stored at this group. This may be @c
sl@0
    67
     * smallest_key, which indicates that the key is infinitely small;
sl@0
    68
     * @c largest_key, which indicates that the key is infinitely
sl@0
    69
     * large; or @c stored_key, which means that the key is unknown,
sl@0
    70
     * but its relationship to other keys can be determined via the
sl@0
    71
     * comparison function object.
sl@0
    72
     */
sl@0
    73
    group_key_kind        kind;
sl@0
    74
sl@0
    75
    /// The parent of this group. Will only be NULL for the dummy root group
sl@0
    76
    group*                parent;
sl@0
    77
sl@0
    78
    /// The rank of this group. Equivalent to the number of children in
sl@0
    79
    /// the group.
sl@0
    80
    rank_type            rank;
sl@0
    81
sl@0
    82
    /** The children of this group. For the dummy root group, these are
sl@0
    83
     * the roots. This is an array of length log n containing pointers
sl@0
    84
     * to the child groups.
sl@0
    85
     */
sl@0
    86
    group**               children;
sl@0
    87
  };
sl@0
    88
sl@0
    89
  size_type log_base_2(size_type n) // log2 is a macro on some platforms
sl@0
    90
  {
sl@0
    91
    size_type leading_zeroes = 0;
sl@0
    92
    do {
sl@0
    93
      size_type next = n << 1;
sl@0
    94
      if (n == (next >> 1)) {
sl@0
    95
        ++leading_zeroes;
sl@0
    96
        n = next;
sl@0
    97
      } else {
sl@0
    98
        break;
sl@0
    99
      }
sl@0
   100
    } while (true);
sl@0
   101
    return sizeof(size_type) * CHAR_BIT - leading_zeroes - 1;
sl@0
   102
  }
sl@0
   103
sl@0
   104
public:
sl@0
   105
  relaxed_heap(size_type n, const Compare& compare = Compare(),
sl@0
   106
               const ID& id = ID())
sl@0
   107
    : compare(compare), id(id), root(smallest_key), groups(n),
sl@0
   108
      smallest_value(0)
sl@0
   109
  {
sl@0
   110
    if (n == 0) {
sl@0
   111
      root.children = new group*[1];
sl@0
   112
      return;
sl@0
   113
    }
sl@0
   114
sl@0
   115
    log_n = log_base_2(n);
sl@0
   116
    if (log_n == 0) log_n = 1;
sl@0
   117
    size_type g = n / log_n;
sl@0
   118
    if (n % log_n > 0) ++g;
sl@0
   119
    size_type log_g = log_base_2(g);
sl@0
   120
    size_type r = log_g;
sl@0
   121
sl@0
   122
    // Reserve an appropriate amount of space for data structures, so
sl@0
   123
    // that we do not need to expand them.
sl@0
   124
    index_to_group.resize(g);
sl@0
   125
    A.resize(r + 1, 0);
sl@0
   126
    root.rank = r + 1;
sl@0
   127
    root.children = new group*[(log_g + 1) * (g + 1)];
sl@0
   128
    for (rank_type i = 0; i < r+1; ++i) root.children[i] = 0;
sl@0
   129
sl@0
   130
    // Build initial heap
sl@0
   131
    size_type idx = 0;
sl@0
   132
    while (idx < g) {
sl@0
   133
      root.children[r] = &index_to_group[idx];
sl@0
   134
      idx = build_tree(root, idx, r, log_g + 1);
sl@0
   135
      if (idx != g)
sl@0
   136
        r = static_cast<size_type>(log_base_2(g-idx));
sl@0
   137
    }
sl@0
   138
  }
sl@0
   139
sl@0
   140
  ~relaxed_heap() { delete [] root.children; }
sl@0
   141
sl@0
   142
  void push(const value_type& x)
sl@0
   143
  {
sl@0
   144
    groups[get(id, x)] = x;
sl@0
   145
    update(x);
sl@0
   146
  }
sl@0
   147
sl@0
   148
  void update(const value_type& x)
sl@0
   149
  {
sl@0
   150
    group* a = &index_to_group[get(id, x) / log_n];
sl@0
   151
    if (!a->value
sl@0
   152
        || *a->value == x
sl@0
   153
        || compare(x, *a->value)) {
sl@0
   154
      if (a != smallest_value) smallest_value = 0;
sl@0
   155
      a->kind = stored_key;
sl@0
   156
      a->value = x;
sl@0
   157
      promote(a);
sl@0
   158
    }
sl@0
   159
  }
sl@0
   160
sl@0
   161
  void remove(const value_type& x)
sl@0
   162
  {
sl@0
   163
    group* a = &index_to_group[get(id, x) / log_n];
sl@0
   164
    assert(groups[get(id, x)] != 0);
sl@0
   165
    a->value = x;
sl@0
   166
    a->kind = smallest_key;
sl@0
   167
    promote(a);
sl@0
   168
    smallest_value = a;
sl@0
   169
    pop();
sl@0
   170
  }
sl@0
   171
sl@0
   172
  value_type& top()
sl@0
   173
  {
sl@0
   174
    find_smallest();
sl@0
   175
    assert(smallest_value->value != 0);
sl@0
   176
    return *smallest_value->value;
sl@0
   177
  }
sl@0
   178
sl@0
   179
  const value_type& top() const
sl@0
   180
  {
sl@0
   181
    find_smallest();
sl@0
   182
    assert(smallest_value->value != 0);
sl@0
   183
    return *smallest_value->value;
sl@0
   184
  }
sl@0
   185
sl@0
   186
  bool empty() const
sl@0
   187
  {
sl@0
   188
    find_smallest();
sl@0
   189
    return !smallest_value || (smallest_value->kind == largest_key);
sl@0
   190
  }
sl@0
   191
sl@0
   192
  bool contains(const value_type& x) const { return groups[get(id, x)]; }
sl@0
   193
sl@0
   194
  void pop()
sl@0
   195
  {
sl@0
   196
    // Fill in smallest_value. This is the group x.
sl@0
   197
    find_smallest();
sl@0
   198
    group* x = smallest_value;
sl@0
   199
    smallest_value = 0;
sl@0
   200
sl@0
   201
    // Make x a leaf, giving it the smallest value within its group
sl@0
   202
    rank_type r = x->rank;
sl@0
   203
    group* p = x->parent;
sl@0
   204
    {
sl@0
   205
      assert(x->value != 0);
sl@0
   206
sl@0
   207
      // Find x's group
sl@0
   208
      size_type start = get(id, *x->value) - get(id, *x->value) % log_n;
sl@0
   209
      size_type end = start + log_n;
sl@0
   210
      if (end > groups.size()) end = groups.size();
sl@0
   211
sl@0
   212
      // Remove the smallest value from the group, and find the new
sl@0
   213
      // smallest value.
sl@0
   214
      groups[get(id, *x->value)].reset();
sl@0
   215
      x->value.reset();
sl@0
   216
      x->kind = largest_key;
sl@0
   217
      for (size_type i = start; i < end; ++i) {
sl@0
   218
        if (groups[i] && (!x->value || compare(*groups[i], *x->value))) {
sl@0
   219
          x->kind = stored_key;
sl@0
   220
          x->value = groups[i];
sl@0
   221
        }
sl@0
   222
      }
sl@0
   223
    }
sl@0
   224
    x->rank = 0;
sl@0
   225
sl@0
   226
    // Combine prior children of x with x
sl@0
   227
    group* y = x;
sl@0
   228
    for (size_type c = 0; c < r; ++c) {
sl@0
   229
      group* child = x->children[c];
sl@0
   230
      if (A[c] == child) A[c] = 0;
sl@0
   231
      y = combine(y, child);
sl@0
   232
    }
sl@0
   233
sl@0
   234
    // If we got back something other than x, let y take x's place
sl@0
   235
    if (y != x) {
sl@0
   236
      y->parent = p;
sl@0
   237
      p->children[r] = y;
sl@0
   238
sl@0
   239
      assert(r == y->rank);
sl@0
   240
      if (A[y->rank] == x)
sl@0
   241
        A[y->rank] = do_compare(y, p)? y : 0;
sl@0
   242
    }
sl@0
   243
  }
sl@0
   244
sl@0
   245
#ifdef BOOST_RELAXED_HEAP_DEBUG
sl@0
   246
  /*************************************************************************
sl@0
   247
   * Debugging support                                                     *
sl@0
   248
   *************************************************************************/
sl@0
   249
  void dump_tree() { dump_tree(std::cout); }
sl@0
   250
  void dump_tree(std::ostream& out) { dump_tree(out, &root); }
sl@0
   251
sl@0
   252
  void dump_tree(std::ostream& out, group* p, bool in_progress = false)
sl@0
   253
  {
sl@0
   254
    if (!in_progress) {
sl@0
   255
      out << "digraph heap {\n"
sl@0
   256
          << "  edge[dir=\"back\"];\n";
sl@0
   257
    }
sl@0
   258
sl@0
   259
    size_type p_index = 0;
sl@0
   260
    if (p != &root) while (&index_to_group[p_index] != p) ++p_index;
sl@0
   261
sl@0
   262
    for (size_type i = 0; i < p->rank; ++i) {
sl@0
   263
      group* c = p->children[i];
sl@0
   264
      if (c) {
sl@0
   265
        size_type c_index = 0;
sl@0
   266
        if (c != &root) while (&index_to_group[c_index] != c) ++c_index;
sl@0
   267
sl@0
   268
        out << "  ";
sl@0
   269
        if (p == &root) out << 'p'; else out << p_index;
sl@0
   270
        out << " -> ";
sl@0
   271
        if (c == &root) out << 'p'; else out << c_index;
sl@0
   272
        if (A[c->rank] == c) out << " [style=\"dotted\"]";
sl@0
   273
        out << ";\n";
sl@0
   274
        dump_tree(out, c, true);
sl@0
   275
sl@0
   276
        // Emit node information
sl@0
   277
        out << "  ";
sl@0
   278
        if (c == &root) out << 'p'; else out << c_index;
sl@0
   279
        out << " [label=\"";
sl@0
   280
        if (c == &root) out << 'p'; else out << c_index;
sl@0
   281
        out << ":";
sl@0
   282
        size_type start = c_index * log_n;
sl@0
   283
        size_type end = start + log_n;
sl@0
   284
        if (end > groups.size()) end = groups.size();
sl@0
   285
        while (start != end) {
sl@0
   286
          if (groups[start]) {
sl@0
   287
            out << " " << get(id, *groups[start]);
sl@0
   288
            if (*groups[start] == *c->value) out << "(*)";
sl@0
   289
          }
sl@0
   290
          ++start;
sl@0
   291
        }
sl@0
   292
        out << '"';
sl@0
   293
sl@0
   294
        if (do_compare(c, p)) {
sl@0
   295
          out << "  ";
sl@0
   296
          if (c == &root) out << 'p'; else out << c_index;
sl@0
   297
          out << ", style=\"filled\", fillcolor=\"gray\"";
sl@0
   298
        }
sl@0
   299
        out << "];\n";
sl@0
   300
      } else {
sl@0
   301
        assert(p->parent == p);
sl@0
   302
      }
sl@0
   303
    }
sl@0
   304
    if (!in_progress) out << "}\n";
sl@0
   305
  }
sl@0
   306
sl@0
   307
  bool valid()
sl@0
   308
  {
sl@0
   309
    // Check that the ranks in the A array match the ranks of the
sl@0
   310
    // groups stored there. Also, the active groups must be the last
sl@0
   311
    // child of their parent.
sl@0
   312
    for (size_type r = 0; r < A.size(); ++r) {
sl@0
   313
      if (A[r] && A[r]->rank != r) return false;
sl@0
   314
sl@0
   315
      if (A[r] && A[r]->parent->children[A[r]->parent->rank-1] != A[r])
sl@0
   316
        return false;
sl@0
   317
    }
sl@0
   318
sl@0
   319
    // The root must have no value and a key of -Infinity
sl@0
   320
    if (root.kind != smallest_key) return false;
sl@0
   321
sl@0
   322
    return valid(&root);
sl@0
   323
  }
sl@0
   324
sl@0
   325
  bool valid(group* p)
sl@0
   326
  {
sl@0
   327
    for (size_type i = 0; i < p->rank; ++i) {
sl@0
   328
      group* c = p->children[i];
sl@0
   329
      if (c) {
sl@0
   330
        // Check link structure
sl@0
   331
        if (c->parent != p) return false;
sl@0
   332
        if (c->rank != i) return false;
sl@0
   333
sl@0
   334
        // A bad group must be active
sl@0
   335
        if (do_compare(c, p) && A[i] != c) return false;
sl@0
   336
sl@0
   337
        // Check recursively
sl@0
   338
        if (!valid(c)) return false;
sl@0
   339
      } else {
sl@0
   340
        // Only the root may
sl@0
   341
        if (p != &root) return false;
sl@0
   342
      }
sl@0
   343
    }
sl@0
   344
    return true;
sl@0
   345
  }
sl@0
   346
sl@0
   347
#endif // BOOST_RELAXED_HEAP_DEBUG
sl@0
   348
sl@0
   349
private:
sl@0
   350
  size_type
sl@0
   351
  build_tree(group& parent, size_type idx, size_type r, size_type max_rank)
sl@0
   352
  {
sl@0
   353
    group& this_group = index_to_group[idx];
sl@0
   354
    this_group.parent = &parent;
sl@0
   355
    ++idx;
sl@0
   356
sl@0
   357
    this_group.children = root.children + (idx * max_rank);
sl@0
   358
    this_group.rank = r;
sl@0
   359
    for (size_type i = 0; i < r; ++i) {
sl@0
   360
      this_group.children[i] = &index_to_group[idx];
sl@0
   361
      idx = build_tree(this_group, idx, i, max_rank);
sl@0
   362
    }
sl@0
   363
    return idx;
sl@0
   364
  }
sl@0
   365
sl@0
   366
  void find_smallest() const
sl@0
   367
  {
sl@0
   368
    group** roots = root.children;
sl@0
   369
sl@0
   370
    if (!smallest_value) {
sl@0
   371
      std::size_t i;
sl@0
   372
      for (i = 0; i < root.rank; ++i) {
sl@0
   373
        if (roots[i] &&
sl@0
   374
            (!smallest_value || do_compare(roots[i], smallest_value))) {
sl@0
   375
          smallest_value = roots[i];
sl@0
   376
        }
sl@0
   377
      }
sl@0
   378
      for (i = 0; i < A.size(); ++i) {
sl@0
   379
        if (A[i] && (!smallest_value || do_compare(A[i], smallest_value)))
sl@0
   380
          smallest_value = A[i];
sl@0
   381
      }
sl@0
   382
    }
sl@0
   383
  }
sl@0
   384
sl@0
   385
  bool do_compare(group* x, group* y) const
sl@0
   386
  {
sl@0
   387
    return (x->kind < y->kind
sl@0
   388
            || (x->kind == y->kind
sl@0
   389
                && x->kind == stored_key
sl@0
   390
                && compare(*x->value, *y->value)));
sl@0
   391
  }
sl@0
   392
sl@0
   393
  void promote(group* a)
sl@0
   394
  {
sl@0
   395
    assert(a != 0);
sl@0
   396
    rank_type r = a->rank;
sl@0
   397
    group* p = a->parent;
sl@0
   398
    assert(p != 0);
sl@0
   399
    if (do_compare(a, p)) {
sl@0
   400
      // s is the rank + 1 sibling
sl@0
   401
      group* s = p->rank > r + 1? p->children[r + 1] : 0;
sl@0
   402
sl@0
   403
      // If a is the last child of p
sl@0
   404
      if (r == p->rank - 1) {
sl@0
   405
        if (!A[r]) A[r] = a;
sl@0
   406
        else if (A[r] != a) pair_transform(a);
sl@0
   407
      } else {
sl@0
   408
        assert(s != 0);
sl@0
   409
        if (A[r + 1] == s) active_sibling_transform(a, s);
sl@0
   410
        else good_sibling_transform(a, s);
sl@0
   411
      }
sl@0
   412
    }
sl@0
   413
  }
sl@0
   414
sl@0
   415
  group* combine(group* a1, group* a2)
sl@0
   416
  {
sl@0
   417
    assert(a1->rank == a2->rank);
sl@0
   418
    if (do_compare(a2, a1)) do_swap(a1, a2);
sl@0
   419
    a1->children[a1->rank++] = a2;
sl@0
   420
    a2->parent = a1;
sl@0
   421
    clean(a1);
sl@0
   422
    return a1;
sl@0
   423
  }
sl@0
   424
sl@0
   425
  void clean(group* q)
sl@0
   426
  {
sl@0
   427
    if (2 > q->rank) return;
sl@0
   428
    group* qp = q->children[q->rank-1];
sl@0
   429
    rank_type s = q->rank - 2;
sl@0
   430
    group* x = q->children[s];
sl@0
   431
    group* xp = qp->children[s];
sl@0
   432
    assert(s == x->rank);
sl@0
   433
sl@0
   434
    // If x is active, swap x and xp
sl@0
   435
    if (A[s] == x) {
sl@0
   436
      q->children[s] = xp;
sl@0
   437
      xp->parent = q;
sl@0
   438
      qp->children[s] = x;
sl@0
   439
      x->parent = qp;
sl@0
   440
    }
sl@0
   441
  }
sl@0
   442
sl@0
   443
  void pair_transform(group* a)
sl@0
   444
  {
sl@0
   445
#if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
sl@0
   446
    std::cerr << "- pair transform\n";
sl@0
   447
#endif
sl@0
   448
    rank_type r = a->rank;
sl@0
   449
sl@0
   450
    // p is a's parent
sl@0
   451
    group* p = a->parent;
sl@0
   452
    assert(p != 0);
sl@0
   453
sl@0
   454
    // g is p's parent (a's grandparent)
sl@0
   455
    group* g = p->parent;
sl@0
   456
    assert(g != 0);
sl@0
   457
sl@0
   458
    // a' <- A(r)
sl@0
   459
    assert(A[r] != 0);
sl@0
   460
    group* ap = A[r];
sl@0
   461
    assert(ap != 0);
sl@0
   462
sl@0
   463
    // A(r) <- nil
sl@0
   464
    A[r] = 0;
sl@0
   465
sl@0
   466
    // let a' have parent p'
sl@0
   467
    group* pp = ap->parent;
sl@0
   468
    assert(pp != 0);
sl@0
   469
sl@0
   470
    // let a' have grandparent g'
sl@0
   471
    group* gp = pp->parent;
sl@0
   472
    assert(gp != 0);
sl@0
   473
sl@0
   474
    // Remove a and a' from their parents
sl@0
   475
    assert(ap == pp->children[pp->rank-1]); // Guaranteed because ap is active
sl@0
   476
    --pp->rank;
sl@0
   477
sl@0
   478
    // Guaranteed by caller
sl@0
   479
    assert(a == p->children[p->rank-1]);
sl@0
   480
    --p->rank;
sl@0
   481
sl@0
   482
    // Note: a, ap, p, pp all have rank r
sl@0
   483
    if (do_compare(pp, p)) {
sl@0
   484
      do_swap(a, ap);
sl@0
   485
      do_swap(p, pp);
sl@0
   486
      do_swap(g, gp);
sl@0
   487
    }
sl@0
   488
sl@0
   489
    // Assuming k(p) <= k(p')
sl@0
   490
    // make p' the rank r child of p
sl@0
   491
    assert(r == p->rank);
sl@0
   492
    p->children[p->rank++] = pp;
sl@0
   493
    pp->parent = p;
sl@0
   494
sl@0
   495
    // Combine a, ap into a rank r+1 group c
sl@0
   496
    group* c = combine(a, ap);
sl@0
   497
sl@0
   498
    // make c the rank r+1 child of g'
sl@0
   499
    assert(gp->rank > r+1);
sl@0
   500
    gp->children[r+1] = c;
sl@0
   501
    c->parent = gp;
sl@0
   502
sl@0
   503
#if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
sl@0
   504
    std::cerr << "After pair transform...\n";
sl@0
   505
    dump_tree();
sl@0
   506
#endif
sl@0
   507
sl@0
   508
    if (A[r+1] == pp) A[r+1] = c;
sl@0
   509
    else promote(c);
sl@0
   510
  }
sl@0
   511
sl@0
   512
  void active_sibling_transform(group* a, group* s)
sl@0
   513
  {
sl@0
   514
#if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
sl@0
   515
    std::cerr << "- active sibling transform\n";
sl@0
   516
#endif
sl@0
   517
    group* p = a->parent;
sl@0
   518
    group* g = p->parent;
sl@0
   519
sl@0
   520
    // remove a, s from their parents
sl@0
   521
    assert(s->parent == p);
sl@0
   522
    assert(p->children[p->rank-1] == s);
sl@0
   523
    --p->rank;
sl@0
   524
    assert(p->children[p->rank-1] == a);
sl@0
   525
    --p->rank;
sl@0
   526
sl@0
   527
    rank_type r = a->rank;
sl@0
   528
    A[r+1] = 0;
sl@0
   529
    a = combine(p, a);
sl@0
   530
    group* c = combine(a, s);
sl@0
   531
sl@0
   532
    // make c the rank r+2 child of g
sl@0
   533
    assert(g->children[r+2] == p);
sl@0
   534
    g->children[r+2] = c;
sl@0
   535
    c->parent = g;
sl@0
   536
    if (A[r+2] == p) A[r+2] = c;
sl@0
   537
    else promote(c);
sl@0
   538
  }
sl@0
   539
sl@0
   540
  void good_sibling_transform(group* a, group* s)
sl@0
   541
  {
sl@0
   542
#if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
sl@0
   543
    std::cerr << "- good sibling transform\n";
sl@0
   544
#endif
sl@0
   545
    rank_type r = a->rank;
sl@0
   546
    group* c = s->children[s->rank-1];
sl@0
   547
    assert(c->rank == r);
sl@0
   548
    if (A[r] == c) {
sl@0
   549
#if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
sl@0
   550
      std::cerr << "- good sibling pair transform\n";
sl@0
   551
#endif
sl@0
   552
      A[r] = 0;
sl@0
   553
      group* p = a->parent;
sl@0
   554
sl@0
   555
      // Remove c from its parent
sl@0
   556
      --s->rank;
sl@0
   557
sl@0
   558
      // Make s the rank r child of p
sl@0
   559
      s->parent = p;
sl@0
   560
      p->children[r] = s;
sl@0
   561
sl@0
   562
      // combine a, c and let the result by the rank r+1 child of p
sl@0
   563
      assert(p->rank > r+1);
sl@0
   564
      group* x = combine(a, c);
sl@0
   565
      x->parent = p;
sl@0
   566
      p->children[r+1] = x;
sl@0
   567
sl@0
   568
      if (A[r+1] == s) A[r+1] = x;
sl@0
   569
      else promote(x);
sl@0
   570
sl@0
   571
#if defined(BOOST_RELAXED_HEAP_DEBUG) && BOOST_RELAXED_HEAP_DEBUG > 1
sl@0
   572
      dump_tree(std::cerr);
sl@0
   573
#endif
sl@0
   574
      //      pair_transform(a);
sl@0
   575
    } else {
sl@0
   576
      // Clean operation
sl@0
   577
      group* p = a->parent;
sl@0
   578
      s->children[r] = a;
sl@0
   579
      a->parent = s;
sl@0
   580
      p->children[r] = c;
sl@0
   581
      c->parent = p;
sl@0
   582
sl@0
   583
      promote(a);
sl@0
   584
    }
sl@0
   585
  }
sl@0
   586
sl@0
   587
  static void do_swap(group*& x, group*& y)
sl@0
   588
  {
sl@0
   589
    group* tmp = x;
sl@0
   590
    x = y;
sl@0
   591
    y = tmp;
sl@0
   592
  }
sl@0
   593
sl@0
   594
  /// Function object that compares two values in the heap
sl@0
   595
  Compare compare;
sl@0
   596
sl@0
   597
  /// Mapping from values to indices in the range [0, n).
sl@0
   598
  ID id;
sl@0
   599
sl@0
   600
  /** The root group of the queue. This group is special because it will
sl@0
   601
   *  never store a value, but it acts as a parent to all of the
sl@0
   602
   *  roots. Thus, its list of children is the list of roots.
sl@0
   603
   */
sl@0
   604
  group root;
sl@0
   605
sl@0
   606
  /** Mapping from the group index of a value to the group associated
sl@0
   607
   *  with that value. If a value is not in the queue, then the "value"
sl@0
   608
   *  field will be empty.
sl@0
   609
   */
sl@0
   610
  std::vector<group> index_to_group;
sl@0
   611
sl@0
   612
  /** Flat data structure containing the values in each of the
sl@0
   613
   *  groups. It will be indexed via the id of the values. The groups
sl@0
   614
   *  are each log_n long, with the last group potentially being
sl@0
   615
   *  smaller.
sl@0
   616
   */
sl@0
   617
  std::vector< ::boost::optional<value_type> > groups;
sl@0
   618
sl@0
   619
  /** The list of active groups, indexed by rank. When A[r] is null,
sl@0
   620
   *  there is no active group of rank r. Otherwise, A[r] is the active
sl@0
   621
   *  group of rank r.
sl@0
   622
   */
sl@0
   623
  std::vector<group*> A;
sl@0
   624
sl@0
   625
  /** The group containing the smallest value in the queue, which must
sl@0
   626
   *  be either a root or an active group. If this group is null, then we
sl@0
   627
   *  will need to search for this group when it is needed.
sl@0
   628
   */
sl@0
   629
  mutable group* smallest_value;
sl@0
   630
sl@0
   631
  /// Cached value log_base_2(n)
sl@0
   632
  size_type log_n;
sl@0
   633
};
sl@0
   634
sl@0
   635
sl@0
   636
} // end namespace boost
sl@0
   637
sl@0
   638
#if defined(BOOST_MSVC)
sl@0
   639
#  pragma warning(pop)
sl@0
   640
#endif
sl@0
   641
sl@0
   642
#endif // BOOST_RELAXED_HEAP_HEADER