-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathcc_treetable.c
1001 lines (865 loc) · 27.1 KB
/
cc_treetable.c
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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Collections-C
* Copyright (C) 2013-2015 Srđan Panić <[email protected]>
*
* This file is part of Collections-C.
*
* Collections-C is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Collections-C 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Collections-C. If not, see <http://www.gnu.org/licenses/>.
*/
/* Tree operations are based on CLRS RB Tree. */
#include "cc_treetable.h"
#define RB_BLACK 1
#define RB_RED 0
struct cc_treetable_s {
RBNode *root;
RBNode *sentinel;
size_t size;
int (*cmp) (const void *k1, const void *k2);
void *(*mem_alloc) (size_t size);
void *(*mem_calloc) (size_t blocks, size_t size);
void (*mem_free) (void *block);
};
static void rotate_left (CC_TreeTable *table, RBNode *n);
static void rotate_right (CC_TreeTable *table, RBNode *n);
static void rebalance_after_insert (CC_TreeTable *table, RBNode *n);
static void rebalance_after_delete (CC_TreeTable *table, RBNode *n);
static void remove_node (CC_TreeTable *table, RBNode *z);
static void tree_destroy (CC_TreeTable *table, RBNode *s);
static INLINE void transplant (CC_TreeTable *table, RBNode *u, RBNode *v);
static INLINE RBNode *tree_min (CC_TreeTable const * const table, RBNode *n);
static INLINE RBNode *tree_max (CC_TreeTable const * const table, RBNode *n);
static RBNode *get_tree_node_by_key(CC_TreeTable const * const table, const void *key);
static RBNode *get_successor_node (CC_TreeTable const * const table, RBNode *x);
static RBNode *get_predecessor_node(CC_TreeTable const * const table, RBNode *x);
/**
* Initializes the TreehTableConf structs fields to default values.
*
* @param[in] conf the struct that is being initialized
*/
void cc_treetable_conf_init(CC_TreeTableConf *conf)
{
conf->mem_alloc = malloc;
conf->mem_calloc = calloc;
conf->mem_free = free;
conf->cmp = NULL;
}
/**
* Creates a new CC_TreeTable and returns a status code.
*
* @param[in] cmp the comparator used to order keys within the table
* @param[out] out Pointer to where the newly created CC_TreeTable is to be stored
*
* @return CC_OK if the creation was successful, or CC_ERR_ALLOC if the memory
* allocation for the new CC_TreeTable failed.
*/
enum cc_stat cc_treetable_new(int (*cmp) (const void*, const void*), CC_TreeTable **tt)
{
CC_TreeTableConf conf;
cc_treetable_conf_init(&conf);
conf.cmp = cmp;
return cc_treetable_new_conf(&conf, tt);
}
/**
* Creates a new CC_TreeTable based on the specified CC_TreeTableConf struct and returns
* a status code.
*
* The table is allocated using the memory allocators specified in the CC_TreeTableConf
* struct.
*
* @param[in] conf the CC_TreeTableConf struct used to configure this new CC_TreeTable
* @param[out] out Pointer to where the newly created CC_TreeTable is stored
*
* @return CC_OK if the creation was successful, or CC_ERR_ALLOC if the memory
* allocation for the new CC_TreeTable structure failed.
*/
enum cc_stat cc_treetable_new_conf(CC_TreeTableConf const * const conf, CC_TreeTable **tt)
{
CC_TreeTable *table = conf->mem_calloc(1, sizeof(CC_TreeTable));
if (!table)
return CC_ERR_ALLOC;
RBNode *sentinel = conf->mem_calloc(1, sizeof(RBNode));
if (!sentinel) {
conf->mem_free(table);
return CC_ERR_ALLOC;
}
sentinel->color = RB_BLACK;
table->size = 0;
table->cmp = conf->cmp;
table->mem_alloc = conf->mem_alloc;
table->mem_calloc = conf->mem_calloc;
table->mem_free = conf->mem_free;
table->root = sentinel;
table->sentinel = sentinel;
*tt = table;
return CC_OK;
}
/**
* Destroys the sub-tree specified by the root node n.
*
* @param[in] table CC_TreeTable to be destroyed.
* @param[in] n root node of the sub tree that is being destroyed
*/
static void tree_destroy(CC_TreeTable *table, RBNode *n)
{
if (n == table->sentinel)
return;
tree_destroy(table, n->left);
tree_destroy(table, n->right);
table->mem_free(n);
}
/**
* Destroys the specified CC_TreeTable structure without destroying the the data
* it holds. In other words the keys and the values are not freed, only the
* table structure is.
*
* @param[in] table CC_TreeTable to be destroyed.
*/
void cc_treetable_destroy(CC_TreeTable *table)
{
tree_destroy(table, table->root);
table->mem_free(table->sentinel);
table->mem_free(table);
}
/**
* Gets a value associated with the specified key and sets the out
* parameter to it.
*
* @param[in] table the table from which the mapping is being returned
* @param[in] key the key that is being looked up
* @param[out] out Pointer to where the returned value is stored
*
* @return CC_OK if the key was found, or CC_ERR_KEY_NOT_FOUND if not.
*/
enum cc_stat cc_treetable_get(CC_TreeTable const * const table, const void *key, void **out)
{
RBNode *node = get_tree_node_by_key(table, key);
if (!node)
return CC_ERR_KEY_NOT_FOUND;
*out = node->value;
return CC_OK;
}
/**
* Gets the value associated with the first (lowest) key in the table
* and sets the out parameter to it.
*
* @param[in] table the table in which the lookup is performed
* @param[out] out Pointer to where the returned value is stored
*
* @return CC_OK if the key was found, or CC_ERR_VALUE_NOT_FOUND if not.
*/
enum cc_stat cc_treetable_get_first_value(CC_TreeTable const * const table, void **out)
{
RBNode *node = tree_min(table, table->root);
if (node != table->sentinel) {
*out = node->value;
return CC_OK;
}
return CC_ERR_VALUE_NOT_FOUND;
}
/**
* Gets the value associated with the highest (last) key in the table
* and sets the out parameter to it.
*
* @param[in] table the table in which the lookup is performed
* @param[out] out Pointer to where the returned value is stored
*
* @return CC_OK if the key was found, or CC_ERR_VALUE_NOT_FOUND if not.
*/
enum cc_stat cc_treetable_get_last_value(CC_TreeTable const * const table, void **out)
{
RBNode *node = tree_max(table, table->root);
if (node != table->sentinel) {
*out = node->value;
return CC_OK;
}
return CC_ERR_VALUE_NOT_FOUND;
}
/**
* Returns the first (lowest) key in the table and sets the out parameter
* to it.
*
* @param[in] table the table in which the lookup is performed
* @param[out] out Pointer to where the returned key is stored
*
* @return CC_OK if the key was found, or CC_ERR_KEY_NOT_FOUND if not.
*/
enum cc_stat cc_treetable_get_first_key(CC_TreeTable const * const table, void **out)
{
RBNode *node = tree_min(table, table->root);
if (node != table->sentinel) {
*out = node->key;
return CC_OK;
}
return CC_ERR_KEY_NOT_FOUND;
}
/**
* Returns the last (highest) key in the table and sets the out parameter
* to it.
*
* @param[in] table the table in which the lookup is performed
* @param[out] out Pointer to where the returned key is stored
*
* @return CC_OK if the key was found, or CC_ERR_KEY_NOT_FOUND if not.
*/
enum cc_stat cc_treetable_get_last_key(CC_TreeTable const * const table, void **out)
{
RBNode *node = tree_max(table, table->root);
if (node != table->sentinel) {
*out = node->key;
return CC_OK;
}
return CC_ERR_KEY_NOT_FOUND;
}
/**
* Gets the immediate successor of the specified key and sets the out
* parameter to its value.
*
* @param[in] table the table into which the lookup is performed
* @param[in] key the key whose successor is being returned
* @param[out] out Pointer to where the returned key is stored
*
* @return CC_OK if the key was found, or CC_ERR_KEY_NOT_FOUND if not.
*/
enum cc_stat cc_treetable_get_greater_than(CC_TreeTable const * const table, const void *key, void **out)
{
RBNode *n = get_tree_node_by_key(table, key);
RBNode *s = get_successor_node(table, n);
if (n && s) {
*out = s->key;
return CC_OK;
}
return CC_ERR_KEY_NOT_FOUND;
}
/**
* Returns the immediate predecessor of the specified key and sets the
* out parameter to its value.
*
* @param[in] table the table into which the lookup is performed
* @param[in] key the key whose predecessor is being returned
* @param[out] out Pointer to where the returned key is stored
*
* @return CC_OK if the key was found, or CC_ERR_KEY_NOT_FOUND if not.
*/
enum cc_stat cc_treetable_get_lesser_than(CC_TreeTable const * const table, const void *key, void **out)
{
RBNode *n = get_tree_node_by_key(table, key);
RBNode *s = get_predecessor_node(table, n);
if (n && s) {
*out = s->key;
return CC_OK;
}
return CC_ERR_KEY_NOT_FOUND;
}
/**
* Returns the size of the specified CC_TreeTable. Size of a CC_TreeTable represents
* the number of key-value mappings within the table.
*
* @param[in] table the table whose size is being returned
*
* @return the size of the table
*/
size_t cc_treetable_size(CC_TreeTable const * const table)
{
return table->size;
}
/**
* Checks whether or not the CC_TreeTable contains the specified key.
*
* @param[in] table the table into which the lookup is performed
* @param[in] key the key that is being looked up
*
* @return true if the table contains the key.
*/
bool cc_treetable_contains_key(CC_TreeTable const * const table, const void *key)
{
RBNode *node = get_tree_node_by_key(table, key);
if (node)
return true;
return false;
}
/**
* Checks whether or not the CC_TreeTable contains the specified value.
*
* @param[in] table the table into which the lookup is performed
* @param[in] value the value that is being looked up
*
* @return number of occurrences of the specified value.
*/
size_t cc_treetable_contains_value(CC_TreeTable const * const table, const void *value)
{
RBNode *node = tree_min(table, table->root);
size_t o = 0;
while (node != table->sentinel) {
if (node->value == value)
o++;
node = get_successor_node(table, node);
}
return o;
}
/**
* Creates a new key-value mapping in the specified CC_TreeTable. If the unique key
* is already mapped to a value in this table, that value is replaced with the
* new value. This operation may fail if the space allocation for the new entry
* fails.
*
* @param[in] table the table to which this new key-value mapping is being added
* @param[in] key a tree table key used to access the specified value
* @param[in] val a value that is being stored in the table
*
* @return CC_OK if the operation was successful, or CC_ERR_ALLOC if the memory
* allocation for the new element failed.
*/
enum cc_stat cc_treetable_add(CC_TreeTable *table, void *key, void *val)
{
RBNode *y = table->sentinel;
RBNode *x = table->root;
int cmp;
while (x != table->sentinel) {
cmp = table->cmp(key, x->key);
y = x;
if (cmp < 0) {
x = x->left;
} else if (cmp > 0) {
x = x->right;
} else {
x->value = val;
return CC_OK;
}
}
RBNode *n = table->mem_alloc(sizeof(RBNode));
if (!n)
return CC_ERR_ALLOC;
n->value = val;
n->key = key;
n->parent = y;
n->left = table->sentinel;
n->right = table->sentinel;
table->size++;
if (y == table->sentinel) {
table->root = n;
n->color = RB_BLACK;
} else {
n->color = RB_RED;
if (table->cmp(key, y->key) < 0) {
y->left = n;
} else {
y->right = n;
}
rebalance_after_insert(table, n);
}
return CC_OK;
}
/**
* Rebalances the tale after an insert.
*
* @param[in] table CC_TreeTable that is being rebalanced
* @param[in] z Node that was inserted
*/
static void rebalance_after_insert(CC_TreeTable *table, RBNode *z)
{
RBNode *y;
while (z->parent->color == RB_RED) {
if (z->parent == z->parent->parent->left) {
y = z->parent->parent->right;
if (y->color == RB_RED) {
z->parent->color = RB_BLACK;
y->color = RB_BLACK;
z->parent->parent->color = RB_RED;
z = z->parent->parent;
} else {
if (z == z->parent->right) {
z = z->parent;
rotate_left(table, z);
}
z->parent->color = RB_BLACK;
z->parent->parent->color = RB_RED;
rotate_right(table, z->parent->parent);
}
} else {
y = z->parent->parent->left;
if (y->color == RB_RED) {
z->parent->color = RB_BLACK;
y->color = RB_BLACK;
z->parent->parent->color = RB_RED;
z = z->parent->parent;
} else {
if (z == z->parent->left) {
z = z->parent;
rotate_right(table, z);
}
z->parent->color = RB_BLACK;
z->parent->parent->color = RB_RED;
rotate_left(table, z->parent->parent);
}
}
}
table->root->color = RB_BLACK;
}
/**
* Rebalances the tale after a delete.
*
* @param[in] table CC_TreeTable that is being rebalanced
* @param[in] z Node that comes after the deleted node
*/
static void rebalance_after_delete(CC_TreeTable *table, RBNode *x)
{
RBNode *w;
while (x != table->root && x->color == RB_BLACK) {
if (x == x->parent->left) {
w = x->parent->right;
if (w->color == RB_RED) {
w->color = RB_BLACK;
x->parent->color = RB_RED;
rotate_left(table, x->parent);
w = x->parent->right;
}
if (w->left->color == RB_BLACK && w->right->color == RB_BLACK) {
w->color = RB_RED;
x = x->parent;
} else {
if (w->right->color == RB_BLACK) {
w->left->color = RB_BLACK;
w->color = RB_RED;
rotate_right(table, w);
w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = RB_BLACK;
w->right->color = RB_BLACK;
rotate_left(table, x->parent);
x = table->root;
}
} else {
w = x->parent->left;
if (w->color == RB_RED) {
w->color = RB_BLACK;
x->parent->color = RB_RED;
rotate_right(table, x->parent);
w = x->parent->left;
}
if (w->right->color == RB_BLACK && w->left->color == RB_BLACK) {
w->color = RB_RED;
x = x->parent;
} else {
if (w->left->color == RB_BLACK) {
w->right->color = RB_BLACK;
w->color = RB_RED;
rotate_left(table, w);
w = x->parent->left;
}
w->color = x->parent->color;
x->parent->color = RB_BLACK;
w->left->color = RB_BLACK;
rotate_right(table, x->parent);
x = table->root;
}
}
}
x->color = RB_BLACK;
}
static INLINE void transplant(CC_TreeTable *table, RBNode *u, RBNode *v)
{
if (u->parent == table->sentinel)
table->root = v;
else if (u == u->parent->left)
u->parent->left = v;
else
u->parent->right = v;
v->parent = u->parent;
}
static INLINE RBNode *tree_min(CC_TreeTable const * const table, RBNode *n)
{
RBNode *s = table->sentinel;
if (n == s)
return s;
while (n->left != s)
n = n->left;
return n;
}
static INLINE RBNode *tree_max(CC_TreeTable const * const table, RBNode *n)
{
RBNode *s = table->sentinel;
if (n == s)
return s;
while (n->right != s)
n = n->right;
return n;
}
/**
* Removes a node from the RB tree.
*
* @param[in] table the table on which this operation is performed
* @param[in] z the node that is being removed
*/
static void remove_node(CC_TreeTable *table, RBNode *z)
{
RBNode *x;
RBNode *y = z;
int y_color = y->color;
if (z->left == table->sentinel) {
x = z->right;
transplant(table, z, z->right);
} else if (z->right == table->sentinel) {
x = z->left;
transplant(table, z, z->left);
} else {
y = tree_min(table, z->right);
y_color = y->color;
x = y->right;
if (y->parent == z) {
x->parent = y;
} else {
transplant(table, y, y->right);
y->right = z->right;
y->right->parent = y;
}
transplant(table, z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
}
if (y_color == RB_BLACK)
rebalance_after_delete(table, x);
table->mem_free(z);
table->size--;
}
/**
* Removes a key-value mapping from the specified CC_TreeTable and sets the out
* parameter to value.
*
* @param[in] table the table from which the key-value pair is being removed
* @param[in] key the key of the value being returned
* @param[out] out Pointer to where the removed value is stored, or NULL
* if it is to be ignored
*
* @return CC_OK if the mapping was successfully removed, or CC_ERR_KEY_NOT_FOUND
* if the key was not found.
*/
enum cc_stat cc_treetable_remove(CC_TreeTable *table, void *key, void **out)
{
RBNode *node = get_tree_node_by_key(table, key);
if (!node)
return CC_ERR_KEY_NOT_FOUND;
if (out)
*out = node->value;
remove_node(table, node);
return CC_OK;
}
/**
* Removes the first (lowest) key from the specified table and sets the out
* parameter to value.
*
* @param[in] table the table from which the first entry is being removed
* @param[out] out Pointer to where the removed value is stored, or NULL
* if it is to be ignored
*
* @return CC_OK if the mapping was successfully removed, or CC_ERR_KEY_NOT_FOUND
* if the key was not found.
*/
enum cc_stat cc_treetable_remove_first(CC_TreeTable *table, void **out)
{
if (table->size == 0)
return CC_ERR_KEY_NOT_FOUND;
RBNode *node = tree_min(table, table->root);
if (out)
*out = node->value;
remove_node(table, node);
return CC_OK;
}
/**
* Removes the last (highest) key from the specified table and sets the out
* parameter to value.
*
* @param[in] table the table from which the last entry is being removed
* @param[out] out Pointer to where the removed value is stored, or NULL
* if it is to be ignored
*
* @return CC_OK if the mapping was successfully removed, or CC_ERR_KEY_NOT_FOUND
* if the key was not found.
*/
enum cc_stat cc_treetable_remove_last(CC_TreeTable *table, void **out)
{
RBNode *node = tree_max(table, table->root);
if (!node)
return CC_ERR_KEY_NOT_FOUND;
if (out)
*out = node->value;
remove_node(table, node);
return CC_OK;
}
/**
* Removes all entries from the table.
*
* @param[in] table the table from which all entries are to be removed
*/
void cc_treetable_remove_all(CC_TreeTable *table)
{
tree_destroy(table, table->root);
table->size = 0;
table->root = table->sentinel;
}
/**
* Performs a right rotation on the specified table's RB tree at root <code>
* x</code>
*
* @param[in] table the table on which this operation is performed
* @param[in] x the node around which this operation is performed
*/
static void rotate_right(CC_TreeTable *table, RBNode *x)
{
RBNode *y = x->left;
x->left = y->right;
if (y->right != table->sentinel)
y->right->parent = x;
y->parent = x->parent;
if (x->parent == table->sentinel)
table->root = y;
else if (x == x->parent->right)
x->parent->right = y;
else
x->parent->left = y;
y->right = x;
x->parent = y;
}
/**
* Performs a left rotation on the specified table's RB tree at root <code>
* x</code>
*
* @param[in] table the table on which this operation is performed
* @param[in] x the node around which this operation is performed
*/
static void rotate_left(CC_TreeTable *table, RBNode *x)
{
RBNode *y = x->right;
x->right = y->left;
if (y->left != table->sentinel)
y->left->parent = x;
y->parent = x->parent;
if (x->parent == table->sentinel)
table->root = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
}
/**
* Returns a tree node associated with the specified key.
*
* @param[in] table the table on which this operation is performed
* @param[in] key the key being looked up
*
* @return tree node associated with the key
*/
static RBNode *get_tree_node_by_key(CC_TreeTable const * const table, const void *key)
{
if (table->size == 0)
return NULL;
RBNode *n = table->root;
RBNode *s = table->sentinel;
int cmp;
do {
cmp = table->cmp(key, n->key);
if (cmp < 0)
n = n->left;
else if (cmp > 0)
n = n->right;
else
return n;
} while (n != s && cmp != 0);
return NULL;
}
/**
* Returns a successor node of the node <code>x</code>
*
* @param[in] table the table on which this operation is performed
* @param[in] x the node whose successor is being returned
*
* @return successor node of x
*/
static RBNode *get_successor_node(CC_TreeTable const * const table, RBNode *x)
{
if (x == NULL)
return NULL;
if (x->right != table->sentinel)
return tree_min(table, x->right);
RBNode *y = x->parent;
while (y != table->sentinel && x == y->right) {
x = y;
y = y->parent;
}
return y;
}
/**
* Returns a predecessor node of the node <code>x</code>
*
* @param[in] table the table on which this operation is performed
* @param[in] x the node whose predecessor is being returned
*
* @return predecessor node of x
*/
static RBNode *get_predecessor_node(CC_TreeTable const * const table, RBNode *x)
{
if (x == NULL)
return NULL;
if (x->left != table->sentinel)
return tree_max(table, x->left);
RBNode *y = x->parent;
while (y != table->sentinel && x == y->left) {
x = y;
y = y->parent;
}
return y;
}
/**
* Applies the function fn to each key of the CC_TreeTable.
*
* @note The operation function should not modify the key. Any modification
* of the key will invalidate the CC_TreeTable.
*
* @param[in] table the table on which this operation is being performed
* @param[in] fn the operation function that is invoked on each key of the table
*/
void cc_treetable_foreach_key(CC_TreeTable *table, void (*fn) (const void *k))
{
RBNode *n = tree_min(table, table->root);
while (n != table->sentinel) {
fn(n->key);
n = get_successor_node(table, n);
}
}
/**
* Applies the function fn to each value of the CC_TreeTable.
*
* @param[in] table the table on which this operation is being performed
* @param[in] fn the operation function that is invoked on each value of the
* table
*/
void cc_treetable_foreach_value(CC_TreeTable *table, void (*fn) (void *k))
{
RBNode *n = tree_min(table, table->root);
while (n != table->sentinel) {
fn(n->value);
n = get_successor_node(table, n);
}
}
/**
* Initializes the CC_TreeTableIter structure.
*
* @param[in] iter the iterator that is being initialized
* @param[in] table the table over whose entries the iterator is going to iterate
*/
void cc_treetable_iter_init(CC_TreeTableIter *iter, CC_TreeTable *table)
{
iter->table = table;
iter->current = table->sentinel;
iter->next = tree_min(table, table->root);
}
/**
* Advances the iterator and sets the out parameter to the value of the
* next CC_TreeTableEntry.
*
* @param[in] iter the iterator that is being advanced
* @param[out] out Pointer to where the next entry is set
*
* @return CC_OK if the iterator was advanced, or CC_ITER_END if the
* end of the CC_TreeTable has been reached.
*/
enum cc_stat cc_treetable_iter_next(CC_TreeTableIter *iter, CC_TreeTableEntry *entry)
{
if (iter->next == iter->table->sentinel)
return CC_ITER_END;
entry->value = iter->next->value;
entry->key = iter->next->key;
iter->current = iter->next;
iter->next = get_successor_node(iter->table, iter->current);
return CC_OK;
}
/**
* Removes the last returned entry by <code>cc_treetable_iter_next()</code>
* function without invalidating the iterator and optionally sets the
* out parameter to the value of the removed entry.
*
* @note This Function should only ever be called after a call to <code>
* cc_treetable_iter_next()</code>
*
* @param[in] iter The iterator on which this operation is performed
* @param[out] out Pointer to where the removed element is stored, or NULL
* if it is to be ignored
*
* @return CC_OK if the entry was successfully removed, or
* CC_ERR_KEY_NOT_FOUND if the entry was already removed.
*/
enum cc_stat cc_treetable_iter_remove(CC_TreeTableIter *iter, void **out)
{
if (!iter->current)
return CC_ERR_KEY_NOT_FOUND;
if (out)
*out = iter->current->value;
remove_node(iter->table, iter->current);
iter->current = NULL;
return CC_OK;
}
size_t cc_treetable_struct_size()
{
return sizeof(CC_TreeTable);
}
#ifdef DEBUG
static int cc_treetable_test(CC_TreeTable *table, RBNode *node, int *nb)
{
if (node == table->sentinel) {
*nb = 1;
return RB_ERROR_OK;
}
/* check tree order */
if (node->left != table->sentinel) {
int cmp = table->cmp(node->left->key, node->key);
if (cmp >= 0)
return RB_ERROR_TREE_STRUCTURE;
}
if (node->right != table->sentinel) {
int cmp = table->cmp(node->right->key, node->key);
if (cmp <= 0)
return RB_ERROR_TREE_STRUCTURE;
}
/* check red rule */
if (node->color == RB_RED && node->parent->color == RB_RED) {
return RB_ERROR_CONSECUTIVE_RED;
}
int nb_left;
int nb_right;
int left_err = cc_treetable_test(table, node->left, &nb_left);
/* propagate the descendant errors all the way up */
if (left_err != RB_ERROR_OK)
return left_err;
int right_err = cc_treetable_test(table, node->right, &nb_right);
if (right_err != RB_ERROR_OK)
return right_err;
/* check black rule */
if (nb_left != nb_right)
return RB_ERROR_BLACK_HEIGHT;
if (node->color == RB_BLACK)
*nb = nb_left + 1;
else
*nb = nb_left;
return RB_ERROR_OK;
}
int cc_treetable_assert_rb_rules(CC_TreeTable *table)
{
int x;
int status = cc_treetable_test(table, table->root, &x);
return status;
}