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); \
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)
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)
219 return !(ARY_SHARED_ROOT_P(ary) ||
OBJ_FROZEN(ary) || ARY_SHARED_P(ary));
223rb_ary_size_as_embedded(
VALUE ary)
227 if (ARY_EMBED_P(ary)) {
228 real_size = ary_embed_size(ARY_EMBED_LEN(ary));
230 else if (rb_ary_embeddable_p(ary)) {
231 real_size = ary_embed_size(ARY_HEAP_CAPA(ary));
234 real_size =
sizeof(
struct RArray);
241#define ary_verify(ary) ary_verify_(ary, __FILE__, __LINE__)
244ary_verify_(
VALUE ary,
const char *file,
int line)
248 if (ARY_SHARED_P(
ary)) {
257 else if (ARY_EMBED_P(
ary)) {
266 for (i=0; i<
len; i++) {
275#define ary_verify(ary) ((void)0)
304ary_mem_clear(
VALUE ary,
long beg,
long size)
312memfill(
register VALUE *mem,
register long size,
register VALUE val)
323 memfill(
ptr + beg, size, val);
333 if (argc > (
int)(128/
sizeof(
VALUE)) ) {
334 rb_gc_writebarrier_remember(buff_owner_ary);
342 for (i=0; i<argc; i++) {
352 ary_memcpy0(
ary, beg, argc, argv,
ary);
356ary_heap_alloc_buffer(
size_t capa)
364 ruby_sized_xfree((
void *)
ptr, size);
370 ary_heap_free_ptr(
ary, ARY_HEAP_PTR(
ary), ARY_HEAP_SIZE(
ary));
374ary_heap_realloc(
VALUE ary,
size_t new_capa)
387 if (!ARY_EMBED_P(
ary)) {
388 const VALUE *buf = ARY_HEAP_PTR(
ary);
389 long len = ARY_HEAP_LEN(
ary);
390 long capa = ARY_HEAP_CAPA(
ary);
393 ARY_SET_EMBED_LEN(
ary,
len);
402ary_resize_capa(
VALUE ary,
long capacity)
408 if (capacity > ary_embed_capa(
ary)) {
409 size_t new_capa = capacity;
410 if (ARY_EMBED_P(
ary)) {
411 long len = ARY_EMBED_LEN(
ary);
412 VALUE *
ptr = ary_heap_alloc_buffer(capacity);
417 ARY_SET_HEAP_LEN(
ary,
len);
420 new_capa = ary_heap_realloc(
ary, capacity);
422 ARY_SET_CAPA(
ary, new_capa);
425 if (!ARY_EMBED_P(
ary)) {
426 long len = ARY_HEAP_LEN(
ary);
427 long old_capa = ARY_HEAP_CAPA(
ary);
430 if (
len > capacity)
len = capacity;
432 ary_heap_free_ptr(
ary,
ptr, old_capa *
sizeof(
VALUE));
445 long capacity = ARY_HEAP_LEN(
ary);
446 long old_capa = ARY_HEAP_CAPA(
ary);
449 if (old_capa > capacity) {
450 size_t new_capa = ary_heap_realloc(
ary, capacity);
451 ARY_SET_CAPA(
ary, new_capa);
460 long new_capa = ARY_CAPA(
ary) / 2;
462 if (new_capa < ARY_DEFAULT_SIZE) {
463 new_capa = ARY_DEFAULT_SIZE;
465 if (new_capa >= ARY_MAX_SIZE - min) {
466 new_capa = (ARY_MAX_SIZE - min) / 2;
469 ary_resize_capa(
ary, new_capa);
488 FL_UNSET_SHARED(
ary);
494 if (ARY_OWNS_HEAP_P(
ary)) {
497 else if (ARY_SHARED_P(
ary)) {
502 ARY_SET_EMBED_LEN(
ary, 0);
527 RB_DEBUG_COUNTER_INC(obj_ary_shared_create);
535 rb_check_frozen(
ary);
542 if (ARY_SHARED_P(
ary)) {
548 if (
len <= ary_embed_capa(
ary)) {
550 FL_UNSET_SHARED(
ary);
554 ARY_SET_EMBED_LEN(
ary,
len);
558 FL_UNSET_SHARED(
ary);
560 ARY_SET_CAPA(
ary, shared_len);
571 ARY_SET_CAPA_FORCE(
ary,
len);
572 ARY_SET_PTR_FORCE(
ary,
ptr);
575 rb_gc_writebarrier_remember(
ary);
583 rb_ary_modify_check(
ary);
584 rb_ary_cancel_sharing(
ary);
588ary_ensure_room_for_push(
VALUE ary,
long add_len)
591 long new_len = old_len + add_len;
594 if (old_len > ARY_MAX_SIZE - add_len) {
597 if (ARY_SHARED_P(
ary)) {
598 if (new_len > ary_embed_capa(
ary)) {
602 rb_ary_modify_check(
ary);
613 ary_double_capa(
ary, new_len);
624 rb_ary_modify_check(
ary);
627 if (new_len >
capa) {
628 ary_double_capa(
ary, new_len);
659 if (!ARY_EMBED_P(
ary) && !ARY_SHARED_P(
ary) && !ARY_SHARED_ROOT_P(
ary)) {
660 ary_shrink_capa(
ary);
676 if (!ARY_EMBED_P(ary1) && ARY_SHARED_P(ary1) &&
677 !ARY_EMBED_P(ary2) && ARY_SHARED_P(ary2) &&
678 ARY_SHARED_ROOT(ary1) == ARY_SHARED_ROOT(ary2) &&
679 ARY_HEAP_LEN(ary1) == ARY_HEAP_LEN(ary2)) {
688 size_t size = ary_embed_size(
capa);
701ary_alloc_heap(
VALUE klass)
705 sizeof(struct
RArray), 0);
707 ary->as.heap.len = 0;
708 ary->as.heap.aux.capa = 0;
709 ary->as.heap.ptr = NULL;
715empty_ary_alloc(
VALUE klass)
717 RUBY_DTRACE_CREATE_HOOK(ARRAY, 0);
718 return ary_alloc_embed(klass, 0);
729 rb_raise(rb_eArgError,
"negative array size (or size too big)");
731 if (
capa > ARY_MAX_SIZE) {
732 rb_raise(rb_eArgError,
"array size too big");
735 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
737 if (ary_embeddable_p(
capa)) {
738 ary = ary_alloc_embed(klass,
capa);
741 ary = ary_alloc_heap(klass);
745 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
746 ARY_SET_HEAP_LEN(
ary, 0);
765(rb_ary_new_from_args)(
long n, ...)
774 for (i=0; i<n; i++) {
784rb_ary_tmp_new_from_values(
VALUE klass,
long n,
const VALUE *elts)
788 ary = ary_new(klass, n);
790 ary_memcpy(
ary, 0, n, elts);
800 return rb_ary_tmp_new_from_values(
rb_cArray, n, elts);
806 size_t size = ary_embed_size(
capa);
823 sizeof(struct
RArray), ec);
825 ary->as.heap.len = 0;
826 ary->as.heap.aux.capa = 0;
827 ary->as.heap.ptr = NULL;
838 rb_raise(rb_eArgError,
"negative array size (or size too big)");
840 if (
capa > ARY_MAX_SIZE) {
841 rb_raise(rb_eArgError,
"array size too big");
844 RUBY_DTRACE_CREATE_HOOK(ARRAY,
capa);
846 if (ary_embeddable_p(
capa)) {
847 ary = ec_ary_alloc_embed(ec, klass,
capa);
850 ary = ec_ary_alloc_heap(ec, klass);
854 ARY_SET_PTR(
ary, ary_heap_alloc_buffer(
capa));
855 ARY_SET_HEAP_LEN(
ary, 0);
868 ary_memcpy(
ary, 0, n, elts);
883rb_ary_hidden_new_fill(
long capa)
894 if (ARY_OWNS_HEAP_P(
ary)) {
895 if (USE_DEBUG_COUNTER &&
896 !ARY_SHARED_ROOT_P(
ary) &&
898 RB_DEBUG_COUNTER_INC(obj_ary_extracapa);
901 RB_DEBUG_COUNTER_INC(obj_ary_ptr);
905 RB_DEBUG_COUNTER_INC(obj_ary_embed);
908 if (ARY_SHARED_P(
ary)) {
909 RB_DEBUG_COUNTER_INC(obj_ary_shared);
911 if (ARY_SHARED_ROOT_P(
ary) && ARY_SHARED_ROOT_OCCUPIED(
ary)) {
912 RB_DEBUG_COUNTER_INC(obj_ary_shared_root_occupied);
916static VALUE fake_ary_flags;
919init_fake_ary_flags(
void)
921 struct RArray fake_ary = {0};
929rb_setup_fake_ary(
struct RArray *fake_ary,
const VALUE *list,
long len)
932 RBASIC_CLEAR_CLASS((
VALUE)fake_ary);
938 return (
VALUE)fake_ary;
944 if (ARY_OWNS_HEAP_P(
ary)) {
945 return ARY_CAPA(
ary) *
sizeof(
VALUE);
957 if (ARY_SHARED_P(
ary)) {
958 return ARY_SHARED_ROOT(
ary);
960 else if (ARY_SHARED_ROOT_P(
ary)) {
972 VALUE shared = ary_alloc_heap(0);
973 FL_SET_SHARED_ROOT(shared);
975 if (ARY_EMBED_P(
ary)) {
977 ARY_SET_PTR(shared,
ptr);
981 ARY_SET_HEAP_LEN(
ary,
len);
988 ARY_SET_LEN(shared,
capa);
990 rb_ary_set_shared(
ary, shared);
1004 if (ary_embeddable_p(
len)) {
1009 ARY_SET_EMBED_LEN(subst,
len);
1013 return rb_ary_increment_share(ary_make_shared(
ary));
1026 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1028#define to_ary rb_to_array_type
1033 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_ary);
1039 return rb_check_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1045 return rb_convert_type_with_id(
ary,
T_ARRAY,
"Array", idTo_a);
1074rb_ary_s_new(
int argc,
VALUE *argv,
VALUE klass)
1080 if (argc > 0 &&
FIXNUM_P(argv[0])) {
1082 if (size < 0) size = 0;
1085 ary = ary_new(klass, size);
1164 if (argc == 1 && !
FIXNUM_P(size)) {
1175 rb_raise(rb_eArgError,
"negative array size");
1177 if (
len > ARY_MAX_SIZE) {
1178 rb_raise(rb_eArgError,
"array size too big");
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);
1762rb_ary_subseq_step(
VALUE ary,
long beg,
long len,
long step)
1767 if (beg > alen)
return Qnil;
1768 if (beg < 0 ||
len < 0)
return Qnil;
1770 if (alen <
len || alen < beg +
len) {
1774 if (
len == 0)
return ary_new(klass, 0);
1776 rb_raise(rb_eArgError,
"slice step cannot be zero");
1778 return ary_make_partial(
ary, klass, beg,
len);
1780 return ary_make_partial_step(
ary, klass, beg,
len, step);
1786 return rb_ary_subseq_step(
ary, beg,
len, 1);
1914 return rb_ary_aref2(
ary, argv[0], argv[1]);
1916 return rb_ary_aref1(
ary, argv[0]);
1933 long beg,
len, step;
1940 switch (rb_arithmetic_sequence_beg_len_step(arg, &beg, &
len, &step,
RARRAY_LEN(
ary), 0)) {
1946 return rb_ary_subseq_step(
ary, beg,
len, step);
1991 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_FIRST);
1997ary_first(
VALUE self)
2013 return ary_last(
ary);
2016 return ary_take_first_or_last(argc, argv,
ary, ARY_TAKE_LAST);
2069 if (block_given && argc == 2) {
2070 rb_warn(
"block supersedes default value argument");
2078 if (block_given)
return rb_yield(pos);
2080 rb_raise(
rb_eIndexError,
"index %ld outside of array bounds: %ld...%ld",
2124 if (!
NIL_P(if_none)) {
2125 return rb_funcallv(if_none, idCall, 0, 0);
2168 idx = (idx >=
len) ?
len : idx;
2171 if (!
NIL_P(if_none)) {
2172 return rb_funcallv(if_none, idCall, 0, 0);
2229 rb_warn(
"given block not used");
2287 rb_warn(
"given block not used");
2305 if (!
NIL_P(tmp))
return tmp;
2320 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2324 if (olen <
len || olen < beg +
len) {
2330 rofs = (rptr >= optr && rptr < optr + olen) ? rptr - optr : -1;
2335 if (beg > ARY_MAX_SIZE - rlen) {
2338 target_ary = ary_ensure_room_for_push(
ary, rlen-
len);
2340 ary_mem_clear(
ary, olen, beg - olen);
2343 ary_memcpy0(
ary, beg, rlen, rptr, target_ary);
2350 if (olen -
len > ARY_MAX_SIZE - rlen) {
2354 alen = olen + rlen -
len;
2355 if (alen >= ARY_CAPA(
ary)) {
2356 ary_double_capa(
ary, alen);
2363 ARY_SET_LEN(
ary, alen);
2367 rb_gc_writebarrier_remember(
ary);
2389 rb_ary_modify_check(
ary);
2390 if (ARY_SHARED_P(
ary)) {
2394 rb_bug(
"probable buffer overflow: %ld for %ld",
len,
capa);
2406 if (
len == olen)
return ary;
2407 if (
len > ARY_MAX_SIZE) {
2411 if (
len > ARY_CAPA(
ary)) {
2412 ary_double_capa(
ary,
len);
2414 ary_mem_clear(
ary, olen,
len - olen);
2417 else if (ARY_EMBED_P(
ary)) {
2418 ARY_SET_EMBED_LEN(
ary,
len);
2420 else if (
len <= ary_embed_capa(
ary)) {
2422 long ptr_capa = ARY_HEAP_SIZE(
ary);
2423 bool is_malloc_ptr = !ARY_SHARED_P(
ary);
2428 ARY_SET_EMBED_LEN(
ary,
len);
2430 if (is_malloc_ptr) ruby_sized_xfree((
void *)
ptr, ptr_capa);
2433 if (olen >
len + ARY_DEFAULT_SIZE) {
2434 size_t new_capa = ary_heap_realloc(
ary,
len);
2435 ARY_SET_CAPA(
ary, new_capa);
2437 ARY_SET_HEAP_LEN(
ary,
len);
2606 long offset, beg,
len;
2609 rb_ary_modify_check(
ary);
2613 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[2]);
2617 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2621 return ary_aset_by_rb_ary_splice(
ary, beg,
len, argv[1]);
2625 return ary_aset_by_rb_ary_store(
ary, offset, argv[1]);
2670 rb_ary_modify_check(
ary);
2672 if (argc == 1)
return ary;
2679 rb_raise(
rb_eIndexError,
"index %ld too small for array; minimum: %ld",
2684 rb_ary_splice(
ary, pos, 0, argv + 1, argc - 1);
2694 return rb_ary_length(
ary);
2901 ARY_SET_LEN(dup,
len);
2919recursive_join(
VALUE obj,
VALUE argp,
int recur)
2924 VALUE result = arg[2];
2925 int *first = (
int *)arg[3];
2928 rb_raise(rb_eArgError,
"recursive array join");
2931 ary_join_1(obj,
ary, sep, 0, result, first);
2943 for (i=0; i<max; i++) {
2946 if (i > 0 && !
NIL_P(sep))
2954ary_join_1_str(
VALUE dst,
VALUE src,
int *first)
2958 rb_enc_copy(dst, src);
2967 rb_raise(rb_eArgError,
"recursive array join");
2976 args[3] = (
VALUE)first;
2987 if (i > 0 && !
NIL_P(sep))
2992 ary_join_1_str(result, val, first);
2995 ary_join_1_ary(val,
ary, sep, result, val, first);
2998 ary_join_1_str(result, tmp, first);
3001 ary_join_1_ary(val,
ary, sep, result, tmp, first);
3013 VALUE val, tmp, result;
3022 for (i=0; i < len_memo; i++) {
3026 if (
NIL_P(tmp) || tmp != val) {
3031 rb_enc_associate(result, rb_usascii_encoding());
3032 i = ary_join_0(
ary, sep, i, result);
3034 ary_join_1(
ary,
ary, sep, i, result, &first);
3037 len += RSTRING_LEN(tmp);
3041 len += RSTRING_LEN(val);
3108 else rb_enc_copy(str, s);
3139 return rb_ary_inspect(
ary);
3203 const VALUE e = rb_ary_elt(
ary, i);
3204 const VALUE elt = block_given ? rb_yield_force_blockarg(e) : e;
3206 if (
NIL_P(key_value_pair)) {
3207 rb_raise(
rb_eTypeError,
"wrong element type %"PRIsVALUE
" at %ld (expected array)",
3211 rb_raise(rb_eArgError,
"wrong array length at %ld (expected 2, was %ld)",
3252 ary_reverse(p1, p2);
3298 do *p2-- = *p1++;
while (--
len > 0);
3305rotate_count(
long cnt,
long len)
3307 return (cnt < 0) ? (
len - (~cnt %
len) - 1) : (cnt %
len);
3318 else if (cnt ==
len - 1) {
3326 if (--cnt > 0) ary_reverse(
ptr,
ptr + cnt);
3338 if (
len > 1 && (cnt = rotate_count(cnt,
len)) > 0) {
3430 cnt = rotate_count(cnt,
len);
3433 ary_memcpy(rotated, 0,
len,
ptr + cnt);
3434 ary_memcpy(rotated,
len, cnt,
ptr);
3440struct ary_sort_data {
3446sort_reentered(
VALUE ary)
3448 if (
RBASIC(ary)->klass) {
3455sort_returned(
struct ary_sort_data *data)
3460 sort_reentered(data->ary);
3464sort_1(
const void *ap,
const void *bp,
void *dummy)
3466 struct ary_sort_data *data = dummy;
3467 VALUE retval = sort_reentered(data->ary);
3475 n = rb_cmpint(retval, a, b);
3476 sort_returned(data);
3481sort_2(
const void *ap,
const void *bp,
void *dummy)
3483 struct ary_sort_data *data = dummy;
3484 VALUE retval = sort_reentered(data->ary);
3489 if ((
long)a > (long)b)
return 1;
3490 if ((
long)a < (long)b)
return -1;
3493 if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(STRING)) {
3497 return rb_float_cmp(a, b);
3500 retval = rb_funcallv(a, id_cmp, 1, &b);
3501 n = rb_cmpint(retval, a, b);
3502 sort_returned(data);
3523 VALUE tmp = ary_make_substitution(ary);
3524 struct ary_sort_data data;
3526 RBASIC_CLEAR_CLASS(tmp);
3528 data.receiver = ary;
3534 if (ARY_EMBED_P(tmp)) {
3535 if (ARY_SHARED_P(ary)) {
3536 rb_ary_unshare(ary);
3539 if (ARY_EMBED_LEN(tmp) > ARY_CAPA(ary)) {
3540 ary_resize_capa(ary, ARY_EMBED_LEN(tmp));
3542 ary_memcpy(ary, 0, ARY_EMBED_LEN(tmp), ARY_EMBED_PTR(tmp));
3543 ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
3546 if (!ARY_EMBED_P(ary) && ARY_HEAP_PTR(ary) == ARY_HEAP_PTR(tmp)) {
3547 FL_UNSET_SHARED(ary);
3552 if (ARY_EMBED_P(ary)) {
3553 FL_UNSET_EMBED(ary);
3555 else if (ARY_SHARED_P(ary)) {
3557 rb_ary_unshare(ary);
3562 ARY_SET_PTR(ary, ARY_HEAP_PTR(tmp));
3563 ARY_SET_HEAP_LEN(ary,
len);
3564 ARY_SET_CAPA(ary, ARY_HEAP_LEN(tmp));
3568 ARY_SET_EMBED_LEN(tmp, 0);
3636rb_ary_bsearch(
VALUE ary)
3638 VALUE index_result = rb_ary_bsearch_index(ary);
3643 return index_result;
3660rb_ary_bsearch_index(
VALUE ary)
3663 int smaller = 0, satisfied = 0;
3667 while (low < high) {
3668 mid = low + ((high - low) / 2);
3675 else if (v ==
Qtrue) {
3679 else if (!
RTEST(v)) {
3684 switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
3686 case 1: smaller = 0;
break;
3687 case -1: smaller = 1;
3692 " (must be numeric, true, false or nil)",
3702 if (!satisfied)
return Qnil;
3736rb_ary_sort_by_bang(
VALUE ary)
3743 sorted =
rb_block_call(ary, rb_intern(
"sort_by"), 0, 0, sort_by_i, 0);
3771rb_ary_collect(
VALUE ary)
3806rb_ary_collect_bang(
VALUE ary)
3822 long beg,
len, i, j;
3824 for (i=0; i<argc; i++) {
3831 long end = olen < beg+
len ? olen : beg+
len;
3832 for (j = beg; j < end; j++) {
3855 const long end = beg +
len;
3979rb_ary_values_at(
int argc,
VALUE *argv,
VALUE ary)
3983 for (i = 0; i < argc; ++i) {
3984 append_values_at_single(result, ary, olen, argv[i]);
4012rb_ary_select(
VALUE ary)
4027struct select_bang_arg {
4033select_bang_i(
VALUE a)
4035 volatile struct select_bang_arg *arg = (
void *)a;
4036 VALUE ary = arg->ary;
4039 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4047 return (i1 == i2) ?
Qnil : ary;
4051select_bang_ensure(
VALUE a)
4053 volatile struct select_bang_arg *arg = (
void *)a;
4054 VALUE ary = arg->ary;
4056 long i1 = arg->len[0], i2 = arg->len[1];
4058 if (i2 <
len && i2 < i1) {
4067 ARY_SET_LEN(ary, i2 + tail);
4095rb_ary_select_bang(
VALUE ary)
4097 struct select_bang_arg args;
4103 args.len[0] = args.len[1] = 0;
4124rb_ary_keep_if(
VALUE ary)
4127 rb_ary_select_bang(ary);
4132ary_resize_smaller(
VALUE ary,
long len)
4136 ARY_SET_LEN(ary,
len);
4137 if (
len * 2 < ARY_CAPA(ary) &&
4138 ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
4139 ary_resize_capa(ary,
len * 2);
4187 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4206 ary_resize_smaller(ary, i2);
4217 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); i1++) {
4232 ary_resize_smaller(ary, i2);
4244 if (pos < 0)
return Qnil;
4252 ARY_INCREASE_LEN(ary, -1);
4292ary_slice_bang_by_rb_ary_splice(
VALUE ary,
long pos,
long len)
4299 else if (pos < -orig_len) {
4305 else if (orig_len < pos) {
4308 if (orig_len < pos +
len) {
4309 len = orig_len - pos;
4316 rb_ary_splice(ary, pos,
len, 0, 0);
4414rb_ary_slice_bang(
int argc,
VALUE *argv,
VALUE ary)
4419 rb_ary_modify_check(ary);
4426 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4433 return ary_slice_bang_by_rb_ary_splice(ary, pos,
len);
4462reject_bang_i(
VALUE a)
4464 volatile struct select_bang_arg *arg = (
void *)a;
4465 VALUE ary = arg->ary;
4468 for (i1 = i2 = 0; i1 <
RARRAY_LEN(ary); arg->len[0] = ++i1) {
4476 return (i1 == i2) ?
Qnil : ary;
4480ary_reject_bang(
VALUE ary)
4482 struct select_bang_arg args;
4483 rb_ary_modify_check(ary);
4485 args.len[0] = args.len[1] = 0;
4510rb_ary_reject_bang(
VALUE ary)
4514 return ary_reject_bang(ary);
4535rb_ary_reject(
VALUE ary)
4541 ary_reject(ary, rejected_ary);
4542 return rejected_ary;
4563rb_ary_delete_if(
VALUE ary)
4567 ary_reject_bang(ary);
4582take_items(
VALUE obj,
long n)
4587 if (n == 0)
return result;
4590 args[0] = result; args[1] = (
VALUE)n;
4591 if (UNDEF_P(rb_check_block_call(obj, idEach, 0, 0, take_i, (
VALUE)args)))
4592 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
4698 for (i=0; i<argc; i++) {
4699 argv[i] = take_items(argv[i],
len);
4703 int arity = rb_block_arity();
4712 for (j=0; j<argc; j++) {
4713 tmp[j+1] = rb_ary_elt(argv[j], i);
4725 for (j=0; j<argc; j++) {
4735 for (i=0; i<
len; i++) {
4739 for (j=0; j<argc; j++) {
4765rb_ary_transpose(
VALUE ary)
4767 long elen = -1, alen, i, j;
4768 VALUE tmp, result = 0;
4772 for (i=0; i<alen; i++) {
4773 tmp = to_ary(rb_ary_elt(ary, i));
4777 for (j=0; j<elen; j++) {
4782 rb_raise(
rb_eIndexError,
"element size differs (%ld should be %ld)",
4785 for (j=0; j<elen; j++) {
4786 rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
4810 rb_ary_modify_check(copy);
4811 orig = to_ary(orig);
4812 if (copy == orig)
return copy;
4817 if (
RARRAY_LEN(orig) <= ary_embed_capa(copy)) {
4824 else if (ARY_EMBED_P(orig)) {
4825 long len = ARY_EMBED_LEN(orig);
4826 VALUE *ptr = ary_heap_alloc_buffer(
len);
4828 FL_UNSET_EMBED(copy);
4829 ARY_SET_PTR(copy, ptr);
4830 ARY_SET_LEN(copy,
len);
4831 ARY_SET_CAPA(copy,
len);
4840 VALUE shared_root = ary_make_shared(orig);
4841 FL_UNSET_EMBED(copy);
4842 ARY_SET_PTR(copy, ARY_HEAP_PTR(orig));
4843 ARY_SET_LEN(copy, ARY_HEAP_LEN(orig));
4844 rb_ary_set_shared(copy, shared_root);
4867 rb_ary_modify_check(ary);
4868 if (ARY_SHARED_P(ary)) {
4869 rb_ary_unshare(ary);
4871 ARY_SET_EMBED_LEN(ary, 0);
4874 ARY_SET_LEN(ary, 0);
4875 if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
4876 ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
5067 long beg = 0, end = 0,
len = 0;
5090 if (beg < 0) beg = 0;
5099 if (beg >= ARY_MAX_SIZE ||
len > ARY_MAX_SIZE - beg) {
5100 rb_raise(rb_eArgError,
"argument too big");
5104 if (end >= ARY_CAPA(ary)) {
5105 ary_resize_capa(ary, end);
5108 ARY_SET_LEN(ary, end);
5111 if (UNDEF_P(item)) {
5115 for (i=beg; i<end; i++) {
5122 ary_memfill(ary, beg,
len, item);
5144 long len, xlen, ylen;
5154 ARY_SET_LEN(z,
len);
5183rb_ary_concat_multi(
int argc,
VALUE *argv,
VALUE ary)
5185 rb_ary_modify_check(ary);
5190 else if (argc > 1) {
5193 for (i = 0; i < argc; i++) {
5196 ary_append(ary, args);
5206 return ary_append(x, to_ary(y));
5245 rb_raise(rb_eArgError,
"negative argument");
5248 rb_raise(rb_eArgError,
"argument too big");
5253 ARY_SET_LEN(ary2,
len);
5258 ary_memcpy(ary2, 0, t, ptr);
5259 while (t <=
len/2) {
5336recursive_equal(
VALUE ary1,
VALUE ary2,
int recur)
5339 const VALUE *p1, *p2;
5341 if (recur)
return Qtrue;
5348 for (i = 0; i < len1; i++) {
5396 if (ary1 == ary2)
return Qtrue;
5409recursive_eql(
VALUE ary1,
VALUE ary2,
int recur)
5413 if (recur)
return Qtrue;
5415 if (!
rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
5443 if (ary1 == ary2)
return Qtrue;
5451ary_hash_values(
long len,
const VALUE *elements,
const VALUE ary)
5459 for (i=0; i<
len; i++) {
5460 n = rb_hash(elements[i]);
5472rb_ary_hash_values(
long len,
const VALUE *elements)
5474 return ary_hash_values(
len, elements, 0);
5493rb_ary_hash(
VALUE ary)
5544recursive_cmp(
VALUE ary1,
VALUE ary2,
int recur)
5548 if (recur)
return Qundef;
5553 for (i=0; i<
len; i++) {
5554 VALUE e1 = rb_ary_elt(ary1, i), e2 = rb_ary_elt(ary2, i);
5555 VALUE v = rb_funcallv(e1, id_cmp, 1, &e2);
5609 if (ary1 == ary2)
return INT2FIX(0);
5611 if (!UNDEF_P(v))
return v;
5625 rb_hash_add_new_element(hash, elt, elt);
5631ary_tmp_hash_new(
VALUE ary)
5634 VALUE hash = rb_hash_new_with_size(size);
5636 RBASIC_CLEAR_CLASS(hash);
5641ary_make_hash(
VALUE ary)
5643 VALUE hash = ary_tmp_hash_new(ary);
5644 return ary_add_hash(hash, ary);
5654 rb_hash_add_new_element(hash, k, v);
5660ary_make_hash_by(
VALUE ary)
5662 VALUE hash = ary_tmp_hash_new(ary);
5663 return ary_add_hash_by(hash, ary);
5691 ary2 = to_ary(ary2);
5692 if (
RARRAY_LEN(ary2) == 0) {
return ary_make_shared_copy(ary1); }
5697 VALUE elt = rb_ary_elt(ary1, i);
5698 if (rb_ary_includes_by_eql(ary2, elt))
continue;
5704 hash = ary_make_hash(ary2);
5706 if (rb_hash_stlike_lookup(hash,
RARRAY_AREF(ary1, i), NULL))
continue;
5733rb_ary_difference_multi(
int argc,
VALUE *argv,
VALUE ary)
5738 bool *is_hash =
ALLOCV_N(
bool, t0, argc);
5742 for (i = 0; i < argc; i++) {
5743 argv[i] = to_ary(argv[i]);
5744 is_hash[i] = (length > SMALL_ARRAY_LEN &&
RARRAY_LEN(argv[i]) > SMALL_ARRAY_LEN);
5745 if (is_hash[i]) argv[i] = ary_make_hash(argv[i]);
5750 VALUE elt = rb_ary_elt(ary, i);
5751 for (j = 0; j < argc; j++) {
5753 if (rb_hash_stlike_lookup(argv[j], elt, NULL))
5757 if (rb_ary_includes_by_eql(argv[j], elt))
break;
5796 VALUE hash, ary3, v;
5800 ary2 = to_ary(ary2);
5807 if (!rb_ary_includes_by_eql(ary2, v))
continue;
5808 if (rb_ary_includes_by_eql(ary3, v))
continue;
5814 hash = ary_make_hash(ary2);
5819 if (rb_hash_stlike_delete(hash, &vv, 0)) {
5849rb_ary_intersection_multi(
int argc,
VALUE *argv,
VALUE ary)
5854 for (i = 0; i < argc; i++) {
5855 result = rb_ary_and(result, argv[i]);
5862ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
5864 if (existing)
return ST_STOP;
5865 *key = *value = (
VALUE)arg;
5874 VALUE elt = rb_ary_elt(ary, i);
5875 if (rb_ary_includes_by_eql(ary_union, elt))
continue;
5886 if (!rb_hash_stlike_update(hash, (st_data_t)elt, ary_hash_orset, (st_data_t)elt)) {
5912 ary2 = to_ary(ary2);
5915 rb_ary_union(ary3, ary1);
5916 rb_ary_union(ary3, ary2);
5920 hash = ary_make_hash(ary1);
5921 rb_ary_union_hash(hash, ary2);
5923 return rb_hash_values(hash);
5950rb_ary_union_multi(
int argc,
VALUE *argv,
VALUE ary)
5957 for (i = 0; i < argc; i++) {
5958 argv[i] = to_ary(argv[i]);
5962 if (sum <= SMALL_ARRAY_LEN) {
5965 rb_ary_union(ary_union, ary);
5966 for (i = 0; i < argc; i++) rb_ary_union(ary_union, argv[i]);
5971 hash = ary_make_hash(ary);
5972 for (i = 0; i < argc; i++) rb_ary_union_hash(hash, argv[i]);
5974 return rb_hash_values(hash);
5994 VALUE hash, v, result, shorter, longer;
5998 ary2 = to_ary(ary2);
6004 if (rb_ary_includes_by_eql(ary2, v))
return Qtrue;
6016 hash = ary_make_hash(shorter);
6022 if (rb_hash_stlike_lookup(hash, vv, 0)) {
6032ary_max_generic(
VALUE ary,
long i,
VALUE vmax)
6040 if (rb_cmpint(rb_funcallv(vmax, id_cmp, 1, &v), vmax, v) < 0) {
6049ary_max_opt_fixnum(
VALUE ary,
long i,
VALUE vmax)
6056 for (; i < n; ++i) {
6060 if ((
long)vmax < (
long)v) {
6065 return ary_max_generic(ary, i, vmax);
6073ary_max_opt_float(
VALUE ary,
long i,
VALUE vmax)
6080 for (; i < n; ++i) {
6084 if (rb_float_cmp(vmax, v) < 0) {
6089 return ary_max_generic(ary, i, vmax);
6097ary_max_opt_string(
VALUE ary,
long i,
VALUE vmax)
6104 for (; i < n; ++i) {
6113 return ary_max_generic(ary, i, vmax);
6176 return rb_nmin_run(ary, num, 0, 1, 1);
6182 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) > 0) {
6190 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6191 return ary_max_opt_fixnum(ary, 1, result);
6193 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6194 return ary_max_opt_string(ary, 1, result);
6197 return ary_max_opt_float(ary, 1, result);
6200 return ary_max_generic(ary, 1, result);
6204 if (UNDEF_P(result))
return Qnil;
6209ary_min_generic(
VALUE ary,
long i,
VALUE vmin)
6217 if (rb_cmpint(rb_funcallv(vmin, id_cmp, 1, &v), vmin, v) > 0) {
6226ary_min_opt_fixnum(
VALUE ary,
long i,
VALUE vmin)
6233 for (; i < n; ++i) {
6237 if ((
long)vmin > (
long)a) {
6242 return ary_min_generic(ary, i, vmin);
6250ary_min_opt_float(
VALUE ary,
long i,
VALUE vmin)
6257 for (; i < n; ++i) {
6261 if (rb_float_cmp(vmin, a) > 0) {
6266 return ary_min_generic(ary, i, vmin);
6274ary_min_opt_string(
VALUE ary,
long i,
VALUE vmin)
6281 for (; i < n; ++i) {
6290 return ary_min_generic(ary, i, vmin);
6353 return rb_nmin_run(ary, num, 0, 0, 1);
6359 if (UNDEF_P(result) || rb_cmpint(
rb_yield_values(2, v, result), v, result) < 0) {
6367 if (
FIXNUM_P(result) && CMP_OPTIMIZABLE(INTEGER)) {
6368 return ary_min_opt_fixnum(ary, 1, result);
6370 else if (STRING_P(result) && CMP_OPTIMIZABLE(STRING)) {
6371 return ary_min_opt_string(ary, 1, result);
6374 return ary_min_opt_float(ary, 1, result);
6377 return ary_min_generic(ary, 1, result);
6381 if (UNDEF_P(result))
return Qnil;
6408rb_ary_minmax(
VALUE ary)
6413 return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary));
6417push_value(st_data_t key, st_data_t val, st_data_t ary)
6451rb_ary_uniq_bang(
VALUE ary)
6456 rb_ary_modify_check(ary);
6460 hash = ary_make_hash_by(ary);
6462 hash = ary_make_hash(ary);
6468 rb_ary_modify_check(ary);
6469 ARY_SET_LEN(ary, 0);
6470 if (ARY_SHARED_P(ary)) {
6471 rb_ary_unshare(ary);
6474 ary_resize_capa(ary, hash_size);
6507rb_ary_uniq(
VALUE ary)
6516 hash = ary_make_hash_by(ary);
6517 uniq = rb_hash_values(hash);
6520 hash = ary_make_hash(ary);
6521 uniq = rb_hash_values(hash);
6544rb_ary_compact_bang(
VALUE ary)
6561 ary_resize_smaller(ary, n);
6581rb_ary_compact(
VALUE ary)
6584 rb_ary_compact_bang(ary);
6616rb_ary_count(
int argc,
VALUE *argv,
VALUE ary)
6632 VALUE obj = argv[0];
6635 rb_warn(
"given block not used");
6646flatten(
VALUE ary,
int level)
6649 VALUE stack, result, tmp = 0, elt;
6665 ARY_SET_LEN(result, i);
6667 stack = ary_new(0, ARY_DEFAULT_SIZE);
6673 rb_hash_aset(memo, ary,
Qtrue);
6674 rb_hash_aset(memo, tmp,
Qtrue);
6683 if (level >= 0 &&
RARRAY_LEN(stack) / 2 >= level) {
6688 if (
RBASIC(result)->klass) {
6690 rb_hash_clear(memo);
6699 if (rb_hash_aref(memo, tmp) ==
Qtrue) {
6700 rb_hash_clear(memo);
6701 rb_raise(rb_eArgError,
"tried to flatten recursive array");
6703 rb_hash_aset(memo, tmp,
Qtrue);
6715 rb_hash_delete(memo, ary);
6723 rb_hash_clear(memo);
6766rb_ary_flatten_bang(
int argc,
VALUE *argv,
VALUE ary)
6768 int mod = 0, level = -1;
6772 rb_ary_modify_check(ary);
6774 if (level == 0)
return Qnil;
6776 result = flatten(ary, level);
6777 if (result == ary) {
6782 if (mod) ARY_SET_EMBED_LEN(result, 0);
6823rb_ary_flatten(
int argc,
VALUE *argv,
VALUE ary)
6830 if (level == 0)
return ary_make_shared_copy(ary);
6833 result = flatten(ary, level);
6834 if (result == ary) {
6835 result = ary_make_shared_copy(ary);
6841#define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1)
6852 long j = RAND_UPTO(i);
6869 rb_ary_shuffle_bang(ec, ary, randgen);
6885 long n,
len, i, j, k, idx[10];
6886 long rnds[numberof(idx)];
6887 long memo_threshold;
6896 return rb_ary_elt(ary, i);
6899 if (n < 0) rb_raise(rb_eArgError,
"negative sample number");
6901 if (n <= numberof(idx)) {
6902 for (i = 0; i < n; ++i) {
6903 rnds[i] = RAND_UPTO(
len - i);
6908 if (
len < k && n <= numberof(idx)) {
6909 for (i = 0; i < n; ++i) {
6919 return rb_ary_new_from_args(1,
RARRAY_AREF(ary, i));
6931 if (j >= i) l = i, g = ++j;
6932 if (k >= l && (++k >= g)) ++k;
6941 if (n <= numberof(idx)) {
6942 long sorted[numberof(idx)];
6943 sorted[0] = idx[0] = rnds[0];
6944 for (i=1; i<n; i++) {
6946 for (j = 0; j < i; ++j) {
6947 if (k < sorted[j])
break;
6950 memmove(&sorted[j+1], &sorted[j],
sizeof(sorted[0])*(i-j));
6951 sorted[j] = idx[i] = k;
6955 for (i=0; i<n; i++) {
6960 else if (n <= memo_threshold / 2) {
6963 st_table *memo = st_init_numtable_with_size(n);
6967 for (i=0; i<n; i++) {
6968 long r = RAND_UPTO(
len-i) + i;
6970 if (r > max_idx) max_idx = r;
6973 if (
len <= max_idx) n = 0;
6974 else if (n >
len) n =
len;
6976 for (i=0; i<n; i++) {
6977 long j2 = j = ptr_result[i];
6980 if (st_lookup(memo, (st_data_t)i, &value)) i2 = (
long)value;
6981 if (st_lookup(memo, (st_data_t)j, &value)) j2 = (
long)value;
6982 st_insert(memo, (st_data_t)j, (st_data_t)i2);
6983 ptr_result[i] = ptr_ary[j2];
6988 st_free_table(memo);
6993 RBASIC_CLEAR_CLASS(result);
6996 for (i=0; i<n; i++) {
6997 j = RAND_UPTO(
len-i) + i;
6999 ptr_result[j] = ptr_result[i];
7003 RBASIC_SET_CLASS_RAW(result,
rb_cArray);
7005 ARY_SET_LEN(result, n);
7033 if (mul <= 0)
return INT2FIX(0);
7035 return rb_fix_mul_fix(rb_ary_length(self), n);
7072rb_ary_cycle(
int argc,
VALUE *argv,
VALUE ary)
7079 if (argc == 0 ||
NIL_P(argv[0])) {
7084 if (n <= 0)
return Qnil;
7087 while (
RARRAY_LEN(ary) > 0 && (n < 0 || 0 < n--)) {
7101yield_indexed_values(
const VALUE values,
const long r,
const long *
const p)
7106 for (i = 0; i < r; i++) ARY_SET(result, i,
RARRAY_AREF(values, p[i]));
7107 ARY_SET_LEN(result, r);
7109 return !
RBASIC(values)->klass;
7125permute0(
const long n,
const long r,
long *
const p,
char *
const used,
const VALUE values)
7127 long i = 0, index = 0;
7130 const char *
const unused = memchr(&used[i], 0, n-i);
7145 for (i = 0; i < n; ++i) {
7146 if (used[i])
continue;
7148 if (!yield_indexed_values(values, r, p)) {
7164descending_factorial(
long from,
long how_many)
7169 while (--how_many > 0) {
7171 cnt = rb_int_mul(cnt,
LONG2FIX(v));
7181binomial_coefficient(
long comb,
long size)
7185 if (comb > size-comb) {
7191 else if (comb == 0) {
7195 for (i = 1; i < comb; ++i) {
7196 r = rb_int_mul(r,
LONG2FIX(size - i));
7197 r = rb_int_idiv(r,
LONG2FIX(i + 1));
7208 return descending_factorial(n, k);
7254rb_ary_permutation(
int argc,
VALUE *argv,
VALUE ary)
7264 if (r < 0 || n < r) {
7277 long *p =
ALLOCV_N(
long, t0, r+roomof(n,
sizeof(
long)));
7278 char *used = (
char*)(p + r);
7279 VALUE ary0 = ary_make_shared_copy(ary);
7280 RBASIC_CLEAR_CLASS(ary0);
7284 permute0(n, r, p, used, ary0);
7292combinate0(
const long len,
const long n,
long *
const stack,
const VALUE values)
7299 for (lev++; lev < n; lev++) {
7300 stack[lev+1] = stack[lev]+1;
7302 if (!yield_indexed_values(values, n, stack+1)) {
7306 if (lev == 0)
return;
7308 }
while (stack[lev+1]+n ==
len+lev+1);
7318 return binomial_coefficient(k, n);
7373 if (n < 0 ||
len < n) {
7385 VALUE ary0 = ary_make_shared_copy(ary);
7387 long *stack =
ALLOCV_N(
long, t0, n+1);
7389 RBASIC_CLEAR_CLASS(ary0);
7390 combinate0(
len, n, stack, ary0);
7410rpermute0(
const long n,
const long r,
long *
const p,
const VALUE values)
7412 long i = 0, index = 0;
7416 if (++index < r-1) {
7420 for (i = 0; i < n; ++i) {
7422 if (!yield_indexed_values(values, r, p)) {
7427 if (index <= 0)
return;
7428 }
while ((i = ++p[--index]) >= n);
7486rb_ary_repeated_permutation(
VALUE ary,
VALUE num)
7508 VALUE ary0 = ary_make_shared_copy(ary);
7509 RBASIC_CLEAR_CLASS(ary0);
7511 rpermute0(n, r, p, ary0);
7519rcombinate0(
const long n,
const long r,
long *
const p,
const long rest,
const VALUE values)
7521 long i = 0, index = 0;
7525 if (++index < r-1) {
7529 for (; i < n; ++i) {
7531 if (!yield_indexed_values(values, r, p)) {
7536 if (index <= 0)
return;
7537 }
while ((i = ++p[--index]) >= n);
7549 return binomial_coefficient(k, n + k - 1);
7592rb_ary_repeated_combination(
VALUE ary,
VALUE num)
7610 else if (
len == 0) {
7616 VALUE ary0 = ary_make_shared_copy(ary);
7617 RBASIC_CLEAR_CLASS(ary0);
7619 rcombinate0(
len, n, p, n, ary0);
7680rb_ary_product(
int argc,
VALUE *argv,
VALUE ary)
7686 int *counters =
ALLOCV_N(
int, t1, n);
7691 RBASIC_CLEAR_CLASS(t0);
7696 for (i = 1; i < n; i++) arrays[i] =
Qnil;
7697 for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
7700 for (i = 0; i < n; i++) counters[i] = 0;
7705 for (i = 0; i < n; i++) {
7707 arrays[i] = ary_make_shared_copy(arrays[i]);
7712 for (i = 0; i < n; i++) {
7718 if (MUL_OVERFLOW_LONG_P(resultlen, k))
7728 for (j = 0; j < n; j++) {
7733 if (
NIL_P(result)) {
7734 FL_SET(t0, RARRAY_SHARED_ROOT_FLAG);
7736 if (!
FL_TEST(t0, RARRAY_SHARED_ROOT_FLAG)) {
7740 FL_UNSET(t0, RARRAY_SHARED_ROOT_FLAG);
7753 while (counters[m] ==
RARRAY_LEN(arrays[m])) {
7756 if (--m < 0)
goto done;
7764 return NIL_P(result) ? ary : result;
7790 rb_raise(rb_eArgError,
"attempt to take negative size");
7817rb_ary_take_while(
VALUE ary)
7825 return rb_ary_take(ary,
LONG2FIX(i));
7853 rb_raise(rb_eArgError,
"attempt to drop negative size");
7880rb_ary_drop_while(
VALUE ary)
7888 return rb_ary_drop(ary,
LONG2FIX(i));
7931rb_ary_any_p(
int argc,
VALUE *argv,
VALUE ary)
7939 rb_warn(
"given block not used");
7946 for (i = 0; i <
len; ++i) {
7998rb_ary_all_p(
int argc,
VALUE *argv,
VALUE ary)
8006 rb_warn(
"given block not used");
8013 for (i = 0; i <
len; ++i) {
8059rb_ary_none_p(
int argc,
VALUE *argv,
VALUE ary)
8067 rb_warn(
"given block not used");
8074 for (i = 0; i <
len; ++i) {
8123rb_ary_one_p(
int argc,
VALUE *argv,
VALUE ary)
8132 rb_warn(
"given block not used");
8136 if (result)
return Qfalse;
8142 for (i = 0; i <
len; ++i) {
8144 if (result)
return Qfalse;
8152 if (result)
return Qfalse;
8184 self = rb_ary_at(self, *argv);
8185 if (!--argc)
return self;
8187 return rb_obj_dig(argc, argv, self,
Qnil);
8191finish_exact_sum(
long n,
VALUE r,
VALUE v,
int z)
8196 v = rb_rational_plus(r, v);
8264 goto init_is_a_value;
8278 else if (RB_BIGNUM_TYPE_P(e))
8279 v = rb_big_plus(e, v);
8284 r = rb_rational_plus(r, e);
8289 v = finish_exact_sum(n, r, v, argc!=0);
8293 v = finish_exact_sum(n, r, v, i!=0);
8305 goto has_float_value;
8315 else if (RB_BIGNUM_TYPE_P(e))
8322 if (isnan(f))
continue;
8328 if (isinf(f) && signbit(x) != signbit(f))
8334 if (isinf(f))
continue;
8337 if (fabs(f) >= fabs(x))
8350 goto has_some_value;
8364rb_ary_deconstruct(
VALUE ary)
8875 fake_ary_flags = init_fake_ary_flags();
9005 rb_vm_register_global_object(rb_cArray_empty_frozen);
9008#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 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.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
VALUE rb_cArray
Array class.
VALUE rb_cObject
Object class.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Identical to rb_class_new_instance(), except it passes the passed keywords if any to the #initialize ...
VALUE rb_obj_frozen_p(VALUE obj)
Just calls RB_OBJ_FROZEN() inside.
int rb_eql(VALUE lhs, VALUE rhs)
Checks for equality of the passed objects, in terms of Object#eql?.
VALUE rb_cNumeric
Numeric class.
VALUE rb_cRandom
Random class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
double rb_num2dbl(VALUE num)
Converts an instance of rb_cNumeric into C's double.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_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().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_output_fs
The field separator character for outputs, or the $,.
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
VALUE rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
Deconstructs a numerical range.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
#define rb_hash_end(h)
Just another name of st_hash_end.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
#define rb_usascii_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "US ASCII" encoding.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
int capa
Designed capacity of the buffer.
int len
Length of the buffer.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY(obj)
Convenient casting macro.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
static VALUE * RARRAY_PTR(VALUE ary)
Wild use of a C pointer.
#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 RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
#define RTEST
This is an old name of RB_TEST.
union RArray::@47 as
Array's specific fields.
struct RBasic basic
Basic part, including flags and class.
struct RArray::@47::@48 heap
Arrays that use separated memory region for elements use this pattern.
const VALUE shared_root
Parent of the array.
const VALUE ary[1]
Embedded elements.
long capa
Capacity of *ptr.
union RArray::@47::@48::@49 aux
Auxiliary info.
long len
Number of elements of the array.
const VALUE * ptr
Pointer to the C array that holds the elements of the array.
VALUE flags
Per-object flags.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.