summaryrefslogtreecommitdiffstats
path: root/memory/build/rb.h
blob: 418d2069112d469576fe93e9948e2995c4820faf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Portions of this file were originally under the following license:
//
// Copyright (C) 2008 Jason Evans <jasone@FreeBSD.org>.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice(s), this list of conditions and the following disclaimer
//    unmodified other than the allowable addition of one or more
//    copyright notices.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice(s), this list of conditions and the following disclaimer in
//    the documentation and/or other materials provided with the
//    distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ****************************************************************************
//
// C++ template implementation of left-leaning red-black trees.
//
// All operations are done non-recursively.  Parent pointers are not used, and
// color bits are stored in the least significant bit of right-child pointers,
// thus making node linkage as compact as is possible for red-black trees.
//
// The RedBlackTree template expects two type arguments: the type of the nodes,
// containing a RedBlackTreeNode, and a trait providing two methods:
//  - a GetTreeNode method that returns a reference to the RedBlackTreeNode
//    corresponding to a given node with the following signature:
//      static RedBlackTreeNode<T>& GetTreeNode(T*)
//  - a Compare function with the following signature:
//      static Order Compare(T* aNode, T* aOther)
//                              ^^^^^
//                           or aKey
//
// Interpretation of comparision function return values:
//
//   Order::eLess: aNode <  aOther
//   Order::eEqual: aNode == aOther
//   Order::eGreater: aNode >  aOther
//
// In all cases, the aNode or aKey argument is the first argument to the
// comparison function, which makes it possible to write comparison functions
// that treat the first argument specially.
//
// ***************************************************************************

#ifndef RB_H_
#define RB_H_

#include "mozilla/Alignment.h"
#include "mozilla/Assertions.h"
#include "Utils.h"

enum NodeColor {
  Black = 0,
  Red = 1,
};

// Node structure.
template <typename T>
class RedBlackTreeNode {
  T* mLeft;
  // The lowest bit is the color
  T* mRightAndColor;

 public:
  T* Left() { return mLeft; }

  void SetLeft(T* aValue) { mLeft = aValue; }

  T* Right() {
    return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(mRightAndColor) &
                                uintptr_t(~1));
  }

  void SetRight(T* aValue) {
    mRightAndColor = reinterpret_cast<T*>(
        (reinterpret_cast<uintptr_t>(aValue) & uintptr_t(~1)) | Color());
  }

  NodeColor Color() {
    return static_cast<NodeColor>(reinterpret_cast<uintptr_t>(mRightAndColor) &
                                  1);
  }

  bool IsBlack() { return Color() == NodeColor::Black; }

  bool IsRed() { return Color() == NodeColor::Red; }

  void SetColor(NodeColor aColor) {
    mRightAndColor = reinterpret_cast<T*>(
        (reinterpret_cast<uintptr_t>(mRightAndColor) & uintptr_t(~1)) | aColor);
  }
};

// Tree structure.
template <typename T, typename Trait>
class RedBlackTree {
 public:
  void Init() { mRoot = nullptr; }

  T* First(T* aStart = nullptr) { return First(TreeNode(aStart)).Get(); }

  T* Last(T* aStart = nullptr) { return Last(TreeNode(aStart)).Get(); }

  T* Next(T* aNode) { return Next(TreeNode(aNode)).Get(); }

  T* Prev(T* aNode) { return Prev(TreeNode(aNode)).Get(); }

  T* Search(T* aKey) { return Search(TreeNode(aKey)).Get(); }

  // Find a match if it exists. Otherwise, find the next greater node, if one
  // exists.
  T* SearchOrNext(T* aKey) { return SearchOrNext(TreeNode(aKey)).Get(); }

  void Insert(T* aNode) { Insert(TreeNode(aNode)); }

  void Remove(T* aNode) { Remove(TreeNode(aNode)); }

