Source

rbtree / src / rbtree_impl.c

The default branch has multiple heads

Full commit
  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
#include "rbtree.h"
#include <Python.h>
#include <string.h>
#include <assert.h>

#define NEW(x,t)       {                        \
        x = (t *) PyMem_Malloc(sizeof(t));      \
        if (x == NULL) return 0;                \
    }

#define DEL(x)           {                      \
        if (x != NULL)                          \
            {                                   \
                PyMem_Free(x);                  \
                x = NULL;                       \
            }                                   \
    }



#define COMPARE(T, X, Y) ((T->compare) == Py_None ?         \
                          T->cmp_func(X, Y) :               \
                          rbtree_pycmp(T->compare, X, Y))



static
int
rbtree_pycmp(PyObject *compare, PyObject *x, PyObject *y)
{
    // Invoke a Python compare function returning the result as an int.
    PyObject *res;
    PyObject *args;
    int i;
    args = PyTuple_New(2);
    if (args == NULL)
        return -1;
    Py_INCREF(x);
    Py_INCREF(y);
    PyTuple_SET_ITEM(args, 0, x);
    PyTuple_SET_ITEM(args, 1, y);
    res = PyObject_Call(compare, args, NULL);
    Py_DECREF(args);
    if (res == NULL) return -1;
    if (!PyInt_Check(res)) {
        Py_DECREF(res);
        PyErr_SetString(PyExc_TypeError, "comparison function must return int");
        return -1;
    }
    i = PyInt_AsLong(res);
    Py_DECREF(res);
    return i;

}

static
int
rbtree_node_compare(PyObject *x, PyObject *y)
{
    int gt, lt;
    /* a three way compare that should work with whatever objects support */
    gt = PyObject_RichCompareBool(x, y, Py_GT);
    if (gt == 1) return 1;
    lt = PyObject_RichCompareBool(x, y, Py_LT);
    if (lt == 1)  return -1;
    return 0;
}

static
int
rbtree_node_compare_string(PyObject *x, PyObject *y)
{
    return strcmp((char*)PyString_AS_STRING(x),
                  (char*)PyString_AS_STRING(y));
}

static
void
__rotate_left     (rbtree_t *T, rbtree_node_t *x)
{
    rbtree_node_t  *y = x->r;

    x->r      = y->l;
    if (x->r != T->nil)
        {
            x->r->p = x;
        }
    y->p      = x->p;

    if (y->p == T->nil)
        {
            T->root = y;
            goto gc;
        }
    if (y->p->l == x)
        {
            y->p->l = y;
            goto gc;
        }
    {
        y->p->r = y;
    }

 gc:
    y->l    = x;
    x->p    = y;
}

static
void
__rotate_right    (rbtree_t *T, rbtree_node_t *x)
{
    rbtree_node_t  *y = x->l;

    x->l      = y->r;
    if (x->l != T->nil)
        {
            x->l->p = x;
        }
    y->p      = x->p;

    if (y->p == T->nil)
        {
            T->root = y;
            goto gc;
        }
    if (y->p->l == x)
        {
            y->p->l = y;
            goto gc;
        }
    {
        y->p->r = y;
    }

 gc:
    y->r    = x;
    x->p    = y;
}

static
rbtree_node_t *
__tree_min        (rbtree_t *T, rbtree_node_t *x)
{
    while (x->l != T->nil)
        {
            x = x->l;
        }

    return x;
}

static
rbtree_node_t *
__tree_max        (rbtree_t *T, rbtree_node_t *x)
{
    while (x->r != T->nil)
        {
            x = x->r;
        }

    return x;
}

static
rbtree_node_t *
__tree_successor  (rbtree_t *T, rbtree_node_t *x)
{
    rbtree_node_t *y;

    if (x->r != T->nil)
        {
            return __tree_min(T, x->r);
        }

    for (y = x->p; (y != T->nil) && (x == y->r); )
        {
            x = y;
            y = y->p;
        }

    return y;
}


static
rbtree_node_t *
__tree_predecessor(rbtree_t *T, rbtree_node_t *x)
{
    rbtree_node_t *y;

    if (x->l != T->nil)
        {
            return __tree_max(T, x->l);
        }

    for (y = x->p; (y != T->nil) && (x == y->l); )
        {
            x = y;
            y = y->p;
        }

    return y;
}

