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/set.h"
27#include "internal/string.h"
28#include "internal/vm.h"
44#include "ruby_assert.h"
47VALUE rb_cArray_empty_frozen;
80#define ARY_DEFAULT_SIZE 16
81#define ARY_MAX_SIZE (LONG_MAX / (int)sizeof(VALUE))
82#define SMALL_ARRAY_LEN 16
86should_be_T_ARRAY(
VALUE ary)
91#define ARY_HEAP_PTR(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
92#define ARY_HEAP_LEN(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
93#define ARY_HEAP_CAPA(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(!ARY_SHARED_ROOT_P(a)), \
94 RARRAY(a)->as.heap.aux.capa)
96#define ARY_EMBED_PTR(a) (RUBY_ASSERT(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
97#define ARY_EMBED_LEN(a) \
98 (RUBY_ASSERT(ARY_EMBED_P(a)), \
99 (long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
100 (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
101#define ARY_HEAP_SIZE(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE))
103#define ARY_OWNS_HEAP_P(a) (RUBY_ASSERT(should_be_T_ARRAY((VALUE)(a))), \
104 !FL_TEST_RAW((a), RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG))
106#define FL_SET_EMBED(a) do { \
107 RUBY_ASSERT(!ARY_SHARED_P(a)); \
108 FL_SET((a), RARRAY_EMBED_FLAG); \
112#define FL_UNSET_EMBED(ary) FL_UNSET((ary), RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK)
113#define FL_SET_SHARED(ary) do { \
114 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
115 FL_SET((ary), RARRAY_SHARED_FLAG); \
117#define FL_UNSET_SHARED(ary) FL_UNSET((ary), RARRAY_SHARED_FLAG)
119#define ARY_SET_PTR_FORCE(ary, p) \
120 (RARRAY(ary)->as.heap.ptr = (p))
121#define ARY_SET_PTR(ary, p) do { \
122 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
123 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
124 ARY_SET_PTR_FORCE(ary, p); \
126#define ARY_SET_EMBED_LEN(ary, n) do { \
128 RUBY_ASSERT(ARY_EMBED_P(ary)); \
129 RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
130 RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
132#define ARY_SET_HEAP_LEN(ary, n) do { \
133 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
134 RARRAY(ary)->as.heap.len = (n); \
136#define ARY_SET_LEN(ary, n) do { \
137 if (ARY_EMBED_P(ary)) { \
138 ARY_SET_EMBED_LEN((ary), (n)); \
141 ARY_SET_HEAP_LEN((ary), (n)); \
143 RUBY_ASSERT(RARRAY_LEN(ary) == (n)); \
145#define ARY_INCREASE_PTR(ary, n) do { \
146 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
147 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
148 RARRAY(ary)->as.heap.ptr += (n); \
150#define ARY_INCREASE_LEN(ary, n) do { \
151 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
152 if (ARY_EMBED_P(ary)) { \
153 ARY_SET_EMBED_LEN((ary), RARRAY_LEN(ary)+(n)); \
156 RARRAY(ary)->as.heap.len += (n); \
160#define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? ary_embed_capa(ary) : \
161 ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : ARY_HEAP_CAPA(ary))
162#define ARY_SET_CAPA_FORCE(ary, n) \
163 RARRAY(ary)->as.heap.aux.capa = (n);
164#define ARY_SET_CAPA(ary, n) do { \
165 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
166 RUBY_ASSERT(!ARY_SHARED_P(ary)); \
167 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
168 ARY_SET_CAPA_FORCE(ary, n); \
171#define ARY_SHARED_ROOT_OCCUPIED(ary) (!OBJ_FROZEN(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1)
172#define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \
173 RUBY_ASSERT(ARY_SHARED_ROOT_P(ary)); \
174 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
175 RUBY_ASSERT((value) >= 0); \
176 RARRAY(ary)->as.heap.aux.capa = (value); \
178#define FL_SET_SHARED_ROOT(ary) do { \
179 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
180 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
181 FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \
195ary_embed_capa(
VALUE ary)
197 size_t size = rb_obj_shape_slot_size(ary) - offsetof(
struct RArray, as.
ary);
199 return size /
sizeof(
VALUE);
203ary_embed_size(
long capa)
206 if (size <
sizeof(
struct RArray)) size =
sizeof(
struct RArray);
211ary_embeddable_p(
long capa)
215 return capa <= embed_len_max && rb_gc_size_allocatable_p(ary_embed_size(
capa));
219rb_ary_embeddable_p(
VALUE ary)
229 return !(ARY_SHARED_ROOT_P(ary) ||
OBJ_FROZEN(ary) || ARY_SHARED_P(ary));
236rb_ary_embedded_shared_root_p(
VALUE ary)
242rb_ary_size_as_embedded(
VALUE ary)
246 if (ARY_EMBED_P(ary)) {
247 real_size = ary_embed_size(ARY_EMBED_LEN(ary));
249 else if (rb_ary_embeddable_p(ary)) {
250 real_size = ary_embed_size(ARY_HEAP_CAPA(ary));
253 real_size =
sizeof(
struct RArray);
260#define ary_verify(ary) ary_verify_(ary, __FILE__, __LINE__)
263ary_verify_(
VALUE ary,
const char *file,
int line)
267 if (ARY_SHARED_P(
ary)) {
276 else if (ARY_EMBED_P(
ary)) {
285 for (i=0; i<
len; i++) {
294#define ary_verify(ary) ((void)0)
323ary_mem_clear(
VALUE ary,
long beg,
long size)
331memfill(
register VALUE *mem,
register long size,
register VALUE val)
342 memfill(
ptr + beg, size, val);
352 if (argc > (
int)(128/
sizeof(
VALUE)) ) {
353 rb_gc_writebarrier_remember(buff_owner_ary);
361 for (i=0; i<argc; i++) {
371 ary_memcpy0(
ary, beg, argc, argv,
ary);
375ary_heap_alloc_buffer(
size_t capa)
383 ruby_xfree_sized((
void *)
ptr, size);
389 ary_heap_free_ptr(
ary, ARY_HEAP_PTR(
ary), ARY_HEAP_SIZE(
ary));
393ary_heap_realloc(
VALUE ary,
size_t new_capa)
406 if (!ARY_EMBED_P(
ary)) {
407 const VALUE *buf = ARY_HEAP_PTR(
ary);
408 long len = ARY_HEAP_LEN(
ary);
409 long capa = ARY_HEAP_CAPA(
ary);
412 ARY_SET_EMBED_LEN(
ary,
len);
421ary_resize_capa(
VALUE ary,
long capacity)
427 if (capacity > ary_embed_capa(
ary)) {
428 size_t new_capa = capacity;
429 if (ARY_EMBED_P(
ary)) {
430 long len = ARY_EMBED_LEN(
ary);
431 VALUE *
ptr = ary_heap_alloc_buffer(capacity);
436 ARY_SET_HEAP_LEN(
ary,
len);
439 new_capa = ary_heap_realloc(
ary, capacity);
441 ARY_SET_CAPA(
ary, new_capa);
444 if (!ARY_EMBED_P(
ary)) {
445 long len = ARY_HEAP_LEN(
ary);
446 long old_capa = ARY_HEAP_CAPA(
ary);
449 if (
len > capacity)
len = capacity;
451 ary_heap_free_ptr(
ary,
ptr, old_capa *
sizeof(
VALUE));
464 long capacity = ARY_HEAP_LEN(
ary);
465 long old_capa = ARY_HEAP_CAPA(
ary);
468 if (old_capa > capacity) {
469 size_t new_capa = ary_heap_realloc(
ary, capacity);
470 ARY_SET_CAPA(
ary, new_capa);
479 long new_capa = ARY_CAPA(
ary) / 2;
481 if (new_capa < ARY_DEFAULT_SIZE) {
482 new_capa = ARY_DEFAULT_SIZE;
484 if (new_capa >= ARY_MAX_SIZE - min) {
485 new_capa = (ARY_MAX_SIZE - min) / 2;
488 ary_resize_capa(
ary, new_capa);
507 FL_UNSET_SHARED(
ary);
513 if (ARY_OWNS_HEAP_P(
ary)) {
516 else if (ARY_SHARED_P(
ary)) {
521 ARY_SET_EMBED_LEN(
ary, 0);
546 RB_DEBUG_COUNTER_INC(obj_ary_shared_create);
554 rb_check_frozen(
ary);
561 if (ARY_SHARED_P(
ary)) {
567 if (
len <= ary_embed_capa(
ary)) {
569 FL_UNSET_SHARED(
ary);
573 ARY_SET_EMBED_LEN(
ary,
len);
577 FL_UNSET_SHARED(
ary);
579 ARY_SET_CAPA(
ary, shared_len);
590 ARY_SET_CAPA_FORCE(
ary,
len);
591 ARY_SET_PTR_FORCE(
ary,
ptr);
594 rb_gc_writebarrier_remember(
ary);
602 rb_ary_modify_check(
ary);
603 rb_ary_cancel_sharing(
ary);
607ary_ensure_room_for_push(
VALUE ary,
long add_len)
610 long new_len = old_len + add_len;
613 if (old_len > ARY_MAX_SIZE - add_len) {
616 if (ARY_SHARED_P(
ary)) {
617 if (new_len > ary_embed_capa(
ary)) {
621 rb_ary_modify_check(
ary);
632 ary_double_capa(
ary, new_len);
643 rb_ary_modify_check(
ary);
646 if (new_len >
capa) {
647 ary_double_capa(
ary, new_len);
669 if (!ARY_EMBED_P(
ary) && !ARY_SHARED_P(
ary) && !ARY_SHARED_ROOT_P(
ary)) {
670 ary_shrink_capa(
ary);
686 if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) &&
687 !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) &&
688 ARY_SHARED_ROOT(ary1) == ARY_SHARED_ROOT(ary2) &&
689 ARY_HEAP_LEN(ary1) == ARY_HEAP_LEN(ary2)) {
698 size_t size = ary_embed_size(
capa);
704 return rb_newobj_of(klass,
T_ARRAY | RARRAY_EMBED_FLAG, size);
708ary_alloc_heap(
VALUE klass)
712 ary->as.heap.len = 0;
713 ary->as.heap.aux.capa = 0;
714 ary->as.heap.ptr = NULL;
720empty_ary_alloc(
VALUE klass)
722 RUBY_DTRACE_CREATE_HOOK(ARRAY, 0);
723 return ary_alloc_embed(klass, 0);
734 rb_raise(rb_eArgError,
"negative array size (or size too big)");
736 if (
capa > ARY_MAX_SIZE) {
737 rb_raise(rb_eArgError,
"array size too big");
740 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
742 if (ary_embeddable_p(
capa)) {
743 ary = ary_alloc_embed(klass,
capa);
746 ary = ary_alloc_heap(klass);
750 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
751 ARY_SET_HEAP_LEN(
ary, 0);
770(rb_ary_new_from_args)(
long n, ...)
779 for (i=0; i<n; i++) {
789rb_ary_tmp_new_from_values(
VALUE klass,
long n,
const VALUE *elts)
793 ary = ary_new(klass, n);
795 ary_memcpy(
ary, 0, n, elts);
805 return rb_ary_tmp_new_from_values(
rb_cArray, n, elts);
811 size_t size = ary_embed_size(
capa);
817 return rb_ec_newobj_of(ec, klass,
T_ARRAY | RARRAY_EMBED_FLAG, size);
836 rb_raise(rb_eArgError,
"negative array size (or size too big)");
838 if (
capa > ARY_MAX_SIZE) {
839 rb_raise(rb_eArgError,
"array size too big");
842 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
844 if (ary_embeddable_p(
capa)) {
845 ary = ec_ary_alloc_embed(ec, klass,
capa);
848 ary = ec_ary_alloc_heap(ec, klass);
852 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
853 ARY_SET_HEAP_LEN(
ary, 0);
866 ary_memcpy(
ary, 0, n, elts);
881rb_ary_hidden_new_fill(
long capa)
892 if (ARY_OWNS_HEAP_P(
ary)) {
893 if (USE_DEBUG_COUNTER &&
894 !ARY_SHARED_ROOT_P(
ary) &&
896 RB_DEBUG_COUNTER_INC(obj_ary_extracapa);
899 RB_DEBUG_COUNTER_INC(obj_ary_ptr);
903 RB_DEBUG_COUNTER_INC(obj_ary_embed);
906 if (ARY_SHARED_P(
ary)) {
907 RB_DEBUG_COUNTER_INC(obj_ary_shared);
909 if (ARY_SHARED_ROOT_P(
ary) && ARY_SHARED_ROOT_OCCUPIED(
ary)) {
910 RB_DEBUG_COUNTER_INC(obj_ary_shared_root_occupied);
914static VALUE fake_ary_flags;
917init_fake_ary_flags(
void)
919 struct RArray fake_ary = {0};
922 RBASIC_SET_FULL_SHAPE_ID(
ary, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER);
928rb_setup_fake_ary(
struct RArray *fake_ary,
const VALUE *list,
long len)
931 RBASIC_CLEAR_CLASS((
VALUE)fake_ary);
937 return (
VALUE)fake_ary;
943 if (ARY_OWNS_HEAP_P(
ary)) {
944 return ARY_CAPA(
ary) *
sizeof(
VALUE);
956 if (ARY_SHARED_P(
ary)) {
957 return ARY_SHARED_ROOT(
ary);
959 else if (ARY_SHARED_ROOT_P(
ary)) {
971 VALUE shared = ary_alloc_heap(0);
972 FL_SET_SHARED_ROOT(shared);
974 if (ARY_EMBED_P(
ary)) {
976 ARY_SET_PTR(shared,
ptr);
980 ARY_SET_HEAP_LEN(
ary,
len);
987 ARY_SET_LEN(shared,
capa);
989 rb_ary_set_shared(
ary, shared);
1003 if (ary_embeddable_p(
len)) {
1008 ARY_SET_EMBED_LEN(subst,
len);
1012 return rb_ary_increment_share(ary_make_shared(
ary));
1025 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1027#define to_ary rb_to_array_type
1032 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1038 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1044 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1073rb_ary_s_new(
int argc,
VALUE *argv,
VALUE klass)
1079 if (argc > 0 &&
FIXNUM_P(argv[0])) {
1081 if (size < 0) size = 0;
1084 ary = ary_new(klass, size);
1163 if (argc == 1 && !
FIXNUM_P(size)) {
1174 rb_raise(rb_eArgError,
"negative array size");
1176 if (
len > ARY_MAX_SIZE) {
1177 rb_raise(rb_eArgError,
"array size too big");
1181 ARY_SET_LEN(
ary, 0);
1182 ary_resize_capa(
ary,
len);
1187 rb_warn(
"block supersedes default value argument");
1189 for (i=0; i<
len; i++) {
1191 ARY_SET_LEN(
ary, i + 1);
1195 ary_memfill(
ary, 0,
len, val);
1212rb_ary_s_create(
int argc,
VALUE *argv,
VALUE klass)
1215 if (argc > 0 && argv) {
1216 ary_memcpy(
ary, 0, argc, argv);
1217 ARY_SET_LEN(
ary, argc);
1231 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
1235 else if (idx >= ARY_MAX_SIZE) {
1240 if (idx >= ARY_CAPA(
ary)) {
1241 ary_double_capa(
ary, idx);
1248 ARY_SET_LEN(
ary, idx + 1);
1250 ARY_SET(
ary, idx, val);
1260 VALUE result = ary_alloc_heap(klass);
1261 size_t embed_capa = ary_embed_capa(result);
1262 if ((
size_t)
len <= embed_capa) {
1263 FL_SET_EMBED(result);
1265 ARY_SET_EMBED_LEN(result,
len);
1268 VALUE shared = ary_make_shared(
ary);
1273 FL_UNSET_EMBED(result);
1277 rb_ary_set_shared(result, shared);
1279 ARY_INCREASE_PTR(result, offset);
1280 ARY_SET_LEN(result,
len);
1290ary_make_partial_step(
VALUE ary,
VALUE klass,
long offset,
long len,
long step)
1297 const long orig_len =
len;
1299 if (step > 0 && step >=
len) {
1300 VALUE result = ary_new(klass, 1);
1305 ARY_SET_EMBED_LEN(result, 1);
1308 else if (step < 0 && step < -
len) {
1312 long ustep = (step < 0) ? -step : step;
1313 len = roomof(
len, ustep);
1316 long j = offset + ((step > 0) ? 0 : (orig_len - 1));
1318 VALUE result = ary_new(klass,
len);
1319 if (ARY_EMBED_P(result)) {
1323 for (i = 0; i <
len; ++i) {
1327 ARY_SET_EMBED_LEN(result,
len);
1333 for (i = 0; i <
len; ++i) {
1338 ARY_SET_LEN(result,
len);
1350enum ary_take_pos_flags
1357ary_take_first_or_last_n(
VALUE ary,
long n,
enum ary_take_pos_flags last)
1366 rb_raise(rb_eArgError,
"negative array size");
1375ary_take_first_or_last(
int argc,
const VALUE *argv,
VALUE ary,
enum ary_take_pos_flags last)
1382 return ary_take_first_or_last_n(
ary,
NUM2LONG(argv[0]), last);
1404 VALUE target_ary = ary_ensure_room_for_push(
ary, 1);
1408 ARY_SET_LEN(
ary, idx + 1);
1417 VALUE target_ary = ary_ensure_room_for_push(
ary,
len);
1418 ary_memcpy0(
ary, oldlen,
len, argv, target_ary);
1419 ARY_SET_LEN(
ary, oldlen +
len);
1451 rb_ary_modify_check(
ary);
1453 if (n == 0)
return Qnil;
1454 if (ARY_OWNS_HEAP_P(
ary) &&
1455 n * 3 < ARY_CAPA(
ary) &&
1456 ARY_CAPA(
ary) > ARY_DEFAULT_SIZE)
1458 ary_resize_capa(
ary, n * 2);
1463 ARY_SET_LEN(
ary, n - 1);
1507 rb_ary_modify_check(
ary);
1508 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
1521 rb_ary_modify_check(
ary);
1527 rb_ary_behead(
ary, 1);
1580 rb_ary_modify_check(
ary);
1581 result = ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1583 rb_ary_behead(
ary,n);
1595 rb_ary_modify_check(
ary);
1597 if (!ARY_SHARED_P(
ary)) {
1602 ARY_INCREASE_LEN(
ary, -n);
1607 ary_mem_clear(
ary, 0, n);
1608 ary_make_shared(
ary);
1610 else if (ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(
ary))) {
1611 ary_mem_clear(
ary, 0, n);
1614 ARY_INCREASE_PTR(
ary, n);
1615 ARY_INCREASE_LEN(
ary, -n);
1624 if (head - sharedp < argc) {
1625 long room =
capa -
len - argc;
1629 head = sharedp + argc + room;
1631 ARY_SET_PTR(
ary, head - argc);
1635 return ARY_SHARED_ROOT(
ary);
1639ary_modify_for_unshift(
VALUE ary,
int argc)
1642 long new_len =
len + argc;
1644 const VALUE *head, *sharedp;
1648 if (
capa - (
capa >> 6) <= new_len) {
1649 ary_double_capa(
ary, new_len);
1653 if (new_len > ARY_DEFAULT_SIZE * 4 && !ARY_EMBED_P(
ary)) {
1658 ary_make_shared(
ary);
1661 return make_room_for_unshift(
ary, head, (
void *)sharedp, argc,
capa,
len);
1675ary_ensure_room_for_unshift(
VALUE ary,
int argc)
1678 long new_len =
len + argc;
1680 if (
len > ARY_MAX_SIZE - argc) {
1683 else if (! ARY_SHARED_P(
ary)) {
1684 return ary_modify_for_unshift(
ary, argc);
1691 return ary_modify_for_unshift(
ary, argc);
1693 else if (new_len >
capa) {
1694 return ary_modify_for_unshift(
ary, argc);
1700 rb_ary_modify_check(
ary);
1701 return make_room_for_unshift(
ary, head, sharedp, argc,
capa,
len);
1727 rb_ary_modify_check(
ary);
1731 target_ary = ary_ensure_room_for_unshift(
ary, argc);
1732 ary_memcpy0(
ary, 0, argc, argv, target_ary);
1733 ARY_SET_LEN(
ary,
len + argc);
1740 return rb_ary_unshift_m(1, &item,
ary);
1749 if (offset < 0 ||
len <= offset) {
1758 return rb_ary_entry_internal(
ary, offset);
1766 if (beg > alen)
return -1;
1767 if (beg < 0 ||
len < 0)
return -1;
1769 if (alen <
len || alen < beg +
len) {
1782 if (
len == 0)
return ary_new(klass, 0);
1783 return ary_make_partial(
ary, klass, beg,
len);
1911 return rb_ary_aref2(
ary, argv[0], argv[1]);
1913 return rb_ary_aref1(
ary, argv[0]);
1930 long beg,
len, step;
1938 switch (rb_arithmetic_sequence_beg_len_step(arg, &beg, &
len, &step,
RARRAY_LEN(
ary), 0)) {
1944 if (step == 0) rb_raise(rb_eArgError,
"slice step cannot be zero");
1946 if (
len <= 0)
return ary_new(klass, 0);
1947 if (step == 1)
return ary_make_partial(
ary, klass, beg,
len);
1948 return ary_make_partial_step(
ary, klass, beg,
len, step);
1993 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1999ary_first(
VALUE self)
2015 return ary_last(
ary);
2018 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
2071 if (block_given && argc == 2) {
2072 rb_warn(
"block supersedes default value argument");
2080 if (block_given)
return rb_yield(pos);
2082 rb_raise(
rb_eIndexError,
"index %ld outside of array bounds: %ld...%ld",
2126 if (!
NIL_P(if_none)) {
2170 idx = (idx >=
len) ?
len : idx;
2173 if (!
NIL_P(if_none)) {
2231 rb_warn(
"given block not used");
2289 rb_warn(
"given block not used");
2307 if (!
NIL_P(tmp))
return tmp;
2312ary_splice(
VALUE ary,
long beg,
long len,
const VALUE *rptr,
long rlen,
int self_insert)
2321 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2325 if (olen <
len || olen < beg +
len) {
2331 if (beg > ARY_MAX_SIZE - rlen) {
2334 target_ary = ary_ensure_room_for_push(
ary, rlen-
len);
2336 ary_mem_clear(
ary, olen, beg - olen);
2340 ary_memcpy0(
ary, beg, rlen, rptr, target_ary);
2347 if (olen -
len > ARY_MAX_SIZE - rlen) {
2351 alen = olen + rlen -
len;
2352 if (alen >= ARY_CAPA(
ary)) {
2353 ary_double_capa(
ary, alen);
2360 ARY_SET_LEN(
ary, alen);
2364 rb_gc_writebarrier_remember(
ary);
2393 rb_ary_modify_check(
ary);
2394 if (ARY_SHARED_P(
ary)) {
2398 rb_bug(
"probable buffer overflow: %ld for %ld",
len,
capa);
2404rb_ary_modify_expand(
VALUE ary,
long expand)
2409 rb_raise(rb_eArgError,
"negative expanding array size");
2411 if (expand >= ARY_MAX_SIZE -
len) {
2412 rb_raise(rb_eArgError,
" size too big");
2414 rb_ary_modify_check(
ary);
2415 if (
len + expand > ARY_CAPA(
ary)) {
2416 ary_resize_capa(
ary,
len + expand);
2428 if (
len == olen)
return ary;
2429 if (
len > ARY_MAX_SIZE) {
2433 if (
len > ARY_CAPA(
ary)) {
2434 ary_double_capa(
ary,
len);
2436 ary_mem_clear(
ary, olen,
len - olen);
2439 else if (ARY_EMBED_P(
ary)) {
2440 ARY_SET_EMBED_LEN(
ary,
len);
2442 else if (
len <= ary_embed_capa(
ary)) {
2444 long ptr_capa = ARY_HEAP_SIZE(
ary);
2445 bool is_malloc_ptr = !ARY_SHARED_P(
ary);
2450 ARY_SET_EMBED_LEN(
ary,
len);
2452 if (is_malloc_ptr) ruby_xfree_sized((
void *)
ptr, ptr_capa);
2455 if (olen >
len + ARY_DEFAULT_SIZE) {
2456 size_t new_capa = ary_heap_realloc(
ary,
len);
2457 ARY_SET_CAPA(
ary, new_capa);
2459 ARY_SET_HEAP_LEN(
ary,
len);
2626 long offset, beg,
len;
2629 rb_ary_modify_check(
ary);
2633 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[2]);
2637 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2641 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[1]);
2645 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2690 rb_ary_modify_check(
ary);
2692 if (argc == 1)
return ary;
2699 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2704 ary_splice(
ary, pos, 0, argv + 1, argc - 1, FALSE);
2714 return rb_ary_length(
ary);
2928 ARY_SET_LEN(dup,
len);
2943rb_zjit_array_new_can_fastpath(
long len,
size_t *alloc_size_out,
VALUE *flags_out)
2945 if (!ary_embeddable_p(
len)) {
2948 long embed_size = ary_embed_size(
len);
2950 *alloc_size_out = embed_size;
2956rb_zjit_array_dup_can_fastpath(
VALUE ary,
size_t *alloc_size_out,
VALUE *flags_out,
long *len_out)
2959 if (!rb_zjit_array_new_can_fastpath(
len, alloc_size_out, flags_out)) {
2974recursive_join(
VALUE obj,
VALUE argp,
int recur)
2979 VALUE result = arg[2];
2980 int *first = (
int *)arg[3];
2983 rb_raise(rb_eArgError,
"recursive array join");
2986 ary_join_1(obj,
ary, sep, 0, result, first);
2998 for (i=0; i<max; i++) {
3001 if (i > 0 && !
NIL_P(sep))
3009ary_join_1_str(
VALUE dst,
VALUE src,
int *first)
3013 rb_enc_copy(dst, src);
3022 rb_raise(rb_eArgError,
"recursive array join");
3031 args[3] = (
VALUE)first;
3042 if (i > 0 && !
NIL_P(sep))
3047 ary_join_1_str(result, val, first);
3050 ary_join_1_ary(val,
ary, sep, result, val, first);
3053 ary_join_1_str(result, tmp, first);
3056 ary_join_1_ary(val,
ary, sep, result, tmp, first);
3074 if (n == 0)
return Qundef;
3079 if (!rb_str_encindex_fastpath(encidx))
return Qundef;
3084 const char *sep_ptr = NULL;
3086 int sep_cr = rb_enc_str_coderange(sep);
3090 sep_ptr = RSTRING_PTR(sep);
3091 sep_len = RSTRING_LEN(sep);
3096 long len = 1 + sep_len * (n - 1);
3097 for (
long i = 0; i < n; i++) {
3100 len += RSTRING_LEN(s);
3105 rb_enc_associate_index(result, encidx);
3106 char *
const buf = RSTRING_PTR(result);
3108 for (
long i = 0; i < n; i++) {
3110 long slen = RSTRING_LEN(s);
3111 if (i > 0 && sep_len) {
3112 memcpy(p, sep_ptr, sep_len);
3115 memcpy(p, RSTRING_PTR(s), slen);
3129 VALUE val, tmp, result;
3135 result = ary_join_fast(
ary, sep);
3136 if (!UNDEF_P(result))
return result;
3142 for (i=0; i < len_memo; i++) {
3146 if (
NIL_P(tmp) || tmp != val) {
3151 rb_enc_associate(result, rb_usascii_encoding());
3152 i = ary_join_0(
ary, sep, i, result);
3154 ary_join_1(
ary,
ary, sep, i, result, &first);
3157 len += RSTRING_LEN(tmp);
3161 len += RSTRING_LEN(val);
3220 else rb_enc_copy(str, s);
3251 return rb_ary_inspect(
ary);
3315 const VALUE e = rb_ary_elt(
ary, i);
3316 const VALUE elt = block_given ? rb_yield_force_blockarg(e) : e;
3318 if (
NIL_P(key_value_pair)) {
3319 rb_raise(
rb_eTypeError,
"wrong element type %"PRIsVALUE
" at %ld (expected array)",
3323 rb_raise(rb_eArgError,
"wrong array length at %ld (expected 2, was %ld)",
3364 ary_reverse(p1, p2);
3410 do *p2-- = *p1++;
while (--
len > 0);
3411 rb_gc_writebarrier_remember(dup);
3418rotate_count(
long cnt,
long len)
3420 return (cnt < 0) ? (
len - (~cnt %
len) - 1) : (cnt %
len);
3431 else if (cnt ==
len - 1) {
3439 if (--cnt > 0) ary_reverse(
ptr,
ptr + cnt);
3451 if (
len > 1 && (cnt = rotate_count(cnt,
len)) > 0) {
3543 cnt = rotate_count(cnt,
len);
3546 ary_memcpy(rotated, 0,
len,
ptr + cnt);
3547 ary_memcpy(rotated,
len, cnt,
ptr);
3553struct ary_sort_data {
3559sort_reentered(
VALUE ary)
3561 if (
RBASIC(ary)->klass) {
3568sort_returned(
struct ary_sort_data *data)
3573 sort_reentered(data->ary);
3577sort_1(
const void *ap,
const void *bp,
void *dummy)
3579 struct ary_sort_data *data = dummy;
3580 VALUE retval = sort_reentered(data->ary);
3588 n = rb_cmpint(retval, a, b);
3589 sort_returned(data);
3594sort_2(
const void *ap,
const void *bp,
void *dummy)
3596 struct ary_sort_data *data = dummy;
3597 VALUE retval = sort_reentered(data->ary);
3602 if ((
long)a > (long)b)
return 1;
3603 if ((
long)a < (long)b)
return -1;
3606 if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(STRING)) {
3610 return rb_float_cmp(a, b);
3614 n = rb_cmpint(retval, a, b);
3615 sort_returned(data);
3636 VALUE tmp = ary_make_substitution(ary);
3637 struct ary_sort_data data;
3639 RBASIC_CLEAR_CLASS(tmp);
3641 data.receiver = ary;
3647 if (ARY_EMBED_P(tmp)) {
3648 if (ARY_SHARED_P(ary)) {
3649 rb_ary_unshare(ary);
3652 if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) {
3653 ary_resize_capa(ary, ARY_EMBED_LEN(tmp));
3655 ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp));
3656 ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
3659 if (!ARY_EMBED_P(ary) && ARY_HEAP_PTR(ary) == ARY_HEAP_PTR(tmp)) {
3660 FL_UNSET_SHARED(ary);
3665 if (ARY_EMBED_P(ary)) {
3666 FL_UNSET_EMBED(ary);
3668 else if (ARY_SHARED_P(ary)) {
3670 rb_ary_unshare(ary);
3675 ARY_SET_PTR(ary, ARY_HEAP_PTR(tmp));
3676 ARY_SET_HEAP_LEN(ary,
len);
3677 ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
3681 ARY_SET_EMBED_LEN(tmp, 0);
3749rb_ary_bsearch(
VALUE ary)
3751 VALUE index_result = rb_ary_bsearch_index(ary);
3756 return index_result;
3773rb_ary_bsearch_index(
VALUE ary)
3776 int smaller = 0, satisfied = 0;
3780 while (low < high) {
3781 mid = low + ((high - low) / 2);
3788 else if (v ==
Qtrue) {
3792 else if (!
RTEST(v)) {
3797 switch (rb_cmpint(
rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
3799 case 1: smaller = 0;
break;
3800 case -1: smaller = 1;
3805 " (must be numeric, true, false or nil)",
3815 if (!satisfied)
return Qnil;
3849rb_ary_sort_by_bang(
VALUE ary)
3856 sorted =
rb_block_call(ary, rb_intern(
"sort_by"), 0, 0, sort_by_i, 0);
3884rb_ary_collect(
VALUE ary)
3919rb_ary_collect_bang(
VALUE ary)
3935 long beg,
len, i, j;
3937 for (i=0; i<argc; i++) {
3944 long end = olen < beg+
len ? olen : beg+
len;
3945 for (j = beg; j < end; j++) {
3971 const long end = beg +
len;
4095rb_ary_values_at(
int argc,
VALUE *argv,
VALUE ary)
4099 for (i = 0; i < argc; ++i) {
4100 append_values_at_single(result, ary, olen, argv[i]);
4128rb_ary_select(
VALUE ary)
4143struct select_bang_arg {
4149select_bang_i(
VALUE a)
4151 volatile struct select_bang_arg *arg = (
void *)a;
4152 VALUE ary = arg->ary;
4155 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4163 return (i1 == i2) ?
Qnil : ary;
4167select_bang_ensure(
VALUE a)
4169 volatile struct select_bang_arg *arg = (
void *)a;
4170 VALUE ary = arg->ary;
4172 long i1 = arg->len[0], i2 = arg->len[1];
4174 if (i2 <
len && i2 < i1) {
4183 ARY_SET_LEN(ary, i2 + tail);
4211rb_ary_select_bang(
VALUE ary)
4213 struct select_bang_arg args;
4219 args.len[0] = args.len[1] = 0;
4240rb_ary_keep_if(
VALUE ary)
4243 rb_ary_select_bang(ary);
4248ary_resize_smaller(
VALUE ary,
long len)
4252 ARY_SET_LEN(ary,
len);
4253 if (
len * 2 < ARY_CAPA(ary) &&
4254 ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
4255 ary_resize_capa(ary,
len * 2);
4303 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4322 ary_resize_smaller(ary, i2);
4333 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4348 ary_resize_smaller(ary, i2);
4360 if (pos < 0)
return Qnil;
4368 ARY_INCREASE_LEN(ary, -1);
4408ary_slice_bang_by_rb_ary_splice(
VALUE ary,
long pos,
long len)
4415 else if (pos < -orig_len) {
4421 else if (orig_len < pos) {
4424 if (orig_len < pos +
len) {
4425 len = orig_len - pos;
4432 ary_splice(ary, pos,
len, 0, 0, FALSE);
4530rb_ary_slice_bang(
int argc,
VALUE *argv,
VALUE ary)
4535 rb_ary_modify_check(ary);
4542 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4549 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4578reject_bang_i(
VALUE a)
4580 volatile struct select_bang_arg *arg = (
void *)a;
4581 VALUE ary = arg->ary;
4584 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4592 return (i1 == i2) ?
Qnil : ary;
4596ary_reject_bang(
VALUE ary)
4598 struct select_bang_arg args;
4599 rb_ary_modify_check(ary);
4601 args.len[0] = args.len[1] = 0;
4626rb_ary_reject_bang(
VALUE ary)
4630 return ary_reject_bang(ary);
4651rb_ary_reject(
VALUE ary)
4657 ary_reject(ary, rejected_ary);
4658 return rejected_ary;
4679rb_ary_delete_if(
VALUE ary)
4683 ary_reject_bang(ary);
4698take_items(
VALUE obj,
long n)
4703 if (n == 0)
return result;
4706 args[0] = result; args[1] = (
VALUE)n;
4707 if (UNDEF_P(rb_check_block_call(obj, idEach, 0, 0, take_i, (
VALUE)args)))
4708 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
4814 for (i=0; i<argc; i++) {
4815 argv[i] = take_items(argv[i],
len);
4819 int arity = rb_block_arity();
4828 for (j=0; j<argc; j++) {
4829 tmp[j+1] = rb_ary_elt(argv[j], i);
4841 for (j=0; j<argc; j++) {
4855 for (j=0; j<argc; j++) {
4881rb_ary_transpose(
VALUE ary)
4883 long elen = -1, alen, i, j;
4884 VALUE tmp, result = 0;
4888 for (i=0; i<alen; i++) {
4889 tmp = to_ary(rb_ary_elt(ary, i));
4893 for (j=0; j<elen; j++) {
4898 rb_raise(
rb_eIndexError,
"element size differs (%ld should be %ld)",
4901 for (j=0; j<elen; j++) {
4902 rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
4926 rb_ary_modify_check(copy);
4927 orig = to_ary(orig);
4928 if (copy == orig)
return copy;
4933 if (
RARRAY_LEN(orig) <= ary_embed_capa(copy)) {
4940 else if (ARY_EMBED_P(orig)) {
4941 long len = ARY_EMBED_LEN(orig);
4942 VALUE *ptr = ary_heap_alloc_buffer(
len);
4944 FL_UNSET_EMBED(copy);
4945 ARY_SET_PTR(copy, ptr);
4946 ARY_SET_LEN(copy,
len);
4947 ARY_SET_CAPA(copy,
len);
4956 VALUE shared_root = ary_make_shared(orig);
4957 FL_UNSET_EMBED(copy);
4958 ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
4959 ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
4960 rb_ary_set_shared(copy, shared_root);
4983 rb_ary_modify_check(ary);
4984 if (ARY_SHARED_P(ary)) {
4985 rb_ary_unshare(ary);
4987 ARY_SET_EMBED_LEN(ary, 0);
4990 ARY_SET_LEN(ary, 0);
4991 if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
4992 ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
5183 long beg = 0, end = 0,
len = 0;
5206 if (beg < 0) beg = 0;
5215 if (beg >= ARY_MAX_SIZE ||
len > ARY_MAX_SIZE - beg) {
5216 rb_raise(rb_eArgError,
"argument too big");
5220 if (end >= ARY_CAPA(ary)) {
5221 ary_resize_capa(ary, end);
5224 ARY_SET_LEN(ary, end);
5227 if (UNDEF_P(item)) {
5231 for (i=beg; i<end; i++) {
5238 ary_memfill(ary, beg,
len, item);
5260 long len, xlen, ylen;
5270 ARY_SET_LEN(z,
len);
5297rb_ary_concat_multi(
int argc,
VALUE *argv,
VALUE ary)
5299 rb_ary_modify_check(ary);
5304 else if (argc > 1) {
5307 for (i = 0; i < argc; i++) {
5310 ary_append(ary, args);
5320 return ary_append(x, to_ary(y));
5359 rb_raise(rb_eArgError,
"negative argument");
5362 rb_raise(rb_eArgError,
"argument too big");
5367 ARY_SET_LEN(ary2,
len);
5372 ary_memcpy(ary2, 0, t, ptr);
5373 while (t <=
len/2) {
5450recursive_equal(
VALUE ary1,
VALUE ary2,
int recur)
5453 const VALUE *p1, *p2;
5455 if (recur)
return Qtrue;
5462 for (i = 0; i < len1; i++) {
5510 if (ary1 == ary2)
return Qtrue;
5523recursive_eql(
VALUE ary1,
VALUE ary2,
int recur)
5527 if (recur)
return Qtrue;
5529 if (!
rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
5557 if (ary1 == ary2)
return Qtrue;
5565ary_hash_values(
long len,
const VALUE *elements,
const VALUE ary)
5573 for (i=0; i<
len; i++) {
5574 n = rb_hash(elements[i]);
5586rb_ary_hash_values(
long len,
const VALUE *elements)
5588 return ary_hash_values(
len, elements, 0);
5607rb_ary_hash(
VALUE ary)
5658recursive_cmp(
VALUE ary1,
VALUE ary2,
int recur)
5662 if (recur)
return Qundef;
5667 for (i=0; i<
len; i++) {
5668 VALUE e1 = rb_ary_elt(ary1, i), e2 = rb_ary_elt(ary2, i);
5723 if (ary1 == ary2)
return INT2FIX(0);
5725 if (!UNDEF_P(v))
return v;
5741ary_to_set(
VALUE ary)
5744 rb_ary_union_set(set, ary);
5769 ary2 = to_ary(ary2);
5770 if (
RARRAY_LEN(ary2) == 0) {
return ary_make_shared_copy(ary1); }
5774 for (
long i = 0; i <
RARRAY_LEN(ary1); i++) {
5775 VALUE elt = rb_ary_elt(ary1, i);
5776 if (rb_ary_includes_by_eql(ary2, elt))
continue;
5782 VALUE set = ary_to_set(ary2);
5783 for (
long i = 0; i <
RARRAY_LEN(ary1); i++) {
5811rb_ary_difference_multi(
int argc,
VALUE *argv,
VALUE ary)
5814 bool *is_set =
ALLOCV_N(
bool, t0, argc);
5818 for (
long i = 0; i < argc; i++) {
5819 argv[i] = to_ary(argv[i]);
5820 is_set[i] = (length > SMALL_ARRAY_LEN &&
RARRAY_LEN(argv[i]) > SMALL_ARRAY_LEN);
5822 argv[i] = ary_to_set(argv[i]);
5828 VALUE elt = rb_ary_elt(ary, i);
5829 for (j = 0; j < argc; j++) {
5835 if (rb_ary_includes_by_eql(argv[j], elt))
break;
5874 ary2 = to_ary(ary2);
5879 for (
long i = 0; i <
RARRAY_LEN(ary1); i++) {
5881 if (!rb_ary_includes_by_eql(ary2, v))
continue;
5882 if (rb_ary_includes_by_eql(ary3, v))
continue;
5888 VALUE set = ary_to_set(ary2);
5890 for (
long i = 0; i <
RARRAY_LEN(ary1); i++) {
5892 if (rb_set_delete_no_check(set, v)) {
5922rb_ary_intersection_multi(
int argc,
VALUE *argv,
VALUE ary)
5927 for (i = 0; i < argc; i++) {
5928 result = rb_ary_and(result, argv[i]);
5939 VALUE elt = rb_ary_elt(ary, i);
5940 if (rb_ary_includes_by_eql(ary_union, elt))
continue;
5963 ary2 = to_ary(ary2);
5966 rb_ary_union(ary3, ary1);
5967 rb_ary_union(ary3, ary2);
5972 rb_ary_union_set(set, ary1);
5973 rb_ary_union_set(set, ary2);
5975 return rb_set_to_a(set);
6002rb_ary_union_multi(
int argc,
VALUE *argv,
VALUE ary)
6005 for (
int i = 0; i < argc; i++) {
6006 argv[i] = to_ary(argv[i]);
6010 if (sum <= SMALL_ARRAY_LEN) {
6013 rb_ary_union(ary_union, ary);
6014 for (
int i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]);
6020 rb_ary_union_set(set, ary);
6021 for (
int i = 0; i < argc; i++) rb_ary_union_set(set, argv[i]);
6023 return rb_set_to_a(set);
6043 ary2 = to_ary(ary2);
6047 for (
long i = 0; i <
RARRAY_LEN(ary1); i++) {
6049 if (rb_ary_includes_by_eql(ary2, v))
return Qtrue;
6054 VALUE shorter = ary1;
6055 VALUE longer = ary2;
6061 VALUE set = ary_to_set(shorter);
6064 for (
long i = 0; i <
RARRAY_LEN(longer); i++) {
6076ary_max_generic(
VALUE ary,
long i,
VALUE vmax)
6084 if (rb_cmpint(
rb_funcallv(vmax, id_cmp, 1, &v), vmax, v) < 0) {
6093ary_max_opt_fixnum(
VALUE ary,
long i,
VALUE vmax)
6100 for (; i < n; ++i) {
6104 if ((
long)vmax < (
long)v) {
6109 return ary_max_generic(ary, i, vmax);
6117ary_max_opt_float(
VALUE ary,
long i,
VALUE vmax)
6124 for (; i < n; ++i) {
6128 if (rb_float_cmp(vmax, v) < 0) {
6133 return ary_max_generic(ary, i, vmax);
6141ary_max_opt_string(
VALUE ary,
long i,
VALUE vmax)
6148 for (; i < n; ++i) {
6157 return ary_max_generic(ary, i, vmax);
6220 return rb_nmin_run(ary, num, 0, 1, 1);
6226 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) > 0) {
6234 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6235 return ary_max_opt_fixnum(ary, 1, result);
6237 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6238 return ary_max_opt_string(ary, 1, result);
6241 return ary_max_opt_float(ary, 1, result);
6244 return ary_max_generic(ary, 1, result);
6248 if (UNDEF_P(result))
return Qnil;
6253ary_min_generic(
VALUE ary,
long i,
VALUE vmin)
6261 if (rb_cmpint(
rb_funcallv(vmin, id_cmp, 1, &v), vmin, v) > 0) {
6270ary_min_opt_fixnum(
VALUE ary,
long i,
VALUE vmin)
6277 for (; i < n; ++i) {
6281 if ((
long)vmin > (
long)a) {
6286 return ary_min_generic(ary, i, vmin);
6294ary_min_opt_float(
VALUE ary,
long i,
VALUE vmin)
6301 for (; i < n; ++i) {
6305 if (rb_float_cmp(vmin, a) > 0) {
6310 return ary_min_generic(ary, i, vmin);
6318ary_min_opt_string(
VALUE ary,
long i,
VALUE vmin)
6325 for (; i < n; ++i) {
6334 return ary_min_generic(ary, i, vmin);
6397 return rb_nmin_run(ary, num, 0, 0, 1);
6403 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) < 0) {
6411 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6412 return ary_min_opt_fixnum(ary, 1, result);
6414 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6415 return ary_min_opt_string(ary, 1, result);
6418 return ary_min_opt_float(ary, 1, result);
6421 return ary_min_generic(ary, 1, result);
6425 if (UNDEF_P(result))
return Qnil;
6452rb_ary_minmax(
VALUE ary)
6457 return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary));
6495rb_ary_uniq_bang(
VALUE ary)
6497 rb_ary_modify_check(ary);
6505 VALUE elt = rb_ary_elt(ary, i);
6506 if (rb_set_add_no_check(set,
rb_yield(elt)))
6515 VALUE set = ary_to_set(ary);
6520 rb_ary_modify_check(ary);
6521 ARY_SET_LEN(ary, 0);
6522 if (ARY_SHARED_P(ary)) {
6523 rb_ary_unshare(ary);
6526 ary_resize_capa(ary, size);
6559rb_ary_uniq(
VALUE ary)
6570 VALUE elt = rb_ary_elt(ary, i);
6571 if (rb_set_add_no_check(set,
rb_yield(elt)))
6577 rb_ary_union_set(set, ary);
6578 return rb_set_to_a(set);
6599rb_ary_compact_bang(
VALUE ary)
6616 ary_resize_smaller(ary, n);
6636rb_ary_compact(
VALUE ary)
6639 rb_ary_compact_bang(ary);
6671rb_ary_count(
int argc,
VALUE *argv,
VALUE ary)
6687 VALUE obj = argv[0];
6690 rb_warn(
"given block not used");
6701flatten(
VALUE ary,
int level)
6725 ARY_SET_LEN(result, i);
6727 stack = ary_new(0, ARY_DEFAULT_SIZE);
6743 if (level >= 0 &&
RARRAY_LEN(stack) / 2 >= level) {
6748 if (
RBASIC(result)->klass) {
6761 rb_raise(rb_eArgError,
"tried to flatten recursive array");
6791single_nested_array(
VALUE ary)
6842rb_ary_flatten_bang(
int argc,
VALUE *argv,
VALUE ary)
6844 int mod = 0, level = -1;
6848 rb_ary_modify_check(ary);
6850 if (level == 0)
return Qnil;
6852 VALUE child = single_nested_array(ary);
6858 if (level > 1) level--;
6859 result = flatten(child, level);
6863 result = flatten(ary, level);
6864 if (result == ary) {
6869 if (result != child && !(mod = ARY_EMBED_P(result)))
rb_ary_freeze(result);
6871 if (mod) ARY_SET_EMBED_LEN(result, 0);
6912rb_ary_flatten(
int argc,
VALUE *argv,
VALUE ary)
6919 if (level == 0)
return ary_make_shared_copy(ary);
6922 VALUE child = single_nested_array(ary);
6929 result = flatten(child, level);
6933 result = flatten(ary, level);
6936 if (result == ary || result == child) {
6937 return ary_make_shared_copy(result);
6943#define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1)
6954 long j = RAND_UPTO(i);
6971 rb_ary_shuffle_bang(ec, ary, randgen);
6980 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE
6987 long n,
len, i, j, k, idx[10];
6988 long rnds[numberof(idx)];
6989 long memo_threshold;
6998 return rb_ary_elt(ary, i);
7001 if (n < 0) rb_raise(rb_eArgError,
"negative sample number");
7003 if (n <= numberof(idx)) {
7004 for (i = 0; i < n; ++i) {
7005 rnds[i] = RAND_UPTO(
len - i);
7010 if (
len < k && n <= numberof(idx)) {
7011 for (i = 0; i < n; ++i) {
7021 return rb_ary_new_from_args(1,
RARRAY_AREF(ary, i));
7033 if (j >= i) l = i, g = ++j;
7034 if (k >= l && (++k >= g)) ++k;
7043 if (n <= numberof(idx)) {
7044 long sorted[numberof(idx)];
7045 sorted[0] = idx[0] = rnds[0];
7046 for (i=1; i<n; i++) {
7048 for (j = 0; j < i; ++j) {
7049 if (k < sorted[j])
break;
7052 memmove(&sorted[j+1], &sorted[j],
sizeof(sorted[0])*(i-j));
7053 sorted[j] = idx[i] = k;
7057 for (i=0; i<n; i++) {
7062 else if (n <= memo_threshold / 2) {
7065 st_table *memo = st_init_numtable_with_size(n);
7069 for (i=0; i<n; i++) {
7070 long r = RAND_UPTO(
len-i) + i;
7072 if (r > max_idx) max_idx = r;
7075 if (
len <= max_idx) n = 0;
7076 else if (n >
len) n =
len;
7078 for (i=0; i<n; i++) {
7079 long j2 = j = ptr_result[i];
7082 if (st_lookup(memo, (st_data_t)i, &value)) i2 = (
long)value;
7083 if (st_lookup(memo, (st_data_t)j, &value)) j2 = (
long)value;
7084 st_insert(memo, (st_data_t)j, (st_data_t)i2);
7085 ptr_result[i] = ptr_ary[j2];
7090 st_free_table(memo);
7095 RBASIC_CLEAR_CLASS(result);
7098 for (i=0; i<n; i++) {
7099 j = RAND_UPTO(
len-i) + i;
7101 ptr_result[j] = ptr_result[i];
7105 RBASIC_SET_CLASS_RAW(result,
rb_cArray);
7107 ARY_SET_LEN(result, n);
7135 if (mul <= 0)
return INT2FIX(0);
7137 return rb_fix_mul_fix(rb_ary_length(self), n);
7174rb_ary_cycle(
int argc,
VALUE *argv,
VALUE ary)
7181 if (argc == 0 ||
NIL_P(argv[0])) {
7186 if (n <= 0)
return Qnil;
7189 while (
RARRAY_LEN(ary) > 0 && (n < 0 || 0 < n--)) {
7203yield_indexed_values(
const VALUE values,
const long r,
const long *
const p)
7208 for (i = 0; i < r; i++) ARY_SET(result, i,
RARRAY_AREF(values, p[i]));
7209 ARY_SET_LEN(result, r);
7211 return !
RBASIC(values)->klass;
7227permute0(
const long n,
const long r,
long *
const p,
char *
const used,
const VALUE values)
7229 long i = 0, index = 0;
7232 const char *
const unused = memchr(&used[i], 0, n-i);
7247 for (i = 0; i < n; ++i) {
7248 if (used[i])
continue;
7250 if (!yield_indexed_values(values, r, p)) {
7266descending_factorial(
long from,
long how_many)
7271 while (--how_many > 0) {
7273 cnt = rb_int_mul(cnt,
LONG2FIX(v));
7283binomial_coefficient(
long comb,
long size)
7287 if (comb > size-comb) {
7293 else if (comb == 0) {
7297 for (i = 1; i < comb; ++i) {
7298 r = rb_int_mul(r,
LONG2FIX(size - i));
7299 r = rb_int_idiv(r,
LONG2FIX(i + 1));
7310 return descending_factorial(n, k);
7356rb_ary_permutation(
int argc,
VALUE *argv,
VALUE ary)
7370 if (r < 0 || n < r) {
7383 long *p =
ALLOCV_N(
long, t0, r+roomof(n,
sizeof(
long)));
7384 char *used = (
char*)(p + r);
7385 VALUE ary0 = ary_make_shared_copy(ary);
7386 RBASIC_CLEAR_CLASS(ary0);
7390 permute0(n, r, p, used, ary0);
7398combinate0(
const long len,
const long n,
long *
const stack,
const VALUE values)
7405 for (lev++; lev < n; lev++) {
7406 stack[lev+1] = stack[lev]+1;
7408 if (!yield_indexed_values(values, n, stack+1)) {
7412 if (lev == 0)
return;
7414 }
while (stack[lev+1]+n ==
len+lev+1);
7424 return binomial_coefficient(k, n);
7479 if (n < 0 ||
len < n) {
7491 VALUE ary0 = ary_make_shared_copy(ary);
7493 long *stack =
ALLOCV_N(
long, t0, n+1);
7495 RBASIC_CLEAR_CLASS(ary0);
7496 combinate0(
len, n, stack, ary0);
7516rpermute0(
const long n,
const long r,
long *
const p,
const VALUE values)
7518 long i = 0, index = 0;
7522 if (++index < r-1) {
7526 for (i = 0; i < n; ++i) {
7528 if (!yield_indexed_values(values, r, p)) {
7533 if (index <= 0)
return;
7534 }
while ((i = ++p[--index]) >= n);
7592rb_ary_repeated_permutation(
VALUE ary,
VALUE num)
7614 VALUE ary0 = ary_make_shared_copy(ary);
7615 RBASIC_CLEAR_CLASS(ary0);
7617 rpermute0(n, r, p, ary0);
7625rcombinate0(
const long n,
const long r,
long *
const p,
const long rest,
const VALUE values)
7627 long i = 0, index = 0;
7631 if (++index < r-1) {
7635 for (; i < n; ++i) {
7637 if (!yield_indexed_values(values, r, p)) {
7642 if (index <= 0)
return;
7643 }
while ((i = ++p[--index]) >= n);
7655 return binomial_coefficient(k, n + k - 1);
7698rb_ary_repeated_combination(
VALUE ary,
VALUE num)
7716 else if (
len == 0) {
7722 VALUE ary0 = ary_make_shared_copy(ary);
7723 RBASIC_CLEAR_CLASS(ary0);
7725 rcombinate0(
len, n, p, n, ary0);
7786rb_ary_product(
int argc,
VALUE *argv,
VALUE ary)
7792 int *counters =
ALLOCV_N(
int, t1, n);
7797 RBASIC_CLEAR_CLASS(t0);
7802 for (i = 1; i < n; i++) arrays[i] =
Qnil;
7803 for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
7806 for (i = 0; i < n; i++) counters[i] = 0;
7811 for (i = 0; i < n; i++) {
7813 arrays[i] = ary_make_shared_copy(arrays[i]);
7818 for (i = 0; i < n; i++) {
7824 if (MUL_OVERFLOW_LONG_P(resultlen, k))
7834 for (j = 0; j < n; j++) {
7839 if (
NIL_P(result)) {
7840 FL_SET(t0, RARRAY_SHARED_ROOT_FLAG);
7842 if (!
FL_TEST(t0, RARRAY_SHARED_ROOT_FLAG)) {
7846 FL_UNSET(t0, RARRAY_SHARED_ROOT_FLAG);
7859 while (counters[m] ==
RARRAY_LEN(arrays[m])) {
7862 if (--m < 0)
goto done;
7870 return NIL_P(result) ? ary : result;
7896 rb_raise(rb_eArgError,
"attempt to take negative size");
7923rb_ary_take_while(
VALUE ary)
7931 return rb_ary_take(ary,
LONG2FIX(i));
7959 rb_raise(rb_eArgError,
"attempt to drop negative size");
7986rb_ary_drop_while(
VALUE ary)
7994 return rb_ary_drop(ary,
LONG2FIX(i));
8037rb_ary_any_p(
int argc,
VALUE *argv,
VALUE ary)
8045 rb_warn(
"given block not used");
8052 for (i = 0; i <
len; ++i) {
8104rb_ary_all_p(
int argc,
VALUE *argv,
VALUE ary)
8112 rb_warn(
"given block not used");
8119 for (i = 0; i <
len; ++i) {
8165rb_ary_none_p(
int argc,
VALUE *argv,
VALUE ary)
8173 rb_warn(
"given block not used");
8180 for (i = 0; i <
len; ++i) {
8229rb_ary_one_p(
int argc,
VALUE *argv,
VALUE ary)
8238 rb_warn(
"given block not used");
8242 if (result)
return Qfalse;
8248 for (i = 0; i <
len; ++i) {
8250 if (result)
return Qfalse;
8258 if (result)
return Qfalse;
8290 self = rb_ary_at(self, *argv);
8291 if (!--argc)
return self;
8293 return rb_obj_dig(argc, argv, self,
Qnil);
8297finish_exact_sum(
long n,
VALUE r,
VALUE v,
int z)
8302 v = rb_rational_plus(r, v);
8369 if (init_is_float) {
8374 goto init_is_a_value;
8388 else if (RB_BIGNUM_TYPE_P(e))
8389 v = rb_big_plus(e, v);
8394 r = rb_rational_plus(r, e);
8399 v = finish_exact_sum(n, r, v, argc!=0);
8400 if (init_is_float) v = rb_float_plus(argv[0], v);
8404 v = finish_exact_sum(n, r, v, i!=0);
8416 goto has_float_value;
8426 else if (RB_BIGNUM_TYPE_P(e))
8433 if (isnan(f))
continue;
8439 if (isinf(f) && signbit(x) != signbit(f))
8445 if (isinf(f))
continue;
8448 if (fabs(f) >= fabs(x))
8461 goto has_some_value;
8475rb_ary_deconstruct(
VALUE ary)
8986 fake_ary_flags = init_fake_ary_flags();
9116 rb_vm_register_global_object(rb_cArray_empty_frozen);
9119#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)
Same as RB_OBJ_FROZEN(), but returns Qtrue/Qfalse instead of #bool.
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)
Same as RB_OBJ_FREEZE(), but returns the given object.
#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.
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.
#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_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.
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.
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 StringValue(v)
Ensures that the parameter object is a String.
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
#define RTEST
This is an old name of RB_TEST.
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.