  // Helper class to avoid having all the tree traversal code further below
  // have to use Trait::GetTreeNode and do manual null pointer checks, adding
  // visual noise. Practically speaking TreeNode(nullptr) acts as a virtual
  // sentinel, that loops back to itself for Left() and Right() and is always
  // black.
  class TreeNode {
   public:
    constexpr TreeNode() : mNode(nullptr) {}

    MOZ_IMPLICIT TreeNode(T* aNode) : mNode(aNode) {}

    TreeNode& operator=(TreeNode aOther) {
      mNode = aOther.mNode;
      return *this;
    }

    TreeNode Left() {
      return TreeNode(mNode ? Trait::GetTreeNode(mNode).Left() : nullptr);
    }

    void SetLeft(TreeNode aNode) {
      MOZ_RELEASE_ASSERT(mNode);
      Trait::GetTreeNode(mNode).SetLeft(aNode.mNode);
    }

    TreeNode Right() {
      return TreeNode(mNode ? Trait::GetTreeNode(mNode).Right() : nullptr);
    }

    void SetRight(TreeNode aNode) {
      MOZ_RELEASE_ASSERT(mNode);
      Trait::GetTreeNode(mNode).SetRight(aNode.mNode);
    }

    NodeColor Color() {
      return mNode ? Trait::GetTreeNode(mNode).Color() : NodeColor::Black;
    }

    bool IsRed() { return Color() == NodeColor::Red; }

    bool IsBlack() { return Color() == NodeColor::Black; }

    void SetColor(NodeColor aColor) {
      MOZ_RELEASE_ASSERT(mNode);
      Trait::GetTreeNode(mNode).SetColor(aColor);
    }

    T* Get() { return mNode; }

    MOZ_IMPLICIT operator bool() { return !!mNode; }

    bool operator==(TreeNode& aOther) { return mNode == aOther.mNode; }

   private:
    T* mNode;
  };

 private:
  // Ideally we'd use a TreeNode for mRoot, but we need RedBlackTree to stay
  // a POD type to avoid a static initializer for gArenas.
  T* mRoot;

  TreeNode First(TreeNode aStart) {
    TreeNode ret;
    for (ret = aStart ? aStart : mRoot; ret.Left(); ret = ret.Left()) {
    }
    return ret;
  }

  TreeNode Last(TreeNode aStart) {
    TreeNode ret;
    for (ret = aStart ? aStart : mRoot; ret.Right(); ret = ret.Right()) {
    }
    return ret;
  }

  TreeNode Next(TreeNode aNode) {
    TreeNode ret;
    if (aNode.Right()) {
      ret = First(aNode.Right());
    } else {
      TreeNode rbp_n_t = mRoot;
      MOZ_ASSERT(rbp_n_t);
      ret = nullptr;
      while (true) {
        Order rbp_n_cmp = Trait::Compare(aNode.Get(), rbp_n_t.Get());
        if (rbp_n_cmp == Order::eLess) {
          ret = rbp_n_t;
          rbp_n_t = rbp_n_t.Left();
        } else if (rbp_n_cmp == Order::eGreater) {
          rbp_n_t = rbp_n_t.Right();
        } else {
          break;
        }
        MOZ_ASSERT(rbp_n_t);
      }
    }
    return ret;
  }

  TreeNode Prev(TreeNode aNode) {
    TreeNode ret;
    if (aNode.Left()) {
      ret = Last(aNode.Left());
    } else {
      TreeNode rbp_p_t = mRoot;
      MOZ_ASSERT(rbp_p_t);
      ret = nullptr;
      while (true) {
        Order rbp_p_cmp = Trait::Compare(aNode.Get(), rbp_p_t.Get());
        if (rbp_p_cmp == Order::eLess) {
          rbp_p_t = rbp_p_t.Left();
        } else if (rbp_p_cmp == Order::eGreater) {
          ret = rbp_p_t;
          rbp_p_t = rbp_p_t.Right();
        } else {
          break;
        }
        MOZ_ASSERT(rbp_p_t);
      }
    }
    return ret;
  }

