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/string.h"
27#include "internal/vm.h"
43#include "ruby_assert.h"
46VALUE rb_cArray_empty_frozen;
79#define ARY_DEFAULT_SIZE 16
80#define ARY_MAX_SIZE (LONG_MAX / (int)sizeof(VALUE))
81#define SMALL_ARRAY_LEN 16
85should_be_T_ARRAY(
VALUE ary)
90#define ARY_HEAP_PTR(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
91#define ARY_HEAP_LEN(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
92#define ARY_HEAP_CAPA(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(!ARY_SHARED_ROOT_P(a)), \
93 RARRAY(a)->as.heap.aux.capa)
95#define ARY_EMBED_PTR(a) (RUBY_ASSERT(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
96#define ARY_EMBED_LEN(a) \
97 (RUBY_ASSERT(ARY_EMBED_P(a)), \
98 (long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
99 (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
100#define ARY_HEAP_SIZE(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE))
102#define ARY_OWNS_HEAP_P(a) (RUBY_ASSERT(should_be_T_ARRAY((VALUE)(a))), \
103 !FL_TEST_RAW((a), RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG))
105#define FL_SET_EMBED(a) do { \
106 RUBY_ASSERT(!ARY_SHARED_P(a)); \
107 FL_SET((a), RARRAY_EMBED_FLAG); \
111#define FL_UNSET_EMBED(ary) FL_UNSET((ary), RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK)
112#define FL_SET_SHARED(ary) do { \
113 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
114 FL_SET((ary), RARRAY_SHARED_FLAG); \
116#define FL_UNSET_SHARED(ary) FL_UNSET((ary), RARRAY_SHARED_FLAG)
118#define ARY_SET_PTR_FORCE(ary, p) \
119 (RARRAY(ary)->as.heap.ptr = (p))
120#define ARY_SET_PTR(ary, p) do { \
121 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
122 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
123 ARY_SET_PTR_FORCE(ary, p); \
125#define ARY_SET_EMBED_LEN(ary, n) do { \
127 RUBY_ASSERT(ARY_EMBED_P(ary)); \
128 RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
129 RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
131#define ARY_SET_HEAP_LEN(ary, n) do { \
132 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
133 RARRAY(ary)->as.heap.len = (n); \
135#define ARY_SET_LEN(ary, n) do { \
136 if (ARY_EMBED_P(ary)) { \
137 ARY_SET_EMBED_LEN((ary), (n)); \
140 ARY_SET_HEAP_LEN((ary), (n)); \
142 RUBY_ASSERT(RARRAY_LEN(ary) == (n)); \
144#define ARY_INCREASE_PTR(ary, n) do { \
145 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
146 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
147 RARRAY(ary)->as.heap.ptr += (n); \
149#define ARY_INCREASE_LEN(ary, n) do { \
150 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
151 if (ARY_EMBED_P(ary)) { \
152 ARY_SET_EMBED_LEN((ary), RARRAY_LEN(ary)+(n)); \
155 RARRAY(ary)->as.heap.len += (n); \
159#define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? ary_embed_capa(ary) : \
160 ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : ARY_HEAP_CAPA(ary))
161#define ARY_SET_CAPA_FORCE(ary, n) \
162 RARRAY(ary)->as.heap.aux.capa = (n);
163#define ARY_SET_CAPA(ary, n) do { \
164 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
165 RUBY_ASSERT(!ARY_SHARED_P(ary)); \
166 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
167 ARY_SET_CAPA_FORCE(ary, n); \
170#define ARY_SHARED_ROOT_OCCUPIED(ary) (!OBJ_FROZEN(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1)
171#define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \
172 RUBY_ASSERT(ARY_SHARED_ROOT_P(ary)); \
173 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
174 RUBY_ASSERT((value) >= 0); \
175 RARRAY(ary)->as.heap.aux.capa = (value); \
177#define FL_SET_SHARED_ROOT(ary) do { \
178 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
179 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
180 FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \
194ary_embed_capa(
VALUE ary)
196 size_t size = rb_obj_shape_slot_size(ary) - offsetof(
struct RArray, as.
ary);
198 return size /
sizeof(
VALUE);
202ary_embed_size(
long capa)
205 if (size <
sizeof(
struct RArray)) size =
sizeof(
struct RArray);
210ary_embeddable_p(
long capa)
214 return capa <= embed_len_max && rb_gc_size_allocatable_p(ary_embed_size(
capa));
218rb_ary_embeddable_p(
VALUE ary)
228 return !(ARY_SHARED_ROOT_P(ary) ||
OBJ_FROZEN(ary) || ARY_SHARED_P(ary));
235rb_ary_embedded_shared_root_p(
VALUE ary)
241rb_ary_size_as_embedded(
VALUE ary)
245 if (ARY_EMBED_P(ary)) {
246 real_size = ary_embed_size(ARY_EMBED_LEN(ary));
248 else if (rb_ary_embeddable_p(ary)) {
249 real_size = ary_embed_size(ARY_HEAP_CAPA(ary));
252 real_size =
sizeof(
struct RArray);
259#define ary_verify(ary) ary_verify_(ary, __FILE__, __LINE__)
262ary_verify_(
VALUE ary,
const char *file,
int line)
266 if (ARY_SHARED_P(
ary)) {
275 else if (ARY_EMBED_P(
ary)) {
284 for (i=0; i<
len; i++) {
293#define ary_verify(ary) ((void)0)
322ary_mem_clear(
VALUE ary,
long beg,
long size)
330memfill(
register VALUE *mem,
register long size,
register VALUE val)
341 memfill(
ptr + beg, size, val);
351 if (argc > (
int)(128/
sizeof(
VALUE)) ) {
352 rb_gc_writebarrier_remember(buff_owner_ary);
360 for (i=0; i<argc; i++) {
370 ary_memcpy0(
ary, beg, argc, argv,
ary);
374ary_heap_alloc_buffer(
size_t capa)
382 ruby_xfree_sized((
void *)
ptr, size);
388 ary_heap_free_ptr(
ary, ARY_HEAP_PTR(
ary), ARY_HEAP_SIZE(
ary));
392ary_heap_realloc(
VALUE ary,
size_t new_capa)
405 if (!ARY_EMBED_P(
ary)) {
406 const VALUE *buf = ARY_HEAP_PTR(
ary);
407 long len = ARY_HEAP_LEN(
ary);
408 long capa = ARY_HEAP_CAPA(
ary);
411 ARY_SET_EMBED_LEN(
ary,
len);
420ary_resize_capa(
VALUE ary,
long capacity)
426 if (capacity > ary_embed_capa(
ary)) {
427 size_t new_capa = capacity;
428 if (ARY_EMBED_P(
ary)) {
429 long len = ARY_EMBED_LEN(
ary);
430 VALUE *
ptr = ary_heap_alloc_buffer(capacity);
435 ARY_SET_HEAP_LEN(
ary,
len);
438 new_capa = ary_heap_realloc(
ary, capacity);
440 ARY_SET_CAPA(
ary, new_capa);
443 if (!ARY_EMBED_P(
ary)) {
444 long len = ARY_HEAP_LEN(
ary);
445 long old_capa = ARY_HEAP_CAPA(
ary);
448 if (
len > capacity)
len = capacity;
450 ary_heap_free_ptr(
ary,
ptr, old_capa *
sizeof(
VALUE));
463 long capacity = ARY_HEAP_LEN(
ary);
464 long old_capa = ARY_HEAP_CAPA(
ary);
467 if (old_capa > capacity) {
468 size_t new_capa = ary_heap_realloc(
ary, capacity);
469 ARY_SET_CAPA(
ary, new_capa);
478 long new_capa = ARY_CAPA(
ary) / 2;
480 if (new_capa < ARY_DEFAULT_SIZE) {
481 new_capa = ARY_DEFAULT_SIZE;
483 if (new_capa >= ARY_MAX_SIZE - min) {
484 new_capa = (ARY_MAX_SIZE - min) / 2;
487 ary_resize_capa(
ary, new_capa);
506 FL_UNSET_SHARED(
ary);
512 if (ARY_OWNS_HEAP_P(
ary)) {
515 else if (ARY_SHARED_P(
ary)) {
520 ARY_SET_EMBED_LEN(
ary, 0);
545 RB_DEBUG_COUNTER_INC(obj_ary_shared_create);
553 rb_check_frozen(
ary);
560 if (ARY_SHARED_P(
ary)) {
566 if (
len <= ary_embed_capa(
ary)) {
568 FL_UNSET_SHARED(
ary);
572 ARY_SET_EMBED_LEN(
ary,
len);
576 FL_UNSET_SHARED(
ary);
578 ARY_SET_CAPA(
ary, shared_len);
589 ARY_SET_CAPA_FORCE(
ary,
len);
590 ARY_SET_PTR_FORCE(
ary,
ptr);
593 rb_gc_writebarrier_remember(
ary);
601 rb_ary_modify_check(
ary);
602 rb_ary_cancel_sharing(
ary);
606ary_ensure_room_for_push(
VALUE ary,
long add_len)
609 long new_len = old_len + add_len;
612 if (old_len > ARY_MAX_SIZE - add_len) {
615 if (ARY_SHARED_P(
ary)) {
616 if (new_len > ary_embed_capa(
ary)) {
620 rb_ary_modify_check(
ary);
631 ary_double_capa(
ary, new_len);
642 rb_ary_modify_check(
ary);
645 if (new_len >
capa) {
646 ary_double_capa(
ary, new_len);
668 if (!ARY_EMBED_P(
ary) && !ARY_SHARED_P(
ary) && !ARY_SHARED_ROOT_P(
ary)) {
669 ary_shrink_capa(
ary);
685 if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) &&
686 !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) &&
687 ARY_SHARED_ROOT(ary1) == ARY_SHARED_ROOT(ary2) &&
688 ARY_HEAP_LEN(ary1) == ARY_HEAP_LEN(ary2)) {
697 size_t size = ary_embed_size(
capa);
703 return rb_newobj_of(klass,
T_ARRAY | RARRAY_EMBED_FLAG, size);
707ary_alloc_heap(
VALUE klass)
711 ary->as.heap.len = 0;
712 ary->as.heap.aux.capa = 0;
713 ary->as.heap.ptr = NULL;
719empty_ary_alloc(
VALUE klass)
721 RUBY_DTRACE_CREATE_HOOK(ARRAY, 0);
722 return ary_alloc_embed(klass, 0);
733 rb_raise(rb_eArgError,
"negative array size (or size too big)");
735 if (
capa > ARY_MAX_SIZE) {
736 rb_raise(rb_eArgError,
"array size too big");
739 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
741 if (ary_embeddable_p(
capa)) {
742 ary = ary_alloc_embed(klass,
capa);
745 ary = ary_alloc_heap(klass);
749 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
750 ARY_SET_HEAP_LEN(
ary, 0);
769(rb_ary_new_from_args)(
long n, ...)
778 for (i=0; i<n; i++) {
788rb_ary_tmp_new_from_values(
VALUE klass,
long n,
const VALUE *elts)
792 ary = ary_new(klass, n);
794 ary_memcpy(
ary, 0, n, elts);
804 return rb_ary_tmp_new_from_values(
rb_cArray, n, elts);
810 size_t size = ary_embed_size(
capa);
816 return rb_ec_newobj_of(ec, klass,
T_ARRAY | RARRAY_EMBED_FLAG, size);
835 rb_raise(rb_eArgError,
"negative array size (or size too big)");
837 if (
capa > ARY_MAX_SIZE) {
838 rb_raise(rb_eArgError,
"array size too big");
841 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
843 if (ary_embeddable_p(
capa)) {
844 ary = ec_ary_alloc_embed(ec, klass,
capa);
847 ary = ec_ary_alloc_heap(ec, klass);
851 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
852 ARY_SET_HEAP_LEN(
ary, 0);
865 ary_memcpy(
ary, 0, n, elts);
880rb_ary_hidden_new_fill(
long capa)
891 if (ARY_OWNS_HEAP_P(
ary)) {
892 if (USE_DEBUG_COUNTER &&
893 !ARY_SHARED_ROOT_P(
ary) &&
895 RB_DEBUG_COUNTER_INC(obj_ary_extracapa);
898 RB_DEBUG_COUNTER_INC(obj_ary_ptr);
902 RB_DEBUG_COUNTER_INC(obj_ary_embed);
905 if (ARY_SHARED_P(
ary)) {
906 RB_DEBUG_COUNTER_INC(obj_ary_shared);
908 if (ARY_SHARED_ROOT_P(
ary) && ARY_SHARED_ROOT_OCCUPIED(
ary)) {
909 RB_DEBUG_COUNTER_INC(obj_ary_shared_root_occupied);
913static VALUE fake_ary_flags;
916init_fake_ary_flags(
void)
918 struct RArray fake_ary = {0};
921 RBASIC_SET_FULL_SHAPE_ID(
ary, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER);
927rb_setup_fake_ary(
struct RArray *fake_ary,
const VALUE *list,
long len)
930 RBASIC_CLEAR_CLASS((
VALUE)fake_ary);
936 return (
VALUE)fake_ary;
942 if (ARY_OWNS_HEAP_P(
ary)) {
943 return ARY_CAPA(
ary) *
sizeof(
VALUE);
955 if (ARY_SHARED_P(
ary)) {
956 return ARY_SHARED_ROOT(
ary);
958 else if (ARY_SHARED_ROOT_P(
ary)) {
970 VALUE shared = ary_alloc_heap(0);
971 FL_SET_SHARED_ROOT(shared);
973 if (ARY_EMBED_P(
ary)) {
975 ARY_SET_PTR(shared,
ptr);
979 ARY_SET_HEAP_LEN(
ary,
len);
986 ARY_SET_LEN(shared,
capa);
988 rb_ary_set_shared(
ary, shared);
1002 if (ary_embeddable_p(
len)) {
1007 ARY_SET_EMBED_LEN(subst,
len);
1011 return rb_ary_increment_share(ary_make_shared(
ary));
1024 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1026#define to_ary rb_to_array_type
1031 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1037 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1043 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1072rb_ary_s_new(
int argc,
VALUE *argv,
VALUE klass)
1078 if (argc > 0 &&
FIXNUM_P(argv[0])) {
1080 if (size < 0) size = 0;
1083 ary = ary_new(klass, size);
1162 if (argc == 1 && !
FIXNUM_P(size)) {
1173 rb_raise(rb_eArgError,
"negative array size");
1175 if (
len > ARY_MAX_SIZE) {
1176 rb_raise(rb_eArgError,
"array size too big");
1180 ary_resize_capa(
ary,
len);
1185 rb_warn(
"block supersedes default value argument");
1187 for (i=0; i<
len; i++) {
1189 ARY_SET_LEN(
ary, i + 1);
1193 ary_memfill(
ary, 0,
len, val);
1210rb_ary_s_create(
int argc,
VALUE *argv,
VALUE klass)
1213 if (argc > 0 && argv) {
1214 ary_memcpy(
ary, 0, argc, argv);
1215 ARY_SET_LEN(
ary, argc);
1229 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
1233 else if (idx >= ARY_MAX_SIZE) {
1238 if (idx >= ARY_CAPA(
ary)) {
1239 ary_double_capa(
ary, idx);
1246 ARY_SET_LEN(
ary, idx + 1);
1248 ARY_SET(
ary, idx, val);
1258 VALUE result = ary_alloc_heap(klass);
1259 size_t embed_capa = ary_embed_capa(result);
1260 if ((
size_t)
len <= embed_capa) {
1261 FL_SET_EMBED(result);
1263 ARY_SET_EMBED_LEN(result,
len);
1266 VALUE shared = ary_make_shared(
ary);
1271 FL_UNSET_EMBED(result);
1275 rb_ary_set_shared(result, shared);
1277 ARY_INCREASE_PTR(result, offset);
1278 ARY_SET_LEN(result,
len);
1288ary_make_partial_step(
VALUE ary,
VALUE klass,
long offset,
long len,
long step)
1295 const long orig_len =
len;
1297 if (step > 0 && step >=
len) {
1298 VALUE result = ary_new(klass, 1);
1303 ARY_SET_EMBED_LEN(result, 1);
1306 else if (step < 0 && step < -
len) {
1310 long ustep = (step < 0) ? -step : step;
1311 len = roomof(
len, ustep);
1314 long j = offset + ((step > 0) ? 0 : (orig_len - 1));
1316 VALUE result = ary_new(klass,
len);
1317 if (ARY_EMBED_P(result)) {
1321 for (i = 0; i <
len; ++i) {
1325 ARY_SET_EMBED_LEN(result,
len);
1331 for (i = 0; i <
len; ++i) {
1336 ARY_SET_LEN(result,
len);
1348enum ary_take_pos_flags
1355ary_take_first_or_last_n(
VALUE ary,
long n,
enum ary_take_pos_flags last)
1364 rb_raise(rb_eArgError,
"negative array size");
1373ary_take_first_or_last(
int argc,
const VALUE *argv,
VALUE ary,
enum ary_take_pos_flags last)
1380 return ary_take_first_or_last_n(
ary,
NUM2LONG(argv[0]), last);
1402 VALUE target_ary = ary_ensure_room_for_push(
ary, 1);
1406 ARY_SET_LEN(
ary, idx + 1);
1415 VALUE target_ary = ary_ensure_room_for_push(
ary,
len);
1416 ary_memcpy0(
ary, oldlen,
len, argv, target_ary);
1417 ARY_SET_LEN(
ary, oldlen +
len);
1449 rb_ary_modify_check(
ary);
1451 if (n == 0)
return Qnil;
1452 if (ARY_OWNS_HEAP_P(
ary) &&
1453 n * 3 < ARY_CAPA(
ary) &&
1454 ARY_CAPA(
ary) > ARY_DEFAULT_SIZE)
1456 ary_resize_capa(
ary, n * 2);
1461 ARY_SET_LEN(
ary, n - 1);
1505 rb_ary_modify_check(
ary);
1506 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
1519 rb_ary_modify_check(
ary);
1525 rb_ary_behead(
ary, 1);
1578 rb_ary_modify_check(
ary);
1579 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1581 rb_ary_behead(
ary,n);
1593 rb_ary_modify_check(
ary);
1595 if (!ARY_SHARED_P(
ary)) {
1600 ARY_INCREASE_LEN(
ary, -n);
1605 ary_mem_clear(
ary, 0, n);
1606 ary_make_shared(
ary);
1608 else if (ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(
ary))) {
1609 ary_mem_clear(
ary, 0, n);
1612 ARY_INCREASE_PTR(
ary, n);
1613 ARY_INCREASE_LEN(
ary, -n);
1622 if (head - sharedp < argc) {
1623 long room =
capa -
len - argc;
1627 head = sharedp + argc + room;
1629 ARY_SET_PTR(
ary, head - argc);
1633 return ARY_SHARED_ROOT(
ary);
1637ary_modify_for_unshift(
VALUE ary,
int argc)
1640 long new_len =
len + argc;
1642 const VALUE *head, *sharedp;
1646 if (
capa - (
capa >> 6) <= new_len) {
1647 ary_double_capa(
ary, new_len);
1651 if (new_len > ARY_DEFAULT_SIZE * 4 && !ARY_EMBED_P(
ary)) {
1656 ary_make_shared(
ary);
1659 return make_room_for_unshift(
ary, head, (
void *)sharedp, argc,
capa,
len);
1673ary_ensure_room_for_unshift(
VALUE ary,
int argc)
1676 long new_len =
len + argc;
1678 if (
len > ARY_MAX_SIZE - argc) {
1681 else if (! ARY_SHARED_P(
ary)) {
1682 return ary_modify_for_unshift(
ary, argc);
1689 return ary_modify_for_unshift(
ary, argc);
1691 else if (new_len >
capa) {
1692 return ary_modify_for_unshift(
ary, argc);
1698 rb_ary_modify_check(
ary);
1699 return make_room_for_unshift(
ary, head, sharedp, argc,
capa,
len);
1725 rb_ary_modify_check(
ary);
1729 target_ary = ary_ensure_room_for_unshift(
ary, argc);
1730 ary_memcpy0(
ary, 0, argc, argv, target_ary);
1731 ARY_SET_LEN(
ary,
len + argc);
1738 return rb_ary_unshift_m(1, &item,
ary);
1747 if (offset < 0 ||
len <= offset) {
1756 return rb_ary_entry_internal(
ary, offset);
1764 if (beg > alen)
return -1;
1765 if (beg < 0 ||
len < 0)
return -1;
1767 if (alen <
len || alen < beg +
len) {
1780 if (
len == 0)
return ary_new(klass, 0);
1781 return ary_make_partial(
ary, klass, beg,
len);
1909 return rb_ary_aref2(
ary, argv[0], argv[1]);
1911 return rb_ary_aref1(
ary, argv[0]);
1928 long beg,
len, step;
1936 switch (rb_arithmetic_sequence_beg_len_step(arg, &beg, &
len, &step,
RARRAY_LEN(
ary), 0)) {
1942 if (step == 0) rb_raise(rb_eArgError,
"slice step cannot be zero");
1944 if (
len == 0)
return ary_new(klass, 0);
1945 if (step == 1)
return ary_make_partial(
ary, klass, beg,
len);
1946 return ary_make_partial_step(
ary, klass, beg,
len, step);
1991 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1997ary_first(
VALUE self)
2013 return ary_last(
ary);
2016 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
2069 if (block_given && argc == 2) {
2070 rb_warn(
"block supersedes default value argument");
2078 if (block_given)
return rb_yield(pos);
2080 rb_raise(
rb_eIndexError,
"index %ld outside of array bounds: %ld...%ld",
2124 if (!
NIL_P(if_none)) {
2168 idx = (idx >=
len) ?
len : idx;
2171 if (!
NIL_P(if_none)) {
2229 rb_warn(
"given block not used");
2287 rb_warn(
"given block not used");
2305 if (!
NIL_P(tmp))
return tmp;
2310ary_splice(
VALUE ary,
long beg,
long len,
const VALUE *rptr,
long rlen,
int self_insert)
2319 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2323 if (olen <
len || olen < beg +
len) {
2329 if (beg > ARY_MAX_SIZE - rlen) {
2332 target_ary = ary_ensure_room_for_push(
ary, rlen-
len);
2334 ary_mem_clear(
ary, olen, beg - olen);
2338 ary_memcpy0(
ary, beg, rlen, rptr, target_ary);
2345 if (olen -
len > ARY_MAX_SIZE - rlen) {
2349 alen = olen + rlen -
len;
2350 if (alen >= ARY_CAPA(
ary)) {
2351 ary_double_capa(
ary, alen);
2358 ARY_SET_LEN(
ary, alen);
2362 rb_gc_writebarrier_remember(
ary);
2391 rb_ary_modify_check(
ary);
2392 if (ARY_SHARED_P(
ary)) {
2396 rb_bug(
"probable buffer overflow: %ld for %ld",
len,
capa);
2402rb_ary_modify_expand(
VALUE ary,
long expand)
2407 rb_raise(rb_eArgError,
"negative expanding array size");
2409 if (expand >= ARY_MAX_SIZE -
len) {
2410 rb_raise(rb_eArgError,
" size too big");
2412 rb_ary_modify_check(
ary);
2413 if (
len + expand > ARY_CAPA(
ary)) {
2414 ary_resize_capa(
ary,
len + expand);
2426 if (
len == olen)
return ary;
2427 if (
len > ARY_MAX_SIZE) {
2431 if (
len > ARY_CAPA(
ary)) {
2432 ary_double_capa(
ary,
len);
2434 ary_mem_clear(
ary, olen,
len - olen);
2437 else if (ARY_EMBED_P(
ary)) {
2438 ARY_SET_EMBED_LEN(
ary,
len);
2440 else if (
len <= ary_embed_capa(
ary)) {
2442 long ptr_capa = ARY_HEAP_SIZE(
ary);
2443 bool is_malloc_ptr = !ARY_SHARED_P(
ary);
2448 ARY_SET_EMBED_LEN(
ary,
len);
2450 if (is_malloc_ptr) ruby_xfree_sized((
void *)
ptr, ptr_capa);
2453 if (olen >
len + ARY_DEFAULT_SIZE) {
2454 size_t new_capa = ary_heap_realloc(
ary,
len);
2455 ARY_SET_CAPA(
ary, new_capa);
2457 ARY_SET_HEAP_LEN(
ary,
len);
2624 long offset, beg,
len;
2627 rb_ary_modify_check(
ary);
2631 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[2]);
2635 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2639 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[1]);
2643 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2688 rb_ary_modify_check(
ary);
2690 if (argc == 1)
return ary;
2697 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2702 ary_splice(
ary, pos, 0, argv + 1, argc - 1, FALSE);
2712 return rb_ary_length(
ary);
2926 ARY_SET_LEN(dup,
len);
2941rb_zjit_array_new_can_fastpath(
long len,
size_t *alloc_size_out,
VALUE *flags_out)
2943 if (!ary_embeddable_p(
len)) {
2946 long embed_size = ary_embed_size(
len);
2948 *alloc_size_out = embed_size;
2954rb_zjit_array_dup_can_fastpath(
VALUE ary,
size_t *alloc_size_out,
VALUE *flags_out,
long *len_out)
2957 if (!rb_zjit_array_new_can_fastpath(
len, alloc_size_out, flags_out)) {
2972recursive_join(
VALUE obj,
VALUE argp,
int recur)
2977 VALUE result = arg[2];
2978 int *first = (
int *)arg[3];
2981 rb_raise(rb_eArgError,
"recursive array join");
2984 ary_join_1(obj,
ary, sep, 0, result, first);
2996 for (i=0; i<max; i++) {
2999 if (i > 0 && !
NIL_P(sep))
3007ary_join_1_str(
VALUE dst,
VALUE src,
int *first)
3011 rb_enc_copy(dst, src);
3020 rb_raise(rb_eArgError,
"recursive array join");
3029 args[3] = (
VALUE)first;
3040 if (i > 0 && !
NIL_P(sep))
3045 ary_join_1_str(result, val, first);
3048 ary_join_1_ary(val,
ary, sep, result, val, first);
3051 ary_join_1_str(result, tmp, first);
3054 ary_join_1_ary(val,
ary, sep, result, tmp, first);
3072 if (n == 0)
return Qundef;
3077 if (!rb_str_encindex_fastpath(encidx))
return Qundef;
3082 const char *sep_ptr = NULL;
3084 int sep_cr = rb_enc_str_coderange(sep);
3088 sep_ptr = RSTRING_PTR(sep);
3089 sep_len = RSTRING_LEN(sep);
3094 long len = 1 + sep_len * (n - 1);
3095 for (
long i = 0; i < n; i++) {
3098 len += RSTRING_LEN(s);
3103 rb_enc_associate_index(result, encidx);
3104 char *
const buf = RSTRING_PTR(result);
3106 for (
long i = 0; i < n; i++) {
3108 long slen = RSTRING_LEN(s);
3109 if (i > 0 && sep_len) {
3110 memcpy(p, sep_ptr, sep_len);
3113 memcpy(p, RSTRING_PTR(s), slen);
3127 VALUE val, tmp, result;
3133 result = ary_join_fast(
ary, sep);
3134 if (!UNDEF_P(result))
return result;
3140 for (i=0; i < len_memo; i++) {
3144 if (
NIL_P(tmp) || tmp != val) {
3149 rb_enc_associate(result, rb_usascii_encoding());
3150 i = ary_join_0(
ary, sep, i, result);
3152 ary_join_1(
ary,
ary, sep, i, result, &first);
3155 len += RSTRING_LEN(tmp);
3159 len += RSTRING_LEN(val);
3218 else rb_enc_copy(str, s);
3249 return rb_ary_inspect(
ary);
3313 const VALUE e = rb_ary_elt(
ary, i);
3314 const VALUE elt = block_given ? rb_yield_force_blockarg(e) : e;
3316 if (
NIL_P(key_value_pair)) {
3317 rb_raise(
rb_eTypeError,
"wrong element type %"PRIsVALUE
" at %ld (expected array)",
3321 rb_raise(rb_eArgError,
"wrong array length at %ld (expected 2, was %ld)",
3362 ary_reverse(p1, p2);
3408 do *p2-- = *p1++;
while (--
len > 0);
3409 rb_gc_writebarrier_remember(dup);
3416rotate_count(
long cnt,
long len)
3418 return (cnt < 0) ? (
len - (~cnt %
len) - 1) : (cnt %
len);
3429 else if (cnt ==
len - 1) {
3437 if (--cnt > 0) ary_reverse(
ptr,
ptr + cnt);
3449 if (
len > 1 && (cnt = rotate_count(cnt,
len)) > 0) {
3541 cnt = rotate_count(cnt,
len);
3544 ary_memcpy(rotated, 0,
len,
ptr + cnt);
3545 ary_memcpy(rotated,
len, cnt,
ptr);
3551struct ary_sort_data {
3557sort_reentered(
VALUE ary)
3559 if (
RBASIC(ary)->klass) {
3566sort_returned(
struct ary_sort_data *data)
3571 sort_reentered(data->ary);
3575sort_1(
const void *ap,
const void *bp,
void *dummy)
3577 struct ary_sort_data *data = dummy;
3578 VALUE retval = sort_reentered(data->ary);
3586 n = rb_cmpint(retval, a, b);
3587 sort_returned(data);
3592sort_2(
const void *ap,
const void *bp,
void *dummy)
3594 struct ary_sort_data *data = dummy;
3595 VALUE retval = sort_reentered(data->ary);
3600 if ((
long)a > (long)b)
return 1;
3601 if ((
long)a < (long)b)
return -1;
3604 if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(STRING)) {
3608 return rb_float_cmp(a, b);
3612 n = rb_cmpint(retval, a, b);
3613 sort_returned(data);
3634 VALUE tmp = ary_make_substitution(ary);
3635 struct ary_sort_data data;
3637 RBASIC_CLEAR_CLASS(tmp);
3639 data.receiver = ary;
3645 if (ARY_EMBED_P(tmp)) {
3646 if (ARY_SHARED_P(ary)) {
3647 rb_ary_unshare(ary);
3650 if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) {
3651 ary_resize_capa(ary, ARY_EMBED_LEN(tmp));
3653 ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp));
3654 ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
3657 if (!ARY_EMBED_P(ary) && ARY_HEAP_PTR(ary) == ARY_HEAP_PTR(tmp)) {
3658 FL_UNSET_SHARED(ary);
3663 if (ARY_EMBED_P(ary)) {
3664 FL_UNSET_EMBED(ary);
3666 else if (ARY_SHARED_P(ary)) {
3668 rb_ary_unshare(ary);
3673 ARY_SET_PTR(ary, ARY_HEAP_PTR(tmp));
3674 ARY_SET_HEAP_LEN(ary,
len);
3675 ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
3679 ARY_SET_EMBED_LEN(tmp, 0);
3747rb_ary_bsearch(
VALUE ary)
3749 VALUE index_result = rb_ary_bsearch_index(ary);
3754 return index_result;
3771rb_ary_bsearch_index(
VALUE ary)
3774 int smaller = 0, satisfied = 0;
3778 while (low < high) {
3779 mid = low + ((high - low) / 2);
3786 else if (v ==
Qtrue) {
3790 else if (!
RTEST(v)) {
3795 switch (rb_cmpint(
rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
3797 case 1: smaller = 0;
break;
3798 case -1: smaller = 1;
3803 " (must be numeric, true, false or nil)",
3813 if (!satisfied)
return Qnil;
3847rb_ary_sort_by_bang(
VALUE ary)
3854 sorted =
rb_block_call(ary, rb_intern(
"sort_by"), 0, 0, sort_by_i, 0);
3882rb_ary_collect(
VALUE ary)
3917rb_ary_collect_bang(
VALUE ary)
3933 long beg,
len, i, j;
3935 for (i=0; i<argc; i++) {
3942 long end = olen < beg+
len ? olen : beg+
len;
3943 for (j = beg; j < end; j++) {
3966 const long end = beg +
len;
4090rb_ary_values_at(
int argc,
VALUE *argv,
VALUE ary)
4094 for (i = 0; i < argc; ++i) {
4095 append_values_at_single(result, ary, olen, argv[i]);
4123rb_ary_select(
VALUE ary)
4138struct select_bang_arg {
4144select_bang_i(
VALUE a)
4146 volatile struct select_bang_arg *arg = (
void *)a;
4147 VALUE ary = arg->ary;
4150 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4158 return (i1 == i2) ?
Qnil : ary;
4162select_bang_ensure(
VALUE a)
4164 volatile struct select_bang_arg *arg = (
void *)a;
4165 VALUE ary = arg->ary;
4167 long i1 = arg->len[0], i2 = arg->len[1];
4169 if (i2 <
len && i2 < i1) {
4178 ARY_SET_LEN(ary, i2 + tail);
4206rb_ary_select_bang(
VALUE ary)
4208 struct select_bang_arg args;
4214 args.len[0] = args.len[1] = 0;
4235rb_ary_keep_if(
VALUE ary)
4238 rb_ary_select_bang(ary);
4243ary_resize_smaller(
VALUE ary,
long len)
4247 ARY_SET_LEN(ary,
len);
4248 if (
len * 2 < ARY_CAPA(ary) &&
4249 ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
4250 ary_resize_capa(ary,
len * 2);
4298 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4317 ary_resize_smaller(ary, i2);
4328 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4343 ary_resize_smaller(ary, i2);
4355 if (pos < 0)
return Qnil;
4363 ARY_INCREASE_LEN(ary, -1);
4403ary_slice_bang_by_rb_ary_splice(
VALUE ary,
long pos,
long len)
4410 else if (pos < -orig_len) {
4416 else if (orig_len < pos) {
4419 if (orig_len < pos +
len) {
4420 len = orig_len - pos;
4427 ary_splice(ary, pos,
len, 0, 0, FALSE);
4525rb_ary_slice_bang(
int argc,
VALUE *argv,
VALUE ary)
4530 rb_ary_modify_check(ary);
4537 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4544 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4573reject_bang_i(
VALUE a)
4575 volatile struct select_bang_arg *arg = (
void *)a;
4576 VALUE ary = arg->ary;
4579 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4587 return (i1 == i2) ?
Qnil : ary;
4591ary_reject_bang(
VALUE ary)
4593 struct select_bang_arg args;
4594 rb_ary_modify_check(ary);
4596 args.len[0] = args.len[1] = 0;
4621rb_ary_reject_bang(
VALUE ary)
4625 return ary_reject_bang(ary);
4646rb_ary_reject(
VALUE ary)
4652 ary_reject(ary, rejected_ary);
4653 return rejected_ary;
4674rb_ary_delete_if(
VALUE ary)
4678 ary_reject_bang(ary);
4693take_items(
VALUE obj,
long n)
4698 if (n == 0)
return result;
4701 args[0] = result; args[1] = (
VALUE)n;
4702 if (UNDEF_P(rb_check_block_call(obj, idEach, 0, 0, take_i, (
VALUE)args)))
4703 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
4809 for (i=0; i<argc; i++) {
4810 argv[i] = take_items(argv[i],
len);
4814 int arity = rb_block_arity();
4823 for (j=0; j<argc; j++) {
4824 tmp[j+1] = rb_ary_elt(argv[j], i);
4836 for (j=0; j<argc; j++) {
4846 for (i=0; i<
len; i++) {
4850 for (j=0; j<argc; j++) {
4876rb_ary_transpose(
VALUE ary)
4878 long elen = -1, alen, i, j;
4879 VALUE tmp, result = 0;
4883 for (i=0; i<alen; i++) {
4884 tmp = to_ary(rb_ary_elt(ary, i));
4888 for (j=0; j<elen; j++) {
4893 rb_raise(
rb_eIndexError,
"element size differs (%ld should be %ld)",
4896 for (j=0; j<elen; j++) {
4897 rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
4921 rb_ary_modify_check(copy);
4922 orig = to_ary(orig);
4923 if (copy == orig)
return copy;
4928 if (
RARRAY_LEN(orig) <= ary_embed_capa(copy)) {
4935 else if (ARY_EMBED_P(orig)) {
4936 long len = ARY_EMBED_LEN(orig);
4937 VALUE *ptr = ary_heap_alloc_buffer(
len);
4939 FL_UNSET_EMBED(copy);
4940 ARY_SET_PTR(copy, ptr);
4941 ARY_SET_LEN(copy,
len);
4942 ARY_SET_CAPA(copy,
len);
4951 VALUE shared_root = ary_make_shared(orig);
4952 FL_UNSET_EMBED(copy);
4953 ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
4954 ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
4955 rb_ary_set_shared(copy, shared_root);
4978 rb_ary_modify_check(ary);
4979 if (ARY_SHARED_P(ary)) {
4980 rb_ary_unshare(ary);
4982 ARY_SET_EMBED_LEN(ary, 0);
4985 ARY_SET_LEN(ary, 0);
4986 if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
4987 ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
5178 long beg = 0, end = 0,
len = 0;
5201 if (beg < 0) beg = 0;
5210 if (beg >= ARY_MAX_SIZE ||
len > ARY_MAX_SIZE - beg) {
5211 rb_raise(rb_eArgError,
"argument too big");
5215 if (end >= ARY_CAPA(ary)) {
5216 ary_resize_capa(ary, end);
5219 ARY_SET_LEN(ary, end);
5222 if (UNDEF_P(item)) {
5226 for (i=beg; i<end; i++) {
5233 ary_memfill(ary, beg,
len, item);
5255 long len, xlen, ylen;
5265 ARY_SET_LEN(z,
len);
5292rb_ary_concat_multi(
int argc,
VALUE *argv,
VALUE ary)
5294 rb_ary_modify_check(ary);
5299 else if (argc > 1) {
5302 for (i = 0; i < argc; i++) {
5305 ary_append(ary, args);
5315 return ary_append(x, to_ary(y));
5354 rb_raise(rb_eArgError,
"negative argument");
5357 rb_raise(rb_eArgError,
"argument too big");
5362 ARY_SET_LEN(ary2,
len);
5367 ary_memcpy(ary2, 0, t, ptr);
5368 while (t <=
len/2) {
5445recursive_equal(
VALUE ary1,
VALUE ary2,
int recur)
5448 const VALUE *p1, *p2;
5450 if (recur)
return Qtrue;
5457 for (i = 0; i < len1; i++) {
5505 if (ary1 == ary2)
return Qtrue;
5518recursive_eql(
VALUE ary1,
VALUE ary2,
int recur)
5522 if (recur)
return Qtrue;
5524 if (!
rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
5552 if (ary1 == ary2)
return Qtrue;
5560ary_hash_values(
long len,
const VALUE *elements,
const VALUE ary)
5568 for (i=0; i<
len; i++) {
5569 n = rb_hash(elements[i]);
5581rb_ary_hash_values(
long len,
const VALUE *elements)
5583 return ary_hash_values(
len, elements, 0);
5602rb_ary_hash(
VALUE ary)
5653recursive_cmp(
VALUE ary1,
VALUE ary2,
int recur)
5657 if (recur)
return Qundef;
5662 for (i=0; i<
len; i++) {
5663 VALUE e1 = rb_ary_elt(ary1, i), e2 = rb_ary_elt(ary2, i);
5718 if (ary1 == ary2)
return INT2FIX(0);
5720 if (!UNDEF_P(v))
return v;
5734 rb_hash_add_new_element(hash, elt, elt);
5740ary_tmp_hash_new(
VALUE ary)
5743 VALUE hash = rb_hash_new_capa(size);
5745 RBASIC_CLEAR_CLASS(hash);
5750ary_make_hash(
VALUE ary)
5752 VALUE hash = ary_tmp_hash_new(ary);
5753 return ary_add_hash(hash, ary);
5763 rb_hash_add_new_element(hash, k, v);
5769ary_make_hash_by(
VALUE ary)
5771 VALUE hash = ary_tmp_hash_new(ary);
5772 return ary_add_hash_by(hash, ary);
5800 ary2 = to_ary(ary2);
5801 if (
RARRAY_LEN(ary2) == 0) {
return ary_make_shared_copy(ary1); }
5806 VALUE elt = rb_ary_elt(ary1, i);
5807 if (rb_ary_includes_by_eql(ary2, elt))
continue;
5813 hash = ary_make_hash(ary2);
5815 if (rb_hash_stlike_lookup(hash,
RARRAY_AREF(ary1, i), NULL))
continue;
5842rb_ary_difference_multi(
int argc,
VALUE *argv,
VALUE ary)
5847 bool *is_hash =
ALLOCV_N(
bool, t0, argc);
5851 for (i = 0; i < argc; i++) {
5852 argv[i] = to_ary(argv[i]);
5853 is_hash[i] = (length > SMALL_ARRAY_LEN &&
RARRAY_LEN(argv[i]) > SMALL_ARRAY_LEN);
5854 if (is_hash[i]) argv[i] = ary_make_hash(argv[i]);
5859 VALUE elt = rb_ary_elt(ary, i);
5860 for (j = 0; j < argc; j++) {
5862 if (rb_hash_stlike_lookup(argv[j], elt, NULL))
5866 if (rb_ary_includes_by_eql(argv[j], elt))
break;
5905 VALUE hash, ary3, v;
5909 ary2 = to_ary(ary2);
5916 if (!rb_ary_includes_by_eql(ary2, v))
continue;
5917 if (rb_ary_includes_by_eql(ary3, v))
continue;
5923 hash = ary_make_hash(ary2);
5928 if (rb_hash_stlike_delete(hash, &vv, 0)) {
5958rb_ary_intersection_multi(
int argc,
VALUE *argv,
VALUE ary)
5963 for (i = 0; i < argc; i++) {
5964 result = rb_ary_and(result, argv[i]);
5971ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
5973 if (existing)
return ST_STOP;
5974 *key = *value = (
VALUE)arg;
5983 VALUE elt = rb_ary_elt(ary, i);
5984 if (rb_ary_includes_by_eql(ary_union, elt))
continue;
5995 if (!rb_hash_stlike_update(hash, (st_data_t)elt, ary_hash_orset, (st_data_t)elt)) {
6021 ary2 = to_ary(ary2);
6024 rb_ary_union(ary3, ary1);
6025 rb_ary_union(ary3, ary2);
6029 hash = ary_make_hash(ary1);
6030 rb_ary_union_hash(hash, ary2);
6032 return rb_hash_values(hash);
6059rb_ary_union_multi(
int argc,
VALUE *argv,
VALUE ary)
6066 for (i = 0; i < argc; i++) {
6067 argv[i] = to_ary(argv[i]);
6071 if (sum <= SMALL_ARRAY_LEN) {
6074 rb_ary_union(ary_union, ary);
6075 for (i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]);
6080 hash = ary_make_hash(ary);
6081 for (i = 0; i < argc; i++) rb_ary_union_hash(hash, argv[i]);
6083 return rb_hash_values(hash);
6103 VALUE hash, v, result, shorter, longer;
6107 ary2 = to_ary(ary2);
6113 if (rb_ary_includes_by_eql(ary2, v))
return Qtrue;
6125 hash = ary_make_hash(shorter);
6131 if (rb_hash_stlike_lookup(hash, vv, 0)) {
6141ary_max_generic(
VALUE ary,
long i,
VALUE vmax)
6149 if (rb_cmpint(
rb_funcallv(vmax, id_cmp, 1, &v), vmax, v) < 0) {
6158ary_max_opt_fixnum(
VALUE ary,
long i,
VALUE vmax)
6165 for (; i < n; ++i) {
6169 if ((
long)vmax < (
long)v) {
6174 return ary_max_generic(ary, i, vmax);
6182ary_max_opt_float(
VALUE ary,
long i,
VALUE vmax)
6189 for (; i < n; ++i) {
6193 if (rb_float_cmp(vmax, v) < 0) {
6198 return ary_max_generic(ary, i, vmax);
6206ary_max_opt_string(
VALUE ary,
long i,
VALUE vmax)
6213 for (; i < n; ++i) {
6222 return ary_max_generic(ary, i, vmax);
6285 return rb_nmin_run(ary, num, 0, 1, 1);
6291 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) > 0) {
6299 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6300 return ary_max_opt_fixnum(ary, 1, result);
6302 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6303 return ary_max_opt_string(ary, 1, result);
6306 return ary_max_opt_float(ary, 1, result);
6309 return ary_max_generic(ary, 1, result);
6313 if (UNDEF_P(result))
return Qnil;
6318ary_min_generic(
VALUE ary,
long i,
VALUE vmin)
6326 if (rb_cmpint(
rb_funcallv(vmin, id_cmp, 1, &v), vmin, v) > 0) {
6335ary_min_opt_fixnum(
VALUE ary,
long i,
VALUE vmin)
6342 for (; i < n; ++i) {
6346 if ((
long)vmin > (
long)a) {
6351 return ary_min_generic(ary, i, vmin);
6359ary_min_opt_float(
VALUE ary,
long i,
VALUE vmin)
6366 for (; i < n; ++i) {
6370 if (rb_float_cmp(vmin, a) > 0) {
6375 return ary_min_generic(ary, i, vmin);
6383ary_min_opt_string(
VALUE ary,
long i,
VALUE vmin)
6390 for (; i < n; ++i) {
6399 return ary_min_generic(ary, i, vmin);
6462 return rb_nmin_run(ary, num, 0, 0, 1);
6468 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) < 0) {
6476 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6477 return ary_min_opt_fixnum(ary, 1, result);
6479 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6480 return ary_min_opt_string(ary, 1, result);
6483 return ary_min_opt_float(ary, 1, result);
6486 return ary_min_generic(ary, 1, result);
6490 if (UNDEF_P(result))
return Qnil;
6517rb_ary_minmax(
VALUE ary)
6522 return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary));
6526push_value(st_data_t key, st_data_t val, st_data_t ary)
6560rb_ary_uniq_bang(
VALUE ary)
6565 rb_ary_modify_check(ary);
6569 hash = ary_make_hash_by(ary);
6571 hash = ary_make_hash(ary);
6577 rb_ary_modify_check(ary);
6578 ARY_SET_LEN(ary, 0);
6579 if (ARY_SHARED_P(ary)) {
6580 rb_ary_unshare(ary);
6583 ary_resize_capa(ary, hash_size);
6616rb_ary_uniq(
VALUE ary)
6625 hash = ary_make_hash_by(ary);
6626 uniq = rb_hash_values(hash);
6629 hash = ary_make_hash(ary);
6630 uniq = rb_hash_values(hash);
6653rb_ary_compact_bang(
VALUE ary)
6670 ary_resize_smaller(ary, n);
6690rb_ary_compact(
VALUE ary)
6693 rb_ary_compact_bang(ary);
6725rb_ary_count(
int argc,
VALUE *argv,
VALUE ary)
6741 VALUE obj = argv[0];
6744 rb_warn(
"given block not used");
6754VALUE rb_ident_set_new(
void);
6757flatten(
VALUE ary,
int level)
6760 VALUE stack, result, tmp = 0, elt;
6776 ARY_SET_LEN(result, i);
6778 stack = ary_new(0, ARY_DEFAULT_SIZE);
6794 if (level >= 0 &&
RARRAY_LEN(stack) / 2 >= level) {
6799 if (
RBASIC(result)->klass) {
6812 rb_raise(rb_eArgError,
"tried to flatten recursive array");
6842single_nested_array(
VALUE ary)
6893rb_ary_flatten_bang(
int argc,
VALUE *argv,
VALUE ary)
6895 int mod = 0, level = -1;
6899 rb_ary_modify_check(ary);
6901 if (level == 0)
return Qnil;
6903 VALUE child = single_nested_array(ary);
6909 if (level > 1) level--;
6910 result = flatten(child, level);
6914 result = flatten(ary, level);
6915 if (result == ary) {
6920 if (result != child && !(mod = ARY_EMBED_P(result)))
rb_ary_freeze(result);
6922 if (mod) ARY_SET_EMBED_LEN(result, 0);
6963rb_ary_flatten(
int argc,
VALUE *argv,
VALUE ary)
6970 if (level == 0)
return ary_make_shared_copy(ary);
6973 VALUE child = single_nested_array(ary);
6980 result = flatten(child, level);
6984 result = flatten(ary, level);
6987 if (result == ary || result == child) {
6988 return ary_make_shared_copy(result);
6994#define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1)
7005 long j = RAND_UPTO(i);
7022 rb_ary_shuffle_bang(ec, ary, randgen);
7031 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE
7038 long n,
len, i, j, k, idx[10];
7039 long rnds[numberof(idx)];
7040 long memo_threshold;
7049 return rb_ary_elt(ary, i);
7052 if (n < 0) rb_raise(rb_eArgError,
"negative sample number");
7054 if (n <= numberof(idx)) {
7055 for (i = 0; i < n; ++i) {
7056 rnds[i] = RAND_UPTO(
len - i);
7061 if (
len < k && n <= numberof(idx)) {
7062 for (i = 0; i < n; ++i) {
7072 return rb_ary_new_from_args(1,
RARRAY_AREF(ary, i));
7084 if (j >= i) l = i, g = ++j;
7085 if (k >= l && (++k >= g)) ++k;
7094 if (n <= numberof(idx)) {
7095 long sorted[numberof(idx)];
7096 sorted[0] = idx[0] = rnds[0];
7097 for (i=1; i<n; i++) {
7099 for (j = 0; j < i; ++j) {
7100 if (k < sorted[j])
break;
7103 memmove(&sorted[j+1], &sorted[j],
sizeof(sorted[0])*(i-j));
7104 sorted[j] = idx[i] = k;
7108 for (i=0; i<n; i++) {
7113 else if (n <= memo_threshold / 2) {
7116 st_table *memo = st_init_numtable_with_size(n);
7120 for (i=0; i<n; i++) {
7121 long r = RAND_UPTO(
len-i) + i;
7123 if (r > max_idx) max_idx = r;
7126 if (
len <= max_idx) n = 0;
7127 else if (n >
len) n =
len;
7129 for (i=0; i<n; i++) {
7130 long j2 = j = ptr_result[i];
7133 if (st_lookup(memo, (st_data_t)i, &value)) i2 = (
long)value;
7134 if (st_lookup(memo, (st_data_t)j, &value)) j2 = (
long)value;
7135 st_insert(memo, (st_data_t)j, (st_data_t)i2);
7136 ptr_result[i] = ptr_ary[j2];
7141 st_free_table(memo);
7146 RBASIC_CLEAR_CLASS(result);
7149 for (i=0; i<n; i++) {
7150 j = RAND_UPTO(
len-i) + i;
7152 ptr_result[j] = ptr_result[i];
7156 RBASIC_SET_CLASS_RAW(result,
rb_cArray);
7158 ARY_SET_LEN(result, n);
7186 if (mul <= 0)
return INT2FIX(0);
7188 return rb_fix_mul_fix(rb_ary_length(self), n);
7225rb_ary_cycle(
int argc,
VALUE *argv,
VALUE ary)
7232 if (argc == 0 ||
NIL_P(argv[0])) {
7237 if (n <= 0)
return Qnil;
7240 while (
RARRAY_LEN(ary) > 0 && (n < 0 || 0 < n--)) {
7254yield_indexed_values(
const VALUE values,
const long r,
const long *
const p)
7259 for (i = 0; i < r; i++) ARY_SET(result, i,
RARRAY_AREF(values, p[i]));
7260 ARY_SET_LEN(result, r);
7262 return !
RBASIC(values)->klass;
7278permute0(
const long n,
const long r,
long *
const p,
char *
const used,
const VALUE values)
7280 long i = 0, index = 0;
7283 const char *
const unused = memchr(&used[i], 0, n-i);
7298 for (i = 0; i < n; ++i) {
7299 if (used[i])
continue;
7301 if (!yield_indexed_values(values, r, p)) {
7317descending_factorial(
long from,
long how_many)
7322 while (--how_many > 0) {
7324 cnt = rb_int_mul(cnt,
LONG2FIX(v));
7334binomial_coefficient(
long comb,
long size)
7338 if (comb > size-comb) {
7344 else if (comb == 0) {
7348 for (i = 1; i < comb; ++i) {
7349 r = rb_int_mul(r,
LONG2FIX(size - i));
7350 r = rb_int_idiv(r,
LONG2FIX(i + 1));
7361 return descending_factorial(n, k);
7407rb_ary_permutation(
int argc,
VALUE *argv,
VALUE ary)
7417 if (r < 0 || n < r) {
7430 long *p =
ALLOCV_N(
long, t0, r+roomof(n,
sizeof(
long)));
7431 char *used = (
char*)(p + r);
7432 VALUE ary0 = ary_make_shared_copy(ary);
7433 RBASIC_CLEAR_CLASS(ary0);
7437 permute0(n, r, p, used, ary0);
7445combinate0(
const long len,
const long n,
long *
const stack,
const VALUE values)
7452 for (lev++; lev < n; lev++) {
7453 stack[lev+1] = stack[lev]+1;
7455 if (!yield_indexed_values(values, n, stack+1)) {
7459 if (lev == 0)
return;
7461 }
while (stack[lev+1]+n ==
len+lev+1);
7471 return binomial_coefficient(k, n);
7526 if (n < 0 ||
len < n) {
7538 VALUE ary0 = ary_make_shared_copy(ary);
7540 long *stack =
ALLOCV_N(
long, t0, n+1);
7542 RBASIC_CLEAR_CLASS(ary0);
7543 combinate0(
len, n, stack, ary0);
7563rpermute0(
const long n,
const long r,
long *
const p,
const VALUE values)
7565 long i = 0, index = 0;
7569 if (++index < r-1) {
7573 for (i = 0; i < n; ++i) {
7575 if (!yield_indexed_values(values, r, p)) {
7580 if (index <= 0)
return;
7581 }
while ((i = ++p[--index]) >= n);
7639rb_ary_repeated_permutation(
VALUE ary,
VALUE num)
7661 VALUE ary0 = ary_make_shared_copy(ary);
7662 RBASIC_CLEAR_CLASS(ary0);
7664 rpermute0(n, r, p, ary0);
7672rcombinate0(
const long n,
const long r,
long *
const p,
const long rest,
const VALUE values)
7674 long i = 0, index = 0;
7678 if (++index < r-1) {
7682 for (; i < n; ++i) {
7684 if (!yield_indexed_values(values, r, p)) {
7689 if (index <= 0)
return;
7690 }
while ((i = ++p[--index]) >= n);
7702 return binomial_coefficient(k, n + k - 1);
7745rb_ary_repeated_combination(
VALUE ary,
VALUE num)
7763 else if (
len == 0) {
7769 VALUE ary0 = ary_make_shared_copy(ary);
7770 RBASIC_CLEAR_CLASS(ary0);
7772 rcombinate0(
len, n, p, n, ary0);
7833rb_ary_product(
int argc,
VALUE *argv,
VALUE ary)
7839 int *counters =
ALLOCV_N(
int, t1, n);
7844 RBASIC_CLEAR_CLASS(t0);
7849 for (i = 1; i < n; i++) arrays[i] =
Qnil;
7850 for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
7853 for (i = 0; i < n; i++) counters[i] = 0;
7858 for (i = 0; i < n; i++) {
7860 arrays[i] = ary_make_shared_copy(arrays[i]);
7865 for (i = 0; i < n; i++) {
7871 if (MUL_OVERFLOW_LONG_P(resultlen, k))
7881 for (j = 0; j < n; j++) {
7886 if (
NIL_P(result)) {
7887 FL_SET(t0, RARRAY_SHARED_ROOT_FLAG);
7889 if (!
FL_TEST(t0, RARRAY_SHARED_ROOT_FLAG)) {
7893 FL_UNSET(t0, RARRAY_SHARED_ROOT_FLAG);
7906 while (counters[m] ==
RARRAY_LEN(arrays[m])) {
7909 if (--m < 0)
goto done;
7917 return NIL_P(result) ? ary : result;
7943 rb_raise(rb_eArgError,
"attempt to take negative size");
7970rb_ary_take_while(
VALUE ary)
7978 return rb_ary_take(ary,
LONG2FIX(i));
8006 rb_raise(rb_eArgError,
"attempt to drop negative size");
8033rb_ary_drop_while(
VALUE ary)
8041 return rb_ary_drop(ary,
LONG2FIX(i));
8084rb_ary_any_p(
int argc,
VALUE *argv,
VALUE ary)
8092 rb_warn(
"given block not used");
8099 for (i = 0; i <
len; ++i) {
8151rb_ary_all_p(
int argc,
VALUE *argv,
VALUE ary)
8159 rb_warn(
"given block not used");
8166 for (i = 0; i <
len; ++i) {
8212rb_ary_none_p(
int argc,
VALUE *argv,
VALUE ary)
8220 rb_warn(
"given block not used");
8227 for (i = 0; i <
len; ++i) {
8276rb_ary_one_p(
int argc,
VALUE *argv,
VALUE ary)
8285 rb_warn(
"given block not used");
8289 if (result)
return Qfalse;
8295 for (i = 0; i <
len; ++i) {
8297 if (result)
return Qfalse;
8305 if (result)
return Qfalse;
8337 self = rb_ary_at(self, *argv);
8338 if (!--argc)
return self;
8340 return rb_obj_dig(argc, argv, self,
Qnil);
8344finish_exact_sum(
long n,
VALUE r,
VALUE v,
int z)
8349 v = rb_rational_plus(r, v);
8416 if (init_is_float) {
8421 goto init_is_a_value;
8435 else if (RB_BIGNUM_TYPE_P(e))
8436 v = rb_big_plus(e, v);
8441 r = rb_rational_plus(r, e);
8446 v = finish_exact_sum(n, r, v, argc!=0);
8447 if (init_is_float) v = rb_float_plus(argv[0], v);
8451 v = finish_exact_sum(n, r, v, i!=0);
8463 goto has_float_value;
8473 else if (RB_BIGNUM_TYPE_P(e))
8480 if (isnan(f))
continue;
8486 if (isinf(f) && signbit(x) != signbit(f))
8492 if (isinf(f))
continue;
8495 if (fabs(f) >= fabs(x))
8508 goto has_some_value;
8522rb_ary_deconstruct(
VALUE ary)
9033 fake_ary_flags = init_fake_ary_flags();
9163 rb_vm_register_global_object(rb_cArray_empty_frozen);
9166#include "array.rbinc"
#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.
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.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
#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 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 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 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 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 rb_ary_new2
Old name of rb_ary_new_capa.
#define ENC_CODERANGE_SET(obj, cr)
Old name of RB_ENC_CODERANGE_SET.
#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_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)
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_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
VALUE rb_ary_rotate(VALUE ary, long rot)
Destructively rotates the passed array in-place to towards its end.
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_cmp(VALUE lhs, VALUE rhs)
Recursively compares each elements of the two arrays one-by-one using <=>.
VALUE rb_ary_rassoc(VALUE alist, VALUE key)
Identical to rb_ary_assoc(), except it scans the passed array from the opposite direction.
VALUE rb_ary_concat(VALUE lhs, VALUE rhs)
Destructively appends the contents of latter into the end of former.
VALUE rb_ary_assoc(VALUE alist, VALUE key)
Looks up the passed key, assuming the passed array is an alist.
VALUE rb_ary_reverse(VALUE ary)
Destructively reverses the passed array in-place.
VALUE rb_ary_shared_with_p(VALUE lhs, VALUE rhs)
Queries if the passed two arrays share the same backend storage.
VALUE rb_ary_shift(VALUE ary)
Destructively deletes an element from the beginning of the passed array and returns what was deleted.
VALUE rb_ary_sort(VALUE ary)
Creates a copy of the passed array, whose elements are sorted according to their <=> result.
VALUE rb_ary_resurrect(VALUE ary)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_includes(VALUE ary, VALUE elem)
Queries if the passed array has the passed entry.
VALUE rb_ary_aref(int argc, const VALUE *argv, VALUE ary)
Queries element(s) of an array.
VALUE rb_get_values_at(VALUE obj, long olen, int argc, const VALUE *argv, VALUE(*func)(VALUE obj, long oidx))
This was a generalisation of Array#values_at, Struct#values_at, and MatchData#values_at.
void rb_ary_free(VALUE ary)
Destroys the given array for no reason.
VALUE rb_ary_each(VALUE ary)
Iteratively yields each element of the passed array to the implicitly passed block if any.
VALUE rb_ary_delete_at(VALUE ary, long pos)
Destructively removes an element which resides at the specific index of the passed array.
VALUE rb_ary_plus(VALUE lhs, VALUE rhs)
Creates a new array, concatenating the former to the latter.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
void rb_ary_modify(VALUE ary)
Declares that the array is about to be modified.
VALUE rb_ary_replace(VALUE copy, VALUE orig)
Replaces the contents of the former object with the contents of the latter.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_to_ary(VALUE obj)
Force converts an object to an array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_subseq(VALUE ary, long beg, long len)
Obtains a part of the passed array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_to_s(VALUE ary)
Converts an array into a human-readable string.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_sort_bang(VALUE ary)
Destructively sorts the passed array in-place, according to each elements' <=> result.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_mem_clear(VALUE *buf, long len)
Fills the memory region with a series of RUBY_Qnil.
VALUE rb_ary_delete(VALUE ary, VALUE elem)
Destructively removes elements from the passed array, so that there would be no elements inside that ...
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#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 $,.
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.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
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.
@ RARRAY_EMBED_LEN_SHIFT
Where RARRAY_EMBED_LEN_MASK resides.
#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.
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.
bool rb_set_lookup(VALUE set, VALUE element)
Whether the set contains the given element.
#define RTEST
This is an old name of RB_TEST.
struct RBasic basic
Basic part, including flags and class.
union RArray::@55 as
Array's specific fields.
const VALUE shared_root
Parent of the array.
struct RArray::@55::@56 heap
Arrays that use separated memory region for elements use this pattern.
const VALUE ary[1]
Embedded elements.
long capa
Capacity of *ptr.
long len
Number of elements of the array.
union RArray::@55::@56::@57 aux
Auxiliary info.
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.