static
rbtree_node_t *
__tree_search     (rbtree_t *T, rbtree_node_t *x, const PyObject *k, int *rc)
{
    rbtree_node_t *y = T->nil;

    for ((*rc) = -1; (x != T->nil); )
        {
            (*rc) = COMPARE(T, (PyObject *)k, x->key);
            y     = x;

            if ((*rc) == 0)
                return x;
            if ((*rc)  < 0) x = x->l;
            else x = x->r;
        }

    return ((*rc) < 0) ? y : __tree_successor(T, y);
}


static
rbtree_node_t *
__tree_insert     (rbtree_t *T, PyObject *k, PyObject *v)
{
    rbtree_node_t *x = T->root;
    rbtree_node_t *y = T->nil;
    int            rc = 0;

    // Use the optimized function until we can't
    if (!PyString_CheckExact(k)) {
        T->cmp_func = rbtree_node_compare;
    }

    while (x != T->nil)
        {
            rc = COMPARE(T, k, x->key);
            y  = x;

            if (rc == 0)
                {
                    // force a value update
                    // XXX: (later) validate this?
                    Py_XDECREF(x->key);
                    Py_XDECREF(x->value);
                    x->key = k;
                    x->value = v;
                    return 0;
                }
            if (rc  < 0) x = x->l;
            else x = x->r;
        }

    NEW(x, rbtree_node_t);
    x->key = k;
    x->value = v;
    x->p   = y;
    x->l   = T->nil;
    x->r   = T->nil;
    MAKE_BLACK(x);
    T->ct++;


    if (y == T->nil)
        {
            T->root = x;
            return x;
        }

    if (rc < 0)
        {
            y->l = x;
            return x;
        }

    {
        y->r = x;
        return x;
    }
}

static
int
__rb_insert (rbtree_t *T, PyObject *k, PyObject *v)
{
    rbtree_node_t *x = __tree_insert(T, k, v);
    rbtree_node_t *y;

    if (x ==  NULL) {
        return 0;
    }

    Py_INCREF(k);
    Py_INCREF(v);


    if (x)
        {
            MAKE_RED  (x);

            while ((x != T->root) && (COLOR(x->p) == RED))
                {
                    if (x->p == x->p->p->l)
                        {
                            y = x->p->p->r;
                            if (COLOR(y) == RED)
                                {
                                    MAKE_BLACK(x->p);
                                    MAKE_BLACK(y);
                                    MAKE_RED  (x->p->p);
                                    x = x->p->p;
                                }
                            else
                                {
                                    if (x == x->p->r)
                                        {
                                            x = x->p;
                                            __rotate_left(T, x);
                                        }
                                    MAKE_BLACK(x->p);
                                    MAKE_RED  (x->p->p);
                                    __rotate_right(T, x->p->p);
                                    break;
                                }
                        }
                    else
                        {
                            y = x->p->p->l;
                            if (COLOR(y) == RED)
                                {
                                    MAKE_BLACK(x->p);
                                    MAKE_BLACK(y);
                                    MAKE_RED  (x->p->p);
                                    x = x->p->p;
                                }
                            else
                                {
                                    if (x == x->p->l)
                                        {
                                            x = x->p;
                                            __rotate_right(T, x);
                                        }
                                    MAKE_BLACK(x->p);
                                    MAKE_RED  (x->p->p);
                                    __rotate_left(T, x->p->p);
                                    break;
                                }
                        }
                }

            MAKE_BLACK(T->root);
            return 0;
        }
    return 1;
}