  TreeNode Search(TreeNode aKey) {
    TreeNode ret = mRoot;
    Order rbp_se_cmp;
    while (ret && (rbp_se_cmp = Trait::Compare(aKey.Get(), ret.Get())) !=
                      Order::eEqual) {
      if (rbp_se_cmp == Order::eLess) {
        ret = ret.Left();
      } else {
        ret = ret.Right();
      }
    }
    return ret;
  }

  TreeNode SearchOrNext(TreeNode aKey) {
    TreeNode ret = nullptr;
    TreeNode rbp_ns_t = mRoot;
    while (rbp_ns_t) {
      Order rbp_ns_cmp = Trait::Compare(aKey.Get(), rbp_ns_t.Get());
      if (rbp_ns_cmp == Order::eLess) {
        ret = rbp_ns_t;
        rbp_ns_t = rbp_ns_t.Left();
      } else if (rbp_ns_cmp == Order::eGreater) {
        rbp_ns_t = rbp_ns_t.Right();
      } else {
        ret = rbp_ns_t;
        break;
      }
    }
    return ret;
  }

  void Insert(TreeNode aNode) {
    // rbp_i_s is only used as a placeholder for its RedBlackTreeNode. Use
    // AlignedStorage2 to avoid running the TreeNode base class constructor.
    mozilla::AlignedStorage2<T> rbp_i_s;
    TreeNode rbp_i_g, rbp_i_p, rbp_i_c, rbp_i_t, rbp_i_u;
    Order rbp_i_cmp = Order::eEqual;
    rbp_i_g = nullptr;
    rbp_i_p = rbp_i_s.addr();
    rbp_i_p.SetLeft(mRoot);
    rbp_i_p.SetRight(nullptr);
    rbp_i_p.SetColor(NodeColor::Black);
    rbp_i_c = mRoot;
    // Iteratively search down the tree for the insertion point,
    // splitting 4-nodes as they are encountered. At the end of each
    // iteration, rbp_i_g->rbp_i_p->rbp_i_c is a 3-level path down
    // the tree, assuming a sufficiently deep tree.
    while (rbp_i_c) {
      rbp_i_t = rbp_i_c.Left();
      rbp_i_u = rbp_i_t.Left();
      if (rbp_i_t.IsRed() && rbp_i_u.IsRed()) {
        // rbp_i_c is the top of a logical 4-node, so split it.
        // This iteration does not move down the tree, due to the
        // disruptiveness of node splitting.
        //
        // Rotate right.
        rbp_i_t = RotateRight(rbp_i_c);
        // Pass red links up one level.
        rbp_i_u = rbp_i_t.Left();
        rbp_i_u.SetColor(NodeColor::Black);
        if (rbp_i_p.Left() == rbp_i_c) {
          rbp_i_p.SetLeft(rbp_i_t);
          rbp_i_c = rbp_i_t;
        } else {
          // rbp_i_c was the right child of rbp_i_p, so rotate
          // left in order to maintain the left-leaning invariant.
          MOZ_ASSERT(rbp_i_p.Right() == rbp_i_c);
          rbp_i_p.SetRight(rbp_i_t);
          rbp_i_u = LeanLeft(rbp_i_p);
          if (rbp_i_g.Left() == rbp_i_p) {
            rbp_i_g.SetLeft(rbp_i_u);
          } else {
            MOZ_ASSERT(rbp_i_g.Right() == rbp_i_p);
            rbp_i_g.SetRight(rbp_i_u);
          }
          rbp_i_p = rbp_i_u;
          rbp_i_cmp = Trait::Compare(aNode.Get(), rbp_i_p.Get());
          if (rbp_i_cmp == Order::eLess) {
            rbp_i_c = rbp_i_p.Left();
          } else {
            MOZ_ASSERT(rbp_i_cmp == Order::eGreater);
            rbp_i_c = rbp_i_p.Right();
          }
          continue;
        }
      }
      rbp_i_g = rbp_i_p;
      rbp_i_p = rbp_i_c;
      rbp_i_cmp = Trait::Compare(aNode.Get(), rbp_i_c.Get());
      if (rbp_i_cmp == Order::eLess) {
        rbp_i_c = rbp_i_c.Left();
      } else {
        MOZ_ASSERT(rbp_i_cmp == Order::eGreater);
        rbp_i_c = rbp_i_c.Right();
      }
    }
    // rbp_i_p now refers to the node under which to insert.
    aNode.SetLeft(nullptr);
    aNode.SetRight(nullptr);
    aNode.SetColor(NodeColor::Red);
    if (rbp_i_cmp == Order::eGreater) {
      rbp_i_p.SetRight(aNode);
      rbp_i_t = LeanLeft(rbp_i_p);
      if (rbp_i_g.Left() == rbp_i_p) {
        rbp_i_g.SetLeft(rbp_i_t);
      } else if (rbp_i_g.Right() == rbp_i_p) {
        rbp_i_g.SetRight(rbp_i_t);
      }
    } else {
      rbp_i_p.SetLeft(aNode);
    }
    // Update the root and make sure that it is black.
    TreeNode root = TreeNode(rbp_i_s.addr()).Left();
    root.SetColor(NodeColor::Black);
    mRoot = root.Get();
  }

