-
Notifications
You must be signed in to change notification settings - Fork 16
/
rbtree.c
1709 lines (1493 loc) · 39.3 KB
/
rbtree.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
/*
* MIT License
* Copyright (c) 2002-2004, 2007, 2009 OZAWA Takuma
*/
#include <ruby/ruby.h>
#include <ruby/version.h>
#include <ruby/st.h>
#include <stdarg.h>
#include "dict.h"
#define RBTREE_PROC_DEFAULT FL_USER2
#define HASH_PROC_DEFAULT FL_USER2
#ifndef RETURN_ENUMERATOR
#define RETURN_ENUMERATOR(obj, argc, argv) ((void)0)
#endif
#ifndef RHASH_TBL
#define RHASH_TBL(h) RHASH(h)->tbl
#endif
#ifndef RHASH_IFNONE
#define RHASH_IFNONE(h) RHASH(h)->ifnone
#endif
VALUE RBTree;
VALUE MultiRBTree;
static ID id_bound;
static ID id_cmp;
static ID id_call;
static ID id_default;
typedef struct {
dict_t* dict;
VALUE ifnone;
int iter_lev;
} rbtree_t;
#define RBTREE(rbtree) DATA_PTR(rbtree)
#define DICT(rbtree) ((rbtree_t*)RBTREE(rbtree))->dict
#define IFNONE(rbtree) ((rbtree_t*)RBTREE(rbtree))->ifnone
#define ITER_LEV(rbtree) ((rbtree_t*)RBTREE(rbtree))->iter_lev
#define COMPARE(rbtree) DICT(rbtree)->dict_compare
#define CONTEXT(rbtree) DICT(rbtree)->dict_context
#define TO_KEY(arg) ((const void*)arg)
#define TO_VAL(arg) ((void*)arg)
#define GET_KEY(dnode) ((VALUE)dnode_getkey(dnode))
#define GET_VAL(dnode) ((VALUE)dnode_get(dnode))
#define ASSOC(dnode) rb_assoc_new(GET_KEY(dnode), GET_VAL(dnode))
/*********************************************************************/
static int
cmpint(VALUE i, VALUE a, VALUE b)
{
return rb_cmpint(i, a, b);
}
static void
rbtree_free(rbtree_t* rbtree)
{
dict_free_nodes(rbtree->dict);
dict_destroy(rbtree->dict);
xfree(rbtree);
}
static void
rbtree_mark(rbtree_t* rbtree)
{
if (rbtree == NULL) return;
if (rbtree->dict != NULL) {
dict_t* dict = rbtree->dict;
dnode_t* node;
for (node = dict_first(dict);
node != NULL;
node = dict_next(dict, node)) {
rb_gc_mark(GET_KEY(node));
rb_gc_mark(GET_VAL(node));
}
rb_gc_mark((VALUE)dict->dict_context);
}
rb_gc_mark(rbtree->ifnone);
}
static dnode_t*
rbtree_alloc_node(void* context)
{
return ALLOC(dnode_t);
}
static void
rbtree_free_node(dnode_t* node, void* context)
{
xfree(node);
}
static void
rbtree_argc_error()
{
rb_raise(rb_eArgError, "wrong number of arguments");
}
static int
rbtree_cmp(const void* key1, const void* key2, void* context)
{
VALUE ret;
if (TYPE(key1) == T_STRING && TYPE(key2) == T_STRING)
return rb_str_cmp((VALUE)key1, (VALUE)key2);
ret = rb_funcall((VALUE)key1, id_cmp, 1, (VALUE)key2);
return cmpint(ret, (VALUE)key1, (VALUE)key2);
}
static int
rbtree_user_cmp(const void* key1, const void* key2, void* cmp_proc)
{
VALUE ret = rb_funcall((VALUE)cmp_proc, id_call, 2,
(VALUE)key1, (VALUE)key2);
return cmpint(ret, (VALUE)key1, (VALUE)key2);
}
static void
rbtree_modify(VALUE self)
{
if (ITER_LEV(self) > 0)
rb_raise(rb_eTypeError, "can't modify rbtree in iteration");
if (OBJ_FROZEN(self))
rb_error_frozen("rbtree");
if (!OBJ_TAINTED(self) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: can't modify rbtree");
}
static VALUE
rbtree_alloc(VALUE klass)
{
dict_t* dict;
VALUE rbtree = Data_Wrap_Struct(klass, rbtree_mark, rbtree_free, 0);
RBTREE(rbtree) = ALLOC(rbtree_t);
MEMZERO(RBTREE(rbtree), rbtree_t, 1);
dict = dict_create(rbtree_cmp);
dict_set_allocator(dict, rbtree_alloc_node, rbtree_free_node,
(void*)Qnil);
if (klass == MultiRBTree)
dict_allow_dupes(dict);
DICT(rbtree) = dict;
IFNONE(rbtree) = Qnil;
return rbtree;
}
VALUE rbtree_aset(VALUE, VALUE, VALUE);
VALUE rbtree_clear(VALUE);
VALUE rbtree_has_key(VALUE, VALUE);
VALUE rbtree_update(VALUE, VALUE);
/*********************************************************************/
static int
hash_to_rbtree_i(VALUE key, VALUE value, VALUE rbtree)
{
if (key != Qundef)
rbtree_aset(rbtree, key, value);
return ST_CONTINUE;
}
/*
*
*/
VALUE
rbtree_s_create(int argc, VALUE* argv, VALUE klass)
{
long i;
VALUE rbtree;
if (argc == 1) {
VALUE tmp;
if (klass == RBTree && CLASS_OF(argv[0]) == MultiRBTree) {
rb_raise(rb_eTypeError, "can't convert MultiRBTree to RBTree");
}
if (rb_obj_is_kind_of(argv[0], klass)) {
rbtree = rbtree_alloc(klass);
rbtree_update(rbtree, argv[0]);
return rbtree;
}
tmp = rb_check_convert_type(argv[0], T_HASH, "Hash", "to_hash");
if (!NIL_P(tmp)) {
rbtree = rbtree_alloc(klass);
st_foreach(RHASH_TBL(tmp), hash_to_rbtree_i, rbtree);
return rbtree;
}
tmp = rb_check_array_type(argv[0]);
if (!NIL_P(tmp)) {
rbtree = rbtree_alloc(klass);
for (i = 0; i < RARRAY_LEN(tmp); i++) {
VALUE v = rb_check_array_type(RARRAY_PTR(tmp)[i]);
if (NIL_P(v)) {
continue;
}
switch(RARRAY_LEN(v)) {
case 1:
rbtree_aset(rbtree, RARRAY_PTR(v)[0], Qnil);
break;
case 2:
rbtree_aset(rbtree, RARRAY_PTR(v)[0], RARRAY_PTR(v)[1]);
break;
default:
continue;
}
}
return rbtree;
}
}
if (argc % 2 != 0)
rb_raise(rb_eArgError, "odd number of arguments for RBTree");
rbtree = rbtree_alloc(klass);
for (i = 0; i < argc; i += 2)
rbtree_aset(rbtree, argv[i], argv[i + 1]);
return rbtree;
}
/*
*
*/
VALUE
rbtree_initialize(int argc, VALUE* argv, VALUE self)
{
rbtree_modify(self);
if (rb_block_given_p()) {
if (argc > 0)
rbtree_argc_error();
IFNONE(self) = rb_block_proc();
FL_SET(self, RBTREE_PROC_DEFAULT);
} else {
if (argc > 1)
rbtree_argc_error();
else if (argc == 1)
IFNONE(self) = argv[0];
}
return self;
}
/*********************************************************************/
typedef enum {
INITIAL_VALUE, NODE_NOT_FOUND, NODE_FOUND
} insert_node_ret_t;
typedef struct {
dict_t* dict;
dnode_t* node;
const void* key;
insert_node_ret_t ret;
} insert_node_t;
static VALUE
insert_node_body(insert_node_t* arg)
{
if (dict_insert(arg->dict, arg->node, arg->key))
arg->ret = NODE_NOT_FOUND;
else
arg->ret = NODE_FOUND;
return Qnil;
}
static VALUE
insert_node_ensure(insert_node_t* arg)
{
dict_t* dict = arg->dict;
dnode_t* node = arg->node;
switch (arg->ret) {
case INITIAL_VALUE:
dict->dict_freenode(node, dict->dict_context);
break;
case NODE_NOT_FOUND:
if (TYPE(arg->key) == T_STRING)
node->dict_key = TO_KEY(rb_str_new4(GET_KEY(node)));
break;
case NODE_FOUND:
dict->dict_freenode(node, dict->dict_context);
break;
}
return Qnil;
}
static void
rbtree_insert(VALUE self, VALUE key, VALUE value)
{
insert_node_t arg;
dict_t* dict = DICT(self);
dnode_t* node = dict->dict_allocnode(dict->dict_context);
dnode_init(node, TO_VAL(value));
arg.dict = dict;
arg.node = node;
arg.key = TO_KEY(key);
arg.ret = INITIAL_VALUE;
rb_ensure(insert_node_body, (VALUE)&arg,
insert_node_ensure, (VALUE)&arg);
}
/*********************************************************************/
/*
*
*/
VALUE
rbtree_aset(VALUE self, VALUE key, VALUE value)
{
rbtree_modify(self);
if (dict_isfull(DICT(self))) {
dnode_t* node = dict_lookup(DICT(self), TO_KEY(key));
if (node == NULL)
rb_raise(rb_eIndexError, "rbtree full");
else
dnode_put(node, TO_VAL(value));
return value;
}
rbtree_insert(self, key, value);
return value;
}
/*
*
*/
VALUE
rbtree_aref(VALUE self, VALUE key)
{
dnode_t* node = dict_lookup(DICT(self), TO_KEY(key));
if (node == NULL)
return rb_funcall(self, id_default, 1, key);
else
return GET_VAL(node);
}
/*
*
*/
VALUE
rbtree_fetch(int argc, VALUE* argv, VALUE self)
{
dnode_t* node;
int block_given;
if (argc == 0 || argc > 2)
rbtree_argc_error();
block_given = rb_block_given_p();
if (block_given && argc == 2)
rb_warn("block supersedes default value argument");
node = dict_lookup(DICT(self), TO_KEY(argv[0]));
if (node != NULL)
return GET_VAL(node);
if (block_given)
return rb_yield(argv[0]);
if (argc == 1)
rb_raise(rb_eIndexError, "key not found");
return argv[1];
}
/*
*
*/
VALUE
rbtree_size(VALUE self)
{
return ULONG2NUM(dict_count(DICT(self)));
}
/*
*
*/
VALUE
rbtree_empty_p(VALUE self)
{
return dict_isempty(DICT(self)) ? Qtrue : Qfalse;
}
/*
*
*/
VALUE
rbtree_default(int argc, VALUE* argv, VALUE self)
{
VALUE key = Qnil;
if (argc == 1)
key = argv[0];
else if (argc > 1)
rbtree_argc_error();
if (FL_TEST(self, RBTREE_PROC_DEFAULT)) {
if (argc == 0) return Qnil;
return rb_funcall(IFNONE(self), id_call, 2, self, key);
}
return IFNONE(self);
}
/*
*
*/
VALUE
rbtree_set_default(VALUE self, VALUE ifnone)
{
rbtree_modify(self);
IFNONE(self) = ifnone;
FL_UNSET(self, RBTREE_PROC_DEFAULT);
return ifnone;
}
/*
*
*/
VALUE
rbtree_default_proc(VALUE self)
{
if (FL_TEST(self, RBTREE_PROC_DEFAULT))
return IFNONE(self);
return Qnil;
}
static int
value_eq(const void* key1, const void* key2)
{
return rb_equal((VALUE)key1, (VALUE)key2) != 0;
}
/*
*
*/
VALUE
rbtree_equal(VALUE self, VALUE other)
{
int ret;
if (self == other)
return Qtrue;
if (!rb_obj_is_kind_of(other, MultiRBTree))
return Qfalse;
ret = dict_equal(DICT(self), DICT(other), value_eq);
return ret ? Qtrue : Qfalse;
}
/*********************************************************************/
typedef enum {
EACH_NEXT, EACH_STOP
} each_return_t;
typedef each_return_t (*each_callback_func)(dnode_t*, void*);
typedef struct {
VALUE self;
each_callback_func func;
void* arg;
int reverse;
} rbtree_each_arg_t;
static VALUE
rbtree_each_ensure(VALUE self)
{
ITER_LEV(self)--;
return Qnil;
}
static VALUE
rbtree_each_body(rbtree_each_arg_t* arg)
{
VALUE self = arg->self;
dict_t* dict = DICT(self);
dnode_t* node;
dnode_t* first_node;
dnode_t* (*next_func)(dict_t*, dnode_t*);
if (arg->reverse) {
first_node = dict_last(dict);
next_func = dict_prev;
} else {
first_node = dict_first(dict);
next_func = dict_next;
}
ITER_LEV(self)++;
for (node = first_node;
node != NULL;
node = next_func(dict, node)) {
if (arg->func(node, arg->arg) == EACH_STOP)
break;
}
return self;
}
static VALUE
rbtree_for_each(VALUE self, each_callback_func func, void* arg)
{
rbtree_each_arg_t each_arg;
each_arg.self = self;
each_arg.func = func;
each_arg.arg = arg;
each_arg.reverse = 0;
return rb_ensure(rbtree_each_body, (VALUE)&each_arg,
rbtree_each_ensure, self);
}
static VALUE
rbtree_reverse_for_each(VALUE self, each_callback_func func, void* arg)
{
rbtree_each_arg_t each_arg;
each_arg.self = self;
each_arg.func = func;
each_arg.arg = arg;
each_arg.reverse = 1;
return rb_ensure(rbtree_each_body, (VALUE)&each_arg,
rbtree_each_ensure, self);
}
/*********************************************************************/
static each_return_t
each_i(dnode_t* node, void* arg)
{
rb_yield(ASSOC(node));
return EACH_NEXT;
}
/*
* call-seq:
* rbtree.each {|key, value| block} => rbtree
*
* Calls block once for each key in order, passing the key and value
* as a two-element array parameters.
*/
VALUE
rbtree_each(VALUE self)
{
RETURN_ENUMERATOR(self, 0, NULL);
return rbtree_for_each(self, each_i, NULL);
}
static each_return_t
each_pair_i(dnode_t* node, void* arg)
{
rb_yield_values(2, GET_KEY(node), GET_VAL(node));
return EACH_NEXT;
}
/*
* call-seq:
* rbtree.each_pair {|key, value| block} => rbtree
*
* Calls block once for each key in order, passing the key and value
* as parameters.
*/
VALUE
rbtree_each_pair(VALUE self)
{
RETURN_ENUMERATOR(self, 0, NULL);
return rbtree_for_each(self, each_pair_i, NULL);
}
static each_return_t
each_key_i(dnode_t* node, void* arg)
{
rb_yield(GET_KEY(node));
return EACH_NEXT;
}
/*
* call-seq:
* rbtree.each_key {|key| block} => rbtree
*
* Calls block once for each key in order, passing the key as
* parameters.
*/
VALUE
rbtree_each_key(VALUE self)
{
RETURN_ENUMERATOR(self, 0, NULL);
return rbtree_for_each(self, each_key_i, NULL);
}
static each_return_t
each_value_i(dnode_t* node, void* arg)
{
rb_yield(GET_VAL(node));
return EACH_NEXT;
}
/*
* call-seq:
* rbtree.each_value {|value| block} => rbtree
*
* Calls block once for each key in order, passing the value as
* parameters.
*/
VALUE
rbtree_each_value(VALUE self)
{
RETURN_ENUMERATOR(self, 0, NULL);
return rbtree_for_each(self, each_value_i, NULL);
}
/*
* call-seq:
* rbtree.reverse_each {|key, value| block} => rbtree
*
* Calls block once for each key in reverse order, passing the key and
* value as parameters.
*/
VALUE
rbtree_reverse_each(VALUE self)
{
RETURN_ENUMERATOR(self, 0, NULL);
return rbtree_reverse_for_each(self, each_pair_i, NULL);
}
static each_return_t
aset_i(dnode_t* node, void* self)
{
rbtree_aset((VALUE)self, GET_KEY(node), GET_VAL(node));
return EACH_NEXT;
}
static void
copy_dict(VALUE src, VALUE dest, dict_comp_t cmp, void* context)
{
VALUE temp = rbtree_alloc(CLASS_OF(dest));
COMPARE(temp) = cmp;
CONTEXT(temp) = context;
rbtree_for_each(src, aset_i, (void*)temp);
{
dict_t* t = DICT(temp);
DICT(temp) = DICT(dest);
DICT(dest) = t;
}
rbtree_free(RBTREE(temp));
rb_gc_force_recycle(temp);
}
/*
*
*/
VALUE
rbtree_initialize_copy(VALUE self, VALUE other)
{
if (self == other)
return self;
if (!rb_obj_is_kind_of(other, CLASS_OF(self))) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
rb_class2name(CLASS_OF(other)),
rb_class2name(CLASS_OF(self)));
}
copy_dict(other, self, COMPARE(other), CONTEXT(other));
IFNONE(self) = IFNONE(other);
if (FL_TEST(other, RBTREE_PROC_DEFAULT))
FL_SET(self, RBTREE_PROC_DEFAULT);
else
FL_UNSET(self, RBTREE_PROC_DEFAULT);
return self;
}
/*
*
*/
VALUE
rbtree_values_at(int argc, VALUE* argv, VALUE self)
{
long i;
VALUE ary = rb_ary_new();
for (i = 0; i < argc; i++)
rb_ary_push(ary, rbtree_aref(self, argv[i]));
return ary;
}
static each_return_t
select_i(dnode_t* node, void* ary)
{
if (RTEST(rb_yield_values(2, GET_KEY(node), GET_VAL(node))))
rb_ary_push((VALUE)ary, ASSOC(node));
return EACH_NEXT;
}
/*
*
*/
VALUE
rbtree_select(VALUE self)
{
VALUE ary;
RETURN_ENUMERATOR(self, 0, NULL);
ary = rb_ary_new();
rbtree_for_each(self, select_i, (void*)ary);
return ary;
}
static each_return_t
index_i(dnode_t* node, void* arg_)
{
VALUE* arg = (VALUE*)arg_;
if (rb_equal(GET_VAL(node), arg[1])) {
arg[0] = GET_KEY(node);
return EACH_STOP;
}
return EACH_NEXT;
}
/*
*
*/
VALUE
rbtree_index(VALUE self, VALUE value)
{
VALUE arg[2];
arg[0] = Qnil;
arg[1] = value;
rbtree_for_each(self, index_i, (void*)&arg);
return arg[0];
}
/*
*
*/
VALUE
rbtree_clear(VALUE self)
{
rbtree_modify(self);
dict_free_nodes(DICT(self));
return self;
}
/*
*
*/
VALUE
rbtree_delete(VALUE self, VALUE key)
{
dict_t* dict = DICT(self);
dnode_t* node;
VALUE value;
rbtree_modify(self);
node = dict_lookup(dict, TO_KEY(key));
if (node == NULL)
return rb_block_given_p() ? rb_yield(key) : Qnil;
value = GET_VAL(node);
dict_delete_free(dict, node);
return value;
}
/*********************************************************************/
typedef struct dnode_list_t_ {
struct dnode_list_t_* prev;
dnode_t* node;
} dnode_list_t;
typedef struct {
VALUE self;
dnode_list_t* list;
int raised;
} rbtree_delete_if_arg_t;
static VALUE
rbtree_delete_if_ensure(rbtree_delete_if_arg_t* arg)
{
dict_t* dict = DICT(arg->self);
dnode_list_t* list = arg->list;
if (--ITER_LEV(arg->self) == 0) {
while (list != NULL) {
dnode_list_t* l = list;
if (!arg->raised)
dict_delete_free(dict, l->node);
list = l->prev;
xfree(l);
}
}
return Qnil;
}
static VALUE
rbtree_delete_if_body(rbtree_delete_if_arg_t* arg)
{
VALUE self = arg->self;
dict_t* dict = DICT(self);
dnode_t* node;
arg->raised = 1;
ITER_LEV(self)++;
for (node = dict_first(dict);
node != NULL;
node = dict_next(dict, node)) {
if (RTEST(rb_yield_values(2, GET_KEY(node), GET_VAL(node)))) {
dnode_list_t* l = ALLOC(dnode_list_t);
l->node = node;
l->prev = arg->list;
arg->list = l;
}
}
arg->raised = 0;
return self;
}
/*********************************************************************/
/*
*
*/
VALUE
rbtree_delete_if(VALUE self)
{
rbtree_delete_if_arg_t arg;
RETURN_ENUMERATOR(self, 0, NULL);
rbtree_modify(self);
arg.self = self;
arg.list = NULL;
return rb_ensure(rbtree_delete_if_body, (VALUE)&arg,
rbtree_delete_if_ensure, (VALUE)&arg);
}
/*
*
*/
VALUE
rbtree_reject_bang(VALUE self)
{
dictcount_t count;
RETURN_ENUMERATOR(self, 0, NULL);
count = dict_count(DICT(self));
rbtree_delete_if(self);
if (count == dict_count(DICT(self)))
return Qnil;
return self;
}
/*
*
*/
VALUE
rbtree_reject(VALUE self)
{
return rbtree_reject_bang(rb_obj_dup(self));
}
static VALUE
rbtree_shift_pop(VALUE self, const int shift)
{
dict_t* dict = DICT(self);
dnode_t* node;
VALUE ret;
rbtree_modify(self);
if (dict_isempty(dict)) {
if (FL_TEST(self, RBTREE_PROC_DEFAULT)) {
return rb_funcall(IFNONE(self), id_call, 2, self, Qnil);
}
return IFNONE(self);
}
if (shift)
node = dict_last(dict);
else
node = dict_first(dict);
ret = ASSOC(node);
dict_delete_free(dict, node);
return ret;
}
/*
* call-seq:
* rbtree.shift => array or object
*
* Removes the first(that is, the smallest) key-value pair and returns
* it as a two-item array.
*/
VALUE
rbtree_shift(VALUE self)
{
return rbtree_shift_pop(self, 0);
}
/*
* call-seq:
* rbtree.pop => array or object
*
* Removes the last(that is, the biggest) key-value pair and returns
* it as a two-item array.
*/
VALUE
rbtree_pop(VALUE self)
{
return rbtree_shift_pop(self, 1);
}
static each_return_t
invert_i(dnode_t* node, void* rbtree)
{
rbtree_aset((VALUE)rbtree, GET_VAL(node), GET_KEY(node));
return EACH_NEXT;
}
/*
*
*/
VALUE
rbtree_invert(VALUE self)
{
VALUE rbtree = rbtree_alloc(CLASS_OF(self));
rbtree_for_each(self, invert_i, (void*)rbtree);
return rbtree;
}
static each_return_t
update_block_i(dnode_t* node, void* self_)
{
VALUE self = (VALUE)self_;
VALUE key = GET_KEY(node);
VALUE value = GET_VAL(node);
if (rbtree_has_key(self, key))
value = rb_yield_values(3, key, rbtree_aref(self, key), value);
rbtree_aset(self, key, value);
return EACH_NEXT;
}
/*
*
*/
VALUE
rbtree_update(VALUE self, VALUE other)
{
rbtree_modify(self);
if (self == other)
return self;
if (!rb_obj_is_kind_of(other, CLASS_OF(self))) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
rb_class2name(CLASS_OF(other)),
rb_class2name(CLASS_OF(self)));
}
if (rb_block_given_p())
rbtree_for_each(other, update_block_i, (void*)self);
else
rbtree_for_each(other, aset_i, (void*)self);
return self;
}
/*
*
*/
VALUE
rbtree_merge(VALUE self, VALUE other)
{
return rbtree_update(rb_obj_dup(self), other);
}
/*
*
*/
VALUE
rbtree_has_key(VALUE self, VALUE key)
{
return dict_lookup(DICT(self), TO_KEY(key)) == NULL ? Qfalse : Qtrue;
}
static each_return_t
has_value_i(dnode_t* node, void* arg_)
{
VALUE* arg = (VALUE*)arg_;
if (rb_equal(GET_VAL(node), arg[1])) {
arg[0] = Qtrue;
return EACH_STOP;
}
return EACH_NEXT;
}
/*
*
*/