14#include "debug_counter.h"
17#include "internal/array.h"
18#include "internal/compar.h"
19#include "internal/enum.h"
20#include "internal/gc.h"
21#include "internal/hash.h"
22#include "internal/numeric.h"
23#include "internal/object.h"
24#include "internal/proc.h"
25#include "internal/rational.h"
26#include "internal/vm.h"
40#include "ruby_assert.h"
43VALUE rb_cArray_empty_frozen;
72#define ARY_DEFAULT_SIZE 16
73#define ARY_MAX_SIZE (LONG_MAX / (int)sizeof(VALUE))
74#define SMALL_ARRAY_LEN 16
78should_be_T_ARRAY(
VALUE ary)
83#define ARY_HEAP_PTR(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
84#define ARY_HEAP_LEN(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
85#define ARY_HEAP_CAPA(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(!ARY_SHARED_ROOT_P(a)), \
86 RARRAY(a)->as.heap.aux.capa)
88#define ARY_EMBED_PTR(a) (RUBY_ASSERT(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
89#define ARY_EMBED_LEN(a) \
90 (RUBY_ASSERT(ARY_EMBED_P(a)), \
91 (long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
92 (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
93#define ARY_HEAP_SIZE(a) (RUBY_ASSERT(!ARY_EMBED_P(a)), RUBY_ASSERT(ARY_OWNS_HEAP_P(a)), ARY_CAPA(a) * sizeof(VALUE))
95#define ARY_OWNS_HEAP_P(a) (RUBY_ASSERT(should_be_T_ARRAY((VALUE)(a))), \
96 !FL_TEST_RAW((a), RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG))
98#define FL_SET_EMBED(a) do { \
99 RUBY_ASSERT(!ARY_SHARED_P(a)); \
100 FL_SET((a), RARRAY_EMBED_FLAG); \
104#define FL_UNSET_EMBED(ary) FL_UNSET((ary), RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK)
105#define FL_SET_SHARED(ary) do { \
106 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
107 FL_SET((ary), RARRAY_SHARED_FLAG); \
109#define FL_UNSET_SHARED(ary) FL_UNSET((ary), RARRAY_SHARED_FLAG)
111#define ARY_SET_PTR_FORCE(ary, p) \
112 (RARRAY(ary)->as.heap.ptr = (p))
113#define ARY_SET_PTR(ary, p) do { \
114 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
115 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
116 ARY_SET_PTR_FORCE(ary, p); \
118#define ARY_SET_EMBED_LEN(ary, n) do { \
120 RUBY_ASSERT(ARY_EMBED_P(ary)); \
121 RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
122 RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
124#define ARY_SET_HEAP_LEN(ary, n) do { \
125 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
126 RARRAY(ary)->as.heap.len = (n); \
128#define ARY_SET_LEN(ary, n) do { \
129 if (ARY_EMBED_P(ary)) { \
130 ARY_SET_EMBED_LEN((ary), (n)); \
133 ARY_SET_HEAP_LEN((ary), (n)); \
135 RUBY_ASSERT(RARRAY_LEN(ary) == (n)); \
137#define ARY_INCREASE_PTR(ary, n) do { \
138 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
139 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
140 RARRAY(ary)->as.heap.ptr += (n); \
142#define ARY_INCREASE_LEN(ary, n) do { \
143 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
144 if (ARY_EMBED_P(ary)) { \
145 ARY_SET_EMBED_LEN((ary), RARRAY_LEN(ary)+(n)); \
148 RARRAY(ary)->as.heap.len += (n); \
152#define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? ary_embed_capa(ary) : \
153 ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : ARY_HEAP_CAPA(ary))
154#define ARY_SET_CAPA_FORCE(ary, n) \
155 RARRAY(ary)->as.heap.aux.capa = (n);
156#define ARY_SET_CAPA(ary, n) do { \
157 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
158 RUBY_ASSERT(!ARY_SHARED_P(ary)); \
159 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
160 ARY_SET_CAPA_FORCE(ary, n); \
163#define ARY_SHARED_ROOT_OCCUPIED(ary) (!OBJ_FROZEN(ary) && ARY_SHARED_ROOT_REFCNT(ary) == 1)
164#define ARY_SET_SHARED_ROOT_REFCNT(ary, value) do { \
165 RUBY_ASSERT(ARY_SHARED_ROOT_P(ary)); \
166 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
167 RUBY_ASSERT((value) >= 0); \
168 RARRAY(ary)->as.heap.aux.capa = (value); \
170#define FL_SET_SHARED_ROOT(ary) do { \
171 RUBY_ASSERT(!OBJ_FROZEN(ary)); \
172 RUBY_ASSERT(!ARY_EMBED_P(ary)); \
173 FL_SET((ary), RARRAY_SHARED_ROOT_FLAG); \
182 RARRAY_ASET(a, i, v);
187ary_embed_capa(
VALUE ary)
189 size_t size = rb_gc_obj_slot_size(ary) - offsetof(
struct RArray, as.ary);
191 return size /
sizeof(
VALUE);
195ary_embed_size(
long capa)
197 size_t size = offsetof(
struct RArray, as.ary) + (
sizeof(
VALUE) *
capa);
198 if (size <
sizeof(
struct RArray)) size =
sizeof(
struct RArray);
203ary_embeddable_p(
long capa)
205 return rb_gc_size_allocatable_p(ary_embed_size(
capa));
209rb_ary_embeddable_p(
VALUE ary)
220 if (ARY_SHARED_ROOT_P(ary) ||
OBJ_FROZEN(ary) || ARY_SHARED_P(ary))
return false;
223 return ARY_HEAP_CAPA(ary) <= embed_len_max;
227rb_ary_size_as_embedded(
VALUE ary)
231 if (ARY_EMBED_P(ary)) {
232 real_size = ary_embed_size(ARY_EMBED_LEN(ary));
234 else if (rb_ary_embeddable_p(ary)) {
235 real_size = ary_embed_size(ARY_HEAP_CAPA(ary));
238 real_size =
sizeof(
struct RArray);
245#define ary_verify(ary) ary_verify_(ary, __FILE__, __LINE__)
248ary_verify_(
VALUE ary,
const char *file,
int line)
252 if (ARY_SHARED_P(ary)) {
253 VALUE root = ARY_SHARED_ROOT(ary);
254 const VALUE *
ptr = ARY_HEAP_PTR(ary);
261 else if (ARY_EMBED_P(ary)) {
270 for (i=0; i<
len; i++) {
279#define ary_verify(ary) ((void)0)
283rb_ary_ptr_use_start(
VALUE ary)
292rb_ary_ptr_use_end(
VALUE ary)
308ary_mem_clear(
VALUE ary,
long beg,
long size)
316memfill(
register VALUE *mem,
register long size,
register VALUE val)
324ary_memfill(
VALUE ary,
long beg,
long size,
VALUE val)
327 memfill(
ptr + beg, size, val);
333ary_memcpy0(
VALUE ary,
long beg,
long argc,
const VALUE *argv,
VALUE buff_owner_ary)
337 if (argc > (
int)(128/
sizeof(
VALUE)) ) {
338 rb_gc_writebarrier_remember(buff_owner_ary);
346 for (i=0; i<argc; i++) {
354ary_memcpy(
VALUE ary,
long beg,
long argc,
const VALUE *argv)
356 ary_memcpy0(ary, beg, argc, argv, ary);
360ary_heap_alloc_buffer(
size_t capa)
368 ruby_sized_xfree((
void *)
ptr, size);
372ary_heap_free(
VALUE ary)
374 ary_heap_free_ptr(ary, ARY_HEAP_PTR(ary), ARY_HEAP_SIZE(ary));
378ary_heap_realloc(
VALUE ary,
size_t new_capa)
381 SIZED_REALLOC_N(
RARRAY(ary)->as.heap.ptr,
VALUE, new_capa, ARY_HEAP_CAPA(ary));
388rb_ary_make_embedded(
VALUE ary)
391 if (!ARY_EMBED_P(ary)) {
392 const VALUE *buf = ARY_HEAP_PTR(ary);
393 long len = ARY_HEAP_LEN(ary);
396 ARY_SET_EMBED_LEN(ary,
len);
400 ary_heap_free_ptr(ary, buf,
len *
sizeof(
VALUE));
405ary_resize_capa(
VALUE ary,
long capacity)
411 if (capacity > ary_embed_capa(ary)) {
412 size_t new_capa = capacity;
413 if (ARY_EMBED_P(ary)) {
414 long len = ARY_EMBED_LEN(ary);
415 VALUE *
ptr = ary_heap_alloc_buffer(capacity);
419 ARY_SET_PTR(ary,
ptr);
420 ARY_SET_HEAP_LEN(ary,
len);
423 new_capa = ary_heap_realloc(ary, capacity);
425 ARY_SET_CAPA(ary, new_capa);
428 if (!ARY_EMBED_P(ary)) {
429 long len = ARY_HEAP_LEN(ary);
430 long old_capa = ARY_HEAP_CAPA(ary);
431 const VALUE *
ptr = ARY_HEAP_PTR(ary);
433 if (
len > capacity)
len = capacity;
435 ary_heap_free_ptr(ary,
ptr, old_capa);
438 ARY_SET_LEN(ary,
len);
446ary_shrink_capa(
VALUE ary)
448 long capacity = ARY_HEAP_LEN(ary);
449 long old_capa = ARY_HEAP_CAPA(ary);
452 if (old_capa > capacity) {
453 size_t new_capa = ary_heap_realloc(ary, capacity);
454 ARY_SET_CAPA(ary, new_capa);
461ary_double_capa(
VALUE ary,
long min)
463 long new_capa = ARY_CAPA(ary) / 2;
465 if (new_capa < ARY_DEFAULT_SIZE) {
466 new_capa = ARY_DEFAULT_SIZE;
468 if (new_capa >= ARY_MAX_SIZE - min) {
469 new_capa = (ARY_MAX_SIZE - min) / 2;
472 ary_resize_capa(ary, new_capa);
478rb_ary_decrement_share(
VALUE shared_root)
481 long num = ARY_SHARED_ROOT_REFCNT(shared_root);
482 ARY_SET_SHARED_ROOT_REFCNT(shared_root, num - 1);
487rb_ary_unshare(
VALUE ary)
489 VALUE shared_root = ARY_SHARED_ROOT(ary);
490 rb_ary_decrement_share(shared_root);
491 FL_UNSET_SHARED(ary);
495rb_ary_reset(
VALUE ary)
497 if (ARY_OWNS_HEAP_P(ary)) {
500 else if (ARY_SHARED_P(ary)) {
505 ARY_SET_EMBED_LEN(ary, 0);
509rb_ary_increment_share(
VALUE shared_root)
512 long num = ARY_SHARED_ROOT_REFCNT(shared_root);
514 ARY_SET_SHARED_ROOT_REFCNT(shared_root, num + 1);
520rb_ary_set_shared(
VALUE ary,
VALUE shared_root)
526 rb_ary_increment_share(shared_root);
530 RB_DEBUG_COUNTER_INC(obj_ary_shared_create);
534rb_ary_modify_check(
VALUE ary)
538 rb_check_frozen(ary);
543rb_ary_cancel_sharing(
VALUE ary)
545 if (ARY_SHARED_P(ary)) {
547 VALUE shared_root = ARY_SHARED_ROOT(ary);
549 ary_verify(shared_root);
551 if (
len <= ary_embed_capa(ary)) {
552 const VALUE *
ptr = ARY_HEAP_PTR(ary);
553 FL_UNSET_SHARED(ary);
556 rb_ary_decrement_share(shared_root);
557 ARY_SET_EMBED_LEN(ary,
len);
559 else if (ARY_SHARED_ROOT_OCCUPIED(shared_root) &&
len > ((shared_len =
RARRAY_LEN(shared_root))>>1)) {
561 FL_UNSET_SHARED(ary);
563 ARY_SET_CAPA(ary, shared_len);
567 FL_SET_EMBED(shared_root);
568 rb_ary_decrement_share(shared_root);
574 ARY_SET_CAPA_FORCE(ary,
len);
575 ARY_SET_PTR_FORCE(ary,
ptr);
578 rb_gc_writebarrier_remember(ary);
586 rb_ary_modify_check(ary);
587 rb_ary_cancel_sharing(ary);
591ary_ensure_room_for_push(
VALUE ary,
long add_len)
594 long new_len = old_len + add_len;
597 if (old_len > ARY_MAX_SIZE - add_len) {
600 if (ARY_SHARED_P(ary)) {
601 if (new_len > ary_embed_capa(ary)) {
602 VALUE shared_root = ARY_SHARED_ROOT(ary);
603 if (ARY_SHARED_ROOT_OCCUPIED(shared_root)) {
605 rb_ary_modify_check(ary);
608 ary_verify(shared_root);
614 capa = ARY_CAPA(ary);
616 ary_double_capa(ary, new_len);
627 rb_ary_modify_check(ary);
629 capa = ARY_CAPA(ary);
630 if (new_len >
capa) {
631 ary_double_capa(ary, new_len);
662 if (!ARY_EMBED_P(ary) && !ARY_SHARED_P(ary) && !ARY_SHARED_ROOT_P(ary)) {
663 ary_shrink_capa(ary);
679 if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) &&
680 !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) &&
681 ARY_SHARED_ROOT(ary1) == ARY_SHARED_ROOT(ary2) &&
682 ARY_HEAP_LEN(ary1) == ARY_HEAP_LEN(ary2)) {
691 size_t size = ary_embed_size(
capa);
693 NEWOBJ_OF(ary,
struct RArray, klass,
704ary_alloc_heap(
VALUE klass)
706 NEWOBJ_OF(ary,
struct RArray, klass,
708 sizeof(
struct RArray), 0);
710 ary->as.heap.len = 0;
711 ary->as.heap.aux.capa = 0;
712 ary->as.heap.ptr = NULL;
718empty_ary_alloc(
VALUE klass)
720 RUBY_DTRACE_CREATE_HOOK(ARRAY, 0);
721 return ary_alloc_embed(klass, 0);
732 rb_raise(rb_eArgError,
"negative array size (or size too big)");
734 if (
capa > ARY_MAX_SIZE) {
735 rb_raise(rb_eArgError,
"array size too big");
738 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
740 if (ary_embeddable_p(
capa)) {
741 ary = ary_alloc_embed(klass,
capa);
744 ary = ary_alloc_heap(klass);
745 ARY_SET_CAPA(ary,
capa);
748 ARY_SET_PTR(ary, ary_heap_alloc_buffer(
capa));
749 ARY_SET_HEAP_LEN(ary, 0);
768(rb_ary_new_from_args)(
long n, ...)
777 for (i=0; i<n; i++) {
778 ARY_SET(ary, i, va_arg(ar,
VALUE));
787rb_ary_tmp_new_from_values(
VALUE klass,
long n,
const VALUE *elts)
791 ary = ary_new(klass, n);
793 ary_memcpy(ary, 0, n, elts);
803 return rb_ary_tmp_new_from_values(
rb_cArray, n, elts);
807ec_ary_alloc_embed(rb_execution_context_t *ec,
VALUE klass,
long capa)
809 size_t size = ary_embed_size(
capa);
811 NEWOBJ_OF(ary,
struct RArray, klass,
822ec_ary_alloc_heap(rb_execution_context_t *ec,
VALUE klass)
824 NEWOBJ_OF(ary,
struct RArray, klass,
826 sizeof(
struct RArray), ec);
828 ary->as.heap.len = 0;
829 ary->as.heap.aux.capa = 0;
830 ary->as.heap.ptr = NULL;
836ec_ary_new(rb_execution_context_t *ec,
VALUE klass,
long capa)
841 rb_raise(rb_eArgError,
"negative array size (or size too big)");
843 if (
capa > ARY_MAX_SIZE) {
844 rb_raise(rb_eArgError,
"array size too big");
847 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
849 if (ary_embeddable_p(
capa)) {
850 ary = ec_ary_alloc_embed(ec, klass,
capa);
853 ary = ec_ary_alloc_heap(ec, klass);
854 ARY_SET_CAPA(ary,
capa);
857 ARY_SET_PTR(ary, ary_heap_alloc_buffer(
capa));
858 ARY_SET_HEAP_LEN(ary, 0);
865rb_ec_ary_new_from_values(rb_execution_context_t *ec,
long n,
const VALUE *elts)
871 ary_memcpy(ary, 0, n, elts);
886rb_ary_hidden_new_fill(
long capa)
890 ARY_SET_LEN(ary,
capa);
897 if (ARY_OWNS_HEAP_P(ary)) {
898 if (USE_DEBUG_COUNTER &&
899 !ARY_SHARED_ROOT_P(ary) &&
901 RB_DEBUG_COUNTER_INC(obj_ary_extracapa);
904 RB_DEBUG_COUNTER_INC(obj_ary_ptr);
908 RB_DEBUG_COUNTER_INC(obj_ary_embed);
911 if (ARY_SHARED_P(ary)) {
912 RB_DEBUG_COUNTER_INC(obj_ary_shared);
914 if (ARY_SHARED_ROOT_P(ary) && ARY_SHARED_ROOT_OCCUPIED(ary)) {
915 RB_DEBUG_COUNTER_INC(obj_ary_shared_root_occupied);
919static VALUE fake_ary_flags;
922init_fake_ary_flags(
void)
924 struct RArray fake_ary = {0};
932rb_setup_fake_ary(
struct RArray *fake_ary,
const VALUE *list,
long len)
935 RBASIC_CLEAR_CLASS((
VALUE)fake_ary);
938 fake_ary->as.heap.ptr = list;
939 fake_ary->as.heap.len =
len;
940 fake_ary->as.heap.aux.capa =
len;
941 return (
VALUE)fake_ary;
945rb_ary_memsize(
VALUE ary)
947 if (ARY_OWNS_HEAP_P(ary)) {
948 return ARY_CAPA(ary) *
sizeof(
VALUE);
956ary_make_shared(
VALUE ary)
960 if (ARY_SHARED_P(ary)) {
961 return ARY_SHARED_ROOT(ary);
963 else if (ARY_SHARED_ROOT_P(ary)) {
970 long capa = ARY_CAPA(ary);
975 VALUE shared = ary_alloc_heap(0);
976 FL_SET_SHARED_ROOT(shared);
978 if (ARY_EMBED_P(ary)) {
980 ARY_SET_PTR(shared,
ptr);
984 ARY_SET_HEAP_LEN(ary,
len);
985 ARY_SET_PTR(ary,
ptr);
991 ARY_SET_LEN(shared,
capa);
993 rb_ary_set_shared(ary, shared);
1003ary_make_substitution(
VALUE ary)
1007 if (ary_embeddable_p(
len)) {
1012 ARY_SET_EMBED_LEN(subst,
len);
1016 return rb_ary_increment_share(ary_make_shared(ary));
1027rb_to_array_type(
VALUE ary)
1029 return rb_convert_type_with_id(ary,
T_ARRAY,
"Array", idTo_ary);
1031#define to_ary rb_to_array_type
1036 return rb_check_convert_type_with_id(ary,
T_ARRAY,
"Array", idTo_ary);
1040rb_check_to_array(
VALUE ary)
1042 return rb_check_convert_type_with_id(ary,
T_ARRAY,
"Array", idTo_a);
1046rb_to_array(
VALUE ary)
1048 return rb_convert_type_with_id(ary,
T_ARRAY,
"Array", idTo_a);
1077rb_ary_s_new(
int argc,
VALUE *argv,
VALUE klass)
1083 if (argc > 0 &&
FIXNUM_P(argv[0])) {
1085 if (size < 0) size = 0;
1088 ary = ary_new(klass, size);
1151rb_ary_initialize(
int argc,
VALUE *argv,
VALUE ary)
1167 if (argc == 1 && !
FIXNUM_P(size)) {
1178 rb_raise(rb_eArgError,
"negative array size");
1180 if (
len > ARY_MAX_SIZE) {
1181 rb_raise(rb_eArgError,
"array size too big");
1185 ARY_SET_LEN(ary, 0);
1186 ary_resize_capa(ary,
len);
1191 rb_warn(
"block supersedes default value argument");
1193 for (i=0; i<
len; i++) {
1195 ARY_SET_LEN(ary, i + 1);
1199 ary_memfill(ary, 0,
len, val);
1200 ARY_SET_LEN(ary,
len);
1216rb_ary_s_create(
int argc,
VALUE *argv,
VALUE klass)
1218 VALUE ary = ary_new(klass, argc);
1219 if (argc > 0 && argv) {
1220 ary_memcpy(ary, 0, argc, argv);
1221 ARY_SET_LEN(ary, argc);
1235 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
1239 else if (idx >= ARY_MAX_SIZE) {
1244 if (idx >= ARY_CAPA(ary)) {
1245 ary_double_capa(ary, idx);
1248 ary_mem_clear(ary,
len, idx -
len + 1);
1252 ARY_SET_LEN(ary, idx + 1);
1254 ARY_SET(ary, idx, val);
1258ary_make_partial(
VALUE ary,
VALUE klass,
long offset,
long len)
1264 VALUE result = ary_alloc_heap(klass);
1265 size_t embed_capa = ary_embed_capa(result);
1266 if ((
size_t)
len <= embed_capa) {
1267 FL_SET_EMBED(result);
1269 ARY_SET_EMBED_LEN(result,
len);
1272 VALUE shared = ary_make_shared(ary);
1277 FL_UNSET_EMBED(result);
1281 rb_ary_set_shared(result, shared);
1283 ARY_INCREASE_PTR(result, offset);
1284 ARY_SET_LEN(result,
len);
1294ary_make_partial_step(
VALUE ary,
VALUE klass,
long offset,
long len,
long step)
1301 const long orig_len =
len;
1303 if (step > 0 && step >=
len) {
1304 VALUE result = ary_new(klass, 1);
1309 ARY_SET_EMBED_LEN(result, 1);
1312 else if (step < 0 && step < -
len) {
1316 long ustep = (step < 0) ? -step : step;
1317 len = roomof(
len, ustep);
1320 long j = offset + ((step > 0) ? 0 : (orig_len - 1));
1322 VALUE result = ary_new(klass,
len);
1323 if (ARY_EMBED_P(result)) {
1327 for (i = 0; i <
len; ++i) {
1331 ARY_SET_EMBED_LEN(result,
len);
1337 for (i = 0; i <
len; ++i) {
1342 ARY_SET_LEN(result,
len);
1349ary_make_shared_copy(
VALUE ary)
1354enum ary_take_pos_flags
1361ary_take_first_or_last_n(
VALUE ary,
long n,
enum ary_take_pos_flags last)
1370 rb_raise(rb_eArgError,
"negative array size");
1375 return ary_make_partial(ary,
rb_cArray, offset, n);
1379ary_take_first_or_last(
int argc,
const VALUE *argv,
VALUE ary,
enum ary_take_pos_flags last)
1381 argc = rb_check_arity(argc, 0, 1);
1386 return ary_take_first_or_last_n(ary,
NUM2LONG(argv[0]), last);
1407 long idx =
RARRAY_LEN((ary_verify(ary), ary));
1408 VALUE target_ary = ary_ensure_room_for_push(ary, 1);
1412 ARY_SET_LEN(ary, idx + 1);
1421 VALUE target_ary = ary_ensure_room_for_push(ary,
len);
1422 ary_memcpy0(ary, oldlen,
len, argv, target_ary);
1423 ARY_SET_LEN(ary, oldlen +
len);
1446rb_ary_push_m(
int argc,
VALUE *argv,
VALUE ary)
1455 rb_ary_modify_check(ary);
1457 if (n == 0)
return Qnil;
1458 if (ARY_OWNS_HEAP_P(ary) &&
1459 n * 3 < ARY_CAPA(ary) &&
1460 ARY_CAPA(ary) > ARY_DEFAULT_SIZE)
1462 ary_resize_capa(ary, n * 2);
1467 ARY_SET_LEN(ary, n - 1);
1503rb_ary_pop_m(
int argc,
VALUE *argv,
VALUE ary)
1511 rb_ary_modify_check(ary);
1512 result = ary_take_first_or_last(argc, argv, ary, ARY_TAKE_LAST);
1513 rb_ary_modify_check(ary);
1526 rb_ary_modify_check(ary);
1532 rb_ary_behead(ary, 1);
1576rb_ary_shift_m(
int argc,
VALUE *argv,
VALUE ary)
1585 rb_ary_modify_check(ary);
1586 result = ary_take_first_or_last(argc, argv, ary, ARY_TAKE_FIRST);
1588 rb_ary_behead(ary,n);
1594rb_ary_behead(
VALUE ary,
long n)
1600 rb_ary_modify_check(ary);
1602 if (!ARY_SHARED_P(ary)) {
1603 if (ARY_EMBED_P(ary) ||
RARRAY_LEN(ary) < ARY_DEFAULT_SIZE) {
1607 ARY_INCREASE_LEN(ary, -n);
1612 ary_mem_clear(ary, 0, n);
1613 ary_make_shared(ary);
1615 else if (ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(ary))) {
1616 ary_mem_clear(ary, 0, n);
1619 ARY_INCREASE_PTR(ary, n);
1620 ARY_INCREASE_LEN(ary, -n);
1629 if (head - sharedp < argc) {
1630 long room =
capa -
len - argc;
1634 head = sharedp + argc + room;
1636 ARY_SET_PTR(ary, head - argc);
1637 RUBY_ASSERT(ARY_SHARED_ROOT_OCCUPIED(ARY_SHARED_ROOT(ary)));
1640 return ARY_SHARED_ROOT(ary);
1644ary_modify_for_unshift(
VALUE ary,
int argc)
1647 long new_len =
len + argc;
1649 const VALUE *head, *sharedp;
1652 capa = ARY_CAPA(ary);
1653 if (
capa - (
capa >> 6) <= new_len) {
1654 ary_double_capa(ary, new_len);
1658 if (new_len > ARY_DEFAULT_SIZE * 4 && !ARY_EMBED_P(ary)) {
1662 capa = ARY_CAPA(ary);
1663 ary_make_shared(ary);
1666 return make_room_for_unshift(ary, head, (
void *)sharedp, argc,
capa,
len);
1680ary_ensure_room_for_unshift(
VALUE ary,
int argc)
1683 long new_len =
len + argc;
1685 if (
len > ARY_MAX_SIZE - argc) {
1688 else if (! ARY_SHARED_P(ary)) {
1689 return ary_modify_for_unshift(ary, argc);
1692 VALUE shared_root = ARY_SHARED_ROOT(ary);
1695 if (! ARY_SHARED_ROOT_OCCUPIED(shared_root)) {
1696 return ary_modify_for_unshift(ary, argc);
1698 else if (new_len >
capa) {
1699 return ary_modify_for_unshift(ary, argc);
1705 rb_ary_modify_check(ary);
1706 return make_room_for_unshift(ary, head, sharedp, argc,
capa,
len);
1726rb_ary_unshift_m(
int argc,
VALUE *argv,
VALUE ary)
1732 rb_ary_modify_check(ary);
1736 target_ary = ary_ensure_room_for_unshift(ary, argc);
1737 ary_memcpy0(ary, 0, argc, argv, target_ary);
1738 ARY_SET_LEN(ary,
len + argc);
1745 return rb_ary_unshift_m(1, &item, ary);
1750rb_ary_elt(
VALUE ary,
long offset)
1754 if (offset < 0 ||
len <= offset) {
1763 return rb_ary_entry_internal(ary, offset);
1767rb_ary_subseq_step(
VALUE ary,
long beg,
long len,
long step)
1772 if (beg > alen)
return Qnil;
1773 if (beg < 0 ||
len < 0)
return Qnil;
1775 if (alen <
len || alen < beg +
len) {
1779 if (
len == 0)
return ary_new(klass, 0);
1781 rb_raise(rb_eArgError,
"slice step cannot be zero");
1783 return ary_make_partial(ary, klass, beg,
len);
1785 return ary_make_partial_step(ary, klass, beg,
len, step);
1791 return rb_ary_subseq_step(ary, beg,
len, 1);
1921 rb_check_arity(argc, 1, 2);
1923 return rb_ary_aref2(ary, argv[0], argv[1]);
1925 return rb_ary_aref1(ary, argv[0]);
1942 long beg,
len, step;
1949 switch (rb_arithmetic_sequence_beg_len_step(arg, &beg, &
len, &step,
RARRAY_LEN(ary), 0)) {
1955 return rb_ary_subseq_step(ary, beg,
len, step);
1993rb_ary_first(
int argc,
VALUE *argv,
VALUE ary)
2000 return ary_take_first_or_last(argc, argv, ary, ARY_TAKE_FIRST);
2006ary_first(
VALUE self)
2019rb_ary_last(
int argc,
const VALUE *argv,
VALUE ary)
2022 return ary_last(ary);
2025 return ary_take_first_or_last(argc, argv, ary, ARY_TAKE_LAST);
2070rb_ary_fetch(
int argc,
VALUE *argv,
VALUE ary)
2078 if (block_given && argc == 2) {
2079 rb_warn(
"block supersedes default value argument");
2087 if (block_given)
return rb_yield(pos);
2089 rb_raise(
rb_eIndexError,
"index %ld outside of array bounds: %ld...%ld",
2123 if_none = rb_check_arity(argc, 0, 1) ? argv[0] :
Qnil;
2126 for (idx = 0; idx <
RARRAY_LEN(ary); idx++) {
2133 if (!
NIL_P(if_none)) {
2134 return rb_funcallv(if_none, idCall, 0, 0);
2161rb_ary_rfind(
int argc,
VALUE *argv,
VALUE ary)
2166 if_none = rb_check_arity(argc, 0, 1) ? argv[0] :
Qnil;
2177 idx = (idx >=
len) ?
len : idx;
2180 if (!
NIL_P(if_none)) {
2181 return rb_funcallv(if_none, idCall, 0, 0);
2221rb_ary_index(
int argc,
VALUE *argv,
VALUE ary)
2235 rb_check_arity(argc, 0, 1);
2238 rb_warn(
"given block not used");
2277rb_ary_rindex(
int argc,
VALUE *argv,
VALUE ary)
2293 rb_check_arity(argc, 0, 1);
2296 rb_warn(
"given block not used");
2314 if (!
NIL_P(tmp))
return tmp;
2319rb_ary_splice(
VALUE ary,
long beg,
long len,
const VALUE *rptr,
long rlen,
int self_insert)
2328 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2332 if (olen <
len || olen < beg +
len) {
2338 if (beg > ARY_MAX_SIZE - rlen) {
2341 target_ary = ary_ensure_room_for_push(ary, rlen-
len);
2343 ary_mem_clear(ary, olen, beg - olen);
2347 ary_memcpy0(ary, beg, rlen, rptr, target_ary);
2349 ARY_SET_LEN(ary,
len);
2354 if (olen -
len > ARY_MAX_SIZE - rlen) {
2358 alen = olen + rlen -
len;
2359 if (alen >= ARY_CAPA(ary)) {
2360 ary_double_capa(ary, alen);
2367 ARY_SET_LEN(ary, alen);
2371 rb_gc_writebarrier_remember(ary);
2393 rb_ary_modify_check(ary);
2394 if (ARY_SHARED_P(ary)) {
2397 if (
len > (
capa = (
long)ARY_CAPA(ary))) {
2398 rb_bug(
"probable buffer overflow: %ld for %ld",
len,
capa);
2400 ARY_SET_LEN(ary,
len);
2410 if (
len == olen)
return ary;
2411 if (
len > ARY_MAX_SIZE) {
2415 if (
len > ARY_CAPA(ary)) {
2416 ary_double_capa(ary,
len);
2418 ary_mem_clear(ary, olen,
len - olen);
2419 ARY_SET_LEN(ary,
len);
2421 else if (ARY_EMBED_P(ary)) {
2422 ARY_SET_EMBED_LEN(ary,
len);
2424 else if (
len <= ary_embed_capa(ary)) {
2425 const VALUE *
ptr = ARY_HEAP_PTR(ary);
2426 long ptr_capa = ARY_HEAP_SIZE(ary);
2427 bool is_malloc_ptr = !ARY_SHARED_P(ary);
2432 ARY_SET_EMBED_LEN(ary,
len);
2434 if (is_malloc_ptr) ruby_sized_xfree((
void *)
ptr, ptr_capa);
2437 if (olen >
len + ARY_DEFAULT_SIZE) {
2438 size_t new_capa = ary_heap_realloc(ary,
len);
2439 ARY_SET_CAPA(ary, new_capa);
2441 ARY_SET_HEAP_LEN(ary,
len);
2448ary_aset_by_rb_ary_store(
VALUE ary,
long key,
VALUE val)
2455ary_aset_by_rb_ary_splice(
VALUE ary,
long beg,
long len,
VALUE val)
2610 long offset, beg,
len;
2612 rb_check_arity(argc, 2, 3);
2613 rb_ary_modify_check(ary);
2617 return ary_aset_by_rb_ary_splice(ary, beg,
len, argv[2]);
2621 return ary_aset_by_rb_ary_store(ary, offset, argv[1]);
2625 return ary_aset_by_rb_ary_splice(ary, beg,
len, argv[1]);
2629 return ary_aset_by_rb_ary_store(ary, offset, argv[1]);
2669rb_ary_insert(
int argc,
VALUE *argv,
VALUE ary)
2674 rb_ary_modify_check(ary);
2676 if (argc == 1)
return ary;
2683 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2688 rb_ary_splice(ary, pos, 0, argv + 1, argc - 1, FALSE);
2693rb_ary_length(
VALUE ary);
2698 return rb_ary_length(ary);
2795rb_ary_each_index(
VALUE ary)
2831rb_ary_reverse_each(
VALUE ary)
2862rb_ary_length(
VALUE ary)
2879rb_ary_empty_p(
VALUE ary)
2890 ARY_SET_LEN(dup,
len);
2908recursive_join(
VALUE obj,
VALUE argp,
int recur)
2913 VALUE result = arg[2];
2914 int *first = (
int *)arg[3];
2917 rb_raise(rb_eArgError,
"recursive array join");
2920 ary_join_1(obj, ary, sep, 0, result, first);
2931 if (max > 0) rb_enc_copy(result,
RARRAY_AREF(ary, 0));
2932 for (i=0; i<max; i++) {
2934 if (!RB_TYPE_P(val,
T_STRING))
break;
2935 if (i > 0 && !
NIL_P(sep))
2943ary_join_1_str(
VALUE dst,
VALUE src,
int *first)
2947 rb_enc_copy(dst, src);
2956 rb_raise(rb_eArgError,
"recursive array join");
2965 args[3] = (
VALUE)first;
2976 if (i > 0 && !
NIL_P(sep))
2981 ary_join_1_str(result, val, first);
2983 else if (RB_TYPE_P(val,
T_ARRAY)) {
2984 ary_join_1_ary(val, ary, sep, result, val, first);
2987 ary_join_1_str(result, tmp, first);
2990 ary_join_1_ary(val, ary, sep, result, tmp, first);
3002 VALUE val, tmp, result;
3011 for (i=0; i < len_memo; i++) {
3013 if (RB_UNLIKELY(!RB_TYPE_P(val,
T_STRING))) {
3015 if (
NIL_P(tmp) || tmp != val) {
3020 rb_enc_associate(result, rb_usascii_encoding());
3021 i = ary_join_0(ary, sep, i, result);
3023 ary_join_1(ary, ary, sep, i, result, &first);
3026 len += RSTRING_LEN(tmp);
3030 len += RSTRING_LEN(val);
3037 ary_join_0(ary, sep,
RARRAY_LEN(ary), result);
3072rb_ary_join_m(
int argc,
VALUE *argv,
VALUE ary)
3076 if (rb_check_arity(argc, 0, 1) == 0 ||
NIL_P(sep = argv[0])) {
3087inspect_ary(
VALUE ary,
VALUE dummy,
int recur)
3097 else rb_enc_copy(str, s);
3119rb_ary_inspect(
VALUE ary)
3128 return rb_ary_inspect(ary);
3149rb_ary_to_a(
VALUE ary)
3185rb_ary_to_h(
VALUE ary)
3192 const VALUE e = rb_ary_elt(ary, i);
3193 const VALUE elt = block_given ? rb_yield_force_blockarg(e) : e;
3195 if (
NIL_P(key_value_pair)) {
3196 rb_raise(
rb_eTypeError,
"wrong element type %"PRIsVALUE
" at %ld (expected array)",
3200 rb_raise(rb_eArgError,
"wrong array length at %ld (expected 2, was %ld)",
3216rb_ary_to_ary_m(
VALUE ary)
3241 ary_reverse(p1, p2);
3262rb_ary_reverse_bang(
VALUE ary)
3279rb_ary_reverse_m(
VALUE ary)
3287 do *p2-- = *p1++;
while (--
len > 0);
3288 rb_gc_writebarrier_remember(dup);
3295rotate_count(
long cnt,
long len)
3297 return (cnt < 0) ? (
len - (~cnt %
len) - 1) : (cnt %
len);
3308 else if (cnt ==
len - 1) {
3316 if (--cnt > 0) ary_reverse(
ptr,
ptr + cnt);
3328 if (
len > 1 && (cnt = rotate_count(cnt,
len)) > 0) {
3369rb_ary_rotate_bang(
int argc,
VALUE *argv,
VALUE ary)
3371 long n = (rb_check_arity(argc, 0, 1) ?
NUM2LONG(argv[0]) : 1);
3410rb_ary_rotate_m(
int argc,
VALUE *argv,
VALUE ary)
3415 long cnt = (rb_check_arity(argc, 0, 1) ?
NUM2LONG(argv[0]) : 1);
3420 cnt = rotate_count(cnt,
len);
3423 ary_memcpy(rotated, 0,
len,
ptr + cnt);
3424 ary_memcpy(rotated,
len, cnt,
ptr);
3430struct ary_sort_data {
3436sort_reentered(
VALUE ary)
3438 if (
RBASIC(ary)->klass) {
3445sort_returned(
struct ary_sort_data *data)
3450 sort_reentered(data->ary);
3454sort_1(
const void *ap,
const void *bp,
void *dummy)
3456 struct ary_sort_data *data = dummy;
3457 VALUE retval = sort_reentered(data->ary);
3465 n = rb_cmpint(retval, a, b);
3466 sort_returned(data);
3471sort_2(
const void *ap,
const void *bp,
void *dummy)
3473 struct ary_sort_data *data = dummy;
3474 VALUE retval = sort_reentered(data->ary);
3479 if ((
long)a > (
long)b)
return 1;
3480 if ((
long)a < (
long)b)
return -1;
3483 if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(STRING)) {
3486 if (RB_FLOAT_TYPE_P(a) && CMP_OPTIMIZABLE(FLOAT)) {
3487 return rb_float_cmp(a, b);
3490 retval = rb_funcallv(a, id_cmp, 1, &b);
3491 n = rb_cmpint(retval, a, b);
3492 sort_returned(data);
3513 VALUE tmp = ary_make_substitution(ary);
3514 struct ary_sort_data data;
3516 RBASIC_CLEAR_CLASS(tmp);
3518 data.receiver = ary;
3524 if (ARY_EMBED_P(tmp)) {
3525 if (ARY_SHARED_P(ary)) {
3526 rb_ary_unshare(ary);
3529 if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) {
3530 ary_resize_capa(ary, ARY_EMBED_LEN(tmp));
3532 ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp));
3533 ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
3536 if (!ARY_EMBED_P(ary) && ARY_HEAP_PTR(ary) == ARY_HEAP_PTR(tmp)) {
3537 FL_UNSET_SHARED(ary);
3542 if (ARY_EMBED_P(ary)) {
3543 FL_UNSET_EMBED(ary);
3545 else if (ARY_SHARED_P(ary)) {
3547 rb_ary_unshare(ary);
3552 ARY_SET_PTR(ary, ARY_HEAP_PTR(tmp));
3553 ARY_SET_HEAP_LEN(ary,
len);
3554 ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
3558 ARY_SET_EMBED_LEN(tmp, 0);
3626rb_ary_bsearch(
VALUE ary)
3628 VALUE index_result = rb_ary_bsearch_index(ary);
3633 return index_result;
3650rb_ary_bsearch_index(
VALUE ary)
3653 int smaller = 0, satisfied = 0;
3657 while (low < high) {
3658 mid = low + ((high - low) / 2);
3665 else if (v ==
Qtrue) {
3669 else if (!
RTEST(v)) {
3674 switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
3676 case 1: smaller = 0;
break;
3677 case -1: smaller = 1;
3682 " (must be numeric, true, false or nil)",
3692 if (!satisfied)
return Qnil;
3726rb_ary_sort_by_bang(
VALUE ary)
3733 sorted =
rb_block_call(ary, rb_intern(
"sort_by"), 0, 0, sort_by_i, 0);
3761rb_ary_collect(
VALUE ary)
3796rb_ary_collect_bang(
VALUE ary)
3812 long beg,
len, i, j;
3814 for (i=0; i<argc; i++) {
3821 long end = olen < beg+
len ? olen : beg+
len;
3822 for (j = beg; j < end; j++) {
3848 const long end = beg +
len;
3972rb_ary_values_at(
int argc,
VALUE *argv,
VALUE ary)
3976 for (i = 0; i < argc; ++i) {
3977 append_values_at_single(result, ary, olen, argv[i]);
4005rb_ary_select(
VALUE ary)
4020struct select_bang_arg {
4026select_bang_i(
VALUE a)
4028 volatile struct select_bang_arg *arg = (
void *)a;
4029 VALUE ary = arg->ary;
4032 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4040 return (i1 == i2) ?
Qnil : ary;
4044select_bang_ensure(
VALUE a)
4046 volatile struct select_bang_arg *arg = (
void *)a;
4047 VALUE ary = arg->ary;
4049 long i1 = arg->len[0], i2 = arg->len[1];
4051 if (i2 <
len && i2 < i1) {
4060 ARY_SET_LEN(ary, i2 + tail);
4088rb_ary_select_bang(
VALUE ary)
4090 struct select_bang_arg args;
4096 args.len[0] = args.len[1] = 0;
4117rb_ary_keep_if(
VALUE ary)
4120 rb_ary_select_bang(ary);
4125ary_resize_smaller(
VALUE ary,
long len)
4129 ARY_SET_LEN(ary,
len);
4130 if (
len * 2 < ARY_CAPA(ary) &&
4131 ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
4132 ary_resize_capa(ary,
len * 2);
4180 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4199 ary_resize_smaller(ary, i2);
4210 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4225 ary_resize_smaller(ary, i2);
4237 if (pos < 0)
return Qnil;
4245 ARY_INCREASE_LEN(ary, -1);
4285ary_slice_bang_by_rb_ary_splice(
VALUE ary,
long pos,
long len)
4292 else if (pos < -orig_len) {
4298 else if (orig_len < pos) {
4301 if (orig_len < pos +
len) {
4302 len = orig_len - pos;
4309 rb_ary_splice(ary, pos,
len, 0, 0, FALSE);
4407rb_ary_slice_bang(
int argc,
VALUE *argv,
VALUE ary)
4412 rb_ary_modify_check(ary);
4413 rb_check_arity(argc, 1, 2);
4419 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4426 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4455reject_bang_i(
VALUE a)
4457 volatile struct select_bang_arg *arg = (
void *)a;
4458 VALUE ary = arg->ary;
4461 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4469 return (i1 == i2) ?
Qnil : ary;
4473ary_reject_bang(
VALUE ary)
4475 struct select_bang_arg args;
4476 rb_ary_modify_check(ary);
4478 args.len[0] = args.len[1] = 0;
4503rb_ary_reject_bang(
VALUE ary)
4507 return ary_reject_bang(ary);
4528rb_ary_reject(
VALUE ary)
4534 ary_reject(ary, rejected_ary);
4535 return rejected_ary;
4556rb_ary_delete_if(
VALUE ary)
4560 ary_reject_bang(ary);
4575take_items(
VALUE obj,
long n)
4580 if (n == 0)
return result;
4583 args[0] = result; args[1] = (
VALUE)n;
4584 if (UNDEF_P(rb_check_block_call(obj, idEach, 0, 0, take_i, (
VALUE)args)))
4585 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
4691 for (i=0; i<argc; i++) {
4692 argv[i] = take_items(argv[i],
len);
4696 int arity = rb_block_arity();
4705 for (j=0; j<argc; j++) {
4706 tmp[j+1] = rb_ary_elt(argv[j], i);
4718 for (j=0; j<argc; j++) {
4732 for (j=0; j<argc; j++) {
4758rb_ary_transpose(
VALUE ary)
4760 long elen = -1, alen, i, j;
4761 VALUE tmp, result = 0;
4765 for (i=0; i<alen; i++) {
4766 tmp = to_ary(rb_ary_elt(ary, i));
4770 for (j=0; j<elen; j++) {
4775 rb_raise(
rb_eIndexError,
"element size differs (%ld should be %ld)",
4778 for (j=0; j<elen; j++) {
4779 rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
4803 rb_ary_modify_check(copy);
4804 orig = to_ary(orig);
4805 if (copy == orig)
return copy;
4810 if (
RARRAY_LEN(orig) <= ary_embed_capa(copy)) {
4817 else if (ARY_EMBED_P(orig)) {
4818 long len = ARY_EMBED_LEN(orig);
4821 FL_UNSET_EMBED(copy);
4822 ARY_SET_PTR(copy,
ptr);
4823 ARY_SET_LEN(copy,
len);
4824 ARY_SET_CAPA(copy,
len);
4833 VALUE shared_root = ary_make_shared(orig);
4834 FL_UNSET_EMBED(copy);
4835 ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
4836 ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
4837 rb_ary_set_shared(copy, shared_root);
4860 rb_ary_modify_check(ary);
4861 if (ARY_SHARED_P(ary)) {
4862 rb_ary_unshare(ary);
4864 ARY_SET_EMBED_LEN(ary, 0);
4867 ARY_SET_LEN(ary, 0);
4868 if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
4869 ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
5060 long beg = 0, end = 0,
len = 0;
5083 if (beg < 0) beg = 0;
5092 if (beg >= ARY_MAX_SIZE ||
len > ARY_MAX_SIZE - beg) {
5093 rb_raise(rb_eArgError,
"argument too big");
5097 if (end >= ARY_CAPA(ary)) {
5098 ary_resize_capa(ary, end);
5101 ARY_SET_LEN(ary, end);
5104 if (UNDEF_P(item)) {
5108 for (i=beg; i<end; i++) {
5116 ary_memfill(ary, beg,
len, item);
5138 long len, xlen, ylen;
5148 ARY_SET_LEN(z,
len);
5177rb_ary_concat_multi(
int argc,
VALUE *argv,
VALUE ary)
5179 rb_ary_modify_check(ary);
5184 else if (argc > 1) {
5187 for (i = 0; i < argc; i++) {
5190 ary_append(ary, args);
5200 return ary_append(x, to_ary(y));
5239 rb_raise(rb_eArgError,
"negative argument");
5242 rb_raise(rb_eArgError,
"argument too big");
5247 ARY_SET_LEN(ary2,
len);
5252 ary_memcpy(ary2, 0, t,
ptr);
5253 while (t <=
len/2) {
5330recursive_equal(
VALUE ary1,
VALUE ary2,
int recur)
5333 const VALUE *p1, *p2;
5335 if (recur)
return Qtrue;
5342 for (i = 0; i < len1; i++) {
5390 if (ary1 == ary2)
return Qtrue;
5391 if (!RB_TYPE_P(ary2,
T_ARRAY)) {
5403recursive_eql(
VALUE ary1,
VALUE ary2,
int recur)
5407 if (recur)
return Qtrue;
5409 if (!
rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
5437 if (ary1 == ary2)
return Qtrue;
5445ary_hash_values(
long len,
const VALUE *elements,
const VALUE ary)
5453 for (i=0; i<
len; i++) {
5454 n = rb_hash(elements[i]);
5466rb_ary_hash_values(
long len,
const VALUE *elements)
5468 return ary_hash_values(
len, elements, 0);
5487rb_ary_hash(
VALUE ary)
5538recursive_cmp(
VALUE ary1,
VALUE ary2,
int recur)
5542 if (recur)
return Qundef;
5547 for (i=0; i<
len; i++) {
5548 VALUE e1 = rb_ary_elt(ary1, i), e2 = rb_ary_elt(ary2, i);
5549 VALUE v = rb_funcallv(e1, id_cmp, 1, &e2);
5603 if (ary1 == ary2)
return INT2FIX(0);
5605 if (!UNDEF_P(v))
return v;
5619 rb_hash_add_new_element(hash, elt, elt);
5625ary_tmp_hash_new(
VALUE ary)
5628 VALUE hash = rb_hash_new_with_size(size);
5630 RBASIC_CLEAR_CLASS(hash);
5635ary_make_hash(
VALUE ary)
5637 VALUE hash = ary_tmp_hash_new(ary);
5638 return ary_add_hash(hash, ary);
5648 rb_hash_add_new_element(hash, k, v);
5654ary_make_hash_by(
VALUE ary)
5656 VALUE hash = ary_tmp_hash_new(ary);
5657 return ary_add_hash_by(hash, ary);
5685 ary2 = to_ary(ary2);
5686 if (
RARRAY_LEN(ary2) == 0) {
return ary_make_shared_copy(ary1); }
5691 VALUE elt = rb_ary_elt(ary1, i);
5692 if (rb_ary_includes_by_eql(ary2, elt))
continue;
5698 hash = ary_make_hash(ary2);
5700 if (rb_hash_stlike_lookup(hash,
RARRAY_AREF(ary1, i), NULL))
continue;
5727rb_ary_difference_multi(
int argc,
VALUE *argv,
VALUE ary)
5732 bool *is_hash =
ALLOCV_N(
bool, t0, argc);
5736 for (i = 0; i < argc; i++) {
5737 argv[i] = to_ary(argv[i]);
5738 is_hash[i] = (length > SMALL_ARRAY_LEN &&
RARRAY_LEN(argv[i]) > SMALL_ARRAY_LEN);
5739 if (is_hash[i]) argv[i] = ary_make_hash(argv[i]);
5744 VALUE elt = rb_ary_elt(ary, i);
5745 for (j = 0; j < argc; j++) {
5747 if (rb_hash_stlike_lookup(argv[j], elt, NULL))
5751 if (rb_ary_includes_by_eql(argv[j], elt))
break;
5790 VALUE hash, ary3, v;
5794 ary2 = to_ary(ary2);
5801 if (!rb_ary_includes_by_eql(ary2, v))
continue;
5802 if (rb_ary_includes_by_eql(ary3, v))
continue;
5808 hash = ary_make_hash(ary2);
5813 if (rb_hash_stlike_delete(hash, &vv, 0)) {
5843rb_ary_intersection_multi(
int argc,
VALUE *argv,
VALUE ary)
5848 for (i = 0; i < argc; i++) {
5849 result = rb_ary_and(result, argv[i]);
5856ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
5858 if (existing)
return ST_STOP;
5859 *key = *value = (
VALUE)arg;
5868 VALUE elt = rb_ary_elt(ary, i);
5869 if (rb_ary_includes_by_eql(ary_union, elt))
continue;
5880 if (!rb_hash_stlike_update(hash, (st_data_t)elt, ary_hash_orset, (st_data_t)elt)) {
5906 ary2 = to_ary(ary2);
5909 rb_ary_union(ary3, ary1);
5910 rb_ary_union(ary3, ary2);
5914 hash = ary_make_hash(ary1);
5915 rb_ary_union_hash(hash, ary2);
5917 return rb_hash_values(hash);
5944rb_ary_union_multi(
int argc,
VALUE *argv,
VALUE ary)
5951 for (i = 0; i < argc; i++) {
5952 argv[i] = to_ary(argv[i]);
5956 if (sum <= SMALL_ARRAY_LEN) {
5959 rb_ary_union(ary_union, ary);
5960 for (i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]);
5965 hash = ary_make_hash(ary);
5966 for (i = 0; i < argc; i++) rb_ary_union_hash(hash, argv[i]);
5968 return rb_hash_values(hash);
5988 VALUE hash, v, result, shorter, longer;
5992 ary2 = to_ary(ary2);
5998 if (rb_ary_includes_by_eql(ary2, v))
return Qtrue;
6010 hash = ary_make_hash(shorter);
6016 if (rb_hash_stlike_lookup(hash, vv, 0)) {
6026ary_max_generic(
VALUE ary,
long i,
VALUE vmax)
6034 if (rb_cmpint(rb_funcallv(vmax, id_cmp, 1, &v), vmax, v) < 0) {
6043ary_max_opt_fixnum(
VALUE ary,
long i,
VALUE vmax)
6050 for (; i < n; ++i) {
6054 if ((
long)vmax < (
long)v) {
6059 return ary_max_generic(ary, i, vmax);
6067ary_max_opt_float(
VALUE ary,
long i,
VALUE vmax)
6074 for (; i < n; ++i) {
6077 if (RB_FLOAT_TYPE_P(v)) {
6078 if (rb_float_cmp(vmax, v) < 0) {
6083 return ary_max_generic(ary, i, vmax);
6091ary_max_opt_string(
VALUE ary,
long i,
VALUE vmax)
6098 for (; i < n; ++i) {
6107 return ary_max_generic(ary, i, vmax);
6169 if (rb_check_arity(argc, 0, 1) && !
NIL_P(num = argv[0]))
6170 return rb_nmin_run(ary, num, 0, 1, 1);
6176 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) > 0) {
6184 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6185 return ary_max_opt_fixnum(ary, 1, result);
6187 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6188 return ary_max_opt_string(ary, 1, result);
6190 else if (RB_FLOAT_TYPE_P(result) && CMP_OPTIMIZABLE(FLOAT)) {
6191 return ary_max_opt_float(ary, 1, result);
6194 return ary_max_generic(ary, 1, result);
6198 if (UNDEF_P(result))
return Qnil;
6203ary_min_generic(
VALUE ary,
long i,
VALUE vmin)
6211 if (rb_cmpint(rb_funcallv(vmin, id_cmp, 1, &v), vmin, v) > 0) {
6220ary_min_opt_fixnum(
VALUE ary,
long i,
VALUE vmin)
6227 for (; i < n; ++i) {
6231 if ((
long)vmin > (
long)a) {
6236 return ary_min_generic(ary, i, vmin);
6244ary_min_opt_float(
VALUE ary,
long i,
VALUE vmin)
6251 for (; i < n; ++i) {
6254 if (RB_FLOAT_TYPE_P(a)) {
6255 if (rb_float_cmp(vmin, a) > 0) {
6260 return ary_min_generic(ary, i, vmin);
6268ary_min_opt_string(
VALUE ary,
long i,
VALUE vmin)
6275 for (; i < n; ++i) {
6284 return ary_min_generic(ary, i, vmin);
6346 if (rb_check_arity(argc, 0, 1) && !
NIL_P(num = argv[0]))
6347 return rb_nmin_run(ary, num, 0, 0, 1);
6353 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) < 0) {
6361 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6362 return ary_min_opt_fixnum(ary, 1, result);
6364 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6365 return ary_min_opt_string(ary, 1, result);
6367 else if (RB_FLOAT_TYPE_P(result) && CMP_OPTIMIZABLE(FLOAT)) {
6368 return ary_min_opt_float(ary, 1, result);
6371 return ary_min_generic(ary, 1, result);
6375 if (UNDEF_P(result))
return Qnil;
6402rb_ary_minmax(
VALUE ary)
6407 return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary));
6411push_value(st_data_t key, st_data_t val, st_data_t ary)
6445rb_ary_uniq_bang(
VALUE ary)
6450 rb_ary_modify_check(ary);
6454 hash = ary_make_hash_by(ary);
6456 hash = ary_make_hash(ary);
6462 rb_ary_modify_check(ary);
6463 ARY_SET_LEN(ary, 0);
6464 if (ARY_SHARED_P(ary)) {
6465 rb_ary_unshare(ary);
6468 ary_resize_capa(ary, hash_size);
6469 rb_hash_foreach(hash, push_value, ary);
6501rb_ary_uniq(
VALUE ary)
6510 hash = ary_make_hash_by(ary);
6511 uniq = rb_hash_values(hash);
6514 hash = ary_make_hash(ary);
6515 uniq = rb_hash_values(hash);
6538rb_ary_compact_bang(
VALUE ary)
6555 ary_resize_smaller(ary, n);
6575rb_ary_compact(
VALUE ary)
6578 rb_ary_compact_bang(ary);
6610rb_ary_count(
int argc,
VALUE *argv,
VALUE ary)
6614 if (rb_check_arity(argc, 0, 1) == 0) {
6626 VALUE obj = argv[0];
6629 rb_warn(
"given block not used");
6640flatten(
VALUE ary,
int level)
6664 ARY_SET_LEN(result, i);
6666 stack = ary_new(0, ARY_DEFAULT_SIZE);
6672 rb_hash_aset(memo, ary,
Qtrue);
6673 rb_hash_aset(memo, tmp,
Qtrue);
6682 if (level >= 0 &&
RARRAY_LEN(stack) / 2 >= level) {
6687 if (
RBASIC(result)->klass) {
6689 rb_hash_clear(memo);
6698 if (rb_hash_aref(memo, tmp) ==
Qtrue) {
6699 rb_hash_clear(memo);
6700 rb_raise(rb_eArgError,
"tried to flatten recursive array");
6702 rb_hash_aset(memo, tmp,
Qtrue);
6714 rb_hash_delete(memo, ary);
6722 rb_hash_clear(memo);
6765rb_ary_flatten_bang(
int argc,
VALUE *argv,
VALUE ary)
6767 int mod = 0, level = -1;
6770 lv = (rb_check_arity(argc, 0, 1) ? argv[0] :
Qnil);
6771 rb_ary_modify_check(ary);
6773 if (level == 0)
return Qnil;
6775 result = flatten(ary, level);
6776 if (result == ary) {
6781 if (mod) ARY_SET_EMBED_LEN(result, 0);
6822rb_ary_flatten(
int argc,
VALUE *argv,
VALUE ary)
6827 if (rb_check_arity(argc, 0, 1) && !
NIL_P(argv[0])) {
6829 if (level == 0)
return ary_make_shared_copy(ary);
6832 result = flatten(ary, level);
6833 if (result == ary) {
6834 result = ary_make_shared_copy(ary);
6840#define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1)
6843rb_ary_shuffle_bang(rb_execution_context_t *ec,
VALUE ary,
VALUE randgen)
6851 long j = RAND_UPTO(i);
6865rb_ary_shuffle(rb_execution_context_t *ec,
VALUE ary,
VALUE randgen)
6868 rb_ary_shuffle_bang(ec, ary, randgen);
6873 .wrap_struct_name =
"ary_sample_memo",
6877 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
6884 long n,
len, i, j, k, idx[10];
6885 long rnds[numberof(idx)];
6886 long memo_threshold;
6895 return rb_ary_elt(ary, i);
6898 if (n < 0) rb_raise(rb_eArgError,
"negative sample number");
6902 if (n <= numberof(idx)) {
6903 for (i = 0; i < n; ++i) {
6904 rnds[i] = RAND_UPTO(
len - i);
6909 if (
len < k && n <= numberof(idx)) {
6910 for (i = 0; i < n; ++i) {
6920 return rb_ary_new_from_args(1,
RARRAY_AREF(ary, i));
6932 if (j >= i) l = i, g = ++j;
6933 if (k >= l && (++k >= g)) ++k;
6942 if (n <= numberof(idx)) {
6943 long sorted[numberof(idx)];
6944 sorted[0] = idx[0] = rnds[0];
6945 for (i=1; i<n; i++) {
6947 for (j = 0; j < i; ++j) {
6948 if (k < sorted[j])
break;
6951 memmove(&sorted[j+1], &sorted[j],
sizeof(sorted[0])*(i-j));
6952 sorted[j] = idx[i] = k;
6956 for (i=0; i<n; i++) {
6961 else if (n <= memo_threshold / 2) {
6964 st_table *memo = st_init_numtable_with_size(n);
6968 for (i=0; i<n; i++) {
6969 long r = RAND_UPTO(
len-i) + i;
6971 if (r > max_idx) max_idx = r;
6974 if (
len <= max_idx) n = 0;
6975 else if (n >
len) n =
len;
6977 for (i=0; i<n; i++) {
6978 long j2 = j = ptr_result[i];
6981 if (st_lookup(memo, (st_data_t)i, &value)) i2 = (long)value;
6982 if (st_lookup(memo, (st_data_t)j, &value)) j2 = (long)value;
6983 st_insert(memo, (st_data_t)j, (st_data_t)i2);
6984 ptr_result[i] = ptr_ary[j2];
6989 st_free_table(memo);
6994 RBASIC_CLEAR_CLASS(result);
6997 for (i=0; i<n; i++) {
6998 j = RAND_UPTO(
len-i) + i;
7000 ptr_result[j] = ptr_result[i];
7004 RBASIC_SET_CLASS_RAW(result,
rb_cArray);
7006 ARY_SET_LEN(result, n);
7012ary_sized_alloc(rb_execution_context_t *ec,
VALUE self)
7018ary_sample0(rb_execution_context_t *ec,
VALUE ary)
7034 if (mul <= 0)
return INT2FIX(0);
7036 return rb_fix_mul_fix(rb_ary_length(self), n);
7073rb_ary_cycle(
int argc,
VALUE *argv,
VALUE ary)
7077 rb_check_arity(argc, 0, 1);
7080 if (argc == 0 ||
NIL_P(argv[0])) {
7085 if (n <= 0)
return Qnil;
7088 while (
RARRAY_LEN(ary) > 0 && (n < 0 || 0 < n--)) {
7102yield_indexed_values(
const VALUE values,
const long r,
const long *
const p)
7107 for (i = 0; i < r; i++) ARY_SET(result, i,
RARRAY_AREF(values, p[i]));
7108 ARY_SET_LEN(result, r);
7110 return !
RBASIC(values)->klass;
7126permute0(
const long n,
const long r,
long *
const p,
char *
const used,
const VALUE values)
7128 long i = 0, index = 0;
7131 const char *
const unused = memchr(&used[i], 0, n-i);
7146 for (i = 0; i < n; ++i) {
7147 if (used[i])
continue;
7149 if (!yield_indexed_values(values, r, p)) {
7165descending_factorial(
long from,
long how_many)
7170 while (--how_many > 0) {
7172 cnt = rb_int_mul(cnt,
LONG2FIX(v));
7182binomial_coefficient(
long comb,
long size)
7186 if (comb > size-comb) {
7192 else if (comb == 0) {
7196 for (i = 1; i < comb; ++i) {
7197 r = rb_int_mul(r,
LONG2FIX(size - i));
7198 r = rb_int_idiv(r,
LONG2FIX(i + 1));
7209 return descending_factorial(n, k);
7255rb_ary_permutation(
int argc,
VALUE *argv,
VALUE ary)
7260 if (rb_check_arity(argc, 0, 1) && !
NIL_P(argv[0])) {
7269 if (r < 0 || n < r) {
7282 long *p =
ALLOCV_N(
long, t0, r+roomof(n,
sizeof(
long)));
7283 char *used = (
char*)(p + r);
7284 VALUE ary0 = ary_make_shared_copy(ary);
7285 RBASIC_CLEAR_CLASS(ary0);
7289 permute0(n, r, p, used, ary0);
7297combinate0(
const long len,
const long n,
long *
const stack,
const VALUE values)
7304 for (lev++; lev < n; lev++) {
7305 stack[lev+1] = stack[lev]+1;
7307 if (!yield_indexed_values(values, n, stack+1)) {
7311 if (lev == 0)
return;
7313 }
while (stack[lev+1]+n ==
len+lev+1);
7323 return binomial_coefficient(k, n);
7378 if (n < 0 ||
len < n) {
7390 VALUE ary0 = ary_make_shared_copy(ary);
7392 long *stack =
ALLOCV_N(
long, t0, n+1);
7394 RBASIC_CLEAR_CLASS(ary0);
7395 combinate0(
len, n, stack, ary0);
7415rpermute0(
const long n,
const long r,
long *
const p,
const VALUE values)
7417 long i = 0, index = 0;
7421 if (++index < r-1) {
7425 for (i = 0; i < n; ++i) {
7427 if (!yield_indexed_values(values, r, p)) {
7432 if (index <= 0)
return;
7433 }
while ((i = ++p[--index]) >= n);
7491rb_ary_repeated_permutation(
VALUE ary,
VALUE num)
7511 VALUE ary0 = ary_make_shared_copy(ary);
7512 RBASIC_CLEAR_CLASS(ary0);
7514 rpermute0(n, r, p, ary0);
7522rcombinate0(
const long n,
const long r,
long *
const p,
const long rest,
const VALUE values)
7524 long i = 0, index = 0;
7528 if (++index < r-1) {
7532 for (; i < n; ++i) {
7534 if (!yield_indexed_values(values, r, p)) {
7539 if (index <= 0)
return;
7540 }
while ((i = ++p[--index]) >= n);
7552 return binomial_coefficient(k, n + k - 1);
7595rb_ary_repeated_combination(
VALUE ary,
VALUE num)
7613 else if (
len == 0) {
7619 VALUE ary0 = ary_make_shared_copy(ary);
7620 RBASIC_CLEAR_CLASS(ary0);
7622 rcombinate0(
len, n, p, n, ary0);
7683rb_ary_product(
int argc,
VALUE *argv,
VALUE ary)
7688 VALUE *arrays = RARRAY_PTR(t0);
7689 int *counters =
ALLOCV_N(
int, t1, n);
7694 RBASIC_CLEAR_CLASS(t0);
7699 for (i = 1; i < n; i++) arrays[i] =
Qnil;
7700 for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
7703 for (i = 0; i < n; i++) counters[i] = 0;
7708 for (i = 0; i < n; i++) {
7710 arrays[i] = ary_make_shared_copy(arrays[i]);
7715 for (i = 0; i < n; i++) {
7721 if (MUL_OVERFLOW_LONG_P(resultlen, k))
7731 for (j = 0; j < n; j++) {
7736 if (
NIL_P(result)) {
7737 FL_SET(t0, RARRAY_SHARED_ROOT_FLAG);
7739 if (!
FL_TEST(t0, RARRAY_SHARED_ROOT_FLAG)) {
7743 FL_UNSET(t0, RARRAY_SHARED_ROOT_FLAG);
7756 while (counters[m] ==
RARRAY_LEN(arrays[m])) {
7759 if (--m < 0)
goto done;
7767 return NIL_P(result) ? ary : result;
7793 rb_raise(rb_eArgError,
"attempt to take negative size");
7820rb_ary_take_while(
VALUE ary)
7828 return rb_ary_take(ary,
LONG2FIX(i));
7856 rb_raise(rb_eArgError,
"attempt to drop negative size");
7883rb_ary_drop_while(
VALUE ary)
7891 return rb_ary_drop(ary,
LONG2FIX(i));
7934rb_ary_any_p(
int argc,
VALUE *argv,
VALUE ary)
7938 rb_check_arity(argc, 0, 1);
7942 rb_warn(
"given block not used");
7949 for (i = 0; i <
len; ++i) {
8001rb_ary_all_p(
int argc,
VALUE *argv,
VALUE ary)
8005 rb_check_arity(argc, 0, 1);
8009 rb_warn(
"given block not used");
8016 for (i = 0; i <
len; ++i) {
8062rb_ary_none_p(
int argc,
VALUE *argv,
VALUE ary)
8066 rb_check_arity(argc, 0, 1);
8070 rb_warn(
"given block not used");
8077 for (i = 0; i <
len; ++i) {
8126rb_ary_one_p(
int argc,
VALUE *argv,
VALUE ary)
8131 rb_check_arity(argc, 0, 1);
8135 rb_warn(
"given block not used");
8139 if (result)
return Qfalse;
8145 for (i = 0; i <
len; ++i) {
8147 if (result)
return Qfalse;
8155 if (result)
return Qfalse;
8187 self = rb_ary_at(self, *argv);
8188 if (!--argc)
return self;
8190 return rb_obj_dig(argc, argv, self,
Qnil);
8194finish_exact_sum(
long n,
VALUE r,
VALUE v,
int z)
8199 v = rb_rational_plus(r, v);
8255 v = (rb_check_arity(argc, 0, 1) ? argv[0] :
LONG2FIX(0));
8265 bool init_is_float = RB_FLOAT_TYPE_P(v);
8266 if (init_is_float) {
8271 goto init_is_a_value;
8285 else if (RB_BIGNUM_TYPE_P(e))
8286 v = rb_big_plus(e, v);
8291 r = rb_rational_plus(r, e);
8296 v = finish_exact_sum(n, r, v, argc!=0);
8297 if (init_is_float) v = rb_float_plus(argv[0], v);
8301 v = finish_exact_sum(n, r, v, i!=0);
8303 if (init_is_float ? (--i, e = argv[0],
true) : RB_FLOAT_TYPE_P(e)) {
8313 goto has_float_value;
8318 if (RB_FLOAT_TYPE_P(e))
8323 else if (RB_BIGNUM_TYPE_P(e))
8330 if (isnan(f))
continue;
8336 if (isinf(f) && signbit(x) != signbit(f))
8342 if (isinf(f))
continue;
8345 if (fabs(f) >= fabs(x))
8358 goto has_some_value;
8372rb_ary_deconstruct(
VALUE ary)
8883 fake_ary_flags = init_fake_ary_flags();
9013 rb_vm_register_global_object(rb_cArray_empty_frozen);
9016#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.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define rb_ary_new4
Old name of rb_ary_new_from_values.
#define FIXABLE
Old name of RB_FIXABLE.
#define LONG2FIX
Old name of RB_INT2FIX.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define NUM2DBL
Old name of rb_num2dbl.
#define FL_SET
Old name of RB_FL_SET.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define DBL2NUM
Old name of rb_float_new.
#define FL_TEST
Old name of RB_FL_TEST.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
void rb_iter_break(void)
Breaks from a block.
VALUE rb_eFrozenError
FrozenError exception.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
VALUE rb_cArray
Array class.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Identical to rb_class_new_instance(), except it passes the passed keywords if any to the #initialize ...
VALUE rb_obj_frozen_p(VALUE obj)
Just calls RB_OBJ_FROZEN() inside.
int rb_eql(VALUE lhs, VALUE rhs)
Checks for equality of the passed objects, in terms of Object#eql?.
VALUE rb_cNumeric
Numeric class.
VALUE rb_cRandom
Random class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
double rb_num2dbl(VALUE num)
Converts an instance of rb_cNumeric into C's double.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
#define RGENGC_WB_PROTECTED_ARRAY
This is a compile-time flag to enable/disable write barrier for struct RArray.
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().
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.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
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.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY(obj)
Convenient casting macro.
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
@ 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.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
#define RTEST
This is an old name of RB_TEST.
struct RBasic basic
Basic part, including flags and class.
VALUE flags
Per-object flags.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t VALUE
Type that represents a Ruby object.