  void Remove(TreeNode aNode) {
    // rbp_r_s is only used as a placeholder for its RedBlackTreeNode. Use
    // AlignedStorage2 to avoid running the TreeNode base class constructor.
    mozilla::AlignedStorage2<T> rbp_r_s;
    TreeNode rbp_r_p, rbp_r_c, rbp_r_xp, rbp_r_t, rbp_r_u;
    Order rbp_r_cmp;
    rbp_r_p = TreeNode(rbp_r_s.addr());
    rbp_r_p.SetLeft(mRoot);
    rbp_r_p.SetRight(nullptr);
    rbp_r_p.SetColor(NodeColor::Black);
    rbp_r_c = mRoot;
    rbp_r_xp = nullptr;
    // Iterate down the tree, but always transform 2-nodes to 3- or
    // 4-nodes in order to maintain the invariant that the current
    // node is not a 2-node. This allows simple deletion once a leaf
    // is reached. Handle the root specially though, since there may
    // be no way to convert it from a 2-node to a 3-node.
    rbp_r_cmp = Trait::Compare(aNode.Get(), rbp_r_c.Get());
    if (rbp_r_cmp == Order::eLess) {
      rbp_r_t = rbp_r_c.Left();
      rbp_r_u = rbp_r_t.Left();
      if (rbp_r_t.IsBlack() && rbp_r_u.IsBlack()) {
        // Apply standard transform to prepare for left move.
        rbp_r_t = MoveRedLeft(rbp_r_c);
        rbp_r_t.SetColor(NodeColor::Black);
        rbp_r_p.SetLeft(rbp_r_t);
        rbp_r_c = rbp_r_t;
      } else {
        // Move left.
        rbp_r_p = rbp_r_c;
        rbp_r_c = rbp_r_c.Left();
      }
    } else {
      if (rbp_r_cmp == Order::eEqual) {
        MOZ_ASSERT(aNode == rbp_r_c);
        if (!rbp_r_c.Right()) {
          // Delete root node (which is also a leaf node).
          if (rbp_r_c.Left()) {
            rbp_r_t = LeanRight(rbp_r_c);
            rbp_r_t.SetRight(nullptr);
          } else {
            rbp_r_t = nullptr;
          }
          rbp_r_p.SetLeft(rbp_r_t);
        } else {
          // This is the node we want to delete, but we will
          // instead swap it with its successor and delete the
          // successor. Record enough information to do the
          // swap later. rbp_r_xp is the aNode's parent.
          rbp_r_xp = rbp_r_p;
          rbp_r_cmp = Order::eGreater;  // Note that deletion is incomplete.
        }
      }
      if (rbp_r_cmp == Order::eGreater) {
        if (rbp_r_c.Right().Left().IsBlack()) {
          rbp_r_t = rbp_r_c.Left();
          if (rbp_r_t.IsRed()) {
            // Standard transform.
            rbp_r_t = MoveRedRight(rbp_r_c);
          } else {
            // Root-specific transform.
            rbp_r_c.SetColor(NodeColor::Red);
            rbp_r_u = rbp_r_t.Left();
            if (rbp_r_u.IsRed()) {
              rbp_r_u.SetColor(NodeColor::Black);
              rbp_r_t = RotateRight(rbp_r_c);
              rbp_r_u = RotateLeft(rbp_r_c);
              rbp_r_t.SetRight(rbp_r_u);
            } else {
              rbp_r_t.SetColor(NodeColor::Red);
              rbp_r_t = RotateLeft(rbp_r_c);
            }
          }
          rbp_r_p.SetLeft(rbp_r_t);
          rbp_r_c = rbp_r_t;
        } else {
          // Move right.
          rbp_r_p = rbp_r_c;
          rbp_r_c = rbp_r_c.Right();
        }
      }
    }
    if (rbp_r_cmp != Order::eEqual) {
      while (true) {
        MOZ_ASSERT(rbp_r_p);
        rbp_r_cmp = Trait::Compare(aNode.Get(), rbp_r_c.Get());
        if (rbp_r_cmp == Order::eLess) {
          rbp_r_t = rbp_r_c.Left();
          if (!rbp_r_t) {
            // rbp_r_c now refers to the successor node to
            // relocate, and rbp_r_xp/aNode refer to the
            // context for the relocation.
            if (rbp_r_xp.Left() == aNode) {
              rbp_r_xp.SetLeft(rbp_r_c);
            } else {
              MOZ_ASSERT(rbp_r_xp.Right() == (aNode));
              rbp_r_xp.SetRight(rbp_r_c);
            }
            rbp_r_c.SetLeft(aNode.Left());
            rbp_r_c.SetRight(aNode.Right());
            rbp_r_c.SetColor(aNode.Color());
            if (rbp_r_p.Left() == rbp_r_c) {
              rbp_r_p.SetLeft(nullptr);
            } else {
              MOZ_ASSERT(rbp_r_p.Right() == rbp_r_c);
              rbp_r_p.SetRight(nullptr);
            }
            break;
          }
          rbp_r_u = rbp_r_t.Left();
          if (rbp_r_t.IsBlack() && rbp_r_u.IsBlack()) {
            rbp_r_t = MoveRedLeft(rbp_r_c);
            if (rbp_r_p.Left() == rbp_r_c) {
              rbp_r_p.SetLeft(rbp_r_t);
            } else {
              rbp_r_p.SetRight(rbp_r_t);
            }
            rbp_r_c = rbp_r_t;
          } else {
            rbp_r_p = rbp_r_c;
            rbp_r_c = rbp_r_c.Left();
          }
        } else {
          // Check whether to delete this node (it has to be
          // the correct node and a leaf node).
          if (rbp_r_cmp == Order::eEqual) {
            MOZ_ASSERT(aNode == rbp_r_c);
            if (!rbp_r_c.Right()) {
              // Delete leaf node.
              if (rbp_r_c.Left()) {
                rbp_r_t = LeanRight(rbp_r_c);
                rbp_r_t.SetRight(nullptr);
              } else {
                rbp_r_t = nullptr;
              }
              if (rbp_r_p.Left() == rbp_r_c) {
                rbp_r_p.SetLeft(rbp_r_t);
              } else {
                rbp_r_p.SetRight(rbp_r_t);
              }
              break;
            }
            // This is the node we want to delete, but we
            // will instead swap it with its successor
            // and delete the successor. Record enough
            // information to do the swap later.
            // rbp_r_xp is aNode's parent.
            rbp_r_xp = rbp_r_p;
          }
          rbp_r_t = rbp_r_c.Right();
          rbp_r_u = rbp_r_t.Left();
          if (rbp_r_u.IsBlack()) {
            rbp_r_t = MoveRedRight(rbp_r_c);
            if (rbp_r_p.Left() == rbp_r_c) {
              rbp_r_p.SetLeft(rbp_r_t);
            } else {
              rbp_r_p.SetRight(rbp_r_t);
            }
            rbp_r_c = rbp_r_t;
          } else {
            rbp_r_p = rbp_r_c;
            rbp_r_c = rbp_r_c.Right();
          }
        }
      }
    }
    // Update root.
    mRoot = TreeNode(rbp_r_s.addr()).Left().Get();
    aNode.SetLeft(nullptr);
    aNode.SetRight(nullptr);
    aNode.SetColor(NodeColor::Black);
  }