static
void
__rb_del_fix(rbtree_t *T, rbtree_node_t *x)
{
    rbtree_node_t *w;

    while ((x != T->root) && (COLOR(x) == BLACK))
        {
            if (x == x->p->l)
                {
                    w = x->p->r;
                    if (COLOR(w) == RED)
                        {
                            MAKE_BLACK(w);
                            MAKE_RED  (x->p);
                            __rotate_left(T, x->p);
                            w = x->p->r;
                        }
                    if ((COLOR(w->l) == BLACK) && (COLOR(w->r) == BLACK))
                        {
                            MAKE_RED(w);
                            x = x->p;
                        }
                    else
                        {
                            if (COLOR(w->r) == BLACK)
                                {
                                    MAKE_BLACK(w->l);
                                    MAKE_RED  (w);
                                    __rotate_right(T, w);
                                    w = x->p->r;
                                }

                            w->color = COLOR(x->p);

                            MAKE_BLACK(x->p);
                            MAKE_BLACK(w->r);
                            __rotate_left(T, x->p);
                            x = T->root;
                            break;
                        }
                }
            else
                {
                    w = x->p->l;
                    if (COLOR(w) == RED)
                        {
                            MAKE_BLACK(w);
                            MAKE_RED  (x->p);
                            __rotate_right(T, x->p);
                            w = x->p->l;
                        }
                    if ((COLOR(w->l) == BLACK) && (COLOR(w->r) == BLACK))
                        {
                            MAKE_RED(w);
                            x = x->p;
                        }
                    else
                        {
                            if (COLOR(w->l) == BLACK)
                                {
                                    MAKE_BLACK(w->r);
                                    MAKE_RED  (w);
                                    __rotate_left(T, w);
                                    w = x->p->l;
                                }
                            w->color = COLOR(x->p);
                            MAKE_BLACK(x->p);
                            MAKE_BLACK(w->l);
                            __rotate_right(T, x->p);
                            x = T->root;
                            break;
                        }
                }
        }

    MAKE_BLACK(x);
}

static
rbtree_node_t *
__rb_del_node(rbtree_t *T, rbtree_node_t *z)
{
    // Delete a node and return successor
    rbtree_node_t  *y;
    rbtree_node_t  *x;
    rbtree_node_t  *remainder = NULL;

    if (z == NULL) return NULL;
    remainder = __tree_successor(T, z);
    if ((z->l == T->nil) || (z->r == T->nil)) y = z;
    else y = remainder;

    if (y->l != T->nil) x = y->l;
    else                x = y->r;

    x->p = y->p;
    if (y->p == T->nil) T->root = x;
    else
        {
            // Disconnect y from tree
            if (y == y->p->l) y->p->l = x;
            else              y->p->r = x;
        }
    if (y != z)
        {
            // copy y to z replace z, the delete target with y's data, then
            // remove the old shell. saves a rotate.
            Py_DECREF(z->key);
            Py_DECREF(z->value);
            z->key = y->key;
            z->value = y->value;
            remainder = z;
        }

    if (COLOR(y) == BLACK)
        {
            __rb_del_fix(T, x);
        }

    DEL(y);

    T->ct--;
    return remainder;
}



static
int
__rb_del    (rbtree_t *T, PyObject *k)
{
    rbtree_node_t  *z;
    int             rc;

    z = __tree_search(T, T->root, k, &rc);
    if (rc == 0) {
        __rb_del_node(T, z);
        return 0;
    }
    return 1;
}


rbtree_node_t *
rbtree_node_del(rbtree_t *T, rbtree_node_t *n) {
    if (n == NULL) return 0;
    if (n == T->nil) return 0;
    return __rb_del_node(T, n);
}


rbtree_t *
rbtree_alloc           (void)
{
    rbtree_t       *l_rbt;

    NEW(l_rbt, rbtree_t);
    return l_rbt;
}

int
rbtree_init            (rbtree_t *T)
{
    NEW(T->nil, rbtree_node_t);
    memset(T->nil, 0, sizeof(rbtree_node_t));
    MAKE_BLACK(T->nil);
    T->nil->l = T->nil;
    T->nil->r = T->nil;
    T->nil->p = T->nil;
    T->root   = T->nil;
    T->ct     = 0;
    T->cmp_func = rbtree_node_compare_string;
    T->compare = Py_None;
    Py_INCREF(Py_None);

    return 0;
}


void
rbtree_set_compare(rbtree_t *T, PyObject *compare)
{
    if (T->ct == 0) {
        // We can only set the compare function when the tree is empty
        Py_DECREF(T->compare);
        T->compare = compare;
        Py_INCREF(T->compare);
    }
}

PyObject *
rbtree_get_compare(rbtree_t *T)
{
    return T->compare;
}

static
void
__destroy              (rbtree_t *T, rbtree_node_t *n)
{
    if (n != T->nil)
        {
            Py_DECREF(n->key);
            Py_DECREF(n->value);
            __destroy(T, n->l);
            __destroy(T, n->r);
            DEL      (n);
        }
}

