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