  TreeNode RotateLeft(TreeNode aNode) {
    TreeNode node = aNode.Right();
    aNode.SetRight(node.Left());
    node.SetLeft(aNode);
    return node;
  }

  TreeNode RotateRight(TreeNode aNode) {
    TreeNode node = aNode.Left();
    aNode.SetLeft(node.Right());
    node.SetRight(aNode);
    return node;
  }

  TreeNode LeanLeft(TreeNode aNode) {
    TreeNode node = RotateLeft(aNode);
    NodeColor color = aNode.Color();
    node.SetColor(color);
    aNode.SetColor(NodeColor::Red);
    return node;
  }

  TreeNode LeanRight(TreeNode aNode) {
    TreeNode node = RotateRight(aNode);
    NodeColor color = aNode.Color();
    node.SetColor(color);
    aNode.SetColor(NodeColor::Red);
    return node;
  }

  TreeNode MoveRedLeft(TreeNode aNode) {
    TreeNode node;
    TreeNode rbp_mrl_t, rbp_mrl_u;
    rbp_mrl_t = aNode.Left();
    rbp_mrl_t.SetColor(NodeColor::Red);
    rbp_mrl_t = aNode.Right();
    rbp_mrl_u = rbp_mrl_t.Left();
    if (rbp_mrl_u.IsRed()) {
      rbp_mrl_u = RotateRight(rbp_mrl_t);
      aNode.SetRight(rbp_mrl_u);
      node = RotateLeft(aNode);
      rbp_mrl_t = aNode.Right();
      if (rbp_mrl_t.IsRed()) {
        rbp_mrl_t.SetColor(NodeColor::Black);
        aNode.SetColor(NodeColor::Red);
        rbp_mrl_t = RotateLeft(aNode);
        node.SetLeft(rbp_mrl_t);
      } else {
        aNode.SetColor(NodeColor::Black);
      }
    } else {
      aNode.SetColor(NodeColor::Red);
      node = RotateLeft(aNode);
    }
    return node;
  }