void
rbtree_dealloc(rbtree_t *T)
{
    __destroy(T, T->root);
    Py_XDECREF(T->compare);
    DEL(T->nil);
}


void
rbtree_free            (rbtree_t *T)
{
    DEL(T);
}


int
rbtree_add             (rbtree_t *T, PyObject *a_ptr, PyObject *value)
{
    return __rb_insert(T, a_ptr, value);
}

int
rbtree_del             (rbtree_t *T, PyObject *a_ptr)
{
    return __rb_del   (T, a_ptr);
}

void *
rbtree_get             (rbtree_t *T, PyObject *key)
{
    rbtree_node_t  *x;
    int             rc;

    x = __tree_search(T, T->root, key, &rc);

    return (rc != 0) ? NULL : (x->value);
}

rbtree_node_t *
rbtree_node_get (rbtree_t *T, PyObject *key)
{
    rbtree_node_t  *x;
    int             rc;

    x = __tree_search(T, T->root, key, &rc);

    return (rc != 0) ? NULL : x;
}

rbtree_node_t *
tree_min(rbtree_t *T, rbtree_node_t *x) {
    rbtree_node_t *n;
    if (x == NULL) x = T->root;
    n = __tree_min(T, x);
    if (n == T->nil) n = NULL;
    return n;
}

rbtree_node_t *
tree_max(rbtree_t *T, rbtree_node_t *x) {
    rbtree_node_t *n;
    if (x == NULL) x = T->root;
    n = __tree_max(T, x);
    if (n == T->nil) n = NULL;
    return n;
}

rbtree_node_t *
tree_successor(rbtree_t *T, rbtree_node_t *x) {
    rbtree_node_t *n = __tree_successor(T, x);
    if (n == T->nil) n = NULL;
    return n;
}

rbtree_node_t *
tree_predecessor(rbtree_t *T, rbtree_node_t *x) {
    rbtree_node_t *n = __tree_predecessor(T, x);
    if (n == T->nil) n = NULL;
    return n;
}


#define ABS( a )     ( (a) < 0 ? -(a) : (a) )

void rbtree_do_slice(rbtree_t *T, PyObject *start, PyObject *end, PyObject *stride, rbtree_t *new) {
    // return a new rbtree object built from the key slice. Because keys
    // are ordered we slice on the actual keys and not their
    // index/offsets
    rbtree_node_t *sn, *en, *cursor;
    long step = 0;
    long offset = 0;
    int rc = 0;

    // Look for the nearest inclusive match
    if (start == Py_None)
        sn = tree_min(T, NULL);
    else
        sn = __tree_search(T, T->root, start, &rc);

    if (end == Py_None)
        en = T->nil;
    else
        en = __tree_search(T, T->root, end, &rc);

    if (stride != Py_None) {
        step = PyInt_AsLong(stride);
        // negative step makes no sense when keys are added via a cmp function
        if (step < 0) step = ABS(step);
    }

    for(cursor = sn; cursor!= en; offset++) {
        if (step == 0 || (offset % step == 0))
            rbtree_add(new, cursor->key, cursor->value);
        cursor = __tree_successor(T, cursor);
    }
}

void rbtree_do_del_slice(rbtree_t *T, PyObject *start, PyObject *end,
                         PyObject *stride)
{
    rbtree_node_t *sn, *en, *cursor;
    long step = 0;
    long offset = 0;
    int rc = 0;

    // Look for the nearest inclusive match
    if (start == Py_None)
        sn = tree_min(T, NULL);
    else
        sn = __tree_search(T, T->root, start, &rc);

    if (end == Py_None)
        en = T->nil;
    else
        en = __tree_search(T, T->root, end, &rc);

    if (stride != Py_None) {
        step = PyInt_AsLong(stride);
        // negative step makes no sense when keys are added via a cmp function
        if (step < 0) step = ABS(step);
    }

    for(cursor = sn; cursor!= en; offset++) {
        if (step == 0 || (offset % step == 0)) {
            cursor = rbtree_node_del(T, cursor);
        } else {
            cursor = __tree_successor(T, cursor);
        }
        if (cursor == T->nil) break;
    }

}