9#include "internal/bits.h"
10#include "internal/error.h"
11#include "internal/hash.h"
12#include "internal/object.h"
13#include "internal/proc.h"
14#include "internal/sanitizers.h"
15#include "internal/set.h"
16#include "internal/set_table.h"
17#include "internal/symbol.h"
18#include "internal/variable.h"
19#include "ruby_assert.h"
32#include "internal/gc.h"
38 union {
double d; st_index_t i;} u;
43static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
44static const uint32_t prime2 = 0x830fcab9;
47mult_and_mix(uint64_t m1, uint64_t m2)
49#if defined HAVE_UINT128_T
50 uint128_t r = (uint128_t) m1 * (uint128_t) m2;
51 return (uint64_t) (r >> 64) ^ (uint64_t) r;
53 uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
54 uint64_t lm1 = m1, lm2 = m2;
55 uint64_t v64_128 = hm1 * hm2;
56 uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
57 uint64_t v1_32 = lm1 * lm2;
59 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
64key64_hash(uint64_t key, uint32_t seed)
66 return mult_and_mix(key + seed, prime1);
70#define set_index_hash(index) key64_hash(rb_hash_start(index), prime2)
73set_ident_hash(st_data_t n)
82 n ^= dbl_to_index(rb_float_value(n));
86 return (st_index_t)set_index_hash((st_index_t)n);
89static const struct st_hash_type identhash = {
94static const struct st_hash_type objhash = {
100static VALUE set_i_compare_by_identity(
VALUE set);
102#define id_each idEach
103static ID id_each_entry;
107static ID id_set_iter_lev;
108static ID id_subclass_compatible;
109static ID id_class_methods;
111#define RSET_INITIALIZED FL_USER1
112#define RSET_LEV_MASK (FL_USER13 | FL_USER14 | FL_USER15 | \
113 FL_USER16 | FL_USER17 | FL_USER18 | FL_USER19)
114#define RSET_LEV_SHIFT (FL_USHIFT + 13)
115#define RSET_LEV_MAX 127
117#define SET_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(SET_DEBUG, expr, #expr)
119#define RSET_SIZE(set) set_table_size(RSET_TABLE(set))
120#define RSET_EMPTY(set) (RSET_SIZE(set) == 0)
121#define RSET_SIZE_NUM(set) SIZET2NUM(RSET_SIZE(set))
122#define RSET_IS_MEMBER(set, item) set_table_lookup(RSET_TABLE(set), (st_data_t)(item))
123#define RSET_COMPARE_BY_IDENTITY(set) (RSET_TABLE(set)->type == &identhash)
130mark_and_pin_key(st_data_t key, st_data_t data)
132 rb_gc_mark((
VALUE)key);
138mark_key(st_data_t key, st_data_t data)
140 rb_gc_mark_movable((
VALUE)key);
150 if (sobj->table.type == &identhash) {
151 set_table_foreach(&sobj->table, mark_and_pin_key, 0);
154 set_table_foreach(&sobj->table, mark_key, 0);
163 set_free_embedded_table(&sobj->table);
167set_size(
const void *ptr)
171 return (
unsigned long)set_memsize(&sobj->table) -
sizeof(sobj->table);
175set_foreach_replace(st_data_t key, st_data_t argp,
int error)
177 if (rb_gc_location((
VALUE)key) != (
VALUE)key) {
185set_replace_ref(st_data_t *key, st_data_t argp,
int existing)
187 rb_gc_mark_and_move((
VALUE *)key);
193set_update_references(
void *ptr)
196 set_foreach_with_replace(&sobj->table, set_foreach_replace, set_replace_ref, 0);
200 .wrap_struct_name =
"set",
205 .dcompact = set_update_references,
207 .flags = RUBY_TYPED_EMBEDDABLE | RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE
219iter_lev_in_ivar(
VALUE set)
224 SET_ASSERT(lev >= 0);
225 return (
unsigned long)lev;
231iter_lev_in_ivar_set(
VALUE set,
unsigned long lev)
233 SET_ASSERT(lev >= RSET_LEV_MAX);
235 rb_ivar_set_internal(set, id_set_iter_lev,
LONG2FIX((
long)lev));
238static inline unsigned long
239iter_lev_in_flags(
VALUE set)
241 return (
unsigned long)((
RBASIC(set)->flags >> RSET_LEV_SHIFT) & RSET_LEV_MAX);
245iter_lev_in_flags_set(
VALUE set,
unsigned long lev)
247 SET_ASSERT(lev <= RSET_LEV_MAX);
248 RBASIC(set)->flags = ((
RBASIC(set)->flags & ~RSET_LEV_MASK) | ((
VALUE)lev << RSET_LEV_SHIFT));
252set_iterating_p(
VALUE set)
254 return iter_lev_in_flags(set) > 0;
258set_iter_lev_inc(
VALUE set)
260 unsigned long lev = iter_lev_in_flags(set);
261 if (lev == RSET_LEV_MAX) {
262 lev = iter_lev_in_ivar(set) + 1;
269 iter_lev_in_flags_set(set, lev);
270 if (lev < RSET_LEV_MAX)
return;
272 iter_lev_in_ivar_set(set, lev);
276set_iter_lev_dec(
VALUE set)
278 unsigned long lev = iter_lev_in_flags(set);
279 if (lev == RSET_LEV_MAX) {
280 lev = iter_lev_in_ivar(set);
281 if (lev > RSET_LEV_MAX) {
282 iter_lev_in_ivar_set(set, lev-1);
285 rb_attr_delete(set, id_set_iter_lev);
290 iter_lev_in_flags_set(set, lev - 1);
294set_foreach_ensure(
VALUE set)
296 set_iter_lev_dec(set);
304 set_foreach_func *func;
309set_iter_status_check(
int status)
311 if (status == ST_CONTINUE) {
319set_foreach_iter(st_data_t key, st_data_t argp,
int error)
323 if (error)
return ST_STOP;
326 int status = (*arg->func)((
VALUE)key, arg->arg);
328 if (RSET_TABLE(arg->set) != tbl) {
332 return set_iter_status_check(status);
336set_foreach_call(
VALUE arg)
340 ret = set_foreach_check(RSET_TABLE(set), set_foreach_iter,
341 (st_data_t)arg, (st_data_t)
Qundef);
349set_iter(
VALUE set, set_foreach_func *func,
VALUE farg)
359 set_foreach_call((
VALUE)&arg);
362 set_iter_lev_inc(set);
363 rb_ensure(set_foreach_call, (
VALUE)&arg, set_foreach_ensure, set);
367NORETURN(
static void no_new_item(
void));
371 rb_raise(
rb_eRuntimeError,
"can't add a new item into set during iteration");
375set_compact_after_delete(
VALUE set)
377 if (!set_iterating_p(set)) {
378 set_compact_table(RSET_TABLE(set));
386 key = rb_hash_key_str(key);
388 int ret = set_insert(tab, (st_data_t)key);
396 return set_table_insert_wb(RSET_TABLE(set), set, key);
400set_alloc_with_size_and_type(
VALUE klass, st_index_t size,
const struct st_hash_type *
type)
406 set_init_table_with_size(&sobj->table,
type, size);
412set_alloc_with_size(
VALUE klass, st_index_t size)
414 return set_alloc_with_size_and_type(klass, size, &objhash);
418set_s_alloc(
VALUE klass)
420 return set_alloc_with_size(klass, 0);
426 return rb_typeddata_is_instance_of(obj, &set_data_type);
445set_s_create(
int argc,
VALUE *argv,
VALUE klass)
447 VALUE set = set_alloc_with_size(klass, argc);
451 for (i=0; i < argc; i++) {
452 set_table_insert_wb(table, set, argv[i]);
475 rb_raise(rb_eArgError,
"value must be a set");
480enum_method_id(
VALUE other)
483 return id_each_entry;
489 rb_raise(rb_eArgError,
"value must be enumerable");
496 return RSET_SIZE_NUM(set);
503 set_insert_wb(set, element);
511 set_insert_wb(set, element);
550set_i_initialize(
int argc,
VALUE *argv,
VALUE set)
552 if (
RBASIC(set)->flags & RSET_INITIALIZED) {
555 RBASIC(set)->flags |= RSET_INITIALIZED;
560 if (argc > 0 && (other = argv[0]) !=
Qnil) {
567 if (block_given) key =
rb_yield(key);
568 set_table_insert_wb(into, set, key);
573 rb_block_given_p() ? set_initialize_with_block : set_initialize_without_block,
585 if (set == other)
return set;
587 if (set_iterating_p(set)) {
594 set_free_embedded_table(&sobj->table);
595 set_copy(&sobj->table, RSET_TABLE(other));
596 rb_gc_writebarrier_remember(set);
602set_inspect_i(st_data_t key, st_data_t arg)
606 if (args[1] ==
Qtrue) {
624 str = rb_sprintf(
"%"PRIsVALUE
"[...]", klass_name);
628 str = rb_sprintf(
"%"PRIsVALUE
"[", klass_name);
630 set_iter(set, set_inspect_i, (st_data_t)args);
648set_i_inspect(
VALUE set)
654set_to_a_i(st_data_t key, st_data_t arg)
674 st_index_t size = RSET_SIZE(set);
677 if (size == 0)
return ary;
679 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
681 size = set_keys(RSET_TABLE(set), ptr, size);
683 rb_gc_writebarrier_remember(ary);
684 rb_ary_set_len(ary, size);
687 set_iter(set, set_to_a_i, (st_data_t)ary);
704enum_to_set(
VALUE obj)
738set_i_to_set(
VALUE set)
744 return enum_to_set(set);
794 rb_check_frozen(set);
795 if (set_iterating_p(set)) {
796 if (!set_table_lookup(RSET_TABLE(set), (st_data_t)item)) {
801 set_insert_wb(set, item);
822 rb_check_frozen(set);
823 if (set_iterating_p(set)) {
824 if (!set_table_lookup(RSET_TABLE(set), (st_data_t)item)) {
830 return set_insert_wb(set, item) ?
Qnil : set;
850 st_data_t item_data = (st_data_t)item;
851 rb_check_frozen(set);
852 if (set_table_delete(RSET_TABLE(set), &item_data)) {
853 set_compact_after_delete(set);
873 st_data_t item_data = (st_data_t)item;
874 rb_check_frozen(set);
875 if (set_table_delete(RSET_TABLE(set), &item_data)) {
876 set_compact_after_delete(set);
883set_delete_if_i(st_data_t key, st_data_t dummy)
906set_i_delete_if(
VALUE set)
909 rb_check_frozen(set);
910 set_iter(set, set_delete_if_i, 0);
911 set_compact_after_delete(set);
932set_i_reject(
VALUE set)
935 rb_check_frozen(set);
938 size_t n = set_table_size(table);
939 set_iter(set, set_delete_if_i, 0);
941 if (n == set_table_size(table))
return Qnil;
943 set_compact_after_delete(set);
948set_classify_i(st_data_t key, st_data_t tmp)
951 VALUE hash = args[0];
953 VALUE set = rb_hash_lookup2(hash, hash_key,
Qundef);
955 set = set_s_alloc(args[1]);
956 if (
RTEST(args[2])) {
957 set_i_compare_by_identity(set);
959 rb_hash_aset(hash, hash_key, set);
987set_i_classify(
VALUE set)
993 args[2] = RBOOL(RSET_COMPARE_BY_IDENTITY(set));
994 set_iter(set, set_classify_i, (st_data_t)args);
1004 while (root != index) {
1005 tmp_array[update_size++] = index;
1007 root = uf_parents[index];
1009 for (
rb_len_t j = 0; j < update_size; j++) {
1011 uf_parents[idx] = root;
1019 rb_len_t root_i = set_divide_union_find_root(uf_parents, i, tmp_array);
1020 rb_len_t root_j = set_divide_union_find_root(uf_parents, j, tmp_array);
1021 if (root_i != root_j) uf_parents[root_j] = root_i;
1025set_divide_arity2(
VALUE set)
1028 rb_len_t size, *uf_parents, *tmp_array;
1030 VALUE items = set_i_to_a(set);
1035 for (
rb_len_t i = 0; i < size; i++) {
1038 for (
rb_len_t i = 0; i < size - 1; i++) {
1040 for (
rb_len_t j = i + 1; j < size; j++) {
1044 set_divide_union_find_merge(uf_parents, i, j, tmp_array);
1050 for (
rb_len_t i = 0; i < size; i++) {
1052 rb_len_t root = set_divide_union_find_root(uf_parents, i, tmp_array);
1054 if (subset ==
Qnil) {
1055 subset = set_s_alloc(set_class);
1056 if (RSET_COMPARE_BY_IDENTITY(set)) {
1057 set_i_compare_by_identity(subset);
1059 rb_hash_aset(hash,
LEN2NUM(root), subset);
1060 set_i_add(final_set, subset);
1062 set_i_add(subset, v);
1069static void set_merge_enum_into(
VALUE set,
VALUE arg);
1117set_i_divide(
VALUE set)
1121 if (rb_block_arity() == 2) {
1122 return set_divide_arity2(set);
1125 VALUE values = rb_hash_values(set_i_classify(set));
1127 set_merge_enum_into(set, values);
1132set_clear_i(st_data_t key, st_data_t dummy)
1148set_i_clear(
VALUE set)
1150 rb_check_frozen(set);
1151 if (RSET_SIZE(set) == 0)
return set;
1152 if (set_iterating_p(set)) {
1153 set_iter(set, set_clear_i, 0);
1156 set_table_clear(RSET_TABLE(set));
1157 set_compact_after_delete(set);
1171set_intersection_i(st_data_t key, st_data_t tmp)
1174 if (set_table_lookup(data->other, key)) {
1175 set_table_insert_wb(data->into, data->set, key);
1184 set_intersection_i((st_data_t)i, (st_data_t)data);
1207 if (RSET_COMPARE_BY_IDENTITY(set)) {
1208 set_i_compare_by_identity(new_set);
1211 set_table *ntable = RSET_TABLE(new_set);
1216 if (set_table_size(stable) >= set_table_size(otable)) {
1229 set_iter(set, set_intersection_i, (st_data_t)data);
1239 rb_block_call(other, enum_method_id(other), 0, 0, set_intersection_block, data);
1275 return RBOOL(RSET_IS_MEMBER(set, item));
1284set_merge_i(st_data_t key, st_data_t data)
1287 set_table_insert_wb(args->into, args->set, key);
1294 VALUE element = key;
1295 set_insert_wb(set, element);
1305 .into = RSET_TABLE(set)
1307 set_iter(arg, set_merge_i, (st_data_t)&args);
1313 set_table_insert_wb(into, set,
RARRAY_AREF(arg, i));
1318 rb_block_call(arg, enum_method_id(arg), 0, 0, set_merge_block, set);
1340 rb_raise(rb_eArgError,
"no keywords accepted");
1343 if (set_iterating_p(set)) {
1347 rb_check_frozen(set);
1351 for (i=0; i < argc; i++) {
1352 set_merge_enum_into(set, argv[i]);
1359set_reset_table_with_type(
VALUE set,
const struct st_hash_type *
type)
1361 rb_check_frozen(set);
1367 size_t size = set_table_size(old);
1369 set_table *
new = set_init_table_with_size(NULL,
type, size);
1374 set_iter(set, set_merge_i, (st_data_t)&args);
1375 set_free_embedded_table(&sobj->table);
1376 memcpy(&sobj->table,
new,
sizeof(*
new));
1380 sobj->table.type =
type;
1413set_i_compare_by_identity(
VALUE set)
1415 if (RSET_COMPARE_BY_IDENTITY(set))
return set;
1417 if (set_iterating_p(set)) {
1421 return set_reset_table_with_type(set, &identhash);
1440set_i_compare_by_identity_p(
VALUE set)
1442 return RBOOL(RSET_COMPARE_BY_IDENTITY(set));
1456set_i_size(
VALUE set)
1458 return RSET_SIZE_NUM(set);
1473set_i_empty(
VALUE set)
1475 return RBOOL(RSET_EMPTY(set));
1479set_xor_i(st_data_t key, st_data_t data)
1484 if (set_table_insert_wb(table, set, element)) {
1485 set_table_delete(table, &element);
1519 set_iter(other, set_xor_i, (st_data_t)new_set);
1523 if (RSET_COMPARE_BY_IDENTITY(new_set)) {
1524 set_i_compare_by_identity(tmp);
1526 set_merge_enum_into(tmp, other);
1527 set_iter(tmp, set_xor_i, (st_data_t)new_set);
1529 set_compact_after_delete(set);
1553 set_merge_enum_into(set, other);
1558set_remove_i(st_data_t key, st_data_t from)
1560 st_data_t key_data = (st_data_t)key;
1561 set_table_delete((
struct set_table *)from, &key_data);
1568 st_data_t key_data = (st_data_t)key;
1569 rb_check_frozen(set);
1570 set_table_delete(RSET_TABLE(set), &key_data);
1571 return (
VALUE)key_data;
1578 set_iter(arg, set_remove_i, (st_data_t)RSET_TABLE(set));
1581 rb_block_call(arg, enum_method_id(arg), 0, 0, set_remove_block, set);
1583 set_compact_after_delete(set);
1602 rb_check_frozen(set);
1603 set_remove_enum_from(set, other);
1626 return set_i_subtract(
rb_obj_dup(set), other);
1630set_each_i(st_data_t key, st_data_t dummy)
1652set_i_each(
VALUE set)
1655 set_iter(set, set_each_i, 0);
1660set_collect_i(st_data_t key, st_data_t data)
1682set_i_collect(
VALUE set)
1685 rb_check_frozen(set);
1688 if (RSET_COMPARE_BY_IDENTITY(set)) {
1689 set_i_compare_by_identity(new_set);
1691 set_iter(set, set_collect_i, (st_data_t)new_set);
1692 set_i_initialize_copy(set, new_set);
1698set_keep_if_i(st_data_t key, st_data_t into)
1701 set_table_delete((
set_table *)into, &key);
1725set_i_keep_if(
VALUE set)
1728 rb_check_frozen(set);
1730 set_iter(set, set_keep_if_i, (st_data_t)RSET_TABLE(set));
1731 set_compact_after_delete(set);
1753set_i_select(
VALUE set)
1756 rb_check_frozen(set);
1759 size_t n = set_table_size(table);
1760 set_iter(set, set_keep_if_i, (st_data_t)table);
1761 set_compact_after_delete(set);
1763 return (n == set_table_size(table)) ?
Qnil : set;
1781 rb_check_frozen(set);
1784 set_i_initialize_copy(set, other);
1787 if (set_iterating_p(set)) {
1792 enum_method_id(other);
1794 set_table_clear(RSET_TABLE(set));
1795 set_merge_enum_into(set, other);
1797 set_compact_after_delete(set);
1824set_i_reset(
VALUE set)
1826 if (set_iterating_p(set)) {
1830 return set_reset_table_with_type(set, RSET_TABLE(set)->
type);
1836set_flatten_merge_i(st_data_t item, st_data_t arg)
1839 VALUE set = args[0];
1841 VALUE e_id = rb_obj_id(item);
1842 VALUE hash = args[2];
1843 switch(rb_hash_aref(hash, e_id)) {
1847 rb_raise(rb_eArgError,
"tried to flatten recursive Set");
1852 rb_hash_aset(hash, e_id,
Qtrue);
1853 set_flatten_merge(set, item, hash);
1854 rb_hash_aset(hash, e_id,
Qfalse);
1857 set_i_add(set, item);
1865 VALUE args[3] = {set, from, hash};
1866 set_iter(from, set_flatten_merge_i, (st_data_t)args);
1890set_i_flatten(
VALUE set)
1893 if (RSET_COMPARE_BY_IDENTITY(set)) {
1894 set_i_compare_by_identity(new_set);
1901set_contains_set_i(st_data_t item, st_data_t arg)
1904 *(
bool *)arg =
true;
1929set_i_flatten_bang(
VALUE set)
1931 bool contains_set =
false;
1932 set_iter(set, set_contains_set_i, (st_data_t)&contains_set);
1933 if (!contains_set)
return Qnil;
1934 rb_check_frozen(set);
1935 return set_i_replace(set, set_i_flatten(set));
1944set_le_i(st_data_t key, st_data_t arg)
1947 if (set_table_lookup(data->table, key))
return ST_CONTINUE;
1956 .table = RSET_TABLE(other),
1959 set_iter(set, set_le_i, (st_data_t)&data);
1981 if (RSET_SIZE(set) >= RSET_SIZE(other))
return Qfalse;
1982 return set_le(set, other);
2003 if (RSET_SIZE(set) > RSET_SIZE(other))
return Qfalse;
2004 return set_le(set, other);
2025 if (RSET_SIZE(set) <= RSET_SIZE(other))
return Qfalse;
2026 return set_le(other, set);
2047 if (RSET_SIZE(set) < RSET_SIZE(other))
return Qfalse;
2048 return set_le(other, set);
2052set_intersect_i(st_data_t key, st_data_t arg)
2055 if (set_table_lookup((
set_table *)args[0], key)) {
2079 size_t set_size = RSET_SIZE(set);
2080 size_t other_size = RSET_SIZE(other);
2085 if (set_size < other_size) {
2087 args[0] = (
VALUE)RSET_TABLE(other);
2091 args[0] = (
VALUE)RSET_TABLE(set);
2093 set_iter(iter_arg, set_intersect_i, (st_data_t)args);
2100 rb_raise(rb_eArgError,
"value must be enumerable");
2119 return RBOOL(!
RTEST(set_i_intersect(set, other)));
2155 size_t set_size = RSET_SIZE(set);
2156 size_t other_size = RSET_SIZE(other);
2158 if (set_size < other_size) {
2159 if (set_le(set, other) ==
Qtrue) {
2163 else if (set_size > other_size) {
2164 if (set_le(other, set) ==
Qtrue) {
2168 else if (set_le(set, other) ==
Qtrue) {
2182set_eql_i(st_data_t item, st_data_t arg)
2186 if (!set_table_lookup(RSET_TABLE(data->set), item)) {
2194set_recursive_eql(
VALUE set,
VALUE dt,
int recur)
2196 if (recur)
return Qtrue;
2198 data->result =
Qtrue;
2199 set_iter(set, set_eql_i, dt);
2200 return data->result;
2220 if (set == other)
return Qtrue;
2224 size_t ssize = set_table_size(stable);
2225 size_t osize = set_table_size(otable);
2227 if (ssize != osize)
return Qfalse;
2228 if (ssize == 0 && osize == 0)
return Qtrue;
2229 if (stable->type != otable->type)
return Qfalse;
2237set_hash_i(st_data_t item, st_data_t(arg))
2239 st_index_t *hval = (st_index_t *)arg;
2240 st_index_t ival = rb_hash(item);
2241 *hval ^= rb_st_hash(&ival,
sizeof(st_index_t), 0);
2257set_i_hash(
VALUE set)
2259 st_index_t size = RSET_SIZE(set);
2260 st_index_t hval = rb_st_hash_start(size);
2263 set_iter(set, set_hash_i, (
VALUE)&hval);
2265 hval = rb_st_hash_end(hval);
2271set_to_hash_i(st_data_t key, st_data_t arg)
2278set_i_to_h(
VALUE set)
2282 if (RSET_COMPARE_BY_IDENTITY(set)) {
2283 hash = rb_ident_hash_new_capa(size);
2286 hash = rb_hash_new_capa(size);
2288 rb_hash_set_default(hash,
Qfalse);
2290 if (size == 0)
return hash;
2292 set_iter(set, set_to_hash_i, (st_data_t)hash);
2297compat_dumper(
VALUE set)
2305set_i_from_hash_i(st_data_t key, st_data_t val, st_data_t set)
2318 if (rb_hash_compare_by_id_p(hash)) set_i_compare_by_identity(set);
2319 rb_hash_stlike_foreach(hash, set_i_from_hash_i, (st_data_t)set);
2326 return set_i_from_hash(self,
rb_ivar_get(a, id_i_hash));
2332rb_ident_set_new(
void)
2334 return set_alloc_with_size_and_type(
rb_cSet, 0, &identhash);
2340 if (set_insert(RSET_TABLE(set), (st_data_t)element) == 0) {
2348rb_set_delete_no_check(
VALUE set,
VALUE element)
2350 st_data_t element_data = (st_data_t)element;
2351 return set_table_delete(RSET_TABLE(set), &element_data) != 0;
2355rb_set_to_a(
VALUE set)
2357 return set_i_to_a(set);
2365 set_iter(set, func, arg);
2371 return set_alloc_with_size(
rb_cSet, 0);
2377 return set_alloc_with_size(
rb_cSet, (st_index_t)
capa);
2383 return RSET_IS_MEMBER(set, element);
2389 return set_i_add_p(set, element) !=
Qnil;
2395 return set_i_clear(set);
2401 return set_i_delete_p(set, element) !=
Qnil;
2407 return RSET_SIZE(set);
2618 id_set_iter_lev = rb_make_internal_id();
#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.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define Qundef
Old name of RUBY_Qundef.
#define LEN2NUM
Old name of RB_LEN2NUM.
#define CLASS_OF
Old name of rb_class_of.
#define LONG2FIX
Old name of RB_INT2FIX.
#define T_HASH
Old name of RUBY_T_HASH.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define POSFIXABLE
Old name of RB_POSFIXABLE.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_cObject
Object class.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_obj_is_instance_of(VALUE obj, VALUE klass)
Queries if the given object is a direct instance of the given class.
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.
VALUE rb_cString
String class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
VALUE rb_str_export_to_enc(VALUE obj, rb_encoding *enc)
Identical to rb_str_export(), except it additionally takes an encoding.
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.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_new_capa(rb_len_t 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?.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
size_t rb_set_size(VALUE set)
Returns the number of elements in the set.
VALUE rb_set_clear(VALUE set)
Removes all entries from set.
bool rb_set_delete(VALUE set, VALUE element)
Removes the element from from set.
bool rb_set_add(VALUE set, VALUE element)
Adds element to set.
void rb_set_foreach(VALUE set, int(*func)(VALUE element, VALUE arg), VALUE arg)
Iterates over a set.
bool rb_set_lookup(VALUE set, VALUE element)
Whether the set contains the given element.
VALUE rb_set_new(void)
Creates a new, empty set object.
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...
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
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.
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 ...
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.
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.
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.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
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().
int capa
Designed capacity of the buffer.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield(VALUE val)
Yields the block.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
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.
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
#define RARRAY_AREF(a, i)
References an object in an array.
#define RBASIC(obj)
Convenient casting macro.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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...
VALUE rb_require(const char *feature)
Identical to rb_require_string(), except it takes C's string instead of Ruby's.
#define RTEST
This is an old name of RB_TEST.
set_table_entry * entries
Array of size 2^entry_power.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.