  TreeNode MoveRedRight(TreeNode aNode) {
    TreeNode node;
    TreeNode rbp_mrr_t;
    rbp_mrr_t = aNode.Left();
    if (rbp_mrr_t.IsRed()) {
      TreeNode rbp_mrr_u, rbp_mrr_v;
      rbp_mrr_u = rbp_mrr_t.Right();
      rbp_mrr_v = rbp_mrr_u.Left();
      if (rbp_mrr_v.IsRed()) {
        rbp_mrr_u.SetColor(aNode.Color());
        rbp_mrr_v.SetColor(NodeColor::Black);
        rbp_mrr_u = RotateLeft(rbp_mrr_t);
        aNode.SetLeft(rbp_mrr_u);
        node = RotateRight(aNode);
        rbp_mrr_t = RotateLeft(aNode);
        node.SetRight(rbp_mrr_t);
      } else {
        rbp_mrr_t.SetColor(aNode.Color());
        rbp_mrr_u.SetColor(NodeColor::Red);
        node = RotateRight(aNode);
        rbp_mrr_t = RotateLeft(aNode);
        node.SetRight(rbp_mrr_t);
      }
      aNode.SetColor(NodeColor::Red);
    } else {
      rbp_mrr_t.SetColor(NodeColor::Red);
      rbp_mrr_t = rbp_mrr_t.Left();
      if (rbp_mrr_t.IsRed()) {
        rbp_mrr_t.SetColor(NodeColor::Black);
        node = RotateRight(aNode);
        rbp_mrr_t = RotateLeft(aNode);
        node.SetRight(rbp_mrr_t);
      } else {
        node = RotateLeft(aNode);
      }
    }
    return node;
  }

