aboutsummaryrefslogtreecommitdiff
path: root/gcc/splay-tree-utils.tcc
blob: 1ea92c400fd0a22761cd9aa41d71483c3104560f (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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
// Splay tree utilities                                             -*- C++ -*-
// Copyright (C) 2020-2023 Free Software Foundation, Inc.
//
// This file is part of GCC.
//
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
//
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// INDEX is either 0 or 1.  If it is 0, return NODE's left child,
// otherwise return NODE's right child.
template<typename Accessors>
inline typename base_splay_tree<Accessors>::node_type
base_splay_tree<Accessors>::get_child (node_type node, unsigned int index)
{
  return Accessors::child (node, index);
}

// INDEX is either 0 or 1.  If it is 0, change NODE's left child to CHILD,
// otherwise change NODE's right child to CHILD.  If CHILD has a parent
// field, record that its parent is now NODE.
template<typename Accessors>
inline void
base_splay_tree<Accessors>::set_child (node_type node, unsigned int index,
				       node_type child)
{
  Accessors::child (node, index) = child;
  if (child)
    set_parent (child, node);
}

// Rotate the tree to promote child number INDEX of NODE, so that that
// child becomes a parent of NODE.  Return the promoted node.
//
// The caller has the responsibility of assigning a correct parent
// to the returned node.
template<typename Accessors>
inline typename base_splay_tree<Accessors>::node_type
base_splay_tree<Accessors>::promote_child (node_type node, unsigned int index)
{
  node_type promoted = get_child (node, index);
  set_child (node, index, get_child (promoted, 1 - index));
  set_child (promoted, 1 - index, node);
  return promoted;
}

// Treat child number INDEX of NODE as being CHILD and rotate the tree
// so that CHILD becomes a parent of NODE.
//
// The caller has the responsibility of assigning a correct parent to CHILD.
template<typename Accessors>
inline void
base_splay_tree<Accessors>::promote_child (node_type node, unsigned int index,
					   node_type child)
{
  set_child (node, index, get_child (child, 1 - index));
  set_child (child, 1 - index, node);
}

// Print NODE to PP, using PRINTER (PP, N) to print the contents of node N.
// Prefix each new line with INDENT_STRING.  CODE is 'T' if NODE is the root
// node, 'L' if NODE is the left child of its parent, or 'R' if NODE is the
// right child of its parent.
template<typename Accessors>
template<typename Printer>
void
base_splay_tree<Accessors>::print (pretty_printer *pp, node_type node,
				   Printer printer, char code,
				   vec<char> &indent_string)
{
  // In the comments below, PREFIX refers to the incoming contents
  // of INDENT_STRING.
  node_type left = get_child (node, 0);
  node_type right = get_child (node, 1);

  auto orig_indent_len = indent_string.length ();
  indent_string.safe_grow (orig_indent_len + 3);
  char *extra_indent = indent_string.address () + orig_indent_len;

  // Print [T], [L], or [R].
  extra_indent[0] = '[';
  extra_indent[1] = code;
  extra_indent[2] = ']';
  pp_append_text (pp, extra_indent, indent_string.end ());
  pp_space (pp);

  // Print the node itself, using PREFIX + " | " or PREFIX + "   " to indent
  // new lines under the "[_]" that we just printed.
  extra_indent[0] = ' ';
  extra_indent[1] = (left || right ? '|' : ' ');
  extra_indent[2] = ' ';
  {
    pretty_printer sub_pp;
    printer (&sub_pp, node);
    const char *text = pp_formatted_text (&sub_pp);
    while (const char *end = strchr (text, '\n'))
      {
	pp_append_text (pp, text, end);
	pp_newline_and_indent (pp, 0);
	pp_append_text (pp, indent_string.begin (), indent_string.end ());
	text = end + 1;
      }
    pp_string (pp, text);
  }

  if (left)
    {
      // Print PREFIX + " +-" for the first line of the left subtree,
      // to be followed by "[L]".
      extra_indent[1] = '+';
      extra_indent[2] = '-';
      pp_newline_and_indent (pp, 0);
      pp_append_text (pp, indent_string.begin (), indent_string.end ());

      // Print the left subtree, using PREFIX + " | " or PREFIX + "   "
      // to indent under the PREFIX + " +-" that we just printed.
      extra_indent[1] = right ? '|' : ' ';
      extra_indent[2] = ' ';
      print (pp, left, printer, 'L', indent_string);
      extra_indent = indent_string.address () + orig_indent_len;

      // If LEFT is not a leaf and we also have a right subtree, use a
      // PREFIX + " |" line to separate them.
      if (right && (get_child (left, 0) || get_child (left, 1)))
	{
	  pp_newline_and_indent (pp, 0);
	  pp_append_text (pp, indent_string.begin (), &extra_indent[2]);
	}
    }
  if (right)
    {
      // Print PREFIX + " +-" for the first line of the right subtree,
      // to be followed by "[R]".
      extra_indent[1] = '+';
      extra_indent[2] = '-';
      pp_newline_and_indent (pp, 0);
      pp_append_text (pp, indent_string.begin (), indent_string.end ());

      // Print the right subtree, using PREFIX + "   " to indent under the
      // PREFIX + " +-" that we just printed.
      extra_indent[1] = ' ';
      extra_indent[2] = ' ';
      print (pp, right, printer, 'R', indent_string);
    }
  indent_string.truncate (orig_indent_len);
}

// See the comment above the declaration.
template<typename Accessors>
template<typename Printer>
void
base_splay_tree<Accessors>::print (pretty_printer *pp, node_type node,
				   Printer printer)
{
  if (!node)
    {
      pp_string (pp, "null");
      return;
    }
  auto_vec<char, 64> indent_string;
  print (pp, node, printer, 'T', indent_string);
}

// If N is 1, splay the last (rightmost) node reachable from START
// to the position that START current holds and return the splayed node.
// START is not itself the last node.
//
// If N is 0, splay the first (leftmost) node reachable from START
// to the position that START current holds and return the splayed node.
// START is not itself the first node.
//
// The caller has the responsibility of updating the parent of the
// returned node.
template<typename Accessors>
template<unsigned int N>
typename base_splay_tree<Accessors>::node_type
base_splay_tree<Accessors>::splay_limit (node_type start)
{
  // This essentially follows the simpilfied top-down method described
  // in Sleator and Tarjan's "Self-adjusting Binary Search Trees", but
  // specialized for the case in which the comparison result is fixed.
  // The first iteration is peeled to avoid the need for stack temporaries.
  //
  // The comments and names reflect the behavior for N == 1, but the
  // N == 0 case behaves analogously.

  // Rotate the tree to promote the right child of START to the root.
  node_type node = promote_child (start, N);
  if (node_type right = get_child (node, N))
    {
      // Perform the link left step, which for this first iteration
      // means making NODE the root of the left tree.
      //
      // NODE will become left child of the final node.  For a right
      // spine starting at NODE of the form:
      //
      //  1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> ... -> N
      //  |    |    |    |    |    |    |           |
      //  V    V    V    V    V    V    V           V
      //  A    B    C    D    E    F    G           NL
      //
      // the next step is to create a subtree of N whose right spine contains
      // the odd-numbered nodes, as follows:
      //
      //  N
      //  |
      //  V
      //  1 ------> 3 ------> 5 ------> 7 -> .... -> NL
      //  |         |         |         |
      //  V         V         V         V
      //  A         2 -> C    4 -> E    6 -> G
      //            |         |         |
      //            V         V         V
      //            B         D         F
      //
      // First record 1 as the left child of the final root (N) and move
      // on to node 2.
      node_type final_child = node;
      node_type new_spine_end = node;
      node = right;
      while (node_type right = get_child (node, N))
	{
	  // Perform another rotate left step.
	  //
	  // We've built the tree rooted at 1 in the diagram above up to,
	  // but not including, an even-numbered node NODE on the original
	  // right spine.  Rotate the tree at NODE to promote the following
	  // odd-numbered node.
	  promote_child (node, N, right);
	  node = right;
	  if (node_type right = get_child (node, N))
	    {
	      // Perform another link left step.
	      //
	      // Add the promoted odd-numbered node to the right spine of the
	      // tree rooted at 1 and move on to the next even-numbered node.
	      set_child (new_spine_end, N, node);
	      new_spine_end = node;
	      node = right;
	    }
	}
      // Perform the assembly step.
      //
      // Add NL to the new spine and make N the new root.
      set_child (new_spine_end, N, get_child (node, 1 - N));
      set_child (node, 1 - N, final_child);
    }
  return node;
}

// Remove NODE from its position in the splay tree.  If NODE has at least
// one child node, return the node that should now hold NODE's position in
// the splay tree.  If NODE has no children, return null.
//
// The caller has the responsibility of updating the parent of the
// returned node.
template<typename Accessors>
inline typename base_splay_tree<Accessors>::node_type
base_splay_tree<Accessors>::remove_node_internal (node_type node)
{
  node_type left = get_child (node, 0);
  node_type right = get_child (node, 1);
  if (!left)
    return right;

  if (!right)
    return left;

  if (get_child (left, 1))
    {
      left = splay_limit<1> (left);
      gcc_checking_assert (!get_child (left, 1));
    }
  set_child (left, 1, right);
  return left;
}

// See the comment above the declaration.
template<typename Accessors>
inline void
base_splay_tree<Accessors>::insert_child (node_type node, unsigned int index,
					  node_type child)
{
  gcc_checking_assert (!get_child (child, 0) && !get_child (child, 1));
  set_child (child, index, get_child (node, index));
  set_child (node, index, child);
}

// Implement splay_next_node if N == 1 and splay_prev_node if N == 0.
template<typename Accessors>
template<unsigned int N>
bool
rooted_splay_tree<Accessors>::splay_neighbor ()
{
  node_type node = m_root;
  node_type new_root = get_child (node, N);
  if (!new_root)
    return false;

  if (get_child (new_root, 1 - N))
    {
      // NEW_ROOT is not itself the required node, so splay the required
      // node into its place.
      new_root = parent::template splay_limit<1 - N> (new_root);
      gcc_checking_assert (!get_child (new_root, 1 - N));
      set_child (node, N, node_type ());
      set_child (new_root, 1 - N, node);
    }
  else
    promote_child (node, N, new_root);
  set_parent (new_root, node_type ());
  m_root = new_root;
  return true;
}

// See the comment above the declaration.
template<typename Accessors>
template<typename Comparator>
bool
rooted_splay_tree<Accessors>::insert (node_type new_node, Comparator compare)
{
  gcc_checking_assert (!get_child (new_node, 0) && !get_child (new_node, 1));
  if (!m_root)
    {
      m_root = new_node;
      return true;
    }

  int comparison = lookup (compare);
  if (comparison == 0)
    return false;

  // Insert NEW_NODE before M_ROOT if COMPARISON < 0 and after M_ROOT
  // otherwise.
  set_child (new_node, comparison < 0, m_root);
  set_child (new_node, comparison > 0, get_child (m_root, comparison > 0));
  set_child (m_root, comparison > 0, nullptr);
  m_root = new_node;
  return true;
}

// See the comment above the declaration.
template<typename Accessors>
inline void
rooted_splay_tree<Accessors>::insert_max_node (node_type new_node)
{
  gcc_checking_assert (!get_child (new_node, 0) && !get_child (new_node, 1));
  set_child (new_node, 0, m_root);
  m_root = new_node;
}

// See the comment above the declaration.
template<typename Accessors>
inline void
rooted_splay_tree<Accessors>::splice_next_tree (rooted_splay_tree next_tree)
{
  splay_max_node ();
  set_child (m_root, 1, next_tree.m_root);
}

// See the comment above the declaration.
template<typename Accessors>
inline void
rooted_splay_tree<Accessors>::replace_max_node_at_root (node_type new_node)
{
  node_type old_node = m_root;
  gcc_checking_assert (!get_child (new_node, 0)
		       && !get_child (new_node, 1)
		       && !get_child (old_node, 1));
  set_child (new_node, 0, get_child (old_node, 0));
  // Clear the links from OLD_NODE.  Its parent and right child are
  // already node_type ().
  set_child (old_node, 0, node_type ());
  m_root = new_node;
}

// See the comment above the declaration.
template<typename Accessors>
inline void
rooted_splay_tree<Accessors>::remove_root ()
{
  node_type node = m_root;
  m_root = parent::remove_node_internal (node);
  if (m_root)
    set_parent (m_root, node_type ());
  // Clear the links from NODE.  Its parent is already node_type ().
  set_child (node, 0, node_type ());
  set_child (node, 1, node_type ());
}

// See the comment above the declaration.
template<typename Accessors>
inline rooted_splay_tree<Accessors>
rooted_splay_tree<Accessors>::split_before_root ()
{
  node_type new_root = get_child (m_root, 0);
  set_child (m_root, 0, node_type ());
  set_parent (new_root, node_type ());
  return new_root;
}

// See the comment above the declaration.
template<typename Accessors>
inline rooted_splay_tree<Accessors>
rooted_splay_tree<Accessors>::split_after_root ()
{
  node_type new_root = get_child (m_root, 1);
  set_child (m_root, 1, node_type ());
  set_parent (new_root, node_type ());
  return new_root;
}

// See the comment above the declaration.
template<typename Accessors>
inline bool
rooted_splay_tree<Accessors>::splay_prev_node ()
{
  return splay_neighbor<0> ();
}

// See the comment above the declaration.
template<typename Accessors>
inline bool
rooted_splay_tree<Accessors>::splay_next_node ()
{
  return splay_neighbor<1> ();
}

// See the comment above the declaration.
template<typename Accessors>
inline void
rooted_splay_tree<Accessors>::splay_min_node ()
{
  if (m_root && get_child (m_root, 0))
    {
      m_root = parent::template splay_limit<0> (m_root);
      set_parent (m_root, node_type ());
    }
}

// See the comment above the declaration.
template<typename Accessors>
inline void
rooted_splay_tree<Accessors>::splay_max_node ()
{
  if (m_root && get_child (m_root, 1))
    {
      m_root = parent::template splay_limit<1> (m_root);
      set_parent (m_root, node_type ());
    }
}

// See the comment above the declaration.
template<typename Accessors>
inline typename rooted_splay_tree<Accessors>::node_type
rooted_splay_tree<Accessors>::min_node ()
{
  splay_min_node ();
  return m_root;
}

// See the comment above the declaration.
template<typename Accessors>
inline typename rooted_splay_tree<Accessors>::node_type
rooted_splay_tree<Accessors>::max_node ()
{
  splay_max_node ();
  return m_root;
}

// See the comment above the declaration.
template<typename Accessors>
template<typename Comparator>
auto
rooted_splay_tree<Accessors>::lookup (Comparator compare)
  -> decltype (compare (m_root))
{
  // This essentially follows the simpilfied top-down method described
  // in Sleator and Tarjan's "Self-adjusting Binary Search Trees", but
  // with the complication that the comparisons are done only once.
  using result_type = decltype (compare (m_root));

  // The roots of the left and right trees.
  node_type link_left_root = node_type ();
  node_type link_right_root = node_type ();

  // Where to add new nodes to the left and right trees.
  node_type *link_left_ptr = &link_left_root;
  node_type *link_right_ptr = &link_right_root;

  // The nodes that contain *LINK_LEFT_PTR and *LINK_RIGHT_PTR,
  // once they no longer point to the roots above.
  node_type link_left_parent = node_type ();
  node_type link_right_parent = node_type ();

  auto link_left = [&](node_type node)
    {
      *link_left_ptr = node;
      link_left_ptr = &Accessors::child (node, 1);
      set_parent (node, link_left_parent);
      link_left_parent = node;
    };

  auto link_right = [&](node_type node)
    {
      *link_right_ptr = node;
      link_right_ptr = &Accessors::child (node, 0);
      set_parent (node, link_right_parent);
      link_right_parent = node;
    };

  node_type node = m_root;
  node_type parent = node_type ();
  result_type result;
  result_type old_result = 0;
  while (1)
    {
      // OLD_RESULT is 0 if NODE is the root of the middle tree.
      // Otherwise, PARENT is the root of the middle tree and OLD_RESULT
      // is how it compared.
      //
      // Results are:
      // < 0 if we want something smaller.
      // = 0 if we found the right node.
      // > 0 if we want something bigger.
      result = compare (node);
      if (old_result < 0)
	{
	  if (result < 0)
	    {
	      // SEARCH < NODE < PARENT
	      //
	      // Promote NODE (rotate right).
	      promote_child (parent, 0, node);
	      node_type next = get_child (node, 0);
	      if (!next)
		break;

	      link_right (node);

	      // NEXT is now the root of the middle tree.
	      node = next;
	      old_result = 0;
	      continue;
	    }

	  // SEARCH >= NODE, NODE < PARENT
	  link_right (parent);
	}
      else if (old_result > 0)
	{
	  if (result > 0)
	    {
	      // SEARCH > NODE > PARENT
	      //
	      // Promote NODE (rotate left).
	      promote_child (parent, 1, node);
	      node_type next = get_child (node, 1);
	      if (!next)
		break;

	      link_left (node);

	      // NEXT is now the root of the middle tree.
	      node = next;
	      old_result = 0;
	      continue;
	    }

	  // SEARCH <= NODE, NODE > PARENT
	  link_left (parent);
	}

      // Microoptimization to allow NODE to be read even if RESULT == 0.
      node_type next = get_child (node, result >= 0);
      if (result == 0 || !next)
	break;

      // NODE is now the root of the tree.
      parent = node;
      node = next;
      old_result = result;
    }

  node_type new_left = link_left_root;
  node_type new_right = link_right_root;

  if (new_left)
    {
      node_type old_left = get_child (node, 0);
      *link_left_ptr = old_left;
      if (old_left)
	set_parent (old_left, link_left_parent);
      set_child (node, 0, new_left);
    }

  if (new_right)
    {
      node_type old_right = get_child (node, 1);
      *link_right_ptr = old_right;
      if (old_right)
	set_parent (old_right, link_right_parent);
      set_child (node, 1, new_right);
    }

  set_parent (node, node_type ());
  m_root = node;
  return result;
}

// See the comment above the declaration.
template<typename Accessors>
template<typename LeftPredicate, typename RightPredicate>
int
rooted_splay_tree<Accessors>::lookup (LeftPredicate want_something_smaller,
				      RightPredicate want_something_bigger)
{
  // This essentially follows the simpilfied top-down method described
  // in Sleator and Tarjan's "Self-adjusting Binary Search Trees"
  // (and follows it more closely than the single-comparator version above).

  // The roots of the left and right trees.
  node_type link_left_root = node_type ();
  node_type link_right_root = node_type ();

  // Where to add new nodes to the left and right trees.
  node_type *link_left_ptr = &link_left_root;
  node_type *link_right_ptr = &link_right_root;

  // The nodes that contain *LINK_LEFT_PTR and *LINK_RIGHT_PTR,
  // once they no longer point to the roots above.
  node_type link_left_parent = node_type ();
  node_type link_right_parent = node_type ();

  node_type node = m_root;
  int result;
  for (;;)
    {
      // NODE is the root of the middle tree.
      if (want_something_smaller (node))
	{
	  result = -1;
	  node_type next = get_child (node, 0);
	  if (!next)
	    break;

	  if (want_something_smaller (next))
	    {
	      // Promote NODE (rotate right).
	      promote_child (node, 0, next);
	      node = next;
	      next = get_child (node, 0);
	      if (!next)
		break;
	    }

	  // Add NODE to the right tree (link right).
	  *link_right_ptr = node;
	  link_right_ptr = &Accessors::child (node, 0);
	  set_parent (node, link_right_parent);
	  link_right_parent = node;

	  node = next;
	}
      else if (want_something_bigger (node))
	{
	  result = 1;
	  node_type next = get_child (node, 1);
	  if (!next)
	    break;

	  if (want_something_bigger (next))
	    {
	      // Promote NODE (rotate left).
	      promote_child (node, 1, next);
	      node = next;
	      next = get_child (node, 1);
	      if (!next)
		break;
	    }

	  // Add NODE to the left tree (link left).
	  *link_left_ptr = node;
	  link_left_ptr = &Accessors::child (node, 1);
	  set_parent (node, link_left_parent);
	  link_left_parent = node;

	  node = next;
	}
      else
	{
	  result = 0;
	  break;
	}
    }

  node_type new_left = link_left_root;
  node_type new_right = link_right_root;

  if (new_left)
    {
      node_type old_left = get_child (node, 0);
      *link_left_ptr = old_left;
      if (old_left)
	set_parent (old_left, link_left_parent);
      set_child (node, 0, new_left);
    }

  if (new_right)
    {
      node_type old_right = get_child (node, 1);
      *link_right_ptr = old_right;
      if (old_right)
	set_parent (old_right, link_right_parent);
      set_child (node, 1, new_right);
    }

  set_parent (node, node_type ());
  m_root = node;
  return result;
}

// See the comment above the declaration.
template<typename Accessors>
template<typename Printer>
inline void
rooted_splay_tree<Accessors>::print (pretty_printer *pp, Printer printer) const
{
  print (pp, m_root, printer);
}

// Return NODE's current parent.
template<typename Accessors>
inline typename rootless_splay_tree<Accessors>::node_type
rootless_splay_tree<Accessors>::get_parent (node_type node)
{
  return Accessors::parent (node);
}

// CHILD is known to be a child of PARENT.  Return which index it has.
template<typename Accessors>
inline unsigned int
rootless_splay_tree<Accessors>::child_index (node_type parent, node_type child)
{
  return get_child (parent, 1) == child;
}

// If N == 1, implement splay_known_max_node, otherwise implement
// splay_known_min_node.
template<typename Accessors>
template<unsigned int N>
inline void
rootless_splay_tree<Accessors>::splay_known_limit (node_type node)
{
  node_type child = node;
  node_type parent = get_parent (child);
  if (!parent)
    return;

  do
    // At this point, NODE conceptually replaces CHILD as a child of
    // PARENT, but we haven't yet updated PARENT accordingly.
    if (node_type grandparent = get_parent (parent))
      {
	node_type greatgrandparent = get_parent (grandparent);
	promote_child (grandparent, N, parent);
	promote_child (parent, N, node);
	child = grandparent;
	parent = greatgrandparent;
      }
    else
      {
	promote_child (parent, N, node);
	break;
      }
  while (parent);
  set_parent (node, node_type ());
}

// See the comment above the declaration.
template<typename Accessors>
typename rootless_splay_tree<Accessors>::node_type
rootless_splay_tree<Accessors>::remove_node (node_type node)
{
  node_type replacement = parent::remove_node_internal (node);
  if (node_type parent = get_parent (node))
    set_child (parent, child_index (parent, node), replacement);
  else if (replacement)
    set_parent (replacement, node_type ());
  // Clear the links from NODE.
  set_parent (node, node_type ());
  set_child (node, 0, node_type ());
  set_child (node, 1, node_type ());
  return replacement;
}

// See the comment above the declaration.
template<typename Accessors>
void
rootless_splay_tree<Accessors>::splay (node_type node)
{
  node_type child = node;
  node_type parent = get_parent (child);
  if (!parent)
    return;

  do
    {
      // At this point, NODE conceptually replaces CHILD as a child of
      // PARENT, but we haven't yet updated PARENT accordingly.
      unsigned int index = child_index (parent, child);
      if (node_type grandparent = get_parent (parent))
	{
	  node_type greatgrandparent = get_parent (grandparent);
	  unsigned int parent_index = child_index (grandparent, parent);
	  if (index == parent_index)
	    {
	      promote_child (grandparent, parent_index, parent);
	      promote_child (parent, index, node);
	    }
	  else
	    {
	      promote_child (parent, index, node);
	      promote_child (grandparent, parent_index, node);
	    }
	  child = grandparent;
	  parent = greatgrandparent;
	}
      else
	{
	  promote_child (parent, index, node);
	  break;
	}
    }
  while (parent);
  set_parent (node, node_type ());
}

// See the comment above the declaration.
template<typename Accessors>
inline void
rootless_splay_tree<Accessors>::splay_known_min_node (node_type node)
{
  splay_known_limit<0> (node);
}

// See the comment above the declaration.
template<typename Accessors>
inline void
rootless_splay_tree<Accessors>::splay_known_max_node (node_type node)
{
  splay_known_limit<1> (node);
}

// See the comment above the declaration.
template<typename Accessors>
template<typename DefaultResult, typename Predicate>
auto
rootless_splay_tree<Accessors>::
splay_and_search (node_type node, DefaultResult default_result,
		  Predicate predicate)
  -> decltype (predicate (node, 0))
{
  using Result = decltype (predicate (node, 0));

  node_type child = node;
  node_type parent = get_parent (child);
  if (!parent)
    return default_result;

  do
    {
      // At this point, NODE conceptually replaces CHILD as a child of
      // PARENT, but we haven't yet updated PARENT accordingly.
      unsigned int index = child_index (parent, child);
      if (Result result = predicate (parent, index))
	{
	  set_child (parent, index, node);
	  return result;
	}
      if (node_type grandparent = get_parent (parent))
	{
	  node_type greatgrandparent = get_parent (grandparent);
	  unsigned int parent_index = child_index (grandparent, parent);
	  if (Result result = predicate (grandparent, parent_index))
	    {
	      set_child (parent, index, node);
	      return result;
	    }
	  if (index == parent_index)
	    {
	      promote_child (grandparent, parent_index, parent);
	      promote_child (parent, index, node);
	    }
	  else
	    {
	      promote_child (parent, index, node);
	      promote_child (grandparent, parent_index, node);
	    }
	  child = grandparent;
	  parent = greatgrandparent;
	}
      else
	{
	  promote_child (parent, index, node);
	  break;
	}
    }
  while (parent);
  set_parent (node, node_type ());
  return default_result;
}

// Splay NODE1 looking to see if one of its ancestors is NODE2.  If it is,
// return -1 if NODE1 comes before NODE2 or 1 if NODE1 comes after NODE2.
// Return 0 if NODE2 is not an ancestor of NODE1.
template<typename Accessors>
int
rootless_splay_tree<Accessors>::compare_nodes_one_way (node_type node1,
						       node_type node2)
{
  auto compare = [&](node_type parent, unsigned int index) -> int
    {
      if (parent == node2)
	return index ? 1 : -1;
      return 0;
    };
  return splay_and_search (node1, 0, compare);
}

// See the comment above the declaration.
template<typename Accessors>
int
rootless_splay_tree<Accessors>::compare_nodes (node_type node1,
					       node_type node2)
{
  if (node1 == node2)
    return 0;

  // Splay NODE1 looking for NODE2.
  int cmp = compare_nodes_one_way (node1, node2);
  if (cmp)
    return cmp;

  // That failed, but NODE1 is now the root of the tree.  Splay NODE2
  // to see on which side of NODE1 it falls.
  cmp = compare_nodes_one_way (node2, node1);
  gcc_checking_assert (cmp);
  return -cmp;
}