14 #include "debug_counter.h"
17 #include "internal/array.h"
18 #include "internal/compar.h"
19 #include "internal/enum.h"
20 #include "internal/gc.h"
21 #include "internal/hash.h"
22 #include "internal/numeric.h"
23 #include "internal/object.h"
24 #include "internal/proc.h"
25 #include "internal/rational.h"
26 #include "internal/vm.h"
38 #include "ruby_assert.h"
41 VALUE rb_cArray_empty_frozen;
70 #define ARY_DEFAULT_SIZE 16
71 #define ARY_MAX_SIZE (LONG_MAX / (int)sizeof(VALUE))
72 #define SMALL_ARRAY_LEN 16
76 should_be_T_ARRAY(
VALUE ary)
81 #define ARY_HEAP_PTR(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
82 #define ARY_HEAP_LEN(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
83 #define ARY_HEAP_CAPA(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(!ARY_SHARED_ROOT_P(a)), \
84 RARRAY(a)->as.heap.aux.capa)
86 #define ARY_EMBED_PTR(a) (RUBY_ASSERT(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
87 #define ARY_EMBED_LEN(a) \
88 (RUBY_ASSERT(ARY_EMBED_P(a)), \
89 (long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
90 (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
91 #define ARY_HEAP_SIZE(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE))
93 #define ARY_OWNS_HEAP_P(a) (RUBY_ASSERT(should_be_T_ARRAY((VALUE)(a))), \
94 !FL_TEST_RAW((a), RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG))
96 #define FL_SET_EMBED(a) do { \
97 RUBY_ASSERT(!ARY_SHARED_P(a)); \
98 FL_SET((a), RARRAY_EMBED_FLAG); \
102 #define FL_UNSET_EMBED(ary) FL_UNSET((ary), RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK)
103 #define FL_SET_SHARED(ary) do { \
104 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
105 FL_SET((ary), RARRAY_SHARED_FLAG); \
107 #define FL_UNSET_SHARED(ary) FL_UNSET((ary), RARRAY_SHARED_FLAG)
109 #define ARY_SET_PTR(ary, p) do { \
110 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
111 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
112 RARRAY(ary)->as.heap.ptr = (p); \
114 #define ARY_SET_EMBED_LEN(ary, n) do { \
116 RUBY_ASSERT(ARY_EMBED_P(ary)); \
117 RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
118 RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
120 #define ARY_SET_HEAP_LEN(ary, n) do { \
121 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
122 RARRAY(ary)->as.heap.len = (n); \
124 #define ARY_SET_LEN(ary, n) do { \
125 if (ARY_EMBED_P(ary)) { \
126 ARY_SET_EMBED_LEN((ary), (n)); \
129 ARY_SET_HEAP_LEN((ary), (n)); \
131 RUBY_ASSERT(RARRAY_LEN(ary) == (n)); \
133 #define ARY_INCREASE_PTR(ary, n) do { \
134 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
135 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
136 RARRAY(ary)->as.heap.ptr += (n); \
138 #define ARY_INCREASE_LEN(ary, n) do { \
139 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
140 if (ARY_EMBED_P(ary)) { \
141 ARY_SET_EMBED_LEN((ary), RARRAY_LEN(ary)+(n)); \
144 RARRAY(ary)->as.heap.len += (n); \
148 #define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? ary_embed_capa(ary) : \
149 ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : ARY_HEAP_CAPA(ary))
150 #define ARY_SET_CAPA(ary, n) do { \
151 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
152 RUBY_ASSERT(!ARY_SHARED_P(ary)); \
153 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
154 RARRAY(ary)->as.heap.aux.capa = (n); \
157 #define ARY_SHARED_ROOT_OCCUPIED(ary) (!OBJ_FROZEN(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1)
158 #define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \
159 RUBY_ASSERT(ARY_SHARED_ROOT_P(ary)); \
160 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
161 RUBY_ASSERT((value) >= 0); \
162 RARRAY(ary)->as.heap.aux.capa = (value); \
164 #define FL_SET_SHARED_ROOT(ary) do { \
165 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
166 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
167 FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \
181 ary_embed_capa(
VALUE ary)
183 size_t size = rb_gc_obj_slot_size(ary) - offsetof(
struct RArray, as.
ary);
185 return size /
sizeof(
VALUE);
189 ary_embed_size(
long capa)
195 ary_embeddable_p(
long capa)
197 return rb_gc_size_allocatable_p(ary_embed_size(
capa));
201 rb_ary_embeddable_p(
VALUE ary)
211 return !(ARY_SHARED_ROOT_P(ary) ||
OBJ_FROZEN(ary) || ARY_SHARED_P(ary));
215 rb_ary_size_as_embedded(
VALUE ary)
219 if (ARY_EMBED_P(ary)) {
220 real_size = ary_embed_size(ARY_EMBED_LEN(ary));
222 else if (rb_ary_embeddable_p(ary)) {
223 real_size = ary_embed_size(ARY_HEAP_CAPA(ary));
226 real_size =
sizeof(
struct RArray);
233 #define ary_verify(ary) ary_verify_(ary, __FILE__, __LINE__)
236 ary_verify_(
VALUE ary,
const char *file,
int line)
240 if (ARY_SHARED_P(
ary)) {
249 else if (ARY_EMBED_P(
ary)) {
258 for (i=0; i<
len; i++) {
273 #define ary_verify(ary) ((void)0)
302 ary_mem_clear(
VALUE ary,
long beg,
long size)
310 memfill(
register VALUE *mem,
register long size,
register VALUE val)
321 memfill(
ptr + beg, size, val);
331 if (argc > (
int)(128/
sizeof(
VALUE)) ) {
332 rb_gc_writebarrier_remember(buff_owner_ary);
340 for (i=0; i<argc; i++) {
348 ary_memcpy(
VALUE ary,
long beg,
long argc,
const VALUE *argv)
350 ary_memcpy0(
ary, beg, argc, argv,
ary);
354 ary_heap_alloc_buffer(
size_t capa)
362 ruby_sized_xfree((
void *)
ptr, size);
368 ary_heap_free_ptr(
ary, ARY_HEAP_PTR(
ary), ARY_HEAP_SIZE(
ary));
372 ary_heap_realloc(
VALUE ary,
size_t new_capa)
385 if (!ARY_EMBED_P(
ary)) {
386 const VALUE *buf = ARY_HEAP_PTR(
ary);
387 long len = ARY_HEAP_LEN(
ary);
390 ARY_SET_EMBED_LEN(
ary,
len);
399 ary_resize_capa(
VALUE ary,
long capacity)
405 if (capacity > ary_embed_capa(
ary)) {
406 size_t new_capa = capacity;
407 if (ARY_EMBED_P(
ary)) {
408 long len = ARY_EMBED_LEN(
ary);
409 VALUE *
ptr = ary_heap_alloc_buffer(capacity);
414 ARY_SET_HEAP_LEN(
ary,
len);
417 new_capa = ary_heap_realloc(
ary, capacity);
419 ARY_SET_CAPA(
ary, new_capa);
422 if (!ARY_EMBED_P(
ary)) {
423 long len = ARY_HEAP_LEN(
ary);
424 long old_capa = ARY_HEAP_CAPA(
ary);
427 if (
len > capacity)
len = capacity;
429 ary_heap_free_ptr(
ary,
ptr, old_capa);
442 long capacity = ARY_HEAP_LEN(
ary);
443 long old_capa = ARY_HEAP_CAPA(
ary);
446 if (old_capa > capacity) {
447 size_t new_capa = ary_heap_realloc(
ary, capacity);
448 ARY_SET_CAPA(
ary, new_capa);
455 ary_double_capa(
VALUE ary,
long min)
457 long new_capa = ARY_CAPA(
ary) / 2;
459 if (new_capa < ARY_DEFAULT_SIZE) {
460 new_capa = ARY_DEFAULT_SIZE;
462 if (new_capa >= ARY_MAX_SIZE - min) {
463 new_capa = (ARY_MAX_SIZE - min) / 2;
466 ary_resize_capa(
ary, new_capa);
485 FL_UNSET_SHARED(
ary);
491 if (ARY_OWNS_HEAP_P(
ary)) {
494 else if (ARY_SHARED_P(
ary)) {
499 ARY_SET_EMBED_LEN(
ary, 0);
524 RB_DEBUG_COUNTER_INC(obj_ary_shared_create);
530 rb_check_frozen(
ary);
537 if (ARY_SHARED_P(
ary)) {
543 if (
len <= ary_embed_capa(
ary)) {
545 FL_UNSET_SHARED(
ary);
549 ARY_SET_EMBED_LEN(
ary,
len);
553 FL_UNSET_SHARED(
ary);
555 ARY_SET_CAPA(
ary, shared_len);
570 rb_gc_writebarrier_remember(
ary);
578 rb_ary_modify_check(
ary);
579 rb_ary_cancel_sharing(
ary);
583 ary_ensure_room_for_push(
VALUE ary,
long add_len)
586 long new_len = old_len + add_len;
589 if (old_len > ARY_MAX_SIZE - add_len) {
592 if (ARY_SHARED_P(
ary)) {
593 if (new_len > ary_embed_capa(
ary)) {
597 rb_ary_modify_check(
ary);
608 ary_double_capa(
ary, new_len);
619 rb_ary_modify_check(
ary);
622 if (new_len >
capa) {
623 ary_double_capa(
ary, new_len);
654 if (!ARY_EMBED_P(
ary) && !ARY_SHARED_P(
ary) && !ARY_SHARED_ROOT_P(
ary)) {
655 ary_shrink_capa(
ary);
671 if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) &&
672 !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) &&
673 ARY_SHARED_ROOT(ary1) == ARY_SHARED_ROOT(ary2) &&
674 ARY_HEAP_LEN(ary1) == ARY_HEAP_LEN(ary2)) {
683 size_t size = ary_embed_size(
capa);
696 ary_alloc_heap(
VALUE klass)
700 sizeof(
struct RArray), 0);
705 empty_ary_alloc(
VALUE klass)
707 RUBY_DTRACE_CREATE_HOOK(ARRAY, 0);
708 return ary_alloc_embed(klass, 0);
719 if (
capa > ARY_MAX_SIZE) {
723 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
725 if (ary_embeddable_p(
capa)) {
726 ary = ary_alloc_embed(klass,
capa);
729 ary = ary_alloc_heap(klass);
733 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
734 ARY_SET_HEAP_LEN(
ary, 0);
762 for (i=0; i<n; i++) {
772 rb_ary_tmp_new_from_values(
VALUE klass,
long n,
const VALUE *elts)
776 ary = ary_new(klass, n);
778 ary_memcpy(
ary, 0, n, elts);
788 return rb_ary_tmp_new_from_values(
rb_cArray, n, elts);
794 size_t size = ary_embed_size(
capa);
811 sizeof(
struct RArray), ec);
823 if (
capa > ARY_MAX_SIZE) {
827 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
829 if (ary_embeddable_p(
capa)) {
830 ary = ec_ary_alloc_embed(ec, klass,
capa);
833 ary = ec_ary_alloc_heap(ec, klass);
837 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
838 ARY_SET_HEAP_LEN(
ary, 0);
851 ary_memcpy(
ary, 0, n, elts);
866 rb_ary_hidden_new_fill(
long capa)
877 if (ARY_OWNS_HEAP_P(
ary)) {
878 if (USE_DEBUG_COUNTER &&
879 !ARY_SHARED_ROOT_P(
ary) &&
881 RB_DEBUG_COUNTER_INC(obj_ary_extracapa);
884 RB_DEBUG_COUNTER_INC(obj_ary_ptr);
888 RB_DEBUG_COUNTER_INC(obj_ary_embed);
891 if (ARY_SHARED_P(
ary)) {
892 RB_DEBUG_COUNTER_INC(obj_ary_shared);
894 if (ARY_SHARED_ROOT_P(
ary) && ARY_SHARED_ROOT_OCCUPIED(
ary)) {
895 RB_DEBUG_COUNTER_INC(obj_ary_shared_root_occupied);
899 static VALUE fake_ary_flags;
902 init_fake_ary_flags(
void)
904 struct RArray fake_ary = {0};
912 rb_setup_fake_ary(
struct RArray *fake_ary,
const VALUE *list,
long len)
915 RBASIC_CLEAR_CLASS((
VALUE)fake_ary);
918 fake_ary->
as.
heap.ptr = list;
921 return (
VALUE)fake_ary;
927 if (ARY_OWNS_HEAP_P(
ary)) {
928 return ARY_CAPA(
ary) *
sizeof(
VALUE);
940 if (ARY_SHARED_P(
ary)) {
941 return ARY_SHARED_ROOT(
ary);
943 else if (ARY_SHARED_ROOT_P(
ary)) {
955 VALUE shared = ary_alloc_heap(0);
956 FL_SET_SHARED_ROOT(shared);
958 if (ARY_EMBED_P(
ary)) {
960 ARY_SET_PTR(shared,
ptr);
964 ARY_SET_HEAP_LEN(
ary,
len);
971 ARY_SET_LEN(shared,
capa);
973 rb_ary_set_shared(
ary, shared);
987 if (ary_embeddable_p(
len)) {
992 ARY_SET_EMBED_LEN(subst,
len);
996 return rb_ary_increment_share(ary_make_shared(
ary));
1009 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1011 #define to_ary rb_to_array_type
1016 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1022 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1028 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1057 rb_ary_s_new(
int argc,
VALUE *argv,
VALUE klass)
1063 if (argc > 0 &&
FIXNUM_P(argv[0])) {
1065 if (size < 0) size = 0;
1068 ary = ary_new(klass, size);
1147 if (argc == 1 && !
FIXNUM_P(size)) {
1160 if (
len > ARY_MAX_SIZE) {
1165 ary_resize_capa(
ary,
len);
1170 rb_warn(
"block supersedes default value argument");
1172 for (i=0; i<
len; i++) {
1174 ARY_SET_LEN(
ary, i + 1);
1178 ary_memfill(
ary, 0,
len, val);
1195 rb_ary_s_create(
int argc,
VALUE *argv,
VALUE klass)
1198 if (argc > 0 && argv) {
1199 ary_memcpy(
ary, 0, argc, argv);
1200 ARY_SET_LEN(
ary, argc);
1218 else if (idx >= ARY_MAX_SIZE) {
1223 if (idx >= ARY_CAPA(
ary)) {
1224 ary_double_capa(
ary, idx);
1231 ARY_SET_LEN(
ary, idx + 1);
1233 ARY_SET(
ary, idx, val);
1243 VALUE result = ary_alloc_heap(klass);
1244 size_t embed_capa = ary_embed_capa(result);
1245 if ((
size_t)
len <= embed_capa) {
1246 FL_SET_EMBED(result);
1248 ARY_SET_EMBED_LEN(result,
len);
1251 VALUE shared = ary_make_shared(
ary);
1256 FL_UNSET_EMBED(result);
1260 rb_ary_set_shared(result, shared);
1262 ARY_INCREASE_PTR(result, offset);
1263 ARY_SET_LEN(result,
len);
1273 ary_make_partial_step(
VALUE ary,
VALUE klass,
long offset,
long len,
long step)
1280 const long orig_len =
len;
1282 if (step > 0 && step >=
len) {
1283 VALUE result = ary_new(klass, 1);
1288 ARY_SET_EMBED_LEN(result, 1);
1291 else if (step < 0 && step < -
len) {
1295 long ustep = (step < 0) ? -step : step;
1296 len = roomof(
len, ustep);
1299 long j = offset + ((step > 0) ? 0 : (orig_len - 1));
1301 VALUE result = ary_new(klass,
len);
1302 if (ARY_EMBED_P(result)) {
1306 for (i = 0; i <
len; ++i) {
1310 ARY_SET_EMBED_LEN(result,
len);
1316 for (i = 0; i <
len; ++i) {
1321 ARY_SET_LEN(result,
len);
1333 enum ary_take_pos_flags
1340 ary_take_first_or_last_n(
VALUE ary,
long n,
enum ary_take_pos_flags last)
1358 ary_take_first_or_last(
int argc,
const VALUE *argv,
VALUE ary,
enum ary_take_pos_flags last)
1365 return ary_take_first_or_last_n(
ary,
NUM2LONG(argv[0]), last);
1387 VALUE target_ary = ary_ensure_room_for_push(
ary, 1);
1391 ARY_SET_LEN(
ary, idx + 1);
1400 VALUE target_ary = ary_ensure_room_for_push(
ary,
len);
1401 ary_memcpy0(
ary, oldlen,
len, argv, target_ary);
1402 ARY_SET_LEN(
ary, oldlen +
len);
1434 rb_ary_modify_check(
ary);
1436 if (n == 0)
return Qnil;
1437 if (ARY_OWNS_HEAP_P(
ary) &&
1438 n * 3 < ARY_CAPA(
ary) &&
1439 ARY_CAPA(
ary) > ARY_DEFAULT_SIZE)
1441 ary_resize_capa(
ary, n * 2);
1444 ARY_SET_LEN(
ary, n);
1488 rb_ary_modify_check(
ary);
1489 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
1502 rb_ary_modify_check(
ary);
1508 rb_ary_behead(
ary, 1);
1561 rb_ary_modify_check(
ary);
1562 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1564 rb_ary_behead(
ary,n);
1576 rb_ary_modify_check(
ary);
1578 if (!ARY_SHARED_P(
ary)) {
1583 ARY_INCREASE_LEN(
ary, -n);
1588 ary_mem_clear(
ary, 0, n);
1589 ary_make_shared(
ary);
1591 else if (ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(
ary))) {
1592 ary_mem_clear(
ary, 0, n);
1595 ARY_INCREASE_PTR(
ary, n);
1596 ARY_INCREASE_LEN(
ary, -n);
1605 if (head - sharedp < argc) {
1606 long room =
capa -
len - argc;
1610 head = sharedp + argc + room;
1612 ARY_SET_PTR(
ary, head - argc);
1616 return ARY_SHARED_ROOT(
ary);
1620 ary_modify_for_unshift(
VALUE ary,
int argc)
1623 long new_len =
len + argc;
1625 const VALUE *head, *sharedp;
1629 if (
capa - (
capa >> 6) <= new_len) {
1630 ary_double_capa(
ary, new_len);
1634 if (new_len > ARY_DEFAULT_SIZE * 4 && !ARY_EMBED_P(
ary)) {
1639 ary_make_shared(
ary);
1642 return make_room_for_unshift(
ary, head, (
void *)sharedp, argc,
capa,
len);
1656 ary_ensure_room_for_unshift(
VALUE ary,
int argc)
1659 long new_len =
len + argc;
1661 if (
len > ARY_MAX_SIZE - argc) {
1664 else if (! ARY_SHARED_P(
ary)) {
1665 return ary_modify_for_unshift(
ary, argc);
1672 return ary_modify_for_unshift(
ary, argc);
1674 else if (new_len >
capa) {
1675 return ary_modify_for_unshift(
ary, argc);
1681 rb_ary_modify_check(
ary);
1682 return make_room_for_unshift(
ary, head, sharedp, argc,
capa,
len);
1708 rb_ary_modify_check(
ary);
1712 target_ary = ary_ensure_room_for_unshift(
ary, argc);
1713 ary_memcpy0(
ary, 0, argc, argv, target_ary);
1714 ARY_SET_LEN(
ary,
len + argc);
1721 return rb_ary_unshift_m(1, &item,
ary);
1730 if (offset < 0 ||
len <= offset) {
1739 return rb_ary_entry_internal(
ary, offset);
1743 rb_ary_subseq_step(
VALUE ary,
long beg,
long len,
long step)
1748 if (beg > alen)
return Qnil;
1749 if (beg < 0 ||
len < 0)
return Qnil;
1751 if (alen <
len || alen < beg +
len) {
1755 if (
len == 0)
return ary_new(klass, 0);
1759 return ary_make_partial(
ary, klass, beg,
len);
1761 return ary_make_partial_step(
ary, klass, beg,
len, step);
1767 return rb_ary_subseq_step(
ary, beg,
len, 1);
1899 return rb_ary_aref2(
ary, argv[0], argv[1]);
1901 return rb_ary_aref1(
ary, argv[0]);
1918 long beg,
len, step;
1931 return rb_ary_subseq_step(
ary, beg,
len, step);
1976 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1982 ary_first(
VALUE self)
1988 ary_last(
VALUE self)
1998 return ary_last(
ary);
2001 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
2054 if (block_given && argc == 2) {
2055 rb_warn(
"block supersedes default value argument");
2063 if (block_given)
return rb_yield(pos);
2125 rb_warn(
"given block not used");
2183 rb_warn(
"given block not used");
2201 if (!
NIL_P(tmp))
return tmp;
2220 if (olen <
len || olen < beg +
len) {
2226 rofs = (rptr >= optr && rptr < optr + olen) ? rptr - optr : -1;
2231 if (beg > ARY_MAX_SIZE - rlen) {
2234 target_ary = ary_ensure_room_for_push(
ary, rlen-
len);
2236 ary_mem_clear(
ary, olen, beg - olen);
2239 ary_memcpy0(
ary, beg, rlen, rptr, target_ary);
2246 if (olen -
len > ARY_MAX_SIZE - rlen) {
2250 alen = olen + rlen -
len;
2251 if (alen >= ARY_CAPA(
ary)) {
2252 ary_double_capa(
ary, alen);
2259 ARY_SET_LEN(
ary, alen);
2263 rb_gc_writebarrier_remember(
ary);
2285 rb_ary_modify_check(
ary);
2286 if (ARY_SHARED_P(
ary)) {
2302 if (
len == olen)
return ary;
2303 if (
len > ARY_MAX_SIZE) {
2307 if (
len >= ARY_CAPA(
ary)) {
2308 ary_double_capa(
ary,
len);
2310 ary_mem_clear(
ary, olen,
len - olen);
2313 else if (ARY_EMBED_P(
ary)) {
2314 ARY_SET_EMBED_LEN(
ary,
len);
2316 else if (
len <= ary_embed_capa(
ary)) {
2318 long ptr_capa = ARY_HEAP_SIZE(
ary);
2319 bool is_malloc_ptr = !ARY_SHARED_P(
ary);
2324 ARY_SET_EMBED_LEN(
ary,
len);
2326 if (is_malloc_ptr) ruby_sized_xfree((
void *)
ptr, ptr_capa);
2329 if (olen >
len + ARY_DEFAULT_SIZE) {
2330 size_t new_capa = ary_heap_realloc(
ary,
len);
2331 ARY_SET_CAPA(
ary, new_capa);
2333 ARY_SET_HEAP_LEN(
ary,
len);
2502 long offset, beg,
len;
2505 rb_ary_modify_check(
ary);
2509 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[2]);
2513 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2517 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[1]);
2521 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2566 rb_ary_modify_check(
ary);
2568 if (argc == 1)
return ary;
2580 rb_ary_splice(
ary, pos, 0, argv + 1, argc - 1);
2590 return rb_ary_length(
ary);
2782 ARY_SET_LEN(dup,
len);
2800 recursive_join(
VALUE obj,
VALUE argp,
int recur)
2805 VALUE result = arg[2];
2806 int *first = (
int *)arg[3];
2812 ary_join_1(obj,
ary, sep, 0, result, first);
2824 for (i=0; i<max; i++) {
2827 if (i > 0 && !
NIL_P(sep))
2835 ary_join_1_str(
VALUE dst,
VALUE src,
int *first)
2857 args[3] = (
VALUE)first;
2868 if (i > 0 && !
NIL_P(sep))
2873 ary_join_1_str(result, val, first);
2876 ary_join_1_ary(val,
ary, sep, result, val, first);
2879 ary_join_1_str(result, tmp, first);
2882 ary_join_1_ary(val,
ary, sep, result, tmp, first);
2894 VALUE val, tmp, result;
2906 if (
NIL_P(tmp) || tmp != val) {
2912 i = ary_join_0(
ary, sep, i, result);
2914 ary_join_1(
ary,
ary, sep, i, result, &first);
3015 return rb_ary_inspect(
ary);
3079 const VALUE e = rb_ary_elt(
ary, i);
3080 const VALUE elt = block_given ? rb_yield_force_blockarg(e) : e;
3082 if (
NIL_P(key_value_pair)) {
3128 ary_reverse(p1, p2);
3174 do *p2-- = *p1++;
while (--
len > 0);
3181 rotate_count(
long cnt,
long len)
3183 return (cnt < 0) ? (
len - (~cnt %
len) - 1) : (cnt %
len);
3194 else if (cnt ==
len - 1) {
3202 if (--cnt > 0) ary_reverse(
ptr,
ptr + cnt);
3214 if (
len > 1 && (cnt = rotate_count(cnt,
len)) > 0) {
3306 cnt = rotate_count(cnt,
len);
3309 ary_memcpy(rotated, 0,
len,
ptr + cnt);
3310 ary_memcpy(rotated,
len, cnt,
ptr);
3322 sort_reentered(
VALUE ary)
3324 if (
RBASIC(ary)->klass) {
3336 sort_reentered(data->ary);
3340 sort_1(
const void *ap,
const void *bp,
void *dummy)
3343 VALUE retval = sort_reentered(data->ary);
3352 sort_returned(data);
3357 sort_2(
const void *ap,
const void *bp,
void *dummy)
3360 VALUE retval = sort_reentered(data->ary);
3365 if ((
long)a > (long)b)
return 1;
3366 if ((
long)a < (long)b)
return -1;
3369 if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(STRING)) {
3373 return rb_float_cmp(a, b);
3378 sort_returned(data);
3399 VALUE tmp = ary_make_substitution(ary);
3402 RBASIC_CLEAR_CLASS(tmp);
3404 data.receiver = ary;
3410 if (ARY_EMBED_P(tmp)) {
3411 if (ARY_SHARED_P(ary)) {
3412 rb_ary_unshare(ary);
3415 if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) {
3416 ary_resize_capa(ary, ARY_EMBED_LEN(tmp));
3418 ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp));
3419 ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
3422 if (!ARY_EMBED_P(ary) && ARY_HEAP_PTR(ary) == ARY_HEAP_PTR(tmp)) {
3423 FL_UNSET_SHARED(ary);
3428 if (ARY_EMBED_P(ary)) {
3429 FL_UNSET_EMBED(ary);
3431 else if (ARY_SHARED_P(ary)) {
3433 rb_ary_unshare(ary);
3438 ARY_SET_PTR(ary, ARY_HEAP_PTR(tmp));
3439 ARY_SET_HEAP_LEN(ary,
len);
3440 ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
3445 ARY_SET_EMBED_LEN(tmp, 0);
3494 static VALUE rb_ary_bsearch_index(
VALUE ary);
3510 rb_ary_bsearch(
VALUE ary)
3512 VALUE index_result = rb_ary_bsearch_index(ary);
3517 return index_result;
3534 rb_ary_bsearch_index(
VALUE ary)
3537 int smaller = 0, satisfied = 0;
3541 while (low < high) {
3542 mid = low + ((high - low) / 2);
3549 else if (v ==
Qtrue) {
3553 else if (!
RTEST(v)) {
3560 case 1: smaller = 0;
break;
3561 case -1: smaller = 1;
3566 " (must be numeric, true, false or nil)",
3576 if (!satisfied)
return Qnil;
3610 rb_ary_sort_by_bang(
VALUE ary)
3643 rb_ary_collect(
VALUE ary)
3678 rb_ary_collect_bang(
VALUE ary)
3694 long beg,
len, i, j;
3696 for (i=0; i<argc; i++) {
3703 long end = olen < beg+
len ? olen : beg+
len;
3704 for (j = beg; j < end; j++) {
3717 append_values_at_single(
VALUE result,
VALUE ary,
long olen,
VALUE idx)
3727 const long end = beg +
len;
3788 rb_ary_values_at(
int argc,
VALUE *argv,
VALUE ary)
3792 for (i = 0; i < argc; ++i) {
3793 append_values_at_single(result, ary, olen, argv[i]);
3821 rb_ary_select(
VALUE ary)
3842 select_bang_i(
VALUE a)
3845 VALUE ary = arg->ary;
3848 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
3856 return (i1 == i2) ?
Qnil : ary;
3860 select_bang_ensure(
VALUE a)
3863 VALUE ary = arg->ary;
3865 long i1 = arg->len[0], i2 = arg->len[1];
3867 if (i2 <
len && i2 < i1) {
3876 ARY_SET_LEN(ary, i2 + tail);
3904 rb_ary_select_bang(
VALUE ary)
3912 args.len[0] = args.len[1] = 0;
3933 rb_ary_keep_if(
VALUE ary)
3936 rb_ary_select_bang(ary);
3941 ary_resize_smaller(
VALUE ary,
long len)
3945 ARY_SET_LEN(ary,
len);
3946 if (
len * 2 < ARY_CAPA(ary) &&
3947 ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
3948 ary_resize_capa(ary,
len * 2);
3996 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4015 ary_resize_smaller(ary, i2);
4026 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4041 ary_resize_smaller(ary, i2);
4053 if (pos < 0)
return Qnil;
4061 ARY_INCREASE_LEN(ary, -1);
4101 ary_slice_bang_by_rb_ary_splice(
VALUE ary,
long pos,
long len)
4108 else if (pos < -orig_len) {
4114 else if (orig_len < pos) {
4117 if (orig_len < pos +
len) {
4118 len = orig_len - pos;
4125 rb_ary_splice(ary, pos,
len, 0, 0);
4223 rb_ary_slice_bang(
int argc,
VALUE *argv,
VALUE ary)
4228 rb_ary_modify_check(ary);
4235 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4242 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4271 reject_bang_i(
VALUE a)
4274 VALUE ary = arg->ary;
4277 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4285 return (i1 == i2) ?
Qnil : ary;
4289 ary_reject_bang(
VALUE ary)
4292 rb_ary_modify_check(ary);
4294 args.len[0] = args.len[1] = 0;
4319 rb_ary_reject_bang(
VALUE ary)
4323 return ary_reject_bang(ary);
4344 rb_ary_reject(
VALUE ary)
4350 ary_reject(ary, rejected_ary);
4351 return rejected_ary;
4372 rb_ary_delete_if(
VALUE ary)
4376 ary_reject_bang(ary);
4391 take_items(
VALUE obj,
long n)
4396 if (n == 0)
return result;
4399 args[0] = result; args[1] = (
VALUE)n;
4400 if (UNDEF_P(rb_check_block_call(obj, idEach, 0, 0, take_i, (
VALUE)args)))
4477 for (i=0; i<argc; i++) {
4478 argv[i] = take_items(argv[i],
len);
4482 int arity = rb_block_arity();
4491 for (j=0; j<argc; j++) {
4492 tmp[j+1] = rb_ary_elt(argv[j], i);
4504 for (j=0; j<argc; j++) {
4514 for (i=0; i<
len; i++) {
4518 for (j=0; j<argc; j++) {
4544 rb_ary_transpose(
VALUE ary)
4546 long elen = -1, alen, i, j;
4547 VALUE tmp, result = 0;
4551 for (i=0; i<alen; i++) {
4552 tmp = to_ary(rb_ary_elt(ary, i));
4556 for (j=0; j<elen; j++) {
4564 for (j=0; j<elen; j++) {
4565 rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
4589 rb_ary_modify_check(copy);
4590 orig = to_ary(orig);
4591 if (copy == orig)
return copy;
4596 if (
RARRAY_LEN(orig) <= ary_embed_capa(copy)) {
4603 else if (ARY_EMBED_P(orig)) {
4604 long len = ARY_EMBED_LEN(orig);
4607 FL_UNSET_EMBED(copy);
4608 ARY_SET_PTR(copy,
ptr);
4609 ARY_SET_LEN(copy,
len);
4610 ARY_SET_CAPA(copy,
len);
4619 VALUE shared_root = ary_make_shared(orig);
4620 FL_UNSET_EMBED(copy);
4621 ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
4622 ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
4623 rb_ary_set_shared(copy, shared_root);
4644 rb_ary_modify_check(ary);
4645 if (ARY_SHARED_P(ary)) {
4646 rb_ary_unshare(ary);
4648 ARY_SET_EMBED_LEN(ary, 0);
4651 ARY_SET_LEN(ary, 0);
4652 if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
4653 ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
4841 rb_ary_fill(
int argc,
VALUE *argv,
VALUE ary)
4844 long beg = 0, end = 0,
len = 0;
4867 if (beg < 0) beg = 0;
4876 if (beg >= ARY_MAX_SIZE ||
len > ARY_MAX_SIZE - beg) {
4881 if (end >= ARY_CAPA(ary)) {
4882 ary_resize_capa(ary, end);
4885 ARY_SET_LEN(ary, end);
4888 if (UNDEF_P(item)) {
4892 for (i=beg; i<end; i++) {
4899 ary_memfill(ary, beg,
len, item);
4921 long len, xlen, ylen;
4931 ARY_SET_LEN(z,
len);
4960 rb_ary_concat_multi(
int argc,
VALUE *argv,
VALUE ary)
4962 rb_ary_modify_check(ary);
4967 else if (argc > 1) {
4970 for (i = 0; i < argc; i++) {
4973 ary_append(ary, args);
4983 return ary_append(x, to_ary(y));
5030 ARY_SET_LEN(ary2,
len);
5035 ary_memcpy(ary2, 0, t,
ptr);
5036 while (t <=
len/2) {
5113 recursive_equal(
VALUE ary1,
VALUE ary2,
int recur)
5116 const VALUE *p1, *p2;
5118 if (recur)
return Qtrue;
5125 for (i = 0; i < len1; i++) {
5173 if (ary1 == ary2)
return Qtrue;
5186 recursive_eql(
VALUE ary1,
VALUE ary2,
int recur)
5190 if (recur)
return Qtrue;
5192 if (!
rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
5220 if (ary1 == ary2)
return Qtrue;
5228 rb_ary_hash_values(
long len,
const VALUE *elements)
5236 for (i=0; i<
len; i++) {
5260 rb_ary_hash(
VALUE ary)
5295 rb_ary_includes_by_eql(
VALUE ary,
VALUE item)
5310 recursive_cmp(
VALUE ary1,
VALUE ary2,
int recur)
5314 if (recur)
return Qundef;
5319 for (i=0; i<
len; i++) {
5320 VALUE e1 = rb_ary_elt(ary1, i), e2 = rb_ary_elt(ary2, i);
5375 if (ary1 == ary2)
return INT2FIX(0);
5377 if (!UNDEF_P(v))
return v;
5391 rb_hash_add_new_element(hash, elt, elt);
5397 ary_tmp_hash_new(
VALUE ary)
5400 VALUE hash = rb_hash_new_with_size(size);
5402 RBASIC_CLEAR_CLASS(hash);
5407 ary_make_hash(
VALUE ary)
5409 VALUE hash = ary_tmp_hash_new(ary);
5410 return ary_add_hash(hash, ary);
5420 rb_hash_add_new_element(hash, k, v);
5426 ary_make_hash_by(
VALUE ary)
5428 VALUE hash = ary_tmp_hash_new(ary);
5429 return ary_add_hash_by(hash, ary);
5457 ary2 = to_ary(ary2);
5458 if (
RARRAY_LEN(ary2) == 0) {
return ary_make_shared_copy(ary1); }
5463 VALUE elt = rb_ary_elt(ary1, i);
5464 if (rb_ary_includes_by_eql(ary2, elt))
continue;
5470 hash = ary_make_hash(ary2);
5472 if (rb_hash_stlike_lookup(hash,
RARRAY_AREF(ary1, i), NULL))
continue;
5499 rb_ary_difference_multi(
int argc,
VALUE *argv,
VALUE ary)
5504 bool *is_hash =
ALLOCV_N(
bool, t0, argc);
5508 for (i = 0; i < argc; i++) {
5509 argv[i] = to_ary(argv[i]);
5510 is_hash[i] = (length > SMALL_ARRAY_LEN &&
RARRAY_LEN(argv[i]) > SMALL_ARRAY_LEN);
5511 if (is_hash[i]) argv[i] = ary_make_hash(argv[i]);
5516 VALUE elt = rb_ary_elt(ary, i);
5517 for (j = 0; j < argc; j++) {
5519 if (rb_hash_stlike_lookup(argv[j],
RARRAY_AREF(ary, i), NULL))
5523 if (rb_ary_includes_by_eql(argv[j], elt))
break;
5562 VALUE hash, ary3, v;
5566 ary2 = to_ary(ary2);
5573 if (!rb_ary_includes_by_eql(ary2, v))
continue;
5574 if (rb_ary_includes_by_eql(ary3, v))
continue;
5580 hash = ary_make_hash(ary2);
5585 if (rb_hash_stlike_delete(hash, &vv, 0)) {
5615 rb_ary_intersection_multi(
int argc,
VALUE *argv,
VALUE ary)
5620 for (i = 0; i < argc; i++) {
5621 result = rb_ary_and(result, argv[i]);
5628 ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
5630 if (existing)
return ST_STOP;
5631 *key = *value = (
VALUE)arg;
5640 VALUE elt = rb_ary_elt(ary, i);
5641 if (rb_ary_includes_by_eql(ary_union, elt))
continue;
5652 if (!rb_hash_stlike_update(hash, (st_data_t)elt, ary_hash_orset, (st_data_t)elt)) {
5678 ary2 = to_ary(ary2);
5681 rb_ary_union(ary3, ary1);
5682 rb_ary_union(ary3, ary2);
5686 hash = ary_make_hash(ary1);
5687 rb_ary_union_hash(hash, ary2);
5689 return rb_hash_values(hash);
5716 rb_ary_union_multi(
int argc,
VALUE *argv,
VALUE ary)
5723 for (i = 0; i < argc; i++) {
5724 argv[i] = to_ary(argv[i]);
5728 if (sum <= SMALL_ARRAY_LEN) {
5731 rb_ary_union(ary_union, ary);
5732 for (i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]);
5737 hash = ary_make_hash(ary);
5738 for (i = 0; i < argc; i++) rb_ary_union_hash(hash, argv[i]);
5740 return rb_hash_values(hash);
5760 VALUE hash, v, result, shorter, longer;
5764 ary2 = to_ary(ary2);
5770 if (rb_ary_includes_by_eql(ary2, v))
return Qtrue;
5782 hash = ary_make_hash(shorter);
5788 if (rb_hash_stlike_lookup(hash, vv, 0)) {
5798 ary_max_generic(
VALUE ary,
long i,
VALUE vmax)
5815 ary_max_opt_fixnum(
VALUE ary,
long i,
VALUE vmax)
5822 for (; i < n; ++i) {
5826 if ((
long)vmax < (
long)v) {
5831 return ary_max_generic(ary, i, vmax);
5839 ary_max_opt_float(
VALUE ary,
long i,
VALUE vmax)
5846 for (; i < n; ++i) {
5850 if (rb_float_cmp(vmax, v) < 0) {
5855 return ary_max_generic(ary, i, vmax);
5863 ary_max_opt_string(
VALUE ary,
long i,
VALUE vmax)
5870 for (; i < n; ++i) {
5879 return ary_max_generic(ary, i, vmax);
5942 return rb_nmin_run(ary, num, 0, 1, 1);
5956 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
5957 return ary_max_opt_fixnum(ary, 1, result);
5959 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
5960 return ary_max_opt_string(ary, 1, result);
5963 return ary_max_opt_float(ary, 1, result);
5966 return ary_max_generic(ary, 1, result);
5970 if (UNDEF_P(result))
return Qnil;
5975 ary_min_generic(
VALUE ary,
long i,
VALUE vmin)
5992 ary_min_opt_fixnum(
VALUE ary,
long i,
VALUE vmin)
5999 for (; i < n; ++i) {
6003 if ((
long)vmin > (
long)a) {
6008 return ary_min_generic(ary, i, vmin);
6016 ary_min_opt_float(
VALUE ary,
long i,
VALUE vmin)
6023 for (; i < n; ++i) {
6027 if (rb_float_cmp(vmin, a) > 0) {
6032 return ary_min_generic(ary, i, vmin);
6040 ary_min_opt_string(
VALUE ary,
long i,
VALUE vmin)
6047 for (; i < n; ++i) {
6056 return ary_min_generic(ary, i, vmin);
6119 return rb_nmin_run(ary, num, 0, 0, 1);
6133 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6134 return ary_min_opt_fixnum(ary, 1, result);
6136 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6137 return ary_min_opt_string(ary, 1, result);
6140 return ary_min_opt_float(ary, 1, result);
6143 return ary_min_generic(ary, 1, result);
6147 if (UNDEF_P(result))
return Qnil;
6174 rb_ary_minmax(
VALUE ary)
6179 return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary));
6183 push_value(st_data_t key, st_data_t val, st_data_t ary)
6217 rb_ary_uniq_bang(
VALUE ary)
6222 rb_ary_modify_check(ary);
6226 hash = ary_make_hash_by(ary);
6228 hash = ary_make_hash(ary);
6234 rb_ary_modify_check(ary);
6235 ARY_SET_LEN(ary, 0);
6236 if (ARY_SHARED_P(ary)) {
6237 rb_ary_unshare(ary);
6240 ary_resize_capa(ary, hash_size);
6273 rb_ary_uniq(
VALUE ary)
6282 hash = ary_make_hash_by(ary);
6283 uniq = rb_hash_values(hash);
6286 hash = ary_make_hash(ary);
6287 uniq = rb_hash_values(hash);
6310 rb_ary_compact_bang(
VALUE ary)
6327 ary_resize_smaller(ary, n);
6347 rb_ary_compact(
VALUE ary)
6350 rb_ary_compact_bang(ary);
6382 rb_ary_count(
int argc,
VALUE *argv,
VALUE ary)
6398 VALUE obj = argv[0];
6401 rb_warn(
"given block not used");
6412 flatten(
VALUE ary,
int level)
6415 VALUE stack, result, tmp = 0, elt;
6431 ARY_SET_LEN(result, i);
6433 stack = ary_new(0, ARY_DEFAULT_SIZE);
6449 if (level >= 0 &&
RARRAY_LEN(stack) / 2 >= level) {
6454 if (
RBASIC(result)->klass) {
6532 rb_ary_flatten_bang(
int argc,
VALUE *argv,
VALUE ary)
6534 int mod = 0, level = -1;
6538 rb_ary_modify_check(ary);
6540 if (level == 0)
return Qnil;
6542 result = flatten(ary, level);
6543 if (result == ary) {
6548 if (mod) ARY_SET_EMBED_LEN(result, 0);
6589 rb_ary_flatten(
int argc,
VALUE *argv,
VALUE ary)
6596 if (level == 0)
return ary_make_shared_copy(ary);
6599 result = flatten(ary, level);
6600 if (result == ary) {
6601 result = ary_make_shared_copy(ary);
6607 #define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1)
6618 long j = RAND_UPTO(i);
6635 rb_ary_shuffle_bang(ec, ary, randgen);
6644 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
6651 long n,
len, i, j, k, idx[10];
6652 long rnds[numberof(idx)];
6653 long memo_threshold;
6662 return rb_ary_elt(ary, i);
6667 if (n <= numberof(idx)) {
6668 for (i = 0; i < n; ++i) {
6669 rnds[i] = RAND_UPTO(
len - i);
6674 if (
len < k && n <= numberof(idx)) {
6675 for (i = 0; i < n; ++i) {
6697 if (j >= i) l = i, g = ++j;
6698 if (k >= l && (++k >= g)) ++k;
6707 if (n <= numberof(idx)) {
6708 long sorted[numberof(idx)];
6709 sorted[0] = idx[0] = rnds[0];
6710 for (i=1; i<n; i++) {
6712 for (j = 0; j < i; ++j) {
6713 if (k < sorted[j])
break;
6716 memmove(&sorted[j+1], &sorted[j],
sizeof(sorted[0])*(i-j));
6717 sorted[j] = idx[i] = k;
6721 for (i=0; i<n; i++) {
6726 else if (n <= memo_threshold / 2) {
6729 st_table *memo = st_init_numtable_with_size(n);
6733 for (i=0; i<n; i++) {
6734 long r = RAND_UPTO(
len-i) + i;
6736 if (r > max_idx) max_idx = r;
6739 if (
len <= max_idx) n = 0;
6740 else if (n >
len) n =
len;
6742 for (i=0; i<n; i++) {
6743 long j2 = j = ptr_result[i];
6746 if (st_lookup(memo, (st_data_t)i, &value)) i2 = (
long)value;
6747 if (st_lookup(memo, (st_data_t)j, &value)) j2 = (
long)value;
6748 st_insert(memo, (st_data_t)j, (st_data_t)i2);
6749 ptr_result[i] = ptr_ary[j2];
6754 st_free_table(memo);
6759 RBASIC_CLEAR_CLASS(result);
6762 for (i=0; i<n; i++) {
6763 j = RAND_UPTO(
len-i) + i;
6765 ptr_result[j] = ptr_result[i];
6769 RBASIC_SET_CLASS_RAW(result,
rb_cArray);
6771 ARY_SET_LEN(result, n);
6793 if (mul <= 0)
return INT2FIX(0);
6795 return rb_fix_mul_fix(rb_ary_length(
self), n);
6832 rb_ary_cycle(
int argc,
VALUE *argv,
VALUE ary)
6839 if (argc == 0 ||
NIL_P(argv[0])) {
6844 if (n <= 0)
return Qnil;
6847 while (
RARRAY_LEN(ary) > 0 && (n < 0 || 0 < n--)) {
6861 yield_indexed_values(
const VALUE values,
const long r,
const long *
const p)
6866 for (i = 0; i < r; i++) ARY_SET(result, i,
RARRAY_AREF(values, p[i]));
6867 ARY_SET_LEN(result, r);
6869 return !
RBASIC(values)->klass;
6885 permute0(
const long n,
const long r,
long *
const p,
char *
const used,
const VALUE values)
6887 long i = 0, index = 0;
6890 const char *
const unused = memchr(&used[i], 0, n-i);
6905 for (i = 0; i < n; ++i) {
6906 if (used[i])
continue;
6908 if (!yield_indexed_values(values, r, p)) {
6924 descending_factorial(
long from,
long how_many)
6929 while (--how_many > 0) {
6931 cnt = rb_int_mul(cnt,
LONG2FIX(v));
6941 binomial_coefficient(
long comb,
long size)
6945 if (comb > size-comb) {
6951 else if (comb == 0) {
6955 for (i = 1; i < comb; ++i) {
6956 r = rb_int_mul(r,
LONG2FIX(size - i));
6957 r = rb_int_idiv(r,
LONG2FIX(i + 1));
6968 return descending_factorial(n, k);
7014 rb_ary_permutation(
int argc,
VALUE *argv,
VALUE ary)
7024 if (r < 0 || n < r) {
7037 long *p =
ALLOCV_N(
long, t0, r+roomof(n,
sizeof(
long)));
7038 char *used = (
char*)(p + r);
7039 VALUE ary0 = ary_make_shared_copy(ary);
7040 RBASIC_CLEAR_CLASS(ary0);
7044 permute0(n, r, p, used, ary0);
7052 combinate0(
const long len,
const long n,
long *
const stack,
const VALUE values)
7059 for (lev++; lev < n; lev++) {
7060 stack[lev+1] = stack[lev]+1;
7062 if (!yield_indexed_values(values, n, stack+1)) {
7066 if (lev == 0)
return;
7068 }
while (stack[lev+1]+n ==
len+lev+1);
7078 return binomial_coefficient(k, n);
7133 if (n < 0 ||
len < n) {
7145 VALUE ary0 = ary_make_shared_copy(ary);
7147 long *stack =
ALLOCV_N(
long, t0, n+1);
7149 RBASIC_CLEAR_CLASS(ary0);
7150 combinate0(
len, n, stack, ary0);
7170 rpermute0(
const long n,
const long r,
long *
const p,
const VALUE values)
7172 long i = 0, index = 0;
7176 if (++index < r-1) {
7180 for (i = 0; i < n; ++i) {
7182 if (!yield_indexed_values(values, r, p)) {
7187 if (index <= 0)
return;
7188 }
while ((i = ++p[--index]) >= n);
7246 rb_ary_repeated_permutation(
VALUE ary,
VALUE num)
7268 VALUE ary0 = ary_make_shared_copy(ary);
7269 RBASIC_CLEAR_CLASS(ary0);
7271 rpermute0(n, r, p, ary0);
7279 rcombinate0(
const long n,
const long r,
long *
const p,
const long rest,
const VALUE values)
7281 long i = 0, index = 0;
7285 if (++index < r-1) {
7289 for (; i < n; ++i) {
7291 if (!yield_indexed_values(values, r, p)) {
7296 if (index <= 0)
return;
7297 }
while ((i = ++p[--index]) >= n);
7309 return binomial_coefficient(k, n + k - 1);
7352 rb_ary_repeated_combination(
VALUE ary,
VALUE num)
7370 else if (
len == 0) {
7376 VALUE ary0 = ary_make_shared_copy(ary);
7377 RBASIC_CLEAR_CLASS(ary0);
7379 rcombinate0(
len, n, p, n, ary0);
7440 rb_ary_product(
int argc,
VALUE *argv,
VALUE ary)
7446 int *counters =
ALLOCV_N(
int, t1, n);
7451 RBASIC_CLEAR_CLASS(t0);
7456 for (i = 1; i < n; i++) arrays[i] =
Qnil;
7457 for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
7460 for (i = 0; i < n; i++) counters[i] = 0;
7465 for (i = 0; i < n; i++) {
7467 arrays[i] = ary_make_shared_copy(arrays[i]);
7472 for (i = 0; i < n; i++) {
7478 if (MUL_OVERFLOW_LONG_P(resultlen, k))
7488 for (j = 0; j < n; j++) {
7493 if (
NIL_P(result)) {
7494 FL_SET(t0, RARRAY_SHARED_ROOT_FLAG);
7496 if (!
FL_TEST(t0, RARRAY_SHARED_ROOT_FLAG)) {
7500 FL_UNSET(t0, RARRAY_SHARED_ROOT_FLAG);
7513 while (counters[m] ==
RARRAY_LEN(arrays[m])) {
7516 if (--m < 0)
goto done;
7524 return NIL_P(result) ? ary : result;
7577 rb_ary_take_while(
VALUE ary)
7585 return rb_ary_take(ary,
LONG2FIX(i));
7640 rb_ary_drop_while(
VALUE ary)
7648 return rb_ary_drop(ary,
LONG2FIX(i));
7691 rb_ary_any_p(
int argc,
VALUE *argv,
VALUE ary)
7699 rb_warn(
"given block not used");
7706 for (i = 0; i <
len; ++i) {
7758 rb_ary_all_p(
int argc,
VALUE *argv,
VALUE ary)
7766 rb_warn(
"given block not used");
7773 for (i = 0; i <
len; ++i) {
7819 rb_ary_none_p(
int argc,
VALUE *argv,
VALUE ary)
7827 rb_warn(
"given block not used");
7834 for (i = 0; i <
len; ++i) {
7883 rb_ary_one_p(
int argc,
VALUE *argv,
VALUE ary)
7892 rb_warn(
"given block not used");
7896 if (result)
return Qfalse;
7902 for (i = 0; i <
len; ++i) {
7904 if (result)
return Qfalse;
7912 if (result)
return Qfalse;
7941 rb_ary_dig(
int argc,
VALUE *argv,
VALUE self)
7944 self = rb_ary_at(
self, *argv);
7945 if (!--argc)
return self;
7947 return rb_obj_dig(argc, argv,
self,
Qnil);
7951 finish_exact_sum(
long n,
VALUE r,
VALUE v,
int z)
7956 v = rb_rational_plus(r, v);
8024 goto init_is_a_value;
8038 else if (RB_BIGNUM_TYPE_P(e))
8044 r = rb_rational_plus(r, e);
8049 v = finish_exact_sum(n, r, v, argc!=0);
8053 v = finish_exact_sum(n, r, v, i!=0);
8065 goto has_float_value;
8075 else if (RB_BIGNUM_TYPE_P(e))
8082 if (isnan(f))
continue;
8088 if (isinf(f) && signbit(x) != signbit(f))
8094 if (isinf(f))
continue;
8097 if (fabs(f) >= fabs(x))
8110 goto has_some_value;
8124 rb_ary_deconstruct(
VALUE ary)
8631 fake_ary_flags = init_fake_ary_flags();
8758 rb_vm_register_global_object(rb_cArray_empty_frozen);
8761 #include "array.rbinc"
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
#define rb_ary_new4
Old name of rb_ary_new_from_values.
#define FIXABLE
Old name of RB_FIXABLE.
#define LONG2FIX
Old name of RB_INT2FIX.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define NUM2DBL
Old name of rb_num2dbl.
#define FL_SET
Old name of RB_FL_SET.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define NUM2INT
Old name of RB_NUM2INT.
#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 NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define DBL2NUM
Old name of rb_float_new.
#define FL_TEST
Old name of RB_FL_TEST.
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
void rb_iter_break(void)
Breaks from a block.
VALUE rb_eFrozenError
FrozenError exception.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
VALUE rb_cArray
Array class.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Identical to rb_class_new_instance(), except it passes the passed keywords if any to the #initialize ...
VALUE rb_obj_frozen_p(VALUE obj)
Just calls RB_OBJ_FROZEN() inside.
int rb_eql(VALUE lhs, VALUE rhs)
Checks for equality of the passed objects, in terms of Object#eql?.
VALUE rb_cNumeric
Numeric class.
VALUE rb_cRandom
Random class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
double rb_num2dbl(VALUE num)
Converts an instance of rb_cNumeric into C's double.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
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_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Identical to rb_enc_associate_index(), except it takes an encoding itself instead of its index.
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
void rb_enc_copy(VALUE dst, VALUE src)
Destructively copies the encoding of the latter object to that of former one.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
#define RGENGC_WB_PROTECTED_ARRAY
This is a compile-time flag to enable/disable write barrier for struct RArray.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_rotate(VALUE ary, long rot)
Destructively rotates the passed array in-place to towards its end.
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_cmp(VALUE lhs, VALUE rhs)
Recursively compares each elements of the two arrays one-by-one using <=>.
VALUE rb_ary_rassoc(VALUE alist, VALUE key)
Identical to rb_ary_assoc(), except it scans the passed array from the opposite direction.
VALUE rb_ary_concat(VALUE lhs, VALUE rhs)
Destructively appends the contents of latter into the end of former.
VALUE rb_ary_assoc(VALUE alist, VALUE key)
Looks up the passed key, assuming the passed array is an alist.
VALUE rb_ary_reverse(VALUE ary)
Destructively reverses the passed array in-place.
VALUE rb_ary_shared_with_p(VALUE lhs, VALUE rhs)
Queries if the passed two arrays share the same backend storage.
VALUE rb_ary_shift(VALUE ary)
Destructively deletes an element from the beginning of the passed array and returns what was deleted.
VALUE rb_ary_sort(VALUE ary)
Creates a copy of the passed array, whose elements are sorted according to their <=> result.
VALUE rb_ary_resurrect(VALUE ary)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_includes(VALUE ary, VALUE elem)
Queries if the passed array has the passed entry.
VALUE rb_ary_aref(int argc, const VALUE *argv, VALUE ary)
Queries element(s) of an array.
VALUE rb_get_values_at(VALUE obj, long olen, int argc, const VALUE *argv, VALUE(*func)(VALUE obj, long oidx))
This was a generalisation of Array#values_at, Struct#values_at, and MatchData#values_at.
void rb_ary_free(VALUE ary)
Destroys the given array for no reason.
VALUE rb_ary_each(VALUE ary)
Iteratively yields each element of the passed array to the implicitly passed block if any.
VALUE rb_ary_delete_at(VALUE ary, long pos)
Destructively removes an element which resides at the specific index of the passed array.
VALUE rb_ary_unshift(VALUE ary, VALUE elem)
Destructively prepends the passed item at the beginning of the passed array.
VALUE rb_ary_plus(VALUE lhs, VALUE rhs)
Creates a new array, concatenating the former to the latter.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
void rb_ary_modify(VALUE ary)
Declares that the array is about to be modified.
VALUE rb_ary_replace(VALUE copy, VALUE orig)
Replaces the contents of the former object with the contents of the latter.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_to_ary(VALUE obj)
Force converts an object to an array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
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_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_subseq(VALUE ary, long beg, long len)
Obtains a part of the passed array.
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_to_s(VALUE ary)
Converts an array into a human-readable string.
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_sort_bang(VALUE ary)
Destructively sorts the passed array in-place, according to each elements' <=> result.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_mem_clear(VALUE *buf, long len)
Fills the memory region with a series of RUBY_Qnil.
VALUE rb_ary_delete(VALUE ary, VALUE elem)
Destructively removes elements from the passed array, so that there would be no elements inside that ...
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_big_plus(VALUE x, VALUE y)
Performs addition of the passed two objects.
double rb_big2dbl(VALUE x)
Converts a bignum into C's double.
int rb_cmpint(VALUE val, VALUE a, VALUE b)
Canonicalises the passed val, which is the return value of a <=> b, into C's {-1, 0,...
VALUE rb_arithmetic_sequence_beg_len_step(VALUE as, long *begp, long *lenp, long *stepp, long len, int err)
Identical to rb_range_beg_len(), except it takes an instance of Enumerator::ArithmericSequence.
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_obj_call_init_kw(VALUE, int, const VALUE *, int)
Identical to rb_obj_call_init(), except you can specify how to handle the last element of the given a...
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_hash_delete(VALUE hash, VALUE key)
Deletes the passed key from the passed hash table, if any.
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_hash(VALUE obj)
Calculates a message authentication code of the passed object.
VALUE rb_hash_clear(VALUE hash)
Swipes everything out of the passed hash table.
VALUE rb_output_fs
The field separator character for outputs, or the $,.
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
VALUE rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
Deconstructs a numerical range.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
#define rb_hash_end(h)
Just another name of st_hash_end.
VALUE rb_usascii_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "US ASCII" encoding.
VALUE rb_usascii_str_new_cstr(const char *ptr)
Identical to rb_str_new_cstr(), except it generates a string of "US ASCII" encoding.
VALUE rb_str_buf_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
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.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
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,...
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.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
int capa
Designed capacity of the buffer.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_block_call(VALUE obj, ID mid, int argc, const VALUE *argv, rb_block_call_func_t proc, VALUE data2)
Identical to rb_funcallv(), except it additionally passes a function as a block.
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_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
static VALUE * RARRAY_PTR(VALUE ary)
Wild use of a C pointer.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY(obj)
Convenient casting macro.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RBASIC(obj)
Convenient casting macro.
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define StringValue(v)
Ensures that the parameter object is a String.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
#define RTEST
This is an old name of RB_TEST.
struct RArray::@43::@44 heap
Arrays that use separated memory region for elements use this pattern.
struct RBasic basic
Basic part, including flags and class.
const VALUE shared_root
Parent of the array.
const VALUE ary[1]
Embedded elements.
union RArray::@43 as
Array's specific fields.
VALUE flags
Per-object flags.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.