summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/core/util/rbtree.c
blob: 740e025541710dd1f2501795dea60c2e47aa8066 (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
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

/*******************************************************************************
 * rbtree.c
 *
 * Red-black tree library
 ******************************************************************************/

#include "xf.h"

/*******************************************************************************
 * Macros definitions
 ******************************************************************************/

/* ...node color */
#define RB_RED                          (1)
#define RB_BLK                          (0)

/* ...pointer to parent node */
#define RB_PARENT(tree, node)           ((node)->parent)

/* ...pointer to left child node */
#define RB_LEFT(tree, node)             ((node)->left)

/* ...pointer to right child node */
#define RB_RIGHT(tree, node)            ((node)->right)

/* ...pointer to right child node */
#define RB_COLOR(tree, node)            ((node)->color & 1)

/* ...check if node is black */
#define RB_IS_BLACK(tree, node)         (!((node)->color & RB_RED))

/* ...root node index of the tree - can be simplified? */
#define RB_ROOT(tree)                   RB_LEFT((tree), &(tree)->root)

/* ...empty node */
#define RB_NULL(tree)                   (&(tree)->root)

/*******************************************************************************
 * Helpers
 ******************************************************************************/

#define RB_SET_P(t, n, p)               \
    ({ (n)->parent = (p); })

#define RB_SET_L(t, n, l)               \
    ({ (n)->left = (l); })

#define RB_SET_R(t, n, r)               \
    ({ (n)->right = (r); })

#define RB_SET_C(t, n, c)               \
    RB_SET_C_##c((t), (n))

#define RB_SET_C_RB_BLK(t, n)           \
    ({ (n)->color &= ~1; })

#define RB_SET_C_RB_RED(t, n)           \
    ({ (n)->color |= 1; })

#define RB_SET_P_C(t, n, p, c)          \
    ({ (n)->parent = (p); RB_SET_C_##c(t, n); })

#define RB_SET_P_L(t, n, p, l)          \
    ({ (n)->parent = (p); (n)->left = (l); })

#define RB_SET_P_L_C(t, n, p, l, c)     \
    ({ (n)->parent = (p); (n)->left = (l); RB_SET_C_##c(t, n); })

#define RB_SET_P_R(t, n, p, r)          \
    ({ (n)->parent = (p); (n)->right = (r); })

#define RB_SET_P_R_C(t, n, p, r, c)     \
    ({ (n)->parent = (p); (n)->right = (r); RB_SET_C_##c(t, n); })

#define RB_SET_P_L_R(t, n, p, l, r)     \
    ({ (n)->parent = (p); (n)->left = (l); (n)->right = (r); })

#define RB_SET_P_L_R_C(t, n, p, l, r, c)\
    ({ (n)->parent = (p); (n)->left = (l); (n)->right = (r); RB_SET_C_##c(t, n); })

#define RB_SET_ROOT(t, n)               \
    RB_SET_L((t), &(t)->root, (n))

/*******************************************************************************
 * RB-tree functions
 ******************************************************************************/

/*******************************************************************************
 * rb_first, rb_last
 *
 * Return pointer to first/last item in the tree
 ******************************************************************************/

rb_idx_t rb_first(rb_tree_t *tree)
{
    rb_idx_t    p_idx, t_idx;
    
    if ((p_idx = RB_ROOT(tree)) != RB_NULL(tree))
    {
        /* ...find left-most item in non-empty tree */
        while ((t_idx = RB_LEFT(tree, p_idx)) != RB_NULL(tree))
            p_idx = t_idx;
    }
    
    return p_idx;
}

rb_idx_t rb_last(rb_tree_t *tree)
{
    rb_idx_t    p_idx, t_idx;
    
    if ((p_idx = RB_ROOT(tree)) != RB_NULL(tree))
    {
        /* ...find right-most item in non-empty tree */
        while ((t_idx = RB_RIGHT(tree, p_idx)) != RB_NULL(tree))
            p_idx = t_idx;
    }
    
    return p_idx;
}

/*******************************************************************************
 * rb_next, rb_prev
 *
 * Return next / previous in-order item in the tree
 ******************************************************************************/

rb_idx_t rb_next(rb_tree_t *tree, rb_idx_t n_idx)
{
    rb_idx_t    p_idx, c_idx, t_idx;
    
    /* ...if we have any right children, process them */
    if ((c_idx = RB_RIGHT(tree, n_idx)) != RB_NULL(tree))
    {
        /* ...descent to the left-most node starting from right child */
        while ((t_idx = RB_LEFT(tree, c_idx)) != RB_NULL(tree))
            c_idx = t_idx;
        return c_idx;
    }
    
    /* ...no right children; ascend to our parent while we are right child */
    while ((p_idx = RB_PARENT(tree, n_idx)) != RB_NULL(tree))
    {
        /* ...as soon as "n" is a left child, return "p" */
        if (n_idx == RB_RIGHT(tree, p_idx))
            n_idx = p_idx;
        else
            return p_idx;
    }
    
    /* ...we were right-most child */
    return p_idx;
}

rb_idx_t rb_prev(rb_tree_t *tree, rb_idx_t n_idx)
{
    rb_idx_t    p_idx, c_idx, t_idx;
    
    /* ...if we have any left children, process them */
    if ((c_idx = RB_LEFT(tree, n_idx)) != RB_NULL(tree))
    {
        /* ...descent to the right-most node starting from left child */
        while ((t_idx = RB_RIGHT(tree, c_idx)) != RB_NULL(tree))
            c_idx = t_idx;
        return c_idx;
    }
    
    /* ...no left children; ascend to our parent while we are left child */
    while ((p_idx = RB_PARENT(tree, n_idx)) != RB_NULL(tree))
    {
        /* ...as soon as "n" is a right child, return "p" */
        if (n_idx == RB_LEFT(tree, p_idx))
            n_idx = p_idx;
        else
            return p_idx;
    }
    
    /* ...we were left-most child */
    return p_idx;
}

/*******************************************************************************
 * rb_init
 *
 * Initialize rb-tree structure
 ******************************************************************************/

void rb_init(rb_tree_t *tree)
{
    /* ...initialize sentinel node of the empty tree */
    RB_SET_P_L_R_C(tree, &tree->root, RB_NULL(tree), RB_NULL(tree), RB_NULL(tree), RB_BLK);
}

/*******************************************************************************
 * rb_insert
 *
 * Insert new item into RB-tree. Returns non-zero node index on success
 ******************************************************************************/

/* ...internal tree balancing function */
static void __rb_insert_balance(rb_tree_t *tree, rb_idx_t n_idx, rb_idx_t p_idx)
{
	rb_idx_t    u_idx, g_idx, t_idx, cl_idx, cr_idx;

rebalance:

	/***************************************************************************
	 * Trivial case #1 - N (red) is a root
	 **************************************************************************/

	if (p_idx == RB_NULL(tree))
    {
		RB_SET_C(tree, n_idx, RB_BLK);
		goto root;
	}

	/***************************************************************************
	 * Trivial case #2 - P is black
	 **************************************************************************/

	if (RB_IS_BLACK(tree, p_idx))
		goto done;

	/***************************************************************************
	 * Complex cases - P is red, N is red
	 **************************************************************************/

	/* ...grandparent must exist and be black */
	g_idx = RB_PARENT(tree, p_idx);
	if (p_idx == RB_LEFT(tree, g_idx))
    {
		/* ...we are left grandchild; get uncle (if it exists) */
		u_idx = RB_RIGHT(tree, g_idx);

		/* ...if U is read, we have conditions of case #3 */
		if (!RB_IS_BLACK(tree, u_idx))
			goto case3;

		/* ...we will need grand-grand-parent later */
		t_idx = RB_PARENT(tree, g_idx);

		/* ...U is black/null; if we are LL grandchild, we have case #5 */
		if (n_idx == RB_LEFT(tree, p_idx))
			goto case5_ll;

		/* ...N is RL grandchild of G; case #4 */
		goto case4_rl;
	}
    else
    {
		/* ...we are right grandchild; get uncle (if it exists) */
		u_idx = RB_LEFT(tree, g_idx);

		/* ...if U is read, we have conditions of case #3 */
		if (!RB_IS_BLACK(tree, u_idx))
			goto case3;

		/* ...we will need grand-grand-parent later */
		t_idx = RB_PARENT(tree, g_idx);

		/* ...U is black/null; if we are RR grandchild, we have case #5 */
		if (n_idx == RB_RIGHT(tree, p_idx))
			goto case5_rr;

		/* ...N is LR grandchild of G; case #4 */
		goto case4_lr;
	}

case4_rl:

	/***************************************************************************
	 * Case #4 - P is red, U is black, N is red RL grandchild of G. We will do
	 * two tree rotations - first the one described in case #4, second is the
	 * one described in case #5 (as have conditions for case #5(LL) with P and
	 * N changing roles
	 **************************************************************************/

	cl_idx = RB_LEFT(tree, n_idx), cr_idx = RB_RIGHT(tree, n_idx);
	RB_SET_P_L_R_C(tree, n_idx, t_idx, p_idx, g_idx, RB_BLK);
	RB_SET_P_R(tree, p_idx, n_idx, cl_idx);
	RB_SET_P(tree, cl_idx, p_idx);
	RB_SET_P_L_C(tree, g_idx, n_idx, cr_idx, RB_RED);
	RB_SET_P(tree, cr_idx, g_idx);

	/* ...new root of subtree is N; adjust T pointer */
	goto case5_xx;

case4_lr:

	/***************************************************************************
	 * Case #4 - P is red, U is black, N is red LR grandchild of G. We will do
	 * two tree rotations - first the one described in case #4, second is the
	 * one described in case #5 (as have conditions for case #5(RR) with P and
	 * N changing roles
	 **************************************************************************/

	cl_idx = RB_LEFT(tree, n_idx), cr_idx = RB_RIGHT(tree, n_idx);
	RB_SET_P_L_R_C(tree, n_idx, t_idx, g_idx, p_idx, RB_BLK);
	RB_SET_P_L(tree, p_idx, n_idx, cr_idx);
	RB_SET_P(tree, cr_idx, p_idx);
	RB_SET_P_R_C(tree, g_idx, n_idx, cl_idx, RB_RED);
	RB_SET_P(tree, cl_idx, g_idx);

	/* ...new root of the subtree is N; adjust T pointer */
	goto case5_xx;

case5_ll:

	/***************************************************************************
	 * Case #5: N is LL grandchild of P; N and P red, G and U black
	 **************************************************************************/

	cr_idx = RB_RIGHT(tree, p_idx);
	RB_SET_P_L_C(tree, g_idx, p_idx, cr_idx, RB_RED);
	RB_SET_P(tree, cr_idx, g_idx);
	RB_SET_P_R_C(tree, p_idx, t_idx, g_idx, RB_BLK);

	/* ...new root of the subtree is P; relabel and adjust T pointer */
	n_idx = p_idx;
	goto case5_xx;

case5_rr:

	/***************************************************************************
	 * Case #5: N is RR grandchild of P; N and P red, G and U black
	 **************************************************************************/

	cl_idx = RB_LEFT(tree, p_idx);
	RB_SET_P_R_C(tree, g_idx, p_idx, cl_idx, RB_RED);
	RB_SET_P(tree, cl_idx, g_idx);
	RB_SET_P_L_C(tree, p_idx, t_idx, g_idx, RB_BLK);

	/* ...new root of the subtree is P; relabel and adjust T pointer */
	n_idx = p_idx;
	goto case5_xx;

case5_xx:

	/* ...N is a (black) root of subtree; check if it is a root of tree as well */
    if (t_idx == RB_NULL(tree))
        goto root;
    else if (g_idx == RB_LEFT(tree, t_idx))
        RB_SET_L(tree, t_idx, n_idx);
    else
        RB_SET_R(tree, t_idx, n_idx);
    
    goto done;

case3:

	/***************************************************************************
	 * Case #3 - P and U are red, G is black
	 **************************************************************************/

	RB_SET_C(tree, p_idx, RB_BLK);
	RB_SET_C(tree, u_idx, RB_BLK);
	RB_SET_C(tree, g_idx, RB_RED);

	/* ...rebalance the tree for a G */
	n_idx = g_idx, p_idx = RB_PARENT(tree, g_idx);
	goto rebalance;

root:
	/* ...adjust root pointer of the tree */
	RB_SET_ROOT(tree, n_idx);

done:
	/* ...tree is balanced */
	return;
}

/* ...high-level API function */
void rb_insert(rb_tree_t *tree, rb_idx_t n_idx, rb_idx_t p_idx)
{
    if (p_idx == RB_NULL(tree))
    {
        /* ...set black root node */
		RB_SET_P_L_R_C(tree, n_idx, p_idx, p_idx, p_idx, RB_BLK);

        /* ...tree consists of the only root node; is balanced */
		RB_SET_ROOT(tree, n_idx);
    }
    else
    {
        /* ...create new node - set parent pointer and paint red */
        RB_SET_P_L_R_C(tree, n_idx, p_idx, RB_NULL(tree), RB_NULL(tree), RB_RED);

        /* ...and rebalance the tree */
        __rb_insert_balance(tree, n_idx, p_idx);
    }
}

/*******************************************************************************
 * rb_delete
 *
 * Remove item from RB-key (by key). Returns zero on success
 ******************************************************************************/

/* ...internal tree balancing function */
static void __rb_delete_rebalance(rb_tree_t *tree, rb_idx_t p_idx)
{
	rb_idx_t    n_idx, s_idx, sl_idx, sr_idx, g_idx, c_idx, cl_idx, cr_idx;

    /* ...initialize rebalancing procedure with null-child of P */
    n_idx = RB_NULL(tree);
    
rebalance:

	/* ...save grand-parent pointer (may be null) */
	g_idx = RB_PARENT(tree, p_idx);

	/***************************************************************************
	 * Check for complex cases
	 **************************************************************************/

	if (n_idx == RB_LEFT(tree, p_idx))
    {
		/* ...N is left child; get sibling (must exist) and its children  */
		s_idx = RB_RIGHT(tree, p_idx);
		sl_idx = RB_LEFT(tree, s_idx);
		sr_idx = RB_RIGHT(tree, s_idx);

		/* ...if S is black, test for conditions 3,4,5,6; otherwise - case 2 */
		if (RB_IS_BLACK(tree, s_idx))
			goto test3_l;
		else
			goto case2_l;
	}
    else
    {
		/* ...N is right child; get sibling (must exist) and its children */
		s_idx = RB_LEFT(tree, p_idx);
		sl_idx = RB_LEFT(tree, s_idx);
		sr_idx = RB_RIGHT(tree, s_idx);

		/* ...if S is black, test for conditions 3,4,5,6; otherwise - case 2 */
		if (RB_IS_BLACK(tree, s_idx))
			goto test3_r;
		else
			goto case2_r;
	}

case2_l:

	/***************************************************************************
	 * Case #2: N is a left child of P; S is red
	 **************************************************************************/

	c_idx = sl_idx;
	RB_SET_P_L_C(tree, s_idx, g_idx, p_idx, RB_BLK);
	RB_SET_P_R_C(tree, p_idx, s_idx, c_idx, RB_RED);
	RB_SET_P(tree, c_idx, p_idx);

	/* ...S is new root of subtree, Sl(C) is new sibling of N; update G */
	goto case2_x;

case2_r:

	/***************************************************************************
	 * Case #2: N is a right child of P; S is red
	 **************************************************************************/

	c_idx = sr_idx;
	RB_SET_P_R_C(tree, s_idx, g_idx, p_idx, RB_BLK);
	RB_SET_P_L_C(tree, p_idx, s_idx, c_idx, RB_RED);
	RB_SET_P(tree, c_idx, p_idx);

	/* ...S is new root of subtree, Sr(C) is new sibling of N; update G */
	goto case2_x;

case2_x:

	/* ...check if S is becoming new (black) root of the tree */
    if (g_idx == RB_NULL(tree))
        RB_SET_ROOT(tree, s_idx);
    else if (p_idx == RB_LEFT(tree, g_idx))
        RB_SET_L(tree, g_idx, s_idx);
    else
        RB_SET_R(tree, g_idx, s_idx);

	/* ...relabel new N's grandparent (now S) as G and new sibling (now C) as S	 */
	g_idx = s_idx, s_idx = c_idx;
	sl_idx = RB_LEFT(tree, s_idx);
	sr_idx = RB_RIGHT(tree, s_idx);

	/* ...N is still one of P's children; select proper side */
	if (n_idx == RB_LEFT(tree, p_idx))
		goto test3_l;
	else
		goto test3_r;

test3_l:

	/***************************************************************************
	 * Test for cases 3,4,5,6; P is any, S is black. N is left child of P
	 **************************************************************************/
    
    if (!RB_IS_BLACK(tree, sr_idx))
		/* ...Sr is red, Sl of any color; conditions for case #6 are met */
		goto case6_l;
    else if (!RB_IS_BLACK(tree, sl_idx))
        /* ...Sr is black and Sl is red; conditions for case #5 are met */
        goto case5_l;
    else if (RB_IS_BLACK(tree, p_idx))
        /* ...Sl and Sr are of the same (black) color and P is black */
        goto case3;
    else
        /* ...Sl and Sr are of the same (black) color and P is red */
        goto case4;

test3_r:

	/***************************************************************************
	 * Test for cases 3,4,5,6; P is any, S is black. N is right child of P
	 **************************************************************************/

    if (!RB_IS_BLACK(tree, sl_idx))
		/* ...Sl is red, Sr of any color; conditions for case #6 are met */
		goto case6_r;
    else if (!RB_IS_BLACK(tree, sr_idx))
        /* ...Sl is black and Sr is red; conditions for case #5 are met */
        goto case5_r;
    else if (RB_IS_BLACK(tree, p_idx))
        /* ...Sl and Sr are of the same (black) color and P is black */
        goto case3;
    else
        /* ...Sl and Sr are of the same (black) color and P is red */
        goto case4;

case3:

	/***************************************************************************
	 * Case #3: N, P, S, Sl and Sr are black
	 **************************************************************************/

	RB_SET_C(tree, s_idx, RB_RED);

	/* ...and rebalance the tree for parent */
	n_idx = p_idx, p_idx = RB_PARENT(tree, p_idx);

	if (p_idx == RB_NULL(tree))
		goto done;
	else
		goto rebalance;

case4:

	/***************************************************************************
	 * Case #4: N and S are black, P is red, Sl and Sr are all black
	 **************************************************************************/

	RB_SET_C(tree, s_idx, RB_RED);
	RB_SET_C(tree, p_idx, RB_BLK);

	goto done;

case5_l:
	/***************************************************************************
	 * Case #5: S is black, Sl is red, Sr is black; N is left child of P. We
	 * have two subsequent transformations (case #5 and case #6) combined
	 **************************************************************************/

	cl_idx = RB_LEFT(tree, sl_idx);
	cr_idx = RB_RIGHT(tree, sl_idx);

	if (RB_IS_BLACK(tree, p_idx))
		RB_SET_P_L_R_C(tree, sl_idx, g_idx, p_idx, s_idx, RB_BLK);
	else
		RB_SET_P_L_R_C(tree, sl_idx, g_idx, p_idx, s_idx, RB_RED);

	RB_SET_P_R_C(tree, p_idx, sl_idx, cl_idx, RB_BLK);
	RB_SET_P(tree, cl_idx, p_idx);
	RB_SET_P_L(tree, s_idx, sl_idx, cr_idx);
	RB_SET_P(tree, cr_idx, s_idx);

	/* ...relabel new root as S (for common processing in case #6) */
	s_idx = sl_idx;
	goto case6_x;

case5_r:
	/***************************************************************************
	 * Case #5: S is black, Sr is red, Sl is black; N is right child of P. We
	 * have two subsequent transformations (case #5 and case #6) combined
	 **************************************************************************/

	cl_idx = RB_LEFT(tree, sr_idx);
	cr_idx = RB_RIGHT(tree, sr_idx);

	if (RB_IS_BLACK(tree, p_idx))
		RB_SET_P_L_R_C(tree, sr_idx, g_idx, s_idx, p_idx, RB_BLK);
	else
		RB_SET_P_L_R_C(tree, sr_idx, g_idx, s_idx, p_idx, RB_RED);

	RB_SET_P_L_C(tree, p_idx, sr_idx, cr_idx, RB_BLK);
	RB_SET_P(tree, cr_idx, p_idx);
	RB_SET_P_R(tree, s_idx, sr_idx, cl_idx);
	RB_SET_P(tree, cl_idx, s_idx);

	/* ...relabel new root as S (for common processing in case #6) */
	s_idx = sr_idx;
	goto case6_x;

case6_l:

	/***************************************************************************
	 * Case #6: S is black, N is the left child of P, Sr is red
	 **************************************************************************/

	if (RB_IS_BLACK(tree, p_idx))
		RB_SET_P_L(tree, s_idx, g_idx, p_idx);
	else
		RB_SET_P_L_C(tree, s_idx, g_idx, p_idx, RB_RED);

	RB_SET_P_R_C(tree, p_idx, s_idx, sl_idx, RB_BLK);
	RB_SET_P(tree, sl_idx, p_idx);
	RB_SET_C(tree, sr_idx, RB_BLK);

	/* ...S is a new root of subtree; update G */
	goto case6_x;

case6_r:

	/***************************************************************************
	 * Case #6: S is black, N is the right child of P, Sl is red
	 **************************************************************************/

	if (RB_IS_BLACK(tree, p_idx))
		RB_SET_P_R(tree, s_idx, g_idx, p_idx);
	else
		RB_SET_P_R_C(tree, s_idx, g_idx, p_idx, RB_RED);

	RB_SET_P_L_C(tree, p_idx, s_idx, sr_idx, RB_BLK);
	RB_SET_P(tree, sr_idx, p_idx);
	RB_SET_C(tree, sl_idx, RB_BLK);

	/* ...S is a new root of subtree; update G */
	goto case6_x;

case6_x:

	/* ...S is a new root of subtree; update G's pointer */
    if (g_idx == RB_NULL(tree))
		RB_SET_ROOT(tree, s_idx);
    else if (p_idx == RB_LEFT(tree, g_idx))
        RB_SET_L(tree, g_idx, s_idx);
    else
        RB_SET_R(tree, g_idx, s_idx);

	/* ...tree is balanced; pass through */

done:

	return;
}

/* ...high-level API function */
rb_idx_t rb_delete(rb_tree_t *tree, rb_idx_t n_idx)
{
	rb_idx_t    p_idx, t_idx, m_idx, c_idx, l_idx, r_idx, k_idx;
    u32         color;
    
    /* ...save parent of element N that we are going to remove */
    p_idx = RB_PARENT(tree, n_idx);

	/* ...get in-order predecessor/successor of n_idx, if possible */
	if ((m_idx = RB_LEFT(tree, n_idx)) != RB_NULL(tree))
    {
		while ((t_idx = RB_RIGHT(tree, m_idx)) != RB_NULL(tree))
			m_idx = t_idx;

        /* ...set the child of in-order predecessor (may be null) */
		c_idx = RB_LEFT(tree, m_idx);   
	}
    else if ((m_idx = RB_RIGHT(tree, n_idx)) != RB_NULL(tree))
    {
		while ((t_idx = RB_LEFT(tree, m_idx)) != RB_NULL(tree))
			m_idx = t_idx;

        /* ...set the child of in-order successor (may be null) */
		c_idx = RB_RIGHT(tree, m_idx);
	}
    else if (p_idx == RB_NULL(tree))
    {
        /* ...tree consists of the only root node N that we are removing */
        RB_SET_ROOT(tree, m_idx);

        /* ..return tree null pointer */
        return m_idx;
    }
    else
    {
        /* ...N is a (non-root) leaf node; M and C are null */
		c_idx = m_idx;

        /* ...save the color of the node we are going to delete */
        color = RB_COLOR(tree, n_idx);
                        
        /* ...set new parent of C */
        t_idx = p_idx;
        
        /* ...pointer that we return as in-order predecessor/successor */
        k_idx = p_idx;
        
        /* ...adjust only parent of the N */
        goto adjust_parent;
    }

    /* ...node that replaces our component is M */
    k_idx = m_idx;
    
	/***************************************************************************
	 * Replace node N with M
	 **************************************************************************/

    /* ...save original color of M (the node that we are deleting) */
    color = RB_COLOR(tree, m_idx);    

    /* ...put M in place of N; get N's children */
    l_idx = RB_LEFT(tree, n_idx);
    r_idx = RB_RIGHT(tree, n_idx);

    /* ...see if M is a child of N */
    if ((t_idx = RB_PARENT(tree, m_idx)) != n_idx)
    {
        /* ...C becomes left or right child of M's original parent T */
        if (c_idx == RB_LEFT(tree, m_idx))
            RB_SET_R(tree, t_idx, c_idx);
        else
            RB_SET_L(tree, t_idx, c_idx);
        
        /* ...adjust C parent pointer (okay if it's null)  */
        RB_SET_P(tree, c_idx, t_idx);

        /* ...set all pointers of node M (it replaces N) */
        RB_SET_P_L_R(tree, m_idx, p_idx, l_idx, r_idx);
        RB_SET_P(tree, l_idx, m_idx);
        RB_SET_P(tree, r_idx, m_idx);
    }
    else
    {
        /* ...M is a left or right child of N; it gets to N's place, and C remains intact */
        if (m_idx == l_idx)
        {
            RB_SET_P_R(tree, m_idx, p_idx, r_idx);
            RB_SET_P(tree, r_idx, m_idx);
        }
        else
        {
            RB_SET_P_L(tree, m_idx, p_idx, l_idx);
            RB_SET_P(tree, l_idx, m_idx);
        }

        /* ...parent of C is still M (we label it as T) */
        t_idx = m_idx;
    }

    /* ...paint M in the same color as N which it replaced */
    if (RB_IS_BLACK(tree, n_idx))
        RB_SET_C(tree, m_idx, RB_BLK);
    else
        RB_SET_C(tree, m_idx, RB_RED);
    
adjust_parent:

    /* ...adjust N's parent node to point to M */
    if (p_idx == RB_NULL(tree))
        RB_SET_ROOT(tree, m_idx);
    else if (n_idx == RB_LEFT(tree, p_idx))
        RB_SET_L(tree, p_idx, m_idx);
    else
        RB_SET_R(tree, p_idx, m_idx);

	/* ...check for a color of deleted item (M or N in case it is a leaf) */
	if (color == RB_BLK)
    {
		if (c_idx == RB_NULL(tree))
            /* ...rebalance the tree for a T node (it is never a null)*/
            __rb_delete_rebalance(tree, t_idx);
		else
            /* ...C node exists and is necessarily red; repaint it black */
            RB_SET_C(tree, c_idx, RB_BLK);
	}

    /* ....return the node K which replaced deleted node N */
    return k_idx;
}

/*******************************************************************************
 * rb_replace
 *
 * Replace the node with the same-key node - adjust tree pointers
 ******************************************************************************/

void rb_replace(rb_tree_t *tree, rb_idx_t n_idx, rb_idx_t t_idx)
{
    rb_idx_t    p_idx, l_idx, r_idx;
    
    /* ...get node pointers */
    p_idx = RB_PARENT(tree, n_idx), l_idx = RB_LEFT(tree, n_idx), r_idx = RB_RIGHT(tree, n_idx);

    /* ...set new node pointers */
    RB_SET_P_L_R(tree, t_idx, p_idx, l_idx, r_idx);
    
    /* ...set node color */
    if (RB_IS_BLACK(tree, n_idx))
        RB_SET_C(tree, t_idx, RB_BLK);
    else
        RB_SET_C(tree, t_idx, RB_RED);

    /* ...update parent node */
    if (p_idx == RB_NULL(tree))
        RB_SET_ROOT(tree, t_idx);
    else if (n_idx == RB_LEFT(tree, p_idx))
        RB_SET_L(tree, p_idx, t_idx);
    else
        RB_SET_R(tree, p_idx, t_idx);
    
    /* ...update children's parent node (okay if null) */
    RB_SET_P(tree, l_idx, t_idx), RB_SET_P(tree, r_idx, t_idx); 
}