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"
41VALUE 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
76should_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); \
181ary_embed_capa(
VALUE ary)
183 size_t size = rb_gc_obj_slot_size(ary) - offsetof(
struct RArray, as.
ary);
185 return size /
sizeof(
VALUE);
189ary_embed_size(
long capa)
195ary_embeddable_p(
long capa)
197 return rb_gc_size_allocatable_p(ary_embed_size(
capa));
201rb_ary_embeddable_p(
VALUE ary)
211 return !(ARY_SHARED_ROOT_P(ary) ||
OBJ_FROZEN(ary) || ARY_SHARED_P(ary));
215rb_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__)
236ary_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++) {
267#define ary_verify(ary) ((void)0)
296ary_mem_clear(
VALUE ary,
long beg,
long size)
299 rb_mem_clear(
ptr + beg, size);
304memfill(
register VALUE *mem,
register long size,
register VALUE val)
315 memfill(
ptr + beg, size, val);
325 if (argc > (
int)(128/
sizeof(
VALUE)) ) {
326 rb_gc_writebarrier_remember(buff_owner_ary);
334 for (i=0; i<argc; i++) {
344 ary_memcpy0(
ary, beg, argc, argv,
ary);
348ary_heap_alloc_buffer(
size_t capa)
356 ruby_sized_xfree((
void *)
ptr, size);
362 ary_heap_free_ptr(
ary, ARY_HEAP_PTR(
ary), ARY_HEAP_SIZE(
ary));
366ary_heap_realloc(
VALUE ary,
size_t new_capa)
379 if (!ARY_EMBED_P(
ary)) {
380 const VALUE *buf = ARY_HEAP_PTR(
ary);
381 long len = ARY_HEAP_LEN(
ary);
384 ARY_SET_EMBED_LEN(
ary,
len);
393ary_resize_capa(
VALUE ary,
long capacity)
399 if (capacity > ary_embed_capa(
ary)) {
400 size_t new_capa = capacity;
401 if (ARY_EMBED_P(
ary)) {
402 long len = ARY_EMBED_LEN(
ary);
403 VALUE *
ptr = ary_heap_alloc_buffer(capacity);
408 ARY_SET_HEAP_LEN(
ary,
len);
411 new_capa = ary_heap_realloc(
ary, capacity);
413 ARY_SET_CAPA(
ary, new_capa);
416 if (!ARY_EMBED_P(
ary)) {
417 long len = ARY_HEAP_LEN(
ary);
418 long old_capa = ARY_HEAP_CAPA(
ary);
421 if (
len > capacity)
len = capacity;
423 ary_heap_free_ptr(
ary,
ptr, old_capa);
436 long capacity = ARY_HEAP_LEN(
ary);
437 long old_capa = ARY_HEAP_CAPA(
ary);
440 if (old_capa > capacity) {
441 size_t new_capa = ary_heap_realloc(
ary, capacity);
442 ARY_SET_CAPA(
ary, new_capa);
451 long new_capa = ARY_CAPA(
ary) / 2;
453 if (new_capa < ARY_DEFAULT_SIZE) {
454 new_capa = ARY_DEFAULT_SIZE;
456 if (new_capa >= ARY_MAX_SIZE - min) {
457 new_capa = (ARY_MAX_SIZE - min) / 2;
460 ary_resize_capa(
ary, new_capa);
479 FL_UNSET_SHARED(
ary);
485 if (ARY_OWNS_HEAP_P(
ary)) {
488 else if (ARY_SHARED_P(
ary)) {
493 ARY_SET_EMBED_LEN(
ary, 0);
518 RB_DEBUG_COUNTER_INC(obj_ary_shared_create);
524 rb_check_frozen(
ary);
531 if (ARY_SHARED_P(
ary)) {
537 if (
len <= ary_embed_capa(
ary)) {
539 FL_UNSET_SHARED(
ary);
543 ARY_SET_EMBED_LEN(
ary,
len);
547 FL_UNSET_SHARED(
ary);
549 ARY_SET_CAPA(
ary, shared_len);
564 rb_gc_writebarrier_remember(
ary);
572 rb_ary_modify_check(
ary);
573 rb_ary_cancel_sharing(
ary);
577ary_ensure_room_for_push(
VALUE ary,
long add_len)
580 long new_len = old_len + add_len;
583 if (old_len > ARY_MAX_SIZE - add_len) {
586 if (ARY_SHARED_P(
ary)) {
587 if (new_len > ary_embed_capa(
ary)) {
591 rb_ary_modify_check(
ary);
602 ary_double_capa(
ary, new_len);
613 rb_ary_modify_check(
ary);
616 if (new_len >
capa) {
617 ary_double_capa(
ary, new_len);
648 if (!ARY_EMBED_P(
ary) && !ARY_SHARED_P(
ary) && !ARY_SHARED_ROOT_P(
ary)) {
649 ary_shrink_capa(
ary);
665 if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) &&
666 !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) &&
667 ARY_SHARED_ROOT(ary1) == ARY_SHARED_ROOT(ary2) &&
668 ARY_HEAP_LEN(ary1) == ARY_HEAP_LEN(ary2)) {
677 size_t size = ary_embed_size(
capa);
690ary_alloc_heap(
VALUE klass)
694 sizeof(struct
RArray), 0);
699empty_ary_alloc(
VALUE klass)
701 RUBY_DTRACE_CREATE_HOOK(ARRAY, 0);
702 return ary_alloc_embed(klass, 0);
711 rb_raise(rb_eArgError,
"negative array size (or size too big)");
713 if (
capa > ARY_MAX_SIZE) {
714 rb_raise(rb_eArgError,
"array size too big");
717 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
719 if (ary_embeddable_p(
capa)) {
720 ary = ary_alloc_embed(klass,
capa);
723 ary = ary_alloc_heap(klass);
727 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
728 ARY_SET_HEAP_LEN(
ary, 0);
743 return rb_ary_new_capa(0);
747(rb_ary_new_from_args)(
long n, ...)
756 for (i=0; i<n; i++) {
766rb_ary_tmp_new_from_values(
VALUE klass,
long n,
const VALUE *elts)
770 ary = ary_new(klass, n);
772 ary_memcpy(
ary, 0, n, elts);
780rb_ary_new_from_values(
long n,
const VALUE *elts)
782 return rb_ary_tmp_new_from_values(
rb_cArray, n, elts);
788 size_t size = ary_embed_size(
capa);
805 sizeof(struct
RArray), ec);
815 rb_raise(rb_eArgError,
"negative array size (or size too big)");
817 if (
capa > ARY_MAX_SIZE) {
818 rb_raise(rb_eArgError,
"array size too big");
821 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
823 if (ary_embeddable_p(
capa)) {
824 ary = ec_ary_alloc_embed(ec, klass,
capa);
827 ary = ec_ary_alloc_heap(ec, klass);
831 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
832 ARY_SET_HEAP_LEN(
ary, 0);
845 ary_memcpy(
ary, 0, n, elts);
860rb_ary_hidden_new_fill(
long capa)
871 if (ARY_OWNS_HEAP_P(
ary)) {
872 if (USE_DEBUG_COUNTER &&
873 !ARY_SHARED_ROOT_P(
ary) &&
875 RB_DEBUG_COUNTER_INC(obj_ary_extracapa);
878 RB_DEBUG_COUNTER_INC(obj_ary_ptr);
882 RB_DEBUG_COUNTER_INC(obj_ary_embed);
885 if (ARY_SHARED_P(
ary)) {
886 RB_DEBUG_COUNTER_INC(obj_ary_shared);
888 if (ARY_SHARED_ROOT_P(
ary) && ARY_SHARED_ROOT_OCCUPIED(
ary)) {
889 RB_DEBUG_COUNTER_INC(obj_ary_shared_root_occupied);
893static VALUE fake_ary_flags;
896init_fake_ary_flags(
void)
898 struct RArray fake_ary = {0};
906rb_setup_fake_ary(
struct RArray *fake_ary,
const VALUE *list,
long len)
909 RBASIC_CLEAR_CLASS((
VALUE)fake_ary);
915 return (
VALUE)fake_ary;
921 if (ARY_OWNS_HEAP_P(
ary)) {
922 return ARY_CAPA(
ary) *
sizeof(
VALUE);
934 if (ARY_SHARED_P(
ary)) {
935 return ARY_SHARED_ROOT(
ary);
937 else if (ARY_SHARED_ROOT_P(
ary)) {
949 VALUE shared = ary_alloc_heap(0);
950 FL_SET_SHARED_ROOT(shared);
952 if (ARY_EMBED_P(
ary)) {
954 ARY_SET_PTR(shared,
ptr);
958 ARY_SET_HEAP_LEN(
ary,
len);
965 ARY_SET_LEN(shared,
capa);
967 rb_ary_set_shared(
ary, shared);
981 if (ary_embeddable_p(
len)) {
986 ARY_SET_EMBED_LEN(subst,
len);
990 return rb_ary_increment_share(ary_make_shared(
ary));
1003 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1005#define to_ary rb_to_array_type
1010 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1016 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1022 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1046 return rb_check_array_type(
ary);
1051rb_ary_s_new(
int argc,
VALUE *argv,
VALUE klass)
1057 if (argc > 0 &&
FIXNUM_P(argv[0])) {
1059 if (size < 0) size = 0;
1062 ary = ary_new(klass, size);
1141 if (argc == 1 && !
FIXNUM_P(size)) {
1142 val = rb_check_array_type(size);
1144 rb_ary_replace(
ary, val);
1152 rb_raise(rb_eArgError,
"negative array size");
1154 if (
len > ARY_MAX_SIZE) {
1155 rb_raise(rb_eArgError,
"array size too big");
1159 ary_resize_capa(
ary,
len);
1164 rb_warn(
"block supersedes default value argument");
1166 for (i=0; i<
len; i++) {
1168 ARY_SET_LEN(
ary, i + 1);
1172 ary_memfill(
ary, 0,
len, val);
1189rb_ary_s_create(
int argc,
VALUE *argv,
VALUE klass)
1192 if (argc > 0 && argv) {
1193 ary_memcpy(
ary, 0, argc, argv);
1194 ARY_SET_LEN(
ary, argc);
1208 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
1212 else if (idx >= ARY_MAX_SIZE) {
1217 if (idx >= ARY_CAPA(
ary)) {
1218 ary_double_capa(
ary, idx);
1225 ARY_SET_LEN(
ary, idx + 1);
1227 ARY_SET(
ary, idx, val);
1237 VALUE result = ary_alloc_heap(klass);
1238 size_t embed_capa = ary_embed_capa(result);
1239 if ((
size_t)
len <= embed_capa) {
1240 FL_SET_EMBED(result);
1242 ARY_SET_EMBED_LEN(result,
len);
1245 VALUE shared = ary_make_shared(
ary);
1250 FL_UNSET_EMBED(result);
1254 rb_ary_set_shared(result, shared);
1256 ARY_INCREASE_PTR(result, offset);
1257 ARY_SET_LEN(result,
len);
1267ary_make_partial_step(
VALUE ary,
VALUE klass,
long offset,
long len,
long step)
1274 const long orig_len =
len;
1276 if (step > 0 && step >=
len) {
1277 VALUE result = ary_new(klass, 1);
1282 ARY_SET_EMBED_LEN(result, 1);
1285 else if (step < 0 && step < -
len) {
1289 long ustep = (step < 0) ? -step : step;
1290 len = roomof(
len, ustep);
1293 long j = offset + ((step > 0) ? 0 : (orig_len - 1));
1295 VALUE result = ary_new(klass,
len);
1296 if (ARY_EMBED_P(result)) {
1300 for (i = 0; i <
len; ++i) {
1304 ARY_SET_EMBED_LEN(result,
len);
1310 for (i = 0; i <
len; ++i) {
1315 ARY_SET_LEN(result,
len);
1327enum ary_take_pos_flags
1334ary_take_first_or_last_n(
VALUE ary,
long n,
enum ary_take_pos_flags last)
1343 rb_raise(rb_eArgError,
"negative array size");
1352ary_take_first_or_last(
int argc,
const VALUE *argv,
VALUE ary,
enum ary_take_pos_flags last)
1359 return ary_take_first_or_last_n(
ary,
NUM2LONG(argv[0]), last);
1381 VALUE target_ary = ary_ensure_room_for_push(
ary, 1);
1385 ARY_SET_LEN(
ary, idx + 1);
1394 VALUE target_ary = ary_ensure_room_for_push(
ary,
len);
1395 ary_memcpy0(
ary, oldlen,
len, argv, target_ary);
1396 ARY_SET_LEN(
ary, oldlen +
len);
1421 return rb_ary_cat(
ary, argv, argc);
1428 rb_ary_modify_check(
ary);
1430 if (n == 0)
return Qnil;
1431 if (ARY_OWNS_HEAP_P(
ary) &&
1432 n * 3 < ARY_CAPA(
ary) &&
1433 ARY_CAPA(
ary) > ARY_DEFAULT_SIZE)
1435 ary_resize_capa(
ary, n * 2);
1438 ARY_SET_LEN(
ary, n);
1479 return rb_ary_pop(
ary);
1482 rb_ary_modify_check(
ary);
1483 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
1496 rb_ary_modify_check(
ary);
1502 rb_ary_behead(
ary, 1);
1552 return rb_ary_shift(
ary);
1555 rb_ary_modify_check(
ary);
1556 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1558 rb_ary_behead(
ary,n);
1570 rb_ary_modify_check(
ary);
1572 if (!ARY_SHARED_P(
ary)) {
1577 ARY_INCREASE_LEN(
ary, -n);
1582 ary_mem_clear(
ary, 0, n);
1583 ary_make_shared(
ary);
1585 else if (ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(
ary))) {
1586 ary_mem_clear(
ary, 0, n);
1589 ARY_INCREASE_PTR(
ary, n);
1590 ARY_INCREASE_LEN(
ary, -n);
1599 if (head - sharedp < argc) {
1600 long room =
capa -
len - argc;
1604 head = sharedp + argc + room;
1606 ARY_SET_PTR(
ary, head - argc);
1610 return ARY_SHARED_ROOT(
ary);
1614ary_modify_for_unshift(
VALUE ary,
int argc)
1617 long new_len =
len + argc;
1619 const VALUE *head, *sharedp;
1623 if (
capa - (
capa >> 6) <= new_len) {
1624 ary_double_capa(
ary, new_len);
1628 if (new_len > ARY_DEFAULT_SIZE * 4 && !ARY_EMBED_P(
ary)) {
1633 ary_make_shared(
ary);
1636 return make_room_for_unshift(
ary, head, (
void *)sharedp, argc,
capa,
len);
1650ary_ensure_room_for_unshift(
VALUE ary,
int argc)
1653 long new_len =
len + argc;
1655 if (
len > ARY_MAX_SIZE - argc) {
1658 else if (! ARY_SHARED_P(
ary)) {
1659 return ary_modify_for_unshift(
ary, argc);
1666 return ary_modify_for_unshift(
ary, argc);
1668 else if (new_len >
capa) {
1669 return ary_modify_for_unshift(
ary, argc);
1675 rb_ary_modify_check(
ary);
1676 return make_room_for_unshift(
ary, head, sharedp, argc,
capa,
len);
1702 rb_ary_modify_check(
ary);
1706 target_ary = ary_ensure_room_for_unshift(
ary, argc);
1707 ary_memcpy0(
ary, 0, argc, argv, target_ary);
1708 ARY_SET_LEN(
ary,
len + argc);
1715 return rb_ary_unshift_m(1, &item,
ary);
1724 if (offset < 0 ||
len <= offset) {
1733 return rb_ary_entry_internal(
ary, offset);
1737rb_ary_subseq_step(
VALUE ary,
long beg,
long len,
long step)
1742 if (beg > alen)
return Qnil;
1743 if (beg < 0 ||
len < 0)
return Qnil;
1745 if (alen <
len || alen < beg +
len) {
1749 if (
len == 0)
return ary_new(klass, 0);
1751 rb_raise(rb_eArgError,
"slice step cannot be zero");
1753 return ary_make_partial(
ary, klass, beg,
len);
1755 return ary_make_partial_step(
ary, klass, beg,
len, step);
1761 return rb_ary_subseq_step(
ary, beg,
len, 1);
1893 return rb_ary_aref2(
ary, argv[0], argv[1]);
1895 return rb_ary_aref1(
ary, argv[0]);
1906 return rb_ary_subseq(
ary, beg,
len);
1912 long beg,
len, step;
1919 switch (rb_arithmetic_sequence_beg_len_step(arg, &beg, &
len, &step,
RARRAY_LEN(
ary), 0)) {
1925 return rb_ary_subseq_step(
ary, beg,
len, step);
1970 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1976ary_first(
VALUE self)
1992 return ary_last(
ary);
1995 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
2048 if (block_given && argc == 2) {
2049 rb_warn(
"block supersedes default value argument");
2057 if (block_given)
return rb_yield(pos);
2059 rb_raise(
rb_eIndexError,
"index %ld outside of array bounds: %ld...%ld",
2119 rb_warn(
"given block not used");
2177 rb_warn(
"given block not used");
2193 VALUE tmp = rb_check_array_type(obj);
2195 if (!
NIL_P(tmp))
return tmp;
2210 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2214 if (olen <
len || olen < beg +
len) {
2220 rofs = (rptr >= optr && rptr < optr + olen) ? rptr - optr : -1;
2225 if (beg > ARY_MAX_SIZE - rlen) {
2228 target_ary = ary_ensure_room_for_push(
ary, rlen-
len);
2230 ary_mem_clear(
ary, olen, beg - olen);
2233 ary_memcpy0(
ary, beg, rlen, rptr, target_ary);
2240 if (olen -
len > ARY_MAX_SIZE - rlen) {
2244 alen = olen + rlen -
len;
2245 if (alen >= ARY_CAPA(
ary)) {
2246 ary_double_capa(
ary, alen);
2253 ARY_SET_LEN(
ary, alen);
2257 rb_gc_writebarrier_remember(
ary);
2279 rb_ary_modify_check(
ary);
2280 if (ARY_SHARED_P(
ary)) {
2284 rb_bug(
"probable buffer overflow: %ld for %ld",
len,
capa);
2296 if (
len == olen)
return ary;
2297 if (
len > ARY_MAX_SIZE) {
2301 if (
len > ARY_CAPA(
ary)) {
2302 ary_double_capa(
ary,
len);
2304 ary_mem_clear(
ary, olen,
len - olen);
2307 else if (ARY_EMBED_P(
ary)) {
2308 ARY_SET_EMBED_LEN(
ary,
len);
2310 else if (
len <= ary_embed_capa(
ary)) {
2312 long ptr_capa = ARY_HEAP_SIZE(
ary);
2313 bool is_malloc_ptr = !ARY_SHARED_P(
ary);
2318 ARY_SET_EMBED_LEN(
ary,
len);
2320 if (is_malloc_ptr) ruby_sized_xfree((
void *)
ptr, ptr_capa);
2323 if (olen >
len + ARY_DEFAULT_SIZE) {
2324 size_t new_capa = ary_heap_realloc(
ary,
len);
2325 ARY_SET_CAPA(
ary, new_capa);
2327 ARY_SET_HEAP_LEN(
ary,
len);
2336 rb_ary_store(
ary, key, val);
2343 VALUE rpl = rb_ary_to_ary(val);
2496 long offset, beg,
len;
2499 rb_ary_modify_check(
ary);
2503 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[2]);
2507 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2511 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[1]);
2515 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2560 rb_ary_modify_check(
ary);
2562 if (argc == 1)
return ary;
2569 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2574 rb_ary_splice(
ary, pos, 0, argv + 1, argc - 1);
2584 return rb_ary_length(
ary);
2776 ARY_SET_LEN(dup,
len);
2789extern VALUE rb_output_fs;
2794recursive_join(
VALUE obj,
VALUE argp,
int recur)
2799 VALUE result = arg[2];
2800 int *first = (
int *)arg[3];
2803 rb_raise(rb_eArgError,
"recursive array join");
2806 ary_join_1(obj,
ary, sep, 0, result, first);
2818 for (i=0; i<max; i++) {
2821 if (i > 0 && !
NIL_P(sep))
2829ary_join_1_str(
VALUE dst,
VALUE src,
int *first)
2833 rb_enc_copy(dst, src);
2842 rb_raise(rb_eArgError,
"recursive array join");
2851 args[3] = (
VALUE)first;
2862 if (i > 0 && !
NIL_P(sep))
2867 ary_join_1_str(result, val, first);
2870 ary_join_1_ary(val,
ary, sep, result, val, first);
2873 ary_join_1_str(result, tmp, first);
2875 else if (!
NIL_P(tmp = rb_check_array_type(val))) {
2876 ary_join_1_ary(val,
ary, sep, result, tmp, first);
2888 VALUE val, tmp, result;
2900 if (
NIL_P(tmp) || tmp != val) {
2905 rb_enc_associate(result, rb_usascii_encoding());
2906 i = ary_join_0(
ary, sep, i, result);
2908 ary_join_1(
ary,
ary, sep, i, result, &first);
2912 len += RSTRING_LEN(tmp);
2964 return rb_ary_join(
ary, sep);
2978 else rb_enc_copy(str, s);
3009 return rb_ary_inspect(
ary);
3034 rb_ary_replace(dup,
ary);
3073 const VALUE e = rb_ary_elt(
ary, i);
3074 const VALUE elt = block_given ? rb_yield_force_blockarg(e) : e;
3075 const VALUE key_value_pair = rb_check_array_type(elt);
3076 if (
NIL_P(key_value_pair)) {
3077 rb_raise(
rb_eTypeError,
"wrong element type %"PRIsVALUE
" at %ld (expected array)",
3081 rb_raise(rb_eArgError,
"wrong array length at %ld (expected 2, was %ld)",
3122 ary_reverse(p1, p2);
3145 return rb_ary_reverse(
ary);
3168 do *p2-- = *p1++;
while (--
len > 0);
3175rotate_count(
long cnt,
long len)
3177 return (cnt < 0) ? (
len - (~cnt %
len) - 1) : (cnt %
len);
3188 else if (cnt ==
len - 1) {
3196 if (--cnt > 0) ary_reverse(
ptr,
ptr + cnt);
3208 if (
len > 1 && (cnt = rotate_count(cnt,
len)) > 0) {
3252 rb_ary_rotate(
ary, n);
3300 cnt = rotate_count(cnt,
len);
3303 ary_memcpy(rotated, 0,
len,
ptr + cnt);
3304 ary_memcpy(rotated,
len, cnt,
ptr);
3316sort_reentered(
VALUE ary)
3318 if (
RBASIC(ary)->klass) {
3330 sort_reentered(data->ary);
3334sort_1(
const void *ap,
const void *bp,
void *dummy)
3337 VALUE retval = sort_reentered(data->ary);
3345 n = rb_cmpint(retval, a, b);
3346 sort_returned(data);
3351sort_2(
const void *ap,
const void *bp,
void *dummy)
3354 VALUE retval = sort_reentered(data->ary);
3359 if ((
long)a > (long)b)
return 1;
3360 if ((
long)a < (long)b)
return -1;
3363 if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(STRING)) {
3367 return rb_float_cmp(a, b);
3370 retval = rb_funcallv(a, id_cmp, 1, &b);
3371 n = rb_cmpint(retval, a, b);
3372 sort_returned(data);
3393 VALUE tmp = ary_make_substitution(ary);
3396 RBASIC_CLEAR_CLASS(tmp);
3398 data.receiver = ary;
3404 if (ARY_EMBED_P(tmp)) {
3405 if (ARY_SHARED_P(ary)) {
3406 rb_ary_unshare(ary);
3409 if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) {
3410 ary_resize_capa(ary, ARY_EMBED_LEN(tmp));
3412 ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp));
3413 ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
3416 if (!ARY_EMBED_P(ary) && ARY_HEAP_PTR(ary) == ARY_HEAP_PTR(tmp)) {
3417 FL_UNSET_SHARED(ary);
3422 if (ARY_EMBED_P(ary)) {
3423 FL_UNSET_EMBED(ary);
3425 else if (ARY_SHARED_P(ary)) {
3427 rb_ary_unshare(ary);
3432 ARY_SET_PTR(ary, ARY_HEAP_PTR(tmp));
3433 ARY_SET_HEAP_LEN(ary,
len);
3434 ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
3439 ARY_SET_EMBED_LEN(tmp, 0);
3483 ary = rb_ary_dup(ary);
3484 rb_ary_sort_bang(ary);
3504rb_ary_bsearch(
VALUE ary)
3506 VALUE index_result = rb_ary_bsearch_index(ary);
3509 return rb_ary_entry(ary,
FIX2LONG(index_result));
3511 return index_result;
3528rb_ary_bsearch_index(
VALUE ary)
3531 int smaller = 0, satisfied = 0;
3535 while (low < high) {
3536 mid = low + ((high - low) / 2);
3537 val = rb_ary_entry(ary, mid);
3543 else if (v ==
Qtrue) {
3547 else if (!
RTEST(v)) {
3552 switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
3554 case 1: smaller = 0;
break;
3555 case -1: smaller = 1;
3560 " (must be numeric, true, false or nil)",
3570 if (!satisfied)
return Qnil;
3604rb_ary_sort_by_bang(
VALUE ary)
3611 sorted =
rb_block_call(ary, rb_intern(
"sort_by"), 0, 0, sort_by_i, 0);
3612 rb_ary_replace(ary, sorted);
3639rb_ary_collect(
VALUE ary)
3674rb_ary_collect_bang(
VALUE ary)
3687rb_get_values_at(
VALUE obj,
long olen,
int argc,
const VALUE *argv,
VALUE (*func) (
VALUE,
long))
3690 long beg,
len, i, j;
3692 for (i=0; i<argc; i++) {
3694 rb_ary_push(result, (*func)(obj,
FIX2LONG(argv[i])));
3699 long end = olen < beg+
len ? olen : beg+
len;
3700 for (j = beg; j < end; j++) {
3701 rb_ary_push(result, (*func)(obj, j));
3704 rb_ary_resize(result,
RARRAY_LEN(result) + (beg +
len) - j);
3707 rb_ary_push(result, (*func)(obj,
NUM2LONG(argv[i])));
3723 const long end = beg +
len;
3726 rb_ary_cat(result, src + beg, end > olen ? olen-beg :
len);
3729 rb_ary_store(result, prevlen +
len - 1,
Qnil);
3737 return rb_ary_push(result, rb_ary_entry(ary, beg));
3847rb_ary_values_at(
int argc,
VALUE *argv,
VALUE ary)
3850 VALUE result = rb_ary_new_capa(argc);
3851 for (i = 0; i < argc; ++i) {
3852 append_values_at_single(result, ary, olen, argv[i]);
3880rb_ary_select(
VALUE ary)
3889 rb_ary_push(result, rb_ary_elt(ary, i));
3901select_bang_i(
VALUE a)
3904 VALUE ary = arg->ary;
3907 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
3911 rb_ary_store(ary, i2, v);
3915 return (i1 == i2) ?
Qnil : ary;
3919select_bang_ensure(
VALUE a)
3922 VALUE ary = arg->ary;
3924 long i1 = arg->len[0], i2 = arg->len[1];
3926 if (i2 <
len && i2 < i1) {
3935 ARY_SET_LEN(ary, i2 + tail);
3963rb_ary_select_bang(
VALUE ary)
3971 args.len[0] = args.len[1] = 0;
3992rb_ary_keep_if(
VALUE ary)
3995 rb_ary_select_bang(ary);
4000ary_resize_smaller(
VALUE ary,
long len)
4004 ARY_SET_LEN(ary,
len);
4005 if (
len * 2 < ARY_CAPA(ary) &&
4006 ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
4007 ary_resize_capa(ary,
len * 2);
4055 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4063 rb_ary_store(ary, i2, e);
4074 ary_resize_smaller(ary, i2);
4085 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4092 rb_ary_store(ary, i2, e);
4100 ary_resize_smaller(ary, i2);
4112 if (pos < 0)
return Qnil;
4120 ARY_INCREASE_LEN(ary, -1);
4156 return rb_ary_delete_at(ary,
NUM2LONG(pos));
4160ary_slice_bang_by_rb_ary_splice(
VALUE ary,
long pos,
long len)
4167 else if (pos < -orig_len) {
4173 else if (orig_len < pos) {
4176 if (orig_len < pos +
len) {
4177 len = orig_len - pos;
4184 rb_ary_splice(ary, pos,
len, 0, 0);
4282rb_ary_slice_bang(
int argc,
VALUE *argv,
VALUE ary)
4287 rb_ary_modify_check(ary);
4294 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4301 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4311 return rb_ary_delete_at(ary,
NUM2LONG(arg1));
4323 rb_ary_push(result, v);
4330reject_bang_i(
VALUE a)
4333 VALUE ary = arg->ary;
4336 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4340 rb_ary_store(ary, i2, v);
4344 return (i1 == i2) ?
Qnil : ary;
4348ary_reject_bang(
VALUE ary)
4351 rb_ary_modify_check(ary);
4353 args.len[0] = args.len[1] = 0;
4378rb_ary_reject_bang(
VALUE ary)
4382 return ary_reject_bang(ary);
4403rb_ary_reject(
VALUE ary)
4408 rejected_ary = rb_ary_new();
4409 ary_reject(ary, rejected_ary);
4410 return rejected_ary;
4431rb_ary_delete_if(
VALUE ary)
4435 ary_reject_bang(ary);
4444 rb_ary_push(args[0], val);
4450take_items(
VALUE obj,
long n)
4452 VALUE result = rb_check_array_type(obj);
4455 if (n == 0)
return result;
4456 if (!
NIL_P(result))
return rb_ary_subseq(result, 0, n);
4458 args[0] = result; args[1] = (
VALUE)n;
4459 if (UNDEF_P(rb_check_block_call(obj, idEach, 0, 0, take_i, (
VALUE)args)))
4460 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
4566 for (i=0; i<argc; i++) {
4567 argv[i] = take_items(argv[i],
len);
4571 int arity = rb_block_arity();
4580 for (j=0; j<argc; j++) {
4581 tmp[j+1] = rb_ary_elt(argv[j], i);
4593 for (j=0; j<argc; j++) {
4594 rb_ary_push(tmp, rb_ary_elt(argv[j], i));
4601 result = rb_ary_new_capa(
len);
4603 for (i=0; i<
len; i++) {
4604 VALUE tmp = rb_ary_new_capa(argc+1);
4607 for (j=0; j<argc; j++) {
4608 rb_ary_push(tmp, rb_ary_elt(argv[j], i));
4610 rb_ary_push(result, tmp);
4633rb_ary_transpose(
VALUE ary)
4635 long elen = -1, alen, i, j;
4636 VALUE tmp, result = 0;
4639 if (alen == 0)
return rb_ary_dup(ary);
4640 for (i=0; i<alen; i++) {
4641 tmp = to_ary(rb_ary_elt(ary, i));
4645 for (j=0; j<elen; j++) {
4650 rb_raise(
rb_eIndexError,
"element size differs (%ld should be %ld)",
4653 for (j=0; j<elen; j++) {
4654 rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
4678 rb_ary_modify_check(copy);
4679 orig = to_ary(orig);
4680 if (copy == orig)
return copy;
4685 if (
RARRAY_LEN(orig) <= ary_embed_capa(copy)) {
4692 else if (ARY_EMBED_P(orig)) {
4693 long len = ARY_EMBED_LEN(orig);
4694 VALUE *ptr = ary_heap_alloc_buffer(
len);
4696 FL_UNSET_EMBED(copy);
4697 ARY_SET_PTR(copy, ptr);
4698 ARY_SET_LEN(copy,
len);
4699 ARY_SET_CAPA(copy,
len);
4708 VALUE shared_root = ary_make_shared(orig);
4709 FL_UNSET_EMBED(copy);
4710 ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
4711 ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
4712 rb_ary_set_shared(copy, shared_root);
4733 rb_ary_modify_check(ary);
4734 if (ARY_SHARED_P(ary)) {
4735 rb_ary_unshare(ary);
4737 ARY_SET_EMBED_LEN(ary, 0);
4740 ARY_SET_LEN(ary, 0);
4741 if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
4742 ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
4933 long beg = 0, end = 0,
len = 0;
4956 if (beg < 0) beg = 0;
4965 if (beg >= ARY_MAX_SIZE ||
len > ARY_MAX_SIZE - beg) {
4966 rb_raise(rb_eArgError,
"argument too big");
4970 if (end >= ARY_CAPA(ary)) {
4971 ary_resize_capa(ary, end);
4974 ARY_SET_LEN(ary, end);
4977 if (UNDEF_P(item)) {
4981 for (i=beg; i<end; i++) {
4988 ary_memfill(ary, beg,
len, item);
5010 long len, xlen, ylen;
5020 ARY_SET_LEN(z,
len);
5049rb_ary_concat_multi(
int argc,
VALUE *argv,
VALUE ary)
5051 rb_ary_modify_check(ary);
5054 rb_ary_concat(ary, argv[0]);
5056 else if (argc > 1) {
5058 VALUE args = rb_ary_hidden_new(argc);
5059 for (i = 0; i < argc; i++) {
5060 rb_ary_concat(args, argv[i]);
5062 ary_append(ary, args);
5072 return ary_append(x, to_ary(y));
5102 return rb_ary_join(ary, tmp);
5111 rb_raise(rb_eArgError,
"negative argument");
5114 rb_raise(rb_eArgError,
"argument too big");
5119 ARY_SET_LEN(ary2,
len);
5124 ary_memcpy(ary2, 0, t, ptr);
5125 while (t <=
len/2) {
5202recursive_equal(
VALUE ary1,
VALUE ary2,
int recur)
5205 const VALUE *p1, *p2;
5207 if (recur)
return Qtrue;
5214 for (i = 0; i < len1; i++) {
5262 if (ary1 == ary2)
return Qtrue;
5275recursive_eql(
VALUE ary1,
VALUE ary2,
int recur)
5279 if (recur)
return Qtrue;
5281 if (!
rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
5309 if (ary1 == ary2)
return Qtrue;
5317rb_ary_hash_values(
long len,
const VALUE *elements)
5325 for (i=0; i<
len; i++) {
5326 n = rb_hash(elements[i]);
5349rb_ary_hash(
VALUE ary)
5399recursive_cmp(
VALUE ary1,
VALUE ary2,
int recur)
5403 if (recur)
return Qundef;
5408 for (i=0; i<
len; i++) {
5409 VALUE e1 = rb_ary_elt(ary1, i), e2 = rb_ary_elt(ary2, i);
5410 VALUE v = rb_funcallv(e1, id_cmp, 1, &e2);
5462 ary2 = rb_check_array_type(ary2);
5464 if (ary1 == ary2)
return INT2FIX(0);
5466 if (!UNDEF_P(v))
return v;
5480 rb_hash_add_new_element(hash, elt, elt);
5486ary_tmp_hash_new(
VALUE ary)
5489 VALUE hash = rb_hash_new_with_size(size);
5491 RBASIC_CLEAR_CLASS(hash);
5496ary_make_hash(
VALUE ary)
5498 VALUE hash = ary_tmp_hash_new(ary);
5499 return ary_add_hash(hash, ary);
5509 rb_hash_add_new_element(hash, k, v);
5515ary_make_hash_by(
VALUE ary)
5517 VALUE hash = ary_tmp_hash_new(ary);
5518 return ary_add_hash_by(hash, ary);
5546 ary2 = to_ary(ary2);
5547 if (
RARRAY_LEN(ary2) == 0) {
return ary_make_shared_copy(ary1); }
5548 ary3 = rb_ary_new();
5552 VALUE elt = rb_ary_elt(ary1, i);
5553 if (rb_ary_includes_by_eql(ary2, elt))
continue;
5554 rb_ary_push(ary3, elt);
5559 hash = ary_make_hash(ary2);
5561 if (rb_hash_stlike_lookup(hash,
RARRAY_AREF(ary1, i), NULL))
continue;
5562 rb_ary_push(ary3, rb_ary_elt(ary1, i));
5588rb_ary_difference_multi(
int argc,
VALUE *argv,
VALUE ary)
5593 bool *is_hash =
ALLOCV_N(
bool, t0, argc);
5594 ary_diff = rb_ary_new();
5597 for (i = 0; i < argc; i++) {
5598 argv[i] = to_ary(argv[i]);
5599 is_hash[i] = (length > SMALL_ARRAY_LEN &&
RARRAY_LEN(argv[i]) > SMALL_ARRAY_LEN);
5600 if (is_hash[i]) argv[i] = ary_make_hash(argv[i]);
5605 VALUE elt = rb_ary_elt(ary, i);
5606 for (j = 0; j < argc; j++) {
5608 if (rb_hash_stlike_lookup(argv[j],
RARRAY_AREF(ary, i), NULL))
5612 if (rb_ary_includes_by_eql(argv[j], elt))
break;
5615 if (j == argc) rb_ary_push(ary_diff, elt);
5651 VALUE hash, ary3, v;
5655 ary2 = to_ary(ary2);
5656 ary3 = rb_ary_new();
5662 if (!rb_ary_includes_by_eql(ary2, v))
continue;
5663 if (rb_ary_includes_by_eql(ary3, v))
continue;
5664 rb_ary_push(ary3, v);
5669 hash = ary_make_hash(ary2);
5674 if (rb_hash_stlike_delete(hash, &vv, 0)) {
5675 rb_ary_push(ary3, v);
5704rb_ary_intersection_multi(
int argc,
VALUE *argv,
VALUE ary)
5706 VALUE result = rb_ary_dup(ary);
5709 for (i = 0; i < argc; i++) {
5710 result = rb_ary_and(result, argv[i]);
5717ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
5719 if (existing)
return ST_STOP;
5720 *key = *value = (
VALUE)arg;
5729 VALUE elt = rb_ary_elt(ary, i);
5730 if (rb_ary_includes_by_eql(ary_union, elt))
continue;
5731 rb_ary_push(ary_union, elt);
5741 if (!rb_hash_stlike_update(hash, (st_data_t)elt, ary_hash_orset, (st_data_t)elt)) {
5767 ary2 = to_ary(ary2);
5769 VALUE ary3 = rb_ary_new();
5770 rb_ary_union(ary3, ary1);
5771 rb_ary_union(ary3, ary2);
5775 hash = ary_make_hash(ary1);
5776 rb_ary_union_hash(hash, ary2);
5778 return rb_hash_values(hash);
5805rb_ary_union_multi(
int argc,
VALUE *argv,
VALUE ary)
5812 for (i = 0; i < argc; i++) {
5813 argv[i] = to_ary(argv[i]);
5817 if (sum <= SMALL_ARRAY_LEN) {
5818 VALUE ary_union = rb_ary_new();
5820 rb_ary_union(ary_union, ary);
5821 for (i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]);
5826 hash = ary_make_hash(ary);
5827 for (i = 0; i < argc; i++) rb_ary_union_hash(hash, argv[i]);
5829 return rb_hash_values(hash);
5849 VALUE hash, v, result, shorter, longer;
5853 ary2 = to_ary(ary2);
5859 if (rb_ary_includes_by_eql(ary2, v))
return Qtrue;
5871 hash = ary_make_hash(shorter);
5877 if (rb_hash_stlike_lookup(hash, vv, 0)) {
5887ary_max_generic(
VALUE ary,
long i,
VALUE vmax)
5895 if (rb_cmpint(rb_funcallv(vmax, id_cmp, 1, &v), vmax, v) < 0) {
5904ary_max_opt_fixnum(
VALUE ary,
long i,
VALUE vmax)
5911 for (; i < n; ++i) {
5915 if ((
long)vmax < (
long)v) {
5920 return ary_max_generic(ary, i, vmax);
5928ary_max_opt_float(
VALUE ary,
long i,
VALUE vmax)
5935 for (; i < n; ++i) {
5939 if (rb_float_cmp(vmax, v) < 0) {
5944 return ary_max_generic(ary, i, vmax);
5952ary_max_opt_string(
VALUE ary,
long i,
VALUE vmax)
5959 for (; i < n; ++i) {
5968 return ary_max_generic(ary, i, vmax);
6031 return rb_nmin_run(ary, num, 0, 1, 1);
6037 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) > 0) {
6045 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6046 return ary_max_opt_fixnum(ary, 1, result);
6048 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6049 return ary_max_opt_string(ary, 1, result);
6052 return ary_max_opt_float(ary, 1, result);
6055 return ary_max_generic(ary, 1, result);
6059 if (UNDEF_P(result))
return Qnil;
6064ary_min_generic(
VALUE ary,
long i,
VALUE vmin)
6072 if (rb_cmpint(rb_funcallv(vmin, id_cmp, 1, &v), vmin, v) > 0) {
6081ary_min_opt_fixnum(
VALUE ary,
long i,
VALUE vmin)
6088 for (; i < n; ++i) {
6092 if ((
long)vmin > (
long)a) {
6097 return ary_min_generic(ary, i, vmin);
6105ary_min_opt_float(
VALUE ary,
long i,
VALUE vmin)
6112 for (; i < n; ++i) {
6116 if (rb_float_cmp(vmin, a) > 0) {
6121 return ary_min_generic(ary, i, vmin);
6129ary_min_opt_string(
VALUE ary,
long i,
VALUE vmin)
6136 for (; i < n; ++i) {
6145 return ary_min_generic(ary, i, vmin);
6208 return rb_nmin_run(ary, num, 0, 0, 1);
6214 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) < 0) {
6222 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6223 return ary_min_opt_fixnum(ary, 1, result);
6225 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6226 return ary_min_opt_string(ary, 1, result);
6229 return ary_min_opt_float(ary, 1, result);
6232 return ary_min_generic(ary, 1, result);
6236 if (UNDEF_P(result))
return Qnil;
6263rb_ary_minmax(
VALUE ary)
6268 return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary));
6272push_value(st_data_t key, st_data_t val, st_data_t ary)
6306rb_ary_uniq_bang(
VALUE ary)
6311 rb_ary_modify_check(ary);
6315 hash = ary_make_hash_by(ary);
6317 hash = ary_make_hash(ary);
6323 rb_ary_modify_check(ary);
6324 ARY_SET_LEN(ary, 0);
6325 if (ARY_SHARED_P(ary)) {
6326 rb_ary_unshare(ary);
6329 ary_resize_capa(ary, hash_size);
6362rb_ary_uniq(
VALUE ary)
6368 uniq = rb_ary_dup(ary);
6371 hash = ary_make_hash_by(ary);
6372 uniq = rb_hash_values(hash);
6375 hash = ary_make_hash(ary);
6376 uniq = rb_hash_values(hash);
6399rb_ary_compact_bang(
VALUE ary)
6416 ary_resize_smaller(ary, n);
6436rb_ary_compact(
VALUE ary)
6438 ary = rb_ary_dup(ary);
6439 rb_ary_compact_bang(ary);
6471rb_ary_count(
int argc,
VALUE *argv,
VALUE ary)
6487 VALUE obj = argv[0];
6490 rb_warn(
"given block not used");
6501flatten(
VALUE ary,
int level)
6504 VALUE stack, result, tmp = 0, elt;
6509 tmp = rb_check_array_type(elt);
6520 ARY_SET_LEN(result, i);
6522 stack = ary_new(0, ARY_DEFAULT_SIZE);
6523 rb_ary_push(stack, ary);
6524 rb_ary_push(stack,
LONG2NUM(i + 1));
6528 rb_hash_aset(memo, ary,
Qtrue);
6529 rb_hash_aset(memo, tmp,
Qtrue);
6538 if (level >= 0 &&
RARRAY_LEN(stack) / 2 >= level) {
6539 rb_ary_push(result, elt);
6542 tmp = rb_check_array_type(elt);
6543 if (
RBASIC(result)->klass) {
6545 rb_hash_clear(memo);
6550 rb_ary_push(result, elt);
6554 if (rb_hash_aref(memo, tmp) ==
Qtrue) {
6555 rb_hash_clear(memo);
6556 rb_raise(rb_eArgError,
"tried to flatten recursive array");
6558 rb_hash_aset(memo, tmp,
Qtrue);
6560 rb_ary_push(stack, ary);
6570 rb_hash_delete(memo, ary);
6572 tmp = rb_ary_pop(stack);
6574 ary = rb_ary_pop(stack);
6578 rb_hash_clear(memo);
6621rb_ary_flatten_bang(
int argc,
VALUE *argv,
VALUE ary)
6623 int mod = 0, level = -1;
6627 rb_ary_modify_check(ary);
6629 if (level == 0)
return Qnil;
6631 result = flatten(ary, level);
6632 if (result == ary) {
6635 if (!(mod = ARY_EMBED_P(result))) rb_ary_freeze(result);
6636 rb_ary_replace(ary, result);
6637 if (mod) ARY_SET_EMBED_LEN(result, 0);
6678rb_ary_flatten(
int argc,
VALUE *argv,
VALUE ary)
6685 if (level == 0)
return ary_make_shared_copy(ary);
6688 result = flatten(ary, level);
6689 if (result == ary) {
6690 result = ary_make_shared_copy(ary);
6696#define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1)
6707 long j = RAND_UPTO(i);
6723 ary = rb_ary_dup(ary);
6724 rb_ary_shuffle_bang(ec, ary, randgen);
6733 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
6740 long n,
len, i, j, k, idx[10];
6741 long rnds[numberof(idx)];
6742 long memo_threshold;
6751 return rb_ary_elt(ary, i);
6754 if (n < 0) rb_raise(rb_eArgError,
"negative sample number");
6756 if (n <= numberof(idx)) {
6757 for (i = 0; i < n; ++i) {
6758 rnds[i] = RAND_UPTO(
len - i);
6763 if (
len < k && n <= numberof(idx)) {
6764 for (i = 0; i < n; ++i) {
6765 if (rnds[i] >=
len)
return rb_ary_new_capa(0);
6771 return rb_ary_new_capa(0);
6774 return rb_ary_new_from_args(1,
RARRAY_AREF(ary, i));
6786 if (j >= i) l = i, g = ++j;
6787 if (k >= l && (++k >= g)) ++k;
6796 if (n <= numberof(idx)) {
6797 long sorted[numberof(idx)];
6798 sorted[0] = idx[0] = rnds[0];
6799 for (i=1; i<n; i++) {
6801 for (j = 0; j < i; ++j) {
6802 if (k < sorted[j])
break;
6805 memmove(&sorted[j+1], &sorted[j],
sizeof(sorted[0])*(i-j));
6806 sorted[j] = idx[i] = k;
6808 result = rb_ary_new_capa(n);
6810 for (i=0; i<n; i++) {
6815 else if (n <= memo_threshold / 2) {
6818 st_table *memo = st_init_numtable_with_size(n);
6820 result = rb_ary_new_capa(n);
6822 for (i=0; i<n; i++) {
6823 long r = RAND_UPTO(
len-i) + i;
6825 if (r > max_idx) max_idx = r;
6828 if (
len <= max_idx) n = 0;
6829 else if (n >
len) n =
len;
6831 for (i=0; i<n; i++) {
6832 long j2 = j = ptr_result[i];
6835 if (st_lookup(memo, (st_data_t)i, &value)) i2 = (
long)value;
6836 if (st_lookup(memo, (st_data_t)j, &value)) j2 = (
long)value;
6837 st_insert(memo, (st_data_t)j, (st_data_t)i2);
6838 ptr_result[i] = ptr_ary[j2];
6843 st_free_table(memo);
6847 result = rb_ary_dup(ary);
6848 RBASIC_CLEAR_CLASS(result);
6851 for (i=0; i<n; i++) {
6852 j = RAND_UPTO(
len-i) + i;
6854 ptr_result[j] = ptr_result[i];
6858 RBASIC_SET_CLASS_RAW(result,
rb_cArray);
6860 ARY_SET_LEN(result, n);
6888 if (mul <= 0)
return INT2FIX(0);
6890 return rb_fix_mul_fix(rb_ary_length(self), n);
6927rb_ary_cycle(
int argc,
VALUE *argv,
VALUE ary)
6934 if (argc == 0 ||
NIL_P(argv[0])) {
6939 if (n <= 0)
return Qnil;
6942 while (
RARRAY_LEN(ary) > 0 && (n < 0 || 0 < n--)) {
6956yield_indexed_values(
const VALUE values,
const long r,
const long *
const p)
6961 for (i = 0; i < r; i++) ARY_SET(result, i,
RARRAY_AREF(values, p[i]));
6962 ARY_SET_LEN(result, r);
6964 return !
RBASIC(values)->klass;
6980permute0(
const long n,
const long r,
long *
const p,
char *
const used,
const VALUE values)
6982 long i = 0, index = 0;
6985 const char *
const unused = memchr(&used[i], 0, n-i);
7000 for (i = 0; i < n; ++i) {
7001 if (used[i])
continue;
7003 if (!yield_indexed_values(values, r, p)) {
7019descending_factorial(
long from,
long how_many)
7024 while (--how_many > 0) {
7026 cnt = rb_int_mul(cnt,
LONG2FIX(v));
7036binomial_coefficient(
long comb,
long size)
7040 if (comb > size-comb) {
7046 else if (comb == 0) {
7050 for (i = 1; i < comb; ++i) {
7051 r = rb_int_mul(r,
LONG2FIX(size - i));
7052 r = rb_int_idiv(r,
LONG2FIX(i + 1));
7063 return descending_factorial(n, k);
7109rb_ary_permutation(
int argc,
VALUE *argv,
VALUE ary)
7119 if (r < 0 || n < r) {
7132 long *p =
ALLOCV_N(
long, t0, r+roomof(n,
sizeof(
long)));
7133 char *used = (
char*)(p + r);
7134 VALUE ary0 = ary_make_shared_copy(ary);
7135 RBASIC_CLEAR_CLASS(ary0);
7139 permute0(n, r, p, used, ary0);
7147combinate0(
const long len,
const long n,
long *
const stack,
const VALUE values)
7154 for (lev++; lev < n; lev++) {
7155 stack[lev+1] = stack[lev]+1;
7157 if (!yield_indexed_values(values, n, stack+1)) {
7161 if (lev == 0)
return;
7163 }
while (stack[lev+1]+n ==
len+lev+1);
7173 return binomial_coefficient(k, n);
7228 if (n < 0 ||
len < n) {
7240 VALUE ary0 = ary_make_shared_copy(ary);
7242 long *stack =
ALLOCV_N(
long, t0, n+1);
7244 RBASIC_CLEAR_CLASS(ary0);
7245 combinate0(
len, n, stack, ary0);
7265rpermute0(
const long n,
const long r,
long *
const p,
const VALUE values)
7267 long i = 0, index = 0;
7271 if (++index < r-1) {
7275 for (i = 0; i < n; ++i) {
7277 if (!yield_indexed_values(values, r, p)) {
7282 if (index <= 0)
return;
7283 }
while ((i = ++p[--index]) >= n);
7341rb_ary_repeated_permutation(
VALUE ary,
VALUE num)
7363 VALUE ary0 = ary_make_shared_copy(ary);
7364 RBASIC_CLEAR_CLASS(ary0);
7366 rpermute0(n, r, p, ary0);
7374rcombinate0(
const long n,
const long r,
long *
const p,
const long rest,
const VALUE values)
7376 long i = 0, index = 0;
7380 if (++index < r-1) {
7384 for (; i < n; ++i) {
7386 if (!yield_indexed_values(values, r, p)) {
7391 if (index <= 0)
return;
7392 }
while ((i = ++p[--index]) >= n);
7404 return binomial_coefficient(k, n + k - 1);
7447rb_ary_repeated_combination(
VALUE ary,
VALUE num)
7465 else if (
len == 0) {
7471 VALUE ary0 = ary_make_shared_copy(ary);
7472 RBASIC_CLEAR_CLASS(ary0);
7474 rcombinate0(
len, n, p, n, ary0);
7535rb_ary_product(
int argc,
VALUE *argv,
VALUE ary)
7538 volatile VALUE t0 = rb_ary_hidden_new(n);
7541 int *counters =
ALLOCV_N(
int, t1, n);
7546 RBASIC_CLEAR_CLASS(t0);
7551 for (i = 1; i < n; i++) arrays[i] =
Qnil;
7552 for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
7555 for (i = 0; i < n; i++) counters[i] = 0;
7560 for (i = 0; i < n; i++) {
7562 arrays[i] = ary_make_shared_copy(arrays[i]);
7567 for (i = 0; i < n; i++) {
7573 if (MUL_OVERFLOW_LONG_P(resultlen, k))
7583 for (j = 0; j < n; j++) {
7584 rb_ary_push(subarray, rb_ary_entry(arrays[j], counters[j]));
7588 if (
NIL_P(result)) {
7589 FL_SET(t0, RARRAY_SHARED_ROOT_FLAG);
7591 if (!
FL_TEST(t0, RARRAY_SHARED_ROOT_FLAG)) {
7595 FL_UNSET(t0, RARRAY_SHARED_ROOT_FLAG);
7599 rb_ary_push(result, subarray);
7608 while (counters[m] ==
RARRAY_LEN(arrays[m])) {
7611 if (--m < 0)
goto done;
7619 return NIL_P(result) ? ary : result;
7645 rb_raise(rb_eArgError,
"attempt to take negative size");
7647 return rb_ary_subseq(obj, 0,
len);
7672rb_ary_take_while(
VALUE ary)
7680 return rb_ary_take(ary,
LONG2FIX(i));
7708 rb_raise(rb_eArgError,
"attempt to drop negative size");
7711 result = rb_ary_subseq(ary, pos,
RARRAY_LEN(ary));
7712 if (
NIL_P(result)) result = rb_ary_new();
7735rb_ary_drop_while(
VALUE ary)
7743 return rb_ary_drop(ary,
LONG2FIX(i));
7786rb_ary_any_p(
int argc,
VALUE *argv,
VALUE ary)
7794 rb_warn(
"given block not used");
7801 for (i = 0; i <
len; ++i) {
7853rb_ary_all_p(
int argc,
VALUE *argv,
VALUE ary)
7861 rb_warn(
"given block not used");
7868 for (i = 0; i <
len; ++i) {
7914rb_ary_none_p(
int argc,
VALUE *argv,
VALUE ary)
7922 rb_warn(
"given block not used");
7929 for (i = 0; i <
len; ++i) {
7978rb_ary_one_p(
int argc,
VALUE *argv,
VALUE ary)
7987 rb_warn(
"given block not used");
7991 if (result)
return Qfalse;
7997 for (i = 0; i <
len; ++i) {
7999 if (result)
return Qfalse;
8007 if (result)
return Qfalse;
8039 self = rb_ary_at(self, *argv);
8040 if (!--argc)
return self;
8042 return rb_obj_dig(argc, argv, self,
Qnil);
8046finish_exact_sum(
long n,
VALUE r,
VALUE v,
int z)
8051 v = rb_rational_plus(r, v);
8119 goto init_is_a_value;
8133 else if (RB_BIGNUM_TYPE_P(e))
8134 v = rb_big_plus(e, v);
8139 r = rb_rational_plus(r, e);
8144 v = finish_exact_sum(n, r, v, argc!=0);
8148 v = finish_exact_sum(n, r, v, i!=0);
8160 goto has_float_value;
8170 else if (RB_BIGNUM_TYPE_P(e))
8177 if (isnan(f))
continue;
8183 if (isinf(f) && signbit(x) != signbit(f))
8189 if (isinf(f))
continue;
8192 if (fabs(f) >= fabs(x))
8205 goto has_some_value;
8219rb_ary_deconstruct(
VALUE ary)
8730 fake_ary_flags = init_fake_ary_flags();
8856 rb_cArray_empty_frozen = rb_ary_freeze(rb_ary_new());
8857 rb_vm_register_global_object(rb_cArray_empty_frozen);
8860#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_method(klass, mid, func, arity)
Defines klass#mid.
#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.
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 rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#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_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_eIndexError
IndexError exception.
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_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
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.
#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.
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.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
#define rb_usascii_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "US ASCII" encoding.
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_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.
int capa
Designed capacity of the buffer.
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_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.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
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(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.
static VALUE * RARRAY_PTR(VALUE ary)
Wild use of a C pointer.
#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.
#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.
union RArray::@47 as
Array's specific fields.
struct RBasic basic
Basic part, including flags and class.
struct RArray::@47::@48 heap
Arrays that use separated memory region for elements use this pattern.
const VALUE shared_root
Parent of the array.
const VALUE ary[1]
Embedded elements.
long capa
Capacity of *ptr.
union RArray::@47::@48::@49 aux
Auxiliary info.
long len
Number of elements of the array.
const VALUE * ptr
Pointer to the C array that holds the elements of the array.
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.