  // The iterator simulates recursion via an array of pointers that store the
  // current path.  This is critical to performance, since a series of calls to
  // rb_{next,prev}() would require time proportional to (n lg n), whereas this
  // implementation only requires time proportional to (n).
  //
  // Since the iterator caches a path down the tree, any tree modification may
  // cause the cached path to become invalid. Don't modify the tree during an
  // iteration.

  // Size the path arrays such that they are always large enough, even if a
  // tree consumes all of memory.  Since each node must contain a minimum of
  // two pointers, there can never be more nodes than:
  //
  //   1 << ((sizeof(void*)<<3) - (log2(sizeof(void*))+1))
  //
  // Since the depth of a tree is limited to 3*lg(#nodes), the maximum depth
  // is:
  //
  //   (3 * ((sizeof(void*)<<3) - (log2(sizeof(void*))+1)))
  //
  // This works out to a maximum depth of 87 and 180 for 32- and 64-bit
  // systems, respectively (approximately 348 and 1440 bytes, respectively).
 public:
  class Iterator {
    TreeNode mPath[3 * ((sizeof(void*) << 3) - (LOG2(sizeof(void*)) + 1))];
    unsigned mDepth;

   public:
    explicit Iterator(RedBlackTree<T, Trait>* aTree) : mDepth(0) {
      // Initialize the path to contain the left spine.
      if (aTree->mRoot) {
        TreeNode node;
        mPath[mDepth++] = aTree->mRoot;
        while ((node = mPath[mDepth - 1].Left())) {
          mPath[mDepth++] = node;
        }
      }
    }

    template <typename Iterator>
    class Item {
      Iterator* mIterator;
      T* mItem;

     public:
      Item(Iterator* aIterator, T* aItem)
          : mIterator(aIterator), mItem(aItem) {}

      bool operator!=(const Item& aOther) const {
        return (mIterator != aOther.mIterator) || (mItem != aOther.mItem);
      }

      T* operator*() const { return mItem; }

      const Item& operator++() {
        mItem = mIterator->Next();
        return *this;
      }
    };

    Item<Iterator> begin() {
      return Item<Iterator>(this,
                            mDepth > 0 ? mPath[mDepth - 1].Get() : nullptr);
    }

    Item<Iterator> end() { return Item<Iterator>(this, nullptr); }

    T* Next() {
      TreeNode node;
      if ((node = mPath[mDepth - 1].Right())) {
        // The successor is the left-most node in the right subtree.
        mPath[mDepth++] = node;
        while ((node = mPath[mDepth - 1].Left())) {
          mPath[mDepth++] = node;
        }
      } else {
        // The successor is above the current node.  Unwind until a
        // left-leaning edge is removed from the path, of the path is empty.
        for (mDepth--; mDepth > 0; mDepth--) {
          if (mPath[mDepth - 1].Left() == mPath[mDepth]) {
            break;
          }
        }
      }
      return mDepth > 0 ? mPath[mDepth - 1].Get() : nullptr;
    }
  };

  Iterator iter() { return Iterator(this); }
};

#endif  // RB_H_