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/imemo.h"
23#include "internal/numeric.h"
24#include "internal/object.h"
25#include "internal/proc.h"
26#include "internal/rational.h"
27#include "internal/set.h"
28#include "internal/string.h"
29#include "internal/vm.h"
45#include "ruby_assert.h"
48VALUE rb_cArray_empty_frozen;
78#define ARY_DEFAULT_SIZE 16
79#define ARY_MAX_SIZE (RB_LEN_MAX / (int)sizeof(VALUE))
80#define SMALL_ARRAY_LEN 16
84should_be_T_ARRAY(
VALUE ary)
89#define ARY_HEAP_PTR(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
90#define ARY_HEAP_LEN(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
91#define ARY_HEAP_CAPA(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(!ARY_SHARED_ROOT_P(a)), \
92 RARRAY(a)->as.heap.aux.capa)
94#define ARY_EMBED_PTR(a) (RUBY_ASSERT(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
95#define ARY_EMBED_LEN(a) \
96 (RUBY_ASSERT(ARY_EMBED_P(a)), \
97 (rb_len_t)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
98 (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
99#define ARY_HEAP_SIZE(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE))
101#define ARY_OWNS_HEAP_P(a) (RUBY_ASSERT(should_be_T_ARRAY((VALUE)(a))), \
102 !FL_TEST_RAW((a), RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG))
104#define FL_SET_EMBED(a) do { \
105 RUBY_ASSERT(!ARY_SHARED_P(a)); \
106 FL_SET((a), RARRAY_EMBED_FLAG); \
110#define FL_UNSET_EMBED(ary) FL_UNSET((ary), RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK)
111#define FL_SET_SHARED(ary) do { \
112 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
113 FL_SET((ary), RARRAY_SHARED_FLAG); \
115#define FL_UNSET_SHARED(ary) FL_UNSET((ary), RARRAY_SHARED_FLAG)
117#define ARY_SET_PTR_FORCE(ary, p) \
118 (RARRAY(ary)->as.heap.ptr = (p))
119#define ARY_SET_PTR(ary, p) do { \
120 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
121 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
122 ARY_SET_PTR_FORCE(ary, p); \
124#define ARY_SET_EMBED_LEN(ary, n) do { \
125 rb_len_t tmp_n = (n); \
126 RUBY_ASSERT(ARY_EMBED_P(ary)); \
127 RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
128 RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
130#define ARY_SET_HEAP_LEN(ary, n) do { \
131 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
132 RARRAY(ary)->as.heap.len = (n); \
134#define ARY_SET_LEN(ary, n) do { \
135 if (ARY_EMBED_P(ary)) { \
136 ARY_SET_EMBED_LEN((ary), (n)); \
139 ARY_SET_HEAP_LEN((ary), (n)); \
141 RUBY_ASSERT(RARRAY_LEN(ary) == (n)); \
143#define ARY_INCREASE_PTR(ary, n) do { \
144 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
145 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
146 RARRAY(ary)->as.heap.ptr += (n); \
148#define ARY_INCREASE_LEN(ary, n) do { \
149 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
150 if (ARY_EMBED_P(ary)) { \
151 ARY_SET_EMBED_LEN((ary), RARRAY_LEN(ary)+(n)); \
154 RARRAY(ary)->as.heap.len += (n); \
158#define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? ary_embed_capa(ary) : \
159 ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : ARY_HEAP_CAPA(ary))
160#define ARY_SET_CAPA_FORCE(ary, n) \
161 RARRAY(ary)->as.heap.aux.capa = (n);
162#define ARY_SET_CAPA(ary, n) do { \
163 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
164 RUBY_ASSERT(!ARY_SHARED_P(ary)); \
165 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
166 ARY_SET_CAPA_FORCE(ary, n); \
169#define ARY_SHARED_ROOT_OCCUPIED(ary) (!OBJ_FROZEN(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1)
170#define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \
171 RUBY_ASSERT(ARY_SHARED_ROOT_P(ary)); \
172 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
173 RUBY_ASSERT((value) >= 0); \
174 RARRAY(ary)->as.heap.aux.capa = (value); \
176#define FL_SET_SHARED_ROOT(ary) do { \
177 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
178 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
179 FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \
193ary_embed_capa(
VALUE ary)
195 size_t size = rb_obj_shape_slot_size(ary) - offsetof(
struct RArray, as.
ary);
197 return size /
sizeof(
VALUE);
204 if (size <
sizeof(
struct RArray)) size =
sizeof(
struct RArray);
213 return capa <= embed_len_max && rb_gc_size_allocatable_p(ary_embed_size(
capa));
231 return ARY_HEAP_CAPA(
ary) <= embed_len_max;
238rb_ary_embedded_shared_root_p(
VALUE ary)
248 if (ARY_EMBED_P(
ary)) {
249 real_size = ary_embed_size(ARY_EMBED_LEN(
ary));
251 else if (rb_ary_embeddable_p(
ary)) {
252 real_size = ary_embed_size(ARY_HEAP_CAPA(
ary));
255 real_size =
sizeof(
struct RArray);
262#define ary_verify(ary) ary_verify_(ary, __FILE__, __LINE__)
265ary_verify_(
VALUE ary,
const char *file,
int line)
269 if (ARY_SHARED_P(
ary)) {
271 const VALUE *ptr = ARY_HEAP_PTR(
ary);
278 else if (ARY_EMBED_P(
ary)) {
287 for (i=0; i<
len; i++) {
296#define ary_verify(ary) ((void)0)
327 memfill(ptr + beg, size, val);
337 if (argc > (
int)(128/
sizeof(
VALUE)) ) {
338 rb_gc_writebarrier_remember(buff_owner_ary);
346 for (i=0; i<argc; i++) {
356 ary_memcpy0(
ary, beg, argc, argv,
ary);
360ary_heap_alloc_buffer(
size_t capa)
368 ruby_xfree_sized((
void *)ptr, size);
374 ary_heap_free_ptr(
ary, ARY_HEAP_PTR(
ary), ARY_HEAP_SIZE(
ary));
378ary_heap_realloc(
VALUE ary,
size_t new_capa)
391 if (!ARY_EMBED_P(
ary)) {
392 const VALUE *buf = ARY_HEAP_PTR(
ary);
397 ARY_SET_EMBED_LEN(
ary,
len);
412 if (capacity > ary_embed_capa(
ary)) {
413 size_t new_capa = capacity;
414 if (ARY_EMBED_P(
ary)) {
416 VALUE *ptr = ary_heap_alloc_buffer(capacity);
420 ARY_SET_PTR(
ary, ptr);
421 ARY_SET_HEAP_LEN(
ary,
len);
424 new_capa = ary_heap_realloc(
ary, capacity);
426 ARY_SET_CAPA(
ary, new_capa);
429 if (!ARY_EMBED_P(
ary)) {
432 const VALUE *ptr = ARY_HEAP_PTR(
ary);
434 if (
len > capacity)
len = capacity;
436 ary_heap_free_ptr(
ary, ptr, old_capa *
sizeof(
VALUE));
453 if (old_capa > capacity) {
454 size_t new_capa = ary_heap_realloc(
ary, capacity);
455 ARY_SET_CAPA(
ary, new_capa);
466 if (new_capa < ARY_DEFAULT_SIZE) {
467 new_capa = ARY_DEFAULT_SIZE;
469 if (new_capa >= ARY_MAX_SIZE - min) {
470 new_capa = (ARY_MAX_SIZE - min) / 2;
473 ary_resize_capa(
ary, new_capa);
492 FL_UNSET_SHARED(
ary);
498 if (ARY_OWNS_HEAP_P(
ary)) {
501 else if (ARY_SHARED_P(
ary)) {
506 ARY_SET_EMBED_LEN(
ary, 0);
531 RB_DEBUG_COUNTER_INC(obj_ary_shared_create);
539 rb_check_frozen(
ary);
546 if (ARY_SHARED_P(
ary)) {
552 if (
len <= ary_embed_capa(
ary)) {
553 const VALUE *ptr = ARY_HEAP_PTR(
ary);
554 FL_UNSET_SHARED(
ary);
558 ARY_SET_EMBED_LEN(
ary,
len);
562 FL_UNSET_SHARED(
ary);
564 ARY_SET_CAPA_FORCE(
ary, shared_len);
572 VALUE *ptr = ary_heap_alloc_buffer(
len);
575 ARY_SET_CAPA_FORCE(
ary,
len);
576 ARY_SET_PTR_FORCE(
ary, ptr);
579 rb_gc_writebarrier_remember(
ary);
587 rb_ary_modify_check(
ary);
588 rb_ary_cancel_sharing(
ary);
595 rb_len_t new_len = old_len + add_len;
598 if (old_len > ARY_MAX_SIZE - add_len) {
601 if (ARY_SHARED_P(
ary)) {
602 if (new_len > ary_embed_capa(
ary)) {
606 rb_ary_modify_check(
ary);
617 ary_double_capa(
ary, new_len);
628 rb_ary_modify_check(
ary);
631 if (new_len >
capa) {
632 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);
689 return rb_newobj_of(klass,
T_ARRAY | RARRAY_EMBED_FLAG, size);
693ary_alloc_heap(
VALUE klass)
697 ary->as.heap.len = 0;
698 ary->as.heap.aux.capa = 0;
699 ary->as.heap.ptr = NULL;
705empty_ary_alloc(
VALUE klass)
707 RUBY_DTRACE_CREATE_HOOK(ARRAY, 0);
708 return ary_alloc_embed(klass, 0);
719 rb_raise(rb_eArgError,
"negative array size (or size too big)");
721 if (
capa > ARY_MAX_SIZE) {
722 rb_raise(rb_eArgError,
"array size too big");
725 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
727 if (ary_embeddable_p(
capa)) {
728 ary = ary_alloc_embed(klass,
capa);
731 ary = ary_alloc_heap(klass);
735 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
736 ARY_SET_HEAP_LEN(
ary, 0);
755(rb_ary_new_from_args)(
rb_len_t n, ...)
764 for (i=0; i<n; i++) {
778 ary = ary_new(klass, n);
780 ary_memcpy(
ary, 0, n, elts);
790 return rb_ary_tmp_new_from_values(
rb_cArray, n, elts);
796 size_t size = ary_embed_size(
capa);
802 return rb_ec_newobj_of(ec, klass,
T_ARRAY | RARRAY_EMBED_FLAG, size);
806ec_ary_alloc_heap(rb_execution_context_t *ec,
VALUE klass)
821 rb_raise(rb_eArgError,
"negative array size (or size too big)");
823 if (
capa > ARY_MAX_SIZE) {
824 rb_raise(rb_eArgError,
"array size too big");
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);
845rb_ec_ary_new_from_values(rb_execution_context_t *ec,
rb_len_t n,
const VALUE *elts)
851 ary_memcpy(
ary, 0, n, elts);
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);
899static VALUE fake_ary_flags;
902init_fake_ary_flags(
void)
904 struct RArray fake_ary = {0};
907 RBASIC_SET_FULL_SHAPE_ID(
ary, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER);
916 RBASIC_CLEAR_CLASS((
VALUE)fake_ary);
922 return (
VALUE)fake_ary;
928 if (ARY_OWNS_HEAP_P(
ary)) {
929 return ARY_CAPA(
ary) *
sizeof(
VALUE);
941 if (ARY_SHARED_P(
ary)) {
942 return ARY_SHARED_ROOT(
ary);
944 else if (ARY_SHARED_ROOT_P(
ary)) {
956 VALUE shared = ary_alloc_heap(0);
957 FL_SET_SHARED_ROOT(shared);
959 if (ARY_EMBED_P(
ary)) {
960 VALUE *ptr = ary_heap_alloc_buffer(
capa);
961 ARY_SET_PTR(shared, ptr);
965 ARY_SET_HEAP_LEN(
ary,
len);
966 ARY_SET_PTR(
ary, ptr);
972 ARY_SET_LEN(shared,
capa);
974 rb_ary_set_shared(
ary, shared);
988 if (ary_embeddable_p(
len)) {
993 ARY_SET_EMBED_LEN(subst,
len);
997 return rb_ary_increment_share(ary_make_shared(
ary));
1010 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1012#define to_ary rb_to_array_type
1017 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1023 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1029 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1058rb_ary_s_new(
int argc,
VALUE *argv,
VALUE klass)
1064 if (argc > 0 &&
FIXNUM_P(argv[0])) {
1066 if (size < 0) size = 0;
1069 ary = ary_new(klass, size);
1148 if (argc == 1 && !
FIXNUM_P(size)) {
1159 rb_raise(rb_eArgError,
"negative array size");
1161 if (
len > ARY_MAX_SIZE) {
1162 rb_raise(rb_eArgError,
"array size too big");
1166 ARY_SET_LEN(
ary, 0);
1167 ary_resize_capa(
ary,
len);
1172 rb_warn(
"block supersedes default value argument");
1174 for (i=0; i<
len; i++) {
1176 ARY_SET_LEN(
ary, i + 1);
1180 ary_memfill(
ary, 0,
len, val);
1197rb_ary_s_create(
int argc,
VALUE *argv,
VALUE klass)
1200 if (argc > 0 && argv) {
1201 ary_memcpy(
ary, 0, argc, argv);
1202 ARY_SET_LEN(
ary, argc);
1216 rb_raise(
rb_eIndexError,
"index %"PRIdLEN
" too small for array; minimum: %"PRIdLEN,
1220 else if (idx >= ARY_MAX_SIZE) {
1225 if (idx >= ARY_CAPA(
ary)) {
1226 ary_double_capa(
ary, idx);
1233 ARY_SET_LEN(
ary, idx + 1);
1235 ARY_SET(
ary, idx, val);
1245 VALUE result = ary_alloc_heap(klass);
1246 size_t embed_capa = ary_embed_capa(result);
1247 if ((
size_t)
len <= embed_capa) {
1248 FL_SET_EMBED(result);
1250 ARY_SET_EMBED_LEN(result,
len);
1253 VALUE shared = ary_make_shared(
ary);
1258 FL_UNSET_EMBED(result);
1262 rb_ary_set_shared(result, shared);
1264 ARY_INCREASE_PTR(result, offset);
1265 ARY_SET_LEN(result,
len);
1284 if (step > 0 && step >=
len) {
1285 VALUE result = ary_new(klass, 1);
1290 ARY_SET_EMBED_LEN(result, 1);
1293 else if (step < 0 && step < -
len) {
1297 rb_len_t ustep = (step < 0) ? -step : step;
1298 len = roomof(
len, ustep);
1301 rb_len_t j = offset + ((step > 0) ? 0 : (orig_len - 1));
1303 VALUE result = ary_new(klass,
len);
1304 if (ARY_EMBED_P(result)) {
1308 for (i = 0; i <
len; ++i) {
1312 ARY_SET_EMBED_LEN(result,
len);
1318 for (i = 0; i <
len; ++i) {
1323 ARY_SET_LEN(result,
len);
1336ary_make_hidden_shared_copy(
VALUE ary)
1341enum ary_take_pos_flags
1348ary_take_first_or_last_n(
VALUE ary,
rb_len_t n,
enum ary_take_pos_flags last)
1357 rb_raise(rb_eArgError,
"negative array size");
1366ary_take_first_or_last(
int argc,
const VALUE *argv,
VALUE ary,
enum ary_take_pos_flags last)
1373 return ary_take_first_or_last_n(
ary,
NUM2LEN(argv[0]), last);
1395 VALUE target_ary = ary_ensure_room_for_push(
ary, 1);
1399 ARY_SET_LEN(
ary, idx + 1);
1408 VALUE target_ary = ary_ensure_room_for_push(
ary,
len);
1409 ary_memcpy0(
ary, oldlen,
len, argv, target_ary);
1410 ARY_SET_LEN(
ary, oldlen +
len);
1442 rb_ary_modify_check(
ary);
1444 if (n == 0)
return Qnil;
1445 if (ARY_OWNS_HEAP_P(
ary) &&
1446 n * 3 < ARY_CAPA(
ary) &&
1447 ARY_CAPA(
ary) > ARY_DEFAULT_SIZE)
1449 ary_resize_capa(
ary, n * 2);
1454 ARY_SET_LEN(
ary, n - 1);
1498 rb_ary_modify_check(
ary);
1499 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
1500 rb_ary_modify_check(
ary);
1513 rb_ary_modify_check(
ary);
1519 rb_ary_behead(
ary, 1);
1572 rb_ary_modify_check(
ary);
1573 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1575 rb_ary_behead(
ary,n);
1587 rb_ary_modify_check(
ary);
1589 if (!ARY_SHARED_P(
ary)) {
1594 ARY_INCREASE_LEN(
ary, -n);
1599 ary_mem_clear(
ary, 0, n);
1600 ary_make_shared(
ary);
1602 else if (ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(
ary))) {
1603 ary_mem_clear(
ary, 0, n);
1606 ARY_INCREASE_PTR(
ary, n);
1607 ARY_INCREASE_LEN(
ary, -n);
1616 if (head - sharedp < argc) {
1621 head = sharedp + argc + room;
1623 ARY_SET_PTR(
ary, head - argc);
1627 return ARY_SHARED_ROOT(
ary);
1631ary_modify_for_unshift(
VALUE ary,
int argc)
1636 const VALUE *head, *sharedp;
1640 if (
capa - (
capa >> 6) <= new_len) {
1641 ary_double_capa(
ary, new_len);
1645 if (new_len > ARY_DEFAULT_SIZE * 4 && !ARY_EMBED_P(
ary)) {
1650 ary_make_shared(
ary);
1653 return make_room_for_unshift(
ary, head, (
void *)sharedp, argc,
capa,
len);
1667ary_ensure_room_for_unshift(
VALUE ary,
int argc)
1672 if (
len > ARY_MAX_SIZE - argc) {
1675 else if (! ARY_SHARED_P(
ary)) {
1676 return ary_modify_for_unshift(
ary, argc);
1683 return ary_modify_for_unshift(
ary, argc);
1685 else if (new_len >
capa) {
1686 return ary_modify_for_unshift(
ary, argc);
1692 rb_ary_modify_check(
ary);
1693 return make_room_for_unshift(
ary, head, sharedp, argc,
capa,
len);
1719 rb_ary_modify_check(
ary);
1723 target_ary = ary_ensure_room_for_unshift(
ary, argc);
1724 ary_memcpy0(
ary, 0, argc, argv, target_ary);
1725 ARY_SET_LEN(
ary,
len + argc);
1732 return rb_ary_unshift_m(1, &item,
ary);
1741 if (offset < 0 ||
len <= offset) {
1750 return rb_ary_entry_internal(
ary, offset);
1758 if (beg > alen)
return -1;
1759 if (beg < 0 ||
len < 0)
return -1;
1761 if (alen <
len || alen < beg +
len) {
1774 if (
len == 0)
return ary_new(klass, 0);
1775 return ary_make_partial(
ary, klass, beg,
len);
1942 return rb_ary_aref2(
ary, argv[0], argv[1]);
1944 return rb_ary_aref1(
ary, argv[0]);
1969 switch (rb_arithmetic_sequence_beg_len_step(arg, &beg, &
len, &step,
RARRAY_LEN(
ary), 0)) {
1975 if (step == 0) rb_raise(rb_eArgError,
"slice step cannot be zero");
1977 if (
len <= 0)
return ary_new(klass, 0);
1978 if (step == 1)
return ary_make_partial(
ary, klass, beg,
len);
1979 return ary_make_partial_step(
ary, klass, beg,
len, step);
2024 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
2030ary_first(
VALUE self)
2046 return ary_last(
ary);
2049 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
2102 if (block_given && argc == 2) {
2103 rb_warn(
"block supersedes default value argument");
2111 if (block_given)
return rb_yield(pos);
2113 rb_raise(
rb_eIndexError,
"index %"PRIdLEN
" outside of array bounds: %"PRIdLEN
"...%"PRIdLEN,
2157 if (!
NIL_P(if_none)) {
2201 idx = (idx >=
len) ?
len : idx;
2204 if (!
NIL_P(if_none)) {
2211offset_option(
VALUE opts)
2213 static ID keywords[1];
2276 int nargc =
rb_scan_args(argc, argv,
"01:", &val, &opts);
2281 rb_warn(
"given block not used");
2284 VALUE initpos = offset_option(opts);
2285 if (!UNDEF_P(initpos)) {
2352 int nargc =
rb_scan_args(argc, argv,
"01:", &val, &opts);
2357 rb_warn(
"given block not used");
2360 VALUE offset_arg = offset_option(opts);
2361 if (!UNDEF_P(offset_arg)) {
2366 if (offset < i) i = offset;
2369 for (; i >= 0; i--) {
2386 if (!
NIL_P(tmp))
return tmp;
2400 rb_raise(
rb_eIndexError,
"index %"PRIdLEN
" too small for array; minimum: %"PRIdLEN,
2404 if (olen <
len || olen < beg +
len) {
2410 if (beg > ARY_MAX_SIZE - rlen) {
2413 target_ary = ary_ensure_room_for_push(
ary, rlen-
len);
2415 ary_mem_clear(
ary, olen, beg - olen);
2419 ary_memcpy0(
ary, beg, rlen, rptr, target_ary);
2426 if (olen -
len > ARY_MAX_SIZE - rlen) {
2430 alen = olen + rlen -
len;
2431 if (alen >= ARY_CAPA(
ary)) {
2432 ary_double_capa(
ary, alen);
2439 ARY_SET_LEN(
ary, alen);
2443 rb_gc_writebarrier_remember(
ary);
2472 rb_ary_modify_check(
ary);
2473 if (ARY_SHARED_P(
ary)) {
2477 rb_bug(
"probable buffer overflow: %"PRIdLEN
" for %"PRIdLEN,
len,
capa);
2488 rb_raise(rb_eArgError,
"negative expanding array size");
2490 if (expand >= ARY_MAX_SIZE -
len) {
2491 rb_raise(rb_eArgError,
" size too big");
2493 rb_ary_modify_check(
ary);
2494 if (
len + expand > ARY_CAPA(
ary)) {
2495 ary_resize_capa(
ary,
len + expand);
2507 if (
len == olen)
return ary;
2508 if (
len > ARY_MAX_SIZE) {
2512 if (
len > ARY_CAPA(
ary)) {
2513 ary_double_capa(
ary,
len);
2515 ary_mem_clear(
ary, olen,
len - olen);
2518 else if (ARY_EMBED_P(
ary)) {
2519 ARY_SET_EMBED_LEN(
ary,
len);
2521 else if (
len <= ary_embed_capa(
ary)) {
2522 const VALUE *ptr = ARY_HEAP_PTR(
ary);
2524 bool is_malloc_ptr = !ARY_SHARED_P(
ary);
2529 ARY_SET_EMBED_LEN(
ary,
len);
2531 if (is_malloc_ptr) ruby_xfree_sized((
void *)ptr, ptr_capa);
2534 if (olen >
len + ARY_DEFAULT_SIZE) {
2535 size_t new_capa = ary_heap_realloc(
ary,
len);
2536 ARY_SET_CAPA(
ary, new_capa);
2538 ARY_SET_HEAP_LEN(
ary,
len);
2708 rb_ary_modify_check(
ary);
2712 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[2]);
2716 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2720 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[1]);
2724 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2769 rb_ary_modify_check(
ary);
2771 if (argc == 1)
return ary;
2778 rb_raise(
rb_eIndexError,
"index %"PRIdLEN
" too small for array; minimum: %"PRIdLEN,
2783 ary_splice(
ary, pos, 0, argv + 1, argc - 1, FALSE);
2793 return rb_ary_length(
ary);
2804rb_builtin_ary_at_end(rb_execution_context_t *ec,
VALUE self,
VALUE index)
2811rb_builtin_ary_at(rb_execution_context_t *ec,
VALUE self,
VALUE index)
2818rb_builtin_fixnum_inc(rb_execution_context_t *ec,
VALUE self,
VALUE num)
2824rb_builtin_ary_first(rb_execution_context_t *ec,
VALUE self)
2826 return ary_first(self);
2830rb_builtin_ary_last(rb_execution_context_t *ec,
VALUE self)
2832 return ary_last(self);
2882 rb_execution_context_t *ec = GET_EC();
2973 ARY_SET_LEN(dup,
len);
2988rb_zjit_array_new_can_fastpath(
rb_len_t len,
size_t *alloc_size_out,
VALUE *flags_out)
2990 if (!ary_embeddable_p(
len)) {
2995 *alloc_size_out = embed_size;
3001rb_zjit_array_dup_can_fastpath(
VALUE ary,
size_t *alloc_size_out,
VALUE *flags_out,
rb_len_t *len_out)
3004 if (!rb_zjit_array_new_can_fastpath(
len, alloc_size_out, flags_out)) {
3019recursive_join(
VALUE obj,
VALUE argp,
int recur)
3024 VALUE result = arg[2];
3025 int *first = (
int *)arg[3];
3028 rb_raise(rb_eArgError,
"recursive array join");
3031 ary_join_1(obj,
ary, sep, 0, result, first);
3043 for (i=0; i<max; i++) {
3046 if (i > 0 && !
NIL_P(sep))
3054ary_join_1_str(
VALUE dst,
VALUE src,
int *first)
3058 rb_enc_copy(dst, src);
3067 rb_raise(rb_eArgError,
"recursive array join");
3076 args[3] = (
VALUE)first;
3087 if (i > 0 && !
NIL_P(sep))
3092 ary_join_1_str(result, val, first);
3095 ary_join_1_ary(val,
ary, sep, result, val, first);
3098 ary_join_1_str(result, tmp, first);
3101 ary_join_1_ary(val,
ary, sep, result, tmp, first);
3119 if (n == 0)
return Qundef;
3124 if (!rb_str_encindex_fastpath(encidx))
return Qundef;
3129 const char *sep_ptr = NULL;
3131 int sep_cr = rb_enc_str_coderange(sep);
3135 sep_ptr = RSTRING_PTR(sep);
3136 sep_len = RSTRING_LEN(sep);
3147 len += RSTRING_LEN(s);
3152 rb_enc_associate_index(result, encidx);
3153 char *
const buf = RSTRING_PTR(result);
3158 if (i > 0 && sep_len) {
3159 memcpy(p, sep_ptr, sep_len);
3162 memcpy(p, RSTRING_PTR(s), slen);
3176 VALUE val, tmp, result;
3182 result = ary_join_fast(
ary, sep);
3183 if (!UNDEF_P(result))
return result;
3189 for (i=0; i < len_memo; i++) {
3193 if (
NIL_P(tmp) || tmp != val) {
3199 i = ary_join_0(
ary, sep, i, result);
3201 ary_join_1(
ary,
ary, sep, i, result, &first);
3204 len += RSTRING_LEN(tmp);
3208 len += RSTRING_LEN(val);
3267 else rb_enc_copy(str, s);
3298 return rb_ary_inspect(
ary);
3362 const VALUE e = rb_ary_elt(
ary, i);
3363 const VALUE elt = block_given ? rb_yield_force_blockarg(e) : e;
3365 if (
NIL_P(key_value_pair)) {
3366 rb_raise(
rb_eTypeError,
"wrong element type %"PRIsVALUE
" at %"PRIdLEN
" (expected array)",
3370 rb_raise(rb_eArgError,
"wrong array length at %"PRIdLEN
" (expected 2, was %"PRIdLEN
")",
3411 ary_reverse(p1, p2);
3457 do *p2-- = *p1++;
while (--
len > 0);
3458 rb_gc_writebarrier_remember(dup);
3467 return (cnt < 0) ? (
len - (~cnt %
len) - 1) : (cnt %
len);
3475 memmove(ptr, ptr + 1,
sizeof(
VALUE)*(
len - 1));
3476 *(ptr +
len - 1) = tmp;
3478 else if (cnt ==
len - 1) {
3480 memmove(ptr + 1, ptr,
sizeof(
VALUE)*(
len - 1));
3485 if (cnt <
len) ary_reverse(ptr + cnt, ptr +
len);
3486 if (--cnt > 0) ary_reverse(ptr, ptr + cnt);
3487 if (
len > 0) ary_reverse(ptr, ptr +
len);
3498 if (
len > 1 && (cnt = rotate_count(cnt,
len)) > 0) {
3590 cnt = rotate_count(cnt,
len);
3593 ary_memcpy(rotated, 0,
len, ptr + cnt);
3594 ary_memcpy(rotated,
len, cnt, ptr);
3600struct ary_sort_data {
3606sort_reentered(
VALUE ary)
3608 if (
RBASIC(ary)->klass) {
3617 int ret = rb_cmpint(cmp, a, b);
3621 sort_reentered(data->ary);
3626sort_1(
const void *ap,
const void *bp,
void *dummy)
3628 struct ary_sort_data *data = dummy;
3634 sort_reentered(data->ary);
3639sort_2(
const void *ap,
const void *bp,
void *dummy)
3641 struct ary_sort_data *data = dummy;
3644 sort_reentered(data->ary);
3645#define rb_cmpint(cmp, a, b) sort_cmpint(data, cmp, a, b)
3646 return OPTIMIZED_CMP(a, b);
3666 VALUE tmp = ary_make_substitution(ary);
3667 struct ary_sort_data data;
3669 RBASIC_CLEAR_CLASS(tmp);
3671 data.receiver = ary;
3677 if (ARY_EMBED_P(tmp)) {
3678 if (ARY_SHARED_P(ary)) {
3679 rb_ary_unshare(ary);
3682 if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) {
3683 ary_resize_capa(ary, ARY_EMBED_LEN(tmp));
3685 ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp));
3686 ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
3688 else if (ARY_SHARED_ROOT_REFCNT(tmp) > 1) {
3690 if (
len > ARY_CAPA(ary)) {
3691 ary_resize_capa(ary,
len);
3693 ary_memcpy(ary, 0,
len, ARY_HEAP_PTR(tmp));
3694 ARY_SET_LEN(ary,
len);
3695 FL_UNSET(tmp, RARRAY_SHARED_ROOT_FLAG);
3700 if (!ARY_EMBED_P(ary) && ARY_HEAP_PTR(ary) == ARY_HEAP_PTR(tmp)) {
3701 FL_UNSET_SHARED(ary);
3706 if (ARY_EMBED_P(ary)) {
3707 FL_UNSET_EMBED(ary);
3709 else if (ARY_SHARED_P(ary)) {
3711 rb_ary_unshare(ary);
3716 ARY_SET_PTR(ary, ARY_HEAP_PTR(tmp));
3717 ARY_SET_HEAP_LEN(ary,
len);
3718 ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
3719 rb_gc_writebarrier_remember(ary);
3723 ARY_SET_EMBED_LEN(tmp, 0);
3791rb_ary_bsearch(
VALUE ary)
3793 VALUE index_result = rb_ary_bsearch_index(ary);
3798 return index_result;
3815rb_ary_bsearch_index(
VALUE ary)
3818 int smaller = 0, satisfied = 0;
3822 while (low < high) {
3823 mid = low + ((high - low) / 2);
3830 else if (v ==
Qtrue) {
3834 else if (!
RTEST(v)) {
3839 switch (rb_cmpint(
rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
3841 case 1: smaller = 0;
break;
3842 case -1: smaller = 1;
3847 " (must be numeric, true, false or nil)",
3857 if (!satisfied)
return Qnil;
3891rb_ary_sort_by_bang(
VALUE ary)
3898 sorted =
rb_block_call(ary, rb_intern(
"sort_by"), 0, 0, sort_by_i, 0);
3926rb_ary_collect(
VALUE ary)
3961rb_ary_collect_bang(
VALUE ary)
3979 for (i=0; i<argc; i++) {
3987 for (j = beg; j < end; j++) {
3990 if (beg +
len > j) {
3994 if (rest > ARY_MAX_SIZE -
RARRAY_LEN(result)) {
4145rb_ary_values_at(
int argc,
VALUE *argv,
VALUE ary)
4149 for (i = 0; i < argc; ++i) {
4150 append_values_at_single(result, ary, olen, argv[i]);
4178rb_ary_select(
VALUE ary)
4193struct select_bang_arg {
4199select_bang_i(
VALUE a)
4201 volatile struct select_bang_arg *arg = (
void *)a;
4202 VALUE ary = arg->ary;
4205 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4213 return (i1 == i2) ?
Qnil : ary;
4217select_bang_ensure(
VALUE a)
4219 volatile struct select_bang_arg *arg = (
void *)a;
4220 VALUE ary = arg->ary;
4222 rb_len_t i1 = arg->len[0], i2 = arg->len[1];
4224 if (i2 <
len && i2 < i1) {
4233 ARY_SET_LEN(ary, i2 + tail);
4261rb_ary_select_bang(
VALUE ary)
4263 struct select_bang_arg args;
4269 args.len[0] = args.len[1] = 0;
4290rb_ary_keep_if(
VALUE ary)
4293 rb_ary_select_bang(ary);
4302 ARY_SET_LEN(ary,
len);
4303 if (
len * 2 < ARY_CAPA(ary) &&
4304 ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
4305 ary_resize_capa(ary,
len * 2);
4353 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4356 if (rb_equal(e, item)) {
4372 ary_resize_smaller(ary, i2);
4383 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4398 ary_resize_smaller(ary, i2);
4410 if (pos < 0)
return Qnil;
4418 ARY_INCREASE_LEN(ary, -1);
4465 else if (pos < -orig_len) {
4471 else if (orig_len < pos) {
4474 if (orig_len < pos +
len) {
4475 len = orig_len - pos;
4482 ary_splice(ary, pos,
len, 0, 0, FALSE);
4580rb_ary_slice_bang(
int argc,
VALUE *argv,
VALUE ary)
4585 rb_ary_modify_check(ary);
4592 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4599 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4628reject_bang_i(
VALUE a)
4630 volatile struct select_bang_arg *arg = (
void *)a;
4631 VALUE ary = arg->ary;
4634 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4642 return (i1 == i2) ?
Qnil : ary;
4646ary_reject_bang(
VALUE ary)
4648 struct select_bang_arg args;
4649 rb_ary_modify_check(ary);
4651 args.len[0] = args.len[1] = 0;
4676rb_ary_reject_bang(
VALUE ary)
4680 return ary_reject_bang(ary);
4701rb_ary_reject(
VALUE ary)
4707 ary_reject(ary, rejected_ary);
4708 return rejected_ary;
4729rb_ary_delete_if(
VALUE ary)
4733 ary_reject_bang(ary);
4740 struct MEMO *memo = MEMO_CAST(cbarg);
4753 if (n == 0)
return result;
4756 memo = (
VALUE)rb_imemo_memo_new(result,
Qnil, n);
4757 if (UNDEF_P(rb_check_block_call(obj, idEach, 0, 0, take_i, memo)))
4758 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
4865 for (i=0; i<argc; i++) {
4866 argv[i] = take_items(argv[i],
len);
4870 int arity = rb_block_arity();
4879 for (j=0; j<argc; j++) {
4880 tmp[j+1] = rb_ary_elt(argv[j], i);
4892 for (j=0; j<argc; j++) {
4906 for (j=0; j<argc; j++) {
4932rb_ary_transpose(
VALUE ary)
4935 VALUE tmp, result = 0;
4939 for (i=0; i<alen; i++) {
4940 tmp = to_ary(rb_ary_elt(ary, i));
4944 for (j=0; j<elen; j++) {
4949 rb_raise(
rb_eIndexError,
"element size differs (%"PRIdLEN
" should be %"PRIdLEN
")",
4952 for (j=0; j<elen; j++) {
4953 rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
4977 rb_ary_modify_check(copy);
4978 orig = to_ary(orig);
4979 if (copy == orig)
return copy;
4984 if (
RARRAY_LEN(orig) <= ary_embed_capa(copy)) {
4991 else if (ARY_EMBED_P(orig)) {
4993 VALUE *ptr = ary_heap_alloc_buffer(
len);
4995 FL_UNSET_EMBED(copy);
4996 ARY_SET_PTR(copy, ptr);
4997 ARY_SET_LEN(copy,
len);
4998 ARY_SET_CAPA(copy,
len);
5007 VALUE shared_root = ary_make_shared(orig);
5008 FL_UNSET_EMBED(copy);
5009 ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
5010 ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
5011 rb_ary_set_shared(copy, shared_root);
5034 rb_ary_modify_check(ary);
5035 if (ARY_SHARED_P(ary)) {
5036 rb_ary_unshare(ary);
5038 ARY_SET_EMBED_LEN(ary, 0);
5041 ARY_SET_LEN(ary, 0);
5042 if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
5043 ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
5257 if (beg < 0) beg = 0;
5266 if (beg >= ARY_MAX_SIZE ||
len > ARY_MAX_SIZE - beg) {
5267 rb_raise(rb_eArgError,
"argument too big");
5271 if (end >= ARY_CAPA(ary)) {
5272 ary_resize_capa(ary, end);
5275 ARY_SET_LEN(ary, end);
5278 if (UNDEF_P(item)) {
5282 for (i=beg; i<end; i++) {
5290 ary_memfill(ary, beg,
len, item);
5322 ARY_SET_LEN(z,
len);
5349rb_ary_concat_multi(
int argc,
VALUE *argv,
VALUE ary)
5351 rb_ary_modify_check(ary);
5356 else if (argc > 1) {
5359 for (i = 0; i < argc; i++) {
5362 ary_append(ary, args);
5372 return ary_append(x, to_ary(y));
5411 rb_raise(rb_eArgError,
"negative argument");
5414 rb_raise(rb_eArgError,
"argument too big");
5419 ARY_SET_LEN(ary2,
len);
5424 ary_memcpy(ary2, 0, t, ptr);
5425 while (t <=
len/2) {
5502recursive_equal(
VALUE ary1,
VALUE ary2,
int recur)
5505 const VALUE *p1, *p2;
5507 if (recur)
return Qtrue;
5514 for (i = 0; i < len1; i++) {
5516 if (rb_equal(*p1, *p2)) {
5562 if (ary1 == ary2)
return Qtrue;
5567 return rb_equal(ary2, ary1);
5575recursive_eql(
VALUE ary1,
VALUE ary2,
int recur)
5578 const VALUE *p1, *p2;
5580 if (recur)
return Qtrue;
5587 for (i = 0; i < len1; i++) {
5588 if (p1[i] != p2[i]) {
5589 if (!rb_eql(p1[i], p2[i]))
return Qfalse;
5621 if (ary1 == ary2)
return Qtrue;
5637 for (i=0; i<
len; i++) {
5638 n = rb_hash(elements[i]);
5652 return ary_hash_values(
len, elements, 0);
5671rb_ary_hash(
VALUE ary)
5699 if (rb_equal(e, item)) {
5714 if (rb_eql(item, e)) {
5722recursive_cmp(
VALUE ary1,
VALUE ary2,
int recur)
5726 if (recur)
return Qundef;
5731 for (i=0; i<
len; i++) {
5732 VALUE e1 = rb_ary_elt(ary1, i), e2 = rb_ary_elt(ary2, i);
5787 if (ary1 == ary2)
return INT2FIX(0);
5789 if (!UNDEF_P(v))
return v;
5805ary_to_set(
VALUE ary)
5808 rb_ary_union_set(set, ary);
5833 ary2 = to_ary(ary2);
5834 if (
RARRAY_LEN(ary2) == 0) {
return ary_make_shared_copy(ary1); }
5839 VALUE elt = rb_ary_elt(ary1, i);
5840 if (rb_ary_includes_by_eql(ary2, elt))
continue;
5846 VALUE set = ary_to_set(ary2);
5875rb_ary_difference_multi(
int argc,
VALUE *argv,
VALUE ary)
5878 bool *is_set =
ALLOCV_N(
bool, t0, argc);
5882 for (
rb_len_t i = 0; i < argc; i++) {
5883 argv[i] = to_ary(argv[i]);
5884 is_set[i] = (length > SMALL_ARRAY_LEN &&
RARRAY_LEN(argv[i]) > SMALL_ARRAY_LEN);
5886 argv[i] = ary_to_set(argv[i]);
5892 VALUE elt = rb_ary_elt(ary, i);
5893 for (j = 0; j < argc; j++) {
5899 if (rb_ary_includes_by_eql(argv[j], elt))
break;
5938 ary2 = to_ary(ary2);
5945 if (!rb_ary_includes_by_eql(ary2, v))
continue;
5946 if (rb_ary_includes_by_eql(ary3, v))
continue;
5952 VALUE set = ary_to_set(ary2);
5956 if (rb_set_delete_no_check(set, v)) {
5986rb_ary_intersection_multi(
int argc,
VALUE *argv,
VALUE ary)
5991 for (i = 0; i < argc; i++) {
5992 result = rb_ary_and(result, argv[i]);
6003 VALUE elt = rb_ary_elt(ary, i);
6004 if (rb_ary_includes_by_eql(ary_union, elt))
continue;
6027 ary2 = to_ary(ary2);
6030 rb_ary_union(ary3, ary1);
6031 rb_ary_union(ary3, ary2);
6036 rb_ary_union_set(set, ary1);
6037 rb_ary_union_set(set, ary2);
6039 return rb_set_to_a(set);
6066rb_ary_union_multi(
int argc,
VALUE *argv,
VALUE ary)
6069 for (
int i = 0; i < argc; i++) {
6070 argv[i] = to_ary(argv[i]);
6074 if (sum <= SMALL_ARRAY_LEN) {
6077 rb_ary_union(ary_union, ary);
6078 for (
int i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]);
6084 rb_ary_union_set(set, ary);
6085 for (
int i = 0; i < argc; i++) rb_ary_union_set(set, argv[i]);
6087 return rb_set_to_a(set);
6107 ary2 = to_ary(ary2);
6113 if (rb_ary_includes_by_eql(ary2, v))
return Qtrue;
6118 VALUE shorter = ary1;
6119 VALUE longer = ary2;
6125 VALUE set = ary_to_set(shorter);
6148 if (rb_cmpint(
rb_funcallv(vmax, id_cmp, 1, &v), vmax, v) < 0) {
6164 for (; i < n; ++i) {
6168 if ((
long)vmax < (
long)v) {
6173 return ary_max_generic(ary, i, vmax);
6188 for (; i < n; ++i) {
6192 if (rb_float_cmp(vmax, v) < 0) {
6197 return ary_max_generic(ary, i, vmax);
6212 for (; i < n; ++i) {
6221 return ary_max_generic(ary, i, vmax);
6284 return rb_nmin_run(ary, num, 0, 1, 1);
6290 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) > 0) {
6298 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6299 return ary_max_opt_fixnum(ary, 1, result);
6301 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6302 return ary_max_opt_string(ary, 1, result);
6305 return ary_max_opt_float(ary, 1, result);
6308 return ary_max_generic(ary, 1, result);
6312 if (UNDEF_P(result))
return Qnil;
6325 if (rb_cmpint(
rb_funcallv(vmin, id_cmp, 1, &v), vmin, v) > 0) {
6341 for (; i < n; ++i) {
6345 if ((
long)vmin > (
long)a) {
6350 return ary_min_generic(ary, i, vmin);
6365 for (; i < n; ++i) {
6369 if (rb_float_cmp(vmin, a) > 0) {
6374 return ary_min_generic(ary, i, vmin);
6389 for (; i < n; ++i) {
6398 return ary_min_generic(ary, i, vmin);
6461 return rb_nmin_run(ary, num, 0, 0, 1);
6467 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) < 0) {
6475 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6476 return ary_min_opt_fixnum(ary, 1, result);
6478 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6479 return ary_min_opt_string(ary, 1, result);
6482 return ary_min_opt_float(ary, 1, result);
6485 return ary_min_generic(ary, 1, result);
6489 if (UNDEF_P(result))
return Qnil;
6516rb_ary_minmax(
VALUE ary)
6521 return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary));
6559rb_ary_uniq_bang(
VALUE ary)
6561 rb_ary_modify_check(ary);
6569 VALUE elt = rb_ary_elt(ary, i);
6570 if (rb_set_add_no_check(set,
rb_yield(elt)))
6579 VALUE set = ary_to_set(ary);
6584 rb_ary_modify_check(ary);
6585 ARY_SET_LEN(ary, 0);
6586 if (ARY_SHARED_P(ary)) {
6587 rb_ary_unshare(ary);
6590 ary_resize_capa(ary, size);
6623rb_ary_uniq(
VALUE ary)
6634 VALUE elt = rb_ary_elt(ary, i);
6635 if (rb_set_add_no_check(set,
rb_yield(elt)))
6641 rb_ary_union_set(set, ary);
6642 return rb_set_to_a(set);
6663rb_ary_compact_bang(
VALUE ary)
6680 ary_resize_smaller(ary, n);
6700rb_ary_compact(
VALUE ary)
6703 rb_ary_compact_bang(ary);
6735rb_ary_count(
int argc,
VALUE *argv,
VALUE ary)
6751 VALUE obj = argv[0];
6754 rb_warn(
"given block not used");
6765flatten(
VALUE ary,
int level)
6789 ARY_SET_LEN(result, i);
6791 stack = ary_new(0, ARY_DEFAULT_SIZE);
6807 if (level >= 0 &&
RARRAY_LEN(stack) / 2 >= level) {
6812 if (
RBASIC(result)->klass) {
6825 rb_raise(rb_eArgError,
"tried to flatten recursive array");
6855single_nested_array(
VALUE ary)
6906rb_ary_flatten_bang(
int argc,
VALUE *argv,
VALUE ary)
6908 int mod = 0, level = -1;
6912 rb_ary_modify_check(ary);
6914 if (level == 0)
return Qnil;
6916 VALUE child = single_nested_array(ary);
6922 if (level > 1) level--;
6923 result = flatten(child, level);
6927 result = flatten(ary, level);
6928 if (result == ary) {
6933 if (result != child && !(mod = ARY_EMBED_P(result)))
rb_ary_freeze(result);
6935 if (mod) ARY_SET_EMBED_LEN(result, 0);
6976rb_ary_flatten(
int argc,
VALUE *argv,
VALUE ary)
6983 if (level == 0)
return ary_make_shared_copy(ary);
6986 VALUE child = single_nested_array(ary);
6993 result = flatten(child, level);
6997 result = flatten(ary, level);
7000 if (result == ary || result == child) {
7001 return ary_make_shared_copy(result);
7007#if SIZEOF_RB_LEN_T > SIZEOF_LONG
7018 if (rb_num_negative_p(v)) {
7019 rb_raise(
rb_eRangeError,
"random number too small %"PRIsVALUE, v);
7021 if (
RTEST(rb_int_ge(v, lim))) {
7026#define RAND_UPTO(max) rand_upto(randgen, (max))
7028#define RAND_UPTO(max) (rb_len_t)rb_random_ulong_limited((randgen), (max)-1)
7032rb_ary_shuffle_bang(rb_execution_context_t *ec,
VALUE ary,
VALUE randgen)
7054rb_ary_shuffle(rb_execution_context_t *ec,
VALUE ary,
VALUE randgen)
7057 rb_ary_shuffle_bang(ec, ary, randgen);
7062 .wrap_struct_name =
"ary_sample_memo",
7066 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE
7070ary_sample1(rb_execution_context_t *ec,
VALUE ary,
const VALUE randgen)
7074 return rb_ary_elt(ary, i);
7078ary_sample0(rb_execution_context_t *ec,
VALUE ary)
7084ary_sample(rb_execution_context_t *ec,
VALUE ary,
VALUE randgen,
VALUE nv)
7091 if (n < 0) rb_raise(rb_eArgError,
"negative sample number");
7095 if (n <= numberof(idx)) {
7096 rb_len_t rnds[numberof(idx)], sorted[numberof(idx)];
7098 for (i = 0; i < n; ++i) {
7099 rnds[i] = RAND_UPTO(
len - i);
7104 for (i = 0; i < n; ++i) {
7114 return rb_ary_new_from_args(1,
RARRAY_AREF(ary, i));
7126 if (j >= i) l = i, g = ++j;
7127 if (k >= l && (++k >= g)) ++k;
7131 sorted[0] = idx[0] = rnds[0];
7132 for (i=1; i<n; i++) {
7134 for (j = 0; j < i; ++j) {
7135 if (k < sorted[j])
break;
7138 memmove(&sorted[j+1], &sorted[j],
sizeof(sorted[0])*(i-j));
7139 sorted[j] = idx[i] = k;
7143 for (i=0; i<n; i++) {
7147 ARY_SET_LEN(result, n);
7156 if (n <= memo_threshold / 2) {
7159 st_table *memo = st_init_numtable_with_size(n);
7163 for (i=0; i<n; i++) {
7166 if (r > max_idx) max_idx = r;
7169 if (
len <= max_idx) n = 0;
7170 else if (n >
len) n =
len;
7172 for (i=0; i<n; i++) {
7176 if (st_lookup(memo, (st_data_t)i, &value)) i2 = (
rb_len_t)value;
7177 if (st_lookup(memo, (st_data_t)j, &value)) j2 = (
rb_len_t)value;
7178 st_insert(memo, (st_data_t)j, (st_data_t)i2);
7179 ptr_result[i] = ptr_ary[j2];
7184 st_free_table(memo);
7189 RBASIC_CLEAR_CLASS(result);
7192 for (i=0; i<n; i++) {
7193 j = RAND_UPTO(
len-i) + i;
7195 ptr_result[j] = ptr_result[i];
7199 RBASIC_SET_CLASS_RAW(result,
rb_cArray);
7201 ARY_SET_LEN(result, n);
7207ary_sized_alloc(rb_execution_context_t *ec,
VALUE self)
7223 if (mul <= 0)
return INT2FIX(0);
7225 return rb_int_mul(rb_ary_length(self), n);
7262rb_ary_cycle(
int argc,
VALUE *argv,
VALUE ary)
7269 if (argc == 0 ||
NIL_P(argv[0])) {
7274 if (n <= 0)
return Qnil;
7277 while (
RARRAY_LEN(ary) > 0 && (n < 0 || 0 < n--)) {
7296 for (i = 0; i < r; i++) ARY_SET(result, i,
RARRAY_AREF(values, p[i]));
7297 ARY_SET_LEN(result, r);
7299 return !
RBASIC(values)->klass;
7320 const char *
const unused = memchr(&used[i], 0, n-i);
7335 for (i = 0; i < n; ++i) {
7336 if (used[i])
continue;
7338 if (!yield_indexed_values(values, r, p)) {
7359 while (--how_many > 0) {
7361 cnt = rb_int_mul(cnt,
LEN2NUM(v));
7375 if (comb > size-comb) {
7381 else if (comb == 0) {
7385 for (i = 1; i < comb; ++i) {
7386 r = rb_int_mul(r,
LEN2NUM(size - i));
7387 r = rb_int_idiv(r,
LEN2NUM(i + 1));
7398 return descending_factorial(n, k);
7444rb_ary_permutation(
int argc,
VALUE *argv,
VALUE ary)
7458 if (r < 0 || n < r) {
7472 char *used = (
char*)(p + r);
7473 VALUE ary0 = ary_make_hidden_shared_copy(ary);
7477 permute0(n, r, p, used, ary0);
7492 for (lev++; lev < n; lev++) {
7493 stack[lev+1] = stack[lev]+1;
7495 if (!yield_indexed_values(values, n, stack+1)) {
7499 if (lev == 0)
return;
7501 }
while (stack[lev+1]+n ==
len+lev+1);
7511 return binomial_coefficient(k, n);
7566 if (n < 0 ||
len < n) {
7578 VALUE ary0 = ary_make_hidden_shared_copy(ary);
7582 combinate0(
len, n, stack, ary0);
7608 if (++index < r-1) {
7612 for (i = 0; i < n; ++i) {
7614 if (!yield_indexed_values(values, r, p)) {
7619 if (index <= 0)
return;
7620 }
while ((i = ++p[--index]) >= n);
7678rb_ary_repeated_permutation(
VALUE ary,
VALUE num)
7698 VALUE ary0 = ary_make_hidden_shared_copy(ary);
7700 rpermute0(n, r, p, ary0);
7714 if (++index < r-1) {
7718 for (; i < n; ++i) {
7720 if (!yield_indexed_values(values, r, p)) {
7725 if (index <= 0)
return;
7726 }
while ((i = ++p[--index]) >= n);
7738 return binomial_coefficient(k, n + k - 1);
7781rb_ary_repeated_combination(
VALUE ary,
VALUE num)
7799 else if (
len == 0) {
7805 VALUE ary0 = ary_make_hidden_shared_copy(ary);
7807 rcombinate0(
len, n, p, n, ary0);
7868rb_ary_product(
int argc,
VALUE *argv,
VALUE ary)
7882 for (i = 1; i < n; i++) arrays[i] =
Qnil;
7883 for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
7886 for (i = 0; i < n; i++) counters[i] = 0;
7891 for (i = 0; i < n; i++) {
7893 arrays[i] = ary_make_shared_copy(arrays[i]);
7898 for (i = 0; i < n; i++) {
7914 for (j = 0; j < n; j++) {
7919 if (
NIL_P(result)) {
7920 FL_SET(t0, RARRAY_SHARED_ROOT_FLAG);
7922 if (!
FL_TEST(t0, RARRAY_SHARED_ROOT_FLAG)) {
7926 FL_UNSET(t0, RARRAY_SHARED_ROOT_FLAG);
7939 while (counters[m] ==
RARRAY_LEN(arrays[m])) {
7942 if (--m < 0)
goto done;
7950 return NIL_P(result) ? ary : result;
7976 rb_raise(rb_eArgError,
"attempt to take negative size");
8003rb_ary_take_while(
VALUE ary)
8011 return rb_ary_take(ary,
LEN2NUM(i));
8039 rb_raise(rb_eArgError,
"attempt to drop negative size");
8066rb_ary_drop_while(
VALUE ary)
8074 return rb_ary_drop(ary,
LEN2NUM(i));
8117rb_ary_any_p(
int argc,
VALUE *argv,
VALUE ary)
8125 rb_warn(
"given block not used");
8132 for (i = 0; i <
len; ++i) {
8184rb_ary_all_p(
int argc,
VALUE *argv,
VALUE ary)
8192 rb_warn(
"given block not used");
8199 for (i = 0; i <
len; ++i) {
8245rb_ary_none_p(
int argc,
VALUE *argv,
VALUE ary)
8253 rb_warn(
"given block not used");
8260 for (i = 0; i <
len; ++i) {
8309rb_ary_one_p(
int argc,
VALUE *argv,
VALUE ary)
8318 rb_warn(
"given block not used");
8322 if (result)
return Qfalse;
8328 for (i = 0; i <
len; ++i) {
8330 if (result)
return Qfalse;
8338 if (result)
return Qfalse;
8370 self = rb_ary_at(self, *argv);
8371 if (!--argc)
return self;
8373 return rb_obj_dig(argc, argv, self,
Qnil);
8377finish_exact_sum(
long n,
VALUE r,
VALUE v,
int z)
8382 v = rb_rational_plus(r, v);
8450 if (init_is_float) {
8455 goto init_is_a_value;
8469 else if (RB_BIGNUM_TYPE_P(e))
8470 v = rb_big_plus(e, v);
8475 r = rb_rational_plus(r, e);
8480 v = finish_exact_sum(n, r, v, argc!=0);
8481 if (init_is_float) v = rb_float_plus(argv[0], v);
8485 v = finish_exact_sum(n, r, v, i!=0);
8488 v = rb_float_plus(argv[0], v);
8489 goto has_some_value;
8502 if (init_is_float) {
8507 goto has_float_value;
8517 else if (RB_BIGNUM_TYPE_P(e))
8524 if (isnan(f))
continue;
8530 if (isinf(f) && signbit(x) != signbit(f))
8536 if (isinf(f))
continue;
8539 if (fabs(f) >= fabs(x))
8552 goto has_some_value;
8566rb_ary_deconstruct(
VALUE ary)
9077 fake_ary_flags = init_fake_ary_flags();
9206 rb_vm_register_global_object(rb_cArray_empty_frozen);
9209#include "array.rbinc"
ruby_coderange_type
What rb_enc_str_coderange() returns.
#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.
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.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
#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 ENC_CODERANGE_AND(a, b)
Old name of RB_ENC_CODERANGE_AND.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define LEN2NUM
Old name of RB_LEN2NUM.
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define CLASS_OF
Old name of rb_class_of.
#define FIX2LEN
Old name of RB_FIX2LEN.
#define rb_ary_new4
Old name of rb_ary_new_from_values.
#define FIXABLE
Old name of RB_FIXABLE.
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
#define LONG2FIX
Old name of RB_INT2FIX.
#define ASSUME
Old name of RBIMPL_ASSUME.
#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 DBL2NUM
Old name of rb_float_new.
#define FL_TEST
Old name of RB_FL_TEST.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define LEN2FIX
Old name of RB_LEN2FIX.
#define ENC_CODERANGE_CLEAR(obj)
Old name of RB_ENC_CODERANGE_CLEAR.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2LEN
Old name of RB_NUM2LEN.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define ENC_CODERANGE_SET(obj, cr)
Old name of RB_ENC_CODERANGE_SET.
#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_cObject
Object 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)
Same as RB_OBJ_FROZEN(), but returns Qtrue/Qfalse instead of bool.
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_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)
Same as RB_OBJ_FREEZE(), but returns the given object.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#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.
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
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_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it only takes public methods into account.
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
VALUE rb_ary_rotate(VALUE ary, rb_len_t rot)
Destructively rotates the passed array in-place to towards its end.
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_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_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.
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.
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_ary_resize(VALUE ary, rb_len_t len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_plus(VALUE lhs, VALUE rhs)
Creates a new array, concatenating the former to the latter.
VALUE rb_ary_delete_at(VALUE ary, rb_len_t pos)
Destructively removes an element which resides at the specific index of the passed 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_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_subseq(VALUE ary, rb_len_t beg, rb_len_t len)
Obtains a part of the passed array.
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.
void rb_mem_clear(VALUE *buf, rb_len_t len)
Fills the memory region with a series of RUBY_Qnil.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, rb_len_t len)
Destructively appends multiple elements at the end of the 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.
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...
VALUE rb_get_values_at(VALUE obj, rb_len_t olen, int argc, const VALUE *argv, VALUE(*func)(VALUE obj, rb_len_t oidx))
This was a generalisation of Array#values_at, Struct#values_at, and MatchData#values_at.
VALUE rb_ary_new_from_values(rb_len_t n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
#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_output_fs
The field separator character for outputs, or the $,.
unsigned long rb_random_ulong_limited(VALUE rnd, unsigned long limit)
Identical to rb_genrand_ulong_limited(), except it generates using the passed RNG.
VALUE rb_range_beg_len(VALUE range, rb_len_t *begp, rb_len_t *lenp, rb_len_t len, int err)
Deconstructs a numerical range.
size_t rb_set_size(VALUE set)
Returns the number of elements in the set.
VALUE rb_set_clear(VALUE set)
Removes all entries from set.
bool rb_set_delete(VALUE set, VALUE element)
Removes the element from from set.
bool rb_set_add(VALUE set, VALUE element)
Adds element to set.
void rb_set_foreach(VALUE set, int(*func)(VALUE element, VALUE arg), VALUE arg)
Iterates over a set.
bool rb_set_lookup(VALUE set, VALUE element)
Whether the set contains the given element.
VALUE rb_set_new_capa(size_t capa)
Identical to rb_set_new(), except it additionally specifies how many elements it is expected to conta...
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
#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.
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
#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.
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_buf_new(rb_len_t capa)
Allocates a "string buffer".
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
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.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
int capa
Designed capacity of the buffer.
int len
Length of the buffer.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
int ruby_thread_has_gvl_p(void)
Whether the current thread is holding the GVL.
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.
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.
#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.
@ RARRAY_EMBED_LEN_SHIFT
Where RARRAY_EMBED_LEN_MASK resides.
#define RARRAY_AREF(a, i)
References an object in an array.
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
#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 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.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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::@045262372243116021320040343026053377105164277244::@107044055235071215233122207133305327077254253150 heap
Arrays that use separated memory region for elements use this pattern.
rb_len_t capa
Capacity of *ptr.
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::@045262372243116021320040343026053377105164277244::@107044055235071215233122207133305327077254253150::@305130333254331304137150213113061105025176332136 aux
Auxiliary info.
union RArray::@045262372243116021320040343026053377105164277244 as
Array's specific fields.
rb_len_t 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.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
#define RB_LEN_MAX
Maximum possible value that a rb_len_t can take.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
#define RB_LEN_MIN
Minimum possible value that a rb_len_t can take.
unsigned long rb_ulen_t
An unsigned integer type that has the same width with rb_len_t.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
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.