Ruby 4.1.0dev (2025-12-29 revision 65634d8df57ea1636efffb5f040fa31c156d307f)
set.c (65634d8df57ea1636efffb5f040fa31c156d307f)
1/* This implements sets using the same hash table implementation as in
2 st.c, but without a value for each hash entry. This results in the
3 same basic performance characteristics as when using an st table,
4 but uses 1/3 less memory.
5 */
6
7#include "id.h"
8#include "internal.h"
9#include "internal/bits.h"
10#include "internal/error.h"
11#include "internal/hash.h"
12#include "internal/proc.h"
13#include "internal/sanitizers.h"
14#include "internal/set_table.h"
15#include "internal/symbol.h"
16#include "internal/variable.h"
17#include "ruby_assert.h"
18
19#include <stdio.h>
20#ifdef HAVE_STDLIB_H
21#include <stdlib.h>
22#endif
23#include <string.h>
24
25#ifndef SET_DEBUG
26#define SET_DEBUG 0
27#endif
28
29#if SET_DEBUG
30#include "internal/gc.h"
31#endif
32
33static st_index_t
34dbl_to_index(double d)
35{
36 union {double d; st_index_t i;} u;
37 u.d = d;
38 return u.i;
39}
40
41static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
42static const uint32_t prime2 = 0x830fcab9;
43
44static inline uint64_t
45mult_and_mix(uint64_t m1, uint64_t m2)
46{
47#if defined HAVE_UINT128_T
48 uint128_t r = (uint128_t) m1 * (uint128_t) m2;
49 return (uint64_t) (r >> 64) ^ (uint64_t) r;
50#else
51 uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
52 uint64_t lm1 = m1, lm2 = m2;
53 uint64_t v64_128 = hm1 * hm2;
54 uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
55 uint64_t v1_32 = lm1 * lm2;
56
57 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
58#endif
59}
60
61static inline uint64_t
62key64_hash(uint64_t key, uint32_t seed)
63{
64 return mult_and_mix(key + seed, prime1);
65}
66
67/* Should cast down the result for each purpose */
68#define set_index_hash(index) key64_hash(rb_hash_start(index), prime2)
69
70static st_index_t
71set_ident_hash(st_data_t n)
72{
73#ifdef USE_FLONUM /* RUBY */
74 /*
75 * - flonum (on 64-bit) is pathologically bad, mix the actual
76 * float value in, but do not use the float value as-is since
77 * many integers get interpreted as 2.0 or -2.0 [Bug #10761]
78 */
79 if (FLONUM_P(n)) {
80 n ^= dbl_to_index(rb_float_value(n));
81 }
82#endif
83
84 return (st_index_t)set_index_hash((st_index_t)n);
85}
86
87static const struct st_hash_type identhash = {
88 rb_st_numcmp,
89 set_ident_hash,
90};
91
92static const struct st_hash_type objhash = {
93 rb_any_cmp,
94 rb_any_hash,
95};
96
98
99#define id_each idEach
100static ID id_each_entry;
101static ID id_any_p;
102static ID id_new;
103static ID id_i_hash;
104static ID id_set_iter_lev;
105static ID id_subclass_compatible;
106static ID id_class_methods;
107
108#define RSET_INITIALIZED FL_USER1
109#define RSET_LEV_MASK (FL_USER13 | FL_USER14 | FL_USER15 | /* FL 13..19 */ \
110 FL_USER16 | FL_USER17 | FL_USER18 | FL_USER19)
111#define RSET_LEV_SHIFT (FL_USHIFT + 13)
112#define RSET_LEV_MAX 127 /* 7 bits */
113
114#define SET_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(SET_DEBUG, expr, #expr)
115
116#define RSET_SIZE(set) set_table_size(RSET_TABLE(set))
117#define RSET_EMPTY(set) (RSET_SIZE(set) == 0)
118#define RSET_SIZE_NUM(set) SIZET2NUM(RSET_SIZE(set))
119#define RSET_IS_MEMBER(set, item) set_table_lookup(RSET_TABLE(set), (st_data_t)(item))
120#define RSET_COMPARE_BY_IDENTITY(set) (RSET_TABLE(set)->type == &identhash)
121
123 set_table table;
124};
125
126static int
127mark_key(st_data_t key, st_data_t data)
128{
129 rb_gc_mark_movable((VALUE)key);
130
131 return ST_CONTINUE;
132}
133
134static void
135set_mark(void *ptr)
136{
137 struct set_object *sobj = ptr;
138 if (sobj->table.entries) set_table_foreach(&sobj->table, mark_key, 0);
139}
140
141static void
142set_free_embedded(struct set_object *sobj)
143{
144 free((&sobj->table)->entries);
145}
146
147static void
148set_free(void *ptr)
149{
150 struct set_object *sobj = ptr;
151 set_free_embedded(sobj);
152 memset(&sobj->table, 0, sizeof(sobj->table));
153}
154
155static size_t
156set_size(const void *ptr)
157{
158 const struct set_object *sobj = ptr;
159 /* Do not count the table size twice, as it is embedded */
160 return (unsigned long)set_memsize(&sobj->table) - sizeof(sobj->table);
161}
162
163static int
164set_foreach_replace(st_data_t key, st_data_t argp, int error)
165{
166 if (rb_gc_location((VALUE)key) != (VALUE)key) {
167 return ST_REPLACE;
168 }
169
170 return ST_CONTINUE;
171}
172
173static int
174set_replace_ref(st_data_t *key, st_data_t argp, int existing)
175{
176 rb_gc_mark_and_move((VALUE *)key);
177
178 return ST_CONTINUE;
179}
180
181static void
182set_update_references(void *ptr)
183{
184 struct set_object *sobj = ptr;
185 set_foreach_with_replace(&sobj->table, set_foreach_replace, set_replace_ref, 0);
186}
187
188static const rb_data_type_t set_data_type = {
189 .wrap_struct_name = "set",
190 .function = {
191 .dmark = set_mark,
192 .dfree = set_free,
193 .dsize = set_size,
194 .dcompact = set_update_references,
195 },
196 .flags = RUBY_TYPED_EMBEDDABLE | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE
197};
198
199static inline set_table *
200RSET_TABLE(VALUE set)
201{
202 struct set_object *sobj;
203 TypedData_Get_Struct(set, struct set_object, &set_data_type, sobj);
204 return &sobj->table;
205}
206
207static unsigned long
208iter_lev_in_ivar(VALUE set)
209{
210 VALUE levval = rb_ivar_get(set, id_set_iter_lev);
211 SET_ASSERT(FIXNUM_P(levval));
212 long lev = FIX2LONG(levval);
213 SET_ASSERT(lev >= 0);
214 return (unsigned long)lev;
215}
216
217void rb_ivar_set_internal(VALUE obj, ID id, VALUE val);
218
219static void
220iter_lev_in_ivar_set(VALUE set, unsigned long lev)
221{
222 SET_ASSERT(lev >= RSET_LEV_MAX);
223 SET_ASSERT(POSFIXABLE(lev)); /* POSFIXABLE means fitting to long */
224 rb_ivar_set_internal(set, id_set_iter_lev, LONG2FIX((long)lev));
225}
226
227static inline unsigned long
228iter_lev_in_flags(VALUE set)
229{
230 return (unsigned long)((RBASIC(set)->flags >> RSET_LEV_SHIFT) & RSET_LEV_MAX);
231}
232
233static inline void
234iter_lev_in_flags_set(VALUE set, unsigned long lev)
235{
236 SET_ASSERT(lev <= RSET_LEV_MAX);
237 RBASIC(set)->flags = ((RBASIC(set)->flags & ~RSET_LEV_MASK) | ((VALUE)lev << RSET_LEV_SHIFT));
238}
239
240static inline bool
241set_iterating_p(VALUE set)
242{
243 return iter_lev_in_flags(set) > 0;
244}
245
246static void
247set_iter_lev_inc(VALUE set)
248{
249 unsigned long lev = iter_lev_in_flags(set);
250 if (lev == RSET_LEV_MAX) {
251 lev = iter_lev_in_ivar(set) + 1;
252 if (!POSFIXABLE(lev)) { /* paranoiac check */
253 rb_raise(rb_eRuntimeError, "too much nested iterations");
254 }
255 }
256 else {
257 lev += 1;
258 iter_lev_in_flags_set(set, lev);
259 if (lev < RSET_LEV_MAX) return;
260 }
261 iter_lev_in_ivar_set(set, lev);
262}
263
264static void
265set_iter_lev_dec(VALUE set)
266{
267 unsigned long lev = iter_lev_in_flags(set);
268 if (lev == RSET_LEV_MAX) {
269 lev = iter_lev_in_ivar(set);
270 if (lev > RSET_LEV_MAX) {
271 iter_lev_in_ivar_set(set, lev-1);
272 return;
273 }
274 rb_attr_delete(set, id_set_iter_lev);
275 }
276 else if (lev == 0) {
277 rb_raise(rb_eRuntimeError, "iteration level underflow");
278 }
279 iter_lev_in_flags_set(set, lev - 1);
280}
281
282static VALUE
283set_foreach_ensure(VALUE set)
284{
285 set_iter_lev_dec(set);
286 return 0;
287}
288
289typedef int set_foreach_func(VALUE, VALUE);
290
292 VALUE set;
293 set_foreach_func *func;
294 VALUE arg;
295};
296
297static int
298set_iter_status_check(int status)
299{
300 if (status == ST_CONTINUE) {
301 return ST_CHECK;
302 }
303
304 return status;
305}
306
307static int
308set_foreach_iter(st_data_t key, st_data_t argp, int error)
309{
310 struct set_foreach_arg *arg = (struct set_foreach_arg *)argp;
311
312 if (error) return ST_STOP;
313
314 set_table *tbl = RSET_TABLE(arg->set);
315 int status = (*arg->func)((VALUE)key, arg->arg);
316
317 if (RSET_TABLE(arg->set) != tbl) {
318 rb_raise(rb_eRuntimeError, "reset occurred during iteration");
319 }
320
321 return set_iter_status_check(status);
322}
323
324static VALUE
325set_foreach_call(VALUE arg)
326{
327 VALUE set = ((struct set_foreach_arg *)arg)->set;
328 int ret = 0;
329 ret = set_foreach_check(RSET_TABLE(set), set_foreach_iter,
330 (st_data_t)arg, (st_data_t)Qundef);
331 if (ret) {
332 rb_raise(rb_eRuntimeError, "ret: %d, set modified during iteration", ret);
333 }
334 return Qnil;
335}
336
337static void
338set_iter(VALUE set, set_foreach_func *func, VALUE farg)
339{
340 struct set_foreach_arg arg;
341
342 if (RSET_EMPTY(set))
343 return;
344 arg.set = set;
345 arg.func = func;
346 arg.arg = farg;
347 if (RB_OBJ_FROZEN(set)) {
348 set_foreach_call((VALUE)&arg);
349 }
350 else {
351 set_iter_lev_inc(set);
352 rb_ensure(set_foreach_call, (VALUE)&arg, set_foreach_ensure, set);
353 }
354}
355
356NORETURN(static void no_new_item(void));
357static void
358no_new_item(void)
359{
360 rb_raise(rb_eRuntimeError, "can't add a new item into set during iteration");
361}
362
363static void
364set_compact_after_delete(VALUE set)
365{
366 if (!set_iterating_p(set)) {
367 set_compact_table(RSET_TABLE(set));
368 }
369}
370
371static int
372set_table_insert_wb(set_table *tab, VALUE set, VALUE key, VALUE *key_addr)
373{
374 if (tab->type != &identhash && rb_obj_class(key) == rb_cString && !RB_OBJ_FROZEN(key)) {
375 key = rb_hash_key_str(key);
376 if (key_addr) *key_addr = key;
377 }
378 int ret = set_insert(tab, (st_data_t)key);
379 if (ret == 0) RB_OBJ_WRITTEN(set, Qundef, key);
380 return ret;
381}
382
383static int
384set_insert_wb(VALUE set, VALUE key, VALUE *key_addr)
385{
386 return set_table_insert_wb(RSET_TABLE(set), set, key, key_addr);
387}
388
389static VALUE
390set_alloc_with_size(VALUE klass, st_index_t size)
391{
392 VALUE set;
393 struct set_object *sobj;
394
395 set = TypedData_Make_Struct(klass, struct set_object, &set_data_type, sobj);
396 set_init_table_with_size(&sobj->table, &objhash, size);
397
398 return set;
399}
400
401
402static VALUE
403set_s_alloc(VALUE klass)
404{
405 return set_alloc_with_size(klass, 0);
406}
407
408/*
409 * call-seq:
410 * Set[*objects] -> new_set
411 *
412 * Returns a new Set object populated with the given objects,
413 * See Set::new.
414 */
415static VALUE
416set_s_create(int argc, VALUE *argv, VALUE klass)
417{
418 VALUE set = set_alloc_with_size(klass, argc);
419 set_table *table = RSET_TABLE(set);
420 int i;
421
422 for (i=0; i < argc; i++) {
423 set_table_insert_wb(table, set, argv[i], NULL);
424 }
425
426 return set;
427}
428
429static VALUE
430set_s_inherited(VALUE klass, VALUE subclass)
431{
432 if (klass == rb_cSet) {
433 // When subclassing directly from Set, include the compatibility layer
434 rb_require("set/subclass_compatible.rb");
435 VALUE subclass_compatible = rb_const_get(klass, id_subclass_compatible);
436 rb_include_module(subclass, subclass_compatible);
437 rb_extend_object(subclass, rb_const_get(subclass_compatible, id_class_methods));
438 }
439 return Qnil;
440}
441
442static void
443check_set(VALUE arg)
444{
445 if (!rb_obj_is_kind_of(arg, rb_cSet)) {
446 rb_raise(rb_eArgError, "value must be a set");
447 }
448}
449
450static ID
451enum_method_id(VALUE other)
452{
453 if (rb_respond_to(other, id_each_entry)) {
454 return id_each_entry;
455 }
456 else if (rb_respond_to(other, id_each)) {
457 return id_each;
458 }
459 else {
460 rb_raise(rb_eArgError, "value must be enumerable");
461 }
462}
463
464static VALUE
465set_enum_size(VALUE set, VALUE args, VALUE eobj)
466{
467 return RSET_SIZE_NUM(set);
468}
469
470static VALUE
471set_initialize_without_block(RB_BLOCK_CALL_FUNC_ARGLIST(i, set))
472{
473 VALUE element = i;
474 set_insert_wb(set, element, &element);
475 return element;
476}
477
478static VALUE
479set_initialize_with_block(RB_BLOCK_CALL_FUNC_ARGLIST(i, set))
480{
481 VALUE element = rb_yield(i);
482 set_insert_wb(set, element, &element);
483 return element;
484}
485
486/*
487 * call-seq:
488 * Set.new -> new_set
489 * Set.new(enum) -> new_set
490 * Set.new(enum) { |elem| ... } -> new_set
491 *
492 * Creates a new set containing the elements of the given enumerable
493 * object.
494 *
495 * If a block is given, the elements of enum are preprocessed by the
496 * given block.
497 *
498 * Set.new([1, 2]) #=> Set[1, 2]
499 * Set.new([1, 2, 1]) #=> Set[1, 2]
500 * Set.new([1, 'c', :s]) #=> Set[1, "c", :s]
501 * Set.new(1..5) #=> Set[1, 2, 3, 4, 5]
502 * Set.new([1, 2, 3]) { |x| x * x } #=> Set[1, 4, 9]
503 */
504static VALUE
505set_i_initialize(int argc, VALUE *argv, VALUE set)
506{
507 if (RBASIC(set)->flags & RSET_INITIALIZED) {
508 rb_raise(rb_eRuntimeError, "cannot reinitialize set");
509 }
510 RBASIC(set)->flags |= RSET_INITIALIZED;
511
512 VALUE other;
513 rb_check_arity(argc, 0, 1);
514
515 if (argc > 0 && (other = argv[0]) != Qnil) {
516 if (RB_TYPE_P(other, T_ARRAY)) {
517 long i;
518 int block_given = rb_block_given_p();
519 set_table *into = RSET_TABLE(set);
520 for (i=0; i<RARRAY_LEN(other); i++) {
521 VALUE key = RARRAY_AREF(other, i);
522 if (block_given) key = rb_yield(key);
523 set_table_insert_wb(into, set, key, NULL);
524 }
525 }
526 else {
527 rb_block_call(other, enum_method_id(other), 0, 0,
528 rb_block_given_p() ? set_initialize_with_block : set_initialize_without_block,
529 set);
530 }
531 }
532
533 return set;
534}
535
536/* :nodoc: */
537static VALUE
538set_i_initialize_copy(VALUE set, VALUE other)
539{
540 if (set == other) return set;
541
542 if (set_iterating_p(set)) {
543 rb_raise(rb_eRuntimeError, "cannot replace set during iteration");
544 }
545
546 struct set_object *sobj;
547 TypedData_Get_Struct(set, struct set_object, &set_data_type, sobj);
548
549 set_free_embedded(sobj);
550 set_copy(&sobj->table, RSET_TABLE(other));
551 rb_gc_writebarrier_remember(set);
552
553 return set;
554}
555
556static int
557set_inspect_i(st_data_t key, st_data_t arg)
558{
559 VALUE *args = (VALUE*)arg;
560 VALUE str = args[0];
561 if (args[1] == Qtrue) {
562 rb_str_buf_cat_ascii(str, ", ");
563 }
564 else {
565 args[1] = Qtrue;
566 }
568
569 return ST_CONTINUE;
570}
571
572static VALUE
573set_inspect(VALUE set, VALUE dummy, int recur)
574{
575 VALUE str;
576 VALUE klass_name = rb_class_path(CLASS_OF(set));
577
578 if (recur) {
579 str = rb_sprintf("%"PRIsVALUE"[...]", klass_name);
580 return rb_str_export_to_enc(str, rb_usascii_encoding());
581 }
582
583 str = rb_sprintf("%"PRIsVALUE"[", klass_name);
584 VALUE args[2] = {str, Qfalse};
585 set_iter(set, set_inspect_i, (st_data_t)args);
586 rb_str_buf_cat2(str, "]");
587
588 return str;
589}
590
591/*
592 * call-seq:
593 * inspect -> new_string
594 *
595 * Returns a new string containing the set entries:
596 *
597 * s = Set.new
598 * s.inspect # => "Set[]"
599 * s.add(1)
600 * s.inspect # => "Set[1]"
601 * s.add(2)
602 * s.inspect # => "Set[1, 2]"
603 *
604 * Related: see {Methods for Converting}[rdoc-ref:Set@Methods+for+Converting].
605 */
606static VALUE
607set_i_inspect(VALUE set)
608{
609 return rb_exec_recursive(set_inspect, set, 0);
610}
611
612static int
613set_to_a_i(st_data_t key, st_data_t arg)
614{
615 rb_ary_push((VALUE)arg, (VALUE)key);
616 return ST_CONTINUE;
617}
618
619/*
620 * call-seq:
621 * to_a -> array
622 *
623 * Returns an array containing all elements in the set.
624 *
625 * Set[1, 2].to_a #=> [1, 2]
626 * Set[1, 'c', :s].to_a #=> [1, "c", :s]
627 */
628static VALUE
629set_i_to_a(VALUE set)
630{
631 st_index_t size = RSET_SIZE(set);
632 VALUE ary = rb_ary_new_capa(size);
633
634 if (size == 0) return ary;
635
636 if (ST_DATA_COMPATIBLE_P(VALUE)) {
637 RARRAY_PTR_USE(ary, ptr, {
638 size = set_keys(RSET_TABLE(set), ptr, size);
639 });
640 rb_gc_writebarrier_remember(ary);
641 rb_ary_set_len(ary, size);
642 }
643 else {
644 set_iter(set, set_to_a_i, (st_data_t)ary);
645 }
646 return ary;
647}
648
649/*
650 * call-seq:
651 * to_set(&block) -> self or new_set
652 *
653 * Without a block, if +self+ is an instance of +Set+, returns +self+.
654 * Otherwise, calls <tt>Set.new(self, &block)</tt>.
655 *
656 * A form with arguments is _deprecated_. It converts the set to another
657 * with <tt>klass.new(self, *args, &block)</tt>.
658 */
659static VALUE
660set_i_to_set(VALUE set)
661{
663 return set;
664 }
665
666 return rb_funcall_passing_block(rb_cSet, id_new, 0, NULL);
667}
668
669/*
670 * call-seq:
671 * join(separator=nil)-> new_string
672 *
673 * Returns a string created by converting each element of the set to a string.
674 */
675static VALUE
676set_i_join(int argc, VALUE *argv, VALUE set)
677{
678 rb_check_arity(argc, 0, 1);
679 return rb_ary_join(set_i_to_a(set), argc == 0 ? Qnil : argv[0]);
680}
681
682/*
683 * call-seq:
684 * add(obj) -> self
685 *
686 * Adds the given object to the set and returns self. Use Set#merge to
687 * add many elements at once.
688 *
689 * Set[1, 2].add(3) #=> Set[1, 2, 3]
690 * Set[1, 2].add([3, 4]) #=> Set[1, 2, [3, 4]]
691 * Set[1, 2].add(2) #=> Set[1, 2]
692 */
693static VALUE
694set_i_add(VALUE set, VALUE item)
695{
696 rb_check_frozen(set);
697 if (set_iterating_p(set)) {
698 if (!set_table_lookup(RSET_TABLE(set), (st_data_t)item)) {
699 no_new_item();
700 }
701 }
702 else {
703 set_insert_wb(set, item, NULL);
704 }
705 return set;
706}
707
708/*
709 * call-seq:
710 * add?(obj) -> self or nil
711 *
712 * Adds the given object to the set and returns self. If the object is
713 * already in the set, returns nil.
714 *
715 * Set[1, 2].add?(3) #=> Set[1, 2, 3]
716 * Set[1, 2].add?([3, 4]) #=> Set[1, 2, [3, 4]]
717 * Set[1, 2].add?(2) #=> nil
718 */
719static VALUE
720set_i_add_p(VALUE set, VALUE item)
721{
722 rb_check_frozen(set);
723 if (set_iterating_p(set)) {
724 if (!set_table_lookup(RSET_TABLE(set), (st_data_t)item)) {
725 no_new_item();
726 }
727 return Qnil;
728 }
729 else {
730 return set_insert_wb(set, item, NULL) ? Qnil : set;
731 }
732}
733
734/*
735 * call-seq:
736 * delete(obj) -> self
737 *
738 * Deletes the given object from the set and returns self. Use subtract
739 * to delete many items at once.
740 */
741static VALUE
742set_i_delete(VALUE set, VALUE item)
743{
744 rb_check_frozen(set);
745 if (set_table_delete(RSET_TABLE(set), (st_data_t *)&item)) {
746 set_compact_after_delete(set);
747 }
748 return set;
749}
750
751/*
752 * call-seq:
753 * delete?(obj) -> self or nil
754 *
755 * Deletes the given object from the set and returns self. If the
756 * object is not in the set, returns nil.
757 */
758static VALUE
759set_i_delete_p(VALUE set, VALUE item)
760{
761 rb_check_frozen(set);
762 if (set_table_delete(RSET_TABLE(set), (st_data_t *)&item)) {
763 set_compact_after_delete(set);
764 return set;
765 }
766 return Qnil;
767}
768
769static int
770set_delete_if_i(st_data_t key, st_data_t dummy)
771{
772 return RTEST(rb_yield((VALUE)key)) ? ST_DELETE : ST_CONTINUE;
773}
774
775/*
776 * call-seq:
777 * delete_if { |o| ... } -> self
778 * delete_if -> enumerator
779 *
780 * Deletes every element of the set for which block evaluates to
781 * true, and returns self. Returns an enumerator if no block is given.
782 */
783static VALUE
784set_i_delete_if(VALUE set)
785{
786 RETURN_SIZED_ENUMERATOR(set, 0, 0, set_enum_size);
787 rb_check_frozen(set);
788 set_iter(set, set_delete_if_i, 0);
789 set_compact_after_delete(set);
790 return set;
791}
792
793/*
794 * call-seq:
795 * reject! { |o| ... } -> self
796 * reject! -> enumerator
797 *
798 * Equivalent to Set#delete_if, but returns nil if no changes were made.
799 * Returns an enumerator if no block is given.
800 */
801static VALUE
802set_i_reject(VALUE set)
803{
804 RETURN_SIZED_ENUMERATOR(set, 0, 0, set_enum_size);
805 rb_check_frozen(set);
806
807 set_table *table = RSET_TABLE(set);
808 size_t n = set_table_size(table);
809 set_iter(set, set_delete_if_i, 0);
810
811 if (n == set_table_size(table)) return Qnil;
812
813 set_compact_after_delete(set);
814 return set;
815}
816
817static int
818set_classify_i(st_data_t key, st_data_t tmp)
819{
820 VALUE* args = (VALUE*)tmp;
821 VALUE hash = args[0];
822 VALUE hash_key = rb_yield(key);
823 VALUE set = rb_hash_lookup2(hash, hash_key, Qundef);
824 if (set == Qundef) {
825 set = set_s_alloc(args[1]);
826 rb_hash_aset(hash, hash_key, set);
827 }
828 set_i_add(set, key);
829
830 return ST_CONTINUE;
831}
832
833/*
834 * call-seq:
835 * classify { |o| ... } -> hash
836 * classify -> enumerator
837 *
838 * Classifies the set by the return value of the given block and
839 * returns a hash of {value => set of elements} pairs. The block is
840 * called once for each element of the set, passing the element as
841 * parameter.
842 *
843 * files = Set.new(Dir.glob("*.rb"))
844 * hash = files.classify { |f| File.mtime(f).year }
845 * hash #=> {2000 => Set["a.rb", "b.rb"],
846 * # 2001 => Set["c.rb", "d.rb", "e.rb"],
847 * # 2002 => Set["f.rb"]}
848 *
849 * Returns an enumerator if no block is given.
850 */
851static VALUE
852set_i_classify(VALUE set)
853{
854 RETURN_SIZED_ENUMERATOR(set, 0, 0, set_enum_size);
855 VALUE args[2];
856 args[0] = rb_hash_new();
857 args[1] = rb_obj_class(set);
858 set_iter(set, set_classify_i, (st_data_t)args);
859 return args[0];
860}
861
862// Union-find with path compression
863static long
864set_divide_union_find_root(long *uf_parents, long index, long *tmp_array)
865{
866 long root = uf_parents[index];
867 long update_size = 0;
868 while (root != index) {
869 tmp_array[update_size++] = index;
870 index = root;
871 root = uf_parents[index];
872 }
873 for (long j = 0; j < update_size; j++) {
874 long idx = tmp_array[j];
875 uf_parents[idx] = root;
876 }
877 return root;
878}
879
880static void
881set_divide_union_find_merge(long *uf_parents, long i, long j, long *tmp_array)
882{
883 long root_i = set_divide_union_find_root(uf_parents, i, tmp_array);
884 long root_j = set_divide_union_find_root(uf_parents, j, tmp_array);
885 if (root_i != root_j) uf_parents[root_j] = root_i;
886}
887
888static VALUE
889set_divide_arity2(VALUE set)
890{
891 VALUE tmp, uf;
892 long size, *uf_parents, *tmp_array;
893 VALUE set_class = rb_obj_class(set);
894 VALUE items = set_i_to_a(set);
895 rb_ary_freeze(items);
896 size = RARRAY_LEN(items);
897 tmp_array = ALLOCV_N(long, tmp, size);
898 uf_parents = ALLOCV_N(long, uf, size);
899 for (long i = 0; i < size; i++) {
900 uf_parents[i] = i;
901 }
902 for (long i = 0; i < size - 1; i++) {
903 VALUE item1 = RARRAY_AREF(items, i);
904 for (long j = i + 1; j < size; j++) {
905 VALUE item2 = RARRAY_AREF(items, j);
906 if (RTEST(rb_yield_values(2, item1, item2)) &&
907 RTEST(rb_yield_values(2, item2, item1))) {
908 set_divide_union_find_merge(uf_parents, i, j, tmp_array);
909 }
910 }
911 }
912 VALUE final_set = set_s_create(0, 0, rb_cSet);
913 VALUE hash = rb_hash_new();
914 for (long i = 0; i < size; i++) {
915 VALUE v = RARRAY_AREF(items, i);
916 long root = set_divide_union_find_root(uf_parents, i, tmp_array);
917 VALUE set = rb_hash_aref(hash, LONG2FIX(root));
918 if (set == Qnil) {
919 set = set_s_create(0, 0, set_class);
920 rb_hash_aset(hash, LONG2FIX(root), set);
921 set_i_add(final_set, set);
922 }
923 set_i_add(set, v);
924 }
925 ALLOCV_END(tmp);
926 ALLOCV_END(uf);
927 return final_set;
928}
929
930static void set_merge_enum_into(VALUE set, VALUE arg);
931
932/*
933 * call-seq:
934 * divide { |o1, o2| ... } -> set
935 * divide { |o| ... } -> set
936 * divide -> enumerator
937 *
938 * Divides the set into a set of subsets according to the commonality
939 * defined by the given block.
940 *
941 * If the arity of the block is 2, elements o1 and o2 are in common
942 * if both block.call(o1, o2) and block.call(o2, o1) are true.
943 * Otherwise, elements o1 and o2 are in common if
944 * block.call(o1) == block.call(o2).
945 *
946 * numbers = Set[1, 3, 4, 6, 9, 10, 11]
947 * set = numbers.divide { |i,j| (i - j).abs == 1 }
948 * set #=> Set[Set[1],
949 * # Set[3, 4],
950 * # Set[6],
951 * # Set[9, 10, 11]]
952 *
953 * Returns an enumerator if no block is given.
954 */
955static VALUE
956set_i_divide(VALUE set)
957{
958 RETURN_SIZED_ENUMERATOR(set, 0, 0, set_enum_size);
959
960 if (rb_block_arity() == 2) {
961 return set_divide_arity2(set);
962 }
963
964 VALUE values = rb_hash_values(set_i_classify(set));
965 set = set_alloc_with_size(rb_cSet, RARRAY_LEN(values));
966 set_merge_enum_into(set, values);
967 return set;
968}
969
970static int
971set_clear_i(st_data_t key, st_data_t dummy)
972{
973 return ST_DELETE;
974}
975
976/*
977 * call-seq:
978 * clear -> self
979 *
980 * Removes all elements and returns self.
981 *
982 * set = Set[1, 'c', :s] #=> Set[1, "c", :s]
983 * set.clear #=> Set[]
984 * set #=> Set[]
985 */
986static VALUE
987set_i_clear(VALUE set)
988{
989 rb_check_frozen(set);
990 if (RSET_SIZE(set) == 0) return set;
991 if (set_iterating_p(set)) {
992 set_iter(set, set_clear_i, 0);
993 }
994 else {
995 set_table_clear(RSET_TABLE(set));
996 set_compact_after_delete(set);
997 }
998 return set;
999}
1000
1002 VALUE set;
1003 set_table *into;
1004 set_table *other;
1005};
1006
1007static int
1008set_intersection_i(st_data_t key, st_data_t tmp)
1009{
1010 struct set_intersection_data *data = (struct set_intersection_data *)tmp;
1011 if (set_table_lookup(data->other, key)) {
1012 set_table_insert_wb(data->into, data->set, key, NULL);
1013 }
1014
1015 return ST_CONTINUE;
1016}
1017
1018static VALUE
1019set_intersection_block(RB_BLOCK_CALL_FUNC_ARGLIST(i, data))
1020{
1021 set_intersection_i((st_data_t)i, (st_data_t)data);
1022 return i;
1023}
1024
1025/*
1026 * call-seq:
1027 * set & enum -> new_set
1028 *
1029 * Returns a new set containing elements common to the set and the given
1030 * enumerable object.
1031 *
1032 * Set[1, 3, 5] & Set[3, 2, 1] #=> Set[3, 1]
1033 * Set['a', 'b', 'z'] & ['a', 'b', 'c'] #=> Set["a", "b"]
1034 */
1035static VALUE
1036set_i_intersection(VALUE set, VALUE other)
1037{
1038 VALUE new_set = set_s_alloc(rb_obj_class(set));
1039 set_table *stable = RSET_TABLE(set);
1040 set_table *ntable = RSET_TABLE(new_set);
1041
1042 if (rb_obj_is_kind_of(other, rb_cSet)) {
1043 set_table *otable = RSET_TABLE(other);
1044 if (set_table_size(stable) >= set_table_size(otable)) {
1045 /* Swap so we iterate over the smaller set */
1046 otable = stable;
1047 set = other;
1048 }
1049
1050 struct set_intersection_data data = {
1051 .set = new_set,
1052 .into = ntable,
1053 .other = otable
1054 };
1055 set_iter(set, set_intersection_i, (st_data_t)&data);
1056 }
1057 else {
1058 struct set_intersection_data data = {
1059 .set = new_set,
1060 .into = ntable,
1061 .other = stable
1062 };
1063 rb_block_call(other, enum_method_id(other), 0, 0, set_intersection_block, (VALUE)&data);
1064 }
1065
1066 return new_set;
1067}
1068
1069/*
1070 * call-seq:
1071 * include?(item) -> true or false
1072 *
1073 * Returns true if the set contains the given object:
1074 *
1075 * Set[1, 2, 3].include? 2 #=> true
1076 * Set[1, 2, 3].include? 4 #=> false
1077 *
1078 * Note that <code>include?</code> and <code>member?</code> do not test member
1079 * equality using <code>==</code> as do other Enumerables.
1080 *
1081 * This is aliased to #===, so it is usable in +case+ expressions:
1082 *
1083 * case :apple
1084 * when Set[:potato, :carrot]
1085 * "vegetable"
1086 * when Set[:apple, :banana]
1087 * "fruit"
1088 * end
1089 * # => "fruit"
1090 *
1091 * See also Enumerable#include?
1092 */
1093static VALUE
1094set_i_include(VALUE set, VALUE item)
1095{
1096 return RBOOL(RSET_IS_MEMBER(set, item));
1097}
1098
1100 VALUE set;
1101 set_table *into;
1102};
1103
1104static int
1105set_merge_i(st_data_t key, st_data_t data)
1106{
1107 struct set_merge_args *args = (struct set_merge_args *)data;
1108 set_table_insert_wb(args->into, args->set, key, NULL);
1109 return ST_CONTINUE;
1110}
1111
1112static VALUE
1113set_merge_block(RB_BLOCK_CALL_FUNC_ARGLIST(key, set))
1114{
1115 VALUE element = key;
1116 set_insert_wb(set, element, &element);
1117 return element;
1118}
1119
1120static void
1121set_merge_enum_into(VALUE set, VALUE arg)
1122{
1123 if (rb_obj_is_kind_of(arg, rb_cSet)) {
1124 struct set_merge_args args = {
1125 .set = set,
1126 .into = RSET_TABLE(set)
1127 };
1128 set_iter(arg, set_merge_i, (st_data_t)&args);
1129 }
1130 else if (RB_TYPE_P(arg, T_ARRAY)) {
1131 long i;
1132 set_table *into = RSET_TABLE(set);
1133 for (i=0; i<RARRAY_LEN(arg); i++) {
1134 set_table_insert_wb(into, set, RARRAY_AREF(arg, i), NULL);
1135 }
1136 }
1137 else {
1138 rb_block_call(arg, enum_method_id(arg), 0, 0, set_merge_block, (VALUE)set);
1139 }
1140}
1141
1142/*
1143 * call-seq:
1144 * merge(*enums, **nil) -> self
1145 *
1146 * Merges the elements of the given enumerable objects to the set and
1147 * returns self.
1148 */
1149static VALUE
1150set_i_merge(int argc, VALUE *argv, VALUE set)
1151{
1152 if (rb_keyword_given_p()) {
1153 rb_raise(rb_eArgError, "no keywords accepted");
1154 }
1155
1156 if (set_iterating_p(set)) {
1157 rb_raise(rb_eRuntimeError, "cannot add to set during iteration");
1158 }
1159
1160 rb_check_frozen(set);
1161
1162 int i;
1163
1164 for (i=0; i < argc; i++) {
1165 set_merge_enum_into(set, argv[i]);
1166 }
1167
1168 return set;
1169}
1170
1171static VALUE
1172set_reset_table_with_type(VALUE set, const struct st_hash_type *type)
1173{
1174 rb_check_frozen(set);
1175
1176 struct set_object *sobj;
1177 TypedData_Get_Struct(set, struct set_object, &set_data_type, sobj);
1178 set_table *old = &sobj->table;
1179
1180 size_t size = set_table_size(old);
1181 if (size > 0) {
1182 set_table *new = set_init_table_with_size(NULL, type, size);
1183 struct set_merge_args args = {
1184 .set = set,
1185 .into = new
1186 };
1187 set_iter(set, set_merge_i, (st_data_t)&args);
1188 set_free_embedded(sobj);
1189 memcpy(&sobj->table, new, sizeof(*new));
1190 free(new);
1191 }
1192 else {
1193 sobj->table.type = type;
1194 }
1195
1196 return set;
1197}
1198
1199/*
1200 * call-seq:
1201 * compare_by_identity -> self
1202 *
1203 * Makes the set compare its elements by their identity and returns self.
1204 */
1205static VALUE
1206set_i_compare_by_identity(VALUE set)
1207{
1208 if (RSET_COMPARE_BY_IDENTITY(set)) return set;
1209
1210 if (set_iterating_p(set)) {
1211 rb_raise(rb_eRuntimeError, "compare_by_identity during iteration");
1212 }
1213
1214 return set_reset_table_with_type(set, &identhash);
1215}
1216
1217/*
1218 * call-seq:
1219 * compare_by_identity? -> true or false
1220 *
1221 * Returns true if the set will compare its elements by their
1222 * identity. Also see Set#compare_by_identity.
1223 */
1224static VALUE
1225set_i_compare_by_identity_p(VALUE set)
1226{
1227 return RBOOL(RSET_COMPARE_BY_IDENTITY(set));
1228}
1229
1230/*
1231 * call-seq:
1232 * size -> integer
1233 *
1234 * Returns the number of elements.
1235 */
1236static VALUE
1237set_i_size(VALUE set)
1238{
1239 return RSET_SIZE_NUM(set);
1240}
1241
1242/*
1243 * call-seq:
1244 * empty? -> true or false
1245 *
1246 * Returns true if the set contains no elements.
1247 */
1248static VALUE
1249set_i_empty(VALUE set)
1250{
1251 return RBOOL(RSET_EMPTY(set));
1252}
1253
1254static int
1255set_xor_i(st_data_t key, st_data_t data)
1256{
1257 VALUE element = (VALUE)key;
1258 VALUE set = (VALUE)data;
1259 set_table *table = RSET_TABLE(set);
1260 if (set_table_insert_wb(table, set, element, &element)) {
1261 set_table_delete(table, &element);
1262 }
1263 return ST_CONTINUE;
1264}
1265
1266/*
1267 * call-seq:
1268 * set ^ enum -> new_set
1269 *
1270 * Returns a new set containing elements exclusive between the set and the
1271 * given enumerable object. <tt>(set ^ enum)</tt> is equivalent to
1272 * <tt>((set | enum) - (set & enum))</tt>.
1273 *
1274 * Set[1, 2] ^ Set[2, 3] #=> Set[3, 1]
1275 * Set[1, 'b', 'c'] ^ ['b', 'd'] #=> Set["d", 1, "c"]
1276 */
1277static VALUE
1278set_i_xor(VALUE set, VALUE other)
1279{
1280 VALUE new_set = rb_obj_dup(set);
1281
1282 if (rb_obj_is_kind_of(other, rb_cSet)) {
1283 set_iter(other, set_xor_i, (st_data_t)new_set);
1284 }
1285 else {
1286 VALUE tmp = set_s_alloc(rb_cSet);
1287 set_merge_enum_into(tmp, other);
1288 set_iter(tmp, set_xor_i, (st_data_t)new_set);
1289 }
1290
1291 return new_set;
1292}
1293
1294/*
1295 * call-seq:
1296 * set | enum -> new_set
1297 *
1298 * Returns a new set built by merging the set and the elements of the
1299 * given enumerable object.
1300 *
1301 * Set[1, 2, 3] | Set[2, 4, 5] #=> Set[1, 2, 3, 4, 5]
1302 * Set[1, 5, 'z'] | (1..6) #=> Set[1, 5, "z", 2, 3, 4, 6]
1303 */
1304static VALUE
1305set_i_union(VALUE set, VALUE other)
1306{
1307 set = rb_obj_dup(set);
1308 set_merge_enum_into(set, other);
1309 return set;
1310}
1311
1312static int
1313set_remove_i(st_data_t key, st_data_t from)
1314{
1315 set_table_delete((struct set_table *)from, (st_data_t *)&key);
1316 return ST_CONTINUE;
1317}
1318
1319static VALUE
1320set_remove_block(RB_BLOCK_CALL_FUNC_ARGLIST(key, set))
1321{
1322 rb_check_frozen(set);
1323 set_table_delete(RSET_TABLE(set), (st_data_t *)&key);
1324 return key;
1325}
1326
1327static void
1328set_remove_enum_from(VALUE set, VALUE arg)
1329{
1330 if (rb_obj_is_kind_of(arg, rb_cSet)) {
1331 set_iter(arg, set_remove_i, (st_data_t)RSET_TABLE(set));
1332 }
1333 else {
1334 rb_block_call(arg, enum_method_id(arg), 0, 0, set_remove_block, (VALUE)set);
1335 }
1336}
1337
1338/*
1339 * call-seq:
1340 * subtract(enum) -> self
1341 *
1342 * Deletes every element that appears in the given enumerable object
1343 * and returns self.
1344 */
1345static VALUE
1346set_i_subtract(VALUE set, VALUE other)
1347{
1348 rb_check_frozen(set);
1349 set_remove_enum_from(set, other);
1350 return set;
1351}
1352
1353/*
1354 * call-seq:
1355 * set - enum -> new_set
1356 *
1357 * Returns a new set built by duplicating the set, removing every
1358 * element that appears in the given enumerable object.
1359 *
1360 * Set[1, 3, 5] - Set[1, 5] #=> Set[3]
1361 * Set['a', 'b', 'z'] - ['a', 'c'] #=> Set["b", "z"]
1362 */
1363static VALUE
1364set_i_difference(VALUE set, VALUE other)
1365{
1366 return set_i_subtract(rb_obj_dup(set), other);
1367}
1368
1369static int
1370set_each_i(st_data_t key, st_data_t dummy)
1371{
1372 rb_yield(key);
1373 return ST_CONTINUE;
1374}
1375
1376/*
1377 * call-seq:
1378 * each { |o| ... } -> self
1379 * each -> enumerator
1380 *
1381 * Calls the given block once for each element in the set, passing
1382 * the element as parameter. Returns an enumerator if no block is
1383 * given.
1384 */
1385static VALUE
1386set_i_each(VALUE set)
1387{
1388 RETURN_SIZED_ENUMERATOR(set, 0, 0, set_enum_size);
1389 set_iter(set, set_each_i, 0);
1390 return set;
1391}
1392
1393static int
1394set_collect_i(st_data_t key, st_data_t data)
1395{
1396 set_insert_wb((VALUE)data, rb_yield((VALUE)key), NULL);
1397 return ST_CONTINUE;
1398}
1399
1400/*
1401 * call-seq:
1402 * collect! { |o| ... } -> self
1403 * collect! -> enumerator
1404 *
1405 * Replaces the elements with ones returned by +collect+.
1406 * Returns an enumerator if no block is given.
1407 */
1408static VALUE
1409set_i_collect(VALUE set)
1410{
1411 RETURN_SIZED_ENUMERATOR(set, 0, 0, set_enum_size);
1412 rb_check_frozen(set);
1413
1414 VALUE new_set = set_s_alloc(rb_obj_class(set));
1415 set_iter(set, set_collect_i, (st_data_t)new_set);
1416 set_i_initialize_copy(set, new_set);
1417
1418 return set;
1419}
1420
1421static int
1422set_keep_if_i(st_data_t key, st_data_t into)
1423{
1424 if (!RTEST(rb_yield((VALUE)key))) {
1425 set_table_delete((set_table *)into, &key);
1426 }
1427 return ST_CONTINUE;
1428}
1429
1430/*
1431 * call-seq:
1432 * keep_if { |o| ... } -> self
1433 * keep_if -> enumerator
1434 *
1435 * Deletes every element of the set for which block evaluates to false, and
1436 * returns self. Returns an enumerator if no block is given.
1437 */
1438static VALUE
1439set_i_keep_if(VALUE set)
1440{
1441 RETURN_SIZED_ENUMERATOR(set, 0, 0, set_enum_size);
1442 rb_check_frozen(set);
1443
1444 set_iter(set, set_keep_if_i, (st_data_t)RSET_TABLE(set));
1445
1446 return set;
1447}
1448
1449/*
1450 * call-seq:
1451 * select! { |o| ... } -> self
1452 * select! -> enumerator
1453 *
1454 * Equivalent to Set#keep_if, but returns nil if no changes were made.
1455 * Returns an enumerator if no block is given.
1456 */
1457static VALUE
1458set_i_select(VALUE set)
1459{
1460 RETURN_SIZED_ENUMERATOR(set, 0, 0, set_enum_size);
1461 rb_check_frozen(set);
1462
1463 set_table *table = RSET_TABLE(set);
1464 size_t n = set_table_size(table);
1465 set_iter(set, set_keep_if_i, (st_data_t)table);
1466
1467 return (n == set_table_size(table)) ? Qnil : set;
1468}
1469
1470/*
1471 * call-seq:
1472 * replace(enum) -> self
1473 *
1474 * Replaces the contents of the set with the contents of the given
1475 * enumerable object and returns self.
1476 *
1477 * set = Set[1, 'c', :s] #=> Set[1, "c", :s]
1478 * set.replace([1, 2]) #=> Set[1, 2]
1479 * set #=> Set[1, 2]
1480 */
1481static VALUE
1482set_i_replace(VALUE set, VALUE other)
1483{
1484 rb_check_frozen(set);
1485
1486 if (rb_obj_is_kind_of(other, rb_cSet)) {
1487 set_i_initialize_copy(set, other);
1488 }
1489 else {
1490 if (set_iterating_p(set)) {
1491 rb_raise(rb_eRuntimeError, "cannot replace set during iteration");
1492 }
1493
1494 // make sure enum is enumerable before calling clear
1495 enum_method_id(other);
1496
1497 set_table_clear(RSET_TABLE(set));
1498 set_merge_enum_into(set, other);
1499 }
1500
1501 return set;
1502}
1503
1504/*
1505 * call-seq:
1506 * reset -> self
1507 *
1508 * Resets the internal state after modification to existing elements
1509 * and returns self. Elements will be reindexed and deduplicated.
1510 */
1511static VALUE
1512set_i_reset(VALUE set)
1513{
1514 if (set_iterating_p(set)) {
1515 rb_raise(rb_eRuntimeError, "reset during iteration");
1516 }
1517
1518 return set_reset_table_with_type(set, RSET_TABLE(set)->type);
1519}
1520
1521static void set_flatten_merge(VALUE set, VALUE from, VALUE seen);
1522
1523static int
1524set_flatten_merge_i(st_data_t item, st_data_t arg)
1525{
1526 VALUE *args = (VALUE *)arg;
1527 VALUE set = args[0];
1528 if (rb_obj_is_kind_of(item, rb_cSet)) {
1529 VALUE e_id = rb_obj_id(item);
1530 VALUE hash = args[2];
1531 switch(rb_hash_aref(hash, e_id)) {
1532 case Qfalse:
1533 return ST_CONTINUE;
1534 case Qtrue:
1535 rb_raise(rb_eArgError, "tried to flatten recursive Set");
1536 default:
1537 break;
1538 }
1539
1540 rb_hash_aset(hash, e_id, Qtrue);
1541 set_flatten_merge(set, item, hash);
1542 rb_hash_aset(hash, e_id, Qfalse);
1543 }
1544 else {
1545 set_i_add(set, item);
1546 }
1547 return ST_CONTINUE;
1548}
1549
1550static void
1551set_flatten_merge(VALUE set, VALUE from, VALUE hash)
1552{
1553 VALUE args[3] = {set, from, hash};
1554 set_iter(from, set_flatten_merge_i, (st_data_t)args);
1555}
1556
1557/*
1558 * call-seq:
1559 * flatten -> set
1560 *
1561 * Returns a new set that is a copy of the set, flattening each
1562 * containing set recursively.
1563 */
1564static VALUE
1565set_i_flatten(VALUE set)
1566{
1567 VALUE new_set = set_s_alloc(rb_obj_class(set));
1568 set_flatten_merge(new_set, set, rb_hash_new());
1569 return new_set;
1570}
1571
1572static int
1573set_contains_set_i(st_data_t item, st_data_t arg)
1574{
1575 if (rb_obj_is_kind_of(item, rb_cSet)) {
1576 *(bool *)arg = true;
1577 return ST_STOP;
1578 }
1579 return ST_CONTINUE;
1580}
1581
1582/*
1583 * call-seq:
1584 * flatten! -> self
1585 *
1586 * Equivalent to Set#flatten, but replaces the receiver with the
1587 * result in place. Returns nil if no modifications were made.
1588 */
1589static VALUE
1590set_i_flatten_bang(VALUE set)
1591{
1592 bool contains_set = false;
1593 set_iter(set, set_contains_set_i, (st_data_t)&contains_set);
1594 if (!contains_set) return Qnil;
1595 rb_check_frozen(set);
1596 return set_i_replace(set, set_i_flatten(set));
1597}
1598
1600 set_table *table;
1601 VALUE result;
1602};
1603
1604static int
1605set_le_i(st_data_t key, st_data_t arg)
1606{
1607 struct set_subset_data *data = (struct set_subset_data *)arg;
1608 if (set_table_lookup(data->table, key)) return ST_CONTINUE;
1609 data->result = Qfalse;
1610 return ST_STOP;
1611}
1612
1613static VALUE
1614set_le(VALUE set, VALUE other)
1615{
1616 struct set_subset_data data = {
1617 .table = RSET_TABLE(other),
1618 .result = Qtrue
1619 };
1620 set_iter(set, set_le_i, (st_data_t)&data);
1621 return data.result;
1622}
1623
1624/*
1625 * call-seq:
1626 * proper_subset?(set) -> true or false
1627 *
1628 * Returns true if the set is a proper subset of the given set.
1629 */
1630static VALUE
1631set_i_proper_subset(VALUE set, VALUE other)
1632{
1633 check_set(other);
1634 if (RSET_SIZE(set) >= RSET_SIZE(other)) return Qfalse;
1635 return set_le(set, other);
1636}
1637
1638/*
1639 * call-seq:
1640 * subset?(set) -> true or false
1641 *
1642 * Returns true if the set is a subset of the given set.
1643 */
1644static VALUE
1645set_i_subset(VALUE set, VALUE other)
1646{
1647 check_set(other);
1648 if (RSET_SIZE(set) > RSET_SIZE(other)) return Qfalse;
1649 return set_le(set, other);
1650}
1651
1652/*
1653 * call-seq:
1654 * proper_superset?(set) -> true or false
1655 *
1656 * Returns true if the set is a proper superset of the given set.
1657 */
1658static VALUE
1659set_i_proper_superset(VALUE set, VALUE other)
1660{
1661 check_set(other);
1662 if (RSET_SIZE(set) <= RSET_SIZE(other)) return Qfalse;
1663 return set_le(other, set);
1664}
1665
1666/*
1667 * call-seq:
1668 * superset?(set) -> true or false
1669 *
1670 * Returns true if the set is a superset of the given set.
1671 */
1672static VALUE
1673set_i_superset(VALUE set, VALUE other)
1674{
1675 check_set(other);
1676 if (RSET_SIZE(set) < RSET_SIZE(other)) return Qfalse;
1677 return set_le(other, set);
1678}
1679
1680static int
1681set_intersect_i(st_data_t key, st_data_t arg)
1682{
1683 VALUE *args = (VALUE *)arg;
1684 if (set_table_lookup((set_table *)args[0], key)) {
1685 args[1] = Qtrue;
1686 return ST_STOP;
1687 }
1688 return ST_CONTINUE;
1689}
1690
1691/*
1692 * call-seq:
1693 * intersect?(set) -> true or false
1694 *
1695 * Returns true if the set and the given enumerable have at least one
1696 * element in common.
1697 *
1698 * Set[1, 2, 3].intersect? Set[4, 5] #=> false
1699 * Set[1, 2, 3].intersect? Set[3, 4] #=> true
1700 * Set[1, 2, 3].intersect? 4..5 #=> false
1701 * Set[1, 2, 3].intersect? [3, 4] #=> true
1702 */
1703static VALUE
1704set_i_intersect(VALUE set, VALUE other)
1705{
1706 if (rb_obj_is_kind_of(other, rb_cSet)) {
1707 size_t set_size = RSET_SIZE(set);
1708 size_t other_size = RSET_SIZE(other);
1709 VALUE args[2];
1710 args[1] = Qfalse;
1711 VALUE iter_arg;
1712
1713 if (set_size < other_size) {
1714 iter_arg = set;
1715 args[0] = (VALUE)RSET_TABLE(other);
1716 }
1717 else {
1718 iter_arg = other;
1719 args[0] = (VALUE)RSET_TABLE(set);
1720 }
1721 set_iter(iter_arg, set_intersect_i, (st_data_t)args);
1722 return args[1];
1723 }
1724 else if (rb_obj_is_kind_of(other, rb_mEnumerable)) {
1725 return rb_funcall(other, id_any_p, 1, set);
1726 }
1727 else {
1728 rb_raise(rb_eArgError, "value must be enumerable");
1729 }
1730}
1731
1732/*
1733 * call-seq:
1734 * disjoint?(set) -> true or false
1735 *
1736 * Returns true if the set and the given enumerable have no
1737 * element in common. This method is the opposite of +intersect?+.
1738 *
1739 * Set[1, 2, 3].disjoint? Set[3, 4] #=> false
1740 * Set[1, 2, 3].disjoint? Set[4, 5] #=> true
1741 * Set[1, 2, 3].disjoint? [3, 4] #=> false
1742 * Set[1, 2, 3].disjoint? 4..5 #=> true
1743 */
1744static VALUE
1745set_i_disjoint(VALUE set, VALUE other)
1746{
1747 return RBOOL(!RTEST(set_i_intersect(set, other)));
1748}
1749
1750/*
1751 * call-seq:
1752 * set <=> other -> -1, 0, 1, or nil
1753 *
1754 * Returns 0 if the set are equal, -1 / 1 if the set is a
1755 * proper subset / superset of the given set, or nil if
1756 * they both have unique elements.
1757 */
1758static VALUE
1759set_i_compare(VALUE set, VALUE other)
1760{
1761 if (rb_obj_is_kind_of(other, rb_cSet)) {
1762 size_t set_size = RSET_SIZE(set);
1763 size_t other_size = RSET_SIZE(other);
1764
1765 if (set_size < other_size) {
1766 if (set_le(set, other) == Qtrue) {
1767 return INT2NUM(-1);
1768 }
1769 }
1770 else if (set_size > other_size) {
1771 if (set_le(other, set) == Qtrue) {
1772 return INT2NUM(1);
1773 }
1774 }
1775 else if (set_le(set, other) == Qtrue) {
1776 return INT2NUM(0);
1777 }
1778 }
1779
1780 return Qnil;
1781}
1782
1784 VALUE result;
1785 VALUE set;
1786};
1787
1788static int
1789set_eql_i(st_data_t item, st_data_t arg)
1790{
1791 struct set_equal_data *data = (struct set_equal_data *)arg;
1792
1793 if (!set_table_lookup(RSET_TABLE(data->set), item)) {
1794 data->result = Qfalse;
1795 return ST_STOP;
1796 }
1797 return ST_CONTINUE;
1798}
1799
1800static VALUE
1801set_recursive_eql(VALUE set, VALUE dt, int recur)
1802{
1803 if (recur) return Qtrue;
1804 struct set_equal_data *data = (struct set_equal_data*)dt;
1805 data->result = Qtrue;
1806 set_iter(set, set_eql_i, dt);
1807 return data->result;
1808}
1809
1810/*
1811 * call-seq:
1812 * set == other -> true or false
1813 *
1814 * Returns true if two sets are equal.
1815 */
1816static VALUE
1817set_i_eq(VALUE set, VALUE other)
1818{
1819 if (!rb_obj_is_kind_of(other, rb_cSet)) return Qfalse;
1820 if (set == other) return Qtrue;
1821
1822 set_table *stable = RSET_TABLE(set);
1823 set_table *otable = RSET_TABLE(other);
1824 size_t ssize = set_table_size(stable);
1825 size_t osize = set_table_size(otable);
1826
1827 if (ssize != osize) return Qfalse;
1828 if (ssize == 0 && osize == 0) return Qtrue;
1829 if (stable->type != otable->type) return Qfalse;
1830
1831 struct set_equal_data data;
1832 data.set = other;
1833 return rb_exec_recursive_paired(set_recursive_eql, set, other, (VALUE)&data);
1834}
1835
1836static int
1837set_hash_i(st_data_t item, st_data_t(arg))
1838{
1839 st_index_t *hval = (st_index_t *)arg;
1840 st_index_t ival = rb_hash(item);
1841 *hval ^= rb_st_hash(&ival, sizeof(st_index_t), 0);
1842 return ST_CONTINUE;
1843}
1844
1845/*
1846 * call-seq:
1847 * hash -> integer
1848 *
1849 * Returns hash code for set.
1850 */
1851static VALUE
1852set_i_hash(VALUE set)
1853{
1854 st_index_t size = RSET_SIZE(set);
1855 st_index_t hval = rb_st_hash_start(size);
1856 hval = rb_hash_uint(hval, (st_index_t)set_i_hash);
1857 if (size) {
1858 set_iter(set, set_hash_i, (VALUE)&hval);
1859 }
1860 hval = rb_st_hash_end(hval);
1861 return ST2FIX(hval);
1862}
1863
1864/* :nodoc: */
1865static int
1866set_to_hash_i(st_data_t key, st_data_t arg)
1867{
1868 rb_hash_aset((VALUE)arg, (VALUE)key, Qtrue);
1869 return ST_CONTINUE;
1870}
1871
1872static VALUE
1873set_i_to_h(VALUE set)
1874{
1875 st_index_t size = RSET_SIZE(set);
1876 VALUE hash;
1877 if (RSET_COMPARE_BY_IDENTITY(set)) {
1878 hash = rb_ident_hash_new_with_size(size);
1879 }
1880 else {
1881 hash = rb_hash_new_with_size(size);
1882 }
1883 rb_hash_set_default(hash, Qfalse);
1884
1885 if (size == 0) return hash;
1886
1887 set_iter(set, set_to_hash_i, (st_data_t)hash);
1888 return hash;
1889}
1890
1891static VALUE
1892compat_dumper(VALUE set)
1893{
1894 VALUE dumper = rb_class_new_instance(0, 0, rb_cObject);
1895 rb_ivar_set(dumper, id_i_hash, set_i_to_h(set));
1896 return dumper;
1897}
1898
1899static int
1900set_i_from_hash_i(st_data_t key, st_data_t val, st_data_t set)
1901{
1902 if ((VALUE)val != Qtrue) {
1903 rb_raise(rb_eRuntimeError, "expect true as Set value: %"PRIsVALUE, rb_obj_class((VALUE)val));
1904 }
1905 set_i_add((VALUE)set, (VALUE)key);
1906 return ST_CONTINUE;
1907}
1908
1909static VALUE
1910set_i_from_hash(VALUE set, VALUE hash)
1911{
1912 Check_Type(hash, T_HASH);
1913 if (rb_hash_compare_by_id_p(hash)) set_i_compare_by_identity(set);
1914 rb_hash_stlike_foreach(hash, set_i_from_hash_i, (st_data_t)set);
1915 return set;
1916}
1917
1918static VALUE
1919compat_loader(VALUE self, VALUE a)
1920{
1921 return set_i_from_hash(self, rb_ivar_get(a, id_i_hash));
1922}
1923
1924/* C-API functions */
1925
1926void
1927rb_set_foreach(VALUE set, int (*func)(VALUE element, VALUE arg), VALUE arg)
1928{
1929 set_iter(set, func, arg);
1930}
1931
1932VALUE
1934{
1935 return set_alloc_with_size(rb_cSet, 0);
1936}
1937
1938VALUE
1940{
1941 return set_alloc_with_size(rb_cSet, (st_index_t)capa);
1942}
1943
1944bool
1946{
1947 return RSET_IS_MEMBER(set, element);
1948}
1949
1950bool
1952{
1953 return set_i_add_p(set, element) != Qnil;
1954}
1955
1956VALUE
1958{
1959 return set_i_clear(set);
1960}
1961
1962bool
1964{
1965 return set_i_delete_p(set, element) != Qnil;
1966}
1967
1968size_t
1970{
1971 return RSET_SIZE(set);
1972}
1973
1974/*
1975 * Document-class: Set
1976 *
1977 * The Set class implements a collection of unordered values with no
1978 * duplicates. It is a hybrid of Array's intuitive inter-operation
1979 * facilities and Hash's fast lookup.
1980 *
1981 * Set is easy to use with Enumerable objects (implementing #each).
1982 * Most of the initializer methods and binary operators accept generic
1983 * Enumerable objects besides sets and arrays. An Enumerable object
1984 * can be converted to Set using the +to_set+ method.
1985 *
1986 * Set uses a data structure similar to Hash for storage, except that
1987 * it only has keys and no values.
1988 *
1989 * * Equality of elements is determined according to Object#eql? and
1990 * Object#hash. Use Set#compare_by_identity to make a set compare
1991 * its elements by their identity.
1992 * * Set assumes that the identity of each element does not change
1993 * while it is stored. Modifying an element of a set will render the
1994 * set to an unreliable state.
1995 * * When a string is to be stored, a frozen copy of the string is
1996 * stored instead unless the original string is already frozen.
1997 *
1998 * == Comparison
1999 *
2000 * The comparison operators <tt><</tt>, <tt>></tt>, <tt><=</tt>, and
2001 * <tt>>=</tt> are implemented as shorthand for the
2002 * {proper_,}{subset?,superset?} methods. The <tt><=></tt>
2003 * operator reflects this order, or returns +nil+ for sets that both
2004 * have distinct elements (<tt>{x, y}</tt> vs. <tt>{x, z}</tt> for example).
2005 *
2006 * == Example
2007 *
2008 * s1 = Set[1, 2] #=> Set[1, 2]
2009 * s2 = [1, 2].to_set #=> Set[1, 2]
2010 * s1 == s2 #=> true
2011 * s1.add("foo") #=> Set[1, 2, "foo"]
2012 * s1.merge([2, 6]) #=> Set[1, 2, "foo", 6]
2013 * s1.subset?(s2) #=> false
2014 * s2.subset?(s1) #=> true
2015 *
2016 * == Contact
2017 *
2018 * - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
2019 *
2020 * == Inheriting from \Set
2021 *
2022 * Before Ruby 4.0 (released December 2025), \Set had a different, less
2023 * efficient implementation. It was reimplemented in C, and the behavior
2024 * of some of the core methods were adjusted.
2025 *
2026 * To keep backward compatibility, when a class is inherited from \Set,
2027 * additional module +Set::SubclassCompatible+ is included, which makes
2028 * the inherited class behavior, as well as internal method names,
2029 * closer to what it was before Ruby 4.0.
2030 *
2031 * It can be easily seen, for example, in the #inspect method behavior:
2032 *
2033 * p Set[1, 2, 3]
2034 * # prints "Set[1, 2, 3]"
2035 *
2036 * class MySet < Set
2037 * end
2038 * p MySet[1, 2, 3]
2039 * # prints "#<MySet: {1, 2, 3}>", like it was in Ruby 3.4
2040 *
2041 * For new code, if backward compatibility is not necessary,
2042 * it is recommended to instead inherit from +Set::CoreSet+, which
2043 * avoids including the "compatibility" layer:
2044 *
2045 * class MyCoreSet < Set::CoreSet
2046 * end
2047 * p MyCoreSet[1, 2, 3]
2048 * # prints "MyCoreSet[1, 2, 3]"
2049 *
2050 * == Set's methods
2051 *
2052 * First, what's elsewhere. \Class \Set:
2053 *
2054 * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here].
2055 * - Includes {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here],
2056 * which provides dozens of additional methods.
2057 *
2058 * In particular, class \Set does not have many methods of its own
2059 * for fetching or for iterating.
2060 * Instead, it relies on those in \Enumerable.
2061 *
2062 * Here, class \Set provides methods that are useful for:
2063 *
2064 * - {Creating a Set}[rdoc-ref:Set@Methods+for+Creating+a+Set]
2065 * - {Set Operations}[rdoc-ref:Set@Methods+for+Set+Operations]
2066 * - {Comparing}[rdoc-ref:Set@Methods+for+Comparing]
2067 * - {Querying}[rdoc-ref:Set@Methods+for+Querying]
2068 * - {Assigning}[rdoc-ref:Set@Methods+for+Assigning]
2069 * - {Deleting}[rdoc-ref:Set@Methods+for+Deleting]
2070 * - {Converting}[rdoc-ref:Set@Methods+for+Converting]
2071 * - {Iterating}[rdoc-ref:Set@Methods+for+Iterating]
2072 * - {And more....}[rdoc-ref:Set@Other+Methods]
2073 *
2074 * === Methods for Creating a \Set
2075 *
2076 * - ::[]:
2077 * Returns a new set containing the given objects.
2078 * - ::new:
2079 * Returns a new set containing either the given objects
2080 * (if no block given) or the return values from the called block
2081 * (if a block given).
2082 *
2083 * === Methods for \Set Operations
2084 *
2085 * - #| (aliased as #union and #+):
2086 * Returns a new set containing all elements from +self+
2087 * and all elements from a given enumerable (no duplicates).
2088 * - #& (aliased as #intersection):
2089 * Returns a new set containing all elements common to +self+
2090 * and a given enumerable.
2091 * - #- (aliased as #difference):
2092 * Returns a copy of +self+ with all elements
2093 * in a given enumerable removed.
2094 * - #^: Returns a new set containing all elements from +self+
2095 * and a given enumerable except those common to both.
2096 *
2097 * === Methods for Comparing
2098 *
2099 * - #<=>: Returns -1, 0, or 1 as +self+ is less than, equal to,
2100 * or greater than a given object.
2101 * - #==: Returns whether +self+ and a given enumerable are equal,
2102 * as determined by Object#eql?.
2103 * - #compare_by_identity?:
2104 * Returns whether the set considers only identity
2105 * when comparing elements.
2106 *
2107 * === Methods for Querying
2108 *
2109 * - #length (aliased as #size):
2110 * Returns the count of elements.
2111 * - #empty?:
2112 * Returns whether the set has no elements.
2113 * - #include? (aliased as #member? and #===):
2114 * Returns whether a given object is an element in the set.
2115 * - #subset? (aliased as #<=):
2116 * Returns whether a given object is a subset of the set.
2117 * - #proper_subset? (aliased as #<):
2118 * Returns whether a given enumerable is a proper subset of the set.
2119 * - #superset? (aliased as #>=):
2120 * Returns whether a given enumerable is a superset of the set.
2121 * - #proper_superset? (aliased as #>):
2122 * Returns whether a given enumerable is a proper superset of the set.
2123 * - #disjoint?:
2124 * Returns +true+ if the set and a given enumerable
2125 * have no common elements, +false+ otherwise.
2126 * - #intersect?:
2127 * Returns +true+ if the set and a given enumerable:
2128 * have any common elements, +false+ otherwise.
2129 * - #compare_by_identity?:
2130 * Returns whether the set considers only identity
2131 * when comparing elements.
2132 *
2133 * === Methods for Assigning
2134 *
2135 * - #add (aliased as #<<):
2136 * Adds a given object to the set; returns +self+.
2137 * - #add?:
2138 * If the given object is not an element in the set,
2139 * adds it and returns +self+; otherwise, returns +nil+.
2140 * - #merge:
2141 * Merges the elements of each given enumerable object to the set; returns +self+.
2142 * - #replace:
2143 * Replaces the contents of the set with the contents
2144 * of a given enumerable.
2145 *
2146 * === Methods for Deleting
2147 *
2148 * - #clear:
2149 * Removes all elements in the set; returns +self+.
2150 * - #delete:
2151 * Removes a given object from the set; returns +self+.
2152 * - #delete?:
2153 * If the given object is an element in the set,
2154 * removes it and returns +self+; otherwise, returns +nil+.
2155 * - #subtract:
2156 * Removes each given object from the set; returns +self+.
2157 * - #delete_if - Removes elements specified by a given block.
2158 * - #select! (aliased as #filter!):
2159 * Removes elements not specified by a given block.
2160 * - #keep_if:
2161 * Removes elements not specified by a given block.
2162 * - #reject!
2163 * Removes elements specified by a given block.
2164 *
2165 * === Methods for Converting
2166 *
2167 * - #classify:
2168 * Returns a hash that classifies the elements,
2169 * as determined by the given block.
2170 * - #collect! (aliased as #map!):
2171 * Replaces each element with a block return-value.
2172 * - #divide:
2173 * Returns a hash that classifies the elements,
2174 * as determined by the given block;
2175 * differs from #classify in that the block may accept
2176 * either one or two arguments.
2177 * - #flatten:
2178 * Returns a new set that is a recursive flattening of +self+.
2179 * - #flatten!:
2180 * Replaces each nested set in +self+ with the elements from that set.
2181 * - #inspect (aliased as #to_s):
2182 * Returns a string displaying the elements.
2183 * - #join:
2184 * Returns a string containing all elements, converted to strings
2185 * as needed, and joined by the given record separator.
2186 * - #to_a:
2187 * Returns an array containing all set elements.
2188 * - #to_set:
2189 * Returns +self+ if given no arguments and no block;
2190 * with a block given, returns a new set consisting of block
2191 * return values.
2192 *
2193 * === Methods for Iterating
2194 *
2195 * - #each:
2196 * Calls the block with each successive element; returns +self+.
2197 *
2198 * === Other Methods
2199 *
2200 * - #reset:
2201 * Resets the internal state; useful if an object
2202 * has been modified while an element in the set.
2203 *
2204 */
2205void
2206Init_Set(void)
2207{
2208 rb_cSet = rb_define_class("Set", rb_cObject);
2210
2211 id_each_entry = rb_intern_const("each_entry");
2212 id_any_p = rb_intern_const("any?");
2213 id_new = rb_intern_const("new");
2214 id_i_hash = rb_intern_const("@hash");
2215 id_subclass_compatible = rb_intern_const("SubclassCompatible");
2216 id_class_methods = rb_intern_const("ClassMethods");
2217 id_set_iter_lev = rb_make_internal_id();
2218
2219 rb_define_alloc_func(rb_cSet, set_s_alloc);
2220 rb_define_singleton_method(rb_cSet, "[]", set_s_create, -1);
2221
2222 rb_define_method(rb_cSet, "initialize", set_i_initialize, -1);
2223 rb_define_method(rb_cSet, "initialize_copy", set_i_initialize_copy, 1);
2224
2225 rb_define_method(rb_cSet, "&", set_i_intersection, 1);
2226 rb_define_alias(rb_cSet, "intersection", "&");
2227 rb_define_method(rb_cSet, "-", set_i_difference, 1);
2228 rb_define_alias(rb_cSet, "difference", "-");
2229 rb_define_method(rb_cSet, "^", set_i_xor, 1);
2230 rb_define_method(rb_cSet, "|", set_i_union, 1);
2231 rb_define_alias(rb_cSet, "+", "|");
2232 rb_define_alias(rb_cSet, "union", "|");
2233 rb_define_method(rb_cSet, "<=>", set_i_compare, 1);
2234 rb_define_method(rb_cSet, "==", set_i_eq, 1);
2235 rb_define_alias(rb_cSet, "eql?", "==");
2236 rb_define_method(rb_cSet, "add", set_i_add, 1);
2237 rb_define_alias(rb_cSet, "<<", "add");
2238 rb_define_method(rb_cSet, "add?", set_i_add_p, 1);
2239 rb_define_method(rb_cSet, "classify", set_i_classify, 0);
2240 rb_define_method(rb_cSet, "clear", set_i_clear, 0);
2241 rb_define_method(rb_cSet, "collect!", set_i_collect, 0);
2242 rb_define_alias(rb_cSet, "map!", "collect!");
2243 rb_define_method(rb_cSet, "compare_by_identity", set_i_compare_by_identity, 0);
2244 rb_define_method(rb_cSet, "compare_by_identity?", set_i_compare_by_identity_p, 0);
2245 rb_define_method(rb_cSet, "delete", set_i_delete, 1);
2246 rb_define_method(rb_cSet, "delete?", set_i_delete_p, 1);
2247 rb_define_method(rb_cSet, "delete_if", set_i_delete_if, 0);
2248 rb_define_method(rb_cSet, "disjoint?", set_i_disjoint, 1);
2249 rb_define_method(rb_cSet, "divide", set_i_divide, 0);
2250 rb_define_method(rb_cSet, "each", set_i_each, 0);
2251 rb_define_method(rb_cSet, "empty?", set_i_empty, 0);
2252 rb_define_method(rb_cSet, "flatten", set_i_flatten, 0);
2253 rb_define_method(rb_cSet, "flatten!", set_i_flatten_bang, 0);
2254 rb_define_method(rb_cSet, "hash", set_i_hash, 0);
2255 rb_define_method(rb_cSet, "include?", set_i_include, 1);
2256 rb_define_alias(rb_cSet, "member?", "include?");
2257 rb_define_alias(rb_cSet, "===", "include?");
2258 rb_define_method(rb_cSet, "inspect", set_i_inspect, 0);
2259 rb_define_alias(rb_cSet, "to_s", "inspect");
2260 rb_define_method(rb_cSet, "intersect?", set_i_intersect, 1);
2261 rb_define_method(rb_cSet, "join", set_i_join, -1);
2262 rb_define_method(rb_cSet, "keep_if", set_i_keep_if, 0);
2263 rb_define_method(rb_cSet, "merge", set_i_merge, -1);
2264 rb_define_method(rb_cSet, "proper_subset?", set_i_proper_subset, 1);
2265 rb_define_alias(rb_cSet, "<", "proper_subset?");
2266 rb_define_method(rb_cSet, "proper_superset?", set_i_proper_superset, 1);
2267 rb_define_alias(rb_cSet, ">", "proper_superset?");
2268 rb_define_method(rb_cSet, "reject!", set_i_reject, 0);
2269 rb_define_method(rb_cSet, "replace", set_i_replace, 1);
2270 rb_define_method(rb_cSet, "reset", set_i_reset, 0);
2271 rb_define_method(rb_cSet, "size", set_i_size, 0);
2272 rb_define_alias(rb_cSet, "length", "size");
2273 rb_define_method(rb_cSet, "select!", set_i_select, 0);
2274 rb_define_alias(rb_cSet, "filter!", "select!");
2275 rb_define_method(rb_cSet, "subset?", set_i_subset, 1);
2276 rb_define_alias(rb_cSet, "<=", "subset?");
2277 rb_define_method(rb_cSet, "subtract", set_i_subtract, 1);
2278 rb_define_method(rb_cSet, "superset?", set_i_superset, 1);
2279 rb_define_alias(rb_cSet, ">=", "superset?");
2280 rb_define_method(rb_cSet, "to_a", set_i_to_a, 0);
2281 rb_define_method(rb_cSet, "to_set", set_i_to_set, 0);
2282
2283 /* :nodoc: */
2284 VALUE compat = rb_define_class_under(rb_cSet, "compatible", rb_cObject);
2285 rb_marshal_define_compat(rb_cSet, compat, compat_dumper, compat_loader);
2286
2287 // Create Set::CoreSet before defining inherited, so it does not include
2288 // the backwards compatibility layer.
2290 rb_define_private_method(rb_singleton_class(rb_cSet), "inherited", set_s_inherited, 1);
2291
2292 rb_provide("set.rb");
2293}
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
Definition fl_type.h:722
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1796
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1589
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition eval.c:1856
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2913
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1620
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2956
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
Definition eval.c:1023
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1010
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
Definition string.h:1681
#define Qundef
Old name of RUBY_Qundef.
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
Definition st_data_t.h:33
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition memory.h:405
#define POSFIXABLE
Old name of RB_POSFIXABLE.
Definition fixnum.h:29
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:406
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1416
VALUE rb_cSet
Set class.
Definition set.c:97
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:2249
VALUE rb_mEnumerable
Enumerable module.
Definition enum.c:27
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:264
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
Definition object.c:582
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:686
VALUE rb_obj_is_instance_of(VALUE obj, VALUE klass)
Queries if the given object is a direct instance of the given class.
Definition object.c:867
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:923
VALUE rb_cString
String class.
Definition string.c:84
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
VALUE rb_str_export_to_enc(VALUE obj, rb_encoding *enc)
Identical to rb_str_export(), except it additionally takes an encoding.
Definition string.c:1448
VALUE rb_funcall_passing_block(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv_public(), except you can pass the passed block.
Definition vm_eval.c:1180
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
Definition enumerator.h:208
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition load.c:695
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
Definition string.h:941
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
Definition string.c:3764
VALUE rb_str_buf_cat_ascii(VALUE dst, const char *src)
Identical to rb_str_cat_cstr(), except it additionally assumes the source string be a NUL terminated ...
Definition string.c:3740
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3448
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:2017
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1492
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:380
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3399
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
int capa
Designed capacity of the buffer.
Definition io.h:11
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
Definition iterator.h:58
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
Definition vm_eval.c:1395
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1372
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
Definition marshal.c:137
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
VALUE type(ANYARGS)
ANYARGS-ed function type.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
Definition rarray.h:348
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:724
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:542
VALUE rb_require(const char *feature)
Identical to rb_require_string(), except it takes C's string instead of Ruby's.
Definition load.c:1466
size_t rb_set_size(VALUE set)
Returns the number of elements in the set.
Definition set.c:1969
VALUE rb_set_clear(VALUE set)
Removes all entries from set.
Definition set.c:1957
bool rb_set_delete(VALUE set, VALUE element)
Removes the element from from set.
Definition set.c:1963
bool rb_set_add(VALUE set, VALUE element)
Adds element to set.
Definition set.c:1951
void rb_set_foreach(VALUE set, int(*func)(VALUE element, VALUE arg), VALUE arg)
Iterates over a set.
Definition set.c:1927
bool rb_set_lookup(VALUE set, VALUE element)
Whether the set contains the given element.
Definition set.c:1945
VALUE rb_set_new(void)
Creates a new, empty set object.
Definition set.c:1933
VALUE rb_set_new_capa(size_t capa)
Identical to rb_set_new(), except it additionally specifies how many elements it is expected to conta...
Definition set.c:1939
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:208
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:215
set_table_entry * entries
Array of size 2^entry_power.
Definition set_table.h:28
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376